
Monads Are Not Metaphors (2010) - ckarmann
http://www.codecommit.com/blog/ruby/monads-are-not-metaphors
======
GreaterFool
Did bad high-school mathematics education conditioned us all to hate
everything that's related to mathematics?

Label a concept "this is just engineering practice" or "this is just physics.
this equation describes how universe works" and people will happily accept it
as is.

But label it "mathematics" and explanations are in order because maths is hard
to understand...

I don't see any posts where authors concern themselves with metaphors for
Maxwell's equations (or anything else in physics; maybe I'm hanging out in the
wrong circles though).

What is a process of understanding a mathematical equation? Stare at it until
you grok it? Use it to solve problems to get a feel of what it means? Write
proofs about it so you can state some properties with certainty?

I don't know what the best/structured process is, but the core is "take this
equation and do something with it".

Do the same with monads! I think people having problems understanding monads
are those that defer writing any code until they feel "they get it". Instead
of "now I get it, monads are burritos, time to write some code" one should
write some code _in order to understand monads_

~~~
ch
I think it has much to do with the abstractness of mathematics (especially
category theory). Some just have a hard time anchoring the ideas to something
concrete, and they are in need of that anchoring to make the ideas stick. With
engineering and physics equations there is always something physical to relate
them to (at least somewhere in the universe(s)) -- not to say there aren't
harder equations to grasp in those fields too, but again that is typically
trending into the more abstract (less macro) areas of the field. The trouble
with math is sometimes you just have to work through equations enough until
you form some intuition about how they work. Some folks just want to skip that
step and others Oblidge them by assisting their mental model with metaphors
which seem to never be universal enough -- if it were it would just be math.

~~~
GreaterFool
Just thought about something that I think is analogous to explaining monads. I
remember reading few articles about "core math" in the States and it had
example problems related to addition, subtraction, multiplication.

The complaint was that those problems where incomprehensible gibberish and
kids and adults alike had really hard time figuring them out.

From what I recall the core of the issue was that the question authors had
some specific mental model of how addition and multiplication works and the
questions were structured in a way that required figuring out what that model
was (otherwise it was very hard to understand what was being asked).

Why does 2 + 3 = 5? Well, because it does. I don't even know what my mental
model of addition is. I just do it. I stared at it long enough as a kid and I
developed neural circuits that compute the result.

Instead if I asked:

I have two stars in a box. How many squares do I have to put in the box to
have 5 shapes?

    
    
        | * * | + | ??? | = | * * # # # |
    

Which is how I recall those weird maths questions were structured.

Now you a have a (terrible IMHO) model of addition. That's what I think those
monad tutorials do for you.

~~~
TuringTest
In order to first learning how to add, examples with apples and bananas are
helpful. Learning happens by relating some new concepts to something you
already know; that applies also to monads.

Those monad metaphors provide the same benefit. Even if some intuitions are
wrong and you still need to learn the match to get the whole picture, at least
you get a sense of (partial) understanding and purpose during the whole
learning process thanks to the metaphor, instead of feeling lost the whole
time.

------
TuringTest
This coming from the "you don't really understand a topic until you learn its
maths" school. I don't mind that position as there's some truth to it, up
until they commit the mistake to consider metaphors harmful and an obstacle.

I greatly benefited from the "conveyor belt" metaphor for learning monads; it
made me grasp instantly something that I would have never understood from a
pure theoretical explanation - namely _why_ they are so useful and widespread
in functional programming, as a building tool to distribute logic among
several composable functions over a data type. Sentences like "We start with
one thing and use its value to compute a new thing" and "Monads are an
abstract, mathematical label affixed to a pattern found in almost all code"
will never convey information about how it's intended to be used in the same
vivid way as the metaphor.

I know for true that the pure mathematical approach leaves me hanging. I
learned linear algebra in the theorem-proof style, and as of today I still
don't know when it's an adequate technique to use. I can't tell what matrix
ranks, kernels or tensors are good for even though I can calculate their
values very precisely.

~~~
jessaustin
_...the "conveyor belt" metaphor for learning monads;_

Thanks for that! Here's a link:

