One iteration of the clocked loop, first half: finality test, step cost and budget subtraction
ProvedMIPRE.Cost.Machine.clockBody_prefixThe clocked self-interpreter runs the evaluation machine under a cost budget. Its loop state is : the encoded configuration , the remaining cost budget, the remaining step budget and the size guard, the last three as unary numerals. The loop body clockBody tests whether the configuration is final and otherwise, if the step budget is positive, computes the cost of the next step with stepCostProg, subtracts it from the remaining cost budget with the unary subtraction subProg, and then runs the second half clockRest (perform the step, compare the new configuration's size with the guard, and continue or stop). Let denote the environment reached at that point: on top of the loop state and the intermediate values it holds the pair and, at variable , the value of the subtraction (the flagged difference, or when the step cost exceeds the remaining budget).
Let be a configuration that is not final, let , let be an environment, a value and . Suppose that the remainder of the body, the program which stops with if the subtraction failed and otherwise runs clockRest, halts in the environment with result at cost . Then there is a cost
such that clockBody halts in the environment with the same result at cost . Here and .
The lemma is the common prefix of the three outcomes of a non-final iteration with positive step budget in the correctness proof of the clocked loop (clockLoop_runs): continue with the stepped configuration, stop because the guard is exceeded, or stop because the step cost exceeds the remaining budget. It serves the clocked universal machine of blueprint lem:universal-tm.
Formalization Note: The step budget is written because the body's first action on a non-final configuration is to test it against (the exhausted-budget case is clockBody_budget0). clockEnv is the explicit twelve-entry environment of Cost/Universal.lean; the statement is a cost transfer from the second half to the whole body, with and the hypothesis run arbitrary.
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.clockBody_prefix (c : Cfg) (hc : ¬ IsFinal c) (rem b Θ : ℕ) (env : Env) (r : Data)
{t : ℕ} (hrest : Eval (clockEnv c rem b Θ env) (.elim 0 (.cons .nil .nil) clockRest) r t) :
∃ s ≤ t + stepCostBound (Cfg.toData c).size + subBound (stepCost c) rem +
4 * (Cfg.toData c).size + 4 * rem + 30,
Eval (clockState c.toData (.ofNat rem) (.ofNat (b + 1)) (.ofNat Θ) :: env) clockBody r s := by sorry