# README.md @ 396d8a0df99015623a282b9dc207b52b6834db14

````
# pvsnp — P versus NP in Lean 4

A from-scratch formalization of the basic theory behind the **P versus NP** question, in Lean 4 with
no dependencies beyond Lean core.

> **Status: P ≠ NP is open, and this project does not solve it.** The statement is written down as a
> definition (`PvsNP.PNeNP : Prop`). Nothing here proves it or its negation, and no axiom assumes
> either. Every theorem below has a complete proof, and the build checks that none of them depends on
> `sorry` or on any axiom beyond Lean's standard three.

## What is here

The definitions, all in [`PvsNP/`](PvsNP):

- **Languages** over an alphabet `α`: `Language α := List α → Prop`.
- **A machine model**: deterministic multi-tape Turing machines (`Machine α k`, with `k + 1` tapes over
  `Option α`) with a *finite* set of states, and their step-by-step runs.
- **Time bounds**: `DecidesIn`, `ComputesIn` and `VerifiesIn` a time bound `T`, measured in the input
  length; `PolyBounded T` means `T n ≤ c * (n + 1) ^ d`.
- **P** (`InP`): decided in polynomial time.
- **NP** (`InNP`): verified in polynomial time, with a certificate on a second tape.
- **Polynomial-time reductions** (`L ≤ₚ L'`), **NP-hardness** and **NP-completeness**.
- **The open problem**: `PEqNP` and `PNeNP`, over the binary alphabet.

The theorems, each with a full proof:

| Result | Lean name | File |
| --- | --- | --- |
| P ⊆ NP | `InP.inNP` | [`Complexity/NP.lean`](PvsNP/Complexity/NP.lean) |
| P is closed under complement | `InP.compl` | [`Complexity/P.lean`](PvsNP/Complexity/P.lean) |
| Polynomial-time functions compose | `PolyTimeComputable.comp` | [`Complexity/Reduction.lean`](PvsNP/Complexity/Reduction.lean) |
| Reductions are reflexive and compose | `Reduces.refl`, `Reduces.trans` | [`Complexity/Reduction.lean`](PvsNP/Complexity/Reduction.lean) |
| P is closed under reductions | `InP.of_reduces` | [`Complexity/Reduction.lean`](PvsNP/Complexity/Reduction.lean) |
| NP is closed under reductions | `InNP.of_reduces` | [`Complexity/NP.lean`](PvsNP/Complexity/NP.lean) |
| An NP-hard language in P puts NP in P | `NPHard.all_inP` | [`Complexity/Completeness.lean`](PvsNP/Complexity/Completeness.lean) |
| For NP-complete `L`: P ≠ NP ↔ `L ∉ P` | `NPComplete.pNeNP_iff` | [`Open.lean`](PvsNP/Open.lean) |
| Running a machine on more tapes is a faithful simulation | `Machine.run_lift` | [`Machine/Lift.lean`](PvsNP/Machine/Lift.lean) |
| Running two machines in sequence | `Machine.run_seq` | [`Machine/Compose.lean`](PvsNP/Machine/Compose.lean) |
| A concrete one-step machine: first-symbol languages are in P | `Examples.firstSymbol_inP` | [`Examples/FirstSymbol.lean`](PvsNP/Examples/FirstSymbol.lean) |

The statement of the open problem, verbatim from [`PvsNP/Open.lean`](PvsNP/Open.lean):

```lean
/-- **P = NP**, over the binary alphabet: every language in NP is in P. -/
def PEqNP : Prop := ∀ L : Language Bool, InNP L → InP L

/-- **P ≠ NP — an open problem.** Stated, not proved. One of the Clay Millennium Prize Problems. -/
def PNeNP : Prop := ¬ PEqNP
```

## Build

