Documentation

CslibFrontier.DataStructure.PriorityQueue.BinaryHeap.Memory

An indexed binary heap over the memory oracle #

The indexed min-priority queue indexedMinPQSpec n K is implemented as a binary heap by programs over Memory.spec w. The storage is a Layout: a one-cell size field and three arrays from base, exactly as in BinaryHeap.Model: the key of each handle, the heap of handles at positions 1, …, size, and the position of each handle (0 when absent). The abstraction function is the model's stateOfMem, and the invariant is the model's HeapInv at the stored size. Entries are ordered by (key, handle), so extractMin returns exactly the spec's findMin.

Sift-up and sift-down are recursive memory programs (siftUp, siftDown). Their proofs are inductions on the recursion, one level per call: the order lemmas of BinaryHeap.Order move the single order defect one level, and the memory steps are discharged by the rules of Memory.Triple. A level costs at most 8 memory units on the way up and 10 on the way down, so every operation costs at most 10 ⌊log₂ n⌋ + 14 memory units (amortized). No register, address arithmetic beyond the layout, or instruction count appears; word fit enters only through the admissibility of Memory.spec w and two static facts of the layout, that n and the largest key fit a word.

The methods write only the layout's cells and never allocate (methods_frame), and the abstraction and invariant read only those cells (abs_congr, Inv.congr), so the heap is framed by its layout (framed) and composes with other structures in the same memory (Memory.Framed.prod). Allocated in memory that is zero above the frontier, the heap starts empty at a cost of two memory units (allocLayout_bind, inv_allocated).

BinaryHeap.Native realizes these same memory programs as one fixed word-RAM program, with their exact source state and history preserved and native arithmetic and control charged.

Main definitions #

Main results #

References #

The storage of an indexed binary heap on the handles Fin n: a one-cell field holding the number of entries, and three arrays from base, laid out as in BinaryHeap.Model.

  • One cell holding the number of entries.

  • base : ℕ

    The start of the key, heap and position arrays.

@[reducible, inline]

The key of each handle, from base.

@[reducible, inline]

The handle at each heap position 1, …, size, from base + n; position 0 is unused.

@[reducible, inline]

The heap position of each handle, 0 when the handle is absent, from base + 2 n + 1.

@[reducible, inline]

The cells of the three arrays.

Static well-formedness at word size w for keys at most K: the size field is one cell outside the touched array cells, and n and K fit a word. Heap position zero is unused and may hold that field without enlarging the array block.

  • size_length : L.size.length = 1

    The size field is one cell.

  • size_separate : ¬L.arrays.Owns L.size.base ∨ L.size.base = L.base + n

    The size cell is outside the arrays, or at their unused heap position zero.

  • n_fits : WordRAM.Fits w n

    The number of handles fits a word, hence so does every handle and position.

  • key_fits : WordRAM.Fits w K

    The largest key fits a word.

@[reducible, inline]

A specialization of the existing layout using exactly space n cells. The size occupies the unused heap position zero; the key, positive heap positions and position array do not move.

The compact layout owns precisely the original three-array block.

Compact storage adds no extra word or width requirement.

The programs #

Swap the handles va at heap position a and vb at heap position b.

@[irreducible]

Sift-up from heap position k: while the entry at k has higher priority than its parent, swap them and continue from the parent.

One step of sift-down: if the child entry (vc, kc) at position c has higher priority than the entry (vk, kk) at position k, swap them and continue with next.

@[irreducible]

Sift-down from heap position k in a heap of size S: while some child has higher priority than the entry at k, swap the entry with its child of highest priority and continue from that child.

The key of handle v: absent when its position is 0. Correctness is stated for v < n through the typed indexed-PQ operations; the program accepts the runtime index word directly.

Insert the absent handle v with key k: store the key, append v at the end of the heap, and sift it up.

Lower the key of the present handle v to k, and sift it up from its position.

The answer of extractMin for the handle u with key k.

Remove the root and return it: move the last entry to the root, sift it down, and store the new size.

The operations of the indexed min-priority queue as memory programs.

@[reducible, inline]

The operations as methods of indexedMinPQSpec n K over Memory.spec w.

The represented queue: the model's decoding of the key and position arrays.

The implementation invariant: the fields are allocated, and the arrays hold a binary heap of the stored size.

The memory cost of one operation on n handles: 10 ⌊log₂ n⌋ + 14 units.

Layout facts #

A key cell is allocated.

A heap cell is allocated.

A position cell is allocated.

Rearranging the heap #

s' is s with the heap and position arrays rearranged: the frontier, the keys, the presence of each handle and every cell outside those two arrays are unchanged.

Every state is a rearrangement of itself.

theorem Cslib.DataStructure.PriorityQueue.BinaryHeap.Memory.Rearranges.trans {n : ℕ} {L : Layout n} {s₁ s₂ s₃ : Memory.State} (h₁ : Rearranges L s₁ s₂) (h₂ : Rearranges L s₂ s₃) :
Rearranges L s₁ s₃

Rearrangements compose.

theorem Cslib.DataStructure.PriorityQueue.BinaryHeap.Memory.Rearranges.swap {n : ℕ} {L : Layout n} {s : Memory.State} {S a b : ℕ} (h : HeapShape n L.base S s.mem) (ha : 1 ≤ a) (haS : a ≤ S) (hb : 1 ≤ b) (hbS : b ≤ S) (hab : a ≠ b) :
Rearranges L s { mem := swapMem n L.base s.mem a b (s.mem (L.base + n + a)) (s.mem (L.base + n + b)), frontier := s.frontier }

A swap of two heap positions rearranges the heap.

Sift-up and sift-down #

theorem Cslib.DataStructure.PriorityQueue.BinaryHeap.Memory.swap_bind {n : ℕ} {L : Layout n} {w : ℕ} {α : Type} {s : Memory.State} {a b va vb c : ℕ} {k : Unit → Memory.Prog α} {Q : α → Memory.State → Prop} (hn : WordRAM.Fits w n) (hA : L.arrays.Allocated s) (ha : a ≤ n) (hb : b ≤ n) (hva : va < n) (hvb : vb < n) (h : Memory.Triple w { mem := swapMem n L.base s.mem a b va vb, frontier := s.frontier } (k ()) c Q) :
Memory.Triple w s (swap L a b va vb >>= k) (c + 4) Q

Swapping two heap positions writes four cells.

theorem Cslib.DataStructure.PriorityQueue.BinaryHeap.Memory.siftUp_triple {n : ℕ} {L : Layout n} {w : ℕ} (hn : WordRAM.Fits w n) {S : ℕ} {s : Memory.State} {k : ℕ} (hA : L.arrays.Allocated s) (hk : 1 ≤ k) (hkS : k ≤ S) (hshape : HeapShape n L.base S s.mem) (hord : UpOrdered (heapEntry n L.base s.mem) S k) :
Memory.Triple w s (siftUp L k) (8 * Nat.log 2 k + 4) fun (x : Unit) (s' : Memory.State) => HeapInv n L.base S s'.mem ∧ Rearranges L s s'

Sift-up repairs an upward defect at heap position k within 8 ⌊log₂ k⌋ + 4 memory units.

theorem Cslib.DataStructure.PriorityQueue.BinaryHeap.Memory.sink_triple {n : ℕ} {L : Layout n} {w : ℕ} {s : Memory.State} {k c vk kk vc kc cost : ℕ} {next : Memory.Prog Unit} {Q : Unit → Memory.State → Prop} (hn : WordRAM.Fits w n) (hA : L.arrays.Allocated s) (hk : k ≤ n) (hc : c ≤ n) (hvk : vk < n) (hvc : vc < n) (hswap : IndexedMinPQ.RawPrioLT (vc, kc) (vk, kk) → Memory.Triple w { mem := swapMem n L.base s.mem k c vk vc, frontier := s.frontier } next cost Q) (hstop : ¬IndexedMinPQ.RawPrioLT (vc, kc) (vk, kk) → Q () s) :
Memory.Triple w s (sink L k c vk kk vc kc next) (cost + 4) Q

One step of sift-down: a swap with the child when it has higher priority, then next.

theorem Cslib.DataStructure.PriorityQueue.BinaryHeap.Memory.siftDown_triple {n : ℕ} {L : Layout n} {w : ℕ} (hn : WordRAM.Fits w n) {S : ℕ} {s : Memory.State} {k : ℕ} (hA : L.arrays.Allocated s) (hkS : k ≤ S) (hshape : HeapShape n L.base S s.mem) (hord : DownOrdered (heapEntry n L.base s.mem) S k) :
Memory.Triple w s (siftDown L S k) (10 * (Nat.log 2 S - Nat.log 2 k) + 6) fun (x : Unit) (s' : Memory.State) => HeapInv n L.base S s'.mem ∧ Rearranges L s s'

Sift-down repairs a downward defect at heap position k of a heap of size S within 10 (⌊log₂ S⌋ - ⌊log₂ k⌋) + 6 memory units.

The abstraction #

A rearrangement represents the same queue.

theorem Cslib.DataStructure.PriorityQueue.BinaryHeap.Memory.abs_eq_update {n : ℕ} {L : Layout n} {s s' : Memory.State} {v : Fin n} {x : Option ℕ} (hother : ∀ (u : Fin n), u ≠ v → s'.mem (L.base + ↑u) = s.mem (L.base + ↑u) ∧ (s'.mem (L.base + 2 * n + 1 + ↑u) = 0 ↔ s.mem (L.base + 2 * n + 1 + ↑u) = 0)) (hv : abs L s' v = x) :
abs L s' = Function.update (abs L s) v x

A memory that keeps the key and the presence of every handle but v represents the queue updated at v.

The operations #

theorem Cslib.DataStructure.PriorityQueue.BinaryHeap.Memory.not_mem_footprint {n : ℕ} {L : Layout n} {w K : ℕ} (hL : L.Valid w K) {a : ℕ} (ha : a ∉ L.footprint) :
a ≠ L.size.base ∧ (a < L.base ∨ L.base + 3 * n + 1 ≤ a)

The cells outside the layout: neither the size cell nor an array cell.

The size cell is outside the array block or at its unused heap position zero.

theorem Cslib.DataStructure.PriorityQueue.BinaryHeap.Memory.Inv.size_lt {n : ℕ} {L : Layout n} {w K : ℕ} (hL : L.Valid w K) {s : Memory.State} (hs : Inv L s) :

The size cell is allocated.

theorem Cslib.DataStructure.PriorityQueue.BinaryHeap.Memory.heapInv_update_size {n : ℕ} {L : Layout n} {w K : ℕ} (hL : L.Valid w K) {S : ℕ} {m : ℕ → ℕ} (h : HeapInv n L.base S m) (x : ℕ) :

A heap invariant survives a write to the size cell.

What a run of the operation o from s establishes: the specified step from the represented queue, the invariant, and the frame: the frontier and every cell outside the layout are unchanged.

theorem Cslib.DataStructure.PriorityQueue.BinaryHeap.Memory.post_self {n : ℕ} {L : Layout n} {s : Memory.State} (hs : Inv L s) {o : IndexedMinPQ.Op n} {r : o.Result} (h : IndexedMinPQ.step (abs L s) o = (abs L s, r)) :
Post L s o r s

An operation that only reads realizes a step that leaves the queue unchanged.

getKey reads the position and, for a present handle, the key.

theorem Cslib.DataStructure.PriorityQueue.BinaryHeap.Memory.insert_triple {n : ℕ} {L : Layout n} {w K : ℕ} (hL : L.Valid w K) {s : Memory.State} (hs : Inv L s) {v : Fin n} {k : ℕ} (hv : IndexedMinPQ.valid K (abs L s) (IndexedMinPQ.Op.insert v k)) :
Memory.Triple w s (insert L (↑v) k) (8 * Nat.log 2 n + 9) (Post L s (IndexedMinPQ.Op.insert v k))

insert appends the handle and sifts it up, within 8 ⌊log₂ n⌋ + 9 memory units.

theorem Cslib.DataStructure.PriorityQueue.BinaryHeap.Memory.decreaseKey_triple {n : ℕ} {L : Layout n} {w K : ℕ} (hL : L.Valid w K) {s : Memory.State} (hs : Inv L s) {v : Fin n} {k : ℕ} (hv : IndexedMinPQ.valid K (abs L s) (IndexedMinPQ.Op.decreaseKey v k)) :
Memory.Triple w s (decreaseKey L (↑v) k) (8 * Nat.log 2 n + 6) (Post L s (IndexedMinPQ.Op.decreaseKey v k))

decreaseKey lowers the key and sifts the handle up, within 8 ⌊log₂ n⌋ + 6 memory units.

The answer for a handle.

theorem Cslib.DataStructure.PriorityQueue.BinaryHeap.Memory.post_extract {n : ℕ} {L : Layout n} {s s' : Memory.State} {u k : ℕ} (hu : u < n) (hfind : IndexedMinPQ.findMin (abs L s) = some (⟨u, hu⟩, k)) (habs : abs L s' = Function.update (abs L s) ⟨u, hu⟩ none) (hinv : Inv L s') (hf : s'.frontier = s.frontier) (hframe : ∀ a ∉ L.footprint, s'.mem a = s.mem a) :

The postcondition of an extraction that removes the minimum (u, k).

extractMin removes the root, moves the last entry up and sifts it down, within opCost n memory units.

theorem Cslib.DataStructure.PriorityQueue.BinaryHeap.Memory.methods_triple {n : ℕ} {L : Layout n} {w K : ℕ} (hL : L.Valid w K) {s : Memory.State} (hs : Inv L s) (o : IndexedMinPQ.Op n) (hv : IndexedMinPQ.valid K (abs L s) o) :
Memory.Triple w s (ops L o) (opCost n) (Post L s o)

Each operation realizes the specified step within opCost n memory units, keeps the invariant, and changes neither the frontier nor any cell outside the layout.

The run of an operation, by methods_triple.

theorem Cslib.DataStructure.PriorityQueue.BinaryHeap.Memory.methods_frame {n : ℕ} {L : Layout n} {w K : ℕ} (hL : L.Valid w K) {s : Memory.State} (hs : Inv L s) (o : IndexedMinPQ.Op n) (hv : IndexedMinPQ.valid K (abs L s) o) (a : ℕ) :
a ∉ L.footprint → ((Memory.spec w).clientState s (ops L o)).mem a = s.mem a

An operation changes no cell outside the layout.

An operation leaves the frontier where it was.

The refinement and its frame #

The binary heap implements the indexed min-priority queue with keys at most K.

theorem Cslib.DataStructure.PriorityQueue.BinaryHeap.Memory.Inv.lt_frontier {n : ℕ} {L : Layout n} {w K : ℕ} (hL : L.Valid w K) {s : Memory.State} (hs : Inv L s) {a : ℕ} (ha : a ∈ L.footprint) :

The layout's cells are allocated.

theorem Cslib.DataStructure.PriorityQueue.BinaryHeap.Memory.abs_congr {n : ℕ} {L : Layout n} {s s' : Memory.State} (h : ∀ a ∈ L.footprint, s'.mem a = s.mem a) :
abs L s' = abs L s

The abstraction reads only the arrays.

theorem Cslib.DataStructure.PriorityQueue.BinaryHeap.Memory.Inv.congr {n : ℕ} {L : Layout n} {w K : ℕ} (hL : L.Valid w K) {s s' : Memory.State} (hs : Inv L s) (h : ∀ a ∈ L.footprint, s'.mem a = s.mem a) (hf : s.frontier ≤ s'.frontier) :
Inv L s'

The invariant reads only the layout's cells and survives a growing frontier.

The binary heap, with its layout's cells as a constant footprint, so that it shares memory with other structures (Memory.Framed.prod).

@[simp]

The footprint of a heap is its layout's cells.

theorem Cslib.DataStructure.PriorityQueue.BinaryHeap.Memory.amortized {n : ℕ} {L : Layout n} {w K : ℕ} (hL : L.Valid w K) :
(framed hL).Amortized Memory.charge (fun (x : (indexedMinPQSpec n K).State) (x_1 : (indexedMinPQSpec n K).Op) => opCost n) fun (x : (Memory.spec w).State) => 0

Every operation costs at most opCost n = 10 ⌊log₂ n⌋ + 14 memory units, with no potential.

The cost of one operation is O(log n): at most 14 (⌊log₂ n⌋ + 1).

theorem Cslib.DataStructure.PriorityQueue.BinaryHeap.Memory.inv_empty {n : ℕ} {L : Layout n} {s : Memory.State} (hsize : L.size.Allocated s) (hA : L.arrays.Allocated s) (hcount : s.mem L.size.base = 0) (hpos : ∀ v < n, s.mem (L.base + 2 * n + 1 + v) = 0) :

Zero count and position cells suffice for the empty invariant; memory outside the layout can be arbitrary. This is a representation fact, not a zeroing program or an allocation charge.

theorem Cslib.DataStructure.PriorityQueue.BinaryHeap.Memory.inv_compactLayout {n base : ℕ} {s : Memory.State} (hA : { base := base, length := space n }.Allocated s) (hz : ∀ (a : ℕ), { base := base, length := space n }.Owns a → s.mem a = 0) :

A locally zero owned block supplies the compact empty heap, without a global ZeroAbove assumption. Native initialization must still pay its actual reservation and setup instructions.

Allocation #

Allocate a heap on n handles without clearing its storage: in memory that is zero above the frontier, it starts empty.

@[reducible, inline]

The layout that allocLayout n hands out at the frontier f.

theorem Cslib.DataStructure.PriorityQueue.BinaryHeap.Memory.allocLayout_bind {n w : ℕ} {α : Type} {s : Memory.State} {c : ℕ} {next : Layout n → Memory.Prog α} {Q : α → Memory.State → Prop} (hfit : WordRAM.Fits w (s.frontier + 1 + space n)) (h : Memory.Triple w { mem := s.mem, frontier := s.frontier + 1 + space n } (next (allocated n s.frontier)) c Q) :
Memory.Triple w s (allocLayout n >>= next) (c + 2) Q

Allocation takes two memory units and hands out the layout at the frontier.

The allocated layout is well formed when n and the largest key fit a word.

The allocated layout lies at or above the frontier it was allocated from.

Allocated from memory that is zero above the frontier, the heap satisfies the invariant and is empty.