---
title: Lean 4 Framework for Poly-Time Reductions
url: https://www.emergentmind.com/topics/lean-4-framework-for-polynomial-time-reductions
type: topic
---

# Lean 4 Framework for Poly-Time Reductions

A Lean 4 framework for polynomial-time reductions provides a mechanized setting for formalizing reductions and complexity-theoretic proofs in a dependent type theory based on Lean 4. It enables rigorous verification of computational complexity statements and automates many aspects of both reduction construction and complexity class membership certification. The framework centralizes the notion of a Karp (many-one) reduction, incorporates explicit polynomial-time witnesses, establishes compositionality, supplies instance-derivation mechanisms for standard classes (NP, coNP, $\Sigma_2^P$), and encodes combinators and tactics for scalable proofs.

## 1. Core Structure: `PolyReduction`

The foundational abstraction is the Lean structure `PolyReduction`, which formalizes a Karp (many-one) reduction between countable types $\alpha$ and $\beta$. A `PolyReduction` instance consists of:
- `reduce : \alpha \to \beta$, a function mapping instances,
- `time_bound`, a bundled witness of polynomial-time computability:
  \[
  \text{PolyReduction}\,(\alpha,\beta) : \left\{\,\text{reduce}:\alpha\to\beta,\,\exists\,p:\mathbb N\to\mathbb N,\,\text{polynomial } p \wedge \forall x,\,\text{runtime}(\text{reduce }x)\le p(\text{size }x) \right\}
  \]
Here, `runtime` statically reflects the computational cost of evaluating `reduce x` (as a natural number), and `size` is the canonical measure derived from the `Countable` encoding of $\alpha$. The predicate `polynomial p` ensures that $p$ is bounded by some polynomial, satisfying complexity-theoretic requirements [2601.15571].

This design strictly mandates that every reduction not only be functionally correct but be accompanied by explicit, verifiable polynomial-time bounds.

## 2. Reduction Composition and Tactic Automation

Polynomial-time reductions compose: if $f : \alpha \mapsto \beta$ and $g : \beta \mapsto \gamma$ are polynomial-time reductions, then so is $g \circ f : \alpha \mapsto \gamma$. The core combination theorem is:
```lean
theorem comp
  {α β γ : Type} [Countable α] [Countable β] [Countable γ]
  (f : α ↠ₚ β) (g : β ↠ₚ γ) : α ↠ₚ γ
```
with the composed runtime bound $p(n) = p_2(p_1(n))$, where $p_1$ and $p_2$ are the polynomials certifying the runtime of $f$ and $g$, respectively. The correctness of compositionality is established both on the function level and in the arithmetic of the polynomial witnesses. Tactic support in `AlgorithmComplexity.lean` includes `by poly_reduce`, which auto-applies reductions and handles polynomial-bound subgoals, and leverages auxiliary lemmas such as `polynomial.add`, `polynomial.comp`, and monotonicity constructs for inequalities.

These features provide a scalable and reusable infrastructure for assembling complex reductions from modular components.

## 3. Certifying Complexity Class Membership

Membership in NP, coNP, and $\Sigma_2^P$ is encoded as Lean classes (`InNP`, `InCoNP`, `InSigma2`), each consisting of:
- a witness type (or types, for $\Sigma_2^P$),
- a verification/refutation predicate (in the form $\texttt{witnessType} \times \alpha \to \texttt{Bool}$ or similar),
- explicit polynomial-time bounds for the verifier,
- and a logical specification relating solutions to witnesses.

For example, `InCoNP` requires
\[
\forall x,\,\lnot P(x) \Leftrightarrow \exists w,\,\text{refute}(w, x) = \text{true}
\]
with all computations in polynomial time.

A distinctive aspect is that, once a reduction from an established complete problem (e.g., TAUTOLOGY for coNP, SET-COVER for NP) to $Q$ is encoded as a `PolyReduction`, membership in the relevant class is inherited automatically, greatly reducing the burden of boilerplate proofs:
```lean
theorem Q_inCoNP [h : TAUTOLOGY ↠ₚ Q] : InCoNP Q :=
  apply CoNP_of_reduction TAUTOLOGY_inCoNP h
