lean4-htt/src/Lean/Compiler
Sebastian Ullrich 88b746dd48 feat: unfold and rewrap instances in inferInstanceAs and deriving
This PR adjusts the results of `inferInstanceAs` and the `def` `deriving` handler to conform to recently strengthened restrictions on reducibility. This change ensures that when deriving or inferring an instance for a semireducible type definition, the definition's RHS is not leaked when the instance is reduced at lower than semireducible transparency.

More specifically, given the "source type" and "target type" (the given and expected type for `inferInstanceAs`, the right-hand side and applied left-hand side of the `def` for `deriving`), we synthesize an instance for the source type and then unfold and rewrap its components (fields, nested instances) as necessary to make them compatible with the target type. The individual steps are represented by the following options, which all default to enabled and can be disabled to help with porting:
- `backward.inferInstanceAs.wrap`: master switch for instance adjustment in both `inferInstanceAs` and the default `deriving` handler
- `backward.inferInstanceAs.wrap.reuseSubInstances`: reuse existing instances for the target type for sub-instance fields to avoid non-defeq instance diamonds
- `backward.inferInstanceAs.wrap.instances`: wrap non-reducible instances in auxiliary definitions
- `backward.inferInstanceAs.wrap.data`: wrap data fields in auxiliary definitions (proof fields are always wrapped)

This PR is an extension and rewrite of prior work in Mathlib: https://github.com/leanprover-community/mathlib4/pull/36420

Last(?) part of fix for #9077

🤖 Prepared with Claude Code

# Breaking changes

Proofs that relied on the prior "defeq abuse" of these instance or that depended on their specific structure may need adjustments. As `inferInstanceAs A` now needs to know the source and target types exactly before it can continue, it cannot be used anymore as a synonym for `(inferInstance : A)`, use the latter instead when source and target type are identical.
2026-03-22 13:25:46 +01:00
..
IR fix: export ABI mismatch (#13022) 2026-03-21 14:52:23 +00:00
LCNF feat: unfold and rewrap instances in inferInstanceAs and deriving 2026-03-22 13:25:46 +01:00
BorrowedAnnotation.lean refactor: module-ize Lean (#9330) 2025-07-25 12:02:51 +00:00
ClosedTermCache.lean fix: closed term cache (#12024) 2026-01-16 12:41:54 +00:00
CSimpAttr.lean feat: shake: track [csimp] uses (#12351) 2026-02-07 14:27:00 +00:00
ExportAttr.lean chore: rename Substring to Substring.Raw (#11154) 2025-11-16 09:30:04 +00:00
ExternAttr.lean refactor: ignore borrow annotations at export/extern tricks (#12930) 2026-03-16 10:03:40 +00:00
FFI.lean refactor: use String.split instead of String.splitOn or String.splitToList (#11250) 2025-11-19 09:35:19 +00:00
ImplementedByAttr.lean perf: put the compiler off the critical path (#12335) 2026-02-05 20:39:11 +00:00
InitAttr.lean chore: remove temp bootstrap code (#13021) 2026-03-21 16:41:14 +00:00
InlineAttrs.lean refactor: use isRecursiveDefinition when validating macro_inline (#12106) 2026-01-22 16:31:34 +00:00
IR.lean refactor: port EmitC to LCNF (#12781) 2026-03-11 14:19:54 +00:00
LCNF.lean chore: remove the LCNF testing framework (#12207) 2026-01-28 10:09:30 +00:00
Main.lean feat: experimental option to move non-meta compilation out of lean build step (#10291) 2026-03-20 10:39:39 +00:00
MetaAttr.lean feat: stricter meta check for temporary programs in native_decide etc (#13005) 2026-03-20 15:51:18 +00:00
ModPkgExt.lean fix: symbol name for native boxed declarations in the interpreter (#12095) 2026-01-21 20:38:29 +00:00
NameDemangling.lean fix: incorrect borrow annotation on demangleBtLinCStr leading to segfault on panic (#12939) 2026-03-17 09:24:57 +00:00
NameMangling.lean perf: separate meta and non-meta initializers (#12016) 2026-02-26 08:05:19 +00:00
NeverExtractAttr.lean chore: remove redundant imports in core (#10750) 2025-10-16 20:27:46 +00:00
NoncomputableAttr.lean fix: mark failed compilations as noncomputable (#12625) 2026-02-23 09:18:21 +00:00
Old.lean refactor: use String.Slice in String.take and variants (#11180) 2025-11-18 16:13:48 +00:00
Options.lean feat: experimental option to move non-meta compilation out of lean build step (#10291) 2026-03-20 10:39:39 +00:00
Specialize.lean chore: shake core (#12276) 2026-02-05 09:10:32 +00:00