Skip to content

fix: use target type's instances in delta deriving#12339

Draft
kim-em wants to merge 1 commit intonightly-with-mathlibfrom
fix-deriving-diamond
Draft

fix: use target type's instances in delta deriving#12339
kim-em wants to merge 1 commit intonightly-with-mathlibfrom
fix-deriving-diamond

Conversation

@kim-em
Copy link
Collaborator

@kim-em kim-em commented Feb 6, 2026

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

Example

class MyBase (α : Type) where value : Nat := 42
class MyHigher (α : Type) [MyBase α] : Prop where prop : True

instance instBaseNat : MyBase Nat := {}
def MyAlias := Nat
instance instBaseMyAlias : MyBase MyAlias := {}  -- Different expression, but defeq

instance instHigherNat : MyHigher Nat where prop := trivial
deriving instance MyHigher for MyAlias

Before: instMyHigherMyAlias : @MyHigher MyAlias instBaseNat → instance search fails
After: instMyHigherMyAlias : @MyHigher MyAlias instBaseMyAlias → instance search succeeds

Motivation

This fixes the CharZero ℕ∞ diamond in Mathlib where the derived instance was using WithTop.addMonoidWithOne instead of the AddMonoidWithOne from CommSemiring ℕ∞, requiring set_option backward.isDefEq.respectTransparency false workarounds.

🤖 Prepared with Claude Code

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>
@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 6, 2026
mathlib-nightly-testing bot pushed a commit to leanprover-community/batteries that referenced this pull request Feb 6, 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 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
@leanprover-bot leanprover-bot added the builds-manual CI has verified that the Lean Language Reference builds against this PR label Feb 6, 2026
@leanprover-bot
Copy link
Collaborator

leanprover-bot commented Feb 6, 2026

Reference manual CI status:

@mathlib-lean-pr-testing mathlib-lean-pr-testing bot added the builds-mathlib CI has verified that Mathlib builds against this PR label Feb 6, 2026
@mathlib-lean-pr-testing
Copy link

Mathlib CI status (docs):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

builds-manual CI has verified that the Lean Language Reference builds against this PR builds-mathlib CI has verified that Mathlib builds against this PR 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