fix: use target type's instances in delta deriving#12339
Draft
kim-em wants to merge 1 commit intonightly-with-mathlibfrom
Draft
fix: use target type's instances in delta deriving#12339kim-em wants to merge 1 commit intonightly-with-mathlibfrom
kim-em wants to merge 1 commit intonightly-with-mathlibfrom
Conversation
When deriving an instance for a type alias (e.g., `def ENat := WithTop ℕ`), the instance-implicit class parameters in the derived instance TYPE were using instances synthesized for the UNDERLYING type, not the ALIAS type. This caused a diamond when the alias has its own instance for a dependency class (e.g., AddMonoidWithOne from CommSemiring) that differs from the underlying type's instance (e.g., WithTop.addMonoidWithOne). Instance search would fail because it expected the alias's instance but the derived instance used the underlying's. The fix: after synthesis succeeds, for each instance-implicit class parameter, re-synthesize for the target type and use that instance if it's defeq to what we synthesized for the underlying type. This fixes the `CharZero ℕ∞` diamond in Mathlib where the derived instance was using `WithTop.addMonoidWithOne` instead of the `AddMonoidWithOne` from `CommSemiring ℕ∞`. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
mathlib-nightly-testing bot
pushed a commit
to leanprover-community/batteries
that referenced
this pull request
Feb 6, 2026
mathlib-nightly-testing bot
pushed a commit
to leanprover-community/mathlib4-nightly-testing
that referenced
this pull request
Feb 6, 2026
leanprover-bot
added a commit
to leanprover/reference-manual
that referenced
this pull request
Feb 6, 2026
Collaborator
|
Reference manual CI status:
|
|
Mathlib CI status (docs):
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When deriving an instance for a type alias (e.g.,
def ENat := WithTop ℕ), the instance-implicit class parameters in the derived instance TYPE were using instances synthesized for the UNDERLYING type, not the ALIAS type.This caused a diamond when the alias has its own instance for a dependency class (e.g.,
AddMonoidWithOnefromCommSemiring) that differs from the underlying type's instance (e.g.,WithTop.addMonoidWithOne). Instance search would fail because it expected the alias's instance but the derived instance used the underlying's.The fix: after synthesis succeeds, for each instance-implicit class parameter, re-synthesize for the target type and use that instance if it's defeq to what we synthesized for the underlying type.
Example
Before:
instMyHigherMyAlias : @MyHigher MyAlias instBaseNat→ instance search failsAfter:
instMyHigherMyAlias : @MyHigher MyAlias instBaseMyAlias→ instance search succeedsMotivation
This fixes the
CharZero ℕ∞diamond in Mathlib where the derived instance was usingWithTop.addMonoidWithOneinstead of theAddMonoidWithOnefromCommSemiring ℕ∞, requiringset_option backward.isDefEq.respectTransparency falseworkarounds.🤖 Prepared with Claude Code