What are monads in programming?


What are monads in programming?

In: 18

think of them as wrappers that ensure type security. if you put any object into the context of a monad, you can define certain behaviours it will follow. the most simple example is the maybe monad, which states that an object may or may not exist. another example is the IO-monad, under which every object expects a user input and produces an output. you can customize a monad any way you like. if a function then returns a monad instead of an object, the object inside of the monad can be of any type, as long as it follows the rules you defined. in a way, they are the antithesis to object-oriented programming, which is based on inheritance and polymorphism

Not commonly used, but in APL a monad or “monadic function” is a function that takes one argument. This is a completely different concept than in Haskell.

That’s easy. Imagine that you have a large pizza delivery organization. There are many people who are trained to pack pizza boxes, transport them, etc,etc, there are many stages of delivery. And all this works perfectly. Imagine also that all this happens in a strange world where people can only pay in cash. They leave their money on the counter in the morning and get their orders delivered in the evening to their houses. And there are no phones.

Suddenly you encounter a problem: sometimes there is no pizza available. In this case you need to return the money. You can build an alternative structure with people transporting money back to customers, but it can be expensive and troublesome. Eventually you find a brilliant solution. You just put money into an empty pizza box along with the note explaining the problem. Your delivery system continues to operate as usual but now it has an additional functionality packed into the same boxes.

Of course it comes at additional cost for you. At least you are sending empty boxes every time you need to return the money. Also some people in your delivery network may need to be retrained, for example there could be some additional weighting station that checks the weight of each box. You need to tell them that there could be unusually light boxes.

Really going for the “like I’m 5” bit here. Let’s see how well this goes.

A monad is like a bank safety deposit box.

The technical definition of a monad (in programming!) is basically that it is a thing which exposes two operations. I’ll call these operations “deposit” (usually called a “constructor”, and usually named “new”, “return”, or “unit”), and “do” (usually called “bind”). Let’s see how each of these operations works.

The “deposit” operation is like when you take something to the bank and ask them to put it in your safety deposit box. You take your precious diary to the bank, give it to them to put in the safety deposit box, and they give you a note that says “your diary is in safety deposit box #15”.

(The more technical version of this is that you take your object `a` and give it to the monad’s “constructor” `M`, and this operation returns a new value `M(a)`, usually called “M of a” or a “monad of a”.)

The “do” (or “bind”) operation lets you manipulate the thing you’ve deposited with the bank. For example, you might want to add a new entry to the diary, so you bring the bank a note that says “please add this entry to my diary: Dear diary, today I…”. Or you might want to erase an old entry because that’s a bad memory now, so you bring the bank a note that says “please delete the entry from January 7th”. Or you might bring the bank a note that says “please change the color of my diary’s front cover from Blue to Green”. Whenever you bring the bank one of these notes, they go back to the safety deposit box, do exactly what you asked, and then give you a new note that says “your *updated* diary is in safety deposit box #15”.

(The more technical version of this is that you have a function `f(a) -> a`, aka a function which takes objects of type `a` and returns another new object of type `a`. Use use the monad’s “bind” method to apply that function to the `a` contained within your monad: instead of simply calling `f(a)`to get your new result, you call `M(a).bind(f)` and get a new monad.)

Now here’s the thing. That all sounds like an awful lot of work. Why would you go to the trouble of asking the person at the bank to do all of these things to your diary for you, when you could just do them yourself? Well, normally you’d have to do all those things one at a time. First add a new entry. Then erase the old entry. *That* seems like a lot of work. The beauty of depositing your diary at the bank is that you can just hand them a stack of notes and they’ll do all of them for you, all at once!

(The more technical version of this is that if you have a *bunch* of functions like the one we described above, instead of having to do the very unintuitive `h(g(f(a)))`, you get to use “bind” to make your code read left-to-right: `M(a).bind(f).bind(g).bind(h)`.)

Obviously this metaphor isn’t perfect, and I probably still haven’t succeeded in making it understandable to a 5 year old. But hopefully that helps you!