The parsing loop of `parseProg` computes the top of the final stack of the stack machine, in time
ProvedMIPRE.Cost.Prog.parseLoop_runsAmbient cost model: data are binary trees (Data, the number of nodes), programs have de Bruijn variables, an environment is a list of data (variable reads the -th entry), and is the timed big-step relation: in , the program halts with result at cost . A list of data is the datum , a -chain ending in ; a bit string encodes as a -chain of bits. Description strings are postorder serializations of data, read back by a stack machine: a pushes , a pops two entries and pushes their pair (a stack with fewer than two entries is left alone); stackRun x s is the stack after running the machine over from the stack . The program parseBody is the loop body of the reader, on a state : at the end of the input it stops with the top of the stack ( for an empty stack); otherwise it performs one step of the stack machine on the first bit and continues on the remaining bits.
Let be a bit string, a list of data, an environment and with . Then
The loop started on the state (remaining bits, current stack) halts with the top of the final stack, within a cost linear in times the size parameter , which dominates the sizes along the run. This is the loop invariant of parseProg_runs, the reader with which the compressor's decider reads the description it is handed.
Formalization Note. The theorem is MIPRE.Cost.Prog.parseLoop_runs (x : BitStr) (s : List Data) (env : Env) (S : ℕ) (hS : (encode x : Data).size + (list s).size + 2 * x.length ≤ S). Env is List Data, Data.list the cons-chain of a list, .loop parseBody the loop construct of Prog, and (stackRun x s).headD Data.nil is List.headD with default (Data.parse x is the case ). The bound has the shape (iterations + 1) times (size parameter + constant) used throughout the loop library, the constant absorbing the per-step cost of parseBody.
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.Linarith 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 import Definitions.Def_MIPRE_Foundations_Halting_Serial open MIPRE open MIPRE.Cost open MIPRE.Cost.Prog open MIPRE.Cost.Data
theorem MIPRE.Cost.Prog.parseLoop_runs (x : BitStr) (s : List Data) (env : Env) (S : ℕ)
(hS : (encode x : Data).size + (list s).size + 2 * x.length ≤ S) :
∃ t ≤ (x.length + 1) * (S + 21),
Eval (Data.cons (encode x) (list s) :: env) (.loop parseBody) ((stackRun x s).headD Data.nil) t := by sorry