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: binderNameHint #6947

Merged
merged 13 commits into from
Feb 6, 2025
Merged

feat: binderNameHint #6947

merged 13 commits into from
Feb 6, 2025

Conversation

nomeata
Copy link
Collaborator

@nomeata nomeata commented Feb 4, 2025

This PR adds the binderNameHint gadget. It can be used in rewrite and simp rules to preserve a user-provided name where possible.

The expression binderNameHint v binder e defined to be e.

If it is used on the right-hand side of an equation that is applied by a tactic like rw or simp,
and v is a local variable, and binder is an expression that (after beta-reduction) is a binder
(so fun w => … or ∀ w, …), then it will rename v to the name used in the binder, and remove
the binderNameHint.

A typical use of this gadget would be as follows; the gadget ensures that after rewriting, the local
variable is still name, and not x:

theorem all_eq_not_any_not (l : List α) (p : α → Bool) :
    l.all p = !l.any fun x => binderNameHint x p (!p x) := sorry

example (names : List String) : names.all (fun name => "Waldo".isPrefixOf name) = true := by
  rw [all_eq_not_any_not]
  -- ⊢ (!names.any fun name => !"Waldo".isPrefixOf name) = true

This gadget is supported by simp, dsimp and rw in the right-hand-side of an equation, but not
in hypotheses or by other tactics.

@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 4, 2025
leanprover-community-mathlib4-bot added a commit to leanprover-community/batteries that referenced this pull request Feb 4, 2025
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Feb 4, 2025
@leanprover-community-bot
Copy link
Collaborator

leanprover-community-bot commented Feb 4, 2025

Mathlib CI status (docs):

  • ✅ Mathlib branch lean-pr-testing-6947 has successfully built against this PR. (2025-02-04 12:46:57) View Log
  • ✅ Mathlib branch lean-pr-testing-6947 has successfully built against this PR. (2025-02-04 14:57:50) View Log
  • ✅ Mathlib branch lean-pr-testing-6947 has successfully built against this PR. (2025-02-04 15:48:23) View Log
  • ✅ Mathlib branch lean-pr-testing-6947 has successfully built against this PR. (2025-02-04 16:43:23) View Log
  • ❗ Batteries CI can not be attempted yet, as the nightly-testing-2025-02-05 tag does not exist there yet. We will retry when you push more commits. If you rebase your branch onto nightly-with-mathlib, Batteries CI should run now. (2025-02-05 11:21:14)
  • ✅ Mathlib branch lean-pr-testing-6947 has successfully built against this PR. (2025-02-06 10:05:30) View Log
  • ✅ Mathlib branch lean-pr-testing-6947 has successfully built against this PR. (2025-02-06 11:23:47) View Log

@leanprover-community-bot leanprover-community-bot added the builds-mathlib CI has verified that Mathlib builds against this PR label Feb 4, 2025
leanprover-community-mathlib4-bot added a commit to leanprover-community/batteries that referenced this pull request Feb 4, 2025
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Feb 4, 2025
@nomeata nomeata added the changelog-language Language features, tactics, and metaprograms label Feb 4, 2025
leanprover-community-mathlib4-bot added a commit to leanprover-community/batteries that referenced this pull request Feb 4, 2025
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Feb 4, 2025
@github-actions github-actions bot temporarily deployed to lean-lang.org/lean4/doc February 4, 2025 15:40 Inactive
leanprover-community-mathlib4-bot added a commit to leanprover-community/batteries that referenced this pull request Feb 4, 2025
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Feb 4, 2025
@nomeata nomeata marked this pull request as ready for review February 5, 2025 10:59
@nomeata nomeata requested a review from leodemoura as a code owner February 5, 2025 10:59
@github-actions github-actions bot temporarily deployed to lean-lang.org/lean4/doc February 5, 2025 11:10 Inactive
@github-actions github-actions bot temporarily deployed to lean-lang.org/lean4/doc February 6, 2025 09:01 Inactive
leanprover-community-mathlib4-bot added a commit to leanprover-community/batteries that referenced this pull request Feb 6, 2025
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Feb 6, 2025
@github-actions github-actions bot temporarily deployed to lean-lang.org/lean4/doc February 6, 2025 10:28 Inactive
leanprover-community-mathlib4-bot added a commit to leanprover-community/batteries that referenced this pull request Feb 6, 2025
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Feb 6, 2025
@nomeata nomeata added this pull request to the merge queue Feb 6, 2025
Merged via the queue into master with commit dc001a0 Feb 6, 2025
16 checks passed
@eric-wieser
Copy link
Contributor

Should this be legal on the LHS too, to allow reverse rewriting?

@nomeata
Copy link
Collaborator Author

nomeata commented Feb 12, 2025

Hmm, for that we’d have to make sure it does not get in the way of matching, so probably has to be marked as reducible? One would have to investigate if that works well first.

in hypotheses or by other tactics.
-/
@[simp ↓]
def binderNameHint {α : Sort u} {β : Sort v} {γ : Sort w} (v : α) (binder : β) (e : γ) : γ := e
Copy link
Contributor

@eric-wieser eric-wieser Feb 12, 2025

Choose a reason for hiding this comment

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

I wonder if this ought to be reducible, so that defeq matching on the types of theorems (for instance, perhaps loogle and exact?) does not get tripped up by it?

More extremely, perhaps it should be a macro that produces an Expr.mdata, so that unaware tactics can easily strip it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Right, making this reducible is worth a try.

I don’t think a mdata can store other expressions, so not sure if this would work better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
builds-mathlib CI has verified that Mathlib builds against this PR 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
Development

Successfully merging this pull request may close these issues.

3 participants