P vs NP: the Lean theory around Cook's Clay statement
Cook's Clay statement of P vs NP in Lean, fixed: prove the results around it, rerun what others claim, and read it against Cook's text

| 1 | import Problems.PVersusNP.Millennium |
| 2 | import Problems.PVersusNP.ForMathlib.Computable |
| 3 | import Problems.PVersusNP.ForMathlib.StateTransition |
| 4 | import Mathlib.Tactic.DeriveFintype |
| 5 | |
| 6 | /-! |
| 7 | # A length guard for certificate checkers |
| 8 | |
| 9 | Scan the two blocks of the canonical input and save the word. Using unary stacks, |
| 10 | start at one and multiply by the left length once for each unit of the fixed |
| 11 | exponent. Compare the result with the right length. On acceptance, restore the |
| 12 | saved word and start the original verifier. On rejection, discard it and return |
| 13 | false. The guard needs only finitely many labels and does not assume closure of |
| 14 | polynomial time under composition. |
| 15 | -/ |
| 16 | |
| 17 | namespace Millennium.Machines.CertificateBound |
| 18 | |
| 19 | open Computability Turing StateTransition |
| 20 | |
| 21 | noncomputable section |
| 22 | |
| 23 | private inductive Aux |
| 24 | | saved | base | temp | count | next | cert |
| 25 | deriving DecidableEq, Fintype |
| 26 | |
| 27 | private inductive Phase (k : ℕ) |
| 28 | | scan (right : Bool) |
| 29 | | power (i : Fin (k + 1)) |
| 30 | | mul (i : Fin (k + 1)) |
| 31 | | copy (i : Fin (k + 1)) |
| 32 | | putBack (i : Fin (k + 1)) |
| 33 | | transfer (i : Fin (k + 1)) |
| 34 | | clearBase |
| 35 | | compare (ok : Bool) |
| 36 | | drain (ok : Bool) |
| 37 | | restore (ok : Bool) |
| 38 | deriving Fintype |
| 39 | |
| 40 | private abbrev Alphabet (tm : FinTM2) : tm.K ⊕ Aux → Type |
| 41 | | .inl j => tm.Γ j |
| 42 | | .inr .saved => tm.Γ tm.k₀ |
| 43 | | .inr _ => Unit |
| 44 | |
| 45 | private abbrev Register (tm : FinTM2) := Option (tm.Γ tm.k₀) × tm.σ |
| 46 | |
| 47 | private abbrev Stmt (tm : FinTM2) (k : ℕ) := |
| 48 | TM2.Stmt (Alphabet tm) (Phase k ⊕ tm.Λ) (Register tm) |
| 49 | |
| 50 | private def liftStmt {tm : FinTM2} {k : ℕ} : tm.Stmt → Stmt tm k |
| 51 | | .push j f q => .push (.inl j) (fun v => f v.2) (liftStmt q) |
| 52 | | .peek j f q => .peek (.inl j) (fun v x => (v.1, f v.2 x)) (liftStmt q) |
| 53 | | .pop j f q => .pop (.inl j) (fun v x => (v.1, f v.2 x)) (liftStmt q) |
| 54 | | .load f q => .load (fun v => (v.1, f v.2)) (liftStmt q) |
| 55 | | .branch f q r => .branch (fun v => f v.2) (liftStmt q) (liftStmt r) |
| 56 | | .goto f => .goto (fun v => .inr (f v.2)) |
| 57 | | .halt => .halt |
| 58 | |
| 59 | private def jump {tm : FinTM2} {k : ℕ} (label : Phase k ⊕ tm.Λ) : Stmt tm k := |
| 60 | .load (fun _ => (none, tm.initialState)) (.goto fun _ => label) |
| 61 | |
| 62 | private def remember {tm : FinTM2} (_ : Register tm) (x : Option (tm.Γ tm.k₀)) : |
| 63 | Register tm := (x, tm.initialState) |
| 64 | |
| 65 | private def mark {tm : FinTM2} (sep : tm.Γ tm.k₀) (_ : Register tm) (x : Option Unit) : |
| 66 | Register tm := (x.map (fun _ => sep), tm.initialState) |
| 67 | |
| 68 | private def prev {k : ℕ} (i : Fin (k + 1)) : Fin (k + 1) := ⟨i.val - 1, by omega⟩ |
| 69 | |
| 70 | private def guard (tm : FinTM2) (sep : tm.Γ tm.k₀) (reject : tm.Γ tm.k₁) |
| 71 | (k : ℕ) : Phase k → Stmt tm k := by |
| 72 | classical |
| 73 | exact fun |
| 74 | | .scan right => |
| 75 | .pop (.inl tm.k₀) remember |
| 76 | (.branch (fun v => v.1.isSome) |
| 77 | (.push (.inr .saved) (fun v => v.1.getD sep) |
| 78 | (if right then |
| 79 | .push (.inr .cert) (fun _ => ()) (jump (.inl (.scan true))) |
| 80 | else |
| 81 | .branch (fun v => decide (v.1 = some sep)) |
| 82 | (.push (.inr .count) (fun _ => ()) (jump (.inl (.scan true)))) |
| 83 | (.push (.inr .base) (fun _ => ()) (jump (.inl (.scan false)))))) |
| 84 | (jump (.inl (.power ⟨k, by omega⟩)))) |
| 85 | | .power i => |
| 86 | if i.val = 0 then jump (.inl .clearBase) else jump (.inl (.mul i)) |
| 87 | | .mul i => |
| 88 | .pop (.inr .count) (mark sep) |
| 89 | (.branch (fun v => v.1.isSome) (jump (.inl (.copy i))) (jump (.inl (.transfer i)))) |
| 90 | | .copy i => |
| 91 | .pop (.inr .base) (mark sep) |
| 92 | (.branch (fun v => v.1.isSome) |
| 93 | (.push (.inr .temp) (fun _ => ()) |
| 94 | (.push (.inr .next) (fun _ => ()) (jump (.inl (.copy i))))) |
| 95 | (jump (.inl (.putBack i)))) |
| 96 | | .putBack i => |
| 97 | .pop (.inr .temp) (mark sep) |
| 98 | (.branch (fun v => v.1.isSome) |
| 99 | (.push (.inr .base) (fun _ => ()) (jump (.inl (.putBack i)))) |
| 100 | (jump (.inl (.mul i)))) |
| 101 | | .transfer i => |
| 102 | .pop (.inr .next) (mark sep) |
| 103 | (.branch (fun v => v.1.isSome) |
| 104 | (.push (.inr .count) (fun _ => ()) (jump (.inl (.transfer i)))) |
| 105 | (jump (.inl (.power (prev i))))) |
| 106 | | .clearBase => |
| 107 | .pop (.inr .base) (mark sep) |
| 108 | (.branch (fun v => v.1.isSome) (jump (.inl .clearBase)) (jump (.inl (.compare true)))) |
| 109 | | .compare ok => |
| 110 | .pop (.inr .cert) (mark sep) |
| 111 | (.branch (fun v => v.1.isSome) |
| 112 | (.pop (.inr .count) (mark sep) |
| 113 | (.branch (fun v => v.1.isSome) |
| 114 | (jump (.inl (.compare ok))) (jump (.inl (.compare false))))) |
| 115 | (jump (.inl (.drain ok)))) |
| 116 | | .drain ok => |
| 117 | .pop (.inr .count) (mark sep) |
| 118 | (.branch (fun v => v.1.isSome) (jump (.inl (.drain ok))) (jump (.inl (.restore ok)))) |
| 119 | | .restore ok => |
| 120 | .pop (.inr .saved) remember |
| 121 | (.branch (fun v => v.1.isSome) |
| 122 | (if ok then |
| 123 | .push (.inl tm.k₀) (fun v => v.1.getD sep) (jump (.inl (.restore true))) |
| 124 | else jump (.inl (.restore false))) |
| 125 | (if ok then jump (.inr tm.main) |
| 126 | else .push (.inl tm.k₁) (fun _ => reject) |
| 127 | (.load (fun _ => (none, tm.initialState)) .halt))) |
| 128 | |
| 129 | private abbrev guarded (tm : FinTM2) (sep : tm.Γ tm.k₀) (reject : tm.Γ tm.k₁) |
| 130 | (k : ℕ) : FinTM2 where |
| 131 | K := tm.K ⊕ Aux |
| 132 | kFin := by letI := tm.kFin; infer_instance |
| 133 | k₀ := .inl tm.k₀ |
| 134 | k₁ := .inl tm.k₁ |
| 135 | Γ := Alphabet tm |
| 136 | Λ := Phase k ⊕ tm.Λ |
| 137 | ΛFin := by letI := tm.ΛFin; infer_instance |
| 138 | main := .inl (.scan false) |
| 139 | σ := Register tm |
| 140 | σFin := by letI := tm.Γk₀Fin; letI := tm.σFin; infer_instance |
| 141 | initialState := (none, tm.initialState) |
| 142 | Γk₀Fin := tm.Γk₀Fin |
| 143 | m := Sum.elim (guard tm sep reject k) (fun label => liftStmt (tm.m label)) |
| 144 | |
| 145 | private def liftStacks {tm : FinTM2} (st : ∀ j, List (tm.Γ j)) : |
| 146 | ∀ j, List (Alphabet tm j) |
| 147 | | .inl j => st j |
| 148 | | .inr _ => [] |
| 149 | |
| 150 | private def liftCfg {tm : FinTM2} {k : ℕ} (c : tm.Cfg) : |
| 151 | TM2.Cfg (Alphabet tm) (Phase k ⊕ tm.Λ) (Register tm) := |
| 152 | ⟨c.l.map Sum.inr, (none, c.var), liftStacks c.stk⟩ |
| 153 | |
| 154 | private theorem liftStacks_update {tm : FinTM2} (st : ∀ j, List (tm.Γ j)) |
| 155 | (j : tm.K) (xs : List (tm.Γ j)) : |
| 156 | Function.update (liftStacks st) (.inl j) xs = liftStacks (Function.update st j xs) := by |
| 157 | funext h |
| 158 | cases h with |
| 159 | | inl h => |
| 160 | by_cases eq : h = j |
| 161 | · subst h; simp [liftStacks] |
| 162 | · have different : (Sum.inl h : tm.K ⊕ Aux) ≠ .inl j := fun e => eq (Sum.inl.inj e) |
| 163 | simp [liftStacks, Function.update_of_ne different, Function.update_of_ne eq] |
| 164 | | inr h => simp [liftStacks, Function.update_of_ne] |
| 165 | |
| 166 | private theorem liftStmt_correct {tm : FinTM2} {k : ℕ} (p : tm.Stmt) |
| 167 | (v : tm.σ) (st : ∀ j, List (tm.Γ j)) : |
| 168 | TM2.stepAux (liftStmt (k := k) p) (none, v) (liftStacks st) = |
| 169 | liftCfg (TM2.stepAux p v st) := by |
| 170 | induction p generalizing v st with |
| 171 | | push j f q ih => |
| 172 | change TM2.stepAux (liftStmt q) (none, v) |
| 173 | (Function.update (liftStacks st) (.inl j) (f v :: st j)) = _ |
| 174 | rw [liftStacks_update] |
| 175 | exact ih v _ |
| 176 | | pop j f q ih => |
| 177 | change TM2.stepAux (liftStmt q) (none, f v (st j).head?) |
| 178 | (Function.update (liftStacks st) (.inl j) (st j).tail) = _ |
| 179 | rw [liftStacks_update] |
| 180 | exact ih _ _ |
| 181 | | peek j f q ih => exact ih _ _ |
| 182 | | load f q ih => exact ih _ _ |
| 183 | | branch f q r ih ir => |
| 184 | cases hf : f v <;> simp only [liftStmt, TM2.stepAux, hf, cond_false, cond_true, ih, ir] |
| 185 | | goto f => rfl |
| 186 | | halt => rfl |
| 187 | |
| 188 | private theorem liftStep {tm : FinTM2} {sep : tm.Γ tm.k₀} {reject : tm.Γ tm.k₁} {k : ℕ} |
| 189 | (c : tm.Cfg) : |
| 190 | (guarded tm sep reject k).step (liftCfg c) = (tm.step c).map liftCfg := by |
| 191 | rcases c with ⟨l, v, st⟩ |
| 192 | cases l with |
| 193 | | none => rfl |
| 194 | | some label => exact congrArg some (liftStmt_correct (tm.m label) v st) |
| 195 | |
| 196 | private def liftRun {tm : FinTM2} {sep : tm.Γ tm.k₀} {reject : tm.Γ tm.k₁} {k : ℕ} |
| 197 | {a b : tm.Cfg} (run : EvalsTo tm.step a (some b)) : |
| 198 | EvalsTo (guarded tm sep reject k).step (liftCfg a) (some (liftCfg b)) := by |
| 199 | let send : Option tm.Cfg → Option (guarded tm sep reject k).Cfg := Option.map liftCfg |
| 200 | have commutes : Function.Semiconj send (flip bind tm.step) |
| 201 | (flip bind (guarded tm sep reject k).step) := by |
| 202 | intro o |
| 203 | cases o with |
| 204 | | none => rfl |
| 205 | | some c => exact (liftStep c).symm |
| 206 | refine ⟨run.steps, ?_⟩ |
| 207 | exact ((commutes.iterate_right run.steps) (some a)).symm.trans |
| 208 | (congrArg send run.evals_in_steps) |
| 209 | |
| 210 | private structure Work (A : Type) where |
| 211 | input : List A := [] |
| 212 | saved : List A := [] |
| 213 | base : ℕ := 0 |
| 214 | temp : ℕ := 0 |
| 215 | count : ℕ := 0 |
| 216 | next : ℕ := 0 |
| 217 | cert : ℕ := 0 |
| 218 | |
| 219 | private def checkpoint {tm : FinTM2} {k : ℕ} (p : Phase k) (w : Work (tm.Γ tm.k₀)) : |
| 220 | TM2.Cfg (Alphabet tm) (Phase k ⊕ tm.Λ) (Register tm) where |
| 221 | l := some (.inl p) |
| 222 | var := (none, tm.initialState) |
| 223 | stk |
| 224 | | .inl j => (initList tm w.input).stk j |
| 225 | | .inr .saved => w.saved |
| 226 | | .inr .base => List.replicate w.base () |
| 227 | | .inr .temp => List.replicate w.temp () |
| 228 | | .inr .count => List.replicate w.count () |
| 229 | | .inr .next => List.replicate w.next () |
| 230 | | .inr .cert => List.replicate w.cert () |
| 231 | |
| 232 | variable {tm : FinTM2} {sep : tm.Γ tm.k₀} {reject : tm.Γ tm.k₁} {k : ℕ} |
| 233 | |
| 234 | private abbrev Run (tm : FinTM2) (sep : tm.Γ tm.k₀) (reject : tm.Γ tm.k₁) (k : ℕ) |
| 235 | (a b : (guarded tm sep reject k).Cfg) := |
| 236 | EvalsTo (guarded tm sep reject k).step a (some b) |
| 237 | |
| 238 | private def beginStep {a b c : (guarded tm sep reject k).Cfg} |
| 239 | (step : (guarded tm sep reject k).step a = some b) |
| 240 | (rest : Run tm sep reject k b c) : Run tm sep reject k a c := |
| 241 | EvalsTo.trans _ _ _ _ (EvalsToInTime.of_step step).toEvalsTo rest |
| 242 | |
| 243 | private def oneStep {a b : (guarded tm sep reject k).Cfg} |
| 244 | (step : (guarded tm sep reject k).step a = some b) : Run tm sep reject k a b := |
| 245 | (EvalsToInTime.of_step step).toEvalsTo |
| 246 | |
| 247 | @[local simp] private theorem initEmpty (tm : FinTM2) (j : tm.K) : |
| 248 | (initList tm []).stk j = [] := by |
| 249 | by_cases hj : j = tm.k₀ |
| 250 | · subst j |
| 251 | exact initList_stk_self tm [] |
| 252 | · exact initList_stk_ne tm [] hj |
| 253 | |
| 254 | macro "certificate_step" : tactic => do |
| 255 | let machine := Lean.mkIdent `tm |
| 256 | `(tactic| |
| 257 | (simp [FinTM2.step, TM2.step, TM2.stepAux, guarded, guard, jump, mark, remember, |
| 258 | checkpoint, liftCfg, liftStacks, List.replicate_succ, *] <;> |
| 259 | (try apply congrArg some) <;> congr 1 <;> |
| 260 | (funext s |
| 261 | cases s with |
| 262 | | inl j => by_cases hj : j = ($machine).k₀ <;> by_cases ho : j = ($machine).k₁ <;> |
| 263 | (try subst j) <;> simp_all [initList, haltList, liftStacks, Function.update] |
| 264 | | inr j => cases j <;> simp_all [liftStacks, Function.update_of_ne]))) |
| 265 | |
| 266 | private def scanRight (ys saved : List (tm.Γ tm.k₀)) (base cert : ℕ) : |
| 267 | Run tm sep reject k |
| 268 | (checkpoint (.scan true) { input := ys, saved := saved, base := base, count := 1, cert := cert }) |
| 269 | (checkpoint (.power ⟨k, by omega⟩) |
| 270 | { saved := ys.reverse ++ saved, base := base, count := 1, cert := cert + ys.length }) := by |
| 271 | induction ys generalizing saved cert with |
| 272 | | nil => apply oneStep; certificate_step |
| 273 | | cons y ys ih => |
| 274 | refine beginStep ?_ (by |
| 275 | simpa [List.reverse_cons, List.append_assoc, Nat.add_assoc, Nat.add_comm 1] using |
| 276 | ih (y :: saved) (cert + 1)) |
| 277 | certificate_step |
| 278 | |
| 279 | private def scanLeft (xs ys saved : List (tm.Γ tm.k₀)) (base : ℕ) |
| 280 | (noSep : ∀ x ∈ xs, x ≠ sep) : |
| 281 | Run tm sep reject k |
| 282 | (checkpoint (.scan false) { input := xs ++ sep :: ys, saved := saved, base := base }) |
| 283 | (checkpoint (.scan true) |
| 284 | { input := ys, saved := sep :: (xs.reverse ++ saved), base := base + xs.length, count := 1 }) := by |
| 285 | induction xs generalizing saved base with |
| 286 | | nil => apply oneStep; certificate_step |
| 287 | | cons x xs ih => |
| 288 | have hx : x ≠ sep := noSep x (by simp) |
| 289 | refine beginStep ?_ (by |
| 290 | simpa [List.reverse_cons, List.append_assoc, Nat.add_assoc, Nat.add_comm 1] using |
| 291 | ih (x :: saved) (base + 1) (fun z hz => noSep z (by simp [hz]))) |
| 292 | certificate_step |
| 293 | |
| 294 | private def copyRun (i : Fin (k + 1)) (n : ℕ) (w : Work (tm.Γ tm.k₀)) : |
| 295 | Run tm sep reject k (checkpoint (.copy i) { w with base := n }) |
| 296 | (checkpoint (.putBack i) { w with base := 0, temp := w.temp + n, next := w.next + n }) := by |
| 297 | induction n generalizing w with |
| 298 | | zero => apply oneStep; certificate_step |
| 299 | | succ n ih => |
| 300 | refine beginStep ?_ (by |
| 301 | simpa [Nat.add_assoc, Nat.add_comm 1] using ih { w with temp := w.temp + 1, next := w.next + 1 }) |
| 302 | certificate_step |
| 303 | |
| 304 | private def putBackRun (i : Fin (k + 1)) (n : ℕ) (w : Work (tm.Γ tm.k₀)) : |
| 305 | Run tm sep reject k (checkpoint (.putBack i) { w with temp := n }) |
| 306 | (checkpoint (.mul i) { w with base := w.base + n, temp := 0 }) := by |
| 307 | induction n generalizing w with |
| 308 | | zero => apply oneStep; certificate_step |
| 309 | | succ n ih => |
| 310 | refine beginStep ?_ (by |
| 311 | simpa [Nat.add_assoc, Nat.add_comm 1] using ih { w with base := w.base + 1 }) |
| 312 | certificate_step |
| 313 | |
| 314 | private def mulRun (i : Fin (k + 1)) (n : ℕ) (w : Work (tm.Γ tm.k₀)) : |
| 315 | Run tm sep reject k (checkpoint (.mul i) { w with count := n, temp := 0 }) |
| 316 | (checkpoint (.transfer i) { w with count := 0, temp := 0, next := w.next + n * w.base }) := by |
| 317 | induction n generalizing w with |
| 318 | | zero => apply oneStep; certificate_step |
| 319 | | succ n ih => |
| 320 | have copied := copyRun (sep := sep) (reject := reject) i w.base { w with count := n, temp := 0 } |
| 321 | have returned := putBackRun (sep := sep) (reject := reject) i w.base |
| 322 | { w with base := 0, count := n, next := w.next + w.base } |
| 323 | simp only [Nat.zero_add] at copied returned |
| 324 | have remaining := ih { w with next := w.next + w.base } |
| 325 | have rest := EvalsTo.trans _ _ _ _ copied (EvalsTo.trans _ _ _ _ returned remaining) |
| 326 | have sums : w.next + w.base + n * w.base = w.next + (n + 1) * w.base := by |
| 327 | rw [Nat.succ_mul] |
| 328 | omega |
| 329 | exact beginStep (by certificate_step) (sums ▸ rest) |
| 330 | |
| 331 | private def transferRun (i : Fin (k + 1)) (n : ℕ) (w : Work (tm.Γ tm.k₀)) : |
| 332 | Run tm sep reject k (checkpoint (.transfer i) { w with next := n }) |
| 333 | (checkpoint (.power (prev i)) { w with next := 0, count := w.count + n }) := by |
| 334 | induction n generalizing w with |
| 335 | | zero => apply oneStep; certificate_step |
| 336 | | succ n ih => |
| 337 | refine beginStep ?_ (by |
| 338 | simpa [Nat.add_assoc, Nat.add_comm 1] using ih { w with count := w.count + 1 }) |
| 339 | certificate_step |
| 340 | |
| 341 | private def powerRun (n : ℕ) (i : Fin (k + 1)) (hi : i.val = n) (w : Work (tm.Γ tm.k₀)) : |
| 342 | Run tm sep reject k (checkpoint (.power i) { w with temp := 0, next := 0 }) |
| 343 | (checkpoint .clearBase { w with temp := 0, next := 0, count := w.count * w.base ^ n }) := by |
| 344 | induction n generalizing i w with |
| 345 | | zero => apply oneStep; certificate_step |
| 346 | | succ n ih => |
| 347 | have hn : i.val ≠ 0 := by omega |
| 348 | have hp : (prev i).val = n := by simp [prev, hi] |
| 349 | have multiplied := mulRun (sep := sep) (reject := reject) i w.count { w with next := 0 } |
| 350 | have transferred := transferRun (sep := sep) (reject := reject) i (w.count * w.base) |
| 351 | { w with count := 0, temp := 0 } |
| 352 | simp only [Nat.zero_add] at multiplied transferred |
| 353 | have remaining := ih (prev i) hp { w with count := w.count * w.base } |
| 354 | have rest := EvalsTo.trans _ _ _ _ multiplied (EvalsTo.trans _ _ _ _ transferred remaining) |
| 355 | have product : (w.count * w.base) * w.base ^ n = w.count * w.base ^ (n + 1) := by |
| 356 | rw [pow_succ', Nat.mul_assoc] |
| 357 | exact beginStep (by certificate_step) (product ▸ rest) |
| 358 | |
| 359 | private def clearBaseRun (n : ℕ) (w : Work (tm.Γ tm.k₀)) : |
| 360 | Run tm sep reject k (checkpoint .clearBase { w with base := n }) |
| 361 | (checkpoint (.compare true) { w with base := 0 }) := by |
| 362 | induction n with |
| 363 | | zero => apply oneStep; certificate_step |
| 364 | | succ n ih => exact beginStep (by certificate_step) ih |
| 365 | |
| 366 | private def drainRun (ok : Bool) (n : ℕ) (w : Work (tm.Γ tm.k₀)) : |
| 367 | Run tm sep reject k (checkpoint (.drain ok) { w with count := n }) |
| 368 | (checkpoint (.restore ok) { w with count := 0 }) := by |
| 369 | induction n with |
| 370 | | zero => apply oneStep; certificate_step |
| 371 | | succ n ih => exact beginStep (by certificate_step) ih |
| 372 | |
| 373 | private def compareRun (m n : ℕ) (ok : Bool) (w : Work (tm.Γ tm.k₀)) : |
| 374 | Run tm sep reject k (checkpoint (.compare ok) { w with cert := m, count := n }) |
| 375 | (checkpoint (.restore (ok && decide (m ≤ n))) { w with cert := 0, count := 0 }) := by |
| 376 | induction m generalizing n ok with |
| 377 | | zero => |
| 378 | refine beginStep ?_ (by simpa using drainRun ok n { w with cert := 0 }) |
| 379 | certificate_step |
| 380 | | succ m ih => |
| 381 | cases n with |
| 382 | | zero => |
| 383 | refine beginStep ?_ (by simpa using ih 0 false) |
| 384 | certificate_step |
| 385 | | succ n => |
| 386 | refine beginStep ?_ (by simpa using ih n ok) |
| 387 | certificate_step |
| 388 | |
| 389 | private def restoreRun (saved inp : List (tm.Γ tm.k₀)) : |
| 390 | Run tm sep reject k (checkpoint (.restore true) { input := inp, saved := saved }) |
| 391 | (liftCfg (initList tm (saved.reverse ++ inp))) := by |
| 392 | induction saved generalizing inp with |
| 393 | | nil => apply oneStep; certificate_step |
| 394 | | cons a xs ih => |
| 395 | refine beginStep ?_ (by simpa [List.reverse_cons, List.append_assoc] using ih (a :: inp)) |
| 396 | certificate_step |
| 397 | |
| 398 | private def rejectRun (saved : List (tm.Γ tm.k₀)) : |
| 399 | Run tm sep reject k (checkpoint (.restore false) { saved := saved }) |
| 400 | (haltList (guarded tm sep reject k) [reject]) := by |
| 401 | induction saved with |
| 402 | | nil => |
| 403 | apply oneStep |
| 404 | let empty (j : tm.K ⊕ Aux) : List (Alphabet tm j) := [] |
| 405 | have blank : (checkpoint (.restore false : Phase k) ({} : Work (tm.Γ tm.k₀))).stk = empty := by |
| 406 | funext j |
| 407 | cases j with |
| 408 | | inl j => exact initEmpty tm j |
| 409 | | inr j => cases j <;> rfl |
| 410 | have clean : Function.update empty (.inr .saved) [] = empty := |
| 411 | Function.update_eq_self _ _ |
| 412 | have output : Function.update empty (.inl tm.k₁) [reject] = |
| 413 | (haltList (guarded tm sep reject k) [reject]).stk := by |
| 414 | funext j |
| 415 | by_cases eq : j = Sum.inl tm.k₁ |
| 416 | · subst j |
| 417 | exact (Function.update_self (Sum.inl tm.k₁ : tm.K ⊕ Aux) [reject] empty).trans |
| 418 | (haltList_stk_self (guarded tm sep reject k) [reject]).symm |
| 419 | · exact (Function.update_of_ne eq [reject] empty).trans |
| 420 | (haltList_stk_ne (guarded tm sep reject k) [reject] eq).symm |
| 421 | apply congrArg some |
| 422 | change TM2.stepAux (guard tm sep reject k (.restore false)) (none, tm.initialState) |
| 423 | (checkpoint (.restore false) ({} : Work (tm.Γ tm.k₀))).stk = _ |
| 424 | rw [blank] |
| 425 | change TM2.Cfg.mk none (none, tm.initialState) |
| 426 | (Function.update (Function.update empty (.inr .saved) []) (.inl tm.k₁) [reject]) = _ |
| 427 | rw [clean] |
| 428 | exact congrArg (fun st => ⟨none, (none, tm.initialState), st⟩) output |
| 429 | | cons a xs ih => exact beginStep (by certificate_step) ih |
| 430 | |
| 431 | private theorem initial (inp : List (tm.Γ tm.k₀)) : |
| 432 | initList (guarded tm sep reject k) inp = checkpoint (.scan false) { input := inp } := by |
| 433 | simp only [initList, checkpoint, guarded] |
| 434 | congr |
| 435 | funext j |
| 436 | cases j with |
| 437 | | inl j => by_cases h : j = tm.k₀ <;> (try subst j) <;> simp [*] |
| 438 | | inr j => cases j <;> rfl |
| 439 | |
| 440 | private theorem final (out : List (tm.Γ tm.k₁)) : |
| 441 | liftCfg (k := k) (haltList tm out) = haltList (guarded tm sep reject k) out := by |
| 442 | simp only [liftCfg, haltList, guarded, Option.map_none] |
| 443 | congr |
| 444 | funext j |
| 445 | cases j with |
| 446 | | inl j => by_cases h : j = tm.k₁ <;> (try subst j) <;> simp [liftStacks, *] |
| 447 | | inr j => rfl |
| 448 | |
| 449 | /-- The old verifier's answer on certificates meeting the length bound, and false |
| 450 | otherwise. Length is measured on the left input's encoding and on the right word. -/ |
| 451 | def check {α Γ : Type} (ea : FinEncoding α) (k : ℕ) (verifier : α × List Γ → Bool) |
| 452 | (p : α × List Γ) : Bool := |
| 453 | if p.2.length ≤ (ea.encode p.1).length ^ k then verifier p else false |
| 454 | |
| 455 | /-- Adding Cook's certificate length bound preserves computable checking. -/ |
| 456 | def checked {α Γ : Type} [Fintype Γ] {ea : FinEncoding α} |
| 457 | {verifier : α × List Γ → Bool} |
| 458 | (h : TM2Computable (pair_encoding ea (fin_encoding_string Γ)).encode |
| 459 | finEncodingBoolBool.encode verifier) (k : ℕ) : |
| 460 | TM2Computable (pair_encoding ea (fin_encoding_string Γ)).encode |
| 461 | finEncodingBoolBool.encode (check ea k verifier) where |
| 462 | tm := guarded h.tm (h.inputAlphabet.symm (Sum.inr none)) (h.outputAlphabet.symm false) k |
| 463 | inputAlphabet := h.inputAlphabet |
| 464 | outputAlphabet := h.outputAlphabet |
| 465 | outputsFun p := by |
| 466 | let rawDecode : ea.Γ ⊕ Option Γ → h.tm.Γ h.tm.k₀ := h.inputAlphabet.symm |
| 467 | let sep := rawDecode (Sum.inr none) |
| 468 | let reject := h.outputAlphabet.symm false |
| 469 | let xs := (ea.encode p.1).map (fun a => rawDecode (Sum.inl a)) |
| 470 | let ys := p.2.map (fun b => rawDecode (Sum.inr (some b))) |
| 471 | have inputEq : |
| 472 | List.map h.inputAlphabet.invFun ((pair_encoding ea (fin_encoding_string Γ)).encode p) = |
| 473 | xs ++ sep :: ys := by |
| 474 | refine (congrArg (List.map h.inputAlphabet.invFun) |
| 475 | (pair_encoding.encode_eq ea (fin_encoding_string Γ) p)).trans ?_ |
| 476 | let left := (ea.encode p.1).map (fun a => (Sum.inl a : ea.Γ ⊕ Option Γ)) |
| 477 | let right := p.2.map (fun b => (Sum.inr (some b) : ea.Γ ⊕ Option Γ)) |
| 478 | let marker : ea.Γ ⊕ Option Γ := Sum.inr none |
| 479 | change List.map rawDecode (List.append left (List.cons marker right)) = xs ++ sep :: ys |
| 480 | calc |
| 481 | _ = List.append (List.map rawDecode left) (List.map rawDecode (marker :: right)) := |
| 482 | List.map_append (f := rawDecode) (l₁ := left) (l₂ := marker :: right) |
| 483 | _ = xs ++ sep :: ys := |
| 484 | congrArg₂ List.append |
| 485 | (List.map_map (g := rawDecode) (l := ea.encode p.1)) |
| 486 | (congrArg (List.cons (rawDecode marker)) (List.map_map (g := rawDecode) (l := p.2))) |
| 487 | have noSep : ∀ x ∈ xs, x ≠ sep := by |
| 488 | intro x hx eq |
| 489 | obtain ⟨a, _, rfl⟩ := List.mem_map.mp hx |
| 490 | have bad := h.inputAlphabet.symm.injective eq |
| 491 | cases bad |
| 492 | have scan := EvalsTo.trans _ _ _ _ |
| 493 | (scanLeft (reject := reject) (k := k) xs ys [] 0 noSep) |
| 494 | (scanRight (sep := sep) (reject := reject) (k := k) |
| 495 | ys (sep :: (xs.reverse ++ [])) (0 + xs.length) 0) |
| 496 | let saved := ys.reverse ++ sep :: (xs.reverse ++ []) |
| 497 | have hs : saved.reverse = xs ++ sep :: ys := by |
| 498 | simp [saved, List.reverse_append, List.reverse_cons, List.append_assoc] |
| 499 | let n := 0 + xs.length |
| 500 | let m := 0 + ys.length |
| 501 | let cap := 1 * n ^ k |
| 502 | have powered := powerRun (sep := sep) (reject := reject) (k := k) k ⟨k, by omega⟩ rfl |
| 503 | { saved := saved, base := n, count := 1, cert := m } |
| 504 | have cleared := clearBaseRun (sep := sep) (reject := reject) (k := k) n |
| 505 | { saved := saved, count := cap, cert := m } |
| 506 | have compared := compareRun (sep := sep) (reject := reject) (k := k) m cap true |
| 507 | { saved := saved } |
| 508 | have prefixRun := EvalsTo.trans _ _ _ _ scan (EvalsTo.trans _ _ _ _ powered |
| 509 | (EvalsTo.trans _ _ _ _ cleared compared)) |
| 510 | have lengths : (m ≤ cap) ↔ |
| 511 | p.2.length ≤ (ea.encode p.1).length ^ k := by |
| 512 | simp only [m, cap, n, xs, ys, Nat.zero_add, Nat.one_mul, List.length_map] <;> rfl |
| 513 | have started := (congrArg (initList (guarded h.tm sep reject k)) inputEq).trans |
| 514 | (initial (sep := sep) (reject := reject) (k := k) (xs ++ sep :: ys)) |
| 515 | let finish (b : Bool) : (guarded h.tm sep reject k).Cfg := |
| 516 | haltList (guarded h.tm sep reject k) |
| 517 | (List.map h.outputAlphabet.invFun (finEncodingBoolBool.encode b)) |
| 518 | by_cases bound : p.2.length ≤ (ea.encode p.1).length ^ k |
| 519 | · have yes := lengths.mpr bound |
| 520 | simp only [yes, decide_true, Bool.and_self] at prefixRun |
| 521 | have original := h.outputsFun p |
| 522 | rw [inputEq] at original |
| 523 | have restored := restoreRun (sep := sep) (reject := reject) (k := k) saved [] |
| 524 | rw [List.append_nil, hs] at restored |
| 525 | have doneRun := EvalsTo.trans _ _ _ _ prefixRun |
| 526 | (EvalsTo.trans _ _ _ _ restored (liftRun original)) |
| 527 | have answer : check ea k verifier p = verifier p := if_pos bound |
| 528 | have ended := (final (sep := sep) (reject := reject) (k := k) |
| 529 | (List.map h.outputAlphabet.invFun (finEncodingBoolBool.encode (verifier p)))).trans |
| 530 | (congrArg finish answer.symm) |
| 531 | exact Eq.mp (congrArg₂ (fun a b => |
| 532 | EvalsTo (guarded h.tm sep reject k).step a (some b)) started.symm ended) doneRun |
| 533 | · have no := lengths.not.mpr bound |
| 534 | simp only [no, decide_false, Bool.and_false] at prefixRun |
| 535 | have doneRun := EvalsTo.trans _ _ _ _ prefixRun (rejectRun saved) |
| 536 | have answer : check ea k verifier p = false := if_neg bound |
| 537 | have ended : haltList (guarded h.tm sep reject k) [reject] = |
| 538 | finish (check ea k verifier p) := congrArg finish answer.symm |
| 539 | exact Eq.mp (congrArg₂ (fun a b => |
| 540 | EvalsTo (guarded h.tm sep reject k).step a (some b)) started.symm ended) doneRun |
| 541 | |
| 542 | end |
| 543 | |
| 544 | end Millennium.Machines.CertificateBound |