How do I determine why ghc is looking for a particular type class instance? : <p>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:</p>

<pre><code>class MyClass1 c
class MyClass2 c
class MyClass3 c

data MyType1 a
data MyType2 a

instance MyClass1 a =&gt; MyClass2 (MyType1 a)
instance MyClass2 a =&gt; MyClass3 (MyType2 a)

foo :: (MyClass3 c) =&gt; c
foo = undefined

bar :: MyType2 (MyType1 Int)
bar = foo
</code></pre>

<p>GHC gives the following error:</p>

<pre><code>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
    |       ^^^
</code></pre>

<p>Supposing I only wrote the definitions for <code>foo</code> and <code>bar</code>, 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 <code>MyClass1</code> instance for <code>Int</code>. This might even be the first time I've ever heard of the <code>MyClass1</code> 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.</p>

<pre><code>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
</code></pre>

<p>Does ghc have a command line option for this? If not, how do I debug this?</p>

<p>Keep in mind that my real problem is much more complicated than this simple example. e.g.</p>

<pre><code>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 :: * -&gt; *) (m :: *
                                                                                   -&gt; *).
                                                  (PP gc (IndexedProblem ac),
                                                   Show
                                                     (Vars
                                                        (DedupingMap
                                                           (Rep (Index gc)) (IndexedProblem ac))),
                                                   Foldable f, MonadNotify m) =&gt;
                                                  f (Index
                                                       (Clzs
                                                          (PartitionedProblem
                                                             gc (IndexedProblem ac))))
                                                  -&gt; 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
    |                               ^^^^^^
</code></pre>

<p>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?</p>
