The bit-query loop answers query about in at most iterations, each of cost quadratic in
ProvedMIPRE.Cost.Prog.bitAtLoop_runsWe work in the ambient cost model: data are binary trees with the number of nodes, programs form a first-order language with de Bruijn variables, and (Eval ρ p r t) means that evaluated in the environment halts with result at cost . A bit string is encoded as the list of its bits. For let be its value read least significant bit first (bitsVal); the representation need not be canonical (trailing bits are allowed), and is its length.
The bit-query program bitAtProg is the loop : starting from a state held in variable , it repeatedly tests for zero and, while it is nonzero, drops the head bit of and decrements in binary; it stops with the head bit of when reaches zero, and with the out-of-range marker when is exhausted. The answer it must compute is (bitQueryAnswer y m)
where is the -th bit of (counting from ) and is the unary numeral (Data.ofNat 2), the out-of-range marker.
The cost of one iteration on a string of encoded size at most and an index of bits is bounded by (bitAtIter)
where bounds the cost of a zero test and the cost of a decrement on an index of bits.
Theorem. Let , let with , let be any environment, and let with and . Then there is a cost such that
This is the loop invariant of the bit-query program, proved by induction on the queried index with the string, the representation of the index and the environment generalized; the corollary bitAtProg_runs instantiates it with the canonical representation , , (the bit length of ) 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 in that budget is the quadratic growth of in (bitAtIter_le: ).
Formalization Note The statement is about Eval in an environment whose head is the state and whose tail is arbitrary and inert; the loop rule of the semantics replaces variable by the new state at each iteration and keeps the tail. The index is an arbitrary bit string of value (bitsVal mb = m) rather than Nat.bits m, because the decrement may leave trailing zeros; the bounds are in terms of the slack parameters (any upper bound on the size of the encoded string) and (the length of the index, which the decrement preserves). Each zero test and decrement copies the index, which is why and are quadratic in , and each iteration also copies the remaining string, whence the term . In the out-of-range case the result has size . The constants , , and are those of the Lean definitions bitAtIter, zeroBound and decBound.
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
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