Documentation

CslibFrontier.DataStructure.PriorityQueue.WordRAM.SoftHeap.Model

Algebraic Model, Representation Invariants, and Pool Specs for Soft Heaps #

Algebraic tree representation SHTree, heap/rank/corruption invariant SHTree.Valid, in-memory linked item and root-list predicates (ItemListRep, TreeMemRep, RootListRep), the full soft-heap invariant SHInv, pool initialization/allocation specifications (shInit_inv, allocPool_spec), and the global corruption bounds (shInv_corrupted_le, shInv_corrupted_le_eps).

Main definitions #

Main results #

Item stored inside a soft-heap node: slot index id, payload val, and key origKey.

  • id : ℕ

    1-based slot index in the item pool.

  • val : ℕ

    Payload value stored with the item.

  • origKey : ℕ

    Original key of the item.

Algebraic soft-heap binary tree with explicit corrupted key ckey, item list, and optional left/right children.

1-based node pool slot index of the root of t.

Corrupted upper-bound key stored at the root of t.

All 1-based node pool indices appearing in t.

@[simp]

Reduction equation for SHTree.nodeIds in terms of Option.elim.

@[simp]
theorem Cslib.DataStructure.PriorityQueue.WordRAM.SoftHeap.SHTree.allEntries_node (id r ck : ℕ) (items : List SHItem) (l r' : Option SHTree) :
(node id r ck items l r').allEntries = List.map (fun (it : SHItem) => (it, ck)) items ++ l.elim [] allEntries ++ r'.elim [] allEntries

Reduction equation for SHTree.allEntries in terms of Option.elim.

All 1-based item pool indices appearing in t.

@[simp]

Reduction equation for SHTree.nodeCount in terms of Option.elim.

@[simp]
theorem Cslib.DataStructure.PriorityQueue.WordRAM.SoftHeap.SHTree.countRank_node (k id r ck : ℕ) (items : List SHItem) (l r' : Option SHTree) :
countRank k (node id r ck items l r') = (if r = k then 1 else 0) + l.elim 0 (countRank k) + r'.elim 0 (countRank k)

Reduction equation for SHTree.countRank in terms of Option.elim.

Sum of itemPhi rt across all items in t.

@[simp]
theorem Cslib.DataStructure.PriorityQueue.WordRAM.SoftHeap.SHTree.itemPhiSum_node (rt id r ck : ℕ) (items : List SHItem) (l r' : Option SHTree) :
itemPhiSum rt (node id r ck items l r') = ↑items.length * itemPhi rt r + l.elim 0 (itemPhiSum rt) + r'.elim 0 (itemPhiSum rt)

Reduction equation for SHTree.itemPhiSum in terms of Option.elim.

Amortized tree potential combining item potential (120) and node credit (30).

Structural, heap-order, rank, and corruption invariants of a soft-heap tree relative to rank threshold rt and parent lower bound lb.

Credit carried by each root of rank r in the doubly-linked root list.

Deficit potential stored on a non-leaf root whose item list dropped below targetSize.

Total soft-heap potential over the list of root trees under rank threshold rt.

All (item, ckey) pairs across all roots in roots.

All 1-based node pool indices across all roots in roots.

All 1-based item pool indices across all roots in roots.

def Cslib.DataStructure.PriorityQueue.WordRAM.SoftHeap.InSHPool (ic₀ nc₀ maxItems maxNodes : ℕ) (h : SHHandle) (a : ℕ) :

Cells belonging to the soft-heap pool slice anchored at (ic₀, nc₀) with capacities (maxItems, maxNodes).

Singly-linked item-pool list representation from head to tail.

theorem Cslib.DataStructure.PriorityQueue.WordRAM.SoftHeap.ItemListRep.congr {itemBase : ℕ} {mem₁ mem₂ : ℕ → ℕ} {head tail : ℕ} {items : List SHItem} (hagree : ∀ it ∈ items, ∀ j < 3, mem₁ (itemBase + 3 * it.id + j) = mem₂ (itemBase + 3 * it.id + j)) (hrep : ItemListRep itemBase mem₁ head tail items) :
ItemListRep itemBase mem₂ head tail items

Item list representation depends only on cells itemBase + 3 * it.id + {0, 1, 2} for it ∈ items.

inductive Cslib.DataStructure.PriorityQueue.WordRAM.SoftHeap.TreeMemRep (rt itemBase nodeBase : ℕ) (mem : ℕ → ℕ) :

Soft-heap node and subtree fields in memory (offsets +0..+7 plus item pool; offsets +8..+11 are managed separately for parent links during defill and root links in RootListRep).

theorem Cslib.DataStructure.PriorityQueue.WordRAM.SoftHeap.TreeMemRep.congr {rt itemBase nodeBase : ℕ} {mem₁ mem₂ : ℕ → ℕ} {t : SHTree} (hnode : ∀ id ∈ t.nodeIds, ∀ j < 8, mem₁ (nodeBase + 12 * id + j) = mem₂ (nodeBase + 12 * id + j)) (hitem : ∀ it ∈ t.allItems, ∀ j < 3, mem₁ (itemBase + 3 * it.id + j) = mem₂ (itemBase + 3 * it.id + j)) (hrep : TreeMemRep rt itemBase nodeBase mem₁ t) :
TreeMemRep rt itemBase nodeBase mem₂ t

TreeMemRep depends only on node offsets +0..+7 for id ∈ t.nodeIds and item offsets +0..+2 for it ∈ t.allItems.

Suffix-minimum root selector breaking ties to the front (t.ckey ≤ sm.ckey), matching updateSuffixMinStep and shInsertLinkRoot.

Doubly-linked root-list links (+9 prevRoot, +10 nextRoot, +11 suffixMin) with strictly increasing ranks.

theorem Cslib.DataStructure.PriorityQueue.WordRAM.SoftHeap.RootListRep.congr {nodeBase : ℕ} {mem₁ mem₂ : ℕ → ℕ} {prev : ℕ} {roots : List SHTree} (hagree : ∀ t ∈ roots, ∀ (j : ℕ), j = 9 ∨ j = 10 ∨ j = 11 → mem₁ (nodeBase + 12 * t.rootId + j) = mem₂ (nodeBase + 12 * t.rootId + j)) (hrep : RootListRep nodeBase mem₁ prev roots) :
RootListRep nodeBase mem₂ prev roots

Framing lemma for RootListRep: if mem₁ and mem₂ agree on {9, 10, 11} of every root in roots, then RootListRep is preserved.

structure Cslib.DataStructure.PriorityQueue.WordRAM.SoftHeap.SHInv (w ic₀ nc₀ maxItems maxNodes : ℕ) (h : SHHandle) (roots : List SHTree) (created : ℕ → ℕ) (s : Memory.State) :

Complete invariant for a dynamic soft heap operating in a pool slice (ic₀, nc₀) up to capacities (maxItems, maxNodes) with amortized root list roots and rank birth counts created.

@[simp]

Potential of an empty soft-heap root list is 0.

@[simp]

Potential of t :: ts splits into root t and tail ts.

Potential distributes over list append.

theorem Cslib.DataStructure.PriorityQueue.WordRAM.SoftHeap.shInit_inv {w itemBase nodeBase ic₀ nc₀ maxItems maxNodes rankThreshold : ℕ} {s : Memory.State} (h16 : WordRAM.Fits w 16) (hwf : Memory.WellFormed w s) (hic : ic₀ ≤ maxItems) (hcap : nc₀ + 2 * (maxItems - ic₀) ≤ maxNodes) (hi_lt : itemBase + 3 * (maxItems + 1) ≤ s.frontier) (hn_lt : nodeBase + 12 * (maxNodes + 1) ≤ s.frontier) (hdisj : itemBase + 3 * (maxItems + 1) ≤ nodeBase ∨ nodeBase + 12 * (maxNodes + 1) ≤ itemBase) (hfuel : WordRAM.Fits w (shFuelBound rankThreshold maxItems)) :
SHInv w ic₀ nc₀ maxItems maxNodes { itemBase := itemBase, nodeBase := nodeBase, firstRoot := 0, itemCount := ic₀, nodeCount := nc₀, rankThreshold := rankThreshold } [] (fun (x : ℕ) => 0) s

Empty initial Soft Heap invariant at handle ⟨itemBase, nodeBase, 0, ic₀, nc₀, rankThreshold⟩.

theorem Cslib.DataStructure.PriorityQueue.WordRAM.SoftHeap.allocPool_spec {w maxItems maxNodes : ℕ} {s : Memory.State} (hwf : Memory.WellFormed w s) (hfit : WordRAM.Fits w (s.frontier + 3 * (maxItems + 1) + 12 * (maxNodes + 1) + 16)) :
(Memory.spec w).ValidHistory s ((Memory.spec w).clientHistory s (allocPool maxItems maxNodes)) ∧ WordRAM.Realization.memoryCost (allocPool maxItems maxNodes) s = 2 ∧ have res := (Memory.spec 0).clientResult s (allocPool maxItems maxNodes); have s' := (Memory.spec 0).clientState s (allocPool maxItems maxNodes); res.1 = s.frontier ∧ res.2 = s.frontier + 3 * (maxItems + 1) ∧ s'.frontier = s.frontier + 3 * (maxItems + 1) + 12 * (maxNodes + 1) ∧ (∀ a < s.frontier, s'.mem a = s.mem a) ∧ Memory.WellFormed w s'

Pool allocator specification returning disjoint (itemBase, nodeBase) blocks below the updated frontier at exact cost 2.

theorem Cslib.DataStructure.PriorityQueue.WordRAM.SoftHeap.SHInv.origKey_le_ckey {w ic₀ nc₀ maxItems maxNodes : ℕ} {h : SHHandle} {roots : List SHTree} {created : ℕ → ℕ} {s : Memory.State} (hinv : SHInv w ic₀ nc₀ maxItems maxNodes h roots created s) (p : SHItem × ℕ) :
p ∈ forestEntries roots → p.1.origKey ≤ p.2

Every surviving entry in a valid soft heap has origKey ≤ ckey (keys are only ever increased by corruption, never lowered).

theorem Cslib.DataStructure.PriorityQueue.WordRAM.SoftHeap.shInv_corrupted_le {w ic₀ nc₀ maxItems maxNodes : ℕ} {h : SHHandle} {roots : List SHTree} {created : ℕ → ℕ} {s : Memory.State} (hinv : SHInv w ic₀ nc₀ maxItems maxNodes h roots created s) :
2 ^ h.rankThreshold * (List.filter (fun (p : SHItem × ℕ) => decide (p.1.origKey < p.2)) (forestEntries roots)).length ≤ 12 * (h.itemCount - ic₀)

Global corruption bound under SHInv (from this module's 12 / 2 ^ r sizeCap recurrence): 2 ^ h.rankThreshold * #corrupted ≤ 12 * (h.itemCount - ic₀) for any threshold.

theorem Cslib.DataStructure.PriorityQueue.WordRAM.SoftHeap.shInv_corrupted_le_eps {ε : ℚ} (hε : 0 < ε) {w ic₀ nc₀ maxItems maxNodes : ℕ} {h : SHHandle} {roots : List SHTree} {created : ℕ → ℕ} {s : Memory.State} (hinv : SHInv w ic₀ nc₀ maxItems maxNodes h roots created s) (hrt : h.rankThreshold = rankThresholdForEps ε) :
↑(List.filter (fun (p : SHItem × ℕ) => decide (p.1.origKey < p.2)) (forestEntries roots)).length ≤ ε * ↑(h.itemCount - ic₀)

Rational error-rate specialization under SHInv: when h.rankThreshold = rankThresholdForEps ε with 0 < ε, at most ε * (h.itemCount - ic₀) surviving items are corrupted.

theorem Cslib.DataStructure.PriorityQueue.WordRAM.SoftHeap.SHInv.congr_outside {w ic₀ nc₀ maxItems maxNodes : ℕ} {h : SHHandle} {roots : List SHTree} {created : ℕ → ℕ} {s s' : Memory.State} (hinv : SHInv w ic₀ nc₀ maxItems maxNodes h roots created s) (hwf' : Memory.WellFormed w s') (hfr' : s'.frontier = s.frontier) (hagree : ∀ (a : ℕ), InSHPool ic₀ nc₀ maxItems maxNodes h a → s'.mem a = s.mem a) :
SHInv w ic₀ nc₀ maxItems maxNodes h roots created s'

SHInv is preserved under memory updates outside InSHPool that keep WellFormed and frontier.