import Std/-! Literal trajectory-minimality conjecture for OEIS A306517.Words are ordinary binary digits, most significant digit first. -/namespace A306517abbrev Word := List Booldef value : Word → Nat| [] => 0| b :: w => (if b then 1 else 0) * 2 ^ w.length + value wtheorem value_append (u v : Word) :value (u ++ v) = value u * 2 ^ v.length + value v := byinduction u with| nil => simp [value]| cons b u ih =>simp [value, ih, Nat.pow_add, Nat.add_mul, Nat.mul_assoc, Nat.add_assoc]@[simp] theorem value_zeros (k : Nat) : value (List.replicate k false) = 0 := byinduction k with| zero => rfl| succ k ih => simp [List.replicate_succ, value, ih]def canonical (w : Word) := w.head? = some true/-- An ordinary binary reverse-and-add step between canonical digit strings. -/def Step (u v : Word) : Prop :=canonical u ∧ canonical v ∧ value v = value u + value u.reverseinductive Walk : Word → Word → Prop where| single {u v} : Step u v → Walk u v| next {u v w} : Walk u v → Step v w → Walk u wdef Earlier (w : Word) : Prop := ∃ u, value u < value w ∧ Walk u wdef gramTail : Nat → Word| 0 => [true, false, true]| k + 1 => [true, false, true, false, false, true, false] ++ gramTail kdef candidate (k : Nat) : Word := [true, false] ++ gramTail k ++ [false, false]def block : Word := [true, false, true, false, true, false, false]def prior : Word := [true, false, false, false, true, true]/-- k+1 copies of b, with g zeroes in each intervening gap. -/def spaced (b : Word) (g : Nat) : Nat → Word| 0 => b| k + 1 => spaced b g k ++ List.replicate g false ++ btheorem spaced_left (b : Word) (g k : Nat) :spaced b g (k+1) = b ++ List.replicate g false ++ spaced b g k := byinduction k with| zero => rfl| succ k ih =>change (spaced b g (k+1) ++ List.replicate g false ++ b) = _calc_ = (b ++ List.replicate g false ++ spaced b g k) ++ List.replicate g false ++ b :=congrArg (fun w => w ++ List.replicate g false ++ b) ih_ = _ := by simp [spaced, List.append_assoc]theorem spaced_reverse (b : Word) (g k : Nat) :(spaced b g k).reverse = spaced b.reverse g k := byinduction k with| zero => rfl| succ k ih =>rw [spaced_left b.reverse g k]simp [spaced, List.reverse_append, ih, List.append_assoc]def geom (q : Nat) : Nat → Nat| 0 => 1| k+1 => geom q k * q + 1theorem geom_pos (q k : Nat) : 0 < geom q k := bycases k <;> simp [geom]theorem spaced_value (b : Word) (g k : Nat) :value (spaced b g k) = value b * geom (2 ^ (g+b.length)) k := byinduction k with| zero => simp [spaced, geom]| succ k ih =>simp [spaced, value_append, ih, geom, Nat.pow_add, Nat.mul_add, Nat.mul_assoc]theorem spaced_canonical (b : Word) (h : canonical b) (g k : Nat) :canonical (spaced b g k) := bycases k with| zero => exact h| succ k =>rw [spaced_left]cases b with| nil => contradiction| cons a b => exact htheorem candidate_spaced (k : Nat) : candidate k = spaced block 0 k := byinduction k with| zero => rfl| succ k ih =>rw [spaced_left, ← ih]rfltheorem all_have_smaller_predecessors (k : Nat) :value (spaced prior 1 k) < value (candidate k) ∧ Step (spaced prior 1 k) (candidate k) := byrw [candidate_spaced]have positive := geom_pos 128 krefine ⟨?_, spaced_canonical prior rfl 1 k,spaced_canonical block rfl 0 k, ?_⟩· rw [spaced_value, spaced_value]change 35 * geom 128 k < 84 * geom 128 komega· rw [spaced_reverse, spaced_value, spaced_value, spaced_value]change 84 * geom 128 k = 35 * geom 128 k + 49 * geom 128 komegatheorem no_candidate_is_trajectory_minimal (k : Nat) : Earlier (candidate k) := byobtain ⟨h₁, h₂⟩ := all_have_smaller_predecessors kexact ⟨_, h₁, .single h₂⟩theorem not_all_candidates_minimal : ¬ (∀ k, ¬ Earlier (candidate k)) := byintro hexact h 1 (no_candidate_is_trajectory_minimal 1)#print axioms no_candidate_is_trajectory_minimal#print axioms not_all_candidates_minimal/-- Even after restricting the earlier starting point to 10…00. -/def sameShape (s : Word) : Prop :=∃ t : Word, s = [true,false] ++ t ++ [false,false]set_option maxRecDepth 8192 intheorem four_step_shaped_predecessor :∃ u, sameShape u ∧ value u < value (candidate 1) ∧ Walk u (candidate 1) := bylet u : Word := [true, false, false, false, true, true, true, true, false, false, false]let v : Word := [true, false, true, false, true, true, false, true, false, false, true]let w : Word := [true, false, true, false, false, false, false, true, true, true, true, false]let z : Word := [true, false, false, false, true, true, false, true, false, false, false, true, true]refine ⟨u, ⟨[false, false, true, true, true, true, false], rfl⟩, ?_, ?_⟩· decide· have uv : Step u v := ⟨rfl, rfl, by decide +kernel⟩have vw : Step v w := ⟨rfl, rfl, by decide +kernel⟩have wz : Step w z := ⟨rfl, rfl, by decide +kernel⟩have zt : Step z (candidate 1) := ⟨rfl, rfl, by decide +kernel⟩exact .next (.next (.next (.single uv) vw) wz) zt#print axioms four_step_shaped_predecessortheorem value_bound (w : Word) : value w < 2 ^ w.length := byinduction w with| nil => decide| cons b w h =>cases b <;> simp [value, Nat.pow_succ] <;> omegatheorem value_injective_fixed {u v : Word} (hlen : u.length = v.length)(h : value u = value v) : u = v := byinduction u generalizing v with| nil => cases v <;> simp_all| cons p u ih =>cases v with| nil => simp at hlen| cons q v =>have hl : u.length = v.length := by simpa using hlenhave hu := value_bound uhave hv := value_bound vrw [hl] at hucases p <;> cases q <;> simp [value, hl] at h <;>first | exact congrArg _ (ih hl h) | omegaprivate theorem longer_larger (u v : Word) (hl : u.length < v.length) :value (true :: u) < value (true :: v) := byhave hp : 2 ^ (u.length+1) ≤ 2 ^ v.length := Nat.pow_le_pow_right (by decide) hlrw [Nat.pow_succ] at hphave hu := value_bound usimp [value]omegatheorem canonical_unique {u v : Word} (hu : canonical u) (hv : canonical v)(heq : value u = value v) : u = v := bycases u with| nil => contradiction| cons p u =>cases v with| nil => contradiction| cons q v =>cases p <;> cases q <;> simp [canonical] at hu hvapply value_injective_fixed ?_ heqsimp only [List.length_cons, Nat.add_right_cancel_iff]by_cases hl : u.length < v.length· have := longer_larger u v hlomegaby_cases hr : v.length < u.length· have := longer_larger v u hromegaomegatheorem successor_unique {x y z : Word} (h : Step x y) (h' : Step x z) : y = z :=canonical_unique h.2.1 h'.2.1 (h.2.2.trans h'.2.2.symm)namespace Orbitstructure Digits wherescale : Intdirect : Intreflected : Intderiving Repr, DecidableEqdef sig (w : Word) : Digits :=⟨2 ^ w.length, value w, value w.reverse⟩def cat (a b : Digits) : Digits :=⟨a.scale * b.scale, a.direct * b.scale + b.direct,b.reflected * a.scale + a.reflected⟩theorem sig_append (u v : Word) : sig (u ++ v) = cat (sig u) (sig v) := bysimp [sig, cat, List.reverse_append, value_append, Int.natCast_pow, Int.natCast_add,Int.natCast_mul, Int.pow_add]def copies (w : Word) : Nat → Word| 0 => []| k+1 => w ++ copies w kdef geom0 (q : Int) : Nat → Int| 0 => 0| k+1 => geom0 q k * q + 1theorem geom0_succ (q : Int) (k : Nat) : geom0 q (k+1) = geom0 q k * q + 1 := rfltheorem sig_copies (w : Word) (q a b : Int) (h : sig w = ⟨q,a,b⟩) (k : Nat) :sig (copies w k) = ⟨(q-1)*geom0 q k + 1, a*geom0 q k, b*geom0 q k⟩ := byinduction k with| zero => simp [copies, geom0, sig, value]| succ k ih =>rw [copies, sig_append, h, ih]rw [geom0_succ]unfold catcongr 1 <;> grind onlyprivate theorem sig_10 : sig [true, false] = ⟨4, 2, 1⟩ := rflprivate theorem sig_11 : sig [true, true] = ⟨4, 3, 3⟩ := rflprivate theorem sig_00 : sig [false, false] = ⟨4, 0, 0⟩ := rflprivate theorem sig_01 : sig [false, true] = ⟨4, 1, 2⟩ := rflprivate theorem sig_1010010 : sig [true, false, true, false, false, true, false] = ⟨128, 82, 37⟩ := rflprivate theorem sig_0100111 : sig [false, true, false, false, true, true, true] = ⟨128, 39, 114⟩ := rflprivate theorem sig_1101010 : sig [true, true, false, true, false, true, false] = ⟨128, 106, 43⟩ := rflprivate theorem sig_1000101 : sig [true, false, false, false, true, false, true] = ⟨128, 69, 81⟩ := rflprivate theorem sig_101 : sig [true, false, true] = ⟨8, 5, 5⟩ := rflprivate theorem sig_010 : sig [false, true, false] = ⟨8, 2, 2⟩ := rflprivate theorem sig_1101 : sig [true, true, false, true] = ⟨16, 13, 11⟩ := rflprivate theorem sig_1000 : sig [true, false, false, false] = ⟨16, 8, 1⟩ := rflprivate theorem sig_1 : sig [true] = ⟨2, 1, 1⟩ := rflprivate theorem sig_0 : sig [false] = ⟨2, 0, 0⟩ := rfldef a (m j : Nat) : Word :=[true, false] ++ copies [true] j ++ copies [true, false, true, false, false, true, false] m ++[true, false, true] ++ copies [false] j ++ [false, false]def b (m j : Nat) : Word :=[true, true] ++ copies [false] j ++ copies [false, true, false, false, true, true, true] m ++[false, true, false] ++ copies [true] j ++ [false, true]def c (m j : Nat) : Word :=[true, false] ++ copies [true] j ++ copies [true, true, false, true, false, true, false] m ++[true, true, false, true] ++ copies [false] j ++ [false, false]def d (m j : Nat) : Word :=[true, true] ++ copies [false] j ++ copies [true, false, false, false, true, false, true] m ++[true, false, false, false] ++ copies [true] j ++ [false, true]private theorem of_sig {u v : Word} (hu : canonical u) (hv : canonical v)(h : (sig v).direct = (sig u).direct + (sig u).reflected) : Step u v := byrefine ⟨hu, hv, ?_⟩have h' : (value v : Int) = (value u : Int) + (value u.reverse : Int) := homegamacro "orbit_calc" : tactic => `(tactic|(apply of_sig· rfl· rflsimp only [a, b, c, d, sig_append,sig_copies _ _ _ _ sig_1, sig_copies _ _ _ _ sig_0,sig_copies _ _ _ _ sig_1010010, sig_copies _ _ _ _ sig_0100111,sig_copies _ _ _ _ sig_1101010, sig_copies _ _ _ _ sig_1000101,sig_10, sig_11, sig_00, sig_01, sig_101, sig_010, sig_1101, sig_1000, cat]try simp only [geom0_succ]grind only))theorem four_phases (m j : Nat) :Step (a m j) (b m j) ∧ Step (b m j) (c m j) ∧Step (c m j) (d m j) ∧ Step (d m j) (a m (j+1)) := byrefine ⟨?_, ?_, ?_, ?_⟩ <;> orbit_calctheorem gramTail_letters (m : Nat) :gramTail m = copies [true, false, true, false, false, true, false] m ++ [true, false, true] := byinduction m with| zero => rfl| succ m ih => simp [gramTail, copies, ih]theorem starting_word (m : Nat) : candidate m = a m 0 := bysimp [candidate, a, copies, gramTail_letters, List.append_assoc]#print axioms four_phases#print axioms starting_word/-- Count changes of bit, including a specified bit immediately to the left. -/def scan (previous : Bool) : Word → Nat × Bool| [] => (0, previous)| digit :: rest =>let r := scan digit rest((if previous = digit then 0 else 1) + r.1, r.2)theorem scan_append (p : Bool) (u v : Word) :scan p (u ++ v) =let s := scan p ulet t := scan s.2 v(s.1 + t.1, t.2) := byinduction u generalizing p with| nil => simp [scan]| cons h u ih => simp [scan, ih, Nat.add_assoc]theorem scan_copies (p : Bool) (w : Word) (weight : Nat)(h : scan p w = (weight,p)) (k : Nat) :scan p (copies w k) = (weight*k,p) := byinduction k with| zero => simp [copies, scan]| succ k ih => simp [copies, scan_append, h, ih, Nat.mul_succ, Nat.add_comm]private theorem scan_ones (k : Nat) :scan true (copies [true] k) = (0,true) := bysimpa using scan_copies true [true] 0 rfl kprivate theorem scan_zeros (k : Nat) :scan false (copies [false] k) = (0,false) := bysimpa using scan_copies false [false] 0 rfl kprivate theorem scan_ones_succ (k : Nat) :scan false (copies [true] (k+1)) = (1,true) := bysimp [copies, scan, scan_ones]private theorem scan_zeros_succ (k : Nat) :scan true (copies [false] (k+1)) = (1,false) := bysimp [copies, scan, scan_zeros]private theorem scan_core0 (k : Nat) :scan false (copies [true, false, true, false, false, true, false] k) = (6*k,false) :=scan_copies false _ 6 rfl kprivate theorem scan_core0_succ (k : Nat) :scan true (copies [true, false, true, false, false, true, false] (k+1)) = (5+6*k,false) := byrw [copies, scan_append]change (5+(scan false (copies _ k)).1,(scan false (copies _ k)).2) = _rw [scan_core0]private theorem scan_core1 (k : Nat) :scan false (copies [true, true, false, true, false, true, false] k) = (6*k,false) :=scan_copies false _ 6 rfl kprivate theorem scan_core1_succ (k : Nat) :scan true (copies [true, true, false, true, false, true, false] (k+1)) = (5+6*k,false) := byrw [copies, scan_append]change (5+(scan false (copies _ k)).1,(scan false (copies _ k)).2) = _rw [scan_core1]theorem even_phase_changes (m j : Nat) :(scan true (a m j)).1 = 6*m+5 ∧ (scan true (c m j)).1 = 6*m+5 := bycases j <;> cases m <;>simp only [a, c, scan_append, scan_ones, scan_zeros,scan_core0, scan_core1, copies, scan,List.nil_append, List.cons_append, Nat.zero_add,Nat.add_zero, Nat.mul_succ, Nat.mul_zero, Bool.false_eq_true, Bool.true_eq_false,if_false, if_true, and_self] <;> omegatheorem different_even_words {m n j k : Nat} (h : m ≠ n) :a m j ≠ a n k ∧ a m j ≠ c n k ∧ c m j ≠ a n k ∧ c m j ≠ c n k := byobtain ⟨ha, hc⟩ := even_phase_changes m jobtain ⟨hb, hd⟩ := even_phase_changes n kconstructor· intro e; rw [e, hb] at ha; omegaconstructor· intro e; rw [e, hd] at ha; omegaconstructor· intro e; rw [e, hb] at hc; omega· intro e; rw [e, hd] at hc; omegatheorem all_nonpalindromic (m j : Nat) :(a m j).reverse ≠ a m j ∧ (b m j).reverse ≠ b m j ∧(c m j).reverse ≠ c m j ∧ (d m j).reverse ≠ d m j := byrefine ⟨?_, ?_, ?_, ?_⟩ <;> intro e <;>have h := congrArg (List.take 2) e <;>simp [a, b, c, d, List.reverse_append] at h#print axioms all_nonpalindromic#print axioms different_even_wordsdef EvenPhase (m : Nat) (w : Word) : Prop := ∃ j, w = a m j ∨ w = c m jdef OddPhase (m : Nat) (w : Word) : Prop := ∃ j, w = b m j ∨ w = d m jdef OnTrack (m : Nat) (w : Word) : Prop := EvenPhase m w ∨ OddPhase m wprivate theorem even_last {m w} (h : EvenPhase m w) : w.getLast? = some false := byobtain ⟨j, h | h⟩ := h <;> subst w <;>rw [← List.head?_reverse] <;> simp [a, c, List.reverse_append]private theorem odd_last {m w} (h : OddPhase m w) : w.getLast? = some true := byobtain ⟨j, h | h⟩ := h <;> subst w <;>rw [← List.head?_reverse] <;> simp [b, d, List.reverse_append]private theorem even_disjoint {m n w} (hn : m ≠ n)(hm : EvenPhase m w) (hn' : EvenPhase n w) : False := byobtain ⟨j, h⟩ := hmobtain ⟨k, h'⟩ := hn'obtain ⟨haa, hac, hca, hcc⟩ := different_even_words (j := j) (k := k) hnrcases h with h | h <;> rcases h' with h' | h' <;> grind onlyprivate theorem odd_next {m w} (h : OddPhase m w) :∃ v, Step w v ∧ EvenPhase m v := byobtain ⟨j, h | h⟩ := h <;> subst w· exact ⟨c m j, (four_phases m j).2.1, j, Or.inr rfl⟩· exact ⟨a m (j+1), (four_phases m j).2.2.2, j+1, Or.inl rfl⟩theorem tracks_disjoint {m n w} (h : m ≠ n) (hm : OnTrack m w) (hn : OnTrack n w) : False := byrcases hm with hm | hm <;> rcases hn with hn | hn· exact even_disjoint h hm hn· have := even_last hmhave := odd_last hngrind only· have := odd_last hmhave := even_last hngrind only· obtain ⟨x, sx, hx⟩ := odd_next hmobtain ⟨y, sy, hy⟩ := odd_next hnhave e := successor_unique sx sysubst yexact even_disjoint h hx hyprivate theorem step_stays {m u v} (h : OnTrack m u) (s : Step u v) : OnTrack m v := byrcases h with h | h· obtain ⟨j, h | h⟩ := h <;> subst u· exact Or.inr ⟨j, Or.inl (successor_unique s (four_phases m j).1)⟩· exact Or.inr ⟨j, Or.inr (successor_unique s (four_phases m j).2.2.1)⟩· obtain ⟨z, sz, hz⟩ := odd_next hhave e := successor_unique s szsubst vexact Or.inl hzprivate theorem walk_stays {m u v} (h : OnTrack m u) (t : Walk u v) : OnTrack m v := byinduction t with| single e => exact step_stays h e| next t e ih => exact step_stays ih edef InOrbit (m : Nat) (w : Word) : Prop := w = candidate m ∨ Walk (candidate m) wtheorem orbit_on_track {m w} (h : InOrbit m w) : OnTrack m w := byhave start : OnTrack m (candidate m) := Or.inl ⟨0, Or.inl (starting_word m)⟩rcases h with rfl | h· exact start· exact walk_stays start htheorem disjoint_orbits {m n w} (h : m ≠ n) (hm : InOrbit m w) (hn : InOrbit n w) : False :=tracks_disjoint h (orbit_on_track hm) (orbit_on_track hn)theorem no_orbit_word_palindromic {m w} (h : InOrbit m w) : w.reverse ≠ w := byrcases orbit_on_track h with ⟨j, e | e⟩ | ⟨j, e | e⟩ <;> subst w· exact (all_nonpalindromic m j).1· exact (all_nonpalindromic m j).2.2.1· exact (all_nonpalindromic m j).2.1· exact (all_nonpalindromic m j).2.2.2#print axioms disjoint_orbits#print axioms no_orbit_word_palindromicend Orbitend A306517