1

再帰的なデータ定義があります:

data Checked a =  forall b. Checked (Either (Warning, Maybe (Checked b), a) a)

Show を再帰的に定義する必要があります。

instance (Show a) => Show (Checked a) where
  show (Right v) = show v
  show (Left (w, Nothing, v) = show w ++ show v
  show (Left (w, Just ch, v) = show w ++ show v ++ "caused by" ++ show ch --recursive here

GHCが与える

 Could not deduce (Show b) arising from a use of `show'
  from the context (Show a)
  bound by the instance declaration at Checked.hs:29:10-35
  Possible fix:
   add (Show b) to the context of
    the data constructor `Checked'
    or the instance declaration
  In the second argument of `(++)', namely `show ch'

インスタンス定義の制約に (Show b) を追加すると、GHC は次のようになります。

 Ambiguous constraint `Show b'
  At least one of the forall'd type variables mentioned by the constraint
  must be reachable from the type after the '=>'
In the instance declaration for `Show (Checked a)'

これをコンパイルするために取るべき次のステップはありますか?

4

2 に答える 2

5

Show bデータ型に制限を追加する必要があります。

data Checked a = forall b. Show b => Checked (Either (Warning, Maybe (Checked b), a) a)

instance Show a => Show (Checked a) where
  show (Checked (Right v)) = show v
  show (Checked (Left (w, Nothing, v))) = show w ++ show v
  show (Checked (Left (w, Just ch, v))) = show w ++ show v ++ "caused by" ++ show ch
于 2012-10-24T09:46:42.157 に答える
3

Showデータ型定義に制約を追加します。

data Checked a =  forall b. Show b => Checked (Either (Warning, Maybe (Checked b), a) a)

制約の種類を次のように使用することもできます

{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE ExistentialQuantification #-}

data Checked a c =  forall b. c b =>  Checked (Either (Maybe (Checked b c), a) a)

instance (Show a) => Show (Checked a Show) where
 show (Checked (Right v)) = show v
 show (Checked (Left (Nothing, v))) = show v
 show (Checked (Left (Just ch, v))) = show v ++ "caused by" ++ show ch
于 2012-10-24T09:52:47.997 に答える