| @@ -0,0 +1,169 @@ |
| 1 | +import Inverse |
| 2 | + |
| 3 | +namespace CollatzRoute |
| 4 | + |
| 5 | +/-- Being able to reach 1 is invariant across a raw edge, in either direction. -/ |
| 6 | +theorem reaches_step_iff (n : Nat) : ReachesOne n ↔ ReachesOne (step n) := by |
| 7 | + constructor |
| 8 | + · rintro ⟨t, ht⟩ |
| 9 | + cases t with |
| 10 | + | zero => |
| 11 | + change n = 1 at ht |
| 12 | + subst n |
| 13 | + exact ⟨2, by decide⟩ |
| 14 | + | succ t => exact ⟨t, ht⟩ |
| 15 | + · rintro ⟨t, ht⟩ |
| 16 | + exact ⟨t + 1, ht⟩ |
| 17 | + |
| 18 | +structure Line where |
| 19 | + slope : Nat |
| 20 | + base : Nat |
| 21 | + deriving DecidableEq, Repr |
| 22 | + |
| 23 | +def Line.value (x : Line) (q : Nat) : Nat := x.slope * q + x.base |
| 24 | +def Line.twice (x : Line) : Line := ⟨2*x.slope, 2*x.base⟩ |
| 25 | +def Line.threeAddOne (x : Line) : Line := ⟨3*x.slope, 3*x.base+1⟩ |
| 26 | +def Line.allOdd (x : Line) : Bool := x.slope % 2 == 0 && x.base % 2 == 1 |
| 27 | + |
| 28 | +theorem Line.value_twice (x : Line) (q : Nat) : x.twice.value q = 2*x.value q := by |
| 29 | + simp [Line.twice, Line.value, Nat.mul_add, Nat.mul_assoc] |
| 30 | + |
| 31 | +theorem Line.value_threeAddOne (x : Line) (q : Nat) : |
| 32 | + x.threeAddOne.value q = 3*x.value q + 1 := by |
| 33 | + simp [Line.threeAddOne, Line.value, Nat.mul_add, Nat.mul_assoc, Nat.add_assoc] |
| 34 | + |
| 35 | +theorem Line.odd_of_allOdd {x : Line} (h : x.allOdd = true) (q : Nat) : |
| 36 | + x.value q % 2 = 1 := by |
| 37 | + simp [Line.allOdd] at h |
| 38 | + simp [Line.value, Nat.add_mod, Nat.mul_mod, h.1, h.2] |
| 39 | + |
| 40 | +/-- H and U follow the usual even/odd rule. D and P walk those edges backwards. -/ |
| 41 | +inductive Move where |
| 42 | + | H | U | D | P |
| 43 | + deriving DecidableEq, Repr |
| 44 | + |
| 45 | +def move? (op : Move) (x : Line) : Option Line := |
| 46 | + match op with |
| 47 | + | .H => |
| 48 | + let y : Line := ⟨x.slope/2, x.base/2⟩ |
| 49 | + if y.twice = x then some y else none |
| 50 | + | .U => if x.allOdd then some x.threeAddOne else none |
| 51 | + | .D => some x.twice |
| 52 | + | .P => |
| 53 | + let y : Line := ⟨x.slope/3, (x.base-1)/3⟩ |
| 54 | + if y.threeAddOne = x ∧ y.allOdd = true then some y else none |
| 55 | + |
| 56 | +theorem down_edge (x : Line) (q : Nat) : |
| 57 | + ReachesOne (x.twice.value q) ↔ ReachesOne (x.value q) := by |
| 58 | + simpa only [Line.value_twice, step_double] using reaches_step_iff (2*x.value q) |
| 59 | + |
| 60 | +theorem up_edge (x : Line) (odd : x.allOdd = true) (q : Nat) : |
| 61 | + ReachesOne (x.value q) ↔ ReachesOne (x.threeAddOne.value q) := by |
| 62 | + simpa only [Line.value_threeAddOne, step_odd (x.odd_of_allOdd odd q)] using reaches_step_iff (x.value q) |
| 63 | + |
| 64 | +theorem move_sound {op : Move} {x y : Line} (h : move? op x = some y) (q : Nat) : |
| 65 | + ReachesOne (x.value q) ↔ ReachesOne (y.value q) := by |
| 66 | + cases op with |
| 67 | + | H => |
| 68 | + simp only [move?] at h |
| 69 | + split at h |
| 70 | + next hx => |
| 71 | + cases h |
| 72 | + have eq := down_edge ⟨x.slope/2, x.base/2⟩ q |
| 73 | + rw [hx] at eq |
| 74 | + exact eq |
| 75 | + next => contradiction |
| 76 | + | U => |
| 77 | + simp only [move?] at h |
| 78 | + split at h |
| 79 | + next hx => cases h; exact up_edge _ hx _ |
| 80 | + next => contradiction |
| 81 | + | D => cases h; exact (down_edge _ _).symm |
| 82 | + | P => |
| 83 | + simp only [move?] at h |
| 84 | + split at h |
| 85 | + next hx => |
| 86 | + cases h |
| 87 | + have eq := up_edge ⟨x.slope/3, (x.base-1)/3⟩ hx.2 q |
| 88 | + rw [hx.1] at eq |
| 89 | + exact eq.symm |
| 90 | + next => contradiction |
| 91 | + |
| 92 | +def walk? : List Move → Line → Option Line |
| 93 | + | [], x => some x |
| 94 | + | op :: rest, x => (move? op x).bind (walk? rest) |
| 95 | + |
| 96 | +theorem walk_sound {w : List Move} {x y : Line} (h : walk? w x = some y) (q : Nat) : |
| 97 | + ReachesOne (x.value q) ↔ ReachesOne (y.value q) := by |
| 98 | + induction w generalizing x with |
| 99 | + | nil => cases h; exact Iff.rfl |
| 100 | + | cons op rest ih => |
| 101 | + cases hm : move? op x with |
| 102 | + | none => simp [walk?, hm] at h |
| 103 | + | some z => |
| 104 | + simp [walk?, hm] at h |
| 105 | + exact (move_sound hm q).trans (ih h) |
| 106 | + |
| 107 | +def certifies (start : Line) (word : List Move) : Bool := |
| 108 | + match walk? word start with |
| 109 | + | none => false |
| 110 | + | some y => decide (y.slope ≤ start.slope ∧ 0 < y.base ∧ y.base < start.base) |
| 111 | + |
| 112 | +theorem certificate_descends {x : Line} {w : List Move} (h : certifies x w = true) (q : Nat) : |
| 113 | + ∃ m, 0 < m ∧ m < x.value q ∧ (ReachesOne (x.value q) ↔ ReachesOne m) := by |
| 114 | + cases he : walk? w x with |
| 115 | + | none => simp [certifies, he] at h |
| 116 | + | some y => |
| 117 | + simp [certifies, he] at h |
| 118 | + refine ⟨y.value q, ?_, ?_, walk_sound he q⟩ |
| 119 | + · dsimp [Line.value]; omega |
| 120 | + · exact Nat.add_lt_add_of_le_of_lt (Nat.mul_le_mul_right q h.1) h.2.2 |
| 121 | + |
| 122 | +def covered (words : List (List Move)) (modulus base : Nat) : Bool := |
| 123 | + words.any (certifies ⟨modulus, base⟩) |
| 124 | + |
| 125 | +/-- Start each progression at its smallest value at least two, and list actual residues. -/ |
| 126 | +def remaining (words : List (List Move)) (modulus : Nat) : List Nat := |
| 127 | + (((List.range modulus).filter fun i => !(covered words modulus (i+2))).map |
| 128 | + fun i => (i+2) % modulus) |
| 129 | + |
| 130 | +theorem claim_iff_remaining (words : List (List Move)) (modulus : Nat) (hm : 0 < modulus) : |
| 131 | + Claim ↔ ∀ n : Nat, 1 < n → n % modulus ∈ remaining words modulus → ReachesOne n := by |
| 132 | + constructor |
| 133 | + · intro goal n hn _ |
| 134 | + exact goal n (by omega) |
| 135 | + · intro rest n hn |
| 136 | + induction n using Nat.strongRecOn with |
| 137 | + | ind n ih => |
| 138 | + by_cases one : n = 1 |
| 139 | + · subst n; exact ⟨0, rfl⟩ |
| 140 | + · let i := (n-2)%modulus |
| 141 | + let q := (n-2)/modulus |
| 142 | + let x : Line := ⟨modulus, i+2⟩ |
| 143 | + have repr : x.value q = n := by |
| 144 | + have h := Nat.mod_add_div (n-2) modulus |
| 145 | + dsimp [Line.value, x, i, q] |
| 146 | + omega |
| 147 | + cases hc : covered words modulus (i+2) with |
| 148 | + | true => |
| 149 | + obtain ⟨w, _, hw⟩ := List.any_eq_true.mp hc |
| 150 | + obtain ⟨m, hp, hlt, iff⟩ := certificate_descends hw q |
| 151 | + change m < x.value q at hlt |
| 152 | + change ReachesOne (x.value q) ↔ ReachesOne m at iff |
| 153 | + rw [repr] at iff hlt |
| 154 | + exact iff.mpr (ih m hlt hp) |
| 155 | + | false => |
| 156 | + apply rest n (by omega) |
| 157 | + apply List.mem_map.mpr |
| 158 | + refine ⟨i, List.mem_filter.mpr ⟨List.mem_range.mpr (Nat.mod_lt _ hm), by simp [hc]⟩, ?_⟩ |
| 159 | + dsimp [i] |
| 160 | + rw [Nat.mod_add_mod] |
| 161 | + congr 1 |
| 162 | + omega |
| 163 | + |
| 164 | +#print axioms reaches_step_iff |
| 165 | +#print axioms walk_sound |
| 166 | +#print axioms certificate_descends |
| 167 | +#print axioms claim_iff_remaining |
| 168 | + |
| 169 | +end CollatzRoute |