Documentation

CslibFrontier.DataStructure.SearchTree.PointerMachine.Rotation

Pointer-machine subtree rotations #

The programs promote either child of the cursor. It repairs both pivot parent links and the middle subtree's parent link, including the empty-middle case. The caller must reconnect the external parent's child link (or update its root register). Scalar payloads, including cached heights, are unchanged; height recomputation belongs to AVL balancing.

The child rewiring adapts Stefan Langerman's earlier lean-bst rotation programs, extended here with parent links.

Promote the left child of cursor zero; repair pivot and middle parent links.

Promote the right child of cursor zero; repair pivot and middle parent links.

def Cslib.DataStructure.SearchTree.PointerMachine.rewire {M : PointerMachine.Model} {extra regs : ℕ} (near far : Fin 3) (h : List (PointerMachine.Record M (layout extra regs))) (x y : ℕ) (middle parent : PointerMachine.Pointer) :

The exact heap writes of a rotation, in execution order. A null middle subtree requires no parent write. Pivot addresses are proof data, not machine inputs.

theorem Cslib.DataStructure.SearchTree.PointerMachine.rewire_frame {M : PointerMachine.Model} {extra regs : ℕ} (near far : Fin 3) (h : List (PointerMachine.Record M (layout extra regs))) (x y a : ℕ) (middle parent : PointerMachine.Pointer) (ax : a ≠ x) (ay : a ≠ y) (am : middle ≠ some a) :
(rewire near far h x y middle parent)[a]? = h[a]?

Records outside the pivots and the middle root are unchanged.

theorem Cslib.DataStructure.SearchTree.PointerMachine.length_rewire {M : PointerMachine.Model} {extra regs : ℕ} (near far : Fin 3) (h : List (PointerMachine.Record M (layout extra regs))) (x y : ℕ) (middle parent : PointerMachine.Pointer) :
(rewire near far h x y middle parent).length = h.length

Rotation creates no records.

The pointer field of the child on a side: zero on the left, one on the right.

theorem Cslib.DataStructure.SearchTree.PointerMachine.AddressesDistinct.rotate {M : PointerMachine.Model} {extra : ℕ} (right : Bool) {x y : Label M extra} {a b c : BinaryTree (Label M extra)} (h : AddressesDistinct (nodeAt right y (nodeAt right x a b) c)) :
AddressesDistinct (nodeAt right x a (nodeAt right y b c))

Address separation survives a rotation on either side.

theorem Cslib.DataStructure.SearchTree.PointerMachine.represents_rewire_rotate {M : PointerMachine.Model} {extra regs : ℕ} (right : Bool) (h : List (PointerMachine.Record M (layout extra regs))) (x y : Label M extra) (a b c : BinaryTree (Label M extra)) (parent : PointerMachine.Pointer) (rep : Represents h parent (nodeAt right y (nodeAt right x a b) c)) (distinct : AddressesDistinct (nodeAt right y (nodeAt right x a b) c)) :
Represents (rewire (childField right) (childField !right) h x.address y.address (root b) parent) parent (nodeAt right x a (nodeAt right y b c))

The heap rewiring of a rotation on either side preserves the full recursive subtree representation, including parent links: y with child x on the selected side, whose own children are a on that side and b on the other, becomes x with a on that side and y on the other, and b becomes the child of y on the selected side. This is a statement about heaps: any program whose writes are rewire inherits it.

Address separation survives rotation, so the result can be rotated again.

Address separation also survives the inverse, left rotation.

theorem Cslib.DataStructure.SearchTree.PointerMachine.represents_rewire_rotateRight {M : PointerMachine.Model} {extra regs : ℕ} (h : List (PointerMachine.Record M (layout extra regs))) (x y : Label M extra) (a b c : BinaryTree (Label M extra)) (parent : PointerMachine.Pointer) (rep : Represents h parent (BinaryTree.node y (BinaryTree.node x a b) c)) (distinct : AddressesDistinct (BinaryTree.node y (BinaryTree.node x a b) c)) :
Represents (rewire 0 1 h x.address y.address (root b) parent) parent (BinaryTree.node x a (BinaryTree.node y b c))

