Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

The clocked loop computes `clockRun` at cost (b+1)(clockBodyBound(Θ,k)+1)(b+1)(\mathtt{clockBodyBound}(\Theta,k)+1)(b+1)(clockBodyBound(Θ,k)+1) for step budget bbb

Proved
MIPRE.Cost.Machine.clockLoop_runs

by thomasvidick · Oct 12, 2026 · Mathlib 0df444a (Lean v4.33.1)

cost-modelmipre-formalizationpolynomial-timeself-interpreteruniversal-machine

The clocked self-interpreter runs the evaluation machine from a configuration c0c_0c0​ under three guards: a cost budget kkk (the cumulative cost costSum(c0,n)\mathsf{costSum}(c_0, n)costSum(c0​,n) of the first nnn steps, which is the sum of the charges stepCost\mathsf{stepCost}stepCost along the run, may not exceed kkk), a step budget bbb (at most bbb further iterations), and a size guard Θ\ThetaΘ (the encoded size of every configuration handled may not exceed Θ\ThetaΘ). Its loop state is clockState(⌈c⌉,ofNat rem,ofNat b,ofNat Θ)\mathtt{clockState}(\lceil c\rceil, \mathsf{ofNat}\,\mathit{rem}, \mathsf{ofNat}\,b, \mathsf{ofNat}\,\Theta)clockState(⌈c⌉,ofNatrem,ofNatb,ofNatΘ), where rem\mathit{rem}rem is the remaining cost budget, and the body clockBody stops with the flagged value cons (cons nil nil) r\mathsf{cons}\,(\mathsf{cons}\,\mathsf{nil}\,\mathsf{nil})\,rcons(consnilnil)r on a final configuration with value rrr, stops with nil\mathsf{nil}nil when the step budget is exhausted, when the next step costs more than the remaining budget, or when the next configuration exceeds the guard, and otherwise continues with the stepped configuration, the decremented budgets and the same guard.

The intended value of the loop is the function clockRun(c0,k,Θ,b,n)\mathtt{clockRun}(c_0, k, \Theta, b, n)clockRun(c0​,k,Θ,b,n), defined by recursion on bbb: if the nnn-th configuration stepn(c0)\mathsf{step}^{n}(c_0)stepn(c0​) is final it is the flagged value of that configuration; otherwise, if b=0b = 0b=0 it is nil\mathsf{nil}nil, and if b=b′+1b = b' + 1b=b′+1 it is clockRun(c0,k,Θ,b′,n+1)\mathtt{clockRun}(c_0, k, \Theta, b', n+1)clockRun(c0​,k,Θ,b′,n+1) when costSum(c0,n+1)≤k\mathsf{costSum}(c_0, n+1) \le kcostSum(c0​,n+1)≤k and size⌈stepn+1(c0)⌉≤Θ\mathrm{size}\lceil\mathsf{step}^{n+1}(c_0)\rceil \le \Thetasize⌈stepn+1(c0​)⌉≤Θ, and nil\mathsf{nil}nil otherwise.

Let c0c_0c0​ be a configuration, k,Θ∈Nk, \Theta \in \mathbb{N}k,Θ∈N and ρ\rhoρ an environment. For all b,n∈Nb, n \in \mathbb{N}b,n∈N such that

  1. b≤3k+1b \le 3k + 1b≤3k+1,
  2. costSum(c0,n)≤k\mathsf{costSum}(c_0, n) \le kcostSum(c0​,n)≤k, and
  3. size⌈stepn(c0)⌉≤Θ\mathrm{size}\lceil\mathsf{step}^{n}(c_0)\rceil \le \Thetasize⌈stepn(c0​)⌉≤Θ,

there is a cost

t≤(b+1)(clockBodyBound(Θ,k)+1)t \le (b+1)\bigl(\mathtt{clockBodyBound}(\Theta, k) + 1\bigr)t≤(b+1)(clockBodyBound(Θ,k)+1)

such that loop  clockBody\mathsf{loop}\;\mathtt{clockBody}loopclockBody, evaluated in the environment clockState(⌈stepn(c0)⌉,ofNat(k−costSum(c0,n)),ofNat b,ofNat Θ)::ρ\mathtt{clockState}\bigl(\lceil\mathsf{step}^{n}(c_0)\rceil, \mathsf{ofNat}(k - \mathsf{costSum}(c_0, n)), \mathsf{ofNat}\,b, \mathsf{ofNat}\,\Theta\bigr) :: \rhoclockState(⌈stepn(c0​)⌉,ofNat(k−costSum(c0​,n)),ofNatb,ofNatΘ)::ρ, halts with result clockRun(c0,k,Θ,b,n)\mathtt{clockRun}(c_0, k, \Theta, b, n)clockRun(c0​,k,Θ,b,n) at cost ttt. Here

