Documentation

CslibFrontier.DataStructure.Dictionary.KickTree.Metadata

Pure Metadata Layer for the k-Kick Tree Dynamic Dictionary #

This module formalizes the pure metadata building blocks for the STOC 2022 k-kick tree dynamic dictionary of Bender, Farach-Colton, J. Kuszmaul, W. Kuszmaul, and Liu ([BFCK+22], arXiv:2111.00602v2, Section 4, pp. 24–26, and Section 5.1, pp. 27–28):

  1. Four-Russians Bit-Table Specifications (FourRussians) (Section 4.1, p. 24): abstract specifications for bit-parallel operations on small bit-vectors of width B (popcount, rank1, rank0, select1, select0, firstZeroBit, firstSetBit), proving rank complementation, rank/select bounds, soundness theorems select1_sound and select0_sound, rank-select inversion rank1_select1 and rank0_select0, and domain completeness select1_isSome_iff and select0_isSome_iff. The Table structure formalizes specification agreement; physical precomputed Word-RAM lookup tables are deferred to Stage A4.
  2. Mini-Array (MiniArray) (Section 4.2, pp. 24–25): abstract array specification supporting point-update semantics and unary delimiter bitmap sizing with exact bit-length theorem delimiters_length_eq_totalBits matching total bits $K + \sum_i s_i$; physical bit-packed Word-RAM representation is deferred to Stage A4.
  3. Local Query Router (LocalQueryRouter / PrefixTrie) (Section 4.3, pp. 25–26): binary prefix-trie router (adapting Lemma 15 for prefix-free key sets: callers supply prefix-free keys and insert? rejects collisions instead of extending stored prefixes via oracle $g$), with exact roundtrip decoding theorem decode_encode, collision-preventing insertion insert?, frame theorem query_insert_of_diverge, key preservation query_insert_two_distinct, and key extension routing query_insert_prefix.
  4. Free-Slot Tree (FreeSlotTree) (Section 5.1, pp. 27–28): constant-depth $B$-ary bitmap tree over $[0, B^{h+1})$ for depth-0 free-slot allocation and deallocation, with full-tree soundness findFree?_sound and completeness findFree?_complete across all heights, semantic bridge hasFree_iff_exists_isFree, full-height update/frame theorems isFree_setSlot_self and isFree_setSlot_of_ne, and preservation of structural validity Valid.

Source attribution #

Main definitions #

Main results #

References #

1. Four-Russians Bit-Table Specifications (arXiv:2111.00602 §4.1, p. 24) #

Population count: number of 1-bits in w < 2 ^ B.

Population count matches the length of the set-bit index list w.val.bitIndices.

Population count matches the sum of individual bits via Nat.sum_testBit_eq_length_bitIndices.

Prefix rank: count of 1-bits among the first k bit positions 0, 1, …, k - 1.

Prefix rank: count of 0-bits among the first k bit positions 0, 1, …, k - 1.

theorem Cslib.DataStructure.Dictionary.KickTree.FourRussians.rank0_add_rank1 (B : ℕ) (w : Fin (2 ^ B)) (k : Fin (B + 1)) :
rank0 B w k + rank1 B w k = ↑k

Complementation: rank0 w k + rank1 w k = k.

theorem Cslib.DataStructure.Dictionary.KickTree.FourRussians.rank1_le (B : ℕ) (w : Fin (2 ^ B)) (k : Fin (B + 1)) :
rank1 B w k ≤ ↑k

Rank 1 is bounded by the prefix length k.

theorem Cslib.DataStructure.Dictionary.KickTree.FourRussians.rank0_le (B : ℕ) (w : Fin (2 ^ B)) (k : Fin (B + 1)) :
rank0 B w k ≤ ↑k

Rank 0 is bounded by the prefix length k.

Full-prefix rank matches the total population count.

Selection: find the 0-indexed position of the k-th 1-bit (0-based index in Fin B).

Selection: find the 0-indexed position of the k-th 0-bit (0-based index in Fin B).

theorem Cslib.DataStructure.Dictionary.KickTree.FourRussians.select1_sound (B : ℕ) (w : Fin (2 ^ B)) (k : ℕ) (idx : Fin B) (h : select1 B w k = some idx) :
(↑w).testBit ↑idx = true

Soundness of select1: any returned position genuinely holds a 1-bit.

theorem Cslib.DataStructure.Dictionary.KickTree.FourRussians.select0_sound (B : ℕ) (w : Fin (2 ^ B)) (k : ℕ) (idx : Fin B) (h : select0 B w k = some idx) :
(↑w).testBit ↑idx = false

Soundness of select0: any returned position genuinely holds a 0-bit.

Find the least index of a 0-bit in w < 2 ^ B (none if all bits are 1).

Find the least index of a 1-bit in w < 2 ^ B (none if all bits are 0).

theorem Cslib.DataStructure.Dictionary.KickTree.FourRussians.rank1_select1 (B : ℕ) (w : Fin (2 ^ B)) (k : ℕ) (idx : Fin B) (h : select1 B w k = some idx) :
rank1 B w ⟨↑idx, ⋯⟩ = k

Rank-select inversion: if select1 B w k = some idx, the rank of idx is k.

theorem Cslib.DataStructure.Dictionary.KickTree.FourRussians.rank0_select0 (B : ℕ) (w : Fin (2 ^ B)) (k : ℕ) (idx : Fin B) (h : select0 B w k = some idx) :
rank0 B w ⟨↑idx, ⋯⟩ = k

Rank-select inversion: if select0 B w k = some idx, the 0-rank of idx is k.

Population count matches the length of the set-bit positions list.

Population count matches the BitString.weight semantic specification.

Domain completeness for select1: select1 B w k is defined if and only if k < popcount B w.

Domain completeness for select0: select0 B w k is defined if and only if k < B - popcount B w.

Specification of a Four-Russians bit-parallel lookup table of block size B.

  • lookup : Fin (2 ^ B) → α

    Direct table lookup function over Fin (2 ^ B).

  • spec : (Fin B → Bool) → α

    High-level semantic specification over bit-vectors Fin B → Bool.

  • correct (w : Fin (2 ^ B)) : self.lookup w = self.spec ((BitString.equivFin B).symm w)

    Table correctness: the table evaluation matches the semantic specification.

Four-Russians lookup table for population count with verified specification equivalence.

2. Mini-Array (arXiv:2111.00602 §4.2, pp. 24–25) #

Abstract mini-array sequence model (arXiv:2111.00602 §4.2, pp. 24–25): sequence of K items of type α supporting point updates and unary delimiter bitmap sizing for packed sequence representations. This represents the functional specification of the $K$-item array; physical bit-packing into machine words is addressed in Stage A4.

  • data : Fin K → α

    The K items stored in the mini-array.

def Cslib.DataStructure.Dictionary.KickTree.MiniArray.init {α : Type u_1} {K : ℕ} (defaultVal : α) :

Initialize a mini-array with a uniform default value.

Read the item at index i.

def Cslib.DataStructure.Dictionary.KickTree.MiniArray.write {α : Type u_1} {K : ℕ} (A : MiniArray α K) (i : Fin K) (val : α) :

Write a value at index i.

@[simp]
theorem Cslib.DataStructure.Dictionary.KickTree.MiniArray.read_write_same {α : Type u_1} {K : ℕ} (A : MiniArray α K) (i : Fin K) (val : α) :
(A.write i val).read i = val
theorem Cslib.DataStructure.Dictionary.KickTree.MiniArray.read_write_of_ne {α : Type u_1} {K : ℕ} (A : MiniArray α K) {i j : Fin K} (h : j ≠ i) (val : α) :
(A.write i val).read j = A.read j
@[simp]
theorem Cslib.DataStructure.Dictionary.KickTree.MiniArray.write_write_same {α : Type u_1} {K : ℕ} (A : MiniArray α K) (i : Fin K) (v₁ v₂ : α) :
(A.write i v₂).write i v₁ = A.write i v₁
theorem Cslib.DataStructure.Dictionary.KickTree.MiniArray.write_comm {α : Type u_1} {K : ℕ} (A : MiniArray α K) {i j : Fin K} (h : i ≠ j) (u v : α) :
(A.write i u).write j v = (A.write j v).write i u
def Cslib.DataStructure.Dictionary.KickTree.MiniArray.totalBits {α : Type u_1} {K : ℕ} (A : MiniArray α K) (sizeOf : α → ℕ) :

Total packed bit length: one delimiter bit per item plus the sum of item sizes.

