Repository navigation
Conversation
| @@ -0,0 +1,12 @@ | |||
|
|
|||
| noncomputable def skipLeft {β : α → Sort _} | |||
There was a problem hiding this comment.
Should these maybe be in the WellFoundedRelation namespace?
And aren't these in Init.WF already?
There was a problem hiding this comment.
They are annoying to use if they are namespaced. Hopefully leanprover/lean4#879 will be handled at some point, which will make this unnecessary in most cases.
There is a similar definition in Init.WF, but it is nondependent. Here the idea is to introduce the parameter to skip as a variable in the context so it can be used in later relations (e.g. in Fin.upRel).
There was a problem hiding this comment.
They are annoying to use if they are namespaced.
Really? open PSigma works just fine (for the core function). (I completely agree that leanprover/lean4#849 is the proper fix here though.)
There is a similar definition in Init.WF, but it is nondependent.
I am not sure if this makes it better; we now have two functions with the same name, same purpose, but different types. At least a prime would be good here for disambiguation.
There was a problem hiding this comment.
It wouldn't be good to have open PSigma at the top level because the user probably isn't doing anything related to PSigma except for the specification of the well founded relation (they didn't ask for it, it is just how the compilation works). If we really want to put these in the PSigma namespace then termination_by should automatically open this namespace (or perhaps some other namespace dedicated to termination relations), because it is so different from everything else in the user's file.
| exact H (a.size_swap i j) _ (lt_of_le_of_ne j.2 h) | ||
| let ⟨a₂, h₂⟩ := heapifyDown lt a' j' | ||
| ⟨a₂, h₂.trans (a.size_swap i j)⟩ | ||
| termination_by invImage (fun ⟨_, _, a, i⟩ => (⟨a.size, i⟩ : (n : ℕ) ×' Fin n)) $ skipLeft Fin.upRel |
There was a problem hiding this comment.
Here's a simpler termination argument:
/-- Core operation for binary heaps, expressed directly on arrays.
Given an array which is a max-heap, push item `i` down to restore the max-heap property. -/
def heapifyDown (lt : α → α → Bool) (a : Array α) (i : Fin a.size) :
{a' : Array α // a'.size = a.size} :=
let left := 2 * i.1 + 1
let right := left + 1
have left_le : i ≤ left := Nat.le_trans
(by rw [Nat.succ_mul, Nat.one_mul]; exact Nat.le_add_left i i)
(Nat.le_add_right ..)
have right_le : i ≤ right := Nat.le_trans left_le (Nat.le_add_right ..)
have i_le : i ≤ i := Nat.le_refl _
have j : {j : Fin a.size // i ≤ j} := if h : left < a.size then
if lt (a.get i) (a.get ⟨left, h⟩) then ⟨⟨left, h⟩, left_le⟩ else ⟨i, i_le⟩ else ⟨i, i_le⟩
have j := if h : right < a.size then
if lt (a.get j) (a.get ⟨right, h⟩) then ⟨⟨right, h⟩, right_le⟩ else j else j
if h : i.1 = j then ⟨a, rfl⟩ else
let a' := a.swap i j
let j' := ⟨j, by rw [a.size_swap i j]; exact j.1.2⟩
have : a.size - j < a.size - i :=
Nat.sub_lt_sub_left i.2 <| Nat.lt_of_le_and_ne j.2 h
let ⟨a₂, h₂⟩ := heapifyDown lt a' j'
⟨a₂, h₂.trans (a.size_swap i j)⟩
termination_by measure fun ⟨α, lt, a, i⟩ => a.size - i
decreasing_by simp [measure, invImage, InvImage]; assumptionBTW, instead of Fin.upRel I think it would be more ergonomic to talk about -i and use the standard order. (alas, this doesn't work directly because you then have two versions of -: one in Fin a.size and one in Fin a'.size...)
There was a problem hiding this comment.
The whole point of Nat.upRel and Fin.upRel is so that you don't have to do this a - x < a - y argument all the time. I think we should try to make this kind of proof more ergonomic, there is no reason to always be doing downward induction. I'm not too happy with the skipLeft stuff, I think more should be automated than currently, but I do think that the original proof is in the right direction.
There was a problem hiding this comment.
Personally I find the measure version much easier to read compared to explicitly specifying the relations. Although ideally measure would accept functions into any well-ordered type. Then you could write measure fun ⟨α, lt, a, i⟩ => (⟨a.size, -i⟩ : (n : ℕ) ×' Fin n) or measure fun ⟨α, lt, a, i⟩ => (⟨a.size, i⟩ : (n : ℕ) ×' OrderDual (Fin n)). The OrderDual version also works for any finite type, not just Fin n.
This is true, as far as I can tell Lean doesn't even generate an equational lemma for WF definitions at the moment. And if you make one yourself you still need to deal with the huge if-then-else soup and use well-founded induction. |
|
bors r+ |
The main addition is a type `BinaryHeap α lt` of binary max-heaps stored in an array. Also some cleanup: * Fix line length and formatting in `Data.Fin.Basic` * Add `Mathlib.Init.WF` for some more tools for proving well foundedness * Use `termination_by` instead of manual termination compilation for `String.toAsciiByteArray` and `ByteSlice.forIn` I originally wrote `heapifyDown` et al without the embedded postcondition, but proving facts about definitions like `heapifyDown` is incredibly painful right now.
|
Pull request successfully merged into master. Build succeeded: |
This PR adds `register_try?_tactic` calls for all finishing tactics currently registered with `register_hint`, which are not already covered by `grind` modules (i.e. I have not registered `linarith` or `ring`), using identical priority values: **Common tactics** (Common.lean): - tauto (priority 500) - aesop (priority 80) - fun_prop (priority 200) **Domain-specific tactics**: - norm_num (1000), noncomm_ring (1000) - field (850), bound (70) - finiteness (1000), compute_degree (1000), positivity (1000), field_simp (1000) 🤖 Prepared with Claude Code
* deprecation in lakefile * chore: bump to nightly-2025-12-10 * Update lean-toolchain for testing leanprover/lean4#11581 * Update lean-toolchain for testing leanprover/lean4#11587 * chore: adaptations for nightly-2025-12-10 * fix * fix for leanprover/lean4#11581 * deprecation * remove unused argument * feat: register mathlib tactics with try? (#136) This PR adds `register_try?_tactic` calls for all finishing tactics currently registered with `register_hint`, which are not already covered by `grind` modules (i.e. I have not registered `linarith` or `ring`), using identical priority values: **Common tactics** (Common.lean): - tauto (priority 500) - aesop (priority 80) - fun_prop (priority 200) **Domain-specific tactics**: - norm_num (1000), noncomm_ring (1000) - field (850), bound (70) - finiteness (1000), compute_degree (1000), positivity (1000), field_simp (1000) 🤖 Prepared with Claude Code * chore: adaptations for nightly-2025-12-10 (#141) Co-authored-by: mathlib4-bot <[email protected]> Co-authored-by: leanprover-community-mathlib4-bot <[email protected]> Co-authored-by: github-actions <[email protected]> Co-authored-by: Joachim Breitner <[email protected]> * fix deprecation in mk_all * chore: adaptations for nightly-2025-12-10 * chore: bump to nightly-2025-12-11 * Kick CI * lake update * fix deps * remove upstreamed * workaround * fix * chore: bump to nightly-2025-12-12 * Update lean-toolchain for testing leanprover/lean4#11620 * more fix * fix the fix * lake update * adding a prime to NameMapExtension * adaptations for leanprover/lean4#11620 * revert spurious change * warning * Merge master into nightly-testing * fixes * fix * deprecations * chore: bump to nightly-2025-12-13 * lake update * lake update * fix merge * revert a lia back to omega * remove upstreamed * deprecation * fixes * fix archive * lint * lake update aesop * chore: adaptations for nightly-2025-12-13 * remove aliases for upstreamed lemmas * deprecation * revert a lia back to omega * lake update * cutsat to lia * hm * note * adaptation notes * chore: bump to nightly-2025-12-14 * remove @[grind =] from even_sign_iff * chore: adaptations for nightly-2025-12-13 (#142) * lake update * chore: adaptations for nightly-2025-12-14 (#143) * chore: adaptations for nightly-2025-12-14 * chore: update lean-toolchain to leanprover/lean4:nightly-2025-12-14 * chore: bump to nightly-2025-12-15 * chore: adaptations for nightly-2025-12-15 (#144) * chore: bump to nightly-2025-12-16 * Update batteries * Adapt to leanprover/lean4#11608 * Pull in aesop fixes (from my fork, sorry for that) * chore: adaptations for nightly-2025-12-16 * fixes * chore: bump to nightly-2025-12-17 * chore: adaptations for nightly-2025-12-17 * chore: adaptations for nightly-2025-12-16 (#145) * chore: adaptations for nightly-2025-12-17 (#146) * chore: adaptations for nightly-2025-12-17 * chore: bump to nightly-2025-12-18 * lake update * chore: adaptations for nightly-2025-12-18 * chore: bump to nightly-2025-12-19 * remove upstreamed * remove upstreamed * fixes * chore: bump to nightly-2025-12-20 * mk_all * chore: adaptations for nightly-2025-12-20 * chore: adaptations for nightly-2025-12-20 (#148) Co-authored-by: mathlib4-bot <[email protected]> Co-authored-by: leanprover-community-mathlib4-bot <[email protected]> Co-authored-by: Kim Morrison <[email protected]> Co-authored-by: Kim Morrison <[email protected]> Co-authored-by: Joachim Breitner <[email protected]> * chore: adaptations for nightly-2025-12-20 * chore: bump to nightly-2025-12-21 * chore: adaptations for nightly-2025-12-21 * chore: adaptations for nightly-2025-12-21 (#149) * chore: adaptations for nightly-2025-12-21 * chore: bump to nightly-2025-12-22 * chore: adaptations for nightly-2025-12-22 * chore: bump to nightly-2025-12-23 * chore: bump to nightly-2025-12-24 * chore: adaptations for nightly-2025-12-24 * chore: bump to nightly-2025-12-25 * chore: adaptations for nightly-2025-12-25 * chore: bump to nightly-2025-12-26 * chore: adaptations for nightly-2025-12-26 * chore: bump to nightly-2025-12-27 * chore: bump to nightly-2025-12-28 * chore: bump to nightly-2025-12-29 * chore: bump to nightly-2025-12-30 * chore: bump to nightly-2025-12-31 * chore: bump to nightly-2026-01-01 * chore: bump to nightly-2026-01-02 * lake update * chore: bump to nightly-2026-01-03 * chore: bump to nightly-2026-01-04 * merge lean-pr-testing-10059 * lake update * deprecations * Revert "merge lean-pr-testing-10387" This reverts commit e166d33, reversing changes made to cf6049c. * fix * Revert incorrectly merged lean-pr-testing branches Reverts the following branches that were incorrectly merged due to a bug in discover-lean-pr-testing.yml where an empty prs.txt caused grep to match ALL lean-pr-testing branches: - lean-pr-testing-2714 (from Oct 2023!) - lean-pr-testing-11455 - lean-pr-testing-11163 - lean-pr-testing-10231 - lean-pr-testing-10178 - lean-pr-testing-10059 These PRs are not in nightly-2026-01-04, so their adaptations should not be merged. The bug fix is in: #33546 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]> * deprecations * Update lean-toolchain for testing leanprover/lean4#11893 * fix * chore: bump to nightly-2026-01-05 * remove upstreamed * lake update * fix * chore: adaptations for nightly-2026-01-05 * chore: bump to nightly-2026-01-06 * chore: adaptations for nightly-2026-01-06 * chore: adaptations for nightly-2026-01-06 * chore: bump to nightly-2026-01-07 * chore: adaptations for nightly-2026-01-07 * chore: adaptations for nightly-2026-01-09 (#159) * replace aesop proof * chore: adaptations for nightly-2026-01-07 * chore: bump to nightly-2026-01-08 * chore: adaptations for nightly-2026-01-08 * chore: bump to nightly-2026-01-09 --------- Co-authored-by: leanprover-community-mathlib4-bot <[email protected]> Co-authored-by: Kim Morrison <[email protected]> Co-authored-by: mathlib4-bot <[email protected]> * chore: adaptations for nightly-2026-01-23 (#160) Co-authored-by: leanprover-community-mathlib4-bot <[email protected]> Co-authored-by: Sebastian Ullrich <[email protected]> Co-authored-by: Rob23oba <[email protected]> Co-authored-by: Rob23oba <[email protected]> Co-authored-by: Markus Himmel <[email protected]> * chore: adaptations for nightly-2026-01-24 (#161) Co-authored-by: leanprover-community-mathlib4-bot <[email protected]> Co-authored-by: Sebastian Ullrich <[email protected]> Co-authored-by: Rob23oba <[email protected]> Co-authored-by: Rob23oba <[email protected]> Co-authored-by: Markus Himmel <[email protected]> * chore: adaptations for nightly-2026-01-25 (#162) Co-authored-by: leanprover-community-mathlib4-bot <[email protected]> Co-authored-by: Sebastian Ullrich <[email protected]> Co-authored-by: Rob23oba <[email protected]> Co-authored-by: Rob23oba <[email protected]> Co-authored-by: Markus Himmel <[email protected]> * chore: bump toolchain to v4.28.0-rc1 * fix: use main/master instead of nightly-testing for dependencies --------- Co-authored-by: leanprover-community-mathlib4-bot <[email protected]> Co-authored-by: mathlib4-bot <[email protected]> Co-authored-by: github-actions <[email protected]> Co-authored-by: Joachim Breitner <[email protected]> Co-authored-by: Sebastian Ullrich <[email protected]> Co-authored-by: Ruben Van de Velde <[email protected]> Co-authored-by: leanprover-community-bot-assistant <97194829+leanprover-community-bot-assistant@users.noreply.github.com> Co-authored-by: Claude Opus 4.5 <[email protected]> Co-authored-by: Kevin Buzzard <[email protected]> Co-authored-by: Rob23oba <[email protected]> Co-authored-by: Rob23oba <[email protected]> Co-authored-by: Markus Himmel <[email protected]>
* deprecation in lakefile * chore: bump to nightly-2025-12-10 * Update lean-toolchain for testing leanprover/lean4#11581 * Update lean-toolchain for testing leanprover/lean4#11587 * chore: adaptations for nightly-2025-12-10 * fix * fix for leanprover/lean4#11581 * deprecation * remove unused argument * feat: register mathlib tactics with try? (leanprover-community#136) This PR adds `register_try?_tactic` calls for all finishing tactics currently registered with `register_hint`, which are not already covered by `grind` modules (i.e. I have not registered `linarith` or `ring`), using identical priority values: **Common tactics** (Common.lean): - tauto (priority 500) - aesop (priority 80) - fun_prop (priority 200) **Domain-specific tactics**: - norm_num (1000), noncomm_ring (1000) - field (850), bound (70) - finiteness (1000), compute_degree (1000), positivity (1000), field_simp (1000) 🤖 Prepared with Claude Code * chore: adaptations for nightly-2025-12-10 (leanprover-community#141) Co-authored-by: mathlib4-bot <[email protected]> Co-authored-by: leanprover-community-mathlib4-bot <[email protected]> Co-authored-by: github-actions <[email protected]> Co-authored-by: Joachim Breitner <[email protected]> * fix deprecation in mk_all * chore: adaptations for nightly-2025-12-10 * chore: bump to nightly-2025-12-11 * Kick CI * lake update * fix deps * remove upstreamed * workaround * fix * chore: bump to nightly-2025-12-12 * Update lean-toolchain for testing leanprover/lean4#11620 * more fix * fix the fix * lake update * adding a prime to NameMapExtension * adaptations for leanprover/lean4#11620 * revert spurious change * warning * Merge master into nightly-testing * fixes * fix * deprecations * chore: bump to nightly-2025-12-13 * lake update * lake update * fix merge * revert a lia back to omega * remove upstreamed * deprecation * fixes * fix archive * lint * lake update aesop * chore: adaptations for nightly-2025-12-13 * remove aliases for upstreamed lemmas * deprecation * revert a lia back to omega * lake update * cutsat to lia * hm * note * adaptation notes * chore: bump to nightly-2025-12-14 * remove @[grind =] from even_sign_iff * chore: adaptations for nightly-2025-12-13 (leanprover-community#142) * lake update * chore: adaptations for nightly-2025-12-14 (leanprover-community#143) * chore: adaptations for nightly-2025-12-14 * chore: update lean-toolchain to leanprover/lean4:nightly-2025-12-14 * chore: bump to nightly-2025-12-15 * chore: adaptations for nightly-2025-12-15 (leanprover-community#144) * chore: bump to nightly-2025-12-16 * Update batteries * Adapt to leanprover/lean4#11608 * Pull in aesop fixes (from my fork, sorry for that) * chore: adaptations for nightly-2025-12-16 * fixes * chore: bump to nightly-2025-12-17 * chore: adaptations for nightly-2025-12-17 * chore: adaptations for nightly-2025-12-16 (leanprover-community#145) * chore: adaptations for nightly-2025-12-17 (leanprover-community#146) * chore: adaptations for nightly-2025-12-17 * chore: bump to nightly-2025-12-18 * lake update * chore: adaptations for nightly-2025-12-18 * chore: bump to nightly-2025-12-19 * remove upstreamed * remove upstreamed * fixes * chore: bump to nightly-2025-12-20 * mk_all * chore: adaptations for nightly-2025-12-20 * chore: adaptations for nightly-2025-12-20 (leanprover-community#148) Co-authored-by: mathlib4-bot <[email protected]> Co-authored-by: leanprover-community-mathlib4-bot <[email protected]> Co-authored-by: Kim Morrison <[email protected]> Co-authored-by: Kim Morrison <[email protected]> Co-authored-by: Joachim Breitner <[email protected]> * chore: adaptations for nightly-2025-12-20 * chore: bump to nightly-2025-12-21 * chore: adaptations for nightly-2025-12-21 * chore: adaptations for nightly-2025-12-21 (leanprover-community#149) * chore: adaptations for nightly-2025-12-21 * chore: bump to nightly-2025-12-22 * chore: adaptations for nightly-2025-12-22 * chore: bump to nightly-2025-12-23 * chore: bump to nightly-2025-12-24 * chore: adaptations for nightly-2025-12-24 * chore: bump to nightly-2025-12-25 * chore: adaptations for nightly-2025-12-25 * chore: bump to nightly-2025-12-26 * chore: adaptations for nightly-2025-12-26 * chore: bump to nightly-2025-12-27 * chore: bump to nightly-2025-12-28 * chore: bump to nightly-2025-12-29 * chore: bump to nightly-2025-12-30 * chore: bump to nightly-2025-12-31 * chore: bump to nightly-2026-01-01 * chore: bump to nightly-2026-01-02 * lake update * chore: bump to nightly-2026-01-03 * chore: bump to nightly-2026-01-04 * merge lean-pr-testing-10059 * lake update * deprecations * Revert "merge lean-pr-testing-10387" This reverts commit e166d33, reversing changes made to cf6049c. * fix * Revert incorrectly merged lean-pr-testing branches Reverts the following branches that were incorrectly merged due to a bug in discover-lean-pr-testing.yml where an empty prs.txt caused grep to match ALL lean-pr-testing branches: - lean-pr-testing-2714 (from Oct 2023!) - lean-pr-testing-11455 - lean-pr-testing-11163 - lean-pr-testing-10231 - lean-pr-testing-10178 - lean-pr-testing-10059 These PRs are not in nightly-2026-01-04, so their adaptations should not be merged. The bug fix is in: leanprover-community#33546 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]> * deprecations * Update lean-toolchain for testing leanprover/lean4#11893 * fix * chore: bump to nightly-2026-01-05 * remove upstreamed * lake update * fix * chore: adaptations for nightly-2026-01-05 * chore: bump to nightly-2026-01-06 * chore: adaptations for nightly-2026-01-06 * chore: adaptations for nightly-2026-01-06 * chore: bump to nightly-2026-01-07 * chore: adaptations for nightly-2026-01-07 * chore: adaptations for nightly-2026-01-09 (leanprover-community#159) * replace aesop proof * chore: adaptations for nightly-2026-01-07 * chore: bump to nightly-2026-01-08 * chore: adaptations for nightly-2026-01-08 * chore: bump to nightly-2026-01-09 --------- Co-authored-by: leanprover-community-mathlib4-bot <[email protected]> Co-authored-by: Kim Morrison <[email protected]> Co-authored-by: mathlib4-bot <[email protected]> * chore: adaptations for nightly-2026-01-23 (leanprover-community#160) Co-authored-by: leanprover-community-mathlib4-bot <[email protected]> Co-authored-by: Sebastian Ullrich <[email protected]> Co-authored-by: Rob23oba <[email protected]> Co-authored-by: Rob23oba <[email protected]> Co-authored-by: Markus Himmel <[email protected]> * chore: adaptations for nightly-2026-01-24 (leanprover-community#161) Co-authored-by: leanprover-community-mathlib4-bot <[email protected]> Co-authored-by: Sebastian Ullrich <[email protected]> Co-authored-by: Rob23oba <[email protected]> Co-authored-by: Rob23oba <[email protected]> Co-authored-by: Markus Himmel <[email protected]> * chore: adaptations for nightly-2026-01-25 (leanprover-community#162) Co-authored-by: leanprover-community-mathlib4-bot <[email protected]> Co-authored-by: Sebastian Ullrich <[email protected]> Co-authored-by: Rob23oba <[email protected]> Co-authored-by: Rob23oba <[email protected]> Co-authored-by: Markus Himmel <[email protected]> * chore: bump toolchain to v4.28.0-rc1 * fix: use main/master instead of nightly-testing for dependencies --------- Co-authored-by: leanprover-community-mathlib4-bot <[email protected]> Co-authored-by: mathlib4-bot <[email protected]> Co-authored-by: github-actions <[email protected]> Co-authored-by: Joachim Breitner <[email protected]> Co-authored-by: Sebastian Ullrich <[email protected]> Co-authored-by: Ruben Van de Velde <[email protected]> Co-authored-by: leanprover-community-bot-assistant <97194829+leanprover-community-bot-assistant@users.noreply.github.com> Co-authored-by: Claude Opus 4.5 <[email protected]> Co-authored-by: Kevin Buzzard <[email protected]> Co-authored-by: Rob23oba <[email protected]> Co-authored-by: Rob23oba <[email protected]> Co-authored-by: Markus Himmel <[email protected]>
The main addition is a type
BinaryHeap α ltof binary max-heaps stored in an array.Also some cleanup:
Data.Fin.BasicMathlib.Init.WFfor some more tools for proving well foundednesstermination_byinstead of manual termination compilation forString.toAsciiByteArrayandByteSlice.forInI originally wrote
heapifyDownet al without the embedded postcondition, but proving facts about definitions likeheapifyDownis incredibly painful right now.