Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

The interpreter loop follows a halting machine run, at cost (m+1)(B(S)+1)(m+1)(B(S)+1)(m+1)(B(S)+1) for mmm remaining steps

Proved
MIPRE.Cost.Machine.interpLoop_runs

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

cost-modelmipre-formalizationpolynomial-timeself-interpreteruniversal-machine

The self-interpreter of the ambient cost model drives the evaluation machine: a configuration ccc (control, environment, stack) is encoded as a value ⌈c⌉\lceil c\rceil⌈c⌉ (Cfg.toData), and the fixed program interpBody acts on such a value as follows: it tests whether the configuration is final (isFinalProg), and if so stops the loop with the returned value, and otherwise continues the loop with the encoding of the next configuration, computed by the step program stepProg. The interpreter loop is loop  interpBody\mathsf{loop}\;\mathtt{interpBody}loopinterpBody; a loop\mathsf{loop}loop of the model iterates its body on the state held in variable 000 until the body signals stop.

Let c0c_0c0​ be a configuration and let S∈NS \in \mathbb{N}S∈N bound the encoded size of every configuration along its run: size(⌈stepn(c0)⌉)≤S\mathrm{size}(\lceil \mathsf{step}^{n}(c_0)\rceil) \le Ssize(⌈stepn(c0​)⌉)≤S for all nnn. Suppose the run reaches, after NNN steps, the final configuration with value rrr and some environment eee: stepN(c0)=⟨ret r,e,[ ]⟩\mathsf{step}^{N}(c_0) = \langle \mathsf{ret}\,r, e, [\,]\ranglestepN(c0​)=⟨retr,e,[]⟩. Let ρ\rhoρ be an arbitrary environment (the entries below the loop state, which the loop never reads). Then for all m,nm, nm,n with n+m=Nn + m = Nn+m=N there is a cost

t≤(m+1)(B(S)+1),B(S)=interpBodyBound(S)=stepBound(S)+3S+30,stepBound(S)=(S+1)(S+40)+200,t \le (m+1)\bigl(B(S) + 1\bigr), \qquad B(S) = \mathtt{interpBodyBound}(S) = \mathtt{stepBound}(S) + 3S + 30, \quad \mathtt{stepBound}(S) = (S+1)(S+40) + 200,t≤(m+1)(B(S)+1),B(S)=interpBodyBound(S)=stepBound(S)+3S+30,stepBound(S)=(S+1)(S+40)+200,

such that evaluating loop  interpBody\mathsf{loop}\;\mathtt{interpBody}loopinterpBody in the environment ⌈stepn(c0)⌉::ρ\lceil \mathsf{step}^{n}(c_0)\rceil :: \rho⌈stepn(c0​)⌉::ρ halts with result rrr at cost ttt.

That is, started on the nnn-th configuration of a run that halts at step NNN, the loop halts with the machine's value, and each of the m=N−nm = N - nm=N−n remaining iterations costs at most one body bound B(S)B(S)B(S), quadratic in SSS. This is the forward half of the efficient universal machine (blueprint lem:universal-tm): together with the polynomial bound SSS on configuration sizes along a run of cost ttt (size_toData_le, eval_steps_bound) and the bound N≤3tN \le 3tN≤3t on the number of machine steps (eval_steps_count), it gives the polynomial time overhead of univProg.

Formalization Note: The statement is phrased for an arbitrary starting index nnn with n+m=Nn + m = Nn+m=N so that it can be proved by induction on the remaining number mmm of steps; the universal program uses it with n=0n = 0n=0, m=Nm = Nm=N. The hypothesis on SSS is required for all nnn, not only n≤Nn \le Nn≤N (configurations after the final one are fixed, so this is no loss). The bound counts one unit for the loop rule per iteration and the body bound B(S)B(S)B(S) dominates both the final iteration (3S+303S + 303S+30) and a stepping iteration (stepBound(S)\mathtt{stepBound}(S)stepBound(S) for stepProg plus the copies and the finality test).

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.interpLoop_runs (c₀ : Cfg) (S : ℕ) (hS : ∀ n, (step^[n] c₀).toData.size ≤ S)
    (r : Data) (e : Env) (N : ℕ) (hN : step^[N] c₀ = ⟨.ret r, e, []⟩) (env : Env) :
    ∀ m n, n + m = N → ∃ t ≤ (m + 1) * (interpBodyBound S + 1),
      Eval ((step^[n] c₀).toData :: env) (.loop interpBody) r t := by sorry
Source
https://github.com/vidick/MIPRE-formalization/blob/70cbf1c52e3d5e1dff1162fdf952c9a9f4bd99ed/MIPRE/Foundations/Cost/Universal.lean#L143-L183; 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