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 Mathlib.Computability.TuringMachine.Computable |
| 2 | import Mathlib.Data.Fintype.Sum |
| 3 | |
| 4 | /-! |
| 5 | # Adding untouched stacks to a finite stack machine |
| 6 | |
| 7 | Extend the original stack index by a disjoint sum. Each primitive still |
| 8 | reads and writes exactly the same symbol type. The additional stacks may |
| 9 | have arbitrary initial contents; they are carried unchanged through a run, |
| 10 | including its final transition to a halted configuration. |
| 11 | -/ |
| 12 | |
| 13 | namespace Millennium.Machines.ExtraStacks |
| 14 | |
| 15 | open Turing StateTransition Function |
| 16 | |
| 17 | variable {K E : Type} {Γ : K → Type} (extra : E → Type) {Λ σ : Type} |
| 18 | |
| 19 | abbrev ExtendedAlphabet (Γ : K → Type) (extra : E → Type) : K ⊕ E → Type := |
| 20 | Sum.elim Γ extra |
| 21 | |
| 22 | /-- Retain the register, labels, and all operations, changing only stack addresses. -/ |
| 23 | def translate : TM2.Stmt Γ Λ σ → TM2.Stmt (ExtendedAlphabet Γ extra) Λ σ |
| 24 | | .push k f q => .push (.inl k) f (translate q) |
| 25 | | .peek k f q => .peek (.inl k) f (translate q) |
| 26 | | .pop k f q => .pop (.inl k) f (translate q) |
| 27 | | .load f q => .load f (translate q) |
| 28 | | .branch f q r => .branch f (translate q) (translate r) |
| 29 | | .goto f => .goto f |
| 30 | | .halt => .halt |
| 31 | |
| 32 | def joined (st : ∀ k, List (Γ k)) (saved : ∀ e, List (extra e)) : |
| 33 | ∀ k, List (ExtendedAlphabet Γ extra k) |
| 34 | | .inl k => st k |
| 35 | | .inr e => saved e |
| 36 | |
| 37 | def extendCfg (saved : ∀ e, List (extra e)) (c : TM2.Cfg Γ Λ σ) : |
| 38 | TM2.Cfg (ExtendedAlphabet Γ extra) Λ σ := |
| 39 | ⟨c.l, c.var, joined extra c.stk saved⟩ |
| 40 | |
| 41 | variable [DecidableEq K] [DecidableEq E] |
| 42 | |
| 43 | private theorem joined_update (st : ∀ k, List (Γ k)) (saved : ∀ e, List (extra e)) |
| 44 | (k : K) (xs : List (Γ k)) : |
| 45 | Function.update (joined extra st saved) (.inl k) xs = |
| 46 | joined extra (Function.update st k xs) saved := by |
| 47 | funext j |
| 48 | cases j with |
| 49 | | inl j => |
| 50 | by_cases h : j = k |
| 51 | · subst j; simp [joined] |
| 52 | · have different : (Sum.inl j : K ⊕ E) ≠ .inl k := fun eq => h (Sum.inl.inj eq) |
| 53 | simp [joined, Function.update_of_ne h, Function.update_of_ne different] |
| 54 | | inr e => simp [joined, Function.update_of_ne] |
| 55 | |
| 56 | private theorem block_correct (q : TM2.Stmt Γ Λ σ) (v : σ) (st : ∀ k, List (Γ k)) |
| 57 | (saved : ∀ e, List (extra e)) : |
| 58 | TM2.stepAux (translate extra q) v (joined extra st saved) = |
| 59 | extendCfg extra saved (TM2.stepAux q v st) := by |
| 60 | induction q generalizing v st with |
| 61 | | push k f q ih => |
| 62 | change TM2.stepAux (translate extra q) v |
| 63 | (Function.update (joined extra st saved) (.inl k) (f v :: st k)) = _ |
| 64 | rw [joined_update] |
| 65 | exact ih v (Function.update st k (f v :: st k)) |
| 66 | | peek k f q ih => |
| 67 | exact ih (f v (st k).head?) st |
| 68 | | pop k f q ih => |
| 69 | change TM2.stepAux (translate extra q) (f v (st k).head?) |
| 70 | (Function.update (joined extra st saved) (.inl k) (st k).tail) = _ |
| 71 | rw [joined_update] |
| 72 | exact ih (f v (st k).head?) (Function.update st k (st k).tail) |
| 73 | | load f q ih => exact ih (f v) st |
| 74 | | branch f q r ih ir => |
| 75 | cases hf : f v <;> simp only [translate, TM2.stepAux, hf, cond_false, cond_true, ih, ir] |
| 76 | | goto f => rfl |
| 77 | | halt => rfl |
| 78 | |
| 79 | private theorem step_correct (prog : Λ → TM2.Stmt Γ Λ σ) (saved : ∀ e, List (extra e)) |
| 80 | (c : TM2.Cfg Γ Λ σ) : |
| 81 | TM2.step (fun l => translate extra (prog l)) (extendCfg extra saved c) = |
| 82 | (TM2.step prog c).map (extendCfg extra saved) := by |
| 83 | rcases c with ⟨l, v, st⟩ |
| 84 | cases l with |
| 85 | | none => rfl |
| 86 | | some l => |
| 87 | exact congrArg some (block_correct extra (prog l) v st saved) |
| 88 | |
| 89 | /-- The enlarged machine has the same initial instruction, input and output roles. -/ |
| 90 | def extendMachine (tm : FinTM2) (E : Type) [DecidableEq E] [Fintype E] |
| 91 | (extra : E → Type) : FinTM2 where |
| 92 | K := tm.K ⊕ E |
| 93 | kFin := by letI := tm.kFin; infer_instance |
| 94 | k₀ := .inl tm.k₀ |
| 95 | k₁ := .inl tm.k₁ |
| 96 | Γ := ExtendedAlphabet tm.Γ extra |
| 97 | Λ := tm.Λ |
| 98 | ΛFin := tm.ΛFin |
| 99 | main := tm.main |
| 100 | σ := tm.σ |
| 101 | σFin := tm.σFin |
| 102 | initialState := tm.initialState |
| 103 | Γk₀Fin := tm.Γk₀Fin |
| 104 | m l := translate extra (tm.m l) |
| 105 | |
| 106 | /-- Every timed computation lifts with the very same step count and untouched |
| 107 | extra stacks, including when the optional result is empty. -/ |
| 108 | def liftRun (tm : FinTM2) {E : Type} [DecidableEq E] [Fintype E] (extra : E → Type) |
| 109 | (saved : ∀ e, List (extra e)) {a : tm.Cfg} {b : Option tm.Cfg} {bound : ℕ} |
| 110 | (run : EvalsToInTime tm.step a b bound) : |
| 111 | EvalsToInTime (extendMachine tm E extra).step (extendCfg extra saved a) |
| 112 | (b.map (extendCfg extra saved)) bound := by |
| 113 | let f := flip bind tm.step |
| 114 | let g := flip bind (extendMachine tm E extra).step |
| 115 | let send : Option tm.Cfg → Option (extendMachine tm E extra).Cfg := |
| 116 | Option.map (extendCfg extra saved) |
| 117 | have commutes : Function.Semiconj send f g := by |
| 118 | intro o |
| 119 | cases o with |
| 120 | | none => rfl |
| 121 | | some c => exact (step_correct extra tm.m saved c).symm |
| 122 | refine ⟨⟨run.steps, ?_⟩, run.steps_le_m⟩ |
| 123 | exact ((commutes.iterate_right run.steps) (some a)).symm.trans |
| 124 | (congrArg send run.evals_in_steps) |
| 125 | |
| 126 | #print axioms liftRun |
| 127 | |
| 128 | end Millennium.Machines.ExtraStacks |