Size bounds along the machine run of a derivation: values , environments , stack
ProvedMIPRE.Cost.Machine.eval_steps_boundThe evaluation machine (MIPRE.Cost.Machine) is a small-step abstract machine in the style of the CEK machine that implements the timed big-step semantics (Eval ρ p r t: the program , in the environment , halts with result at cost ) of the ambient cost model, in which data are binary trees with the number of nodes and programs form a first-order language with de Bruijn variables. A configuration (Cfg) consists of a control , which is either ("evaluate the program ") or ("return the value "), an environment (a list of values), and a stack of continuation frames (Frame) of four kinds: (the head of a pair is being evaluated; evaluate the tail in next), (the head has been computed; pair it with the tail's value), (the bound expression of a let is being evaluated; then run the body in extended by its value), and (the body of a loop is being evaluated in ; then dispatch on its value). The step function is total and deterministic, is its -fold iterate, and each step is charged the cost of the evaluation rule it carries out (stepCost), the steps that carry out no rule — entering a loop, returning a value to a , or frame, or a final configuration — being free. We write (Steps) when and the costs of these steps add up to .
For , a configuration is bounded, , if: a returned value has size at most ; a program under evaluation has encoded size at most (programs are encoded by their syntax trees, ); the environment has length at most and each of its values has size at most ; the stack has at most frames; and every frame is bounded in the same sense (FrameBound): its program has encoded size at most , its environment has length at most with values of size at most , and the value of a frame has size at most .
Theorem. Let be a derivation. Then for every stack and all such that the initial configuration is bounded by and , there exist and an environment such that
In words: started on in above the stack , the machine reaches in steps the configuration that returns above the same stack , the accumulated cost being exactly the cost of the derivation, and every configuration visited on the way is bounded with the same value bound and program bound , an environment-length bound increased by , and a stack-depth bound increased by .
This is the central quantitative lemma behind the efficient universal machine of blueprint lem:universal-tm. The self-interpreter univProg of the ambient model iterates a program implementing on encoded configurations, whose cost is polynomial in the size of the configuration; one therefore needs every configuration of the run simulating a halting computation to be small. Combined with size_toData_le (the encoded size of a bounded configuration is at most a polynomial in ) and eval_steps_count (the run has at most steps), this lemma yields the polynomial overhead of univProg (UniversalMachine.time_le) and the size guard of the clocked universal machine univTProg. Its variant eval_steps_bound_forever extends the bound to all when is empty, the final configuration being a fixed point of .
Formalization Note The hypothesis is what makes the value bound uniform: every value produced along the run is the result of a sub-derivation of cost at most , hence has size at most (Eval.size_le); the initial values must separately be bounded by through the hypothesis on the initial configuration. The increments are read off the rules: an elim on a pair pushes two values onto the environment at unit cost (whence ), a let or a loop iteration pushes one; a cons, a let or a loop pushes one frame at a cost of at least one unit (whence ). Programs under evaluation are always subterms of or of the programs stored in the frames of , so the bound never grows. The stack below the run is arbitrary and is returned unchanged, and the final environment is existentially quantified (the machine restores environments from frames but keeps the bindings made by elim, so may extend ). The cost in Steps is the Lean's stepCost, which charges the costs of the Eval rules at designated steps so that a run accumulates exactly the cost of the derivation.
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 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_Unary 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_MachineBound open MIPRE open MIPRE.Cost open MIPRE.Cost.Machine
theorem MIPRE.Cost.Machine.eval_steps_bound {env : Env} {p : Prog} {r : Data} {t : ℕ} (h : Eval env p r t) :
∀ (k : List Frame) (V L P K : ℕ), CfgBound V L P K ⟨.ev p, env, k⟩ → t ≤ V →
∃ N env', Steps ⟨.ev p, env, k⟩ N ⟨.ret r, env', k⟩ t ∧
∀ n ≤ N, CfgBound V (L + 2 * t) P (K + t) (step^[n] ⟨.ev p, env, k⟩) := by sorry