Documentation

CslibFrontier.Algorithms.WordRAM.BinarySearch

Binary Search on the Word RAM #

This module implements overflow-safe bisection (mid = lo + ((hi - lo) >>> 1)) on the standard Word RAM in two forms:

  1. Reusable bisection combinator (bisectCode): parameterized by any structured probe block probe : Code .standard r that tests mid ∈ [L, R) within T_probe instructions and sets res to a positive word when t mid = true and 0 otherwise. From any state with L ≤ lo, hi ≤ R, and Fits w hi, bisectCode executes within (T_probe + 8) * (Nat.log 2 (hi - lo) + 1) + 2 instructions (2 instructions when hi ≤ lo) and leaves Cslib.Algorithms.Lean.lowerBound t lo hi in register lo.

  2. Standalone sorted-array lower-bound program (lowerBoundCode, lowerBoundProgram): searches an array arr of words for the first index whose entry is at least key, computing [lowerBound (fun i => decide (arr.toArray[i]! < key)) 0 arr.length] in 13 * (Nat.log 2 |arr| + 1) + 14 instructions at every word width w with Fits w (|arr| + 3). On sorted arrays (arr.Pairwise (· ≤ ·)), this index splits arr into entries < key and entries ≥ key (lowerBound_sorted_spec).

Main definitions #

Main results #

Parameterized Word-RAM bisection combinator #

Structured Word-RAM bisection on [lo, hi): while lo < hi, computes the overflow-safe midpoint mid ← lo + ((hi - lo) >>> 1), runs probe (which tests mid and writes a positive word to res for true or 0 for false), and updates lo ← mid + 1 or hi ← mid. Leaves Cslib.Algorithms.Lean.lowerBound in lo (and hi = lo when lo ≤ hi). Requires registers zero and one holding 0 and 1.