clockBodyBound(Θ,k)=stepCostBound(Θ)+subBound(Θ,k)+stepBound(Θ)+sizeBound(stepBound Θ)+subBound(stepBound Θ,Θ)+6 stepBound(Θ)+12Θ+30k+200,\mathtt{clockBodyBound}(\Theta, k) = \mathtt{stepCostBound}(\Theta) + \mathtt{subBound}(\Theta, k) + \mathtt{stepBound}(\Theta) + \mathtt{sizeBound}(\mathtt{stepBound}\,\Theta) + \mathtt{subBound}(\mathtt{stepBound}\,\Theta, \Theta) + 6\,\mathtt{stepBound}(\Theta) + 12\Theta + 30k + 200 ,clockBodyBound(Θ,k)=stepCostBound(Θ)+subBound(Θ,k)+stepBound(Θ)+sizeBound(stepBoundΘ)+subBound(stepBoundΘ,Θ)+6stepBound(Θ)+12Θ+30k+200,

a polynomial in Θ\ThetaΘ and kkk (with stepBound(S)=(S+1)(S+40)+200\mathtt{stepBound}(S) = (S+1)(S+40)+200stepBound(S)=(S+1)(S+40)+200, stepCostBound(S)=(S+1)(7S+32)+13S+50\mathtt{stepCostBound}(S) = (S+1)(7S+32)+13S+50stepCostBound(S)=(S+1)(7S+32)+13S+50, subBound(a,b)=(a+1)(2a+2b+14)\mathtt{subBound}(a,b) = (a+1)(2a+2b+14)subBound(a,b)=(a+1)(2a+2b+14) and sizeBound(s)=(s+1)(5s+20)+s+6\mathtt{sizeBound}(s) = (s+1)(5s+20)+s+6sizeBound(s)=(s+1)(5s+20)+s+6).

This is the core of the clocked universal machine of blueprint lem:universal-tm: univTProg_runs applies it from the initial configuration with n=0n = 0n=0 and b=3k+1b = 3k+1b=3k+1, and identifies clockRun\mathtt{clockRun}clockRun with the budgeted result clockedResult (clockRun_eq, from clockRun_of_final and clockRun_of_not). The bound shows that the loop costs at most polynomially in kkk and Θ\ThetaΘ whether or not the simulated run halts.

Formalization Note: The remaining cost budget in the state is the truncated difference k−costSum(c0,n)k - \mathsf{costSum}(c_0, n)k−costSum(c0​,n), which hypothesis 2 makes an honest difference; the condition b≤3k+1b \le 3k+1b≤3k+1 is only used to bound the cost of reading the step budget. clockRun is a noncomputable definition (it decides finality classically) used purely as a specification. All numerals in the state are unary.

Preamble
import Mathlib.Algebra.Polynomial.Eval.Defs
import Mathlib.Computability.Primrec.List
import Mathlib.Data.List.Basic
import Mathlib.Data.Nat.Bits
import Mathlib.Data.Nat.Notation
import Mathlib.Data.Nat.Size
import Mathlib.Logic.Denumerable
import Mathlib.Logic.Function.Iterate
import Mathlib.Tactic.Linarith
import Mathlib.Tactic.Ring
import Definitions.Def_MIPRE_Foundations_Cost_Basic
import Definitions.Def_MIPRE_Foundations_Cost_Encoding
import Definitions.Def_MIPRE_Foundations_Cost_PolyTime
import Definitions.Def_MIPRE_Foundations_Cost_Closure
import Definitions.Def_MIPRE_Foundations_Cost_Loops
import Definitions.Def_MIPRE_Foundations_Cost_Unary
import Definitions.Def_MIPRE_Foundations_Cost_Toolkit
import Definitions.Def_MIPRE_Foundations_Cost_Machine
import Definitions.Def_MIPRE_Foundations_Cost_Codable
import Definitions.Def_MIPRE_Foundations_Cost_MachineData
import Definitions.Def_MIPRE_Foundations_Cost_Interpreter
import Definitions.Def_MIPRE_Foundations_Cost_MachineBound
import Definitions.Def_MIPRE_Foundations_Cost_Universal

open MIPRE
open MIPRE.Cost
open MIPRE.Cost.Machine
open Polynomial
open MIPRE.Cost.Prog
Formal statement
theorem MIPRE.Cost.Machine.clockLoop_runs (c₀ : Cfg) (k Θ : ℕ) (env : Env) : ∀ (b n : ℕ), b ≤ 3 * k + 1 →
    costSum c₀ n ≤ k → (step^[n] c₀).toData.size ≤ Θ →
    ∃ t ≤ (b + 1) * (clockBodyBound Θ k + 1),
      Eval (clockState (step^[n] c₀).toData (.ofNat (k - costSum c₀ n)) (.ofNat b) (.ofNat Θ)
          :: env) (.loop clockBody) (clockRun c₀ k Θ b n) t := by sorry
Source
https://github.com/vidick/MIPRE-formalization/blob/70cbf1c52e3d5e1dff1162fdf952c9a9f4bd99ed/MIPRE/Foundations/Cost/Universal.lean#L1031-L1105; internal lemma of the formalization of blueprint lem:universal-tm, https://vidick.github.io/MIPRE-formalization/blueprint/

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