Skip to content

feat(Tactic): erw tries rw first and warns if that succeeds#17638

Closed
Vierkantor wants to merge 3 commits intomasterfrom
try_this-erw
Closed

feat(Tactic): erw tries rw first and warns if that succeeds#17638
Vierkantor wants to merge 3 commits intomasterfrom
try_this-erw

Conversation

@Vierkantor
Copy link
Copy Markdown
Contributor

@Vierkantor Vierkantor commented Oct 11, 2024

This PR adds a macro rule to the erw tactic that first tries rw at reducible transparency, and displays a "Try this:" if that succeeded. This should help us get rid of uselessly slow erws.

This is a relatively simple implementation that does not verify if erw and rw would have resulted in the same exact rewrite. So it is possible that erw and rw both succeed but rewrite different occurrences, and there would be a suggestion to replace erw with rw in that case. I think the complication of running both at the same time and checking the goal state afterwards doesn't weigh up to accepting that this happens and adding an explanation to the warning generated by erw.

I import the erw extension in Tactic.Common: it might be nice to migrate this up even earlier in the import hierarchy but Mathlib.Tactic.TryThis is a somewhat heavy import to put in Mathlib.Tactic.Core.

Zulip thread: https://leanprover.zulipchat.com/#narrow/stream/287929-mathlib4/topic/Fixing.20the.20.60erw.60.20hell.20around.20concrete.20categories


Open in Gitpod

@Vierkantor Vierkantor added the t-meta Tactics, attributes or user commands label Oct 11, 2024
@Vierkantor Vierkantor requested a review from jcommelin October 11, 2024 08:40
@github-actions
Copy link
Copy Markdown

github-actions bot commented Oct 11, 2024

PR summary de4fb5fbf8

Import changes for modified files

Dependency changes

File Base Count Head Count Change
Mathlib.Tactic.Common 244 246 +2 (+0.82%)
Mathlib.Algebra.Order.CauSeq.Basic 543 545 +2 (+0.37%)
Mathlib.CategoryTheory.Sites.Subcanonical 680 682 +2 (+0.29%)
Import changes for all files
Files Import difference
There are 4496 files with changed transitive imports taking up over 190394 characters: this is too many to display!
You can run scripts/import_trans_difference.sh all locally to see the whole output.

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/declarations_diff.sh <optional_commit>

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

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

@grunweg
Copy link
Copy Markdown
Contributor

grunweg commented Oct 11, 2024

Thanks for this PR! I'm (perhaps needlessly so) worries about the performance effects of this change. Can you benchmark this/is the speedcenter up and working already?
Or is the idea that this will reduce slow erw's over time, and hence be worth it even if it's a small slow-down?

@Vierkantor
Copy link
Copy Markdown
Contributor Author

Thanks for this PR! I'm (perhaps needlessly so) worries about the performance effects of this change. Can you benchmark this/is the speedcenter up and working already? Or is the idea that this will reduce slow erw's over time, and hence be worth it even if it's a small slow-down?

Unfortunately the speedcenter was still down yesterday, and I don't see any movement in the zulip thread. I agree that this will make Mathlib a bit slower, but the extra cost of doing a rw in addition to erw should make that cost somewhat marginal. And removing erws definitely makes up for that. And even if it weren't about speed, then simply cleaning up erws that don't need to be erw is nice for code clarity.

@mathlib4-dependent-issues-bot mathlib4-dependent-issues-bot added the blocked-by-other-PR This PR depends on another PR (this label is automatically managed by a bot) label Oct 11, 2024
@Vierkantor Vierkantor removed the blocked-by-other-PR This PR depends on another PR (this label is automatically managed by a bot) label Oct 11, 2024
Copy link
Copy Markdown
Contributor

@grunweg grunweg left a comment

Choose a reason for hiding this comment

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

Two nits about outdated comments - otherwise the erw -> rw replacements look good to me. The macro looks plausible to me, but I'm not an expert on them - that's a low-confidence approval of that part.

@Vierkantor
Copy link
Copy Markdown
Contributor Author

Two nits about outdated comments

Nicely spotted, thanks!

@leanprover-community-bot-assistant leanprover-community-bot-assistant added the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Oct 12, 2024
@kim-em
Copy link
Copy Markdown
Contributor

kim-em commented Oct 13, 2024

Lets split this into separate PRs for changing proofs and for the macro.

@kim-em
Copy link
Copy Markdown
Contributor

kim-em commented Oct 13, 2024

I would prefer a solution where we only run the extra check in CI, like we do for says.

@Vierkantor
Copy link
Copy Markdown
Contributor Author

Lets split this into separate PRs for changing proofs and for the macro.

See #17703 for only the changed proofs.

@mathlib4-dependent-issues-bot mathlib4-dependent-issues-bot added the blocked-by-other-PR This PR depends on another PR (this label is automatically managed by a bot) label Oct 13, 2024
@leanprover-community-bot-assistant leanprover-community-bot-assistant removed the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Oct 13, 2024
@grunweg
Copy link
Copy Markdown
Contributor

