[Haskell-beginners] MonadPlus
Brent Yorgey
byorgey at seas.upenn.edu
Thu Jul 29 05:02:39 EDT 2010
On Wed, Jul 28, 2010 at 12:56:58PM -0700, Johann Bach wrote:
> On Wed, Jul 28, 2010 at 3:51 AM, Brent Yorgey <byorgey at seas.upenn.edu> wrote:
> > On Wed, Jul 28, 2010 at 03:03:01AM -0700, Johann Bach wrote:
> >> I'm looking at Douglas Auclair's MonadPlus article, here
> >> http://www.haskell.org/sitewiki/images/6/6a/TMR-Issue11.pdf
> >>
> >> So consider an example like
> >>
> >> t2 = do
> >> x <- [1,2,3]
> >> y <- [1,2]
> >> guard $ x+y > 2
> >> return (x,y)
> >>
> >> This uses the list instance of MonadPlus.
> >>
> >> Now, Douglas is talking about his own code, not the above example, but
> >> his own code is similar. And he says "the entire computation is
> >> chained by mplus". I'm confused because I thought it was chained by
> >> >>=. In fact, I can't see where the above code makes use of mplus. The
> >> definition of "guard" uses mzero.
> >
> > You are correct. That sentence is an error. Indeed, it ought to say
> > "since the entire computation is chained with (>>=), a failure of one
> > test voids the entire branch". This is because of the required law
>
> Brent -- also, in his example (similar to my example above) it looks
> like the only benefit of the list instance of MonadPlus in this case
> is to use the pre-existing definition of guard. Is that true? His
> example is indeed more complex... it's in TMR issue 11.
Yes, that's true in a sense, since mzero = [] and mplus = (++) are
rather trivial for the list instance of MonadPlus. Of course, you
also gain the generality of having code that will work in any
MonadPlus. For example, if you have written some search code in a
MonadPlus style and decide you only want the first solution rather
than collecting all of them, you can just switch the type from [] to
Maybe without changing any of the actual code.
-Brent
More information about the Beginners
mailing list