## 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.

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:

$\mu$
‘s widely known as `join`

, and
$\eta$
as `return`

to programmers.
$T$
is a monad type,
$T^2$
is type composition e.g
$\text{List}^2$
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 $\mu$ 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>>.join().join()`

and `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 $\mu$ 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 $\eta$ (aka return) is:

It means
$\mu$
must yield same result when given either T of results of
$\eta$
or result of
$\eta$
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
$\eta$
corresponds to monoid’s identity.

Hope this picture gives you a clear view at it.

$\eta$ conforms monoid’s identity property.

You may wondering, wait,
$\eta$
is a function! How could a function serve as a identity object?

It’s because applying
$\eta$
to the entire
$Tx$
or to only
$x$
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.

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/

## Read next

### Golang Split String by Delimiter Using Split, SplitN, and SplitAfter

Divyanshu Shekhar -

### Convert Int To String in Golang | Itoa and FormatInt

Divyanshu Shekhar -

### BFE.dev #107. Find the largest difference

jser -

### Hollywood aside, how much damage can hackers actually do with someone's ip address ?

Deschant Kounou -

## Discussion