Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Vierkantor
Copy link
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
Copy link

github-actions bot commented Oct 11, 2024

PR summary af3c28c713

Import changes for modified files

Dependency changes

File Base Count Head Count Change
Mathlib.Tactic.Common 244 246 +2 (+0.82%)
Mathlib.CategoryTheory.Shift.Basic 445 447 +2 (+0.45%)
Mathlib.CategoryTheory.Limits.Shapes.Reflexive 556 558 +2 (+0.36%)
Mathlib.Topology.Order.ScottTopology 671 673 +2 (+0.30%)
Import changes for all files
Files Import difference
There are 4319 files with changed transitive imports taking up over 182543 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
Collaborator

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
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 to Mathlib (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 to Mathlib (this label is automatically managed by a bot) label Oct 11, 2024
Copy link
Collaborator

@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.

Mathlib/Topology/Category/TopCat/Limits/Pullbacks.lean Outdated Show resolved Hide resolved
Mathlib/Topology/Category/TopCat/Limits/Pullbacks.lean Outdated Show resolved Hide resolved
@Vierkantor
Copy link
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
Contributor

kim-em commented Oct 13, 2024

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

@kim-em
Copy link
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
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 to Mathlib (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
Collaborator

grunweg commented Oct 13, 2024

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

@grunweg
Copy link
Collaborator

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 <[email protected]>
@mathlib4-dependent-issues-bot mathlib4-dependent-issues-bot removed the blocked-by-other-PR This PR depends on another PR to Mathlib (this label is automatically managed by a bot) label Oct 13, 2024
@mathlib4-dependent-issues-bot
Copy link
Collaborator

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

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
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
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 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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