Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

One iteration of the bit-query walk with a nonzero index: continue with the tail and the decremented index

Proved
MIPRE.Cost.Prog.bitAtBody_succ

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

computabilitycost-modelmipre-formalizationsuccinct-descriptions

We work in the ambient cost model of the formalization. Data are binary trees (Data, built from nil\mathrm{nil}nil and cons\mathrm{cons}cons), and size(d)\mathrm{size}(d)size(d) is the number of nodes of ddd. Programs (Prog) form a small first-order language with de Bruijn variables, and we write ρ⊢p⇓tr\rho \vdash p \Downarrow^{t} rρ⊢p⇓tr for the timed big-step semantics Eval ρ p r t: the program ppp, evaluated in the environment ρ\rhoρ (a list of values, variable 000 first), halts with result rrr at cost ttt. A bit string x∈{0,1}∗x \in \{0,1\}^*x∈{0,1}∗ is encoded as the list enc(x)\mathrm{enc}(x)enc(x) of its bits, enc(b::x)=cons(enc(b),enc(x))\mathrm{enc}(b :: x) = \mathrm{cons}(\mathrm{enc}(b), \mathrm{enc}(x))enc(b::x)=cons(enc(b),enc(x)), and a binary index is such a bit string read least significant bit first.

The bit-query program bitAtProg, which returns the mmm-th bit of a string, is a loop whose body bitAtBody acts on a state cons(enc(y),enc(m))\mathrm{cons}(\mathrm{enc}(y), \mathrm{enc}(\mathbf m))cons(enc(y),enc(m)) made of the remaining string yyy and the remaining binary index m\mathbf mm. The body first checks whether yyy is exhausted; otherwise it calls the zero test isZeroProg on m\mathbf mm and, if m\mathbf mm is nonzero, calls the decrement decProg on m\mathbf mm and signals that the loop should continue with the tail of yyy and the decremented index. This lemma is the exact evaluation of the body in that last case.