The heap rewiring of a right rotation preserves the full recursive subtree representation, including parent links.

theorem Cslib.DataStructure.SearchTree.PointerMachine.represents_rewire_rotateLeft {M : PointerMachine.Model} {extra regs : ℕ} (h : List (PointerMachine.Record M (layout extra regs))) (x y : Label M extra) (a b c : BinaryTree (Label M extra)) (parent : PointerMachine.Pointer) (rep : Represents h parent (BinaryTree.node y a (BinaryTree.node x b c))) (distinct : AddressesDistinct (BinaryTree.node y a (BinaryTree.node x b c))) :
Represents (rewire 1 0 h x.address y.address (root b) parent) parent (BinaryTree.node x (BinaryTree.node y a b) c)

The heap rewiring of a left rotation preserves the full recursive subtree representation, including parent links.

theorem Cslib.DataStructure.SearchTree.PointerMachine.rotateRight_represents {M : PointerMachine.Model} {extra regs : ℕ} (s : PointerMachine.State M (layout extra regs)) (x y : Label M extra) (a b c : BinaryTree (Label M extra)) (parent : PointerMachine.Pointer) (rep : Represents s.heap parent (BinaryTree.node y (BinaryTree.node x a b) c)) (distinct : AddressesDistinct (BinaryTree.node y (BinaryTree.node x a b) c)) (cursor : s.ptr 0 = some y.address) (pc : s.pc = 0) :
∃ (t : PointerMachine.State M (layout extra regs)), PointerMachine.Executes rotateRight s t 10 ∧ Represents t.heap parent (BinaryTree.node x a (BinaryTree.node y b c)) ∧ t.ptr 0 = some x.address ∧ t.data = s.data ∧ t.heap.length = s.heap.length ∧ (∀ (i : Fin (layout extra regs).ptrRegs), i ≠ 0 → i ≠ 2 → i ≠ 3 → i ≠ 4 → t.ptr i = s.ptr i) ∧ ∀ (z : ℕ), z ≠ x.address → z ≠ y.address → root b ≠ some z → t.heap[z]? = s.heap[z]?

A right rotation preserves the full recursive subtree representation, including parent links, in ten actual instructions. The external parent's child link and cached heights are the caller's responsibility. Registers outside the scratch set and records outside the pivots and middle root are preserved.

theorem Cslib.DataStructure.SearchTree.PointerMachine.rotateLeft_represents {M : PointerMachine.Model} {extra regs : ℕ} (s : PointerMachine.State M (layout extra regs)) (x y : Label M extra) (a b c : BinaryTree (Label M extra)) (parent : PointerMachine.Pointer) (rep : Represents s.heap parent (BinaryTree.node y a (BinaryTree.node x b c))) (distinct : AddressesDistinct (BinaryTree.node y a (BinaryTree.node x b c))) (cursor : s.ptr 0 = some y.address) (pc : s.pc = 0) :
∃ (t : PointerMachine.State M (layout extra regs)), PointerMachine.Executes rotateLeft s t 10 ∧ Represents t.heap parent (BinaryTree.node x (BinaryTree.node y a b) c) ∧ t.ptr 0 = some x.address ∧ t.data = s.data ∧ t.heap.length = s.heap.length ∧ (∀ (i : Fin (layout extra regs).ptrRegs), i ≠ 0 → i ≠ 2 → i ≠ 3 → i ≠ 4 → t.ptr i = s.ptr i) ∧ ∀ (z : ℕ), z ≠ x.address → z ≠ y.address → root b ≠ some z → t.heap[z]? = s.heap[z]?

A left rotation preserves the full recursive subtree representation, including parent links, in ten actual instructions. The external parent's child link and cached heights are the caller's responsibility. Registers outside the scratch set and records outside the pivots and middle root are preserved.