Matěj Zábský
Matěj Zábský

Reputation: 17272

Haskell: Deriving Show for custom type

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

Upvotes: 35

Views: 48991

Answers (2)

Thomas M. DuBuisson
Thomas M. DuBuisson

Reputation: 64750

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

Upvotes: 31

hugomg
hugomg

Reputation: 69964

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

Upvotes: 35

Related Questions