次のコードがあるとします。
{-# LANGUAGE GADTs, DeriveDataTypeable, StandaloneDeriving #-}
import Data.Typeable
class Eq t => OnlyEq t
class (Eq t, Typeable t) => BothEqAndTypeable t
data Wrapper a where
Wrap :: BothEqAndTypeable a => a -> Wrapper a
deriving instance Eq (Wrapper a)
deriving instance Typeable1 Wrapper
次に、次のインスタンス宣言が制約なしt
で機能します。
instance OnlyEq (Wrapper t)
そして私が期待することをします。
ただし、次のインスタンス宣言は機能しません。
instance BothEqAndTypeable (Wrapper t)
GHC(私は7.6.1を使用しています)が次のように不平を言っているので:
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)'
Typeable t
もちろん、コンテキストへの追加は機能します。ただし、次のインスタンスを追加することもできます。
instance Typeable (Wrapper t) where
typeOf (Wrap x) = typeOf1 (Wrap x) `mkAppTy` typeOf x
GHCにこの後者のインスタンスを作成させる方法はありますか?もしそうなら、どのように?そうでない場合は、なぜですか?
制約の場合と同じように、GHCがコンストラクターTypeable
のコンテキストから制約をプルできることを期待していました。私の問題は、GHCが明示的に書き込みを禁止しているという事実に要約されていると思います。また、標準インスタンスは辞書を見つけるために「内部を調べる」ことができません。Wrap
Eq
deriving instance Typeable (Wrapper t)
(Typeable1 s, Typeable a) => Typeable (s a)
s a
Typeable a