Documentation

DescriptiveComplexity

Descriptive complexity in Lean 4 #

A library for descriptive complexity built on Mathlib's ModelTheory: machine-model-free hardness reductions and a logically-defined polynomial hierarchy, in the style of Immerman's Descriptive Complexity (Immerman 1999).

Complexity theory is largely absent from Mathlib because formalizing a model of computation with resource bounds is hard. The observation this library rests on is that many classical NP-hardness reductions do not need the full strength of a Turing machine: they are first-order expressible. Classically, an FO reduction is computable in AC⁰ ⊆ LOGSPACE ⊆ PTIME, so exhibiting one is strictly stronger than exhibiting a Karp reduction (Karp 1972), while needing no machine model at all – only first-order logic, which Mathlib already provides.

This page is the high-level map of the library, part by part. The README gives the general pitch; the paper Senellart and Gnatenko 2026 presents the design and the main results; the worked examples in DescriptiveComplexity.Examples.ConjunctiveQueries (conjunctive-query evaluation and containment) and DescriptiveComplexity.Examples.GraphCrawling (Web data acquisition, with a cardinality threshold, a reachability certificate and an ordered reduction) are the hands-on tutorials; individual declarations are documented on their own pages.

The framework: problems, interpretations, reductions #

Writing a problem down #

Three authoring aids, used by the tutorials of DescriptiveComplexity.Examples and by the catalog. They are macros and commands: what they elaborate to is what one would have written by hand, so they change nothing that reads the declarations they produce.

The abstract complexity layer #

The polynomial hierarchy, defined logically #

Polynomial time, by the Horn fragment #

The inflationary and partial fixed points #

Reductions with a logic inside: FO(TC) and FO(LFP) #

Every reduction above is first-order, hence computable in AC⁰; the textbook notions for the classes from logarithmic space up are the logarithmic-space and the polynomial-time reduction. Without machines, those are interpretations whose formulas are written in a logic capturing that class – Immerman's FO(TC) and FO(LFP) reductions, built here on the walk and the inflationary layers above. They nest, ≤ᶠᵒ[≤]≤ᵈᵗᶜ≤ᵗᶜ≤ˡᶠᵖ, with the first inclusion strict.

Inexpressibility: Ehrenfeucht–Fraïssé games #

The one part of the library that proves things logic cannot do, with no complexity-theoretic assumption anywhere – the payoff a machine-first development has no counterpart for.

Nondeterministic logarithmic space, by the Krom fragment #

The numeric predicates, and AC⁰ #

The bottom of the ordered world, and the only vocabulary here that is a function of the order rather than part of an instance.

Polynomial space, by second-order transitive closure #

The exponential classes, by expansion #

Value invention, towards the recursively enumerable #

Shared encodings #

The bridge to Mathlib's computability layer #

Every class of this development is defined by a logic, and every problem is an isomorphism-closed property of finite structures. ComputablePred and REPred, on the other hand, are predicates on a Primcodable type, so relating the two needs a passage from isomorphism classes to data. It is built once for the catalog rather than once per problem.

The problem catalog #

DescriptiveComplexity.Problems holds one decision problem per file, each with its vocabulary, FO reductions and a completeness theorem. Every class in the table below is defined logically; each problem listed is proved complete for it – both a member and hard under FO reductions. The catalog covers all of Karp's 21 NP-complete problems. Each problem's own module page documents its reduction and certificate in full.

