Discussion about this post

User's avatar
corisco's avatar

You can think of a monad as a design pattern with a clear contract. Basically, to have a monad, you must implement bind (also known as flatMap) and the other operations from Functor and Applicative, and that's it.

If you want, you can just ignore category theory, although it provides a nice framework for thinking about problems in abstract terms.

P.S.: One interesting fact I recall that might clarify the mathematical inclination of functional programming is related to the origins of programming itself. Programming languages were conceived before computer systems, in order to solve the decidability problem posed by Hilbert's program. Three programming formalisms were devised at the time: a general recursive function definition by Gödel, Turing machines by Turing, and the lambda calculus by Church. It’s therefore only natural to use mathematics and logic for abstraction, and I think category theory is a nice fit, even for imperative programming.

If you’ve used Rust’s Option and Result types, they can be thought of as categories, or more specifically, as coproducts and monads. But it doesn’t matter if you understand the math behind them; you can use these types without ever worrying about that aspect. You can just view them as design patterns. The benefit is that, because they were developed with mathematical rigor, they are all well-defined and obey specific laws. In contrast, while many object-oriented patterns are useful, not all of them are well-defined or must obey rigid laws.

Expand full comment
Pedro's avatar

Thanks for your honest and objective remarks. I can relate to your remarks on lack of materials on tooling. However, I don't agree with your view that FP is trying to impose a mathematical view on top of a "fundamentally imperative" execution model. Every abstraction layer in a computing is just that - an abstraction layer - and there is nothing more "fundamental" about the imperative model. After all, the hardware doesn't start at the assembly level and even something like the program counter must be implemented using logic gates. Modern CPUs actually spend a lot of effort to paralelise execution of machine instructions while preserving the illusion of sequenciality.

Expand full comment
10 more comments...

No posts