Documentation

CslibFrontier.DataStructure.Dictionary.SuccinctWhpSwarBTreeSpec

Subtree Correctness and Node-Splitting Lemmas for the SWAR B+-Tree #

This module proves sorted SWAR rank routing (swarRank_sorted_spec, branch_child_routing), subtree lookup, deletion, and in-place value update (btreeLookup_eq_of_subtree, btreeDelete_subtree_spec, btreeUpdate_subtree_spec), unified proactive full-node splitting (splitFullNode_spec, splitRootIfFull_spec), and leaf insertion (finishLeafInsert_leaf_spec) for the top-down split-only SWAR B+-tree defined in CslibFrontier.DataStructure.Dictionary.SuccinctWhpSwarBTree.

Main results #

References #

Section 1: Sorted SWAR rank characterization, descent, lookup, delete, and update #

theorem Cslib.DataStructure.Dictionary.SuccinctWhp.swarRank_sorted_spec {fpBits capLanes cnt q keysWord : ℕ} (hcnt : cnt < capLanes) (hcap : capLanes < 2 ^ (fpBits + 2)) (hq : q < 2 ^ fpBits) (hkw : keysWord < 2 ^ (cnt * (fpBits + 2))) (hlanes : ∀ j < cnt, laneGet (fpBits + 2) keysWord j < 2 ^ fpBits) (hmono : ∀ (i j : ℕ), i < j → j < cnt → laneGet (fpBits + 2) keysWord i < laneGet (fpBits + 2) keysWord j) :
have r := swarRank fpBits capLanes cnt q keysWord; r ≤ cnt ∧ (∀ j < r, laneGet (fpBits + 2) keysWord j < q) ∧ ∀ (j : ℕ), r ≤ j → j < cnt → q ≤ laneGet (fpBits + 2) keysWord j

Partition property of swarRank on a strictly increasing lane array: r ≤ cnt, lanes < r are < q, and lanes r .. cnt - 1 are ≥ q.

theorem Cslib.DataStructure.Dictionary.SuccinctWhp.branch_child_routing {isRoot : Bool} {fpBits W_val tMin alloc idx cnt lo hi q : ℕ} {nodes : ℕ → BTreeNode} {mapFn : ℕ → Option ℕ} {cs : Fin (cnt + 1) → BTreeTree} (ht2 : 2 ≤ tMin) (hcap : 2 * tMin < 2 ^ (fpBits + 2)) (hv : ValidSubtree isRoot fpBits W_val tMin alloc nodes lo hi mapFn (BTreeTree.branch idx cnt cs)) (hhi : hi < 2 ^ fpBits) (hqlo : lo ≤ q) (hqhi : q ≤ hi) :
have W_key := fpBits + 2; have nd := nodes idx; have r := swarRank fpBits (2 * tMin) cnt q nd.keysWord; r ≤ cnt ∧ childLo W_key lo nd.keysWord r ≤ q ∧ q ≤ childHi W_key hi cnt nd.keysWord r ∧ ∀ (i : Fin (cnt + 1)), ↑i ≠ r → ∀ (q' : ℕ), childLo W_key lo nd.keysWord ↑i ≤ q' → q' ≤ childHi W_key hi cnt nd.keysWord ↑i → q' ≠ q

Internal branch routing: swarRank selects the unique child interval [childLo r, childHi r] containing q, and every other child interval is disjoint from {q}.

theorem Cslib.DataStructure.Dictionary.SuccinctWhp.btreeLookup_eq_of_subtree {fpBits W_val tMin : ℕ} {sl : BTreeSlice} {mapFn : ℕ → Option ℕ} {T : BTreeTree} {q : ℕ} (ht2 : 2 ≤ tMin) (hcap : 2 * tMin < 2 ^ (fpBits + 2)) (halloc_pos : 1 ≤ sl.alloc) (hroot : T.rootIdx = sl.root) (hv : ValidSubtree true fpBits W_val tMin sl.alloc sl.nodes 0 (2 ^ fpBits - 1) mapFn T) (hfuel : T.height ≤ hMaxBTree) (hq : q < 2 ^ fpBits) :
btreeLookup fpBits W_val tMin sl q = mapFn q

Exact lookup specification on any valid subtree T with T.height ≤ hMaxBTree.

theorem Cslib.DataStructure.Dictionary.SuccinctWhp.btreeDelete_subtree_spec {fpBits W_val tMin : ℕ} {sl : BTreeSlice} {mapFn : ℕ → Option ℕ} {T : BTreeTree} {q : ℕ} (ht2 : 2 ≤ tMin) (hcap : 2 * tMin < 2 ^ (fpBits + 2)) (halloc_pos : 1 ≤ sl.alloc) (hroot : T.rootIdx = sl.root) (hv : ValidSubtree true fpBits W_val tMin sl.alloc sl.nodes 0 (2 ^ fpBits - 1) mapFn T) (hfuel : T.height ≤ hMaxBTree) (hq : q < 2 ^ fpBits) :
have sl' := btreeDelete fpBits W_val tMin sl q; sl'.alloc = sl.alloc ∧ ∃ (T' : BTreeTree), T'.rootIdx = sl'.root ∧ T'.height = T.height ∧ BTreeTree.potential tMin T' ≤ BTreeTree.potential tMin T ∧ ValidSubtree true fpBits W_val tMin sl'.alloc sl'.nodes 0 (2 ^ fpBits - 1) (Function.update mapFn q none) T'

Subtree deletion specification: btreeDelete preserves ValidSubtree, keeps alloc and height unchanged, does not increase potential, and updates mapFn to Function.update mapFn q none.

theorem Cslib.DataStructure.Dictionary.SuccinctWhp.btreeUpdate_subtree_spec {fpBits W_val tMin : ℕ} {sl : BTreeSlice} {mapFn : ℕ → Option ℕ} {T : BTreeTree} {q newVal : ℕ} (ht2 : 2 ≤ tMin) (hcap : 2 * tMin < 2 ^ (fpBits + 2)) (halloc_pos : 1 ≤ sl.alloc) (hroot : T.rootIdx = sl.root) (hv : ValidSubtree true fpBits W_val tMin sl.alloc sl.nodes 0 (2 ^ fpBits - 1) mapFn T) (hfuel : T.height ≤ hMaxBTree) (hq : q < 2 ^ fpBits) (hval : newVal < 2 ^ W_val) :
have sl' := btreeUpdate fpBits W_val tMin sl q newVal; have mapFn' := if (mapFn q).isSome = true then Function.update mapFn q (some newVal) else mapFn; sl'.alloc = sl.alloc ∧ ∃ (T' : BTreeTree), T'.rootIdx = sl'.root ∧ T'.height = T.height ∧ BTreeTree.potential tMin T' ≤ BTreeTree.potential tMin T ∧ ValidSubtree true fpBits W_val tMin sl'.alloc sl'.nodes 0 (2 ^ fpBits - 1) mapFn' T'

Subtree in-place value update specification: when newVal < 2 ^ W_val, btreeUpdate preserves ValidSubtree, alloc, height, and potential, updating mapFn q to some newVal whenever q is present (and leaving mapFn unchanged when mapFn q = none).

Section 2: Proactive node splitting (splitFullNode, splitRootIfFull) #

theorem Cslib.DataStructure.Dictionary.SuccinctWhp.splitFullNode_spec {isRoot : Bool} {fpBits W_val tMin alloc alloc' lo hi leftIdx rightIdx : ℕ} {nodes nodes' : ℕ → BTreeNode} {mapFn : ℕ → Option ℕ} {T : BTreeTree} (ht2 : 2 ≤ tMin) (hv : ValidSubtree isRoot fpBits W_val tMin alloc nodes lo hi mapFn T) (hfull : metaCount (nodes T.rootIdx).metaWord = 2 * tMin - 1) (halloc : alloc ≤ alloc') (hl_lt : leftIdx < alloc') (hr_lt : rightIdx < alloc') (hlr_ne : leftIdx ≠ rightIdx) (hl_nd : nodes' leftIdx = (splitFullNode fpBits W_val tMin (nodes T.rootIdx)).left) (hr_nd : nodes' rightIdx = (splitFullNode fpBits W_val tMin (nodes T.rootIdx)).right) (herase : ∀ v ∈ T.nodeSet.erase T.rootIdx, nodes' v = nodes v ∧ v ≠ leftIdx ∧ v ≠ rightIdx) :
have res := splitFullNode fpBits W_val tMin (nodes T.rootIdx); have sep := res.sep; ∃ (leftT : BTreeTree) (rightT : BTreeTree), leftT.rootIdx = leftIdx ∧ rightT.rootIdx = rightIdx ∧ metaCount (nodes' leftIdx).metaWord ≤ tMin ∧ metaCount (nodes' rightIdx).metaWord = tMin - 1 ∧ leftT.height = T.height ∧ rightT.height = T.height ∧ leftT.nodeSet ⊆ insert leftIdx (T.nodeSet.erase T.rootIdx) ∧ rightT.nodeSet ⊆ insert rightIdx (T.nodeSet.erase T.rootIdx) ∧ Disjoint leftT.nodeSet rightT.nodeSet ∧ lo ≤ sep ∧ sep < hi ∧ BTreeTree.potential tMin leftT + BTreeTree.potential tMin rightT + (tMin - 1) ≤ BTreeTree.potential tMin T ∧ ValidSubtree false fpBits W_val tMin alloc' nodes' lo sep mapFn leftT ∧ ValidSubtree false fpBits W_val tMin alloc' nodes' (sep + 1) hi mapFn rightT

Unified specification of splitFullNode on any full (2 * tMin - 1-key) valid subtree T (leaf or internal branch), covering both splitRootIfFull (leftIdx = alloc, rightIdx = alloc + 1) and descendSplitToLeaf (leftIdx = T.rootIdx, rightIdx = alloc).

theorem Cslib.DataStructure.Dictionary.SuccinctWhp.splitRootIfFull_spec {fpBits W_val tMin : ℕ} {sl : BTreeSlice} {mapFn : ℕ → Option ℕ} {T : BTreeTree} (ht3 : 3 ≤ tMin) (halloc_pos : 1 ≤ sl.alloc) (halloc_val : sl.alloc + 1 < 2 ^ W_val) (hroot : T.rootIdx = sl.root) (hv : ValidSubtree true fpBits W_val tMin sl.alloc sl.nodes 0 (2 ^ fpBits - 1) mapFn T) :
have sl1 := splitRootIfFull fpBits W_val tMin sl; 1 ≤ sl1.alloc ∧ sl1.alloc ≤ sl.alloc + 2 ∧ sl1.root = sl.root ∧ ∃ (T1 : BTreeTree), T1.rootIdx = sl1.root ∧ metaCount (sl1.nodes sl1.root).metaWord < 2 * tMin - 1 ∧ T1.height ≤ T.height + 1 ∧ (tMin - 2) * (sl1.alloc - 1) + 2 * BTreeTree.potential tMin T1 ≤ (tMin - 2) * (sl.alloc - 1) + 2 * BTreeTree.potential tMin T ∧ ValidSubtree true fpBits W_val tMin sl1.alloc sl1.nodes 0 (2 ^ fpBits - 1) mapFn T1

Specification of splitRootIfFull: ensures the root is strictly non-full (< 2 * tMin - 1 keys), increases height by at most 1, allocates at most 2 nodes, and never increases the excess-key split potential (tMin - 2) * (alloc - 1) + 2 * T.potential tMin when 3 ≤ tMin.

theorem Cslib.DataStructure.Dictionary.SuccinctWhp.finishLeafInsert_leaf_spec {isRoot : Bool} {fpBits W_val tMin leafIdx cnt lo hi q val : ℕ} {sl : BTreeSlice} {mapFn : ℕ → Option ℕ} (ht2 : 2 ≤ tMin) (hcap : 2 * tMin < 2 ^ (fpBits + 2)) (hval : val < 2 ^ W_val) (hhi : hi < 2 ^ fpBits) (hqlo : lo ≤ q) (hqhi : q ≤ hi) (hv : ValidSubtree isRoot fpBits W_val tMin sl.alloc sl.nodes lo hi mapFn (BTreeTree.leaf leafIdx cnt)) (hnonfull : cnt < 2 * tMin - 1) :
have sl' := finishLeafInsert fpBits W_val tMin sl leafIdx q val; sl'.alloc = sl.alloc ∧ sl'.root = sl.root ∧ (∀ (v : ℕ), v ≠ leafIdx → sl'.nodes v = sl.nodes v) ∧ ∃ cnt' ≤ cnt + 1, ValidSubtree isRoot fpBits W_val tMin sl'.alloc sl'.nodes lo hi (Function.update mapFn q (some val)) (BTreeTree.leaf leafIdx cnt')

Exact specification of finishLeafInsert on a non-full (cnt < 2 * tMin - 1) valid leaf.