Skip to content

feat: verification of binary heap#1602

Open
cmlsharp wants to merge 212 commits intoleanprover-community:mainfrom
cmlsharp:binomial_heap
Open

feat: verification of binary heap#1602
cmlsharp wants to merge 212 commits intoleanprover-community:mainfrom
cmlsharp:binomial_heap

Conversation

@cmlsharp
Copy link
Copy Markdown
Contributor

@cmlsharp cmlsharp commented Jan 6, 2026

This PR is still a work in progress, but I wanted to get it out there.

This PR begins the process of addressing #1442. Thus far I have managed to prove that heapifyDown, heapifyUp, and mkHeap preserve the binary heap correctness property, These should be the lions share of the difficulty, as the remaining operations are largely wrappers around the former two.

As suggested in that thread, this PR also alters the definition of BinaryHeap to use an Ord instance on the contained type rather than a comparison function. The proofs additionally rely on Std.OrientedOrd and Std.TransOrd.

The core correctness property is encoded as:

@[expose]
public def WF.children [Ord α] (a : Vector α sz) (i : Fin sz) : Prop :=
  let left := 2 * i.val + 1
  let right := left + 1
  (∀ _ : left < sz, compare a[i] a[left] |>.isGE) ∧
  (∀ _ : right < sz, compare a[i] a[right] |>.isGE)


@[expose]
public def WF [Ord α] (v : Vector α sz) : Prop :=
  ∀ i : Fin sz, WF.children v i

However, for proving heapifyUp it was more convenient to use a "bottom up" version of a correctness property

def WF.parent [Ord α] (a : Vector α sz) (i : Fin sz) : Prop :=
  ∀ _ : 0 < i.val, compare a[i] a[(i.val - 1)/2] |>.isLE
  
def WF.bottomUp [Ord α] (v : Vector α sz) : Prop :=
  ∀ i : Fin sz, WF.parent v i

I provide a theorem that WF is equivalent to WF.bottomUp.

@cmlsharp
Copy link
Copy Markdown
Contributor Author

cmlsharp commented Jan 6, 2026

WIP

@github-actions github-actions bot added awaiting-review This PR is ready for review; the author thinks it is ready to be merged. WIP work in progress and removed awaiting-review This PR is ready for review; the author thinks it is ready to be merged. labels Jan 6, 2026
@cmlsharp cmlsharp marked this pull request as draft January 6, 2026 06:44
@cmlsharp
Copy link
Copy Markdown
Contributor Author

cmlsharp commented Jan 6, 2026

(I will say, I'm currently overusing grind so compilation is slow at the moment)

@fgdorais
Copy link
Copy Markdown
Collaborator

fgdorais commented Jan 7, 2026

Please merge main to clarify API changes.

@ghost ghost added the merge-conflict This PR has merge conflicts with the `main` branch which must be resolved by the author. label Jan 7, 2026
ghost pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Jan 7, 2026
@ghost ghost added breaks-mathlib and removed merge-conflict This PR has merge conflicts with the `main` branch which must be resolved by the author. labels Jan 7, 2026
@ghost
Copy link
Copy Markdown

ghost commented Jan 7, 2026

Mathlib CI status (docs):

mathlib-nightly-testing bot added a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Feb 17, 2026
@cmlsharp
Copy link
Copy Markdown
Contributor Author

I made a bunch of simplifications to a few of the lemmas, and factored out some duplicated shared structure between them.

Also replace . with \cdot in some places
mathlib-nightly-testing bot added a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Mar 5, 2026
@fgdorais fgdorais changed the title Verification of binary heap feat: verification of binary heap Mar 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting-review This PR is ready for review; the author thinks it is ready to be merged. builds-mathlib

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants