Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

One return-step of the self-interpreter on a nonempty stack runs within cost 8S+1208S + 1208S+120

Proved
MIPRE.Cost.Machine.stepRetProg_runs_cons

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

mipre-formalizationpolynomial-timeself-interpreteruniversal-machine

We work in the ambient cost model of the formalization: data are binary trees (Data, from nil and cons), size(d)\mathrm{size}(d)size(d) is the number of nodes of ddd, and Eval(env,p,r,t)\mathrm{Eval}(\mathrm{env}, p, r, t)Eval(env,p,r,t) is the timed semantics (the program ppp in the environment env\mathrm{env}env halts with rrr at cost exactly ttt; one unit per rule, plus the size of every value read by a variable or literal). The self-interpreter implements, as a program, the step function of a small-step evaluation machine whose configurations are encoded as data: an environment is a cons-chain of values, a stack a cons-chain of encoded frames, and a program ppp is its tree encoding toData(p)\mathrm{toData}(p)toData(p), a unary tag (000 to 666 for var, nil, cons, elim, let_, loop, const) paired with the fields.

A frame of the machine is one of: cons1 t env (evaluating the head of a cons; next evaluate ttt in env\mathrm{env}env), cons2 a (the head value aaa; next return cons(a,v)\texttt{cons}(a, v)cons(a,v)), let1 b env (next evaluate bbb in v::envv :: \mathrm{env}v::env), loop1 b env (next dispatch on the value returned by the loop body: stop with nil, stop with rrr on cons(nil,r)\texttt{cons}(\texttt{nil}, r)cons(nil,r), or iterate on cons(cons(⋅,⋅),v′)\texttt{cons}(\texttt{cons}(\cdot,\cdot), v')cons(cons(⋅,⋅),v′)). Frames are encoded as data by toData(f)=cons(ofNat(tag),payload)\mathrm{toData}(f) = \texttt{cons}(\mathrm{ofNat}(\mathrm{tag}), \mathrm{payload})toData(f)=cons(ofNat(tag),payload) with tags 000 to 333. The step on a "return vvv" control with a nonempty stack is the function stepRet(v,env,cons(toData(f),k))\mathrm{stepRet}(v, \mathrm{env}, \texttt{cons}(\mathrm{toData}(f), k))stepRet(v,env,cons(toData(f),k)) of the machine on data, and stepRetProg is the program implementing it, on the input cons(v,cons(env,kont))\texttt{cons}(v, \texttt{cons}(\mathrm{env}, \mathrm{kont}))cons(v,cons(env,kont)).

Let v,envv, \mathrm{env}v,env be data, fff a frame and kkk data (the rest of the stack). Write S=size(v)+size(env)+size(toData(f))+size(k)S = \mathrm{size}(v) + \mathrm{size}(\mathrm{env}) + \mathrm{size}(\mathrm{toData}(f)) + \mathrm{size}(k)S=size(v)+size(env)+size(toData(f))+size(k). Then there is a cost sss with

s≤8S+120andstepRetProg on cons(v,cons(env,cons(toData(f),k))) halts with stepRet(v,env,cons(toData(f),k)) at cost s.s \le 8S + 120 \quad\text{and}\quad \texttt{stepRetProg}\ \text{on}\ \texttt{cons}(v, \texttt{cons}(\mathrm{env}, \texttt{cons}(\mathrm{toData}(f), k)))\ \text{halts with}\ \mathrm{stepRet}(v, \mathrm{env}, \texttt{cons}(\mathrm{toData}(f), k))\ \text{at cost}\ s.s≤8S+120andstepRetProg on cons(v,cons(env,cons(toData(f),k))) halts with stepRet(v,env,cons(toData(f),k)) at cost s.

The return step is linear in the configuration size because no variable lookup is needed. With the empty-stack case (stepRetProg_runs_nil, cost at most size(v)+size(env)+12\mathrm{size}(v) + \mathrm{size}(\mathrm{env}) + 12size(v)+size(env)+12) and the evaluate step (stepEvProg_runs), it gives the step program of the self-interpreter (stepProg_runs), the body of the efficient universal machine's loop.

Formalization Note. The bound is stepRetBound S = 8 * S + 120 of MIPRE.Cost.Machine; the stack is passed as cons(toData(f),k)\texttt{cons}(\mathrm{toData}(f), k)cons(toData(f),k) with the top frame a genuine Frame (so that the four frame shapes, and the three shapes of vvv under a loop1 frame, can be enumerated) and kkk arbitrary data. The environment stored in a frame is a cons-chain (Data.list), and in the iterating case of loop1 the next environment is v′::tail(env′)v' :: \mathrm{tail}(\mathrm{env}')v′::tail(env′), computed with one projection; this is the only place a size inequality (size(right(d))≤size(d)\mathrm{size}(\mathrm{right}(d)) \le \mathrm{size}(d)size(right(d))≤size(d)) enters.

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_Machine
import Definitions.Def_MIPRE_Foundations_Cost_Codable
import Definitions.Def_MIPRE_Foundations_Cost_MachineData
import Definitions.Def_MIPRE_Foundations_Cost_Interpreter

open MIPRE
open MIPRE.Cost
open MIPRE.Cost.Machine
open MIPRE.Cost.Prog
Formal statement
theorem MIPRE.Cost.Machine.stepRetProg_runs_cons (v env : Data) (f : Frame) (k : Data) :
    ∃ s ≤ stepRetBound (v.size + env.size + (Frame.toData f).size + k.size),
      stepRetProg.Runs (.cons v (.cons env (.cons (Frame.toData f) k)))
        (stepRet v env (.cons (Frame.toData f) k)) s := by sorry
Source
https://github.com/vidick/MIPRE-formalization/blob/70cbf1c52e3d5e1dff1162fdf952c9a9f4bd99ed/MIPRE/Foundations/Cost/Interpreter.lean#L733-L809; internal lemma of the formalization of blueprint lem:universal-tm (the return half of the self-interpreter's step program)

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