Binary heaps the kernel can run #
Batteries' binary heaps (Batteries.BinaryHeap) come with proofs: inserting and removing the
maximum keep the heap well formed (Batteries.BinaryHeap.insert_wf,
Batteries.BinaryHeap.popMax_wf), the maximum is at least every element
(Batteries.BinaryHeap.max_ge_all), and the operations permute the elements. But their sifting
functions recurse by well-founded recursion, which the kernel does not evaluate, and the CDCL
solver is meant to run in the kernel. So this file defines sifting by structural recursion on a
fuel (heapifyUpF, heapifyDownF), proves that with enough fuel it computes exactly what
Batteries' functions compute, and defines insertion and removal of the maximum with it
(Batteries.BinaryHeap.insertF, Batteries.BinaryHeap.popMaxF), equal to Batteries' operations,
so that all of Batteries' theorems apply. The empty heap, the maximum and the larger child are
redefined too (Batteries.BinaryHeap.emptyF, Batteries.BinaryHeap.maxF,
Batteries.BinaryHeap.maxChildF), as Batteries does not expose their bodies to other modules,
which the kernel would need.
Main definitions #
Batteries.BinaryHeap.insertF,Batteries.BinaryHeap.popMaxF: insertion and removal of the maximum by structural recursion.Batteries.BinaryHeap.emptyF,Batteries.BinaryHeap.maxF: the empty heap and the maximum.
Main results #
Batteries.BinaryHeap.insertF_eq,Batteries.BinaryHeap.popMaxF_eq: they are Batteries'insertandpopMax.Batteries.BinaryHeap.mem_popMaxF,Batteries.BinaryHeap.mem_of_mem_popMaxF: removing the maximum removes it and nothing else.Batteries.BinaryHeap.heapifyUpF_perm,Batteries.BinaryHeap.heapifyDownF_perm: sifting permutes the elements.Batteries.BinaryHeap.topDown_heapifyUp_of_isGE: after the order changed at one element, as when the key of a variable alone grew, sifting it up restores the heap.
The empty heap, empty with an exposed body.
The maximum, max with an exposed body.
With fuel above the position, heapifyUpF is heapifyUp.
With fuel above the distance to the end, heapifyDownF is heapifyDown.
Insertion by structural recursion. The size is read before the push, so that the array is updated in place when the heap is not shared.
Removing the maximum by structural recursion.
Removing the maximum adds no element.
Sifting up permutes the elements.
Sifting down permutes the elements.
Heaps whose order changes #
A heap stays a heap in another order: if every comparison a[k] ≥ a[j] in the order o₁
holds in the order o₂, a heap for o₁ is one for o₂.
Sifting up after the order changed at one element: if a is a heap for the order o₁,
and every comparison a[k] ≥ a[j] in o₁ with j ≠ i holds in the order o₂ (as when the key
of a[i] alone grew), sifting a[i] up in o₂ gives a heap for o₂.