| @@ -0,0 +1,235 @@ |
| 1 | +import Problems.PVersusNP.Millennium |
| 2 | +import Mathlib.Tactic.DeriveFintype |
| 3 | + |
| 4 | +/-! |
| 5 | +# A linear-time palindrome decider |
| 6 | + |
| 7 | +Copy the input into two reversed work stacks. Move the scratch copy back onto |
| 8 | +the input stack, then drain the input and the retained reversed copy in parallel. |
| 9 | +The output alphabet is Boolean, independently of the finite input alphabet. |
| 10 | +-/ |
| 11 | + |
| 12 | +namespace Millennium.Examples.Palindrome |
| 13 | + |
| 14 | +noncomputable section |
| 15 | + |
| 16 | +open Turing Computability StateTransition |
| 17 | + |
| 18 | +inductive Stack where |
| 19 | + | input | scratch | backwards | output |
| 20 | + deriving DecidableEq, Fintype |
| 21 | + |
| 22 | +inductive Phase where |
| 23 | + | copy | restore | compare |
| 24 | + deriving DecidableEq, Fintype |
| 25 | + |
| 26 | +abbrev Alphabet (α : Type) : Stack → Type |
| 27 | + | .input => α |
| 28 | + | .scratch | .backwards => Option α |
| 29 | + | .output => Bool |
| 30 | + |
| 31 | +abbrev Register (α : Type) := Option α × Bool |
| 32 | + |
| 33 | +/-- In the successful restore branch a symbol exists. Keeping the fallback |
| 34 | +inside this instruction lets the machine work for the empty alphabet too. -/ |
| 35 | +private noncomputable def putInput (α : Type) : |
| 36 | + TM2.Stmt (Alphabet α) Phase (Register α) := by |
| 37 | + classical |
| 38 | + exact if h : Nonempty α then |
| 39 | + .push .input (fun v => v.1.getD (Classical.choice h)) (.goto fun _ => .restore) |
| 40 | + else .halt |
| 41 | + |
| 42 | +noncomputable def machine (α : Type) [Fintype α] [DecidableEq α] : FinTM2 where |
| 43 | + K := Stack |
| 44 | + k₀ := .input |
| 45 | + k₁ := .output |
| 46 | + Γ := Alphabet α |
| 47 | + Λ := Phase |
| 48 | + main := .copy |
| 49 | + σ := Register α |
| 50 | + initialState := (none, true) |
| 51 | + Γk₀Fin := inferInstanceAs (Fintype α) |
| 52 | + m |
| 53 | + | .copy => |
| 54 | + .pop .input (fun v x => (x, v.2)) |
| 55 | + (.branch (fun v => v.1.isSome) |
| 56 | + (.push .scratch (fun v => v.1) |
| 57 | + (.push .backwards (fun v => v.1) (.goto fun _ => .copy))) |
| 58 | + (.goto fun _ => .restore)) |
| 59 | + | .restore => |
| 60 | + .pop .scratch (fun v x => (x.join, v.2)) |
| 61 | + (.branch (fun v => v.1.isSome) (putInput α) (.goto fun _ => .compare)) |
| 62 | + | .compare => |
| 63 | + .pop .input (fun v x => (x, v.2)) |
| 64 | + (.branch (fun v => v.1.isSome) |
| 65 | + (.pop .backwards (fun v x => (none, v.2 && decide (v.1 = x.join))) |
| 66 | + (.goto fun _ => .compare)) |
| 67 | + (.push .output (fun v => v.2) |
| 68 | + (.load (fun _ => (none, true)) .halt))) |
| 69 | + |
| 70 | +variable {α : Type} [Fintype α] [DecidableEq α] |
| 71 | + |
| 72 | +/-- Reachable configurations store only real symbols on the work stacks. -/ |
| 73 | +def cfg (l : Option Phase) (v : Register α) (inp scratch backwards : List α) |
| 74 | + (out : List Bool) : (machine α).Cfg where |
| 75 | + l := l |
| 76 | + var := v |
| 77 | + stk |
| 78 | + | .input => inp |
| 79 | + | .scratch => scratch.map some |
| 80 | + | .backwards => backwards.map some |
| 81 | + | .output => out |
| 82 | + |
| 83 | +/-- A running configuration at one of the three loop labels. -/ |
| 84 | +abbrev atPhase (phase : Phase) (v : Register α) (inp s r : List α) (out : List Bool) := |
| 85 | + cfg (some phase) v inp s r out |
| 86 | + |
| 87 | +macro "pal_step" : tactic => |
| 88 | + `(tactic| (simp [atPhase, FinTM2.step, TM2.step, TM2.stepAux, machine, cfg, putInput, *] |
| 89 | + try (congr <;> first | rfl | (funext k; cases k <;> rfl) | (simp; done)))) |
| 90 | + |
| 91 | +theorem copy_cons (v : Register α) (a : α) (inp s r : List α) (out : List Bool) : |
| 92 | + (machine α).step (atPhase .copy v (a :: inp) s r out) = |
| 93 | + some (atPhase .copy (some a, v.2) inp (a :: s) (a :: r) out) := by |
| 94 | + pal_step |
| 95 | + |
| 96 | +theorem copy_nil (v : Register α) (s r : List α) (out : List Bool) : |
| 97 | + (machine α).step (atPhase .copy v [] s r out) = |
| 98 | + some (atPhase .restore (none, v.2) [] s r out) := by |
| 99 | + pal_step |
| 100 | + |
| 101 | +theorem restore_cons (v : Register α) (a : α) (inp s r : List α) (out : List Bool) : |
| 102 | + (machine α).step (atPhase .restore v inp (a :: s) r out) = |
| 103 | + some (atPhase .restore (some a, v.2) (a :: inp) s r out) := by |
| 104 | + have hex : Nonempty α := ⟨a⟩ |
| 105 | + pal_step |
| 106 | + |
| 107 | +theorem restore_nil (v : Register α) (inp r : List α) (out : List Bool) : |
| 108 | + (machine α).step (atPhase .restore v inp [] r out) = |
| 109 | + some (atPhase .compare (none, v.2) inp [] r out) := by |
| 110 | + pal_step |
| 111 | + |
| 112 | +theorem compare_cons (v : Register α) (a b : α) (xs ys : List α) (out : List Bool) : |
| 113 | + (machine α).step (atPhase .compare v (a :: xs) [] (b :: ys) out) = |
| 114 | + some (atPhase .compare (none, v.2 && decide (a = b)) xs [] ys out) := by |
| 115 | + pal_step |
| 116 | + |
| 117 | +theorem compare_nil (v : Register α) (out : List Bool) : |
| 118 | + (machine α).step (atPhase .compare v [] [] [] out) = |
| 119 | + some (cfg none (none, true) ([] : List α) [] [] (v.2 :: out)) := by |
| 120 | + pal_step |
| 121 | + |
| 122 | +/-- A single transition as a timed run. -/ |
| 123 | +private def oneStep {s t : (machine α).Cfg} (h : (machine α).step s = some t) : |
| 124 | + EvalsToInTime (machine α).step s (some t) 1 := |
| 125 | + { steps := 1 |
| 126 | + evals_in_steps := by simpa [flip, Option.bind] using h |
| 127 | + steps_le_m := le_rfl } |
| 128 | + |
| 129 | +private def follow {s t u : (machine α).Cfg} {m n : ℕ} |
| 130 | + (a : EvalsToInTime (machine α).step s (some t) m) |
| 131 | + (b : EvalsToInTime (machine α).step t (some u) n) : |
| 132 | + EvalsToInTime (machine α).step s (some u) (m + n) := |
| 133 | + (Nat.add_comm n m) ▸ EvalsToInTime.trans _ m n _ _ _ a b |
| 134 | + |
| 135 | +/-- After consuming the input, each work stack has acquired its reverse. -/ |
| 136 | +def copyRun (inp : List α) (v : Register α) (s r : List α) (out : List Bool) : |
| 137 | + EvalsToInTime (machine α).step (atPhase .copy v inp s r out) |
| 138 | + (some (atPhase .restore (none, v.2) [] (inp.reverse ++ s) (inp.reverse ++ r) out)) |
| 139 | + (inp.length + 1) := by |
| 140 | + induction inp generalizing v s r with |
| 141 | + | nil => simpa using oneStep (copy_nil v s r out) |
| 142 | + | cons a xs ih => |
| 143 | + have h := follow (oneStep (copy_cons v a xs s r out)) |
| 144 | + (ih (some a, v.2) (a :: s) (a :: r)) |
| 145 | + simpa [List.reverse_cons, List.append_assoc, Nat.add_comm 1] using h |
| 146 | + |
| 147 | +/-- Drain the scratch stack back onto the input, preserving the reversed copy. -/ |
| 148 | +def restoreRun (s : List α) (v : Register α) (inp r : List α) (out : List Bool) : |
| 149 | + EvalsToInTime (machine α).step (atPhase .restore v inp s r out) |
| 150 | + (some (atPhase .compare (none, v.2) (s.reverse ++ inp) [] r out)) |
| 151 | + (s.length + 1) := by |
| 152 | + induction s generalizing v inp with |
| 153 | + | nil => simpa using oneStep (restore_nil v inp r out) |
| 154 | + | cons a xs ih => |
| 155 | + have h := follow (oneStep (restore_cons v a inp xs r out)) |
| 156 | + (ih (some a, v.2) (a :: inp)) |
| 157 | + simpa [List.reverse_cons, List.append_assoc, Nat.add_comm 1] using h |
| 158 | + |
| 159 | +/-- Drain equally long stacks together. An earlier mismatch remains recorded. -/ |
| 160 | +def compareRun (xs ys : List α) (same : xs.length = ys.length) |
| 161 | + (v : Register α) (out : List Bool) : |
| 162 | + EvalsToInTime (machine α).step (atPhase .compare v xs [] ys out) |
| 163 | + (some (cfg none (none, true) ([] : List α) [] [] |
| 164 | + ((v.2 && decide (xs = ys)) :: out))) (xs.length + 1) := by |
| 165 | + induction xs generalizing ys v with |
| 166 | + | nil => |
| 167 | + cases ys with |
| 168 | + | nil => simpa using oneStep (compare_nil v out) |
| 169 | + | cons b ys => simp at same |
| 170 | + | cons a xs ih => |
| 171 | + cases ys with |
| 172 | + | nil => simp at same |
| 173 | + | cons b ys => |
| 174 | + have agree : ((v.2 && decide (a = b)) && decide (xs = ys)) = |
| 175 | + (v.2 && decide (a :: xs = b :: ys)) := by |
| 176 | + by_cases hab : a = b <;> simp [hab] |
| 177 | + have h := follow (oneStep (compare_cons v a b xs ys out)) |
| 178 | + (ih ys (by simpa using same) (none, v.2 && decide (a = b))) |
| 179 | + simpa only [agree, List.length_cons, Nat.add_comm 1] using h |
| 180 | + |
| 181 | +theorem init_eq (inp : List α) : |
| 182 | + initList (machine α) inp = atPhase .copy (none, true) inp [] [] [] := by |
| 183 | + unfold atPhase initList cfg |
| 184 | + congr |
| 185 | + funext k |
| 186 | + cases k <;> rfl |
| 187 | + |
| 188 | +theorem halt_eq (out : List Bool) : |
| 189 | + haltList (machine α) out = cfg none (none, true) ([] : List α) [] [] out := by |
| 190 | + unfold haltList cfg |
| 191 | + congr |
| 192 | + funext k |
| 193 | + cases k <;> rfl |
| 194 | + |
| 195 | +/-- Each phase takes at most one step per symbol and one final transition. -/ |
| 196 | +def run (inp : List α) : |
| 197 | + EvalsToInTime (machine α).step (initList (machine α) inp) |
| 198 | + (some (haltList (machine α) [decide (inp = inp.reverse)])) |
| 199 | + (3 * inp.length + 3) := by |
| 200 | + have hc := copyRun inp (none, true) [] [] [] |
| 201 | + have hr := restoreRun inp.reverse (none, true) [] inp.reverse [] |
| 202 | + have he := compareRun inp inp.reverse (by simp) (none, true) [] |
| 203 | + simp only [List.append_nil, List.reverse_reverse, List.length_reverse, Bool.true_and] at hc hr he |
| 204 | + rw [init_eq, halt_eq] |
| 205 | + have h := follow (follow hc hr) he |
| 206 | + rw [show 3 * inp.length + 3 = |
| 207 | + (inp.length + 1) + (inp.length + 1) + (inp.length + 1) by omega] |
| 208 | + convert h using 1 |
| 209 | + congr 5 |
| 210 | + |
| 211 | +/-- The polynomial-time witness for comparison with the reversed input. -/ |
| 212 | +noncomputable def decider (α : Type) [Fintype α] [DecidableEq α] : |
| 213 | + TM2ComputableInPolyTime (fin_encoding_string α).encode finEncodingBoolBool.encode |
| 214 | + (fun inp => decide (inp = inp.reverse)) where |
| 215 | + tm := machine α |
| 216 | + inputAlphabet := Equiv.refl _ |
| 217 | + outputAlphabet := Equiv.refl _ |
| 218 | + time := 3 * Polynomial.X + 3 |
| 219 | + outputsFun inp := by |
| 220 | + show EvalsToInTime (machine α).step (initList (machine α) (List.map id inp)) |
| 221 | + (some (haltList (machine α) (List.map id [decide (inp = inp.reverse)]))) |
| 222 | + ((3 * Polynomial.X + 3 : Polynomial ℕ).eval inp.length) |
| 223 | + simpa using run inp |
| 224 | + |
| 225 | +/-- Over every finite alphabet, the language of words equal to their reversal is in P. -/ |
| 226 | +theorem palindromes_in_P (α : Type) [Fintype α] : |
| 227 | + InPolynomialTime (fin_encoding_string α) (fun inp => inp = inp.reverse) := by |
| 228 | + classical |
| 229 | + exact ⟨_, decider α, fun _ => by simp⟩ |
| 230 | + |
| 231 | +#print axioms palindromes_in_P |
| 232 | + |
| 233 | +end |
| 234 | + |
| 235 | +end Millennium.Examples.Palindrome |