Tag dispatch over closed programs costs the called program's time plus
ProvedMIPRE.Cost.Machine.dispatch_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.
The interpreter selects the case program for a step by the tag of the program or frame at hand, a unary numeral. For a list of closed programs and variable indices , the dispatcher is a chain of elims on variable : it calls on the value of variable if that numeral is , and otherwise recurses on the tail of the list, with the index of the numeral now and the index of the argument shifted by (each elim on a cons binds two more variables); the last program of the list is called unconditionally. Calling a closed program on variable is .
Let be a list of programs that are all well-scoped in a one-entry environment (closed), let , and let be an environment whose variable holds the unary numeral and whose variable holds the value . Suppose that , run on the input , halts with result at cost . Then there is a cost with
This is the generic lemma behind the two step programs of the self-interpreter: stepEvProg dispatches over the seven case programs of "evaluate" by the tag of the program, and stepRetProg over the four case programs of "return" by the tag of the top frame.
Formalization Note. The statement quantifies over the list cs : List Prog and the indices explicitly, as the recursion on the list requires (MIPRE.Cost.Machine.dispatch is defined by cases [], [c], c :: c' :: cs). The hypothesis on is Eval [pk] (cs.getD k .nil) r t, a run in the one-entry environment; the overhead is one copy of the argument (), one unit per elim passed (, or when the chain has not yet reached its last element), and the units of the let_ and var of the call.
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.dispatch_runs : ∀ (cs : List Prog) (k i idx : ℕ) (E : Env) (pk r : Data) (t : ℕ),
k < cs.length → (∀ c ∈ cs, c.WellScoped 1) → E.get i = Data.ofNat k → E.get idx = pk →
Eval [pk] (cs.getD k .nil) r t →
∃ s ≤ t + pk.size + k + 3, Eval E (dispatch cs i idx) r s := by sorry