Documentation

CslibFrontier.DataStructure.PriorityQueue.FibonacciHeap.MinPQ

A Fibonacci heap with handles and values over memory #

The min-priority queue minPQSpec n U V stores key–value pairs and hands out its own handles. It is implemented in two refinement steps over the indexed Fibonacci heap of Memory.lean:

  1. toAlloc: the deterministic allocator PriorityQueue.MinPQ.stepAlloc (a stack of freed handles, then a bump pointer) refines the relational specification, by the existing stepAlloc_refines_spec.
  2. impl: the allocator over the indexed heap beside a record of Slots, the handles' values, the stack of freed handles, the bump pointer and the stack's height. Each operation calls the heap at most once and reads or writes at most four slots.

Both the heap and the slot record are allocated in memory (FibonacciHeap.allocator, Record.allocator), side by side (Memory.Allocator.prod), and the two steps go on top (Memory.Allocator.comp): allocator. The potentials stack (amortized_allocator), so every operation costs O(1) memory units, amortized, but extractMin, which costs O(log m) on a queue of m entries.

The module adapts Renfei Zhou's native formalization of the same structure (WordRAM/FibonacciHeap, its MinPQ.lean) to the memory level: his value array, handle stack and bump pointer become the slot record, and his allocator model AllocState/stepAlloc is reused unchanged.

Main definitions #

Main results #

The allocator as a specification #

@[reducible, inline]

The deterministic handle allocator PriorityQueue.MinPQ.stepAlloc as a specification, with the admissibility of minPQSpec.

The allocator refines the relational queue: each of its deterministic choices of a handle and of a minimum entry is one the specification allows.

Slots beside the heap #

The words kept beside the heap.

  • val (h : ℕ) : Slot

    The value stored with a handle.

  • free (i : ℕ) : Slot

    An entry of the stack of freed handles, from the bottom.

  • fresh : Slot

    The bump pointer: handles from here on were never used.

  • top : Slot

    The height of the stack of freed handles.

The slots for n handles: n values, n stack entries, and two scalars.

Instances For
    @[reducible, inline]

    The lower interface: the indexed heap with keys at most K, beside the slots.

    The allocator state that the heap and the slots represent: the heap's keys with the slots' values, the stack read from its top down, and the bump pointer.

    The representation invariant: the allocator state is well formed, the stack holds handles, and keys are at most K.

    @[simp]
    theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.MinPQ.runClient_heapOp_bind {n K w : ℕ} {α : Type} (s : IndexedMinPQ.State n) (g : Slot → ℕ) (o : IndexedMinPQ.Op n) (k : o.Result → (Lower n K w).Client α) :
    (Lower n K w).runClient (s, g) (heapOp o >>= k) = match (Lower n K w).runClient ((IndexedMinPQ.step s o).1, g) (k (IndexedMinPQ.step s o).2) with | (v, e, os) => (v, e, Sum.inl o :: os)

    A heap call steps the heap.

    @[simp]
    theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.MinPQ.runClient_rd_bind {n K w : ℕ} {α : Type} (s : IndexedMinPQ.State n) (g : Slot → ℕ) (c : Slot) (k : ℕ → (Lower n K w).Client α) :
    (Lower n K w).runClient (s, g) (rd c >>= k) = match (Lower n K w).runClient (s, g) (k (g c)) with | (v, e, os) => (v, e, Sum.inr (Record.Op.read c) :: os)

    A read answers the slot.

    @[simp]
    theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.MinPQ.runClient_wr_bind {n K w : ℕ} {α : Type} (s : IndexedMinPQ.State n) (g : Slot → ℕ) (c : Slot) (x : ℕ) (k : Unit → (Lower n K w).Client α) :
    (Lower n K w).runClient (s, g) (wr c x >>= k) = match (Lower n K w).runClient (s, Function.update g c x) (k ()) with | (v, e, os) => (v, e, Sum.inr (Record.Op.write c x) :: os)

    A write updates the slot.

    Admissibility of a heap call.

    Admissibility of a read.

    Admissibility of a write.

    The queue's operations over the heap and the slots.

    The allocator over the heap and the slots #

    The allocator's entries forget the values into the heap's keys.

    The allocator and the heap hold the same handles.

    get h asks the heap for the key, and reads the value if h is present.

    decreaseKey h u' lowers the key in the heap.

    The stack lists the handles of its top cells.

    The stack's top cell holds the head of the free list.

    The representation: Inv of the heap's state and the slots.

    theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.MinPQ.methods_spec {n U V w : ℕ} {s : IndexedMinPQ.State n} {g : Slot → ℕ} (hn : WordRAM.Fits w n) (hV : WordRAM.Fits w V) (hI : Inv n U s g) (o : MinPQ.Op n) (hv : (minPQSpec n U V).valid (allocState (s, g)).entries o) :
    (Lower n U w).ValidHistory (s, g) ((Lower n U w).clientHistory (s, g) (methods n U V U w o)) ∧ MinPQ.stepAlloc (allocState (s, g)) o = (allocState ((Lower n U w).clientState (s, g) (methods n U V U w o)), (Lower n U w).clientResult (s, g) (methods n U V U w o)) ∧ Rep n U ((Lower n U w).clientState (s, g) (methods n U V U w o))

    Each operation does what the allocator does, admissibly, and keeps the invariant.

    The allocator over the heap and the slots.

    Costs #

    The amortized charge of each operation on a queue s, in memory units: one heap call and up to four slot operations, with the heap's charges.

    The charges on the allocator's states.

    Each allocator call is one call.

    theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.MinPQ.amortized_impl {n U V w : ℕ} (hn : WordRAM.Fits w n) (hV : WordRAM.Fits w V) :
    (impl hn hV).Amortized (productCharge (FibonacciHeap.charge n) fun (x : (Record.spec (Slot.ok n) w).State) (x_1 : (Record.spec (Slot.ok n) w).Op) => 1) (allocCharge n) fun (x : (Lower n U w).State) => 0

    Each operation costs its charge in heap charges and slot operations.

    In memory #

    The slots at 2 n + 2 consecutive cells from base: values, stack, bump pointer, height.

    Allocating the slots: 2 n + 2 zeroed cells, the height last.

    Every slot operation costs one memory unit.

    The zeroed heap and slots represent the empty allocator.

    The allocator over memory: the heap and the slots allocated side by side, with the allocator on top. It answers deterministically.

    The allocated allocator is well formed.

    The min-priority queue over memory, as a Memory.Allocator for minPQSpec, so that it composes with other structures in one memory.

    Stacked potentials, deterministic form: every operation costs its allocCharge in memory units, amortized, with no potential once allocated.

    Stacked potentials: every operation costs its charge in memory units, amortized, with no potential once allocated.

    Nothing is needed of the memory but room.

    theorem Cslib.DataStructure.PriorityQueue.FibonacciHeap.MinPQ.run_correct {n U V w : ℕ} (hn : WordRAM.Fits w n) (hU : WordRAM.Fits w U) (hV : WordRAM.Fits w V) {β : Type} (p : (allocSpec n U V).Client β) {s : Memory.State} (hroom : WordRAM.Fits w (s.frontier + (allocAllocator hn hU hV).room)) (hv : (allocSpec n U V).ValidHistory (allocAllocator hn hU hV).init ((allocSpec n U V).clientHistory (allocAllocator hn hU hV).init p)) :

    Correctness of a run. From a memory with room for the heap and the slots, an admissible client of the allocator issues admissible memory operations and answers as the allocator does from the empty queue.

    Cost of a run: the allocation's cost plus the charges of the client's operations.