[http://web.archive.org/web/20100910074354/http://www.haskell...](http://web.archive.org/web/20100910074354/http://www.haskell.org/all_about_monads/html/analogy.html)

------
orkoden
[http://fuckingmonads.com](http://fuckingmonads.com)

------
crimsonalucard
Anybody who didn't know what monads were and now reached an epiphany thanks to
this article please comment.

Also is there anyone here who understands what monads are but has never
programmed in haskell or a functional language?

~~~
olavk
I don't believe monads are worth the bother without some syntactic sugar.
Without sugar, it is just a sequence of operations converted into a chain of
nested lambdas with one lambda for each operation. Cool, but far to convoluted
to write for any real-world programs.

The attempts to introduce monads in other languages like Python, Ruby etc. is
missing what make them a useful tool in Haskell.

~~~
tikhonj
I've used Async and LWT in OCaml—libraries for monadic asynchronous
programming—and the abstraction can still be useful without syntax sugar.

Hell, JavaScript promises are basically a bastardized and slightly
inconsistent monad, and they're still useful even without syntax sugar. If we
could generalize over the API and use them for other things it would be even
better, but that's not the JavaScript way.

~~~
olavk
How are JS promises monads?

~~~
tikhonj
They actually operate on more or less the same principle as Async and LWT,
similar to Haskell's Cont.

The .then method is bind. Or it would be if they didn't automatically flatten
nested promises. Since they do, then is a hybrid of map and bind that doesn't
quite cover the use cases for both.

------
wyager
This article seems a bit heavy on the computational monads.

Monads don't necessarily have anything to do with sequencing or computation,
and the author is objectively incorrect to say "monads are a pattern, not a
specific type". In fact, monads are an algebraic structure, defined only by
the types of the operations defined on them and the laws those operations
obey.

That's not to say that concrete examples like the one here aren't useful, but
the author is not covering the full scope of what monads are.

------
a3n
Brushing aside "you can do anything in one language that you can in another,"
what do monads allow me to do _better_ that I can only approach without them?

I've tried reading (but not coding), and all I hear is sequence of operations,
chaining, and similar. I know I'm missing something.

Basically I haven't seen or synthesized the tl;dr that makes me want to learn
a language that has them.

~~~
TheCoelacanth
All useful languages have monads. It's a pattern that is nearly impossible to
avoid. Where Haskell is different than most languages is that it lets you
write generic code that operates on any monad.

~~~
AnimalMuppet
Meaning that any language that doesn't have monads, you're defining to not be
useful? Or meaning that monad-the-pattern is everywhere, in every even barely
useful language, even if there's no explicit monads in the language?

~~~
TheCoelacanth
The monad pattern is everywhere, most languages just don't explicitly
recognize it. Any non-trivial program will include many different monads; they
just won't be recognized as such in most languages.

For instance in C#, there is a monad with bind=?? and return=no-op (similar in
use to the Maybe monad in Haskell). There's another monad with bind=; and
return=no-op (similar in use to the IO monad in Haskell). C# just doesn't
explicitly recognize those as monads.

~~~
ch
I like this comment because it helps illustrate the subtlety of monads as they
apply to programming.

I think mode C# programmers would likely think of ';' and '??' as syntax, and
who is thinking of 'no-op' as anything!?

But if you were in haskell this would not be syntax, but just functions on an
instance of the Monad type class.

Haskell could easily add some sugar to make these constructs syntax as well,
but it would just be sugar -- and that is where the power, and confusion I
think arises when most programmers think about monads.

------
olavk
Metaphors a tricky. Reminds me of the many attempts to explain objects and
classes in terms of real-world object. (Cars, fruits, persons etc.) These
explanations are often more confusing than enlightening, since object and
classes are actually not like real world objects at all.

~~~
Tomte
Relevant:
[https://web.archive.org/web/20110829152110/http://lists.cano...](https://web.archive.org/web/20110829152110/http://lists.canonical.org/pipermail/kragen-
tol/2011-August/000937.html)

------
saurik
Related:

[https://byorgey.wordpress.com/2009/01/12/abstraction-
intuiti...](https://byorgey.wordpress.com/2009/01/12/abstraction-intuition-
and-the-monad-tutorial-fallacy/)

------
runnig
Could someone translate this to Python?

