Skip to content

[Merged by Bors] - chore(Order/Defs/LinearOrder): avoid grind in very fundamental lemmas#35841

Closed
astrainfinita wants to merge 8 commits intoleanprover-community:masterfrom
astrainfinita:linearorder_lemmas
Closed

[Merged by Bors] - chore(Order/Defs/LinearOrder): avoid grind in very fundamental lemmas#35841
astrainfinita wants to merge 8 commits intoleanprover-community:masterfrom
astrainfinita:linearorder_lemmas

Conversation

@astrainfinita
Copy link
Copy Markdown
Collaborator

@astrainfinita astrainfinita commented Feb 27, 2026

Some lemmas can be optimized more elegantly, but it requires moving lemmas from other files, so I will do it in a subsequent PR.


Zulip

Open in Gitpod

@astrainfinita astrainfinita added the t-order Order theory label Feb 27, 2026
@github-actions
Copy link
Copy Markdown

github-actions bot commented Feb 27, 2026

PR summary ba4b2e5a2c

Import changes for modified files

No significant changes to the import graph

Import changes for all files
Files Import difference

Declarations diff

No declarations were harmed in the making of this PR! 🐙

You can run this locally as follows
## summary with just the declaration names:
./scripts/pr_summary/declarations_diff.sh <optional_commit>

## more verbose report:
./scripts/pr_summary/declarations_diff.sh long <optional_commit>

The doc-module for scripts/pr_summary/declarations_diff.sh contains some details about this script.


No changes to technical debt.

You can run this locally as

./scripts/reporting/technical-debt-metrics.sh pr_summary
  • The relative value is the weighted sum of the differences with weight given by the inverse of the current value of the statistic.
  • The absolute value is the relative value divided by the total sum of the inverses of the current values (i.e. the weighted average of the differences).

@grunweg
Copy link
Copy Markdown
Contributor

grunweg commented Feb 27, 2026

You're also moving a few lemmas, right? Can you say why the new location is better (or maybe split that into a separate PR)? And can you add a comment to the file that using grind is intentionally avoided, to avoid regressions?

@grunweg grunweg added the awaiting-author A reviewer has asked the author a question or requested changes. label Feb 27, 2026
@astrainfinita astrainfinita removed the awaiting-author A reviewer has asked the author a question or requested changes. label Feb 28, 2026
Copy link
Copy Markdown
Contributor

@Komyyy Komyyy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution!

If you want to make the proofs more understandable, you can prove these lemma elegantly without simps!

Comment on lines +206 to +207
rw [LinearOrder.compare_eq_compareOfLessAndEq, compareOfLessAndEq]
grind
simpa [apply_ite (· = Ordering.gt), ne_iff_lt_or_gt, and_or_left, - not_lt] using not_lt_of_gt
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  rw [LinearOrder.compare_eq_compareOfLessAndEq,
    compareOfLessAndEq_eq_gt le_antisymm le_total not_le]

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since some people expressed concern about the axiom of choice on zulip, I used a slightly more verbose proof instead.

Copy link
Copy Markdown
Contributor

@Komyyy Komyyy Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recall that change avoiding choice should only be done when the change is small.

Rather than avoiding compareOfLessAndEq_eq_gt, we should fix the proof of lt_iff_not_gt_and_ne_of_antisymm_of_total_of_not_le, which compareOfLessAndEq_eq_gt depends on.

With this small change, we can indeed eliminate the use of choice. The current proof appears to mistakenly use DecidableLT where DecidableLE is needed:

theorem lt_iff_not_gt_and_ne_of_antisymm_of_total_of_not_le
    {α : Type u} [LT α] [LE α] [DecidableLT α] [DecidableEq α]
    (antisymm : ∀ {x y : α}, x ≤ y → y ≤ x → x = y)
    (total : ∀ (x y : α), x ≤ y ∨ y ≤ x) (not_le : ∀ {x y : α}, ¬ x ≤ y ↔ y < x) (x y : α) :
    x < y ↔ ¬ y < x ∧ x ≠ y := by
  simp only [← not_le, Classical.not_not]
  constructor
  · intro h
    have refl := by cases total y y <;> assumption
    exact ⟨(total _ _).resolve_left h, fun h' => (h' ▸ h) refl⟩
  · intro ⟨h₁, h₂⟩ h₃
    exact h₂ (antisymm h₁ h₃)

