One iteration of the bit-query walk with a nonzero index: continue with the tail and the decremented index
ProvedMIPRE.Cost.Prog.bitAtBody_succWe work in the ambient cost model of the formalization. Data are binary trees (Data, built from and ), and is the number of nodes of . Programs (Prog) form a small first-order language with de Bruijn variables, and we write for the timed big-step semantics Eval ρ p r t: the program , evaluated in the environment (a list of values, variable first), halts with result at cost . A bit string is encoded as the list of its bits, , and a binary index is such a bit string read least significant bit first.
The bit-query program bitAtProg, which returns the -th bit of a string, is a loop whose body bitAtBody acts on a state made of the remaining string and the remaining binary index . The body first checks whether is exhausted; otherwise it calls the zero test isZeroProg on and, if is nonzero, calls the decrement decProg on and signals that the loop should continue with the tail of and the decremented index. This lemma is the exact evaluation of the body in that last case.
Let and , let , and assume:
- some bit of equals (
mb.any id = true), i.e. the index is nonzero; - : the zero test, run on alone, returns the encoded Boolean "some bit of is " (so here ) at cost ;
- : the decrement program, run on alone, returns the bit string at cost .
Then, in the one-element environment holding the state ,
The result is the loop's "continue" signal: a nonzero flag followed by the new state , so that the enclosing loop proceeds with the head bit dropped and the index replaced by .
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 , and hence where the succinctness budget 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 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 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 ; the lemma uses neither their specifications nor the fact that represents the predecessor of , which enter only in bitAtLoop_runs. The zero test's result is written (encode (mb.any id)); hypothesis 1 identifies it with .
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.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