```
Similar mechanisms exist for NP and $\Sigma_2^P$, accelerating the classification of new decision problems.

## 4. Explicit Polynomial-Time Bookkeeping

The framework requires precise management of polynomial-time witnesses for every reduction or verifier. The idiom is to construct an explicit $p(n)$, prove `polynomial p`, and establish the inequality $\forall x,\, \text{runtime}(\cdot) \le p(\text{size }x)$. The granular control extends to fine details, such as composition, additive constants, and utilization of monotonicity for handling the arithmetic of size and running time propagation:
```lean
let p := fun n => p₂ (p₁ n) + 3 * n + 5
-- then refine ⟨polynomial.add (polynomial.comp hp₂ hp₁) (polynomial.add ...), ...⟩
```
Intermediate reasoning unfolds in Lean via `calc` blocks and numeric coercions to maintain correctness and transparency throughout polynomial-time bound construction.

This explicit bookkeeping reinforces the foundational correctness guarantees of the formalization and ensures verifiability of all complexity claims.

## 5. Example Reduction: Set-Cover to Sufficiency-Check

To illustrate, the reduction from SET-COVER to SUFFICIENCY-CHECK is formalized as a `PolyReduction`:
```lean
def redSetCoverToSuffCheck : SetCoverInst ↠ₚ SuffCheckInst where
  reduce := fun ⟨coverFam, …⟩ => {…}
  time_bound := by
    let p := fun n => n^3 + 10*n^2 + 5
    use p
    ...
```
Here, the concretely described `reduce` function repackages SET-COVER instances as SUFFICIENCY-CHECK problems via appropriate encodings of actions, coordinates, and query sets. The explicit polynomial $p(n) = n^3 + 10n^2 + 5$ bounds runtime for all steps, ensuring the reduction is correct within the formal framework.

Once such reductions are established, the framework automatically derives that SUFFICIENCY-CHECK is coNP-complete (TAUTOLOGY $\to$ SET-COVER $\to$ SUFFICIENCY-CHECK), with the compositional infrastructure handling all requisite instance constructions [2601.15571]. The result is a formally verified complexity landscape for the problem of identifying decision-relevant information.

## 6. Workflow and Formalization Scale

The full workflow comprises:
1. Defining `structure PolyReduction` with explicit reduction and runtime bound fields.
2. Proving combinators such as `comp`, `id`, and others for building new reductions.
3. Building tactic support to streamline reduction assembly and polynomial-bound verification.
4. Defining class-level encodings for NP, coNP, and $\Sigma_2^P$ along with instance constructors.
5. For a new problem $Q$, either producing an explicit reduction from a known complete problem or directly building a verifier or $\Sigma_2$ witness.
6. Letting Lean’s type-class and instance inference propagate memberships automatically.

The mechanized artifact spans approximately 5,600 lines of Lean 4 code across 36 files, supporting 230+ theorems, with all polynomial bounds and complexity claims fully explicit and machine-checked.

## 7. Significance and Scope of the Framework

The Lean 4 framework enables exhaustive and trustworthy formalization of classical and novel complexity-theoretic reductions, systematically capturing coNP-completeness, hardness of approximation, exponential lower bounds (ETH), and parameterized complexity (W[2]-hardness) for problems such as SUFFICIENCY-CHECK. The dichotomy between explicit and succinct encodings is directly formalized, and reduction correctness together with time bounds are mechanically verified. This strongly facilitates research in complexity theory by removing ambiguity and human error from reduction-based arguments and by enabling large-scale, reusable mechanized libraries for complexity-theoretic reasoning [2601.15571].

Source: https://www.emergentmind.com/topics/lean-4-framework-for-polynomial-time-reductions