theorem lt_iff_not_gt_and_ne_of_antisymm_of_total_of_not_le
    {α : Type u} [LT α] [LE α] [DecidableLE α] [DecidableEq α]
    (antisymm : ∀ {x y : α}, x ≤ y → y ≤ x → x = y)
    (total : ∀ (x y : α), x ≤ y ∨ y ≤ x) (not_le : ∀ {x y : α}, ¬ x ≤ y ↔ y < x) (x y : α) :
    x < y ↔ ¬ y < x ∧ x ≠ y := by
  simp only [← not_le, Decidable.not_not]
  constructor
  · intro h
    have refl := by cases total y y <;> assumption
    exact ⟨(total _ _).resolve_left h, fun h' => (h' ▸ h) refl⟩
  · intro ⟨h₁, h₂⟩ h₃
    exact h₂ (antisymm h₁ h₃)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will add [DecidableLE α] to other APIs, and AFAIK, Lean core is unlikely to accept contributions for avoiding choice. But indeed, compareOfLessAndEq needs refactoring, so let's just go ahead with this.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Lean core will accept this change, as it seems more like an argument type fix than a change to avoid choice, and the number of affected lemmas does not seem to be much.

@Komyyy Komyyy added the awaiting-author A reviewer has asked the author a question or requested changes. label Mar 8, 2026
@astrainfinita astrainfinita removed the awaiting-author A reviewer has asked the author a question or requested changes. label Mar 8, 2026
@Komyyy Komyyy added the awaiting-author A reviewer has asked the author a question or requested changes. label Mar 9, 2026
@astrainfinita astrainfinita removed the awaiting-author A reviewer has asked the author a question or requested changes. label Mar 10, 2026
Copy link
Copy Markdown
Contributor

@Komyyy Komyyy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

maintainer merge

@github-actions
Copy link
Copy Markdown

🚀 Pull request has been placed on the maintainer queue by Komyyy.


trigger_name: pull_request_review
wf_run: workflow_run

@mathlib-triage mathlib-triage bot added the maintainer-merge A reviewer has approved the changed; awaiting maintainer approval. label Mar 10, 2026
@Komyyy Komyyy removed their assignment Mar 10, 2026
@Vierkantor
Copy link
Copy Markdown
Contributor

bors r+

mathlib-bors bot pushed a commit that referenced this pull request Mar 11, 2026
…as (#35841)

Some lemmas can be optimized more elegantly, but it requires moving lemmas from other files, so I will do it in a subsequent PR.
@mathlib-triage mathlib-triage bot added ready-to-merge This PR has been sent to bors. and removed maintainer-merge A reviewer has approved the changed; awaiting maintainer approval. labels Mar 11, 2026
(lt_or_ge a b).imp_right (fun h ↦ (Decidable.lt_or_eq_of_le' h).symm)

@[to_dual self]
lemma le_of_not_gt (h : ¬b < a) : a ≤ b := (le_or_gt a b).resolve_right h
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a much nicer proof! 🎉


@[to_dual]
lemma min_assoc (a b c : α) : min (min a b) c = min a (min b c) := by grind
lemma min_assoc (a b c : α) : min (min a b) c = min a (min b c) :=
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit dubious to me, since we can also prove this by case analysis (on who is the smallest element of {a, b, c}). But it matches the original proof pre-grind so I can live with this!

@mathlib-bors
Copy link
Copy Markdown
Contributor

mathlib-bors bot commented Mar 11, 2026

Pull request successfully merged into master.

Build succeeded:

@mathlib-bors mathlib-bors bot changed the title chore(Order/Defs/LinearOrder): avoid grind in very fundamental lemmas [Merged by Bors] - chore(Order/Defs/LinearOrder): avoid grind in very fundamental lemmas Mar 11, 2026
@mathlib-bors mathlib-bors bot closed this Mar 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-to-merge This PR has been sent to bors. t-order Order theory

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants