blob: 53b4dd7fc62438797c3bba5a4d2a4fb4af5a22b9 [file]
/-
Copyright (c) 2019 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Daniel Selsam, Leonardo de Moura
Type class instance synthesizer using tabled resolution.
-/
module
prelude
public import Init.Data.Array.InsertionSort
public import Lean.Meta.Instances
public import Lean.Meta.AbstractMVars
public import Lean.Meta.Check
import Init.While
import Lean.Util.CollectFVars
public section
namespace Lean.Meta
register_builtin_option synthInstance.maxHeartbeats : Nat := {
defValue := 20000
descr := "maximum amount of heartbeats per typeclass resolution problem. A heartbeat is number of (small) memory allocations (in thousands), 0 means no limit"
}
register_builtin_option debug.synthInstance.checkCacheHits : Bool := {
defValue := false
descr := "differentially validate type class resolution cache hits: rerun every served query and panic if the result differs from the cached one, which means a dependency of the entry was not recorded (roughly doubles resolution cost)"
}
register_builtin_option synthInstance.maxSize : Nat := {
defValue := 128
descr := "maximum number of instances used to construct a solution in the type class instance synthesis procedure"
}
register_builtin_option backward.synthInstance.canonInstances : Bool := {
defValue := true
descr := "use optimization that relies on 'morally canonical' instances during type class resolution"
}
namespace SynthInstance
def getMaxHeartbeats (opts : Options) : Nat :=
synthInstance.maxHeartbeats.get opts * 1000
structure Instance where
val : Expr
synthOrder : Array Nat
/--
If `true`, `val` is a constant whose universe levels must be replaced with fresh universe
metavariables before it is used. We instantiate them lazily because goals such as
`DFunLike ?F ?α ?β` match thousands of instances, and resolution usually tries only a few.
-/
freshLevels : Bool := false
deriving Inhabited
/-- Returns `inst.val`, instantiating its universe levels with fresh metavariables if needed. -/
def Instance.getVal (inst : Instance) : MetaM Expr := do
if inst.freshLevels then
if let .const declName us := inst.val then
return .const declName (← us.mapM fun _ => mkFreshLevelMVar)
return inst.val
structure GeneratorNode where
mvar : Expr
key : Expr
mctx : MetavarContext
instances : Array Instance
currInstanceIdx : Nat
/--
`typeHasMVars := true` if type of `mvar` contains metavariables.
We store this information to implement an optimization that relies on the fact
that instances are "morally canonical."
That is, we need to find at most one answer for this generator node if the type
does not have metavariables.
-/
typeHasMVars : Bool
deriving Inhabited
structure ConsumerNode where
mvar : Expr
key : Expr
mctx : MetavarContext
subgoals : List Expr
size : Nat -- instance size so far
deriving Inhabited
inductive Waiter where
| consumerNode : ConsumerNode → Waiter
| root : Waiter
def Waiter.isRoot : Waiter → Bool
| .consumerNode _ => false
| .root => true
/-!
In tabled resolution, we creating a mapping from goals (e.g., `Coe Nat ?x`) to
answers and waiters. Waiters are consumer nodes that are waiting for answers for a
particular node.
We implement this mapping using a `HashMap` where the keys are
normalized expressions. That is, we replace assignable metavariables
with auxiliary free variables of the form `_tc.<idx>`. We do
not declare these free variables in any local context, and we should
view them as "normalized names" for metavariables. For example, the
term `f ?m ?m ?n` is normalized as
`f _tc.0 _tc.0 _tc.1`.
This approach is structural, and we may visit the same goal more
than once if the different occurrences are just definitionally
equal, but not structurally equal.
Remark: a metavariable is assignable only if its depth is equal to
the metavar context depth.
-/
namespace MkTableKey
structure State where
nextIdx : Nat := 0
lmap : Std.HashMap LMVarId Level := {}
emap : Std.HashMap MVarId Expr := {}
mctx : MetavarContext
abbrev M := StateM State
@[always_inline]
instance : MonadMCtx M where
getMCtx := return (← get).mctx
modifyMCtx f := modify fun s => { s with mctx := f s.mctx }
partial def normLevel (u : Level) : M Level := do
if !u.hasMVar then
return u
else match u with
| .succ v => return u.updateSucc! (← normLevel v)
| .max v w => return u.updateMax! (← normLevel v) (← normLevel w)
| .imax v w => return u.updateIMax! (← normLevel v) (← normLevel w)
| .mvar mvarId =>
if (← getMCtx).getLevelDepth mvarId != (← getMCtx).depth then
return u
else
let s ← get
match (← get).lmap[mvarId]? with
| some u' => pure u'
| none =>
let u' := mkLevelParam <| Name.mkNum `_tc s.nextIdx
modify fun s => { s with nextIdx := s.nextIdx + 1, lmap := s.lmap.insert mvarId u' }
return u'
| u => return u
partial def normExpr (e : Expr) : M Expr := do
if !e.hasMVar then
pure e
else match e with
| .const _ us => return e.updateConst! (← us.mapM normLevel)
| .sort u => return e.updateSort! (← normLevel u)
| .app f a => return e.updateApp! (← normExpr f) (← normExpr a)
| .letE _ t v b _ => return e.updateLetE! (← normExpr t) (← normExpr v) (← normExpr b)
| .forallE _ d b _ => return e.updateForallE! (← normExpr d) (← normExpr b)
| .lam _ d b _ => return e.updateLambdaE! (← normExpr d) (← normExpr b)
| .mdata _ b => return e.updateMData! (← normExpr b)
| .proj _ _ b => return e.updateProj! (← normExpr b)
| .mvar mvarId =>
if !(← mvarId.isAssignable) then
return e
else
let s ← get
match s.emap[mvarId]? with
| some e' => pure e'
| none => do
let e' := mkFVar { name := Name.mkNum `_tc s.nextIdx }
modify fun s => { s with nextIdx := s.nextIdx + 1, emap := s.emap.insert mvarId e' }
return e'
| _ => return e
end MkTableKey
/-- Remark: `mkTableKey` assumes `e` does not contain assigned metavariables. -/
def mkTableKey [Monad m] [MonadMCtx m] (e : Expr) : m Expr := do
let (r, s) := MkTableKey.normExpr e |>.run { mctx := (← getMCtx) }
setMCtx s.mctx
return r
structure Answer where
result : AbstractMVarsResult
resultType : Expr
size : Nat
deriving Inhabited
structure TableEntry where
waiters : Array Waiter
answers : Array Answer := #[]
structure Context where
maxResultSize : Nat
maxHeartbeats : Nat
/--
Remark: the SynthInstance.State is not really an extension of `Meta.State`.
The field `postponed` is not needed, and the field `mctx` is misleading since
`synthInstance` methods operate over different `MetavarContext`s simultaneously.
That being said, we still use `extends` because it makes it simpler to move from
`M` to `MetaM`.
-/
structure State where
result? : Option AbstractMVarsResult := none
generatorStack : Array GeneratorNode := #[]
resumeStack : Array (ConsumerNode × Answer) := #[]
tableEntries : Std.HashMap Expr TableEntry := {}
abbrev SynthM := ReaderT Context $ StateRefT State MetaM
def checkSystem : SynthM Unit := do
Core.checkInterrupted
Core.checkMaxHeartbeatsCore "typeclass" `synthInstance.maxHeartbeats (← read).maxHeartbeats
instance : Inhabited (SynthM α) where
default := fun _ _ => default
/--
Return globals and locals instances that may unify with `type`.
The universe levels of global instances are not instantiated, see `Instance.freshLevels`.
-/
def getInstancesLazy (type : Expr) : MetaM (Array Instance) := do
-- We must retrieve `localInstances` before we use `forallTelescopeReducing` because it will update the set of local instances
let localInstances ← getLocalInstances
forallTelescopeReducing type fun _ type => do
let className? ← isClass? type
match className? with
| none => throwError "type class instance expected{indentExpr type}"
| some className =>
recordLocalInstClass className
recordInstClassChanges className (getInstanceClassChanges (← getEnv) className)
let globalInstances ← getGlobalInstancesIndex
let result ← globalInstances.getUnify type
-- Using insertion sort because it is stable and the array `result` should be mostly sorted.
-- Most instances have default priority.
let result := result.insertionSort fun e₁ e₂ => e₁.priority < e₂.priority
let erasedInstances ← getErasedInstances
let env ← getEnv
let mut result ← result.filterMapM fun e => match e.val with
| .const constName us =>
if erasedInstances.contains constName then
return none
else if env.isExporting && !env.contains constName then
-- private instances must not leak into public scope
return none
else
return some { val := e.val, synthOrder := e.synthOrder, freshLevels := !us.isEmpty }
| _ => panic! "global instance is not a constant"
for linst in localInstances do
if linst.className == className then
let synthOrder ← forallTelescopeReducing (← inferType linst.fvar) fun xs _ => do
if xs.isEmpty then return #[]
let mut order := #[]
for i in *...xs.size, x in xs do
if (← getFVarLocalDecl x).binderInfo == .instImplicit then
order := order.push i
return order
result := result.push { val := linst.fvar, synthOrder }
trace[Meta.synthInstance.instances] result.map (·.val)
return result
/-- Return globals and locals instances that may unify with `type` -/
def getInstances (type : Expr) : MetaM (Array Instance) := do
(← getInstancesLazy type).mapM fun inst => return { inst with val := (← inst.getVal), freshLevels := false }
def mkGeneratorNode? (key mvar : Expr) : MetaM (Option GeneratorNode) := do
let mvarType ← inferType mvar
let mvarType ← instantiateMVars mvarType
let instances ← getInstancesLazy mvarType
if instances.isEmpty then
return none
else
let mctx ← getMCtx
return some {
mvar, key, mctx, instances
typeHasMVars := mvarType.hasMVar
currInstanceIdx := instances.size
}
/--
Create a new generator node for `mvar` and add `waiter` as its waiter.
`key` must be `mkTableKey mctx mvarType`. -/
def newSubgoal (mctx : MetavarContext) (key : Expr) (mvar : Expr) (waiter : Waiter) : SynthM Unit :=
withMCtx mctx do withTraceNode' `Meta.synthInstance do
match (← mkGeneratorNode? key mvar) with
| none => pure ((), m!"no instances for {key}")
| some node =>
let entry : TableEntry := { waiters := #[waiter] }
modify fun s =>
{ s with
generatorStack := s.generatorStack.push node
tableEntries := s.tableEntries.insert key entry }
pure ((), m!"new goal {key}")
def findEntry? (key : Expr) : SynthM (Option TableEntry) := do
return (← get).tableEntries[key]?
def getEntry (key : Expr) : SynthM TableEntry := do
match (← findEntry? key) with
| none => panic! "invalid key at synthInstance"
| some entry => pure entry
/--
Create a `key` for the goal associated with the given metavariable.
That is, we create a key for the type of the metavariable.
We must instantiate assigned metavariables before we invoke `mkTableKey`. -/
def mkTableKeyFor (mctx : MetavarContext) (mvar : Expr) : SynthM Expr :=
withMCtx mctx do
let mvarType ← inferType mvar
let mvarType ← instantiateMVars mvarType
mkTableKey mvarType
/-- See `getSubgoals` and `getSubgoalsAux`
We use the parameter `j` to reduce the number of `instantiate*` invocations.
It is the same approach we use at `forallTelescope` and `lambdaTelescope`.
Given `getSubgoalsAux args j subgoals instVal type`,
we have that `type.instantiateRevRange j args.size args` does not have loose bound variables. -/
structure SubgoalsResult where
subgoals : List Expr
instVal : Expr
instTypeBody : Expr
/--
`getSubgoals lctx localInsts xs inst` creates the subgoals for the instance `inst`.
The subgoals are in the context of the free variables `xs`, and
`(lctx, localInsts)` is the local context and instances before we added the free variables to it.
This extra complication is required because
1- We want all metavariables created by `synthInstance` to share the same local context.
2- We want to ensure that applications such as `mvar xs` are higher order patterns.
The method `getGoals` create a new metavariable for each parameter of `inst`.
For example, suppose the type of `inst` is `forall (x_1 : A_1) ... (x_n : A_n), B x_1 ... x_n`.
Then, we create the metavariables `?m_i : forall xs, A_i`, and return the subset of these
metavariables that are instance implicit arguments, and the expressions:
- `inst (?m_1 xs) ... (?m_n xs)` (aka `instVal`)
- `B (?m_1 xs) ... (?m_n xs)` -/
def getSubgoals (lctx : LocalContext) (localInsts : LocalInstances) (xs : Array Expr) (inst : Instance) : MetaM SubgoalsResult := do
let mut instVal ← inst.getVal
let mut instType ← inferType instVal
let mut mvars := #[]
let mut subst := #[]
repeat do
if let .forallE _ d b _ := instType then
let d := d.instantiateRev subst
let mvar ← mkFreshExprMVarAt lctx localInsts (← mkForallFVars xs d)
subst := subst.push (mkAppN mvar xs)
instVal := mkApp instVal (mkAppN mvar xs)
instType := b
mvars := mvars.push mvar
else
instType ← whnf (instType.instantiateRev subst)
instVal := instVal.instantiateRev subst
subst := #[]
unless instType.isForall do break
return {
instVal := instVal.instantiateRev subst
instTypeBody := instType.instantiateRev subst
subgoals := inst.synthOrder.map (mvars[·]!) |>.toList
}
/--
Try to synthesize metavariable `mvar` using the instance `inst`.
Remark: `mctx` is set using `withMCtx`.
If it succeeds, the result is a new updated metavariable context and a new list of subgoals.
A subgoal is created for each instance implicit parameter of `inst`. -/
def tryResolve (mvar : Expr) (inst : Instance) : MetaM (Option (MetavarContext × List Expr)) := do
if (← isDiagnosticsEnabled) then
if let .const declName _ := inst.val.getAppFn then
recordInstance declName
let mvarType ← inferType mvar
let lctx ← getLCtx
let localInsts ← getLocalInstances
forallTelescopeReducing mvarType fun xs mvarTypeBody => do
let { subgoals, instVal, instTypeBody } ← getSubgoals lctx localInsts xs inst
-- Mark the instance-argument metavariables so that a would-be assignment during unification
-- with the goal type must preserve the type at instance transparency.
-- See `backward.isDefEq.respectTransparency.instanceSearchTypes` and issue #9077.
subgoals.forM fun subgoal => subgoal.mvarId!.markInstanceTyped
withTraceNode `Meta.synthInstance.tryResolve (fun _ => do withMCtx (← getMCtx) do
return m!"{← instantiateMVars mvarTypeBody} ≟ {← instantiateMVars instTypeBody}") do
if (← isDefEq mvarTypeBody instTypeBody) then
/-
We set `etaReduce := true`.
For example, suppose `e` is the local variable `inst x y`, and `xs` is `#[x, y]`, then
the result is `inst` instead of `fun x y => inst x y`.
Consider the following definition.
```
def filter (p : α → Prop) [inst : DecidablePred p] (xs : List α) : List α :=
match xs with
| [] => []
| x :: xs' => if p x then x :: filter p xs' else filter p xs'
```
Without `etaReduce := true`, the implicit instance at the `filter` applications would be `fun x => inst x` instead of `inst`.
Moreover, the equation lemmas associated with `filter` would have `fun x => inst x` on their right-hand-side. Then,
we would start getting terms such as `fun x => (fun x => inst x) x` when using the equational theorem.
-/
let instVal ← mkLambdaFVars xs instVal (etaReduce := true)
/-
When the goal type is metavariable-free, we assign `instVal` directly: the final
`isDefEq mvar instVal` recheck is redundant (the goal type and `instTypeBody` have
just been unified, and the type of `instVal` is `instTypeBody` by construction) and
can be very expensive, since it re-infers the type of `instVal` and re-unifies it
with the goal type.
When the goal type contains metavariables, re-unifying the two (definitionally equal, but not
necessarily syntactically equal) types has side effects that elaboration relies on.
In particular, `isDefEqArgs` runs `trySynthPending` on metavariables in
instance-implicit argument positions of the applications it descends into. Example:
the goal `IsPredArchimedean ι ?pre ?pd` (from Mathlib) — created when elaborating a class
projection, whose class parameters are demoted to plain implicit binders — matches a
candidate by assigning the candidate's fresh metavariables to `?pre`/`?pd` without
determining them. No other component is responsible for these metavariables, and the
recheck's `trySynthPending` is what synthesizes them; without it, the answer is
parametric in `?pd` and elaboration fails with "don't know how to synthesize
implicit argument" (see `tests/elab/synthPendingClassMVars.lean`).
Moreover, the set of metavariables the recheck synthesizes is not a function of the
goal alone: `isDefEqArgs` only descends into subterms whose two spellings differ
(e.g. rechecking `C (f a ?m) =?= C (f a' ?m)` pends `?m` iff `a` and `a'` are
syntactically different), so the side effects cannot be replayed after a direct
assignment, which has only one spelling. Explicit replacements fail in both
directions: synthesizing all pending class metavariables in the goal breaks stage2
(`Init/Internal/Order/Basic.lean`: a higher-order `[Nonempty ε]` metavariable inside
the goal's subject argument must be left to unification), and synthesizing none
breaks Mathlib (`Mathlib/Order/SuccPred/LinearLocallyFinite.lean`). Hence we keep
the recheck whenever the goal type contains metavariables.
**Note**: We should consider eliminating this nasty side effect and fixing
Mathlib in the few places that rely on it. There are ~10 such places.
Remark: we check only `mvarTypeBody`. The goal's hypotheses could contain
metavariables too, but checking the body is cheaper and good enough in practice,
and we want to remove this check altogether (see note above).
-/
if !(← instantiateMVars mvarTypeBody).hasExprMVar then
-- Remark: `mvar` is not assigned here: `tryResolve` runs on the generator node's
-- metavariable context snapshot, in which `mvar` is fresh.
mvar.mvarId!.assign instVal
else
unless (← isDefEq mvar instVal) do return none
return some ((← getMCtx), subgoals)
return none
/--
Assign a precomputed answer to `mvar`.
If it succeeds, the result is a new updated metavariable context and a new list of subgoals. -/
def tryAnswer (mctx : MetavarContext) (mvar : Expr) (answer : Answer) : SynthM (Option MetavarContext) :=
withMCtx mctx do
let (_, _, val) ← openAbstractMVarsResult answer.result
if (← isDefEq mvar val) then
return some (← getMCtx)
else
return none
/-- Move waiters that are waiting for the given answer to the resume stack. -/
def wakeUp (answer : Answer) : Waiter → SynthM Unit
| .root => do
/- Recall that we now use `ignoreLevelMVarDepth := true`. Thus, we should allow solutions
containing universe metavariables, and not check `answer.result.paramNames.isEmpty`.
We use `openAbstractMVarsResult` to construct the universe metavariables
at the correct depth. -/
if answer.result.numMVars == 0 then
modify fun s => { s with result? := answer.result }
else
let (_, _, answerExpr) ← openAbstractMVarsResult answer.result
trace[Meta.synthInstance] "skip answer containing metavariables {answerExpr}"
| .consumerNode cNode =>
modify fun s => { s with resumeStack := s.resumeStack.push (cNode, answer) }
def isNewAnswer (oldAnswers : Array Answer) (answer : Answer) : Bool :=
oldAnswers.all fun oldAnswer =>
-- Remark: isDefEq here is too expensive. TODO: if `==` is too imprecise, add some light normalization to `resultType` at `addAnswer`
-- iseq ← isDefEq oldAnswer.resultType answer.resultType; pure (!iseq)
oldAnswer.resultType != answer.resultType
private def mkAnswer (cNode : ConsumerNode) : MetaM Answer :=
withMCtx cNode.mctx do
let val ← instantiateMVars cNode.mvar
trace[Meta.synthInstance.newAnswer] "size: {cNode.size}, val: {val}"
let result ← abstractMVars val -- assignable metavariables become parameters
let resultType ← inferType result.expr
return { result, resultType, size := cNode.size + 1 }
/--
Create a new answer after `cNode` resolved all subgoals.
That is, `cNode.subgoals == []`.
And then, store it in the tabled entries map, and wakeup waiters. -/
def addAnswer (cNode : ConsumerNode) : SynthM Unit := do
withMCtx cNode.mctx do
if cNode.size ≥ (← read).maxResultSize then
trace[Meta.synthInstance.answer] "{crossEmoji} {← instantiateMVars (← inferType cNode.mvar)}{Format.line}(size: {cNode.size} ≥ {(← read).maxResultSize})"
else
withTraceNode `Meta.synthInstance.answer
(fun _ => return m!"{← instantiateMVars (← inferType cNode.mvar)}") do
let answer ← mkAnswer cNode
-- Remark: `answer` does not contain assignable or assigned metavariables.
let key := cNode.key
let { waiters, answers } ← getEntry key
if isNewAnswer answers answer then
let newEntry := { waiters, answers := answers.push answer }
modify fun s => { s with tableEntries := s.tableEntries.insert key newEntry }
waiters.forM (wakeUp answer)
/--
Return `true` if a type of the form `(a_1 : A_1) → ... → (a_n : A_n) → B` has an unused argument `a_i`.
Remark: This is syntactic check and no reduction is performed.
-/
private def hasUnusedArguments : Expr → Bool
| .forallE _ _ b _ => !b.hasLooseBVar 0 || hasUnusedArguments b
| _ => false
/--
If the type of the metavariable `mvar` has unused argument, return a pair `(α, transformer)`
where `α` is a new type without the unused arguments and the `transformer` is a function for converting a
solution with type `α` into a value that can be assigned to `mvar`.
Example: suppose `mvar` has type `(a : A) → (b : B a) → (c : C a) → D a c`, the result is the pair
```
((a : A) → (c : C a) → D a c,
fun (f : (a : A) → (c : C a) → D a c) (a : A) (b : B a) (c : C a) => f a c
)
```
This method is used to improve the effectiveness of the TC resolution procedure. It was suggested and prototyped by
Tomas Skrivan. It improves the support for instances of type `a : A → C` where `a` does not appear in class `C`.
When we look for such an instance it is enough to look for an instance `c : C` and then return `fun _ => c`.
Tomas' approach makes sure that instance of a type like `a : A → C` never gets tabled/cached. More on that later.
At the core is this method. it takes an expression E and does two things:
The modification to TC resolution works this way: We are looking for an instance of `E`, if it is tabled
just get it as normal, but if not first remove all unused arguments producing `E'`. Now we look up the table again but
for `E'`. If it exists, use the transformer to create E. If it does not exists, create a new goal `E'`.
-/
private def removeUnusedArguments? (mctx : MetavarContext) (mvar : Expr) : MetaM (Option (Expr × Expr)) :=
withMCtx mctx do
let mvarType ← instantiateMVars (← inferType mvar)
if !hasUnusedArguments mvarType then
return none
else
forallTelescope mvarType fun xs body => do
let ys ← xs.foldrM (init := []) fun x ys => do
if body.containsFVar x.fvarId! then
return x :: ys
else if (← ys.anyM fun y => return (← inferType y).containsFVar x.fvarId!) then
return x :: ys
else
return ys
let ys := ys.toArray
let mvarType' ← mkForallFVars ys body
withLocalDeclD `redf mvarType' fun f => do
let transformer ← mkLambdaFVars #[f] (← mkLambdaFVars xs (mkAppN f ys) (etaReduce := true)) (etaReduce := true)
trace[Meta.synthInstance.unusedArgs] "{mvarType}\nhas unused arguments, reduced type{indentExpr mvarType'}\nTransformer{indentExpr transformer}"
return some (mvarType', transformer)
/-- Process the next subgoal in the given consumer node. -/
def consume (cNode : ConsumerNode) : SynthM Unit := do
/- Filter out subgoals that have already been assigned when solving typing constraints.
This may happen when a local instance type depends on other local instances.
For example, in Mathlib, we have
```
@Submodule.setLike : {R : Type u_1} → {M : Type u_2} →
[_inst_1 : Semiring R] →
[_inst_2 : AddCommMonoid M] →
[_inst_3 : @ModuleS R M _inst_1 _inst_2] →
SetLike (@Submodule R M _inst_1 _inst_2 _inst_3) M
```
-/
let cNode := { cNode with
subgoals := ← withMCtx cNode.mctx do
cNode.subgoals.filterM (not <$> ·.mvarId!.isAssigned)
}
match cNode.subgoals with
| [] => addAnswer cNode
| mvar::_ =>
let waiter := Waiter.consumerNode cNode
let key ← mkTableKeyFor cNode.mctx mvar
let entry? ← findEntry? key
match entry? with
| none =>
-- Remove unused arguments and try again, see comment at `removeUnusedArguments?`
match (← removeUnusedArguments? cNode.mctx mvar) with
| none => newSubgoal cNode.mctx key mvar waiter
| some (mvarType', transformer) =>
let key' ← withMCtx cNode.mctx <| mkTableKey mvarType'
match (← findEntry? key') with
| none =>
let (mctx', mvar') ← withMCtx cNode.mctx do
let mvar' ← mkFreshExprMVar mvarType'
return (← getMCtx, mvar')
newSubgoal mctx' key' mvar' (Waiter.consumerNode { cNode with mctx := mctx', subgoals := mvar'::cNode.subgoals })
| some entry' =>
let answers' ← entry'.answers.mapM fun a => withMCtx cNode.mctx do
let trAnswr := Expr.betaRev transformer #[← instantiateMVars a.result.expr]
let trAnswrType ← inferType trAnswr
pure { a with result.expr := trAnswr, resultType := trAnswrType }
modify fun s =>
{ s with
resumeStack := answers'.foldl (fun s answer => s.push (cNode, answer)) s.resumeStack,
tableEntries := s.tableEntries.insert key' { entry' with waiters := entry'.waiters.push waiter } }
| some entry => modify fun s =>
{ s with
resumeStack := entry.answers.foldl (fun s answer => s.push (cNode, answer)) s.resumeStack,
tableEntries := s.tableEntries.insert key { entry with waiters := entry.waiters.push waiter } }
def getTop : SynthM GeneratorNode :=
return (← get).generatorStack.back!
@[inline] def modifyTop (f : GeneratorNode → GeneratorNode) : SynthM Unit :=
modify fun s => { s with generatorStack := s.generatorStack.modify (s.generatorStack.size - 1) f }
/-- Try the next instance in the node on the top of the generator stack. -/
def generate : SynthM Unit := do
let gNode ← getTop
if gNode.currInstanceIdx == 0 then
modify fun s => { s with generatorStack := s.generatorStack.pop }
else
let key := gNode.key
let idx := gNode.currInstanceIdx - 1
let inst := gNode.instances[idx]!
let mctx := gNode.mctx
let mvar := gNode.mvar
/- See comment at `typeHasMVars` -/
if (← getRecordedOption backward.synthInstance.canonInstances) then
unless gNode.typeHasMVars do
if let some entry := (← get).tableEntries[key]? then
if entry.answers.any fun answer => answer.result.numMVars == 0 then
/-
We already have an answer that:
1. its result does not have metavariables.
2. its types do not have metavariables.
Thus, we can skip other solutions because we assume instances are "morally canonical".
We have added this optimization to address issue #3996.
Remark: Condition 1 is important since root nodes only take into account results
that do **not** contain metavariables. This extra check was added to address issue #4213.
-/
modify fun s => { s with generatorStack := s.generatorStack.pop }
return
discard do withMCtx mctx do
withTraceNode `Meta.synthInstance.apply
(fun _ => return m!"apply {inst.val} to {← instantiateMVars (← inferType mvar)}") do
modifyTop fun gNode => { gNode with currInstanceIdx := idx }
if let some (mctx, subgoals) ← tryResolve mvar inst then
consume { key, mvar, subgoals, mctx, size := 0 }
return some ()
return none
def getNextToResume : SynthM (ConsumerNode × Answer) := do
let r := (← get).resumeStack.back!
modify fun s => { s with resumeStack := s.resumeStack.pop }
return r
/--
Given `(cNode, answer)` on the top of the resume stack, continue execution by using `answer` to solve the
next subgoal. -/
def resume : SynthM Unit := do
let (cNode, answer) ← getNextToResume
match cNode.subgoals with
| [] => panic! "resume found no remaining subgoals"
| mvar::rest =>
match (← tryAnswer cNode.mctx mvar answer) with
| none => return ()
| some mctx =>
withMCtx mctx do
let goal ← inferType cNode.mvar
let subgoal ← inferType mvar
withTraceNode `Meta.synthInstance.resume
(fun _ => withMCtx cNode.mctx do
return m!"propagating {← instantiateMVars answer.resultType} to subgoal {← instantiateMVars subgoal} of {← instantiateMVars goal}") do
trace[Meta.synthInstance.resume] "size: {cNode.size + answer.size}"
consume { key := cNode.key, mvar := cNode.mvar, subgoals := rest, mctx, size := cNode.size + answer.size }
def step : SynthM Bool := do
checkSystem
let s ← get
if !s.resumeStack.isEmpty then
resume
return true
else if !s.generatorStack.isEmpty then
generate
return true
else
return false
def getResult : SynthM (Option AbstractMVarsResult) :=
return (← get).result?
partial def synth : SynthM (Option AbstractMVarsResult) := do
if (← step) then
match (← getResult) with
| none => synth
| some result => return result
else
return none
def main (type : Expr) (maxResultSize : Nat) : MetaM (Option AbstractMVarsResult) :=
withCurrHeartbeats do
let mvar ← mkFreshExprMVar type
let key ← mkTableKey type
let action : SynthM (Option AbstractMVarsResult) := do
newSubgoal (← getMCtx) key mvar Waiter.root
synth
tryCatchRuntimeEx
-- unrestricted: a limit, exceeding it throws
(action.run { maxResultSize, maxHeartbeats := getMaxHeartbeats (← getOptionsUnrestricted) } |>.run' {})
fun ex =>
if ex.isRuntime then
throwError "failed to synthesize{indentExpr type}\n{ex.toMessageData}{useDiagnosticMsg}"
else
throw ex
end SynthInstance
/-!
Type class parameters can be annotated with `outParam` annotations.
Given `C a_1 ... a_n`, we replace `a_i` with a fresh metavariable `?m_i` IF
`a_i` is an `outParam`.
The result is type correct because we reject type class declarations IF
it contains a regular parameter X that depends on an `out` parameter Y.
Then, we execute type class resolution as usual.
If it succeeds, and metavariables ?m_i have been assigned, we try to unify
the original type `C a_1 ... a_n` with the normalized one.
-/
/-- Result kind for `preprocess` -/
private inductive PreprocessKind where
| /--
Target type does not have metavariables.
We use the type to construct the cache key even if the class has output parameters.
Reason: we want to avoid the normalization step in this case.
-/
noMVars
| /-- Target type has metavariables, and class does not have output parameters. -/
mvarsNoOutputParams
| /-- Target type has metavariables, and class has output parameters. -/
mvarsOutputParams
/-- Return type for `preprocess` -/
private structure PreprocessResult where
type : Expr
cacheKeyType : Expr := type
kind : PreprocessKind
/--
Returns `{ type, cacheKeyType, hasOutParams }`, where `type` is the normalized type, and `cacheKeyType`
is part of the key for the type class resolution cache. If the class associated with `type`
does not have output parameters, then, `cacheKeyType` is `type`.
If it has, we replace arguments corresponding with output parameters with wildcard terms.
For example, the cache key for a query like
`HAppend.{0, 0, ?u} (BitVec 8) (BitVec 8) ?m` should be independent of the specific
metavariable IDs in output parameter positions. To achieve this, output parameter arguments
are erased from the cache key. However, universe levels that only appear in output parameter
types (e.g., `?u` corresponding to the result type's universe) must also be erased to avoid
cache misses when the same query is issued with different universe metavariable IDs.
-/
private def preprocess (type : Expr) : MetaM PreprocessResult :=
let keyExprWildcard := mkFVar { name := `__wild__ }
let keyLevelWildcard := mkLevelParam `__wild__
forallTelescopeReducing type fun xs typeBody => do
let typeBody ← whnf typeBody
let type ← mkForallFVars xs typeBody
if !type.hasMVar then return { type, kind := .noMVars }
/-
**Note**: Workaround for classes such as `class ToLevel.{u}`. They do not have any parameters,
the universe parameter inference engine at `Class.lean` assumes `u` is an output parameter,
but this is not correct. We can remove this check after we update `Class.lean` and perform an
update stage0
-/
if typeBody.isConst then return { type, kind := .mvarsNoOutputParams }
let c := typeBody.getAppFn
let .const declName us := c | return { type, kind := .mvarsNoOutputParams }
let env ← getEnv
let some outParamsPos := getOutParamPositions? env declName | return { type, kind := .mvarsNoOutputParams }
let some outLevelParamPos := getOutLevelParamPositions? env declName | unreachable!
if outParamsPos.isEmpty && outLevelParamPos.isEmpty then return { type, kind := .mvarsNoOutputParams }
let c := if outLevelParamPos.isEmpty then c else
let rec normLevels (us : List Level) (i : Nat) : List Level :=
match us with
| [] => []
| u :: us =>
let u := if i ∈ outLevelParamPos then keyLevelWildcard else u
u :: normLevels us (i+1)
mkConst declName (normLevels us 0)
let rec norm (e : Expr) (i : Nat) : Expr :=
match e with
| .app f a =>
let a := if i ∈ outParamsPos then keyExprWildcard else a
mkApp (norm f (i-1)) a
| _ => c
let typeBody := norm typeBody (typeBody.getAppNumArgs - 1)
let cacheKeyType ← mkForallFVars xs typeBody
return { type, cacheKeyType, kind := .mvarsOutputParams }
private partial def preprocessOutParam (type : Expr) : MetaM Expr :=
forallTelescope type fun xs typeBody => do
/- **Note**: See similar test at preprocess. -/
if typeBody.isConst then return type
let c := typeBody.getAppFn
let .const declName us := c | return type
let env ← getEnv
let some outParamsPos := getOutParamPositions? env declName | return type
let some outLevelParamPos := getOutLevelParamPositions? env declName | unreachable!
if outParamsPos.isEmpty && outLevelParamPos.isEmpty then return type
let c ← if outLevelParamPos.isEmpty then pure c else
-- Replace universe parameters corresponding to output parameters with fresh universe metavariables.
let rec preprocessLevels (us : List Level) (i : Nat) : MetaM (List Level) := do
match us with
| [] => return []
| u :: us =>
let u ← if i ∈ outLevelParamPos then mkFreshLevelMVar else pure u
let us ← preprocessLevels us (i+1)
return u :: us
pure <| mkConst declName (← preprocessLevels us 0)
let rec preprocessArgs (type : Expr) (i : Nat) (args : Array Expr) : MetaM (Array Expr) := do
if h : i < args.size then
let type ← whnf type
match type with
| .forallE _ d b _ => do
let arg := args[i]
/-
We should not simply check `d.isOutParam`. See `checkOutParam` and issue #1852.
If an instance implicit argument depends on an `outParam`, it is treated as an `outParam` too.
-/
let arg ← if outParamsPos.contains i then mkFreshExprMVar d else pure arg
let args := args.set i arg
preprocessArgs (b.instantiate1 arg) (i+1) args
| _ =>
throwError "type class resolution failed, insufficient number of arguments" -- TODO improve error message
else
return args
let args := typeBody.getAppArgs
if outParamsPos.isEmpty then
mkForallFVars xs (mkAppN c args)
else
let cType ← inferType c
let args ← preprocessArgs cType 0 args
mkForallFVars xs (mkAppN c args)
/-!
Remark: when `maxResultSize? == none`, the configuration option `synthInstance.maxResultSize` is used.
Remark: we use a different option for controlling the maximum result size for coercions.
-/
private def assignOutParams (type : Expr) (result : Expr) : MetaM Bool := do
let resultType ← inferType result
/-
Output parameters of local instances may be marked as `syntheticOpaque` by the application-elaborator.
We use `withAssignableSyntheticOpaque` to make sure this kind of parameter can be assigned by the following `isDefEq`.
TODO: rewrite this check to avoid `withAssignableSyntheticOpaque`.
**Note**: We tried to remove `withDefault` at the following `isDefEq` because it was a potential performance footgun. TC is supposed to unfold only `reducible` definitions and `instances`.
We reverted the change because it triggered thousands of failures related to the `OrderDual` type. Example:
```
variable {ι : Type}
def OrderDual (α : Type) : Type := α
instance [I : DecidableEq ι] : DecidableEq (OrderDual ι) := inferInstance -- Failure
```
Mathlib developers are currently trying to refactor the `OrderDual` declaration,
but it will take time. We will try to remove the `withDefault` again after the refactoring.
-/
let defEq ← withDefault <| withAssignableSyntheticOpaque <| isDefEq type resultType
unless defEq do
trace[Meta.synthInstance] "{crossEmoji} result type{indentExpr resultType}\nis not definitionally equal to{indentExpr type}"
return defEq
/--
Returns `true` if the `check` at `applyAbstractResult?` may have observable side effects
for `result`. The unifications performed by the check operate on expressions derived from
`result` itself, from the types (and values) of its free variables, and from constant type
schemes instantiated with `result`'s own universe levels. Thus, if no metavariable is
reachable through `result` or through the (transitive) types and values of its free
variables, every unification is between ground expressions and cannot assign anything, so
the check is redundant. Note that mere absence of metavariables in `result` is not enough:
in issue #796, the universe constraint flows through the type `E.{?v} a` of a local
instance occurring in `result`.
-/
private def checkMayHaveSideEffects (result : Expr) : MetaM Bool := do
if result.hasExprMVar || result.hasLevelMVar then return true
let mut s := collectFVars {} result
let mut i := 0
while h : i < s.fvarIds.size do
let localDecl ← s.fvarIds[i].getDecl
let type ← instantiateMVars localDecl.type
if type.hasExprMVar || type.hasLevelMVar then return true
s := collectFVars s type
if let some value := localDecl.value? then
let value ← instantiateMVars value
if value.hasExprMVar || value.hasLevelMVar then return true
s := collectFVars s value
i := i + 1
return false
/--
Auxiliary function for converting the `AbstractMVarsResult` returned by `SynthInstance.main` into an `Expr`.
-/
private def applyAbstractResult? (type : Expr) (abstResult? : Option AbstractMVarsResult) : MetaM (Option Expr) := do
let some abstResult := abstResult? | return none
let (_, _, result) ← openAbstractMVarsResult abstResult
unless (← assignOutParams type result) do return none
let result ← instantiateMVars result
unless (← checkMayHaveSideEffects result) do
return some result
/- We use `check` to propagate universe constraints implied by the `result`.
Recall that we use `allowLevelAssignments := true` which allows universe metavariables in the current depth to be assigned,
but these assignments are discarded by `withNewMCtxDepth`.
The example in the issue #796 exposed this issue.
```
structure A
class B (a : outParam A) (α : Sort u)
class C {a : A} (α : Sort u) [B a α]
class D {a : A} (α : Sort u) [B a α] [c : C α]
class E (a : A) where [c (α : Sort u) [B a α] : C α]
instance c {a : A} [e : E a] (α : Sort u) [B a α] : C α := e.c α
def d {a : A} [e : E a] (α : Sort u) [b : B a α] : D α := ⟨⟩
```
The term `D α` has two instance implicit arguments. The second one has type `C α`, and TC
resolution produces the result `@c.{u} a e α b`.
Note that the `e` has type `E.{?v} a`, and `E` is universe polymorphic,
but the universe does not occur in the parameter `a`. We have that `?v := u` is implied by `@c.{u} a e α b`,
but this assignment is lost.
**Note**: We tried to skip this `check` by tracking whether a universe metavariable
from a lower depth was assigned during the search (a flag set by the level-unification
procedures; such assignments can only happen during TC resolution and are exactly the
ones discarded by `withNewMCtxDepth`). The tracking is insufficient: without
`checkMayHaveSideEffects`, a clean Mathlib build produced 5 failures
(`CategoryTheory/Limits/FilteredColimitCommutesProduct`, `CategoryTheory/Limits/Presheaf`,
`Topology/Category/CompHausLike/SigmaComparison`, `Algebra/Category/ModuleCat/Colimits`,
`Analysis/CStarAlgebra/ContinuousFunctionalCalculus/Isometric`), ranging from
declarations with leaked universe metavariables and kernel type mismatches to
"don't know how to synthesize implicit argument". We suspect the situation is
analogous to the `isDefEq` test at `tryResolve` (see the **Note** there): the `check`
produces unintended side effects (e.g., `trySynthPending` on expression metavariables,
universe assignments the search itself never derived) that these few Mathlib places
rely on, possibly by accident. We should diagnose whether they work by accident and,
if so, fix Mathlib and remove (or further weaken) this `check`.
-/
check result
return some result
/-- Returns whether every recorded lookup in `log` gives the same answer in `opts`. -/
private def validOptionAccesses (opts : Options) (log : RecordedDeps) : Bool :=
log.options.all fun n => opts.find? n == log.base.find? n
/-- Returns whether the environment dependencies in `deps` hold in `env`. -/
private def validEnvDeps (env : Environment) (deps : RecordedDeps) : Bool :=
-- While `trackedGen` is still `baseTrackedGen` and no change was logged since, no environment
-- dependency can have changed.
deps.baseTrackedGen == env.trackedGen && deps.baseChangeLogPos == env.declChangeLog.size ||
deps.extGens.all (fun (idx, gen) => EnvExtension.getGenAt env idx == gen) &&
deps.instClassChanges.all (fun (c, l) =>
-- An unchanged class usually still has the very list that was recorded.
let l' := getInstanceClassChanges env c
(unsafe ptrEq l' l) || l' == l) &&
env.checkDeclChangeLog deps.baseChangeLogPos deps.baseConstGen
/-- Adds the dependencies of a nested query or a used cache entry to those of the enclosing query. -/
private def _root_.Lean.RecordedDeps.mergeInto (child parent : RecordedDeps) : RecordedDeps :=
let options := child.options.foldl (init := parent.options) fun l n =>
-- A lookup answering differently from the parent's `base` was served by a write the parent
-- itself opened, so it is not a dependency of the parent.
if l.contains n || parent.base.find? n != child.base.find? n then l else l.push n
-- Keep the parent's earlier generation, as in `recordExtGenAccess`.
let extGens := child.extGens.foldl (init := parent.extGens) fun l d =>
if l.any (·.1 == d.1) then l else l.push d
let localInstClasses := child.localInstClasses.foldl (init := parent.localInstClasses) fun l n =>
if l.contains n then l else l.push n
let instClassChanges := child.instClassChanges.foldl (init := parent.instClassChanges) fun l d =>
if l.any (·.1 == d.1) then l else l.push d
{ parent with options, extGens, localInstClasses, instClassChanges }
/-- The maximum number of shared entries, i.e. for different local instances, kept under one key. -/
private def maxSharedEntries : Nat := 64
/-- Whether `a` and `b` recorded the same option lookups with the same answers. -/
private def sameOptionAccesses (a b : SynthInstanceCacheEntry) : Bool :=
a.deps.options == b.deps.options && a.deps.base == b.deps.base
/--
Adds `entry` to `c.raw`, replacing the entries for `key` that recorded the same option lookups with
the same answers: those did not hold in the current environment, or were superseded by `entry`.
-/
private def SynthInstanceCache.insertRaw (c : SynthInstanceCache) (key : SynthInstanceCacheKey)
(entry : SynthInstanceCacheEntry) : SynthInstanceCache :=
{ c with raw := c.raw.alter key fun entries? =>
some <| entry :: (entries?.getD [] |>.filter (!sameOptionAccesses · entry)) }
/--
Adds `entry` to `c.shared` like `insertRaw`, replacing only the entries for the same local
instances, and keeping at most `maxSharedEntries` under `key`, the newest.
-/
private def SynthInstanceCache.insertShared (c : SynthInstanceCache) (key : SynthInstanceCacheKey)
(entry : SynthInstanceSharedEntry) : SynthInstanceCache :=
{ c with shared := c.shared.alter key fun entries? =>
let rest := entries?.getD [] |>.filter fun e =>
!sameOptionAccesses e.toSynthInstanceCacheEntry entry.toSynthInstanceCacheEntry ||
e.relevant != entry.relevant
some <| entry :: rest.take (maxSharedEntries - 1) }
/-- Applies `f` to the transient tier, and to the persistent tier if `persist` is set. -/
private def modifySynthCache (f : SynthInstanceCache → SynthInstanceCache) (persist : Bool) :
MetaM Unit := do
if persist then
-- not `Meta.modifyEnv`, which would clear `Meta.Cache`
modifyThe Core.State fun s => { s with env := s.env.setSynthCache (f s.env.synthCache) }
modifyCache fun c => { c with synthInstance := f c.synthInstance }
/--
Returns the entry for `key` whose recorded dependencies hold in the current context, if any, from the
transient tier or else the persistent one.
-/
private def findCachedResult? (key : SynthInstanceCacheKey) : MetaM (Option SynthInstanceCacheEntry) := do
-- unrestricted: compared against the recorded lookups
let opts ← getOptionsUnrestricted
let env ← getEnv
let find? (c : SynthInstanceCache) := c.raw.find? key |>.bind fun entries =>
entries.find? fun e => validOptionAccesses opts e.deps && validEnvDeps env e.deps
let (entry, persistent) ← if let some entry := find? (← get).cache.synthInstance then
pure (entry, false)
else if let some entry := find? env.synthCache then
pure (entry, true)
else
return none
if entry.deps.baseTrackedGen != env.trackedGen ||
entry.deps.baseChangeLogPos != env.declChangeLog.size then
-- Re-stamped, so that later lookups skip the environment checks just done.
let entry := { entry with deps := { entry.deps with
baseTrackedGen := env.trackedGen, baseChangeLogPos := env.declChangeLog.size } }
if persistent then
modifyThe Core.State fun s =>
{ s with env := s.env.setSynthCache (s.env.synthCache.insertRaw key entry) }
else
modifyCache fun c => { c with synthInstance := c.synthInstance.insertRaw key entry }
return entry
/--
Auxiliary function for converting a cached `AbstractMVarsResult` returned by `SynthInstance.main` into an `Expr`.
This function tries to avoid the potentially expensive `check` at `applyCachedAbstractResult?`.
-/
private def applyCachedAbstractResult? (type : Expr) (abstResult? : Option AbstractMVarsResult) : MetaM (Option Expr) := do
let some abstResult := abstResult? | return none
if abstResult.numMVars == 0 && abstResult.paramNames.isEmpty then
/-
Result does not introduce new metavariables, thus we don't need to perform (again)
the `check` at `applyAbstractResult?`.
This is an optimization.
-/
unless (← assignOutParams type abstResult.expr) do
return none
return some abstResult.expr
else
applyAbstractResult? type abstResult?
/-!
Free-variable normalization of the cache key and result. Queries that are structurally identical up
to the identities of their free variables (e.g. `Foo α` under `[Foo α]` vs. `Foo β` under
`[Foo β]`) share a cache entry: every free variable reachable from the query type and the local
instances of the consulted classes (`SynthInstanceSharedEntry.relevant`) is abstracted to a loose
bound variable by its canonical position, and the result is stored abstracted in the same way and
re-instantiated with the current context's free variables on a hit.
This is sound because a hit means the normalized components are `BEq`-equal, i.e. the two contexts
are identical up to free-variable renaming, and the synthesized result only mentions free variables
in that closure. Queries that cannot be soundly normalized (see `addFVars`) are only cached under
their raw key.
-/
namespace SynthNorm
private structure State where
/-- The free variables in `idx2fvar`. Persistent, so that extending a shared state
(`extendJoint?`) does not copy it. -/
fvarSet : PersistentHashSet FVarId := {}
/-- The closure's free variables by canonical position, each after the variables its type
mentions. -/
idx2fvar : Array Expr := #[]
/-- The type of the free variable at each canonical position, abstracted over the preceding
variables. -/
types : Array Expr := #[]
/-- Set when the closure cannot be soundly normalized; see `addFVars`. -/
bail : Bool := false
private abbrev M := StateT State MetaM
/--
Adds the free variables of `e` to the closure, and transitively those of their types. Sets `bail` on
* a let-bound variable, whose value is visible to definitional unfolding and would have to be part
of the key, and
* a variable whose type contains an unassigned metavariable, which is not context-free.
-/
private partial def addFVars (e : Expr) : M Unit := do
unless e.hasFVar do return
for id in (collectFVars {} e).fvarIds do
if (← get).bail then return
if (← get).fvarSet.contains id then continue
let decl ← id.getDecl
-- NOTE: A nondependent `ldecl` (`have`) has `none` as `value?` but as it hides its value from
-- definitional unfolding as well, it is safe to consider it a value-less ldecl.
if decl.value?.isSome then
modify fun s => { s with bail := true }
return
-- `Expr.hasMVar` is a syntactic flag: it stays set for metavariables that are already
-- assigned, whose values are context-free. Instantiate before deciding to bail.
let mut type := decl.type
if type.hasMVar then
type ← instantiateMVars type
if type.hasMVar then
modify fun s => { s with bail := true }
return
addFVars type
if (← get).bail then return
modify fun s =>
{ s with fvarSet := s.fvarSet.insert id, types := s.types.push (type.abstract s.idx2fvar),
idx2fvar := s.idx2fvar.push (.fvar id) }
/-- The free-variable normalization of a query type and local instances; see `extendJoint?`. -/
private structure Context where
normType : Expr
canonLocalInsts : LocalInstances
fvarTypes : Array Expr
idx2fvar : Array Expr
/--
The normalization state of the free variables of `type` (and of their types, transitively), or
`none` where `addFVars` bails.
-/
private def normalizeType? (type : Expr) : MetaM (Option State) := do
let (_, st) ← (addFVars type).run {}
return if st.bail then none else some st
/--
Normalizes the free variables of `type` and of the local instances `rel` jointly, from the state
`typeSt` that `normalizeType? type` computed: those of `type` take the first canonical positions, so
the result for `rel := #[]` is a key for `type` alone. Returns `none` where `addFVars` bails.
-/
private def extendJoint? (typeSt : State) (type : Expr) (rel : LocalInstances) : MetaM (Option Context) := do
let (_, st) ← (rel.forM fun (li : LocalInstance) =>
-- The key only covers the types of free variables. A local instance that is another term, such
-- as a metavariable (see `LocalInstance.fvar`), can have a type mentioning other variables.
if li.fvar.isFVar then addFVars li.fvar else modify fun s => { s with bail := true }).run typeSt
if st.bail then return none
let canonLocalInsts := rel.map fun (li : LocalInstance) => { li with fvar := li.fvar.abstract st.idx2fvar }
return some { normType := type.abstract st.idx2fvar, canonLocalInsts, fvarTypes := st.types,
idx2fvar := st.idx2fvar }
end SynthNorm
/--
Looks up an entry for `baseKey` (the query keyed by its type alone) whose relevant local instances
match those of the current context, see `SynthInstanceSharedEntry`. Returns the entry and
the joint normalization of the current context, against which its result is abstracted.
-/
private def findRelevantResult? (baseKey : SynthInstanceCacheKey) (typeSt : SynthNorm.State) (type : Expr)
(localInsts : LocalInstances) :
MetaM (Option (SynthInstanceSharedEntry × SynthNorm.Context)) := do
let opts ← getOptionsUnrestricted
let env ← getEnv
let candidates :=
((← get).cache.synthInstance.shared.find? baseKey).getD [] ++
(env.synthCache.shared.find? baseKey).getD []
-- Entries for different contexts mostly share their classes: normalize once per class set.
let mut ctxs : Array (Array Name × Option SynthNorm.Context) := #[]
for e in candidates do
let rel := e.relevant
unless validOptionAccesses opts e.deps && validEnvDeps env e.deps do continue
let ctx? ← match ctxs.find? (·.1 == rel.classes) with
| some (_, ctx?) => pure ctx?
| none =>
let ctx? ← SynthNorm.extendJoint? typeSt type (localInsts.filter fun li => rel.classes.contains li.className)
ctxs := ctxs.push (rel.classes, ctx?)
pure ctx?
let some ctx := ctx? | continue
if ctx.canonLocalInsts == rel.canonLocalInsts && ctx.fvarTypes == rel.fvarTypes then
return some (e, ctx)
return none
/--
The value cached for a search result `abstResult?`, given the result `result?` of applying it to the
query.
-/
private def cacheValue? (kind : PreprocessKind) (abstResult? : Option AbstractMVarsResult)
(result? : Option Expr) : Option AbstractMVarsResult :=
-- A closed result is stored with an empty `AbstractMVarsResult`, so that
-- `applyCachedAbstractResult?` skips the `check`.
abstResult?.bind fun abstResult =>
if abstResult.numMVars == 0 && abstResult.paramNames.isEmpty && kind matches .noMVars | .mvarsNoOutputParams then
result?.map fun result => { expr := result, paramNames := #[], mvars := #[] }
else
some abstResult
/--
The dependencies stored with an entry for the recorded log `log`: with sorted option names and `base`
restricted to them, so that equal logs record the same lookups with the same answers and an entry
does not keep the full options alive.
-/
private def entryDeps (log : RecordedDeps) : RecordedDeps :=
let options := log.options.qsort Name.quickLt
let base := options.foldl (init := {}) fun b n => match log.base.find? n with
| some v => b.insert n v
| none => b
{ log with options, base }
/-- Helper function for caching synthesized type class instances under the raw key `cacheKey`. -/
private def cacheResult (cacheKey : SynthInstanceCacheKey) (log : RecordedDeps) (kind : PreprocessKind)
(abstResult? : Option AbstractMVarsResult) (result? : Option Expr) : MetaM Unit := do
let value? := cacheValue? kind abstResult? result?
-- Only context-free entries are persisted. A raw key with local instances or free variables
-- cannot recur in a later command, so persisting it would only grow the cache, and would be wrong
-- where a name generator is restarted and a `FVarId` thus denotes another variable (the keys of
-- `cacheRelevantResult` name their free variables by position). Results with metavariables are
-- only valid relative to the metavariable context that created them (e.g. universe metavariables
-- not determined by the key are resolved by ambient constraints).
let persist := kind matches .noMVars && cacheKey.localInsts.isEmpty && !cacheKey.type.hasFVar &&
value?.all fun r => r.numMVars == 0 && r.paramNames.isEmpty && !r.expr.hasFVar
modifySynthCache (·.insertRaw cacheKey { deps := entryDeps log, result? := value? }) persist
/--
Stores the result of a search under `baseKey`, the query keyed by its type alone, for contexts whose
local instances of the classes the search consulted agree with the current ones; see
`SynthInstanceSharedEntry`.
-/
private def cacheRelevantResult (baseKey : SynthInstanceCacheKey) (typeSt : SynthNorm.State) (type : Expr)
(localInsts : LocalInstances)
(log : RecordedDeps) (kind : PreprocessKind) (abstResult? : Option AbstractMVarsResult)
(result? : Option Expr) : MetaM Unit := do
let classes := log.localInstClasses
let rel := localInsts.filter fun li => classes.contains li.className
let some ctx ← SynthNorm.extendJoint? typeSt type rel | return
let value? := (cacheValue? kind abstResult? result?).map fun r => { r with expr := r.expr.abstract ctx.idx2fvar }
-- A result mentioning other free variables is not determined by the key.
if value?.any (·.expr.hasFVar) then return
let closed := value?.all fun r => r.numMVars == 0 && r.paramNames.isEmpty
let relevant : SynthInstanceRelevantLocalInsts :=
{ classes := classes, canonLocalInsts := ctx.canonLocalInsts, fvarTypes := ctx.fvarTypes }
modifySynthCache (·.insertShared baseKey { deps := entryDeps log, result? := value?, relevant })
closed
/--
Panics with `msg` without throwing, which a `panic!` of type `MetaM Unit` would do.
-/
-- Not inlined, as the compiler drops a panic whose value is unused.
@[noinline] private def panicCacheHitDiffers (msg : String) : BaseIO Unit :=
return panic! msg
/--
The `Meta.Config` of every type class resolution query. It replaces the ambient configuration,
which is not part of the cache key, so that e.g. a `canUnfoldPredicateConfig` set by `simp` cannot
leak into cached results.
-/
private def synthInstanceConfig : Config :=
{ isDefEqStuckEx := true, transparency := .instances,
foApprox := true, ctxApprox := true, constApprox := false, univApprox := false }
def synthInstanceCore? (type : Expr) (maxResultSize? : Option Nat := none) : MetaM (Option Expr) := do
-- Inside an enclosing query, this lookup is recorded as its dependency.
let maxResultSize ← match maxResultSize? with
| some n => pure n
| none => getRecordedOption synthInstance.maxSize
-- The dependencies the search observes are recorded into `Core.State.recordedDeps`, which
-- becomes the entry's dependency log (`SynthInstanceCache`). The enclosing query's log, if any, is
-- saved here and merged with this query's on exit, as it observed the result.
let parentRecording := (← readThe Core.Context).isRecordingDeps
-- These are the options `findCachedResult?` below validates entries against, so a lookup is
-- recorded with the answer that validation later compares.
let base ← getOptionsUnrestricted
let env ← getEnv
let parentDeps ← modifyGetThe Core.State fun s => (s.recordedDeps, { s with
recordedDeps := { base, baseTrackedGen := env.trackedGen,
baseChangeLogPos := env.declChangeLog.size, baseConstGen := env.constGen }
env := env.markRecordingStart.setRecordingDeps true })
try
withTheReader Core.Context (fun ctx => { ctx with isRecordingDeps := true }) do
withTraceNode `Meta.synthInstance
(fun _ => return m!"{← instantiateMVars type}") do
withConfig (fun _ => synthInstanceConfig) do
withInTypeClassResolution do
let localInsts ← getLocalInstances
let type ← instantiateMVars type
let { type, cacheKeyType, kind } ← preprocess type
let rawKey : SynthInstanceCacheKey :=
{ localInsts, type := cacheKeyType, synthPendingDepth := (← read).synthPendingDepth,
maxResultSize, optionFlags := (← getOptionFlags),
tracing := (← isTracingEnabledFor `Meta.synthInstance),
isExporting := (← getEnv).isExporting }
let runSearch : MetaM (Option AbstractMVarsResult) :=
withNewMCtxDepth (allowLevelAssignments := true) do
match kind with
| .noMVars =>
/-
**Note**: The expensive `preprocessOutParam` step is morally **not** needed here because
the output params should be uniquely determined by the input params. During type class
resolution, definitional equality only unfolds `[reducible]` and `[instance_reducible]`
declarations. This is a contract with our users to ensure performance is reasonable.
However, the same `OrderDual` declaration that creates problems for `assignOutParams`
also prevents us from using this optimization. As an example, suppose we are trying to
synthesize
```
FunLike F (OrderDual α) (OrderDual β)
```
where the last two arguments of `FunLike` are output parameters. This term has no
metavariables, and it seems natural to skip `preprocessOutParam`, which would replace
the last two arguments with metavariables. However, if we don't replace them,
TC resolution fails because it cannot unfold `OrderDual` since it is semireducible.
**Note**: We should remove `preprocessOutParam` from the following line as soon as
Mathlib refactors `OrderDual`.
-/
SynthInstance.main (← preprocessOutParam type) maxResultSize
| .mvarsNoOutputParams => SynthInstance.main type maxResultSize
| .mvarsOutputParams => SynthInstance.main (← preprocessOutParam type) maxResultSize
-- `debug.synthInstance.checkCacheHits`: the search performs no cache lookup itself, so rerunning
-- it bypasses the entry under test.
let checkHit (served? : Option AbstractMVarsResult) : MetaM Unit := do
-- unrestricted: diagnostics only
unless debug.synthInstance.checkCacheHits.get (← getOptionsUnrestricted) do return
-- The recomputation must not affect the elaboration it checks: its state changes are
-- discarded and its heartbeats are not charged to the enclosing computation.
let heartbeats ← IO.getNumHeartbeats
let fresh? : Except String (Option AbstractMVarsResult) ← withoutModifyingState do
-- A fresh heartbeat budget, and a throwing search counts as a divergence.
try .ok <$> withCurrHeartbeats do
-- Cache hits inside the recomputation are not rechecked in turn, which would be
-- exponential in the nesting depth.
let abstResult? ← withSetOption debug.synthInstance.checkCacheHits false runSearch
-- compared as cached, i.e. after the out-param check
tryCatch (return cacheValue? kind abstResult? (← applyAbstractResult? type abstResult?)) fun ex =>
-- The out-param check depends on the query's output parameters, which are not part of
-- the key, and can be stuck on them. Serving the entry is then stuck in the same way:
-- nothing to compare.
if ex matches .internal .. then return served? else throw ex
catch ex => pure <| .error s!"exception: {← ex.toMessageData.toString}"
IO.setNumHeartbeats heartbeats
-- `toString` rather than the pretty printer, which would read options under recording
let fmt : Option AbstractMVarsResult → String
| none => "none"
| some r => toString r.expr
let mismatch? := match fresh?, served? with
| .error e, _ => some e
| .ok none, none => none
| .ok (some a), some b =>
if a.numMVars == b.numMVars && a.paramNames == b.paramNames && a.expr == b.expr then none
else some (fmt (some a))
| .ok r, _ => some (fmt r)
if let some fresh := mismatch? then
panicCacheHitDiffers s!"type class resolution cache hit differs from recomputation for\n \
{toString type}\ncached: {fmt served?}\nrecomputed: {fresh}\n\
a dependency of the entry was not recorded"
let serve (entry : SynthInstanceCacheEntry) : MetaM (Option Expr) := do
trace[Meta.synthInstance.cache] "cached: {type}"
-- The used entry's dependencies become dependencies of the enclosing query, if any.
if parentRecording then
modifyThe Core.State fun s => { s with recordedDeps := entry.deps.mergeInto s.recordedDeps }
checkHit entry.result?
let result? ← applyCachedAbstractResult? type entry.result?
trace[Meta.synthInstance] "result {result?} (cached)"
return result?
-- A query repeated in the same context is found under the key as it is, without normalizing it.
if let some entry ← findCachedResult? rawKey then
return (← serve entry)
-- The query keyed by its type alone: its entries hold where the local instances of the classes
-- their search consulted agree with the current ones.
let typeSt? ← if kind matches .noMVars then SynthNorm.normalizeType? cacheKeyType else pure none
let base? ← match typeSt? with
| some st => SynthNorm.extendJoint? st cacheKeyType #[]
| none => pure none
let baseKey? := base?.map fun b =>
{ rawKey with localInsts := #[], type := b.normType, normFVarTypes := b.fvarTypes }
if let (some baseKey, some typeSt) := (baseKey?, typeSt?) then
if let some (entry, ctx) ← findRelevantResult? baseKey typeSt cacheKeyType localInsts then
-- Stored under the raw key as well: repeated queries in one context must return the same
-- result object, which re-instantiating the entry on every hit would not, and consumers
-- relying on pointer identity for sharing would pay deep structural work for every copy.
let result? := entry.result?.map fun r =>
{ r with expr := r.expr.instantiateRev ctx.idx2fvar }
let entry : SynthInstanceCacheEntry := { entry.toSynthInstanceCacheEntry with result? }
modifySynthCache (·.insertRaw rawKey entry) (persist := false)
return (← serve entry)
trace[Meta.synthInstance.cache] "new: {type}"
let abstResult? ← runSearch
let result? ← applyAbstractResult? type abstResult?
trace[Meta.synthInstance] "result {result?}"
let log := (← getThe Core.State).recordedDeps
cacheResult rawKey log kind abstResult? result?
if let (some baseKey, some typeSt) := (baseKey?, typeSt?) then
cacheRelevantResult baseKey typeSt cacheKeyType localInsts log kind abstResult? result?
return result?
finally
-- Restore the enclosing accumulator, merging this query's dependencies into it.
modifyThe Core.State fun s => { s with
env := s.env.setRecordingDeps parentRecording
recordedDeps := if parentRecording then s.recordedDeps.mergeInto parentDeps else parentDeps }
def synthInstance? (type : Expr) (maxResultSize? : Option Nat := none) : MetaM (Option Expr) := do
-- unrestricted: profiler collection only
profileitM Exception "typeclass inference" (← getOptionsUnrestricted) (decl := type.getAppFn.constName?.getD .anonymous) do
synthInstanceCore? type maxResultSize?
/--
Return `LOption.some r` if succeeded, `LOption.none` if it failed, and `LOption.undef` if
instance cannot be synthesized right now because `type` contains metavariables. -/
def trySynthInstance (type : Expr) (maxResultSize? : Option Nat := none) : MetaM (LOption Expr) := do
catchInternalId isDefEqStuckExceptionId
(toLOptionM <| synthInstance? type maxResultSize?)
(fun _ => pure LOption.undef)
def throwFailedToSynthesize (type : Expr) : MetaM Expr :=
throwError "failed to synthesize{indentExpr type}{useDiagnosticMsg}"
def synthInstance (type : Expr) (maxResultSize? : Option Nat := none) : MetaM Expr :=
catchInternalId isDefEqStuckExceptionId
(do
let result? ← synthInstance? type maxResultSize?
match result? with
| some result => pure result
| none => throwFailedToSynthesize type)
(fun _ => throwFailedToSynthesize type)
set_option compiler.ignoreBorrowAnnotation true in
@[export lean_synth_pending]
private def synthPendingImp (mvarId : MVarId) : MetaM Bool := withIncRecDepth <| mvarId.withContext do
let mvarDecl ← mvarId.getDecl
match mvarDecl.kind with
| .syntheticOpaque => return false
| _ =>
/- Check whether the type of the given metavariable is a class or not. If yes, then try to synthesize
it using type class resolution. We only do it for `synthetic` and `natural` metavariables. -/
match (← isClass? mvarDecl.type) with
| none =>
return false
| some _ =>
let max ← getRecordedOption maxSynthPendingDepth
if (← read).synthPendingDepth > max then
trace[Meta.synthPending] "too many nested synthPending invocations"
recordSynthPendingFailure mvarDecl.type
return false
else
withIncSynthPending do
trace[Meta.synthPending] "synthPending {mkMVar mvarId}"
let val? ← catchInternalId isDefEqStuckExceptionId (synthInstance? mvarDecl.type (maxResultSize? := none)) (fun _ => pure none)
match val? with
| none =>
return false
| some val =>
if (← mvarId.isAssigned) then
return false
else
mvarId.assign val
return true
register_builtin_option trace.Meta.synthInstance : Bool := {
defValue := false
descr := "track the backtracking attempt to synthesize type class instances"
}
builtin_initialize
registerTraceClass `Meta.synthPending
registerTraceClass `Meta.synthInstance.apply (inherited := true)
registerTraceClass `Meta.synthInstance.instances (inherited := true)
registerTraceClass `Meta.synthInstance.tryResolve (inherited := true)
registerTraceClass `Meta.synthInstance.answer (inherited := true)
registerTraceClass `Meta.synthInstance.resume (inherited := true)
registerTraceClass `Meta.synthInstance.unusedArgs
registerTraceClass `Meta.synthInstance.newAnswer
registerTraceClass `Meta.synthInstance.cache
end Lean.Meta