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

| 1 | import Mathlib.Computability.TuringMachine.StackTuringMachine |
| 2 | import Mathlib.Computability.Primrec.List |
| 3 | import Mathlib.Computability.TuringMachine.Computable |
| 4 | import Mathlib.Computability.Encoding |
| 5 | import Mathlib.Data.Finset.Basic |
| 6 | import Mathlib.Data.Set.Basic |
| 7 | import Mathlib.Order.Basic |
| 8 | import Init.Data.List.Lemmas |
| 9 | import Problems.PVersusNP.Millennium |
| 10 | |
| 11 | /-! |
| 12 | # The Polynomial Hierarchy |
| 13 | |
| 14 | This file defines the polynomial hierarchy (PH), a hierarchy of complexity classes extending P and NP. |
| 15 | We use the standard "quantifier" characterization: each level is obtained by adding a polynomially |
| 16 | bounded existential witness in front of a predicate from the previous co-level. |
| 17 | |
| 18 | ## Overview |
| 19 | |
| 20 | The hierarchy is defined recursively (quantifier form): |
| 21 | - `Σ₀ᴾ = Π₀ᴾ = P` |
| 22 | - `Σₖ₊₁ᴾ` consists of languages `L` such that `a ∈ L ↔ ∃ b (|b| ≤ poly(|a|) ∧ (a,b) ∈ L')` for some `L' ∈ Πₖᴾ` |
| 23 | - `Πₖᴾ` is defined as complements of `Σₖᴾ` |
| 24 | |
| 25 | This file uses the quantifier characterization, so it omits the oracle-machine `Δₖᴾ` classes. |
| 26 | |
| 27 | ## Examples |
| 28 | |
| 29 | Examples of problems in different levels: |
| 30 | - Σ₁ᴾ (NP): SAT, Hamiltonian Path, Clique |
| 31 | - Π₁ᴾ (co-NP): TAUTOLOGY, No-Hamiltonian Path |
| 32 | - Σ₂ᴾ: MINSAT (minimizing the number of satisfied clauses) |
| 33 | - Π₂ᴾ: FORALL-EXISTS-SAT (formulas of the form ∀x∃y.φ(x,y)) |
| 34 | |
| 35 | ## Collapse |
| 36 | |
| 37 | If P = NP, then the entire polynomial hierarchy collapses to P. Many complexity theorists believe |
| 38 | the hierarchy is strict, meaning Σᵏᴾ ⊂ Σᵏ⁺¹ᴾ for all k. |
| 39 | -/ |
| 40 | |
| 41 | namespace Millennium |
| 42 | |
| 43 | open Turing |
| 44 | open Computability |
| 45 | |
| 46 | /- |
| 47 | We'll represent the polynomial hierarchy using alternating quantifiers. |
| 48 | First, we define the complement of a language. |
| 49 | -/ |
| 50 | /-- |
| 51 | The complement of a language L is the language consisting of |
| 52 | all the strings that are not in L. |
| 53 | |
| 54 | For example, if SAT is the set of satisfiable Boolean formulas, |
| 55 | then co-SAT is the set of unsatisfiable Boolean formulas. |
| 56 | -/ |
| 57 | def complement {α : Type} [Primcodable α] (L : Language α) : Language α := |
| 58 | fun a => ¬(L a) |
| 59 | |
| 60 | /-- |
| 61 | `Σₖᴾ` defined via polynomially-bounded alternating quantifiers (quantifier form). |
| 62 | |
| 63 | This is one standard characterization of the polynomial hierarchy; it avoids having to formalize |
| 64 | oracle machines inside the development. |
| 65 | -/ |
| 66 | def polynomial_hierarchy_sigma_level (k : ℕ) : {α : Type} → [_inst : Primcodable α] → (ea : FinEncoding α) → Language α → Prop |
| 67 | | α, _inst, ea, L => |
| 68 | match k with |
| 69 | | 0 => InPolynomialTime ea L |
| 70 | | k+1 => |
| 71 | -- The witness `b` ranges over *all* finite strings over a finite alphabet `β` (identity |
| 72 | -- encoding `fin_encoding_string β`), as in `InNondeterministicPolynomialTime`. |
| 73 | ∃ (β : Type) (_instβ : Fintype β) (_primβ : Primcodable β) (L' : Language (α × List β)), |
| 74 | -- `L'` is in `Πₖᴾ`, i.e. its complement is in `Σₖᴾ`. |
| 75 | polynomial_hierarchy_sigma_level k (pair_encoding ea (fin_encoding_string β)) (complement L') ∧ |
| 76 | -- Witness size is polynomially bounded. |
| 77 | ∃ (p : Polynomial ℕ), |
| 78 | ∀ a, L a ↔ ∃ b : List β, b.length ≤ Polynomial.eval (ea.encode a).length p ∧ L' (a, b) |
| 79 | |
| 80 | /-- |
| 81 | `Πₖᴾ` is defined as complements of `Σₖᴾ`. |
| 82 | -/ |
| 83 | def polynomial_hierarchy_pi_level (k : ℕ) : {α : Type} → [_inst : Primcodable α] → (ea : FinEncoding α) → Language α → Prop |
| 84 | | _α, _inst, ea, L => polynomial_hierarchy_sigma_level k ea (complement L) |
| 85 | |
| 86 | /-- |
| 87 | Σ₀ᴾ = Π₀ᴾ = P |
| 88 | |
| 89 | The base level of the polynomial hierarchy is P, the class of |
| 90 | problems solvable in polynomial time. |
| 91 | -/ |
| 92 | def polynomial_hierarchy_sigma_level_zero {α : Type} [Primcodable α] (ea : FinEncoding α) : Language α → Prop := |
| 93 | polynomial_hierarchy_sigma_level 0 ea |
| 94 | |
| 95 | /-- `Π₀ᴾ = P` (since complements do not add power at level `0`). -/ |
| 96 | def polynomial_hierarchy_pi_level_zero {α : Type} [Primcodable α] (ea : FinEncoding α) : Language α → Prop := |
| 97 | polynomial_hierarchy_pi_level 0 ea |
| 98 | |
| 99 | /-- |
| 100 | Σ₁ᴾ = NP |
| 101 | |
| 102 | The first level of the existential hierarchy is NP, which consists of |
| 103 | problems verifiable in polynomial time with a suitable certificate. |
| 104 | |
| 105 | Examples: SAT, Hamiltonian Path, Clique problem |
| 106 | -/ |
| 107 | def polynomial_hierarchy_sigma_level_one {α : Type} [Primcodable α] (ea : FinEncoding α) : Language α → Prop := |
| 108 | polynomial_hierarchy_sigma_level 1 ea |
| 109 | |
| 110 | /-- |
| 111 | Π₁ᴾ = co-NP |
| 112 | |
| 113 | The first level of the universal hierarchy is co-NP, which consists of |
| 114 | problems whose complement is in NP. |
| 115 | |
| 116 | Examples: TAUTOLOGY, No-Hamiltonian Path, No-Clique |
| 117 | -/ |
| 118 | def polynomial_hierarchy_pi_level_one {α : Type} [Primcodable α] (ea : FinEncoding α) (L : Language α) : Prop := |
| 119 | polynomial_hierarchy_pi_level 1 ea L |
| 120 | /-- |
| 121 | The polynomial hierarchy is the union of all Σₖᴾ classes. |
| 122 | |
| 123 | A problem is in PH if it is in Σₖᴾ for some k. |
| 124 | |
| 125 | The polynomial hierarchy collapses if there exists some k such that |
| 126 | Σₖᴾ = Σₖ₊₁ᴾ, which would imply that for all j ≥ k, Σⱼᴾ = Σₖᴾ. |
| 127 | -/ |
| 128 | def polynomial_hierarchy {α : Type} [Primcodable α] (ea : FinEncoding α) (L : Language α) : Prop := |
| 129 | ∃ k, polynomial_hierarchy_sigma_level k ea L |
| 130 | |
| 131 | /-- |
| 132 | # The polynomial hierarchy collapse conjecture. |
| 133 | |
| 134 | This states that the polynomial hierarchy collapses to some finite level. |
| 135 | Most complexity theorists believe this is false, meaning the hierarchy |
| 136 | is infinite (each level is strictly contained in the next). |
| 137 | |
| 138 | If P = NP, then the whole hierarchy collapses to P. |
| 139 | -/ |
| 140 | def polynomial_hierarchy_collapses : Prop := |
| 141 | ∃ k, ∀ (α : Type) [Primcodable α] (ea : FinEncoding α) (L : Language α), |
| 142 | polynomial_hierarchy_sigma_level (k+1) ea L → polynomial_hierarchy_sigma_level k ea L |
| 143 | |
| 144 | /-- |
| 145 | # A common conjecture that the polynomial hierarchy does not collapse. |
| 146 | |
| 147 | This is related to the P ≠ NP conjecture. If P = NP, then the polynomial |
| 148 | hierarchy collapses to the first level (P = NP = PH). |
| 149 | -/ |
| 150 | def polynomial_hierarchy_does_not_collapse : Prop := |
| 151 | ¬polynomial_hierarchy_collapses |
| 152 | end Millennium |