Send Beginners mailing list submissions to
        beginners@haskell.org

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
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

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 <s...@geffroy.info>
Subject: [Haskell-beginners] Problem with typeclass
To: beginners@haskell.org
Message-ID: <13d082e6-6431-470d-98dd-423f2e9c6...@geffroy.info>
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 <stephen.tet...@gmail.com>
Subject: Re: [Haskell-beginners] Problem with typeclass
To: Sebastien Geffroy <s...@geffroy.info>
Cc: beginners@haskell.org
Message-ID:
        <aanlktimnva_dnzyoey8ofyzwjxja_jdfsrmcol6p8...@mail.gmail.com>
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
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 24, Issue 24
*****************************************

Reply via email to