<p>A monad is an endofunctor <span class="math-container">$T:C\rightarrow C$</span> with natural transformations <span class="math-container">$\eta:id_C\rightarrow T$</span> and <span class="math-container">$\mu:T^2\rightarrow T$</span>.</p>
<p>Being natural transformations mean that <span class="math-container">$$T(f)\circ \eta_A = \eta_B\circ id_C(f)$$</span> should hold and <span class="math-container">$$T(f)\circ\mu_A = \mu_B\circ T^2(f)$$</span> also. These are the algebraic expressions that the defining diagrams of natural transformations commute.</p>
<p>Translating this to the language of functional programming, <span class="math-container">$\eta$</span> is <code>unit</code>, and a polymorphic one, so <span class="math-container">$\eta_{nat}$</span> is the monomorphisation of <code>unit</code> to the <code>nat</code> type. <span class="math-container">$\mu$</span> is <code>join</code> in the same sense. The functor <span class="math-container">$T$</span> consists of two things: it takes object and morphisms. The latter part is called <code>map</code> in functional programming. So the above equations translate to</p>
<pre><code>map f o unit = unit o f
</code></pre>
<p>where, if <code>f</code> is <code>A -&gt; B</code> then this is</p>
<pre><code>map f o unit(A) = unit(B) o f
</code></pre>
<p>but that is usually handled with a polymorphic type system and the second equation is</p>
<pre><code>map f o join = join o map (map f)
</code></pre>
<p>So, <code>map</code> is the morphism-translating part of the endofunctor <span class="math-container">$T$</span>.</p>
<p>But what is the object-translating part of <span class="math-container">$T$</span> in functional programming? Is it the type operator <code>M</code> when we write <code>unit: 'a -&gt; 'a M</code> ?</p>

