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

Merge pull request #66 from keyan-xiaozhushou/p1-statement-guide
Browse filesdocs: add a reader guide to the P versus NP statement
+107 −0 Problems/PVersusNP/GUIDE.md added View file
| @@ -0,0 +1,107 @@ | ||
| 1 | +# A reader’s guide to the P versus NP statement | |
| 2 | + | |
| 3 | +Read this guide beside [`Millennium.lean`](Millennium.lean) and Stephen Cook’s | |
| 4 | +[*The P versus NP Problem*](https://www.claymath.org/wp-content/uploads/2022/06/pvsnp.pdf). | |
| 5 | +The line numbers refer to declarations in `Millennium.lean`. The page numbers | |
| 6 | +refer to Cook’s printed pages. Cook describes a one-tape machine in his appendix. | |
| 7 | +Lean uses Mathlib’s `TM2ComputableInPolyTime`. The links below locate the | |
| 8 | +corresponding definitions for a side-by-side reading. | |
| 9 | + | |
| 10 | +## From languages to the two targets | |
| 11 | + | |
| 12 | +| Lean declaration | Cook’s text and how to read the declaration | | |
| 13 | +| --- | --- | | |
| 14 | +| [`Language`, L77](Millennium.lean#L77) | §1, p. 1, “a language over Σ is a subset L of Σ∗.” Lean represents a subset by a predicate `α → Prop`. For strings, `α` is `List alphabet`. [`fin_encoding_string`, L217](Millennium.lean#L217) uses the list itself as its encoding. | | |
| 15 | +| [`InPolynomialTime`, L92](Millennium.lean#L92) | §1, pp. 1–2, the definition of P uses a machine that “runs in polynomial time.” Lean asks for a Boolean decider with polynomial-time machine evidence and requires its output to be true exactly on members of the language. | | |
| 16 | +| [`pair_encoding`, L158](Millennium.lean#L158) | §1, p. 2, Cook writes “Lᵣ = {w#y \| R(w, y)}.” The Lean encoding tags the left and right symbols and places a distinct separator between them. [`pair_encoding.encode_eq`, L180](Millennium.lean#L180) shows the resulting word. | | |
| 17 | +| [`PolynomialTimeCheckingRelation`, L269](Millennium.lean#L269) | §1, p. 2, “We say that R is polynomial-time iff Lᵣ ∈ P.” Lean applies `InPolynomialTime` to the predicate `R p.1 p.2` on pairs under `pair_encoding`. | | |
| 18 | +| [`InNondeterministicPolynomialTime`, L286](Millennium.lean#L286) | §1, p. 2, Cook gives “w ∈ L ⟺ ∃y(\|y\| ≤ \|w\|ᵏ and R(w, y)).” Lean quantifies over a finite certificate alphabet, a checking relation and an exponent. Certificates are lists over that alphabet, and the input length is the length of its encoding. | | |
| 19 | +| [`PolynomialTimeReducible`, L297](Millennium.lean#L297) | §2, p. 4, Definition 3 asks for a “polynomial-time computable function” with “x ∈ L₁ ⟺ f(x) ∈ L₂.” Lean supplies machine evidence for `f` between the two encodings and states that membership equivalence for every source input. | | |
| 20 | +| [`NondeterministicPolynomialTimeComplete`, L331](Millennium.lean#L331) | §2, p. 4, Definition 4 says “L is in NP, and L′ ≤ₚ L for every language L′ in NP.” The two conjuncts in Lean express membership in NP and reducibility from every NP language, with its own encoded input type. | | |
| 21 | +| [`Millennium.ClayPVersusNP`, L1547](Millennium.lean#L1547) | §1, p. 2, “Does P = NP?” The positive proposition unfolds to [`ClayPVersusNP.Formulations.ClassEquality`, L1535](Millennium.lean#L1535). For every language over a finite alphabet with at least two symbols, being in P is equivalent to being in NP. | | |
| 22 | +| [`Millennium.ClayPVersusNP.Formulations.NegativeBranch`, L1860](Millennium.lean#L1860) | §3, p. 7, “Suppose, on the other hand, that P ≠ NP, and consider how one might prove it.” The negative proposition is the negation of the positive proposition above. | | |
| 23 | + | |
| 24 | +Cook poses the equality in §1 and then supplies the definitions of reduction and | |
| 25 | +NP-completeness in §2. His opening sentence in §1 points to the inclusion | |
| 26 | +`NP ⊆ P`, expressed at | |
| 27 | +[`ClayPVersusNP.Formulations.HardDirection`, L1379](Millennium.lean#L1379). | |
| 28 | +In Lean, the equivalence between that inclusion and literal class equality uses | |
| 29 | +the composition hypothesis described below. | |
| 30 | + | |
| 31 | +## One verified machine for the left projection | |
| 32 | + | |
| 33 | +Lines [495–1370](Millennium.lean#L495-L1370) build the finite machine that | |
| 34 | +returns `w` on a canonical input `w#y` whose two strings use the same finite | |
| 35 | +alphabet. Its transition table copies the left symbols onto a work stack until | |
| 36 | +it sees the separator, discards the right block, and drains the work stack to | |
| 37 | +the output stack. The two stack transfers restore the original order. The | |
| 38 | +one-step proofs are assembled into an evaluation bounded by | |
| 39 | +`4 * w.length + y.length + 4` at | |
| 40 | +[`string_projection_evals_canonical`, L1127](Millennium.lean#L1127). | |
| 41 | +The length of the tagged input is `w.length + 1 + y.length`, so | |
| 42 | +[`LeftProjection.linear_time_correctness`, L1349](Millennium.lean#L1349) | |
| 43 | +supplies the bound `4n + 4` in the encoded input length `n`. | |
| 44 | +Cook’s §1 proof of `P ⊆ NP` uses the relation `R(w, y) ⟺ w ∈ L`. | |
| 45 | +The projection supplies the first machine in that construction. A decider | |
| 46 | +for `L` supplies the second. | |
| 47 | + | |
| 48 | +## The 23 theorems that take polynomial-time composition | |
| 49 | + | |
| 50 | +[`ClayPVersusNP.Support.PolynomialTimeComputableComposition`, L308](Millennium.lean#L308) | |
| 51 | +asks for machine evidence for `g ∘ f` from polynomial-time machine evidence | |
| 52 | +for `f` and `g` with a common intermediate encoding. Each of the following | |
| 53 | +theorem signatures explicitly takes that proposition. | |
| 54 | + | |
| 55 | +| Declaration within `Millennium` | What it gives | | |
| 56 | +| --- | --- | | |
| 57 | +| [`PolynomialTimeReducible.source_in_p`, L339](Millennium.lean#L339) | Pulls a P decider back along a reduction (Cook, Proposition 1(a)). | | |
| 58 | +| [`PolynomialTimeReducible.trans`, L358](Millennium.lean#L358) | Composes two polynomial-time many-one reductions. | | |
| 59 | +| [`NondeterministicPolynomialTimeComplete.transfer`, L375](Millennium.lean#L375) | Transfers NP-completeness along a reduction to an NP language (Proposition 1(b)). | | |
| 60 | +| [`ClayPVersusNP.Support.DeciderAsVerifier.of_projection`, L1471](Millennium.lean#L1471) | Lifts a P decider to a verifier given a polynomial-time left projection. | | |
| 61 | +| [`ClayPVersusNP.Support.DeciderAsVerifier.of_left_projection_machine`, L1491](Millennium.lean#L1491) | Lifts a P decider using the verified left-projection machine. | | |
| 62 | +| [`PolynomialTimeContainedInNondeterministicPolynomialTime.of_turing_machine_composition`, L1522](Millennium.lean#L1522) | Derives P ⊆ NP via the certificate-ignoring verifier. | | |
| 63 | +| [`ClayPVersusNP.Formulations.ClassEquality.of_hard_direction_and_projection`, L1635](Millennium.lean#L1635) | Gets class equality from the hard direction and a supplied polynomial-time projection. | | |
| 64 | +| [`ClayPVersusNP.Formulations.ClassEquality.of_hard_direction_and_turing_machine_composition`, L1646](Millennium.lean#L1646) | Gets class equality from the hard direction using the verified projection. | | |
| 65 | +| [`ClayPVersusNP.Formulations.ClassEquality.iff_hard_direction`, L1655](Millennium.lean#L1655) | Equates class equality with the hard direction. | | |
| 66 | +| [`ClayPVersusNP.iff_hard_direction`, L1663](Millennium.lean#L1663) | Equates the positive target with the hard direction. | | |
| 67 | +| [`ClayPVersusNP.Formulations.HardDirection.polynomial_time_equals_nondeterministic_polynomial_time`, L1672](Millennium.lean#L1672) | Derives the positive target from the hard direction. | | |
| 68 | +| [`ClayPVersusNP.iff_nondeterministic_polynomial_time_simulation`, L1678](Millennium.lean#L1678) | Equates the positive target with deterministic simulation. | | |
| 69 | +| [`ClayPVersusNP.Formulations.DeterministicSimulation.polynomial_time_equals_nondeterministic_polynomial_time`, L1688](Millennium.lean#L1688) | Derives the positive target from deterministic simulation. | | |
| 70 | +| [`ClayPVersusNP.iff_checkable`, L1699](Millennium.lean#L1699) | Equates the positive target with checkability implying solvability. | | |
| 71 | +| [`ClayPVersusNP.Formulations.CheckableImpliesSolvable.polynomial_time_equals_nondeterministic_polynomial_time`, L1709](Millennium.lean#L1709) | Derives the positive target from checkability implying solvability. | | |
| 72 | +| [`CookNondeterministicPolynomialTimeCompleteInPolynomialTime.nondeterministic_polynomial_time_contained_in_polynomial_time`, L1720](Millennium.lean#L1720) | Derives the hard direction from an NP-complete language in P (Proposition 1(c), inclusion form). | | |
| 73 | +| [`CookNondeterministicPolynomialTimeCompleteInPolynomialTime.polynomial_time_equals_nondeterministic_polynomial_time`, L1734](Millennium.lean#L1734) | Derives class equality from an NP-complete language in P and a supplied decider-as-verifier principle. | | |
| 74 | +| [`CookNondeterministicPolynomialTimeCompleteInPolynomialTime.of_projection`, L1746](Millennium.lean#L1746) | Derives class equality from an NP-complete language in P and a supplied polynomial-time projection. | | |
| 75 | +| [`CookNondeterministicPolynomialTimeCompleteInPolynomialTime.of_turing_machine_composition`, L1760](Millennium.lean#L1760) | Derives class equality from an NP-complete language in P using the verified projection. | | |
| 76 | +| [`ClayPVersusNP.Consequences.NondeterministicPolynomialTimeCompleteInPolynomialTime.of_turing_machine_composition`, L1783](Millennium.lean#L1783) | Proves the global implication from any NP-complete language in P to class equality. | | |
| 77 | +| [`ClayPVersusNP.nondeterministic_polynomial_time_complete_in_polynomial_time`, L1790](Millennium.lean#L1790) | Derives the positive target given an NP-complete language in P. | | |
| 78 | +| [`ClayFiniteAlphabet.NondeterministicPolynomialTimeCompleteInPolynomialTime.of_turing_machine_composition`, L1822](Millennium.lean#L1822) | Proves the implication from an NP-complete language in P to class equality over a fixed alphabet. | | |
| 79 | +| [`ClayFiniteAlphabet.PolynomialTimeEqualsNondeterministicPolynomialTime.nondeterministic_polynomial_time_complete_in_polynomial_time`, L1829](Millennium.lean#L1829) | Derives class equality over a fixed alphabet given an NP-complete language in P. | | |
| 80 | + | |
| 81 | +## What the file proves without the composition hypothesis | |
| 82 | + | |
| 83 | +- The encoding equations give the canonical pair word at | |
| 84 | + [L180](Millennium.lean#L180) and its length at [L188](Millennium.lean#L188). | |
| 85 | + The projection proof has its own machine transitions and runtime bound | |
| 86 | + at [L495–1370](Millennium.lean#L495-L1370). | |
| 87 | + [`LeftProjection.polynomial_time_computable.of_linear_time`, L1455](Millennium.lean#L1455) | |
| 88 | + turns the linear-time evidence supplied at L1349 into a polynomial-time | |
| 89 | + machine witness. | |
| 90 | +- [`ClayVerifiableLanguage.iff_in_nondeterministic_polynomial_time`, L422](Millennium.lean#L422) | |
| 91 | + repackages the same bounded-certificate data in both directions. | |
| 92 | + [`ClayFiniteAlphabet.VerifiableLanguage.iff_in_nondeterministic_polynomial_time`, L468](Millennium.lean#L468) | |
| 93 | + specializes that statement to a bundled alphabet. | |
| 94 | + The equivalence of checking with the hard direction is proved at | |
| 95 | + [L1405](Millennium.lean#L1405), and the equivalence of checking with | |
| 96 | + deterministic simulation at [L1422](Millennium.lean#L1422). | |
| 97 | +- [`ClayPVersusNP.Formulations.ClassEquality.iff_subsets`, L1576](Millennium.lean#L1576) | |
| 98 | + expresses equality as the conjunction of the two inclusions. | |
| 99 | + A proof of the positive target gives the hard direction at | |
| 100 | + [L1592](Millennium.lean#L1592) and the easy direction at | |
| 101 | + [L1616](Millennium.lean#L1616). The standalone easy inclusion at L1522 | |
| 102 | + uses composition. Its earlier helper | |
| 103 | + [`PolynomialTimeContainedInNondeterministicPolynomialTime.certificate_verifier`, L1503](Millennium.lean#L1503) | |
| 104 | + takes the verifier lift as a premise and chooses the empty certificate. | |
| 105 | +- [`NondeterministicPolynomialTimeComplete.in_nondeterministic_polynomial_time`, L1840](Millennium.lean#L1840) | |
| 106 | + and [`NondeterministicPolynomialTimeComplete.reduces`, L1849](Millennium.lean#L1849) | |
| 107 | + extract the two parts of a supplied NP-completeness proof. | |