Documentation

CslibFrontier.Algorithms.Lean.CDCL.Heap

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 #

Main results #

The empty heap, empty with an exposed body.

def Batteries.BinaryHeap.maxF {α : Type u} [Ord α] (heap : BinaryHeap α) :

The maximum, max with an exposed body.

theorem Batteries.BinaryHeap.maxF_eq {α : Type u} [Ord α] (heap : BinaryHeap α) :
heap.maxF = heap.max
def Batteries.BinaryHeap.maxChildF {α : Type u} [Ord α] {sz : ℕ} (a : Vector α sz) (i : Fin sz) :
Option (Fin sz)

The larger child, maxChild with an exposed body.

theorem Batteries.BinaryHeap.maxChildF_eq {α : Type u} [Ord α] {sz : ℕ} (a : Vector α sz) (i : Fin sz) :
def Batteries.BinaryHeap.heapifyUpF {α : Type u} [Ord α] (fuel : ℕ) {sz : ℕ} :
Vector α sz → Fin sz → Vector α sz

heapifyUp by structural recursion on fuel.

theorem Batteries.BinaryHeap.heapifyUpF_eq {α : Type u} [Ord α] (fuel : ℕ) {sz : ℕ} (a : Vector α sz) (i : Fin sz) :
↑i < fuel → heapifyUpF fuel a i = heapifyUp a i

With fuel above the position, heapifyUpF is heapifyUp.

def Batteries.BinaryHeap.heapifyDownF {α : Type u} [Ord α] (fuel : ℕ) {sz : ℕ} :
Vector α sz → Fin sz → Vector α sz

heapifyDown by structural recursion on fuel.

theorem Batteries.BinaryHeap.heapifyDownF_eq {α : Type u} [Ord α] (fuel : ℕ) {sz : ℕ} (a : Vector α sz) (i : Fin sz) :
sz - ↑i ≤ fuel → heapifyDownF fuel a i = heapifyDown a i

With fuel above the distance to the end, heapifyDownF is heapifyDown.

def Batteries.BinaryHeap.insertF {α : Type u} [Ord α] (heap : BinaryHeap α) (x : α) :

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.

theorem Batteries.BinaryHeap.insertF_eq {α : Type u} [Ord α] (heap : BinaryHeap α) (x : α) :
heap.insertF x = heap.insert x
def Batteries.BinaryHeap.popMaxF {α : Type u} [Ord α] (heap : BinaryHeap α) :

Removing the maximum by structural recursion.

theorem Batteries.BinaryHeap.popMaxF_eq {α : Type u} [Ord α] (heap : BinaryHeap α) :
heap.popMaxF = heap.popMax
theorem Batteries.BinaryHeap.eq_getElem_zero_of_max {α : Type u} [Ord α] {heap : BinaryHeap α} {x : α} (h : heap.maxF = some x) :
∃ (hpos : 0 < heap.size), x = heap.arr[0]

The maximum is the first element.

theorem Batteries.BinaryHeap.mem_of_max {α : Type u} [Ord α] {heap : BinaryHeap α} {x : α} (h : heap.maxF = some x) :
x ∈ heap
theorem Batteries.BinaryHeap.mem_popMaxF {α : Type u} [Ord α] {heap : BinaryHeap α} {x y : α} (hx : heap.maxF = some x) (hy : y ∈ heap) :
y = x ∨ y ∈ heap.popMaxF

Removing the maximum x leaves every other element.

theorem Batteries.BinaryHeap.mem_of_mem_popMaxF {α : Type u} [Ord α] {heap : BinaryHeap α} {y : α} (hy : y ∈ heap.popMaxF) :
y ∈ heap

Removing the maximum adds no element.

theorem Batteries.BinaryHeap.heapifyUpF_perm {α : Type u} [Ord α] (fuel : ℕ) {sz : ℕ} (a : Vector α sz) (i : Fin sz) :
(heapifyUpF fuel a i).Perm a

Sifting up permutes the elements.

theorem Batteries.BinaryHeap.heapifyDownF_perm {α : Type u} [Ord α] (fuel : ℕ) {sz : ℕ} (a : Vector α sz) (i : Fin sz) :
(heapifyDownF fuel a i).Perm a

Sifting down permutes the elements.

theorem Batteries.BinaryHeap.wf_of_topDown {α : Type u} [Ord α] {sz : ℕ} {v : Vector α sz} (h : WF.TopDown v) :
{ arr := v.toArray }.WF

A well-formed vector makes a well-formed heap.

Heaps whose order changes #

theorem Batteries.BinaryHeap.topDown_of_isGE {α : Type u} {o₁ o₂ : Ord α} {sz : ℕ} {a : Vector α sz} (h : ∀ (k j : Fin sz), (compare a[k] a[j]).isGE = true → (compare a[k] a[j]).isGE = true) (hwf : WF.TopDown a) :

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₂.

theorem Batteries.BinaryHeap.topDown_heapifyUp_of_isGE {α : Type u} {o₁ o₂ : Ord α} [h₁ : Std.TransOrd α] [h₂ : Std.TransOrd α] {sz : ℕ} {a : Vector α sz} {i : Fin sz} (h : ∀ (k j : Fin sz), j ≠ i → (compare a[k] a[j]).isGE = true → (compare a[k] a[j]).isGE = true) (hwf : WF.TopDown a) :

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₂.