lean4-htt/src/Lean/Meta.lean
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

64 lines
2.1 KiB
Text

/-
Copyright (c) 2019 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
module
prelude
public import Lean.Meta.Basic
public import Lean.Meta.LevelDefEq
public import Lean.Meta.WHNF
public import Lean.Meta.InferType
public import Lean.Meta.FunInfo
public import Lean.Meta.ExprDefEq
public import Lean.Meta.DecLevel
public import Lean.Meta.DiscrTree
public import Lean.Meta.Reduce
public import Lean.Meta.Instances
public import Lean.Meta.AbstractMVars
public import Lean.Meta.SynthInstance
public import Lean.Meta.AppBuilder
public import Lean.Meta.Sorry
public import Lean.Meta.Tactic
public import Lean.Meta.KAbstract
public import Lean.Meta.RecursorInfo
public import Lean.Meta.GeneralizeTelescope
public import Lean.Meta.Match
public import Lean.Meta.ReduceEval
public import Lean.Meta.Closure
public import Lean.Meta.AbstractNestedProofs
public import Lean.Meta.InstanceNormalForm
public import Lean.Meta.LetToHave
public import Lean.Meta.ForEachExpr
public import Lean.Meta.Transform
public import Lean.Meta.PPGoal
public import Lean.Meta.UnificationHint
public import Lean.Meta.Inductive
public import Lean.Meta.SizeOf
public import Lean.Meta.IndPredBelow
public import Lean.Meta.Coe
public import Lean.Meta.CollectFVars
public import Lean.Meta.GeneralizeVars
public import Lean.Meta.Injective
public import Lean.Meta.Structure
public import Lean.Meta.Constructions
public import Lean.Meta.CongrTheorems
public import Lean.Meta.Eqns
public import Lean.Meta.ExprLens
public import Lean.Meta.ExprTraverse
public import Lean.Meta.Eval
public import Lean.Meta.CoeAttr
public import Lean.Meta.Iterator
public import Lean.Meta.LazyDiscrTree
public import Lean.Meta.LitValues
public import Lean.Meta.CheckTactic
public import Lean.Meta.Canonicalizer
public import Lean.Meta.Diagnostics
public import Lean.Meta.BinderNameHint
public import Lean.Meta.TryThis
public import Lean.Meta.Hint
public import Lean.Meta.MethodSpecs
public import Lean.Meta.CtorIdxHInj
public import Lean.Meta.Sym
public import Lean.Meta.MonadSimp
public import Lean.Meta.HaveTelescope