7598bc42 / 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
40The statement of the open problem, verbatim from [`PvsNP/Open.lean`](PvsNP/Open.lean):
41
42```lean
43/-- **P = NP**, over the binary alphabet: every language in NP is in P. -/
44def PEqNP : Prop := ∀ L : Language Bool, InNP L → InP L
45
46/-- **P ≠ NP — an open problem.** Stated, not proved. One of the Clay Millennium Prize Problems. -/
47def PNeNP : Prop := ¬ PEqNP
48```
49
50## Build
51
52You need [elan](https://github.com/leanprover/elan), which installs the Lean version pinned in
53[`lean-toolchain`](lean-toolchain) (Lean `v4.36.0`) on first use.
54
55```sh
56lake build
57```
58
59This builds the library and the axiom audit ([`Audit/Axioms.lean`](Audit/Axioms.lean)). The audit
60pins the output of `#print axioms` for every headline theorem, so the build fails if any of them
61comes to rely on `sorry` or on a new axiom. A clean build takes well under a minute.
62
63## Layout
64
65```text
66PvsNP.lean root module: imports everything
67PvsNP/
68 Basic/Finite.lean IsFinite: a list enumerates the type (core Lean has no Fintype)
69 Basic/Poly.lean polynomial bounds and their closure under +, *, composition
70 Language.lean languages, complement
71 Machine/Tape.lean tapes: Int-indexed cells, head, read / write / move
72 Machine/Defs.lean multi-tape machines, configurations, step, run, halting
73 Machine/Lift.lean running a machine on more tapes (simulation theorem)
74 Machine/Compose.lean running one machine after another (composition theorem)
75 Complexity/Time.lean deciding, computing and verifying within a time bound
76 Complexity/P.lean the class P; closure under complement
77 Complexity/Reduction.lean polynomial-time functions and reductions
78 Complexity/NP.lean the class NP; P ⊆ NP; closure under reductions
79 Complexity/Completeness.lean NP-hardness and NP-completeness
80 Open.lean the P vs NP statement (open)
81Audit/Axioms.lean build-time axiom audit
82```
83
84How the modules depend on each other:
85
86```mermaid
87graph TD
88 Finite[Basic/Finite] --> Defs[Machine/Defs]
89 Tape[Machine/Tape] --> Defs
90 Defs --> Lift[Machine/Lift]
91 Defs --> Compose[Machine/Compose]
92 Language --> Time[Complexity/Time]
93 Lift --> Time
94 Compose --> Time
95 Poly[Basic/Poly] --> P[Complexity/P]
96 Time --> P
97 P --> Reduction[Complexity/Reduction]
98 Reduction --> NP[Complexity/NP]
99 NP --> Completeness[Complexity/Completeness]
100 Completeness --> Open
101```
102
103## Modelling choices
104
105These are deliberate, and each is a place where a reader should check that the definitions mean what
106they claim:
107
108- **Finite control.** A machine's state type must be finite (`IsFinite`). Without that, one state
109 could store the whole input and every language would be decidable in one step.
110- **Tape alphabet `Option α`.** Machines write input symbols or blanks, nothing else. For alphabets with
111 at least two symbols this costs only a polynomial slowdown, and it lets machines over the same
112 alphabet run one after another with no re-encoding.
113- **Clean outputs.** A machine computing `f` must halt with `f x` on tape `0`, every other tape blank and
114 every head at `0`, which is exactly how the next machine expects its input. This makes composition
115 a literal hand-over.
116- **Verifiers in the style of Sipser.** The verifier's time bound depends on `|x|` only, and
117 `x ∈ L ↔ ∃ w, V accepts (x, w)`. A verifier running `T |x|` steps can read at most `T |x|`
118 certificate symbols, so certificates are polynomially short in effect.
119- **Output bound stated, not derived.** `PolyTimeComputable f` asks for `|f x|` to be polynomially
120 bounded. That follows from the time bound in any reasonable model; deriving it is on the roadmap.
121
122## Roadmap
123
124The milestones, in order; details and acceptance criteria are in [`ROADMAP.md`](ROADMAP.md).
125
1261. **Done:** machines, P, NP, reductions, P ⊆ NP, closure properties, NP-completeness, the open statement.
1272. Examples: concrete machines for small languages, run in the kernel.
1283. Derive the output-length bound from the time bound.
1294. Short certificates suffice: verifiers only ever need certificates of length `≤ T |x|`.
1305. Boolean formulas, CNF and an encoding into `List Bool`.
1316. **SAT ∈ NP.**
1327. **Cook–Levin, stated**: SAT is NP-complete, as a `Prop`.
1338. Cook–Levin, proved.
1349. Alphabet independence: P and NP over `Bool` match P and NP over any finite alphabet with two symbols.
135
136## License
137
138[Apache License 2.0](LICENSE).