The “What Are Monads?” Fallacy
The Haskell community has a monad problem.
No, that's not quite right. Let me rephrase that.
Haskell beginners have a monad problem, and the Haskell community is partly to blame.
The Monad Tutorial Fallacy
It's a common desire among Haskell beginners to "learn monads". They struggle for weeks, or even months, and then suddenly – insight! When they finally understand monads, the traditional thing to do is to write a blog post explaining them. This blog post is often misguided and, in the worst cases, actively hurt the understanding of others.
All of this is nothing new. It was explained years ago in the excellent blog post on The Monad Tutorial Fallacy by Brent Yorgey. In his article, Yorgey hints at what I'm going to say, but I think it deserves to be said again, with slightly different words.
The core theme of The Monad Tutorial Fallacy is that monad tutorials, tutorials that teach people "what monads are", shouldn't use the crazy analogies they use, because such analogies don't help. But there is a more general problem.
The "What Are Monads" Fallacy
I think the question is the wrong question to start with. There seems to be a misunderstanding among beginners that if you only knew what monads were, you would be able to use them. I personally think that's bollocks for 99% of the people who attempt to learn Haskell. Sure, it might be true for some especially mathematically-minded people, but far from all of us are that.
Attempting to learn how to use monads by understanding what they are is like asking "What is a musical instrument?" and then assuming once you know the answer to that, you'll be able to play all of the musical instruments. If you have that preconception, you'll have a really hard time learning to work with a piano, a guitar, a hang drum, a harmonica or indeed any other musical instrument. Just knowing what a musical instrument is will trigger in you the thought, "Okay? So what?" and you will not learn anything about actually playing a specific musical instrument.
Similarly, knowing what a monad is – a type class with a couple of functions and three laws – will not do anything for your ability to use monads successfully.
Instead, learn to use specific monads. Learn how Maybe a
works, learn how Either e a
works. Learn how IO a
and [a]
and r -> a
works. Those are all monads. Learn to use them with the >>=
operator and with do
notation.
Once you've learned how to work with all of those, you'll have a really good idea of how monads can be used.
Asking "What is a monad?" to learn how to use monads is as wrong as asking "Whatis a musical instrument?" to learn how to play musical instruments. It's a good start, but it won't teach you very much.
Your question should never be, "I have trouble with monads, can someone help me understand?" because that's like stating you "have trouble with musical instruments." Tell us which monad you have trouble with. "I have trouble understanding how >>=
works in the Either e
monad" is an infinitely better question.
Why Does This Happen?
I think that maybe some Haskell users are way too excited over their typeclasses. I mean they are good and all, but they aren't what they are in isolation. They are what they are because they make sense within the rest of the language and libraries. I think the rest of the language and libraries are sometimes forgotten in this discussion.
And I understand the tendency. It's not very catchy to say "I finally understand how to use Maybe and Either and List and Reader and IO!" It's much catchier saying "I finally understand monads." But it does unfortunately give off the wrong impression about what monads are.
Imagine if everyone who made music in the world started saying, "I finally understand musical instruments!" Uninitiated would get the wrong impression about that as well. (Don't get me wrong. The theory behind how musical instruments work in general is super exciting stuff, but it doesn't make you any good at playing the instruments.)
How can we fix it? I don't know if it's fixable or worth fixing. We can, however, be more clear to beginners that "monads" are not a single thing you learn and then use, but that there are as many monads as there are musical instruments, and they are as different in their operation.