Pointer-based red-black trees in the memory of the CentralStrQ word RAM
DefinitionCentralStrQ_RBTreesRed-black trees stored in the memory of the deterministic word RAM CentralStrQ, together with a structured program (Def_CentralStrQ_Structured) that inserts into them.
Functional trees. The trees are Batteries' RBTree.RBNode ℕ. Their insertion RBNode.insert compare k is Okasaki's insertion with the rebalancing functions balance1 and balance2. Batteries also proves the red-black invariant RBNode.WF and the depth bound for well-formed trees.
Memory layout.
- A node at address occupies four cells: holds the key, the left-child pointer, the right-child pointer, and the color (
colorCode: red , black ). - The empty tree is the null pointer .
rbNodes m p tlists the node addresses of stored at .RBAt m lo hi p tsays that , with its colors, is stored at , every node block lies in , and the blocks of distinct nodes are disjoint (Blocks4Disjoint,InBlocks4).
Program. rbInsertStmt g keeps its registers in the consecutive cells : slot , current node , key , flag , scratch , allocation pointer , root cell , stack pointer , root-color flag , and scratch . The stack starts at . The program runs in three phases:
- Descent (
rbDescBody). It follows the search path from the root cell and pushes each slot (the cell holding the pointer to the current subtree) on the stack. If it reaches an empty slot, it allocates a red leaf at the allocation pointer (rbNewNode) and advances the pointer by . If it finds the key, it stops. - Ascent (
rbAscBody). It pops the slots in reverse order. At each black node it runsbalance1(rbBal1) orbalance2(rbBal2) on the side of the key. These perform Okasaki's two rotation cases in place, reusing the three nodes involved. - Root. If the old root was red, the program colors the new root black.
RBInsertHyp bundles the hypotheses of the program: registers and stack below the node area, no overflow, the tree at the root, the key in its cell, the allocation pointer at the end of the node area, and a space bound.
import Mathlib
import Definitions.Def_CentralStrQ_Machine
import Definitions.Def_CentralStrQ_Hoare
import Definitions.Def_CentralStrQ_Structured
import Batteries.Recycling.RBTree.Depth
set_option autoImplicit false
namespace CentralStrQ
open WordRAM
/-!
# Red-black trees in the memory of the `CentralStrQ` word RAM: definitions
Pointer-based red-black trees on the published word RAM `Def_CentralStrQ_Machine`, for programs
of the structured language `Def_CentralStrQ_Structured`. The functional trees are Batteries'
`RBTree.RBNode` (Leonardo de Moura, Mario Carneiro; `Batteries.Recycling.RBTree`), whose
insertion `RBNode.insert` is Okasaki's (C. Okasaki, Red-black trees in a functional setting,
J. Funct. Programming 9(4), 1999); its invariant `RBNode.WF` and the depth bound
`RBNode.WF.depth_bound` are proved there.
* Memory layout: a node at address `q` occupies the cells `q` (key), `q + 1` (left child
pointer), `q + 2` (right child pointer) and `q + 3` (color: `colorCode`, red `0`, black `1`);
the empty tree is the null pointer `0`. `rbNodes m p t` lists the node addresses of `t`
stored at `p`; `RBAt m lo hi p t` says that `t` is stored at `p` with every node block inside
`[lo, hi)` and the blocks of distinct nodes disjoint (`Blocks4Disjoint`, `InBlocks4`).
* Programs: `rbInsertStmt g` inserts the key `[g + 2]` into the tree whose root pointer is in
`g + 6`: a descent (`rbDescBody`, allocating a red leaf with `rbNewNode`) that pushes the slots
of the search path on a stack, an ascent (`rbAscBody`) that pops them and runs Okasaki's
`balance1` / `balance2` at black nodes (`rbBal1`, `rbBal2` and their cases), and a final
blackening of a red root. `RBInsertHyp` bundles its hypotheses.
-/
variable {w : Width}
/-- Color code in memory: red `0`, black `1`. -/
def colorCode : RBTree.RBColor → ℕ
| .red => 0
| .black => 1
/-- The node addresses of the red-black tree `t` stored at `p` (preorder). A node at `q` occupies
the four cells `q` (key), `q + 1` (left pointer), `q + 2` (right pointer), `q + 3` (color). -/
def rbNodes (m : Memory w) : ℕ → RBTree.RBNode ℕ → List ℕ
| _, .nil => []
| p, .node _ l _ r => p :: (rbNodes m (m (p + 1)).toNat l ++ rbNodes m (m (p + 2)).toNat r)
/-- The four-cell node blocks at the addresses `ns` and at `ns'` are pairwise disjoint. -/
def Blocks4Disjoint (ns ns' : List ℕ) : Prop :=
∀ q ∈ ns, ∀ q' ∈ ns', q + 4 ≤ q' ∨ q' + 4 ≤ q
/-- The cell `x` lies in the four-cell block of one of the nodes `ns`. -/
def InBlocks4 (ns : List ℕ) (x : ℕ) : Prop := ∃ q ∈ ns, q ≤ x ∧ x < q + 4
/-- `RBAt m lo hi p t`: the red-black tree `t` is stored at address `p` (`0` for the empty
tree), every node block lies in `[lo, hi)`, and the blocks of distinct nodes are disjoint. -/
def RBAt (m : Memory w) (lo hi : ℕ) : ℕ → RBTree.RBNode ℕ → Prop
| p, .nil => p = 0
| p, .node c l a r =>
p ≠ 0 ∧ lo ≤ p ∧ p + 4 ≤ hi ∧ (m p).toNat = a ∧ (m (p + 3)).toNat = colorCode c ∧
RBAt m lo hi (m (p + 1)).toNat l ∧ RBAt m lo hi (m (p + 2)).toNat r ∧
Blocks4Disjoint [p] (rbNodes m (m (p + 1)).toNat l) ∧
Blocks4Disjoint [p] (rbNodes m (m (p + 2)).toNat r) ∧
Blocks4Disjoint (rbNodes m (m (p + 1)).toNat l) (rbNodes m (m (p + 2)).toNat r)
/-- Registers of the rebalancing step (consecutive cells from `g`): `g` the slot cell, `g + 1` the
black node `q` at the slot, `g + 4` and `g + 11` scratch, `g + 9` the child `L` (or `R`) on the
insertion side, `g + 10` its child. -/
def rbBal1Case2 (g : ℕ) : Stmt :=
.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 10))) (.immediate 1))
(.seq (.copy (.direct (g + 11)) (.memory (.indirect (g + 4))))
(.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 9))) (.immediate 2))
(.seq (.copy (.indirect (g + 4)) (.memory (.direct (g + 11))))
(.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 10))) (.immediate 2))
(.seq (.copy (.direct (g + 11)) (.memory (.indirect (g + 4))))
(.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 1))) (.immediate 1))
(.seq (.copy (.indirect (g + 4)) (.memory (.direct (g + 11))))
(.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 10))) (.immediate 1))
(.seq (.copy (.indirect (g + 4)) (.memory (.direct (g + 9))))
(.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 10))) (.immediate 2))
(.seq (.copy (.indirect (g + 4)) (.memory (.direct (g + 1))))
(.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 9))) (.immediate 3))
(.seq (.copy (.indirect (g + 4)) (.immediate 1))
(.copy (.indirect g) (.memory (.direct (g + 10)))))))))))))))))
/-- Check the left-right grandchild and rotate if it is red (second case of `balance1`). -/
def rbBal1LR (g : ℕ) : Stmt :=
.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 9))) (.immediate 2))
(.seq (.copy (.direct (g + 10)) (.memory (.indirect (g + 4))))
(.ite .eq (.memory (.direct (g + 10))) (.immediate 0)
.skip
(.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 10))) (.immediate 3))
(.ite .eq (.memory (.indirect (g + 4))) (.immediate 0)
(rbBal1Case2 g)
.skip))))
/-- First case of `balance1` as machine code: rotate when the left-left grandchild is red. -/
def rbBal1Case1 (g : ℕ) : Stmt :=
.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 9))) (.immediate 2))
(.seq (.copy (.direct (g + 11)) (.memory (.indirect (g + 4))))
(.seq (.copy (.indirect (g + 4)) (.memory (.direct (g + 1))))
(.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 1))) (.immediate 1))
(.seq (.copy (.indirect (g + 4)) (.memory (.direct (g + 11))))
(.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 10))) (.immediate 3))
(.seq (.copy (.indirect (g + 4)) (.immediate 1))
(.copy (.indirect g) (.memory (.direct (g + 9))))))))))
/-- Okasaki's `balance1` at the slot `[g]` holding the black node `[g + 1]`, whose left child was
just rebuilt by the insertion. -/
def rbBal1 (g : ℕ) : Stmt :=
.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 1))) (.immediate 1))
(.seq (.copy (.direct (g + 9)) (.memory (.indirect (g + 4))))
(.ite .eq (.memory (.direct (g + 9))) (.immediate 0)
.skip
(.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 9))) (.immediate 3))
(.ite .eq (.memory (.indirect (g + 4))) (.immediate 0)
(.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 9))) (.immediate 1))
(.seq (.copy (.direct (g + 10)) (.memory (.indirect (g + 4))))
(.ite .eq (.memory (.direct (g + 10))) (.immediate 0)
(rbBal1LR g)
(.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 10))) (.immediate 3))
(.ite .eq (.memory (.indirect (g + 4))) (.immediate 0)
(rbBal1Case1 g)
(rbBal1LR g))))))
.skip))))
/-- First case of `balance2` as machine code: rotate when the right-right grandchild is red. -/
def rbBal2Case1 (g : ℕ) : Stmt :=
.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 9))) (.immediate 1))
(.seq (.copy (.direct (g + 11)) (.memory (.indirect (g + 4))))
(.seq (.copy (.indirect (g + 4)) (.memory (.direct (g + 1))))
(.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 1))) (.immediate 2))
(.seq (.copy (.indirect (g + 4)) (.memory (.direct (g + 11))))
(.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 10))) (.immediate 3))
(.seq (.copy (.indirect (g + 4)) (.immediate 1))
(.copy (.indirect g) (.memory (.direct (g + 9))))))))))
/-- Second case of `balance2` as machine code: rotate when the right-left grandchild is red. -/
def rbBal2Case2 (g : ℕ) : Stmt :=
.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 10))) (.immediate 1))
(.seq (.copy (.direct (g + 11)) (.memory (.indirect (g + 4))))
(.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 1))) (.immediate 2))
(.seq (.copy (.indirect (g + 4)) (.memory (.direct (g + 11))))
(.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 10))) (.immediate 2))
(.seq (.copy (.direct (g + 11)) (.memory (.indirect (g + 4))))
(.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 9))) (.immediate 1))
(.seq (.copy (.indirect (g + 4)) (.memory (.direct (g + 11))))
(.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 10))) (.immediate 1))
(.seq (.copy (.indirect (g + 4)) (.memory (.direct (g + 1))))
(.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 10))) (.immediate 2))
(.seq (.copy (.indirect (g + 4)) (.memory (.direct (g + 9))))
(.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 9))) (.immediate 3))
(.seq (.copy (.indirect (g + 4)) (.immediate 1))
(.copy (.indirect g) (.memory (.direct (g + 10)))))))))))))))))
/-- Check the right-left grandchild and rotate if it is red (second case of `balance2`). -/
def rbBal2RL (g : ℕ) : Stmt :=
.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 9))) (.immediate 1))
(.seq (.copy (.direct (g + 10)) (.memory (.indirect (g + 4))))
(.ite .eq (.memory (.direct (g + 10))) (.immediate 0)
.skip
(.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 10))) (.immediate 3))
(.ite .eq (.memory (.indirect (g + 4))) (.immediate 0)
(rbBal2Case2 g)
.skip))))
/-- Okasaki's `balance2` at the slot `[g]` holding the black node `[g + 1]`, whose right child was
just rebuilt by the insertion. -/
def rbBal2 (g : ℕ) : Stmt :=
.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 1))) (.immediate 2))
(.seq (.copy (.direct (g + 9)) (.memory (.indirect (g + 4))))
(.ite .eq (.memory (.direct (g + 9))) (.immediate 0)
.skip
(.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 9))) (.immediate 3))
(.ite .eq (.memory (.indirect (g + 4))) (.immediate 0)
(.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 9))) (.immediate 2))
(.seq (.copy (.direct (g + 10)) (.memory (.indirect (g + 4))))
(.ite .eq (.memory (.direct (g + 10))) (.immediate 0)
(rbBal2RL g)
(.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 10))) (.immediate 3))
(.ite .eq (.memory (.indirect (g + 4))) (.immediate 0)
(rbBal2Case1 g)
(rbBal2RL g))))))
.skip))))
/-- Allocate a red leaf holding the key `[g + 2]` at the allocation pointer `[g + 5]`, link it into
the slot `[g]`, advance the allocation pointer by `4` and set the flag `[g + 3] := 2`. -/
def rbNewNode (g : ℕ) : Stmt :=
.seq (.copy (.indirect (g + 5)) (.memory (.direct (g + 2))))
(.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 5))) (.immediate 1))
(.seq (.copy (.indirect (g + 4)) (.immediate 0))
(.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 5))) (.immediate 2))
(.seq (.copy (.indirect (g + 4)) (.immediate 0))
(.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 5))) (.immediate 3))
(.seq (.copy (.indirect (g + 4)) (.immediate 0))
(.seq (.copy (.indirect g) (.memory (.direct (g + 5))))
(.seq (.binary .add (.direct (g + 5)) (.memory (.direct (g + 5))) (.immediate 4))
(.copy (.direct (g + 3)) (.immediate 2))))))))))
/-- One step of the descent: load the node at the slot `[g]`; at an empty slot allocate the
leaf; at a node holding the key stop with flag `1`; otherwise push the slot on the stack
`[g + 7]` and move the slot to the left or right pointer cell of the node. -/
def rbDescBody (g : ℕ) : Stmt :=
.seq (.copy (.direct (g + 1)) (.memory (.indirect g)))
(.ite .eq (.memory (.direct (g + 1))) (.immediate 0)
(rbNewNode g)
(.ite .eq (.memory (.indirect (g + 1))) (.memory (.direct (g + 2)))
(.copy (.direct (g + 3)) (.immediate 1))
(.seq (.copy (.indirect (g + 7)) (.memory (.direct g)))
(.seq (.binary .add (.direct (g + 7)) (.memory (.direct (g + 7))) (.immediate 1))
(.seq (.binary .add (.direct g) (.memory (.direct (g + 1))) (.immediate 1))
(.ite .lt (.memory (.direct (g + 2))) (.memory (.indirect (g + 1)))
.skip
(.binary .add (.direct g) (.memory (.direct g)) (.immediate 1))))))))
section rbinsert
variable (g lo hi k Rb : ℕ) (t : RBTree.RBNode ℕ) (m₀ : Memory w)
/-- Hypotheses of red-black insertion: registers in the cells `g, …, g + 11` (root pointer in
`g + 6`, key in `g + 2`, allocation pointer `[g + 5] = hi`), a stack of `t.depth` cells from
`g + 12` below the node area `[lo, hi)`, room for one more node without overflow, the tree `t`
stored at the root, and `Rb ≥ hi + 4` bounds the space. -/
structure RBInsertHyp : Prop where
below : g + 12 + t.depth ≤ lo
lohi : lo ≤ hi
fit : hi + 4 < 2 ^ w.bits
tree : RBAt m₀ lo hi (m₀ (g + 6)).toNat t
key : (m₀ (g + 2)).toNat = k
topV : (m₀ (g + 5)).toNat = hi
bound : hi + 4 ≤ Rb
end rbinsert
/-- One step of the ascent: pop the slot of the next path node from the stack, and if that node
is black rebalance it on the side of the key (`balance1` on the left, `balance2` on the right). -/
def rbAscBody (g : ℕ) : Stmt :=
.seq (.binary .sub (.direct (g + 7)) (.memory (.direct (g + 7))) (.immediate 1))
(.seq (.copy (.direct g) (.memory (.indirect (g + 7))))
(.seq (.copy (.direct (g + 1)) (.memory (.indirect g)))
(.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 1))) (.immediate 3))
(.ite .eq (.memory (.indirect (g + 4))) (.immediate 0)
.skip
(.ite .lt (.memory (.direct (g + 2))) (.memory (.indirect (g + 1)))
(rbBal1 g)
(rbBal2 g))))))
/-- **Red-black insertion as a word-RAM program.** -/
def rbInsertStmt (g : ℕ) : Stmt :=
.seq (.copy (.direct (g + 8)) (.immediate 0))
(.seq (.copy (.direct (g + 1)) (.memory (.direct (g + 6))))
(.seq (.ite .eq (.memory (.direct (g + 1))) (.immediate 0)
.skip
(.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 1))) (.immediate 3))
(.ite .eq (.memory (.indirect (g + 4))) (.immediate 0)
(.copy (.direct (g + 8)) (.immediate 1))
.skip)))
(.seq (.copy (.direct g) (.immediate (g + 6)))
(.seq (.copy (.direct (g + 7)) (.immediate (g + 12)))
(.seq (.copy (.direct (g + 3)) (.immediate 0))
(.seq (.loop .lt (.memory (.direct (g + 3))) (.immediate 1) (rbDescBody g))
(.seq (.loop .lt (.immediate (g + 12)) (.memory (.direct (g + 7))) (rbAscBody g))
(.ite .eq (.memory (.direct (g + 8))) (.immediate 1)
(.seq (.copy (.direct (g + 1)) (.memory (.direct (g + 6))))
(.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 1))) (.immediate 3))
(.copy (.indirect (g + 4)) (.immediate 1))))
.skip))))))))
end CentralStrQ