Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Not sure I understand what was the question but yes, you are right, monad in Haskell is defined as a triple:</p> <pre><code>m :: * -&gt; * -- this is endofunctor from haskell types to haskell types! return :: a -&gt; m a (&gt;&gt;=) :: m a -&gt; (a -&gt; m b) -&gt; m b </code></pre> <p>but common definition from category theory is another triple:</p> <pre><code>m :: * -&gt; * return :: a -&gt; m a join :: m (m a) -&gt; m a </code></pre> <p>It is slightly confusing but it's not so hard to show that these two definitions are equal. To do that we need to define <code>join</code> in terms of <code>(&gt;&gt;=)</code> (and vice versa). First step:</p> <pre><code>join :: m (m a) -&gt; m a join x = ? </code></pre> <p>This gives us <code>x :: m (m a)</code>.<br> All we can do with something that have type <code>m _</code> is to aply (>>=) to it:</p> <pre><code>(x &gt;&gt;=) :: (m a -&gt; m b) -&gt; m b </code></pre> <p>Now we need something as a second argument for (>>=), and also, from the type of join we have constraint <code>(x &gt;&gt;= y) :: ma</code>.<br> So <code>y</code> here will have type <code>y :: ma -&gt; ma</code> and <code>id :: a -&gt; a</code> fits it very well:</p> <pre><code>join x = x &gt;&gt;= id </code></pre> <h2>The other way</h2> <pre><code>(&gt;&gt;=) :: ma -&gt; (a -&gt; mb) -&gt; m b (&gt;&gt;=) x y = ? </code></pre> <p>Where <code>x :: m a</code> and <code>y :: a -&gt; m b</code>. To get <code>m b</code> from <code>x</code> and <code>y</code> we need something of type <code>a</code>.<br> Unfortunately, we can't extract <code>a</code> from <code>m a</code>. But we can substitute it for something else (remember, monad is a functor also):</p> <pre><code>fmap :: (a -&gt; b) -&gt; m a -&gt; m b fmap y x :: m (m b) </code></pre> <p>And it's perfectly fits as argument for <code>join</code>: <code>(&gt;&gt;=) x y = join (fmap y x)</code>.</p>
 

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