Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Tag dispatch over closed programs costs the called program's time plus size(pk)+k+3\mathrm{size}(p_k) + k + 3size(pk​)+k+3

Proved
MIPRE.Cost.Machine.dispatch_runs

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

cost-modelmipre-formalizationself-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.

The interpreter selects the case program for a step by the tag of the program or frame at hand, a unary numeral. For a list of closed programs cs=[c0,…,cℓ−1]cs = [c_0, \dots, c_{\ell-1}]cs=[c0​,…,cℓ−1​] and variable indices i,idxi, \mathit{idx}i,idx, the dispatcher dispatch(cs,i,idx)\mathrm{dispatch}(cs, i, \mathit{idx})dispatch(cs,i,idx) is a chain of elims on variable iii: it calls c0c_0c0​ on the value of variable idx\mathit{idx}idx if that numeral is 000, and otherwise recurses on the tail of the list, with the index of the numeral now 111 and the index of the argument shifted by 222 (each elim on a cons binds two more variables); the last program of the list is called unconditionally. Calling a closed program ccc on variable vvv is callVar(v,c)=let_ (var v) c\mathrm{callVar}(v, c) = \texttt{let\_}\,(\texttt{var}\,v)\ ccallVar(v,c)=let_(varv) c.

Let cscscs be a list of programs that are all well-scoped in a one-entry environment (closed), let k<∣cs∣k < |cs|k<∣cs∣, and let EEE be an environment whose variable iii holds the unary numeral ofNat(k)\mathrm{ofNat}(k)ofNat(k) and whose variable idx\mathit{idx}idx holds the value pkp_kpk​. Suppose that ckc_kck​, run on the input pkp_kpk​, halts with result rrr at cost ttt. Then there is a cost sss with

s≤t+size(pk)+k+3andEval(E, dispatch(cs,i,idx), r, s).s \le t + \mathrm{size}(p_k) + k + 3 \quad\text{and}\quad \mathrm{Eval}\bigl(E,\ \mathrm{dispatch}(cs, i, \mathit{idx}),\ r,\ s\bigr).s≤t+size(pk​)+k+3andEval(E, dispatch(cs,i,idx), r, s).

This is the generic lemma behind the two step programs of the self-interpreter: stepEvProg dispatches over the seven case programs of "evaluate" by the tag of the program, and stepRetProg over the four case programs of "return" by the tag of the top frame.

Formalization Note. The statement quantifies over the list cs : List Prog and the indices explicitly, as the recursion on the list requires (MIPRE.Cost.Machine.dispatch is defined by cases [], [c], c :: c' :: cs). The hypothesis on ckc_kck​ is Eval [pk] (cs.getD k .nil) r t, a run in the one-entry environment; the overhead size(pk)+k+3\mathrm{size}(p_k) + k + 3size(pk​)+k+3 is one copy of the argument (size(pk)+1\mathrm{size}(p_k)+1size(pk​)+1), one unit per elim passed (kkk, or k+1k+1k+1 when the chain has not yet reached its last element), and the units of the let_ and var of the call.

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.dispatch_runs : ∀ (cs : List Prog) (k i idx : ℕ) (E : Env) (pk r : Data) (t : ℕ),
    k < cs.length → (∀ c ∈ cs, c.WellScoped 1) → E.get i = Data.ofNat k → E.get idx = pk →
    Eval [pk] (cs.getD k .nil) r t →
    ∃ s ≤ t + pk.size + k + 3, Eval E (dispatch cs i idx) r s := by sorry
Source
https://github.com/vidick/MIPRE-formalization/blob/70cbf1c52e3d5e1dff1162fdf952c9a9f4bd99ed/MIPRE/Foundations/Cost/Interpreter.lean#L348-L366; internal lemma of the formalization of blueprint lem:universal-tm (the tag dispatch of the self-interpreter's step programs)

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