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 #
Cslib.DataStructure.PriorityQueue.WordRAM.SoftHeap.SHItem,SHTree,SHTree.Valid: algebraic soft-heap items ((id, val, origKey)) and valid multi-item binary trees.Cslib.DataStructure.PriorityQueue.WordRAM.SoftHeap.totalPhi,popDeficit,rootCredit: exact rational potential function over the root list parameterized by rank thresholdrt.Cslib.DataStructure.PriorityQueue.WordRAM.SoftHeap.SHInv: full pool, structural, and amortized rank-birth invariant.
Main results #
Cslib.DataStructure.PriorityQueue.WordRAM.SoftHeap.shInit_inv: emptySHInvinitialization.Cslib.DataStructure.PriorityQueue.WordRAM.SoftHeap.allocPool_spec: pool allocator correctness at exact memory cost2.Cslib.DataStructure.PriorityQueue.WordRAM.SoftHeap.SHInv.origKey_le_ckey: surviving entries never have keys lowered beloworigKey.Cslib.DataStructure.PriorityQueue.WordRAM.SoftHeap.shInv_corrupted_le,shInv_corrupted_le_eps: global corruption bounds for arbitrary thresholdh.rankThresholdand rationalε > 0.
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.
Rank of the root of t.
Corrupted upper-bound key stored at the root of t.
Item list stored directly at the root of t.
Left child subtree of the root of t.
Right child subtree of the root of t.
All 1-based node pool indices appearing in t.
Reduction equation for SHTree.allEntries in terms of Option.elim.
All items appearing anywhere in t.
All 1-based item pool indices appearing in t.
Total number of nodes in t.
Reduction equation for SHTree.nodeCount in terms of Option.elim.
Number of nodes of rank k in t.
Reduction equation for SHTree.countRank in terms of Option.elim.
Sum of itemPhi rt across all items in t.
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.
- node {rt lb id r ck : ℕ} {items : List SHItem} {l r' : Option SHTree} : items ≠ [] →
items.length ≤ sizeCap rt r →
lb ≤ ck →
(∀ it ∈ items, it.origKey ≤ ck) →
(r ≤ rt → ∀ it ∈ items, it.origKey = ck) →
(l.isSome = true ∨ r'.isSome = true → targetSize rt r ≤ 2 * items.length) →
(∀ (c : SHTree), l = some c → c.rank + 1 = r) →
(∀ (c : SHTree), l = some c → Valid rt ck c) →
(∀ (c : SHTree), r' = some c → c.rank + 1 = r) →
(∀ (c : SHTree), r' = some c → Valid rt ck c) → Valid rt lb (SHTree.node id r ck items l r')
Valid soft-heap tree node relative to 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.
Item list representation depends only on cells itemBase + 3 * it.id + {0, 1, 2} for
it ∈ items.
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).
- node {rt itemBase nodeBase : ℕ} {mem : ℕ → ℕ} {id r ck : ℕ} {items : List SHItem} {l r' : Option SHTree} : id ≠ 0 →
mem (nodeBase + 12 * id) = ck →
mem (nodeBase + 12 * id + 1) = r →
mem (nodeBase + 12 * id + 2) = targetSize rt r →
mem (nodeBase + 12 * id + 3) = items.length →
ItemListRep itemBase mem (mem (nodeBase + 12 * id + 4)) (mem (nodeBase + 12 * id + 5)) items →
mem (nodeBase + 12 * id + 6) = l.elim 0 SHTree.rootId →
mem (nodeBase + 12 * id + 7) = r'.elim 0 SHTree.rootId →
(∀ (c : SHTree), l = some c → TreeMemRep rt itemBase nodeBase mem c) →
(∀ (c : SHTree), r' = some c → TreeMemRep rt itemBase nodeBase mem c) →
TreeMemRep rt itemBase nodeBase mem (SHTree.node id r ck items l r')
Valid in-memory node representation (
+0..+7and linked item list).
TreeMemRep depends only on node offsets +0..+7 for id ∈ t.nodeIds and item offsets
+0..+2 for it ∈ t.allItems.
Framing lemma for RootListRep: if mem₁ and mem₂ agree on {9, 10, 11} of every root
in roots, then RootListRep is preserved.
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.
- fits16 : WordRAM.Fits w 16
Word size at least 17 bits (
Fits w 16). - wf : Memory.WellFormed w s
Well-formed memory state.
Initial item floor.
Item capacity bound.
Initial node floor.
Node capacity bound.
Guaranteed node pool headroom for
2 * (maxItems - ic₀)allocated nodes.Binomial forest bound
(nodeCount - nc₀) + roots.length ≤ 2 * (itemCount - ic₀).Item pool lies below
s.frontier.Node pool lies below
s.frontier.- pools_disj : h.itemBase + 3 * (maxItems + 1) ≤ h.nodeBase ∨ h.nodeBase + 12 * (maxNodes + 1) ≤ h.itemBase
Item and node pools are disjoint.
defillfuel fits in a machine word.- nodeIds_nodup : (forestNodeIds roots).Nodup
All live node IDs are distinct.
All live node IDs lie in
(nc₀, h.nodeCount].- itemIds_nodup : (forestItemIds roots).Nodup
All live item IDs are distinct.
All live item IDs lie in
(ic₀, h.itemCount].- dead_nodes_empty (idx : ℕ) : nc₀ < idx → idx ≤ h.nodeCount → idx ∉ forestNodeIds roots → s.mem (h.nodeBase + 12 * idx + 4) = 0
Deallocated nodes in
(nc₀, h.nodeCount]havehead = 0soshCollectCorruptedskips them. All root trees satisfy
SHTree.Valid h.rankThreshold 0.All root trees match memory offsets
+0..+7.Root list pointers and suffix minima match
roots.Handle
firstRootequals head ofroots(or0when empty).created 0equals total items inserted sinceic₀.- created_step (k : ℕ) : 2 * created (k + 1) + List.countP (fun (t : SHTree) => decide (t.rank = k)) roots ≤ created k
Amortized birth inequality across ranks.
Total live nodes at rank
kare bounded bycreated k.Root ranks are bounded by
h.nodeCount - nc₀.
Potential of t :: ts splits into root t and tail ts.
Empty initial Soft Heap invariant at handle
⟨itemBase, nodeBase, 0, ic₀, nc₀, rankThreshold⟩.
Pool allocator specification returning disjoint (itemBase, nodeBase) blocks below the
updated frontier at exact cost 2.
Every surviving entry in a valid soft heap has origKey ≤ ckey (keys are only ever
increased by corruption, never lowered).
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.
Rational error-rate specialization under SHInv: when h.rankThreshold = rankThresholdForEps ε
with 0 < ε, at most ε * (h.itemCount - ic₀) surviving items are corrupted.
SHInv is preserved under memory updates outside InSHPool that keep WellFormed and
frontier.