34

我有这个类型定义:

data Operace = Op (Int->Int->Int) String (Int->Int->Int) deriving Show

我想将此类型打印到交互式外壳(GHCi)中。应该打印的只是String字段。

我试过这个:

instance Show Operace where
    show (Op op str inv) = show str

但我仍然不断得到

No instance for (Show (Int -> Int -> Int))
  arising from the 'deriving' clause of a data type declaration
Possible fix:
  add an instance declaration for (Show (Int -> Int -> Int))
  or use a standalone 'deriving instance' declaration,
       so you can specify the instance context yourself
When deriving the instance for (Show Operace)

我不想添加Showfor (Int->Int->Int),我只想打印字符串。

感谢帮助!

编辑:

为了将来参考,固定版本是:

data Operace = Op (Int->Int->Int) String (Int->Int->Int)

instance Show Operace where
    show (Op _ str _) = str
4

2 回答 2

34

您所做的实例声明是正确的方法。您似乎忘记deriving从原始data声明中删除该错误条款。

data Operace = Op (Int->Int->Int) String (Int->Int->Int)

instance Show Operace where
   show (Op op str inv) = show str
于 2011-05-21T13:43:44.097 回答
30

您可以派生Show,只需先导入Text.Show.Functions

于 2011-05-21T14:22:30.007 回答