lean4-htt/src/Lean
Kim Morrison 5c7a508e21
fix: use target type's instances in delta deriving (#12339)
This PR fixes a diamond problem in delta deriving where
instance-implicit class parameters in the derived instance type were
using instances synthesized for the underlying type, not the alias type.

When deriving an instance for a type alias (e.g., `def ENat := WithTop
ℕ`), 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

```lean
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 under #12179 where the
derived instance was using `WithTop.addMonoidWithOne` instead of the
`AddMonoidWithOne` from `CommSemiring ℕ∞`.

🤖 Prepared with Claude Code

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-16 00:35:59 +00:00
..
Compiler fix: type class resolution cache (#12286) 2026-02-15 03:07:15 +00:00
Data chore: shake core (#12276) 2026-02-05 09:10:32 +00:00
DocString fix: error messages from Verso docstring parser (#12372) 2026-02-07 07:49:06 +00:00
Elab fix: use target type's instances in delta deriving (#12339) 2026-02-16 00:35:59 +00:00
Language chore: shake core (#12276) 2026-02-05 09:10:32 +00:00
LibrarySuggestions chore: shake core (#12276) 2026-02-05 09:10:32 +00:00
Linter chore: shake core (#12276) 2026-02-05 09:10:32 +00:00
Meta chore: revert "feat: add higher-order Miller pattern support in e-matching (#12483)" (#12492) 2026-02-15 09:53:55 +00:00
Parser fix: parse indented Verso docstrings specially (#12331) 2026-02-06 21:03:56 +00:00
ParserCompiler chore: remove redundant imports in core (#10750) 2025-10-16 20:27:46 +00:00
PrettyPrinter chore: shake core (#12276) 2026-02-05 09:10:32 +00:00
Server fix: set data? field in eager code actions (#12332) 2026-02-06 08:57:27 +00:00
Util perf: put the compiler off the critical path (#12335) 2026-02-05 20:39:11 +00:00
Widget chore: shake core (#12276) 2026-02-05 09:10:32 +00:00
AddDecl.lean refactor: move getOriginalConstKind? into its own module to avoid future import cycle (#12265) 2026-02-01 16:18:51 +00:00
Attributes.lean fix: run @[init] declarations in declaration order (#12221) 2026-01-29 15:32:56 +00:00
AuxRecursor.lean perf: add introSubstEq shortcut (#12190) 2026-01-28 12:33:14 +00:00
BuiltinDocAttr.lean chore: remove redundant imports in core (#10750) 2025-10-16 20:27:46 +00:00
Class.lean feat: add [univ_out_params] (#12423) 2026-02-11 15:42:00 +00:00
Compiler.lean chore: remove public section from end of files (#10684) 2025-10-06 13:30:48 +00:00
CoreM.lean chore: do not rely on Name.lt for ordering fvars in acLt (#12306) 2026-02-08 14:25:31 +00:00
Data.lean chore: remove unused Lean.Data.Xml (#12302) 2026-02-04 09:54:25 +00:00
Declaration.lean chore: shake core (#12276) 2026-02-05 09:10:32 +00:00
DeclarationRange.lean chore: remove redundant imports in core (#10750) 2025-10-16 20:27:46 +00:00
DefEqAttrib.lean
DocString.lean
Elab.lean feat: add #import_path, assert_not_exists, assert_not_imported commands (#11726) 2025-12-19 04:09:33 +00:00
EnvExtension.lean fix: avoid panic in TagDeclarationExtension.tag on partial elaboration (#11882) 2026-01-05 00:36:58 +00:00
Environment.lean doc: improve comments around asyncMayModify (#12270) 2026-02-05 12:43:02 +00:00
ErrorExplanation.lean chore: shake core (#12276) 2026-02-05 09:10:32 +00:00
Exception.lean refactor: move error explanation text to the manual (#11688) 2025-12-26 17:14:58 +00:00
Expr.lean refactor: reset reuse pass to LCNF (#12315) 2026-02-05 15:54:46 +00:00
ExtraModUses.lean feat: shake: make Mathlib-ready (#11496) 2025-12-05 09:37:58 +00:00
HeadIndex.lean
Hygiene.lean chore: do not set unused Option.Decl.group (#11307) 2025-11-21 16:44:38 +00:00
IdentifierSuggestion.lean chore: shake core (#12276) 2026-02-05 09:10:32 +00:00
ImportingFlag.lean
InternalExceptionId.lean chore: shake core (#12276) 2026-02-05 09:10:32 +00:00
KeyedDeclsAttribute.lean fix: some ExtraModUses (#10620) 2025-10-03 15:50:40 +00:00
LabelAttribute.lean chore: shake core (#12276) 2026-02-05 09:10:32 +00:00
Level.lean chore: shake core (#12276) 2026-02-05 09:10:32 +00:00
LibrarySuggestions.lean fix: make library suggestions available in module files (#11373) 2025-11-26 05:39:27 +00:00
Linter.lean feat: lint coercions that are deprecated or banned in core (#11511) 2025-12-12 15:09:13 +00:00
LoadDynlib.lean chore: shake core (#12276) 2026-02-05 09:10:32 +00:00
LocalContext.lean chore: shake core (#12276) 2026-02-05 09:10:32 +00:00
Log.lean refactor: move error explanation text to the manual (#11688) 2025-12-26 17:14:58 +00:00
Message.lean chore: shake core (#12276) 2026-02-05 09:10:32 +00:00
Meta.lean refactor: have telescope support (#11914) 2026-01-06 19:20:25 +00:00
MetavarContext.lean chore: shake core (#12276) 2026-02-05 09:10:32 +00:00
Modifiers.lean chore: more module system fixes and refinements for finishing batteries port (#10819) 2025-10-21 08:19:50 +00:00
MonadEnv.lean feat: grind support for .ctorIdx (#11652) 2025-12-13 13:32:19 +00:00
Namespace.lean
OriginalConstKind.lean refactor: move getOriginalConstKind? into its own module to avoid future import cycle (#12265) 2026-02-01 16:18:51 +00:00
Parser.lean feat: hexnum parser (#10716) 2025-10-08 21:12:03 +00:00
ParserCompiler.lean chore: shake core (#12276) 2026-02-05 09:10:32 +00:00
PrettyPrinter.lean chore: shake core (#12276) 2026-02-05 09:10:32 +00:00
PrivateName.lean
ProjFns.lean chore: delete obsolete C++ file (#11561) 2025-12-09 15:47:54 +00:00
ReducibilityAttrs.lean chore: shake core (#12276) 2026-02-05 09:10:32 +00:00
Replay.lean chore: remove redundant imports in core (#10750) 2025-10-16 20:27:46 +00:00
ReservedNameAction.lean
ResolveName.lean chore: more module system fixes and refinements for finishing batteries port (#10819) 2025-10-21 08:19:50 +00:00
Runtime.lean
ScopedEnvExtension.lean chore: remove redundant imports in core (#10750) 2025-10-16 20:27:46 +00:00
Server.lean feat: revamp server logging (#10787) 2025-10-28 16:26:59 +00:00
Setup.lean fix: symbol clashes between packages (#11082) 2025-11-19 02:24:44 +00:00
Shell.lean chore: shake core (#12276) 2026-02-05 09:10:32 +00:00
Structure.lean chore: shake core (#12276) 2026-02-05 09:10:32 +00:00
SubExpr.lean chore: shake core (#12276) 2026-02-05 09:10:32 +00:00
Syntax.lean chore: shake core (#12276) 2026-02-05 09:10:32 +00:00
ToExpr.lean feat: support for Rat scientific literals (#10961) 2025-10-26 02:05:26 +00:00
ToLevel.lean
Util.lean feat: instantiate tactic parameter optimizer (#10916) 2025-10-23 01:22:33 +00:00
Widget.lean chore: remove public section from end of files (#10684) 2025-10-06 13:30:48 +00:00