Let b∈{0,1}b \in \{0,1\}b∈{0,1} and ys,m,m′∈{0,1}∗y_s, \mathbf m, \mathbf m' \in \{0,1\}^*ys​,m,m′∈{0,1}∗, let tz,td∈Nt_z, t_d \in \mathbb{N}tz​,td​∈N, and assume:

  1. some bit of m\mathbf mm equals 111 (mb.any id = true), i.e. the index is nonzero;
  2. [enc(m)]⊢isZeroProg⇓tzenc(any(m))[\mathrm{enc}(\mathbf m)] \vdash \mathtt{isZeroProg} \Downarrow^{t_z} \mathrm{enc}(\mathrm{any}(\mathbf m))[enc(m)]⊢isZeroProg⇓tz​enc(any(m)): the zero test, run on m\mathbf mm alone, returns the encoded Boolean "some bit of m\mathbf mm is 111" (so here enc(true)=cons(nil,nil)\mathrm{enc}(\mathrm{true}) = \mathrm{cons}(\mathrm{nil},\mathrm{nil})enc(true)=cons(nil,nil)) at cost tzt_ztz​;
  3. [enc(m)]⊢decProg⇓tdenc(m′)[\mathrm{enc}(\mathbf m)] \vdash \mathtt{decProg} \Downarrow^{t_d} \mathrm{enc}(\mathbf m')[enc(m)]⊢decProg⇓td​enc(m′): the decrement program, run on m\mathbf mm alone, returns the bit string m′\mathbf m'm′ at cost tdt_dtd​.

Then, in the one-element environment holding the state cons(enc(b::ys),enc(m))\mathrm{cons}(\mathrm{enc}(b :: y_s), \mathrm{enc}(\mathbf m))cons(enc(b::ys​),enc(m)),

[cons(enc(b::ys),enc(m))]  ⊢  bitAtBody  ⇓ 2 size(enc(m))+tz+td+size(enc(ys))+14  cons(cons(nil,nil), cons(enc(ys),enc(m′))).[\mathrm{cons}(\mathrm{enc}(b :: y_s), \mathrm{enc}(\mathbf m))] \;\vdash\; \mathtt{bitAtBody} \;\Downarrow^{\,2\,\mathrm{size}(\mathrm{enc}(\mathbf m)) + t_z + t_d + \mathrm{size}(\mathrm{enc}(y_s)) + 14}\; \mathrm{cons}\bigl(\mathrm{cons}(\mathrm{nil},\mathrm{nil}),\, \mathrm{cons}(\mathrm{enc}(y_s), \mathrm{enc}(\mathbf m'))\bigr).[cons(enc(b::ys​),enc(m))]⊢bitAtBody⇓2size(enc(m))+tz​+td​+size(enc(ys​))+14cons(cons(nil,nil),cons(enc(ys​),enc(m′))).

The result is the loop's "continue" signal: a nonzero flag cons(nil,nil)\mathrm{cons}(\mathrm{nil},\mathrm{nil})cons(nil,nil) followed by the new state (ys,m′)(y_s, \mathbf m')(ys​,m′), so that the enclosing loop proceeds with the head bit bbb dropped and the index replaced by m′\mathbf m'm′.

Together with the two other cases of the body — an exhausted string (bitAtBody_nil, which stops with the out-of-range marker) and a zero index (bitAtBody_zero, which stops with the head bit) — this is the inductive step of bitAtLoop_runs, the correctness and cost analysis of bitAtProg. That analysis is where the bit-query program of the recursive compression argument gets its cost quadratic in ∣m∣|m|∣m∣, and hence where the succinctness budget (n+1)(∣m∣+1)2(n+1)(|m|+1)^2(n+1)(∣m∣+1)2 of IsSuccinctDesc (blueprint def:succinct) comes from.

Formalization Note The statement is in the timed big-step semantics Eval of MIPRE.Foundations.Cost.Basic: one unit per rule, plus the size of the value for each variable read (var) or literal (const), since values are copied when read. The cost 2 size(enc(m))+tz+td+size(enc(ys))+142\,\mathrm{size}(\mathrm{enc}(\mathbf m)) + t_z + t_d + \mathrm{size}(\mathrm{enc}(y_s)) + 142size(enc(m))+tz​+td​+size(enc(ys​))+14 is exact, not an upper bound: the index is copied twice (once for each call, made through callVar, which binds a copy of a variable as the callee's input), the tail ysy_sys​ once, and the fourteen remaining units are the rule costs of the body — three elims, three lets (the body's own and one inside each call), the unit part of three variable reads, and the five constructor rules building the flag and the two pairs of the result. The hypotheses on isZeroProg and decProg are the runs themselves, with arbitrary costs tz,tdt_z, t_dtz​,td​; the lemma uses neither their specifications nor the fact that m′\mathbf m'm′ represents the predecessor of m\mathbf mm, which enter only in bitAtLoop_runs. The zero test's result is written enc(any(m))\mathrm{enc}(\mathrm{any}(\mathbf m))enc(any(m)) (encode (mb.any id)); hypothesis 1 identifies it with enc(true)\mathrm{enc}(\mathrm{true})enc(true).

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.Linarith
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
import Definitions.Def_MIPRE_Foundations_Cost_BitQuery

open MIPRE
open MIPRE.Cost
open MIPRE.Cost.Prog
Formal statement
theorem MIPRE.Cost.Prog.bitAtBody_succ (b : Bool) (ys mb mb' : BitStr) (hz : mb.any id = true) (tz td : ℕ)
    (hzr : Eval [encode mb] isZeroProg (encode (mb.any id)) tz)
    (hd : Eval [encode mb] decProg (encode mb') td) :
    Eval [.cons (encode (b :: ys)) (encode mb)] bitAtBody
      (.cons (.cons .nil .nil) (.cons (encode ys) (encode mb')))
      (2 * (encode mb).size + tz + td + (encode ys).size + 14) := by sorry
Source
https://github.com/vidick/MIPRE-formalization/blob/70cbf1c52e3d5e1dff1162fdf952c9a9f4bd99ed/MIPRE/Foundations/Cost/BitQuery.lean#L128-L145; internal lemma of the formalization of blueprint def:succinct (the bit-query program), https://vidick.github.io/MIPRE-formalization/blueprint/; [MNY] A. Marks, S. S. Nezhadi, H. Yuen, The recursive compression method for proving undecidability results, manuscript, Definition 2.4

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