Skip to content

feat: use explicit allowlist instead of transparency bump in whnfMatcher#13363

Draft
nomeata wants to merge 18 commits intonightly-with-mathlibfrom
joachim/matchwhnf
Draft

feat: use explicit allowlist instead of transparency bump in whnfMatcher#13363
nomeata wants to merge 18 commits intonightly-with-mathlibfrom
joachim/matchwhnf

Conversation

@nomeata
Copy link
Copy Markdown
Collaborator

@nomeata nomeata commented Apr 10, 2026

This PR replaces the transparency bump from .reducible to .instances in whnfMatcher with an explicit, minimal allowlist in canUnfoldAtMatcher. Previously, whnfMatcher would unfold all implicitReducible definitions and all fromClass projections when reducing match discriminants. This made it impossible to mark definitions as implicit_reducible without silently affecting match reduction behavior.

The new canUnfoldAtMatcher delegates to canUnfoldDefault first (respecting the ambient transparency), then allows unfolding of match_pattern-attributed definitions, and finally checks a small explicit allowlist: OfNat.ofNat (for numeric literals) and NatCast.natCast (for ↑m coercions, pervasive in Int proofs).

All other definitions that were previously unfolded via the blanket implicitReducible/fromClass checks have been removed from the allowlist after verifying they are not needed, or the proofs that depended on them have been updated:

  • BVDecide RupAddResult proofs: add Fin.getElem_fin to normalize Fin-indexed array access before clearUnit unfolds
  • PRange NatLemmas/IntLemmas: add explicit Least?.least? and UpwardEnumerable.succ? to simp calls
  • mvcgenTutorial test: add pure to dsimp only call
  • unfold1 test: update expected output (match discriminant no longer over-unfolds)
  • TakeDrop proof: use length, drop_succ_cons instead of drop
  • Derived BEq instances: use Nat.decEq instead of generic decEq (extracted to separate PR refactor: use Nat.decEq in derived BEq instances #13390)

Previously, `whnfMatcher` would increase the transparency from `.reducible`
to `.instances` when reducing match expressions. This caused surprising
over-unfolding when `implicit_reducible` definitions appeared in match
discriminants, as reported in the discussion around `whnfMatcher`'s TODO
comment.

This PR removes the `withTransparency .instances` call so that `whnfMatcher`
now runs at the ambient transparency. To ensure match discriminants involving
class projections (e.g. `OfNat.ofNat`, `NatCast.natCast`, `Pure.pure`) still
reduce to expose constructors, `canUnfoldAtMatcher` now allows unfolding any
class projection (`projInfo.fromClass`).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@nomeata nomeata added the changelog-language Language features and metaprograms label Apr 10, 2026
@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 Apr 10, 2026
@leanprover-bot
Copy link
Copy Markdown
Collaborator

Reference manual CI status:

  • ❗ Reference manual CI can not be attempted yet, as the nightly-testing-2026-04-07 tag does not exist there yet. We will retry when you push more commits. If you rebase your branch onto nightly-with-manual, reference manual CI should run now. You can force reference manual CI using the force-manual-ci label. (2026-04-10 16:37:34)

mathlib-nightly-testing bot pushed a commit to leanprover-community/batteries that referenced this pull request Apr 10, 2026
@github-actions github-actions bot added the mathlib4-nightly-available A branch for this PR exists at leanprover-community/mathlib4-nightly-testing:lean-pr-testing-NNNN label Apr 10, 2026
mathlib-nightly-testing bot pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Apr 10, 2026
@mathlib-lean-pr-testing mathlib-lean-pr-testing bot added the builds-mathlib CI has verified that Mathlib builds against this PR label Apr 10, 2026
@mathlib-lean-pr-testing
Copy link
Copy Markdown

Mathlib CI status (docs):

@nomeata nomeata marked this pull request as ready for review April 10, 2026 18:12
@nomeata nomeata marked this pull request as draft April 10, 2026 18:55
…fMatcher`

Replace the `implicitReducible` and `fromClass` checks in
`canUnfoldAtMatcher` with an explicit allowlist of class projections and
instances. Delegate to `canUnfoldDefault` first so that
`withCanUnfoldPred` does not accidentally block sub-calls at different
transparencies.

This ensures that marking more definitions as `implicit_reducible` in the
future will not silently change which terms get unfolded in match
discriminants.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
mathlib-nightly-testing bot pushed a commit to leanprover-community/batteries that referenced this pull request Apr 13, 2026
mathlib-nightly-testing bot pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Apr 13, 2026
@mathlib-lean-pr-testing mathlib-lean-pr-testing bot added breaks-mathlib This is not necessarily a blocker for merging: but there needs to be a plan and removed builds-mathlib CI has verified that Mathlib builds against this PR labels Apr 13, 2026
@mathlib-lean-pr-testing
Copy link
Copy Markdown

Mathlib CI status (docs):

nomeata and others added 11 commits April 13, 2026 12:22
Remove entries that are not needed for tests or stage 2 to pass:
Std.Stream.next?, MonadStateOf.get, MonadState.get, Bind.bind,
Ord.compare, LT.lt, LE.le, HAppend.hAppend, Append.append,
GetElem?.getElem?, BEq.beq, HSub.hSub, Sub.sub, HMod.hMod, Mod.mod.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Remove: instDecidableEqBool, instBEqOfDecidableEq, String.decEq,
List.hasDecEq, Char.ofNat, Char.ofNatAux.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…` allowlist

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The Nat.add → instAddNat → instHAdd chain is not needed because these
definitions are already handled by canUnfoldDefault or matchPattern
attributes.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…eeded

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Use `Nat.decEq` (which is reducible) directly instead of the generic
`decEq` (which is semireducible) when comparing constructor indices in
derived BEq instances. This removes the need for `decEq`,
`instDecidableEqNat`, and `Nat.decEq` in the `canUnfoldAtMatcher`
allowlist.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The `unfold` tactic no longer over-unfolds `2 * x` to `Nat.mul 2 x` in
match discriminants. Update `unfold1` test expected output accordingly.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
mathlib-nightly-testing bot pushed a commit to leanprover-community/batteries that referenced this pull request Apr 13, 2026
mathlib-nightly-testing bot pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Apr 13, 2026
@mathlib-lean-pr-testing
Copy link
Copy Markdown

Mathlib CI status (docs):

mathlib-nightly-testing bot pushed a commit to leanprover-community/batteries that referenced this pull request Apr 13, 2026
nomeata and others added 4 commits April 13, 2026 16:34
…` allowlist

Without Pure.pure, the mvcgenTutorial test needs an explicit
`dsimp only [pure]` before `grind`. Without the Nat.mul chain,
`unfold isEven` no longer reduces the match on `2 * (x + 1)` in
unfold1, but the proof still works by definitional equality.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add explicit `Least?.least?` to the `simp` calls in the five PRange
Nat lemmas that need it to unfold `least?` to `some 0`.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…owlist

Add explicit `UpwardEnumerable.succ?` to the `simp` calls in the two
PRange Int lemmas that need it.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add `Fin.getElem_fin` to the `simp` calls in BVDecide's RupAddResult
proofs so that `units[j]` (Fin indexing) is normalized to `units[↑j]`
(Nat indexing), allowing the induction hypotheses to match before
`clearUnit` unfolds.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@nomeata nomeata changed the base branch from master to nightly-with-mathlib April 13, 2026 21:17
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@nomeata nomeata changed the title refactor: stop bumping transparency to .instances in whnfMatcher feat: use explicit allowlist instead of transparency bump in whnfMatcher Apr 13, 2026
mathlib-nightly-testing bot pushed a commit to leanprover-community/batteries that referenced this pull request Apr 13, 2026
mathlib-nightly-testing bot pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Apr 13, 2026
@mathlib-lean-pr-testing
Copy link
Copy Markdown

Mathlib CI status (docs):

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 and metaprograms mathlib4-nightly-available A branch for this PR exists at leanprover-community/mathlib4-nightly-testing:lean-pr-testing-NNNN 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.

2 participants