Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The definition "Monads are just monoids in the category of endofunctors.", which although true is a bad place to start. It's from a <a href="http://james-iry.blogspot.com/2009/05/brief-incomplete-and-mostly-wrong.html" rel="noreferrer">blog post</a> that was largely intended to be a joke. But if you are interested in the correspondence it can be demonstrated in Haskell:</p> <p>The laymen description of a category is an abstract collection of objects and morphisms between objects. Mappings between categories are called <em>functors</em> and map objects to objects and morphisms to morphisms associatively and preserves identities. An <em>endofunctor</em> is a functor from a category to itself.</p> <pre><code>{-# LANGUAGE MultiParamTypeClasses, ConstraintKinds, FlexibleInstances, FlexibleContexts #-} class Category c where id :: c x x (.) :: c y z -&gt; c x y -&gt; c x z class (Category c, Category d) =&gt; Functor c d t where fmap :: c a b -&gt; d (t a) (t b) type Endofunctor c f = Functor c c f </code></pre> <p>Mappings between functors which satisfy the so called <a href="https://en.wikipedia.org/wiki/Natural_transformation#Definition" rel="noreferrer">naturality conditions</a> are called <em>natural transformations</em>. In Haskell these are polymorphic functions of type: <code>(Functor f, Functor g) =&gt; forall a. f a -&gt; g a</code>.</p> <p>A <em>monad</em> on a category <code>C</code> is three things <code>(T,η,μ)</code>, <code>T</code> is endofunctor and <code>1</code> is the identity functor on <code>C</code>. Mu and eta are two natural transformations that satisfy a <a href="http://ncatlab.org/nlab/show/monad#definition_18" rel="noreferrer">triangle identity</a> and an associativity identity, and are defined as:</p> <ul> <li><code>η : 1 → T</code></li> <li><code>μ : T^2 → T</code></li> </ul> <p>In Haskell <code>μ</code> is <code>join</code> and <code>η</code> is <code>return</code> </p> <ul> <li><code>return :: Monad m =&gt; a -&gt; m a</code></li> <li><code>join :: Monad m =&gt; m (m a) -&gt; m a</code></li> </ul> <p>A categorical definition of Monad in Haskell could be written:</p> <pre><code>class (Endofunctor c t) =&gt; Monad c t where eta :: c a (t a) mu :: c (t (t a)) (t a) </code></pre> <p>The bind operator can be derived from these.</p> <pre><code>(&gt;&gt;=) :: (Monad c t) =&gt; c a (t b) -&gt; c (t a) (t b) (&gt;&gt;=) f = mu . fmap f </code></pre> <p>This is a complete definition, but equivalently you can also show that the Monad laws can be expressed as Monoid laws with a <em>functor category</em>. We can construct this functor category which is a category with objects as functors ( i.e. mappings between categories) and natural transformations (i.e. mappings between functors) as morphisms. In a category of endofunctors all functors are functors between the same category.</p> <pre><code>newtype CatFunctor c t a b = CatFunctor (c (t a) (t b)) </code></pre> <p>We can show this gives rise to a category with functor composition as morphism composition:</p> <pre><code>-- Note needs UndecidableInstances to typecheck instance (Endofunctor c t) =&gt; Category (CatFunctor c t) where id = CatFunctor id (CatFunctor g) . (CatFunctor f) = CatFunctor (g . f) </code></pre> <p>The monoid has the usual definition:</p> <pre><code>class Monoid m where unit :: m mult :: m -&gt; m -&gt; m </code></pre> <p>A monoid over a category of functors has a natural transformations as identity a and a multiplication operation which combines natural transformations. Kleisli composition can be defined to satisfy the multiplication law.</p> <pre><code>(&lt;=&lt;) :: (Monad c t) =&gt; c y (t z) -&gt; c x (t y) -&gt; c x (t z) f &lt;=&lt; g = mu . fmap f . g </code></pre> <p>And so you have it "Monads are just monoids in the category of endofunctors" which is just a "pointfree" version of the normal definition of monads from endofunctors and (mu, eta).</p> <pre><code>instance (Monad c t) =&gt; Monoid (c a (t a)) where unit = eta mult = (&lt;=&lt;) </code></pre> <p>With a bit of substitution one can show that the monoidal properties of <code>(&lt;=&lt;)</code> Are equivalent statement of the triangle and associativity diagrams for the monad's natural transformations.</p> <pre><code>f &lt;=&lt; unit == f unit &lt;=&lt; f == f f &lt;=&lt; (g &lt;=&lt; h) == (f &lt;=&lt; g) &lt;=&lt; h </code></pre> <p>If you're interested in <a href="http://www.stephendiehl.com/posts/monads.html" rel="noreferrer">diagrammatic representations</a> I have written a bit about representing them with string diagrams.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload