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