<commit_before><!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="x-ua-compatible" content="ie=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Monad Anti-tutorial - Vaibhav Sagar</title>
        <link rel="stylesheet" type="text/css" href="../../../../../css/default.css" />
        <link rel="stylesheet" type="text/css" href="../../../../../css/syntax.css" />
        <script>
          (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
          (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
          })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

          ga('create', 'UA-79891461-1', 'auto');
          ga('send', 'pageview');

        </script>
    </head>
    <body>
        <div id="header">
            <div id="logo">
                <a href="../../../../../">Vaibhav Sagar</a>
            </div>
            <div id="navigation">
                <a href="../../../../../about/">About</a>
                <a href="../../../../../talks/">Talks</a>
                <a href="../../../../../archive/">Archive</a>
            </div>
        </div>

        <div id="content">
            <h1>Monad Anti-tutorial</h1>

            <div class="info">
    Posted on 12 October 2016
    
</div>
<div class="info">
    
        Tags: <a href="../../../../../blog/tags/programming/">programming</a>, <a href="../../../../../blog/tags/haskell/">haskell</a>, <a href="../../../../../blog/tags/monads/">monads</a>
    
</div>

<p>I think becoming familiar with the mechanics of working with monads is more important than the question of what a monad is. For this reason, I think monad tutorials solve the wrong problem. I’m giving you a monad anti-tutorial instead, where I don’t try to explain what a monad is but I do try to show you how to use them.</p>
<p>I’m assuming basic familiarity with Haskell’s syntax, but even if you’re not I hope you’ll still be able to follow this.</p>
<p>Let’s start with a definition. <code>Monad</code> in Haskell is a typeclass, which is very much like an interface in many other languages. Because Haskell is not object-oriented, it has types conform to typeclasses instead of objects conforming to interfaces, and each type can declare that it implements a particular typeclass by supplying definitions for each function that the typeclass specifies. <code>Monad</code> looks very much like this:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb1-1" title="1"><span class="kw">class</span> <span class="dt">Monad</span> m <span class="kw">where</span></a>
<a class="sourceLine" id="cb1-2" title="2"><span class="ot">    return ::</span> a   <span class="ot">-&gt;</span> m a</a>
<a class="sourceLine" id="cb1-3" title="3"><span class="ot">    (&gt;&gt;=)  ::</span> m a <span class="ot">-&gt;</span> (a <span class="ot">-&gt;</span> m b) <span class="ot">-&gt;</span> m b</a></code></pre></div>
<p>This means that for any type <code>m</code>, if you provide two function definitions called <code>return</code> and <code>(&gt;&gt;=)</code> (pronounced ‘bind’) with the correct type signatures, you’ve made a <code>Monad</code> instance!</p>
<p>There are two things I think are confusing about this definition:</p>
<ol type="1">
<li><p><code>return</code> is a regular function whose meaning is unrelated to the <code>return</code> statement in most other programming languages.</p></li>
<li><p>Haskell allows you to define infix operators by surrounding them in parentheses, and this one looks a bit odd and vaguely threatening with its spiky edges.</p></li>
</ol>
<p>There are some laws that well-behaved implementations are supposed to observe:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb2-1" title="1"><span class="fu">return</span> a <span class="op">&gt;&gt;=</span> f           <span class="ot">=</span>  f a</a>
<a class="sourceLine" id="cb2-2" title="2">m <span class="op">&gt;&gt;=</span> <span class="fu">return</span>             <span class="ot">=</span>  m</a>
<a class="sourceLine" id="cb2-3" title="3">m <span class="op">&gt;&gt;=</span> (\x <span class="ot">-&gt;</span> f x <span class="op">&gt;&gt;=</span> g)  <span class="ot">=</span>  (m <span class="op">&gt;&gt;=</span> f) <span class="op">&gt;&gt;=</span> g</a></code></pre></div>
<p>I include these because they’re an important part of what a <code>Monad</code> is. These laws aren’t enforced by the typeclass definition so it is possible to define unlawful instances. That’s a very bad idea though.</p>
<p>And that’s all. Really.</p>
<p>So what’s all the hype about?</p>
<p>One reason might be that Haskell has syntax sugar for this particular typeclass. This syntax sugar is known as <code>do</code>-notation, and it allows you to write code that looks like</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb3-1" title="1">foo a b <span class="ot">=</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb3-2" title="2">    a' <span class="ot">&lt;-</span> a</a>
<a class="sourceLine" id="cb3-3" title="3">    b' <span class="ot">&lt;-</span> b</a>
<a class="sourceLine" id="cb3-4" title="4">    <span class="fu">return</span> (a' <span class="op">+</span> b')</a></code></pre></div>
<p>that then gets rewritten to</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" title="1">foo a b <span class="ot">=</span></a>
<a class="sourceLine" id="cb4-2" title="2">    a <span class="op">&gt;&gt;=</span> \a' <span class="ot">-&gt;</span></a>
<a class="sourceLine" id="cb4-3" title="3">    b <span class="op">&gt;&gt;=</span> \b' <span class="ot">-&gt;</span></a>
<a class="sourceLine" id="cb4-4" title="4">    <span class="fu">return</span> (a' <span class="op">+</span> b')</a></code></pre></div>
<p>If you want to define variables in between you can use</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb5-1" title="1">bar a b <span class="ot">=</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb5-2" title="2">    a' <span class="ot">&lt;-</span> a</a>
<a class="sourceLine" id="cb5-3" title="3">    <span class="kw">let</span> a'' <span class="ot">=</span> a' <span class="op">+</span> <span class="dv">1</span></a>
<a class="sourceLine" id="cb5-4" title="4">    b' <span class="ot">&lt;-</span> b</a>
<a class="sourceLine" id="cb5-5" title="5">    <span class="fu">return</span> (a'' <span class="op">+</span> b')</a></code></pre></div>
<p>and this becomes</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb6-1" title="1">bar a b <span class="ot">=</span></a>
<a class="sourceLine" id="cb6-2" title="2">    a <span class="op">&gt;&gt;=</span> \a' <span class="ot">-&gt;</span></a>
<a class="sourceLine" id="cb6-3" title="3">    <span class="kw">let</span> a'' <span class="ot">=</span> a' <span class="op">+</span> <span class="dv">1</span> <span class="kw">in</span></a>
<a class="sourceLine" id="cb6-4" title="4">    b <span class="op">&gt;&gt;=</span> \b' <span class="ot">-&gt;</span></a>
<a class="sourceLine" id="cb6-5" title="5">    <span class="fu">return</span> (a'' <span class="op">+</span> b)</a></code></pre></div>
<p>Sometimes you don’t care about the variable on the left hand side of the <code>&lt;-</code>, and you can omit it like</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb7-1" title="1">baz a b <span class="ot">=</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb7-2" title="2">    a</a>
<a class="sourceLine" id="cb7-3" title="3">    b' <span class="ot">&lt;-</span> b</a>
<a class="sourceLine" id="cb7-4" title="4">    <span class="fu">return</span> b'</a></code></pre></div>
<p>which desugars to</p><commit_msg>Travis build 512 pushed to Github Pages<commit_after><!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="x-ua-compatible" content="ie=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Monad Anti-tutorial - Vaibhav Sagar</title>
        <link rel="stylesheet" type="text/css" href="../../../../../css/default.css" />
        <link rel="stylesheet" type="text/css" href="../../../../../css/syntax.css" />
        <script>
          (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
          (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
          })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

          ga('create', 'UA-79891461-1', 'auto');
          ga('send', 'pageview');

        </script>
    </head>
    <body>
        <div id="header">
            <div id="logo">
                <a href="../../../../../">Vaibhav Sagar</a>
            </div>
            <div id="navigation">
                <a href="../../../../../about/">About</a>
                <a href="../../../../../talks/">Talks</a>
                <a href="../../../../../archive/">Archive</a>
            </div>
        </div>

        <div id="content">
            <h1>Monad Anti-tutorial</h1>

            <div class="info">
    Posted on 12 October 2016
    
</div>
<div class="info">
    
        Tags: <a href="../../../../../blog/tags/programming/">programming</a>, <a href="../../../../../blog/tags/haskell/">haskell</a>, <a href="../../../../../blog/tags/monads/">monads</a>
    
</div>

<p>I think becoming familiar with the mechanics of working with monads is more important than the question of what a monad is. For this reason, I think monad tutorials solve the wrong problem. I’m giving you a monad anti-tutorial instead, where I don’t try to explain what a monad is but I do try to show you how to use them.</p>
<p>I’m assuming basic familiarity with Haskell’s syntax, but even if you’re not I hope you’ll still be able to follow this.</p>
<p>Let’s start with a definition. <code>Monad</code> in Haskell is a typeclass, which is very much like an interface in many other languages. Because Haskell is not object-oriented, it has types conform to typeclasses instead of objects conforming to interfaces, and each type can declare that it implements a particular typeclass by supplying definitions for each function that the typeclass specifies. <code>Monad</code> looks very much like this:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb1-1" title="1"><span class="kw">class</span> <span class="dt">Monad</span> m <span class="kw">where</span></a>
<a class="sourceLine" id="cb1-2" title="2"><span class="ot">    return ::</span> a   <span class="ot">-&gt;</span> m a</a>
<a class="sourceLine" id="cb1-3" title="3"><span class="ot">    (&gt;&gt;=)  ::</span> m a <span class="ot">-&gt;</span> (a <span class="ot">-&gt;</span> m b) <span class="ot">-&gt;</span> m b</a></code></pre></div>
<p>This means that for any type <code>m</code>, if you provide two function definitions called <code>return</code> and <code>(&gt;&gt;=)</code> (pronounced ‘bind’) with the correct type signatures, you’ve made a <code>Monad</code> instance!</p>
<p>There are two things I think are confusing about this definition:</p>
<ol type="1">
<li><p><code>return</code> is a regular function whose meaning is unrelated to the <code>return</code> statement in most other programming languages.</p></li>
<li><p>Haskell allows you to define infix operators by surrounding them in parentheses, and this one looks a bit odd and vaguely threatening with its spiky edges.</p></li>
</ol>
<p>There are some laws that well-behaved implementations are supposed to observe:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb2-1" title="1"><span class="fu">return</span> a <span class="op">&gt;&gt;=</span> f           <span class="ot">=</span>  f a</a>
<a class="sourceLine" id="cb2-2" title="2">m <span class="op">&gt;&gt;=</span> <span class="fu">return</span>             <span class="ot">=</span>  m</a>
<a class="sourceLine" id="cb2-3" title="3">m <span class="op">&gt;&gt;=</span> (\x <span class="ot">-&gt;</span> f x <span class="op">&gt;&gt;=</span> g)  <span class="ot">=</span>  (m <span class="op">&gt;&gt;=</span> f) <span class="op">&gt;&gt;=</span> g</a></code></pre></div>
<p>I include these because they’re an important part of what a <code>Monad</code> is. These laws aren’t enforced by the typeclass definition so it is possible to define unlawful instances. That’s a very bad idea though.</p>
<p>And that’s all. Really.</p>
<p>So what’s all the hype about?</p>
<p>One reason might be that Haskell has syntax sugar for this particular typeclass. This syntax sugar is known as <code>do</code>-notation, and it allows you to write code that looks like</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb3-1" title="1">foo a b <span class="ot">=</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb3-2" title="2">    a' <span class="ot">&lt;-</span> a</a>
<a class="sourceLine" id="cb3-3" title="3">    b' <span class="ot">&lt;-</span> b</a>
<a class="sourceLine" id="cb3-4" title="4">    <span class="fu">return</span> (a' <span class="op">+</span> b')</a></code></pre></div>
<p>that then gets rewritten to</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" title="1">foo a b <span class="ot">=</span></a>
<a class="sourceLine" id="cb4-2" title="2">    a <span class="op">&gt;&gt;=</span> \a' <span class="ot">-&gt;</span></a>
<a class="sourceLine" id="cb4-3" title="3">    b <span class="op">&gt;&gt;=</span> \b' <span class="ot">-&gt;</span></a>
<a class="sourceLine" id="cb4-4" title="4">    <span class="fu">return</span> (a' <span class="op">+</span> b')</a></code></pre></div>
<p>If you want to define variables in between you can use</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb5-1" title="1">bar a b <span class="ot">=</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb5-2" title="2">    a' <span class="ot">&lt;-</span> a</a>
<a class="sourceLine" id="cb5-3" title="3">    <span class="kw">let</span> a'' <span class="ot">=</span> a' <span class="op">+</span> <span class="dv">1</span></a>
<a class="sourceLine" id="cb5-4" title="4">    b' <span class="ot">&lt;-</span> b</a>
<a class="sourceLine" id="cb5-5" title="5">    <span class="fu">return</span> (a'' <span class="op">+</span> b')</a></code></pre></div>
<p>and this becomes</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb6-1" title="1">bar a b <span class="ot">=</span></a>
<a class="sourceLine" id="cb6-2" title="2">    a <span class="op">&gt;&gt;=</span> \a' <span class="ot">-&gt;</span></a>
<a class="sourceLine" id="cb6-3" title="3">    <span class="kw">let</span> a'' <span class="ot">=</span> a' <span class="op">+</span> <span class="dv">1</span> <span class="kw">in</span></a>
<a class="sourceLine" id="cb6-4" title="4">    b <span class="op">&gt;&gt;=</span> \b' <span class="ot">-&gt;</span></a>
<a class="sourceLine" id="cb6-5" title="5">    <span class="fu">return</span> (a'' <span class="op">+</span> b')</a></code></pre></div>
<p>Sometimes you don’t care about the variable on the left hand side of the <code>&lt;-</code>, and you can omit it like</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb7-1" title="1">baz a b <span class="ot">=</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb7-2" title="2">    a</a>
<a class="sourceLine" id="cb7-3" title="3">    b' <span class="ot">&lt;-</span> b</a>
<a class="sourceLine" id="cb7-4" title="4">    <span class="fu">return</span> b'</a></code></pre></div>
<p>which desugars to</p>
{'fraction_non_alphanumeric': 0.23182605579117138, 'fraction_numerical': 0.014037393226211099, 'mean_word_length': 4.204227541187442, 'pattern_counts': {'":': 0, '<': 645, '<?xml version=': 0, '>': 645, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4930747', 'n_tokens_mistral': 5901, 'n_tokens_neox': 5719, 'n_words': 1571}