Anybody who tries to popularize Haskell has to deal with the monad problem. You can’t write the simplest Haskell program without a monad (the IO monad in particular) and yet there is no easy way to explain what a monad is. Mind you, when teaching object oriented programming, nobody starts with a definition of “object.” There’s no need for that because everybody has some intuition about objects — we are surrounded by objects in real life. Which is very convenient because if you tried to define what an object is in C++ or Java, you’d have to use quite a bit of hard-core PL theory; plus you’d have to wave your hands a lot. This trick doesn’t work with monads because, except for category theorists, nobody comes to Haskell with any intuitions about monads. The name monad is scary in itself (this is probably why monads in F# are innocuously called “computation expressions”).
In my n’th attempt to tackle monads I tried a different, mystical, approach. In mysticism you often talk about things that you cannot define. Taoism takes this approach to the extreme — any attempt to define Tao must fail: “Tao that can be described is not the eternal Tao.” Hence the slightly tongue-in-cheek approach I took in this tutorial, Pure Functions, Laziness, I/O, and Monads. If you’re new to Haskell, it might help if you read the two previous tutorials in my Basics of Haskell. Notice that these tutorials are in the new format introduced by the School of Haskell, where you can run and edit program snippets directly in your browser.
I must confess that this tutorial got slightly cold welcome in the Haskell community. That’s why I’m curious what non-Haskellers think of it. Does my approach make monads less or more scary? Take into account that I decided to tackle monads right after explaining the very basics of Haskell syntax — in most courses or books monads are treated as an advanced topic.
March 7, 2013 at 12:55 pm
This difficulty in explaining the essential properties of a monad, yet explain how a developer would use this language construct points to a fundamental problem of functional language usage outside a dedicated few. Perhaps addressing the parallel programming problem by looking at all systems space as evaluated functions is just too mind-numbing for the mainstream?
March 7, 2013 at 1:32 pm
You lost me here:
“In the case of the Hello World program, the Universe is modified by displaying the text “Hello World!” on your screen.
So the Universe we are living in is being constantly modified by Haskell programs. That’s why being a Haskell programmer feels like being the Master of the Universe.”
When getting to this point I had no reason think that putting “hello world” on the screen would make me feel like the master of the universe.
March 7, 2013 at 3:04 pm
Non-Haskeller here but I’ve read several monad tutorials out of general curiosity. My concept is that monads allow the functional pipeline to be folded intricately so that values are delivered at the right place and time. But I hadn’t fit the IO monad into the picture. Now it makes sense: the universe is the input to the pipe; the universe is the output of the pipe.
Thanks. I look forward to more installments.
March 7, 2013 at 3:16 pm
Aha! Another aspect of my understanding is that a monad wraps some values and sneaks them through some functions until they are needed at other functions. With the IO monad most of the universe sneaks all the way through.
March 7, 2013 at 3:22 pm
… sneaks all the way through and lazily goes its way, unevaluated.
Wow, this Tao stuffs works.
March 7, 2013 at 6:11 pm
It’s an abstraction of how one executes a sequence of expressions, of course.
March 8, 2013 at 4:21 pm
All my dilemmas have vanished after I thought:
ID’s Quake is a computation which:
– gets mouse, keyboard inputs and time
– gives graphics frames & sounds
Of course, it’s a “const” function: as long as each “time tick with imput” is lazy evaluated, and context is recursively passed on and on…. 😀
It was long before this article – but I have to admit, this article is:
– with simpler example :p
– more or less exactly how I -feel- monad concept
I tried quake example on my non-functional friends some time ago, and they have grasped the idea.
IMHO your style is nice, could work on some of us (I’m used to borrow ideas from functional hobby to my imperative paid work)
March 9, 2013 at 2:51 pm
You are very close to describing Functional Reactive Programming, FRP, where the program looks like it has access to the whole history of user interactions (which will, in fact, occur in the future; some of them in response to program output) and processes them as one very long (potentially infinite) list.
March 9, 2013 at 8:17 pm
Sorry for this little off-topic, but you really made my day. It bothered me from time to time how to comprehend these topics in FP. It’s like you are a small child which use loops – and know that they are ugly. And then someone show you the light of map and reduce.
This express how I fell now, and thanks very much for your comment. I definitely write some small game in Elm, It will be pure joy for my mind.
Mind which knew that goto is evil, knew that callbacks are nasty – but there are no other way in OOP and have to be used. Mind which was too narrow to find FRP 😀
Yep, I’m really THAT happy.
March 10, 2013 at 1:24 am
[…] The Tao of Monad ::: Bartosz Milewski’s Programming Cafe […]
April 16, 2013 at 1:53 pm
Trying to be evil here 😉 Just joking, Im noob, but I would like to know As to this.
1. How do you match performance of for example lock free concurrent hash map with immutability?
2/ How do you do cache oblivious algorithms with Haskell?
Coursera) Any chance of you running a Haskell for C++ programmers on Corsera? AFAIK you arent in academia atm, but maybe SPJ could be like the official head of the course, and you would be like this young 🙂 genius TA. 😀
April 28, 2013 at 12:56 pm
The first link is broken…
https://www.fpcomplete.com/school/basics-of-haskell/3-pure-functions-laziness-i-o-and-monads
April 28, 2013 at 3:34 pm
@TP: Sorry about that. I wrote a more traditional version of that tutorial and the old link moved as a result. I updated it in the body of the blog.
April 29, 2013 at 1:23 pm
I’ve read it already, thank you for the quick answer though.
Do you plan to write an article about Composing monads for C++ programmers? I’m sure this is a feature that is needed even for writing trivial programs and it is extremely hard for me to understand it or build an intuition about it.
Probably an article about a Calculator that logs every operation (push/pop/add) to stdout 🙂
May 1, 2013 at 9:35 pm
I think my tutorial will eventually get to monad transformers. As a matter of fact I’m planning to use the example of a symbolic calculator in my tutorials. I used is in my C++ book and I had fun rewriting it in Haskell from scratch.