grunweg commented Oct 13, 2024

I'm curious about the overall balance of this PR, thus let me bench it.

@grunweg
Copy link
Copy Markdown
Contributor

grunweg commented Oct 13, 2024

!bench

mathlib-bors bot pushed a commit that referenced this pull request Oct 13, 2024
Go through Mathlib and replace `erw` calls where `rw` would also succeed.

This PR is split off from #17638, containing all the changes that the linter suggested.



Co-authored-by: Anne Baanen <Vierkantor@users.noreply.github.com>
@mathlib4-dependent-issues-bot mathlib4-dependent-issues-bot removed the blocked-by-other-PR This PR depends on another PR (this label is automatically managed by a bot) label Oct 13, 2024
@mathlib4-dependent-issues-bot
Copy link
Copy Markdown
Collaborator

@leanprover-bot
Copy link
Copy Markdown
Collaborator

Here are the benchmark results for commit edc1e5c.
There were significant changes against commit 626e646:

  Benchmark                                                  Metric         Change
  ================================================================================
- ~Mathlib.Algebra.Module.LocalizedModule                    instructions     8.2%
- ~Mathlib.CategoryTheory.Adjunction.Lifting.Right           instructions    46.6%
- ~Mathlib.Geometry.RingedSpace.PresheafedSpace.Gluing       instructions    15.4%
- ~Mathlib.RepresentationTheory.GroupCohomology.Resolution   instructions     8.1%

@grunweg
Copy link
Copy Markdown
Contributor

grunweg commented Oct 13, 2024

The previous PR was performance-neutral - so the benchmark should mostly represent the effect of this PR. Overall, there is a slowdown of 200*10⁹ instruction (about 0.1-0.2% of all of mathlib).

@Vierkantor
Copy link
Copy Markdown
Contributor Author

I'll hold off on merging this PR then, running it manually every so often until the number of erws is more manageable.

@Vierkantor Vierkantor added the WIP Work in progress label Oct 15, 2024
@leanprover-community-bot-assistant leanprover-community-bot-assistant added the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Oct 16, 2024
@Vierkantor Vierkantor force-pushed the try_this-erw branch 2 times, most recently from 6f76810 to af3c28c Compare October 18, 2024 16:03
mathlib-bors bot pushed a commit that referenced this pull request Oct 18, 2024
Automatically found by the `erw` linter from #17638.
@leanprover-community-bot-assistant leanprover-community-bot-assistant removed the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Oct 18, 2024
mathlib-bors bot pushed a commit that referenced this pull request Oct 28, 2024
The `erw` linter of #17638 spotted two `erw`s that can be turned into `rw` for free.
This PR adds a macro rule to the `erw` tactic that first tries `rw` at reducible transparency, and displays a "Try this:" if that succeeded. This should help us get rid of uselessly slow `erw`s.

This is a relatively simple implementation that does not verify if `erw` and `rw` would have resulted in the same exact rewrite. So it is possible that `erw` and `rw` both succeed but rewrite different occurrences, and there would be a suggestion to replace `erw` with `rw` in that case. I think the complication of running both at the same time and checking the goal state afterwards doesn't weigh up to accepting that this happens and adding an explanation to the warning generated by `erw`.

I import the `erw` extension in `Tactic.Common`: it might be nice to migrate this up even earlier in the import hierarchy but `Mathlib.Tactic.TryThis` is a somewhat heavy import to put in `Mathlib.Tactic.Core`.
mathlib-bors bot pushed a commit that referenced this pull request Nov 11, 2024
The linter of #17638 found five more `erw`s that could also be `rw`s. 🐙
mathlib-bors bot pushed a commit that referenced this pull request Nov 11, 2024
The linter of #17638 found five more `erw`s that could also be `rw`s. 🐙
Jun2M pushed a commit that referenced this pull request Nov 17, 2024
The linter of #17638 found five more `erw`s that could also be `rw`s. 🐙
TobiasLeichtfried pushed a commit that referenced this pull request Nov 21, 2024
The linter of #17638 found five more `erw`s that could also be `rw`s. 🐙
Vierkantor added a commit that referenced this pull request Dec 2, 2024
The `erw` linter from #17638 caught these `erw`s that can become `rw` without breaking the proofs.
mathlib-bors bot pushed a commit that referenced this pull request Dec 2, 2024
The `erw` linter from #17638 caught these `erw`s that can become `rw` without breaking the proofs.
@leanprover-community-bot-assistant leanprover-community-bot-assistant added the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Feb 11, 2025
@Vierkantor
Copy link
Copy Markdown
Contributor Author

I think this can now be definitively closed in favour of erw?.

@Vierkantor Vierkantor closed this Feb 24, 2025
@YaelDillies YaelDillies deleted the try_this-erw branch August 17, 2025 11:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) t-meta Tactics, attributes or user commands WIP Work in progress

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants