Lean / Reader
Lean reader

LewisWonRepair.lean

191 lines21 declarationsno sorryimports LongtailProofs.LewisWon317 LongtailProofs.LewisWonRows Mathlib.Analysis.SpecialFunctions.Pow.Real Mathlib.Tactic.IntervalCases

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.

  1. import LongtailProofs.LewisWon317
  2. import LongtailProofs.LewisWonRows
  3. import Mathlib.Analysis.SpecialFunctions.Pow.Real
  4. import Mathlib.Tactic.IntervalCases
  5. /-! Finite and algebraic parts of the repaired Lewis–Won interval theorem.
  6. `RepairedInterval` states the target, not a claimed Lean proof of the analytic tail. -/
  7. namespace LongtailProofs.LewisWonRepair
  8. open LongtailProofs.LewisWon317
  9. def RepairedInterval : Prop := ∀ n : ℕ, 4 ≤ n → ∀ x : ℝ, Endpoint n x →
  10. ∀ m : ℕ, Maximizes n m → x < (m : ℝ) ∧ (m : ℝ) < x + 2
  11. /-- Dynamic programming, with out-of-range columns interpreted as zero. -/
  12. def nextRow (s : ℕ) (a : Array ℕ) :=
  13. Array.ofFn fun i : Fin (s+2) => match i.val with
  14. | 0 => 0
  15. | k+1 => (k+1) * a.getD (k+1) 0 + a.getD k 0
  16. def row : ℕ → Array ℕ
  17. | 0 => #[1]
  18. | s+1 => nextRow s (row s)
  19. theorem row_correct (s k : ℕ) : (row s).getD k 0 = Nat.stirlingSecond s k := by
  20. induction s generalizing k with
  21. | zero => cases k with
  22. | zero => rfl
  23. | succ k => simp [row, Array.getD, Nat.stirlingSecond]
  24. | succ s ih =>
  25. rw [Array.getD_eq_getD_getElem?]
  26. simp only [row, nextRow, Array.getElem?_ofFn]
  27. split
  28. · cases k with
  29. | zero => rfl
  30. | succ k => simp [ih, Nat.stirlingSecond_succ_succ]
  31. · rename_i hn
  32. rw [Nat.stirlingSecond_eq_zero_of_lt (by omega)]
  33. rfl
  34. def certRow (m : ℕ) : Array ℕ := certificates.getD m #[]
  35. set_option maxRecDepth 10000 in
  36. set_option maxHeartbeats 10000000 in
  37. theorem cert_steps : ∀ s : Fin 128, nextRow s.val (certRow s.val) = certRow (s.val+1) := by
  38. rintro ⟨i, hi⟩
  39. change nextRow i (certRow i) = certRow (i+1)
  40. interval_cases i <;> as_aux_lemma => decide +kernel
  41. set_option maxRecDepth 10000 in
  42. theorem cert_eq_row (m : ℕ) (hm : m ≤ 128) : certRow m = row m := by
  43. induction m with
  44. | zero => rfl
  45. | succ m ih =>
  46. exact (cert_steps ⟨m, by omega⟩).symm.trans (congrArg (nextRow m) (ih (by omega)))
  47. def fastValue (n m : ℕ) : ℕ :=
  48. let a := certRow m
  49. ∑ i ∈ Finset.range m, (a.getD (i+1) 0 * (i+1)^(n-2*m) * (i+1).factorial)^2
  50. theorem fastValue_correct (n m : ℕ) (hm : m ≤ 128) : fastValue n m = value n m := by
  51. simp only [fastValue, value, cert_eq_row m hm, row_correct]
  52. /-- Candidate exponents; the certificate checks each value on BOTH sides. -/
  53. 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]
  54. def threshold (m e : ℕ) : Prop := m^e ≤ m * (m-1)^e ∧ m * (m-1)^(e+1) < m^(e+1)
  55. deriving instance Decidable for threshold
  56. def test (i : ℕ) : Bool := Id.run do
  57. let m := i + 2
  58. let e := exponents.getD i 0
  59. let n := 2*(m-1)+e
  60. return decide (threshold m e ∧ n ≤ 864 ∧ (2*(m+1) ≤ n ↔ 4 ≤ m)) &&
  61. (!(decide (2*(m+1) ≤ n)) || decide (fastValue n (m+1) < fastValue n m))
  62. set_option maxRecDepth 20000 in
  63. set_option maxHeartbeats 10000000 in
  64. theorem finite_check : ∀ i : Fin 126, test i.val = true := by
  65. rintro ⟨i, hi⟩
  66. change test i = true
  67. interval_cases i <;> as_aux_lemma => decide +kernel
  68. /-- From m=2 through 127 certify adjacent powers bracketing h(m−1), the
  69. feasibility split, and strict descent at every feasible maximal integer n. -/
  70. theorem finite_endpoints (m : ℕ) (h₁ : 2 ≤ m) (h₂ : m < 128) :
  71. ∃ e, threshold m e ∧ (let n := 2*(m-1)+e; n ≤ 864 ∧ (2*(m+1) ≤ n ↔ 4 ≤ m) ∧
  72. (2*(m+1) ≤ n → value n (m+1) < value n m)) := by
  73. let i : Fin 126 := ⟨m-2, by omega⟩
  74. have hi : i.val + 2 = m := by dsimp [i]; omega
  75. have ht := finite_check i
  76. simp [test, hi, fastValue_correct _ m (by omega), fastValue_correct _ (m+1) (by omega)] at ht
  77. refine ⟨exponents[i.val]?.getD 0, ht.1.1, ht.1.2.1, ht.1.2.2, fun hf => ?_⟩
  78. exact ht.2.resolve_left (not_lt_of_ge hf)
  79. /-- Convert the adjacent integer power inequalities to the real logarithmic
  80. bracket. Thus e really is the floor of the logarithmic part of h(m−1). -/
  81. theorem threshold_logs (m e : ℕ) (hm : 2 ≤ m) (ht : threshold m e) :
  82. (e : ℝ) ≤ Real.log (m : ℝ) / Real.log ((m : ℝ) / ((m-1 : ℕ) : ℝ)) ∧
  83. Real.log (m : ℝ) / Real.log ((m : ℝ) / ((m-1 : ℕ) : ℝ)) < (e : ℝ) + 1 := by
  84. have hb : 0 < ((m-1 : ℕ) : ℝ) := by exact_mod_cast (show 0 < m-1 by omega)
  85. have ha : 0 < (m : ℝ) := by exact_mod_cast (show 0 < m by omega)
  86. have hab : ((m-1 : ℕ) : ℝ) < (m : ℝ) := by exact_mod_cast (show m-1 < m by omega)
  87. have hd := Real.log_pos ((one_lt_div hb).2 hab)
  88. have h₁ : (m : ℝ)^e ≤ m * ((m-1 : ℕ) : ℝ)^e := by exact_mod_cast ht.1
  89. have h₂ : (m : ℝ) * ((m-1 : ℕ) : ℝ)^(e+1) < (m : ℝ)^(e+1) := by exact_mod_cast ht.2
  90. have l₁ := Real.log_le_log (pow_pos ha _) h₁
  91. have l₂ := Real.log_lt_log (mul_pos ha (pow_pos hb _)) h₂
  92. rw [Real.log_mul ha.ne' (pow_pos hb _).ne', Real.log_pow, Real.log_pow] at l₁ l₂
  93. rw [le_div_iff₀ hd, div_lt_iff₀ hd, Real.log_div ha.ne' hb.ne']
  94. push_cast at l₂
  95. constructor <;> nlinarith
  96. /-- Endpoint certificate in the paper's logarithmic parametrization. -/
  97. theorem finite_transitions (m : ℕ) (h₁ : 2 ≤ m) (h₂ : m < 128) :
  98. ∃ e : ℕ, ((e : ℝ) ≤ Real.log m / Real.log ((m : ℝ) / ((m-1 : ℕ) : ℝ)) ∧
  99. Real.log m / Real.log ((m : ℝ) / ((m-1 : ℕ) : ℝ)) < (e : ℝ)+1) ∧
  100. (let n := 2*(m-1)+e; n ≤ 864 ∧ (2*(m+1) ≤ n ↔ 4 ≤ m) ∧
  101. (2*(m+1) ≤ n → value n (m+1) < value n m)) := by
  102. obtain ⟨e, ht, h⟩ := finite_endpoints m h₁ h₂
  103. exact ⟨e, threshold_logs m e h₁ ht, h⟩
  104. theorem order_two (x : ℝ) (hx : Endpoint 2 x) :
  105. Maximizes 2 1 ∧ x < 1 ∧ 1 < x + 2 := by
  106. obtain ⟨hp, he⟩ := hx
  107. refine ⟨⟨by decide, fun j hj => ?_⟩, ?_, by linarith⟩
  108. · rcases (show j = 0 ∨ j = 1 by omega) with rfl | rfl <;> decide
  109. · by_contra! hh
  110. have hlog := Real.log_pos (show 1 < x+1 by linarith)
  111. have hd := Real.log_pos (show 1 < 1+1/x by have := one_div_pos.mpr hp; linarith)
  112. have := div_pos hlog hd
  113. norm_num only [Nat.cast_ofNat] at he
  114. linarith
  115. theorem order_one (m : ℕ) (h : 2*m ≤ 1) : m = 0 := by omega
  116. /-- Exact rational domination of the first-moment error by the diagonal slack,
  117. valid on a larger domain than the analytic proof needs. -/
  118. theorem strict_budget (m N : ℝ) (hm : 0 < m) (hN : 0 ≤ N) :
  119. (2/m)*(16/33)*(4*N/(3*m)+14/3) < (2/m)*(8/3+N/m) := by
  120. have : 0 ≤ N / m := div_nonneg hN hm.le
  121. rw [mul_assoc]
  122. apply mul_lt_mul_of_pos_left _ (by positivity)
  123. rw [show 4*N/(3*m) = (4/3)*(N/m) by ring]
  124. linarith
  125. /-- The sign needed in every shared column of two successive rows. -/
  126. theorem shared_column {m k : ℕ} (hk : 1 ≤ k) (hkm : k ≤ m) :
  127. Nat.stirlingSecond (m+1) k ≤ k^2 * Nat.stirlingSecond m k := by
  128. induction m generalizing k with
  129. | zero => omega
  130. | succ m ih =>
  131. by_cases he : k = m+1
  132. · subst k
  133. rw [Nat.stirlingSecond_succ_self_left, Nat.stirlingSecond_self, mul_one,
  134. Nat.choose_two_right]
  135. apply Nat.div_le_of_le_mul
  136. simp only [Nat.add_sub_cancel]
  137. nlinarith [Nat.zero_le (m^2)]
  138. · by_cases ho : k = 1
  139. · subst k
  140. simp [Nat.stirlingSecond_one_right]
  141. · obtain ⟨l, rfl⟩ := Nat.exists_eq_succ_of_ne_zero (show k ≠ 0 by omega)
  142. have a := ih (k := l+1) (by omega) (by omega)
  143. have b := ih (k := l) (by omega) (by omega)
  144. have hb : l^2 * Nat.stirlingSecond m l ≤ (l+1)^2 * Nat.stirlingSecond m l :=
  145. Nat.mul_le_mul_right _ (by nlinarith)
  146. calc
  147. _ = (l+1)*Nat.stirlingSecond (m+1) (l+1) + Nat.stirlingSecond (m+1) l := rfl
  148. _ ≤ (l+1)*((l+1)^2 * Nat.stirlingSecond m (l+1)) + (l+1)^2 * Nat.stirlingSecond m l :=
  149. Nat.add_le_add (Nat.mul_le_mul_left _ a) (b.trans hb)
  150. _ = _ := by rw [Nat.stirlingSecond_succ_succ]; simp only [Nat.succ_eq_add_one]; ring
  151. /-- Abstract monotonicity step: after division by the new diagonal base,
  152. all nonconstant terms have nonpositive coefficients and bases at most one. -/
  153. theorem signed_difference_mono (s : Finset ℕ) (d b : ℕ → ℝ) (C : ℝ)
  154. (hd : ∀ i ∈ s, 0 ≤ d i) (hb : ∀ i ∈ s, 0 < b i ∧ b i ≤ 1) :
  155. Monotone (fun N : ℝ => C - ∑ i ∈ s, d i * b i ^ (2*N)) := by
  156. intro N M hNM
  157. apply sub_le_sub_left
  158. apply Finset.sum_le_sum
  159. intro i hi
  160. exact mul_le_mul_of_nonneg_left
  161. (Real.rpow_le_rpow_of_exponent_ge (hb i hi).1 (hb i hi).2 (by linarith)) (hd i hi)
  162. #print axioms finite_transitions
  163. #print axioms finite_endpoints
  164. #print axioms shared_column
  165. #print axioms signed_difference_mono
  166. #print axioms strict_budget
  167. end LongtailProofs.LewisWonRepair