The clocked loop computes `clockRun` at cost for step budget
ProvedMIPRE.Cost.Machine.clockLoop_runsThe clocked self-interpreter runs the evaluation machine from a configuration under three guards: a cost budget (the cumulative cost of the first steps, which is the sum of the charges along the run, may not exceed ), a step budget (at most further iterations), and a size guard (the encoded size of every configuration handled may not exceed ). Its loop state is , where is the remaining cost budget, and the body clockBody stops with the flagged value on a final configuration with value , stops with when the step budget is exhausted, when the next step costs more than the remaining budget, or when the next configuration exceeds the guard, and otherwise continues with the stepped configuration, the decremented budgets and the same guard.
The intended value of the loop is the function , defined by recursion on : if the -th configuration is final it is the flagged value of that configuration; otherwise, if it is , and if it is when and , and otherwise.
Let be a configuration, and an environment. For all such that
- ,
- , and
- ,
there is a cost
such that , evaluated in the environment , halts with result at cost . Here
a polynomial in and (with , , and ).
This is the core of the clocked universal machine of blueprint lem:universal-tm: univTProg_runs applies it from the initial configuration with and , and identifies with the budgeted result clockedResult (clockRun_eq, from clockRun_of_final and clockRun_of_not). The bound shows that the loop costs at most polynomially in and whether or not the simulated run halts.
Formalization Note: The remaining cost budget in the state is the truncated difference , which hypothesis 2 makes an honest difference; the condition is only used to bound the cost of reading the step budget. clockRun is a noncomputable definition (it decides finality classically) used purely as a specification. All numerals in the state are unary.
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.clockLoop_runs (c₀ : Cfg) (k Θ : ℕ) (env : Env) : ∀ (b n : ℕ), b ≤ 3 * k + 1 →
costSum c₀ n ≤ k → (step^[n] c₀).toData.size ≤ Θ →
∃ t ≤ (b + 1) * (clockBodyBound Θ k + 1),
Eval (clockState (step^[n] c₀).toData (.ofNat (k - costSum c₀ n)) (.ofNat b) (.ofNat Θ)
:: env) (.loop clockBody) (clockRun c₀ k Θ b n) t := by sorry