Monads are structures commonly used to abstract over the explicit passing of context, thus making programs cleaner and easier to understand. However, their underlying implementation is often described overly complicated or is hidden in a mess of convoluted types and instantiations.
If you go to the roots of a monad, you will find surprisingly simple mechanisms. After experimenting with monads myself in bruijn – an untyped, pure language – I found some beauty in purely functional, yet untyped, implementations that I want to share. As a side effect, the monads become so small that most definitions take only a few characters!
I’ve received some constructive criticism about the inaccessibility of my writing, so here I use a common JavaScript syntax instead of bruijn and try to explain myself better. You can find all the functions as a library in this repository.
I start with a primer on functional data structures – we want to encode state without using structures like objects or arrays! We also don’t care about any potential type checks.