lean4-htt/src/Lean/Compiler/LCNF
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
..
Simp feat: unfold and rewrap instances in inferInstanceAs and deriving 2026-03-22 13:25:46 +01:00
AlphaEqv.lean feat: support for del, isShared, oset and setTag (#12687) 2026-02-25 10:43:15 +00:00
AuxDeclCache.lean refactor: introduce a phase separation to the IR (#12214) 2026-01-30 09:42:29 +00:00
BaseTypes.lean chore: remove redundant imports in core (#10750) 2025-10-16 20:27:46 +00:00
Basic.lean feat: experimental option to move non-meta compilation out of lean build step (#10291) 2026-03-20 10:39:39 +00:00
Bind.lean feat: respect user provided borrow annotations (#12830) 2026-03-20 14:28:17 +00:00
Check.lean chore: turn on new do elaborator in Core (#12656) 2026-03-09 12:38:33 +00:00
Closure.lean refactor: introduce a phase separation to the IR (#12214) 2026-01-30 09:42:29 +00:00
CompatibleTypes.lean refactor: introduce a phase separation to the IR (#12214) 2026-01-30 09:42:29 +00:00
CompilerM.lean feat: unfold and rewrap instances in inferInstanceAs and deriving 2026-03-22 13:25:46 +01:00
ConfigOptions.lean refactor: reset reuse pass to LCNF (#12315) 2026-02-05 15:54:46 +00:00
CSE.lean refactor: introduce a phase separation to the IR (#12214) 2026-01-30 09:42:29 +00:00
DeclHash.lean feat: support for del, isShared, oset and setTag (#12687) 2026-02-25 10:43:15 +00:00
DependsOn.lean feat: support for del, isShared, oset and setTag (#12687) 2026-02-25 10:43:15 +00:00
ElimDead.lean feat: support for del, isShared, oset and setTag (#12687) 2026-02-25 10:43:15 +00:00
ElimDeadBranches.lean feat: experimental option to move non-meta compilation out of lean build step (#10291) 2026-03-20 10:39:39 +00:00
EmitC.lean feat: increase default stack size from 8MB to 1GB (#12971) 2026-03-20 15:40:00 +00:00
EmitUtil.lean refactor: port EmitC to LCNF (#12781) 2026-03-11 14:19:54 +00:00
ExpandResetReuse.lean feat: non exponential codegen for reset-reuse (#12665) 2026-02-26 09:35:45 +00:00
ExplicitBoxing.lean feat: support for del, isShared, oset and setTag (#12687) 2026-02-25 10:43:15 +00:00
ExplicitRC.lean feat: support for del, isShared, oset and setTag (#12687) 2026-02-25 10:43:15 +00:00
ExtractClosed.lean feat: boxed simple ground literal extraction (#12727) 2026-02-27 16:15:14 +00:00
FixedParams.lean refactor: introduce a phase separation to the IR (#12214) 2026-01-30 09:42:29 +00:00
FloatLetIn.lean perf: put mapMonoM where sensible in the compiler (#12610) 2026-02-20 13:12:59 +00:00
FVarUtil.lean feat: support for del, isShared, oset and setTag (#12687) 2026-02-25 10:43:15 +00:00
InferBorrow.lean perf: forward propagation of user defined borrrow annotations (#13001) 2026-03-20 19:03:17 +00:00
InferType.lean chore: turn on new do elaborator in Core (#12656) 2026-03-09 12:38:33 +00:00
Internalize.lean perf: faster LCNF internalization (#12878) 2026-03-11 08:15:05 +00:00
Irrelevant.lean feat: zero cost BaseIO (#10625) 2025-10-22 10:55:12 +02:00
JoinPoints.lean perf: put mapMonoM where sensible in the compiler (#12610) 2026-02-20 13:12:59 +00:00
LambdaLifting.lean refactor: rename instance_reducible to implicit_reducible (#12567) 2026-02-18 22:19:16 +00:00
LCtx.lean feat: support for del, isShared, oset and setTag (#12687) 2026-02-25 10:43:15 +00:00
Level.lean refactor: introduce a phase separation to the IR (#12214) 2026-01-30 09:42:29 +00:00
LiveVars.lean feat: support for del, isShared, oset and setTag (#12687) 2026-02-25 10:43:15 +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
MonadScope.lean refactor: introduce a phase separation to the IR (#12214) 2026-01-30 09:42:29 +00:00
MonoTypes.lean chore: shake core (#12276) 2026-02-05 09:10:32 +00:00
OtherDecl.lean feat: lambda pure conversion in LCNF (#12272) 2026-02-03 10:24:59 +00:00
Passes.lean feat: experimental option to move non-meta compilation out of lean build step (#10291) 2026-03-20 10:39:39 +00:00
PassManager.lean doc: the different chunks of the pass manager (#12400) 2026-02-10 08:48:36 +00:00
PhaseExt.lean feat: experimental option to move non-meta compilation out of lean build step (#10291) 2026-03-20 10:39:39 +00:00
PrettyPrinter.lean feat: support for del, isShared, oset and setTag (#12687) 2026-02-25 10:43:15 +00:00
Probing.lean feat: support for del, isShared, oset and setTag (#12687) 2026-02-25 10:43:15 +00:00
PropagateBorrow.lean perf: forward propagation of user defined borrrow annotations (#13001) 2026-03-20 19:03:17 +00:00
PublicDeclsExt.lean refactor: port EmitC to LCNF (#12781) 2026-03-11 14:19:54 +00:00
PullFunDecls.lean refactor: introduce a phase separation to the IR (#12214) 2026-01-30 09:42:29 +00:00
PullLetDecls.lean refactor: introduce a phase separation to the IR (#12214) 2026-01-30 09:42:29 +00:00
PushProj.lean feat: support for del, isShared, oset and setTag (#12687) 2026-02-25 10:43:15 +00:00
ReduceArity.lean feat: lambda pure conversion in LCNF (#12272) 2026-02-03 10:24:59 +00:00
ReduceJpArity.lean refactor: introduce a phase separation to the IR (#12214) 2026-01-30 09:42:29 +00:00
Renaming.lean feat: support for del, isShared, oset and setTag (#12687) 2026-02-25 10:43:15 +00:00
ResetReuse.lean perf: forward propagation of user defined borrrow annotations (#13001) 2026-03-20 19:03:17 +00:00
ScopeM.lean refactor: module-ize Lean (#9330) 2025-07-25 12:02:51 +00:00
Simp.lean refactor: introduce a phase separation to the IR (#12214) 2026-01-30 09:42:29 +00:00
SimpCase.lean refactor: port EmitC to LCNF (#12781) 2026-03-11 14:19:54 +00:00
SimpleGroundExpr.lean feat: extract simple array literals as static initializers (#12724) 2026-02-27 18:42:21 +00:00
Specialize.lean refactor: rename instance_reducible to implicit_reducible (#12567) 2026-02-18 22:19:16 +00:00
SpecInfo.lean feat: experimental option to move non-meta compilation out of lean build step (#10291) 2026-03-20 10:39:39 +00:00
SplitSCC.lean refactor: use Code.forM more (#12649) 2026-02-23 14:06:28 +00:00
StructProjCases.lean perf: put mapMonoM where sensible in the compiler (#12610) 2026-02-20 13:12:59 +00:00
ToDecl.lean feat: throw an error when export declarations have borrow annotations (#13017) 2026-03-20 23:22:52 +00:00
ToExpr.lean feat: support for del, isShared, oset and setTag (#12687) 2026-02-25 10:43:15 +00:00
ToImpure.lean perf: remove void JP arguments (#12790) 2026-03-04 15:46:42 +00:00
ToImpureType.lean chore: shake core (#12276) 2026-02-05 09:10:32 +00:00
ToLCNF.lean feat: respect user provided borrow annotations (#12830) 2026-03-20 14:28:17 +00:00
ToMono.lean chore: shake core (#12276) 2026-02-05 09:10:32 +00:00
Toposort.lean refactor: port EmitC to LCNF (#12781) 2026-03-11 14:19:54 +00:00
Types.lean perf: boxing a uint64 yields and object not a tobject (#12465) 2026-02-13 12:14:37 +00:00
Util.lean chore: shake core (#12276) 2026-02-05 09:10:32 +00:00
Visibility.lean feat: experimental option to move non-meta compilation out of lean build step (#10291) 2026-03-20 10:39:39 +00:00