Lean / Reader
Lean reader

Counterexample.lean

463 lines88 declarationsno sorryimports Std
  1. import Std
  2. /-! Literal trajectory-minimality conjecture for OEIS A306517.
  3. Words are ordinary binary digits, most significant digit first. -/
  4. namespace A306517
  5. abbrev Word := List Bool
  6. def value : Word → Nat
  7. | [] => 0
  8. | b :: w => (if b then 1 else 0) * 2 ^ w.length + value w
  9. theorem value_append (u v : Word) :
  10. value (u ++ v) = value u * 2 ^ v.length + value v := by
  11. induction u with
  12. | nil => simp [value]
  13. | cons b u ih =>
  14. simp [value, ih, Nat.pow_add, Nat.add_mul, Nat.mul_assoc, Nat.add_assoc]
  15. @[simp] theorem value_zeros (k : Nat) : value (List.replicate k false) = 0 := by
  16. induction k with
  17. | zero => rfl
  18. | succ k ih => simp [List.replicate_succ, value, ih]
  19. def canonical (w : Word) := w.head? = some true
  20. /-- An ordinary binary reverse-and-add step between canonical digit strings. -/
  21. def Step (u v : Word) : Prop :=
  22. canonical u ∧ canonical v ∧ value v = value u + value u.reverse
  23. inductive Walk : Word → Word → Prop where
  24. | single {u v} : Step u v → Walk u v
  25. | next {u v w} : Walk u v → Step v w → Walk u w
  26. def Earlier (w : Word) : Prop := ∃ u, value u < value w ∧ Walk u w
  27. def gramTail : Nat → Word
  28. | 0 => [true, false, true]
  29. | k + 1 => [true, false, true, false, false, true, false] ++ gramTail k
  30. def candidate (k : Nat) : Word := [true, false] ++ gramTail k ++ [false, false]
  31. def block : Word := [true, false, true, false, true, false, false]
  32. def prior : Word := [true, false, false, false, true, true]
  33. /-- k+1 copies of b, with g zeroes in each intervening gap. -/
  34. def spaced (b : Word) (g : Nat) : Nat → Word
  35. | 0 => b
  36. | k + 1 => spaced b g k ++ List.replicate g false ++ b
  37. theorem spaced_left (b : Word) (g k : Nat) :
  38. spaced b g (k+1) = b ++ List.replicate g false ++ spaced b g k := by
  39. induction k with
  40. | zero => rfl
  41. | succ k ih =>
  42. change (spaced b g (k+1) ++ List.replicate g false ++ b) = _
  43. calc
  44. _ = (b ++ List.replicate g false ++ spaced b g k) ++ List.replicate g false ++ b :=
  45. congrArg (fun w => w ++ List.replicate g false ++ b) ih
  46. _ = _ := by simp [spaced, List.append_assoc]
  47. theorem spaced_reverse (b : Word) (g k : Nat) :
  48. (spaced b g k).reverse = spaced b.reverse g k := by
  49. induction k with
  50. | zero => rfl
  51. | succ k ih =>
  52. rw [spaced_left b.reverse g k]
  53. simp [spaced, List.reverse_append, ih, List.append_assoc]
  54. def geom (q : Nat) : Nat → Nat
  55. | 0 => 1
  56. | k+1 => geom q k * q + 1
  57. theorem geom_pos (q k : Nat) : 0 < geom q k := by
  58. cases k <;> simp [geom]
  59. theorem spaced_value (b : Word) (g k : Nat) :
  60. value (spaced b g k) = value b * geom (2 ^ (g+b.length)) k := by
  61. induction k with
  62. | zero => simp [spaced, geom]
  63. | succ k ih =>
  64. simp [spaced, value_append, ih, geom, Nat.pow_add, Nat.mul_add, Nat.mul_assoc]
  65. theorem spaced_canonical (b : Word) (h : canonical b) (g k : Nat) :
  66. canonical (spaced b g k) := by
  67. cases k with
  68. | zero => exact h
  69. | succ k =>
  70. rw [spaced_left]
  71. cases b with
  72. | nil => contradiction
  73. | cons a b => exact h
  74. theorem candidate_spaced (k : Nat) : candidate k = spaced block 0 k := by
  75. induction k with
  76. | zero => rfl
  77. | succ k ih =>
  78. rw [spaced_left, ← ih]
  79. rfl
  80. theorem all_have_smaller_predecessors (k : Nat) :
  81. value (spaced prior 1 k) < value (candidate k) ∧ Step (spaced prior 1 k) (candidate k) := by
  82. rw [candidate_spaced]
  83. have positive := geom_pos 128 k
  84. refine ⟨?_, spaced_canonical prior rfl 1 k,
  85. spaced_canonical block rfl 0 k, ?_⟩
  86. · rw [spaced_value, spaced_value]
  87. change 35 * geom 128 k < 84 * geom 128 k
  88. omega
  89. · rw [spaced_reverse, spaced_value, spaced_value, spaced_value]
  90. change 84 * geom 128 k = 35 * geom 128 k + 49 * geom 128 k
  91. omega
  92. theorem no_candidate_is_trajectory_minimal (k : Nat) : Earlier (candidate k) := by
  93. obtain ⟨h₁, h₂⟩ := all_have_smaller_predecessors k
  94. exact ⟨_, h₁, .single h₂⟩
  95. theorem not_all_candidates_minimal : ¬ (∀ k, ¬ Earlier (candidate k)) := by
  96. intro h
  97. exact h 1 (no_candidate_is_trajectory_minimal 1)
  98. #print axioms no_candidate_is_trajectory_minimal
  99. #print axioms not_all_candidates_minimal
  100. /-- Even after restricting the earlier starting point to 10…00. -/
  101. def sameShape (s : Word) : Prop :=
  102. ∃ t : Word, s = [true,false] ++ t ++ [false,false]
  103. set_option maxRecDepth 8192 in
  104. theorem four_step_shaped_predecessor :
  105. ∃ u, sameShape u ∧ value u < value (candidate 1) ∧ Walk u (candidate 1) := by
  106. let u : Word := [true, false, false, false, true, true, true, true, false, false, false]
  107. let v : Word := [true, false, true, false, true, true, false, true, false, false, true]
  108. let w : Word := [true, false, true, false, false, false, false, true, true, true, true, false]
  109. let z : Word := [true, false, false, false, true, true, false, true, false, false, false, true, true]
  110. refine ⟨u, ⟨[false, false, true, true, true, true, false], rfl⟩, ?_, ?_⟩
  111. · decide
  112. · have uv : Step u v := ⟨rfl, rfl, by decide +kernel⟩
  113. have vw : Step v w := ⟨rfl, rfl, by decide +kernel⟩
  114. have wz : Step w z := ⟨rfl, rfl, by decide +kernel⟩
  115. have zt : Step z (candidate 1) := ⟨rfl, rfl, by decide +kernel⟩
  116. exact .next (.next (.next (.single uv) vw) wz) zt
  117. #print axioms four_step_shaped_predecessor
  118. theorem value_bound (w : Word) : value w < 2 ^ w.length := by
  119. induction w with
  120. | nil => decide
  121. | cons b w h =>
  122. cases b <;> simp [value, Nat.pow_succ] <;> omega
  123. theorem value_injective_fixed {u v : Word} (hlen : u.length = v.length)
  124. (h : value u = value v) : u = v := by
  125. induction u generalizing v with
  126. | nil => cases v <;> simp_all
  127. | cons p u ih =>
  128. cases v with
  129. | nil => simp at hlen
  130. | cons q v =>
  131. have hl : u.length = v.length := by simpa using hlen
  132. have hu := value_bound u
  133. have hv := value_bound v
  134. rw [hl] at hu
  135. cases p <;> cases q <;> simp [value, hl] at h <;>
  136. first | exact congrArg _ (ih hl h) | omega
  137. private theorem longer_larger (u v : Word) (hl : u.length < v.length) :
  138. value (true :: u) < value (true :: v) := by
  139. have hp : 2 ^ (u.length+1) ≤ 2 ^ v.length := Nat.pow_le_pow_right (by decide) hl
  140. rw [Nat.pow_succ] at hp
  141. have hu := value_bound u
  142. simp [value]
  143. omega
  144. theorem canonical_unique {u v : Word} (hu : canonical u) (hv : canonical v)
  145. (heq : value u = value v) : u = v := by
  146. cases u with
  147. | nil => contradiction
  148. | cons p u =>
  149. cases v with
  150. | nil => contradiction
  151. | cons q v =>
  152. cases p <;> cases q <;> simp [canonical] at hu hv
  153. apply value_injective_fixed ?_ heq
  154. simp only [List.length_cons, Nat.add_right_cancel_iff]
  155. by_cases hl : u.length < v.length
  156. · have := longer_larger u v hl
  157. omega
  158. by_cases hr : v.length < u.length
  159. · have := longer_larger v u hr
  160. omega
  161. omega
  162. theorem successor_unique {x y z : Word} (h : Step x y) (h' : Step x z) : y = z :=
  163. canonical_unique h.2.1 h'.2.1 (h.2.2.trans h'.2.2.symm)
  164. namespace Orbit
  165. structure Digits where
  166. scale : Int
  167. direct : Int
  168. reflected : Int
  169. deriving Repr, DecidableEq
  170. def sig (w : Word) : Digits :=
  171. ⟨2 ^ w.length, value w, value w.reverse⟩
  172. def cat (a b : Digits) : Digits :=
  173. ⟨a.scale * b.scale, a.direct * b.scale + b.direct,
  174. b.reflected * a.scale + a.reflected⟩
  175. theorem sig_append (u v : Word) : sig (u ++ v) = cat (sig u) (sig v) := by
  176. simp [sig, cat, List.reverse_append, value_append, Int.natCast_pow, Int.natCast_add,
  177. Int.natCast_mul, Int.pow_add]
  178. def copies (w : Word) : Nat → Word
  179. | 0 => []
  180. | k+1 => w ++ copies w k
  181. def geom0 (q : Int) : Nat → Int
  182. | 0 => 0
  183. | k+1 => geom0 q k * q + 1
  184. theorem geom0_succ (q : Int) (k : Nat) : geom0 q (k+1) = geom0 q k * q + 1 := rfl
  185. theorem sig_copies (w : Word) (q a b : Int) (h : sig w = ⟨q,a,b⟩) (k : Nat) :
  186. sig (copies w k) = ⟨(q-1)*geom0 q k + 1, a*geom0 q k, b*geom0 q k⟩ := by
  187. induction k with
  188. | zero => simp [copies, geom0, sig, value]
  189. | succ k ih =>
  190. rw [copies, sig_append, h, ih]
  191. rw [geom0_succ]
  192. unfold cat
  193. congr 1 <;> grind only
  194. private theorem sig_10 : sig [true, false] = ⟨4, 2, 1⟩ := rfl
  195. private theorem sig_11 : sig [true, true] = ⟨4, 3, 3⟩ := rfl
  196. private theorem sig_00 : sig [false, false] = ⟨4, 0, 0⟩ := rfl
  197. private theorem sig_01 : sig [false, true] = ⟨4, 1, 2⟩ := rfl
  198. private theorem sig_1010010 : sig [true, false, true, false, false, true, false] = ⟨128, 82, 37⟩ := rfl
  199. private theorem sig_0100111 : sig [false, true, false, false, true, true, true] = ⟨128, 39, 114⟩ := rfl
  200. private theorem sig_1101010 : sig [true, true, false, true, false, true, false] = ⟨128, 106, 43⟩ := rfl
  201. private theorem sig_1000101 : sig [true, false, false, false, true, false, true] = ⟨128, 69, 81⟩ := rfl
  202. private theorem sig_101 : sig [true, false, true] = ⟨8, 5, 5⟩ := rfl
  203. private theorem sig_010 : sig [false, true, false] = ⟨8, 2, 2⟩ := rfl
  204. private theorem sig_1101 : sig [true, true, false, true] = ⟨16, 13, 11⟩ := rfl
  205. private theorem sig_1000 : sig [true, false, false, false] = ⟨16, 8, 1⟩ := rfl
  206. private theorem sig_1 : sig [true] = ⟨2, 1, 1⟩ := rfl
  207. private theorem sig_0 : sig [false] = ⟨2, 0, 0⟩ := rfl
  208. def a (m j : Nat) : Word :=
  209. [true, false] ++ copies [true] j ++ copies [true, false, true, false, false, true, false] m ++
  210. [true, false, true] ++ copies [false] j ++ [false, false]
  211. def b (m j : Nat) : Word :=
  212. [true, true] ++ copies [false] j ++ copies [false, true, false, false, true, true, true] m ++
  213. [false, true, false] ++ copies [true] j ++ [false, true]
  214. def c (m j : Nat) : Word :=
  215. [true, false] ++ copies [true] j ++ copies [true, true, false, true, false, true, false] m ++
  216. [true, true, false, true] ++ copies [false] j ++ [false, false]
  217. def d (m j : Nat) : Word :=
  218. [true, true] ++ copies [false] j ++ copies [true, false, false, false, true, false, true] m ++
  219. [true, false, false, false] ++ copies [true] j ++ [false, true]
  220. private theorem of_sig {u v : Word} (hu : canonical u) (hv : canonical v)
  221. (h : (sig v).direct = (sig u).direct + (sig u).reflected) : Step u v := by
  222. refine ⟨hu, hv, ?_⟩
  223. have h' : (value v : Int) = (value u : Int) + (value u.reverse : Int) := h
  224. omega
  225. macro "orbit_calc" : tactic => `(tactic|
  226. (apply of_sig
  227. · rfl
  228. · rfl
  229. simp only [a, b, c, d, sig_append,
  230. sig_copies _ _ _ _ sig_1, sig_copies _ _ _ _ sig_0,
  231. sig_copies _ _ _ _ sig_1010010, sig_copies _ _ _ _ sig_0100111,
  232. sig_copies _ _ _ _ sig_1101010, sig_copies _ _ _ _ sig_1000101,
  233. sig_10, sig_11, sig_00, sig_01, sig_101, sig_010, sig_1101, sig_1000, cat]
  234. try simp only [geom0_succ]
  235. grind only))
  236. theorem four_phases (m j : Nat) :
  237. Step (a m j) (b m j) ∧ Step (b m j) (c m j) ∧
  238. Step (c m j) (d m j) ∧ Step (d m j) (a m (j+1)) := by
  239. refine ⟨?_, ?_, ?_, ?_⟩ <;> orbit_calc
  240. theorem gramTail_letters (m : Nat) :
  241. gramTail m = copies [true, false, true, false, false, true, false] m ++ [true, false, true] := by
  242. induction m with
  243. | zero => rfl
  244. | succ m ih => simp [gramTail, copies, ih]
  245. theorem starting_word (m : Nat) : candidate m = a m 0 := by
  246. simp [candidate, a, copies, gramTail_letters, List.append_assoc]
  247. #print axioms four_phases
  248. #print axioms starting_word
  249. /-- Count changes of bit, including a specified bit immediately to the left. -/
  250. def scan (previous : Bool) : Word → Nat × Bool
  251. | [] => (0, previous)
  252. | digit :: rest =>
  253. let r := scan digit rest
  254. ((if previous = digit then 0 else 1) + r.1, r.2)
  255. theorem scan_append (p : Bool) (u v : Word) :
  256. scan p (u ++ v) =
  257. let s := scan p u
  258. let t := scan s.2 v
  259. (s.1 + t.1, t.2) := by
  260. induction u generalizing p with
  261. | nil => simp [scan]
  262. | cons h u ih => simp [scan, ih, Nat.add_assoc]
  263. theorem scan_copies (p : Bool) (w : Word) (weight : Nat)
  264. (h : scan p w = (weight,p)) (k : Nat) :
  265. scan p (copies w k) = (weight*k,p) := by
  266. induction k with
  267. | zero => simp [copies, scan]
  268. | succ k ih => simp [copies, scan_append, h, ih, Nat.mul_succ, Nat.add_comm]
  269. private theorem scan_ones (k : Nat) :
  270. scan true (copies [true] k) = (0,true) := by
  271. simpa using scan_copies true [true] 0 rfl k
  272. private theorem scan_zeros (k : Nat) :
  273. scan false (copies [false] k) = (0,false) := by
  274. simpa using scan_copies false [false] 0 rfl k
  275. private theorem scan_ones_succ (k : Nat) :
  276. scan false (copies [true] (k+1)) = (1,true) := by
  277. simp [copies, scan, scan_ones]
  278. private theorem scan_zeros_succ (k : Nat) :
  279. scan true (copies [false] (k+1)) = (1,false) := by
  280. simp [copies, scan, scan_zeros]
  281. private theorem scan_core0 (k : Nat) :
  282. scan false (copies [true, false, true, false, false, true, false] k) = (6*k,false) :=
  283. scan_copies false _ 6 rfl k
  284. private theorem scan_core0_succ (k : Nat) :
  285. scan true (copies [true, false, true, false, false, true, false] (k+1)) = (5+6*k,false) := by
  286. rw [copies, scan_append]
  287. change (5+(scan false (copies _ k)).1,(scan false (copies _ k)).2) = _
  288. rw [scan_core0]
  289. private theorem scan_core1 (k : Nat) :
  290. scan false (copies [true, true, false, true, false, true, false] k) = (6*k,false) :=
  291. scan_copies false _ 6 rfl k
  292. private theorem scan_core1_succ (k : Nat) :
  293. scan true (copies [true, true, false, true, false, true, false] (k+1)) = (5+6*k,false) := by
  294. rw [copies, scan_append]
  295. change (5+(scan false (copies _ k)).1,(scan false (copies _ k)).2) = _
  296. rw [scan_core1]
  297. theorem even_phase_changes (m j : Nat) :
  298. (scan true (a m j)).1 = 6*m+5 ∧ (scan true (c m j)).1 = 6*m+5 := by
  299. cases j <;> cases m <;>
  300. simp only [a, c, scan_append, scan_ones, scan_zeros,
  301. scan_core0, scan_core1, copies, scan,
  302. List.nil_append, List.cons_append, Nat.zero_add,
  303. Nat.add_zero, Nat.mul_succ, Nat.mul_zero, Bool.false_eq_true, Bool.true_eq_false,
  304. if_false, if_true, and_self] <;> omega
  305. theorem different_even_words {m n j k : Nat} (h : m ≠ n) :
  306. a m j ≠ a n k ∧ a m j ≠ c n k ∧ c m j ≠ a n k ∧ c m j ≠ c n k := by
  307. obtain ⟨ha, hc⟩ := even_phase_changes m j
  308. obtain ⟨hb, hd⟩ := even_phase_changes n k
  309. constructor
  310. · intro e; rw [e, hb] at ha; omega
  311. constructor
  312. · intro e; rw [e, hd] at ha; omega
  313. constructor
  314. · intro e; rw [e, hb] at hc; omega
  315. · intro e; rw [e, hd] at hc; omega
  316. theorem all_nonpalindromic (m j : Nat) :
  317. (a m j).reverse ≠ a m j ∧ (b m j).reverse ≠ b m j ∧
  318. (c m j).reverse ≠ c m j ∧ (d m j).reverse ≠ d m j := by
  319. refine ⟨?_, ?_, ?_, ?_⟩ <;> intro e <;>
  320. have h := congrArg (List.take 2) e <;>
  321. simp [a, b, c, d, List.reverse_append] at h
  322. #print axioms all_nonpalindromic
  323. #print axioms different_even_words
  324. def EvenPhase (m : Nat) (w : Word) : Prop := ∃ j, w = a m j ∨ w = c m j
  325. def OddPhase (m : Nat) (w : Word) : Prop := ∃ j, w = b m j ∨ w = d m j
  326. def OnTrack (m : Nat) (w : Word) : Prop := EvenPhase m w ∨ OddPhase m w
  327. private theorem even_last {m w} (h : EvenPhase m w) : w.getLast? = some false := by
  328. obtain ⟨j, h | h⟩ := h <;> subst w <;>
  329. rw [← List.head?_reverse] <;> simp [a, c, List.reverse_append]
  330. private theorem odd_last {m w} (h : OddPhase m w) : w.getLast? = some true := by
  331. obtain ⟨j, h | h⟩ := h <;> subst w <;>
  332. rw [← List.head?_reverse] <;> simp [b, d, List.reverse_append]
  333. private theorem even_disjoint {m n w} (hn : m ≠ n)
  334. (hm : EvenPhase m w) (hn' : EvenPhase n w) : False := by
  335. obtain ⟨j, h⟩ := hm
  336. obtain ⟨k, h'⟩ := hn'
  337. obtain ⟨haa, hac, hca, hcc⟩ := different_even_words (j := j) (k := k) hn
  338. rcases h with h | h <;> rcases h' with h' | h' <;> grind only
  339. private theorem odd_next {m w} (h : OddPhase m w) :
  340. ∃ v, Step w v ∧ EvenPhase m v := by
  341. obtain ⟨j, h | h⟩ := h <;> subst w
  342. · exact ⟨c m j, (four_phases m j).2.1, j, Or.inr rfl⟩
  343. · exact ⟨a m (j+1), (four_phases m j).2.2.2, j+1, Or.inl rfl⟩
  344. theorem tracks_disjoint {m n w} (h : m ≠ n) (hm : OnTrack m w) (hn : OnTrack n w) : False := by
  345. rcases hm with hm | hm <;> rcases hn with hn | hn
  346. · exact even_disjoint h hm hn
  347. · have := even_last hm
  348. have := odd_last hn
  349. grind only
  350. · have := odd_last hm
  351. have := even_last hn
  352. grind only
  353. · obtain ⟨x, sx, hx⟩ := odd_next hm
  354. obtain ⟨y, sy, hy⟩ := odd_next hn
  355. have e := successor_unique sx sy
  356. subst y
  357. exact even_disjoint h hx hy
  358. private theorem step_stays {m u v} (h : OnTrack m u) (s : Step u v) : OnTrack m v := by
  359. rcases h with h | h
  360. · obtain ⟨j, h | h⟩ := h <;> subst u
  361. · exact Or.inr ⟨j, Or.inl (successor_unique s (four_phases m j).1)⟩
  362. · exact Or.inr ⟨j, Or.inr (successor_unique s (four_phases m j).2.2.1)⟩
  363. · obtain ⟨z, sz, hz⟩ := odd_next h
  364. have e := successor_unique s sz
  365. subst v
  366. exact Or.inl hz
  367. private theorem walk_stays {m u v} (h : OnTrack m u) (t : Walk u v) : OnTrack m v := by
  368. induction t with
  369. | single e => exact step_stays h e
  370. | next t e ih => exact step_stays ih e
  371. def InOrbit (m : Nat) (w : Word) : Prop := w = candidate m ∨ Walk (candidate m) w
  372. theorem orbit_on_track {m w} (h : InOrbit m w) : OnTrack m w := by
  373. have start : OnTrack m (candidate m) := Or.inl ⟨0, Or.inl (starting_word m)⟩
  374. rcases h with rfl | h
  375. · exact start
  376. · exact walk_stays start h
  377. theorem disjoint_orbits {m n w} (h : m ≠ n) (hm : InOrbit m w) (hn : InOrbit n w) : False :=
  378. tracks_disjoint h (orbit_on_track hm) (orbit_on_track hn)
  379. theorem no_orbit_word_palindromic {m w} (h : InOrbit m w) : w.reverse ≠ w := by
  380. rcases orbit_on_track h with ⟨j, e | e⟩ | ⟨j, e | e⟩ <;> subst w
  381. · exact (all_nonpalindromic m j).1
  382. · exact (all_nonpalindromic m j).2.2.1
  383. · exact (all_nonpalindromic m j).2.1
  384. · exact (all_nonpalindromic m j).2.2.2
  385. #print axioms disjoint_orbits
  386. #print axioms no_orbit_word_palindromic
  387. end Orbit
  388. end A306517