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

fix: move auxDeclToFullName to LocalContext to fix name (un)resolution #7075

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

Conversation

jrr6
Copy link
Contributor

@jrr6 jrr6 commented Feb 13, 2025

This PR ensures that names suggested by tactics like simp? are not shadowed by auxiliary declarations in the local context and that names of let rec and where declarations are correctly resolved in tactic blocks.

This PR contains the following potentially breaking changes:

  • Moves the auxDeclToFullName map from TermElab.Context to LocalContext.
  • Refactors Lean.Elab.Term.resolveLocalName : Name → TermElabM … to Lean.resolveLocalName [MonadResolveName m] [MonadEnv m] [MonadLCtx m] : Name → m ….
  • Refactors the TermElabM action Lean.Elab.Term.withAuxDecl to a monad-polymorphic action Lean.Meta.withAuxDecl.
  • Adds an optional filter argument to Lean.unresolveNameGlobal.

Closes #6706, closes #7073.

@jrr6 jrr6 force-pushed the unresolve-avoiding-local-metam branch from 2421ebe to 23829df Compare February 13, 2025 17:57
@github-actions github-actions bot added the toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN label Feb 13, 2025
leanprover-community-mathlib4-bot added a commit to leanprover-community/batteries that referenced this pull request Feb 13, 2025
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Feb 13, 2025
@leanprover-community-bot leanprover-community-bot added the breaks-mathlib This is not necessarily a blocker for merging: but there needs to be a plan label Feb 13, 2025
@leanprover-community-bot
Copy link
Collaborator

leanprover-community-bot commented Feb 13, 2025

Mathlib CI status (docs):

@@ -428,18 +432,8 @@ where
candidate := Name.appendCore cmpt candidate
if let [(potentialMatch, _)] ← resolveGlobalName candidate then
if potentialMatch == n₀ then
return some candidate
if (← filter candidate) then
return some candidate
Copy link
Collaborator

Choose a reason for hiding this comment

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

With resolveLocalName now living in MetaM, do you need the filter parameter and the separate avoidingLocals function? Why not simply always avoiding locals, for a simpler API?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did this to remain consistent with our current API, which has both functions (though unresolveNameGlobalAvoidingLocals isn't currently restricted to MetaM). According to #4593, this was done so that Lean.PrettyPrinter.Delaborator.delabConst can continue to use unresolveNameGlobal: in particular, there are certain circumstances in which that delaborator does not use the local-avoiding behavior (e.g., in match patterns or when pretty-printing universes). I'm not entirely certain of the rationale, but that behavior is only possible if we have a separate unresolveNameGlobal function.

Copy link
Collaborator

Choose a reason for hiding this comment

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

So much complexity :-)

Ok, convinced.

At this point we might auxDeclToFullName as well move into LocalContext so that unresolveNameGlobalAvoidingLocals can continue to use MonadLCtx, and resolveLocalName as well. Or an extra method to MonadLCtx. All a bit hard to decide when MetaM is the only instance of MonadLCtx :-D

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I opted for the first approach. This turned out to be a slightly more fiddly refactor than I anticipated, but now that it appears to be working, it happily resolves for free the issue I brought up in the other PR discussion: since local contexts are saved at the point of metavariable creation, the auxiliary-declaration mapping persists when we resume elaboration, so let recs and wheres resolve correctly. Also, since neither resolveLocalName nor unresolveNameGlobalAvoidingLocals depends on MetaM anymore, I've moved both of these functions to Lean.ResolveName.

One question: I noticed that the C++ code references the LocalContext type and some of its associated functions, but I'm not too familiar with that part of the code base. Is it necessary to make any changes to the kernel or runtime as a result of modifying the definition of LocalContext? (Everything seems to build and run fine as is.)

Copy link
Collaborator

Choose a reason for hiding this comment

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

If they reference just the functions, and not hard-coded offsets into it, it should be fine.

@jrr6 jrr6 changed the title fix: move auxDeclToFullName to MetaM to fix name unresolution fix: move auxDeclToFullName to LocalContext to fix name (un)resolution Feb 15, 2025
leanprover-community-mathlib4-bot added a commit to leanprover-community/batteries that referenced this pull request Feb 15, 2025
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Feb 15, 2025
@jrr6 jrr6 force-pushed the unresolve-avoiding-local-metam branch from b10aa38 to 08d18d1 Compare February 17, 2025 22:59
@jrr6 jrr6 marked this pull request as ready for review February 17, 2025 23:00
@jrr6 jrr6 added the changelog-language Language features, tactics, and metaprograms label Feb 17, 2025
leanprover-community-mathlib4-bot added a commit to leanprover-community/batteries that referenced this pull request Feb 17, 2025
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Feb 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaks-mathlib This is not necessarily a blocker for merging: but there needs to be a plan changelog-language Language features, tactics, and metaprograms toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN
Projects
None yet
3 participants