Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

First phase of binary decrement: flipping the trailing zeros onto an accumulator, within cost (j+1)(S+15)(j+1)(S+15)(j+1)(S+15)

Proved
MIPRE.Cost.Prog.decLoop_runs

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

cost-modelmipre-formalizationpolynomial-timesuccinct-descriptions

In the ambient cost model (binary-tree data, size\mathrm{size}size = number of nodes, bit strings encoded as cons-chains of bits, numerals written least significant bit first, timed semantics Eval(env,p,r,t)\mathrm{Eval}(\mathrm{env}, p, r, t)Eval(env,p,r,t) charging one unit per rule plus the size of every value read), the decrement program decProg works in two phases. This lemma analyzes the first phase, the loop loop decBody, whose state is a pair (bits,acc)(\mathrm{bits}, \mathrm{acc})(bits,acc): as long as the leading bit of bits\mathrm{bits}bits is 000 it is removed and a 111 is pushed onto the accumulator; at the first 111 bit the loop stops with the pair (0⋅acc,rest)(0 \cdot \mathrm{acc}, \mathrm{rest})(0⋅acc,rest), where rest\mathrm{rest}rest is what follows that bit.

Let j,i∈Nj, i \in \mathbb{N}j,i∈N, let r∈{0,1}∗r \in \{0,1\}^*r∈{0,1}∗, let env\mathrm{env}env be any environment and let S∈NS \in \mathbb{N}S∈N satisfy

2j+size(encode(r))+4  +  (4i+1)  +  2j  ≤  S.2j + \mathrm{size}(\mathrm{encode}(r)) + 4 \;+\; (4i + 1) \;+\; 2j \;\le\; S.2j+size(encode(r))+4+(4i+1)+2j≤S.

Here 2j+size(encode(r))+42j + \mathrm{size}(\mathrm{encode}(r)) + 42j+size(encode(r))+4 is exactly the size of the encoded input string 0j1r0^j 1 r0j1r, 4i+14i+14i+1 is exactly the size of the encoded accumulator 1i1^i1i, and the additional 2j2j2j covers the growth of the state during the loop (each converted bit removes two nodes from the string and adds four to the accumulator). Then there is a cost ttt with

t≤(j+1)(S+15)andEval(cons(encode(0j1r),encode(1i))::env, loop decBody, cons(encode(0 1i+j),encode(r)), t):t \le (j+1)(S+15) \quad\text{and}\quad \mathrm{Eval}\bigl(\texttt{cons}(\mathrm{encode}(0^j 1 r), \mathrm{encode}(1^i)) :: \mathrm{env},\ \texttt{loop decBody},\ \texttt{cons}(\mathrm{encode}(0\,1^{i+j}), \mathrm{encode}(r)),\ t\bigr):t≤(j+1)(S+15)andEval(cons(encode(0j1r),encode(1i))::env, loop decBody, cons(encode(01i+j),encode(r)), t):

started on the state (0j1r,1i)(0^j 1 r, 1^i)(0j1r,1i), the loop halts with the pair (0 1i+j,r)(0\,1^{i+j}, r)(01i+j,r).

The lemma is the inductive core of decProg_runs, the cost analysis of binary decrement (the second phase reverses 0 1j0\,1^{j}01j onto rrr to obtain 1j0r1^j 0 r1j0r); decrement, in turn, is one of the two arithmetic primitives of the bit-query program behind succinct descriptions.

Formalization Note. The statement is about the loop Prog.loop decBody of MIPRE.Cost.Prog.decProg, with the generalized accumulator 1i1^i1i needed for the induction. The slack hypothesis on SSS is stated with the explicit sizes 2j+size(encode r)+42j + \mathrm{size}(\mathrm{encode}\,r) + 42j+size(encoder)+4 and 4i+14i+14i+1 so that the bound (j+1)(S+15)(j+1)(S+15)(j+1)(S+15) is a single product; each iteration costs at most S+15S + 15S+15 because the loop state is copied by var reads whose cost is its size. The strings are lists of Booleans (List Bool) with the least significant bit first, and 0j1r0^j 1 r0j1r is List.replicate j false ++ true :: rest.

Preamble
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 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_Numeric

open MIPRE
open MIPRE.Cost
open MIPRE.Cost.Prog
Formal statement
theorem MIPRE.Cost.Prog.decLoop_runs (j : ℕ) (rest : BitStr) (i : ℕ) (env : Env) (S : ℕ)
    (hS : 2 * j + (encode rest).size + 4 + (4 * i + 1) + 2 * j ≤ S) :
    ∃ t ≤ (j + 1) * (S + 15),
      Eval (.cons (encode (List.replicate j false ++ true :: rest))
          (encode (List.replicate i true)) :: env) (.loop decBody)
        (.cons (encode (false :: List.replicate (i + j) true)) (encode rest)) t := by sorry
Source
https://github.com/vidick/MIPRE-formalization/blob/70cbf1c52e3d5e1dff1162fdf952c9a9f4bd99ed/MIPRE/Foundations/Cost/Numeric.lean#L237-L272; internal lemma of the formalization of blueprint def:succinct (first phase of the binary decrement used by the bit-indexing program `bitAtProg`)

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