Lambda the Ultimate

inactiveTopic All About Monads
started 8/12/2003; 2:03:20 PM - last post 8/19/2003; 1:35:44 AM
Ehud Lamm - All About Monads  blueArrow
8/12/2003; 2:03:20 PM (reads: 2984, responses: 13)
All About Monads
This tutorial aims to explain the concept of a monad and its application to functional programming in a way that is easy to understand and useful to beginning and intermediate Haskell programmers. Familiarity with the Haskell language is assumed, but no prior experience with monads is required. The tutorial covers a lot of material and the later sections require a thorough understanding of the earlier material. Many code examples are provided along the way to demonstrate monadic programming. It is not advisable to attempt to absorb all of the material in a single reading.

Quite an extensive tutorial, that even covers things like monad transformers. Includes a large catalog of standard monads.

I won't venture an opinion since I just read a tutorial about monads written by one of my students, and naturally I am a bit biased...

Posted to functional by Ehud Lamm on 8/12/03; 2:06:16 PM

Bryn Keller - Re: All About Monads  blueArrow
8/12/2003; 2:36:06 PM (reads: 1221, responses: 0)
I thought it was quite good, though naturally I haven't read the one by Ehud's student :-). The section on monad transformers was especially nice, since I don't think I've ever seen a decent introduction to them before.

Patrick Logan - Re: All About Monads  blueArrow
8/12/2003; 2:52:50 PM (reads: 1215, responses: 0)
The best and most comprehensive tutorial on monads I have seen. I'm taking it to the beach this weekend.

Dominic Fox - Re: All About Monads  blueArrow
8/13/2003; 1:06:01 AM (reads: 1123, responses: 1)

The initial discussion (of Maybe, and the parentage of cloned livestock) is excellent, and helpful in that provides a meaningful context in which to introduce the nuts and bolts of monads before we get to a discussion of those members of the "awkward squad" that monads are used to tackle. I think this is a smart move, because it makes it easier to see why monads are a good approach to take to these issues. Other tutorials I've read have tended to say, in effect, "State and I/O are a problem, and Haskell has this weird, obscurely-named and difficult-to-understand mechanism for getting around that problem, which I will now endeavour to explain to you", which is not encouraging.

Browsing ahead, this looks like it deals with some of the harder (for a newb) questions about monads, such as how you combine monadic actions of different types, in a timely fashion; which is good, because if you're impatient like me you'll have started asking those questions long before some treatments of the subject get to them (if they get to them at all).

This is really useful!

Ehud Lamm - Re: All About Monads  blueArrow
8/13/2003; 1:13:51 AM (reads: 1148, responses: 0)
It is, indeed, a refreshing approach.

But I think it is worthwhile to discuss the different approaches for handling state and IO in pure languages (CPS etc.), and see how monads are related to them.

Dominic Fox - Re: All About Monads  blueArrow
8/13/2003; 2:45:33 AM (reads: 1107, responses: 1)

As in this example?

Unlike when I first read this discussion, I now have some idea what "lifting over a backtracking monad" might mean...

Dominic Fox - Re: All About Monads  blueArrow
8/13/2003; 3:00:58 AM (reads: 1102, responses: 0)

Perhaps less intimidatingly (I am quite hopelessly out of my depth with much of this sort of thing), you could compare two approaches to nondeterminism - the list monad, and the implementation of (amb) using call/cc discussed in "Teach Yourself Scheme in Fixnum Days".

Ehud Lamm - Re: All About Monads  blueArrow
8/13/2003; 5:42:42 AM (reads: 1093, responses: 0)
Welll, Maybe. I think the monad transformer section isn't extremely readable. It seems to me the subject requires a longer, more detailed, explanation.

Jeff Newbern - Re: All About Monads  blueArrow
8/13/2003; 11:03:41 PM (reads: 980, responses: 1)
As the author of the tutorial in question, I completely agree that the section on monad transformers needs to be expanded and clarified, and I am looking for feedback and suggestions. The goal of the tutorial is to impart a useful understanding of monads to people who approach the subject as programmers, not mathematicians. If anyone here has suggestions, corrections, illustrative examples, etc. please let me know.

Ehud Lamm - Re: All About Monads  blueArrow
8/13/2003; 11:28:31 PM (reads: 997, responses: 0)
As the author of the tutorial in question, I completely agree that the section on monad transformers needs to be expanded and clarified

Great! Your tutorial is very good, so it's well worth it to spend the time to make it even better.

Dominic Fox - Re: All About Monads  blueArrow
8/14/2003; 1:59:30 AM (reads: 951, responses: 0)

Here - just to scrape some fingernails down people's inner blackboards - is a (no doubt rather wonky) OO/Imperative analogy for monadic actions:

Consider a Command object that implements a method that receives a representation of the state of a process, and returns a representation of the state of that process after the command has been executed.

Now consider a Command object that contains a sequence of smaller Command objects - Sub-commands. When this Command's "execute" method is called, the initial state-representation is passed to the first Sub-command in the sequence, and the modified state-representation returned by that Command is then passed to the next Sub-command in the sequence. All of the Sub-commands are chained together in this way, and the modified state-representation returned by the last Sub-command is finally returned by the Command object.

Now consider a Command object that may fail, and that contains a sequence of chained Sub-commands that must fail if any one of the Commands in the sequence fails. Also consider a Command object that passes the same initial state-representation to each of its Sub-commands in turn (i.e. without chaining them together) until one of them does not fail, and then returns the return value from that Sub-command without troubling to execute any of the others.

Given the ability to compose Commands by adding them to sequences of Sub-commands maintained by other Commands, and the ability to either chain Sub-commands together or try them out one at a time with the same initial conditions, it is possible to implement nondeterministic processes of various sorts, backtracking whenever some sub-sequence of processes fails. The key to this is of course the ability to create, modify and pass around representations of the state of the process.

What happens inside a Parser monad is similar to the above, except that functions are used instead of Command objects, and function composition (using an infix operator) is used instead of storing nested sub-sequences of Commands inside each other...

Neel Krishnaswami - Re: All About Monads  blueArrow
8/18/2003; 10:36:41 AM (reads: 762, responses: 1)
In the last few days, I've been reading a lot about various categorical approached to programming (only slightly hindered by my ignorance of category theory), and one of the nicest papers I've found is Erik Meijer's Merging Monads and Folds for Functional Programming. It's a scary title, but the first two thirds of the paper are a very clear and direct tutorial on both morphisms (folds and unfolds, basically) and on monads. He doesn't use typeclasses until the very end, so the underlying ideas aren't obscured by the language features.

I haven't looked at Jeff's tutorial yet, though it's on my list.

Ehud Lamm - Re: All About Monads  blueArrow
8/18/2003; 12:15:18 PM (reads: 772, responses: 0)
Merging Monads and Folds for Functional Programming.

Yes. Plenty of good stuff in this paper. Useful alternative (and source of examples) for The Little Haskellist...

Jeff Newbern - Re: All About Monads  blueArrow
8/19/2003; 1:35:44 AM (reads: 744, responses: 0)
I have updated the tutorial based on the feedback I got from the first version. The changes in the current version include an expanded section on monad transformers, additional examples, more precise language in the discussion of the IO monad, example code clean-ups and typo fixes.

Thanks to all of the people that sent in suggestions and criticisms!