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 に答える