How is a book a monad?

This ain't no "a monad is a burrito" talk. This is a real-world monad, found in the wild. We look at how reading a book is monadic. It's what lets you see a list of lines of words as a single stream of words.


Eric Normand: How is a book a monad? Hi, my name is Eric Normand. These are my thoughts on functional programming. I'm always on the lookout for monads in the real world because I feel like it is such an abstract concept, that it's good to have concrete examples to explain them with.

They're actually everywhere. They're just harder to see than some other things. Monoids are really common, really easy to find. You just see them everywhere, like even a pile of rocks. Adding two piles of rocks is a monoid.

Putting two trains together, two halves of a train, that's a monoid, but a monad is a little more difficult to see. I have a tutorial on my site. It's a video where I demonstrate a monad with a bag of bag of candies.

Sometimes you buy a bag of candy, but really it's not a bag of candy if you're being type-specific. It is a bag of bags of candies. There's bags inside the bag. There's a big bag with little bags inside and each of those bags has candies in it.

The monad is a category where you have a join operation, which is basically emptying the smaller bags into the bigger bag. Now you just have a bag of candies, you don't have a bag of bag of candies.

The thing is, we do that operation in our head all the time. If I buy a bag of bags of candies, I don't tell my friend I bought a bag of bags of candies. I say, "I bought a bag of candies." The person understands that. If I give it to them, they're not like, "Hey, dude, you told me it was a bag of candies, but this is a bag of bags of candies."

They understand that that's the same. It's essentially the same. They do the join operation in their head when they're thinking about the thing. All right, that's an example. I thought of another example and I'd like to try it out here on the podcast.

If I have a page of words, I write a document, just one page, I say it's a page of words, but if I look at it more closely with a very analytical eye, it's actually a page of lines of words. It's not a continuous sequence of words. It is a sequence going from up to down of sequences going from left to right, of words.

Let's just simplify like that, no hyphenation, and no...Let's not worry about punctuation right now. Let's just say it's words. It's a sequence of sequences of words. Then you notice the same structure as before. We had a bag of bags of candies. Now we have a sequence of sequences of words.

Yet, in common everyday language, we could say it's a page of words. When you read it, you just read like a continuous stream of words. You do that join operation with your eyes. Basically, you're turning this sequence of sequences into a single sequence. That's what the joint operation is.

The monad is what lets us operate on these nested structures as a single structure. If you're in Haskell, and you're using the IO instance of monad, which is very common, what you're doing is a join...All right, here. You need to make a decision, but you don't have all the information to make the decision.

How do you get the information to make the decision? You read from the database. That's an IO operation. You make the decision, and that's going to generate a new action to do. Let's say, you read a configuration file, and you choose from that configuration file which database to read from, like what's the connection information about it.

What you're doing it's a nested action. It's an action that generates a new action. The monad unnests those and turns them into just one single action. Does that make sense? Kind of does. We use them all the time. These are monads.

This is maybe just my personal opinion, but I don't think a concept as abstract as monad could be useful unless it was very common. Mathematicians are finding regularities in the world and abstracting them into a mental space and then describing them, but those regularities are just pervasive in the world.

They have to be all over, we just don't notice it. We just don't notice them until a mathematician points them out. That's monad. There's two monads, your bag of bags of candies and your page of words.

Now notice, here's another thing, a book isn't just one page. A book is a sequence of pages, but you can still consider it like a stream, a small single sequence of words. You do this page turn operation which lets you continuously get the words in one single stream.

You got a sequence of pages, so the sequence of sequences of sequences of words and because you know how to join them up, you're good. You've got the join operation you need and you can treat it like whew to start reading.

Just like with candy, like, "I ate this whole bag of candy." "No, you ate a bag of bags of candy." "No," you can just say, "I ate the whole bag of candy," because you know the join operation which is open the little bags.

All right. My name is Eric Normand. You can reach me on Twitter. I'm @ericnormand. You can also email me Awesome. See you later. Bye.