Skip to content

Conversation

@cmlsharp
Copy link
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
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
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
Collaborator

fgdorais commented Jan 7, 2026

Please merge main to clarify API changes.

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

Mathlib CI status (docs):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaks-mathlib WIP work in progress

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants