Reference · The Applied Higher-Order Functions catalogue
rainbow.algorithms.proofs.ahof.binary-heap
AHoF Binary Heap: the array-backed complete binary tree behind the priority queue and heapsort, maintaining the HEAP PROPERTY -- in a min-heap every parent key is at most its children's keys, so the minimum is always at the root. Insert (sift-up) and extract-min (sift-down) restore the property in O(log n) by swapping a node with a child that violates it. The heap-order test between a parent and a child is: heap-ordered? parent child = decide (parent <= child) over exact-rational keys. Proved native and axiom-free: the heap-order test; worked instances on concrete keys -- a parent 2 over a child 5 is correctly ordered (no swap needed); the certificate 2 <= 5 as a native nnrat/at-most; and a parent 7 over a child 3 violates the property (sift must swap). The array indexing and the sift-up/down loops are executable; what is proved is the heap-order comparison at these instances, not the heap invariant itself. Requires nnrat/at-most, audit/decide-nnrat-at-most, nnrat/decide-at-most-true-sound (all in the D63 checkpoint). Scope (measured by the kernel): this module has 3 worked instances (statements about specific values). None of its theorems is a guarantee about the algorithm for all inputs; `lein verify rainbow.algorithms.proofs.ahof.binary-heap` prints this classification.
binary-heap/heap-ordered?
binary-heap/heap-ordered? : (x₁ x₂ : ℚ≥0) → data/Bool
ℚ≥0 = nnrat/NonnegativeRational
binary-heap/parent-below-child-ordered
binary-heap/parent-below-child-ordered : → bin-packing/fits? 2 5 = data/true
= = equality/Eq
binary-heap/ordered-parent-at-most-child
binary-heap/ordered-parent-at-most-child : → 2 ≤ 5
≤ = nnrat/at-most
binary-heap/parent-above-child-violates
binary-heap/parent-above-child-violates : → bin-packing/fits? 7 3 = data/false
= = equality/Eq