theorem Cslib.Algorithms.WordRAM.BinarySearch.bisectCode_exec_tight {r w T_probe L R : ℕ} {lo hi mid res zero one : Fin r} (hd : [lo, hi, mid, res, zero, one].Nodup) {probe : WordRAM.Code WordRAM.Basis.standard r} {Inv : WordRAM.Data r → Prop} (t : ℕ → Bool) (hz : ∀ (s : WordRAM.Data r), Inv s → s.regs zero = 0) (ho : ∀ (s : WordRAM.Data r), Inv s → s.regs one = 1) (hset_lo : ∀ (s : WordRAM.Data r) (v : ℕ), Inv s → Inv (s.set lo v)) (hset_hi : ∀ (s : WordRAM.Data r) (v : ℕ), Inv s → Inv (s.set hi v)) (hset_mid : ∀ (s : WordRAM.Data r) (v : ℕ), Inv s → Inv (s.set mid v)) (hprobe : ∀ (s : WordRAM.Data r), Inv s → L ≤ s.regs mid → s.regs mid < R → ∃ (s' : WordRAM.Data r), WordRAM.ExecWithin w probe s s' T_probe ∧ Inv s' ∧ s'.regs lo = s.regs lo ∧ s'.regs hi = s.regs hi ∧ s'.regs mid = s.regs mid ∧ (0 < s'.regs res ↔ t (s.regs mid) = true)) (x : WordRAM.Data r) (hx : Inv x) (hL : L ≤ x.regs lo) (hR : x.regs hi ≤ R) (hfit_hi : WordRAM.Fits w (x.regs hi)) :
∃ (y : WordRAM.Data r), WordRAM.ExecWithin w (bisectCode lo hi mid res zero one probe) x y ((T_probe + 8) * Nat.log 2 (2 * (x.regs hi - x.regs lo)) + 2) ∧ Inv y ∧ y.regs lo = Lean.lowerBound t (x.regs lo) (x.regs hi) ∧ (x.regs lo ≤ x.regs hi → y.regs hi = y.regs lo)

Tight logarithmic execution bound for bisectCode. Runs within (T_probe + 8) * Nat.log 2 (2 * (hi - lo)) + 2 instructions (2 instructions when hi ≤ lo), preserving Inv and setting lo to lowerBound t lo hi.

theorem Cslib.Algorithms.WordRAM.BinarySearch.bisectCode_exec {r w T_probe L R : ℕ} {lo hi mid res zero one : Fin r} (hd : [lo, hi, mid, res, zero, one].Nodup) {probe : WordRAM.Code WordRAM.Basis.standard r} {Inv : WordRAM.Data r → Prop} (t : ℕ → Bool) (hz : ∀ (s : WordRAM.Data r), Inv s → s.regs zero = 0) (ho : ∀ (s : WordRAM.Data r), Inv s → s.regs one = 1) (hset_lo : ∀ (s : WordRAM.Data r) (v : ℕ), Inv s → Inv (s.set lo v)) (hset_hi : ∀ (s : WordRAM.Data r) (v : ℕ), Inv s → Inv (s.set hi v)) (hset_mid : ∀ (s : WordRAM.Data r) (v : ℕ), Inv s → Inv (s.set mid v)) (hprobe : ∀ (s : WordRAM.Data r), Inv s → L ≤ s.regs mid → s.regs mid < R → ∃ (s' : WordRAM.Data r), WordRAM.ExecWithin w probe s s' T_probe ∧ Inv s' ∧ s'.regs lo = s.regs lo ∧ s'.regs hi = s.regs hi ∧ s'.regs mid = s.regs mid ∧ (0 < s'.regs res ↔ t (s.regs mid) = true)) (x : WordRAM.Data r) (hx : Inv x) (hL : L ≤ x.regs lo) (hR : x.regs hi ≤ R) (hfit_hi : WordRAM.Fits w (x.regs hi)) :
∃ (y : WordRAM.Data r), WordRAM.ExecWithin w (bisectCode lo hi mid res zero one probe) x y ((T_probe + 8) * (Nat.log 2 (x.regs hi - x.regs lo) + 1) + 2) ∧ Inv y ∧ y.regs lo = Lean.lowerBound t (x.regs lo) (x.regs hi) ∧ (x.regs lo ≤ x.regs hi → y.regs hi = y.regs lo)

Logarithmic execution bound for bisectCode. Runs within (T_probe + 8) * (Nat.log 2 (hi - lo) + 1) + 2 instructions, preserving Inv and setting lo to lowerBound t lo hi.

Standalone sorted-array lower-bound program #

Input to standalone Word-RAM array lower-bound search: a target key and a word array.

  • key : ℕ

    Target key to locate.

  • arr : List ℕ

    Word array searched by bisection.

Instances For

    Flat word-list encoding of LowerBoundInput: key :: arr, so State.init I.encode places |arr| + 1 at mem[0], key at mem[1], and arr at [2, 2 + |arr|).

    Pure array lower-bound predicate for I: arr[i]! < key.

    theorem Cslib.Algorithms.WordRAM.BinarySearch.lowerBound_sorted_spec (I : LowerBoundInput) (hsort : List.Pairwise (fun (x1 x2 : ℕ) => x1 ≤ x2) I.arr) :
    I.result ≤ I.arr.length ∧ (∀ (i : ℕ) (hi : i < I.result), I.arr[i] < I.key) ∧ ∀ (i : ℕ) (hi : i < I.arr.length), I.result ≤ i → I.key ≤ I.arr[i]

    On a sorted (Pairwise (· ≤ ·)) array, I.result lies in [0, |I.arr|], every element before I.result is strictly below I.key, and every element from I.result onward is at least I.key.

    @[reducible, inline]

    Output base register (9) for lowerBoundCode and lowerBoundProgram.

    Structured 10-register Word-RAM program computing [I.result] at Reg.out.

    Uniform Basis.standard Word-RAM program for array lower-bound search.

    lowerBoundCode executes within 13 * (Nat.log 2 |I.arr| + 1) + 13 instructions from Data.init I.encode whenever Fits w (|I.arr| + 3), writing [I.result] as a length-prefixed output array at y.regs Reg.out = 2 + |I.arr| and preserving I.arr at [2, 2 + |I.arr|).

    lowerBoundProgram computes [I.result] in 13 * (Nat.log 2 |I.arr| + 1) + 14 instructions at every word width w with Fits w (|I.arr| + 3).