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:
toAlloc: the deterministic allocatorPriorityQueue.MinPQ.stepAlloc(a stack of freed handles, then a bump pointer) refines the relational specification, by the existingstepAlloc_refines_spec.impl: the allocator over the indexed heap beside a record ofSlots, 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 #
allocSpec: the deterministic allocator as a specification.toAlloc: the allocator refinesminPQSpec.Slot,Lower,allocState,Inv,methods,impl: the allocator over the heap and the slots.charge: the amortized charges1,3,47,104andextractCharge m + 4.allocAllocator,allocator: the deterministic allocator and the relational queue allocated in memory.
Main results #
amortized_impl: each operation costs its charge in calls to the heap and the slots.amortized_allocAllocator,amortized_allocator: each operation costs its charge in memory units, amortized.run_correct,run_cost: a client of the deterministic allocator (allocSpec) run over memory throughallocAllocator. RelationalminPQSpecclients ofallocatorare not covered by a whole-run theorem here (Memory.Allocator.run_correctrequiresHasStepFn).
The allocator as a specification #
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.
The slots for n handles: n values, n stack entries, and two scalars.
Instances For
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.
A call to the heap.
A heap call steps the heap.
A read answers the slot.
Admissibility of a heap call.
Admissibility of a read.
Admissibility of a write.
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.
isEmpty asks the heap.
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.
insert u v with no freed handle takes the bump pointer's.
insert u v pops the top freed handle h.
extractMin takes the heap's minimum, reads its value, and pushes its handle.
The stack's top cell holds the head of the free list.
The representation: Inv of the heap's state and the slots.
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.
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.
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.