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: `complement L` is the language `fun a => ¬ L a`. |
| 52 | |
| 53 | `Language α` is `α → Prop`, so this is the set-theoretic complement; the `Primcodable α` argument |
| 54 | is part of the signature only, and the definition does not use it. |
| 55 | |
| 56 | For example, if SAT is the set of satisfiable Boolean formulas, then co-SAT is the set of |
| 57 | unsatisfiable Boolean formulas. |
| 58 | -/ |
| 59 | def complement {α : Type} [Primcodable α] (L : Language α) : Language α := |
| 60 | fun a => ¬(L a) |
| 61 | |
| 62 | /-- |
| 63 | `Σₖᴾ` in quantifier form, on a language `L : α → Prop` with a fixed encoding `ea : FinEncoding α` |
| 64 | and `α`'s `Primcodable` instance. |
| 65 | |
| 66 | - `k = 0`: `polynomial_hierarchy_sigma_level 0 ea L` is `InPolynomialTime ea L`. |
| 67 | - `k + 1`: there are a type `β` with `Fintype` and `Primcodable` instances, a language |
| 68 | `L' : Language (α × List β)`, the predicate `polynomial_hierarchy_sigma_level k e (complement L')` |
| 69 | with `e = pair_encoding ea (fin_encoding_string β)` — that is, `L'`'s *complement* is at `Σₖᴾ` — |
| 70 | and a polynomial `p : Polynomial ℕ` such that, for every `a`, |
| 71 | `L a ↔ ∃ b : List β, b.length ≤ Polynomial.eval (ea.encode a).length p ∧ L' (a, b)`. |
| 72 | |
| 73 | The witness `b` is a finite list over `β`, encoded by `fin_encoding_string β` (the identity encoding |
| 74 | on `List β`); the bound is `p` evaluated at the encoded input length `(ea.encode a).length`, and the |
| 75 | quantifier asks for *some* polynomial `p`, not for a bound that holds for all `p`. Level `0` carries |
| 76 | no witness and no bound. |
| 77 | -/ |
| 78 | def polynomial_hierarchy_sigma_level (k : ℕ) : {α : Type} → [_inst : Primcodable α] → (ea : FinEncoding α) → Language α → Prop |
| 79 | | α, _inst, ea, L => |
| 80 | match k with |
| 81 | | 0 => InPolynomialTime ea L |
| 82 | | k+1 => |
| 83 | -- The witness `b` ranges over *all* finite strings over a finite alphabet `β` (identity |
| 84 | -- encoding `fin_encoding_string β`), as in `InNondeterministicPolynomialTime`. |
| 85 | ∃ (β : Type) (_instβ : Fintype β) (_primβ : Primcodable β) (L' : Language (α × List β)), |
| 86 | -- `L'` is in `Πₖᴾ`, i.e. its complement is in `Σₖᴾ`. |
| 87 | polynomial_hierarchy_sigma_level k (pair_encoding ea (fin_encoding_string β)) (complement L') ∧ |
| 88 | -- Witness size is polynomially bounded. |
| 89 | ∃ (p : Polynomial ℕ), |
| 90 | ∀ a, L a ↔ ∃ b : List β, b.length ≤ Polynomial.eval (ea.encode a).length p ∧ L' (a, b) |
| 91 | |
| 92 | /-- |
| 93 | `Πₖᴾ` is, by definition, `polynomial_hierarchy_sigma_level k ea (complement L)`: the witness alphabet, |
| 94 | the pair encoding `pair_encoding ea (fin_encoding_string β)` and the polynomial bound are exactly those |
| 95 | of `polynomial_hierarchy_sigma_level`, applied to the complement of `L`. |
| 96 | -/ |
| 97 | def polynomial_hierarchy_pi_level (k : ℕ) : {α : Type} → [_inst : Primcodable α] → (ea : FinEncoding α) → Language α → Prop |
| 98 | | _α, _inst, ea, L => polynomial_hierarchy_sigma_level k ea (complement L) |
| 99 | |
| 100 | /-- |
| 101 | `Σ₀ᴾ` at a fixed encoding `ea`: by definition this is `polynomial_hierarchy_sigma_level 0 ea`, which |
| 102 | is `InPolynomialTime ea`. No witness and no polynomial bound are introduced at this level. |
| 103 | -/ |
| 104 | def polynomial_hierarchy_sigma_level_zero {α : Type} [Primcodable α] (ea : FinEncoding α) : Language α → Prop := |
| 105 | polynomial_hierarchy_sigma_level 0 ea |
| 106 | |
| 107 | /-- `Π₀ᴾ` at a fixed encoding `ea`: by definition this is `polynomial_hierarchy_pi_level 0 ea`, which |
| 108 | is `InPolynomialTime ea (complement L)`. -/ |
| 109 | def polynomial_hierarchy_pi_level_zero {α : Type} [Primcodable α] (ea : FinEncoding α) : Language α → Prop := |
| 110 | polynomial_hierarchy_pi_level 0 ea |
| 111 | |
| 112 | /-- |
| 113 | `Σ₁ᴾ` at a fixed encoding `ea`: by definition `polynomial_hierarchy_sigma_level 1 ea`, i.e. one |
| 114 | polynomially bounded existential witness over a finite alphabet in front of a `Π₀ᴾ` predicate on |
| 115 | `pair_encoding ea (fin_encoding_string β)`. Whether this predicate is the statement's `NP` is S-4; |
| 116 | it is not a definitional identity here. |
| 117 | |
| 118 | Examples: SAT, Hamiltonian Path, Clique problem |
| 119 | -/ |
| 120 | def polynomial_hierarchy_sigma_level_one {α : Type} [Primcodable α] (ea : FinEncoding α) : Language α → Prop := |
| 121 | polynomial_hierarchy_sigma_level 1 ea |
| 122 | |
| 123 | /-- |
| 124 | `Π₁ᴾ` at a fixed encoding `ea`: by definition `polynomial_hierarchy_pi_level 1 ea`, i.e. |
| 125 | `polynomial_hierarchy_sigma_level 1 ea (complement L)` — the complement of a `Σ₁ᴾ` language. |
| 126 | |
| 127 | Examples of the corresponding decision problems: TAUTOLOGY, No-Hamiltonian Path, No-Clique |
| 128 | -/ |
| 129 | def polynomial_hierarchy_pi_level_one {α : Type} [Primcodable α] (ea : FinEncoding α) (L : Language α) : Prop := |
| 130 | polynomial_hierarchy_pi_level 1 ea L |
| 131 | /-- |
| 132 | The polynomial hierarchy at a fixed encoding `ea`: `∃ k : ℕ, polynomial_hierarchy_sigma_level k ea L`. |
| 133 | Membership is membership in some `Σₖᴾ` level; the `Π` levels enter only through `complement` inside |
| 134 | each level. The union is taken over every natural number `k`. |
| 135 | -/ |
| 136 | def polynomial_hierarchy {α : Type} [Primcodable α] (ea : FinEncoding α) (L : Language α) : Prop := |
| 137 | ∃ k, polynomial_hierarchy_sigma_level k ea L |
| 138 | |
| 139 | /-- |
| 140 | # The polynomial hierarchy collapse statement. |
| 141 | |
| 142 | Literally: there is a level `k` such that, for every type `α` with a `Primcodable` instance, every |
| 143 | encoding `ea : FinEncoding α` and every language `L : Language α`, the predicate |
| 144 | `polynomial_hierarchy_sigma_level (k+1) ea L` implies `polynomial_hierarchy_sigma_level k ea L`. |
| 145 | |
| 146 | The quantifier over `α`, its `Primcodable` instance and `ea` is part of the statement; the level `k` |
| 147 | is fixed inside it. Nothing in this file decides whether it holds. |
| 148 | -/ |
| 149 | def polynomial_hierarchy_collapses : Prop := |
| 150 | ∃ k, ∀ (α : Type) [Primcodable α] (ea : FinEncoding α) (L : Language α), |
| 151 | polynomial_hierarchy_sigma_level (k+1) ea L → polynomial_hierarchy_sigma_level k ea L |
| 152 | |
| 153 | /-- |
| 154 | # The negation of `polynomial_hierarchy_collapses`. |
| 155 | |
| 156 | Literally: for every `k`, some type `α` with a `Primcodable` instance, some encoding |
| 157 | `ea : FinEncoding α` and some language `L : Language α` satisfy |
| 158 | `polynomial_hierarchy_sigma_level (k+1) ea L` but not `polynomial_hierarchy_sigma_level k ea L`. |
| 159 | Nothing in this file proves or refutes it. |
| 160 | -/ |
| 161 | def polynomial_hierarchy_does_not_collapse : Prop := |
| 162 | ¬polynomial_hierarchy_collapses |
| 163 | |
| 164 | /-! ## Lemmas that hold by unfolding the definitions -/ |
| 165 | |
| 166 | /-- At level `0` the `Σ` level is exactly `InPolynomialTime`: `Σ₀ᴾ = P` read as a definitional |
| 167 | identity at the fixed encoding `ea`. -/ |
| 168 | theorem polynomial_hierarchy_sigma_level_zero_iff {α : Type} [Primcodable α] |
| 169 | (ea : FinEncoding α) (L : Language α) : |
| 170 | polynomial_hierarchy_sigma_level 0 ea L ↔ InPolynomialTime ea L := |
| 171 | Iff.rfl |
| 172 | |
| 173 | /-- The `Π` level is, by definition, the `Σ` level of the complement. -/ |
| 174 | theorem polynomial_hierarchy_pi_level_iff_sigma_complement {α : Type} [Primcodable α] |
| 175 | (k : ℕ) (ea : FinEncoding α) (L : Language α) : |
| 176 | polynomial_hierarchy_pi_level k ea L ↔ |
| 177 | polynomial_hierarchy_sigma_level k ea (complement L) := |
| 178 | Iff.rfl |
| 179 | |
| 180 | /-- A language at some `Σₖᴾ` level belongs to the polynomial hierarchy. -/ |
| 181 | theorem polynomial_hierarchy_sigma_level_mem {α : Type} [Primcodable α] |
| 182 | (k : ℕ) (ea : FinEncoding α) (L : Language α) |
| 183 | (h : polynomial_hierarchy_sigma_level k ea L) : polynomial_hierarchy ea L := |
| 184 | ⟨k, h⟩ |
| 185 | |
| 186 | end Millennium |