Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Polynomial-time computable functions are closed under composition

Proved
PvsNP.isPolyTime_comp

by LG · Sep 14, 2026 · Mathlib 0df444a (Lean v4.33.1)

complexity-theorycomputabilityturing-machines

Let α\alphaα, β\betaβ, γ\gammaγ be types equipped with canonical bitstring encodings, and let f:α→βf : \alpha \to \betaf:α→β and g:β→γg : \beta \to \gammag:β→γ be computable in polynomial time by two-stack Turing machines with respect to those encodings. Then the composite g∘fg \circ fg∘f is computable in polynomial time as well.

The intended construction is the standard one: run the machine for fff on the input, copy its output stack to the input stack of the machine for ggg, and run the machine for ggg. The composed machine can be realised as a single two-stack machine with one stack for each stack of the two component machines. For the time bound, note that a machine writes at most one symbol per step, so on input xxx the intermediate string enc(f(x))\mathrm{enc}(f(x))enc(f(x)) has length at most tf(∣enc(x)∣)t_f(|\mathrm{enc}(x)|)tf​(∣enc(x)∣), where tft_ftf​ is the polynomial time bound of the first machine; the total running time is therefore bounded by

tf(∣enc(x)∣)  +  O(tf(∣enc(x)∣))  +  tg(tf(∣enc(x)∣)),t_f(|\mathrm{enc}(x)|) \;+\; O\bigl(t_f(|\mathrm{enc}(x)|)\bigr) \;+\; t_g\bigl(t_f(|\mathrm{enc}(x)|)\bigr),tf​(∣enc(x)∣)+O(tf​(∣enc(x)∣))+tg​(tf​(∣enc(x)∣)),

which is again a polynomial in ∣enc(x)∣|\mathrm{enc}(x)|∣enc(x)∣ since polynomials with natural-number coefficients are closed under composition, sums and products.

This closure property is the basic workhorse of polynomial-time complexity theory: it underlies the fact that P\mathsf{P}P is closed downwards under polynomial-time many-one (Karp) reductions, and hence every completeness argument.

Preamble
import Definitions.Def_PvsNP_complexity_classes
Formal statement
namespace PvsNP

theorem isPolyTime_comp {α β γ : Type} [BitstringEncoding α] [BitstringEncoding β]
    [BitstringEncoding γ] (f : α → β) (g : β → γ) (hf : IsPolyTime f) (hg : IsPolyTime g) :
    IsPolyTime (g ∘ f) := by sorry

end PvsNP
Source
Stated as `proof_wanted Turing.TM2ComputableInPolyTime.comp` in Mathlib, Mathlib/Computability/TuringMachine/Computable.lean (revision 0df444a360eaa60ab8c11dca51a86af692955474), end of file; mathematical content: Sanjeev Arora and Boaz Barak, Computational Complexity: A Modern Approach, Cambridge University Press, 2009, Chapter 1 (Claim 1.6, composition/simulation of Turing machines) and Chapter 2, Definition 2.7 ff.

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, with reuse governed by our licensing terms.

How Prove2Me worksResearch paperProve2Me Team
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me