Skip to content

fix(ClearExcept): modify so that each isAuxDecl is retained#36723

Open
oliver-butterley wants to merge 18 commits intoleanprover-community:masterfrom
oliver-butterley:oliver-butterley/issue36722
Open

fix(ClearExcept): modify so that each isAuxDecl is retained#36723
oliver-butterley wants to merge 18 commits intoleanprover-community:masterfrom
oliver-butterley:oliver-butterley/issue36722

Conversation

@oliver-butterley
Copy link
Copy Markdown
Collaborator

@oliver-butterley oliver-butterley commented Mar 16, 2026

This PR updates the defintion of ClearExcept to skip any isAuxDecl and adds a test to confirm this behaviour.

The intent is that clear * - only clears user-visible local declarations; hidden auxiliary declarations should be handled by more specific mechanisms when needed.

Motivation

When writing a recursive proof in tactic mode, Lean places the recursive hypothesis in the local context as an auxiliary declaration (LocalDecl.isAuxDecl). This declaration is:

  • Completely invisible in the Infoview (even with all display options enabled)
  • Required for the recursive call to elaborate

Previously the clear * - tactic (Mathlib.Tactic.ClearExcept) iterates over all declarations in the local context and clears everything not in the explicit keep set or a type-class instance. As such it silently clears the recursive hypothesis. While the hypothesis can technically be added to the keep set by name, it is not shown in the Infoview, making it easy to overlook.

In simple examples this is never an issue, the inconvenience was found when working with software verification where the context gets huge and needs clearing for performance and where the recursive goals are closed by automation and so it is easy to overlook the need to explicitly add the name of the theorem to the exclude list.

Zulip discussion: (#general > ClearExcept clears even if isAuxDecl, is this intended?).


Open in Gitpod

@oliver-butterley
Copy link
Copy Markdown
Collaborator Author

bench!

@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 16, 2026

PR summary fb131a4c00

Import changes for modified files

No significant changes to the import graph

Import changes for all files
Files Import difference

Declarations diff

+ instances,
+ myLen
+ myLen_eq_length

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

@mathlib-dependent-issues mathlib-dependent-issues bot added the blocked-by-other-PR This PR depends on another PR (this label is automatically managed by a bot) label Mar 21, 2026
@oliver-butterley oliver-butterley changed the title Fix (ClearExcept): update to skip isAuxDecl Fix (ClearExcept): modify so to retail all isAuxDecl Mar 22, 2026
@oliver-butterley oliver-butterley changed the title Fix (ClearExcept): modify so to retail all isAuxDecl Fix (ClearExcept): modify so that each isAuxDecl is retained Mar 22, 2026
@oliver-butterley oliver-butterley marked this pull request as ready for review March 22, 2026 08:42
@adam84-hub
Copy link
Copy Markdown

Thanks, this looks like the right direction for the recursive-hypothesis case.

One bounded edge-case question before merge: this change now preserves every isAuxDecl unconditionally inside getVarsToClear. Is the intended semantics of clear * - now “always retain auxiliary declarations”, analogous to how instances are preserved, or is the goal specifically to protect recursion-critical aux decls?

I’m asking because the current test clearly covers the motivating recursive example, but the implementation change is slightly broader than that example. If the broader semantics are intended, a short note in the docstring/test rationale may be enough.

@oliver-butterley
Copy link
Copy Markdown
Collaborator Author

Thanks, this looks like the right direction for the recursive-hypothesis case.

One bounded edge-case question before merge: this change now preserves every isAuxDecl unconditionally inside getVarsToClear. Is the intended semantics of clear * - now “always retain auxiliary declarations”, analogous to how instances are preserved, or is the goal specifically to protect recursion-critical aux decls?

I’m asking because the current test clearly covers the motivating recursive example, but the implementation change is slightly broader than that example. If the broader semantics are intended, a short note in the docstring/test rationale may be enough.

Good point! The description of the PR is very focussed on the recursion case in order to be specific however the primary motive is to improve the user friendliness of clear * - in that it only clears stuff which hasn't been chosen to be hidden. The expectation is that if something is chosen to be an auxDecl and so hidden then there should be another mechanism to remove this, like nonrec for recursive auxDecl. About all other varied possibilities, I'm sure I don't have the experience to know them all.

I'll improve the docstring. Any suggestions?

@adam84-hub
Copy link
Copy Markdown

One small wording suggestion, if it matches your intent: maybe make the rationale slightly more explicit than the recursive example.

For example, something like:

"Returns the free variable IDs that can be cleared, excluding those in the preserve list, class instances, and hidden auxiliary declarations (for example recursive hypotheses). The intent is that clear * - only clears user-visible local declarations; hidden auxiliary declarations should be handled by more specific mechanisms when needed."

That seemed to match your explanation that the recursion case is the motivating example, but the intended behavior is broader.

Copy link
Copy Markdown
Collaborator Author

@oliver-butterley oliver-butterley left a comment

Choose a reason for hiding this comment

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

Thanks for all the feedback! Yes, that phrases very well the intent. I wasn't sure where you suggested it would be best to add the clarification. I updated the docstring of the tactic so it is very user-visible since that made most sense to me. Does that seem reasonable?

@grunweg grunweg changed the title Fix (ClearExcept): modify so that each isAuxDecl is retained fix(ClearExcept): modify so that each isAuxDecl is retained Mar 31, 2026
@grunweg
Copy link
Copy Markdown
Contributor

grunweg commented Mar 31, 2026

If I remember correctly, this PR had some zulip discussion about it. Can you also link that in the PR description, so future readers can find it more easily? Thanks!

@grunweg grunweg added the awaiting-author A reviewer has asked the author a question or requested changes. label Mar 31, 2026
mathlib-bors bot pushed a commit that referenced this pull request Mar 31, 2026
It is not necessary, and caused confusion in #36723.
@oliver-butterley
Copy link
Copy Markdown
Collaborator Author

If I remember correctly, this PR had some zulip discussion about it. Can you also link that in the PR description, so future readers can find it more easily? Thanks!

Good thinking. Done!

@oliver-butterley oliver-butterley removed the awaiting-author A reviewer has asked the author a question or requested changes. label Mar 31, 2026
@mathlib-dependent-issues mathlib-dependent-issues bot removed the blocked-by-other-PR This PR depends on another PR (this label is automatically managed by a bot) label Mar 31, 2026
@mathlib-dependent-issues
Copy link
Copy Markdown

aditya-ramabadran pushed a commit to aditya-ramabadran/mathlib4 that referenced this pull request Apr 1, 2026
@adam84-hub
Copy link
Copy Markdown

Yes, this seems reasonable to me. Thanks for making the intended broader semantics explicit in the docstrings and for linking the Zulip discussion as well.

xroblot pushed a commit to xroblot/mathlib4 that referenced this pull request Apr 3, 2026
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants