A homepage of lambdas,
computer science, drumming, and related interesting things
Five Minute Monad Madness!!!
Authored: September 22, 2012
Purely functional programming gives us a certain sense of mathematical completeness. Programming with functions gives us a very high level of expressiveness, along with an enhanced view of safety. When you don’t have any state underlying you, you can program in a much more compositional style: you don’t have to worry about as much context. However, it’s not immediately obvious how you can encode stateful operations within purely functional languages. This starts to become a problem when we start to interface our functional languages with the “real world,” which typically involves stateful objects (the infamous “fire missiles” example: once you fire the missiles, they’re gone, you can’t fire them again, and you can’t reset the state of the world).
Initially, the way to deal with this was to basically say: “hey, okay, so state exists, but we’re just not going to use very much of it in our code, and we’ll probably be okay forgetting about it most of the time.” (This was the approach taken by ML, for example, and while some ML uses a good number of references, much does not — or at least presents a purely functional interface to the impure code.) However, the question of how to reconcile the seemingly stateful world with our purely functional upbringings started nagging on the community more and more. A variety of solutions were posited to the community (for example, “Linear Types Can Change the World!”), but none seemed to catch on until…
Monads. It’s a complicated sounding word, expressing a simple-ish concept. Eventually, monads were settled upon by the purely functional (Haskell) community as the way you did stateful feeling things inside a purely functional framework. In reality, you can do much more, as the definition of monads is high level and really doesn’t specify all that much: you can chain things together, need some identities, and some composition laws hold. (I remember the quote “to say something about a category is really to say very little at all…” from somewhere, does anyone remember?)
If you haven’t read the second sentence of every monad tutorial ever “monads come from a category theory.” This is such a canned sentence, it’s almost infamous among the purely functional web community. I feel like I could walk into bars with my suave not-quite-shaven look, order an old fashioned, walk up a cool looking group of people, and say “yeah, you know, monads come from category theory.” To me, this line basically conveys:
It seems like nobody truly gets monads until they write a monad tutorial. In fact, given by the number of monad tutorials on the web, I must only conclude that this is the best monad tutorial in history:
So I’m telling you to do that! That’s right! But I’ve got a few rules for you!
I want to make a small meta comment: I’m not writing this because I’m implying that I have a great five minute monad speech: I don’t! I’m writing this because I think it will be very hard to get across the main point of monads in a very short interval, and I think that most of the tutorials out there on the web are instances of people half understanding the main point, writing up a bunch of stuff, and putting it on their blog. (But hey, that’s probably me right now, too, so you can’t feel too bad.)
Love the lambda!
– Kris Micinskicomments powered by Disqus
Generated with Hakyll