Documentation

CslibFrontier.DataStructure.Dictionary.SuccinctWhpBufferTrie

Bounded-Depth Character Trie Buffer (BufTrie) for Deamortized Salt Collisions #

This module formalizes the pure model and exact invariants of the constant-depth character trie (BufTrie) used for the growing and shrinking epoch buffers (BufCur and BufOld) in the with-high-probability succinct dynamic dictionary (Bender, Farach-Colton, J. Kuszmaul, and W. Kuszmaul, SOSA 2024, Section 2.2 Lemma 2.2 and Section 5, pp. 363–373; author copy pp. 3 and 6).

Each Stage-1 reduced key rKey < 2 ^ (cTab * bChar) is decomposed into cTab digits in base 2 ^ bChar (Cslib.DataStructure.laneGet, eq_of_forall_laneGet_eq_of_lt). Augmenting SOSA 2024's k-trie for deterministic per-step background draining and cleanup without scanning all 2 ^ bChar child slots, a BufTrie stores:

Cost convention: All O(1) and O(cTab) operation descriptions in this module count logical table accesses/updates or clear pops (with cTab fixed). The definitions use pure Nat arithmetic and functional ℕ → ℕ tables; this module does not certify Lean runtime or Word-RAM instruction/memory cost.

Main definitions #

Main results #

References #

Section 1: Pure BufTrie state and logical buffer operations #

Pure state of a bounded-depth character trie buffer with append-only item log and parent-edge log for incremental re-zeroing in O(1) logical table updates per clear pop.

  • nodeAlloc : ℕ

    Number of allocated trie nodes (1 initially; node 0 is the root).

  • itemAlloc : ℕ

    Number of allocated item log slots (0 .. itemAlloc - 1).

  • trieChild : ℕ → ℕ → ℕ

    Child pointer table: 0 denotes absent, u ≥ 1 denotes child node u.

  • nodeParent : ℕ → ℕ

    Parent node of allocated non-root trie node u ≥ 1.

  • nodeChar : ℕ → ℕ

    Character edge from nodeParent u to u.

  • nodeLeafItem : ℕ → ℕ

    Epoch-allocated item slot + 1 at leaf node u (0 if never allocated in this epoch; retained across deletions while nodeActive records whether the slot is currently active).

  • nodeActive : ℕ → ℕ

    Active flag (0 if deleted/inactive, non-zero if currently active) at leaf node u.

  • itemLeaf : ℕ → ℕ

    Leaf trie node associated with item log slot id.

  • itemBucket : ℕ → ℕ

    Stored bucket index b for item log slot id.

  • itemLoc : ℕ → ℕ

    Stored local coordinate loc for item log slot id.

def Cslib.DataStructure.Dictionary.SuccinctWhp.bufWalk (bChar : ℕ) (child : ℕ → ℕ → ℕ) (rKey : ℕ) :
ℕ → ℕ → ℕ → Option ℕ

Walk rem digits starting at digit offset off from trie node cur, returning some leaf or none if a zero child pointer is met.

Lookup rKey in tr, returning some (b, loc) if present or none if absent.

Whether rKey has ever been assigned a leaf item slot in tr during the current epoch.

