# 'In depth understanding of Monad

At one time, I thought I understood Monad. However, when I try to connect my understanding of code to the piece of theory, I found myself still not clear. So here it is:

**part 1.** Here is the popular educational SO link a-monad-is-just-a-monoid-in-the-category-of-endofunctors-whats-the-problem.

My questions:

The highest scored answer says monad is a monoid. And the monoid is consisted of a set S, an operation * and e. The it explains what monad is right after the monoid definition. The first thing that I want to confirm is that the element of the set S is the endofunctor T:X->X mentioned in Monad definition, correct?

The question title quotes the famous phrase saying "a monoid in the category of endofunctors". Here, I am a bit confused by "category of endofunctors". A category as we know consisted of objects and morphisms. So here is it correct to say endofunctors are the objects and the operation * mentioned in the previous question is the morphism? I don't quite understand what does the "

**in**" (a monoid**in**the category...) mean in mathematics. Does it mean "belongs to", "is" or "include"?

**part 2.** Here is another link No, really, what's a monad?, which I initially found very instructive. However recently I found I could not clearly map the code in its example to the formal definition. My experience with Haskell is limited, so I focus on its c++ sample code. Here is the one the author claims it's a monad.

```
#include <iostream>
#include <string>
using namespace std;
string ret(string x) {
return "[" + x + "]";
}
string bind(string x, string (*func) (string)) {
return "[" + func(x.substr(1, x.length() - 2)) + "]";
}
string step1(string x) {
return "Hello " + x;
}
string step2(string x) {
return x + ", monads aren't that complicated.";
}
string step3(string x) {
return "***" + x + "***";
}
template<typename T> T run(T (*ret) (string),
T (*bind) (T, string (*) (string))) {
T x = "friend";
// first, wrap up the value of x
x = ret(x);
// now use wrapCall to run each step
x = bind(x, step1);
x = bind(x, step2);
x = bind(x, step3);
return x;
}
int main() {
cout << run<string>(ret, bind) << "\n";
}
```

Then the author says:

Believe it or not, we’ve just written a monad (specifically, the monad is the pair of bind and ret)!

My questions:

Is the function

`string ret(string x)`

here corresponding to the e in monoid definition? But the e should apply to the set S elements.In this code, which function is the T (the element of the set S)? It's supposed to be a functor. I guess

`step1, step2, step3`

are. However, they don't look like functors but categories. Since they just act like arrows linking objects (type string to type string). A functor should link two categories.Is

`bind`

the operation * (natural transformation μ : T × T → T)?

Hopefully I state my confusion well, any help is appreciate.

## Solution 1:^{[1]}

I think I've figured it out. Let me answer this question by myself. In the C++ code that I posted, the `[]`

is the type constructor, `bind`

function corresponding to the `fmap`

in Haskell, hence they two form a functor, which is the `T`

mentioned here a-monad-is-just-a-monoid-in-the-category-of-endofunctors-whats-the-problem.

The `ret`

function is the natural transformation, ?. The `run`

actually represent the natural transformation, ?.

## Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution | Source |
---|---|

Solution 1 |