Documentation

CslibFrontier.Algorithms.Lean.Query.BinarySearch

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 #

Main results #

@[irreducible]

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).

theorem Cslib.Algorithms.Lean.lowerBound_of_le (t : ℕ → Bool) {lo hi : ℕ} (hle : hi ≤ lo) :
lowerBound t lo hi = lo

On an empty interval (hi ≤ lo), lowerBound returns lo.

theorem Cslib.Algorithms.Lean.lowerBound_bounds (t : ℕ → Bool) (lo hi : ℕ) :
lo ≤ lowerBound t lo hi ∧ lowerBound t lo hi ≤ max lo hi

Unconditional range bounds: for any test t, lowerBound t lo hi lies in [lo, max lo hi].

theorem Cslib.Algorithms.Lean.lowerBound_spec (t : ℕ → Bool) {lo hi : ℕ} (hmono : ∀ (m m' : ℕ), lo ≤ m → m ≤ m' → m' < hi → t m' = true → t m = true) :
lo ≤ lowerBound t lo hi ∧ lowerBound t lo hi ≤ max lo hi ∧ (∀ (m : ℕ), lo ≤ m → m < lowerBound t lo hi → t m = true) ∧ ∀ (m : ℕ), lowerBound t lo hi ≤ m → m < hi → t m = false

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.

@[reducible, inline]

Lift ProbeQuery.probe m into FreeM ProbeQuery Bool.

@[simp]
theorem Cslib.Query.ProbeQuery.eval_ask (oracle : {ι : Type} → ProbeQuery ι → ι) (m : ℕ) :
FreeM.eval (fun {ι : Type} => oracle) (ask m) = oracle (probe m)
def Cslib.Query.ProbeQuery.oracleOf (t : ℕ → Bool) {ι : Type} :
ProbeQuery ι → ι

Build a ProbeQuery oracle from a pure predicate ℕ → Bool.

@[simp]
theorem Cslib.Query.ProbeQuery.oracleOf_probe (t : ℕ → Bool) (m : ℕ) :
oracleOf t (probe m) = t m

Every ProbeQuery ι has response type ι = Bool, of cardinality 2.

@[irreducible]
def Cslib.Query.lowerBoundBy {Q : Type → Type} (ask : ℕ → FreeM Q Bool) (lo hi : ℕ) :

Query-monad bisection on [lo, hi) parameterized by a probe sub-program ask : ℕ → FreeM Q Bool for any query signature Q : Type → Type.

@[simp]
theorem Cslib.Query.eval_lowerBoundBy {Q : Type → Type} (oracle : {ι : Type} → Q ι → ι) (ask : ℕ → FreeM Q Bool) (lo hi : ℕ) :
FreeM.eval (fun {ι : Type} => oracle) (lowerBoundBy ask lo hi) = Algorithms.Lean.lowerBound (fun (m : ℕ) => FreeM.eval (fun {ι : Type} => oracle) (ask m)) lo hi

Evaluating lowerBoundBy ask lo hi against oracle computes pure lowerBound for the induced Boolean predicate fun m => (ask m).eval oracle.

theorem Cslib.Query.cost_lowerBoundBy_of_le {Q : Type → Type} {T : Type} [AddMonoid T] (oracle : {ι : Type} → Q ι → ι) (weight : {ι : Type} → Q ι → T) (ask : ℕ → FreeM Q Bool) {lo hi : ℕ} (hle : hi ≤ lo) :
FreeM.cost (fun {ι : Type} => oracle) (fun {ι : Type} => weight) (lowerBoundBy ask lo hi) = 0

On an empty interval (hi ≤ lo), lowerBoundBy incurs zero query cost.

theorem Cslib.Query.countQueries_lowerBoundBy_of_le {Q : Type → Type} (oracle : {ι : Type} → Q ι → ι) (ask : ℕ → FreeM Q Bool) {lo hi : ℕ} (hle : hi ≤ lo) :
FreeM.countQueries (fun {ι : Type} => oracle) (lowerBoundBy ask lo hi) = 0

On an empty interval (hi ≤ lo), lowerBoundBy makes zero queries.

theorem Cslib.Query.cost_lowerBoundBy_le {Q : Type → Type} (oracle : {ι : Type} → Q ι → ι) (weight : {ι : Type} → Q ι → ℕ) (ask : ℕ → FreeM Q Bool) {c lo hi : ℕ} (hc : ∀ (m : ℕ), lo ≤ m → m < hi → FreeM.cost (fun {ι : Type} => oracle) (fun {ι : Type} => weight) (ask m) ≤ c) :
FreeM.cost (fun {ι : Type} => oracle) (fun {ι : Type} => weight) (lowerBoundBy ask lo hi) ≤ c * (Nat.log 2 (hi - lo) + 1)

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).

theorem Cslib.Query.countQueries_lowerBoundBy_le {Q : Type → Type} (oracle : {ι : Type} → Q ι → ι) (ask : ℕ → FreeM Q Bool) {c lo hi : ℕ} (hc : ∀ (m : ℕ), lo ≤ m → m < hi → FreeM.countQueries (fun {ι : Type} => oracle) (ask m) ≤ c) :
FreeM.countQueries (fun {ι : Type} => oracle) (lowerBoundBy ask lo hi) ≤ 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.

@[simp]
theorem Cslib.Query.eval_lowerBoundQ (oracle : {ι : Type} → ProbeQuery ι → ι) (lo hi : ℕ) :
FreeM.eval (fun {ι : Type} => oracle) (lowerBoundQ lo hi) = Algorithms.Lean.lowerBound (fun (m : ℕ) => oracle (ProbeQuery.probe m)) lo hi

Evaluating lowerBoundQ lo hi against oracle computes lowerBound.

theorem Cslib.Query.cost_lowerBoundQ_of_le {T : Type} [AddMonoid T] (oracle : {ι : Type} → ProbeQuery ι → ι) (weight : {ι : Type} → ProbeQuery ι → T) {lo hi : ℕ} (hle : hi ≤ lo) :
FreeM.cost (fun {ι : Type} => oracle) (fun {ι : Type} => weight) (lowerBoundQ lo hi) = 0

On an empty interval (hi ≤ lo), lowerBoundQ incurs zero cost.

theorem Cslib.Query.countQueries_lowerBoundQ_of_le (oracle : {ι : Type} → ProbeQuery ι → ι) {lo hi : ℕ} (hle : hi ≤ lo) :
FreeM.countQueries (fun {ι : Type} => oracle) (lowerBoundQ lo hi) = 0

On an empty interval (hi ≤ lo), lowerBoundQ makes zero queries.

theorem Cslib.Query.cost_lowerBoundQ_le (oracle : {ι : Type} → ProbeQuery ι → ι) (weight : {ι : Type} → ProbeQuery ι → ℕ) {c lo hi : ℕ} (hc : ∀ (m : ℕ), lo ≤ m → m < hi → weight (ProbeQuery.probe m) ≤ c) :
FreeM.cost (fun {ι : Type} => oracle) (fun {ι : Type} => weight) (lowerBoundQ lo hi) ≤ c * (Nat.log 2 (hi - lo) + 1)

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).

theorem Cslib.Query.countQueries_lowerBoundQ_le (oracle : {ι : Type} → ProbeQuery ι → ι) (lo hi : ℕ) :
FreeM.countQueries (fun {ι : Type} => oracle) (lowerBoundQ lo hi) ≤ 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.

theorem Cslib.Query.upperBound_lowerBoundQ :
UpperBound (fun (p : ℕ × ℕ) => lowerBoundQ p.1 p.2) (fun (p : ℕ × ℕ) => p.2 - p.1) fun (n : ℕ) => Nat.log 2 n + 1

Cslib.Query.UpperBound formulation of the ⌊log₂ n⌋ + 1 probe bound for lowerBoundQ.