Haskell:为自定义类型导出显示

时间:2011-05-21 13:38:56

标签: haskell show typeclass ghci

我有这种类型的定义:

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

我想将此类型打印到交互式shell(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)

我不想为Show添加(Int->Int->Int),我想要打印的只是字符串。

感谢您的帮助!

修改

供将来参考,固定版本为:

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

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

2 个答案:

答案 0 :(得分:22)

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

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

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

答案 1 :(得分:21)

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