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, with DescriptiveComplexity.Examples.ProbabilisticQueries (query evaluation over probabilistic databases, a hard query and an easy one) for the counting side; 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 #

Counting problems, and the class #P #

Everything above measures decision problems. A counting problem (DescriptiveComplexity.CountingProblem, in DescriptiveComplexity.Counting) attaches an isomorphism-invariant natural number to every structure, and its reductions are the parsimonious ones, C ≤ᵖ D and C ≤ᵖ[≤] D: a first-order interpretation under which the two counts are equal. They compose like the decision reductions, for the same reason. As on the decision side there is a relativized form C ≤ʳᵖ[≤] D, with a definable target universe (DescriptiveComplexity.Counting.Relativized), for the problems whose solutions span the universe; parsimonious hardness for a counting class is hardness under those (DescriptiveComplexity.parsimoniousHard_sharpP_iff), every ordinary reduction being one. Membership in #P travels backward along them too (DescriptiveComplexity.SharpPDefinable.of_relOrderedParsimonious, in DescriptiveComplexity.Counting.RelClosure). Pulling a kernel back through a definable domain is only a retraction on assignments, a pulled relation being free to hold of tuples that are not points of the target, which would count every witness several times; one more conjunct in the kernel, saying that the pulled relations hold only of points of the domain (DescriptiveComplexity.supportSentence), makes it a bijection (DescriptiveComplexity.witnessCount_mapRel).

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 conjoined—SAT-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——
⊕Pthe number of witnesses of an ∃SO sentence is odd (DescriptiveComplexity.ParityP)nondeterministic polynomial-time Turing machine, the parity of its accepting runs⊕SAT · the parity of every parsimoniously #P-complete problem (DescriptiveComplexity.parityP_complete_of_sharpP_parsimoniousComplete)
Mod_k Pthe number of witnesses is not a multiple of k (DescriptiveComplexity.ModP)the same machine, the residue of its accepting runsMod_k-SAT · likewise for every parsimoniously #P-complete problem
PPone witness count exceeds another (DescriptiveComplexity.PP)—SelMajSAT: the selected variable is true in more models than it is false
C₌Ptwo witness counts are equal (DescriptiveComplexity.CeqP)—SelEqSAT: in exactly as many
UPat most one witness, and the answer is whether there is one (DescriptiveComplexity.UP)—— (none is known)
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)
FP (polynomial-time functions with natural-number values, DescriptiveComplexity.FP)QFO(LFP), the quantitative first-order logic of Arenas, Muñoz, Riveros 2020 over least fixed points: sums and products, over the elements, of polynomial-time conditions––
#P (a counting class, DescriptiveComplexity.SharpP)the number of witnesses of an ∃SO sentence, over a linearly ordered universe: #FO, equivalently ΣQSO(FO)nondeterministic polynomial-time Turing machine, counting its accepting runs#SAT · #3SAT · #1-in-SAT · #Exact Cover · #Knapsack · #0-1 Integer Programming (both with binary numbers) · #Clique · #Independent Set · #Vertex Cover · #Set Packing · #Set Cover · #Hitting Set · #Dominating Set · #Feedback Vertex Set · #Feedback Arc Set · #Steiner Tree (all ten counting the solutions of exactly the threshold size) · #Directed Hamilton Circuit · #Hamilton Circuit (circuits as sets of edges) · counting the accepting runs of such a machine – all parsimoniously complete, a stronger notion than the #P-completeness of the literature · #DNF, complete under subtractive reductions (plain #P-complete here) · #2SAT · #HORN-SAT · #Monotone-2SAT · #NAE-SAT · #Set Splitting · #3-Colorability · counting all the independent sets or all the vertex covers of a graph · #BIS (the independent sets of a bipartite graph) · #PP2DNF · the permanent (#Cycle Cover), complete under one-call reductions only
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 construction—for 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 #