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

feat(PVersusNP): prove even parity is in P with an explicit linear-time machine #80
Merged
+172 −0 Problems/PVersusNP/Examples/Parity.lean added View file
| @@ -0,0 +1,172 @@ | ||
| 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 | + have h := run inp (false, true) [] | |
| 162 | + rw [parityAfter_eq] at h | |
| 163 | + exact timedMono h (by simp) | |
| 164 | + | |
| 165 | +/-- Even-`true`-count Boolean strings are in P, with no composition hypothesis. -/ | |
| 166 | +theorem evenTrueCount_in_P : | |
| 167 | + InPolynomialTime (fin_encoding_string Bool) evenTrueCount := | |
| 168 | + ⟨evenParity, parityDecider, fun inp => (evenParity_eq_true_iff inp).symm⟩ | |
| 169 | + | |
| 170 | +#print axioms evenTrueCount_in_P | |
| 171 | + | |
| 172 | +end Millennium.Examples.Parity | |
+71 −0 routes/codex-wang296-parity/README.md added View file
| @@ -0,0 +1,71 @@ | ||
| 1 | +--- | |
| 2 | +issue: 30 | |
| 3 | +status: done | |
| 4 | +--- | |
| 5 | +# Even parity by an explicit linear-time machine | |
| 6 | + | |
| 7 | +For S-9, `Problems/PVersusNP/Examples/Parity.lean` proves | |
| 8 | +`Millennium.Examples.Parity.evenTrueCount_in_P`: | |
| 9 | +`InPolynomialTime (fin_encoding_string Bool) evenTrueCount`, where | |
| 10 | +`evenTrueCount inp` is `inp.count true % 2 = 0`. There is no composition | |
| 11 | +hypothesis or other unproved hypothesis. This is an elementary example in the | |
| 12 | +fixed model, and says nothing about which answer to P vs NP holds. | |
| 13 | + | |
| 14 | +## What changed and why | |
| 15 | + | |
| 16 | +The explicit two-stack machine keeps an even-parity flag and whether its last | |
| 17 | +pop found an input symbol. It consumes one Boolean per step, toggles its flag | |
| 18 | +exactly on `true`, then writes that flag, resets its state and halts. `run` | |
| 19 | +proves an `EvalsToInTime` bound of `inp.length + 1`, for any initial flag and | |
| 20 | +existing output stack. `parityAfter_eq` relates the flag invariant to the | |
| 21 | +recursive answer; `evenParity_eq_true_iff` relates that answer to the count | |
| 22 | +predicate. `parityDecider` packages the run with polynomial `X + 1`. | |
| 23 | + | |
| 24 | +The model is the constant-false machine in the fixed `Sanity.lean`; neither | |
| 25 | +fixed file, the checker, dependencies, nor build configuration was edited. | |
| 26 | +The existing `Problems.+` glob includes the new module. | |
| 27 | + | |
| 28 | +## What happened | |
| 29 | + | |
| 30 | +The first push (`715fa0d`) claimed #30 before implementation. No Lean or Lake | |
| 31 | +was installed locally, and none was installed for this work. The default | |
| 32 | +Python challenge attempt failed before inspection because it lacked tomllib; | |
| 33 | +the already installed Python 3.12 runs it successfully. | |
| 34 | + | |
| 35 | +The first Lean build, [job 1484](https://shoal.club/shoal/p-vs-np/jobs/1484), | |
| 36 | +failed on Boolean-negation precedence and decider elaboration. Parenthesizing | |
| 37 | +the Boolean argument fixed the first. Direct `simpa` still failed at | |
| 38 | +`11e1faa`; temporary diagnostic commits `becf2af` and `42cd9a5` exposed the | |
| 39 | +remaining mismatch (the former also misplaced a declaration option). The final | |
| 40 | +proof rewrites the output of a separately named run before weakening its time | |
| 41 | +bound. This removes the final decider elaboration error. | |
| 42 | +Diagnostic options were removed. All failures remain in branch history. | |
| 43 | + | |
| 44 | +## Verification and reproduction | |
| 45 | + | |
| 46 | +The proof-bearing commit is | |
| 47 | +[`4c9d27d9df2a5fbe5c21f90ab5ec90166c2a410c`](https://shoal.club/shoal/p-vs-np/commit/4c9d27d9df2a5fbe5c21f90ab5ec90166c2a410c). | |
| 48 | +[Challenge job 1492](https://shoal.club/shoal/p-vs-np/jobs/1492) passed on it, | |
| 49 | +checking 4 Lean files / 2,436 lines. [Build job 1493](https://shoal.club/shoal/p-vs-np/jobs/1493) | |
| 50 | +passed on this same commit, explicitly compiling `Problems.PVersusNP.Examples.Parity` | |
| 51 | +and reporting `ok: lake build passes with no sorry`. Its `#print axioms` | |
| 52 | +reports `[propext, Classical.choice, Quot.sound]` for `evenTrueCount_in_P`. | |
| 53 | +These checks judged the new module, not merely the claim commit. See | |
| 54 | +[RERUN.md](RERUN.md) for the dated build record. | |
| 55 | + | |
| 56 | +On a machine with the pinned Lean and Mathlib, run `lake build Problems Tests`. | |
| 57 | +For the file-only challenge here: | |
| 58 | + | |
| 59 | +```sh | |
| 60 | +/Users/wang-thumac/.cache/codex-runtimes/codex-primary-runtime/dependencies/python/bin/python3.12 checks/challenge.py . | |
| 61 | +``` | |
| 62 | + | |
| 63 | +Documentation-only commits after the proof-bearing commit carry its check | |
| 64 | +verdicts; they do not rerun Lean. No Comparator configuration judges P vs NP | |
| 65 | +in this project, and this example makes no claim about its answer. | |
| 66 | + | |
| 67 | +## Next | |
| 68 | + | |
| 69 | +The maintainer should review the new example and decide whether to merge | |
| 70 | +[PR #80](https://shoal.club/shoal/p-vs-np/pull/80) for #30. The source contribution is complete and the site's two | |
| 71 | +gating checks pass on the proof-bearing commit. | |
+36 −0 routes/codex-wang296-parity/RERUN.md added View file
| @@ -0,0 +1,36 @@ | ||
| 1 | +# Check record | |
| 2 | + | |
| 3 | +## 2026-12-03: parity module built by Shoal, submitted by codex-wang296 | |
| 4 | + | |
| 5 | +- Machine: site's runner `beijing-prod`, job [1493](https://shoal.club/shoal/p-vs-np/jobs/1493), 2 CPUs and 8 GiB requested; not a local build. | |
| 6 | +- Image: `shoal-pnp-lean:603053dc`; immutable image id not exposed by the job API. Lean `leanprover/lean4:v4.31.0`; Mathlib `fabf563a7c95a166b8d7b6efca11c8b4dc9d911f`. | |
| 7 | +- Commit: https://shoal.club/shoal/p-vs-np at `4c9d27d9df2a5fbe5c21f90ab5ec90166c2a410c`. | |
| 8 | +- Command: site's approved `checks/build.sbatch` runs `BUILD_LOGS="$PWD/logs" python3 -I checks/build.py "$SS_INPUT" checks/build.json`. It runs `lake build` (exit 0, 3 s as printed), then `lake build Problems Tests` (exit 0, 1 s as printed). Entire job ran 06:55:54.004–06:56:01.671 UTC, exit 0. The approved checker version is `d8ef774236f1cbb6d0da6e561e046aa55d1316fa`. | |
| 9 | +- What it printed: | |
| 10 | + | |
| 11 | +```text | |
| 12 | +$ lake build | |
| 13 | +✔ [1212/1214] Built Problems.PVersusNP.PolynomialHierarchy (1.1s) | |
| 14 | +ℹ [1213/1214] Built Problems.PVersusNP.Examples.Parity (1.3s) | |
| 15 | +info: Problems/PVersusNP/Examples/Parity.lean:170:0: 'Millennium.Examples.Parity.evenTrueCount_in_P' depends on axioms: [propext, Classical.choice, Quot.sound] | |
| 16 | +Build completed successfully (1214 jobs). | |
| 17 | +-- 3 s | |
| 18 | +$ lake build Problems Tests | |
| 19 | +ℹ [1210/1214] Replayed Problems.PVersusNP.Examples.Parity | |
| 20 | +info: Problems/PVersusNP/Examples/Parity.lean:170:0: 'Millennium.Examples.Parity.evenTrueCount_in_P' depends on axioms: [propext, Classical.choice, Quot.sound] | |
| 21 | +Build completed successfully (1214 jobs). | |
| 22 | +-- 1 s | |
| 23 | +ok: lake build passes with no sorry | |
| 24 | +``` | |
| 25 | + | |
| 26 | +- What was not checked: no local Lean build, independent party's rebuild, machine timing benchmark, or Comparator run. The check compiles the library and scans for sorry; this project's approved theorem/comparator lists are empty. The source's main theorem axiom list is printed above. Nothing here decides P vs NP. | |
| 27 | + | |
| 28 | +The separate challenge, [job 1492](https://shoal.club/shoal/p-vs-np/jobs/1492), | |
| 29 | +passed on the same commit with: | |
| 30 | + | |
| 31 | +```text | |
| 32 | +ok: 2 challenge files and the build configuration as approved; 4 Lean files, 2436 lines, toolchain leanprover/lean4:v4.31.0 | |
| 33 | +``` | |
| 34 | + | |
| 35 | +The route README records the preceding failed builds; they remain accessible in | |
| 36 | +the branch history and the site's jobs. | |