<functional-programming><monads>
What is the object translating part of a monadic endofunctor?
CC BY-SA 4.0
2023-08-15T12:25:28.933Z
I think this question would be more suitable on [Computer Science SE](https://cs.stackexchange.com/); I'm not sure it is on-topic here.
kaya3
CC BY-SA 4.0
2023-08-15T12:37:55.550Z
I had a similar question concerning monads and a moderator moved it from the Computer Science site to here: https://langdev.stackexchange.com/questions/2675/why-do-they-use-bind-in-haskell-over-kleisli-composition
Gergely
CC BY-SA 4.0
2023-08-15T12:39:22.907Z
That other question is on-topic here because it is about the reasoning behind a design decision made by the Haskell developers. Could you elaborate on how this question relates to programming language design or implementation?
kaya3
CC BY-SA 4.0
2023-08-15T12:41:04.850Z
I let the moderators decide. In an ideal world, a functional programming Stack Exchange site would be the place for my question.
Gergely
CC BY-SA 4.0
2023-08-15T13:48:40.597Z
But Okay, I posted it on CS SE, you can close this at your ease.
Gergely
<p>It is indeed the type operator <code>M</code>. You can think of it as &quot;giving enough data to capture the effects modeled by the monad.&quot;</p>
<p>In general, when thinking about monads as a functional design pattern, you should prefer <code>bind : ('a -&gt; 'b M) -&gt; 'a M -&gt; 'b M</code> to <code>join : 'a M M -&gt; 'a M</code>. They are mutually derivable, but <code>bind</code> plays the more critical role.</p>
<p>To see why, consider an effectful function represented as a function <code>'a -&gt; 'b M</code>. Thus, we can consider the Kleisli category to be the category of effectful functions. To spell that out, the Kleisli category has types as objects, and morphisms <span class="math-container">$A \to B$</span> are functions <code>A -&gt; B M</code>. We can write <span class="math-container">$\hat{f}$</span> for the Kleisli morphism <span class="math-container">$A \to B$</span> with underlying function <code>f : A -&gt; B M</code>. We need an identity and a composition operator. The identity is given by <span class="math-container">$\eta_A : A \to M A$</span> or <code>unit : A -&gt; A M</code>. Note that in the Kleisli category, this is a morphism <span class="math-container">$A \to A$</span>, as desired. To compose <span class="math-container">$\hat{f} : A \to B$</span> and <span class="math-container">$\hat{g} : B \to C$</span>, we need to find a way to combine <code>f : A -&gt; B M</code> and <code>g : B -&gt; C M</code>. We can do this by using <code>bind</code> on <code>g</code> to get it into the right form: <code>f; bind(g) : A -&gt; C M</code> (where <code>;</code> is the diagram-order or program-order composition operator). The monad laws are exactly what are needed for this to form a category.</p>

CC BY-SA 4.0
2023-08-15T13:49:38.707Z
2023-08-15T13:49:38.707Z
Andrew K. Hirsch
# What is the object translating part of a monadic endofunctor?
Posted by: username_0 on 2023-08-15
A monad is an endofunctor $T:C\rightarrow C$ with natural transformations $\eta:id_C\rightarrow T$ and $\mu:T^2\rightarrow T$.
Being natural transformations mean that $$T(f)\circ \eta_A = \eta_B\circ id_C(f)$$ should hold and $$T(f)\circ\mu_A = \mu_B\circ T^2(f)$$ also. These are the algebraic expressions that the defining diagrams of natural transformations commute.
Translating this to the language of functional programming, $\eta$ is unit, and a polymorphic one, so $\eta_{nat}$ is the monomorphisation of unit to the nat type. $\mu$ is join in the same sense. The functor $T$ consists of two things: it takes object and morphisms. The latter part is called map in functional programming. So the above equations translate to
map f o unit = unit o f

where, if f is A -> B then this is
map f o unit(A) = unit(B) o f

but that is usually handled with a polymorphic type system and the second equation is
map f o join = join o map (map f)

So, map is the morphism-translating part of the endofunctor $T$.
But what is the object-translating part of $T$ in functional programming? Is it the type operator M when we write unit: 'a -> 'a M ?
**Tags:** functional-programming monads
> **Comments:**
> **username_1**: I think this question would be more suitable on [Computer Science SE](https://cs.stackexchange.com/); I'm not sure it is on-topic here.  
> **username_0**: I had a similar question concerning monads and a moderator moved it from the Computer Science site to here: https://langdev.stackexchange.com/questions/2675/why-do-they-use-bind-in-haskell-over-kleisli-composition  
> **username_1**: That other question is on-topic here because it is about the reasoning behind a design decision made by the Haskell developers. Could you elaborate on how this question relates to programming language design or implementation?  
> **username_0**: I let the moderators decide. In an ideal world, a functional programming Stack Exchange site would be the place for my question.  
> **username_0**: But Okay, I posted it on CS SE, you can close this at your ease.  
---
## 1 answers
### Answer by username_2 on 2023-08-15. Score: 2 | Accepted answer
It is indeed the type operator `M`. You can think of it as "giving enough data to capture the effects modeled by the monad."

In general, when thinking about monads as a functional design pattern, you should prefer `bind : ('a -> 'b M) -> 'a M -> 'b M` to `join : 'a M M -> 'a M`. They are mutually derivable, but `bind` plays the more critical role.

To see why, consider an effectful function represented as a function `'a -> 'b M`. Thus, we can consider the Kleisli category to be the category of effectful functions. To spell that out, the Kleisli category has types as objects, and morphisms $A \to B$ are functions `A -> B M`. We can write $\hat{f}$ for the Kleisli morphism $A \to B$ with underlying function `f : A -> B M`. We need an identity and a composition operator. The identity is given by $\eta\_A : A \to M A$ or `unit : A -> A M`. Note that in the Kleisli category, this is a morphism $A \to A$, as desired. To compose $\hat{f} : A \to B$ and $\hat{g} : B \to C$, we need to find a way to combine `f : A -> B M` and `g : B -> C M`. We can do this by using `bind` on `g` to get it into the right form: `f; bind(g) : A -> C M` (where `;` is the diagram-order or program-order composition operator). The monad laws are exactly what are needed for this to form a category.
---
