The canonicity loop computes the walk within cost linear in the list length times the state size
ProvedMIPRE.Cost.Prog.canonLoop_runsPrograms of the ambient cost model (Prog) use de Bruijn variables, and Eval env p r t is the timed big-step semantics: in the environment (a list of data, variable first) the program halts with result at cost , one unit per evaluation rule plus the size of every value read. The construct iterates on the state held in variable : returns , and the loop stops with result if and continues with state otherwise (a body result stops with ). is the fixed program of the module that performs one step of the canonicity walk on a state . For a datum , counts its nodes ( counting one) and is the length of its right spine, the length of read as a list; a Boolean is encoded as , .
Let be a datum, a Boolean, any environment and with . Then
The bound has the shape budget times size that the format checks of the wrapper decider need (Decider.TimeBoundAt charges ), the budget here being the number of list elements walked. It is the loop inside normBinProg, whose cost bound normBinProg_runs is assembled from it.
Formalization Note. The statement is MIPRE.Cost.Prog.canonLoop_runs. The loop runs in the environment Data.cons d (encode last) :: env with an arbitrary tail env, and the result is encode (canonWalk d last), the encoded Boolean of Data.canonWalk. The parameter S is any upper bound on d.size + 3, so that the cost can be bounded by a quantity fixed before the walk; the bound is (d.spine + 1) * (S + 21) with Data.spine the right-spine length, which the docstring writes as . The cost t is existentially quantified.
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.Tactic.Ring import Definitions.Def_MIPRE_Foundations_Cost_Basic import Definitions.Def_MIPRE_Foundations_Cost_Basic_B import Definitions.Def_MIPRE_Foundations_Cost_Encoding import Definitions.Def_MIPRE_Foundations_Cost_Encoding_B 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_Loops_B import Definitions.Def_MIPRE_Foundations_Cost_Codable import Definitions.Def_MIPRE_Foundations_Cost_Codable_B import Definitions.Def_MIPRE_Foundations_Cost_Unary import Definitions.Def_MIPRE_Foundations_Halting_Descriptions import Definitions.Def_MIPRE_Foundations_Cost_Numeric import Definitions.Def_MIPRE_Foundations_Cost_Numeric_B import Definitions.Def_MIPRE_Foundations_Halting_Lists open MIPRE open MIPRE.Cost open MIPRE.Cost.Prog open MIPRE.Cost.Data
theorem MIPRE.Cost.Prog.canonLoop_runs (d : Data) (last : Bool) (env : Env) (S : ℕ) (hS : d.size + 3 ≤ S) :
∃ t ≤ (d.spine + 1) * (S + 21),
Eval (Data.cons d (encode last) :: env) (.loop canonBody) (encode (canonWalk d last)) t := by sorry