Unary subtraction with comparison: `subProg` returns the flagged difference , or if
ProvedMIPRE.Cost.Machine.subProg_runsNatural numbers used as counters and budgets inside the self-interpreter are represented in unary: is the list of copies of (so ). The program subProg is a that, on a pair of unary numerals, strips one from each until the first is exhausted or the second runs out first. Its intended value is
a difference flagged as "defined", or the failure value .
For all and every environment , there is a cost
such that evaluating subProg in the environment halts with result at cost .
The clocked universal machine (blueprint lem:universal-tm) uses this program twice per simulated step: to subtract the exact cost of the step from the remaining cost budget, where the failure value signals that the budget is exhausted, and to compare the size of the new configuration with the size guard .
Formalization Note: The program is run with the pair at variable of an environment with arbitrary further entries (the lemma is used inside callVar, which copies the argument on top of the current environment). The bound is quadratic because each of the at most iterations copies both numerals, at cost proportional to ; subBound is monotone in both arguments (subBound_mono), which is how it is used in the cost accounting of the clocked loop.
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.subProg_runs (a : ℕ) : ∀ (b : ℕ) (env : Env),
∃ t ≤ subBound a b,
Eval (Data.cons (.ofNat a) (.ofNat b) :: env) subProg (subResult a b) t := by sorry