2

(省略) のように定義された GADT があります。

{-# LANGUAGE StandaloneDeriving #-}
data D t where
    C :: t -> D t
    R :: D b -> D (Either a b)
deriving instance Show t => Show (D t)

コンパイラは、R の Show を導出できないと正しく不平を言います。この場合、(Either ab) は Show クラスですが、b が Show クラスである場合、これが true であることを知ることはできません。エラーメッセージは、

Could not deduce (Show b) from the context (t ~ Either a b)
  arising from a use of `showsPrec' at tmp.hs:37:0-37
Possible fix: add (Show b) to the context of the constructor `R'
In the second argument of `(.)', namely `(showsPrec 11 b1)'
In the second argument of `showParen', namely
    `((.) (showString "R ") (showsPrec 11 b1))'
In the expression:
    showParen ((a >= 11)) ((.) (showString "R ") (showsPrec 11 b1))
When typechecking a standalone-derived method for `Show (D t)':
  showsPrec a (C b1)
              = showParen ((a >= 11)) ((.) (showString "C ") (showsPrec 11 b1))
  showsPrec a (R b1)
              = showParen ((a >= 11)) ((.) (showString "R ") (showsPrec 11 b1))

存在型 ​​b に対して "Show b => Show (D (Either ab))" のようなものを言うか、"(Show a, Show b) = 含意をアップグレードできるようにする必要があるようです。 > Show (Eith ab)」なので、双方向です。

どうもありがとう!

(タイトルや説明は自由に削除してください。)

4

1 に答える 1

4

Show実存に制約を追加し、

data D t where
    C :: t -> D t
    R :: Show b => D b -> D (Either a b)

そしてあなたはビジネスをしています。

Prelude> :r
[1 of 1] Compiling A                ( A.hs, interpreted )
Ok, modules loaded: A.
*A> R (C 7)
R (C 7)
于 2011-05-01T00:48:15.970 に答える