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.
Searching neither changes records nor allocates.
All scalar registers except temporary register one are preserved.
All pointer registers except cursor zero and null register five are preserved.
Nonnull output agrees with the existing functional search.
- key (a : ℕ) : y.ptr 0 = some a → ∃ (path : PosNum) (label : Label M extra) (rec : PointerMachine.Record M (layout extra regs)), BinaryTree.get path t = some label ∧ label.address = a ∧ y.heap[a]? = some rec ∧ rec.data = label.data ∧ rec.data 0 = x.data 0
A nonnull output belongs to this tree, with its complete scalar payload intact.
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.