Lean / Reader
Lean readerLewisWonRepair.lean
Lean 4 Web has Mathlib, so it can check files that import only Mathlib. This one also imports LongtailProofs.LewisWon317, LongtailProofs.LewisWonRows, which it does not have: you can read it there, but it will not check. Build the project itself to check it.
import LongtailProofs.LewisWon317import LongtailProofs.LewisWonRowsimport Mathlib.Analysis.SpecialFunctions.Pow.Realimport Mathlib.Tactic.IntervalCases/-! Finite and algebraic parts of the repaired Lewis–Won interval theorem.`RepairedInterval` states the target, not a claimed Lean proof of the analytic tail. -/namespace LongtailProofs.LewisWonRepairopen LongtailProofs.LewisWon317def RepairedInterval : Prop := ∀ n : ℕ, 4 ≤ n → ∀ x : ℝ, Endpoint n x →∀ m : ℕ, Maximizes n m → x < (m : ℝ) ∧ (m : ℝ) < x + 2/-- Dynamic programming, with out-of-range columns interpreted as zero. -/def nextRow (s : ℕ) (a : Array ℕ) :=Array.ofFn fun i : Fin (s+2) => match i.val with| 0 => 0| k+1 => (k+1) * a.getD (k+1) 0 + a.getD k 0def row : ℕ → Array ℕ| 0 => #[1]| s+1 => nextRow s (row s)theorem row_correct (s k : ℕ) : (row s).getD k 0 = Nat.stirlingSecond s k := byinduction s generalizing k with| zero => cases k with| zero => rfl| succ k => simp [row, Array.getD, Nat.stirlingSecond]| succ s ih =>rw [Array.getD_eq_getD_getElem?]simp only [row, nextRow, Array.getElem?_ofFn]split· cases k with| zero => rfl| succ k => simp [ih, Nat.stirlingSecond_succ_succ]· rename_i hnrw [Nat.stirlingSecond_eq_zero_of_lt (by omega)]rfldef certRow (m : ℕ) : Array ℕ := certificates.getD m #[]set_option maxRecDepth 10000 inset_option maxHeartbeats 10000000 intheorem cert_steps : ∀ s : Fin 128, nextRow s.val (certRow s.val) = certRow (s.val+1) := byrintro ⟨i, hi⟩change nextRow i (certRow i) = certRow (i+1)interval_cases i <;> as_aux_lemma => decide +kernelset_option maxRecDepth 10000 intheorem cert_eq_row (m : ℕ) (hm : m ≤ 128) : certRow m = row m := byinduction m with| zero => rfl| succ m ih =>exact (cert_steps ⟨m, by omega⟩).symm.trans (congrArg (nextRow m) (ih (by omega)))def fastValue (n m : ℕ) : ℕ :=let a := certRow m∑ i ∈ Finset.range m, (a.getD (i+1) 0 * (i+1)^(n-2*m) * (i+1).factorial)^2theorem fastValue_correct (n m : ℕ) (hm : m ≤ 128) : fastValue n m = value n m := bysimp only [fastValue, value, cert_eq_row m hm, row_correct]/-- Candidate exponents; the certificate checks each value on BOTH sides. -/def exponents : Array ℕ := #[1,2,4,7,9,12,15,18,21,25,28,32,35,39,42,46,50,54,58,62,66,70,74,78,83,87,91,95,100,104,109,113,118,122,127,131,136,141,145,150,155,159,164,169,174,179,183,188,193,198,203,208,213,218,223,228,233,238,243,248,253,258,264,269,274,279,284,290,295,300,305,311,316,321,326,332,337,342,348,353,359,364,369,375,380,386,391,397,402,408,413,419,424,430,435,441,447,452,458,463,469,475,480,486,491,497,503,509,514,520,526,531,537,543,549,554,560,566,572,577,583,589,595,601,606,612]def threshold (m e : ℕ) : Prop := m^e ≤ m * (m-1)^e ∧ m * (m-1)^(e+1) < m^(e+1)deriving instance Decidable for thresholddef test (i : ℕ) : Bool := Id.run dolet m := i + 2let e := exponents.getD i 0let n := 2*(m-1)+ereturn decide (threshold m e ∧ n ≤ 864 ∧ (2*(m+1) ≤ n ↔ 4 ≤ m)) &&(!(decide (2*(m+1) ≤ n)) || decide (fastValue n (m+1) < fastValue n m))set_option maxRecDepth 20000 inset_option maxHeartbeats 10000000 intheorem finite_check : ∀ i : Fin 126, test i.val = true := byrintro ⟨i, hi⟩change test i = trueinterval_cases i <;> as_aux_lemma => decide +kernel/-- From m=2 through 127 certify adjacent powers bracketing h(m−1), thefeasibility split, and strict descent at every feasible maximal integer n. -/theorem finite_endpoints (m : ℕ) (h₁ : 2 ≤ m) (h₂ : m < 128) :∃ e, threshold m e ∧ (let n := 2*(m-1)+e; n ≤ 864 ∧ (2*(m+1) ≤ n ↔ 4 ≤ m) ∧(2*(m+1) ≤ n → value n (m+1) < value n m)) := bylet i : Fin 126 := ⟨m-2, by omega⟩have hi : i.val + 2 = m := by dsimp [i]; omegahave ht := finite_check isimp [test, hi, fastValue_correct _ m (by omega), fastValue_correct _ (m+1) (by omega)] at htrefine ⟨exponents[i.val]?.getD 0, ht.1.1, ht.1.2.1, ht.1.2.2, fun hf => ?_⟩exact ht.2.resolve_left (not_lt_of_ge hf)/-- Convert the adjacent integer power inequalities to the real logarithmicbracket. Thus e really is the floor of the logarithmic part of h(m−1). -/theorem threshold_logs (m e : ℕ) (hm : 2 ≤ m) (ht : threshold m e) :(e : ℝ) ≤ Real.log (m : ℝ) / Real.log ((m : ℝ) / ((m-1 : ℕ) : ℝ)) ∧Real.log (m : ℝ) / Real.log ((m : ℝ) / ((m-1 : ℕ) : ℝ)) < (e : ℝ) + 1 := byhave hb : 0 < ((m-1 : ℕ) : ℝ) := by exact_mod_cast (show 0 < m-1 by omega)have ha : 0 < (m : ℝ) := by exact_mod_cast (show 0 < m by omega)have hab : ((m-1 : ℕ) : ℝ) < (m : ℝ) := by exact_mod_cast (show m-1 < m by omega)have hd := Real.log_pos ((one_lt_div hb).2 hab)have h₁ : (m : ℝ)^e ≤ m * ((m-1 : ℕ) : ℝ)^e := by exact_mod_cast ht.1have h₂ : (m : ℝ) * ((m-1 : ℕ) : ℝ)^(e+1) < (m : ℝ)^(e+1) := by exact_mod_cast ht.2have l₁ := Real.log_le_log (pow_pos ha _) h₁have l₂ := Real.log_lt_log (mul_pos ha (pow_pos hb _)) h₂rw [Real.log_mul ha.ne' (pow_pos hb _).ne', Real.log_pow, Real.log_pow] at l₁ l₂rw [le_div_iff₀ hd, div_lt_iff₀ hd, Real.log_div ha.ne' hb.ne']push_cast at l₂constructor <;> nlinarith/-- Endpoint certificate in the paper's logarithmic parametrization. -/theorem finite_transitions (m : ℕ) (h₁ : 2 ≤ m) (h₂ : m < 128) :∃ e : ℕ, ((e : ℝ) ≤ Real.log m / Real.log ((m : ℝ) / ((m-1 : ℕ) : ℝ)) ∧Real.log m / Real.log ((m : ℝ) / ((m-1 : ℕ) : ℝ)) < (e : ℝ)+1) ∧(let n := 2*(m-1)+e; n ≤ 864 ∧ (2*(m+1) ≤ n ↔ 4 ≤ m) ∧(2*(m+1) ≤ n → value n (m+1) < value n m)) := byobtain ⟨e, ht, h⟩ := finite_endpoints m h₁ h₂exact ⟨e, threshold_logs m e h₁ ht, h⟩theorem order_two (x : ℝ) (hx : Endpoint 2 x) :Maximizes 2 1 ∧ x < 1 ∧ 1 < x + 2 := byobtain ⟨hp, he⟩ := hxrefine ⟨⟨by decide, fun j hj => ?_⟩, ?_, by linarith⟩· rcases (show j = 0 ∨ j = 1 by omega) with rfl | rfl <;> decide· by_contra! hhhave hlog := Real.log_pos (show 1 < x+1 by linarith)have hd := Real.log_pos (show 1 < 1+1/x by have := one_div_pos.mpr hp; linarith)have := div_pos hlog hdnorm_num only [Nat.cast_ofNat] at helinariththeorem order_one (m : ℕ) (h : 2*m ≤ 1) : m = 0 := by omega/-- Exact rational domination of the first-moment error by the diagonal slack,valid on a larger domain than the analytic proof needs. -/theorem strict_budget (m N : ℝ) (hm : 0 < m) (hN : 0 ≤ N) :(2/m)*(16/33)*(4*N/(3*m)+14/3) < (2/m)*(8/3+N/m) := byhave : 0 ≤ N / m := div_nonneg hN hm.lerw [mul_assoc]apply mul_lt_mul_of_pos_left _ (by positivity)rw [show 4*N/(3*m) = (4/3)*(N/m) by ring]linarith/-- The sign needed in every shared column of two successive rows. -/theorem shared_column {m k : ℕ} (hk : 1 ≤ k) (hkm : k ≤ m) :Nat.stirlingSecond (m+1) k ≤ k^2 * Nat.stirlingSecond m k := byinduction m generalizing k with| zero => omega| succ m ih =>by_cases he : k = m+1· subst krw [Nat.stirlingSecond_succ_self_left, Nat.stirlingSecond_self, mul_one,Nat.choose_two_right]apply Nat.div_le_of_le_mulsimp only [Nat.add_sub_cancel]nlinarith [Nat.zero_le (m^2)]· by_cases ho : k = 1· subst ksimp [Nat.stirlingSecond_one_right]· obtain ⟨l, rfl⟩ := Nat.exists_eq_succ_of_ne_zero (show k ≠ 0 by omega)have a := ih (k := l+1) (by omega) (by omega)have b := ih (k := l) (by omega) (by omega)have hb : l^2 * Nat.stirlingSecond m l ≤ (l+1)^2 * Nat.stirlingSecond m l :=Nat.mul_le_mul_right _ (by nlinarith)calc_ = (l+1)*Nat.stirlingSecond (m+1) (l+1) + Nat.stirlingSecond (m+1) l := rfl_ ≤ (l+1)*((l+1)^2 * Nat.stirlingSecond m (l+1)) + (l+1)^2 * Nat.stirlingSecond m l :=Nat.add_le_add (Nat.mul_le_mul_left _ a) (b.trans hb)_ = _ := by rw [Nat.stirlingSecond_succ_succ]; simp only [Nat.succ_eq_add_one]; ring/-- Abstract monotonicity step: after division by the new diagonal base,all nonconstant terms have nonpositive coefficients and bases at most one. -/theorem signed_difference_mono (s : Finset ℕ) (d b : ℕ → ℝ) (C : ℝ)(hd : ∀ i ∈ s, 0 ≤ d i) (hb : ∀ i ∈ s, 0 < b i ∧ b i ≤ 1) :Monotone (fun N : ℝ => C - ∑ i ∈ s, d i * b i ^ (2*N)) := byintro N M hNMapply sub_le_sub_leftapply Finset.sum_le_sumintro i hiexact mul_le_mul_of_nonneg_left(Real.rpow_le_rpow_of_exponent_ge (hb i hi).1 (hb i hi).2 (by linarith)) (hd i hi)#print axioms finite_transitions#print axioms finite_endpoints#print axioms shared_column#print axioms signed_difference_mono#print axioms strict_budgetend LongtailProofs.LewisWonRepair