Binary Search in the Threshold-Probe Query Model #
Cslib.Algorithms.Lean.lowerBound finds, by bisection, the first index of [lo, hi) at which a
Boolean predicate t : ℕ → Bool fails, assuming t holds on an initial segment of [lo, hi) and
fails on the rest. Cslib.Query.lowerBoundBy expresses bisection over any query signature
Q : Type → Type in FreeM Q ℕ (Cslib.Algorithms.Lean.Query.FreeM), and
Cslib.Query.lowerBoundQ specializes it to Cslib.Query.ProbeQuery, where pure index arithmetic is
uncharged and oracle probes enter only through FreeM.lift.
Main definitions #
Cslib.Algorithms.Lean.lowerBound: pure bisection on[lo, hi).Cslib.Query.ProbeQuery: threshold-probe query signature (probe (m : ℕ) : ProbeQuery Bool).Cslib.Query.ProbeQuery.ask,Cslib.Query.ProbeQuery.oracleOf: single-probe helper and oracle constructor fromℕ → Bool.Cslib.Query.lowerBoundBy,Cslib.Query.lowerBoundQ: bisection on[lo, hi)inFreeM Q ℕandFreeM ProbeQuery ℕ.
Main results #
Cslib.Algorithms.Lean.lowerBound_bounds:lo ≤ lowerBound t lo hi ≤ max lo hifor everyt.Cslib.Algorithms.Lean.lowerBound_spec: iftis antitone on[lo, hi), the resultksatisfieslo ≤ k ≤ max lo hi,tholds on[lo, k)and fails on[k, hi).Cslib.Query.eval_lowerBoundBy,Cslib.Query.eval_lowerBoundQ:FreeM.evalagrees with purelowerBound.Cslib.Query.cost_lowerBoundBy_le,Cslib.Query.cost_lowerBoundQ_le: if every probe in[lo, hi)has cost at mostc,FreeM.costis at mostc * (Nat.log 2 (hi - lo) + 1)(0whenhi ≤ lo).Cslib.Query.countQueries_lowerBoundBy_le,Cslib.Query.countQueries_lowerBoundQ_le,Cslib.Query.upperBound_lowerBoundQ: bisection makes at mostNat.log 2 (hi - lo) + 1queries (0whenhi ≤ lo).
Pure bisection for the boundary of a Boolean test on [lo, hi): probe the midpoint
m = (lo + hi) / 2 and continue on [m + 1, hi) if t m holds and on [lo, m) otherwise.
When t holds on an initial segment of [lo, hi), the result is the first index where it fails
(hi if none, and lo when hi ≤ lo).
On an empty interval (hi ≤ lo), lowerBound returns lo.
Unconditional range bounds: for any test t, lowerBound t lo hi lies in [lo, max lo hi].
Correctness of lowerBound: if t is antitone on [lo, hi) (once it fails, it keeps
failing), then the result k lies in [lo, max lo hi], t holds on [lo, k) and fails on
[k, hi).
Threshold-probe query signature: ProbeQuery.probe m tests a predicate at m : ℕ, returning
Bool.
- probe (m : ℕ) : ProbeQuery Bool
Probe the Boolean predicate at index
m.
Lift ProbeQuery.probe m into FreeM ProbeQuery Bool.
Build a ProbeQuery oracle from a pure predicate ℕ → Bool.
Every ProbeQuery ι has response type ι = Bool, of cardinality 2.
Evaluating lowerBoundBy ask lo hi against oracle computes pure lowerBound for the induced
Boolean predicate fun m => (ask m).eval oracle.
On an empty interval (hi ≤ lo), lowerBoundBy makes zero queries.
Weighted query-cost bound for lowerBoundBy. If every probe sub-program ask m for
m ∈ [lo, hi) has weighted cost at most c, then lowerBoundBy ask lo hi has total query cost at
most c * (Nat.log 2 (hi - lo) + 1).
Query-count bound for lowerBoundBy. If every probe sub-program ask m for m ∈ [lo, hi)
makes at most c queries, then lowerBoundBy ask lo hi makes at most
c * (Nat.log 2 (hi - lo) + 1) queries.
Query-monad bisection on [lo, hi) over ProbeQuery.
Evaluating lowerBoundQ lo hi against oracle computes lowerBound.
On an empty interval (hi ≤ lo), lowerBoundQ incurs zero cost.
On an empty interval (hi ≤ lo), lowerBoundQ makes zero queries.
Weighted query-cost bound for lowerBoundQ. If every probe at m ∈ [lo, hi) has weight at
most c, then lowerBoundQ lo hi has total query cost at most c * (Nat.log 2 (hi - lo) + 1).
Query-count bound for lowerBoundQ. lowerBoundQ lo hi makes at most
Nat.log 2 (hi - lo) + 1 threshold probes against any oracle.
Cslib.Query.UpperBound formulation of the ⌊log₂ n⌋ + 1 probe bound for lowerBoundQ.