Documentation

CslibFrontier.DataStructure.SearchTree.PointerMachine.Seek

Searching with a parent cursor and comparison path #

seek follows the ordinary binary-search decisions while retaining the immediate parent in register two, including when the search reaches an empty child. Its ghost context records ancestors and the inequalities that selected each child, for subsequent insertion proofs. The program is read-only and uses no machine stack. This is an original implementation of Frontier's functional search, with explicit pointer-machine instruction accounting.

Search from cursor zero for query data register zero, retaining the parent in pointer two. Pointer five is null scratch and data one is comparison scratch.

Every ancestor frame records the strict comparison that selected its focused child.

theorem Cslib.DataStructure.SearchTree.PointerMachine.SearchPath.vacant {M : PointerMachine.Model} {extra regs : ℕ} [LinearOrder M.Value] (lt : M.Test) (compare : ∀ (a b : M.Value), M.evalTest lt a b = decide (a < b)) (s : PointerMachine.State M (layout extra regs)) (cs : List (Frame M extra)) (path : SearchPath (s.data 0) cs) (rep : Represents s.heap none (plug cs BinaryTree.nil)) (parent : s.ptr 2 = contextParent cs) (rootReg : s.ptr 1 = root (plug cs BinaryTree.nil)) :
have parentKey := (Option.map (fun (n : PointerMachine.Record M (layout extra regs)) => n.data 0) (s.deref 2)).getD (s.data 2); have side := if M.evalTest lt (s.data 0) parentKey = true then 0 else 1; (match s.ptr 2 with | none => s.ptr 1 = none | some a => ∃ (par : PointerMachine.Record M (layout extra regs)), s.heap[a]? = some par ∧ par.ptr (if M.evalTest lt (s.data 0) (par.data 0) = true then 0 else 1) = none) ∧ match cs, path, rep, parent, rootReg with | [], path, rep, parent, rootReg => True | Frame.left label right :: tail, path, rep, parent, rootReg => side = 0 | Frame.right left label :: tail, path, rep, parent, rootReg => side = 1

An unsuccessful search path selects a vacant parent slot. The concrete comparison with that parent's key chooses exactly the side recorded by the ghost context.

structure Cslib.DataStructure.SearchTree.PointerMachine.SeekResult {M : PointerMachine.Model} {extra regs : ℕ} [LinearOrder M.Value] (x y : PointerMachine.State M (layout extra regs)) (whole focus : BinaryTree (Label M extra)) (cs : List (Frame M extra)) :

A search focus and its ancestor context, with the comparison decisions and register frame. The context is mathematical proof data, not storage used by the machine.

  • heap : y.heap = x.heap

    No record is modified or allocated.

  • data (i : Fin (layout extra regs).dataRegs) : i ≠ 1 → y.data i = x.data i

    Only comparison scratch data register one may change.

  • ptr (i : Fin (layout extra regs).ptrRegs) : i ≠ 0 → i ≠ 2 → i ≠ 5 → y.ptr i = x.ptr i

    Only cursor, parent and null scratch pointers may change.

  • cursor : y.ptr 0 = root focus

    The cursor identifies the final focus.

  • parent : y.ptr 2 = contextParent cs

    The parent survives even when the focus is empty.

  • reconstruction : whole = plug cs focus

    The original whole tree is recovered from the returned focus and context.

  • rep : Represents y.heap (contextParent cs) focus

    The focused subtree retains its complete heap representation.

  • context : ContextRep y.heap (root focus) cs

    All ancestor links and sibling subtrees retain their representation.

  • path : SearchPath (x.data 0) cs

    Each descent follows the query comparison at its ancestor.

  • stop : match focus, ⋯, ⋯, ⋯, ⋯ with | BinaryTree.nil, cursor, reconstruction, rep, context => True | BinaryTree.node a left right, cursor, reconstruction, rep, context => a.data 0 = x.data 0

    The focus is empty or its root key equals the query.

  • found : Option.isSome (y.ptr 0) = contains (x.data 0) (keys whole)

    A nonnull focus agrees with the existing functional search.

theorem Cslib.DataStructure.SearchTree.PointerMachine.seek_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)) (rep : Represents x.heap none t) (cursor : x.ptr 0 = root t) (pc : x.pc = 0) :
∃ (y : PointerMachine.State M (layout extra regs)) (time : ℕ) (cs : List (Frame M extra)) (focus : BinaryTree (Label M extra)), PointerMachine.Executes (seek lt) x y time ∧ time ≤ 7 * searchVisits (x.data 0) (keys t) + 4 ∧ SeekResult x y t focus cs

Parent-preserving search uses at most seven instructions per visited node plus four for initialization and termination, including halt. It returns the entire comparison path; no ordering premise is needed for agreement with the functional search algorithm.