Variable lookup along an encoded environment runs within cost in the sizes
ProvedMIPRE.Cost.Machine.getListProg_runsWe work in the ambient cost model of the formalization: data are binary trees (Data, from nil and cons), is the number of nodes of , and is the timed semantics (the program in the environment halts with at cost exactly ; 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 is its tree encoding , a unary tag ( to 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 and let be the -th element of the cons-chain (nil beyond its end), and for data let be the length of the right spine of , so that a unary numeral ( nested cons nil) reads back as . The program getListProg is a loop on a state : if is exhausted it stops with the head of , otherwise it continues with the tail of and the predecessor of .
Let and be any data and any further environment. Then there is a cost with
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 , not only unary numerals: only the length of the right spine of matters, and 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 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.
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
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