5bdcd988 / README.md

1# pvsnp — P versus NP in Lean 4
2
3A from-scratch formalization of the basic theory behind the **P versus NP** question, in Lean 4 with
4no dependencies beyond Lean core.
5
6> **Status: P ≠ NP is open, and this project does not solve it.** The statement is written down as a
7> definition (`PvsNP.PNeNP : Prop`). Nothing here proves it or its negation, and no axiom assumes
8> either. Every theorem below has a complete proof, and the build checks that none of them depends on
9> `sorry` or on any axiom beyond Lean's standard three.
10
11## What is here
12
13The 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
25The 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
41The 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. -/
45def 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. -/
48def PNeNP : Prop := ¬ PEqNP
49```
50
51## Build
52
53You 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
57lake build
58```
59
60This builds the library and the axiom audit ([`Audit/Axioms.lean`](Audit/Axioms.lean)). The audit
61pins the output of `#print axioms` for every headline theorem, so the build fails if any of them
62comes to rely on `sorry` or on a new axiom. A clean build takes well under a minute.
63
64## Layout
65
66```text
67PvsNP.lean root module: imports everything
68PvsNP/
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
83Audit/Axioms.lean build-time axiom audit
84```
85
86How the modules depend on each other:
87
88```mermaid
89graph 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
107These are deliberate, and each is a place where a reader should check that the definitions mean what
108they claim:
109
110- **Finite control.** A machine's state type must be finite (`IsFinite`). Without that, one state
111 could store the whole input and every language would be decidable in one step.
112- **Tape alphabet `Option α`.** Machines write input symbols or blanks, nothing else. For alphabets with
113 at least two symbols this costs only a polynomial slowdown, and it lets machines over the same
114 alphabet run one after another with no re-encoding.
115- **Clean outputs.** A machine computing `f` must halt with `f x` on tape `0`, every other tape blank and
116 every head at `0`, which is exactly how the next machine expects its input. This makes composition
117 a literal hand-over.
118- **Verifiers in the style of Sipser.** The verifier's time bound depends on `|x|` only, and
119 `x ∈ L ↔ ∃ w, V accepts (x, w)`. A verifier running `T |x|` steps can read at most `T |x|`
120 certificate symbols, so certificates are polynomially short in effect.
121- **Output bound stated, not derived.** `PolyTimeComputable f` asks for `|f x|` to be polynomially
122 bounded. That follows from the time bound in any reasonable model; deriving it is on the roadmap.
123
124## Roadmap
125
126The milestones, in order; details and acceptance criteria are in [`ROADMAP.md`](ROADMAP.md).
127
1281. **Done:** machines, P, NP, reductions, P ⊆ NP, closure properties, NP-completeness, the open statement.
1292. **Done:** examples: concrete machines for small languages, run in the kernel.
1303. Derive the output-length bound from the time bound.
1314. Short certificates suffice: verifiers only ever need certificates of length `≤ T |x|`.
1325. Boolean formulas, CNF and an encoding into `List Bool`.
1336. **SAT ∈ NP.**
1347. **Cook–Levin, stated**: SAT is NP-complete, as a `Prop`.
1358. Cook–Levin, proved.
1369. Alphabet independence: P and NP over `Bool` match P and NP over any finite alphabet with two symbols.
137
138## License
139
140[Apache License 2.0](LICENSE).