Skip to content

Instantly share code, notes, and snippets.

View fatcerberus's full-sized avatar

Bruce Pascoe fatcerberus

  • Clark, NJ, USA
View GitHub Profile
@fatcerberus
fatcerberus / monads4all.md
Last active October 19, 2024 07:42
Monads for the Rest of Us

Monads for the Rest of Us

by Bruce Pascoe - 1 May, 2019

"A monad is just a monoid in the category of endofunctors. What's the problem?" ~James Iry[^1]

The problem... is that there are several problems.

It's been said that monads bear a dreadful curse. Once you finally understand what they are, you begin to see them everywhere--but somehow become completely incapable of explaining them to anyone else. Many tutorial writers have tried to break the Great Curse--the Web is lousy with bold attempts and half successes that attest to this--and just as many have failed. Well, I'm here to address the elephant in the room[^2] and tell you that I intend to break the Great Curse once and for all.

There are basically two ways a monad tutorial tends to go. One is a paragraph or two of minimal descriptions of one or two common monads (Haskell's Maybe in particular is very popular), followed by a lot of intimidating Haskell syntax trying to explain--precisely--how it all fits together. This is well

@fatcerberus
fatcerberus / io.md
Last active May 1, 2019 02:03
The Curious Case of the IO Monad

The Curious Case of the IO Monad

by Bruce Pascoe - 30 April, 2019

Haskell's IO monad has been a tremendous source of confusion and misconceptions over the years. At its core, it exists to solve the problem of How do we properly sequence function calls which may have side effects in an environment that assumes all functions are pure? Unfortunately, it is also many people's first exposure to the concept of monads, leading them to assume monads are all about isolating side effects. This is not the case at all; the problem IO solves is specific to IO alone. It just so happens that that problem is solvable within the monadic framework.

What is a monad?

I suppose, in the general case, this question is a little out of my depth. My grasp on category theory is tenuous at best, and any answer I tried to give would likely be wildly inaccurate. However, I am reasonably sure of what monads provide in a programming context, so let's pretend I asked that instead.

A monad is a specific

CARPE NOCTEM

But who prays for Satan? Who, in eighteen centuries, has had the common humanity to pray for the one sinner that needed it most? ~ Mark Twain

Twice victorious is the man who conquers himself in battle. ~ Anonymous

Sometimes ya' gotta back up, to go forward! ~ Bryan the Beach Master, Happy Feet Two