Documentation

CslibFrontier.DataStructure.AVLTree.PointerMachine.Rotation

Height-maintaining pointer-machine AVL rotation #

A concrete program saves the old root, rotates links, recomputes the old root's height, then follows its parent to recompute the promoted root's height. All primitive blocks are relocated into one finite program, including their charged continuation jumps. The external parent's child link is left for the caller to reconnect.

Wrap a pointer rotation with saving its former root and repairing both pivot heights. The supplied block must leave the former root's parent pointing to the promoted root. The certified entry points below supply the concrete left and right rotations.

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

Right subtree rotation followed by bottom-up recomputation of both pivot heights. Data temporaries one/two and pointer registers zero/two/three/four/six may change.

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

Left subtree rotation followed by bottom-up recomputation of both pivot heights. Data temporaries one/two and pointer registers zero/two/three/four/six may change.

theorem Cslib.DataStructure.AVLTree.PointerMachine.rotateRight_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)) (notKey : field ≠ 0) (s : PointerMachine.State M (SearchTree.PointerMachine.layout extra (regs + 1))) (x y : SearchTree.PointerMachine.Label M extra) (a b c : BinaryTree (SearchTree.PointerMachine.Label M extra)) (A B C : AVLTree M.Value) (parent : PointerMachine.Pointer) (rep : SearchTree.PointerMachine.Represents s.heap parent (BinaryTree.node y (BinaryTree.node x a b) c)) (distinct : SearchTree.PointerMachine.AddressesDistinct (BinaryTree.node y (BinaryTree.node x a b) c)) (ma : Matches encode field a A) (mb : Matches encode field b B) (mc : Matches encode field c C) (ha : A.HeightConsistent) (hb : B.HeightConsistent) (hc : C.HeightConsistent) (cursor : s.ptr 0 = some y.address) (pc : s.pc = 0) :
have oldHeight := encode (mkNode (y.data 0) B C).cachedHeight; have newHeight := encode (mkNode (x.data 0) A (mkNode (y.data 0) B C)).cachedHeight; have y' := { address := y.address, data := Function.update y.data field oldHeight }; have x' := { address := x.address, data := Function.update x.data field newHeight }; have tree := BinaryTree.node x' a (BinaryTree.node y' b c); ∃ (t : PointerMachine.State M (SearchTree.PointerMachine.layout extra (regs + 1))), PointerMachine.Executes (rotateRight zero maxSucc field) s t 39 ∧ SearchTree.PointerMachine.Represents t.heap parent tree ∧ Matches encode field tree (AVLTree.rotateRight (y.data 0) (mkNode (x.data 0) A B) C) ∧ SearchTree.PointerMachine.AddressesDistinct tree ∧ SearchTree.PointerMachine.root tree = some x.address ∧ t.ptr 0 = SearchTree.PointerMachine.root tree ∧ t.heap.length = s.heap.length ∧ (∀ (i : Fin (SearchTree.PointerMachine.layout extra (regs + 1)).dataRegs), i ≠ 1 → i ≠ 2 → t.data i = s.data i) ∧ (∀ (i : Fin (SearchTree.PointerMachine.layout extra (regs + 1)).ptrRegs), i ≠ 0 → i ≠ 2 → i ≠ 3 → i ≠ 4 → i ≠ 6 → t.ptr i = s.ptr i) ∧ t.ptr 6 = s.ptr 0 ∧ ∀ (z : ℕ), z ≠ x.address → z ≠ y.address → SearchTree.PointerMachine.root b ≠ some z → t.heap[z]? = s.heap[z]?

A concrete right rotation implements the existing AVL rotation's two cached-height updates in 39 instructions. The three untouched child trees need correct caches; the two pivot caches may initially be arbitrary. External-parent reconnection is separate.

theorem Cslib.DataStructure.AVLTree.PointerMachine.rotateLeft_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)) (notKey : field ≠ 0) (s : PointerMachine.State M (SearchTree.PointerMachine.layout extra (regs + 1))) (x y : SearchTree.PointerMachine.Label M extra) (a b c : BinaryTree (SearchTree.PointerMachine.Label M extra)) (A B C : AVLTree M.Value) (parent : PointerMachine.Pointer) (rep : SearchTree.PointerMachine.Represents s.heap parent (BinaryTree.node y a (BinaryTree.node x b c))) (distinct : SearchTree.PointerMachine.AddressesDistinct (BinaryTree.node y a (BinaryTree.node x b c))) (ma : Matches encode field a A) (mb : Matches encode field b B) (mc : Matches encode field c C) (ha : A.HeightConsistent) (hb : B.HeightConsistent) (hc : C.HeightConsistent) (cursor : s.ptr 0 = some y.address) (pc : s.pc = 0) :
have oldHeight := encode (mkNode (y.data 0) A B).cachedHeight; have newHeight := encode (mkNode (x.data 0) (mkNode (y.data 0) A B) C).cachedHeight; have y' := { address := y.address, data := Function.update y.data field oldHeight }; have x' := { address := x.address, data := Function.update x.data field newHeight }; have tree := BinaryTree.node x' (BinaryTree.node y' a b) c; ∃ (t : PointerMachine.State M (SearchTree.PointerMachine.layout extra (regs + 1))), PointerMachine.Executes (rotateLeft zero maxSucc field) s t 39 ∧ SearchTree.PointerMachine.Represents t.heap parent tree ∧ Matches encode field tree (AVLTree.rotateLeft (y.data 0) A (mkNode (x.data 0) B C)) ∧ SearchTree.PointerMachine.AddressesDistinct tree ∧ SearchTree.PointerMachine.root tree = some x.address ∧ t.ptr 0 = SearchTree.PointerMachine.root tree ∧ t.heap.length = s.heap.length ∧ (∀ (i : Fin (SearchTree.PointerMachine.layout extra (regs + 1)).dataRegs), i ≠ 1 → i ≠ 2 → t.data i = s.data i) ∧ (∀ (i : Fin (SearchTree.PointerMachine.layout extra (regs + 1)).ptrRegs), i ≠ 0 → i ≠ 2 → i ≠ 3 → i ≠ 4 → i ≠ 6 → t.ptr i = s.ptr i) ∧ t.ptr 6 = s.ptr 0 ∧ ∀ (z : ℕ), z ≠ x.address → z ≠ y.address → SearchTree.PointerMachine.root b ≠ some z → t.heap[z]? = s.heap[z]?

A concrete left rotation implements the existing AVL rotation's two cached-height updates in 39 instructions. The three untouched child trees need correct caches; the two pivot caches may initially be arbitrary. External-parent reconnection is separate.