leanprover-community / leanprover-community/mathlib4
feat(Computability): Step counting and complexity classes P/NP for TM1
Nobody has claimed this yet.
- Dominant language
- Lean
- Stars
- 4.2k
- Forks
- 1.7k
- PR merge metrics
- No merged PRs in 30d
Description
Summary
I have a working formalization of computational complexity classes P and NP built on top of Mathlib's existing Turing.TM1 model (from PostTuringMachine.lean). The code compiles against Mathlib v4.28.0-rc1 with zero sorry, zero warnings under Mathlib lint rules.
I would like to contribute this to Mathlib, incrementally, and am opening this issue to discuss the approach before submitting PRs.
What this adds
-
Turing.TM1.step_none_iff: Basic lemma —step M c = none ↔ c.l = none(follows directly from the definition). -
Turing.TM1.runN: Fuel-based step counter — execute a TM1 machine for at mostnsteps. Key lemmas:runN_stable(halted configs are fixed),runN_comp(composition:runN (a+b) = runN b ∘ runN a),runN_eq_of_halted(output stability). -
Complexity classes:
haltedAt,outputAt,acceptsAt,rejectsAt— predicates on TM1 executionIsPolynomial— polynomial bound on functionsℕ → ℕDecidableInTime— machine decides language within time boundInP,InNP— complexity classes parameterized by acceptance predicate on tape headp_sub_np— P ⊆ NP (proven, zerosorry)PEqNP,PNeNP— formal propositions for the open problem
Relation to existing work
PostTuringMachine.lean(Carneiro 2018): Defines TM0/TM1 models,Turing.eval,Turing.Reaches. No step counting or time bounds. Our work extends this.- PR #33132 (BoltonBailey): Defines a new
FinTM0bundled type withEvalsTo/EvalsToInTime. Complementary approach — builds new TM definitions, while ours builds on existingTuring.TM1. TMComputable.lean: Multi-tape TM2 complexity. Orthogonal.- Simas (2026, arXiv:2601.15571): Polynomial-time reductions in Lean 4, but does not use Mathlib's TMs and does not define P/NP classes.
Design choices
- Fuel-based
runNvs. structuralEvalsTo: We userunN : Nat → Cfg → Cfg(fuel-based), which makes monotonicity proofs straightforward. PR #33132 usesEvalsTo(a struct with steps + proof). Both are valid; ours is simpler but less informative. - Acceptance via tape head predicate: TM1 has no accept/reject states. We parameterize by
accept : Γ → Propon the tape head at halt, following Sipser §3.1. - Generalized alphabet:
InPandInNPare parameterized over{Γ : Type*} [Inhabited Γ], not a fixed alphabet.
Proposed PR sequence
- PR 1 (small):
step_none_ifflemma inPostTuringMachine.lean - PR 2 (medium):
runN+ lemmas in a new fileMathlib/Computability/TM1Complexity.lean - PR 3 (large): Complexity classes +
p_sub_nptheorem
Questions for maintainers
- Is building on
Turing.TM1the right base model, or should this wait for/coordinate with PR #33132'sFinTM0approach? - Is
runN(fuel-based) acceptable, or would a relational approach (EvalsTo-style) be preferred? - Should
IsPolynomiallive in a separate file (e.g.,Mathlib/Computability/Polynomial.lean)?
The full code is available at: https://github.com/KrystianYCSilva/cli-and-ai/blob/main/prompt_os_lean/agente_matematico/AgenteMatematico/MathComplexityContrib.lean
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading PostTuringMachine.lean and the related TMComputable.lean work, then review the proposed MathComplexityContrib.lean implementation. Compare the fuel-based runN approach with PR #33132's EvalsTo design and resolve the maintainers' questions before splitting the work into the three proposed PRs.
Written by the indexing model from the issue text.
Assessment
- Domain
- devtools
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100