Lean / Reader
Lean reader

Equalizing.lean

143 lines22 declarationsno sorryimports Mathlib.Tactic
  1. import Mathlib.Tactic
  2. namespace Equalizing
  3. abbrev State := ℕ × ℕ
  4. /-- False increments the first coordinate and doubles the second. True does the reverse. -/
  5. def run : List Bool → State → State
  6. | [], p => p
  7. | false::w, (x,y) => run w (x+1,2*y)
  8. | true::w, (x,y) => run w (2*x,y+1)
  9. private theorem run_append (w v : List Bool) (p : State) : run (w++v) p = run v (run w p) := by
  10. induction w generalizing p with
  11. | nil => rfl
  12. | cons b w ih => rcases p with ⟨x,y⟩;cases b <;> apply ih
  13. private theorem left_run (i x y : ℕ) : run (List.replicate i false) (x,y)=(x+i,2^i*y) := by
  14. induction i generalizing x y with
  15. | zero => simp [run]
  16. | succ i ih => rw [List.replicate_succ,run,ih,pow_succ];congr 1 <;> ring
  17. private theorem lower_count (w : List Bool) (x y : ℕ) : 2^(w.count true)*x ≤ (run w (x,y)).1 := by
  18. induction w generalizing x y with
  19. | nil => simp [run]
  20. | cons b w ih =>
  21. cases b
  22. · simpa [run] using (show 2^w.count true*x≤_ from (Nat.mul_le_mul_left _ (Nat.le_succ x)).trans (ih (x+1) (2*y)))
  23. · simp [run,pow_succ,Nat.mul_assoc] at *
  24. exact ih _ _
  25. private def singleR (i c : ℕ) : List Bool := List.replicate i false ++ true::List.replicate c false
  26. private theorem classify (w : List Bool) :
  27. w=List.replicate w.length false ∨ (∃ i c, w=singleR i c) ∨ 2≤w.count true := by
  28. induction w with
  29. | nil => exact Or.inl rfl
  30. | cons b w ih =>
  31. cases b
  32. · rcases ih with e|⟨i,c,e⟩|h
  33. · left;simpa [List.replicate_succ] using congrArg (false::·) e
  34. · right;left;exact ⟨i+1,c,by rw [e];simp [singleR,List.replicate_succ]⟩
  35. · exact Or.inr (Or.inr (by simpa using h))
  36. · rcases ih with e|⟨i,c,e⟩|h
  37. · exact Or.inr (Or.inl ⟨0,w.length,by simpa [singleR] using congrArg (true::·) e⟩)
  38. · right;right;rw [e];simp [singleR,List.count_append];omega
  39. · right;right;simp only [List.count_cons_self];omega
  40. private theorem single_run (n i c : ℕ) : run (singleR i c) (n,0)=(2*(n+i)+c,2^c) := by
  41. rw [singleR,run_append,left_run]
  42. simp [run,left_run]
  43. /-- t starts at zero here. The number of final left moves is a positive multiple of four. -/
  44. def block (t : ℕ) := 4*t+4
  45. def scale (t : ℕ) := 4*16^t
  46. def start (t : ℕ) := 5*scale t-t-2
  47. def shortWord (t : ℕ) := [true,false,false,true] ++ List.replicate (block t) false
  48. def longWord (t : ℕ) := singleR (3*scale t-(t+1)) (block t+2)
  49. private theorem scale_lower (t : ℕ) : 4*(t+1) ≤ scale t := by
  50. induction t with
  51. | zero => decide
  52. | succ t ih => simp only [scale,pow_succ] at *;omega
  53. private theorem pow_block (t : ℕ) : 2^(block t)=4*scale t := by
  54. simp [block,scale,pow_add,pow_mul];ring
  55. theorem short_route (t : ℕ) : (shortWord t).length=block t+4 ∧
  56. run (shortWord t) (start t,0)=(20*scale t,20*scale t) := by
  57. constructor
  58. · simp [shortWord]
  59. · rw [shortWord,run_append]
  60. simp only [run,left_run,pow_block]
  61. have := scale_lower t
  62. unfold start block
  63. congr 1 <;> omega
  64. theorem long_route (t : ℕ) :
  65. run (longWord t) (start t,0)=(16*scale t,16*scale t) := by
  66. rw [longWord,single_run,pow_add,pow_block]
  67. have := scale_lower t
  68. unfold start block
  69. norm_num
  70. congr 1 <;> omega
  71. theorem short_lower (t m : ℕ) (w : List Bool) (len : w.length ≤ block t+4)
  72. (diag : run w (start t,0) = (m,m)) : 4*start t ≤ m := by
  73. have hk := scale_lower t
  74. rcases classify w with eqn|⟨i,c,rfl⟩|h
  75. · rw [eqn,left_run] at diag
  76. simp at diag
  77. unfold start at *;omega
  78. · rw [single_run] at diag
  79. simp only [Prod.mk.injEq] at diag
  80. simp [singleR] at len
  81. have ub : 2^(block t+1) = 8*scale t := by rw [pow_succ,pow_block];ring
  82. have low : block t+2 ≤ c := by
  83. by_contra! small
  84. have hh := Nat.pow_le_pow_right (by decide : 0<2) (show c≤block t+1 by omega)
  85. rw [ub] at hh
  86. unfold start at *;omega
  87. have lb : 2^(block t+2) = 16*scale t := by rw [pow_add,pow_block];ring
  88. have hh := Nat.pow_le_pow_right (by decide : 0<2) low
  89. rw [lb] at hh
  90. unfold start block at *;omega
  91. · have hh := lower_count w (start t) 0
  92. rw [diag] at hh
  93. exact (Nat.mul_le_mul_right _ (Nat.pow_le_pow_right (by decide : 0<2) h)).trans hh
  94. def Shortest (p : State) (w : List Bool) (m : ℕ) : Prop :=
  95. run w p=(m,m) ∧ ∀ v z, run v p=(z,z) → w.length≤v.length
  96. theorem family (t : ℕ) : (∃ w m, Shortest (start t,0) w m) ∧
  97. (∀ w m, Shortest (start t,0) w m → 16*scale t + 12*t + 8 ≤ m) ∧
  98. run (longWord t) (start t,0)=(16*scale t,16*scale t) := by
  99. classical
  100. obtain ⟨hs,hd⟩ := short_route t
  101. have possible : ∃ l, ∃ w m, w.length=l ∧ run w (start t,0)=(m,m) :=
  102. ⟨_,shortWord t,_,rfl,hd⟩
  103. obtain ⟨w,m,hl,hm⟩ := Nat.find_spec possible
  104. refine ⟨⟨w,m,hm,fun v z hv => hl.trans_le (Nat.find_min' possible ⟨v,z,rfl,hv⟩)⟩,?_,long_route t⟩
  105. intro v z hv
  106. have ub := (hv.2 _ _ hd).trans_eq hs
  107. have lb := short_lower t z v ub hv.1
  108. have hk := scale_lower t
  109. unfold start at lb
  110. omega
  111. /-- Original table minimizes the endpoint among the walks of minimum length. -/
  112. def TableEntry (p : State) (m : ℕ) : Prop :=
  113. (∃ w, Shortest p w m) ∧ ∀ v z, Shortest p v z → m≤z
  114. theorem refutes_table_conjecture : ¬ ∀ n k : ℕ, k≤n →
  115. ∃ m, TableEntry (n,k) m ∧ ∀ v z, run v (n,k)=(z,z) → m≤z := by
  116. intro alleged
  117. obtain ⟨m,h,global⟩ := alleged (start 0) 0 (Nat.zero_le _)
  118. obtain ⟨w,hw⟩ := h.1
  119. have lb := (family 0).2.1 w m hw
  120. have ub := global _ _ (long_route 0)
  121. omega
  122. #print axioms family
  123. #print axioms refutes_table_conjecture
  124. end Equalizing