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

Implement the three machine phases and prove their single transitions
Browse filesChecks
build Program · lake build, no sorry
Details
challenge Program · statement files and build config
Details
+119 −0 Problems/PVersusNP/Examples/Palindrome.lean added View file
| @@ -0,0 +1,119 @@ | ||
| 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 | +open Turing Computability StateTransition | |
| 15 | + | |
| 16 | +inductive Stack where | |
| 17 | + | input | scratch | backwards | output | |
| 18 | + deriving DecidableEq, Fintype | |
| 19 | + | |
| 20 | +inductive Phase where | |
| 21 | + | copy | restore | compare | |
| 22 | + deriving DecidableEq, Fintype | |
| 23 | + | |
| 24 | +abbrev Alphabet (α : Type) : Stack → Type | |
| 25 | + | .input => α | |
| 26 | + | .scratch | .backwards => Option α | |
| 27 | + | .output => Bool | |
| 28 | + | |
| 29 | +abbrev Register (α : Type) := Option α × Bool | |
| 30 | + | |
| 31 | +/-- In the successful restore branch a symbol exists. Keeping the fallback | |
| 32 | +inside this instruction lets the machine work for the empty alphabet too. -/ | |
| 33 | +private noncomputable def putInput (α : Type) : | |
| 34 | + TM2.Stmt (Alphabet α) Phase (Register α) := by | |
| 35 | + classical | |
| 36 | + exact if h : Nonempty α then | |
| 37 | + .push .input (fun v => v.1.getD (Classical.choice h)) (.goto fun _ => .restore) | |
| 38 | + else .halt | |
| 39 | + | |
| 40 | +noncomputable def machine (α : Type) [Fintype α] [DecidableEq α] : FinTM2 where | |
| 41 | + K := Stack | |
| 42 | + k₀ := .input | |
| 43 | + k₁ := .output | |
| 44 | + Γ := Alphabet α | |
| 45 | + Λ := Phase | |
| 46 | + main := .copy | |
| 47 | + σ := Register α | |
| 48 | + initialState := (none, true) | |
| 49 | + Γk₀Fin := inferInstanceAs (Fintype α) | |
| 50 | + m | |
| 51 | + | .copy => | |
| 52 | + .pop .input (fun v x => (x, v.2)) | |
| 53 | + (.branch (fun v => v.1.isSome) | |
| 54 | + (.push .scratch (fun v => v.1) | |
| 55 | + (.push .backwards (fun v => v.1) (.goto fun _ => .copy))) | |
| 56 | + (.goto fun _ => .restore)) | |
| 57 | + | .restore => | |
| 58 | + .pop .scratch (fun v x => (x.join, v.2)) | |
| 59 | + (.branch (fun v => v.1.isSome) (putInput α) (.goto fun _ => .compare)) | |
| 60 | + | .compare => | |
| 61 | + .pop .input (fun v x => (x, v.2)) | |
| 62 | + (.branch (fun v => v.1.isSome) | |
| 63 | + (.pop .backwards (fun v x => (none, v.2 && decide (v.1 = x.join))) | |
| 64 | + (.goto fun _ => .compare)) | |
| 65 | + (.push .output (fun v => v.2) | |
| 66 | + (.load (fun _ => (none, true)) .halt))) | |
| 67 | + | |
| 68 | +variable {α : Type} [Fintype α] [DecidableEq α] | |
| 69 | + | |
| 70 | +/-- Reachable configurations store only real symbols on the work stacks. -/ | |
| 71 | +def cfg (l : Option Phase) (v : Register α) (inp scratch backwards : List α) | |
| 72 | + (out : List Bool) : (machine α).Cfg where | |
| 73 | + l := l | |
| 74 | + var := v | |
| 75 | + stk | |
| 76 | + | .input => inp | |
| 77 | + | .scratch => scratch.map some | |
| 78 | + | .backwards => backwards.map some | |
| 79 | + | .output => out | |
| 80 | + | |
| 81 | +private def Go (phase : Phase) (v : Register α) (inp s r : List α) (out : List Bool) := | |
| 82 | + cfg (some phase) v inp s r out | |
| 83 | + | |
| 84 | +macro "pal_step" : tactic => | |
| 85 | + `(tactic| (simp [Go, FinTM2.step, TM2.step, TM2.stepAux, machine, cfg, putInput, *] | |
| 86 | + try (congr <;> first | rfl | (funext k; cases k <;> rfl) | (simp; done)))) | |
| 87 | + | |
| 88 | +theorem copy_cons (v : Register α) (a : α) (inp s r : List α) (out : List Bool) : | |
| 89 | + (machine α).step (Go .copy v (a :: inp) s r out) = | |
| 90 | + some (Go .copy (some a, v.2) inp (a :: s) (a :: r) out) := by | |
| 91 | + pal_step | |
| 92 | + | |
| 93 | +theorem copy_nil (v : Register α) (s r : List α) (out : List Bool) : | |
| 94 | + (machine α).step (Go .copy v [] s r out) = | |
| 95 | + some (Go .restore (none, v.2) [] s r out) := by | |
| 96 | + pal_step | |
| 97 | + | |
| 98 | +theorem restore_cons (v : Register α) (a : α) (inp s r : List α) (out : List Bool) : | |
| 99 | + (machine α).step (Go .restore v inp (a :: s) r out) = | |
| 100 | + some (Go .restore (some a, v.2) (a :: inp) s r out) := by | |
| 101 | + have hex : Nonempty α := ⟨a⟩ | |
| 102 | + pal_step | |
| 103 | + | |
| 104 | +theorem restore_nil (v : Register α) (inp r : List α) (out : List Bool) : | |
| 105 | + (machine α).step (Go .restore v inp [] r out) = | |
| 106 | + some (Go .compare (none, v.2) inp [] r out) := by | |
| 107 | + pal_step | |
| 108 | + | |
| 109 | +theorem compare_cons (v : Register α) (a b : α) (xs ys : List α) (out : List Bool) : | |
| 110 | + (machine α).step (Go .compare v (a :: xs) [] (b :: ys) out) = | |
| 111 | + some (Go .compare (none, v.2 && decide (a = b)) xs [] ys out) := by | |
| 112 | + pal_step | |
| 113 | + | |
| 114 | +theorem compare_nil (v : Register α) (out : List Bool) : | |
| 115 | + (machine α).step (Go .compare v [] [] [] out) = | |
| 116 | + some (cfg none (none, true) ([] : List α) [] [] (v.2 :: out)) := by | |
| 117 | + pal_step | |
| 118 | + | |
| 119 | +end Millennium.Examples.Palindrome | |