lean4-htt/src/Lean/Parser
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
..
Module chore: fix core after rebootstrap 2026-02-25 11:40:02 +01:00
Tactic fix: make first token detection work in modules (#12047) 2026-01-20 11:12:05 +00:00
Term feat: stricter meta check for temporary programs in native_decide etc (#13005) 2026-03-20 15:51:18 +00:00
Attr.lean fix: make first token detection work in modules (#12047) 2026-01-20 11:12:05 +00:00
Basic.lean chore: fix typo in parser docstring (#11753) 2025-12-20 23:17:47 +00:00
Command.lean feat: stricter meta check for temporary programs in native_decide etc (#13005) 2026-03-20 15:51:18 +00:00
Do.lean feat: support expected type annotation in doPatDecl (#12866) 2026-03-10 11:42:03 +00:00
Extension.lean feat: strengthen evalConst meta check 2026-02-25 11:40:02 +01:00
Extra.lean chore: shake core (#12276) 2026-02-05 09:10:32 +00:00
Level.lean refactor: remove some unnecessary meta imports (#9542) 2025-07-25 15:14:02 +00:00
Module.lean feat: stricter meta check for temporary programs in native_decide etc (#13005) 2026-03-20 15:51:18 +00:00
StrInterpolation.lean chore: rename String.Pos to String.Pos.Raw (#10624) 2025-10-01 07:45:24 +00:00
Syntax.lean feat: enable notationItem in "mixfix" notation commands (#10378) 2025-09-14 18:54:36 +00:00
Tactic.lean fix: make first token detection work in modules (#12047) 2026-01-20 11:12:05 +00:00
Term.lean feat: unfold and rewrap instances in inferInstanceAs and deriving 2026-03-22 13:25:46 +01:00
Types.lean chore: turn on new do elaborator in Core (#12656) 2026-03-09 12:38:33 +00:00