Documentation

CslibFrontier.DataStructure.SearchTree.PointerMachine.Search

Binary search by a pointer-machine program #

The fixed program search implements Frontier's SearchTree.contains by following left/right links. It returns the matching node in pointer register zero, or null on failure. Data register zero holds the query; register one is temporary. No record is changed or allocated. A scalar predicate implementing strict key comparison is the only required primitive.

Search from pointer register zero for the key in data register zero. Pointer register five is cleared for null tests; data register one is temporary. All other registers are preserved. Both successful and unsuccessful searches terminate at explicit halt instructions.

Search returns a matching node or null and preserves the heap and every register outside its declared cursor, null and temporary registers.

theorem Cslib.DataStructure.SearchTree.PointerMachine.search_correct {M : PointerMachine.Model} {extra regs : ℕ} [LinearOrder M.Value] (lt : M.Test) (compare : ∀ (a b : M.Value), M.evalTest lt a b = decide (a < b)) (x : PointerMachine.State M (layout extra regs)) (t : BinaryTree (Label M extra)) (parent : PointerMachine.Pointer) (rep : Represents x.heap parent t) (cursor : x.ptr 0 = root t) (pc : x.pc = 0) :
∃ (y : PointerMachine.State M (layout extra regs)) (time : ℕ), PointerMachine.Executes (search lt) x y time ∧ time ≤ 6 * searchVisits (x.data 0) (keys t) + 3 ∧ SearchResult x y t

The fixed search program implements SearchTree.contains, with at most six instructions per visited node plus three instructions for setup and termination. The count includes halt and unsuccessful searches. Ordering is needed only to interpret contains as set membership, not for agreement with that functional algorithm.