0

I'm trying to make a pair an instance of Printable, but I can't figure out the correct syntax. My Printable is this:

class Printable a where
    toString :: a -> [Char]

instance Printable Bool where
    toString True = "true"
    toString False = "false"
instance Printable () where
    toString () = "unit type"

and my instance for pairs is this:

instance  Printable ( a, b ) where
    toString (a,b) = "(" ++ toString a ++ ","++ toString b ++ ")"

which, upon compiling, gives me a No instance for (Printable a) arising from a use of ‘toString’. What am I doing wrong?

Chiffa
  • 1,486
  • 2
  • 19
  • 38

1 Answers1

5

You need a and b to be instances of Printable:

instance (Printable a, Printable b) => Printable ( a, b ) where
    toString (a,b) = "(" ++ toString a ++ ","++ toString b ++ ")"
Lee
  • 142,018
  • 20
  • 234
  • 287