Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I get GHC to generate instances of Data.Typeable for GADTs with Typeable in the context?
    primarykey
    data
    text
    <p>Suppose I have the following code:</p> <pre><code>{-# LANGUAGE GADTs, DeriveDataTypeable, StandaloneDeriving #-} import Data.Typeable class Eq t =&gt; OnlyEq t class (Eq t, Typeable t) =&gt; BothEqAndTypeable t data Wrapper a where Wrap :: BothEqAndTypeable a =&gt; a -&gt; Wrapper a deriving instance Eq (Wrapper a) deriving instance Typeable1 Wrapper </code></pre> <p>Then, the following instance declaration works, <em>without</em> a constraint on <code>t</code>:</p> <pre><code>instance OnlyEq (Wrapper t) </code></pre> <p>and does what I expect it to do.</p> <hr> <p>But the following instance declaration doesn't work:</p> <pre><code>instance BothEqAndTypeable (Wrapper t) </code></pre> <p>since GHC - I'm using 7.6.1 - complains that:</p> <pre><code>No instance for (Typeable t) arising from the superclasses of an instance declaration Possible fix: add (Typeable t) to the context of the instance declaration In the instance declaration for `BothEqAndTypeable (Wrapper t)' </code></pre> <p>Adding <code>Typeable t</code> to the context works, of course. But so does adding the following instance:</p> <pre><code>instance Typeable (Wrapper t) where typeOf (Wrap x) = typeOf1 (Wrap x) `mkAppTy` typeOf x </code></pre> <p>Is there a way to get GHC to write this latter instance for me? If so, how? If not, why not? </p> <p>I was hoping GHC would be able to pull the <code>Typeable</code> constraint from the context on the <code>Wrap</code> constructor, just as it did with the <code>Eq</code> constraint. I think that my problems boils down to the fact that GHC explicitly disallows writing <code>deriving instance Typeable (Wrapper t)</code>, and the standard <code>(Typeable1 s, Typeable a) =&gt; Typeable (s a)</code> instance can't 'look inside' <code>s a</code> to find a <code>Typeable a</code> dictionary.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload