Intro to Monads
Posted on July 20, 2017
What isnât a Monad? And why doesnât it matter?
Some people have said to me they were deeply disappointed that this wasnât a real intro to Monads. Iâm sorry for misleading you. Iâve added some links to some actually useful resources at the end of this post.
Look. Nobody knows what a Monad is. Really.
Why do you think Haskellâs package manager is called Cabal? Itâs because thereâs a cabal of computer scientists making up words and seeing if we actually read their papers or instead just blindly believe everything Bartosz Milewski blogs and follows along (spoiler: thatâs what we do.)
People donât actually read FP papers. People who think they understand FP papers are people positive that they know the names of constellations. They donât. They just made it up and no-one ever corrected them. Yes, Iâm Sirius, people who read FP papers are people who read tea leaves. Thereâs no punch line there, thatâs just something I noticed.
What we should be discussing is what a Monad isnât. Because learning what a Monad is, is a side effect. You might learn what it is and then do stuff. And Monadâs are about never doing things. So explaining what one is, would be a proof by contradictionâŠ
See! You didnât even read that. Letâs see what Brian Lonsdorf and Hardy Jones says on the topic:
Hardy: âHi!â
Brian: âHi!â
Hardy and Brian: âHi!â
No no, after that.
Hardy: âHi!â
Brian: âHi!â
Hardy and Brian: âHi!â
Just skip 20 mins in. Itâs really goodâŠ
Brian: âYou get a Profunctorâ
Hardy: âYeahâ
Brian: âAnd you dot chain the comonadâ
Hardy: âYeahâ
Brian: âAnd you take the inverse procopromonadâ
Hardy: âSips Whisky Yeahâ
Brian: âAnd you over throw capitalismâ
Hardy: âYeahâ
Brian: âBy replacing the automatons with Sage Crystalsâ
Hardy: âYeah. Wait what are you talking about dude?â
Brian: âBecause all we are doing by writing code is accelerating the robopocalypseâ
Salesforce Representative: âDrBoolean in no way speaks on behalf of Salesforce and the Salesforce Robopocalypse.â
Truly.
So what do we do? Do we go back to writing for loops?
OF COURSE NOT
So what then?
-
Describe what you want to do
-
Give it to Haskell
-
When you get some type errors go on Stack Overflow and ask for help
-
When people berate you for your ignorance - open a python shell
-
Write a program that prints random mathematical symbols, greek letters and escape codes
-
Surround the garbage with lorem ipsum and PUBLISH A PAPER.
-
Wait for Elm to implement your program intuitively.
-
Run the elm-package
-
It outputsâŠ
You are in a room. A room without walls. You are alone in the darkness. In the absence of anything. You have achieved complete understanding. You have arrived nowhere.
-
Quit the program.
-
WAKE UP! You were dreaming. There was no program and there was no output.
Our program doesnât do anything. Our program doesnât exist! We live in a simulation. Powered by blood transfusions - of children. DELICIOUS!
Thatâs the point, thatâs the point. 0 Side Effects.
See Monads are Burritos. You donât eat burritos, you give the burrito to a friend and the friend eats the Burrito. Itâs the same thing with Monads.
You donât eat Monads, you write a paper about them and actually you know whatâŠ
BREAKING NEWS BREAKING NEWS BREAKING NEWS
Rememeber Monads? Well FORGET THEM. Because now we have Free Monads.
What is a Free Monad?
Itâs exactly like a Burrito. But instead of giving it to your friend. You tell your friend the Burrito youâd like to have, then they make the Burrito for you then you give the Burrito back to them and they eat it.
Oh and its Free hermano.
The remainder of this briefing will be encoded via lambda calculus.
(λg.λh.h (g f)), then T(n)(λu.x) = (λh.h(f(nâ1)(x)))
Thank you for your [side effect redacted]
What a Monad actually is
A Monad, in the context of software, is an interface for writing sequential code. That interface includes some laws. Laws are just rules that you have to follow to call your code a Monad. Those laws are pretty straight forward and are useful because they guarantee behaviour in a lot of different contexts.
The reason people say Monads âare actually really easyâ with a straight face is because it is just a list of behaviours that are and arenât allowed. And following that list doesnât require any understanding of category theory at all. In a way, learning what a Monad is, is sort of useless information without knowing why itâs useful.
So what a Monad is, is only half of the story. Why a Monad is useful, and how to use them, is potentially a more approachable and useful topic. Hereâs a few resources on how Monadâs can be useful.
- A justification and introduction of Algebraic types: http://james-forbes.com/?/posts/the-perfect-api
- A demonstration of Algebraic types for managing branching logic Railway Oriented Programming
- My demonstration of Array methods as an intro to the Maybe Monad: http://james-forbes.com/?/posts/versatility-of-array-methods
- A free online book walking through FP and using Algebraic types in Javascript: Dr Booleanâs Mostly Adequate Guide
- Tom Hardingâs walkthrough of every single Typeclass in the Javascript Algebraic Type standard: Fantasy Land http://www.tomharding.me/2017/03/03/fantas-eel-and-specification/
Apologies and Footnotes
- Brian and Hardyâs podcast is actually a great resource for learning FP and not anti robo-armageddon propaganda.
- Bartoz Milewskiâs somehow clearly distills the most esoteric mathematical concepts into light reading. No small feat and highly recommended.
- Lots of people do read FP papers, they are real, they are smart. If you find it a struggle donât fret, I absolutely cannot read them and so this article is just me projecting.
- Whatâs with Burritos? Itâs best not to ask. But you did so hereâs a link to one of the least helpful resources on Monads by Doug Crockford.
Thank you for reading. If youâd like to reach out you can get in touch on Mastodon, BlueSky or Twitter.