The complexity classes , and
DefinitionPvsNP_complexity_classesA decision problem is a function , i.e. a language given by its characteristic function; a complexity class is a set of decision problems.
A function between types carrying bitstring encodings is polynomial-time computable if some two-stack Turing machine computes on the encoded inputs within a number of steps bounded by a polynomial in the input length (Mathlib's Turing.TM2ComputableInPolyTime).
This is the verifier definition of (Arora–Barak, Definition 2.1): the witness is a bitstring of length at most and the verifier runs in polynomial time in the length of its (encoded) pair input.
where is the pointwise Boolean negation of .
import Definitions.Def_PvsNP_bitstring_encoding
import Mathlib.Computability.TuringMachine.Computable
/-!
# Complexity classes P, NP and coNP
Decision problems are Boolean-valued functions on bitstrings, complexity classes are sets of
decision problems, and polynomial-time computability is the notion
`Turing.TM2ComputableInPolyTime` from Mathlib, taken with respect to the canonical bitstring
encodings.
Adapted from the `FormalConjecturesForMathlib.Computability.Complexity` file of the
`google-deepmind/formal-conjectures` project (Apache License 2.0), following
Arora–Barak, *Computational Complexity: A Modern Approach* (2009), Definitions 1.13 and 2.1.
-/
open Computability Turing
namespace PvsNP
/-- The type of decision problems: predicates on bitstrings, valued in `Bool`. -/
abbrev DecisionProblem := List Bool → Bool
/-- The type of complexity classes: sets of decision problems. -/
abbrev DecisionComplexityClass := Set DecisionProblem
/-- `IsPolyTimeWithEncoding ea eb f` asserts that `f` is computable in polynomial time by a
two-stack Turing machine when its input and output are encoded via `ea` and `eb`. -/
def IsPolyTimeWithEncoding {α β Γα Γβ : Type} (ea : Encoding α Γα) (eb : Encoding β Γβ)
(f : α → β) : Prop :=
Nonempty (TM2ComputableInPolyTime ea.encode eb.encode f)
/-- A function between types with canonical bitstring encodings is polynomial-time computable
if it is computable in polynomial time with respect to those encodings. -/
def IsPolyTime {α β : Type} [BitstringEncoding α] [BitstringEncoding β] (f : α → β) : Prop :=
IsPolyTimeWithEncoding (BitstringEncoding.toEncoding (α := α))
(BitstringEncoding.toEncoding (α := β)) f
/-- The class `P`: decision problems decidable in polynomial time by a deterministic Turing
machine. -/
def P : DecisionComplexityClass :=
{ L | IsPolyTime L }
/-- The class `NP`: decision problems `L` for which there is a polynomial `p` and a
polynomial-time verifier `R` such that `L x = true` if and only if some witness `w` of length
at most `p |x|` satisfies `R (x, w) = true`. -/
def NP : DecisionComplexityClass :=
{ L | ∃ (p : Polynomial ℕ), ∃ R : (List Bool × List Bool) → Bool,
IsPolyTime R ∧
∀ x, L x ↔ ∃ w : List Bool, w.length ≤ p.eval x.length ∧ R (x, w) }
/-- The class `coNP`: decision problems whose pointwise complement lies in `NP`. -/
def coNP : DecisionComplexityClass :=
{ L | Lᶜ ∈ NP }
end PvsNP
Read-back
What the Lean code literally says, in plain math · aristotle-harmonic
This file fixes a computational setting and defines three sets of functions.
A decision problem is declared to be exactly a total function from finite lists of Booleans to Booleans — not a subset, not a partial function, and not a -valued predicate. A complexity class is declared to be exactly a set of such functions, so two classes are equal precisely when they contain the same functions.
For types with encodings (into an alphabet ) and (into ) in Mathlib's sense, the proposition is defined to be the statement that the type is nonempty. Unfolded, this says: there exists a two-stack Turing machine in the sense of Mathlib, together with a polynomial over the naturals, such that on every input the machine started from the encoding of halts with output the encoding of , within a number of steps bounded by that polynomial evaluated at the length of the encoded input. Only the existence of such a machine is asserted; the machine itself is not part of the data of the proposition.
For types carrying canonical bitstring encodings, is the above proposition instantiated at those canonical encodings, in which bitstrings encode as concatenations of self-delimiting blocks (each payload bit written as , each block ended by , doubling the length and adding one) and a pair encodes as the self-delimiting block of the encoding of followed by the encoding of . Time bounds are therefore measured against the length of these particular encodings.
is defined as the set of decision problems such that holds, where is viewed as a function from bitstrings to Booleans with the canonical encodings on both sides.
is defined as the set of decision problems for which there exist a polynomial with natural-number coefficients and a function from pairs of bitstrings to Booleans such that holds and, for every bitstring ,
Here is the number of bits of itself, and is evaluated at that natural number; the polynomial has no positivity or monotonicity requirement, and may be the zero polynomial, in which case the only admissible witness is the empty string. The running time of is bounded polynomially in the length of the encoded pair under the canonical pair encoding. The empty input is included in the quantification.
is defined as the set of decision problems whose pointwise complement — the function , obtained from the Boolean complement applied pointwise — belongs to .
No relationship among the three sets is asserted here, and no problem is exhibited as belonging to any of them.