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?

1
  • BTW, this is a type error, not a syntax error. Commented Oct 12, 2015 at 13:22

1 Answer 1

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 ++ ")"
Sign up to request clarification or add additional context in comments.

1 Comment

That's it. Thanks. I should re-read some things about instances, probably.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.