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

refactor(PVersusNP): extract general machine run and configuration helpers
Browse filesChecks
build Program · lake build, no sorry
Details
challenge Program · statement files and build config
Details
+22 −21 Problems/PVersusNP/Examples/Parity.lean View file
| @@ -1,4 +1,6 @@ | ||
| 1 | 1 | import Problems.PVersusNP.Millennium |
| 2 | +import Problems.PVersusNP.ForMathlib.StateTransition | |
| 3 | +import Problems.PVersusNP.ForMathlib.Computable | |
| 2 | 4 | |
| 3 | 5 | /-! |
| 4 | 6 | # Even parity in polynomial time |
| @@ -102,47 +104,46 @@ theorem step_nil (v : Bool × Bool) (out : List Bool) : | ||
| 102 | 104 | some (cfg none (false, true) [] (v.2 :: out)) := by |
| 103 | 105 | parity_step_tac |
| 104 | 106 | |
| 105 | -/-- Weaken a timed computation's upper bound. -/ | |
| 106 | -private def timedMono {σ : Type} {f : σ → Option σ} {a : σ} {b : Option σ} | |
| 107 | - {m m' : ℕ} (h : EvalsToInTime f a b m) (hm : m ≤ m') : | |
| 108 | - EvalsToInTime f a b m' := | |
| 109 | - ⟨h.toEvalsTo, le_trans h.steps_le_m hm⟩ | |
| 110 | - | |
| 111 | -/-- Package an explicit machine transition as a one-step timed run. -/ | |
| 112 | -private def oneStep {a b : parityMachine.Cfg} (h : parityMachine.step a = some b) : | |
| 113 | - EvalsToInTime parityMachine.step a (some b) 1 := | |
| 114 | - { steps := 1 | |
| 115 | - evals_in_steps := by simpa [flip, Option.bind] using h | |
| 116 | - steps_le_m := le_rfl } | |
| 117 | - | |
| 118 | 107 | /-- One transition per input symbol, followed by one transition writing the answer. -/ |
| 119 | 108 | def run (inp : List Bool) (v : Bool × Bool) (out : List Bool) : |
| 120 | 109 | EvalsToInTime parityMachine.step (cfg (some ()) v inp out) |
| 121 | 110 | (some (cfg none (false, true) [] (parityAfter inp v.2 :: out))) |
| 122 | 111 | (inp.length + 1) := by |
| 123 | 112 | induction inp generalizing v with |
| 124 | - | nil => simpa [parityAfter] using oneStep (step_nil v out) | |
| 113 | + | nil => simpa [parityAfter] using EvalsToInTime.of_step (step_nil v out) | |
| 125 | 114 | | cons a inp ih => |
| 126 | - have h₁ := oneStep (step_cons v a inp out) | |
| 115 | + have h₁ := EvalsToInTime.of_step (step_cons v a inp out) | |
| 127 | 116 | have h₂ := ih (true, toggle a v.2) |
| 128 | 117 | have h := EvalsToInTime.trans parityMachine.step 1 (inp.length + 1) _ _ _ h₁ h₂ |
| 129 | - exact timedMono h (by simp) | |
| 118 | + exact EvalsToInTime.mono h (by simp) | |
| 130 | 119 | |
| 131 | 120 | /-- The canonical initial configuration starts with even parity. -/ |
| 132 | 121 | theorem initList_eq (inp : List Bool) : |
| 133 | 122 | initList parityMachine inp = cfg (some ()) (false, true) inp [] := by |
| 134 | - unfold initList cfg | |
| 123 | + change ({ l := (initList parityMachine inp).l, | |
| 124 | + var := (initList parityMachine inp).var, | |
| 125 | + stk := (initList parityMachine inp).stk } : parityMachine.Cfg) = _ | |
| 126 | + rw [initList_l, initList_var] | |
| 127 | + unfold cfg | |
| 135 | 128 | congr |
| 136 | 129 | funext k |
| 137 | - cases k <;> rfl | |
| 130 | + cases k with | |
| 131 | + | false => exact initList_stk_ne parityMachine inp (by decide) | |
| 132 | + | true => exact initList_stk_self parityMachine inp | |
| 138 | 133 | |
| 139 | 134 | /-- The canonical halted configuration has the reset state and only its output stack. -/ |
| 140 | 135 | theorem haltList_eq (out : List Bool) : |
| 141 | 136 | haltList parityMachine out = cfg none (false, true) [] out := by |
| 142 | - unfold haltList cfg | |
| 137 | + change ({ l := (haltList parityMachine out).l, | |
| 138 | + var := (haltList parityMachine out).var, | |
| 139 | + stk := (haltList parityMachine out).stk } : parityMachine.Cfg) = _ | |
| 140 | + rw [haltList_l, haltList_var] | |
| 141 | + unfold cfg | |
| 143 | 142 | congr |
| 144 | 143 | funext k |
| 145 | - cases k <;> rfl | |
| 144 | + cases k with | |
| 145 | + | false => exact haltList_stk_self parityMachine out | |
| 146 | + | true => exact haltList_stk_ne parityMachine out (by decide) | |
| 146 | 147 | |
| 147 | 148 | /-- The explicit parity machine computes the Boolean answer with time polynomial `X + 1`. -/ |
| 148 | 149 | noncomputable def parityDecider : |
| @@ -160,7 +161,7 @@ noncomputable def parityDecider : | ||
| 160 | 161 | rw [List.map_id, List.map_id, initList_eq, haltList_eq] |
| 161 | 162 | have h := run inp (false, true) [] |
| 162 | 163 | rw [parityAfter_eq] at h |
| 163 | - exact timedMono h (by simp) | |
| 164 | + exact EvalsToInTime.mono h (by simp) | |
| 164 | 165 | |
| 165 | 166 | /-- Even-`true`-count Boolean strings are in P, with no composition hypothesis. -/ |
| 166 | 167 | theorem evenTrueCount_in_P : |
+51 −0 Problems/PVersusNP/ForMathlib/Computable.lean added View file
| @@ -0,0 +1,51 @@ | ||
| 1 | +import Mathlib.Computability.TuringMachine.Computable | |
| 2 | + | |
| 3 | +/-! | |
| 4 | +# Stack configuration API for bundled machines | |
| 5 | + | |
| 6 | +The initial and halted configurations select exactly one designated stack. | |
| 7 | +These lemmas expose their label, state and stacks without requiring a client | |
| 8 | +to unfold their dependent conditional definitions. They use only Mathlib's | |
| 9 | +machine model; their proposed upstream home is | |
| 10 | +`Mathlib/Computability/TuringMachine/Computable.lean`. | |
| 11 | +-/ | |
| 12 | + | |
| 13 | +namespace Turing | |
| 14 | + | |
| 15 | +/-- The initial configuration starts at the machine's main label. -/ | |
| 16 | +@[simp] theorem initList_l (tm : FinTM2) (s : List (tm.Γ tm.k₀)) : | |
| 17 | + (initList tm s).l = some tm.main := rfl | |
| 18 | + | |
| 19 | +/-- The initial configuration uses the machine's initial state. -/ | |
| 20 | +@[simp] theorem initList_var (tm : FinTM2) (s : List (tm.Γ tm.k₀)) : | |
| 21 | + (initList tm s).var = tm.initialState := rfl | |
| 22 | + | |
| 23 | +/-- The input stack of the initial configuration contains the input list. -/ | |
| 24 | +@[simp] theorem initList_stk_self (tm : FinTM2) (s : List (tm.Γ tm.k₀)) : | |
| 25 | + (initList tm s).stk tm.k₀ = s := by | |
| 26 | + simp [initList] | |
| 27 | + | |
| 28 | +/-- Every stack other than the input stack is initially empty. -/ | |
| 29 | +@[simp] theorem initList_stk_ne (tm : FinTM2) (s : List (tm.Γ tm.k₀)) | |
| 30 | + {k : tm.K} (h : k ≠ tm.k₀) : (initList tm s).stk k = [] := by | |
| 31 | + simp [initList, h] | |
| 32 | + | |
| 33 | +/-- The halted configuration has no current label. -/ | |
| 34 | +@[simp] theorem haltList_l (tm : FinTM2) (s : List (tm.Γ tm.k₁)) : | |
| 35 | + (haltList tm s).l = none := rfl | |
| 36 | + | |
| 37 | +/-- The halted configuration resets to the machine's initial state. -/ | |
| 38 | +@[simp] theorem haltList_var (tm : FinTM2) (s : List (tm.Γ tm.k₁)) : | |
| 39 | + (haltList tm s).var = tm.initialState := rfl | |
| 40 | + | |
| 41 | +/-- The output stack of the halted configuration contains the output list. -/ | |
| 42 | +@[simp] theorem haltList_stk_self (tm : FinTM2) (s : List (tm.Γ tm.k₁)) : | |
| 43 | + (haltList tm s).stk tm.k₁ = s := by | |
| 44 | + simp [haltList] | |
| 45 | + | |
| 46 | +/-- Every stack other than the output stack is empty in the halted configuration. -/ | |
| 47 | +@[simp] theorem haltList_stk_ne (tm : FinTM2) (s : List (tm.Γ tm.k₁)) | |
| 48 | + {k : tm.K} (h : k ≠ tm.k₁) : (haltList tm s).stk k = [] := by | |
| 49 | + simp [haltList, h] | |
| 50 | + | |
| 51 | +end Turing | |
+34 −0 Problems/PVersusNP/ForMathlib/StateTransition.lean added View file
| @@ -0,0 +1,34 @@ | ||
| 1 | +import Mathlib.Computability.StateTransition | |
| 2 | + | |
| 3 | +/-! | |
| 4 | +# General timed state-transition helpers | |
| 5 | + | |
| 6 | +These definitions mention only Mathlib's state-transition model, with no machine | |
| 7 | +or complexity-class assumptions. Their proposed upstream home is | |
| 8 | +`Mathlib/Computability/StateTransition.lean`, beside `EvalsToInTime.refl` and | |
| 9 | +`EvalsToInTime.trans`. | |
| 10 | + | |
| 11 | +`EvalsToInTime` stores a run and its step count as data, so the constructors below | |
| 12 | +are definitions rather than propositions. | |
| 13 | +-/ | |
| 14 | + | |
| 15 | +universe u | |
| 16 | + | |
| 17 | +namespace StateTransition | |
| 18 | + | |
| 19 | +/-- A computation within `m` steps is also a computation within any larger bound. | |
| 20 | +This preserves both the recorded run and its exact step count. -/ | |
| 21 | +def EvalsToInTime.mono {σ : Type u} {f : σ → Option σ} {a : σ} {b : Option σ} | |
| 22 | + {m m' : ℕ} (h : EvalsToInTime f a b m) (hm : m ≤ m') : | |
| 23 | + EvalsToInTime f a b m' := | |
| 24 | + ⟨h.toEvalsTo, le_trans h.steps_le_m hm⟩ | |
| 25 | + | |
| 26 | +/-- A single transition, including a transition to `none`, gives a one-step run. | |
| 27 | +The resulting witness records exactly one step. -/ | |
| 28 | +def EvalsToInTime.of_step {σ : Type u} {f : σ → Option σ} {a : σ} {b : Option σ} | |
| 29 | + (h : f a = b) : EvalsToInTime f a b 1 := | |
| 30 | + { steps := 1 | |
| 31 | + evals_in_steps := by simpa [flip, Option.bind] using h | |
| 32 | + steps_le_m := le_rfl } | |
| 33 | + | |
| 34 | +end StateTransition | |