Documentation

CslibFrontier.DataStructure.SearchTree.PointerMachine.Height

Recomputing a linked tree's cached height #

The nine-instruction program reads the two child caches, treats null children as height zero, computes their maximum plus one, and writes the root cache. The scalar model supplies zero and maximum-plus-one operations with explicit correctness contracts. This is the local height-update step of AVL balancing, not an insertion or balancing algorithm.

def Cslib.DataStructure.SearchTree.PointerMachine.heightValue {M : PointerMachine.Model} {extra : ℕ} (zero : M.Value) (field : Fin (extra + 1)) :
BinaryTree (Label M extra) → M.Value

Cached scalar height at a subtree root, with the supplied zero for an empty tree.

def Cslib.DataStructure.SearchTree.PointerMachine.readHeightsPrefix {M : PointerMachine.Model} {extra regs : ℕ} (zero : M.Op) (field : Fin (extra + 1)) (base : Fin 7) :
List (PointerMachine.Instr M (layout extra (regs + 1)))

Six instructions read child caches into data registers one and two. Pointer register two is scratch; a null child keeps the supplied scalar zero.

theorem Cslib.DataStructure.SearchTree.PointerMachine.readHeightsPrefix_runs {M : PointerMachine.Model} {extra regs : ℕ} (zero : M.Op) (z : M.Value) (zero_spec : ∀ (a b : M.Value), M.evalOp zero a b = z) (field : Fin (extra + 1)) (base : Fin 7) (scratch : base ≠ 2) (suffix : List (PointerMachine.Instr M (layout extra (regs + 1)))) (s : PointerMachine.State M (layout extra (regs + 1))) (label : Label M extra) (l r : BinaryTree (Label M extra)) (parent : PointerMachine.Pointer) (rep : Represents s.heap parent (BinaryTree.node label l r)) (cursor : s.ptr base = some label.address) (pc : s.pc = 0) :
∃ (t : PointerMachine.State M (layout extra (regs + 1))), PointerMachine.Runs (readHeightsPrefix zero field base ++ suffix) s t 6 ∧ t.pc = 6 ∧ t.heap = s.heap ∧ t.data 1 = heightValue z field l ∧ t.data 2 = heightValue z field r ∧ (∀ (i : Fin (layout extra (regs + 1)).dataRegs), i ≠ 1 → i ≠ 2 → t.data i = s.data i) ∧ ∀ (i : Fin (layout extra (regs + 1)).ptrRegs), i ≠ 2 → t.ptr i = s.ptr i

The cache-read prefix executes before any suffix, preserving the heap and client registers.

def Cslib.DataStructure.SearchTree.PointerMachine.recomputeHeight {M : PointerMachine.Model} {extra regs : ℕ} (zero maxSucc : M.Op) (field : Fin (extra + 1)) :
List (PointerMachine.Instr M (layout extra (regs + 1)))

Recompute the cursor's height. Data registers one/two and pointer register two are scratch. Null-child loads preserve the initialized scalar zero.

theorem Cslib.DataStructure.SearchTree.PointerMachine.recomputeHeight_correct {M : PointerMachine.Model} {extra regs : ℕ} (zero maxSucc : M.Op) (encode : ℕ → M.Value) (zero_spec : ∀ (a b : M.Value), M.evalOp zero a b = encode 0) (max_spec : ∀ (a b : ℕ), M.evalOp maxSucc (encode a) (encode b) = encode (max a b + 1)) (field : Fin (extra + 1)) (x : PointerMachine.State M (layout extra (regs + 1))) (label : Label M extra) (l r : BinaryTree (Label M extra)) (parent : PointerMachine.Pointer) (rep : Represents x.heap parent (BinaryTree.node label l r)) (left : heightValue (encode 0) field l = encode l.height) (right : heightValue (encode 0) field r = encode r.height) (cursor : x.ptr 0 = some label.address) (pc : x.pc = 0) :
∃ (y : PointerMachine.State M (layout extra (regs + 1))), PointerMachine.Executes (recomputeHeight zero maxSucc field) x y 9 ∧ (y.heap = x.heap.modify label.address fun (n : PointerMachine.Record M (layout extra (regs + 1))) => { ptr := n.ptr, data := Function.update n.data field (encode (max l.height r.height + 1)) }) ∧ Represents y.heap parent (updateData (BinaryTree.node label l r) label.address field (encode (BinaryTree.node label l r).height)) ∧ (∀ (i : Fin (layout extra (regs + 1)).dataRegs), i ≠ 1 → i ≠ 2 → y.data i = x.data i) ∧ ∀ (i : Fin (layout extra (regs + 1)).ptrRegs), i ≠ 2 → y.ptr i = x.ptr i

Correct child caches give a correct root cache in nine instructions, including halt. Only the root's selected scalar field, data temporaries one/two and pointer temporary two change. The statement uses structural height; it does not assume that the root cache was already correct.