7

次のコードがあるとします。

{-# 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が明示的に書き込みを禁止しているという事実に要約されていると思います。また、標準インスタンスは辞書を見つけるために「内部を調べる」ことができません。WrapEqderiving instance Typeable (Wrapper t)(Typeable1 s, Typeable a) => Typeable (s a)s aTypeable a

4

1 に答える 1

5
于 2013-03-20T18:50:54.613 に答える