Monads are just WTF in the category of huh?
Scala developers love to discuss Monads, their metaphors, and their many use cases.
burritos are just tacoids in the category of enchiladafunctors— Richard Minerich (@rickasaurus) March 2, 2016
Parts of functional programming (FP) may be built on the mathematical principles from category theory, but at its core, FP is a style of programming. This post aims to prove you don’t need a PhD or be a Haskell programmer to understand these patterns. One disclaimer - the explanation does assume that you know some basics of Scala (like functions, polymorphism, and traits).
We’ll start by defining some of the most referenced components in order to define Monads. We also explore why Monadic design is useful, why it’s dangerous, and discuss some tradeoffs of using these types.
Code examples used can be found here: https://github.com/robinske/monad-examples
Monoid is any type
A that carries the following properties:
appendmethod that can take two instances of
Aand produce another, singular, instance of
A. This method is associative; if you use it to append multiple values together, the grouping of values doesn’t matter.
identityelement such that performing
identityas one of the arguments returns the other argument.
The extension here wouldn’t quite compile, but it’s a good example of using functions as types which will be important later. 1
Monoids are a useful construct in every language. While not always explicitly defined as this type, the four examples above are ubiquitous language features.
Functor is concept that applies to a family of types
F with a single generic type parameter. For example,
List is a type family, because
List[A] is a distinct type for each distinct type
A. A type family
F is a
Functor if it can define a
map method with the following properties:
identityfunction is a no-op.
mapwith a composition of functions is equivalent to composing separate calls to
mapon each function individually.
If you have experience programming in Scala, you’ll know this encompasses a lot of types.
map is a useful method because it allows you to chain operations together (composition). Since mapped functions don’t need to be executed immediately, you can also defer evaluation until the result is needed.
For all practical purposes, implementations of
Functors in Scala are also
Endofunctors (‘endo’ meaning “internal” or “within”) because it’s
map method goes from one category to itself - that category in Scala is Scala Types. 2
The term monad is a bit vacuous if you are not a mathematician. An alternative term is computation builder. 3
We’ve established that we don’t have to be mathematicians to do this, so let’s take a look at the practical implementation details.
Monad is a type that has implemented the
flatMap 4 methods.
pure is a method that takes any type and creates the “computation builder”, wrapping it in the container type or “context”. (Why some people have described monads as burritos 5).
Once you have the monad methods, you can work backwords to define the monoid and functor operations. Here you can see how we can define
You can also define the Monoid operations
identity by using
pure. Above, we defined the trait
Monoid with a generic type. Here, that type is a function:
A => M[B] where
B are not fixed and can be any type. 1
Monoids already allow composition of functions as we saw above.
Monads are useful because they allow you to compose functions for values in a context (
M[_]), something that we see all over our programs (think
Option). Building composable programs is extremely useful, it’s one of the things that functional programmers love the most about all their functional-programming-ness. When we talk about composable architecture we often cite the benefits of modularity, statelessness, and managing side effects:
A functional style pushes side effects to the edges: “gather information, make decisions, act.” A good plan in most life situations too. - Jessica Kerr 6
Building systems in this manner can provide greater maintainability and code reuse, and increase understanding of complex logic by breaking it into smaller, simpler pieces. What’s better is that the benefits of
Monads are largely builtin to the Scala language whether you realize it or not. Using types like
Option means using
Monads, without having to do any of the tedious setup or method definitions.
These are complicated concepts, but hopefully (by applying the principles of FP!) we have broken it into smaller, digestable explanations. The resources and references below are useful if you want to explore this more; I tried not to reference Haskell, but I do like this explanation using pictures.
Slides from my Scala Days talk:
Notes and references:
It’s really difficult to define a syntax in Scala that allows A and B to be any type. People have done it trying to copy something similar in Haskell https://stackoverflow.com/questions/7213676/forall-in-scala but that boilerplate isn’t necessary here to show the concepts. ↩ ↩2
pureis also known as
flatMapis also known as