One return-step of the self-interpreter on a nonempty stack runs within cost
ProvedMIPRE.Cost.Machine.stepRetProg_runs_consWe 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.
A frame of the machine is one of: cons1 t env (evaluating the head of a cons; next evaluate in ), cons2 a (the head value ; next return ), let1 b env (next evaluate in ), loop1 b env (next dispatch on the value returned by the loop body: stop with nil, stop with on , or iterate on ). Frames are encoded as data by with tags to . The step on a "return " control with a nonempty stack is the function of the machine on data, and stepRetProg is the program implementing it, on the input .
Let be data, a frame and data (the rest of the stack). Write . Then there is a cost with
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 ) 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 with the top frame a genuine Frame (so that the four frame shapes, and the three shapes of under a loop1 frame, can be enumerated) and 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 , computed with one projection; this is the only place a size inequality () enters.
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.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