Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

The parsing loop of `parseProg` computes the top of the final stack of the stack machine, in time (∣x∣+1)(S+21)(|x| + 1)(S + 21)(∣x∣+1)(S+21)

Proved
MIPRE.Cost.Prog.parseLoop_runs

by thomasvidick · Oct 15, 2026 · Mathlib 0df444a (Lean v4.33.1)

bit-stringscost-modellist-programsmip-remipre-formalization

Ambient cost model: data are binary trees (Data, size\mathrm{size}size the number of nodes), programs have de Bruijn variables, an environment ρ\rhoρ is a list of data (variable iii reads the iii-th entry), and Eval ρ p r t\mathrm{Eval}\ \rho\ p\ r\ tEval ρ p r t is the timed big-step relation: in ρ\rhoρ, the program ppp halts with result rrr at cost ttt. A list of data sss is the datum list(s)\mathrm{list}(s)list(s), a cons\mathtt{cons}cons-chain ending in nil\mathtt{nil}nil; a bit string encodes as a cons\mathtt{cons}cons-chain of bits. Description strings are postorder serializations of data, read back by a stack machine: a 000 pushes nil\mathtt{nil}nil, a 111 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 xxx from the stack sss. The program parseBody is the loop body of the reader, on a state cons(bits,stack)\mathtt{cons}(\text{bits}, \text{stack})cons(bits,stack): at the end of the input it stops with the top of the stack (nil\mathtt{nil}nil for an empty stack); otherwise it performs one step of the stack machine on the first bit and continues on the remaining bits.

Let xxx be a bit string, sss a list of data, ρ\rhoρ an environment and S∈NS \in \mathbb NS∈N with size(encode x)+size(list s)+2∣x∣≤S\mathrm{size}(\mathrm{encode}\ x) + \mathrm{size}(\mathrm{list}\ s) + 2|x| \le Ssize(encode x)+size(list s)+2∣x∣≤S. Then

∃ t≤(∣x∣+1)(S+21):Eval (cons(encode x,list s)::ρ) (loop parseBody) (headnil(stackRun(x,s))) t.\exists\, t \le (|x| + 1)(S + 21):\qquad \mathrm{Eval}\ \bigl(\mathtt{cons}(\mathrm{encode}\ x, \mathrm{list}\ s) :: \rho\bigr)\ (\mathtt{loop}\ \mathrm{parseBody})\ \bigl(\mathrm{head}_{\mathtt{nil}}(\mathrm{stackRun}(x, s))\bigr)\ t .∃t≤(∣x∣+1)(S+21):Eval (cons(encode x,list s)::ρ) (loop parseBody) (headnil​(stackRun(x,s))) t.

The loop started on the state (remaining bits, current stack) halts with the top of the final stack, within a cost linear in ∣x∣|x|∣x∣ times the size parameter SSS, 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 nil\mathtt{nil}nil (Data.parse x is the case s=[ ]s = [\,]s=[]). The bound has the shape (iterations + 1) times (size parameter + constant) used throughout the loop library, the constant 212121 absorbing the per-step cost of parseBody.

Preamble
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
Formal statement
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
Source
https://github.com/vidick/MIPRE-formalization/blob/70cbf1c52e3d5e1dff1162fdf952c9a9f4bd99ed/MIPRE/Foundations/Halting/Serial.lean#L169-L198; internal lemma of the formalization of the halting reduction (the reader of description strings, in the ambient model)

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, with reuse governed by our licensing terms.

How Prove2Me worksResearch paperProve2Me Team
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me