The interpreter loop follows a halting machine run, at cost for remaining steps
ProvedMIPRE.Cost.Machine.interpLoop_runsThe self-interpreter of the ambient cost model drives the evaluation machine: a configuration (control, environment, stack) is encoded as a value (Cfg.toData), and the fixed program interpBody acts on such a value as follows: it tests whether the configuration is final (isFinalProg), and if so stops the loop with the returned value, and otherwise continues the loop with the encoding of the next configuration, computed by the step program stepProg. The interpreter loop is ; a of the model iterates its body on the state held in variable until the body signals stop.
Let be a configuration and let bound the encoded size of every configuration along its run: for all . Suppose the run reaches, after steps, the final configuration with value and some environment : . Let be an arbitrary environment (the entries below the loop state, which the loop never reads). Then for all with there is a cost
such that evaluating in the environment halts with result at cost .
That is, started on the -th configuration of a run that halts at step , the loop halts with the machine's value, and each of the remaining iterations costs at most one body bound , quadratic in . This is the forward half of the efficient universal machine (blueprint lem:universal-tm): together with the polynomial bound on configuration sizes along a run of cost (size_toData_le, eval_steps_bound) and the bound on the number of machine steps (eval_steps_count), it gives the polynomial time overhead of univProg.
Formalization Note: The statement is phrased for an arbitrary starting index with so that it can be proved by induction on the remaining number of steps; the universal program uses it with , . The hypothesis on is required for all , not only (configurations after the final one are fixed, so this is no loss). The bound counts one unit for the loop rule per iteration and the body bound dominates both the final iteration () and a stepping iteration ( for stepProg plus the copies and the finality test).
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_Unary import Definitions.Def_MIPRE_Foundations_Cost_Toolkit 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 import Definitions.Def_MIPRE_Foundations_Cost_MachineBound import Definitions.Def_MIPRE_Foundations_Cost_Universal open MIPRE open MIPRE.Cost open MIPRE.Cost.Machine open Polynomial open MIPRE.Cost.Prog
theorem MIPRE.Cost.Machine.interpLoop_runs (c₀ : Cfg) (S : ℕ) (hS : ∀ n, (step^[n] c₀).toData.size ≤ S)
(r : Data) (e : Env) (N : ℕ) (hN : step^[N] c₀ = ⟨.ret r, e, []⟩) (env : Env) :
∀ m n, n + m = N → ∃ t ≤ (m + 1) * (interpBodyBound S + 1),
Eval ((step^[n] c₀).toData :: env) (.loop interpBody) r t := by sorry