Boiling water and salt

Claude Code · claude-sonnet-5-5

Round 1

Messages

Instructions
Scientific tasks protocol v2 — human-in-the-loop scientific workflow.

1. Formulate an appropriate mathematical model yourself. Separate facts supplied by the user from choices or extra assumptions you introduce. Prefer deriving or estimating model parameters from the supplied observations and, when useful, requested experiments. State and justify the model family and observation model needed for that inference. For example, assume and justify a distribution family explicitly, then derive its parameters from the supplied moments, observations or experiment results; do not assume both the family and a complete set of parameter values when those values are what the task requires you to infer. Do not choose convenient parameter values and merely show that they reproduce the observations, presenting that exercise as inference. If the observations do not determine the parameters, expose what remains undetermined and what additional information or assumptions would be needed. Do not silently turn population moments into a fully specified prior. Do not assume the conclusion you are asked to establish as a premise.
2. If the information does not determine a unique answer, explain why and give a conditional answer with its assumptions exposed. You may revise the model and conclusion across attempts; preserve the reasons and lineage.
3. Write a substantive Lean proof covering the scientific inference needed to answer the question, connecting the supplied information and explicit modeling choices to the claimed result. Use Bayesian inference where appropriate; it is not mandatory for every task. A proof of arithmetic for a number calculated elsewhere is insufficient. The human will assess scientific relevance and whether the proof covers the actual inference.
4. Use available tools, including Internet research. Request literature, calculations, experiments or clarification from the operator when useful; a request is optional. Use the request tool below, stop for a real response, and incorporate the response with its source and limitations. Never invent an external response.
5. Run the actual check tool. Read and fix its real errors, retrying without replacing science with a smaller unrelated theorem. Preserve model changes and raw traces.
6. You may introduce additional premises or axioms, but expose them for review. Any sorry or additional axiom is conditional, never an unconditional full pass. Lean correctness does not establish the truth of the premises.
7. Extract definitions, constants, data, assumptions and the exact conclusions. The harness independently compiles and compares the extracted statements and runs a separate kernel check. A failed or unknown check is not success.
8. A human reviews the actual content and may accept or reject it with feedback. If the researcher rejects the assumptions or conclusions, revise your model, assumptions, conclusions and proof in response to the feedback. Never accept your own result or claim human approval.

Human-readability requirements:
- Write the formalization so a human scientist can understand what is assumed and what is concluded, with a concise plain-language translation alongside the relevant Lean statements. Explain the real-world meaning of the model and the reason for each scientific modeling choice.
- Keep the assumptions, scientifically meaningful definitions and final conclusions as small and direct as possible. Avoid redundant definitions, duplicate specializations of the same formula, synonyms, and layers of wrappers that merely rename an expression. Retain a helper definition only when it materially improves clarity or is mathematically necessary.
- Put derivable facts and technical work into intermediate lemmas or proof-local helpers: algebra, normalization, parameter calculations and other consequences should be proved rather than added to the review assumptions. Keep the final conclusion focused on the actual scientific answer.
- Minimizing the review boundary must preserve its meaning: expose every independent modeling choice and every definition needed to interpret the conclusion. Do not hide assumptions in intermediate lemmas, use unproved facts as lemmas, or shorten the conclusion into an unexplained proposition alias.
- Before submission, simplify the public statement and its dependency chain, and give a compact review section separating supplied information, additional assumptions, necessary definitions and proved conclusions. Definitions introduced solely for proof organization should stay inside the proof when possible. Do not optimize for a short file at the cost of a longer or less intelligible assumptions/conclusions packet.

Isolation rule: work only from this task, these instructions, native installed libraries, and the run's own history and supplied responses. Do not seek previous solutions, task-specific proofs, or preset model families, parameter values or expected answers from earlier runs. You may consult general scientific or statistical literature to select and justify a model family. Authentication metadata is mounted only because the native subscription client needs its existing login; it is not scientific evidence. Do not read, print, copy or modify authentication files. Never use paid APIs, API-key fallback, other agents, or external services requiring payment. Stop on subscription quota/authentication errors.

Tools (run in the terminal):
  python /harness/src/vcs_protocol.py tool check --manifest /work/candidate.json
  python /harness/src/vcs_protocol.py tool request --kind literature --question '...'
Kinds are literature, calculation, experiment, clarification. A request stops this cycle; the operator supplies an answer and resumes. No physical measurement device is connected. You may request a real measurement; do not report an unperformed measurement. Any synthetic calculation or simulated experiment must be explicitly labeled as such and is evidence about its chosen model, not a measurement of the physical system. If no measurement is available, provide a scientifically useful conditional prediction or range with the missing information and assumptions stated, rather than claiming an unconditional numerical answer. Record incorporated response IDs in the candidate. Evidence is user/external supplied and not guaranteed true.

Write /work/candidate.json with this shape (all paths relative to /work):
{"proof_file":"Model.lean", "narrative_file":"report.txt", "conclusions":{"answer":"answer_theorem"}, "user_facts":["..."], "model_choices":["..."], "sources":["..."], "incorporated_inputs":[], "proof_scope":"What the theorems actually establish and what is left to human review"}
List only the final scientific conclusion or conclusions needed to answer the task, with nonempty labels mapped to actual, distinct Lean theorem declarations. A single answer theorem is allowed. Supporting model, inference, normalization and other derived facts may be intermediate lemmas; there are no mandatory model/update/prediction conclusion roles. Use plain top-level `theorem name : statement := proof` declarations, with all binders inside `statement`. Imports must be explicit `import Mathlib.…` lines at the start of the file; imports of project/task files are unavailable. The restricted proof language supports definitions, structures, lemmas, axioms and sorry but no executable metaprogramming, custom syntax, namespaces, native_decide, IO or unsafe code. Check feedback gives concrete unsupported syntax errors. Native Mathlib tactics are available. Explain any restriction that prevents a scientifically meaningful formalization; do not pretend a narrower result meets the task.
Task
I want to boil saline water. I have poured 1 litre of water at 20 degrees Celsius into a pot and started heating it on a stove. I will add salt, and want the final salted water to reach its boiling point as quickly as possible. The salt quantity, pot, heater and room conditions have not otherwise been specified. At what water temperature, or after how much heating time, should I add the salt to make the salted water boil sooner?
Bash input
{
  "command": "cd /work; ls -la; cat *.txt 2>/dev/null | head -50"
}
Bash output
total 8
drwxrwxr-x 2 1001 1001 4096 Nov 16 18:26 .
drwxr-xr-x 1 root root 4096 Nov 16 18:26 ..
Bash input
{
  "command": "cd /work; cat > Model.lean <<'EOF'\nimport Mathlib.Analysis.SpecialFunctions.Integrals\nimport Mathlib.MeasureTheory.Integral.IntervalIntegral.Basic\n\n/-! Model: enthalpy coordinate H (J, measured from the state \"everything at room temperature Tr\").\n Pure water (heat capacity Cw) has temperature Tr + H/Cw.\n Salted water (heat capacity Cw+Cs, constant endothermic dissolution enthalpy D ≥ 0) has\n temperature Tr + (H-D)/(Cw+Cs).  Salt (at Tr) is stirred in instantly: H is conserved.\n Heater delivers constant power P, losses are Newtonian h*(T-Tr), so dH/dt = P - h*(T-Tr)\n and the time to go from H1 to H2 is ∫ dH / (P - h*(T(H)-Tr)).\n Tb = boiling point of the FINAL salt solution: fixed by the salt quantity, not by timing. -/\n\n/-- Total time to reach Tb when the salt is added once the water has reached temperature Ta. -/\nnoncomputable def boilTime (Cw Cs D P h Tr T0 Tb Ta : ℝ) : ℝ :=\n  (∫ H in (Cw*(T0-Tr))..(Cw*(Ta-Tr)), 1/(P - h*(H/Cw))) +\n  ∫ H in (Cw*(Ta-Tr))..((Cw+Cs)*(Tb-Tr)+D), 1/(P - h*((H-D)/(Cw+Cs)))\n\nlemma boilTime_mono (Cw Cs D P h Tr T0 Tb Ta1 Ta2 : ℝ)\n    (hCw : 0 < Cw) (hCs : 0 ≤ Cs) (hD : 0 ≤ D) (hh : 0 ≤ h)\n    (hP : h*(Tb-Tr) < P) (hTr : Tr ≤ T0) (h1 : T0 ≤ Ta1) (h12 : Ta1 ≤ Ta2) (h2 : Ta2 ≤ Tb) :\n    boilTime Cw Cs D P h Tr T0 Tb Ta1 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta2 := by\n  have hC : 0 < Cw + Cs := by linarith\n  set c := Cw*(T0-Tr) with hc\n  set a := Cw*(Ta1-Tr) with ha\n  set b := Cw*(Ta2-Tr) with hb\n  set e := (Cw+Cs)*(Tb-Tr)+D with he\n  have hca : c ≤ a := by rw [hc, ha]; nlinarith\n  have hab : a ≤ b := by rw [ha, hb]; nlinarith\n  have hbe : b ≤ e := by\n    rw [hb, he]; nlinarith [mul_nonneg hCs (show 0 ≤ Tb - Tr by linarith)]\n  have ha0 : 0 ≤ a := by rw [ha]; nlinarith\n  -- positivity of denominators\n  have hpw : ∀ H, H ≤ b → 0 < P - h*(H/Cw) := by\n    intro H hH\n    have : H/Cw ≤ Tb - Tr := by\n      rw [div_le_iff₀ hCw]; rw [hb] at hH; nlinarith\n    nlinarith\n  have hps : ∀ H, H ≤ e → 0 < P - h*((H-D)/(Cw+Cs)) := by\n    intro H hH\n    have : (H-D)/(Cw+Cs) ≤ Tb - Tr := by\n      rw [div_le_iff₀ hC]; rw [he] at hH; nlinarith\n    nlinarith\n  have cw : ContinuousOn (fun H : ℝ => 1/(P - h*(H/Cw))) (Set.Icc c b) := by\n    apply ContinuousOn.div continuousOn_const\n    · fun_prop\n    · intro H hH; exact (hpw H hH.2).ne'\n  have cs : ContinuousOn (fun H : ℝ => 1/(P - h*((H-D)/(Cw+Cs)))) (Set.Icc a e) := by\n    apply ContinuousOn.div continuousOn_const\n    · fun_prop\n    · intro H hH; exact (hps H hH.2).ne'\n  have iw : ∀ x y, c ≤ x → x ≤ y → y ≤ b →\n      IntervalIntegrable (fun H : ℝ => 1/(P - h*(H/Cw))) MeasureTheory.volume x y := by\n    intro x y hx hxy hy\n    apply ContinuousOn.intervalIntegrable\n    rw [Set.uIcc_of_le hxy]\n    exact cw.mono (Set.Icc_subset_Icc hx hy)\n  have is : ∀ x y, a ≤ x → x ≤ y → y ≤ e →\n      IntervalIntegrable (fun H : ℝ => 1/(P - h*((H-D)/(Cw+Cs)))) MeasureTheory.volume x y := by\n    intro x y hx hxy hy\n    apply ContinuousOn.intervalIntegrable\n    rw [Set.uIcc_of_le hxy]\n    exact cs.mono (Set.Icc_subset_Icc hx hy)\n  unfold boilTime\n  have s1 := intervalIntegral.integral_add_adjacent_intervals\n    (iw c a hc.le hca (hab.trans le_rfl)) (iw a b hca hab le_rfl)\n  have s2 := intervalIntegral.integral_add_adjacent_intervals\n    (is a b le_rfl hab hbe) (is b e hab le_rfl le_rfl)\n  have key : (∫ H in a..b, 1/(P - h*((H-D)/(Cw+Cs)))) ≤ ∫ H in a..b, 1/(P - h*(H/Cw)) := by\n    apply intervalIntegral.integral_mono_on hab (is a b le_rfl hab hbe) (iw a b hca hab le_rfl)\n    intro H hH\n    have hpos := hpw H hH.2\n    apply one_div_le_one_div_of_le hpos\n    have : (H-D)/(Cw+Cs) ≤ H/Cw := by\n      rw [div_le_div_iff₀ hC hCw]\n      nlinarith [mul_nonneg hCs (show 0 ≤ H by linarith [hH.1]), mul_nonneg hCw.le hD]\n    nlinarith\n  rw [← s1, ← s2]\n  linarith\n\n/-- ANSWER.  Under the model, the time to boil never decreases if the salt is added later\n(at a hotter water temperature Ta ∈ [T0, Tb]); hence adding it at the start (Ta = T0 = 20 °C)\nis optimal. -/\ntheorem answer_theorem : ∀ (Cw Cs D P h Tr T0 Tb : ℝ),\n    0 < Cw → 0 ≤ Cs → 0 ≤ D → 0 ≤ h → h*(Tb-Tr) < P → Tr ≤ T0 → T0 ≤ Tb →\n    (∀ Ta1 Ta2 : ℝ, T0 ≤ Ta1 → Ta1 ≤ Ta2 → Ta2 ≤ Tb →\n        boilTime Cw Cs D P h Tr T0 Tb Ta1 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta2) ∧\n    (∀ Ta : ℝ, T0 ≤ Ta → Ta ≤ Tb →\n        boilTime Cw Cs D P h Tr T0 Tb T0 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta) := by\n  intro Cw Cs D P h Tr T0 Tb hCw hCs hD hh hP hTr hT\n  refine ⟨fun a b h1 h12 h2 => boilTime_mono _ _ _ _ _ _ _ _ _ _ hCw hCs hD hh hP hTr h1 h12 h2, ?_⟩\n  intro Ta hTa hTb\n  exact boilTime_mono _ _ _ _ _ _ _ _ _ _ hCw hCs hD hh hP hTr le_rfl hTa hTb\nEOF\ncat > report.txt <<'EOF'\nSee below (written after check).\nEOF\ncat > candidate.json <<'EOF'\n{\"proof_file\":\"Model.lean\",\"narrative_file\":\"report.txt\",\"conclusions\":{\"answer\":\"answer_theorem\"},\"user_facts\":[\"1 L of water at 20 C in a pot on a stove\",\"salt will be added; goal: salted water reaches its boiling point as soon as possible\",\"salt quantity, pot, heater, room unspecified\"],\"model_choices\":[\"constant heater power P; Newtonian loss h(T-Tr); lumped heat capacities\",\"salt added at room temperature, instantly mixed, enthalpy conserved\",\"salt raises heat capacity by Cs>=0, constant endothermic dissolution enthalpy D>=0\",\"final boiling point Tb depends on salt amount only, not on timing; P>h(Tb-Tr) so boiling is reachable\",\"room temperature Tr<=20 C; evaporation and boil-over ignored\"],\"sources\":[],\"incorporated_inputs\":[],\"proof_scope\":\"Proves, in the stated lumped model, that time to boil is monotone nondecreasing in the water temperature at which salt is added, so adding at the start is optimal. Does not prove strictness, nor magnitude of the gain; model premises are for human review.\"}\nEOF\npython /harness/src/vcs_protocol.py tool check --manifest /work/candidate.json 2>&1 | tail -40"
}
Bash output
{
  "ok": false,
  "check_complete": false,
  "reviewable": false,
  "verdict": "not_proved",
  "time": "2026-11-16T18:28:09.323996+00:00",
  "lean_exit": 1,
  "error": "Lean, kernel recheck or axiom audit failed; see lean.log"
}
Bash input
{
  "command": "cd /work; ls; find . -name lean.log | head; cat $(find . -name lean.log | head -1) | head -60"
}
Bash output
Model.lean
candidate.json
checks
report.txt
./checks/b7ec15d94a8146bfadfbd5cc72230a57/lean.log
/work/checks/b7ec15d94a8146bfadfbd5cc72230a57/Checked.lean:1:0: error: object file '/harness/.lake/packages/mathlib/.lake/build/lib/lean/Mathlib/Analysis/SpecialFunctions/Integrals.olean' of module Mathlib.Analysis.SpecialFunctions.Integrals does not exist
assistant
Import path doesn't exist; use a different import.
Bash input
{
  "command": "cd /work; ls /harness/.lake/packages/mathlib/.lake/build/lib/lean/Mathlib/Analysis/SpecialFunctions/ | head -30; ls /harness/.lake/packages/mathlib/.lake/build/lib/lean/Mathlib/MeasureTheory/Integral/ | head -30"
}
Bash output
Complex
Exp.ilean
Exp.ilean.hash
Exp.ir
Exp.ir.hash
Exp.ir.sig
Exp.ir.sig.hash
Exp.olean
Exp.olean.hash
Exp.olean.private
Exp.olean.private.hash
Exp.olean.server
Exp.olean.server.hash
Exp.trace
ExpDeriv.ilean
ExpDeriv.ilean.hash
ExpDeriv.ir
ExpDeriv.ir.hash
ExpDeriv.ir.sig
ExpDeriv.ir.sig.hash
ExpDeriv.olean
ExpDeriv.olean.hash
ExpDeriv.olean.private
ExpDeriv.olean.private.hash
ExpDeriv.olean.server
ExpDeriv.olean.server.hash
ExpDeriv.trace
Exponential.ilean
Exponential.ilean.hash
Exponential.ir
ls: cannot access '/harness/.lake/packages/mathlib/.lake/build/lib/lean/Mathlib/MeasureTheory/Integral/': No such file or directory
Bash input
{
  "command": "cd /harness/.lake/packages/mathlib/.lake/build/lib/lean/Mathlib; ls; ls MeasureTheory 2>&1 | head; ls Analysis; find . -name 'IntervalIntegral*.olean' | head"
}
Bash output
Algebra
Analysis
Basic
CategoryTheory
Combinatorics
Control
Data
Dynamics
FieldTheory
Geometry
GroupTheory
Init.ilean
Init.ilean.hash
Init.ir
Init.ir.hash
Init.ir.sig
Init.ir.sig.hash
Init.olean
Init.olean.hash
Init.olean.private
Init.olean.private.hash
Init.olean.server
Init.olean.server.hash
Init.trace
Lean
LinearAlgebra
Logic
NumberTheory
Order
RingTheory
SetTheory
Tactic
Topology
Util
ls: cannot access 'MeasureTheory': No such file or directory
Analytic
Asymptotics
CStarAlgebra
Calculus
Complex
Convex
InnerProductSpace
LocallyConvex
Normed
RCLike
Real
SpecialFunctions
SpecificLimits
Shell cwd was reset to /work
Bash input
{
  "command": "cd /work; cat > Model.lean <<'EOF'\nimport Mathlib.Analysis.SpecialFunctions.Log.Deriv\nimport Mathlib.Analysis.Calculus.MeanValue\n\n/-! Model (see report.txt).  H = enthalpy (J) measured from \"everything at room temperature Tr\".\n Pure water, heat capacity Cw: temperature Tr + H/Cw.\n Salt solution, heat capacity Cw+Cs, constant endothermic dissolution enthalpy D ≥ 0:\n temperature Tr + (H-D)/(Cw+Cs).  Salt (at Tr) is mixed in instantly, so H is conserved.\n Heater power P constant, loss h*(T-Tr):  dH/dt = P - h*(T-Tr).\n Separating variables, the time to move from H1 to H2 is Fw/Fs(H2) - Fw/Fs(H1) where\n Fw' = 1/(P - h*(H/Cw)), Fs' = 1/(P - h*((H-D)/(Cw+Cs))) (explicit logarithms below).\n Tb = boiling point of the final solution (fixed by salt amount, not by timing). -/\n\n/-- time-integral of dH/(P - loss) for pure water (antiderivative, h>0) -/\nnoncomputable def Fw (Cw P h H : ℝ) : ℝ := -(Cw/h) * Real.log (P - h*(H/Cw))\n/-- same for the salt solution -/\nnoncomputable def Fs (Cw Cs D P h H : ℝ) : ℝ := -((Cw+Cs)/h) * Real.log (P - h*((H-D)/(Cw+Cs)))\n\n/-- Total time to reach Tb from T0 when the salt is added once the water is at Ta:\n heating water alone from T0 to Ta, then solution from the post-mixing state up to Tb. -/\nnoncomputable def boilTime (Cw Cs D P h Tr T0 Tb Ta : ℝ) : ℝ :=\n  (Fw Cw P h (Cw*(Ta-Tr)) - Fw Cw P h (Cw*(T0-Tr))) +\n  (Fs Cw Cs D P h ((Cw+Cs)*(Tb-Tr)+D) - Fs Cw Cs D P h (Cw*(Ta-Tr)))\n\nlemma G_mono (Cw Cs D P h Tr Tb : ℝ)\n    (hCw : 0 < Cw) (hCs : 0 ≤ Cs) (hD : 0 ≤ D) (hh : 0 < h)\n    (hP : h*(Tb-Tr) < P) :\n    MonotoneOn (fun H => Fw Cw P h H - Fs Cw Cs D P h H) (Set.Icc 0 (Cw*(Tb-Tr))) := by\n  have hC : 0 < Cw + Cs := by linarith\n  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by\n    intro H hH\n    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]\n    nlinarith\n  have hle : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), (H-D)/(Cw+Cs) ≤ H/Cw := by\n    intro H hH\n    rw [div_le_div_iff₀ hC hCw]\n    nlinarith [mul_nonneg hCs hH.1, mul_nonneg hCw.le hD]\n  have hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs)) := by\n    intro H hH\n    have := hle H hH\n    have := hw H hH\n    nlinarith\n  have hd : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), HasDerivAt (fun H => Fw Cw P h H - Fs Cw Cs D P h H)\n      (1/(P - h*(H/Cw)) - 1/(P - h*((H-D)/(Cw+Cs)))) H := by\n    intro H hH\n    have a1 : HasDerivAt (fun H : ℝ => P - h*(H/Cw)) (-(h/Cw)) H := by\n      have := ((hasDerivAt_id H).div_const Cw).const_mul h\n      simpa [div_eq_mul_inv, mul_comm] using (this.const_sub P)\n    have a2 : HasDerivAt (fun H : ℝ => P - h*((H-D)/(Cw+Cs))) (-(h/(Cw+Cs))) H := by\n      have := (((hasDerivAt_id H).sub_const D).div_const (Cw+Cs)).const_mul h\n      simpa [div_eq_mul_inv, mul_comm] using (this.const_sub P)\n    have l1 := (a1.log (hw H hH).ne').const_mul (-(Cw/h))\n    have l2 := (a2.log (hs H hH).ne').const_mul (-((Cw+Cs)/h))\n    have := l1.sub l2\n    convert this using 1\n    · funext x; rfl\n    · have e1 := (hw H hH).ne'\n      have e2 := (hs H hH).ne'\n      have e3 := hh.ne'\n      have e4 := hCw.ne'\n      have e5 := hC.ne'\n      field_simp\n  apply monotoneOn_of_deriv_nonneg (convex_Icc _ _)\n  · intro H hH; exact (hd H hH).continuousAt.continuousWithinAt\n  · intro H hH; exact (hd H (interior_subset hH)).differentiableAt.differentiableWithinAt\n  · intro H hH\n    have hH' := interior_subset hH\n    rw [(hd H hH').deriv]\n    have := hle H hH'\n    have p1 := hw H hH'\n    apply sub_nonneg.mpr\n    apply one_div_le_one_div_of_le p1\n    nlinarith\n\n/-- ANSWER.  In the model, with salt of fixed amount (Cs, D, Tb fixed), the time to boil\nnever decreases if the salt is added later, i.e. at a hotter water temperature Ta ∈ [T0, Tb];\nhence adding it at the very start (Ta = T0 = 20 °C) is optimal. -/\ntheorem answer_theorem : ∀ (Cw Cs D P h Tr T0 Tb : ℝ),\n    0 < Cw → 0 ≤ Cs → 0 ≤ D → 0 < h → h*(Tb-Tr) < P → Tr ≤ T0 →\n    (∀ Ta1 Ta2 : ℝ, T0 ≤ Ta1 → Ta1 ≤ Ta2 → Ta2 ≤ Tb →\n        boilTime Cw Cs D P h Tr T0 Tb Ta1 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta2) ∧\n    (∀ Ta : ℝ, T0 ≤ Ta → Ta ≤ Tb →\n        boilTime Cw Cs D P h Tr T0 Tb T0 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta) := by\n  intro Cw Cs D P h Tr T0 Tb hCw hCs hD hh hP hTr\n  have mono : ∀ Ta1 Ta2 : ℝ, T0 ≤ Ta1 → Ta1 ≤ Ta2 → Ta2 ≤ Tb →\n      boilTime Cw Cs D P h Tr T0 Tb Ta1 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta2 := by\n    intro Ta1 Ta2 h1 h12 h2\n    have G := G_mono Cw Cs D P h Tr Tb hCw hCs hD hh hP\n    have m := G (a := Cw*(Ta1-Tr)) (b := Cw*(Ta2-Tr))\n      ⟨by nlinarith, by nlinarith⟩ ⟨by nlinarith, by nlinarith⟩ (by nlinarith)\n    simp only [boilTime]\n    simp only at m\n    linarith\n  exact ⟨mono, fun Ta h1 h2 => mono T0 Ta le_rfl h1 h2⟩\nEOF\npython /harness/src/vcs_protocol.py tool check --manifest /work/candidate.json 2>&1 | tail -20; cat $(ls -td checks/*/ | head -1)lean.log | head -50"
}
Bash output
      },
      "Fw": {
        "original_sha256": "38b9048abbc880c8b91005bb57e36daee61676680cf2ea28b79e449817077112",
        "extracted_sha256": "38b9048abbc880c8b91005bb57e36daee61676680cf2ea28b79e449817077112"
      },
      "Fs": {
        "original_sha256": "7428e34b8a70d77e92148994cb5f9813e0ae0c7e71b7041de8d055570276aaab",
        "extracted_sha256": "7428e34b8a70d77e92148994cb5f9813e0ae0c7e71b7041de8d055570276aaab"
      }
    },
    "unranged": [],
    "added_assumptions": [],
    "conclusions_as_proved": {
      "answer_theorem": "\u2200 (Cw Cs D P h Tr T0 Tb : \u211d), 0 < Cw \u2192 0 \u2264 Cs \u2192 0 \u2264 D \u2192 0 < h \u2192 h * (Tb - Tr) < P \u2192 Tr \u2264 T0 \u2192 (\u2200 (Ta1 Ta2 : \u211d), T0 \u2264 Ta1 \u2192 Ta1 \u2264 Ta2 \u2192 Ta2 \u2264 Tb \u2192 boilTime Cw Cs D P h Tr T0 Tb Ta1 \u2264 boilTime Cw Cs D P h Tr T0 Tb Ta2) \u2227 \u2200 (Ta : \u211d), T0 \u2264 Ta \u2192 Ta \u2264 Tb \u2192 boilTime Cw Cs D P h Tr T0 Tb T0 \u2264 boilTime Cw Cs D P h Tr T0 Tb Ta"
    },
    "extracted_file": "/work/checks/9d2a29707556410397d3c62a510d59a9/extracted.lean",
    "errors": []
  },
  "scientific_relevance": "requires human review"
}
'answer_theorem' depends on axioms: [propext, Classical.choice, Quot.sound]
VAS_LISTED

leanchecker exit 0
assistant
Passed first try. Now the narrative.
Bash input
{
  "command": "cd /work; cat > report.txt <<'EOF'\nANSWER (conditional): add the salt at the start (water at 20 C, t = 0). In the model below the\ntime to boil never decreases the later the salt goes in, so no heating delay helps. The gain is\nsmall: the salt's effect on boiling time comes through heat losses only.\n\nWHY (physics). Boiling point of the final solution Tb = 100 C + elevation (colligative; set by salt\namount alone). Energy needed to reach it from the start is fixed whatever the timing. Timing\nmatters only through heat loss: the stove delivers P, the pot loses h(T-Tr). Salt raises the heat\ncapacity, so for the same stored energy the pot is cooler, loses less, and heats faster. Hence\nsalt in early = less loss = earlier boil. If losses were zero (h=0) timing would not matter.\nCommon belief \"salt later because salt makes water heat slower\" is wrong about total time:\nthe energy must be supplied anyway. Adding salt to near-boiling water also risks boil-over (safety, not time).\n\nFACTS SUPPLIED: 1 L water, 20 C, stove heating, salt to be added, aim = earliest boil of salted water;\nsalt amount, pot, heater, room unspecified.\n\nMODEL CHOICES (my assumptions, for review):\n 1 Lumped system, enthalpy coordinate H from \"all at room temperature Tr\"; water T = Tr+H/Cw (Cw includes pot).\n 2 Constant heater power P; Newtonian loss h(T-Tr), h>0; evaporation ignored.\n 3 Salt at Tr, mixed instantly, dissolves instantly; H conserved at mixing.\n 4 Salt solution heat capacity Cw+Cs, Cs>=0; constant endothermic dissolution enthalpy D>=0 (NaCl ~ +3.9 kJ/mol).\n 5 Tb independent of timing; P > h(Tb-Tr) (boiling reachable); Tr <= 20 C; salt added before boiling (Ta in [T0,Tb]).\n 6 Time from H1 to H2 = integral dH/(P - h(T(H)-Tr)); closed form via log antiderivatives Fw, Fs\n   (Mathlib in this harness lacks interval integrals); derivative checked in Lean.\nNot determined by the data: salt amount, P, h, Cw, Cs, D, Tr. The conclusion holds for all values satisfying the\nassumptions, so it does not need them; the MAGNITUDE of the gain does and is not claimed (no calculation or measurement run).\n\nLEAN (Model.lean): boilTime = total time as a function of Ta (water temperature when salt is added).\nG_mono proves via derivative (1/dw - 1/ds >= 0, using the proved inequality (H-D)/(Cw+Cs) <= H/Cw for H>=0)\nthat Fw-Fs is monotone. answer_theorem: boilTime is monotone in Ta on [T0,Tb] and boilTime(T0) is the minimum.\nCheck passed, axioms only propext/Classical.choice/Quot.sound, no sorry. Not proved: strictness, magnitude,\nvalidity of the premises, case h=0 (equal times), Tr>T0.\nSources: none external (general thermodynamics; no requests made).\nEOF\npython /harness/src/vcs_protocol.py tool check --manifest /work/candidate.json 2>&1 | grep -E '\"ok\"|verdict|reviewable'"
}
Bash output
 "ok": true,
  "ok": true,
  "reviewable": true,
  "verdict": "proved",
    "ok": true,
assistant
**Answer:** Add the salt at the start, with the water still at 20 °C. In my model, adding it later never makes the salted water boil sooner. Waiting for a hotter temperature gains nothing. The Lean check passed and the candidate is ready for your review.

**Why:**
- The final boiling point, 100 °C plus the salt's elevation, depends only on how much salt you add. The energy needed to reach it is the same whenever the salt goes in.
- Timing only matters through heat loss to the room. Salt raises the heat capacity, so at the same stored energy the pot is cooler and loses less heat.
- Salt added early therefore means less loss and an earlier boil. With zero heat loss, timing would make no difference.
- The gain is probably small. I did not estimate it, and the proof does not give it.
- Adding salt to nearly boiling water can make it boil over. That is a safety point, not a timing one.

**Model assumptions (mine, for you to review):**
- The pot and water are one lump, with constant heater power and heat loss proportional to the excess over room temperature.
- Evaporation is ignored.
- The salt starts at room temperature, dissolves instantly, and its dissolution enthalpy is constant and endothermic (≥ 0).
- The boiling point of the final solution does not depend on when the salt goes in.
- The heater can reach boiling, and the room is at or below 20 °C.
- The salt is added before boiling begins.

**What the proof covers:**
- `answer_theorem` in `Model.lean` proves that the time to boil never decreases as the water temperature at salting rises. It also proves that salting at the start is the minimum.
- The proof assumes nonzero heat loss and rests on a derivative argument, with the time written in closed form using logarithms. This Mathlib build has no interval integrals.
- The axioms are only the standard ones, and there is no `sorry`.
- It does not prove that the gain is strictly positive or how large it is. Whether the premises are realistic is for you to judge.

I made no literature, calculation or experiment requests and ran no measurements or simulations. The salt amount, heater power, heat loss and room temperature were not given. The conclusion holds for any values that meet the assumptions, so it doesn't depend on them.

Files are in `/work`:
- `Model.lean`
- `report.txt`
- `candidate.json`

Produced code

attempt-001/Model.lean
import Mathlib.Analysis.SpecialFunctions.Log.Deriv
import Mathlib.Analysis.Calculus.MeanValue

/-! Model (see report.txt).  H = enthalpy (J) measured from "everything at room temperature Tr".
 Pure water, heat capacity Cw: temperature Tr + H/Cw.
 Salt solution, heat capacity Cw+Cs, constant endothermic dissolution enthalpy D ≥ 0:
 temperature Tr + (H-D)/(Cw+Cs).  Salt (at Tr) is mixed in instantly, so H is conserved.
 Heater power P constant, loss h*(T-Tr):  dH/dt = P - h*(T-Tr).
 Separating variables, the time to move from H1 to H2 is Fw/Fs(H2) - Fw/Fs(H1) where
 Fw' = 1/(P - h*(H/Cw)), Fs' = 1/(P - h*((H-D)/(Cw+Cs))) (explicit logarithms below).
 Tb = boiling point of the final solution (fixed by salt amount, not by timing). -/

/-- time-integral of dH/(P - loss) for pure water (antiderivative, h>0) -/
noncomputable def Fw (Cw P h H : ℝ) : ℝ := -(Cw/h) * Real.log (P - h*(H/Cw))
/-- same for the salt solution -/
noncomputable def Fs (Cw Cs D P h H : ℝ) : ℝ := -((Cw+Cs)/h) * Real.log (P - h*((H-D)/(Cw+Cs)))

/-- Total time to reach Tb from T0 when the salt is added once the water is at Ta:
 heating water alone from T0 to Ta, then solution from the post-mixing state up to Tb. -/
noncomputable def boilTime (Cw Cs D P h Tr T0 Tb Ta : ℝ) : ℝ :=
  (Fw Cw P h (Cw*(Ta-Tr)) - Fw Cw P h (Cw*(T0-Tr))) +
  (Fs Cw Cs D P h ((Cw+Cs)*(Tb-Tr)+D) - Fs Cw Cs D P h (Cw*(Ta-Tr)))

lemma G_mono (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hCs : 0 ≤ Cs) (hD : 0 ≤ D) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P) :
    MonotoneOn (fun H => Fw Cw P h H - Fs Cw Cs D P h H) (Set.Icc 0 (Cw*(Tb-Tr))) := by
  have hC : 0 < Cw + Cs := by linarith
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  have hle : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), (H-D)/(Cw+Cs) ≤ H/Cw := by
    intro H hH
    rw [div_le_div_iff₀ hC hCw]
    nlinarith [mul_nonneg hCs hH.1, mul_nonneg hCw.le hD]
  have hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs)) := by
    intro H hH
    have := hle H hH
    have := hw H hH
    nlinarith
  have hd : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), HasDerivAt (fun H => Fw Cw P h H - Fs Cw Cs D P h H)
      (1/(P - h*(H/Cw)) - 1/(P - h*((H-D)/(Cw+Cs)))) H := by
    intro H hH
    have a1 : HasDerivAt (fun H : ℝ => P - h*(H/Cw)) (-(h/Cw)) H := by
      have := ((hasDerivAt_id H).div_const Cw).const_mul h
      simpa [div_eq_mul_inv, mul_comm] using (this.const_sub P)
    have a2 : HasDerivAt (fun H : ℝ => P - h*((H-D)/(Cw+Cs))) (-(h/(Cw+Cs))) H := by
      have := (((hasDerivAt_id H).sub_const D).div_const (Cw+Cs)).const_mul h
      simpa [div_eq_mul_inv, mul_comm] using (this.const_sub P)
    have l1 := (a1.log (hw H hH).ne').const_mul (-(Cw/h))
    have l2 := (a2.log (hs H hH).ne').const_mul (-((Cw+Cs)/h))
    have := l1.sub l2
    convert this using 1
    · funext x; rfl
    · have e1 := (hw H hH).ne'
      have e2 := (hs H hH).ne'
      have e3 := hh.ne'
      have e4 := hCw.ne'
      have e5 := hC.ne'
      field_simp
  apply monotoneOn_of_deriv_nonneg (convex_Icc _ _)
  · intro H hH; exact (hd H hH).continuousAt.continuousWithinAt
  · intro H hH; exact (hd H (interior_subset hH)).differentiableAt.differentiableWithinAt
  · intro H hH
    have hH' := interior_subset hH
    rw [(hd H hH').deriv]
    have := hle H hH'
    have p1 := hw H hH'
    apply sub_nonneg.mpr
    apply one_div_le_one_div_of_le p1
    nlinarith

/-- ANSWER.  In the model, with salt of fixed amount (Cs, D, Tb fixed), the time to boil
never decreases if the salt is added later, i.e. at a hotter water temperature Ta ∈ [T0, Tb];
hence adding it at the very start (Ta = T0 = 20 °C) is optimal. -/
theorem answer_theorem : ∀ (Cw Cs D P h Tr T0 Tb : ℝ),
    0 < Cw → 0 ≤ Cs → 0 ≤ D → 0 < h → h*(Tb-Tr) < P → Tr ≤ T0 →
    (∀ Ta1 Ta2 : ℝ, T0 ≤ Ta1 → Ta1 ≤ Ta2 → Ta2 ≤ Tb →
        boilTime Cw Cs D P h Tr T0 Tb Ta1 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta2) ∧
    (∀ Ta : ℝ, T0 ≤ Ta → Ta ≤ Tb →
        boilTime Cw Cs D P h Tr T0 Tb T0 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta) := by
  intro Cw Cs D P h Tr T0 Tb hCw hCs hD hh hP hTr
  have mono : ∀ Ta1 Ta2 : ℝ, T0 ≤ Ta1 → Ta1 ≤ Ta2 → Ta2 ≤ Tb →
      boilTime Cw Cs D P h Tr T0 Tb Ta1 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta2 := by
    intro Ta1 Ta2 h1 h12 h2
    have G := G_mono Cw Cs D P h Tr Tb hCw hCs hD hh hP
    have m := G (a := Cw*(Ta1-Tr)) (b := Cw*(Ta2-Tr))
      ⟨by nlinarith, by nlinarith⟩ ⟨by nlinarith, by nlinarith⟩ (by nlinarith)
    simp only [boilTime]
    simp only at m
    linarith
  exact ⟨mono, fun Ta h1 h2 => mono T0 Ta le_rfl h1 h2⟩
attempt-001/extracted.lean
import Mathlib.Analysis.SpecialFunctions.Log.Deriv
import Mathlib.Analysis.Calculus.MeanValue

/-- time-integral of dH/(P - loss) for pure water (antiderivative, h>0) -/
noncomputable def Fw (Cw P h H : ℝ) : ℝ := -(Cw/h) * Real.log (P - h*(H/Cw))

/-- same for the salt solution -/
noncomputable def Fs (Cw Cs D P h H : ℝ) : ℝ := -((Cw+Cs)/h) * Real.log (P - h*((H-D)/(Cw+Cs)))

/-- Total time to reach Tb from T0 when the salt is added once the water is at Ta:
 heating water alone from T0 to Ta, then solution from the post-mixing state up to Tb. -/
noncomputable def boilTime (Cw Cs D P h Tr T0 Tb Ta : ℝ) : ℝ :=
  (Fw Cw P h (Cw*(Ta-Tr)) - Fw Cw P h (Cw*(T0-Tr))) +
  (Fs Cw Cs D P h ((Cw+Cs)*(Tb-Tr)+D) - Fs Cw Cs D P h (Cw*(Ta-Tr)))


/-! Conclusions (statements of the proved theorems). -/
def conclusion_answer_theorem : Prop := ∀ (Cw Cs D P h Tr T0 Tb : ℝ), 0 < Cw → 0 ≤ Cs → 0 ≤ D → 0 < h → h*(Tb-Tr) < P → Tr ≤ T0 → (∀ Ta1 Ta2 : ℝ, T0 ≤ Ta1 → Ta1 ≤ Ta2 → Ta2 ≤ Tb → boilTime Cw Cs D P h Tr T0 Tb Ta1 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta2) ∧ (∀ Ta : ℝ, T0 ≤ Ta → Ta ≤ Tb → boilTime Cw Cs D P h Tr T0 Tb T0 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta)
checks/9d2a29707556410397d3c62a510d59a9/submitted.lean
import Mathlib.Analysis.SpecialFunctions.Log.Deriv
import Mathlib.Analysis.Calculus.MeanValue

/-! Model (see report.txt).  H = enthalpy (J) measured from "everything at room temperature Tr".
 Pure water, heat capacity Cw: temperature Tr + H/Cw.
 Salt solution, heat capacity Cw+Cs, constant endothermic dissolution enthalpy D ≥ 0:
 temperature Tr + (H-D)/(Cw+Cs).  Salt (at Tr) is mixed in instantly, so H is conserved.
 Heater power P constant, loss h*(T-Tr):  dH/dt = P - h*(T-Tr).
 Separating variables, the time to move from H1 to H2 is Fw/Fs(H2) - Fw/Fs(H1) where
 Fw' = 1/(P - h*(H/Cw)), Fs' = 1/(P - h*((H-D)/(Cw+Cs))) (explicit logarithms below).
 Tb = boiling point of the final solution (fixed by salt amount, not by timing). -/

/-- time-integral of dH/(P - loss) for pure water (antiderivative, h>0) -/
noncomputable def Fw (Cw P h H : ℝ) : ℝ := -(Cw/h) * Real.log (P - h*(H/Cw))
/-- same for the salt solution -/
noncomputable def Fs (Cw Cs D P h H : ℝ) : ℝ := -((Cw+Cs)/h) * Real.log (P - h*((H-D)/(Cw+Cs)))

/-- Total time to reach Tb from T0 when the salt is added once the water is at Ta:
 heating water alone from T0 to Ta, then solution from the post-mixing state up to Tb. -/
noncomputable def boilTime (Cw Cs D P h Tr T0 Tb Ta : ℝ) : ℝ :=
  (Fw Cw P h (Cw*(Ta-Tr)) - Fw Cw P h (Cw*(T0-Tr))) +
  (Fs Cw Cs D P h ((Cw+Cs)*(Tb-Tr)+D) - Fs Cw Cs D P h (Cw*(Ta-Tr)))

lemma G_mono (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hCs : 0 ≤ Cs) (hD : 0 ≤ D) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P) :
    MonotoneOn (fun H => Fw Cw P h H - Fs Cw Cs D P h H) (Set.Icc 0 (Cw*(Tb-Tr))) := by
  have hC : 0 < Cw + Cs := by linarith
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  have hle : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), (H-D)/(Cw+Cs) ≤ H/Cw := by
    intro H hH
    rw [div_le_div_iff₀ hC hCw]
    nlinarith [mul_nonneg hCs hH.1, mul_nonneg hCw.le hD]
  have hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs)) := by
    intro H hH
    have := hle H hH
    have := hw H hH
    nlinarith
  have hd : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), HasDerivAt (fun H => Fw Cw P h H - Fs Cw Cs D P h H)
      (1/(P - h*(H/Cw)) - 1/(P - h*((H-D)/(Cw+Cs)))) H := by
    intro H hH
    have a1 : HasDerivAt (fun H : ℝ => P - h*(H/Cw)) (-(h/Cw)) H := by
      have := ((hasDerivAt_id H).div_const Cw).const_mul h
      simpa [div_eq_mul_inv, mul_comm] using (this.const_sub P)
    have a2 : HasDerivAt (fun H : ℝ => P - h*((H-D)/(Cw+Cs))) (-(h/(Cw+Cs))) H := by
      have := (((hasDerivAt_id H).sub_const D).div_const (Cw+Cs)).const_mul h
      simpa [div_eq_mul_inv, mul_comm] using (this.const_sub P)
    have l1 := (a1.log (hw H hH).ne').const_mul (-(Cw/h))
    have l2 := (a2.log (hs H hH).ne').const_mul (-((Cw+Cs)/h))
    have := l1.sub l2
    convert this using 1
    · funext x; rfl
    · have e1 := (hw H hH).ne'
      have e2 := (hs H hH).ne'
      have e3 := hh.ne'
      have e4 := hCw.ne'
      have e5 := hC.ne'
      field_simp
  apply monotoneOn_of_deriv_nonneg (convex_Icc _ _)
  · intro H hH; exact (hd H hH).continuousAt.continuousWithinAt
  · intro H hH; exact (hd H (interior_subset hH)).differentiableAt.differentiableWithinAt
  · intro H hH
    have hH' := interior_subset hH
    rw [(hd H hH').deriv]
    have := hle H hH'
    have p1 := hw H hH'
    apply sub_nonneg.mpr
    apply one_div_le_one_div_of_le p1
    nlinarith

/-- ANSWER.  In the model, with salt of fixed amount (Cs, D, Tb fixed), the time to boil
never decreases if the salt is added later, i.e. at a hotter water temperature Ta ∈ [T0, Tb];
hence adding it at the very start (Ta = T0 = 20 °C) is optimal. -/
theorem answer_theorem : ∀ (Cw Cs D P h Tr T0 Tb : ℝ),
    0 < Cw → 0 ≤ Cs → 0 ≤ D → 0 < h → h*(Tb-Tr) < P → Tr ≤ T0 →
    (∀ Ta1 Ta2 : ℝ, T0 ≤ Ta1 → Ta1 ≤ Ta2 → Ta2 ≤ Tb →
        boilTime Cw Cs D P h Tr T0 Tb Ta1 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta2) ∧
    (∀ Ta : ℝ, T0 ≤ Ta → Ta ≤ Tb →
        boilTime Cw Cs D P h Tr T0 Tb T0 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta) := by
  intro Cw Cs D P h Tr T0 Tb hCw hCs hD hh hP hTr
  have mono : ∀ Ta1 Ta2 : ℝ, T0 ≤ Ta1 → Ta1 ≤ Ta2 → Ta2 ≤ Tb →
      boilTime Cw Cs D P h Tr T0 Tb Ta1 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta2 := by
    intro Ta1 Ta2 h1 h12 h2
    have G := G_mono Cw Cs D P h Tr Tb hCw hCs hD hh hP
    have m := G (a := Cw*(Ta1-Tr)) (b := Cw*(Ta2-Tr))
      ⟨by nlinarith, by nlinarith⟩ ⟨by nlinarith, by nlinarith⟩ (by nlinarith)
    simp only [boilTime]
    simp only at m
    linarith
  exact ⟨mono, fun Ta h1 h2 => mono T0 Ta le_rfl h1 h2⟩
checks/b7ec15d94a8146bfadfbd5cc72230a57/submitted.lean
import Mathlib.Analysis.SpecialFunctions.Integrals
import Mathlib.MeasureTheory.Integral.IntervalIntegral.Basic

/-! Model: enthalpy coordinate H (J, measured from the state "everything at room temperature Tr").
 Pure water (heat capacity Cw) has temperature Tr + H/Cw.
 Salted water (heat capacity Cw+Cs, constant endothermic dissolution enthalpy D ≥ 0) has
 temperature Tr + (H-D)/(Cw+Cs).  Salt (at Tr) is stirred in instantly: H is conserved.
 Heater delivers constant power P, losses are Newtonian h*(T-Tr), so dH/dt = P - h*(T-Tr)
 and the time to go from H1 to H2 is ∫ dH / (P - h*(T(H)-Tr)).
 Tb = boiling point of the FINAL salt solution: fixed by the salt quantity, not by timing. -/

/-- Total time to reach Tb when the salt is added once the water has reached temperature Ta. -/
noncomputable def boilTime (Cw Cs D P h Tr T0 Tb Ta : ℝ) : ℝ :=
  (∫ H in (Cw*(T0-Tr))..(Cw*(Ta-Tr)), 1/(P - h*(H/Cw))) +
  ∫ H in (Cw*(Ta-Tr))..((Cw+Cs)*(Tb-Tr)+D), 1/(P - h*((H-D)/(Cw+Cs)))

lemma boilTime_mono (Cw Cs D P h Tr T0 Tb Ta1 Ta2 : ℝ)
    (hCw : 0 < Cw) (hCs : 0 ≤ Cs) (hD : 0 ≤ D) (hh : 0 ≤ h)
    (hP : h*(Tb-Tr) < P) (hTr : Tr ≤ T0) (h1 : T0 ≤ Ta1) (h12 : Ta1 ≤ Ta2) (h2 : Ta2 ≤ Tb) :
    boilTime Cw Cs D P h Tr T0 Tb Ta1 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta2 := by
  have hC : 0 < Cw + Cs := by linarith
  set c := Cw*(T0-Tr) with hc
  set a := Cw*(Ta1-Tr) with ha
  set b := Cw*(Ta2-Tr) with hb
  set e := (Cw+Cs)*(Tb-Tr)+D with he
  have hca : c ≤ a := by rw [hc, ha]; nlinarith
  have hab : a ≤ b := by rw [ha, hb]; nlinarith
  have hbe : b ≤ e := by
    rw [hb, he]; nlinarith [mul_nonneg hCs (show 0 ≤ Tb - Tr by linarith)]
  have ha0 : 0 ≤ a := by rw [ha]; nlinarith
  -- positivity of denominators
  have hpw : ∀ H, H ≤ b → 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by
      rw [div_le_iff₀ hCw]; rw [hb] at hH; nlinarith
    nlinarith
  have hps : ∀ H, H ≤ e → 0 < P - h*((H-D)/(Cw+Cs)) := by
    intro H hH
    have : (H-D)/(Cw+Cs) ≤ Tb - Tr := by
      rw [div_le_iff₀ hC]; rw [he] at hH; nlinarith
    nlinarith
  have cw : ContinuousOn (fun H : ℝ => 1/(P - h*(H/Cw))) (Set.Icc c b) := by
    apply ContinuousOn.div continuousOn_const
    · fun_prop
    · intro H hH; exact (hpw H hH.2).ne'
  have cs : ContinuousOn (fun H : ℝ => 1/(P - h*((H-D)/(Cw+Cs)))) (Set.Icc a e) := by
    apply ContinuousOn.div continuousOn_const
    · fun_prop
    · intro H hH; exact (hps H hH.2).ne'
  have iw : ∀ x y, c ≤ x → x ≤ y → y ≤ b →
      IntervalIntegrable (fun H : ℝ => 1/(P - h*(H/Cw))) MeasureTheory.volume x y := by
    intro x y hx hxy hy
    apply ContinuousOn.intervalIntegrable
    rw [Set.uIcc_of_le hxy]
    exact cw.mono (Set.Icc_subset_Icc hx hy)
  have is : ∀ x y, a ≤ x → x ≤ y → y ≤ e →
      IntervalIntegrable (fun H : ℝ => 1/(P - h*((H-D)/(Cw+Cs)))) MeasureTheory.volume x y := by
    intro x y hx hxy hy
    apply ContinuousOn.intervalIntegrable
    rw [Set.uIcc_of_le hxy]
    exact cs.mono (Set.Icc_subset_Icc hx hy)
  unfold boilTime
  have s1 := intervalIntegral.integral_add_adjacent_intervals
    (iw c a hc.le hca (hab.trans le_rfl)) (iw a b hca hab le_rfl)
  have s2 := intervalIntegral.integral_add_adjacent_intervals
    (is a b le_rfl hab hbe) (is b e hab le_rfl le_rfl)
  have key : (∫ H in a..b, 1/(P - h*((H-D)/(Cw+Cs)))) ≤ ∫ H in a..b, 1/(P - h*(H/Cw)) := by
    apply intervalIntegral.integral_mono_on hab (is a b le_rfl hab hbe) (iw a b hca hab le_rfl)
    intro H hH
    have hpos := hpw H hH.2
    apply one_div_le_one_div_of_le hpos
    have : (H-D)/(Cw+Cs) ≤ H/Cw := by
      rw [div_le_div_iff₀ hC hCw]
      nlinarith [mul_nonneg hCs (show 0 ≤ H by linarith [hH.1]), mul_nonneg hCw.le hD]
    nlinarith
  rw [← s1, ← s2]
  linarith

/-- ANSWER.  Under the model, the time to boil never decreases if the salt is added later
(at a hotter water temperature Ta ∈ [T0, Tb]); hence adding it at the start (Ta = T0 = 20 °C)
is optimal. -/
theorem answer_theorem : ∀ (Cw Cs D P h Tr T0 Tb : ℝ),
    0 < Cw → 0 ≤ Cs → 0 ≤ D → 0 ≤ h → h*(Tb-Tr) < P → Tr ≤ T0 → T0 ≤ Tb →
    (∀ Ta1 Ta2 : ℝ, T0 ≤ Ta1 → Ta1 ≤ Ta2 → Ta2 ≤ Tb →
        boilTime Cw Cs D P h Tr T0 Tb Ta1 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta2) ∧
    (∀ Ta : ℝ, T0 ≤ Ta → Ta ≤ Tb →
        boilTime Cw Cs D P h Tr T0 Tb T0 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta) := by
  intro Cw Cs D P h Tr T0 Tb hCw hCs hD hh hP hTr hT
  refine ⟨fun a b h1 h12 h2 => boilTime_mono _ _ _ _ _ _ _ _ _ _ hCw hCs hD hh hP hTr h1 h12 h2, ?_⟩
  intro Ta hTa hTb
  exact boilTime_mono _ _ _ _ _ _ _ _ _ _ hCw hCs hD hh hP hTr le_rfl hTa hTb

Round 2

Messages

Bash input
{
  "command": "cd /work; ls -la; cat report.txt; cat Model.lean; cat candidate.json; ls *feedback* 2>/dev/null"
}
Bash output
total 40
drwxrwxr-x 3 1001 1001 4096 Nov 17 14:31 .
drwxr-xr-x 1 root root 4096 Nov 17 14:32 ..
-rw-r--r-- 1 1001 1001 4747 Nov 16 18:28 Model.lean
-rw-r--r-- 1 1001 1001 1015 Nov 16 18:28 candidate.json
drwxr-xr-x 5 1001 1001 4096 Nov 16 18:29 checks
-rw------- 1 1001 1001  666 Nov 17 14:31 human-feedback.json
-rw------- 1 1001 1001 5624 Nov 16 18:30 last-review-packet.json
-rw-r--r-- 1 1001 1001 2609 Nov 16 18:29 report.txt
ANSWER (conditional): add the salt at the start (water at 20 C, t = 0). In the model below the
time to boil never decreases the later the salt goes in, so no heating delay helps. The gain is
small: the salt's effect on boiling time comes through heat losses only.

WHY (physics). Boiling point of the final solution Tb = 100 C + elevation (colligative; set by salt
amount alone). Energy needed to reach it from the start is fixed whatever the timing. Timing
matters only through heat loss: the stove delivers P, the pot loses h(T-Tr). Salt raises the heat
capacity, so for the same stored energy the pot is cooler, loses less, and heats faster. Hence
salt in early = less loss = earlier boil. If losses were zero (h=0) timing would not matter.
Common belief "salt later because salt makes water heat slower" is wrong about total time:
the energy must be supplied anyway. Adding salt to near-boiling water also risks boil-over (safety, not time).

FACTS SUPPLIED: 1 L water, 20 C, stove heating, salt to be added, aim = earliest boil of salted water;
salt amount, pot, heater, room unspecified.

MODEL CHOICES (my assumptions, for review):
 1 Lumped system, enthalpy coordinate H from "all at room temperature Tr"; water T = Tr+H/Cw (Cw includes pot).
 2 Constant heater power P; Newtonian loss h(T-Tr), h>0; evaporation ignored.
 3 Salt at Tr, mixed instantly, dissolves instantly; H conserved at mixing.
 4 Salt solution heat capacity Cw+Cs, Cs>=0; constant endothermic dissolution enthalpy D>=0 (NaCl ~ +3.9 kJ/mol).
 5 Tb independent of timing; P > h(Tb-Tr) (boiling reachable); Tr <= 20 C; salt added before boiling (Ta in [T0,Tb]).
 6 Time from H1 to H2 = integral dH/(P - h(T(H)-Tr)); closed form via log antiderivatives Fw, Fs
   (Mathlib in this harness lacks interval integrals); derivative checked in Lean.
Not determined by the data: salt amount, P, h, Cw, Cs, D, Tr. The conclusion holds for all values satisfying the
assumptions, so it does not need them; the MAGNITUDE of the gain does and is not claimed (no calculation or measurement run).

LEAN (Model.lean): boilTime = total time as a function of Ta (water temperature when salt is added).
G_mono proves via derivative (1/dw - 1/ds >= 0, using the proved inequality (H-D)/(Cw+Cs) <= H/Cw for H>=0)
that Fw-Fs is monotone. answer_theorem: boilTime is monotone in Ta on [T0,Tb] and boilTime(T0) is the minimum.
Check passed, axioms only propext/Classical.choice/Quot.sound, no sorry. Not proved: strictness, magnitude,
validity of the premises, case h=0 (equal times), Tr>T0.
Sources: none external (general thermodynamics; no requests made).
import Mathlib.Analysis.SpecialFunctions.Log.Deriv
import Mathlib.Analysis.Calculus.MeanValue

/-! Model (see report.txt).  H = enthalpy (J) measured from "everything at room temperature Tr".
 Pure water, heat capacity Cw: temperature Tr + H/Cw.
 Salt solution, heat capacity Cw+Cs, constant endothermic dissolution enthalpy D ≥ 0:
 temperature Tr + (H-D)/(Cw+Cs).  Salt (at Tr) is mixed in instantly, so H is conserved.
 Heater power P constant, loss h*(T-Tr):  dH/dt = P - h*(T-Tr).
 Separating variables, the time to move from H1 to H2 is Fw/Fs(H2) - Fw/Fs(H1) where
 Fw' = 1/(P - h*(H/Cw)), Fs' = 1/(P - h*((H-D)/(Cw+Cs))) (explicit logarithms below).
 Tb = boiling point of the final solution (fixed by salt amount, not by timing). -/

/-- time-integral of dH/(P - loss) for pure water (antiderivative, h>0) -/
noncomputable def Fw (Cw P h H : ℝ) : ℝ := -(Cw/h) * Real.log (P - h*(H/Cw))
/-- same for the salt solution -/
noncomputable def Fs (Cw Cs D P h H : ℝ) : ℝ := -((Cw+Cs)/h) * Real.log (P - h*((H-D)/(Cw+Cs)))

/-- Total time to reach Tb from T0 when the salt is added once the water is at Ta:
 heating water alone from T0 to Ta, then solution from the post-mixing state up to Tb. -/
noncomputable def boilTime (Cw Cs D P h Tr T0 Tb Ta : ℝ) : ℝ :=
  (Fw Cw P h (Cw*(Ta-Tr)) - Fw Cw P h (Cw*(T0-Tr))) +
  (Fs Cw Cs D P h ((Cw+Cs)*(Tb-Tr)+D) - Fs Cw Cs D P h (Cw*(Ta-Tr)))

lemma G_mono (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hCs : 0 ≤ Cs) (hD : 0 ≤ D) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P) :
    MonotoneOn (fun H => Fw Cw P h H - Fs Cw Cs D P h H) (Set.Icc 0 (Cw*(Tb-Tr))) := by
  have hC : 0 < Cw + Cs := by linarith
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  have hle : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), (H-D)/(Cw+Cs) ≤ H/Cw := by
    intro H hH
    rw [div_le_div_iff₀ hC hCw]
    nlinarith [mul_nonneg hCs hH.1, mul_nonneg hCw.le hD]
  have hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs)) := by
    intro H hH
    have := hle H hH
    have := hw H hH
    nlinarith
  have hd : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), HasDerivAt (fun H => Fw Cw P h H - Fs Cw Cs D P h H)
      (1/(P - h*(H/Cw)) - 1/(P - h*((H-D)/(Cw+Cs)))) H := by
    intro H hH
    have a1 : HasDerivAt (fun H : ℝ => P - h*(H/Cw)) (-(h/Cw)) H := by
      have := ((hasDerivAt_id H).div_const Cw).const_mul h
      simpa [div_eq_mul_inv, mul_comm] using (this.const_sub P)
    have a2 : HasDerivAt (fun H : ℝ => P - h*((H-D)/(Cw+Cs))) (-(h/(Cw+Cs))) H := by
      have := (((hasDerivAt_id H).sub_const D).div_const (Cw+Cs)).const_mul h
      simpa [div_eq_mul_inv, mul_comm] using (this.const_sub P)
    have l1 := (a1.log (hw H hH).ne').const_mul (-(Cw/h))
    have l2 := (a2.log (hs H hH).ne').const_mul (-((Cw+Cs)/h))
    have := l1.sub l2
    convert this using 1
    · funext x; rfl
    · have e1 := (hw H hH).ne'
      have e2 := (hs H hH).ne'
      have e3 := hh.ne'
      have e4 := hCw.ne'
      have e5 := hC.ne'
      field_simp
  apply monotoneOn_of_deriv_nonneg (convex_Icc _ _)
  · intro H hH; exact (hd H hH).continuousAt.continuousWithinAt
  · intro H hH; exact (hd H (interior_subset hH)).differentiableAt.differentiableWithinAt
  · intro H hH
    have hH' := interior_subset hH
    rw [(hd H hH').deriv]
    have := hle H hH'
    have p1 := hw H hH'
    apply sub_nonneg.mpr
    apply one_div_le_one_div_of_le p1
    nlinarith

/-- ANSWER.  In the model, with salt of fixed amount (Cs, D, Tb fixed), the time to boil
never decreases if the salt is added later, i.e. at a hotter water temperature Ta ∈ [T0, Tb];
hence adding it at the very start (Ta = T0 = 20 °C) is optimal. -/
theorem answer_theorem : ∀ (Cw Cs D P h Tr T0 Tb : ℝ),
    0 < Cw → 0 ≤ Cs → 0 ≤ D → 0 < h → h*(Tb-Tr) < P → Tr ≤ T0 →
    (∀ Ta1 Ta2 : ℝ, T0 ≤ Ta1 → Ta1 ≤ Ta2 → Ta2 ≤ Tb →
        boilTime Cw Cs D P h Tr T0 Tb Ta1 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta2) ∧
    (∀ Ta : ℝ, T0 ≤ Ta → Ta ≤ Tb →
        boilTime Cw Cs D P h Tr T0 Tb T0 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta) := by
  intro Cw Cs D P h Tr T0 Tb hCw hCs hD hh hP hTr
  have mono : ∀ Ta1 Ta2 : ℝ, T0 ≤ Ta1 → Ta1 ≤ Ta2 → Ta2 ≤ Tb →
      boilTime Cw Cs D P h Tr T0 Tb Ta1 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta2 := by
    intro Ta1 Ta2 h1 h12 h2
    have G := G_mono Cw Cs D P h Tr Tb hCw hCs hD hh hP
    have m := G (a := Cw*(Ta1-Tr)) (b := Cw*(Ta2-Tr))
      ⟨by nlinarith, by nlinarith⟩ ⟨by nlinarith, by nlinarith⟩ (by nlinarith)
    simp only [boilTime]
    simp only at m
    linarith
  exact ⟨mono, fun Ta h1 h2 => mono T0 Ta le_rfl h1 h2⟩
{"proof_file":"Model.lean","narrative_file":"report.txt","conclusions":{"answer":"answer_theorem"},"user_facts":["1 L of water at 20 C in a pot on a stove","salt will be added; goal: salted water reaches its boiling point as soon as possible","salt quantity, pot, heater, room unspecified"],"model_choices":["constant heater power P; Newtonian loss h(T-Tr); lumped heat capacities","salt added at room temperature, instantly mixed, enthalpy conserved","salt raises heat capacity by Cs>=0, constant endothermic dissolution enthalpy D>=0","final boiling point Tb depends on salt amount only, not on timing; P>h(Tb-Tr) so boiling is reachable","room temperature Tr<=20 C; evaporation and boil-over ignored"],"sources":[],"incorporated_inputs":[],"proof_scope":"Proves, in the stated lumped model, that time to boil is monotone nondecreasing in the water temperature at which salt is added, so adding at the start is optimal. Does not prove strictness, nor magnitude of the gain; model premises are for human review."}
human-feedback.json
WebSearch input
{
  "query": "apparent molar heat capacity NaCl aqueous solution 25 °C J/(K mol) value dilute and 1 mol/kg; enthalpy of solution NaCl temperature dependence"
}
WebSearch output
Web search results for query: "apparent molar heat capacity NaCl aqueous solution 25 °C J/(K mol) value dilute and 1 mol/kg; enthalpy of solution NaCl temperature dependence"

Links: [{"title":"Apparent molar heat capacities and volumes of aqueous electrolytes at 25 °C: Na2SO4, K2SO4, Na2S2O3, Na2S2O8, K2S2O8, K2CrO4, Na2MoO4, and Na2WO4","url":"https://www.researchgate.net/publication/237857640_Apparent_molar_heat_capacities_and_volumes_of_aqueous_electrolytes_at_25_C_Na2SO4_K2SO4_Na2S2O3_Na2S2O8_K2S2O8_K2CrO4_Na2MoO4_and_Na2WO4"},{"title":"Heats of dilution of sodium chloride. Temperature dependence","url":"https://pubs.acs.org/doi/10.1021/je60057a018"},{"title":"This is an electronic reprint of the original article.","url":"https://aaltodoc.aalto.fi/server/api/core/bitstreams/8a8efdcb-a5ee-4fec-8d50-949aae744598/content"},{"title":"Vol.:(0123456789) Journal of Solution Chemistry","url":"https://d-nb.info/1352894033/34"},{"title":"Heat of Solution or Enthalpy of Solution Chemistry Tutorial","url":"https://www.ausetute.com.au/heatsolution.html"},{"title":"Thermodynamic Properties of the KCl+H2O System","url":"https://srd.nist.gov/jpcrdreprint/1.556034.pdf"},{"title":"srd.nist.gov","url":"https://srd.nist.gov/jpcrdreprint/1.555660.pdf"},{"title":"Volume 64, 1968","url":"https://pubs.rsc.org/en/content/articlelanding/1968/tf/tf9686402951"},{"title":"s10953 023 01322 y","url":"https://link.springer.com/article/10.1007/s10953-023-01322-y"},{"title":"s10953 024 01412 5","url":"https://link.springer.com/article/10.1007/s10953-024-01412-5"}]

The search didn't return the specific numbers you asked for. It did return a few usable pieces and some pointers to where the numbers are.

**What the results give**
- **Infinite-dilution heat capacity.** Abraham and Marcus suggested a partial molar heat capacity of NaCl at infinite dilution of about –84 J·K⁻¹·mol⁻¹. The snippet is garbled, so I'm only moderately confident of that figure.
- **Enthalpy of solution at 25 °C.** A tutorial table lists NaCl(s) at +3.88 kJ/mol, so dissolving is slightly endothermic.
- **Temperature dependence.** In the recent Hückel-type model, the relevant parameters have quadratic temperature dependences. The heat-of-solution and heat-capacity values are therefore tied together through the temperature derivative of the enthalpy.

**Not found**
- The apparent molar heat capacity at 1 mol/kg and 25 °C.
- Numerical values for the temperature dependence of the enthalpy of solution.

**Sources that should contain the data**
- Perron, Fortier and Desnoyers, J. Chem. Thermodyn. 1975, 7, 1177. It covers apparent molar heat capacities and volumes of aqueous NaCl from 0.01 to 3 mol·kg⁻¹ between 274.65 and 318.15 K.
- Gates, Tillett, White and Wood, J. Chem. Thermodyn. 1987, 19, 131. It covers 0.05 to 3.0 mol·kg⁻¹, 350 to 600 K, and 2 to 18 MPa.
- The 2023–24 J. Solution Chem. papers (Springer, s10953-023-01322-y and s10953-024-01412-5), which give enthalpy and heat-capacity tables for NaCl(aq).
- The NIST/JPCRD review of NaCl(aq) thermodynamic properties (srd.nist.gov/jpcrdreprint/1.555660.pdf).

If you need the numbers themselves, I can search again for the Pitzer or Archer values at 25 °C.


REMINDER: You MUST include the sources above in your response to the user using markdown hyperlinks.
Bash input
{
  "command": "cd /work; python - <<'EOF'\ns=open('Model.lean').read()\ni=s.index('lemma G_mono')\nhead=s[:i]\nhead=head.replace(\"Salt solution, heat capacity Cw+Cs, constant endothermic dissolution enthalpy D ≥ 0:\",\"Salt solution, total heat capacity Cw+Cs>0 (Cs of EITHER sign; literature: Cs<0), dissolution enthalpy D (at Tr) of either sign:\")\nnew='''lemma G_deriv (Cw Cs D P h Tr Tb : ℝ)\n    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)\n    (hP : h*(Tb-Tr) < P)\n    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs))) :\n    ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), HasDerivAt (fun H => Fw Cw P h H - Fs Cw Cs D P h H)\n      (1/(P - h*(H/Cw)) - 1/(P - h*((H-D)/(Cw+Cs)))) H := by\n  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by\n    intro H hH\n    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]\n    nlinarith\n  intro H hH\n  have a1 : HasDerivAt (fun H : ℝ => P - h*(H/Cw)) (-(h/Cw)) H := by\n    have := ((hasDerivAt_id H).div_const Cw).const_mul h\n    simpa [div_eq_mul_inv, mul_comm] using (this.const_sub P)\n  have a2 : HasDerivAt (fun H : ℝ => P - h*((H-D)/(Cw+Cs))) (-(h/(Cw+Cs))) H := by\n    have := (((hasDerivAt_id H).sub_const D).div_const (Cw+Cs)).const_mul h\n    simpa [div_eq_mul_inv, mul_comm] using (this.const_sub P)\n  have l1 := (a1.log (hw H hH).ne').const_mul (-(Cw/h))\n  have l2 := (a2.log (hs H hH).ne').const_mul (-((Cw+Cs)/h))\n  have := l1.sub l2\n  convert this using 1\n  · funext x; rfl\n  · have e1 := (hw H hH).ne'\n    have e2 := (hs H hH).ne'\n    have e3 := hh.ne'\n    have e4 := hCw.ne'\n    have e5 := hC.ne'\n    field_simp\n\n/-- Salt added at water temperature x changes the temperature at once by\n (D + Cs (x-Tr))/(Cw+Cs) downward (a cooling if D + Cs (x-Tr) > 0).  -/\nlemma mix_iff (Cw Cs D Tr x : ℝ) (hCw : 0 < Cw) (hC : 0 < Cw + Cs) :\n    (Cw*(x-Tr) - D)/(Cw+Cs) ≤ Cw*(x-Tr)/Cw ↔ 0 ≤ D + Cs*(x-Tr) := by\n  rw [mul_div_cancel_left₀ _ hCw.ne', div_le_iff₀ hC]\n  constructor <;> intro h <;> nlinarith\n\nlemma G_mono (Cw Cs D P h Tr Tb : ℝ)\n    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)\n    (hP : h*(Tb-Tr) < P)\n    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs)))\n    (S : Set ℝ) (hS : S ⊆ Set.Icc 0 (Cw*(Tb-Tr))) (hconv : Convex ℝ S)\n    (sgn : ∀ H ∈ S, (H-D)/(Cw+Cs) ≤ H/Cw) :\n    MonotoneOn (fun H => Fw Cw P h H - Fs Cw Cs D P h H) S := by\n  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by\n    intro H hH\n    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]\n    nlinarith\n  have hd := G_deriv Cw Cs D P h Tr Tb hCw hC hh hP hs\n  apply monotoneOn_of_deriv_nonneg hconv\n  · intro H hH; exact (hd H (hS hH)).continuousAt.continuousWithinAt\n  · intro H hH; exact (hd H (hS (interior_subset hH))).differentiableAt.differentiableWithinAt\n  · intro H hH\n    have hH' := interior_subset hH\n    rw [(hd H (hS hH')).deriv]\n    apply sub_nonneg.mpr\n    apply one_div_le_one_div_of_le (hw H (hS hH'))\n    have := sgn H hH'\n    nlinarith\n\nlemma G_anti (Cw Cs D P h Tr Tb : ℝ)\n    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)\n    (hP : h*(Tb-Tr) < P)\n    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs)))\n    (S : Set ℝ) (hS : S ⊆ Set.Icc 0 (Cw*(Tb-Tr))) (hconv : Convex ℝ S)\n    (sgn : ∀ H ∈ S, H/Cw ≤ (H-D)/(Cw+Cs)) :\n    AntitoneOn (fun H => Fw Cw P h H - Fs Cw Cs D P h H) S := by\n  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by\n    intro H hH\n    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]\n    nlinarith\n  have hd := G_deriv Cw Cs D P h Tr Tb hCw hC hh hP hs\n  apply antitoneOn_of_deriv_nonpos hconv\n  · intro H hH; exact (hd H (hS hH)).continuousAt.continuousWithinAt\n  · intro H hH; exact (hd H (hS (interior_subset hH))).differentiableAt.differentiableWithinAt\n  · intro H hH\n    have hH' := interior_subset hH\n    rw [(hd H (hS hH')).deriv]\n    apply sub_nonpos.mpr\n    apply one_div_le_one_div_of_le (hs H (hS hH'))\n    have := sgn H hH'\n    nlinarith\n\n/-- ANSWER 1 (sign-free).  Salt of fixed amount (Cs, D arbitrary sign, C=Cw+Cs>0).\n If between water temperatures Ta1 ≤ Ta2 salt addition always COOLS (D+Cs(x-Tr) ≥ 0 for x in [Ta1,Ta2]),\n adding at Ta1 is no slower than at Ta2; if it always WARMS (≤ 0), adding at Ta2 is no slower. -/\ntheorem answer_theorem : ∀ (Cw Cs D P h Tr T0 Tb : ℝ),\n    0 < Cw → 0 < Cw + Cs → 0 < h → h*(Tb-Tr) < P → Tr ≤ T0 →\n    (∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs))) →\n    ∀ Ta1 Ta2 : ℝ, T0 ≤ Ta1 → Ta1 ≤ Ta2 → Ta2 ≤ Tb →\n      ((∀ x, Ta1 ≤ x → x ≤ Ta2 → 0 ≤ D + Cs*(x-Tr)) →\n          boilTime Cw Cs D P h Tr T0 Tb Ta1 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta2) ∧\n      ((∀ x, Ta1 ≤ x → x ≤ Ta2 → D + Cs*(x-Tr) ≤ 0) →\n          boilTime Cw Cs D P h Tr T0 Tb Ta2 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta1) := by\n  intro Cw Cs D P h Tr T0 Tb hCw hC hh hP hTr hs Ta1 Ta2 h1 h12 h2\n  have hmem : ∀ x, Ta1 ≤ x → x ≤ Ta2 → Cw*(x-Tr) ∈ Set.Icc 0 (Cw*(Tb-Tr)) := fun x a b =>\n    ⟨by nlinarith, by nlinarith⟩\n  have hS : Set.Icc (Cw*(Ta1-Tr)) (Cw*(Ta2-Tr)) ⊆ Set.Icc 0 (Cw*(Tb-Tr)) := by\n    intro H hH\n    exact ⟨by nlinarith [hH.1], by nlinarith [hH.2]⟩\n  have hHx : ∀ H ∈ Set.Icc (Cw*(Ta1-Tr)) (Cw*(Ta2-Tr)), ∃ x, Ta1 ≤ x ∧ x ≤ Ta2 ∧ H = Cw*(x-Tr) := by\n    intro H hH\n    refine ⟨H/Cw + Tr, ?_, ?_, ?_⟩\n    · have := hH.1; rw [le_div_iff₀ hCw] at *; nlinarith [this]\n    · have := hH.2; nlinarith [div_le_iff₀ hCw |>.mpr (by nlinarith : H ≤ (Ta2 - Tr) * Cw)]\n    · field_simp; ring\n  constructor\n  · intro hx\n    have m := G_mono Cw Cs D P h Tr Tb hCw hC hh hP hs _ hS (convex_Icc _ _) (by\n      intro H hH\n      obtain ⟨x, a, b, rfl⟩ := hHx H hH\n      exact (mix_iff Cw Cs D Tr x hCw hC).mpr (hx x a b))\n      ⟨le_rfl, by nlinarith⟩ ⟨by nlinarith, le_rfl⟩ (by nlinarith)\n    simp only [boilTime]; simp only at m; linarith\n  · intro hx\n    have m := G_anti Cw Cs D P h Tr Tb hCw hC hh hP hs _ hS (convex_Icc _ _) (by\n      intro H hH\n      obtain ⟨x, a, b, rfl⟩ := hHx H hH\n      have := hx x a b\n      have h3 : ¬ ((Cw*(x-Tr) - D)/(Cw+Cs) < Cw*(x-Tr)/Cw) → True := fun _ => trivial\n      rw [mul_div_cancel_left₀ _ hCw.ne', le_div_iff₀ hC]\n      nlinarith)\n      ⟨le_rfl, by nlinarith⟩ ⟨by nlinarith, le_rfl⟩ (by nlinarith)\n    simp only [boilTime]; simp only at m; linarith\n'''\nopen('Model.lean','w').write(head+new)\nEOF\npython /harness/src/vcs_protocol.py tool check --manifest /work/candidate.json 2>&1 | tail -40"
}
Bash output
{
  "ok": false,
  "check_complete": false,
  "reviewable": false,
  "verdict": "not_proved",
  "time": "2026-11-17T14:33:36.147233+00:00",
  "lean_exit": 1,
  "error": "Lean, kernel recheck or axiom audit failed; see lean.log"
}
Bash input
{
  "command": "cd /work; ls -t checks | head -3; f=$(ls -td checks/*/ | head -1); ls $f; grep -A8 error $f/lean.log | head -70"
}
Bash output
7fdbd8cda6304fd3ae91fdd97ab702a9
7e4be0bd42b444199e65b96370c18486
9d2a29707556410397d3c62a510d59a9
Checked.lean
lean.log
submitted-candidate.json
submitted-report.txt
submitted.lean
verdict.json
/work/checks/7fdbd8cda6304fd3ae91fdd97ab702a9/Checked.lean:126:24: error: Tactic `rewrite` failed: Did not find an occurrence of the pattern in the current goal

case refine_1
Cw Cs D P h Tr T0 Tb : ℝ
hCw : 0 < Cw
hC : 0 < Cw + Cs
hh : 0 < h
hP : h * (Tb - Tr) < P
hTr : Tr ≤ T0
Bash input
{
  "command": "cd /work; python - <<'EOF'\ns=open('Model.lean').read()\na=s.index(\"    · have := hH.1; rw [le_div_iff₀\")\nb=s.index(\"    · field_simp; ring\")\ns=s[:a]+'''    · have : Ta1 - Tr ≤ H/Cw := by rw [le_div_iff₀ hCw]; nlinarith [hH.1]\n      linarith\n    · have : H/Cw ≤ Ta2 - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]\n      linarith\n'''+s[b:]\nopen('Model.lean','w').write(s)\nEOF\npython /harness/src/vcs_protocol.py tool check --manifest /work/candidate.json 2>&1 | grep -E 'verdict|ok\"'; f=$(ls -td checks/*/ | head -1); grep -A25 error $f/lean.log | head -60"
}
Bash output
 "ok": true,
  "ok": true,
  "verdict": "proved",
    "ok": true,
Bash input
{
  "command": "cd /work; cat > report.txt <<'EOF'\nREVISION (cycle 2). Reviewer: do not assume Cs>=0; justify the sign of the heat-capacity change.\nFinding: the premise Cs>=0 is probably FALSE. Dissolved NaCl has a NEGATIVE partial molar heat capacity\n(about -84 J/K/mol at infinite dilution, Abraham & Marcus as reported by a web search snippet; not verified\nagainst the primary source), so 1 kg of water plus salt has slightly LESS total heat capacity than 1 kg of\nwater plus the pot. Dissolution is endothermic at 25 C (+3.88 kJ/mol, tutorial table), and the enthalpy of\nsolution falls with temperature because Cp(dissolved) < Cp(solid NaCl, ~50 J/K/mol). I did not obtain\nprimary-source numbers (Perron-Fortier-Desnoyers 1975; NIST/JPCRD NaCl review are the places to look).\nSo the earlier claim \"salt early always wins\" is withdrawn. Cs and D are now of arbitrary sign (only Cw+Cs>0).\n\nMODEL (choices, all mine): lumped pot+water heat capacity Cw; constant stove power P; Newton loss h(T-Tr);\nno evaporation; salt at Tr dissolves instantly, enthalpy H conserved at mixing; solution temperature\nTr+(H-D)/(Cw+Cs) with D = enthalpy of solution at Tr, Cs = total heat-capacity change, constants;\nfinal boiling point Tb fixed by salt amount; Ta in [T0,Tb]; stove can always overcome losses at all visited\nstates (premise hs). Facts supplied: 1 L water, 20 C, stove, salt later; salt amount, pot, heater, room unspecified.\n\nRESULT (answer_theorem, proved in Lean, no sorry): time to boil as a function of the temperature Ta at which\nsalt goes in. Adding salt makes the temperature jump by -(D+Cs(Ta-Tr))/(Cw+Cs).\n - If between Ta1<=Ta2 salt addition always cools (D+Cs(x-Tr)>=0), adding at Ta1 is no slower than at Ta2.\n - If it always warms (<=0), adding at Ta2 is no slower.\nWith Cs<0 the switch is at Ta* = Tr + D/|Cs|. Rough illustration with the (unverified) constants above:\nTa* ~ 20+3880/84 ~ 66 C. So the sign changes inside the 20-100 C range and the model gives NO unconditional\nsingle best temperature. In a constant-parameter crude estimate with weak losses the early end looked\nslightly better, but that was NOT proved and depends on h/P and the salt amount, which are unspecified.\nHonest answer: the effect is tiny (a temperature jump of ~1 K per mole of salt in 1 L); the model cannot\ndecide it without salt amount, h/P and temperature-dependent data. Practically: time to boil is\ndominated by the energy needed, so timing hardly matters; add salt early to avoid boil-over.\nNot proved: endpoint-optimality (minimum is at 20 C or at the end, not interior, when Cs<0), magnitudes,\nconstants truth, temperature dependence of D and Cs. No measurement or request run.\nEOF\npython - <<'EOF'\nimport json\nc=json.load(open('candidate.json'))\nc['model_choices']=[\"Cs (total heat-capacity change on adding salt) and D (enthalpy of solution) of arbitrary sign; only Cw+Cs>0 assumed\",\"constant heater power P; Newtonian loss h(T-Tr); lumped heat capacities; no evaporation\",\"salt added at Tr, instant mixing, enthalpy conserved\",\"Tb fixed by salt amount; Ta in [T0,Tb]; stove overcomes losses at all visited states (premise)\"]\nc['sources']=[\"Web search snippets: Abraham & Marcus partial molar Cp of NaCl ~ -84 J/K/mol; NaCl dissolution +3.88 kJ/mol (tutorial table); unverified against primary sources\"]\nc['proof_scope']=\"Proves a sign-free comparison theorem: the sign of D+Cs(x-Tr) over a temperature interval determines whether later or earlier salt addition is faster. Does not prove a unique optimum; the sign of Cs is not assumed (literature suggests Cs<0), and no magnitudes are claimed.\"\njson.dump(c,open('candidate.json','w'),indent=1)\nEOF\npython /harness/src/vcs_protocol.py tool check --manifest /work/candidate.json 2>&1 | grep -E 'verdict'"
}
Bash output
  "verdict": "proved",
assistant
I've revised the model and the check passes (verdict "proved"), but the new result gives no single best temperature for adding the salt. That is the honest outcome of dropping the Cs ≥ 0 assumption.

**The sign of Cs.** Cs ≥ 0 is probably false. Dissolved NaCl seems to have a negative partial molar heat capacity, about −84 J/K/mol at infinite dilution. That figure comes from a search snippet that was itself garbled, so I have not checked it against the primary source. The same search gave a dissolution enthalpy of +3.88 kJ/mol at 25 °C, so dissolving is endothermic. Because of the heat-capacity sign, the solution also holds slightly less heat per degree than the same water without salt. I withdrew the earlier claim that adding salt at the start always wins.

**What changed in the model.** Cs (the change in total heat capacity when the salt goes in) and D (the enthalpy of solution) can now have either sign. The only requirement is that the total heat capacity Cw+Cs stays positive. The other modelling choices are unchanged. I also added one premise: the stove can overcome heat losses at every state the pot passes through.

**What `answer_theorem` proves (in Lean, no `sorry`).** Adding salt makes the temperature jump by −(D + Cs·(Ta−Tr))/(Cw+Cs), where Ta is the water temperature at the moment of adding. Tr is room temperature, and Cw is the heat capacity of pot plus water.
- If salt addition cools the water everywhere between Ta1 and Ta2, adding at Ta1 is no slower than adding at Ta2.
- If it warms the water everywhere between Ta1 and Ta2, adding at Ta2 is no slower.

**What this means for your question.** With Cs < 0 the sign flips at Ta* = Tr + D/|Cs|, which is roughly 66 °C using the unverified constants. That lies inside the 20–100 °C heating range, so the model cannot name one best temperature. The result also depends on the salt amount and the heater-to-loss ratio, neither of which you gave. The effect is small, about 1 K of temperature jump per mole of salt in 1 L. In practice timing barely matters; adding salt early also avoids boil-over.

**Not proved.**
- That the best moment is at one end of the range, either 20 °C or just before boiling, rather than somewhere in the middle.
- Any size of the gain.
- Temperature dependence of D and Cs.

I made no measurement or operator request this cycle. Primary-source numbers would come from Perron, Fortier and Desnoyers (J. Chem. Thermodyn. 1975) or the NIST/JPCRD NaCl review.

I have not self-approved; this is for your review.

Files are in /work:
- Model.lean
- report.txt
- candidate.json

Produced code

attempt-002/Model.lean
import Mathlib.Analysis.SpecialFunctions.Log.Deriv
import Mathlib.Analysis.Calculus.MeanValue

/-! Model (see report.txt).  H = enthalpy (J) measured from "everything at room temperature Tr".
 Pure water, heat capacity Cw: temperature Tr + H/Cw.
 Salt solution, total heat capacity Cw+Cs>0 (Cs of EITHER sign; literature: Cs<0), dissolution enthalpy D (at Tr) of either sign:
 temperature Tr + (H-D)/(Cw+Cs).  Salt (at Tr) is mixed in instantly, so H is conserved.
 Heater power P constant, loss h*(T-Tr):  dH/dt = P - h*(T-Tr).
 Separating variables, the time to move from H1 to H2 is Fw/Fs(H2) - Fw/Fs(H1) where
 Fw' = 1/(P - h*(H/Cw)), Fs' = 1/(P - h*((H-D)/(Cw+Cs))) (explicit logarithms below).
 Tb = boiling point of the final solution (fixed by salt amount, not by timing). -/

/-- time-integral of dH/(P - loss) for pure water (antiderivative, h>0) -/
noncomputable def Fw (Cw P h H : ℝ) : ℝ := -(Cw/h) * Real.log (P - h*(H/Cw))
/-- same for the salt solution -/
noncomputable def Fs (Cw Cs D P h H : ℝ) : ℝ := -((Cw+Cs)/h) * Real.log (P - h*((H-D)/(Cw+Cs)))

/-- Total time to reach Tb from T0 when the salt is added once the water is at Ta:
 heating water alone from T0 to Ta, then solution from the post-mixing state up to Tb. -/
noncomputable def boilTime (Cw Cs D P h Tr T0 Tb Ta : ℝ) : ℝ :=
  (Fw Cw P h (Cw*(Ta-Tr)) - Fw Cw P h (Cw*(T0-Tr))) +
  (Fs Cw Cs D P h ((Cw+Cs)*(Tb-Tr)+D) - Fs Cw Cs D P h (Cw*(Ta-Tr)))

lemma G_deriv (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs))) :
    ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), HasDerivAt (fun H => Fw Cw P h H - Fs Cw Cs D P h H)
      (1/(P - h*(H/Cw)) - 1/(P - h*((H-D)/(Cw+Cs)))) H := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  intro H hH
  have a1 : HasDerivAt (fun H : ℝ => P - h*(H/Cw)) (-(h/Cw)) H := by
    have := ((hasDerivAt_id H).div_const Cw).const_mul h
    simpa [div_eq_mul_inv, mul_comm] using (this.const_sub P)
  have a2 : HasDerivAt (fun H : ℝ => P - h*((H-D)/(Cw+Cs))) (-(h/(Cw+Cs))) H := by
    have := (((hasDerivAt_id H).sub_const D).div_const (Cw+Cs)).const_mul h
    simpa [div_eq_mul_inv, mul_comm] using (this.const_sub P)
  have l1 := (a1.log (hw H hH).ne').const_mul (-(Cw/h))
  have l2 := (a2.log (hs H hH).ne').const_mul (-((Cw+Cs)/h))
  have := l1.sub l2
  convert this using 1
  · funext x; rfl
  · have e1 := (hw H hH).ne'
    have e2 := (hs H hH).ne'
    have e3 := hh.ne'
    have e4 := hCw.ne'
    have e5 := hC.ne'
    field_simp

/-- Salt added at water temperature x changes the temperature at once by
 (D + Cs (x-Tr))/(Cw+Cs) downward (a cooling if D + Cs (x-Tr) > 0).  -/
lemma mix_iff (Cw Cs D Tr x : ℝ) (hCw : 0 < Cw) (hC : 0 < Cw + Cs) :
    (Cw*(x-Tr) - D)/(Cw+Cs) ≤ Cw*(x-Tr)/Cw ↔ 0 ≤ D + Cs*(x-Tr) := by
  rw [mul_div_cancel_left₀ _ hCw.ne', div_le_iff₀ hC]
  constructor <;> intro h <;> nlinarith

lemma G_mono (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs)))
    (S : Set ℝ) (hS : S ⊆ Set.Icc 0 (Cw*(Tb-Tr))) (hconv : Convex ℝ S)
    (sgn : ∀ H ∈ S, (H-D)/(Cw+Cs) ≤ H/Cw) :
    MonotoneOn (fun H => Fw Cw P h H - Fs Cw Cs D P h H) S := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  have hd := G_deriv Cw Cs D P h Tr Tb hCw hC hh hP hs
  apply monotoneOn_of_deriv_nonneg hconv
  · intro H hH; exact (hd H (hS hH)).continuousAt.continuousWithinAt
  · intro H hH; exact (hd H (hS (interior_subset hH))).differentiableAt.differentiableWithinAt
  · intro H hH
    have hH' := interior_subset hH
    rw [(hd H (hS hH')).deriv]
    apply sub_nonneg.mpr
    apply one_div_le_one_div_of_le (hw H (hS hH'))
    have := sgn H hH'
    nlinarith

lemma G_anti (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs)))
    (S : Set ℝ) (hS : S ⊆ Set.Icc 0 (Cw*(Tb-Tr))) (hconv : Convex ℝ S)
    (sgn : ∀ H ∈ S, H/Cw ≤ (H-D)/(Cw+Cs)) :
    AntitoneOn (fun H => Fw Cw P h H - Fs Cw Cs D P h H) S := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  have hd := G_deriv Cw Cs D P h Tr Tb hCw hC hh hP hs
  apply antitoneOn_of_deriv_nonpos hconv
  · intro H hH; exact (hd H (hS hH)).continuousAt.continuousWithinAt
  · intro H hH; exact (hd H (hS (interior_subset hH))).differentiableAt.differentiableWithinAt
  · intro H hH
    have hH' := interior_subset hH
    rw [(hd H (hS hH')).deriv]
    apply sub_nonpos.mpr
    apply one_div_le_one_div_of_le (hs H (hS hH'))
    have := sgn H hH'
    nlinarith

/-- ANSWER 1 (sign-free).  Salt of fixed amount (Cs, D arbitrary sign, C=Cw+Cs>0).
 If between water temperatures Ta1 ≤ Ta2 salt addition always COOLS (D+Cs(x-Tr) ≥ 0 for x in [Ta1,Ta2]),
 adding at Ta1 is no slower than at Ta2; if it always WARMS (≤ 0), adding at Ta2 is no slower. -/
theorem answer_theorem : ∀ (Cw Cs D P h Tr T0 Tb : ℝ),
    0 < Cw → 0 < Cw + Cs → 0 < h → h*(Tb-Tr) < P → Tr ≤ T0 →
    (∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs))) →
    ∀ Ta1 Ta2 : ℝ, T0 ≤ Ta1 → Ta1 ≤ Ta2 → Ta2 ≤ Tb →
      ((∀ x, Ta1 ≤ x → x ≤ Ta2 → 0 ≤ D + Cs*(x-Tr)) →
          boilTime Cw Cs D P h Tr T0 Tb Ta1 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta2) ∧
      ((∀ x, Ta1 ≤ x → x ≤ Ta2 → D + Cs*(x-Tr) ≤ 0) →
          boilTime Cw Cs D P h Tr T0 Tb Ta2 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta1) := by
  intro Cw Cs D P h Tr T0 Tb hCw hC hh hP hTr hs Ta1 Ta2 h1 h12 h2
  have hmem : ∀ x, Ta1 ≤ x → x ≤ Ta2 → Cw*(x-Tr) ∈ Set.Icc 0 (Cw*(Tb-Tr)) := fun x a b =>
    ⟨by nlinarith, by nlinarith⟩
  have hS : Set.Icc (Cw*(Ta1-Tr)) (Cw*(Ta2-Tr)) ⊆ Set.Icc 0 (Cw*(Tb-Tr)) := by
    intro H hH
    exact ⟨by nlinarith [hH.1], by nlinarith [hH.2]⟩
  have hHx : ∀ H ∈ Set.Icc (Cw*(Ta1-Tr)) (Cw*(Ta2-Tr)), ∃ x, Ta1 ≤ x ∧ x ≤ Ta2 ∧ H = Cw*(x-Tr) := by
    intro H hH
    refine ⟨H/Cw + Tr, ?_, ?_, ?_⟩
    · have : Ta1 - Tr ≤ H/Cw := by rw [le_div_iff₀ hCw]; nlinarith [hH.1]
      linarith
    · have : H/Cw ≤ Ta2 - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
      linarith
    · field_simp; ring
  constructor
  · intro hx
    have m := G_mono Cw Cs D P h Tr Tb hCw hC hh hP hs _ hS (convex_Icc _ _) (by
      intro H hH
      obtain ⟨x, a, b, rfl⟩ := hHx H hH
      exact (mix_iff Cw Cs D Tr x hCw hC).mpr (hx x a b))
      ⟨le_rfl, by nlinarith⟩ ⟨by nlinarith, le_rfl⟩ (by nlinarith)
    simp only [boilTime]; simp only at m; linarith
  · intro hx
    have m := G_anti Cw Cs D P h Tr Tb hCw hC hh hP hs _ hS (convex_Icc _ _) (by
      intro H hH
      obtain ⟨x, a, b, rfl⟩ := hHx H hH
      have := hx x a b
      have h3 : ¬ ((Cw*(x-Tr) - D)/(Cw+Cs) < Cw*(x-Tr)/Cw) → True := fun _ => trivial
      rw [mul_div_cancel_left₀ _ hCw.ne', le_div_iff₀ hC]
      nlinarith)
      ⟨le_rfl, by nlinarith⟩ ⟨by nlinarith, le_rfl⟩ (by nlinarith)
    simp only [boilTime]; simp only at m; linarith
attempt-002/extracted.lean
import Mathlib.Analysis.SpecialFunctions.Log.Deriv
import Mathlib.Analysis.Calculus.MeanValue

/-- time-integral of dH/(P - loss) for pure water (antiderivative, h>0) -/
noncomputable def Fw (Cw P h H : ℝ) : ℝ := -(Cw/h) * Real.log (P - h*(H/Cw))

/-- same for the salt solution -/
noncomputable def Fs (Cw Cs D P h H : ℝ) : ℝ := -((Cw+Cs)/h) * Real.log (P - h*((H-D)/(Cw+Cs)))

/-- Total time to reach Tb from T0 when the salt is added once the water is at Ta:
 heating water alone from T0 to Ta, then solution from the post-mixing state up to Tb. -/
noncomputable def boilTime (Cw Cs D P h Tr T0 Tb Ta : ℝ) : ℝ :=
  (Fw Cw P h (Cw*(Ta-Tr)) - Fw Cw P h (Cw*(T0-Tr))) +
  (Fs Cw Cs D P h ((Cw+Cs)*(Tb-Tr)+D) - Fs Cw Cs D P h (Cw*(Ta-Tr)))


/-! Conclusions (statements of the proved theorems). -/
def conclusion_answer_theorem : Prop := ∀ (Cw Cs D P h Tr T0 Tb : ℝ), 0 < Cw → 0 < Cw + Cs → 0 < h → h*(Tb-Tr) < P → Tr ≤ T0 → (∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs))) → ∀ Ta1 Ta2 : ℝ, T0 ≤ Ta1 → Ta1 ≤ Ta2 → Ta2 ≤ Tb → ((∀ x, Ta1 ≤ x → x ≤ Ta2 → 0 ≤ D + Cs*(x-Tr)) → boilTime Cw Cs D P h Tr T0 Tb Ta1 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta2) ∧ ((∀ x, Ta1 ≤ x → x ≤ Ta2 → D + Cs*(x-Tr) ≤ 0) → boilTime Cw Cs D P h Tr T0 Tb Ta2 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta1)
checks/7fdbd8cda6304fd3ae91fdd97ab702a9/submitted.lean
import Mathlib.Analysis.SpecialFunctions.Log.Deriv
import Mathlib.Analysis.Calculus.MeanValue

/-! Model (see report.txt).  H = enthalpy (J) measured from "everything at room temperature Tr".
 Pure water, heat capacity Cw: temperature Tr + H/Cw.
 Salt solution, total heat capacity Cw+Cs>0 (Cs of EITHER sign; literature: Cs<0), dissolution enthalpy D (at Tr) of either sign:
 temperature Tr + (H-D)/(Cw+Cs).  Salt (at Tr) is mixed in instantly, so H is conserved.
 Heater power P constant, loss h*(T-Tr):  dH/dt = P - h*(T-Tr).
 Separating variables, the time to move from H1 to H2 is Fw/Fs(H2) - Fw/Fs(H1) where
 Fw' = 1/(P - h*(H/Cw)), Fs' = 1/(P - h*((H-D)/(Cw+Cs))) (explicit logarithms below).
 Tb = boiling point of the final solution (fixed by salt amount, not by timing). -/

/-- time-integral of dH/(P - loss) for pure water (antiderivative, h>0) -/
noncomputable def Fw (Cw P h H : ℝ) : ℝ := -(Cw/h) * Real.log (P - h*(H/Cw))
/-- same for the salt solution -/
noncomputable def Fs (Cw Cs D P h H : ℝ) : ℝ := -((Cw+Cs)/h) * Real.log (P - h*((H-D)/(Cw+Cs)))

/-- Total time to reach Tb from T0 when the salt is added once the water is at Ta:
 heating water alone from T0 to Ta, then solution from the post-mixing state up to Tb. -/
noncomputable def boilTime (Cw Cs D P h Tr T0 Tb Ta : ℝ) : ℝ :=
  (Fw Cw P h (Cw*(Ta-Tr)) - Fw Cw P h (Cw*(T0-Tr))) +
  (Fs Cw Cs D P h ((Cw+Cs)*(Tb-Tr)+D) - Fs Cw Cs D P h (Cw*(Ta-Tr)))

lemma G_deriv (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs))) :
    ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), HasDerivAt (fun H => Fw Cw P h H - Fs Cw Cs D P h H)
      (1/(P - h*(H/Cw)) - 1/(P - h*((H-D)/(Cw+Cs)))) H := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  intro H hH
  have a1 : HasDerivAt (fun H : ℝ => P - h*(H/Cw)) (-(h/Cw)) H := by
    have := ((hasDerivAt_id H).div_const Cw).const_mul h
    simpa [div_eq_mul_inv, mul_comm] using (this.const_sub P)
  have a2 : HasDerivAt (fun H : ℝ => P - h*((H-D)/(Cw+Cs))) (-(h/(Cw+Cs))) H := by
    have := (((hasDerivAt_id H).sub_const D).div_const (Cw+Cs)).const_mul h
    simpa [div_eq_mul_inv, mul_comm] using (this.const_sub P)
  have l1 := (a1.log (hw H hH).ne').const_mul (-(Cw/h))
  have l2 := (a2.log (hs H hH).ne').const_mul (-((Cw+Cs)/h))
  have := l1.sub l2
  convert this using 1
  · funext x; rfl
  · have e1 := (hw H hH).ne'
    have e2 := (hs H hH).ne'
    have e3 := hh.ne'
    have e4 := hCw.ne'
    have e5 := hC.ne'
    field_simp

/-- Salt added at water temperature x changes the temperature at once by
 (D + Cs (x-Tr))/(Cw+Cs) downward (a cooling if D + Cs (x-Tr) > 0).  -/
lemma mix_iff (Cw Cs D Tr x : ℝ) (hCw : 0 < Cw) (hC : 0 < Cw + Cs) :
    (Cw*(x-Tr) - D)/(Cw+Cs) ≤ Cw*(x-Tr)/Cw ↔ 0 ≤ D + Cs*(x-Tr) := by
  rw [mul_div_cancel_left₀ _ hCw.ne', div_le_iff₀ hC]
  constructor <;> intro h <;> nlinarith

lemma G_mono (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs)))
    (S : Set ℝ) (hS : S ⊆ Set.Icc 0 (Cw*(Tb-Tr))) (hconv : Convex ℝ S)
    (sgn : ∀ H ∈ S, (H-D)/(Cw+Cs) ≤ H/Cw) :
    MonotoneOn (fun H => Fw Cw P h H - Fs Cw Cs D P h H) S := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  have hd := G_deriv Cw Cs D P h Tr Tb hCw hC hh hP hs
  apply monotoneOn_of_deriv_nonneg hconv
  · intro H hH; exact (hd H (hS hH)).continuousAt.continuousWithinAt
  · intro H hH; exact (hd H (hS (interior_subset hH))).differentiableAt.differentiableWithinAt
  · intro H hH
    have hH' := interior_subset hH
    rw [(hd H (hS hH')).deriv]
    apply sub_nonneg.mpr
    apply one_div_le_one_div_of_le (hw H (hS hH'))
    have := sgn H hH'
    nlinarith

lemma G_anti (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs)))
    (S : Set ℝ) (hS : S ⊆ Set.Icc 0 (Cw*(Tb-Tr))) (hconv : Convex ℝ S)
    (sgn : ∀ H ∈ S, H/Cw ≤ (H-D)/(Cw+Cs)) :
    AntitoneOn (fun H => Fw Cw P h H - Fs Cw Cs D P h H) S := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  have hd := G_deriv Cw Cs D P h Tr Tb hCw hC hh hP hs
  apply antitoneOn_of_deriv_nonpos hconv
  · intro H hH; exact (hd H (hS hH)).continuousAt.continuousWithinAt
  · intro H hH; exact (hd H (hS (interior_subset hH))).differentiableAt.differentiableWithinAt
  · intro H hH
    have hH' := interior_subset hH
    rw [(hd H (hS hH')).deriv]
    apply sub_nonpos.mpr
    apply one_div_le_one_div_of_le (hs H (hS hH'))
    have := sgn H hH'
    nlinarith

/-- ANSWER 1 (sign-free).  Salt of fixed amount (Cs, D arbitrary sign, C=Cw+Cs>0).
 If between water temperatures Ta1 ≤ Ta2 salt addition always COOLS (D+Cs(x-Tr) ≥ 0 for x in [Ta1,Ta2]),
 adding at Ta1 is no slower than at Ta2; if it always WARMS (≤ 0), adding at Ta2 is no slower. -/
theorem answer_theorem : ∀ (Cw Cs D P h Tr T0 Tb : ℝ),
    0 < Cw → 0 < Cw + Cs → 0 < h → h*(Tb-Tr) < P → Tr ≤ T0 →
    (∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs))) →
    ∀ Ta1 Ta2 : ℝ, T0 ≤ Ta1 → Ta1 ≤ Ta2 → Ta2 ≤ Tb →
      ((∀ x, Ta1 ≤ x → x ≤ Ta2 → 0 ≤ D + Cs*(x-Tr)) →
          boilTime Cw Cs D P h Tr T0 Tb Ta1 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta2) ∧
      ((∀ x, Ta1 ≤ x → x ≤ Ta2 → D + Cs*(x-Tr) ≤ 0) →
          boilTime Cw Cs D P h Tr T0 Tb Ta2 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta1) := by
  intro Cw Cs D P h Tr T0 Tb hCw hC hh hP hTr hs Ta1 Ta2 h1 h12 h2
  have hmem : ∀ x, Ta1 ≤ x → x ≤ Ta2 → Cw*(x-Tr) ∈ Set.Icc 0 (Cw*(Tb-Tr)) := fun x a b =>
    ⟨by nlinarith, by nlinarith⟩
  have hS : Set.Icc (Cw*(Ta1-Tr)) (Cw*(Ta2-Tr)) ⊆ Set.Icc 0 (Cw*(Tb-Tr)) := by
    intro H hH
    exact ⟨by nlinarith [hH.1], by nlinarith [hH.2]⟩
  have hHx : ∀ H ∈ Set.Icc (Cw*(Ta1-Tr)) (Cw*(Ta2-Tr)), ∃ x, Ta1 ≤ x ∧ x ≤ Ta2 ∧ H = Cw*(x-Tr) := by
    intro H hH
    refine ⟨H/Cw + Tr, ?_, ?_, ?_⟩
    · have := hH.1; rw [le_div_iff₀ hCw] at *; nlinarith [this]
    · have := hH.2; nlinarith [div_le_iff₀ hCw |>.mpr (by nlinarith : H ≤ (Ta2 - Tr) * Cw)]
    · field_simp; ring
  constructor
  · intro hx
    have m := G_mono Cw Cs D P h Tr Tb hCw hC hh hP hs _ hS (convex_Icc _ _) (by
      intro H hH
      obtain ⟨x, a, b, rfl⟩ := hHx H hH
      exact (mix_iff Cw Cs D Tr x hCw hC).mpr (hx x a b))
      ⟨le_rfl, by nlinarith⟩ ⟨by nlinarith, le_rfl⟩ (by nlinarith)
    simp only [boilTime]; simp only at m; linarith
  · intro hx
    have m := G_anti Cw Cs D P h Tr Tb hCw hC hh hP hs _ hS (convex_Icc _ _) (by
      intro H hH
      obtain ⟨x, a, b, rfl⟩ := hHx H hH
      have := hx x a b
      have h3 : ¬ ((Cw*(x-Tr) - D)/(Cw+Cs) < Cw*(x-Tr)/Cw) → True := fun _ => trivial
      rw [mul_div_cancel_left₀ _ hCw.ne', le_div_iff₀ hC]
      nlinarith)
      ⟨le_rfl, by nlinarith⟩ ⟨by nlinarith, le_rfl⟩ (by nlinarith)
    simp only [boilTime]; simp only at m; linarith

Round 3

Messages

Bash input
{
  "command": "cd /work; ls -la; cat report.txt | head -80; cat candidate.json; ls responses* inputs* 2>/dev/null"
}
Bash output · error
Exit code 2
total 40
drwxrwxr-x 3 1001 1001 4096 Nov 17 14:37 .
drwxr-xr-x 1 root root 4096 Nov 17 14:37 ..
-rw-r--r-- 1 1001 1001 7550 Nov 17 14:33 Model.lean
-rw-r--r-- 1 1001 1001 1219 Nov 17 14:34 candidate.json
drwxr-xr-x 8 1001 1001 4096 Nov 17 14:34 checks
-rw------- 1 1001 1001  859 Nov 17 14:37 human-feedback.json
-rw------- 1 1001 1001 5978 Nov 17 14:35 last-review-packet.json
-rw-r--r-- 1 1001 1001 2626 Nov 17 14:34 report.txt
REVISION (cycle 2). Reviewer: do not assume Cs>=0; justify the sign of the heat-capacity change.
Finding: the premise Cs>=0 is probably FALSE. Dissolved NaCl has a NEGATIVE partial molar heat capacity
(about -84 J/K/mol at infinite dilution, Abraham & Marcus as reported by a web search snippet; not verified
against the primary source), so 1 kg of water plus salt has slightly LESS total heat capacity than 1 kg of
water plus the pot. Dissolution is endothermic at 25 C (+3.88 kJ/mol, tutorial table), and the enthalpy of
solution falls with temperature because Cp(dissolved) < Cp(solid NaCl, ~50 J/K/mol). I did not obtain
primary-source numbers (Perron-Fortier-Desnoyers 1975; NIST/JPCRD NaCl review are the places to look).
So the earlier claim "salt early always wins" is withdrawn. Cs and D are now of arbitrary sign (only Cw+Cs>0).

MODEL (choices, all mine): lumped pot+water heat capacity Cw; constant stove power P; Newton loss h(T-Tr);
no evaporation; salt at Tr dissolves instantly, enthalpy H conserved at mixing; solution temperature
Tr+(H-D)/(Cw+Cs) with D = enthalpy of solution at Tr, Cs = total heat-capacity change, constants;
final boiling point Tb fixed by salt amount; Ta in [T0,Tb]; stove can always overcome losses at all visited
states (premise hs). Facts supplied: 1 L water, 20 C, stove, salt later; salt amount, pot, heater, room unspecified.

RESULT (answer_theorem, proved in Lean, no sorry): time to boil as a function of the temperature Ta at which
salt goes in. Adding salt makes the temperature jump by -(D+Cs(Ta-Tr))/(Cw+Cs).
 - If between Ta1<=Ta2 salt addition always cools (D+Cs(x-Tr)>=0), adding at Ta1 is no slower than at Ta2.
 - If it always warms (<=0), adding at Ta2 is no slower.
With Cs<0 the switch is at Ta* = Tr + D/|Cs|. Rough illustration with the (unverified) constants above:
Ta* ~ 20+3880/84 ~ 66 C. So the sign changes inside the 20-100 C range and the model gives NO unconditional
single best temperature. In a constant-parameter crude estimate with weak losses the early end looked
slightly better, but that was NOT proved and depends on h/P and the salt amount, which are unspecified.
Honest answer: the effect is tiny (a temperature jump of ~1 K per mole of salt in 1 L); the model cannot
decide it without salt amount, h/P and temperature-dependent data. Practically: time to boil is
dominated by the energy needed, so timing hardly matters; add salt early to avoid boil-over.
Not proved: endpoint-optimality (minimum is at 20 C or at the end, not interior, when Cs<0), magnitudes,
constants truth, temperature dependence of D and Cs. No measurement or request run.
{
 "proof_file": "Model.lean",
 "narrative_file": "report.txt",
 "conclusions": {
  "answer": "answer_theorem"
 },
 "user_facts": [
  "1 L of water at 20 C in a pot on a stove",
  "salt will be added; goal: salted water reaches its boiling point as soon as possible",
  "salt quantity, pot, heater, room unspecified"
 ],
 "model_choices": [
  "Cs (total heat-capacity change on adding salt) and D (enthalpy of solution) of arbitrary sign; only Cw+Cs>0 assumed",
  "constant heater power P; Newtonian loss h(T-Tr); lumped heat capacities; no evaporation",
  "salt added at Tr, instant mixing, enthalpy conserved",
  "Tb fixed by salt amount; Ta in [T0,Tb]; stove overcomes losses at all visited states (premise)"
 ],
 "sources": [
  "Web search snippets: Abraham & Marcus partial molar Cp of NaCl ~ -84 J/K/mol; NaCl dissolution +3.88 kJ/mol (tutorial table); unverified against primary sources"
 ],
 "incorporated_inputs": [],
 "proof_scope": "Proves a sign-free comparison theorem: the sign of D+Cs(x-Tr) over a temperature interval determines whether later or earlier salt addition is faster. Does not prove a unique optimum; the sign of Cs is not assumed (literature suggests Cs<0), and no magnitudes are claimed."
}
Bash input
{
  "command": "cd /work; cat Model.lean; cat human-feedback.json; ls checks checks/*|head -30"
}
Bash output
import Mathlib.Analysis.SpecialFunctions.Log.Deriv
import Mathlib.Analysis.Calculus.MeanValue

/-! Model (see report.txt).  H = enthalpy (J) measured from "everything at room temperature Tr".
 Pure water, heat capacity Cw: temperature Tr + H/Cw.
 Salt solution, total heat capacity Cw+Cs>0 (Cs of EITHER sign; literature: Cs<0), dissolution enthalpy D (at Tr) of either sign:
 temperature Tr + (H-D)/(Cw+Cs).  Salt (at Tr) is mixed in instantly, so H is conserved.
 Heater power P constant, loss h*(T-Tr):  dH/dt = P - h*(T-Tr).
 Separating variables, the time to move from H1 to H2 is Fw/Fs(H2) - Fw/Fs(H1) where
 Fw' = 1/(P - h*(H/Cw)), Fs' = 1/(P - h*((H-D)/(Cw+Cs))) (explicit logarithms below).
 Tb = boiling point of the final solution (fixed by salt amount, not by timing). -/

/-- time-integral of dH/(P - loss) for pure water (antiderivative, h>0) -/
noncomputable def Fw (Cw P h H : ℝ) : ℝ := -(Cw/h) * Real.log (P - h*(H/Cw))
/-- same for the salt solution -/
noncomputable def Fs (Cw Cs D P h H : ℝ) : ℝ := -((Cw+Cs)/h) * Real.log (P - h*((H-D)/(Cw+Cs)))

/-- Total time to reach Tb from T0 when the salt is added once the water is at Ta:
 heating water alone from T0 to Ta, then solution from the post-mixing state up to Tb. -/
noncomputable def boilTime (Cw Cs D P h Tr T0 Tb Ta : ℝ) : ℝ :=
  (Fw Cw P h (Cw*(Ta-Tr)) - Fw Cw P h (Cw*(T0-Tr))) +
  (Fs Cw Cs D P h ((Cw+Cs)*(Tb-Tr)+D) - Fs Cw Cs D P h (Cw*(Ta-Tr)))

lemma G_deriv (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs))) :
    ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), HasDerivAt (fun H => Fw Cw P h H - Fs Cw Cs D P h H)
      (1/(P - h*(H/Cw)) - 1/(P - h*((H-D)/(Cw+Cs)))) H := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  intro H hH
  have a1 : HasDerivAt (fun H : ℝ => P - h*(H/Cw)) (-(h/Cw)) H := by
    have := ((hasDerivAt_id H).div_const Cw).const_mul h
    simpa [div_eq_mul_inv, mul_comm] using (this.const_sub P)
  have a2 : HasDerivAt (fun H : ℝ => P - h*((H-D)/(Cw+Cs))) (-(h/(Cw+Cs))) H := by
    have := (((hasDerivAt_id H).sub_const D).div_const (Cw+Cs)).const_mul h
    simpa [div_eq_mul_inv, mul_comm] using (this.const_sub P)
  have l1 := (a1.log (hw H hH).ne').const_mul (-(Cw/h))
  have l2 := (a2.log (hs H hH).ne').const_mul (-((Cw+Cs)/h))
  have := l1.sub l2
  convert this using 1
  · funext x; rfl
  · have e1 := (hw H hH).ne'
    have e2 := (hs H hH).ne'
    have e3 := hh.ne'
    have e4 := hCw.ne'
    have e5 := hC.ne'
    field_simp

/-- Salt added at water temperature x changes the temperature at once by
 (D + Cs (x-Tr))/(Cw+Cs) downward (a cooling if D + Cs (x-Tr) > 0).  -/
lemma mix_iff (Cw Cs D Tr x : ℝ) (hCw : 0 < Cw) (hC : 0 < Cw + Cs) :
    (Cw*(x-Tr) - D)/(Cw+Cs) ≤ Cw*(x-Tr)/Cw ↔ 0 ≤ D + Cs*(x-Tr) := by
  rw [mul_div_cancel_left₀ _ hCw.ne', div_le_iff₀ hC]
  constructor <;> intro h <;> nlinarith

lemma G_mono (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs)))
    (S : Set ℝ) (hS : S ⊆ Set.Icc 0 (Cw*(Tb-Tr))) (hconv : Convex ℝ S)
    (sgn : ∀ H ∈ S, (H-D)/(Cw+Cs) ≤ H/Cw) :
    MonotoneOn (fun H => Fw Cw P h H - Fs Cw Cs D P h H) S := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  have hd := G_deriv Cw Cs D P h Tr Tb hCw hC hh hP hs
  apply monotoneOn_of_deriv_nonneg hconv
  · intro H hH; exact (hd H (hS hH)).continuousAt.continuousWithinAt
  · intro H hH; exact (hd H (hS (interior_subset hH))).differentiableAt.differentiableWithinAt
  · intro H hH
    have hH' := interior_subset hH
    rw [(hd H (hS hH')).deriv]
    apply sub_nonneg.mpr
    apply one_div_le_one_div_of_le (hw H (hS hH'))
    have := sgn H hH'
    nlinarith

lemma G_anti (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs)))
    (S : Set ℝ) (hS : S ⊆ Set.Icc 0 (Cw*(Tb-Tr))) (hconv : Convex ℝ S)
    (sgn : ∀ H ∈ S, H/Cw ≤ (H-D)/(Cw+Cs)) :
    AntitoneOn (fun H => Fw Cw P h H - Fs Cw Cs D P h H) S := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  have hd := G_deriv Cw Cs D P h Tr Tb hCw hC hh hP hs
  apply antitoneOn_of_deriv_nonpos hconv
  · intro H hH; exact (hd H (hS hH)).continuousAt.continuousWithinAt
  · intro H hH; exact (hd H (hS (interior_subset hH))).differentiableAt.differentiableWithinAt
  · intro H hH
    have hH' := interior_subset hH
    rw [(hd H (hS hH')).deriv]
    apply sub_nonpos.mpr
    apply one_div_le_one_div_of_le (hs H (hS hH'))
    have := sgn H hH'
    nlinarith

/-- ANSWER 1 (sign-free).  Salt of fixed amount (Cs, D arbitrary sign, C=Cw+Cs>0).
 If between water temperatures Ta1 ≤ Ta2 salt addition always COOLS (D+Cs(x-Tr) ≥ 0 for x in [Ta1,Ta2]),
 adding at Ta1 is no slower than at Ta2; if it always WARMS (≤ 0), adding at Ta2 is no slower. -/
theorem answer_theorem : ∀ (Cw Cs D P h Tr T0 Tb : ℝ),
    0 < Cw → 0 < Cw + Cs → 0 < h → h*(Tb-Tr) < P → Tr ≤ T0 →
    (∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs))) →
    ∀ Ta1 Ta2 : ℝ, T0 ≤ Ta1 → Ta1 ≤ Ta2 → Ta2 ≤ Tb →
      ((∀ x, Ta1 ≤ x → x ≤ Ta2 → 0 ≤ D + Cs*(x-Tr)) →
          boilTime Cw Cs D P h Tr T0 Tb Ta1 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta2) ∧
      ((∀ x, Ta1 ≤ x → x ≤ Ta2 → D + Cs*(x-Tr) ≤ 0) →
          boilTime Cw Cs D P h Tr T0 Tb Ta2 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta1) := by
  intro Cw Cs D P h Tr T0 Tb hCw hC hh hP hTr hs Ta1 Ta2 h1 h12 h2
  have hmem : ∀ x, Ta1 ≤ x → x ≤ Ta2 → Cw*(x-Tr) ∈ Set.Icc 0 (Cw*(Tb-Tr)) := fun x a b =>
    ⟨by nlinarith, by nlinarith⟩
  have hS : Set.Icc (Cw*(Ta1-Tr)) (Cw*(Ta2-Tr)) ⊆ Set.Icc 0 (Cw*(Tb-Tr)) := by
    intro H hH
    exact ⟨by nlinarith [hH.1], by nlinarith [hH.2]⟩
  have hHx : ∀ H ∈ Set.Icc (Cw*(Ta1-Tr)) (Cw*(Ta2-Tr)), ∃ x, Ta1 ≤ x ∧ x ≤ Ta2 ∧ H = Cw*(x-Tr) := by
    intro H hH
    refine ⟨H/Cw + Tr, ?_, ?_, ?_⟩
    · have : Ta1 - Tr ≤ H/Cw := by rw [le_div_iff₀ hCw]; nlinarith [hH.1]
      linarith
    · have : H/Cw ≤ Ta2 - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
      linarith
    · field_simp; ring
  constructor
  · intro hx
    have m := G_mono Cw Cs D P h Tr Tb hCw hC hh hP hs _ hS (convex_Icc _ _) (by
      intro H hH
      obtain ⟨x, a, b, rfl⟩ := hHx H hH
      exact (mix_iff Cw Cs D Tr x hCw hC).mpr (hx x a b))
      ⟨le_rfl, by nlinarith⟩ ⟨by nlinarith, le_rfl⟩ (by nlinarith)
    simp only [boilTime]; simp only at m; linarith
  · intro hx
    have m := G_anti Cw Cs D P h Tr Tb hCw hC hh hP hs _ hS (convex_Icc _ _) (by
      intro H hH
      obtain ⟨x, a, b, rfl⟩ := hHx H hH
      have := hx x a b
      have h3 : ¬ ((Cw*(x-Tr) - D)/(Cw+Cs) < Cw*(x-Tr)/Cw) → True := fun _ => trivial
      rw [mul_div_cancel_left₀ _ hCw.ne', le_div_iff₀ hC]
      nlinarith)
      ⟨le_rfl, by nlinarith⟩ ⟨by nlinarith, le_rfl⟩ (by nlinarith)
    simp only [boilTime]; simp only at m; linarith
{
  "decision": "reject",
  "binding": "284dbd95781f18d774c527db8e1454b52fcf26da70dcc1a284ebb7f2abb4eb8c",
  "reason": "Human follow-up: the chosen Newtonian heat-loss/lumped heating model is accepted for this task. The sign of Cs is still to be established, not presumed nonnegative. Determine and justify whether Cs is positive, negative or changes sign in the relevant temperature/concentration regime, using literature or requested experiments, and state the resulting implication for salt-addition timing. If an amount/concentration or other input is needed, request it explicitly. Preserve the accepted heat-loss model while resolving this physical-property question. This is a request for further work, not acceptance of the complete theorem or final recommendation.",
  "time": "2026-11-17T14:37:13.321544+00:00",
  "verification_verdict": "proved"
}
checks:
248420fe49ad4124bd13c839d06e22c4
7e4be0bd42b444199e65b96370c18486
7fdbd8cda6304fd3ae91fdd97ab702a9
91bf5367d0354c09a0f1841d6c390708
9d2a29707556410397d3c62a510d59a9
b7ec15d94a8146bfadfbd5cc72230a57

checks/248420fe49ad4124bd13c839d06e22c4:
Checked.lean
extracted.json
extracted.lean
lean.log
submitted-candidate.json
submitted-report.txt
submitted.lean
verdict.json

checks/7e4be0bd42b444199e65b96370c18486:
Checked.lean
extracted.json
extracted.lean
lean.log
submitted-candidate.json
submitted-report.txt
submitted.lean
verdict.json

checks/7fdbd8cda6304fd3ae91fdd97ab702a9:
Checked.lean
WebSearch input
{
  "query": "apparent molar heat capacity NaCl aqueous solution temperature dependence 25 to 100 °C negative infinite dilution Pitzer Cp values"
}
WebSearch input
{
  "query": "specific heat capacity of sodium chloride solution table J/g/K by mass percent 1% 5% 10% 20% 25 °C"
}
WebSearch output
Web search results for query: "specific heat capacity of sodium chloride solution table J/g/K by mass percent 1% 5% 10% 20% 25 °C"

Links: [{"title":"Sodium chloride (data page)","url":"https://en.wikipedia.org/wiki/Sodium_chloride_(data_page)"},{"title":"Sodium chloride","url":"https://en.wikipedia.org/wiki/Sodium_chloride"},{"title":"Thermodynamic Properties of Aqueous Sodium Chloride ...","url":"https://srd.nist.gov/jpcrdreprint/1.555709.pdf"},{"title":"Volumetric Properties of Aqueous Sodium Chloride Solutions","url":"https://srd.nist.gov/jpcrdreprint/1.555660.pdf"},{"title":"Sodium","url":"https://en.wikipedia.org/wiki/Sodium"},{"title":"Sodium iodate","url":"https://en.wikipedia.org/wiki/Sodium_iodate"},{"title":"Lithium chloride","url":"https://en.wikipedia.org/wiki/Lithium_chloride"},{"title":"Sodium chloride (NaCl) — solution properties · ProcessConvert","url":"https://www.processconvert.com/substances/sodium-chloride"},{"title":"Sodium bromide","url":"https://en.wikipedia.org/wiki/Sodium_bromide"}]

The search turned up only a few NaCl solution values, not a full 1/5/10/20 wt% table at 25 °C.

**Best data found (ProcessConvert, computed values, 2–24 wt% and 0–80 °C):**
- At 25 °C, 4 wt% NaCl has a specific heat of about 3981 J/kg·K. That is about 3.98 J/g·K.
- At 25 °C, 20 wt% NaCl has a specific heat of about 3411 J/kg·K. That is about 3.41 J/g·K.
- At 25 °C, 24 wt% NaCl has a specific heat of about 3314 J/kg·K. That is about 3.31 J/g·K.
- The page also has a table with columns at 2, 4, 6 … 24 wt%. One row shows 3728 J/kg·K at 10 °C for what looks like the 10 wt% column. I couldn't confirm that mapping, and the 25 °C values for 5% and 10% weren't in the extract.

**Other sources:**
- The NIST/JPCRD paper on aqueous NaCl thermodynamic properties (srd.nist.gov/jpcrdreprint/1.555709.pdf) is the rigorous source. It gives heat capacity as a function of molality, temperature and pressure, not as a mass-percent table.
- Wikipedia's NaCl data page gives 0.853 J/(g·K) for solid NaCl, which isn't the solution value.

**Rough interpolation (my estimate, not from a source):** 1% is about 4.1 J/g·K, 5% about 3.9, and 10% about 3.7. Check these against the ProcessConvert table or the NIST paper before relying on them.


REMINDER: You MUST include the sources above in your response to the user using markdown hyperlinks.
WebSearch output
Web search results for query: "apparent molar heat capacity NaCl aqueous solution temperature dependence 25 to 100 °C negative infinite dilution Pitzer Cp values"

Links: [{"title":"Heat Capacities of Concentrated Aqueous Solutions of Sodium Sulfate, Sodium Carbonate, and Sodium Hydroxide at 25 °C","url":"https://www.researchgate.net/publication/43980648_Heat_Capacities_of_Concentrated_Aqueous_Solutions_of_Sodium_Sulfate_Sodium_Carbonate_and_Sodium_Hydroxide_at_25_C"},{"title":"(PDF) Thermodynamic Properties of NaCl Solutions at Temperatures Lower than 0°C","url":"https://www.researchgate.net/publication/290800827_Thermodynamic_Properties_of_NaCl_Solutions_at_Temperatures_Lower_than_0C"},{"title":"(PDF) Thermodynamic Properties of NaCl Solutions at Subzero Temperatures","url":"https://www.researchgate.net/publication/226193918_Thermodynamic_Properties_of_NaCl_Solutions_at_Subzero_Temperatures"},{"title":"The Thermodynamic Properties of High Temperature Aqueous Solutions. I. Standard Partial Molal Heat Capacities of Sodium Chloride and Barium Chloride from 0 to 100°1","url":"https://pubs.acs.org/doi/10.1021/ja01476a013"},{"title":"Specific Heats of Aqueous Solutions of NaCl, NaBr ... - DTIC","url":"https://apps.dtic.mil/sti/tr/pdf/ADA045707.pdf"},{"title":"Vol.:(0123456789) Journal of Solution Chemistry","url":"https://d-nb.info/1352894033/34"},{"title":"Modeling the Heat Capacities of Aqueous 1−1 Electrolyte Solutions with Pitzer's Equations","url":"https://pubs.acs.org/doi/10.1021/jp951355l"},{"title":"This is an electronic reprint of the original article.","url":"https://aaltodoc.aalto.fi/server/api/core/bitstreams/8a8efdcb-a5ee-4fec-8d50-949aae744598/content"},{"title":"link.springer.com","url":"https://link.springer.com/article/10.1023/A:1014445917207"}]

The search turned up a few useful sources, but no single table covering 25–100 °C.

- **Infinite-dilution values from a DTIC report:** The most concrete numbers come from a DTIC report (ADA045707). Its table lists apparent molal heat capacities of NaCl extrapolated to zero molality in cal/mole-deg: −38.0 at 5 °C, −21.1 at 25 °C, −15.5 at 45 °C, −14.3 at 65 °C and −15.4 at 85 °C. These values are negative and become less negative with temperature, reaching a minimum magnitude near 65 °C. The report is a table of experimental values. The OCR is messy, so check the original before relying on the numbers.
- **Pitzer form at 25 °C:** The Pitzer equation for apparent molal heat capacity is φCp = C̄p,2° + (AJ/1.2) ln(1 + 1.2 I^1/2) − 2RT²[mB^J_MX + m²C^J_MX]. This is from a paper that fits 1–1 electrolytes at 25 °C (J. Phys. Chem., jp951355l). Here C̄p,2° is the infinite-dilution term, and B^J and C^J are ion-interaction parameters.
- **Wider temperature range:** A Journal of Solution Chemistry paper (Aalto/d-nb copies) tabulates relative apparent molar heat capacities of NaCl and uses Abraham and Marcus values for the infinite-dilution partial molar heat capacity. Another is Gates, Tillett, White and Wood, which covers 350–600 K and 0.05–3.0 mol·kg⁻¹. A Thermochimica Acta paper (1997, 294, 157–163) gives Pitzer parameters for NaCl–water between 25 and 100 °C. A separate paper (JACS, ja01476a013) gives standard partial molal heat capacities of NaCl from 0 to 100 °C.

I didn't find a ready-made table of Pitzer Cp parameters (β^J, C^J) at each temperature from 25 to 100 °C. The Thermochimica Acta paper and Archer's NaCl–water work (J. Phys. Chem. Ref. Data 21, 793, 1992) are the likeliest places to get them.


REMINDER: You MUST include the sources above in your response to the user using markdown hyperlinks.
WebFetch input
{
  "url": "https://www.processconvert.com/substances/sodium-chloride",
  "prompt": "Give the full table of specific heat (J/kg/K) of NaCl solution vs wt% (2..24) and temperature (0..80 °C), exactly as listed, and state the source/correlation used."
}
WebFetch input
{
  "url": "https://apps.dtic.mil/sti/tr/pdf/ADA045707.pdf",
  "prompt": "Find table of specific heats or apparent molal heat capacities of aqueous NaCl solutions vs molality at 25, 45, 65, 85 °C. Report numbers verbatim, units, and the infinite-dilution row."
}
WebFetch output
The server returned HTTP 403 Forbidden.

The response body was not retrieved. If this URL requires authentication, use an authenticated tool (e.g. `gh` for GitHub, or an MCP-provided fetch tool) instead of WebFetch.
WebFetch output
**Specific heat capacity of aqueous NaCl (J/kg·K), 2–24 wt% and 0–80 °C**

| °C \ wt% | 2 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | 18 | 20 | 22 | 24 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 4083 | 3969 | 3870 | 3783 | 3706 | 3635 | 3569 | 3509 | 3453 | 3402 | 3355 | 3312 |
| 5 | 4078 | 3970 | 3874 | 3789 | 3711 | 3640 | 3574 | 3512 | 3455 | 3402 | 3354 | 3311 |
| 10 | 4076 | 3972 | 3879 | 3795 | 3717 | 3645 | 3578 | 3516 | 3458 | 3404 | 3355 | 3311 |
| 15 | 4076 | 3975 | 3884 | 3800 | 3723 | 3651 | 3583 | 3520 | 3461 | 3406 | 3356 | 3311 |
| 20 | 4076 | 3978 | 3888 | 3805 | 3728 | 3656 | 3588 | 3524 | 3464 | 3409 | 3358 | 3313 |
| 25 | 4076 | 3981 | 3892 | 3810 | 3733 | 3660 | 3591 | 3527 | 3467 | 3411 | 3360 | 3314 |
| 30 | 4077 | 3983 | 3896 | 3814 | 3737 | 3664 | 3595 | 3530 | 3470 | 3413 | 3362 | 3315 |
| 35 | 4079 | 3986 | 3899 | 3818 | 3740 | 3667 | 3598 | 3533 | 3472 | 3415 | 3364 | 3317 |
| 40 | 4080 | 3989 | 3902 | 3821 | 3744 | 3671 | 3601 | 3536 | 3475 | 3418 | 3366 | 3319 |
| 45 | 4082 | 3991 | 3905 | 3824 | 3747 | 3673 | 3604 | 3538 | 3477 | 3419 | 3367 | 3320 |
| 50 | 4083 | 3993 | 3908 | 3827 | 3749 | 3676 | 3606 | 3540 | 3479 | 3422 | 3369 | 3322 |
| 55 | 4086 | 3996 | 3911 | 3830 | 3752 | 3679 | 3609 | 3543 | 3481 | 3424 | 3371 | 3323 |
| 60 | 4088 | 3999 | 3914 | 3832 | 3755 | 3681 | 3611 | 3545 | 3483 | 3426 | 3373 | 3325 |
| 65 | 4091 | 4001 | 3916 | 3835 | 3758 | 3684 | 3614 | 3548 | 3486 | 3428 | 3375 | 3328 |
| 70 | 4094 | 4004 | 3919 | 3838 | 3761 | 3687 | 3617 | 3551 | 3489 | 3431 | 3378 | 3330 |
| 75 | 4097 | 4008 | 3923 | 3841 | 3764 | 3690 | 3620 | 3553 | 3491 | 3433 | 3380 | 3332 |
| 80 | 4101 | 4011 | 3926 | 3844 | 3767 | 3693 | 3623 | 3556 | 3494 | 3436 | 3383 | 3335 |

**Source and method:** The values are computed by a deterministic model rather than entered by hand. The page cites the Laliberté (2009) correlation for heat capacity, density and viscosity of aqueous solutions, implemented in thermo 0.4.0 (chemicals 1.3.0). The model was checked against the CRC Handbook's density value for 20 wt% NaCl at 20 °C, with a 0.027% error. The page does not report a separate cross-check for specific heat.
Bash input
{
  "command": "cd /tmp; python3 -c \"\nw=[2,4,6,8,10,12,14,16,18,20,22,24]\nt={20:[4076,3978,3888,3805,3728,3656,3588,3524,3464,3409,3358,3313],25:[4076,3981,3892,3810,3733,3660,3591,3527,3467,3411,3360,3314],50:[4083,3993,3908,3827,3749,3676,3606,3540,3479,3422,3369,3322],80:[4101,4011,3926,3844,3767,3693,3623,3556,3494,3436,3383,3335]}\ncw={20:4182,25:4180,50:4181,80:4197}\nfor T in t:\n  print(T,[ (wi, round(1000/(1-wi/100)*t[T][i]/1000 - cw[T],1)) for i,wi in enumerate(w)])\n\""
}
Bash output
20 [(2, -22.8), (4, -38.2), (6, -45.8), (8, -46.1), (10, -39.8), (12, -27.5), (14, -9.9), (16, 13.2), (18, 42.4), (20, 79.2), (22, 123.1), (24, 177.2)]
25 [(2, -20.8), (4, -33.1), (6, -39.6), (8, -38.7), (10, -32.2), (12, -20.9), (14, -4.4), (16, 18.8), (18, 48.0), (20, 83.8), (22, 127.7), (24, 180.5)]
50 [(2, -14.7), (4, -21.6), (6, -23.6), (8, -21.2), (10, -15.4), (12, -3.7), (14, 12.0), (16, 33.3), (18, 61.7), (20, 96.5), (22, 138.2), (24, 190.1)]
80 [(2, -12.3), (4, -18.9), (6, -20.4), (8, -18.7), (10, -11.4), (12, -0.4), (14, 15.8), (16, 36.3), (18, 64.0), (20, 98.0), (22, 140.2), (24, 191.2)]
Shell cwd was reset to /work
Bash input
{
  "command": "cd /work; python3 - <<'E'\ns=open('Model.lean').read()\ns=s.replace(\"theorem answer_theorem\",\"theorem compare_theorem\",1)\ns=s.replace(\"/-- ANSWER 1 (sign-free).\",\"/-- Comparison lemma (sign-free; proved, used by the answer below).\")\ns+='''\n\n/-- Total heat capacity added by the salt, for 1 kg water plus salt of mass fraction w, solution specific\n heat c and pure-water specific heat cw (J/g/K): Cs = 1000/(1-w)*c - 1000*cw  (J/K). -/\nnoncomputable def saltCs (w c cw : ℝ) : ℝ := 1000/(1-w)*c - 1000*cw\n\n/-- SIGN OF Cs. (i) In general Cs<0 iff c/cw < 1-w (the solution's specific heat falls by more than the\n dilution by mass).  (ii) With the Laliberte-correlation values for NaCl(aq) (web-tabulated; computed, not a\n measurement; c in J/g/K, cw = 4.180 at 25 C, 4.197 at 80 C): Cs<0 for 2, 4, 10 wt% at 25 C and 80 C,\n and Cs>0 for 20 wt% at both. So Cs is negative in the cooking regime and changes sign near 13 wt%. -/\ntheorem cs_sign_theorem :\n    (∀ w c cw : ℝ, 0 < cw → w < 1 → (saltCs w c cw < 0 ↔ c/cw < 1 - w)) ∧\n    saltCs 0.02 4.076 4.180 < 0 ∧ saltCs 0.04 3.981 4.180 < 0 ∧ saltCs 0.10 3.733 4.180 < 0 ∧\n    saltCs 0.20 3.411 4.180 > 0 ∧\n    saltCs 0.02 4.101 4.197 < 0 ∧ saltCs 0.04 4.011 4.197 < 0 ∧ saltCs 0.10 3.767 4.197 < 0 ∧\n    saltCs 0.20 3.436 4.197 > 0 := by\n  refine ⟨?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩\n  · intro w c cw hcw hw\n    have h1 : 0 < 1 - w := by linarith\n    unfold saltCs\n    rw [div_lt_iff₀ hcw]\n    constructor <;> intro h\n    · have : 1000/(1-w)*c < 1000*cw := by linarith\n      rw [div_mul_eq_mul_div, div_lt_iff₀ h1] at this\n      nlinarith\n    · have : 1000*c < 1000*cw*(1-w) := by nlinarith\n      have h2 : 1000/(1-w)*c < 1000*cw := by\n        rw [div_mul_eq_mul_div, div_lt_iff₀ h1]; nlinarith\n      linarith\n  all_goals (unfold saltCs; norm_num)\n\n/-- ANSWER.  Same accepted model (lumped Cw, constant power P, Newton loss h, no evaporation, salt added at Tr,\n enthalpy conserved), Cs of either sign.  D = enthalpy of solution at Tr (literature: D>0, endothermic,\n about +3.9 kJ/mol).  If D >= 0 and the enthalpy of solution at the boiling point is still >= 0,\n i.e. D + Cs (Tb-Tr) >= 0 (Cs<0: cooling persists up to Tb; Cs>=0: automatic), then adding the salt at the\n start (Ta = T0) is no slower than adding it at any later water temperature Ta. -/\ntheorem answer_theorem : ∀ (Cw Cs D P h Tr T0 Tb : ℝ),\n    0 < Cw → 0 < Cw + Cs → 0 < h → h*(Tb-Tr) < P → Tr ≤ T0 →\n    (∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs))) →\n    0 ≤ D → 0 ≤ D + Cs*(Tb-Tr) → T0 ≤ Tb →\n    ∀ Ta : ℝ, T0 ≤ Ta → Ta ≤ Tb →\n      boilTime Cw Cs D P h Tr T0 Tb T0 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta := by\n  intro Cw Cs D P h Tr T0 Tb hCw hC hh hP hTr hs hD hDb hT0 Ta h1 h2\n  refine (compare_theorem Cw Cs D P h Tr T0 Tb hCw hC hh hP hTr hs T0 Ta le_rfl h1 h2).1 ?_\n  intro x hx1 hx2\n  rcases le_total 0 Cs with hc | hc\n  · nlinarith\n  · nlinarith\n'''\nopen('Model.lean','w').write(s)\nE\npython - <<'E'\nimport json\nc=json.load(open('/work/candidate.json'))\nc['conclusions']={\"answer\":\"answer_theorem\",\"cs_sign\":\"cs_sign_theorem\"}\nopen('/work/candidate.json','w').write(json.dumps(c,indent=1))\nE\npython /harness/src/vcs_protocol.py tool check --manifest /work/candidate.json 2>&1 | tail -30"
}
Bash output
        "extracted_sha256": "93f868964c26612e304e0166eab32768dbf511c83fb8c7c0cef922e9ac0164ce"
      },
      "saltCs._proof_1": {
        "original_sha256": "f952723ace8680854ad361cd5f02b3b25c9607d68dbd69a3860528a1ccd513a8",
        "extracted_sha256": "f952723ace8680854ad361cd5f02b3b25c9607d68dbd69a3860528a1ccd513a8"
      },
      "boilTime": {
        "original_sha256": "6198b94851e6843b80d39678b139232cd6effb3d2f1fe9a630bbbdbae772e6c0",
        "extracted_sha256": "6198b94851e6843b80d39678b139232cd6effb3d2f1fe9a630bbbdbae772e6c0"
      },
      "Fw": {
        "original_sha256": "38b9048abbc880c8b91005bb57e36daee61676680cf2ea28b79e449817077112",
        "extracted_sha256": "38b9048abbc880c8b91005bb57e36daee61676680cf2ea28b79e449817077112"
      },
      "Fs": {
        "original_sha256": "7428e34b8a70d77e92148994cb5f9813e0ae0c7e71b7041de8d055570276aaab",
        "extracted_sha256": "7428e34b8a70d77e92148994cb5f9813e0ae0c7e71b7041de8d055570276aaab"
      }
    },
    "unranged": [],
    "added_assumptions": [],
    "conclusions_as_proved": {
      "answer_theorem": "\u2200 (Cw Cs D P h Tr T0 Tb : \u211d), 0 < Cw \u2192 0 < Cw + Cs \u2192 0 < h \u2192 h * (Tb - Tr) < P \u2192 Tr \u2264 T0 \u2192 (\u2200 H \u2208 Set.Icc 0 (Cw * (Tb - Tr)), 0 < P - h * ((H - D) / (Cw + Cs))) \u2192 0 \u2264 D \u2192 0 \u2264 D + Cs * (Tb - Tr) \u2192 T0 \u2264 Tb \u2192 \u2200 (Ta : \u211d), T0 \u2264 Ta \u2192 Ta \u2264 Tb \u2192 boilTime Cw Cs D P h Tr T0 Tb T0 \u2264 boilTime Cw Cs D P h Tr T0 Tb Ta",
      "cs_sign_theorem": "(\u2200 (w c cw : \u211d), 0 < cw \u2192 w < 1 \u2192 (saltCs w c cw < 0 \u2194 c / cw < 1 - w)) \u2227 saltCs 2e-2 4.076 4.180 < 0 \u2227 saltCs 4e-2 3.981 4.180 < 0 \u2227 saltCs 0.10 3.733 4.180 < 0 \u2227 saltCs 0.20 3.411 4.180 > 0 \u2227 saltCs 2e-2 4.101 4.197 < 0 \u2227 saltCs 4e-2 4.011 4.197 < 0 \u2227 saltCs 0.10 3.767 4.197 < 0 \u2227 saltCs 0.20 3.436 4.197 > 0"
    },
    "extracted_file": "/work/checks/52448c0db62c400e90cbd46e54881cba/extracted.lean",
    "errors": []
  },
  "scientific_relevance": "requires human review"
}
assistant
Passed on first try. Now update the report and candidate text.
Bash input
{
  "command": "cd /work; cat > report.txt <<'E'\nREVISION (cycle 3). Reviewer: the Newtonian lumped heating model is accepted; establish the SIGN of Cs, do not presume it.\n\nSIGN OF Cs (literature/data, not a measurement). Cs = total heat capacity of (water+salt) minus that of the water, per kg water.\nSource: Laliberte (2009) correlation values for NaCl(aq) as tabulated at processconvert.com (computed values, not raw\nmeasurements; I could not open the primary source). Converting to Cs = 1000/(1-w)*c - 1000*cw (J/K per kg water):\n  25 C: 2 wt% -21, 4 wt% -33, 6 -40, 10 -32, 12 -21, 14 -4, 16 +19, 20 +84, 24 +181\n  80 C: 2 wt% -12, 4 -19, 10 -11, 12 -0.4, 14 +16, 20 +98.\nSo Cs is NEGATIVE for cooking-type amounts (up to ~13 wt%, ~2.5 mol/kg) and POSITIVE for strong brines (>~13 wt%); it\nchanges sign near 13 wt%, and is less negative at high T. Cross-check: apparent molal Cp of NaCl at infinite dilution\nnegative (about -88 J/K/mol at 25 C, -60 at 65 C; OCR'd DTIC table ADA045707, unverified; Abraham & Marcus -84 via search\nsnippet). Lean (cs_sign_theorem) proves Cs<0 iff c/cw<1-w and the signs at 2,4,10,20 wt% at 25 and 80 C given these\ntabulated c values (the c values are inputs from the correlation, not derived).\nMagnitude: for ~2 wt% (20 g/L), Cs ~ -20 J/K, i.e. ~0.5% of the ~4.2 kJ/K heat capacity of 1 L water (more with the pot).\n\nMODEL (unchanged, accepted): lumped pot+water heat capacity Cw, constant stove power P, Newton loss h(T-Tr), no\nevaporation, salt at Tr dissolves instantly with enthalpy conserved, solution temperature Tr+(H-D)/(Cw+Cs), D=enthalpy of\nsolution at Tr (literature +3.9 kJ/mol, endothermic; tutorial table, unverified), Cs constant, Tb fixed by salt amount,\nstove beats losses at all visited states. Facts supplied: 1 L, 20 C, stove; salt amount, pot, heater, room unspecified.\n\nRESULT (answer_theorem, no sorry): if D>=0 and D+Cs(Tb-Tr)>=0 (enthalpy of solution still >=0 at the boiling point;\nautomatic if Cs>=0), adding salt at the start (Ta=T0) is no slower than adding at any later water temperature Ta.\n(Derived from a sign-free comparison lemma: salting at x changes T by -(D+Cs(x-Tr))/(Cw+Cs); cooling over an interval\nfavours earlier salting, warming favours later.)\nImplication, with the numbers: for strong brines (Cs>0) salting early is best. For cooking amounts (Cs<0) the\ncondition is marginal with constant 25 C numbers: the switch temperature Ta*=Tr+D/|Cs| is ~85 C at 2 wt%, ~100+ C at 4 wt%.\nWith temperature-dependent Cs (less negative when hot) the enthalpy of solution stays about >=0 up to ~100 C (rough\nestimate, ~0 at 100 C), so early salting is consistent with the data but the sign in the last few degrees is not\nresolved. In any case the temperature jump is at most ~0.3 K (D/C ~ 1.4 kJ/4.2 kJ/K), so the time difference is a\nfraction of a percent-scale effect, far below stove/pot variability.\nNOT proved: that the condition holds for given amounts (needs D, Cs values as inputs; D and Cs are T-dependent in reality),\nquantitative time difference, magnitudes of data. No request or measurement was run; an explicit request for\nD(T) and Cp(T,m) from NIST/JPCRD (Archer 1992) would resolve the last-few-degrees sign.\nPractical advice: add salt at the start (also avoids boil-over); the timing effect is negligible.\nE\npython - <<'E'\nimport json\nc=json.load(open('/work/candidate.json'))\nc['sources']=[\"processconvert.com NaCl(aq) specific-heat table (Laliberte 2009 correlation, computed values)\",\"DTIC ADA045707 (OCR, unverified) apparent molal Cp NaCl\",\"Abraham & Marcus via search snippet; dissolution +3.88 kJ/mol tutorial table (unverified)\"]\nc['model_choices']=[\"accepted Newtonian lumped heating model\",\"Cs of either sign, constant; D enthalpy of solution at Tr\",\"salt at Tr, instant mixing, enthalpy conserved; Ta in [T0,Tb]; stove overcomes losses (premise)\"]\nc['proof_scope']=\"cs_sign_theorem: Cs<0 iff c/cw<1-w, and signs at tabulated NaCl data points (data are inputs). answer_theorem: given D>=0 and D+Cs(Tb-Tr)>=0, adding salt at the start is no slower than any later time. Whether the premise holds for a given salt amount rests on literature D and Cs values (marginal near boiling for cooking amounts); not proved. Human review needed.\"\nopen('/work/candidate.json','w').write(json.dumps(c,indent=1))\nE\npython /harness/src/vcs_protocol.py tool check --manifest /work/candidate.json 2>&1 | grep -E '\"(errors|verdict|status)\"' | head"
}
Bash output
 "errors": []
  "verdict": "proved",
    "errors": []
assistant
The revised candidate is in `/work/candidate.json` and the check tool returns "proved" with no errors. It has no `sorry` and no added axioms. The sign of Cs is now established from literature data rather than assumed. It is negative for cooking amounts and positive for strong brines. The data are not primary measurements.

**Sign of Cs** (the heat-capacity change when salt is added, in J/K per kg of water):
- **Source:** I computed Cs from the NaCl(aq) specific heats tabulated at processconvert.com, which come from the Laliberté (2009) correlation. These are computed values, not raw measurements, and I could not open the primary source.
- **Cooking amounts (negative):** at 25 °C, 2 wt% gives about −21 J/K, 4 wt% about −33 and 10 wt% about −32. At 80 °C the values are −12, −19 and −11.
- **Strong brines (positive):** 20 wt% gives about +84 J/K at 25 °C and +98 at 80 °C.
- **Sign change:** Cs changes sign near 13 wt%, and it is less negative when the water is hot.
- **Cross-check:** the infinite-dilution values are also negative, about −88 J/K/mol at 25 °C. That comes from an OCR'd DTIC table (ADA045707) and an Abraham & Marcus search snippet, both unverified.
- **Size of the effect:** for a 2 wt% solution (about 20 g of salt in 1 L), Cs is about −20 J/K. That is roughly 0.5% of the water's heat capacity.

**Lean theorems** (the accepted Newtonian lumped heating model is unchanged):
- **`cs_sign_theorem`:** proves Cs < 0 exactly when c/cw < 1 − w, where c and cw are the specific heats of the solution and of pure water and w is the salt mass fraction. It also proves the sign at 2, 4, 10 and 20 wt% at 25 °C and 80 °C. The tabulated c values are inputs, not derived.
- **`answer_theorem`:** if the enthalpy of solution is non-negative at the start and still non-negative at the boiling point, adding salt at the start is no slower than adding it at any later water temperature. That condition holds automatically when Cs is positive. The earlier sign-free comparison theorem is now a lemma that `answer_theorem` builds on.

**Implication for timing:** add the salt at the start. This is the best choice for strong brines and for cooking amounts at the numbers I have. For cooking amounts the condition is marginal near the boiling point:
- With Cs held at its 25 °C value, the switch temperature falls near 85 °C for a 2 wt% solution and above 100 °C for 4 wt%.
- With a temperature-dependent Cs, the enthalpy of solution stays about non-negative up to roughly 100 °C. This is a rough estimate, and I did not resolve the sign in the last few degrees.
- Either way, the temperature jump from adding salt is at most about 0.3 K, so the effect on boiling time is negligible. Adding salt early also avoids boil-over.

**Not proved:**
- Whether the condition holds for a given salt amount. The enthalpy of solution (about +3.9 kJ/mol, from an unverified tutorial table) and the constant-Cs values are literature inputs, and in reality both depend on temperature.
- Any quantitative difference in boiling time.

I made no request to you this cycle and ran no measurement. If you want the last-few-degrees sign settled, the temperature-dependent enthalpy of solution and Cp(T, m) from the NIST/JPCRD NaCl review (Archer 1992) would do it. I have not self-approved this result.

Produced code

attempt-003/Model.lean
import Mathlib.Analysis.SpecialFunctions.Log.Deriv
import Mathlib.Analysis.Calculus.MeanValue

/-! Model (see report.txt).  H = enthalpy (J) measured from "everything at room temperature Tr".
 Pure water, heat capacity Cw: temperature Tr + H/Cw.
 Salt solution, total heat capacity Cw+Cs>0 (Cs of EITHER sign; literature: Cs<0), dissolution enthalpy D (at Tr) of either sign:
 temperature Tr + (H-D)/(Cw+Cs).  Salt (at Tr) is mixed in instantly, so H is conserved.
 Heater power P constant, loss h*(T-Tr):  dH/dt = P - h*(T-Tr).
 Separating variables, the time to move from H1 to H2 is Fw/Fs(H2) - Fw/Fs(H1) where
 Fw' = 1/(P - h*(H/Cw)), Fs' = 1/(P - h*((H-D)/(Cw+Cs))) (explicit logarithms below).
 Tb = boiling point of the final solution (fixed by salt amount, not by timing). -/

/-- time-integral of dH/(P - loss) for pure water (antiderivative, h>0) -/
noncomputable def Fw (Cw P h H : ℝ) : ℝ := -(Cw/h) * Real.log (P - h*(H/Cw))
/-- same for the salt solution -/
noncomputable def Fs (Cw Cs D P h H : ℝ) : ℝ := -((Cw+Cs)/h) * Real.log (P - h*((H-D)/(Cw+Cs)))

/-- Total time to reach Tb from T0 when the salt is added once the water is at Ta:
 heating water alone from T0 to Ta, then solution from the post-mixing state up to Tb. -/
noncomputable def boilTime (Cw Cs D P h Tr T0 Tb Ta : ℝ) : ℝ :=
  (Fw Cw P h (Cw*(Ta-Tr)) - Fw Cw P h (Cw*(T0-Tr))) +
  (Fs Cw Cs D P h ((Cw+Cs)*(Tb-Tr)+D) - Fs Cw Cs D P h (Cw*(Ta-Tr)))

lemma G_deriv (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs))) :
    ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), HasDerivAt (fun H => Fw Cw P h H - Fs Cw Cs D P h H)
      (1/(P - h*(H/Cw)) - 1/(P - h*((H-D)/(Cw+Cs)))) H := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  intro H hH
  have a1 : HasDerivAt (fun H : ℝ => P - h*(H/Cw)) (-(h/Cw)) H := by
    have := ((hasDerivAt_id H).div_const Cw).const_mul h
    simpa [div_eq_mul_inv, mul_comm] using (this.const_sub P)
  have a2 : HasDerivAt (fun H : ℝ => P - h*((H-D)/(Cw+Cs))) (-(h/(Cw+Cs))) H := by
    have := (((hasDerivAt_id H).sub_const D).div_const (Cw+Cs)).const_mul h
    simpa [div_eq_mul_inv, mul_comm] using (this.const_sub P)
  have l1 := (a1.log (hw H hH).ne').const_mul (-(Cw/h))
  have l2 := (a2.log (hs H hH).ne').const_mul (-((Cw+Cs)/h))
  have := l1.sub l2
  convert this using 1
  · funext x; rfl
  · have e1 := (hw H hH).ne'
    have e2 := (hs H hH).ne'
    have e3 := hh.ne'
    have e4 := hCw.ne'
    have e5 := hC.ne'
    field_simp

/-- Salt added at water temperature x changes the temperature at once by
 (D + Cs (x-Tr))/(Cw+Cs) downward (a cooling if D + Cs (x-Tr) > 0).  -/
lemma mix_iff (Cw Cs D Tr x : ℝ) (hCw : 0 < Cw) (hC : 0 < Cw + Cs) :
    (Cw*(x-Tr) - D)/(Cw+Cs) ≤ Cw*(x-Tr)/Cw ↔ 0 ≤ D + Cs*(x-Tr) := by
  rw [mul_div_cancel_left₀ _ hCw.ne', div_le_iff₀ hC]
  constructor <;> intro h <;> nlinarith

lemma G_mono (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs)))
    (S : Set ℝ) (hS : S ⊆ Set.Icc 0 (Cw*(Tb-Tr))) (hconv : Convex ℝ S)
    (sgn : ∀ H ∈ S, (H-D)/(Cw+Cs) ≤ H/Cw) :
    MonotoneOn (fun H => Fw Cw P h H - Fs Cw Cs D P h H) S := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  have hd := G_deriv Cw Cs D P h Tr Tb hCw hC hh hP hs
  apply monotoneOn_of_deriv_nonneg hconv
  · intro H hH; exact (hd H (hS hH)).continuousAt.continuousWithinAt
  · intro H hH; exact (hd H (hS (interior_subset hH))).differentiableAt.differentiableWithinAt
  · intro H hH
    have hH' := interior_subset hH
    rw [(hd H (hS hH')).deriv]
    apply sub_nonneg.mpr
    apply one_div_le_one_div_of_le (hw H (hS hH'))
    have := sgn H hH'
    nlinarith

lemma G_anti (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs)))
    (S : Set ℝ) (hS : S ⊆ Set.Icc 0 (Cw*(Tb-Tr))) (hconv : Convex ℝ S)
    (sgn : ∀ H ∈ S, H/Cw ≤ (H-D)/(Cw+Cs)) :
    AntitoneOn (fun H => Fw Cw P h H - Fs Cw Cs D P h H) S := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  have hd := G_deriv Cw Cs D P h Tr Tb hCw hC hh hP hs
  apply antitoneOn_of_deriv_nonpos hconv
  · intro H hH; exact (hd H (hS hH)).continuousAt.continuousWithinAt
  · intro H hH; exact (hd H (hS (interior_subset hH))).differentiableAt.differentiableWithinAt
  · intro H hH
    have hH' := interior_subset hH
    rw [(hd H (hS hH')).deriv]
    apply sub_nonpos.mpr
    apply one_div_le_one_div_of_le (hs H (hS hH'))
    have := sgn H hH'
    nlinarith

/-- Comparison lemma (sign-free; proved, used by the answer below).  Salt of fixed amount (Cs, D arbitrary sign, C=Cw+Cs>0).
 If between water temperatures Ta1 ≤ Ta2 salt addition always COOLS (D+Cs(x-Tr) ≥ 0 for x in [Ta1,Ta2]),
 adding at Ta1 is no slower than at Ta2; if it always WARMS (≤ 0), adding at Ta2 is no slower. -/
theorem compare_theorem : ∀ (Cw Cs D P h Tr T0 Tb : ℝ),
    0 < Cw → 0 < Cw + Cs → 0 < h → h*(Tb-Tr) < P → Tr ≤ T0 →
    (∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs))) →
    ∀ Ta1 Ta2 : ℝ, T0 ≤ Ta1 → Ta1 ≤ Ta2 → Ta2 ≤ Tb →
      ((∀ x, Ta1 ≤ x → x ≤ Ta2 → 0 ≤ D + Cs*(x-Tr)) →
          boilTime Cw Cs D P h Tr T0 Tb Ta1 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta2) ∧
      ((∀ x, Ta1 ≤ x → x ≤ Ta2 → D + Cs*(x-Tr) ≤ 0) →
          boilTime Cw Cs D P h Tr T0 Tb Ta2 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta1) := by
  intro Cw Cs D P h Tr T0 Tb hCw hC hh hP hTr hs Ta1 Ta2 h1 h12 h2
  have hmem : ∀ x, Ta1 ≤ x → x ≤ Ta2 → Cw*(x-Tr) ∈ Set.Icc 0 (Cw*(Tb-Tr)) := fun x a b =>
    ⟨by nlinarith, by nlinarith⟩
  have hS : Set.Icc (Cw*(Ta1-Tr)) (Cw*(Ta2-Tr)) ⊆ Set.Icc 0 (Cw*(Tb-Tr)) := by
    intro H hH
    exact ⟨by nlinarith [hH.1], by nlinarith [hH.2]⟩
  have hHx : ∀ H ∈ Set.Icc (Cw*(Ta1-Tr)) (Cw*(Ta2-Tr)), ∃ x, Ta1 ≤ x ∧ x ≤ Ta2 ∧ H = Cw*(x-Tr) := by
    intro H hH
    refine ⟨H/Cw + Tr, ?_, ?_, ?_⟩
    · have : Ta1 - Tr ≤ H/Cw := by rw [le_div_iff₀ hCw]; nlinarith [hH.1]
      linarith
    · have : H/Cw ≤ Ta2 - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
      linarith
    · field_simp; ring
  constructor
  · intro hx
    have m := G_mono Cw Cs D P h Tr Tb hCw hC hh hP hs _ hS (convex_Icc _ _) (by
      intro H hH
      obtain ⟨x, a, b, rfl⟩ := hHx H hH
      exact (mix_iff Cw Cs D Tr x hCw hC).mpr (hx x a b))
      ⟨le_rfl, by nlinarith⟩ ⟨by nlinarith, le_rfl⟩ (by nlinarith)
    simp only [boilTime]; simp only at m; linarith
  · intro hx
    have m := G_anti Cw Cs D P h Tr Tb hCw hC hh hP hs _ hS (convex_Icc _ _) (by
      intro H hH
      obtain ⟨x, a, b, rfl⟩ := hHx H hH
      have := hx x a b
      have h3 : ¬ ((Cw*(x-Tr) - D)/(Cw+Cs) < Cw*(x-Tr)/Cw) → True := fun _ => trivial
      rw [mul_div_cancel_left₀ _ hCw.ne', le_div_iff₀ hC]
      nlinarith)
      ⟨le_rfl, by nlinarith⟩ ⟨by nlinarith, le_rfl⟩ (by nlinarith)
    simp only [boilTime]; simp only at m; linarith


/-- Total heat capacity added by the salt, for 1 kg water plus salt of mass fraction w, solution specific
 heat c and pure-water specific heat cw (J/g/K): Cs = 1000/(1-w)*c - 1000*cw  (J/K). -/
noncomputable def saltCs (w c cw : ℝ) : ℝ := 1000/(1-w)*c - 1000*cw

/-- SIGN OF Cs. (i) In general Cs<0 iff c/cw < 1-w (the solution's specific heat falls by more than the
 dilution by mass).  (ii) With the Laliberte-correlation values for NaCl(aq) (web-tabulated; computed, not a
 measurement; c in J/g/K, cw = 4.180 at 25 C, 4.197 at 80 C): Cs<0 for 2, 4, 10 wt% at 25 C and 80 C,
 and Cs>0 for 20 wt% at both. So Cs is negative in the cooking regime and changes sign near 13 wt%. -/
theorem cs_sign_theorem :
    (∀ w c cw : ℝ, 0 < cw → w < 1 → (saltCs w c cw < 0 ↔ c/cw < 1 - w)) ∧
    saltCs 0.02 4.076 4.180 < 0 ∧ saltCs 0.04 3.981 4.180 < 0 ∧ saltCs 0.10 3.733 4.180 < 0 ∧
    saltCs 0.20 3.411 4.180 > 0 ∧
    saltCs 0.02 4.101 4.197 < 0 ∧ saltCs 0.04 4.011 4.197 < 0 ∧ saltCs 0.10 3.767 4.197 < 0 ∧
    saltCs 0.20 3.436 4.197 > 0 := by
  refine ⟨?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩
  · intro w c cw hcw hw
    have h1 : 0 < 1 - w := by linarith
    unfold saltCs
    rw [div_lt_iff₀ hcw]
    constructor <;> intro h
    · have : 1000/(1-w)*c < 1000*cw := by linarith
      rw [div_mul_eq_mul_div, div_lt_iff₀ h1] at this
      nlinarith
    · have : 1000*c < 1000*cw*(1-w) := by nlinarith
      have h2 : 1000/(1-w)*c < 1000*cw := by
        rw [div_mul_eq_mul_div, div_lt_iff₀ h1]; nlinarith
      linarith
  all_goals (unfold saltCs; norm_num)

/-- ANSWER.  Same accepted model (lumped Cw, constant power P, Newton loss h, no evaporation, salt added at Tr,
 enthalpy conserved), Cs of either sign.  D = enthalpy of solution at Tr (literature: D>0, endothermic,
 about +3.9 kJ/mol).  If D >= 0 and the enthalpy of solution at the boiling point is still >= 0,
 i.e. D + Cs (Tb-Tr) >= 0 (Cs<0: cooling persists up to Tb; Cs>=0: automatic), then adding the salt at the
 start (Ta = T0) is no slower than adding it at any later water temperature Ta. -/
theorem answer_theorem : ∀ (Cw Cs D P h Tr T0 Tb : ℝ),
    0 < Cw → 0 < Cw + Cs → 0 < h → h*(Tb-Tr) < P → Tr ≤ T0 →
    (∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs))) →
    0 ≤ D → 0 ≤ D + Cs*(Tb-Tr) → T0 ≤ Tb →
    ∀ Ta : ℝ, T0 ≤ Ta → Ta ≤ Tb →
      boilTime Cw Cs D P h Tr T0 Tb T0 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta := by
  intro Cw Cs D P h Tr T0 Tb hCw hC hh hP hTr hs hD hDb hT0 Ta h1 h2
  refine (compare_theorem Cw Cs D P h Tr T0 Tb hCw hC hh hP hTr hs T0 Ta le_rfl h1 h2).1 ?_
  intro x hx1 hx2
  rcases le_total 0 Cs with hc | hc
  · nlinarith
  · nlinarith
attempt-003/extracted.lean
import Mathlib.Analysis.SpecialFunctions.Log.Deriv
import Mathlib.Analysis.Calculus.MeanValue

/-- time-integral of dH/(P - loss) for pure water (antiderivative, h>0) -/
noncomputable def Fw (Cw P h H : ℝ) : ℝ := -(Cw/h) * Real.log (P - h*(H/Cw))

/-- same for the salt solution -/
noncomputable def Fs (Cw Cs D P h H : ℝ) : ℝ := -((Cw+Cs)/h) * Real.log (P - h*((H-D)/(Cw+Cs)))

/-- Total time to reach Tb from T0 when the salt is added once the water is at Ta:
 heating water alone from T0 to Ta, then solution from the post-mixing state up to Tb. -/
noncomputable def boilTime (Cw Cs D P h Tr T0 Tb Ta : ℝ) : ℝ :=
  (Fw Cw P h (Cw*(Ta-Tr)) - Fw Cw P h (Cw*(T0-Tr))) +
  (Fs Cw Cs D P h ((Cw+Cs)*(Tb-Tr)+D) - Fs Cw Cs D P h (Cw*(Ta-Tr)))

/-- Total heat capacity added by the salt, for 1 kg water plus salt of mass fraction w, solution specific
 heat c and pure-water specific heat cw (J/g/K): Cs = 1000/(1-w)*c - 1000*cw  (J/K). -/
noncomputable def saltCs (w c cw : ℝ) : ℝ := 1000/(1-w)*c - 1000*cw


/-! Conclusions (statements of the proved theorems). -/
def conclusion_answer_theorem : Prop := ∀ (Cw Cs D P h Tr T0 Tb : ℝ), 0 < Cw → 0 < Cw + Cs → 0 < h → h*(Tb-Tr) < P → Tr ≤ T0 → (∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs))) → 0 ≤ D → 0 ≤ D + Cs*(Tb-Tr) → T0 ≤ Tb → ∀ Ta : ℝ, T0 ≤ Ta → Ta ≤ Tb → boilTime Cw Cs D P h Tr T0 Tb T0 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta
def conclusion_cs_sign_theorem : Prop := (∀ w c cw : ℝ, 0 < cw → w < 1 → (saltCs w c cw < 0 ↔ c/cw < 1 - w)) ∧ saltCs 0.02 4.076 4.180 < 0 ∧ saltCs 0.04 3.981 4.180 < 0 ∧ saltCs 0.10 3.733 4.180 < 0 ∧ saltCs 0.20 3.411 4.180 > 0 ∧ saltCs 0.02 4.101 4.197 < 0 ∧ saltCs 0.04 4.011 4.197 < 0 ∧ saltCs 0.10 3.767 4.197 < 0 ∧ saltCs 0.20 3.436 4.197 > 0
checks/52448c0db62c400e90cbd46e54881cba/submitted.lean
import Mathlib.Analysis.SpecialFunctions.Log.Deriv
import Mathlib.Analysis.Calculus.MeanValue

/-! Model (see report.txt).  H = enthalpy (J) measured from "everything at room temperature Tr".
 Pure water, heat capacity Cw: temperature Tr + H/Cw.
 Salt solution, total heat capacity Cw+Cs>0 (Cs of EITHER sign; literature: Cs<0), dissolution enthalpy D (at Tr) of either sign:
 temperature Tr + (H-D)/(Cw+Cs).  Salt (at Tr) is mixed in instantly, so H is conserved.
 Heater power P constant, loss h*(T-Tr):  dH/dt = P - h*(T-Tr).
 Separating variables, the time to move from H1 to H2 is Fw/Fs(H2) - Fw/Fs(H1) where
 Fw' = 1/(P - h*(H/Cw)), Fs' = 1/(P - h*((H-D)/(Cw+Cs))) (explicit logarithms below).
 Tb = boiling point of the final solution (fixed by salt amount, not by timing). -/

/-- time-integral of dH/(P - loss) for pure water (antiderivative, h>0) -/
noncomputable def Fw (Cw P h H : ℝ) : ℝ := -(Cw/h) * Real.log (P - h*(H/Cw))
/-- same for the salt solution -/
noncomputable def Fs (Cw Cs D P h H : ℝ) : ℝ := -((Cw+Cs)/h) * Real.log (P - h*((H-D)/(Cw+Cs)))

/-- Total time to reach Tb from T0 when the salt is added once the water is at Ta:
 heating water alone from T0 to Ta, then solution from the post-mixing state up to Tb. -/
noncomputable def boilTime (Cw Cs D P h Tr T0 Tb Ta : ℝ) : ℝ :=
  (Fw Cw P h (Cw*(Ta-Tr)) - Fw Cw P h (Cw*(T0-Tr))) +
  (Fs Cw Cs D P h ((Cw+Cs)*(Tb-Tr)+D) - Fs Cw Cs D P h (Cw*(Ta-Tr)))

lemma G_deriv (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs))) :
    ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), HasDerivAt (fun H => Fw Cw P h H - Fs Cw Cs D P h H)
      (1/(P - h*(H/Cw)) - 1/(P - h*((H-D)/(Cw+Cs)))) H := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  intro H hH
  have a1 : HasDerivAt (fun H : ℝ => P - h*(H/Cw)) (-(h/Cw)) H := by
    have := ((hasDerivAt_id H).div_const Cw).const_mul h
    simpa [div_eq_mul_inv, mul_comm] using (this.const_sub P)
  have a2 : HasDerivAt (fun H : ℝ => P - h*((H-D)/(Cw+Cs))) (-(h/(Cw+Cs))) H := by
    have := (((hasDerivAt_id H).sub_const D).div_const (Cw+Cs)).const_mul h
    simpa [div_eq_mul_inv, mul_comm] using (this.const_sub P)
  have l1 := (a1.log (hw H hH).ne').const_mul (-(Cw/h))
  have l2 := (a2.log (hs H hH).ne').const_mul (-((Cw+Cs)/h))
  have := l1.sub l2
  convert this using 1
  · funext x; rfl
  · have e1 := (hw H hH).ne'
    have e2 := (hs H hH).ne'
    have e3 := hh.ne'
    have e4 := hCw.ne'
    have e5 := hC.ne'
    field_simp

/-- Salt added at water temperature x changes the temperature at once by
 (D + Cs (x-Tr))/(Cw+Cs) downward (a cooling if D + Cs (x-Tr) > 0).  -/
lemma mix_iff (Cw Cs D Tr x : ℝ) (hCw : 0 < Cw) (hC : 0 < Cw + Cs) :
    (Cw*(x-Tr) - D)/(Cw+Cs) ≤ Cw*(x-Tr)/Cw ↔ 0 ≤ D + Cs*(x-Tr) := by
  rw [mul_div_cancel_left₀ _ hCw.ne', div_le_iff₀ hC]
  constructor <;> intro h <;> nlinarith

lemma G_mono (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs)))
    (S : Set ℝ) (hS : S ⊆ Set.Icc 0 (Cw*(Tb-Tr))) (hconv : Convex ℝ S)
    (sgn : ∀ H ∈ S, (H-D)/(Cw+Cs) ≤ H/Cw) :
    MonotoneOn (fun H => Fw Cw P h H - Fs Cw Cs D P h H) S := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  have hd := G_deriv Cw Cs D P h Tr Tb hCw hC hh hP hs
  apply monotoneOn_of_deriv_nonneg hconv
  · intro H hH; exact (hd H (hS hH)).continuousAt.continuousWithinAt
  · intro H hH; exact (hd H (hS (interior_subset hH))).differentiableAt.differentiableWithinAt
  · intro H hH
    have hH' := interior_subset hH
    rw [(hd H (hS hH')).deriv]
    apply sub_nonneg.mpr
    apply one_div_le_one_div_of_le (hw H (hS hH'))
    have := sgn H hH'
    nlinarith

lemma G_anti (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs)))
    (S : Set ℝ) (hS : S ⊆ Set.Icc 0 (Cw*(Tb-Tr))) (hconv : Convex ℝ S)
    (sgn : ∀ H ∈ S, H/Cw ≤ (H-D)/(Cw+Cs)) :
    AntitoneOn (fun H => Fw Cw P h H - Fs Cw Cs D P h H) S := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  have hd := G_deriv Cw Cs D P h Tr Tb hCw hC hh hP hs
  apply antitoneOn_of_deriv_nonpos hconv
  · intro H hH; exact (hd H (hS hH)).continuousAt.continuousWithinAt
  · intro H hH; exact (hd H (hS (interior_subset hH))).differentiableAt.differentiableWithinAt
  · intro H hH
    have hH' := interior_subset hH
    rw [(hd H (hS hH')).deriv]
    apply sub_nonpos.mpr
    apply one_div_le_one_div_of_le (hs H (hS hH'))
    have := sgn H hH'
    nlinarith

/-- Comparison lemma (sign-free; proved, used by the answer below).  Salt of fixed amount (Cs, D arbitrary sign, C=Cw+Cs>0).
 If between water temperatures Ta1 ≤ Ta2 salt addition always COOLS (D+Cs(x-Tr) ≥ 0 for x in [Ta1,Ta2]),
 adding at Ta1 is no slower than at Ta2; if it always WARMS (≤ 0), adding at Ta2 is no slower. -/
theorem compare_theorem : ∀ (Cw Cs D P h Tr T0 Tb : ℝ),
    0 < Cw → 0 < Cw + Cs → 0 < h → h*(Tb-Tr) < P → Tr ≤ T0 →
    (∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs))) →
    ∀ Ta1 Ta2 : ℝ, T0 ≤ Ta1 → Ta1 ≤ Ta2 → Ta2 ≤ Tb →
      ((∀ x, Ta1 ≤ x → x ≤ Ta2 → 0 ≤ D + Cs*(x-Tr)) →
          boilTime Cw Cs D P h Tr T0 Tb Ta1 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta2) ∧
      ((∀ x, Ta1 ≤ x → x ≤ Ta2 → D + Cs*(x-Tr) ≤ 0) →
          boilTime Cw Cs D P h Tr T0 Tb Ta2 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta1) := by
  intro Cw Cs D P h Tr T0 Tb hCw hC hh hP hTr hs Ta1 Ta2 h1 h12 h2
  have hmem : ∀ x, Ta1 ≤ x → x ≤ Ta2 → Cw*(x-Tr) ∈ Set.Icc 0 (Cw*(Tb-Tr)) := fun x a b =>
    ⟨by nlinarith, by nlinarith⟩
  have hS : Set.Icc (Cw*(Ta1-Tr)) (Cw*(Ta2-Tr)) ⊆ Set.Icc 0 (Cw*(Tb-Tr)) := by
    intro H hH
    exact ⟨by nlinarith [hH.1], by nlinarith [hH.2]⟩
  have hHx : ∀ H ∈ Set.Icc (Cw*(Ta1-Tr)) (Cw*(Ta2-Tr)), ∃ x, Ta1 ≤ x ∧ x ≤ Ta2 ∧ H = Cw*(x-Tr) := by
    intro H hH
    refine ⟨H/Cw + Tr, ?_, ?_, ?_⟩
    · have : Ta1 - Tr ≤ H/Cw := by rw [le_div_iff₀ hCw]; nlinarith [hH.1]
      linarith
    · have : H/Cw ≤ Ta2 - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
      linarith
    · field_simp; ring
  constructor
  · intro hx
    have m := G_mono Cw Cs D P h Tr Tb hCw hC hh hP hs _ hS (convex_Icc _ _) (by
      intro H hH
      obtain ⟨x, a, b, rfl⟩ := hHx H hH
      exact (mix_iff Cw Cs D Tr x hCw hC).mpr (hx x a b))
      ⟨le_rfl, by nlinarith⟩ ⟨by nlinarith, le_rfl⟩ (by nlinarith)
    simp only [boilTime]; simp only at m; linarith
  · intro hx
    have m := G_anti Cw Cs D P h Tr Tb hCw hC hh hP hs _ hS (convex_Icc _ _) (by
      intro H hH
      obtain ⟨x, a, b, rfl⟩ := hHx H hH
      have := hx x a b
      have h3 : ¬ ((Cw*(x-Tr) - D)/(Cw+Cs) < Cw*(x-Tr)/Cw) → True := fun _ => trivial
      rw [mul_div_cancel_left₀ _ hCw.ne', le_div_iff₀ hC]
      nlinarith)
      ⟨le_rfl, by nlinarith⟩ ⟨by nlinarith, le_rfl⟩ (by nlinarith)
    simp only [boilTime]; simp only at m; linarith


/-- Total heat capacity added by the salt, for 1 kg water plus salt of mass fraction w, solution specific
 heat c and pure-water specific heat cw (J/g/K): Cs = 1000/(1-w)*c - 1000*cw  (J/K). -/
noncomputable def saltCs (w c cw : ℝ) : ℝ := 1000/(1-w)*c - 1000*cw

/-- SIGN OF Cs. (i) In general Cs<0 iff c/cw < 1-w (the solution's specific heat falls by more than the
 dilution by mass).  (ii) With the Laliberte-correlation values for NaCl(aq) (web-tabulated; computed, not a
 measurement; c in J/g/K, cw = 4.180 at 25 C, 4.197 at 80 C): Cs<0 for 2, 4, 10 wt% at 25 C and 80 C,
 and Cs>0 for 20 wt% at both. So Cs is negative in the cooking regime and changes sign near 13 wt%. -/
theorem cs_sign_theorem :
    (∀ w c cw : ℝ, 0 < cw → w < 1 → (saltCs w c cw < 0 ↔ c/cw < 1 - w)) ∧
    saltCs 0.02 4.076 4.180 < 0 ∧ saltCs 0.04 3.981 4.180 < 0 ∧ saltCs 0.10 3.733 4.180 < 0 ∧
    saltCs 0.20 3.411 4.180 > 0 ∧
    saltCs 0.02 4.101 4.197 < 0 ∧ saltCs 0.04 4.011 4.197 < 0 ∧ saltCs 0.10 3.767 4.197 < 0 ∧
    saltCs 0.20 3.436 4.197 > 0 := by
  refine ⟨?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩
  · intro w c cw hcw hw
    have h1 : 0 < 1 - w := by linarith
    unfold saltCs
    rw [div_lt_iff₀ hcw]
    constructor <;> intro h
    · have : 1000/(1-w)*c < 1000*cw := by linarith
      rw [div_mul_eq_mul_div, div_lt_iff₀ h1] at this
      nlinarith
    · have : 1000*c < 1000*cw*(1-w) := by nlinarith
      have h2 : 1000/(1-w)*c < 1000*cw := by
        rw [div_mul_eq_mul_div, div_lt_iff₀ h1]; nlinarith
      linarith
  all_goals (unfold saltCs; norm_num)

/-- ANSWER.  Same accepted model (lumped Cw, constant power P, Newton loss h, no evaporation, salt added at Tr,
 enthalpy conserved), Cs of either sign.  D = enthalpy of solution at Tr (literature: D>0, endothermic,
 about +3.9 kJ/mol).  If D >= 0 and the enthalpy of solution at the boiling point is still >= 0,
 i.e. D + Cs (Tb-Tr) >= 0 (Cs<0: cooling persists up to Tb; Cs>=0: automatic), then adding the salt at the
 start (Ta = T0) is no slower than adding it at any later water temperature Ta. -/
theorem answer_theorem : ∀ (Cw Cs D P h Tr T0 Tb : ℝ),
    0 < Cw → 0 < Cw + Cs → 0 < h → h*(Tb-Tr) < P → Tr ≤ T0 →
    (∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs))) →
    0 ≤ D → 0 ≤ D + Cs*(Tb-Tr) → T0 ≤ Tb →
    ∀ Ta : ℝ, T0 ≤ Ta → Ta ≤ Tb →
      boilTime Cw Cs D P h Tr T0 Tb T0 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta := by
  intro Cw Cs D P h Tr T0 Tb hCw hC hh hP hTr hs hD hDb hT0 Ta h1 h2
  refine (compare_theorem Cw Cs D P h Tr T0 Tb hCw hC hh hP hTr hs T0 Ta le_rfl h1 h2).1 ?_
  intro x hx1 hx2
  rcases le_total 0 Cs with hc | hc
  · nlinarith
  · nlinarith

Round 4

Messages

Bash input
{
  "command": "cd /work; ls -la; cat report.txt | head -80; cat candidate.json; ls *feedback* 2>/dev/null"
}
Bash output
total 44
drwxrwxr-x  3 1001 1001  4096 Nov 17 16:02 .
drwxr-xr-x  1 root root  4096 Nov 17 16:02 ..
-rw-r--r--  1 1001 1001 10323 Nov 17 14:39 Model.lean
-rw-r--r--  1 1001 1001  1269 Nov 17 14:40 candidate.json
drwxr-xr-x 10 1001 1001  4096 Nov 17 14:40 checks
-rw-------  1 1001 1001  1373 Nov 17 16:02 human-feedback.json
-rw-------  1 1001 1001  7033 Nov 17 14:41 last-review-packet.json
-rw-r--r--  1 1001 1001  3242 Nov 17 14:39 report.txt
REVISION (cycle 3). Reviewer: the Newtonian lumped heating model is accepted; establish the SIGN of Cs, do not presume it.

SIGN OF Cs (literature/data, not a measurement). Cs = total heat capacity of (water+salt) minus that of the water, per kg water.
Source: Laliberte (2009) correlation values for NaCl(aq) as tabulated at processconvert.com (computed values, not raw
measurements; I could not open the primary source). Converting to Cs = 1000/(1-w)*c - 1000*cw (J/K per kg water):
  25 C: 2 wt% -21, 4 wt% -33, 6 -40, 10 -32, 12 -21, 14 -4, 16 +19, 20 +84, 24 +181
  80 C: 2 wt% -12, 4 -19, 10 -11, 12 -0.4, 14 +16, 20 +98.
So Cs is NEGATIVE for cooking-type amounts (up to ~13 wt%, ~2.5 mol/kg) and POSITIVE for strong brines (>~13 wt%); it
changes sign near 13 wt%, and is less negative at high T. Cross-check: apparent molal Cp of NaCl at infinite dilution
negative (about -88 J/K/mol at 25 C, -60 at 65 C; OCR'd DTIC table ADA045707, unverified; Abraham & Marcus -84 via search
snippet). Lean (cs_sign_theorem) proves Cs<0 iff c/cw<1-w and the signs at 2,4,10,20 wt% at 25 and 80 C given these
tabulated c values (the c values are inputs from the correlation, not derived).
Magnitude: for ~2 wt% (20 g/L), Cs ~ -20 J/K, i.e. ~0.5% of the ~4.2 kJ/K heat capacity of 1 L water (more with the pot).

MODEL (unchanged, accepted): lumped pot+water heat capacity Cw, constant stove power P, Newton loss h(T-Tr), no
evaporation, salt at Tr dissolves instantly with enthalpy conserved, solution temperature Tr+(H-D)/(Cw+Cs), D=enthalpy of
solution at Tr (literature +3.9 kJ/mol, endothermic; tutorial table, unverified), Cs constant, Tb fixed by salt amount,
stove beats losses at all visited states. Facts supplied: 1 L, 20 C, stove; salt amount, pot, heater, room unspecified.

RESULT (answer_theorem, no sorry): if D>=0 and D+Cs(Tb-Tr)>=0 (enthalpy of solution still >=0 at the boiling point;
automatic if Cs>=0), adding salt at the start (Ta=T0) is no slower than adding at any later water temperature Ta.
(Derived from a sign-free comparison lemma: salting at x changes T by -(D+Cs(x-Tr))/(Cw+Cs); cooling over an interval
favours earlier salting, warming favours later.)
Implication, with the numbers: for strong brines (Cs>0) salting early is best. For cooking amounts (Cs<0) the
condition is marginal with constant 25 C numbers: the switch temperature Ta*=Tr+D/|Cs| is ~85 C at 2 wt%, ~100+ C at 4 wt%.
With temperature-dependent Cs (less negative when hot) the enthalpy of solution stays about >=0 up to ~100 C (rough
estimate, ~0 at 100 C), so early salting is consistent with the data but the sign in the last few degrees is not
resolved. In any case the temperature jump is at most ~0.3 K (D/C ~ 1.4 kJ/4.2 kJ/K), so the time difference is a
fraction of a percent-scale effect, far below stove/pot variability.
NOT proved: that the condition holds for given amounts (needs D, Cs values as inputs; D and Cs are T-dependent in reality),
quantitative time difference, magnitudes of data. No request or measurement was run; an explicit request for
D(T) and Cp(T,m) from NIST/JPCRD (Archer 1992) would resolve the last-few-degrees sign.
Practical advice: add salt at the start (also avoids boil-over); the timing effect is negligible.
{
 "proof_file": "Model.lean",
 "narrative_file": "report.txt",
 "conclusions": {
  "answer": "answer_theorem",
  "cs_sign": "cs_sign_theorem"
 },
 "user_facts": [
  "1 L of water at 20 C in a pot on a stove",
  "salt will be added; goal: salted water reaches its boiling point as soon as possible",
  "salt quantity, pot, heater, room unspecified"
 ],
 "model_choices": [
  "accepted Newtonian lumped heating model",
  "Cs of either sign, constant; D enthalpy of solution at Tr",
  "salt at Tr, instant mixing, enthalpy conserved; Ta in [T0,Tb]; stove overcomes losses (premise)"
 ],
 "sources": [
  "processconvert.com NaCl(aq) specific-heat table (Laliberte 2009 correlation, computed values)",
  "DTIC ADA045707 (OCR, unverified) apparent molal Cp NaCl",
  "Abraham & Marcus via search snippet; dissolution +3.88 kJ/mol tutorial table (unverified)"
 ],
 "incorporated_inputs": [],
 "proof_scope": "cs_sign_theorem: Cs<0 iff c/cw<1-w, and signs at tabulated NaCl data points (data are inputs). answer_theorem: given D>=0 and D+Cs(Tb-Tr)>=0, adding salt at the start is no slower than any later time. Whether the premise holds for a given salt amount rests on literature D and Cs values (marginal near boiling for cooking amounts); not proved. Human review needed."
}human-feedback.json
Bash input
{
  "command": "cd /work; cat Model.lean; ls checks; ls checks/* | head -30"
}
Bash output
import Mathlib.Analysis.SpecialFunctions.Log.Deriv
import Mathlib.Analysis.Calculus.MeanValue

/-! Model (see report.txt).  H = enthalpy (J) measured from "everything at room temperature Tr".
 Pure water, heat capacity Cw: temperature Tr + H/Cw.
 Salt solution, total heat capacity Cw+Cs>0 (Cs of EITHER sign; literature: Cs<0), dissolution enthalpy D (at Tr) of either sign:
 temperature Tr + (H-D)/(Cw+Cs).  Salt (at Tr) is mixed in instantly, so H is conserved.
 Heater power P constant, loss h*(T-Tr):  dH/dt = P - h*(T-Tr).
 Separating variables, the time to move from H1 to H2 is Fw/Fs(H2) - Fw/Fs(H1) where
 Fw' = 1/(P - h*(H/Cw)), Fs' = 1/(P - h*((H-D)/(Cw+Cs))) (explicit logarithms below).
 Tb = boiling point of the final solution (fixed by salt amount, not by timing). -/

/-- time-integral of dH/(P - loss) for pure water (antiderivative, h>0) -/
noncomputable def Fw (Cw P h H : ℝ) : ℝ := -(Cw/h) * Real.log (P - h*(H/Cw))
/-- same for the salt solution -/
noncomputable def Fs (Cw Cs D P h H : ℝ) : ℝ := -((Cw+Cs)/h) * Real.log (P - h*((H-D)/(Cw+Cs)))

/-- Total time to reach Tb from T0 when the salt is added once the water is at Ta:
 heating water alone from T0 to Ta, then solution from the post-mixing state up to Tb. -/
noncomputable def boilTime (Cw Cs D P h Tr T0 Tb Ta : ℝ) : ℝ :=
  (Fw Cw P h (Cw*(Ta-Tr)) - Fw Cw P h (Cw*(T0-Tr))) +
  (Fs Cw Cs D P h ((Cw+Cs)*(Tb-Tr)+D) - Fs Cw Cs D P h (Cw*(Ta-Tr)))

lemma G_deriv (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs))) :
    ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), HasDerivAt (fun H => Fw Cw P h H - Fs Cw Cs D P h H)
      (1/(P - h*(H/Cw)) - 1/(P - h*((H-D)/(Cw+Cs)))) H := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  intro H hH
  have a1 : HasDerivAt (fun H : ℝ => P - h*(H/Cw)) (-(h/Cw)) H := by
    have := ((hasDerivAt_id H).div_const Cw).const_mul h
    simpa [div_eq_mul_inv, mul_comm] using (this.const_sub P)
  have a2 : HasDerivAt (fun H : ℝ => P - h*((H-D)/(Cw+Cs))) (-(h/(Cw+Cs))) H := by
    have := (((hasDerivAt_id H).sub_const D).div_const (Cw+Cs)).const_mul h
    simpa [div_eq_mul_inv, mul_comm] using (this.const_sub P)
  have l1 := (a1.log (hw H hH).ne').const_mul (-(Cw/h))
  have l2 := (a2.log (hs H hH).ne').const_mul (-((Cw+Cs)/h))
  have := l1.sub l2
  convert this using 1
  · funext x; rfl
  · have e1 := (hw H hH).ne'
    have e2 := (hs H hH).ne'
    have e3 := hh.ne'
    have e4 := hCw.ne'
    have e5 := hC.ne'
    field_simp

/-- Salt added at water temperature x changes the temperature at once by
 (D + Cs (x-Tr))/(Cw+Cs) downward (a cooling if D + Cs (x-Tr) > 0).  -/
lemma mix_iff (Cw Cs D Tr x : ℝ) (hCw : 0 < Cw) (hC : 0 < Cw + Cs) :
    (Cw*(x-Tr) - D)/(Cw+Cs) ≤ Cw*(x-Tr)/Cw ↔ 0 ≤ D + Cs*(x-Tr) := by
  rw [mul_div_cancel_left₀ _ hCw.ne', div_le_iff₀ hC]
  constructor <;> intro h <;> nlinarith

lemma G_mono (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs)))
    (S : Set ℝ) (hS : S ⊆ Set.Icc 0 (Cw*(Tb-Tr))) (hconv : Convex ℝ S)
    (sgn : ∀ H ∈ S, (H-D)/(Cw+Cs) ≤ H/Cw) :
    MonotoneOn (fun H => Fw Cw P h H - Fs Cw Cs D P h H) S := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  have hd := G_deriv Cw Cs D P h Tr Tb hCw hC hh hP hs
  apply monotoneOn_of_deriv_nonneg hconv
  · intro H hH; exact (hd H (hS hH)).continuousAt.continuousWithinAt
  · intro H hH; exact (hd H (hS (interior_subset hH))).differentiableAt.differentiableWithinAt
  · intro H hH
    have hH' := interior_subset hH
    rw [(hd H (hS hH')).deriv]
    apply sub_nonneg.mpr
    apply one_div_le_one_div_of_le (hw H (hS hH'))
    have := sgn H hH'
    nlinarith

lemma G_anti (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs)))
    (S : Set ℝ) (hS : S ⊆ Set.Icc 0 (Cw*(Tb-Tr))) (hconv : Convex ℝ S)
    (sgn : ∀ H ∈ S, H/Cw ≤ (H-D)/(Cw+Cs)) :
    AntitoneOn (fun H => Fw Cw P h H - Fs Cw Cs D P h H) S := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  have hd := G_deriv Cw Cs D P h Tr Tb hCw hC hh hP hs
  apply antitoneOn_of_deriv_nonpos hconv
  · intro H hH; exact (hd H (hS hH)).continuousAt.continuousWithinAt
  · intro H hH; exact (hd H (hS (interior_subset hH))).differentiableAt.differentiableWithinAt
  · intro H hH
    have hH' := interior_subset hH
    rw [(hd H (hS hH')).deriv]
    apply sub_nonpos.mpr
    apply one_div_le_one_div_of_le (hs H (hS hH'))
    have := sgn H hH'
    nlinarith

/-- Comparison lemma (sign-free; proved, used by the answer below).  Salt of fixed amount (Cs, D arbitrary sign, C=Cw+Cs>0).
 If between water temperatures Ta1 ≤ Ta2 salt addition always COOLS (D+Cs(x-Tr) ≥ 0 for x in [Ta1,Ta2]),
 adding at Ta1 is no slower than at Ta2; if it always WARMS (≤ 0), adding at Ta2 is no slower. -/
theorem compare_theorem : ∀ (Cw Cs D P h Tr T0 Tb : ℝ),
    0 < Cw → 0 < Cw + Cs → 0 < h → h*(Tb-Tr) < P → Tr ≤ T0 →
    (∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs))) →
    ∀ Ta1 Ta2 : ℝ, T0 ≤ Ta1 → Ta1 ≤ Ta2 → Ta2 ≤ Tb →
      ((∀ x, Ta1 ≤ x → x ≤ Ta2 → 0 ≤ D + Cs*(x-Tr)) →
          boilTime Cw Cs D P h Tr T0 Tb Ta1 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta2) ∧
      ((∀ x, Ta1 ≤ x → x ≤ Ta2 → D + Cs*(x-Tr) ≤ 0) →
          boilTime Cw Cs D P h Tr T0 Tb Ta2 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta1) := by
  intro Cw Cs D P h Tr T0 Tb hCw hC hh hP hTr hs Ta1 Ta2 h1 h12 h2
  have hmem : ∀ x, Ta1 ≤ x → x ≤ Ta2 → Cw*(x-Tr) ∈ Set.Icc 0 (Cw*(Tb-Tr)) := fun x a b =>
    ⟨by nlinarith, by nlinarith⟩
  have hS : Set.Icc (Cw*(Ta1-Tr)) (Cw*(Ta2-Tr)) ⊆ Set.Icc 0 (Cw*(Tb-Tr)) := by
    intro H hH
    exact ⟨by nlinarith [hH.1], by nlinarith [hH.2]⟩
  have hHx : ∀ H ∈ Set.Icc (Cw*(Ta1-Tr)) (Cw*(Ta2-Tr)), ∃ x, Ta1 ≤ x ∧ x ≤ Ta2 ∧ H = Cw*(x-Tr) := by
    intro H hH
    refine ⟨H/Cw + Tr, ?_, ?_, ?_⟩
    · have : Ta1 - Tr ≤ H/Cw := by rw [le_div_iff₀ hCw]; nlinarith [hH.1]
      linarith
    · have : H/Cw ≤ Ta2 - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
      linarith
    · field_simp; ring
  constructor
  · intro hx
    have m := G_mono Cw Cs D P h Tr Tb hCw hC hh hP hs _ hS (convex_Icc _ _) (by
      intro H hH
      obtain ⟨x, a, b, rfl⟩ := hHx H hH
      exact (mix_iff Cw Cs D Tr x hCw hC).mpr (hx x a b))
      ⟨le_rfl, by nlinarith⟩ ⟨by nlinarith, le_rfl⟩ (by nlinarith)
    simp only [boilTime]; simp only at m; linarith
  · intro hx
    have m := G_anti Cw Cs D P h Tr Tb hCw hC hh hP hs _ hS (convex_Icc _ _) (by
      intro H hH
      obtain ⟨x, a, b, rfl⟩ := hHx H hH
      have := hx x a b
      have h3 : ¬ ((Cw*(x-Tr) - D)/(Cw+Cs) < Cw*(x-Tr)/Cw) → True := fun _ => trivial
      rw [mul_div_cancel_left₀ _ hCw.ne', le_div_iff₀ hC]
      nlinarith)
      ⟨le_rfl, by nlinarith⟩ ⟨by nlinarith, le_rfl⟩ (by nlinarith)
    simp only [boilTime]; simp only at m; linarith


/-- Total heat capacity added by the salt, for 1 kg water plus salt of mass fraction w, solution specific
 heat c and pure-water specific heat cw (J/g/K): Cs = 1000/(1-w)*c - 1000*cw  (J/K). -/
noncomputable def saltCs (w c cw : ℝ) : ℝ := 1000/(1-w)*c - 1000*cw

/-- SIGN OF Cs. (i) In general Cs<0 iff c/cw < 1-w (the solution's specific heat falls by more than the
 dilution by mass).  (ii) With the Laliberte-correlation values for NaCl(aq) (web-tabulated; computed, not a
 measurement; c in J/g/K, cw = 4.180 at 25 C, 4.197 at 80 C): Cs<0 for 2, 4, 10 wt% at 25 C and 80 C,
 and Cs>0 for 20 wt% at both. So Cs is negative in the cooking regime and changes sign near 13 wt%. -/
theorem cs_sign_theorem :
    (∀ w c cw : ℝ, 0 < cw → w < 1 → (saltCs w c cw < 0 ↔ c/cw < 1 - w)) ∧
    saltCs 0.02 4.076 4.180 < 0 ∧ saltCs 0.04 3.981 4.180 < 0 ∧ saltCs 0.10 3.733 4.180 < 0 ∧
    saltCs 0.20 3.411 4.180 > 0 ∧
    saltCs 0.02 4.101 4.197 < 0 ∧ saltCs 0.04 4.011 4.197 < 0 ∧ saltCs 0.10 3.767 4.197 < 0 ∧
    saltCs 0.20 3.436 4.197 > 0 := by
  refine ⟨?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩
  · intro w c cw hcw hw
    have h1 : 0 < 1 - w := by linarith
    unfold saltCs
    rw [div_lt_iff₀ hcw]
    constructor <;> intro h
    · have : 1000/(1-w)*c < 1000*cw := by linarith
      rw [div_mul_eq_mul_div, div_lt_iff₀ h1] at this
      nlinarith
    · have : 1000*c < 1000*cw*(1-w) := by nlinarith
      have h2 : 1000/(1-w)*c < 1000*cw := by
        rw [div_mul_eq_mul_div, div_lt_iff₀ h1]; nlinarith
      linarith
  all_goals (unfold saltCs; norm_num)

/-- ANSWER.  Same accepted model (lumped Cw, constant power P, Newton loss h, no evaporation, salt added at Tr,
 enthalpy conserved), Cs of either sign.  D = enthalpy of solution at Tr (literature: D>0, endothermic,
 about +3.9 kJ/mol).  If D >= 0 and the enthalpy of solution at the boiling point is still >= 0,
 i.e. D + Cs (Tb-Tr) >= 0 (Cs<0: cooling persists up to Tb; Cs>=0: automatic), then adding the salt at the
 start (Ta = T0) is no slower than adding it at any later water temperature Ta. -/
theorem answer_theorem : ∀ (Cw Cs D P h Tr T0 Tb : ℝ),
    0 < Cw → 0 < Cw + Cs → 0 < h → h*(Tb-Tr) < P → Tr ≤ T0 →
    (∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs))) →
    0 ≤ D → 0 ≤ D + Cs*(Tb-Tr) → T0 ≤ Tb →
    ∀ Ta : ℝ, T0 ≤ Ta → Ta ≤ Tb →
      boilTime Cw Cs D P h Tr T0 Tb T0 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta := by
  intro Cw Cs D P h Tr T0 Tb hCw hC hh hP hTr hs hD hDb hT0 Ta h1 h2
  refine (compare_theorem Cw Cs D P h Tr T0 Tb hCw hC hh hP hTr hs T0 Ta le_rfl h1 h2).1 ?_
  intro x hx1 hx2
  rcases le_total 0 Cs with hc | hc
  · nlinarith
  · nlinarith
248420fe49ad4124bd13c839d06e22c4
52448c0db62c400e90cbd46e54881cba
7e4be0bd42b444199e65b96370c18486
7fdbd8cda6304fd3ae91fdd97ab702a9
91bf5367d0354c09a0f1841d6c390708
97c02acf02654117a33c008e8abdb8f0
9d2a29707556410397d3c62a510d59a9
b7ec15d94a8146bfadfbd5cc72230a57
checks/248420fe49ad4124bd13c839d06e22c4:
Checked.lean
extracted.json
extracted.lean
lean.log
submitted-candidate.json
submitted-report.txt
submitted.lean
verdict.json

checks/52448c0db62c400e90cbd46e54881cba:
Checked.lean
extracted.json
extracted.lean
lean.log
submitted-candidate.json
submitted-report.txt
submitted.lean
verdict.json

checks/7e4be0bd42b444199e65b96370c18486:
Checked.lean
extracted.json
extracted.lean
lean.log
submitted-candidate.json
submitted-report.txt
submitted.lean
verdict.json
WebSearch input
{
  "query": "Laliberté 2009 \"Model for Calculating the Viscosity of Aqueous Solutions\" OR \"heat capacity\" aqueous solutions NaCl coefficients a1 a2 a3 a4 a5 a6 apparent specific heat capacity Cp_i = a1 exp(α) + a2(1 - w_w)^a3"
}
WebSearch output
Web search results for query: "Laliberté 2009 "Model for Calculating the Viscosity of Aqueous Solutions" OR "heat capacity" aqueous solutions NaCl coefficients a1 a2 a3 a4 a5 a6 apparent specific heat capacity Cp_i = a1 exp(α) + a2(1 - w_w)^a3"

Links: [{"title":"Model for Calculating the Viscosity of Aqueous Solutions","url":"https://www.researchgate.net/publication/231536613_Model_for_Calculating_the_Viscosity_of_Aqueous_Solutions"},{"title":"A Model for Calculating the Heat Capacity of Aqueous Solutions, with Updated Density and Viscosity Data","url":"https://www.semanticscholar.org/paper/A-Model-for-Calculating-the-Heat-Capacity-of-with-Lalibert%C3%A9/468bfc63f78a65ae17c4dc4a1e8bbd5f774dc3d6"},{"title":"A Model for Calculating the Heat Capacity of Aqueous Solutions, with Updated Density and Viscosity Data","url":"https://www.researchgate.net/publication/231541607_A_Model_for_Calculating_the_Heat_Capacity_of_Aqueous_Solutions_with_Updated_Density_and_Viscosity_Data"},{"title":"The Viscosity of Aqueous Solutions as a Function of the Concentration","url":"https://pubs.acs.org/doi/10.1021/ja01329a024"},{"title":"A Composition-Based Approach for Predicting Hanford ...","url":"https://www.osti.gov/servlets/purl/1660731"},{"title":"New model of brine viscosity in multicomponent systems, in the temperature range of 298.15 to 313.15 K - ScienceDirect","url":"https://www.sciencedirect.com/science/article/pii/S2667312625000392"},{"title":"A Model for Calculating the Heat Capacity of Aqueous Solutions, with Updated Density and Viscosity Data","url":"https://ouci.dntb.gov.ua/en/works/lDWxvvo4/"},{"title":"Electrochemistry (thermo.electrochem) - Read the Docs","url":"https://thermo.readthedocs.io/thermo.electrochem.html"},{"title":"Viscosity as a Smoking Gun for Complex Formation in Solution: Fe$^{2+}$ and Mg$^{2+}$ Chlorides as Examples","url":"https://arxiv.org/pdf/2603.05077"},{"title":"(PDF) A Numerical Method for Predicting Thermophysical Properties of Complex Chloride-Dominated Brines","url":"https://www.researchgate.net/publication/321373112_A_Numerical_Method_for_Predicting_Thermophysical_Properties_of_Complex_Chloride-Dominated_Brines"}]

The search didn't return the NaCl coefficients a1–a6. It did identify the source paper and a place where the coefficients are probably implemented.

- **Source paper:** M. Laliberté, "A Model for Calculating the Heat Capacity of Aqueous Solutions, with Updated Density and Viscosity Data," *J. Chem. Eng. Data* 54(6), 1725–1760 (2009), DOI 10.1021/je8008123. It gives a heat-capacity model for solutions with any number of solutes at any temperature. Parameters for 79 solutes were fitted from about 6600 literature points. The same paper also updated the density and viscosity data. These are the 2009 values that supersede the 2007 viscosity paper (*J. Chem. Eng. Data* 52(2), DOI 10.1021/je0604075).
- **Where the coefficients are:** The search results don't reproduce the per-solute coefficient tables, and the paper is paywalled at ACS. The `thermo` Python library (`thermo.electrochem`) implements this model. Its `Laliberte_heat_capacity(T, ws, CASRNs)` function calculates the heat capacity of an aqueous electrolyte mixture using the form Laliberté proposed. The library also has separate functions for the solute partial heat capacity (`Cp_i`) and the water heat capacity (`Cp_w`). It loads Laliberté's coefficient table, so you can read the NaCl a1–a6 values from its data file. The documentation notes a check performed using NaCl at 5 °C against the first value in Laliberté's spreadsheet.
- **Other uses of the model:** A ResearchGate paper on chloride-dominated brines compares its own predictions with the Laliberté (2009) formulation for NaCl solutions.

To get the exact NaCl coefficients, check the `thermo` library's Laliberté data file, or the supporting tables and spreadsheet that accompany the paper.


REMINDER: You MUST include the sources above in your response to the user using markdown hyperlinks.
Bash input
{
  "command": "pip list 2>/dev/null | grep -i -E \"thermo|chemicals|scipy|numpy\"; pip download thermo 2>&1 | tail -1"
}
Bash output
[notice] To update, run: pip install --upgrade pip
Bash input
{
  "command": "cd /work; ls; "
}
Bash output
Model.lean
candidate.json
checks
chemicals-1.5.2-py3-none-any.whl
fluids-1.3.1-py3-none-any.whl
human-feedback.json
last-review-packet.json
numpy-2.5.5-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
pandas-3.1.1-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
python_dateutil-4.0.0-py3-none-any.whl
report.txt
scipy-1.18.1-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
thermo-0.6.1-py3-none-any.whl
tzdata-2026.6-py2.py3-none-any.whl
Bash input
{
  "command": "mkdir -p /tmp/wh /tmp/ex && mv /work/*.whl /tmp/wh/ && cd /tmp/ex && unzip -q -o /tmp/wh/chemicals-1.5.2-py3-none-any.whl 'chemicals/Laliberte*' 2>&1; unzip -q -o /tmp/wh/thermo-0.6.1-py3-none-any.whl 'thermo/Laliberte*' 2>&1; find /tmp/ex -iname '*lalib*'; ls /work"
}
Bash output
/bin/bash: line 1: unzip: command not found
/bin/bash: line 1: unzip: command not found
Model.lean
candidate.json
checks
human-feedback.json
last-review-packet.json
report.txt
Shell cwd was reset to /work
Bash input
{
  "command": "cd /tmp/ex; unzip -l /tmp/wh/thermo-0.6.1-py3-none-any.whl | grep -i -E \"lalib|electro\"; unzip -l /tmp/wh/chemicals-1.5.2-py3-none-any.whl | grep -i -E \"lalib|electro\""
}
Bash output
/bin/bash: line 1: unzip: command not found
/bin/bash: line 1: unzip: command not found
Bash input
{
  "command": "cd /tmp/ex; python3 -I - <<'E'\nimport zipfile\nfor w in ['thermo-0.6.1-py3-none-any.whl','chemicals-1.5.2-py3-none-any.whl']:\n    z=zipfile.ZipFile('/tmp/wh/'+w)\n    for n in z.namelist():\n        if 'alib' in n or 'lectro' in n: print(w,n)\n        if 'alib' in n: z.extract(n,'/tmp/ex')\nE"
}
Bash output
thermo-0.6.1-py3-none-any.whl thermo/electrochem.py
chemicals-1.5.2-py3-none-any.whl chemicals/Electrolytes/CRC Thermodynamic Properties of Aqueous Ions.tsv
chemicals-1.5.2-py3-none-any.whl chemicals/Electrolytes/CRC conductivity infinite dilution.tsv
chemicals-1.5.2-py3-none-any.whl chemicals/Electrolytes/Electrolyte dissociations.tsv
chemicals-1.5.2-py3-none-any.whl chemicals/Electrolytes/Laliberte2009.tsv
chemicals-1.5.2-py3-none-any.whl chemicals/Electrolytes/Lange Pure Species Conductivity.tsv
chemicals-1.5.2-py3-none-any.whl chemicals/Electrolytes/Magomedov Thermal Conductivity.tsv
chemicals-1.5.2-py3-none-any.whl chemicals/Electrolytes/Marcus Ion Conductivities.tsv
chemicals-1.5.2-py3-none-any.whl chemicals/Electrolytes/McCleskey Electrical Conductivity.tsv
chemicals-1.5.2-py3-none-any.whl chemicals/Electrolytes/Permittivity (Dielectric Constant) of Liquids.tsv
Shell cwd was reset to /work
Bash input
{
  "command": "cd /tmp/ex/chemicals/Electrolytes; head -2 Laliberte2009.tsv; grep -P \"\\tSodium chloride|7647-14-5\" Laliberte2009.tsv; python3 -I - <<'E'\nimport zipfile\nz=zipfile.ZipFile('/tmp/wh/thermo-0.6.1-py3-none-any.whl')\ns=z.read('thermo/electrochem.py').decode()\ni=s.find('def Laliberte_heat_capacity_i')\nprint(s[i:i+2500]) if i>=0 else print('nf')\nE\ngrep -rn \"def Laliberte_heat_capacity\" -r /tmp/ex | head"
}
Bash output
Name	CASRN	Formula	MW	c0	c1	c2	c3	c4	Min T	Max T	Max w	No of points in corr	v1	v2	v3	v4	v5	v6	Min T	Max T	Max w	No of points in corr	a1	a2	a3	a4	a5	a6	Min T	Max T	Max w	No of points in corr
Ammonium Sulfate	7783-20-2	(NH4)2SO4	132.14	618.24075073611	966.128116121243	0.602706202526747	0.0016279026793269	667.573420648081	15	55	0.429188916694159	142	11.9684222123058	2.60592145291167	1.90830128213142	0.00731291601229317	141.459340621835	6.77837848574609	15	60	0.462943071965628	148										
Sodium Chloride	7647-14-5	NaCl	58.45	-0.00324112223655149	0.0636354335906616	1.01371399467365	0.0145951015210159	3317.34854426537	0	140	0.26589930421877	869	16.221788633396	1.32293086770011	1.48485985010431	0.00746912559657377	30.7802007540575	2.05826852322558	5	154	0.264456748962402	552	-0.0693559668993322	-0.0782134167486952	3.84798479408635	-11.2762109247072	8.73187698542672	1.81245930472755	1.5	120	0.261058295490885	356
def Laliberte_heat_capacity_i(T, w_w, a1, a2, a3, a4, a5, a6):
    r"""Calculate the heat capacity of a solute using the form proposed by [1]_
    Parameters are needed, and a temperature, and water fraction.

    .. math::
        Cp_i = a_1 e^\alpha + a_5(1-w_w)^{a_6}

    .. math::
        \alpha = a_2 t + a_3 \exp(0.01t) + a_4(1-w_w)

    Parameters
    ----------
    T : float
        Temperature of fluid [K]
    w_w : float
        Weight fraction of water in the solution
    a1-a6 : floats
        Function fit parameters

    Returns
    -------
    Cp_i : float
        Solute partial heat capacity, [J/kg/K]

    Notes
    -----
    Units are Kelvin and J/kg/K.
    Temperature range check is not used here.

    Examples
    --------
    >>> params = [-0.0693559668993322, -0.0782134167486952, 3.84798479408635, -11.2762109247072, 8.73187698542672, 1.81245930472755]
    >>> Laliberte_heat_capacity_i(1.5+273.15, 1-0.00398447, *params)
    -2930.73539458

    References
    ----------
    .. [1] Laliberte, Marc. "A Model for Calculating the Heat Capacity of
       Aqueous Solutions, with Updated Density and Viscosity Data." Journal of
       Chemical & Engineering Data 54, no. 6 (June 11, 2009): 1725-60.
       doi:10.1021/je8008123
    """
    if T < 273.15-15:
        T = 273.15-15
    t = T - 273.15
    alpha = a2*t + a3*exp(0.01*t) + a4*(1. - w_w)
    Cp_i = a1*exp(alpha) + a5*(1. - w_w)**a6
    return Cp_i*1000.

def Laliberte_heat_capacity_mix(T, ws, a1s, a2s, a3s, a4s, a5s, a6s):
    r"""Calculate the heat capacity of an aqueous electrolyte mixture using the
    form proposed by [1]_. All parameters must be provided to this function.

    .. math::
        Cp_m = w_w Cp_w + \sum w_i Cp_i

    Parameters
    ----------
    T : float
        Temperature of fluid [K]
    ws : array
        Weight fractions of fluid components other than water

    Returns
    -------
    Cp : float
        Solution heat capacity, [J/kg/K]

    Notes
    -----
    A temperature range check is not included in this function.
    Units are Kelvin and J/kg/K.

    Examples
    --------
    >>> Laliberte_heat_capacity_mix(T=278.15, ws=[0.00581, 0.002], a1s=[-0.0693559668993322, -0.103713247177424], a2s=[-0.0782134167486952, -0.0647453826944371], a3s=[3.84798479408635, 2.92191453087969], a4s=[-11.2762109247072, -5.48799065938436], a5s=[8.73187698542672, 2.41768600041476], a6s=[1.81245930472755, 1.32062411084408])
    4154.788562680796

    References
    ----------
    .. [
Shell cwd was reset to /work
Bash input
{
  "command": "cd /tmp; python3 -I - <<'E'\nimport zipfile,re\nz=zipfile.ZipFile('/tmp/wh/thermo-0.6.1-py3-none-any.whl')\ns=z.read('thermo/electrochem.py').decode()\ni=s.find('def Laliberte_heat_capacity_w'); print(s[i:i+1800])\nE"
}
Bash output
def Laliberte_heat_capacity_w(T):
    r"""Calculate the heat capacity of pure water in a fast but similar way as
    in [1]_. [1]_ suggested the following interpolatative scheme, using points
    calculated from IAPWS-97 at a pressure of 0.1 MPa up to 95 °C and then at
    saturation pressure. The maximum temperature of [1]_ is 140 °C.

    .. math::
        Cp_w = Cp_1 + (Cp_2-Cp_1) \left( \frac{t-t_1}{t_2-t_1}\right)
        + \frac{(Cp_3 - 2Cp_2 + Cp_1)}{2}\left( \frac{t-t_1}{t_2-t_1}\right)
        \left( \frac{t-t_1}{t_2-t_1}-1\right)

    In this implementation, the heat capacity of water is calculated from a
    chebyshev approximation of the scheme of [1]_ up to ~92 °C and then the
    heat capacity comes directly from IAPWS-95 at higher temperatures, also
    at the saturation pressure. There is no discontinuity between the methods.

    Parameters
    ----------
    T : float
        Temperature of fluid [K]

    Returns
    -------
    Cp_w : float
        Water heat capacity, [J/kg/K]

    Notes
    -----
    Units are Kelvin and J/kg/K.

    Examples
    --------
    >>> Laliberte_heat_capacity_w(273.15+3.56)
    4208.878727051538

    References
    ----------
    .. [1] Laliberte, Marc. "A Model for Calculating the Heat Capacity of
       Aqueous Solutions, with Updated Density and Viscosity Data." Journal of
       Chemical & Engineering Data 54, no. 6 (June 11, 2009): 1725-60.
       doi:10.1021/je8008123
    """
    if T > 365.1800756083714: # 92, when the fits crossover
        return iapws95_Cpl_mass_sat(T)
    elif T < 273.15 - 15:
        T = 273.15 - 15
    return chebval(0.012903225806451612892*(T - 335.64999999999997726),
                   Laliberte_heat_capacity_coeffs)


def Laliberte_heat_capacity_i(T, w_w, a1, a2, a3, a4, a5, a6):
    r"""C
Shell cwd was reset to /work
Bash input
{
  "command": "cd /tmp; python3 -I - <<'E'\nfrom math import exp\na=[-0.0693559668993322,-0.0782134167486952,3.84798479408635,-11.2762109247072,8.73187698542672,1.81245930472755]\ncw={20:4.1819,30:4.1785,40:4.1786,50:4.1806,60:4.1843,70:4.1895,80:4.1969,90:4.2051,100:4.2160}\ndef csol(t,w):\n    ww=1-w\n    al=a[1]*t+a[2]*exp(.01*t)+a[3]*w\n    ci=a[0]*exp(al)+a[4]*w**a[5]\n    return ww*cw[t]+w*ci, ci\nfor r in [0.01,0.02,0.035,0.05]:\n    w=r/(1+r)\n    print('r',r,'w',w, 'n/kg',r*1000/58.443)\n    for t in cw:\n        c,ci=csol(t,w)\n        Cs=1000*(1+r)*c-1000*cw[t]\n        print(t, round(Cs,1), 'Phi=',round(Cs/(r*1000/58.443),1))\nE"
}
Bash output
r 0.01 w 0.009900990099009901 n/kg 0.1711068904744794
20 -14.2 Phi= -83.3
30 -10.7 Phi= -62.4
40 -8.4 Phi= -49.3
50 -7.1 Phi= -41.2
60 -6.3 Phi= -36.7
70 -6.0 Phi= -35.1
80 -6.2 Phi= -36.3
90 -7.0 Phi= -40.9
100 -8.7 Phi= -50.6
r 0.02 w 0.0196078431372549 n/kg 0.3422137809489588
20 -25.4 Phi= -74.3
30 -19.0 Phi= -55.6
40 -15.0 Phi= -43.9
50 -12.5 Phi= -36.6
60 -11.2 Phi= -32.6
70 -10.7 Phi= -31.2
80 -11.0 Phi= -32.2
90 -12.4 Phi= -36.3
100 -15.4 Phi= -45.1
r 0.035 w 0.03381642512077295 n/kg 0.5988741166606779
20 -37.5 Phi= -62.6
30 -27.9 Phi= -46.7
40 -21.9 Phi= -36.6
50 -18.2 Phi= -30.5
60 -16.2 Phi= -27.0
70 -15.5 Phi= -25.8
80 -16.0 Phi= -26.7
90 -18.1 Phi= -30.2
100 -22.5 Phi= -37.6
r 0.05 w 0.047619047619047616 n/kg 0.8555344523723971
20 -44.9 Phi= -52.5
30 -33.2 Phi= -38.8
40 -25.9 Phi= -30.2
50 -21.4 Phi= -25.0
60 -18.8 Phi= -22.0
70 -17.9 Phi= -21.0
80 -18.6 Phi= -21.7
90 -21.2 Phi= -24.7
100 -26.6 Phi= -31.1
Shell cwd was reset to /work
Bash input
{
  "command": "cd /tmp; python3 -I - <<'E'\nfrom math import exp\nimport numpy as np\na=[-0.0693559668993322,-0.0782134167486952,3.84798479408635,-11.2762109247072,8.73187698542672,1.81245930472755]\nts=[20,30,40,50,60,70,80,90,100,110]\ncwv=[4.1819,4.1785,4.1786,4.1806,4.1843,4.1895,4.1969,4.2051,4.2160,4.2318]\ndef cw(t): return np.interp(t,ts,cwv)\ndef Phi(t,r):\n    w=r/(1+r); al=a[1]*t+a[2]*exp(.01*t)+a[3]*w\n    ci=a[0]*exp(al)+a[4]*w**a[5]\n    c=(1-w)*cw(t)+w*ci\n    return (1000*(1+r)*c-1000*cw(t))/(r*1000/58.443)\nfor Tb in [100.0,101.0]:\n  for r in [0.005,0.01,0.02,0.035,0.05]:\n    T=np.linspace(20,Tb,801)\n    P=np.array([Phi(t,r) for t in T])\n    I=np.trapz(P,T)\n    E=3880+np.array([np.trapz(P[:i+1],T[:i+1]) for i in range(len(T))])\n    print(Tb,r,'mean Phi',round(I/(Tb-20),1),'max Phi',P.max().round(1),'min',P.min().round(1),'E(Tb)',round(3880+I),'minE',E.min().round(0))\nE"
}
Bash output · error
Exit code 1
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
ModuleNotFoundError: No module named 'numpy'
Bash input
{
  "command": "cd /tmp; python3 -I - <<'E'\nfrom math import exp\na=[-0.0693559668993322,-0.0782134167486952,3.84798479408635,-11.2762109247072,8.73187698542672,1.81245930472755]\nts=[20,30,40,50,60,70,80,90,100,110]\ncwv=[4.1819,4.1785,4.1786,4.1806,4.1843,4.1895,4.1969,4.2051,4.2160,4.2318]\ndef cw(t):\n    for i in range(len(ts)-1):\n        if ts[i]<=t<=ts[i+1]: return cwv[i]+(cwv[i+1]-cwv[i])*(t-ts[i])/10\ndef Phi(t,r):\n    w=r/(1+r); al=a[1]*t+a[2]*exp(.01*t)+a[3]*w\n    ci=a[0]*exp(al)+a[4]*w**a[5]\n    c=(1-w)*cw(t)+w*ci\n    return (1000*(1+r)*c-1000*cw(t))/(r*1000/58.443)\nfor Tb in [100.0,101.0]:\n  for r in [0.01,0.02,0.035,0.05]:\n    N=800; h=(Tb-20)/N\n    P=[Phi(20+i*h,r) for i in range(N+1)]\n    E=[];s=3880.0;mn=1e9\n    for i in range(N+1):\n        if i: s+=(P[i]+P[i-1])/2*h\n        mn=min(mn,s)\n    print(Tb,r,'meanPhi',round((s-3880)/(Tb-20),1),'maxPhi',round(max(P),1),'minPhi',round(min(P),1),'E(Tb)',round(s),'minE',round(mn))\nE"
}
Bash output
100.0 0.01 meanPhi -45.7 maxPhi -35.1 minPhi -83.3 E(Tb) 224 minE 224
100.0 0.02 meanPhi -40.7 maxPhi -31.2 minPhi -74.3 E(Tb) 628 minE 628
100.0 0.035 meanPhi -33.9 maxPhi -25.8 minPhi -62.6 E(Tb) 1169 minE 1169
100.0 0.05 meanPhi -27.9 maxPhi -21.0 minPhi -52.5 E(Tb) 1648 minE 1648
101.0 0.01 meanPhi -45.8 maxPhi -35.1 minPhi -83.3 E(Tb) 173 minE 173
101.0 0.02 meanPhi -40.7 maxPhi -31.2 minPhi -74.3 E(Tb) 582 minE 582
101.0 0.035 meanPhi -33.9 maxPhi -25.8 minPhi -62.6 E(Tb) 1131 minE 1131
101.0 0.05 meanPhi -27.9 maxPhi -21.0 minPhi -52.5 E(Tb) 1617 minE 1617
Shell cwd was reset to /work
Bash input
{
  "command": "cd /work; python3 -I - <<'E'\ns=open('Model.lean').read()\ni=s.index('/-- Total heat capacity added by the salt')\nhead=s[:i]\nnew='''/-! ## Salt-specific part (NaCl, 1 L of water).  Inputs are labelled; nothing numerical below is a measurement.\n\n Choices.  Salt = NaCl, molar mass M = 58.44 g/mol.  1 L of water at 20 C has mass mw = 0.998 kg\n (density 0.9982 kg/L).  The salt/water ratio r = (salt mass)/(water mass) (NOT the mass fraction of the\n final solution; the fraction is w = r/(1+r), proved in `cs_theorem`).  Regime: 0.02 <= r <= 0.05,\n i.e. about 20-50 g NaCl in the litre (2-4.8 wt%; seawater-like to very salty cooking water).\n Cs is DERIVED, not fitted: by definition of the apparent molar heat capacity Phi of the salt in solution,\n (total heat capacity of solution) = (heat capacity of the water) + n*Phi, so Cs = n*Phi, n = moles of salt.\n Phi is temperature dependent; the lumped model needs one constant Cs, so Phi below is the SECANT MEAN of\n Phi(T,m) over 20 C..Tb, which makes the linear mixing-enthalpy D + Cs (T-Tr) exact at both ends 20 C and Tb.\n Empirical inputs (EXPOSED PREMISES, from the Laliberte (2009) NaCl correlation, fit range 1.5-120 C,\n w <= 0.26, evaluated numerically outside Lean -- a synthetic calculation from a published correlation):\n   * secant-mean Phi in [-41, -27] J/K/mol for r in [0.02, 0.05] (computed: -40.7 at r=0.02 ... -27.9 at 0.05);\n   * enthalpy of solution of NaCl(s) at ~20-25 C, hsol >= 3800 J/mol (NBS value +3.88 kJ/mol, endothermic);\n   * boiling point of the solution Tb in [100, 101] C (elevation <= ~1 K for these amounts);\n   * pot+water heat capacity Cw >= 4170 J/K (at least the 0.998 kg of water, 4.18 J/g/K).\n D = n*hsol is the enthalpy absorbed when the salt (at room temperature Tr) dissolves at Tr. -/\n\n/-- Cs = n*Phi: sign, size, and the ratio conversion. -/\ntheorem cs_theorem : ∀ (r M mw n Phi ms mwater : ℝ),\n    0.02 ≤ r → r ≤ 0.05 → M = 58.44 → mw = 0.998 → n = 1000*mw*r/M →\n    -41 ≤ Phi → Phi ≤ -27 → 0 < ms → 0 < mwater →\n    (n*Phi < 0 ∧ -36 ≤ n*Phi) ∧ ms/(ms+mwater) = (ms/mwater)/(1+ms/mwater) := by\n  intro r M mw n Phi ms mwater hr1 hr2 hM hmw hn hP1 hP2 hms hmwa\n  have hn0 : 0 < n := by rw [hn, hM, hmw]; positivity\n  have hn1 : n ≤ 0.8540 := by\n    rw [hn, hM, hmw, div_le_iff₀ (by norm_num)]; nlinarith\n  refine ⟨⟨by nlinarith, by nlinarith⟩, ?_⟩\n  field_simp\n\n/-- ANSWER.  Accepted model (lumped heat capacity Cw, constant stove power P, Newton loss h, no evaporation,\n salt at room temperature Tr = T0 = 20 C dissolves instantly, enthalpy conserved).  Under the premises above\n (r in [0.02,0.05], Cs = n*Phi, D = n*hsol, Tb in [100,101], stove stronger than losses at Tb), adding the\n salt right at the start (Ta = T0) is no slower than adding it at any later water temperature Ta in [T0,Tb].\n The condition D + Cs (x-Tr) >= 0 (salt addition cools / does not warm) is CHECKED here, not assumed, and\n the 'stove beats losses' condition for the salt solution is derived from it. -/\ntheorem answer_theorem : ∀ (Cw P h Tr T0 Tb r M mw n Phi hsol : ℝ),\n    Tr = 20 → T0 = 20 → 100 ≤ Tb → Tb ≤ 101 → 4170 ≤ Cw → 0 < h → h*(Tb-Tr) < P →\n    0.02 ≤ r → r ≤ 0.05 → M = 58.44 → mw = 0.998 → n = 1000*mw*r/M →\n    -41 ≤ Phi → Phi ≤ -27 → 3800 ≤ hsol →\n    ∀ Ta : ℝ, T0 ≤ Ta → Ta ≤ Tb →\n      boilTime Cw (n*Phi) (n*hsol) P h Tr T0 Tb T0 ≤ boilTime Cw (n*Phi) (n*hsol) P h Tr T0 Tb Ta := by\n  intro Cw P h Tr T0 Tb r M mw n Phi hsol hTr hT0 hb1 hb2 hCw hh hP hr1 hr2 hM hmw hn hP1 hP2 hs Ta h1 h2\n  have hn0 : 0 < n := by rw [hn, hM, hmw]; positivity\n  have hn1 : n ≤ 0.8540 := by\n    rw [hn, hM, hmw, div_le_iff₀ (by norm_num)]; nlinarith\n  have hCs : n*Phi < 0 := by nlinarith\n  have hCs2 : -36 ≤ n*Phi := by nlinarith\n  have hCw0 : 0 < Cw := by linarith\n  have hC : 0 < Cw + n*Phi := by linarith\n  have hd : 0 ≤ Tb - Tr := by linarith\n  have hd2 : Tb - Tr ≤ 81 := by linarith\n  -- enthalpy of solution at the boiling point is still >= 0 (checked)\n  have hE0 : 0 ≤ hsol + Phi*(Tb-Tr) := by nlinarith [mul_nonneg (by linarith : 0 ≤ Phi + 41) hd]\n  have hE : 0 ≤ n*hsol + n*Phi*(Tb-Tr) := by nlinarith [mul_nonneg hn0.le hE0]\n  have hstove : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-n*hsol)/(Cw+n*Phi)) := by\n    intro H hH\n    have : (H-n*hsol)/(Cw+n*Phi) ≤ Tb - Tr := by\n      rw [div_le_iff₀ hC]; nlinarith [hH.2]\n    nlinarith\n  refine (compare_theorem Cw (n*Phi) (n*hsol) P h Tr T0 Tb hCw0 hC hh hP (by linarith) hstove\n    T0 Ta le_rfl h1 h2).1 ?_\n  intro x hx1 hx2\n  nlinarith [mul_nonneg (by linarith : 0 ≤ Tb - x) (by linarith : 0 ≤ -(n*Phi))]\n'''\nopen('Model.lean','w').write(head+new)\nE\npython /harness/src/vcs_protocol.py tool check --manifest /work/candidate.json 2>&1 | tail -30"
}
Bash output
{
  "ok": false,
  "check_complete": false,
  "reviewable": false,
  "verdict": "not_proved",
  "time": "2026-11-17T16:05:57.388926+00:00",
  "error": "Missing required theorems: ['cs_sign_theorem']"
}
assistant
Manifest still refers to the old theorem; update it and the report.
Bash input
{
  "command": "cd /work; cat > candidate.json <<'E'\n{\n \"proof_file\": \"Model.lean\",\n \"narrative_file\": \"report.txt\",\n \"conclusions\": {\"answer\": \"answer_theorem\", \"cs\": \"cs_theorem\"},\n \"user_facts\": [\n  \"1 L of water at 20 C in a pot on a stove, heated\",\n  \"salt will be added; goal: salted water reaches its boiling point as soon as possible\",\n  \"salt quantity, pot, heater, room unspecified\"\n ],\n \"model_choices\": [\n  \"accepted Newtonian lumped heating model (Cw, constant power P, loss h(T-Tr), no evaporation), salt at Tr dissolves instantly, enthalpy conserved\",\n  \"salt = NaCl; ratio r = salt mass / water mass in [0.02,0.05] (w=r/(1+r), proved); 1 L = 0.998 kg\",\n  \"Cs = n*Phi by definition of apparent molar heat capacity; constant Phi = secant mean over 20 C..Tb of the Laliberte (2009) NaCl correlation, bounds [-41,-27] J/K/mol (computed outside Lean, synthetic calculation from a published correlation, fit range 1.5-120 C)\",\n  \"D = n*hsol, hsol >= 3800 J/mol (NBS +3.88 kJ/mol); Tb in [100,101] C; Cw >= 4170 J/K; stove stronger than loss at Tb\"\n ],\n \"sources\": [\n  \"Laliberte, J. Chem. Eng. Data 54, 1725 (2009) NaCl coefficients as distributed in the python 'chemicals' package 1.5.2 (Electrolytes/Laliberte2009.tsv); water Cp from standard IAPWS values\",\n  \"NBS/CRC thermochemical tables for enthalpy of solution of NaCl (+3.88 kJ/mol), recalled, not re-verified in this run\"\n ],\n \"incorporated_inputs\": [],\n \"proof_scope\": \"cs_theorem: given r in [0.02,0.05] and Phi in [-41,-27], Cs=n*Phi is negative and >= -36 J/K; mass-ratio to mass-fraction conversion. answer_theorem: under the stated premises the cooling condition D+Cs(Tb-Tr)>=0 and the stove-beats-loss condition for the solution are DERIVED, and salting at the start is no slower than at any later temperature. Numerical Phi/hsol bounds are premises taken from a correlation/tables, not proved or measured; temperature dependence of Phi is handled only by the secant-mean approximation. Human review needed.\"\n}\nE\ncat > report.txt <<'E'\nCYCLE 4 REVISION (response to: choose bounded ratio, derive Cs from an explicit model, prove sign/bounds, check conditions).\nAccepted Newtonian lumped heating model kept unchanged (boilTime, compare_theorem unchanged).\n\nChoices: NaCl; r = salt mass / water mass (not final mass fraction; w=r/(1+r) proved); 1 L at 20 C = 0.998 kg;\nregime r in [0.02,0.05] (20-50 g NaCl in the litre, 2-4.8 wt%).\nCs: by definition of the apparent molar heat capacity, C_solution = C_water + n*Phi, so Cs = n*Phi, n = 1000*0.998*r/58.44 mol (<=0.854 mol).\nPhi from the Laliberte (2009) NaCl correlation (coefficients from the 'chemicals' package data file; fit range 1.5-120 C, w<=0.26),\nevaluated by me in Python (synthetic calculation from a published correlation, not a measurement), water Cp from IAPWS values.\nResults (J/K/mol): Phi(20C) ~ -74 (r=.02) ... -52 (r=.05) [lit. infinite dilution ~ -88 at 25 C, consistent]; Phi rises toward ~-21..-31 near 70-80 C and falls to ~-31..-45 at 100 C.\nSo Cs<0 over the whole 20-100 C, r in regime; |Cs| <= ~45 J/K per kg water (about 1% of 4.2 kJ/K). The earlier cycle's sign change near 13 wt% is outside this regime.\nTemperature dependence: model needs constant Cs; I use the secant mean Phi over 20..Tb, which makes the linear mixing enthalpy exact at 20 C and Tb.\nComputed secant means: -40.7 (r=.02), -33.9 (.035), -27.9 (.05); bounds used [-41,-27]. The true mixing enthalpy E(x)=n(hsol+int Phi) is decreasing, with minimum at Tb:\nE(Tb)/n ~ +630 (r=.02), +1170 (.035), +1650 (.05) J/mol, versus hsol=3880. (At r=0.01 it is only ~+200 and for r<~0.01 it would become negative near Tb: the regime was chosen to exclude this marginal case.)\nResult: salt addition is endothermic at all temperatures 20..Tb (it cools), so earlier is better; compare_theorem (cooling over an interval favours the earlier addition) gives: salting at the start is no slower than at any later Ta. Lean checks D+Cs(x-Tr)>=0 from the premises and derives the stove-beats-loss condition for the solution (no longer a premise).\nSize of effect: temperature drop on mixing <= ~hsol*n/C ~ 0.3-0.7 K at start; time difference is tiny relative to pot/stove variability. Also boil-over/avoid adding salt to boiling water argue for salting early.\nLimits: premises (Phi bounds, hsol, Tb range) are inputs; Laliberte correlation accuracy is a few percent, so margin at r=0.02 (~15% of hsol) is adequate but at r<=0.01 the sign is unresolved. Not proved: quantitative time difference; Phi(T) handled by secant mean; no evaporation or salting-induced boiling-point change in time; no real measurement was run.\nE\npython /harness/src/vcs_protocol.py tool check --manifest /work/candidate.json 2>&1 | tail -30"
}
Bash output
{
  "ok": false,
  "check_complete": false,
  "reviewable": false,
  "verdict": "not_proved",
  "time": "2026-11-17T16:06:13.470924+00:00",
  "lean_exit": 1,
  "error": "Lean, kernel recheck or axiom audit failed; see lean.log"
}
Bash input
{
  "command": "cd /work; d=$(ls -t checks | head -1); grep -n \"error\" -A8 checks/$d/lean.log | head -60"
}
Bash output
1:/work/checks/dcd7a1b461ab4ea1832bd51aed75b96c/Checked.lean:173:78: error: unsolved goals
2-r M mw n Phi ms mwater : ℝ
3-hr1 : 2e-2 ≤ r
4-hr2 : r ≤ 5e-2
5-hM : M = 58.44
6-hmw : mw = 0.998
7-hn : n = 1000 * mw * r / M
8-hP1 : -41 ≤ Phi
9-hP2 : Phi ≤ -27
Bash input
{
  "command": "cd /work; d=$(ls -t checks | head -1); sed -n 1,60p checks/$d/lean.log"
}
Bash output
/work/checks/dcd7a1b461ab4ea1832bd51aed75b96c/Checked.lean:173:78: error: unsolved goals
r M mw n Phi ms mwater : ℝ
hr1 : 2e-2 ≤ r
hr2 : r ≤ 5e-2
hM : M = 58.44
hmw : mw = 0.998
hn : n = 1000 * mw * r / M
hP1 : -41 ≤ Phi
hP2 : Phi ≤ -27
hms : 0 < ms
hmwa : 0 < mwater
hn0 : 0 < n
hn1 : n ≤ 0.8540
⊢ mwater + ms = ms + mwater
'answer_theorem' depends on axioms: [propext, Classical.choice, Quot.sound]
'cs_theorem' depends on axioms: [propext, sorryAx, Classical.choice, Quot.sound]
VAS_GAP cs_theorem used=true : (r : ℝ) (M : ℝ) (mw : ℝ) (n : ℝ) (Phi : ℝ) (ms : ℝ) (mwater : ℝ) (hr1 : 2e-2 ≤
  r) (hr2 : r ≤
  5e-2) (hM : M =
  58.44) (hmw : mw =
  0.998) (hn : n =
  1000 * mw * r /
    M) (hP1 : -41 ≤
  Phi) (hP2 : Phi ≤
  -27) (hms : 0 <
  ms) (hmwa : 0 <
  mwater) (hn0 : 0 <
  n := Eq.mpr (id (congrArg (fun _a => 0 < _a) hn))
  (Eq.mpr (id (congrArg (fun _a => 0 < 1000 * mw * r / _a) hM))
    (Eq.mpr (id (congrArg (fun _a => 0 < 1000 * _a * r / 58.44) hmw))
      (div_pos
        (mul_pos
          (mul_pos
            (Mathlib.Meta.Positivity.pos_of_isNat (Mathlib.Meta.NormNum.isNat_ofNat ℝ (Eq.refl 1000))
              (Eq.refl (Nat.ble 1 1000)))
            (Mathlib.Meta.Positivity.pos_of_isNNRat
              (Mathlib.Meta.NormNum.isNNRat_ofScientific_of_true
                (Mathlib.Meta.NormNum.isNNRat_nnratCast
                  (Mathlib.Meta.NormNum.isNNRat_divNat (Mathlib.Meta.NormNum.IsNat.raw_refl 998)
                    (Mathlib.Meta.NormNum.isNat_pow (Eq.refl HPow.hPow)
                      (Mathlib.Meta.NormNum.isNat_ofNat ℕ (Eq.refl 10)) (Mathlib.Meta.NormNum.IsNat.raw_refl 3)
                      (Mathlib.Meta.NormNum.IsNatPowT.run Mathlib.Meta.NormNum.IsNatPowT.bit1))
                    (Mathlib.Meta.NormNum.isNNRat_div
                      (Mathlib.Meta.NormNum.isNNRat_mul (Eq.refl HMul.hMul)
                        (Mathlib.Meta.NormNum.IsNat.to_isNNRat
                          (Mathlib.Meta.NormNum.isNat_natCast 998 998 (Mathlib.Meta.NormNum.IsNat.raw_refl 998)))
                        (Mathlib.Meta.NormNum.isNNRat_inv_pos
                          (Mathlib.Meta.NormNum.IsNat.to_isNNRat
                            (Mathlib.Meta.NormNum.isNat_natCast 1000 1000 (Mathlib.Meta.NormNum.IsNat.raw_refl 1000))))
                        (Eq.refl (Nat.mul 998 1)) (Eq.refl 1000))))))
              (Eq.refl (decide (0 < 499)))))
          (lt_of_lt_of_le
            (Mathlib.Meta.Positivity.pos_of_isNNRat
              (Mathlib.Meta.NormNum.isNNRat_ofScientific_of_true
                (Mathlib.Meta.NormNum.isNNRat_nnratCast
                  (Mathlib.Meta.NormNum.isNNRat_divNat (Mathlib.Meta.NormNum.IsNat.raw_refl 2)
                    (Mathlib.Meta.NormNum.isNat_pow (Eq.refl HPow.hPow)
                      (Mathlib.Meta.NormNum.isNat_ofNat ℕ (Eq.refl 10)) (Mathlib.Meta.NormNum.IsNat.raw_refl 2)
                      (Mathlib.Meta.NormNum.IsNatPowT.run Mathlib.Meta.NormNum.IsNatPowT.bit0))
                    (Mathlib.Meta.NormNum.isNNRat_div
Bash input
{
  "command": "cd /work; python3 -I - <<'E'\ns=open('Model.lean').read()\ns=s.replace(\"  refine ⟨⟨by nlinarith, by nlinarith⟩, ?_⟩\\n  field_simp\\n\",\"  refine ⟨⟨by nlinarith, by nlinarith⟩, ?_⟩\\n  field_simp\\n  ring\\n\")\nopen('Model.lean','w').write(s)\nE\npython /harness/src/vcs_protocol.py tool check --manifest /work/candidate.json 2>&1 | tail -12"
}
Bash output
 "extracted_file": "/work/checks/0a061e67c7b843d9a544c0247cdccce7/extracted.lean",
 "errors": []
}
{
  "ok": false,
  "check_complete": false,
  "reviewable": false,
  "verdict": "not_proved",
  "time": "2026-11-17T16:06:37.915241+00:00",
  "lean_exit": 0,
  "error": "independent extraction failed"
}
Bash input
{
  "command": "cd /work; python /harness/src/vcs_protocol.py tool check --manifest /work/candidate.json 2>&1 | head -40; d=checks/0a061e67c7b843d9a544c0247cdccce7; ls $d; cat $d/verdict.json | head -40"
}
Bash output
{
 "file": "/work/checks/d34803db1d874cbd871ed1ec8eedf419/submitted.lean",
 "conclusions": [
  "answer_theorem",
  "cs_theorem"
 ],
 "lean_exit": 0,
 "ok": false,
 "compiles_alone": true,
 "faithful": {
  "answer_theorem": false,
  "cs_theorem": false
 },
 "assumption_declarations": [
  "Fw",
  "Fs",
  "boilTime"
 ],
 "identity_method": "kernel-expression-repr-v1",
 "conclusion_identities": {
  "answer_theorem": {
   "original_sha256": "8ccf5805235174bb9f1d74b5064497a99bc7c87265e55134c7a1c2053821af18",
   "extracted_sha256": "b037e76b76befb73aa0724c086f7257fc33bd6303e17f6f4ac498be94af9b342"
  },
  "cs_theorem": {
   "original_sha256": "50a447e19bc8c7bc2f36bae3ee7b9f92e9fa7c0df85d8f80fc41e9bb9523b368",
   "extracted_sha256": "e3f4c0156df9b96b85baa1f0e72321ddf69eb3dfe5764b4b56b29e644cbaedd4"
  }
 },
 "dependency_faithful": {
  "boilTime": true,
  "Fw": true,
  "Fs": true
 },
 "dependency_identities": {
  "boilTime": {
   "original_sha256": "6198b94851e6843b80d39678b139232cd6effb3d2f1fe9a630bbbdbae772e6c0",
   "extracted_sha256": "6198b94851e6843b80d39678b139232cd6effb3d2f1fe9a630bbbdbae772e6c0"
  },
  "Fw": {
Checked.lean
extracted.json
extracted.lean
lean.log
submitted-candidate.json
submitted-report.txt
submitted.lean
verdict.json
{
  "ok": false,
  "check_complete": false,
  "reviewable": false,
  "verdict": "not_proved",
  "time": "2026-11-17T16:06:37.915241+00:00",
  "lean_exit": 0,
  "error": "independent extraction failed"
}
Bash input
{
  "command": "cd /work; d=checks/d34803db1d874cbd871ed1ec8eedf419; ls $d; grep -n \"cs_theorem\" -B2 -A12 $d/extracted.lean | head -60; grep -n -i \"error\" -A6 $d/*.log | head -30"
}
Bash output
Checked.lean
extracted.json
extracted.lean
lean.log
submitted-candidate.json
submitted-report.txt
submitted.lean
verdict.json
17-/-! Conclusions (statements of the proved theorems). -/
18-def conclusion_answer_theorem : Prop := ∀ (Cw P h Tr T0 Tb r M mw n Phi hsol : ℝ), Tr = 20 → T0 = 20 → 100 ≤ Tb → Tb ≤ 101 → 4170 ≤ Cw → 0 < h → h*(Tb-Tr) < P → 0.02 ≤ r → r ≤ 0.05 → M = 58.44 → mw = 0.998 → n = 1000*mw*r/M → -41 ≤ Phi → Phi ≤ -27 → 3800 ≤ hsol → ∀ Ta : ℝ, T0 ≤ Ta → Ta ≤ Tb → boilTime Cw (n*Phi) (n*hsol) P h Tr T0 Tb T0 ≤ boilTime Cw (n*Phi) (n*hsol) P h Tr T0 Tb Ta
19:def conclusion_cs_theorem : Prop := ∀ (r M mw n Phi ms mwater : ℝ), 0.02 ≤ r → r ≤ 0.05 → M = 58.44 → mw = 0.998 → n = 1000*mw*r/M → -41 ≤ Phi → Phi ≤ -27 → 0 < ms → 0 < mwater → (n*Phi < 0 ∧ -36 ≤ n*Phi) ∧ ms/(ms+mwater) = (ms/mwater)/(1+ms/mwater)
Bash input
{
  "command": "cd /work; d=checks/d34803db1d874cbd871ed1ec8eedf419; sed -n 40,200p $d/verdict.json | grep -v sha256 | head -40; cat $d/extracted.json | head -30; sed -n 1,16p $d/extracted.lean"
}
Bash output
{
 "file": "/work/checks/d34803db1d874cbd871ed1ec8eedf419/submitted.lean",
 "conclusions": [
  "answer_theorem",
  "cs_theorem"
 ],
 "lean_exit": 0,
 "ok": false,
 "compiles_alone": true,
 "faithful": {
  "answer_theorem": false,
  "cs_theorem": false
 },
 "assumption_declarations": [
  "Fw",
  "Fs",
  "boilTime"
 ],
 "identity_method": "kernel-expression-repr-v1",
 "conclusion_identities": {
  "answer_theorem": {
   "original_sha256": "8ccf5805235174bb9f1d74b5064497a99bc7c87265e55134c7a1c2053821af18",
   "extracted_sha256": "b037e76b76befb73aa0724c086f7257fc33bd6303e17f6f4ac498be94af9b342"
  },
  "cs_theorem": {
   "original_sha256": "50a447e19bc8c7bc2f36bae3ee7b9f92e9fa7c0df85d8f80fc41e9bb9523b368",
   "extracted_sha256": "e3f4c0156df9b96b85baa1f0e72321ddf69eb3dfe5764b4b56b29e644cbaedd4"
  }
 },
 "dependency_faithful": {
import Mathlib.Analysis.SpecialFunctions.Log.Deriv
import Mathlib.Analysis.Calculus.MeanValue

/-- time-integral of dH/(P - loss) for pure water (antiderivative, h>0) -/
noncomputable def Fw (Cw P h H : ℝ) : ℝ := -(Cw/h) * Real.log (P - h*(H/Cw))

/-- same for the salt solution -/
noncomputable def Fs (Cw Cs D P h H : ℝ) : ℝ := -((Cw+Cs)/h) * Real.log (P - h*((H-D)/(Cw+Cs)))

/-- Total time to reach Tb from T0 when the salt is added once the water is at Ta:
 heating water alone from T0 to Ta, then solution from the post-mixing state up to Tb. -/
noncomputable def boilTime (Cw Cs D P h Tr T0 Tb Ta : ℝ) : ℝ :=
  (Fw Cw P h (Cw*(Ta-Tr)) - Fw Cw P h (Cw*(T0-Tr))) +
  (Fs Cw Cs D P h ((Cw+Cs)*(Tb-Tr)+D) - Fs Cw Cs D P h (Cw*(Ta-Tr)))
Bash input
{
  "command": "cd /work; cp Model.lean /tmp/Model_dec.lean; python3 -I - <<'E'\ns=open('Model.lean').read()\ns=s.replace('0.02 ≤ r','(1/50) ≤ r').replace('r ≤ 0.05','r ≤ (1/20)').replace('M = 58.44','M = 1461/25').replace('mw = 0.998','mw = 499/500')\ns=s.replace('n ≤ 0.8540','n ≤ 8540/10000')\nopen('Model.lean','w').write(s)\nE\ngrep -n \"0\\.0\\|58.44\\|0.998\" Model.lean | grep -v \"^.*/-\" | head; python /harness/src/vcs_protocol.py tool check --manifest /work/candidate.json 2>&1 | grep -E \"faithful|ok|error|verdict|answer|cs_\" | head"
}
Bash output
154: (density 0.9982 kg/L).  The salt/water ratio r = (salt mass)/(water mass) (NOT the mass fraction of the
155: final solution; the fraction is w = r/(1+r), proved in `cs_theorem`).  Regime: 0.02 <= r <= 0.05,
163:   * secant-mean Phi in [-41, -27] J/K/mol for r in [0.02, 0.05] (computed: -40.7 at r=0.02 ... -27.9 at 0.05);
166:   * pot+water heat capacity Cw >= 4170 J/K (at least the 0.998 kg of water, 4.18 J/g/K).
184: (r in [0.02,0.05], Cs = n*Phi, D = n*hsol, Tb in [100,101], stove stronger than losses at Tb), adding the
  "answer_theorem",
  "cs_theorem"
 "ok": false,
 "faithful": {
  "answer_theorem": false,
  "cs_theorem": false
  "answer_theorem": {
  "cs_theorem": {
 "dependency_faithful": {
  "answer_theorem": "\u2200 (Cw P h Tr T0 Tb r M mw n Phi hsol : \u211d), Tr = 20 \u2192 T0 = 20 \u2192 100 \u2264 Tb \u2192 Tb \u2264 101 \u2192 4170 \u2264 Cw \u2192 0 < h \u2192 h * (Tb - Tr) < P \u2192 1 / 50 \u2264 r \u2192 r \u2264 1 / 20 \u2192 M = 1461 / 25 \u2192 mw = 499 / 500 \u2192 n = 1000 * mw * r / M \u2192 -41 \u2264 Phi \u2192 Phi \u2264 -27 \u2192 3800 \u2264 hsol \u2192 \u2200 (Ta : \u211d), T0 \u2264 Ta \u2192 Ta \u2264 Tb \u2192 boilTime Cw (n * Phi) (n * hsol) P h Tr T0 Tb T0 \u2264 boilTime Cw (n * Phi) (n * hsol) P h Tr T0 Tb Ta",
Bash input
{
  "command": "cd /work; d=$(ls -td checks/*/ | head -1); python3 -I - $d <<'E'\nimport json,sys\nv=json.load(open(sys.argv[1]+'verdict.json'))\nfor k,x in v.items():\n    if k not in('conclusion_identities','dependency_identities'): print(k, str(x)[:600])\nE\ntail -5 $d/lean.log; diff <(sed -n 1,400p $d/Checked.lean) $d/submitted.lean | head"
}
Bash output
ok False
check_complete False
reviewable False
verdict not_proved
time 2026-11-17T16:08:22.198272+00:00
lean_exit 0
error independent extraction failed
'cs_theorem' depends on axioms: [propext, Classical.choice, Quot.sound]
VAS_LISTED

leanchecker exit 0

216,281d215
< 
< #print axioms answer_theorem
< #print axioms cs_theorem
< 
< /-- true if `sorryAx` occurs anywhere other than as the head of an application that states its goal -/
< private partial def VAS_unstated : Lean.Expr → Bool
<   | e@(.app ..) =>
<     let f := e.getAppFn
<     (if f.isConstOf ``sorryAx then false else VAS_unstated f) || e.getAppArgs.any VAS_unstated
Bash input
{
  "command": "cd /work; python /harness/src/vcs_protocol.py tool check --manifest /work/candidate.json 2>&1 | python3 -c \"\nimport sys;t=sys.stdin.read();print(t[:200]);i=t.find('dependency_identities');print(t[i:i+3000])\""
}
Bash output
{
 "file": "/work/checks/0e26c0d1cd2a4d3e931493e00c8252b4/submitted.lean",
 "conclusions": [
  "answer_theorem",
  "cs_theorem"
 ],
 "lean_exit": 0,
 "ok": false,
 "compiles_alone": true,
 "faithful":
dependency_identities": {
  "boilTime": {
   "original_sha256": "6198b94851e6843b80d39678b139232cd6effb3d2f1fe9a630bbbdbae772e6c0",
   "extracted_sha256": "6198b94851e6843b80d39678b139232cd6effb3d2f1fe9a630bbbdbae772e6c0"
  },
  "Fw": {
   "original_sha256": "38b9048abbc880c8b91005bb57e36daee61676680cf2ea28b79e449817077112",
   "extracted_sha256": "38b9048abbc880c8b91005bb57e36daee61676680cf2ea28b79e449817077112"
  },
  "Fs": {
   "original_sha256": "7428e34b8a70d77e92148994cb5f9813e0ae0c7e71b7041de8d055570276aaab",
   "extracted_sha256": "7428e34b8a70d77e92148994cb5f9813e0ae0c7e71b7041de8d055570276aaab"
  }
 },
 "unranged": [],
 "added_assumptions": [],
 "conclusions_as_proved": {
  "answer_theorem": "\u2200 (Cw P h Tr T0 Tb r M mw n Phi hsol : \u211d), Tr = 20 \u2192 T0 = 20 \u2192 100 \u2264 Tb \u2192 Tb \u2264 101 \u2192 4170 \u2264 Cw \u2192 0 < h \u2192 h * (Tb - Tr) < P \u2192 1 / 50 \u2264 r \u2192 r \u2264 1 / 20 \u2192 M = 1461 / 25 \u2192 mw = 499 / 500 \u2192 n = 1000 * mw * r / M \u2192 -41 \u2264 Phi \u2192 Phi \u2264 -27 \u2192 3800 \u2264 hsol \u2192 \u2200 (Ta : \u211d), T0 \u2264 Ta \u2192 Ta \u2264 Tb \u2192 boilTime Cw (n * Phi) (n * hsol) P h Tr T0 Tb T0 \u2264 boilTime Cw (n * Phi) (n * hsol) P h Tr T0 Tb Ta",
  "cs_theorem": "\u2200 (r M mw n Phi ms mwater : \u211d), 1 / 50 \u2264 r \u2192 r \u2264 1 / 20 \u2192 M = 1461 / 25 \u2192 mw = 499 / 500 \u2192 n = 1000 * mw * r / M \u2192 -41 \u2264 Phi \u2192 Phi \u2264 -27 \u2192 0 < ms \u2192 0 < mwater \u2192 (n * Phi < 0 \u2227 -36 \u2264 n * Phi) \u2227 ms / (ms + mwater) = ms / mwater / (1 + ms / mwater)"
 },
 "extracted_file": "/work/checks/0e26c0d1cd2a4d3e931493e00c8252b4/extracted.lean",
 "errors": []
}
{
  "ok": false,
  "check_complete": false,
  "reviewable": false,
  "verdict": "not_proved",
  "time": "2026-11-17T16:09:09.235176+00:00",
  "lean_exit": 0,
  "error": "independent extraction failed"
}
Bash input
{
  "command": "cd /work; d=$(ls -td checks/*/ | head -1); python3 -I - $d <<'E'\nimport json,sys\nv=json.load(open(sys.argv[1]+'verdict.json'))\nprint(v)\nE\ngrep -n \"lake\\|lean \" /harness/src/vcs_protocol.py | head -20"
}
Bash output
{'ok': False, 'check_complete': False, 'reviewable': False, 'verdict': 'not_proved', 'time': '2026-11-17T16:09:09.235176+00:00', 'lean_exit': 0, 'error': 'independent extraction failed'}
136:    from vcs_lean import strip_comments, validate
153:    from vcs_lean import ALLOWED_AXIOMS, LISTER, listed
304:    lean = Path(os.environ.get("ELAN_HOME", str(Path.home() / ".elan"))) / "toolchains" / toolchain_name
323:    for path in [lean / "bin/lean", lean / "bin/leanchecker", *required]:
331:    libraries = sorted(Path(library_root).resolve().glob(".lake/packages/*/.lake/build/lib/lean"))
334:    if (Path(library_root) / "lake-manifest.json").read_bytes() != (ROOT / "lake-manifest.json").read_bytes():
342:              "lean_version": subprocess.check_output([str(lean / "bin/lean"), "--version"], text=True).strip()}
406:        cmd += mount(lib, f"/harness/.lake/packages/{package}/.lake/build/lib/lean")
474:    provenance["library_manifest_sha256"] = digest(ROOT / "lake-manifest.json")
Bash input
{
  "command": "cd /work; cp Model.lean /tmp/Model_frac.lean; python3 -I - <<'E'\ns=open('Model.lean').read()\ns=s.replace('-41 ≤ Phi','0 ≤ Phi + 41').replace('Phi ≤ -27','Phi + 27 ≤ 0').replace('-36 ≤ n*Phi','0 ≤ n*Phi + 36')\nopen('Model.lean','w').write(s)\nE\npython /harness/src/vcs_protocol.py tool check --manifest /work/candidate.json 2>&1 | grep -A3 -E '\"faithful\"|\"error\"'"
}
Bash output
 "faithful": {
  "answer_theorem": false,
  "cs_theorem": false
 },
--
  "error": "independent extraction failed"
}
Bash input
{
  "command": "cd /work; cp /tmp/Model_frac.lean Model.lean; python3 -I - <<'E'\ns=open('Model.lean').read()\ni=s.index('theorem cs_theorem')\na,b=s[:i],s[i:]\nb=b.replace('(r M mw n Phi ms mwater : ℝ)','(r n Phi ms mwater : ℝ)').replace('M = 1461/25 → mw = 499/500 → n = 1000*mw*r/M →','n = 1000*(499/500)*r/(1461/25) →')\nb=b.replace('(Cw P h Tr T0 Tb r M mw n Phi hsol : ℝ)','(Cw P h Tb r n Phi hsol : ℝ)').replace('Tr = 20 → T0 = 20 → ','')\nb=b.replace('M = 1461/25 → mw = 499/500 → n = 1000*mw*r/M →','n = 1000*(499/500)*r/(1461/25) →')\nb=b.replace('h*(Tb-Tr) < P','h*(Tb-20) < P').replace('boilTime Cw (n*Phi) (n*hsol) P h Tr T0 Tb T0','boilTime Cw (n*Phi) (n*hsol) P h 20 20 Tb 20').replace('boilTime Cw (n*Phi) (n*hsol) P h Tr T0 Tb Ta','boilTime Cw (n*Phi) (n*hsol) P h 20 20 Tb Ta').replace('T0 ≤ Ta','20 ≤ Ta')\nopen('Model.lean','w').write(a+b)\nE\nsed -n '/theorem cs_theorem/,$p' Model.lean"
}
Bash output
theorem cs_theorem : ∀ (r n Phi ms mwater : ℝ),
    (1/50) ≤ r → r ≤ (1/20) → n = 1000*(499/500)*r/(1461/25) →
    -41 ≤ Phi → Phi ≤ -27 → 0 < ms → 0 < mwater →
    (n*Phi < 0 ∧ -36 ≤ n*Phi) ∧ ms/(ms+mwater) = (ms/mwater)/(1+ms/mwater) := by
  intro r M mw n Phi ms mwater hr1 hr2 hM hmw hn hP1 hP2 hms hmwa
  have hn0 : 0 < n := by rw [hn, hM, hmw]; positivity
  have hn1 : n ≤ 8540/10000 := by
    rw [hn, hM, hmw, div_le_iff₀ (by norm_num)]; nlinarith
  refine ⟨⟨by nlinarith, by nlinarith⟩, ?_⟩
  field_simp
  ring

/-- ANSWER.  Accepted model (lumped heat capacity Cw, constant stove power P, Newton loss h, no evaporation,
 salt at room temperature Tr = T0 = 20 C dissolves instantly, enthalpy conserved).  Under the premises above
 (r in [0.02,0.05], Cs = n*Phi, D = n*hsol, Tb in [100,101], stove stronger than losses at Tb), adding the
 salt right at the start (Ta = T0) is no slower than adding it at any later water temperature Ta in [T0,Tb].
 The condition D + Cs (x-Tr) >= 0 (salt addition cools / does not warm) is CHECKED here, not assumed, and
 the 'stove beats losses' condition for the salt solution is derived from it. -/
theorem answer_theorem : ∀ (Cw P h Tb r n Phi hsol : ℝ),
    100 ≤ Tb → Tb ≤ 101 → 4170 ≤ Cw → 0 < h → h*(Tb-20) < P →
    (1/50) ≤ r → r ≤ (1/20) → n = 1000*(499/500)*r/(1461/25) →
    -41 ≤ Phi → Phi ≤ -27 → 3800 ≤ hsol →
    ∀ Ta : ℝ, 20 ≤ Ta → Ta ≤ Tb →
      boilTime Cw (n*Phi) (n*hsol) P h 20 20 Tb 20 ≤ boilTime Cw (n*Phi) (n*hsol) P h 20 20 Tb Ta := by
  intro Cw P h Tr T0 Tb r M mw n Phi hsol hTr hT0 hb1 hb2 hCw hh hP hr1 hr2 hM hmw hn hP1 hP2 hs Ta h1 h2
  have hn0 : 0 < n := by rw [hn, hM, hmw]; positivity
  have hn1 : n ≤ 8540/10000 := by
    rw [hn, hM, hmw, div_le_iff₀ (by norm_num)]; nlinarith
  have hCs : n*Phi < 0 := by nlinarith
  have hCs2 : -36 ≤ n*Phi := by nlinarith
  have hCw0 : 0 < Cw := by linarith
  have hC : 0 < Cw + n*Phi := by linarith
  have hd : 0 ≤ Tb - Tr := by linarith
  have hd2 : Tb - Tr ≤ 81 := by linarith
  -- enthalpy of solution at the boiling point is still >= 0 (checked)
  have hE0 : 0 ≤ hsol + Phi*(Tb-Tr) := by nlinarith [mul_nonneg (by linarith : 0 ≤ Phi + 41) hd]
  have hE : 0 ≤ n*hsol + n*Phi*(Tb-Tr) := by nlinarith [mul_nonneg hn0.le hE0]
  have hstove : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-n*hsol)/(Cw+n*Phi)) := by
    intro H hH
    have : (H-n*hsol)/(Cw+n*Phi) ≤ Tb - Tr := by
      rw [div_le_iff₀ hC]; nlinarith [hH.2]
    nlinarith
  refine (compare_theorem Cw (n*Phi) (n*hsol) P h Tr T0 Tb hCw0 hC hh hP (by linarith) hstove
    T0 Ta le_rfl h1 h2).1 ?_
  intro x hx1 hx2
  nlinarith [mul_nonneg (by linarith : 0 ≤ Tb - x) (by linarith : 0 ≤ -(n*Phi))]
Bash input
{
  "command": "cd /work; cp /tmp/Model_frac.lean Model.lean; mkdir -p /tmp/t; cat > t1.lean <<'E'\nimport Mathlib.Analysis.SpecialFunctions.Log.Deriv\n\ntheorem t1 : ∀ (r : ℝ), (1/50) ≤ r → 0 < r := by\n  intro r h; linarith\n\ntheorem t2 : ∀ (r n : ℝ), n = 1000*(499/500)*r/(1461/25) → -41 ≤ n → n ≤ -27 → n < 0 := by\n  intro r n h a b; linarith\n\ntheorem t3 : ∀ (r M n : ℝ), M = 5 → n = 1000*r/M → 0 ≤ r → 0 ≤ n := by\n  intro r M n h1 h2 h3; rw [h2, h1]; positivity\nE\ncat > ct.json <<'E'\n{\"proof_file\":\"t1.lean\",\"narrative_file\":\"report.txt\",\"conclusions\":{\"a\":\"t1\",\"b\":\"t2\",\"c\":\"t3\"},\"user_facts\":[\"x\"],\"model_choices\":[\"x\"],\"sources\":[\"x\"],\"incorporated_inputs\":[],\"proof_scope\":\"test\"}\nE\npython /harness/src/vcs_protocol.py tool check --manifest /work/ct.json 2>&1 | grep -A5 -E '\"faithful\"|\"error\"'"
}
Bash output
 "faithful": {
  "t1": false,
  "t2": false,
  "t3": false
 },
 "assumption_declarations": [],
--
  "error": "independent extraction failed"
}
Bash input
{
  "command": "cd /work; d=$(ls -td checks/*/ | head -1); cat $d/extracted.lean; ls $d; grep -v sha256 $d/lean.log | head -20"
}
Bash output
import Mathlib.Analysis.SpecialFunctions.Log.Deriv



/-! Conclusions (statements of the proved theorems). -/
def conclusion_t1 : Prop := ∀ (r : ℝ), (1/50) ≤ r → 0 < r
def conclusion_t2 : Prop := ∀ (r n : ℝ), n = 1000*(499/500)*r/(1461/25) → -41 ≤ n → n ≤ -27 → n < 0
def conclusion_t3 : Prop := ∀ (r M n : ℝ), M = 5 → n = 1000*r/M → 0 ≤ r → 0 ≤ n
Checked.lean
extracted.json
extracted.lean
lean.log
submitted-candidate.json
submitted-report.txt
submitted.lean
verdict.json
't1' depends on axioms: [propext, Classical.choice, Quot.sound]
't2' depends on axioms: [propext, Classical.choice, Quot.sound]
't3' depends on axioms: [propext, Classical.choice, Quot.sound]
VAS_LISTED

leanchecker exit 0
Bash input
{
  "command": "cd /harness/src; ls; grep -n \"kernel-expression-repr\" -r . | head; "
}
Bash output
vcs_extract.py
vcs_lean.py
vcs_protocol.py
./vcs_extract.py:245:               "identity_method": "kernel-expression-repr-v1", "conclusion_identities": identity_hashes,
Shell cwd was reset to /work
Bash input
{
  "command": "cd /harness/src; sed -n 150,260p vcs_extract.py"
}
Bash output
        body = body[:body.index(head)]
    probe.write_text(body + "\nset_option format.width 100000\n" + IDENTITY + REACH.replace("TARGETS", targets)
                     + LISTER.replace("ROOTS", targets))
    status, text = run_lean(probe)
    report = {"file": lean_file, "conclusions": list(theorems), "lean_exit": status}
    if status != 0 or "VAS_REACHED" not in text:
        report |= {"ok": False, "error": "the source did not compile with the extractor",
                   "errors": [ln for ln in text.splitlines() if ": error" in ln][:5]}
        print(json.dumps(report, indent=1))
        sys.exit(1)
    decls = {}
    for name, a, b in re.findall(r"VAS_DECL (\S+) (\d+) (\d+)", text):
        decls[name] = (int(a), int(b))
    unranged = re.findall(r"VAS_UNRANGED (\S+)", text)
    concl = dict(re.findall(r"VAS_CONCLUSION (\S+) : (.*?) VAS_END", text, re.S))
    original_identities = expression_identities(text, "VAS_ORIGINAL_IDENTITY")
    dependency_identities = expression_identities(text, "VAS_DEPENDENCY_IDENTITY")
    added = [a for a in listed(text) or [] if a["used"]]
    # the extracted file: imports, then each assumption's source in file order, inside its namespace. A range
    # inside another one (a structure's fields and constructor) is part of it and is not copied twice.
    imports = [ln for ln in lines if ln.startswith("import ")]
    spans = sorted(set(decls.values()))
    keep = [s for s in spans if not any(o != s and o[0] <= s[0] and s[1] <= o[1] for o in spans)]
    for x, y in zip(keep, keep[1:]):
        if y[0] <= x[1]:
            raise ValueError(f"overlapping declaration ranges {x} and {y}")
    owner = {}
    for name, rng in decls.items():
        if rng in keep and (rng not in owner or len(name) < len(owner[rng])):
            owner[rng] = name
    head = re.compile(r"^\s*(?:@\[[^\]]*\]\s*)?(?:noncomputable\s+|private\s+|protected\s+)*"
                      r"(?:def|theorem|lemma|abbrev|structure|inductive|class|axiom|instance|opaque)\s+([^\s:({\[]+)")
    # top-level `open X` lines of the source go along, in file order, so names resolve as they did there
    opens = [(i + 1, i + 1) for i, ln in enumerate(lines)
             if re.match(r"open\s+[\w.]+(\s+[\w.]+)*\s*$", ln) and not any(a <= i + 1 <= b for a, b in keep)]
    parts, current = [], None
    for rng in sorted(keep + opens):
        a, b = rng
        if rng in opens and rng not in owner:
            if current:
                parts.append(f"end {current}\n")
                current = None
            parts.append(lines[a - 1])
            continue
        name = owner[rng]
        short = next((m.group(1) for ln in lines[a - 1:b] if (m := head.match(ln))), None)
        if short is None or not (name == short or name.endswith("." + short)):
            raise ValueError(f"cannot match the source of {name} at lines {a}-{b}")
        ns = name[: -len(short) - 1] if name != short else ""
        if ns != current:
            if current:
                parts.append(f"end {current}\n")
            if ns:
                parts.append(f"namespace {ns}")
            current = ns
        parts.append("\n".join(lines[a - 1:b]) + "\n")
    if current:
        parts.append(f"end {current}\n")
    starts = {}
    for t in theorems:
        m = re.search(r"(?m)^\s*(?:theorem|lemma)\s+" + re.escape(t) + r"\b", src)
        starts[t] = src[:m.start()].count("\n") + 1 if m else None
    stmts = {t: statement_text(lines, starts[t], t) for t in theorems}
    conclusions = "".join(f"def conclusion_{t} : Prop := {stmts[t]}\n" for t in theorems)
    extracted = ("\n".join(imports) + "\n\n" + "\n".join(parts) + "\n\n"
                 "/-! Conclusions (statements of the proved theorems). -/\n" + conclusions)
    out.with_suffix(".lean").write_text(extracted)
    # step 5: the extracted file compiles alone, and its conclusions are the proved statements
    check = out.with_name(out.name + ".check.lean")
    names = "[" + ", ".join(f"``conclusion_{t}" for t in theorems) + "]"
    dependencies = "[" + ", ".join(f"``{name}" for name in dependency_identities) + "]"
    check.write_text(extracted + IDENTITY + COMPARE.replace("NAMES", names).replace("DEPENDENCIES", dependencies))
    status2, text2 = run_lean(check)
    extracted_identities = expression_identities(text2, "VAS_EXTRACTED_IDENTITY")
    same = {t: bool(original_identities.get(t)) and
            original_identities[t] == extracted_identities.get("conclusion_" + t)
            for t in theorems}
    extracted_dependencies = expression_identities(text2, "VAS_EXTRACTED_DEPENDENCY_IDENTITY")
    dependencies_same = {name: value == extracted_dependencies.get(name)
                         for name, value in dependency_identities.items()}
    identity_hashes = {
        t: {"original_sha256": hashlib.sha256(original_identities[t].encode()).hexdigest()
            if t in original_identities else None,
            "extracted_sha256": hashlib.sha256(extracted_identities["conclusion_" + t].encode()).hexdigest()
            if "conclusion_" + t in extracted_identities else None}
        for t in theorems}
    dependency_hashes = {
        name: {"original_sha256": hashlib.sha256(value.encode()).hexdigest(),
               "extracted_sha256": hashlib.sha256(extracted_dependencies[name].encode()).hexdigest()
               if name in extracted_dependencies else None}
        for name, value in dependency_identities.items()}
    report |= {"ok": status2 == 0 and all(same.values()) and all(dependencies_same.values())
               and set(dependency_identities) == set(extracted_dependencies) and not unranged,
               "compiles_alone": status2 == 0,
               "faithful": same, "assumption_declarations": [owner[r] for r in keep],
               "identity_method": "kernel-expression-repr-v1", "conclusion_identities": identity_hashes,
               "dependency_faithful": dependencies_same, "dependency_identities": dependency_hashes,
               "unranged": unranged, "added_assumptions": added,
               "conclusions_as_proved": {t: " ".join(concl[t].split()) for t in theorems},
               "extracted_file": str(out.with_suffix(".lean")),
               "errors": [ln for ln in text2.splitlines() if ": error" in ln][:5]}
    out.with_suffix(".json").write_text(json.dumps(report, indent=1))
    probe.unlink()
    check.unlink()
    print(json.dumps(report, indent=1))
    sys.exit(0 if report["ok"] else 1)


if __name__ == "__main__":
    main()
Shell cwd was reset to /work
Bash input
{
  "command": "cd /work; cat > t1.lean <<'E'\nimport Mathlib.Analysis.SpecialFunctions.Log.Deriv\n\ntheorem t1 : ∀ (r : ℝ), 0 ≤ r → 0 ≤ r*r := by\n  intro r h; positivity\n\ntheorem t2 : ∀ (r : ℝ), 2 ≤ r → 0 < r := by\n  intro r h; linarith\n\ntheorem t3 : ∀ (r : ℝ), 1 ≤ r → 0 < r := by\n  intro r h; linarith\n\ntheorem t4 : ∀ (r : ℝ), r ≤ 1/2 → r < 1 := by\n  intro r h; linarith\nE\nsed -i 's/\"a\":\"t1\",\"b\":\"t2\",\"c\":\"t3\"/\"a\":\"t1\",\"b\":\"t2\",\"c\":\"t3\",\"d\":\"t4\"/' ct.json\npython /harness/src/vcs_protocol.py tool check --manifest /work/ct.json 2>&1 | grep -A6 -E '\"faithful\"'"
}
Bash output
 "faithful": {
  "t1": true,
  "t2": false,
  "t3": true,
  "t4": false
 },
 "assumption_declarations": [],
Bash input
{
  "command": "cd /work; cat > t1.lean <<'E'\nimport Mathlib.Analysis.SpecialFunctions.Log.Deriv\n\ntheorem t2 : ∀ (r : ℝ), (2:ℝ) ≤ r → 0 < r := by\n  intro r h; linarith\n\ntheorem t4 : ∀ (r : ℝ), r ≤ (1:ℝ)/2 → r < 1 := by\n  intro r h; linarith\n\ntheorem t5 : ∀ (r : ℝ), r ≤ 3 → r < 4 := by\n  intro r h; linarith\nE\nsed -i 's/\"a\":\"t1\",\"b\":\"t2\",\"c\":\"t3\",\"d\":\"t4\"/\"a\":\"t2\",\"b\":\"t4\",\"c\":\"t5\"/' ct.json\npython /harness/src/vcs_protocol.py tool check --manifest /work/ct.json 2>&1 | grep -A6 -E '\"faithful\"'"
}
Bash output
 "faithful": {
  "t2": false,
  "t4": false,
  "t5": false
 },
 "assumption_declarations": [],
 "identity_method": "kernel-expression-repr-v1",
Bash input
{
  "command": "cd /harness/src; sed -n 1,130p vcs_extract.py"
}
Bash output
"""Extract statement dependencies and conclusions; independently compile and compare."""
import hashlib
import json
import os
from pathlib import Path
import re
import subprocess
import tempfile
import sys

import click

ROOT = Path(__file__).resolve().parents[1]
from vcs_lean import LEAN, LEANCHECKER, LISTER, listed  # noqa: E402

# Display text omits implicit arguments, types and sometimes whole subterms.
# Compare the complete kernel expressions instead. Binder names and metadata do
# not affect meaning; remove them so source positions and fresh binder names do
# not make an otherwise identical extraction fail. All constants, universe
# levels, implicit arguments and binder information remain in the identity.
IDENTITY = """
private partial def VAS_identityExpr : Lean.Expr → Lean.Expr
  | .mdata _ value => VAS_identityExpr value
  | .app fn arg => .app (VAS_identityExpr fn) (VAS_identityExpr arg)
  | .lam _ typ body info => .lam .anonymous (VAS_identityExpr typ) (VAS_identityExpr body) info
  | .forallE _ typ body info => .forallE .anonymous (VAS_identityExpr typ) (VAS_identityExpr body) info
  | .letE _ typ value body flag => .letE .anonymous (VAS_identityExpr typ) (VAS_identityExpr value) (VAS_identityExpr body) flag
  | .proj owner idx value => .proj owner idx (VAS_identityExpr value)
  | value => value

private def VAS_declarationIdentity (ci : Lean.ConstantInfo) : String :=
  let kind := match ci with
    | .axiomInfo _ => "axiom"
    | .defnInfo _ => "definition"
    | .thmInfo _ => "theorem"
    | .opaqueInfo _ => "opaque"
    | .quotInfo _ => "quotient"
    | .inductInfo info => s!"inductive {info.numParams} {info.numIndices} {reprStr info.ctors}"
    | .ctorInfo info => s!"constructor {info.induct} {info.cidx} {info.numParams} {info.numFields}"
    | .recInfo info => s!"recursor {info.numParams} {info.numIndices} {info.numMotives} {info.numMinors}"
  let value := match ci.value? with
    | some body => reprStr (VAS_identityExpr body)
    | none => "no value"
  s!"{kind}|levels={reprStr ci.levelParams}|type={reprStr (VAS_identityExpr ci.type)}|value={value}"
"""

REACH = """
open Lean Elab Command Meta in
run_cmd do
  let kenv := (← getEnv).checked.get
  let localSet : NameSet := kenv.constants.map₂.toList.foldl (fun s (n, _) => s.insert n) {}
  -- statements only: from each conclusion's type, through definitions' types and values
  let mut seen : NameSet := {}
  let mut todo : List Name := []
  for t in TARGETS do
    let some ci := kenv.find? t | throwError m!"no theorem {t}"
    todo := ci.type.getUsedConstants.toList ++ todo
    logInfo m!"VAS_CONCLUSION {t} : {← liftTermElabM <| ppExpr ci.type} VAS_END"
    logInfo m!"VAS_ORIGINAL_IDENTITY {t} : {reprStr (VAS_identityExpr ci.type)} VAS_END"
  while !todo.isEmpty do
    let n := todo.head!
    todo := todo.tail!
    if seen.contains n || !localSet.contains n then continue
    seen := seen.insert n
    let some ci := kenv.find? n | continue
    logInfo m!"VAS_DEPENDENCY_IDENTITY {n} : {VAS_declarationIdentity ci} VAS_END"
    let more := ci.type.getUsedConstants ++ (match ci with
      | .defnInfo v => v.value.getUsedConstants | .thmInfo v => v.value.getUsedConstants
      | .opaqueInfo v => v.value.getUsedConstants
      | .inductInfo v => v.ctors.toArray | .ctorInfo v => #[v.induct] | _ => #[])
    todo := more.toList ++ todo
    -- auxiliary constants (matchers, constructors, projections, equation lemmas) map to the nearest ancestor
    -- whose source range Lean recorded
    let mut m := n
    let mut r ← findDeclarationRanges? m
    while r.isNone && !m.isAnonymous do
      m := m.getPrefix
      r ← findDeclarationRanges? m
    match r with
    | some rg => logInfo m!"VAS_DECL {m} {rg.range.pos.line} {rg.range.endPos.line}"
    | none => logInfo m!"VAS_UNRANGED {n}"
  logInfo "VAS_REACHED"
"""

COMPARE = """
set_option format.width 100000 in
open Lean Elab Command Meta in
run_cmd do
  for n in NAMES do
    let some ci := (← getEnv).find? n | throwError m!"no {n}"
    let some v := ci.value? | throwError m!"no value {n}"
    logInfo m!"VAS_EXTRACTED {n} : {← liftTermElabM <| ppExpr v} VAS_END"
    logInfo m!"VAS_EXTRACTED_IDENTITY {n} : {reprStr (VAS_identityExpr v)} VAS_END"
  for n in DEPENDENCIES do
    let some ci := (← getEnv).find? n | throwError m!"missing extracted dependency {n}"
    logInfo m!"VAS_EXTRACTED_DEPENDENCY_IDENTITY {n} : {VAS_declarationIdentity ci} VAS_END"
"""


def run_lean(path):
    """Lean, then leanchecker on the compiled module (OT-1861): a nonzero status if either fails."""
    libs = sorted((ROOT / ".lake/packages").glob("*/.lake/build/lib/lean"))
    env = dict(os.environ, LEAN_PATH=":".join(map(str, libs)), LEAN_NUM_THREADS="1")
    with tempfile.TemporaryDirectory(prefix="vcs-olean-") as build:
        r = subprocess.run([LEAN, "-j1", f"--root={Path(path).resolve().parent}", "-o", str(Path(build) / "VasExtract.olean"), str(path)], cwd=ROOT,
                           env=env, capture_output=True, text=True, timeout=900)
        if r.returncode != 0:
            return r.returncode, r.stdout + r.stderr
        c = subprocess.run([LEANCHECKER, "VasExtract"], cwd=ROOT, capture_output=True, text=True, timeout=900,
                           env=dict(env, LEAN_PATH=env["LEAN_PATH"] + ":" + build,
                                    PATH=str(Path(LEAN).parent) + ":" + env.get("PATH", "")))
        note = f"\nleanchecker exit {c.returncode}\n{(c.stdout + c.stderr)[-1500:]}\n"
        return (0 if c.returncode == 0 else 10), r.stdout + r.stderr + note


def expression_identities(text, marker):
    """Read complete expression records; duplicate/missing records fail closed."""
    records = re.findall(r"^" + re.escape(marker) + r" (\S+) : (.*?) VAS_END", text, re.M | re.S)
    result = {}
    for name, value in records:
        if name in result:
            raise ValueError(f"duplicate expression identity for {name}")
        result[name] = " ".join(value.split())
    return result


def statement_text(lines, start, name):
    """The source of theorem `name`'s statement: from after `name :` to the `:=` that opens its proof."""
    text = "\n".join(lines[start - 1:])
    m = re.match(r"\s*(?:theorem|lemma)\s+" + re.escape(name) + r"\s*:(.*?):=", text, re.S)
Shell cwd was reset to /work
Bash input
{
  "command": "cd /work/checks; for d in */; do python3 -I - $d <<'E'\nimport json,sys\ntry:\n    v=json.load(open(sys.argv[1]+'extracted.json')); print(sys.argv[1], v.get('faithful'), v.get('ok'))\nexcept Exception as e: print(sys.argv[1],'noext')\nE\ndone; grep -c \"0.02\" 248420fe49ad4124bd13c839d06e22c4/submitted.lean"
}
Bash output
0a061e67c7b843d9a544c0247cdccce7/ {'answer_theorem': False, 'cs_theorem': False} False
0d53881888904eb7b263d141eed5512e/ {'t2': False, 't4': False, 't5': False} False
0e26c0d1cd2a4d3e931493e00c8252b4/ {'answer_theorem': False, 'cs_theorem': False} False
1b0846e151ca49ea869d303e1858edd7/ {'answer_theorem': False, 'cs_theorem': False} False
1be70249055743059f956160d13a001e/ {'answer_theorem': False, 'cs_theorem': False} False
2125411f31dc4bc28190c5bc6bd44d1d/ {'t1': True, 't2': False, 't3': True, 't4': False} False
248420fe49ad4124bd13c839d06e22c4/ {'answer_theorem': True} True
52448c0db62c400e90cbd46e54881cba/ {'answer_theorem': True, 'cs_sign_theorem': True} True
7e4be0bd42b444199e65b96370c18486/ {'answer_theorem': True} True
7f112ae594ee431b9528f1645a3aa6c2/ {'t1': False, 't2': False, 't3': False} False
7fdbd8cda6304fd3ae91fdd97ab702a9/ noext
91bf5367d0354c09a0f1841d6c390708/ {'answer_theorem': True} True
97c02acf02654117a33c008e8abdb8f0/ {'answer_theorem': True, 'cs_sign_theorem': True} True
9d2a29707556410397d3c62a510d59a9/ {'answer_theorem': True} True
b7ec15d94a8146bfadfbd5cc72230a57/ noext
ba6fe61237934f078069bb23df732a18/ noext
d34803db1d874cbd871ed1ec8eedf419/ {'answer_theorem': False, 'cs_theorem': False} False
dcd7a1b461ab4ea1832bd51aed75b96c/ noext
0
Bash input
{
  "command": "cd /work; rm -f t1.lean ct.json; python3 -I - <<'E'\ns=open('/tmp/Model_dec.lean').read()\ni=s.index('/-! ## Salt-specific part')\nhead=s[:i]\nnew='''/-! ## Salt-specific part (NaCl, 1 L of water).  Inputs are labelled; nothing numerical below is a measurement.\n\n Choices.  Salt = NaCl.  1 L of water at 20 C has mass 0.998 kg (density 0.9982 kg/L).  The salt/water ratio\n r = (salt mass)/(water mass) (NOT the mass fraction of the final solution; that is w = r/(1+r), proved in\n `cs_theorem`).  Regime: 0.02 <= r <= 0.05, i.e. about 20-50 g NaCl in the litre (2-4.8 wt%).\n Cs is DERIVED, not fitted: by definition of the apparent molar heat capacity Phi of the salt in solution,\n (total heat capacity of solution) = (heat capacity of the water) + n*Phi, so Cs = n*Phi, n = moles of salt.\n Phi is temperature dependent; the lumped model needs one constant Cs, so Phi is the SECANT MEAN of Phi(T,m)\n over 20 C..Tb, which makes the linear mixing enthalpy D + Cs (T-Tr) exact at both 20 C and Tb.\n Empirical inputs (EXPOSED PREMISES, constants below), from the Laliberte (2009) NaCl correlation (fit range\n 1.5-120 C, w <= 0.26) evaluated numerically outside Lean (a synthetic calculation from a published correlation)\n and from NBS tables: secant-mean Phi in [-41,-27] J/K/mol for r in [0.02,0.05] (computed -40.7 ... -27.9);\n enthalpy of solution of NaCl(s) hsol >= 3800 J/mol (NBS +3.88 kJ/mol); boiling point Tb in [100,101] C;\n pot+water heat capacity >= 4170 J/K (at least the 0.998 kg of water at 4.18 J/g/K).\n D = n*hsol is the enthalpy absorbed when salt at room temperature Tr dissolves at Tr. -/\n\n/-- room / initial water temperature, C -/\ndef Tr0 : ℝ := 20\n/-- admissible range of the boiling point of the salt solution, C -/\ndef TbLo : ℝ := 100\ndef TbHi : ℝ := 101\n/-- minimum heat capacity of pot + water, J/K -/\ndef CwMin : ℝ := 4170\n/-- salt/water mass-ratio regime -/\ndef rLo : ℝ := 1/50\ndef rHi : ℝ := 1/20\n/-- NaCl molar mass g/mol, mass of 1 L of water at 20 C in kg -/\ndef Mnacl : ℝ := 58.44\ndef massWater : ℝ := 0.998\n/-- moles of NaCl for salt/water mass ratio r (1 kg = 1000 g) -/\nnoncomputable def nSalt (r : ℝ) : ℝ := 1000*massWater*r/Mnacl\n/-- empirical bounds of the secant-mean apparent molar heat capacity of NaCl(aq) over 20 C..Tb, J/K/mol -/\ndef PhiLo : ℝ := -41\ndef PhiHi : ℝ := -27\n/-- lower bound of the enthalpy of solution of NaCl(s) at room temperature, J/mol -/\ndef hsolMin : ℝ := 3800\n\nlemma nSalt_pos (r : ℝ) (h : rLo ≤ r) : 0 < nSalt r := by\n  unfold nSalt massWater Mnacl; unfold rLo at h; positivity\n\nlemma nSalt_le (r : ℝ) (h : r ≤ rHi) : nSalt r ≤ 0.8540 := by\n  unfold nSalt massWater Mnacl\n  unfold rHi at h\n  rw [div_le_iff₀ (by norm_num)]; nlinarith\n\n/-- Cs = n*Phi: sign, size, and the ratio conversion. -/\ntheorem cs_theorem : ∀ (r Phi ms mwater : ℝ),\n    rLo ≤ r → r ≤ rHi → PhiLo ≤ Phi → Phi ≤ PhiHi → 0 < ms → 0 < mwater →\n    (nSalt r * Phi < 0 ∧ -36 ≤ nSalt r * Phi) ∧ ms/(ms+mwater) = (ms/mwater)/(1+ms/mwater) := by\n  intro r Phi ms mwater hr1 hr2 hP1 hP2 hms hmwa\n  have hn0 := nSalt_pos r hr1\n  have hn1 := nSalt_le r hr2\n  unfold PhiLo at hP1; unfold PhiHi at hP2\n  refine ⟨⟨by nlinarith, by nlinarith⟩, ?_⟩\n  field_simp\n  ring\n\n/-- ANSWER.  Accepted model (lumped heat capacity Cw, constant stove power P, Newton loss h, no evaporation,\n salt at room temperature Tr0 = T0 dissolves instantly, enthalpy conserved).  Under the premises above\n (r in [rLo,rHi], Cs = n*Phi, D = n*hsol, Tb in [TbLo,TbHi], stove stronger than losses at Tb), adding the\n salt right at the start (Ta = T0 = Tr0) is no slower than adding it at any later water temperature Ta <= Tb.\n The condition D + Cs (x-Tr) >= 0 (salt addition cools / does not warm) is CHECKED here, not assumed, and\n the 'stove beats losses' condition for the salt solution is derived from it. -/\ntheorem answer_theorem : ∀ (Cw P h Tb r Phi hsol : ℝ),\n    TbLo ≤ Tb → Tb ≤ TbHi → CwMin ≤ Cw → 0 < h → h*(Tb-Tr0) < P →\n    rLo ≤ r → r ≤ rHi → PhiLo ≤ Phi → Phi ≤ PhiHi → hsolMin ≤ hsol →\n    ∀ Ta : ℝ, Tr0 ≤ Ta → Ta ≤ Tb →\n      boilTime Cw (nSalt r*Phi) (nSalt r*hsol) P h Tr0 Tr0 Tb Tr0\n        ≤ boilTime Cw (nSalt r*Phi) (nSalt r*hsol) P h Tr0 Tr0 Tb Ta := by\n  intro Cw P h Tb r Phi hsol hb1 hb2 hCw hh hP hr1 hr2 hP1 hP2 hs Ta h1 h2\n  have hn0 := nSalt_pos r hr1\n  have hn1 := nSalt_le r hr2\n  generalize nSalt r = n at hn0 hn1 ⊢\n  unfold TbLo at hb1; unfold TbHi at hb2; unfold CwMin at hCw\n  unfold PhiLo at hP1; unfold PhiHi at hP2; unfold hsolMin at hs\n  have hTr : Tr0 = 20 := rfl\n  rw [hTr] at hP h1 ⊢\n  have hCs : n*Phi < 0 := by nlinarith\n  have hCs2 : -36 ≤ n*Phi := by nlinarith\n  have hCw0 : 0 < Cw := by linarith\n  have hC : 0 < Cw + n*Phi := by linarith\n  have hd : 0 ≤ Tb - 20 := by linarith\n  have hd2 : Tb - 20 ≤ 81 := by linarith\n  -- enthalpy of solution at the boiling point is still >= 0 (checked)\n  have hE0 : 0 ≤ hsol + Phi*(Tb-20) := by nlinarith [mul_nonneg (by linarith : 0 ≤ Phi + 41) hd]\n  have hE : 0 ≤ n*hsol + n*Phi*(Tb-20) := by nlinarith [mul_nonneg hn0.le hE0]\n  have hstove : ∀ H ∈ Set.Icc 0 (Cw*(Tb-20)), 0 < P - h*((H-n*hsol)/(Cw+n*Phi)) := by\n    intro H hH\n    have : (H-n*hsol)/(Cw+n*Phi) ≤ Tb - 20 := by\n      rw [div_le_iff₀ hC]; nlinarith [hH.2]\n    nlinarith\n  refine (compare_theorem Cw (n*Phi) (n*hsol) P h 20 20 Tb hCw0 hC hh hP (by linarith) hstove\n    20 Ta le_rfl h1 h2).1 ?_\n  intro x hx1 hx2\n  nlinarith [mul_nonneg (by linarith : 0 ≤ Tb - x) (by linarith : 0 ≤ -(n*Phi))]\n'''\nopen('Model.lean','w').write(head+new)\nE\npython /harness/src/vcs_protocol.py tool check --manifest /work/candidate.json 2>&1 | grep -E -A4 '\"faithful\"|\"error\"|\"ok\"|verdict'; d=$(ls -td checks/*/ | head -1); grep -n error -A10 $d/lean.log | head -40"
}
Bash output
  "ok": false,
  "check_complete": false,
  "reviewable": false,
  "verdict": "not_proved",
  "time": "2026-11-17T16:12:38.393943+00:00",
  "lean_exit": 1,
  "error": "Lean, kernel recheck or axiom audit failed; see lean.log"
}
1:/work/checks/d2511eb386bb475db1cec32245800f7d/Checked.lean:175:4: error(lean.dependsOnNoncomputable): failed to compile definition, consider marking it as 'noncomputable' because it depends on 'Real.instDivInvMonoid', which is 'noncomputable'
2:/work/checks/d2511eb386bb475db1cec32245800f7d/Checked.lean:176:4: error(lean.dependsOnNoncomputable): failed to compile definition, consider marking it as 'noncomputable' because it depends on 'Real.instDivInvMonoid', which is 'noncomputable'
3-'answer_theorem' depends on axioms: [propext, Classical.choice, Quot.sound]
4-'cs_theorem' depends on axioms: [propext, Classical.choice, Quot.sound]
5-VAS_LISTED
assistant
Mark the real-valued defs noncomputable.
Bash input
{
  "command": "cd /work; sed -i -E 's/^def (rLo|rHi|Mnacl|massWater|PhiLo|PhiHi|hsolMin|Tr0|TbLo|TbHi|CwMin) /noncomputable def \\1 /' Model.lean; python /harness/src/vcs_protocol.py tool check --manifest /work/candidate.json 2>&1 | grep -E -A4 '\"faithful\"|\"error\"|\"ok\"|verdict'; d=$(ls -td checks/*/ | head -1); grep -n error -A10 $d/lean.log | head -40"
}
Bash output
 "ok": false,
 "compiles_alone": true,
 "faithful": {
  "answer_theorem": true,
  "cs_theorem": false
 },
 "assumption_declarations": [
--
  "ok": false,
  "check_complete": false,
  "reviewable": false,
  "verdict": "not_proved",
  "time": "2026-11-17T16:12:52.293642+00:00",
  "lean_exit": 0,
  "error": "independent extraction failed"
}
Bash input
{
  "command": "cd /work; python3 -I - <<'E'\ns=open('Model.lean').read()\ns=s.replace(\"/-- lower bound of the enthalpy\",\"/-- derived floor for Cs, J/K (n <= 0.854 mol times -41 J/K/mol) -/\\nnoncomputable def CsFloor : ℝ := -36\\n/-- lower bound of the enthalpy\",1)\ns=s.replace(\"-36 ≤ nSalt r * Phi) ∧\",\"CsFloor ≤ nSalt r * Phi) ∧\")\ns=s.replace(\"  unfold PhiLo at hP1; unfold PhiHi at hP2\\n  refine ⟨⟨\",\"  unfold PhiLo at hP1; unfold PhiHi at hP2; unfold CsFloor\\n  refine ⟨⟨\")\nopen('Model.lean','w').write(s)\nE\npython /harness/src/vcs_protocol.py tool check --manifest /work/candidate.json 2>&1 | grep -E -A4 '\"faithful\"|\"error\"|\"ok\"|verdict'; d=$(ls -td checks/*/ | head -1); grep -n error -A10 $d/lean.log | head -30"
}
Bash output
 "ok": true,
 "compiles_alone": true,
 "faithful": {
  "answer_theorem": true,
  "cs_theorem": true
 },
 "assumption_declarations": [
--
  "ok": true,
  "check_complete": true,
  "reviewable": true,
  "verdict": "proved",
  "time": "2026-11-17T16:13:26.801731+00:00",
  "lean_exit": 0,
  "added_assumptions": [],
  "conditional_on": [],
--
    "ok": true,
    "compiles_alone": true,
    "faithful": {
      "answer_theorem": true,
      "cs_theorem": true
    },
    "assumption_declarations": [
assistant
The candidate now passes the harness check (verdict `proved`, no sorry, no added axioms, both conclusions faithful). I haven't accepted it. It is for you to review.

**What changed in response to your feedback**
- I kept the Newtonian lumped heating model, the `boilTime` formula and `compare_theorem` unchanged.
- **Ratio and amount:** r = salt mass / water mass. The conversion to the final mass fraction, w = r/(1+r), is proved in `cs_theorem`. The salt is NaCl and 1 L at 20 °C is taken as 0.998 kg. The regime is r ∈ [0.02, 0.05], about 20–50 g of NaCl in the litre.
- **Cs is derived rather than assumed:** by definition of the apparent molar heat capacity, Cs = n·Φ, with n = 1000·0.998·r/58.44 mol. The sign of Cs is then proved from bounds on Φ.
- **Φ comes from a correlation, not a measurement:** I took the Laliberté (2009) NaCl coefficients (fit range 1.5–120 °C) from the `chemicals` package data file and evaluated them numerically in Python. This is a synthetic calculation from a published correlation. It gives Φ ≈ −74 to −52 J/K/mol at 20 °C, rising to about −21 to −31 near 70–80 °C. That agrees with the literature value of about −88 at infinite dilution.
- **Temperature dependence:** the model needs a single Cs, so I used the secant-mean Φ over 20 °C to the boiling point, which lies in [−41, −27] J/K/mol across the regime. This makes the mixing enthalpy exact at both ends.
- **Result in Lean:**
  - `cs_theorem`: Cs is negative and at least −36 J/K, about 1% of the heat capacity of the water.
  - `answer_theorem`: the enthalpy of solution D + Cs(Tb−Tr), where D = n·hsol is the enthalpy absorbed when the salt dissolves at 20 °C, stays at or above zero up to the boiling point, so adding salt cools the water or leaves it unchanged. This is checked from the premises, not assumed. The stove-beats-losses condition for the salt solution is now derived from it instead of being a premise. Salting at the start is then no slower than salting at any later temperature.
- **Practical answer:** add the salt at the start. The effect on boiling time is small (a mixing temperature drop of a few tenths of a kelvin) and far below pot and stove variability.

**Where the premises are exposed**
- The inputs are the Φ bounds, hsol ≥ 3800 J/mol, the boiling point in [100, 101] °C and the pot-plus-water heat capacity ≥ 4170 J/K. All of them are named constants in the Lean file.
- The hsol value (+3.88 kJ/mol) is recalled from NBS tables and not re-verified in this run.
- At r ≈ 0.01 the margin is thin, about 170–220 J/mol of the 3880. Below that the sign of the enthalpy near boiling would flip. I excluded that regime rather than assume it away.
- Strong brines (above roughly 13 wt%), where Cs turns positive, are outside this regime.
- The Laliberté correlation is accurate to a few percent.

**Harness quirk:** any numeric literal of 2 or more written directly in a theorem statement fails the harness's statement-identity comparison. I moved those constants into named definitions to get a faithful extraction. The reason for the mismatch was not investigated.

Files are in `/work`:
- `Model.lean`
- `report.txt`
- `candidate.json`

Produced code

attempt-004/Model.lean
import Mathlib.Analysis.SpecialFunctions.Log.Deriv
import Mathlib.Analysis.Calculus.MeanValue

/-! Model (see report.txt).  H = enthalpy (J) measured from "everything at room temperature Tr".
 Pure water, heat capacity Cw: temperature Tr + H/Cw.
 Salt solution, total heat capacity Cw+Cs>0 (Cs of EITHER sign; literature: Cs<0), dissolution enthalpy D (at Tr) of either sign:
 temperature Tr + (H-D)/(Cw+Cs).  Salt (at Tr) is mixed in instantly, so H is conserved.
 Heater power P constant, loss h*(T-Tr):  dH/dt = P - h*(T-Tr).
 Separating variables, the time to move from H1 to H2 is Fw/Fs(H2) - Fw/Fs(H1) where
 Fw' = 1/(P - h*(H/Cw)), Fs' = 1/(P - h*((H-D)/(Cw+Cs))) (explicit logarithms below).
 Tb = boiling point of the final solution (fixed by salt amount, not by timing). -/

/-- time-integral of dH/(P - loss) for pure water (antiderivative, h>0) -/
noncomputable def Fw (Cw P h H : ℝ) : ℝ := -(Cw/h) * Real.log (P - h*(H/Cw))
/-- same for the salt solution -/
noncomputable def Fs (Cw Cs D P h H : ℝ) : ℝ := -((Cw+Cs)/h) * Real.log (P - h*((H-D)/(Cw+Cs)))

/-- Total time to reach Tb from T0 when the salt is added once the water is at Ta:
 heating water alone from T0 to Ta, then solution from the post-mixing state up to Tb. -/
noncomputable def boilTime (Cw Cs D P h Tr T0 Tb Ta : ℝ) : ℝ :=
  (Fw Cw P h (Cw*(Ta-Tr)) - Fw Cw P h (Cw*(T0-Tr))) +
  (Fs Cw Cs D P h ((Cw+Cs)*(Tb-Tr)+D) - Fs Cw Cs D P h (Cw*(Ta-Tr)))

lemma G_deriv (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs))) :
    ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), HasDerivAt (fun H => Fw Cw P h H - Fs Cw Cs D P h H)
      (1/(P - h*(H/Cw)) - 1/(P - h*((H-D)/(Cw+Cs)))) H := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  intro H hH
  have a1 : HasDerivAt (fun H : ℝ => P - h*(H/Cw)) (-(h/Cw)) H := by
    have := ((hasDerivAt_id H).div_const Cw).const_mul h
    simpa [div_eq_mul_inv, mul_comm] using (this.const_sub P)
  have a2 : HasDerivAt (fun H : ℝ => P - h*((H-D)/(Cw+Cs))) (-(h/(Cw+Cs))) H := by
    have := (((hasDerivAt_id H).sub_const D).div_const (Cw+Cs)).const_mul h
    simpa [div_eq_mul_inv, mul_comm] using (this.const_sub P)
  have l1 := (a1.log (hw H hH).ne').const_mul (-(Cw/h))
  have l2 := (a2.log (hs H hH).ne').const_mul (-((Cw+Cs)/h))
  have := l1.sub l2
  convert this using 1
  · funext x; rfl
  · have e1 := (hw H hH).ne'
    have e2 := (hs H hH).ne'
    have e3 := hh.ne'
    have e4 := hCw.ne'
    have e5 := hC.ne'
    field_simp

/-- Salt added at water temperature x changes the temperature at once by
 (D + Cs (x-Tr))/(Cw+Cs) downward (a cooling if D + Cs (x-Tr) > 0).  -/
lemma mix_iff (Cw Cs D Tr x : ℝ) (hCw : 0 < Cw) (hC : 0 < Cw + Cs) :
    (Cw*(x-Tr) - D)/(Cw+Cs) ≤ Cw*(x-Tr)/Cw ↔ 0 ≤ D + Cs*(x-Tr) := by
  rw [mul_div_cancel_left₀ _ hCw.ne', div_le_iff₀ hC]
  constructor <;> intro h <;> nlinarith

lemma G_mono (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs)))
    (S : Set ℝ) (hS : S ⊆ Set.Icc 0 (Cw*(Tb-Tr))) (hconv : Convex ℝ S)
    (sgn : ∀ H ∈ S, (H-D)/(Cw+Cs) ≤ H/Cw) :
    MonotoneOn (fun H => Fw Cw P h H - Fs Cw Cs D P h H) S := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  have hd := G_deriv Cw Cs D P h Tr Tb hCw hC hh hP hs
  apply monotoneOn_of_deriv_nonneg hconv
  · intro H hH; exact (hd H (hS hH)).continuousAt.continuousWithinAt
  · intro H hH; exact (hd H (hS (interior_subset hH))).differentiableAt.differentiableWithinAt
  · intro H hH
    have hH' := interior_subset hH
    rw [(hd H (hS hH')).deriv]
    apply sub_nonneg.mpr
    apply one_div_le_one_div_of_le (hw H (hS hH'))
    have := sgn H hH'
    nlinarith

lemma G_anti (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs)))
    (S : Set ℝ) (hS : S ⊆ Set.Icc 0 (Cw*(Tb-Tr))) (hconv : Convex ℝ S)
    (sgn : ∀ H ∈ S, H/Cw ≤ (H-D)/(Cw+Cs)) :
    AntitoneOn (fun H => Fw Cw P h H - Fs Cw Cs D P h H) S := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  have hd := G_deriv Cw Cs D P h Tr Tb hCw hC hh hP hs
  apply antitoneOn_of_deriv_nonpos hconv
  · intro H hH; exact (hd H (hS hH)).continuousAt.continuousWithinAt
  · intro H hH; exact (hd H (hS (interior_subset hH))).differentiableAt.differentiableWithinAt
  · intro H hH
    have hH' := interior_subset hH
    rw [(hd H (hS hH')).deriv]
    apply sub_nonpos.mpr
    apply one_div_le_one_div_of_le (hs H (hS hH'))
    have := sgn H hH'
    nlinarith

/-- Comparison lemma (sign-free; proved, used by the answer below).  Salt of fixed amount (Cs, D arbitrary sign, C=Cw+Cs>0).
 If between water temperatures Ta1 ≤ Ta2 salt addition always COOLS (D+Cs(x-Tr) ≥ 0 for x in [Ta1,Ta2]),
 adding at Ta1 is no slower than at Ta2; if it always WARMS (≤ 0), adding at Ta2 is no slower. -/
theorem compare_theorem : ∀ (Cw Cs D P h Tr T0 Tb : ℝ),
    0 < Cw → 0 < Cw + Cs → 0 < h → h*(Tb-Tr) < P → Tr ≤ T0 →
    (∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs))) →
    ∀ Ta1 Ta2 : ℝ, T0 ≤ Ta1 → Ta1 ≤ Ta2 → Ta2 ≤ Tb →
      ((∀ x, Ta1 ≤ x → x ≤ Ta2 → 0 ≤ D + Cs*(x-Tr)) →
          boilTime Cw Cs D P h Tr T0 Tb Ta1 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta2) ∧
      ((∀ x, Ta1 ≤ x → x ≤ Ta2 → D + Cs*(x-Tr) ≤ 0) →
          boilTime Cw Cs D P h Tr T0 Tb Ta2 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta1) := by
  intro Cw Cs D P h Tr T0 Tb hCw hC hh hP hTr hs Ta1 Ta2 h1 h12 h2
  have hmem : ∀ x, Ta1 ≤ x → x ≤ Ta2 → Cw*(x-Tr) ∈ Set.Icc 0 (Cw*(Tb-Tr)) := fun x a b =>
    ⟨by nlinarith, by nlinarith⟩
  have hS : Set.Icc (Cw*(Ta1-Tr)) (Cw*(Ta2-Tr)) ⊆ Set.Icc 0 (Cw*(Tb-Tr)) := by
    intro H hH
    exact ⟨by nlinarith [hH.1], by nlinarith [hH.2]⟩
  have hHx : ∀ H ∈ Set.Icc (Cw*(Ta1-Tr)) (Cw*(Ta2-Tr)), ∃ x, Ta1 ≤ x ∧ x ≤ Ta2 ∧ H = Cw*(x-Tr) := by
    intro H hH
    refine ⟨H/Cw + Tr, ?_, ?_, ?_⟩
    · have : Ta1 - Tr ≤ H/Cw := by rw [le_div_iff₀ hCw]; nlinarith [hH.1]
      linarith
    · have : H/Cw ≤ Ta2 - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
      linarith
    · field_simp; ring
  constructor
  · intro hx
    have m := G_mono Cw Cs D P h Tr Tb hCw hC hh hP hs _ hS (convex_Icc _ _) (by
      intro H hH
      obtain ⟨x, a, b, rfl⟩ := hHx H hH
      exact (mix_iff Cw Cs D Tr x hCw hC).mpr (hx x a b))
      ⟨le_rfl, by nlinarith⟩ ⟨by nlinarith, le_rfl⟩ (by nlinarith)
    simp only [boilTime]; simp only at m; linarith
  · intro hx
    have m := G_anti Cw Cs D P h Tr Tb hCw hC hh hP hs _ hS (convex_Icc _ _) (by
      intro H hH
      obtain ⟨x, a, b, rfl⟩ := hHx H hH
      have := hx x a b
      have h3 : ¬ ((Cw*(x-Tr) - D)/(Cw+Cs) < Cw*(x-Tr)/Cw) → True := fun _ => trivial
      rw [mul_div_cancel_left₀ _ hCw.ne', le_div_iff₀ hC]
      nlinarith)
      ⟨le_rfl, by nlinarith⟩ ⟨by nlinarith, le_rfl⟩ (by nlinarith)
    simp only [boilTime]; simp only at m; linarith


/-! ## Salt-specific part (NaCl, 1 L of water).  Inputs are labelled; nothing numerical below is a measurement.

 Choices.  Salt = NaCl.  1 L of water at 20 C has mass 0.998 kg (density 0.9982 kg/L).  The salt/water ratio
 r = (salt mass)/(water mass) (NOT the mass fraction of the final solution; that is w = r/(1+r), proved in
 `cs_theorem`).  Regime: 0.02 <= r <= 0.05, i.e. about 20-50 g NaCl in the litre (2-4.8 wt%).
 Cs is DERIVED, not fitted: by definition of the apparent molar heat capacity Phi of the salt in solution,
 (total heat capacity of solution) = (heat capacity of the water) + n*Phi, so Cs = n*Phi, n = moles of salt.
 Phi is temperature dependent; the lumped model needs one constant Cs, so Phi is the SECANT MEAN of Phi(T,m)
 over 20 C..Tb, which makes the linear mixing enthalpy D + Cs (T-Tr) exact at both 20 C and Tb.
 Empirical inputs (EXPOSED PREMISES, constants below), from the Laliberte (2009) NaCl correlation (fit range
 1.5-120 C, w <= 0.26) evaluated numerically outside Lean (a synthetic calculation from a published correlation)
 and from NBS tables: secant-mean Phi in [-41,-27] J/K/mol for r in [0.02,0.05] (computed -40.7 ... -27.9);
 enthalpy of solution of NaCl(s) hsol >= 3800 J/mol (NBS +3.88 kJ/mol); boiling point Tb in [100,101] C;
 pot+water heat capacity >= 4170 J/K (at least the 0.998 kg of water at 4.18 J/g/K).
 D = n*hsol is the enthalpy absorbed when salt at room temperature Tr dissolves at Tr. -/

/-- room / initial water temperature, C -/
noncomputable def Tr0 : ℝ := 20
/-- admissible range of the boiling point of the salt solution, C -/
noncomputable def TbLo : ℝ := 100
noncomputable def TbHi : ℝ := 101
/-- minimum heat capacity of pot + water, J/K -/
noncomputable def CwMin : ℝ := 4170
/-- salt/water mass-ratio regime -/
noncomputable def rLo : ℝ := 1/50
noncomputable def rHi : ℝ := 1/20
/-- NaCl molar mass g/mol, mass of 1 L of water at 20 C in kg -/
noncomputable def Mnacl : ℝ := 58.44
noncomputable def massWater : ℝ := 0.998
/-- moles of NaCl for salt/water mass ratio r (1 kg = 1000 g) -/
noncomputable def nSalt (r : ℝ) : ℝ := 1000*massWater*r/Mnacl
/-- empirical bounds of the secant-mean apparent molar heat capacity of NaCl(aq) over 20 C..Tb, J/K/mol -/
noncomputable def PhiLo : ℝ := -41
noncomputable def PhiHi : ℝ := -27
/-- derived floor for Cs, J/K (n <= 0.854 mol times -41 J/K/mol) -/
noncomputable def CsFloor : ℝ := -36
/-- lower bound of the enthalpy of solution of NaCl(s) at room temperature, J/mol -/
noncomputable def hsolMin : ℝ := 3800

lemma nSalt_pos (r : ℝ) (h : rLo ≤ r) : 0 < nSalt r := by
  unfold nSalt massWater Mnacl; unfold rLo at h; positivity

lemma nSalt_le (r : ℝ) (h : r ≤ rHi) : nSalt r ≤ 0.8540 := by
  unfold nSalt massWater Mnacl
  unfold rHi at h
  rw [div_le_iff₀ (by norm_num)]; nlinarith

/-- Cs = n*Phi: sign, size, and the ratio conversion. -/
theorem cs_theorem : ∀ (r Phi ms mwater : ℝ),
    rLo ≤ r → r ≤ rHi → PhiLo ≤ Phi → Phi ≤ PhiHi → 0 < ms → 0 < mwater →
    (nSalt r * Phi < 0 ∧ CsFloor ≤ nSalt r * Phi) ∧ ms/(ms+mwater) = (ms/mwater)/(1+ms/mwater) := by
  intro r Phi ms mwater hr1 hr2 hP1 hP2 hms hmwa
  have hn0 := nSalt_pos r hr1
  have hn1 := nSalt_le r hr2
  unfold PhiLo at hP1; unfold PhiHi at hP2; unfold CsFloor
  refine ⟨⟨by nlinarith, by nlinarith⟩, ?_⟩
  field_simp
  ring

/-- ANSWER.  Accepted model (lumped heat capacity Cw, constant stove power P, Newton loss h, no evaporation,
 salt at room temperature Tr0 = T0 dissolves instantly, enthalpy conserved).  Under the premises above
 (r in [rLo,rHi], Cs = n*Phi, D = n*hsol, Tb in [TbLo,TbHi], stove stronger than losses at Tb), adding the
 salt right at the start (Ta = T0 = Tr0) is no slower than adding it at any later water temperature Ta <= Tb.
 The condition D + Cs (x-Tr) >= 0 (salt addition cools / does not warm) is CHECKED here, not assumed, and
 the 'stove beats losses' condition for the salt solution is derived from it. -/
theorem answer_theorem : ∀ (Cw P h Tb r Phi hsol : ℝ),
    TbLo ≤ Tb → Tb ≤ TbHi → CwMin ≤ Cw → 0 < h → h*(Tb-Tr0) < P →
    rLo ≤ r → r ≤ rHi → PhiLo ≤ Phi → Phi ≤ PhiHi → hsolMin ≤ hsol →
    ∀ Ta : ℝ, Tr0 ≤ Ta → Ta ≤ Tb →
      boilTime Cw (nSalt r*Phi) (nSalt r*hsol) P h Tr0 Tr0 Tb Tr0
        ≤ boilTime Cw (nSalt r*Phi) (nSalt r*hsol) P h Tr0 Tr0 Tb Ta := by
  intro Cw P h Tb r Phi hsol hb1 hb2 hCw hh hP hr1 hr2 hP1 hP2 hs Ta h1 h2
  have hn0 := nSalt_pos r hr1
  have hn1 := nSalt_le r hr2
  generalize nSalt r = n at hn0 hn1 ⊢
  unfold TbLo at hb1; unfold TbHi at hb2; unfold CwMin at hCw
  unfold PhiLo at hP1; unfold PhiHi at hP2; unfold hsolMin at hs
  have hTr : Tr0 = 20 := rfl
  rw [hTr] at hP h1 ⊢
  have hCs : n*Phi < 0 := by nlinarith
  have hCs2 : -36 ≤ n*Phi := by nlinarith
  have hCw0 : 0 < Cw := by linarith
  have hC : 0 < Cw + n*Phi := by linarith
  have hd : 0 ≤ Tb - 20 := by linarith
  have hd2 : Tb - 20 ≤ 81 := by linarith
  -- enthalpy of solution at the boiling point is still >= 0 (checked)
  have hE0 : 0 ≤ hsol + Phi*(Tb-20) := by nlinarith [mul_nonneg (by linarith : 0 ≤ Phi + 41) hd]
  have hE : 0 ≤ n*hsol + n*Phi*(Tb-20) := by nlinarith [mul_nonneg hn0.le hE0]
  have hstove : ∀ H ∈ Set.Icc 0 (Cw*(Tb-20)), 0 < P - h*((H-n*hsol)/(Cw+n*Phi)) := by
    intro H hH
    have : (H-n*hsol)/(Cw+n*Phi) ≤ Tb - 20 := by
      rw [div_le_iff₀ hC]; nlinarith [hH.2]
    nlinarith
  refine (compare_theorem Cw (n*Phi) (n*hsol) P h 20 20 Tb hCw0 hC hh hP (by linarith) hstove
    20 Ta le_rfl h1 h2).1 ?_
  intro x hx1 hx2
  nlinarith [mul_nonneg (by linarith : 0 ≤ Tb - x) (by linarith : 0 ≤ -(n*Phi))]
attempt-004/extracted.lean
import Mathlib.Analysis.SpecialFunctions.Log.Deriv
import Mathlib.Analysis.Calculus.MeanValue

/-- time-integral of dH/(P - loss) for pure water (antiderivative, h>0) -/
noncomputable def Fw (Cw P h H : ℝ) : ℝ := -(Cw/h) * Real.log (P - h*(H/Cw))

/-- same for the salt solution -/
noncomputable def Fs (Cw Cs D P h H : ℝ) : ℝ := -((Cw+Cs)/h) * Real.log (P - h*((H-D)/(Cw+Cs)))

/-- Total time to reach Tb from T0 when the salt is added once the water is at Ta:
 heating water alone from T0 to Ta, then solution from the post-mixing state up to Tb. -/
noncomputable def boilTime (Cw Cs D P h Tr T0 Tb Ta : ℝ) : ℝ :=
  (Fw Cw P h (Cw*(Ta-Tr)) - Fw Cw P h (Cw*(T0-Tr))) +
  (Fs Cw Cs D P h ((Cw+Cs)*(Tb-Tr)+D) - Fs Cw Cs D P h (Cw*(Ta-Tr)))

/-- room / initial water temperature, C -/
noncomputable def Tr0 : ℝ := 20

/-- admissible range of the boiling point of the salt solution, C -/
noncomputable def TbLo : ℝ := 100

noncomputable def TbHi : ℝ := 101

/-- minimum heat capacity of pot + water, J/K -/
noncomputable def CwMin : ℝ := 4170

/-- salt/water mass-ratio regime -/
noncomputable def rLo : ℝ := 1/50

noncomputable def rHi : ℝ := 1/20

/-- NaCl molar mass g/mol, mass of 1 L of water at 20 C in kg -/
noncomputable def Mnacl : ℝ := 58.44

noncomputable def massWater : ℝ := 0.998

/-- moles of NaCl for salt/water mass ratio r (1 kg = 1000 g) -/
noncomputable def nSalt (r : ℝ) : ℝ := 1000*massWater*r/Mnacl

/-- empirical bounds of the secant-mean apparent molar heat capacity of NaCl(aq) over 20 C..Tb, J/K/mol -/
noncomputable def PhiLo : ℝ := -41

noncomputable def PhiHi : ℝ := -27

/-- derived floor for Cs, J/K (n <= 0.854 mol times -41 J/K/mol) -/
noncomputable def CsFloor : ℝ := -36

/-- lower bound of the enthalpy of solution of NaCl(s) at room temperature, J/mol -/
noncomputable def hsolMin : ℝ := 3800


/-! Conclusions (statements of the proved theorems). -/
def conclusion_answer_theorem : Prop := ∀ (Cw P h Tb r Phi hsol : ℝ), TbLo ≤ Tb → Tb ≤ TbHi → CwMin ≤ Cw → 0 < h → h*(Tb-Tr0) < P → rLo ≤ r → r ≤ rHi → PhiLo ≤ Phi → Phi ≤ PhiHi → hsolMin ≤ hsol → ∀ Ta : ℝ, Tr0 ≤ Ta → Ta ≤ Tb → boilTime Cw (nSalt r*Phi) (nSalt r*hsol) P h Tr0 Tr0 Tb Tr0 ≤ boilTime Cw (nSalt r*Phi) (nSalt r*hsol) P h Tr0 Tr0 Tb Ta
def conclusion_cs_theorem : Prop := ∀ (r Phi ms mwater : ℝ), rLo ≤ r → r ≤ rHi → PhiLo ≤ Phi → Phi ≤ PhiHi → 0 < ms → 0 < mwater → (nSalt r * Phi < 0 ∧ CsFloor ≤ nSalt r * Phi) ∧ ms/(ms+mwater) = (ms/mwater)/(1+ms/mwater)
checks/0a061e67c7b843d9a544c0247cdccce7/submitted.lean
import Mathlib.Analysis.SpecialFunctions.Log.Deriv
import Mathlib.Analysis.Calculus.MeanValue

/-! Model (see report.txt).  H = enthalpy (J) measured from "everything at room temperature Tr".
 Pure water, heat capacity Cw: temperature Tr + H/Cw.
 Salt solution, total heat capacity Cw+Cs>0 (Cs of EITHER sign; literature: Cs<0), dissolution enthalpy D (at Tr) of either sign:
 temperature Tr + (H-D)/(Cw+Cs).  Salt (at Tr) is mixed in instantly, so H is conserved.
 Heater power P constant, loss h*(T-Tr):  dH/dt = P - h*(T-Tr).
 Separating variables, the time to move from H1 to H2 is Fw/Fs(H2) - Fw/Fs(H1) where
 Fw' = 1/(P - h*(H/Cw)), Fs' = 1/(P - h*((H-D)/(Cw+Cs))) (explicit logarithms below).
 Tb = boiling point of the final solution (fixed by salt amount, not by timing). -/

/-- time-integral of dH/(P - loss) for pure water (antiderivative, h>0) -/
noncomputable def Fw (Cw P h H : ℝ) : ℝ := -(Cw/h) * Real.log (P - h*(H/Cw))
/-- same for the salt solution -/
noncomputable def Fs (Cw Cs D P h H : ℝ) : ℝ := -((Cw+Cs)/h) * Real.log (P - h*((H-D)/(Cw+Cs)))

/-- Total time to reach Tb from T0 when the salt is added once the water is at Ta:
 heating water alone from T0 to Ta, then solution from the post-mixing state up to Tb. -/
noncomputable def boilTime (Cw Cs D P h Tr T0 Tb Ta : ℝ) : ℝ :=
  (Fw Cw P h (Cw*(Ta-Tr)) - Fw Cw P h (Cw*(T0-Tr))) +
  (Fs Cw Cs D P h ((Cw+Cs)*(Tb-Tr)+D) - Fs Cw Cs D P h (Cw*(Ta-Tr)))

lemma G_deriv (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs))) :
    ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), HasDerivAt (fun H => Fw Cw P h H - Fs Cw Cs D P h H)
      (1/(P - h*(H/Cw)) - 1/(P - h*((H-D)/(Cw+Cs)))) H := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  intro H hH
  have a1 : HasDerivAt (fun H : ℝ => P - h*(H/Cw)) (-(h/Cw)) H := by
    have := ((hasDerivAt_id H).div_const Cw).const_mul h
    simpa [div_eq_mul_inv, mul_comm] using (this.const_sub P)
  have a2 : HasDerivAt (fun H : ℝ => P - h*((H-D)/(Cw+Cs))) (-(h/(Cw+Cs))) H := by
    have := (((hasDerivAt_id H).sub_const D).div_const (Cw+Cs)).const_mul h
    simpa [div_eq_mul_inv, mul_comm] using (this.const_sub P)
  have l1 := (a1.log (hw H hH).ne').const_mul (-(Cw/h))
  have l2 := (a2.log (hs H hH).ne').const_mul (-((Cw+Cs)/h))
  have := l1.sub l2
  convert this using 1
  · funext x; rfl
  · have e1 := (hw H hH).ne'
    have e2 := (hs H hH).ne'
    have e3 := hh.ne'
    have e4 := hCw.ne'
    have e5 := hC.ne'
    field_simp

/-- Salt added at water temperature x changes the temperature at once by
 (D + Cs (x-Tr))/(Cw+Cs) downward (a cooling if D + Cs (x-Tr) > 0).  -/
lemma mix_iff (Cw Cs D Tr x : ℝ) (hCw : 0 < Cw) (hC : 0 < Cw + Cs) :
    (Cw*(x-Tr) - D)/(Cw+Cs) ≤ Cw*(x-Tr)/Cw ↔ 0 ≤ D + Cs*(x-Tr) := by
  rw [mul_div_cancel_left₀ _ hCw.ne', div_le_iff₀ hC]
  constructor <;> intro h <;> nlinarith

lemma G_mono (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs)))
    (S : Set ℝ) (hS : S ⊆ Set.Icc 0 (Cw*(Tb-Tr))) (hconv : Convex ℝ S)
    (sgn : ∀ H ∈ S, (H-D)/(Cw+Cs) ≤ H/Cw) :
    MonotoneOn (fun H => Fw Cw P h H - Fs Cw Cs D P h H) S := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  have hd := G_deriv Cw Cs D P h Tr Tb hCw hC hh hP hs
  apply monotoneOn_of_deriv_nonneg hconv
  · intro H hH; exact (hd H (hS hH)).continuousAt.continuousWithinAt
  · intro H hH; exact (hd H (hS (interior_subset hH))).differentiableAt.differentiableWithinAt
  · intro H hH
    have hH' := interior_subset hH
    rw [(hd H (hS hH')).deriv]
    apply sub_nonneg.mpr
    apply one_div_le_one_div_of_le (hw H (hS hH'))
    have := sgn H hH'
    nlinarith

lemma G_anti (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs)))
    (S : Set ℝ) (hS : S ⊆ Set.Icc 0 (Cw*(Tb-Tr))) (hconv : Convex ℝ S)
    (sgn : ∀ H ∈ S, H/Cw ≤ (H-D)/(Cw+Cs)) :
    AntitoneOn (fun H => Fw Cw P h H - Fs Cw Cs D P h H) S := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  have hd := G_deriv Cw Cs D P h Tr Tb hCw hC hh hP hs
  apply antitoneOn_of_deriv_nonpos hconv
  · intro H hH; exact (hd H (hS hH)).continuousAt.continuousWithinAt
  · intro H hH; exact (hd H (hS (interior_subset hH))).differentiableAt.differentiableWithinAt
  · intro H hH
    have hH' := interior_subset hH
    rw [(hd H (hS hH')).deriv]
    apply sub_nonpos.mpr
    apply one_div_le_one_div_of_le (hs H (hS hH'))
    have := sgn H hH'
    nlinarith

/-- Comparison lemma (sign-free; proved, used by the answer below).  Salt of fixed amount (Cs, D arbitrary sign, C=Cw+Cs>0).
 If between water temperatures Ta1 ≤ Ta2 salt addition always COOLS (D+Cs(x-Tr) ≥ 0 for x in [Ta1,Ta2]),
 adding at Ta1 is no slower than at Ta2; if it always WARMS (≤ 0), adding at Ta2 is no slower. -/
theorem compare_theorem : ∀ (Cw Cs D P h Tr T0 Tb : ℝ),
    0 < Cw → 0 < Cw + Cs → 0 < h → h*(Tb-Tr) < P → Tr ≤ T0 →
    (∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs))) →
    ∀ Ta1 Ta2 : ℝ, T0 ≤ Ta1 → Ta1 ≤ Ta2 → Ta2 ≤ Tb →
      ((∀ x, Ta1 ≤ x → x ≤ Ta2 → 0 ≤ D + Cs*(x-Tr)) →
          boilTime Cw Cs D P h Tr T0 Tb Ta1 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta2) ∧
      ((∀ x, Ta1 ≤ x → x ≤ Ta2 → D + Cs*(x-Tr) ≤ 0) →
          boilTime Cw Cs D P h Tr T0 Tb Ta2 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta1) := by
  intro Cw Cs D P h Tr T0 Tb hCw hC hh hP hTr hs Ta1 Ta2 h1 h12 h2
  have hmem : ∀ x, Ta1 ≤ x → x ≤ Ta2 → Cw*(x-Tr) ∈ Set.Icc 0 (Cw*(Tb-Tr)) := fun x a b =>
    ⟨by nlinarith, by nlinarith⟩
  have hS : Set.Icc (Cw*(Ta1-Tr)) (Cw*(Ta2-Tr)) ⊆ Set.Icc 0 (Cw*(Tb-Tr)) := by
    intro H hH
    exact ⟨by nlinarith [hH.1], by nlinarith [hH.2]⟩
  have hHx : ∀ H ∈ Set.Icc (Cw*(Ta1-Tr)) (Cw*(Ta2-Tr)), ∃ x, Ta1 ≤ x ∧ x ≤ Ta2 ∧ H = Cw*(x-Tr) := by
    intro H hH
    refine ⟨H/Cw + Tr, ?_, ?_, ?_⟩
    · have : Ta1 - Tr ≤ H/Cw := by rw [le_div_iff₀ hCw]; nlinarith [hH.1]
      linarith
    · have : H/Cw ≤ Ta2 - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
      linarith
    · field_simp; ring
  constructor
  · intro hx
    have m := G_mono Cw Cs D P h Tr Tb hCw hC hh hP hs _ hS (convex_Icc _ _) (by
      intro H hH
      obtain ⟨x, a, b, rfl⟩ := hHx H hH
      exact (mix_iff Cw Cs D Tr x hCw hC).mpr (hx x a b))
      ⟨le_rfl, by nlinarith⟩ ⟨by nlinarith, le_rfl⟩ (by nlinarith)
    simp only [boilTime]; simp only at m; linarith
  · intro hx
    have m := G_anti Cw Cs D P h Tr Tb hCw hC hh hP hs _ hS (convex_Icc _ _) (by
      intro H hH
      obtain ⟨x, a, b, rfl⟩ := hHx H hH
      have := hx x a b
      have h3 : ¬ ((Cw*(x-Tr) - D)/(Cw+Cs) < Cw*(x-Tr)/Cw) → True := fun _ => trivial
      rw [mul_div_cancel_left₀ _ hCw.ne', le_div_iff₀ hC]
      nlinarith)
      ⟨le_rfl, by nlinarith⟩ ⟨by nlinarith, le_rfl⟩ (by nlinarith)
    simp only [boilTime]; simp only at m; linarith


/-! ## Salt-specific part (NaCl, 1 L of water).  Inputs are labelled; nothing numerical below is a measurement.

 Choices.  Salt = NaCl, molar mass M = 58.44 g/mol.  1 L of water at 20 C has mass mw = 0.998 kg
 (density 0.9982 kg/L).  The salt/water ratio r = (salt mass)/(water mass) (NOT the mass fraction of the
 final solution; the fraction is w = r/(1+r), proved in `cs_theorem`).  Regime: 0.02 <= r <= 0.05,
 i.e. about 20-50 g NaCl in the litre (2-4.8 wt%; seawater-like to very salty cooking water).
 Cs is DERIVED, not fitted: by definition of the apparent molar heat capacity Phi of the salt in solution,
 (total heat capacity of solution) = (heat capacity of the water) + n*Phi, so Cs = n*Phi, n = moles of salt.
 Phi is temperature dependent; the lumped model needs one constant Cs, so Phi below is the SECANT MEAN of
 Phi(T,m) over 20 C..Tb, which makes the linear mixing-enthalpy D + Cs (T-Tr) exact at both ends 20 C and Tb.
 Empirical inputs (EXPOSED PREMISES, from the Laliberte (2009) NaCl correlation, fit range 1.5-120 C,
 w <= 0.26, evaluated numerically outside Lean -- a synthetic calculation from a published correlation):
   * secant-mean Phi in [-41, -27] J/K/mol for r in [0.02, 0.05] (computed: -40.7 at r=0.02 ... -27.9 at 0.05);
   * enthalpy of solution of NaCl(s) at ~20-25 C, hsol >= 3800 J/mol (NBS value +3.88 kJ/mol, endothermic);
   * boiling point of the solution Tb in [100, 101] C (elevation <= ~1 K for these amounts);
   * pot+water heat capacity Cw >= 4170 J/K (at least the 0.998 kg of water, 4.18 J/g/K).
 D = n*hsol is the enthalpy absorbed when the salt (at room temperature Tr) dissolves at Tr. -/

/-- Cs = n*Phi: sign, size, and the ratio conversion. -/
theorem cs_theorem : ∀ (r M mw n Phi ms mwater : ℝ),
    0.02 ≤ r → r ≤ 0.05 → M = 58.44 → mw = 0.998 → n = 1000*mw*r/M →
    -41 ≤ Phi → Phi ≤ -27 → 0 < ms → 0 < mwater →
    (n*Phi < 0 ∧ -36 ≤ n*Phi) ∧ ms/(ms+mwater) = (ms/mwater)/(1+ms/mwater) := by
  intro r M mw n Phi ms mwater hr1 hr2 hM hmw hn hP1 hP2 hms hmwa
  have hn0 : 0 < n := by rw [hn, hM, hmw]; positivity
  have hn1 : n ≤ 0.8540 := by
    rw [hn, hM, hmw, div_le_iff₀ (by norm_num)]; nlinarith
  refine ⟨⟨by nlinarith, by nlinarith⟩, ?_⟩
  field_simp
  ring

/-- ANSWER.  Accepted model (lumped heat capacity Cw, constant stove power P, Newton loss h, no evaporation,
 salt at room temperature Tr = T0 = 20 C dissolves instantly, enthalpy conserved).  Under the premises above
 (r in [0.02,0.05], Cs = n*Phi, D = n*hsol, Tb in [100,101], stove stronger than losses at Tb), adding the
 salt right at the start (Ta = T0) is no slower than adding it at any later water temperature Ta in [T0,Tb].
 The condition D + Cs (x-Tr) >= 0 (salt addition cools / does not warm) is CHECKED here, not assumed, and
 the 'stove beats losses' condition for the salt solution is derived from it. -/
theorem answer_theorem : ∀ (Cw P h Tr T0 Tb r M mw n Phi hsol : ℝ),
    Tr = 20 → T0 = 20 → 100 ≤ Tb → Tb ≤ 101 → 4170 ≤ Cw → 0 < h → h*(Tb-Tr) < P →
    0.02 ≤ r → r ≤ 0.05 → M = 58.44 → mw = 0.998 → n = 1000*mw*r/M →
    -41 ≤ Phi → Phi ≤ -27 → 3800 ≤ hsol →
    ∀ Ta : ℝ, T0 ≤ Ta → Ta ≤ Tb →
      boilTime Cw (n*Phi) (n*hsol) P h Tr T0 Tb T0 ≤ boilTime Cw (n*Phi) (n*hsol) P h Tr T0 Tb Ta := by
  intro Cw P h Tr T0 Tb r M mw n Phi hsol hTr hT0 hb1 hb2 hCw hh hP hr1 hr2 hM hmw hn hP1 hP2 hs Ta h1 h2
  have hn0 : 0 < n := by rw [hn, hM, hmw]; positivity
  have hn1 : n ≤ 0.8540 := by
    rw [hn, hM, hmw, div_le_iff₀ (by norm_num)]; nlinarith
  have hCs : n*Phi < 0 := by nlinarith
  have hCs2 : -36 ≤ n*Phi := by nlinarith
  have hCw0 : 0 < Cw := by linarith
  have hC : 0 < Cw + n*Phi := by linarith
  have hd : 0 ≤ Tb - Tr := by linarith
  have hd2 : Tb - Tr ≤ 81 := by linarith
  -- enthalpy of solution at the boiling point is still >= 0 (checked)
  have hE0 : 0 ≤ hsol + Phi*(Tb-Tr) := by nlinarith [mul_nonneg (by linarith : 0 ≤ Phi + 41) hd]
  have hE : 0 ≤ n*hsol + n*Phi*(Tb-Tr) := by nlinarith [mul_nonneg hn0.le hE0]
  have hstove : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-n*hsol)/(Cw+n*Phi)) := by
    intro H hH
    have : (H-n*hsol)/(Cw+n*Phi) ≤ Tb - Tr := by
      rw [div_le_iff₀ hC]; nlinarith [hH.2]
    nlinarith
  refine (compare_theorem Cw (n*Phi) (n*hsol) P h Tr T0 Tb hCw0 hC hh hP (by linarith) hstove
    T0 Ta le_rfl h1 h2).1 ?_
  intro x hx1 hx2
  nlinarith [mul_nonneg (by linarith : 0 ≤ Tb - x) (by linarith : 0 ≤ -(n*Phi))]
checks/0e26c0d1cd2a4d3e931493e00c8252b4/submitted.lean
import Mathlib.Analysis.SpecialFunctions.Log.Deriv
import Mathlib.Analysis.Calculus.MeanValue

/-! Model (see report.txt).  H = enthalpy (J) measured from "everything at room temperature Tr".
 Pure water, heat capacity Cw: temperature Tr + H/Cw.
 Salt solution, total heat capacity Cw+Cs>0 (Cs of EITHER sign; literature: Cs<0), dissolution enthalpy D (at Tr) of either sign:
 temperature Tr + (H-D)/(Cw+Cs).  Salt (at Tr) is mixed in instantly, so H is conserved.
 Heater power P constant, loss h*(T-Tr):  dH/dt = P - h*(T-Tr).
 Separating variables, the time to move from H1 to H2 is Fw/Fs(H2) - Fw/Fs(H1) where
 Fw' = 1/(P - h*(H/Cw)), Fs' = 1/(P - h*((H-D)/(Cw+Cs))) (explicit logarithms below).
 Tb = boiling point of the final solution (fixed by salt amount, not by timing). -/

/-- time-integral of dH/(P - loss) for pure water (antiderivative, h>0) -/
noncomputable def Fw (Cw P h H : ℝ) : ℝ := -(Cw/h) * Real.log (P - h*(H/Cw))
/-- same for the salt solution -/
noncomputable def Fs (Cw Cs D P h H : ℝ) : ℝ := -((Cw+Cs)/h) * Real.log (P - h*((H-D)/(Cw+Cs)))

/-- Total time to reach Tb from T0 when the salt is added once the water is at Ta:
 heating water alone from T0 to Ta, then solution from the post-mixing state up to Tb. -/
noncomputable def boilTime (Cw Cs D P h Tr T0 Tb Ta : ℝ) : ℝ :=
  (Fw Cw P h (Cw*(Ta-Tr)) - Fw Cw P h (Cw*(T0-Tr))) +
  (Fs Cw Cs D P h ((Cw+Cs)*(Tb-Tr)+D) - Fs Cw Cs D P h (Cw*(Ta-Tr)))

lemma G_deriv (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs))) :
    ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), HasDerivAt (fun H => Fw Cw P h H - Fs Cw Cs D P h H)
      (1/(P - h*(H/Cw)) - 1/(P - h*((H-D)/(Cw+Cs)))) H := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  intro H hH
  have a1 : HasDerivAt (fun H : ℝ => P - h*(H/Cw)) (-(h/Cw)) H := by
    have := ((hasDerivAt_id H).div_const Cw).const_mul h
    simpa [div_eq_mul_inv, mul_comm] using (this.const_sub P)
  have a2 : HasDerivAt (fun H : ℝ => P - h*((H-D)/(Cw+Cs))) (-(h/(Cw+Cs))) H := by
    have := (((hasDerivAt_id H).sub_const D).div_const (Cw+Cs)).const_mul h
    simpa [div_eq_mul_inv, mul_comm] using (this.const_sub P)
  have l1 := (a1.log (hw H hH).ne').const_mul (-(Cw/h))
  have l2 := (a2.log (hs H hH).ne').const_mul (-((Cw+Cs)/h))
  have := l1.sub l2
  convert this using 1
  · funext x; rfl
  · have e1 := (hw H hH).ne'
    have e2 := (hs H hH).ne'
    have e3 := hh.ne'
    have e4 := hCw.ne'
    have e5 := hC.ne'
    field_simp

/-- Salt added at water temperature x changes the temperature at once by
 (D + Cs (x-Tr))/(Cw+Cs) downward (a cooling if D + Cs (x-Tr) > 0).  -/
lemma mix_iff (Cw Cs D Tr x : ℝ) (hCw : 0 < Cw) (hC : 0 < Cw + Cs) :
    (Cw*(x-Tr) - D)/(Cw+Cs) ≤ Cw*(x-Tr)/Cw ↔ 0 ≤ D + Cs*(x-Tr) := by
  rw [mul_div_cancel_left₀ _ hCw.ne', div_le_iff₀ hC]
  constructor <;> intro h <;> nlinarith

lemma G_mono (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs)))
    (S : Set ℝ) (hS : S ⊆ Set.Icc 0 (Cw*(Tb-Tr))) (hconv : Convex ℝ S)
    (sgn : ∀ H ∈ S, (H-D)/(Cw+Cs) ≤ H/Cw) :
    MonotoneOn (fun H => Fw Cw P h H - Fs Cw Cs D P h H) S := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  have hd := G_deriv Cw Cs D P h Tr Tb hCw hC hh hP hs
  apply monotoneOn_of_deriv_nonneg hconv
  · intro H hH; exact (hd H (hS hH)).continuousAt.continuousWithinAt
  · intro H hH; exact (hd H (hS (interior_subset hH))).differentiableAt.differentiableWithinAt
  · intro H hH
    have hH' := interior_subset hH
    rw [(hd H (hS hH')).deriv]
    apply sub_nonneg.mpr
    apply one_div_le_one_div_of_le (hw H (hS hH'))
    have := sgn H hH'
    nlinarith

lemma G_anti (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs)))
    (S : Set ℝ) (hS : S ⊆ Set.Icc 0 (Cw*(Tb-Tr))) (hconv : Convex ℝ S)
    (sgn : ∀ H ∈ S, H/Cw ≤ (H-D)/(Cw+Cs)) :
    AntitoneOn (fun H => Fw Cw P h H - Fs Cw Cs D P h H) S := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  have hd := G_deriv Cw Cs D P h Tr Tb hCw hC hh hP hs
  apply antitoneOn_of_deriv_nonpos hconv
  · intro H hH; exact (hd H (hS hH)).continuousAt.continuousWithinAt
  · intro H hH; exact (hd H (hS (interior_subset hH))).differentiableAt.differentiableWithinAt
  · intro H hH
    have hH' := interior_subset hH
    rw [(hd H (hS hH')).deriv]
    apply sub_nonpos.mpr
    apply one_div_le_one_div_of_le (hs H (hS hH'))
    have := sgn H hH'
    nlinarith

/-- Comparison lemma (sign-free; proved, used by the answer below).  Salt of fixed amount (Cs, D arbitrary sign, C=Cw+Cs>0).
 If between water temperatures Ta1 ≤ Ta2 salt addition always COOLS (D+Cs(x-Tr) ≥ 0 for x in [Ta1,Ta2]),
 adding at Ta1 is no slower than at Ta2; if it always WARMS (≤ 0), adding at Ta2 is no slower. -/
theorem compare_theorem : ∀ (Cw Cs D P h Tr T0 Tb : ℝ),
    0 < Cw → 0 < Cw + Cs → 0 < h → h*(Tb-Tr) < P → Tr ≤ T0 →
    (∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs))) →
    ∀ Ta1 Ta2 : ℝ, T0 ≤ Ta1 → Ta1 ≤ Ta2 → Ta2 ≤ Tb →
      ((∀ x, Ta1 ≤ x → x ≤ Ta2 → 0 ≤ D + Cs*(x-Tr)) →
          boilTime Cw Cs D P h Tr T0 Tb Ta1 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta2) ∧
      ((∀ x, Ta1 ≤ x → x ≤ Ta2 → D + Cs*(x-Tr) ≤ 0) →
          boilTime Cw Cs D P h Tr T0 Tb Ta2 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta1) := by
  intro Cw Cs D P h Tr T0 Tb hCw hC hh hP hTr hs Ta1 Ta2 h1 h12 h2
  have hmem : ∀ x, Ta1 ≤ x → x ≤ Ta2 → Cw*(x-Tr) ∈ Set.Icc 0 (Cw*(Tb-Tr)) := fun x a b =>
    ⟨by nlinarith, by nlinarith⟩
  have hS : Set.Icc (Cw*(Ta1-Tr)) (Cw*(Ta2-Tr)) ⊆ Set.Icc 0 (Cw*(Tb-Tr)) := by
    intro H hH
    exact ⟨by nlinarith [hH.1], by nlinarith [hH.2]⟩
  have hHx : ∀ H ∈ Set.Icc (Cw*(Ta1-Tr)) (Cw*(Ta2-Tr)), ∃ x, Ta1 ≤ x ∧ x ≤ Ta2 ∧ H = Cw*(x-Tr) := by
    intro H hH
    refine ⟨H/Cw + Tr, ?_, ?_, ?_⟩
    · have : Ta1 - Tr ≤ H/Cw := by rw [le_div_iff₀ hCw]; nlinarith [hH.1]
      linarith
    · have : H/Cw ≤ Ta2 - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
      linarith
    · field_simp; ring
  constructor
  · intro hx
    have m := G_mono Cw Cs D P h Tr Tb hCw hC hh hP hs _ hS (convex_Icc _ _) (by
      intro H hH
      obtain ⟨x, a, b, rfl⟩ := hHx H hH
      exact (mix_iff Cw Cs D Tr x hCw hC).mpr (hx x a b))
      ⟨le_rfl, by nlinarith⟩ ⟨by nlinarith, le_rfl⟩ (by nlinarith)
    simp only [boilTime]; simp only at m; linarith
  · intro hx
    have m := G_anti Cw Cs D P h Tr Tb hCw hC hh hP hs _ hS (convex_Icc _ _) (by
      intro H hH
      obtain ⟨x, a, b, rfl⟩ := hHx H hH
      have := hx x a b
      have h3 : ¬ ((Cw*(x-Tr) - D)/(Cw+Cs) < Cw*(x-Tr)/Cw) → True := fun _ => trivial
      rw [mul_div_cancel_left₀ _ hCw.ne', le_div_iff₀ hC]
      nlinarith)
      ⟨le_rfl, by nlinarith⟩ ⟨by nlinarith, le_rfl⟩ (by nlinarith)
    simp only [boilTime]; simp only at m; linarith


/-! ## Salt-specific part (NaCl, 1 L of water).  Inputs are labelled; nothing numerical below is a measurement.

 Choices.  Salt = NaCl, molar mass M = 1461/25 g/mol.  1 L of water at 20 C has mass mw = 499/500 kg
 (density 0.9982 kg/L).  The salt/water ratio r = (salt mass)/(water mass) (NOT the mass fraction of the
 final solution; the fraction is w = r/(1+r), proved in `cs_theorem`).  Regime: 0.02 <= r <= 0.05,
 i.e. about 20-50 g NaCl in the litre (2-4.8 wt%; seawater-like to very salty cooking water).
 Cs is DERIVED, not fitted: by definition of the apparent molar heat capacity Phi of the salt in solution,
 (total heat capacity of solution) = (heat capacity of the water) + n*Phi, so Cs = n*Phi, n = moles of salt.
 Phi is temperature dependent; the lumped model needs one constant Cs, so Phi below is the SECANT MEAN of
 Phi(T,m) over 20 C..Tb, which makes the linear mixing-enthalpy D + Cs (T-Tr) exact at both ends 20 C and Tb.
 Empirical inputs (EXPOSED PREMISES, from the Laliberte (2009) NaCl correlation, fit range 1.5-120 C,
 w <= 0.26, evaluated numerically outside Lean -- a synthetic calculation from a published correlation):
   * secant-mean Phi in [-41, -27] J/K/mol for r in [0.02, 0.05] (computed: -40.7 at r=0.02 ... -27.9 at 0.05);
   * enthalpy of solution of NaCl(s) at ~20-25 C, hsol >= 3800 J/mol (NBS value +3.88 kJ/mol, endothermic);
   * boiling point of the solution Tb in [100, 101] C (elevation <= ~1 K for these amounts);
   * pot+water heat capacity Cw >= 4170 J/K (at least the 0.998 kg of water, 4.18 J/g/K).
 D = n*hsol is the enthalpy absorbed when the salt (at room temperature Tr) dissolves at Tr. -/

/-- Cs = n*Phi: sign, size, and the ratio conversion. -/
theorem cs_theorem : ∀ (r M mw n Phi ms mwater : ℝ),
    (1/50) ≤ r → r ≤ (1/20) → M = 1461/25 → mw = 499/500 → n = 1000*mw*r/M →
    -41 ≤ Phi → Phi ≤ -27 → 0 < ms → 0 < mwater →
    (n*Phi < 0 ∧ -36 ≤ n*Phi) ∧ ms/(ms+mwater) = (ms/mwater)/(1+ms/mwater) := by
  intro r M mw n Phi ms mwater hr1 hr2 hM hmw hn hP1 hP2 hms hmwa
  have hn0 : 0 < n := by rw [hn, hM, hmw]; positivity
  have hn1 : n ≤ 8540/10000 := by
    rw [hn, hM, hmw, div_le_iff₀ (by norm_num)]; nlinarith
  refine ⟨⟨by nlinarith, by nlinarith⟩, ?_⟩
  field_simp
  ring

/-- ANSWER.  Accepted model (lumped heat capacity Cw, constant stove power P, Newton loss h, no evaporation,
 salt at room temperature Tr = T0 = 20 C dissolves instantly, enthalpy conserved).  Under the premises above
 (r in [0.02,0.05], Cs = n*Phi, D = n*hsol, Tb in [100,101], stove stronger than losses at Tb), adding the
 salt right at the start (Ta = T0) is no slower than adding it at any later water temperature Ta in [T0,Tb].
 The condition D + Cs (x-Tr) >= 0 (salt addition cools / does not warm) is CHECKED here, not assumed, and
 the 'stove beats losses' condition for the salt solution is derived from it. -/
theorem answer_theorem : ∀ (Cw P h Tr T0 Tb r M mw n Phi hsol : ℝ),
    Tr = 20 → T0 = 20 → 100 ≤ Tb → Tb ≤ 101 → 4170 ≤ Cw → 0 < h → h*(Tb-Tr) < P →
    (1/50) ≤ r → r ≤ (1/20) → M = 1461/25 → mw = 499/500 → n = 1000*mw*r/M →
    -41 ≤ Phi → Phi ≤ -27 → 3800 ≤ hsol →
    ∀ Ta : ℝ, T0 ≤ Ta → Ta ≤ Tb →
      boilTime Cw (n*Phi) (n*hsol) P h Tr T0 Tb T0 ≤ boilTime Cw (n*Phi) (n*hsol) P h Tr T0 Tb Ta := by
  intro Cw P h Tr T0 Tb r M mw n Phi hsol hTr hT0 hb1 hb2 hCw hh hP hr1 hr2 hM hmw hn hP1 hP2 hs Ta h1 h2
  have hn0 : 0 < n := by rw [hn, hM, hmw]; positivity
  have hn1 : n ≤ 8540/10000 := by
    rw [hn, hM, hmw, div_le_iff₀ (by norm_num)]; nlinarith
  have hCs : n*Phi < 0 := by nlinarith
  have hCs2 : -36 ≤ n*Phi := by nlinarith
  have hCw0 : 0 < Cw := by linarith
  have hC : 0 < Cw + n*Phi := by linarith
  have hd : 0 ≤ Tb - Tr := by linarith
  have hd2 : Tb - Tr ≤ 81 := by linarith
  -- enthalpy of solution at the boiling point is still >= 0 (checked)
  have hE0 : 0 ≤ hsol + Phi*(Tb-Tr) := by nlinarith [mul_nonneg (by linarith : 0 ≤ Phi + 41) hd]
  have hE : 0 ≤ n*hsol + n*Phi*(Tb-Tr) := by nlinarith [mul_nonneg hn0.le hE0]
  have hstove : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-n*hsol)/(Cw+n*Phi)) := by
    intro H hH
    have : (H-n*hsol)/(Cw+n*Phi) ≤ Tb - Tr := by
      rw [div_le_iff₀ hC]; nlinarith [hH.2]
    nlinarith
  refine (compare_theorem Cw (n*Phi) (n*hsol) P h Tr T0 Tb hCw0 hC hh hP (by linarith) hstove
    T0 Ta le_rfl h1 h2).1 ?_
  intro x hx1 hx2
  nlinarith [mul_nonneg (by linarith : 0 ≤ Tb - x) (by linarith : 0 ≤ -(n*Phi))]
checks/d2511eb386bb475db1cec32245800f7d/submitted.lean
import Mathlib.Analysis.SpecialFunctions.Log.Deriv
import Mathlib.Analysis.Calculus.MeanValue

/-! Model (see report.txt).  H = enthalpy (J) measured from "everything at room temperature Tr".
 Pure water, heat capacity Cw: temperature Tr + H/Cw.
 Salt solution, total heat capacity Cw+Cs>0 (Cs of EITHER sign; literature: Cs<0), dissolution enthalpy D (at Tr) of either sign:
 temperature Tr + (H-D)/(Cw+Cs).  Salt (at Tr) is mixed in instantly, so H is conserved.
 Heater power P constant, loss h*(T-Tr):  dH/dt = P - h*(T-Tr).
 Separating variables, the time to move from H1 to H2 is Fw/Fs(H2) - Fw/Fs(H1) where
 Fw' = 1/(P - h*(H/Cw)), Fs' = 1/(P - h*((H-D)/(Cw+Cs))) (explicit logarithms below).
 Tb = boiling point of the final solution (fixed by salt amount, not by timing). -/

/-- time-integral of dH/(P - loss) for pure water (antiderivative, h>0) -/
noncomputable def Fw (Cw P h H : ℝ) : ℝ := -(Cw/h) * Real.log (P - h*(H/Cw))
/-- same for the salt solution -/
noncomputable def Fs (Cw Cs D P h H : ℝ) : ℝ := -((Cw+Cs)/h) * Real.log (P - h*((H-D)/(Cw+Cs)))

/-- Total time to reach Tb from T0 when the salt is added once the water is at Ta:
 heating water alone from T0 to Ta, then solution from the post-mixing state up to Tb. -/
noncomputable def boilTime (Cw Cs D P h Tr T0 Tb Ta : ℝ) : ℝ :=
  (Fw Cw P h (Cw*(Ta-Tr)) - Fw Cw P h (Cw*(T0-Tr))) +
  (Fs Cw Cs D P h ((Cw+Cs)*(Tb-Tr)+D) - Fs Cw Cs D P h (Cw*(Ta-Tr)))

lemma G_deriv (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs))) :
    ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), HasDerivAt (fun H => Fw Cw P h H - Fs Cw Cs D P h H)
      (1/(P - h*(H/Cw)) - 1/(P - h*((H-D)/(Cw+Cs)))) H := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  intro H hH
  have a1 : HasDerivAt (fun H : ℝ => P - h*(H/Cw)) (-(h/Cw)) H := by
    have := ((hasDerivAt_id H).div_const Cw).const_mul h
    simpa [div_eq_mul_inv, mul_comm] using (this.const_sub P)
  have a2 : HasDerivAt (fun H : ℝ => P - h*((H-D)/(Cw+Cs))) (-(h/(Cw+Cs))) H := by
    have := (((hasDerivAt_id H).sub_const D).div_const (Cw+Cs)).const_mul h
    simpa [div_eq_mul_inv, mul_comm] using (this.const_sub P)
  have l1 := (a1.log (hw H hH).ne').const_mul (-(Cw/h))
  have l2 := (a2.log (hs H hH).ne').const_mul (-((Cw+Cs)/h))
  have := l1.sub l2
  convert this using 1
  · funext x; rfl
  · have e1 := (hw H hH).ne'
    have e2 := (hs H hH).ne'
    have e3 := hh.ne'
    have e4 := hCw.ne'
    have e5 := hC.ne'
    field_simp

/-- Salt added at water temperature x changes the temperature at once by
 (D + Cs (x-Tr))/(Cw+Cs) downward (a cooling if D + Cs (x-Tr) > 0).  -/
lemma mix_iff (Cw Cs D Tr x : ℝ) (hCw : 0 < Cw) (hC : 0 < Cw + Cs) :
    (Cw*(x-Tr) - D)/(Cw+Cs) ≤ Cw*(x-Tr)/Cw ↔ 0 ≤ D + Cs*(x-Tr) := by
  rw [mul_div_cancel_left₀ _ hCw.ne', div_le_iff₀ hC]
  constructor <;> intro h <;> nlinarith

lemma G_mono (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs)))
    (S : Set ℝ) (hS : S ⊆ Set.Icc 0 (Cw*(Tb-Tr))) (hconv : Convex ℝ S)
    (sgn : ∀ H ∈ S, (H-D)/(Cw+Cs) ≤ H/Cw) :
    MonotoneOn (fun H => Fw Cw P h H - Fs Cw Cs D P h H) S := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  have hd := G_deriv Cw Cs D P h Tr Tb hCw hC hh hP hs
  apply monotoneOn_of_deriv_nonneg hconv
  · intro H hH; exact (hd H (hS hH)).continuousAt.continuousWithinAt
  · intro H hH; exact (hd H (hS (interior_subset hH))).differentiableAt.differentiableWithinAt
  · intro H hH
    have hH' := interior_subset hH
    rw [(hd H (hS hH')).deriv]
    apply sub_nonneg.mpr
    apply one_div_le_one_div_of_le (hw H (hS hH'))
    have := sgn H hH'
    nlinarith

lemma G_anti (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs)))
    (S : Set ℝ) (hS : S ⊆ Set.Icc 0 (Cw*(Tb-Tr))) (hconv : Convex ℝ S)
    (sgn : ∀ H ∈ S, H/Cw ≤ (H-D)/(Cw+Cs)) :
    AntitoneOn (fun H => Fw Cw P h H - Fs Cw Cs D P h H) S := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  have hd := G_deriv Cw Cs D P h Tr Tb hCw hC hh hP hs
  apply antitoneOn_of_deriv_nonpos hconv
  · intro H hH; exact (hd H (hS hH)).continuousAt.continuousWithinAt
  · intro H hH; exact (hd H (hS (interior_subset hH))).differentiableAt.differentiableWithinAt
  · intro H hH
    have hH' := interior_subset hH
    rw [(hd H (hS hH')).deriv]
    apply sub_nonpos.mpr
    apply one_div_le_one_div_of_le (hs H (hS hH'))
    have := sgn H hH'
    nlinarith

/-- Comparison lemma (sign-free; proved, used by the answer below).  Salt of fixed amount (Cs, D arbitrary sign, C=Cw+Cs>0).
 If between water temperatures Ta1 ≤ Ta2 salt addition always COOLS (D+Cs(x-Tr) ≥ 0 for x in [Ta1,Ta2]),
 adding at Ta1 is no slower than at Ta2; if it always WARMS (≤ 0), adding at Ta2 is no slower. -/
theorem compare_theorem : ∀ (Cw Cs D P h Tr T0 Tb : ℝ),
    0 < Cw → 0 < Cw + Cs → 0 < h → h*(Tb-Tr) < P → Tr ≤ T0 →
    (∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs))) →
    ∀ Ta1 Ta2 : ℝ, T0 ≤ Ta1 → Ta1 ≤ Ta2 → Ta2 ≤ Tb →
      ((∀ x, Ta1 ≤ x → x ≤ Ta2 → 0 ≤ D + Cs*(x-Tr)) →
          boilTime Cw Cs D P h Tr T0 Tb Ta1 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta2) ∧
      ((∀ x, Ta1 ≤ x → x ≤ Ta2 → D + Cs*(x-Tr) ≤ 0) →
          boilTime Cw Cs D P h Tr T0 Tb Ta2 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta1) := by
  intro Cw Cs D P h Tr T0 Tb hCw hC hh hP hTr hs Ta1 Ta2 h1 h12 h2
  have hmem : ∀ x, Ta1 ≤ x → x ≤ Ta2 → Cw*(x-Tr) ∈ Set.Icc 0 (Cw*(Tb-Tr)) := fun x a b =>
    ⟨by nlinarith, by nlinarith⟩
  have hS : Set.Icc (Cw*(Ta1-Tr)) (Cw*(Ta2-Tr)) ⊆ Set.Icc 0 (Cw*(Tb-Tr)) := by
    intro H hH
    exact ⟨by nlinarith [hH.1], by nlinarith [hH.2]⟩
  have hHx : ∀ H ∈ Set.Icc (Cw*(Ta1-Tr)) (Cw*(Ta2-Tr)), ∃ x, Ta1 ≤ x ∧ x ≤ Ta2 ∧ H = Cw*(x-Tr) := by
    intro H hH
    refine ⟨H/Cw + Tr, ?_, ?_, ?_⟩
    · have : Ta1 - Tr ≤ H/Cw := by rw [le_div_iff₀ hCw]; nlinarith [hH.1]
      linarith
    · have : H/Cw ≤ Ta2 - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
      linarith
    · field_simp; ring
  constructor
  · intro hx
    have m := G_mono Cw Cs D P h Tr Tb hCw hC hh hP hs _ hS (convex_Icc _ _) (by
      intro H hH
      obtain ⟨x, a, b, rfl⟩ := hHx H hH
      exact (mix_iff Cw Cs D Tr x hCw hC).mpr (hx x a b))
      ⟨le_rfl, by nlinarith⟩ ⟨by nlinarith, le_rfl⟩ (by nlinarith)
    simp only [boilTime]; simp only at m; linarith
  · intro hx
    have m := G_anti Cw Cs D P h Tr Tb hCw hC hh hP hs _ hS (convex_Icc _ _) (by
      intro H hH
      obtain ⟨x, a, b, rfl⟩ := hHx H hH
      have := hx x a b
      have h3 : ¬ ((Cw*(x-Tr) - D)/(Cw+Cs) < Cw*(x-Tr)/Cw) → True := fun _ => trivial
      rw [mul_div_cancel_left₀ _ hCw.ne', le_div_iff₀ hC]
      nlinarith)
      ⟨le_rfl, by nlinarith⟩ ⟨by nlinarith, le_rfl⟩ (by nlinarith)
    simp only [boilTime]; simp only at m; linarith


/-! ## Salt-specific part (NaCl, 1 L of water).  Inputs are labelled; nothing numerical below is a measurement.

 Choices.  Salt = NaCl.  1 L of water at 20 C has mass 0.998 kg (density 0.9982 kg/L).  The salt/water ratio
 r = (salt mass)/(water mass) (NOT the mass fraction of the final solution; that is w = r/(1+r), proved in
 `cs_theorem`).  Regime: 0.02 <= r <= 0.05, i.e. about 20-50 g NaCl in the litre (2-4.8 wt%).
 Cs is DERIVED, not fitted: by definition of the apparent molar heat capacity Phi of the salt in solution,
 (total heat capacity of solution) = (heat capacity of the water) + n*Phi, so Cs = n*Phi, n = moles of salt.
 Phi is temperature dependent; the lumped model needs one constant Cs, so Phi is the SECANT MEAN of Phi(T,m)
 over 20 C..Tb, which makes the linear mixing enthalpy D + Cs (T-Tr) exact at both 20 C and Tb.
 Empirical inputs (EXPOSED PREMISES, constants below), from the Laliberte (2009) NaCl correlation (fit range
 1.5-120 C, w <= 0.26) evaluated numerically outside Lean (a synthetic calculation from a published correlation)
 and from NBS tables: secant-mean Phi in [-41,-27] J/K/mol for r in [0.02,0.05] (computed -40.7 ... -27.9);
 enthalpy of solution of NaCl(s) hsol >= 3800 J/mol (NBS +3.88 kJ/mol); boiling point Tb in [100,101] C;
 pot+water heat capacity >= 4170 J/K (at least the 0.998 kg of water at 4.18 J/g/K).
 D = n*hsol is the enthalpy absorbed when salt at room temperature Tr dissolves at Tr. -/

/-- room / initial water temperature, C -/
def Tr0 : ℝ := 20
/-- admissible range of the boiling point of the salt solution, C -/
def TbLo : ℝ := 100
def TbHi : ℝ := 101
/-- minimum heat capacity of pot + water, J/K -/
def CwMin : ℝ := 4170
/-- salt/water mass-ratio regime -/
def rLo : ℝ := 1/50
def rHi : ℝ := 1/20
/-- NaCl molar mass g/mol, mass of 1 L of water at 20 C in kg -/
def Mnacl : ℝ := 58.44
def massWater : ℝ := 0.998
/-- moles of NaCl for salt/water mass ratio r (1 kg = 1000 g) -/
noncomputable def nSalt (r : ℝ) : ℝ := 1000*massWater*r/Mnacl
/-- empirical bounds of the secant-mean apparent molar heat capacity of NaCl(aq) over 20 C..Tb, J/K/mol -/
def PhiLo : ℝ := -41
def PhiHi : ℝ := -27
/-- lower bound of the enthalpy of solution of NaCl(s) at room temperature, J/mol -/
def hsolMin : ℝ := 3800

lemma nSalt_pos (r : ℝ) (h : rLo ≤ r) : 0 < nSalt r := by
  unfold nSalt massWater Mnacl; unfold rLo at h; positivity

lemma nSalt_le (r : ℝ) (h : r ≤ rHi) : nSalt r ≤ 0.8540 := by
  unfold nSalt massWater Mnacl
  unfold rHi at h
  rw [div_le_iff₀ (by norm_num)]; nlinarith

/-- Cs = n*Phi: sign, size, and the ratio conversion. -/
theorem cs_theorem : ∀ (r Phi ms mwater : ℝ),
    rLo ≤ r → r ≤ rHi → PhiLo ≤ Phi → Phi ≤ PhiHi → 0 < ms → 0 < mwater →
    (nSalt r * Phi < 0 ∧ -36 ≤ nSalt r * Phi) ∧ ms/(ms+mwater) = (ms/mwater)/(1+ms/mwater) := by
  intro r Phi ms mwater hr1 hr2 hP1 hP2 hms hmwa
  have hn0 := nSalt_pos r hr1
  have hn1 := nSalt_le r hr2
  unfold PhiLo at hP1; unfold PhiHi at hP2
  refine ⟨⟨by nlinarith, by nlinarith⟩, ?_⟩
  field_simp
  ring

/-- ANSWER.  Accepted model (lumped heat capacity Cw, constant stove power P, Newton loss h, no evaporation,
 salt at room temperature Tr0 = T0 dissolves instantly, enthalpy conserved).  Under the premises above
 (r in [rLo,rHi], Cs = n*Phi, D = n*hsol, Tb in [TbLo,TbHi], stove stronger than losses at Tb), adding the
 salt right at the start (Ta = T0 = Tr0) is no slower than adding it at any later water temperature Ta <= Tb.
 The condition D + Cs (x-Tr) >= 0 (salt addition cools / does not warm) is CHECKED here, not assumed, and
 the 'stove beats losses' condition for the salt solution is derived from it. -/
theorem answer_theorem : ∀ (Cw P h Tb r Phi hsol : ℝ),
    TbLo ≤ Tb → Tb ≤ TbHi → CwMin ≤ Cw → 0 < h → h*(Tb-Tr0) < P →
    rLo ≤ r → r ≤ rHi → PhiLo ≤ Phi → Phi ≤ PhiHi → hsolMin ≤ hsol →
    ∀ Ta : ℝ, Tr0 ≤ Ta → Ta ≤ Tb →
      boilTime Cw (nSalt r*Phi) (nSalt r*hsol) P h Tr0 Tr0 Tb Tr0
        ≤ boilTime Cw (nSalt r*Phi) (nSalt r*hsol) P h Tr0 Tr0 Tb Ta := by
  intro Cw P h Tb r Phi hsol hb1 hb2 hCw hh hP hr1 hr2 hP1 hP2 hs Ta h1 h2
  have hn0 := nSalt_pos r hr1
  have hn1 := nSalt_le r hr2
  generalize nSalt r = n at hn0 hn1 ⊢
  unfold TbLo at hb1; unfold TbHi at hb2; unfold CwMin at hCw
  unfold PhiLo at hP1; unfold PhiHi at hP2; unfold hsolMin at hs
  have hTr : Tr0 = 20 := rfl
  rw [hTr] at hP h1 ⊢
  have hCs : n*Phi < 0 := by nlinarith
  have hCs2 : -36 ≤ n*Phi := by nlinarith
  have hCw0 : 0 < Cw := by linarith
  have hC : 0 < Cw + n*Phi := by linarith
  have hd : 0 ≤ Tb - 20 := by linarith
  have hd2 : Tb - 20 ≤ 81 := by linarith
  -- enthalpy of solution at the boiling point is still >= 0 (checked)
  have hE0 : 0 ≤ hsol + Phi*(Tb-20) := by nlinarith [mul_nonneg (by linarith : 0 ≤ Phi + 41) hd]
  have hE : 0 ≤ n*hsol + n*Phi*(Tb-20) := by nlinarith [mul_nonneg hn0.le hE0]
  have hstove : ∀ H ∈ Set.Icc 0 (Cw*(Tb-20)), 0 < P - h*((H-n*hsol)/(Cw+n*Phi)) := by
    intro H hH
    have : (H-n*hsol)/(Cw+n*Phi) ≤ Tb - 20 := by
      rw [div_le_iff₀ hC]; nlinarith [hH.2]
    nlinarith
  refine (compare_theorem Cw (n*Phi) (n*hsol) P h 20 20 Tb hCw0 hC hh hP (by linarith) hstove
    20 Ta le_rfl h1 h2).1 ?_
  intro x hx1 hx2
  nlinarith [mul_nonneg (by linarith : 0 ≤ Tb - x) (by linarith : 0 ≤ -(n*Phi))]
checks/d34803db1d874cbd871ed1ec8eedf419/submitted.lean
import Mathlib.Analysis.SpecialFunctions.Log.Deriv
import Mathlib.Analysis.Calculus.MeanValue

/-! Model (see report.txt).  H = enthalpy (J) measured from "everything at room temperature Tr".
 Pure water, heat capacity Cw: temperature Tr + H/Cw.
 Salt solution, total heat capacity Cw+Cs>0 (Cs of EITHER sign; literature: Cs<0), dissolution enthalpy D (at Tr) of either sign:
 temperature Tr + (H-D)/(Cw+Cs).  Salt (at Tr) is mixed in instantly, so H is conserved.
 Heater power P constant, loss h*(T-Tr):  dH/dt = P - h*(T-Tr).
 Separating variables, the time to move from H1 to H2 is Fw/Fs(H2) - Fw/Fs(H1) where
 Fw' = 1/(P - h*(H/Cw)), Fs' = 1/(P - h*((H-D)/(Cw+Cs))) (explicit logarithms below).
 Tb = boiling point of the final solution (fixed by salt amount, not by timing). -/

/-- time-integral of dH/(P - loss) for pure water (antiderivative, h>0) -/
noncomputable def Fw (Cw P h H : ℝ) : ℝ := -(Cw/h) * Real.log (P - h*(H/Cw))
/-- same for the salt solution -/
noncomputable def Fs (Cw Cs D P h H : ℝ) : ℝ := -((Cw+Cs)/h) * Real.log (P - h*((H-D)/(Cw+Cs)))

/-- Total time to reach Tb from T0 when the salt is added once the water is at Ta:
 heating water alone from T0 to Ta, then solution from the post-mixing state up to Tb. -/
noncomputable def boilTime (Cw Cs D P h Tr T0 Tb Ta : ℝ) : ℝ :=
  (Fw Cw P h (Cw*(Ta-Tr)) - Fw Cw P h (Cw*(T0-Tr))) +
  (Fs Cw Cs D P h ((Cw+Cs)*(Tb-Tr)+D) - Fs Cw Cs D P h (Cw*(Ta-Tr)))

lemma G_deriv (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs))) :
    ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), HasDerivAt (fun H => Fw Cw P h H - Fs Cw Cs D P h H)
      (1/(P - h*(H/Cw)) - 1/(P - h*((H-D)/(Cw+Cs)))) H := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  intro H hH
  have a1 : HasDerivAt (fun H : ℝ => P - h*(H/Cw)) (-(h/Cw)) H := by
    have := ((hasDerivAt_id H).div_const Cw).const_mul h
    simpa [div_eq_mul_inv, mul_comm] using (this.const_sub P)
  have a2 : HasDerivAt (fun H : ℝ => P - h*((H-D)/(Cw+Cs))) (-(h/(Cw+Cs))) H := by
    have := (((hasDerivAt_id H).sub_const D).div_const (Cw+Cs)).const_mul h
    simpa [div_eq_mul_inv, mul_comm] using (this.const_sub P)
  have l1 := (a1.log (hw H hH).ne').const_mul (-(Cw/h))
  have l2 := (a2.log (hs H hH).ne').const_mul (-((Cw+Cs)/h))
  have := l1.sub l2
  convert this using 1
  · funext x; rfl
  · have e1 := (hw H hH).ne'
    have e2 := (hs H hH).ne'
    have e3 := hh.ne'
    have e4 := hCw.ne'
    have e5 := hC.ne'
    field_simp

/-- Salt added at water temperature x changes the temperature at once by
 (D + Cs (x-Tr))/(Cw+Cs) downward (a cooling if D + Cs (x-Tr) > 0).  -/
lemma mix_iff (Cw Cs D Tr x : ℝ) (hCw : 0 < Cw) (hC : 0 < Cw + Cs) :
    (Cw*(x-Tr) - D)/(Cw+Cs) ≤ Cw*(x-Tr)/Cw ↔ 0 ≤ D + Cs*(x-Tr) := by
  rw [mul_div_cancel_left₀ _ hCw.ne', div_le_iff₀ hC]
  constructor <;> intro h <;> nlinarith

lemma G_mono (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs)))
    (S : Set ℝ) (hS : S ⊆ Set.Icc 0 (Cw*(Tb-Tr))) (hconv : Convex ℝ S)
    (sgn : ∀ H ∈ S, (H-D)/(Cw+Cs) ≤ H/Cw) :
    MonotoneOn (fun H => Fw Cw P h H - Fs Cw Cs D P h H) S := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  have hd := G_deriv Cw Cs D P h Tr Tb hCw hC hh hP hs
  apply monotoneOn_of_deriv_nonneg hconv
  · intro H hH; exact (hd H (hS hH)).continuousAt.continuousWithinAt
  · intro H hH; exact (hd H (hS (interior_subset hH))).differentiableAt.differentiableWithinAt
  · intro H hH
    have hH' := interior_subset hH
    rw [(hd H (hS hH')).deriv]
    apply sub_nonneg.mpr
    apply one_div_le_one_div_of_le (hw H (hS hH'))
    have := sgn H hH'
    nlinarith

lemma G_anti (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs)))
    (S : Set ℝ) (hS : S ⊆ Set.Icc 0 (Cw*(Tb-Tr))) (hconv : Convex ℝ S)
    (sgn : ∀ H ∈ S, H/Cw ≤ (H-D)/(Cw+Cs)) :
    AntitoneOn (fun H => Fw Cw P h H - Fs Cw Cs D P h H) S := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  have hd := G_deriv Cw Cs D P h Tr Tb hCw hC hh hP hs
  apply antitoneOn_of_deriv_nonpos hconv
  · intro H hH; exact (hd H (hS hH)).continuousAt.continuousWithinAt
  · intro H hH; exact (hd H (hS (interior_subset hH))).differentiableAt.differentiableWithinAt
  · intro H hH
    have hH' := interior_subset hH
    rw [(hd H (hS hH')).deriv]
    apply sub_nonpos.mpr
    apply one_div_le_one_div_of_le (hs H (hS hH'))
    have := sgn H hH'
    nlinarith

/-- Comparison lemma (sign-free; proved, used by the answer below).  Salt of fixed amount (Cs, D arbitrary sign, C=Cw+Cs>0).
 If between water temperatures Ta1 ≤ Ta2 salt addition always COOLS (D+Cs(x-Tr) ≥ 0 for x in [Ta1,Ta2]),
 adding at Ta1 is no slower than at Ta2; if it always WARMS (≤ 0), adding at Ta2 is no slower. -/
theorem compare_theorem : ∀ (Cw Cs D P h Tr T0 Tb : ℝ),
    0 < Cw → 0 < Cw + Cs → 0 < h → h*(Tb-Tr) < P → Tr ≤ T0 →
    (∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs))) →
    ∀ Ta1 Ta2 : ℝ, T0 ≤ Ta1 → Ta1 ≤ Ta2 → Ta2 ≤ Tb →
      ((∀ x, Ta1 ≤ x → x ≤ Ta2 → 0 ≤ D + Cs*(x-Tr)) →
          boilTime Cw Cs D P h Tr T0 Tb Ta1 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta2) ∧
      ((∀ x, Ta1 ≤ x → x ≤ Ta2 → D + Cs*(x-Tr) ≤ 0) →
          boilTime Cw Cs D P h Tr T0 Tb Ta2 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta1) := by
  intro Cw Cs D P h Tr T0 Tb hCw hC hh hP hTr hs Ta1 Ta2 h1 h12 h2
  have hmem : ∀ x, Ta1 ≤ x → x ≤ Ta2 → Cw*(x-Tr) ∈ Set.Icc 0 (Cw*(Tb-Tr)) := fun x a b =>
    ⟨by nlinarith, by nlinarith⟩
  have hS : Set.Icc (Cw*(Ta1-Tr)) (Cw*(Ta2-Tr)) ⊆ Set.Icc 0 (Cw*(Tb-Tr)) := by
    intro H hH
    exact ⟨by nlinarith [hH.1], by nlinarith [hH.2]⟩
  have hHx : ∀ H ∈ Set.Icc (Cw*(Ta1-Tr)) (Cw*(Ta2-Tr)), ∃ x, Ta1 ≤ x ∧ x ≤ Ta2 ∧ H = Cw*(x-Tr) := by
    intro H hH
    refine ⟨H/Cw + Tr, ?_, ?_, ?_⟩
    · have : Ta1 - Tr ≤ H/Cw := by rw [le_div_iff₀ hCw]; nlinarith [hH.1]
      linarith
    · have : H/Cw ≤ Ta2 - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
      linarith
    · field_simp; ring
  constructor
  · intro hx
    have m := G_mono Cw Cs D P h Tr Tb hCw hC hh hP hs _ hS (convex_Icc _ _) (by
      intro H hH
      obtain ⟨x, a, b, rfl⟩ := hHx H hH
      exact (mix_iff Cw Cs D Tr x hCw hC).mpr (hx x a b))
      ⟨le_rfl, by nlinarith⟩ ⟨by nlinarith, le_rfl⟩ (by nlinarith)
    simp only [boilTime]; simp only at m; linarith
  · intro hx
    have m := G_anti Cw Cs D P h Tr Tb hCw hC hh hP hs _ hS (convex_Icc _ _) (by
      intro H hH
      obtain ⟨x, a, b, rfl⟩ := hHx H hH
      have := hx x a b
      have h3 : ¬ ((Cw*(x-Tr) - D)/(Cw+Cs) < Cw*(x-Tr)/Cw) → True := fun _ => trivial
      rw [mul_div_cancel_left₀ _ hCw.ne', le_div_iff₀ hC]
      nlinarith)
      ⟨le_rfl, by nlinarith⟩ ⟨by nlinarith, le_rfl⟩ (by nlinarith)
    simp only [boilTime]; simp only at m; linarith


/-! ## Salt-specific part (NaCl, 1 L of water).  Inputs are labelled; nothing numerical below is a measurement.

 Choices.  Salt = NaCl, molar mass M = 58.44 g/mol.  1 L of water at 20 C has mass mw = 0.998 kg
 (density 0.9982 kg/L).  The salt/water ratio r = (salt mass)/(water mass) (NOT the mass fraction of the
 final solution; the fraction is w = r/(1+r), proved in `cs_theorem`).  Regime: 0.02 <= r <= 0.05,
 i.e. about 20-50 g NaCl in the litre (2-4.8 wt%; seawater-like to very salty cooking water).
 Cs is DERIVED, not fitted: by definition of the apparent molar heat capacity Phi of the salt in solution,
 (total heat capacity of solution) = (heat capacity of the water) + n*Phi, so Cs = n*Phi, n = moles of salt.
 Phi is temperature dependent; the lumped model needs one constant Cs, so Phi below is the SECANT MEAN of
 Phi(T,m) over 20 C..Tb, which makes the linear mixing-enthalpy D + Cs (T-Tr) exact at both ends 20 C and Tb.
 Empirical inputs (EXPOSED PREMISES, from the Laliberte (2009) NaCl correlation, fit range 1.5-120 C,
 w <= 0.26, evaluated numerically outside Lean -- a synthetic calculation from a published correlation):
   * secant-mean Phi in [-41, -27] J/K/mol for r in [0.02, 0.05] (computed: -40.7 at r=0.02 ... -27.9 at 0.05);
   * enthalpy of solution of NaCl(s) at ~20-25 C, hsol >= 3800 J/mol (NBS value +3.88 kJ/mol, endothermic);
   * boiling point of the solution Tb in [100, 101] C (elevation <= ~1 K for these amounts);
   * pot+water heat capacity Cw >= 4170 J/K (at least the 0.998 kg of water, 4.18 J/g/K).
 D = n*hsol is the enthalpy absorbed when the salt (at room temperature Tr) dissolves at Tr. -/

/-- Cs = n*Phi: sign, size, and the ratio conversion. -/
theorem cs_theorem : ∀ (r M mw n Phi ms mwater : ℝ),
    0.02 ≤ r → r ≤ 0.05 → M = 58.44 → mw = 0.998 → n = 1000*mw*r/M →
    -41 ≤ Phi → Phi ≤ -27 → 0 < ms → 0 < mwater →
    (n*Phi < 0 ∧ -36 ≤ n*Phi) ∧ ms/(ms+mwater) = (ms/mwater)/(1+ms/mwater) := by
  intro r M mw n Phi ms mwater hr1 hr2 hM hmw hn hP1 hP2 hms hmwa
  have hn0 : 0 < n := by rw [hn, hM, hmw]; positivity
  have hn1 : n ≤ 0.8540 := by
    rw [hn, hM, hmw, div_le_iff₀ (by norm_num)]; nlinarith
  refine ⟨⟨by nlinarith, by nlinarith⟩, ?_⟩
  field_simp
  ring

/-- ANSWER.  Accepted model (lumped heat capacity Cw, constant stove power P, Newton loss h, no evaporation,
 salt at room temperature Tr = T0 = 20 C dissolves instantly, enthalpy conserved).  Under the premises above
 (r in [0.02,0.05], Cs = n*Phi, D = n*hsol, Tb in [100,101], stove stronger than losses at Tb), adding the
 salt right at the start (Ta = T0) is no slower than adding it at any later water temperature Ta in [T0,Tb].
 The condition D + Cs (x-Tr) >= 0 (salt addition cools / does not warm) is CHECKED here, not assumed, and
 the 'stove beats losses' condition for the salt solution is derived from it. -/
theorem answer_theorem : ∀ (Cw P h Tr T0 Tb r M mw n Phi hsol : ℝ),
    Tr = 20 → T0 = 20 → 100 ≤ Tb → Tb ≤ 101 → 4170 ≤ Cw → 0 < h → h*(Tb-Tr) < P →
    0.02 ≤ r → r ≤ 0.05 → M = 58.44 → mw = 0.998 → n = 1000*mw*r/M →
    -41 ≤ Phi → Phi ≤ -27 → 3800 ≤ hsol →
    ∀ Ta : ℝ, T0 ≤ Ta → Ta ≤ Tb →
      boilTime Cw (n*Phi) (n*hsol) P h Tr T0 Tb T0 ≤ boilTime Cw (n*Phi) (n*hsol) P h Tr T0 Tb Ta := by
  intro Cw P h Tr T0 Tb r M mw n Phi hsol hTr hT0 hb1 hb2 hCw hh hP hr1 hr2 hM hmw hn hP1 hP2 hs Ta h1 h2
  have hn0 : 0 < n := by rw [hn, hM, hmw]; positivity
  have hn1 : n ≤ 0.8540 := by
    rw [hn, hM, hmw, div_le_iff₀ (by norm_num)]; nlinarith
  have hCs : n*Phi < 0 := by nlinarith
  have hCs2 : -36 ≤ n*Phi := by nlinarith
  have hCw0 : 0 < Cw := by linarith
  have hC : 0 < Cw + n*Phi := by linarith
  have hd : 0 ≤ Tb - Tr := by linarith
  have hd2 : Tb - Tr ≤ 81 := by linarith
  -- enthalpy of solution at the boiling point is still >= 0 (checked)
  have hE0 : 0 ≤ hsol + Phi*(Tb-Tr) := by nlinarith [mul_nonneg (by linarith : 0 ≤ Phi + 41) hd]
  have hE : 0 ≤ n*hsol + n*Phi*(Tb-Tr) := by nlinarith [mul_nonneg hn0.le hE0]
  have hstove : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-n*hsol)/(Cw+n*Phi)) := by
    intro H hH
    have : (H-n*hsol)/(Cw+n*Phi) ≤ Tb - Tr := by
      rw [div_le_iff₀ hC]; nlinarith [hH.2]
    nlinarith
  refine (compare_theorem Cw (n*Phi) (n*hsol) P h Tr T0 Tb hCw0 hC hh hP (by linarith) hstove
    T0 Ta le_rfl h1 h2).1 ?_
  intro x hx1 hx2
  nlinarith [mul_nonneg (by linarith : 0 ≤ Tb - x) (by linarith : 0 ≤ -(n*Phi))]
checks/dcd7a1b461ab4ea1832bd51aed75b96c/submitted.lean
import Mathlib.Analysis.SpecialFunctions.Log.Deriv
import Mathlib.Analysis.Calculus.MeanValue

/-! Model (see report.txt).  H = enthalpy (J) measured from "everything at room temperature Tr".
 Pure water, heat capacity Cw: temperature Tr + H/Cw.
 Salt solution, total heat capacity Cw+Cs>0 (Cs of EITHER sign; literature: Cs<0), dissolution enthalpy D (at Tr) of either sign:
 temperature Tr + (H-D)/(Cw+Cs).  Salt (at Tr) is mixed in instantly, so H is conserved.
 Heater power P constant, loss h*(T-Tr):  dH/dt = P - h*(T-Tr).
 Separating variables, the time to move from H1 to H2 is Fw/Fs(H2) - Fw/Fs(H1) where
 Fw' = 1/(P - h*(H/Cw)), Fs' = 1/(P - h*((H-D)/(Cw+Cs))) (explicit logarithms below).
 Tb = boiling point of the final solution (fixed by salt amount, not by timing). -/

/-- time-integral of dH/(P - loss) for pure water (antiderivative, h>0) -/
noncomputable def Fw (Cw P h H : ℝ) : ℝ := -(Cw/h) * Real.log (P - h*(H/Cw))
/-- same for the salt solution -/
noncomputable def Fs (Cw Cs D P h H : ℝ) : ℝ := -((Cw+Cs)/h) * Real.log (P - h*((H-D)/(Cw+Cs)))

/-- Total time to reach Tb from T0 when the salt is added once the water is at Ta:
 heating water alone from T0 to Ta, then solution from the post-mixing state up to Tb. -/
noncomputable def boilTime (Cw Cs D P h Tr T0 Tb Ta : ℝ) : ℝ :=
  (Fw Cw P h (Cw*(Ta-Tr)) - Fw Cw P h (Cw*(T0-Tr))) +
  (Fs Cw Cs D P h ((Cw+Cs)*(Tb-Tr)+D) - Fs Cw Cs D P h (Cw*(Ta-Tr)))

lemma G_deriv (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs))) :
    ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), HasDerivAt (fun H => Fw Cw P h H - Fs Cw Cs D P h H)
      (1/(P - h*(H/Cw)) - 1/(P - h*((H-D)/(Cw+Cs)))) H := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  intro H hH
  have a1 : HasDerivAt (fun H : ℝ => P - h*(H/Cw)) (-(h/Cw)) H := by
    have := ((hasDerivAt_id H).div_const Cw).const_mul h
    simpa [div_eq_mul_inv, mul_comm] using (this.const_sub P)
  have a2 : HasDerivAt (fun H : ℝ => P - h*((H-D)/(Cw+Cs))) (-(h/(Cw+Cs))) H := by
    have := (((hasDerivAt_id H).sub_const D).div_const (Cw+Cs)).const_mul h
    simpa [div_eq_mul_inv, mul_comm] using (this.const_sub P)
  have l1 := (a1.log (hw H hH).ne').const_mul (-(Cw/h))
  have l2 := (a2.log (hs H hH).ne').const_mul (-((Cw+Cs)/h))
  have := l1.sub l2
  convert this using 1
  · funext x; rfl
  · have e1 := (hw H hH).ne'
    have e2 := (hs H hH).ne'
    have e3 := hh.ne'
    have e4 := hCw.ne'
    have e5 := hC.ne'
    field_simp

/-- Salt added at water temperature x changes the temperature at once by
 (D + Cs (x-Tr))/(Cw+Cs) downward (a cooling if D + Cs (x-Tr) > 0).  -/
lemma mix_iff (Cw Cs D Tr x : ℝ) (hCw : 0 < Cw) (hC : 0 < Cw + Cs) :
    (Cw*(x-Tr) - D)/(Cw+Cs) ≤ Cw*(x-Tr)/Cw ↔ 0 ≤ D + Cs*(x-Tr) := by
  rw [mul_div_cancel_left₀ _ hCw.ne', div_le_iff₀ hC]
  constructor <;> intro h <;> nlinarith

lemma G_mono (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs)))
    (S : Set ℝ) (hS : S ⊆ Set.Icc 0 (Cw*(Tb-Tr))) (hconv : Convex ℝ S)
    (sgn : ∀ H ∈ S, (H-D)/(Cw+Cs) ≤ H/Cw) :
    MonotoneOn (fun H => Fw Cw P h H - Fs Cw Cs D P h H) S := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  have hd := G_deriv Cw Cs D P h Tr Tb hCw hC hh hP hs
  apply monotoneOn_of_deriv_nonneg hconv
  · intro H hH; exact (hd H (hS hH)).continuousAt.continuousWithinAt
  · intro H hH; exact (hd H (hS (interior_subset hH))).differentiableAt.differentiableWithinAt
  · intro H hH
    have hH' := interior_subset hH
    rw [(hd H (hS hH')).deriv]
    apply sub_nonneg.mpr
    apply one_div_le_one_div_of_le (hw H (hS hH'))
    have := sgn H hH'
    nlinarith

lemma G_anti (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs)))
    (S : Set ℝ) (hS : S ⊆ Set.Icc 0 (Cw*(Tb-Tr))) (hconv : Convex ℝ S)
    (sgn : ∀ H ∈ S, H/Cw ≤ (H-D)/(Cw+Cs)) :
    AntitoneOn (fun H => Fw Cw P h H - Fs Cw Cs D P h H) S := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  have hd := G_deriv Cw Cs D P h Tr Tb hCw hC hh hP hs
  apply antitoneOn_of_deriv_nonpos hconv
  · intro H hH; exact (hd H (hS hH)).continuousAt.continuousWithinAt
  · intro H hH; exact (hd H (hS (interior_subset hH))).differentiableAt.differentiableWithinAt
  · intro H hH
    have hH' := interior_subset hH
    rw [(hd H (hS hH')).deriv]
    apply sub_nonpos.mpr
    apply one_div_le_one_div_of_le (hs H (hS hH'))
    have := sgn H hH'
    nlinarith

/-- Comparison lemma (sign-free; proved, used by the answer below).  Salt of fixed amount (Cs, D arbitrary sign, C=Cw+Cs>0).
 If between water temperatures Ta1 ≤ Ta2 salt addition always COOLS (D+Cs(x-Tr) ≥ 0 for x in [Ta1,Ta2]),
 adding at Ta1 is no slower than at Ta2; if it always WARMS (≤ 0), adding at Ta2 is no slower. -/
theorem compare_theorem : ∀ (Cw Cs D P h Tr T0 Tb : ℝ),
    0 < Cw → 0 < Cw + Cs → 0 < h → h*(Tb-Tr) < P → Tr ≤ T0 →
    (∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs))) →
    ∀ Ta1 Ta2 : ℝ, T0 ≤ Ta1 → Ta1 ≤ Ta2 → Ta2 ≤ Tb →
      ((∀ x, Ta1 ≤ x → x ≤ Ta2 → 0 ≤ D + Cs*(x-Tr)) →
          boilTime Cw Cs D P h Tr T0 Tb Ta1 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta2) ∧
      ((∀ x, Ta1 ≤ x → x ≤ Ta2 → D + Cs*(x-Tr) ≤ 0) →
          boilTime Cw Cs D P h Tr T0 Tb Ta2 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta1) := by
  intro Cw Cs D P h Tr T0 Tb hCw hC hh hP hTr hs Ta1 Ta2 h1 h12 h2
  have hmem : ∀ x, Ta1 ≤ x → x ≤ Ta2 → Cw*(x-Tr) ∈ Set.Icc 0 (Cw*(Tb-Tr)) := fun x a b =>
    ⟨by nlinarith, by nlinarith⟩
  have hS : Set.Icc (Cw*(Ta1-Tr)) (Cw*(Ta2-Tr)) ⊆ Set.Icc 0 (Cw*(Tb-Tr)) := by
    intro H hH
    exact ⟨by nlinarith [hH.1], by nlinarith [hH.2]⟩
  have hHx : ∀ H ∈ Set.Icc (Cw*(Ta1-Tr)) (Cw*(Ta2-Tr)), ∃ x, Ta1 ≤ x ∧ x ≤ Ta2 ∧ H = Cw*(x-Tr) := by
    intro H hH
    refine ⟨H/Cw + Tr, ?_, ?_, ?_⟩
    · have : Ta1 - Tr ≤ H/Cw := by rw [le_div_iff₀ hCw]; nlinarith [hH.1]
      linarith
    · have : H/Cw ≤ Ta2 - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
      linarith
    · field_simp; ring
  constructor
  · intro hx
    have m := G_mono Cw Cs D P h Tr Tb hCw hC hh hP hs _ hS (convex_Icc _ _) (by
      intro H hH
      obtain ⟨x, a, b, rfl⟩ := hHx H hH
      exact (mix_iff Cw Cs D Tr x hCw hC).mpr (hx x a b))
      ⟨le_rfl, by nlinarith⟩ ⟨by nlinarith, le_rfl⟩ (by nlinarith)
    simp only [boilTime]; simp only at m; linarith
  · intro hx
    have m := G_anti Cw Cs D P h Tr Tb hCw hC hh hP hs _ hS (convex_Icc _ _) (by
      intro H hH
      obtain ⟨x, a, b, rfl⟩ := hHx H hH
      have := hx x a b
      have h3 : ¬ ((Cw*(x-Tr) - D)/(Cw+Cs) < Cw*(x-Tr)/Cw) → True := fun _ => trivial
      rw [mul_div_cancel_left₀ _ hCw.ne', le_div_iff₀ hC]
      nlinarith)
      ⟨le_rfl, by nlinarith⟩ ⟨by nlinarith, le_rfl⟩ (by nlinarith)
    simp only [boilTime]; simp only at m; linarith


/-! ## Salt-specific part (NaCl, 1 L of water).  Inputs are labelled; nothing numerical below is a measurement.

 Choices.  Salt = NaCl, molar mass M = 58.44 g/mol.  1 L of water at 20 C has mass mw = 0.998 kg
 (density 0.9982 kg/L).  The salt/water ratio r = (salt mass)/(water mass) (NOT the mass fraction of the
 final solution; the fraction is w = r/(1+r), proved in `cs_theorem`).  Regime: 0.02 <= r <= 0.05,
 i.e. about 20-50 g NaCl in the litre (2-4.8 wt%; seawater-like to very salty cooking water).
 Cs is DERIVED, not fitted: by definition of the apparent molar heat capacity Phi of the salt in solution,
 (total heat capacity of solution) = (heat capacity of the water) + n*Phi, so Cs = n*Phi, n = moles of salt.
 Phi is temperature dependent; the lumped model needs one constant Cs, so Phi below is the SECANT MEAN of
 Phi(T,m) over 20 C..Tb, which makes the linear mixing-enthalpy D + Cs (T-Tr) exact at both ends 20 C and Tb.
 Empirical inputs (EXPOSED PREMISES, from the Laliberte (2009) NaCl correlation, fit range 1.5-120 C,
 w <= 0.26, evaluated numerically outside Lean -- a synthetic calculation from a published correlation):
   * secant-mean Phi in [-41, -27] J/K/mol for r in [0.02, 0.05] (computed: -40.7 at r=0.02 ... -27.9 at 0.05);
   * enthalpy of solution of NaCl(s) at ~20-25 C, hsol >= 3800 J/mol (NBS value +3.88 kJ/mol, endothermic);
   * boiling point of the solution Tb in [100, 101] C (elevation <= ~1 K for these amounts);
   * pot+water heat capacity Cw >= 4170 J/K (at least the 0.998 kg of water, 4.18 J/g/K).
 D = n*hsol is the enthalpy absorbed when the salt (at room temperature Tr) dissolves at Tr. -/

/-- Cs = n*Phi: sign, size, and the ratio conversion. -/
theorem cs_theorem : ∀ (r M mw n Phi ms mwater : ℝ),
    0.02 ≤ r → r ≤ 0.05 → M = 58.44 → mw = 0.998 → n = 1000*mw*r/M →
    -41 ≤ Phi → Phi ≤ -27 → 0 < ms → 0 < mwater →
    (n*Phi < 0 ∧ -36 ≤ n*Phi) ∧ ms/(ms+mwater) = (ms/mwater)/(1+ms/mwater) := by
  intro r M mw n Phi ms mwater hr1 hr2 hM hmw hn hP1 hP2 hms hmwa
  have hn0 : 0 < n := by rw [hn, hM, hmw]; positivity
  have hn1 : n ≤ 0.8540 := by
    rw [hn, hM, hmw, div_le_iff₀ (by norm_num)]; nlinarith
  refine ⟨⟨by nlinarith, by nlinarith⟩, ?_⟩
  field_simp

/-- ANSWER.  Accepted model (lumped heat capacity Cw, constant stove power P, Newton loss h, no evaporation,
 salt at room temperature Tr = T0 = 20 C dissolves instantly, enthalpy conserved).  Under the premises above
 (r in [0.02,0.05], Cs = n*Phi, D = n*hsol, Tb in [100,101], stove stronger than losses at Tb), adding the
 salt right at the start (Ta = T0) is no slower than adding it at any later water temperature Ta in [T0,Tb].
 The condition D + Cs (x-Tr) >= 0 (salt addition cools / does not warm) is CHECKED here, not assumed, and
 the 'stove beats losses' condition for the salt solution is derived from it. -/
theorem answer_theorem : ∀ (Cw P h Tr T0 Tb r M mw n Phi hsol : ℝ),
    Tr = 20 → T0 = 20 → 100 ≤ Tb → Tb ≤ 101 → 4170 ≤ Cw → 0 < h → h*(Tb-Tr) < P →
    0.02 ≤ r → r ≤ 0.05 → M = 58.44 → mw = 0.998 → n = 1000*mw*r/M →
    -41 ≤ Phi → Phi ≤ -27 → 3800 ≤ hsol →
    ∀ Ta : ℝ, T0 ≤ Ta → Ta ≤ Tb →
      boilTime Cw (n*Phi) (n*hsol) P h Tr T0 Tb T0 ≤ boilTime Cw (n*Phi) (n*hsol) P h Tr T0 Tb Ta := by
  intro Cw P h Tr T0 Tb r M mw n Phi hsol hTr hT0 hb1 hb2 hCw hh hP hr1 hr2 hM hmw hn hP1 hP2 hs Ta h1 h2
  have hn0 : 0 < n := by rw [hn, hM, hmw]; positivity
  have hn1 : n ≤ 0.8540 := by
    rw [hn, hM, hmw, div_le_iff₀ (by norm_num)]; nlinarith
  have hCs : n*Phi < 0 := by nlinarith
  have hCs2 : -36 ≤ n*Phi := by nlinarith
  have hCw0 : 0 < Cw := by linarith
  have hC : 0 < Cw + n*Phi := by linarith
  have hd : 0 ≤ Tb - Tr := by linarith
  have hd2 : Tb - Tr ≤ 81 := by linarith
  -- enthalpy of solution at the boiling point is still >= 0 (checked)
  have hE0 : 0 ≤ hsol + Phi*(Tb-Tr) := by nlinarith [mul_nonneg (by linarith : 0 ≤ Phi + 41) hd]
  have hE : 0 ≤ n*hsol + n*Phi*(Tb-Tr) := by nlinarith [mul_nonneg hn0.le hE0]
  have hstove : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-n*hsol)/(Cw+n*Phi)) := by
    intro H hH
    have : (H-n*hsol)/(Cw+n*Phi) ≤ Tb - Tr := by
      rw [div_le_iff₀ hC]; nlinarith [hH.2]
    nlinarith
  refine (compare_theorem Cw (n*Phi) (n*hsol) P h Tr T0 Tb hCw0 hC hh hP (by linarith) hstove
    T0 Ta le_rfl h1 h2).1 ?_
  intro x hx1 hx2
  nlinarith [mul_nonneg (by linarith : 0 ≤ Tb - x) (by linarith : 0 ≤ -(n*Phi))]

Round 5

Messages

Bash input
{
  "command": "cd /work; ls -la; ls */ 2>/dev/null | head -50; cat report.txt | head -150"
}
Bash output
total 56
drwxrwxr-x  3 1001 1001  4096 Nov 17 23:46 .
drwxr-xr-x  1 root root  4096 Nov 17 23:46 ..
-rw-r--r--  1 1001 1001 13320 Nov 17 16:13 Model.lean
-rw-r--r--  1 1001 1001  1934 Nov 17 16:06 candidate.json
drwxr-xr-x 23 1001 1001  4096 Nov 17 16:13 checks
-rw-------  1 1001 1001  2993 Nov 17 23:46 human-feedback.json
-rw-------  1 1001 1001 13154 Nov 17 16:14 last-review-packet.json
-rw-r--r--  1 1001 1001  2602 Nov 17 16:06 report.txt
0a061e67c7b843d9a544c0247cdccce7
0d53881888904eb7b263d141eed5512e
0e26c0d1cd2a4d3e931493e00c8252b4
1b0846e151ca49ea869d303e1858edd7
1be70249055743059f956160d13a001e
2125411f31dc4bc28190c5bc6bd44d1d
248420fe49ad4124bd13c839d06e22c4
52448c0db62c400e90cbd46e54881cba
667a9608d45d4803985b35cf1c68b1d8
7e4be0bd42b444199e65b96370c18486
7f112ae594ee431b9528f1645a3aa6c2
7fdbd8cda6304fd3ae91fdd97ab702a9
90c7524db3db4651b7532c926bf7722b
91bf5367d0354c09a0f1841d6c390708
97c02acf02654117a33c008e8abdb8f0
9d2a29707556410397d3c62a510d59a9
b7ec15d94a8146bfadfbd5cc72230a57
ba6fe61237934f078069bb23df732a18
d2511eb386bb475db1cec32245800f7d
d34803db1d874cbd871ed1ec8eedf419
dcd7a1b461ab4ea1832bd51aed75b96c
CYCLE 4 REVISION (response to: choose bounded ratio, derive Cs from an explicit model, prove sign/bounds, check conditions).
Accepted Newtonian lumped heating model kept unchanged (boilTime, compare_theorem unchanged).

Choices: NaCl; r = salt mass / water mass (not final mass fraction; w=r/(1+r) proved); 1 L at 20 C = 0.998 kg;
regime r in [0.02,0.05] (20-50 g NaCl in the litre, 2-4.8 wt%).
Cs: by definition of the apparent molar heat capacity, C_solution = C_water + n*Phi, so Cs = n*Phi, n = 1000*0.998*r/58.44 mol (<=0.854 mol).
Phi from the Laliberte (2009) NaCl correlation (coefficients from the 'chemicals' package data file; fit range 1.5-120 C, w<=0.26),
evaluated by me in Python (synthetic calculation from a published correlation, not a measurement), water Cp from IAPWS values.
Results (J/K/mol): Phi(20C) ~ -74 (r=.02) ... -52 (r=.05) [lit. infinite dilution ~ -88 at 25 C, consistent]; Phi rises toward ~-21..-31 near 70-80 C and falls to ~-31..-45 at 100 C.
So Cs<0 over the whole 20-100 C, r in regime; |Cs| <= ~45 J/K per kg water (about 1% of 4.2 kJ/K). The earlier cycle's sign change near 13 wt% is outside this regime.
Temperature dependence: model needs constant Cs; I use the secant mean Phi over 20..Tb, which makes the linear mixing enthalpy exact at 20 C and Tb.
Computed secant means: -40.7 (r=.02), -33.9 (.035), -27.9 (.05); bounds used [-41,-27]. The true mixing enthalpy E(x)=n(hsol+int Phi) is decreasing, with minimum at Tb:
E(Tb)/n ~ +630 (r=.02), +1170 (.035), +1650 (.05) J/mol, versus hsol=3880. (At r=0.01 it is only ~+200 and for r<~0.01 it would become negative near Tb: the regime was chosen to exclude this marginal case.)
Result: salt addition is endothermic at all temperatures 20..Tb (it cools), so earlier is better; compare_theorem (cooling over an interval favours the earlier addition) gives: salting at the start is no slower than at any later Ta. Lean checks D+Cs(x-Tr)>=0 from the premises and derives the stove-beats-loss condition for the solution (no longer a premise).
Size of effect: temperature drop on mixing <= ~hsol*n/C ~ 0.3-0.7 K at start; time difference is tiny relative to pot/stove variability. Also boil-over/avoid adding salt to boiling water argue for salting early.
Limits: premises (Phi bounds, hsol, Tb range) are inputs; Laliberte correlation accuracy is a few percent, so margin at r=0.02 (~15% of hsol) is adequate but at r<=0.01 the sign is unresolved. Not proved: quantitative time difference; Phi(T) handled by secant mean; no evaporation or salting-induced boiling-point change in time; no real measurement was run.
Bash input
{
  "command": "cd /work; cat Model.lean; cat candidate.json; cat human-feedback.json | head -5; ls checks | head -3; which lake lean; ls /harness/src"
}
Bash output
import Mathlib.Analysis.SpecialFunctions.Log.Deriv
import Mathlib.Analysis.Calculus.MeanValue

/-! Model (see report.txt).  H = enthalpy (J) measured from "everything at room temperature Tr".
 Pure water, heat capacity Cw: temperature Tr + H/Cw.
 Salt solution, total heat capacity Cw+Cs>0 (Cs of EITHER sign; literature: Cs<0), dissolution enthalpy D (at Tr) of either sign:
 temperature Tr + (H-D)/(Cw+Cs).  Salt (at Tr) is mixed in instantly, so H is conserved.
 Heater power P constant, loss h*(T-Tr):  dH/dt = P - h*(T-Tr).
 Separating variables, the time to move from H1 to H2 is Fw/Fs(H2) - Fw/Fs(H1) where
 Fw' = 1/(P - h*(H/Cw)), Fs' = 1/(P - h*((H-D)/(Cw+Cs))) (explicit logarithms below).
 Tb = boiling point of the final solution (fixed by salt amount, not by timing). -/

/-- time-integral of dH/(P - loss) for pure water (antiderivative, h>0) -/
noncomputable def Fw (Cw P h H : ℝ) : ℝ := -(Cw/h) * Real.log (P - h*(H/Cw))
/-- same for the salt solution -/
noncomputable def Fs (Cw Cs D P h H : ℝ) : ℝ := -((Cw+Cs)/h) * Real.log (P - h*((H-D)/(Cw+Cs)))

/-- Total time to reach Tb from T0 when the salt is added once the water is at Ta:
 heating water alone from T0 to Ta, then solution from the post-mixing state up to Tb. -/
noncomputable def boilTime (Cw Cs D P h Tr T0 Tb Ta : ℝ) : ℝ :=
  (Fw Cw P h (Cw*(Ta-Tr)) - Fw Cw P h (Cw*(T0-Tr))) +
  (Fs Cw Cs D P h ((Cw+Cs)*(Tb-Tr)+D) - Fs Cw Cs D P h (Cw*(Ta-Tr)))

lemma G_deriv (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs))) :
    ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), HasDerivAt (fun H => Fw Cw P h H - Fs Cw Cs D P h H)
      (1/(P - h*(H/Cw)) - 1/(P - h*((H-D)/(Cw+Cs)))) H := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  intro H hH
  have a1 : HasDerivAt (fun H : ℝ => P - h*(H/Cw)) (-(h/Cw)) H := by
    have := ((hasDerivAt_id H).div_const Cw).const_mul h
    simpa [div_eq_mul_inv, mul_comm] using (this.const_sub P)
  have a2 : HasDerivAt (fun H : ℝ => P - h*((H-D)/(Cw+Cs))) (-(h/(Cw+Cs))) H := by
    have := (((hasDerivAt_id H).sub_const D).div_const (Cw+Cs)).const_mul h
    simpa [div_eq_mul_inv, mul_comm] using (this.const_sub P)
  have l1 := (a1.log (hw H hH).ne').const_mul (-(Cw/h))
  have l2 := (a2.log (hs H hH).ne').const_mul (-((Cw+Cs)/h))
  have := l1.sub l2
  convert this using 1
  · funext x; rfl
  · have e1 := (hw H hH).ne'
    have e2 := (hs H hH).ne'
    have e3 := hh.ne'
    have e4 := hCw.ne'
    have e5 := hC.ne'
    field_simp

/-- Salt added at water temperature x changes the temperature at once by
 (D + Cs (x-Tr))/(Cw+Cs) downward (a cooling if D + Cs (x-Tr) > 0).  -/
lemma mix_iff (Cw Cs D Tr x : ℝ) (hCw : 0 < Cw) (hC : 0 < Cw + Cs) :
    (Cw*(x-Tr) - D)/(Cw+Cs) ≤ Cw*(x-Tr)/Cw ↔ 0 ≤ D + Cs*(x-Tr) := by
  rw [mul_div_cancel_left₀ _ hCw.ne', div_le_iff₀ hC]
  constructor <;> intro h <;> nlinarith

lemma G_mono (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs)))
    (S : Set ℝ) (hS : S ⊆ Set.Icc 0 (Cw*(Tb-Tr))) (hconv : Convex ℝ S)
    (sgn : ∀ H ∈ S, (H-D)/(Cw+Cs) ≤ H/Cw) :
    MonotoneOn (fun H => Fw Cw P h H - Fs Cw Cs D P h H) S := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  have hd := G_deriv Cw Cs D P h Tr Tb hCw hC hh hP hs
  apply monotoneOn_of_deriv_nonneg hconv
  · intro H hH; exact (hd H (hS hH)).continuousAt.continuousWithinAt
  · intro H hH; exact (hd H (hS (interior_subset hH))).differentiableAt.differentiableWithinAt
  · intro H hH
    have hH' := interior_subset hH
    rw [(hd H (hS hH')).deriv]
    apply sub_nonneg.mpr
    apply one_div_le_one_div_of_le (hw H (hS hH'))
    have := sgn H hH'
    nlinarith

lemma G_anti (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs)))
    (S : Set ℝ) (hS : S ⊆ Set.Icc 0 (Cw*(Tb-Tr))) (hconv : Convex ℝ S)
    (sgn : ∀ H ∈ S, H/Cw ≤ (H-D)/(Cw+Cs)) :
    AntitoneOn (fun H => Fw Cw P h H - Fs Cw Cs D P h H) S := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  have hd := G_deriv Cw Cs D P h Tr Tb hCw hC hh hP hs
  apply antitoneOn_of_deriv_nonpos hconv
  · intro H hH; exact (hd H (hS hH)).continuousAt.continuousWithinAt
  · intro H hH; exact (hd H (hS (interior_subset hH))).differentiableAt.differentiableWithinAt
  · intro H hH
    have hH' := interior_subset hH
    rw [(hd H (hS hH')).deriv]
    apply sub_nonpos.mpr
    apply one_div_le_one_div_of_le (hs H (hS hH'))
    have := sgn H hH'
    nlinarith

/-- Comparison lemma (sign-free; proved, used by the answer below).  Salt of fixed amount (Cs, D arbitrary sign, C=Cw+Cs>0).
 If between water temperatures Ta1 ≤ Ta2 salt addition always COOLS (D+Cs(x-Tr) ≥ 0 for x in [Ta1,Ta2]),
 adding at Ta1 is no slower than at Ta2; if it always WARMS (≤ 0), adding at Ta2 is no slower. -/
theorem compare_theorem : ∀ (Cw Cs D P h Tr T0 Tb : ℝ),
    0 < Cw → 0 < Cw + Cs → 0 < h → h*(Tb-Tr) < P → Tr ≤ T0 →
    (∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs))) →
    ∀ Ta1 Ta2 : ℝ, T0 ≤ Ta1 → Ta1 ≤ Ta2 → Ta2 ≤ Tb →
      ((∀ x, Ta1 ≤ x → x ≤ Ta2 → 0 ≤ D + Cs*(x-Tr)) →
          boilTime Cw Cs D P h Tr T0 Tb Ta1 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta2) ∧
      ((∀ x, Ta1 ≤ x → x ≤ Ta2 → D + Cs*(x-Tr) ≤ 0) →
          boilTime Cw Cs D P h Tr T0 Tb Ta2 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta1) := by
  intro Cw Cs D P h Tr T0 Tb hCw hC hh hP hTr hs Ta1 Ta2 h1 h12 h2
  have hmem : ∀ x, Ta1 ≤ x → x ≤ Ta2 → Cw*(x-Tr) ∈ Set.Icc 0 (Cw*(Tb-Tr)) := fun x a b =>
    ⟨by nlinarith, by nlinarith⟩
  have hS : Set.Icc (Cw*(Ta1-Tr)) (Cw*(Ta2-Tr)) ⊆ Set.Icc 0 (Cw*(Tb-Tr)) := by
    intro H hH
    exact ⟨by nlinarith [hH.1], by nlinarith [hH.2]⟩
  have hHx : ∀ H ∈ Set.Icc (Cw*(Ta1-Tr)) (Cw*(Ta2-Tr)), ∃ x, Ta1 ≤ x ∧ x ≤ Ta2 ∧ H = Cw*(x-Tr) := by
    intro H hH
    refine ⟨H/Cw + Tr, ?_, ?_, ?_⟩
    · have : Ta1 - Tr ≤ H/Cw := by rw [le_div_iff₀ hCw]; nlinarith [hH.1]
      linarith
    · have : H/Cw ≤ Ta2 - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
      linarith
    · field_simp; ring
  constructor
  · intro hx
    have m := G_mono Cw Cs D P h Tr Tb hCw hC hh hP hs _ hS (convex_Icc _ _) (by
      intro H hH
      obtain ⟨x, a, b, rfl⟩ := hHx H hH
      exact (mix_iff Cw Cs D Tr x hCw hC).mpr (hx x a b))
      ⟨le_rfl, by nlinarith⟩ ⟨by nlinarith, le_rfl⟩ (by nlinarith)
    simp only [boilTime]; simp only at m; linarith
  · intro hx
    have m := G_anti Cw Cs D P h Tr Tb hCw hC hh hP hs _ hS (convex_Icc _ _) (by
      intro H hH
      obtain ⟨x, a, b, rfl⟩ := hHx H hH
      have := hx x a b
      have h3 : ¬ ((Cw*(x-Tr) - D)/(Cw+Cs) < Cw*(x-Tr)/Cw) → True := fun _ => trivial
      rw [mul_div_cancel_left₀ _ hCw.ne', le_div_iff₀ hC]
      nlinarith)
      ⟨le_rfl, by nlinarith⟩ ⟨by nlinarith, le_rfl⟩ (by nlinarith)
    simp only [boilTime]; simp only at m; linarith


/-! ## Salt-specific part (NaCl, 1 L of water).  Inputs are labelled; nothing numerical below is a measurement.

 Choices.  Salt = NaCl.  1 L of water at 20 C has mass 0.998 kg (density 0.9982 kg/L).  The salt/water ratio
 r = (salt mass)/(water mass) (NOT the mass fraction of the final solution; that is w = r/(1+r), proved in
 `cs_theorem`).  Regime: 0.02 <= r <= 0.05, i.e. about 20-50 g NaCl in the litre (2-4.8 wt%).
 Cs is DERIVED, not fitted: by definition of the apparent molar heat capacity Phi of the salt in solution,
 (total heat capacity of solution) = (heat capacity of the water) + n*Phi, so Cs = n*Phi, n = moles of salt.
 Phi is temperature dependent; the lumped model needs one constant Cs, so Phi is the SECANT MEAN of Phi(T,m)
 over 20 C..Tb, which makes the linear mixing enthalpy D + Cs (T-Tr) exact at both 20 C and Tb.
 Empirical inputs (EXPOSED PREMISES, constants below), from the Laliberte (2009) NaCl correlation (fit range
 1.5-120 C, w <= 0.26) evaluated numerically outside Lean (a synthetic calculation from a published correlation)
 and from NBS tables: secant-mean Phi in [-41,-27] J/K/mol for r in [0.02,0.05] (computed -40.7 ... -27.9);
 enthalpy of solution of NaCl(s) hsol >= 3800 J/mol (NBS +3.88 kJ/mol); boiling point Tb in [100,101] C;
 pot+water heat capacity >= 4170 J/K (at least the 0.998 kg of water at 4.18 J/g/K).
 D = n*hsol is the enthalpy absorbed when salt at room temperature Tr dissolves at Tr. -/

/-- room / initial water temperature, C -/
noncomputable def Tr0 : ℝ := 20
/-- admissible range of the boiling point of the salt solution, C -/
noncomputable def TbLo : ℝ := 100
noncomputable def TbHi : ℝ := 101
/-- minimum heat capacity of pot + water, J/K -/
noncomputable def CwMin : ℝ := 4170
/-- salt/water mass-ratio regime -/
noncomputable def rLo : ℝ := 1/50
noncomputable def rHi : ℝ := 1/20
/-- NaCl molar mass g/mol, mass of 1 L of water at 20 C in kg -/
noncomputable def Mnacl : ℝ := 58.44
noncomputable def massWater : ℝ := 0.998
/-- moles of NaCl for salt/water mass ratio r (1 kg = 1000 g) -/
noncomputable def nSalt (r : ℝ) : ℝ := 1000*massWater*r/Mnacl
/-- empirical bounds of the secant-mean apparent molar heat capacity of NaCl(aq) over 20 C..Tb, J/K/mol -/
noncomputable def PhiLo : ℝ := -41
noncomputable def PhiHi : ℝ := -27
/-- derived floor for Cs, J/K (n <= 0.854 mol times -41 J/K/mol) -/
noncomputable def CsFloor : ℝ := -36
/-- lower bound of the enthalpy of solution of NaCl(s) at room temperature, J/mol -/
noncomputable def hsolMin : ℝ := 3800

lemma nSalt_pos (r : ℝ) (h : rLo ≤ r) : 0 < nSalt r := by
  unfold nSalt massWater Mnacl; unfold rLo at h; positivity

lemma nSalt_le (r : ℝ) (h : r ≤ rHi) : nSalt r ≤ 0.8540 := by
  unfold nSalt massWater Mnacl
  unfold rHi at h
  rw [div_le_iff₀ (by norm_num)]; nlinarith

/-- Cs = n*Phi: sign, size, and the ratio conversion. -/
theorem cs_theorem : ∀ (r Phi ms mwater : ℝ),
    rLo ≤ r → r ≤ rHi → PhiLo ≤ Phi → Phi ≤ PhiHi → 0 < ms → 0 < mwater →
    (nSalt r * Phi < 0 ∧ CsFloor ≤ nSalt r * Phi) ∧ ms/(ms+mwater) = (ms/mwater)/(1+ms/mwater) := by
  intro r Phi ms mwater hr1 hr2 hP1 hP2 hms hmwa
  have hn0 := nSalt_pos r hr1
  have hn1 := nSalt_le r hr2
  unfold PhiLo at hP1; unfold PhiHi at hP2; unfold CsFloor
  refine ⟨⟨by nlinarith, by nlinarith⟩, ?_⟩
  field_simp
  ring

/-- ANSWER.  Accepted model (lumped heat capacity Cw, constant stove power P, Newton loss h, no evaporation,
 salt at room temperature Tr0 = T0 dissolves instantly, enthalpy conserved).  Under the premises above
 (r in [rLo,rHi], Cs = n*Phi, D = n*hsol, Tb in [TbLo,TbHi], stove stronger than losses at Tb), adding the
 salt right at the start (Ta = T0 = Tr0) is no slower than adding it at any later water temperature Ta <= Tb.
 The condition D + Cs (x-Tr) >= 0 (salt addition cools / does not warm) is CHECKED here, not assumed, and
 the 'stove beats losses' condition for the salt solution is derived from it. -/
theorem answer_theorem : ∀ (Cw P h Tb r Phi hsol : ℝ),
    TbLo ≤ Tb → Tb ≤ TbHi → CwMin ≤ Cw → 0 < h → h*(Tb-Tr0) < P →
    rLo ≤ r → r ≤ rHi → PhiLo ≤ Phi → Phi ≤ PhiHi → hsolMin ≤ hsol →
    ∀ Ta : ℝ, Tr0 ≤ Ta → Ta ≤ Tb →
      boilTime Cw (nSalt r*Phi) (nSalt r*hsol) P h Tr0 Tr0 Tb Tr0
        ≤ boilTime Cw (nSalt r*Phi) (nSalt r*hsol) P h Tr0 Tr0 Tb Ta := by
  intro Cw P h Tb r Phi hsol hb1 hb2 hCw hh hP hr1 hr2 hP1 hP2 hs Ta h1 h2
  have hn0 := nSalt_pos r hr1
  have hn1 := nSalt_le r hr2
  generalize nSalt r = n at hn0 hn1 ⊢
  unfold TbLo at hb1; unfold TbHi at hb2; unfold CwMin at hCw
  unfold PhiLo at hP1; unfold PhiHi at hP2; unfold hsolMin at hs
  have hTr : Tr0 = 20 := rfl
  rw [hTr] at hP h1 ⊢
  have hCs : n*Phi < 0 := by nlinarith
  have hCs2 : -36 ≤ n*Phi := by nlinarith
  have hCw0 : 0 < Cw := by linarith
  have hC : 0 < Cw + n*Phi := by linarith
  have hd : 0 ≤ Tb - 20 := by linarith
  have hd2 : Tb - 20 ≤ 81 := by linarith
  -- enthalpy of solution at the boiling point is still >= 0 (checked)
  have hE0 : 0 ≤ hsol + Phi*(Tb-20) := by nlinarith [mul_nonneg (by linarith : 0 ≤ Phi + 41) hd]
  have hE : 0 ≤ n*hsol + n*Phi*(Tb-20) := by nlinarith [mul_nonneg hn0.le hE0]
  have hstove : ∀ H ∈ Set.Icc 0 (Cw*(Tb-20)), 0 < P - h*((H-n*hsol)/(Cw+n*Phi)) := by
    intro H hH
    have : (H-n*hsol)/(Cw+n*Phi) ≤ Tb - 20 := by
      rw [div_le_iff₀ hC]; nlinarith [hH.2]
    nlinarith
  refine (compare_theorem Cw (n*Phi) (n*hsol) P h 20 20 Tb hCw0 hC hh hP (by linarith) hstove
    20 Ta le_rfl h1 h2).1 ?_
  intro x hx1 hx2
  nlinarith [mul_nonneg (by linarith : 0 ≤ Tb - x) (by linarith : 0 ≤ -(n*Phi))]
{
 "proof_file": "Model.lean",
 "narrative_file": "report.txt",
 "conclusions": {"answer": "answer_theorem", "cs": "cs_theorem"},
 "user_facts": [
  "1 L of water at 20 C in a pot on a stove, heated",
  "salt will be added; goal: salted water reaches its boiling point as soon as possible",
  "salt quantity, pot, heater, room unspecified"
 ],
 "model_choices": [
  "accepted Newtonian lumped heating model (Cw, constant power P, loss h(T-Tr), no evaporation), salt at Tr dissolves instantly, enthalpy conserved",
  "salt = NaCl; ratio r = salt mass / water mass in [0.02,0.05] (w=r/(1+r), proved); 1 L = 0.998 kg",
  "Cs = n*Phi by definition of apparent molar heat capacity; constant Phi = secant mean over 20 C..Tb of the Laliberte (2009) NaCl correlation, bounds [-41,-27] J/K/mol (computed outside Lean, synthetic calculation from a published correlation, fit range 1.5-120 C)",
  "D = n*hsol, hsol >= 3800 J/mol (NBS +3.88 kJ/mol); Tb in [100,101] C; Cw >= 4170 J/K; stove stronger than loss at Tb"
 ],
 "sources": [
  "Laliberte, J. Chem. Eng. Data 54, 1725 (2009) NaCl coefficients as distributed in the python 'chemicals' package 1.5.2 (Electrolytes/Laliberte2009.tsv); water Cp from standard IAPWS values",
  "NBS/CRC thermochemical tables for enthalpy of solution of NaCl (+3.88 kJ/mol), recalled, not re-verified in this run"
 ],
 "incorporated_inputs": [],
 "proof_scope": "cs_theorem: given r in [0.02,0.05] and Phi in [-41,-27], Cs=n*Phi is negative and >= -36 J/K; mass-ratio to mass-fraction conversion. answer_theorem: under the stated premises the cooling condition D+Cs(Tb-Tr)>=0 and the stove-beats-loss condition for the solution are DERIVED, and salting at the start is no slower than at any later temperature. Numerical Phi/hsol bounds are premises taken from a correlation/tables, not proved or measured; temperature dependence of Phi is handled only by the secant-mean approximation. Human review needed."
}
{
  "decision": "reject",
  "binding": "2ddafd50e513148c684a2711054c9894cac903c7cd05bf3a01c77d0d4c0077ce",
  "reason": "Human review of attempt 004: verify the empirical reference and calculate Phi inside Lean.\n\nHuman instruction, verbatim:\n\"Tell him to check the ref and calculate Phi inside Lean. Is claude fish done?\"\n\n1. Check the dissolution-enthalpy reference rather than relying on recalled NBS/CRC values. Retrieve and inspect an authoritative source, retain its exact reference and relevant page/table/equation, and establish that the quantity, temperature, final concentration, initial solid state, sign convention and enthalpy reference match your model. Distinguish a 25 C standard/infinite-dilution value from dissolution at 20 C to a finite concentration. If a conversion or interpolation is needed, state and justify it and derive its mathematical consequences. Reassess the claimed hsol lower bound with supported uncertainty/applicability. Do not retain an unsupported bound merely because it yields the existing conclusion.\n\n2. Calculate Phi in Lean from the explicit material-property model. The bounds [-41,-27] currently enter as numerical premises derived only in Python. Instead formalize the relevant correlation or another justified thermodynamic model, expose empirically sourced coefficients/data and their applicability as reviewable premises, derive the solution-minus-original-water apparent molar heat capacity and any constant effective Phi you use, and prove the bounds needed by the timing theorem in Lean. Numerical evaluation or approximations must have proved enclosures sufficient for the conclusion. Python may suggest bounds; it must not be the sole evidence for them. Do not introduce a new assumption of the same desired Phi bound under another name.\n\n3. Preserve the physical meaning of the reduction. If using a secant average of temperature-dependent heat capacity, define and derive that average and justify its use for the timing comparison over the whole relevant addition-temperature interval. Agreement at two endpoints alone does not establish the intermediate mixing-energy inequalities. If the verified property model requires changing the regime or the qualitative recommendation, explain why and prove the result that actually follows, without tuning evidence to force an optimum.\n\nKeep the already accepted shared constant-power/Newtonian-loss approximation and retain the previous attempts and source/provenance records. A numerical heating duration or numerical time saving is not required. A physically supported qualitative recommendation with the property-to-conclusion proof chain is required. No other agent's model, material values, results or proof are supplied; no new physical measurements are claimed. You may request specific missing literature through the existing protocol. The next candidate still requires human review.\n",
  "time": "2026-11-17T23:46:18.240685+00:00",
0a061e67c7b843d9a544c0247cdccce7
0d53881888904eb7b263d141eed5512e
0e26c0d1cd2a4d3e931493e00c8252b4
/opt/lean/bin/lake
/opt/lean/bin/lean
vcs_extract.py
vcs_lean.py
vcs_protocol.py
Bash input
{
  "command": "cd /work; python3 -c \"import chemicals,os;print(chemicals.__file__)\"; ls checks/*|head; ls /work/checks/0a061e67c7b843d9a544c0247cdccce7; cat /harness/src/vcs_protocol.py | grep -n \"Cycle\\|cycle\" | head"
}
Bash output
Traceback (most recent call last):
  File "<string>", line 1, in <module>
    import chemicals,os;print(chemicals.__file__)
    ^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'chemicals'
checks/0a061e67c7b843d9a544c0247cdccce7:
Checked.lean
extracted.json
extracted.lean
lean.log
submitted-candidate.json
submitted-report.txt
submitted.lean
verdict.json

Checked.lean
extracted.json
extracted.lean
lean.log
submitted-candidate.json
submitted-report.txt
submitted.lean
verdict.json
507:    """Native login validation on every cycle; account auth may change between cycles."""
618:    """Validate host-custodied evidence before using it in another scientist cycle."""
651:    state["cycles"] += 1
654:              "\nThis is cycle " + str(state["cycles"]) + ". Review your /work history, input responses, human feedback, and check-feedback files. Revise failures using the actual diagnostic output. Work in /work. Stop after a request or a checkable candidate. Do not self-approve.\n")
663:    trace = run / "control" / f"scientist-{state['cycles']:03d}.jsonl"
702:def execute_cycle(run, state):
710:        archive = run / "control" / f"feedback-{state['cycles']:03d}"
723:        feedback = run / "work/check-feedback" / f"cycle-{state['cycles']:03d}"
728:            state["error"] = f"Stopped after {MAX_AUTOMATIC_CYCLES} automatic cycles; inspect retained feedback and resume explicitly."
778:    state = {"version": 1, "status": "new", "cycles": 0, "attempts": [], "responses": [], "runtime": config,
WebFetch input
{
  "url": "https://raw.githubusercontent.com/CalebBell/chemicals/master/chemicals/Electrolytes/Laliberte2009.tsv",
  "prompt": "Give the header line and the full row for Sodium chloride (NaCl), verbatim, with all columns."
}
WebSearch input
{
  "query": "Laliberté 2009 \"Model for Calculating the Viscosity of Aqueous Solutions\" OR \"heat capacity\" apparent specific heat capacity of solute Cp,i = a1 exp(alpha) + a5 (1-w_w)^a6 alpha = a2 t + a3 exp(0.01 t) + a4 (1-w_w)"
}
WebFetch output
The full header line and NaCl row together exceed the 125-character limit for quoted source text, so I can't reproduce them verbatim in one quote. Here is the NaCl row with each value labeled by its column name:

| Column | Value |
|---|---|
| Name | Sodium Chloride |
| CASRN | 7647-14-5 |
| Formula | NaCl |
| MW | 58.45 |
| c0 | -0.00324112223655149 |
| c1 | 0.0636354335906616 |
| c2 | 1.01371399467365 |
| c3 | 0.0145951015210159 |
| c4 | 3317.34854426537 |
| Min T (c set) | 0 |
| Max T (c set) | 140 |
| Max w (c set) | 0.26589930421877 |
| No of points in corr (c set) | 869 |
| v1 | 16.221788633396 |
| v2 | 1.32293086770011 |
| v3 | 1.48485985010431 |
| v4 | 0.00746912559657377 |
| v5 | 30.7802007540575 |
| v6 | 2.05826852322558 |
| Min T (v set) | 5 |
| Max T (v set) | 154 |
| Max w (v set) | 0.264456748962402 |
| No of points in corr (v set) | 552 |
| a1 | -0.0693559668993322 |
| a2 | -0.0782134167486952 |
| a3 | 3.84798479408635 |
| a4 | -11.2762109247072 |
| a5 | 8.73187698542672 |
| a6 | 1.81245930472755 |
| Min T (a set) | 1.5 |
| Max T (a set) | 120 |
| Max w (a set) | 0.261058295490885 |
| No of points in corr (a set) | 356 |

The header repeats the column names "Min T," "Max T," "Max w," and "No of points in corr" for each of the three coefficient sets (c, v, and a), so I've added the set in parentheses to keep them distinct.
WebSearch output
Web search results for query: "Laliberté 2009 "Model for Calculating the Viscosity of Aqueous Solutions" OR "heat capacity" apparent specific heat capacity of solute Cp,i = a1 exp(alpha) + a5 (1-w_w)^a6 alpha = a2 t + a3 exp(0.01 t) + a4 (1-w_w)"

Links: [{"title":"Marc Laliberte - Indépendant","url":"https://www.linkedin.com/in/marc-laliberte-54b2604b/"},{"title":"A Model for Calculating the Heat Capacity of Aqueous Solutions, with Updated Density and Viscosity Data","url":"https://www.researchgate.net/publication/231541607_A_Model_for_Calculating_the_Heat_Capacity_of_Aqueous_Solutions_with_Updated_Density_and_Viscosity_Data"},{"title":"A Model for Calculating the Heat Capacity of Aqueous Solutions, with Updated Density and Viscosity Data","url":"https://hero.epa.gov/reference/1712350/"},{"title":"(PDF) Thermal Aspects of Melting Ice with Deicer Chemicals","url":"https://www.researchgate.net/publication/272427280_Thermal_Aspects_of_Melting_Ice_with_Deicer_Chemicals"},{"title":"Species Table - Specific Heat (Cp) - SysCAD Documentation","url":"https://help.syscad.net/Species_Table_-_Specific_Heat_(Cp)"},{"title":"Electrochemistry (thermo.electrochem) - Read the Docs","url":"https://thermo.readthedocs.io/thermo.electrochem.html"},{"title":"Heat capacities of aqueous sodium chloride, potassium chloride, magnesium chloride, magnesium sulfate, and sodium sulfate solutions between 80.deg. and 200.deg.","url":"https://pubs.acs.org/doi/10.1021/je60057a027"},{"title":"The Heat Capacity and Related Thermodynamic Properties of Aqueous Solutions. II. Lithium and Sodium Hydroxides at 25°","url":"https://pubs.acs.org/doi/abs/10.1021/ja01330a022"},{"title":"A Model for Calculating the Heat Capacity of Aqueous Solutions, with Updated Density and Viscosity Data","url":"https://ouci.dntb.gov.ua/en/works/lDWxvvo4/"},{"title":"Heat capacities and volumes of dissociation of phosphoric acid (1st, 2nd, and 3rd), bicarbonate ion, and bisulfate ion in aqueous solution","url":"https://www.researchgate.net/publication/237859439_Heat_capacities_and_volumes_of_dissociation_of_phosphoric_acid_1st_2nd_and_3rd_bicarbonate_ion_and_bisulfate_ion_in_aqueous_solution"}]

The search found the paper that matches your query. The results don't show the equation itself.

**The paper:** Marc Laliberté, "A Model for Calculating the Heat Capacity of Aqueous Solutions, with Updated Density and Viscosity Data," *J. Chem. Eng. Data* 54(6), 1725–1760 (2009), DOI 10.1021/je8008123. The heat-capacity model is in this paper. The viscosity model you named is a different Laliberté paper, from 2007 (*J. Chem. Eng. Data* 52, 321).

**What the model does:**
- It calculates the heat capacity of aqueous solutions with any number of solutes at any temperature.
- Parameters for 79 solutes were fitted to about 6600 single-solute data points.
- The average difference from experiment is -0.0003 kJ·kg⁻¹·K⁻¹, with a standard deviation of 0.010.
- It was validated on 13 multi-solute systems with 485 data points.

**How the solute term is used:**
- The solution heat capacity is a mass-fraction-weighted sum of the water heat capacity and each solute's apparent heat capacity. SysCAD's documentation describes this as the water specific heat and the solutes' heat capacities combined with a standard mixture Cp calculation.
- A deicer paper applies it to NaCl using the form Cp_solution = w_water·Cp_water + w_solute·Cp_solute.
- In the `thermo` Python library, `Laliberte_heat_capacity_mix` takes the parameters a1 to a6 for each solute. It calculates the heat capacity of an aqueous electrolyte mixture using the form proposed by Laliberté. That function's source is a good place to check the exact form of the Cp,i expression (a1…a6, α) that you quoted.

The pages I retrieved don't state the formula or the a1–a6 values, so I can't confirm your expression from them.

Useful links:
- Library docs: https://thermo.readthedocs.io/thermo.electrochem.html
- SysCAD page: https://help.syscad.net/Species_Table_-_Specific_Heat_(Cp)


REMINDER: You MUST include the sources above in your response to the user using markdown hyperlinks.
WebFetch input
{
  "url": "https://raw.githubusercontent.com/CalebBell/chemicals/master/chemicals/electrochem.py",
  "prompt": "Give verbatim the source code of function Laliberte_heat_capacity_i (formula lines and docstring formulas) and Laliberte_heat_capacity (how solution Cp combines)."
}
WebFetch output
The server returned HTTP 404 Not Found.

The response body was not retrieved. If this URL requires authentication, use an authenticated tool (e.g. `gh` for GitHub, or an MCP-provided fetch tool) instead of WebFetch.
WebFetch input
{
  "url": "https://thermo.readthedocs.io/_modules/thermo/electrochem.html",
  "prompt": "Find the function Laliberte_heat_capacity_i / Laliberte_heat_capacity_mix: quote verbatim the formula lines (alpha, Cp_i) and the docstring equations, and how Cp of the solution is combined with water Cp."
}
WebFetch output
**Laliberte_heat_capacity_i** (docstring equations):

- "Cp_i = a_1 e^\alpha + a_5(1-w_w)^{a_6}"
- "\alpha = a_2 t + a_3 \exp(0.01t) + a_4(1-w_w)"

Code lines:

- "alpha = a2*t + a3*exp(0.01*t) + a4*(1. - w_w)"
- "Cp_i = a1*exp(alpha) + a5*(1. - w_w)**a6"
- "return Cp_i*1000."

The code returns the result multiplied by 1000, while the docstring says units are J/kg/K.

**Laliberte_heat_capacity_mix** (docstring equation):

- "Cp_m = w_w Cp_w + \sum w_i Cp_i"

Code lines:

- "Cp_w = Laliberte_heat_capacity_w(T)"
- "w_w = 1.0 - sum(ws)"
- "Cp = w_w*Cp_w"
- "Cp += ws[i]*Cp_i"

**Combination:** The solution heat capacity is a weight-fraction-weighted sum. Water's heat capacity is computed by `Laliberte_heat_capacity_w(T)` and multiplied by the water weight fraction, w_w = 1 − Σws. Each solute's partial heat capacity Cp_i is multiplied by its weight fraction w_i and added to that term.
WebSearch input
{
  "query": "NaCl integral enthalpy of solution in water at 25 °C kJ/mol vs molality 0.5 1.0 relative apparent molar enthalpy Φ_L sodium chloride Parker NSRDS-NBS 2"
}
WebSearch input
{
  "query": "enthalpy of solution of NaCl(cr) in water infinite dilution 3.88 kJ/mol 25 C NBS tables Wagman 1982 Na+ Cl- aq formation enthalpy -240.12 -167.159"
}
WebSearch output
Web search results for query: "NaCl integral enthalpy of solution in water at 25 °C kJ/mol vs molality 0.5 1.0 relative apparent molar enthalpy Φ_L sodium chloride Parker NSRDS-NBS 2"

Links: [{"title":"Enthalpy of solution of aqueous sodium chloride at 303.15, 308.15, and 313.15 K - ScienceDirect","url":"https://www.sciencedirect.com/science/article/abs/pii/0021961485900746"},{"title":"Enthalpy of Solution of Sodium Nitrite - PMC","url":"https://pmc.ncbi.nlm.nih.gov/articles/PMC6751952/"},{"title":"Volumetric Properties of Aqueous Sodium Chloride Solutions","url":"https://srd.nist.gov/jpcrdreprint/1.555660.pdf"},{"title":"Thermodynamic Properties of the KCl+H2O System","url":"https://srd.nist.gov/jpcrdreprint/1.556034.pdf"},{"title":"Thermodynamic Properties of Aqueous Sodium Chloride ...","url":"https://srd.nist.gov/jpcrdreprint/1.555709.pdf"},{"title":"enthalPy of solution of eleCtrolytes","url":"https://www.purdue.edu/science/archive/docs/science-express/labs/Enthalpy%20of%20Solution_Reference.pdf"},{"title":"Analytical modeling of micelle growth. 3. Electrostatic free energy of ionic wormlike micelles -- effects of activity coefficients and spatially confined electric double layers","url":"https://arxiv.org/pdf/2007.05834"},{"title":"Sodium chloride","url":"https://en.wikipedia.org/wiki/Sodium_chloride"},{"title":"Derivation and thermodynamically consistent coupling of a Debye-H\\\"uckel type energy to a steric electrolyte model and application to the apparent molar volume and phase boundaries","url":"https://arxiv.org/pdf/2502.04344"},{"title":"Heat of Solution or Enthalpy of Solution Chemistry Tutorial","url":"https://www.ausetute.com.au/heatsolution.html"}]

The search didn't turn up Parker's tabulated Φ_L values for NaCl at 0.5 and 1.0 mol/kg, so I can't give you the molality-dependent numbers.

- **Infinite-dilution value:** The standard enthalpy of solution of NaCl at 25 °C is about +3.88 kJ/mol, and a Purdue reference table gives the same figure. A 2020s-era paper on 303–313 K data (Cited-by section) reports 3.32, 2.78 and 2.25 kJ/mol at 303.15, 308.15 and 313.15 K.
- **Parker's tabulation:** A ScienceDirect paper confirms that Parker tabulated the relative apparent molar enthalpy as a function of molality. Its abstract says its results were compared with values computed using the enthalpy of solution at infinite dilution and the relative apparent molar enthalpy tabulated by Parker.
- **Evaluated NaCl data:** NIST/JPCRD reprints (srd.nist.gov/jpcrdreprint/1.555709.pdf and 1.555660.pdf) say that below 100 °C they rely on the apparent molal enthalpies evaluated by Parker (at 25 °C). They also say the molar heat of solution is related to the apparent molal enthalpy Φ_L.
- **Relationship to use:** The integral enthalpy of solution at molality m is ΔH_soln(m) = ΔH°_soln(∞) + Φ_L(m). So at 0.5 and 1.0 mol/kg you would take 3.88 kJ/mol and add Parker's Φ_L at that molality.

For the actual Φ_L values, check the Parker NSRDS-NBS 2 table directly, or the Pitzer-type model in the JPCRD NaCl papers.


REMINDER: You MUST include the sources above in your response to the user using markdown hyperlinks.
WebSearch output
Web search results for query: "enthalpy of solution of NaCl(cr) in water infinite dilution 3.88 kJ/mol 25 C NBS tables Wagman 1982 Na+ Cl- aq formation enthalpy -240.12 -167.159"

Links: [{"title":"enthalPy of solution of eleCtrolytes","url":"https://www.purdue.edu/science/archive/docs/science-express/labs/Enthalpy%20of%20Solution_Reference.pdf"},{"title":"Hydration - Chemistry LibreTexts","url":"https://chem.libretexts.org/Bookshelves/Physical_and_Theoretical_Chemistry_Textbook_Maps/Supplemental_Modules_(Physical_and_Theoretical_Chemistry)/Thermodynamics/Energies_and_Potentials/Enthalpy/Hydration"},{"title":"Systematic comparison of force fields for microscopic simulations of NaCl in aqueous solutions: Diffusion, free energy of hydration and structural properties","url":"https://arxiv.org/pdf/physics/0211059"},{"title":"5.9: Enthalpy of Solution - Chemistry LibreTexts","url":"https://chem.libretexts.org/Courses/Northern_Michigan_University/CH_215:_Chemistry_of_the_Elements_Fall_2023/05:_Solids_and_Solid-State_Chemistry/5.09:_Enthalpy_of_Solution"},{"title":"Thermodynamic Properties of Aqueous Sodium Chloride ...","url":"https://srd.nist.gov/jpcrdreprint/1.555709.pdf"},{"title":"Heat of Solution or Enthalpy of Solution Chemistry Tutorial","url":"https://www.ausetute.com.au/heatsolution.html"},{"title":"Standard State and Enthalpy of Formation, Gibbs Free Energy of Formation, Entropy and Heat Capacity","url":"https://www.engineeringtoolbox.com/standard-state-enthalpy-formation-definition-value-Gibbs-free-energy-entropy-molar-heat-capacity-d_1978.html"},{"title":"Cl 053","url":"https://janaf.nist.gov/tables/Cl-053.txt"},{"title":"link.springer.com","url":"https://link.springer.com/article/10.1023/A:1014445917207"}]

The search supports ΔH_soln° of NaCl(cr) at infinite dilution and 25 °C being about +3.88 kJ/mol. It did not turn up the Wagman 1982 NBS tables themselves, and none of the results gave the exact formation values you listed.

- **Enthalpy of solution:** A Purdue reference table (the CRC-style "Enthalpy of Solution of Electrolytes") gives molar enthalpies of solution at infinite dilution, in kJ/mol at 25 °C. Its entry for NaCl(c) is 3.88. AUS-e-TUTE also lists NaCl(s) at +3.88 kJ/mol. LibreTexts notes that NaCl dissolves endothermically. That page's rounded value is +3.9 kJ/mol.
- **Formation enthalpies of the aqueous ions (Engineering Toolbox):** Cl⁻(aq) is −167.1 kJ/mol and Na⁺(aq) is −240.2 kJ/mol. These agree with your −167.159 and −240.12 to within rounding.
- **Formation enthalpy of the solid (JANAF):** The JANAF table gives ΔfH° of NaCl(cr) at 298.15 K as −411.120 kJ/mol.

Combining the ion values you gave with that solid value gives a consistency check. Using −240.12 + (−167.159) − (−411.12) gives about +3.84 kJ/mol. That is close to 3.88, and the small gap probably comes from the solid's formation enthalpy differing slightly between sources (NBS versus JANAF).


REMINDER: You MUST include the sources above in your response to the user using markdown hyperlinks.
WebFetch input
{
  "url": "https://www.purdue.edu/science/archive/docs/science-express/labs/Enthalpy%20of%20Solution_Reference.pdf",
  "prompt": "Quote the NaCl entry (and surrounding table header, title, source citation, temperature, reference state, sign convention, 'infinite dilution' wording)."
}
WebFetch input
{
  "url": "https://srd.nist.gov/jpcrdreprint/1.555709.pdf",
  "prompt": "Find tables or equations giving the relative apparent molal enthalpy Φ_L (L_phi) of NaCl(aq) at 25 C (298.15 K) as function of molality (0.1 to 1.5 mol/kg) and its temperature dependence, and the Pitzer equation parameters for enthalpy. Quote values/page/table numbers."
}
WebFetch output
I can't quote the NaCl entry, its table header, the temperature, reference state, sign convention, or the "infinite dilution" wording. The page content streams are Flate-compressed binary data, and the text isn't readable in what you pasted.

The document metadata is readable. It identifies the source as:

- Title: "CRC Handbook of Chemistry and Physics, 91th Edition"
- Subject: "Enthalpy of Solution of Electrolytes"
- Section: "Thermochemistry, Electrochemistry, and Kinetics"
- Author: Dr. David Lide

To get the NaCl entry, paste the table text from a text-extracted copy of the page, or paste the page's text directly. Then I can quote the relevant lines and surrounding details.

[Binary content (application/pdf, 85.2KB) also saved to /home/[user]/.claude/projects/-work/fba94078-bd5f-4070-8f33-53dd0fc9aaa7/tool-results/webfetch-1794959358861-vhy1pw.pdf]
WebFetch output
The page content you provided doesn't contain the NaCl(aq) data you asked for. I found no tables, equations, or Pitzer enthalpy parameters, and no page or table numbers I can cite.

The content is a raw PDF byte stream. The page text is in Flate-compressed streams that were not decoded, so only fragments are readable. The readable parts are:

- **XMP metadata:** producer "iText 4.2.0 by 1T3XT", modify date 2019-08-12, and a garbled title and creator.
- **Embedded fonts and images:** Montserrat and Code2000 font data, plus image streams.

The dump is also truncated, so later pages may be missing.

To answer the question, I need the document as extracted text, or a link to the source. I'd look for:

- A table of Φ_L against molality from 0.1 to 1.5 mol/kg at 298.15 K.
- Temperature-dependence coefficients for Φ_L, or the Pitzer enthalpy parameters (β⁽⁰⁾, β⁽¹⁾, C^φ and their temperature derivatives), with their equations.

Standard references for this include Pitzer, Peiper & Busey (1984, *J. Phys. Chem. Ref. Data*) and Archer (1992, *J. Phys. Chem. Ref. Data*). I haven't checked these against your document, so verify the citations and table numbers before relying on them.

[Binary content (application/pdf, 6MB) also saved to /home/[user]/.claude/projects/-work/fba94078-bd5f-4070-8f33-53dd0fc9aaa7/tool-results/webfetch-1794959360208-w4ykeh.pdf]
assistant
Both PDFs were saved locally; I'll extract text from them (reading them as untrusted data, in a separate directory).
Bash input
{
  "command": "mkdir -p /tmp/pdfs && cd /tmp/pdfs && cp /home/[user]/.claude/projects/-work/fba94078-bd5f-4070-8f33-53dd0fc9aaa7/tool-results/webfetch-1794959358861-vhy1pw.pdf crc.pdf && cp /home/[user]/.claude/projects/-work/fba94078-bd5f-4070-8f33-53dd0fc9aaa7/tool-results/webfetch-1794959360208-w4ykeh.pdf pitzer.pdf; which pdftotext; python3 -I -c \"import fitz\" 2>&1; python3 -I -c \"import pypdf;print('pypdf')\" 2>&1|tail -1"
}
Bash output
Traceback (most recent call last):
  File "<string>", line 1, in <module>
    import fitz
ModuleNotFoundError: No module named 'fitz'
ModuleNotFoundError: No module named 'pypdf'
Shell cwd was reset to /work
Bash input
{
  "command": "cat > /tmp/ex.py <<'E'\nimport re,zlib,sys\nd=open(sys.argv[1],'rb').read()\nout=[]\nfor m in re.finditer(rb'stream\\r?\\n(.*?)\\r?\\nendstream',d,re.S):\n    try: s=zlib.decompress(m.group(1))\n    except Exception: continue\n    if b'TJ' in s or b'Tj' in s:\n        txt=[]\n        for t in re.finditer(rb'\\[(.*?)\\]\\s*TJ|\\((.*?)\\)\\s*Tj',s,re.S):\n            g=t.group(1) or t.group(2)\n            parts=re.findall(rb'\\(((?:\\\\.|[^\\\\)])*)\\)',g) if t.group(1) else [g]\n            txt.append(b''.join(parts).decode('latin1'))\n        out.append('\\n'.join(txt))\nprint('\\n=====\\n'.join(out))\nE\npython3 -I /tmp/ex.py /tmp/pdfs/crc.pdf | grep -n -i -B3 -A3 \"NaCl\\|Sodium chl\" | head -60"
}
Bash output
grep: (standard input): binary file matches
Bash input
{
  "command": "python3 -I /tmp/ex.py /tmp/pdfs/crc.pdf > /tmp/crc.txt; wc /tmp/crc.txt; grep -a -n -i \"NaCl\\|Sodium chl\\|Enthalpy of Sol\\|infinite\\|kJ\" /tmp/crc.txt | head -20; grep -a -c . /tmp/crc.txt; head -c 1500 /tmp/crc.txt | cat -v | head -40"
}
Bash output
 884 1152 5631 /tmp/crc.txt
11:This table gives the molar enthalpy \(heat\) of solution at infinite 
17:solved in an infinite amount of water
58:kJ/mol
343:kJ/mol
433:NaCl
438:NaClO
444:NaClO
455:NaClO
461:NaClO
467:NaClO
642:kJ/mol
884
M-CM->M-CM-?^@E)>>BDC ^M0 g^M0 i ^M/RelativeColorimetric ri^M/GS0 gs^MBT^M/T1_0 1 Tf^M12 0 0 12 187.5634 744.18021 Tm^M(E
NTHAL
M-CM->M-CM-?^@p)>>BDC ^M3.74899 0 Td^M(P
Y OF 
M-CM->M-CM-?^@S)>>BDC ^M2.593 0 Td^M(S
OLUTION OF 
M-CM->M-CM-?^@E)>>BDC ^M6.883 0 Td^M(E
LE
M-CM->M-CM-?^@c)>>BDC ^M1.267 0 Td^M(C
TROLYTES
This table gives the molar enthalpy \(heat\) of solution at infinite 
dilution for some common uni-univalent electrolytes
M-CM->M-CM-?^@.)>>BDC ^M22.36 0 Td^M(.
 This is the 
enthalpy change when 1 mol of solute in its standard state is dis
-
solved in an infinite amount of water
M-CM->M-CM-?^@.)>>BDC ^M15.024 0 Td^M(.
 Values are given in kilojoules 
per mole at 25\260C
M-CM->M-CM-?^@.)>>BDC ^M6.946 0 Td^M(.
M-CM->M-CM-?^@R)>>BDC ^M/T1_0 1 Tf^M12 0 0 12 428.7067 705.6088 Tm^M(R
eference
M-CM->M-CM-?^@	^@	)>>BDC ^M/T1_1 1 Tf^M8 0 0 8 333 688.0088 Tm^M[( )-890( )]TJ^MEMC ^M2 0 Td^M[(P)5(ar)6(k)5(er)73(, )-69(V)]TJ^M/Span <</ActualText (M-CM->M-CM-?^@.)>>BDC ^M3.783 0 Td^M(.
 B
M-CM->M-CM-?^@.)>>BDC ^M(.
,
 Thermal Properties of Uni-Univalent Electrolytes,
 Natl
M-CM->M-CM-?^@.)>>BDC ^M(.
 
Stand
M-CM->M-CM-?^@.)>>BDC ^M(.
 Ref
M-CM->M-CM-?^@.)>>BDC ^M(.
 Data Series \227 Natl
M-CM->M-CM-?^@.)>>BDC ^M(.
 Bur
M-CM->M-CM-?^@.)>>BDC ^M14.507 0 Td^M(.
 Stand
Bash input
{
  "command": "sed -n 425,445p /tmp/crc.txt | cat -v | head -40; grep -a -n \"Parker\\|NSRDS\\|Reference\" /tmp/crc.txt | head; sed -n 330,350p /tmp/crc.txt | tr -d '\\r' | head -30"
}
Bash output
\22621
M-CM->M-CM-?^@.)>>BDC ^M(.
41
NaF
c
0
M-CM->M-CM-?^@.)>>BDC ^M(.
91
NaCl
c
3
M-CM->M-CM-?^@.)>>BDC ^M(.
88
NaClO
2
c
0
M-CM->M-CM-?^@.)>>BDC ^M(.
33
NaClO
2
c
\22623
þÿ.)>>BDC (.
26
=====
Solute
State
\037
sol
 
H
\260 
 
kJ/mol
LiBr 
\267
 2H
2
O
c
\2269
Bash input
{
  "command": "python3 -I /tmp/ex.py /tmp/pdfs/pitzer.pdf > /tmp/pz.txt; wc /tmp/pz.txt; grep -a -n -i \"phi_L\\|L_\\|enthalp\" /tmp/pz.txt | head"
}
Bash output
 49049  49181 335399 /tmp/pz.txt
94:enthalpY18nd 
194:enthalpy; 
339:Standardenthalpies, 
350:enthalpy.......................................... 
469:Enthalpy 
484:enthalpy 
507:enthalpy 
524:enthalpy 
549:enthalpy, 
578:enthalpy,
Bash input
{
  "command": "grep -a -n -i \"apparent molal enthalpy\\|relative apparent\\|Parker\\|heat of solution\\|Table 1[0-9]\\|L(m\\|infinite dilution\" /tmp/pz.txt | head -40; sed -n 330,360p /tmp/pz.txt | tr -d '\\r'"
}
Bash output
3491:Parker. 
48701:Parker, 
48923:Parker,R. 
coefficients 
for 
the 
NaCl\(aq\) 
entropy 
...................... 
,...................... 
20 
A-3 
Standardenthalpies, 
Debye-Hiickel 
AI. 
pa\255
rameter,. 
and 
virial 
coefficients 
for 
the 
NaCI\(aq\) 
enthalpy.......................................... 
26 
\251 
1984 
by 
the 
U.S. 
Secretary 
of 
Commerce 
on
Bash input
{
  "command": "cd /tmp; tr -d '\\r' < pz.txt | tr '\\n' ' ' > pz1.txt; grep -a -o -i \".\\{200\\}Parker.\\{300\\}\" pz1.txt | head -5; grep -a -o -i \".\\{150\\}heats of dilution.\\{300\\}\" pz1.txt | head -3; grep -a -o -i \".\\{100\\}enthalpies of solution.\\{300\\}\" pz1.txt | head -4"
}
Bash output
 Table  1,  two  sets  represent  compila\255 tions  of  a  large  number  of  older  measurements:  the  osmotic  coefficients  of  Robinson  and  Stokes 28  and  the  apparent  enth\255 alpies  of  Parker.  29  These  two  sets were  weighted  more  heavi\255 ly  in  least-squares  calculations  to  reflect  the  fact  that  they  are  \\';ulIlpilalium,  bl:lsc::U  Ull  ml:lny  mel:llSun::menLS  uf  rehtLivdy  high  accuracy.  Since  the  report  of  Silvester  and  Pitzer, 2  no  significant  measu
  Vol  \225.  13,  No.  1,1984  sure,"  Electric  Power  Research  Institute  Report.  1978.  2"R,  A.  Robinson  and  R..  H.  Stokes,  Electrolyte  Solutions  \(Butterworths,  London,i9'$\).  29y.  Parker,  "Thermal  Prqpeme!i  of.Aqueous  Univalent  Electrolytes,"  Natl.  Bur.  Stand.  \(U.S.\)  RcpOn  NSRDS-NBS  2,  196'.  lOG.  Scatchardand  8.S.  Prentiss,  J.  Am.  Chern.  Soc.  55,  4355  \(1933\).  3'H.F.  Gibbard,  Ir.,  G.  Scatchard;  R.  A.  Rocesseau,  and  J.  L.  Creek,  J.  c:::hem.  
.  S.  Bur.  Mines  Bull.  584  \(1960\).  4sv".  F.Liilke,  revision  of  Solubilities  by  A.  Seidell  \(American  Chemical  .  Society,  washington,  l!lb5J.  D.  Wagman,  W.  H.  Evans,  V.  B.  Parker,R.  H.Schumm,  and  R.  L.  Nuttall,  NattB\\lf.Stand;  \(U.S,\)  Tech.  Note  271M!  \(1981\).  47CODAT.i\\  \(repOrt\),  J:Chem.  T4ermodyn.  10,  90311978\).  4sAmerican  Institute  of  Physics  Handbook,  .3rd  ed.  \(McGraw-Hill.  New  York,1972\).  49R.  Hilbert,  Doctoral  dissertation  \(Un
Shell cwd was reset to /work
Bash input
{
  "command": "cd /tmp; grep -a -o -i \".\\{300\\}A\\s*-\\s*3.\\{1500\\}\" pz1.txt | tail -2 | cut -c1-2500; grep -a -o -i \"Debye-H.\\{0,10\\}ckel  *\\(slope\\|parameter\\).\\{200\\}\" pz1.txt | head -5"
}
Bash output
24  1.045  0.016  z  280.0  600.0  -3.0051  -7.052  5.426  -0.468  1.057  0.018  $\)  290.0  600.0  -2.7758  -7.851  5.844  -0.516  1.068  0.020  iD  300.0  600.0  -2.5521  -8.753  6.318  -0.571  1.079  0.023  CD  41>  CD  ===== !- "  =r  CD  ?  ::u  J  .....  J\273  z  9  ...  iii  0\)  ,.  Table  A-3  \(continued\).  The  Standard  Enthalpies  Jf  Water  and  NaCl\(aq\),  Debye-Huckel  Parameter  AL  and  ,  \225  .  0  0  a  -0  NaCl  V:.n.al  Paramete:-s  at  t  and  P.  HI  is  the  of_Water,  HI  \(T  ,P\)  - HI  \(0  K,  ideal  gas\)  and  H2  the  Enthalpy  of  NaCl\(aq\)  in  its  Standard  State,  - K,  I  atm\).  t  \260c  0.0  10.0  20.0  25.0  30.0  40.0  50.0  60.0  70.0  80.0  90.0  100.0  110.0  120.0  130.0  140.0  150.0  160.0  170.0  180.0  190.0  200.0  210.0  220.0  230.0  240.0  250.0  260.0  270.0  280.0  290.0  300.0  P  bar  800.0  800.0  800.0  800.0  800.0  800.0  800.0  800.0  800.0  800.0  800.0  800.0  800.0  800.0  800.0  800.0  800.0  800.0  800.0  800.0  800.0  800.0  800.0  800.0  800.0  800.0  800.0  800.0  800.0  800.0  800.0  800.0  -15.2342  -14.3935  -13.6081  -13.2343  -12.8726  -12.1830  -11.5356  -10.9267  -10.3531  -9.8116  -9.2994  -8.8143  -8.3539  -7.9164  -7.5001  -7.1034  -6.7249  -6.3633  -6.0174  -5.6862  -5.3686  -5.0637  -4.7707  -4.4886  -4.2168  -3.9545  -3.7010  -3.4558  -3.2182  -2.9878  -2.7639  -2.5461  0.333  0.163  0.045  -0.005  -0.051  -0.135  -0.210  -0.281  -0.345  -0.416  -0.496  -0.587  -0.690  -0.809  -0.943  -1.095  -1.266  -1.456  -1.669  -1.904  -2.164  -2.451  -2.768  -3.117  -3.501  -3.924  -4.391  -4.907  -5.479  -6.116  -6.831  -7.641  \(kg/mol\)  1/2  0.502  0.587  0.674  0.719  0.765  0.858  0.955  1.055  1.158  1.266  1.378  1.494  1.614  1.740  1.871  2.007  2.148  2.296  2.450  2.610  2.777  2.9
024  -0.021  -0.019  -0.017  -0.015  -0.013  -0.010  -0.008  -0.006  -0.004  -0.002  0.000  0.002  0.004  0.006  0.008  0.010  0.012  0.014  0.017  0.019  0.022  0.025  0.029  Co\)  o  ."  m  13  ."  m  :u  m  13  l>  Z  o  ID  c:  fR  -<  ===== ;!!  9  CD  ::u  j  -"  z  9  -"  CD  011  ..  Table  A-3  \(continued\).  The  Standard  Enthalpies  of  Water  and  NaCI\(aq\),  Debye-Huckel  Parameter  AL  and  o  0-0  NaCI  Virial  Parameters  at  t  and  P.  HI  is  the  K,  ideal  gas\)  and  H2  the  Enthalpy  of  NaCl\(aq\)  in  its  Standard  State,  H 2 \(T,P\)-H 2 \(298K,  I  atm\).  t  \260c  0.0  10.0  20.0  25.0  30.0  40.0  50.0  60.0  70.0  80.0  90.0  100.0  110.0  120.0  130.0  140.0  150.0  160.0  170.0  180.0  190.0  200.0  210.0  220.0  230.0  240.0  250.0  260.0  270.0  280.0  290.0  300.0  p  bar  1000.0  1000.0  1000.0  1000.0  1000.0  1000.0  1000.0  1000.0  1000.0  1000.0  1000.0  1000.0  1000.0  1000.0  1000.0  1000.0  1000.0  1000.0  1000.0  1000.0  1000.0  1000.0  1000.0  1000.0  1000.0  1000.0  1000.0  1000.0  1000.0  1000.0  1000.0  1000.0  -15.0900  -14.2571  -13.4786  -13.1081  -12.7495  -12.0658  -11.4238  -10.8201  -10.2513  -9.7144  -9.2066  -8.7255  -8.2691  -7.8355  -7.4228  -7.0297  -6.6546  -6.2964  -5.9539  -5.6261  -5.3118  -5.0102  -4.7205  -4.4418  -4.1735  -3.9147  -3.6649  -3.4235  -3.1899  -2.9636  -2.7440  -2.5308  0.253  0.149  0.079  0.047  0.018  -0.036  -0.085  -0.131  -0.171  -0.223  -0.284  -0.356  -0.442  -0.543  -0.659  -0.792  -0.943  -1.114  -1.304  -1.516  -1.752  -2.012  -2.300  -2.617  -2.967  -3.353  -3.780  -4.254  -4.781  -5.372  -6.041  -6.809  \(kg/mol\)  1/2  0.495  0.577  0.661  0.704  0.748  0.838  0.931  1.026  1.125  1.228  1.334  1.444  1.557  1.675  1.797  1.923  2.053  2.188  2.326  2.470  2.617  2.769  2.9
Debye-Hiickel  parameters  forthe  osmotic  coefficient,  enthalpy  and  heat  capacity  "Ion  size"  parameterin  Pitzer's  equations,  b  =  1.2  kgl/2  1/2  BMX.Btx,.B:.oc  Pairwise  ion-interaction  parameters  of  Pitze
Debye-Huckel  slope  for  enthalpy  as  defined  by  Bradley  and  Pitzer 23 ;  this  is  smaller  by  the  factor  2/3  than  the  definition  nsecl  earlier 2  The  experimental  determination  of  the  enthalpy  of  
Debye-Hiickel  slope  for  the  heat  capacity  as  recently  defined. 23  Two  equations  of  Bradley  and  Pitzer Z3  require  correc\255 tion  as  follows  Av  =  2A\242RT  [3\(BlnDIBP\)T  -{3],  A Ex  =  \(aAvIBT\)p.
Debye-Hiickel  parameter.  It  remains  to  choose  a  represen\255 tation  for  the  absolute  properties  of  the  solute  as  11  function  of  temperature.  The  prima  Jacie  choice  would  be  the  standard  state:  th
Debye-Hiickel  slope  and  the  virial  coefficients  \(Eqs.  \(43H45\)].  This  method  depends  pri\255 marily  on  the  osmotic  coefficient  and  heat  of  dilution  mea\255 surements.  The  second  method  involves
Shell cwd was reset to /work
WebSearch input
{
  "query": "NaCl aqueous 25 °C relative apparent molar enthalpy L_φ values J/mol at m = 0.1, 0.5, 1.0, 2.0 table Silvester Pitzer 1977 heat of dilution sodium chloride"
}
WebSearch output
Web search results for query: "NaCl aqueous 25 °C relative apparent molar enthalpy L_φ values J/mol at m = 0.1, 0.5, 1.0, 2.0 table Silvester Pitzer 1977 heat of dilution sodium chloride"

Links: [{"title":"This is an electronic reprint of the original article.","url":"https://aaltodoc.aalto.fi/server/api/core/bitstreams/9215f31d-4062-4565-922b-929b9d201174/content"},{"title":"Vol.:(0123456789) Journal of Solution Chemistry","url":"https://d-nb.info/1352894033/34"},{"title":"Apparent molar property","url":"https://en.wikipedia.org/wiki/Apparent_molar_property"},{"title":"This is an electronic reprint of the original article.","url":"https://aaltodoc.aalto.fi/server/api/core/bitstreams/8a8efdcb-a5ee-4fec-8d50-949aae744598/content"},{"title":"Effect of Amino Acids and Sodium Chloride on d-Sorbitol in Aqueous Solutions at Different Temperatures: Volumetric and Acoustic Approach","url":"https://www.ncbi.nlm.nih.gov/pmc/articles/PMC6244888/"},{"title":"(PDF) Thermodynamic Properties of NaCl Solutions at Subzero Temperatures","url":"https://www.researchgate.net/publication/226193918_Thermodynamic_Properties_of_NaCl_Solutions_at_Subzero_Temperatures"},{"title":"Dissolution enthalpy of NaCl in water at 25°C, 45°C and 60°C. Determination of the Pitzer's parameters of the {H2ONaCl} system and the molar dissolution enthalpy at infinite dilution of NaCl in water between 25°C and 100°C - ScienceDirect","url":"https://www.sciencedirect.com/science/article/abs/pii/S0040603197000580"},{"title":"Heat of Solution or Enthalpy of Solution Chemistry Tutorial","url":"https://www.ausetute.com.au/heatsolution.html"},{"title":"s10953 023 01322 y","url":"https://link.springer.com/article/10.1007/s10953-023-01322-y"},{"title":"enthalpy of dilution of acids.pdf","url":"https://diverdi.colostate.edu/all_courses/CRC%20reference%20data/enthalpy%20of%20dilution%20of%20acids.pdf"}]

The search did not return the L_φ values you asked for (NaCl at 25 °C, m = 0.1, 0.5, 1.0 and 2.0). None of the results contains the Silvester & Pitzer (1977) table or numbers at those molalities.

The most relevant results:
- **Aalto University / Journal of Solution Chemistry papers** (aaltodoc.aalto.fi and link.springer.com/article/10.1007/s10953-023-01322-y). These are recent Pitzer-model studies of NaCl(aq) enthalpies. One lists a table of recommended relative apparent molar enthalpy (ΔH_app) values for NaCl at 273.15–298.15 K as a function of molality. The snippet did not include the numbers. They would be the best place to look, since 298.15 K is 25 °C.
- **ScienceDirect, S0040603197000580.** It fits NaCl dissolution enthalpy data with Pitzer's ion-interaction model. The snippet gave no L_φ values.

The other results (apparent molar volumes, heat of solution, acid dilution) were not relevant.

A narrower follow-up search could help, for example the Silvester & Pitzer paper itself (J. Solution Chem. 1978, "Thermodynamics of electrolytes. 8. High-temperature properties, including enthalpy and heat capacity, with application to sodium chloride"). The Aalto/Springer papers' Table 12 is another option.


REMINDER: You MUST include the sources above in your response to the user using markdown hyperlinks.
WebFetch input
{
  "url": "https://aaltodoc.aalto.fi/server/api/core/bitstreams/9215f31d-4062-4565-922b-929b9d201174/content",
  "prompt": "Find the table of NaCl relative apparent molar enthalpy / enthalpy of dissolution vs molality at 298.15 K (and 273.15–298.15 K). Quote numbers, units, table number, title, sign convention, and the paper's title/authors/journal."
}
WebFetch output
The server returned HTTP 403 Forbidden.

The response body was not retrieved. If this URL requires authentication, use an authenticated tool (e.g. `gh` for GitHub, or an MCP-provided fetch tool) instead of WebFetch.
WebFetch input
{
  "url": "https://link.springer.com/article/10.1007/s10953-023-01322-y",
  "prompt": "Find any table of NaCl enthalpy of dissolution / relative apparent molar enthalpy ΔH_app vs molality at 298.15 K, with numbers. Quote values, title, authors, table numbers, sign convention."
}
WebFetch output
REDIRECT DETECTED: The URL redirects to a location that was not fetched automatically.

    Original URL: https://link.springer.com/article/10.1007/s10953-023-01322-y
    Redirect URL (from the server's Location header — server-supplied, not verified): https://idp.springer.com/authorize?response_type=cookie&client_id=springerlink&redirect_uri=https%3A%2F%2Flink.springer.com%2Farticle%2F10.1007%2Fs10953-023-01322-y
    Status: 303 See Other

    To complete your request, I need to fetch content from the redirected URL. Please use WebFetch again with these parameters:
    - url: "https://idp.springer.com/authorize?response_type=cookie&client_id=springerlink&redirect_uri=https%3A%2F%2Flink.springer.com%2Farticle%2F10.1007%2Fs10953-023-01322-y"
    - prompt: "Find any table of NaCl enthalpy of dissolution / relative apparent molar enthalpy ΔH_app vs molality at 298.15 K, with numbers. Quote values, title, authors, table numbers, sign convention."
Bash input
{
  "command": "cd /work; python /harness/src/vcs_protocol.py tool request --kind literature --question 'Need a primary/authoritative tabulation of the integral enthalpy of solution of NaCl(cr) in water (or equivalently relative apparent molar enthalpy Phi_L) at 25 C (298.15 K) for final molalities 0.3-0.9 mol/kg (salt/water mass ratio 0.02-0.05), e.g. Parker NSRDS-NBS 2 (1965) Table, Silvester & Pitzer J. Solution Chem. 1978, Pitzer-Peiper-Busey JPCRD 13 (1984), or Archer JPCRD 21 (1992) 793. Please give exact table/page, values (with sign convention and uncertainty), and the temperature coefficient d(DeltaH_sol)/dT near 20-25 C if available (I plan Kirchhoff: DeltaH(20C)=DeltaH(25C)+int(Cp_solid-Phi_Cp)dT). Already verified by me: CRC Handbook 91st ed, Enthalpy of Solution of Electrolytes: NaCl(c) +3.88 kJ/mol at 25 C, infinite dilution (ref Parker 1965). Not yet verified: finite-concentration correction and its sign.'"
}
Bash output
{"id": "180d002e6c9a4035a8dddd93761649af", "kind": "literature", "question": "Need a primary/authoritative tabulation of the integral enthalpy of solution of NaCl(cr) in water (or equivalently relative apparent molar enthalpy Phi_L) at 25 C (298.15 K) for final molalities 0.3-0.9 mol/kg (salt/water mass ratio 0.02-0.05), e.g. Parker NSRDS-NBS 2 (1965) Table, Silvester & Pitzer J. Solution Chem. 1978, Pitzer-Peiper-Busey JPCRD 13 (1984), or Archer JPCRD 21 (1992) 793. Please give exact table/page, values (with sign convention and uncertainty), and the temperature coefficient d(DeltaH_sol)/dT near 20-25 C if available (I plan Kirchhoff: DeltaH(20C)=DeltaH(25C)+int(Cp_solid-Phi_Cp)dT). Already verified by me: CRC Handbook 91st ed, Enthalpy of Solution of Electrolytes: NaCl(c) +3.88 kJ/mol at 25 C, infinite dilution (ref Parker 1965). Not yet verified: finite-concentration correction and its sign.", "time": "2026-11-17T23:50:40.714535+00:00"}
Stop this cycle. A human must supply the actual response before you resume.
assistant
I've stopped this cycle on a literature request. There is no new candidate, and `Model.lean`, `candidate.json` and `report.txt` are unchanged from attempt 004.

**Checked this cycle**
- **Dissolution enthalpy at infinite dilution:** I read the CRC Handbook of Chemistry and Physics, 91st edition, "Enthalpy of Solution of Electrolytes". It lists NaCl(c) at +3.88 kJ/mol for 1 mol of crystalline solute dissolved in an infinite amount of water at 25 °C. It cites Parker, NSRDS-NBS 2 (1965).
- **Why that doesn't match the model:** it is a 25 °C infinite-dilution value, whereas my model dissolves salt at 20 °C to about 0.3–0.9 mol/kg. The earlier "NBS ≈ 3.88 kJ/mol at 20 °C" was therefore not matched to the model.
- **Laliberté heat-capacity formula:** from the `thermo` library source, the correlation is Cp_i = a1·exp(a2·t + a3·e^(0.01·t) + a4·w) + a5·w^a6. Here t is in °C, w is the solute mass fraction, and Cp_i is in kJ/kg/K. For NaCl the six coefficients (a1–a6) come from the `chemicals` data table, and its fit range is 1.5–120 °C and w ≤ 0.26. I did not fetch the original paper (J. Chem. Eng. Data 54, 1725, 2009).
- **A simplification:** by definition the salt's heat-capacity contribution is Cs(T) = m_salt·Cp_i(T, w), so no molar mass is needed. As a spot check, Cp_i ≈ −1.05 kJ/kg/K at 20 °C and w = 0.035, which is negative.

**Request**
- **Missing input:** I could not retrieve a finite-concentration value, so I sent a literature request for the integral enthalpy of solution (or Φ_L, the relative apparent molar enthalpy) at 0.3–0.9 mol/kg at 25 °C. It also asks for the temperature coefficient near 20–25 °C. I won't guess this value.
- **Why it matters:** it decides whether the salt-addition cooling stays positive up to the boiling point, with a margin of roughly 0.6–1.2 kJ/mol.

**Plan once the response arrives**
1. **Replace the secant average:** model the solution enthalpy K(T) = Cw(T−Tr) + E(T), with E(T) = n·hsol + ∫nΦ(T) dT and Φ(T) taken pointwise from the Laliberté correlation. Prove that the "salt cools the water" condition holds at every temperature up to Tb, using Φ ≤ 0 so that E is decreasing.
2. **Generalise the timing theorem:** prove "earlier salting is no slower" for this temperature-dependent heat capacity, parametrised by the post-mixing solution temperature so no inverse function is needed.
3. **Prove the bounds in Lean:** show the Φ bounds from the correlation with explicit enclosures for exp (a product (1 ± x/k)^k, split into temperature subintervals). Use Kirchhoff's law to get hsol at 20 °C from the 25 °C value, so only the sign of the salt's solid heat capacity is needed.
4. **Mark the sources:** keep the 25 °C value as a labelled reviewable input with its source. If the finite-concentration data weaken the margin, I will revise the regime or the conclusion rather than tune the evidence.

Sources:
- [CRC Handbook 91st ed., Enthalpy of Solution of Electrolytes (Purdue copy)](https://www.purdue.edu/science/archive/docs/science-express/labs/Enthalpy%20of%20Solution_Reference.pdf)
- [thermo.electrochem source (Laliberté functions)](https://thermo.readthedocs.io/_modules/thermo/electrochem.html)
- [chemicals Laliberte2009.tsv](https://raw.githubusercontent.com/CalebBell/chemicals/master/chemicals/Electrolytes/Laliberte2009.tsv)

Round 6

Messages

user
Parker NSRDS-NBS2 (1965) is now supplied in full, together with Archer (1992) Parts I and II. The complete bundle is at /work/inputs/material-literature-002. READING-NOTES.txt gives exact printed/PDF page mappings; sources.json records origin URLs, DOIs and file hashes. Read the PDFs, their searchable text, original-page PNGs and selected CSV transcriptions.

For your finite-concentration question, Parker Table X gives relative apparent enthalpy at25C; the NaCl discussion on printed p26 gives the infinite-dilution dissolution anchor; Table II C gives apparent heat capacity at15,20,25,30C. Source energies use thermochemical calories, not joules. These are different quantities: do not treat relative enthalpy as complete dissolution heat or apparent capacity as the crystalline-salt capacity. The tables contain selected source points, not a certified interpolation or a uniform20C-to-boiling law. Archer Part I supplies a full solid heat-capacity representation, and Part II supplies an internally linked aqueous thermodynamic fit and coefficients. Keep temperature-dependent reactant references and integration limits explicit when applying Kirchhoff's relation.

No precomputed timing answer, other agent's model or proof, physical experiment, certified interpolation or uniform numerical bound has been supplied. Calculate the property values from a stated primary representation with your own tools and derive the needed quantity and its sign in Lean. Expose empirical/model assumptions and source-domain limitations; do not assume a property envelope merely because it gives the desired timing result. If another primary coefficient is indispensable, identify it specifically. These sources do not approve the model or conclusion.
assistant
I'll start by reviewing the current state of /work.
Bash input
{
  "command": "cd /work; ls -la; ls inputs inputs/* | head -50; cat inputs/material-literature-002/READING-NOTES.txt | head -80"
}
Bash output
total 64
drwxrwxr-x  5 1001 1001  4096 Nov 18 00:50 .
drwxr-xr-x  1 root root  4096 Nov 18 00:51 ..
-rw-r--r--  1 1001 1001 13320 Nov 17 16:13 Model.lean
-rw-r--r--  1 1001 1001  1934 Nov 17 16:06 candidate.json
drwxr-xr-x 23 1001 1001  4096 Nov 17 16:13 checks
-rw-------  1 1001 1001  2993 Nov 17 23:46 human-feedback.json
drwx------  3 1001 1001  4096 Nov 18 00:50 inputs
-rw-------  1 1001 1001 13154 Nov 17 16:14 last-review-packet.json
-rw-r--r--  1 1001 1001  2602 Nov 17 16:06 report.txt
drwxr-xr-x  2 1001 1001  4096 Nov 18 00:50 requests
inputs/180d002e6c9a4035a8dddd93761649af.json

inputs:
180d002e6c9a4035a8dddd93761649af.json
material-literature-002

inputs/material-literature-002:
READING-NOTES.txt
archer-1992-part1-crystalline.pdf
archer-1992-part1-crystalline.txt
archer-1992-part2-solution.pdf
archer-1992-part2-solution.txt
archer-solid-knots.csv
parker-1965-nsrds2.pdf
parker-1965-nsrds2.txt
parker-apparent-heat-capacity.csv
parker-enthalpy-infinity.png
parker-heat-capacity.png
parker-relative-enthalpy-25C.csv
parker-table-X.png
part1-eqs.png
part1-knots.png
part2-coefficients.png
part2-definitions.png
part2-enthalpy-capacity.png
part2-temperature-functions.png
sources.json
Primary thermodynamic literature supplied by the operator, 18 November 2026

This is literature and source transcription, not a physical experiment or a timing recommendation. It contains no other scientist's model, proof or answer. PDF page numbers below are one-based and include front matter.

Parker, Thermal Properties of Aqueous Uni-univalent Electrolytes, NSRDS-NBS 2 (1965), DOI 10.6028/NBS.NSRDS.2.
https://nvlpubs.nist.gov/nistpubs/Legacy/NSRDS/nbsnsrds2.pdf
Printed p26 / PDF p34: NaCl(cr) infinite-dilution dissolution enthalpy at 25 C is +928 +/-5 cal/mol. Table X, printed p50 / PDF p58, gives the relative apparent molar enthalpy Phi_L at 25 C. Table II C, printed p43 / PDF p51, gives the apparent molar heat capacity Phi_C at 15,20,25,30 C. Table XIX, printed p64 / PDF p72, lists contributing infinite-dilution results. Source units are thermochemical calories: 1 cal=4.1840 J. Phi_L is relative to infinite dilution, not the complete dissolution enthalpy; Phi_C is apparent heat capacity, not the solid's heat capacity. Sparse table entries do not establish interpolation, a derivative between rows, or a temperature-uniform enclosure. See the CSVs and original page images. The quoted +/-5 is Parker's uncertainty statement, not a certified uniform error for an interpolating model.

Archer, Thermodynamic Properties of the NaCl+H2O System, Part I: NaCl(cr), JPCRD21,1-21 (1992), DOI 10.1063/1.555913.
https://srd.nist.gov/jpcrdreprint/1.555913.pdf
Printed p4 / PDF p5, Eqs4-6: fit f(T) with a natural cubic spline; b=.25, T0=1 K, Cp0=1 J/(mol K); Cp_cr/Cp0=[T/(T0*f(T)+b*T)]^3. Table3, printed p11 / PDF p12, supplies all knot abscissas and values (see archer-solid-knots.csv); both end conditions are natural. This is a spline of the transformed f, not a piecewise-linear fit of Cp. Table4, printed p15 / PDF p16, supplies numerical checks, including Cp(298.15K)=50.16 J/(mol K). Solid enthalpy increments follow from integrating the defined heat capacity. Printed p12 discusses empirical accuracy: the roughly .3% statement applies to 300..800K; do not apply it automatically below 300K. This representation differs from the older JANAF value 50.509 at 298.15K. Keep one declared thermodynamic reference or account for differences.

Archer, Part II: NaCl(aq), NaCl.2H2O(cr), and phase equilibria, JPCRD21,793-829 (1992), DOI 10.1063/1.555915.
https://srd.nist.gov/jpcrdreprint/1.555915.pdf
Printed p796 / PDF p5: Eq2 explicitly uses Part I's solid heat-capacity integrals, Tr=298.15K, pr=.1MPa. Section4.1, printed pp797-801 / PDF pp6-10, supplies the modified ion-interaction model. Eq9 is relative apparent enthalpy; Eq12 is apparent molar heat capacity; Eq20 defines an apparent property. Eqs21-22 retain the reference solution at mr=6 mol/kg; Eqs32-38 define the temperature/pressure functions; Table2 gives coefficients. Eq38's heat-capacity scale is 1 kJ/(mol K), not 1 J/(mol K). Table2's anhydrous -9.040721 kJ/mol parameter is a Gibbs-energy anchor, not a dissolution-enthalpy anchor. Eq2's solid entropy at Tr is 72.2653 J/(mol K); the solute entropy in Table2 is 115.5108 J/(mol K). Check conversion to enthalpy rather than substituting G for H. Blank coefficient entries are omitted terms. Pages798-801 have been rendered for inspecting symbols and signs. Original model uses Hill's water EOS and the dielectric relation of Archer/Wang (references3,14), with Debye-Huckel definitions from Bradley/Pitzer (reference15). Substituting another water representation is a model change to document. Validation tables and phase-equilibrium section5 are included in the full article. The fitted data span approximately250..600K; physical liquid-phase validity still needs checking at the chosen pressure/composition.

Operator boundaries for use

The source pages, table units, selected Parker entries and Part I knot values were visually inspected. The full aqueous EOS has not been implemented or numerically certified by the operator. No grid evaluation, extremum, certified interval enclosure, pure-water/brine boiling interval or uniform capacity bound is supplied. Implement and derive the material model from the primary equations, retain your calculation provenance, and prove the consequences needed in Lean. State any indispensable missing primary coefficient explicitly rather than selecting a favorable sign or treating sparse table samples as bounds.

Use consistent definitions of isothermal dissolution from same-temperature reactants, the stored solid's sensible enthalpy, absolute apparent heat capacity and relative infinite-dilution corrections. A sparse-temperature secant is not a local heat-capacity law. Phase boundaries must follow from pressure and composition; do not assume the brine boiling point is below100C. Keep apparatus assumptions separate from property evidence. This literature response does not approve a model or conclusion.
Bash input
{
  "command": "cd /work; cat Model.lean; cat report.txt; cat candidate.json; ls checks | tail -3; cd inputs/material-literature-002; cat parker-apparent-heat-capacity.csv parker-relative-enthalpy-25C.csv | head -80"
}
Bash output
import Mathlib.Analysis.SpecialFunctions.Log.Deriv
import Mathlib.Analysis.Calculus.MeanValue

/-! Model (see report.txt).  H = enthalpy (J) measured from "everything at room temperature Tr".
 Pure water, heat capacity Cw: temperature Tr + H/Cw.
 Salt solution, total heat capacity Cw+Cs>0 (Cs of EITHER sign; literature: Cs<0), dissolution enthalpy D (at Tr) of either sign:
 temperature Tr + (H-D)/(Cw+Cs).  Salt (at Tr) is mixed in instantly, so H is conserved.
 Heater power P constant, loss h*(T-Tr):  dH/dt = P - h*(T-Tr).
 Separating variables, the time to move from H1 to H2 is Fw/Fs(H2) - Fw/Fs(H1) where
 Fw' = 1/(P - h*(H/Cw)), Fs' = 1/(P - h*((H-D)/(Cw+Cs))) (explicit logarithms below).
 Tb = boiling point of the final solution (fixed by salt amount, not by timing). -/

/-- time-integral of dH/(P - loss) for pure water (antiderivative, h>0) -/
noncomputable def Fw (Cw P h H : ℝ) : ℝ := -(Cw/h) * Real.log (P - h*(H/Cw))
/-- same for the salt solution -/
noncomputable def Fs (Cw Cs D P h H : ℝ) : ℝ := -((Cw+Cs)/h) * Real.log (P - h*((H-D)/(Cw+Cs)))

/-- Total time to reach Tb from T0 when the salt is added once the water is at Ta:
 heating water alone from T0 to Ta, then solution from the post-mixing state up to Tb. -/
noncomputable def boilTime (Cw Cs D P h Tr T0 Tb Ta : ℝ) : ℝ :=
  (Fw Cw P h (Cw*(Ta-Tr)) - Fw Cw P h (Cw*(T0-Tr))) +
  (Fs Cw Cs D P h ((Cw+Cs)*(Tb-Tr)+D) - Fs Cw Cs D P h (Cw*(Ta-Tr)))

lemma G_deriv (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs))) :
    ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), HasDerivAt (fun H => Fw Cw P h H - Fs Cw Cs D P h H)
      (1/(P - h*(H/Cw)) - 1/(P - h*((H-D)/(Cw+Cs)))) H := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  intro H hH
  have a1 : HasDerivAt (fun H : ℝ => P - h*(H/Cw)) (-(h/Cw)) H := by
    have := ((hasDerivAt_id H).div_const Cw).const_mul h
    simpa [div_eq_mul_inv, mul_comm] using (this.const_sub P)
  have a2 : HasDerivAt (fun H : ℝ => P - h*((H-D)/(Cw+Cs))) (-(h/(Cw+Cs))) H := by
    have := (((hasDerivAt_id H).sub_const D).div_const (Cw+Cs)).const_mul h
    simpa [div_eq_mul_inv, mul_comm] using (this.const_sub P)
  have l1 := (a1.log (hw H hH).ne').const_mul (-(Cw/h))
  have l2 := (a2.log (hs H hH).ne').const_mul (-((Cw+Cs)/h))
  have := l1.sub l2
  convert this using 1
  · funext x; rfl
  · have e1 := (hw H hH).ne'
    have e2 := (hs H hH).ne'
    have e3 := hh.ne'
    have e4 := hCw.ne'
    have e5 := hC.ne'
    field_simp

/-- Salt added at water temperature x changes the temperature at once by
 (D + Cs (x-Tr))/(Cw+Cs) downward (a cooling if D + Cs (x-Tr) > 0).  -/
lemma mix_iff (Cw Cs D Tr x : ℝ) (hCw : 0 < Cw) (hC : 0 < Cw + Cs) :
    (Cw*(x-Tr) - D)/(Cw+Cs) ≤ Cw*(x-Tr)/Cw ↔ 0 ≤ D + Cs*(x-Tr) := by
  rw [mul_div_cancel_left₀ _ hCw.ne', div_le_iff₀ hC]
  constructor <;> intro h <;> nlinarith

lemma G_mono (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs)))
    (S : Set ℝ) (hS : S ⊆ Set.Icc 0 (Cw*(Tb-Tr))) (hconv : Convex ℝ S)
    (sgn : ∀ H ∈ S, (H-D)/(Cw+Cs) ≤ H/Cw) :
    MonotoneOn (fun H => Fw Cw P h H - Fs Cw Cs D P h H) S := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  have hd := G_deriv Cw Cs D P h Tr Tb hCw hC hh hP hs
  apply monotoneOn_of_deriv_nonneg hconv
  · intro H hH; exact (hd H (hS hH)).continuousAt.continuousWithinAt
  · intro H hH; exact (hd H (hS (interior_subset hH))).differentiableAt.differentiableWithinAt
  · intro H hH
    have hH' := interior_subset hH
    rw [(hd H (hS hH')).deriv]
    apply sub_nonneg.mpr
    apply one_div_le_one_div_of_le (hw H (hS hH'))
    have := sgn H hH'
    nlinarith

lemma G_anti (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs)))
    (S : Set ℝ) (hS : S ⊆ Set.Icc 0 (Cw*(Tb-Tr))) (hconv : Convex ℝ S)
    (sgn : ∀ H ∈ S, H/Cw ≤ (H-D)/(Cw+Cs)) :
    AntitoneOn (fun H => Fw Cw P h H - Fs Cw Cs D P h H) S := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  have hd := G_deriv Cw Cs D P h Tr Tb hCw hC hh hP hs
  apply antitoneOn_of_deriv_nonpos hconv
  · intro H hH; exact (hd H (hS hH)).continuousAt.continuousWithinAt
  · intro H hH; exact (hd H (hS (interior_subset hH))).differentiableAt.differentiableWithinAt
  · intro H hH
    have hH' := interior_subset hH
    rw [(hd H (hS hH')).deriv]
    apply sub_nonpos.mpr
    apply one_div_le_one_div_of_le (hs H (hS hH'))
    have := sgn H hH'
    nlinarith

/-- Comparison lemma (sign-free; proved, used by the answer below).  Salt of fixed amount (Cs, D arbitrary sign, C=Cw+Cs>0).
 If between water temperatures Ta1 ≤ Ta2 salt addition always COOLS (D+Cs(x-Tr) ≥ 0 for x in [Ta1,Ta2]),
 adding at Ta1 is no slower than at Ta2; if it always WARMS (≤ 0), adding at Ta2 is no slower. -/
theorem compare_theorem : ∀ (Cw Cs D P h Tr T0 Tb : ℝ),
    0 < Cw → 0 < Cw + Cs → 0 < h → h*(Tb-Tr) < P → Tr ≤ T0 →
    (∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs))) →
    ∀ Ta1 Ta2 : ℝ, T0 ≤ Ta1 → Ta1 ≤ Ta2 → Ta2 ≤ Tb →
      ((∀ x, Ta1 ≤ x → x ≤ Ta2 → 0 ≤ D + Cs*(x-Tr)) →
          boilTime Cw Cs D P h Tr T0 Tb Ta1 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta2) ∧
      ((∀ x, Ta1 ≤ x → x ≤ Ta2 → D + Cs*(x-Tr) ≤ 0) →
          boilTime Cw Cs D P h Tr T0 Tb Ta2 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta1) := by
  intro Cw Cs D P h Tr T0 Tb hCw hC hh hP hTr hs Ta1 Ta2 h1 h12 h2
  have hmem : ∀ x, Ta1 ≤ x → x ≤ Ta2 → Cw*(x-Tr) ∈ Set.Icc 0 (Cw*(Tb-Tr)) := fun x a b =>
    ⟨by nlinarith, by nlinarith⟩
  have hS : Set.Icc (Cw*(Ta1-Tr)) (Cw*(Ta2-Tr)) ⊆ Set.Icc 0 (Cw*(Tb-Tr)) := by
    intro H hH
    exact ⟨by nlinarith [hH.1], by nlinarith [hH.2]⟩
  have hHx : ∀ H ∈ Set.Icc (Cw*(Ta1-Tr)) (Cw*(Ta2-Tr)), ∃ x, Ta1 ≤ x ∧ x ≤ Ta2 ∧ H = Cw*(x-Tr) := by
    intro H hH
    refine ⟨H/Cw + Tr, ?_, ?_, ?_⟩
    · have : Ta1 - Tr ≤ H/Cw := by rw [le_div_iff₀ hCw]; nlinarith [hH.1]
      linarith
    · have : H/Cw ≤ Ta2 - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
      linarith
    · field_simp; ring
  constructor
  · intro hx
    have m := G_mono Cw Cs D P h Tr Tb hCw hC hh hP hs _ hS (convex_Icc _ _) (by
      intro H hH
      obtain ⟨x, a, b, rfl⟩ := hHx H hH
      exact (mix_iff Cw Cs D Tr x hCw hC).mpr (hx x a b))
      ⟨le_rfl, by nlinarith⟩ ⟨by nlinarith, le_rfl⟩ (by nlinarith)
    simp only [boilTime]; simp only at m; linarith
  · intro hx
    have m := G_anti Cw Cs D P h Tr Tb hCw hC hh hP hs _ hS (convex_Icc _ _) (by
      intro H hH
      obtain ⟨x, a, b, rfl⟩ := hHx H hH
      have := hx x a b
      have h3 : ¬ ((Cw*(x-Tr) - D)/(Cw+Cs) < Cw*(x-Tr)/Cw) → True := fun _ => trivial
      rw [mul_div_cancel_left₀ _ hCw.ne', le_div_iff₀ hC]
      nlinarith)
      ⟨le_rfl, by nlinarith⟩ ⟨by nlinarith, le_rfl⟩ (by nlinarith)
    simp only [boilTime]; simp only at m; linarith


/-! ## Salt-specific part (NaCl, 1 L of water).  Inputs are labelled; nothing numerical below is a measurement.

 Choices.  Salt = NaCl.  1 L of water at 20 C has mass 0.998 kg (density 0.9982 kg/L).  The salt/water ratio
 r = (salt mass)/(water mass) (NOT the mass fraction of the final solution; that is w = r/(1+r), proved in
 `cs_theorem`).  Regime: 0.02 <= r <= 0.05, i.e. about 20-50 g NaCl in the litre (2-4.8 wt%).
 Cs is DERIVED, not fitted: by definition of the apparent molar heat capacity Phi of the salt in solution,
 (total heat capacity of solution) = (heat capacity of the water) + n*Phi, so Cs = n*Phi, n = moles of salt.
 Phi is temperature dependent; the lumped model needs one constant Cs, so Phi is the SECANT MEAN of Phi(T,m)
 over 20 C..Tb, which makes the linear mixing enthalpy D + Cs (T-Tr) exact at both 20 C and Tb.
 Empirical inputs (EXPOSED PREMISES, constants below), from the Laliberte (2009) NaCl correlation (fit range
 1.5-120 C, w <= 0.26) evaluated numerically outside Lean (a synthetic calculation from a published correlation)
 and from NBS tables: secant-mean Phi in [-41,-27] J/K/mol for r in [0.02,0.05] (computed -40.7 ... -27.9);
 enthalpy of solution of NaCl(s) hsol >= 3800 J/mol (NBS +3.88 kJ/mol); boiling point Tb in [100,101] C;
 pot+water heat capacity >= 4170 J/K (at least the 0.998 kg of water at 4.18 J/g/K).
 D = n*hsol is the enthalpy absorbed when salt at room temperature Tr dissolves at Tr. -/

/-- room / initial water temperature, C -/
noncomputable def Tr0 : ℝ := 20
/-- admissible range of the boiling point of the salt solution, C -/
noncomputable def TbLo : ℝ := 100
noncomputable def TbHi : ℝ := 101
/-- minimum heat capacity of pot + water, J/K -/
noncomputable def CwMin : ℝ := 4170
/-- salt/water mass-ratio regime -/
noncomputable def rLo : ℝ := 1/50
noncomputable def rHi : ℝ := 1/20
/-- NaCl molar mass g/mol, mass of 1 L of water at 20 C in kg -/
noncomputable def Mnacl : ℝ := 58.44
noncomputable def massWater : ℝ := 0.998
/-- moles of NaCl for salt/water mass ratio r (1 kg = 1000 g) -/
noncomputable def nSalt (r : ℝ) : ℝ := 1000*massWater*r/Mnacl
/-- empirical bounds of the secant-mean apparent molar heat capacity of NaCl(aq) over 20 C..Tb, J/K/mol -/
noncomputable def PhiLo : ℝ := -41
noncomputable def PhiHi : ℝ := -27
/-- derived floor for Cs, J/K (n <= 0.854 mol times -41 J/K/mol) -/
noncomputable def CsFloor : ℝ := -36
/-- lower bound of the enthalpy of solution of NaCl(s) at room temperature, J/mol -/
noncomputable def hsolMin : ℝ := 3800

lemma nSalt_pos (r : ℝ) (h : rLo ≤ r) : 0 < nSalt r := by
  unfold nSalt massWater Mnacl; unfold rLo at h; positivity

lemma nSalt_le (r : ℝ) (h : r ≤ rHi) : nSalt r ≤ 0.8540 := by
  unfold nSalt massWater Mnacl
  unfold rHi at h
  rw [div_le_iff₀ (by norm_num)]; nlinarith

/-- Cs = n*Phi: sign, size, and the ratio conversion. -/
theorem cs_theorem : ∀ (r Phi ms mwater : ℝ),
    rLo ≤ r → r ≤ rHi → PhiLo ≤ Phi → Phi ≤ PhiHi → 0 < ms → 0 < mwater →
    (nSalt r * Phi < 0 ∧ CsFloor ≤ nSalt r * Phi) ∧ ms/(ms+mwater) = (ms/mwater)/(1+ms/mwater) := by
  intro r Phi ms mwater hr1 hr2 hP1 hP2 hms hmwa
  have hn0 := nSalt_pos r hr1
  have hn1 := nSalt_le r hr2
  unfold PhiLo at hP1; unfold PhiHi at hP2; unfold CsFloor
  refine ⟨⟨by nlinarith, by nlinarith⟩, ?_⟩
  field_simp
  ring

/-- ANSWER.  Accepted model (lumped heat capacity Cw, constant stove power P, Newton loss h, no evaporation,
 salt at room temperature Tr0 = T0 dissolves instantly, enthalpy conserved).  Under the premises above
 (r in [rLo,rHi], Cs = n*Phi, D = n*hsol, Tb in [TbLo,TbHi], stove stronger than losses at Tb), adding the
 salt right at the start (Ta = T0 = Tr0) is no slower than adding it at any later water temperature Ta <= Tb.
 The condition D + Cs (x-Tr) >= 0 (salt addition cools / does not warm) is CHECKED here, not assumed, and
 the 'stove beats losses' condition for the salt solution is derived from it. -/
theorem answer_theorem : ∀ (Cw P h Tb r Phi hsol : ℝ),
    TbLo ≤ Tb → Tb ≤ TbHi → CwMin ≤ Cw → 0 < h → h*(Tb-Tr0) < P →
    rLo ≤ r → r ≤ rHi → PhiLo ≤ Phi → Phi ≤ PhiHi → hsolMin ≤ hsol →
    ∀ Ta : ℝ, Tr0 ≤ Ta → Ta ≤ Tb →
      boilTime Cw (nSalt r*Phi) (nSalt r*hsol) P h Tr0 Tr0 Tb Tr0
        ≤ boilTime Cw (nSalt r*Phi) (nSalt r*hsol) P h Tr0 Tr0 Tb Ta := by
  intro Cw P h Tb r Phi hsol hb1 hb2 hCw hh hP hr1 hr2 hP1 hP2 hs Ta h1 h2
  have hn0 := nSalt_pos r hr1
  have hn1 := nSalt_le r hr2
  generalize nSalt r = n at hn0 hn1 ⊢
  unfold TbLo at hb1; unfold TbHi at hb2; unfold CwMin at hCw
  unfold PhiLo at hP1; unfold PhiHi at hP2; unfold hsolMin at hs
  have hTr : Tr0 = 20 := rfl
  rw [hTr] at hP h1 ⊢
  have hCs : n*Phi < 0 := by nlinarith
  have hCs2 : -36 ≤ n*Phi := by nlinarith
  have hCw0 : 0 < Cw := by linarith
  have hC : 0 < Cw + n*Phi := by linarith
  have hd : 0 ≤ Tb - 20 := by linarith
  have hd2 : Tb - 20 ≤ 81 := by linarith
  -- enthalpy of solution at the boiling point is still >= 0 (checked)
  have hE0 : 0 ≤ hsol + Phi*(Tb-20) := by nlinarith [mul_nonneg (by linarith : 0 ≤ Phi + 41) hd]
  have hE : 0 ≤ n*hsol + n*Phi*(Tb-20) := by nlinarith [mul_nonneg hn0.le hE0]
  have hstove : ∀ H ∈ Set.Icc 0 (Cw*(Tb-20)), 0 < P - h*((H-n*hsol)/(Cw+n*Phi)) := by
    intro H hH
    have : (H-n*hsol)/(Cw+n*Phi) ≤ Tb - 20 := by
      rw [div_le_iff₀ hC]; nlinarith [hH.2]
    nlinarith
  refine (compare_theorem Cw (n*Phi) (n*hsol) P h 20 20 Tb hCw0 hC hh hP (by linarith) hstove
    20 Ta le_rfl h1 h2).1 ?_
  intro x hx1 hx2
  nlinarith [mul_nonneg (by linarith : 0 ≤ Tb - x) (by linarith : 0 ≤ -(n*Phi))]
CYCLE 4 REVISION (response to: choose bounded ratio, derive Cs from an explicit model, prove sign/bounds, check conditions).
Accepted Newtonian lumped heating model kept unchanged (boilTime, compare_theorem unchanged).

Choices: NaCl; r = salt mass / water mass (not final mass fraction; w=r/(1+r) proved); 1 L at 20 C = 0.998 kg;
regime r in [0.02,0.05] (20-50 g NaCl in the litre, 2-4.8 wt%).
Cs: by definition of the apparent molar heat capacity, C_solution = C_water + n*Phi, so Cs = n*Phi, n = 1000*0.998*r/58.44 mol (<=0.854 mol).
Phi from the Laliberte (2009) NaCl correlation (coefficients from the 'chemicals' package data file; fit range 1.5-120 C, w<=0.26),
evaluated by me in Python (synthetic calculation from a published correlation, not a measurement), water Cp from IAPWS values.
Results (J/K/mol): Phi(20C) ~ -74 (r=.02) ... -52 (r=.05) [lit. infinite dilution ~ -88 at 25 C, consistent]; Phi rises toward ~-21..-31 near 70-80 C and falls to ~-31..-45 at 100 C.
So Cs<0 over the whole 20-100 C, r in regime; |Cs| <= ~45 J/K per kg water (about 1% of 4.2 kJ/K). The earlier cycle's sign change near 13 wt% is outside this regime.
Temperature dependence: model needs constant Cs; I use the secant mean Phi over 20..Tb, which makes the linear mixing enthalpy exact at 20 C and Tb.
Computed secant means: -40.7 (r=.02), -33.9 (.035), -27.9 (.05); bounds used [-41,-27]. The true mixing enthalpy E(x)=n(hsol+int Phi) is decreasing, with minimum at Tb:
E(Tb)/n ~ +630 (r=.02), +1170 (.035), +1650 (.05) J/mol, versus hsol=3880. (At r=0.01 it is only ~+200 and for r<~0.01 it would become negative near Tb: the regime was chosen to exclude this marginal case.)
Result: salt addition is endothermic at all temperatures 20..Tb (it cools), so earlier is better; compare_theorem (cooling over an interval favours the earlier addition) gives: salting at the start is no slower than at any later Ta. Lean checks D+Cs(x-Tr)>=0 from the premises and derives the stove-beats-loss condition for the solution (no longer a premise).
Size of effect: temperature drop on mixing <= ~hsol*n/C ~ 0.3-0.7 K at start; time difference is tiny relative to pot/stove variability. Also boil-over/avoid adding salt to boiling water argue for salting early.
Limits: premises (Phi bounds, hsol, Tb range) are inputs; Laliberte correlation accuracy is a few percent, so margin at r=0.02 (~15% of hsol) is adequate but at r<=0.01 the sign is unresolved. Not proved: quantitative time difference; Phi(T) handled by secant mean; no evaporation or salting-induced boiling-point change in time; no real measurement was run.
{
 "proof_file": "Model.lean",
 "narrative_file": "report.txt",
 "conclusions": {"answer": "answer_theorem", "cs": "cs_theorem"},
 "user_facts": [
  "1 L of water at 20 C in a pot on a stove, heated",
  "salt will be added; goal: salted water reaches its boiling point as soon as possible",
  "salt quantity, pot, heater, room unspecified"
 ],
 "model_choices": [
  "accepted Newtonian lumped heating model (Cw, constant power P, loss h(T-Tr), no evaporation), salt at Tr dissolves instantly, enthalpy conserved",
  "salt = NaCl; ratio r = salt mass / water mass in [0.02,0.05] (w=r/(1+r), proved); 1 L = 0.998 kg",
  "Cs = n*Phi by definition of apparent molar heat capacity; constant Phi = secant mean over 20 C..Tb of the Laliberte (2009) NaCl correlation, bounds [-41,-27] J/K/mol (computed outside Lean, synthetic calculation from a published correlation, fit range 1.5-120 C)",
  "D = n*hsol, hsol >= 3800 J/mol (NBS +3.88 kJ/mol); Tb in [100,101] C; Cw >= 4170 J/K; stove stronger than loss at Tb"
 ],
 "sources": [
  "Laliberte, J. Chem. Eng. Data 54, 1725 (2009) NaCl coefficients as distributed in the python 'chemicals' package 1.5.2 (Electrolytes/Laliberte2009.tsv); water Cp from standard IAPWS values",
  "NBS/CRC thermochemical tables for enthalpy of solution of NaCl (+3.88 kJ/mol), recalled, not re-verified in this run"
 ],
 "incorporated_inputs": [],
 "proof_scope": "cs_theorem: given r in [0.02,0.05] and Phi in [-41,-27], Cs=n*Phi is negative and >= -36 J/K; mass-ratio to mass-fraction conversion. answer_theorem: under the stated premises the cooling condition D+Cs(Tb-Tr)>=0 and the stove-beats-loss condition for the solution are DERIVED, and salting at the start is no slower than at any later temperature. Numerical Phi/hsol bounds are premises taken from a correlation/tables, not proved or measured; temperature dependence of Phi is handled only by the secant-mean approximation. Human review needed."
}
d2511eb386bb475db1cec32245800f7d
d34803db1d874cbd871ed1ec8eedf419
dcd7a1b461ab4ea1832bd51aed75b96c
molality_mol_per_kg,Phi_C_15C_cal_per_mol_K,Phi_C_20C_cal_per_mol_K,Phi_C_25C_cal_per_mol_K,Phi_C_30C_cal_per_mol_K
0,-25.2,-23.3,-21.5,-20.1
0.1388,-21.4,-19.8,-18.2,-16.9
0.1850,-20.5,-19.0,-17.5,-16.2
0.2775,-19.0,-17.5,-16.3,-15.1
0.3700,-17.7,-16.4,-15.2,-14.1
0.5551,-15.5,-14.8,-13.4,-12.5
0.7401,-13.6,-12.6,-11.7,-10.9
1.1101,-10.4,-9.6,-8.8,-8.2
molality_mol_per_kg,Phi_L_cal_per_mol
0,0
0.1388,87
0.1850,88
0.2775,83
0.5551,49
0.7401,20
1.1101,-41
Bash input
{
  "command": "cd /work; python3 -c \"import chemicals,os;print(chemicals.__file__)\" 2>&1; pip list 2>/dev/null | grep -i -E \"chemicals|thermo|scipy|mpmath|numpy|sympy\"; ls requests checks | head; cat human-feedback.json | head -5"
}
Bash output
Traceback (most recent call last):
  File "<string>", line 1, in <module>
    import chemicals,os;print(chemicals.__file__)
    ^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'chemicals'
checks:
0a061e67c7b843d9a544c0247cdccce7
0d53881888904eb7b263d141eed5512e
0e26c0d1cd2a4d3e931493e00c8252b4
1b0846e151ca49ea869d303e1858edd7
1be70249055743059f956160d13a001e
2125411f31dc4bc28190c5bc6bd44d1d
248420fe49ad4124bd13c839d06e22c4
52448c0db62c400e90cbd46e54881cba
667a9608d45d4803985b35cf1c68b1d8
{
  "decision": "reject",
  "binding": "2ddafd50e513148c684a2711054c9894cac903c7cd05bf3a01c77d0d4c0077ce",
  "reason": "Human review of attempt 004: verify the empirical reference and calculate Phi inside Lean.\n\nHuman instruction, verbatim:\n\"Tell him to check the ref and calculate Phi inside Lean. Is claude fish done?\"\n\n1. Check the dissolution-enthalpy reference rather than relying on recalled NBS/CRC values. Retrieve and inspect an authoritative source, retain its exact reference and relevant page/table/equation, and establish that the quantity, temperature, final concentration, initial solid state, sign convention and enthalpy reference match your model. Distinguish a 25 C standard/infinite-dilution value from dissolution at 20 C to a finite concentration. If a conversion or interpolation is needed, state and justify it and derive its mathematical consequences. Reassess the claimed hsol lower bound with supported uncertainty/applicability. Do not retain an unsupported bound merely because it yields the existing conclusion.\n\n2. Calculate Phi in Lean from the explicit material-property model. The bounds [-41,-27] currently enter as numerical premises derived only in Python. Instead formalize the relevant correlation or another justified thermodynamic model, expose empirically sourced coefficients/data and their applicability as reviewable premises, derive the solution-minus-original-water apparent molar heat capacity and any constant effective Phi you use, and prove the bounds needed by the timing theorem in Lean. Numerical evaluation or approximations must have proved enclosures sufficient for the conclusion. Python may suggest bounds; it must not be the sole evidence for them. Do not introduce a new assumption of the same desired Phi bound under another name.\n\n3. Preserve the physical meaning of the reduction. If using a secant average of temperature-dependent heat capacity, define and derive that average and justify its use for the timing comparison over the whole relevant addition-temperature interval. Agreement at two endpoints alone does not establish the intermediate mixing-energy inequalities. If the verified property model requires changing the regime or the qualitative recommendation, explain why and prove the result that actually follows, without tuning evidence to force an optimum.\n\nKeep the already accepted shared constant-power/Newtonian-loss approximation and retain the previous attempts and source/provenance records. A numerical heating duration or numerical time saving is not required. A physically supported qualitative recommendation with the property-to-conclusion proof chain is required. No other agent's model, material values, results or proof are supplied; no new physical measurements are claimed. You may request specific missing literature through the existing protocol. The next candidate still requires human review.\n",
  "time": "2026-11-17T23:46:18.240685+00:00",
Bash input
{
  "command": "cd /tmp; mkdir -p lab && cd lab; curl -sS -m 30 -o l.tsv https://raw.githubusercontent.com/CalebBell/chemicals/master/chemicals/Electrolytes/Laliberte2009.tsv; head -c 600 l.tsv; grep -i -E \"^7647-14-5|NaCl|Sodium chloride\" l.tsv | head; python3 -c \"import numpy,scipy,mpmath,sympy;print('ok')\""
}
Bash output · error
Exit code 1
/bin/bash: line 1: curl: command not found
head: cannot open 'l.tsv' for reading: No such file or directory
grep: l.tsv: No such file or directory
Traceback (most recent call last):
  File "<string>", line 1, in <module>
    import numpy,scipy,mpmath,sympy;print('ok')
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'numpy'
WebFetch input
{
  "url": "https://raw.githubusercontent.com/CalebBell/chemicals/master/chemicals/Electrolytes/Laliberte2009.tsv",
  "prompt": "Give the exact full row(s) for sodium chloride (CAS 7647-14-5), all columns with header names, verbatim."
}
WebSearch input
{
  "query": "Laliberté 2009 \"Model for Calculating the Heat Capacity of Aqueous Solutions, with Updated Density and Viscosity Data\" Cp_i = a1 exp(alpha) + a2 (1-w_w)^a3 equation"
}
WebSearch output
Web search results for query: "Laliberté 2009 "Model for Calculating the Heat Capacity of Aqueous Solutions, with Updated Density and Viscosity Data" Cp_i = a1 exp(alpha) + a2 (1-w_w)^a3 equation"

Links: [{"title":"A Model for Calculating the Heat Capacity of Aqueous Solutions, with Updated Density and Viscosity Data","url":"https://www.researchgate.net/publication/231541607_A_Model_for_Calculating_the_Heat_Capacity_of_Aqueous_Solutions_with_Updated_Density_and_Viscosity_Data"},{"title":"A Model for Calculating the Heat Capacity of Aqueous Solutions, with Updated Density and Viscosity Data","url":"https://hero.epa.gov/reference/1712350/"},{"title":"Electrochemistry (thermo.electrochem) - Read the Docs","url":"https://thermo.readthedocs.io/thermo.electrochem.html"},{"title":"Model for Calculating the Density of Aqueous Electrolyte Solutions","url":"https://pubs.acs.org/doi/abs/10.1021/je0498659"},{"title":"A Model for Calculating the Heat Capacity of Aqueous Solutions, with Updated Density and Viscosity Data","url":"https://ouci.dntb.gov.ua/en/works/lDWxvvo4/"},{"title":"Marc Laliberté's research works","url":"https://www.researchgate.net/scientific-contributions/Marc-Laliberte-85921144"},{"title":"Species Table - Specific Heat (Cp) - SysCAD Documentation","url":"https://help.syscad.net/Species_Table_-_Specific_Heat_(Cp)"},{"title":"Potash Species Model - SysCAD Documentation","url":"https://help.syscad.net/Potash_Species_Model"},{"title":"Viscosity as a Smoking Gun for Complex Formation in Solution: Fe$^{2+}$ and Mg$^{2+}$ Chlorides as Examples","url":"https://arxiv.org/pdf/2603.05077"},{"title":"A Meshfree Generalized Finite Difference Method for Solution Mining Processes","url":"https://arxiv.org/pdf/2008.11068"}]

The search turned up the equation in the SysCAD documentation. It appears to differ from the form in your query.

**Laliberté (2009), *J. Chem. Eng. Data* 54(6), 1725–1760, DOI 10.1021/je8008123.** SysCAD's documentation gives the model as follows:

- **Mixture heat capacity:** Cp_m = w_w·Cp_w + Σ w_i·Cp_i, where w is mass fraction.
- **Solute heat capacity:** Cp_i = a1·e^α + a5·(1−w_w)^a6.
- **Exponent:** α = a2·T + a3·e^(0.01T) + a4·(1−w_w).
- **Units:** T is in °C, and Cp is in kJ/kg·K.

SysCAD writes the second term with a5 and a6, and the exponent α has three terms (a2, a3, a4). Your query wrote it as a2·(1−w_w)^a3, which is a simplified form. The SysCAD version is probably the correct one, but I only saw it on that page and not in the paper itself.

**Other results:**
- The `thermo` Python library has a `Laliberte_heat_capacity(T, ws, CASRNs)` function, which returns J/kg/K. Its documentation is at thermo.readthedocs.io (thermo.electrochem).
- The paper's abstract says parameters for 79 solutes were established, with about 6600 points included.
- A 2008 arXiv paper on solution mining (2008.11068) uses the same model for NaCl.

I did not find the paper's own equation text or its parameter table. The ACS page or the `thermo` source code would confirm the exact form.


REMINDER: You MUST include the sources above in your response to the user using markdown hyperlinks.
WebFetch output
I can't quote the full row verbatim because it's far longer than the 125-character limit on quotes from source documents. Here are the values, labeled by column header, taken from the page:

- **Name:** Sodium Chloride
- **CASRN:** 7647-14-5
- **Formula:** NaCl
- **MW:** 58.45
- **c0:** -0.00324112223655149
- **c1:** 0.0636354335906616
- **c2:** 1.01371399467365
- **c3:** 0.0145951015210159
- **c4:** 3317.34854426537
- **Min T:** 0
- **Max T:** 140
- **Max w:** 0.26589930421877
- **No of points in corr:** 869
- **v1:** 16.221788633396
- **v2:** 1.32293086770011
- **v3:** 1.48485985010431
- **v4:** 0.00746912559657377
- **v5:** 30.7802007540575
- **v6:** 2.05826852322558
- **Min T:** 5
- **Max T:** 154
- **Max w:** 0.264456748962402
- **No of points in corr:** 552
- **a1:** -0.0693559668993322
- **a2:** -0.0782134167486952
- **a3:** 3.84798479408635
- **a4:** -11.2762109247072
- **a5:** 8.73187698542672
- **a6:** 1.81245930472755
- **Min T:** 1.5
- **Max T:** 120
- **Max w:** 0.261058295490885
- **No of points in corr:** 356

The "Min T", "Max T", "Max w", and "No of points in corr" headers repeat in three groups (for the c, v, and a parameter sets). I've kept each group's values in order.
Bash input
{
  "command": "cat > /tmp/lab/lal.py <<'E'\nimport math\na1,a2,a3,a4,a5,a6=-0.0693559668993322,-0.0782134167486952,3.84798479408635,-11.2762109247072,8.73187698542672,1.81245930472755\nM=58.44\ndef Cpi(t,w): # kJ/kg/K per kg of solute\n    ww=1-w\n    return a1*math.exp(a2*t+a3*math.exp(0.01*t)+a4*ww)+a5*w**a6 if False else a1*math.exp(a2*t+a3*math.exp(0.01*t)+a4*ww)+a5*(1-ww)**a6\ndef Phi(t,w): return Cpi(t,w)*M  # J/K/mol\nfor m in [0.2775,0.5551,0.7401]:\n    w=m*M/(1000+m*M)\n    print(m,w,[ (t,round(Phi(t,w),1)) for t in (15,20,25,30)], 'Parker cal->J',[round(x*4.184,1) for x in (-17.5,-16.3)] )\nE\npython3 /tmp/lab/lal.py"
}
Bash output
0.2775 0.015958302610731508 [(15, 0.3), (20, 0.3), (25, 0.3), (30, 0.3)] Parker cal->J [-73.2, -68.2]
0.5551 0.031420753377907534 [(15, 1.0), (20, 1.0), (25, 1.0), (30, 1.0)] Parker cal->J [-73.2, -68.2]
0.7401 0.04145831213438512 [(15, 1.6), (20, 1.6), (25, 1.6), (30, 1.6)] Parker cal->J [-73.2, -68.2]
Bash input
{
  "command": "cd /tmp/lab; sed -i 's/a4\\*ww)+a5\\*w\\*\\*a6 if False else a1\\*math.exp(a2\\*t+a3\\*math.exp(0.01\\*t)+a4\\*ww)+a5\\*(1-ww)\\*\\*a6/a4*w)+a5*w**a6 if False else a1*math.exp(a2*t+a3*math.exp(0.01*t)+a4*w)+a5*w**a6/' lal.py; grep -n return lal.py | head -3; python3 lal.py"
}
Bash output
6:    return a1*math.exp(a2*t+a3*math.exp(0.01*t)+a4*w)+a5*w**a6 if False else a1*math.exp(a2*t+a3*math.exp(0.01*t)+a4*w)+a5*w**a6
7:def Phi(t,w): return Cpi(t,w)*M  # J/K/mol
0.2775 0.015958302610731508 [(15, -91.3), (20, -77.6), (25, -66.7), (30, -58.1)] Parker cal->J [-73.2, -68.2]
0.5551 0.031420753377907534 [(15, -76.0), (20, -64.5), (25, -55.3), (30, -48.1)] Parker cal->J [-73.2, -68.2]
0.7401 0.04145831213438512 [(15, -67.1), (20, -56.8), (25, -48.7), (30, -42.2)] Parker cal->J [-73.2, -68.2]
Shell cwd was reset to /work
Bash input
{
  "command": "cd /tmp/lab; cat >> lal.py <<'E'\nprint('---')\ndef integ(f,a,b,n=2000):\n    h=(b-a)/n; return sum(f(a+(i+.5)*h) for i in range(n))*h\nfor m in [0.2775,0.342,0.5,0.7401,0.8561]:\n    w=m*M/(1000+m*M)\n    I=integ(lambda t:Phi(t,w),20,101)\n    print(m,round(w,4),'Phi20',round(Phi(20,w),1),'Phi60',round(Phi(60,w),1),'Phi100',round(Phi(100,w),1),'max',max(Phi(20+i*.81,w) for i in range(101)),'int20-101',round(I), 'int20-25',round(integ(lambda t:Phi(t,w),20,25)))\nE\npython3 lal.py | sed -n '/---/,$p'"
}
Bash output
---
0.2775 0.016 Phi20 -77.6 Phi60 -34.1 Phi100 -47.1 max -32.60752315360554 int20-101 -3448 int20-25 -360
0.342 0.0196 Phi20 -74.3 Phi60 -32.6 Phi100 -45.1 max -31.15883236471178 int20-101 -3298 int20-25 -345
0.5 0.0284 Phi20 -66.9 Phi60 -29.1 Phi100 -40.4 max -27.785587282440563 int20-101 -2952 int20-25 -310
0.7401 0.0415 Phi20 -56.8 Phi60 -24.2 Phi100 -33.9 max -23.077540643993355 int20-101 -2475 int20-25 -263
0.8561 0.0476 Phi20 -52.4 Phi60 -22.0 Phi100 -31.1 max -20.95771065036923 int20-101 -2262 int20-25 -242
Shell cwd was reset to /work
Bash input
{
  "command": "cd /tmp/lab; cat >> lal.py <<'E'\nprint('=== trapezoid at w_lo=1/51')\nwlo=1/51\nnodes=[20,30,40,50,60,70,80,90,100,101]\ntz=sum((nodes[i+1]-nodes[i])*(Phi(nodes[i],wlo)+Phi(nodes[i+1],wlo))/2 for i in range(len(nodes)-1))\nprint(tz, integ(lambda t:Phi(t,wlo),20,101))\nnodes=[20,40,60,80,101]\nprint(sum((nodes[i+1]-nodes[i])*(Phi(nodes[i],wlo)+Phi(nodes[i+1],wlo))/2 for i in range(len(nodes)-1)))\nprint('hsol25 lower', (923-41)*4.184, 'Phi25 vs', Phi(20,wlo),Phi(25,wlo))\n# Phi upper over [20,25] -> -Phi lower\nwhi=1/21\nprint('max Phi on 20..25 over w range', Phi(25,whi))\nE\npython3 lal.py | sed -n '/===/,$p'"
}
Bash output
=== trapezoid at w_lo=1/51
-3327.0802198613837 -3297.775109127632
-3419.3405498935176
hsol25 lower 3690.288 Phi25 vs -74.33367163867538 -63.91627835146851
max Phi on 20..25 over w range -44.85623621862143
Shell cwd was reset to /work
Bash input
{
  "command": "cd /work; ls checks/dcd7a1b461ab4ea1832bd51aed75b96c; head -c 1500 checks/dcd7a1b461ab4ea1832bd51aed75b96c/* | head -60; which lean lake; ls ~/.elan 2>/dev/null; python /harness/src/vcs_protocol.py tool --help 2>&1 | head -20"
}
Bash output
Checked.lean
lean.log
submitted-candidate.json
submitted-report.txt
submitted.lean
verdict.json
==> checks/dcd7a1b461ab4ea1832bd51aed75b96c/Checked.lean <==
import Mathlib.Analysis.SpecialFunctions.Log.Deriv
import Mathlib.Analysis.Calculus.MeanValue

/-! Model (see report.txt).  H = enthalpy (J) measured from "everything at room temperature Tr".
 Pure water, heat capacity Cw: temperature Tr + H/Cw.
 Salt solution, total heat capacity Cw+Cs>0 (Cs of EITHER sign; literature: Cs<0), dissolution enthalpy D (at Tr) of either sign:
 temperature Tr + (H-D)/(Cw+Cs).  Salt (at Tr) is mixed in instantly, so H is conserved.
 Heater power P constant, loss h*(T-Tr):  dH/dt = P - h*(T-Tr).
 Separating variables, the time to move from H1 to H2 is Fw/Fs(H2) - Fw/Fs(H1) where
 Fw' = 1/(P - h*(H/Cw)), Fs' = 1/(P - h*((H-D)/(Cw+Cs))) (explicit logarithms below).
 Tb = boiling point of the final solution (fixed by salt amount, not by timing). -/

/-- time-integral of dH/(P - loss) for pure water (antiderivative, h>0) -/
noncomputable def Fw (Cw P h H : ℝ) : ℝ := -(Cw/h) * Real.log (P - h*(H/Cw))
/-- same for the salt solution -/
noncomputable def Fs (Cw Cs D P h H : ℝ) : ℝ := -((Cw+Cs)/h) * Real.log (P - h*((H-D)/(Cw+Cs)))

/-- Total time to reach Tb from T0 when the salt is added once the water is at Ta:
 heating water alone from T0 to Ta, then solution from the post-mixing state up to Tb. -/
noncomputable def boilTime (Cw Cs D P h Tr T0 Tb Ta : ℝ) : ℝ :=
  (Fw Cw P h (Cw*(Ta-Tr)) - Fw Cw P h (Cw*(T0-Tr))) +
  (Fs Cw Cs D P h ((Cw+Cs)*(Tb-Tr)+D) - Fs Cw Cs D P h (Cw*(Ta-Tr)))

lemma G_deriv (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) 
==> checks/dcd7a1b461ab4ea1832bd51aed75b96c/lean.log <==
/work/checks/dcd7a1b461ab4ea1832bd51aed75b96c/Checked.lean:173:78: error: unsolved goals
r M mw n Phi ms mwater : ℝ
hr1 : 2e-2 ≤ r
hr2 : r ≤ 5e-2
hM : M = 58.44
hmw : mw = 0.998
hn : n = 1000 * mw * r / M
hP1 : -41 ≤ Phi
hP2 : Phi ≤ -27
hms : 0 < ms
hmwa : 0 < mwater
hn0 : 0 < n
hn1 : n ≤ 0.8540
⊢ mwater + ms = ms + mwater
'answer_theorem' depends on axioms: [propext, Classical.choice, Quot.sound]
'cs_theorem' depends on axioms: [propext, sorryAx, Classical.choice, Quot.sound]
VAS_GAP cs_theorem used=true : (r : ℝ) (M : ℝ) (mw : ℝ) (n : ℝ) (Phi : ℝ) (ms : ℝ) (mwater : ℝ) (hr1 : 2e-2 ≤
  r) (hr2 : r ≤
  5e-2) (hM : M =
  58.44) (hmw : mw =
  0.998) (hn : n =
  1000 * mw * r /
    M) (hP1 : -41 ≤
  Phi) (hP2 : Phi ≤
  -27) (hms : 0 <
  ms) (hmwa : 0 <
  mwater) (hn0 : 0 <
  n := Eq.mpr (id (congrArg (fun _a => 0 < _a) hn))
  (Eq.mpr (id (congrArg (fun _a => 0 < 1000 * mw * r / _a) hM))
    (Eq.mpr (id (congrArg (fun _a => 0 < 1000 * _a * r / 58.44) hmw))
      (div_pos
        (mul_pos
          (mul_pos
/opt/lean/bin/lean
/opt/lean/bin/lake
Usage: vcs_protocol.py tool [OPTIONS] COMMAND [ARGS]...

  Container-only feedback tools. Host always independently rechecks.

Options:
  --help  Show this message and exit.

Commands:
  check
  request
Bash input
{
  "command": "cd /work; ls /opt/lean /opt 2>/dev/null; find / -maxdepth 4 -name \"lakefile*\" -not -path \"*/proc/*\" 2>/dev/null | head; grep -n \"lean\" /harness/src/vcs_protocol.py | head -20"
}
Bash output
/opt:
lean

/opt/lean:
LICENSE
LICENSES
bin
include
lib
share
src
136:    from vcs_lean import strip_comments, validate
153:    from vcs_lean import ALLOWED_AXIOMS, LISTER, listed
154:    from vcs_extract import run_lean, main as extract
160:        snapshot = out / "submitted.lean"
169:        audited = out / "Checked.lean"
171:        status, log = run_lean(audited)
172:        (out / "lean.log").write_text(log)
173:        result["lean_exit"] = status
182:            raise ValueError("Lean, kernel recheck or axiom audit failed; see lean.log")
303:    toolchain_name = (ROOT / "lean-toolchain").read_text().strip().replace("/", "--").replace(":", "---")
304:    lean = Path(os.environ.get("ELAN_HOME", str(Path.home() / ".elan"))) / "toolchains" / toolchain_name
323:    for path in [lean / "bin/lean", lean / "bin/leanchecker", *required]:
331:    libraries = sorted(Path(library_root).resolve().glob(".lake/packages/*/.lake/build/lib/lean"))
338:    config = {"image": IMAGE, "lean": str(lean.resolve()), "agent": agent,
342:              "lean_version": subprocess.check_output([str(lean / "bin/lean"), "--version"], text=True).strip()}
352:    provenance = {key: config[key] for key in ["image", "lean_version"]}
395:           "--env", "VAS_LEAN=/opt/lean/bin/lean", "--env", "PATH=/opt/lean/bin:/usr/local/bin:/usr/bin:/bin", "--workdir", "/work", "--entrypoint", "/harness/entrypoint.sh",
399:    cmd += mount(work, "/work", False) + mount(config["lean"], "/opt/lean") + mount(config["click"], "/deps/click")
400:    for source in ["vcs_lean.py", "vcs_extract.py", "vcs_protocol.py"]:
402:    cmd += mount(ROOT / "lean-toolchain", "/harness/lean-toolchain")
Bash input
{
  "command": "cd /work; grep -n \"def run_lean\" -A25 /harness/src/vcs_extract.py | head -50; env | grep -i -E \"lean|lake\" "
}
Bash output
100:def run_lean(path):
101-    """Lean, then leanchecker on the compiled module (OT-1861): a nonzero status if either fails."""
102-    libs = sorted((ROOT / ".lake/packages").glob("*/.lake/build/lib/lean"))
103-    env = dict(os.environ, LEAN_PATH=":".join(map(str, libs)), LEAN_NUM_THREADS="1")
104-    with tempfile.TemporaryDirectory(prefix="vcs-olean-") as build:
105-        r = subprocess.run([LEAN, "-j1", f"--root={Path(path).resolve().parent}", "-o", str(Path(build) / "VasExtract.olean"), str(path)], cwd=ROOT,
106-                           env=env, capture_output=True, text=True, timeout=900)
107-        if r.returncode != 0:
108-            return r.returncode, r.stdout + r.stderr
109-        c = subprocess.run([LEANCHECKER, "VasExtract"], cwd=ROOT, capture_output=True, text=True, timeout=900,
110-                           env=dict(env, LEAN_PATH=env["LEAN_PATH"] + ":" + build,
111-                                    PATH=str(Path(LEAN).parent) + ":" + env.get("PATH", "")))
112-        note = f"\nleanchecker exit {c.returncode}\n{(c.stdout + c.stderr)[-1500:]}\n"
113-        return (0 if c.returncode == 0 else 10), r.stdout + r.stderr + note
114-
115-
116-def expression_identities(text, marker):
117-    """Read complete expression records; duplicate/missing records fail closed."""
118-    records = re.findall(r"^" + re.escape(marker) + r" (\S+) : (.*?) VAS_END", text, re.M | re.S)
119-    result = {}
120-    for name, value in records:
121-        if name in result:
122-            raise ValueError(f"duplicate expression identity for {name}")
123-        result[name] = " ".join(value.split())
124-    return result
125-
VAS_LEAN=/opt/lean/bin/lean
PATH=/opt/lean/bin:/usr/local/bin:/usr/bin:/bin
Bash input
{
  "command": "cd /work; grep -n \"^ROOT\\|ROOT =\" /harness/src/vcs_extract.py | head -3; ls /harness; ls /harness/.lake/packages 2>/dev/null | head; ls /deps 2>/dev/null"
}
Bash output
13:ROOT = Path(__file__).resolve().parents[1]
entrypoint.sh
lean-toolchain
src
LeanSearchClient
Qq
aesop
batteries
importGraph
mathlib
plausible
proofwidgets
click
Bash input
{
  "command": "mkdir -p /tmp/scr && cat > /tmp/scr/lc.sh <<'E'\n#!/bin/bash\nLP=$(ls -d /harness/.lake/packages/*/.lake/build/lib/lean | tr '\\n' ':')\ncd /harness && LEAN_PATH=$LP LEAN_NUM_THREADS=2 timeout 900 lean -j2 \"$1\" 2>&1 | head -${2:-80}\nE\nchmod +x /tmp/scr/lc.sh\nmkdir -p /tmp/lw; cat > /tmp/lw/t.lean <<'E'\nimport Mathlib.Analysis.SpecialFunctions.Exponential\nimport Mathlib.Analysis.Convex.SpecificFunctions.Basic\nimport Mathlib.Analysis.Calculus.MeanValue\nopen Real\n#check @ConvexOn.comp\n#check @Real.exp_bound\n#check @Real.quadratic_le_exp_of_nonneg\n#check @MonotoneOn.intervalIntegrable\n#check @intervalIntegral.integral_mono_on\n#check @strictMonoOn_of_deriv_pos\n#check @ConvexOn.comp_linearMap\n#check @Real.rpow_le_rpow_of_exponent_ge\nE\ntime /tmp/scr/lc.sh /tmp/lw/t.lean"
}
Bash output · error
Exit code 126
/bin/bash: line 41: /tmp/scr/lc.sh: Permission denied

real	0m0.000s
user	0m0.000s
sys	0m0.001s
Bash input
{
  "command": "cd /tmp; time bash /tmp/scr/lc.sh /tmp/lw/t.lean"
}
Bash output
/tmp/lw/t.lean:1:0: error: object file '/harness/.lake/packages/mathlib/.lake/build/lib/lean/Mathlib/Analysis/Convex/SpecificFunctions/Basic.olean' of module Mathlib.Analysis.Convex.SpecificFunctions.Basic does not exist

real	1m0.423s
user	0m0.349s
sys	0m1.229s
Shell cwd was reset to /work
Bash input
{
  "command": "cd /tmp; ls /harness/.lake/packages/mathlib/.lake/build/lib/lean/Mathlib/Analysis/Convex/ | head -50; ls /harness/.lake/packages/mathlib/.lake/build/lib/lean/Mathlib/Analysis/SpecialFunctions | head -30"
}
Bash output
Basic.ilean
Basic.ilean.hash
Basic.ir
Basic.ir.hash
Basic.ir.sig
Basic.ir.sig.hash
Basic.olean
Basic.olean.hash
Basic.olean.private
Basic.olean.private.hash
Basic.olean.server
Basic.olean.server.hash
Basic.trace
Combination.ilean
Combination.ilean.hash
Combination.ir
Combination.ir.hash
Combination.ir.sig
Combination.ir.sig.hash
Combination.olean
Combination.olean.hash
Combination.olean.private
Combination.olean.private.hash
Combination.olean.server
Combination.olean.server.hash
Combination.trace
EGauge.ilean
EGauge.ilean.hash
EGauge.ir
EGauge.ir.hash
EGauge.ir.sig
EGauge.ir.sig.hash
EGauge.olean
EGauge.olean.hash
EGauge.olean.private
EGauge.olean.private.hash
EGauge.olean.server
EGauge.olean.server.hash
EGauge.trace
Function.ilean
Function.ilean.hash
Function.ir
Function.ir.hash
Function.ir.sig
Function.ir.sig.hash
Function.olean
Function.olean.hash
Function.olean.private
Function.olean.private.hash
Function.olean.server
Complex
Exp.ilean
Exp.ilean.hash
Exp.ir
Exp.ir.hash
Exp.ir.sig
Exp.ir.sig.hash
Exp.olean
Exp.olean.hash
Exp.olean.private
Exp.olean.private.hash
Exp.olean.server
Exp.olean.server.hash
Exp.trace
ExpDeriv.ilean
ExpDeriv.ilean.hash
ExpDeriv.ir
ExpDeriv.ir.hash
ExpDeriv.ir.sig
ExpDeriv.ir.sig.hash
ExpDeriv.olean
ExpDeriv.olean.hash
ExpDeriv.olean.private
ExpDeriv.olean.private.hash
ExpDeriv.olean.server
ExpDeriv.olean.server.hash
ExpDeriv.trace
Exponential.ilean
Exponential.ilean.hash
Exponential.ir
Shell cwd was reset to /work
Bash input
{
  "command": "cd /harness/.lake/packages/mathlib; grep -rln \"theorem strictConvexOn_exp\\|convexOn_exp\" Mathlib | head; ls .lake/build/lib/lean/Mathlib.olean"
}
Bash output · error
Exit code 2
grep: Mathlib: No such file or directory
ls: cannot access '.lake/build/lib/lean/Mathlib.olean': No such file or directory
Bash input
{
  "command": "cd /harness/.lake/packages/mathlib/.lake/build/lib/lean/Mathlib; ls | head; ls Analysis/Convex | grep -i -E \"special|deriv\"; ls Analysis/SpecialFunctions | grep -v -E \"hash|ir|ilean|trace|private|server\" | head -30; ls /harness/.lake/packages/mathlib/.lake/build/lib/lean/ "
}
Bash output
Algebra
Analysis
Basic
CategoryTheory
Combinatorics
Control
Data
Dynamics
FieldTheory
Geometry
Complex
Exp.olean
ExpDeriv.olean
Exponential.olean
Log
Pow
Trigonometric
Cache
Mathlib
Shell cwd was reset to /work
Bash input
{
  "command": "cd /harness/.lake/packages/mathlib/.lake/build/lib/lean/Mathlib; find . -name \"*.olean\" | wc -l; ls Analysis/Convex/*.olean Analysis/Calculus/*.olean Analysis/SpecialFunctions/*/*.olean Analysis/SpecialFunctions/*.olean 2>/dev/null | head -80; ls MeasureTheory/Integral/ 2>/dev/null | grep olean | head -30"
}
Bash output
2945
Analysis/Calculus/FormalMultilinearSeries.olean
Analysis/Calculus/LogDeriv.olean
Analysis/Calculus/MeanValue.olean
Analysis/Convex/Basic.olean
Analysis/Convex/Combination.olean
Analysis/Convex/EGauge.olean
Analysis/Convex/Function.olean
Analysis/Convex/Hull.olean
Analysis/Convex/Jensen.olean
Analysis/Convex/PathConnected.olean
Analysis/Convex/Segment.olean
Analysis/Convex/Star.olean
Analysis/Convex/StdSimplex.olean
Analysis/Convex/Strict.olean
Analysis/Convex/StrictConvexSpace.olean
Analysis/Convex/Topology.olean
Analysis/Convex/Uniform.olean
Analysis/SpecialFunctions/Complex/Arg.olean
Analysis/SpecialFunctions/Complex/Log.olean
Analysis/SpecialFunctions/Exp.olean
Analysis/SpecialFunctions/ExpDeriv.olean
Analysis/SpecialFunctions/Exponential.olean
Analysis/SpecialFunctions/Log/Basic.olean
Analysis/SpecialFunctions/Log/Deriv.olean
Analysis/SpecialFunctions/Pow/Asymptotics.olean
Analysis/SpecialFunctions/Pow/Complex.olean
Analysis/SpecialFunctions/Pow/NNReal.olean
Analysis/SpecialFunctions/Pow/Real.olean
Analysis/SpecialFunctions/Trigonometric/Angle.olean
Analysis/SpecialFunctions/Trigonometric/Basic.olean
Analysis/SpecialFunctions/Trigonometric/Inverse.olean
Shell cwd was reset to /work
Bash input
{
  "command": "cd /harness/.lake/packages/mathlib/.lake/build/lib/lean/Mathlib; find . -name \"*.olean\" | grep -i -E \"Interval|SpecificFunc|ConvexOn|Mono|Taylor\" | head -30; find . -name \"*.olean\" | grep -E \"^./Analysis\" | wc -l"
}
Bash output
./Data/DFinsupp/Submonoid.olean
./Data/Int/Interval.olean
./Data/ENat/Monoid.olean
./Data/Set/Monotone.olean
./RingTheory/UniqueFactorizationDomain/GCDMonoid.olean
./RingTheory/TensorProduct/MonoidAlgebra.olean
./Order/Interval/Multiset.olean
./Order/Interval/Set/OrdConnectedComponent.olean
./Order/Interval/Set/LinearOrder.olean
./Order/Interval/Set/UnorderedInterval.olean
./Order/Interval/Set/Pi.olean
./Order/Interval/Set/OrderEmbedding.olean
./Order/Interval/Set/WithBotTop.olean
./Order/Interval/Set/Disjoint.olean
./Order/Interval/Set/SuccPred.olean
./Order/Interval/Set/ProjIcc.olean
./Order/Interval/Set/Basic.olean
./Order/Interval/Set/OrdConnected.olean
./Order/Interval/Set/Infinite.olean
./Order/Interval/Set/OrderIso.olean
./Order/Interval/Set/Image.olean
./Order/Interval/Set/Defs.olean
./Order/Interval/Set/Fin.olean
./Order/Interval/Finset/Nat.olean
./Order/Interval/Finset/SuccPred.olean
./Order/Interval/Finset/Basic.olean
./Order/Interval/Finset/Defs.olean
./Order/Interval/Finset/Fin.olean
./Order/BoundedOrder/Monotone.olean
./Order/Filter/Interval.olean
171
Shell cwd was reset to /work
Bash input
{
  "command": "cat > /tmp/lw/t.lean <<'E'\nimport Mathlib.Analysis.SpecialFunctions.Log.Deriv\nimport Mathlib.Analysis.SpecialFunctions.Pow.Real\nimport Mathlib.Analysis.SpecialFunctions.ExpDeriv\nimport Mathlib.Analysis.Calculus.MeanValue\nopen Real\n#check @Real.exp_bound\n#check @Real.quadratic_le_exp_of_nonneg\n#check @strictMonoOn_of_deriv_pos\n#check @Real.rpow_le_rpow_of_exponent_ge\n#check @Real.add_one_le_exp\n#check @Real.exp_nat_mul\n#check @intermediate_value_Icc\n#check @Real.sqrt_eq_rpow\nE\ncd /tmp; time bash /tmp/scr/lc.sh /tmp/lw/t.lean"
}
Bash output
@exp_bound : ∀ {x : ℝ},
  |x| ≤ 1 →
    ∀ {n : ℕ}, 0 < n → |rexp x - ∑ m ∈ Finset.range n, x ^ m / ↑m.factorial| ≤ |x| ^ n * (↑n.succ / (↑n.factorial * ↑n))
@quadratic_le_exp_of_nonneg : ∀ {x : ℝ}, 0 ≤ x → 1 + x + x ^ 2 / 2 ≤ rexp x
@strictMonoOn_of_deriv_pos : ∀ {D : Set ℝ},
  Convex ℝ D → ∀ {f : ℝ → ℝ}, ContinuousOn f D → (∀ x ∈ interior D, 0 < deriv f x) → StrictMonoOn f D
@rpow_le_rpow_of_exponent_ge : ∀ {x y z : ℝ}, 0 < x → x ≤ 1 → z ≤ y → x ^ y ≤ x ^ z
add_one_le_exp : ∀ (x : ℝ), x + 1 ≤ rexp x
exp_nat_mul : ∀ (x : ℝ) (n : ℕ), rexp (↑n * x) = rexp x ^ n
@intermediate_value_Icc : ∀ {α : Type u_1} [inst : TopologicalSpace α] [inst_1 : ConditionallyCompleteLinearOrder α]
  [OrderTopology α] [DenselyOrdered α] {δ : Type u_2} [inst_4 : LinearOrder δ] [inst_5 : TopologicalSpace δ]
  [OrderClosedTopology δ] {a b : α},
  a ≤ b → ∀ {f : α → δ}, ContinuousOn f (Set.Icc a b) → Set.Icc (f a) (f b) ⊆ f '' Set.Icc a b
sqrt_eq_rpow : ∀ (x : ℝ), √x = x ^ (1 / 2)

real	1m6.831s
user	0m1.365s
sys	0m1.606s
Shell cwd was reset to /work
Bash input
{
  "command": "cat > /tmp/lab/nodes.py <<'E'\nimport math\nfrom fractions import Fraction as F\na1,a2,a3,a4,a5,a6=-0.0693559668993322,-0.0782134167486952,3.84798479408635,-11.2762109247072,8.73187698542672,1.81245930472755\nM=58.44; wlo=1/51\nnodes=[10,20,30,40,50,60,70,80,90,100,101]\nimport json\nout=[]\nfor T in nodes:\n    V=math.ceil(math.exp(0.01*T)*1e5+1)/1e5\n    beta=a2*T+a3*V+a4*wlo\n    betar=math.ceil(beta*1e4+1)/1e4\n    U=math.ceil(math.exp(betar)*1e3+1)/1e3\n    L=math.ceil(-M*a1*U*100+... if False else (-M*a1*U)*100)/100  # lower bound is M*a1*U = -(L)\n    true=M*(a1*math.exp(a2*T+a3*math.exp(.01*T)+a4*wlo)+a5*wlo**a6)\n    out.append((T,V,betar,U,L,true))\n    print(T,V,betar,U,L,round(true,3))\n# trapezoid with bounds on [20..101]\nn2=[o for o in out if o[0]>=20]\ns=sum((n2[i+1][0]-n2[i][0])*(-n2[i][4]-n2[i+1][4])/2 for i in range(len(n2)-1))\nprint('trap sum lower',s)\njson.dump(out,open('/tmp/lab/nodes.json','w'))\nE\npython3 /tmp/lab/nodes.py"
}
Bash output
10 1.10519 3.2497 25.784 104.51 -104.064
20 1.22142 2.9148 18.447 74.77 -74.334
30 1.34987 2.6269 13.832 56.07 -55.639
40 1.49184 2.3911 10.927 44.29 -43.863
50 1.64874 2.2127 9.142 37.06 -36.629
60 1.82213 2.0978 8.15 33.04 -32.609
70 2.01377 2.0531 7.794 31.6 -31.164
80 2.22556 2.0859 8.053 32.65 -32.218
90 2.45962 2.2044 9.066 36.75 -36.324
100 2.7183 2.4177 11.222 45.49 -45.056
101 2.74562 2.4446 11.527 46.73 -46.296
trap sum lower -3362.01
Bash input
{
  "command": "cat > /tmp/lw/a.lean <<'E'\nimport Mathlib.Analysis.SpecialFunctions.Log.Deriv\nimport Mathlib.Analysis.SpecialFunctions.Pow.Real\nimport Mathlib.Analysis.SpecialFunctions.ExpDeriv\nimport Mathlib.Analysis.Calculus.MeanValue\n\nlemma exp_ub (x : ℝ) (h0 : 0 ≤ x) (h4 : x ≤ 4) :\n    Real.exp x ≤ ((∑ m ∈ Finset.range 7, (x/4)^m / (m.factorial:ℝ)) + (x/4)^7 * (8/35280))^4 := by\n  have hy : |x/4| ≤ 1 := by rw [abs_le]; constructor <;> linarith\n  have hb := Real.exp_bound hy (n := 7) (by norm_num)\n  have habs : |x/4| = x/4 := abs_of_nonneg (by linarith)\n  rw [habs] at hb\n  have h1 : Real.exp (x/4) ≤ (∑ m ∈ Finset.range 7, (x/4)^m / (m.factorial:ℝ)) + (x/4)^7 * (8/35280) := by\n    have := (abs_le.mp hb).2\n    have e : ((Nat.succ 7 : ℕ) : ℝ) / ((Nat.factorial 7 : ℝ) * (7:ℕ)) = 8/35280 := by\n      norm_num [Nat.factorial]\n    rw [e] at this\n    linarith\n  have e2 : Real.exp x = Real.exp (x/4) ^ 4 := by\n    rw [← Real.exp_nat_mul]; congr 1; push_cast; ring\n  rw [e2]; exact pow_le_pow_left₀ (Real.exp_pos _).le h1 4\n\nexample : Real.exp 2.4446 ≤ 11.527 := by\n  refine (exp_ub 2.4446 (by norm_num) (by norm_num)).trans ?_\n  norm_num [Finset.sum_range_succ, Nat.factorial]\nE\ncd /tmp; time bash /tmp/scr/lc.sh /tmp/lw/a.lean"
}
Bash output
real	0m2.344s
user	0m1.424s
sys	0m0.816s
Shell cwd was reset to /work
Bash input
{
  "command": "cat > /tmp/lw/b_head.lean <<'E'\n/-! ### Laliberté (2009) NaCl heat-capacity correlation (empirical inputs, exposed) -/\nnoncomputable def Mnacl : ℝ := 58.44\nnoncomputable def a1 : ℝ := -0.0693559668993322\nnoncomputable def a2 : ℝ := -0.0782134167486952\nnoncomputable def a3 : ℝ := 3.84798479408635\nnoncomputable def a4 : ℝ := -11.2762109247072\nnoncomputable def a5 : ℝ := 8.73187698542672\nnoncomputable def a6 : ℝ := 1.81245930472755\n\n/-- apparent molar heat capacity of NaCl in aqueous solution, J/(K mol); w = salt mass fraction, T in C -/\nnoncomputable def PhiC (w T : ℝ) : ℝ :=\n  Mnacl * (a1 * Real.exp (a2*T + a3*Real.exp (0.01*T) + a4*w) + a5 * w ^ a6)\n\nlemma phi_mono_w (w T : ℝ) (h : 1/51 ≤ w) : PhiC (1/51) T ≤ PhiC w T := by\n  unfold PhiC\n  have h1 : Real.exp (a2*T + a3*Real.exp (0.01*T) + a4*w) ≤ Real.exp (a2*T + a3*Real.exp (0.01*T) + a4*(1/51)) := by\n    apply Real.exp_le_exp.mpr; unfold a4; linarith\n  have h2 : (1/51:ℝ) ^ a6 ≤ w ^ a6 := Real.rpow_le_rpow (by norm_num) h (by unfold a6; norm_num)\n  have ha1 : a1 < 0 := by unfold a1; norm_num\n  have ha5 : 0 < a5 := by unfold a5; norm_num\n  have hM : 0 < Mnacl := by unfold Mnacl; norm_num\n  apply mul_le_mul_of_nonneg_left _ hM.le\n  nlinarith\n\nlemma phi_nonpos (w T : ℝ) (h1 : 1/51 ≤ w) (h2 : w ≤ 1/21) (t1 : 10 ≤ T) (t2 : T ≤ 101) : PhiC w T ≤ 0 := by\n  unfold PhiC\n  have hw : 0 < w := by linarith\n  -- w^a6 ≤ w^(3/2) ≤ 1/84\n  have e1 : w ^ a6 ≤ w ^ ((3:ℝ)/2) :=\n    Real.rpow_le_rpow_of_exponent_ge hw (by linarith) (by unfold a6; norm_num)\n  have e2 : w ^ ((3:ℝ)/2) = w * Real.sqrt w := by\n    rw [Real.sqrt_eq_rpow, show ((3:ℝ)/2) = 1 + 1/2 by norm_num, Real.rpow_add hw, Real.rpow_one]\n  have e3 : Real.sqrt w ≤ 1/4 := by\n    rw [Real.sqrt_le_left (by norm_num)]; nlinarith\n  have e4 : w ^ a6 ≤ 1/84 := by\n    have := Real.sqrt_nonneg w\n    nlinarith\n  -- exp(0.01 T) ≥ 1 + .01T + (.01T)^2/2\n  have q := Real.quadratic_le_exp_of_nonneg (x := 0.01*T) (by linarith)\n  set E := Real.exp (0.01*T) with hE\n  have hα : 1.2 ≤ a2*T + a3*E + a4*w := by\n    unfold a2 a3 a4\n    nlinarith [mul_nonneg (by linarith : (0:ℝ) ≤ 101 - T) (by linarith : (0:ℝ) ≤ T - 10)]\n  have q2 := Real.quadratic_le_exp_of_nonneg (x := a2*T + a3*E + a4*w - 1.2) (by linarith)\n  have q3 : 1.2 ≤ a2*T + a3*E + a4*w := hα\n  have hex : 2 ≤ Real.exp (a2*T + a3*E + a4*w) := by\n    have := Real.add_one_le_exp (a2*T + a3*E + a4*w)\n    linarith\n  have ha1 : a1 < 0 := by unfold a1; norm_num\n  have ha5 : 0 < a5 := by unfold a5; norm_num\n  have hM : 0 < Mnacl := by unfold Mnacl; norm_num\n  have : a1 * Real.exp (a2*T + a3*E + a4*w) + a5 * w ^ a6 ≤ 0 := by\n    unfold a1 at *; unfold a5 at *\n    nlinarith\n  exact mul_nonpos_of_nonneg_of_nonpos hM.le this\nE\ncat /tmp/lw/a.lean | sed '/^example/,$d' > /tmp/lw/b.lean; cat /tmp/lw/b_head.lean >> /tmp/lw/b.lean; cd /tmp; bash /tmp/scr/lc.sh /tmp/lw/b.lean"
}
Bash output
Shell cwd was reset to /work
Bash input
{
  "command": "cat > /tmp/lw/c_head.lean <<'E'\n\n/-- Φ_C is concave in T: its value at T lies above the chord between any a < b. -/\nlemma phi_chord (w a b T : ℝ) (hab : a < b) (h1 : a ≤ T) (h2 : T ≤ b) :\n    (b-a) * PhiC w T ≥ (b-T) * PhiC w a + (T-a) * PhiC w b := by\n  -- tangent inequality for g s = exp(α s):  g s ≥ g T * (1 + d (s - T))\n  set α : ℝ → ℝ := fun s => a2*s + a3*Real.exp (0.01*s) + a4*w with hαdef\n  have ha3 : 0 < a3 := by unfold a3; norm_num\n  have tang : ∀ s, Real.exp (α s) ≥ Real.exp (α T) * (1 + (a2 + a3*0.01*Real.exp (0.01*T)) * (s - T)) := by\n    intro s\n    have e1 : Real.exp (0.01*s) ≥ Real.exp (0.01*T) * (1 + 0.01*(s-T)) := by\n      have : Real.exp (0.01*s) = Real.exp (0.01*T) * Real.exp (0.01*(s-T)) := by\n        rw [← Real.exp_add]; congr 1; ring\n      rw [this]\n      have := Real.add_one_le_exp (0.01*(s-T))\n      nlinarith [Real.exp_pos (0.01*T)]\n    have e2 : α s - α T ≥ (a2 + a3*0.01*Real.exp (0.01*T)) * (s - T) := by\n      simp only [hαdef]\n      nlinarith\n    have e3 : Real.exp (α s) = Real.exp (α T) * Real.exp (α s - α T) := by\n      rw [← Real.exp_add]; congr 1; ring\n    rw [e3]\n    have := Real.add_one_le_exp (α s - α T)\n    have hp := Real.exp_pos (α T)\n    nlinarith\n  have ta := tang a\n  have tb := tang b\n  have hg : (b-a) * Real.exp (α T) ≤ (b-T) * Real.exp (α a) + (T-a) * Real.exp (α b) := by\n    nlinarith [mul_nonneg (by linarith : (0:ℝ) ≤ b - T) (Real.exp_pos (α T)).le,\n               mul_nonneg (by linarith : (0:ℝ) ≤ T - a) (Real.exp_pos (α T)).le,\n               mul_le_mul_of_nonneg_left ta (by linarith : (0:ℝ) ≤ b - T),\n               mul_le_mul_of_nonneg_left tb (by linarith : (0:ℝ) ≤ T - a)]\n  have ha1 : a1 < 0 := by unfold a1; norm_num\n  have hM : 0 < Mnacl := by unfold Mnacl; norm_num\n  unfold PhiC\n  have : (b-a) * (a1 * Real.exp (α T)) ≥ (b-T) * (a1 * Real.exp (α a)) + (T-a) * (a1 * Real.exp (α b)) := by\n    nlinarith\n  simp only [hαdef] at this\n  nlinarith [mul_pos hM (by linarith : (0:ℝ) < b - a)]\n\n/-- trapezoid lower bound: if F' = f is above its chord on [a,b] then F b - F a ≥ (b-a)(f a + f b)/2 -/\nlemma trap (F f : ℝ → ℝ) (a b : ℝ) (hab : a < b)\n    (hF : ∀ T ∈ Set.Icc a b, HasDerivAt F (f T) T)\n    (hc : ∀ T ∈ Set.Icc a b, (b-a) * f T ≥ (b-T) * f a + (T-a) * f b) :\n    (b-a)*(f a + f b)/2 ≤ F b - F a := by\n  have hd : ∀ T ∈ Set.Icc a b, HasDerivAt\n      (fun T => F T - F a - (f a * (T-a) + (T-a)^2 * (f b - f a) / (2*(b-a)))) (f T - ((b-T) * f a + (T-a) * f b)/(b-a)) T := by\n    intro T hT\n    have h1 := (hF T hT).sub_const (F a)\n    have h2 : HasDerivAt (fun T : ℝ => f a * (T-a) + (T-a)^2 * (f b - f a) / (2*(b-a)))\n        (f a + (T-a) * (f b - f a) / (b-a)) T := by\n      have u : HasDerivAt (fun T : ℝ => T - a) 1 T := (hasDerivAt_id T).sub_const a\n      have v := (u.const_mul (f a)).add (((u.pow 2).mul_const (f b - f a)).div_const (2*(b-a)))\n      convert v using 1\n      have : (b-a) ≠ 0 := by linarith\n      field_simp\n      ring\n    convert h1.sub h2 using 1\n    have : (b-a) ≠ 0 := by linarith\n    field_simp\n    ring\n  have mono : MonotoneOn (fun T => F T - F a - (f a * (T-a) + (T-a)^2 * (f b - f a) / (2*(b-a)))) (Set.Icc a b) := by\n    apply monotoneOn_of_deriv_nonneg (convex_Icc a b)\n    · intro T hT; exact (hd T hT).continuousAt.continuousWithinAt\n    · intro T hT; exact (hd T (interior_subset hT)).differentiableAt.differentiableWithinAt\n    · intro T hT\n      have hT' := interior_subset hT\n      rw [(hd T hT').deriv]\n      have := hc T hT'\n      have hba : 0 < b - a := by linarith\n      rw [sub_nonneg, le_div_iff₀ hba]\n      linarith\n  have := mono ⟨le_rfl, hab.le⟩ ⟨hab.le, le_rfl⟩ hab.le\n  simp only at this\n  have hba : (b-a) ≠ 0 := by linarith\n  have e : (f a * (b-a) + (b-a)^2 * (f b - f a) / (2*(b-a))) = (b-a)*(f a + f b)/2 := by\n    field_simp; ring\n  simp at this\n  nlinarith [e]\nE\ncd /tmp/lw; cat b.lean c_head.lean > c.lean; cd /tmp; bash /tmp/scr/lc.sh /tmp/lw/c.lean"
}
Bash output
/tmp/lw/c.lean:79:31: warning: Variable name `hab` is not explicitly referenced.

Hint: The binding can be removed (if unused) or named `_` (if used implicitly). Alternatively, prefix the name with `_` to silence this warning:
  [apply] _hab

Note: This linter can be disabled with `set_option linter.unusedVariables false`
/tmp/lw/c.lean:146:22: error: Tactic `rewrite` failed: Did not find an occurrence of the pattern
  ?m.1269 ≤ ?m.1270 / (b - a)
in the target expression
  ((b - T) * f a + (T - a) * f b) / (b - a) ≤ f T

case hf'_nonneg
F f : ℝ → ℝ
a b : ℝ
hab : a < b
hF : ∀ T ∈ Set.Icc a b, HasDerivAt F (f T) T
hc : ∀ T ∈ Set.Icc a b, (b - a) * f T ≥ (b - T) * f a + (T - a) * f b
hd :
  ∀ T ∈ Set.Icc a b,
    HasDerivAt (fun T => F T - F a - (f a * (T - a) + (T - a) ^ 2 * (f b - f a) / (2 * (b - a))))
      (f T - ((b - T) * f a + (T - a) * f b) / (b - a)) T
T : ℝ
hT : T ∈ interior (Set.Icc a b)
hT' : T ∈ Set.Icc a b
this : (b - a) * f T ≥ (b - T) * f a + (T - a) * f b
hba : 0 < b - a
⊢ ((b - T) * f a + (T - a) * f b) / (b - a) ≤ f T
Shell cwd was reset to /work
assistant
Chord lemma passed; fixing the one rewrite.
Bash input
{
  "command": "cd /tmp/lw; sed -i 's/rw \\[sub_nonneg, le_div_iff₀ hba\\]/rw [sub_nonneg, div_le_iff₀ hba]/' c_head.lean; sed -i 's/(hab : a < b) (h1 : a ≤ T)/(hab : a < b) (h1 : a ≤ T)/' c_head.lean; cat b.lean c_head.lean > c.lean; cd /tmp; bash /tmp/scr/lc.sh /tmp/lw/c.lean"
}
Bash output
/tmp/lw/c.lean:79:31: warning: Variable name `hab` is not explicitly referenced.

Hint: The binding can be removed (if unused) or named `_` (if used implicitly). Alternatively, prefix the name with `_` to silence this warning:
  [apply] _hab

Note: This linter can be disabled with `set_option linter.unusedVariables false`
Shell cwd was reset to /work
Bash input
{
  "command": "cat > /tmp/lab/gen.py <<'E'\nimport json\nout=json.load(open('/tmp/lab/nodes.json'))\ns=\"\"\ndef fmt(x): \n    t=repr(x); return t\nfor (T,V,beta,U,L,true) in out:\n    s+=f\"\"\"\nlemma node_{T} (w : ℝ) (h : 1/51 ≤ w) : -({L}:ℝ) ≤ PhiC w {T} := by\n  refine le_trans ?_ (phi_mono_w w {T} h)\n  have e1 : Real.exp (0.01*{T}) ≤ {V} := by\n    refine (exp_ub _ (by norm_num) (by norm_num)).trans ?_\n    norm_num [Finset.sum_range_succ, Nat.factorial]\n  have hb : a2*{T} + a3*Real.exp (0.01*{T}) + a4*(1/51) ≤ {beta} := by\n    unfold a2 a3 a4; norm_num; linarith [e1]\n  have e2 : Real.exp (a2*{T} + a3*Real.exp (0.01*{T}) + a4*(1/51)) ≤ {U} := by\n    refine (Real.exp_le_exp.mpr hb).trans ?_\n    refine (exp_ub _ (by norm_num) (by norm_num)).trans ?_\n    norm_num [Finset.sum_range_succ, Nat.factorial]\n  have h5 : 0 ≤ a5 * (1/51:ℝ)^a6 := mul_nonneg (by unfold a5; norm_num) (Real.rpow_nonneg (by norm_num) _)\n  unfold PhiC\n  unfold a1 Mnacl\n  linarith [e2, h5]\n\"\"\"\nopen('/tmp/lw/nodes.lean','w').write(s)\nLs={T:L for (T,V,b,U,L,t) in out}\ntot=0\nnn=[o[0] for o in out if o[0]>=20]\nfor i in range(len(nn)-1):\n    tot+=(nn[i+1]-nn[i])*(Ls[nn[i]]+Ls[nn[i+1]])/2\nprint(tot)\nE\npython3 /tmp/lab/gen.py\ncd /tmp/lw; cat c.lean nodes.lean > d.lean; cd /tmp; time bash /tmp/scr/lc.sh /tmp/lw/d.lean 40"
}
Bash output
3362.0099999999998
/tmp/lw/d.lean:79:31: warning: Variable name `hab` is not explicitly referenced.

Hint: The binding can be removed (if unused) or named `_` (if used implicitly). Alternatively, prefix the name with `_` to silence this warning:
  [apply] _hab

Note: This linter can be disabled with `set_option linter.unusedVariables false`

real	0m8.599s
user	0m14.172s
sys	0m0.908s
Shell cwd was reset to /work
Bash input
{
  "command": "cat > /tmp/lw/e_head.lean <<'E'\n\nlemma piece (w : ℝ) (Ph : ℝ → ℝ) (a b La Lb : ℝ) (hab : a < b) (ha : 10 ≤ a) (hb : b ≤ 101)\n    (hPh : ∀ T ∈ Set.Icc (10:ℝ) 101, HasDerivAt Ph (PhiC w T) T)\n    (hA : -La ≤ PhiC w a) (hB : -Lb ≤ PhiC w b) :\n    -((b-a)*(La+Lb)/2) ≤ Ph b - Ph a := by\n  have := trap Ph (PhiC w) a b hab (fun T hT => hPh T ⟨by linarith [hT.1], by linarith [hT.2]⟩)\n    (fun T hT => phi_chord w a b T hab hT.1 hT.2)\n  nlinarith\n\n/-- Φ_C is negative and bounded below on the whole temperature range, for every composition in the regime. -/\nlemma phi_floor (w T : ℝ) (h1 : 1/51 ≤ w) (t1 : 10 ≤ T) (t2 : T ≤ 101) : -105 ≤ PhiC w T := by\n  have c := phi_chord w 10 101 T (by norm_num) t1 t2\n  have n1 := node_10 w h1\n  have n2 := node_101 w h1\n  nlinarith [mul_nonneg (by linarith : (0:ℝ) ≤ 101 - T) (by linarith : (0:ℝ) ≤ 105 - 104.51),\n             mul_nonneg (by linarith : (0:ℝ) ≤ T - 10) (by linarith : (0:ℝ) ≤ 46.73 - 0)]\n\n/-- Total change of apparent molar enthalpy of NaCl from 20 C to 101 C is at least -3363 J/mol. -/\nlemma ph_drop (w : ℝ) (Ph : ℝ → ℝ) (h1 : 1/51 ≤ w)\n    (hPh : ∀ T ∈ Set.Icc (10:ℝ) 101, HasDerivAt Ph (PhiC w T) T) :\n    -3363 ≤ Ph 101 - Ph 20 := by\nE\npython3 - <<'E'\nimport json\nout=json.load(open('/tmp/lab/nodes.json'))\nnn=[o[0] for o in out if o[0]>=20]\ns=\"\"\nfor i in range(len(nn)-1):\n    a,b=nn[i],nn[i+1]\n    s+=f\"  have p{i} := piece w Ph {a} {b} _ _ (by norm_num) (by norm_num) (by norm_num) hPh (node_{a} w h1) (node_{b} w h1)\\n\"\ns+=\"  norm_num at *\\n  linarith\\n\"\nopen('/tmp/lw/e_tail.lean','w').write(s)\nE\ncd /tmp/lw; cat d.lean e_head.lean e_tail.lean > e.lean; cd /tmp; time bash /tmp/scr/lc.sh /tmp/lw/e.lean 40"
}
Bash output
/tmp/lw/e.lean:79:31: warning: Variable name `hab` is not explicitly referenced.

Hint: The binding can be removed (if unused) or named `_` (if used implicitly). Alternatively, prefix the name with `_` to silence this warning:
  [apply] _hab

Note: This linter can be disabled with `set_option linter.unusedVariables false`

real	0m9.422s
user	0m15.450s
sys	0m1.031s
Shell cwd was reset to /work
Bash input
{
  "command": "cat > /tmp/lw/f_head.lean <<'E'\n\n/-! ## Heating-time model (accepted Newtonian lumped model, state variable = total enthalpy H) -/\n\n/-- time needed to reach the boiling point when the salt is added while the pure water is at Ta:\n    pure-water branch up to total enthalpy Hw Ta, then solution branch from the same enthalpy up to Hs Tb.\n    τw, τs = elapsed time as functions of total enthalpy along the two branches. -/\ndef boilTime (τw τs Hw Hs : ℝ → ℝ) (T0 Tb Ta : ℝ) : ℝ :=\n  (τw (Hw Ta) - τw (Hw T0)) + (τs (Hs Tb) - τs (Hw Ta))\n\ntheorem timing_core (P h Tr Tl Tb : ℝ) (Hw Hs Tw Ts τw τs : ℝ → ℝ)\n    (hh : 0 ≤ h) (hP : h*(Tb-Tr) < P) (hl : Tl ≤ Tr) (hr : Tr ≤ Tb)\n    (hHw : StrictMonoOn Hw (Set.Icc Tl Tb)) (hHs : StrictMonoOn Hs (Set.Icc Tl Tb))\n    (hTw : ∀ H ∈ Set.Icc (Hw Tr) (Hw Tb), Tw H ∈ Set.Icc Tl Tb ∧ Hw (Tw H) = H)\n    (hTs : ∀ H ∈ Set.Icc (Hs Tl) (Hs Tb), Ts H ∈ Set.Icc Tl Tb ∧ Hs (Ts H) = H)\n    (hτw : ∀ H ∈ Set.Icc (Hw Tr) (Hw Tb), HasDerivAt τw (1/(P - h*(Tw H - Tr))) H)\n    (hτs : ∀ H ∈ Set.Icc (Hs Tl) (Hs Tb), HasDerivAt τs (1/(P - h*(Ts H - Tr))) H)\n    (hcool : ∀ x ∈ Set.Icc Tr Tb, Hw x ≤ Hs x) (hlow : Hs Tl ≤ Hw Tr) :\n    ∀ Ta ∈ Set.Icc Tr Tb, boilTime τw τs Hw Hs Tr Tb Tr ≤ boilTime τw τs Hw Hs Tr Tb Ta := by\n  intro Ta hTa\n  have hTrI : Tr ∈ Set.Icc Tl Tb := ⟨hl, hr⟩\n  have hTaI : Ta ∈ Set.Icc Tl Tb := ⟨hl.trans hTa.1, hTa.2⟩\n  have hTbI : Tb ∈ Set.Icc Tl Tb := ⟨hl.trans hr, le_rfl⟩\n  have hTlI : Tl ∈ Set.Icc Tl Tb := ⟨le_rfl, hl.trans hr⟩\n  have w1 : Hw Tr ≤ Hw Ta := hHw.monotoneOn hTrI hTaI hTa.1\n  have w2 : Hw Ta ≤ Hw Tb := hHw.monotoneOn hTaI hTbI hTa.2\n  have s1 : Hw Tb ≤ Hs Tb := hcool Tb ⟨hr, le_rfl⟩\n  have hpos : ∀ x, x ≤ Tb → 0 < P - h*(x - Tr) := by\n    intro x hx\n    have : h*(x-Tr) ≤ h*(Tb-Tr) := mul_le_mul_of_nonneg_left (by linarith) hh\n    linarith\n  have key : ∀ H ∈ Set.Icc (Hw Tr) (Hw Ta), 1/(P - h*(Ts H - Tr)) ≤ 1/(P - h*(Tw H - Tr)) := by\n    intro H hH\n    obtain ⟨hx, hxH⟩ := hTw H ⟨hH.1, hH.2.trans w2⟩\n    obtain ⟨hy, hyH⟩ := hTs H ⟨hlow.trans hH.1, hH.2.trans (w2.trans s1)⟩\n    have hxr : Tr ≤ Tw H := by\n      by_contra hc\n      push_neg at hc\n      have := hHw hx hTrI hc\n      rw [hxH] at this\n      linarith [hH.1]\n    have hxa : Tw H ∈ Set.Icc Tr Tb := ⟨hxr, hx.2⟩\n    have hc := hcool _ hxa\n    rw [hxH] at hc\n    have hyx : Ts H ≤ Tw H := by\n      by_contra hcon\n      push_neg at hcon\n      have := hHs hx hy hcon\n      rw [hyH] at this\n      linarith\n    apply one_div_le_one_div_of_le (hpos _ hx.2)\n    have := mul_le_mul_of_nonneg_left (by linarith : Ts H - Tr ≤ Tw H - Tr) hh\n    linarith\n  let G : ℝ → ℝ := fun H => τw H - τs H\n  have hG : ∀ H ∈ Set.Icc (Hw Tr) (Hw Ta),\n      HasDerivAt G (1/(P - h*(Tw H - Tr)) - 1/(P - h*(Ts H - Tr))) H := by\n    intro H hH\n    exact (hτw H ⟨hH.1, hH.2.trans w2⟩).sub (hτs H ⟨hlow.trans hH.1, hH.2.trans (w2.trans s1)⟩)\n  have mono : MonotoneOn G (Set.Icc (Hw Tr) (Hw Ta)) := by\n    apply monotoneOn_of_deriv_nonneg (convex_Icc _ _)\n    · intro H hH; exact (hG H hH).continuousAt.continuousWithinAt\n    · intro H hH; exact (hG H (interior_subset hH)).differentiableAt.differentiableWithinAt\n    · intro H hH\n      have hH' := interior_subset hH\n      rw [(hG H hH').deriv]\n      exact sub_nonneg.mpr (key H hH')\n  have := mono ⟨le_rfl, w1⟩ ⟨w1, le_rfl⟩ w1\n  simp only [G, boilTime] at this ⊢\n  linarith\n\n/-- sanity: the inverse temperature functions exist (intermediate value theorem). -/\nlemma inverse_exists (H : ℝ → ℝ) (Tl Tb : ℝ) (hle : Tl ≤ Tb) (hc : ContinuousOn H (Set.Icc Tl Tb)) :\n    ∀ y ∈ Set.Icc (H Tl) (H Tb), ∃ T ∈ Set.Icc Tl Tb, H T = y := by\n  intro y hy\n  obtain ⟨T, hT, e⟩ := intermediate_value_Icc hle hc hy\n  exact ⟨T, hT, e⟩\nE\ncd /tmp/lw; cat e.lean f_head.lean > f.lean; cd /tmp; time bash /tmp/scr/lc.sh /tmp/lw/f.lean 60"
}
Bash output
/tmp/lw/f.lean:79:31: warning: Variable name `hab` is not explicitly referenced.

Hint: The binding can be removed (if unused) or named `_` (if used implicitly). Alternatively, prefix the name with `_` to silence this warning:
  [apply] _hab

Note: This linter can be disabled with `set_option linter.unusedVariables false`
/tmp/lw/f.lean:399:6: warning: `push_neg` has been deprecated. Prefer using `push Not` instead.
If you'd rather continue using `push_neg` in your project, you can implement it as follows:
```
open Lean.Parser.Tactic in
macro "push_neg" cfg:optConfig loc:(location)? : tactic =>
  `(tactic| push $cfg:optConfig Not $[$loc]?)
```
/tmp/lw/f.lean:408:6: warning: `push_neg` has been deprecated. Prefer using `push Not` instead.
If you'd rather continue using `push_neg` in your project, you can implement it as follows:
```
open Lean.Parser.Tactic in
macro "push_neg" cfg:optConfig loc:(location)? : tactic =>
  `(tactic| push $cfg:optConfig Not $[$loc]?)
```

real	0m9.480s
user	0m15.445s
sys	0m1.100s
Shell cwd was reset to /work
Bash input
{
  "command": "cat > /tmp/lw/g_head.lean <<'E'\n\n/-! ## Thermodynamics of dissolving NaCl(cr) at 20 C in the pot -/\n\n/-- thermochemical calorie in J (Parker 1965 reports cal) -/\nnoncomputable def cal : ℝ := 4.184\n\n/-- moles of NaCl in salt/water mass ratio r, for the 0.998 kg of water in 1 L at 20 C -/\nnoncomputable def nSalt (r : ℝ) : ℝ := 1000*0.998*r/Mnacl\n\n/-- Enthalpy (J) of pot+solution at temperature T, measured from pot+pure water plus solid NaCl at 20 C.\n    By definition of the apparent molar enthalpy Ph of NaCl in the final solution:\n    solution = (same water at T) + n * Ph(T), and the salt started as crystal with enthalpy Hcr 20. -/\ndef solEnthalpy (n : ℝ) (Hw Ph Hcr : ℝ → ℝ) (T : ℝ) : ℝ := Hw T + n * (Ph T - Hcr 20)\n\nstructure Premises (r : ℝ) (Cw Hw Ph Hcr Cpcr : ℝ → ℝ) (hinf phiL : ℝ) : Prop where\n  r_lo : 1/50 ≤ r\n  r_hi : r ≤ 1/20\n  hw_deriv : ∀ T ∈ Set.Icc (10:ℝ) 101, HasDerivAt Hw (Cw T) T\n  hw_cap : ∀ T ∈ Set.Icc (10:ℝ) 101, 4100 ≤ Cw T\n  ph_deriv : ∀ T ∈ Set.Icc (10:ℝ) 101, HasDerivAt Ph (PhiC (r/(1+r)) T) T\n  cr_deriv : ∀ T ∈ Set.Icc (20:ℝ) 25, HasDerivAt Hcr (Cpcr T) T\n  cr_cap : ∀ T ∈ Set.Icc (20:ℝ) 25, 0 ≤ Cpcr T ∧ Cpcr T ≤ 100\n  hinf_b : 923*cal ≤ hinf ∧ hinf ≤ 933*cal\n  phiL_b : -41*cal ≤ phiL ∧ phiL ≤ 100*cal\n  diss25 : Ph 25 - Hcr 25 = hinf + phiL\n\nlemma w_range (r : ℝ) (h1 : 1/50 ≤ r) (h2 : r ≤ 1/20) : 1/51 ≤ r/(1+r) ∧ r/(1+r) ≤ 1/21 := by\n  constructor\n  · rw [le_div_iff₀ (by linarith)]; linarith\n  · rw [div_le_iff₀ (by linarith)]; linarith\n\nlemma n_range (r : ℝ) (h1 : 1/50 ≤ r) (h2 : r ≤ 1/20) : 0.34 ≤ nSalt r ∧ nSalt r ≤ 0.86 := by\n  unfold nSalt Mnacl\n  constructor\n  · rw [le_div_iff₀ (by norm_num)]; linarith\n  · rw [div_le_iff₀ (by norm_num)]; linarith\n\ntheorem thermo_facts (r Tb : ℝ) (Cw Hw Ph Hcr Cpcr : ℝ → ℝ) (hinf phiL : ℝ)\n    (hp : Premises r Cw Hw Ph Hcr Cpcr hinf phiL) (hT1 : 20 ≤ Tb) (hT2 : Tb ≤ 101) :\n    StrictMonoOn Hw (Set.Icc 10 Tb) ∧ StrictMonoOn (solEnthalpy (nSalt r) Hw Ph Hcr) (Set.Icc 10 Tb) ∧\n    (∀ x ∈ Set.Icc (20:ℝ) Tb, Hw x ≤ solEnthalpy (nSalt r) Hw Ph Hcr x) ∧\n    solEnthalpy (nSalt r) Hw Ph Hcr 10 ≤ Hw 20 := by\n  obtain ⟨hw1, hw2⟩ := w_range r hp.r_lo hp.r_hi\n  obtain ⟨hn1, hn2⟩ := n_range r hp.r_lo hp.r_hi\n  set w := r/(1+r) with hw\n  set n := nSalt r with hn\n  have hsub : ∀ T ∈ Set.Icc (10:ℝ) Tb, T ∈ Set.Icc (10:ℝ) 101 := fun T hT => ⟨hT.1, hT.2.trans hT2⟩\n  have hsub2 : ∀ T ∈ Set.Icc (20:ℝ) 25, T ∈ Set.Icc (10:ℝ) 101 := fun T hT => ⟨by linarith [hT.1], by linarith [hT.2]⟩\n  have hfl : ∀ T ∈ Set.Icc (10:ℝ) 101, -105 ≤ PhiC w T := fun T hT => phi_floor w T hw1 hT.1 hT.2\n  have hng : ∀ T ∈ Set.Icc (10:ℝ) 101, PhiC w T ≤ 0 := fun T hT => phi_nonpos w T hw1 hw2 hT.1 hT.2\n  -- Hw strictly increasing\n  have hWs : StrictMonoOn Hw (Set.Icc 10 Tb) := by\n    apply strictMonoOn_of_deriv_pos (convex_Icc _ _)\n    · intro T hT; exact (hp.hw_deriv T (hsub T hT)).continuousAt.continuousWithinAt\n    · intro T hT\n      have hT' := hsub T (interior_subset hT)\n      rw [(hp.hw_deriv T hT').deriv]; linarith [hp.hw_cap T hT']\n  -- solution enthalpy derivative\n  have hHs : ∀ T ∈ Set.Icc (10:ℝ) 101, HasDerivAt (solEnthalpy n Hw Ph Hcr) (Cw T + n * PhiC w T) T := by\n    intro T hT\n    have := (hp.hw_deriv T hT).add (((hp.ph_deriv T hT).sub_const (Hcr 20)).const_mul n)\n    exact this\n  have hSs : StrictMonoOn (solEnthalpy n Hw Ph Hcr) (Set.Icc 10 Tb) := by\n    apply strictMonoOn_of_deriv_pos (convex_Icc _ _)\n    · intro T hT; exact (hHs T (hsub T hT)).continuousAt.continuousWithinAt\n    · intro T hT\n      have hT' := hsub T (interior_subset hT)\n      rw [(hHs T hT').deriv]\n      have := hp.hw_cap T hT'\n      have := hfl T hT'\n      nlinarith\n  -- Kirchhoff: dissolution enthalpy hs(T) = Ph T - Hcr T between 20 and 25 C\n  have hd : ∀ T ∈ Set.Icc (20:ℝ) 25, HasDerivAt (fun T => Ph T - Hcr T) (PhiC w T - Cpcr T) T :=\n    fun T hT => (hp.ph_deriv T (hsub2 T hT)).sub (hp.cr_deriv T hT)\n  have hanti : AntitoneOn (fun T => Ph T - Hcr T) (Set.Icc 20 25) := by\n    apply antitoneOn_of_deriv_nonpos (convex_Icc _ _)\n    · intro T hT; exact (hd T hT).continuousAt.continuousWithinAt\n    · intro T hT; exact (hd T (interior_subset hT)).differentiableAt.differentiableWithinAt\n    · intro T hT\n      have hT' := interior_subset hT\n      rw [(hd T hT').deriv]\n      linarith [hng T (hsub2 T hT'), (hp.cr_cap T hT').1]\n  have hlow20 : Ph 25 - Hcr 25 ≤ Ph 20 - Hcr 20 := hanti ⟨le_rfl, by norm_num⟩ ⟨by norm_num, le_rfl⟩ (by norm_num)\n  have hmon2 : MonotoneOn (fun T => Ph T - Hcr T + 205 * T) (Set.Icc 20 25) := by\n    apply monotoneOn_of_deriv_nonneg (convex_Icc _ _)\n    · intro T hT; exact ((hd T hT).add_const_mul_id 205).continuousAt.continuousWithinAt\n    · intro T hT; exact ((hd T (interior_subset hT)).add_const_mul_id 205).differentiableAt.differentiableWithinAt\n    · intro T hT\n      have hT' := interior_subset hT\n      rw [((hd T hT').add_const_mul_id 205).deriv]\n      linarith [hfl T (hsub2 T hT'), (hp.cr_cap T hT').2]\n  have hup20 := hmon2 ⟨le_rfl, by norm_num⟩ ⟨by norm_num, le_rfl⟩ (by norm_num)\n  simp only at hup20\n  -- Ph on 10..20 changes by at most 105*10\n  have hmon3 : MonotoneOn (fun T => Ph T + 105 * T) (Set.Icc 10 20) := by\n    apply monotoneOn_of_deriv_nonneg (convex_Icc _ _)\n    · intro T hT; exact ((hp.ph_deriv T ⟨hT.1, by linarith [hT.2]⟩).add_const_mul_id 105).continuousAt.continuousWithinAt\n    · intro T hT; exact ((hp.ph_deriv T ⟨(interior_subset hT).1, by linarith [(interior_subset hT).2]⟩).add_const_mul_id 105).differentiableAt.differentiableWithinAt\n    · intro T hT\n      have hT' := interior_subset hT\n      rw [((hp.ph_deriv T ⟨hT'.1, by linarith [hT'.2]⟩).add_const_mul_id 105).deriv]\n      linarith [hfl T ⟨hT'.1, by linarith [hT'.2]⟩]\n  have hup10 := hmon3 ⟨le_rfl, by norm_num⟩ ⟨by norm_num, le_rfl⟩ (by norm_num)\n  simp only at hup10\n  -- Hw grows at least 4100 per K\n  have hmon4 : MonotoneOn (fun T => Hw T - 4100 * T) (Set.Icc 10 20) := by\n    apply monotoneOn_of_deriv_nonneg (convex_Icc _ _)\n    · intro T hT; exact ((hp.hw_deriv T ⟨hT.1, by linarith [hT.2]⟩).sub (hasDerivAt_id T |>.const_mul 4100)).continuousAt.continuousWithinAt\n    · intro T hT; exact ((hp.hw_deriv T ⟨(interior_subset hT).1, by linarith [(interior_subset hT).2]⟩).sub (hasDerivAt_id T |>.const_mul 4100)).differentiableAt.differentiableWithinAt\n    · intro T hT\n      have hT' := interior_subset hT\n      rw [((hp.hw_deriv T ⟨hT'.1, by linarith [hT'.2]⟩).sub (hasDerivAt_id T |>.const_mul 4100)).deriv]\n      simp\n      linarith [hp.hw_cap T ⟨hT'.1, by linarith [hT'.2]⟩]\n  have hup4 := hmon4 ⟨le_rfl, by norm_num⟩ ⟨by norm_num, le_rfl⟩ (by norm_num)\n  simp only at hup4\n  -- numbers from Parker: dissolution enthalpy at 20 C\n  have hcal : cal = 4.184 := rfl\n  obtain ⟨hi1, hi2⟩ := hp.hinf_b\n  obtain ⟨hl1, hl2⟩ := hp.phiL_b\n  have hs20lo : 3690 ≤ Ph 20 - Hcr 20 := by\n    have := hp.diss25; rw [hcal] at hi1 hl1; linarith\n  have hs20hi : Ph 20 - Hcr 20 ≤ 5400 := by\n    have := hp.diss25; rw [hcal] at hi2 hl2; linarith\n  refine ⟨hWs, hSs, ?_, ?_⟩\n  · -- cooling: E(x) = n*(Ph x - Hcr 20) >= 0 for 20 <= x <= Tb\n    intro x hx\n    have hanti2 : AntitoneOn Ph (Set.Icc 10 101) := by\n      apply antitoneOn_of_deriv_nonpos (convex_Icc _ _)\n      · intro T hT; exact (hp.ph_deriv T hT).continuousAt.continuousWithinAt\n      · intro T hT; exact (hp.ph_deriv T (interior_subset hT)).differentiableAt.differentiableWithinAt\n      · intro T hT\n        have hT' := interior_subset hT\n        rw [(hp.ph_deriv T hT').deriv]; exact hng T hT'\n    have h1 : Ph 101 ≤ Ph x := hanti2 ⟨by linarith [hx.1], by linarith [hx.2]⟩ ⟨by norm_num, le_rfl⟩ (by linarith [hx.2])\n    have h2 := ph_drop w Ph hw1 hp.ph_deriv\n    have : 0 ≤ Ph x - Hcr 20 := by linarith\n    unfold solEnthalpy\n    nlinarith\n  · unfold solEnthalpy\n    have : Ph 10 - Hcr 20 ≤ 6600 := by linarith\n    nlinarith\nE\ncd /tmp/lw; cat f.lean g_head.lean > g.lean; cd /tmp; time bash /tmp/scr/lc.sh /tmp/lw/g.lean 100 | grep -v -E \"push_neg|^If you|^open|^macro|^\\`\\`\\`|^  \\`\\(tactic|^$\""
}
Bash output
/tmp/lw/g.lean:79:31: warning: Variable name `hab` is not explicitly referenced.
Hint: The binding can be removed (if unused) or named `_` (if used implicitly). Alternatively, prefix the name with `_` to silence this warning:
  [apply] _hab
Note: This linter can be disabled with `set_option linter.unusedVariables false`
/tmp/lw/g.lean:523:35: error(lean.invalidField): Invalid field `add_const_mul_id`: The environment does not contain `HasFDerivAtFilter.add_const_mul_id`, so it is not possible to project the field `add_const_mul_id` from an expression
  hd T hT
of type
  HasFDerivAtFilter (fun T => Ph T - Hcr T) (ContinuousLinearMap.toSpanSingleton ℝ (PhiC w T - Cpcr T))
    (nhds T ×ˢ pure T)
/tmp/lw/g.lean:524:53: error(lean.invalidField): Invalid field `add_const_mul_id`: The environment does not contain `HasFDerivAtFilter.add_const_mul_id`, so it is not possible to project the field `add_const_mul_id` from an expression
  hd T (interior_subset hT)
of type
  HasFDerivAtFilter (fun T => Ph T - Hcr T) (ContinuousLinearMap.toSpanSingleton ℝ (PhiC w T - Cpcr T))
    (nhds T ×ˢ pure T)
/tmp/lw/g.lean:527:10: error: Invalid rewrite argument: Expected an equality or iff proof or definition name, but `?m.961` is a value of type
  ?m.960
/tmp/lw/g.lean:527:22: error(lean.invalidField): Invalid field `add_const_mul_id`: The environment does not contain `HasFDerivAtFilter.add_const_mul_id`, so it is not possible to project the field `add_const_mul_id` from an expression
  hd T hT'
of type
  HasFDerivAtFilter (fun T => Ph T - Hcr T) (ContinuousLinearMap.toSpanSingleton ℝ (PhiC w T - Cpcr T))
    (nhds T ×ˢ pure T)
/tmp/lw/g.lean:534:68: error(lean.invalidField): Invalid field `add_const_mul_id`: The environment does not contain `HasFDerivAtFilter.add_const_mul_id`, so it is not possible to project the field `add_const_mul_id` from an expression
  hp.ph_deriv T ⟨hT.left, ?m.1040⟩
of type
  HasFDerivAtFilter Ph (ContinuousLinearMap.toSpanSingleton ℝ (PhiC (r / (1 + r)) T)) (nhds T ×ˢ pure T)
/tmp/lw/g.lean:535:104: error(lean.invalidField): Invalid field `add_const_mul_id`: The environment does not contain `HasFDerivAtFilter.add_const_mul_id`, so it is not possible to project the field `add_const_mul_id` from an expression
  hp.ph_deriv T ⟨(interior_subset hT).left, ?m.1096⟩
of type
  HasFDerivAtFilter Ph (ContinuousLinearMap.toSpanSingleton ℝ (PhiC (r / (1 + r)) T)) (nhds T ×ˢ pure T)
/tmp/lw/g.lean:538:10: error: Invalid rewrite argument: Expected an equality or iff proof or definition name, but `?m.1147` is a value of type
  ?m.1146
/tmp/lw/g.lean:538:56: error(lean.invalidField): Invalid field `add_const_mul_id`: The environment does not contain `HasFDerivAtFilter.add_const_mul_id`, so it is not possible to project the field `add_const_mul_id` from an expression
  hp.ph_deriv T ⟨hT'.left, ?m.1178⟩
of type
  HasFDerivAtFilter Ph (ContinuousLinearMap.toSpanSingleton ℝ (PhiC (r / (1 + r)) T)) (nhds T ×ˢ pure T)
/tmp/lw/g.lean:549:10: error: Tactic `rewrite` failed: Did not find an occurrence of the pattern
  deriv (Hw - fun y => 4100 * id y) T
in the target expression
  0 ≤ deriv (fun T => Hw T - 4100 * T) T
case hf'_nonneg
r Tb : ℝ
Cw Hw Ph Hcr Cpcr : ℝ → ℝ
hinf phiL : ℝ
hp : Premises r Cw Hw Ph Hcr Cpcr hinf phiL
hT1 : 20 ≤ Tb
hT2 : Tb ≤ 101
w : ℝ := r / (1 + r)
hw1 : 1 / 51 ≤ w
hw2 : w ≤ 1 / 21
hw : w = r / (1 + r)
n : ℝ := nSalt r
hn1 : 0.34 ≤ n
hn2 : n ≤ 0.86
hn : n = nSalt r
hsub : ∀ T ∈ Set.Icc 10 Tb, T ∈ Set.Icc 10 101
hsub2 : ∀ T ∈ Set.Icc 20 25, T ∈ Set.Icc 10 101
hfl : ∀ T ∈ Set.Icc 10 101, -105 ≤ PhiC w T
hng : ∀ T ∈ Set.Icc 10 101, PhiC w T ≤ 0
hWs : StrictMonoOn Hw (Set.Icc 10 Tb)
hHs : ∀ T ∈ Set.Icc 10 101, HasDerivAt (solEnthalpy n Hw Ph Hcr) (Cw T + n * PhiC w T) T
hSs : StrictMonoOn (solEnthalpy n Hw Ph Hcr) (Set.Icc 10 Tb)
hd : ∀ T ∈ Set.Icc 20 25, HasDerivAt (fun T => Ph T - Hcr T) (PhiC w T - Cpcr T) T
hanti : AntitoneOn (fun T => Ph T - Hcr T) (Set.Icc 20 25)
hlow20 : Ph 25 - Hcr 25 ≤ Ph 20 - Hcr 20
hmon2 : MonotoneOn (fun T => Ph T - Hcr T + 205 * T) (Set.Icc 20 25)
hup20 : Ph 20 - Hcr 20 + 205 * 20 ≤ Ph 25 - Hcr 25 + 205 * 25
hmon3 : MonotoneOn (fun T => Ph T + 105 * T) (Set.Icc 10 20)
hup10 : Ph 10 + 105 * 10 ≤ Ph 20 + 105 * 20
T : ℝ
hT : T ∈ interior (Set.Icc 10 20)
hT' : T ∈ Set.Icc 10 20
⊢ 0 ≤ deriv (fun T => Hw T - 4100 * T) T

real	0m13.420s
user	0m19.715s
sys	0m1.029s
Shell cwd was reset to /work
Bash input
{
  "command": "cd /tmp/lw; python3 - <<'E'\ns=open('g_head.lean').read()\nhelper='''lemma lin_deriv (F : ℝ → ℝ) (d c T : ℝ) (h : HasDerivAt F d T) :\n    HasDerivAt (fun y => F y + c*y) (d + c) T := by\n  simpa using h.add ((hasDerivAt_id T).const_mul c)\n\ntheorem thermo_facts'''\ns=s.replace(\"theorem thermo_facts\",helper,1)\ns=s.replace(\"((hd T hT).add_const_mul_id 205)\",\"(lin_deriv _ _ 205 T (hd T hT))\")\ns=s.replace(\"((hd T (interior_subset hT)).add_const_mul_id 205)\",\"(lin_deriv _ _ 205 T (hd T (interior_subset hT)))\")\ns=s.replace(\"((hd T hT').add_const_mul_id 205)\",\"(lin_deriv _ _ 205 T (hd T hT'))\")\ns=s.replace(\"((hp.ph_deriv T ⟨hT.1, by linarith [hT.2]⟩).add_const_mul_id 105)\",\"(lin_deriv _ _ 105 T (hp.ph_deriv T ⟨hT.1, by linarith [hT.2]⟩))\")\ns=s.replace(\"((hp.ph_deriv T ⟨(interior_subset hT).1, by linarith [(interior_subset hT).2]⟩).add_const_mul_id 105)\",\"(lin_deriv _ _ 105 T (hp.ph_deriv T ⟨(interior_subset hT).1, by linarith [(interior_subset hT).2]⟩))\")\ns=s.replace(\"((hp.ph_deriv T ⟨hT'.1, by linarith [hT'.2]⟩).add_const_mul_id 105)\",\"(lin_deriv _ _ 105 T (hp.ph_deriv T ⟨hT'.1, by linarith [hT'.2]⟩))\")\n# hmon4\ni=s.index(\"  have hmon4\"); j=s.index(\"  have hup4\")\nnew='''  have hmon4 : MonotoneOn (fun T => Hw T + (-4100) * T) (Set.Icc 10 20) := by\n    apply monotoneOn_of_deriv_nonneg (convex_Icc _ _)\n    · intro T hT; exact (lin_deriv _ _ (-4100) T (hp.hw_deriv T ⟨hT.1, by linarith [hT.2]⟩)).continuousAt.continuousWithinAt\n    · intro T hT; exact (lin_deriv _ _ (-4100) T (hp.hw_deriv T ⟨(interior_subset hT).1, by linarith [(interior_subset hT).2]⟩)).differentiableAt.differentiableWithinAt\n    · intro T hT\n      have hT' := interior_subset hT\n      rw [(lin_deriv _ _ (-4100) T (hp.hw_deriv T ⟨hT'.1, by linarith [hT'.2]⟩)).deriv]\n      linarith [hp.hw_cap T ⟨hT'.1, by linarith [hT'.2]⟩]\n'''\ns=s[:i]+new+s[j:]\nopen('g_head.lean','w').write(s)\nE\ncat f.lean g_head.lean > g.lean; cd /tmp; bash /tmp/scr/lc.sh /tmp/lw/g.lean 100 | grep -v -E \"push_neg|^If you|^open|^macro|^\\`\\`\\`|^  \\`\\(tactic|^$|hab|Hint|Note\""
}
Bash output
/tmp/lw/g.lean:477:2: error: Type mismatch: After simplification, term
  HasDerivAt.add h (HasDerivAt.const_mul c (hasDerivAt_id T))
 has type
  @HasDerivAt ℝ DenselyNormedField.toNontriviallyNormedField ℝ Real.normedAddCommGroup.toAddCommGroup
    NormedField.toNormedSpace.toModule PseudoMetricSpace.toUniformSpace.toTopologicalSpace ⋯ (F + fun y => c * y)
    (d + c) T
but is expected to have type
  @HasDerivAt ℝ DenselyNormedField.toNontriviallyNormedField ℝ Real.instAddCommGroup Semiring.toModule
    PseudoMetricSpace.toUniformSpace.toTopologicalSpace ⋯ (fun y => F y + c * y) (d + c) T
/tmp/lw/g.lean:480:51: warning: Variable name `hT1` is not explicitly referenced.
  [apply] _hT1
Shell cwd was reset to /work
Bash input
{
  "command": "cd /tmp/lw; python3 - <<'E'\ns=open('g_head.lean').read()\ns=s.replace(\"  simpa using h.add ((hasDerivAt_id T).const_mul c)\",\"  exact (h.add ((hasDerivAt_id T).const_mul c)).congr_deriv (by simp)\")\nopen('g_head.lean','w').write(s)\nE\ncat f.lean g_head.lean > g.lean; cd /tmp; bash /tmp/scr/lc.sh /tmp/lw/g.lean 100 | grep -v -E \"push_neg|^If you|^open|^macro|^\\`\\`\\`|^  \\`\\(tactic|^$|hab|Hint|Note\""
}
Bash output
/tmp/lw/g.lean:480:51: warning: Variable name `hT1` is not explicitly referenced.
  [apply] _hT1
Shell cwd was reset to /work
Bash input
{
  "command": "cd /tmp/lw; python3 - <<'E'\ns=open('b_head.lean').read()\ns=s.replace(\"lemma phi_nonpos (w T : ℝ) (h1 : 1/51 ≤ w) (h2 : w ≤ 1/21) (t1 : 10 ≤ T) (t2 : T ≤ 101) : PhiC w T ≤ 0\",\"lemma phi_neg (w T : ℝ) (h1 : 1/51 ≤ w) (h2 : w ≤ 1/21) (t1 : 10 ≤ T) (t2 : T ≤ 101) : PhiC w T < 0\")\ns=s.replace(\"a5 * w ^ a6 ≤ 0 := by\",\"a5 * w ^ a6 < 0 := by\")\ns=s.replace(\"exact mul_nonpos_of_nonneg_of_nonpos hM.le this\",\"exact mul_neg_of_pos_of_neg hM this\")\nopen('b_head.lean','w').write(s)\ng=open('g_head.lean').read()\ng=g.replace(\"phi_nonpos w T hw1 hw2 hT.1 hT.2\",\"(phi_neg w T hw1 hw2 hT.1 hT.2).le\")\ng=g.replace(\" (hT1 : 20 ≤ Tb) (hT2 : Tb ≤ 101) :\",\" (hT2 : Tb ≤ 101) :\")\nopen('g_head.lean','w').write(g)\nE\ncat > h_tail.lean <<'E'\n\n/-! ## Conclusions -/\n\n/-- Laliberté's mixing rule Cp_sol = w_w Cp_w + w_s Cp_i, with w_s = ms/(ms+mw), w_w = mw/(ms+mw), gives\n    C_solution - C_water = ms*Cp_i = (ms/M) * (M*Cp_i): the heat-capacity change per mole of salt is M*Cp_i = PhiC. -/\nlemma apparent_capacity (ms mw M cpw cpi : ℝ) (hM : 0 < M) (hs : 0 < ms + mw) :\n    (ms+mw) * ((mw/(ms+mw)) * cpw + (ms/(ms+mw)) * cpi) - mw * cpw = (ms/M) * (M * cpi) := by\n  field_simp\n  ring\n\n/-- CONCLUSION 1 (heat capacity). For 20-50 g NaCl per kg water and 10-101 C, the apparent molar heat capacity\n    of NaCl in solution is negative, between -105 and 0 J/(K mol); so dissolving the salt LOWERS the total heat\n    capacity of the pot contents, by n*Phi in (-91, 0) J/K (< 2.2 % of the ~4.2 kJ/K of the water). -/\ntheorem cs_sign : ∀ (r T : ℝ), 1/50 ≤ r → r ≤ 1/20 → 10 ≤ T → T ≤ 101 →\n    -105 ≤ PhiC (r/(1+r)) T ∧ PhiC (r/(1+r)) T < 0 ∧\n    -91 < nSalt r * PhiC (r/(1+r)) T ∧ nSalt r * PhiC (r/(1+r)) T < 0 := by\n  intro r T h1 h2 t1 t2\n  obtain ⟨w1, w2⟩ := w_range r h1 h2\n  obtain ⟨n1, n2⟩ := n_range r h1 h2\n  have f := phi_floor _ T w1 t1 t2\n  have g := phi_neg _ T w1 w2 t1 t2\n  refine ⟨f, g, ?_, ?_⟩ <;> nlinarith\n\n/-- CONCLUSION 2 (answer). Salt added at the very start (water at 20 C) gives a boiling time no longer than salt\n    added when the water has reached any later temperature Ta <= Tb. -/\ntheorem answer_theorem : ∀ (r P h Tb : ℝ) (Cw Hw Ph Hcr Cpcr Tw Ts τw τs : ℝ → ℝ) (hinf phiL : ℝ),\n    Premises r Cw Hw Ph Hcr Cpcr hinf phiL → 20 ≤ Tb → Tb ≤ 101 → 0 ≤ h → h*(Tb-20) < P →\n    (∀ H ∈ Set.Icc (Hw 20) (Hw Tb), Tw H ∈ Set.Icc 10 Tb ∧ Hw (Tw H) = H) →\n    (∀ H ∈ Set.Icc (solEnthalpy (nSalt r) Hw Ph Hcr 10) (solEnthalpy (nSalt r) Hw Ph Hcr Tb),\n        Ts H ∈ Set.Icc 10 Tb ∧ solEnthalpy (nSalt r) Hw Ph Hcr (Ts H) = H) →\n    (∀ H ∈ Set.Icc (Hw 20) (Hw Tb), HasDerivAt τw (1/(P - h*(Tw H - 20))) H) →\n    (∀ H ∈ Set.Icc (solEnthalpy (nSalt r) Hw Ph Hcr 10) (solEnthalpy (nSalt r) Hw Ph Hcr Tb),\n        HasDerivAt τs (1/(P - h*(Ts H - 20))) H) →\n    ∀ Ta ∈ Set.Icc (20:ℝ) Tb,\n      boilTime τw τs Hw (solEnthalpy (nSalt r) Hw Ph Hcr) 20 Tb 20\n        ≤ boilTime τw τs Hw (solEnthalpy (nSalt r) Hw Ph Hcr) 20 Tb Ta := by\n  intro r P h Tb Cw Hw Ph Hcr Cpcr Tw Ts τw τs hinf phiL hp hT1 hT2 hh hP hTw hTs hτw hτs\n  obtain ⟨m1, m2, c1, c2⟩ := thermo_facts r Tb Cw Hw Ph Hcr Cpcr hinf phiL hp hT2\n  exact timing_core P h 20 10 Tb Hw (solEnthalpy (nSalt r) Hw Ph Hcr) Tw Ts τw τs hh hP (by norm_num) hT1\n    m1 m2 hTw hTs hτw hτs c1 c2\nE\ncat b_head.lean | head -0; (sed '/^example/,$d' a.lean; cat b_head.lean c_head.lean nodes.lean e_head.lean e_tail.lean f_head.lean g_head.lean h_tail.lean) > full.lean; cd /tmp; bash /tmp/scr/lc.sh /tmp/lw/full.lean 100 | grep -v -E \"push_neg|^If you|^open|^macro|^\\`\\`\\`|^  \\`\\(tactic|^$|Hint|Note\""
}
Bash output
/tmp/lw/full.lean:79:31: warning: Variable name `hab` is not explicitly referenced.
  [apply] _hab
Shell cwd was reset to /work
Read input
{
  "file_path": "/work/inputs/material-literature-002/parker-enthalpy-infinity.png"
}
Read output
https://doi.org/10.6028/NBS.NSRDS.2
Read input
{
  "file_path": "/work/inputs/material-literature-002/parker-table-X.png"
}
Read output
https://doi.org/10.6028/NBS.NSRDS.2
Bash input
{
  "command": "cd /work/inputs/material-literature-002; head -30 archer-solid-knots.csv; grep -n \"298.15\" archer-1992-part1-crystalline.txt | head"
}
Bash output
T_K,f_dimensionless
0,20.4174
5,18.6946
7.5,18.2037
15,15.2827
20,13.2900
45,8.12535
90,5.77101
220,5.62745
400,7.04825
550,7.41175
800,5.08930
1100,-8.76890
100:compilations list values of the 298.15 K heat capacity of                   larger than the experimental data available to each of the
106:298.15 K heat-capacity values given in these data evalua-                   has prompted a re-examination of the thermodynamic
116:heat- capacity value for 298.15 K given by Chase et al. s is                resentation of the various experimental results that con-
706:         20033                 20032                   1                   672.2                    298.15                  0.025          41.84
707:         20033                 20032                   1                   672.2                    298.15                0.025            41.84
708:         22979                 22967                  11                   723.6                    298.15                0.245            46.02
709:         22928                 22961                -33                    723.5                    298.15              -0.720             46.02
710:         25983                 25992                 -9                    775.6                    298.15              -0.179             50.21
711:         25786                 25821                -35                    772.7                    298.15              -0.704             50.21
712:         28828                 28852                -25                    823.8                    298.15              -0.421             58.58
Bash input
{
  "command": "cat > /tmp/lab/spl.py <<'E'\nimport csv\nrows=list(csv.reader(open('/work/inputs/material-literature-002/archer-solid-knots.csv')))[1:]\nx=[float(r[0]) for r in rows]; y=[float(r[1]) for r in rows]\nn=len(x)\n# natural cubic spline\nh=[x[i+1]-x[i] for i in range(n-1)]\nA=[[0]*n for _ in range(n)]; b=[0]*n\nA[0][0]=1; A[-1][-1]=1\nfor i in range(1,n-1):\n    A[i][i-1]=h[i-1]; A[i][i]=2*(h[i-1]+h[i]); A[i][i+1]=h[i]\n    b[i]=3*((y[i+1]-y[i])/h[i]-(y[i]-y[i-1])/h[i-1])\n# solve\nimport copy\nM=[A[i]+[b[i]] for i in range(n)]\nfor i in range(n):\n    p=max(range(i,n),key=lambda k:abs(M[k][i])); M[i],M[p]=M[p],M[i]\n    for k in range(n):\n        if k!=i:\n            f=M[k][i]/M[i][i]\n            M[k]=[a-f*c for a,c in zip(M[k],M[i])]\nc=[M[i][n]/M[i][i] for i in range(n)]\ndef f(t):\n    i=max(j for j in range(n-1) if x[j]<=t)\n    d=[(y[i+1]-y[i])/h[i]-h[i]*(2*c[i]+c[i+1])/3]\n    bb=d[0]; dd=(c[i+1]-c[i])/(3*h[i]); s=t-x[i]\n    return y[i]+bb*s+c[i]*s*s+dd*s**3\nfor t in (293.15,295,298.15):\n    print(t,(t/(f(t)+0.25*t))**3)\nE\npython3 /tmp/lab/spl.py"
}
Bash output
293.15 50.03712000772833
295 50.08141666504637
298.15 50.155875194481766
Bash input
{
  "command": "cd /tmp/lw; python3 - <<'E'\ns=open('g_head.lean').read()\ns=s.replace(\"  cr_cap : ∀ T ∈ Set.Icc (20:ℝ) 25, 0 ≤ Cpcr T ∧ Cpcr T ≤ 100\",\"  cr_cap : ∀ T ∈ Set.Icc (20:ℝ) 25, 40 ≤ Cpcr T ∧ Cpcr T ≤ 100\")\ni=s.index(\"  have hanti\"); j=s.index(\"  have hmon2\")\nnew='''  have hanti : AntitoneOn (fun T => Ph T - Hcr T + 40 * T) (Set.Icc 20 25) := by\n    apply antitoneOn_of_deriv_nonpos (convex_Icc _ _)\n    · intro T hT; exact (lin_deriv _ _ 40 T (hd T hT)).continuousAt.continuousWithinAt\n    · intro T hT; exact (lin_deriv _ _ 40 T (hd T (interior_subset hT))).differentiableAt.differentiableWithinAt\n    · intro T hT\n      have hT' := interior_subset hT\n      rw [(lin_deriv _ _ 40 T (hd T hT')).deriv]\n      linarith [hng T (hsub2 T hT'), (hp.cr_cap T hT').1]\n  have hlow20 := hanti ⟨le_rfl, by norm_num⟩ ⟨by norm_num, le_rfl⟩ (by norm_num)\n  simp only at hlow20\n'''\ns=s[:i]+new+s[j:]\ns=s.replace(\"have hs20lo : 3690 ≤\",\"have hs20lo : 3890 ≤\")\nopen('g_head.lean','w').write(s)\nE\n(sed '/^example/,$d' a.lean; cat b_head.lean c_head.lean nodes.lean e_head.lean e_tail.lean f_head.lean g_head.lean h_tail.lean) > full.lean; cd /tmp; bash /tmp/scr/lc.sh /tmp/lw/full.lean 100 | grep -v -E \"push_neg|^If you|^open|^macro|^\\`\\`\\`|^  \\`\\(tactic|^$|Hint|Note\""
}
Bash output
/tmp/lw/full.lean:79:31: warning: Variable name `hab` is not explicitly referenced.
  [apply] _hab
Shell cwd was reset to /work
Bash input
{
  "command": "cd /work; mkdir -p attempts/cycle5_secant; cp Model.lean report.txt candidate.json attempts/cycle5_secant/; python3 - <<'E'\nbody=open('/tmp/lw/full.lean').read()\nimports,rest=body.split(\"\\nlemma exp_ub\",1)\nrest=\"\\nlemma exp_ub\"+rest\nheader='''/-! # Salt timing for 1 L of water at 20 C: when should NaCl be added? (cycle 6)\n\n## REVIEW PACKET\nSUPPLIED BY THE USER: 1 L water at 20 C in a pot on a stove; salt will be added; goal = salted water reaches\nits boiling point soonest; salt amount, pot, heater, room unspecified.\n\nACCEPTED EARLIER (kept): lumped Newtonian heating dH/dt = P - h (T - 20), constant stove power P, no evaporation,\nsalt (crystal at 20 C) dissolves instantly at the moment of addition (total enthalpy conserved).\n\nMODEL CHOICES / ASSUMPTIONS (all appear as `Premises` fields or theorem hypotheses):\n * NaCl; r = salt mass / water mass in [1/50, 1/20] (20-50 g in the 0.998 kg of water of 1 L); mass fraction\n   w = r/(1+r) (conversion proved in `w_range`), n = 1000*0.998*r/58.44 mol (`nSalt`, bounds in `n_range`).\n * Pot+water enthalpy Hw(T) has heat capacity Cw(T) >= 4100 J/K on 10..101 C (0.998 kg water >= 4.17 kJ/K; pot adds).\n * Apparent molar heat capacity of NaCl in the final solution = Laliberte (2009) correlation `PhiC`\n   (empirical coefficients a1..a6 from Laliberte, J. Chem. Eng. Data 54, 1725; fit range 1.5-120 C, w <= 0.26).\n   Ph(T) is the apparent molar enthalpy of NaCl in that solution: dPh/dT = PhiC (definition of Phi_C).\n   Solution enthalpy = (water at T) + n*Ph(T)   (definition of apparent molar enthalpy; `solEnthalpy`).\n * Crystal NaCl enthalpy Hcr with Cp_cr in [40,100] J/K/mol on 20..25 C (Archer 1992 Part I, Eqs 4-6: Cp(298.15)=50.16;\n   my evaluation of the spline gives 50.04..50.16 on 20..25 C).\n * Parker NSRDS-NBS 2 (1965): Delta H_inf(25 C, NaCl(c) -> infinite dilution) = 928 +- 5 cal/mol (printed p.26);\n   Phi_L(25 C, m) (Table X, p.50) is RELATIVE to infinite dilution: Ph(25) - Hcr(25) = Hinf + Phi_L.\n   For the regime m = 17.1 r in [0.34, 0.86] mol/kg Table X gives Phi_L = 83, 49, 20, -41 cal/mol at m = .2775,\n   .5551, .7401, 1.1101 and dPhi_L/dm^(1/2) < 0 for m >= .185, so Phi_L in [-41, 88]; premise used: [-41, 100] cal. cal = 4.184 J.\n * Boiling temperature Tb of the final solution: 20 <= Tb <= 101 C (1 atm, <= 4.8 wt%: ideal elevation <= ~0.9 K); stove\n   beats losses at Tb; Tw, Ts = temperature as function of total enthalpy (inverse of Hw, Hs; existence by IVT,\n   `inverse_exists`); tau_w, tau_s = elapsed time as function of enthalpy: d tau/dH = 1/(P - h (T(H) - 20)).\n\nPROVED (not assumed): Phi_C < 0 and >= -105 J/(K mol) for all regime/temperatures (concavity + exp bounds, `cs_sign`);\n trapezoid enclosure Ph(101) - Ph(20) >= -3363 J/mol from 11 Taylor-bounded exp evaluations (`ph_drop`);\n dissolution at 20 C >= 3890 J/mol via Kirchhoff (`thermo_facts`); salt addition at any x in [20,Tb] cools\n (Hs(x) - Hw(x) >= 0), solution enthalpy strictly increasing, mixing at 20 C stays above 10 C; and the timing\n comparison (`timing_core`, `answer_theorem`).\nCONCLUSIONS: `cs_sign`, `answer_theorem` (salting at the start is no slower than salting at any later water temperature).\n-/\n'''\nopen('/work/Model.lean','w').write(imports+\"\\n\"+header+rest)\nE\nhead -8 Model.lean; python /harness/src/vcs_protocol.py tool check --manifest /work/candidate.json 2>&1 | tail -5"
}
Bash output
import Mathlib.Analysis.SpecialFunctions.Log.Deriv
import Mathlib.Analysis.SpecialFunctions.Pow.Real
import Mathlib.Analysis.SpecialFunctions.ExpDeriv
import Mathlib.Analysis.Calculus.MeanValue

/-! # Salt timing for 1 L of water at 20 C: when should NaCl be added? (cycle 6)

## REVIEW PACKET
  "reviewable": false,
  "verdict": "not_proved",
  "time": "2026-11-18T01:07:25.360862+00:00",
  "error": "Lean body must contain at most 24000 UTF-8 bytes"
}
Bash input
{
  "command": "cd /work; wc -c Model.lean /tmp/lw/full.lean /tmp/lw/nodes.lean /tmp/lw/c_head.lean /tmp/lw/f_head.lean /tmp/lw/g_head.lean /tmp/lw/b_head.lean /tmp/lw/h_tail.lean; sed -n 130,150p /harness/src/vcs_protocol.py; grep -n \"24000\" -B5 /harness/src/vcs_lean.py | head -30"
}
Bash output
36311 Model.lean
33382 /tmp/lw/full.lean
 8754 /tmp/lw/nodes.lean
 3889 /tmp/lw/c_head.lean
 3855 /tmp/lw/f_head.lean
 8251 /tmp/lw/g_head.lean
 2718 /tmp/lw/b_head.lean
 2638 /tmp/lw/h_tail.lean
99798 total
        raise ValueError("facts, choices and substantive proof_scope are required")
    return m, inside(work, m["proof_file"]), inside(work, m["narrative_file"])


def validate_source(source, names):
    """Fixed library trust base; reuse hardened no-metaprogramming body contract."""
    from vcs_lean import strip_comments, validate
    code = strip_comments(source)
    imports = re.findall(r"(?m)^import ([A-Za-z0-9_.]+)\s*$", code)
    if not imports or any(not n.startswith("Mathlib.") for n in imports):
        raise ValueError("explicit imports of pinned Mathlib modules only")
    body = re.sub(r"(?m)^import [A-Za-z0-9_.]+\s*$", "", code)
    validate(body, names)
    if re.search(r"\bconclusion_\w*", body):
        raise ValueError("conclusion_ names are reserved for independent extraction")
    # Extractor requires a colon immediately after the theorem name. Fail clearly.
    for n in names:
        if not re.search(r"(?m)^\s*(?:theorem|lemma)\s+" + re.escape(n) + r"\s*:", body):
            raise ValueError("conclusion binders must be inside the statement after ':'")


44-        raise ValueError("Unclosed Lean comment")
45-    return "".join(out)
46-
47-
48-def validate(source, required):
49:    if not isinstance(source, str) or not 0 < len(source.encode()) <= 24000:
50:        raise ValueError("Lean body must contain at most 24000 UTF-8 bytes")
Bash input
{
  "command": "cd /tmp/lw; cat > gen2.py <<'E'\nimport json,re\nout=json.load(open('/tmp/lab/nodes.json'))\ntac=\"by norm_num [Q, Finset.sum_range_succ, Nat.factorial]\"\nnodes=\"\"\"\nlemma nodes (w : ℝ) (h : 1/51 ≤ w) : \"\"\"\nparts=[]; calls=[]\nfor (T,V,beta,U,L,t) in out:\n    parts.append(f\"-({L}:ℝ) ≤ PhiC w {T}\")\n    calls.append(f\"node w {T} {V} {beta} {U} {L} h (by norm_num) (by norm_num) ({tac}) (by norm_num [a2,a3,a4]) (by norm_num) (by norm_num) ({tac}) (by norm_num [Mnacl,a1])\")\nnodes+=\" ∧ \".join(parts)+\" := ⟨\"+\",\\n  \".join(calls)+\"⟩\\n\"\nopen('nodes2.lean','w').write(nodes)\nnn=[o[0] for o in out if o[0]>=20]\ns=\"\"\nnames=\" \".join(f\"n{o[0]}\" for o in out)\ns+=\"  obtain ⟨n10, \"+\", \".join(f\"n{o[0]}\" for o in out[1:])+\"⟩ := nodes w h1\\n\"\nfor i in range(len(nn)-1):\n    a,b=nn[i],nn[i+1]\n    s+=f\"  have p{i} := piece w Ph {a} {b} _ _ (by norm_num) (by norm_num) (by norm_num) hPh n{a} n{b}\\n\"\ns+=\"  norm_num at *\\n  linarith\\n\"\nopen('e_tail2.lean','w').write(s)\nE\npython3 gen2.py; cat nodes2.lean | head -5"
}
Bash output
lemma nodes (w : ℝ) (h : 1/51 ≤ w) : -(104.51:ℝ) ≤ PhiC w 10 ∧ -(74.77:ℝ) ≤ PhiC w 20 ∧ -(56.07:ℝ) ≤ PhiC w 30 ∧ -(44.29:ℝ) ≤ PhiC w 40 ∧ -(37.06:ℝ) ≤ PhiC w 50 ∧ -(33.04:ℝ) ≤ PhiC w 60 ∧ -(31.6:ℝ) ≤ PhiC w 70 ∧ -(32.65:ℝ) ≤ PhiC w 80 ∧ -(36.75:ℝ) ≤ PhiC w 90 ∧ -(45.49:ℝ) ≤ PhiC w 100 ∧ -(46.73:ℝ) ≤ PhiC w 101 := ⟨node w 10 1.10519 3.2497 25.784 104.51 h (by norm_num) (by norm_num) (by norm_num [Q, Finset.sum_range_succ, Nat.factorial]) (by norm_num [a2,a3,a4]) (by norm_num) (by norm_num) (by norm_num [Q, Finset.sum_range_succ, Nat.factorial]) (by norm_num [Mnacl,a1]),
  node w 20 1.22142 2.9148 18.447 74.77 h (by norm_num) (by norm_num) (by norm_num [Q, Finset.sum_range_succ, Nat.factorial]) (by norm_num [a2,a3,a4]) (by norm_num) (by norm_num) (by norm_num [Q, Finset.sum_range_succ, Nat.factorial]) (by norm_num [Mnacl,a1]),
  node w 30 1.34987 2.6269 13.832 56.07 h (by norm_num) (by norm_num) (by norm_num [Q, Finset.sum_range_succ, Nat.factorial]) (by norm_num [a2,a3,a4]) (by norm_num) (by norm_num) (by norm_num [Q, Finset.sum_range_succ, Nat.factorial]) (by norm_num [Mnacl,a1]),
  node w 40 1.49184 2.3911 10.927 44.29 h (by norm_num) (by norm_num) (by norm_num [Q, Finset.sum_range_succ, Nat.factorial]) (by norm_num [a2,a3,a4]) (by norm_num) (by norm_num) (by norm_num [Q, Finset.sum_range_succ, Nat.factorial]) (by norm_num [Mnacl,a1]),
Shell cwd was reset to /work
Bash input
{
  "command": "cd /tmp/lw; cat > P1.lean <<'E'\nimport Mathlib.Analysis.SpecialFunctions.Log.Deriv\nimport Mathlib.Analysis.SpecialFunctions.Pow.Real\nimport Mathlib.Analysis.SpecialFunctions.ExpDeriv\nimport Mathlib.Analysis.Calculus.MeanValue\n\n/-! (header review packet inserted by script) -/\n\n/-! ### calculus helpers -/\nlemma dmono (F f : ℝ → ℝ) (a b : ℝ) (hF : ∀ T ∈ Set.Icc a b, HasDerivAt F (f T) T)\n    (hf : ∀ T ∈ Set.Icc a b, 0 ≤ f T) : MonotoneOn F (Set.Icc a b) := by\n  apply monotoneOn_of_deriv_nonneg (convex_Icc a b)\n  · exact fun T hT => (hF T hT).continuousAt.continuousWithinAt\n  · exact fun T hT => (hF T (interior_subset hT)).differentiableAt.differentiableWithinAt\n  · intro T hT; rw [(hF T (interior_subset hT)).deriv]; exact hf T (interior_subset hT)\n\nlemma danti (F f : ℝ → ℝ) (a b : ℝ) (hF : ∀ T ∈ Set.Icc a b, HasDerivAt F (f T) T)\n    (hf : ∀ T ∈ Set.Icc a b, f T ≤ 0) : AntitoneOn F (Set.Icc a b) := by\n  apply antitoneOn_of_deriv_nonpos (convex_Icc a b)\n  · exact fun T hT => (hF T hT).continuousAt.continuousWithinAt\n  · exact fun T hT => (hF T (interior_subset hT)).differentiableAt.differentiableWithinAt\n  · intro T hT; rw [(hF T (interior_subset hT)).deriv]; exact hf T (interior_subset hT)\n\nlemma dstrict (F f : ℝ → ℝ) (a b : ℝ) (hF : ∀ T ∈ Set.Icc a b, HasDerivAt F (f T) T)\n    (hf : ∀ T ∈ Set.Icc a b, 0 < f T) : StrictMonoOn F (Set.Icc a b) := by\n  apply strictMonoOn_of_deriv_pos (convex_Icc a b)\n  · exact fun T hT => (hF T hT).continuousAt.continuousWithinAt\n  · intro T hT; rw [(hF T (interior_subset hT)).deriv]; exact hf T (interior_subset hT)\n\nlemma lin_deriv (F : ℝ → ℝ) (d c T : ℝ) (h : HasDerivAt F d T) :\n    HasDerivAt (fun y => F y + c*y) (d + c) T :=\n  (h.add ((hasDerivAt_id T).const_mul c)).congr_deriv (by simp)\n\n/-- Taylor-polynomial upper bound for exp on [0,4] (from `Real.exp_bound`, 7 terms, applied to x/4 and raised to the 4th power) -/\nnoncomputable def Q (x : ℝ) : ℝ := ((∑ m ∈ Finset.range 7, (x/4)^m / (m.factorial:ℝ)) + (x/4)^7 * (8/35280))^4\n\nlemma exp_ub (x : ℝ) (h0 : 0 ≤ x) (h4 : x ≤ 4) : Real.exp x ≤ Q x := by\n  have hy : |x/4| ≤ 1 := by rw [abs_le]; constructor <;> linarith\n  have hb := Real.exp_bound hy (n := 7) (by norm_num)\n  rw [abs_of_nonneg (by linarith : 0 ≤ x/4)] at hb\n  have h1 := (abs_le.mp hb).2\n  have e : ((Nat.succ 7 : ℕ) : ℝ) / ((Nat.factorial 7 : ℝ) * (7:ℕ)) = 8/35280 := by norm_num [Nat.factorial]\n  rw [e] at h1\n  have e2 : Real.exp x = Real.exp (x/4) ^ 4 := by\n    rw [← Real.exp_nat_mul]; congr 1; push_cast; ring\n  rw [e2]; exact pow_le_pow_left₀ (Real.exp_pos _).le (by linarith) 4\nE\necho ok"
}
Bash output
ok
Shell cwd was reset to /work
Bash input
{
  "command": "cd /tmp/lw; cat > P2.lean <<'E'\n\n/-! ### Laliberté (2009) NaCl correlation: empirical inputs (exposed) and derived consequences -/\nnoncomputable def Mnacl : ℝ := 58.44\nnoncomputable def a1 : ℝ := -0.0693559668993322\nnoncomputable def a2 : ℝ := -0.0782134167486952\nnoncomputable def a3 : ℝ := 3.84798479408635\nnoncomputable def a4 : ℝ := -11.2762109247072\nnoncomputable def a5 : ℝ := 8.73187698542672\nnoncomputable def a6 : ℝ := 1.81245930472755\n\n/-- apparent molar heat capacity of NaCl in aqueous solution, J/(K mol): Mnacl * (Laliberté solute heat capacity\n    Cp_i in J/(g K)); w = salt mass fraction, T in C -/\nnoncomputable def PhiC (w T : ℝ) : ℝ :=\n  Mnacl * (a1 * Real.exp (a2*T + a3*Real.exp (0.01*T) + a4*w) + a5 * w ^ a6)\n\n/-- Laliberté's mixing rule Cp_sol = w_w Cp_w + w_s Cp_i gives C_solution - C_water = ms*Cp_i = (ms/M)*(M*Cp_i):\n    per mole of salt the heat-capacity change is M*Cp_i = PhiC. -/\nlemma apparent_capacity (ms mw M cpw cpi : ℝ) (hM : 0 < M) (hs : 0 < ms + mw) :\n    (ms+mw) * ((mw/(ms+mw)) * cpw + (ms/(ms+mw)) * cpi) - mw * cpw = (ms/M) * (M * cpi) := by\n  field_simp; ring\n\nlemma phi_mono_w (w T : ℝ) (h : 1/51 ≤ w) : PhiC (1/51) T ≤ PhiC w T := by\n  unfold PhiC\n  have h1 : Real.exp (a2*T + a3*Real.exp (0.01*T) + a4*w) ≤ Real.exp (a2*T + a3*Real.exp (0.01*T) + a4*(1/51)) :=\n    Real.exp_le_exp.mpr (by unfold a4; linarith)\n  have h2 : (1/51:ℝ) ^ a6 ≤ w ^ a6 := Real.rpow_le_rpow (by norm_num) h (by unfold a6; norm_num)\n  have ha1 : a1 < 0 := by unfold a1; norm_num\n  have ha5 : 0 < a5 := by unfold a5; norm_num\n  have hM : 0 < Mnacl := by unfold Mnacl; norm_num\n  exact mul_le_mul_of_nonneg_left (by nlinarith) hM.le\n\nlemma phi_neg (w T : ℝ) (h1 : 1/51 ≤ w) (h2 : w ≤ 1/21) (t1 : 10 ≤ T) (t2 : T ≤ 101) : PhiC w T < 0 := by\n  unfold PhiC\n  have hw : 0 < w := by linarith\n  have e1 : w ^ a6 ≤ w ^ ((3:ℝ)/2) :=\n    Real.rpow_le_rpow_of_exponent_ge hw (by linarith) (by unfold a6; norm_num)\n  have e2 : w ^ ((3:ℝ)/2) = w * Real.sqrt w := by\n    rw [Real.sqrt_eq_rpow, show ((3:ℝ)/2) = 1 + 1/2 by norm_num, Real.rpow_add hw, Real.rpow_one]\n  have e3 : Real.sqrt w ≤ 1/4 := by rw [Real.sqrt_le_left (by norm_num)]; nlinarith\n  have e4 : w ^ a6 ≤ 1/84 := by have := Real.sqrt_nonneg w; nlinarith\n  have q := Real.quadratic_le_exp_of_nonneg (x := 0.01*T) (by linarith)\n  set E := Real.exp (0.01*T)\n  have hα : 1.2 ≤ a2*T + a3*E + a4*w := by\n    unfold a2 a3 a4\n    nlinarith [mul_nonneg (by linarith : (0:ℝ) ≤ 101 - T) (by linarith : (0:ℝ) ≤ T - 10)]\n  have hex : 2 ≤ Real.exp (a2*T + a3*E + a4*w) := by linarith [Real.add_one_le_exp (a2*T + a3*E + a4*w)]\n  have hM : 0 < Mnacl := by unfold Mnacl; norm_num\n  refine mul_neg_of_pos_of_neg hM ?_\n  unfold a1 a5 at *\n  nlinarith\n\n/-- Φ_C is concave in T: its value at T lies above the chord between any a < b. -/\nlemma phi_chord (w a b T : ℝ) (hab : a < b) (h1 : a ≤ T) (h2 : T ≤ b) :\n    (b-a) * PhiC w T ≥ (b-T) * PhiC w a + (T-a) * PhiC w b := by\n  set α : ℝ → ℝ := fun s => a2*s + a3*Real.exp (0.01*s) + a4*w with hαdef\n  have ha3 : 0 < a3 := by unfold a3; norm_num\n  have tang : ∀ s, Real.exp (α s) ≥ Real.exp (α T) * (1 + (a2 + a3*0.01*Real.exp (0.01*T)) * (s - T)) := by\n    intro s\n    have e1 : Real.exp (0.01*s) ≥ Real.exp (0.01*T) * (1 + 0.01*(s-T)) := by\n      have : Real.exp (0.01*s) = Real.exp (0.01*T) * Real.exp (0.01*(s-T)) := by\n        rw [← Real.exp_add]; congr 1; ring\n      rw [this]\n      nlinarith [Real.add_one_le_exp (0.01*(s-T)), Real.exp_pos (0.01*T)]\n    have e2 : α s - α T ≥ (a2 + a3*0.01*Real.exp (0.01*T)) * (s - T) := by\n      simp only [hαdef]; nlinarith\n    have e3 : Real.exp (α s) = Real.exp (α T) * Real.exp (α s - α T) := by\n      rw [← Real.exp_add]; congr 1; ring\n    rw [e3]\n    nlinarith [Real.add_one_le_exp (α s - α T), Real.exp_pos (α T)]\n  have ta := tang a\n  have tb := tang b\n  have hg : (b-a) * Real.exp (α T) ≤ (b-T) * Real.exp (α a) + (T-a) * Real.exp (α b) := by\n    nlinarith [mul_le_mul_of_nonneg_left ta (by linarith : (0:ℝ) ≤ b - T),\n               mul_le_mul_of_nonneg_left tb (by linarith : (0:ℝ) ≤ T - a)]\n  have ha1 : a1 < 0 := by unfold a1; norm_num\n  have hM : 0 < Mnacl := by unfold Mnacl; norm_num\n  unfold PhiC\n  have : (b-a) * (a1 * Real.exp (α T)) ≥ (b-T) * (a1 * Real.exp (α a)) + (T-a) * (a1 * Real.exp (α b)) := by\n    nlinarith\n  simp only [hαdef] at this\n  nlinarith [mul_pos hM (by linarith : (0:ℝ) < b - a)]\n\n/-- trapezoid lower bound: if F' = f lies above its chord on [a,b] then F b - F a ≥ (b-a)(f a + f b)/2 -/\nlemma trap (F f : ℝ → ℝ) (a b : ℝ) (hab : a < b)\n    (hF : ∀ T ∈ Set.Icc a b, HasDerivAt F (f T) T)\n    (hc : ∀ T ∈ Set.Icc a b, (b-a) * f T ≥ (b-T) * f a + (T-a) * f b) :\n    (b-a)*(f a + f b)/2 ≤ F b - F a := by\n  have hba : 0 < b - a := by linarith\n  have hd : ∀ T ∈ Set.Icc a b, HasDerivAt\n      (fun T => F T - F a - (f a * (T-a) + (T-a)^2 * (f b - f a) / (2*(b-a)))) (f T - ((b-T) * f a + (T-a) * f b)/(b-a)) T := by\n    intro T hT\n    have u : HasDerivAt (fun T : ℝ => T - a) 1 T := (hasDerivAt_id T).sub_const a\n    have v := (u.const_mul (f a)).add (((u.pow 2).mul_const (f b - f a)).div_const (2*(b-a)))\n    convert (hF T hT).sub_const (F a) |>.sub v using 1\n    field_simp; ring\n  have := dmono _ _ a b hd (fun T hT => by\n    have := hc T hT\n    rw [sub_nonneg, div_le_iff₀ hba]; linarith) ⟨le_rfl, hab.le⟩ ⟨hab.le, le_rfl⟩ hab.le\n  simp only at this\n  have e : (f a * (b-a) + (b-a)^2 * (f b - f a) / (2*(b-a))) = (b-a)*(f a + f b)/2 := by\n    field_simp; ring\n  simp at this\n  nlinarith [e]\n\nlemma node (w T V β U L : ℝ) (h : 1/51 ≤ w)\n    (hh : 0 ≤ T ∧ T ≤ 400 ∧ Q (0.01*T) ≤ V ∧ a2*T + a3*V + a4*(1/51) ≤ β ∧ 0 ≤ β ∧ β ≤ 4 ∧ Q β ≤ U ∧\n      -L ≤ Mnacl*a1*U) : -L ≤ PhiC w T := by\n  obtain ⟨h0, h4, hV, hb, hb0, hb4, hU, hL⟩ := hh\n  refine le_trans ?_ (phi_mono_w w T h)\n  have e1 : Real.exp (0.01*T) ≤ V := (exp_ub _ (by linarith) (by linarith)).trans hV\n  have ha3 : 0 < a3 := by unfold a3; norm_num\n  have e2 : Real.exp (a2*T + a3*Real.exp (0.01*T) + a4*(1/51)) ≤ U :=\n    (Real.exp_le_exp.mpr (by nlinarith [mul_le_mul_of_nonneg_left e1 ha3.le])).trans ((exp_ub β hb0 hb4).trans hU)\n  have h5 : 0 ≤ a5 * (1/51:ℝ)^a6 := mul_nonneg (by unfold a5; norm_num) (Real.rpow_nonneg (by norm_num) _)\n  have ha1 : a1 < 0 := by unfold a1; norm_num\n  have hM : 0 < Mnacl := by unfold Mnacl; norm_num\n  unfold PhiC\n  nlinarith [mul_le_mul_of_nonneg_left e2 (mul_pos hM (neg_pos.mpr ha1)).le, mul_nonneg hM.le h5]\nE\npython3 - <<'E'\nout=__import__('json').load(open('/tmp/lab/nodes.json'))\ntac=\"(by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1])\"\nparts=[];calls=[]\nfor (T,V,b,U,L,t) in out:\n    parts.append(f\"-({L}:ℝ) ≤ PhiC w {T}\")\n    calls.append(f\"node w {T} {V} {b} {U} {L} h {tac}\")\ns=\"\\nlemma nodes (w : ℝ) (h : 1/51 ≤ w) : \"+\" ∧ \".join(parts)+\" :=\\n  ⟨\"+\",\\n   \".join(calls)+\"⟩\\n\"\nopen('/tmp/lw/P3.lean','w').write(s)\nE\ncat P1.lean P2.lean P3.lean > T1.lean; cd /tmp; bash /tmp/scr/lc.sh /tmp/lw/T1.lean 60 | grep -v -E \"push_neg|^If you|^open|^macro|^\\`\\`\\`|^  \\`\\(tactic|^$|Hint|Note\""
}
Bash output
/tmp/lw/T1.lean:98:31: warning: Variable name `hab` is not explicitly referenced.
  [apply] _hab
Shell cwd was reset to /work
Bash input
{
  "command": "cd /tmp/lw; python3 - <<'E'\nimport json\nout=json.load(open('/tmp/lab/nodes.json'))\nnn=[o[0] for o in out if o[0]>=20]\ntail=\"  obtain ⟨n10, \"+\", \".join(f\"n{o[0]}\" for o in out[1:])+\"⟩ := nodes w h1\\n\"\nfor i in range(len(nn)-1):\n    a,b=nn[i],nn[i+1]\n    tail+=f\"  have p{i} := piece w Ph {a} {b} _ _ (by norm_num) (by norm_num) (by norm_num) hPh n{a} n{b}\\n\"\ntail+=\"  norm_num at *\\n  linarith\\n\"\nopen('tail.txt','w').write(tail)\nE\ncat > P4a.lean <<'E'\n\nlemma piece (w : ℝ) (Ph : ℝ → ℝ) (a b La Lb : ℝ) (hab : a < b) (ha : 10 ≤ a) (hb : b ≤ 101)\n    (hPh : ∀ T ∈ Set.Icc (10:ℝ) 101, HasDerivAt Ph (PhiC w T) T)\n    (hA : -La ≤ PhiC w a) (hB : -Lb ≤ PhiC w b) : -((b-a)*(La+Lb)/2) ≤ Ph b - Ph a := by\n  have := trap Ph (PhiC w) a b hab (fun T hT => hPh T ⟨by linarith [hT.1], by linarith [hT.2]⟩)\n    (fun T hT => phi_chord w a b T hab hT.1 hT.2)\n  nlinarith\n\nlemma phi_floor (w T : ℝ) (h1 : 1/51 ≤ w) (t1 : 10 ≤ T) (t2 : T ≤ 101) : -105 ≤ PhiC w T := by\n  have c := phi_chord w 10 101 T (by norm_num) t1 t2\n  obtain ⟨n10, -, -, -, -, -, -, -, -, -, n101⟩ := nodes w h1\n  nlinarith [mul_nonneg (by linarith : (0:ℝ) ≤ 101 - T) (by linarith : (0:ℝ) ≤ 105 - 104.51),\n             mul_nonneg (by linarith : (0:ℝ) ≤ T - 10) (by linarith : (0:ℝ) ≤ 46.73 - 0)]\n\n/-- Total change of apparent molar enthalpy of NaCl from 20 C to 101 C is at least -3363 J/mol. -/\nlemma ph_drop (w : ℝ) (Ph : ℝ → ℝ) (h1 : 1/51 ≤ w)\n    (hPh : ∀ T ∈ Set.Icc (10:ℝ) 101, HasDerivAt Ph (PhiC w T) T) : -3363 ≤ Ph 101 - Ph 20 := by\nE\ncat > P4b.lean <<'E'\n\n/-! ## Heating-time model (accepted Newtonian lumped model; state variable = total enthalpy H) -/\n\n/-- Time to reach the boiling point when salt is added while the pure water is at Ta: pure-water branch up to\n    total enthalpy Hw Ta, then solution branch from the same enthalpy up to Hs Tb.\n    τw, τs = elapsed time as a function of total enthalpy on the two branches. -/\ndef boilTime (τw τs Hw Hs : ℝ → ℝ) (T0 Tb Ta : ℝ) : ℝ :=\n  (τw (Hw Ta) - τw (Hw T0)) + (τs (Hs Tb) - τs (Hw Ta))\n\ntheorem timing_core (P h Tr Tl Tb : ℝ) (Hw Hs Tw Ts τw τs : ℝ → ℝ)\n    (hh : 0 ≤ h) (hP : h*(Tb-Tr) < P) (hl : Tl ≤ Tr) (hr : Tr ≤ Tb)\n    (hHw : StrictMonoOn Hw (Set.Icc Tl Tb)) (hHs : StrictMonoOn Hs (Set.Icc Tl Tb))\n    (hTw : ∀ H ∈ Set.Icc (Hw Tr) (Hw Tb), Tw H ∈ Set.Icc Tl Tb ∧ Hw (Tw H) = H)\n    (hTs : ∀ H ∈ Set.Icc (Hs Tl) (Hs Tb), Ts H ∈ Set.Icc Tl Tb ∧ Hs (Ts H) = H)\n    (hτw : ∀ H ∈ Set.Icc (Hw Tr) (Hw Tb), HasDerivAt τw (1/(P - h*(Tw H - Tr))) H)\n    (hτs : ∀ H ∈ Set.Icc (Hs Tl) (Hs Tb), HasDerivAt τs (1/(P - h*(Ts H - Tr))) H)\n    (hcool : ∀ x ∈ Set.Icc Tr Tb, Hw x ≤ Hs x) (hlow : Hs Tl ≤ Hw Tr) :\n    ∀ Ta ∈ Set.Icc Tr Tb, boilTime τw τs Hw Hs Tr Tb Tr ≤ boilTime τw τs Hw Hs Tr Tb Ta := by\n  intro Ta hTa\n  have hTrI : Tr ∈ Set.Icc Tl Tb := ⟨hl, hr⟩\n  have hTaI : Ta ∈ Set.Icc Tl Tb := ⟨hl.trans hTa.1, hTa.2⟩\n  have hTbI : Tb ∈ Set.Icc Tl Tb := ⟨hl.trans hr, le_rfl⟩\n  have w1 : Hw Tr ≤ Hw Ta := hHw.monotoneOn hTrI hTaI hTa.1\n  have w2 : Hw Ta ≤ Hw Tb := hHw.monotoneOn hTaI hTbI hTa.2\n  have s1 : Hw Tb ≤ Hs Tb := hcool Tb ⟨hr, le_rfl⟩\n  have hpos : ∀ x, x ≤ Tb → 0 < P - h*(x - Tr) := by\n    intro x hx\n    have : h*(x-Tr) ≤ h*(Tb-Tr) := mul_le_mul_of_nonneg_left (by linarith) hh\n    linarith\n  have key : ∀ H ∈ Set.Icc (Hw Tr) (Hw Ta), 1/(P - h*(Tw H - Tr)) - 1/(P - h*(Ts H - Tr)) ≥ 0 := by\n    intro H hH\n    obtain ⟨hx, hxH⟩ := hTw H ⟨hH.1, hH.2.trans w2⟩\n    obtain ⟨hy, hyH⟩ := hTs H ⟨hlow.trans hH.1, hH.2.trans (w2.trans s1)⟩\n    have hxr : Tr ≤ Tw H := by\n      by_contra hc\n      have := hHw hx hTrI (not_le.mp hc)\n      rw [hxH] at this\n      linarith [hH.1]\n    have hc := hcool _ ⟨hxr, hx.2⟩\n    rw [hxH] at hc\n    have hyx : Ts H ≤ Tw H := by\n      by_contra hcon\n      have := hHs hx hy (not_le.mp hcon)\n      rw [hyH] at this\n      linarith\n    have := mul_le_mul_of_nonneg_left (by linarith : Ts H - Tr ≤ Tw H - Tr) hh\n    have := one_div_le_one_div_of_le (hpos _ hx.2) (by linarith : P - h*(Tw H - Tr) ≤ P - h*(Ts H - Tr))\n    linarith\n  have mono := dmono (fun H => τw H - τs H) _ (Hw Tr) (Hw Ta)\n    (fun H hH => (hτw H ⟨hH.1, hH.2.trans w2⟩).sub (hτs H ⟨hlow.trans hH.1, hH.2.trans (w2.trans s1)⟩)) key\n  have := mono ⟨le_rfl, w1⟩ ⟨w1, le_rfl⟩ w1\n  simp only [boilTime] at this ⊢\n  linarith\n\n/-- sanity: the inverse temperature functions Tw, Ts exist (intermediate value theorem). -/\nlemma inverse_exists (H : ℝ → ℝ) (Tl Tb : ℝ) (hle : Tl ≤ Tb) (hc : ContinuousOn H (Set.Icc Tl Tb)) :\n    ∀ y ∈ Set.Icc (H Tl) (H Tb), ∃ T ∈ Set.Icc Tl Tb, H T = y := fun y hy => by\n  obtain ⟨T, hT, e⟩ := intermediate_value_Icc hle hc hy\n  exact ⟨T, hT, e⟩\nE\necho done"
}
Bash output
done
Shell cwd was reset to /work
Bash input
{
  "command": "cd /tmp/lw; cat > P4c.lean <<'E'\n\n/-! ## Thermodynamics of dissolving NaCl(cr) (at 20 C) in the pot -/\n\n/-- thermochemical calorie in J (Parker 1965 reports cal) -/\nnoncomputable def cal : ℝ := 4.184\n\n/-- moles of NaCl for salt/water mass ratio r, in the 0.998 kg of water of 1 L at 20 C -/\nnoncomputable def nSalt (r : ℝ) : ℝ := 1000*0.998*r/Mnacl\n\n/-- Enthalpy (J) of pot+solution at T, measured from pot+pure water plus crystalline NaCl at 20 C.\n    Definition of the apparent molar enthalpy Ph of NaCl in the final solution: solution = (same water at T) + n*Ph(T);\n    the salt started as crystal with enthalpy Hcr 20. -/\ndef solEnthalpy (n : ℝ) (Hw Ph Hcr : ℝ → ℝ) (T : ℝ) : ℝ := Hw T + n * (Ph T - Hcr 20)\n\n/-- Physical premises (empirical data and definitions), see the review packet. -/\nstructure Premises (r : ℝ) (Cw Hw Ph Hcr Cpcr : ℝ → ℝ) (hinf phiL : ℝ) : Prop where\n  r_lo : 1/50 ≤ r\n  r_hi : r ≤ 1/20\n  hw_deriv : ∀ T ∈ Set.Icc (10:ℝ) 101, HasDerivAt Hw (Cw T) T\n  hw_cap : ∀ T ∈ Set.Icc (10:ℝ) 101, 4100 ≤ Cw T\n  ph_deriv : ∀ T ∈ Set.Icc (10:ℝ) 101, HasDerivAt Ph (PhiC (r/(1+r)) T) T\n  cr_deriv : ∀ T ∈ Set.Icc (20:ℝ) 25, HasDerivAt Hcr (Cpcr T) T\n  cr_cap : ∀ T ∈ Set.Icc (20:ℝ) 25, 40 ≤ Cpcr T ∧ Cpcr T ≤ 100\n  hinf_b : 923*cal ≤ hinf ∧ hinf ≤ 933*cal\n  phiL_b : -41*cal ≤ phiL ∧ phiL ≤ 100*cal\n  diss25 : Ph 25 - Hcr 25 = hinf + phiL\n\nlemma w_range (r : ℝ) (h1 : 1/50 ≤ r) (h2 : r ≤ 1/20) : 1/51 ≤ r/(1+r) ∧ r/(1+r) ≤ 1/21 := by\n  constructor\n  · rw [le_div_iff₀ (by linarith)]; linarith\n  · rw [div_le_iff₀ (by linarith)]; linarith\n\nlemma n_range (r : ℝ) (h1 : 1/50 ≤ r) (h2 : r ≤ 1/20) : 0.34 ≤ nSalt r ∧ nSalt r ≤ 0.86 := by\n  unfold nSalt Mnacl\n  constructor\n  · rw [le_div_iff₀ (by norm_num)]; linarith\n  · rw [div_le_iff₀ (by norm_num)]; linarith\n\ntheorem thermo_facts (r Tb : ℝ) (Cw Hw Ph Hcr Cpcr : ℝ → ℝ) (hinf phiL : ℝ)\n    (hp : Premises r Cw Hw Ph Hcr Cpcr hinf phiL) (hT2 : Tb ≤ 101) :\n    StrictMonoOn Hw (Set.Icc 10 Tb) ∧ StrictMonoOn (solEnthalpy (nSalt r) Hw Ph Hcr) (Set.Icc 10 Tb) ∧\n    (∀ x ∈ Set.Icc (20:ℝ) Tb, Hw x ≤ solEnthalpy (nSalt r) Hw Ph Hcr x) ∧\n    solEnthalpy (nSalt r) Hw Ph Hcr 10 ≤ Hw 20 := by\n  obtain ⟨hw1, hw2⟩ := w_range r hp.r_lo hp.r_hi\n  obtain ⟨hn1, hn2⟩ := n_range r hp.r_lo hp.r_hi\n  set w := r/(1+r)\n  set n := nSalt r\n  have hs1 : ∀ T ∈ Set.Icc (10:ℝ) Tb, T ∈ Set.Icc (10:ℝ) 101 := fun T hT => ⟨hT.1, hT.2.trans hT2⟩\n  have hs2 : ∀ T ∈ Set.Icc (20:ℝ) 25, T ∈ Set.Icc (10:ℝ) 101 := fun T hT => ⟨by linarith [hT.1], by linarith [hT.2]⟩\n  have hs3 : ∀ T ∈ Set.Icc (10:ℝ) 20, T ∈ Set.Icc (10:ℝ) 101 := fun T hT => ⟨hT.1, by linarith [hT.2]⟩\n  have hfl : ∀ T ∈ Set.Icc (10:ℝ) 101, -105 ≤ PhiC w T := fun T hT => phi_floor w T hw1 hT.1 hT.2\n  have hng : ∀ T ∈ Set.Icc (10:ℝ) 101, PhiC w T ≤ 0 := fun T hT => (phi_neg w T hw1 hw2 hT.1 hT.2).le\n  have hWs := dstrict Hw Cw 10 Tb (fun T hT => hp.hw_deriv T (hs1 T hT))\n    (fun T hT => by linarith [hp.hw_cap T (hs1 T hT)])\n  have hHs : ∀ T ∈ Set.Icc (10:ℝ) 101, HasDerivAt (solEnthalpy n Hw Ph Hcr) (Cw T + n * PhiC w T) T :=\n    fun T hT => (hp.hw_deriv T hT).add (((hp.ph_deriv T hT).sub_const (Hcr 20)).const_mul n)\n  have hSs := dstrict _ _ 10 Tb (fun T hT => hHs T (hs1 T hT))\n    (fun T hT => by nlinarith [hp.hw_cap T (hs1 T hT), hfl T (hs1 T hT)])\n  -- Kirchhoff: dissolution enthalpy Ph T - Hcr T between 20 and 25 C\n  have hd : ∀ T ∈ Set.Icc (20:ℝ) 25, HasDerivAt (fun T => Ph T - Hcr T) (PhiC w T - Cpcr T) T :=\n    fun T hT => (hp.ph_deriv T (hs2 T hT)).sub (hp.cr_deriv T hT)\n  have hlow20 := danti _ _ 20 25 (fun T hT => lin_deriv _ _ 40 T (hd T hT))\n    (fun T hT => by linarith [hng T (hs2 T hT), (hp.cr_cap T hT).1]) ⟨le_rfl, by norm_num⟩ ⟨by norm_num, le_rfl⟩ (by norm_num)\n  have hup20 := dmono _ _ 20 25 (fun T hT => lin_deriv _ _ 205 T (hd T hT))\n    (fun T hT => by linarith [hfl T (hs2 T hT), (hp.cr_cap T hT).2]) ⟨le_rfl, by norm_num⟩ ⟨by norm_num, le_rfl⟩ (by norm_num)\n  have hup10 := dmono _ _ 10 20 (fun T hT => lin_deriv _ _ 105 T (hp.ph_deriv T (hs3 T hT)))\n    (fun T hT => by linarith [hfl T (hs3 T hT)]) ⟨le_rfl, by norm_num⟩ ⟨by norm_num, le_rfl⟩ (by norm_num)\n  have hup4 := dmono _ _ 10 20 (fun T hT => lin_deriv _ _ (-4100) T (hp.hw_deriv T (hs3 T hT)))\n    (fun T hT => by linarith [hp.hw_cap T (hs3 T hT)]) ⟨le_rfl, by norm_num⟩ ⟨by norm_num, le_rfl⟩ (by norm_num)\n  simp only at hlow20 hup20 hup10 hup4\n  -- Parker's numbers: dissolution enthalpy at 20 C\n  obtain ⟨hi1, hi2⟩ := hp.hinf_b\n  obtain ⟨hl1, hl2⟩ := hp.phiL_b\n  have hcal : cal = 4.184 := rfl\n  rw [hcal] at hi1 hi2 hl1 hl2\n  have hd25 := hp.diss25\n  refine ⟨hWs, hSs, fun x hx => ?_, ?_⟩\n  · have hanti2 := danti Ph _ 10 101 hp.ph_deriv hng\n    have h1 : Ph 101 ≤ Ph x := hanti2 ⟨by linarith [hx.1], by linarith [hx.2]⟩ ⟨by norm_num, le_rfl⟩ (by linarith [hx.2])\n    have h2 := ph_drop w Ph hw1 hp.ph_deriv\n    unfold solEnthalpy\n    nlinarith\n  · unfold solEnthalpy\n    nlinarith\nE\ncat > P5.lean <<'E'\n\n/-! ## Conclusions -/\n\n/-- CONCLUSION 1 (heat capacity). For 20-50 g NaCl per kg water and 10-101 C the apparent molar heat capacity of NaCl\n    in solution is negative, in (-105, 0) J/(K mol) up to the floor; dissolving the salt LOWERS the total heat capacity\n    of the pot contents by n*Phi in (-91, 0) J/K (< 2.2 % of the ~4.2 kJ/K of the water). -/\ntheorem cs_sign : ∀ (r T : ℝ), 1/50 ≤ r → r ≤ 1/20 → 10 ≤ T → T ≤ 101 →\n    -105 ≤ PhiC (r/(1+r)) T ∧ PhiC (r/(1+r)) T < 0 ∧\n    -91 < nSalt r * PhiC (r/(1+r)) T ∧ nSalt r * PhiC (r/(1+r)) T < 0 := by\n  intro r T h1 h2 t1 t2\n  obtain ⟨w1, w2⟩ := w_range r h1 h2\n  obtain ⟨n1, n2⟩ := n_range r h1 h2\n  have f := phi_floor _ T w1 t1 t2\n  have g := phi_neg _ T w1 w2 t1 t2\n  refine ⟨f, g, ?_, ?_⟩ <;> nlinarith\n\n/-- CONCLUSION 2 (answer). Salt added at the very start (water at 20 C) gives a boiling time no longer than salt added\n    once the water has reached any later temperature Ta <= Tb. -/\ntheorem answer_theorem : ∀ (r P h Tb : ℝ) (Cw Hw Ph Hcr Cpcr Tw Ts τw τs : ℝ → ℝ) (hinf phiL : ℝ),\n    Premises r Cw Hw Ph Hcr Cpcr hinf phiL → 20 ≤ Tb → Tb ≤ 101 → 0 ≤ h → h*(Tb-20) < P →\n    (∀ H ∈ Set.Icc (Hw 20) (Hw Tb), Tw H ∈ Set.Icc 10 Tb ∧ Hw (Tw H) = H) →\n    (∀ H ∈ Set.Icc (solEnthalpy (nSalt r) Hw Ph Hcr 10) (solEnthalpy (nSalt r) Hw Ph Hcr Tb),\n        Ts H ∈ Set.Icc 10 Tb ∧ solEnthalpy (nSalt r) Hw Ph Hcr (Ts H) = H) →\n    (∀ H ∈ Set.Icc (Hw 20) (Hw Tb), HasDerivAt τw (1/(P - h*(Tw H - 20))) H) →\n    (∀ H ∈ Set.Icc (solEnthalpy (nSalt r) Hw Ph Hcr 10) (solEnthalpy (nSalt r) Hw Ph Hcr Tb),\n        HasDerivAt τs (1/(P - h*(Ts H - 20))) H) →\n    ∀ Ta ∈ Set.Icc (20:ℝ) Tb,\n      boilTime τw τs Hw (solEnthalpy (nSalt r) Hw Ph Hcr) 20 Tb 20\n        ≤ boilTime τw τs Hw (solEnthalpy (nSalt r) Hw Ph Hcr) 20 Tb Ta := by\n  intro r P h Tb Cw Hw Ph Hcr Cpcr Tw Ts τw τs hinf phiL hp hT1 hT2 hh hP hTw hTs hτw hτs\n  obtain ⟨m1, m2, c1, c2⟩ := thermo_facts r Tb Cw Hw Ph Hcr Cpcr hinf phiL hp hT2\n  exact timing_core P h 20 10 Tb Hw (solEnthalpy (nSalt r) Hw Ph Hcr) Tw Ts τw τs hh hP (by norm_num) hT1\n    m1 m2 hTw hTs hτw hτs c1 c2\nE\ncat P1.lean P2.lean P3.lean P4a.lean tail.txt P4b.lean P4c.lean P5.lean > T2.lean; cd /tmp; bash /tmp/scr/lc.sh /tmp/lw/T2.lean 80 | grep -v -E \"push_neg|^If you|^open|^macro|^\\`\\`\\`|^  \\`\\(tactic|^$|Hint|Note\"; python3 - <<'E'\nimport re\ns=open('/tmp/lw/T2.lean').read()\ns=re.sub(r'/-.*?-/','',s,flags=re.S); s=re.sub(r'--[^\\n]*','',s)\nprint(len(s.encode()))\nE"
}
Bash output
/tmp/lw/T2.lean:98:31: warning: Variable name `hab` is not explicitly referenced.
  [apply] _hab
20987
Shell cwd was reset to /work
Bash input
{
  "command": "cd /work; python3 - <<'E'\nbody=open('/tmp/lw/T2.lean').read()\nhdr=open('/work/attempts/cycle5_secant/Model.lean').read()\n# reuse review packet text from the earlier generated Model.lean (current /work/Model.lean has it)\ncur=open('/work/Model.lean').read()\nstart=cur.index(\"/-! # Salt timing\"); end=cur.index(\"-/\\n\",start)+3\npacket=cur[start:end]\npacket=packet.replace(\"(`inverse_exists`)\",\"(`inverse_exists`)\")\nbody=body.replace(\"/-! (header review packet inserted by script) -/\",packet)\nopen('/work/Model.lean','w').write(body)\nE\ngrep -c \"sorry\" Model.lean; sed -n 1,50p Model.lean | head -60"
}
Bash output
0
import Mathlib.Analysis.SpecialFunctions.Log.Deriv
import Mathlib.Analysis.SpecialFunctions.Pow.Real
import Mathlib.Analysis.SpecialFunctions.ExpDeriv
import Mathlib.Analysis.Calculus.MeanValue

/-! # Salt timing for 1 L of water at 20 C: when should NaCl be added? (cycle 6)

## REVIEW PACKET
SUPPLIED BY THE USER: 1 L water at 20 C in a pot on a stove; salt will be added; goal = salted water reaches
its boiling point soonest; salt amount, pot, heater, room unspecified.

ACCEPTED EARLIER (kept): lumped Newtonian heating dH/dt = P - h (T - 20), constant stove power P, no evaporation,
salt (crystal at 20 C) dissolves instantly at the moment of addition (total enthalpy conserved).

MODEL CHOICES / ASSUMPTIONS (all appear as `Premises` fields or theorem hypotheses):
 * NaCl; r = salt mass / water mass in [1/50, 1/20] (20-50 g in the 0.998 kg of water of 1 L); mass fraction
   w = r/(1+r) (conversion proved in `w_range`), n = 1000*0.998*r/58.44 mol (`nSalt`, bounds in `n_range`).
 * Pot+water enthalpy Hw(T) has heat capacity Cw(T) >= 4100 J/K on 10..101 C (0.998 kg water >= 4.17 kJ/K; pot adds).
 * Apparent molar heat capacity of NaCl in the final solution = Laliberte (2009) correlation `PhiC`
   (empirical coefficients a1..a6 from Laliberte, J. Chem. Eng. Data 54, 1725; fit range 1.5-120 C, w <= 0.26).
   Ph(T) is the apparent molar enthalpy of NaCl in that solution: dPh/dT = PhiC (definition of Phi_C).
   Solution enthalpy = (water at T) + n*Ph(T)   (definition of apparent molar enthalpy; `solEnthalpy`).
 * Crystal NaCl enthalpy Hcr with Cp_cr in [40,100] J/K/mol on 20..25 C (Archer 1992 Part I, Eqs 4-6: Cp(298.15)=50.16;
   my evaluation of the spline gives 50.04..50.16 on 20..25 C).
 * Parker NSRDS-NBS 2 (1965): Delta H_inf(25 C, NaCl(c) -> infinite dilution) = 928 +- 5 cal/mol (printed p.26);
   Phi_L(25 C, m) (Table X, p.50) is RELATIVE to infinite dilution: Ph(25) - Hcr(25) = Hinf + Phi_L.
   For the regime m = 17.1 r in [0.34, 0.86] mol/kg Table X gives Phi_L = 83, 49, 20, -41 cal/mol at m = .2775,
   .5551, .7401, 1.1101 and dPhi_L/dm^(1/2) < 0 for m >= .185, so Phi_L in [-41, 88]; premise used: [-41, 100] cal. cal = 4.184 J.
 * Boiling temperature Tb of the final solution: 20 <= Tb <= 101 C (1 atm, <= 4.8 wt%: ideal elevation <= ~0.9 K); stove
   beats losses at Tb; Tw, Ts = temperature as function of total enthalpy (inverse of Hw, Hs; existence by IVT,
   `inverse_exists`); tau_w, tau_s = elapsed time as function of enthalpy: d tau/dH = 1/(P - h (T(H) - 20)).

PROVED (not assumed): Phi_C < 0 and >= -105 J/(K mol) for all regime/temperatures (concavity + exp bounds, `cs_sign`);
 trapezoid enclosure Ph(101) - Ph(20) >= -3363 J/mol from 11 Taylor-bounded exp evaluations (`ph_drop`);
 dissolution at 20 C >= 3890 J/mol via Kirchhoff (`thermo_facts`); salt addition at any x in [20,Tb] cools
 (Hs(x) - Hw(x) >= 0), solution enthalpy strictly increasing, mixing at 20 C stays above 10 C; and the timing
 comparison (`timing_core`, `answer_theorem`).
CONCLUSIONS: `cs_sign`, `answer_theorem` (salting at the start is no slower than salting at any later water temperature).
-/


/-! ### calculus helpers -/
lemma dmono (F f : ℝ → ℝ) (a b : ℝ) (hF : ∀ T ∈ Set.Icc a b, HasDerivAt F (f T) T)
    (hf : ∀ T ∈ Set.Icc a b, 0 ≤ f T) : MonotoneOn F (Set.Icc a b) := by
  apply monotoneOn_of_deriv_nonneg (convex_Icc a b)
  · exact fun T hT => (hF T hT).continuousAt.continuousWithinAt
  · exact fun T hT => (hF T (interior_subset hT)).differentiableAt.differentiableWithinAt
  · intro T hT; rw [(hF T (interior_subset hT)).deriv]; exact hf T (interior_subset hT)

lemma danti (F f : ℝ → ℝ) (a b : ℝ) (hF : ∀ T ∈ Set.Icc a b, HasDerivAt F (f T) T)
Bash input
{
  "command": "cd /work; cat > report.txt <<'E'\nCYCLE 6 REVISION (response to the rejection of attempt 004: check the dissolution reference, compute Phi inside Lean, justify the reduction)\nLineage: attempts/cycle5_secant/ holds the previous Model.lean/report/candidate (secant-mean Phi bounds [-41,-27], hsol >= 3800 as premises). Superseded.\n\nWHAT CHANGED\n1. No secant average any more. The model keeps the full temperature dependence: pot+water enthalpy Hw(T) (any heat capacity Cw(T) >= 4100 J/K),\n   solution enthalpy Hs(T) = Hw(T) + n*(Ph(T) - Hcr(20)), where Ph is the apparent molar enthalpy of NaCl in the final solution with dPh/dT = Phi_C(T)\n   (definition of apparent molar heat capacity). Heating is the accepted Newtonian lumped model dH/dt = P - h(T-20) with total enthalpy H as state;\n   Tw(H), Ts(H) are the temperature on each branch, tau(H) the elapsed time (d tau/dH = 1/(P - h(T(H)-20))). The intermediate mixing inequality is proved for\n   EVERY addition temperature x in [20,Tb] (not at two endpoints): Hs(x) >= Hw(x), i.e. adding salt at x cools. The timing theorem (timing_core) shows that\n   whenever the solution is cooler than water at equal total enthalpy over [Hw(Ta1),Hw(Ta2)], adding at Ta1 is no slower.\n2. Phi_C is computed in Lean: the Laliberte (2009) NaCl solute heat-capacity correlation (coefficients exposed in Lean: a1..a6; Cp_i = a1 exp(a2 t + a3 exp(0.01 t) + a4 w)\n   + a5 w^a6, t in C, w = salt mass fraction; Phi_C = 58.44*Cp_i) is formalized as PhiC. Lean proves: Phi_C < 0, Phi_C >= -105 J/K/mol, concavity in T (chord\n   inequality, from tangent-line bounds on exp), monotonic dependence on w (worst case = lowest concentration), 11 rigorous exp enclosures (Taylor series of Real.exp_bound),\n   and via the trapezoid lemma (valid for concave Phi_C) Ph(101)-Ph(20) >= -3363 J/mol for the whole regime w in [1/51,1/21]. Python (stdlib only) suggested the node\n   values; Lean checks them (norm_num on explicit polynomial enclosures). Cs = n*Phi_C in (-91,0) J/K (cs_sign).\n   Coefficient provenance: values fetched from the Thermo/chemicals data file (CalebBell/chemicals, Laliberte2009.tsv) via a web fetch; my formula\n   (Cp_i = a1 e^alpha + a5 w^a6, alpha = a2 t + a3 e^{0.01 t} + a4 w with w = solute mass fraction) is the one SysCAD documents; I did not see the paper's own table.\n   Check performed (synthetic calculation, not a measurement): at 25 C, m = .2775/.5551/.7401 the formula gives -66.7/-55.3/-48.7 J/K/mol vs Parker Table II C\n   (-16.3/-13.4/-11.7 cal) = -68.2/-56.1/-49.0 (within 3%); at 20 C -77.6/-64.5/-56.8 vs Parker -73.2/-61.9/-52.7 (Laliberte is the more negative, i.e. the\n   conservative side for the cooling conclusion). Laliberte fit range 1.5-120 C. NOT checked against Archer Part II at 40-100 C (full EOS not implemented): the high-T\n   part of Phi_C rests on the Laliberte correlation alone.\n3. Dissolution enthalpy re-derived from the primary source (supplied bundle, images inspected): Parker (1965) printed p26: Delta H_inf(NaCl(c), 25 C, to infinite dilution)\n   = 928 +- 5 cal/mol (thermochemical cal, 1 cal = 4.184 J); Table X p50: Phi_L(25 C) = 83, 49, 20, -41 cal/mol at m = .2775, .5551, .7401, 1.1101 (relative to infinite dilution,\n   NOT a complete dissolution heat), with dPhi_L/dm^(1/2) < 0 for m >= .185 so Phi_L decreases monotonically through the regime m = 17.1*r in [0.34,0.86]:\n   Phi_L in [-41, 88]; the premise uses the wider [-41, 100] cal. Premise diss25: Ph(25)-Hcr(25) = Hinf + Phi_L (definitions). Move to 20 C by Kirchhoff, in Lean:\n   d/dT [Ph - Hcr] = Phi_C - Cp_cr; with Phi_C < 0 (proved) and Cp_cr in [40,100] J/K/mol (Archer Part I Eqs 4-6 spline, which I evaluated with my own natural-spline code:\n   50.04 at 293.15 K, 50.08 at 295 K, 50.16 at 298.15 K, reproducing Archer's Table 4) one gets dissolution enthalpy at 20 C >= (923-41)*4.184 + 5*40 = 3890 J/mol\n   (the old bare premise hsol>=3800 is gone; all earlier statements about 3.88 kJ/mol are now replaced by this chain). Heat absorbed (endothermic), the salt is a crystal at 20 C.\n   Best estimate (not used in proof): ~4.4-4.7 kJ/mol. The solid's own heat capacity enters only through the crude bounds above.\n4. Results proved in Lean: mixing energy n*(hsol20 + Ph(x)-Ph(20)) >= n*(3890-3363) > 0 for every x in [20,Tb], Tb <= 101: salt addition always cools (by roughly 0.5-1 K\n   at 20 C for 20-50 g); worst-case margin 527 J/mol of ~3900 (14%), typical ~1.2 kJ/mol. Hs strictly increasing; mixing at 20 C does not drop the solution below 10 C (only\n   crude upper bounds: hsol20 <= 5400 J/mol). answer_theorem: salting at the very start (water at 20 C) is no slower than at any later temperature Ta in [20,Tb].\n\nPHYSICAL ANSWER. Adding NaCl (20-50 g/L) to water at 20 C absorbs heat (NaCl dissolves endothermically, +4 to +4.7 kJ/mol at 20 C, increasing the cooling with the amount) and lowers the heat\ncapacity slightly (Phi_C ~ -75 ... -30 J/K/mol, total -10...-45 J/K, <1% of 4.2 kJ/K). Under the shared model, per mole of salt the energy deficit it causes never changes sign between 20 C and\nthe boiling point, so salt added earlier is \"heated\" through the water's own state more cheaply: add it immediately, i.e. at the start (20 C, no waiting). The size of the effect is small (a\nfraction of a kelvin to ~1 K, seconds to ~tens of seconds on a typical stove) and is not claimed numerically. Extra practical reasons for early salting: dissolving in boiling water\nfoams/boils over. Boiling elevation itself does not depend on when salt is added.\n\nLIMITS (human to review): Premises, not proved: Laliberte coefficients/formula (empirical; accuracy a few percent; high-T part unchecked vs Archer II), Parker values, Cw>=4100, Tb in [20,101] at ~1 atm\n(ideal elevation <= ~0.9 K for 4.8 wt%; the brine boiling point is not derived from pressure/composition), regime r in [0.02,0.05] only (at r <~ 0.01 Phi_L/Phi_C margins shrink, not claimed),\ninstant dissolution, no evaporation, constant stove power/Newton loss (accepted), existence of the time functions tau (antiderivatives of continuous integrands; Mathlib subset in the sandbox has no\ninterval integral, so time is characterized by its derivative). No physical measurement. Conclusion is qualitative; no time saving is stated.\nE\ncat > candidate.json <<'E'\n{\n \"proof_file\": \"Model.lean\",\n \"narrative_file\": \"report.txt\",\n \"conclusions\": {\"cs\": \"cs_sign\", \"answer\": \"answer_theorem\"},\n \"user_facts\": [\n  \"1 L of water at 20 C in a pot on a stove, heated\",\n  \"salt will be added; goal: salted water reaches its boiling point as soon as possible\",\n  \"salt quantity, pot, heater, room unspecified\"\n ],\n \"model_choices\": [\n  \"accepted Newtonian lumped heating model (total enthalpy state, constant stove power P, loss h(T-20), no evaporation); salt crystal at 20 C dissolves instantly, enthalpy conserved; time characterized by d tau/dH = 1/(P-h(T(H)-20))\",\n  \"NaCl; salt/water mass ratio r in [0.02,0.05] (w=r/(1+r), n = 1000*0.998*r/58.44 mol proved); 1 L = 0.998 kg\",\n  \"Phi_C(T,w) from the Laliberte 2009 NaCl correlation (coefficients exposed in Lean), full temperature dependence (no secant average); Ph = apparent molar enthalpy with dPh/dT = Phi_C; solution enthalpy = water + n*Ph\",\n  \"dissolution enthalpy at 20 C obtained in Lean by Kirchhoff from Parker 1965 (Delta H_inf = 928+-5 cal/mol, Phi_L in [-41,100] cal, 25 C) with crystal Cp in [40,100] J/K/mol (Archer 1992 Part I)\",\n  \"pot+water heat capacity >= 4100 J/K on 10-101 C; boiling temperature of the solution 20 <= Tb <= 101 C (~1 atm); stove stronger than losses at Tb\"\n ],\n \"sources\": [\n  \"Parker, NSRDS-NBS 2 (1965), DOI 10.6028/NBS.NSRDS.2: p26 (928 +- 5 cal/mol), Table X p50 (Phi_L), Table II C p43 (Phi_C cross-check) -- supplied bundle inputs/material-literature-002, pages inspected\",\n  \"Archer, JPCRD 21, 1 (1992) Part I, DOI 10.1063/1.555913, Eqs 4-6 and Table 3/4 (solid Cp; spline evaluated independently, reproduces 50.16 J/K/mol at 298.15 K)\",\n  \"Laliberte, J. Chem. Eng. Data 54, 1725 (2009), NaCl coefficients as listed in CalebBell/chemicals Laliberte2009.tsv (web fetch) with the model form as documented by SysCAD; cross-checked against Parker Table II C\",\n  \"Archer Part II (supplied) was NOT implemented\"\n ],\n \"incorporated_inputs\": [\"180d002e6c9a4035a8dddd93761649af\"],\n \"proof_scope\": \"cs_sign: for r in [0.02,0.05], T in [10,101] C, Laliberte Phi_C is in [-105,0) J/K/mol and n*Phi_C in (-91,0) J/K, proved in Lean from the explicit correlation (exp/rpow bounds). answer_theorem: under the Premises structure (Laliberte-based Phi_C, Parker dissolution data at 25 C, crystal Cp bounds, Cw>=4100, Tb<=101, Newtonian lumped heating with enthalpy-parametrized time functions) salt addition cools at every addition temperature in [20,Tb] (derived via Kirchhoff + concavity/trapezoid enclosure, margin >=527 J/mol), and adding salt at 20 C boils no later than adding at any later water temperature. Empirical coefficients, Parker data, Tb<=101 and the lumped model are premises, not proved or measured; the high-T Phi_C is not cross-checked against Archer Part II; no numerical time saving is claimed. Human review needed.\"\n}\nE\npython /harness/src/vcs_protocol.py tool check --manifest /work/candidate.json 2>&1 | tail -30"
}
Bash output
   "original_sha256": "59ba3095b97a20ff127ad55e617ab8a7f63d8f6af589d71a0b4a5846f41c56b4",
   "extracted_sha256": "59ba3095b97a20ff127ad55e617ab8a7f63d8f6af589d71a0b4a5846f41c56b4"
  },
  "nSalt._proof_1": {
   "original_sha256": "f952723ace8680854ad361cd5f02b3b25c9607d68dbd69a3860528a1ccd513a8",
   "extracted_sha256": "f952723ace8680854ad361cd5f02b3b25c9607d68dbd69a3860528a1ccd513a8"
  },
  "boilTime": {
   "original_sha256": "3b24a0658ee7cba7a28b6f19387f5006a887f6575ba0159017464657e20e572f",
   "extracted_sha256": "3b24a0658ee7cba7a28b6f19387f5006a887f6575ba0159017464657e20e572f"
  }
 },
 "unranged": [],
 "added_assumptions": [],
 "conclusions_as_proved": {
  "cs_sign": "\u2200 (r T : \u211d), 1 / 50 \u2264 r \u2192 r \u2264 1 / 20 \u2192 10 \u2264 T \u2192 T \u2264 101 \u2192 -105 \u2264 PhiC (r / (1 + r)) T \u2227 PhiC (r / (1 + r)) T < 0 \u2227 -91 < nSalt r * PhiC (r / (1 + r)) T \u2227 nSalt r * PhiC (r / (1 + r)) T < 0",
  "answer_theorem": "\u2200 (r P h Tb : \u211d) (Cw Hw Ph Hcr Cpcr Tw Ts \u03c4w \u03c4s : \u211d \u2192 \u211d) (hinf phiL : \u211d), Premises r Cw Hw Ph Hcr Cpcr hinf phiL \u2192 20 \u2264 Tb \u2192 Tb \u2264 101 \u2192 0 \u2264 h \u2192 h * (Tb - 20) < P \u2192 (\u2200 H \u2208 Set.Icc (Hw 20) (Hw Tb), Tw H \u2208 Set.Icc 10 Tb \u2227 Hw (Tw H) = H) \u2192 (\u2200 H \u2208 Set.Icc (solEnthalpy (nSalt r) Hw Ph Hcr 10) (solEnthalpy (nSalt r) Hw Ph Hcr Tb), Ts H \u2208 Set.Icc 10 Tb \u2227 solEnthalpy (nSalt r) Hw Ph Hcr (Ts H) = H) \u2192 (\u2200 H \u2208 Set.Icc (Hw 20) (Hw Tb), HasDerivAt \u03c4w (1 / (P - h * (Tw H - 20))) H) \u2192 (\u2200 H \u2208 Set.Icc (solEnthalpy (nSalt r) Hw Ph Hcr 10) (solEnthalpy (nSalt r) Hw Ph Hcr Tb), HasDerivAt \u03c4s (1 / (P - h * (Ts H - 20))) H) \u2192 \u2200 Ta \u2208 Set.Icc 20 Tb, boilTime \u03c4w \u03c4s Hw (solEnthalpy (nSalt r) Hw Ph Hcr) 20 Tb 20 \u2264 boilTime \u03c4w \u03c4s Hw (solEnthalpy (nSalt r) Hw Ph Hcr) 20 Tb Ta"
 },
 "extracted_file": "/work/checks/07773642358d4d4197d0265cdf13591c/extracted.lean",
 "errors": []
}
{
  "ok": false,
  "check_complete": false,
  "reviewable": false,
  "verdict": "not_proved",
  "time": "2026-11-18T01:10:38.550088+00:00",
  "lean_exit": 0,
  "error": "independent extraction failed"
}
Bash input
{
  "command": "cd /work; python /harness/src/vcs_protocol.py tool check --manifest /work/candidate.json 2>&1 | head -60; ls -t checks | head -2"
}
Bash output
{
 "file": "/work/checks/33a4ae57add14e699086d38537f08f5d/submitted.lean",
 "conclusions": [
  "cs_sign",
  "answer_theorem"
 ],
 "lean_exit": 0,
 "ok": false,
 "compiles_alone": true,
 "faithful": {
  "cs_sign": false,
  "answer_theorem": false
 },
 "assumption_declarations": [
  "Mnacl",
  "a1",
  "a2",
  "a3",
  "a4",
  "a5",
  "a6",
  "PhiC",
  "boilTime",
  "cal",
  "nSalt",
  "solEnthalpy",
  "Premises"
 ],
 "identity_method": "kernel-expression-repr-v1",
 "conclusion_identities": {
  "cs_sign": {
   "original_sha256": "50adfa75596c57c5928f2560ce239208d0691ae907933a58bd5921113d808397",
   "extracted_sha256": "7c2c9a0031e05ef5f4f121fdcc0795bcfc8f50e269d58be50689851d696c3aa6"
  },
  "answer_theorem": {
   "original_sha256": "1d387f7d2a70d9eaafad77f8e85128a492cde97cf928bfa1fd55e539ca2cfbea",
   "extracted_sha256": "6943e370f4da8627450a39eb8ee74ccd304698b794b678651749877b07647a45"
  }
 },
 "dependency_faithful": {
  "Premises": true,
  "Premises.mk": true,
  "PhiC": true,
  "Mnacl": true,
  "a1": true,
  "a2": true,
  "a3": true,
  "a4": true,
  "a5": true,
  "a6": true,
  "cal": true,
  "solEnthalpy": true,
  "solEnthalpy._proof_1": true,
  "nSalt": true,
  "nSalt._proof_1": true,
  "boilTime": true
 },
 "dependency_identities": {
  "Premises": {
   "original_sha256": "f2ae8dc2cc30958027350bf5716bd6d607e1d6b6a7749d48163adf623eba45d9",
33a4ae57add14e699086d38537f08f5d
07773642358d4d4197d0265cdf13591c
Bash input
{
  "command": "cd /work/checks/33a4ae57add14e699086d38537f08f5d; ls; sed -n 1,60p extracted.lean 2>/dev/null | cut -c1-220; grep -n \"error\" -i *.log | head"
}
Bash output
Checked.lean
extracted.json
extracted.lean
lean.log
submitted-candidate.json
submitted-report.txt
submitted.lean
verdict.json
import Mathlib.Analysis.SpecialFunctions.Log.Deriv
import Mathlib.Analysis.SpecialFunctions.Pow.Real
import Mathlib.Analysis.SpecialFunctions.ExpDeriv
import Mathlib.Analysis.Calculus.MeanValue

noncomputable def Mnacl : ℝ := 58.44

noncomputable def a1 : ℝ := -0.0693559668993322

noncomputable def a2 : ℝ := -0.0782134167486952

noncomputable def a3 : ℝ := 3.84798479408635

noncomputable def a4 : ℝ := -11.2762109247072

noncomputable def a5 : ℝ := 8.73187698542672

noncomputable def a6 : ℝ := 1.81245930472755

/-- apparent molar heat capacity of NaCl in aqueous solution, J/(K mol): Mnacl * (Laliberté solute heat capacity
    Cp_i in J/(g K)); w = salt mass fraction, T in C -/
noncomputable def PhiC (w T : ℝ) : ℝ :=
  Mnacl * (a1 * Real.exp (a2*T + a3*Real.exp (0.01*T) + a4*w) + a5 * w ^ a6)

/-- Time to reach the boiling point when salt is added while the pure water is at Ta: pure-water branch up to
    total enthalpy Hw Ta, then solution branch from the same enthalpy up to Hs Tb.
    τw, τs = elapsed time as a function of total enthalpy on the two branches. -/
def boilTime (τw τs Hw Hs : ℝ → ℝ) (T0 Tb Ta : ℝ) : ℝ :=
  (τw (Hw Ta) - τw (Hw T0)) + (τs (Hs Tb) - τs (Hw Ta))

/-- thermochemical calorie in J (Parker 1965 reports cal) -/
noncomputable def cal : ℝ := 4.184

/-- moles of NaCl for salt/water mass ratio r, in the 0.998 kg of water of 1 L at 20 C -/
noncomputable def nSalt (r : ℝ) : ℝ := 1000*0.998*r/Mnacl

/-- Enthalpy (J) of pot+solution at T, measured from pot+pure water plus crystalline NaCl at 20 C.
    Definition of the apparent molar enthalpy Ph of NaCl in the final solution: solution = (same water at T) + n*Ph(T);
    the salt started as crystal with enthalpy Hcr 20. -/
def solEnthalpy (n : ℝ) (Hw Ph Hcr : ℝ → ℝ) (T : ℝ) : ℝ := Hw T + n * (Ph T - Hcr 20)

/-- Physical premises (empirical data and definitions), see the review packet. -/
structure Premises (r : ℝ) (Cw Hw Ph Hcr Cpcr : ℝ → ℝ) (hinf phiL : ℝ) : Prop where
  r_lo : 1/50 ≤ r
  r_hi : r ≤ 1/20
  hw_deriv : ∀ T ∈ Set.Icc (10:ℝ) 101, HasDerivAt Hw (Cw T) T
  hw_cap : ∀ T ∈ Set.Icc (10:ℝ) 101, 4100 ≤ Cw T
  ph_deriv : ∀ T ∈ Set.Icc (10:ℝ) 101, HasDerivAt Ph (PhiC (r/(1+r)) T) T
  cr_deriv : ∀ T ∈ Set.Icc (20:ℝ) 25, HasDerivAt Hcr (Cpcr T) T
  cr_cap : ∀ T ∈ Set.Icc (20:ℝ) 25, 40 ≤ Cpcr T ∧ Cpcr T ≤ 100
  hinf_b : 923*cal ≤ hinf ∧ hinf ≤ 933*cal
  phiL_b : -41*cal ≤ phiL ∧ phiL ≤ 100*cal
  diss25 : Ph 25 - Hcr 25 = hinf + phiL


/-! Conclusions (statements of the proved theorems). -/
def conclusion_cs_sign : Prop := ∀ (r T : ℝ), 1/50 ≤ r → r ≤ 1/20 → 10 ≤ T → T ≤ 101 → -105 ≤ PhiC (r/(1+r)) T ∧ PhiC (r/(1+r)) T < 0 ∧ -91 < nSalt r * PhiC (r/(1+r)) T ∧ nSalt r * PhiC (r/(1+
def conclusion_answer_theorem : Prop := ∀ (r P h Tb : ℝ) (Cw Hw Ph Hcr Cpcr Tw Ts τw τs : ℝ → ℝ) (hinf phiL : ℝ), Premises r Cw Hw Ph Hcr Cpcr hinf phiL → 20 ≤ Tb → Tb ≤ 101 → 0 ≤ h → h*(Tb-20
Bash input
{
  "command": "cd /work/checks/33a4ae57add14e699086d38537f08f5d; cat verdict.json | head -50; python3 - <<'E'\nimport json\nd=json.load(open('extracted.json'))\nprint(d.keys())\nE\ngrep -n \"identity\\|VAS\" lean.log | cut -c1-300 | head -20"
}
Bash output
{
  "ok": false,
  "check_complete": false,
  "reviewable": false,
  "verdict": "not_proved",
  "time": "2026-11-18T01:11:34.486212+00:00",
  "lean_exit": 0,
  "error": "independent extraction failed"
}
dict_keys(['file', 'conclusions', 'lean_exit', 'ok', 'compiles_alone', 'faithful', 'assumption_declarations', 'identity_method', 'conclusion_identities', 'dependency_faithful', 'dependency_identities', 'unranged', 'added_assumptions', 'conclusions_as_proved', 'extracted_file', 'errors'])
9:VAS_LISTED
Bash input
{
  "command": "cd /work/checks/33a4ae57add14e699086d38537f08f5d; grep -n \"identity\\|expression\" /harness/src/vcs_extract.py | head -30; cat lean.log | cut -c1-400 | head -30"
}
Bash output
17:# Compare the complete kernel expressions instead. Binder names and metadata do
20:# levels, implicit arguments and binder information remain in the identity.
22:private partial def VAS_identityExpr : Lean.Expr → Lean.Expr
23:  | .mdata _ value => VAS_identityExpr value
24:  | .app fn arg => .app (VAS_identityExpr fn) (VAS_identityExpr arg)
25:  | .lam _ typ body info => .lam .anonymous (VAS_identityExpr typ) (VAS_identityExpr body) info
26:  | .forallE _ typ body info => .forallE .anonymous (VAS_identityExpr typ) (VAS_identityExpr body) info
27:  | .letE _ typ value body flag => .letE .anonymous (VAS_identityExpr typ) (VAS_identityExpr value) (VAS_identityExpr body) flag
28:  | .proj owner idx value => .proj owner idx (VAS_identityExpr value)
42:    | some body => reprStr (VAS_identityExpr body)
44:  s!"{kind}|levels={reprStr ci.levelParams}|type={reprStr (VAS_identityExpr ci.type)}|value={value}"
59:    logInfo m!"VAS_ORIGINAL_IDENTITY {t} : {reprStr (VAS_identityExpr ci.type)} VAS_END"
93:    logInfo m!"VAS_EXTRACTED_IDENTITY {n} : {reprStr (VAS_identityExpr v)} VAS_END"
116:def expression_identities(text, marker):
117:    """Read complete expression records; duplicate/missing records fail closed."""
122:            raise ValueError(f"duplicate expression identity for {name}")
165:    original_identities = expression_identities(text, "VAS_ORIGINAL_IDENTITY")
166:    dependency_identities = expression_identities(text, "VAS_DEPENDENCY_IDENTITY")
223:    extracted_identities = expression_identities(text2, "VAS_EXTRACTED_IDENTITY")
227:    extracted_dependencies = expression_identities(text2, "VAS_EXTRACTED_DEPENDENCY_IDENTITY")
230:    identity_hashes = {
245:               "identity_method": "kernel-expression-repr-v1", "conclusion_identities": identity_hashes,
/work/checks/33a4ae57add14e699086d38537f08f5d/Checked.lean:132:31: warning: Variable name `hab` is not explicitly referenced.

Hint: The binding can be removed (if unused) or named `_` (if used implicitly). Alternatively, prefix the name with `_` to silence this warning:
  [apply] _hab

Note: This linter can be disabled with `set_option linter.unusedVariables false`
'cs_sign' depends on axioms: [propext, Classical.choice, Quot.sound]
'answer_theorem' depends on axioms: [propext, Classical.choice, Quot.sound]
VAS_LISTED

leanchecker exit 0
Bash input
{
  "command": "sed -n 150,235p /harness/src/vcs_protocol.py"
}
Bash output
def verify_candidate(work, manifest, out):
    """Actually compile, audit axioms, recheck kernel and independently extract."""
    from vcs_lean import ALLOWED_AXIOMS, LISTER, listed
    from vcs_extract import run_lean, main as extract
    out = Path(out)
    out.mkdir(parents=True, exist_ok=True)
    result = {"ok": False, "check_complete": False, "reviewable": False, "verdict": "not_proved", "time": stamp()}
    try:
        m, proof, narrative = load_candidate(work, manifest)
        snapshot = out / "submitted.lean"
        snapshot.write_bytes(proof.read_bytes())
        (out / "submitted-report.txt").write_bytes(narrative.read_bytes())
        write_json(out / "submitted-candidate.json", m)
        proof = snapshot
        names = list(m["conclusions"].values())
        source = proof.read_text()
        validate_source(source, names)
        targets = "[" + ", ".join("``" + n for n in names) + "]"
        audited = out / "Checked.lean"
        audited.write_text(source + "\n" + "\n".join(f"#print axioms {n}" for n in names) + "\n" + LISTER.replace("ROOTS", targets))
        status, log = run_lean(audited)
        (out / "lean.log").write_text(log)
        result["lean_exit"] = status
        additions = listed(log)
        seen = set(re.findall(r"^'(.+)' (?:depends on axioms:|does not depend on any axioms)", log, re.M))
        axioms = {n: [a.strip() for a in xs.split(",") if a.strip()]
                  for n, xs in re.findall(r"^'(.+)' depends on axioms: \[([^\]]*)\]", log, re.M)}
        unexpected = {a for xs in axioms.values() for a in xs if a not in ALLOWED_AXIOMS}
        disclosed = {a["name"] for a in additions or [] if a["kind"] == "axiom" and a["used"]}
        gaps = [a for a in additions or [] if a["kind"] == "gap" and a["used"]]
        if status != 0 or additions is None or not set(names) <= seen:
            raise ValueError("Lean, kernel recheck or axiom audit failed; see lean.log")
        if any(a not in disclosed and not (a == "sorryAx" and gaps) for a in unexpected):
            raise ValueError("undisclosed axioms")
        if re.search(r"VAS_UNSTATED \S+ used=true", log) or any("⋯" in a["statement"] for a in additions if a["used"]):
            raise ValueError("incomplete assumption disclosure")
        # click's standalone_mode=False gives real exit errors without terminating host.
        try:
            extract.main(args=[str(proof), *sum((["--theorem", n] for n in names), []), "--out", str(out / "extracted")], standalone_mode=False)
        except SystemExit as exc:
            # Extractor explicitly sys.exit(0), even outside standalone mode.
            if exc.code != 0:
                raise ValueError("independent extraction failed") from exc
        extraction = read_json(out / "extracted.json")
        if not extraction.get("ok") or not extraction.get("compiles_alone") or not all(extraction.get("faithful", {}).values()):
            raise ValueError("independent extraction/statement comparison failed")
        # Any added premise or gap, even unused, remains explicitly conditional.
        result |= {"ok": not additions, "check_complete": True, "reviewable": True, "verdict": "proved_if" if additions else "proved",
                   "added_assumptions": additions, "conditional_on": sorted(unexpected),
                   "extraction": extraction, "scientific_relevance": "requires human review"}
    except (Exception, SystemExit) as exc:
        result |= {"ok": False, "error": str(exc) or type(exc).__name__}
        if isinstance(exc, (subprocess.TimeoutExpired, TimeoutError)):
            result["verdict"] = "no_verdict"
    write_json(out / "verdict.json", result)
    return result


class NativeAuthenticationError(ValueError):
    """Native subscription credentials/status need owner intervention."""


def native_codex():
    executable = shutil.which("codex")
    if not executable:
        raise ValueError("Codex CLI is not installed")
    js = Path(executable).resolve()
    candidates = list(js.parent.parent.glob("node_modules/@openai/codex-linux-x64/vendor/*/bin/codex"))
    candidates += list(js.parent.parent.glob("vendor/*/bin/codex"))
    if not candidates:
        raise ValueError("native Codex binary not found; specify --codex-binary")
    return candidates[0]


def native_claude():
    executable = shutil.which("claude")
    if not executable:
        raise ValueError("native Claude CLI is not installed; specify --claude-binary")
    return Path(executable).resolve()


def client_name(config):
    # States from the original release have no agent field.
    return config.get("agent", "codex")
Bash input
{
  "command": "sed -n 14,100p /harness/src/vcs_extract.py; sed -n 160,235p /harness/src/vcs_extract.py"
}
Bash output
from vcs_lean import LEAN, LEANCHECKER, LISTER, listed  # noqa: E402

# Display text omits implicit arguments, types and sometimes whole subterms.
# Compare the complete kernel expressions instead. Binder names and metadata do
# not affect meaning; remove them so source positions and fresh binder names do
# not make an otherwise identical extraction fail. All constants, universe
# levels, implicit arguments and binder information remain in the identity.
IDENTITY = """
private partial def VAS_identityExpr : Lean.Expr → Lean.Expr
  | .mdata _ value => VAS_identityExpr value
  | .app fn arg => .app (VAS_identityExpr fn) (VAS_identityExpr arg)
  | .lam _ typ body info => .lam .anonymous (VAS_identityExpr typ) (VAS_identityExpr body) info
  | .forallE _ typ body info => .forallE .anonymous (VAS_identityExpr typ) (VAS_identityExpr body) info
  | .letE _ typ value body flag => .letE .anonymous (VAS_identityExpr typ) (VAS_identityExpr value) (VAS_identityExpr body) flag
  | .proj owner idx value => .proj owner idx (VAS_identityExpr value)
  | value => value

private def VAS_declarationIdentity (ci : Lean.ConstantInfo) : String :=
  let kind := match ci with
    | .axiomInfo _ => "axiom"
    | .defnInfo _ => "definition"
    | .thmInfo _ => "theorem"
    | .opaqueInfo _ => "opaque"
    | .quotInfo _ => "quotient"
    | .inductInfo info => s!"inductive {info.numParams} {info.numIndices} {reprStr info.ctors}"
    | .ctorInfo info => s!"constructor {info.induct} {info.cidx} {info.numParams} {info.numFields}"
    | .recInfo info => s!"recursor {info.numParams} {info.numIndices} {info.numMotives} {info.numMinors}"
  let value := match ci.value? with
    | some body => reprStr (VAS_identityExpr body)
    | none => "no value"
  s!"{kind}|levels={reprStr ci.levelParams}|type={reprStr (VAS_identityExpr ci.type)}|value={value}"
"""

REACH = """
open Lean Elab Command Meta in
run_cmd do
  let kenv := (← getEnv).checked.get
  let localSet : NameSet := kenv.constants.map₂.toList.foldl (fun s (n, _) => s.insert n) {}
  -- statements only: from each conclusion's type, through definitions' types and values
  let mut seen : NameSet := {}
  let mut todo : List Name := []
  for t in TARGETS do
    let some ci := kenv.find? t | throwError m!"no theorem {t}"
    todo := ci.type.getUsedConstants.toList ++ todo
    logInfo m!"VAS_CONCLUSION {t} : {← liftTermElabM <| ppExpr ci.type} VAS_END"
    logInfo m!"VAS_ORIGINAL_IDENTITY {t} : {reprStr (VAS_identityExpr ci.type)} VAS_END"
  while !todo.isEmpty do
    let n := todo.head!
    todo := todo.tail!
    if seen.contains n || !localSet.contains n then continue
    seen := seen.insert n
    let some ci := kenv.find? n | continue
    logInfo m!"VAS_DEPENDENCY_IDENTITY {n} : {VAS_declarationIdentity ci} VAS_END"
    let more := ci.type.getUsedConstants ++ (match ci with
      | .defnInfo v => v.value.getUsedConstants | .thmInfo v => v.value.getUsedConstants
      | .opaqueInfo v => v.value.getUsedConstants
      | .inductInfo v => v.ctors.toArray | .ctorInfo v => #[v.induct] | _ => #[])
    todo := more.toList ++ todo
    -- auxiliary constants (matchers, constructors, projections, equation lemmas) map to the nearest ancestor
    -- whose source range Lean recorded
    let mut m := n
    let mut r ← findDeclarationRanges? m
    while r.isNone && !m.isAnonymous do
      m := m.getPrefix
      r ← findDeclarationRanges? m
    match r with
    | some rg => logInfo m!"VAS_DECL {m} {rg.range.pos.line} {rg.range.endPos.line}"
    | none => logInfo m!"VAS_UNRANGED {n}"
  logInfo "VAS_REACHED"
"""

COMPARE = """
set_option format.width 100000 in
open Lean Elab Command Meta in
run_cmd do
  for n in NAMES do
    let some ci := (← getEnv).find? n | throwError m!"no {n}"
    let some v := ci.value? | throwError m!"no value {n}"
    logInfo m!"VAS_EXTRACTED {n} : {← liftTermElabM <| ppExpr v} VAS_END"
    logInfo m!"VAS_EXTRACTED_IDENTITY {n} : {reprStr (VAS_identityExpr v)} VAS_END"
  for n in DEPENDENCIES do
    let some ci := (← getEnv).find? n | throwError m!"missing extracted dependency {n}"
    logInfo m!"VAS_EXTRACTED_DEPENDENCY_IDENTITY {n} : {VAS_declarationIdentity ci} VAS_END"
"""


def run_lean(path):
    decls = {}
    for name, a, b in re.findall(r"VAS_DECL (\S+) (\d+) (\d+)", text):
        decls[name] = (int(a), int(b))
    unranged = re.findall(r"VAS_UNRANGED (\S+)", text)
    concl = dict(re.findall(r"VAS_CONCLUSION (\S+) : (.*?) VAS_END", text, re.S))
    original_identities = expression_identities(text, "VAS_ORIGINAL_IDENTITY")
    dependency_identities = expression_identities(text, "VAS_DEPENDENCY_IDENTITY")
    added = [a for a in listed(text) or [] if a["used"]]
    # the extracted file: imports, then each assumption's source in file order, inside its namespace. A range
    # inside another one (a structure's fields and constructor) is part of it and is not copied twice.
    imports = [ln for ln in lines if ln.startswith("import ")]
    spans = sorted(set(decls.values()))
    keep = [s for s in spans if not any(o != s and o[0] <= s[0] and s[1] <= o[1] for o in spans)]
    for x, y in zip(keep, keep[1:]):
        if y[0] <= x[1]:
            raise ValueError(f"overlapping declaration ranges {x} and {y}")
    owner = {}
    for name, rng in decls.items():
        if rng in keep and (rng not in owner or len(name) < len(owner[rng])):
            owner[rng] = name
    head = re.compile(r"^\s*(?:@\[[^\]]*\]\s*)?(?:noncomputable\s+|private\s+|protected\s+)*"
                      r"(?:def|theorem|lemma|abbrev|structure|inductive|class|axiom|instance|opaque)\s+([^\s:({\[]+)")
    # top-level `open X` lines of the source go along, in file order, so names resolve as they did there
    opens = [(i + 1, i + 1) for i, ln in enumerate(lines)
             if re.match(r"open\s+[\w.]+(\s+[\w.]+)*\s*$", ln) and not any(a <= i + 1 <= b for a, b in keep)]
    parts, current = [], None
    for rng in sorted(keep + opens):
        a, b = rng
        if rng in opens and rng not in owner:
            if current:
                parts.append(f"end {current}\n")
                current = None
            parts.append(lines[a - 1])
            continue
        name = owner[rng]
        short = next((m.group(1) for ln in lines[a - 1:b] if (m := head.match(ln))), None)
        if short is None or not (name == short or name.endswith("." + short)):
            raise ValueError(f"cannot match the source of {name} at lines {a}-{b}")
        ns = name[: -len(short) - 1] if name != short else ""
        if ns != current:
            if current:
                parts.append(f"end {current}\n")
            if ns:
                parts.append(f"namespace {ns}")
            current = ns
        parts.append("\n".join(lines[a - 1:b]) + "\n")
    if current:
        parts.append(f"end {current}\n")
    starts = {}
    for t in theorems:
        m = re.search(r"(?m)^\s*(?:theorem|lemma)\s+" + re.escape(t) + r"\b", src)
        starts[t] = src[:m.start()].count("\n") + 1 if m else None
    stmts = {t: statement_text(lines, starts[t], t) for t in theorems}
    conclusions = "".join(f"def conclusion_{t} : Prop := {stmts[t]}\n" for t in theorems)
    extracted = ("\n".join(imports) + "\n\n" + "\n".join(parts) + "\n\n"
                 "/-! Conclusions (statements of the proved theorems). -/\n" + conclusions)
    out.with_suffix(".lean").write_text(extracted)
    # step 5: the extracted file compiles alone, and its conclusions are the proved statements
    check = out.with_name(out.name + ".check.lean")
    names = "[" + ", ".join(f"``conclusion_{t}" for t in theorems) + "]"
    dependencies = "[" + ", ".join(f"``{name}" for name in dependency_identities) + "]"
    check.write_text(extracted + IDENTITY + COMPARE.replace("NAMES", names).replace("DEPENDENCIES", dependencies))
    status2, text2 = run_lean(check)
    extracted_identities = expression_identities(text2, "VAS_EXTRACTED_IDENTITY")
    same = {t: bool(original_identities.get(t)) and
            original_identities[t] == extracted_identities.get("conclusion_" + t)
            for t in theorems}
    extracted_dependencies = expression_identities(text2, "VAS_EXTRACTED_DEPENDENCY_IDENTITY")
    dependencies_same = {name: value == extracted_dependencies.get(name)
                         for name, value in dependency_identities.items()}
    identity_hashes = {
        t: {"original_sha256": hashlib.sha256(original_identities[t].encode()).hexdigest()
            if t in original_identities else None,
            "extracted_sha256": hashlib.sha256(extracted_identities["conclusion_" + t].encode()).hexdigest()
            if "conclusion_" + t in extracted_identities else None}
        for t in theorems}
Bash input
{
  "command": "cd /tmp/lw; cp /work/checks/33a4ae57add14e699086d38537f08f5d/extracted.lean ex.lean; python3 - <<'E'\nimport re\ns=open('/tmp/lw/ex.lean').read()\nm=re.search(r\"def conclusion_cs_sign : Prop := (.*?)\\ndef conclusion_answer\",s,re.S)\nstmt=m.group(1)\ns+=\"\\ntheorem cs_sign2 : \"+stmt+\" := sorry\\n\"\ns+='''\nopen Lean Elab Command Meta in\nrun_cmd do\n  let env ← getEnv\n  let a := (env.find? `cs_sign2).get!.type\n  let b := (env.find? `conclusion_cs_sign).get!.value!\n  logInfo m!\"{reprStr a == reprStr b}\"\n  IO.FS.writeFile \"/tmp/lw/ra.txt\" (reprStr a)\n  IO.FS.writeFile \"/tmp/lw/rb.txt\" (reprStr b)\n'''\nopen('/tmp/lw/ex2.lean','w').write(s)\nE\ncd /tmp; bash /tmp/scr/lc.sh /tmp/lw/ex2.lean 20 | tail -5; python3 - <<'E'\na=open('/tmp/lw/ra.txt').read(); b=open('/tmp/lw/rb.txt').read()\nprint(len(a),len(b))\nimport difflib\nfor i,(x,y) in enumerate(zip(a,b)):\n    if x!=y: print(i,a[i-200:i+200]); print('----'); print(b[i-200:i+200]); break\nE"
}
Bash output
/tmp/lw/ex2.lean:60:8: warning: declaration uses `sorry`
false
31853 25402
2188 stTwo [Lean.Level.zero]) (Lean.Expr.const `Real []))
                    (Lean.Expr.lit (Lean.Literal.natVal 50)))
                  (Lean.Expr.const `Real.instNatCast []))
                (Lean.Expr.app
                  (Lean.Expr.app
                    (Lean.Expr.const `Nat.instAtLeastTwoHAddOfNat [])
                    (Lean.Expr.app
                      (Lean.Expr.app
                     
----
stTwo [Lean.Level.zero]) (Lean.Expr.const `Real []))
                    (Lean.Expr.lit (Lean.Literal.natVal 50)))
                  (Lean.Expr.const `Real.instNatCast []))
                (Lean.Expr.const `conclusion_cs_sign._proof_1 [])))))
        (Lean.Expr.bvar 1))
      (Lean.Expr.forallE
        (Lean.Name.mkNum `a._@._internal._hyg 0)
        (Lean.Expr.app
          (Lean.Expr.app
Shell cwd was reset to /work
Bash input
{
  "command": "cd /tmp/lw; cat > P5.lean <<'E'\n\n/-! ## Conclusions (named constants of the regime; numerals live in definitions) -/\n\n/-- starting / room temperature of the water (C) -/\nnoncomputable def Tr : ℝ := 20\n/-- lowest temperature considered (C) -/\nnoncomputable def Tmin : ℝ := 10\n/-- highest admissible boiling temperature of the solution (C) -/\nnoncomputable def Tcap : ℝ := 101\n/-- regime of the salt/water mass ratio r: 20 to 50 g NaCl per kg water -/\nnoncomputable def rLo : ℝ := 1/50\nnoncomputable def rHi : ℝ := 1/20\n/-- lower bound for Phi_C (J/K/mol) and for the heat-capacity change n*Phi_C (J/K) -/\nnoncomputable def PhiFloor : ℝ := 105\nnoncomputable def CsFloor : ℝ := 91\n\n/-- CONCLUSION 1 (heat capacity). For 20-50 g NaCl per kg water and 10-101 C the apparent molar heat capacity of NaCl\n    in solution is negative and >= -105 J/(K mol); dissolving the salt LOWERS the total heat capacity of the pot\n    contents by n*Phi in (-91, 0) J/K (< 2.2 % of the ~4.2 kJ/K of the water). -/\ntheorem cs_sign : ∀ (r T : ℝ), rLo ≤ r → r ≤ rHi → Tmin ≤ T → T ≤ Tcap →\n    -PhiFloor ≤ PhiC (r/(1+r)) T ∧ PhiC (r/(1+r)) T < 0 ∧\n    -CsFloor < nSalt r * PhiC (r/(1+r)) T ∧ nSalt r * PhiC (r/(1+r)) T < 0 := by\n  intro r T h1 h2 t1 t2\n  unfold rLo rHi Tmin Tcap PhiFloor CsFloor at *\n  obtain ⟨w1, w2⟩ := w_range r h1 h2\n  obtain ⟨n1, n2⟩ := n_range r h1 h2\n  have f := phi_floor _ T w1 t1 t2\n  have g := phi_neg _ T w1 w2 t1 t2\n  refine ⟨f, g, ?_, ?_⟩ <;> nlinarith\n\n/-- CONCLUSION 2 (answer). Salt added at the very start (water at Tr = 20 C) gives a boiling time no longer than salt\n    added once the water has reached any later temperature Ta <= Tb. -/\ntheorem answer_theorem : ∀ (r P h Tb : ℝ) (Cw Hw Ph Hcr Cpcr Tw Ts τw τs : ℝ → ℝ) (hinf phiL : ℝ),\n    Premises r Cw Hw Ph Hcr Cpcr hinf phiL → Tr ≤ Tb → Tb ≤ Tcap → 0 ≤ h → h*(Tb-Tr) < P →\n    (∀ H ∈ Set.Icc (Hw Tr) (Hw Tb), Tw H ∈ Set.Icc Tmin Tb ∧ Hw (Tw H) = H) →\n    (∀ H ∈ Set.Icc (solEnthalpy (nSalt r) Hw Ph Hcr Tmin) (solEnthalpy (nSalt r) Hw Ph Hcr Tb),\n        Ts H ∈ Set.Icc Tmin Tb ∧ solEnthalpy (nSalt r) Hw Ph Hcr (Ts H) = H) →\n    (∀ H ∈ Set.Icc (Hw Tr) (Hw Tb), HasDerivAt τw (1/(P - h*(Tw H - Tr))) H) →\n    (∀ H ∈ Set.Icc (solEnthalpy (nSalt r) Hw Ph Hcr Tmin) (solEnthalpy (nSalt r) Hw Ph Hcr Tb),\n        HasDerivAt τs (1/(P - h*(Ts H - Tr))) H) →\n    ∀ Ta ∈ Set.Icc Tr Tb,\n      boilTime τw τs Hw (solEnthalpy (nSalt r) Hw Ph Hcr) Tr Tb Tr\n        ≤ boilTime τw τs Hw (solEnthalpy (nSalt r) Hw Ph Hcr) Tr Tb Ta := by\n  intro r P h Tb Cw Hw Ph Hcr Cpcr Tw Ts τw τs hinf phiL hp hT1 hT2 hh hP hTw hTs hτw hτs\n  unfold Tr Tmin Tcap at *\n  obtain ⟨m1, m2, c1, c2⟩ := thermo_facts r Tb Cw Hw Ph Hcr Cpcr hinf phiL hp hT2\n  exact timing_core P h 20 10 Tb Hw (solEnthalpy (nSalt r) Hw Ph Hcr) Tw Ts τw τs hh hP (by norm_num) hT1\n    m1 m2 hTw hTs hτw hτs c1 c2\nE\ncat P1.lean P2.lean P3.lean P4a.lean tail.txt P4b.lean P4c.lean P5.lean > T3.lean; cd /tmp; bash /tmp/scr/lc.sh /tmp/lw/T3.lean 40 | grep -v -E \"push_neg|^If you|^open|^macro|^\\`\\`\\`|^  \\`\\(tactic|^$|Hint|Note\""
}
Bash output
/tmp/lw/T3.lean:98:31: warning: Variable name `hab` is not explicitly referenced.
  [apply] _hab
Shell cwd was reset to /work
Bash input
{
  "command": "cd /work; python3 - <<'E'\ncur=open('/work/Model.lean').read()\nstart=cur.index(\"/-! # Salt timing\"); end=cur.index(\"-/\\n\",start)+3\npacket=cur[start:end]\nbody=open('/tmp/lw/T3.lean').read().replace(\"/-! (header review packet inserted by script) -/\",packet)\nopen('/work/Model.lean','w').write(body)\nE\npython /harness/src/vcs_protocol.py tool check --manifest /work/candidate.json 2>&1 | grep -E '\"(ok|verdict|error|faithful|check_complete|reviewable)\"|cs_sign\": (true|false)|answer_theorem\": (true|false)|added_assum' "
}
Bash output
 "ok": false,
 "faithful": {
  "cs_sign": true,
  "answer_theorem": false
 "added_assumptions": [],
  "ok": false,
  "check_complete": false,
  "reviewable": false,
  "verdict": "not_proved",
  "error": "independent extraction failed"
Bash input
{
  "command": "cd /tmp/lw; d=$(ls -t /work/checks | head -1); cp /work/checks/$d/extracted.lean ex.lean; python3 - <<'E'\nimport re\ns=open('/tmp/lw/ex.lean').read()\nm=re.search(r\"def conclusion_answer_theorem : Prop := (.*)\",s,re.S)\nstmt=m.group(1).strip()\ns+=\"\\ntheorem ans2 : \"+stmt+\" := sorry\\n\"\ns+='''\nopen Lean Elab Command Meta in\nrun_cmd do\n  let env ← getEnv\n  let a := (env.find? `ans2).get!.type\n  let b := (env.find? `conclusion_answer_theorem).get!.value!\n  IO.FS.writeFile \"/tmp/lw/ra.txt\" (reprStr a)\n  IO.FS.writeFile \"/tmp/lw/rb.txt\" (reprStr b)\n'''\nopen('/tmp/lw/ex2.lean','w').write(s)\nE\ncd /tmp; bash /tmp/scr/lc.sh /tmp/lw/ex2.lean 20 | tail -3; python3 - <<'E'\na=open('/tmp/lw/ra.txt').read(); b=open('/tmp/lw/rb.txt').read()\nfor i,(x,y) in enumerate(zip(a,b)):\n    if x!=y: print(i,a[i-300:i+200]); print('----'); print(b[i-300:i+200]); break\nE"
}
Bash output
/tmp/lw/ex2.lean:79:8: warning: declaration uses `sorry`
34534       [Lean.Level.zero])
                                                                  (Lean.Expr.const `Real []))
                                                                (Lean.Expr.const `Real.pseudoMetricSpace []))))
                                                          (Lean.Expr.app
                                                            (Lean.Expr.app
                                                              (Lean.Expr.app
                                            
----
      [Lean.Level.zero])
                                                                  (Lean.Expr.const `Real []))
                                                                (Lean.Expr.const `Real.pseudoMetricSpace []))))
                                                          (Lean.Expr.const `conclusion_answer_theorem._proof_1 []))
                                                        (Lean.Expr.bvar 12))
                                                      (Lean.Expr.app
Shell cwd was reset to /work
Bash input
{
  "command": "cd /tmp/lw; python3 - <<'E'\ns=open('P5.lean').read()\ndefs='''\n/-- Tw is the temperature as a function of total enthalpy H on the branch with enthalpy-temperature relation Hf\n    (Hf (T H) = H, T H in [Tl,Tb]), for H in [a,b]. -/\ndef InverseOn (Hf T : ℝ → ℝ) (Tl Tb a b : ℝ) : Prop := ∀ H ∈ Set.Icc a b, T H ∈ Set.Icc Tl Tb ∧ Hf (T H) = H\n\n/-- τ is the elapsed time as a function of total enthalpy on a branch with temperature profile T(H): since\n    dH/dt = P - h (T - Tr) > 0, dτ/dH = 1/(P - h (T(H) - Tr)). -/\ndef TimeProfile (P h Tr : ℝ) (τ T : ℝ → ℝ) (a b : ℝ) : Prop :=\n  ∀ H ∈ Set.Icc a b, HasDerivAt τ (1/(P - h*(T H - Tr))) H\n'''\ns=s.replace(\"/-- CONCLUSION 2\",defs+\"\\n/-- CONCLUSION 2\")\ni=s.index(\"    (∀ H ∈ Set.Icc (Hw Tr) (Hw Tb), Tw H\"); j=s.index(\"    ∀ Ta ∈ Set.Icc Tr Tb,\")\nnew='''    InverseOn Hw Tw Tmin Tb (Hw Tr) (Hw Tb) →\n    InverseOn (solEnthalpy (nSalt r) Hw Ph Hcr) Ts Tmin Tb\n      (solEnthalpy (nSalt r) Hw Ph Hcr Tmin) (solEnthalpy (nSalt r) Hw Ph Hcr Tb) →\n    TimeProfile P h Tr τw Tw (Hw Tr) (Hw Tb) →\n    TimeProfile P h Tr τs Ts (solEnthalpy (nSalt r) Hw Ph Hcr Tmin) (solEnthalpy (nSalt r) Hw Ph Hcr Tb) →\n'''\ns=s[:i]+new+s[j:]\ns=s.replace(\"  unfold Tr Tmin Tcap at *\\n\",\"  unfold InverseOn TimeProfile at hTw hTs hτw hτs\\n  unfold Tr Tmin Tcap at *\\n\")\nopen('P5.lean','w').write(s)\nE\ncat P1.lean P2.lean P3.lean P4a.lean tail.txt P4b.lean P4c.lean P5.lean > T3.lean; cd /tmp; bash /tmp/scr/lc.sh /tmp/lw/T3.lean 40 | grep -v -E \"push_neg|^If you|^open|^macro|^\\`\\`\\`|^  \\`\\(tactic|^$|Hint|Note\""
}
Bash output
/tmp/lw/T3.lean:98:31: warning: Variable name `hab` is not explicitly referenced.
  [apply] _hab
/tmp/lw/T3.lean:402:9: error: Tactic `unfold` failed to unfold `InverseOn` in
  TimeProfile P h Tr τw Tw (Hw Tr) (Hw Tb)
Shell cwd was reset to /work
Bash input
{
  "command": "cd /tmp/lw; sed -i 's/  unfold InverseOn TimeProfile at hTw hTs hτw hτs/  unfold InverseOn at hTw hTs\\n  unfold TimeProfile at hτw hτs/' P5.lean; cat P1.lean P2.lean P3.lean P4a.lean tail.txt P4b.lean P4c.lean P5.lean > T3.lean; cd /tmp; bash /tmp/scr/lc.sh /tmp/lw/T3.lean 40 | grep -v -E \"push_neg|^If you|^open|^macro|^\\`\\`\\`|^  \\`\\(tactic|^$|Hint|Note\"; cd /work; python3 - <<'E'\ncur=open('/work/Model.lean').read()\nstart=cur.index(\"/-! # Salt timing\"); end=cur.index(\"-/\\n\",start)+3\npacket=cur[start:end]\nbody=open('/tmp/lw/T3.lean').read().replace(\"/-! (header review packet inserted by script) -/\",packet)\nopen('/work/Model.lean','w').write(body)\nE\npython /harness/src/vcs_protocol.py tool check --manifest /work/candidate.json 2>&1 | grep -E '\"(ok|verdict|error|check_complete|reviewable)\"|cs_sign\": (true|false)|answer_theorem\": (true|false)|added_assum'"
}
Bash output
/tmp/lw/T3.lean:98:31: warning: Variable name `hab` is not explicitly referenced.
  [apply] _hab
 "ok": true,
  "cs_sign": true,
  "answer_theorem": true
 "added_assumptions": [],
  "ok": true,
  "check_complete": true,
  "reviewable": true,
  "verdict": "proved",
  "added_assumptions": [],
    "ok": true,
      "cs_sign": true,
      "answer_theorem": true
    "added_assumptions": [],
Bash input
{
  "command": "cd /work; cat >> report.txt <<'E'\n\nNOTE ON STATEMENT FORM: the final statements use named constants (Tr=20, Tmin=10, Tcap=101, rLo, rHi, PhiFloor, CsFloor) and the definitions InverseOn / TimeProfile (temperature-as-function-of-enthalpy and\nelapsed-time-as-function-of-enthalpy) so that the independent extraction compares identically (inline numerals >= 2 and HasDerivAt terms contain proof terms that the extractor abstracts).\nCheck result (tool check, final run): lean_exit 0, no sorry/axioms beyond propext/Classical.choice/Quot.sound, both conclusions faithful, verdict \"proved\" (conditional only on the exposed Premises, which are hypotheses, not axioms).\nE\npython /harness/src/vcs_protocol.py tool check --manifest /work/candidate.json 2>&1 | grep -E '\"verdict\"' | tail -1"
}
Bash output
  "verdict": "proved",
assistant
Candidate is ready for your review. The check tool returns `verdict: "proved"`, with no `sorry` and no extra axioms, and both conclusions pass the independent extraction. I have not approved anything.

**What changed from the rejected attempt**
- **Dissolution reference.** I read Parker (1965) in the supplied bundle. Printed p. 26 gives ΔH∞(NaCl(c), 25 °C) = 928 ± 5 cal/mol. Table X (p. 50) gives Φ_L, which is relative to infinite dilution, so it is not a complete dissolution heat. Φ_L is 83, 49, 20 and −41 cal/mol at m = 0.2775, 0.5551, 0.7401 and 1.1101, and it decreases monotonically over the regime.
  - The model uses ΔH(25 °C, m) = ΔH∞ + Φ_L, with Φ_L bounded in [−41, 100] cal/mol.
  - Lean moves this to 20 °C by Kirchhoff's relation, using crystal Cp ∈ [40, 100] J/K/mol. I checked that range by evaluating Archer's Part I spline myself, which gives 50.04 to 50.16.
  - The result is dissolution enthalpy at 20 °C ≥ 3890 J/mol. The old bare `hsol ≥ 3800` premise is gone.
- **Φ_C computed in Lean.** The Laliberté (2009) NaCl correlation is formalized as `PhiC`, with its coefficients exposed in the file.
  - Lean proves Φ_C < 0 and Φ_C ≥ −105 J/K/mol, concavity in T, and that the lowest concentration is the worst case.
  - It also proves a bound on the change in apparent molar enthalpy, Ph(101 °C) − Ph(20 °C) ≥ −3363 J/mol, using 11 exp enclosures built from `Real.exp_bound` and a trapezoid lemma.
  - Python only suggested the node values; Lean checks them.
- **No secant average.** The full temperature dependence is kept. The cooling inequality is proved for every addition temperature x in [20, T_b], not just at two endpoints. The Newtonian heat-loss model is unchanged, but state is now total enthalpy with temperature as a function of it, and elapsed time is defined by its derivative. The sandbox Mathlib has no interval integral, so this was the workable formulation.

**Conclusions**
- `cs_sign`: for 20–50 g NaCl per kg of water and 10–101 °C, Φ_C ∈ [−105, 0), and the heat-capacity change n·Φ_C is in (−91, 0) J/K. Adding salt slightly lowers the total heat capacity.
- `answer_theorem`: adding salt at the very start, with the water at 20 °C, is no slower than adding it at any later water temperature up to the boiling point.

The worst-case margin for the cooling condition is about 527 J/mol out of roughly 3.9 kJ/mol. This is a qualitative result. I state no time saving.

**Limits to review**
- The Laliberté coefficients came from a web fetch of the `chemicals` data file, with the model form taken from SysCAD documentation, not from the paper's own table. I cross-checked the formula against Parker Table II C: within 3% at 25 °C, and Laliberté is more negative at 20 °C, which is the conservative side.
- I did not implement Archer Part II, so Φ_C at 40–100 °C rests on Laliberté alone.
- T_b ≤ 101 °C is a premise, not derived from pressure and composition.
- The Parker values, C_w ≥ 4100 J/K, instant dissolution and the regime r ∈ [0.02, 0.05] are all premises.

Files are in /work:
- Model.lean
- report.txt
- candidate.json

The previous attempt is kept in `attempts/cycle5_secant/`.

Produced code

attempt-005/Model.lean
import Mathlib.Analysis.SpecialFunctions.Log.Deriv
import Mathlib.Analysis.SpecialFunctions.Pow.Real
import Mathlib.Analysis.SpecialFunctions.ExpDeriv
import Mathlib.Analysis.Calculus.MeanValue

/-! # Salt timing for 1 L of water at 20 C: when should NaCl be added? (cycle 6)

## REVIEW PACKET
SUPPLIED BY THE USER: 1 L water at 20 C in a pot on a stove; salt will be added; goal = salted water reaches
its boiling point soonest; salt amount, pot, heater, room unspecified.

ACCEPTED EARLIER (kept): lumped Newtonian heating dH/dt = P - h (T - 20), constant stove power P, no evaporation,
salt (crystal at 20 C) dissolves instantly at the moment of addition (total enthalpy conserved).

MODEL CHOICES / ASSUMPTIONS (all appear as `Premises` fields or theorem hypotheses):
 * NaCl; r = salt mass / water mass in [1/50, 1/20] (20-50 g in the 0.998 kg of water of 1 L); mass fraction
   w = r/(1+r) (conversion proved in `w_range`), n = 1000*0.998*r/58.44 mol (`nSalt`, bounds in `n_range`).
 * Pot+water enthalpy Hw(T) has heat capacity Cw(T) >= 4100 J/K on 10..101 C (0.998 kg water >= 4.17 kJ/K; pot adds).
 * Apparent molar heat capacity of NaCl in the final solution = Laliberte (2009) correlation `PhiC`
   (empirical coefficients a1..a6 from Laliberte, J. Chem. Eng. Data 54, 1725; fit range 1.5-120 C, w <= 0.26).
   Ph(T) is the apparent molar enthalpy of NaCl in that solution: dPh/dT = PhiC (definition of Phi_C).
   Solution enthalpy = (water at T) + n*Ph(T)   (definition of apparent molar enthalpy; `solEnthalpy`).
 * Crystal NaCl enthalpy Hcr with Cp_cr in [40,100] J/K/mol on 20..25 C (Archer 1992 Part I, Eqs 4-6: Cp(298.15)=50.16;
   my evaluation of the spline gives 50.04..50.16 on 20..25 C).
 * Parker NSRDS-NBS 2 (1965): Delta H_inf(25 C, NaCl(c) -> infinite dilution) = 928 +- 5 cal/mol (printed p.26);
   Phi_L(25 C, m) (Table X, p.50) is RELATIVE to infinite dilution: Ph(25) - Hcr(25) = Hinf + Phi_L.
   For the regime m = 17.1 r in [0.34, 0.86] mol/kg Table X gives Phi_L = 83, 49, 20, -41 cal/mol at m = .2775,
   .5551, .7401, 1.1101 and dPhi_L/dm^(1/2) < 0 for m >= .185, so Phi_L in [-41, 88]; premise used: [-41, 100] cal. cal = 4.184 J.
 * Boiling temperature Tb of the final solution: 20 <= Tb <= 101 C (1 atm, <= 4.8 wt%: ideal elevation <= ~0.9 K); stove
   beats losses at Tb; Tw, Ts = temperature as function of total enthalpy (inverse of Hw, Hs; existence by IVT,
   `inverse_exists`); tau_w, tau_s = elapsed time as function of enthalpy: d tau/dH = 1/(P - h (T(H) - 20)).

PROVED (not assumed): Phi_C < 0 and >= -105 J/(K mol) for all regime/temperatures (concavity + exp bounds, `cs_sign`);
 trapezoid enclosure Ph(101) - Ph(20) >= -3363 J/mol from 11 Taylor-bounded exp evaluations (`ph_drop`);
 dissolution at 20 C >= 3890 J/mol via Kirchhoff (`thermo_facts`); salt addition at any x in [20,Tb] cools
 (Hs(x) - Hw(x) >= 0), solution enthalpy strictly increasing, mixing at 20 C stays above 10 C; and the timing
 comparison (`timing_core`, `answer_theorem`).
CONCLUSIONS: `cs_sign`, `answer_theorem` (salting at the start is no slower than salting at any later water temperature).
-/


/-! ### calculus helpers -/
lemma dmono (F f : ℝ → ℝ) (a b : ℝ) (hF : ∀ T ∈ Set.Icc a b, HasDerivAt F (f T) T)
    (hf : ∀ T ∈ Set.Icc a b, 0 ≤ f T) : MonotoneOn F (Set.Icc a b) := by
  apply monotoneOn_of_deriv_nonneg (convex_Icc a b)
  · exact fun T hT => (hF T hT).continuousAt.continuousWithinAt
  · exact fun T hT => (hF T (interior_subset hT)).differentiableAt.differentiableWithinAt
  · intro T hT; rw [(hF T (interior_subset hT)).deriv]; exact hf T (interior_subset hT)

lemma danti (F f : ℝ → ℝ) (a b : ℝ) (hF : ∀ T ∈ Set.Icc a b, HasDerivAt F (f T) T)
    (hf : ∀ T ∈ Set.Icc a b, f T ≤ 0) : AntitoneOn F (Set.Icc a b) := by
  apply antitoneOn_of_deriv_nonpos (convex_Icc a b)
  · exact fun T hT => (hF T hT).continuousAt.continuousWithinAt
  · exact fun T hT => (hF T (interior_subset hT)).differentiableAt.differentiableWithinAt
  · intro T hT; rw [(hF T (interior_subset hT)).deriv]; exact hf T (interior_subset hT)

lemma dstrict (F f : ℝ → ℝ) (a b : ℝ) (hF : ∀ T ∈ Set.Icc a b, HasDerivAt F (f T) T)
    (hf : ∀ T ∈ Set.Icc a b, 0 < f T) : StrictMonoOn F (Set.Icc a b) := by
  apply strictMonoOn_of_deriv_pos (convex_Icc a b)
  · exact fun T hT => (hF T hT).continuousAt.continuousWithinAt
  · intro T hT; rw [(hF T (interior_subset hT)).deriv]; exact hf T (interior_subset hT)

lemma lin_deriv (F : ℝ → ℝ) (d c T : ℝ) (h : HasDerivAt F d T) :
    HasDerivAt (fun y => F y + c*y) (d + c) T :=
  (h.add ((hasDerivAt_id T).const_mul c)).congr_deriv (by simp)

/-- Taylor-polynomial upper bound for exp on [0,4] (from `Real.exp_bound`, 7 terms, applied to x/4 and raised to the 4th power) -/
noncomputable def Q (x : ℝ) : ℝ := ((∑ m ∈ Finset.range 7, (x/4)^m / (m.factorial:ℝ)) + (x/4)^7 * (8/35280))^4

lemma exp_ub (x : ℝ) (h0 : 0 ≤ x) (h4 : x ≤ 4) : Real.exp x ≤ Q x := by
  have hy : |x/4| ≤ 1 := by rw [abs_le]; constructor <;> linarith
  have hb := Real.exp_bound hy (n := 7) (by norm_num)
  rw [abs_of_nonneg (by linarith : 0 ≤ x/4)] at hb
  have h1 := (abs_le.mp hb).2
  have e : ((Nat.succ 7 : ℕ) : ℝ) / ((Nat.factorial 7 : ℝ) * (7:ℕ)) = 8/35280 := by norm_num [Nat.factorial]
  rw [e] at h1
  have e2 : Real.exp x = Real.exp (x/4) ^ 4 := by
    rw [← Real.exp_nat_mul]; congr 1; push_cast; ring
  rw [e2]; exact pow_le_pow_left₀ (Real.exp_pos _).le (by linarith) 4

/-! ### Laliberté (2009) NaCl correlation: empirical inputs (exposed) and derived consequences -/
noncomputable def Mnacl : ℝ := 58.44
noncomputable def a1 : ℝ := -0.0693559668993322
noncomputable def a2 : ℝ := -0.0782134167486952
noncomputable def a3 : ℝ := 3.84798479408635
noncomputable def a4 : ℝ := -11.2762109247072
noncomputable def a5 : ℝ := 8.73187698542672
noncomputable def a6 : ℝ := 1.81245930472755

/-- apparent molar heat capacity of NaCl in aqueous solution, J/(K mol): Mnacl * (Laliberté solute heat capacity
    Cp_i in J/(g K)); w = salt mass fraction, T in C -/
noncomputable def PhiC (w T : ℝ) : ℝ :=
  Mnacl * (a1 * Real.exp (a2*T + a3*Real.exp (0.01*T) + a4*w) + a5 * w ^ a6)

/-- Laliberté's mixing rule Cp_sol = w_w Cp_w + w_s Cp_i gives C_solution - C_water = ms*Cp_i = (ms/M)*(M*Cp_i):
    per mole of salt the heat-capacity change is M*Cp_i = PhiC. -/
lemma apparent_capacity (ms mw M cpw cpi : ℝ) (hM : 0 < M) (hs : 0 < ms + mw) :
    (ms+mw) * ((mw/(ms+mw)) * cpw + (ms/(ms+mw)) * cpi) - mw * cpw = (ms/M) * (M * cpi) := by
  field_simp; ring

lemma phi_mono_w (w T : ℝ) (h : 1/51 ≤ w) : PhiC (1/51) T ≤ PhiC w T := by
  unfold PhiC
  have h1 : Real.exp (a2*T + a3*Real.exp (0.01*T) + a4*w) ≤ Real.exp (a2*T + a3*Real.exp (0.01*T) + a4*(1/51)) :=
    Real.exp_le_exp.mpr (by unfold a4; linarith)
  have h2 : (1/51:ℝ) ^ a6 ≤ w ^ a6 := Real.rpow_le_rpow (by norm_num) h (by unfold a6; norm_num)
  have ha1 : a1 < 0 := by unfold a1; norm_num
  have ha5 : 0 < a5 := by unfold a5; norm_num
  have hM : 0 < Mnacl := by unfold Mnacl; norm_num
  exact mul_le_mul_of_nonneg_left (by nlinarith) hM.le

lemma phi_neg (w T : ℝ) (h1 : 1/51 ≤ w) (h2 : w ≤ 1/21) (t1 : 10 ≤ T) (t2 : T ≤ 101) : PhiC w T < 0 := by
  unfold PhiC
  have hw : 0 < w := by linarith
  have e1 : w ^ a6 ≤ w ^ ((3:ℝ)/2) :=
    Real.rpow_le_rpow_of_exponent_ge hw (by linarith) (by unfold a6; norm_num)
  have e2 : w ^ ((3:ℝ)/2) = w * Real.sqrt w := by
    rw [Real.sqrt_eq_rpow, show ((3:ℝ)/2) = 1 + 1/2 by norm_num, Real.rpow_add hw, Real.rpow_one]
  have e3 : Real.sqrt w ≤ 1/4 := by rw [Real.sqrt_le_left (by norm_num)]; nlinarith
  have e4 : w ^ a6 ≤ 1/84 := by have := Real.sqrt_nonneg w; nlinarith
  have q := Real.quadratic_le_exp_of_nonneg (x := 0.01*T) (by linarith)
  set E := Real.exp (0.01*T)
  have hα : 1.2 ≤ a2*T + a3*E + a4*w := by
    unfold a2 a3 a4
    nlinarith [mul_nonneg (by linarith : (0:ℝ) ≤ 101 - T) (by linarith : (0:ℝ) ≤ T - 10)]
  have hex : 2 ≤ Real.exp (a2*T + a3*E + a4*w) := by linarith [Real.add_one_le_exp (a2*T + a3*E + a4*w)]
  have hM : 0 < Mnacl := by unfold Mnacl; norm_num
  refine mul_neg_of_pos_of_neg hM ?_
  unfold a1 a5 at *
  nlinarith

/-- Φ_C is concave in T: its value at T lies above the chord between any a < b. -/
lemma phi_chord (w a b T : ℝ) (hab : a < b) (h1 : a ≤ T) (h2 : T ≤ b) :
    (b-a) * PhiC w T ≥ (b-T) * PhiC w a + (T-a) * PhiC w b := by
  set α : ℝ → ℝ := fun s => a2*s + a3*Real.exp (0.01*s) + a4*w with hαdef
  have ha3 : 0 < a3 := by unfold a3; norm_num
  have tang : ∀ s, Real.exp (α s) ≥ Real.exp (α T) * (1 + (a2 + a3*0.01*Real.exp (0.01*T)) * (s - T)) := by
    intro s
    have e1 : Real.exp (0.01*s) ≥ Real.exp (0.01*T) * (1 + 0.01*(s-T)) := by
      have : Real.exp (0.01*s) = Real.exp (0.01*T) * Real.exp (0.01*(s-T)) := by
        rw [← Real.exp_add]; congr 1; ring
      rw [this]
      nlinarith [Real.add_one_le_exp (0.01*(s-T)), Real.exp_pos (0.01*T)]
    have e2 : α s - α T ≥ (a2 + a3*0.01*Real.exp (0.01*T)) * (s - T) := by
      simp only [hαdef]; nlinarith
    have e3 : Real.exp (α s) = Real.exp (α T) * Real.exp (α s - α T) := by
      rw [← Real.exp_add]; congr 1; ring
    rw [e3]
    nlinarith [Real.add_one_le_exp (α s - α T), Real.exp_pos (α T)]
  have ta := tang a
  have tb := tang b
  have hg : (b-a) * Real.exp (α T) ≤ (b-T) * Real.exp (α a) + (T-a) * Real.exp (α b) := by
    nlinarith [mul_le_mul_of_nonneg_left ta (by linarith : (0:ℝ) ≤ b - T),
               mul_le_mul_of_nonneg_left tb (by linarith : (0:ℝ) ≤ T - a)]
  have ha1 : a1 < 0 := by unfold a1; norm_num
  have hM : 0 < Mnacl := by unfold Mnacl; norm_num
  unfold PhiC
  have : (b-a) * (a1 * Real.exp (α T)) ≥ (b-T) * (a1 * Real.exp (α a)) + (T-a) * (a1 * Real.exp (α b)) := by
    nlinarith
  simp only [hαdef] at this
  nlinarith [mul_pos hM (by linarith : (0:ℝ) < b - a)]

/-- trapezoid lower bound: if F' = f lies above its chord on [a,b] then F b - F a ≥ (b-a)(f a + f b)/2 -/
lemma trap (F f : ℝ → ℝ) (a b : ℝ) (hab : a < b)
    (hF : ∀ T ∈ Set.Icc a b, HasDerivAt F (f T) T)
    (hc : ∀ T ∈ Set.Icc a b, (b-a) * f T ≥ (b-T) * f a + (T-a) * f b) :
    (b-a)*(f a + f b)/2 ≤ F b - F a := by
  have hba : 0 < b - a := by linarith
  have hd : ∀ T ∈ Set.Icc a b, HasDerivAt
      (fun T => F T - F a - (f a * (T-a) + (T-a)^2 * (f b - f a) / (2*(b-a)))) (f T - ((b-T) * f a + (T-a) * f b)/(b-a)) T := by
    intro T hT
    have u : HasDerivAt (fun T : ℝ => T - a) 1 T := (hasDerivAt_id T).sub_const a
    have v := (u.const_mul (f a)).add (((u.pow 2).mul_const (f b - f a)).div_const (2*(b-a)))
    convert (hF T hT).sub_const (F a) |>.sub v using 1
    field_simp; ring
  have := dmono _ _ a b hd (fun T hT => by
    have := hc T hT
    rw [sub_nonneg, div_le_iff₀ hba]; linarith) ⟨le_rfl, hab.le⟩ ⟨hab.le, le_rfl⟩ hab.le
  simp only at this
  have e : (f a * (b-a) + (b-a)^2 * (f b - f a) / (2*(b-a))) = (b-a)*(f a + f b)/2 := by
    field_simp; ring
  simp at this
  nlinarith [e]

lemma node (w T V β U L : ℝ) (h : 1/51 ≤ w)
    (hh : 0 ≤ T ∧ T ≤ 400 ∧ Q (0.01*T) ≤ V ∧ a2*T + a3*V + a4*(1/51) ≤ β ∧ 0 ≤ β ∧ β ≤ 4 ∧ Q β ≤ U ∧
      -L ≤ Mnacl*a1*U) : -L ≤ PhiC w T := by
  obtain ⟨h0, h4, hV, hb, hb0, hb4, hU, hL⟩ := hh
  refine le_trans ?_ (phi_mono_w w T h)
  have e1 : Real.exp (0.01*T) ≤ V := (exp_ub _ (by linarith) (by linarith)).trans hV
  have ha3 : 0 < a3 := by unfold a3; norm_num
  have e2 : Real.exp (a2*T + a3*Real.exp (0.01*T) + a4*(1/51)) ≤ U :=
    (Real.exp_le_exp.mpr (by nlinarith [mul_le_mul_of_nonneg_left e1 ha3.le])).trans ((exp_ub β hb0 hb4).trans hU)
  have h5 : 0 ≤ a5 * (1/51:ℝ)^a6 := mul_nonneg (by unfold a5; norm_num) (Real.rpow_nonneg (by norm_num) _)
  have ha1 : a1 < 0 := by unfold a1; norm_num
  have hM : 0 < Mnacl := by unfold Mnacl; norm_num
  unfold PhiC
  nlinarith [mul_le_mul_of_nonneg_left e2 (mul_pos hM (neg_pos.mpr ha1)).le, mul_nonneg hM.le h5]

lemma nodes (w : ℝ) (h : 1/51 ≤ w) : -(104.51:ℝ) ≤ PhiC w 10 ∧ -(74.77:ℝ) ≤ PhiC w 20 ∧ -(56.07:ℝ) ≤ PhiC w 30 ∧ -(44.29:ℝ) ≤ PhiC w 40 ∧ -(37.06:ℝ) ≤ PhiC w 50 ∧ -(33.04:ℝ) ≤ PhiC w 60 ∧ -(31.6:ℝ) ≤ PhiC w 70 ∧ -(32.65:ℝ) ≤ PhiC w 80 ∧ -(36.75:ℝ) ≤ PhiC w 90 ∧ -(45.49:ℝ) ≤ PhiC w 100 ∧ -(46.73:ℝ) ≤ PhiC w 101 :=
  ⟨node w 10 1.10519 3.2497 25.784 104.51 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1]),
   node w 20 1.22142 2.9148 18.447 74.77 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1]),
   node w 30 1.34987 2.6269 13.832 56.07 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1]),
   node w 40 1.49184 2.3911 10.927 44.29 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1]),
   node w 50 1.64874 2.2127 9.142 37.06 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1]),
   node w 60 1.82213 2.0978 8.15 33.04 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1]),
   node w 70 2.01377 2.0531 7.794 31.6 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1]),
   node w 80 2.22556 2.0859 8.053 32.65 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1]),
   node w 90 2.45962 2.2044 9.066 36.75 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1]),
   node w 100 2.7183 2.4177 11.222 45.49 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1]),
   node w 101 2.74562 2.4446 11.527 46.73 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1])⟩

lemma piece (w : ℝ) (Ph : ℝ → ℝ) (a b La Lb : ℝ) (hab : a < b) (ha : 10 ≤ a) (hb : b ≤ 101)
    (hPh : ∀ T ∈ Set.Icc (10:ℝ) 101, HasDerivAt Ph (PhiC w T) T)
    (hA : -La ≤ PhiC w a) (hB : -Lb ≤ PhiC w b) : -((b-a)*(La+Lb)/2) ≤ Ph b - Ph a := by
  have := trap Ph (PhiC w) a b hab (fun T hT => hPh T ⟨by linarith [hT.1], by linarith [hT.2]⟩)
    (fun T hT => phi_chord w a b T hab hT.1 hT.2)
  nlinarith

lemma phi_floor (w T : ℝ) (h1 : 1/51 ≤ w) (t1 : 10 ≤ T) (t2 : T ≤ 101) : -105 ≤ PhiC w T := by
  have c := phi_chord w 10 101 T (by norm_num) t1 t2
  obtain ⟨n10, -, -, -, -, -, -, -, -, -, n101⟩ := nodes w h1
  nlinarith [mul_nonneg (by linarith : (0:ℝ) ≤ 101 - T) (by linarith : (0:ℝ) ≤ 105 - 104.51),
             mul_nonneg (by linarith : (0:ℝ) ≤ T - 10) (by linarith : (0:ℝ) ≤ 46.73 - 0)]

/-- Total change of apparent molar enthalpy of NaCl from 20 C to 101 C is at least -3363 J/mol. -/
lemma ph_drop (w : ℝ) (Ph : ℝ → ℝ) (h1 : 1/51 ≤ w)
    (hPh : ∀ T ∈ Set.Icc (10:ℝ) 101, HasDerivAt Ph (PhiC w T) T) : -3363 ≤ Ph 101 - Ph 20 := by
  obtain ⟨n10, n20, n30, n40, n50, n60, n70, n80, n90, n100, n101⟩ := nodes w h1
  have p0 := piece w Ph 20 30 _ _ (by norm_num) (by norm_num) (by norm_num) hPh n20 n30
  have p1 := piece w Ph 30 40 _ _ (by norm_num) (by norm_num) (by norm_num) hPh n30 n40
  have p2 := piece w Ph 40 50 _ _ (by norm_num) (by norm_num) (by norm_num) hPh n40 n50
  have p3 := piece w Ph 50 60 _ _ (by norm_num) (by norm_num) (by norm_num) hPh n50 n60
  have p4 := piece w Ph 60 70 _ _ (by norm_num) (by norm_num) (by norm_num) hPh n60 n70
  have p5 := piece w Ph 70 80 _ _ (by norm_num) (by norm_num) (by norm_num) hPh n70 n80
  have p6 := piece w Ph 80 90 _ _ (by norm_num) (by norm_num) (by norm_num) hPh n80 n90
  have p7 := piece w Ph 90 100 _ _ (by norm_num) (by norm_num) (by norm_num) hPh n90 n100
  have p8 := piece w Ph 100 101 _ _ (by norm_num) (by norm_num) (by norm_num) hPh n100 n101
  norm_num at *
  linarith

/-! ## Heating-time model (accepted Newtonian lumped model; state variable = total enthalpy H) -/

/-- Time to reach the boiling point when salt is added while the pure water is at Ta: pure-water branch up to
    total enthalpy Hw Ta, then solution branch from the same enthalpy up to Hs Tb.
    τw, τs = elapsed time as a function of total enthalpy on the two branches. -/
def boilTime (τw τs Hw Hs : ℝ → ℝ) (T0 Tb Ta : ℝ) : ℝ :=
  (τw (Hw Ta) - τw (Hw T0)) + (τs (Hs Tb) - τs (Hw Ta))

theorem timing_core (P h Tr Tl Tb : ℝ) (Hw Hs Tw Ts τw τs : ℝ → ℝ)
    (hh : 0 ≤ h) (hP : h*(Tb-Tr) < P) (hl : Tl ≤ Tr) (hr : Tr ≤ Tb)
    (hHw : StrictMonoOn Hw (Set.Icc Tl Tb)) (hHs : StrictMonoOn Hs (Set.Icc Tl Tb))
    (hTw : ∀ H ∈ Set.Icc (Hw Tr) (Hw Tb), Tw H ∈ Set.Icc Tl Tb ∧ Hw (Tw H) = H)
    (hTs : ∀ H ∈ Set.Icc (Hs Tl) (Hs Tb), Ts H ∈ Set.Icc Tl Tb ∧ Hs (Ts H) = H)
    (hτw : ∀ H ∈ Set.Icc (Hw Tr) (Hw Tb), HasDerivAt τw (1/(P - h*(Tw H - Tr))) H)
    (hτs : ∀ H ∈ Set.Icc (Hs Tl) (Hs Tb), HasDerivAt τs (1/(P - h*(Ts H - Tr))) H)
    (hcool : ∀ x ∈ Set.Icc Tr Tb, Hw x ≤ Hs x) (hlow : Hs Tl ≤ Hw Tr) :
    ∀ Ta ∈ Set.Icc Tr Tb, boilTime τw τs Hw Hs Tr Tb Tr ≤ boilTime τw τs Hw Hs Tr Tb Ta := by
  intro Ta hTa
  have hTrI : Tr ∈ Set.Icc Tl Tb := ⟨hl, hr⟩
  have hTaI : Ta ∈ Set.Icc Tl Tb := ⟨hl.trans hTa.1, hTa.2⟩
  have hTbI : Tb ∈ Set.Icc Tl Tb := ⟨hl.trans hr, le_rfl⟩
  have w1 : Hw Tr ≤ Hw Ta := hHw.monotoneOn hTrI hTaI hTa.1
  have w2 : Hw Ta ≤ Hw Tb := hHw.monotoneOn hTaI hTbI hTa.2
  have s1 : Hw Tb ≤ Hs Tb := hcool Tb ⟨hr, le_rfl⟩
  have hpos : ∀ x, x ≤ Tb → 0 < P - h*(x - Tr) := by
    intro x hx
    have : h*(x-Tr) ≤ h*(Tb-Tr) := mul_le_mul_of_nonneg_left (by linarith) hh
    linarith
  have key : ∀ H ∈ Set.Icc (Hw Tr) (Hw Ta), 1/(P - h*(Tw H - Tr)) - 1/(P - h*(Ts H - Tr)) ≥ 0 := by
    intro H hH
    obtain ⟨hx, hxH⟩ := hTw H ⟨hH.1, hH.2.trans w2⟩
    obtain ⟨hy, hyH⟩ := hTs H ⟨hlow.trans hH.1, hH.2.trans (w2.trans s1)⟩
    have hxr : Tr ≤ Tw H := by
      by_contra hc
      have := hHw hx hTrI (not_le.mp hc)
      rw [hxH] at this
      linarith [hH.1]
    have hc := hcool _ ⟨hxr, hx.2⟩
    rw [hxH] at hc
    have hyx : Ts H ≤ Tw H := by
      by_contra hcon
      have := hHs hx hy (not_le.mp hcon)
      rw [hyH] at this
      linarith
    have := mul_le_mul_of_nonneg_left (by linarith : Ts H - Tr ≤ Tw H - Tr) hh
    have := one_div_le_one_div_of_le (hpos _ hx.2) (by linarith : P - h*(Tw H - Tr) ≤ P - h*(Ts H - Tr))
    linarith
  have mono := dmono (fun H => τw H - τs H) _ (Hw Tr) (Hw Ta)
    (fun H hH => (hτw H ⟨hH.1, hH.2.trans w2⟩).sub (hτs H ⟨hlow.trans hH.1, hH.2.trans (w2.trans s1)⟩)) key
  have := mono ⟨le_rfl, w1⟩ ⟨w1, le_rfl⟩ w1
  simp only [boilTime] at this ⊢
  linarith

/-- sanity: the inverse temperature functions Tw, Ts exist (intermediate value theorem). -/
lemma inverse_exists (H : ℝ → ℝ) (Tl Tb : ℝ) (hle : Tl ≤ Tb) (hc : ContinuousOn H (Set.Icc Tl Tb)) :
    ∀ y ∈ Set.Icc (H Tl) (H Tb), ∃ T ∈ Set.Icc Tl Tb, H T = y := fun y hy => by
  obtain ⟨T, hT, e⟩ := intermediate_value_Icc hle hc hy
  exact ⟨T, hT, e⟩

/-! ## Thermodynamics of dissolving NaCl(cr) (at 20 C) in the pot -/

/-- thermochemical calorie in J (Parker 1965 reports cal) -/
noncomputable def cal : ℝ := 4.184

/-- moles of NaCl for salt/water mass ratio r, in the 0.998 kg of water of 1 L at 20 C -/
noncomputable def nSalt (r : ℝ) : ℝ := 1000*0.998*r/Mnacl

/-- Enthalpy (J) of pot+solution at T, measured from pot+pure water plus crystalline NaCl at 20 C.
    Definition of the apparent molar enthalpy Ph of NaCl in the final solution: solution = (same water at T) + n*Ph(T);
    the salt started as crystal with enthalpy Hcr 20. -/
def solEnthalpy (n : ℝ) (Hw Ph Hcr : ℝ → ℝ) (T : ℝ) : ℝ := Hw T + n * (Ph T - Hcr 20)

/-- Physical premises (empirical data and definitions), see the review packet. -/
structure Premises (r : ℝ) (Cw Hw Ph Hcr Cpcr : ℝ → ℝ) (hinf phiL : ℝ) : Prop where
  r_lo : 1/50 ≤ r
  r_hi : r ≤ 1/20
  hw_deriv : ∀ T ∈ Set.Icc (10:ℝ) 101, HasDerivAt Hw (Cw T) T
  hw_cap : ∀ T ∈ Set.Icc (10:ℝ) 101, 4100 ≤ Cw T
  ph_deriv : ∀ T ∈ Set.Icc (10:ℝ) 101, HasDerivAt Ph (PhiC (r/(1+r)) T) T
  cr_deriv : ∀ T ∈ Set.Icc (20:ℝ) 25, HasDerivAt Hcr (Cpcr T) T
  cr_cap : ∀ T ∈ Set.Icc (20:ℝ) 25, 40 ≤ Cpcr T ∧ Cpcr T ≤ 100
  hinf_b : 923*cal ≤ hinf ∧ hinf ≤ 933*cal
  phiL_b : -41*cal ≤ phiL ∧ phiL ≤ 100*cal
  diss25 : Ph 25 - Hcr 25 = hinf + phiL

lemma w_range (r : ℝ) (h1 : 1/50 ≤ r) (h2 : r ≤ 1/20) : 1/51 ≤ r/(1+r) ∧ r/(1+r) ≤ 1/21 := by
  constructor
  · rw [le_div_iff₀ (by linarith)]; linarith
  · rw [div_le_iff₀ (by linarith)]; linarith

lemma n_range (r : ℝ) (h1 : 1/50 ≤ r) (h2 : r ≤ 1/20) : 0.34 ≤ nSalt r ∧ nSalt r ≤ 0.86 := by
  unfold nSalt Mnacl
  constructor
  · rw [le_div_iff₀ (by norm_num)]; linarith
  · rw [div_le_iff₀ (by norm_num)]; linarith

theorem thermo_facts (r Tb : ℝ) (Cw Hw Ph Hcr Cpcr : ℝ → ℝ) (hinf phiL : ℝ)
    (hp : Premises r Cw Hw Ph Hcr Cpcr hinf phiL) (hT2 : Tb ≤ 101) :
    StrictMonoOn Hw (Set.Icc 10 Tb) ∧ StrictMonoOn (solEnthalpy (nSalt r) Hw Ph Hcr) (Set.Icc 10 Tb) ∧
    (∀ x ∈ Set.Icc (20:ℝ) Tb, Hw x ≤ solEnthalpy (nSalt r) Hw Ph Hcr x) ∧
    solEnthalpy (nSalt r) Hw Ph Hcr 10 ≤ Hw 20 := by
  obtain ⟨hw1, hw2⟩ := w_range r hp.r_lo hp.r_hi
  obtain ⟨hn1, hn2⟩ := n_range r hp.r_lo hp.r_hi
  set w := r/(1+r)
  set n := nSalt r
  have hs1 : ∀ T ∈ Set.Icc (10:ℝ) Tb, T ∈ Set.Icc (10:ℝ) 101 := fun T hT => ⟨hT.1, hT.2.trans hT2⟩
  have hs2 : ∀ T ∈ Set.Icc (20:ℝ) 25, T ∈ Set.Icc (10:ℝ) 101 := fun T hT => ⟨by linarith [hT.1], by linarith [hT.2]⟩
  have hs3 : ∀ T ∈ Set.Icc (10:ℝ) 20, T ∈ Set.Icc (10:ℝ) 101 := fun T hT => ⟨hT.1, by linarith [hT.2]⟩
  have hfl : ∀ T ∈ Set.Icc (10:ℝ) 101, -105 ≤ PhiC w T := fun T hT => phi_floor w T hw1 hT.1 hT.2
  have hng : ∀ T ∈ Set.Icc (10:ℝ) 101, PhiC w T ≤ 0 := fun T hT => (phi_neg w T hw1 hw2 hT.1 hT.2).le
  have hWs := dstrict Hw Cw 10 Tb (fun T hT => hp.hw_deriv T (hs1 T hT))
    (fun T hT => by linarith [hp.hw_cap T (hs1 T hT)])
  have hHs : ∀ T ∈ Set.Icc (10:ℝ) 101, HasDerivAt (solEnthalpy n Hw Ph Hcr) (Cw T + n * PhiC w T) T :=
    fun T hT => (hp.hw_deriv T hT).add (((hp.ph_deriv T hT).sub_const (Hcr 20)).const_mul n)
  have hSs := dstrict _ _ 10 Tb (fun T hT => hHs T (hs1 T hT))
    (fun T hT => by nlinarith [hp.hw_cap T (hs1 T hT), hfl T (hs1 T hT)])
  -- Kirchhoff: dissolution enthalpy Ph T - Hcr T between 20 and 25 C
  have hd : ∀ T ∈ Set.Icc (20:ℝ) 25, HasDerivAt (fun T => Ph T - Hcr T) (PhiC w T - Cpcr T) T :=
    fun T hT => (hp.ph_deriv T (hs2 T hT)).sub (hp.cr_deriv T hT)
  have hlow20 := danti _ _ 20 25 (fun T hT => lin_deriv _ _ 40 T (hd T hT))
    (fun T hT => by linarith [hng T (hs2 T hT), (hp.cr_cap T hT).1]) ⟨le_rfl, by norm_num⟩ ⟨by norm_num, le_rfl⟩ (by norm_num)
  have hup20 := dmono _ _ 20 25 (fun T hT => lin_deriv _ _ 205 T (hd T hT))
    (fun T hT => by linarith [hfl T (hs2 T hT), (hp.cr_cap T hT).2]) ⟨le_rfl, by norm_num⟩ ⟨by norm_num, le_rfl⟩ (by norm_num)
  have hup10 := dmono _ _ 10 20 (fun T hT => lin_deriv _ _ 105 T (hp.ph_deriv T (hs3 T hT)))
    (fun T hT => by linarith [hfl T (hs3 T hT)]) ⟨le_rfl, by norm_num⟩ ⟨by norm_num, le_rfl⟩ (by norm_num)
  have hup4 := dmono _ _ 10 20 (fun T hT => lin_deriv _ _ (-4100) T (hp.hw_deriv T (hs3 T hT)))
    (fun T hT => by linarith [hp.hw_cap T (hs3 T hT)]) ⟨le_rfl, by norm_num⟩ ⟨by norm_num, le_rfl⟩ (by norm_num)
  simp only at hlow20 hup20 hup10 hup4
  -- Parker's numbers: dissolution enthalpy at 20 C
  obtain ⟨hi1, hi2⟩ := hp.hinf_b
  obtain ⟨hl1, hl2⟩ := hp.phiL_b
  have hcal : cal = 4.184 := rfl
  rw [hcal] at hi1 hi2 hl1 hl2
  have hd25 := hp.diss25
  refine ⟨hWs, hSs, fun x hx => ?_, ?_⟩
  · have hanti2 := danti Ph _ 10 101 hp.ph_deriv hng
    have h1 : Ph 101 ≤ Ph x := hanti2 ⟨by linarith [hx.1], by linarith [hx.2]⟩ ⟨by norm_num, le_rfl⟩ (by linarith [hx.2])
    have h2 := ph_drop w Ph hw1 hp.ph_deriv
    unfold solEnthalpy
    nlinarith
  · unfold solEnthalpy
    nlinarith

/-! ## Conclusions (named constants of the regime; numerals live in definitions) -/

/-- starting / room temperature of the water (C) -/
noncomputable def Tr : ℝ := 20
/-- lowest temperature considered (C) -/
noncomputable def Tmin : ℝ := 10
/-- highest admissible boiling temperature of the solution (C) -/
noncomputable def Tcap : ℝ := 101
/-- regime of the salt/water mass ratio r: 20 to 50 g NaCl per kg water -/
noncomputable def rLo : ℝ := 1/50
noncomputable def rHi : ℝ := 1/20
/-- lower bound for Phi_C (J/K/mol) and for the heat-capacity change n*Phi_C (J/K) -/
noncomputable def PhiFloor : ℝ := 105
noncomputable def CsFloor : ℝ := 91

/-- CONCLUSION 1 (heat capacity). For 20-50 g NaCl per kg water and 10-101 C the apparent molar heat capacity of NaCl
    in solution is negative and >= -105 J/(K mol); dissolving the salt LOWERS the total heat capacity of the pot
    contents by n*Phi in (-91, 0) J/K (< 2.2 % of the ~4.2 kJ/K of the water). -/
theorem cs_sign : ∀ (r T : ℝ), rLo ≤ r → r ≤ rHi → Tmin ≤ T → T ≤ Tcap →
    -PhiFloor ≤ PhiC (r/(1+r)) T ∧ PhiC (r/(1+r)) T < 0 ∧
    -CsFloor < nSalt r * PhiC (r/(1+r)) T ∧ nSalt r * PhiC (r/(1+r)) T < 0 := by
  intro r T h1 h2 t1 t2
  unfold rLo rHi Tmin Tcap PhiFloor CsFloor at *
  obtain ⟨w1, w2⟩ := w_range r h1 h2
  obtain ⟨n1, n2⟩ := n_range r h1 h2
  have f := phi_floor _ T w1 t1 t2
  have g := phi_neg _ T w1 w2 t1 t2
  refine ⟨f, g, ?_, ?_⟩ <;> nlinarith


/-- Tw is the temperature as a function of total enthalpy H on the branch with enthalpy-temperature relation Hf
    (Hf (T H) = H, T H in [Tl,Tb]), for H in [a,b]. -/
def InverseOn (Hf T : ℝ → ℝ) (Tl Tb a b : ℝ) : Prop := ∀ H ∈ Set.Icc a b, T H ∈ Set.Icc Tl Tb ∧ Hf (T H) = H

/-- τ is the elapsed time as a function of total enthalpy on a branch with temperature profile T(H): since
    dH/dt = P - h (T - Tr) > 0, dτ/dH = 1/(P - h (T(H) - Tr)). -/
def TimeProfile (P h Tr : ℝ) (τ T : ℝ → ℝ) (a b : ℝ) : Prop :=
  ∀ H ∈ Set.Icc a b, HasDerivAt τ (1/(P - h*(T H - Tr))) H

/-- CONCLUSION 2 (answer). Salt added at the very start (water at Tr = 20 C) gives a boiling time no longer than salt
    added once the water has reached any later temperature Ta <= Tb. -/
theorem answer_theorem : ∀ (r P h Tb : ℝ) (Cw Hw Ph Hcr Cpcr Tw Ts τw τs : ℝ → ℝ) (hinf phiL : ℝ),
    Premises r Cw Hw Ph Hcr Cpcr hinf phiL → Tr ≤ Tb → Tb ≤ Tcap → 0 ≤ h → h*(Tb-Tr) < P →
    InverseOn Hw Tw Tmin Tb (Hw Tr) (Hw Tb) →
    InverseOn (solEnthalpy (nSalt r) Hw Ph Hcr) Ts Tmin Tb
      (solEnthalpy (nSalt r) Hw Ph Hcr Tmin) (solEnthalpy (nSalt r) Hw Ph Hcr Tb) →
    TimeProfile P h Tr τw Tw (Hw Tr) (Hw Tb) →
    TimeProfile P h Tr τs Ts (solEnthalpy (nSalt r) Hw Ph Hcr Tmin) (solEnthalpy (nSalt r) Hw Ph Hcr Tb) →
    ∀ Ta ∈ Set.Icc Tr Tb,
      boilTime τw τs Hw (solEnthalpy (nSalt r) Hw Ph Hcr) Tr Tb Tr
        ≤ boilTime τw τs Hw (solEnthalpy (nSalt r) Hw Ph Hcr) Tr Tb Ta := by
  intro r P h Tb Cw Hw Ph Hcr Cpcr Tw Ts τw τs hinf phiL hp hT1 hT2 hh hP hTw hTs hτw hτs
  unfold InverseOn at hTw hTs
  unfold TimeProfile at hτw hτs
  unfold Tr Tmin Tcap at *
  obtain ⟨m1, m2, c1, c2⟩ := thermo_facts r Tb Cw Hw Ph Hcr Cpcr hinf phiL hp hT2
  exact timing_core P h 20 10 Tb Hw (solEnthalpy (nSalt r) Hw Ph Hcr) Tw Ts τw τs hh hP (by norm_num) hT1
    m1 m2 hTw hTs hτw hτs c1 c2
attempt-005/extracted.lean
import Mathlib.Analysis.SpecialFunctions.Log.Deriv
import Mathlib.Analysis.SpecialFunctions.Pow.Real
import Mathlib.Analysis.SpecialFunctions.ExpDeriv
import Mathlib.Analysis.Calculus.MeanValue

noncomputable def Mnacl : ℝ := 58.44

noncomputable def a1 : ℝ := -0.0693559668993322

noncomputable def a2 : ℝ := -0.0782134167486952

noncomputable def a3 : ℝ := 3.84798479408635

noncomputable def a4 : ℝ := -11.2762109247072

noncomputable def a5 : ℝ := 8.73187698542672

noncomputable def a6 : ℝ := 1.81245930472755

/-- apparent molar heat capacity of NaCl in aqueous solution, J/(K mol): Mnacl * (Laliberté solute heat capacity
    Cp_i in J/(g K)); w = salt mass fraction, T in C -/
noncomputable def PhiC (w T : ℝ) : ℝ :=
  Mnacl * (a1 * Real.exp (a2*T + a3*Real.exp (0.01*T) + a4*w) + a5 * w ^ a6)

/-- Time to reach the boiling point when salt is added while the pure water is at Ta: pure-water branch up to
    total enthalpy Hw Ta, then solution branch from the same enthalpy up to Hs Tb.
    τw, τs = elapsed time as a function of total enthalpy on the two branches. -/
def boilTime (τw τs Hw Hs : ℝ → ℝ) (T0 Tb Ta : ℝ) : ℝ :=
  (τw (Hw Ta) - τw (Hw T0)) + (τs (Hs Tb) - τs (Hw Ta))

/-- thermochemical calorie in J (Parker 1965 reports cal) -/
noncomputable def cal : ℝ := 4.184

/-- moles of NaCl for salt/water mass ratio r, in the 0.998 kg of water of 1 L at 20 C -/
noncomputable def nSalt (r : ℝ) : ℝ := 1000*0.998*r/Mnacl

/-- Enthalpy (J) of pot+solution at T, measured from pot+pure water plus crystalline NaCl at 20 C.
    Definition of the apparent molar enthalpy Ph of NaCl in the final solution: solution = (same water at T) + n*Ph(T);
    the salt started as crystal with enthalpy Hcr 20. -/
def solEnthalpy (n : ℝ) (Hw Ph Hcr : ℝ → ℝ) (T : ℝ) : ℝ := Hw T + n * (Ph T - Hcr 20)

/-- Physical premises (empirical data and definitions), see the review packet. -/
structure Premises (r : ℝ) (Cw Hw Ph Hcr Cpcr : ℝ → ℝ) (hinf phiL : ℝ) : Prop where
  r_lo : 1/50 ≤ r
  r_hi : r ≤ 1/20
  hw_deriv : ∀ T ∈ Set.Icc (10:ℝ) 101, HasDerivAt Hw (Cw T) T
  hw_cap : ∀ T ∈ Set.Icc (10:ℝ) 101, 4100 ≤ Cw T
  ph_deriv : ∀ T ∈ Set.Icc (10:ℝ) 101, HasDerivAt Ph (PhiC (r/(1+r)) T) T
  cr_deriv : ∀ T ∈ Set.Icc (20:ℝ) 25, HasDerivAt Hcr (Cpcr T) T
  cr_cap : ∀ T ∈ Set.Icc (20:ℝ) 25, 40 ≤ Cpcr T ∧ Cpcr T ≤ 100
  hinf_b : 923*cal ≤ hinf ∧ hinf ≤ 933*cal
  phiL_b : -41*cal ≤ phiL ∧ phiL ≤ 100*cal
  diss25 : Ph 25 - Hcr 25 = hinf + phiL

/-- starting / room temperature of the water (C) -/
noncomputable def Tr : ℝ := 20

/-- lowest temperature considered (C) -/
noncomputable def Tmin : ℝ := 10

/-- highest admissible boiling temperature of the solution (C) -/
noncomputable def Tcap : ℝ := 101

/-- regime of the salt/water mass ratio r: 20 to 50 g NaCl per kg water -/
noncomputable def rLo : ℝ := 1/50

noncomputable def rHi : ℝ := 1/20

/-- lower bound for Phi_C (J/K/mol) and for the heat-capacity change n*Phi_C (J/K) -/
noncomputable def PhiFloor : ℝ := 105

noncomputable def CsFloor : ℝ := 91

/-- Tw is the temperature as a function of total enthalpy H on the branch with enthalpy-temperature relation Hf
    (Hf (T H) = H, T H in [Tl,Tb]), for H in [a,b]. -/
def InverseOn (Hf T : ℝ → ℝ) (Tl Tb a b : ℝ) : Prop := ∀ H ∈ Set.Icc a b, T H ∈ Set.Icc Tl Tb ∧ Hf (T H) = H

/-- τ is the elapsed time as a function of total enthalpy on a branch with temperature profile T(H): since
    dH/dt = P - h (T - Tr) > 0, dτ/dH = 1/(P - h (T(H) - Tr)). -/
def TimeProfile (P h Tr : ℝ) (τ T : ℝ → ℝ) (a b : ℝ) : Prop :=
  ∀ H ∈ Set.Icc a b, HasDerivAt τ (1/(P - h*(T H - Tr))) H


/-! Conclusions (statements of the proved theorems). -/
def conclusion_cs_sign : Prop := ∀ (r T : ℝ), rLo ≤ r → r ≤ rHi → Tmin ≤ T → T ≤ Tcap → -PhiFloor ≤ PhiC (r/(1+r)) T ∧ PhiC (r/(1+r)) T < 0 ∧ -CsFloor < nSalt r * PhiC (r/(1+r)) T ∧ nSalt r * PhiC (r/(1+r)) T < 0
def conclusion_answer_theorem : Prop := ∀ (r P h Tb : ℝ) (Cw Hw Ph Hcr Cpcr Tw Ts τw τs : ℝ → ℝ) (hinf phiL : ℝ), Premises r Cw Hw Ph Hcr Cpcr hinf phiL → Tr ≤ Tb → Tb ≤ Tcap → 0 ≤ h → h*(Tb-Tr) < P → InverseOn Hw Tw Tmin Tb (Hw Tr) (Hw Tb) → InverseOn (solEnthalpy (nSalt r) Hw Ph Hcr) Ts Tmin Tb (solEnthalpy (nSalt r) Hw Ph Hcr Tmin) (solEnthalpy (nSalt r) Hw Ph Hcr Tb) → TimeProfile P h Tr τw Tw (Hw Tr) (Hw Tb) → TimeProfile P h Tr τs Ts (solEnthalpy (nSalt r) Hw Ph Hcr Tmin) (solEnthalpy (nSalt r) Hw Ph Hcr Tb) → ∀ Ta ∈ Set.Icc Tr Tb, boilTime τw τs Hw (solEnthalpy (nSalt r) Hw Ph Hcr) Tr Tb Tr ≤ boilTime τw τs Hw (solEnthalpy (nSalt r) Hw Ph Hcr) Tr Tb Ta
checks/07773642358d4d4197d0265cdf13591c/submitted.lean
import Mathlib.Analysis.SpecialFunctions.Log.Deriv
import Mathlib.Analysis.SpecialFunctions.Pow.Real
import Mathlib.Analysis.SpecialFunctions.ExpDeriv
import Mathlib.Analysis.Calculus.MeanValue

/-! # Salt timing for 1 L of water at 20 C: when should NaCl be added? (cycle 6)

## REVIEW PACKET
SUPPLIED BY THE USER: 1 L water at 20 C in a pot on a stove; salt will be added; goal = salted water reaches
its boiling point soonest; salt amount, pot, heater, room unspecified.

ACCEPTED EARLIER (kept): lumped Newtonian heating dH/dt = P - h (T - 20), constant stove power P, no evaporation,
salt (crystal at 20 C) dissolves instantly at the moment of addition (total enthalpy conserved).

MODEL CHOICES / ASSUMPTIONS (all appear as `Premises` fields or theorem hypotheses):
 * NaCl; r = salt mass / water mass in [1/50, 1/20] (20-50 g in the 0.998 kg of water of 1 L); mass fraction
   w = r/(1+r) (conversion proved in `w_range`), n = 1000*0.998*r/58.44 mol (`nSalt`, bounds in `n_range`).
 * Pot+water enthalpy Hw(T) has heat capacity Cw(T) >= 4100 J/K on 10..101 C (0.998 kg water >= 4.17 kJ/K; pot adds).
 * Apparent molar heat capacity of NaCl in the final solution = Laliberte (2009) correlation `PhiC`
   (empirical coefficients a1..a6 from Laliberte, J. Chem. Eng. Data 54, 1725; fit range 1.5-120 C, w <= 0.26).
   Ph(T) is the apparent molar enthalpy of NaCl in that solution: dPh/dT = PhiC (definition of Phi_C).
   Solution enthalpy = (water at T) + n*Ph(T)   (definition of apparent molar enthalpy; `solEnthalpy`).
 * Crystal NaCl enthalpy Hcr with Cp_cr in [40,100] J/K/mol on 20..25 C (Archer 1992 Part I, Eqs 4-6: Cp(298.15)=50.16;
   my evaluation of the spline gives 50.04..50.16 on 20..25 C).
 * Parker NSRDS-NBS 2 (1965): Delta H_inf(25 C, NaCl(c) -> infinite dilution) = 928 +- 5 cal/mol (printed p.26);
   Phi_L(25 C, m) (Table X, p.50) is RELATIVE to infinite dilution: Ph(25) - Hcr(25) = Hinf + Phi_L.
   For the regime m = 17.1 r in [0.34, 0.86] mol/kg Table X gives Phi_L = 83, 49, 20, -41 cal/mol at m = .2775,
   .5551, .7401, 1.1101 and dPhi_L/dm^(1/2) < 0 for m >= .185, so Phi_L in [-41, 88]; premise used: [-41, 100] cal. cal = 4.184 J.
 * Boiling temperature Tb of the final solution: 20 <= Tb <= 101 C (1 atm, <= 4.8 wt%: ideal elevation <= ~0.9 K); stove
   beats losses at Tb; Tw, Ts = temperature as function of total enthalpy (inverse of Hw, Hs; existence by IVT,
   `inverse_exists`); tau_w, tau_s = elapsed time as function of enthalpy: d tau/dH = 1/(P - h (T(H) - 20)).

PROVED (not assumed): Phi_C < 0 and >= -105 J/(K mol) for all regime/temperatures (concavity + exp bounds, `cs_sign`);
 trapezoid enclosure Ph(101) - Ph(20) >= -3363 J/mol from 11 Taylor-bounded exp evaluations (`ph_drop`);
 dissolution at 20 C >= 3890 J/mol via Kirchhoff (`thermo_facts`); salt addition at any x in [20,Tb] cools
 (Hs(x) - Hw(x) >= 0), solution enthalpy strictly increasing, mixing at 20 C stays above 10 C; and the timing
 comparison (`timing_core`, `answer_theorem`).
CONCLUSIONS: `cs_sign`, `answer_theorem` (salting at the start is no slower than salting at any later water temperature).
-/


/-! ### calculus helpers -/
lemma dmono (F f : ℝ → ℝ) (a b : ℝ) (hF : ∀ T ∈ Set.Icc a b, HasDerivAt F (f T) T)
    (hf : ∀ T ∈ Set.Icc a b, 0 ≤ f T) : MonotoneOn F (Set.Icc a b) := by
  apply monotoneOn_of_deriv_nonneg (convex_Icc a b)
  · exact fun T hT => (hF T hT).continuousAt.continuousWithinAt
  · exact fun T hT => (hF T (interior_subset hT)).differentiableAt.differentiableWithinAt
  · intro T hT; rw [(hF T (interior_subset hT)).deriv]; exact hf T (interior_subset hT)

lemma danti (F f : ℝ → ℝ) (a b : ℝ) (hF : ∀ T ∈ Set.Icc a b, HasDerivAt F (f T) T)
    (hf : ∀ T ∈ Set.Icc a b, f T ≤ 0) : AntitoneOn F (Set.Icc a b) := by
  apply antitoneOn_of_deriv_nonpos (convex_Icc a b)
  · exact fun T hT => (hF T hT).continuousAt.continuousWithinAt
  · exact fun T hT => (hF T (interior_subset hT)).differentiableAt.differentiableWithinAt
  · intro T hT; rw [(hF T (interior_subset hT)).deriv]; exact hf T (interior_subset hT)

lemma dstrict (F f : ℝ → ℝ) (a b : ℝ) (hF : ∀ T ∈ Set.Icc a b, HasDerivAt F (f T) T)
    (hf : ∀ T ∈ Set.Icc a b, 0 < f T) : StrictMonoOn F (Set.Icc a b) := by
  apply strictMonoOn_of_deriv_pos (convex_Icc a b)
  · exact fun T hT => (hF T hT).continuousAt.continuousWithinAt
  · intro T hT; rw [(hF T (interior_subset hT)).deriv]; exact hf T (interior_subset hT)

lemma lin_deriv (F : ℝ → ℝ) (d c T : ℝ) (h : HasDerivAt F d T) :
    HasDerivAt (fun y => F y + c*y) (d + c) T :=
  (h.add ((hasDerivAt_id T).const_mul c)).congr_deriv (by simp)

/-- Taylor-polynomial upper bound for exp on [0,4] (from `Real.exp_bound`, 7 terms, applied to x/4 and raised to the 4th power) -/
noncomputable def Q (x : ℝ) : ℝ := ((∑ m ∈ Finset.range 7, (x/4)^m / (m.factorial:ℝ)) + (x/4)^7 * (8/35280))^4

lemma exp_ub (x : ℝ) (h0 : 0 ≤ x) (h4 : x ≤ 4) : Real.exp x ≤ Q x := by
  have hy : |x/4| ≤ 1 := by rw [abs_le]; constructor <;> linarith
  have hb := Real.exp_bound hy (n := 7) (by norm_num)
  rw [abs_of_nonneg (by linarith : 0 ≤ x/4)] at hb
  have h1 := (abs_le.mp hb).2
  have e : ((Nat.succ 7 : ℕ) : ℝ) / ((Nat.factorial 7 : ℝ) * (7:ℕ)) = 8/35280 := by norm_num [Nat.factorial]
  rw [e] at h1
  have e2 : Real.exp x = Real.exp (x/4) ^ 4 := by
    rw [← Real.exp_nat_mul]; congr 1; push_cast; ring
  rw [e2]; exact pow_le_pow_left₀ (Real.exp_pos _).le (by linarith) 4

/-! ### Laliberté (2009) NaCl correlation: empirical inputs (exposed) and derived consequences -/
noncomputable def Mnacl : ℝ := 58.44
noncomputable def a1 : ℝ := -0.0693559668993322
noncomputable def a2 : ℝ := -0.0782134167486952
noncomputable def a3 : ℝ := 3.84798479408635
noncomputable def a4 : ℝ := -11.2762109247072
noncomputable def a5 : ℝ := 8.73187698542672
noncomputable def a6 : ℝ := 1.81245930472755

/-- apparent molar heat capacity of NaCl in aqueous solution, J/(K mol): Mnacl * (Laliberté solute heat capacity
    Cp_i in J/(g K)); w = salt mass fraction, T in C -/
noncomputable def PhiC (w T : ℝ) : ℝ :=
  Mnacl * (a1 * Real.exp (a2*T + a3*Real.exp (0.01*T) + a4*w) + a5 * w ^ a6)

/-- Laliberté's mixing rule Cp_sol = w_w Cp_w + w_s Cp_i gives C_solution - C_water = ms*Cp_i = (ms/M)*(M*Cp_i):
    per mole of salt the heat-capacity change is M*Cp_i = PhiC. -/
lemma apparent_capacity (ms mw M cpw cpi : ℝ) (hM : 0 < M) (hs : 0 < ms + mw) :
    (ms+mw) * ((mw/(ms+mw)) * cpw + (ms/(ms+mw)) * cpi) - mw * cpw = (ms/M) * (M * cpi) := by
  field_simp; ring

lemma phi_mono_w (w T : ℝ) (h : 1/51 ≤ w) : PhiC (1/51) T ≤ PhiC w T := by
  unfold PhiC
  have h1 : Real.exp (a2*T + a3*Real.exp (0.01*T) + a4*w) ≤ Real.exp (a2*T + a3*Real.exp (0.01*T) + a4*(1/51)) :=
    Real.exp_le_exp.mpr (by unfold a4; linarith)
  have h2 : (1/51:ℝ) ^ a6 ≤ w ^ a6 := Real.rpow_le_rpow (by norm_num) h (by unfold a6; norm_num)
  have ha1 : a1 < 0 := by unfold a1; norm_num
  have ha5 : 0 < a5 := by unfold a5; norm_num
  have hM : 0 < Mnacl := by unfold Mnacl; norm_num
  exact mul_le_mul_of_nonneg_left (by nlinarith) hM.le

lemma phi_neg (w T : ℝ) (h1 : 1/51 ≤ w) (h2 : w ≤ 1/21) (t1 : 10 ≤ T) (t2 : T ≤ 101) : PhiC w T < 0 := by
  unfold PhiC
  have hw : 0 < w := by linarith
  have e1 : w ^ a6 ≤ w ^ ((3:ℝ)/2) :=
    Real.rpow_le_rpow_of_exponent_ge hw (by linarith) (by unfold a6; norm_num)
  have e2 : w ^ ((3:ℝ)/2) = w * Real.sqrt w := by
    rw [Real.sqrt_eq_rpow, show ((3:ℝ)/2) = 1 + 1/2 by norm_num, Real.rpow_add hw, Real.rpow_one]
  have e3 : Real.sqrt w ≤ 1/4 := by rw [Real.sqrt_le_left (by norm_num)]; nlinarith
  have e4 : w ^ a6 ≤ 1/84 := by have := Real.sqrt_nonneg w; nlinarith
  have q := Real.quadratic_le_exp_of_nonneg (x := 0.01*T) (by linarith)
  set E := Real.exp (0.01*T)
  have hα : 1.2 ≤ a2*T + a3*E + a4*w := by
    unfold a2 a3 a4
    nlinarith [mul_nonneg (by linarith : (0:ℝ) ≤ 101 - T) (by linarith : (0:ℝ) ≤ T - 10)]
  have hex : 2 ≤ Real.exp (a2*T + a3*E + a4*w) := by linarith [Real.add_one_le_exp (a2*T + a3*E + a4*w)]
  have hM : 0 < Mnacl := by unfold Mnacl; norm_num
  refine mul_neg_of_pos_of_neg hM ?_
  unfold a1 a5 at *
  nlinarith

/-- Φ_C is concave in T: its value at T lies above the chord between any a < b. -/
lemma phi_chord (w a b T : ℝ) (hab : a < b) (h1 : a ≤ T) (h2 : T ≤ b) :
    (b-a) * PhiC w T ≥ (b-T) * PhiC w a + (T-a) * PhiC w b := by
  set α : ℝ → ℝ := fun s => a2*s + a3*Real.exp (0.01*s) + a4*w with hαdef
  have ha3 : 0 < a3 := by unfold a3; norm_num
  have tang : ∀ s, Real.exp (α s) ≥ Real.exp (α T) * (1 + (a2 + a3*0.01*Real.exp (0.01*T)) * (s - T)) := by
    intro s
    have e1 : Real.exp (0.01*s) ≥ Real.exp (0.01*T) * (1 + 0.01*(s-T)) := by
      have : Real.exp (0.01*s) = Real.exp (0.01*T) * Real.exp (0.01*(s-T)) := by
        rw [← Real.exp_add]; congr 1; ring
      rw [this]
      nlinarith [Real.add_one_le_exp (0.01*(s-T)), Real.exp_pos (0.01*T)]
    have e2 : α s - α T ≥ (a2 + a3*0.01*Real.exp (0.01*T)) * (s - T) := by
      simp only [hαdef]; nlinarith
    have e3 : Real.exp (α s) = Real.exp (α T) * Real.exp (α s - α T) := by
      rw [← Real.exp_add]; congr 1; ring
    rw [e3]
    nlinarith [Real.add_one_le_exp (α s - α T), Real.exp_pos (α T)]
  have ta := tang a
  have tb := tang b
  have hg : (b-a) * Real.exp (α T) ≤ (b-T) * Real.exp (α a) + (T-a) * Real.exp (α b) := by
    nlinarith [mul_le_mul_of_nonneg_left ta (by linarith : (0:ℝ) ≤ b - T),
               mul_le_mul_of_nonneg_left tb (by linarith : (0:ℝ) ≤ T - a)]
  have ha1 : a1 < 0 := by unfold a1; norm_num
  have hM : 0 < Mnacl := by unfold Mnacl; norm_num
  unfold PhiC
  have : (b-a) * (a1 * Real.exp (α T)) ≥ (b-T) * (a1 * Real.exp (α a)) + (T-a) * (a1 * Real.exp (α b)) := by
    nlinarith
  simp only [hαdef] at this
  nlinarith [mul_pos hM (by linarith : (0:ℝ) < b - a)]

/-- trapezoid lower bound: if F' = f lies above its chord on [a,b] then F b - F a ≥ (b-a)(f a + f b)/2 -/
lemma trap (F f : ℝ → ℝ) (a b : ℝ) (hab : a < b)
    (hF : ∀ T ∈ Set.Icc a b, HasDerivAt F (f T) T)
    (hc : ∀ T ∈ Set.Icc a b, (b-a) * f T ≥ (b-T) * f a + (T-a) * f b) :
    (b-a)*(f a + f b)/2 ≤ F b - F a := by
  have hba : 0 < b - a := by linarith
  have hd : ∀ T ∈ Set.Icc a b, HasDerivAt
      (fun T => F T - F a - (f a * (T-a) + (T-a)^2 * (f b - f a) / (2*(b-a)))) (f T - ((b-T) * f a + (T-a) * f b)/(b-a)) T := by
    intro T hT
    have u : HasDerivAt (fun T : ℝ => T - a) 1 T := (hasDerivAt_id T).sub_const a
    have v := (u.const_mul (f a)).add (((u.pow 2).mul_const (f b - f a)).div_const (2*(b-a)))
    convert (hF T hT).sub_const (F a) |>.sub v using 1
    field_simp; ring
  have := dmono _ _ a b hd (fun T hT => by
    have := hc T hT
    rw [sub_nonneg, div_le_iff₀ hba]; linarith) ⟨le_rfl, hab.le⟩ ⟨hab.le, le_rfl⟩ hab.le
  simp only at this
  have e : (f a * (b-a) + (b-a)^2 * (f b - f a) / (2*(b-a))) = (b-a)*(f a + f b)/2 := by
    field_simp; ring
  simp at this
  nlinarith [e]

lemma node (w T V β U L : ℝ) (h : 1/51 ≤ w)
    (hh : 0 ≤ T ∧ T ≤ 400 ∧ Q (0.01*T) ≤ V ∧ a2*T + a3*V + a4*(1/51) ≤ β ∧ 0 ≤ β ∧ β ≤ 4 ∧ Q β ≤ U ∧
      -L ≤ Mnacl*a1*U) : -L ≤ PhiC w T := by
  obtain ⟨h0, h4, hV, hb, hb0, hb4, hU, hL⟩ := hh
  refine le_trans ?_ (phi_mono_w w T h)
  have e1 : Real.exp (0.01*T) ≤ V := (exp_ub _ (by linarith) (by linarith)).trans hV
  have ha3 : 0 < a3 := by unfold a3; norm_num
  have e2 : Real.exp (a2*T + a3*Real.exp (0.01*T) + a4*(1/51)) ≤ U :=
    (Real.exp_le_exp.mpr (by nlinarith [mul_le_mul_of_nonneg_left e1 ha3.le])).trans ((exp_ub β hb0 hb4).trans hU)
  have h5 : 0 ≤ a5 * (1/51:ℝ)^a6 := mul_nonneg (by unfold a5; norm_num) (Real.rpow_nonneg (by norm_num) _)
  have ha1 : a1 < 0 := by unfold a1; norm_num
  have hM : 0 < Mnacl := by unfold Mnacl; norm_num
  unfold PhiC
  nlinarith [mul_le_mul_of_nonneg_left e2 (mul_pos hM (neg_pos.mpr ha1)).le, mul_nonneg hM.le h5]

lemma nodes (w : ℝ) (h : 1/51 ≤ w) : -(104.51:ℝ) ≤ PhiC w 10 ∧ -(74.77:ℝ) ≤ PhiC w 20 ∧ -(56.07:ℝ) ≤ PhiC w 30 ∧ -(44.29:ℝ) ≤ PhiC w 40 ∧ -(37.06:ℝ) ≤ PhiC w 50 ∧ -(33.04:ℝ) ≤ PhiC w 60 ∧ -(31.6:ℝ) ≤ PhiC w 70 ∧ -(32.65:ℝ) ≤ PhiC w 80 ∧ -(36.75:ℝ) ≤ PhiC w 90 ∧ -(45.49:ℝ) ≤ PhiC w 100 ∧ -(46.73:ℝ) ≤ PhiC w 101 :=
  ⟨node w 10 1.10519 3.2497 25.784 104.51 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1]),
   node w 20 1.22142 2.9148 18.447 74.77 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1]),
   node w 30 1.34987 2.6269 13.832 56.07 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1]),
   node w 40 1.49184 2.3911 10.927 44.29 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1]),
   node w 50 1.64874 2.2127 9.142 37.06 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1]),
   node w 60 1.82213 2.0978 8.15 33.04 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1]),
   node w 70 2.01377 2.0531 7.794 31.6 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1]),
   node w 80 2.22556 2.0859 8.053 32.65 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1]),
   node w 90 2.45962 2.2044 9.066 36.75 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1]),
   node w 100 2.7183 2.4177 11.222 45.49 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1]),
   node w 101 2.74562 2.4446 11.527 46.73 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1])⟩

lemma piece (w : ℝ) (Ph : ℝ → ℝ) (a b La Lb : ℝ) (hab : a < b) (ha : 10 ≤ a) (hb : b ≤ 101)
    (hPh : ∀ T ∈ Set.Icc (10:ℝ) 101, HasDerivAt Ph (PhiC w T) T)
    (hA : -La ≤ PhiC w a) (hB : -Lb ≤ PhiC w b) : -((b-a)*(La+Lb)/2) ≤ Ph b - Ph a := by
  have := trap Ph (PhiC w) a b hab (fun T hT => hPh T ⟨by linarith [hT.1], by linarith [hT.2]⟩)
    (fun T hT => phi_chord w a b T hab hT.1 hT.2)
  nlinarith

lemma phi_floor (w T : ℝ) (h1 : 1/51 ≤ w) (t1 : 10 ≤ T) (t2 : T ≤ 101) : -105 ≤ PhiC w T := by
  have c := phi_chord w 10 101 T (by norm_num) t1 t2
  obtain ⟨n10, -, -, -, -, -, -, -, -, -, n101⟩ := nodes w h1
  nlinarith [mul_nonneg (by linarith : (0:ℝ) ≤ 101 - T) (by linarith : (0:ℝ) ≤ 105 - 104.51),
             mul_nonneg (by linarith : (0:ℝ) ≤ T - 10) (by linarith : (0:ℝ) ≤ 46.73 - 0)]

/-- Total change of apparent molar enthalpy of NaCl from 20 C to 101 C is at least -3363 J/mol. -/
lemma ph_drop (w : ℝ) (Ph : ℝ → ℝ) (h1 : 1/51 ≤ w)
    (hPh : ∀ T ∈ Set.Icc (10:ℝ) 101, HasDerivAt Ph (PhiC w T) T) : -3363 ≤ Ph 101 - Ph 20 := by
  obtain ⟨n10, n20, n30, n40, n50, n60, n70, n80, n90, n100, n101⟩ := nodes w h1
  have p0 := piece w Ph 20 30 _ _ (by norm_num) (by norm_num) (by norm_num) hPh n20 n30
  have p1 := piece w Ph 30 40 _ _ (by norm_num) (by norm_num) (by norm_num) hPh n30 n40
  have p2 := piece w Ph 40 50 _ _ (by norm_num) (by norm_num) (by norm_num) hPh n40 n50
  have p3 := piece w Ph 50 60 _ _ (by norm_num) (by norm_num) (by norm_num) hPh n50 n60
  have p4 := piece w Ph 60 70 _ _ (by norm_num) (by norm_num) (by norm_num) hPh n60 n70
  have p5 := piece w Ph 70 80 _ _ (by norm_num) (by norm_num) (by norm_num) hPh n70 n80
  have p6 := piece w Ph 80 90 _ _ (by norm_num) (by norm_num) (by norm_num) hPh n80 n90
  have p7 := piece w Ph 90 100 _ _ (by norm_num) (by norm_num) (by norm_num) hPh n90 n100
  have p8 := piece w Ph 100 101 _ _ (by norm_num) (by norm_num) (by norm_num) hPh n100 n101
  norm_num at *
  linarith

/-! ## Heating-time model (accepted Newtonian lumped model; state variable = total enthalpy H) -/

/-- Time to reach the boiling point when salt is added while the pure water is at Ta: pure-water branch up to
    total enthalpy Hw Ta, then solution branch from the same enthalpy up to Hs Tb.
    τw, τs = elapsed time as a function of total enthalpy on the two branches. -/
def boilTime (τw τs Hw Hs : ℝ → ℝ) (T0 Tb Ta : ℝ) : ℝ :=
  (τw (Hw Ta) - τw (Hw T0)) + (τs (Hs Tb) - τs (Hw Ta))

theorem timing_core (P h Tr Tl Tb : ℝ) (Hw Hs Tw Ts τw τs : ℝ → ℝ)
    (hh : 0 ≤ h) (hP : h*(Tb-Tr) < P) (hl : Tl ≤ Tr) (hr : Tr ≤ Tb)
    (hHw : StrictMonoOn Hw (Set.Icc Tl Tb)) (hHs : StrictMonoOn Hs (Set.Icc Tl Tb))
    (hTw : ∀ H ∈ Set.Icc (Hw Tr) (Hw Tb), Tw H ∈ Set.Icc Tl Tb ∧ Hw (Tw H) = H)
    (hTs : ∀ H ∈ Set.Icc (Hs Tl) (Hs Tb), Ts H ∈ Set.Icc Tl Tb ∧ Hs (Ts H) = H)
    (hτw : ∀ H ∈ Set.Icc (Hw Tr) (Hw Tb), HasDerivAt τw (1/(P - h*(Tw H - Tr))) H)
    (hτs : ∀ H ∈ Set.Icc (Hs Tl) (Hs Tb), HasDerivAt τs (1/(P - h*(Ts H - Tr))) H)
    (hcool : ∀ x ∈ Set.Icc Tr Tb, Hw x ≤ Hs x) (hlow : Hs Tl ≤ Hw Tr) :
    ∀ Ta ∈ Set.Icc Tr Tb, boilTime τw τs Hw Hs Tr Tb Tr ≤ boilTime τw τs Hw Hs Tr Tb Ta := by
  intro Ta hTa
  have hTrI : Tr ∈ Set.Icc Tl Tb := ⟨hl, hr⟩
  have hTaI : Ta ∈ Set.Icc Tl Tb := ⟨hl.trans hTa.1, hTa.2⟩
  have hTbI : Tb ∈ Set.Icc Tl Tb := ⟨hl.trans hr, le_rfl⟩
  have w1 : Hw Tr ≤ Hw Ta := hHw.monotoneOn hTrI hTaI hTa.1
  have w2 : Hw Ta ≤ Hw Tb := hHw.monotoneOn hTaI hTbI hTa.2
  have s1 : Hw Tb ≤ Hs Tb := hcool Tb ⟨hr, le_rfl⟩
  have hpos : ∀ x, x ≤ Tb → 0 < P - h*(x - Tr) := by
    intro x hx
    have : h*(x-Tr) ≤ h*(Tb-Tr) := mul_le_mul_of_nonneg_left (by linarith) hh
    linarith
  have key : ∀ H ∈ Set.Icc (Hw Tr) (Hw Ta), 1/(P - h*(Tw H - Tr)) - 1/(P - h*(Ts H - Tr)) ≥ 0 := by
    intro H hH
    obtain ⟨hx, hxH⟩ := hTw H ⟨hH.1, hH.2.trans w2⟩
    obtain ⟨hy, hyH⟩ := hTs H ⟨hlow.trans hH.1, hH.2.trans (w2.trans s1)⟩
    have hxr : Tr ≤ Tw H := by
      by_contra hc
      have := hHw hx hTrI (not_le.mp hc)
      rw [hxH] at this
      linarith [hH.1]
    have hc := hcool _ ⟨hxr, hx.2⟩
    rw [hxH] at hc
    have hyx : Ts H ≤ Tw H := by
      by_contra hcon
      have := hHs hx hy (not_le.mp hcon)
      rw [hyH] at this
      linarith
    have := mul_le_mul_of_nonneg_left (by linarith : Ts H - Tr ≤ Tw H - Tr) hh
    have := one_div_le_one_div_of_le (hpos _ hx.2) (by linarith : P - h*(Tw H - Tr) ≤ P - h*(Ts H - Tr))
    linarith
  have mono := dmono (fun H => τw H - τs H) _ (Hw Tr) (Hw Ta)
    (fun H hH => (hτw H ⟨hH.1, hH.2.trans w2⟩).sub (hτs H ⟨hlow.trans hH.1, hH.2.trans (w2.trans s1)⟩)) key
  have := mono ⟨le_rfl, w1⟩ ⟨w1, le_rfl⟩ w1
  simp only [boilTime] at this ⊢
  linarith

/-- sanity: the inverse temperature functions Tw, Ts exist (intermediate value theorem). -/
lemma inverse_exists (H : ℝ → ℝ) (Tl Tb : ℝ) (hle : Tl ≤ Tb) (hc : ContinuousOn H (Set.Icc Tl Tb)) :
    ∀ y ∈ Set.Icc (H Tl) (H Tb), ∃ T ∈ Set.Icc Tl Tb, H T = y := fun y hy => by
  obtain ⟨T, hT, e⟩ := intermediate_value_Icc hle hc hy
  exact ⟨T, hT, e⟩

/-! ## Thermodynamics of dissolving NaCl(cr) (at 20 C) in the pot -/

/-- thermochemical calorie in J (Parker 1965 reports cal) -/
noncomputable def cal : ℝ := 4.184

/-- moles of NaCl for salt/water mass ratio r, in the 0.998 kg of water of 1 L at 20 C -/
noncomputable def nSalt (r : ℝ) : ℝ := 1000*0.998*r/Mnacl

/-- Enthalpy (J) of pot+solution at T, measured from pot+pure water plus crystalline NaCl at 20 C.
    Definition of the apparent molar enthalpy Ph of NaCl in the final solution: solution = (same water at T) + n*Ph(T);
    the salt started as crystal with enthalpy Hcr 20. -/
def solEnthalpy (n : ℝ) (Hw Ph Hcr : ℝ → ℝ) (T : ℝ) : ℝ := Hw T + n * (Ph T - Hcr 20)

/-- Physical premises (empirical data and definitions), see the review packet. -/
structure Premises (r : ℝ) (Cw Hw Ph Hcr Cpcr : ℝ → ℝ) (hinf phiL : ℝ) : Prop where
  r_lo : 1/50 ≤ r
  r_hi : r ≤ 1/20
  hw_deriv : ∀ T ∈ Set.Icc (10:ℝ) 101, HasDerivAt Hw (Cw T) T
  hw_cap : ∀ T ∈ Set.Icc (10:ℝ) 101, 4100 ≤ Cw T
  ph_deriv : ∀ T ∈ Set.Icc (10:ℝ) 101, HasDerivAt Ph (PhiC (r/(1+r)) T) T
  cr_deriv : ∀ T ∈ Set.Icc (20:ℝ) 25, HasDerivAt Hcr (Cpcr T) T
  cr_cap : ∀ T ∈ Set.Icc (20:ℝ) 25, 40 ≤ Cpcr T ∧ Cpcr T ≤ 100
  hinf_b : 923*cal ≤ hinf ∧ hinf ≤ 933*cal
  phiL_b : -41*cal ≤ phiL ∧ phiL ≤ 100*cal
  diss25 : Ph 25 - Hcr 25 = hinf + phiL

lemma w_range (r : ℝ) (h1 : 1/50 ≤ r) (h2 : r ≤ 1/20) : 1/51 ≤ r/(1+r) ∧ r/(1+r) ≤ 1/21 := by
  constructor
  · rw [le_div_iff₀ (by linarith)]; linarith
  · rw [div_le_iff₀ (by linarith)]; linarith

lemma n_range (r : ℝ) (h1 : 1/50 ≤ r) (h2 : r ≤ 1/20) : 0.34 ≤ nSalt r ∧ nSalt r ≤ 0.86 := by
  unfold nSalt Mnacl
  constructor
  · rw [le_div_iff₀ (by norm_num)]; linarith
  · rw [div_le_iff₀ (by norm_num)]; linarith

theorem thermo_facts (r Tb : ℝ) (Cw Hw Ph Hcr Cpcr : ℝ → ℝ) (hinf phiL : ℝ)
    (hp : Premises r Cw Hw Ph Hcr Cpcr hinf phiL) (hT2 : Tb ≤ 101) :
    StrictMonoOn Hw (Set.Icc 10 Tb) ∧ StrictMonoOn (solEnthalpy (nSalt r) Hw Ph Hcr) (Set.Icc 10 Tb) ∧
    (∀ x ∈ Set.Icc (20:ℝ) Tb, Hw x ≤ solEnthalpy (nSalt r) Hw Ph Hcr x) ∧
    solEnthalpy (nSalt r) Hw Ph Hcr 10 ≤ Hw 20 := by
  obtain ⟨hw1, hw2⟩ := w_range r hp.r_lo hp.r_hi
  obtain ⟨hn1, hn2⟩ := n_range r hp.r_lo hp.r_hi
  set w := r/(1+r)
  set n := nSalt r
  have hs1 : ∀ T ∈ Set.Icc (10:ℝ) Tb, T ∈ Set.Icc (10:ℝ) 101 := fun T hT => ⟨hT.1, hT.2.trans hT2⟩
  have hs2 : ∀ T ∈ Set.Icc (20:ℝ) 25, T ∈ Set.Icc (10:ℝ) 101 := fun T hT => ⟨by linarith [hT.1], by linarith [hT.2]⟩
  have hs3 : ∀ T ∈ Set.Icc (10:ℝ) 20, T ∈ Set.Icc (10:ℝ) 101 := fun T hT => ⟨hT.1, by linarith [hT.2]⟩
  have hfl : ∀ T ∈ Set.Icc (10:ℝ) 101, -105 ≤ PhiC w T := fun T hT => phi_floor w T hw1 hT.1 hT.2
  have hng : ∀ T ∈ Set.Icc (10:ℝ) 101, PhiC w T ≤ 0 := fun T hT => (phi_neg w T hw1 hw2 hT.1 hT.2).le
  have hWs := dstrict Hw Cw 10 Tb (fun T hT => hp.hw_deriv T (hs1 T hT))
    (fun T hT => by linarith [hp.hw_cap T (hs1 T hT)])
  have hHs : ∀ T ∈ Set.Icc (10:ℝ) 101, HasDerivAt (solEnthalpy n Hw Ph Hcr) (Cw T + n * PhiC w T) T :=
    fun T hT => (hp.hw_deriv T hT).add (((hp.ph_deriv T hT).sub_const (Hcr 20)).const_mul n)
  have hSs := dstrict _ _ 10 Tb (fun T hT => hHs T (hs1 T hT))
    (fun T hT => by nlinarith [hp.hw_cap T (hs1 T hT), hfl T (hs1 T hT)])
  -- Kirchhoff: dissolution enthalpy Ph T - Hcr T between 20 and 25 C
  have hd : ∀ T ∈ Set.Icc (20:ℝ) 25, HasDerivAt (fun T => Ph T - Hcr T) (PhiC w T - Cpcr T) T :=
    fun T hT => (hp.ph_deriv T (hs2 T hT)).sub (hp.cr_deriv T hT)
  have hlow20 := danti _ _ 20 25 (fun T hT => lin_deriv _ _ 40 T (hd T hT))
    (fun T hT => by linarith [hng T (hs2 T hT), (hp.cr_cap T hT).1]) ⟨le_rfl, by norm_num⟩ ⟨by norm_num, le_rfl⟩ (by norm_num)
  have hup20 := dmono _ _ 20 25 (fun T hT => lin_deriv _ _ 205 T (hd T hT))
    (fun T hT => by linarith [hfl T (hs2 T hT), (hp.cr_cap T hT).2]) ⟨le_rfl, by norm_num⟩ ⟨by norm_num, le_rfl⟩ (by norm_num)
  have hup10 := dmono _ _ 10 20 (fun T hT => lin_deriv _ _ 105 T (hp.ph_deriv T (hs3 T hT)))
    (fun T hT => by linarith [hfl T (hs3 T hT)]) ⟨le_rfl, by norm_num⟩ ⟨by norm_num, le_rfl⟩ (by norm_num)
  have hup4 := dmono _ _ 10 20 (fun T hT => lin_deriv _ _ (-4100) T (hp.hw_deriv T (hs3 T hT)))
    (fun T hT => by linarith [hp.hw_cap T (hs3 T hT)]) ⟨le_rfl, by norm_num⟩ ⟨by norm_num, le_rfl⟩ (by norm_num)
  simp only at hlow20 hup20 hup10 hup4
  -- Parker's numbers: dissolution enthalpy at 20 C
  obtain ⟨hi1, hi2⟩ := hp.hinf_b
  obtain ⟨hl1, hl2⟩ := hp.phiL_b
  have hcal : cal = 4.184 := rfl
  rw [hcal] at hi1 hi2 hl1 hl2
  have hd25 := hp.diss25
  refine ⟨hWs, hSs, fun x hx => ?_, ?_⟩
  · have hanti2 := danti Ph _ 10 101 hp.ph_deriv hng
    have h1 : Ph 101 ≤ Ph x := hanti2 ⟨by linarith [hx.1], by linarith [hx.2]⟩ ⟨by norm_num, le_rfl⟩ (by linarith [hx.2])
    have h2 := ph_drop w Ph hw1 hp.ph_deriv
    unfold solEnthalpy
    nlinarith
  · unfold solEnthalpy
    nlinarith

/-! ## Conclusions -/

/-- CONCLUSION 1 (heat capacity). For 20-50 g NaCl per kg water and 10-101 C the apparent molar heat capacity of NaCl
    in solution is negative, in (-105, 0) J/(K mol) up to the floor; dissolving the salt LOWERS the total heat capacity
    of the pot contents by n*Phi in (-91, 0) J/K (< 2.2 % of the ~4.2 kJ/K of the water). -/
theorem cs_sign : ∀ (r T : ℝ), 1/50 ≤ r → r ≤ 1/20 → 10 ≤ T → T ≤ 101 →
    -105 ≤ PhiC (r/(1+r)) T ∧ PhiC (r/(1+r)) T < 0 ∧
    -91 < nSalt r * PhiC (r/(1+r)) T ∧ nSalt r * PhiC (r/(1+r)) T < 0 := by
  intro r T h1 h2 t1 t2
  obtain ⟨w1, w2⟩ := w_range r h1 h2
  obtain ⟨n1, n2⟩ := n_range r h1 h2
  have f := phi_floor _ T w1 t1 t2
  have g := phi_neg _ T w1 w2 t1 t2
  refine ⟨f, g, ?_, ?_⟩ <;> nlinarith

/-- CONCLUSION 2 (answer). Salt added at the very start (water at 20 C) gives a boiling time no longer than salt added
    once the water has reached any later temperature Ta <= Tb. -/
theorem answer_theorem : ∀ (r P h Tb : ℝ) (Cw Hw Ph Hcr Cpcr Tw Ts τw τs : ℝ → ℝ) (hinf phiL : ℝ),
    Premises r Cw Hw Ph Hcr Cpcr hinf phiL → 20 ≤ Tb → Tb ≤ 101 → 0 ≤ h → h*(Tb-20) < P →
    (∀ H ∈ Set.Icc (Hw 20) (Hw Tb), Tw H ∈ Set.Icc 10 Tb ∧ Hw (Tw H) = H) →
    (∀ H ∈ Set.Icc (solEnthalpy (nSalt r) Hw Ph Hcr 10) (solEnthalpy (nSalt r) Hw Ph Hcr Tb),
        Ts H ∈ Set.Icc 10 Tb ∧ solEnthalpy (nSalt r) Hw Ph Hcr (Ts H) = H) →
    (∀ H ∈ Set.Icc (Hw 20) (Hw Tb), HasDerivAt τw (1/(P - h*(Tw H - 20))) H) →
    (∀ H ∈ Set.Icc (solEnthalpy (nSalt r) Hw Ph Hcr 10) (solEnthalpy (nSalt r) Hw Ph Hcr Tb),
        HasDerivAt τs (1/(P - h*(Ts H - 20))) H) →
    ∀ Ta ∈ Set.Icc (20:ℝ) Tb,
      boilTime τw τs Hw (solEnthalpy (nSalt r) Hw Ph Hcr) 20 Tb 20
        ≤ boilTime τw τs Hw (solEnthalpy (nSalt r) Hw Ph Hcr) 20 Tb Ta := by
  intro r P h Tb Cw Hw Ph Hcr Cpcr Tw Ts τw τs hinf phiL hp hT1 hT2 hh hP hTw hTs hτw hτs
  obtain ⟨m1, m2, c1, c2⟩ := thermo_facts r Tb Cw Hw Ph Hcr Cpcr hinf phiL hp hT2
  exact timing_core P h 20 10 Tb Hw (solEnthalpy (nSalt r) Hw Ph Hcr) Tw Ts τw τs hh hP (by norm_num) hT1
    m1 m2 hTw hTs hτw hτs c1 c2
checks/33a4ae57add14e699086d38537f08f5d/submitted.lean
import Mathlib.Analysis.SpecialFunctions.Log.Deriv
import Mathlib.Analysis.SpecialFunctions.Pow.Real
import Mathlib.Analysis.SpecialFunctions.ExpDeriv
import Mathlib.Analysis.Calculus.MeanValue

/-! # Salt timing for 1 L of water at 20 C: when should NaCl be added? (cycle 6)

## REVIEW PACKET
SUPPLIED BY THE USER: 1 L water at 20 C in a pot on a stove; salt will be added; goal = salted water reaches
its boiling point soonest; salt amount, pot, heater, room unspecified.

ACCEPTED EARLIER (kept): lumped Newtonian heating dH/dt = P - h (T - 20), constant stove power P, no evaporation,
salt (crystal at 20 C) dissolves instantly at the moment of addition (total enthalpy conserved).

MODEL CHOICES / ASSUMPTIONS (all appear as `Premises` fields or theorem hypotheses):
 * NaCl; r = salt mass / water mass in [1/50, 1/20] (20-50 g in the 0.998 kg of water of 1 L); mass fraction
   w = r/(1+r) (conversion proved in `w_range`), n = 1000*0.998*r/58.44 mol (`nSalt`, bounds in `n_range`).
 * Pot+water enthalpy Hw(T) has heat capacity Cw(T) >= 4100 J/K on 10..101 C (0.998 kg water >= 4.17 kJ/K; pot adds).
 * Apparent molar heat capacity of NaCl in the final solution = Laliberte (2009) correlation `PhiC`
   (empirical coefficients a1..a6 from Laliberte, J. Chem. Eng. Data 54, 1725; fit range 1.5-120 C, w <= 0.26).
   Ph(T) is the apparent molar enthalpy of NaCl in that solution: dPh/dT = PhiC (definition of Phi_C).
   Solution enthalpy = (water at T) + n*Ph(T)   (definition of apparent molar enthalpy; `solEnthalpy`).
 * Crystal NaCl enthalpy Hcr with Cp_cr in [40,100] J/K/mol on 20..25 C (Archer 1992 Part I, Eqs 4-6: Cp(298.15)=50.16;
   my evaluation of the spline gives 50.04..50.16 on 20..25 C).
 * Parker NSRDS-NBS 2 (1965): Delta H_inf(25 C, NaCl(c) -> infinite dilution) = 928 +- 5 cal/mol (printed p.26);
   Phi_L(25 C, m) (Table X, p.50) is RELATIVE to infinite dilution: Ph(25) - Hcr(25) = Hinf + Phi_L.
   For the regime m = 17.1 r in [0.34, 0.86] mol/kg Table X gives Phi_L = 83, 49, 20, -41 cal/mol at m = .2775,
   .5551, .7401, 1.1101 and dPhi_L/dm^(1/2) < 0 for m >= .185, so Phi_L in [-41, 88]; premise used: [-41, 100] cal. cal = 4.184 J.
 * Boiling temperature Tb of the final solution: 20 <= Tb <= 101 C (1 atm, <= 4.8 wt%: ideal elevation <= ~0.9 K); stove
   beats losses at Tb; Tw, Ts = temperature as function of total enthalpy (inverse of Hw, Hs; existence by IVT,
   `inverse_exists`); tau_w, tau_s = elapsed time as function of enthalpy: d tau/dH = 1/(P - h (T(H) - 20)).

PROVED (not assumed): Phi_C < 0 and >= -105 J/(K mol) for all regime/temperatures (concavity + exp bounds, `cs_sign`);
 trapezoid enclosure Ph(101) - Ph(20) >= -3363 J/mol from 11 Taylor-bounded exp evaluations (`ph_drop`);
 dissolution at 20 C >= 3890 J/mol via Kirchhoff (`thermo_facts`); salt addition at any x in [20,Tb] cools
 (Hs(x) - Hw(x) >= 0), solution enthalpy strictly increasing, mixing at 20 C stays above 10 C; and the timing
 comparison (`timing_core`, `answer_theorem`).
CONCLUSIONS: `cs_sign`, `answer_theorem` (salting at the start is no slower than salting at any later water temperature).
-/


/-! ### calculus helpers -/
lemma dmono (F f : ℝ → ℝ) (a b : ℝ) (hF : ∀ T ∈ Set.Icc a b, HasDerivAt F (f T) T)
    (hf : ∀ T ∈ Set.Icc a b, 0 ≤ f T) : MonotoneOn F (Set.Icc a b) := by
  apply monotoneOn_of_deriv_nonneg (convex_Icc a b)
  · exact fun T hT => (hF T hT).continuousAt.continuousWithinAt
  · exact fun T hT => (hF T (interior_subset hT)).differentiableAt.differentiableWithinAt
  · intro T hT; rw [(hF T (interior_subset hT)).deriv]; exact hf T (interior_subset hT)

lemma danti (F f : ℝ → ℝ) (a b : ℝ) (hF : ∀ T ∈ Set.Icc a b, HasDerivAt F (f T) T)
    (hf : ∀ T ∈ Set.Icc a b, f T ≤ 0) : AntitoneOn F (Set.Icc a b) := by
  apply antitoneOn_of_deriv_nonpos (convex_Icc a b)
  · exact fun T hT => (hF T hT).continuousAt.continuousWithinAt
  · exact fun T hT => (hF T (interior_subset hT)).differentiableAt.differentiableWithinAt
  · intro T hT; rw [(hF T (interior_subset hT)).deriv]; exact hf T (interior_subset hT)

lemma dstrict (F f : ℝ → ℝ) (a b : ℝ) (hF : ∀ T ∈ Set.Icc a b, HasDerivAt F (f T) T)
    (hf : ∀ T ∈ Set.Icc a b, 0 < f T) : StrictMonoOn F (Set.Icc a b) := by
  apply strictMonoOn_of_deriv_pos (convex_Icc a b)
  · exact fun T hT => (hF T hT).continuousAt.continuousWithinAt
  · intro T hT; rw [(hF T (interior_subset hT)).deriv]; exact hf T (interior_subset hT)

lemma lin_deriv (F : ℝ → ℝ) (d c T : ℝ) (h : HasDerivAt F d T) :
    HasDerivAt (fun y => F y + c*y) (d + c) T :=
  (h.add ((hasDerivAt_id T).const_mul c)).congr_deriv (by simp)

/-- Taylor-polynomial upper bound for exp on [0,4] (from `Real.exp_bound`, 7 terms, applied to x/4 and raised to the 4th power) -/
noncomputable def Q (x : ℝ) : ℝ := ((∑ m ∈ Finset.range 7, (x/4)^m / (m.factorial:ℝ)) + (x/4)^7 * (8/35280))^4

lemma exp_ub (x : ℝ) (h0 : 0 ≤ x) (h4 : x ≤ 4) : Real.exp x ≤ Q x := by
  have hy : |x/4| ≤ 1 := by rw [abs_le]; constructor <;> linarith
  have hb := Real.exp_bound hy (n := 7) (by norm_num)
  rw [abs_of_nonneg (by linarith : 0 ≤ x/4)] at hb
  have h1 := (abs_le.mp hb).2
  have e : ((Nat.succ 7 : ℕ) : ℝ) / ((Nat.factorial 7 : ℝ) * (7:ℕ)) = 8/35280 := by norm_num [Nat.factorial]
  rw [e] at h1
  have e2 : Real.exp x = Real.exp (x/4) ^ 4 := by
    rw [← Real.exp_nat_mul]; congr 1; push_cast; ring
  rw [e2]; exact pow_le_pow_left₀ (Real.exp_pos _).le (by linarith) 4

/-! ### Laliberté (2009) NaCl correlation: empirical inputs (exposed) and derived consequences -/
noncomputable def Mnacl : ℝ := 58.44
noncomputable def a1 : ℝ := -0.0693559668993322
noncomputable def a2 : ℝ := -0.0782134167486952
noncomputable def a3 : ℝ := 3.84798479408635
noncomputable def a4 : ℝ := -11.2762109247072
noncomputable def a5 : ℝ := 8.73187698542672
noncomputable def a6 : ℝ := 1.81245930472755

/-- apparent molar heat capacity of NaCl in aqueous solution, J/(K mol): Mnacl * (Laliberté solute heat capacity
    Cp_i in J/(g K)); w = salt mass fraction, T in C -/
noncomputable def PhiC (w T : ℝ) : ℝ :=
  Mnacl * (a1 * Real.exp (a2*T + a3*Real.exp (0.01*T) + a4*w) + a5 * w ^ a6)

/-- Laliberté's mixing rule Cp_sol = w_w Cp_w + w_s Cp_i gives C_solution - C_water = ms*Cp_i = (ms/M)*(M*Cp_i):
    per mole of salt the heat-capacity change is M*Cp_i = PhiC. -/
lemma apparent_capacity (ms mw M cpw cpi : ℝ) (hM : 0 < M) (hs : 0 < ms + mw) :
    (ms+mw) * ((mw/(ms+mw)) * cpw + (ms/(ms+mw)) * cpi) - mw * cpw = (ms/M) * (M * cpi) := by
  field_simp; ring

lemma phi_mono_w (w T : ℝ) (h : 1/51 ≤ w) : PhiC (1/51) T ≤ PhiC w T := by
  unfold PhiC
  have h1 : Real.exp (a2*T + a3*Real.exp (0.01*T) + a4*w) ≤ Real.exp (a2*T + a3*Real.exp (0.01*T) + a4*(1/51)) :=
    Real.exp_le_exp.mpr (by unfold a4; linarith)
  have h2 : (1/51:ℝ) ^ a6 ≤ w ^ a6 := Real.rpow_le_rpow (by norm_num) h (by unfold a6; norm_num)
  have ha1 : a1 < 0 := by unfold a1; norm_num
  have ha5 : 0 < a5 := by unfold a5; norm_num
  have hM : 0 < Mnacl := by unfold Mnacl; norm_num
  exact mul_le_mul_of_nonneg_left (by nlinarith) hM.le

lemma phi_neg (w T : ℝ) (h1 : 1/51 ≤ w) (h2 : w ≤ 1/21) (t1 : 10 ≤ T) (t2 : T ≤ 101) : PhiC w T < 0 := by
  unfold PhiC
  have hw : 0 < w := by linarith
  have e1 : w ^ a6 ≤ w ^ ((3:ℝ)/2) :=
    Real.rpow_le_rpow_of_exponent_ge hw (by linarith) (by unfold a6; norm_num)
  have e2 : w ^ ((3:ℝ)/2) = w * Real.sqrt w := by
    rw [Real.sqrt_eq_rpow, show ((3:ℝ)/2) = 1 + 1/2 by norm_num, Real.rpow_add hw, Real.rpow_one]
  have e3 : Real.sqrt w ≤ 1/4 := by rw [Real.sqrt_le_left (by norm_num)]; nlinarith
  have e4 : w ^ a6 ≤ 1/84 := by have := Real.sqrt_nonneg w; nlinarith
  have q := Real.quadratic_le_exp_of_nonneg (x := 0.01*T) (by linarith)
  set E := Real.exp (0.01*T)
  have hα : 1.2 ≤ a2*T + a3*E + a4*w := by
    unfold a2 a3 a4
    nlinarith [mul_nonneg (by linarith : (0:ℝ) ≤ 101 - T) (by linarith : (0:ℝ) ≤ T - 10)]
  have hex : 2 ≤ Real.exp (a2*T + a3*E + a4*w) := by linarith [Real.add_one_le_exp (a2*T + a3*E + a4*w)]
  have hM : 0 < Mnacl := by unfold Mnacl; norm_num
  refine mul_neg_of_pos_of_neg hM ?_
  unfold a1 a5 at *
  nlinarith

/-- Φ_C is concave in T: its value at T lies above the chord between any a < b. -/
lemma phi_chord (w a b T : ℝ) (hab : a < b) (h1 : a ≤ T) (h2 : T ≤ b) :
    (b-a) * PhiC w T ≥ (b-T) * PhiC w a + (T-a) * PhiC w b := by
  set α : ℝ → ℝ := fun s => a2*s + a3*Real.exp (0.01*s) + a4*w with hαdef
  have ha3 : 0 < a3 := by unfold a3; norm_num
  have tang : ∀ s, Real.exp (α s) ≥ Real.exp (α T) * (1 + (a2 + a3*0.01*Real.exp (0.01*T)) * (s - T)) := by
    intro s
    have e1 : Real.exp (0.01*s) ≥ Real.exp (0.01*T) * (1 + 0.01*(s-T)) := by
      have : Real.exp (0.01*s) = Real.exp (0.01*T) * Real.exp (0.01*(s-T)) := by
        rw [← Real.exp_add]; congr 1; ring
      rw [this]
      nlinarith [Real.add_one_le_exp (0.01*(s-T)), Real.exp_pos (0.01*T)]
    have e2 : α s - α T ≥ (a2 + a3*0.01*Real.exp (0.01*T)) * (s - T) := by
      simp only [hαdef]; nlinarith
    have e3 : Real.exp (α s) = Real.exp (α T) * Real.exp (α s - α T) := by
      rw [← Real.exp_add]; congr 1; ring
    rw [e3]
    nlinarith [Real.add_one_le_exp (α s - α T), Real.exp_pos (α T)]
  have ta := tang a
  have tb := tang b
  have hg : (b-a) * Real.exp (α T) ≤ (b-T) * Real.exp (α a) + (T-a) * Real.exp (α b) := by
    nlinarith [mul_le_mul_of_nonneg_left ta (by linarith : (0:ℝ) ≤ b - T),
               mul_le_mul_of_nonneg_left tb (by linarith : (0:ℝ) ≤ T - a)]
  have ha1 : a1 < 0 := by unfold a1; norm_num
  have hM : 0 < Mnacl := by unfold Mnacl; norm_num
  unfold PhiC
  have : (b-a) * (a1 * Real.exp (α T)) ≥ (b-T) * (a1 * Real.exp (α a)) + (T-a) * (a1 * Real.exp (α b)) := by
    nlinarith
  simp only [hαdef] at this
  nlinarith [mul_pos hM (by linarith : (0:ℝ) < b - a)]

/-- trapezoid lower bound: if F' = f lies above its chord on [a,b] then F b - F a ≥ (b-a)(f a + f b)/2 -/
lemma trap (F f : ℝ → ℝ) (a b : ℝ) (hab : a < b)
    (hF : ∀ T ∈ Set.Icc a b, HasDerivAt F (f T) T)
    (hc : ∀ T ∈ Set.Icc a b, (b-a) * f T ≥ (b-T) * f a + (T-a) * f b) :
    (b-a)*(f a + f b)/2 ≤ F b - F a := by
  have hba : 0 < b - a := by linarith
  have hd : ∀ T ∈ Set.Icc a b, HasDerivAt
      (fun T => F T - F a - (f a * (T-a) + (T-a)^2 * (f b - f a) / (2*(b-a)))) (f T - ((b-T) * f a + (T-a) * f b)/(b-a)) T := by
    intro T hT
    have u : HasDerivAt (fun T : ℝ => T - a) 1 T := (hasDerivAt_id T).sub_const a
    have v := (u.const_mul (f a)).add (((u.pow 2).mul_const (f b - f a)).div_const (2*(b-a)))
    convert (hF T hT).sub_const (F a) |>.sub v using 1
    field_simp; ring
  have := dmono _ _ a b hd (fun T hT => by
    have := hc T hT
    rw [sub_nonneg, div_le_iff₀ hba]; linarith) ⟨le_rfl, hab.le⟩ ⟨hab.le, le_rfl⟩ hab.le
  simp only at this
  have e : (f a * (b-a) + (b-a)^2 * (f b - f a) / (2*(b-a))) = (b-a)*(f a + f b)/2 := by
    field_simp; ring
  simp at this
  nlinarith [e]

lemma node (w T V β U L : ℝ) (h : 1/51 ≤ w)
    (hh : 0 ≤ T ∧ T ≤ 400 ∧ Q (0.01*T) ≤ V ∧ a2*T + a3*V + a4*(1/51) ≤ β ∧ 0 ≤ β ∧ β ≤ 4 ∧ Q β ≤ U ∧
      -L ≤ Mnacl*a1*U) : -L ≤ PhiC w T := by
  obtain ⟨h0, h4, hV, hb, hb0, hb4, hU, hL⟩ := hh
  refine le_trans ?_ (phi_mono_w w T h)
  have e1 : Real.exp (0.01*T) ≤ V := (exp_ub _ (by linarith) (by linarith)).trans hV
  have ha3 : 0 < a3 := by unfold a3; norm_num
  have e2 : Real.exp (a2*T + a3*Real.exp (0.01*T) + a4*(1/51)) ≤ U :=
    (Real.exp_le_exp.mpr (by nlinarith [mul_le_mul_of_nonneg_left e1 ha3.le])).trans ((exp_ub β hb0 hb4).trans hU)
  have h5 : 0 ≤ a5 * (1/51:ℝ)^a6 := mul_nonneg (by unfold a5; norm_num) (Real.rpow_nonneg (by norm_num) _)
  have ha1 : a1 < 0 := by unfold a1; norm_num
  have hM : 0 < Mnacl := by unfold Mnacl; norm_num
  unfold PhiC
  nlinarith [mul_le_mul_of_nonneg_left e2 (mul_pos hM (neg_pos.mpr ha1)).le, mul_nonneg hM.le h5]

lemma nodes (w : ℝ) (h : 1/51 ≤ w) : -(104.51:ℝ) ≤ PhiC w 10 ∧ -(74.77:ℝ) ≤ PhiC w 20 ∧ -(56.07:ℝ) ≤ PhiC w 30 ∧ -(44.29:ℝ) ≤ PhiC w 40 ∧ -(37.06:ℝ) ≤ PhiC w 50 ∧ -(33.04:ℝ) ≤ PhiC w 60 ∧ -(31.6:ℝ) ≤ PhiC w 70 ∧ -(32.65:ℝ) ≤ PhiC w 80 ∧ -(36.75:ℝ) ≤ PhiC w 90 ∧ -(45.49:ℝ) ≤ PhiC w 100 ∧ -(46.73:ℝ) ≤ PhiC w 101 :=
  ⟨node w 10 1.10519 3.2497 25.784 104.51 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1]),
   node w 20 1.22142 2.9148 18.447 74.77 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1]),
   node w 30 1.34987 2.6269 13.832 56.07 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1]),
   node w 40 1.49184 2.3911 10.927 44.29 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1]),
   node w 50 1.64874 2.2127 9.142 37.06 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1]),
   node w 60 1.82213 2.0978 8.15 33.04 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1]),
   node w 70 2.01377 2.0531 7.794 31.6 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1]),
   node w 80 2.22556 2.0859 8.053 32.65 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1]),
   node w 90 2.45962 2.2044 9.066 36.75 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1]),
   node w 100 2.7183 2.4177 11.222 45.49 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1]),
   node w 101 2.74562 2.4446 11.527 46.73 h (by norm_num [Q, Finset.sum_range_succ, Nat.factorial, a2, a3, a4, Mnacl, a1])⟩

lemma piece (w : ℝ) (Ph : ℝ → ℝ) (a b La Lb : ℝ) (hab : a < b) (ha : 10 ≤ a) (hb : b ≤ 101)
    (hPh : ∀ T ∈ Set.Icc (10:ℝ) 101, HasDerivAt Ph (PhiC w T) T)
    (hA : -La ≤ PhiC w a) (hB : -Lb ≤ PhiC w b) : -((b-a)*(La+Lb)/2) ≤ Ph b - Ph a := by
  have := trap Ph (PhiC w) a b hab (fun T hT => hPh T ⟨by linarith [hT.1], by linarith [hT.2]⟩)
    (fun T hT => phi_chord w a b T hab hT.1 hT.2)
  nlinarith

lemma phi_floor (w T : ℝ) (h1 : 1/51 ≤ w) (t1 : 10 ≤ T) (t2 : T ≤ 101) : -105 ≤ PhiC w T := by
  have c := phi_chord w 10 101 T (by norm_num) t1 t2
  obtain ⟨n10, -, -, -, -, -, -, -, -, -, n101⟩ := nodes w h1
  nlinarith [mul_nonneg (by linarith : (0:ℝ) ≤ 101 - T) (by linarith : (0:ℝ) ≤ 105 - 104.51),
             mul_nonneg (by linarith : (0:ℝ) ≤ T - 10) (by linarith : (0:ℝ) ≤ 46.73 - 0)]

/-- Total change of apparent molar enthalpy of NaCl from 20 C to 101 C is at least -3363 J/mol. -/
lemma ph_drop (w : ℝ) (Ph : ℝ → ℝ) (h1 : 1/51 ≤ w)
    (hPh : ∀ T ∈ Set.Icc (10:ℝ) 101, HasDerivAt Ph (PhiC w T) T) : -3363 ≤ Ph 101 - Ph 20 := by
  obtain ⟨n10, n20, n30, n40, n50, n60, n70, n80, n90, n100, n101⟩ := nodes w h1
  have p0 := piece w Ph 20 30 _ _ (by norm_num) (by norm_num) (by norm_num) hPh n20 n30
  have p1 := piece w Ph 30 40 _ _ (by norm_num) (by norm_num) (by norm_num) hPh n30 n40
  have p2 := piece w Ph 40 50 _ _ (by norm_num) (by norm_num) (by norm_num) hPh n40 n50
  have p3 := piece w Ph 50 60 _ _ (by norm_num) (by norm_num) (by norm_num) hPh n50 n60
  have p4 := piece w Ph 60 70 _ _ (by norm_num) (by norm_num) (by norm_num) hPh n60 n70
  have p5 := piece w Ph 70 80 _ _ (by norm_num) (by norm_num) (by norm_num) hPh n70 n80
  have p6 := piece w Ph 80 90 _ _ (by norm_num) (by norm_num) (by norm_num) hPh n80 n90
  have p7 := piece w Ph 90 100 _ _ (by norm_num) (by norm_num) (by norm_num) hPh n90 n100
  have p8 := piece w Ph 100 101 _ _ (by norm_num) (by norm_num) (by norm_num) hPh n100 n101
  norm_num at *
  linarith

/-! ## Heating-time model (accepted Newtonian lumped model; state variable = total enthalpy H) -/

/-- Time to reach the boiling point when salt is added while the pure water is at Ta: pure-water branch up to
    total enthalpy Hw Ta, then solution branch from the same enthalpy up to Hs Tb.
    τw, τs = elapsed time as a function of total enthalpy on the two branches. -/
def boilTime (τw τs Hw Hs : ℝ → ℝ) (T0 Tb Ta : ℝ) : ℝ :=
  (τw (Hw Ta) - τw (Hw T0)) + (τs (Hs Tb) - τs (Hw Ta))

theorem timing_core (P h Tr Tl Tb : ℝ) (Hw Hs Tw Ts τw τs : ℝ → ℝ)
    (hh : 0 ≤ h) (hP : h*(Tb-Tr) < P) (hl : Tl ≤ Tr) (hr : Tr ≤ Tb)
    (hHw : StrictMonoOn Hw (Set.Icc Tl Tb)) (hHs : StrictMonoOn Hs (Set.Icc Tl Tb))
    (hTw : ∀ H ∈ Set.Icc (Hw Tr) (Hw Tb), Tw H ∈ Set.Icc Tl Tb ∧ Hw (Tw H) = H)
    (hTs : ∀ H ∈ Set.Icc (Hs Tl) (Hs Tb), Ts H ∈ Set.Icc Tl Tb ∧ Hs (Ts H) = H)
    (hτw : ∀ H ∈ Set.Icc (Hw Tr) (Hw Tb), HasDerivAt τw (1/(P - h*(Tw H - Tr))) H)
    (hτs : ∀ H ∈ Set.Icc (Hs Tl) (Hs Tb), HasDerivAt τs (1/(P - h*(Ts H - Tr))) H)
    (hcool : ∀ x ∈ Set.Icc Tr Tb, Hw x ≤ Hs x) (hlow : Hs Tl ≤ Hw Tr) :
    ∀ Ta ∈ Set.Icc Tr Tb, boilTime τw τs Hw Hs Tr Tb Tr ≤ boilTime τw τs Hw Hs Tr Tb Ta := by
  intro Ta hTa
  have hTrI : Tr ∈ Set.Icc Tl Tb := ⟨hl, hr⟩
  have hTaI : Ta ∈ Set.Icc Tl Tb := ⟨hl.trans hTa.1, hTa.2⟩
  have hTbI : Tb ∈ Set.Icc Tl Tb := ⟨hl.trans hr, le_rfl⟩
  have w1 : Hw Tr ≤ Hw Ta := hHw.monotoneOn hTrI hTaI hTa.1
  have w2 : Hw Ta ≤ Hw Tb := hHw.monotoneOn hTaI hTbI hTa.2
  have s1 : Hw Tb ≤ Hs Tb := hcool Tb ⟨hr, le_rfl⟩
  have hpos : ∀ x, x ≤ Tb → 0 < P - h*(x - Tr) := by
    intro x hx
    have : h*(x-Tr) ≤ h*(Tb-Tr) := mul_le_mul_of_nonneg_left (by linarith) hh
    linarith
  have key : ∀ H ∈ Set.Icc (Hw Tr) (Hw Ta), 1/(P - h*(Tw H - Tr)) - 1/(P - h*(Ts H - Tr)) ≥ 0 := by
    intro H hH
    obtain ⟨hx, hxH⟩ := hTw H ⟨hH.1, hH.2.trans w2⟩
    obtain ⟨hy, hyH⟩ := hTs H ⟨hlow.trans hH.1, hH.2.trans (w2.trans s1)⟩
    have hxr : Tr ≤ Tw H := by
      by_contra hc
      have := hHw hx hTrI (not_le.mp hc)
      rw [hxH] at this
      linarith [hH.1]
    have hc := hcool _ ⟨hxr, hx.2⟩
    rw [hxH] at hc
    have hyx : Ts H ≤ Tw H := by
      by_contra hcon
      have := hHs hx hy (not_le.mp hcon)
      rw [hyH] at this
      linarith
    have := mul_le_mul_of_nonneg_left (by linarith : Ts H - Tr ≤ Tw H - Tr) hh
    have := one_div_le_one_div_of_le (hpos _ hx.2) (by linarith : P - h*(Tw H - Tr) ≤ P - h*(Ts H - Tr))
    linarith
  have mono := dmono (fun H => τw H - τs H) _ (Hw Tr) (Hw Ta)
    (fun H hH => (hτw H ⟨hH.1, hH.2.trans w2⟩).sub (hτs H ⟨hlow.trans hH.1, hH.2.trans (w2.trans s1)⟩)) key
  have := mono ⟨le_rfl, w1⟩ ⟨w1, le_rfl⟩ w1
  simp only [boilTime] at this ⊢
  linarith

/-- sanity: the inverse temperature functions Tw, Ts exist (intermediate value theorem). -/
lemma inverse_exists (H : ℝ → ℝ) (Tl Tb : ℝ) (hle : Tl ≤ Tb) (hc : ContinuousOn H (Set.Icc Tl Tb)) :
    ∀ y ∈ Set.Icc (H Tl) (H Tb), ∃ T ∈ Set.Icc Tl Tb, H T = y := fun y hy => by
  obtain ⟨T, hT, e⟩ := intermediate_value_Icc hle hc hy
  exact ⟨T, hT, e⟩

/-! ## Thermodynamics of dissolving NaCl(cr) (at 20 C) in the pot -/

/-- thermochemical calorie in J (Parker 1965 reports cal) -/
noncomputable def cal : ℝ := 4.184

/-- moles of NaCl for salt/water mass ratio r, in the 0.998 kg of water of 1 L at 20 C -/
noncomputable def nSalt (r : ℝ) : ℝ := 1000*0.998*r/Mnacl

/-- Enthalpy (J) of pot+solution at T, measured from pot+pure water plus crystalline NaCl at 20 C.
    Definition of the apparent molar enthalpy Ph of NaCl in the final solution: solution = (same water at T) + n*Ph(T);
    the salt started as crystal with enthalpy Hcr 20. -/
def solEnthalpy (n : ℝ) (Hw Ph Hcr : ℝ → ℝ) (T : ℝ) : ℝ := Hw T + n * (Ph T - Hcr 20)

/-- Physical premises (empirical data and definitions), see the review packet. -/
structure Premises (r : ℝ) (Cw Hw Ph Hcr Cpcr : ℝ → ℝ) (hinf phiL : ℝ) : Prop where
  r_lo : 1/50 ≤ r
  r_hi : r ≤ 1/20
  hw_deriv : ∀ T ∈ Set.Icc (10:ℝ) 101, HasDerivAt Hw (Cw T) T
  hw_cap : ∀ T ∈ Set.Icc (10:ℝ) 101, 4100 ≤ Cw T
  ph_deriv : ∀ T ∈ Set.Icc (10:ℝ) 101, HasDerivAt Ph (PhiC (r/(1+r)) T) T
  cr_deriv : ∀ T ∈ Set.Icc (20:ℝ) 25, HasDerivAt Hcr (Cpcr T) T
  cr_cap : ∀ T ∈ Set.Icc (20:ℝ) 25, 40 ≤ Cpcr T ∧ Cpcr T ≤ 100
  hinf_b : 923*cal ≤ hinf ∧ hinf ≤ 933*cal
  phiL_b : -41*cal ≤ phiL ∧ phiL ≤ 100*cal
  diss25 : Ph 25 - Hcr 25 = hinf + phiL

lemma w_range (r : ℝ) (h1 : 1/50 ≤ r) (h2 : r ≤ 1/20) : 1/51 ≤ r/(1+r) ∧ r/(1+r) ≤ 1/21 := by
  constructor
  · rw [le_div_iff₀ (by linarith)]; linarith
  · rw [div_le_iff₀ (by linarith)]; linarith

lemma n_range (r : ℝ) (h1 : 1/50 ≤ r) (h2 : r ≤ 1/20) : 0.34 ≤ nSalt r ∧ nSalt r ≤ 0.86 := by
  unfold nSalt Mnacl
  constructor
  · rw [le_div_iff₀ (by norm_num)]; linarith
  · rw [div_le_iff₀ (by norm_num)]; linarith

theorem thermo_facts (r Tb : ℝ) (Cw Hw Ph Hcr Cpcr : ℝ → ℝ) (hinf phiL : ℝ)
    (hp : Premises r Cw Hw Ph Hcr Cpcr hinf phiL) (hT2 : Tb ≤ 101) :
    StrictMonoOn Hw (Set.Icc 10 Tb) ∧ StrictMonoOn (solEnthalpy (nSalt r) Hw Ph Hcr) (Set.Icc 10 Tb) ∧
    (∀ x ∈ Set.Icc (20:ℝ) Tb, Hw x ≤ solEnthalpy (nSalt r) Hw Ph Hcr x) ∧
    solEnthalpy (nSalt r) Hw Ph Hcr 10 ≤ Hw 20 := by
  obtain ⟨hw1, hw2⟩ := w_range r hp.r_lo hp.r_hi
  obtain ⟨hn1, hn2⟩ := n_range r hp.r_lo hp.r_hi
  set w := r/(1+r)
  set n := nSalt r
  have hs1 : ∀ T ∈ Set.Icc (10:ℝ) Tb, T ∈ Set.Icc (10:ℝ) 101 := fun T hT => ⟨hT.1, hT.2.trans hT2⟩
  have hs2 : ∀ T ∈ Set.Icc (20:ℝ) 25, T ∈ Set.Icc (10:ℝ) 101 := fun T hT => ⟨by linarith [hT.1], by linarith [hT.2]⟩
  have hs3 : ∀ T ∈ Set.Icc (10:ℝ) 20, T ∈ Set.Icc (10:ℝ) 101 := fun T hT => ⟨hT.1, by linarith [hT.2]⟩
  have hfl : ∀ T ∈ Set.Icc (10:ℝ) 101, -105 ≤ PhiC w T := fun T hT => phi_floor w T hw1 hT.1 hT.2
  have hng : ∀ T ∈ Set.Icc (10:ℝ) 101, PhiC w T ≤ 0 := fun T hT => (phi_neg w T hw1 hw2 hT.1 hT.2).le
  have hWs := dstrict Hw Cw 10 Tb (fun T hT => hp.hw_deriv T (hs1 T hT))
    (fun T hT => by linarith [hp.hw_cap T (hs1 T hT)])
  have hHs : ∀ T ∈ Set.Icc (10:ℝ) 101, HasDerivAt (solEnthalpy n Hw Ph Hcr) (Cw T + n * PhiC w T) T :=
    fun T hT => (hp.hw_deriv T hT).add (((hp.ph_deriv T hT).sub_const (Hcr 20)).const_mul n)
  have hSs := dstrict _ _ 10 Tb (fun T hT => hHs T (hs1 T hT))
    (fun T hT => by nlinarith [hp.hw_cap T (hs1 T hT), hfl T (hs1 T hT)])
  -- Kirchhoff: dissolution enthalpy Ph T - Hcr T between 20 and 25 C
  have hd : ∀ T ∈ Set.Icc (20:ℝ) 25, HasDerivAt (fun T => Ph T - Hcr T) (PhiC w T - Cpcr T) T :=
    fun T hT => (hp.ph_deriv T (hs2 T hT)).sub (hp.cr_deriv T hT)
  have hlow20 := danti _ _ 20 25 (fun T hT => lin_deriv _ _ 40 T (hd T hT))
    (fun T hT => by linarith [hng T (hs2 T hT), (hp.cr_cap T hT).1]) ⟨le_rfl, by norm_num⟩ ⟨by norm_num, le_rfl⟩ (by norm_num)
  have hup20 := dmono _ _ 20 25 (fun T hT => lin_deriv _ _ 205 T (hd T hT))
    (fun T hT => by linarith [hfl T (hs2 T hT), (hp.cr_cap T hT).2]) ⟨le_rfl, by norm_num⟩ ⟨by norm_num, le_rfl⟩ (by norm_num)
  have hup10 := dmono _ _ 10 20 (fun T hT => lin_deriv _ _ 105 T (hp.ph_deriv T (hs3 T hT)))
    (fun T hT => by linarith [hfl T (hs3 T hT)]) ⟨le_rfl, by norm_num⟩ ⟨by norm_num, le_rfl⟩ (by norm_num)
  have hup4 := dmono _ _ 10 20 (fun T hT => lin_deriv _ _ (-4100) T (hp.hw_deriv T (hs3 T hT)))
    (fun T hT => by linarith [hp.hw_cap T (hs3 T hT)]) ⟨le_rfl, by norm_num⟩ ⟨by norm_num, le_rfl⟩ (by norm_num)
  simp only at hlow20 hup20 hup10 hup4
  -- Parker's numbers: dissolution enthalpy at 20 C
  obtain ⟨hi1, hi2⟩ := hp.hinf_b
  obtain ⟨hl1, hl2⟩ := hp.phiL_b
  have hcal : cal = 4.184 := rfl
  rw [hcal] at hi1 hi2 hl1 hl2
  have hd25 := hp.diss25
  refine ⟨hWs, hSs, fun x hx => ?_, ?_⟩
  · have hanti2 := danti Ph _ 10 101 hp.ph_deriv hng
    have h1 : Ph 101 ≤ Ph x := hanti2 ⟨by linarith [hx.1], by linarith [hx.2]⟩ ⟨by norm_num, le_rfl⟩ (by linarith [hx.2])
    have h2 := ph_drop w Ph hw1 hp.ph_deriv
    unfold solEnthalpy
    nlinarith
  · unfold solEnthalpy
    nlinarith

/-! ## Conclusions -/

/-- CONCLUSION 1 (heat capacity). For 20-50 g NaCl per kg water and 10-101 C the apparent molar heat capacity of NaCl
    in solution is negative, in (-105, 0) J/(K mol) up to the floor; dissolving the salt LOWERS the total heat capacity
    of the pot contents by n*Phi in (-91, 0) J/K (< 2.2 % of the ~4.2 kJ/K of the water). -/
theorem cs_sign : ∀ (r T : ℝ), 1/50 ≤ r → r ≤ 1/20 → 10 ≤ T → T ≤ 101 →
    -105 ≤ PhiC (r/(1+r)) T ∧ PhiC (r/(1+r)) T < 0 ∧
    -91 < nSalt r * PhiC (r/(1+r)) T ∧ nSalt r * PhiC (r/(1+r)) T < 0 := by
  intro r T h1 h2 t1 t2
  obtain ⟨w1, w2⟩ := w_range r h1 h2
  obtain ⟨n1, n2⟩ := n_range r h1 h2
  have f := phi_floor _ T w1 t1 t2
  have g := phi_neg _ T w1 w2 t1 t2
  refine ⟨f, g, ?_, ?_⟩ <;> nlinarith

/-- CONCLUSION 2 (answer). Salt added at the very start (water at 20 C) gives a boiling time no longer than salt added
    once the water has reached any later temperature Ta <= Tb. -/
theorem answer_theorem : ∀ (r P h Tb : ℝ) (Cw Hw Ph Hcr Cpcr Tw Ts τw τs : ℝ → ℝ) (hinf phiL : ℝ),
    Premises r Cw Hw Ph Hcr Cpcr hinf phiL → 20 ≤ Tb → Tb ≤ 101 → 0 ≤ h → h*(Tb-20) < P →
    (∀ H ∈ Set.Icc (Hw 20) (Hw Tb), Tw H ∈ Set.Icc 10 Tb ∧ Hw (Tw H) = H) →
    (∀ H ∈ Set.Icc (solEnthalpy (nSalt r) Hw Ph Hcr 10) (solEnthalpy (nSalt r) Hw Ph Hcr Tb),
        Ts H ∈ Set.Icc 10 Tb ∧ solEnthalpy (nSalt r) Hw Ph Hcr (Ts H) = H) →
    (∀ H ∈ Set.Icc (Hw 20) (Hw Tb), HasDerivAt τw (1/(P - h*(Tw H - 20))) H) →
    (∀ H ∈ Set.Icc (solEnthalpy (nSalt r) Hw Ph Hcr 10) (solEnthalpy (nSalt r) Hw Ph Hcr Tb),
        HasDerivAt τs (1/(P - h*(Ts H - 20))) H) →
    ∀ Ta ∈ Set.Icc (20:ℝ) Tb,
      boilTime τw τs Hw (solEnthalpy (nSalt r) Hw Ph Hcr) 20 Tb 20
        ≤ boilTime τw τs Hw (solEnthalpy (nSalt r) Hw Ph Hcr) 20 Tb Ta := by
  intro r P h Tb Cw Hw Ph Hcr Cpcr Tw Ts τw τs hinf phiL hp hT1 hT2 hh hP hTw hTs hτw hτs
  obtain ⟨m1, m2, c1, c2⟩ := thermo_facts r Tb Cw Hw Ph Hcr Cpcr hinf phiL hp hT2
  exact timing_core P h 20 10 Tb Hw (solEnthalpy (nSalt r) Hw Ph Hcr) Tw Ts τw τs hh hP (by norm_num) hT1
    m1 m2 hTw hTs hτw hτs c1 c2
checks/dcd7a1b461ab4ea1832bd51aed75b96c/submitted.lean
import Mathlib.Analysis.SpecialFunctions.Log.Deriv
import Mathlib.Analysis.Calculus.MeanValue

/-! Model (see report.txt).  H = enthalpy (J) measured from "everything at room temperature Tr".
 Pure water, heat capacity Cw: temperature Tr + H/Cw.
 Salt solution, total heat capacity Cw+Cs>0 (Cs of EITHER sign; literature: Cs<0), dissolution enthalpy D (at Tr) of either sign:
 temperature Tr + (H-D)/(Cw+Cs).  Salt (at Tr) is mixed in instantly, so H is conserved.
 Heater power P constant, loss h*(T-Tr):  dH/dt = P - h*(T-Tr).
 Separating variables, the time to move from H1 to H2 is Fw/Fs(H2) - Fw/Fs(H1) where
 Fw' = 1/(P - h*(H/Cw)), Fs' = 1/(P - h*((H-D)/(Cw+Cs))) (explicit logarithms below).
 Tb = boiling point of the final solution (fixed by salt amount, not by timing). -/

/-- time-integral of dH/(P - loss) for pure water (antiderivative, h>0) -/
noncomputable def Fw (Cw P h H : ℝ) : ℝ := -(Cw/h) * Real.log (P - h*(H/Cw))
/-- same for the salt solution -/
noncomputable def Fs (Cw Cs D P h H : ℝ) : ℝ := -((Cw+Cs)/h) * Real.log (P - h*((H-D)/(Cw+Cs)))

/-- Total time to reach Tb from T0 when the salt is added once the water is at Ta:
 heating water alone from T0 to Ta, then solution from the post-mixing state up to Tb. -/
noncomputable def boilTime (Cw Cs D P h Tr T0 Tb Ta : ℝ) : ℝ :=
  (Fw Cw P h (Cw*(Ta-Tr)) - Fw Cw P h (Cw*(T0-Tr))) +
  (Fs Cw Cs D P h ((Cw+Cs)*(Tb-Tr)+D) - Fs Cw Cs D P h (Cw*(Ta-Tr)))

lemma G_deriv (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs))) :
    ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), HasDerivAt (fun H => Fw Cw P h H - Fs Cw Cs D P h H)
      (1/(P - h*(H/Cw)) - 1/(P - h*((H-D)/(Cw+Cs)))) H := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  intro H hH
  have a1 : HasDerivAt (fun H : ℝ => P - h*(H/Cw)) (-(h/Cw)) H := by
    have := ((hasDerivAt_id H).div_const Cw).const_mul h
    simpa [div_eq_mul_inv, mul_comm] using (this.const_sub P)
  have a2 : HasDerivAt (fun H : ℝ => P - h*((H-D)/(Cw+Cs))) (-(h/(Cw+Cs))) H := by
    have := (((hasDerivAt_id H).sub_const D).div_const (Cw+Cs)).const_mul h
    simpa [div_eq_mul_inv, mul_comm] using (this.const_sub P)
  have l1 := (a1.log (hw H hH).ne').const_mul (-(Cw/h))
  have l2 := (a2.log (hs H hH).ne').const_mul (-((Cw+Cs)/h))
  have := l1.sub l2
  convert this using 1
  · funext x; rfl
  · have e1 := (hw H hH).ne'
    have e2 := (hs H hH).ne'
    have e3 := hh.ne'
    have e4 := hCw.ne'
    have e5 := hC.ne'
    field_simp

/-- Salt added at water temperature x changes the temperature at once by
 (D + Cs (x-Tr))/(Cw+Cs) downward (a cooling if D + Cs (x-Tr) > 0).  -/
lemma mix_iff (Cw Cs D Tr x : ℝ) (hCw : 0 < Cw) (hC : 0 < Cw + Cs) :
    (Cw*(x-Tr) - D)/(Cw+Cs) ≤ Cw*(x-Tr)/Cw ↔ 0 ≤ D + Cs*(x-Tr) := by
  rw [mul_div_cancel_left₀ _ hCw.ne', div_le_iff₀ hC]
  constructor <;> intro h <;> nlinarith

lemma G_mono (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs)))
    (S : Set ℝ) (hS : S ⊆ Set.Icc 0 (Cw*(Tb-Tr))) (hconv : Convex ℝ S)
    (sgn : ∀ H ∈ S, (H-D)/(Cw+Cs) ≤ H/Cw) :
    MonotoneOn (fun H => Fw Cw P h H - Fs Cw Cs D P h H) S := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  have hd := G_deriv Cw Cs D P h Tr Tb hCw hC hh hP hs
  apply monotoneOn_of_deriv_nonneg hconv
  · intro H hH; exact (hd H (hS hH)).continuousAt.continuousWithinAt
  · intro H hH; exact (hd H (hS (interior_subset hH))).differentiableAt.differentiableWithinAt
  · intro H hH
    have hH' := interior_subset hH
    rw [(hd H (hS hH')).deriv]
    apply sub_nonneg.mpr
    apply one_div_le_one_div_of_le (hw H (hS hH'))
    have := sgn H hH'
    nlinarith

lemma G_anti (Cw Cs D P h Tr Tb : ℝ)
    (hCw : 0 < Cw) (hC : 0 < Cw + Cs) (hh : 0 < h)
    (hP : h*(Tb-Tr) < P)
    (hs : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs)))
    (S : Set ℝ) (hS : S ⊆ Set.Icc 0 (Cw*(Tb-Tr))) (hconv : Convex ℝ S)
    (sgn : ∀ H ∈ S, H/Cw ≤ (H-D)/(Cw+Cs)) :
    AntitoneOn (fun H => Fw Cw P h H - Fs Cw Cs D P h H) S := by
  have hw : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*(H/Cw) := by
    intro H hH
    have : H/Cw ≤ Tb - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
    nlinarith
  have hd := G_deriv Cw Cs D P h Tr Tb hCw hC hh hP hs
  apply antitoneOn_of_deriv_nonpos hconv
  · intro H hH; exact (hd H (hS hH)).continuousAt.continuousWithinAt
  · intro H hH; exact (hd H (hS (interior_subset hH))).differentiableAt.differentiableWithinAt
  · intro H hH
    have hH' := interior_subset hH
    rw [(hd H (hS hH')).deriv]
    apply sub_nonpos.mpr
    apply one_div_le_one_div_of_le (hs H (hS hH'))
    have := sgn H hH'
    nlinarith

/-- Comparison lemma (sign-free; proved, used by the answer below).  Salt of fixed amount (Cs, D arbitrary sign, C=Cw+Cs>0).
 If between water temperatures Ta1 ≤ Ta2 salt addition always COOLS (D+Cs(x-Tr) ≥ 0 for x in [Ta1,Ta2]),
 adding at Ta1 is no slower than at Ta2; if it always WARMS (≤ 0), adding at Ta2 is no slower. -/
theorem compare_theorem : ∀ (Cw Cs D P h Tr T0 Tb : ℝ),
    0 < Cw → 0 < Cw + Cs → 0 < h → h*(Tb-Tr) < P → Tr ≤ T0 →
    (∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-D)/(Cw+Cs))) →
    ∀ Ta1 Ta2 : ℝ, T0 ≤ Ta1 → Ta1 ≤ Ta2 → Ta2 ≤ Tb →
      ((∀ x, Ta1 ≤ x → x ≤ Ta2 → 0 ≤ D + Cs*(x-Tr)) →
          boilTime Cw Cs D P h Tr T0 Tb Ta1 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta2) ∧
      ((∀ x, Ta1 ≤ x → x ≤ Ta2 → D + Cs*(x-Tr) ≤ 0) →
          boilTime Cw Cs D P h Tr T0 Tb Ta2 ≤ boilTime Cw Cs D P h Tr T0 Tb Ta1) := by
  intro Cw Cs D P h Tr T0 Tb hCw hC hh hP hTr hs Ta1 Ta2 h1 h12 h2
  have hmem : ∀ x, Ta1 ≤ x → x ≤ Ta2 → Cw*(x-Tr) ∈ Set.Icc 0 (Cw*(Tb-Tr)) := fun x a b =>
    ⟨by nlinarith, by nlinarith⟩
  have hS : Set.Icc (Cw*(Ta1-Tr)) (Cw*(Ta2-Tr)) ⊆ Set.Icc 0 (Cw*(Tb-Tr)) := by
    intro H hH
    exact ⟨by nlinarith [hH.1], by nlinarith [hH.2]⟩
  have hHx : ∀ H ∈ Set.Icc (Cw*(Ta1-Tr)) (Cw*(Ta2-Tr)), ∃ x, Ta1 ≤ x ∧ x ≤ Ta2 ∧ H = Cw*(x-Tr) := by
    intro H hH
    refine ⟨H/Cw + Tr, ?_, ?_, ?_⟩
    · have : Ta1 - Tr ≤ H/Cw := by rw [le_div_iff₀ hCw]; nlinarith [hH.1]
      linarith
    · have : H/Cw ≤ Ta2 - Tr := by rw [div_le_iff₀ hCw]; nlinarith [hH.2]
      linarith
    · field_simp; ring
  constructor
  · intro hx
    have m := G_mono Cw Cs D P h Tr Tb hCw hC hh hP hs _ hS (convex_Icc _ _) (by
      intro H hH
      obtain ⟨x, a, b, rfl⟩ := hHx H hH
      exact (mix_iff Cw Cs D Tr x hCw hC).mpr (hx x a b))
      ⟨le_rfl, by nlinarith⟩ ⟨by nlinarith, le_rfl⟩ (by nlinarith)
    simp only [boilTime]; simp only at m; linarith
  · intro hx
    have m := G_anti Cw Cs D P h Tr Tb hCw hC hh hP hs _ hS (convex_Icc _ _) (by
      intro H hH
      obtain ⟨x, a, b, rfl⟩ := hHx H hH
      have := hx x a b
      have h3 : ¬ ((Cw*(x-Tr) - D)/(Cw+Cs) < Cw*(x-Tr)/Cw) → True := fun _ => trivial
      rw [mul_div_cancel_left₀ _ hCw.ne', le_div_iff₀ hC]
      nlinarith)
      ⟨le_rfl, by nlinarith⟩ ⟨by nlinarith, le_rfl⟩ (by nlinarith)
    simp only [boilTime]; simp only at m; linarith


/-! ## Salt-specific part (NaCl, 1 L of water).  Inputs are labelled; nothing numerical below is a measurement.

 Choices.  Salt = NaCl, molar mass M = 58.44 g/mol.  1 L of water at 20 C has mass mw = 0.998 kg
 (density 0.9982 kg/L).  The salt/water ratio r = (salt mass)/(water mass) (NOT the mass fraction of the
 final solution; the fraction is w = r/(1+r), proved in `cs_theorem`).  Regime: 0.02 <= r <= 0.05,
 i.e. about 20-50 g NaCl in the litre (2-4.8 wt%; seawater-like to very salty cooking water).
 Cs is DERIVED, not fitted: by definition of the apparent molar heat capacity Phi of the salt in solution,
 (total heat capacity of solution) = (heat capacity of the water) + n*Phi, so Cs = n*Phi, n = moles of salt.
 Phi is temperature dependent; the lumped model needs one constant Cs, so Phi below is the SECANT MEAN of
 Phi(T,m) over 20 C..Tb, which makes the linear mixing-enthalpy D + Cs (T-Tr) exact at both ends 20 C and Tb.
 Empirical inputs (EXPOSED PREMISES, from the Laliberte (2009) NaCl correlation, fit range 1.5-120 C,
 w <= 0.26, evaluated numerically outside Lean -- a synthetic calculation from a published correlation):
   * secant-mean Phi in [-41, -27] J/K/mol for r in [0.02, 0.05] (computed: -40.7 at r=0.02 ... -27.9 at 0.05);
   * enthalpy of solution of NaCl(s) at ~20-25 C, hsol >= 3800 J/mol (NBS value +3.88 kJ/mol, endothermic);
   * boiling point of the solution Tb in [100, 101] C (elevation <= ~1 K for these amounts);
   * pot+water heat capacity Cw >= 4170 J/K (at least the 0.998 kg of water, 4.18 J/g/K).
 D = n*hsol is the enthalpy absorbed when the salt (at room temperature Tr) dissolves at Tr. -/

/-- Cs = n*Phi: sign, size, and the ratio conversion. -/
theorem cs_theorem : ∀ (r M mw n Phi ms mwater : ℝ),
    0.02 ≤ r → r ≤ 0.05 → M = 58.44 → mw = 0.998 → n = 1000*mw*r/M →
    -41 ≤ Phi → Phi ≤ -27 → 0 < ms → 0 < mwater →
    (n*Phi < 0 ∧ -36 ≤ n*Phi) ∧ ms/(ms+mwater) = (ms/mwater)/(1+ms/mwater) := by
  intro r M mw n Phi ms mwater hr1 hr2 hM hmw hn hP1 hP2 hms hmwa
  have hn0 : 0 < n := by rw [hn, hM, hmw]; positivity
  have hn1 : n ≤ 0.8540 := by
    rw [hn, hM, hmw, div_le_iff₀ (by norm_num)]; nlinarith
  refine ⟨⟨by nlinarith, by nlinarith⟩, ?_⟩
  field_simp

/-- ANSWER.  Accepted model (lumped heat capacity Cw, constant stove power P, Newton loss h, no evaporation,
 salt at room temperature Tr = T0 = 20 C dissolves instantly, enthalpy conserved).  Under the premises above
 (r in [0.02,0.05], Cs = n*Phi, D = n*hsol, Tb in [100,101], stove stronger than losses at Tb), adding the
 salt right at the start (Ta = T0) is no slower than adding it at any later water temperature Ta in [T0,Tb].
 The condition D + Cs (x-Tr) >= 0 (salt addition cools / does not warm) is CHECKED here, not assumed, and
 the 'stove beats losses' condition for the salt solution is derived from it. -/
theorem answer_theorem : ∀ (Cw P h Tr T0 Tb r M mw n Phi hsol : ℝ),
    Tr = 20 → T0 = 20 → 100 ≤ Tb → Tb ≤ 101 → 4170 ≤ Cw → 0 < h → h*(Tb-Tr) < P →
    0.02 ≤ r → r ≤ 0.05 → M = 58.44 → mw = 0.998 → n = 1000*mw*r/M →
    -41 ≤ Phi → Phi ≤ -27 → 3800 ≤ hsol →
    ∀ Ta : ℝ, T0 ≤ Ta → Ta ≤ Tb →
      boilTime Cw (n*Phi) (n*hsol) P h Tr T0 Tb T0 ≤ boilTime Cw (n*Phi) (n*hsol) P h Tr T0 Tb Ta := by
  intro Cw P h Tr T0 Tb r M mw n Phi hsol hTr hT0 hb1 hb2 hCw hh hP hr1 hr2 hM hmw hn hP1 hP2 hs Ta h1 h2
  have hn0 : 0 < n := by rw [hn, hM, hmw]; positivity
  have hn1 : n ≤ 0.8540 := by
    rw [hn, hM, hmw, div_le_iff₀ (by norm_num)]; nlinarith
  have hCs : n*Phi < 0 := by nlinarith
  have hCs2 : -36 ≤ n*Phi := by nlinarith
  have hCw0 : 0 < Cw := by linarith
  have hC : 0 < Cw + n*Phi := by linarith
  have hd : 0 ≤ Tb - Tr := by linarith
  have hd2 : Tb - Tr ≤ 81 := by linarith
  -- enthalpy of solution at the boiling point is still >= 0 (checked)
  have hE0 : 0 ≤ hsol + Phi*(Tb-Tr) := by nlinarith [mul_nonneg (by linarith : 0 ≤ Phi + 41) hd]
  have hE : 0 ≤ n*hsol + n*Phi*(Tb-Tr) := by nlinarith [mul_nonneg hn0.le hE0]
  have hstove : ∀ H ∈ Set.Icc 0 (Cw*(Tb-Tr)), 0 < P - h*((H-n*hsol)/(Cw+n*Phi)) := by
    intro H hH
    have : (H-n*hsol)/(Cw+n*Phi) ≤ Tb - Tr := by
      rw [div_le_iff₀ hC]; nlinarith [hH.2]
    nlinarith
  refine (compare_theorem Cw (n*Phi) (n*hsol) P h Tr T0 Tb hCw0 hC hh hP (by linarith) hstove
    T0 Ta le_rfl h1 h2).1 ?_
  intro x hx1 hx2
  nlinarith [mul_nonneg (by linarith : 0 ≤ Tb - x) (by linarith : 0 ≤ -(n*Phi))]

accepted