def Cslib.DataStructure.Dictionary.KickTree.MiniArray.itemOffset {α : Type u_1} {K : ℕ} (A : MiniArray α K) (sizeOf : α → ℕ) (i : Fin K) :

Starting bit-offset of item i within the packed sequence.

Unary delimiter bitmap: sizeOf (A.data i) 0-bits followed by a 1-bit delimiter per item.

The total bit length of the unary delimiters equals totalBits.

Delimiter count: the total number of delimiter 1-bits across all K items is exactly K.

3. Local Query Router (arXiv:2111.00602 §4.3, Lemma 15, pp. 25–26) #

Binary prefix trie for local prefix routing (arXiv:2111.00602 §4.3, Lemma 15). In the terminology of Bender et al. (Lemma 15), this is an uncompressed binary trie where each leaf stores its payload value and internal nodes branch on successive bits of the key. Adaptation: in place of the paper's oracle $g$ dynamically extending prefixes during insertion, the formal router operates on prefix-free key sets, where insert? rejects collisions and returns none (zero silent drops or overwrites). Once a leaf is reached, its stored payload is returned for any key extension. Bit-packed Four-Russians decoding subroutines on Word-RAM belong to Stage A4.

Instances For
    @[instance_reducible]

    Total number of nodes in the prefix trie.

    Route a bit-sequence through the prefix trie. Once a leaf is reached (corresponding to the stored prefix), its stored payload is returned for any key extension (arXiv:2111.00602 §4.3, p. 25).

    Insert a stored prefix and value into the prefix trie. Returns none if a prefix collision occurs (a leaf already exists at or above k, or branch subtrees already exist below k), ensuring zero silent drops or overwrites.

    Inserting at or below an existing leaf returns none (zero silent drops).

    Routing a query through pruneBranch l r is semantically equivalent to routing through .branch l r.

    Delete a prefix from the prefix trie, removing the target leaf and contracting empty branches back to .empty.

    theorem Cslib.DataStructure.Dictionary.KickTree.PrefixTrie.query_insert_prefix {α : Type u_1} (val : α) (t t' : PrefixTrie α) (k ext : List Bool) (hins : insert? val t k = some t') :
    t'.query (k ++ ext) = some val

    Routing any key extension of a stored unique prefix returns the stored payload.

    theorem Cslib.DataStructure.Dictionary.KickTree.PrefixTrie.query_insert_same {α : Type u_1} (val : α) (t t' : PrefixTrie α) (k : List Bool) (hins : insert? val t k = some t') :
    t'.query k = some val

    Querying the exact inserted prefix returns the inserted value.

    Querying a deleted prefix returns none.

    theorem Cslib.DataStructure.Dictionary.KickTree.PrefixTrie.query_insert_of_diverge {α : Type u_1} (val : α) (p : List Bool) (b1 b2 : Bool) (hb : b1 ≠ b2) (s1 s2 : List Bool) (t t' : PrefixTrie α) (hins : insert? val t (p ++ b1 :: s1) = some t') :
    t'.query (p ++ b2 :: s2) = t.query (p ++ b2 :: s2)

    General frame theorem for insertion: inserting a key does not alter queries for any key that diverges from it at some bit position.

    theorem Cslib.DataStructure.Dictionary.KickTree.PrefixTrie.query_insert_two_distinct {α : Type u_1} (v1 v2 : α) (p : List Bool) (b1 b2 : Bool) (hb : b1 ≠ b2) (s1 s2 : List Bool) (t1 t2 : PrefixTrie α) (hins1 : insert? v1 empty (p ++ b1 :: s1) = some t1) (hins2 : insert? v2 t1 (p ++ b2 :: s2) = some t2) :
    t2.query (p ++ b1 :: s1) = some v1 ∧ t2.query (p ++ b2 :: s2) = some v2

    Key-preservation for distinct prefix-incomparable keys: inserting two divergent keys preserves the retrieval of both payloads.

    Deleting from the left branch does not alter queries routed to the right branch.

    Deleting from the right branch does not alter queries routed to the left branch.

    DFS tree-shape encoding $E_1$ (arXiv:2111.00602 §4.3, p. 25): preorder sequence encoding node types ([false, false] for empty, [false, true] for leaf, true :: (encodeE1 l ++ encodeE1 r) for branch), adapting the binary tree shape for unambiguous DFS reconstruction.

    Leaf payload encoding $E_2$ (arXiv:2111.00602 §4.3, p. 25): list of leaf values in DFS traversal order (List α).

    Reconstruct a prefix trie from its $(E_1, E_2)$ DFS encodings.

    Exact DFS roundtrip decoding theorem for prefix tries.

    @[reducible, inline]

    Local query router alias for the prefix trie.

    4. Free-Slot Tree (arXiv:2111.00602 §5.1, pp. 27–28) #

    Constant-depth $B$-ary bitmap tree over $[0, B^{h+1})$ for depth-0 free-slot allocation.

    Check whether the tree has at least one free slot.

    Test whether a slot is free in the tree.

    Find the first free slot index in the tree, if one exists.

    Set the occupancy of a slot in the tree (b = true for free, b = false for allocated).

    Mark a slot as occupied (allocated).

    Mark a slot as free (deallocated).

    Bitmap tree structural validity invariant: summary bits accurately reflect child occupancy.

    Setting a slot preserves structural validity across all tree levels.

    Allocating a slot preserves structural validity across all tree levels.

    Freeing a slot preserves structural validity across all tree levels.

    Full-tree soundness of free-slot lookup: any slot index returned by findFree? is genuinely free.

    Full-tree completeness of free-slot lookup: if hasFree t = true and Valid t, findFree? succeeds in finding a free slot.

    Semantic completeness bridge: the summary bitmap reflects existence of free slots.

    Full-tree semantic completeness: if any slot in the tree is free, findFree? succeeds.

    theorem Cslib.DataStructure.Dictionary.KickTree.FreeSlotTree.isFree_setSlot_self {B h : ℕ} (t : FreeSlotTree B h) (b : Bool) (idx : ℕ) :
    idx < B ^ (h + 1) → (setSlot b idx t).isFree idx = b

    Updating a slot to b correctly sets the slot occupancy at idx across all tree heights.

    Updating slot idx preserves the occupancy of any distinct slot j ≠ idx.

    theorem Cslib.DataStructure.Dictionary.KickTree.FreeSlotTree.allocSlot_self {B h : ℕ} (t : FreeSlotTree B h) {idx : ℕ} (hidx : idx < B ^ (h + 1)) :
    (t.allocSlot idx).isFree idx = false

    Allocating slot idx marks it as occupied across all tree heights.

    theorem Cslib.DataStructure.Dictionary.KickTree.FreeSlotTree.freeSlot_self {B h : ℕ} (t : FreeSlotTree B h) {idx : ℕ} (hidx : idx < B ^ (h + 1)) :
    (t.freeSlot idx).isFree idx = true

    Freeing slot idx marks it as free across all tree heights.

    Allocating slot idx preserves the status of any distinct slot j ≠ idx.

    Freeing slot idx preserves the status of any distinct slot j ≠ idx.

    theorem Cslib.DataStructure.Dictionary.KickTree.FreeSlotTree.allocSlot_leaf_self {B : ℕ} (freeBits : Fin B → Bool) (i : Fin B) :
    ((leaf freeBits).allocSlot ↑i).isFree ↑i = false

    Allocating leaf slot i marks it as occupied.

    theorem Cslib.DataStructure.Dictionary.KickTree.FreeSlotTree.freeSlot_leaf_self {B : ℕ} (freeBits : Fin B → Bool) (i : Fin B) :
    ((leaf freeBits).freeSlot ↑i).isFree ↑i = true

    Freeing leaf slot i marks it as free.

    theorem Cslib.DataStructure.Dictionary.KickTree.FreeSlotTree.allocSlot_leaf_of_ne {B : ℕ} (freeBits : Fin B → Bool) {i j : Fin B} (h : j ≠ i) :
    ((leaf freeBits).allocSlot ↑i).isFree ↑j = (leaf freeBits).isFree ↑j

    Allocating leaf slot i preserves distinct leaf slot j ≠ i.

    theorem Cslib.DataStructure.Dictionary.KickTree.FreeSlotTree.freeSlot_leaf_of_ne {B : ℕ} (freeBits : Fin B → Bool) {i j : Fin B} (h : j ≠ i) :
    ((leaf freeBits).freeSlot ↑i).isFree ↑j = (leaf freeBits).isFree ↑j

    Freeing leaf slot i preserves distinct leaf slot j ≠ i.