In this post I will try to explain why monad is monoid in programmer’s perspective without mathematical prerequisites.
Before I begin I want to encourage you by showing that it’s not difficult at all, in fact, is just around the corner.
Let’s compare the two definitions side by side. You don’t need to understand them, just see how they looks.
Do you see the similarities?
Let’s see in the other point of view.
Similarity is striking! No doubt they must be the same thing at some level! Once you know which property of Monad corresponds to Monoid’s, you will see the oneness.
Monad <-> Monoid
Monad is probably most familiar to programmers as following definition:
‘s widely known as
return to programmers.
is a monad type,
is type composition e.g
would be a List of List
Of course mere existence of functions of the signature won’t automatically turn T into a monad. There are additional laws need to be fulfilled. It seems like they don’t get enough attention they deserve from programmers probably because currently (as far as I know) there’s no compiler that can impose those laws on monad type. But you need to know them because the monadic laws are the ones who corresponds to the monoid’s associativity and identity.
Monad’s Join = Monoid’s Associativity
Monadic law on is:
It means no matter which pair of T you join first you will get same result. Take
List as example. For
List to be a monad, it must have
join defined that
list<list<list>>.map(join).join() yields the same result.
This law is the one who corresponds to Monoid’s associativity.
Hope this picture gives you better view at it.
Monad with as monoid operator conforms to monoid’s associativity.
Three Ts in monad are waiting in line to be evaluated (resolved) into another type. No matter which pair (left or right) gets evaluated first the result will be same by monadic law. This property of monad exactly corresponds to monoid’s associativity. Three Ts in monoid are waiting in line to be evaluated (calculated) into another algebraic value. It as well doesn’t matter which pair gets evaluated first because they are associative by definition of monoid.
Monad’s Return = Monoid’s Identity
Law on (aka return) is:
must yield same result when given either T of results of
or result of
on T. Again, take
List as example. For List to be a monad
return(list).join() must yield same result. Law of
corresponds to monoid’s identity.
Hope this picture gives you a clear view at it.
conforms monoid’s identity property.
You may wondering, wait,
is a function! How could a function serve as a identity object?
It’s because applying to the entire or to only corresponds to the act of multiplying (which can be defined as function) identity object left and right respectively.
Monad is monoid of generic type with one polymorphic parameter.
If you want further learning material on this topic checkout Category Theory for Programmer - Monads Categorically.
If you want to learn category theory these are excellent materials:
Category Theory for Programmers
Math3ma - Category Theory
These are my original solutions to Challenges from Category Theory for Programmers: ingun37.github.io/answers/
Top comments (0)