Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

The bit-query loop answers query mmm about yyy in at most ∣y∣+1|y|+1∣y∣+1 iterations, each of cost quadratic in ∣m∣|m|∣m∣

Proved
MIPRE.Cost.Prog.bitAtLoop_runs

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

computabilitycost-modelmipre-formalizationpolynomial-timesuccinct-descriptions

We work in the ambient cost model: data are binary trees with size\mathrm{size}size the number of nodes, programs form a first-order language with de Bruijn variables, and ρ⊢p⇓tr\rho \vdash p \Downarrow^{t} rρ⊢p⇓tr (Eval ρ p r t) means that ppp evaluated in the environment ρ\rhoρ 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. For m∈{0,1}∗\mathbf m \in \{0,1\}^*m∈{0,1}∗ let val(m)=∑imi2i\mathrm{val}(\mathbf m) = \sum_i \mathbf m_i 2^ival(m)=∑i​mi​2i be its value read least significant bit first (bitsVal); the representation need not be canonical (trailing 000 bits are allowed), and ∣m∣|\mathbf m|∣m∣ is its length.

The bit-query program bitAtProg is the loop loop bitAtBody\mathtt{loop}\ \mathtt{bitAtBody}loop bitAtBody: starting from a state cons(enc(y),enc(m))\mathrm{cons}(\mathrm{enc}(y), \mathrm{enc}(\mathbf m))cons(enc(y),enc(m)) held in variable 000, it repeatedly tests m\mathbf mm for zero and, while it is nonzero, drops the head bit of yyy and decrements m\mathbf mm in binary; it stops with the head bit of yyy when m\mathbf mm reaches zero, and with the out-of-range marker when yyy is exhausted. The answer it must compute is (bitQueryAnswer y m)

ans(y,m)={enc(ym)if m<∣y∣,2‾otherwise,\mathrm{ans}(y, m) = \begin{cases} \mathrm{enc}(y_m) & \text{if } m < |y|,\\ \underline{2} & \text{otherwise,}\end{cases}ans(y,m)={enc(ym​)2​​if m<∣y∣,otherwise,​

where ymy_mym​ is the mmm-th bit of yyy (counting from 000) and 2‾\underline 22​ is the unary numeral 222 (Data.ofNat 2), the out-of-range marker.

The cost of one iteration on a string of encoded size at most sys_ysy​ and an index of LLL bits is bounded by (bitAtIter)

bitAtIter(sy,L)=sy+8L+zeroBound(L)+decBound(L)+20,\mathrm{bitAtIter}(s_y, L) = s_y + 8L + \mathrm{zeroBound}(L) + \mathrm{decBound}(L) + 20,bitAtIter(sy​,L)=sy​+8L+zeroBound(L)+decBound(L)+20,

where zeroBound(L)=(L+1)(4L+9)\mathrm{zeroBound}(L) = (L+1)(4L+9)zeroBound(L)=(L+1)(4L+9) bounds the cost of a zero test and decBound(L)=(L+2)(60L+100)\mathrm{decBound}(L) = (L+2)(60L+100)decBound(L)=(L+2)(60L+100) the cost of a decrement on an index of LLL bits.

Theorem. Let m∈Nm \in \mathbb{N}m∈N, let y,m∈{0,1}∗y, \mathbf m \in \{0,1\}^*y,m∈{0,1}∗ with val(m)=m\mathrm{val}(\mathbf m) = mval(m)=m, let ρ\rhoρ be any environment, and let sy,L∈Ns_y, L \in \mathbb{N}sy​,L∈N with size(enc(y))≤sy\mathrm{size}(\mathrm{enc}(y)) \le s_ysize(enc(y))≤sy​ and ∣m∣=L|\mathbf m| = L∣m∣=L. Then there is a cost ttt such that

t≤(∣y∣+1)⋅bitAtIter(sy,L)andcons(enc(y),enc(m))::ρ  ⊢  bitAtProg  ⇓t  ans(y,m).t \le (|y| + 1)\cdot \mathrm{bitAtIter}(s_y, L) \qquad\text{and}\qquad \mathrm{cons}(\mathrm{enc}(y), \mathrm{enc}(\mathbf m)) :: \rho \;\vdash\; \mathtt{bitAtProg} \;\Downarrow^{t}\; \mathrm{ans}(y, m).t≤(∣y∣+1)⋅bitAtIter(sy​,L)andcons(enc(y),enc(m))::ρ⊢bitAtProg⇓tans(y,m).

This is the loop invariant of the bit-query program, proved by induction on the queried index mmm with the string, the representation of the index and the environment generalized; the corollary bitAtProg_runs instantiates it with the canonical representation m=bits(m)\mathbf m = \mathrm{bits}(m)m=bits(m), sy=size(enc(y))s_y = \mathrm{size}(\mathrm{enc}(y))sy​=size(enc(y)), L=∣m∣L = |m|L=∣m∣ (the bit length of mmm) and the empty environment. Through bitQueryProg_runs and isSuccinctDesc_hardcode it is what lets the recursive compression argument answer bit-queries about an implicitly given string within the succinctness budget of blueprint def:succinct: the factor (∣m∣+1)2(|m|+1)^2(∣m∣+1)2 in that budget is the quadratic growth of bitAtIter\mathrm{bitAtIter}bitAtIter in LLL (bitAtIter_le: bitAtIter(sy,L)≤(sy+534)(L+1)2\mathrm{bitAtIter}(s_y, L) \le (s_y + 534)(L+1)^2bitAtIter(sy​,L)≤(sy​+534)(L+1)2).

Formalization Note The statement is about Eval in an environment whose head is the state and whose tail ρ\rhoρ is arbitrary and inert; the loop rule of the semantics replaces variable 000 by the new state at each iteration and keeps the tail. The index is an arbitrary bit string m\mathbf mm of value mmm (bitsVal mb = m) rather than Nat.bits m, because the decrement may leave trailing zeros; the bounds are in terms of the slack parameters sys_ysy​ (any upper bound on the size of the encoded string) and LLL (the length of the index, which the decrement preserves). Each zero test and decrement copies the index, which is why zeroBound\mathrm{zeroBound}zeroBound and decBound\mathrm{decBound}decBound are quadratic in LLL, and each iteration also copies the remaining string, whence the term sys_ysy​. In the out-of-range case the result 2‾=cons(nil,cons(nil,nil))\underline 2 = \mathrm{cons}(\mathrm{nil}, \mathrm{cons}(\mathrm{nil},\mathrm{nil}))2​=cons(nil,cons(nil,nil)) has size 555. The constants 8L8L8L, 202020, 4L+94L+94L+9 and 60L+10060L+10060L+100 are those of the Lean definitions bitAtIter, zeroBound and decBound.

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.bitAtLoop_runs (m : ℕ) (y mb : BitStr) (hmb : bitsVal mb = m) (env : Env) (sy L : ℕ)
    (hsy : (encode y).size ≤ sy) (hL : mb.length = L) :
    ∃ t ≤ (y.length + 1) * bitAtIter sy L,
      Eval (.cons (encode y) (encode mb) :: env) bitAtProg (bitQueryAnswer y m) t := by sorry
Source
https://github.com/vidick/MIPRE-formalization/blob/70cbf1c52e3d5e1dff1162fdf952c9a9f4bd99ed/MIPRE/Foundations/Cost/BitQuery.lean#L147-L209; 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