Complexitylib is a Lean 4 library of computational complexity theory, built on CSLib and Mathlib. It develops reusable arguments about algorithms, resource bounds, reductions, lower bounds, and cryptographic security.
A central goal is to let you write recognizable functional programs and prove their efficiency compositionally, with Turing-machine implementations justified underneath. Nonconstructive mathematics and model-specific lower bounds also belong. Developments should establish useful results or make those results easier to state, prove, and reuse.
Development is AI-assisted. Contributors are responsible for the mathematical statements, source attribution, and maintainability of their changes. Lean checks proofs against their stated definitions; independent review checks that those definitions and statements express the intended mathematics. See Contributing to propose, implement, or review a development.
| I want to… | Start with… |
|---|---|
| Certify functional programs and loops | PolyTime examples |
| State P/NP membership or prove a problem NP-complete | Checked plain examples, then Clique |
| Construct circuits for polynomial-time function families | Uniform circuit examples |
| Write probabilistic programs or oracle reductions | PPT examples, oracle examples |
| Find a computational model or mathematical tool | Search the API documentation, rebuilt nightly from main; Complexity.lean lists every module, and each documents its definitions, results, and references |
| Contribute a result or improve an API | Contributing |
The checked examples show fun_prop, polytime, and ppt with explicit encodings and
client-supplied invariants. The deterministic backend builds on Christian Reitwiessner's tidy
machine calculus.
For an NP-completeness proof, fix the instance encoding, give a verifier with a polynomial
certificate-length bound, and reduce a known NP-hard problem to yours. Prove the verifier and
reduction map polynomial-time with fun_prop or polytime, then prove their membership
equivalences. The Clique example
uses graph complementation; the reduction index
points to the other proved problems and their reduction maps. The checked examples state these
claims with plain ComputableInPolyTime.
The selected formal abstracts present a small set of major results with inspectable definitions and checked Lean statements. The combined PDF and individual PDFs are generated from those sources. Each states its model and limitations; the selection is not an exhaustive API reference.
- Cook–Levin: SAT is NP-complete: Every efficiently checkable bit-string problem reduces in polynomial time to satisfiability.
- The PCP theorem: NP proofs can be checked with logarithmic randomness and constantly many nonadaptive queries.
- Fagin: logic captures NP: Existential second-order logic captures precisely the isomorphism-invariant NP properties of finite relational structures.
- Savitch: NPSPACE equals PSPACE: Nondeterminism does not enlarge polynomial space.
- Reingold: undirected reachability is in L: Undirected graph connectivity can be decided with logarithmic work space.
- Immerman–Szelepcsényi: NL equals coNL: Native nondeterministic logarithmic-space machines are closed under complement, with every branch of the complement machine halting.
- Shamir: IP equals PSPACE: Interaction and randomness give a polynomial-time verifier the power of polynomial space.
- Toda: a counting oracle decides the polynomial hierarchy: Every problem in the polynomial hierarchy is decidable in polynomial time with an exact counting oracle.
- Barrington: five states capture NC¹: Polynomial-length permutation programs on five states capture logarithmic-depth polynomial-size Boolean circuits.
- Håstad and Razborov–Smolensky: parity resists shallow circuits: At fixed depth, parity requires more than polynomially many gates, including over an odd-prime modular basis.
- Formula lower bounds: parity and Andreev: Tree-shaped computation pays for repeated work: parity requires quadratic leaf size, and Andreev gives a stronger explicit bound.
- One-way functions yield pseudorandom generators and adaptive functions: One-wayness yields pseudorandomness, from an extra output bit to functions secure against adaptive oracle queries.
- Haken: pigeonhole formulas require long resolution proofs: The pigeonhole principle has short CNF statements but requires exponentially many resolution lines.
- Communication: disjointness and inner product resist randomness: Both problems require linearly many communicated bits even with shared randomness and bounded error.
- Sipser–Gács–Lautemann: BPP lies in the second polynomial level: Bounded-error polynomial time admits both existential–universal and universal–existential polynomial descriptions.
The package is named complexitylib; imports begin with Complexity.. Most declaration
namespaces begin with Cslib, and Turing-machine declarations use Turing. Use the version in
lean-toolchain and the pinned dependencies in lakefile.toml.
From a checkout:
scripts/cache/get.sh # prebuilt Mathlib and Complexitylib outputs
lake build --wfail --iofailscripts/cache/get.sh downloads the outputs CI built for main, so lake build compiles only
local changes; without it the first build compiles the whole library. See
scripts/cache/README.md for how the caches work and what they trust.
Import the specific modules you need. import Complexity imports the whole library.
To build searchable API documentation from the module docstrings (Python 3.11+):
python3 scripts/check_docs.py --config-only
lake -d docbuild build Complexity:docs
python3 scripts/check_docs.py
python3 -m http.server --bind 127.0.0.1 --directory docbuild/.lake/build/docOpen http://localhost:8000 to browse and search. The API documentation workflow publishes
main to the API documentation site nightly.
A manual run on another branch produces an api-docs artifact instead; serve it by replacing
the directory argument above with the extracted artifact directory.
The guide introduces the library area by area, with Lean examples
that are checked against the library whenever the guide is built. It is written with
Verso in a separate guide project. To build and preview it:
scripts/guide.sh
python3 -m http.server --bind 127.0.0.1 --directory guide/.lake/build/site/html-multiThe first build compiles Verso and links the guide's generator, which takes a few minutes. The
API documentation workflow publishes the guide from main with the API documentation, at
/guide/. The Guide workflow builds
pull requests that change the guide and uploads the pages as a guide artifact.
This is the canonical CSLib-based Complexitylib. The renamed historical
pre-CSLib implementation remains available
with archival pending; it has a different API, and some developments remain to be ported. The temporary
repository and package name complexitylib2 was retired when this repository moved to CSLib
Community. Downstream projects using that package name should change it to complexitylib; Lean
imports remain unchanged. APIs are still evolving, so downstream projects should pin a commit.
Modules follow CSLib's APIs, paths, namespaces, and contribution standards so they can move
upstream with the import prefix changed from Complexity. to Cslib.. Acceptance requires
CSLib review. ROADMAP.md records outstanding work;
UPSTREAM.md records upstream coordination and exceptional integration decisions.
Source headers retain authorship, module docstrings credit mathematical and formalization sources, and references.bib contains the bibliography. Licensed under Apache 2.0.