Walk and allocate missing trie nodes along rem digits starting at off from cur, returning (tr', leaf).

Insert or overwrite rKey ↦ (b, loc) in tr:

  • If rKey's leaf already has an allocated epoch slot id = id1 - 1 (active or previously deleted), reactivates and updates (itemBucket, itemLoc) in place without incrementing itemAlloc or nodeAlloc.
  • Otherwise allocates id := tr1.itemAlloc (itemAlloc + 1) and links leaf ↔ id.

Delete rKey from tr in O(cTab) logical table accesses and at most one flag update (clearing nodeActive leaf := 0 while preserving its epoch slot nodeLeafItem leaf so re-insertions of rKey allocate zero new slots; pure functional model, not certified Lean or Word-RAM execution cost).

Scan probe at item slot id < tr.itemAlloc using O(1) logical table reads (pure functional model, not certified Lean or Word-RAM execution cost): returns some (b, loc) iff item id is still active (nodeLeafItem (itemLeaf id) = id + 1 ∧ nodeActive (itemLeaf id) ≠ 0).

One elementary cleanup pop using O(1) logical table reads/updates (pure functional model, not certified Lean or Word-RAM execution cost): zeroes itemAlloc := 0 on every step, pops one allocated non-root node u = nodeAlloc - 1 and re-zeroes trieChild (nodeParent u) (nodeChar u) and nodeLeafItem u when 1 < nodeAlloc, and sets nodeAlloc := 1 and nodeLeafItem 0 := 0 when nodeAlloc ≤ 1.

Section 2: Exact structural and map invariant (ValidBufTrie) #

Structural, parent-log, and map-refinement invariant for BufTrie at character count cTab and character bit-width bChar.

theorem Cslib.DataStructure.Dictionary.SuccinctWhp.ValidBufTrie.lookup_eq {cTab bChar rKey : ℕ} {tr : BufTrie} {mapFn : ℕ → Option (ℕ × ℕ)} (hv : ValidBufTrie cTab bChar tr mapFn) (hk : rKey < 2 ^ (cTab * bChar)) :
bufLookup cTab bChar tr rKey = mapFn rKey

Lookup on a ValidBufTrie agrees with mapFn on < 2 ^ (cTab * bChar).

theorem Cslib.DataStructure.Dictionary.SuccinctWhp.ValidBufTrie.bounds {cTab bChar : ℕ} {tr : BufTrie} {mapFn : ℕ → Option (ℕ × ℕ)} (hv : ValidBufTrie cTab bChar tr mapFn) :
1 ≤ tr.nodeAlloc ∧ (∀ (u : ℕ), tr.nodeAlloc ≤ u → tr.nodeLeafItem u = 0) ∧ (∀ (v ch : ℕ), tr.trieChild v ch ≠ 0 → tr.trieChild v ch < tr.nodeAlloc) ∧ ∀ u < tr.nodeAlloc, tr.nodeLeafItem u ≠ 0 → tr.nodeLeafItem u ≤ tr.itemAlloc

Allocation and index-range projections of ValidBufTrie for Word-RAM packed-array safety.

theorem Cslib.DataStructure.Dictionary.SuccinctWhp.ValidBufTrie.delete_spec {cTab bChar rKey : ℕ} {tr : BufTrie} {mapFn : ℕ → Option (ℕ × ℕ)} (hv : ValidBufTrie cTab bChar tr mapFn) (hk : rKey < 2 ^ (cTab * bChar)) :
have tr' := bufDelete cTab bChar tr rKey; ValidBufTrie cTab bChar tr' (Function.update mapFn rKey none) ∧ tr'.nodeAlloc = tr.nodeAlloc ∧ tr'.itemAlloc = tr.itemAlloc ∧ bufEverPresent cTab bChar tr' = bufEverPresent cTab bChar tr ∧ ∀ (id : ℕ), bufCheckSlot tr id = none → bufCheckSlot tr' id = none

Exact specification of bufDelete: preserves ValidBufTrie for Function.update mapFn rKey none, leaves nodeAlloc, itemAlloc, and bufEverPresent unchanged, and never activates an inactive item slot.

theorem Cslib.DataStructure.Dictionary.SuccinctWhp.ValidBufTrie.insert_spec {cTab bChar rKey b loc : ℕ} {tr : BufTrie} {mapFn : ℕ → Option (ℕ × ℕ)} (hv : ValidBufTrie cTab bChar tr mapFn) (hk : rKey < 2 ^ (cTab * bChar)) :
have tr' := bufInsert cTab bChar tr rKey b loc; ValidBufTrie cTab bChar tr' (Function.update mapFn rKey (some (b, loc))) ∧ tr.nodeAlloc ≤ tr'.nodeAlloc ∧ tr'.nodeAlloc ≤ tr.nodeAlloc + cTab ∧ (tr'.itemAlloc = if bufEverPresent cTab bChar tr rKey = true then tr.itemAlloc else tr.itemAlloc + 1) ∧ (∀ k < 2 ^ (cTab * bChar), bufEverPresent cTab bChar tr' k = if k = rKey then true else bufEverPresent cTab bChar tr k) ∧ (bufEverPresent cTab bChar tr rKey = true → tr'.nodeAlloc = tr.nodeAlloc ∧ tr'.itemAlloc = tr.itemAlloc) ∧ (mapFn rKey ≠ none → tr'.nodeAlloc = tr.nodeAlloc ∧ tr'.itemAlloc = tr.itemAlloc ∧ ∀ (id : ℕ), bufCheckSlot tr id = none → bufCheckSlot tr' id = none)

Exact specification of bufInsert:

theorem Cslib.DataStructure.Dictionary.SuccinctWhp.ValidBufTrie.checkSlot_some {cTab bChar id b loc : ℕ} {tr : BufTrie} {mapFn : ℕ → Option (ℕ × ℕ)} (hv : ValidBufTrie cTab bChar tr mapFn) (hchk : bufCheckSlot tr id = some (b, loc)) :
id < tr.itemAlloc ∧ ∃ k < 2 ^ (cTab * bChar), bufWalk bChar tr.trieChild k cTab 0 0 = some (tr.itemLeaf id) ∧ mapFn k = some (b, loc)

Forward scan specification: if bufCheckSlot tr id = some (b, loc), then id < tr.itemAlloc and some active key k < 2 ^ (cTab * bChar) maps to (b, loc) in mapFn (with leaf tr.itemLeaf id).

theorem Cslib.DataStructure.Dictionary.SuccinctWhp.ValidBufTrie.exists_checkSlot_of_map_some {cTab bChar k b loc : ℕ} {tr : BufTrie} {mapFn : ℕ → Option (ℕ × ℕ)} (hv : ValidBufTrie cTab bChar tr mapFn) (hmk : mapFn k = some (b, loc)) :
∃ id < tr.itemAlloc, bufCheckSlot tr id = some (b, loc)

Converse scan specification: every active key mapFn k = some (b, loc) has an active scan slot id < tr.itemAlloc with bufCheckSlot tr id = some (b, loc). In particular, once ∀ id < tr.itemAlloc, bufCheckSlot tr id = none, mapFn is identically none.

Section 3: Incremental single-pop cleanup (ClearableBufTrie, clearSteps_spec) #

Structural parent-log invariant maintained across intermediate bufClearOne steps while re-zeroing BufOld in the second half of an epoch.

theorem Cslib.DataStructure.Dictionary.SuccinctWhp.ValidBufTrie.toClearable {cTab bChar : ℕ} {tr : BufTrie} {mapFn : ℕ → Option (ℕ × ℕ)} (hv : ValidBufTrie cTab bChar tr mapFn) :

Every ValidBufTrie satisfies ClearableBufTrie.

Allocation and parent-index projections of ClearableBufTrie for Word-RAM packed-array safety.

theorem Cslib.DataStructure.Dictionary.SuccinctWhp.ClearableBufTrie.clearSteps_spec (cTab bChar k : ℕ) {tr : BufTrie} :
ClearableBufTrie tr → tr.nodeAlloc ≤ k → have tr' := bufClearSteps k tr; (ValidBufTrie cTab bChar tr' fun (x : ℕ) => none) ∧ tr'.nodeAlloc = 1 ∧ tr'.itemAlloc = 0

Executing k ≥ tr.nodeAlloc elementary bufClearOne steps on any ClearableBufTrie produces a clean empty ValidBufTrie (mapFn = fun _ => none, nodeAlloc = 1, itemAlloc = 0).

theorem Cslib.DataStructure.Dictionary.SuccinctWhp.ValidBufTrie.insert_nodeAlloc_le {cTab bChar rKey b loc : ℕ} {tr : BufTrie} {mapFn : ℕ → Option (ℕ × ℕ)} (hv : ValidBufTrie cTab bChar tr mapFn) (hk : rKey < 2 ^ (cTab * bChar)) (hlin : tr.nodeAlloc ≤ 1 + cTab * tr.itemAlloc) :
have tr' := bufInsert cTab bChar tr rKey b loc; tr'.nodeAlloc ≤ 1 + cTab * tr'.itemAlloc

bufInsert preserves the linear bound nodeAlloc ≤ 1 + cTab * itemAlloc across all insertions, overwrites, and reactivations.