Documentation

CslibFrontier.DataStructure.BTree.WordRAM.Lookup

Word-RAM B-Tree Lookup (O(t log_t n)) #

Root-to-leaf B-tree lookup code lookupCode and complete halting flat program lookupProgram over Cslib.WordRAM.Basis.standard (20 registers), computing BTree.lookup k tr in at most (16 * t + 21) * tr.height + 16 (O(t log_t n)) instructions without modifying memory while preserving Rep.

Main definitions #

Main results #

Look up key rKey in the arena at rBase of minimum degree rDeg, setting rFound (1 or 0) and rVal (found value or 0) without modifying memory.

theorem Cslib.DataStructure.BTree.WordRAM.lookupCode_spec {w base capNodes t k : ℕ} {tr : BTree ℕ ℕ} {x : WordRAM.Data 20} (hr : Rep base capNodes t w tr x) (hkey : x.regs Reg.rKey = k) :
∃ (y : WordRAM.Data 20), WordRAM.ExecWithin w lookupCode x y ((16 * t + 21) * tr.height + 16) ∧ Rep base capNodes t w tr y ∧ y.mem = x.mem ∧ (y.regs Reg.rFound = if (lookup k tr).isSome = true then 1 else 0) ∧ y.regs Reg.rVal = (lookup k tr).getD 0

lookupCode evaluates BTree.lookup k tr in at most (16 * t + 21) * tr.height + 16 standard Word-RAM instructions without modifying memory, preserving Rep.

theorem Cslib.DataStructure.BTree.WordRAM.lookupCode_log_spec {w base capNodes t k : ℕ} {tr : BTree ℕ ℕ} {x : WordRAM.Data 20} (hr : Rep base capNodes t w tr x) (hkey : x.regs Reg.rKey = k) :
∃ (y : WordRAM.Data 20), WordRAM.ExecWithin w lookupCode x y ((16 * t + 21) * (Nat.log t ((tr.size + 1) / 2) + 1) + 16) ∧ Rep base capNodes t w tr y ∧ y.mem = x.mem ∧ (y.regs Reg.rFound = if (lookup k tr).isSome = true then 1 else 0) ∧ y.regs Reg.rVal = (lookup k tr).getD 0

Logarithmic O(t log_t n) Word-RAM instruction bound for lookupCode.

Complete flat Word-RAM lookup program (lookupCode followed by halt rVal).

theorem Cslib.DataStructure.BTree.WordRAM.lookupProgram_spec {w base capNodes t k : ℕ} {tr : BTree ℕ ℕ} {x : WordRAM.Data 20} (hr : Rep base capNodes t w tr x) (hkey : x.regs Reg.rKey = k) :
∃ (y : WordRAM.Data 20) (steps : ℕ), WordRAM.Runs w lookupProgram { pc := 0, data := x } { pc := lookupCode.size, data := y } steps ∧ WordRAM.step w lookupProgram { pc := lookupCode.size, data := y } = WordRAM.Outcome.halt ((lookup k tr).getD 0) ∧ steps + 1 ≤ (16 * t + 21) * (Nat.log t ((tr.size + 1) / 2) + 1) + 17 ∧ Rep base capNodes t w tr y ∧ y.mem = x.mem ∧ (y.regs Reg.rFound = if (lookup k tr).isSome = true then 1 else 0) ∧ y.regs Reg.rVal = (lookup k tr).getD 0

Complete halting flat-program specification of lookupProgram: runs in at most (16 * t + 21) * (Nat.log t ((tr.size + 1) / 2) + 1) + 17 Word-RAM instructions (charging the final halt), halting with (BTree.lookup k tr).getD 0 in rVal, the hit/miss bit in rFound (distinguishing some 0 from none), unchanged memory y.mem = x.mem, and preserved Rep.