Skip to content

About

Computational complexity theory in Lean built on top of CSLib.

Resources

Contributing

Stars

5 stars

Watchers

0 watching

Forks

Repository files navigation

Complexitylib

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.

Start here

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.

Selected results

Build and use

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 --iofail

scripts/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/doc

Open 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-multi

The 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.

Project information

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.

About

Computational complexity theory in Lean built on top of CSLib.

Resources

Contributing

Stars

5 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages