1

This is not one of the myriad of questions already on SO

Rather it's a question on the answers themselves. As of today there are ~269,000 Google results for the query what is a monad in functional programming?, a ton of attempts to explain here and on the web with analogies ranging from burritos to love affairs. It seems that every attempt to "simply" answer turns into a labyrinth, each analogy becoming more confusing than the next. I could be wrong but it seems to me that this phenomenon is unique to monads.

Many have tried, few have succeeded

What is it about monads that makes explaining them so tempting but so elusive?

mazin
  • 395
  • 2
  • 7
  • It's quite an opinion-based question, but, I think, monads just don't have a good analogy in the real world, so to understand monads, you need to understand monads. That said, introduction of types like [`Optional`](https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html), [`Stream`](https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html) or [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) in object-oriented languages might make it easier. – bereal Dec 22 '21 at 18:07
  • But many things are abstract. It seems Monads are the hardest to explain – mazin Dec 22 '21 at 18:10
  • @AdamNathan they require that you understand a lot of other things first, and too many people try to jump into monads without the prerequisites. IMO an absolute minimum to understand monads you need to understand the following: difference between compile-time and run-time. Generics. Function composition. Types that manipulate other types e.g. `Nullable`. It's also helpful to have an understanding of union types and higher-kinded types. Many programmers don't work in compiled languages, or work in languages without a rich type system, or work shallowly with their language's type system. – Jared Smith Dec 23 '21 at 15:02
  • FWIW I think the [explanation here](https://stackoverflow.com/a/2704795/3757232) is completely understandable by many programmers working in popular statically-typed languages (C++/C#/Java/Typescript). I don't even use C# and yet it was the first explanation of monads I really grokked. – Jared Smith Dec 23 '21 at 15:05
  • @JaredSmith if I understand correctly, you are saying that monads can only be understood with a firm grasp in category theory first? I hear that, but IMO complicated things can be explained intuitively without details obscuring it and why the obsession to try to explain them? Perhaps they lie on the edge between things we think we understand and things we know we don't?? I mean we don't see that many tutorials for say, Yoneda embedding or whatever – mazin Dec 23 '21 at 16:38
  • @AdamNathan I didn't say anything about category theory, and it isn't necessary for understanding monads. Category theory is a branch of math, and I listed only programming concepts some of which do not even have a mathematical analog. I was trying to make the point that many programmers do not actually encounter those things during the normal course of their careers/hobby, so they lack enough context to understand monads. For one example I can't see how someone who had only programmed in dynamic languages could understand them, since they only exist in static type systems. – Jared Smith Dec 23 '21 at 16:50
  • @JaredSmith you say `they only exist in static type systems` is that true though? I've seen many monad tutorials in JavaScript – mazin Dec 23 '21 at 17:26
  • Stumbled on this blog post: https://byorgey.wordpress.com/2009/01/12/abstraction-intuition-and-the-monad-tutorial-fallacy/ – mazin Dec 23 '21 at 17:27
  • I mean, you can code something in Javascript that kinda sorta looks like a monad if you squint at it. I could write a static method like `Promise.bind = function (f) { return function g(p) { return p.then(f) } };` and ignore the lack of ap, map, and the auto-flattening thing you could say that it kinda sorta qualifies. But without types I'm not sure that's interesting enough to merit being a thing: it's just a helper function. It's doing it in a way that's fully general and still type-safe that makes it special. – Jared Smith Dec 24 '21 at 01:35

1 Answers1

2

I think it's one of those few abstractions in mathematics or programming that has no clear metaphor with the real world.

For that reason, all those articles that attempt to compare monads to burritos fail because a monad is not something that corresponds to human experience.

This seems to confuse many people, because they have the built-in expectation that every abstract concept has a root in something 'real. For instance, monoids (not monads) are things that you can 'smash together', like adding or multiplying numbers, or concatenating lists.

It seems to me that monads are more like quantum mechanics or relativity theory. Any attempt at explaining them using the human experience fails, because they're outside of natural experience.

Like quantum mechanics or relativity, though, they're actually not that hard to understand. (To be fair, I only have high-school understanding of both of these, but as I recall, once you see the formulas, they aren't that hard to understand.)

My experience with teaching monads is exactly that it works best if you dispense with all attempts at making the concept digestible by comparing it to something from the real world.

Instead, I start by explaining functors, which are a little easier to get across. Once people grasp functors, I tell them that monads are just functors that you can 'flatten'. There's no metaphor or simile here - just the 'raw' abstraction.

I also tell people to do some exercises to get familiar with these abstractions, just as it helps looking at the proofs and formulas when trying to understand (basic) quantum mechanics and special relativity theory.

It still typically takes some days (or weeks) of exercises before the concept of functors and monads click for people, but in my experience, the best teaching strategy is to realise that there's no direct metaphor from the real world that helps. Rather, teaching the 'raw' formula makes things easier.

In short, monads are hard to explain because we've yet to identify anything in the human experience that corresponds to this useful abstraction. This is, in my opinion, comparable to quantum mechanics or relativity theory. Our brains are evolved to deal with what we can perceive, and just like we don't perceive picometer-scale things or speeds close to the speed of light, we don't usually experience anything reminiscent of monads.

That doesn't mean that they aren't real, though.

Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
  • I hear your point, but again all of category theory is abstract (almost by definition) yet the Monad seems to generate the most confusion, and I've not seen physics being explained by 1000s of weak analogies. Normally Schrodinger's cat or using a ball to show space-time curvature is enough. – mazin Dec 22 '21 at 18:20
  • 2
    @AdamNathan perhaps because the only category theory most people encounter is the concept of a monad? – juanpa.arrivillaga Dec 22 '21 at 18:22
  • @AdamNathan Shrodinger's cat doesn't do anything to explain quantum mechanics, indeed, it is supposed to show a paradox that arises from quantum superposition, it is *a great example of how quantum mechanics is difficult to explain in a way that is intuitive*. It is absolutely not enough, it doesn't even *attempt* to explain things! – juanpa.arrivillaga Dec 22 '21 at 18:25
  • @juanpa.arrivillaga true but it gives enough intuition to understand the problem – mazin Dec 22 '21 at 18:32
  • @AdamNathan no it doesn't, it's suppose to show *why it is unintuitive*, that is the point! It's a paradox. It's like saying that Zeno's paradox makes classical mechanics more intuitive. – juanpa.arrivillaga Dec 22 '21 at 18:33
  • @juanpa.arrivillaga I queried Physics.SE and found no questions like 'what is quantum mechanics' with 100s of convoluted weak answers. – mazin Dec 22 '21 at 18:43
  • @AdamNathan Quantum mechanics have been around for a hundred years, now. I think the physics community has come to grips with how to teach it. Monads, OTOH, are only 30-40 years old... – Mark Seemann Dec 22 '21 at 19:03
  • @MarkSeemann possibly, but there seems to be something specific to Monads that create the feeling that it should be easy to explain but never is. I'm just curious to know why. Maybe it's deceivingly obvious when it's not like when a child asks you why two negative numbers multiplied become positive, the urge is to trip over your tongue before losing patience and shouting do your homework! :-) – mazin Dec 22 '21 at 19:28