[BLOG] A no-maths guide to monads

Prompted by someone complaining online about Haskell documentation, I wrote a zero-maths guide to monads (in OCaml, but it applies somewhat more broadly, it’s just OCaml has nice binding opearators):

18 Likes

Nice guide! I appreciate it as I really believe all the resources exercising “mathematics envy” can be actively harmful to the wider functional programming community; I’ve met people who’ve been alienated by such explanations and, as a result, have never even tried functional programming.


I tend to teach monadic style after continuation passing style. For complete beginners, becoming familiar with sequentialising a computation through continuations provides the adequate background for the generalisation to monadic style. Similar to your article, emphasising monadic style (as an operational construct) over monads (as a concept) proves much more valuable in the long run. Then, from there, the nitty-gritty utility of functionalising the sequencing of code in this way becomes apparent (e.g. the continuation closures have, as free variables, the required values to continue).

3 Likes

I tend to teach monadic style after continuation passing style.

Do you have a link to your teaching material implementing this approach?

Sadly not: it tends to be ad-hoc teaching as I’m not employed in any formal capacity as an educator.

I find that CPS isn’t often explicitly taught, yet I’d say it’s effectively - assumed - background in resources about monads (along with being a useful algorithmic tool generally - e.g. in A-Normal Form conversion, for example). There is a lot to be said about the connections between asynchronous libraries (in monadic style), CPS (and trampolining it), static CPS conversion, delimited control operators (dynamically reifying continuations), user-provided schedulers, etc. which I think a holistic resource, seeking to cut through layers of abstraction, would need to cover.

1 Like

Sorry, “not-quite-a-value” strikes me as deeply misleading. Why on earth do people think they ought to be able to understand mathematical and logical concepts without any understanding of mathematics or logic? If that’s what you’re telling your students then you are failing them, badly.

1 Like

I like Evan Czaplicki’s one-liner explanation: monads are callbacks. Of course, that’s not quite the whole story, but at a beginner level we don’t really care to explain the nitty-gritty details because it will just leave the new learners even more confused than they were before.

That said, imho the most important thing to explain about ‘monads’ is not ‘what is it’, but ‘why should I care about it’, and concretely that means showing the before-after of how it makes callback-heavy code look like simple direct-style code. And if that’s the goal I believe it’s not even that important to introduce the ‘bind’ function but rather just go straight to the ( let* ) operator and explain that it works kind of like async/await in other languages but in a more generalized way.

2 Likes

well I don’t have students… but mostly,

I explicitly set the goal of teaching people how to set up and/or use a monad without maths. It’s called “a no-maths guide to monad”. So I don’t expect my readers to understand mathematical and logical concepts nor actually any mathematics or logics. In fact, I expect people who want to understand the mathematical and logical concepts underlying a monad will read a tutorial that has a different name.

From personal experience, you can start learning monads by practice and look up the underlying concepts later on. (Started in the deep end of the Lwt world, learned the monadic laws later, never learned category theory, have successfully written monads and monadic code for a long time now.)

I think that anyone writing code revolving around some boxing data-type and noticing that their binding sites are mostly boilerplate should have the right to add let* to their code.

1 Like

I think that is unnecessary gatekeeping. You don’t have to understand all concepts in depth before using them. You can use lists without knowing monads. I don’t understand in detail how the compiler works, yet I get to use OCaml.

It this tutorial gets people to have a better understanding of monads, I don’t see how its failing them. If the tutorial would say “close this tab and study math” I think it would be failing students in a worse way.

2 Likes