Auxiliary notions for the correctness proof of positional insertion on the CentralStrQ word RAM
DefinitionCentralStrQ_PosInsertAuxaugmented-treeshoare-logicred-black-treesverified-programmingword-ram
This bundle states the notions in which the correctness proof of the positional insertion program posInsertStmt g (bundle Def_CentralStrQ_PosInsert) is split into separately verified pieces. It adds no new program.
Search paths in position trees.
PFrame: a frame of a path, with the color, the segment and the sibling subtree of a node, and the direction (left) taken there.pPlug P s: the tree made of the pathP(frames listed from the root down) with the subtreesplugged in at its end.PFrame.up f uandpUp P u: the steps ofposInsalong a path, applied bottom-up to the resultuof the recursive call. A red frame just rebuilds the node; a black frame applies Batteries'balance1(left) orbalance2(right).PosToward s i P j: the pathPis exactly the descent ofposInsfrom the position . At a left frame of the left subtree; at a right frame and the position becomes . The descent arrives at the position in .
Memory along a path (six-cell nodes, PosAt of Def_CentralStrQ_PosTrees):
pSlot m c P: the slot cell (root cell or child pointer cell) reached from the slot cell by following the pathPin memory .pLeftCells m c P: the left-length cells of the nodes at whichPgoes left.InBlocks6 ns x: the cell lies in the six-cell block of one of the nodes .
Outcome of one rebalancing step (PBalPost g lo hi Rb c q old new m₀ m r):
- the slot now stores
new(a valid position tree), and its nodes are among the old nodes ofold, rooted at ; - only cells of old node blocks, the slot and the scratch registers , , , changed;
- the space usage is .
Loop invariants.
posDescInv(the descent, with remaining budget ). The tree ispPlug P s, withPosToward s i P j, and the stack from holds the slots of the path. Either the descent continues at the slot of : the left lengths of the left turns already include , and nothing else changed. Or the new leaf has been allocated and linked: the root cell storespPlug P (posIns x s j)in .posAscInv(the ascent, with path nodes left on the stack). The register holds the slot of the rebuilt subtree. The root cell stores the path prefixP.take k, withpUp (P.drop k) (posIns x s j)plugged in.
Definition code
import Mathlib
import Definitions.Def_CentralStrQ_Machine
import Definitions.Def_CentralStrQ_Hoare
import Definitions.Def_CentralStrQ_Structured
import Definitions.Def_CentralStrQ_RBTrees
import Definitions.Def_CentralStrQ_PosTrees
import Definitions.Def_CentralStrQ_PosInsert
set_option autoImplicit false
namespace CentralStrQ
open WordRAM
/-!
# Positional insertion on the `CentralStrQ` word RAM: auxiliary definitions of the proof
The notions in which the correctness proof of `posInsertStmt` (`Def_CentralStrQ_PosInsert`) is
stated in pieces.
* Search paths in position trees: frames `PFrame` (color, segment, sibling subtree, direction),
`pPlug P s` (the tree made of the path `P` with `s` plugged in), `PFrame.up` / `pUp` (the steps
of `posIns` along a path, bottom-up), `PosToward s i P j` (the path is the descent of `posIns`
from the position `i`, arriving at `j`).
* Memory along a path: `pSlot m c P` (the slot cell reached from the slot `c`), `pLeftCells m c P`
(the left-length cells of the nodes where the path turns left), `InBlocks6 ns x` (the cell `x`
lies in one of the six-cell node blocks `ns`).
* `PBalPost`: the outcome of one rebalancing step at a slot; `posDescInv` / `posAscInv`: the loop
invariants of the descent and of the ascent.
-/
variable {w : Width}
/-- One step of a search path in a position tree: the node's color and segment, the subtree that
is not on the path, and whether the path continues to the left. -/
structure PFrame where
col : RBTree.RBColor
key : ℕ × ℕ
sib : RBTree.RBNode (ℕ × ℕ)
left : Bool
/-- Plug a subtree into a path (frames listed from the root down). -/
def pPlug : List PFrame → RBTree.RBNode (ℕ × ℕ) → RBTree.RBNode (ℕ × ℕ)
| [], s => s
| f :: P, s => if f.left then .node f.col (pPlug P s) f.key f.sib
else .node f.col f.sib f.key (pPlug P s)
/-- The step of `posIns` at a frame, given the result `u` of the recursive call. -/
def PFrame.up (f : PFrame) (u : RBTree.RBNode (ℕ × ℕ)) : RBTree.RBNode (ℕ × ℕ) :=
match f.col, f.left with
| .red, true => .node .red u f.key f.sib
| .red, false => .node .red f.sib f.key u
| .black, true => RBTree.RBNode.balance1 u f.key f.sib
| .black, false => RBTree.RBNode.balance2 f.sib f.key u
/-- Apply the steps of `posIns` along a path, from the bottom up. -/
def pUp : List PFrame → RBTree.RBNode (ℕ × ℕ) → RBTree.RBNode (ℕ × ℕ)
| [], u => u
| f :: P, u => f.up (pUp P u)
/-- The path `P` (with the subtree `s` plugged in) is the descent of `posIns` from the position
`i`, arriving at the position `j` in `s`. -/
def PosToward (s : RBTree.RBNode (ℕ × ℕ)) : ℕ → List PFrame → ℕ → Prop
| i, [], j => j = i
| i, f :: P, j => if f.left then i ≤ wsum (pPlug P s) ∧ PosToward s i P j
else wsum f.sib < i ∧ PosToward s (i - wsum f.sib - f.key.1) P j
/-- The cell `x` lies in the six-cell block of one of the nodes `ns`. -/
def InBlocks6 (ns : List ℕ) (x : ℕ) : Prop := ∃ q ∈ ns, q ≤ x ∧ x < q + 6
/-- The slot cell reached from the slot cell `c` by following the path `P` in memory `m`. -/
def pSlot (m : Memory w) (c : ℕ) : List PFrame → ℕ
| [] => c
| f :: P => pSlot m ((m c).toNat + if f.left then 1 else 2) P
/-- The left-length cells of the nodes at which the path `P` (from the slot `c`) goes left. -/
def pLeftCells (m : Memory w) (c : ℕ) : List PFrame → List ℕ
| [] => []
| f :: P => (if f.left then [(m c).toNat + 5] else []) ++
pLeftCells m ((m c).toNat + if f.left then 1 else 2) P
/-- The outcome of a rebalancing step at the slot `c` whose old subtree `old` is rooted at `q`:
the slot holds `new` on old nodes, only old node cells, the slot and the scratch registers
changed, and the space usage is at most `Rb`. -/
def PBalPost (g lo hi Rb c q : ℕ) (old new : RBTree.RBNode (ℕ × ℕ)) (m₀ : Memory w) (m : Memory w)
(r : ℕ) : Prop :=
PosAt m lo hi (m c).toNat new ∧ (∀ p ∈ posNodes m (m c).toNat new, p ∈ posNodes m₀ q old) ∧
(∀ v, ¬ InBlocks6 (posNodes m₀ q old) v → v ≠ c → v ≠ g + 4 → v ≠ g + 9 → v ≠ g + 10 →
v ≠ g + 11 → m v = m₀ v) ∧ r ≤ Rb
/-- Loop invariant of the descent with budget `K`: the tree is the path `P` with the subtree `s`
plugged in, reached from the position `i` at the position `j`; the stack holds the slots of the
path; either the descent goes on at the slot of `s` (the left lengths of the left turns of the
path already include the new length, nothing else changed), or the leaf has been linked and the
root cell holds `pPlug P (posIns x s j)`. -/
def posDescInv (g lo hi Rb : ℕ) (x : ℕ × ℕ) (i : ℕ) (t : RBTree.RBNode (ℕ × ℕ)) (m₀ : Memory w)
(K : ℕ) (m : Memory w) (r : ℕ) : Prop :=
r ≤ Rb ∧ m (g + 2) = m₀ (g + 2) ∧ m (g + 8) = m₀ (g + 8) ∧ m (g + 12) = m₀ (g + 12) ∧
∃ (P : List PFrame) (s : RBTree.RBNode (ℕ × ℕ)) (j : ℕ), t = pPlug P s ∧ PosToward s i P j ∧
P.length + s.depth ≤ t.depth ∧ (m (g + 7)).toNat = g + 16 + P.length ∧
(∀ k < P.length, (m (g + 16 + k)).toNat = pSlot m₀ (g + 6) (P.take k)) ∧
(((m (g + 3)).toNat = 0 ∧ (m g).toNat = pSlot m₀ (g + 6) P ∧ s.depth < K ∧
(m (g + 13)).toNat = j ∧
(∀ v ∈ pLeftCells m₀ (g + 6) P, (m v).toNat = (m₀ v).toNat + x.1) ∧
(∀ v, ((v < g + 16 ∧ v ≠ g ∧ v ≠ g + 1 ∧ v ≠ g + 3 ∧ v ≠ g + 4 ∧ v ≠ g + 7 ∧
v ≠ g + 13 ∧ v ≠ g + 14) ∨ g + 16 + P.length ≤ v) →
v ∉ pLeftCells m₀ (g + 6) P → m v = m₀ v)) ∨
((m (g + 3)).toNat ≠ 0 ∧ (m (g + 5)).toNat = hi + 6 ∧
PosAt m lo (hi + 6) (m (g + 6)).toNat (pPlug P (posIns x s j)) ∧
(m g).toNat = pSlot m (g + 6) P ∧
(∀ k ≤ P.length, pSlot m (g + 6) (P.take k) = pSlot m₀ (g + 6) (P.take k)) ∧
(∀ v, v < g ∨ (g + 16 + P.length ≤ v ∧ v < lo) ∨ hi + 6 ≤ v → m v = m₀ v)))
/-- Loop invariant of the ascent with `k` path nodes left on the stack: the slot register `g`
holds the slot of the rebuilt subtree, and the root cell holds the path prefix above it with the
bottom-up steps of `posIns` applied below. -/
def posAscInv (g lo hi Rb : ℕ) (x : ℕ × ℕ) (i : ℕ) (t : RBTree.RBNode (ℕ × ℕ)) (m₀ : Memory w)
(k : ℕ) (m : Memory w) (r : ℕ) : Prop :=
r ≤ Rb ∧ m (g + 2) = m₀ (g + 2) ∧ m (g + 8) = m₀ (g + 8) ∧ m (g + 12) = m₀ (g + 12) ∧
∃ (P : List PFrame) (s : RBTree.RBNode (ℕ × ℕ)) (j : ℕ), t = pPlug P s ∧ PosToward s i P j ∧
P.length ≤ t.depth ∧ k ≤ P.length ∧ (m (g + 5)).toNat = hi + 6 ∧
(m (g + 7)).toNat = g + 16 + k ∧
(∀ j' < k, (m (g + 16 + j')).toNat = pSlot m (g + 6) (P.take j')) ∧
(m g).toNat = pSlot m (g + 6) (P.take k) ∧
PosAt m lo (hi + 6) (m (g + 6)).toNat (pPlug (P.take k) (pUp (P.drop k) (posIns x s j))) ∧
(∀ v, v < g ∨ (g + 16 + t.depth ≤ v ∧ v < lo) ∨ hi + 6 ≤ v → m v = m₀ v)
end CentralStrQ
Source
Positional insertion into balanced trees of segments ordered by position, augmented with the total length of the left subtree (order-statistic trees; T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein, Introduction to Algorithms, 3rd ed., MIT Press 2009, Chapter 14 Augmenting Data Structures), with Okasaki insertion into red-black trees (C. Okasaki, Red-black trees in a functional setting, J. Funct. Programming 9(4), 1999, https://doi.org/10.1017/S0956796899003494; L. J. Guibas, R. Sedgewick, A dichromatic framework for balanced trees, FOCS 1978, https://doi.org/10.1109/SFCS.1978.3) as formalized in the Lean library Batteries (module Batteries.Recycling.RBTree: RBNode.balance1, RBNode.balance2, RBNode.toList, balance1_toList, balance2_toList, setBlack_toList, RBNode.Balanced, RedRed.balance1, RedRed.balance2, Balanced.ins, Balanced.insert, Balanced.depth_bound; authors L. de Moura, M. Carneiro, https://github.com/leanprover-community/batteries). Used to maintain the phrases of a parsing of a dynamic string (Boneh, Fried, Golan, Kraus, Dynamic LZ77: No Space for Improvement, STOC 2027 submission, Section 4.1, p. 16, dynamic algorithms and edit operations). Verified with the published structured Hoare logic Def_CentralStrQ_Structured on the published deterministic word RAM Def_CentralStrQ_Machine (Kempa, Kociumaka, A Complete Characterization of Central String Queries in Compact Space, STOC 2027 submission, Section 3.2, p. 13; T. Hagerup, Sorting and searching on the word RAM, STACS 1998, https://doi.org/10.1007/BFb0028575); the machine-level proof adapts the published red-black insertion of Def_CentralStrQ_RBTrees (rbInsert_spec); Hoare-style rules after C. A. R. Hoare, An axiomatic basis for computer programming, Commun. ACM 12(10), 1969, https://doi.org/10.1145/363235.363259.