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:  Typeclasses and "inheritance" (Chadda? Fouch?)
   2.  uses of random number generators (Michael P Mossey)
   3. Re:  uses of random number generators (Brandon S. Allbery KF8NH)
   4.  Re: uses of random number generators (Aditya Mahajan)
   5.  Question about class declaration
      (Rafael Gustavo da Cunha Pereira Pinto)
   6. Re:  Question about class declaration (Brent Yorgey)
   7. Re:  Question about class declaration (Felipe Lessa)
   8. Re:  Question about class declaration
      (Rafael Gustavo da Cunha Pereira Pinto)
   9. Re:  Typeclasses and "inheritance" (Patrick LeBoutillier)


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

Message: 1
Date: Thu, 23 Jul 2009 22:26:07 +0200
From: Chadda? Fouch? <[email protected]>
Subject: Re: [Haskell-beginners] Typeclasses and "inheritance"
To: Patrick LeBoutillier <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

On Thu, Jul 23, 2009 at 10:18 PM, Chaddaï
Fouché<[email protected]> wrote:
> Your instance would look like that :
>
>> instance IPAddr IPv4Addr IPv4Host IPv4Mask where
>>   type Host IPv4Addr = IPv4Host
>>   type Mask IPv4Addr = IPv4Mask
>>   host (IPv4Addr h _) = h
>>   mask (IPv4Addr _ m) = m

Oops... I forgot to trim the extra parameters !

> instance IPAddr IPv4Addr where
>   type Host IPv4Addr = IPv4Host
>   type Mask IPv4Addr = IPv4Mask
>   host (IPv4Addr h _) = h
>   mask (IPv4Addr _ m) = m

One advantage compared to the multiparameter + functional dependencies
solution is that you can write :
(IPAddr a) => ...
In your context rather than introducing h and m when they're not needed.

If you need to write "Host a" several time in a function, you can put
the following in your context :
(IPAdrr a, Host a ~ h) => ...
and use h for Host a thereafter.

-- 
Jedaï


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

Message: 2
Date: Thu, 23 Jul 2009 22:35:04 -0700
From: Michael P Mossey <[email protected]>
Subject: [Haskell-beginners] uses of random number generators
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Hello,

I have an application for random numbers. So far, in my journey with Haskell, I 
have learned basics and a few things about monads, and I was hoping I could get 
some guidance how to employ random # gens.

I need to run some simulated experiments to calculate some statistics by a 
Monte 
Carlo method.

There are several components to the experiment, but one component is:

- Given a hat with 5 red squares and 5 blue squares, draw one square at a time, 
note its color, for a total of 8 squares. Remember the order of the 8 squares.

Another function will take two inputs each of which is "red" or "blue". If they 
are the same color, return the other color. Otherwise "toss a coin" to 
determine 
which color to return.


-- this uses Bool rather than "red" "blue" for obvious reasons.
balanceColors :: Bool -> Bool -> Bool
balanceColors c1 c2 =
     if c1 == c2
       then not c1
       else -- flip coin --

Can I get a few examples or pointers? I believe I will have to run this in a 
State monad or the IO monad, will I not?

Thanks,
Mike


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

Message: 3
Date: Fri, 24 Jul 2009 01:42:48 -0400
From: "Brandon S. Allbery KF8NH" <[email protected]>
Subject: Re: [Haskell-beginners] uses of random number generators
To: Michael P Mossey <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"

On Jul 24, 2009, at 01:35 , Michael P Mossey wrote:
> Can I get a few examples or pointers? I believe I will have to run  
> this in a State monad or the IO monad, will I not?


IO is only needed to get an initial seed.  The standard random number  
functions are designed for use with State, but you might also want to  
look at http://hackage.haskell.org/package/MonadRandom .

-- 
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] [email protected]
system administrator [openafs,heimdal,too many hats] [email protected]
electrical and computer engineering, carnegie mellon university    KF8NH


-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 195 bytes
Desc: This is a digitally signed message part
Url : 
http://www.haskell.org/pipermail/beginners/attachments/20090724/8e8d82cb/PGP-0001.bin

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

Message: 4
Date: Fri, 24 Jul 2009 02:05:16 -0400
From: Aditya Mahajan <[email protected]>
Subject: [Haskell-beginners] Re: uses of random number generators
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed

On Thu, 23 Jul 2009, Michael P Mossey wrote:

> Hello,
>
> I have an application for random numbers. So far, in my journey with Haskell, 
> I have learned basics and a few things about monads, and I was hoping I could 
> get some guidance how to employ random # gens.
>
> I need to run some simulated experiments to calculate some statistics by a 
> Monte Carlo method.
>
> Can I get a few examples or pointers? I believe I will have to run this in a 
> State monad or the IO monad, will I not?

Have a look at the monte-carlo monad: 
http://hackage.haskell.org/cgi-bin/hackage-scripts/package/monte-carlo

For example usages see 
http://quantile95.com/2008/12/31/monte-carlo-poker-odds/ and 
http://randomdeterminism.wordpress.com/2009/01/06/monty-hall-problem-using-monte-carlo-simulations/

Aditya



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

Message: 5
Date: Fri, 24 Jul 2009 09:41:34 -0300
From: Rafael Gustavo da Cunha Pereira Pinto
        <[email protected]>
Subject: [Haskell-beginners] Question about class declaration
To: beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

Hi folks,

Once in a while I stumble upon declarations like this:

class EditAction e a | e -> a where
  apply :: a -> e -> a



Why is the vertical bar there? I understand that => defines a context, but
can't find what is the "|" for.


Thanks


Rafael Gustavo da Cunha Pereira Pinto
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20090724/79212b47/attachment-0001.html

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

Message: 6
Date: Fri, 24 Jul 2009 08:44:32 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Question about class declaration
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Fri, Jul 24, 2009 at 09:41:34AM -0300, Rafael Gustavo da Cunha Pereira Pinto 
wrote:
> Hi folks,
> 
> Once in a while I stumble upon declarations like this:
> 
> class EditAction e a | e -> a where
>   apply :: a -> e -> a
> 
> 
> 
> Why is the vertical bar there? I understand that => defines a context, but
> can't find what is the "|" for.

It's a functional dependency: the e -> a means that the type e
determines the type a, that is, there can only be one instance of
EditAction for any particular type e.

-Brent


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

Message: 7
Date: Fri, 24 Jul 2009 09:57:47 -0300
From: Felipe Lessa <[email protected]>
Subject: Re: [Haskell-beginners] Question about class declaration
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Fri, Jul 24, 2009 at 08:44:32AM -0400, Brent Yorgey wrote:
> On Fri, Jul 24, 2009 at 09:41:34AM -0300, Rafael Gustavo da Cunha Pereira 
> Pinto wrote:
> > class EditAction e a | e -> a where
> >   apply :: a -> e -> a
>
> It's a functional dependency: the e -> a means that the type e
> determines the type a, that is, there can only be one instance of
> EditAction for any particular type e.

Alternatively you could write that class using a type family:

class EditAction e where
  type Text e :: *
  apply :: Text e -> e -> Text e

(I don't know if "Text e" is a good name, but Haskell-wise this
should do the same trick as the fundep class.)

One example of an instance (out of the top of my head):

data Editor = Editor {text :: String, ...}
data EditorAction = Prepend String | ...
instance EditAction EditorAction where
  type Text EditorAction = Editor
  apply editor (Prepend s) = editor {text = s ++ text editor}

HTH,

--
Felipe.


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

Message: 8
Date: Fri, 24 Jul 2009 10:53:08 -0300
From: Rafael Gustavo da Cunha Pereira Pinto
        <[email protected]>
Subject: Re: [Haskell-beginners] Question about class declaration
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

So, it is a language extension as I predicted. A cool one, by the way.

Thank you all!



On Fri, Jul 24, 2009 at 09:44, Brent Yorgey <[email protected]> wrote:

> On Fri, Jul 24, 2009 at 09:41:34AM -0300, Rafael Gustavo da Cunha Pereira
> Pinto wrote:
> > Hi folks,
> >
> > Once in a while I stumble upon declarations like this:
> >
> > class EditAction e a | e -> a where
> >   apply :: a -> e -> a
> >
> >
> >
> > Why is the vertical bar there? I understand that => defines a context,
> but
> > can't find what is the "|" for.
>
> It's a functional dependency: the e -> a means that the type e
> determines the type a, that is, there can only be one instance of
> EditAction for any particular type e.
>
> -Brent
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>



-- 
Rafael Gustavo da Cunha Pereira Pinto
Electronic Engineer, MSc.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20090724/ea716dba/attachment-0001.html

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

Message: 9
Date: Fri, 24 Jul 2009 12:16:26 -0400
From: Patrick LeBoutillier <[email protected]>
Subject: Re: [Haskell-beginners] Typeclasses and "inheritance"
To: Chadda? Fouch? <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

Hi,

On Thu, Jul 23, 2009 at 4:26 PM, Chaddaï Fouché <[email protected]>wrote:

> On Thu, Jul 23, 2009 at 10:18 PM, Chaddaï
> Fouché<[email protected]> wrote:
> > Your instance would look like that :
>
> Oops... I forgot to trim the extra parameters !
>
> > instance IPAddr IPv4Addr where
> >   type Host IPv4Addr = IPv4Host
> >   type Mask IPv4Addr = IPv4Mask
> >   host (IPv4Addr h _) = h
> >   mask (IPv4Addr _ m) = m
>

This is great stuff and exactly what I wanted. Thank you very much guys!

I kept on hacking at it and so far I have this code:
http://hpaste.org/fastcgi/hpaste.fcgi/view?id=7428
which unfortunately doesn't  compile:

Net/IP.hs:44:39:
    Couldn't match expected type `Word (Host a)'
           against inferred type `Word (Mask a)'
    In the second argument of `($)', namely `(bits h) .&. (bits m)'
    In the first argument of `makeIPAddr', namely
        `(fromBits $ (bits h) .&. (bits m))'
    In the first argument of `($)', namely
        `makeIPAddr (fromBits $ (bits h) .&. (bits m))'

In the declaration of the class IPAddr, is there any way to force that the
IPHost and IPMask types are made up from the same IPBits type? Basically I
would like the compiler to enforce that Word (Host a) and Word (Mask a) be
the same type for a specific instance of IPAddr.

Note: I'm not sure how practical all this is going to be in the end (perhaps
a bit to convoluted), but it's an excellent learning exercise for me.


Thanks a lot,

Patrick


>
> One advantage compared to the multiparameter + functional dependencies
> solution is that you can write :
> (IPAddr a) => ...
> In your context rather than introducing h and m when they're not needed.
>
> If you need to write "Host a" several time in a function, you can put
> the following in your context :
> (IPAdrr a, Host a ~ h) => ...
> and use h for Host a thereafter.
>
> --
> Jedaï
>



-- 
=====================
Patrick LeBoutillier
Rosemère, Québec, Canada
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20090724/be992793/attachment.html

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

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


End of Beginners Digest, Vol 13, Issue 14
*****************************************

Reply via email to