4 ms·
Monads are not complex. A monad is a box. Any monad has two functions (plus others, imagine this is a minimal interface): bind and return. You can put anything
by aetherspawn 10y ago
Monads are not complex. A monad is a box. Any monad has two functions (plus others, imagine this is a minimal interface): bind and return.
You can put anything in the box. It's TARDIS-like.
`return` replaces the thing that was in the box.
`bind` replaces the box.
Most monads have a number of functions that can only be executed over boxes. This is because the boxes have special metadata (for example, someone has been scribbling state underneath the box). The 'get' function from the State monad just tells you to read the gibberish on the box instead of unpacking the box. The 'set' function scribbles more stuff on the bottom of the box.
Useful monads then provide a function to put things into the box, work with the box and then throw the box away (or inspect it by itself). These are the functions called 'runWhatever' for example 'runState', which lets you put an apple in the box, put a shipping label onto the box and then eventually separate the apple and the shipping label into each hand, throwing the box in the bin.
You can put anything in a box. Even more boxes. And when you're inside the box, you can't really tell how deep you are. If you're in the bottom box you can't actually see that you're in 20 layers of boxes, and this is why Monads are so powerful for creating libraries and frameworks.
- aetherspawn 10y agoHere's an extended explanation of how monad 'lift' works: You have a box (one monad). Inside the box is a jar (one monad) and inside the jar is an apple. You want to write on the jar, so you give it to your friend but he's a moron and doesn't know how to open the box. The `lift` function opens the box, gives him the jar so he can write on it, then puts it back in the same box. `lift` is for executing monad stuff within other monads when you have jars in boxes and need to juggle them around a little bit, but all your friends are idiots. `liftM` is the same thing actually, but can be used on the apple in the jar since the apple isn't already a monad. If you need to swap the apple for another apple, but it's inside the jar inside the box, you need to use the composition of `lift` and `liftM` to first take the jar out the box, and then the apple out of the jar (but it'll pack them all up when you're done).
- bogomipz 10y agoThanks for your explanation. I confused as to why so much discussion and blog posts surround the topic of monads as if they were very difficult to understand then? I would be curious to get your thoughts. Also I didn't understand the word TARDIS-like.
- aetherspawn 10y agoThe TARDIS is a box from doctor who that is bigger on the inside (holds a small pocket universe inside). You can put anything inside the TARDIS. My explanation of Monads approaches them from a practical angle rather than a theoretical angle. It won't help you make your own, but it will help you appreciate why people make them.
- bogomipz 10y agoI see, thanks for the explanation. That's a good analogy. Cheers.
- floatboth 10y agoIncidentally, https://hackage.haskell.org/package/tardis-0.4.1.0/docs/Control-Monad-Tardis.html https://hackage.haskell.org/package/tardis-0.4.1.0/docs/Cont...