Monads for Functional Programming, by Philip Wadler, 1992.
After reading this paper, I think I finally have an intuition for what monads are. They're basically a fold over computations: unit is used to transform values, and bind is used to transform unary function application. The fact that a monad can only fold over unary application is, one would hope, not limiting because of currying (but I'm not sure). The monad laws can be thought of as restricting the fold to having semantics similar to function application (respectively: referential transparency, lambda abstraction, and composition).
All monads have a type argument. My understanding of why this is necessary is because functions are opaque, and the only thing you can do with them is apply them to the exact type that they expect. Therefore the original value must be shuttled through the monad so that it can be passed to the functions that are being folded over. One alternative would be to not apply the function at all, but that only works if your intention is to abort the computation (the way exception monads do). You certainly can't keep following the original computation, because functions are black boxes that are useless for anything other than being applied to a value.
Because monads always wrap an underlying type with additional information, an alternate intuition is to think of a monad-wrapped type as a subclass in an OO language, with the original type being the base class. In this view, unit is basically a factory method. However, I'm not sure the monad laws have any meaning in this point of view, and it's even difficult to find an OO analog for bind.
I think the paper would have been better if the long discussion of particular monads (array reader/writer, parser, etc) had been replaced with a discussion of how to combine monads (after all, imperative programming lets you use state and exceptions and I/O, all at the same time). Perhaps he was trying to give a feel for where Monad research was going, but I don't think that's appropriate for a paper that starts out as didactic as this one does.
My intuition is telling me that monads are not really an optimal technique for adding semantics to computations, for two reasons. One, they're restricted to unary function application. Two, functions are useless for anything other than application. I have the feeling that Monads do not, in general, approach the power of abstract interpretation.
Read more reactions to this paper on the Advanced Functional Programming wiki.
Posted on October 16, 2003 06:43 PM
More languages articles
Ah, yes, monads. That reminds me, I wanted to ask you why you've decided to focus on Haskell.
Like you I used the imperative languages for my day job (Java in my case) but wanted something more expressing for my self-education and "fun" programming. I started functional programming with Haskell, did just fine with referential transparency, higher-order functions, and all that. Monads stopped me cold. Maybe I'm too dumb or too lazy to stop and figure them out, or maybe I'm too pragmatic. I went on to mixed imperative/funcitonal languages, first OCaml then Commmon Lisp, focusing on just solving my problems and not focusing that much on the language.
So anyway, I was wondering what other languages you considered before Haskell, and how they were lacking. If you're interested in my "journey", look at http://alu.cliki.net/Ralph%20Richard%20Cook.
I focus on haskell mainly because it's weird. Laziness makes it qualitatively different from most other languages. I already know enough variants of algol and smalltalk that I don't really want to explore that path any further right now. I think haskell's oddities force me to learn new things instead of simply relying on my existing habits.
For the same reason, I'm also interested in concatenative languages, logic languages, chinese, sign language, and that clicking language in "The Gods Must be Crazy".
I considered Erlang, but I was disappointed (I was expecting a logic language, not a functional language that didn't even have currying). I also considered O'Caml, because lots of hackers use it, but little things about the syntax just drove me crazy -- like the distinction between let and let rec, and the use of semicolons to separate list elements.
OK, as long as it's just for the wierdness and not because you think it's useful... :-)
Coincidentally, this was just blogged...when one of the designers of Haskell says "Pure functional programmers, your days are numbered. The grim reaper is knocking at your door.", you know just a little destructive assignment won't hurt, and, c'mon, all the cool kids are doing it...
Posted by: Ralph Richard Cook at October 21, 2003 11:09 AMThere's some debate about to what degree that blog entry was meant as satire...
Posted by: kim at October 21, 2003 05:40 PM