Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Variable lookup along an encoded environment runs within cost (∣i∣+1)(∣env∣+∣i∣+12)(|i|+1)(|\mathrm{env}|+|i|+12)(∣i∣+1)(∣env∣+∣i∣+12) in the sizes

Proved
MIPRE.Cost.Machine.getListProg_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.

Variable lookup is the one non-constant operation of a machine step. For data ddd and n∈Nn \in \mathbb{N}n∈N let getList(d,n)=left(rightn(d))\mathrm{getList}(d, n) = \mathrm{left}(\mathrm{right}^{n}(d))getList(d,n)=left(rightn(d)) be the nnn-th element of the cons-chain ddd (nil beyond its end), and for data iii let unaryToNat(i)\mathrm{unaryToNat}(i)unaryToNat(i) be the length of the right spine of iii, so that a unary numeral ofNat(n)\mathrm{ofNat}(n)ofNat(n) (nnn nested cons nil) reads back as nnn. The program getListProg is a loop on a state cons(env,i)\texttt{cons}(\mathrm{env}, i)cons(env,i): if iii is exhausted it stops with the head of env\mathrm{env}env, otherwise it continues with the tail of env\mathrm{env}env and the predecessor of iii.

Let iii and env\mathrm{env}env be any data and rest\mathrm{rest}rest any further environment. Then there is a cost ttt with

t≤(size(i)+1) (size(env)+size(i)+12)andEval(cons(env,i)::rest, getListProg, getList(env,unaryToNat(i)), t).t \le (\mathrm{size}(i) + 1)\,\bigl(\mathrm{size}(\mathrm{env}) + \mathrm{size}(i) + 12\bigr) \quad\text{and}\quad \mathrm{Eval}\bigl(\texttt{cons}(\mathrm{env}, i) :: \mathrm{rest},\ \texttt{getListProg},\ \mathrm{getList}(\mathrm{env}, \mathrm{unaryToNat}(i)),\ t\bigr).t≤(size(i)+1)(size(env)+size(i)+12)andEval(cons(env,i)::rest, getListProg, getList(env,unaryToNat(i)), t).

The lemma supplies the var and elim cases of the evaluate-step program stepEvProg, where a de Bruijn index (a unary numeral) is looked up in the encoded environment; it is the only reason the cost of one interpreter step (stepProg_runs) is quadratic rather than linear in the size of the configuration, and so it enters the polynomial overhead of the efficient universal machine.

Formalization Note. The statement is for arbitrary data iii, not only unary numerals: only the length of the right spine of iii matters, and size(i)\mathrm{size}(i)size(i) bounds it. The bound is quadratic because each iteration copies the remaining environment (its tail is read by a var, at a cost equal to its size); the constant 121212 accounts for the fixed elims, conses and the two projections of one iteration. Data.getList and Data.unaryToNat are defined in Cost/Codable.lean as first-order operations so that the same step function is also primitive recursive.

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.getListProg_runs (idx env : Data) (rest : Env) :
    ∃ t ≤ (idx.size + 1) * (env.size + idx.size + 12),
      Eval (.cons env idx :: rest) getListProg (Data.getList env (Data.unaryToNat idx)) t := by sorry
Source
https://github.com/vidick/MIPRE-formalization/blob/70cbf1c52e3d5e1dff1162fdf952c9a9f4bd99ed/MIPRE/Foundations/Cost/Interpreter.lean#L93-L134; internal lemma of the formalization of blueprint lem:universal-tm (the environment walk of the self-interpreter)

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