Skip to content

[Merged by Bors] - chore: deduplicate "transform at location" methods - #27562

Closed
hrmacbeth wants to merge 33 commits into
leanprover-community:masterfrom
hrmacbeth:at-location
Closed

hrmacbeth wants to merge 33 commits into
leanprover-community:masterfrom
hrmacbeth:at-location

Conversation

@hrmacbeth

@hrmacbeth hrmacbeth commented Jul 27, 2025 •

Copy link
Copy Markdown
Member

Many metaprograms have the following general structure: the input is an expression e and the output is a new expression e', together with a proof that e = e'. Such a metaprogram produces a variety of tactics: it may be used to modify the goal, a specified hypothesis, or (via Tactic.Location) a combination of these.

Roughly the same code for this is repeated throughout the library, for the tactics ring_nf, abel_nf, norm_num, push_neg and reduce_mod_char; possibly it originates from the ring_nf version, #552.

This PR extracts this repeated code to stand-alone functions Mathlib.Util.AtLocation and Mathlib.Util.AtNondepPropLocation.


Open in Gitpod

@hrmacbeth hrmacbeth added the t-meta Tactics, attributes or user commands label Jul 27, 2025
@github-actions github-actions Bot added the large-import Automatically added label for PRs with a significant increase in transitive imports label Jul 27, 2025
@github-actions

github-actions Bot commented Jul 27, 2025 •

Copy link
Copy Markdown

PR summary 3e97beff8a

Import changes exceeding 2%

% File
+2.50% Mathlib.Tactic.Push

Import changes for modified files

Dependency changes

File Base Count Head Count Change
Mathlib.Tactic.Push 80 82 +2 (+2.50%)
Mathlib.Tactic.NormNum.Core 230 232 +2 (+0.87%)
Mathlib.Tactic.Abel 417 419 +2 (+0.48%)
Mathlib.Tactic.Ring.RingNF 494 496 +2 (+0.40%)
Mathlib.Analysis.Convex.Piecewise 1058 1060 +2 (+0.19%)
Mathlib.Tactic.ReduceModChar 1328 1330 +2 (+0.15%)
Import changes for all files
Files Import difference
There are 6238 files with changed transitive imports taking up over 268856 characters: this is too many to display!
You can run scripts/import_trans_difference.sh all locally to see the whole output.

Declarations diff

+ Lean.Elab.Tactic.withNondepPropLocation
+ inductive_proof
+ test
+ transformAtLocalDecl
+ transformAtLocation
+ transformAtNondepPropLocation
+ transformAtTarget
- abelNFLocalDecl
- abelNFTarget
- normNumAt
- pushNegLocalDecl
- pushNegTarget
- ringNFLocalDecl
- ringNFTarget

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.


No changes to technical debt.

You can run this locally as

./scripts/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).

Comment thread MathlibTest/abel.lean Resolved
@grunweg

grunweg commented Jul 27, 2025

Copy link
Copy Markdown
Contributor

This PR looks really nice. I can hopefully take a detailed look on Tuesday or Wednesday.

@hrmacbeth
hrmacbeth marked this pull request as ready for review July 28, 2025 00:54
@grunweg grunweg self-assigned this Aug 5, 2025
Comment thread Mathlib/Tactic/Abel.lean
/-- Use `abel_nf` to rewrite the main goal. -/
def abelNFTarget (s : IO.Ref AtomM.State) (cfg : AbelNF.Config) : TacticM Unit := withMainContext do
let goal ← getMainGoal
let tgt ← withReducible goal.getType'

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.

Flagging for a final look: the new code does instantiateMVars (instead of withReducible).

Comment thread Mathlib/Tactic/Abel.lean Resolved
Comment thread Mathlib/Tactic/Abel.lean Resolved
Comment thread Mathlib/Tactic/Push.lean Resolved
Comment thread Mathlib/Tactic/Push.lean Resolved
Comment thread Mathlib/Tactic/ReduceModChar.lean Resolved
Comment thread Mathlib/Tactic/NormNum/Core.lean Resolved
* `simplifyTarget`: true if the target is selected in the location argument
* `useSimp`: true if we used `norm_num` instead of `norm_num1`
-/
def normNumAt (g : MVarId) (ctx : Simp.Context) (fvarIdsToSimp : Array FVarId)

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 change looks broadly fine, but I don't feel confident signing off on all of it. Another pair of eyes would be appreciated.

Comment thread MathlibTest/norm_num.lean Resolved
@grunweg

grunweg commented Aug 5, 2025

Copy link
Copy Markdown
Contributor

I just finished a detailed review: 90% of the PR are straightforward; for the last 10%, I'd need another pair of eyes. (I asked a bunch of naive questions above.) @fldutchmann If you're in the office tomorrow, would you like to look over this together?

On a higher level, this deduplication is really nice (and, one may argue, long overdue). Thanks a lot for doing this often-invisible work!

@grunweg grunweg left a comment

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.

Making progress clarifying the review comments; to be continued!

Comment thread Mathlib/Tactic/Abel.lean Resolved
Comment thread Mathlib/Tactic/Push.lean Resolved
Comment thread Mathlib/Tactic/Push.lean Resolved
Comment thread Mathlib/Tactic/Push.lean Resolved
@grunweg

grunweg commented Aug 7, 2025

Copy link
Copy Markdown
Contributor

Side note: the new file should import Mathlib.Init --- and the lake exe lint-style step should have failed because of that. (I'll investigate the latter later.)

Comment thread Mathlib/Util/AtLocation.lean Resolved
Comment thread Mathlib/Tactic/NormNum/Core.lean Resolved
@grunweg grunweg added the awaiting-author Reply -awaiting-author to remove the label on your PR once you have addressed all comments. label Aug 7, 2025
Comment thread MathlibTest/abel.lean Resolved
@kckennylau

kckennylau commented Aug 15, 2025 •

Copy link
Copy Markdown
Collaborator

I notice that the idiom (loc.map expandLocation).getD (.targets #[] true) is repeated 5 times in the various tactics you modified. Would it make more sense for atLocation to take Option Location instead of Location? Or even take a location? syntax? Or perhaps make another atLocationStx function that takes the syntax and then passes it on to atLocation?

@hrmacbeth

Copy link
Copy Markdown
Member Author

I notice that the idiom (loc.map expandLocation).getD (.targets #[] true) is repeated 5 times in the various tactics you modified. Would it make more sense for atLocation to take Option Location instead of Location? Or even take a location? syntax? Or perhaps make another atLocationStx function that takes the syntax and then passes it on to atLocation?

There's an alternative spelling, expandOptLocation (Lean.mkOptionalNode loc). But indeed, maybe it would be nice to have a single function for this rather than having to combine two. Do you want to make that? It's orthogonal to this PR.

Comment thread Mathlib/Util/AtLocation.lean Resolved
Comment thread Mathlib/Util/AtLocation.lean Resolved
Comment thread Mathlib/Util/AtLocation.lean Resolved
Comment thread Mathlib/Util/AtLocation.lean Outdated
Comment on lines +45 to +55
/-- A metaprogram whose input is an expression `e` and whose output is a new expression `e'`,
together with a proof that `e = e'`.

The metaprogram may optionally depend on `ctx`, a `Simp.Context`; if it does, then when the
metaprogram is run on a local hypothesis `h`, the context `ctx` will be modified to forget `h`. -/
inductive SimprocLike
/-- Default case: a metaprogram which does not depend on a `Simp.Context`. -/
| noContext (m : Expr → MetaM Simp.Result)
/-- A metaprogram which depends on a `Simp.Context`. In this case we must also provide the
`Simp.Context` to use it with; this will be modified at each hypothesis `h` to forget `h`. -/
| withContext (ctx : Simp.Context) (m : Simp.Context → Expr → MetaM Simp.Result)

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.

Why have this inductive? Why not just always carry the context, and either the callers can pass a function that ignores the context, or provide a constructor that does that for you?

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'm not too worried here, so don't change if you think it's best. But it feels strange to me, and if this could be sorted out cleanly I'd aspire to upstreaming this.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

OK, done!

@kim-em

kim-em commented Aug 20, 2025

Copy link
Copy Markdown
Contributor

bors merge

@ghost ghost added the ready-to-merge This PR has been sent to bors. label Aug 20, 2025
mathlib-bors Bot pushed a commit that referenced this pull request Aug 20, 2025
Many metaprograms have the following general structure: the input is an expression `e` and the output is a new expression `e'`, together with a proof that `e = e'`. Such a metaprogram produces a variety of tactics: it may be used to modify the goal, a specified hypothesis, or (via `Tactic.Location`) a combination of these.

Roughly the same code for this is repeated throughout the library, for the tactics `ring_nf`, `abel_nf`, `norm_num`, `push_neg` and `reduce_mod_char`; possibly it originates from the `ring_nf` version, #552.

This PR extracts this repeated code to stand-alone functions `Mathlib.Util.AtLocation` and `Mathlib.Util.AtNondepPropLocation`.
@mathlib-bors

mathlib-bors Bot commented Aug 20, 2025

Copy link
Copy Markdown
Contributor

Pull request successfully merged into master.

Build succeeded:

@mathlib-bors mathlib-bors Bot changed the title chore: deduplicate "transform at location" methods [Merged by Bors] - chore: deduplicate "transform at location" methods Aug 20, 2025
@mathlib-bors mathlib-bors Bot closed this Aug 20, 2025
@dwrensha

dwrensha commented Aug 20, 2025 •

Copy link
Copy Markdown
Member

I'm not sure whether this is an intended consequence, but this PR broke proofs such as the following:

import Mathlib.Tactic

example (h : 0 = 1) (h2 : 0 = 2) : False := by
  norm_num at h h2 -- error: No goals to be solved

-- slightly more realistic example
example {x : Nat} (hx : x < 2) (h : x = 1) (h2 : x = 2) : False := by
  interval_cases x <;> norm_num at h h2 -- error: No goals to be solved

It seems that if norm_num at h succeeds in closing the proof, then norm_num at h h2 is now an error. These were not errors before this PR landed.

pfaffelh pushed a commit to pfaffelh/mathlib4 that referenced this pull request Aug 20, 2025
…ity#27562)

Many metaprograms have the following general structure: the input is an expression `e` and the output is a new expression `e'`, together with a proof that `e = e'`. Such a metaprogram produces a variety of tactics: it may be used to modify the goal, a specified hypothesis, or (via `Tactic.Location`) a combination of these.

Roughly the same code for this is repeated throughout the library, for the tactics `ring_nf`, `abel_nf`, `norm_num`, `push_neg` and `reduce_mod_char`; possibly it originates from the `ring_nf` version, leanprover-community#552.

This PR extracts this repeated code to stand-alone functions `Mathlib.Util.AtLocation` and `Mathlib.Util.AtNondepPropLocation`.
@hrmacbeth

hrmacbeth commented Aug 20, 2025 •

Copy link
Copy Markdown
Member Author

@dwrensha Oops! On the one hand, yes, this is unintended. On the other hand, given that the logic is this easy to mess up, it's probably all the better that we standardize it in a single function ...

Do you mind opening an issue? I'll try to get to it soon, unless someone else does -- a quick impression is that maybe this requires moving away from the implementation on top of Lean.Elab.Tactic.withLocation.

Another question: what should be done if the tactic fails at one of the specified hypotheses, but also closes the goal (by reducing to False) at another?

@dwrensha

Copy link
Copy Markdown
Member

I expect the norm_num behavior to match the simp behavior. The following continues to work without error:

import Mathlib.Tactic

example (h : 0 = 1) (h2 : 0 = 2) : False := by
  simp at h h2

-- slightly more realistic example
example {x : Nat} (hx : x < 2) (h : x = 1) (h2 : x = 2) : False := by
  interval_cases x <;> simp at h h2

I'll open an issue.

@dwrensha

Copy link
Copy Markdown
Member

#28703

Paul-Lez pushed a commit to Paul-Lez/mathlib4 that referenced this pull request Aug 23, 2025
…ity#27562)

Many metaprograms have the following general structure: the input is an expression `e` and the output is a new expression `e'`, together with a proof that `e = e'`. Such a metaprogram produces a variety of tactics: it may be used to modify the goal, a specified hypothesis, or (via `Tactic.Location`) a combination of these.

Roughly the same code for this is repeated throughout the library, for the tactics `ring_nf`, `abel_nf`, `norm_num`, `push_neg` and `reduce_mod_char`; possibly it originates from the `ring_nf` version, leanprover-community#552.

This PR extracts this repeated code to stand-alone functions `Mathlib.Util.AtLocation` and `Mathlib.Util.AtNondepPropLocation`.
pechersky pushed a commit to pechersky/mathlib4 that referenced this pull request Aug 25, 2025
…ity#27562)

Many metaprograms have the following general structure: the input is an expression `e` and the output is a new expression `e'`, together with a proof that `e = e'`. Such a metaprogram produces a variety of tactics: it may be used to modify the goal, a specified hypothesis, or (via `Tactic.Location`) a combination of these.

Roughly the same code for this is repeated throughout the library, for the tactics `ring_nf`, `abel_nf`, `norm_num`, `push_neg` and `reduce_mod_char`; possibly it originates from the `ring_nf` version, leanprover-community#552.

This PR extracts this repeated code to stand-alone functions `Mathlib.Util.AtLocation` and `Mathlib.Util.AtNondepPropLocation`.
pfaffelh pushed a commit to pfaffelh/mathlib4 that referenced this pull request Jan 9, 2026
…ity#27562)

Many metaprograms have the following general structure: the input is an expression `e` and the output is a new expression `e'`, together with a proof that `e = e'`. Such a metaprogram produces a variety of tactics: it may be used to modify the goal, a specified hypothesis, or (via `Tactic.Location`) a combination of these.

Roughly the same code for this is repeated throughout the library, for the tactics `ring_nf`, `abel_nf`, `norm_num`, `push_neg` and `reduce_mod_char`; possibly it originates from the `ring_nf` version, leanprover-community#552.

This PR extracts this repeated code to stand-alone functions `Mathlib.Util.AtLocation` and `Mathlib.Util.AtNondepPropLocation`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

large-import Automatically added label for PRs with a significant increase in transitive imports ready-to-merge This PR has been sent to bors. t-meta Tactics, attributes or user commands

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants