Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Problem with typeclass (Sebastien Geffroy)
2. Re: Problem with typeclass (Stephen Tetley)
----------------------------------------------------------------------
Message: 1
Date: Sun, 20 Jun 2010 10:32:46 +0200
From: Sebastien Geffroy <[email protected]>
Subject: [Haskell-beginners] Problem with typeclass
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
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
------------------------------
Message: 2
Date: Sun, 20 Jun 2010 11:03:02 +0100
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] Problem with typeclass
To: Sebastien Geffroy <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hello Sebastien
Try
> (fromJValue (JBool True)) :: Bool
Or
> (fromJValue (JBool True)) :: JValue
The class function /fromJValue/ is polymorphic on its answer, so you
need to tell GHC / GHCi what type the answer will be. GHC can't infer
it because it could be more than one type.
Best wishes
Stephen
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 24, Issue 24
*****************************************