Position trees (balanced trees of segments ordered by position) on the CentralStrQ word RAM
DefinitionCentralStrQ_PosTreesA sequence of segments (for example the phrases of a parsing of a dynamic string) kept in a balanced binary tree ordered by position. The functional trees are Batteries' red-black trees RBTree.RBNode (ℕ × ℕ). An element is a segment of length with payload , and the sequence is the in-order list t.toList. No key order is involved; the balance invariant is Batteries' RBNode.Balanced.
Functional search.
wsum tis the total length .segFind L iscans a list of segments for the one containing the position (0-based). It returns the segment and the offset of in it, ornonewhen is at least the total length.posFind t iis the same search by descent: go left if is smaller than the left subtree's total length ; report the node if ; otherwise go right with .
Memory layout. PosAt m lo hi p t says that is stored at address ( for the empty tree). A node at occupies six cells:
| cell | content |
|---|---|
| length | |
| left child pointer | |
| right child pointer | |
color (colorCode of Def_CentralStrQ_RBTrees) | |
| payload | |
| total length of the left subtree |
Every node block lies in , and distinct blocks are disjoint (posNodes, Blocks6Disjoint). Storing the left length rather than the subtree length lets a search read it without visiting the child.
Program. posFindStmt g (loop body posFindBody g) uses registers in the cells :
- the position in and the root pointer in ;
- the current node in , a stop flag in , scratch in , the relative position in and the left length in ;
- the answer in in the format
SegAt: flag fornone; flag with the length, the payload and the offset for an answer.
PosFindHyp g lo hi i Rb t m₀ bundles its hypotheses: registers below the node area, , stored at , and .
import Mathlib
import Definitions.Def_CentralStrQ_Machine
import Definitions.Def_CentralStrQ_Hoare
import Definitions.Def_CentralStrQ_Structured
import Definitions.Def_CentralStrQ_RBTrees
set_option autoImplicit false
namespace CentralStrQ
open WordRAM
/-!
# Position trees on the `CentralStrQ` word RAM: definitions
A sequence of segments (for example the phrases of a parsing of a dynamic string) kept in a
balanced tree ordered by position. The functional trees are Batteries' red-black trees
`RBTree.RBNode (ℕ × ℕ)`: an element `(ℓ, v)` is a segment of length `ℓ` with payload `v`, and
the sequence is the in-order list `RBNode.toList` (no key order is involved; the balance
invariant is Batteries' `RBNode.Balanced`).
* `wsum t`: the total length. `segFind L i`: the segment of the list `L` containing the position
`i` (`0`-based) with the offset of `i` in it (`none` past the end); `posFind t i`: the same
search by descent, using the left lengths.
* Memory layout (`PosAt m lo hi p t`, nodes listed by `posNodes`, disjointness
`Blocks6Disjoint`): a node at `q` occupies the six cells `q` (length `ℓ`), `q + 1` (left
pointer), `q + 2` (right pointer), `q + 3` (color, `colorCode` of `Def_CentralStrQ_RBTrees`),
`q + 4` (payload `v`) and `q + 5` (total length of the left subtree); the empty tree is the
null pointer `0`.
* `posFindStmt g` (loop body `posFindBody g`): registers in the cells `g, …, g + 11` (position
in `g + 2`, root pointer in `g + 6`), the answer in `g + 8, …, g + 11` in the format `SegAt`.
`PosFindHyp` bundles its hypotheses.
-/
variable {w : Width}
/-- The total length `ℓ₁ + ⋯ + ℓ_k` of a position tree, a Batteries red-black tree whose
elements `(ℓ, v)` are segments of length `ℓ` with payload `v`, in order. -/
def wsum : RBTree.RBNode (ℕ × ℕ) → ℕ
| .nil => 0
| .node _ l x r => wsum l + x.1 + wsum r
/-- The segment of a list of segments `(ℓ, v)` containing the position `i` (`0`-based), with the
offset of `i` in it; `none` when `i` is at least the total length. -/
def segFind : List (ℕ × ℕ) → ℕ → Option ((ℕ × ℕ) × ℕ)
| [], _ => none
| x :: xs, i => if i < x.1 then some (x, i) else segFind xs (i - x.1)
/-- The same search by descent in a position tree. -/
def posFind : RBTree.RBNode (ℕ × ℕ) → ℕ → Option ((ℕ × ℕ) × ℕ)
| .nil, _ => none
| .node _ l x r, i =>
if i < wsum l then posFind l i
else if i - wsum l < x.1 then some (x, i - wsum l)
else posFind r (i - wsum l - x.1)
/-- The node addresses of the position tree `t` stored at `p` (preorder). A node at `q` occupies
the six cells `q` (length), `q + 1` (left pointer), `q + 2` (right pointer), `q + 3` (color),
`q + 4` (payload), `q + 5` (total length of the left subtree). -/
def posNodes (m : Memory w) : ℕ → RBTree.RBNode (ℕ × ℕ) → List ℕ
| _, .nil => []
| p, .node _ l _ r => p :: (posNodes m (m (p + 1)).toNat l ++ posNodes m (m (p + 2)).toNat r)
/-- The six-cell node blocks at the addresses `ns` and at `ns'` are pairwise disjoint. -/
def Blocks6Disjoint (ns ns' : List ℕ) : Prop :=
∀ q ∈ ns, ∀ q' ∈ ns', q + 6 ≤ q' ∨ q' + 6 ≤ q
/-- `PosAt m lo hi p t`: the position tree `t` is stored at address `p` (`0` for the empty
tree): every node holds its segment `(ℓ, v)` (length at `q`, payload at `q + 4`), its color at
`q + 3` and the total length of its left subtree at `q + 5`; every node block lies in `[lo, hi)`,
and the blocks of distinct nodes are disjoint. -/
def PosAt (m : Memory w) (lo hi : ℕ) : ℕ → RBTree.RBNode (ℕ × ℕ) → Prop
| p, .nil => p = 0
| p, .node c l x r =>
p ≠ 0 ∧ lo ≤ p ∧ p + 6 ≤ hi ∧ (m p).toNat = x.1 ∧ (m (p + 4)).toNat = x.2 ∧
(m (p + 5)).toNat = wsum l ∧ (m (p + 3)).toNat = colorCode c ∧
PosAt m lo hi (m (p + 1)).toNat l ∧ PosAt m lo hi (m (p + 2)).toNat r ∧
Blocks6Disjoint [p] (posNodes m (m (p + 1)).toNat l) ∧
Blocks6Disjoint [p] (posNodes m (m (p + 2)).toNat r) ∧
Blocks6Disjoint (posNodes m (m (p + 1)).toNat l) (posNodes m (m (p + 2)).toNat r)
/-- An answer of the position search in the cells `g + 8, …, g + 11`: `none` is the flag
`[g + 8] = 0`; `some ((ℓ, v), o)` is the flag `1` with `[g + 9] = ℓ`, `[g + 10] = v` and the
offset `[g + 11] = o`. -/
def SegAt (m : Memory w) (g : ℕ) : Option ((ℕ × ℕ) × ℕ) → Prop
| none => (m (g + 8)).toNat = 0
| some ((a, b), o) => (m (g + 8)).toNat = 1 ∧ (m (g + 9)).toNat = a ∧
(m (g + 10)).toNat = b ∧ (m (g + 11)).toNat = o
/-- One step of the position search (registers from `g`: `g + 1` the current node, `g + 3` the
stop flag, `g + 4` scratch, `g + 5` the position relative to the current subtree, `g + 7` the
left length, the answer in `g + 8, …, g + 11`): stop at an empty subtree; go left when the
position lies in the left subtree; report the node when it lies in the node's segment; otherwise
go right with the position reduced by the left length and the segment length. -/
def posFindBody (g : ℕ) : Stmt :=
.ite .eq (.memory (.direct (g + 1))) (.immediate 0)
(.seq (.copy (.direct (g + 8)) (.immediate 0)) (.copy (.direct (g + 3)) (.immediate 1)))
(.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 1))) (.immediate 5))
(.seq (.copy (.direct (g + 7)) (.memory (.indirect (g + 4))))
(.ite .lt (.memory (.direct (g + 5))) (.memory (.direct (g + 7)))
(.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 1))) (.immediate 1))
(.copy (.direct (g + 1)) (.memory (.indirect (g + 4)))))
(.seq (.binary .sub (.direct (g + 5)) (.memory (.direct (g + 5)))
(.memory (.direct (g + 7))))
(.ite .lt (.memory (.direct (g + 5))) (.memory (.indirect (g + 1)))
(.seq (.copy (.direct (g + 9)) (.memory (.indirect (g + 1))))
(.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 1))) (.immediate 4))
(.seq (.copy (.direct (g + 10)) (.memory (.indirect (g + 4))))
(.seq (.copy (.direct (g + 11)) (.memory (.direct (g + 5))))
(.seq (.copy (.direct (g + 8)) (.immediate 1))
(.copy (.direct (g + 3)) (.immediate 1)))))))
(.seq (.binary .sub (.direct (g + 5)) (.memory (.direct (g + 5)))
(.memory (.indirect (g + 1))))
(.seq (.binary .add (.direct (g + 4)) (.memory (.direct (g + 1))) (.immediate 2))
(.copy (.direct (g + 1)) (.memory (.indirect (g + 4)))))))))))
/-- **Position search as a word-RAM program**: the segment containing the position `[g + 2]`
(`0`-based) of the position tree whose root pointer is in `g + 6`, with the offset of the
position in it, reported in the cells `g + 8, …, g + 11` (`SegAt`). -/
def posFindStmt (g : ℕ) : Stmt :=
.seq (.copy (.direct (g + 1)) (.memory (.direct (g + 6))))
(.seq (.copy (.direct (g + 5)) (.memory (.direct (g + 2))))
(.seq (.copy (.direct (g + 3)) (.immediate 0))
(.loop .lt (.memory (.direct (g + 3))) (.immediate 1) (posFindBody g))))
section posfind
variable (g lo hi i Rb : ℕ) (t : RBTree.RBNode (ℕ × ℕ)) (m₀ : Memory w)
/-- Hypotheses of the position search: registers in the cells `g, …, g + 11` below the node area
`[lo, hi)` (root pointer in `g + 6`, position `i` in `g + 2`), no overflow, the position tree
`t` stored at the root, and `Rb ≥ hi` bounds the space. -/
structure PosFindHyp : Prop where
below : g + 12 ≤ lo
lohi : lo ≤ hi
fit : hi < 2 ^ w.bits
tree : PosAt m₀ lo hi (m₀ (g + 6)).toNat t
pos : (m₀ (g + 2)).toNat = i
bound : hi ≤ Rb
end posfind
end CentralStrQ