Documentation

CslibFrontier.DataStructure.AVLTree.PointerMachine.Insert

Pointer-machine AVL insertion #

The insertion program searches with a parent cursor, initializes and attaches one new leaf on a miss, and balances ancestors by following parent links. Comparisons and height operations are scalar primitives. Ghost contexts describe the proof; the program uses only fixed registers.

def Cslib.DataStructure.AVLTree.PointerMachine.leafRegisters {extra regs : ℕ} (field : Fin (extra + 1)) (source : Fin (extra + 1) → Fin (regs + 3)) (f : Fin (extra + 1)) :
Fin (regs + 3)

Scalar sources for a new AVL leaf: query register zero, initialized height register one, and explicit client sources for all remaining fields.

def Cslib.DataStructure.AVLTree.PointerMachine.createLeaf {M : PointerMachine.Model} {extra regs : ℕ} (zero maxSucc : M.Op) (lt : M.Test) (field : Fin (extra + 1)) (source : Fin (extra + 1) → Fin (regs + 3)) :

Initialize a height-one leaf, choose its child link by comparison with the parent key, and allocate and attach it. A null parent installs the whole-tree root.

def Cslib.DataStructure.AVLTree.PointerMachine.ancestorStep {M : PointerMachine.Model} {extra regs : ℕ} (zero maxSucc : M.Op) (heavy le : M.Test) (field : Fin (extra + 1)) :

Balance the current ancestor, reconnect its new root, and move to its parent. The enclosing loop decides whether another ancestor exists.

def Cslib.DataStructure.AVLTree.PointerMachine.rebalanceAncestors {M : PointerMachine.Model} {extra regs : ℕ} (zero maxSucc : M.Op) (heavy le : M.Test) (field : Fin (extra + 1)) :

Follow parent links, balancing and reconnecting every ancestor until the cursor is null.

def Cslib.DataStructure.AVLTree.PointerMachine.insertAfterSeek {M : PointerMachine.Model} {extra regs : ℕ} (zero maxSucc : M.Op) (lt heavy le : M.Test) (field : Fin (extra + 1)) (source : Fin (extra + 1) → Fin (regs + 3)) :

On an unsuccessful search, create a leaf and repair its ancestors. A successful search halts without allocating or changing the tree.

def Cslib.DataStructure.AVLTree.PointerMachine.insert {M : PointerMachine.Model} {extra regs : ℕ} (zero maxSucc : M.Op) (lt heavy le : M.Test) (field : Fin (extra + 1)) (source : Fin (extra + 1) → Fin (regs + 3)) :

Insert the query in data register zero into the AVL tree at cursor zero. Root register one tracks the whole tree. All extra leaf payload sources are explicit.

structure Cslib.DataStructure.AVLTree.PointerMachine.InsertResult {M : PointerMachine.Model} {extra regs : ℕ} [LinearOrder M.Value] (encode : ℕ → M.Value) (field : Fin (extra + 1)) (source : Fin (extra + 1) → Fin (regs + 3)) (s t : PointerMachine.State M (SearchTree.PointerMachine.layout extra (regs + 1))) (before after : BinaryTree (SearchTree.PointerMachine.Label M extra)) (original : AVLTree M.Value) :

Linked-tree, register and scalar-payload outcome of set insertion. Existing records retain all scalar fields except the height cache; a new record receives its explicit client payload. This contract does not assert a full frame for records outside the tree.

theorem Cslib.DataStructure.AVLTree.PointerMachine.InsertResult.before {M : PointerMachine.Model} {extra regs : ℕ} [LinearOrder M.Value] {encode : ℕ → M.Value} {field : Fin (extra + 1)} {source : Fin (extra + 1) → Fin (regs + 3)} {s x t : PointerMachine.State M (SearchTree.PointerMachine.layout extra (regs + 1))} {before after : BinaryTree (SearchTree.PointerMachine.Label M extra)} {original : AVLTree M.Value} (h : InsertResult encode field source x t before after original) (heap : x.heap = s.heap) (data : ∀ (i : Fin (SearchTree.PointerMachine.layout extra (regs + 1)).dataRegs), i ≠ 1 → i ≠ 2 → x.data i = s.data i) (safe : ∀ (f : Fin (extra + 1)), f ≠ field → f ≠ 0 → source f ≠ 1 ∧ source f ≠ 2) :
InsertResult encode field source s t before after original

An insertion result may be restated from an earlier state with the same heap and the same scalar registers other than the two scratch registers.

theorem Cslib.DataStructure.AVLTree.PointerMachine.insert_correct {M : PointerMachine.Model} {extra regs : ℕ} [LinearOrder M.Value] (zero maxSucc : M.Op) (lt heavy le : M.Test) (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)) (compare : ∀ (a b : M.Value), M.evalTest lt a b = decide (a < b)) (heavy_spec : ∀ (a b : ℕ), M.evalTest heavy (encode a) (encode b) = decide (b + 1 < a)) (le_spec : ∀ (a b : ℕ), M.evalTest le (encode a) (encode b) = decide (a ≤ b)) (field : Fin (extra + 1)) (notKey : field ≠ 0) (source : Fin (extra + 1) → Fin (regs + 3)) (safe : ∀ (f : Fin (extra + 1)), f ≠ field → f ≠ 0 → source f ≠ 1 ∧ source f ≠ 2) (s : PointerMachine.State M (SearchTree.PointerMachine.layout extra (regs + 1))) (tree : BinaryTree (SearchTree.PointerMachine.Label M extra)) (original : AVLTree M.Value) (rep : SearchTree.PointerMachine.Represents s.heap none tree) (distinct : SearchTree.PointerMachine.AddressesDistinct tree) (matched : Matches encode field tree original) (valid : original.Valid) (cursor : s.ptr 0 = SearchTree.PointerMachine.root tree) (rootReg : s.ptr 1 = SearchTree.PointerMachine.root tree) (pc : s.pc = 0) :
∃ (t : PointerMachine.State M (SearchTree.PointerMachine.layout extra (regs + 1))) (result : BinaryTree (SearchTree.PointerMachine.Label M extra)) (time : ℕ), PointerMachine.Executes (insert zero maxSucc lt heavy le field source) s t time ∧ time ≤ 141 * original.cachedHeight + extra + 31 ∧ time ≤ 282 * Nat.log 2 (original.elements.card + 1) + extra + 31 ∧ InsertResult encode field source s t tree result original

The concrete finite program implements AVLTree.insert, preserving linked representation, address separation and payloads while allocating at most one record. Its instruction bound is linear in the original AVL height and logarithmic in the number of stored keys; the additive term charges every initialized scalar field. All counts include allocation, jumps and halt.