Note [Local instances and incoherence]
Consider
f :: forall b c. (Eq b, forall a. Eq a => Eq (c a))
=> c b -> Bool
f x = x==x
We get [W] Eq (c b), and we must use the local instance to solve it.
BUT that wanted also unifies with the top-level Eq [a] instance,
and Eq (Maybe a) etc. We want the local instance to "win", otherwise
we can't solve the wanted at all. So we mark it as Incohherent.
According to Note [Rules for instance lookup] in GHC.Core.InstEnv, that'll
make it win even if there are other instances that unify.
Moreover this is not a hack! The evidence for this local instance
will be constructed by GHC at a call site... from the very instances
that unify with it here. It is not like an incoherent user-written
instance which might have utterly different behaviour.
Consider f :: Eq a => blah. If we have [W] Eq a, we certainly
get it from the Eq a context, without worrying that there are
lots of top-level instances that unify with [W] Eq a! We'll use
those instances to build evidence to pass to f. That's just the
nullary case of what's happening here. References 1
- Rules for instance lookup GHC.Core.InstEnv
Referenced by 1
- GHC.Tc.Solver.Dict call site