List reversal onto an accumulator runs in time in the ambient cost model
ProvedMIPRE.Cost.Prog.revOntoProg_runsIn the ambient cost model of the formalization, data are binary trees built from and , and is the number of nodes of a tree ( counting one). A finite list of data is represented as the -chain (Data.list), so that . Programs are terms of a small first-order language with de Bruijn variables, and the timed big-step semantics (Eval) states that the program , run in the environment (a list of values, variable first), halts with result at cost : each rule costs one unit, and reading a variable additionally costs the size of the value read.
The program revOntoProg is a single loop whose state, held in variable , is a pair : an iteration on a non-empty continues with the state , and on the loop stops with the result .
Statement. Let and be lists of data, let be an arbitrary environment, and let satisfy . Then there is a cost such that
where is the length of and is list concatenation.
This is a primitive of the closure library of the cost model (its second part, on loops). Plain reversal revProg is this program started with an empty accumulator, and the binary decrement decProg of the numeric library uses revOntoProg as its second phase, to put the flipped low-order bits back in front of the untouched high-order bits. The decrement is in turn the index-walking step of the bit-query program bitAtProg, whose cost fixes the time budget in the definition of a succinct description (IsSuccinctDesc, blueprint def:succinct).
Formalization Note: The bound is stated with a free parameter dominating the sizes involved, in the form common to the loop library, so that it is a single atom for linear arithmetic during the induction; the environment below the loop state is arbitrary, as needed when the loop sits under let_ binders. The constants are those of the model: an iteration on with accumulator costs exactly (including the unit of the loop rule), and the final test costs .
import Mathlib.Algebra.Polynomial.Eval.Defs import Mathlib.Data.List.Basic import Mathlib.Data.Nat.Bits import Mathlib.Data.Nat.Notation import Mathlib.Data.Nat.Size 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 open MIPRE open MIPRE.Cost open MIPRE.Cost.Prog
theorem MIPRE.Cost.Prog.revOntoProg_runs (l acc : List Data) (env : Env) (S : ℕ)
(hS : (Data.list l).size + (Data.list acc).size ≤ S) :
∃ t ≤ (l.length + 1) * (S + 12),
Eval (.cons (.list l) (.list acc) :: env) revOntoProg (.list (l.reverse ++ acc)) t := by sorry