35

I have this type definition:

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

I want to print this type into the interactive shell (GHCi). All that should be printed is the String field.

I tried this:

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

But I still keep getting

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)

I don't want to add Show for (Int->Int->Int), all I want to print is the string.

Thanks for help!

EDIT:

For future reference, the fixed version is:

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

instance Show Operace where
    show (Op _ str _) = str
Joe the Person
  • 4,470
  • 3
  • 22
  • 32
Matěj Zábský
  • 16,909
  • 15
  • 69
  • 114

2 Answers2

35

The instance declaration you made is the correct way to go. It seems you forgot to remove that faulty deriving clause from the original data declaration.

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

instance Show Operace where
   show (Op op str inv) = show str
hugomg
  • 68,213
  • 24
  • 160
  • 246
  • 1
    Yes, that was it, thanks. I expected it would work like in C-like languages, where you declare list of parent types in class definition. – Matěj Zábský May 21 '11 at 13:50
  • 1
    @mzabsky: This is a powerful mechanism, as you can define additional type class instances for an existing type without touching its definition. – hammar May 21 '11 at 19:34
30

You can derive Show, just import Text.Show.Functions first.

Thomas M. DuBuisson
  • 64,245
  • 7
  • 109
  • 166