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:
Reusable bisection combinator (
bisectCode): parameterized by any structured probe blockprobe : Code .standard rthat testsmid ∈ [L, R)withinT_probeinstructions and setsresto a positive word whent mid = trueand0otherwise. From any state withL ≤ lo,hi ≤ R, andFits w hi,bisectCodeexecutes within(T_probe + 8) * (Nat.log 2 (hi - lo) + 1) + 2instructions (2instructions whenhi ≤ lo) and leavesCslib.Algorithms.Lean.lowerBound t lo hiin registerlo.Standalone sorted-array lower-bound program (
lowerBoundCode,lowerBoundProgram): searches an arrayarrof words for the first index whose entry is at leastkey, computing[lowerBound (fun i => decide (arr.toArray[i]! < key)) 0 arr.length]in13 * (Nat.log 2 |arr| + 1) + 14instructions at every word widthwwithFits w (|arr| + 3). On sorted arrays (arr.Pairwise (· ≤ ·)), this index splitsarrinto entries< keyand entries≥ key(lowerBound_sorted_spec).
Main definitions #
Cslib.Algorithms.WordRAM.BinarySearch.bisectCode: structured Word-RAM bisection combinator parameterized by a probeCode.Cslib.Algorithms.WordRAM.BinarySearch.LowerBoundInput: input record (key,arr) for array lower-bound search, withLowerBoundInput.encode.Cslib.Algorithms.WordRAM.BinarySearch.Reg.out: output base register (9).Cslib.Algorithms.WordRAM.BinarySearch.lowerBoundCode,lowerBoundProgram: standalone Word-RAM array lower-bound code and uniformProgram .standard 10.
Main results #
Cslib.Algorithms.WordRAM.BinarySearch.bisectCode_exec_tightandbisectCode_exec: logarithmic instruction-count bounds and exact functional agreement withCslib.Algorithms.Lean.lowerBound.Cslib.Algorithms.WordRAM.BinarySearch.lowerBoundCode_execandlowerBoundProgram_computesListInTime:O(log |arr|)Word-RAM array lower-bound execution.Cslib.Algorithms.WordRAM.BinarySearch.lowerBound_sorted_spec: sorted-array partition property of the computed lower-bound index.
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.
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.
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 #
Pure array lower-bound predicate for I: arr[i]! < key.
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.
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).