Documentation

CslibFrontier.DataStructure.SearchTree.PointerMachine.InsertLeaf

Allocating and attaching a leaf #

insertLeaf fills a vacant child link of parent register two, or initializes root register one when the parent is null. The side is fixed in the program. The original construction below proves the exact heap update, fresh leaf representation and instruction count; ordering and rebalancing belong to callers.

Attach cursor zero to the selected child of parent two, or set root one if parent is null. Register five is scratch.

def Cslib.DataStructure.SearchTree.PointerMachine.insertLeaf {M : PointerMachine.Model} {extra regs : ℕ} (source : Fin (extra + 1) → Fin (regs + 2)) (side : Fin 2) :

Allocate a leaf with the supplied scalar fields and attach it at the selected vacant link. The null-parent case initializes root register one.

theorem Cslib.DataStructure.SearchTree.PointerMachine.insertLeaf_correct {M : PointerMachine.Model} {extra regs : ℕ} (source : Fin (extra + 1) → Fin (regs + 2)) (side : Fin 2) (x : PointerMachine.State M (layout extra regs)) (pc : x.pc = 0) (vacant : match x.ptr 2 with | none => x.ptr 1 = none | some a => ∃ (par : PointerMachine.Record M (layout extra regs)), x.heap[a]? = some par ∧ par.ptr side.castSucc = none) :
have label := { address := x.heap.length, data := fun (f : Fin (extra + 1)) => x.data (source f) }; have heap := x.heap ++ [record none none (x.ptr 2) label]; ∃ (y : PointerMachine.State M (layout extra regs)), PointerMachine.Executes (insertLeaf source side) x y (extra + 9) ∧ (y.heap = match x.ptr 2 with | none => heap | some a => heap.modify a fun (n : PointerMachine.Record M (layout extra regs)) => { ptr := Function.update n.ptr side.castSucc (some x.heap.length), data := n.data }) ∧ Represents y.heap (x.ptr 2) (BinaryTree.node label BinaryTree.nil BinaryTree.nil) ∧ y.heap.length = x.heap.length + 1 ∧ y.ptr 0 = some x.heap.length ∧ (y.ptr 1 = if x.ptr 2 = none then some x.heap.length else x.ptr 1) ∧ y.data = x.data ∧ y.ptr 5 = none ∧ (∀ (i : Fin (layout extra regs).ptrRegs), i ≠ 0 → i ≠ 1 → i ≠ 5 → y.ptr i = x.ptr i) ∧ ∀ (a : ℕ) (par : PointerMachine.Record M (layout extra regs)), x.ptr 2 = some a → x.heap[a]? = some par → y.heap[a]? = some { ptr := Function.update par.ptr side.castSucc (some x.heap.length), data := par.data }

Filling a vacant child slot (or empty root) costs exactly extra + 9 instructions. The new leaf has the complete supplied payload and parent link. The heap formula preserves every old record except the selected parent's child link; all scalar registers and pointer registers other than cursor, root and null scratch are unchanged. No ordering or balance is asserted.