[Haskell-beginners] Problem with typeclass

Sebastien Geffroy seb at geffroy.info
Sun Jun 20 04:32:46 EDT 2010


Hi all,

Below is an example from the book "Real world haskell"

data JValue = JString String
            | JBool Bool
            | JNumber Int
            deriving(Show)

class JSON a where
    toJValue :: a -> JValue
    fromJValue :: JValue -> Either String a

instance JSON JValue where
    toJValue = id
    fromJValue = Right

instance JSON Bool where
    toJValue = JBool
    fromJValue (JBool a) = Right a
    fromJValue _         = Left "Not a JValue"

I'm trying to test this in ghci :

> fromJValue (JBool True)

and i got
<interactive>:1:0:
    Ambiguous type variable `a' in the constraint:
      `JSON a' arising from a use of `fromJValue' at <interactive>:1:0-22
    Probable fix: add a type signature that fixes these type variable(s)

I don't understand where i have to declare a type signature ? in the class definition for "JSON a" ? in the instance declaration for "JSON Bool" ?

Thanks in advance.
Sheb


More information about the Beginners mailing list