Monad is Monoid - explained without math
Tags: monad, monoid, categorytheory, math
Published: 2020-03-25 11:55:37

---
title: Monad is Monoid - explained without math
published: true
description: Monad is actually a Monoid and you don’t even need a math
tags: monad, monoid, categorytheory, math
---

## Motivation

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.

{% katex %}
\begin{aligned}
&\text{Monad} \\\\
&\mu :: T^2 \rightarrow T \\\\
&\eta :: I \rightarrow T \\\\
&\mu(T,\mu(T^2)) = \mu(\mu(T^2),T) \\\\
&\mu(\eta(I),T) = \mu(T,\eta(I)) = T \\\\
\end{aligned}

\hspace{1em}

\begin{aligned}
&\text{Monoid} \\\\
&\otimes :: T \times T \rightarrow T \\\\
&i \\\\
&t_1 \otimes (t_2 \otimes t_3) = (t_1 \otimes t_2) \otimes t_3 \\\\
&i \otimes t = t \otimes i = t \\\\
\end{aligned}
{% endkatex %}

Do you see the similarities?
Let’s see in the other point of view.

{% katex %}
\text{Monad} \\\\
\text{}\\\\
\begin{gathered}
1 \\\\
\darr_{\eta} \\\\
T \\\\
\end{gathered}

\hspace{1em}

\begin{gathered}
T^2 \\\\
\darr_{\mu} \\\\
T \\\\
\end{gathered}

\hspace{1em}

\begin{gathered}
T \times 1  \\\\
\darr  \\\\
T  \\\\
\end{gathered}

\begin{gathered}
\xrightarrow{id\times\eta} \\\\
\\\\
\rightarrow \\\\
\end{gathered}

\begin{gathered}
T^2  \\\\
\darr_{\mu}  \\\\
T  \\\\
\end{gathered}

\begin{gathered}
\xleftarrow{\eta\times id}  \\\\
\\\\
\leftarrow  \\\\
\end{gathered}

\begin{gathered}
1 \times T  \\\\
\darr  \\\\
T  \\\\
\end{gathered}

\hspace{1em}

\begin{gathered}
T^3  \\\\
\darr_{id \times \mu}  \\\\
T^2  \\\\
\end{gathered}

\begin{gathered}
\xrightarrow{\mu\times id} \\\\
\\\\
\xrightarrow{\mu}  \\\\
\end{gathered}

\begin{gathered}
T^2  \\\\
\darr_{\mu}  \\\\
T  \\\\
\end{gathered}
{% endkatex %}

{% katex %}
\text{Monoid} \\\\
\text{}\\\\
\begin{gathered}
1 \\\\
\darr_{id} \\\\
T \\\\
\end{gathered}

\hspace{1em}

\begin{gathered}
T\times T \\\\
\darr_{\otimes} \\\\
T \\\\
\end{gathered}

\hspace{1em}

\begin{gathered}
T \times 1 \\\\
\darr  \\\\
T  \\\\
\end{gathered}

\begin{gathered}
\xrightarrow{1\times id} \\\\
\\\\
\rightarrow \\\\
\end{gathered}

\begin{gathered}
T \times T  \\\\
\darr_{\otimes}  \\\\
T  \\\\
\end{gathered}

\begin{gathered}
\xleftarrow{id \times 1}  \\\\
\\\\
\leftarrow  \\\\
\end{gathered}

\begin{gathered}
1 \times T  \\\\
\darr  \\\\
T  \\\\
\end{gathered}

\hspace{1em}

\begin{gathered}
T\times T\times T  \\\\
\darr_{1 \times \otimes}  \\\\
T\times T  \\\\
\end{gathered}

\begin{gathered}
\xrightarrow{\otimes\times 1} \\\\
\\\\
\xrightarrow{\otimes}  \\\\
\end{gathered}

\begin{gathered}
T\times T  \\\\
\darr_{\otimes}  \\\\
T  \\\\
\end{gathered}
{% endkatex %}

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:

{% katex %}
\mu(T^2) \rightarrow T \newline
\eta(I) \rightarrow T
{% endkatex %}

{% katex inline %}\mu{% endkatex %}‘s widely known as `join`, and {% katex inline %}\eta{% endkatex %} as `return` to programmers. {% katex inline %}T{% endkatex %} is a monad type, {% katex inline %}T^2{% endkatex %} is type composition e.g {%katex inline%}\text{List}^2{%endkatex%} 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 {% katex inline %}\mu{% endkatex %} is:

{% katex %}
\mu(T,\mu(T^2)) = \mu(\mu(T^2),T)
{% endkatex %}

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>>.join().join()` and `list<list<list>>.map(join).join()` yields the same result.

This law is the one who corresponds to Monoid’s associativity.

{% katex %}
(t_1 \otimes t_2) \otimes t_3 = t_1 \otimes (t_2 \otimes t_3)
{% endkatex %}

Hope this picture gives you better view at it.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/in8dzhqgobk9g9mvsdsq.jpeg)

Monad with {% katex inline %}\mu{% endkatex %} 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 {% katex inline %}\eta{% endkatex %} (aka return) is:

{% katex %}
\mu(\eta(I),T) = \mu(T,\eta(I))
{% endkatex %}

It means {%katex inline%}\mu{%endkatex%} must yield same result when given either T of results of {%katex inline%}\eta{%endkatex%} or result of {% katex inline %}\eta{% endkatex %} on T. Again, take `List` as example. For  List to be a monad `list.map(return).join()` and `return(list).join()` must yield same result. Law of {% katex inline %}\eta{% endkatex %} corresponds to monoid’s identity.

{% katex %}
i \otimes t = t \otimes i = t
{% endkatex %}

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/37rn3ldwymiursx3p0la.jpeg)

Hope this picture gives you a clear view at it.

{% katex inline %}\eta{% endkatex %} conforms monoid’s identity property.

You may wondering, wait, {% katex inline %}\eta{% endkatex %} is a function! How could a function serve as a identity object? 
It’s because applying {% katex inline %}\eta{% endkatex %} to the entire {% katex inline %}Tx{% endkatex %} or to only {% katex inline %}x{% endkatex %} corresponds to the **act of multiplying** (which can be defined as function) identity object left and right respectively.

## Conclusion

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](https://bartoszmilewski.com/2016/12/27/monads-categorically/).
If you want to learn category theory these are excellent materials:
[Category Theory for Programmers](https://bartoszmilewski.com/2014/10/28/category-theory-for-programmers-the-preface/)
[Math3ma - Category Theory](https://www.math3ma.com/categories/category-theory)
These are my original solutions to Challenges from Category Theory for Programmers: [ingun37.github.io/answers/](https://ingun37.github.io/answers/?sha1=b614f31d04b3bc2b3d23ee4337475251429e5a9f)