| 1 | # pvsnp — P versus NP in Lean 4 |
| 2 | |
| 3 | A from-scratch formalization of the basic theory behind the **P versus NP** question, in Lean 4 with |
| 4 | no dependencies beyond Lean core. |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | ## What is here |
| 12 | |
| 13 | The definitions, all in [`PvsNP/`](PvsNP): |
| 14 | |
| 15 | - **Languages** over an alphabet `α`: `Language α := List α → Prop`. |
| 16 | - **A machine model**: deterministic multi-tape Turing machines (`Machine α k`, with `k + 1` tapes over |
| 17 | `Option α`) with a *finite* set of states, and their step-by-step runs. |
| 18 | - **Time bounds**: `DecidesIn`, `ComputesIn` and `VerifiesIn` a time bound `T`, measured in the input |
| 19 | length; `PolyBounded T` means `T n ≤ c * (n + 1) ^ d`. |
| 20 | - **P** (`InP`): decided in polynomial time. |
| 21 | - **NP** (`InNP`): verified in polynomial time, with a certificate on a second tape. |
| 22 | - **Polynomial-time reductions** (`L ≤ₚ L'`), **NP-hardness** and **NP-completeness**. |
| 23 | - **The open problem**: `PEqNP` and `PNeNP`, over the binary alphabet. |
| 24 | |
| 25 | The theorems, each with a full proof: |
| 26 | |
| 27 | | Result | Lean name | File | |
| 28 | | --- | --- | --- | |
| 29 | | P ⊆ NP | `InP.inNP` | [`Complexity/NP.lean`](PvsNP/Complexity/NP.lean) | |
| 30 | | P is closed under complement | `InP.compl` | [`Complexity/P.lean`](PvsNP/Complexity/P.lean) | |
| 31 | | Polynomial-time functions compose | `PolyTimeComputable.comp` | [`Complexity/Reduction.lean`](PvsNP/Complexity/Reduction.lean) | |
| 32 | | Reductions are reflexive and compose | `Reduces.refl`, `Reduces.trans` | [`Complexity/Reduction.lean`](PvsNP/Complexity/Reduction.lean) | |
| 33 | | P is closed under reductions | `InP.of_reduces` | [`Complexity/Reduction.lean`](PvsNP/Complexity/Reduction.lean) | |
| 34 | | NP is closed under reductions | `InNP.of_reduces` | [`Complexity/NP.lean`](PvsNP/Complexity/NP.lean) | |
| 35 | | An NP-hard language in P puts NP in P | `NPHard.all_inP` | [`Complexity/Completeness.lean`](PvsNP/Complexity/Completeness.lean) | |
| 36 | | For NP-complete `L`: P ≠ NP ↔ `L ∉ P` | `NPComplete.pNeNP_iff` | [`Open.lean`](PvsNP/Open.lean) | |
| 37 | | Running a machine on more tapes is a faithful simulation | `Machine.run_lift` | [`Machine/Lift.lean`](PvsNP/Machine/Lift.lean) | |
| 38 | | Running two machines in sequence | `Machine.run_seq` | [`Machine/Compose.lean`](PvsNP/Machine/Compose.lean) | |
| 39 | | A concrete one-step machine: first-symbol languages are in P | `Examples.firstSymbol_inP` | [`Examples/FirstSymbol.lean`](PvsNP/Examples/FirstSymbol.lean) | |
| 40 | |
| 41 | The statement of the open problem, verbatim from [`PvsNP/Open.lean`](PvsNP/Open.lean): |
| 42 | |
| 43 | ```lean |
| 44 | /-- **P = NP**, over the binary alphabet: every language in NP is in P. -/ |
| 45 | def PEqNP : Prop := ∀ L : Language Bool, InNP L → InP L |
| 46 | |
| 47 | /-- **P ≠ NP — an open problem.** Stated, not proved. One of the Clay Millennium Prize Problems. -/ |
| 48 | def PNeNP : Prop := ¬ PEqNP |
| 49 | ``` |
| 50 | |
| 51 | ## Build |
| 52 | |
| 53 | You need [elan](https://github.com/leanprover/elan), which installs the Lean version pinned in |
| 54 | [`lean-toolchain`](lean-toolchain) (Lean `v4.36.0`) on first use. |
| 55 | |
| 56 | ```sh |
| 57 | lake build |
| 58 | ``` |
| 59 | |
| 60 | This builds the library and the axiom audit ([`Audit/Axioms.lean`](Audit/Axioms.lean)). The audit |
| 61 | pins the output of `#print axioms` for every headline theorem, so the build fails if any of them |
| 62 | comes to rely on `sorry` or on a new axiom. A clean build takes well under a minute. |
| 63 | |
| 64 | ## Layout |
| 65 | |
| 66 | ```text |
| 67 | PvsNP.lean root module: imports everything |
| 68 | PvsNP/ |
| 69 | Basic/Finite.lean IsFinite: a list enumerates the type (core Lean has no Fintype) |
| 70 | Basic/Poly.lean polynomial bounds and their closure under +, *, composition |
| 71 | Language.lean languages, complement |
| 72 | Machine/Tape.lean tapes: Int-indexed cells, head, read / write / move |
| 73 | Machine/Defs.lean multi-tape machines, configurations, step, run, halting |
| 74 | Machine/Lift.lean running a machine on more tapes (simulation theorem) |
| 75 | Machine/Compose.lean running one machine after another (composition theorem) |
| 76 | Complexity/Time.lean deciding, computing and verifying within a time bound |
| 77 | Complexity/P.lean the class P; closure under complement |
| 78 | Complexity/Reduction.lean polynomial-time functions and reductions |
| 79 | Complexity/NP.lean the class NP; P ⊆ NP; closure under reductions |
| 80 | Complexity/Completeness.lean NP-hardness and NP-completeness |
| 81 | Open.lean the P vs NP statement (open) |
| 82 | Examples/FirstSymbol.lean concrete machines: languages decided by the first symbol |
| 83 | Audit/Axioms.lean build-time axiom audit |
| 84 | ``` |
| 85 | |
| 86 | How the modules depend on each other: |
| 87 | |
| 88 | ```mermaid |
| 89 | graph TD |
| 90 | Finite[Basic/Finite] --> Defs[Machine/Defs] |
| 91 | Tape[Machine/Tape] --> Defs |
| 92 | Defs --> Lift[Machine/Lift] |
| 93 | Defs --> Compose[Machine/Compose] |
| 94 | Language --> Time[Complexity/Time] |
| 95 | Lift --> Time |
| 96 | Compose --> Time |
| 97 | Poly[Basic/Poly] --> P[Complexity/P] |
| 98 | Time --> P |
| 99 | P --> Reduction[Complexity/Reduction] |
| 100 | Reduction --> NP[Complexity/NP] |
| 101 | NP --> Completeness[Complexity/Completeness] |
| 102 | Completeness --> Open |
| 103 | ``` |
| 104 | |
| 105 | ## Modelling choices |
| 106 | |
| 107 | These are deliberate, and each is a place where a reader should check that the definitions mean what |
| 108 | they claim: |
| 109 | |
| 110 | - **Finite control, finite alphabet.** A machine's state type must be finite (`IsFinite`): otherwise |
| 111 | it could read its input into its state and then apply any predicate at all. P, NP and |
| 112 | polynomial-time functions also require a finite alphabet: over `Nat`, a machine reading a single |
| 113 | symbol could test membership in the halting set. |
| 114 | - **Tape alphabet `Option α`.** Machines write input symbols or blanks, nothing else. For alphabets with |
| 115 | at least two symbols this costs only a polynomial slowdown, and it lets machines over the same |
| 116 | alphabet run one after another with no re-encoding. |
| 117 | - **Clean outputs.** A machine computing `f` must halt with `f x` on tape `0`, every other tape blank and |
| 118 | every head at `0`, which is exactly how the next machine expects its input. This makes composition |
| 119 | a literal hand-over. |
| 120 | - **Verifiers in the style of Sipser.** The verifier's time bound depends on `|x|` only, and |
| 121 | `x ∈ L ↔ ∃ w, V accepts (x, w)`. A verifier running `T |x|` steps can read at most `T |x|` |
| 122 | certificate symbols, so certificates are polynomially short in effect. Certificates use the input |
| 123 | alphabet: the usual NP over a finite alphabet of two or more symbols, while over a one-symbol |
| 124 | alphabet certificates are unary rather than binary. |
| 125 | - **Output bound stated, not derived.** `PolyTimeComputable f` asks for `|f x|` to be polynomially |
| 126 | bounded. That follows from the time bound in any reasonable model; deriving it is on the roadmap. |
| 127 | |
| 128 | ## Roadmap |
| 129 | |
| 130 | The milestones, in order; details and acceptance criteria are in [`ROADMAP.md`](ROADMAP.md). |
| 131 | |
| 132 | 1. **Done:** machines, P, NP, reductions, P ⊆ NP, closure properties, NP-completeness, the open statement. |
| 133 | 2. **Done:** examples: concrete machines for small languages, run in the kernel. |
| 134 | 3. Derive the output-length bound from the time bound. |
| 135 | 4. Short certificates suffice: verifiers only ever need certificates of length `≤ T |x|`. |
| 136 | 5. Boolean formulas, CNF and an encoding into `List Bool`. |
| 137 | 6. **SAT ∈ NP.** |
| 138 | 7. **Cook–Levin, stated**: SAT is NP-complete, as a `Prop`. |
| 139 | 8. Cook–Levin, proved. |
| 140 | 9. Alphabet independence: P and NP over `Bool` match P and NP over any finite alphabet with two symbols. |
| 141 | |
| 142 | ## License |
| 143 | |
| 144 | [Apache License 2.0](LICENSE). |