import Mathlib.Computability.TuringMachine.StackTuringMachine import Mathlib.Computability.Primrec.List import Mathlib.Computability.TuringMachine.Computable import Mathlib.Computability.Encoding import Mathlib.Data.Finset.Basic import Mathlib.Data.Set.Basic import Mathlib.Order.Basic import Init.Data.List.Lemmas import Problems.PVersusNP.Millennium /-! # The Polynomial Hierarchy This file defines the polynomial hierarchy (PH), a hierarchy of complexity classes extending P and NP. We use the standard "quantifier" characterization: each level is obtained by adding a polynomially bounded existential witness in front of a predicate from the previous co-level. ## Overview The hierarchy is defined recursively (quantifier form): - `Σ₀ᴾ = Π₀ᴾ = P` - `Σₖ₊₁ᴾ` consists of languages `L` such that `a ∈ L ↔ ∃ b (|b| ≤ poly(|a|) ∧ (a,b) ∈ L')` for some `L' ∈ Πₖᴾ` - `Πₖᴾ` is defined as complements of `Σₖᴾ` This file uses the quantifier characterization, so it omits the oracle-machine `Δₖᴾ` classes. ## Examples Examples of problems in different levels: - Σ₁ᴾ (NP): SAT, Hamiltonian Path, Clique - Π₁ᴾ (co-NP): TAUTOLOGY, No-Hamiltonian Path - Σ₂ᴾ: MINSAT (minimizing the number of satisfied clauses) - Π₂ᴾ: FORALL-EXISTS-SAT (formulas of the form ∀x∃y.φ(x,y)) ## Collapse If P = NP, then the entire polynomial hierarchy collapses to P. Many complexity theorists believe the hierarchy is strict, meaning Σᵏᴾ ⊂ Σᵏ⁺¹ᴾ for all k. -/ namespace Millennium open Turing open Computability /- We'll represent the polynomial hierarchy using alternating quantifiers. First, we define the complement of a language. -/ /-- The complement of a language: `complement L` is the language `fun a => ¬ L a`. `Language α` is `α → Prop`, so this is the set-theoretic complement; the `Primcodable α` argument is part of the signature only, and the definition does not use it. For example, if SAT is the set of satisfiable Boolean formulas, then co-SAT is the set of unsatisfiable Boolean formulas. -/ def complement {α : Type} [Primcodable α] (L : Language α) : Language α := fun a => ¬(L a) /-- `Σₖᴾ` in quantifier form, on a language `L : α → Prop` with a fixed encoding `ea : FinEncoding α` and `α`'s `Primcodable` instance. - `k = 0`: `polynomial_hierarchy_sigma_level 0 ea L` is `InPolynomialTime ea L`. - `k + 1`: there are a type `β` with `Fintype` and `Primcodable` instances, a language `L' : Language (α × List β)`, the predicate `polynomial_hierarchy_sigma_level k e (complement L')` with `e = pair_encoding ea (fin_encoding_string β)` — that is, `L'`'s *complement* is at `Σₖᴾ` — and a polynomial `p : Polynomial ℕ` such that, for every `a`, `L a ↔ ∃ b : List β, b.length ≤ Polynomial.eval (ea.encode a).length p ∧ L' (a, b)`. The witness `b` is a finite list over `β`, encoded by `fin_encoding_string β` (the identity encoding on `List β`); the bound is `p` evaluated at the encoded input length `(ea.encode a).length`, and the quantifier asks for *some* polynomial `p`, not for a bound that holds for all `p`. Level `0` carries no witness and no bound. -/ def polynomial_hierarchy_sigma_level (k : ℕ) : {α : Type} → [_inst : Primcodable α] → (ea : FinEncoding α) → Language α → Prop | α, _inst, ea, L => match k with | 0 => InPolynomialTime ea L | k+1 => -- The witness `b` ranges over *all* finite strings over a finite alphabet `β` (identity -- encoding `fin_encoding_string β`), as in `InNondeterministicPolynomialTime`. ∃ (β : Type) (_instβ : Fintype β) (_primβ : Primcodable β) (L' : Language (α × List β)), -- `L'` is in `Πₖᴾ`, i.e. its complement is in `Σₖᴾ`. polynomial_hierarchy_sigma_level k (pair_encoding ea (fin_encoding_string β)) (complement L') ∧ -- Witness size is polynomially bounded. ∃ (p : Polynomial ℕ), ∀ a, L a ↔ ∃ b : List β, b.length ≤ Polynomial.eval (ea.encode a).length p ∧ L' (a, b) /-- `Πₖᴾ` is, by definition, `polynomial_hierarchy_sigma_level k ea (complement L)`: the witness alphabet, the pair encoding `pair_encoding ea (fin_encoding_string β)` and the polynomial bound are exactly those of `polynomial_hierarchy_sigma_level`, applied to the complement of `L`. -/ def polynomial_hierarchy_pi_level (k : ℕ) : {α : Type} → [_inst : Primcodable α] → (ea : FinEncoding α) → Language α → Prop | _α, _inst, ea, L => polynomial_hierarchy_sigma_level k ea (complement L) /-- `Σ₀ᴾ` at a fixed encoding `ea`: by definition this is `polynomial_hierarchy_sigma_level 0 ea`, which is `InPolynomialTime ea`. No witness and no polynomial bound are introduced at this level. -/ def polynomial_hierarchy_sigma_level_zero {α : Type} [Primcodable α] (ea : FinEncoding α) : Language α → Prop := polynomial_hierarchy_sigma_level 0 ea /-- `Π₀ᴾ` at a fixed encoding `ea`: by definition this is `polynomial_hierarchy_pi_level 0 ea`, which is `InPolynomialTime ea (complement L)`. -/ def polynomial_hierarchy_pi_level_zero {α : Type} [Primcodable α] (ea : FinEncoding α) : Language α → Prop := polynomial_hierarchy_pi_level 0 ea /-- `Σ₁ᴾ` at a fixed encoding `ea`: by definition `polynomial_hierarchy_sigma_level 1 ea`, i.e. one polynomially bounded existential witness over a finite alphabet in front of a `Π₀ᴾ` predicate on `pair_encoding ea (fin_encoding_string β)`. Whether this predicate is the statement's `NP` is S-4; it is not a definitional identity here. Examples: SAT, Hamiltonian Path, Clique problem -/ def polynomial_hierarchy_sigma_level_one {α : Type} [Primcodable α] (ea : FinEncoding α) : Language α → Prop := polynomial_hierarchy_sigma_level 1 ea /-- `Π₁ᴾ` at a fixed encoding `ea`: by definition `polynomial_hierarchy_pi_level 1 ea`, i.e. `polynomial_hierarchy_sigma_level 1 ea (complement L)` — the complement of a `Σ₁ᴾ` language. Examples of the corresponding decision problems: TAUTOLOGY, No-Hamiltonian Path, No-Clique -/ def polynomial_hierarchy_pi_level_one {α : Type} [Primcodable α] (ea : FinEncoding α) (L : Language α) : Prop := polynomial_hierarchy_pi_level 1 ea L /-- The polynomial hierarchy at a fixed encoding `ea`: `∃ k : ℕ, polynomial_hierarchy_sigma_level k ea L`. Membership is membership in some `Σₖᴾ` level; the `Π` levels enter only through `complement` inside each level. The union is taken over every natural number `k`. -/ def polynomial_hierarchy {α : Type} [Primcodable α] (ea : FinEncoding α) (L : Language α) : Prop := ∃ k, polynomial_hierarchy_sigma_level k ea L /-- # The polynomial hierarchy collapse statement. Literally: there is a level `k` such that, for every type `α` with a `Primcodable` instance, every encoding `ea : FinEncoding α` and every language `L : Language α`, the predicate `polynomial_hierarchy_sigma_level (k+1) ea L` implies `polynomial_hierarchy_sigma_level k ea L`. The quantifier over `α`, its `Primcodable` instance and `ea` is part of the statement; the level `k` is fixed inside it. Nothing in this file decides whether it holds. -/ def polynomial_hierarchy_collapses : Prop := ∃ k, ∀ (α : Type) [Primcodable α] (ea : FinEncoding α) (L : Language α), polynomial_hierarchy_sigma_level (k+1) ea L → polynomial_hierarchy_sigma_level k ea L /-- # The negation of `polynomial_hierarchy_collapses`. Literally: for every `k`, some type `α` with a `Primcodable` instance, some encoding `ea : FinEncoding α` and some language `L : Language α` satisfy `polynomial_hierarchy_sigma_level (k+1) ea L` but not `polynomial_hierarchy_sigma_level k ea L`. Nothing in this file proves or refutes it. -/ def polynomial_hierarchy_does_not_collapse : Prop := ¬polynomial_hierarchy_collapses /-! ## Lemmas that hold by unfolding the definitions -/ /-- At level `0` the `Σ` level is exactly `InPolynomialTime`: `Σ₀ᴾ = P` read as a definitional identity at the fixed encoding `ea`. -/ theorem polynomial_hierarchy_sigma_level_zero_iff {α : Type} [Primcodable α] (ea : FinEncoding α) (L : Language α) : polynomial_hierarchy_sigma_level 0 ea L ↔ InPolynomialTime ea L := Iff.rfl /-- The `Π` level is, by definition, the `Σ` level of the complement. -/ theorem polynomial_hierarchy_pi_level_iff_sigma_complement {α : Type} [Primcodable α] (k : ℕ) (ea : FinEncoding α) (L : Language α) : polynomial_hierarchy_pi_level k ea L ↔ polynomial_hierarchy_sigma_level k ea (complement L) := Iff.rfl /-- A language at some `Σₖᴾ` level belongs to the polynomial hierarchy. -/ theorem polynomial_hierarchy_sigma_level_mem {α : Type} [Primcodable α] (k : ℕ) (ea : FinEncoding α) (L : Language α) (h : polynomial_hierarchy_sigma_level k ea L) : polynomial_hierarchy ea L := ⟨k, h⟩ end Millennium