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 | |
| 3 | /-! |
| 4 | # Even parity in polynomial time |
| 5 | |
| 6 | An explicit two-stack machine reads each input bit once, toggling an even-parity |
| 7 | flag on `true`, then writes that flag and halts. Its configuration-level run takes |
| 8 | at most `input.length + 1` steps. The accepted language is stated using |
| 9 | `List.count true`, and `evenTrueCount_in_P` requires no composition hypothesis. |
| 10 | -/ |
| 11 | |
| 12 | namespace Millennium.Examples.Parity |
| 13 | |
| 14 | open Turing Computability StateTransition |
| 15 | |
| 16 | /-- Toggle the running parity flag exactly when the input bit is `true`. -/ |
| 17 | def toggle (a v : Bool) : Bool := if a then !v else v |
| 18 | |
| 19 | /-- The even-parity answer, expressed independently of the machine's state. -/ |
| 20 | def evenParity : List Bool → Bool |
| 21 | | [] => true |
| 22 | | a :: inp => if a then !(evenParity inp) else evenParity inp |
| 23 | |
| 24 | /-- The language of Boolean strings with an even number of `true` symbols. -/ |
| 25 | def evenTrueCount (inp : List Bool) : Prop := inp.count true % 2 = 0 |
| 26 | |
| 27 | /-- The recursive Boolean answer agrees with counting the `true` symbols. -/ |
| 28 | theorem evenParity_eq_true_iff (inp : List Bool) : |
| 29 | evenParity inp = true ↔ evenTrueCount inp := by |
| 30 | induction inp with |
| 31 | | nil => simp [evenParity, evenTrueCount] |
| 32 | | cons a inp ih => |
| 33 | cases a with |
| 34 | | false => simpa [evenParity, evenTrueCount] using ih |
| 35 | | true => |
| 36 | have hnot : (!(evenParity inp)) = true ↔ ¬ (evenParity inp = true) := by |
| 37 | cases evenParity inp <;> decide |
| 38 | change (!(evenParity inp)) = true ↔ (true :: inp).count true % 2 = 0 |
| 39 | rw [hnot, ih] |
| 40 | have hcount : (true :: inp).count true = inp.count true + 1 := by simp |
| 41 | rw [hcount] |
| 42 | unfold evenTrueCount |
| 43 | omega |
| 44 | |
| 45 | /-- Run the parity update from an arbitrary initial flag. -/ |
| 46 | def parityAfter : List Bool → Bool → Bool |
| 47 | | [], v => v |
| 48 | | a :: inp, v => parityAfter inp (toggle a v) |
| 49 | |
| 50 | /-- Starting with the opposite flag complements the final answer. -/ |
| 51 | theorem parityAfter_eq (inp : List Bool) (v : Bool) : |
| 52 | parityAfter inp v = if v then evenParity inp else !(evenParity inp) := by |
| 53 | induction inp generalizing v with |
| 54 | | nil => cases v <;> rfl |
| 55 | | cons a inp ih => |
| 56 | rw [parityAfter, ih] |
| 57 | cases a <;> cases v <;> simp [toggle, evenParity] |
| 58 | |
| 59 | /-- The first state bit records whether the last pop succeeded, and the second |
| 60 | is the running even-parity flag. Input and output both use the Boolean alphabet. -/ |
| 61 | def parityMachine : FinTM2 where |
| 62 | K := Bool |
| 63 | k₀ := true |
| 64 | k₁ := false |
| 65 | Γ := fun _ => Bool |
| 66 | Λ := Unit |
| 67 | main := () |
| 68 | σ := Bool × Bool |
| 69 | initialState := (false, true) |
| 70 | Γk₀Fin := inferInstanceAs (Fintype Bool) |
| 71 | m _ := |
| 72 | TM2.Stmt.pop true (fun v x => |
| 73 | (x.isSome, match x with |
| 74 | | none => v.2 |
| 75 | | some a => toggle a v.2)) |
| 76 | (TM2.Stmt.branch (fun v => v.1) (TM2.Stmt.goto fun _ => ()) |
| 77 | (TM2.Stmt.push false (fun v => v.2) |
| 78 | (TM2.Stmt.load (fun _ => (false, true)) TM2.Stmt.halt))) |
| 79 | |
| 80 | /-- Explicit configurations for the parity machine. -/ |
| 81 | def cfg (l : Option Unit) (v : Bool × Bool) (inp out : List Bool) : |
| 82 | parityMachine.Cfg where |
| 83 | l := l |
| 84 | var := v |
| 85 | stk |
| 86 | | true => inp |
| 87 | | false => out |
| 88 | |
| 89 | macro "parity_step_tac" : tactic => |
| 90 | `(tactic| (simp [FinTM2.step, TM2.step, TM2.stepAux, parityMachine, cfg] |
| 91 | try (congr <;> first | rfl | (funext k; cases k <;> rfl) | (simp; done)))) |
| 92 | |
| 93 | /-- Reading a symbol updates the flag and returns to the same instruction. -/ |
| 94 | theorem step_cons (v : Bool × Bool) (a : Bool) (inp out : List Bool) : |
| 95 | parityMachine.step (cfg (some ()) v (a :: inp) out) = |
| 96 | some (cfg (some ()) (true, toggle a v.2) inp out) := by |
| 97 | parity_step_tac |
| 98 | |
| 99 | /-- The empty-input step outputs the accumulated flag, resets the state and halts. -/ |
| 100 | theorem step_nil (v : Bool × Bool) (out : List Bool) : |
| 101 | parityMachine.step (cfg (some ()) v [] out) = |
| 102 | some (cfg none (false, true) [] (v.2 :: out)) := by |
| 103 | parity_step_tac |
| 104 | |
| 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 | /-- One transition per input symbol, followed by one transition writing the answer. -/ |
| 119 | def run (inp : List Bool) (v : Bool × Bool) (out : List Bool) : |
| 120 | EvalsToInTime parityMachine.step (cfg (some ()) v inp out) |
| 121 | (some (cfg none (false, true) [] (parityAfter inp v.2 :: out))) |
| 122 | (inp.length + 1) := by |
| 123 | induction inp generalizing v with |
| 124 | | nil => simpa [parityAfter] using oneStep (step_nil v out) |
| 125 | | cons a inp ih => |
| 126 | have h₁ := oneStep (step_cons v a inp out) |
| 127 | have h₂ := ih (true, toggle a v.2) |
| 128 | have h := EvalsToInTime.trans parityMachine.step 1 (inp.length + 1) _ _ _ h₁ h₂ |
| 129 | exact timedMono h (by simp) |
| 130 | |
| 131 | /-- The canonical initial configuration starts with even parity. -/ |
| 132 | theorem initList_eq (inp : List Bool) : |
| 133 | initList parityMachine inp = cfg (some ()) (false, true) inp [] := by |
| 134 | unfold initList cfg |
| 135 | congr |
| 136 | funext k |
| 137 | cases k <;> rfl |
| 138 | |
| 139 | /-- The canonical halted configuration has the reset state and only its output stack. -/ |
| 140 | theorem haltList_eq (out : List Bool) : |
| 141 | haltList parityMachine out = cfg none (false, true) [] out := by |
| 142 | unfold haltList cfg |
| 143 | congr |
| 144 | funext k |
| 145 | cases k <;> rfl |
| 146 | |
| 147 | /-- The explicit parity machine computes the Boolean answer with time polynomial `X + 1`. -/ |
| 148 | noncomputable def parityDecider : |
| 149 | TM2ComputableInPolyTime (fin_encoding_string Bool).encode |
| 150 | finEncodingBoolBool.encode evenParity where |
| 151 | tm := parityMachine |
| 152 | inputAlphabet := Equiv.refl _ |
| 153 | outputAlphabet := Equiv.refl _ |
| 154 | time := Polynomial.X + 1 |
| 155 | outputsFun inp := by |
| 156 | show EvalsToInTime parityMachine.step |
| 157 | (initList parityMachine (List.map id inp)) |
| 158 | (some (haltList parityMachine (List.map id [evenParity inp]))) |
| 159 | ((Polynomial.X + 1 : Polynomial ℕ).eval inp.length) |
| 160 | rw [List.map_id, List.map_id, initList_eq, haltList_eq] |
| 161 | simpa [parityAfter_eq] using run inp (false, true) [] |
| 162 | |
| 163 | /-- Even-`true`-count Boolean strings are in P, with no composition hypothesis. -/ |
| 164 | theorem evenTrueCount_in_P : |
| 165 | InPolynomialTime (fin_encoding_string Bool) evenTrueCount := |
| 166 | ⟨evenParity, parityDecider, fun inp => (evenParity_eq_true_iff inp).symm⟩ |
| 167 | |
| 168 | #print axioms evenTrueCount_in_P |
| 169 | |
| 170 | end Millennium.Examples.Parity |