Complexity classLogical characterizationMachine modelProblems proved complete
LOGSPACE (L)FO(≤, DTC): first-order logic with a deterministic transitive closure, over a linearly ordered universedeterministic two-way k-head automaton †REACHd · UNREACHd
NLSO-Krom(≤): ∃SO with a Krom kernel, at most two second-order literals per clause; equivalently FO(≤, TC), first-order logic with a transitive closure – both over a linearly ordered universetwo-way k-head automaton †REACH · UNREACH · 2SAT
PTIME = Σ₀ᵖ = Π₀ᵖSO-Horn(≤): ∃SO with a Horn kernel; equivalently FO(≤, LFP), first-order logic with a least fixed point – both over a linearly ordered universedeterministic polynomial-time Turing machineHORN-SAT · CVP
NP = Σ₁ᵖ∃SO: existential second-order logicnondeterministic polynomial-time Turing machineSAT-family: SAT · 3SAT · NAE-SAT · NAE-3SAT · 1-in-SAT<br>Coloring: 3-Colorability · k-Colorability (k ≥ 3) · Chromatic Number · Clique Cover<br>Cliques & subgraphs: Clique · Independent Set · Vertex Cover · Subgraph Isomorphism<br>Sets & hypergraphs: Set Cover · Hitting Set · Set Packing · Exact Cover · Set Splitting · Dominating Set · 3-Dimensional Matching<br>Graphs: Feedback Vertex Set · Feedback Arc Set · Steiner Tree (node- & edge-weighted) · Max Cut · Hamilton Circuit (directed & undirected)<br>Numbers (in binary): Knapsack · Partition · 0-1 Integer Programming · Job Sequencing<br>Machines: acceptance by a nondeterministic polynomial-time Turing machine
coNP = Π₁ᵖ∀SO: universal second-order logicthe same machine, accepting when every run doesTAUT · 3-DNF-TAUT · 3-UNSAT (and QBF∀ at one block) · ATMAccept 1 false
DPa Σ₁ and a Π₁ sentence conjoinedSAT-UNSAT
Σₖᵖ (k ≥ 1)Σₖ¹: k alternating second-order quantifier blocks, existential firstalternating polynomial-time Turing machine, k blocks, existential firstQBF k – at k = 1, NP · ATMAccept k true
Πₖᵖ (k ≥ 1)Πₖ¹: k alternating second-order quantifier blocks, universal firstthe same machine, universal firstQBF∀ k – at k = 1, coNP · ATMAccept k false
PHfull second-order logic
PSPACESO(TC): second-order logic with a transitive closure over assignments of a block of relation variablespolynomial-space Turing machine, deterministic or notSUCCINCT-REACH · QSAT · space-bounded machine acceptance (deterministic & not)
EXPTIMESO(LFP): PTIME read over an exponential expansion; equivalently SO-GAME, a second-order alternating gamealternating polynomial-space Turing machineacceptance by such a machine (APSPACE = EXPTIME)
NEXPTIME∃SO over an exponential expansion, i.e., NP read there; equivalently ∃SO[new, exp], value invention bounded exponentiallywide machine, clockedacceptance by such a machine within its clock · tiling a wide square (the 2ⁿ × 2ⁿ tiling)
EXPSPACESO(PFP): PSPACE read over an exponential expansionwide machine, space-boundedacceptance by such a machine in bounded space (deterministic & not) · tiling a wide corridor (width 2ⁿ, unbounded height)
RE∃SO[new]: ∃SO with value invention, the relation variables ranging over the universe extended by finitely many invented valuesTuring machine, no step or space boundFINSAT (Trakhtenbrot's theorem) · CODEHALT · HALT · PCP (Post's correspondence problem)
the degree of a problem: DescriptiveComplexity.ComplexityClass.below Q₀, e.g., GInone – a downward closure under ≤ᶠᵒ[≤] rather than a logic, which is the point of the constructionfor GI: Graph Isomorphism · Digraph Isomorphism · DAG Isomorphism

Two of the models are named rather than described: both head automata walk a linear order of the universe, and a wide machine carries its control in the instance while its tape is addressed by an exponential expansion of it (DescriptiveComplexity.Problems.Wide).

Each entry of the machine model column is an equivalence proved here between the logical definition of the class and acceptance by that model: DescriptiveComplexity.mem_LOGSPACE_iff_automaton and DescriptiveComplexity.mem_NL_iff_automaton (†), and, through the completeness of an acceptance problem, DescriptiveComplexity.mem_PTIME_iff_le_dtmAccept, DescriptiveComplexity.mem_NP_iff_le_ntmAccept, DescriptiveComplexity.le_dtmAcceptSpace_of_mem_PSPACE, DescriptiveComplexity.atmAcceptSpace_EXPTIME_complete, DescriptiveComplexity.wideRegAccept_NEXPTIME_complete, DescriptiveComplexity.wideAcceptSpace_EXPSPACE_complete and DescriptiveComplexity.mem_RE_iff_rePred. A dash marks a class for which no such equivalence is proved here.

† Capture theorems, both directions: DescriptiveComplexity.mem_NL_iff_automaton for NL, and DescriptiveComplexity.mem_LOGSPACE_iff_automaton for LOGSPACE, where the machine is required to be deterministic.

Headline results and cross-references:

Worked examples #