You need [elan](https://github.com/leanprover/elan), which installs the Lean version pinned in
[`lean-toolchain`](lean-toolchain) (Lean `v4.36.0`) on first use.

```sh
lake build
```

This builds the library and the axiom audit ([`Audit/Axioms.lean`](Audit/Axioms.lean)). The audit
pins the output of `#print axioms` for every headline theorem, so the build fails if any of them
comes to rely on `sorry` or on a new axiom. A clean build takes well under a minute.

## Layout

```text
PvsNP.lean                  root module: imports everything
PvsNP/
  Basic/Finite.lean         IsFinite: a list enumerates the type (core Lean has no Fintype)
  Basic/Poly.lean           polynomial bounds and their closure under +, *, composition
  Language.lean             languages, complement
  Machine/Tape.lean         tapes: Int-indexed cells, head, read / write / move
  Machine/Defs.lean         multi-tape machines, configurations, step, run, halting
  Machine/Lift.lean         running a machine on more tapes (simulation theorem)
  Machine/Compose.lean      running one machine after another (composition theorem)
  Complexity/Time.lean      deciding, computing and verifying within a time bound
  Complexity/P.lean         the class P; closure under complement
  Complexity/Reduction.lean polynomial-time functions and reductions
  Complexity/NP.lean        the class NP; P ⊆ NP; closure under reductions
  Complexity/Completeness.lean  NP-hardness and NP-completeness
  Open.lean                 the P vs NP statement (open)
  Examples/FirstSymbol.lean concrete machines: languages decided by the first symbol
Audit/Axioms.lean           build-time axiom audit
```

How the modules depend on each other:

```mermaid
graph TD
  Finite[Basic/Finite] --> Defs[Machine/Defs]
  Tape[Machine/Tape] --> Defs
  Defs --> Lift[Machine/Lift]
  Defs --> Compose[Machine/Compose]
  Language --> Time[Complexity/Time]
  Lift --> Time
  Compose --> Time
  Poly[Basic/Poly] --> P[Complexity/P]
  Time --> P
  P --> Reduction[Complexity/Reduction]
  Reduction --> NP[Complexity/NP]
  NP --> Completeness[Complexity/Completeness]
  Completeness --> Open
```

## Modelling choices

These are deliberate, and each is a place where a reader should check that the definitions mean what
they claim:

- **Finite control.** A machine's state type must be finite (`IsFinite`). Without that, one state
  could store the whole input and every language would be decidable in one step.
- **Tape alphabet `Option α`.** Machines write input symbols or blanks, nothing else. For alphabets with
  at least two symbols this costs only a polynomial slowdown, and it lets machines over the same
  alphabet run one after another with no re-encoding.
- **Clean outputs.** A machine computing `f` must halt with `f x` on tape `0`, every other tape blank and
  every head at `0`, which is exactly how the next machine expects its input. This makes composition
  a literal hand-over.
- **Verifiers in the style of Sipser.** The verifier's time bound depends on `|x|` only, and
  `x ∈ L ↔ ∃ w, V accepts (x, w)`. A verifier running `T |x|` steps can read at most `T |x|`
  certificate symbols, so certificates are polynomially short in effect.
- **Output bound stated, not derived.** `PolyTimeComputable f` asks for `|f x|` to be polynomially
  bounded. That follows from the time bound in any reasonable model; deriving it is on the roadmap.

## Roadmap

The milestones, in order; details and acceptance criteria are in [`ROADMAP.md`](ROADMAP.md).

1. **Done:** machines, P, NP, reductions, P ⊆ NP, closure properties, NP-completeness, the open statement.
2. **Done:** examples: concrete machines for small languages, run in the kernel.
3. Derive the output-length bound from the time bound.
4. Short certificates suffice: verifiers only ever need certificates of length `≤ T |x|`.
5. Boolean formulas, CNF and an encoding into `List Bool`.
6. **SAT ∈ NP.**
7. **Cook–Levin, stated**: SAT is NP-complete, as a `Prop`.
8. Cook–Levin, proved.
9. Alphabet independence: P and NP over `Bool` match P and NP over any finite alphabet with two symbols.

## License

[Apache License 2.0](LICENSE).

````

API: `https://git.neoopensource.com/v1/repos/pvsnp/blob?rev=396d8a0df99015623a282b9dc207b52b6834db14&path=README.md`
