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. Re:  Convert my own types to the Haskell types using
      typeclass (David McBride)
   2. Re:  Convert my own types to the Haskell types using
      typeclass (Magnus Therning)
   3. Re:  Convert my own types to the Haskell types using
      typeclass (ke dou)


----------------------------------------------------------------------

Message: 1
Date: Fri, 11 Apr 2014 13:22:29 -0400
From: David McBride <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Convert my own types to the Haskell
        types using typeclass
Message-ID:
        <CAN+Tr40soFtiQ2cmVTJv21_=T5haiPT=x=g=roxhwbz8anf...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

There is nothing to fix.  It is doing exactly as you intended.  You are
confusing types vs values.  Integer is a type, 1, 2, and 3 are values.

toHaskell takes a type (MyOption a) and converts it to a type (Maybe a).
But in your actual code you will use values ie. toHaskell (Some 'a' ::
MyOption Char) and get back a (Just 'a' :: Maybe Char).


On Fri, Apr 11, 2014 at 1:13 PM, ke dou <[email protected]> wrote:

> Thank you, Bob.
> I also noticed that, but don't know how to fix it.
> Any suggestions?
>
> --Ke
>
>
> On Fri, Apr 11, 2014 at 1:05 PM, Bob Ippolito <[email protected]> wrote:
>
>> On Fri, Apr 11, 2014 at 9:50 AM, ke dou <[email protected]> wrote:
>>
>>> Hi,
>>>
>>> I want to define a typeclass that can convert my own types like MyBool,
>>> MyInt, MyOption to according Haskell types -- Bool, Int, Maybe.
>>>
>>> Currently I can convert the first two types, but for MyOption, I don't
>>> how to do, since it is a polymophic type.
>>>
>>> Here is my toy program:
>>> --------------------------------------------------------------
>>> {-# LANGUAGE TypeFamilies #-}
>>>
>>> module Coercible where
>>>
>>> import qualified Prelude
>>>
>>> data MyBool = MyTrue | MyFalse
>>> data MyInt = Zero | One | Two
>>> data MyOption a =
>>>    Some a
>>>  | None
>>>
>>> class Coercible a where
>>>   type Return a :: *
>>>   toHaskell :: a -> (Return a)
>>>
>>> instance Coercible MyBool where
>>>   type Return MyBool = Prelude.Bool
>>>   toHaskell MyTrue = Prelude.True
>>>   toHaskell MyFalse = Prelude.False
>>>
>>> instance Coercible MyInt where
>>>   type Return MyInt = Prelude.Int
>>>   toHaskell Zero = 0
>>>   toHaskell One = 1
>>>   toHaskell Two = 2
>>>
>>> instance Coercible (MyOption a) where
>>>   type Return (MyOption a) = Prelude.Maybe a
>>>   toHaskell (Some a) = Prelude.Just a
>>>   toHaskell None = Prelude.Nothing
>>>
>>> --------------------------------------------------------------------------
>>> The problem occurs when I am trying to use "toHaskell (Some MyBool)",
>>> the error message is "Not in scope: data constructor `MyBool'".
>>> Any hints will be appreciated !!
>>>
>>
>> `Some MyBool` isn't valid Haskell. MyBool is the name of a type, there's
>> no binding or constructor with that name. `toHaskell (Some MyTrue)`
>> evaluates to `Just MyTrue` as expected.
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20140411/f37876fd/attachment-0001.html>

------------------------------

Message: 2
Date: Fri, 11 Apr 2014 20:43:18 +0200
From: Magnus Therning <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Convert my own types to the Haskell
        types using typeclass
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"

On Fri, Apr 11, 2014 at 12:50:45PM -0400, ke dou wrote:
> instance Coercible (MyOption a) where
>   type Return (MyOption a) = Prelude.Maybe a
>   toHaskell (Some a) = Prelude.Just a
>   toHaskell None = Prelude.Nothing

The basic issue has been solved in another thread, but wouldn't you
want this one to be something like this:

instance Coercible a => Coercible (MyOption a) where
    type Return (MyOption a) = Prelude.Maybe (Return a)
    toHaskell (Some a) = Prelude.Just (toHaskell a)
    toHaskell None = Prelude.Nothing

/M

-- 
Magnus Therning                      OpenPGP: 0xAB4DFBA4 
email: [email protected]   jabber: [email protected]
twitter: magthe               http://therning.org/magnus

What gets measured, gets done.
     -- Tom Peters
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: not available
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20140411/e06e239a/attachment-0001.sig>

------------------------------

Message: 3
Date: Fri, 11 Apr 2014 16:15:48 -0400
From: ke dou <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Convert my own types to the Haskell
        types using typeclass
Message-ID:
        <CAG1_UCTq3ek7=tTFR=1HonGtZifra=NFRH8u=nnw7g+hbol...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Thank you very much!
That is exactly what I want.

Best,
Ke

On Fri, Apr 11, 2014 at 2:43 PM, Magnus Therning <[email protected]>wrote:

> On Fri, Apr 11, 2014 at 12:50:45PM -0400, ke dou wrote:
> > instance Coercible (MyOption a) where
> >   type Return (MyOption a) = Prelude.Maybe a
> >   toHaskell (Some a) = Prelude.Just a
> >   toHaskell None = Prelude.Nothing
>
> The basic issue has been solved in another thread, but wouldn't you
> want this one to be something like this:
>
> instance Coercible a => Coercible (MyOption a) where
>     type Return (MyOption a) = Prelude.Maybe (Return a)
>     toHaskell (Some a) = Prelude.Just (toHaskell a)
>     toHaskell None = Prelude.Nothing
>
> /M
>
> --
> Magnus Therning                      OpenPGP: 0xAB4DFBA4
> email: [email protected]   jabber: [email protected]
> twitter: magthe               http://therning.org/magnus
>
> What gets measured, gets done.
>      -- Tom Peters
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20140411/791736f8/attachment-0001.html>

------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 70, Issue 25
*****************************************

Reply via email to