[Haskell-beginners] Monads

Brent Yorgey byorgey at seas.upenn.edu
Sat Mar 5 16:11:32 CET 2011


On Sat, Mar 05, 2011 at 09:25:21AM -0500, Patrick Lynch wrote:
> Brent,
> 
> Eureeka! I'll be damned - I can create my own class...(I had a 'typo'
> in my class MyCalculator class code -- that is, myadd::...should be
> myAdd::...) it works just fine...thank you. You should of bet me...
> I've purchased Awodey's book: "Category Theory"...thanks again...
> 
> I've got the following books on my reading list:
>    "Programming in Haskell" by Graham Hutton
>    "Learn You a Haskell for Great Good!" by Miran Lipovaca
>    "Real World Haskell" by Bryan O'Sullivan, etal
>    "The Haskell Road to Logic, Maths and Programming" by Kees Doets, etal
>    "An Introduction to Functioanl Programming Systems Using Haskell"
> by A.J.T Davie
>    "Introduction to Functional Programming using Haskell" by Richard Bird
>    "The Craft of Functional Programming" by Simon Thompson
> I've been working with Simon Thompson, so I'll start working his book
> next...
> 
> If you can recommend a book that covers Monads [for a mere practicing
> software consultant], I'd welcome it...

Real World Haskell is such a book -- I haven't read the part about
monads but I've heard good things about it, that it introduces them in
a quite practical, example-driven, no-nonsense way.

-Brent

> 
> Good weekend,
> Pat
> 
> ----- Original Message ----- From: "Brent Yorgey"
> <byorgey at seas.upenn.edu>
> To: "Patrick Lynch" <kmandpjlynch at verizon.net>
> Cc: <beginners at haskell.org>
> Sent: Friday, March 04, 2011 11:33 PM
> Subject: Re: [Haskell-beginners] Monads
> 
> 
> >On Fri, Mar 04, 2011 at 05:20:27PM -0500, Patrick Lynch wrote:
> >>
> >>I'd love to take a crack at Category Theory [my mathematics is good]
> >>but this looks like a formidable task...
> >
> >It is not that formidable just to get started.  But it is so abstract
> >that it can be difficult to gain intuition for.  If you want to learn
> >some category theory I recommend Awodey's book.
> >
> >>I'll proceed using the Monad classes and instances as is - and hope
> >>that at some point I'll be able to create my own Monads...
> >
> >Creating one's own monads is overrated.  I almost never do it.
> >Usually I can just put something together using monad transformers
> >that fits my needs.
> >
> >>
> >>btw: can you create a class in Haskell -[I'm guessing the answer to
> >>this is no -- see below, it doesn't work]?
> >>
> >>    class MyCalculator a where
> >>    myadd::( Num a) => a -> a -> a
> >>    myAdd x y = x + y
> >
> >Sure, you can do that.  What do you mean when you say that it doesn't
> >work?
> >
> >-Brent
> >
> >>
> >>----- Original Message ----- From: "Brent Yorgey"
> >><byorgey at seas.upenn.edu>
> >>To: <beginners at haskell.org>
> >>Cc: "Patrick Lynch" <kmandpjlynch at verizon.net>
> >>Sent: Friday, March 04, 2011 3:07 PM
> >>Subject: Re: [Haskell-beginners] Monads
> >>
> >>
> >>>On Wed, Mar 02, 2011 at 04:38:07PM -0500, Patrick Lynch wrote:
> >>>>
> >>>>----- Original Message ----- From: "Brent Yorgey"
> >>>><byorgey at seas.upenn.edu>
> >>>>To: <beginners at haskell.org>
> >>>>Sent: Wednesday, March 02, 2011 10:37 AM
> >>>>Subject: Re: [Haskell-beginners] Monads
> >>>>
> >>>>
> >>>>>On Wed, Mar 02, 2011 at 10:04:38AM -0500, Patrick Lynch wrote:
> >>>>>>Daniel,
> >>>>>>Thank you...I understand now...
> >>>>>>Good day
> >>>>>>
> >>>>>>    instance Functor Maybe where
> >>>>>>      fmap f (Just x) = Just (f x)
> >>>>>>      fmap f Nothing = Nothing
> >>>>>
> >>>>>So you are confused about this code?  Can you be more specific what
> >>>>>you are confused about?  Try thinking carefully about all the types
> >>>>>involved.  What is the type of f? (You already answered this: a -> b.)
> >>>>>What is the type of (Just x)? The type of x?  What type is required on
> >>>>>the right hand side of the = ?  And so on.
> >>>>
> >>>>
> >>>>-Hi Brent
> >>>>I know that Maybe type is: data Maybe a = Nothing | Just a
> >>>>...so, I assume that the type to the right of the '=' should be
> >>>>Maybe a..
> >>>
> >>>Not quite.  Let's look again at the type of fmap here:
> >>>
> >>> fmap :: (a -> b) -> Maybe a -> Maybe b
> >>>
> >>>So fmap will be taking *two* parameters, the first of type (a -> b),
> >>>the second of type Maybe a.  Then it needs to return something of type
> >>>Maybe b (so this is what will be on the right hand side of the =).
> >>>
> >>>>    instance Functor Maybe where
> >>>>      fmap Nothing = Nothing
> >>>>      fmap Just x = Just x
> >>>
> >>>You're missing the first argument to fmap (the one of type a -> b).
> >>>Let's call it f.  Since the second argument is of type (Maybe a)
> >>>you're right that we should handle all cases (Nothing and Just).
> >>>
> >>> fmap f Nothing = Nothing
> >>>
> >>>so here we need to return something of type 'Maybe b'.  Well, we don't
> >>>have any values of type b, so the only thing we can do is return
> >>>Nothing.
> >>>
> >>> fmap f (Just x) = ?
> >>>
> >>>The reason we need the parentheses around (Just x) is simply that
> >>>otherwise the compiler thinks we are writing a definition of fmap that
> >>>takes three arguments: f, Just, and x, but that doesn't make sense.
> >>>
> >>>Now, here f has type (a -> b), and (Just x) has type (Maybe a), hence
> >>>x has type a.  If we apply f to x we get something of type b, so we
> >>>can define
> >>>
> >>> fmap f (Just x) = Just (f x)
> >>>
> >>>Does that make sense?
> >>>
> >>>-Brent
> >>
> 



More information about the Beginners mailing list