When conditional type class instances run deep, it can be difficult to figure out why ghc is complaining about a missing type class instance. For instance:
class MyClass1 c
class MyClass2 c
class MyClass3 c
data MyType1 a
data MyType2 a
instance MyClass1 a => MyClass2 (MyType1 a)
instance MyClass2 a => MyClass3 (MyType2 a)
foo :: (MyClass3 c) => c
foo = undefined
bar :: MyType2 (MyType1 Int)
bar = foo
GHC gives the following error:
Example.hs:149:7-9: error:
• No instance for (MyClass1 Int) arising from a use of ‘foo’
• In the expression: foo
In an equation for ‘bar’: bar = foo
|
149 | bar = foo
| ^^^
Supposing I only wrote the definitions for foo
and bar
, and everything else was imported code which I did not write, I may be very confused as to why ghc is attempting to find a MyClass1
instance for Int
. This might even be the first time I've ever heard of the MyClass1
class, if so far I've been relying on imported instances. It would be nice if ghc could give me a "stack trace" of the type class instance chain, e.g.
Sought (MyClass2 (MyType1 Int)) to satisfy (MyClass3 (MyType2 (MyType1 Int))) from conditional type class instance OtherModule.hs:37:1-18
Sought (MyClass1 Int) to satisfy (MyClass2 (MyType1 Int)) from conditional type class instance OtherModule.hs:36:1-18
Does ghc have a command line option for this? If not, how do I debug this?
Keep in mind that my real problem is much more complicated than this simple example. e.g.
Search.hs:110:31-36: error:
• Could not deduce (Ord
(Vars (DedupingMap (Rep (Index gc)) (IndexedProblem ac))))
arising from a use of ‘search’
from the context: (PP gc (IndexedProblem ac),
Show (Vars (DedupingMap (Rep (Index gc)) (IndexedProblem ac))),
Foldable f, MonadNotify m)
bound by the type signature for:
searchIndexedReplicaProblem :: forall gc ac (f :: * -> *) (m :: *
-> *).
(PP gc (IndexedProblem ac),
Show
(Vars
(DedupingMap
(Rep (Index gc)) (IndexedProblem ac))),
Foldable f, MonadNotify m) =>
f (Index
(Clzs
(PartitionedProblem
gc (IndexedProblem ac))))
-> m (Maybe
(Vars
(PartitionedProblem
gc (IndexedProblem ac))))
at Search.hs:(103,1)-(109,131)
• In the expression: search
In an equation for ‘searchIndexedReplicaProblem’:
searchIndexedReplicaProblem = search
|
110 | searchIndexedReplicaProblem = search
| ^^^^^^
There are five coverage conditions for PP, and I'm using type families and undecidable instances, so it's extremely not obvious why ghc is giving me its error. What tools can I use to track down the problem?