<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
    <channel>
      <title></title>
      <link>https://danieljharvey.github.io</link>
      <description></description>
      <generator>Zola</generator>
      <language>en</language>
      <atom:link href="https://danieljharvey.github.io/rss.xml" rel="self" type="application/rss+xml"/>
      <lastBuildDate>Sun, 07 Sep 2025 00:00:00 +0000</lastBuildDate>
      <item>
          <title>Let&#x27;s write a database (part 4)</title>
          <pubDate>Sun, 07 Sep 2025 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/ordering-and-limits/</link>
          <guid>https://danieljharvey.github.io/ordering-and-limits/</guid>
          <description xml:base="https://danieljharvey.github.io/ordering-and-limits/">&lt;p&gt;Hello, hello, hello. Welcome to part four of this adventure where we&#x27;re making a database by hand. In part three we did some joins, and today we&#x27;re doing to do some limiting.&lt;&#x2F;p&gt;
&lt;p&gt;If you&#x27;d like to look at the code, it&#x27;s &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;danieljharvey&#x2F;lets-build-a-database&quot;&gt;right here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;no-no-no-no-no-no-no-no-no-no-no-no-there-s-no-limits&quot;&gt;No no, no no no no, no no no no, no no, there&#x27;s no limits&lt;&#x2F;h2&gt;
&lt;p&gt;Limit is very straightforward, it&#x27;s used to control the amount of data we get back from a query.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sql&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-sql &quot;&gt;&lt;code class=&quot;language-sql&quot; data-lang=&quot;sql&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;select &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;* &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt; albums &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;limit &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;10
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;...will get 10 items from the albums table. To help decide how we choose &lt;em&gt;which&lt;&#x2F;em&gt; 10, we&#x27;ll need ordering which will come next.&lt;&#x2F;p&gt;
&lt;p&gt;We add a new &lt;code&gt;Limit&lt;&#x2F;code&gt; to the &lt;code&gt;Query&lt;&#x2F;code&gt; type, containing the following:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;pub struct &lt;&#x2F;span&gt;&lt;span&gt;Limit {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;pub &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;: Box&amp;lt;Query&amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;pub &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;limit&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;u64&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Then we add this to our &lt;code&gt;run_query&lt;&#x2F;code&gt; function:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span&gt;Query::Limit(Limit { limit, from }) =&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; QueryStep {
&lt;&#x2F;span&gt;&lt;span&gt;        schema,
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span&gt; rows,
&lt;&#x2F;span&gt;&lt;span&gt;    } = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;run_query&lt;&#x2F;span&gt;&lt;span&gt;(from)?;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; size: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;usize &lt;&#x2F;span&gt;&lt;span&gt;= (*limit).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;try_into&lt;&#x2F;span&gt;&lt;span&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    rows.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;truncate&lt;&#x2F;span&gt;&lt;span&gt;(size);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    Ok(QueryStep { schema, rows })
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That&#x27;s the whole thing. What a disappointing blog post.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;what-s-next&quot;&gt;What&#x27;s next?&lt;&#x2F;h2&gt;
&lt;p&gt;Next time we&#x27;re doing ordering, which is marginally more exciting, but not that exciting tbh.&lt;&#x2F;p&gt;
&lt;p&gt;Make sense? If not, &lt;a href=&quot;&#x2F;contact.html&quot;&gt;get in touch&lt;&#x2F;a&gt;!&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Let&#x27;s write a database (part 3)</title>
          <pubDate>Sun, 24 Aug 2025 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/lets-write-a-database-part-3/</link>
          <guid>https://danieljharvey.github.io/lets-write-a-database-part-3/</guid>
          <description xml:base="https://danieljharvey.github.io/lets-write-a-database-part-3/">&lt;p&gt;Well, well, well, if it isn&#x27;t part 3. Last time we tidied up a bit so that we&#x27;re ready to tackle joins. As ever, all the code lives &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;danieljharvey&#x2F;lets-build-a-database&quot;&gt;right here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;what-is-a-join-then&quot;&gt;What is a join then?&lt;&#x2F;h2&gt;
&lt;p&gt;Say we have a table of Albums that looks something like:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;json&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-json &quot;&gt;&lt;code class=&quot;language-json&quot; data-lang=&quot;json&quot;&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;albumId&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;title&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Best of the Beatles&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;artistId&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1
&lt;&#x2F;span&gt;&lt;span&gt;},
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;albumId&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;title&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Horses&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;artistId&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;...and another table full of Artists like:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;json&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-json &quot;&gt;&lt;code class=&quot;language-json&quot; data-lang=&quot;json&quot;&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;artistId&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;The Beatles&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;},
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;artistId&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Patti Smith&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;A join is what lets us look up the artist name for a given album.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sql&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-sql &quot;&gt;&lt;code class=&quot;language-sql&quot; data-lang=&quot;sql&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;select &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;* &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt; Albums
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;left join&lt;&#x2F;span&gt;&lt;span&gt; Artists on artistId
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Will return something like:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;json&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-json &quot;&gt;&lt;code class=&quot;language-json&quot; data-lang=&quot;json&quot;&gt;&lt;span&gt;[
&lt;&#x2F;span&gt;&lt;span&gt;  {&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;albumId&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;title&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Best of the Beatles&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;The Beatles&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;},
&lt;&#x2F;span&gt;&lt;span&gt;  {&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;albumId&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;title&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Horses&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Patti Smith&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;}
&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;What makes it a &#x27;join&#x27; is that for each row on the left hand side we attach the rows from the right hand table where the &lt;code&gt;join condition&lt;&#x2F;code&gt; (here, &lt;code&gt;Albums.artistId = Artists.artistId&lt;&#x2F;code&gt;). Where one row matches from each side like this, it&#x27;s all very neat.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sql&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-sql &quot;&gt;&lt;code class=&quot;language-sql&quot; data-lang=&quot;sql&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;select &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;* &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt; Artists
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;left join&lt;&#x2F;span&gt;&lt;span&gt; Albums on artistId
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now, each artist may have zero or more albums, so our result will look like&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;json&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-json &quot;&gt;&lt;code class=&quot;language-json&quot; data-lang=&quot;json&quot;&gt;&lt;span&gt;[
&lt;&#x2F;span&gt;&lt;span&gt;  {&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;artistId&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;The Beatles&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;title&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Best of the Beatles&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;},
&lt;&#x2F;span&gt;&lt;span&gt;  {&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;artistId&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;The Beatles&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;title&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Revolver&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;},
&lt;&#x2F;span&gt;&lt;span&gt;  {&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;artistId&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;The Beatles&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;title&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Help&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;},
&lt;&#x2F;span&gt;&lt;span&gt;  {&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;artistId&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Patti Smith&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;title&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Horses&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;},
&lt;&#x2F;span&gt;&lt;span&gt;  {&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;artistId&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Johnny Noalbums&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;}
&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Because &lt;code&gt;The Beatles&lt;&#x2F;code&gt; have multiple albums, we make a copy of each left hand side for each right hand side. What about our old friend &lt;code&gt;Johnny Noalbums&lt;&#x2F;code&gt; though? He has no matching albums, but we still include the left hand side. If we didn&#x27;t want to include him though, we could use an &lt;code&gt;inner join&lt;&#x2F;code&gt; instead:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sql&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-sql &quot;&gt;&lt;code class=&quot;language-sql&quot; data-lang=&quot;sql&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;select &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;* &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt; Artists
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;inner join&lt;&#x2F;span&gt;&lt;span&gt; Albums on artistId
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Here we&#x27;d only include items where there is a match on both sides, so our result would look like:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;json&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-json &quot;&gt;&lt;code class=&quot;language-json&quot; data-lang=&quot;json&quot;&gt;&lt;span&gt;[
&lt;&#x2F;span&gt;&lt;span&gt;  {&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;artistId&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;The Beatles&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;title&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Best of the Beatles&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;},
&lt;&#x2F;span&gt;&lt;span&gt;  {&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;artistId&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;The Beatles&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;title&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Revolver&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;},
&lt;&#x2F;span&gt;&lt;span&gt;  {&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;artistId&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;The Beatles&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;title&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Help&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;},
&lt;&#x2F;span&gt;&lt;span&gt;  {&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;artistId&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Patti Smith&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;title&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Horses&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;}
&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;There are a bunch of other kinds of joins too, but we&#x27;ll stick with these two for now.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;implementing-the-joins&quot;&gt;Implementing the joins&lt;&#x2F;h2&gt;
&lt;p&gt;We&#x27;ll be using the &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Hash_join&quot;&gt;classic hash join&lt;&#x2F;a&gt; to join our tables.&lt;&#x2F;p&gt;
&lt;p&gt;So in our &lt;code&gt;Artists -&amp;gt; Albums&lt;&#x2F;code&gt; join, we start by creating a &lt;code&gt;HashMap&lt;&#x2F;code&gt; and adding an empty array for each &lt;code&gt;artistId&lt;&#x2F;code&gt; found in the &lt;code&gt;Artists&lt;&#x2F;code&gt; table (as &lt;code&gt;artistId&lt;&#x2F;code&gt; is the column we are joining on). It should look like:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;json&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-json &quot;&gt;&lt;code class=&quot;language-json&quot; data-lang=&quot;json&quot;&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#bf616a;color:#2b303b;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;: [],
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#bf616a;color:#2b303b;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;: [],
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#bf616a;color:#2b303b;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;: []
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Then we go through the &lt;code&gt;Albums&lt;&#x2F;code&gt; table, and for every row with an &lt;code&gt;artistId&lt;&#x2F;code&gt;, add the entire row&#x27;s values under the key in the &lt;code&gt;HashMap&lt;&#x2F;code&gt;. Our &lt;code&gt;HashMap&lt;&#x2F;code&gt; should now look like:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;json&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-json &quot;&gt;&lt;code class=&quot;language-json&quot; data-lang=&quot;json&quot;&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#bf616a;color:#2b303b;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;: [
&lt;&#x2F;span&gt;&lt;span&gt;    {&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;artistId&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;:&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Best of the Beatles&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;},
&lt;&#x2F;span&gt;&lt;span&gt;    {&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;artistId&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;:&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Revolver&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;},
&lt;&#x2F;span&gt;&lt;span&gt;    {&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;artistId&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;:&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Help&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;}
&lt;&#x2F;span&gt;&lt;span&gt;  ],
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#bf616a;color:#2b303b;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;: [
&lt;&#x2F;span&gt;&lt;span&gt;    {&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;artistId&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;,&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;:&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Horses&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;}
&lt;&#x2F;span&gt;&lt;span&gt;  ],
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#bf616a;color:#2b303b;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;: []
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The final step is to put everything together. For each row in the &lt;code&gt;Artist&lt;&#x2F;code&gt; table, we:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Look up the &lt;code&gt;artistId&lt;&#x2F;code&gt; in the &lt;code&gt;HashMap&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;For each entry we find, output a new row combining the &lt;code&gt;Artist&lt;&#x2F;code&gt; row with the &lt;code&gt;Album&lt;&#x2F;code&gt; entry from the &lt;code&gt;HashMap&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;if we don&#x27;t find any entries, then&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;for &lt;code&gt;left outer join&lt;&#x2F;code&gt;, emit a new row containing the &lt;code&gt;Artist&lt;&#x2F;code&gt; row columns only, with &lt;code&gt;null&lt;&#x2F;code&gt; for any other columns in &lt;code&gt;Album&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;for &lt;code&gt;inner join&lt;&#x2F;code&gt;, don&#x27;t emit a row.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;in-code&quot;&gt;In code&lt;&#x2F;h2&gt;
&lt;p&gt;We have a new &lt;code&gt;Query::Join&lt;&#x2F;code&gt; item.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;pub struct &lt;&#x2F;span&gt;&lt;span&gt;Join {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;pub &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;join_type&lt;&#x2F;span&gt;&lt;span&gt;: JoinType,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;pub &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;left_from&lt;&#x2F;span&gt;&lt;span&gt;: Box&amp;lt;Query&amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;pub &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;right_from&lt;&#x2F;span&gt;&lt;span&gt;: Box&amp;lt;Query&amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;pub &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;on&lt;&#x2F;span&gt;&lt;span&gt;: JoinOn,
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;pub struct &lt;&#x2F;span&gt;&lt;span&gt;JoinOn {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;pub &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;left&lt;&#x2F;span&gt;&lt;span&gt;: Column,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;pub &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;right&lt;&#x2F;span&gt;&lt;span&gt;: Column,
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;pub enum &lt;&#x2F;span&gt;&lt;span&gt;JoinType {
&lt;&#x2F;span&gt;&lt;span&gt;    Inner,
&lt;&#x2F;span&gt;&lt;span&gt;    LeftOuter,
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We deal with these in &lt;code&gt;run_query&lt;&#x2F;code&gt; as follows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span&gt;Query::Join(Join {
&lt;&#x2F;span&gt;&lt;span&gt;    left_from,
&lt;&#x2F;span&gt;&lt;span&gt;    right_from,
&lt;&#x2F;span&gt;&lt;span&gt;    join_type,
&lt;&#x2F;span&gt;&lt;span&gt;    on,
&lt;&#x2F;span&gt;&lt;span&gt;}) =&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; get the left-hand side items
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; QueryStep {
&lt;&#x2F;span&gt;&lt;span&gt;        schema: left_schema,
&lt;&#x2F;span&gt;&lt;span&gt;        rows: left_rows,
&lt;&#x2F;span&gt;&lt;span&gt;    } = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;run_query&lt;&#x2F;span&gt;&lt;span&gt;(left_from)?;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; get the right-hand side items
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; QueryStep {
&lt;&#x2F;span&gt;&lt;span&gt;        schema: right_schema,
&lt;&#x2F;span&gt;&lt;span&gt;        rows: right_rows,
&lt;&#x2F;span&gt;&lt;span&gt;    } = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;run_query&lt;&#x2F;span&gt;&lt;span&gt;(right_from)?;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; join the left and right hand sides
&lt;&#x2F;span&gt;&lt;span&gt;    join::hash_join(
&lt;&#x2F;span&gt;&lt;span&gt;        left_rows,
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;amp;left_schema,
&lt;&#x2F;span&gt;&lt;span&gt;        right_rows,
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;amp;right_schema,
&lt;&#x2F;span&gt;&lt;span&gt;        on,
&lt;&#x2F;span&gt;&lt;span&gt;        join_type,
&lt;&#x2F;span&gt;&lt;span&gt;    )
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Here is the &lt;code&gt;hash_join&lt;&#x2F;code&gt; function:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;pub fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;hash_join&lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;left_rows&lt;&#x2F;span&gt;&lt;span&gt;: Vec&amp;lt;Row&amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;left_schema&lt;&#x2F;span&gt;&lt;span&gt;: &amp;amp;Schema,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;right_rows&lt;&#x2F;span&gt;&lt;span&gt;: Vec&amp;lt;Row&amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;right_schema&lt;&#x2F;span&gt;&lt;span&gt;: &amp;amp;Schema,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;on&lt;&#x2F;span&gt;&lt;span&gt;: &amp;amp;JoinOn,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;join_type&lt;&#x2F;span&gt;&lt;span&gt;: &amp;amp;JoinType,
&lt;&#x2F;span&gt;&lt;span&gt;) -&amp;gt; Result&amp;lt;QueryStep, QueryError&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let mut&lt;&#x2F;span&gt;&lt;span&gt; build_items = HashMap::new();
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; add all the relevant `on` values to map,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; left_row in &amp;amp;left_rows {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; value = 
&lt;&#x2F;span&gt;&lt;span&gt;          left_row
&lt;&#x2F;span&gt;&lt;span&gt;            .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;get_column&lt;&#x2F;span&gt;&lt;span&gt;(&amp;amp;on.left, left_schema)
&lt;&#x2F;span&gt;&lt;span&gt;            .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;ok_or_else&lt;&#x2F;span&gt;&lt;span&gt;(|| {
&lt;&#x2F;span&gt;&lt;span&gt;              QueryError::ColumnNotFoundInSchema {
&lt;&#x2F;span&gt;&lt;span&gt;                  column_name: on.left.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;(),
&lt;&#x2F;span&gt;&lt;span&gt;              }
&lt;&#x2F;span&gt;&lt;span&gt;            })?;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;        build_items.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;insert&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;calculate_hash&lt;&#x2F;span&gt;&lt;span&gt;(value), vec![]);
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; collect all the different right side values
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; right_row in right_rows {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; value = right_row
&lt;&#x2F;span&gt;&lt;span&gt;            .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;get_column&lt;&#x2F;span&gt;&lt;span&gt;(&amp;amp;on.right, right_schema)
&lt;&#x2F;span&gt;&lt;span&gt;            .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;ok_or_else&lt;&#x2F;span&gt;&lt;span&gt;(|| QueryError::ColumnNotFoundInSchema {
&lt;&#x2F;span&gt;&lt;span&gt;                column_name: on.right.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;(),
&lt;&#x2F;span&gt;&lt;span&gt;            })?;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; this assumes left or inner join and 
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; ignores where there&amp;#39;s no left match
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if let &lt;&#x2F;span&gt;&lt;span&gt;Some(items) = build_items.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;get_mut&lt;&#x2F;span&gt;&lt;span&gt;(&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;calculate_hash&lt;&#x2F;span&gt;&lt;span&gt;(value)) {
&lt;&#x2F;span&gt;&lt;span&gt;            items.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;push&lt;&#x2F;span&gt;&lt;span&gt;(right_row.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;());
&lt;&#x2F;span&gt;&lt;span&gt;        }
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let mut&lt;&#x2F;span&gt;&lt;span&gt; output_rows = vec![];
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; left_row in left_rows {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; value = 
&lt;&#x2F;span&gt;&lt;span&gt;          left_row
&lt;&#x2F;span&gt;&lt;span&gt;            .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;get_column&lt;&#x2F;span&gt;&lt;span&gt;(&amp;amp;on.left, left_schema)
&lt;&#x2F;span&gt;&lt;span&gt;            .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;ok_or_else&lt;&#x2F;span&gt;&lt;span&gt;(|| {
&lt;&#x2F;span&gt;&lt;span&gt;              QueryError::ColumnNotFoundInSchema {
&lt;&#x2F;span&gt;&lt;span&gt;                  column_name: on.left.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;(),
&lt;&#x2F;span&gt;&lt;span&gt;              }
&lt;&#x2F;span&gt;&lt;span&gt;            })?;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if let &lt;&#x2F;span&gt;&lt;span&gt;Some(rhs) = build_items.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;get&lt;&#x2F;span&gt;&lt;span&gt;(&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;calculate_hash&lt;&#x2F;span&gt;&lt;span&gt;(value)) {
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; rhs.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;is_empty&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; if left outer join
&lt;&#x2F;span&gt;&lt;span&gt;                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if let &lt;&#x2F;span&gt;&lt;span&gt;JoinType::LeftOuter = join_type {
&lt;&#x2F;span&gt;&lt;span&gt;                    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let mut&lt;&#x2F;span&gt;&lt;span&gt; whole_row = left_row.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;                    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; we can&amp;#39;t find value, so add a bunch of nulls
&lt;&#x2F;span&gt;&lt;span&gt;                    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;_ in &amp;amp;right_schema.columns {
&lt;&#x2F;span&gt;&lt;span&gt;                        whole_row.items.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;push&lt;&#x2F;span&gt;&lt;span&gt;(serde_json::Value::Null);
&lt;&#x2F;span&gt;&lt;span&gt;                    }
&lt;&#x2F;span&gt;&lt;span&gt;                    output_rows.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;push&lt;&#x2F;span&gt;&lt;span&gt;(whole_row);
&lt;&#x2F;span&gt;&lt;span&gt;                }
&lt;&#x2F;span&gt;&lt;span&gt;            } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; item in rhs {
&lt;&#x2F;span&gt;&lt;span&gt;                    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let mut&lt;&#x2F;span&gt;&lt;span&gt; whole_row = left_row.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;                    whole_row.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;extend&lt;&#x2F;span&gt;&lt;span&gt;(item.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;());
&lt;&#x2F;span&gt;&lt;span&gt;                    output_rows.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;push&lt;&#x2F;span&gt;&lt;span&gt;(whole_row);
&lt;&#x2F;span&gt;&lt;span&gt;                }
&lt;&#x2F;span&gt;&lt;span&gt;            }
&lt;&#x2F;span&gt;&lt;span&gt;        }
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let mut&lt;&#x2F;span&gt;&lt;span&gt; schema = left_schema.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;    schema.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;extend&lt;&#x2F;span&gt;&lt;span&gt;(right_schema.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;());
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    Ok(QueryStep {
&lt;&#x2F;span&gt;&lt;span&gt;        rows: output_rows,
&lt;&#x2F;span&gt;&lt;span&gt;        schema,
&lt;&#x2F;span&gt;&lt;span&gt;    })
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;is-that-it&quot;&gt;Is that it?&lt;&#x2F;h2&gt;
&lt;p&gt;Well yeah, sorta. We only handle two kinds of joins here, &lt;code&gt;left outer&lt;&#x2F;code&gt; and &lt;code&gt;inner&lt;&#x2F;code&gt;, and so there&#x27;s a bunch more we can do here. Really with joins the trick is optimising them right - ideally we want the smaller table on the left hand side, for instance. We&#x27;ll get to that once we start implementing some optimisations.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;what-s-next&quot;&gt;What&#x27;s next?&lt;&#x2F;h2&gt;
&lt;p&gt;Haven&#x27;t really decided yet actually. Perhaps some nice optimisations? Let&#x27;s see what takes our fancy.&lt;&#x2F;p&gt;
&lt;p&gt;Make sense? If not, &lt;a href=&quot;&#x2F;contact.html&quot;&gt;get in touch&lt;&#x2F;a&gt;!&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Let&#x27;s write a database (part 2)</title>
          <pubDate>Tue, 05 Aug 2025 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/lets-write-a-database-part-2/</link>
          <guid>https://danieljharvey.github.io/lets-write-a-database-part-2/</guid>
          <description xml:base="https://danieljharvey.github.io/lets-write-a-database-part-2/">&lt;p&gt;Hello, hello, hello. Welcome to part two of this adventure where we&#x27;re making a database by hand. In part one we stole a SQL parser, did some table scans, filtered some results, and projected the ones we&#x27;re interested in. I promised we&#x27;d look at joins today, but first we need to do a bit of housekeeping.&lt;&#x2F;p&gt;
&lt;p&gt;If you&#x27;d like to look at the code, it&#x27;s &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;danieljharvey&#x2F;lets-build-a-database&quot;&gt;right here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;storing-our-rows-better&quot;&gt;Storing our rows better&lt;&#x2F;h2&gt;
&lt;p&gt;Previously we passed our rows around as &lt;code&gt;Vec&amp;lt;serde_json::Value&amp;gt;&lt;&#x2F;code&gt; so each row was a JSON value that looked like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;json&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-json &quot;&gt;&lt;code class=&quot;language-json&quot; data-lang=&quot;json&quot;&gt;&lt;span&gt;{ 
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;id&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Mr Horse&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;age&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;100
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is poor for two reasons:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Having the column names in every row is wasteful&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;If we want to rename columns (or disambiguate the &quot;name&quot; column from two tables that have been joined), we have to change every row&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Instead we have a &lt;code&gt;Row&lt;&#x2F;code&gt; type that contains a &lt;code&gt;Vec&lt;&#x2F;code&gt; full of individual &lt;code&gt;serde_json::Value&lt;&#x2F;code&gt; types for each column entry:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;pub struct &lt;&#x2F;span&gt;&lt;span&gt;Row {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;pub &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;items&lt;&#x2F;span&gt;&lt;span&gt;: Vec&amp;lt;serde_json::Value&amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And we pass around a single &lt;code&gt;Schema&lt;&#x2F;code&gt; type that holds all the column names:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;pub struct &lt;&#x2F;span&gt;&lt;span&gt;Schema {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;pub &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;columns&lt;&#x2F;span&gt;&lt;span&gt;: Vec&amp;lt;Column&amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;pub struct &lt;&#x2F;span&gt;&lt;span&gt;Column {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;pub &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;: String,
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;changes-to-our-query-functions&quot;&gt;Changes to our query functions&lt;&#x2F;h2&gt;
&lt;p&gt;Previously we just returned a big pile of rows from each query function, but now each one returns a &lt;code&gt;QueryStep&lt;&#x2F;code&gt; type:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;pub struct &lt;&#x2F;span&gt;&lt;span&gt;QueryStep {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;pub &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;schema&lt;&#x2F;span&gt;&lt;span&gt;: Schema,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;pub &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rows&lt;&#x2F;span&gt;&lt;span&gt;: Vec&amp;lt;Row&amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now we have a bunch of rows, and a &lt;code&gt;Schema&lt;&#x2F;code&gt; so we know what&#x27;s in them.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;from&quot;&gt;From&lt;&#x2F;h3&gt;
&lt;p&gt;When selecting fields, we grab all the rows, and then return a schema too. These are hardcoded for now:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;schema&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;table_name&lt;&#x2F;span&gt;&lt;span&gt;: &amp;amp;TableName) -&amp;gt; Vec&amp;lt;Column&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;match&lt;&#x2F;span&gt;&lt;span&gt; table_name.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;as_str&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Album&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; =&amp;gt; vec![&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;AlbumId&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;into&lt;&#x2F;span&gt;&lt;span&gt;(), &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Title&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;into&lt;&#x2F;span&gt;&lt;span&gt;(), &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;ArtistId&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;into&lt;&#x2F;span&gt;&lt;span&gt;()],
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Artist&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; =&amp;gt; vec![&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;ArtistId&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;into&lt;&#x2F;span&gt;&lt;span&gt;(), &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;into&lt;&#x2F;span&gt;&lt;span&gt;()],
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Track&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; =&amp;gt; vec![
&lt;&#x2F;span&gt;&lt;span&gt;            &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;TrackId&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;into&lt;&#x2F;span&gt;&lt;span&gt;(),
&lt;&#x2F;span&gt;&lt;span&gt;            &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;into&lt;&#x2F;span&gt;&lt;span&gt;(),
&lt;&#x2F;span&gt;&lt;span&gt;            &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;AlbumId&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;into&lt;&#x2F;span&gt;&lt;span&gt;(),
&lt;&#x2F;span&gt;&lt;span&gt;            &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;MediaTypeId&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;into&lt;&#x2F;span&gt;&lt;span&gt;(),
&lt;&#x2F;span&gt;&lt;span&gt;            &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;GenreId&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;into&lt;&#x2F;span&gt;&lt;span&gt;(),
&lt;&#x2F;span&gt;&lt;span&gt;            &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Composer&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;into&lt;&#x2F;span&gt;&lt;span&gt;(),
&lt;&#x2F;span&gt;&lt;span&gt;            &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Milliseconds&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;into&lt;&#x2F;span&gt;&lt;span&gt;(),
&lt;&#x2F;span&gt;&lt;span&gt;            &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Bytes&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;into&lt;&#x2F;span&gt;&lt;span&gt;(),
&lt;&#x2F;span&gt;&lt;span&gt;            &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;UnitPrice&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;into&lt;&#x2F;span&gt;&lt;span&gt;(),
&lt;&#x2F;span&gt;&lt;span&gt;        ],
&lt;&#x2F;span&gt;&lt;span&gt;        _ =&amp;gt; todo!(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;unknown schema&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;),
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;filtering&quot;&gt;Filtering&lt;&#x2F;h3&gt;
&lt;p&gt;For instance, this means our filtering now looks like this, passing the schema through unchanged from whatever &lt;code&gt;Query&lt;&#x2F;code&gt; it wraps:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span&gt;Query::Filter(Filter { from, filter }) =&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; QueryStep {
&lt;&#x2F;span&gt;&lt;span&gt;        schema,
&lt;&#x2F;span&gt;&lt;span&gt;        rows,
&lt;&#x2F;span&gt;&lt;span&gt;    } = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;run_query&lt;&#x2F;span&gt;&lt;span&gt;(from)?;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let mut&lt;&#x2F;span&gt;&lt;span&gt; filtered_rows = vec![];
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; row in rows {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;filter::apply_predicate(&amp;amp;row, &amp;amp;schema, filter)? {
&lt;&#x2F;span&gt;&lt;span&gt;            filtered_rows.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;push&lt;&#x2F;span&gt;&lt;span&gt;(row);
&lt;&#x2F;span&gt;&lt;span&gt;        }
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    Ok(QueryStep {
&lt;&#x2F;span&gt;&lt;span&gt;        schema,
&lt;&#x2F;span&gt;&lt;span&gt;        rows: filtered_rows
&lt;&#x2F;span&gt;&lt;span&gt;    })
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;projections&quot;&gt;Projections&lt;&#x2F;h3&gt;
&lt;p&gt;Our projections let us drop and reorder fields, so they&#x27;ll change the schemas as well as the rows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span&gt;Query::Project(Project { from, fields }) =&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; QueryStep {
&lt;&#x2F;span&gt;&lt;span&gt;        schema,
&lt;&#x2F;span&gt;&lt;span&gt;        rows,
&lt;&#x2F;span&gt;&lt;span&gt;    } = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;run_query&lt;&#x2F;span&gt;&lt;span&gt;(from)?;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let mut&lt;&#x2F;span&gt;&lt;span&gt; projected_rows = vec![];
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; row in &amp;amp;rows {
&lt;&#x2F;span&gt;&lt;span&gt;        projected_rows.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;push&lt;&#x2F;span&gt;&lt;span&gt;(project::project_fields(row, &amp;amp;schema, fields)?);
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; schema = project::project_schema(&amp;amp;schema, fields)?;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    Ok(QueryStep {
&lt;&#x2F;span&gt;&lt;span&gt;        schema,
&lt;&#x2F;span&gt;&lt;span&gt;        rows: projected_rows,
&lt;&#x2F;span&gt;&lt;span&gt;    })
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As well as a &lt;code&gt;project_fields&lt;&#x2F;code&gt; function we have a matching &lt;code&gt;project_schema&lt;&#x2F;code&gt; function that creates a new schema.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;pub fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;project_schema&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;schema&lt;&#x2F;span&gt;&lt;span&gt;: &amp;amp;Schema, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;fields&lt;&#x2F;span&gt;&lt;span&gt;: &amp;amp;[Column]) 
&lt;&#x2F;span&gt;&lt;span&gt;  -&amp;gt; Result&amp;lt;Schema, QueryError&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let mut&lt;&#x2F;span&gt;&lt;span&gt; columns = vec![];
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; field in fields {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; index = schema.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;get_index_for_column&lt;&#x2F;span&gt;&lt;span&gt;(field).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;ok_or_else&lt;&#x2F;span&gt;&lt;span&gt;(|| {
&lt;&#x2F;span&gt;&lt;span&gt;      QueryError::ColumnNotFoundInSchema {
&lt;&#x2F;span&gt;&lt;span&gt;        column_name: field.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;(),
&lt;&#x2F;span&gt;&lt;span&gt;      }
&lt;&#x2F;span&gt;&lt;span&gt;    })?;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; column = schema
&lt;&#x2F;span&gt;&lt;span&gt;      .columns
&lt;&#x2F;span&gt;&lt;span&gt;      .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;get&lt;&#x2F;span&gt;&lt;span&gt;(index)
&lt;&#x2F;span&gt;&lt;span&gt;      .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;ok_or&lt;&#x2F;span&gt;&lt;span&gt;(QueryError::IndexNotFoundInSchema { index })?;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    columns.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;push&lt;&#x2F;span&gt;&lt;span&gt;(column.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;());
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  Ok(Schema { columns })
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;outputting-everything&quot;&gt;Outputting everything&lt;&#x2F;h3&gt;
&lt;p&gt;We still want to output everything in JSON as before, so our &lt;code&gt;QueryStep&lt;&#x2F;code&gt; has a &lt;code&gt;to_json&lt;&#x2F;code&gt; function that puts everything back as it was before.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;impl &lt;&#x2F;span&gt;&lt;span&gt;QueryStep {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; reconstruct JSON output
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;pub fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;to_json&lt;&#x2F;span&gt;&lt;span&gt;(&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;) -&amp;gt; serde_json::Value {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let mut&lt;&#x2F;span&gt;&lt;span&gt; output_rows = vec![];
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; row in &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;.rows {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let mut&lt;&#x2F;span&gt;&lt;span&gt; output_row = serde_json::Map::new();
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; column in &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;.schema.columns {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; value = row.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;get_column&lt;&#x2F;span&gt;&lt;span&gt;(column, &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;.schema).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;        output_row.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;insert&lt;&#x2F;span&gt;&lt;span&gt;(column.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;to_string&lt;&#x2F;span&gt;&lt;span&gt;(), value.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;());
&lt;&#x2F;span&gt;&lt;span&gt;      }
&lt;&#x2F;span&gt;&lt;span&gt;      output_rows.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;push&lt;&#x2F;span&gt;&lt;span&gt;(serde_json::Value::Object(output_row));
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;    serde_json::Value::Array(output_rows)
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;After this, all the outputs look the same as before and our tests all pass again. Nice!&lt;&#x2F;p&gt;
&lt;h2 id=&quot;what-s-next&quot;&gt;What&#x27;s next?&lt;&#x2F;h2&gt;
&lt;p&gt;OK, we&#x27;ve got all our ducks in a row, next time we&#x27;ll do the joins, I promise.&lt;&#x2F;p&gt;
&lt;p&gt;Make sense? If not, &lt;a href=&quot;&#x2F;contact.html&quot;&gt;get in touch&lt;&#x2F;a&gt;!&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Let&#x27;s write a database (part 1)</title>
          <pubDate>Sat, 26 Jul 2025 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/fuck-it-lets-write-a-database/</link>
          <guid>https://danieljharvey.github.io/fuck-it-lets-write-a-database/</guid>
          <description xml:base="https://danieljharvey.github.io/fuck-it-lets-write-a-database/">&lt;p&gt;So, confession, I love playing on the computer, but I&#x27;m terrible at SQL. I know that it&#x27;s Good or whatever and there&#x27;s this whole very sensible looking &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Relational_algebra&quot;&gt;Relational Algebra&lt;&#x2F;a&gt; behind it all, but like when I have to interact with it, I get the job done and then immediately wipe whatever I learned from my brain.&lt;&#x2F;p&gt;
&lt;p&gt;Enough is enough. It&#x27;s time to learn it properly. And what better way than to write an ANSI SQL database from scratch!&lt;&#x2F;p&gt;
&lt;h2 id=&quot;disclaimers-etc&quot;&gt;Disclaimers etc&lt;&#x2F;h2&gt;
&lt;p&gt;Setting some expectations, this is very much a &quot;fun&quot; learning project, and we&#x27;re gonna get a lot of things wrong. We&#x27;ll be using Rust, because I write that a lot atm and so it&#x27;s what my brain thinks in, but I would definitely not read this hoping for a Rust tutorial because I am almost as bad at Rust as SQL. I am going to describe things in a very hand-wavy way that will probably annoy people that know what they&#x27;re talking about, but it&#x27;s my blog and I can do what I want.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;so-let-s-do-it&quot;&gt;So, let&#x27;s do it?&lt;&#x2F;h2&gt;
&lt;p&gt;OK. So what we&#x27;re going to do in part 1 is:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Make (steal) a SQL parser&lt;&#x2F;li&gt;
&lt;li&gt;Do a table scan (&lt;code&gt;From&lt;&#x2F;code&gt;)&lt;&#x2F;li&gt;
&lt;li&gt;Filter results from that table (&lt;code&gt;Filter&lt;&#x2F;code&gt;)&lt;&#x2F;li&gt;
&lt;li&gt;Choose the fields we want to look at (&lt;code&gt;Project&lt;&#x2F;code&gt;)&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;We&#x27;re going to be concentrating on the Query Engine part of the database initially,mostly because I&#x27;m more interested in it, so our tables are going to be static JSON files taken from the &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;marko-knoebl&#x2F;chinook-database-json&quot;&gt;chinook dataset&lt;&#x2F;a&gt;. We are using this because it&#x27;s full of rock albums and it&#x27;s nice to be reminder that Led Zeppelin are a totally sick band from time to time.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;our-types&quot;&gt;Our types&lt;&#x2F;h2&gt;
&lt;p&gt;We might not be implementing in Haskell but it&#x27;s spirit lives on - let&#x27;s start by defining the types of our query.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; the type that defines a query
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;enum &lt;&#x2F;span&gt;&lt;span&gt;Query {
&lt;&#x2F;span&gt;&lt;span&gt;  From(From),
&lt;&#x2F;span&gt;&lt;span&gt;  Filter(Filter),
&lt;&#x2F;span&gt;&lt;span&gt;  Project(Project)
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; select `FROM` a table
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span&gt;From {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;table_name&lt;&#x2F;span&gt;&lt;span&gt;: TableName
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; filter the results for a `WHERE` clause
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span&gt;Filter {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;: Box&amp;lt;Query&amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;filter&lt;&#x2F;span&gt;&lt;span&gt;: Expr,
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; boolean expression type
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;pub enum &lt;&#x2F;span&gt;&lt;span&gt;Expr {
&lt;&#x2F;span&gt;&lt;span&gt;  ColumnComparison {
&lt;&#x2F;span&gt;&lt;span&gt;    column: Column,
&lt;&#x2F;span&gt;&lt;span&gt;    op: Op,
&lt;&#x2F;span&gt;&lt;span&gt;    literal: serde_json::Value,
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; compare two items  
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;pub enum &lt;&#x2F;span&gt;&lt;span&gt;Op {
&lt;&#x2F;span&gt;&lt;span&gt;  Equals,
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; choose which fields to return 
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span&gt;Project {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt;: Box&amp;lt;Query&amp;gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;fields&lt;&#x2F;span&gt;&lt;span&gt;: Vec&amp;lt;ColumnName&amp;gt; 
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; name of a table
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span&gt;TableName(pub String);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; name of a column
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span&gt;ColumnName(pub String);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;a-parser&quot;&gt;A parser&lt;&#x2F;h2&gt;
&lt;p&gt;This could quickly become a parsing tutorial and we don&#x27;t want that, so we&#x27;re going to use the &lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;sqlparser&#x2F;latest&#x2F;sqlparser&quot;&gt;sqlparser&lt;&#x2F;a&gt; crate. It takes a string input and returns either it&#x27;s own AST or an error. We&#x27;ll pattern match on this and extract only the things we support into a &lt;code&gt;Query&lt;&#x2F;code&gt; type we defined above.&lt;&#x2F;p&gt;
&lt;p&gt;Nothing about this is very interesting, so I will just &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;danieljharvey&#x2F;lets-build-a-database&#x2F;blob&#x2F;main&#x2F;crates&#x2F;core&#x2F;src&#x2F;parser.rs#L51&quot;&gt;link to it&lt;&#x2F;a&gt;. Know that we parse some SQL and make the types above.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;our-run-query-function&quot;&gt;Our &lt;code&gt;run_query&lt;&#x2F;code&gt; function&lt;&#x2F;h2&gt;
&lt;p&gt;Now we&#x27;ve worked out what the user wants, we need to run the query. Initially we&#x27;ll do this by matching on the &lt;code&gt;Query&lt;&#x2F;code&gt; type.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;pub fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;run_query&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;query&lt;&#x2F;span&gt;&lt;span&gt;: &amp;amp;Query) -&amp;gt; Vec&amp;lt;serde_json::Value&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;match&lt;&#x2F;span&gt;&lt;span&gt; query {
&lt;&#x2F;span&gt;&lt;span&gt;    Query::From(_) =&amp;gt; todo!(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Query::From&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;), 
&lt;&#x2F;span&gt;&lt;span&gt;    Query::Filter(_) =&amp;gt; todo!(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Query::Filter&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;), 
&lt;&#x2F;span&gt;&lt;span&gt;    Query::Project(_) =&amp;gt; todo!(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Query::Project&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We&#x27;ll fill these &lt;code&gt;todo!&lt;&#x2F;code&gt; out one by one now.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;query-from&quot;&gt;Query::From&lt;&#x2F;h2&gt;
&lt;p&gt;The first thing we&#x27;ll implement is a simple table scan. A table scan is &quot;get all of the rows in the table&quot;. If you&#x27;re thinking &quot;that doesn&#x27;t sound wildly performant&quot;, rest assured your Software Craftsperson spidey-sense is still working correctly. However, our tables only have ~300 items in them, so for now we&#x27;ll live with it until we start thinking about indexes.&lt;&#x2F;p&gt;
&lt;p&gt;Here is some code. Forgive me, Padre.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;table_scan&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;table_name&lt;&#x2F;span&gt;&lt;span&gt;: &amp;amp;TableName) -&amp;gt; Vec&amp;lt;serde_json::Value&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;match&lt;&#x2F;span&gt;&lt;span&gt; table_name.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;as_str&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Album&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; =&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; my_str = include_str!(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;..&#x2F;static&#x2F;Album.json&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;      serde_json::from_str::&amp;lt;Vec&amp;lt;serde_json::Value&amp;gt;&amp;gt;(my_str).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Artist&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; =&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; my_str = include_str!(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;..&#x2F;static&#x2F;Artist.json&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;      serde_json::from_str::&amp;lt;Vec&amp;lt;serde_json::Value&amp;gt;&amp;gt;(my_str).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;    _ =&amp;gt; panic!(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;table not found {table_name:?}&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;),
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Let&#x27;s smash that into our &lt;code&gt;run_query&lt;&#x2F;code&gt; function:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;pub fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;run_query&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;query&lt;&#x2F;span&gt;&lt;span&gt;: &amp;amp;Query) -&amp;gt; Vec&amp;lt;serde_json::Value&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;match&lt;&#x2F;span&gt;&lt;span&gt; query {
&lt;&#x2F;span&gt;&lt;span&gt;    Query::From(From { table_name }) =&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;table_scan&lt;&#x2F;span&gt;&lt;span&gt;(table_name), 
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; ..  
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It is not good code, but it is code. We&#x27;d test it, but it would still fail because of the other &lt;code&gt;todo!&lt;&#x2F;code&gt;. Oh well. Onwards.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;query-filter&quot;&gt;Query::Filter&lt;&#x2F;h2&gt;
&lt;p&gt;Call me a staunch traditionalist, but often when I am accessing a database I do not wish to download all of it&#x27;s data at once. We&#x27;re going to allow users to filter data using a &lt;code&gt;where&lt;&#x2F;code&gt; clause, which lets us define properties about rows we are interested in.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s recap on our &lt;code&gt;Expr&lt;&#x2F;code&gt; type:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;pub enum &lt;&#x2F;span&gt;&lt;span&gt;Expr {
&lt;&#x2F;span&gt;&lt;span&gt;  ColumnComparison {
&lt;&#x2F;span&gt;&lt;span&gt;    column: Column,
&lt;&#x2F;span&gt;&lt;span&gt;    op: Op,
&lt;&#x2F;span&gt;&lt;span&gt;    literal: serde_json::Value,
&lt;&#x2F;span&gt;&lt;span&gt;  },
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;pub enum &lt;&#x2F;span&gt;&lt;span&gt;Op {
&lt;&#x2F;span&gt;&lt;span&gt;  Equals,
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is pretty limited, but it does let us express &lt;code&gt;select * from Album where album_id = 1&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;We start by defining a function for deciding whether we care about a row. It takes a row (which we store as a &lt;code&gt;serde_json::Value&lt;&#x2F;code&gt;) and an &lt;code&gt;Expr&lt;&#x2F;code&gt;, returning a &lt;code&gt;bool&lt;&#x2F;code&gt; telling us to keep the row or throw it in the bin.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;apply_predicate&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;row&lt;&#x2F;span&gt;&lt;span&gt;: &amp;amp;serde_json::Value, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;where_expr&lt;&#x2F;span&gt;&lt;span&gt;: &amp;amp;Expr) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;bool &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;match&lt;&#x2F;span&gt;&lt;span&gt; where_expr {
&lt;&#x2F;span&gt;&lt;span&gt;    Expr::ColumnComparison {
&lt;&#x2F;span&gt;&lt;span&gt;      column,
&lt;&#x2F;span&gt;&lt;span&gt;      op,
&lt;&#x2F;span&gt;&lt;span&gt;      literal,
&lt;&#x2F;span&gt;&lt;span&gt;    } =&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; unwrap row into a map
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; row_object = row.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;as_object&lt;&#x2F;span&gt;&lt;span&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; grab the column we care about 
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; value = row_object.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;get&lt;&#x2F;span&gt;&lt;span&gt;(&amp;amp;column.name).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; compare it to `value` 
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;match&lt;&#x2F;span&gt;&lt;span&gt; op {
&lt;&#x2F;span&gt;&lt;span&gt;        Op::Equals =&amp;gt; value == literal,
&lt;&#x2F;span&gt;&lt;span&gt;      }
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Let&#x27;s use it in our &lt;code&gt;run_query&lt;&#x2F;code&gt; function:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;pub fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;run_query&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;query&lt;&#x2F;span&gt;&lt;span&gt;: &amp;amp;Query) -&amp;gt; Vec&amp;lt;serde_json::Value&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;match&lt;&#x2F;span&gt;&lt;span&gt; query {
&lt;&#x2F;span&gt;&lt;span&gt;    Query::From(From { table_name }) =&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;table_scan&lt;&#x2F;span&gt;&lt;span&gt;(table_name),
&lt;&#x2F;span&gt;&lt;span&gt;    Query::Filter(Filter { from, filter }) =&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;run_query&lt;&#x2F;span&gt;&lt;span&gt;(from)
&lt;&#x2F;span&gt;&lt;span&gt;      .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;into_iter&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;      .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;filter&lt;&#x2F;span&gt;&lt;span&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;row&lt;&#x2F;span&gt;&lt;span&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;apply_predicate&lt;&#x2F;span&gt;&lt;span&gt;(row, filter))
&lt;&#x2F;span&gt;&lt;span&gt;      .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;collect&lt;&#x2F;span&gt;&lt;span&gt;(), 
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; ..
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We&#x27;d test a query, but it&#x27;d still fail. But nearly!&lt;&#x2F;p&gt;
&lt;h2 id=&quot;query-project&quot;&gt;Query::Project&lt;&#x2F;h2&gt;
&lt;p&gt;So far we return every single field from our table scan, so every &lt;code&gt;select&lt;&#x2F;code&gt; is a &lt;code&gt;select * from ...&lt;&#x2F;code&gt;. We can do better than that, let&#x27;s implement &lt;code&gt;Project&lt;&#x2F;code&gt;, which is how we extract fields from rows. Eventually, we&#x27;ll allowing renaming things with aliases, but that&#x27;s quite boring and fiddly, so for now we&#x27;re just supporting stuff like &lt;code&gt;select Title, ArtistId from Album&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;project_fields&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;row&lt;&#x2F;span&gt;&lt;span&gt;: serde_json::Value, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;fields&lt;&#x2F;span&gt;&lt;span&gt;: &amp;amp;[Column]) -&amp;gt; serde_json::Value {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; make set of columns to keep
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; field_set: BTreeSet&amp;lt;_&amp;gt; = 
&lt;&#x2F;span&gt;&lt;span&gt;    fields
&lt;&#x2F;span&gt;&lt;span&gt;      .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;iter&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;      .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;c&lt;&#x2F;span&gt;&lt;span&gt;| c.name.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;())
&lt;&#x2F;span&gt;&lt;span&gt;      .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;collect&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if let &lt;&#x2F;span&gt;&lt;span&gt;serde_json::Value::Object(map) = row {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; collect all the items we still want
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; new_map = map
&lt;&#x2F;span&gt;&lt;span&gt;      .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;into_iter&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;      .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;filter&lt;&#x2F;span&gt;&lt;span&gt;(|(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;k&lt;&#x2F;span&gt;&lt;span&gt;, _)| field_set.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;contains&lt;&#x2F;span&gt;&lt;span&gt;(k))
&lt;&#x2F;span&gt;&lt;span&gt;      .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;collect&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; wrap it back up again
&lt;&#x2F;span&gt;&lt;span&gt;    serde_json::Value::Object(new_map)
&lt;&#x2F;span&gt;&lt;span&gt;  } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    panic!(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;expected Object&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Then we add it to &lt;code&gt;run_query&lt;&#x2F;code&gt;, completing it for now.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;pub fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;run_query&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;query&lt;&#x2F;span&gt;&lt;span&gt;: &amp;amp;Query) -&amp;gt; Vec&amp;lt;serde_json::Value&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;match&lt;&#x2F;span&gt;&lt;span&gt; query {
&lt;&#x2F;span&gt;&lt;span&gt;    Query::From(From { table_name }) =&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;table_scan&lt;&#x2F;span&gt;&lt;span&gt;(table_name),
&lt;&#x2F;span&gt;&lt;span&gt;    Query::Filter(Filter { from, filter }) =&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;run_query&lt;&#x2F;span&gt;&lt;span&gt;(from)
&lt;&#x2F;span&gt;&lt;span&gt;      .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;into_iter&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;      .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;filter&lt;&#x2F;span&gt;&lt;span&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;row&lt;&#x2F;span&gt;&lt;span&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;apply_predicate&lt;&#x2F;span&gt;&lt;span&gt;(row, filter))
&lt;&#x2F;span&gt;&lt;span&gt;      .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;collect&lt;&#x2F;span&gt;&lt;span&gt;(),
&lt;&#x2F;span&gt;&lt;span&gt;    Query::Project(Project { from, fields }) =&amp;gt; {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; filter the columns in each row
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;run_query&lt;&#x2F;span&gt;&lt;span&gt;(from)
&lt;&#x2F;span&gt;&lt;span&gt;        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;into_iter&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span&gt;(|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;row&lt;&#x2F;span&gt;&lt;span&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;project_fields&lt;&#x2F;span&gt;&lt;span&gt;(row, fields))
&lt;&#x2F;span&gt;&lt;span&gt;        .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;collect&lt;&#x2F;span&gt;&lt;span&gt;(),
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;bringing-it-together&quot;&gt;Bringing it together&lt;&#x2F;h2&gt;
&lt;p&gt;We then add a basic CLI using &lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;clap&#x2F;latest&#x2F;clap&#x2F;&quot;&gt;clap&lt;&#x2F;a&gt;, that takes a single argument &lt;code&gt;--sql&lt;&#x2F;code&gt;. Nothing surprising or interesting here, sorry.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span&gt;clap::Parser;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span&gt;core::{parse, run_query};
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;derive&lt;&#x2F;span&gt;&lt;span&gt;(Parser, Debug)]
&lt;&#x2F;span&gt;&lt;span&gt;#[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;command&lt;&#x2F;span&gt;&lt;span&gt;(version, about, long_about = None)]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;struct &lt;&#x2F;span&gt;&lt;span&gt;Args {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F;&#x2F; SQL query to run
&lt;&#x2F;span&gt;&lt;span&gt;  #[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;arg&lt;&#x2F;span&gt;&lt;span&gt;(short, long)]
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sql&lt;&#x2F;span&gt;&lt;span&gt;: String,
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; args = Args::parse();
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; query = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;parse&lt;&#x2F;span&gt;&lt;span&gt;(&amp;amp;args.sql).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; results = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;run_query&lt;&#x2F;span&gt;&lt;span&gt;(&amp;amp;query);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; result in results {
&lt;&#x2F;span&gt;&lt;span&gt;    println!(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;{result}&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This means we can run a query with &lt;code&gt;cargo run --bin cli -- --sql &#x27;select Title from Album where AlbumId = 48&#x27; | jq &lt;&#x2F;code&gt; and look what we get:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;AlbumId&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt; 48,
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Title&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;The Essential Miles Davis (Disc 1)&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;ArtistId&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt; 68
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Not bad, not bad at all.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;what-s-next&quot;&gt;What&#x27;s next?&lt;&#x2F;h2&gt;
&lt;p&gt;If you&#x27;ve made it this far without being furious about my use of &lt;code&gt;unwrap()&lt;&#x2F;code&gt;, then in part two we&#x27;re going to add some JOINS.&lt;&#x2F;p&gt;
&lt;p&gt;Make sense? If not, &lt;a href=&quot;&#x2F;contact.html&quot;&gt;get in touch&lt;&#x2F;a&gt;!&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>llvm-calc is dead, long live wasm-calc</title>
          <pubDate>Wed, 03 Jul 2024 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/announcing-wasm-calc/</link>
          <guid>https://danieljharvey.github.io/announcing-wasm-calc/</guid>
          <description xml:base="https://danieljharvey.github.io/announcing-wasm-calc/">&lt;p&gt;So last year I &lt;a href=&quot;&#x2F;tags&#x2F;llvm.html&quot;&gt;started writing a
series&lt;&#x2F;a&gt; on compiling a functional language to
LLVM, and I have to confess, I came &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;danieljharvey&#x2F;llvm-calc&#x2F;pull&#x2F;2&quot;&gt;utterly unstuck&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;failure.webp&quot; alt=&quot;Failure&quot; title=&quot;Failure&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Until recent versions, LLVM tried to be helpful by making you specify types
for all pointers you pass around. This is really helpful in simple cases as you
get basic typechecking at compile time. However, once you start implementing things like sum types (ie, reserve a slab of memory, and stuff it with one of several sets of data), it becomes super annoying.&lt;&#x2F;p&gt;
&lt;p&gt;For example, compiling an &lt;code&gt;Either&amp;lt;Int, Bool&amp;gt;&lt;&#x2F;code&gt; type requires you to constantly cast between &quot;arbitrary array type we&#x27;ve chosen
that takes up the right amount of memory&quot;, &quot;this thing if it turns out
to be &lt;code&gt;Left&amp;lt;Int&amp;gt;&lt;&#x2F;code&gt;&quot; and &quot;this thing if it turns out to be &lt;code&gt;Right&amp;lt;Bool&amp;gt;&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;This has been solved though! More recent versions of LLVM have the concept of an &lt;a href=&quot;https:&#x2F;&#x2F;llvm.org&#x2F;docs&#x2F;OpaquePointers.html&quot;&gt;Opaque
pointer&lt;&#x2F;a&gt;, which allows us to pass
pointers around without caring what type they are, and then think about the
types when it comes to actually interacting with the underlying data. This is
ideal!&lt;&#x2F;p&gt;
&lt;p&gt;However, it&#x27;s only available in LLVM 14 onwards, and getting a working
version-matched set
of LLVM Haskell libraries turns out to be precarious at best. I got &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;danieljharvey&#x2F;llvm-calc&#x2F;tree&#x2F;djh&#x2F;llvm-15&quot;&gt;reasonably
close&lt;&#x2F;a&gt; using
the &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;llvm-hs&#x2F;llvm-hs&#x2F;tree&#x2F;llvm-15&quot;&gt;LLVM-15 branch&lt;&#x2F;a&gt; of &lt;code&gt;llvm-hs-pure&lt;&#x2F;code&gt;, but could not find an equivalent for &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;llvm-hs&#x2F;llvm-hs-pretty&quot;&gt;llvm-hs-pretty&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Basically, it was all getting a bit fiddly and stupid, and writing a tutorial that is either a) already out of date or b) relies on a set of arbitrary commits from a
bunch of forks doesn&#x27;t feel like it will be particularly useful, so I have decided to change tack.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-good-news&quot;&gt;The good news?&lt;&#x2F;h2&gt;
&lt;p&gt;I decided to port the project to compile to WebAssembly instead! Development of
the thing itself has been going really well, see for yourself here: &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;danieljharvey&#x2F;wasm-calc&quot;&gt;wasm-calc&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The language is fully featured enough that we can create a very simple
&lt;a href=&quot;https:&#x2F;&#x2F;wasm4.org&#x2F;&quot;&gt;wasm-4&lt;&#x2F;a&gt;
game with it.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;wasm-4.png&quot; alt=&quot;Some sort of graphics created from our language&quot; title=&quot;Some sort of graphics created from our language&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Some thoughts so far:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;The &lt;a href=&quot;https:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;wasm&quot;&gt;wasm&lt;&#x2F;a&gt; package for Haskell is lovely!&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Not being able to cheat by including a bunch of C in a runtime has forced me to learn how allocators work&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;There&#x27;s a bunch of useful resources, like
&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;EmNudge&#x2F;watlings&quot;&gt;watlings&lt;&#x2F;a&gt; and &lt;a href=&quot;https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;WebAssembly&#x2F;Understanding_the_text_format&quot;&gt;Mozilla&#x27;s
docs&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;what-s-next&quot;&gt;What&#x27;s next?&lt;&#x2F;h2&gt;
&lt;p&gt;As well as serving as a big excuse, this blog is supposed to be a forcing
function to write the tutorials to go with all the &lt;code&gt;wasm-calc&lt;&#x2F;code&gt; versions. I&#x27;ll
start by porting the &lt;code&gt;llvm-calc&lt;&#x2F;code&gt; blogs, and then move onto the new features
we&#x27;ve got.&lt;&#x2F;p&gt;
&lt;p&gt;Make sense? If not, &lt;a href=&quot;&#x2F;contact.html&quot;&gt;get in touch&lt;&#x2F;a&gt;!&lt;&#x2F;p&gt;
&lt;p&gt;Further reading:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;danieljharvey&#x2F;wasm-calc&quot;&gt;wasm-calc&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Compiling a functional language to LLVM, part 3</title>
          <pubDate>Tue, 11 Apr 2023 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/llvm-compiler-part-3/</link>
          <guid>https://danieljharvey.github.io/llvm-compiler-part-3/</guid>
          <description xml:base="https://danieljharvey.github.io/llvm-compiler-part-3/">&lt;p&gt;Welcome to part 3 of this series in compiling functional languages to LLVM. In
&lt;a href=&quot;&#x2F;posts&#x2F;2023-02-23-llvm-compiler-part-2.html&quot;&gt;part 2&lt;&#x2F;a&gt; we added basic control
flow to our langauge with an &lt;code&gt;if &#x2F; then &#x2F; else&lt;&#x2F;code&gt; construct.&lt;&#x2F;p&gt;
&lt;p&gt;Today we&#x27;re going to get one important step towards a functional programming
language by adding functions and variables.&lt;&#x2F;p&gt;
&lt;p&gt;By the end of today we&#x27;ll be able to make small modules such as:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;add&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a:&lt;&#x2F;span&gt;&lt;span&gt; integer, b: integer) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt; + b
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;increment&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a:&lt;&#x2F;span&gt;&lt;span&gt; integer) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt; + 1
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;add&lt;&#x2F;span&gt;&lt;span&gt;(increment(1)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; add(2, 3))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;However, there are quite a few things our functions &lt;em&gt;won&#x27;t&lt;&#x2F;em&gt; do.&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;They can&#x27;t call themselves recursively (ie, &lt;code&gt;function factorial(a: Integer) { if a == 0 then 1 else a * factorial(a - 1) }&lt;&#x2F;code&gt;. This is to simplify the typechecker
implementation for the time being rather than any limitation in LLVM - we&#x27;ll come back round to making this
possible.&lt;&#x2F;li&gt;
&lt;li&gt;We must define all function arguments up front (ie, &lt;code&gt;a: Integer, b: Boolean&lt;&#x2F;code&gt;). It is possible to infer these, but let&#x27;s keep things simple for
now.&lt;&#x2F;li&gt;
&lt;li&gt;Functions can only call functions defined before them. This can be solved by
doing some dependency analysis before typechecking (ie, move things around to
typecheck them in a sensible order). We avoid this for now, but will come
back to it.&lt;&#x2F;li&gt;
&lt;li&gt;Functions can&#x27;t return other functions, they can only be defined at the top
level. This is a limitation of LLVM, however in future chapters we&#x27;ll work around this by
implementing closures.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;ok-lets-get-concrete&quot;&gt;OK, lets get concrete&lt;&#x2F;h2&gt;
&lt;p&gt;We&#x27;re going to need some new datatypes to express all the new things we want to
do.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;function&quot;&gt;Function&lt;&#x2F;h3&gt;
&lt;p&gt;Firstly, we have a &lt;code&gt;Function&lt;&#x2F;code&gt; type.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Function&lt;&#x2F;span&gt;&lt;span&gt; ann = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Function
&lt;&#x2F;span&gt;&lt;span&gt;  { fnAnn :: ann,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;fnArgs &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;ArgumentName&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann&lt;&#x2F;span&gt;&lt;span&gt;)],
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;fnFunctionName &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: FunctionName&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;fnBody &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Expr &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; stock (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Eq&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Ord&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Show&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Functor&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This lets us describe something like:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;add&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a:&lt;&#x2F;span&gt;&lt;span&gt; Integer, b: Integer) { 
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt; + b 
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Note that the function body is a single &lt;code&gt;Expr&lt;&#x2F;code&gt;, and that it can use variables
&lt;code&gt;a&lt;&#x2F;code&gt; and &lt;code&gt;b&lt;&#x2F;code&gt; introduced as function arguments.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;ann&lt;&#x2F;code&gt; type will contain file location information
after parsing, and then will contain the type of the function after
typechecking.&lt;&#x2F;p&gt;
&lt;p&gt;Our function implementation is very limited - we can only
use variables passed into the function as arguments, and must explicitly
annotate each function argument with it&#x27;s type.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;module&quot;&gt;Module&lt;&#x2F;h3&gt;
&lt;p&gt;A &lt;code&gt;Module&lt;&#x2F;code&gt; lets us combine multiple functions and a &lt;code&gt;main&lt;&#x2F;code&gt; expression.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Module&lt;&#x2F;span&gt;&lt;span&gt; ann = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Module
&lt;&#x2F;span&gt;&lt;span&gt;  { mdFunctions :: [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Function&lt;&#x2F;span&gt;&lt;span&gt; ann],
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;mdExpr &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Expr &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; stock (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Eq&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Ord&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Show&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Functor&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This lets us write code like:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;increment&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a:&lt;&#x2F;span&gt;&lt;span&gt; Integer) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt; + 1
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;decrement&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a:&lt;&#x2F;span&gt;&lt;span&gt; Integer) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt; - 1
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;increment&lt;&#x2F;span&gt;&lt;span&gt;(decrement(1)) =&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;1
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As mentioned earlier, functions can only be used in the order
they are defined. Therefore &lt;code&gt;decrement&lt;&#x2F;code&gt; could call &lt;code&gt;increment&lt;&#x2F;code&gt;, but not the
other way round. We can improve this in future with some basic dependency
analysis.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;expr&quot;&gt;Expr&lt;&#x2F;h3&gt;
&lt;p&gt;Our &lt;code&gt;Expr&lt;&#x2F;code&gt; needs a couple of new constructors.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt; ann
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EPrim&lt;&#x2F;span&gt;&lt;span&gt; ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Prim
&lt;&#x2F;span&gt;&lt;span&gt;  | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EInfix&lt;&#x2F;span&gt;&lt;span&gt; ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Op&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt; ann) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt; ann)
&lt;&#x2F;span&gt;&lt;span&gt;  | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EIf&lt;&#x2F;span&gt;&lt;span&gt; ann (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt; ann) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt; ann) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt; ann)
&lt;&#x2F;span&gt;&lt;span&gt;  | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EVar&lt;&#x2F;span&gt;&lt;span&gt; ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Identifier &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- new!
&lt;&#x2F;span&gt;&lt;span&gt;  | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EApply&lt;&#x2F;span&gt;&lt;span&gt; ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;FunctionName&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt; ann] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- new!
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; stock (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Eq&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Ord&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Show&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Functor&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Foldable&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Traversable&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;EVar&lt;&#x2F;code&gt; represents a variable such as &lt;code&gt;a&lt;&#x2F;code&gt;, and &lt;code&gt;EApply&lt;&#x2F;code&gt; represents function application (&lt;code&gt;add(1,2)&lt;&#x2F;code&gt;).&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;horse3.jpg&quot; alt=&quot;Horse break&quot; title=&quot;Horse break&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;typechecker-changes&quot;&gt;Typechecker changes&lt;&#x2F;h2&gt;
&lt;p&gt;After a rather sizable swerve into bidirectional typechecking in the previous
part, we are going to focus less on the changes here.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;adding-state&quot;&gt;Adding state&lt;&#x2F;h3&gt;
&lt;p&gt;The most important part is that it&#x27;s become stateful, as we will be learning
about both functions and variables as we typecheck. We have created a
&lt;code&gt;TypecheckM&lt;&#x2F;code&gt; newtype that we use, that contains both a &lt;code&gt;ReaderT&lt;&#x2F;code&gt; and a
&lt;code&gt;StateT&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;newtype &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TypecheckEnv&lt;&#x2F;span&gt;&lt;span&gt; ann = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TypecheckEnv
&lt;&#x2F;span&gt;&lt;span&gt;  { tceVars :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;HashMap Identifier&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Type&lt;&#x2F;span&gt;&lt;span&gt; ann)
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; stock (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Eq&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Ord&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Show&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;newtype &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TypecheckState&lt;&#x2F;span&gt;&lt;span&gt; ann = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TypecheckState
&lt;&#x2F;span&gt;&lt;span&gt;  {tcsFunctions :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;HashMap FunctionName&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Type&lt;&#x2F;span&gt;&lt;span&gt; ann)}
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; stock (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Eq&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Ord&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Show&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;newtype &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TypecheckM&lt;&#x2F;span&gt;&lt;span&gt; ann a = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TypecheckM
&lt;&#x2F;span&gt;&lt;span&gt;  { getTypecheckM ::
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;ReaderT&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TypecheckEnv&lt;&#x2F;span&gt;&lt;span&gt; ann)
&lt;&#x2F;span&gt;&lt;span&gt;          (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;StateT&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TypecheckState&lt;&#x2F;span&gt;&lt;span&gt; ann) 
&lt;&#x2F;span&gt;&lt;span&gt;            (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Either&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TypeError&lt;&#x2F;span&gt;&lt;span&gt; ann))) a
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving newtype
&lt;&#x2F;span&gt;&lt;span&gt;    ( &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Functor&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Applicative&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Monad&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MonadReader&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TypecheckEnv&lt;&#x2F;span&gt;&lt;span&gt; ann),
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MonadError&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TypeError&lt;&#x2F;span&gt;&lt;span&gt; ann),
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MonadState&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TypecheckState&lt;&#x2F;span&gt;&lt;span&gt; ann)
&lt;&#x2F;span&gt;&lt;span&gt;    )
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The reasons for separate &lt;code&gt;Reader&lt;&#x2F;code&gt; and &lt;code&gt;State&lt;&#x2F;code&gt; are the nature of the state in
them. Variables only live for the life of a function that are defined in, so
they live in the &lt;code&gt;TypecheckEnv&lt;&#x2F;code&gt; used by &lt;code&gt;Reader&lt;&#x2F;code&gt;, and disappear after the
function definition is typechecked (&lt;a href=&quot;https:&#x2F;&#x2F;blog.cofree.coffee&#x2F;2021-08-13-that-one-cool-reader-trick&#x2F;&quot;&gt;more information on this technique
here&lt;&#x2F;a&gt;).&lt;&#x2F;p&gt;
&lt;p&gt;The trick is using the &lt;code&gt;local&lt;&#x2F;code&gt; function from &lt;code&gt;Control.Monad.Reader&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;withFunctionArgs &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Identifier&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann&lt;&#x2F;span&gt;&lt;span&gt;)] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; TypecheckM &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; TypecheckM &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann a
&lt;&#x2F;span&gt;&lt;span&gt;withFunctionArgs args computation =
&lt;&#x2F;span&gt;&lt;span&gt;  local
&lt;&#x2F;span&gt;&lt;span&gt;    ( \tce -&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;        tce
&lt;&#x2F;span&gt;&lt;span&gt;          { tceVars = tceVars tce &amp;lt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;HM&lt;&#x2F;span&gt;&lt;span&gt;.fromList args
&lt;&#x2F;span&gt;&lt;span&gt;          }
&lt;&#x2F;span&gt;&lt;span&gt;    )
&lt;&#x2F;span&gt;&lt;span&gt;    computation
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We pass in some &lt;code&gt;args&lt;&#x2F;code&gt;, which are the function arguments and their types, and
&lt;code&gt;computation&lt;&#x2F;code&gt;, which is whatever typechecking we&#x27;d like do. Then throughout
running &lt;code&gt;computation&lt;&#x2F;code&gt;, we&#x27;ll have extra variables in scope, and then they&#x27;ll
disappear again. This is helpful for typechecking functions, where the vars
only exist inside.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;inference-changes&quot;&gt;Inference changes&lt;&#x2F;h3&gt;
&lt;p&gt;We have two new &lt;code&gt;infer&lt;&#x2F;code&gt; cases, &lt;code&gt;EVar&lt;&#x2F;code&gt; and &lt;code&gt;EApply&lt;&#x2F;code&gt;. &lt;code&gt;EVar&lt;&#x2F;code&gt; is pretty
straightforward:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;infer (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EVar&lt;&#x2F;span&gt;&lt;span&gt; ann var) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  ty &amp;lt;- lookupVar ann var
&lt;&#x2F;span&gt;&lt;span&gt;  pure (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EVar&lt;&#x2F;span&gt;&lt;span&gt; ty var)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We lookup the type for &lt;code&gt;var&lt;&#x2F;code&gt;, and decorate the type with it. If &lt;code&gt;lookupVar&lt;&#x2F;code&gt;
fails, it &quot;throws&quot; a &lt;code&gt;TypeError ann&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- | look up a saved identifier &amp;quot;in the environment&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;lookupVar &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Identifier -&amp;gt; TypecheckM &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;lookupVar ann identifier = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  maybeType &amp;lt;- asks (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;HM&lt;&#x2F;span&gt;&lt;span&gt;.lookup identifier . tceVars)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;case&lt;&#x2F;span&gt;&lt;span&gt; maybeType &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;of
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; found -&amp;gt; pure found
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing &lt;&#x2F;span&gt;&lt;span&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;      allIdentifiers &amp;lt;- asks (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;HM&lt;&#x2F;span&gt;&lt;span&gt;.keysSet . tceVars)
&lt;&#x2F;span&gt;&lt;span&gt;      throwError (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;VarNotFound&lt;&#x2F;span&gt;&lt;span&gt; ann identifier allIdentifiers)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The other new &lt;code&gt;infer&lt;&#x2F;code&gt; case is &lt;code&gt;EApply&lt;&#x2F;code&gt;. This is used to get the type of an
applied function.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;infer (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EApply&lt;&#x2F;span&gt;&lt;span&gt; ann fnName args) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- lookup function by name in State
&lt;&#x2F;span&gt;&lt;span&gt;  fn &amp;lt;- lookupFunction ann fnName
&lt;&#x2F;span&gt;&lt;span&gt;  (ty, elabArgs) &amp;lt;- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;case&lt;&#x2F;span&gt;&lt;span&gt; fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;of
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TFunction&lt;&#x2F;span&gt;&lt;span&gt; _ tArgs tReturn -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- check the arguments length match the function 
&lt;&#x2F;span&gt;&lt;span&gt;      when
&lt;&#x2F;span&gt;&lt;span&gt;        (length args &#x2F;= length tArgs)
&lt;&#x2F;span&gt;&lt;span&gt;        (throwError $ 
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;FunctionArgumentLengthMismatch&lt;&#x2F;span&gt;&lt;span&gt; ann 
&lt;&#x2F;span&gt;&lt;span&gt;              (length tArgs) 
&lt;&#x2F;span&gt;&lt;span&gt;              (length args)
&lt;&#x2F;span&gt;&lt;span&gt;        )
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- check each arg against type
&lt;&#x2F;span&gt;&lt;span&gt;      elabArgs &amp;lt;- zipWithM check tArgs args      
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- return type and elaborated arguments
&lt;&#x2F;span&gt;&lt;span&gt;      pure (tReturn, elabArgs)
&lt;&#x2F;span&gt;&lt;span&gt;    _ -&amp;gt; throwError $ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NonFunctionTypeFound&lt;&#x2F;span&gt;&lt;span&gt; ann fn
&lt;&#x2F;span&gt;&lt;span&gt;  pure (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EApply&lt;&#x2F;span&gt;&lt;span&gt; (ty $&amp;gt; ann) fnName elabArgs)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Note how we use &lt;code&gt;check&lt;&#x2F;code&gt; here to check each argument against the expected type in the
function. This is where the bidirectional type checking approach really starts
to shine, as any problems become immediately apparent.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;lookupFunction&lt;&#x2F;code&gt; helper is very similar to &lt;code&gt;lookupVar&lt;&#x2F;code&gt;, except it looks in
the &lt;code&gt;State&lt;&#x2F;code&gt; instead of &lt;code&gt;Reader&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- | look up a saved identifier &amp;quot;in the environment&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;lookupFunction &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; FunctionName -&amp;gt; TypecheckM &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;lookupFunction ann fnName = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  maybeType &amp;lt;- gets (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;HM&lt;&#x2F;span&gt;&lt;span&gt;.lookup fnName . tcsFunctions)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;case&lt;&#x2F;span&gt;&lt;span&gt; maybeType &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;of
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; found -&amp;gt; pure found
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing &lt;&#x2F;span&gt;&lt;span&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;      allFunctions &amp;lt;- gets (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;HM&lt;&#x2F;span&gt;&lt;span&gt;.keysSet . tcsFunctions)
&lt;&#x2F;span&gt;&lt;span&gt;      throwError (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;FunctionNotFound&lt;&#x2F;span&gt;&lt;span&gt; ann fnName allFunctions)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;These types, and the functions used to store &#x2F; fetch variables and functions
are defined
&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;danieljharvey&#x2F;llvm-calc&#x2F;blob&#x2F;trunk&#x2F;llvm-calc3&#x2F;src&#x2F;Calc&#x2F;Typecheck&#x2F;Types.hs&quot;&gt;here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;elaborating-a-function&quot;&gt;Elaborating a function&lt;&#x2F;h3&gt;
&lt;p&gt;Our functions have types for the arguments, so we push them into the &lt;code&gt;Reader&lt;&#x2F;code&gt;
environment, and then elaborate the expression inside.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;elaborateFunction &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;TypecheckM &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Function&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann&lt;&#x2F;span&gt;&lt;span&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;elaborateFunction (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Function&lt;&#x2F;span&gt;&lt;span&gt; ann args name expr) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- with the `args` added to the Reader, infer the type of `expr`
&lt;&#x2F;span&gt;&lt;span&gt;  exprA &amp;lt;- withFunctionArgs args (infer expr)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- adjust the types of the arguments
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; argsA :: [(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;ArgumentName&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Type&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Type&lt;&#x2F;span&gt;&lt;span&gt; ann))]
&lt;&#x2F;span&gt;&lt;span&gt;      argsA = fmap (second (\ty -&amp;gt; fmap (const ty) ty)) args
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- create type of function
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; tyFn = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TFunction&lt;&#x2F;span&gt;&lt;span&gt; ann (snd &amp;lt;$&amp;gt; args) (getOuterAnnotation exprA)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- wrap it all back up again
&lt;&#x2F;span&gt;&lt;span&gt;  pure (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Function&lt;&#x2F;span&gt;&lt;span&gt; tyFn argsA name exprA)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We&#x27;ve extended the &lt;code&gt;Type&lt;&#x2F;code&gt; datatype to add a &lt;code&gt;TFunction&lt;&#x2F;code&gt; constructor, which
contains the types of all the arguments, and the return type. All functions
will have a &lt;code&gt;TFunction&lt;&#x2F;code&gt; type.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Type&lt;&#x2F;span&gt;&lt;span&gt; ann
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TPrim&lt;&#x2F;span&gt;&lt;span&gt; ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TypePrim
&lt;&#x2F;span&gt;&lt;span&gt;  | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TFunction&lt;&#x2F;span&gt;&lt;span&gt; ann [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Type&lt;&#x2F;span&gt;&lt;span&gt; ann] (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Type&lt;&#x2F;span&gt;&lt;span&gt; ann) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- new!
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; stock (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Eq&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Ord&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Show&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Functor&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;elaborating-a-module&quot;&gt;Elaborating a module&lt;&#x2F;h3&gt;
&lt;p&gt;Elaborating a module involves:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Elaborate each function&lt;&#x2F;li&gt;
&lt;li&gt;Push it&#x27;s type into the &lt;code&gt;State&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Elaborate the &lt;code&gt;main&lt;&#x2F;code&gt; expression&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;elaborateModule &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;forall ann&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Module &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Either&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;TypeError &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Module&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann&lt;&#x2F;span&gt;&lt;span&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;elaborateModule (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Module&lt;&#x2F;span&gt;&lt;span&gt; {mdFunctions, mdExpr}) = runTypecheckM (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TypecheckEnv&lt;&#x2F;span&gt;&lt;span&gt; mempty) $ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- typecheck all functions...
&lt;&#x2F;span&gt;&lt;span&gt;  fns &amp;lt;-
&lt;&#x2F;span&gt;&lt;span&gt;    traverse
&lt;&#x2F;span&gt;&lt;span&gt;      ( \fn -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- typecheck function
&lt;&#x2F;span&gt;&lt;span&gt;          elabFn &amp;lt;- elaborateFunction fn
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- add it to State
&lt;&#x2F;span&gt;&lt;span&gt;          storeFunction (fnFunctionName elabFn) (fnAnn elabFn)
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- return it
&lt;&#x2F;span&gt;&lt;span&gt;          pure elabFn
&lt;&#x2F;span&gt;&lt;span&gt;      )
&lt;&#x2F;span&gt;&lt;span&gt;      mdFunctions
&lt;&#x2F;span&gt;&lt;span&gt;  
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- typecheck `expr`, and wrap everything back together
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Module&lt;&#x2F;span&gt;&lt;span&gt; fns &amp;lt;$&amp;gt; infer mdExpr
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;horse1.jpg&quot; alt=&quot;Horse break&quot; title=&quot;Horse break&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;updating-the-interpreter&quot;&gt;Updating the interpreter&lt;&#x2F;h2&gt;
&lt;p&gt;We won&#x27;t go into the interpreter changes today, they work in the same way as
the typechecker,
storing variables in the &lt;code&gt;Reader&lt;&#x2F;code&gt; env and functions in the &lt;code&gt;State&lt;&#x2F;code&gt;. &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;danieljharvey&#x2F;llvm-calc&#x2F;blob&#x2F;trunk&#x2F;llvm-calc3&#x2F;src&#x2F;Calc&#x2F;Interpreter.hs&quot;&gt;The code
lives
here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;to-the-ir&quot;&gt;To the IR!&lt;&#x2F;h2&gt;
&lt;p&gt;Here is an expression:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;sum&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a:&lt;&#x2F;span&gt;&lt;span&gt; Integer, b: Integer) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt; + b
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sum&lt;&#x2F;span&gt;&lt;span&gt;(20, 22)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Here is the LLVN output for it. Hopefully it&#x27;s not too brutal.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;llvm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-llvm &quot;&gt;&lt;code class=&quot;language-llvm&quot; data-lang=&quot;llvm&quot;&gt;&lt;span&gt;; ModuleID = &amp;#39;example&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;declare external ccc  void @printint(i32)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;define external ccc  i32 @sum(i32  %a_0, i32  %b_0)    {
&lt;&#x2F;span&gt;&lt;span&gt;  %1 = add   i32 %a_0, %b_0
&lt;&#x2F;span&gt;&lt;span&gt;  ret i32 %1
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;define external ccc  i32 @main()    {
&lt;&#x2F;span&gt;&lt;span&gt;  %1 =  call ccc  i32  @sum(i32  20, i32  22)
&lt;&#x2F;span&gt;&lt;span&gt;  call ccc  void  @printint(i32  %1)
&lt;&#x2F;span&gt;&lt;span&gt;  ret i32 0
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;llvm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-llvm &quot;&gt;&lt;code class=&quot;language-llvm&quot; data-lang=&quot;llvm&quot;&gt;&lt;span&gt;; ModuleID = &amp;#39;example&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;A comment, lol.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;pre data-lang=&quot;llvm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-llvm &quot;&gt;&lt;code class=&quot;language-llvm&quot; data-lang=&quot;llvm&quot;&gt;&lt;span&gt;declare external ccc  void @printint(i32)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We define our output function from our standard library.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;pre data-lang=&quot;llvm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-llvm &quot;&gt;&lt;code class=&quot;language-llvm&quot; data-lang=&quot;llvm&quot;&gt;&lt;span&gt;define external ccc  i32 @sum(i32  %a_0, i32  %b_0)    {
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Define the function. The first &lt;code&gt;i32&lt;&#x2F;code&gt; is the return type. &lt;code&gt;%a_0&lt;&#x2F;code&gt; is the first
argument with type &lt;code&gt;i32&lt;&#x2F;code&gt;, and &lt;code&gt;%b_0&lt;&#x2F;code&gt; is the second argument, with type &lt;code&gt;i32&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;pre data-lang=&quot;llvm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-llvm &quot;&gt;&lt;code class=&quot;language-llvm&quot; data-lang=&quot;llvm&quot;&gt;&lt;span&gt;  %1 = add   i32 %a_0, %b_0
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Body of the &lt;code&gt;sum&lt;&#x2F;code&gt; function, add &lt;code&gt;%a_0&lt;&#x2F;code&gt; and &lt;code&gt;%b_0&lt;&#x2F;code&gt; and assign the result to
&lt;code&gt;%1&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;pre data-lang=&quot;llvm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-llvm &quot;&gt;&lt;code class=&quot;language-llvm&quot; data-lang=&quot;llvm&quot;&gt;&lt;span&gt;  ret i32 %1
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Return &lt;code&gt;%1&lt;&#x2F;code&gt; from the function.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;llvm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-llvm &quot;&gt;&lt;code class=&quot;language-llvm&quot; data-lang=&quot;llvm&quot;&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;End of &lt;code&gt;sum&lt;&#x2F;code&gt; function.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;pre data-lang=&quot;llvm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-llvm &quot;&gt;&lt;code class=&quot;language-llvm&quot; data-lang=&quot;llvm&quot;&gt;&lt;span&gt;define external ccc  i32 @main()    {
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This defines the &lt;code&gt;main&lt;&#x2F;code&gt; function, the entry point to our application.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;pre data-lang=&quot;llvm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-llvm &quot;&gt;&lt;code class=&quot;language-llvm&quot; data-lang=&quot;llvm&quot;&gt;&lt;span&gt;%1 =  call ccc  i32  @sum(i32  20, i32  22)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Body of the &lt;code&gt;main&lt;&#x2F;code&gt; function - this calls the &lt;code&gt;sum&lt;&#x2F;code&gt; function, passing it &lt;code&gt;20&lt;&#x2F;code&gt;
amd &lt;code&gt;22&lt;&#x2F;code&gt; as args. The result is stored in &lt;code&gt;%1&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;pre data-lang=&quot;llvm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-llvm &quot;&gt;&lt;code class=&quot;language-llvm&quot; data-lang=&quot;llvm&quot;&gt;&lt;span&gt;call ccc  void  @printint(i32  %1)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Pass the result (&lt;code&gt;%1&lt;&#x2F;code&gt;) to the &lt;code&gt;printint&lt;&#x2F;code&gt; function in our standard library.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;pre data-lang=&quot;llvm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-llvm &quot;&gt;&lt;code class=&quot;language-llvm&quot; data-lang=&quot;llvm&quot;&gt;&lt;span&gt;ret i32 0
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Return the value &lt;code&gt;0&lt;&#x2F;code&gt; to show the program successfully completed.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;pre data-lang=&quot;llvm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-llvm &quot;&gt;&lt;code class=&quot;language-llvm&quot; data-lang=&quot;llvm&quot;&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Nice closing bracket. Time for a rest.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;horse2.jpg&quot; alt=&quot;Horse break&quot; title=&quot;Horse break&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;generating-ir-from-haskell&quot;&gt;Generating IR from Haskell&lt;&#x2F;h2&gt;
&lt;p&gt;Adding functions and variables means that our IR generation also becomes
stateful. We&#x27;re going to define a couple more types:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;OutputState &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;OutputState
&lt;&#x2F;span&gt;&lt;span&gt;  { osFunctions :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Map FunctionName LLVM&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Operand&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;osVars &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Map Identifier LLVM&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Operand
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It also means something can go wrong:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;OutputError
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;CantFindVar Identifier
&lt;&#x2F;span&gt;&lt;span&gt;  | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;CantFindFunction FunctionName
&lt;&#x2F;span&gt;&lt;span&gt;  | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NonFunctionType&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Type ()&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; stock (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Eq&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Ord&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Show&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Both these errors &lt;em&gt;shouldn&#x27;t&lt;&#x2F;em&gt; happen if the typechecking is working, however
it&#x27;s nice to capture them properly rather than just throwing with &lt;code&gt;error&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;It means we also need a similar set of functions for adding and looking up
functions and variables, which can be found
&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;danieljharvey&#x2F;llvm-calc&#x2F;blob&#x2F;trunk&#x2F;llvm-calc3&#x2F;src&#x2F;Calc&#x2F;Compile&#x2F;ToLLVM.hs#L38&quot;&gt;here&lt;&#x2F;a&gt; and are hopefully unsurprising.&lt;&#x2F;p&gt;
&lt;p&gt;A lot of our implementation are various &quot;turn things into LLVM&quot; functions:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;typeToLLVM &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; LLVM&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Type
&lt;&#x2F;span&gt;&lt;span&gt;typeToLLVM (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TPrim&lt;&#x2F;span&gt;&lt;span&gt; _ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TBool&lt;&#x2F;span&gt;&lt;span&gt;) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.i1
&lt;&#x2F;span&gt;&lt;span&gt;typeToLLVM (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TPrim&lt;&#x2F;span&gt;&lt;span&gt; _ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TInt&lt;&#x2F;span&gt;&lt;span&gt;) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.i32
&lt;&#x2F;span&gt;&lt;span&gt;typeToLLVM (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TFunction&lt;&#x2F;span&gt;&lt;span&gt; _ tyArgs tyRet) =
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;FunctionType&lt;&#x2F;span&gt;&lt;span&gt; (typeToLLVM tyRet) (typeToLLVM &amp;lt;$&amp;gt; tyArgs) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;False
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;functionNameToLLVM &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: FunctionName -&amp;gt; LLVM&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Name
&lt;&#x2F;span&gt;&lt;span&gt;functionNameToLLVM (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;FunctionName&lt;&#x2F;span&gt;&lt;span&gt; fnName) =
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Name&lt;&#x2F;span&gt;&lt;span&gt; (fromString (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;.unpack fnName))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;functionArgToLLVM &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::
&lt;&#x2F;span&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;ArgumentName&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Type&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann&lt;&#x2F;span&gt;&lt;span&gt;)) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Type&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;ParameterName&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;functionArgToLLVM (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;ArgumentName&lt;&#x2F;span&gt;&lt;span&gt; argName, ty) =
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; llvmType = typeToLLVM (getOuterTypeAnnotation ty)
&lt;&#x2F;span&gt;&lt;span&gt;      paramName = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;ParameterName&lt;&#x2F;span&gt;&lt;span&gt; (fromString (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;T&lt;&#x2F;span&gt;&lt;span&gt;.unpack argName))
&lt;&#x2F;span&gt;&lt;span&gt;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;in&lt;&#x2F;span&gt;&lt;span&gt; (llvmType, paramName)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now we can create IR for each function in our module. We put each function
variable in &lt;code&gt;State&lt;&#x2F;code&gt; so that we can look them up when generating expressions:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;functionToLLVM &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::
&lt;&#x2F;span&gt;&lt;span&gt;  ( &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MonadModuleBuilder &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;m&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MonadFix &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;m&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MonadState OutputState &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;m&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MonadError OutputError &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;m
&lt;&#x2F;span&gt;&lt;span&gt;  ) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Function&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;m &lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;functionToLLVM (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Function&lt;&#x2F;span&gt;&lt;span&gt; {fnAnn, fnFunctionName, fnBody, fnArgs}) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- get llvm type of function
&lt;&#x2F;span&gt;&lt;span&gt;  retType &amp;lt;- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;case&lt;&#x2F;span&gt;&lt;span&gt; fnAnn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;of
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TFunction&lt;&#x2F;span&gt;&lt;span&gt; _ _ tyRet -&amp;gt; pure $ typeToLLVM tyRet
&lt;&#x2F;span&gt;&lt;span&gt;    _ -&amp;gt; throwError (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NonFunctionType&lt;&#x2F;span&gt;&lt;span&gt; (fnAnn $&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;()&lt;&#x2F;span&gt;&lt;span&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; argTypes = functionArgToLLVM &amp;lt;$&amp;gt; fnArgs
&lt;&#x2F;span&gt;&lt;span&gt;      functionName = functionNameToLLVM fnFunctionName
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- create the LLVM function
&lt;&#x2F;span&gt;&lt;span&gt;  llvmFunction &amp;lt;- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.function functionName argTypes retType $ \args -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- save the args in the environment
&lt;&#x2F;span&gt;&lt;span&gt;    saveArgs
&lt;&#x2F;span&gt;&lt;span&gt;      ( &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;M&lt;&#x2F;span&gt;&lt;span&gt;.fromList $
&lt;&#x2F;span&gt;&lt;span&gt;          zipWith
&lt;&#x2F;span&gt;&lt;span&gt;            ( \(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;ArgumentName&lt;&#x2F;span&gt;&lt;span&gt; argName, _) arg -&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;                (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Identifier&lt;&#x2F;span&gt;&lt;span&gt; argName, arg)
&lt;&#x2F;span&gt;&lt;span&gt;            )
&lt;&#x2F;span&gt;&lt;span&gt;            fnArgs
&lt;&#x2F;span&gt;&lt;span&gt;            args
&lt;&#x2F;span&gt;&lt;span&gt;      )
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- build the LLVM AST for our expression
&lt;&#x2F;span&gt;&lt;span&gt;    ourExpression &amp;lt;- exprToLLVM fnBody
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- return result
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.ret ourExpression
&lt;&#x2F;span&gt;&lt;span&gt;  
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- save reference to this function in our State to lookup in other
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- expressions
&lt;&#x2F;span&gt;&lt;span&gt;  saveFunction fnFunctionName llvmFunction
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We have two new cases in &lt;code&gt;exprToLLVM&lt;&#x2F;code&gt;, for &lt;code&gt;EVar&lt;&#x2F;code&gt; and &lt;code&gt;EApply&lt;&#x2F;code&gt;. These somewhat
echo the implementation in the typechecker above. For &lt;code&gt;EVar&lt;&#x2F;code&gt;, we lookup the
LLVM IR in our &lt;code&gt;State&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;exprToLLVM (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EVar&lt;&#x2F;span&gt;&lt;span&gt; _ var) =
&lt;&#x2F;span&gt;&lt;span&gt;  lookupArg var
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And for &lt;code&gt;EApply&lt;&#x2F;code&gt;, we lookup the function in &lt;code&gt;State&lt;&#x2F;code&gt;, then use LLVM&#x27;s &lt;code&gt;call&lt;&#x2F;code&gt; to
pass all the arguments to it.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;exprToLLVM (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EApply&lt;&#x2F;span&gt;&lt;span&gt; _ fnName args) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  irFunc &amp;lt;- lookupFunction fnName
&lt;&#x2F;span&gt;&lt;span&gt;  irArgs &amp;lt;- traverse exprToLLVM args
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.call irFunc ((,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;[]&lt;&#x2F;span&gt;&lt;span&gt;) &amp;lt;$&amp;gt; irArgs)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Finally, we bring it all together in the top level &lt;code&gt;moduleToLLVM&lt;&#x2F;code&gt; function,
which takes our typechecked &lt;code&gt;Module&lt;&#x2F;code&gt; and creates an LLVM module.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- | given our `Module` type, turn it into an LLVM module
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;moduleToLLVM &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Module&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Either OutputError LLVM&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Module
&lt;&#x2F;span&gt;&lt;span&gt;moduleToLLVM (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Module&lt;&#x2F;span&gt;&lt;span&gt; {mdExpr = expr, mdFunctions}) =
&lt;&#x2F;span&gt;&lt;span&gt;  flip evalStateT (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;OutputState&lt;&#x2F;span&gt;&lt;span&gt; mempty mempty) $ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.buildModuleT &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;example&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; $ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- get the printing function for our `expr`&amp;#39;s return type
&lt;&#x2F;span&gt;&lt;span&gt;    printFn &amp;lt;- printFunction (getOuterAnnotation expr)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- create all our functions
&lt;&#x2F;span&gt;&lt;span&gt;    traverse_ functionToLLVM mdFunctions
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- create a function called `main` that will be the entry point to our
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- program
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.function &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;[] LLVM&lt;&#x2F;span&gt;&lt;span&gt;.i32 $ \_ -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- build the LLVM AST for our expression
&lt;&#x2F;span&gt;&lt;span&gt;      ourExpression &amp;lt;- exprToLLVM expr
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- print our result to stdout
&lt;&#x2F;span&gt;&lt;span&gt;      _ &amp;lt;- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.call printFn [(ourExpression, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;[]&lt;&#x2F;span&gt;&lt;span&gt;)]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- return success exit code of `0`
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.ret (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.int32 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You can see all of the LLVM generation code
&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;danieljharvey&#x2F;llvm-calc&#x2F;blob&#x2F;trunk&#x2F;llvm-calc3&#x2F;src&#x2F;Calc&#x2F;Compile&#x2F;ToLLVM.hs&quot;&gt;here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h3 id=&quot;well-well-well-if-it-s-not-the-end-of-the-article&quot;&gt;Well, well, well, if it&#x27;s not the end of the article&lt;&#x2F;h3&gt;
&lt;p&gt;Congratulations, you are all functional programming language implementation experts now. Hopefully you found that somewhat interesting and&#x2F;or useful.
Next time we&#x27;ll be adding basic product types and pattern matching. Great!&lt;&#x2F;p&gt;
&lt;p&gt;Make sense? If not, &lt;a href=&quot;&#x2F;contact.html&quot;&gt;get in touch&lt;&#x2F;a&gt;!&lt;&#x2F;p&gt;
&lt;p&gt;Further reading:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;llvm.org&#x2F;docs&#x2F;LangRef.html&quot;&gt;llvm reference&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;danieljharvey&#x2F;llvm-calc&quot;&gt;llvm-calc&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Compiling a functional language to LLVM, part 2</title>
          <pubDate>Thu, 23 Feb 2023 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/llvm-compiler-part-2/</link>
          <guid>https://danieljharvey.github.io/llvm-compiler-part-2/</guid>
          <description xml:base="https://danieljharvey.github.io/llvm-compiler-part-2/">&lt;p&gt;Welcome to part 2 of this series in compiling functional languages to LLVM. In
&lt;a href=&quot;&#x2F;posts&#x2F;2023-02-08-llvm-compiler-part-1.html&quot;&gt;part 1&lt;&#x2F;a&gt; we created a very simple
calculator that let us add, subtract and multiply integers like &lt;code&gt;1 + 1&lt;&#x2F;code&gt; or &lt;code&gt;6 * (5 - 2)&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Today we&#x27;re going to spice things up a touch by adding some basic control flow.
By the end of today we&#x27;re going to be writing sweet syntax such as:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt; + 2 == 5
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;6&lt;&#x2F;span&gt;&lt;span&gt; == 6 then False else True
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;what-s-new-then&quot;&gt;What&#x27;s new then?&lt;&#x2F;h2&gt;
&lt;p&gt;To make our dreams come true, we&#x27;re going to two new
syntactic features:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;if&lt;&#x2F;code&gt; expressions&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;an &lt;code&gt;==&lt;&#x2F;code&gt; infix operator&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Let&#x27;s do that now!&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- | operators for combining expressions
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Op
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;OpAdd
&lt;&#x2F;span&gt;&lt;span&gt;  | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;OpMultiply
&lt;&#x2F;span&gt;&lt;span&gt;  | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;OpSubtract
&lt;&#x2F;span&gt;&lt;span&gt;  | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;OpEquals &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- this is new! 
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; stock (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Eq&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Ord&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Show&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- | Expressions, decorated with some unknown `ann`
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt; ann
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EPrim&lt;&#x2F;span&gt;&lt;span&gt; ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Prim &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- this `Prim` used to always be `Int`
&lt;&#x2F;span&gt;&lt;span&gt;  | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EInfix&lt;&#x2F;span&gt;&lt;span&gt; ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Op&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt; ann) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt; ann)
&lt;&#x2F;span&gt;&lt;span&gt;  | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EIf&lt;&#x2F;span&gt;&lt;span&gt; ann (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt; ann) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt; ann) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt; ann) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- this is new!
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; stock (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Eq&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Ord&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Show&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Functor&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Foldable&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Traversable&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Our &lt;code&gt;EIf&lt;&#x2F;code&gt; constructor takes three &lt;code&gt;Expr ann&lt;&#x2F;code&gt; as arguments. The first is the
&lt;code&gt;predicate&lt;&#x2F;code&gt;, ie, the thing that must evaluate to &lt;code&gt;True&lt;&#x2F;code&gt; or &lt;code&gt;False&lt;&#x2F;code&gt;, and the
other two are expressions to be evaluated on the &lt;code&gt;then&lt;&#x2F;code&gt; and &lt;code&gt;else&lt;&#x2F;code&gt; branches.
For all of this to make any sense, these will need to be the same type.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;types&quot;&gt;Types?&lt;&#x2F;h2&gt;
&lt;p&gt;Previously we sort of glossed over the idea of types, because every value in
our calculator was either an &lt;code&gt;Integer&lt;&#x2F;code&gt; (ie, &lt;code&gt;1&lt;&#x2F;code&gt;, &lt;code&gt;42&lt;&#x2F;code&gt;) or an expression that would eventually
evaluate into an &lt;code&gt;Integer&lt;&#x2F;code&gt; (like &lt;code&gt;1 + 1&lt;&#x2F;code&gt;, &lt;code&gt;6 * 12&lt;&#x2F;code&gt;).&lt;&#x2F;p&gt;
&lt;p&gt;However, the expression &lt;code&gt;1 == 1&lt;&#x2F;code&gt; doesn&#x27;t resolve to an &lt;code&gt;Integer&lt;&#x2F;code&gt;, it can only
be &lt;code&gt;True&lt;&#x2F;code&gt; or &lt;code&gt;False&lt;&#x2F;code&gt;, ie a &lt;code&gt;Boolean&lt;&#x2F;code&gt; type. (it is true that we could express
this with an &lt;code&gt;Integer&lt;&#x2F;code&gt; but if we start cutting corners this early in the game
we&#x27;ll never get anywhere). This means we&#x27;ll need to extend our &lt;code&gt;Prim&lt;&#x2F;code&gt; type to also describe &lt;code&gt;Boolean&lt;&#x2F;code&gt; values as
well as &lt;code&gt;Integer&lt;&#x2F;code&gt;s.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- | types of basic values
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Prim
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;PInt Integer
&lt;&#x2F;span&gt;&lt;span&gt;  | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;PBool Bool
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; stock (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Eq&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Ord&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Show&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;However this means we are in danger of our users being able to make silly mistakes like &lt;code&gt;if 27 then False else 6&lt;&#x2F;code&gt;? How can we stop this? This can only mean one thing: we are going to need
to write a bidirectional type checker.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;bidirect-what&quot;&gt;Bidirect what?&lt;&#x2F;h3&gt;
&lt;p&gt;A bidirectional type checker is a way of working which types parts of an
expression has, and identifying parts that don&#x27;t make sense. What makes it
&quot;bidirectional&quot; is that it works in two &quot;modes&quot;:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;infer&lt;&#x2F;code&gt; mode: given an expression, give me the type&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;check&lt;&#x2F;code&gt; mode: given an expression and the type we think it has, give me the
type&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;The broad idea is when we don&#x27;t know anything about an expression, we &lt;code&gt;infer&lt;&#x2F;code&gt;
what types it has, but as we learn more, we use that information to help us
work the rest out. Although we could arguably get away with just an &lt;code&gt;infer&lt;&#x2F;code&gt; mode for a language
this simple, we will need this special two-way magic in future.&lt;&#x2F;p&gt;
&lt;p&gt;Enough waffle, let&#x27;s see some code, and then talk about it.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;code&quot;&gt;Code&lt;&#x2F;h3&gt;
&lt;p&gt;Firstly, we need a type for types. We&#x27;ll call it &lt;code&gt;Type&lt;&#x2F;code&gt;. We are adding an &lt;code&gt;ann&lt;&#x2F;code&gt;
type argument to it, so that we can attach source code locations etc. This will
be helpful for showing our user helpful errors, which we will be doing today:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TypePrim &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TBool &lt;&#x2F;span&gt;&lt;span&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TInt
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; stock (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Eq&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Ord&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Show&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- the `ann` is used to attach source code location etc
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Type&lt;&#x2F;span&gt;&lt;span&gt; ann
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TPrim&lt;&#x2F;span&gt;&lt;span&gt; ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TypePrim
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; stock (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Eq&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Ord&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Show&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Functor&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;things-go-wrong&quot;&gt;Things go wrong&lt;&#x2F;h3&gt;
&lt;p&gt;Any old typechecker can tell you when things are going well, but the ones that
are really worth their salt are the ones that tell you helpfully what is going
wrong. We will need these ones:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TypeError&lt;&#x2F;span&gt;&lt;span&gt; ann
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;PredicateIsNotBoolean&lt;&#x2F;span&gt;&lt;span&gt; ann (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Type&lt;&#x2F;span&gt;&lt;span&gt; ann)
&lt;&#x2F;span&gt;&lt;span&gt;  | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;InfixTypeMismatch Op&lt;&#x2F;span&gt;&lt;span&gt; [(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Type&lt;&#x2F;span&gt;&lt;span&gt; ann, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Type&lt;&#x2F;span&gt;&lt;span&gt; ann)]
&lt;&#x2F;span&gt;&lt;span&gt;  | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TypeMismatch&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Type&lt;&#x2F;span&gt;&lt;span&gt; ann) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Type&lt;&#x2F;span&gt;&lt;span&gt; ann)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; stock (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Eq&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Ord&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Show&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;the-meat-of-the-thing&quot;&gt;The meat of the thing&lt;&#x2F;h3&gt;
&lt;p&gt;Therefore, the point of the typechecker is to take &lt;code&gt;Expr ann&lt;&#x2F;code&gt; (ie, an &lt;code&gt;Expr&lt;&#x2F;code&gt;
carrying around some &lt;code&gt;ann&lt;&#x2F;code&gt; that does not concern us), and turn it into either
&lt;code&gt;TypeError ann&lt;&#x2F;code&gt; or &lt;code&gt;Expr (Type ann)&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;Expr (Type ann)&lt;&#x2F;code&gt; means that we&#x27;ll have &quot;decorated&quot; each part of the &lt;code&gt;Expr&lt;&#x2F;code&gt;
with it&#x27;s type. We&#x27;ll take whatever &lt;code&gt;ann&lt;&#x2F;code&gt; was in there and put it in the &lt;code&gt;Type&lt;&#x2F;code&gt;
instead. For example:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;oneWhichIsAnInteger &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Expr&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Type &lt;&#x2F;span&gt;&lt;span&gt;())
&lt;&#x2F;span&gt;&lt;span&gt;oneWhichIsAnInteger = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EPrim&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TPrim () TInt&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;PInt 1&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The entire typechecker lives
&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;danieljharvey&#x2F;llvm-calc&#x2F;blob&#x2F;trunk&#x2F;llvm-calc2&#x2F;src&#x2F;Calc&#x2F;Typecheck&#x2F;Elaborate.hs&quot;&gt;here&lt;&#x2F;a&gt;.
Let&#x27;s go through the key parts:&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- | this is the function we run
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;elaborate &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Expr &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Either&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;TypeError &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann&lt;&#x2F;span&gt;&lt;span&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;elaborate = infer &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- start with `infer` because we know nothing
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;elaborate&lt;&#x2F;code&gt; is the function the typechecker exports. It takes an untypechecked
&lt;code&gt;Expr ann&lt;&#x2F;code&gt; and returns either &lt;code&gt;Expr (Type ann)&lt;&#x2F;code&gt; or an excuse. It starts by
running &lt;code&gt;infer&lt;&#x2F;code&gt;, which we&#x27;ll see shortly.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;typeFromPrim &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Prim -&amp;gt; Type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann
&lt;&#x2F;span&gt;&lt;span&gt;typeFromPrim ann (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;PInt&lt;&#x2F;span&gt;&lt;span&gt; _) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TPrim&lt;&#x2F;span&gt;&lt;span&gt; ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TInt &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- It&amp;#39;s an Integer!
&lt;&#x2F;span&gt;&lt;span&gt;typeFromPrim ann (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;PBool&lt;&#x2F;span&gt;&lt;span&gt; _) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TPrim&lt;&#x2F;span&gt;&lt;span&gt; ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TBool &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- It&amp;#39;s a Boolean!
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The most basic type inference we can do is looking at a primitive value. As it
stands in our language, there is one number type and one boolean type, so we
can unambiguously work out the type just by looking at the value.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;inferIf &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Expr &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Expr &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Expr &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Either&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;TypeError &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann&lt;&#x2F;span&gt;&lt;span&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;inferIf ann predExpr thenExpr elseExpr = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  predA &amp;lt;- infer predExpr
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;case&lt;&#x2F;span&gt;&lt;span&gt; getOuterAnnotation predA &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;of
&lt;&#x2F;span&gt;&lt;span&gt;    (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TPrim&lt;&#x2F;span&gt;&lt;span&gt; _ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TBool&lt;&#x2F;span&gt;&lt;span&gt;) -&amp;gt; pure &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;    otherType -&amp;gt; throwError (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;PredicateIsNotBoolean&lt;&#x2F;span&gt;&lt;span&gt; ann otherType)
&lt;&#x2F;span&gt;&lt;span&gt;  thenA &amp;lt;- infer thenExpr
&lt;&#x2F;span&gt;&lt;span&gt;  elseA &amp;lt;- check (getOuterAnnotation thenA) elseExpr
&lt;&#x2F;span&gt;&lt;span&gt;  pure (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EIf&lt;&#x2F;span&gt;&lt;span&gt; (getOuterAnnotation elseA) predA thenA elseA)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is how &lt;code&gt;if&lt;&#x2F;code&gt; works. We &lt;code&gt;infer&lt;&#x2F;code&gt; the type of the predicate, then use
&lt;code&gt;getOuterAnnotation&lt;&#x2F;code&gt; to get the &lt;code&gt;Type ann&lt;&#x2F;code&gt; out of it. We then case match on it
to see if it&#x27;s a &lt;code&gt;Boolean&lt;&#x2F;code&gt; or not, &quot;throwing&quot; an error if not.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;inferInfix &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Op -&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Expr &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Expr &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Either&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;TypeError &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann&lt;&#x2F;span&gt;&lt;span&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;inferInfix ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;OpEquals&lt;&#x2F;span&gt;&lt;span&gt; a b = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  elabA &amp;lt;- infer a
&lt;&#x2F;span&gt;&lt;span&gt;  elabB &amp;lt;- infer b
&lt;&#x2F;span&gt;&lt;span&gt;  ty &amp;lt;- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;case&lt;&#x2F;span&gt;&lt;span&gt; (getOuterAnnotation elabA, getOuterAnnotation elabB) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;of
&lt;&#x2F;span&gt;&lt;span&gt;    (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TPrim&lt;&#x2F;span&gt;&lt;span&gt; _ tA, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TPrim&lt;&#x2F;span&gt;&lt;span&gt; _ tB)
&lt;&#x2F;span&gt;&lt;span&gt;      | tA == tB -&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- if the types are the same, then great! it&amp;#39;s a bool!
&lt;&#x2F;span&gt;&lt;span&gt;          pure (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TPrim&lt;&#x2F;span&gt;&lt;span&gt; ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TBool&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    (otherA, otherB) -&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- otherwise, error!
&lt;&#x2F;span&gt;&lt;span&gt;      throwError (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TypeMismatch&lt;&#x2F;span&gt;&lt;span&gt; otherA otherB)
&lt;&#x2F;span&gt;&lt;span&gt;  pure (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EInfix&lt;&#x2F;span&gt;&lt;span&gt; ty &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;OpEquals&lt;&#x2F;span&gt;&lt;span&gt; elabA elabB)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;When typechecking &lt;code&gt;==&lt;&#x2F;code&gt;, we want to make sure both sides have the same type,
&quot;throwing&quot; an error if not.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;inferInfix ann op a b = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  elabA &amp;lt;- infer a
&lt;&#x2F;span&gt;&lt;span&gt;  elabB &amp;lt;- infer b
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- all the other infix operators need to be Int -&amp;gt; Int -&amp;gt; Int
&lt;&#x2F;span&gt;&lt;span&gt;  ty &amp;lt;- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;case&lt;&#x2F;span&gt;&lt;span&gt; (getOuterAnnotation elabA, getOuterAnnotation elabB) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;of
&lt;&#x2F;span&gt;&lt;span&gt;    (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TPrim&lt;&#x2F;span&gt;&lt;span&gt; _ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TInt&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TPrim&lt;&#x2F;span&gt;&lt;span&gt; _ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TInt&lt;&#x2F;span&gt;&lt;span&gt;) -&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- if the types are the same, then great! it&amp;#39;s an int!
&lt;&#x2F;span&gt;&lt;span&gt;      pure (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TPrim&lt;&#x2F;span&gt;&lt;span&gt; ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TInt&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TPrim&lt;&#x2F;span&gt;&lt;span&gt; _ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TInt&lt;&#x2F;span&gt;&lt;span&gt;, other) -&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      throwError
&lt;&#x2F;span&gt;&lt;span&gt;        ( &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;InfixTypeMismatch
&lt;&#x2F;span&gt;&lt;span&gt;            op
&lt;&#x2F;span&gt;&lt;span&gt;            [ ( &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TPrim&lt;&#x2F;span&gt;&lt;span&gt; (getOuterTypeAnnotation other) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TInt&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                other
&lt;&#x2F;span&gt;&lt;span&gt;              )
&lt;&#x2F;span&gt;&lt;span&gt;            ]
&lt;&#x2F;span&gt;&lt;span&gt;        )
&lt;&#x2F;span&gt;&lt;span&gt;    (other, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TPrim&lt;&#x2F;span&gt;&lt;span&gt; _ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TInt&lt;&#x2F;span&gt;&lt;span&gt;) -&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      throwError
&lt;&#x2F;span&gt;&lt;span&gt;        ( &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;InfixTypeMismatch
&lt;&#x2F;span&gt;&lt;span&gt;            op
&lt;&#x2F;span&gt;&lt;span&gt;            [ ( &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TPrim&lt;&#x2F;span&gt;&lt;span&gt; (getOuterTypeAnnotation other) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TInt&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                other
&lt;&#x2F;span&gt;&lt;span&gt;              )
&lt;&#x2F;span&gt;&lt;span&gt;            ]
&lt;&#x2F;span&gt;&lt;span&gt;        )
&lt;&#x2F;span&gt;&lt;span&gt;    (otherA, otherB) -&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- otherwise, error!
&lt;&#x2F;span&gt;&lt;span&gt;      throwError
&lt;&#x2F;span&gt;&lt;span&gt;        ( &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;InfixTypeMismatch
&lt;&#x2F;span&gt;&lt;span&gt;            op
&lt;&#x2F;span&gt;&lt;span&gt;            [ (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TPrim&lt;&#x2F;span&gt;&lt;span&gt; (getOuterTypeAnnotation otherA) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TInt&lt;&#x2F;span&gt;&lt;span&gt;, otherA),
&lt;&#x2F;span&gt;&lt;span&gt;              (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TPrim&lt;&#x2F;span&gt;&lt;span&gt; (getOuterTypeAnnotation otherB) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TInt&lt;&#x2F;span&gt;&lt;span&gt;, otherB)
&lt;&#x2F;span&gt;&lt;span&gt;            ]
&lt;&#x2F;span&gt;&lt;span&gt;        )
&lt;&#x2F;span&gt;&lt;span&gt;  pure (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EInfix&lt;&#x2F;span&gt;&lt;span&gt; ty op elabA elabB)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Here are the other operators. Both the arguments should be &lt;code&gt;Integer&lt;&#x2F;code&gt; and the
return type is &lt;code&gt;Integer&lt;&#x2F;code&gt;, otherwise we construct and return an error type. It
seems like a lot of work to be so specific, but look how helpful our errors
are!&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;llvm-2-type-error-1.png&quot; alt=&quot;Nice!&quot; title=&quot;Nice!&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;llvm-2-type-error-2.png&quot; alt=&quot;Great!&quot; title=&quot;Great!&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;llvm-2-type-error-3.png&quot; alt=&quot;Tremendous!&quot; title=&quot;Tremendous!&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;llvm-2-type-error-4.png&quot; alt=&quot;Whoa!&quot; title=&quot;Whoa!&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;llvm-2-type-error-5.png&quot; alt=&quot;What?&quot; title=&quot;What?&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;llvm-2-type-error-6.png&quot; alt=&quot;Absolutely!&quot; title=&quot;Absolutely!&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;llvm-2-type-error-7.png&quot; alt=&quot;Surely not?&quot; title=&quot;Surely not?&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;llvm-2-type-error-8.png&quot; alt=&quot;Ridiculous!&quot; title=&quot;Ridiculous!&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;llvm-2-type-error-9.png&quot; alt=&quot;Huh?&quot; title=&quot;Huh?&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;You can see all the error rendering code
&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;danieljharvey&#x2F;llvm-calc&#x2F;blob&#x2F;trunk&#x2F;llvm-calc2&#x2F;src&#x2F;Calc&#x2F;Typecheck&#x2F;Error.hs&quot;&gt;here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;infer &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Expr &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Either&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;TypeError &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann&lt;&#x2F;span&gt;&lt;span&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;infer (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EPrim&lt;&#x2F;span&gt;&lt;span&gt; ann prim) =
&lt;&#x2F;span&gt;&lt;span&gt;  pure (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EPrim&lt;&#x2F;span&gt;&lt;span&gt; (typeFromPrim ann prim) prim)
&lt;&#x2F;span&gt;&lt;span&gt;infer (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EIf&lt;&#x2F;span&gt;&lt;span&gt; ann predExpr thenExpr elseExpr) =
&lt;&#x2F;span&gt;&lt;span&gt;  inferIf ann predExpr thenExpr elseExpr
&lt;&#x2F;span&gt;&lt;span&gt;infer (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EInfix&lt;&#x2F;span&gt;&lt;span&gt; ann op a b) =
&lt;&#x2F;span&gt;&lt;span&gt;  inferInfix ann op a b
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That&#x27;s how we put &lt;code&gt;infer&lt;&#x2F;code&gt; together, easy!&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;check &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Expr &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Either&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;TypeError &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann&lt;&#x2F;span&gt;&lt;span&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;check ty expr = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  exprA &amp;lt;- infer expr
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; void (getOuterAnnotation exprA) == void ty
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;then&lt;&#x2F;span&gt;&lt;span&gt; pure (expr $&amp;gt; ty)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else&lt;&#x2F;span&gt;&lt;span&gt; throwError (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TypeMismatch&lt;&#x2F;span&gt;&lt;span&gt; ty (getOuterAnnotation exprA))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Lastly, here&#x27;s &lt;code&gt;check&lt;&#x2F;code&gt;. We only use it when comparing arms of &lt;code&gt;if&lt;&#x2F;code&gt; statements,
but soon this will become more interesting.&lt;&#x2F;p&gt;
&lt;p&gt;You can see all of the typechecker code
&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;danieljharvey&#x2F;llvm-calc&#x2F;blob&#x2F;trunk&#x2F;llvm-calc2&#x2F;src&#x2F;Calc&#x2F;Typecheck&#x2F;Elaborate.hs&quot;&gt;here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h2 id=&quot;interpreting-our-new-friends&quot;&gt;Interpreting our new friends&lt;&#x2F;h2&gt;
&lt;p&gt;Before heading back into LLVM land, let&#x27;s update our manual interpreter so we
can understand what&#x27;s needed here.&lt;&#x2F;p&gt;
&lt;p&gt;Firstly, it&#x27;s now possible that our interpreter can fail. This will only happen
if our typechecker is not working as expected, but we should make a proper
error type for it anyway because we are good programmers who care about our
users.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;InterpreterError&lt;&#x2F;span&gt;&lt;span&gt; ann
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NonBooleanPredicate&lt;&#x2F;span&gt;&lt;span&gt; ann (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt; ann)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; stock (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Eq&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Ord&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Show&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Interpreting infix expressions is a little bit more complicated, as our pattern
matches have to make sure we&#x27;re looking at the right &lt;code&gt;Prim&lt;&#x2F;code&gt; values. The
eagle-eyed may notice that a broken typechecker could send this into a loop.
Can you see where?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;interpretInfix &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::
&lt;&#x2F;span&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MonadError&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;InterpreterError &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;m&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Op -&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Expr &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Expr &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;m&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Expr &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;interpretInfix ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;OpAdd&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EPrim&lt;&#x2F;span&gt;&lt;span&gt; _ (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;PInt&lt;&#x2F;span&gt;&lt;span&gt; a)) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EPrim&lt;&#x2F;span&gt;&lt;span&gt; _ (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;PInt&lt;&#x2F;span&gt;&lt;span&gt; b)) =
&lt;&#x2F;span&gt;&lt;span&gt;  pure $ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EPrim&lt;&#x2F;span&gt;&lt;span&gt; ann (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;PInt &lt;&#x2F;span&gt;&lt;span&gt;$ a + b)
&lt;&#x2F;span&gt;&lt;span&gt;interpretInfix ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;OpSubtract&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EPrim&lt;&#x2F;span&gt;&lt;span&gt; _ (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;PInt&lt;&#x2F;span&gt;&lt;span&gt; a)) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EPrim&lt;&#x2F;span&gt;&lt;span&gt; _ (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;PInt&lt;&#x2F;span&gt;&lt;span&gt; b)) =
&lt;&#x2F;span&gt;&lt;span&gt;  pure $ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EPrim&lt;&#x2F;span&gt;&lt;span&gt; ann (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;PInt &lt;&#x2F;span&gt;&lt;span&gt;$ a - b)
&lt;&#x2F;span&gt;&lt;span&gt;interpretInfix ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;OpMultiply&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EPrim&lt;&#x2F;span&gt;&lt;span&gt; _ (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;PInt&lt;&#x2F;span&gt;&lt;span&gt; a)) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EPrim&lt;&#x2F;span&gt;&lt;span&gt; _ (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;PInt&lt;&#x2F;span&gt;&lt;span&gt; b)) =
&lt;&#x2F;span&gt;&lt;span&gt;  pure $ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EPrim&lt;&#x2F;span&gt;&lt;span&gt; ann (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;PInt &lt;&#x2F;span&gt;&lt;span&gt;$ a * b)
&lt;&#x2F;span&gt;&lt;span&gt;interpretInfix ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;OpEquals&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EPrim&lt;&#x2F;span&gt;&lt;span&gt; _ a) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EPrim&lt;&#x2F;span&gt;&lt;span&gt; _ b) =
&lt;&#x2F;span&gt;&lt;span&gt;  pure $ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EPrim&lt;&#x2F;span&gt;&lt;span&gt; ann (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;PBool &lt;&#x2F;span&gt;&lt;span&gt;$ a == b)
&lt;&#x2F;span&gt;&lt;span&gt;interpretInfix ann op a b = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  iA &amp;lt;- interpret a
&lt;&#x2F;span&gt;&lt;span&gt;  iB &amp;lt;- interpret b
&lt;&#x2F;span&gt;&lt;span&gt;  interpretInfix ann op iA iB
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We ended up with a &lt;code&gt;MonadError&lt;&#x2F;code&gt; constraint above - why&#x27;s that? It&#x27;s because the
main &lt;code&gt;interpret&lt;&#x2F;code&gt; function can now &quot;explode&quot; if we try and match a non-predicate
in an if statement. Our typechecker &lt;em&gt;should&lt;&#x2F;em&gt; stop this happening of course.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- | just keep reducing the thing until the smallest thing
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;interpret &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::
&lt;&#x2F;span&gt;&lt;span&gt;  ( &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MonadError&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;InterpreterError &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;m
&lt;&#x2F;span&gt;&lt;span&gt;  ) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Expr &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;m&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Expr &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;interpret (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EPrim&lt;&#x2F;span&gt;&lt;span&gt; ann p) = pure (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EPrim&lt;&#x2F;span&gt;&lt;span&gt; ann p)
&lt;&#x2F;span&gt;&lt;span&gt;interpret (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EInfix&lt;&#x2F;span&gt;&lt;span&gt; ann op a b) =
&lt;&#x2F;span&gt;&lt;span&gt;  interpretInfix ann op a b
&lt;&#x2F;span&gt;&lt;span&gt;interpret (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EIf&lt;&#x2F;span&gt;&lt;span&gt; ann predExpr thenExpr elseExpr) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  predA &amp;lt;- interpret predExpr
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;case&lt;&#x2F;span&gt;&lt;span&gt; predA &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;of
&lt;&#x2F;span&gt;&lt;span&gt;    (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EPrim&lt;&#x2F;span&gt;&lt;span&gt; _ (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;PBool True&lt;&#x2F;span&gt;&lt;span&gt;)) -&amp;gt; interpret thenExpr
&lt;&#x2F;span&gt;&lt;span&gt;    (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EPrim&lt;&#x2F;span&gt;&lt;span&gt; _ (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;PBool False&lt;&#x2F;span&gt;&lt;span&gt;)) -&amp;gt; interpret elseExpr
&lt;&#x2F;span&gt;&lt;span&gt;    other -&amp;gt; throwError (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NonBooleanPredicate&lt;&#x2F;span&gt;&lt;span&gt; ann other)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We interpret if statements by reducing the predicate down to a boolean, then
taking a peek, and then interpreting the appropriate branch. If we don&#x27;t need a
branch, there&#x27;s no need to interpret it!&lt;&#x2F;p&gt;
&lt;p&gt;You can see all of the interpreter code
&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;danieljharvey&#x2F;llvm-calc&#x2F;blob&#x2F;trunk&#x2F;llvm-calc2&#x2F;src&#x2F;Calc&#x2F;Interpreter.hs&quot;&gt;here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;ok-llvm-time&quot;&gt;OK, LLVM time&lt;&#x2F;h2&gt;
&lt;p&gt;I feel like I&#x27;m rushing through all this, and maybe copy pasta-ing an entire
typechecker in the preamble was somewhat undisclined of me.&lt;&#x2F;p&gt;
&lt;p&gt;BUT, here we go.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;digression&quot;&gt;Digression&lt;&#x2F;h3&gt;
&lt;p&gt;Firstly, we&#x27;ll add a new function to our C &quot;standard library&quot;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;printbool&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(b ? &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; : &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;False&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It will take an LLVM boolean, and print either &lt;code&gt;True&lt;&#x2F;code&gt; or &lt;code&gt;False&lt;&#x2F;code&gt; depending on
whether it is &lt;code&gt;0&lt;&#x2F;code&gt; or not.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;to-the-ir&quot;&gt;To the IR!&lt;&#x2F;h3&gt;
&lt;p&gt;We&#x27;re going to start by looking at the LLVM IR for the following arbitrary
expression:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt; == 1 then True else False 
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;llvm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-llvm &quot;&gt;&lt;code class=&quot;language-llvm&quot; data-lang=&quot;llvm&quot;&gt;&lt;span&gt;; ModuleID = &amp;#39;example&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;declare external ccc  void @printbool(i1)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;define external ccc  i32 @main()    {
&lt;&#x2F;span&gt;&lt;span&gt;  %1 = icmp eq i32 2, 1
&lt;&#x2F;span&gt;&lt;span&gt;  %2 = alloca i1
&lt;&#x2F;span&gt;&lt;span&gt;  br i1 %1, label %then_0, label %else_0
&lt;&#x2F;span&gt;&lt;span&gt;then_0:
&lt;&#x2F;span&gt;&lt;span&gt;  store   i1 1, i1* %2
&lt;&#x2F;span&gt;&lt;span&gt;  br label %done_0
&lt;&#x2F;span&gt;&lt;span&gt;else_0:
&lt;&#x2F;span&gt;&lt;span&gt;  store   i1 0, i1* %2
&lt;&#x2F;span&gt;&lt;span&gt;  br label %done_0
&lt;&#x2F;span&gt;&lt;span&gt;done_0:
&lt;&#x2F;span&gt;&lt;span&gt;  %3 = load   i1, i1* %2
&lt;&#x2F;span&gt;&lt;span&gt;   call ccc  void  @printbool(i1  %3)
&lt;&#x2F;span&gt;&lt;span&gt;  ret i32 0
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;What a ride! Let&#x27;s take it line by line.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;pre data-lang=&quot;llvm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-llvm &quot;&gt;&lt;code class=&quot;language-llvm&quot; data-lang=&quot;llvm&quot;&gt;&lt;span&gt;; ModuleID = &amp;#39;example&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Once again, let&#x27;s ease ourselves in with a code comment.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;pre data-lang=&quot;llvm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-llvm &quot;&gt;&lt;code class=&quot;language-llvm&quot; data-lang=&quot;llvm&quot;&gt;&lt;span&gt;declare external ccc  void @printbool(i1)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Declaration for the new function in our standard library. It takes an &lt;code&gt;i1&lt;&#x2F;code&gt; (a
boolean, stored as &lt;code&gt;0&lt;&#x2F;code&gt; or &lt;code&gt;1&lt;&#x2F;code&gt;) and returns &lt;code&gt;void&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;pre data-lang=&quot;llvm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-llvm &quot;&gt;&lt;code class=&quot;language-llvm&quot; data-lang=&quot;llvm&quot;&gt;&lt;span&gt;define external ccc  i32 @main()    {
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We define the &lt;code&gt;main&lt;&#x2F;code&gt; function, which is the entry point of our program. It
takes no arguments, and returns an &lt;code&gt;i32&lt;&#x2F;code&gt; integer value (which becomes the exit
code).&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;pre data-lang=&quot;llvm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-llvm &quot;&gt;&lt;code class=&quot;language-llvm&quot; data-lang=&quot;llvm&quot;&gt;&lt;span&gt;%1 = icmp eq i32 2, 1
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Here we are making a new variable, &lt;code&gt;%1&lt;&#x2F;code&gt;, by comparing two integers, &lt;code&gt;2&lt;&#x2F;code&gt; and
&lt;code&gt;1&lt;&#x2F;code&gt;, using &lt;code&gt;eq&lt;&#x2F;code&gt;. This is our &lt;code&gt;2 == 1&lt;&#x2F;code&gt; expression, and maps across quite neatly.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;pre data-lang=&quot;llvm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-llvm &quot;&gt;&lt;code class=&quot;language-llvm&quot; data-lang=&quot;llvm&quot;&gt;&lt;span&gt;%2 = alloca i1
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To make control flow works, we are going to need to jump to different places.
However, LLVM has no way of passing a value back between sections. Therefore,
we are going to create a mutable placeholder for the result, and each branch
will be responsible for storing the result here. &lt;code&gt;alloca&lt;&#x2F;code&gt; is broadly &quot;allocate
memory&quot; and &lt;code&gt;i1&lt;&#x2F;code&gt; is the LLVM type for a &lt;code&gt;Boolean&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;pre data-lang=&quot;llvm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-llvm &quot;&gt;&lt;code class=&quot;language-llvm&quot; data-lang=&quot;llvm&quot;&gt;&lt;span&gt;br i1 %1, label %then_0, label %else_0
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is where we do the branching. &lt;code&gt;br&lt;&#x2F;code&gt; takes an &lt;code&gt;i1&lt;&#x2F;code&gt; value for the predicate,
and then two labels for blocks that we&#x27;ll jump to depending on the value of the predicate.
Therefore if &lt;code&gt;%1&lt;&#x2F;code&gt; is &lt;code&gt;1&lt;&#x2F;code&gt; we&#x27;ll jump to &lt;code&gt;then_0&lt;&#x2F;code&gt;, otherwise we&#x27;ll jump to
&lt;code&gt;else_0&lt;&#x2F;code&gt;. We&#x27;ll define these shortly.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;pre data-lang=&quot;llvm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-llvm &quot;&gt;&lt;code class=&quot;language-llvm&quot; data-lang=&quot;llvm&quot;&gt;&lt;span&gt;then_0:
&lt;&#x2F;span&gt;&lt;span&gt;  store   i1 1, i1* %2
&lt;&#x2F;span&gt;&lt;span&gt;  br label %done_0
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This defines a block labelled &lt;code&gt;then_0&lt;&#x2F;code&gt;. We will &quot;jump&quot; here in the &quot;then&quot; case
of the if statement. We store &lt;code&gt;1&lt;&#x2F;code&gt; in the &lt;code&gt;%2&lt;&#x2F;code&gt; variable, and then jump to the
&lt;code&gt;done_0&lt;&#x2F;code&gt; block.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;pre data-lang=&quot;llvm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-llvm &quot;&gt;&lt;code class=&quot;language-llvm&quot; data-lang=&quot;llvm&quot;&gt;&lt;span&gt;else_0:
&lt;&#x2F;span&gt;&lt;span&gt;  store   i1 0, i1* %2
&lt;&#x2F;span&gt;&lt;span&gt;  br label %done_0
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This defines a block labelled &lt;code&gt;else_0&lt;&#x2F;code&gt;. We will &quot;jump&quot; here in the &quot;else&quot; case.
Once again, we store &lt;code&gt;0&lt;&#x2F;code&gt; in the &lt;code&gt;%2&lt;&#x2F;code&gt; variable, and then jump to &lt;code&gt;done_0&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;pre data-lang=&quot;llvm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-llvm &quot;&gt;&lt;code class=&quot;language-llvm&quot; data-lang=&quot;llvm&quot;&gt;&lt;span&gt;done_0:
&lt;&#x2F;span&gt;&lt;span&gt;  %3 = load   i1, i1* %2
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This introduces a new block called &lt;code&gt;done_0&lt;&#x2F;code&gt;. As our if construct is an
expression, we always need to return something, so ee jump here when the
&lt;code&gt;then&lt;&#x2F;code&gt; or &lt;code&gt;expr&lt;&#x2F;code&gt; branches are finishing doing their business, and load whatever
they stored in &lt;code&gt;%2&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;pre data-lang=&quot;llvm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-llvm &quot;&gt;&lt;code class=&quot;language-llvm&quot; data-lang=&quot;llvm&quot;&gt;&lt;span&gt;call ccc  void  @printbool(i1  %3)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Call the &lt;code&gt;printbool&lt;&#x2F;code&gt; function from our standard library with the loaded value.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;pre data-lang=&quot;llvm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-llvm &quot;&gt;&lt;code class=&quot;language-llvm&quot; data-lang=&quot;llvm&quot;&gt;&lt;span&gt;ret i32 0
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As our program succeeded, we return a &lt;code&gt;0&lt;&#x2F;code&gt;, this becomes our exit code.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;pre data-lang=&quot;llvm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-llvm &quot;&gt;&lt;code class=&quot;language-llvm&quot; data-lang=&quot;llvm&quot;&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As a little palette cleanser, a nice closing brace.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;generating-it-from-haskell&quot;&gt;Generating it from Haskell&lt;&#x2F;h3&gt;
&lt;p&gt;Now we have &lt;code&gt;Boolean&lt;&#x2F;code&gt; as well as &lt;code&gt;Integer&lt;&#x2F;code&gt; values, we&#x27;ll need to represent them
in LLVM. We&#x27;ll use a &lt;code&gt;bit&lt;&#x2F;code&gt; which is a 1-bit LLVM number to represent
&lt;code&gt;Boolean&lt;&#x2F;code&gt;s.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;primToLLVM &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Prim -&amp;gt; LLVM&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Operand
&lt;&#x2F;span&gt;&lt;span&gt;primToLLVM (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;PInt&lt;&#x2F;span&gt;&lt;span&gt; i) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.int32 (fromIntegral i)
&lt;&#x2F;span&gt;&lt;span&gt;primToLLVM (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;PBool True&lt;&#x2F;span&gt;&lt;span&gt;) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.bit &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1
&lt;&#x2F;span&gt;&lt;span&gt;primToLLVM (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;PBool False&lt;&#x2F;span&gt;&lt;span&gt;) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.bit &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now we&#x27;ll need to choose the right printing function:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- import the correct output function from our standard library
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- depending on the output type of our expression
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;printFunction &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MonadModuleBuilder &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;m&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; Type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;m &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Operand
&lt;&#x2F;span&gt;&lt;span&gt;printFunction (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TPrim&lt;&#x2F;span&gt;&lt;span&gt; _ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TInt&lt;&#x2F;span&gt;&lt;span&gt;) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.extern &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;printint&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.i32] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.void
&lt;&#x2F;span&gt;&lt;span&gt;printFunction (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TPrim&lt;&#x2F;span&gt;&lt;span&gt; _ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TBool&lt;&#x2F;span&gt;&lt;span&gt;) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.extern &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;printbool&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.i1] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.void
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The most interesting part is &lt;code&gt;if&lt;&#x2F;code&gt; expressions. We use the &lt;code&gt;RecursiveDo&lt;&#x2F;code&gt;
extension, which gives us the &lt;code&gt;mdo&lt;&#x2F;code&gt; syntax. This lets us use bindings before
they are created. This will allow us to use &lt;code&gt;thenBlock&lt;&#x2F;code&gt; and &lt;code&gt;elseBlock&lt;&#x2F;code&gt; before
they&#x27;re defined. We create IR for the &lt;code&gt;predExpr&lt;&#x2F;code&gt;, then pass it to
&lt;code&gt;LLVM.condBr&lt;&#x2F;code&gt;, which will then jump to the appropriate block depending on the
value.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;ifToLLVM &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::
&lt;&#x2F;span&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MonadIRBuilder &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;m&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MonadModuleBuilder &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;m&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MonadFix &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;m&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;m &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Operand
&lt;&#x2F;span&gt;&lt;span&gt;ifToLLVM tyReturn predExpr thenExpr elseExpr = mdo
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- create IR for predicate
&lt;&#x2F;span&gt;&lt;span&gt;  irPred &amp;lt;- exprToLLVM predExpr
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- make variable for return value
&lt;&#x2F;span&gt;&lt;span&gt;  irReturnValue &amp;lt;- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.alloca (typeToLLVM tyReturn) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing 0
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- this does the switching
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- we haven&amp;#39;t created these blocks yet but RecursiveDo lets us do this with
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- MonadFix magic
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.condBr irPred thenBlock elseBlock
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- create a block for the &amp;#39;then` branch
&lt;&#x2F;span&gt;&lt;span&gt;  thenBlock &amp;lt;- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.block `&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.named` &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;then&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- create ir for the then branch
&lt;&#x2F;span&gt;&lt;span&gt;  irThen &amp;lt;- exprToLLVM thenExpr
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- store the result in irResultValue
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.store irReturnValue &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt; irThen
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- branch back to the &amp;#39;done&amp;#39; block
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.br doneBlock
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- create a block for the &amp;#39;else&amp;#39; branch
&lt;&#x2F;span&gt;&lt;span&gt;  elseBlock &amp;lt;- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.block `&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.named` &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;else&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- create ir for the else branch
&lt;&#x2F;span&gt;&lt;span&gt;  irElse &amp;lt;- exprToLLVM elseExpr
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- store the result in irReturnValue
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.store irReturnValue &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt; irElse
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- branch back to the `done` block
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.br doneBlock
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- create a block for &amp;#39;done&amp;#39; that we always branch to
&lt;&#x2F;span&gt;&lt;span&gt;  doneBlock &amp;lt;- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.block `&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.named` &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;done&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- load the result and return it
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.load irReturnValue &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To work out which kind of type to &lt;code&gt;alloca&lt;&#x2F;code&gt;, we take the return type and use it
to work which LLVM type to use.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;typeToLLVM &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; LLVM&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Type
&lt;&#x2F;span&gt;&lt;span&gt;typeToLLVM (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TPrim&lt;&#x2F;span&gt;&lt;span&gt; _ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TBool&lt;&#x2F;span&gt;&lt;span&gt;) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.i1
&lt;&#x2F;span&gt;&lt;span&gt;typeToLLVM (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TPrim&lt;&#x2F;span&gt;&lt;span&gt; _ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TInt&lt;&#x2F;span&gt;&lt;span&gt;) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.i32
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You can see all of the LLVM conversion code
&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;danieljharvey&#x2F;llvm-calc&#x2F;blob&#x2F;trunk&#x2F;llvm-calc2&#x2F;src&#x2F;Calc&#x2F;Compile&#x2F;ToLLVM.hs&quot;&gt;here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h3 id=&quot;well-that-s-that&quot;&gt;Well that&#x27;s that&lt;&#x2F;h3&gt;
&lt;p&gt;Congratulations, you are all bidirectional type checking experts now. Hopefully that was
somewhat helpful. Next time we&#x27;ll be adding basic functions and variables.
Great!&lt;&#x2F;p&gt;
&lt;p&gt;Make sense? If not, &lt;a href=&quot;&#x2F;contact.html&quot;&gt;get in touch&lt;&#x2F;a&gt;!&lt;&#x2F;p&gt;
&lt;p&gt;Further reading:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;llvm.org&#x2F;docs&#x2F;LangRef.html&quot;&gt;llvm reference&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;danieljharvey&#x2F;llvm-calc&quot;&gt;llvm-calc&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Compiling a functional language to LLVM, part 1</title>
          <pubDate>Wed, 08 Feb 2023 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/llvm-compiler-part-1/</link>
          <guid>https://danieljharvey.github.io/llvm-compiler-part-1/</guid>
          <description xml:base="https://danieljharvey.github.io/llvm-compiler-part-1/">&lt;p&gt;Recently I thought it would be good to start compiling the small functional
language &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;danieljharvey&#x2F;mimsa&quot;&gt;mimsa&lt;&#x2F;a&gt; I&#x27;ve been messing
around with for the last few years to LLVM. There&#x27;s a few really helpful
resources - Joseph Morag&#x27;s &lt;a href=&quot;https:&#x2F;&#x2F;blog.josephmorag.com&#x2F;posts&#x2F;mcc0&#x2F;&quot;&gt;Micro-C&lt;&#x2F;a&gt;
series, and &lt;a href=&quot;https:&#x2F;&#x2F;mapping-high-level-constructs-to-llvm-ir.readthedocs.io&#x2F;&quot;&gt;Mapping High Level Concepts to LLVM
IR&lt;&#x2F;a&gt; in
particular, but neither go into converting functional programming paradigms
such as pattern matching, lambdas and ADTs in much depth.&lt;&#x2F;p&gt;
&lt;p&gt;So, here we&#x27;re going to give it a go. The plan is that we start with an
entirely working but tiny language, get it all working, and then as we add
features to the language, we&#x27;ll also add a typechecker and introduce more LLVM
concepts. The source code for the whole working calculator, along with a basic repl, can be found
at &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;danieljharvey&#x2F;llvm-calc&#x2F;tree&#x2F;trunk&#x2F;llvm-calc&quot;&gt;llvm-calc&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;our-language&quot;&gt;Our language&lt;&#x2F;h2&gt;
&lt;p&gt;The first iteration of our language is going to be the most basic of
calculators. Here is a valid program in our language:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt; + 2 * (1 + 100) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt; 100
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Here are the datatypes for our language. We will use this to represent our
programs internally.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt; ann
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- | An integer value
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EPrim&lt;&#x2F;span&gt;&lt;span&gt; ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Int
&lt;&#x2F;span&gt;&lt;span&gt;  | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- | Two values combined with an operator
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EInfix&lt;&#x2F;span&gt;&lt;span&gt; ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Op&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt; ann) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt; ann)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; stock (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Eq&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Ord&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Show&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Functor&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Foldable&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Traversable&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- | Operators for combining integers
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Op &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;OpAdd &lt;&#x2F;span&gt;&lt;span&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;OpMultiply &lt;&#x2F;span&gt;&lt;span&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;OpSubtract
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; stock (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Eq&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Ord&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Show&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Here&#x27;s some example values:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- | the number `1`
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;one &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Expr &lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;one = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EPrim () 1
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- | the expression `2 + 3`
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;twoPlusTwo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Expr &lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;twoPlusTwo = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EInfix () OpAdd&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EPrim () 2&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EPrim () 3&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We call datatypes like &lt;code&gt;Expr&lt;&#x2F;code&gt; &lt;em&gt;Abstract Syntax Trees&lt;&#x2F;em&gt; (or ASTs).&lt;&#x2F;p&gt;
&lt;p&gt;You may notice the &lt;code&gt;ann&lt;&#x2F;code&gt; type variable. We&#x27;ll use this to &quot;decorate&quot; items in
our AST. For now, we&#x27;ll just use &lt;code&gt;()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;parsing-text-into-ast-terms&quot;&gt;Parsing text into AST terms&lt;&#x2F;h2&gt;
&lt;p&gt;Although we could provide our compiler as a Haskell library and ask our users
to manually create &lt;code&gt;Expr&lt;&#x2F;code&gt; values for us, it&#x27;d be much nicer to be able to read
input from a user.&lt;&#x2F;p&gt;
&lt;p&gt;This process is called &lt;em&gt;parsing&lt;&#x2F;em&gt;, and looks something like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;parseInput &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Text -&amp;gt; Either ParseError&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Expr &lt;&#x2F;span&gt;&lt;span&gt;())
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Given some user input, parsing returns either a valid &lt;code&gt;Expr&lt;&#x2F;code&gt; or a (hopefully)
helpful error.&lt;&#x2F;p&gt;
&lt;p&gt;The whole parser can be seen
&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;danieljharvey&#x2F;llvm-calc&#x2F;tree&#x2F;trunk&#x2F;llvm-calc&#x2F;src&#x2F;Calc&#x2F;Parser&quot;&gt;here&lt;&#x2F;a&gt;. We&#x27;ll not go into too much depth as there are already lots of great references
on parsing, but a few details are worth noting:&lt;&#x2F;p&gt;
&lt;h3 id=&quot;which-library&quot;&gt;Which library?&lt;&#x2F;h3&gt;
&lt;p&gt;We&#x27;ll be using a library called
&lt;a href=&quot;https:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;megaparsec&quot;&gt;megaparsec&lt;&#x2F;a&gt;. It&#x27;s fast, it&#x27;s got a &lt;a href=&quot;https:&#x2F;&#x2F;markkarpov.com&#x2F;tutorial&#x2F;megaparsec.html&quot;&gt;great
tutorial&lt;&#x2F;a&gt;, and it generates errors that we can render nicely with the &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;Mesabloo&#x2F;diagnose&quot;&gt;diagnose&lt;&#x2F;a&gt; library.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;llvm-1-parse-error.png&quot; alt=&quot;A megaparsec parse error made nice with diagnose&quot; title=&quot;A megaparsec parse error made nice with diagnose&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;dealing-with-whitespace&quot;&gt;Dealing with whitespace&lt;&#x2F;h3&gt;
&lt;p&gt;Although some languages make whitespace meaningful, we won&#x27;t be bothering with
any of that for now. However we would like to allow our users some flexibility
with how they lay out their code. &lt;code&gt;1+1&lt;&#x2F;code&gt; and &lt;code&gt;  1    + 1&lt;&#x2F;code&gt; should mean the same thing. That means we either need a lot of code like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;parseInfix &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Parser Int
&lt;&#x2F;span&gt;&lt;span&gt;parseInfix = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  _ &amp;lt;- space0 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- this accepts 0 or more spaces 
&lt;&#x2F;span&gt;&lt;span&gt;  a &amp;lt;- parseInt
&lt;&#x2F;span&gt;&lt;span&gt;  _ &amp;lt;- space0 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- space between number and operator
&lt;&#x2F;span&gt;&lt;span&gt;  op &amp;lt;- parseOp
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;- &amp;lt;- space0 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- space between operator and second number
&lt;&#x2F;span&gt;&lt;span&gt;  b &amp;lt;- parseInt
&lt;&#x2F;span&gt;&lt;span&gt;  pure (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Infix&lt;&#x2F;span&gt;&lt;span&gt; op a b)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;...or we need a neat way of automating this. &lt;code&gt;megaparsec&lt;&#x2F;code&gt; has a handy thing
called a &lt;code&gt;lexeme&lt;&#x2F;code&gt;. This is a sort of rule that says
&quot;when eating this value, also eat all the whitespace before it&quot;. When you see
&lt;code&gt;myLexeme&lt;&#x2F;code&gt; in the code, this means we&#x27;re wrapping a parser with
whitespace-eating powers. This is
covered in much better detail in the
&lt;a href=&quot;https:&#x2F;&#x2F;markkarpov.com&#x2F;tutorial&#x2F;megaparsec.html#white-space&quot;&gt;megaparsec tutorial&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;parsing-infixes&quot;&gt;Parsing infixes&lt;&#x2F;h3&gt;
&lt;p&gt;One problem with parser combinator libraries is that it&#x27;s easy to get into an
infinite loop. &lt;code&gt;megaparsec&lt;&#x2F;code&gt; has a bunch of useful helpers for this, in
particular, support for &lt;code&gt;Operator&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- | to make infix stuff like easier, `megaparsec` provides
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- the `Operator` type
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;table &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Operator Parser&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Expr Annotation&lt;&#x2F;span&gt;&lt;span&gt;)]]
&lt;&#x2F;span&gt;&lt;span&gt;table =
&lt;&#x2F;span&gt;&lt;span&gt;  [ [binary &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EInfix&lt;&#x2F;span&gt;&lt;span&gt; mempty &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;OpMultiply&lt;&#x2F;span&gt;&lt;span&gt;)],
&lt;&#x2F;span&gt;&lt;span&gt;    [ binary &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EInfix&lt;&#x2F;span&gt;&lt;span&gt; mempty &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;OpAdd&lt;&#x2F;span&gt;&lt;span&gt;),
&lt;&#x2F;span&gt;&lt;span&gt;      binary &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EInfix&lt;&#x2F;span&gt;&lt;span&gt; mempty &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;OpSubtract&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    ]
&lt;&#x2F;span&gt;&lt;span&gt;  ]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;A nice part of the the above code is that by putting &lt;code&gt;*&lt;&#x2F;code&gt; in a group before &lt;code&gt;+&lt;&#x2F;code&gt;
and &lt;code&gt;-&lt;&#x2F;code&gt;, we have defined operator precidence and so &lt;code&gt;6 * 1 + 1&lt;&#x2F;code&gt; equals &lt;code&gt;7&lt;&#x2F;code&gt; rather
than &lt;code&gt;12&lt;&#x2F;code&gt; (which we&#x27;d get from &lt;code&gt;6 * (1 + 1)&lt;&#x2F;code&gt;).&lt;&#x2F;p&gt;
&lt;h3 id=&quot;source-code-location&quot;&gt;Source code location&lt;&#x2F;h3&gt;
&lt;p&gt;You may have noticed the &lt;code&gt;ann&lt;&#x2F;code&gt; type variable in our &lt;code&gt;Expr&lt;&#x2F;code&gt; type. This allows us
to &quot;decorate&quot; our AST nodes. In parsing, it&#x27;s useful to decorate each node with
it&#x27;s location in the code.&lt;&#x2F;p&gt;
&lt;p&gt;We use the following datatype for this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Annotation &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Location Int Int
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; stock (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Eq&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Ord&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Show&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The two &lt;code&gt;Int&lt;&#x2F;code&gt; values are &quot;character number we start at&quot; and &quot;length of the
string&quot;. Therefore parsing &quot; 100&quot; should return:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;parseResult &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Expr Annotation
&lt;&#x2F;span&gt;&lt;span&gt;parseResult = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EPrim&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Location 1 3&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;100
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We won&#x27;t be using these values today as our language is so limited that it&#x27;s
pretty difficult to break, but as we add multiple types (and thus, the
possibility of type errors) we&#x27;ll use them to show the user where they did a
boo boo.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;a-simple-interpreter&quot;&gt;A simple interpreter&lt;&#x2F;h2&gt;
&lt;p&gt;Before we get stuck into &lt;code&gt;LLVM&lt;&#x2F;code&gt;, it&#x27;s good to be able to evaluate our language
internally. Although it&#x27;s pretty clear how this calculator should work, it
will be useful to compare our own simple interpreter with the LLVM output as
things get more complicated.&lt;&#x2F;p&gt;
&lt;p&gt;You can view the full code
&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;danieljharvey&#x2F;llvm-calc&#x2F;blob&#x2F;trunk&#x2F;llvm-calc&#x2F;src&#x2F;Calc&#x2F;Interpreter.hs&quot;&gt;here&lt;&#x2F;a&gt;.
As you can see, there isn&#x27;t very much of it:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;interpret &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Expr &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Expr &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann
&lt;&#x2F;span&gt;&lt;span&gt;interpret (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EPrim&lt;&#x2F;span&gt;&lt;span&gt; ann p) = 
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- don&amp;#39;t do anything with these
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EPrim&lt;&#x2F;span&gt;&lt;span&gt; ann p
&lt;&#x2F;span&gt;&lt;span&gt;interpret (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EInfix&lt;&#x2F;span&gt;&lt;span&gt; ann op a b) =
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- attempt to simplify these 
&lt;&#x2F;span&gt;&lt;span&gt;  interpretInfix ann op a b
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;interpretInfix &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Op -&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Expr &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Expr &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Expr &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann
&lt;&#x2F;span&gt;&lt;span&gt;interpretInfix ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;OpAdd&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EPrim&lt;&#x2F;span&gt;&lt;span&gt; _ a) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EPrim&lt;&#x2F;span&gt;&lt;span&gt; _ b) =
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- add the int values
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EPrim&lt;&#x2F;span&gt;&lt;span&gt; ann (a + b)
&lt;&#x2F;span&gt;&lt;span&gt;interpretInfix ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;OpSubtract&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EPrim&lt;&#x2F;span&gt;&lt;span&gt; _ a) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EPrim&lt;&#x2F;span&gt;&lt;span&gt; _ b) =
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- subtract the int values
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EPrim&lt;&#x2F;span&gt;&lt;span&gt; ann (a - b)
&lt;&#x2F;span&gt;&lt;span&gt;interpretInfix ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;OpMultiply&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EPrim&lt;&#x2F;span&gt;&lt;span&gt; _ a) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EPrim&lt;&#x2F;span&gt;&lt;span&gt; _ b) =
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- multiply the int values
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EPrim&lt;&#x2F;span&gt;&lt;span&gt; ann (a * b)
&lt;&#x2F;span&gt;&lt;span&gt;interpretInfix ann op a b = 
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- one of our arguments isn&amp;#39;t yet a number
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- so simplify the leaves and try again
&lt;&#x2F;span&gt;&lt;span&gt;  interpretInfix ann op (interpret a) (interpret b)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;When we see an &lt;code&gt;EInfix&lt;&#x2F;code&gt; value, we look for &lt;code&gt;EPrim&lt;&#x2F;code&gt; values inside and &lt;code&gt;add&lt;&#x2F;code&gt; &#x2F; &lt;code&gt;subtract&lt;&#x2F;code&gt; &#x2F; &lt;code&gt;multiply&lt;&#x2F;code&gt; them. If they&#x27;re
not &lt;code&gt;EPrim&lt;&#x2F;code&gt; values yet, then they must be nested expressions, so we &lt;code&gt;interpret&lt;&#x2F;code&gt; them
and try again.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;llvm-1-giant-calculator.jpg&quot; alt=&quot;A big calculator&quot; title=&quot;A big calculator&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;enough-nonsense-let-s-do-some-compiling&quot;&gt;Enough nonsense, let&#x27;s do some compiling&lt;&#x2F;h2&gt;
&lt;p&gt;Hopefully we understand what our small language is now, so let&#x27;s get down to
the business of turning it into real life native code.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;what-is-llvm&quot;&gt;What is LLVM&lt;&#x2F;h3&gt;
&lt;p&gt;LLVM stands for Low Level Virtual Machine. The idea is that higher level languages compile into LLVM, and then LLVM is turned into whatever local version of assembly is required. This means that by using LLVM, your programs will work on lots of architectures without you needing to understand a tremendous amount about them. As somebody who understands pretty much nothing about any processor architecture, this is very appealing indeed. If you need further persuading, &lt;code&gt;Rust&lt;&#x2F;code&gt;, &lt;code&gt;Swift&lt;&#x2F;code&gt; and &lt;code&gt;GHC&lt;&#x2F;code&gt; can all compile to
LLVM.&lt;&#x2F;p&gt;
&lt;p&gt;LLVM is part of the &lt;code&gt;clang&lt;&#x2F;code&gt; C compiler, and as a result, it&#x27;s very C shaped.
It&#x27;s got a bunch of number types, structs, and arrays, functions, and enough pointers to have a
very bad time.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;our-runtime&quot;&gt;Our &quot;runtime&quot;&lt;&#x2F;h3&gt;
&lt;p&gt;In order to &lt;em&gt;do things&lt;&#x2F;em&gt; our language will need a runtime, This is a bunch of helper code written in something that be compiled to the target
language. Because we&#x27;ll be compiling using &lt;code&gt;clang&lt;&#x2F;code&gt;, we&#x27;ll use &lt;code&gt;C&lt;&#x2F;code&gt;. I don&#x27;t know much about &lt;code&gt;C&lt;&#x2F;code&gt;, but what I can tell you is that this file contains a single function called &lt;code&gt;printint&lt;&#x2F;code&gt;, that takes an &lt;code&gt;int&lt;&#x2F;code&gt; and prints
it to &lt;code&gt;stdout&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdlib.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;printint&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;i&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, i);
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is the only &lt;code&gt;C&lt;&#x2F;code&gt; you&#x27;ll need to see today. I&#x27;m sorry about that. Onwards!&lt;&#x2F;p&gt;
&lt;h3 id=&quot;our-first-llvm-module&quot;&gt;Our first LLVM module&lt;&#x2F;h3&gt;
&lt;p&gt;LLVM is organised in &lt;a href=&quot;https:&#x2F;&#x2F;llvm.org&#x2F;docs&#x2F;LangRef.html#module-structure&quot;&gt;modules&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;To start us off, here is the IR (Intermediate Representation) for printing the number &lt;code&gt;42&lt;&#x2F;code&gt; to &lt;code&gt;stdout&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;llvm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-llvm &quot;&gt;&lt;code class=&quot;language-llvm&quot; data-lang=&quot;llvm&quot;&gt;&lt;span&gt;; ModuleID = &amp;#39;calc&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;declare external ccc i32 @printint(i32)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;define external ccc i32 @main()    {
&lt;&#x2F;span&gt;&lt;span&gt;  %1 =  call ccc i32 @printint(i32  42)
&lt;&#x2F;span&gt;&lt;span&gt;  ret i32 0
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Let&#x27;s take this apart line by line:&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;pre data-lang=&quot;llvm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-llvm &quot;&gt;&lt;code class=&quot;language-llvm&quot; data-lang=&quot;llvm&quot;&gt;&lt;span&gt;; ModuleID = &amp;#39;calc&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is a comment. See, LLVM is easy.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;pre data-lang=&quot;llvm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-llvm &quot;&gt;&lt;code class=&quot;language-llvm&quot; data-lang=&quot;llvm&quot;&gt;&lt;span&gt;declare external ccc i32 @printint(i32)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This declares an external function we&#x27;d like to use. In this case it&#x27;s the
&lt;code&gt;printint&lt;&#x2F;code&gt; function from our standard library defined above. It
takes a single argument, an &lt;code&gt;i32&lt;&#x2F;code&gt;, and returns an &lt;code&gt;i32&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;pre data-lang=&quot;llvm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-llvm &quot;&gt;&lt;code class=&quot;language-llvm&quot; data-lang=&quot;llvm&quot;&gt;&lt;span&gt;define external ccc i32 @main() {
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This defines a function called &lt;code&gt;main&lt;&#x2F;code&gt; that takes no arguments and returns a
single &lt;code&gt;i32&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;ccc&lt;&#x2F;code&gt; part is the &quot;calling convention&quot;. This defines the manner in which
LLVM will generate the function code. There are &lt;a href=&quot;https:&#x2F;&#x2F;llvm.org&#x2F;docs&#x2F;LangRef.html#calling-conventions&quot;&gt;absolutely loads of these&lt;&#x2F;a&gt;, many
designs to optimise specific languages. We&#x27;ll keep to &lt;code&gt;ccc&lt;&#x2F;code&gt; for now, but may
want to change our minds when we start doing tail calls.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;pre data-lang=&quot;llvm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-llvm &quot;&gt;&lt;code class=&quot;language-llvm&quot; data-lang=&quot;llvm&quot;&gt;&lt;span&gt;%1 =  call ccc i32 @printint(i32  42)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This line of defines a variable &lt;code&gt;%1&lt;&#x2F;code&gt;, and sets it the result of calling the
&lt;code&gt;printint&lt;&#x2F;code&gt; function we imported above. We pass the &lt;code&gt;printint&lt;&#x2F;code&gt; function a single
argument, an &lt;code&gt;i32&lt;&#x2F;code&gt; value of &lt;code&gt;42&lt;&#x2F;code&gt;. This will print the number &lt;code&gt;42&lt;&#x2F;code&gt; to stdout.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;pre data-lang=&quot;llvm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-llvm &quot;&gt;&lt;code class=&quot;language-llvm&quot; data-lang=&quot;llvm&quot;&gt;&lt;span&gt;ret i32 0
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Functions in &lt;code&gt;LLVM&lt;&#x2F;code&gt; must return something (although that thing can be &lt;code&gt;null&lt;&#x2F;code&gt;).
This line returns an &lt;code&gt;i32&lt;&#x2F;code&gt; value of &lt;code&gt;0&lt;&#x2F;code&gt;. Because this is the &lt;code&gt;main&lt;&#x2F;code&gt; function of
our program, this is what will be returned to the operating system as our exit
code. &lt;code&gt;0&lt;&#x2F;code&gt; means &quot;great job, everything went fine&quot;.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;pre data-lang=&quot;llvm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-llvm &quot;&gt;&lt;code class=&quot;language-llvm&quot; data-lang=&quot;llvm&quot;&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is a closing curly brace. &lt;code&gt;LLVM&lt;&#x2F;code&gt; is like &lt;code&gt;C&lt;&#x2F;code&gt;, we have to specifically
end our functions. No great hardship though.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h3 id=&quot;compiling-and-running-our-hand-baked-module&quot;&gt;Compiling and running our hand-baked module&lt;&#x2F;h3&gt;
&lt;p&gt;To check that the code above does what we say it does, and to check we&#x27;ve got
everything installed that we need, let&#x27;s write the LLVM to a file called
&lt;code&gt;module.ll&lt;&#x2F;code&gt;, the C code to &lt;code&gt;runtime.c&lt;&#x2F;code&gt;, and then compile it using &lt;code&gt;clang&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;clang -Wno-override-module -lm&lt;&#x2F;span&gt;&lt;span&gt; module.ll runtime.c&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -o&lt;&#x2F;span&gt;&lt;span&gt; a.out 
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This should output an executable called &lt;code&gt;a.out&lt;&#x2F;code&gt;. If we run it, we should see
this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.&#x2F;a.out
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;42
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Hooray!&lt;&#x2F;p&gt;
&lt;h2 id=&quot;compiling-our-programming-language-to-llvm&quot;&gt;Compiling our programming language to LLVM&lt;&#x2F;h2&gt;
&lt;p&gt;OK. So now we&#x27;ve had a taste of the raw power available to us, let&#x27;s get down
to business. Although we could just create raw LLVM IR by hand, instead we will use the following
libraries:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;llvm-hs-pure&quot;&gt;llvm-hs-pure&lt;&#x2F;a&gt; - a set of
types for LLVM IR, along with some helpful building functions.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;llvm-hs-pretty&quot;&gt;llvm-hs-pretty&lt;&#x2F;a&gt; - a
prettyprinter for &lt;code&gt;llvm-hs-pure&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;We&#x27;ll use the datatypes in &lt;code&gt;llvm-hs-pure&lt;&#x2F;code&gt; to create modules, then use &lt;code&gt;llvm-hs-pretty&lt;&#x2F;code&gt; to render these to files and then compile by hand. This means that when we
invariably generate LLVM errors, we&#x27;ll at least we able to look at the error
and reference the code we&#x27;ve given it to try and work out what went wrong.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;shut-up-and-show-me-some-haskell-code&quot;&gt;Shut up and show me some Haskell code&lt;&#x2F;h3&gt;
&lt;p&gt;The whole module can be seen
&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;danieljharvey&#x2F;llvm-calc&#x2F;blob&#x2F;trunk&#x2F;llvm-calc&#x2F;src&#x2F;Calc&#x2F;Compile&#x2F;ToLLVM.hs&quot;&gt;here&lt;&#x2F;a&gt;, but let&#x27;s look at the highlights:&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;primToLLVM &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int -&amp;gt; LLVM&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Operand
&lt;&#x2F;span&gt;&lt;span&gt;primToLLVM i = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.int32 (fromIntegral i)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This function creates integer literals from our &lt;code&gt;EPrim&lt;&#x2F;code&gt; constructors, using
the &lt;a href=&quot;https:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;llvm-hs-pure-9.0.0&#x2F;docs&#x2F;LLVM-IRBuilder-Constant.html#v:int32&quot;&gt;int32&lt;&#x2F;a&gt; function.
This returns an &lt;code&gt;Operand&lt;&#x2F;code&gt; (an LLVM value, broadly) that we can pass to other LLVM functions. We&#x27;ll
expand this function to include more types as we need them.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;exprToLLVM &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::
&lt;&#x2F;span&gt;&lt;span&gt;  ( &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MonadIRBuilder &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;m&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MonadModuleBuilder &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;m
&lt;&#x2F;span&gt;&lt;span&gt;  ) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Expr &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;m &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Operand
&lt;&#x2F;span&gt;&lt;span&gt;exprToLLVM (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EPrim&lt;&#x2F;span&gt;&lt;span&gt; _ prim) = 
&lt;&#x2F;span&gt;&lt;span&gt;  pure $ primToLLVM prim
&lt;&#x2F;span&gt;&lt;span&gt;exprToLLVM (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EInfix&lt;&#x2F;span&gt;&lt;span&gt; _ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;OpAdd&lt;&#x2F;span&gt;&lt;span&gt; a b) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  lhs &amp;lt;- exprToLLVM a
&lt;&#x2F;span&gt;&lt;span&gt;  rhs &amp;lt;- exprToLLVM b
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.add lhs rhs
&lt;&#x2F;span&gt;&lt;span&gt;exprToLLVM (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EInfix&lt;&#x2F;span&gt;&lt;span&gt; _ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;OpSubtract&lt;&#x2F;span&gt;&lt;span&gt; a b) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  lhs &amp;lt;- exprToLLVM a
&lt;&#x2F;span&gt;&lt;span&gt;  rhs &amp;lt;- exprToLLVM b
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.sub lhs rhs
&lt;&#x2F;span&gt;&lt;span&gt;exprToLLVM (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;EInfix&lt;&#x2F;span&gt;&lt;span&gt; _ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;OpMultiply&lt;&#x2F;span&gt;&lt;span&gt; a b) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  lhs &amp;lt;- exprToLLVM a
&lt;&#x2F;span&gt;&lt;span&gt;  rhs &amp;lt;- exprToLLVM b
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.mul lhs rhs
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Here we compile our &lt;code&gt;Expr&lt;&#x2F;code&gt; into and take care of adding &#x2F;
subtracting &#x2F; multiplying integers.&lt;&#x2F;p&gt;
&lt;p&gt;Because we&#x27;re using the &lt;code&gt;MonadIRBuilder&lt;&#x2F;code&gt;
and &lt;code&gt;MonadModuleBuilder&lt;&#x2F;code&gt;, it&#x27;s almost as if we&#x27;re writing code to do the
interpreting by hand, as all the actual code output is plumbed away in the
monad. If you squint, it looks very similar to the interpreter we wrote earlier, so if you
understand that, you pretty much understand this.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- | given our `Expr` type, turn it into an LLVM module
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;toLLVM &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Expr &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ann &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; LLVM&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Module
&lt;&#x2F;span&gt;&lt;span&gt;toLLVM expr =
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.buildModule &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;example&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; $ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- import `printint` from our standard library
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- it takes an `i32` and returns an `i32`
&lt;&#x2F;span&gt;&lt;span&gt;    printInt &amp;lt;- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.extern &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;printint&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.i32] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.i32
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- create a function called `main` that will be the entry point to our
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- program
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.function &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;[] LLVM&lt;&#x2F;span&gt;&lt;span&gt;.i32 $ \_ -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- build the LLVM AST for our expression
&lt;&#x2F;span&gt;&lt;span&gt;      ourExpression &amp;lt;- exprToLLVM expr
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- print our result to stdout
&lt;&#x2F;span&gt;&lt;span&gt;      _ &amp;lt;- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.call printInt [(ourExpression, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;[]&lt;&#x2F;span&gt;&lt;span&gt;)]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- return success exit code of `0`
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.ret (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LLVM&lt;&#x2F;span&gt;&lt;span&gt;.int32 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Finally, we wrap up our compiled &lt;code&gt;Expr&lt;&#x2F;code&gt; and turn it into a
module. We are going to import &lt;code&gt;printint&lt;&#x2F;code&gt; from our standard library, output the
response of our computation to stdout, and then return exit code &lt;code&gt;0&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s look at the generated LLVM for &lt;code&gt;6 * 8 - 3&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;llvm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-llvm &quot;&gt;&lt;code class=&quot;language-llvm&quot; data-lang=&quot;llvm&quot;&gt;&lt;span&gt;; ModuleID = &amp;#39;example&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;declare external ccc i32 @printint(i32)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;define external ccc i32 @main() {
&lt;&#x2F;span&gt;&lt;span&gt;  %1 = mul   i32 6, 8
&lt;&#x2F;span&gt;&lt;span&gt;  %2 = sub   i32 %1, 3
&lt;&#x2F;span&gt;&lt;span&gt;  %3 =  call ccc  i32  @printint(i32  %2)
&lt;&#x2F;span&gt;&lt;span&gt;  ret i32 0
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Hopefully, nothing too surprising.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;running-our-code&quot;&gt;Running our code&lt;&#x2F;h2&gt;
&lt;p&gt;As with earlier, our little compiler works by pretty printing the LLVM module
we created, then running &lt;code&gt;clang&lt;&#x2F;code&gt; to compile it into an executable. I pretty
much stole the compiling code from
&lt;a href=&quot;https:&#x2F;&#x2F;blog.josephmorag.com&#x2F;posts&#x2F;mcc3&#x2F;&quot;&gt;Micro-C&lt;&#x2F;a&gt; (thanks&#x2F;sorry, Joseph!).
You can see our slightly tattered version
&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;danieljharvey&#x2F;llvm-calc&#x2F;blob&#x2F;trunk&#x2F;llvm-calc&#x2F;src&#x2F;Calc&#x2F;Compile&#x2F;RunLLVM.hs&quot;&gt;here&lt;&#x2F;a&gt; if for some reason you don&#x27;t believe any of this actually works.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;well-that-s-that&quot;&gt;Well that&#x27;s that&lt;&#x2F;h3&gt;
&lt;p&gt;Congratulations, you are all low-level compiler experts now. Hopefully that was
somewhat helpful. Next time we&#x27;ll be adding the equality operator and some basic control flow.
Exciting!&lt;&#x2F;p&gt;
&lt;p&gt;Make sense? If not, &lt;a href=&quot;&#x2F;contact.html&quot;&gt;get in touch&lt;&#x2F;a&gt;!&lt;&#x2F;p&gt;
&lt;p&gt;Further reading:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;llvm.org&#x2F;docs&#x2F;LangRef.html&quot;&gt;llvm reference&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;danieljharvey&#x2F;llvm-calc&quot;&gt;llvm-calc&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Hindley Milner typechecking by example</title>
          <pubDate>Sun, 24 Jul 2022 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/typechecking-pt1/</link>
          <guid>https://danieljharvey.github.io/typechecking-pt1/</guid>
          <description xml:base="https://danieljharvey.github.io/typechecking-pt1/">&lt;p&gt;So I&#x27;ve been getting really interested in typechecking, and well, the best way
to learn is to try and explain it in words, so here we go.&lt;&#x2F;p&gt;
&lt;p&gt;There are many kinds of typechecking, but today I want to explain a variation
on Hindley Milner (I&#x27;ve also seen it called Damas Milner, please feel free to
ping with the actual truth, fact fans). This kind of type system is the kind
you&#x27;ll find under OCaml, Elm, Haskell and Purescript, although each language has their own variations (and in the case of Haskell, a terrifying number of extensions to it).&lt;&#x2F;p&gt;
&lt;h2 id=&quot;a-working-example&quot;&gt;A working example&lt;&#x2F;h2&gt;
&lt;p&gt;We&#x27;re going to start by typechecking the simplest stuff, and then build up
features as we go. There are three stages to our algorithm, &lt;code&gt;Elaboration&lt;&#x2F;code&gt;,
&lt;code&gt;Unification&lt;&#x2F;code&gt; and &lt;code&gt;Substitution&lt;&#x2F;code&gt;. We&#x27;ll introduce them as we need them.&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;🥚 Elaboration 🥚&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Here go through the expression we&#x27;d like to typecheck, marking each
part of the AST with the thing we think it is (and if we don&#x27;t know, we sort of guess). We&#x27;ll also produce
a set of constraints about the types we&#x27;ve worked out (a constraint is
something like &lt;strong&gt;&quot;I don&#x27;t know &lt;code&gt;a&lt;&#x2F;code&gt; or &lt;code&gt;b&lt;&#x2F;code&gt; are but I know they&#x27;re the same
thing&quot;&lt;&#x2F;strong&gt;)&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;h3 id=&quot;literals&quot;&gt;Literals&lt;&#x2F;h3&gt;
&lt;p&gt;Literals are static values declared in code. Our toy language has the
&lt;code&gt;Boolean&lt;&#x2F;code&gt;, &lt;code&gt;Integer&lt;&#x2F;code&gt; and &lt;code&gt;String&lt;&#x2F;code&gt; types. As this means there is no ambiguity
between them, we can work out their types in the &lt;code&gt;Elaboration&lt;&#x2F;code&gt; stage alone.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;typecheck-1-elaborate-literals.png&quot; alt=&quot;Elaborating literal values&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The values are on the left in black, the types on the right in blue. No weird surprises here, hopefully.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;let-bindings&quot;&gt;Let bindings&lt;&#x2F;h3&gt;
&lt;p&gt;It is nice to be able to give things names, so we should have some let bindings
in our language. These allow us to introduce a new concept - the typechecking
environment. A let binding has two sub-expressions - the first is the one
that gets &quot;bound&quot; and assigned to the name, and then the body, which can refer
to the bound expression by name.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;typecheck-1-elaborate-let.png&quot; alt=&quot;elaborating let binding for let a = 1 in true&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;In this example, our environment (shown in green) is initially empty, but as we were able to
elaborate the type of &lt;code&gt;a&lt;&#x2F;code&gt; as &lt;code&gt;Integer&lt;&#x2F;code&gt;, the environment when typechecking the
body also includes &lt;code&gt;a == Integer&lt;&#x2F;code&gt;. However, this has no effect on the type of
the whole expression, as the body of the let binding is &lt;code&gt;True&lt;&#x2F;code&gt; which we know is of type &lt;code&gt;Boolean&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;variable&quot;&gt;Variable&lt;&#x2F;h3&gt;
&lt;p&gt;It&#x27;s all well and good making variables, but it&#x27;s even more useful to refer to
them later. Given a typechecking environment that already knows about &lt;code&gt;b&lt;&#x2F;code&gt;, we
can look it up:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;typecheck-1-elaborate-var.png&quot; alt=&quot;Elaborating variable b&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;If it&#x27;s not there, we
throw a type error and tell the user they&#x27;ve made a typo or something. If it
is, then we return the type from the environment.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;if-then-else&quot;&gt;If &#x2F; Then &#x2F; Else&lt;&#x2F;h3&gt;
&lt;p&gt;An if statement in our language is an expression, meaning it must return
something from both the &lt;code&gt;then&lt;&#x2F;code&gt; and the &lt;code&gt;else&lt;&#x2F;code&gt; branch. It contains three
sub-expressions, one for the predicate we are testing (which must be of type
&lt;code&gt;Boolean&lt;&#x2F;code&gt;), one for the &lt;code&gt;then&lt;&#x2F;code&gt; branch, and one for the &lt;code&gt;else&lt;&#x2F;code&gt; branch.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;typecheck-1-elaborate-if.png&quot; alt=&quot;Elaborating if True then 1 else 2&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Because each of these sub-expressions are literal values, we can elaborate their
values as we did above. The return type is &lt;code&gt;Integer&lt;&#x2F;code&gt;, as that is the type we
have worked out for the &lt;code&gt;then&lt;&#x2F;code&gt; branch (it could as well be the type of the
&lt;code&gt;else&lt;&#x2F;code&gt; branch, they should be the same).&lt;&#x2F;p&gt;
&lt;p&gt;How do we ensure all our rules work out? That&#x27;s the job of the constraints that
we have generated (shown in red). We have one that ensures the type of &lt;code&gt;True&lt;&#x2F;code&gt; matches
&lt;code&gt;Boolean&lt;&#x2F;code&gt;, and another that ensures the types of the &lt;code&gt;then&lt;&#x2F;code&gt; and &lt;code&gt;else&lt;&#x2F;code&gt; branches are the same.&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;🥚 Unification 🥚&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Now that we have constraints, we need to check they make sense. Unification in the context of typechecking is about smashing two things
together and seeing what we learn.&lt;&#x2F;p&gt;
&lt;p&gt;The outcome of unification is either a type error or a set of substitutions, such as &lt;strong&gt;&quot;replace all &lt;code&gt;c&lt;&#x2F;code&gt; values with &lt;code&gt;String&lt;&#x2F;code&gt;&quot;&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;Here the two things are the same in both cases, so everything is fine and we
learn nothing new.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;typecheck-1-unify-if.png&quot; alt=&quot;Unifying if &#x2F; then &#x2F; else expressions&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;lambda&quot;&gt;Lambda&lt;&#x2F;h3&gt;
&lt;p&gt;A lambda is a function abstraction. When elaborating lambdas, we don&#x27;t know what the type of the argument will be. Different type systems have different approaches to this problem.
One solution is to make the programmer annotate the argument with a type (ie,
&lt;code&gt;\(a: String) -&amp;gt; ...&lt;&#x2F;code&gt;). However, Hindley Milner typechecking is all about
minimising type annotations, so instead we&#x27;ll guess it. We do this by creating a fresh unknown type variable called a
&quot;unification variable&quot;. Each of these should be unique within the typechecking,
so they often use numbers with an internal counter to generate fresh ones.
We&#x27;re going to use the unimaginative &lt;code&gt;unknown-1&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;typecheck-1-elaborate-lambda.png&quot; alt=&quot;Elaborating lambda \a -&amp;gt; a&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Then, when we elaborate the body of the lambda, we add &lt;code&gt;a == unknown-1&lt;&#x2F;code&gt; to the
environment, which makes typechecking the variable &lt;code&gt;a&lt;&#x2F;code&gt; straightforward, making
the type of the whole lambda &lt;code&gt;unknown-1 -&amp;gt; unknown-1&lt;&#x2F;code&gt; (the &lt;code&gt;-&amp;gt;&lt;&#x2F;code&gt; meaning &quot;a
function from a to b&quot;)&lt;&#x2F;p&gt;
&lt;h3 id=&quot;function-application&quot;&gt;Function application&lt;&#x2F;h3&gt;
&lt;h4 id=&quot;elaboration&quot;&gt;Elaboration&lt;&#x2F;h4&gt;
&lt;p&gt;What&#x27;s the use in functions if we can&#x27;t apply values to them? Given the &lt;code&gt;id&lt;&#x2F;code&gt; function has already been defined, let&#x27;s apply a value to it.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;typecheck-1-elaborate-application.png&quot; alt=&quot;Elaborating function application for id True&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Since the function type is &lt;code&gt;unknown-1 -&amp;gt; unknown-1&lt;&#x2F;code&gt;, we apply &lt;code&gt;Boolean&lt;&#x2F;code&gt; to it,
by creating a constraint between the first &lt;code&gt;unknown-1&lt;&#x2F;code&gt; and &lt;code&gt;Boolean&lt;&#x2F;code&gt;. Then the
return type from elaboration is the second half of the function type (also
&lt;code&gt;unknown-1&lt;&#x2F;code&gt;).&lt;&#x2F;p&gt;
&lt;p&gt;Surely it should be &lt;code&gt;Boolean&lt;&#x2F;code&gt; though right? No, but also, yes, but also, it&#x27;s
complicated...&lt;&#x2F;p&gt;
&lt;h4 id=&quot;unifying-our-new-constraint&quot;&gt;Unifying our new constraint&lt;&#x2F;h4&gt;
&lt;p&gt;Let&#x27;s look at the constraint we&#x27;ve generated during elaboration.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;typecheck-1-unify-application.png&quot; alt=&quot;Unifying function application&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;As we briefly mentioned earlier, we unify values to learn things about them. Because &lt;code&gt;unknown-1&lt;&#x2F;code&gt; is a unification variable, it represents an unknown thing that we are trying to find out more about.
Unifying it with &lt;code&gt;Boolean&lt;&#x2F;code&gt; tells us &lt;code&gt;unknown-1&lt;&#x2F;code&gt; is actually &lt;code&gt;Boolean&lt;&#x2F;code&gt;, and therefore we can substitute any instance of &lt;code&gt;unknown-1&lt;&#x2F;code&gt; for &lt;code&gt;Boolean&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;apply-the-substitution&quot;&gt;Apply the substitution&lt;&#x2F;h4&gt;
&lt;p&gt;We apply a substitution by changing all instances of one thing in a type for another. Applying a substitution to the result of elaboration gives us our final type,
&lt;code&gt;Boolean&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;typecheck-1-substitute-application.png&quot; alt=&quot;Substituting function application&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;🥚 Substitution 🥚&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Assuming we had no type errors in the previous step, then
subsitution is the process of taking all the things we have learned and
applying them to both to expression and to the other substitutions.&lt;&#x2F;p&gt;
&lt;p&gt;For example, if we have learned &lt;strong&gt;&quot;&lt;code&gt;a&lt;&#x2F;code&gt; is an &lt;code&gt;Integer&lt;&#x2F;code&gt;&quot;&lt;&#x2F;strong&gt; then we can change any &lt;code&gt;a&lt;&#x2F;code&gt;
values in our expression to &lt;code&gt;Integer&lt;&#x2F;code&gt;, and also change the substitution &lt;strong&gt;&quot;&lt;code&gt;c&lt;&#x2F;code&gt;
is a pair of &lt;code&gt;a&lt;&#x2F;code&gt; and &lt;code&gt;b&lt;&#x2F;code&gt;&quot;&lt;&#x2F;strong&gt; into &lt;strong&gt;&quot;&lt;code&gt;c&lt;&#x2F;code&gt; is a pair of &lt;code&gt;Integer&lt;&#x2F;code&gt; and &lt;code&gt;b&lt;&#x2F;code&gt;&quot;&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;h3 id=&quot;a-bigger-example&quot;&gt;A bigger example&lt;&#x2F;h3&gt;
&lt;p&gt;To see Hindley Milner typechecking in action, here is a slightly bigger
example. Here we&#x27;ll see how we can get full type inference without a type
annotation in sight.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;typecheck-1-elaborate-lambda-2.png&quot; alt=&quot;Elaborating \a -&amp;gt; if a then 1 else 2&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;We start by elaborating the function argument, &lt;code&gt;a&lt;&#x2F;code&gt;. We have no idea what it is,
so we create a unification variable, &lt;code&gt;unknown-1&lt;&#x2F;code&gt;, and use that instead.&lt;&#x2F;p&gt;
&lt;p&gt;Then we move onto the body of the lambda, an if expression. The predicate
expression is &lt;code&gt;a&lt;&#x2F;code&gt;. Fortunately, we already have the type for &lt;code&gt;a&lt;&#x2F;code&gt; in the type
environment - so we can grab it. We don&#x27;t know what &lt;code&gt;unknown-1&lt;&#x2F;code&gt; is yet, but we
have another piece of information on our side - the predicate for an &lt;code&gt;if&lt;&#x2F;code&gt; must
be a &lt;code&gt;Boolean&lt;&#x2F;code&gt;. We express this by creating a new constraint &lt;code&gt;unknown-1 == Boolean&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;then&lt;&#x2F;code&gt; and &lt;code&gt;else&lt;&#x2F;code&gt; expressions are both literals, so we can infer those
quite simply, making &lt;code&gt;Integer&lt;&#x2F;code&gt; the return type of the &lt;code&gt;if&lt;&#x2F;code&gt; expression. There is
also another constraint here, that both branches should have the same type -
&lt;code&gt;Integer == Integer&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;unifying-our-new-constraints&quot;&gt;Unifying our new constraints&lt;&#x2F;h4&gt;
&lt;p&gt;We have two contraints from elaboration.&lt;&#x2F;p&gt;
&lt;p&gt;First &lt;code&gt;Integer == Integer&lt;&#x2F;code&gt;, which teaches us nothing (but does type check).&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;typecheck-1-unify-lambda-2.png&quot; alt=&quot;Unifying big lambda&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Secondly &lt;code&gt;unknown-1 == Boolean&lt;&#x2F;code&gt; gives us a substitution.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;apply-the-substitution-1&quot;&gt;Apply the substitution&lt;&#x2F;h4&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;typecheck-1-substitute-lambda-2.png&quot; alt=&quot;Substituting big lambda&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;So, therefore, the type of &lt;code&gt;\a -&amp;gt; if a then 1 else 2&lt;&#x2F;code&gt; is &lt;code&gt;Boolean -&amp;gt; Integer&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;that-s-all-folks&quot;&gt;That&#x27;s all folks&lt;&#x2F;h3&gt;
&lt;p&gt;The writing that is, that is the end of the writing. There is a bunch more to
Hindley Milner typechecking, we&#x27;ve neatly sidestepped any complicated
polymorphism, however rest assured that it doesn&#x27;t make things &lt;em&gt;too&lt;&#x2F;em&gt; much
complicated which is nice. I might write about that in future, so given the
glacial pace of my writing, look forward to a sequel in 2024.&lt;&#x2F;p&gt;
&lt;p&gt;Make sense? If not, &lt;a href=&quot;&#x2F;contact.html&quot;&gt;get in touch&lt;&#x2F;a&gt;!&lt;&#x2F;p&gt;
&lt;p&gt;Further reading:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;gilmi.me&#x2F;blog&#x2F;tags&#x2F;giml&quot;&gt;giml&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;smunix.github.io&#x2F;dev.stephendiehl.com&#x2F;fun&#x2F;006_hindley_milner.html&quot;&gt;Write you a Haskell&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;danieljharvey&#x2F;mimsa&quot;&gt;mimsa&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>What the hell is a content addressed language</title>
          <pubDate>Tue, 28 Dec 2021 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/content-addressed-languages/</link>
          <guid>https://danieljharvey.github.io/content-addressed-languages/</guid>
          <description xml:base="https://danieljharvey.github.io/content-addressed-languages/">&lt;p&gt;Over the last year and a half I haven&#x27;t really written much stuff, because I
have been working on a small content-addressed programming language called
&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;danieljharvey&#x2F;mimsa&quot;&gt;mimsa&lt;&#x2F;a&gt;. I&#x27;m not really sure why I am doing this, other than that I watched a
&lt;a href=&quot;https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=frM7GhBERAs&quot;&gt;neat tutorial about how typecheckers worked&lt;&#x2F;a&gt;, and then got somewhat carried away.&lt;&#x2F;p&gt;
&lt;p&gt;I has sat down to write about my recent implementation of property testing in said
lang, then realised that before I can do that I should probably provide some
context.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;so-what-is-a-content-addressed-language&quot;&gt;So, what IS a content-addressed language?&lt;&#x2F;h3&gt;
&lt;p&gt;According to &lt;a href=&quot;https:&#x2F;&#x2F;www.unisonweb.org&#x2F;&quot;&gt;Unison&lt;&#x2F;a&gt;, the language that pioneered
this concept, this means &quot;code is immutable and identified by its content&quot;.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s take that apart a bit:&lt;&#x2F;p&gt;
&lt;p&gt;Say I have a function for adding two numbers:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;add &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int -&amp;gt; Int -&amp;gt; Int
&lt;&#x2F;span&gt;&lt;span&gt;add a b = a + b
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Instead of just dumping this in a text file, I would &quot;save&quot; this function. In
the &lt;code&gt;mimsa&lt;&#x2F;code&gt; repl, I would do this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;:&amp;gt; :bind add = \a -&amp;gt; \b -&amp;gt; a + b
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Bound&lt;&#x2F;span&gt;&lt;span&gt; add.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This has saved the expression &lt;code&gt;\a -&amp;gt; \b -&amp;gt; a + b&lt;&#x2F;code&gt; in the project, and pointed &lt;code&gt;add&lt;&#x2F;code&gt;
to refer to said expression.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s use our new function:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;:&amp;gt; add &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1 3
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4 &lt;&#x2F;span&gt;&lt;span&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Int
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;:&amp;gt; add &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2 2
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4 &lt;&#x2F;span&gt;&lt;span&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Int
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Hopefully, no surprises there.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s make a new function that uses &lt;code&gt;add&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;:&amp;gt; bind add3 = add &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This has saved the expression &lt;code&gt;add 3&lt;&#x2F;code&gt; in the DB, and pointed &lt;code&gt;add3&lt;&#x2F;code&gt; at it. More
importantly, it has also remembered exactly which &lt;code&gt;add&lt;&#x2F;code&gt; function it used (more
on this shortly).&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;Like &lt;code&gt;haskell&lt;&#x2F;code&gt; or &lt;code&gt;elm&lt;&#x2F;code&gt;, &lt;code&gt;mimsa&lt;&#x2F;code&gt; functions only take one argument, so
applying &lt;code&gt;3&lt;&#x2F;code&gt; to &lt;code&gt;add&lt;&#x2F;code&gt; (which has type &lt;code&gt;Int -&amp;gt; Int -&amp;gt; Int&lt;&#x2F;code&gt;) returns a new
function with type &lt;code&gt;Int -&amp;gt; Int&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;Again, let&#x27;s test it and check for surprises:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;:&amp;gt; add3 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5 &lt;&#x2F;span&gt;&lt;span&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Int
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;:&amp;gt; add3 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3 &lt;&#x2F;span&gt;&lt;span&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Int
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;All seems well.&lt;&#x2F;p&gt;
&lt;p&gt;Now what if we decide to make &lt;code&gt;add&lt;&#x2F;code&gt; evil? We are allowed to rebind it, so let&#x27;s
do that:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;:&amp;gt; bind add = \a -&amp;gt; \b -&amp;gt; a + b + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Updated&lt;&#x2F;span&gt;&lt;span&gt; binding &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;of&lt;&#x2F;span&gt;&lt;span&gt; add.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now when we use &lt;code&gt;add&lt;&#x2F;code&gt; we&#x27;ll get wonky answers as expected.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;:&amp;gt; add &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2 2
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5 &lt;&#x2F;span&gt;&lt;span&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Int
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;:&amp;gt; add &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0 0
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1 &lt;&#x2F;span&gt;&lt;span&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Int
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;However, look what happens when we use &lt;code&gt;add3&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;:&amp;gt; add3 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4 &lt;&#x2F;span&gt;&lt;span&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Int
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We still get the original correct result.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;so-what-has-happened-here&quot;&gt;So, what has happened here?&lt;&#x2F;h3&gt;
&lt;p&gt;The answer is in how &lt;code&gt;mimsa&lt;&#x2F;code&gt; expressions are stored. When we stored &lt;code&gt;add&lt;&#x2F;code&gt;, we
stored not only the expression &lt;code&gt;\a -&amp;gt; \b -&amp;gt; a + b&lt;&#x2F;code&gt; , but the fact that
&lt;code&gt;add&lt;&#x2F;code&gt; has no dependencies. Internally, it would look a bit like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- | Distinct type for our hashes, a wrapper around Text
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;newtype &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;ExpressionHash
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;ExpressionHash Text
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- | Simplified version of our StoreExpression type
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;StoreExpression &lt;&#x2F;span&gt;&lt;span&gt;= 
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;StoreExpression&lt;&#x2F;span&gt;&lt;span&gt; {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- | the code for this expression
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;expression &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Text&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- | a map from the names of dependencies we&amp;#39;ve used to their hashes
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;dependencies &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Map Name ExpressionHash
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- how our `add` expression is stored internally
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;addExpression &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: StoreExpression
&lt;&#x2F;span&gt;&lt;span&gt;addExpression = 
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;StoreExpression
&lt;&#x2F;span&gt;&lt;span&gt;    { expression = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\\&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;a -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\\&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;b -&amp;gt; a + b&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      dependencies = mempty
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Once we&#x27;ve made the &lt;code&gt;addExpression&lt;&#x2F;code&gt;, we can create a hash of it, which
we&#x27;ll pretend looks like &lt;code&gt;&quot;abc123&quot;&lt;&#x2F;code&gt; in hexadecimal. Then when we &lt;code&gt;bind&lt;&#x2F;code&gt; the
name &lt;code&gt;add&lt;&#x2F;code&gt; to it, we can store it in the project:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- | Simplified version of our Project type
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Project
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Project 
&lt;&#x2F;span&gt;&lt;span&gt;      { &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- | map from names like `add` to hashes
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;bindings &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Map Name ExpressionHash&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- | map from hashes to expressions
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;store &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Map ExpressionHash StoreExpression
&lt;&#x2F;span&gt;&lt;span&gt;      }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;projectWithAdd &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Project
&lt;&#x2F;span&gt;&lt;span&gt;projectWithAdd = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Project
&lt;&#x2F;span&gt;&lt;span&gt;  { bindings: fromList [(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;add&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;abc123&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)],
&lt;&#x2F;span&gt;&lt;span&gt;    store: fromList [(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;abc123&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, addExpression)]
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now when we create &lt;code&gt;add3&lt;&#x2F;code&gt;, the following happens:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;add3Expression &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: StoreExpression
&lt;&#x2F;span&gt;&lt;span&gt;add3Expression =
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;StoreExpression
&lt;&#x2F;span&gt;&lt;span&gt;    { expression = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;add 3&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      dependencies = fromList [(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;add&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;abc123&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)]
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;When evaluating &lt;code&gt;add 3&lt;&#x2F;code&gt;, &lt;code&gt;mimsa&lt;&#x2F;code&gt; has looked in the project for something called
&lt;code&gt;add&lt;&#x2F;code&gt;, found it, and then saved that it is needed in this expression. Because
we refer to &lt;code&gt;add&lt;&#x2F;code&gt; by a hash of it&#x27;s content, this is what makes it &quot;content
addressed&quot;.&lt;&#x2F;p&gt;
&lt;p&gt;Assuming &lt;code&gt;add3&lt;&#x2F;code&gt; has a hash of &lt;code&gt;&quot;def456&quot;&lt;&#x2F;code&gt;, our project would look like this now:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;projectWithAddAndAdd3 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Project
&lt;&#x2F;span&gt;&lt;span&gt;projectWithAddAndAdd3 = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Project
&lt;&#x2F;span&gt;&lt;span&gt;  { bindings: fromList [
&lt;&#x2F;span&gt;&lt;span&gt;      (&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;add&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;abc123&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;), 
&lt;&#x2F;span&gt;&lt;span&gt;      (&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;add3&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;def456&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    ],
&lt;&#x2F;span&gt;&lt;span&gt;    store: fromList [
&lt;&#x2F;span&gt;&lt;span&gt;      (&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;abc123&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, addExpression),
&lt;&#x2F;span&gt;&lt;span&gt;      (&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;def456&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, add3Expression)
&lt;&#x2F;span&gt;&lt;span&gt;    ] 
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now, when we come to update &lt;code&gt;add&lt;&#x2F;code&gt; and make it evil, we create the following:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;evilAddExpression &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: StoreExpression
&lt;&#x2F;span&gt;&lt;span&gt;evilAddExpression = 
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;StoreExpression
&lt;&#x2F;span&gt;&lt;span&gt;    { expression =  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\\&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;a -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\\&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;b -&amp;gt; a + b + 1&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;      dependencies = mempty
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Assuming it has a hash of &lt;code&gt;&quot;ghi789&quot;&lt;&#x2F;code&gt;, then the project looks like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;projectWithAddAndAdd3 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Project
&lt;&#x2F;span&gt;&lt;span&gt;projectWithAddAndAdd3 = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Project
&lt;&#x2F;span&gt;&lt;span&gt;  { bindings: fromList [
&lt;&#x2F;span&gt;&lt;span&gt;      (&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;add&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;ghi789&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;), 
&lt;&#x2F;span&gt;&lt;span&gt;      (&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;add3&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;def456&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    ],
&lt;&#x2F;span&gt;&lt;span&gt;    store: fromList [
&lt;&#x2F;span&gt;&lt;span&gt;      (&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;abc123&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, addExpression),
&lt;&#x2F;span&gt;&lt;span&gt;      (&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;def456&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, add3Expression),
&lt;&#x2F;span&gt;&lt;span&gt;      (&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;ghi789&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, evilAddExpression)
&lt;&#x2F;span&gt;&lt;span&gt;    ] 
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The project has been updated, but note we haven&#x27;t deleted any items from the
&lt;code&gt;store&lt;&#x2F;code&gt;. All that has been updated in the project is the binding for &lt;code&gt;add&lt;&#x2F;code&gt;.
This means any new uses of &lt;code&gt;add&lt;&#x2F;code&gt; will use the new broken
function, but &lt;code&gt;add3&lt;&#x2F;code&gt; is completely unaffected. If you did want &lt;code&gt;add3&lt;&#x2F;code&gt; to adopt
the new terrible behaviour, it would be a case of binding it again, where it
would use the broken &lt;code&gt;add&lt;&#x2F;code&gt; function from the project.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;why-would-you-do-this&quot;&gt;Why would you do this?&lt;&#x2F;h3&gt;
&lt;h4 id=&quot;maximum-cachability&quot;&gt;Maximum cachability&lt;&#x2F;h4&gt;
&lt;p&gt;Because any of the expressions we can created don&#x27;t change once created, they
don&#x27;t need reparsing or typechecking (or transpiling to JS, etc) over and over. &lt;code&gt;add&lt;&#x2F;code&gt; will have the type
&lt;code&gt;Int -&amp;gt; Int -&amp;gt; Int&lt;&#x2F;code&gt; forever, meaning that the idea of a slow build is kinda
removed.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;no-namespacing-issues&quot;&gt;No namespacing issues&lt;&#x2F;h4&gt;
&lt;p&gt;In many languages conflicting names of packages or imports can cause issues. We
might want to use function A from package version 1.1 but function B from
package version 1.3. Most package managers won&#x27;t let you do this (or they let
it sort of happens sometimes, but weird stuff happens with globals interacting, I&#x27;m looking at you React). Expressions
in a content-addressed language refer to each other by hashes rather than names,
so function A and function B will have no idea about one another unless they
depend on one another somehow, and everything works great.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;granular-code-sharing&quot;&gt;Granular code sharing&lt;&#x2F;h4&gt;
&lt;p&gt;Because of the above, the idea of the package as the unit of shared
functionality is somewhat obselete. &lt;a href=&quot;https:&#x2F;&#x2F;share.unison-lang.org&#x2F;&quot;&gt;Unison Share&lt;&#x2F;a&gt;
demonstrates what it could be like if we shared smaller units instead.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;tests-don-t-need-running-over-and-over&quot;&gt;Tests don&#x27;t need running over and over&lt;&#x2F;h4&gt;
&lt;p&gt;If we test that &lt;code&gt;add 1 3 == 4&lt;&#x2F;code&gt;, and I know that &lt;code&gt;add&lt;&#x2F;code&gt; is not going to change,
then I can keep this test result around and don&#x27;t need to run it again. When
&lt;code&gt;add&lt;&#x2F;code&gt; is rebound, we can make a copy of the test and run it on the new
implementation to see if the same test still passes. Property tests, it turns
out, aren&#x27;t quite so simple, but I&#x27;ll come to that in a later post.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;doesn-t-this-mean-that-most-developer-tools-that-are-built-assuming-programming-means-text-files-and-diffs-to-said-files-are-all-unusable-here&quot;&gt;Doesn&#x27;t this mean that most developer tools that are built assuming programming means text files and diffs to said files are all unusable here?&lt;&#x2F;h3&gt;
&lt;p&gt;Ahem.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;that-s-all-the-words&quot;&gt;That&#x27;s all the words&lt;&#x2F;h3&gt;
&lt;p&gt;That&#x27;s some context of what content-addressed languages are, at least, my weird
understanding of them anyway. The actual implementation in &lt;code&gt;mimsa&lt;&#x2F;code&gt; is more
complicated than above (for instance, we store the raw AST rather than the
text syntax so we can change the syntax without breaking the expressions, and
store the history of name bindings rather than just the newest one) but
hopefully it gives you a clue about what is going on under the hood.&lt;&#x2F;p&gt;
&lt;p&gt;Make sense? If not, &lt;a href=&quot;&#x2F;contact.html&quot;&gt;get in touch&lt;&#x2F;a&gt;!&lt;&#x2F;p&gt;
&lt;p&gt;Further reading:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.unisonweb.org&#x2F;docs&#x2F;tour&quot;&gt;Unison - the big idea&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;danieljharvey&#x2F;mimsa&quot;&gt;mimsa&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Property testing a content-addressed language</title>
          <pubDate>Tue, 28 Dec 2021 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/property-testing-mimsa/</link>
          <guid>https://danieljharvey.github.io/property-testing-mimsa/</guid>
          <description xml:base="https://danieljharvey.github.io/property-testing-mimsa/">&lt;p&gt;So in the &lt;a href=&quot;&#x2F;posts&#x2F;2021-12-28-content-addressed-languages.html&quot;&gt;last post&lt;&#x2F;a&gt; I quickly introduced the project I&#x27;ve been working on for a
while called &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;danieljharvey&#x2F;mimsa&quot;&gt;mimsa&lt;&#x2F;a&gt;. The reason I did this was so that I could write this post
about recently adding property tests to the language, and what was good&#x2F;bad
about that, so without further ado here are many words:&lt;&#x2F;p&gt;
&lt;h3 id=&quot;tests-in-mimsa&quot;&gt;Tests in mimsa&lt;&#x2F;h3&gt;
&lt;p&gt;So, for context, unit tests work as follows in &lt;code&gt;mimsa&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;We create a (broken) &lt;code&gt;add&lt;&#x2F;code&gt; function:&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;:&amp;gt; :bind add = \a -&amp;gt; \b -&amp;gt; a + b + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Bound&lt;&#x2F;span&gt;&lt;span&gt; add.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ul&gt;
&lt;li&gt;We create a unit tests that confirms that &lt;code&gt;add&lt;&#x2F;code&gt; is indeed broken:&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;:&amp;gt; :addTest &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;2 + 2 == 4&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; add &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2 2 &lt;&#x2F;span&gt;&lt;span&gt;== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--- FAIL --- 2 + 2 == 4
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ul&gt;
&lt;li&gt;We update &lt;code&gt;add&lt;&#x2F;code&gt; to a working implementation, and now the test passes:&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;:&amp;gt; :bind add = \a -&amp;gt; \b -&amp;gt; a + b
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Updated&lt;&#x2F;span&gt;&lt;span&gt; binding &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;of&lt;&#x2F;span&gt;&lt;span&gt; add.
&lt;&#x2F;span&gt;&lt;span&gt;+++ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;PASS &lt;&#x2F;span&gt;&lt;span&gt;+++ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2 &lt;&#x2F;span&gt;&lt;span&gt;+ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2 &lt;&#x2F;span&gt;&lt;span&gt;== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4 
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;What is really neat is that what looks like &lt;strong&gt;one test being run twice&lt;&#x2F;strong&gt; is
actually &lt;strong&gt;two tests being run once each&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Why? The reason is immutability - once the first test is
created it can&#x27;t be changed. It also doesn&#x27;t need re-running - it always has
the same arguments, and it&#x27;s only dependency, the original &lt;code&gt;add&lt;&#x2F;code&gt; function,
never changes, so the test stands as a static assertion of the behaviour of
said function.&lt;&#x2F;p&gt;
&lt;p&gt;And where does the second test come from? When we bind a new version of &lt;code&gt;add&lt;&#x2F;code&gt;, it stands to reason we might
want to run the same tests on it, therefore a new copy of any test is created
that uses the new &lt;code&gt;add&lt;&#x2F;code&gt; function as it&#x27;s dependency.&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;A really nice property of testing in this way is that we could also make it
run in reverse. If I add another test to the new &lt;code&gt;add&lt;&#x2F;code&gt; function there is no
reason not to auto-create versions of it for the old &lt;code&gt;add&lt;&#x2F;code&gt; bindings too. This
is super helpful when finding a new regression, as we can write a new test for
old code to find out when said regression occurred, a sort of super-powered
git bisect. I say &lt;em&gt;could&lt;&#x2F;em&gt; here because I have not done this yet because I am
lazy.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;h3 id=&quot;property-testing-a-recap&quot;&gt;Property testing, a recap&lt;&#x2F;h3&gt;
&lt;p&gt;Property testing is a kind of testing where instead of confirming our code
works against known good values we confirms that it obeys certain rules.&lt;&#x2F;p&gt;
&lt;p&gt;A non-property test could be &lt;code&gt;add 2 2 == 4&lt;&#x2F;code&gt; - we know this should be the answer
because we have checked on a calculator that &lt;code&gt;2 + 2&lt;&#x2F;code&gt; does indeed equal &lt;code&gt;4&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;A property test might be &lt;code&gt;add 0 a == a&lt;&#x2F;code&gt;, given any &lt;code&gt;a&lt;&#x2F;code&gt;. When this is run by
&lt;a href=&quot;https:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;QuickCheck&quot;&gt;quickcheck&lt;&#x2F;a&gt; or
&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;dubzzz&#x2F;fast-check&quot;&gt;fast-check&lt;&#x2F;a&gt; the property testing
framework will run the test with loads and loads of different values for &lt;code&gt;a&lt;&#x2F;code&gt;,
and return all the ones that break it (in this case, none of them). If the
tests all pass then we know the property &lt;strong&gt;adding zero to a number returns the same
number&lt;&#x2F;strong&gt; holds.&lt;&#x2F;p&gt;
&lt;p&gt;Property testing is very useful for finding edge cases in functions, and used
well, can be a lot more thorough than unit tests, for not a lot more effort.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;the-current-implementation&quot;&gt;The current implementation&lt;&#x2F;h3&gt;
&lt;p&gt;For a unit test to be valid in &lt;code&gt;mimsa&lt;&#x2F;code&gt;, it must have the type &lt;code&gt;Boolean&lt;&#x2F;code&gt; (There
is a lot wrong with this, as it doesn&#x27;t let you see what went wrong, but we
can&#x27;t just go around making things &quot;good&quot; all the time).&lt;&#x2F;p&gt;
&lt;p&gt;Therefore this are the simplest possible tests:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;:&amp;gt; :addTest &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;True
&lt;&#x2F;span&gt;&lt;span&gt;+++ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;PASS &lt;&#x2F;span&gt;&lt;span&gt;+++ true
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;:&amp;gt; :addTest &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;false&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;False
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--- FAIL --- false
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To make a property test, we provide an expression with the type &lt;code&gt;something -&amp;gt; Boolean&lt;&#x2F;code&gt;, like:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;:&amp;gt; :addTest &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;or true is always true&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; \bool -&amp;gt; or bool &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;True
&lt;&#x2F;span&gt;&lt;span&gt;+++ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;PASS &lt;&#x2F;span&gt;&lt;span&gt;+++ or true is always true
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;What&#x27;s happened here?&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;mimsa&lt;&#x2F;code&gt; typechecker has inferred that &lt;code&gt;bool&lt;&#x2F;code&gt; must be a &lt;code&gt;Boolean&lt;&#x2F;code&gt;, generated
a big bucket of booleans, and then checked that each time &lt;code&gt;or bool True == True&lt;&#x2F;code&gt;. This returns &lt;code&gt;True&lt;&#x2F;code&gt; everytime so the test passes.&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;The &lt;code&gt;or&lt;&#x2F;code&gt; function has type &lt;code&gt;Boolean -&amp;gt; Boolean -&amp;gt; Boolean&lt;&#x2F;code&gt; and looks something like &lt;code&gt;\a -&amp;gt; \b -&amp;gt; if a then True else b&lt;&#x2F;code&gt;. It is
similar to the &lt;code&gt;||&lt;&#x2F;code&gt; operator in most programming languages.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;Let&#x27;s try another:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;:&amp;gt; :addTest &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;and true is always true&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; \bool -&amp;gt; and bool &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;True
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--- FAIL --- and true is always true
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Failing&lt;&#x2F;span&gt;&lt;span&gt; inputs:
&lt;&#x2F;span&gt;&lt;span&gt; - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;False
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This property test has failed, and it&#x27;s telling us that when you pass &lt;code&gt;False&lt;&#x2F;code&gt;
to the test function, it does not return &lt;code&gt;True&lt;&#x2F;code&gt;. The &lt;code&gt;and&lt;&#x2F;code&gt; function here is
equivalent to &lt;code&gt;&amp;amp;&amp;amp;&lt;&#x2F;code&gt;, so this makes sense, as &lt;code&gt;False &amp;amp;&amp;amp; True&lt;&#x2F;code&gt; equals &lt;code&gt;False&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;As the input is a &lt;code&gt;Boolean&lt;&#x2F;code&gt; there are only two inputs, and the &lt;code&gt;mimsa&lt;&#x2F;code&gt; property
test generates &lt;code&gt;1000&lt;&#x2F;code&gt; sample inputs, it&#x27;s fairly likely we&#x27;ll get both a &lt;code&gt;False&lt;&#x2F;code&gt;
and a &lt;code&gt;True&lt;&#x2F;code&gt; input.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;breaking-it&quot;&gt;Breaking it&lt;&#x2F;h3&gt;
&lt;p&gt;However, it&#x27;s not too difficult to make a less definitive test:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;:&amp;gt; :bind flakyTest = 
&lt;&#x2F;span&gt;&lt;span&gt;  \val -&amp;gt; match val with 
&lt;&#x2F;span&gt;&lt;span&gt;    (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just True&lt;&#x2F;span&gt;&lt;span&gt;)))) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;False 
&lt;&#x2F;span&gt;&lt;span&gt;  | _ -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;True
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Bound&lt;&#x2F;span&gt;&lt;span&gt; flakyTest.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;:&amp;gt; :addTest &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Flaky&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; flakyTest
&lt;&#x2F;span&gt;&lt;span&gt;+++ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;PASS &lt;&#x2F;span&gt;&lt;span&gt;+++ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Flaky
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;:&amp;gt; :addTest &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Flaky 2&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; flakyTest
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--- FAIL --- Flaky 2
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Failing&lt;&#x2F;span&gt;&lt;span&gt; inputs:
&lt;&#x2F;span&gt;&lt;span&gt; - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just True&lt;&#x2F;span&gt;&lt;span&gt;)))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Because the input type here would be &lt;code&gt;Maybe (Maybe (Maybe (Maybe Boolean)))&lt;&#x2F;code&gt; there are a lot of potential inputs,
and it&#x27;s quite possible that &lt;code&gt;Just (Just (Just (Just True)))&lt;&#x2F;code&gt; is not amongst
the generated values.&lt;&#x2F;p&gt;
&lt;p&gt;Another type of flaky test can be generated like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;:&amp;gt; :addTest &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;flaky string&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; \str -&amp;gt; not (str == &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;dog&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;+++ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;PASS &lt;&#x2F;span&gt;&lt;span&gt;+++ flaky string
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It passes every time I have tried it, although as a statement it makes no
sense - &lt;em&gt;there is no string that equals &quot;dog&quot;&lt;&#x2F;em&gt;. This kind of test is hard just
because there are so many potential strings out there that a total search of
the space is close to impossible.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;the-problems-solutions&quot;&gt;The problems &#x2F; solutions&lt;&#x2F;h3&gt;
&lt;h4 id=&quot;simple-tests-could-be-non-deterministic-and-aren-t&quot;&gt;Simple tests could be non-deterministic and aren&#x27;t:&lt;&#x2F;h4&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;\bool -&amp;gt; and bool &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;True
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This could be two unit tests that are run once and never again, however as a
property test it must be run over and over. This is the least of the problems
but worth mentioning.&lt;&#x2F;p&gt;
&lt;h4 id=&quot;complex-finite-tests-become-flaky&quot;&gt;Complex finite tests become flaky&lt;&#x2F;h4&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;\val -&amp;gt; match val with
&lt;&#x2F;span&gt;&lt;span&gt;    (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just True&lt;&#x2F;span&gt;&lt;span&gt;)))) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;False
&lt;&#x2F;span&gt;&lt;span&gt;  | _ -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;True
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Given that a &lt;code&gt;Maybe&lt;&#x2F;code&gt; is either &lt;code&gt;Nothing&lt;&#x2F;code&gt; or the value inside, and &lt;code&gt;Boolean&lt;&#x2F;code&gt; can
be 1 of 2 values, there are &lt;code&gt;1 + 1 + 1 + 1 + 2 == 6&lt;&#x2F;code&gt; potential inputs here.
It doesn&#x27;t seem out of the question to auto-generate all the possible values
here and run tests against them all. This would ensure a) the same results each
time and b) that we could cache the results once and never run them again.&lt;&#x2F;p&gt;
&lt;p&gt;However, if the input type contains an &lt;code&gt;Int&lt;&#x2F;code&gt;, &lt;code&gt;String&lt;&#x2F;code&gt;, &lt;code&gt;Array&lt;&#x2F;code&gt; or any recursive
type then generating a complete set of inputs is no longer practical.&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;A recursive type is one that contains itself. For instance, a linked list is
defined in &lt;code&gt;mimsa&lt;&#x2F;code&gt; as &lt;code&gt;type List a = Nil | Cons a (List a)&lt;&#x2F;code&gt;. The second &lt;code&gt;List a&lt;&#x2F;code&gt; argument to &lt;code&gt;Cons&lt;&#x2F;code&gt; means &quot;and another list&quot; so this datatype can grow and
grow and grow.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;h4 id=&quot;string-and-number-spaces-just-too-huge&quot;&gt;String and number spaces just too huge&lt;&#x2F;h4&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;\str -&amp;gt; not (str == &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;dog&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Due to the way the &lt;code&gt;mimsa&lt;&#x2F;code&gt; web client works, we can&#x27;t really fall back to brute
forcing an absolute shit ton of inputs for &lt;code&gt;String&lt;&#x2F;code&gt; and &lt;code&gt;Int&lt;&#x2F;code&gt; inputs. However,
we have access to the AST when running the test. Would it be so terrible to add all of the string and number literals found in the
expression into the test values? For instance, if we used the input &lt;code&gt;&quot;dog&quot;&lt;&#x2F;code&gt;
above we&#x27;d find the breaking case immediately.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;no-more-words&quot;&gt;No more words&lt;&#x2F;h3&gt;
&lt;p&gt;That is the end of the words. It&#x27;s a bit of a braindump, and pretty much only
for my own benefit, but I hope it is perhaps vaguely interesting. Who knows?&lt;&#x2F;p&gt;
&lt;p&gt;Make sense? If not, &lt;a href=&quot;&#x2F;contact.html&quot;&gt;get in touch&lt;&#x2F;a&gt;!&lt;&#x2F;p&gt;
&lt;p&gt;Further reading:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;QuickCheck&quot;&gt;quickcheck&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;dubzzz&#x2F;fast-check&quot;&gt;fast-check&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;danieljharvey&#x2F;mimsa&quot;&gt;mimsa&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Newtypes for Cheap</title>
          <pubDate>Sat, 28 Mar 2020 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/tags-and-shit/</link>
          <guid>https://danieljharvey.github.io/tags-and-shit/</guid>
          <description xml:base="https://danieljharvey.github.io/tags-and-shit/">&lt;p&gt;So previously we&#x27;ve spoken about the idea of a &lt;code&gt;newtype&lt;&#x2F;code&gt; in Haskell. A
&lt;code&gt;newtype&lt;&#x2F;code&gt; is a wrapper you can put around a value to distinguish it from other
items. It&#x27;s common to give value that has it&#x27;s own domain meaning it&#x27;s own
&lt;code&gt;newtype&lt;&#x2F;code&gt;, for instance:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;newtype &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Firstname &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Firstname&lt;&#x2F;span&gt;&lt;span&gt; { getFirstname :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt; }
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Eq&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Ord&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Show&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;newtype &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Surname &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Surname&lt;&#x2F;span&gt;&lt;span&gt; { getSurname :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt; }
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Eq&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Ord&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Show&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We create values as such:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;first &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Firstname
&lt;&#x2F;span&gt;&lt;span&gt;first = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Firstname &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Bobby&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;last &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Surname
&lt;&#x2F;span&gt;&lt;span&gt;last = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Surname &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Davehead&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And unwrap them to use like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;fullName &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Firstname -&amp;gt; Surname -&amp;gt; String
&lt;&#x2F;span&gt;&lt;span&gt;fullName first&amp;#39; last&amp;#39; = getFirstname first&amp;#39; &amp;lt;&amp;gt; &amp;quot; &amp;quot; &amp;lt;&amp;gt; getSurname last&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- fullName first last == &amp;quot;Bobby Davehead&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This would stop different bits of names, that would usually be stored as
strings, getting mixed up. It&#x27;s neat. What&#x27;s really nice about it though, as although
these look like records with a single item inside, and in code we treat them as
such, as runtime that shit gets erased and really it&#x27;s just a &lt;code&gt;String&lt;&#x2F;code&gt; being
passed around for &lt;code&gt;#performance&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;They have two benefits:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Firstly, if you are a goof like me, you get this wrong all the time and put
arguments in the wrong order and generally make a mess of things from
10x-ing all the time, and this makes such terrible errors a little trickier to
make.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Secondly, and much more usefully, it allows us to practice Alexis King&#x27;s
&lt;a href=&quot;https:&#x2F;&#x2F;lexi-lambda.github.io&#x2F;blog&#x2F;2019&#x2F;11&#x2F;05&#x2F;parse-don-t-validate&#x2F;&quot;&gt;Parse, don&#x27;t
validate&lt;&#x2F;a&gt;
pattern. It&#x27;s well worth reading in full, but the very tl;dr is - if I check a
&lt;code&gt;String&lt;&#x2F;code&gt; to see if it&#x27;s a valid name, I should return a type that tells me
that fact, so I a) don&#x27;t have to check again and b) don&#x27;t mix it up with the
unchecked version.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Anyway. It all sounds like good stuff, and I have been thinking about the same
patterns in Typescript as that&#x27;s a place I often find myself these days.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;prior-art&quot;&gt;Prior art&lt;&#x2F;h3&gt;
&lt;p&gt;Now a good place to start when trying to reproduce any Haskell &#x2F; Purescript
concept in Typescript is to crack open &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;gcanti&quot;&gt;gcanti&#x27;s
Github&lt;&#x2F;a&gt; where, as usual, he does not disappoint:
&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;gcanti&#x2F;newtype-ts&quot;&gt;newtype-ts&lt;&#x2F;a&gt; is a pretty full featured
library for this built on top of his &lt;code&gt;monocle-ts&lt;&#x2F;code&gt; lens library.&lt;&#x2F;p&gt;
&lt;p&gt;Now this is all very well, but does sort of require everybody working on your
codebase to buy in (especially as it requires you to use &lt;code&gt;modify&lt;&#x2F;code&gt; from
&lt;code&gt;monocle-ts&lt;&#x2F;code&gt; to change the values inside, and to explicitly &lt;code&gt;wrap&lt;&#x2F;code&gt; and &lt;code&gt;unwrap&lt;&#x2F;code&gt;
them). If everybody is into this, go for it, I am a big fan of the &lt;code&gt;fp-ts&lt;&#x2F;code&gt;
stack and swear by &lt;code&gt;io-ts&lt;&#x2F;code&gt; at the moment. However, I often need something cheap and hacky, that doesn&#x27;t leak complexity throughout the codebase.&lt;&#x2F;p&gt;
&lt;p&gt;Fortunately, good man and Habito colleague Mateusz (who I cannot find any links to on
the internet, somebody please help me remedy this), came up with a neat way of
tagging types for cheap, which I&#x27;d like to share today.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;the-10x-solution&quot;&gt;The 10x solution&lt;&#x2F;h3&gt;
&lt;p&gt;Here is the whole library:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;typescript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-typescript &quot;&gt;&lt;code class=&quot;language-typescript&quot; data-lang=&quot;typescript&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span&gt;Nominal&amp;lt;T&amp;gt; = {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;readonly &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;symbol&lt;&#x2F;span&gt;&lt;span&gt;: T
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;export type &lt;&#x2F;span&gt;&lt;span&gt;Tagged&amp;lt;Tag &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span&gt;string, A&amp;gt; = A &amp;amp; Nominal&amp;lt;Tag&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This means we can make unique tagged versions of regular datatypes like so:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;typescript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-typescript &quot;&gt;&lt;code class=&quot;language-typescript&quot; data-lang=&quot;typescript&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span&gt;Firstname = Tagged&amp;lt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Firstname&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, string&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span&gt;Surname = Tagged&amp;lt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Surname&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, string&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span&gt;Age = Tagged&amp;lt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Age&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, number&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The great thing is that when the Javascript is generated from this, we just
end up with plain &lt;code&gt;string&lt;&#x2F;code&gt; and &lt;code&gt;number&lt;&#x2F;code&gt; values.&lt;&#x2F;p&gt;
&lt;p&gt;Now, let&#x27;s see what these buy us:&lt;&#x2F;p&gt;
&lt;h3 id=&quot;stopping-idiots-like-me-mixing-stuff-up&quot;&gt;Stopping idiots like me mixing stuff up&lt;&#x2F;h3&gt;
&lt;p&gt;The most basic use is just tagging our types with &lt;code&gt;as&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;typescript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-typescript &quot;&gt;&lt;code class=&quot;language-typescript&quot; data-lang=&quot;typescript&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;first &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Bobby&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;as &lt;&#x2F;span&gt;&lt;span&gt;Firstname
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;surname &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Davehead&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;as &lt;&#x2F;span&gt;&lt;span&gt;Surname
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;functionThatNeedsSpecificallyFirstname &lt;&#x2F;span&gt;&lt;span&gt;= (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;firstname&lt;&#x2F;span&gt;&lt;span&gt;: Firstname): string &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    `&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Hello, person whose first name is very much ${&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;firstname&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;}&lt;&#x2F;span&gt;&lt;span&gt;`
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now, we can make sure only the right tagged value:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;typescript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-typescript &quot;&gt;&lt;code class=&quot;language-typescript&quot; data-lang=&quot;typescript&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;functionThatNeedsSpecificallyFirstname&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;first&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; ok!
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;functionThatNeedsSpecificallyFirstname&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;surname&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; type error!
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;When you have a function like &lt;code&gt;apiCall(url: string, token: string, accountId: string) =&amp;gt; ...&lt;&#x2F;code&gt; this really helps to stop you messing stupid things up.&lt;&#x2F;p&gt;
&lt;p&gt;Now, how about that parsing stuff mentioned earlier?&lt;&#x2F;p&gt;
&lt;h3 id=&quot;parse-don-t-validate&quot;&gt;Parse, don&#x27;t validate&lt;&#x2F;h3&gt;
&lt;p&gt;So, we can also use this tactic to tag data that we have validated, so that we
know certain properties about it have been validated.&lt;&#x2F;p&gt;
&lt;p&gt;Say we have a &lt;code&gt;Person&lt;&#x2F;code&gt; type, because I lack imagination:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;typescript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-typescript &quot;&gt;&lt;code class=&quot;language-typescript&quot; data-lang=&quot;typescript&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;interface &lt;&#x2F;span&gt;&lt;span&gt;Person {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;firstname&lt;&#x2F;span&gt;&lt;span&gt;: string
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;surname&lt;&#x2F;span&gt;&lt;span&gt;: string
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;age&lt;&#x2F;span&gt;&lt;span&gt;: number
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now, we want to check a few properties about these things to make sure they&#x27;re
not terrible.&lt;&#x2F;p&gt;
&lt;p&gt;The naive version of this looks something like:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;typescript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-typescript &quot;&gt;&lt;code class=&quot;language-typescript&quot; data-lang=&quot;typescript&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;isValidPerson &lt;&#x2F;span&gt;&lt;span&gt;= (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;person&lt;&#x2F;span&gt;&lt;span&gt;: Person): boolean &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; 
&lt;&#x2F;span&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;person&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;firstname&lt;&#x2F;span&gt;&lt;span&gt;.length &amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0 &lt;&#x2F;span&gt;&lt;span&gt;&amp;amp;&amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;person&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;firstname&lt;&#x2F;span&gt;&lt;span&gt;.length &amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;100&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;amp;&amp;amp; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;person&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;surname&lt;&#x2F;span&gt;&lt;span&gt;.length &amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0 &lt;&#x2F;span&gt;&lt;span&gt;&amp;amp;&amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;person&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;surname&lt;&#x2F;span&gt;&lt;span&gt;.length &amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;50&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;amp;&amp;amp; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;person&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;age &lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0 &lt;&#x2F;span&gt;&lt;span&gt;&amp;amp;&amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;person&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;age &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;120&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Before using &lt;code&gt;Person&lt;&#x2F;code&gt; anywhere, you could check it made sense:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;typescript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-typescript &quot;&gt;&lt;code class=&quot;language-typescript&quot; data-lang=&quot;typescript&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;isValidPerson&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;person&lt;&#x2F;span&gt;&lt;span&gt;)) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;doThingWithPerson&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;person&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;} &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;throw &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Person is not valid&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;...and throw an error if it isn&#x27;t. The thing is, it&#x27;s nicer to do validation on the boundaries of
our app and then know afterwards that things are OK. What if we could use our &lt;code&gt;Tagged&lt;&#x2F;code&gt; type
to help us?&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s rewrite our validation functions:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;typescript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-typescript &quot;&gt;&lt;code class=&quot;language-typescript&quot; data-lang=&quot;typescript&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;validateFirstname &lt;&#x2F;span&gt;&lt;span&gt;= (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rawString&lt;&#x2F;span&gt;&lt;span&gt;: string): Firstname | null &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; 
&lt;&#x2F;span&gt;&lt;span&gt;    (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rawString&lt;&#x2F;span&gt;&lt;span&gt;.length &amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0 &lt;&#x2F;span&gt;&lt;span&gt;&amp;amp;&amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rawString&lt;&#x2F;span&gt;&lt;span&gt;.length &amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;100&lt;&#x2F;span&gt;&lt;span&gt;) 
&lt;&#x2F;span&gt;&lt;span&gt;       ? &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rawString &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;as &lt;&#x2F;span&gt;&lt;span&gt;Firstname : &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;null
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;validateSurname &lt;&#x2F;span&gt;&lt;span&gt;= (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rawString&lt;&#x2F;span&gt;&lt;span&gt;: string): Surname | null &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; 
&lt;&#x2F;span&gt;&lt;span&gt;    (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rawString&lt;&#x2F;span&gt;&lt;span&gt;.length &amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0 &lt;&#x2F;span&gt;&lt;span&gt;&amp;amp;&amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rawString&lt;&#x2F;span&gt;&lt;span&gt;.length &amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;50&lt;&#x2F;span&gt;&lt;span&gt;) 
&lt;&#x2F;span&gt;&lt;span&gt;       ? &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rawString &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;as &lt;&#x2F;span&gt;&lt;span&gt;Surname : &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;null
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;validateAge &lt;&#x2F;span&gt;&lt;span&gt;= (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rawAge&lt;&#x2F;span&gt;&lt;span&gt;: number): Age | null &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; 
&lt;&#x2F;span&gt;&lt;span&gt;    (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rawAge &lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0 &lt;&#x2F;span&gt;&lt;span&gt;&amp;amp;&amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rawAge &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;120&lt;&#x2F;span&gt;&lt;span&gt;) ? &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rawAge &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;as &lt;&#x2F;span&gt;&lt;span&gt;Age : &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;null
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;interface &lt;&#x2F;span&gt;&lt;span&gt;ValidPerson {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;firstname&lt;&#x2F;span&gt;&lt;span&gt;: Firstname
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;surname&lt;&#x2F;span&gt;&lt;span&gt;: Surname
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;age&lt;&#x2F;span&gt;&lt;span&gt;: Age
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;validatePerson &lt;&#x2F;span&gt;&lt;span&gt;= (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;person&lt;&#x2F;span&gt;&lt;span&gt;: Person): ValidPerson | null &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;firstname &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;validateFirstname&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;person&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;firstname&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;surname &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;validateSurname&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;person&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;surname&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;age &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;validateAge&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;person&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;age&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;firstname &lt;&#x2F;span&gt;&lt;span&gt;&amp;amp;&amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;surname &lt;&#x2F;span&gt;&lt;span&gt;&amp;amp;&amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;age&lt;&#x2F;span&gt;&lt;span&gt;) ?  {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;firstname&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;surname&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;age
&lt;&#x2F;span&gt;&lt;span&gt;    } : &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;null
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It looks a bit more verbose, but that&#x27;s mostly because I&#x27;ve broken each
validation function out. What it means is that we can take our raw &lt;code&gt;Person&lt;&#x2F;code&gt;
data, run the validator and get a &lt;code&gt;ValidPerson&lt;&#x2F;code&gt; (because it&#x27;s valid) or &lt;code&gt;null&lt;&#x2F;code&gt;
(it wasn&#x27;t valid).&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;typescript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-typescript &quot;&gt;&lt;code class=&quot;language-typescript&quot; data-lang=&quot;typescript&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;badPerson &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;validatePerson&lt;&#x2F;span&gt;&lt;span&gt;({ firstname: &amp;quot;&amp;quot;, surname: &amp;quot;&amp;quot;, age: -&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1 &lt;&#x2F;span&gt;&lt;span&gt;})
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; null
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;goodPerson &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;validatePerson&lt;&#x2F;span&gt;&lt;span&gt;({ firstname: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Tom&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, surname: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Hanks&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, age: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;50 &lt;&#x2F;span&gt;&lt;span&gt;})
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; { firstname: &amp;quot;Tom&amp;quot;, surname: &amp;quot;Hanks&amp;quot;, age: 50 }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We can then pass &lt;code&gt;ValidPerson&lt;&#x2F;code&gt; around the rest of the codebase, knowing that
the values are Good and Valid and not needing to check them again.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;disclaimer&quot;&gt;Disclaimer&lt;&#x2F;h3&gt;
&lt;p&gt;Now, the disadvantage with such a technique is that there&#x27;s nothing stopping me
just using &lt;code&gt;as&lt;&#x2F;code&gt; to change, say a &lt;code&gt;Firstname&lt;&#x2F;code&gt; into a &lt;code&gt;Surname&lt;&#x2F;code&gt; and cheating the
whole thing.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;typescript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-typescript &quot;&gt;&lt;code class=&quot;language-typescript&quot; data-lang=&quot;typescript&quot;&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;giveMeFirstName &lt;&#x2F;span&gt;&lt;span&gt;= (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;thanks&lt;&#x2F;span&gt;&lt;span&gt;: Firstname): Firstname &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;thanks
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;surname&lt;&#x2F;span&gt;&lt;span&gt;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;whoa&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; as Surname
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;giveMeFirstName&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;surname &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;as &lt;&#x2F;span&gt;&lt;span&gt;Firstname) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; cheating!
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;However, if that&#x27;s really a problem in your codebase (and I would
delicately suggest that what you have there is more a social and not a coding
problem) - then perhaps it&#x27;s time to go all in on &lt;code&gt;newtype-ts&lt;&#x2F;code&gt; or similar.&lt;&#x2F;p&gt;
&lt;p&gt;Make sense? No? Don&#x27;t care? &lt;a href=&quot;&#x2F;contact.html&quot;&gt;Let me know!&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Contract testing with Typescript</title>
          <pubDate>Sun, 23 Feb 2020 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/typescript-contract-tests/</link>
          <guid>https://danieljharvey.github.io/typescript-contract-tests/</guid>
          <description xml:base="https://danieljharvey.github.io/typescript-contract-tests/">&lt;p&gt;Hello. In our &lt;a href=&quot;https:&#x2F;&#x2F;danieljharvey.github.io&#x2F;posts&#x2F;2020-02-23-contract-testing.html&quot;&gt;last article&lt;&#x2F;a&gt; we described how to generate test cases for contract
testing in Haskell. This time, we are going to look at generating the front end
portion of these in &lt;code&gt;Typescript&lt;&#x2F;code&gt;. This will probably be a lot briefer because I
am a very lazy person at heart.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;first-some-rambling&quot;&gt;First, some rambling&lt;&#x2F;h3&gt;
&lt;p&gt;The main differences with the type system of &lt;code&gt;Haskell&lt;&#x2F;code&gt; or &lt;code&gt;Purescript&lt;&#x2F;code&gt; compared
to that of &lt;code&gt;Typescript&lt;&#x2F;code&gt; is that typeclasses allow the types of things to affect
the code that is generated or used. That&#x27;s how a &lt;code&gt;newtype&lt;&#x2F;code&gt; like &lt;code&gt;Sum&lt;&#x2F;code&gt; or
&lt;code&gt;Product&lt;&#x2F;code&gt; can change the &lt;code&gt;Monoid&lt;&#x2F;code&gt; function used to combine two values, even
though both are just &lt;code&gt;Int&lt;&#x2F;code&gt; values underneath.&lt;&#x2F;p&gt;
&lt;p&gt;The same thing applied to our &lt;code&gt;arbitrary&lt;&#x2F;code&gt; instances - we set a type, derived
the necessary instances, and then all the code was generated for us.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;Typescript&lt;&#x2F;code&gt; is different. Because all the type info is destroyed at runtime,
we have to create run-time (or &lt;code&gt;value level&lt;&#x2F;code&gt;) descriptions of our types, and
then derive the &lt;code&gt;types&lt;&#x2F;code&gt; from those. Make sense? No? Doesn&#x27;t really matter
anyway.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;frisbee-1.png&quot; alt=&quot;Whoa, great job!&quot; title=&quot;Whoa, great job!&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;some-datatypes&quot;&gt;Some datatypes&lt;&#x2F;h3&gt;
&lt;p&gt;Fortunately, generating run-time validators and types for them is exactly what
the excellent &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;gcanti&#x2F;io-ts&quot;&gt;io-ts&lt;&#x2F;a&gt; is for. It allows to
create validators for our datatypes using runtime functions, but then also
derive types from that. Let&#x27;s see an example:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;typescript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-typescript &quot;&gt;&lt;code class=&quot;language-typescript&quot; data-lang=&quot;typescript&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;* &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;as &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;t &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;io-ts&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;export const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PetShape &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;t&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;type&lt;&#x2F;span&gt;&lt;span&gt;({
&lt;&#x2F;span&gt;&lt;span&gt;  petName: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;t&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;string
&lt;&#x2F;span&gt;&lt;span&gt;})
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;export const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;UserShape &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;t&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;type&lt;&#x2F;span&gt;&lt;span&gt;({
&lt;&#x2F;span&gt;&lt;span&gt;  userId: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;t&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;number&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  name: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;t&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;string&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  pets: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;t&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;array&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PetShape&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;})
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span&gt;Pet = t.TypeOf&amp;lt;typeof PetShape&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;export type &lt;&#x2F;span&gt;&lt;span&gt;User = t.TypeOf&amp;lt;typeof UserShape&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Here we have defined &lt;code&gt;Pet&lt;&#x2F;code&gt; and &lt;code&gt;User&lt;&#x2F;code&gt; and created validators for them both.
What does this mean?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;typescript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-typescript &quot;&gt;&lt;code class=&quot;language-typescript&quot; data-lang=&quot;typescript&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;userJson &lt;&#x2F;span&gt;&lt;span&gt;= { userId: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, name: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Frank&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, pets: [] }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ok &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;UserShape&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;decode&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;userJson&lt;&#x2F;span&gt;&lt;span&gt;))
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; ok == Right with a User type inside
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;badUserJson &lt;&#x2F;span&gt;&lt;span&gt;= { blah: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;dfgdfgdgf&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;no &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;UserShape&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;decode&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;badUserJson&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; no == Left with an error message
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This means that we can validate JSON we receive into our application and ensure
that when Typescript says we are talking about a &lt;code&gt;User&lt;&#x2F;code&gt;, we really are.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;frisbee-2.png&quot; alt=&quot;Really nice!&quot; title=&quot;Really nice!&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;reading-sample-json&quot;&gt;Reading sample JSON&lt;&#x2F;h3&gt;
&lt;p&gt;Therefore, hopefully you can work out how we&#x27;re going to check our generated
backend responses from earlier.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;typescript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-typescript &quot;&gt;&lt;code class=&quot;language-typescript&quot; data-lang=&quot;typescript&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;{ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;either &lt;&#x2F;span&gt;&lt;span&gt;} &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;fp-ts&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;* &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;as &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;path &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;path&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;* &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;as &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;fs &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;fs&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; get array of nums 0...99
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rangeArr &lt;&#x2F;span&gt;&lt;span&gt;= [...&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Array&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;100&lt;&#x2F;span&gt;&lt;span&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;keys&lt;&#x2F;span&gt;&lt;span&gt;()];
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; path to wherever our responses are saved
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;outputPath &lt;&#x2F;span&gt;&lt;span&gt;= &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;.&#x2F;responses&#x2F;user&#x2F;&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;describe&lt;&#x2F;span&gt;&lt;span&gt;(&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Read contract tests&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, () &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;test&lt;&#x2F;span&gt;&lt;span&gt;(&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Write sample responses from files&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, () &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;rangeArr&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;forEach&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;index &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; construct the path
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;filename &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;path&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;resolve&lt;&#x2F;span&gt;&lt;span&gt;(`&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;${&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;outputPath&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;}${&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;index&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;}.json&lt;&#x2F;span&gt;&lt;span&gt;`)
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; read the file and turn it into a JS object
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;file &lt;&#x2F;span&gt;&lt;span&gt;= JSON.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;parse&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;fs&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;readFileSync&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;filename&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;utf8&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;))
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; assert whether it validates properly
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;either&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;isRight&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;UserShape&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;decode&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;file&lt;&#x2F;span&gt;&lt;span&gt;))).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;toBeTruthy&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;    })
&lt;&#x2F;span&gt;&lt;span&gt;  })
&lt;&#x2F;span&gt;&lt;span&gt;})
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This &lt;code&gt;Jest&lt;&#x2F;code&gt; test loads each &lt;code&gt;.json&lt;&#x2F;code&gt; file from a given folder and checks that it
decodes correctly with our &lt;code&gt;UserShape&lt;&#x2F;code&gt; validator. If they do, then we know, at
least for this particular endpoint, that our frontend and backend agree with
one another.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;frisbee-3.png&quot; alt=&quot;You&amp;#39;ve done this before!&quot; title=&quot;You&amp;#39;ve done this before!&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;generating-sample-json&quot;&gt;Generating sample JSON&lt;&#x2F;h3&gt;
&lt;p&gt;What has traditionally been a bit harder in &lt;code&gt;Typescript&lt;&#x2F;code&gt; in generating
&lt;code&gt;arbitrary&lt;&#x2F;code&gt; responses. Since we can&#x27;t use out types to inform which values are
made, we have to be a bit creative.&lt;&#x2F;p&gt;
&lt;p&gt;But that&#x27;s hard work. What if somebody else had already solved this and we
could just piggyback off their great work?&lt;&#x2F;p&gt;
&lt;p&gt;Enter &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;dubzzz&#x2F;fast-check&quot;&gt;fast-check&lt;&#x2F;a&gt; and
&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;giogonzo&#x2F;fast-check-io-ts&quot;&gt;io-ts-fast-check&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;fast-check&lt;&#x2F;code&gt; is a property testing tool for &lt;code&gt;Typescript&lt;&#x2F;code&gt;. Essentially, for our
purposes, it&#x27;s &lt;code&gt;QuickCheck&lt;&#x2F;code&gt;. &lt;code&gt;io-ts-fast-check&lt;&#x2F;code&gt; is the secret sauce that says
&quot;if you can give me a validator, I can generate you random values that satisfy
it.&quot;&lt;&#x2F;p&gt;
&lt;p&gt;Therefore, we can use it to create our random front end requests, ready for our
backend to decode and validate. Great!&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;typescript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-typescript &quot;&gt;&lt;code class=&quot;language-typescript&quot; data-lang=&quot;typescript&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;* &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;as &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;fc &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;fast-check&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;* &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;as &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;fs &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;fs&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;* &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;as &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;path &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;path&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;{ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;getArbitrary &lt;&#x2F;span&gt;&lt;span&gt;} &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;fast-check-io-ts&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;{ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;either &lt;&#x2F;span&gt;&lt;span&gt;} &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;fp-ts&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; this creates a fast-check `Arbitrary` from our `User` validator
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;userArbitrary &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;getArbitrary&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;UserShape&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; this takes an output folder and an Arbitrary, and fills the folder with 100
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; json example files 
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;export const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;generate &lt;&#x2F;span&gt;&lt;span&gt;= &amp;lt;A&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;outputPath&lt;&#x2F;span&gt;&lt;span&gt;: string, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;arb&lt;&#x2F;span&gt;&lt;span&gt;: fc.Arbitrary&amp;lt;A&amp;gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;fc&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;sample&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;arb&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;100&lt;&#x2F;span&gt;&lt;span&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;JSON.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;stringify&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;)).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span&gt;((&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;json&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;index&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; construct file path for saving the file
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;filename &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;path&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;resolve&lt;&#x2F;span&gt;&lt;span&gt;(`&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;${&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;outputPath&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;}${&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;index&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;}.json&lt;&#x2F;span&gt;&lt;span&gt;`)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; save that goddamn file
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;fs&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;writeFileSync&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;filename&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;json&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  })
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; example of use
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;generate&lt;&#x2F;span&gt;&lt;span&gt;(&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;.&#x2F;requests&#x2F;users&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;userArbitrary&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;frisbee-4.png&quot; alt=&quot;Feels good, yeah?&quot; title=&quot;Feels good, yeah?&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;great-stuff&quot;&gt;Great stuff!&lt;&#x2F;h3&gt;
&lt;p&gt;Now we can read our sample requests into the backend and see that everything is
fine. Running tests like this before each deploy of either front or back end
service is a great way to make sure nothing will explode. Source code is
available &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;danieljharvey&#x2F;ts-contract-tests&quot;&gt;here&lt;&#x2F;a&gt; if you
can&#x27;t get it to do what you want for some reason.&lt;&#x2F;p&gt;
&lt;p&gt;Make sense? No? Yes? &lt;a href=&quot;&#x2F;contact.html&quot;&gt;Let me know!&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Contract testing with Quickcheck</title>
          <pubDate>Sat, 22 Feb 2020 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/contract-testing/</link>
          <guid>https://danieljharvey.github.io/contract-testing/</guid>
          <description xml:base="https://danieljharvey.github.io/contract-testing/">&lt;p&gt;Hello. I hope you are well. Over the last few months I have been trying out a
method for generating &lt;code&gt;Contract Tests&lt;&#x2F;code&gt; between services using &lt;code&gt;Arbitrary&lt;&#x2F;code&gt;
instances from &lt;a href=&quot;https:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;QuickCheck&quot;&gt;Quickcheck&lt;&#x2F;a&gt; which I thought it might be good to share. It&#x27;s not particularly clever, which is
partially what I like about it, and as a result I may not have been the first
to come up with it. If I have therefore somewhat stolen your thunder please
accept my apologies in advance and then maybe do some reading around SEO.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;what-is-contract-testing-anyway&quot;&gt;What is Contract Testing anyway?&lt;&#x2F;h3&gt;
&lt;p&gt;Contract Testing is a way of checking that two services that are going to
communicate agree on what is going to be sent and received between the two.
There is a good intro to the concept on the &lt;a href=&quot;https:&#x2F;&#x2F;docs.pact.io&#x2F;&quot;&gt;Pact
website&lt;&#x2F;a&gt; - which is itself a contract testing tool. It
is a good tool - HOWEVER it takes a lot of work and I am lazy so there what I
am going to describe is hopefully less work.&lt;&#x2F;p&gt;
&lt;p&gt;For example, a frontend says &quot;I am going to call &lt;code&gt;&#x2F;users&#x2F;horse&#x2F;1000&lt;&#x2F;code&gt; - is that cool?&quot;
and the contract test confirms that the backend is indeed &quot;cool&quot; with that,
shows what kind of thing it would return from that call, to which the frontend
confirms it is also &quot;cool&quot; with that.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;contract-handshake.png&quot; alt=&quot;This is the agreement between the services probably looked like.&quot; title=&quot;This is what the agreement between the services
probably looked like.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;what-is-quickcheck&quot;&gt;What is Quickcheck?&lt;&#x2F;h3&gt;
&lt;p&gt;Quickcheck is a Haskell library that does a thing called Property Testing. It has since been ported to many other languages. A nice intro lives here
at &lt;a href=&quot;https:&#x2F;&#x2F;www.schoolofhaskell.com&#x2F;user&#x2F;pbv&#x2F;an-introduction-to-quickcheck-testing&quot;&gt;School of
Haskell&lt;&#x2F;a&gt; - the tldr is that it lets us test properties about our programs by randomly generating examples and seeing if they fit rules that we define.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;so-what-is-your-so-called-idea-then&quot;&gt;So, what is your so-called idea then?&lt;&#x2F;h3&gt;
&lt;p&gt;OK. So one of the main concepts behind QuickCheck is the &lt;code&gt;Arbitrary&lt;&#x2F;code&gt; typeclass.
Any datatype with an instance of this typeclass is able to generate random
example values that can be used for testing.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;module &lt;&#x2F;span&gt;&lt;span&gt;ContractTests &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;Data.Aeson
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;GHC.Generics
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;Test.QuickCheck.Arbitrary
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Horse
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BigHorse
&lt;&#x2F;span&gt;&lt;span&gt;  | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;SmallHorse
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Generic&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;FromJSON&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Arbitrary Horse where
&lt;&#x2F;span&gt;&lt;span&gt;  arbitrary =
&lt;&#x2F;span&gt;&lt;span&gt;    oneof
&lt;&#x2F;span&gt;&lt;span&gt;      [ pure &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BigHorse&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        pure &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;SmallHorse
&lt;&#x2F;span&gt;&lt;span&gt;      ]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This datatype &lt;code&gt;Horse&lt;&#x2F;code&gt; describes the two kinds of horse, &lt;code&gt;BigHorse&lt;&#x2F;code&gt; and &lt;code&gt;SmallHorse&lt;&#x2F;code&gt;. As is hopefully hand-wavingly evident, when the &lt;code&gt;arbitrary&lt;&#x2F;code&gt; function is run it returns one of &lt;code&gt;BigHorse&lt;&#x2F;code&gt; or &lt;code&gt;SmallHorse&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;contract-horse.png&quot; alt=&quot;This is a horse.&quot; title=&quot;This is a horse.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;If we can make one &lt;code&gt;Horse&lt;&#x2F;code&gt;, then surely we can make a load of them? Indeed!
This is what the instance for &lt;code&gt;List&lt;&#x2F;code&gt; look like - the &lt;code&gt;Arbitrary a =&amp;gt;&lt;&#x2F;code&gt;
constraint means that if we can make any &lt;code&gt;Arbitrary&lt;&#x2F;code&gt; value, we can make a list
of them.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Arbitrary &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; Arbitrary&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  ...
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The Quickcheck library defines instances for most basic types, as well as most
collections, therefore it is quite simple to build &lt;code&gt;Arbitrary&lt;&#x2F;code&gt; instances for
our request and response datatypes - and that is exactly what we&#x27;re going to
do.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;an-example&quot;&gt;An example&lt;&#x2F;h3&gt;
&lt;p&gt;Let&#x27;s think about a nice simple API. It receives &lt;code&gt;POST&lt;&#x2F;code&gt; requests in &lt;code&gt;JSON&lt;&#x2F;code&gt; format that translate into a datatype that looks like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;APIRequest
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;APIRequest
&lt;&#x2F;span&gt;&lt;span&gt;      { name      :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;String
&lt;&#x2F;span&gt;&lt;span&gt;      , age       :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Int
&lt;&#x2F;span&gt;&lt;span&gt;      , horseSize :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Horse
&lt;&#x2F;span&gt;&lt;span&gt;      }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Assuming that the requests are OK, it returns a response shaped like this
(again, sent over the wire in &lt;code&gt;JSON&lt;&#x2F;code&gt; format).&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;APIResponse
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;APIResponse
&lt;&#x2F;span&gt;&lt;span&gt;      { weight :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Int
&lt;&#x2F;span&gt;&lt;span&gt;      , goodHorse :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Bool
&lt;&#x2F;span&gt;&lt;span&gt;      }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;These types are shown in &lt;code&gt;Haskell&lt;&#x2F;code&gt;, but the equivalent pair will also exist in
the frontend, and it&#x27;s the compatibility between the two pairs that we will be checking.&lt;&#x2F;p&gt;
&lt;p&gt;Our testing is going to work like this:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Our frontend will generate 100 example &lt;code&gt;APIRequest&lt;&#x2F;code&gt; values&lt;&#x2F;li&gt;
&lt;li&gt;Each one will be turned into JSON&lt;&#x2F;li&gt;
&lt;li&gt;Each piece of JSON is saved into a file&lt;&#x2F;li&gt;
&lt;li&gt;Our backend will decode each piece of JSON and see if it makes sense&lt;&#x2F;li&gt;
&lt;li&gt;If it does - great!&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Then, for responses, we do the same thing in reverse:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Our backend will generate 100 example &lt;code&gt;APIResponse&lt;&#x2F;code&gt; values&lt;&#x2F;li&gt;
&lt;li&gt;Each one will be turned into JSON&lt;&#x2F;li&gt;
&lt;li&gt;Each piece of JSON is saved into a file&lt;&#x2F;li&gt;
&lt;li&gt;Our frontend will decode each piece of JSON and see if it makes sense&lt;&#x2F;li&gt;
&lt;li&gt;If it does - great!&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Essentially, a contract between two services is a complete set of these for each endpoint. In this article I will explain the &lt;code&gt;Haskell&lt;&#x2F;code&gt; part of this, and will follow with the front end portion in the next one.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;contract-horses.png&quot; alt=&quot;These are all horses.&quot; title=&quot;These are all horses.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;creating-the-sample-responses&quot;&gt;Creating the sample responses&lt;&#x2F;h3&gt;
&lt;p&gt;We&#x27;re going to need to add some typeclass instances for our &lt;code&gt;APIResponse&lt;&#x2F;code&gt; type first, so let&#x27;s change it to the following:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;Test.QuickCheck.Arbitrary.Generic
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;APIResponse
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;APIResponse
&lt;&#x2F;span&gt;&lt;span&gt;      { weight :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;goodHorse &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Bool
&lt;&#x2F;span&gt;&lt;span&gt;      }
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Generic&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;ToJSON&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Arbitrary APIResponse where
&lt;&#x2F;span&gt;&lt;span&gt;  arbitrary = genericArbitrary
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;genericArbitrary&lt;&#x2F;code&gt; is provided by the &lt;code&gt;generic-arbitrary&lt;&#x2F;code&gt; package which allows &lt;code&gt;Arbitrary&lt;&#x2F;code&gt; instances to be created for any datatype with a &lt;code&gt;Generic&lt;&#x2F;code&gt; instance. (For more intro on the idea of generics, the &lt;a href=&quot;https:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;base-4.12.0.0&#x2F;docs&#x2F;GHC-Generics.html&quot;&gt;Hackage&lt;&#x2F;a&gt; page is a good start.)&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;ToJSON&lt;&#x2F;code&gt; is provided by &lt;code&gt;Aeson&lt;&#x2F;code&gt;, the excellent package for all dealings with JSON, and deserves a whole post of it&#x27;s own. For our purposes, all we need to know is that for any datatype with a &lt;code&gt;Generic&lt;&#x2F;code&gt; instance, we can derive a free typeclass for turning it to and from JSON.&lt;&#x2F;p&gt;
&lt;p&gt;The special sauce for all of this action is the &lt;code&gt;generate&lt;&#x2F;code&gt; function from
&lt;code&gt;Test.QuickCheck.Gen&lt;&#x2F;code&gt;, which generates any number of values for a given
&lt;code&gt;Arbitrary&lt;&#x2F;code&gt; instance. We use this with a bunch of other housekeeping functions to take these 100
items and turn them into 100 files. I have broken this down into a bunch of
functions so that it&#x27;s hopefully easier to follow.&lt;&#x2F;p&gt;
&lt;p&gt;Firstly, a couple of helpers for adding index numbers to lists...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;indexList &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; [(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;)]
&lt;&#x2F;span&gt;&lt;span&gt;indexList as =
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;List&lt;&#x2F;span&gt;&lt;span&gt;.zip [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1 &lt;&#x2F;span&gt;&lt;span&gt;..] as
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- indexList [&amp;#39;A&amp;#39;, &amp;#39;B&amp;#39;] == [(1, &amp;#39;A&amp;#39;), (2, &amp;#39;B&amp;#39;)]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;...and creating a file path using said index...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;createPath &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: String -&amp;gt; Int -&amp;gt; String
&lt;&#x2F;span&gt;&lt;span&gt;createPath path index =
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;.&#x2F;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;lt;&amp;gt; path &amp;lt;&amp;gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&#x2F;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;lt;&amp;gt; (show index) &amp;lt;&amp;gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;.json&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- createPath &amp;quot;output&amp;quot; 1 == &amp;quot;.&#x2F;output&#x2F;1.json&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Next, we will make our functions for generating instances and saving them to
files. This first function uses a &lt;code&gt;Proxy&lt;&#x2F;code&gt; (from
&lt;a href=&quot;https:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;base-4.12.0.0&#x2F;docs&#x2F;Data-Proxy.html&quot;&gt;Data.Proxy&lt;&#x2F;a&gt;)
to pass the type we would like to generate (as such). We have chosen &lt;code&gt;100&lt;&#x2F;code&gt; as
it is as good a number as any.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;getResponses &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Arbitrary &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; Proxy &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; IO&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;getResponses _ = generate $ vector &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;100
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This next function takes our list of randomised values, turns them to JSON, and pops them in a &lt;code&gt;Tuple&lt;&#x2F;code&gt; along with an index.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;listToJSON &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;ToJSON &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; [(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;BS&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;ByteString&lt;&#x2F;span&gt;&lt;span&gt;)]
&lt;&#x2F;span&gt;&lt;span&gt;listToJSON = (indexList . jsonifyList)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;    jsonifyList = fmap encode
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Finally, we put them together along with some glue code (using &lt;code&gt;writeFile&lt;&#x2F;code&gt; from
&lt;code&gt;Data.ByteString.Lazy&lt;&#x2F;code&gt;) to save the generated &lt;code&gt;JSON&lt;&#x2F;code&gt; files.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;contractWrite &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::
&lt;&#x2F;span&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;ToJSON &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Arbitrary &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Proxy &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;String -&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;IO &lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;contractWrite arbType srcPath = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; saveFile = \path (index, json) -&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BS&lt;&#x2F;span&gt;&lt;span&gt;.writeFile (createPath path index) json
&lt;&#x2F;span&gt;&lt;span&gt;  responses &amp;lt;- listToJSON &amp;lt;$&amp;gt; (getResponses arbType)
&lt;&#x2F;span&gt;&lt;span&gt;  mapM_ (saveFile srcPath) responses
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To use it with our datatype, we use a &lt;code&gt;Proxy&lt;&#x2F;code&gt; as such to pass it the type we
want (in our case, &lt;code&gt;APIResponse&lt;&#x2F;code&gt;, but the same code will work for any type with
&lt;code&gt;Arbitrary&lt;&#x2F;code&gt; and &lt;code&gt;ToJSON&lt;&#x2F;code&gt; instances)&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;contractWriteAPIResponses &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: String -&amp;gt; IO &lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;contractWriteAPIResponses srcPath =
&lt;&#x2F;span&gt;&lt;span&gt;  contractWrite (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Proxy &lt;&#x2F;span&gt;&lt;span&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Proxy APIResponse&lt;&#x2F;span&gt;&lt;span&gt;) srcPath
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If we crack this open in &lt;code&gt;ghci&lt;&#x2F;code&gt; we can run &lt;code&gt;contractWriteAPIResponses &quot;sample&quot;&lt;&#x2F;code&gt;, and it will create files called &lt;code&gt;1.json&lt;&#x2F;code&gt;, &lt;code&gt;2.json&lt;&#x2F;code&gt; (up to &lt;code&gt;100.json&lt;&#x2F;code&gt;) in the &lt;code&gt;sample&lt;&#x2F;code&gt; folder in the current working directory.&lt;&#x2F;p&gt;
&lt;p&gt;Our frontend tests can now read these and make sure that they understand them.
But how do we make sure our backend understands the front end requests?&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;contract-business-man.png&quot; alt=&quot;I am feeling bad about how many words there are.&quot; title=&quot;I am feeling bad about how many words
there are.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;reading-the-sample-requests&quot;&gt;Reading the sample requests&lt;&#x2F;h3&gt;
&lt;p&gt;Assuming that our front end has also created some sample requests in a similar fashion, reading them and checking they are decodable is a simpler affair.&lt;&#x2F;p&gt;
&lt;p&gt;Given a path that points to a folder full of 100 json files, we can write code to attempt to read them. We are reusing the &lt;code&gt;createPath&lt;&#x2F;code&gt; function from above, but other than, this should do it.&lt;&#x2F;p&gt;
&lt;p&gt;This function takes a &lt;code&gt;Proxy&lt;&#x2F;code&gt; for our decoding type, a path and an index, and
tries to decode the file it finds.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;testFile &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: FromJSON &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; Proxy &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; String -&amp;gt; Int -&amp;gt; IO&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Maybe &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;testFile _ path i = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  str &amp;lt;- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BS&lt;&#x2F;span&gt;&lt;span&gt;.readFile (createPath path i)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;case&lt;&#x2F;span&gt;&lt;span&gt; eitherDecode str &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;of
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left&lt;&#x2F;span&gt;&lt;span&gt; e -&amp;gt; putStrLn (show e) &amp;gt;&amp;gt;= \_ -&amp;gt; pure &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span&gt; b -&amp;gt; pure (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; b)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Here we take a path to the folder full of files and attempt to read &lt;code&gt;100&lt;&#x2F;code&gt;
numbered &lt;code&gt;.json&lt;&#x2F;code&gt; files in it.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;contractRead &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: FromJSON &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; Proxy &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; String -&amp;gt; IO Int
&lt;&#x2F;span&gt;&lt;span&gt;contractRead arbType srcPath = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  maybeFound &amp;lt;- mapM (testFile arbType srcPath) [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1 &lt;&#x2F;span&gt;&lt;span&gt;.. &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;100&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;  pure $ length $ catMaybes maybeFound
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And here we put it all together using our &lt;code&gt;APIRequest&lt;&#x2F;code&gt; type.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;contractReadAPIRequest &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: String -&amp;gt; IO Int
&lt;&#x2F;span&gt;&lt;span&gt;contractReadAPIRequest srcPath =
&lt;&#x2F;span&gt;&lt;span&gt;  contractRead (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Proxy &lt;&#x2F;span&gt;&lt;span&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Proxy APIRequest&lt;&#x2F;span&gt;&lt;span&gt;) srcPath
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Cracking open &lt;code&gt;ghci&lt;&#x2F;code&gt; and running &lt;code&gt;contractRead &quot;sample&quot;&lt;&#x2F;code&gt; will attempt to read 100 numbered &lt;code&gt;.json&lt;&#x2F;code&gt; files in the &lt;code&gt;sample&lt;&#x2F;code&gt; folder.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;digression-1&quot;&gt;Digression 1.&lt;&#x2F;h3&gt;
&lt;p&gt;Note we have used a &lt;code&gt;Proxy&lt;&#x2F;code&gt; type here to pass the type around. An alternative way to do this could be a mixture of &lt;code&gt;TypeApplications&lt;&#x2F;code&gt; and &lt;code&gt;ScopedTypeVariables&lt;&#x2F;code&gt; but in the spirit of &lt;code&gt;#simplehaskell&lt;&#x2F;code&gt; we&#x27;ll avoid them.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;digression-2&quot;&gt;Digression 2.&lt;&#x2F;h3&gt;
&lt;p&gt;Whilst preaching simplicity, it seems enjoyably hypocritical to point out in
the same breath that we needn&#x27;t have made a standalone instance of &lt;code&gt;arbitrary&lt;&#x2F;code&gt;
each time, and it&#x27;s actually a great opportunity to crack open &lt;code&gt;DerivingVia&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;An alternative method could looks something like this, and save up to 15
characters per datatype.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- this newtype can derive Arbitrary via Generic, so we use Deriving Via to
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- steal it&amp;#39;s powers!
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;newtype &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;GenericArb&lt;&#x2F;span&gt;&lt;span&gt; a
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;GenericArb&lt;&#x2F;span&gt;&lt;span&gt; {getGenericArb :: a}
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Generic&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Generic &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Arbitrary &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; Arbitrary&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;GenericArb &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  arbitrary = genericArbitrary
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;APIRequest2
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;APIRequest2
&lt;&#x2F;span&gt;&lt;span&gt;      { name2 :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;age2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;horseSize2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Horse
&lt;&#x2F;span&gt;&lt;span&gt;      }
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Generic&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;FromJSON&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Arbitrary&lt;&#x2F;span&gt;&lt;span&gt;) via (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;GenericArb APIRequest2&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;APIResponse2
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;APIResponse2
&lt;&#x2F;span&gt;&lt;span&gt;      { weight2 :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;goodHorse2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Bool
&lt;&#x2F;span&gt;&lt;span&gt;      }
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Generic&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;ToJSON&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Arbitrary&lt;&#x2F;span&gt;&lt;span&gt;) via (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;GenericArb APIResponse2&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;make-sense&quot;&gt;Make sense?&lt;&#x2F;h3&gt;
&lt;p&gt;No. Thought not. Regardless, I&#x27;ll follow up with how to great the front end
part. I&#x27;ll be using &lt;code&gt;Typescript&lt;&#x2F;code&gt; because quite frankly if you understand this
then doing it in &lt;code&gt;Purescript&lt;&#x2F;code&gt; isn&#x27;t wildly different.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>The Genius Of Concrete</title>
          <pubDate>Wed, 09 Oct 2019 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/the-genius-of-concrete/</link>
          <guid>https://danieljharvey.github.io/the-genius-of-concrete/</guid>
          <description xml:base="https://danieljharvey.github.io/the-genius-of-concrete/">&lt;p&gt;Note: I have taken a break from whatever it is that I usually crap on about to
do some &lt;em&gt;opinions&lt;&#x2F;em&gt;. Beware.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;whimsical-story&quot;&gt;Whimsical Story&lt;&#x2F;h3&gt;
&lt;p&gt;When I was much younger I was what you might call &quot;quite a trying child&quot;. Apparently, a thing I liked was to ask lots of questions.&lt;&#x2F;p&gt;
&lt;p&gt;One that I remember very clearly was this:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;&quot;Where do washing machines come from?&quot;&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;(A reasonable, if somewhat tangental question I suppose. I can&#x27;t remember why I
was interested.)&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;&quot;A washing machine&quot;&lt;&#x2F;strong&gt;, they told me, &lt;strong&gt;&quot;is made in a washing-machine-making-machine.&quot;&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Very well, I thought. But that wasn&#x27;t enough.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;&quot;So where do the washing-machine-making-machines come from?&quot;&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;(That&#x27;ll fox them.)&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;&quot;Ahh, well you see&quot;&lt;&#x2F;strong&gt;, the reply went &lt;strong&gt;&quot;those washing-machine-making-machines are
made by washing-machine-making-machine-making-machines&quot;&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;This went on as you might predict. I won&#x27;t bore you with what was obviously some sort of early lesson in the dangers of
unsupervised recursion, but as you can imagine, it was machine-making-machines
all the way down in my dreams for months after that.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;washing-1.png&quot; alt=&quot;Did you really just google &amp;#39;horrible washing machine&amp;#39; and put the first one you found in your blog?.&quot; title=&quot;Did you really just google &amp;#39;horrible washing machine&amp;#39; and put the first one you found in your blog?&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Anyway. There was a point to this. Or at least I thought there was when I
couldn&#x27;t get to sleep and decided I needed to get up and write all this down.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;abstractions&quot;&gt;Abstractions&lt;&#x2F;h3&gt;
&lt;p&gt;So when using the computers and things, we generally start with very concrete
problems, such as &lt;strong&gt;&quot;how we make a washing machine&quot;&lt;&#x2F;strong&gt;, and then later, &lt;strong&gt;&quot;also, how can we put another light on this machine machine that
blinks when the washing is ready?&quot;&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Now, being close to the metal, as such, the manual manufacturer of said washing machine
is not going to have too much trouble (crudely, perhaps) adding such a light and making it work. After all,
they are equipped with washing machine making tools (hammer? wrench?
screwdriver? bear with me here) for the job.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;but-is-the-true-work-of-an-engineer&quot;&gt;But is the true work of an engineer?&lt;&#x2F;h3&gt;
&lt;p&gt;The trouble is, nobody makes washing machines by hand anymore, at least not for
long. Once the maker has gotten the hang of things they think &lt;strong&gt;&quot;this work is
very manual and perhaps somewhat beneath me now, surely we can make this easier?&quot;&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Soon enough, their days with the hammer, the wrench and the
screwdriver are wasted, as they have constructed the &lt;strong&gt;washing-machine-maker&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;washing-2.png&quot; alt=&quot;It was probably something like this.&quot; title=&quot;It was probably
something like this.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;we-are-all-very-pleased-about-this-development&quot;&gt;We are all very pleased about this development.&lt;&#x2F;h3&gt;
&lt;p&gt;It really is good stuff. We&#x27;re making washing machines at a much quicker pace
now, and soon somebody hires in some consultants who point out that we could be
Scaling A Lot Better if we abstract further. Soon enough, they&#x27;re right - and
through a mixture of capitalism and boredom the  &lt;strong&gt;washing-machine-making-machine-maker&lt;&#x2F;strong&gt; is born.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;washing-3.png&quot; alt=&quot;It was probably something like this.&quot; title=&quot;It was probably
something like this.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;nice&quot;&gt;Nice.&lt;&#x2F;h3&gt;
&lt;p&gt;One thing we notice in making the even bigger machine is that many of the parts
in the big machine aren&#x27;t as specialised as we thought, and we replace them
with a combination of more general parts that are tried and tested. This is
generally agreed to be good engineering practice, plus additionally the tried and tested stuff breaks less and is cheaper, so
everybody involved is having a positive time.&lt;&#x2F;p&gt;
&lt;p&gt;It&#x27;s not long until somebody points out that with a little tweaking, we can expand and have a higher level machine called the &lt;strong&gt;washing-machine-and-also-fridge-making-machine-maker-machine-maker&lt;&#x2F;strong&gt; that is also capable of making something that may at one point end up making a fridge as well. Business is booming.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;washing-4.png&quot; alt=&quot;It was probably something like this.&quot; title=&quot;It was probably
something like this.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;oh-shit&quot;&gt;Oh shit.&lt;&#x2F;h3&gt;
&lt;p&gt;But wait. A new requirement has been found.&lt;&#x2F;p&gt;
&lt;p&gt;The washing machine needs Another Light on it. This one is a new colour, not
found in washing machines or fridges. The customer bought one of our first
fridges and was impressed with how quickly they had worked for him. However now
changing the washing machine isn&#x27;t a job for a hammer, a wrench or a
screwdriver anymore, it&#x27;s going to require changes to the
&lt;strong&gt;washing-machine-and-also-fridge-making-machine-maker-machine-maker&lt;&#x2F;strong&gt; to
change the &lt;strong&gt;washing-machine-making-machine-maker&lt;&#x2F;strong&gt; to construct a  &lt;strong&gt;washing-machine-maker&lt;&#x2F;strong&gt; that will output the required &lt;strong&gt;washing-machine&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;It&#x27;d probably be quicker to knock it all down and start over, but now we&#x27;ve got
a shitload of fridges to worry about too.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;fridge.png&quot; alt=&quot;Oh shit indeed.&quot; title=&quot;Oh shit indeed.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;get-to-the-point-you-goddamn-meandering-jerk&quot;&gt;Get to the point, you goddamn meandering jerk.&lt;&#x2F;h3&gt;
&lt;p&gt;My point here is that the recursive levels of washing machine creating
abstractions here are taking us further and further from our real aim here,
which in this already stretched allegory seems to be &lt;strong&gt;&quot;maintain an ability to
add lights to washing machines in a timely manner as and when users of said
machine take an interest in such.&quot;&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;These building blocks, they&#x27;re all very well, but why did we really start this?
Was it because we needed to? Or because we were bored of building fridges?&lt;&#x2F;p&gt;
&lt;h3 id=&quot;let-s-talk-about-concrete&quot;&gt;Let&#x27;s talk about concrete.&lt;&#x2F;h3&gt;
&lt;p&gt;So. Concrete is shit.&lt;&#x2F;p&gt;
&lt;p&gt;(That is to say - it has very little value. I don&#x27;t know what it&#x27;s made of, and looking it up now on
Wikipedia would seem somewhat insincere, but I&#x27;m pretty sure it&#x27;s not made of
actual shit)&lt;&#x2F;p&gt;
&lt;p&gt;However, I am pretty confident you can make things out of it.&lt;&#x2F;p&gt;
&lt;p&gt;Hole in the floor? &lt;strong&gt;Pour that shit in!&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Need a wall? &lt;strong&gt;Slop that shit up!&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Got it wrong? &lt;strong&gt;Smash that shit to pieces!&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;(People can visit the Barbican all they like, it does not change the fact that nobody in human history has been or ever will be emotionally attached to the stuff.)&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;concrete.png&quot; alt=&quot;It&amp;#39;s basically &amp;#39;big sand&amp;#39;.&quot; title=&quot;It&amp;#39;s basically &amp;#39;big sand&amp;#39;.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s not be mistaken - our collection of recursive white goods factories are very nice. They may be the solution sometimes.
But what I think bothers me most about them is just how much emotional investment we end up having in them. When they&#x27;re wrong,
we don&#x27;t abandon ship, but instead save our precious work, bending it to our new needs, however grossly deformed the result.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;so-what-am-i-suggesting&quot;&gt;So, What Am I Suggesting?&lt;&#x2F;h3&gt;
&lt;p&gt;How about just being boring? Would that be so wrong?&lt;&#x2F;p&gt;
&lt;p&gt;Sure, concrete code is boring code to write, and nobody wants to be bored, or worse, boring by association with it.&lt;&#x2F;p&gt;
&lt;p&gt;It is easy to understand though (and even easier to delete) because it&#x27;s not wrapped up in 5 other levels of
abstraction. Do you really need to solve the meta-problem of your problem? Perhaps! I&#x27;m not
suggesting there aren&#x27;t generalised problems worth solving, but check you&#x27;re
not just doing it because your actual problem isn&#x27;t interesting enough.&lt;&#x2F;p&gt;
&lt;p&gt;Don&#x27;t wrap your self-worth up in your cleverness. That way lies, at best, sadness, and at worst, singletons.&lt;&#x2F;p&gt;
&lt;p&gt;Solve the thing, have a nice time, go home.&lt;&#x2F;p&gt;
&lt;p&gt;There&#x27;ll be another problem tomorrow, and this time you might be ready to
solve it rather than label it &quot;scope creep&quot; for not fitting yesterday&#x27;s
solution.&lt;&#x2F;p&gt;
&lt;p&gt;If your work ends up getting thrown away (it will), fine.&lt;&#x2F;p&gt;
&lt;p&gt;It was made of shit anyway.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Free like pizza, not like jazz</title>
          <pubDate>Sat, 07 Sep 2019 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/free-like-pizza/</link>
          <guid>https://danieljharvey.github.io/free-like-pizza/</guid>
          <description xml:base="https://danieljharvey.github.io/free-like-pizza/">&lt;p&gt;When I first started working at a &lt;em&gt;Serious Software Place&lt;&#x2F;em&gt;, I remember being very excited about the idea of testing things and dependency injection and purity and all such things. One day, I was spouting about this (presuming, I suppose, that everybody else was also so new to and thus enamoured with the topic), as well as these &lt;code&gt;Functors&lt;&#x2F;code&gt; I had been reading about, and one of the more senior engineers said, &quot;Well &lt;em&gt;(raises eyebrows suggestively)&lt;&#x2F;em&gt;...you&#x27;re going to &lt;strong&gt;LOVE&lt;&#x2F;strong&gt; Free Monads!&quot;.&lt;&#x2F;p&gt;
&lt;p&gt;Now, many people that know me would agree I am a fairly jaded man, but I have to admit that even my interest was piqued. However, it&#x27;s taken me absolutely fucking ages to work out what they are and how they relate to this original promise, and I &lt;em&gt;think&lt;&#x2F;em&gt; they do, just not how I thought.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;the-intuition-for-free-monads&quot;&gt;The Intuition For Free Monads&lt;&#x2F;h3&gt;
&lt;p&gt;So, the thing that took me the longest time to lose was this assumption I had made that a &lt;code&gt;Free Monad&lt;&#x2F;code&gt; was a wild monad, from the edges of abstract math. It&#x27;s Ornette Coleman! It&#x27;s John Zorn! It&#x27;s a saxophone played with a hoover! It can do anything, at any time! Needless to say, this was a very exciting idea, so I set out to read about, framing everything I read with this wild (but admittedly very attractive) mis-assumption. Needless to say, I was very confused and wasted a lot of time that I could have spent doing fun things instead.&lt;&#x2F;p&gt;
&lt;p&gt;The reality is that the &lt;code&gt;Free&lt;&#x2F;code&gt; in &lt;code&gt;Free Monad&lt;&#x2F;code&gt; is not free like &lt;em&gt;&quot;jazz&quot;&lt;&#x2F;em&gt; or &lt;em&gt;&quot;thinking&quot;&lt;&#x2F;em&gt;, but free as in &lt;em&gt;&quot;pizza, at a tech meetup, knowing that of course the real price is listening through the following recruitment messages&quot;&lt;&#x2F;em&gt;. They are still very good, but &lt;em&gt;(in my humble opinion, etc)&lt;&#x2F;em&gt;, not half as fun as I had originally promised myself they would be.&lt;&#x2F;p&gt;
&lt;p&gt;With this crushing piece of disappointment aside, let&#x27;s look at what they actually are.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;dogs-disguised-as-people-by-way-of-wearing-a-coat-and-hat&quot;&gt;Dogs Disguised As People By Way Of Wearing A Coat And Hat&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;dogs-in-coat.png&quot; alt=&quot;Bear with me here.&quot; title=&quot;Bear with me here.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s start with the idea of a &lt;code&gt;Functor&lt;&#x2F;code&gt;. If this is confusing to you I have written a &lt;a href=&quot;http:&#x2F;&#x2F;localhost:8000&#x2F;posts&#x2F;2018-11-16-typeclasses-functor.html&quot;&gt;vague explanation&lt;&#x2F;a&gt; and so have much more &lt;a href=&quot;http:&#x2F;&#x2F;www.tomharding.me&#x2F;2017&#x2F;03&#x2F;27&#x2F;fantas-eel-and-specification-6&#x2F;&quot;&gt;competent&lt;&#x2F;a&gt; &lt;a href=&quot;http:&#x2F;&#x2F;adit.io&#x2F;posts&#x2F;2013-04-17-functors,_applicatives,_and_monads_in_pictures.html&quot;&gt;people&lt;&#x2F;a&gt; which are probably worth digesting before we go on.&lt;&#x2F;p&gt;
&lt;p&gt;A functor is a data structure which contains some other data, and we can &quot;map over&quot; the data inside it to change that without fucking up the functor itself. For example, we can use the &lt;code&gt;map&lt;&#x2F;code&gt; function on a &lt;code&gt;Maybe&lt;&#x2F;code&gt; value, and it will change the &lt;code&gt;a&lt;&#x2F;code&gt; on a &lt;code&gt;Just&lt;&#x2F;code&gt;, and do nothing on &lt;code&gt;Nothing&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Maybe
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; a
&lt;&#x2F;span&gt;&lt;span&gt;  | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;fmap (+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just 1&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Just 2
&lt;&#x2F;span&gt;&lt;span&gt;fmap (+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Nothing
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now, there is already a &lt;code&gt;Monad&lt;&#x2F;code&gt; instance for &lt;code&gt;Maybe&lt;&#x2F;code&gt;, and this means we can use &lt;code&gt;do notation&lt;&#x2F;code&gt; and all sorts of great stuff with it, like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- get the first item of the first list
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- and the first item of the second list
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- and then smash them together using append
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- from their semigroup instance
&lt;&#x2F;span&gt;&lt;span&gt;firstAndSecondCombine 
&lt;&#x2F;span&gt;&lt;span&gt;  :: (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Semigroup&lt;&#x2F;span&gt;&lt;span&gt; a) 
&lt;&#x2F;span&gt;&lt;span&gt;  =&amp;gt; [a] 
&lt;&#x2F;span&gt;&lt;span&gt;  -&amp;gt; [a] 
&lt;&#x2F;span&gt;&lt;span&gt;  -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Maybe&lt;&#x2F;span&gt;&lt;span&gt; a
&lt;&#x2F;span&gt;&lt;span&gt;firstAndSecondCombine firstList secondList = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  a &amp;lt;- first&amp;#39; firstList
&lt;&#x2F;span&gt;&lt;span&gt;  b &amp;lt;- first&amp;#39; secondList
&lt;&#x2F;span&gt;&lt;span&gt;  pure (a &amp;lt;&amp;gt; b)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- helper that returns the first item of a list if it exists
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;first&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Maybe &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;first&amp;#39; stuff 
&lt;&#x2F;span&gt;&lt;span&gt;    = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;case&lt;&#x2F;span&gt;&lt;span&gt; stuff &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;of
&lt;&#x2F;span&gt;&lt;span&gt;        (x:_) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; x
&lt;&#x2F;span&gt;&lt;span&gt;        _     -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Let&#x27;s try them with the &lt;code&gt;Product&lt;&#x2F;code&gt; monoid &lt;em&gt;(which multiplies its contents together when combined)&lt;&#x2F;em&gt;, to satisfy ourselves that this function does a thing that we vaguely like and approve of.&lt;&#x2F;p&gt;
&lt;p&gt;This fails as it should....&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;nah &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Maybe&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Product Int&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;nah = firstAndSecondCombine &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;[] []
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- nah == Nothing
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;...and this gives us an answer.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;yeah &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Maybe&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Product Int&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;yeah = firstAndSecondCombine 
&lt;&#x2F;span&gt;&lt;span&gt;          [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Product 10&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Product 11&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;          [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Product 20&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Product 5&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- yeah == Just (Product 200)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;em&gt;(Isn&#x27;t &lt;code&gt;Monoid&lt;&#x2F;code&gt; such a nice thing?)&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;that-didn-t-mention-dogs-or-hats-what-gives&quot;&gt;That Didn&#x27;t Mention Dogs Or Hats, What Gives?&lt;&#x2F;h3&gt;
&lt;p&gt;Ahh shit, yes.&lt;&#x2F;p&gt;
&lt;p&gt;So.&lt;&#x2F;p&gt;
&lt;p&gt;What if there isn&#x27;t a &lt;code&gt;Monad&lt;&#x2F;code&gt; instance? Or we want to write programs like this, but don&#x27;t want to commit to using &lt;code&gt;Maybe&lt;&#x2F;code&gt;? (or perhaps &lt;code&gt;Maybe&lt;&#x2F;code&gt; is &lt;strong&gt;TOO&lt;&#x2F;strong&gt; safe, and we want the option of throwing loads of exceptions instead). Could this be a job for...a &lt;code&gt;Free Monad&lt;&#x2F;code&gt;?&lt;&#x2F;p&gt;
&lt;p&gt;&lt;em&gt;(note for readers with subtext disabled in their browser: yes)&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;p&gt;OK. Let&#x27;s make a datatype that&#x27;s like &lt;code&gt;Maybe&lt;&#x2F;code&gt;, but isn&#x27;t.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MoybeF&lt;&#x2F;span&gt;&lt;span&gt; a
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Jost&lt;&#x2F;span&gt;&lt;span&gt; a
&lt;&#x2F;span&gt;&lt;span&gt;  | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothong
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Let&#x27;s make an instance of &lt;code&gt;Functor&lt;&#x2F;code&gt; for it to prove there is no funny business at hand.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Functor MoybeF where
&lt;&#x2F;span&gt;&lt;span&gt;  fmap _ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothong  &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothong
&lt;&#x2F;span&gt;&lt;span&gt;  fmap f (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Jost&lt;&#x2F;span&gt;&lt;span&gt; a) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Jost&lt;&#x2F;span&gt;&lt;span&gt; (f a)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Map the &lt;code&gt;f&lt;&#x2F;code&gt; function over an &lt;code&gt;a&lt;&#x2F;code&gt; if there is one, if not, do nothing. Good?&lt;&#x2F;p&gt;
&lt;h3 id=&quot;still-no-dogs-what-gives&quot;&gt;Still No Dogs, What Gives?&lt;&#x2F;h3&gt;
&lt;p&gt;Right. In this example, &lt;code&gt;MoybeF&lt;&#x2F;code&gt; is our dog. It&#x27;s a plain old functor that doesn&#x27;t really do anything except contain an &lt;code&gt;a&lt;&#x2F;code&gt; inside &lt;code&gt;Jost&lt;&#x2F;code&gt;, or contain nothing at all inside &lt;code&gt;Nothong&lt;&#x2F;code&gt;. What &lt;code&gt;Free&lt;&#x2F;code&gt; does for us (and by that, I mean &lt;code&gt;Free&lt;&#x2F;code&gt; from &lt;a href=&quot;http:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;free-3.3.1&#x2F;docs&#x2F;Control-Monad-Free.html&quot;&gt;Control.Monad.Free&lt;&#x2F;a&gt;) is gives us a coat to wrap about this &lt;code&gt;Functor&lt;&#x2F;code&gt; to let us use it like a &lt;code&gt;Monad&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;em&gt;(In this tenuous analogy, the &lt;code&gt;Monad&lt;&#x2F;code&gt; represents a human adult, or at least some impression of one)&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Free turns MoybeF (a mere functor)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- into Moybe (a mighty monad)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Moybe&lt;&#x2F;span&gt;&lt;span&gt; a = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Free MoybeF&lt;&#x2F;span&gt;&lt;span&gt; a
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;em&gt;(An aside: this metaphor felt a lot more powerful in my mind, but a cursory image search came up with a lot less than I had expected, and quite frankly this picture creeps me the fuck out).&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;free-structure.png&quot; alt=&quot;You deserve better than this.&quot; title=&quot;You deserve better than this.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;OK. So now, by use of the &lt;code&gt;liftF&lt;&#x2F;code&gt; functor from &lt;code&gt;Control.Monad.Free&lt;&#x2F;code&gt;, we can change any &lt;code&gt;MoybeF&lt;&#x2F;code&gt; value into a &lt;code&gt;Moybe&lt;&#x2F;code&gt; monad value instead. Let&#x27;s define some helper functions that do this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- lift a into the Moybe monad
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;jost &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Moybe &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a 
&lt;&#x2F;span&gt;&lt;span&gt;jost = liftF . &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Jost
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- create an empty Moybe value
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;nothong &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Moybe &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;nothong = liftF &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothong
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now, we can redefine our function from above to our our new exciting &lt;code&gt;Moybe&lt;&#x2F;code&gt; type too!&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- get first item (if there is one)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;fFirst&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Moybe &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;fFirst&amp;#39; stuff 
&lt;&#x2F;span&gt;&lt;span&gt;    = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;case&lt;&#x2F;span&gt;&lt;span&gt; stuff &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;of
&lt;&#x2F;span&gt;&lt;span&gt;        (x:_) -&amp;gt; jost x
&lt;&#x2F;span&gt;&lt;span&gt;        _     -&amp;gt; nothong
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;(We could just have used &lt;code&gt;liftF&lt;&#x2F;code&gt; directly, and avoided the helpers, but this way hopefully we can see better how equivalent the two &lt;code&gt;first&#x27;&lt;&#x2F;code&gt; functions are)&lt;&#x2F;p&gt;
&lt;p&gt;Now, let&#x27;s remake our list smashing function from earlier using &lt;code&gt;Moybe&lt;&#x2F;code&gt;....&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;fFirstAndSecondCombine 
&lt;&#x2F;span&gt;&lt;span&gt;  :: (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Semigroup&lt;&#x2F;span&gt;&lt;span&gt; a) 
&lt;&#x2F;span&gt;&lt;span&gt;  =&amp;gt; [a] 
&lt;&#x2F;span&gt;&lt;span&gt;  -&amp;gt; [a] 
&lt;&#x2F;span&gt;&lt;span&gt;  -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Moybe&lt;&#x2F;span&gt;&lt;span&gt; a
&lt;&#x2F;span&gt;&lt;span&gt;fFirstAndSecondCombine as bs = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  a &amp;lt;- fFirst&amp;#39; as
&lt;&#x2F;span&gt;&lt;span&gt;  b &amp;lt;- fFirst&amp;#39; bs
&lt;&#x2F;span&gt;&lt;span&gt;  pure (a &amp;lt;&amp;gt; b)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Hopefully, you can see it&#x27;s pretty much the same. But how do we use it? If we run &lt;code&gt;fFirstAndSecondCombine [] []&lt;&#x2F;code&gt;, for instance, what we get back is a &lt;code&gt;MoybeF&lt;&#x2F;code&gt; again. So what the hell use is that? I went to all of this trouble to create a &lt;code&gt;Monad&lt;&#x2F;code&gt; (admittedly as cheaply as possible) - but it doesn&#x27;t do anything!&lt;&#x2F;p&gt;
&lt;h3 id=&quot;interpreting-the-computation&quot;&gt;Interpreting The Computation&lt;&#x2F;h3&gt;
&lt;p&gt;So what is &lt;code&gt;MoybeF&lt;&#x2F;code&gt;? Essentially, it&#x27;s a data structure describing a monadic computation. Therefore, if we want to turn into something that does things, we need to &lt;em&gt;interpret&lt;&#x2F;em&gt; it.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s turn it back into our original &lt;code&gt;Maybe&lt;&#x2F;code&gt; computation:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;interpretMaybe &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Moybe &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Maybe &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;interpretMaybe
&lt;&#x2F;span&gt;&lt;span&gt;  = foldFree interpret
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;interpret &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: MoybeF &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Maybe &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;    interpret prog&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;      = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;case&lt;&#x2F;span&gt;&lt;span&gt; prog&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;of
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Jost&lt;&#x2F;span&gt;&lt;span&gt; a  -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; a
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothong &lt;&#x2F;span&gt;&lt;span&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As you can see, we&#x27;re just substituting &lt;code&gt;Jost&lt;&#x2F;code&gt; for &lt;code&gt;Just&lt;&#x2F;code&gt; and &lt;code&gt;Nothong&lt;&#x2F;code&gt; for the inferior &lt;code&gt;Nothing&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Now we can use our function to actually do things like before:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;ans2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Maybe&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Product Int&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;ans2 = interpretMaybe (fFirstAndSecondCombine [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Product 10&lt;&#x2F;span&gt;&lt;span&gt;] [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Product 5&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Product 20&lt;&#x2F;span&gt;&lt;span&gt;])
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- ans2 == Just (Product 200)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;ans3 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Maybe&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Product Int&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;ans3 = interpretMaybe (fFirstAndSecondCombine &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;[]&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Product 5&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Product 20&lt;&#x2F;span&gt;&lt;span&gt;])
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- ans3 == Nothing
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;i-see-that-once-again-we-re-doing-something-easy-the-difficult-way&quot;&gt;I See That Once Again We&#x27;re Doing Something Easy The Difficult Way&lt;&#x2F;h3&gt;
&lt;p&gt;OK, fair point. Where is the value in all this indirection?&lt;&#x2F;p&gt;
&lt;p&gt;How about if we want to interpret this computation differently? Perhaps we need to return an &lt;code&gt;Either&lt;&#x2F;code&gt; instead? Changing the interpreter is pretty straightforward:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;interpretEither &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Moybe &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Either String &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;interpretEither
&lt;&#x2F;span&gt;&lt;span&gt;  = foldFree interpret
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;interpret &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: MoybeF &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Either String &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;    interpret prog&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;      = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;case&lt;&#x2F;span&gt;&lt;span&gt; prog&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;of
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Jost&lt;&#x2F;span&gt;&lt;span&gt; a  -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span&gt; a
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothong &lt;&#x2F;span&gt;&lt;span&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Nah, that didn&amp;#39;t work&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;ans4 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Either String&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Product Int&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;ans4 = interpretEither (fFirstAndSecondCombine [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Product 10&lt;&#x2F;span&gt;&lt;span&gt;] [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Product 5&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Product 20&lt;&#x2F;span&gt;&lt;span&gt;])
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- ans4 == Right (Product 200)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;ans5 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Either String&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Product Int&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;ans5 = interpretEither (fFirstAndSecondCombine &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;[]&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Product 5&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Product 20&lt;&#x2F;span&gt;&lt;span&gt;])
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- ans5 == Left &amp;quot;Nah, that didn&amp;#39;t work&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;is-this-a-domain-specific-language&quot;&gt;Is This...A Domain Specific Language?&lt;&#x2F;h3&gt;
&lt;p&gt;That&#x27;s right. One of the main strengths of &lt;code&gt;Free&lt;&#x2F;code&gt; is in creating small little sub-languages for writing logic, that can be interpreted in a number of different ways (such as &lt;em&gt;&quot;what even is this doing?&quot;&lt;&#x2F;em&gt; and &lt;em&gt;&quot;i don&#x27;t understand this code&quot;&lt;&#x2F;em&gt;)&lt;&#x2F;p&gt;
&lt;h3 id=&quot;how-does-this-relate-to-testing&quot;&gt;How Does This Relate To Testing?&lt;&#x2F;h3&gt;
&lt;p&gt;So, this example is pointedly very simple indeed, and involve swapping one pure thing for another, but a much more common use of this pattern is to take some code that does effectful things (like save files, write to a database, etc) and write them in a way where they can be run in the program using &lt;code&gt;IO&lt;&#x2F;code&gt;, but then testing by turning them into something pure like &lt;code&gt;Writer&lt;&#x2F;code&gt;, and looking at the output.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;do-you-have-an-example-of-exactly-that-preferably-centered-about-reading-and-writing-to-a-terminal-window-perchance&quot;&gt;Do You Have An Example Of Exactly That, Preferably Centered About Reading And Writing To A Terminal Window, Perchance?&lt;&#x2F;h3&gt;
&lt;p&gt;Why yes, I do.&lt;&#x2F;p&gt;
&lt;p&gt;So &lt;code&gt;ConsoleF&lt;&#x2F;code&gt; is a &lt;code&gt;Functor&lt;&#x2F;code&gt; with two commands for input and output from a terminal.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;ConsoleF&lt;&#x2F;span&gt;&lt;span&gt; next
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Write String&lt;&#x2F;span&gt;&lt;span&gt; next
&lt;&#x2F;span&gt;&lt;span&gt;  | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Read&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;String &lt;&#x2F;span&gt;&lt;span&gt;-&amp;gt; next)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Functor ConsoleF where
&lt;&#x2F;span&gt;&lt;span&gt;    fmap f (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Write&lt;&#x2F;span&gt;&lt;span&gt; s next) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Write&lt;&#x2F;span&gt;&lt;span&gt; s (f next)
&lt;&#x2F;span&gt;&lt;span&gt;    fmap f (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Read&lt;&#x2F;span&gt;&lt;span&gt; next)    = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Read&lt;&#x2F;span&gt;&lt;span&gt; (f . next)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We power it up into a &lt;code&gt;Free Monad&lt;&#x2F;code&gt;...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Console&lt;&#x2F;span&gt;&lt;span&gt; a = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Free ConsoleF&lt;&#x2F;span&gt;&lt;span&gt; a
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;...and then we write our helpers with &lt;code&gt;liftF&lt;&#x2F;code&gt;...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- write to the console
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;fWrite &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: String -&amp;gt; Console &lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;fWrite str = liftF $ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Write&lt;&#x2F;span&gt;&lt;span&gt; str &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- await user input
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;fRead &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Console String
&lt;&#x2F;span&gt;&lt;span&gt;fRead = liftF $ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Read&lt;&#x2F;span&gt;&lt;span&gt; id
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;...then we write our program with it...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;consoleProg &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Console &lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;consoleProg = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;    fWrite &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;What is your name?&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    a &amp;lt;- fRead
&lt;&#x2F;span&gt;&lt;span&gt;    fWrite $ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Sure? &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++ a
&lt;&#x2F;span&gt;&lt;span&gt;    b &amp;lt;- fRead
&lt;&#x2F;span&gt;&lt;span&gt;    fWrite &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Great.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;...then we interpret in &lt;code&gt;IO&lt;&#x2F;code&gt; to make a working program...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;interpretIO &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Console &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; IO &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;interpretIO
&lt;&#x2F;span&gt;&lt;span&gt;  = foldFree interpret
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;interpret &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: ConsoleF &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; IO &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;    interpret prog&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;      = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;case&lt;&#x2F;span&gt;&lt;span&gt; prog&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;of
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Write&lt;&#x2F;span&gt;&lt;span&gt; s a -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Prelude&lt;&#x2F;span&gt;&lt;span&gt;.putStrLn s &amp;gt;&amp;gt; pure a
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Read&lt;&#x2F;span&gt;&lt;span&gt; a    -&amp;gt; a &amp;lt;$&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Prelude&lt;&#x2F;span&gt;&lt;span&gt;.getLine
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;...then we interpret it in &lt;code&gt;Writer&lt;&#x2F;code&gt; for testing...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;interpretWrite &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Console &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Writer&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;interpretWrite = foldFree interpretConsoleWrite
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;    interpretConsoleWrite prog&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;      = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;case&lt;&#x2F;span&gt;&lt;span&gt; prog&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;of
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Write&lt;&#x2F;span&gt;&lt;span&gt; s a -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;            _ &amp;lt;- tell [s]
&lt;&#x2F;span&gt;&lt;span&gt;            pure a
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Read&lt;&#x2F;span&gt;&lt;span&gt; a -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;            _ &amp;lt;- tell [&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;[wait for input]&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;]
&lt;&#x2F;span&gt;&lt;span&gt;            pure (a &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;[user input]&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;...run it...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;output &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;output = (snd . runWriter . interpretWrite ) consoleProg
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- output == [&amp;quot;What is your name?&amp;quot;,&amp;quot;[wait for input]&amp;quot;,&amp;quot;Sure? [user input]&amp;quot;,&amp;quot;[wait for input]&amp;quot;,&amp;quot;Great.&amp;quot;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;...and we go home for a nice sit down.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;isn-t-this-the-point-of-typeclasses&quot;&gt;Isn&#x27;t This The Point Of Typeclasses?&lt;&#x2F;h3&gt;
&lt;p&gt;Aye, &lt;a href=&quot;https:&#x2F;&#x2F;markkarpov.com&#x2F;post&#x2F;free-monad-considered-harmful.html&quot;&gt;some would say so&lt;&#x2F;a&gt;. But what&#x27;s the point of Haskell if there isn&#x27;t 10 ways of doing the same thing, and endless bloody fights to the death about the minor differences between them?&lt;&#x2F;p&gt;
&lt;h3 id=&quot;that-is-true-you-are-so-right-any-other-helpful-links&quot;&gt;That Is True, You Are So Right. Any Other Helpful Links?&lt;&#x2F;h3&gt;
&lt;p&gt;The thing that finally made all this click for me was this video by &lt;a href=&quot;https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=eKkxmVFcd74&quot;&gt;Nate Faubion&lt;&#x2F;a&gt;. Once I did understand it wasn&#x27;t about jazz, this by &lt;a href=&quot;https:&#x2F;&#x2F;www.parsonsmatt.org&#x2F;2017&#x2F;09&#x2F;22&#x2F;what_does_free_buy_us.html&quot;&gt;Matt Parsons&lt;&#x2F;a&gt; was really great too.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Mr Migration</title>
          <pubDate>Sun, 14 Jul 2019 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/data-migrations/</link>
          <guid>https://danieljharvey.github.io/data-migrations/</guid>
          <description xml:base="https://danieljharvey.github.io/data-migrations/">&lt;p&gt;A few weeks ago I gave a talk at London Haskell that was basically an advert for a data migrating library I haven&#x27;t even finished yet. However, I managed on the whole to hide this fact and some people even came up afterwards and asked me how the library was performing in production to which of course I said &quot;very well indeed, because of course i am very good at computers&quot; and quickly changed the subject.&lt;&#x2F;p&gt;
&lt;p&gt;What follows is basically the crap from my slides, occasionally turned from bullet lists into sentences where I realised I had overused that particular presentation device a little much.&lt;&#x2F;p&gt;
&lt;p&gt;Sitting comfortable? Then we shall begin...&lt;&#x2F;p&gt;
&lt;h3 id=&quot;in-the-beginning&quot;&gt;In The Beginning...&lt;&#x2F;h3&gt;
&lt;p&gt;...we had server side applications.&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;If the code agrees with the DB schema...&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Deploy!&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Great!&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;then-came-javascript&quot;&gt;Then Came Javascript...&lt;&#x2F;h3&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Suddenly all our data was spread around the place&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Things didn&#x27;t necessarily agree with one another&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;There was sometimes &lt;em&gt;JQuery&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;And at some point we land at&lt;&#x2F;p&gt;
&lt;h3 id=&quot;the-traditional-backend-frontend-monolith&quot;&gt;The Traditional Backend &#x2F; Frontend Monolith&lt;&#x2F;h3&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;When the back end changes...&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Change the front end too.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Deploy everything together&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;(did you forget to update the DB schema?)&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;DO THAT QUICKLY&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Forget about the past&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;YOLO&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;so&quot;&gt;So...&lt;&#x2F;h3&gt;
&lt;p&gt;What could possibly go wrong with this?&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;migrate-1.png&quot; alt=&quot;A relatable picture that was not in the presentation to break things up a bit.&quot; title=&quot;A relatable picture that was not in the presentation to break things up a bit.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;problem-one&quot;&gt;Problem One&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;code&gt;Company A&lt;&#x2F;code&gt; have found that DB schema changes make changing the application more difficult than they would like. Therefore they choose to use &lt;strong&gt;event sourcing&lt;&#x2F;strong&gt;. As the application changes, the DB schemas keep up, but they are soon left with an event table full of various historical versions of JSON data.&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;What is this data? Do they still understand it?&lt;&#x2F;em&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;problem-two&quot;&gt;Problem Two&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;code&gt;Company B&lt;&#x2F;code&gt; decide their code is so good that they are going to create a &lt;strong&gt;public API&lt;&#x2F;strong&gt;. Other companies decide to use this API, and annoyingly want it not to change at random. Therefore the API owners make promises not to change their API (even though they bloody love changing their API because they are 10x hackers who just can&#x27;t stop delivering business value).&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;How can they make changes to this without breaking everything?&lt;&#x2F;em&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;problem-three&quot;&gt;Problem Three&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;code&gt;Company C&lt;&#x2F;code&gt; have noticed that &lt;em&gt;large monoliths take ages to deploy&lt;&#x2F;em&gt;. They would also like to decouple teams to maximise parallelisation of work. They adopt a &lt;strong&gt;microservice architecture&lt;&#x2F;strong&gt;. Suddenly, services that talk to one another aren&#x27;t guaranteed to have versions or interfaces that match, so a old service can be receiving requests from a very old service that hasn&#x27;t been updated.&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;How will they cope with communicating with any number of historical deployments?&lt;&#x2F;em&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;a-concrete-example&quot;&gt;A concrete example&lt;&#x2F;h3&gt;
&lt;p&gt;Here&#x27;s a data type that we use in our business critical application. It is called &lt;code&gt;OldUser&lt;&#x2F;code&gt;, which we never really questioned at the time.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;OldUser
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;OldUser 
&lt;&#x2F;span&gt;&lt;span&gt;    { firstName :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;String
&lt;&#x2F;span&gt;&lt;span&gt;    , surname   :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;String
&lt;&#x2F;span&gt;&lt;span&gt;    , pet       :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;String
&lt;&#x2F;span&gt;&lt;span&gt;    , age       :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Int
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Business is going pretty well, I can&#x27;t imagine my meeting this afternoon will go badly...&lt;&#x2F;p&gt;
&lt;h3 id=&quot;oh-no&quot;&gt;Oh no!&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;migrate-2.png&quot; alt=&quot;A change in business requirements has been spotted.&quot; title=&quot;A change in business requirements has been spotted.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;pivot-immediately&quot;&gt;Pivot immediately&lt;&#x2F;h3&gt;
&lt;p&gt;Apparently we can increase profitability by &lt;em&gt;30%&lt;&#x2F;em&gt; by using newtypes properly.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;newtype &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Name
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Name&lt;&#x2F;span&gt;&lt;span&gt; { getName :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Text&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Text&lt;&#x2F;span&gt;&lt;span&gt; }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And replacing &lt;code&gt;String&lt;&#x2F;code&gt; selections of &lt;code&gt;pet&lt;&#x2F;code&gt; types with a more restrictive sum type.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;OldPet
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;OldDog
&lt;&#x2F;span&gt;&lt;span&gt;  | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;OldCat
&lt;&#x2F;span&gt;&lt;span&gt;  | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NoPet
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;These changes took all night, but you really pulled through there.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NewUser
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NewUser
&lt;&#x2F;span&gt;&lt;span&gt;    { newFirstName :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Name
&lt;&#x2F;span&gt;&lt;span&gt;    , newSurname   :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Name
&lt;&#x2F;span&gt;&lt;span&gt;    , newPet       :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;OldPet
&lt;&#x2F;span&gt;&lt;span&gt;    , newAge       :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Int
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Great job.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;hold-on-though&quot;&gt;Hold On Though&lt;&#x2F;h3&gt;
&lt;p&gt;Business is obviously booming now, but what are we going to do about:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Third parties that will insist on using &lt;code&gt;OldUser&lt;&#x2F;code&gt; in their API calls for the
next 18 months&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Stored JSON data with the old data shape&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;what-options-do-we-have&quot;&gt;What options do we have?&lt;&#x2F;h3&gt;
&lt;ol&gt;
&lt;li&gt;As well as developing new code, we keep &lt;strong&gt;old code&lt;&#x2F;strong&gt; for dealing with the &lt;strong&gt;old data&lt;&#x2F;strong&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;ul&gt;
&lt;li&gt;This seems fine...&lt;&#x2F;li&gt;
&lt;li&gt;...till you&#x27;re fixing bugs in the old system too&lt;&#x2F;li&gt;
&lt;li&gt;Shit! More fixes! Now you&#x27;re got an new-old system as well as the new-new one.&lt;&#x2F;li&gt;
&lt;li&gt;More fixes again! Now we have a new-old-new-old system to maintain as well as your main one, which is a new-new-new system by now? It may have been superceded too. Oh dear.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;ol start=&quot;2&quot;&gt;
&lt;li&gt;Or migrate the &lt;strong&gt;old datatypes&lt;&#x2F;strong&gt; to the &lt;strong&gt;new datatypes&lt;&#x2F;strong&gt; and keep one set of logic.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;ul&gt;
&lt;li&gt;Logic stays in new code&lt;&#x2F;li&gt;
&lt;li&gt;Bug fixes in business logic happen once&lt;&#x2F;li&gt;
&lt;li&gt;Logic of migration separated from business logic&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;decision-time&quot;&gt;Decision time&lt;&#x2F;h3&gt;
&lt;p&gt;If you choose Option &lt;code&gt;1&lt;&#x2F;code&gt;, you are on your own. However, if you have chosen Option &lt;code&gt;2&lt;&#x2F;code&gt;, read on...&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;migrate-3.png&quot; alt=&quot;Another picture of an angry business person.&quot; title=&quot;Another picture of an angry business person.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;functions-we-will-need-migrate&quot;&gt;Functions we will need: migrate&lt;&#x2F;h3&gt;
&lt;p&gt;The first thing we&#x27;ll need is a function to convert our &lt;em&gt;old terrible datatype&lt;&#x2F;em&gt; into our &lt;em&gt;new incredible exciting datatype&lt;&#x2F;em&gt;, in this case &lt;code&gt;OldUser&lt;&#x2F;code&gt; to &lt;code&gt;NewUser&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;migrate &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: OldUser -&amp;gt; NewUser
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Actually. Let&#x27;s be realistic about this, and account for the idea that this operation could fail, as life is a bin.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;migrate &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: OldUser -&amp;gt; Maybe NewUser
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;functions-we-will-need-parse&quot;&gt;Functions we will need: parse&lt;&#x2F;h3&gt;
&lt;p&gt;We&#x27;ll also need some functions for decoding JSON, as I have made the somewhat brazen assumption this is a REST API that only receives JSON payloads. We&#x27;ll use functions from the &lt;code&gt;Aeson&lt;&#x2F;code&gt; library because it is reasonably ubiquitous.&lt;&#x2F;p&gt;
&lt;p&gt;This function will attempt converting some &lt;code&gt;JSON&lt;&#x2F;code&gt; into an &lt;code&gt;OldUser&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;parseOldUser &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: JSON -&amp;gt; Maybe OldUser
&lt;&#x2F;span&gt;&lt;span&gt;parseOldUser json
&lt;&#x2F;span&gt;&lt;span&gt;  = parseMaybe (parseJSON json)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And this very similar function will try and convert from &lt;code&gt;JSON&lt;&#x2F;code&gt; into a &lt;code&gt;NewUser&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;parseNewUser &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: JSON -&amp;gt; Maybe NewUser
&lt;&#x2F;span&gt;&lt;span&gt;parseNewUser
&lt;&#x2F;span&gt;&lt;span&gt;  = parseMaybe . parseJSON
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;putting-it-together&quot;&gt;Putting it together&lt;&#x2F;h3&gt;
&lt;p&gt;We can then make a function that takes some &lt;code&gt;JSON&lt;&#x2F;code&gt;, and then tries to decode it into a &lt;code&gt;NewUser&lt;&#x2F;code&gt;. If it can&#x27;t decode it into a &lt;code&gt;NewUser&lt;&#x2F;code&gt;, it tries to parse it into an &lt;code&gt;OldUser&lt;&#x2F;code&gt;, and if that succeeds, it uses some sort of &lt;code&gt;migrate&lt;&#x2F;code&gt; function to turn &lt;code&gt;OldUser&lt;&#x2F;code&gt; into a &lt;code&gt;NewUser&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Said function looks something like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;parseSomeKindOfUser &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: JSON -&amp;gt; Maybe NewUser
&lt;&#x2F;span&gt;&lt;span&gt;parseSomeKindOfUser json
&lt;&#x2F;span&gt;&lt;span&gt;   =   parseNewUser json
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;|&amp;gt; (parseOldUser json &amp;gt;&amp;gt;= migrate)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;(The &lt;code&gt;&amp;lt;|&amp;gt;&lt;&#x2F;code&gt; operator comes from the &lt;code&gt;Alternative&lt;&#x2F;code&gt; typeclass, and works sort of like the &lt;code&gt;||&lt;&#x2F;code&gt; function. The intuition is &lt;code&gt;try this OR try this&lt;&#x2F;code&gt;)&lt;&#x2F;p&gt;
&lt;h3 id=&quot;does-it-scale-though&quot;&gt;Does it scale though?&lt;&#x2F;h3&gt;
&lt;p&gt;It seems to do the job, with a couple of datatypes, however it&#x27;s easy to see how it could get out of hand...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;thing = parse a
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;|&amp;gt; parse b &amp;gt;&amp;gt;= migrate
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;|&amp;gt; parse c &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;|&amp;gt; parse d &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;|&amp;gt; parse e &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;|&amp;gt; parse f &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;|&amp;gt; parse g &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;|&amp;gt; parse h &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;|&amp;gt; parse i &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;|&amp;gt; parse j &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;|&amp;gt; parse k &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;|&amp;gt; parse l &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate 
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;|&amp;gt; parse m &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate &amp;gt;&amp;gt;= migrate
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;so-this-method-is-a-no&quot;&gt;So this method is a no?&lt;&#x2F;h3&gt;
&lt;p&gt;I don&#x27;t think so. But writing all of that is a bit much. Hopefully right now you are asking &quot;Why can&#x27;t I get the compiler to do this for me?&quot;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;migrate-4.png&quot; alt=&quot;The compiler is your friend.&quot; title=&quot;The compiler is your friend.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;good-point&quot;&gt;Good Point.&lt;&#x2F;h3&gt;
&lt;p&gt;OK! Let&#x27;s give it a smash. We start by making a typeclass and using it to version tag our datatype.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Versioned&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;label&lt;&#x2F;span&gt;&lt;span&gt; :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Symbol&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;num&lt;&#x2F;span&gt;&lt;span&gt; :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Nat&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;type&lt;&#x2F;span&gt;&lt;span&gt; num &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;`&lt;&#x2F;span&gt;&lt;span&gt;VersionOf&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;`&lt;&#x2F;span&gt;&lt;span&gt; label :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Type
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;(This &lt;code&gt;VersionOf&lt;&#x2F;code&gt; type here is an &lt;code&gt;associated type family&lt;&#x2F;code&gt; - a type level function that is scoped to only work inside the typeclass it is defined in.)&lt;&#x2F;p&gt;
&lt;p&gt;The typeclass defines a function we can use to find a datatype from the &lt;code&gt;label&lt;&#x2F;code&gt; and the &lt;code&gt;num&lt;&#x2F;code&gt;. For example, we can make a label called &lt;code&gt;&quot;User&quot;&lt;&#x2F;code&gt;, and make &lt;code&gt;OldUser&lt;&#x2F;code&gt; version &lt;code&gt;1&lt;&#x2F;code&gt; of it:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Versioned&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;User&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; 1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;`&lt;&#x2F;span&gt;&lt;span&gt;VersionOf&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;` &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;User&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;OldUser
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We&#x27;ll also define an instance for &lt;code&gt;NewUser&lt;&#x2F;code&gt;, and make that version &lt;code&gt;2&lt;&#x2F;code&gt; of it.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Versioned&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;User&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; 2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;`&lt;&#x2F;span&gt;&lt;span&gt;VersionOf&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;` &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;User&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NewUser
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;linking-versions-together&quot;&gt;Linking versions together&lt;&#x2F;h3&gt;
&lt;p&gt;Next we make a typeclass for migrations that uses our &lt;code&gt;VersionOf&lt;&#x2F;code&gt; type function:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Migratable&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;label&lt;&#x2F;span&gt;&lt;span&gt; :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Symbol&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;num&lt;&#x2F;span&gt;&lt;span&gt; :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Nat&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  fromPrevious
&lt;&#x2F;span&gt;&lt;span&gt;    :: (num - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;`&lt;&#x2F;span&gt;&lt;span&gt;VersionOf&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;`&lt;&#x2F;span&gt;&lt;span&gt; label
&lt;&#x2F;span&gt;&lt;span&gt;    -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Maybe&lt;&#x2F;span&gt;&lt;span&gt; (num &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;`&lt;&#x2F;span&gt;&lt;span&gt;VersionOf&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;`&lt;&#x2F;span&gt;&lt;span&gt; label)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It lets us define a function from the previous version of a datatype to the current one, so let&#x27;s use it to migrate &lt;code&gt;OldUser&lt;&#x2F;code&gt; to &lt;code&gt;NewUser&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Migratable&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;User&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; 2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;fromPrevious &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: OldUser -&amp;gt; Maybe NewUser
&lt;&#x2F;span&gt;&lt;span&gt;  fromPrevious older
&lt;&#x2F;span&gt;&lt;span&gt;    = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just &lt;&#x2F;span&gt;&lt;span&gt;$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NewUser 
&lt;&#x2F;span&gt;&lt;span&gt;              { newFirstName = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Name&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Text&lt;&#x2F;span&gt;&lt;span&gt;.pack (firstName older))
&lt;&#x2F;span&gt;&lt;span&gt;              , newSurname   = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Name&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Text&lt;&#x2F;span&gt;&lt;span&gt;.pack (surname older))
&lt;&#x2F;span&gt;&lt;span&gt;              , newPet       = readPet (pet older)
&lt;&#x2F;span&gt;&lt;span&gt;              , newAge       = age older
&lt;&#x2F;span&gt;&lt;span&gt;              }
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;      readPet s
&lt;&#x2F;span&gt;&lt;span&gt;        | s == &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;dog&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;OldDog
&lt;&#x2F;span&gt;&lt;span&gt;        | s == &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;cat&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;OldCat
&lt;&#x2F;span&gt;&lt;span&gt;        | otherwise  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NoPet
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;problem-migrating-from-old-json-versions&quot;&gt;Problem: Migrating from old JSON versions&lt;&#x2F;h3&gt;
&lt;p&gt;Once we&#x27;ve defined instances of the &lt;code&gt;Versioned&lt;&#x2F;code&gt; and &lt;code&gt;Migratable&lt;&#x2F;code&gt; typeclasses for our data, the &lt;code&gt;Data.Migratable&lt;&#x2F;code&gt; library functions start doing helpful things. It provides a version of &lt;code&gt;migrate&lt;&#x2F;code&gt; function we defined concretely earlier, but with a much more exciting (confusing) type signature.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;migrate 
&lt;&#x2F;span&gt;&lt;span&gt;  :: earliest &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;`&lt;&#x2F;span&gt;&lt;span&gt;VersionOf&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;`&lt;&#x2F;span&gt;&lt;span&gt; label 
&lt;&#x2F;span&gt;&lt;span&gt;  -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Maybe&lt;&#x2F;span&gt;&lt;span&gt; (target &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;`&lt;&#x2F;span&gt;&lt;span&gt;VersionOf&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;`&lt;&#x2F;span&gt;&lt;span&gt; label)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It means &quot;I can take the &lt;code&gt;earliest&lt;&#x2F;code&gt; version of &lt;code&gt;label&lt;&#x2F;code&gt; and &lt;code&gt;Maybe&lt;&#x2F;code&gt; return you the &lt;code&gt;target&lt;&#x2F;code&gt; version of &lt;code&gt;label&lt;&#x2F;code&gt;&quot;. We use &lt;em&gt;type applications&lt;&#x2F;em&gt; to pass &lt;code&gt;versions&lt;&#x2F;code&gt; and a &lt;code&gt;label&lt;&#x2F;code&gt; to convert an old datatype to a new one.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;oldToNew &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: OldUser -&amp;gt; Maybe NewUser
&lt;&#x2F;span&gt;&lt;span&gt;oldToNew = migrate @&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt; @&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt; @&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;User&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Going from version &lt;code&gt;1&lt;&#x2F;code&gt; to &lt;code&gt;2&lt;&#x2F;code&gt; means we&#x27;ve just done a shitload of work for a single conversion, but the same function will recursively convert through as many versions of the datatype as you like:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;veryOldToVeryNew &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: OldUser -&amp;gt; Maybe VeryNewUser
&lt;&#x2F;span&gt;&lt;span&gt;veryOldToVeryNew = migrate @&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt; @&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;100&lt;&#x2F;span&gt;&lt;span&gt; @&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;User&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;solution-parsejsonvia&quot;&gt;Solution: parseJSONVia&lt;&#x2F;h3&gt;
&lt;p&gt;The &lt;code&gt;Data.Migratable&lt;&#x2F;code&gt; library also provides us &lt;code&gt;parseJSONVia&lt;&#x2F;code&gt;, with the following exciting type signature:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;parseJSONVia
&lt;&#x2F;span&gt;&lt;span&gt;  :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;JSON 
&lt;&#x2F;span&gt;&lt;span&gt;  -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Maybe&lt;&#x2F;span&gt;&lt;span&gt; (target &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;`&lt;&#x2F;span&gt;&lt;span&gt;VersionOf&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;`&lt;&#x2F;span&gt;&lt;span&gt; label)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We could use to try converting from both &lt;code&gt;OldUser&lt;&#x2F;code&gt; and &lt;code&gt;NewUser&lt;&#x2F;code&gt; like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;parseSomeKindOfUser 
&lt;&#x2F;span&gt;&lt;span&gt;  :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;JSON 
&lt;&#x2F;span&gt;&lt;span&gt;  -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Maybe NewUser
&lt;&#x2F;span&gt;&lt;span&gt;parseSomeKindOfUser 
&lt;&#x2F;span&gt;&lt;span&gt;  = parseJSONVia @&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;User&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; @&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt; @&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Underneath, this is doing our &lt;code&gt;parse&lt;&#x2F;code&gt; and &lt;code&gt;migrate&lt;&#x2F;code&gt; pattern under thge good&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;thing = parse @&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NewUser
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;|&amp;gt; parse @&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;OldUser &lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;gt;= migrate
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;a-note-on-type-applications&quot;&gt;A note on Type Applications&lt;&#x2F;h3&gt;
&lt;p&gt;To understand how to pass the types to the &lt;code&gt;parseJSONVia&lt;&#x2F;code&gt; function, we need to look at the &lt;code&gt;Schema&lt;&#x2F;code&gt; typeclass that provides this functionality:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Schema&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;label&lt;&#x2F;span&gt;&lt;span&gt; :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Symbol&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;earliest&lt;&#x2F;span&gt;&lt;span&gt; :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Nat&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;target&lt;&#x2F;span&gt;&lt;span&gt; :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Nat&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;parseJSONVia &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: JSON&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Value -&amp;gt; JSON&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Parser&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;target&lt;&#x2F;span&gt;&lt;span&gt; `&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;VersionOf&lt;&#x2F;span&gt;&lt;span&gt;` &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;label&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;When we use it, we are passing it types in the order they appear in the class declaration.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;parseJSONVia @&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;User&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; @&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt; @&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Therefore, we are passing the type-level symbol &lt;code&gt;&quot;User&quot;&lt;&#x2F;code&gt; as the first argument &lt;code&gt;label&lt;&#x2F;code&gt;, then a type-level natural number &lt;code&gt;1&lt;&#x2F;code&gt; as the starting version &lt;code&gt;earliest&lt;&#x2F;code&gt;, and finally another type-level natural &lt;code&gt;2&lt;&#x2F;code&gt; as the target version &lt;code&gt;target&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;problem-uniqueness-checking&quot;&gt;Problem: Uniqueness checking&lt;&#x2F;h3&gt;
&lt;p&gt;Uniqueness checking is another feature of &lt;code&gt;Data.Migratable&lt;&#x2F;code&gt;. Let&#x27;s say that we have this data type &lt;code&gt;Info&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Info
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Info 
&lt;&#x2F;span&gt;&lt;span&gt;      { amount: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Pounds&lt;&#x2F;span&gt;&lt;span&gt; }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Then, after another hard pivot, we change the units:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NewInfo
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NewInfo
&lt;&#x2F;span&gt;&lt;span&gt;      { amount :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Pennies&lt;&#x2F;span&gt;&lt;span&gt; }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In our static typed ivory tower, we are fine, but our clients keep sending us
this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;json&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-json &quot;&gt;&lt;code class=&quot;language-json&quot; data-lang=&quot;json&quot;&gt;&lt;span&gt;{ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;amount&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;100 &lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;What is it? 100 pennies? 100 pounds? How can we stop this confusion?&lt;&#x2F;p&gt;
&lt;h3 id=&quot;solution-checking-for-duplicates-with-quickcheck&quot;&gt;Solution: Checking for duplicates with QuickCheck&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;code&gt;Data.Migratable&lt;&#x2F;code&gt; provides us with the &lt;code&gt;matchAll&lt;&#x2F;code&gt; function:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;matchAll &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: IO&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Either&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MatchError&lt;&#x2F;span&gt;&lt;span&gt;] [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Integer&lt;&#x2F;span&gt;&lt;span&gt;])
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;It uses &lt;code&gt;QuickCheck&lt;&#x2F;code&gt; and it&#x27;s &lt;code&gt;Arbitrary&lt;&#x2F;code&gt; instances to generate random &lt;code&gt;JSON&lt;&#x2F;code&gt;
values for each datatype&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Then tries to load each generated value as each version of the datatype&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;And tells us how many version of a datatype each generated instance is able
to decode&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;If it&#x27;s one each - we&#x27;re going to have a good time:&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;describe &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Uses Arbitrary to generate said tests&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; $ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  it &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Checks if our datatypes will get confused&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; $ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;    found &amp;lt;- matchAll @&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt; @&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt; @&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;User&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    found &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;`&lt;&#x2F;span&gt;&lt;span&gt;shouldBe&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;` &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ul&gt;
&lt;li&gt;But if our &lt;code&gt;JSON&lt;&#x2F;code&gt; representations are non-unique, we&#x27;ll know:&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;describe &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Our Pennies and Pounds schema&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; $ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  it &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Spots our problematic matching schema&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; $ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;    found &amp;lt;- matchAll @&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt; @&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt; @&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Same&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    found &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;`&lt;&#x2F;span&gt;&lt;span&gt;shouldBe&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;` &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Duplicates 1&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;], &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Duplicates 2&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;]]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ul&gt;
&lt;li&gt;And we can fix our data types to ensure uniqueness.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Info
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Info 
&lt;&#x2F;span&gt;&lt;span&gt;      { amountPounds :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Pounds&lt;&#x2F;span&gt;&lt;span&gt; }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NewInfo
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NewInfo
&lt;&#x2F;span&gt;&lt;span&gt;      { amountPennies :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Pennies&lt;&#x2F;span&gt;&lt;span&gt; }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;ul&gt;
&lt;li&gt;Good job.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;problem-getting-a-fromjson-instance&quot;&gt;Problem: getting a FromJSON instance&lt;&#x2F;h3&gt;
&lt;p&gt;The &lt;code&gt;Aeson&lt;&#x2F;code&gt; library works by making datatypes define instances of the &lt;code&gt;FromJSON&lt;&#x2F;code&gt; typeclass, and packages like &lt;code&gt;Servant&lt;&#x2F;code&gt; allow us to automagically create web servers that use these types. Can we still use all this good stuff?&lt;&#x2F;p&gt;
&lt;h3 id=&quot;solution-fromjson-instance-around-a-newtype-wrapper&quot;&gt;Solution: FromJSON instance around a newtype wrapper&lt;&#x2F;h3&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;newtype &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;APIUser
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;APIUser&lt;&#x2F;span&gt;&lt;span&gt; { getAPIUser :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;WhateverTheNewestUserTypeIsTheseDays&lt;&#x2F;span&gt;&lt;span&gt; }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;...and using &lt;code&gt;parseJSONVia&lt;&#x2F;code&gt; to create a &lt;code&gt;FromJSON&lt;&#x2F;code&gt; instance for that datatype...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance JSON&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;FromJSON APIUser where
&lt;&#x2F;span&gt;&lt;span&gt;  parseJSON a
&lt;&#x2F;span&gt;&lt;span&gt;    = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;APIUser &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;$&amp;gt; parseJSONVia @&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;User&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; @&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt; @&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt; a
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;...we can make a &lt;code&gt;Servant&lt;&#x2F;code&gt; server that can read any of our historical datatypes.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;ExcellentApi &lt;&#x2F;span&gt;&lt;span&gt;=
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;user&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; :&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Get&lt;&#x2F;span&gt;&lt;span&gt; &amp;#39;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;JSON&lt;&#x2F;span&gt;&lt;span&gt;] [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;APIUser&lt;&#x2F;span&gt;&lt;span&gt;] 
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Great job!&lt;&#x2F;p&gt;
&lt;h3 id=&quot;problem-mistakes-in-our-datatypes&quot;&gt;Problem: Mistakes in our datatypes&lt;&#x2F;h3&gt;
&lt;p&gt;What if we make &quot;mistakes&quot; in our types - like removing a piece of data we later decide we need? Here is a first version of some data.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Dog
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Dog&lt;&#x2F;span&gt;&lt;span&gt; { name :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;String
&lt;&#x2F;span&gt;&lt;span&gt;        , age  :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Int
&lt;&#x2F;span&gt;&lt;span&gt;        }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Versioned&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Dog&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; 1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;`&lt;&#x2F;span&gt;&lt;span&gt;VersionOf&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;` &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Dog&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Dog
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is the second version of the datatype, where we remove &lt;code&gt;age&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;AgelessDog
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;AgelessDog
&lt;&#x2F;span&gt;&lt;span&gt;      { name :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt; }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Versioned&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Dog&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; 2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;`&lt;&#x2F;span&gt;&lt;span&gt;VersionOf&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;` &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Dog&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;AgelessDog
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Oops. We needed that. It&#x27;s back in version &lt;code&gt;3&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;WithAgeDog
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;WithAgeDog
&lt;&#x2F;span&gt;&lt;span&gt;      { name :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;String
&lt;&#x2F;span&gt;&lt;span&gt;      , age  :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Int
&lt;&#x2F;span&gt;&lt;span&gt;      , tail :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Bool
&lt;&#x2F;span&gt;&lt;span&gt;      }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Versioned&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Dog&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; 3 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;`&lt;&#x2F;span&gt;&lt;span&gt;VersionOf&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;` &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Dog&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;WithAgeDog
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;However, this means any version &lt;code&gt;1&lt;&#x2F;code&gt; piece of data will convert through version &lt;code&gt;2&lt;&#x2F;code&gt; and lose everything on the way.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;solution-multiple-import-paths&quot;&gt;Solution: multiple import paths&lt;&#x2F;h3&gt;
&lt;p&gt;Let&#x27;s change our declarations...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Dog
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Dog&lt;&#x2F;span&gt;&lt;span&gt; { name :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;String
&lt;&#x2F;span&gt;&lt;span&gt;        , age  :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Int
&lt;&#x2F;span&gt;&lt;span&gt;        }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Versioned&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Dog&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; 1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;`&lt;&#x2F;span&gt;&lt;span&gt;VersionOf&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;` &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Dog&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Dog
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Versioned&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;AgeDog&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; 1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;`&lt;&#x2F;span&gt;&lt;span&gt;VersionOf&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;` &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;AgeDog&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Dog
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;(We&#x27;ve ignored the middle one for now - it is the same)&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;WithAgeDog
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;WithAgeDog
&lt;&#x2F;span&gt;&lt;span&gt;      { name :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;String
&lt;&#x2F;span&gt;&lt;span&gt;      , age  :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Int
&lt;&#x2F;span&gt;&lt;span&gt;      , tail :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Bool
&lt;&#x2F;span&gt;&lt;span&gt;      }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Versioned&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Dog&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; 3 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;`&lt;&#x2F;span&gt;&lt;span&gt;VersionOf&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;` &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Dog&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;WithAgeDog
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Versioned&lt;&#x2F;span&gt;&lt;span&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;AgeDog&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; 2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;`&lt;&#x2F;span&gt;&lt;span&gt;VersionOf&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;` &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;AgeDog&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;WithAgeDog
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Then our parsing function becomes (something like)&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;parseSomeKindOfDog &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: JSON -&amp;gt; Maybe WithAgeDog
&lt;&#x2F;span&gt;&lt;span&gt;parseSomeKindOfDog json
&lt;&#x2F;span&gt;&lt;span&gt;  =  parseJSONVia @&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;AgeDog&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; @&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt; @&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt; json
&lt;&#x2F;span&gt;&lt;span&gt; &amp;lt;|&amp;gt; parseJSONVia @&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Dog&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; @&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt; @&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt; json
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;How does it work?&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;First, we try the lossless path&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Failing that, we try the lossy path to pick up any &lt;code&gt;AgeLessDog&lt;&#x2F;code&gt; values.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;ok-sum-up-what-you-ve-said-and-stop-selling-me-your-crappy-non-existent-library&quot;&gt;OK. Sum up what you&#x27;ve said and stop selling me your crappy non-existent library&lt;&#x2F;h3&gt;
&lt;p&gt;So hopefully, this technique should let you:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Define migrations outside the main logic code, throw those into a file and forget about them forever until the next migration.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Use simple ADTs for my types if one feels like it.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Avoid historical code making new code more complicated.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;but-does-it-work&quot;&gt;But does it work?&lt;&#x2F;h3&gt;
&lt;p&gt;Who knows? See the code at
&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;danieljharvey&#x2F;migratable&quot;&gt;https:&#x2F;&#x2F;github.com&#x2F;danieljharvey&#x2F;migratable&lt;&#x2F;a&gt; and decide for yourself. I mean, the tests pass, but what does that really tell us.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;addendum&quot;&gt;Addendum&lt;&#x2F;h3&gt;
&lt;p&gt;After the talk, somebody suggested that it&#x27;s all very well accepting old JSON data version in an API, but really you&#x27;d need to provide the response in the old format too. This was a really annoyingly good point, so this is the next feature I am working on for the library - the plan so far is to create a kind of opposite of the &lt;code&gt;Migratable&lt;&#x2F;code&gt; typeclass for responses, that goes from newer to older versions instead.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Refine, fine, fine</title>
          <pubDate>Fri, 05 Jul 2019 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/refined-types/</link>
          <guid>https://danieljharvey.github.io/refined-types/</guid>
          <description xml:base="https://danieljharvey.github.io/refined-types/">&lt;p&gt;About a month ago I gave a talk about &lt;code&gt;Refined&lt;&#x2F;code&gt; types at a React meetup.
Needless to say, it was a resounding success so I thought I would share an
adapted version of the slides so that you can all learn to be as learned as me
when it comes to such a topic.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s start by listing some things that we as programmers generally agree we don&#x27;t particularly like:&lt;&#x2F;p&gt;
&lt;h3 id=&quot;runtime-errors-caused-by-javascript-yolo&quot;&gt;Runtime errors caused by Javascript YOLO&lt;&#x2F;h3&gt;
&lt;p&gt;Here is some classic code written in the Javascript programming language:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;makeBig&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;thing&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;thing&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;toUpperCase&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;At a casual glance, it would appear to be some rather convoluted code for
making a string uppercase.&lt;&#x2F;p&gt;
&lt;p&gt;Therefore, this seems fine:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;makeBig&lt;&#x2F;span&gt;&lt;span&gt;(&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;horse&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; &amp;#39;HORSE&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;But this isn&#x27;t so great:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;makeBig&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;12&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Error: toUpperCase is not defined
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;What happened? Well it wasn&#x27;t really a function for making strings uppercase,
it was a function that takes any piece of data, then makes it uppercase if it&#x27;s
a string, and just breaks weirdly with anything else.&lt;&#x2F;p&gt;
&lt;p&gt;Another thing we don&#x27;t really like doing as programmers is...&lt;&#x2F;p&gt;
&lt;h3 id=&quot;overly-defensive-code-around-user-input&quot;&gt;Overly defensive code around user input&lt;&#x2F;h3&gt;
&lt;p&gt;So if your start in programming involved more than a sprinkling of PHP, then
you&#x27;ll be used to starting all your functions with the &lt;code&gt;manual typechecking dance&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;printName&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;name &lt;&#x2F;span&gt;&lt;span&gt;&amp;amp;&amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;.length &amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0 &lt;&#x2F;span&gt;&lt;span&gt;&amp;amp;&amp;amp; typeof &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;name &lt;&#x2F;span&gt;&lt;span&gt;=== &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;string&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;then &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;name
&lt;&#x2F;span&gt;&lt;span&gt;  } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;No name!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;defense-against-the-dark-arts&quot;&gt;Defense against the dark arts&lt;&#x2F;h3&gt;
&lt;p&gt;Another favourite is manually checking our values to check basic mathematical
operators aren&#x27;t going to explode the whole computer.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;divide&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b &lt;&#x2F;span&gt;&lt;span&gt;== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0 &lt;&#x2F;span&gt;&lt;span&gt;|| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;isNaN&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) || &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;isNaN&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span&gt;)) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;then &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; would have caused error
&lt;&#x2F;span&gt;&lt;span&gt;  } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span&gt;&#x2F; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;how-far-does-something-like-typescript-get-us&quot;&gt;How far does something like Typescript get us?&lt;&#x2F;h3&gt;
&lt;p&gt;So we can change our weird uppercasing function...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;makeBig&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;thing&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;thing&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;toUpperCase&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;...to only take a string like we intended.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;typescript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-typescript &quot;&gt;&lt;code class=&quot;language-typescript&quot; data-lang=&quot;typescript&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;makeBig&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;thing&lt;&#x2F;span&gt;&lt;span&gt;: string) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;thing&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;toUpperCase&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Let&#x27;s give it a smash:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;typescript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-typescript &quot;&gt;&lt;code class=&quot;language-typescript&quot; data-lang=&quot;typescript&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;makeBig&lt;&#x2F;span&gt;&lt;span&gt;(&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;horse&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; &amp;#39;HORSE&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Excellent stuff.&lt;&#x2F;p&gt;
&lt;p&gt;And now, if we try and do some wild type stupidity, our code doesn&#x27;t even compile:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;typescript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-typescript &quot;&gt;&lt;code class=&quot;language-typescript&quot; data-lang=&quot;typescript&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;makeBig&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;12&lt;&#x2F;span&gt;&lt;span&gt;) 
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; Argument of type &amp;#39;6&amp;#39; is not assignable to parameter of type &amp;#39;string&amp;#39;.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;+1&lt;&#x2F;p&gt;
&lt;h3 id=&quot;what-about-this-potentially-malformed-user-input&quot;&gt;What about this potentially malformed user input?&lt;&#x2F;h3&gt;
&lt;p&gt;This Wild West Cowboy Javascript...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;printName&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;name &lt;&#x2F;span&gt;&lt;span&gt;&amp;amp;&amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;.length &amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0 &lt;&#x2F;span&gt;&lt;span&gt;&amp;amp;&amp;amp; typeof &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;name &lt;&#x2F;span&gt;&lt;span&gt;=== &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;string&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;then &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;name
&lt;&#x2F;span&gt;&lt;span&gt;  } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;No name!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;...gets a string type, which means we don&#x27;t have to check that &lt;code&gt;name&lt;&#x2F;code&gt; exists...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;typescript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-typescript &quot;&gt;&lt;code class=&quot;language-typescript&quot; data-lang=&quot;typescript&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;printName&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;: string) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;.length &amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;then &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;name
&lt;&#x2F;span&gt;&lt;span&gt;  } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;No name!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;...but we still need to check whether &lt;code&gt;name&lt;&#x2F;code&gt; is long enough and return a default if not.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;what-about-that-classic-divide-by-zero-problem&quot;&gt;What about that classic divide by zero problem?&lt;&#x2F;h3&gt;
&lt;p&gt;What can basic types give us here?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;divide&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b &lt;&#x2F;span&gt;&lt;span&gt;== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0 &lt;&#x2F;span&gt;&lt;span&gt;|| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;isNaN&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) || &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;isNaN&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span&gt;)) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;then &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; would have caused error
&lt;&#x2F;span&gt;&lt;span&gt;  } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span&gt;&#x2F; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We can get rid of the number checks...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;typescript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-typescript &quot;&gt;&lt;code class=&quot;language-typescript&quot; data-lang=&quot;typescript&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;divide&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;: number, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span&gt;: number) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b &lt;&#x2F;span&gt;&lt;span&gt;== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;then &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; would have caused error
&lt;&#x2F;span&gt;&lt;span&gt;  } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span&gt;&#x2F; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;...but we&#x27;ve still got to check for that zero value. Better, but not great. What if I told you we could do better than this?&lt;&#x2F;p&gt;
&lt;h3 id=&quot;refined&quot;&gt;Refined&lt;&#x2F;h3&gt;
&lt;p&gt;Enter &lt;code&gt;Refined&lt;&#x2F;code&gt; types. A Refined type looks like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;newtype &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Refined&lt;&#x2F;span&gt;&lt;span&gt; predicate value = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Refined&lt;&#x2F;span&gt;&lt;span&gt; value
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As it is a &lt;code&gt;newtype&lt;&#x2F;code&gt; it is a wrapper around a value that is used for type
purposes at compile time but then erased at run time (so when the program runs,
&lt;code&gt;Refined 100&lt;&#x2F;code&gt; is just &lt;code&gt;100&lt;&#x2F;code&gt; as far as memory etc is concerned)&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;value&lt;&#x2F;code&gt; is the type of actual data we are refining, for example &lt;code&gt;Int&lt;&#x2F;code&gt; or &lt;code&gt;Number&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;predicate&lt;&#x2F;code&gt; is a type that lets us better describe the &lt;code&gt;value&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The most interesting thing to note here is that &lt;code&gt;predicate&lt;&#x2F;code&gt; only exists on the type side (ie before the &lt;code&gt;=&lt;&#x2F;code&gt;) and not after - this makes it a &lt;code&gt;phantom type&lt;&#x2F;code&gt; which is only used to add contextual information. Let&#x27;s see what that actually means...&lt;&#x2F;p&gt;
&lt;h3 id=&quot;making-refined-values&quot;&gt;Making Refined values&lt;&#x2F;h3&gt;
&lt;p&gt;There are a few ways to make &lt;code&gt;Refined&lt;&#x2F;code&gt; values, especially in the Haskell
library - we&#x27;ll concentrate on two. I&#x27;m going to use the types from the
Purescript version because a) they&#x27;re simpler and b) I made them and am thus
less likely to get it wrong.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;refine 
&lt;&#x2F;span&gt;&lt;span&gt;  :: value 
&lt;&#x2F;span&gt;&lt;span&gt;  -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Either RefinedError&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Refined&lt;&#x2F;span&gt;&lt;span&gt; predicate value)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is the regular way to make &lt;code&gt;Refined&lt;&#x2F;code&gt; value - you pass it a plain value and
it returns either &lt;code&gt;Left&lt;&#x2F;code&gt; with a &lt;code&gt;RefinedError&lt;&#x2F;code&gt; describing the problem, or &lt;code&gt;Right&lt;&#x2F;code&gt; with
the &lt;code&gt;Refined&lt;&#x2F;code&gt; value inside.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;unsafeRefine 
&lt;&#x2F;span&gt;&lt;span&gt;  :: value 
&lt;&#x2F;span&gt;&lt;span&gt;  -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Refined&lt;&#x2F;span&gt;&lt;span&gt; predicate value
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This ignores the &lt;code&gt;predicate&lt;&#x2F;code&gt; and leaves it to the programmer to go full YOLO
and decide whether the predicate will be fine. I have used this to make
&lt;code&gt;Monoid&lt;&#x2F;code&gt; classes where I want to add two positive numbers without checking
that the outcome will still be positive.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;id&quot;&gt;Id&lt;&#x2F;h3&gt;
&lt;p&gt;The most basic predicate is &lt;code&gt;id&lt;&#x2F;code&gt;, which doesn&#x27;t really do anything.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Refined Id Int
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It&#x27;s named after the &lt;code&gt;id&lt;&#x2F;code&gt; (or &lt;code&gt;identity&lt;&#x2F;code&gt;) function - the function that returns whatever it
receives, basically doing nothing.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;identity &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;x &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;x
&lt;&#x2F;span&gt;&lt;span&gt;identity x = x
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;For example, any value that is a value &lt;code&gt;Int&lt;&#x2F;code&gt; can be made into a valid &lt;code&gt;Refined Id Int&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;id1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Either RefinedError&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Refined Id Int&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;id1 = refine &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;11233
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- id1 == Right (Refined 11233)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;id2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Either RefinedError&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Refined Id Int&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;id2 = refine (-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;213123&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- id2 == Right (Refined (-213123)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;positive&quot;&gt;Positive&lt;&#x2F;h3&gt;
&lt;p&gt;The &lt;code&gt;Positive&lt;&#x2F;code&gt; predicate, which only allows numbers over 0.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Refined Positive Int
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This refinement would pass the predicate:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;positive1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Either RefinedError&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Refined Positive Int&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;positive1 = refine &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;10
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- positive1 == Right (Refined 10)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This clearly very negative number clearly won&#x27;t fly. Nice try, ding dongs!&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;positive2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Either RefinedError&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Refined Positive Int&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;positive2 = refine (-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- positive2 == Left (GreaterThanError 0 (-10))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;from&quot;&gt;From&lt;&#x2F;h3&gt;
&lt;p&gt;We can be even more specific with these types too. The &lt;code&gt;From&lt;&#x2F;code&gt; predicate takes an integer and only allows values equal to or above
it.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Refined&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;From D10&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Int
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;(A note here - that &lt;code&gt;D10&lt;&#x2F;code&gt; is a type-level &lt;code&gt;10&lt;&#x2F;code&gt;. It is provided by the &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;bodil&#x2F;purescript-typelevel&quot;&gt;purescript-typelevel&lt;&#x2F;a&gt; package.)&lt;&#x2F;p&gt;
&lt;p&gt;Therefore this &lt;code&gt;9&lt;&#x2F;code&gt; is clearly taking the piss and totally won&#x27;t &lt;code&gt;refine&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;from1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Either RefinedError&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Refined&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;From D10&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;from1 = refine &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;9
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- from1 == Left (FromError 10 9) 
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;However this &lt;code&gt;100&lt;&#x2F;code&gt; is cool with me, and will happily &lt;code&gt;refine&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;from2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Either RefinedError&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Refined&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;From D10&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;from2 = refine &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;100
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- from2 == Right (Refined 100)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;to&quot;&gt;To&lt;&#x2F;h3&gt;
&lt;p&gt;Hopefully it should be fairly intuitive how the &lt;code&gt;To&lt;&#x2F;code&gt; predicate works...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;to1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Either RefinedError&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Refined&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;To D20&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;to1 = refine &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;21
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- to1 == Left (ToError 20 21)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;to2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Either RefinedError&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Refined&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;To D20&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;to2 = refine &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;17
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- to2 == Right (Refined 17)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;sizeequalto-sizegreaterthan-sizelessthan&quot;&gt;SizeEqualTo, SizeGreaterThan, SizeLessThan&lt;&#x2F;h3&gt;
&lt;p&gt;Refinements don&#x27;t have to just be about numbers - we can use them on &lt;code&gt;foldable&lt;&#x2F;code&gt;
structures too, such as &lt;code&gt;Lists&lt;&#x2F;code&gt;. The refinements let us be specific about sizes
of said structure. Therefore we could make a non-empty &lt;code&gt;List&lt;&#x2F;code&gt; of &lt;code&gt;Boolean&lt;&#x2F;code&gt;
values with &lt;code&gt;Refined (SizeGreaterThan D0) (List Boolean)&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Refined&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;SizeGreaterThan 3&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;List Number&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Therefore this list does not &lt;code&gt;refine&lt;&#x2F;code&gt;...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;size1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Refined RefinedError&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Refined&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;SizeGreaterThan D3&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;List Number&lt;&#x2F;span&gt;&lt;span&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;size1 = refine [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- size1 == Left (SizeGreaterThanError 3 [1, 2])
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;...but this one is fine.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;size2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Refined RefinedError&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Refined&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;SizeGreaterThan D3&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;List Number&lt;&#x2F;span&gt;&lt;span&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;size2 = refine [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- size2 == Right (Refined [1, 2, 3, 4])
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;and-or&quot;&gt;And, Or&lt;&#x2F;h3&gt;
&lt;p&gt;These type signatures are starting to get pretty hefty, but we can do better
than that - we&#x27;ve also got &lt;code&gt;And&lt;&#x2F;code&gt; and &lt;code&gt;Or&lt;&#x2F;code&gt; for combining them.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s only allow whole numbers from &lt;code&gt;1&lt;&#x2F;code&gt; to &lt;code&gt;100&lt;&#x2F;code&gt;...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Refined&lt;&#x2F;span&gt;&lt;span&gt; ((&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;From D1&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;And&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;To D100&lt;&#x2F;span&gt;&lt;span&gt;)) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Int
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Or indeed, allow all whole numbers EXCEPT &lt;code&gt;1&lt;&#x2F;code&gt; to &lt;code&gt;100&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Refined&lt;&#x2F;span&gt;&lt;span&gt; ((&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;To D0&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Or&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;From D101&lt;&#x2F;span&gt;&lt;span&gt;)) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Int
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This type describes the roll of a dice.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Dice &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Refined&lt;&#x2F;span&gt;&lt;span&gt; ((&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;From D1&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;And&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;To D6&lt;&#x2F;span&gt;&lt;span&gt;)) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Int
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Or this one, which describes the first bunch of prime numbers, and is all a bit silly
to be honest.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Prime 
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Refined 
&lt;&#x2F;span&gt;&lt;span&gt;      (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Or&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Equal D2&lt;&#x2F;span&gt;&lt;span&gt;) 
&lt;&#x2F;span&gt;&lt;span&gt;        (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Or&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Equal D3&lt;&#x2F;span&gt;&lt;span&gt;) 
&lt;&#x2F;span&gt;&lt;span&gt;          (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Or&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Equal D5&lt;&#x2F;span&gt;&lt;span&gt;) 
&lt;&#x2F;span&gt;&lt;span&gt;            (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Or&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Equal D7&lt;&#x2F;span&gt;&lt;span&gt;) 
&lt;&#x2F;span&gt;&lt;span&gt;              (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Or&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Equal D11&lt;&#x2F;span&gt;&lt;span&gt;) 
&lt;&#x2F;span&gt;&lt;span&gt;                (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Or&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Equal D13&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Equal D17&lt;&#x2F;span&gt;&lt;span&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;              )
&lt;&#x2F;span&gt;&lt;span&gt;            )
&lt;&#x2F;span&gt;&lt;span&gt;          )
&lt;&#x2F;span&gt;&lt;span&gt;        )
&lt;&#x2F;span&gt;&lt;span&gt;      ) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Int
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;back-to-our-stupid-contrived-problems&quot;&gt;Back to our stupid contrived problems...&lt;&#x2F;h3&gt;
&lt;p&gt;Now with the power of &lt;code&gt;Refined&lt;&#x2F;code&gt; types, our defensive &lt;code&gt;printName&lt;&#x2F;code&gt; function is pretty much unnecessary...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Name &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Refined&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;SizeFrom 1&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;String
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;printName &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Name -&amp;gt; String
&lt;&#x2F;span&gt;&lt;span&gt;printName name = unrefine name
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Plus we can make a type to make division safe from fear, at last..&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Divide &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Refined&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Not&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Equal 0&lt;&#x2F;span&gt;&lt;span&gt;)) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Number
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;divide &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Number -&amp;gt; Divide -&amp;gt; Number
&lt;&#x2F;span&gt;&lt;span&gt;divide a b = a &#x2F; (unrefine b)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;automatic-json-validation&quot;&gt;Automatic JSON validation&lt;&#x2F;h3&gt;
&lt;p&gt;So let&#x27;s say we have this data type using &lt;code&gt;Refined&lt;&#x2F;code&gt;...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;AlcoholUser
&lt;&#x2F;span&gt;&lt;span&gt;  = { name :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Refined&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;SizeFrom 1&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;String
&lt;&#x2F;span&gt;&lt;span&gt;    , age  :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Refined&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;From 18&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Int
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;...if we want to use it as an API request, sounds like a lot of work right?
Maybe not! Because &lt;code&gt;refined&lt;&#x2F;code&gt; instances have &lt;code&gt;fromJSON&lt;&#x2F;code&gt; and &lt;code&gt;toJSON&lt;&#x2F;code&gt; instances
for &lt;code&gt;Aeson&lt;&#x2F;code&gt; (or for &lt;code&gt;Argonaut&lt;&#x2F;code&gt; in Purescript) then we can automatically decode
them from &lt;code&gt;JSON&lt;&#x2F;code&gt; and make the decoding fail if the &lt;code&gt;predicate&lt;&#x2F;code&gt; does not pass.&lt;&#x2F;p&gt;
&lt;p&gt;This way, anywhere in our app, &lt;code&gt;name&lt;&#x2F;code&gt; will always be non-empty. and &lt;code&gt;age&lt;&#x2F;code&gt; will
always be &lt;code&gt;18&lt;&#x2F;code&gt; or more.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;well-shit&quot;&gt;Well, shit.&lt;&#x2F;h3&gt;
&lt;p&gt;Yep. For more details, check out the &lt;a href=&quot;http:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;refined&quot;&gt;Refined Haskell
library&lt;&#x2F;a&gt; or indeed the
&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;danieljharvey&#x2F;purescript-refined&quot;&gt;purescript-refined&lt;&#x2F;a&gt;
library which I ported from the Haskell one.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>A game in Purescript (Part 2 - Effect and Aff)</title>
          <pubDate>Sun, 31 Mar 2019 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/purescript-aff/</link>
          <guid>https://danieljharvey.github.io/purescript-aff/</guid>
          <description xml:base="https://danieljharvey.github.io/purescript-aff/">&lt;p&gt;Good morning and&#x2F;or evening. I found myself defaulting to starting with an apology for the amount of time since my last post, then I caught myself and reminded myself that &lt;em&gt;It&#x27;s My Blog And I Can Post Whenever I Feel Like It Actually&lt;&#x2F;em&gt;. So with that in mind, let&#x27;s continue to today&#x27;s main course, at exactly the rambling pace
of my choosing.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;eggmore.png&quot; alt=&quot;The new game looking completely OK.&quot; title=&quot;The new game looking completely OK.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;As I may have mentioned, my current timesink of choice is a Purescript re-write of a browser game I wrote a couple of years back called &lt;a href=&quot;http:&#x2F;&#x2F;itistheegg.com&#x2F;&quot;&gt;It Is The Egg&lt;&#x2F;a&gt;. It is going pretty OK, all told, I&#x27;ve gotten all the awful fiddly stuff like rendering sorted so now I am free to remake the game logic in nice pure functions and generally have a good time. For those of that don&#x27;t spend their free time ignoring their loved ones and instead writing terrible games in a functional programming style, a few notes on the vague architecture that I have settled on.&lt;&#x2F;p&gt;
&lt;p&gt;It falls into three parts:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Admin&lt;&#x2F;li&gt;
&lt;li&gt;Rendering&lt;&#x2F;li&gt;
&lt;li&gt;Logic&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;admin&quot;&gt;Admin&lt;&#x2F;h3&gt;
&lt;p&gt;The admin of the game is everything that needs setting up at the start. Images need loading, level files need loading, window events need setting, and we need to start a game loop that will run everything all the other parts. All of this stuff is very &lt;em&gt;effectful&lt;&#x2F;em&gt; as it involves interacting with the real actual world, the thing that functional programmers are rightly terrified of. This is mostly the layer that we will be discussing today. This layer is difficult to test, so don&#x27;t.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;rendering&quot;&gt;Rendering&lt;&#x2F;h3&gt;
&lt;p&gt;Much like in the React architecture, the Rendering layer is only concerned with receiving the current state of the game, and displaying it on the screen. We will come back to this later when we talk about working with the HTML Canvas in Purescript, and how we use buffering to keep things (vaguely) smooth. The Rendering layer only has access to a) the current game state and b) the previous game state, so that we can do a few optimisations to avoid unnecessary work. It has no way of changing the game state in any way. This layer, because of it&#x27;s need to write to the canvas, is also &lt;em&gt;effectful&lt;&#x2F;em&gt;, and difficult to test so try and keep it as thin as possible.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;logic&quot;&gt;Logic&lt;&#x2F;h3&gt;
&lt;p&gt;The Logic layer is run every &quot;turn&quot; of the game and is a function that takes the current game state, any input that has been received since the last turn, and uses this to compute the next game state to pass to Rendering layer. Everything in the Logic layer is pure functions, and is very easy to test, therefore this should have lots of tests because we all good programmers who appreciate software craftsmanship. Anybody that has written Redux reducers should find this very familiar.&lt;&#x2F;p&gt;
&lt;p&gt;(Anybody vaguely familiar with design patterns in Haskell might be thinking &quot;shit, this sounds a lot like Matt Parsons&#x27; &lt;a href=&quot;https:&#x2F;&#x2F;www.parsonsmatt.org&#x2F;2018&#x2F;03&#x2F;22&#x2F;three_layer_haskell_cake.html&quot;&gt;Three Layer Haskell Cake&lt;&#x2F;a&gt; pattern, this could be pretty awkward&quot; - and yes - it is indeed pretty similar, and shamelessly so.)&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;eggs.png&quot; alt=&quot;More screenshots because code is tiring.&quot; title=&quot;More screenshots because code is tiring.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;So today we&#x27;re going to talk about the game setup part, and on the way, discover, as I did, the actual difference between Purescript&#x27;s &lt;code&gt;Effect&lt;&#x2F;code&gt; and &lt;code&gt;Aff&lt;&#x2F;code&gt; and how to operate the two of them.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;firstly-some-background&quot;&gt;Firstly, some background&lt;&#x2F;h3&gt;
&lt;p&gt;Whilst any Haskell programming must start with a function such as this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: IO &lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;main = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  putStrLn &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Hello world!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;(The type signature &lt;code&gt;IO ()&lt;&#x2F;code&gt; means &quot;this returns a function that does some IO and returns nothing of interest&quot;)&lt;&#x2F;p&gt;
&lt;p&gt;The Purescript equivalent is this, taken from the default program generated by &lt;code&gt;Pulp&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Effect Unit
&lt;&#x2F;span&gt;&lt;span&gt;main = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  log &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Hello sailor!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This program uses &lt;code&gt;console.log&lt;&#x2F;code&gt; to write out a friendly message, and returns nothing of interest. The &lt;code&gt;Effect&lt;&#x2F;code&gt; type is like Haskell&#x27;s &lt;code&gt;IO&lt;&#x2F;code&gt;, meaning &quot;this function does something &lt;em&gt;effectful&lt;&#x2F;em&gt;&quot; and &lt;code&gt;Unit&lt;&#x2F;code&gt; is the same as &lt;code&gt;()&lt;&#x2F;code&gt;, meaning &quot;nothing&quot;, but Purescript prefers it written using words which is arguably easier to understand.&lt;&#x2F;p&gt;
&lt;p&gt;There is a more subtle difference though - Haskell&#x27;s IO is blocking - meaning we can write code like this, no problem:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: IO &lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;main = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  putStrLn &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Hello, what is your name?&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  name &amp;lt;- readLine
&lt;&#x2F;span&gt;&lt;span&gt;  putStrLn (&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Greetings, &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++ name)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;When we run this program approximate this will happen:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Hello,&lt;&#x2F;span&gt;&lt;span&gt; what is your name?
&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; Mr &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Horse   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# user input
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Greetings,&lt;&#x2F;span&gt;&lt;span&gt; Mr Horse
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The program greets the user, ask them their name, read the response, and use that name in the reply. The type signature for &lt;code&gt;readLine&lt;&#x2F;code&gt; is &lt;code&gt;IO String&lt;&#x2F;code&gt;, meaning a function that does some IO and returns a &lt;code&gt;String&lt;&#x2F;code&gt;. It does not specify when said string will arrive. If the user sits and waits for 100 years to type a response, the program will happily sit and do absolutely nothing else, obediently waiting for the user before continuing with the program.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;egg-sprite-yellow.png&quot; alt=&quot;Let&amp;#39;s distract ourselves briefly with some eggs.&quot; title=&quot;Let&amp;#39;s distract ourselves briefly with some eggs.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Purescript, however, is built on top of Javascript, and some of you may be lucky enough to remember first getting stung by the concepts of callbacks etc when first moving to writing front end code. It&#x27;s equivalent function to &lt;code&gt;readLine&lt;&#x2F;code&gt;, looks something like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;readLine
&lt;&#x2F;span&gt;&lt;span&gt;  :: (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;String &lt;&#x2F;span&gt;&lt;span&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Effect Unit&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Effect Unit
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Hopefully what should be bothering you is that this function returns &lt;code&gt;Effect Unit&lt;&#x2F;code&gt; - ie, with no &lt;code&gt;String&lt;&#x2F;code&gt; to be seen. What&#x27;s going on? Well - the actual return is happening in the the callback function, which is the first argument - &lt;code&gt;(String -&amp;gt; Effect Unit)&lt;&#x2F;code&gt;. This function will immediately return &lt;code&gt;Effect Unit&lt;&#x2F;code&gt; but then send the &lt;code&gt;String&lt;&#x2F;code&gt; to the callback whenever the user sees fit to provide on. Therefore we can write the original greeting function in this new style as thus:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Effect Unit
&lt;&#x2F;span&gt;&lt;span&gt;main = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  log &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Hello, what is your name?&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  readLine (\name -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;    log (&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Greetings, &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++ name)
&lt;&#x2F;span&gt;&lt;span&gt;  )
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This seems pretty OK, however if we end up asking a lot of questions, we&#x27;re going to end in a classic Javascript anti-pattern, the Pyramid Of Death.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;bigChat &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Effect Unit
&lt;&#x2F;span&gt;&lt;span&gt;bigChat
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;      log &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;What is your name?&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;      _ &amp;lt;- readLine
&lt;&#x2F;span&gt;&lt;span&gt;        (\name -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;          log &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;What?&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;          _ &amp;lt;- readLine
&lt;&#x2F;span&gt;&lt;span&gt;            (\what -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;              log &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Are those your hands?&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;              _ &amp;lt;- readLine
&lt;&#x2F;span&gt;&lt;span&gt;                (\hands -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;                  log &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Have you washed them recently?&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;                  _ &amp;lt;- readLine
&lt;&#x2F;span&gt;&lt;span&gt;                    (\clean -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;                      log &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;I see.&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;                  pure unit)
&lt;&#x2F;span&gt;&lt;span&gt;              pure unit)
&lt;&#x2F;span&gt;&lt;span&gt;          pure unit)
&lt;&#x2F;span&gt;&lt;span&gt;      pure unit
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Not ideal.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;what-has-this-got-to-do-with-eggs&quot;&gt;What has this got to do with eggs?&lt;&#x2F;h3&gt;
&lt;p&gt;Sure, sure. Coming back round to our game, we have a similar problem. The game is tile based and so before we can dream of drawing anything we&#x27;re going to need to load a big pile of images.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;egg-sprite.png&quot; alt=&quot;A sprite sheet for an egg.&quot; title=&quot;A sprite sheet for an egg.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;As we can see in the &lt;a href=&quot;https:&#x2F;&#x2F;pursuit.purescript.org&#x2F;packages&#x2F;purescript-canvas&#x2F;4.0.0&#x2F;docs&#x2F;Graphics.Canvas&quot;&gt;Purescript Canvas Docs&lt;&#x2F;a&gt; we are going to need a &lt;code&gt;CanvasImageSource&lt;&#x2F;code&gt; to draw a sprite onto the canvas:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;drawImage &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Context2D -&amp;gt; CanvasImageSource -&amp;gt; Number -&amp;gt; Number -&amp;gt; Effect Unit
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;How do we load one? With this &lt;code&gt;tryLoadImage&lt;&#x2F;code&gt; function.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;tryLoadImage &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: String -&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Maybe CanvasImageSource -&amp;gt; Effect Unit&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Effect Unit
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It takes a &lt;code&gt;String&lt;&#x2F;code&gt; (the path to the image), a callback function (that will be passed &lt;code&gt;Maybe CanvasImageSource&lt;&#x2F;code&gt;), and then returns &lt;code&gt;Effect Unit&lt;&#x2F;code&gt;. This is very similar to &lt;code&gt;readLine&lt;&#x2F;code&gt;, so if we want to load lots of images we are going to end up with a weird pyramid of callbacks and generally a bad mess.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;enter-aff&quot;&gt;Enter Aff&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;slamdata&#x2F;purescript-aff&quot;&gt;Aff&lt;&#x2F;a&gt; is the asynchronous effect monad for Purescript. It allows to sequence async events without using callbacks, and we&#x27;re going to use it to load loads of images. First we are going to need to wrap our &lt;code&gt;Effect&lt;&#x2F;code&gt; function to make an &lt;code&gt;Aff&lt;&#x2F;code&gt; function.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;tryLoadImageAff &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;String -&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Aff CanvasImageSource
&lt;&#x2F;span&gt;&lt;span&gt;tryLoadImageAff path = makeAff wrappedFn
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;    wrappedFn done = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;      tryLoadImage path (\maybeImage -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;case&lt;&#x2F;span&gt;&lt;span&gt; maybeImage &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;of
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; canvasImage -&amp;gt; done (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span&gt; canvasImage))
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing          &lt;&#x2F;span&gt;&lt;span&gt;-&amp;gt; done (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left&lt;&#x2F;span&gt;&lt;span&gt; (error &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Could not load &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;lt;&amp;gt; path))
&lt;&#x2F;span&gt;&lt;span&gt;      )
&lt;&#x2F;span&gt;&lt;span&gt;    pure mempty &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- return empty cancellation function
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;How does this work then?&lt;&#x2F;p&gt;
&lt;p&gt;Firstly, it probably helps to look at the type signature for &lt;code&gt;makeAff&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;makeAff &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;forall a&lt;&#x2F;span&gt;&lt;span&gt;. ((&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Either Error &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Effect Unit&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Effect Canceler&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Aff &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;OK. It&#x27;s a bit weird. The &lt;code&gt;a&lt;&#x2F;code&gt; can be whatever item we&#x27;re trying to move around, let&#x27;s replace it with &lt;code&gt;CanvasImageSource&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;makeAff &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; ((&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Either Error CanvasImageSource -&amp;gt; Effect Unit&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Effect Canceler&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Aff CanvasImageSource
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This function takes a function that returns an &lt;code&gt;Effect Canceler&lt;&#x2F;code&gt;, and returns an &lt;code&gt;Aff&lt;&#x2F;code&gt; function that returns a &lt;code&gt;CanvasImageSource&lt;&#x2F;code&gt;. The &lt;code&gt;(Either Error CanvasImageSource -&amp;gt; Effect Unit)&lt;&#x2F;code&gt; function will actually be passed to you, to use as the callback for the &lt;code&gt;Effect&lt;&#x2F;code&gt; function you are wrapping.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s look closer at &lt;code&gt;wrappedFn&lt;&#x2F;code&gt;, with a type signature added for clarity.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;wrappedFn
&lt;&#x2F;span&gt;&lt;span&gt;  :: (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Either Error CanvasImageSource &lt;&#x2F;span&gt;&lt;span&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Effect Unit&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Effect Canceler
&lt;&#x2F;span&gt;&lt;span&gt;wrappedFn done = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  tryLoadImage path (\maybeImage -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;case&lt;&#x2F;span&gt;&lt;span&gt; maybeImage &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;of
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; canvasImage -&amp;gt; done (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span&gt; canvasImage))
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing          &lt;&#x2F;span&gt;&lt;span&gt;-&amp;gt; done (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left&lt;&#x2F;span&gt;&lt;span&gt; (error &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Could not load &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;lt;&amp;gt; path))
&lt;&#x2F;span&gt;&lt;span&gt;  )
&lt;&#x2F;span&gt;&lt;span&gt;  pure mempty
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It is passed &lt;code&gt;done&lt;&#x2F;code&gt; - which takes an &lt;code&gt;Either&lt;&#x2F;code&gt; holding either an &lt;code&gt;Error&lt;&#x2F;code&gt; for failure, or a &lt;code&gt;CanvasImageSource&lt;&#x2F;code&gt; if all went well.&lt;&#x2F;p&gt;
&lt;p&gt;The callback in &lt;code&gt;tryLoadImage&lt;&#x2F;code&gt; gives us a &lt;code&gt;Maybe&lt;&#x2F;code&gt; so we pattern match on that, add a helpful &lt;code&gt;Error&lt;&#x2F;code&gt; if things go wrong, or return the &lt;code&gt;CanvasImageSource&lt;&#x2F;code&gt; if it all works.&lt;&#x2F;p&gt;
&lt;p&gt;Finally, it creates an &lt;code&gt;Effect Canceler&lt;&#x2F;code&gt; with &lt;code&gt;pure mempty&lt;&#x2F;code&gt; - which just returns a &lt;code&gt;Canceler&lt;&#x2F;code&gt; that does nothing.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;using-our-exciting-new-function&quot;&gt;Using our exciting new function&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;crate.png&quot; alt=&quot;A sprite of a box.&quot; title=&quot;A sprite of a box.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Now instead of using callbacks, we can use regular monad binds to get our images out.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;loadLots &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Aff&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Array CanvasImageSource&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;loadLots = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  file1 &amp;lt;- tryLoadImageAff &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;.&#x2F;static&#x2F;images&#x2F;brick.png&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  file2 &amp;lt;- tryLoadImageAff &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;.&#x2F;static&#x2F;images&#x2F;tile.png&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  file3 &amp;lt;- tryLoadImageAff &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;.&#x2F;static&#x2F;images&#x2F;egg.png&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  pure [file1, file2, file3]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And even better than that, if we take an array of file paths...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;paths &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Array String
&lt;&#x2F;span&gt;&lt;span&gt;paths = [ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;.&#x2F;static&#x2F;images&#x2F;brick.png&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;        , &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;.&#x2F;static&#x2F;images&#x2F;tile.png&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;        , &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;.&#x2F;static&#x2F;images&#x2F;egg.png&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;        , &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;.&#x2F;static&#x2F;images&#x2F;egg2.png&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;        , &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;.&#x2F;static&#x2F;images&#x2F;egg3.png&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;        ]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;...we can use traverse to turn an array of paths into an array of &lt;code&gt;CanvasImageSource&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;loadImages &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Array String -&amp;gt; Aff&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Array CanvasImageSource&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;loadImages paths
&lt;&#x2F;span&gt;&lt;span&gt;  = traverse tryLoadImageAff paths
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;loadImages&lt;&#x2F;code&gt; takes our list of file paths, and returns an &lt;code&gt;Aff&lt;&#x2F;code&gt; containing an &lt;code&gt;Array&lt;&#x2F;code&gt; of &lt;code&gt;CanvasImageSource&lt;&#x2F;code&gt;. &lt;code&gt;traverse&lt;&#x2F;code&gt; runs our function on each item in the array, then turns the types inside out so we can &lt;code&gt;Aff (Array CanvasImageSource)&lt;&#x2F;code&gt; instead of &lt;code&gt;Array (Aff CanvasImageSource)&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Constructing this &lt;code&gt;Aff&lt;&#x2F;code&gt; doesn&#x27;t do anything until we actually run it, which we can do with &lt;code&gt;runAff&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;runAff_ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;forall a&lt;&#x2F;span&gt;&lt;span&gt;. (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Either Error &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Effect Unit&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Aff &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Effect Unit
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It takes a callback, the &lt;code&gt;Aff&lt;&#x2F;code&gt; we have constructed, and then returns &lt;code&gt;Effect Unit&lt;&#x2F;code&gt;. Our whole image loading program would thus look like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Effect Unit
&lt;&#x2F;span&gt;&lt;span&gt;main = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; imageAff
&lt;&#x2F;span&gt;&lt;span&gt;        = loadImages paths
&lt;&#x2F;span&gt;&lt;span&gt;      callback
&lt;&#x2F;span&gt;&lt;span&gt;        = (\images -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;case&lt;&#x2F;span&gt;&lt;span&gt; images &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;of
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left&lt;&#x2F;span&gt;&lt;span&gt; a  -&amp;gt; log a
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span&gt; a -&amp;gt; log $ show (length a) &amp;lt;&amp;gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; images loaded!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;          )
&lt;&#x2F;span&gt;&lt;span&gt;  runAff_ callback imageAff
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This will attempt to load the files, and then either log out the error message or the number of files loaded! Magic! Next time, we&#x27;ll grab these images and print them all over the screen in an exciting manner. For now though, this is quite enough.&lt;&#x2F;p&gt;
&lt;p&gt;Further reading:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;pursuit.purescript.org&#x2F;packages&#x2F;purescript-canvas&#x2F;4.0.0&#x2F;docs&#x2F;Graphics.Canvas&quot;&gt;Graphics.Canvas&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;pursuit.purescript.org&#x2F;packages&#x2F;purescript-aff&#x2F;5.1.1&#x2F;docs&#x2F;Effect.Aff&quot;&gt;Effect.Aff&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Typeclasses - Monad</title>
          <pubDate>Mon, 18 Mar 2019 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/typeclasses-monad/</link>
          <guid>https://danieljharvey.github.io/typeclasses-monad/</guid>
          <description xml:base="https://danieljharvey.github.io/typeclasses-monad/">&lt;p&gt;The &lt;code&gt;Monad&lt;&#x2F;code&gt; is one of the most infamous things around Haskell, and indeed functional programming, and so writing tutorials around them has become something of a cliche. Let me be clear - I really did try and avoid writing one, but it&#x27;s gotten to the point that it&#x27;s difficult to talk about the other more interesting stuff without at least mentioning it.&lt;&#x2F;p&gt;
&lt;p&gt;To try and avoid falling into the regular pitfalls, we are instead going to define the simplest possible &lt;code&gt;Monad&lt;&#x2F;code&gt;, and then look at some examples of them in action. Hopefully you&#x27;ll see that &lt;code&gt;Monads&lt;&#x2F;code&gt; aren&#x27;t that complicated or interesting - it&#x27;s actually the properties of the different datatypes that provide all the different behaviours and make it interesting.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;id&quot;&gt;Id&lt;&#x2F;h3&gt;
&lt;p&gt;So. This is &lt;code&gt;Id&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;newtype &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Id&lt;&#x2F;span&gt;&lt;span&gt; a
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Id&lt;&#x2F;span&gt;&lt;span&gt; { getId :: a}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It is a container that contains any value that we give it.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;val &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Id Int
&lt;&#x2F;span&gt;&lt;span&gt;val = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Id 7
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Which we can take out at any time with &lt;code&gt;getId&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;plainVal &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int
&lt;&#x2F;span&gt;&lt;span&gt;plainVal = getId val
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- plainVal = 7
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Every &lt;code&gt;Monad&lt;&#x2F;code&gt; must first be a &lt;code&gt;Functor&lt;&#x2F;code&gt;, so let&#x27;s define that for &lt;code&gt;Id&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Functor Id where
&lt;&#x2F;span&gt;&lt;span&gt;  fmap f (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Id&lt;&#x2F;span&gt;&lt;span&gt; a) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Id&lt;&#x2F;span&gt;&lt;span&gt; (f a)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;doubled &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Id Int
&lt;&#x2F;span&gt;&lt;span&gt;doubled = fmap (\*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;) val
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- doubled == Id 14
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It must also be an &lt;code&gt;Applicative&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Applicative Id where
&lt;&#x2F;span&gt;&lt;span&gt;  pure = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Id
&lt;&#x2F;span&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Id&lt;&#x2F;span&gt;&lt;span&gt; f) &amp;lt;*&amp;gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Id&lt;&#x2F;span&gt;&lt;span&gt; a) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Id&lt;&#x2F;span&gt;&lt;span&gt; (f a)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;idValue &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Id String
&lt;&#x2F;span&gt;&lt;span&gt;idValue = pure &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Hello!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- idValue = Id &amp;quot;Hello!&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;getLength &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Id Int
&lt;&#x2F;span&gt;&lt;span&gt;getLength = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Id&lt;&#x2F;span&gt;&lt;span&gt; length &amp;lt;\*&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Id &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Dogs&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- getLength == Id 4
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Great stuff. Now we&#x27;ve got &lt;code&gt;Functor&lt;&#x2F;code&gt; and &lt;code&gt;Applicative&lt;&#x2F;code&gt;, we only need one more function to make a &lt;code&gt;Monad&lt;&#x2F;code&gt; instance, and thats &lt;code&gt;bind&lt;&#x2F;code&gt; (or &lt;code&gt;&amp;gt;&amp;gt;=&lt;&#x2F;code&gt;). &lt;code&gt;Bind&lt;&#x2F;code&gt; takes an &lt;code&gt;Id a&lt;&#x2F;code&gt; value, and a function of signature &lt;code&gt;(a -&amp;gt; Id b)&lt;&#x2F;code&gt; that takes the value inside, does something to it, and adds another layer of &lt;code&gt;Id&lt;&#x2F;code&gt;. It then removes the extra layer, leaving us with &lt;code&gt;Id b&lt;&#x2F;code&gt;. In some places it&#x27;s called &lt;code&gt;flatMap&lt;&#x2F;code&gt;, as it maps over the value and then flattens it.&lt;&#x2F;p&gt;
&lt;p&gt;For &lt;code&gt;Id&lt;&#x2F;code&gt;, it takes the value out, and then runs the function on it.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Monad Id where
&lt;&#x2F;span&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Id&lt;&#x2F;span&gt;&lt;span&gt; m) &amp;gt;&amp;gt;= k = k m
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Pretty dull. Let&#x27;s plop a few &lt;code&gt;bind&lt;&#x2F;code&gt; functions together. We&#x27;re using Do Notation, which is a way of making using &lt;code&gt;Monad&lt;&#x2F;code&gt; values easier.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;doubleAndWrap &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int -&amp;gt; Id Int
&lt;&#x2F;span&gt;&lt;span&gt;doubleAndWrap i
&lt;&#x2F;span&gt;&lt;span&gt;  = pure (i \* &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- doubleAndWrap 1 = Id 2
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;doubleAFewTimes &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int -&amp;gt; Id Int
&lt;&#x2F;span&gt;&lt;span&gt;doubleAFewTimes i = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  j &amp;lt;- doubleAndWrap i
&lt;&#x2F;span&gt;&lt;span&gt;  k &amp;lt;- doubleAndWrap j
&lt;&#x2F;span&gt;&lt;span&gt;  l &amp;lt;- doubleAndWrap k
&lt;&#x2F;span&gt;&lt;span&gt;  doubleAndWrap l
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- doubleAFewTimes 10 = 160
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;OK! Not very interesting to be honest. &lt;code&gt;Id&lt;&#x2F;code&gt; doesn&#x27;t really &lt;em&gt;do&lt;&#x2F;em&gt; anything other than make adding numbers up more confusing. Let&#x27;s look at other &lt;code&gt;Monad&lt;&#x2F;code&gt; instances and try and see what they do.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;maybe&quot;&gt;Maybe&lt;&#x2F;h3&gt;
&lt;p&gt;We&#x27;re going to use the &lt;code&gt;Maybe&lt;&#x2F;code&gt; monad to chain together a &lt;code&gt;head&lt;&#x2F;code&gt;-type function that returns the first item &lt;em&gt;if&lt;&#x2F;em&gt; it&#x27;s there.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;firstItem &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Maybe &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;firstItem &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;[] &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing
&lt;&#x2F;span&gt;&lt;span&gt;firstItem (a:\_) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; a
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;head3 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [[[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;]]] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Maybe &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;head3 aaas = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;aas &amp;lt;- firstItem aaas
&lt;&#x2F;span&gt;&lt;span&gt;as &amp;lt;- firstItem aas
&lt;&#x2F;span&gt;&lt;span&gt;a &amp;lt;- firstItem as
&lt;&#x2F;span&gt;&lt;span&gt;pure a
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- head3 []          -- Nothing
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- head3 [[[1,2,3]]] -- Just 1
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Note that as soon as we get a &lt;code&gt;Nothing&lt;&#x2F;code&gt;, the calculation stops, as such.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;either&quot;&gt;Either&lt;&#x2F;h3&gt;
&lt;p&gt;We&#x27;re going to use &lt;code&gt;Either&lt;&#x2F;code&gt; to do some string validation. Here is our error type:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Error
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;TooLong
&lt;&#x2F;span&gt;&lt;span&gt;  | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;ContainsHorse
&lt;&#x2F;span&gt;&lt;span&gt;  | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;IsEmpty
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Show&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Eq&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Ord&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And here are a series of string validation functions that all return either &lt;code&gt;Right String&lt;&#x2F;code&gt; if the string is OK, or &lt;code&gt;Left Error&lt;&#x2F;code&gt; if not.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;isEmpty &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: String -&amp;gt; Either Error String
&lt;&#x2F;span&gt;&lt;span&gt;isEmpty s
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; null s
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;then &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left IsEmpty
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span&gt; s
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;tooLong &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: String -&amp;gt; Either Error String
&lt;&#x2F;span&gt;&lt;span&gt;tooLong s
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; length s &amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;10
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;then &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left TooLong
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span&gt; s
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;containsHorse &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: String -&amp;gt; Either Error String
&lt;&#x2F;span&gt;&lt;span&gt;containsHorse s
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;horse&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;`&lt;&#x2F;span&gt;&lt;span&gt;isInfixOf&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;`&lt;&#x2F;span&gt;&lt;span&gt; s
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;then &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left ContainsHorse
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span&gt; s
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This &lt;code&gt;validate&lt;&#x2F;code&gt; function chains all the validations, returning either the &lt;code&gt;String&lt;&#x2F;code&gt; or the first &lt;code&gt;Error&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;validate &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: String -&amp;gt; Either Error String
&lt;&#x2F;span&gt;&lt;span&gt;validate s = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  t &amp;lt;- isEmpty s
&lt;&#x2F;span&gt;&lt;span&gt;  u &amp;lt;- tooLong t
&lt;&#x2F;span&gt;&lt;span&gt;  containsHorse u
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- validate &amp;quot;&amp;quot;                   == Left IsEmpty
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- validate &amp;quot;bah horse&amp;quot;          == Left ContainsHorse
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- validate &amp;quot;really long string&amp;quot; == Left TooLong
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- validate &amp;quot;Hello&amp;quot;              == Right &amp;quot;Hello&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;list&quot;&gt;List&lt;&#x2F;h3&gt;
&lt;p&gt;The &lt;code&gt;List&lt;&#x2F;code&gt; monad is interesting because the flattening effect of &lt;code&gt;bind&lt;&#x2F;code&gt; means functions that turn values into more lists get flattened into one big list.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;moreList &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int -&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;moreList a = [a - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, a, a + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- moreList 1 == [0, 1, 2]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;lotsMoreList &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int -&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;lotsMoreList a = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  b &amp;lt;- moreList a
&lt;&#x2F;span&gt;&lt;span&gt;  moreList b
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- lotsMoreList 1 == [-1,0,1,0,1,2,1,2,3]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;reader&quot;&gt;Reader&lt;&#x2F;h3&gt;
&lt;p&gt;The &lt;code&gt;Reader&lt;&#x2F;code&gt; monad is used to pass configuration around a program. Here we will define a type for &lt;code&gt;Config&lt;&#x2F;code&gt; and a value for it.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Config
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Config&lt;&#x2F;span&gt;&lt;span&gt; { ipAddress :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;String
&lt;&#x2F;span&gt;&lt;span&gt;           , name :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;String
&lt;&#x2F;span&gt;&lt;span&gt;           }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;config &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Config
&lt;&#x2F;span&gt;&lt;span&gt;config = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Config&lt;&#x2F;span&gt;&lt;span&gt; { ipAddress = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;127.0.0.1&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;                , name = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;localhost&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;                }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;These functions use the &lt;code&gt;Config&lt;&#x2F;code&gt; value in the &lt;code&gt;Reader&lt;&#x2F;code&gt; to make strings, using the &lt;code&gt;ask&lt;&#x2F;code&gt; function to access the config.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;printName &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Reader Config String
&lt;&#x2F;span&gt;&lt;span&gt;printName = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  config &amp;lt;- ask
&lt;&#x2F;span&gt;&lt;span&gt;  pure (&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;the name is &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;lt;&amp;gt; name config)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;printIp &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Reader Config String
&lt;&#x2F;span&gt;&lt;span&gt;printIp = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  config &amp;lt;- ask
&lt;&#x2F;span&gt;&lt;span&gt;  pure (&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;The ip address is &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;lt;&amp;gt; ipAddress config)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;configReader&lt;&#x2F;code&gt; function shows how we can combine different &lt;code&gt;Reader&lt;&#x2F;code&gt; instances with &lt;code&gt;bind&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;configReader &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Reader Config String
&lt;&#x2F;span&gt;&lt;span&gt;configReader = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  ip &amp;lt;- printIp
&lt;&#x2F;span&gt;&lt;span&gt;  name &amp;lt;- printName
&lt;&#x2F;span&gt;&lt;span&gt;  pure (ip &amp;lt;&amp;gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;lt;&amp;gt; name)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;withConfig &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: String
&lt;&#x2F;span&gt;&lt;span&gt;withConfig = runReader configReader config
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- withConfig == &amp;quot;The ip address is 127.0.0.1, the name is localhost&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;writer&quot;&gt;Writer&lt;&#x2F;h3&gt;
&lt;p&gt;The &lt;code&gt;Writer&lt;&#x2F;code&gt; monad accumulates a log as it does computations, by using the &lt;code&gt;tell&lt;&#x2F;code&gt; function to add items.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;addOne &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int -&amp;gt; Writer String Int
&lt;&#x2F;span&gt;&lt;span&gt;addOne i = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  tell &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Add one &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  pure (i + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;timesTwo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int -&amp;gt; Writer String Int
&lt;&#x2F;span&gt;&lt;span&gt;timesTwo i = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  tell &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;times two &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  pure (i \* &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;maths &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int -&amp;gt; Writer String Int
&lt;&#x2F;span&gt;&lt;span&gt;maths i = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  j &amp;lt;- addOne i
&lt;&#x2F;span&gt;&lt;span&gt;  k &amp;lt;- timesTwo j
&lt;&#x2F;span&gt;&lt;span&gt;  pure k
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- runWriter maths 10 == (22, &amp;quot;Add one times two&amp;quot;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;io&quot;&gt;IO&lt;&#x2F;h3&gt;
&lt;p&gt;The &lt;code&gt;IO&lt;&#x2F;code&gt; monad is a very interesting one. Each time we run &lt;code&gt;bind&lt;&#x2F;code&gt;, the value that comes out can be affected by stuff like user input, system time, or other side effects. This is how we can write effectful code in a relatively safe manner. This is how we write code like this that actually interacts with users.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;things &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: IO Unit
&lt;&#x2F;span&gt;&lt;span&gt;things = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;  \_ &amp;lt;- putStrLn &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Hello! What is your name?&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  name &amp;lt;- readLn
&lt;&#x2F;span&gt;&lt;span&gt;  putStrLn (&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Hello, &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++ name)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That is all. I feel this somewhat rushes things a lot, and there is a lot to chew on, but hopefully these examples will help you build up an intuition for how these work.&lt;&#x2F;p&gt;
&lt;p&gt;Further Reading:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;egghead.io&#x2F;lessons&#x2F;javascript-you-ve-been-using-monads&quot;&gt;You&#x27;ve Been Using Monads&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.google.com&#x2F;search?q=monad+tutorial&quot;&gt;Lots More Of This Kind Of Thing&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Datatypes - Either</title>
          <pubDate>Sun, 17 Mar 2019 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/datatypes-either/</link>
          <guid>https://danieljharvey.github.io/datatypes-either/</guid>
          <description xml:base="https://danieljharvey.github.io/datatypes-either/">&lt;p&gt;It&#x27;s one thing to understand typeclasses individually, but another to see them in context. This is the second in a series looking at some common datatypes and see how their instances of the main typeclasses act. We started with one of the simplest, &lt;code&gt;Maybe&lt;&#x2F;code&gt;, and now we move onto it&#x27;s slightly more powerful cousin &lt;code&gt;Either&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;one-thing-or-the-other-thing&quot;&gt;One thing or the other thing&lt;&#x2F;h3&gt;
&lt;p&gt;Here is the data definition for &lt;code&gt;Either&lt;&#x2F;code&gt;. We have used &lt;code&gt;deriving&lt;&#x2F;code&gt; to auto-generate instances of the &lt;code&gt;Eq&lt;&#x2F;code&gt;, &lt;code&gt;Ord&lt;&#x2F;code&gt; and &lt;code&gt;Show&lt;&#x2F;code&gt; typeclasses as we don&#x27;t need anything special going on with them.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Either&lt;&#x2F;span&gt;&lt;span&gt; a b
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left&lt;&#x2F;span&gt;&lt;span&gt; a
&lt;&#x2F;span&gt;&lt;span&gt;  | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span&gt; b
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Eq&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Ord&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Show&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Whilst &lt;code&gt;Maybe&lt;&#x2F;code&gt; only had one type parameter, &lt;code&gt;a&lt;&#x2F;code&gt;, &lt;code&gt;Either&lt;&#x2F;code&gt; has both &lt;code&gt;a&lt;&#x2F;code&gt; and &lt;code&gt;b&lt;&#x2F;code&gt;. If we use the &lt;code&gt;Left&lt;&#x2F;code&gt; constructor, it holds an &lt;code&gt;a&lt;&#x2F;code&gt;, or the &lt;code&gt;Right&lt;&#x2F;code&gt; constructor holds a &lt;code&gt;b&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Here, we are returning either a &lt;code&gt;Left String&lt;&#x2F;code&gt; with some harsh words, or a &lt;code&gt;Right Int&lt;&#x2F;code&gt; with a nice answer to an easy sum.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;incrementOrTellMeOff &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int -&amp;gt; Either String Int
&lt;&#x2F;span&gt;&lt;span&gt;incrementOrTellMeOff i
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; i == &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;6
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;then&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;I hate the number six, why have you done this?&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span&gt; (i + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;incrementOrTellMeOff &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Right 2
&lt;&#x2F;span&gt;&lt;span&gt;incrementOrTellMeOff &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;6 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Left &amp;quot;I hate the number six, why have you done this?&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;functor&quot;&gt;Functor&lt;&#x2F;h3&gt;
&lt;p&gt;Next we&#x27;ll define a &lt;code&gt;functor&lt;&#x2F;code&gt; instance for &lt;code&gt;Either&lt;&#x2F;code&gt;. The important intuition here is that the function &lt;code&gt;f&lt;&#x2F;code&gt; that is provided will only be run on a value inside &lt;code&gt;Right&lt;&#x2F;code&gt;. Anything held in a &lt;code&gt;Left&lt;&#x2F;code&gt; will be returned untouched.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Functor&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Either &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  fmap f (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span&gt; b) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span&gt; (f b) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- run the function over Right
&lt;&#x2F;span&gt;&lt;span&gt;  fmap _ (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left&lt;&#x2F;span&gt;&lt;span&gt; a)  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left&lt;&#x2F;span&gt;&lt;span&gt; a      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- ignore the function for Left
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;fmap (+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right 1&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Right 2
&lt;&#x2F;span&gt;&lt;span&gt;fmap (+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left 1&lt;&#x2F;span&gt;&lt;span&gt;)  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Left 1
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;Functor&lt;&#x2F;code&gt; typeclass is only allowed to affect one type parameter, so anything held in a &lt;code&gt;Left&lt;&#x2F;code&gt; will be returned untouched, so if we want to run functions over that, we&#x27;ll need...&lt;&#x2F;p&gt;
&lt;h3 id=&quot;bifunctor&quot;&gt;Bifunctor&lt;&#x2F;h3&gt;
&lt;p&gt;This instance allows us to map over the left, right or indeed both sides of a datatype. We define it with the &lt;code&gt;bimap&lt;&#x2F;code&gt; function which takes a function for each side.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Bifunctor Either where
&lt;&#x2F;span&gt;&lt;span&gt;  bimap f _ (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left&lt;&#x2F;span&gt;&lt;span&gt; a)  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left&lt;&#x2F;span&gt;&lt;span&gt; (f a)
&lt;&#x2F;span&gt;&lt;span&gt;  bimap _ g (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span&gt; b) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span&gt; (g b)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;bimap (+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) (+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;100&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left 1&lt;&#x2F;span&gt;&lt;span&gt;)   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Left 2
&lt;&#x2F;span&gt;&lt;span&gt;bimap (+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1000&lt;&#x2F;span&gt;&lt;span&gt;) (+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right 1&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Right 2
&lt;&#x2F;span&gt;&lt;span&gt;first (+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right 1&lt;&#x2F;span&gt;&lt;span&gt;)         &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Right 1
&lt;&#x2F;span&gt;&lt;span&gt;first (+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left 1&lt;&#x2F;span&gt;&lt;span&gt;)          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Left 2
&lt;&#x2F;span&gt;&lt;span&gt;second (+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left 1&lt;&#x2F;span&gt;&lt;span&gt;)         &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Left 1
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Just defining &lt;code&gt;bimap&lt;&#x2F;code&gt; also means we get the &lt;code&gt;first&lt;&#x2F;code&gt; function which just maps over the &lt;code&gt;Left&lt;&#x2F;code&gt; side, and the &lt;code&gt;second&lt;&#x2F;code&gt; function which just maps over the &lt;code&gt;Right&lt;&#x2F;code&gt; (so it&#x27;s basically &lt;code&gt;fmap&lt;&#x2F;code&gt; with another name).&lt;&#x2F;p&gt;
&lt;h3 id=&quot;applicative&quot;&gt;Applicative&lt;&#x2F;h3&gt;
&lt;p&gt;The &lt;code&gt;applicative&lt;&#x2F;code&gt; instance for &lt;code&gt;Either&lt;&#x2F;code&gt; has two functions, &lt;code&gt;pure&lt;&#x2F;code&gt; and &lt;code&gt;&amp;lt;*&amp;gt;&lt;&#x2F;code&gt; (also called &lt;code&gt;ap&lt;&#x2F;code&gt;). We use &lt;code&gt;pure&lt;&#x2F;code&gt; to define a default instance of the datatype, so we just take the value and wrap it in &lt;code&gt;Right&lt;&#x2F;code&gt;. The &lt;code&gt;&amp;lt;*&amp;gt;&lt;&#x2F;code&gt; function is used to apply a function inside a &lt;code&gt;Right&lt;&#x2F;code&gt; to a value wrapped in another &lt;code&gt;Right&lt;&#x2F;code&gt;. However, if we hit a &lt;code&gt;Left&lt;&#x2F;code&gt; we return the first one immediately and stop computing.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Applicative&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Either &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  pure a = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span&gt; a
&lt;&#x2F;span&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span&gt; f) &amp;lt;*&amp;gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span&gt; a) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span&gt; (f a)
&lt;&#x2F;span&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span&gt; f) &amp;lt;*&amp;gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left&lt;&#x2F;span&gt;&lt;span&gt; a)  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left&lt;&#x2F;span&gt;&lt;span&gt; a
&lt;&#x2F;span&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left&lt;&#x2F;span&gt;&lt;span&gt; a)  &amp;lt;*&amp;gt; \_        = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left&lt;&#x2F;span&gt;&lt;span&gt; a
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span&gt; (+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) &amp;lt;*&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right 1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Right 2
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span&gt; (+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) &amp;lt;*&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left 1  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Left 1
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left 10    &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;*&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right 1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Left 10
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left 2     &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;*&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left 3  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Left 2
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Sometimes, this behaviour is not what we want as we might want to collect together all of the &lt;code&gt;Left&lt;&#x2F;code&gt; values rather than just the first one found. For this there is a variation on &lt;code&gt;Either&lt;&#x2F;code&gt; called &lt;code&gt;Validation&lt;&#x2F;code&gt;, which we&#x27;ll look at in future.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;monad&quot;&gt;Monad&lt;&#x2F;h3&gt;
&lt;p&gt;The &lt;code&gt;monad&lt;&#x2F;code&gt; instance for &lt;code&gt;Either&lt;&#x2F;code&gt; provides us with &lt;code&gt;&amp;gt;&amp;gt;=&lt;&#x2F;code&gt; (or &lt;code&gt;bind&lt;&#x2F;code&gt;). The behaviour is much like the &lt;code&gt;Applicative&lt;&#x2F;code&gt; above or indeed the &lt;code&gt;Monad&lt;&#x2F;code&gt; instance for &lt;code&gt;Maybe&lt;&#x2F;code&gt; - as soon as an error is found (ie, a &lt;code&gt;Left&lt;&#x2F;code&gt;) we return that error value and shortcircuit the computation, as such.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Monad&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Either &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span&gt; a &amp;gt;&amp;gt;= k = k a
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left&lt;&#x2F;span&gt;&lt;span&gt; e &amp;gt;&amp;gt;= \_ = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left&lt;&#x2F;span&gt;&lt;span&gt; e
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right 10 &lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;gt;= \a -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span&gt; (a + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;))      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Right 11
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;oh no&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;gt;&amp;gt;= \a -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span&gt; (a + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;))  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Left &amp;quot;oh no&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;semigroup&quot;&gt;Semigroup&lt;&#x2F;h3&gt;
&lt;p&gt;This &lt;code&gt;semigroup&lt;&#x2F;code&gt; instance for &lt;code&gt;Either&lt;&#x2F;code&gt; differs from the standard one in &lt;code&gt;Data.Either&lt;&#x2F;code&gt; as, like our &lt;code&gt;Semigroup&lt;&#x2F;code&gt; instance for &lt;code&gt;Maybe&lt;&#x2F;code&gt; it has a constraint allowing us to combine &lt;code&gt;Semigroup&lt;&#x2F;code&gt; values inside. I like this better, having nested &lt;code&gt;Semigroup&lt;&#x2F;code&gt; values all magically combining is very enjoyable to my mind.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Semigroup &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; Semigroup&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Either &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;e a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span&gt; a) &amp;lt;&amp;gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span&gt; b) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span&gt; (a &amp;lt;&amp;gt; b)
&lt;&#x2F;span&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left&lt;&#x2F;span&gt;&lt;span&gt; a)  &amp;lt;&amp;gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left&lt;&#x2F;span&gt;&lt;span&gt; b)  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left&lt;&#x2F;span&gt;&lt;span&gt; a
&lt;&#x2F;span&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left&lt;&#x2F;span&gt;&lt;span&gt; a)  &amp;lt;&amp;gt; b         = b
&lt;&#x2F;span&gt;&lt;span&gt;  a         &amp;lt;&amp;gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left&lt;&#x2F;span&gt;&lt;span&gt; b)  = a
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;] &amp;lt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;6&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Right [1,2,3,4,5,6]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;] &amp;lt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left 1        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Right [1,2,3]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left 1 &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;6&lt;&#x2F;span&gt;&lt;span&gt;]        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Right [4,5,6]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left 1 &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left 2               &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Left 1
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;monoid&quot;&gt;Monoid&lt;&#x2F;h3&gt;
&lt;p&gt;Sadly, because &lt;code&gt;Either&lt;&#x2F;code&gt; has two type parameters rather than &lt;code&gt;Maybe&lt;&#x2F;code&gt;&#x27;s one, we can&#x27;t have a &lt;code&gt;Monoid&lt;&#x2F;code&gt; instance as we won&#x27;t know which value to put in &lt;code&gt;Left&lt;&#x2F;code&gt; to represent nothingness. Oh well.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;foldable&quot;&gt;Foldable&lt;&#x2F;h3&gt;
&lt;p&gt;If we&#x27;d like to extract our value out of this &lt;code&gt;Either&lt;&#x2F;code&gt; context at some point we can use &lt;code&gt;foldable&lt;&#x2F;code&gt;. Note that we still provide a default &lt;code&gt;a&lt;&#x2F;code&gt; in the &lt;code&gt;Left&lt;&#x2F;code&gt; version of the function - the &lt;code&gt;Left&lt;&#x2F;code&gt; values might be a different type to the &lt;code&gt;Right&lt;&#x2F;code&gt; ones so there&#x27;s no guarantee they&#x27;ll be helpful in our fold, hence we still provide a default value.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Foldable&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Either &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;e&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  foldr \_ a (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left&lt;&#x2F;span&gt;&lt;span&gt; e) = a
&lt;&#x2F;span&gt;&lt;span&gt;  foldr f a (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span&gt; b) = f b a
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;foldr &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;(+) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left 10&lt;&#x2F;span&gt;&lt;span&gt;)  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- 1
&lt;&#x2F;span&gt;&lt;span&gt;foldr &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;(+) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right 10&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- 11
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;alternative&quot;&gt;Alternative&lt;&#x2F;h3&gt;
&lt;p&gt;For the same reasons that defining &lt;code&gt;mempty&lt;&#x2F;code&gt; is impossible for &lt;code&gt;Monoid&lt;&#x2F;code&gt;, we can&#x27;t define &lt;code&gt;Alternative&lt;&#x2F;code&gt; for &lt;code&gt;Either&lt;&#x2F;code&gt; because it&#x27;s just too messy. I&#x27;m sorry.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;monadplus&quot;&gt;MonadPlus&lt;&#x2F;h3&gt;
&lt;p&gt;As I&#x27;m sure we all remember, &lt;code&gt;MonadPlus&lt;&#x2F;code&gt; is just &lt;code&gt;Alternative&lt;&#x2F;code&gt; with a more dynamic sounding name, so we don&#x27;t get this one either.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;traversable&quot;&gt;Traversable&lt;&#x2F;h3&gt;
&lt;p&gt;The &lt;code&gt;traversable&lt;&#x2F;code&gt; instance for &lt;code&gt;Either&lt;&#x2F;code&gt; is very similar to &lt;code&gt;Maybe&lt;&#x2F;code&gt;. If &lt;code&gt;traverse&lt;&#x2F;code&gt; is run on a &lt;code&gt;Left&lt;&#x2F;code&gt; it wraps said &lt;code&gt;Left&lt;&#x2F;code&gt; and it&#x27;s value inside whichever &lt;code&gt;Applicative&lt;&#x2F;code&gt; it is used with (the &lt;code&gt;pure&lt;&#x2F;code&gt; function coming from the other type rather than from &lt;code&gt;Either&lt;&#x2F;code&gt;). If we &lt;code&gt;traverse&lt;&#x2F;code&gt; a &lt;code&gt;Right&lt;&#x2F;code&gt; value then the provided function &lt;code&gt;f&lt;&#x2F;code&gt; is run on the &lt;code&gt;Right&lt;&#x2F;code&gt; values the same way in which they worked on &lt;code&gt;Just&lt;&#x2F;code&gt;. Note if we have several &lt;code&gt;Left&lt;&#x2F;code&gt; values, the shortcircuiting behaviour means we only get the first one back, shown here by using the &lt;code&gt;sequence&lt;&#x2F;code&gt; function (which is just &lt;code&gt;traverse id&lt;&#x2F;code&gt;, fact fans).&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Traversable&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Either &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;e&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  traverse \_ (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left&lt;&#x2F;span&gt;&lt;span&gt; e) = pure (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left&lt;&#x2F;span&gt;&lt;span&gt; e)
&lt;&#x2F;span&gt;&lt;span&gt;  traverse f (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span&gt; a) = fmap &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span&gt; (f a)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;sequence [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right 1&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right 2&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Right [1,2]
&lt;&#x2F;span&gt;&lt;span&gt;sequence [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left 1&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right 1&lt;&#x2F;span&gt;&lt;span&gt;]  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Left 1
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you are ever trying to turn a &lt;code&gt;List&lt;&#x2F;code&gt; of &lt;code&gt;Maybe&lt;&#x2F;code&gt; values into a &lt;code&gt;Maybe List&lt;&#x2F;code&gt;, or indeed turn any pair of &lt;code&gt;Applicatives&lt;&#x2F;code&gt; inside out, then &lt;code&gt;sequence&lt;&#x2F;code&gt; is probably what you are looking for. It&#x27;s magic, honestly.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;monadfail&quot;&gt;MonadFail&lt;&#x2F;h3&gt;
&lt;p&gt;It turns out &lt;code&gt;MonadFail&lt;&#x2F;code&gt; is quite fussy about things, and so because the &lt;code&gt;e&lt;&#x2F;code&gt; in &lt;code&gt;Either e a&lt;&#x2F;code&gt; can be a &lt;code&gt;String&lt;&#x2F;code&gt;, but might not be, we can&#x27;t define it. What a pain in the arse, all told.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;right-right-right-right&quot;&gt;Right, right, right, right.&lt;&#x2F;h3&gt;
&lt;p&gt;Anyway, that&#x27;s some things. Tldr; &lt;code&gt;Either&lt;&#x2F;code&gt; isn&#x27;t too dissimilar to &lt;code&gt;Maybe&lt;&#x2F;code&gt; when everything is going well, and a bit different when errors start happening. Also, &lt;code&gt;Either&lt;&#x2F;code&gt; has way less instances than I thought, which was somewhat a relief I have to admit. I can&#x27;t decide whether to do &lt;code&gt;List&lt;&#x2F;code&gt; or something jazzier like &lt;code&gt;Reader&lt;&#x2F;code&gt; next, but it&#x27;ll be something like that. Please note these aren&#x27;t the same definitions as you&#x27;ll find in the Haskell Prelude, as I have tried to write them with an emphasis on clarity&#x2F;simplicity (or indeed go completely off-piste with &lt;code&gt;Semigroup&lt;&#x2F;code&gt;.) By all means check out the originals on &lt;a href=&quot;http:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;base-4.12.0.0&#x2F;docs&#x2F;src&#x2F;Data.Either.html#Either&quot;&gt;Hackage&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;If having skim-read this post you find yourself with strong feelings about it (positive or otherwise) I&#x27;d appreciate you shouting them at my face via the &lt;a href=&quot;&#x2F;contact.html&quot;&gt;usual channels&lt;&#x2F;a&gt;. It&#x27;s lonely down here in the soupy bottom of this old council bin.&lt;&#x2F;p&gt;
&lt;p&gt;That&#x27;s all.&lt;&#x2F;p&gt;
&lt;p&gt;Further reading:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;http:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;base-4.12.0.0&#x2F;docs&#x2F;Data-Either.html&quot;&gt;Data.Either&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Typeclasses - Profunctor</title>
          <pubDate>Tue, 12 Mar 2019 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/typeclasses-profunctor/</link>
          <guid>https://danieljharvey.github.io/typeclasses-profunctor/</guid>
          <description xml:base="https://danieljharvey.github.io/typeclasses-profunctor/">&lt;p&gt;Hello. Let&#x27;s make a box for putting functions in...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;newtype &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;FuncBox&lt;&#x2F;span&gt;&lt;span&gt; b c
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;FuncBox&lt;&#x2F;span&gt;&lt;span&gt; { runFuncBox :: b -&amp;gt; c }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;...and a function...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;length &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: String -&amp;gt; Int
&lt;&#x2F;span&gt;&lt;span&gt;length s = foldl&amp;#39; (\c _ -&amp;gt; c+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt; s
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;...and then let&#x27;s put a function in this box:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;length&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: FuncBox String Int
&lt;&#x2F;span&gt;&lt;span&gt;length&amp;#39; = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;FuncBox&lt;&#x2F;span&gt;&lt;span&gt; length
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Great. A function in a box. You may be concerned that something interesting is going on here, so just to assure you that it&#x27;s not, let&#x27;s run the function using &lt;code&gt;runFuncBox&lt;&#x2F;code&gt;, to reassure ourselves that this box is doing no funny business and we&#x27;re really just unwrapping that &lt;code&gt;newtype&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;length&amp;#39;&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int
&lt;&#x2F;span&gt;&lt;span&gt;length&amp;#39;&amp;#39; = runFuncBox length&amp;#39; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;dog&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- length&amp;#39;&amp;#39; == 3
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;OK. Good stuff. We now have a very longwinded way of running the &lt;code&gt;length&lt;&#x2F;code&gt; function. Good stuff. Big day.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;profunctor-optics.png&quot; alt=&quot;A relatable piece of content to break up this block of text.&quot; title=&quot;A relatable piece of content to break up this block of text.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;the-plot-thickens&quot;&gt;The plot thickens&lt;&#x2F;h3&gt;
&lt;p&gt;Now what if want to run this weird function, but instead of having a &lt;code&gt;String&lt;&#x2F;code&gt; to hand, we only have an &lt;code&gt;Animal&lt;&#x2F;code&gt;...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Animal &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Horse &lt;&#x2F;span&gt;&lt;span&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Dog &lt;&#x2F;span&gt;&lt;span&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Cat
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Show&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I can see you there. You&#x27;re getting excited and jumping straight for our old pal &lt;a href=&quot;&#x2F;posts&#x2F;2018-11-17-typeclasses-contravariant.markdown&quot;&gt;Contravariant&lt;&#x2F;a&gt; aren&#x27;t you. But wait! That&#x27;s not an entirely terrible decision but it turns out there are other problems that we must also solve.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;oh-no&quot;&gt;Oh no&lt;&#x2F;h3&gt;
&lt;p&gt;That&#x27;s right. Heartbreaking, it also turns out that our somewhat contrived API can output numbers, but instead only lists full of a delicous datatype called &lt;code&gt;Egg&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Egg &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Egg
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We can turn any &lt;code&gt;Int&lt;&#x2F;code&gt; into an &lt;code&gt;[Egg]&lt;&#x2F;code&gt; using this excellent function.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;repeatEgg &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int -&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Egg&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;repeatEgg s
&lt;&#x2F;span&gt;&lt;span&gt;  = replicate s &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Egg
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;threeEggs &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: List Egg
&lt;&#x2F;span&gt;&lt;span&gt;threeEggs = repeatEgg &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- threeEggs == [Egg, Egg, Egg]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;OK. So somehow we need to turn an &lt;code&gt;Animal&lt;&#x2F;code&gt; into a &lt;code&gt;List&lt;&#x2F;code&gt; of &lt;code&gt;Egg&lt;&#x2F;code&gt;. We could &lt;code&gt;contramap&lt;&#x2F;code&gt; over our input to turn &lt;code&gt;Animal&lt;&#x2F;code&gt; into &lt;code&gt;String&lt;&#x2F;code&gt;, run the original &lt;code&gt;length&lt;&#x2F;code&gt; function, and then &lt;code&gt;fmap&lt;&#x2F;code&gt; over the result to turn &lt;code&gt;Int&lt;&#x2F;code&gt; into &lt;code&gt;[Egg]&lt;&#x2F;code&gt;. That could work. But what if we could solve this problem using a typeclass, that would be pretty fucking snazzy wouldn&#x27;t it?&lt;&#x2F;p&gt;
&lt;h3 id=&quot;hold-on-he-s-only-gone-and-done-it&quot;&gt;Hold on, he&#x27;s only gone and done it&lt;&#x2F;h3&gt;
&lt;p&gt;Looks like a job for our new friend &lt;code&gt;Profunctor&lt;&#x2F;code&gt; I reckon. Let&#x27;s have a wee look in &lt;code&gt;ghci&lt;&#x2F;code&gt; and see what what the hell it&#x27;s deal is.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; Data.Profunctor
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Data.Profunctor&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; :i Profunctor
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Profunctor&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p&lt;&#x2F;span&gt;&lt;span&gt; :: _ -&amp;gt; _ -&amp;gt; \*) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;dimap &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;c &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;d&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p b c &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p a d
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;lmap &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p b c &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p a c
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;rmap &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;c&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p a b &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p a c
&lt;&#x2F;span&gt;&lt;span&gt;    {-# &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MINIMAL&lt;&#x2F;span&gt;&lt;span&gt; dimap | lmap, rmap #-}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Look at that! It gives a &lt;code&gt;Contravariant&lt;&#x2F;code&gt; with the &lt;code&gt;lmap&lt;&#x2F;code&gt; function, and a regular &lt;code&gt;Functor&lt;&#x2F;code&gt; with &lt;code&gt;rmap&lt;&#x2F;code&gt;, or everything smashed together in this new exciting &lt;code&gt;dimap&lt;&#x2F;code&gt; function. Let&#x27;s look at that a little closer.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;dimap &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;c &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;d&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p b c &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p a d
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That&#x27;s pretty beastly, let&#x27;s put in some real concrete things.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;dimap
&lt;&#x2F;span&gt;&lt;span&gt;  :: (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Animal &lt;&#x2F;span&gt;&lt;span&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;)   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- function to go at the start
&lt;&#x2F;span&gt;&lt;span&gt;  -&amp;gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Int &lt;&#x2F;span&gt;&lt;span&gt;-&amp;gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Egg&lt;&#x2F;span&gt;&lt;span&gt;])       &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- function to go at the end
&lt;&#x2F;span&gt;&lt;span&gt;  -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;FuncBox String Int   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- original container
&lt;&#x2F;span&gt;&lt;span&gt;  -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;FuncBox Animal&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Egg&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- exciting new container
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;OK. Let&#x27;s implement it for our &lt;code&gt;FuncBox&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;instances-binstances-dinstances&quot;&gt;Instances, binstances, dinstances&lt;&#x2F;h3&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;Data.Profunctor
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Profunctor FuncBox where
&lt;&#x2F;span&gt;&lt;span&gt;  dimap before after (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;FuncBox&lt;&#x2F;span&gt;&lt;span&gt; f)
&lt;&#x2F;span&gt;&lt;span&gt;    = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;FuncBox&lt;&#x2F;span&gt;&lt;span&gt; (after . f . before)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That&#x27;s all really. The &lt;code&gt;.&lt;&#x2F;code&gt; is function composition, so therefore our function unwraps the original function and calls it &lt;code&gt;f&lt;&#x2F;code&gt;, then returns a new &lt;code&gt;FuncBox&lt;&#x2F;code&gt; which runs the &lt;code&gt;before&lt;&#x2F;code&gt; function (ie, the &lt;code&gt;a -&amp;gt; b&lt;&#x2F;code&gt; one), then the original &lt;code&gt;f&lt;&#x2F;code&gt; function, and finally the &lt;code&gt;after&lt;&#x2F;code&gt; function (&lt;code&gt;c -&amp;gt; d&lt;&#x2F;code&gt;). The resulting &lt;code&gt;FuncBox&lt;&#x2F;code&gt; can be used exactly as before, and nobody using it knows how secretly clever it is.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s use it to make our all important function for turning an &lt;code&gt;Animal&lt;&#x2F;code&gt; in to a &lt;code&gt;[Egg]&lt;&#x2F;code&gt;, using our &lt;code&gt;length&#x27;&lt;&#x2F;code&gt; and &lt;code&gt;repeatEgg&lt;&#x2F;code&gt; functions from earlier.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;dimapped &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: FuncBox Animal&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Egg&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;dimapped
&lt;&#x2F;span&gt;&lt;span&gt;= dimap show repeatEgg length&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This gives us a new &lt;code&gt;FuncBox&lt;&#x2F;code&gt; that turns &lt;code&gt;Animal&lt;&#x2F;code&gt; into &lt;code&gt;[Egg]&lt;&#x2F;code&gt;, but of course we all know that underneath the hood this function it&#x27;s converting &lt;code&gt;Animal -&amp;gt; String -&amp;gt; Int -&amp;gt; [Egg]&lt;&#x2F;code&gt;. Let&#x27;s see it in action:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;test &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Egg&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;test = runFuncBox dimapped &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Dog
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- test == [Egg, Egg, Egg]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;test2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int
&lt;&#x2F;span&gt;&lt;span&gt;test2 = runFuncBox dimapped &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Horse
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- test2 == [Egg, Egg, Egg, Egg, Egg]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Brilliant. What an absolutely useful non-waste-of-time. OK. So these are stupid examples, but hopefully they give you a rough idea what a &lt;code&gt;Profunctor&lt;&#x2F;code&gt; is under the hood. You often hear of their use in Lenses, as using &lt;code&gt;dimap&lt;&#x2F;code&gt; on a function for changing two small things can make it into a function that changes a small thing inside a much bigger thing, and they compose in the same nice way.&lt;&#x2F;p&gt;
&lt;p&gt;Good stuff.&lt;&#x2F;p&gt;
&lt;p&gt;Feel free to shout your brains about how stupid and wrong I am via the &lt;a href=&quot;&#x2F;contact.html&quot;&gt;usual channels&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Further reading:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.schoolofhaskell.com&#x2F;user&#x2F;griba&#x2F;easier_lenses_profunctor_based_with_mezzolens&quot;&gt;Easier lenses&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;http:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;profunctors-5.3&#x2F;docs&#x2F;Data-Profunctor.html&quot;&gt;Data.Profunctor&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Datatypes - Maybe</title>
          <pubDate>Sun, 17 Feb 2019 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/datatypes-maybe/</link>
          <guid>https://danieljharvey.github.io/datatypes-maybe/</guid>
          <description xml:base="https://danieljharvey.github.io/datatypes-maybe/">&lt;p&gt;It&#x27;s one thing to understand typeclasses individually, but another to see them in context. This is the first in a series where we&#x27;ll look at some common datatypes and see how their instances of the main typeclasses act. We&#x27;re starting with one of the simplest, &lt;code&gt;Maybe&lt;&#x2F;code&gt;, and I hope this will help you get a feel for the way it acts. The plan is to move onto &lt;code&gt;Either&lt;&#x2F;code&gt;, &lt;code&gt;List&lt;&#x2F;code&gt; and then &lt;code&gt;Reader&lt;&#x2F;code&gt;, &lt;code&gt;Writer&lt;&#x2F;code&gt; and &lt;code&gt;State&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;it-really-meant-nothing-frank&quot;&gt;It Really Meant Nothing, Frank&lt;&#x2F;h3&gt;
&lt;p&gt;Let&#x27;s start with a definition. We have used &lt;code&gt;deriving&lt;&#x2F;code&gt; to auto-generate instances of the &lt;code&gt;Eq&lt;&#x2F;code&gt;, &lt;code&gt;Ord&lt;&#x2F;code&gt; and &lt;code&gt;Show&lt;&#x2F;code&gt; typeclasses as we don&#x27;t need anything special going on with them.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Maybe&lt;&#x2F;span&gt;&lt;span&gt; a = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; a | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Eq&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Ord&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Show&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As is hopefully apparently, &lt;code&gt;Maybe&lt;&#x2F;code&gt; can either be a &lt;code&gt;Just&lt;&#x2F;code&gt; with an &lt;code&gt;a&lt;&#x2F;code&gt; wrapped inside, or &lt;code&gt;Nothing&lt;&#x2F;code&gt; which holds no value.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;noNinePlease &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int -&amp;gt; Maybe Int
&lt;&#x2F;span&gt;&lt;span&gt;noNinePlease i
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; i == &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;9
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;then &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; i
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;noNinePlease &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Just 8
&lt;&#x2F;span&gt;&lt;span&gt;noNinePlease &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;9 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Nothing
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;functor&quot;&gt;Functor&lt;&#x2F;h3&gt;
&lt;p&gt;Next we&#x27;ll define a &lt;code&gt;functor&lt;&#x2F;code&gt; instance for &lt;code&gt;Maybe&lt;&#x2F;code&gt;. Essentially, if there is a value inside, let&#x27;s run the provided function over it, and if not, return the same &lt;code&gt;Nothing&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Functor Maybe where
&lt;&#x2F;span&gt;&lt;span&gt;  fmap f (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; a) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; (f a)
&lt;&#x2F;span&gt;&lt;span&gt;  fmap _ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing  &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;fmap (+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just 1&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Just 2
&lt;&#x2F;span&gt;&lt;span&gt;fmap (+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Nothing
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;applicative&quot;&gt;Applicative&lt;&#x2F;h3&gt;
&lt;p&gt;The &lt;code&gt;applicative&lt;&#x2F;code&gt; instance for &lt;code&gt;Maybe&lt;&#x2F;code&gt; has two functions, &lt;code&gt;pure&lt;&#x2F;code&gt; and &lt;code&gt;&amp;lt;*&amp;gt;&lt;&#x2F;code&gt; (also called &lt;code&gt;ap&lt;&#x2F;code&gt;). We use &lt;code&gt;pure&lt;&#x2F;code&gt; to define a default instance of the datatype, so we just take the value and wrap it in &lt;code&gt;Just&lt;&#x2F;code&gt;. The &lt;code&gt;&amp;lt;*&amp;gt;&lt;&#x2F;code&gt; function is used to apply a function inside a &lt;code&gt;Just&lt;&#x2F;code&gt; to a value wrapped in another &lt;code&gt;Just&lt;&#x2F;code&gt;. Therefore, if either of those are a &lt;code&gt;Nothing&lt;&#x2F;code&gt;, that&#x27;s not going to work, so we return &lt;code&gt;Nothing&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Applicative Maybe where
&lt;&#x2F;span&gt;&lt;span&gt;  pure a = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; a
&lt;&#x2F;span&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; f) &amp;lt;*&amp;gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; a) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; (f a)
&lt;&#x2F;span&gt;&lt;span&gt;  _        &amp;lt;*&amp;gt; _        = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;pure &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Just 1
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; (+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) &amp;lt;*&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just 1  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Just 2
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; (+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) &amp;lt;*&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Nothing
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;monad&quot;&gt;Monad&lt;&#x2F;h3&gt;
&lt;p&gt;The &lt;code&gt;monad&lt;&#x2F;code&gt; instance for &lt;code&gt;Maybe&lt;&#x2F;code&gt; has only one additional function, &lt;code&gt;&amp;gt;&amp;gt;=&lt;&#x2F;code&gt; (or &lt;code&gt;bind&lt;&#x2F;code&gt;). The most important thing in the &lt;code&gt;Maybe&lt;&#x2F;code&gt; case is that if we start with a &lt;code&gt;Nothing&lt;&#x2F;code&gt;, then we don&#x27;t bother doing anything, allowing the computation to be shortcircuited, as such.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Monad Maybe where
&lt;&#x2F;span&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; a) &amp;gt;&amp;gt;= k   = k a
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing  &lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;gt;= _   = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just 1 &lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;gt;= (\a -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; (a + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;))  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Just 2
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing &lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;gt;= (\a -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; (a + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;)) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Nothing
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;semigroup&quot;&gt;Semigroup&lt;&#x2F;h3&gt;
&lt;p&gt;The &lt;code&gt;semigroup&lt;&#x2F;code&gt; instance for &lt;code&gt;Maybe&lt;&#x2F;code&gt; is used to combine multiple &lt;code&gt;Maybe&lt;&#x2F;code&gt; values together. An important thing to note is the constraint &lt;code&gt;Semigroup a&lt;&#x2F;code&gt; - this means that for two &lt;code&gt;Maybe&lt;&#x2F;code&gt; values to be combined, the values inside must also have a &lt;code&gt;semigroup&lt;&#x2F;code&gt; instance, allowing them to be combined as well.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Semigroup &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; Semigroup&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Maybe &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; a) &amp;lt;&amp;gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; b) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; (a &amp;lt;&amp;gt; b)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing  &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;gt; a        = a
&lt;&#x2F;span&gt;&lt;span&gt;  a        &amp;lt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing  &lt;&#x2F;span&gt;&lt;span&gt;= a
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;] &amp;lt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;6&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Just [1,2,3,4,5,6]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;]      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Just [1,2,3]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;] &amp;lt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Just [1,2,3]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing           &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Nothing
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;monoid&quot;&gt;Monoid&lt;&#x2F;h3&gt;
&lt;p&gt;Notice that when a &lt;code&gt;Just&lt;&#x2F;code&gt; is combined with a &lt;code&gt;Nothing&lt;&#x2F;code&gt;, we still get a &lt;code&gt;Just&lt;&#x2F;code&gt; value. This is because &lt;code&gt;Nothing&lt;&#x2F;code&gt; is our &lt;code&gt;empty&lt;&#x2F;code&gt; element, meaning that when it is combined to any value it does not change it. Defining this upgrades our &lt;code&gt;semigroup&lt;&#x2F;code&gt; instance into an exciting &lt;code&gt;Monoid&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Semigroup &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; Monoid&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Maybe &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  mempty = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;mempty &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Nothing
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;foldable&quot;&gt;Foldable&lt;&#x2F;h3&gt;
&lt;p&gt;Now carrying all these wrapped values around is great, but at some point we may want to extract values from these &lt;code&gt;Maybe&lt;&#x2F;code&gt; values, so we use &lt;code&gt;foldable&lt;&#x2F;code&gt;. Note the &lt;code&gt;a&lt;&#x2F;code&gt; in the &lt;code&gt;Nothing&lt;&#x2F;code&gt; version of the function - this makes the user of the typeclass provide a default value so that we don&#x27;t end up without a value for &lt;code&gt;Nothing&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Foldable Maybe where
&lt;&#x2F;span&gt;&lt;span&gt;  foldr _ a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing  &lt;&#x2F;span&gt;&lt;span&gt;= a
&lt;&#x2F;span&gt;&lt;span&gt;  foldr f a (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; b) = f b a
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;foldr &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;(+) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1 Nothing   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- 1
&lt;&#x2F;span&gt;&lt;span&gt;foldr &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;(+) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just 10&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- 11
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;alternative&quot;&gt;Alternative&lt;&#x2F;h3&gt;
&lt;p&gt;A good intuition for the &lt;code&gt;Alternative&lt;&#x2F;code&gt; typeclass is that it&#x27;s a like the &lt;code&gt;or&lt;&#x2F;code&gt; operator &lt;code&gt;||&lt;&#x2F;code&gt;. Therefore it can be used to return the first out of a list of values that is wrapped in &lt;code&gt;Just&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Alternative Maybe where
&lt;&#x2F;span&gt;&lt;span&gt;  empty                 = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing
&lt;&#x2F;span&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; a) &amp;lt;|&amp;gt; _        = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; a
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing  &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;|&amp;gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; b) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; b
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing  &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;|&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing  &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;|&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Nothing
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just 1 &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;|&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just 2   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Just 1
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;|&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just 2  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Just 2
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;monadplus&quot;&gt;MonadPlus&lt;&#x2F;h3&gt;
&lt;p&gt;Since we&#x27;re on a roll with defining typeclasses, let&#x27;s plop in a quick instance of &lt;code&gt;MonadPlus&lt;&#x2F;code&gt;, which is basically &lt;code&gt;Alternative&lt;&#x2F;code&gt; with a different name, with &lt;code&gt;mzero&lt;&#x2F;code&gt; replacing &lt;code&gt;empty&lt;&#x2F;code&gt; and &lt;code&gt;mplus&lt;&#x2F;code&gt; replacing &lt;code&gt;&amp;lt;|&amp;gt;&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance MonadPlus Maybe where
&lt;&#x2F;span&gt;&lt;span&gt;  mzero = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  mplus (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; a) _ = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; a
&lt;&#x2F;span&gt;&lt;span&gt;  mplus (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; b) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; b
&lt;&#x2F;span&gt;&lt;span&gt;  mplus _ _ = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;`&lt;&#x2F;span&gt;&lt;span&gt;mplus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;` &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Nothing
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just 1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;`&lt;&#x2F;span&gt;&lt;span&gt;mplus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;` &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Just 1
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;`&lt;&#x2F;span&gt;&lt;span&gt;mplus&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;` &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just 2  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Just 2
&lt;&#x2F;span&gt;&lt;span&gt;mzero                   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Nothing
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;traversable&quot;&gt;Traversable&lt;&#x2F;h3&gt;
&lt;p&gt;The &lt;code&gt;traversable&lt;&#x2F;code&gt; instance for &lt;code&gt;Maybe&lt;&#x2F;code&gt; isn&#x27;t too unusual, if &lt;code&gt;traverse&lt;&#x2F;code&gt; is run on a &lt;code&gt;Nothing&lt;&#x2F;code&gt; it wraps a &lt;code&gt;Nothing&lt;&#x2F;code&gt; inside whichever &lt;code&gt;Applicative&lt;&#x2F;code&gt; it is used with (the &lt;code&gt;pure&lt;&#x2F;code&gt; function coming from the other type rather than from &lt;code&gt;Maybe&lt;&#x2F;code&gt;). If we &lt;code&gt;traverse&lt;&#x2F;code&gt; a &lt;code&gt;Just&lt;&#x2F;code&gt; value then the provided function &lt;code&gt;f&lt;&#x2F;code&gt; is run on the value inside &lt;code&gt;Just&lt;&#x2F;code&gt; which wraps the &lt;code&gt;a&lt;&#x2F;code&gt; in an &lt;code&gt;applicative&lt;&#x2F;code&gt; functor, and we then use &lt;code&gt;fmap&lt;&#x2F;code&gt; to make the value inside a &lt;code&gt;Just&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Traversable Maybe where
&lt;&#x2F;span&gt;&lt;span&gt;  traverse _ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing  &lt;&#x2F;span&gt;&lt;span&gt;= pure &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing
&lt;&#x2F;span&gt;&lt;span&gt;  traverse f (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; a) = fmap &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; (f a)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;traverse (\a -&amp;gt; [a,a]) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- [Nothing]
&lt;&#x2F;span&gt;&lt;span&gt;traverse (\a -&amp;gt; [a,a]) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just 10&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- [Just 10, Just 10]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;monadfail&quot;&gt;MonadFail&lt;&#x2F;h3&gt;
&lt;p&gt;The &lt;code&gt;MonadFail&lt;&#x2F;code&gt; typeclass hasn&#x27;t come up before, but it&#x27;s a very generic way of allowing all computations to fail in a similar way. The &lt;code&gt;fail&lt;&#x2F;code&gt; function has the type signature &lt;code&gt;String -&amp;gt; m a&lt;&#x2F;code&gt;. However because we cannot carry around any values inside &lt;code&gt;Nothing&lt;&#x2F;code&gt; we simply discard the &lt;code&gt;String&lt;&#x2F;code&gt; and return &lt;code&gt;Nothing&lt;&#x2F;code&gt;. The usefulness of this typeclass will become much more apparent with &lt;code&gt;Either&lt;&#x2F;code&gt; and monad transformer stacks. (what? - we&#x27;ll come to it...)&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance MonadFail Maybe where
&lt;&#x2F;span&gt;&lt;span&gt;  fail _ = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;yes&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;gt;&amp;gt;= fail &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Nothing
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;just-a-great-job&quot;&gt;Just &quot;a great job&quot;&lt;&#x2F;h3&gt;
&lt;p&gt;Anyway. This is quite a laborious post but I hope it is somewhat helpful. I intend to do similar ones for &lt;code&gt;Either&lt;&#x2F;code&gt; and &lt;code&gt;List&lt;&#x2F;code&gt; next. Please note these aren&#x27;t the same definitions as you&#x27;ll find in the Haskell Prelude, as I have tried to write them with an emphasis on clarity&#x2F;simplicity. If you are a purist, or just bloody hate clarity, by all means check out the originals on &lt;a href=&quot;http:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;base-4.12.0.0&#x2F;docs&#x2F;src&#x2F;GHC.Base.html#line-854&quot;&gt;Hackage&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;If having skim-read this post you find yourself with strong feelings about it (positive or otherwise) I&#x27;d appreciate you shouting them at my face via the &lt;a href=&quot;&#x2F;contact.html&quot;&gt;usual channels&lt;&#x2F;a&gt;. It&#x27;s lonely out here in the bleak abyss of South East London.&lt;&#x2F;p&gt;
&lt;p&gt;That&#x27;s all.&lt;&#x2F;p&gt;
&lt;p&gt;Further reading:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;http:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;base-4.12.0.0&#x2F;docs&#x2F;Data-Maybe.html&quot;&gt;Data.Maybe&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>A game in Purescript (Part 1 - Semigroups)</title>
          <pubDate>Sun, 17 Feb 2019 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/purescript-semigroups/</link>
          <guid>https://danieljharvey.github.io/purescript-semigroups/</guid>
          <description xml:base="https://danieljharvey.github.io/purescript-semigroups/">&lt;p&gt;Hello! I hope you are well. It has been a little while between posts as I keep starting things and then not finishing them.&lt;&#x2F;p&gt;
&lt;p&gt;My most recent distraction has been remaking a game that I wrote a year or two back called &lt;a href=&quot;http:&#x2F;&#x2F;itistheegg.com&#x2F;&quot;&gt;It Is The Egg&lt;&#x2F;a&gt;, which is an HTML canvas game where eggs roll around and generally have a nice time. I wanted to add more levels and features but I have no strong desire to write Typescript in my free time, so I have made the somewhat foolish and time-sucking decision to port it over to Purescript.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;it-is-the-egg.png&quot; alt=&quot;The original game in it&amp;#39;s full glory.&quot; title=&quot;The original game in it&amp;#39;s full glory&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;So far I&#x27;ve learned a few things, listed thus:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Rewriting an entire game takes a while actually.&lt;&#x2F;li&gt;
&lt;li&gt;Using Purescript means about 30% of the LOC it took in Typescript&lt;&#x2F;li&gt;
&lt;li&gt;A lot of the problems have already been solved by other people&lt;&#x2F;li&gt;
&lt;li&gt;Abstractions are helpful&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Anyhow, I figured concrete useful examples are the most difficult thing to find when explaining functional programming, and since I stumbled across a few in this process I figured I&#x27;d share some. Today I&#x27;m going to example how to describe positions and movement using a &lt;code&gt;semigroup&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Meaningless? Possibly! Perhaps some examples...&lt;&#x2F;p&gt;
&lt;p&gt;So we have a &lt;code&gt;Player&lt;&#x2F;code&gt; type - it describes one of the eggs onscreen. The eggs have a &lt;code&gt;position&lt;&#x2F;code&gt; onscreen, and a &lt;code&gt;direction&lt;&#x2F;code&gt; that they are currently moving in.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Player
&lt;&#x2F;span&gt;&lt;span&gt;  = { position  :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Coord
&lt;&#x2F;span&gt;&lt;span&gt;    , direction :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Coord
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is the &lt;code&gt;Coord&lt;&#x2F;code&gt; type. The eagle-eyed might notice it&#x27;s a &lt;code&gt;newtype&lt;&#x2F;code&gt; rather than a &lt;code&gt;type&lt;&#x2F;code&gt; - the reason for this will be explained shortly.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;newtype &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Coord &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Coord
&lt;&#x2F;span&gt;&lt;span&gt;    { x       :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Int
&lt;&#x2F;span&gt;&lt;span&gt;    , y       :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Int
&lt;&#x2F;span&gt;&lt;span&gt;    , offsetX :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Int
&lt;&#x2F;span&gt;&lt;span&gt;    , offsetY :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Int
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The game board is a grid, so &lt;code&gt;position&lt;&#x2F;code&gt; is a &lt;code&gt;Coord&lt;&#x2F;code&gt; where &lt;code&gt;x&lt;&#x2F;code&gt; and &lt;code&gt;y&lt;&#x2F;code&gt; describe the current square the &lt;code&gt;Player&lt;&#x2F;code&gt; is in, and any movement away from the center is described by &lt;code&gt;offsetX&lt;&#x2F;code&gt; and &lt;code&gt;offsetY&lt;&#x2F;code&gt;. Once a players &lt;code&gt;position&lt;&#x2F;code&gt; offset goes over a certain boundary, we increase or decrease &lt;code&gt;x&lt;&#x2F;code&gt; or &lt;code&gt;y&lt;&#x2F;code&gt; and set &lt;code&gt;offsetX&lt;&#x2F;code&gt; or &lt;code&gt;offsetY&lt;&#x2F;code&gt; back to zero.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;position&lt;&#x2F;code&gt; for the pink egg in the above screenshot would look something like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;position &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Coord
&lt;&#x2F;span&gt;&lt;span&gt;position
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Coord&lt;&#x2F;span&gt;&lt;span&gt; { x:       &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3
&lt;&#x2F;span&gt;&lt;span&gt;          , y:       &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2
&lt;&#x2F;span&gt;&lt;span&gt;          , offsetX: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span&gt;          , offsetY: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;32
&lt;&#x2F;span&gt;&lt;span&gt;          }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;direction&lt;&#x2F;code&gt; is also a &lt;code&gt;Coord&lt;&#x2F;code&gt;, where we use &lt;code&gt;x&lt;&#x2F;code&gt; and &lt;code&gt;y&lt;&#x2F;code&gt; to express which direction the player is headed. Our pink egg above is falling downwards, which would look like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;falling &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Coord
&lt;&#x2F;span&gt;&lt;span&gt;falling
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Coord&lt;&#x2F;span&gt;&lt;span&gt; { x: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span&gt;          , y: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1
&lt;&#x2F;span&gt;&lt;span&gt;          , offsetX: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span&gt;          , offsetY: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span&gt;          }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;offsetX&lt;&#x2F;code&gt; and &lt;code&gt;offsetY&lt;&#x2F;code&gt; don&#x27;t really have much use in this context, but that&#x27;s fine.&lt;&#x2F;p&gt;
&lt;p&gt;So what&#x27;s the use in expressing two slightly different things in the same data type and also what about that &lt;code&gt;newtype&lt;&#x2F;code&gt; and why have we not mentioned &lt;code&gt;semigroup&lt;&#x2F;code&gt; yet?&lt;&#x2F;p&gt;
&lt;p&gt;An abridged version of each game turn goes as follows.&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Look around to see if we are still able to move in direction we want to&lt;&#x2F;li&gt;
&lt;li&gt;If not, change direction&lt;&#x2F;li&gt;
&lt;li&gt;Move the player in whichever direction we&#x27;re now decided on&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;h3 id=&quot;looking-around-the-board&quot;&gt;Looking around the board&lt;&#x2F;h3&gt;
&lt;p&gt;OK. So firstly we need to check around the board to see if we are allowed to carry on moving where we are moving. Without going into the whole mechanics of the game board, let&#x27;s say we have a function that tells us whether a certain &lt;code&gt;Coord&lt;&#x2F;code&gt; on the board is a place we are allowed to go.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;canMove &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Coord -&amp;gt; Boolean
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;What this function does is none of our business at this point - all we need to know is whether the square we intend to move into is happy with our decision to do so.&lt;&#x2F;p&gt;
&lt;p&gt;My first implementation followed the original logic and looked something like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;canIMoveNext &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Player -&amp;gt; Boolean
&lt;&#x2F;span&gt;&lt;span&gt;canIMoveNext player@{ position: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Coord&lt;&#x2F;span&gt;&lt;span&gt; pos, direction: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Coord&lt;&#x2F;span&gt;&lt;span&gt; dir }
&lt;&#x2F;span&gt;&lt;span&gt;  | dir.x &amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0 &lt;&#x2F;span&gt;&lt;span&gt;= canMove ( &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Coord &lt;&#x2F;span&gt;&lt;span&gt;$ pos { x = pos.x - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt; } )
&lt;&#x2F;span&gt;&lt;span&gt;  | dir.x &amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0 &lt;&#x2F;span&gt;&lt;span&gt;= canMove ( &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Coord &lt;&#x2F;span&gt;&lt;span&gt;$ pos { x = pos.x + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt; } )
&lt;&#x2F;span&gt;&lt;span&gt;  | dir.y &amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0 &lt;&#x2F;span&gt;&lt;span&gt;= canMove ( &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Coord &lt;&#x2F;span&gt;&lt;span&gt;$ pos { y = pos.y - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt; } )
&lt;&#x2F;span&gt;&lt;span&gt;  | dir.y &amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0 &lt;&#x2F;span&gt;&lt;span&gt;= canMove ( &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Coord &lt;&#x2F;span&gt;&lt;span&gt;$ pos { y = pos.y + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt; } )
&lt;&#x2F;span&gt;&lt;span&gt;  | otherwise       = true
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&quot;If we are moving left then look to the current square but with x reduced once but if we&#x27;re looking right then look to the current square but with x increased but if we&#x27;re looking up....&quot;.&lt;&#x2F;p&gt;
&lt;p&gt;Quite a laborious thing to read really, and it says nothing of it&#x27;s intentions.&lt;&#x2F;p&gt;
&lt;p&gt;(If you are not familiar with &lt;code&gt;guard&lt;&#x2F;code&gt; syntax, think of this as very similar to &lt;code&gt;select case&lt;&#x2F;code&gt; statement in Javascript.)&lt;&#x2F;p&gt;
&lt;h3 id=&quot;stand-back-i-am-going-to-use-maths&quot;&gt;Stand back, I am going to use Maths.&lt;&#x2F;h3&gt;
&lt;p&gt;OK, so we made that &lt;code&gt;Coord&lt;&#x2F;code&gt; a &lt;code&gt;newtype&lt;&#x2F;code&gt; for a reason right? Let&#x27;s add some typeclass instances to it and get it working for us for a change.&lt;&#x2F;p&gt;
&lt;p&gt;First we must please the gods of boilerplate by deriving some standard instances of &lt;code&gt;Eq&lt;&#x2F;code&gt;, &lt;code&gt;Ord&lt;&#x2F;code&gt; and &lt;code&gt;Show&lt;&#x2F;code&gt;. Purescript needs this to be a little more explicit that Haskell does, sadly.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;derive &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;newtype instance &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;eqCoord&lt;&#x2F;span&gt;&lt;span&gt; :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Eq Coord
&lt;&#x2F;span&gt;&lt;span&gt;derive &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;newtype instance &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ordCoord&lt;&#x2F;span&gt;&lt;span&gt; :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Ord Coord
&lt;&#x2F;span&gt;&lt;span&gt;derive &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;newtype instance &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;showCoord&lt;&#x2F;span&gt;&lt;span&gt; :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Show Coord
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;derive newtype instance&lt;&#x2F;code&gt; means &quot;You know how you&#x27;re a &lt;code&gt;newtype&lt;&#x2F;code&gt; wrapped around something, can you just copy whatever the thing inside does? Ace, thanks.&quot;&lt;&#x2F;p&gt;
&lt;p&gt;This means we can now compare and order things, which is helpful although not the main point here.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s define a &lt;code&gt;semigroup&lt;&#x2F;code&gt;!&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;semigroupCoord&lt;&#x2F;span&gt;&lt;span&gt; :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Semigroup Coord where
&lt;&#x2F;span&gt;&lt;span&gt;  append (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Coord&lt;&#x2F;span&gt;&lt;span&gt; fst) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Coord&lt;&#x2F;span&gt;&lt;span&gt; snd)
&lt;&#x2F;span&gt;&lt;span&gt;    = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Coord&lt;&#x2F;span&gt;&lt;span&gt; { x: fst.x + snd.x
&lt;&#x2F;span&gt;&lt;span&gt;            , y: fst.y + snd.y
&lt;&#x2F;span&gt;&lt;span&gt;            , offsetX: fst.offsetX + snd.offsetX
&lt;&#x2F;span&gt;&lt;span&gt;            , offsetY: fst.offsetY + snd.offsetY
&lt;&#x2F;span&gt;&lt;span&gt;            }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;(Notice that unlike Haskell, Purescript likes us to give our instances names, hence we have chosen the olympically dull &lt;code&gt;semigroupCoord&lt;&#x2F;code&gt;. It could be &lt;code&gt;pintsOfCream&lt;&#x2F;code&gt; or &lt;code&gt;indepedenceDayIsPrettyUnderatedAsFarAsBlockbustersGo&lt;&#x2F;code&gt;, go absolutely wild if you like.)&lt;&#x2F;p&gt;
&lt;p&gt;So what does this &lt;code&gt;append&lt;&#x2F;code&gt; function we have defined do then?&lt;&#x2F;p&gt;
&lt;p&gt;It takes two &lt;code&gt;Coord&lt;&#x2F;code&gt; values (&lt;code&gt;fst&lt;&#x2F;code&gt; and &lt;code&gt;snd&lt;&#x2F;code&gt; in this case) - adds up &lt;code&gt;x&lt;&#x2F;code&gt;, &lt;code&gt;y&lt;&#x2F;code&gt;, &lt;code&gt;offsetX&lt;&#x2F;code&gt; and &lt;code&gt;offsetY&lt;&#x2F;code&gt; - then makes and returns a new &lt;code&gt;Coord&lt;&#x2F;code&gt;. Therefore we can do stuff like add a &lt;code&gt;position&lt;&#x2F;code&gt; and a &lt;code&gt;direction&lt;&#x2F;code&gt; together.&lt;&#x2F;p&gt;
&lt;p&gt;In the above example of our pink egg, and our &lt;code&gt;position&lt;&#x2F;code&gt; and &lt;code&gt;falling&lt;&#x2F;code&gt; values defined above, we can check whether we&#x27;re OK to keep falling downwards as such.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;canIMoveDown &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Boolean
&lt;&#x2F;span&gt;&lt;span&gt;canIMoveDown = canMove (position &amp;lt;&amp;gt; falling)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- canIMoveDown == true (according to the picture)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;(Note we&#x27;re using &lt;code&gt;&amp;lt;&amp;gt;&lt;&#x2F;code&gt; which is another name for the &lt;code&gt;append&lt;&#x2F;code&gt; function, we could just as easily have written &lt;code&gt;canMove (append position falling)&lt;&#x2F;code&gt; but I think the little &lt;code&gt;&amp;lt;&amp;gt;&lt;&#x2F;code&gt; looks nicer.)&lt;&#x2F;p&gt;
&lt;h3 id=&quot;looking-around-the-board-with-maths&quot;&gt;Looking around the board with Maths&lt;&#x2F;h3&gt;
&lt;p&gt;Back to our implementation of &lt;code&gt;canIMoveNext&lt;&#x2F;code&gt;, here is the new version that combines our player&#x27;s &lt;code&gt;direction&lt;&#x2F;code&gt; and &lt;code&gt;position&lt;&#x2F;code&gt; in a nice tidy way.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;canIMoveTwo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Player -&amp;gt; Boolean
&lt;&#x2F;span&gt;&lt;span&gt;canIMoveTwo player
&lt;&#x2F;span&gt;&lt;span&gt;  = canMove (player.position &amp;lt;&amp;gt; player.direction)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Nice.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;changing-direction&quot;&gt;Changing direction&lt;&#x2F;h3&gt;
&lt;p&gt;What if our egg hits a wall or something? What should we do then? It should be as easy as reversing the &lt;code&gt;direction&lt;&#x2F;code&gt;, right?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;invert &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Coord -&amp;gt; Coord
&lt;&#x2F;span&gt;&lt;span&gt;invert (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Coord&lt;&#x2F;span&gt;&lt;span&gt; coord)
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Coord&lt;&#x2F;span&gt;&lt;span&gt; { x: (-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) * coord.x
&lt;&#x2F;span&gt;&lt;span&gt;          , y: (-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) * coord.y
&lt;&#x2F;span&gt;&lt;span&gt;          , offsetX: (-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) * coord.offsetX
&lt;&#x2F;span&gt;&lt;span&gt;          , offsetY: (-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) * coord.offsetY
&lt;&#x2F;span&gt;&lt;span&gt;          }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Therefore we can use it on our player like thus:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;turnAround &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Player -&amp;gt; Player
&lt;&#x2F;span&gt;&lt;span&gt;turnAround player
&lt;&#x2F;span&gt;&lt;span&gt;  = player { direction = invert player.direction }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And wrap up the entire logic for the turn like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;updateDirection &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Player -&amp;gt; Player
&lt;&#x2F;span&gt;&lt;span&gt;updateDirection player
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; canMove player.position
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;then&lt;&#x2F;span&gt;&lt;span&gt; player
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else&lt;&#x2F;span&gt;&lt;span&gt; turnAround player
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;(Readers with a keen eye will notice we did not use anything particularly clever or typeclass based here, I just included it for completeness.)&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;eggy2.png&quot; alt=&quot;Another screenshot of the original game to break up the large amount of text.&quot; title=&quot;Another screenshot of the original game to break up the large amount of text.&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;actually-moving-that-egg-around&quot;&gt;Actually moving that egg around&lt;&#x2F;h3&gt;
&lt;p&gt;Let&#x27;s get back to moving the egg around. Once we&#x27;ve worked out that our egg is moving somewhere it is allowed to, we need to actually update the &lt;code&gt;offsetX&lt;&#x2F;code&gt; and &lt;code&gt;offsetY&lt;&#x2F;code&gt; in it&#x27;s &lt;code&gt;position&lt;&#x2F;code&gt; so that&#x27;s in the new location and ready to go through all this hell again. As web browsers are flaky at best, instead of moving by a set amount the actually amount to increment the change depends on how much time has passed since the last frame. Therefore we need a function that takes the amount to move and a &lt;code&gt;Player&lt;&#x2F;code&gt;, and returns a new &lt;code&gt;Player&lt;&#x2F;code&gt; that has moved in some way.&lt;&#x2F;p&gt;
&lt;p&gt;Here, in a similar manner to our earlier function, is something like my original painful version.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;incrementPlayerPosition &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int -&amp;gt; Player -&amp;gt; Player
&lt;&#x2F;span&gt;&lt;span&gt;incrementPlayerPosition moveAmount player@{ position: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Coord&lt;&#x2F;span&gt;&lt;span&gt; pos, direction: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Coord&lt;&#x2F;span&gt;&lt;span&gt; dir }
&lt;&#x2F;span&gt;&lt;span&gt;  = player { position = newPosition }
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;    newPosition
&lt;&#x2F;span&gt;&lt;span&gt;      | dir.x &amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0 &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Coord &lt;&#x2F;span&gt;&lt;span&gt;$ pos { offsetX = pos.offsetX - moveAmount }
&lt;&#x2F;span&gt;&lt;span&gt;      | dir.x &amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0 &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Coord &lt;&#x2F;span&gt;&lt;span&gt;$ pos { offsetX = pos.offsetX + moveAmount }
&lt;&#x2F;span&gt;&lt;span&gt;      | dir.y &amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0 &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Coord &lt;&#x2F;span&gt;&lt;span&gt;$ pos { offsetY = pos.offsetY - moveAmount }
&lt;&#x2F;span&gt;&lt;span&gt;      | dir.y &amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0 &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Coord &lt;&#x2F;span&gt;&lt;span&gt;$ pos { offsetY = pos.offsetY + moveAmount }
&lt;&#x2F;span&gt;&lt;span&gt;      | otherwise = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Coord&lt;&#x2F;span&gt;&lt;span&gt; pos
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It should work but it&#x27;s a bit terrifying. Let&#x27;s break it up and get some of that sweet &lt;code&gt;semigroup&lt;&#x2F;code&gt; magic working for us.&lt;&#x2F;p&gt;
&lt;p&gt;Firstly, a helper function - this takes an &lt;code&gt;Int&lt;&#x2F;code&gt; and a &lt;code&gt;Coord&lt;&#x2F;code&gt; and creates a new &lt;code&gt;Coord&lt;&#x2F;code&gt; that describes the movement we want the egg to do.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;createMoveCoord &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int -&amp;gt; Coord -&amp;gt; Coord
&lt;&#x2F;span&gt;&lt;span&gt;createMoveCoord amount (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Coord&lt;&#x2F;span&gt;&lt;span&gt; coord)
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Coord&lt;&#x2F;span&gt;&lt;span&gt; { x: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span&gt;          , y: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span&gt;          , offsetX: amount * coord.x
&lt;&#x2F;span&gt;&lt;span&gt;          , offsetY: amount * coord.y
&lt;&#x2F;span&gt;&lt;span&gt;          }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Therefore if we take the &lt;code&gt;falling&lt;&#x2F;code&gt; value from earlier, and a moving amount of &lt;code&gt;20&lt;&#x2F;code&gt;, it would create something like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;downMove &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Coord
&lt;&#x2F;span&gt;&lt;span&gt;downMove
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Coord&lt;&#x2F;span&gt;&lt;span&gt; { x: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span&gt;          , y: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span&gt;          , offsetX: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span&gt;          , offsetY: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;20
&lt;&#x2F;span&gt;&lt;span&gt;          }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;With our helpful helper function in hand, and a dash of &lt;code&gt;&amp;lt;&amp;gt;&lt;&#x2F;code&gt;, we can now rewrite our move function like thus:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;incrementPlayerDirection &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int -&amp;gt; Player -&amp;gt; Player
&lt;&#x2F;span&gt;&lt;span&gt;incrementPlayerDirection amount player
&lt;&#x2F;span&gt;&lt;span&gt;  = player { position = newPosition }
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;newPosition &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Coord
&lt;&#x2F;span&gt;&lt;span&gt;    newPosition
&lt;&#x2F;span&gt;&lt;span&gt;      = player.position &amp;lt;&amp;gt; moveCoord
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;moveCoord &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Coord
&lt;&#x2F;span&gt;&lt;span&gt;    moveCoord
&lt;&#x2F;span&gt;&lt;span&gt;      = createMoveCoord amount player.direction
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So here we&#x27;re creating a &lt;code&gt;Coord&lt;&#x2F;code&gt; for the movement (called &lt;code&gt;moveCoord&lt;&#x2F;code&gt;) - and then using the &lt;code&gt;&amp;lt;&amp;gt;&lt;&#x2F;code&gt; function to combine it with the current &lt;code&gt;position&lt;&#x2F;code&gt; to make a new &lt;code&gt;Coord&lt;&#x2F;code&gt; called &lt;code&gt;newPosition&lt;&#x2F;code&gt;, and then making a new &lt;code&gt;Player&lt;&#x2F;code&gt; with that &lt;code&gt;position&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;putting-it-all-together&quot;&gt;Putting it all together&lt;&#x2F;h3&gt;
&lt;p&gt;Now we&#x27;ve created all our functions, we can plop them together like this to do the whole move:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;doMove &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int -&amp;gt; Player -&amp;gt; Player
&lt;&#x2F;span&gt;&lt;span&gt;doMove amount
&lt;&#x2F;span&gt;&lt;span&gt;  = updateDirection
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;gt;&amp;gt;&amp;gt; incrementPlayerDirection amount
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Easy as pie.&lt;&#x2F;p&gt;
&lt;p&gt;&quot;But I thought we&#x27;d actually draw one of those attractive looking eggs and instead we&#x27;ve just done some shitty maths&quot;, I hear you say. OK, OK, sure, I get it, you want adventure, you want action. We&#x27;ll come to it soon, I promise.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>IO, IO, it&#x27;s off to test we go</title>
          <pubDate>Sun, 27 Jan 2019 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/testing-io/</link>
          <guid>https://danieljharvey.github.io/testing-io/</guid>
          <description xml:base="https://danieljharvey.github.io/testing-io/">&lt;p&gt;Let&#x27;s say we have a function that uses the &lt;code&gt;IO&lt;&#x2F;code&gt; monad that we want to test. Now you may be aware that &lt;code&gt;IO&lt;&#x2F;code&gt; is the place in which all the bad things happen that aren&#x27;t supposed to happen in Haskell. Things like &lt;em&gt;Database Connections&lt;&#x2F;em&gt;, and &lt;em&gt;Mutable Global Variables&lt;&#x2F;em&gt; and (worst of all) &lt;em&gt;Actual User Interactions&lt;&#x2F;em&gt;. It doesn&#x27;t bear thinking about to be honest.&lt;&#x2F;p&gt;
&lt;p&gt;However, mock dramatics aside, &lt;code&gt;IO&lt;&#x2F;code&gt; is the part of our code where &lt;em&gt;Side Effects&lt;&#x2F;em&gt; happen, meaning stuff is a lot more difficult to write tests for, given our functions may not always return the things we expect them to. Last time, we spoke about &lt;a href=&quot;&#x2F;posts&#x2F;2019-01-21-polymorphism.markdown&quot;&gt;polymorphism&lt;&#x2F;a&gt; - and this seems like a nice opportunity to expand on that and use our new polymorphic powers to make some code easier to test.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;contrived-example&quot;&gt;Contrived example&lt;&#x2F;h3&gt;
&lt;p&gt;Here is some code that uses &lt;code&gt;getCurrentTime&lt;&#x2F;code&gt; from the &lt;code&gt;Data.Time.Clock&lt;&#x2F;code&gt; library to fetch the current system time and then work out whether it is time to stop working and eat some lunch.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import qualified &lt;&#x2F;span&gt;&lt;span&gt;Data.Time.Clock     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;as &lt;&#x2F;span&gt;&lt;span&gt;Clock
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import qualified &lt;&#x2F;span&gt;&lt;span&gt;Data.Time.Calendar  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;as &lt;&#x2F;span&gt;&lt;span&gt;Cal
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import qualified &lt;&#x2F;span&gt;&lt;span&gt;Data.Time.LocalTime &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;as &lt;&#x2F;span&gt;&lt;span&gt;Time
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;getHour &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Clock&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;UTCTime -&amp;gt; Int
&lt;&#x2F;span&gt;&lt;span&gt;getHour = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Time&lt;&#x2F;span&gt;&lt;span&gt;.todHour
&lt;&#x2F;span&gt;&lt;span&gt;        . &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Time&lt;&#x2F;span&gt;&lt;span&gt;.timeToTimeOfDay
&lt;&#x2F;span&gt;&lt;span&gt;        . &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Clock&lt;&#x2F;span&gt;&lt;span&gt;.utctDayTime
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;isItLunchTime &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: IO Bool
&lt;&#x2F;span&gt;&lt;span&gt;isItLunchTime
&lt;&#x2F;span&gt;&lt;span&gt;  = lunchCheck &amp;lt;$&amp;gt; getHour &amp;lt;$&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Clock&lt;&#x2F;span&gt;&lt;span&gt;.getCurrentTime
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;      lunchCheck hr = hr &amp;gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;12&lt;&#x2F;span&gt;&lt;span&gt; &amp;amp;&amp;amp; hr &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;14
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;getHour&lt;&#x2F;code&gt; function is a simple pure function helper - mainly we&#x27;ll be looking at &lt;code&gt;isItLunchTime&lt;&#x2F;code&gt;. Let&#x27;s look at the type signature again:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;isItLunchTime &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: IO Bool
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This function has no inputs, and the &lt;code&gt;IO Bool&lt;&#x2F;code&gt; return type means that it will return &lt;code&gt;True&lt;&#x2F;code&gt; or &lt;code&gt;False&lt;&#x2F;code&gt;, wrapped in the &lt;code&gt;IO&lt;&#x2F;code&gt; type to remind us that evil has been done and the gods of referential transparency have not been appeased.&lt;&#x2F;p&gt;
&lt;p&gt;If you are thinking something like &quot;can we not just extract the pure functions, test those, and not bother about the rest?&quot;, then Don&#x27;t Read On because although that&#x27;s Not A Terrible Idea we&#x27;re absolutely Not Doing That.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;injection-based-solution&quot;&gt;Injection-based solution&lt;&#x2F;h3&gt;
&lt;p&gt;In other languages, the solution to making stuff more testable is to use &lt;code&gt;Dependency Injection&lt;&#x2F;code&gt; to pass in any functions that use side effects, allowing mock versions to be passed in when testing.&lt;&#x2F;p&gt;
&lt;p&gt;This version of the function lets us pass in a time fetching function (such as &lt;code&gt;getCurrentTime&lt;&#x2F;code&gt;) instead.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;injectableLunch &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: IO Clock&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;UTCTime -&amp;gt; IO Bool
&lt;&#x2F;span&gt;&lt;span&gt;injectableLunch getCurrentTime
&lt;&#x2F;span&gt;&lt;span&gt;  = lunchCheck &amp;lt;$&amp;gt; getHour &amp;lt;$&amp;gt; getCurrentTime
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;      lunchCheck hr = hr &amp;gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;12&lt;&#x2F;span&gt;&lt;span&gt; &amp;amp;&amp;amp; hr &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;14
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now, instead of calling &lt;code&gt;isItLunchTime&lt;&#x2F;code&gt;, we&#x27;d call &lt;code&gt;injectableLunch Clock.getCurrentTime&lt;&#x2F;code&gt;. It&#x27;s still not much more testable though because of that &lt;code&gt;IO&lt;&#x2F;code&gt; though. Does it need to be &lt;code&gt;IO&lt;&#x2F;code&gt; though? What if we generalise the type signature though? Instead of &lt;code&gt;IO&lt;&#x2F;code&gt;, let&#x27;s just use any old &lt;code&gt;Monad&lt;&#x2F;code&gt; and see if that works.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;testableLunch &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Monad &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;m&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;m &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Clock&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;UTCTime -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;m &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Bool
&lt;&#x2F;span&gt;&lt;span&gt;testableLunch getCurrentTime
&lt;&#x2F;span&gt;&lt;span&gt;  = lunchCheck &amp;lt;$&amp;gt; getHour &amp;lt;$&amp;gt; getCurrentTime
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;      lunchCheck hr = hr &amp;gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;12&lt;&#x2F;span&gt;&lt;span&gt; &amp;amp;&amp;amp; hr &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;14
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Because we&#x27;re not actually running any functions that require the &lt;code&gt;IO&lt;&#x2F;code&gt; monad (ie, &lt;code&gt;readFile&lt;&#x2F;code&gt;, &lt;code&gt;putStrLn&lt;&#x2F;code&gt;) any monad is totally fine for this function. So long as the Monad &lt;code&gt;m&lt;&#x2F;code&gt; of our passed in time-getting function matches the Monad &lt;code&gt;m&lt;&#x2F;code&gt; of the return value, then this function is having a Great Time.&lt;&#x2F;p&gt;
&lt;p&gt;As a slight detour, let&#x27;s make some fake &lt;code&gt;UTCTime&lt;&#x2F;code&gt; values for testing:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;baseTestTime &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Clock&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;UTCTime
&lt;&#x2F;span&gt;&lt;span&gt;baseTestTime
&lt;&#x2F;span&gt;&lt;span&gt;  = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Clock&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;UTCTime
&lt;&#x2F;span&gt;&lt;span&gt;      { &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Clock&lt;&#x2F;span&gt;&lt;span&gt;.utctDay     = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Cal&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;ModifiedJulianDay 12000
&lt;&#x2F;span&gt;&lt;span&gt;      , &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Clock&lt;&#x2F;span&gt;&lt;span&gt;.utctDayTime = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span&gt;      }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;lunchTestTime &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Clock&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;UTCTime
&lt;&#x2F;span&gt;&lt;span&gt;lunchTestTime
&lt;&#x2F;span&gt;&lt;span&gt;  = baseTestTime { &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Clock&lt;&#x2F;span&gt;&lt;span&gt;.utctDayTime = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;44000&lt;&#x2F;span&gt;&lt;span&gt; }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Excellent. What a nice couple of examples. For more information about what these do, check out the &lt;a href=&quot;http:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;time-1.9.2&quot;&gt;time&lt;&#x2F;a&gt; library.&lt;&#x2F;p&gt;
&lt;p&gt;Now we have some test values, we can then make functions to test our &lt;code&gt;testableLunch&lt;&#x2F;code&gt; function with a safer Monad like &lt;code&gt;Identity&lt;&#x2F;code&gt;. &lt;code&gt;Identity&lt;&#x2F;code&gt; is the Do Nothing monad, it has no special characteristics and does nothing for any particular interest.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;testNotLunch &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Identity Bool
&lt;&#x2F;span&gt;&lt;span&gt;testNotLunch = testableLunch (pure baseTestTime)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Identity False
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;testIsLunch &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Identity Bool
&lt;&#x2F;span&gt;&lt;span&gt;testIsLunch = testableLunch (pure lunchTestTime)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Identity True
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;(What&#x27;s &lt;code&gt;pure&lt;&#x2F;code&gt;? It&#x27;s just a function that wraps a value inside the default Monad context, so in this case, turns &lt;code&gt;&quot;dog&quot;&lt;&#x2F;code&gt; into &lt;code&gt;Identity &quot;dog&quot;&lt;&#x2F;code&gt; etc)&lt;&#x2F;p&gt;
&lt;p&gt;Then when we want to export the function for use with &lt;code&gt;IO&lt;&#x2F;code&gt;, we can export this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;isItLunchTime3 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: IO Bool
&lt;&#x2F;span&gt;&lt;span&gt;isItLunchTime3 = testableLunch &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Clock&lt;&#x2F;span&gt;&lt;span&gt;.getCurrentTime
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Seems pretty OK. What about a more idiomatic Haskell approach using our pal typeclasses?&lt;&#x2F;p&gt;
&lt;h3 id=&quot;testing-io-with-a-typeclass&quot;&gt;Testing IO with a typeclass&lt;&#x2F;h3&gt;
&lt;p&gt;Another way to do this is capture the idea of &quot;Monad That Knows What The Time Is&quot; using a typeclass.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s call ours &lt;code&gt;MonadTime&lt;&#x2F;code&gt;, because we lack imagination:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Monad &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;m&lt;&#x2F;span&gt;&lt;span&gt; =&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;MonadTime &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;m &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;getTheTimePlease &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;m &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Clock&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;UTCTime
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Note the constraint - we can only make instances of &lt;code&gt;MonadTime&lt;&#x2F;code&gt; for things that are already an instance of &lt;code&gt;Monad&lt;&#x2F;code&gt;. By writing instances of &lt;code&gt;MonadTime&lt;&#x2F;code&gt; for &lt;code&gt;IO&lt;&#x2F;code&gt; and &lt;code&gt;Identity&lt;&#x2F;code&gt;, we can tell Haskell what to do when it&#x27;s asked for the time.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance MonadTime Identity where
&lt;&#x2F;span&gt;&lt;span&gt;  getTheTimePlease = pure lunchTestTime
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance MonadTime IO where
&lt;&#x2F;span&gt;&lt;span&gt;  getTheTimePlease = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Clock&lt;&#x2F;span&gt;&lt;span&gt;.getCurrentTime
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;Identity&lt;&#x2F;code&gt; will return a static value, and &lt;code&gt;IO&lt;&#x2F;code&gt; will use the actual function.&lt;&#x2F;p&gt;
&lt;p&gt;Here&#x27;s our new version of the lunch function. Notice we&#x27;ve changed our constraint from &lt;code&gt;Monad m&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;classyLunch &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MonadTime &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;m&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;m &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Bool
&lt;&#x2F;span&gt;&lt;span&gt;classyLunch
&lt;&#x2F;span&gt;&lt;span&gt;  = lunchCheck &amp;lt;$&amp;gt; getHour &amp;lt;$&amp;gt; getTheTimePlease
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;      lunchCheck hr = hr &amp;gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;12&lt;&#x2F;span&gt;&lt;span&gt; &amp;amp;&amp;amp; hr &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;14
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now, depending on the context in which we use &lt;code&gt;classyLunch&lt;&#x2F;code&gt;, it&#x27;ll do different things.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;testClassyLunch &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Identity Bool
&lt;&#x2F;span&gt;&lt;span&gt;testClassyLunch = classyLunch
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;ioClassyLunch &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: IO Bool
&lt;&#x2F;span&gt;&lt;span&gt;ioClassyLunch = classyLunch
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Hooray! Our contrived function is now testable and also ready for production. Great stuff.&lt;&#x2F;p&gt;
&lt;p&gt;This concept of different typeclasses for Monads is a key part of what is called &lt;code&gt;mtl&lt;&#x2F;code&gt; style of code, which we will come to once we&#x27;ve looked at Monad Transformers.&lt;&#x2F;p&gt;
&lt;p&gt;Further reading:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;http:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;time-1.9.2&quot;&gt;Time library&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;commentedcode.org&#x2F;blog&#x2F;2017&#x2F;08&#x2F;21&#x2F;better-io-testing-through-monads&#x2F;&quot;&gt;Better IO testing through Monads&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Why The Hell Should I Care About Polymorphism?</title>
          <pubDate>Mon, 21 Jan 2019 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/polymorphism/</link>
          <guid>https://danieljharvey.github.io/polymorphism/</guid>
          <description xml:base="https://danieljharvey.github.io/polymorphism/">&lt;p&gt;Let&#x27;s talk about type signatures.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;hello &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: String -&amp;gt; Int -&amp;gt; String
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Pretty clear what is going on, right? Seems fine. What about this lad?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;wtflol &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Show &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Num &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That&#x27;s pretty unhelpful to be honest.&lt;&#x2F;p&gt;
&lt;p&gt;What are these letters doing in our types when we wanted, well, types?&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s work up from a simple one to a stupid one and maybe learn something along the way. Good? Great.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;probably-the-worst-function-ever&quot;&gt;Probably the worst function ever&lt;&#x2F;h3&gt;
&lt;p&gt;Here is a type signature for a mysterious function:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;spooky &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Given any &lt;code&gt;a&lt;&#x2F;code&gt;, and knowing nothing about that &lt;code&gt;a&lt;&#x2F;code&gt;, what is the only thing that we can do with it?&lt;&#x2F;p&gt;
&lt;p&gt;If you guessed &quot;just return it&quot;, then yes, you were right. It&#x27;s the classic &lt;code&gt;identity&lt;&#x2F;code&gt; function.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;identity &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;identity a = a
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- identity 10 == 10
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;What do you think this function does?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;mystery &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Since we don&#x27;t know anything about &lt;code&gt;b&lt;&#x2F;code&gt;, and the function returns an &lt;code&gt;a&lt;&#x2F;code&gt;, then all we can do is ignore the &lt;code&gt;b&lt;&#x2F;code&gt; altogether. This is called the &lt;code&gt;const&lt;&#x2F;code&gt; function, and is used when we have to want to make a &lt;code&gt;map&lt;&#x2F;code&gt; function do not very much at all.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;const a _ = a
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- const &amp;quot;dog&amp;quot; 100 == &amp;quot;dog&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Hopefully the pattern you are seeing here is that if we know nothing about our variables, then there is actually very little we can do with them.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;lists-of-mysterious-things&quot;&gt;Lists of mysterious things&lt;&#x2F;h3&gt;
&lt;p&gt;A place most people come across polymorphism quite early on in their Haskell careers is in stuff like lists. Instead of one function that works on &lt;code&gt;List String&lt;&#x2F;code&gt;, and another one &lt;code&gt;List Int&lt;&#x2F;code&gt;, and another on &lt;code&gt;List (Maybe Tuple (Int, String))&lt;&#x2F;code&gt; we can use polymorphism to act upon the structure of the list itself but not the items inside.&lt;&#x2F;p&gt;
&lt;p&gt;With this in mind, and given that &lt;code&gt;[a]&lt;&#x2F;code&gt; means a &lt;code&gt;List&lt;&#x2F;code&gt; that is full of zero or more &lt;code&gt;a&lt;&#x2F;code&gt; values, what possible things could this function do to our &lt;code&gt;[a]&lt;&#x2F;code&gt;?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;thing &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;According to the function search engine &lt;a href=&quot;https:&#x2F;&#x2F;www.haskell.org&#x2F;hoogle&#x2F;?hoogle=%5Ba%5D+-%3E+%5Ba%5D&quot;&gt;Hoogle&lt;&#x2F;a&gt; - not a huge amount. It lists four functions that match this signature - and two of them throw exceptions if passed an empty list so they&#x27;re not the kind of functions that we like to spend our time with. This leaves:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;cycle&lt;&#x2F;code&gt; - this repeats the list of values infinitely.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;reverse&lt;&#x2F;code&gt; - reverses the order of the list.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;The important thing is that since we don&#x27;t know anything about what the hell &lt;code&gt;a&lt;&#x2F;code&gt; is, all the functions can do is mess around with the ordering a bit, as is their right, I suppose. How can we find out more about these &lt;code&gt;a&lt;&#x2F;code&gt; and &lt;code&gt;b&lt;&#x2F;code&gt; values without concretely specifying what they are?&lt;&#x2F;p&gt;
&lt;h3 id=&quot;constraints&quot;&gt;Constraints&lt;&#x2F;h3&gt;
&lt;p&gt;If there&#x27;s anything going on before the &lt;code&gt;=&amp;gt;&lt;&#x2F;code&gt; in a type signature, there&#x27;s a good chance it&#x27;s a &lt;code&gt;constraint&lt;&#x2F;code&gt;. These are rules that apply to each type they refer to. Here, we are using a constraint on &lt;code&gt;a&lt;&#x2F;code&gt;, that says &quot;I don&#x27;t care what &lt;code&gt;a&lt;&#x2F;code&gt; is, so long as it has a &lt;code&gt;Show&lt;&#x2F;code&gt; instance&quot;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;showTheList &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Show &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; String
&lt;&#x2F;span&gt;&lt;span&gt;showTheList &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;[]       &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;showTheList (a : as) = show a ++ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;, &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++ showTheList as
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- showTheList [1,2,3] == &amp;quot;1, 2, 3, &amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Therefore, the only thing we can really do to these &lt;code&gt;a&lt;&#x2F;code&gt; values is &lt;code&gt;show&lt;&#x2F;code&gt; them, as we don&#x27;t know anything else about it.&lt;&#x2F;p&gt;
&lt;p&gt;We can do something similar with the &lt;code&gt;Num&lt;&#x2F;code&gt; typeclass (which is the typeclass of numbers).&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;addStuffUp &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Num &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;addStuffUp &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;[]       &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span&gt;addStuffUp (a : as) = a + addStuffUp as
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- addStuffUp [1,2,3] == 6
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Here, the constraint means we don&#x27;t mind which kind of number we are passed - since they all implement &lt;code&gt;+&lt;&#x2F;code&gt; so we can add them together.&lt;&#x2F;p&gt;
&lt;p&gt;We can have as many constraints as we like on our variables (often multiple ones on the same value, such as &lt;code&gt;(Show a, Ord a) =&amp;gt;&lt;&#x2F;code&gt; for things that can be sorted and eventually turned into strings) - and can use any typeclass to do the restraining.&lt;&#x2F;p&gt;
&lt;p&gt;I hope this makes sense. This started melting into something about IO testing, but I have decided to spin that into a separate post before this one spirals out of control, so let&#x27;s stop for now. Thanks.&lt;&#x2F;p&gt;
&lt;p&gt;Make sense? If not, why not &lt;a href=&quot;&#x2F;contact.html&quot;&gt;get in touch&lt;&#x2F;a&gt;?&lt;&#x2F;p&gt;
&lt;p&gt;Further reading:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;http:&#x2F;&#x2F;www.haskellforall.com&#x2F;2015&#x2F;10&#x2F;polymorphism-for-dummies.html&quot;&gt;Polymorphism for Dummies&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;http:&#x2F;&#x2F;andrew.gibiansky.com&#x2F;blog&#x2F;haskell&#x2F;haskell-typeclasses&#x2F;&quot;&gt;Polymorphism in Haskell&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Typeclasses - Bifunctor</title>
          <pubDate>Sun, 13 Jan 2019 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/typeclasses-bifunctor/</link>
          <guid>https://danieljharvey.github.io/typeclasses-bifunctor/</guid>
          <description xml:base="https://danieljharvey.github.io/typeclasses-bifunctor/">&lt;p&gt;Let&#x27;s talk about things that could be one thing, or indeed could be another thing altogether.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Things&lt;&#x2F;span&gt;&lt;span&gt; a b = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;This&lt;&#x2F;span&gt;&lt;span&gt; a | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;That&lt;&#x2F;span&gt;&lt;span&gt; b
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This could be a &lt;code&gt;This&lt;&#x2F;code&gt; with an &lt;code&gt;a&lt;&#x2F;code&gt; inside, like a &lt;code&gt;String&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;thisEgg &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Things String Int
&lt;&#x2F;span&gt;&lt;span&gt;thisEgg = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;This &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Egg&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Or indeed a &lt;code&gt;That&lt;&#x2F;code&gt; with a &lt;code&gt;b&lt;&#x2F;code&gt; inside, like an &lt;code&gt;Int&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;thatNumber &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Things String Int
&lt;&#x2F;span&gt;&lt;span&gt;thatNumber = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;That 68
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now hopefully you are now thinking - &quot;Oh please, I do hope we map a function over one of these values soon&quot; - and worry not, we absolutely bloody can.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;mappity-mappity-map&quot;&gt;Mappity Mappity Map&lt;&#x2F;h3&gt;
&lt;p&gt;Let&#x27;s really push the boat out, and add one to the value inside.&lt;&#x2F;p&gt;
&lt;p&gt;First we&#x27;ll need a &lt;a href=&quot;&#x2F;posts&#x2F;2018-11-16-typeclasses-functor.markdown&quot;&gt;Functor&lt;&#x2F;a&gt; instance. Hopefully nothing too surprising here.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Functor&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Things &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  fmap f (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;That&lt;&#x2F;span&gt;&lt;span&gt; b) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;That&lt;&#x2F;span&gt;&lt;span&gt; (f b)
&lt;&#x2F;span&gt;&lt;span&gt;  fmap _ (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;This&lt;&#x2F;span&gt;&lt;span&gt; a) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;This&lt;&#x2F;span&gt;&lt;span&gt; a
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now we can map away to our heart&#x27;s content:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;biggerNumber &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Things String Int
&lt;&#x2F;span&gt;&lt;span&gt;biggerNumber = fmap addOne (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;That 68&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- biggerNumber == That 69
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Nice.&lt;&#x2F;p&gt;
&lt;p&gt;But what about `This &quot;Egg&quot;``? I&#x27;d like to get at that egg. Perhaps eat it.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;eat &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: String -&amp;gt; String
&lt;&#x2F;span&gt;&lt;span&gt;eat s = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;The &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++ s ++ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; was delicious!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Can we do that with &lt;code&gt;Functor&lt;&#x2F;code&gt;?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;doesntWork &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Things String Int
&lt;&#x2F;span&gt;&lt;span&gt;doesntWork = fmap eat thisEgg
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- ERROR: Couldn&amp;#39;t match type ‘[Char]’ with ‘Int’
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I&#x27;m afraid not. Looking back at our &lt;code&gt;Functor&lt;&#x2F;code&gt; instance we can see that the &lt;code&gt;fmap&lt;&#x2F;code&gt; function only lets us map over the values inside &lt;code&gt;That&lt;&#x2F;code&gt;, leaving poor &lt;code&gt;This&lt;&#x2F;code&gt; and our lonely egg very much map-less. But fear not! We have another weapon at hand that will let us get at it.&lt;&#x2F;p&gt;
&lt;p&gt;Enter....&lt;code&gt;Bifunctor!&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;(cue lightning, thunder, explosions and sounds of a large crowd who are clearly quite impressed).&lt;&#x2F;p&gt;
&lt;h3 id=&quot;definition&quot;&gt;Definition&lt;&#x2F;h3&gt;
&lt;p&gt;Let&#x27;s ask &lt;code&gt;ghci&lt;&#x2F;code&gt; what&#x27;s up.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;Data.bifunctor
&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; :i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Bifunctor
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Bifunctor&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p&lt;&#x2F;span&gt;&lt;span&gt; :: * -&amp;gt; * -&amp;gt; *) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;first &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p a c &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p b c
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;second &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;c&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p a b &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p a c
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;bimap &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;c &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;d&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p a c &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p b d
&lt;&#x2F;span&gt;&lt;span&gt;  {-# &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MINIMAL&lt;&#x2F;span&gt;&lt;span&gt; bimap | first, second #-}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Ok. Three functions in here, and it looks like we can make something a &lt;code&gt;Bifunctor&lt;&#x2F;code&gt; by implementing instances of both &lt;code&gt;first&lt;&#x2F;code&gt; and &lt;code&gt;second&lt;&#x2F;code&gt; or just &lt;code&gt;bimap&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s take a look at them.&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;first :: (a -&amp;gt; b) -&amp;gt; p a c -&amp;gt; p b c&lt;&#x2F;code&gt; - this takes a &lt;code&gt;Bifunctor&lt;&#x2F;code&gt; that may contain some &lt;code&gt;a&lt;&#x2F;code&gt; and &lt;code&gt;c&lt;&#x2F;code&gt; values, and a function that turns an &lt;code&gt;a&lt;&#x2F;code&gt; into some sort of &lt;code&gt;b&lt;&#x2F;code&gt;. It then runs the function on the &lt;code&gt;a&lt;&#x2F;code&gt; value, turning it into a &lt;code&gt;b&lt;&#x2F;code&gt; value. Sort of like doing an &lt;code&gt;fmap&lt;&#x2F;code&gt; over the &lt;code&gt;a&lt;&#x2F;code&gt; inside &lt;code&gt;This&lt;&#x2F;code&gt; from earlier. Pretty nice. Tl;dr - it&#x27;s &lt;code&gt;fmap&lt;&#x2F;code&gt; but over the left value.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;second :: (b -&amp;gt; c) -&amp;gt; p a b -&amp;gt; p a c&lt;&#x2F;code&gt; - this takes a &lt;code&gt;Bifunctor&lt;&#x2F;code&gt; with an &lt;code&gt;a&lt;&#x2F;code&gt; and a &lt;code&gt;b&lt;&#x2F;code&gt; and a function that turns the &lt;code&gt;b&lt;&#x2F;code&gt; values into &lt;code&gt;c&lt;&#x2F;code&gt; values. In the case of our &lt;code&gt;Things&lt;&#x2F;code&gt; datatype of &lt;code&gt;This&lt;&#x2F;code&gt; and &lt;code&gt;That&lt;&#x2F;code&gt;, this let&#x27;s us get at the &lt;code&gt;That&lt;&#x2F;code&gt; values, which we could anyway so big whoop. Tl;dr - it&#x27;s our pal &lt;code&gt;fmap&lt;&#x2F;code&gt; again.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;bimap :: (a -&amp;gt; b) -&amp;gt; (c -&amp;gt; d) -&amp;gt; p a c -&amp;gt; p b d&lt;&#x2F;code&gt; - this takes a &lt;code&gt;Bifunctor&lt;&#x2F;code&gt; that may contain &lt;code&gt;a&lt;&#x2F;code&gt; and &lt;code&gt;c&lt;&#x2F;code&gt; values, and runs a function over both sides. It&#x27;s doing &lt;code&gt;first&lt;&#x2F;code&gt; and &lt;code&gt;second&lt;&#x2F;code&gt; at the same time.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;OK. If you understand &lt;code&gt;Functor&lt;&#x2F;code&gt; there&#x27;s hopefully nothing out of the ordinary going on here. Let&#x27;s slop an instance together and get to work on that delicious egg.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;instances&quot;&gt;Instances&lt;&#x2F;h3&gt;
&lt;p&gt;Laziness dictates that we should define &lt;code&gt;bimap&lt;&#x2F;code&gt; because it is one function instead of two.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Bifunctor Things where
&lt;&#x2F;span&gt;&lt;span&gt;  bimap f _ (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;This&lt;&#x2F;span&gt;&lt;span&gt; a) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;This&lt;&#x2F;span&gt;&lt;span&gt; (f a)
&lt;&#x2F;span&gt;&lt;span&gt;  bimap _ g (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;That&lt;&#x2F;span&gt;&lt;span&gt; b) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;That&lt;&#x2F;span&gt;&lt;span&gt; (g b)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Seems fairly sensible hopefully. Let&#x27;s give it a spin.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;delicious &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Things String Int
&lt;&#x2F;span&gt;&lt;span&gt;delicious = first eat (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;This &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Egg&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- delicious = This &amp;quot;The Egg was delicious!&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Hooray! Although we defined &lt;code&gt;bimap&lt;&#x2F;code&gt; we got &lt;code&gt;first&lt;&#x2F;code&gt; for free, and that egg was pretty nice.&lt;&#x2F;p&gt;
&lt;p&gt;We can still map over the right hand value too!&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;doesWork &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Things String Int
&lt;&#x2F;span&gt;&lt;span&gt;doesWork = second addOne (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;That 68&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- doesWork == That 69
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Again, nice.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;tuples-pooples&quot;&gt;Tuples, Pooples&lt;&#x2F;h3&gt;
&lt;p&gt;Although our &lt;code&gt;Things&lt;&#x2F;code&gt; example is about &lt;code&gt;sum&lt;&#x2F;code&gt; types, we can also use it on &lt;code&gt;product&lt;&#x2F;code&gt; types like a &lt;code&gt;Tuple&lt;&#x2F;code&gt;, and use &lt;code&gt;Bifunctor&lt;&#x2F;code&gt; to mess with either value as we please.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;twoThings &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;twoThings = (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;100&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Dogs&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now, we could go ahead and show you &lt;code&gt;first&lt;&#x2F;code&gt; and &lt;code&gt;second&lt;&#x2F;code&gt; but I think you can work out what&#x27;s going to happen, so let&#x27;s go absolutely bonkers and race straight to &lt;code&gt;bimap&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;(but first, a helper function. Nothing untoward - it merely returns the first thing you give it and ignores the second.)&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;myConst &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;myConst a _ = a
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now we can turn our &lt;code&gt;Tuple&lt;&#x2F;code&gt; into a bestselling novel.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;oneBestSeller &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;oneBestSeller = bimap (+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) (myConst &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Dalmations&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;) twoThings
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- oneBestSeller = (101, &amp;quot;Dalmations&amp;quot;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I Bet You Did Not See That Coming.&lt;&#x2F;p&gt;
&lt;p&gt;For a bonus point, why not try and define &lt;code&gt;first&lt;&#x2F;code&gt; and &lt;code&gt;second&lt;&#x2F;code&gt; for &lt;code&gt;Tuple&lt;&#x2F;code&gt; types using &lt;code&gt;bimap&lt;&#x2F;code&gt; and &lt;code&gt;myConst&lt;&#x2F;code&gt;? Go on. You&#x27;ll have a great time, I absolutely promise.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;that-s-all-folks&quot;&gt;That&#x27;s all, folks&lt;&#x2F;h3&gt;
&lt;p&gt;So although helpful with &lt;code&gt;Tuple&lt;&#x2F;code&gt; and &lt;code&gt;Either&lt;&#x2F;code&gt; types, &lt;code&gt;Bifunctor&lt;&#x2F;code&gt; isn&#x27;t particularly mindblowing, but comes into it&#x27;s own when we combine it with &lt;a href=&quot;&#x2F;posts&#x2F;2018-11-17-typeclasses-contravariant.markdown&quot;&gt;Contravariant&lt;&#x2F;a&gt; to make &lt;code&gt;Profunctor&lt;&#x2F;code&gt;. More on that in the future though!&lt;&#x2F;p&gt;
&lt;p&gt;Further reading:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=JZPXzJ5tp9w&quot;&gt;The Extended Functor Family (video)&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;http:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;base-4.12.0.0&#x2F;docs&#x2F;Data-Bifunctor.html&quot;&gt;Data.Bifunctor&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Typeclasses - Alternative</title>
          <pubDate>Sun, 06 Jan 2019 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/typeclasses-alternative/</link>
          <guid>https://danieljharvey.github.io/typeclasses-alternative/</guid>
          <description xml:base="https://danieljharvey.github.io/typeclasses-alternative/">&lt;p&gt;Let&#x27;s think about things that may or may not happen (again, I know, I&#x27;m sorry).&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Perhaps&lt;&#x2F;span&gt;&lt;span&gt; a = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Yeah&lt;&#x2F;span&gt;&lt;span&gt; a | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nope &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Eq&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;A classic datatype, that we can use for expressing either &lt;em&gt;Some Sort Of Value&lt;&#x2F;em&gt; or &lt;em&gt;The Entire Concept Of Failure&lt;&#x2F;em&gt;. We can use it when getting the first item of a list, which may or may not actually exist.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;first &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Perhaps &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;first (a:_) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Yeah&lt;&#x2F;span&gt;&lt;span&gt; a
&lt;&#x2F;span&gt;&lt;span&gt;first _     = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nope
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If there IS a first item in the list we get the item wrapped in a &lt;code&gt;Yeah&lt;&#x2F;code&gt;, if not we get &lt;code&gt;Nope&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;We can make another very similar function for getting the second item as well...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;second &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Perhaps &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;second (_:b:_) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Yeah&lt;&#x2F;span&gt;&lt;span&gt; b
&lt;&#x2F;span&gt;&lt;span&gt;second _       = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nope
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now let&#x27;s say that for some entirely incomprehensible reason, we&#x27;d like the second item in the list, if not, the first item. What would that function look like?&lt;&#x2F;p&gt;
&lt;h3 id=&quot;first-solution&quot;&gt;First solution&lt;&#x2F;h3&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;naiveImplementation &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Perhaps &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;naiveImplementation as = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;case&lt;&#x2F;span&gt;&lt;span&gt; second as &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;of
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Yeah&lt;&#x2F;span&gt;&lt;span&gt; s -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Yeah&lt;&#x2F;span&gt;&lt;span&gt; s
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nope   &lt;&#x2F;span&gt;&lt;span&gt;-&amp;gt; first as
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Does it work?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;emptyList &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Perhaps Int
&lt;&#x2F;span&gt;&lt;span&gt;emptyList = naiveImplementation &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;[]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- emptyList == Nope
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;oneItemList &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Perhaps Int
&lt;&#x2F;span&gt;&lt;span&gt;oneItemList = naiveImplementation [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- oneItemList == Yeah 1
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;twoItemList &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Perhaps Int
&lt;&#x2F;span&gt;&lt;span&gt;twoItemList = naiveImplementation [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- oneItemList == Yeah 2
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It bloody does! Lovely stuff. Let&#x27;s all go home and put our feet up, we&#x27;ve nailed it.&lt;&#x2F;p&gt;
&lt;p&gt;OK. But hang on, that &lt;code&gt;naiveImplementation&lt;&#x2F;code&gt; function does seem a bit much though, especially if we start adding more cases to it. Plus that name kinda suggests, well, that the function might not represent the best way to go about this.&lt;&#x2F;p&gt;
&lt;p&gt;What if there was a typeclass that was designed to make things like this easier to deal with?&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s meet &lt;code&gt;Alternative&lt;&#x2F;code&gt;!&lt;&#x2F;p&gt;
&lt;h3 id=&quot;definition&quot;&gt;Definition&lt;&#x2F;h3&gt;
&lt;p&gt;What does &lt;code&gt;ghci&lt;&#x2F;code&gt; have to say about this so-called &lt;code&gt;Alternative&lt;&#x2F;code&gt;?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Prelude&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;Control.Applicative
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Prelude&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; :i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Alternative
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Applicative &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span&gt; =&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Alternative&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span&gt; :: * -&amp;gt; *) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;empty &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;f a
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;(&amp;lt;|&amp;gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;f a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;f a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;f a
&lt;&#x2F;span&gt;&lt;span&gt;  {-# &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MINIMAL&lt;&#x2F;span&gt;&lt;span&gt; empty, (&amp;lt;|&amp;gt;) #-}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;OK. So the most interesting thing is that any &lt;code&gt;Alternative&lt;&#x2F;code&gt; first needs to be a valid instance of &lt;a href=&quot;&#x2F;posts&#x2F;2018-11-17-typeclasses-applicative.markdown&quot;&gt;Applicative&lt;&#x2F;a&gt; first.&lt;&#x2F;p&gt;
&lt;p&gt;Secondly, we can get one just by defining &lt;code&gt;empty&lt;&#x2F;code&gt; and &lt;code&gt;&amp;lt;|&amp;gt;&lt;&#x2F;code&gt;. What are those then?&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;empty&lt;&#x2F;code&gt; is the &lt;em&gt;identity&lt;&#x2F;em&gt; element. This is a bit like the &lt;code&gt;mempty&lt;&#x2F;code&gt; in &lt;a href=&quot;&#x2F;posts&#x2F;2019-01-04-typeclasses-monoid.markdown&quot;&gt;Monoid&lt;&#x2F;a&gt; - it is a value that when added to the datatype, does nothing to it.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;&amp;lt;|&amp;gt;&lt;&#x2F;code&gt; is sort of an &lt;code&gt;or&lt;&#x2F;code&gt; for data types. Given two values, it returns the first &lt;em&gt;valid&lt;&#x2F;em&gt; one, as such. What &lt;em&gt;valid&lt;&#x2F;em&gt; means depends on the datatype, but usually that value won&#x27;t be the same as &lt;code&gt;empty&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;If you&#x27;re looking at this and thinking &quot;This Seems Very Similar To A Monoid&quot; then, yes, it sort of does, consisting as it does of one &lt;em&gt;Smash Things Together&lt;&#x2F;em&gt; function and one &lt;em&gt;Sort Of Nothingy&lt;&#x2F;em&gt; value.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s define it for our exciting &lt;code&gt;Perhaps&lt;&#x2F;code&gt; type.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;instances&quot;&gt;Instances&lt;&#x2F;h3&gt;
&lt;p&gt;Before we can have an &lt;code&gt;Applicative&lt;&#x2F;code&gt; we&#x27;ll need a &lt;a href=&quot;&#x2F;posts&#x2F;2018-11-16-typeclasses-functor.markdown&quot;&gt;Functor&lt;&#x2F;a&gt; instance. This one runs the function over the value if it&#x27;s there, if not, it does nothing at all.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Functor Perhaps where
&lt;&#x2F;span&gt;&lt;span&gt;  fmap f (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Yeah&lt;&#x2F;span&gt;&lt;span&gt; a) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Yeah&lt;&#x2F;span&gt;&lt;span&gt; (f a)
&lt;&#x2F;span&gt;&lt;span&gt;  fmap _ _        = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nope
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;OK. Great stuff. Let&#x27;s make an &lt;code&gt;Applicative&lt;&#x2F;code&gt; instance next. The &lt;code&gt;pure&lt;&#x2F;code&gt; instance just wraps whatever it&#x27;s passed in a &lt;code&gt;Yeah&lt;&#x2F;code&gt;. The &lt;code&gt;&amp;lt;*&amp;gt;&lt;&#x2F;code&gt; (or &lt;code&gt;apply&lt;&#x2F;code&gt;) function is written so that if we have a function inside &lt;code&gt;f&lt;&#x2F;code&gt; and a value in &lt;code&gt;a&lt;&#x2F;code&gt; then we&#x27;ll get the function &lt;code&gt;f&lt;&#x2F;code&gt; applied to &lt;code&gt;a&lt;&#x2F;code&gt; and wrapped in &lt;code&gt;Yeah&lt;&#x2F;code&gt;, if not we&#x27;ll get a &lt;code&gt;Nope&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Applicative Perhaps where
&lt;&#x2F;span&gt;&lt;span&gt;  pure a = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Yeah&lt;&#x2F;span&gt;&lt;span&gt; a
&lt;&#x2F;span&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Yeah&lt;&#x2F;span&gt;&lt;span&gt; f) &amp;lt;*&amp;gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Yeah&lt;&#x2F;span&gt;&lt;span&gt; a) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Yeah&lt;&#x2F;span&gt;&lt;span&gt; (f a)
&lt;&#x2F;span&gt;&lt;span&gt;  _        &amp;lt;*&amp;gt; _        = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nope
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Yeah? Yeah.&lt;&#x2F;p&gt;
&lt;p&gt;OK. Here is the exciting part:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Alternative Perhaps where
&lt;&#x2F;span&gt;&lt;span&gt;    empty             = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nope
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Yeah&lt;&#x2F;span&gt;&lt;span&gt; x  &amp;lt;|&amp;gt; _     = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Yeah&lt;&#x2F;span&gt;&lt;span&gt; x
&lt;&#x2F;span&gt;&lt;span&gt;    _       &amp;lt;|&amp;gt; y     = y
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Look at that! An exciting &lt;code&gt;Alternative&lt;&#x2F;code&gt; instance. &lt;code&gt;Nope&lt;&#x2F;code&gt; is our &lt;code&gt;empty&lt;&#x2F;code&gt; (or &lt;em&gt;identity&lt;&#x2F;em&gt;) value, and our &lt;code&gt;&amp;lt;|&amp;gt;&lt;&#x2F;code&gt; function returns the first value if it&#x27;s a &lt;code&gt;Yeah&lt;&#x2F;code&gt; (which represents &lt;em&gt;valid&lt;&#x2F;em&gt; data in the context of our &lt;code&gt;Perhaps&lt;&#x2F;code&gt; datatype), if not it returns whatever the second value is.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s use it to make some contrived code ever so slightly smaller and more difficult to understand.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;better-solution&quot;&gt;Better Solution&lt;&#x2F;h3&gt;
&lt;p&gt;Here is our &lt;em&gt;Get The Second Item, Or If That Doesn&#x27;t Work Out Then Let&#x27;s Settle For The First Item&lt;&#x2F;em&gt; function.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;getPreferred &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Perhaps &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;getPreferred as = second as &amp;lt;|&amp;gt; first as
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Much easier to understand! It almost looks like &lt;code&gt;second || first&lt;&#x2F;code&gt;, which I like a lot.&lt;&#x2F;p&gt;
&lt;p&gt;Don&#x27;t trust me?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;notFound &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Perhaps Int
&lt;&#x2F;span&gt;&lt;span&gt;notFound = getPreferred &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;[]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- notFound == Nope
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;found1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Perhaps Int
&lt;&#x2F;span&gt;&lt;span&gt;found1 = getPreferred [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- found1 == Yeah 1
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;found2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Perhaps Int
&lt;&#x2F;span&gt;&lt;span&gt;found2 = getPreferred [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- found2 = Yeah 2
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;See! And you doubted me!&lt;&#x2F;p&gt;
&lt;p&gt;Now, that seems like a lot of instance writing for not much, but often the place you&#x27;ll find &lt;code&gt;Alternative&lt;&#x2F;code&gt; is in stuff like routing for front end applications. Let&#x27;s have a look at something like that:&lt;&#x2F;p&gt;
&lt;h3 id=&quot;routing-example&quot;&gt;Routing Example&lt;&#x2F;h3&gt;
&lt;p&gt;Here&#x27;s a datatype to describe all the pages in a completely fictional and somewhat limited website...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Route &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Index
&lt;&#x2F;span&gt;&lt;span&gt;           | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Gallery
&lt;&#x2F;span&gt;&lt;span&gt;           | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Contact
&lt;&#x2F;span&gt;&lt;span&gt;           | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Complaints
&lt;&#x2F;span&gt;&lt;span&gt;           | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Help &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Eq&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Show&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;...some type aliases to make the type signatures we&#x27;re about to write easier to understand...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Url &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;String
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Match &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;String
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;...and a (rather basic) function for taking the passed &lt;code&gt;url&lt;&#x2F;code&gt; and matching it to the page we want to be looking at:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;matches &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Match -&amp;gt; Route -&amp;gt; Url -&amp;gt; Perhaps Route
&lt;&#x2F;span&gt;&lt;span&gt;matches match route url = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; isInfixOf match&amp;#39; url&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;                        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;then &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Yeah&lt;&#x2F;span&gt;&lt;span&gt; route
&lt;&#x2F;span&gt;&lt;span&gt;                        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nope &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;                          match&amp;#39; = toLower &amp;lt;$&amp;gt; match
&lt;&#x2F;span&gt;&lt;span&gt;                          url&amp;#39;   = toLower &amp;lt;$&amp;gt; url
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;What&#x27;s going on here?&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;First, it converts our &lt;code&gt;match&lt;&#x2F;code&gt; and &lt;code&gt;url&lt;&#x2F;code&gt; strings to lowercase by mapping &lt;code&gt;toLower&lt;&#x2F;code&gt; from &lt;code&gt;Data.Char&lt;&#x2F;code&gt; over.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Then, it uses &lt;code&gt;isInfixOf&lt;&#x2F;code&gt; from &lt;code&gt;Data.List&lt;&#x2F;code&gt; which returns a &lt;code&gt;Boolean&lt;&#x2F;code&gt; telling us whether the &lt;code&gt;match&lt;&#x2F;code&gt; string can be found inside the &lt;code&gt;url&lt;&#x2F;code&gt; string.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;If so, return the passed &lt;code&gt;Route&lt;&#x2F;code&gt; wrapped in a &lt;code&gt;Yeah&lt;&#x2F;code&gt;, if not, return &lt;code&gt;Nope&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Good stuff!&lt;&#x2F;p&gt;
&lt;p&gt;And now here is our actual logic, that uses the &lt;code&gt;matches&lt;&#x2F;code&gt; function to find out where we should navigate to in our excellent website.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;matchRoute &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Url -&amp;gt; Perhaps Route
&lt;&#x2F;span&gt;&lt;span&gt;matchRoute url = matches &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;gallery&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Gallery&lt;&#x2F;span&gt;&lt;span&gt; url
&lt;&#x2F;span&gt;&lt;span&gt;             &amp;lt;|&amp;gt; matches &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;contact&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Contact&lt;&#x2F;span&gt;&lt;span&gt; url
&lt;&#x2F;span&gt;&lt;span&gt;             &amp;lt;|&amp;gt; matches &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;complaints&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Complaints&lt;&#x2F;span&gt;&lt;span&gt; url
&lt;&#x2F;span&gt;&lt;span&gt;             &amp;lt;|&amp;gt; matches &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;help&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Help&lt;&#x2F;span&gt;&lt;span&gt; url
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Assuming we always want to navigate somewhere, we can also make a wrapper function that returns a default &lt;code&gt;Route&lt;&#x2F;code&gt; if we do not find a valid one.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;matchRouteDefault &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Url -&amp;gt; Route
&lt;&#x2F;span&gt;&lt;span&gt;matchRouteDefault url =
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;case&lt;&#x2F;span&gt;&lt;span&gt; matchRoute url &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;of
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Yeah&lt;&#x2F;span&gt;&lt;span&gt; route -&amp;gt; route
&lt;&#x2F;span&gt;&lt;span&gt;    _          -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Index
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Let&#x27;s use it to navigate to the &lt;code&gt;Gallery&lt;&#x2F;code&gt;, which I have no doubt is a very good example of the genre.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;findGallery &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Route
&lt;&#x2F;span&gt;&lt;span&gt;findGallery = matchRouteDefault &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;internet.com&#x2F;gallery&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- findGallery == Gallery
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Or default to the &lt;code&gt;Index&lt;&#x2F;code&gt; when we&#x27;re passed a load of old rubbish.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;findDefault &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Route
&lt;&#x2F;span&gt;&lt;span&gt;findDefault = matchRouteDefault &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;internet.com&#x2F;rubbish&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- findDefault == Index
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We&#x27;re basically front end development experts now. Great stuff.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;wrapping-up&quot;&gt;Wrapping Up&lt;&#x2F;h3&gt;
&lt;p&gt;The &lt;code&gt;Control.Applicative&lt;&#x2F;code&gt; documentation describes &lt;code&gt;Alternative&lt;&#x2F;code&gt; as a &lt;em&gt;&quot;a Monoid on applicative functors&quot;&lt;&#x2F;em&gt;, and perhaps I could have just led with that and saved us all a bunch of trouble. This kind of typeclass mainly gives us a nicer syntax so that we can bend our code to closer match the domain we are working in, which is generally a Good Thing in my book.&lt;&#x2F;p&gt;
&lt;p&gt;(You may notice we&#x27;ve done a lot of work here defining &lt;code&gt;Perhaps&lt;&#x2F;code&gt; when we could have just used &lt;code&gt;Maybe&lt;&#x2F;code&gt; and saved ourselves writing a lot of instances but it&#x27;s my blog and I can do what I want.)&lt;&#x2F;p&gt;
&lt;p&gt;Make sense? If not, why not &lt;a href=&quot;&#x2F;contact.html&quot;&gt;get in touch&lt;&#x2F;a&gt;?&lt;&#x2F;p&gt;
&lt;p&gt;Further reading:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;http:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;base-4.12.0.0&#x2F;docs&#x2F;Control-Applicative.html&quot;&gt;Control.Applicative&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;en.wikibooks.org&#x2F;wiki&#x2F;Haskell&#x2F;Alternative_and_MonadPlus&quot;&gt;Alternative and MonadPlus&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Why The Hell Should I Care About Typeclasses?</title>
          <pubDate>Sat, 05 Jan 2019 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/what-is-a-typeclass/</link>
          <guid>https://danieljharvey.github.io/what-is-a-typeclass/</guid>
          <description xml:base="https://danieljharvey.github.io/what-is-a-typeclass/">&lt;p&gt;So recently I have become one of those jerks that has sailed through &lt;em&gt;Read A Couple Of Chapters Of A Book About Haskell&lt;&#x2F;em&gt;, by way of &lt;em&gt;Snuck Maybe Types Into My Work Project&lt;&#x2F;em&gt; to arrive at full-blown &lt;em&gt;Good Morning Sir&#x2F;Madam, Have You Got A Spare Moment To Talk About Monads™?&lt;&#x2F;em&gt;. That is to say, by selfishly forcing these concepts upon my colleagues I have ended up spending a lot of my working life explaining them and why I think they are good to stop everybody hating me for making their lives unnecessary complicated. In doing so, I think I have come across one of the main conceptual stumbling blocks, which I hope to make less-stumble-able.&lt;&#x2F;p&gt;
&lt;p&gt;So the problem, as I see it, isn&#x27;t grasping what a &lt;code&gt;Monad&lt;&#x2F;code&gt; is (it&#x27;s a burrito, it&#x27;s a poster tube, it&#x27;s a snobby waste of time, easy!), or even what a &lt;code&gt;Functor&lt;&#x2F;code&gt; is (it&#x27;s mappable, right?), but whether they are even &quot;things&quot; at all. It&#x27;s one thing to see words like &lt;code&gt;Monad&lt;&#x2F;code&gt;, &lt;code&gt;Functor&lt;&#x2F;code&gt;, &lt;code&gt;Monoid&lt;&#x2F;code&gt; or &lt;code&gt;Traversable&lt;&#x2F;code&gt; around the place but it&#x27;s a little unclear how to make, or god forbid, actually use one in something approaching real life code. Are they objects? Do I make a new &lt;code&gt;Monad&lt;&#x2F;code&gt; object just like I&#x27;d make a new &lt;code&gt;Promise&lt;&#x2F;code&gt;?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;response &lt;&#x2F;span&gt;&lt;span&gt;= new Monad(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ajaxUrl&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Is is a &lt;code&gt;Functor&lt;&#x2F;code&gt; a method I can run on something to increase the amount of maths?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;makeProgramFaster &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;response&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;Functor&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;MAXIMISE DATA&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The short answer is &quot;no&quot;, and the long answer, well, that&#x27;s also &quot;no&quot;, followed by &quot;ok, let me tell you what typeclasses are and hopefully the other stuff will sort of get cleared up along the way.&quot;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;maths-is-sadness&quot;&gt;Maths is sadness&lt;&#x2F;h3&gt;
&lt;p&gt;Reading about type theory, particularly materials written from a very mathematical perspective, can often make it feel even worse, with explanations that feel so broad and simple that they feel either a) meaningless or b) like they must contain some unseen trap we mere mortals do not understand.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;typeclass1.png&quot; alt=&quot;Perhaps, yes.&quot; title=&quot;Are monads a waste of time?&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Anyhow, I thought perhaps a better approach here is to explain what these things are not and work from there. Because the aim here is to end up with some real life knowledge we can use to write real code, instead of using examples from the world of computers, let&#x27;s use them from the world of crime dramas, because I have been watching a lot of those recently.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;time-for-a-tenuous-analogy&quot;&gt;Time for a tenuous analogy&lt;&#x2F;h3&gt;
&lt;p&gt;So we have a police officer. In this case, it&#x27;s Detective Sergeant Kate Fleming from Jed Mercurio&#x27;s surprisingly gripping Line Of Duty series (I too struggled with the series title&#x27;s almost Clancy-ian levels of Really Must We Spell This Out, do persevere with it though, it really is OK, you probably only need to watch the first couple of seasons to follow this article)&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;typeclass3.png&quot; alt=&quot;DS Kate Fleming, a human woman&quot; title=&quot;DS Kate Fleming&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;DS Kate Fleming&lt;&#x2F;strong&gt; is a &lt;code&gt;Police Officer&lt;&#x2F;code&gt; in AC-12, a unit in the police force somewhere in London that investigates &lt;code&gt;Bent Coppers&lt;&#x2F;code&gt; and makes sure there is no &lt;em&gt;Dodgy Dealings&lt;&#x2F;em&gt; or &lt;em&gt;Underhand Business&lt;&#x2F;em&gt;. &lt;strong&gt;DS Kate Fleming&lt;&#x2F;strong&gt; was not born a police officer, but certainly displays all the characteristics of one - she &lt;em&gt;Catches Criminals&lt;&#x2F;em&gt;, &lt;em&gt;Likes A Pint&lt;&#x2F;em&gt;, and &lt;em&gt;Her Marriage Is In Tatters&lt;&#x2F;em&gt; because of course she is &lt;em&gt;Married To The Job&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;![DC Nigel &quot;Nige&quot; Morton, a human man](&#x2F;images&#x2F;typeclass4.png &quot;DC Nigel &quot;Nige&quot; Morton, a human man&quot;)&lt;&#x2F;p&gt;
&lt;p&gt;Here is another &lt;code&gt;Police Officer&lt;&#x2F;code&gt; - in this case &lt;strong&gt;Detective Constable Nigel &quot;Nige&quot; Morton&lt;&#x2F;strong&gt;, played by 90s lad hero and renowned bounding frown Neil Morrissey. &lt;strong&gt;&quot;Nige&quot;&lt;&#x2F;strong&gt; was also not born a copper, but he is certainly one now as he &lt;em&gt;Likes A Pint&lt;&#x2F;em&gt; and also, to some extent, &lt;em&gt;Catches Criminals&lt;&#x2F;em&gt;. &lt;strong&gt;&quot;Nige&quot;&lt;&#x2F;strong&gt;, however, is a bit of a sneaky prick, and has been known from time to time to take a few backhanders in exchange for leaking information to the press and to other criminals. Therefore, to some extent, as well being a &lt;code&gt;Police Officer&lt;&#x2F;code&gt;, &lt;strong&gt;&quot;Nige&quot;&lt;&#x2F;strong&gt; is a &lt;code&gt;Bent Copper&lt;&#x2F;code&gt; because of his &lt;em&gt;Underhand Business&lt;&#x2F;em&gt; and &lt;em&gt;Dodgy Dealings&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;how-does-this-relate-to-typeclasses-then&quot;&gt;How does this relate to typeclasses then?&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;strong&gt;DS Kate Fleming&lt;&#x2F;strong&gt; is not a typeclass.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;DC Nigel &quot;Nige&quot; Morton&lt;&#x2F;strong&gt; is also not a typeclass.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;em&gt;Liking A Pint&lt;&#x2F;em&gt; is not a typeclass.&lt;&#x2F;p&gt;
&lt;p&gt;Neither is &lt;em&gt;Underhand Business&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;typeclass-examples&quot;&gt;Typeclass examples&lt;&#x2F;h3&gt;
&lt;p&gt;So what are?&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Human Person&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Both &lt;strong&gt;DS Kate Fleming&lt;&#x2F;strong&gt; and &lt;strong&gt;DC Nigel &quot;Nige&quot; Morton&lt;&#x2F;strong&gt; can have instances of the typeclass &lt;code&gt;Human Person&lt;&#x2F;code&gt;, because they &lt;em&gt;Breath&lt;&#x2F;em&gt;, &lt;em&gt;Eat&lt;&#x2F;em&gt; and &lt;em&gt;Poo&lt;&#x2F;em&gt;. They are &#x27;lawful&#x27; instances of &lt;code&gt;Human Person&lt;&#x2F;code&gt; because I am too lazy to think of a reason that they are not.&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Police Officer&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;&lt;strong&gt;DS Kate Fleming&lt;&#x2F;strong&gt; can be an instance of the typeclass of &lt;code&gt;Police Officer&lt;&#x2F;code&gt; because she &lt;em&gt;Catches Criminals&lt;&#x2F;em&gt; and &lt;em&gt;Likes A Pint&lt;&#x2F;em&gt;. She is also a &#x27;lawful&#x27; instance, we can say, because as well as this, she does no &lt;em&gt;Underhand Business&lt;&#x2F;em&gt; or &lt;em&gt;Dodgy Dealings&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;DC Nigel &quot;Nige&quot; Morton&lt;&#x2F;strong&gt; is, to the innocent eye, an instance of the typeclass &lt;code&gt;Police Officer&lt;&#x2F;code&gt;. He also &lt;em&gt;Likes A Pint&lt;&#x2F;em&gt;, &lt;em&gt;Catches Criminals&lt;&#x2F;em&gt;, and although he is seen to have a marriage earlier in the show, later he is seen to be mowing his lawn in a solitary fashion shot through a lens tinged with regret. However, he is not a &#x27;lawful&#x27; instance of the typeclass &lt;code&gt;Police Officer&lt;&#x2F;code&gt;, because of his &lt;em&gt;Underhand Business&lt;&#x2F;em&gt; or &lt;em&gt;Dodgy Dealings&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Bent Copper&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;&lt;strong&gt;DS Kate Fleming&lt;&#x2F;strong&gt; is not in the typeclass &lt;code&gt;Bent Copper&lt;&#x2F;code&gt;, because (spoiler alert!) throughout the show she is seen to behave in a moral fashion befitting an officer working for AC-12, the anti-corruption unit at the centre of the whole show.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;DC Nigel &quot;Nige&quot; Morton&lt;&#x2F;strong&gt; however is in the typeclass &lt;code&gt;Bent Copper&lt;&#x2F;code&gt;, exactly because of his &lt;em&gt;Underhand Business&lt;&#x2F;em&gt;, &lt;em&gt;Dodgy Dealings&lt;&#x2F;em&gt; and &lt;em&gt;Perhaps Not Really Needing That Walking Stick But Claiming Disability Benefits Regardless&lt;&#x2F;em&gt;. He is probably also a lawful instance of &lt;code&gt;Bent Copper&lt;&#x2F;code&gt; (although I understand at this juncture that this makes the whole analogy somewhat confusing) - simply because &lt;em&gt;Bent Coppers Don&#x27;t Really Have Rules, And If They Did, Playing By Them Would Somewhat Defeat The Point.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Anti-Corruption Police Officer&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;&lt;strong&gt;DS Kate Fleming&lt;&#x2F;strong&gt; is also in this typeclass because she &lt;em&gt;Nicks Bent Coppers&lt;&#x2F;em&gt;, whilst &lt;strong&gt;DC Nigel &quot;Nige&quot; Morton&lt;&#x2F;strong&gt; is not because he wouldn&#x27;t &lt;em&gt;Dob In One Of Our Own&lt;&#x2F;em&gt;. Clear? As mud? Great.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;back-to-stupid-code&quot;&gt;Back to stupid code&lt;&#x2F;h3&gt;
&lt;p&gt;So how does this relate to computers and showing our colleagues how incredibly intellectual we are?&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;typeclass6.png&quot; alt=&quot;Computer crime is also a type of crime&quot; title=&quot;Computer crime is also a type of crime&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s take an array. That&#x27;s a thing you have probably used.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;arr &lt;&#x2F;span&gt;&lt;span&gt;= [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;65&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;7&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;strong&gt;arr&lt;&#x2F;strong&gt; is not a typeclass, it is an object (or more broadly, a value).&lt;&#x2F;p&gt;
&lt;p&gt;A &lt;code&gt;Functor&lt;&#x2F;code&gt; is a typeclass though, and &lt;strong&gt;arr&lt;&#x2F;strong&gt; can be an instance of &lt;code&gt;Functor&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The rules of a &#x27;lawful&#x27; &lt;code&gt;Functor&lt;&#x2F;code&gt; is that we can run a Map function over the contents of the array and afterward &lt;em&gt;The Length Of The Array Remains The Same&lt;&#x2F;em&gt; and &lt;em&gt;It&#x27;s Still An Array&lt;&#x2F;em&gt; and &lt;em&gt;Basically Nothing Really Weird Has Happened&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;arr&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;x &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; arr == [1, 2, 2, 4, 65, 7]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As you see here, our array is the same length and the function has been applied to each item without any &lt;em&gt;Funny Business&lt;&#x2F;em&gt;. To tie it back to our police-based analogy - if &lt;strong&gt;DS Kate Fleming&lt;&#x2F;strong&gt; was given some important evidence to take back to the station, this result constitutes the evidence arriving at the station untampered for some forensic analysis that will pull the case together in the final nail-biting moments of the series.&lt;&#x2F;p&gt;
&lt;p&gt;However, what if &lt;strong&gt;DC Nigel &quot;Nige&quot; Morton&lt;&#x2F;strong&gt; was given that same evidence? That&#x27;s a nice &lt;code&gt;65&lt;&#x2F;code&gt; value in there, surely nobody would notice if he snuck it for himself? Surely he&#x27;s &lt;em&gt;Done His Bit Over The Years And Deserves It&lt;&#x2F;em&gt;, after all.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;var &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;nigesFilthyPocket &lt;&#x2F;span&gt;&lt;span&gt;= []
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;arr&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;x &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;x &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;65&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;x
&lt;&#x2F;span&gt;&lt;span&gt;  } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;nigesFilthyPocket&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;push&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;})
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; arr == [1, 2, 2, 4, 0, 7]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you can follow this code (and forgive the use of a mutable variable in an article that is, despite severely beating around the bush about the fact, written about the subject of functional programming) I think we can all agree that &lt;strong&gt;&quot;Nige&quot;&lt;&#x2F;strong&gt; here is a bit of a swine.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;typeclass5.png&quot; alt=&quot;An example of a bad crime&quot; title=&quot;An example of a bad crime&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;&quot;Nige&quot;&lt;&#x2F;strong&gt;&#x27;s horrible law-breaking evidence-stealing &lt;code&gt;map&lt;&#x2F;code&gt; function does not make a &#x27;lawful&#x27; &lt;code&gt;Functor&lt;&#x2F;code&gt;, as it breaks the &lt;em&gt;Basically Nothing Really Weird Has Happened&lt;&#x2F;em&gt; rule above by doing a massive side-effect and mutating an outside variable when it should just be doing nothing outside of the &lt;strong&gt;arr&lt;&#x2F;strong&gt;. However, if there is a &lt;code&gt;TotallyAwfulFunctor&lt;&#x2F;code&gt; typeclass, this would make an excellent instance of it.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;ok-i-m-exhausted-please-tell-me-what-is-a-typeclass&quot;&gt;OK, I&#x27;m exhausted, please, tell me, what is a typeclass?&lt;&#x2F;h3&gt;
&lt;p&gt;Sure.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;typeclass2.png&quot; alt=&quot;Irrelevant graph&quot; title=&quot;Irrelevant graph&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Objects and datatypes can be made instances of the typeclass &lt;code&gt;Functor&lt;&#x2F;code&gt;, provided they implement &lt;em&gt;map&lt;&#x2F;em&gt; without any funny business. These could be arrays, tree structure, &lt;code&gt;Maybe&lt;&#x2F;code&gt; and &lt;code&gt;Either&lt;&#x2F;code&gt;, and loads more. You can &lt;em&gt;map&lt;&#x2F;em&gt; over them all and expect the same behaviour.&lt;&#x2F;p&gt;
&lt;p&gt;People are instances of the typeclass &lt;code&gt;Human Being&lt;&#x2F;code&gt;, as in general they implement the &lt;em&gt;Breath&lt;&#x2F;em&gt;, &lt;em&gt;Eat&lt;&#x2F;em&gt; and &lt;em&gt;Poo&lt;&#x2F;em&gt; functions. (todo: add javascript example)&lt;&#x2F;p&gt;
&lt;p&gt;Anything that can be compared to another thing to check whether it is the same or not can be an instance of the typeclass &lt;code&gt;Eq&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Many objects and datatypes can be made instances of the typeclass &lt;code&gt;Monad&lt;&#x2F;code&gt;, if they are also lawful &lt;code&gt;Functors&lt;&#x2F;code&gt; and &lt;code&gt;Applicative Functors&lt;&#x2F;code&gt; and implement &lt;em&gt;bind&lt;&#x2F;em&gt; and &lt;em&gt;return&lt;&#x2F;em&gt; functions. (What? Not now...)&lt;&#x2F;p&gt;
&lt;p&gt;Therefore...&lt;&#x2F;p&gt;
&lt;h3 id=&quot;a-typeclass-is-a-way-of-categorising-things-based-not-on-what-they-are-but-what-they-can-and-can-t-do&quot;&gt;A typeclass is a way of categorising things based not on what they are but what they can and can&#x27;t do.&lt;&#x2F;h3&gt;
&lt;p&gt;(Thanks &lt;a href=&quot;https:&#x2F;&#x2F;okay.codes&#x2F;&quot;&gt;Riku&lt;&#x2F;a&gt; for helping me put that more succinctly. You may find it hard to believe that I have a tendency to ramble).&lt;&#x2F;p&gt;
&lt;p&gt;Make sense? If not, why not &lt;a href=&quot;&#x2F;contact.html&quot;&gt;get in touch&lt;&#x2F;a&gt;?&lt;&#x2F;p&gt;
&lt;p&gt;Further reading:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;wiki.haskell.org&#x2F;Typeclassopedia&quot;&gt;Typeclassopedia&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;&#x2F;tags&#x2F;typeclasses.html&quot;&gt;More about Typeclasses&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Typeclasses - Monoid</title>
          <pubDate>Fri, 04 Jan 2019 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/typeclasses-monoid/</link>
          <guid>https://danieljharvey.github.io/typeclasses-monoid/</guid>
          <description xml:base="https://danieljharvey.github.io/typeclasses-monoid/">&lt;p&gt;In an exciting previous episode, we talked about the &lt;a href=&quot;https:&#x2F;&#x2F;danieljharvey.github.io&#x2F;typeclasses-monoid&#x2F;.&#x2F;posts&#x2F;2019-01-03-typeclasses-semigroup.markdown&quot;&gt;Semigroup&lt;&#x2F;a&gt; type class that is used for smashing two things together.&lt;&#x2F;p&gt;
&lt;p&gt;Today we&#x27;re going to talk about the &lt;code&gt;Monoid&lt;&#x2F;code&gt; typeclass, which is a way of generalising a collection of things that can be combined together. Seems very similar, but the important difference between combining two things, and combining any number of things, is that that number of things might be zero things.&lt;&#x2F;p&gt;
&lt;p&gt;Therefore as well as the &lt;code&gt;append&lt;&#x2F;code&gt; function &lt;code&gt;&amp;lt;&amp;gt;&lt;&#x2F;code&gt; we will need a way of expressing what no items looks like. Seems weird? Yeah, it kind of is. Some examples may help.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;combining-lists&quot;&gt;Combining Lists&lt;&#x2F;h3&gt;
&lt;p&gt;We need to combine a &lt;code&gt;List&lt;&#x2F;code&gt; of other &lt;code&gt;List&lt;&#x2F;code&gt; values, and we&#x27;re all functional programmers, so let&#x27;s go ahead and make a function for doing that.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;combineList &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;]] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;combineList (a:as) = a ++ (combineList as)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If we use it on an actual value, then great job.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;great &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;great = combineList [[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;],[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;6&lt;&#x2F;span&gt;&lt;span&gt;]]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- great == [1,2,3,4,5,6]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;But what about if we use it on an empty list?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;error &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;error = combineList &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;[]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Ahh shit! We get this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;uncaught exception: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;PatternMatchFail
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That&#x27;s not great. Looks like we&#x27;re going to need an empty value to use for empty lists.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;combineList1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;]] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;combineList1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;[] &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;[]
&lt;&#x2F;span&gt;&lt;span&gt;combineList1 (a:as) = a ++ (combineList1 as)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;Haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-Haskell &quot;&gt;&lt;code class=&quot;language-Haskell&quot; data-lang=&quot;Haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;ok &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;ok = combineList1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;[]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- ok == []
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So here the empty value is &lt;code&gt;[]&lt;&#x2F;code&gt; - and having this value is what means we have a &lt;code&gt;Monoid&lt;&#x2F;code&gt; as well as a &lt;code&gt;Semigroup&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;definition&quot;&gt;Definition&lt;&#x2F;h3&gt;
&lt;p&gt;What does &lt;code&gt;ghci&lt;&#x2F;code&gt; say about &lt;code&gt;Monoid&lt;&#x2F;code&gt;?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Prelude&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; :i Monoid
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Semigroup &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt; =&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Monoid &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;mempty &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;mappend &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;mconcat &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;  {-# &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MINIMAL&lt;&#x2F;span&gt;&lt;span&gt; mempty #-}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;OK. So firstly, whatever we want to make a &lt;code&gt;Monoid&lt;&#x2F;code&gt; must also be a &lt;code&gt;Semigroup&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;(Incidentally, the terminology of the relationship between these two is that &lt;code&gt;Semigroup&lt;&#x2F;code&gt; is a &lt;code&gt;superclass&lt;&#x2F;code&gt; of &lt;code&gt;Monoid&lt;&#x2F;code&gt;, meaning anything that is a &lt;code&gt;Monoid&lt;&#x2F;code&gt; is also a &lt;code&gt;Semigroup&lt;&#x2F;code&gt;. Conversely &lt;code&gt;Monoid&lt;&#x2F;code&gt; is a &lt;code&gt;subclass&lt;&#x2F;code&gt; of &lt;code&gt;Semigroup&lt;&#x2F;code&gt;. You&#x27;ll notice many of these relationships amongst Haskell classes, like between &lt;code&gt;Eq&lt;&#x2F;code&gt; and &lt;code&gt;Ord&lt;&#x2F;code&gt;, and between &lt;code&gt;Functor&lt;&#x2F;code&gt; and &lt;code&gt;Applicative&lt;&#x2F;code&gt;. More words! More confusion! Great!)&lt;&#x2F;p&gt;
&lt;p&gt;Secondly, the only function we need to define (because of &lt;code&gt;MINIMAL&lt;&#x2F;code&gt;) is &lt;code&gt;mempty&lt;&#x2F;code&gt; - which defines our empty element (we also need &lt;code&gt;mappend&lt;&#x2F;code&gt;, but this is the same as &lt;code&gt;&amp;lt;&amp;gt;&lt;&#x2F;code&gt; and is supplied by our &lt;code&gt;Semigroup&lt;&#x2F;code&gt; unless we have a burning desire to write another one).&lt;&#x2F;p&gt;
&lt;p&gt;Therefore, if we take a &lt;code&gt;Semigroup&lt;&#x2F;code&gt; and plop an &lt;code&gt;mempty&lt;&#x2F;code&gt; function on it too we can have a &lt;code&gt;Monoid&lt;&#x2F;code&gt;. Clear as mud!&lt;&#x2F;p&gt;
&lt;h3 id=&quot;more-about-mempty&quot;&gt;More about mempty&lt;&#x2F;h3&gt;
&lt;p&gt;The important test for this &lt;code&gt;mempty&lt;&#x2F;code&gt; value is that when we &lt;code&gt;mappend&lt;&#x2F;code&gt; it onto our &lt;code&gt;Monoid&lt;&#x2F;code&gt; it does nothing whatsoever.&lt;&#x2F;p&gt;
&lt;p&gt;Here&#x27;s an empty element for &lt;code&gt;List&lt;&#x2F;code&gt;...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;emptyList &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;emptyList = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;[]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;...and here it combined with another &lt;code&gt;List&lt;&#x2F;code&gt; and achieving absolutely nothing.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;addNothing &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;addNothing = [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;] ++ emptyList
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- addNothing == [1,2,3]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;defining-a-list-monoid&quot;&gt;Defining a List Monoid&lt;&#x2F;h3&gt;
&lt;p&gt;So we need to make a &lt;code&gt;List Monoid&lt;&#x2F;code&gt; then?&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;An operation called &lt;code&gt;mappend&lt;&#x2F;code&gt; for combining two values - here &lt;code&gt;++&lt;&#x2F;code&gt; will do the trick.&lt;&#x2F;li&gt;
&lt;li&gt;An empty value called &lt;code&gt;mempty&lt;&#x2F;code&gt; - here we would use &lt;code&gt;[]&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Here goes:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;Haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-Haskell &quot;&gt;&lt;code class=&quot;language-Haskell&quot; data-lang=&quot;Haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Semigroup&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  a &amp;lt;&amp;gt; b = a ++ b
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Monoid&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  mempty = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;[]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Great stuff!&lt;&#x2F;p&gt;
&lt;h3 id=&quot;we-get-mconcat-for-free&quot;&gt;We get mconcat for free&lt;&#x2F;h3&gt;
&lt;p&gt;Once we&#x27;ve defined that, we get &lt;code&gt;mconcat&lt;&#x2F;code&gt; (or rather our nice &lt;code&gt;combineList1&lt;&#x2F;code&gt;) for free! All it does is a fold, starting with &lt;code&gt;mempty&lt;&#x2F;code&gt; and then applying &lt;code&gt;mappend&lt;&#x2F;code&gt; to each element in the list. Great!&lt;&#x2F;p&gt;
&lt;h3 id=&quot;combining-numbers&quot;&gt;Combining Numbers&lt;&#x2F;h3&gt;
&lt;p&gt;The empty element is interesting on our number &lt;code&gt;Semigroup&lt;&#x2F;code&gt; instances from before too.&lt;&#x2F;p&gt;
&lt;p&gt;Addition works like this, with an empty element of &lt;code&gt;0&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;Haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-Haskell &quot;&gt;&lt;code class=&quot;language-Haskell&quot; data-lang=&quot;Haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;newtype &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MySum&lt;&#x2F;span&gt;&lt;span&gt; a = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MySum&lt;&#x2F;span&gt;&lt;span&gt; {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;getMySum &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Num &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; Semigroup&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MySum &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MySum&lt;&#x2F;span&gt;&lt;span&gt; a &amp;lt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MySum&lt;&#x2F;span&gt;&lt;span&gt; b = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MySum&lt;&#x2F;span&gt;&lt;span&gt; (a + b)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Num &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; Monoid&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MySum &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;    mempty = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MySum 0
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;ten &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int
&lt;&#x2F;span&gt;&lt;span&gt;ten = getMySum $ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MySum 1 &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MySum 7 &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MySum 2
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- ten == 10
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Why zero? Because adding 0 to a number does nothing!&lt;&#x2F;p&gt;
&lt;p&gt;Multiplication is not the same though.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;newtype &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MyProduct&lt;&#x2F;span&gt;&lt;span&gt; a = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MyProduct&lt;&#x2F;span&gt;&lt;span&gt; {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;getMyProduct &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Num &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; Semigroup&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MyProduct &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MyProduct&lt;&#x2F;span&gt;&lt;span&gt; a &amp;lt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MyProduct&lt;&#x2F;span&gt;&lt;span&gt; b = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MyProduct&lt;&#x2F;span&gt;&lt;span&gt; (a * b)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Num &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; Monoid&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MyProduct &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;    mempty = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MyProduct 1
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;sixtySix &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int
&lt;&#x2F;span&gt;&lt;span&gt;sixtySix = getMyProduct $ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MyProduct 11 &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MyProduct 2 &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MyProduct 3
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- sixtySix == 66
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Here the &lt;code&gt;mempty&lt;&#x2F;code&gt; value must be 1, because multiplying anything by &lt;code&gt;1&lt;&#x2F;code&gt; changes nothing.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;so-what-does-it-all-mean&quot;&gt;So What Does It All Mean?&lt;&#x2F;h3&gt;
&lt;p&gt;This all seems a lot of work to get a free &lt;code&gt;mconcat&lt;&#x2F;code&gt; function, but the &lt;code&gt;Monoid&lt;&#x2F;code&gt; typeclass really comes into it&#x27;s own when used with stuff like &lt;a href=&quot;https:&#x2F;&#x2F;danieljharvey.github.io&#x2F;typeclasses-monoid&#x2F;.&#x2F;posts&#x2F;2018-12-03-typeclasses-foldable.markdown&quot;&gt;Foldable&lt;&#x2F;a&gt;. Just by adding this empty value we get a hell of a lot more &quot;for free&quot;, as such.&lt;&#x2F;p&gt;
&lt;p&gt;Make sense? If not, why not &lt;a href=&quot;&#x2F;contact.html&quot;&gt;get in touch&lt;&#x2F;a&gt;?&lt;&#x2F;p&gt;
&lt;p&gt;Further reading:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;http:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;base-4.12.0.0&#x2F;docs&#x2F;Data-Monoid.html&quot;&gt;Data.Monoid on Hackage&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;medium.com&#x2F;@sjsyrek&#x2F;five-minutes-to-monoid-fe6f364d0bba&quot;&gt;5 minutes to Monoids&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.schoolofhaskell.com&#x2F;user&#x2F;mgsloan&#x2F;monoids-tour&quot;&gt;Monoids tour on School of Haskell&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Typeclasses - Semigroup</title>
          <pubDate>Thu, 03 Jan 2019 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/typeclasses-semigroup/</link>
          <guid>https://danieljharvey.github.io/typeclasses-semigroup/</guid>
          <description xml:base="https://danieljharvey.github.io/typeclasses-semigroup/">&lt;p&gt;Let&#x27;s think about things that can be put together.&lt;&#x2F;p&gt;
&lt;p&gt;Here&#x27;s two lists, combined into one big list.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;list &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;list = [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;] ++ [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;6&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- list == [1,2,3,4,5,6]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Great!&lt;&#x2F;p&gt;
&lt;p&gt;What about two strings combined into one large excellent string?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;string &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: String
&lt;&#x2F;span&gt;&lt;span&gt;string = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Great&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; Stuff&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- string == &amp;quot;Great Stuff&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Sure! Seems great. &lt;code&gt;String&lt;&#x2F;code&gt; is actually a &lt;code&gt;List&lt;&#x2F;code&gt; of &lt;code&gt;Char&lt;&#x2F;code&gt; so really that&#x27;s just the same thing happening twice there. So what&#x27;s &lt;code&gt;Semigroup&lt;&#x2F;code&gt; got to do with this? &lt;code&gt;Semigroup&lt;&#x2F;code&gt; is the generalisation of combining things together. How is it defined?&lt;&#x2F;p&gt;
&lt;h3 id=&quot;definition&quot;&gt;Definition&lt;&#x2F;h3&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Prelude&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; :i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Semigroup
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;...gets us...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Semigroup &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;(&amp;lt;&amp;gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;  {-# &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MINIMAL&lt;&#x2F;span&gt;&lt;span&gt; (&amp;lt;&amp;gt;) #-}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So by the looks we can just define it with the &lt;code&gt;&amp;lt;&amp;gt;&lt;&#x2F;code&gt; function.&lt;&#x2F;p&gt;
&lt;p&gt;Therefore we could write our string concat function as &lt;code&gt;&quot;Great&quot; &amp;lt;&amp;gt; &quot;Job&quot;&lt;&#x2F;code&gt; or combine lists like &lt;code&gt;[1,2,3] &amp;lt;&amp;gt; [4,5,6]&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;It doesn&#x27;t just involve putting sets of things together.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;maybe-semigroup&quot;&gt;Maybe Semigroup&lt;&#x2F;h3&gt;
&lt;p&gt;Let&#x27;s define our own datatype &lt;code&gt;MyMaybe&lt;&#x2F;code&gt; which you may notice is very similar to &lt;code&gt;Maybe&lt;&#x2F;code&gt;. The &lt;code&gt;Semigroup&lt;&#x2F;code&gt; instance can be used to combine two of them.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MyMaybe&lt;&#x2F;span&gt;&lt;span&gt; a = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Yeah&lt;&#x2F;span&gt;&lt;span&gt; a | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nope
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;What&#x27;s interesting about this one is that by insisting that the value inside is also a &lt;code&gt;Semigroup&lt;&#x2F;code&gt;, we can do some exciting multi-level combining.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Semigroup &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; Semigroup&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MyMaybe &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Yeah&lt;&#x2F;span&gt;&lt;span&gt; a) &amp;lt;&amp;gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Yeah&lt;&#x2F;span&gt;&lt;span&gt; b) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Yeah&lt;&#x2F;span&gt;&lt;span&gt; (a &amp;lt;&amp;gt; b)
&lt;&#x2F;span&gt;&lt;span&gt;  a        &amp;lt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nope     &lt;&#x2F;span&gt;&lt;span&gt;= a
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nope     &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;gt; b        = b
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Let&#x27;s see it at work.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;nah &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: MyMaybe String
&lt;&#x2F;span&gt;&lt;span&gt;nah = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nope &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nope
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- nah == Nope
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Sure.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;first &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: MyMaybe String
&lt;&#x2F;span&gt;&lt;span&gt;first = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Yeah &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Totally&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;lt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nope
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- first == Yeah &amp;quot;Totally&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Plausible.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;second &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: MyMaybe String
&lt;&#x2F;span&gt;&lt;span&gt;second = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nope &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Yeah &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Great&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- second == Yeah &amp;quot;Great&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;OK.&lt;&#x2F;p&gt;
&lt;p&gt;And the more interesting one...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;both &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: MyMaybe String
&lt;&#x2F;span&gt;&lt;span&gt;both = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Yeah &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Totally&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; &amp;lt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Yeah &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Great&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- both = Yeah &amp;quot;TotallyGreat&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;What happened here? We combined two &lt;code&gt;MyMaybe&lt;&#x2F;code&gt; values AND the &lt;code&gt;String&lt;&#x2F;code&gt; values inside them as well, without really putting much effort in whatsoever. Great job, &lt;code&gt;Semigroup&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;sum-semigroup&quot;&gt;Sum Semigroup&lt;&#x2F;h3&gt;
&lt;p&gt;We can also use this pattern to describe combining numbers. Integers, for instance, can form several Monoids. One is addition. Let&#x27;s build a &lt;a href=&quot;https:&#x2F;&#x2F;danieljharvey.github.io&#x2F;typeclasses-semigroup&#x2F;.&#x2F;posts&#x2F;2018-12-19-new-types.markdown&quot;&gt;newtype&lt;&#x2F;a&gt; so that we can make a &lt;code&gt;Semigroup&lt;&#x2F;code&gt; instance.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;newtype &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MySum&lt;&#x2F;span&gt;&lt;span&gt; a = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MySum&lt;&#x2F;span&gt;&lt;span&gt; {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;getMySum &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Here is a &lt;code&gt;Semigroup&lt;&#x2F;code&gt; instance, it uses pattern matching to take the original values out, adds them together, them wraps them in another &lt;code&gt;MySum&lt;&#x2F;code&gt; instance.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Num &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; Semigroup&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MySum &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MySum&lt;&#x2F;span&gt;&lt;span&gt; a &amp;lt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MySum&lt;&#x2F;span&gt;&lt;span&gt; b = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MySum&lt;&#x2F;span&gt;&lt;span&gt; (a + b)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We can create several &lt;code&gt;MySum&lt;&#x2F;code&gt; instances, and then combine them together with &lt;code&gt;&amp;lt;&amp;gt;&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;ten &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: MySum Int
&lt;&#x2F;span&gt;&lt;span&gt;ten = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MySum 1 &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MySum 7 &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MySum 2
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- ten == MySum 10
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Or we could use the &lt;code&gt;getMySum&lt;&#x2F;code&gt; function in the newtype to unwrap it.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;anotherTen &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int
&lt;&#x2F;span&gt;&lt;span&gt;anotherTen = getMySum $ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MySum 1 &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MySum 7 &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MySum 2
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- anotherTen == 10
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;(This is obviously quite an overwrought way to add 3 numbers together)&lt;&#x2F;p&gt;
&lt;h3 id=&quot;product-semigroup&quot;&gt;Product Semigroup&lt;&#x2F;h3&gt;
&lt;p&gt;We could also combine numbers by multiplying them together.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;newtype &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MyProd&lt;&#x2F;span&gt;&lt;span&gt; a = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MyProd&lt;&#x2F;span&gt;&lt;span&gt; {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;getMyProd &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Num &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; Semigroup&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MyProd &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MyProd&lt;&#x2F;span&gt;&lt;span&gt; a &amp;lt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MyProd&lt;&#x2F;span&gt;&lt;span&gt; b = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MyProd&lt;&#x2F;span&gt;&lt;span&gt; (a * b)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;sixtySix &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Prod Int
&lt;&#x2F;span&gt;&lt;span&gt;sixtySix = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MyProd 11 &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MyProd 2 &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MyProd 3
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- sixtySix = Prod 66
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;anotherSixtySix &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int
&lt;&#x2F;span&gt;&lt;span&gt;anotherSixtySix = getMyProd $ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MyProd 11 &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MyProd 2 &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MyProd 3
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- anotherSixtySix = 66
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;so-yes&quot;&gt;So, yes.&lt;&#x2F;h3&gt;
&lt;p&gt;Hopefully you get the idea of what&#x27;s going on here. A &lt;code&gt;Semigroup&lt;&#x2F;code&gt; is a nice way of describing things we can smash together, and it becomes even better when we extend it into &lt;code&gt;Monoid&lt;&#x2F;code&gt;, that gets us a load of &lt;code&gt;Foldable&lt;&#x2F;code&gt; stuff for free. We&#x27;ll come to that soon...&lt;&#x2F;p&gt;
&lt;p&gt;Make sense? If not, why not &lt;a href=&quot;&#x2F;contact.html&quot;&gt;get in touch&lt;&#x2F;a&gt;?&lt;&#x2F;p&gt;
&lt;p&gt;Further reading:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;http:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;base-4.12.0.0&#x2F;docs&#x2F;Data-Semigroup.html&quot;&gt;Data.Semigroup&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>My First Haskell Project - Part 1</title>
          <pubDate>Wed, 26 Dec 2018 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/getting-started-pt-1/</link>
          <guid>https://danieljharvey.github.io/getting-started-pt-1/</guid>
          <description xml:base="https://danieljharvey.github.io/getting-started-pt-1/">&lt;p&gt;I have not always found it clear how to get started with Haskell, and I have a small library I want to make as an experiment so I figured I&#x27;d document the process as a sort of Getting Started. The tiny library I wish to make is for reading environment variables, which can be used for database connections or similar. I use MacOS but will try not to make anything too platform specific, and link to docs where info for other systems can be found where possible.&lt;&#x2F;p&gt;
&lt;p&gt;First, let&#x27;s assume we have nothing Haskell-based on the system whatsoever, and start by installing Stack.&lt;&#x2F;p&gt;
&lt;p&gt;On MacOS this is as easy as running&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;brew&lt;&#x2F;span&gt;&lt;span&gt; install stack
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;but for other systems you will need to look at the &lt;a href=&quot;https:&#x2F;&#x2F;docs.haskellstack.org&#x2F;en&#x2F;stable&#x2F;install_and_upgrade&#x2F;&quot;&gt;Stack docs&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Assuming that&#x27;s all gone swimmingly, let&#x27;s start a new project, which we will call &lt;code&gt;simple-env&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;stack&lt;&#x2F;span&gt;&lt;span&gt; new simple-env
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This will create a new folder called &lt;code&gt;simple-env&lt;&#x2F;code&gt; containing the following:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;.gitignore
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ChangeLog.md
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;LICENSE
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;README.md
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Setup.hs
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;app
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;package.yaml
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;simple-env.cabal
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;src
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;stack.yaml
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;test
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Looks great. There are two files here that describe our project... &lt;code&gt;simple-env.cabal&lt;&#x2F;code&gt; generated by Cabal, and &lt;code&gt;package.yaml&lt;&#x2F;code&gt; generated by Stack. Don&#x27;t make the mistake I made and change the Cabal one directly - instead use &lt;code&gt;package.yaml&lt;&#x2F;code&gt; as the source of truth and allow Stack to auto-generate sensible &lt;code&gt;.cabal&lt;&#x2F;code&gt; files.&lt;&#x2F;p&gt;
&lt;p&gt;Here is our default &lt;code&gt;package.yaml&lt;&#x2F;code&gt; file:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;yaml&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-yaml &quot;&gt;&lt;code class=&quot;language-yaml&quot; data-lang=&quot;yaml&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;simple-env
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;version&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0.1.0.0
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;github&lt;&#x2F;span&gt;&lt;span&gt;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;githubuser&#x2F;simple-env&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;license&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;BSD3
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;author&lt;&#x2F;span&gt;&lt;span&gt;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Author name here&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;maintainer&lt;&#x2F;span&gt;&lt;span&gt;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;example@example.com&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;copyright&lt;&#x2F;span&gt;&lt;span&gt;: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;2018 Author name here&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;extra-source-files&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;  - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;README.md
&lt;&#x2F;span&gt;&lt;span&gt;  - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;ChangeLog.md
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# Metadata used when publishing your package
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# synopsis:            Short description of your package
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# category:            Web
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# To avoid duplicated efforts in documentation and dealing with the
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# complications of embedding Haddock markup inside cabal files, it is
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;# common to point users to the README.md file.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;description&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Please see the README on GitHub at &amp;lt;https:&#x2F;&#x2F;github.com&#x2F;githubuser&#x2F;simple-env#readme&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;dependencies&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;  - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;base &amp;gt;= 4.7 &amp;amp;&amp;amp; &amp;lt; 5
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;library&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;source-dirs&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;src
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;executables&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;simple-env-exe&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Main.hs
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;source-dirs&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;app
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ghc-options&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;      - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;-threaded
&lt;&#x2F;span&gt;&lt;span&gt;      - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;-rtsopts
&lt;&#x2F;span&gt;&lt;span&gt;      - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;-with-rtsopts=-N
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;dependencies&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;      - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;simple-env
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;tests&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;simple-env-test&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Spec.hs
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;source-dirs&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;test
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ghc-options&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;      - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;-threaded
&lt;&#x2F;span&gt;&lt;span&gt;      - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;-rtsopts
&lt;&#x2F;span&gt;&lt;span&gt;      - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;-with-rtsopts=-N
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;dependencies&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;      - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;simple-env
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you&#x27;re familar with the Javascript world, this isn&#x27;t a million miles away from a &lt;code&gt;package.json&lt;&#x2F;code&gt; file.&lt;&#x2F;p&gt;
&lt;p&gt;We aren&#x27;t going to need any new libraries for our project, but it seems sensible to explain how that&#x27;s done. Let&#x27;s install &lt;a href=&quot;http:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;contravariant-1.5&#x2F;docs&#x2F;Data-Functor-Contravariant.html&quot;&gt;contravariant&lt;&#x2F;a&gt; package, because why not.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s add it to here:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;yaml&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-yaml &quot;&gt;&lt;code class=&quot;language-yaml&quot; data-lang=&quot;yaml&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;dependencies&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;  - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;base &amp;gt;= 4.7 &amp;amp;&amp;amp; &amp;lt; 5
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;so we have&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;yaml&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-yaml &quot;&gt;&lt;code class=&quot;language-yaml&quot; data-lang=&quot;yaml&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;dependencies&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;  - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;base &amp;gt;= 4.7 &amp;amp;&amp;amp; &amp;lt; 5
&lt;&#x2F;span&gt;&lt;span&gt;  - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;contravariant
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We don&#x27;t mind what version in this case - Stack will choose us a sensible one that fits with our other dependencies, that&#x27;s what it&#x27;s for.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s run&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;stack&lt;&#x2F;span&gt;&lt;span&gt; build
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;and watch the action.&lt;&#x2F;p&gt;
&lt;p&gt;The first time you run this on any given project, Stack will download the GHC compiler and all the libraries so you may wish to take a break and go and read War and Peace or something. After the initial wait subsequent builds will be very quick, but this one is a bit of a stinker.&lt;&#x2F;p&gt;
&lt;p&gt;Ok. Great, we have filled our hard drive with nonsense and we are ready to Haskell.&lt;&#x2F;p&gt;
&lt;p&gt;What else have we got in this folder?&lt;&#x2F;p&gt;
&lt;p&gt;Firstly, we have the &lt;code&gt;app&lt;&#x2F;code&gt; folder which contains one file, &lt;code&gt;Main.hs&lt;&#x2F;code&gt;. This is the entry-point to our application, and looks like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;module &lt;&#x2F;span&gt;&lt;span&gt;Main &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;Lib
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: IO &lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;main = someFunc
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;When a Haskell program is run the &lt;code&gt;main&lt;&#x2F;code&gt; function in a module called &lt;code&gt;Main&lt;&#x2F;code&gt; is run, and then it is responsible for everything else that happens. Therefore we can deduce that this program is importing &lt;code&gt;someFunc&lt;&#x2F;code&gt; from somewhere and running that. Seems plausible. Let&#x27;s run it and see what happens.&lt;&#x2F;p&gt;
&lt;p&gt;If we look back in &lt;code&gt;package.yaml&lt;&#x2F;code&gt; we have a section that looks like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;yaml&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-yaml &quot;&gt;&lt;code class=&quot;language-yaml&quot; data-lang=&quot;yaml&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;executables&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;simple-env-exe&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Main.hs
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;source-dirs&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;app
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ghc-options&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;      - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;-threaded
&lt;&#x2F;span&gt;&lt;span&gt;      - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;-rtsopts
&lt;&#x2F;span&gt;&lt;span&gt;      - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;-with-rtsopts=-N
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;dependencies&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;      - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;simple-env
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;What it telling us? Well, a bunch of things, but two that stick out.&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Firstly, yes, our assumptions were correct - our main source directory is &lt;code&gt;app&lt;&#x2F;code&gt; and the main file is &lt;code&gt;Main.hs&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;li&gt;Secondly, that our executable file is called &lt;code&gt;simple-env-exe&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Let&#x27;s run it then!&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;stack&lt;&#x2F;span&gt;&lt;span&gt; exec simple-env-exe
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It should just print &quot;someFunc&quot; to the screen and exit, which is admittedly quite underwhelming.&lt;&#x2F;p&gt;
&lt;p&gt;We can do better than this.&lt;&#x2F;p&gt;
&lt;p&gt;So it looks like &lt;code&gt;someFunc&lt;&#x2F;code&gt; is a function in &lt;code&gt;src&#x2F;Lib.hs&lt;&#x2F;code&gt;. The whole file looks like this. Let&#x27;s start work in here.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;module &lt;&#x2F;span&gt;&lt;span&gt;Lib
&lt;&#x2F;span&gt;&lt;span&gt;    ( &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;someFunc
&lt;&#x2F;span&gt;&lt;span&gt;    ) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;someFunc &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: IO &lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;someFunc = putStrLn &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;someFunc&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Our library is going to extract environment variables so they can be used in programs. This is helpful for stuff like database credentials that we don&#x27;t want to save in version control.&lt;&#x2F;p&gt;
&lt;p&gt;Here is the MVP version:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;module &lt;&#x2F;span&gt;&lt;span&gt;Lib
&lt;&#x2F;span&gt;&lt;span&gt;    ( &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;someFunc
&lt;&#x2F;span&gt;&lt;span&gt;    ) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import           &lt;&#x2F;span&gt;&lt;span&gt;System.Environment (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;lookupEnv&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;someFunc &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: IO &lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;someFunc = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;    testValue &amp;lt;- lookupEnv &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;TEST_VALUE&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    putStrLn (showResult testValue)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;showResult &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Maybe String -&amp;gt; String
&lt;&#x2F;span&gt;&lt;span&gt;showResult maybeValue = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;case&lt;&#x2F;span&gt;&lt;span&gt; maybeValue &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;of
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; value -&amp;gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;TEST_VALUE&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++ value
&lt;&#x2F;span&gt;&lt;span&gt;    _          -&amp;gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;TEST_VALUE could not found!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Let&#x27;s take this apart a bit.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import           &lt;&#x2F;span&gt;&lt;span&gt;System.Environment (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;lookupEnv&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Firstly, we have an import. We are using the &lt;code&gt;lookupEnv&lt;&#x2F;code&gt; function from System.Environment. If we look in the docs for it in &lt;a href=&quot;http:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;base-4.12.0.0&#x2F;docs&#x2F;System-Environment.html&quot;&gt;Hackage&lt;&#x2F;a&gt; we can see it has the following type signature:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;lookupEnv &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: String -&amp;gt; IO&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Maybe String&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This means we need to give it a &lt;code&gt;String&lt;&#x2F;code&gt; (the name of the environment variable we wish to check for) and it will return a &lt;code&gt;Maybe String&lt;&#x2F;code&gt;. This means if it CAN find a &lt;code&gt;String&lt;&#x2F;code&gt; it will return it, if not, it won&#x27;t explode or anything, which is handy. However, that &lt;code&gt;Maybe String&lt;&#x2F;code&gt; is wrapped in an &lt;code&gt;IO&lt;&#x2F;code&gt;. What does this mean?&lt;&#x2F;p&gt;
&lt;p&gt;It means that this function needs to do interaction with the outside world, therefore it must also be run from another &lt;code&gt;IO&lt;&#x2F;code&gt; function using &lt;code&gt;bind&lt;&#x2F;code&gt;. Think of &lt;code&gt;IO&lt;&#x2F;code&gt; like the electricity that allows access to side-effects and the outside world. It&#x27;s where the problems are most likely to come from, so Haskell forces us to be very explicit about where it is and more importantly, where it isn&#x27;t. Our &lt;code&gt;main&lt;&#x2F;code&gt; functions have access to &lt;code&gt;IO&lt;&#x2F;code&gt;, and they must pass the &lt;code&gt;IO&lt;&#x2F;code&gt;-ness to wherever else needs it.&lt;&#x2F;p&gt;
&lt;p&gt;Opinionated note: &lt;code&gt;IO&lt;&#x2F;code&gt; is one of the more complicated parts of Haskell, and it&#x27;s somewhat unfortunate for adoption of the language that we have to deal with it in the first lines of any program. I would wager that this is why so many tutorials and books start with hacking in the &lt;code&gt;ghci&lt;&#x2F;code&gt; repl instead, as it saves having to have The IO Chat. I am a firm believer in uncomfortable truths, moreso if they are explained terribly like this, but if you are starting out, feel utterly free just to accept this part is a bit weird, learn to live with it, and come back to in depth later. The &lt;code&gt;IO&lt;&#x2F;code&gt; concept really is quite a good thing, it just presents something of a Learning Kerb.&lt;&#x2F;p&gt;
&lt;p&gt;We also have a helper function called &lt;code&gt;showResult&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;showResult &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Maybe String -&amp;gt; String
&lt;&#x2F;span&gt;&lt;span&gt;showResult maybeValue = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;case&lt;&#x2F;span&gt;&lt;span&gt; maybeValue &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;of
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; value -&amp;gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;TEST_VALUE&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++ value
&lt;&#x2F;span&gt;&lt;span&gt;    _          -&amp;gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;TEST_VALUE could not found!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This function just takes our &lt;code&gt;Maybe String&lt;&#x2F;code&gt; and turns it into a &lt;code&gt;String&lt;&#x2F;code&gt; ready for us to print to the console. It uses pattern matching of the &lt;code&gt;maybeValue&lt;&#x2F;code&gt; to display either the result (&lt;code&gt;value&lt;&#x2F;code&gt;) that the environment variable was set to, or a fallback message. Therefore we should get:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;showResult (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;horses&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- &amp;quot;TEST_VALUE: horses&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;showResult (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- &amp;quot;TEST_VALUE could not be found!
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Pretty OK, huh?&lt;&#x2F;p&gt;
&lt;p&gt;OK, lastly the glue function, &lt;code&gt;someFunc&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;someFunc &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: IO &lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;someFunc = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;    testValue &amp;lt;- lookupEnv &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;TEST_VALUE&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    putStrLn (showResult testValue)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;What&#x27;s the deal here? Ok. So firstly, the &lt;code&gt;Do&lt;&#x2F;code&gt; - it says that we&#x27;re starting some &lt;code&gt;Do notation&lt;&#x2F;code&gt; which allows us to write in a slightly more imperative style. Like the &lt;code&gt;IO&lt;&#x2F;code&gt; concept in general it&#x27;s a bit of a heavy concept to throw at beginners in the first few lines so let&#x27;s also make a mental note to just nod and come back to the concept later. Again, it is a pretty neat thing, but confusing to start with.&lt;&#x2F;p&gt;
&lt;p&gt;Moving on, this line is more interesting:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;testValue &amp;lt;- lookupEnv &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;TEST_VALUE&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So as discussed earlier, our &lt;code&gt;lookupEnv&lt;&#x2F;code&gt; takes a &lt;code&gt;String&lt;&#x2F;code&gt; (in this case &quot;TEST_VALUE&quot;) and returns &lt;code&gt;Maybe String&lt;&#x2F;code&gt; wrapped in &lt;code&gt;IO&lt;&#x2F;code&gt;. This &lt;code&gt;&amp;lt;-&lt;&#x2F;code&gt; pulls it out of the &lt;code&gt;IO&lt;&#x2F;code&gt; so that in effect, this line is setting &lt;code&gt;testValue&lt;&#x2F;code&gt; to &lt;code&gt;Maybe String&lt;&#x2F;code&gt;. If &lt;code&gt;lookupEnv&lt;&#x2F;code&gt; finds an environment variable called &quot;TEST_VALUE&quot; then &lt;code&gt;testValue&lt;&#x2F;code&gt; will be &lt;code&gt;Just &quot;whatever_the_value_was&quot;&lt;&#x2F;code&gt;, but if it fails, it&#x27;ll be &lt;code&gt;Nothing&lt;&#x2F;code&gt;. Note that we were only able to even use the &lt;code&gt;lookupEnv&lt;&#x2F;code&gt; function because &lt;code&gt;someFunc&lt;&#x2F;code&gt; is also an &lt;code&gt;IO&lt;&#x2F;code&gt; function, so in effect we have passed the &lt;code&gt;IO&lt;&#x2F;code&gt; power along to &lt;code&gt;lookupEnv&lt;&#x2F;code&gt; to let it do it&#x27;s magic.&lt;&#x2F;p&gt;
&lt;p&gt;Great stuff. We have done a thing. Now let&#x27;s tell our wonderful user all about it.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;putStrLn (showResult testValue)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;OK. So this line just takes &lt;code&gt;testValue&lt;&#x2F;code&gt; (a &lt;code&gt;Maybe String&lt;&#x2F;code&gt;), uses &lt;code&gt;showResult&lt;&#x2F;code&gt; to turn it into a nice &lt;code&gt;String&lt;&#x2F;code&gt; that tells us what happened, and then uses &lt;code&gt;putStrLn&lt;&#x2F;code&gt; (put string line) to show it on the screen. &lt;code&gt;putStrLn&lt;&#x2F;code&gt; is another &lt;code&gt;IO&lt;&#x2F;code&gt; action, with this type signature:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;putStrLn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: String -&amp;gt; IO &lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This means it takes a &lt;code&gt;String&lt;&#x2F;code&gt;, and then returns &lt;code&gt;()&lt;&#x2F;code&gt; (unit, the nothing value) inside an &lt;code&gt;IO&lt;&#x2F;code&gt;. Again, it must be run inside another &lt;code&gt;IO&lt;&#x2F;code&gt; context to get it&#x27;s &quot;power&quot;, as such. Also, the fact it returns &lt;code&gt;()&lt;&#x2F;code&gt; is helpful - our &lt;code&gt;someFunc&lt;&#x2F;code&gt; is of type &lt;code&gt;IO ()&lt;&#x2F;code&gt; meaning it should also return nothing, so &lt;code&gt;putStrLn&lt;&#x2F;code&gt; is also doing that for us. Tidy.&lt;&#x2F;p&gt;
&lt;p&gt;Great stuff.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s try our program.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;stack&lt;&#x2F;span&gt;&lt;span&gt; build
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;stack&lt;&#x2F;span&gt;&lt;span&gt; exec simple-env-exe
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Assuming you&#x27;ve not got an environment variable called &quot;TEST_VALUE&quot; set you should see:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;stack&lt;&#x2F;span&gt;&lt;span&gt; exec simple-env-exe
&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;TEST_VALUE could not found!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Let&#x27;s set one (assuming you&#x27;re in Bash or Zsh):&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;export &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;TEST_VALUE&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;horses!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;...and run the program again.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;stack&lt;&#x2F;span&gt;&lt;span&gt; exec simple-env-exe
&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;TEST_VALUE: horses!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Great stuff. It&#x27;s not much of a library, but it&#x27;s a start. Next time we&#x27;ll make a nicer API for fetching multiple variables at once so it&#x27;s actually a bit more helpful for our users.&lt;&#x2F;p&gt;
&lt;p&gt;Make sense? If not, why not &lt;a href=&quot;&#x2F;contact.html&quot;&gt;get in touch&lt;&#x2F;a&gt;?&lt;&#x2F;p&gt;
&lt;p&gt;Further reading:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;docs.haskellstack.org&#x2F;en&#x2F;stable&#x2F;README&#x2F;&quot;&gt;The Haskell Stack Tool&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;http:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;base-4.12.0.0&#x2F;docs&#x2F;System-Environment.html&quot;&gt;System.Environment&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;wiki.haskell.org&#x2F;Introduction_to_Haskell_IO&#x2F;Actions&quot;&gt;Intro to IO&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;http:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;base-4.12.0.0&#x2F;docs&#x2F;System-IO.html&quot;&gt;System.IO&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;http:&#x2F;&#x2F;learnyouahaskell.com&#x2F;input-and-output&quot;&gt;Learn You A Haskell: IO&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Typeclasses - Traversable</title>
          <pubDate>Sat, 22 Dec 2018 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/typeclasses-traversable/</link>
          <guid>https://danieljharvey.github.io/typeclasses-traversable/</guid>
          <description xml:base="https://danieljharvey.github.io/typeclasses-traversable/">&lt;p&gt;Let&#x27;s think about Trees.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MyTree&lt;&#x2F;span&gt;&lt;span&gt; a = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Leaf&lt;&#x2F;span&gt;&lt;span&gt; a | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Branch&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MyTree&lt;&#x2F;span&gt;&lt;span&gt; a) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MyTree&lt;&#x2F;span&gt;&lt;span&gt; a)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;A tree can be either a &lt;code&gt;Leaf&lt;&#x2F;code&gt; with contains some of value, or a Branch that has two slots for either another &lt;code&gt;Branch&lt;&#x2F;code&gt; or perhaps a &lt;code&gt;Leaf&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;We can build up a tree like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;sampleTree &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: MyTree Int
&lt;&#x2F;span&gt;&lt;span&gt;sampleTree = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Branch&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Branch&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Leaf 2&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Leaf 3&lt;&#x2F;span&gt;&lt;span&gt;)) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Branch&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Leaf 5&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Leaf 2&lt;&#x2F;span&gt;&lt;span&gt;))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So if you remember way back when when we talked about &lt;a href=&quot;&#x2F;posts&#x2F;2018-12-03-typeclasses-foldable.html&quot;&gt;foldable&lt;&#x2F;a&gt;, we can very easily teach this &lt;code&gt;MyTree&lt;&#x2F;code&gt; type we have created to fold over itself and do handy things.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Foldable MyTree where
&lt;&#x2F;span&gt;&lt;span&gt;  foldMap f (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Branch&lt;&#x2F;span&gt;&lt;span&gt; l r) = (foldMap f l) &amp;lt;&amp;gt; (foldMap f r)
&lt;&#x2F;span&gt;&lt;span&gt;  foldMap f (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Leaf&lt;&#x2F;span&gt;&lt;span&gt; a) = f a
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Then we can ask it to do helpful things like add up all the values in the tree.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;sampleTreeTotal &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int
&lt;&#x2F;span&gt;&lt;span&gt;sampleTreeTotal = getSum $ foldMap &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Sum&lt;&#x2F;span&gt;&lt;span&gt; sampleTree
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- sampleTreeTotal == 12
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Great!&lt;&#x2F;p&gt;
&lt;p&gt;This is all very well and good, but what if our tree contains more complex types that just a number? It is entirely plausible to end up with a bunch of optional values like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;maybeTree &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: MyTree&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Maybe Int&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;maybeTree = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Branch&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Branch&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Leaf &lt;&#x2F;span&gt;&lt;span&gt;$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just 2&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Leaf &lt;&#x2F;span&gt;&lt;span&gt;$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just 3&lt;&#x2F;span&gt;&lt;span&gt;)) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Branch&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Leaf &lt;&#x2F;span&gt;&lt;span&gt;$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just 5&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Leaf &lt;&#x2F;span&gt;&lt;span&gt;$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just 2&lt;&#x2F;span&gt;&lt;span&gt;))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We could add up all the values without too much trouble, but what if we want to remove all the &lt;code&gt;Just&lt;&#x2F;code&gt; and &lt;code&gt;Nothing&lt;&#x2F;code&gt; from the &lt;code&gt;MyTree&lt;&#x2F;code&gt; but keep the structure intact? Enter &lt;code&gt;Traversal&lt;&#x2F;code&gt;!&lt;&#x2F;p&gt;
&lt;h3 id=&quot;traversable&quot;&gt;Traversable&lt;&#x2F;h3&gt;
&lt;p&gt;What does &lt;code&gt;ghci&lt;&#x2F;code&gt; have to say about it?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Main&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; :i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Traversable
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Functor &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;t&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Foldable &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;t&lt;&#x2F;span&gt;&lt;span&gt;) =&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Traversable&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;t&lt;&#x2F;span&gt;&lt;span&gt; :: * -&amp;gt; *) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;traverse &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Applicative &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;f &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;f b&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;t a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;t b&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;sequenceA &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Applicative &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;f &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;t&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;f a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;t a&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  {-# &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MINIMAL&lt;&#x2F;span&gt;&lt;span&gt; traverse | sequenceA #-}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;A few things here:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;The &lt;code&gt;(Functor t, Foldable t) =&amp;gt;&lt;&#x2F;code&gt; part means we&#x27;ll need to create &lt;code&gt;Foldable&lt;&#x2F;code&gt; and &lt;code&gt;Functor&lt;&#x2F;code&gt; instances for our datatype before it&#x27;s allowed to be &lt;code&gt;Traversable&lt;&#x2F;code&gt; too.&lt;&#x2F;li&gt;
&lt;li&gt;The &lt;code&gt;Applicative f =&amp;gt;&lt;&#x2F;code&gt; part means whichever other type we use it with must have an instance of &lt;code&gt;Applicative&lt;&#x2F;code&gt;. Fortunately many useful typeclasses are.&lt;&#x2F;li&gt;
&lt;li&gt;We can implement either &lt;code&gt;traverse&lt;&#x2F;code&gt; or &lt;code&gt;sequenceA&lt;&#x2F;code&gt; and the rest will sort itself out.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;We&#x27;ll choose &lt;code&gt;traverse&lt;&#x2F;code&gt; for our example.&lt;&#x2F;p&gt;
&lt;p&gt;Firstly, let&#x27;s make a &lt;code&gt;Functor&lt;&#x2F;code&gt; instance for &lt;code&gt;MyTree&lt;&#x2F;code&gt;. Nothing untoward here, we just recurse through the tree and run &lt;code&gt;f&lt;&#x2F;code&gt; on any &lt;code&gt;a&lt;&#x2F;code&gt;s we find laying around. We&#x27;ll not need to use this directly ourselves but it be used internally by other functions.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Functor MyTree where
&lt;&#x2F;span&gt;&lt;span&gt;  fmap f (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Branch&lt;&#x2F;span&gt;&lt;span&gt; l r) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Branch&lt;&#x2F;span&gt;&lt;span&gt; (fmap f l) (fmap f r)
&lt;&#x2F;span&gt;&lt;span&gt;  fmap f (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Leaf&lt;&#x2F;span&gt;&lt;span&gt; a) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Leaf&lt;&#x2F;span&gt;&lt;span&gt; (f a)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now let&#x27;s implement a &lt;code&gt;Traversable&lt;&#x2F;code&gt; instance for &lt;code&gt;MyTree&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Traversable MyTree where
&lt;&#x2F;span&gt;&lt;span&gt;  traverse f (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Branch&lt;&#x2F;span&gt;&lt;span&gt; l r) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Branch &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;$&amp;gt; (traverse f l) &amp;lt;*&amp;gt; (traverse f r)
&lt;&#x2F;span&gt;&lt;span&gt;  traverse f (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Leaf&lt;&#x2F;span&gt;&lt;span&gt; a) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Leaf &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;$&amp;gt; f a
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;A few notes here:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;&amp;lt;$&amp;gt;&lt;&#x2F;code&gt; is the infix version of &lt;code&gt;fmap&lt;&#x2F;code&gt;. &lt;code&gt;(+1) &amp;lt;$&amp;gt; Just 2&lt;&#x2F;code&gt; is the same as &lt;code&gt;fmap (+1) Just 2&lt;&#x2F;code&gt;. Writing it this way shows us how similar it is to the &lt;code&gt;functor&lt;&#x2F;code&gt; instance.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;&amp;lt;*&amp;gt;&lt;&#x2F;code&gt; is the infix version of &lt;code&gt;apply&lt;&#x2F;code&gt; from &lt;code&gt;Applicative&lt;&#x2F;code&gt;. Our &lt;code&gt;Applicative f =&amp;gt;&lt;&#x2F;code&gt; constraint means this is supplied by whichever &lt;code&gt;Applicative&lt;&#x2F;code&gt; we are using in this function. As we will see, this means different applicatives give us very different outcomes.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;It&#x27;s perhaps not the most intuitive thing to look at and understand, so let&#x27;s try using and see what&#x27;s up. Although we&#x27;ve implemented the typeclass using &lt;code&gt;traverse&lt;&#x2F;code&gt;, the more intuitive function it provides us is &lt;code&gt;sequence&lt;&#x2F;code&gt;, which is sort of a &quot;swap the types around&quot; function. Let&#x27;s try it with a few different instances of &lt;code&gt;Applicative&lt;&#x2F;code&gt; to get a feel for it.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;maybe&quot;&gt;Maybe&lt;&#x2F;h3&gt;
&lt;p&gt;Although we just casually used &lt;code&gt;Maybe&lt;&#x2F;code&gt;, earlier, let&#x27;s clarify what it is. The type definition looks something like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Maybe&lt;&#x2F;span&gt;&lt;span&gt; a = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; a | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It is just for holding a value that might be there (&lt;code&gt;Just &quot;i am a value&quot;&lt;&#x2F;code&gt;) or expressing a lack of value (&lt;code&gt;Nothing&lt;&#x2F;code&gt;). Let&#x27;s put some in our &lt;code&gt;MyTree&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;maybeTree &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: MyTree&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Maybe Int&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;maybeTree = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Branch&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Branch&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Leaf &lt;&#x2F;span&gt;&lt;span&gt;$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just 2&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Leaf &lt;&#x2F;span&gt;&lt;span&gt;$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just 3&lt;&#x2F;span&gt;&lt;span&gt;)) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Branch&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Leaf &lt;&#x2F;span&gt;&lt;span&gt;$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just 5&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Leaf &lt;&#x2F;span&gt;&lt;span&gt;$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just 2&lt;&#x2F;span&gt;&lt;span&gt;))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;...and use &lt;code&gt;sequence&lt;&#x2F;code&gt; to pull them out and wrap the whole thing in a &lt;code&gt;Maybe&lt;&#x2F;code&gt; instead.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;justTree &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Maybe&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MyTree Int&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;justTree = sequence maybeTree
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- justTree == Just (Branch (Branch (Leaf 2) (Leaf 3)) (Branch (Leaf 5) (Leaf 2)))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;What&#x27;s gone on here then? All of the &lt;code&gt;Just&lt;&#x2F;code&gt; values inside the tree have gone, but have been replaced with a single &lt;code&gt;Just&lt;&#x2F;code&gt; at the start. If you squint, it kind of looks like we&#x27;ve turned the types inside out, and we kind of have. If this seems a bit odd to comprehend, it might remind you of this javascript:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;promise1 &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Promise&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;resolve&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;yeah&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;promise2 &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Promise&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;resolve&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;great&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Promise&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;all&lt;&#x2F;span&gt;&lt;span&gt;([&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;promise1&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;promise2&lt;&#x2F;span&gt;&lt;span&gt;]).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;then&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;as &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;console&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;as&lt;&#x2F;span&gt;&lt;span&gt;); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; [&amp;quot;yeah&amp;quot;, &amp;quot;great&amp;quot;]
&lt;&#x2F;span&gt;&lt;span&gt;})
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Here we can taken an array of Promises, and returned a single Promise returning an array of values. However, what happens if one of those promises fails?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;promise1 &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Promise&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;resolve&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;yeah&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;promise2 &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Promise&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;reject&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;great&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Promise&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;all&lt;&#x2F;span&gt;&lt;span&gt;([&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;promise1&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;promise2&lt;&#x2F;span&gt;&lt;span&gt;]).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;then&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;as &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; never happens!
&lt;&#x2F;span&gt;&lt;span&gt;}).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;catch&lt;&#x2F;span&gt;&lt;span&gt;(() &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;console&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;everything went terribly wrong&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39;)
&lt;&#x2F;span&gt;&lt;span&gt;})
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It short circuits and fails! Going back to our tree, let&#x27;s try putting a &lt;code&gt;Nothing&lt;&#x2F;code&gt; in there and see how that changes things.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;nothingTree &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Maybe&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MyTree Int&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;nothingTree = sequence &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Branch&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Leaf Nothing&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Leaf &lt;&#x2F;span&gt;&lt;span&gt;$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just 3&lt;&#x2F;span&gt;&lt;span&gt;))
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- nothingTree == Nothing
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Ok! So the same behaviour! Why is that though? There&#x27;s nothing in our &lt;code&gt;MyTree&lt;&#x2F;code&gt; structure that does any checking of these sorts of things.&lt;&#x2F;p&gt;
&lt;p&gt;The key here is in how &lt;code&gt;apply&lt;&#x2F;code&gt; (or &lt;code&gt;&amp;lt;*&amp;gt;&lt;&#x2F;code&gt;) is implemented in &lt;code&gt;Maybe&lt;&#x2F;code&gt; itself:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Applicative Maybe where
&lt;&#x2F;span&gt;&lt;span&gt;    pure = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; f  &amp;lt;*&amp;gt; m      = fmap f m
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;*&amp;gt; _      = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Basically, as soon as we find any &lt;code&gt;Nothing&lt;&#x2F;code&gt;, whatever we&#x27;re doing becomes &lt;code&gt;Nothing&lt;&#x2F;code&gt;, so therefore &lt;code&gt;Maybe&lt;&#x2F;code&gt; is giving us our same short-circuiting behaviour from the javascript Promise.&lt;&#x2F;p&gt;
&lt;p&gt;Good? Great. Let&#x27;s look at another.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;list&quot;&gt;List&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;code&gt;List&lt;&#x2F;code&gt; is another interesting &lt;code&gt;Applicative&lt;&#x2F;code&gt; in that it treats every list like a set of possibilities.&lt;&#x2F;p&gt;
&lt;p&gt;It&#x27;s defined as something like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;List&lt;&#x2F;span&gt;&lt;span&gt; a = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Cons&lt;&#x2F;span&gt;&lt;span&gt; a (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;List&lt;&#x2F;span&gt;&lt;span&gt; a) | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nil
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Look what happens when we &lt;code&gt;sequence&lt;&#x2F;code&gt; this small tree that contains a &lt;code&gt;List&lt;&#x2F;code&gt; in each of it&#x27;s leaves...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;invertedListTree &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MyTree Int&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;invertedListTree = sequence &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Branch&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Leaf&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;]) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Leaf&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;])
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;{-
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;invertedListTree ==
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;  [ Branch (Leaf 1) (Leaf 3)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;  , Branch (Leaf 1) (Leaf 4)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;  , Branch (Leaf 2) (Leaf 3)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;  , Branch (Leaf 2) (Leaf 4)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;  ]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It returns a &lt;code&gt;List&lt;&#x2F;code&gt; of every possible &lt;code&gt;MyTree Int&lt;&#x2F;code&gt; that could be made using the items in each list. If you look at the page on &lt;a href=&quot;&#x2F;posts&#x2F;2018-11-17-typeclasses-applicative.markdown&quot;&gt;applicative&lt;&#x2F;a&gt; - particularly the &lt;code&gt;applicativeList&lt;&#x2F;code&gt; - this may make more sense.&lt;&#x2F;p&gt;
&lt;p&gt;This &lt;code&gt;sequence&lt;&#x2F;code&gt; function we are using is merely &lt;code&gt;traverse id&lt;&#x2F;code&gt; by the way - so we can start to mess with it even more by using &lt;code&gt;traverse&lt;&#x2F;code&gt; with different functions. By passing the &lt;code&gt;reverse&lt;&#x2F;code&gt; function, we can get the same thing but backwards...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;reversedListTree &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MyTree Int&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;reversedListTree = traverse reverse listTree
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;{-
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;reversedListTree ==
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;  [ Branch (Leaf 2) (Leaf 4)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;  , Branch (Leaf 2) (Leaf 3)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;  , Branch (Leaf 1) (Leaf 4)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;  , Branch (Leaf 1) (Leaf 3)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;(Why? I don&#x27;t know, it&#x27;s difficult coming up with useful examples all the time, give me a break.)&lt;&#x2F;p&gt;
&lt;p&gt;What about &lt;code&gt;Either&lt;&#x2F;code&gt;?&lt;&#x2F;p&gt;
&lt;h3 id=&quot;either&quot;&gt;Either&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;code&gt;Either&lt;&#x2F;code&gt; is used to express a value that could be one of two things. It&#x27;s datatype looks something like:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Either&lt;&#x2F;span&gt;&lt;span&gt; a b = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left&lt;&#x2F;span&gt;&lt;span&gt; a | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span&gt; b
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;Left&lt;&#x2F;code&gt; usually expresses an error or something, whilst &lt;code&gt;Right&lt;&#x2F;code&gt; expresses everything being somewhat Hunky Dory.&lt;&#x2F;p&gt;
&lt;p&gt;What happens when all the values are &lt;code&gt;Right&lt;&#x2F;code&gt;?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;rightTree &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Either String&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MyTree Int&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;rightTree = sequence &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Branch&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Leaf &lt;&#x2F;span&gt;&lt;span&gt;$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right 100&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Leaf &lt;&#x2F;span&gt;&lt;span&gt;$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right 200&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- rightTree == Right (Branch (Leaf 100) (Leaf 200))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;OK, that seems reasonable, just like the &lt;code&gt;Maybe&lt;&#x2F;code&gt; really.&lt;&#x2F;p&gt;
&lt;p&gt;What about if we throw a &lt;code&gt;Left&lt;&#x2F;code&gt; in there?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;failsTree &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Either String&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MyTree Int&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;failsTree = sequence $ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Branch&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Leaf &lt;&#x2F;span&gt;&lt;span&gt;$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right 1&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Leaf &lt;&#x2F;span&gt;&lt;span&gt;$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- failsTree == Left &amp;quot;2&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;OK - we get the &lt;code&gt;Left&lt;&#x2F;code&gt; value back, which seems reasonable (especially looking at the type signature of our new structure).&lt;&#x2F;p&gt;
&lt;p&gt;What about if there are multiple &lt;code&gt;Left&lt;&#x2F;code&gt; values inside?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;failsTree2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Either String&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MyTree Int&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;failsTree2 = sequence $ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Branch&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Leaf &lt;&#x2F;span&gt;&lt;span&gt;$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Branch&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Leaf &lt;&#x2F;span&gt;&lt;span&gt;$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Leaf &lt;&#x2F;span&gt;&lt;span&gt;$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Left &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;))
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- failsTree2 == Left &amp;quot;1&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Oh. That seems somewhat counterintuitive. Even though we have many &lt;code&gt;Left&lt;&#x2F;code&gt; items in our tree, when we &lt;code&gt;sequence&lt;&#x2F;code&gt; them we only get the first one back. If you want to know why - sure - it&#x27;s because of the way &lt;code&gt;Either&lt;&#x2F;code&gt; implements the &lt;code&gt;&amp;lt;*&amp;gt;&lt;&#x2F;code&gt; function. Is there an alterntaive in which we get more of the &lt;code&gt;Left&lt;&#x2F;code&gt; items back? Yes - see the bonus item at the bottom of the page.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;so-what-does-this-mean&quot;&gt;So what does this mean?&lt;&#x2F;h3&gt;
&lt;p&gt;Basically (lol), what these examples hopefully start to show is that &lt;code&gt;traverse&lt;&#x2F;code&gt; let&#x27;s us combine different types together. Whilst &lt;code&gt;Foldable&lt;&#x2F;code&gt; used a &lt;code&gt;Monoid&lt;&#x2F;code&gt; instance to combine values together with &lt;code&gt;&amp;lt;&amp;gt;&lt;&#x2F;code&gt;, &lt;code&gt;Traversable&lt;&#x2F;code&gt; lets us combine them together using their &lt;code&gt;Applicative&lt;&#x2F;code&gt; instance and &lt;code&gt;&amp;lt;*&amp;gt;&lt;&#x2F;code&gt;. &lt;code&gt;Applicative&lt;&#x2F;code&gt; is a pretty powerful typeclass that let&#x27;s us do a lot of wild shit, so &lt;code&gt;Traversable&lt;&#x2F;code&gt; ends up pretty powerful as a result. A lot of the &lt;code&gt;Lens&lt;&#x2F;code&gt; package uses &lt;code&gt;Traversable&lt;&#x2F;code&gt;, so understanding this gives you a much better idea of what&#x27;s doing.&lt;&#x2F;p&gt;
&lt;p&gt;There are plenty of &lt;code&gt;Traversable&lt;&#x2F;code&gt; instances in the wild to play with, so try smashing a few things together and see what happens. In case it&#x27;s not entirely obvious, that&#x27;s basically all I did in writing this article.&lt;&#x2F;p&gt;
&lt;p&gt;Anyhow, that&#x27;s enough for now I think.&lt;&#x2F;p&gt;
&lt;p&gt;Make sense? If not, why not &lt;a href=&quot;&#x2F;contact.html&quot;&gt;get in touch&lt;&#x2F;a&gt;?&lt;&#x2F;p&gt;
&lt;p&gt;Further reading:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;http:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;base-4.12.0.0&#x2F;docs&#x2F;Data-Traversable.html&quot;&gt;Data.Traversable&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;bonus-item-validation&quot;&gt;Bonus item: Validation&lt;&#x2F;h3&gt;
&lt;p&gt;If you were enjoying the &lt;code&gt;Either&lt;&#x2F;code&gt; example but wondering if there&#x27;s a way to gather all of the &lt;code&gt;Left&lt;&#x2F;code&gt; values in a tree, then, firstly, yes, and secondly, it&#x27;s done using a different datatype called &lt;code&gt;Validation&lt;&#x2F;code&gt;. We won&#x27;t go into it in depth now, but it&#x27;s much like an &lt;code&gt;Either&lt;&#x2F;code&gt; that let&#x27;s you collect &lt;code&gt;Left&lt;&#x2F;code&gt; items together.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;validationTree &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: MyTree&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Validation&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;validationTree = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Branch&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Leaf &lt;&#x2F;span&gt;&lt;span&gt;$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Success 100&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Branch&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Leaf &lt;&#x2F;span&gt;&lt;span&gt;$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Failure&lt;&#x2F;span&gt;&lt;span&gt; [&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;]) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Leaf &lt;&#x2F;span&gt;&lt;span&gt;$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Failure&lt;&#x2F;span&gt;&lt;span&gt; [&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;]))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;collectFails &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Validation&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;] (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MyTree Int&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;collectFails = traverse id validationTree
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- collectFails == Failure [&amp;quot;2&amp;quot;,&amp;quot;3&amp;quot;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</description>
      </item>
      <item>
          <title>Why The Hell Should I Care About Newtypes?</title>
          <pubDate>Wed, 19 Dec 2018 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/new-types/</link>
          <guid>https://danieljharvey.github.io/new-types/</guid>
          <description xml:base="https://danieljharvey.github.io/new-types/">&lt;p&gt;Good question. What are &lt;code&gt;newtypes&lt;&#x2F;code&gt;?&lt;&#x2F;p&gt;
&lt;p&gt;You see them in Haskell a lot. Here&#x27;s one.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;newtype &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Dog&lt;&#x2F;span&gt;&lt;span&gt; a = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Dog&lt;&#x2F;span&gt;&lt;span&gt; { getDog :: a }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We can make a &lt;code&gt;Dog&lt;&#x2F;code&gt; as a container for a thing (in this case, a &lt;code&gt;String&lt;&#x2F;code&gt;)&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;frank &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Dog String
&lt;&#x2F;span&gt;&lt;span&gt;frank = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Dog &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Frank&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- frank == Dog &amp;quot;Frank&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Or we can unwrap it again and lose nothing along the way (this means the types &lt;code&gt;String&lt;&#x2F;code&gt; and &lt;code&gt;Dog String&lt;&#x2F;code&gt; are &lt;code&gt;isomorphic&lt;&#x2F;code&gt; in maths terms)&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;name &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: String
&lt;&#x2F;span&gt;&lt;span&gt;name = getDog frank
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- name == &amp;quot;Frank&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In short, they are basically the same thing. Once compiled in fact, they&#x27;re exactly the same, so there&#x27;s no cost to all this, computationally.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;itsTheSame &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Bool
&lt;&#x2F;span&gt;&lt;span&gt;itsTheSame = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Frank&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; == getDog (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Dog &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Frank&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So why do this?&lt;&#x2F;p&gt;
&lt;p&gt;Well, the nice thing about a &lt;code&gt;newtype&lt;&#x2F;code&gt; is that we can use it to pass data around with a bit more contextual information about what it means.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s calculate a salary. That seems like a plausible thing to do with a computer.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;calculateSalaryBad &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int -&amp;gt; Int
&lt;&#x2F;span&gt;&lt;span&gt;calculateSalaryBad months = months * &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1000
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This function takes a number of months, and calculates how much this person should get paid, based on a salary of &lt;code&gt;1000&lt;&#x2F;code&gt; (of some unknown unit) a month.&lt;&#x2F;p&gt;
&lt;p&gt;But what happens if we give it an invalid number of months?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;badAmount = calculateSalaryBad (-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;100&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- badAmount == -100000
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That&#x27;s crazy talk! Surely this weird minus payment will send even the most well-meaning of accountants into a spin.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s improve it a bit by checking if the number is negative.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;calculateSalaryBetter &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Num &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Ord &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Maybe &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;calculateSalaryBetter i = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; i &amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span&gt;                        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;then &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing
&lt;&#x2F;span&gt;&lt;span&gt;                        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; (i * &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1000&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Note that we&#x27;re introducing the &lt;code&gt;Ord&lt;&#x2F;code&gt; typeclass here, as we need to compare amounts. We don&#x27;t mind what &lt;code&gt;a&lt;&#x2F;code&gt; is as long as it is both a valid number (ie, in the &lt;code&gt;Num&lt;&#x2F;code&gt; typeclass) and is orderable (ie, the &lt;code&gt;Ord&lt;&#x2F;code&gt; typeclass).&lt;&#x2F;p&gt;
&lt;p&gt;So now if we try this on a stupid amount, we get &lt;code&gt;Nothing&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;safeAmount = calculateSalaryBetter (-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;100&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- safeAmount = Nothing
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;OK. Good stuff. That should stop the accounts department crying into their sensibly priced but ultimately unsatisfying packed lunches.&lt;&#x2F;p&gt;
&lt;p&gt;The thing is, when we run this, we get this &lt;code&gt;Just&lt;&#x2F;code&gt; wrapped around things.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;anAmount = calculateSalaryBetter &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;12
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- anAmount == Just 12000
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is fine in isolation, but if we wanted to do a lot of calculations here we don&#x27;t want to be wrapping and unwrapping &lt;code&gt;Maybe&lt;&#x2F;code&gt; values all over the place. It means mixing up our validation logic with our actual business logic or whatever, and that&#x27;s Bad.&lt;&#x2F;p&gt;
&lt;p&gt;What about a nice &lt;code&gt;newtype&lt;&#x2F;code&gt; solution?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;newtype &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;PositiveNum&lt;&#x2F;span&gt;&lt;span&gt; a = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;PositiveNum&lt;&#x2F;span&gt;&lt;span&gt; { getPositiveNum :: a } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Eq&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Show&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Nothing to write home about so far, but the trick here is that Haskell allows us to export the type &lt;code&gt;PositiveNum&lt;&#x2F;code&gt; but not the constructor &lt;code&gt;PositiveNum&lt;&#x2F;code&gt;. That means that instead we can provide a function for making a &lt;code&gt;PositiveNum&lt;&#x2F;code&gt; that does some validation. This means that, outside our module itself, there is no way to create a &lt;code&gt;PositiveNum&lt;&#x2F;code&gt; that doesn&#x27;t make sense.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;makePositiveNum &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Num &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Ord &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Maybe&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;PositiveNum &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;makePositiveNum i
&lt;&#x2F;span&gt;&lt;span&gt;    | i &amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0 &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing
&lt;&#x2F;span&gt;&lt;span&gt;    | otherwise = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;PositiveNum&lt;&#x2F;span&gt;&lt;span&gt; i)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It comes wrapped in a &lt;code&gt;Maybe&lt;&#x2F;code&gt;, sure, but only one. It can be used over and over without needing validation, and once it is available it can be unwrapped with a quick &lt;code&gt;getPositiveNum&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;num &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int
&lt;&#x2F;span&gt;&lt;span&gt;num = getPositiveNum (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;PositiveNum 10&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- num == 10
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Great stuff.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s make a nicer salary calculator.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;calculateSalary &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Num &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; PositiveNum &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;calculateSalary months = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1000&lt;&#x2F;span&gt;&lt;span&gt; * (getPositiveNum months)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Pretty OK. Let&#x27;s bring it all together. First our library functions:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;makePositiveNum &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Num &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Ord &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Maybe&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;PositiveNum &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;makePositiveNum i
&lt;&#x2F;span&gt;&lt;span&gt;    | i &amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0 &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing
&lt;&#x2F;span&gt;&lt;span&gt;    | otherwise = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;PositiveNum&lt;&#x2F;span&gt;&lt;span&gt; i)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;zero &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Num &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; PositiveNum &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;zero = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;PositiveNum 0
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We&#x27;ve added &lt;code&gt;zero&lt;&#x2F;code&gt; that just makes a default &lt;code&gt;PositiveNum&lt;&#x2F;code&gt; with a value of &lt;code&gt;0&lt;&#x2F;code&gt; here, to use as a fallback if the value is ridiculous.&lt;&#x2F;p&gt;
&lt;p&gt;Now we have a function for getting a &lt;code&gt;PositiveNum&lt;&#x2F;code&gt; for our number of months:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;months &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Num &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Ord &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; PositiveNum &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;months i = fromMaybe zero (makePositiveNum i)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;yes &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: PositiveNum Int
&lt;&#x2F;span&gt;&lt;span&gt;yes = months &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;12
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- yes = PositiveNum 12
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;nope &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: PositiveNum Int
&lt;&#x2F;span&gt;&lt;span&gt;nope = months (-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;12&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- nope = PositiveNum 0
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Which we can use as follows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;total = calculateSalary (months &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;12&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- total == 12000
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Nice. By pushing all of the validation concerns into the &lt;code&gt;months&lt;&#x2F;code&gt; function, our actual function is nice and simple and easy to understand. Also we have a nice re-usable tool, &lt;code&gt;PositiveNum&lt;&#x2F;code&gt; that can be used across our project everytime we need some guarantees about a value.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;bonus-credit-functor-instance-for-a-newtype&quot;&gt;Bonus credit: Functor instance for a &lt;code&gt;newtype&lt;&#x2F;code&gt;.&lt;&#x2F;h3&gt;
&lt;p&gt;We can treat &lt;code&gt;newtypes&lt;&#x2F;code&gt; like any other type, and create typeclass instances for them. For instance, we could create a &lt;code&gt;functor&lt;&#x2F;code&gt; instance for &lt;code&gt;PositiveNum&lt;&#x2F;code&gt; and do calculations inside it by mapping instead.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Functor PositiveNum where
&lt;&#x2F;span&gt;&lt;span&gt;    fmap f (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;PositiveNum&lt;&#x2F;span&gt;&lt;span&gt; i) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;PositiveNum&lt;&#x2F;span&gt;&lt;span&gt; (f i)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This lets us change the value inside &lt;code&gt;PositiveNum&lt;&#x2F;code&gt; with an &lt;code&gt;fmap&lt;&#x2F;code&gt; function.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;calculateSalaryClever &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Num &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; PositiveNum &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; PositiveNum &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;calculateSalaryClever = fmap (*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1000&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- calculateSalaryClever 2 == PositiveNum 2000
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Or the same, but unwrap it afterwards:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;calculateSalaryClever2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Num &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; PositiveNum &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;calculateSalaryClever2 i = getPositiveNum (fmap (*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1000&lt;&#x2F;span&gt;&lt;span&gt;) i)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- calculateSalaryClever2 20 == 20000
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That seems pretty OK to me. Anyway, that&#x27;s enough things, time for bed.&lt;&#x2F;p&gt;
&lt;p&gt;Make sense? If not, why not &lt;a href=&quot;&#x2F;contact.html&quot;&gt;get in touch&lt;&#x2F;a&gt;?&lt;&#x2F;p&gt;
&lt;p&gt;Further reading:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;wiki.haskell.org&#x2F;Newtype&quot;&gt;Newtype in Haskell Wiki&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;http:&#x2F;&#x2F;degoes.net&#x2F;articles&#x2F;newtypes-suck&quot;&gt;Newtypes aren&#x27;t as cool as you think&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Why The Hell Should I Care About Lens? (Part 2)</title>
          <pubDate>Tue, 18 Dec 2018 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/optics-pt-2-prism/</link>
          <guid>https://danieljharvey.github.io/optics-pt-2-prism/</guid>
          <description xml:base="https://danieljharvey.github.io/optics-pt-2-prism/">&lt;p&gt;So last time we looked at &lt;a href=&quot;&#x2F;posts&#x2F;2018-10-30-optics-pt-1-lens.html&quot;&gt;lens&lt;&#x2F;a&gt; and saw had to jump into record-shaped structures and change things around like big hacker professionals. However we didn&#x27;t try to change anything with a &lt;code&gt;sum type&lt;&#x2F;code&gt; in it, like &lt;code&gt;Either&lt;&#x2F;code&gt; or &lt;code&gt;Maybe&lt;&#x2F;code&gt; or something. What can we use for that? Only a bloody &lt;code&gt;Prism&lt;&#x2F;code&gt;, apparently.&lt;&#x2F;p&gt;
&lt;p&gt;Here&#x27;s an example sum type that can either contain a dog&#x27;s name or it&#x27;s age, and is in no way utterly contrived.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;DogFact &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;DogName String &lt;&#x2F;span&gt;&lt;span&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;DogAge Int
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Here are some examples of it is in use. Here it is valiantly holding a dog&#x27;s name, a &lt;code&gt;String&lt;&#x2F;code&gt;, with the &lt;code&gt;DogName&lt;&#x2F;code&gt; constructor.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;spruceBruce &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: DogFact
&lt;&#x2F;span&gt;&lt;span&gt;spruceBruce = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;DogName &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Spruce Bruce&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Great!&lt;&#x2F;p&gt;
&lt;p&gt;And here, instead, is a dog&#x27;s age, stored as an &lt;code&gt;Int&lt;&#x2F;code&gt; inside &lt;code&gt;DogAge&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;oldDog &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: DogFact
&lt;&#x2F;span&gt;&lt;span&gt;oldDog = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;DogAge 100
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Excellent stuff, I&#x27;m sure you&#x27;ll agree.&lt;&#x2F;p&gt;
&lt;p&gt;Now, we could start making all our getters and settings by hand again like this...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;getDogNameFromDogFact &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: DogFact -&amp;gt; Maybe String
&lt;&#x2F;span&gt;&lt;span&gt;getDogNameFromDogFact (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;DogName&lt;&#x2F;span&gt;&lt;span&gt; s) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; s
&lt;&#x2F;span&gt;&lt;span&gt;getDogNameFromDogFact _           = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;getDogAgeFromDogFact &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: DogFact -&amp;gt; Maybe Int
&lt;&#x2F;span&gt;&lt;span&gt;getDogAgeFromDogFact (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;DogAge&lt;&#x2F;span&gt;&lt;span&gt; s) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; s
&lt;&#x2F;span&gt;&lt;span&gt;getDogAgeFromDogFact _          = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;...but I&#x27;m sure it&#x27;s clear it&#x27;s soon going to get quite verbose and we should probably use something more clever instead.&lt;&#x2F;p&gt;
&lt;p&gt;Enter &lt;code&gt;Prism&lt;&#x2F;code&gt;! (the crowd goes wild, etc.)&lt;&#x2F;p&gt;
&lt;p&gt;A &lt;code&gt;Prism&lt;&#x2F;code&gt; is like a &lt;code&gt;Lens&lt;&#x2F;code&gt;, except it let&#x27;s you peek into a particular part of a &lt;code&gt;sum type&lt;&#x2F;code&gt;. Therefore we can make one &lt;code&gt;Prism&lt;&#x2F;code&gt; that is interested in the name of dogs...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;dogNamePrism &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Prism&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;DogFact String
&lt;&#x2F;span&gt;&lt;span&gt;dogNamePrism = prism&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;DogName&lt;&#x2F;span&gt;&lt;span&gt; (\e -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;case&lt;&#x2F;span&gt;&lt;span&gt; e &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;of
&lt;&#x2F;span&gt;&lt;span&gt;                                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;DogName&lt;&#x2F;span&gt;&lt;span&gt; a -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; a
&lt;&#x2F;span&gt;&lt;span&gt;                                _         -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;...and another which only cares about the age of dogs...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;dogAgePrism &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Prism&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;DogFact Int
&lt;&#x2F;span&gt;&lt;span&gt;dogAgePrism = prism&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;DogAge&lt;&#x2F;span&gt;&lt;span&gt; (\e -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;case&lt;&#x2F;span&gt;&lt;span&gt; e &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;of
&lt;&#x2F;span&gt;&lt;span&gt;                              &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;DogAge&lt;&#x2F;span&gt;&lt;span&gt; b -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; b
&lt;&#x2F;span&gt;&lt;span&gt;                              _        -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;OK. So how do we use them?&lt;&#x2F;p&gt;
&lt;p&gt;In &lt;code&gt;lens&lt;&#x2F;code&gt;, we had &lt;code&gt;view&lt;&#x2F;code&gt; for peeking inside, &lt;code&gt;set&lt;&#x2F;code&gt; for changing values, and &lt;code&gt;over&lt;&#x2F;code&gt; for mapping over the values inside. What has &lt;code&gt;Prism&lt;&#x2F;code&gt; got going on?&lt;&#x2F;p&gt;
&lt;h3 id=&quot;preview&quot;&gt;preview&lt;&#x2F;h3&gt;
&lt;p&gt;Unlike &lt;code&gt;lens&lt;&#x2F;code&gt; which has &lt;code&gt;set&lt;&#x2F;code&gt;, &lt;code&gt;prism&lt;&#x2F;code&gt; has a good pal called &lt;code&gt;preview&lt;&#x2F;code&gt; that it uses for checking out values. It returns the result inside a &lt;code&gt;Maybe&lt;&#x2F;code&gt; as the value is not guaranteed to actually be there.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;dogAge &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Maybe Int
&lt;&#x2F;span&gt;&lt;span&gt;dogAge = preview dogAgePrism (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;DogAge 100&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- dogAge == Just 100
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That seems reasonable.&lt;&#x2F;p&gt;
&lt;p&gt;What about running that on &lt;code&gt;spruceBruce&lt;&#x2F;code&gt; (that is built from a &lt;code&gt;DogName&lt;&#x2F;code&gt;)?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;notDogAge &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Maybe Int
&lt;&#x2F;span&gt;&lt;span&gt;notDogAge = preview dogAgePrism spruceBruce
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- notDogAge == Nothing
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;No dice. Zero dice. A complete lack of dice. No age, no &lt;code&gt;int&lt;&#x2F;code&gt;, sorry.&lt;&#x2F;p&gt;
&lt;p&gt;Maybe our &lt;code&gt;dogNamePrism&lt;&#x2F;code&gt; can help though...?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;dogName &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Maybe String
&lt;&#x2F;span&gt;&lt;span&gt;dogName = preview dogNamePrism spruceBruce
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- dogName == Just &amp;quot;Spruce Bruce&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Yes! Good stuff. Does he work on ages though?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;notDogName &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Maybe String
&lt;&#x2F;span&gt;&lt;span&gt;notDogName = preview dogNamePrism (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;DogAge 69&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- notDogName == Nothing
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;No. Damn.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;set&quot;&gt;set&lt;&#x2F;h3&gt;
&lt;p&gt;In &lt;code&gt;lens&lt;&#x2F;code&gt; we used &lt;code&gt;set&lt;&#x2F;code&gt; for changing the value of something nested. How does that work here?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;youngerDog &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: DogFact
&lt;&#x2F;span&gt;&lt;span&gt;youngerDog = set dogAgePrism &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;27&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;DogAge 100&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- youngerDog == DogAge 27
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;All seems well here, pretty similar to a regular &lt;code&gt;lens&lt;&#x2F;code&gt;. How about we give our dog a name instead of an age?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;noNewName &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: DogFact
&lt;&#x2F;span&gt;&lt;span&gt;noNewName = set dogNamePrism &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Nice Name&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;DogAge 100&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- noNewName == DogAge 100
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;What&#x27;s happened here? Where is the name? We&#x27;ve ended up with exactly what we started with! How the hell do we give the dog a name? I must admit this confused me for quite a while, until I realised that &lt;code&gt;Prism&lt;&#x2F;code&gt; is doing exactly as it should - letting us get at the values inside a sum type without changing it&#x27;s structure.&lt;&#x2F;p&gt;
&lt;p&gt;Therefore we can change one &lt;code&gt;DogName&lt;&#x2F;code&gt; for another...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;newDogName &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;dogFact
&lt;&#x2F;span&gt;&lt;span&gt;newDogName = set dogNamePrism &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Excellent Bruce&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;DogName &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Steve&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- newDogName == DogName &amp;quot;Excellent Bruce&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;...but not change the name of &lt;code&gt;DogAge&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;noNewDogName &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;dogFact
&lt;&#x2F;span&gt;&lt;span&gt;noNewDogName = set dogNamePrism &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Good Old Nigel&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;DogAge 400&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- noNewDogName == DogAge 400
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;over&quot;&gt;over&lt;&#x2F;h3&gt;
&lt;p&gt;As well as inheriting &lt;code&gt;set&lt;&#x2F;code&gt; from &lt;code&gt;lens&lt;&#x2F;code&gt; the &lt;code&gt;prism&lt;&#x2F;code&gt; also has &lt;code&gt;over&lt;&#x2F;code&gt;, that lets us map a function over nested value.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;stillQuiteOldDog &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: DogFact
&lt;&#x2F;span&gt;&lt;span&gt;stillQuiteOldDog = over dogAgePrism (-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) oldDog
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- stillQuiteOldDog == DogAge 99
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;When you think about how mapping a function doesn&#x27;t change the structure around the value (hello, &lt;code&gt;functor&lt;&#x2F;code&gt;) then the behaviour of &lt;code&gt;set&lt;&#x2F;code&gt; that initially confused me so much seems a lot more reasonable. The &lt;code&gt;over&lt;&#x2F;code&gt; function just lets us muddle around with whatever is inside our chosen sum type, should it be there to muddle around with. As such. Clear as mud, right?&lt;&#x2F;p&gt;
&lt;h3 id=&quot;composition&quot;&gt;Composition&lt;&#x2F;h3&gt;
&lt;p&gt;Ok. So like all these examples, the one above is quite simple so that you can see what&#x27;s going on, but like all abstractions, it can leave us thinking &quot;thanks pal, but this all seems a little bit much like hard work, surely&quot;. But what makes it all worthwhile is when we combine a &lt;code&gt;Prism&lt;&#x2F;code&gt; with other optics to make a giant super-optic. Remember our example from part one?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;appData &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: AppConfig
&lt;&#x2F;span&gt;&lt;span&gt;appData = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;AppConfig&lt;&#x2F;span&gt;&lt;span&gt; {
&lt;&#x2F;span&gt;&lt;span&gt;    count = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right 100
&lt;&#x2F;span&gt;&lt;span&gt;  , title = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Hello&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  , dbConfig = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;DbConfig&lt;&#x2F;span&gt;&lt;span&gt; {
&lt;&#x2F;span&gt;&lt;span&gt;      ipAddress = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;127.0.0.1&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    , thePort = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8080
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;What if we wanted to change what&#x27;s inside &lt;code&gt;count&lt;&#x2F;code&gt;? That would be nice, wouldn&#x27;t it.&lt;&#x2F;p&gt;
&lt;p&gt;First, let&#x27;s make a &lt;code&gt;prism&lt;&#x2F;code&gt; for &lt;code&gt;count&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;countIntPrism &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Prism&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Either Error Int&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Int
&lt;&#x2F;span&gt;&lt;span&gt;countIntPrism = prism&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span&gt; (\e -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;case&lt;&#x2F;span&gt;&lt;span&gt; e &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;of
&lt;&#x2F;span&gt;&lt;span&gt;                               &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right&lt;&#x2F;span&gt;&lt;span&gt; b -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; b
&lt;&#x2F;span&gt;&lt;span&gt;                               _       -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;...and a &lt;code&gt;lens&lt;&#x2F;code&gt; for getting a &lt;code&gt;count&lt;&#x2F;code&gt; out of the main &lt;code&gt;appConfig&lt;&#x2F;code&gt;...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;countLens &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Lens&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;AppConfig&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Either String Int&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;countLens = lens count (\app newVal -&amp;gt; app { count = newVal } )
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;...and then we can compose them together to make a new thing.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;fullCountInt &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Traversal&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;AppConfig Int
&lt;&#x2F;span&gt;&lt;span&gt;fullCountInt = countLens . countIntPrism
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Let&#x27;s use it to grab that &lt;code&gt;count&lt;&#x2F;code&gt; value...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;initialCount &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Maybe Int
&lt;&#x2F;span&gt;&lt;span&gt;initialCount = preview fullCountInt appData
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- initialCount == Just 100
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Great job!&lt;&#x2F;p&gt;
&lt;p&gt;And we can use it to change things too! With &lt;code&gt;set&lt;&#x2F;code&gt;!&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;changedCount &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;appConfig
&lt;&#x2F;span&gt;&lt;span&gt;changedCount = set fullCountInt &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1000&lt;&#x2F;span&gt;&lt;span&gt; appData
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- changedCount = AppConfig
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--  { value = Right 1000
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--  , title = &amp;quot;Hello!!!&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--  , dbConfig = DbConfig
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--    { ipAddress = &amp;quot;127.0.0.1&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--    , thePort = 8080
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--    }
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--  }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And &lt;code&gt;over&lt;&#x2F;code&gt;!&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;incrementedCount &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;appConfig
&lt;&#x2F;span&gt;&lt;span&gt;incrementedCount = set fullCountInt (+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) appData
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- incrementedCount = AppConfig
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--  { value = Right 101
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--  , title = &amp;quot;Hello!!!&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--  , dbConfig = DbConfig
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--    { ipAddress = &amp;quot;127.0.0.1&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--    , thePort = 8080
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--    }
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--  }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;All our friends are here!&lt;&#x2F;p&gt;
&lt;p&gt;You might notice the new type &lt;code&gt;Traversal&lt;&#x2F;code&gt; that has been produced by combining a &lt;code&gt;Lens&lt;&#x2F;code&gt; and a &lt;code&gt;Prism&lt;&#x2F;code&gt;. That&#x27;s an interesting thing in itself, but we&#x27;ll come to that another time.&lt;&#x2F;p&gt;
&lt;p&gt;That&#x27;s quite enough.&lt;&#x2F;p&gt;
&lt;p&gt;Make sense? If not, why not &lt;a href=&quot;&#x2F;contact.html&quot;&gt;get in touch&lt;&#x2F;a&gt;?&lt;&#x2F;p&gt;
&lt;p&gt;Further reading:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;lens-by-example.chrispenner.ca&#x2F;articles&#x2F;prisms&#x2F;overview&quot;&gt;Lens by example - Prisms&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Typeclasses - Foldable</title>
          <pubDate>Mon, 03 Dec 2018 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/typeclasses-foldable/</link>
          <guid>https://danieljharvey.github.io/typeclasses-foldable/</guid>
          <description xml:base="https://danieljharvey.github.io/typeclasses-foldable/">&lt;p&gt;Let&#x27;s think about sets of things that we want to make into one thing.&lt;&#x2F;p&gt;
&lt;p&gt;A classic example might be a list of numbers that we want to add up.&lt;&#x2F;p&gt;
&lt;p&gt;In Javascript we might do something like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;added &lt;&#x2F;span&gt;&lt;span&gt;= [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;].&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;reduce&lt;&#x2F;span&gt;&lt;span&gt;((&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;total&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;item&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;total &lt;&#x2F;span&gt;&lt;span&gt;+ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;item&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; added == 10
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Or perhaps we could get the maximum of the same list.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;maxNo &lt;&#x2F;span&gt;&lt;span&gt;= [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;].&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;reduce&lt;&#x2F;span&gt;&lt;span&gt;((&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;highest&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;item&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;highest &lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;item &lt;&#x2F;span&gt;&lt;span&gt;? &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;highest &lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;item&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; maxNo == 4
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;definition&quot;&gt;Definition&lt;&#x2F;h3&gt;
&lt;p&gt;So in Haskell we have the very similar &lt;code&gt;foldr&lt;&#x2F;code&gt; with the following signature:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Prelude&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; :i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Foldable
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Foldable&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;t&lt;&#x2F;span&gt;&lt;span&gt; :: * -&amp;gt; *) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;foldMap &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Monoid &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;m &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;m&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;t a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;m
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;foldr &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;t a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b
&lt;&#x2F;span&gt;&lt;span&gt;  {-# &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MINIMAL&lt;&#x2F;span&gt;&lt;span&gt; foldMap | foldr #-}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;(there is actually loads more but these are the key ones)&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;foldr &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Foldable &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;t &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;t a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;foldMap &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Foldable &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;t&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Monoid &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;m&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;m&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;t a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;m
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Here are the above JS functions using &lt;code&gt;foldr&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;added &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int
&lt;&#x2F;span&gt;&lt;span&gt;added = foldr (\a b -&amp;gt; a + b) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- added = 10
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;maxNo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int
&lt;&#x2F;span&gt;&lt;span&gt;maxNo = foldr (\a b -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; a &amp;gt; b &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;then&lt;&#x2F;span&gt;&lt;span&gt; a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;else&lt;&#x2F;span&gt;&lt;span&gt; b) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- maxNo = 4
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Not hugely different from the Javascript equivalent really. If you squint you can see the combining function, the initial value, and the data itself.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;foldMap&lt;&#x2F;code&gt; works a little differently. Instead of taking a custom combining function and using that to combine the items together, it takes a &lt;code&gt;a -&amp;gt; m&lt;&#x2F;code&gt; function (where the &lt;code&gt;m&lt;&#x2F;code&gt; in question is any &lt;code&gt;Monoid&lt;&#x2F;code&gt; instance). It uses this to turn each item into a &lt;code&gt;Monoid&lt;&#x2F;code&gt;, and then uses the &lt;code&gt;&amp;lt;&amp;gt;&lt;&#x2F;code&gt; and &lt;code&gt;mempty&lt;&#x2F;code&gt; functions for that Monoid to combine the items.&lt;&#x2F;p&gt;
&lt;p&gt;Here&#x27;s a &lt;code&gt;newtype&lt;&#x2F;code&gt; I made earlier: &lt;code&gt;MySum&lt;&#x2F;code&gt;. It&#x27;s &lt;code&gt;Monoid&lt;&#x2F;code&gt; instance adds numbers together when combined.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;newtype &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MySum&lt;&#x2F;span&gt;&lt;span&gt; a = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MySum&lt;&#x2F;span&gt;&lt;span&gt; { getMySum :: a }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Semigroup instance
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Num &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; Semigroup&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MySum &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MySum&lt;&#x2F;span&gt;&lt;span&gt; a &amp;lt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MySum&lt;&#x2F;span&gt;&lt;span&gt; b = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MySum&lt;&#x2F;span&gt;&lt;span&gt; (a + b)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- Monoid instance
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Num &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; Monoid&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MySum &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;    mempty = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MySum 0
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now we can use &lt;code&gt;foldMap&lt;&#x2F;code&gt; with the &lt;code&gt;MySum&lt;&#x2F;code&gt; constructor to add up a list of numbers.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;addTwo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: MySum Int
&lt;&#x2F;span&gt;&lt;span&gt;addTwo = foldMap &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MySum&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- addTwo = MySum 10
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Great stuff! Now our answer is still wrapped up in a &lt;code&gt;MySum&lt;&#x2F;code&gt;, but it&#x27;s easy enough to take it out.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;addTwoUnwrapped &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: MySum Int
&lt;&#x2F;span&gt;&lt;span&gt;addTwoUnwrapped = getMySum $ foldMap &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MySum&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- addTwoUnwrapped = 10
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Excellent!&lt;&#x2F;p&gt;
&lt;p&gt;This seems laborious, but actually &lt;code&gt;MySum&lt;&#x2F;code&gt; isn&#x27;t my invention, I&#x27;ve just stolen a thing called &lt;code&gt;Sum&lt;&#x2F;code&gt; that comes with the Haskell Prelude. Therefore we can just do &lt;code&gt;getSum $ foldMap Sum [1,2,3,4,5,6]&lt;&#x2F;code&gt; to Monoidally combine the list items.&lt;&#x2F;p&gt;
&lt;p&gt;It also provides a similar invention for multiplying numbers called &lt;code&gt;Product&lt;&#x2F;code&gt; that works like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;twentyFour &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int
&lt;&#x2F;span&gt;&lt;span&gt;twentyFour = getProduct $ foldMap &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Product&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- twentyFour == 24
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Folding can also capture logic, here we are using &lt;code&gt;foldMap&lt;&#x2F;code&gt; to check all of a list is true.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;newtype &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MyAll &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MyAll&lt;&#x2F;span&gt;&lt;span&gt; { getMyAll :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Bool&lt;&#x2F;span&gt;&lt;span&gt; }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Semigroup MyAll where
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MyAll&lt;&#x2F;span&gt;&lt;span&gt; a &amp;lt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MyAll&lt;&#x2F;span&gt;&lt;span&gt; b = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MyAll&lt;&#x2F;span&gt;&lt;span&gt; (a &amp;amp;&amp;amp; b)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Monoid MyAll where
&lt;&#x2F;span&gt;&lt;span&gt;    mempty = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MyAll True
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;allOfThem &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Bool
&lt;&#x2F;span&gt;&lt;span&gt;allOfThem = getMyAll $ foldMap &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MyAll&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- allOfThem == True
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;notAll &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Bool
&lt;&#x2F;span&gt;&lt;span&gt;notAll = getMyAll $ foldMap &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;MyAll&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;False&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- notAll == False
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;(I have also stolen &lt;code&gt;MyAll&lt;&#x2F;code&gt;, it is usually called just &lt;code&gt;All&lt;&#x2F;code&gt;. You can see the pattern here.)&lt;&#x2F;p&gt;
&lt;p&gt;We could also very easily make a &lt;code&gt;MyAny&lt;&#x2F;code&gt; type which uses or (ie, &lt;code&gt;||&lt;&#x2F;code&gt;) which we could use to return a &lt;code&gt;True&lt;&#x2F;code&gt; whenever a single one of a collection of &lt;code&gt;Bool&lt;&#x2F;code&gt;s happens to be &lt;code&gt;True&lt;&#x2F;code&gt;. You might want to have a think about what the &lt;code&gt;mempty&lt;&#x2F;code&gt; value would be for that to work though. That&#x27;s up to you.&lt;&#x2F;p&gt;
&lt;p&gt;Anyhow. I&#x27;m bored of typing now so I guess this is it for this one.&lt;&#x2F;p&gt;
&lt;p&gt;Make sense? If not, why not &lt;a href=&quot;&#x2F;contact.html&quot;&gt;get in touch&lt;&#x2F;a&gt;?&lt;&#x2F;p&gt;
&lt;p&gt;Further reading:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;http:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;base-4.12.0.0&#x2F;docs&#x2F;Data-Foldable.html&quot;&gt;Data.Foldable&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Typeclasses - Applicative</title>
          <pubDate>Sat, 17 Nov 2018 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/typeclasses-applicative/</link>
          <guid>https://danieljharvey.github.io/typeclasses-applicative/</guid>
          <description xml:base="https://danieljharvey.github.io/typeclasses-applicative/">&lt;p&gt;In my learning of Haskell and all the associated mathematical stuff attached to it, one of the tougher conceptual cookies to crack has been the Applicative Functor.&lt;&#x2F;p&gt;
&lt;p&gt;Coming from a background of fairly functional Javascript, functor is a very easy sell - it&#x27;s &lt;code&gt;array.map&lt;&#x2F;code&gt;. Easy. If there&#x27;s a thing in the box, do something to the thing, if there&#x27;s nothing in the box, don&#x27;t freak out about the lack of things or explode. Great!&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;increase &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int -&amp;gt; Int
&lt;&#x2F;span&gt;&lt;span&gt;increase i = i + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- fmap increase [1,2,3] == [2,3,4]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;These functions can even return lists of their own...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;moreList &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int -&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;moreList a = [a -&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, a, a + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;listOfLists &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;]]
&lt;&#x2F;span&gt;&lt;span&gt;listOfLists = fmap moreList [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- listOfLists = [[0,1,2], [1,2,3], [2,3,4]]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Trying to understand the actual point of an Applicative Functor, especially to my Javascript-addled mind, was much trickier. The first explanations I heard were something like:&lt;&#x2F;p&gt;
&lt;h4 id=&quot;oh-applicative-it-s-for-taking-a-function-that-in-a-box-and-a-value-in-a-box-and-then-combining-them-to-make-a-new-value-which-is-also-in-a-box&quot;&gt;&quot;Oh, applicative? It&#x27;s for taking a function that in a box, and a value in a box, and then combining them to make a new value which is also in a box&quot;&lt;&#x2F;h4&gt;
&lt;p&gt;Now I am all up for putting values in boxes. Bloody love it. But functions? What are you playing at? Ridiculous talk.&lt;&#x2F;p&gt;
&lt;p&gt;Could it help us with our list changing exercise? Sort of...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;minusOne &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int -&amp;gt; Int
&lt;&#x2F;span&gt;&lt;span&gt;minusOne i = i - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;doNothing &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int -&amp;gt; Int
&lt;&#x2F;span&gt;&lt;span&gt;doNothing = id
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;plusOne &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int -&amp;gt; Int
&lt;&#x2F;span&gt;&lt;span&gt;plusOne i = i + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;applicativeList &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;applicativeList = [minusOne, doNothing, plusOne] &amp;lt;*&amp;gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- applicativeList == [0,1,2,1,2,3,2,3,4]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;OK, so instead of a list of lists, we have one list (and one new terrifyingly meaningless infix operator, &lt;code&gt;&amp;lt;*&amp;gt;&lt;&#x2F;code&gt;, wonderful). Seems like a lot more work too, and we could probably just have used &lt;code&gt;concat&lt;&#x2F;code&gt; on the result and saved ourselves a whole load of intellectual soup. Sure. I get it. Terrible example. I&#x27;ve let us both down. I&#x27;m sorry.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s see if we can find a slightly more helpful example.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;CalcFace&lt;&#x2F;span&gt;&lt;span&gt; a = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;CalcFace&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;] a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Eq&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Show&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This data type is for describing calculations. The &lt;code&gt;a&lt;&#x2F;code&gt; contains the current total, and the &lt;code&gt;[String]&lt;&#x2F;code&gt; is used to describe what is happening. We&#x27;ve deriving standard &lt;code&gt;Eq&lt;&#x2F;code&gt; and &lt;code&gt;Show&lt;&#x2F;code&gt; instances for it, plus making our own fairly basic &lt;code&gt;Functor&lt;&#x2F;code&gt; instance that, as you might expected, maps the function &lt;code&gt;f&lt;&#x2F;code&gt; over any &lt;code&gt;a&lt;&#x2F;code&gt; inside the datatype and leaves everything else alone like a good functor should.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Functor CalcFace where
&lt;&#x2F;span&gt;&lt;span&gt;   fmap f (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;CalcFace&lt;&#x2F;span&gt;&lt;span&gt; names a) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;CalcFace&lt;&#x2F;span&gt;&lt;span&gt; names $ f a
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We also have this function, which we can use to print out the answer with our working.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;showCalculation &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Show &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; CalcFace &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; String
&lt;&#x2F;span&gt;&lt;span&gt;showCalculation (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;CalcFace&lt;&#x2F;span&gt;&lt;span&gt; names a) =
&lt;&#x2F;span&gt;&lt;span&gt;        concat parts ++ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt; equals &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++ show a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;        parts = intersperse &amp;quot; &amp;quot; names
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Let&#x27;s try it out.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;one &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: CalcFace Int
&lt;&#x2F;span&gt;&lt;span&gt;one = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;CalcFace&lt;&#x2F;span&gt;&lt;span&gt; [&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;showCalculation one
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- == &amp;quot;1 equals 1&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Seems very reasonable.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s try some mapping.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;two &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: CalcFace Int
&lt;&#x2F;span&gt;&lt;span&gt;two = fmap (+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) one
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;showCalculation two
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- == &amp;quot;1 equals 2&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Not so good. The answer is correct as we ran a &lt;code&gt;+1&lt;&#x2F;code&gt; function over the &lt;code&gt;1&lt;&#x2F;code&gt; to get &lt;code&gt;2&lt;&#x2F;code&gt;, however we were unable to add anything to our log and thus the string output makes no sense. Perhaps &lt;code&gt;Functor&lt;&#x2F;code&gt; is not powerful enough for our task.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s give Applicative a go. Here&#x27;s one of those functions in a box we were talking about. This time we can also include a description of what the function does.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;addOne &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: CalcFace&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Int -&amp;gt; Int&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;addOne = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;CalcFace&lt;&#x2F;span&gt;&lt;span&gt; [&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;add 1&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;] (+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We&#x27;ll need an &lt;code&gt;Applicative&lt;&#x2F;code&gt; instance for our datatype too. What does that look like?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Prelude&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; :i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Applicative
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Functor &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span&gt; =&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Applicative&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span&gt; :: * -&amp;gt; *) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;pure &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;f a
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;(&amp;lt;*&amp;gt;) &lt;&#x2F;span&gt;&lt;span&gt;:: f (a -&amp;gt; b) -&amp;gt; f a -&amp;gt; f b
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;GHC&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Base&lt;&#x2F;span&gt;&lt;span&gt;.liftA2 :: (a -&amp;gt; b -&amp;gt; c) -&amp;gt; f a -&amp;gt; f b -&amp;gt; f c
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;(*&amp;gt;) &lt;&#x2F;span&gt;&lt;span&gt;:: f a -&amp;gt; f b -&amp;gt; f b
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;(&amp;lt;*) &lt;&#x2F;span&gt;&lt;span&gt;:: f a -&amp;gt; f b -&amp;gt; f a
&lt;&#x2F;span&gt;&lt;span&gt;  {-# &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MINIMAL&lt;&#x2F;span&gt;&lt;span&gt; pure, ((&amp;lt;*&amp;gt;) | liftA2) #-}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;There&#x27;s some pretty interesting jumping off points in there (such as the whole concept of &lt;code&gt;lifting&lt;&#x2F;code&gt;, but we&#x27;ll come back to that), but fortunately we only need to implement two functions for this typeclass.&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;pure&lt;&#x2F;code&gt; - which takes an &lt;code&gt;a&lt;&#x2F;code&gt; value and returns a sort of default value for that datatype. This is used internally in constructing new values.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;&amp;lt;*&amp;gt;&lt;&#x2F;code&gt; (called &lt;code&gt;apply&lt;&#x2F;code&gt; IRL) - this takes one wrapped function and one wrapped value and combines them.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Confused? Me too! Let&#x27;s give it a pop.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Applicative CalcFace where
&lt;&#x2F;span&gt;&lt;span&gt;    pure a = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;CalcFace []&lt;&#x2F;span&gt;&lt;span&gt; a
&lt;&#x2F;span&gt;&lt;span&gt;    (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;CalcFace&lt;&#x2F;span&gt;&lt;span&gt; operation f) &amp;lt;*&amp;gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;CalcFace&lt;&#x2F;span&gt;&lt;span&gt; value a) =
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;CalcFace&lt;&#x2F;span&gt;&lt;span&gt; newNames (f a) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;            newNames = value &amp;lt;&amp;gt; operation
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;OK. So &lt;code&gt;pure&lt;&#x2F;code&gt; is reasonably straightforward - it just makes the most basic &lt;code&gt;CalcFace&lt;&#x2F;code&gt; imaginable and puts whatever &lt;code&gt;a&lt;&#x2F;code&gt; we pass inside it. The other function is a bit trickier though. Basically we are taking the function &lt;code&gt;f&lt;&#x2F;code&gt; and applying to &lt;code&gt;a&lt;&#x2F;code&gt;, but also taking the &lt;code&gt;[String]&lt;&#x2F;code&gt; called &lt;code&gt;operation&lt;&#x2F;code&gt; and plopping it on the end of the &lt;code&gt;[String]&lt;&#x2F;code&gt; named &lt;code&gt;value&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Seems weird? Sure. Let&#x27;s see it in action though.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;oneAddOne &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: CalcFace Int
&lt;&#x2F;span&gt;&lt;span&gt;oneAddOne = addOne &amp;lt;*&amp;gt; one
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- showCalculation oneAddOne == &amp;quot;1 add 1 equals 2&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Nice!&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;oneAddOneAddOne &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: CalcFace Int
&lt;&#x2F;span&gt;&lt;span&gt;oneAddOneAddOne = addOne &amp;lt;*&amp;gt; oneAddOne
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- showCalculation oneAddOneAddOne == &amp;quot;1 add 1 add 1 equals 3&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Great stuff. The key here is that not only have we acted upon the values, the way that &lt;code&gt;functor&lt;&#x2F;code&gt; did, but we have also done something to the structure of the datatype too (in this case, adding to the list of operations). This seems trivial when the operators are just basic mathematics, but if the functions we are running are multiple database operations and calls to other servers, then you&#x27;ve got yourself the start of tidy little pure functional logging solution there, son.&lt;&#x2F;p&gt;
&lt;p&gt;Anyway. This barely scratches the surface, but hopefully gives you the start of an intuition for these things. Applicative does lots of things. Go find them!&lt;&#x2F;p&gt;
&lt;p&gt;Make sense? If not, why not &lt;a href=&quot;&#x2F;contact.html&quot;&gt;get in touch&lt;&#x2F;a&gt;?&lt;&#x2F;p&gt;
&lt;p&gt;Further reading:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;http:&#x2F;&#x2F;adit.io&#x2F;posts&#x2F;2013-04-17-functors,_applicatives,_and_monads_in_pictures.html&quot;&gt;Functors, Applicative and Monads in Pictures&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Typeclasses - Contravariant</title>
          <pubDate>Sat, 17 Nov 2018 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/typeclasses-contravariant/</link>
          <guid>https://danieljharvey.github.io/typeclasses-contravariant/</guid>
          <description xml:base="https://danieljharvey.github.io/typeclasses-contravariant/">&lt;p&gt;Let&#x27;s think about Predicates.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;newtype &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Preddy&lt;&#x2F;span&gt;&lt;span&gt; a = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Preddy&lt;&#x2F;span&gt;&lt;span&gt; { getPreddy :: a -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Bool&lt;&#x2F;span&gt;&lt;span&gt; }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;What the hell is this?&lt;&#x2F;p&gt;
&lt;p&gt;OK. So it&#x27;s a &lt;code&gt;newtype&lt;&#x2F;code&gt;. What is basically does is let&#x27;s us take some sort of value (here, any function from &lt;code&gt;a -&amp;gt; Bool&lt;&#x2F;code&gt;) and make a new type out of it so we can give it typeclasses and do magic shit to it.&lt;&#x2F;p&gt;
&lt;p&gt;So let&#x27;s say we have a basic function that takes a number and let&#x27;s us know whether that number is over 3.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;threePred &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int -&amp;gt; Bool
&lt;&#x2F;span&gt;&lt;span&gt;threePred i = i &amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;3
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Great!&lt;&#x2F;p&gt;
&lt;p&gt;To put this into a &lt;code&gt;newtype&lt;&#x2F;code&gt; (our &lt;code&gt;Preddy&lt;&#x2F;code&gt; one, to be exact) - we do this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;overThree &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Preddy Int
&lt;&#x2F;span&gt;&lt;span&gt;overThree = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Preddy&lt;&#x2F;span&gt;&lt;span&gt; threePred
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The type has changed - the &lt;code&gt;a&lt;&#x2F;code&gt; in &lt;code&gt;a -&amp;gt; Bool&lt;&#x2F;code&gt; is &lt;code&gt;Int&lt;&#x2F;code&gt; so we get &lt;code&gt;Preddy Int&lt;&#x2F;code&gt;. We don&#x27;t need to specify the &lt;code&gt;Bool&lt;&#x2F;code&gt; anywhere because that&#x27;s sort of implicit in the &lt;code&gt;Preddy&lt;&#x2F;code&gt;-ness.&lt;&#x2F;p&gt;
&lt;p&gt;OK. I get it, you&#x27;re worried. My function is gone for ever. Can I get it out again? Sure.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;isOverThree &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int -&amp;gt; Bool
&lt;&#x2F;span&gt;&lt;span&gt;isOverThree = getPreddy overThree
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;threePred&lt;&#x2F;code&gt; and &lt;code&gt;isOverThree&lt;&#x2F;code&gt; are completely the same thing. So now we&#x27;re comfortable this &lt;code&gt;newtype&lt;&#x2F;code&gt; thing is basically doing nothing awful to our code, what it is good for?&lt;&#x2F;p&gt;
&lt;p&gt;Oh yeah. We said earlier. Because &lt;code&gt;Preddy&lt;&#x2F;code&gt; is a datatype of it&#x27;s own we can make typeclass instances for it. Let&#x27;s make a &lt;code&gt;Contravariant&lt;&#x2F;code&gt; instance, for no other reason than this being the name of the post and we&#x27;re a long way into it without doing anything particularly useful.&lt;&#x2F;p&gt;
&lt;p&gt;What will we need for doing that then?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Prelude&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; :i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Contravariant
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If are typing along with all this and get an error, please skip to the bottom of the page for some useful links, however if you have the &lt;code&gt;contravariant&lt;&#x2F;code&gt; library floating around or are just happy to take my word for it then GREAT! You&#x27;ll see this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Contravariant&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span&gt; :: * -&amp;gt; *) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;contramap &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;f b &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;f a
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;(&amp;gt;$) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;f b &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;f a
&lt;&#x2F;span&gt;&lt;span&gt;  {-# &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MINIMAL&lt;&#x2F;span&gt;&lt;span&gt; contramap #-}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The key thing to look when starting out is this &lt;code&gt;MINIMAL&lt;&#x2F;code&gt; part - as it means the only functions we need to worry about for the time being.&lt;&#x2F;p&gt;
&lt;p&gt;So, &lt;code&gt;contramap&lt;&#x2F;code&gt; is it. What does this do?&lt;&#x2F;p&gt;
&lt;p&gt;It takes a function from &lt;code&gt;(a -&amp;gt; b)&lt;&#x2F;code&gt; and then it takes an &lt;code&gt;f b&lt;&#x2F;code&gt; and returns an &lt;code&gt;f a&lt;&#x2F;code&gt;. Sure.&lt;&#x2F;p&gt;
&lt;p&gt;What?&lt;&#x2F;p&gt;
&lt;p&gt;Are you out of your fucking mind? How do I turn &lt;code&gt;b&lt;&#x2F;code&gt; into &lt;code&gt;a&lt;&#x2F;code&gt; using &lt;code&gt;(a -&amp;gt; b)&lt;&#x2F;code&gt;?&lt;&#x2F;p&gt;
&lt;p&gt;Is this...a backwards functor? Can I implement undo on all my functions? Is the special magic Haskell sauce I&#x27;ve been waiting for?&lt;&#x2F;p&gt;
&lt;p&gt;So it turns out no.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;nameLength &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: String -&amp;gt; Int
&lt;&#x2F;span&gt;&lt;span&gt;nameLength &amp;quot;&amp;quot;      = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span&gt;nameLength (x: xs) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1 &lt;&#x2F;span&gt;&lt;span&gt;+ nameLength xs
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So a regular &lt;code&gt;functor&lt;&#x2F;code&gt; is actually a &lt;code&gt;covariant functor&lt;&#x2F;code&gt; which I guess means forwards functor. When we map over it, we changing what happens after it. Hence if I have a &lt;code&gt;Maybe String&lt;&#x2F;code&gt; and &lt;code&gt;fmap&lt;&#x2F;code&gt; a &lt;code&gt;String -&amp;gt; Int&lt;&#x2F;code&gt; function like &lt;code&gt;nameLength&lt;&#x2F;code&gt; over it I get a &lt;code&gt;Maybe Int&lt;&#x2F;code&gt; and all is well in the world.&lt;&#x2F;p&gt;
&lt;p&gt;However our pal &lt;code&gt;Preddy&lt;&#x2F;code&gt;, unbeknownst to him, is about to become a &lt;code&gt;contravariant functor&lt;&#x2F;code&gt; which sort of means backwards. What this means is if he&#x27;s waiting for an &lt;code&gt;Int&lt;&#x2F;code&gt; to see if it&#x27;s over 3, we can &lt;code&gt;contramap&lt;&#x2F;code&gt; that same &lt;code&gt;String -&amp;gt; Int&lt;&#x2F;code&gt; function &lt;code&gt;nameLength&lt;&#x2F;code&gt; over it, and instead &lt;code&gt;Preddy&lt;&#x2F;code&gt; is waiting for a &lt;code&gt;String&lt;&#x2F;code&gt; so he can tell you whether the &lt;code&gt;String&lt;&#x2F;code&gt; has over 3 characters. The map happens beforehand, basically. That&#x27;s how an &lt;code&gt;(a -&amp;gt; b)&lt;&#x2F;code&gt; function turns a &lt;code&gt;Preddy b&lt;&#x2F;code&gt; (predicate waiting for a &lt;code&gt;b&lt;&#x2F;code&gt;) into a &lt;code&gt;Preddy a&lt;&#x2F;code&gt; (predicate waiting for an &lt;code&gt;a&lt;&#x2F;code&gt;).&lt;&#x2F;p&gt;
&lt;p&gt;Seems weird? Sure. Let&#x27;s look at an example.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Contravariant Preddy where
&lt;&#x2F;span&gt;&lt;span&gt;  contramap f (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Preddy&lt;&#x2F;span&gt;&lt;span&gt; p) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Preddy&lt;&#x2F;span&gt;&lt;span&gt; (p . f)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This looks a lot like &lt;code&gt;fmap&lt;&#x2F;code&gt;, except our new &lt;code&gt;f&lt;&#x2F;code&gt; function happens BEFORE the &lt;code&gt;p&lt;&#x2F;code&gt; function that is already in there.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;nameLengthOverThree &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Preddy String
&lt;&#x2F;span&gt;&lt;span&gt;nameLengthOverThree = contramap nameLength overThree
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Here we&#x27;ve turned our &lt;code&gt;Preddy Int&lt;&#x2F;code&gt; function &lt;code&gt;overThree&lt;&#x2F;code&gt; into a &lt;code&gt;Preddy String&lt;&#x2F;code&gt; function using a &lt;code&gt;String -&amp;gt; Int&lt;&#x2F;code&gt; function and &lt;code&gt;contramap&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s use it!&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;nameIsOverThree &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: String -&amp;gt; Bool
&lt;&#x2F;span&gt;&lt;span&gt;nameIsOverThree = getPreddy nameLengthOverThree
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- nameIsOverThree &amp;quot;Lou&amp;quot; == False
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- nameIsOverThree &amp;quot;Doug&amp;quot; == True
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Great! Now we can use it to see if words are Just Too Long.&lt;&#x2F;p&gt;
&lt;p&gt;All this wrapping and unwrapping seems a lot of work for that, but what if we start using &lt;code&gt;contramap&lt;&#x2F;code&gt; for more?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Person &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Person&lt;&#x2F;span&gt;&lt;span&gt; { name :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;, age :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Int&lt;&#x2F;span&gt;&lt;span&gt; } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Show&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;steve &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Person
&lt;&#x2F;span&gt;&lt;span&gt;steve = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Person&lt;&#x2F;span&gt;&lt;span&gt; { name = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Steve&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, age = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;100&lt;&#x2F;span&gt;&lt;span&gt; }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;lou &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Person
&lt;&#x2F;span&gt;&lt;span&gt;lou = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Person&lt;&#x2F;span&gt;&lt;span&gt; { name = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Lou&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, age = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;69&lt;&#x2F;span&gt;&lt;span&gt; }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Here are Steve and Lou. They are, at least syntactically, people.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s use mathematics to judge whether their names are too long.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;personTooLong &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Person -&amp;gt; Bool
&lt;&#x2F;span&gt;&lt;span&gt;personTooLong = getPreddy personPreddy &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  personPreddy = contramap (nameLength . name) overThree
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- personTooLong steve == True
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- personTooLong lou == False
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Excellent! That will show them.&lt;&#x2F;p&gt;
&lt;p&gt;You will note that here we&#x27;ve somewhat rushed and combined the &lt;code&gt;contramap&lt;&#x2F;code&gt;-ing and unwrapping into one function. This was mostly to show that when you come to use these things, it doesn&#x27;t need to be quite as laborious as our broken down examples above. Hopefully you can follow, the key thing is that &lt;code&gt;(nameLength . name)&lt;&#x2F;code&gt; means &quot;put the value into &lt;code&gt;name&lt;&#x2F;code&gt; and the pass the result to &lt;code&gt;nameLength&lt;&#x2F;code&gt;&quot;.&lt;&#x2F;p&gt;
&lt;p&gt;Contravariant doesn&#x27;t show up a huge amount on it&#x27;s own, but it comes into it&#x27;s own as part of &lt;code&gt;Profunctor&lt;&#x2F;code&gt; (lightning flashes, thunder, excitement!) which no doubt we&#x27;ll flop towards at some unspecified future moment.&lt;&#x2F;p&gt;
&lt;p&gt;Make sense? If not, why not &lt;a href=&quot;&#x2F;contact.html&quot;&gt;get in touch&lt;&#x2F;a&gt;?&lt;&#x2F;p&gt;
&lt;p&gt;Further reading:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=JZPXzJ5tp9w&quot;&gt;The Extended Functor Family (video)&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;http:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;contravariant-1.5&#x2F;docs&#x2F;Data-Functor-Contravariant.html&quot;&gt;Data.Functor.Contravariant&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;NOTE: If you happened to get this error:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;&amp;lt;interactive&amp;gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;: error: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Not &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;in&lt;&#x2F;span&gt;&lt;span&gt; scope: ‘&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Contravariant&lt;&#x2F;span&gt;&lt;span&gt;’
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;...then you will need to install the &lt;code&gt;contravariant&lt;&#x2F;code&gt; package from Hackage. I&#x27;m not going to go into Haskell package management at this point but here is a good starting point: &lt;a href=&quot;http:&#x2F;&#x2F;seanhess.github.io&#x2F;2015&#x2F;08&#x2F;04&#x2F;practical-haskell-getting-started.html&quot;&gt;Practical Haskell - Getting Started&lt;&#x2F;a&gt;. It&#x27;s all going to be fine, I promise. The documentation isn&#x27;t great but the tools are pretty nice.&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Typeclasses - Functor</title>
          <pubDate>Fri, 16 Nov 2018 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/typeclasses-functor/</link>
          <guid>https://danieljharvey.github.io/typeclasses-functor/</guid>
          <description xml:base="https://danieljharvey.github.io/typeclasses-functor/">&lt;p&gt;Let&#x27;s think about things that might or might not happen.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Perhaps&lt;&#x2F;span&gt;&lt;span&gt; a = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Yeah&lt;&#x2F;span&gt;&lt;span&gt; a | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nah
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Perhaps is a datatype that we can also use as a container for other data (by holding it inside a nice &lt;code&gt;Yeah&lt;&#x2F;code&gt;), or to show that we have no data with &lt;code&gt;Nah&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s put things in it.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;john &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Perhaps String
&lt;&#x2F;span&gt;&lt;span&gt;john = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Yeah &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;John&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Great job. John is having a nice time there. How might we express an absence of John?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;nope &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Perhaps String
&lt;&#x2F;span&gt;&lt;span&gt;nope = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nah
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;OK. No Johns here.&lt;&#x2F;p&gt;
&lt;p&gt;So what if we have a function that receives something wrapped in a &lt;code&gt;Perhaps&lt;&#x2F;code&gt; and wants to do stuff with it.&lt;&#x2F;p&gt;
&lt;p&gt;I guess we have pattern matching, so we can use this to pull stuff out and do things with it. Let&#x27;s add some question marks to this name, because that is always a valuable thing to do.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;questionAdd &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Perhaps String -&amp;gt; Perhaps String
&lt;&#x2F;span&gt;&lt;span&gt;questionAdd &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nah         &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nah
&lt;&#x2F;span&gt;&lt;span&gt;questionAdd (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Yeah&lt;&#x2F;span&gt;&lt;span&gt; name) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Yeah&lt;&#x2F;span&gt;&lt;span&gt; (name ++ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;???&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- questionAdd Nah  = Nah
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- questionAdd john = Yeah &amp;quot;John???&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Ok. Seems good. What about adding a simple exclaimation point instead? (Remembering of course that anybody more than one at any point is a sure sign of insanity.)&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;exclaimAdd &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Perhaps String -&amp;gt; Perhaps String
&lt;&#x2F;span&gt;&lt;span&gt;exclaimAdd &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nah         &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nah
&lt;&#x2F;span&gt;&lt;span&gt;exclaimAdd (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Yeah&lt;&#x2F;span&gt;&lt;span&gt; name) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Yeah&lt;&#x2F;span&gt;&lt;span&gt; (name ++ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- exclaimAdd Nah  = Nah
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- exclaimAdd john = Yeah &amp;quot;John!&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Ok. Sure. So far, so functional. Feels like we&#x27;re repeating ourselves though. What if we could abstract out away the unwrapping-and-then-wrapping-again and the function itself? Well sure we can! That typeclass is called &lt;code&gt;Functor&lt;&#x2F;code&gt;. Let&#x27;s open ghci and take a look.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Prelude&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; :i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Functor
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Here&#x27;s the definition:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Functor&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span&gt; :: * -&amp;gt; *) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;fmap &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;f a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;f b
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;(&amp;lt;$) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;f b &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;f a
&lt;&#x2F;span&gt;&lt;span&gt;  {-# &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MINIMAL&lt;&#x2F;span&gt;&lt;span&gt; fmap #-}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Looks like we can implement it by just using &lt;code&gt;fmap&lt;&#x2F;code&gt;. And that type signature kind of looks like what we want (if we squint very hard indeed). It&#x27;s saying take a function of any &lt;code&gt;a&lt;&#x2F;code&gt; to any &lt;code&gt;b&lt;&#x2F;code&gt; (ie, &lt;code&gt;(a -&amp;gt; b)&lt;&#x2F;code&gt;) and then pass me a functor with an &lt;code&gt;a&lt;&#x2F;code&gt; in it (ie, &lt;code&gt;f a&lt;&#x2F;code&gt;) and I will return the same functor but with the &lt;code&gt;b&lt;&#x2F;code&gt; from your function in it (ie, &lt;code&gt;f b&lt;&#x2F;code&gt;). So basically &lt;code&gt;fmap&lt;&#x2F;code&gt; is the &quot;unwrapping-and-then-wrapping-again&quot; we talked about earlier. Sounds good. Let&#x27;s implement it.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Functor Perhaps where
&lt;&#x2F;span&gt;&lt;span&gt;  fmap _ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nah      &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nah
&lt;&#x2F;span&gt;&lt;span&gt;  fmap f (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Yeah&lt;&#x2F;span&gt;&lt;span&gt; a) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Yeah&lt;&#x2F;span&gt;&lt;span&gt; (f a)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Looks great. The first thing you&#x27;ll notice is that the &lt;code&gt;fmap&lt;&#x2F;code&gt; for &lt;code&gt;Nah&lt;&#x2F;code&gt; doesn&#x27;t do anything. That&#x27;s because although our &lt;code&gt;Yeah&lt;&#x2F;code&gt; can contains some &lt;code&gt;a&lt;&#x2F;code&gt;, our &lt;code&gt;Nah&lt;&#x2F;code&gt; contains nothing at all so cares very little for our &lt;code&gt;a -&amp;gt; b&lt;&#x2F;code&gt; function. However, look! The second line is unwrapping the &lt;code&gt;a&lt;&#x2F;code&gt; from the &lt;code&gt;Yeah&lt;&#x2F;code&gt;, and then making a new &lt;code&gt;Yeah b&lt;&#x2F;code&gt; (because we make a &lt;code&gt;b&lt;&#x2F;code&gt; by running &lt;code&gt;f&lt;&#x2F;code&gt;, which is our &lt;code&gt;a -&amp;gt; b&lt;&#x2F;code&gt; function). Therefore we can throw any old function at this and we should have a Nice Time.&lt;&#x2F;p&gt;
&lt;p&gt;This function for instance, is used on a &lt;code&gt;String&lt;&#x2F;code&gt; to make the caller look somewhat unhinged.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;exclaim &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: String -&amp;gt; String
&lt;&#x2F;span&gt;&lt;span&gt;exclaim str = str ++ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;!!!!!!!!!!!!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- exclaim &amp;quot;Horse&amp;quot; == &amp;quot;Horse!!!!!!!!!!!!&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;But using our new &lt;code&gt;Functor&lt;&#x2F;code&gt; instance means we can run it on our poor friend &lt;code&gt;john&lt;&#x2F;code&gt; from earlier, even though he is wrapped up in all that &lt;code&gt;Yeah&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;veryJohn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Perhaps String
&lt;&#x2F;span&gt;&lt;span&gt;veryJohn = fmap exclaim john
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- veryJohn == Yeah &amp;quot;John!!!!!!!!!!!!&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Sorry John.&lt;&#x2F;p&gt;
&lt;p&gt;What if we run the same function over a &lt;code&gt;Nah&lt;&#x2F;code&gt;? Does everything explode?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;stillNope &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Perhaps String
&lt;&#x2F;span&gt;&lt;span&gt;stillNope = fmap exclaim nope
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- stillNope = Nah
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Nah. Seems fine. &lt;code&gt;Nah&lt;&#x2F;code&gt; ignores the function altogether, as it has no &lt;code&gt;a&lt;&#x2F;code&gt; in it, so no interest.&lt;&#x2F;p&gt;
&lt;p&gt;Ok. So this seems pretty great. There is one thing to know about &lt;code&gt;Functor&lt;&#x2F;code&gt; however that can be a little bit brain bending at first, and that&#x27;s the idea that they can be &quot;lawful&quot;. That is to say, that when you &lt;code&gt;fmap&lt;&#x2F;code&gt; over something, it doesn&#x27;t also break that thing, and that it unwraps and wraps in the right way. Haskell&#x27;s type system can help you make sure your &lt;code&gt;fmap&lt;&#x2F;code&gt; has the right types, but it can&#x27;t enforce that your &lt;code&gt;Functor&lt;&#x2F;code&gt; makes sense I&#x27;m afraid. That&#x27;s up to you.&lt;&#x2F;p&gt;
&lt;p&gt;The first law is called Identity. It means that if you &lt;code&gt;fmap&lt;&#x2F;code&gt; using an Identity function, nothing will change. This is a trick I suppose - the Identity function looks like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;identity &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;identity a = a
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- this is also called id in the Prelude
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So running it on anything does nothing - the Identity law is basically checking the &lt;code&gt;Functor&lt;&#x2F;code&gt; is up to no funny business. Our Perhaps functor is OK, but what about this chancer?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Poohoops&lt;&#x2F;span&gt;&lt;span&gt; a = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Yerp&lt;&#x2F;span&gt;&lt;span&gt; a | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nerp &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Eq&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Show&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Functor Poohoops where
&lt;&#x2F;span&gt;&lt;span&gt;    fmap _ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nerp     &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nerp
&lt;&#x2F;span&gt;&lt;span&gt;    fmap f (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Yeah&lt;&#x2F;span&gt;&lt;span&gt; a) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nerp
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If we &lt;code&gt;fmap identity Nerp&lt;&#x2F;code&gt; we get &lt;code&gt;Nerp&lt;&#x2F;code&gt;, so that all seems fine. However, if we &lt;code&gt;fmap identity Yeah &quot;Detroit&quot;&lt;&#x2F;code&gt; then we also get &lt;code&gt;Nerp&lt;&#x2F;code&gt; which is an absolute bloody disaster. This functor is broken. Put it straight in the bin, and set the bin on fire.&lt;&#x2F;p&gt;
&lt;p&gt;The second law is called Composition. It means that if we &lt;code&gt;fmap&lt;&#x2F;code&gt; one function over our &lt;code&gt;Functor&lt;&#x2F;code&gt; and then &lt;code&gt;fmap&lt;&#x2F;code&gt; a second function over the result, it would be the same as combining the two functions and doing a single &lt;code&gt;fmap&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;I&#x27;m sorry. What?&lt;&#x2F;p&gt;
&lt;p&gt;OK, let&#x27;s have an example of that.&lt;&#x2F;p&gt;
&lt;p&gt;Therefore, if as well as our rather worrying &quot;exclaim&quot; function, we have one for shouting as well (don&#x27;t be distracted by the &lt;code&gt;&amp;lt;$&amp;gt;&lt;&#x2F;code&gt; for now, we&#x27;ll come to it in future...)&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;capitalise &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: String -&amp;gt; String
&lt;&#x2F;span&gt;&lt;span&gt;capitalise str = toUpper &amp;lt;$&amp;gt; str
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- capitalise &amp;quot;Horse&amp;quot; == &amp;quot;HORSE&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;...we can combine it to make one terrifying function...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;shouting &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Perhaps String -&amp;gt; PerhapsString
&lt;&#x2F;span&gt;&lt;span&gt;shouting p = fmap (capitalise . exclaim) p
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- shouting (Yeah &amp;quot;Bruce&amp;quot;) == Yeah &amp;quot;BRUCE!!!!!!!!!!!!&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;...which is exactly the same as this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;shouting2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Perhaps String -&amp;gt; PerhapsString
&lt;&#x2F;span&gt;&lt;span&gt;shouting2 p = fmap capitalise (fmap exclaim p)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- shouting2 (Yeah &amp;quot;Bruce&amp;quot;) == Yeah &amp;quot;BRUCE!!!!!!!!!!!!&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The Composition law just makes sure these are the same thing, so again, no funny business can take place.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;compositionLaw &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Bool
&lt;&#x2F;span&gt;&lt;span&gt;compositionLaw = fmap (capitalise . exclaim) (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Yeah &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Bruce&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;              == fmap capitalise (fmap exclaim (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Yeah &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Bruce&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;))
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- compositionLaw == True
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- ie, either way of doing this ends up the same
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Anyway. This is just one introduction to a kind of &lt;code&gt;functor&lt;&#x2F;code&gt;. In short, hand-wavey terms:&lt;&#x2F;p&gt;
&lt;h4 id=&quot;a-functor-is-a-thing-that-lets-you-safely-crap-around-with-the-values-inside-it-without-breaking-the-thing-itself&quot;&gt;&quot;A functor is a thing that lets you safely crap around with the values inside it without breaking the thing itself&quot;&lt;&#x2F;h4&gt;
&lt;p&gt;A few important notes and disclaimers:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;There doesn&#x27;t have to be just one &lt;code&gt;a&lt;&#x2F;code&gt; in the functor for this pattern to work. Another goto example for this is &lt;code&gt;List&lt;&#x2F;code&gt;, which can have absolutely loads in. In that case, the &lt;code&gt;fmap&lt;&#x2F;code&gt; runs the &lt;code&gt;(a -&amp;gt; b)&lt;&#x2F;code&gt; function on every item in the list, like &lt;code&gt;array.map&lt;&#x2F;code&gt; from Javascript, so it takes &lt;code&gt;[a]&lt;&#x2F;code&gt; and turns it into &lt;code&gt;[b]&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;There are functor instances for many datatypes such as &lt;code&gt;IO&lt;&#x2F;code&gt; where the &lt;code&gt;a&lt;&#x2F;code&gt; inside might represents a value that isn&#x27;t there yet (like perhaps it will come from some user input etc). Therefore doing &lt;code&gt;fmap&lt;&#x2F;code&gt; on such a datatype is just saying &quot;change the &lt;code&gt;a&lt;&#x2F;code&gt; inside to &lt;code&gt;b&lt;&#x2F;code&gt; whenever it happens to turn up&quot;, like &quot;when the user types their name, change the string to have lots of exclamation marks so they look like a weirdo when we later print it back for them&quot; or similar. This kind of idea can get a bit brain bending but once it settles it&#x27;s sort of magical to know you can crap around with the future just like you&#x27;re working with an array.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;ol start=&quot;3&quot;&gt;
&lt;li&gt;The &lt;code&gt;Perhaps&lt;&#x2F;code&gt; data type we have invented is really called &lt;code&gt;Maybe&lt;&#x2F;code&gt; and it&#x27;s all over the place. More on that another time, perhaps.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;That&#x27;s quite enough for now.&lt;&#x2F;p&gt;
&lt;p&gt;Make sense? If not, why not &lt;a href=&quot;&#x2F;contact.html&quot;&gt;get in touch&lt;&#x2F;a&gt;?&lt;&#x2F;p&gt;
&lt;p&gt;Further reading:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;http:&#x2F;&#x2F;adit.io&#x2F;posts&#x2F;2013-04-17-functors,_applicatives,_and_monads_in_pictures.html&quot;&gt;Functors, Applicative and Monads in Pictures&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Typeclasses - Show</title>
          <pubDate>Sun, 11 Nov 2018 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/typeclasses-show/</link>
          <guid>https://danieljharvey.github.io/typeclasses-show/</guid>
          <description xml:base="https://danieljharvey.github.io/typeclasses-show/">&lt;p&gt;Let&#x27;s think about things.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Thing &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Thing1 &lt;&#x2F;span&gt;&lt;span&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Thing2
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Seems great so far. Let&#x27;s try and look at one in &lt;code&gt;ghci&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Prelude&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; show &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Thing1
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Oh shit!&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;• &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;No &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Show Thing&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;arising from a use of&lt;&#x2F;span&gt;&lt;span&gt; ‘&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;show&lt;&#x2F;span&gt;&lt;span&gt;’
&lt;&#x2F;span&gt;&lt;span&gt;• &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;In&lt;&#x2F;span&gt;&lt;span&gt; the expression: show &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Thing1
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;In&lt;&#x2F;span&gt;&lt;span&gt; an equation for ‘it’: it = show &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Thing1
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Totally bogus!&lt;&#x2F;p&gt;
&lt;p&gt;So what&#x27;s the problem? So for something to be shown in the console, we need to know how to convert it into a String. This is a pretty common thing to be able to do, so we have a typeclass for it, called &lt;code&gt;Show&lt;&#x2F;code&gt;. Let&#x27;s check it out in &lt;code&gt;ghci&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Prelude&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; :i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Show
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Show &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;showsPrec &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Int -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; ShowS
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;show &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; String
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;showList &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; ShowS
&lt;&#x2F;span&gt;&lt;span&gt;  {-# &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MINIMAL&lt;&#x2F;span&gt;&lt;span&gt; showsPrec | show #-}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;A few more things than expected, tbh. Fortunately, that &lt;code&gt;{-# MINIMAL showsPrec | show #-}&lt;&#x2F;code&gt; means we can just define &lt;code&gt;show&lt;&#x2F;code&gt;, which takes any value of a type and turns it into a &lt;code&gt;String&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s make another data type.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BetterThing &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Better1 &lt;&#x2F;span&gt;&lt;span&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Better2
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This time, we&#x27;ll create an instance of &lt;code&gt;Show&lt;&#x2F;code&gt; for it.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Show BetterThing where
&lt;&#x2F;span&gt;&lt;span&gt;    show &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Better1 &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Better thing 1&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    show &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Better2 &lt;&#x2F;span&gt;&lt;span&gt;= &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;The other even better thing&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now we can &lt;code&gt;show&lt;&#x2F;code&gt; it as much as we like.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;showBetter1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: String
&lt;&#x2F;span&gt;&lt;span&gt;showBetter1 = show &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Better1
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- showBetter1 = &amp;quot;Better thing 1&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;showBetter2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: String
&lt;&#x2F;span&gt;&lt;span&gt;showBetter2 = show &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Better2
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- showBetter2 = &amp;quot;The other even better thing&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Great! What if we don&#x27;t need particularly clever string output? Just the name of the type would be fine. Can we cheat and derive one?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LazyThing &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Lazy &lt;&#x2F;span&gt;&lt;span&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Crazy &lt;&#x2F;span&gt;&lt;span&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Other &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Show&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Nothing seemed to complain there. Good stuff!&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;showLazy &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: String
&lt;&#x2F;span&gt;&lt;span&gt;showLazy = show &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Lazy
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- showLazy == &amp;quot;Lazy&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Success! And, so, as one would expect...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;showCrazy &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: String
&lt;&#x2F;span&gt;&lt;span&gt;showCrazy = show &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Crazy
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- showCrazy == &amp;quot;Crazy&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That&#x27;s better, but is there anything a bit more interesting we could do with this? Sure thing! What about a data type for creating HTML markup?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Href &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;String
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;HTMLTree &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Div&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;HTMLTree&lt;&#x2F;span&gt;&lt;span&gt; a]
&lt;&#x2F;span&gt;&lt;span&gt;              | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Title String
&lt;&#x2F;span&gt;&lt;span&gt;              | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;P String
&lt;&#x2F;span&gt;&lt;span&gt;              | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;A Href&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;HTMLTree&lt;&#x2F;span&gt;&lt;span&gt; a]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That &lt;code&gt;type&lt;&#x2F;code&gt; alias for &lt;code&gt;Href&lt;&#x2F;code&gt; just means it&#x27;s a normal string, but it makes the intent a bit clearer. So this little markup type has four kinds of element.&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;Div&lt;&#x2F;code&gt; has no content of it&#x27;s own but is also a wrapper for a list of &lt;code&gt;HTMLTree a&lt;&#x2F;code&gt; elements.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;Title&lt;&#x2F;code&gt; also takes a &lt;code&gt;String&lt;&#x2F;code&gt; and has no child elements.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;P&lt;&#x2F;code&gt; takes a &lt;code&gt;String&lt;&#x2F;code&gt; and has no child elements.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;A&lt;&#x2F;code&gt; takes an &lt;code&gt;Href&lt;&#x2F;code&gt; (an alias for &lt;code&gt;String&lt;&#x2F;code&gt;) and a list of children of type &lt;code&gt;HTMLTree a&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Let&#x27;s put it to work making an internet website.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;website &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Tree String
&lt;&#x2F;span&gt;&lt;span&gt;website = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Div
&lt;&#x2F;span&gt;&lt;span&gt;        [ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Title &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;My interesting website&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;        , &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;P &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Item of interest&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;        , &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;A &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;danieljharvey.github.io&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; [ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;P &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Home&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ]
&lt;&#x2F;span&gt;&lt;span&gt;        , &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Div&lt;&#x2F;span&gt;&lt;span&gt; [ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;P &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Sub-item&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;              , &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;P &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Another sub-item&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;              ]
&lt;&#x2F;span&gt;&lt;span&gt;        ]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Looks like the beginnings of a world beating internet business to me. How can &lt;code&gt;Show&lt;&#x2F;code&gt; help us though?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Show &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt; Show&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Tree &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;    show (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Div&lt;&#x2F;span&gt;&lt;span&gt; as) = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;lt;div&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++ concatSubtags as ++ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;lt;&#x2F;div&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    show (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;A&lt;&#x2F;span&gt;&lt;span&gt; href as) = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;lt;a href=&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++ href ++ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;#39;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;                     ++ concatSubtags as ++ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;lt;&#x2F;a&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    show (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;P&lt;&#x2F;span&gt;&lt;span&gt; a) = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;lt;p&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++ a ++ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;lt;&#x2F;p&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    show (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Title&lt;&#x2F;span&gt;&lt;span&gt; a) = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;lt;h1&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++ a ++ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;&amp;lt;&#x2F;h1&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- utility function for showing child items
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;concatSubtags &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Show &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Tree &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; String
&lt;&#x2F;span&gt;&lt;span&gt;concatSubtags as = concat $ fmap show as
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;By defining a &lt;code&gt;show&lt;&#x2F;code&gt; function for each of our tags (and recursively calling &lt;code&gt;show&lt;&#x2F;code&gt; for tags with child elements) we can generate HTML markup using one easy function:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Prelude&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; show website
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Hooray! A website!&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;div&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;h1&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;My interesting website&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;h1&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;Item of interest&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;href&lt;&#x2F;span&gt;&lt;span&gt;=&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;http:&#x2F;&#x2F;danieljharvey.github.io&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;&amp;gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;Website&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;div&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;Sub-item&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;Another sub-item&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;p&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;div&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;div&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;(Actually it comes out in one big line, and then I added the line breaks to fit it on the page. Oh, the lies!)&lt;&#x2F;p&gt;
&lt;p&gt;It could do with a bit of sprucing up, and a few more tags, but in my by no means humble opinion we&#x27;re on our way to creating the next React-beater. Great job, &lt;code&gt;show&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;(Incidentally, although this is a more fun example of &lt;code&gt;show&lt;&#x2F;code&gt;, I&#x27;d probably recommend just using a normal function for this kind of thing. Something classic like &lt;code&gt;renderTree :: (Show a) =&amp;gt; Tree a -&amp;gt; String&lt;&#x2F;code&gt; maybe. This isn&#x27;t strictly what the &lt;code&gt;Show&lt;&#x2F;code&gt; typeclass is for. There is also another type class called &lt;code&gt;Read&lt;&#x2F;code&gt; whose job is to turn the &lt;code&gt;String&lt;&#x2F;code&gt; output of &lt;code&gt;show&lt;&#x2F;code&gt; back into the original data type, and so making a clever &lt;code&gt;show&lt;&#x2F;code&gt; instance you&#x27;re somewhat implicitly promising that you can turn it back somehow. This could end up being a Bad Time, and that&#x27;s exactly the kind of time we don&#x27;t want to have.)&lt;&#x2F;p&gt;
&lt;p&gt;Make sense? If not, why not &lt;a href=&quot;&#x2F;contact.html&quot;&gt;get in touch&lt;&#x2F;a&gt;?&lt;&#x2F;p&gt;
&lt;p&gt;Further reading:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;http:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;base-4.12.0.0&#x2F;docs&#x2F;Text-Show.html&quot;&gt;Text.Show&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Typeclasses - Eq</title>
          <pubDate>Sat, 03 Nov 2018 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/typeclasses-eq/</link>
          <guid>https://danieljharvey.github.io/typeclasses-eq/</guid>
          <description xml:base="https://danieljharvey.github.io/typeclasses-eq/">&lt;p&gt;Let&#x27;s think about horses. There are three kinds of Horse:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Horse &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;SmallHorse &lt;&#x2F;span&gt;&lt;span&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LargeHorse &lt;&#x2F;span&gt;&lt;span&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;OtherHorse
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Let&#x27;s make a function to check whether two Horses are in fact equivalently sized.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;isSameHorse &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Horse -&amp;gt; Horse -&amp;gt; Bool
&lt;&#x2F;span&gt;&lt;span&gt;isSameHorse first second = first == second
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Looks like a classic. Let&#x27;s run it!&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;isSameHorse &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;SmallHorse LargeHorse
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Shit!&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;• &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;No &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Eq Horse&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;arising from a use of&lt;&#x2F;span&gt;&lt;span&gt; ‘==’
&lt;&#x2F;span&gt;&lt;span&gt;• &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;In&lt;&#x2F;span&gt;&lt;span&gt; the expression: first == second
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;In&lt;&#x2F;span&gt;&lt;span&gt; an equation for ‘isSameHorse’:
&lt;&#x2F;span&gt;&lt;span&gt;        isSameHorse first second = first == second
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That&#x27;s terrible news. What&#x27;s wrong here? Apparently, we need to make an instance of the &lt;code&gt;Eq&lt;&#x2F;code&gt; (short for &#x27;equality&#x27;) typeclass for &lt;code&gt;Horse&lt;&#x2F;code&gt; before they can be compared. What&#x27;s the &lt;code&gt;Eq&lt;&#x2F;code&gt; typeclass?&lt;&#x2F;p&gt;
&lt;p&gt;We can find out more by firing up &lt;code&gt;ghci&lt;&#x2F;code&gt;, the GHC repl.&lt;&#x2F;p&gt;
&lt;p&gt;You should see a prompt with the following:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Prelude&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If we enter &lt;code&gt;:info Eq&lt;&#x2F;code&gt;, we get the following:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Eq &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;(==) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Bool
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;(&#x2F;=) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Bool
&lt;&#x2F;span&gt;&lt;span&gt;  {-# &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MINIMAL&lt;&#x2F;span&gt;&lt;span&gt; (==) | (&#x2F;=) #-}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It shows there are two functions in the &lt;code&gt;Eq&lt;&#x2F;code&gt; typeclass, &lt;code&gt;==&lt;&#x2F;code&gt; and &lt;code&gt;&#x2F;=&lt;&#x2F;code&gt; (equals and not-equals), and that a &quot;minimal&quot; definition of &lt;code&gt;Eq&lt;&#x2F;code&gt; only needs one of those.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s start again and make a better horse.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;BetterHorse &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Tiny &lt;&#x2F;span&gt;&lt;span&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Average &lt;&#x2F;span&gt;&lt;span&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Huge
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Let&#x27;s not make the same mistake this time - let&#x27;s make an instance of the &lt;code&gt;Eq&lt;&#x2F;code&gt; typeclass for them. We are going to implement &lt;code&gt;==&lt;&#x2F;code&gt; which has a type of &lt;code&gt;a -&amp;gt; a -&amp;gt; Bool&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Eq BetterHorse where
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Tiny &lt;&#x2F;span&gt;&lt;span&gt;== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Tiny &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;True
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Average &lt;&#x2F;span&gt;&lt;span&gt;== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Average &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;True
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Huge &lt;&#x2F;span&gt;&lt;span&gt;== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Huge &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;True
&lt;&#x2F;span&gt;&lt;span&gt;    _ == _ = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;False
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;OK, seems fine. We&#x27;ve listed all the times two &lt;code&gt;BetterHorse&lt;&#x2F;code&gt; are the same and used &lt;code&gt;_ == _ = False&lt;&#x2F;code&gt; to mean &quot;anything else is not equal&quot; to save ourselves listing every alternative.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;isSameBetterHorse &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: BetterHorse -&amp;gt; BetterHorse -&amp;gt; Bool
&lt;&#x2F;span&gt;&lt;span&gt;isSameBetterHorse first second = first == second
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now our &lt;code&gt;BetterHorse&lt;&#x2F;code&gt; comparing function works. Let&#x27;s give it a go.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;nope &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Bool
&lt;&#x2F;span&gt;&lt;span&gt;nope = isSameBetterHorse &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Tiny Huge
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- nope = False
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;yep &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Bool
&lt;&#x2F;span&gt;&lt;span&gt;yep = isSameBetterHorse &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Average Average
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- yep = True
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;All seems to be fine here. We even get the &lt;code&gt;&#x2F;=&lt;&#x2F;code&gt; function for free by defining &lt;code&gt;==&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;nice &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Bool
&lt;&#x2F;span&gt;&lt;span&gt;nice = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Average &lt;&#x2F;span&gt;&lt;span&gt;&#x2F;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Tiny
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- nice = True
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you&#x27;re thinking &quot;this seems laborious&quot;, you&#x27;d be right. Fortunately, for basic data types like this, we can simply auto-generate an &lt;code&gt;Eq&lt;&#x2F;code&gt; instance in the data definition like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LazyHorse &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LazyTiny &lt;&#x2F;span&gt;&lt;span&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LazyOther &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Eq&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;workingNow = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LazyTiny &lt;&#x2F;span&gt;&lt;span&gt;== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LazyTiny
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- workingNow == True
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Great!&lt;&#x2F;p&gt;
&lt;p&gt;Make sense? If not, why not &lt;a href=&quot;&#x2F;contact.html&quot;&gt;get in touch&lt;&#x2F;a&gt;?&lt;&#x2F;p&gt;
&lt;p&gt;Further reading:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;http:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;base-4.12.0.0&#x2F;docs&#x2F;Data-Eq.html&quot;&gt;Data.Eq&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Typeclasses - Ord</title>
          <pubDate>Sat, 03 Nov 2018 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/typeclasses-ord/</link>
          <guid>https://danieljharvey.github.io/typeclasses-ord/</guid>
          <description xml:base="https://danieljharvey.github.io/typeclasses-ord/">&lt;p&gt;Let&#x27;s think about moods. Psychologists all agree there are only 5 real emotional states.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Mood &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Awful &lt;&#x2F;span&gt;&lt;span&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;QuiteBad &lt;&#x2F;span&gt;&lt;span&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;OK &lt;&#x2F;span&gt;&lt;span&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Good &lt;&#x2F;span&gt;&lt;span&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Great
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Which one is better? Is &lt;code&gt;Great&lt;&#x2F;code&gt; better than &lt;code&gt;Awful&lt;&#x2F;code&gt;?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;broken &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Bool
&lt;&#x2F;span&gt;&lt;span&gt;broken = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Awful &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Great
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Shit!&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;• &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;No &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;Ord Mood&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;arising from a use of&lt;&#x2F;span&gt;&lt;span&gt; ‘&amp;lt;’
&lt;&#x2F;span&gt;&lt;span&gt;• &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;In&lt;&#x2F;span&gt;&lt;span&gt; the expression: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Awful &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Great
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;In&lt;&#x2F;span&gt;&lt;span&gt; an equation for ‘broken’: broken = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Awful &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Great
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;These cannot be ordered! We need to implement the &lt;code&gt;Ord&lt;&#x2F;code&gt; typeclass (short for &quot;ordering&quot;) so that we can compare these values and sort them.&lt;&#x2F;p&gt;
&lt;p&gt;What will we need to do that then?&lt;&#x2F;p&gt;
&lt;p&gt;We can find out more by firing up &lt;code&gt;ghci&lt;&#x2F;code&gt;, the GHC repl.&lt;&#x2F;p&gt;
&lt;p&gt;You should see a prompt with the following:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;Prelude&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;Prelude&lt;&#x2F;code&gt; is telling us that we&#x27;ve loaded the Haskell Prelude, a standard library of functions. &lt;code&gt;Ord&lt;&#x2F;code&gt; is part of that, so let&#x27;s get some info.&lt;&#x2F;p&gt;
&lt;p&gt;Enter &lt;code&gt;:info Ord&lt;&#x2F;code&gt; into the repl (and press enter). You should see something like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Eq &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt; =&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ebcb8b;&quot;&gt;Ord &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;compare &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Ordering
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;(&amp;lt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Bool
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;(&amp;lt;=) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Bool
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;(&amp;gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Bool
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;(&amp;gt;=) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Bool
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;max &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;min &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;  {-# &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;MINIMAL&lt;&#x2F;span&gt;&lt;span&gt; compare | (&amp;lt;=) #-}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is a list of functions that the &lt;code&gt;Ord&lt;&#x2F;code&gt; typeclass implements. Looks like hard work. What&#x27;s interesting here?&lt;&#x2F;p&gt;
&lt;p&gt;Firstly - the &lt;code&gt;Eq a =&amp;gt;&lt;&#x2F;code&gt; constraint means that we can only define an &lt;code&gt;Ord&lt;&#x2F;code&gt; instance for something that has an &lt;code&gt;Eq&lt;&#x2F;code&gt; instance. I guess if we can&#x27;t tell if two values are the same, how can we dream of putting them in some sort of order?&lt;&#x2F;p&gt;
&lt;p&gt;Secondly - the slighly cryptic &lt;code&gt;{-# MINIMAL compare | (&amp;lt;=) #-}&lt;&#x2F;code&gt; line is telling us that we can define &lt;code&gt;Ord&lt;&#x2F;code&gt; by either defining the &lt;code&gt;compare&lt;&#x2F;code&gt; or &lt;code&gt;&amp;lt;=&lt;&#x2F;code&gt; functions. Haskell can generate the other functions by using either of these ones. That&#x27;s great news because hard work is hard and we don&#x27;t want to do hard work.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;SuperMood &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Worse &lt;&#x2F;span&gt;&lt;span&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;PrettyAverage &lt;&#x2F;span&gt;&lt;span&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Fine &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Eq&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We&#x27;re auto-generating the &lt;code&gt;Eq&lt;&#x2F;code&gt; because why have a bad time. Let&#x27;s make our &lt;code&gt;Ord&lt;&#x2F;code&gt; instance.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;instance Ord SuperMood where
&lt;&#x2F;span&gt;&lt;span&gt;    _ &amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Fine &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;True
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Worse &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;PrettyAverage &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;True
&lt;&#x2F;span&gt;&lt;span&gt;    _ &amp;lt;= _ = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;False
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We&#x27;re implementing it in terms of &lt;code&gt;&amp;lt;=&lt;&#x2F;code&gt; as it returns a &lt;code&gt;Bool&lt;&#x2F;code&gt; which is more straightforward than the &lt;code&gt;Ordering&lt;&#x2F;code&gt; datatype that &lt;code&gt;compare&lt;&#x2F;code&gt; uses).&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;_ &amp;lt;= Fine = True&lt;&#x2F;code&gt; means that all values are equal to or less than &lt;code&gt;Fine&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;Worse &amp;lt;= PrettyAverage = True&lt;&#x2F;code&gt; means that &lt;code&gt;Worse&lt;&#x2F;code&gt; is less than or equal to &lt;code&gt;PrettyAverage&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;_ &amp;lt;= _ = False&lt;&#x2F;code&gt; means any other combination returns &lt;code&gt;False&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Even though we&#x27;ve only implemented one function, since the others can defined using it we get all of them for free:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;yep &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Bool
&lt;&#x2F;span&gt;&lt;span&gt;yep = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Worse &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;PrettyAverage
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- yep = True
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;yep2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Bool
&lt;&#x2F;span&gt;&lt;span&gt;yep2 = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Fine &lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Worse
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- yep2 = True
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;yep3 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Bool
&lt;&#x2F;span&gt;&lt;span&gt;yep3 = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Fine &lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Fine
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- yep2 = True
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;nope &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Bool
&lt;&#x2F;span&gt;&lt;span&gt;nope = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Fine &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Fine
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- nope = False
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We also get to use any function that requires an &lt;code&gt;Ord&lt;&#x2F;code&gt; instance for free, like &lt;code&gt;sort&lt;&#x2F;code&gt; from &lt;code&gt;Data.List&lt;&#x2F;code&gt;, which has the following type signature:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;sort &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Ord &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This means &quot;if you pass me a list of any &lt;code&gt;a&lt;&#x2F;code&gt; which is orderable, I can return you a sorted list&quot;. Thanks!&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;Data.List
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;moods &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;SuperMood&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;moods = [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Fine&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Fine&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Worse&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;PrettyAverage&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;sorted &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;SuperMood&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;sorted = sort moods
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- sorted = [Worse, PrettyAverage, Fine, Fine]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Great job!&lt;&#x2F;p&gt;
&lt;p&gt;Still seems like hard work though, can we auto generate this typeclass too? Yes!&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;LazyMood &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Sloppy
&lt;&#x2F;span&gt;&lt;span&gt;              | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Ploppy
&lt;&#x2F;span&gt;&lt;span&gt;              | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Poopy
&lt;&#x2F;span&gt;&lt;span&gt;              | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nicey &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Eq&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Ord&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;lazySorted &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;LazyMood&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;lazySorted = sort [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nicey&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Poopy&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Ploppy&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Sloppy&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- lazySorted = [Sloppy, Ploppy, Poopy, Nicey]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Nice.&lt;&#x2F;p&gt;
&lt;p&gt;Make sense? If not, why not &lt;a href=&quot;&#x2F;contact.html&quot;&gt;get in touch&lt;&#x2F;a&gt;?&lt;&#x2F;p&gt;
&lt;p&gt;Further reading:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;http:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;base-4.12.0.0&#x2F;docs&#x2F;Data-Ord.html&quot;&gt;Data.Ord&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Why The Hell Should I Care About Do Notation?</title>
          <pubDate>Tue, 30 Oct 2018 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/do-notation/</link>
          <guid>https://danieljharvey.github.io/do-notation/</guid>
          <description xml:base="https://danieljharvey.github.io/do-notation/">&lt;p&gt;Do notation is a strange thing. You first see it in examples of &lt;code&gt;IO ()&lt;&#x2F;code&gt; with something like:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: IO &lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;main = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;    firstName &amp;lt;- getLine
&lt;&#x2F;span&gt;&lt;span&gt;    surname &amp;lt;- getLine
&lt;&#x2F;span&gt;&lt;span&gt;    print (&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Hello &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++ firstName ++ &amp;quot; &amp;quot; ++ surname)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And all seems well! This functional programming seems fine! But what&#x27;s going on here?&lt;&#x2F;p&gt;
&lt;p&gt;Actually what&#x27;s really going on here is&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: IO &lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;main2 = getLine
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;gt;&amp;gt;= (\firstName -&amp;gt; getLine
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;gt;&amp;gt;= (\surname -&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;        print (&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Hello &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; ++ firstName ++ &amp;quot; &amp;quot; ++ surname)))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;OK. That&#x27;s a little scarier. Quite a lot of weird arrows and stuff. What&#x27;s this mean then?&lt;&#x2F;p&gt;
&lt;p&gt;So &lt;code&gt;getStrLn&lt;&#x2F;code&gt; is a function that returns an &lt;code&gt;IO String&lt;&#x2F;code&gt;, which means &lt;code&gt;a string wrapped in the IO monad&lt;&#x2F;code&gt;. With a bit of hand-waving, the &lt;code&gt;&amp;gt;&amp;gt;=&lt;&#x2F;code&gt; (bind) function runs it, unwraps the &lt;code&gt;String&lt;&#x2F;code&gt; inside and passes it into the anonymous function (the one starting with &lt;code&gt;(\firstName -&amp;gt; ...)&lt;&#x2F;code&gt;). This function runs it AGAIN and passes the unwrapped result into the next function. As you can see, the layers could get quite deep, hence the do notation which aims to make everything clearer.&lt;&#x2F;p&gt;
&lt;p&gt;So is do notation it just an &lt;code&gt;IO&lt;&#x2F;code&gt; thing?&lt;&#x2F;p&gt;
&lt;p&gt;No!&lt;&#x2F;p&gt;
&lt;p&gt;Here&#x27;s an example with Maybe. &lt;code&gt;safeHead&lt;&#x2F;code&gt; is a function that returns the first item of a list wrapped in a &lt;code&gt;Just&lt;&#x2F;code&gt;, which means that if there isn&#x27;t a first item, instead of crapping out and ruining everything we return a &lt;code&gt;Nothing&lt;&#x2F;code&gt; which is easier to cope with.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;safeHead &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Maybe &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;safeHead &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;[]    &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing
&lt;&#x2F;span&gt;&lt;span&gt;safeHead (a:_) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; a
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And here is a function that does it twice with do notation. Note not an &lt;code&gt;IO ()&lt;&#x2F;code&gt; in sight!&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;safeHeadTwice &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;]] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Maybe &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;safeHeadTwice aas = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;    as &amp;lt;- safeHead aas
&lt;&#x2F;span&gt;&lt;span&gt;    a &amp;lt;- safeHead as
&lt;&#x2F;span&gt;&lt;span&gt;    return a
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;What does this do? Well it takes a list of lists, and returns the first value of the first list inside. Therefore there are two potential points of failure, that being, the entire input being an empty list, or the first list inside the main input being an empty list. Let&#x27;s desugar it!&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;safeHeadTwice2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;]] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Maybe &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;safeHeadTwice2 aas = safeHead aas
&lt;&#x2F;span&gt;&lt;span&gt;            &amp;gt;&amp;gt;= (\as -&amp;gt; safeHead as
&lt;&#x2F;span&gt;&lt;span&gt;            &amp;gt;&amp;gt;= (\a -&amp;gt; return a))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;There is probably a shorter way to write this one to be fair.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;safeHeadTwiceShort &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;]] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Maybe &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;safeHeadTwiceShort a = safeHead a &amp;gt;&amp;gt;= safeHead
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;OK, so in this case, &lt;code&gt;do notation&lt;&#x2F;code&gt; doesn&#x27;t look so great. Oh well. Remember: clever things are interesting but they&#x27;re not always good.&lt;&#x2F;p&gt;
&lt;p&gt;Make sense? If not, why not &lt;a href=&quot;&#x2F;contact.html&quot;&gt;get in touch&lt;&#x2F;a&gt;?&lt;&#x2F;p&gt;
&lt;p&gt;Further reading:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;en.wikibooks.org&#x2F;wiki&#x2F;Haskell&#x2F;do_notation&quot;&gt;Do Notation in Wikibooks&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;http:&#x2F;&#x2F;learnyouahaskell.com&#x2F;a-fistful-of-monads&quot;&gt;A Fistful of Monads&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Why The Hell Should I Care About Lens? (Part 1)</title>
          <pubDate>Tue, 30 Oct 2018 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/optics-pt-1-lens/</link>
          <guid>https://danieljharvey.github.io/optics-pt-1-lens/</guid>
          <description xml:base="https://danieljharvey.github.io/optics-pt-1-lens/">&lt;p&gt;Lenses are a thing that Haskell people talk about a lot. They bloody love a lens. Everywhere you go, lens, lens, lens. What is lens? Should we, mere mortals, care?&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s try and find out what the big deal is.&lt;&#x2F;p&gt;
&lt;p&gt;So.&lt;&#x2F;p&gt;
&lt;p&gt;Immutability is great, but it does mean that updating a value that lives deep within a big data structure can become an utter pain in the arse. Let&#x27;s define a data structure:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;AppConfig &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;AppConfig&lt;&#x2F;span&gt;&lt;span&gt; { value    :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Either String Int
&lt;&#x2F;span&gt;&lt;span&gt;                           , title    :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;String
&lt;&#x2F;span&gt;&lt;span&gt;                           , dbConfig :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;DbConfig
&lt;&#x2F;span&gt;&lt;span&gt;                           } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Show&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It contains &lt;code&gt;DbConfig&lt;&#x2F;code&gt; which looks like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;DbConfig &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;DbConfig&lt;&#x2F;span&gt;&lt;span&gt; { ipAddress :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;String
&lt;&#x2F;span&gt;&lt;span&gt;                         , thePort   :: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Int
&lt;&#x2F;span&gt;&lt;span&gt;                         } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;deriving&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Show&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And here is an example of the data:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;appData &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: AppConfig
&lt;&#x2F;span&gt;&lt;span&gt;appData = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;AppConfig&lt;&#x2F;span&gt;&lt;span&gt; {
&lt;&#x2F;span&gt;&lt;span&gt;    value = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Right 100
&lt;&#x2F;span&gt;&lt;span&gt;  , title = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Hello&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  , dbConfig = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;DbConfig&lt;&#x2F;span&gt;&lt;span&gt; {
&lt;&#x2F;span&gt;&lt;span&gt;      ipAddress = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;127.0.0.1&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    , thePort = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;8080
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;How would we get data from this structure in something like javascript?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;thePort &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;appData&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;dbConfig&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;thePort&lt;&#x2F;span&gt;&lt;span&gt;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; easy!
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It&#x27;s not too bad in Haskell either, as it auto creates selector functions for records like thus:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;getPort &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: AppConfig -&amp;gt; Int
&lt;&#x2F;span&gt;&lt;span&gt;getPort app = thePort (dbConfig app)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;(This could also be written as &lt;code&gt;thePort $ dbConfig app&lt;&#x2F;code&gt; but lets keep things simple.)&lt;&#x2F;p&gt;
&lt;p&gt;That all seems well and fine, but how would we change the port in this app config? In mutable JS-land, this would be easy, something like:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;appData&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;dbConfig&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;thePort &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;appData&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;dbConfig&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;thePort &lt;&#x2F;span&gt;&lt;span&gt;+ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Ignoring for a moment that we have ruined the original object and probably confused anything that depends on it, this is OK, and nicely concise. The immutable way is a bit wordier though...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;newAppData &lt;&#x2F;span&gt;&lt;span&gt;= {
&lt;&#x2F;span&gt;&lt;span&gt;  ...&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;appData&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  dbConfig: {
&lt;&#x2F;span&gt;&lt;span&gt;    ...&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;appData&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;dbConfig&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    thePort: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;appData&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;dbConfig&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;thePort &lt;&#x2F;span&gt;&lt;span&gt;+ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;};
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And this will only get worse as the levels get deeper. Haskell has a similar problem with deep updates in records, here&#x27;s the equivalent code:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;incrementPort &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: AppConfig -&amp;gt; AppConfig
&lt;&#x2F;span&gt;&lt;span&gt;incrementPort app =
&lt;&#x2F;span&gt;&lt;span&gt;    app { dbConfig = (dbConfig app) { thePort = oldPort + &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;} }
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;where&lt;&#x2F;span&gt;&lt;span&gt; oldPort = getPort app
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The worst thing about the code above is that the only really relevant part is &lt;code&gt;thePort = oldPort + 1&lt;&#x2F;code&gt;. Urgh. What&#x27;s the solution then? Lens!&lt;&#x2F;p&gt;
&lt;p&gt;A lens captures the idea of both a &lt;code&gt;getter&lt;&#x2F;code&gt; and a &lt;code&gt;setter&lt;&#x2F;code&gt; of a piece of data and a sub-part of it. Here is one for getting &lt;code&gt;title&lt;&#x2F;code&gt; from an &lt;code&gt;AppConfig&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;titleLens &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Lens&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;AppConfig String
&lt;&#x2F;span&gt;&lt;span&gt;titleLens = lens title (\app new -&amp;gt; app { title = new } )
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We can get the title of the app using &lt;code&gt;view&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;title = view titleLens appData
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- title = &amp;quot;Hello&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We can change the app&#x27;s title using &lt;code&gt;set&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;newApp = set titleLens &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;New Title&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot; appData
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- newApp = AppConfig
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--  { value = Right 100
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--  , title = &amp;quot;New Title&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--  , dbConfig = DbConfig
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--    { ipAddress = &amp;quot;127.0.0.1&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--    , thePort = 8080
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--    }
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--  }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Lastly, we can map over the app&#x27;s title using &lt;code&gt;over&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;newApp = over titleLens (\a -&amp;gt; a ++ &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;!!!&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;) appData
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- newApp = AppConfig
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--  { value = Right 100
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--  , title = &amp;quot;Hello!!!&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--  , dbConfig = DbConfig
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--    { ipAddress = &amp;quot;127.0.0.1&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--    , thePort = 8080
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--    }
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--  }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;OK. All so well and good, but this does not solve our deep structure update problem. How might we change &lt;code&gt;thePort&lt;&#x2F;code&gt; inside &lt;code&gt;dbConfig&lt;&#x2F;code&gt; in the same way?&lt;&#x2F;p&gt;
&lt;p&gt;By composing Lenses!&lt;&#x2F;p&gt;
&lt;p&gt;This lens gets us the &lt;code&gt;DbConfig&lt;&#x2F;code&gt; inside &lt;code&gt;AppConfig&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;dbConfigLens &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Lens&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;AppConfig DbConfig
&lt;&#x2F;span&gt;&lt;span&gt;dbConfigLens = lens dbConfig (\app db -&amp;gt; app { dbConfig = db })
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And this one gets us &lt;code&gt;thePort&lt;&#x2F;code&gt; inside &lt;code&gt;DbConfig&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;portLens &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Lens&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;DbConfig Int
&lt;&#x2F;span&gt;&lt;span&gt;portLens = lens thePort (\db port -&amp;gt; db { thePort = port } )
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;But by composing them together we get a lens that takes us from &lt;code&gt;AppConfig&lt;&#x2F;code&gt; all the way to &lt;code&gt;thePort&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;fullPortLens &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: Lens&lt;&#x2F;span&gt;&lt;span&gt;&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;AppConfig Int
&lt;&#x2F;span&gt;&lt;span&gt;fullPortLens = dbConfigLens . portLens
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;(Function composition with the &lt;code&gt;.&lt;&#x2F;code&gt; operator usually means that the right-hand function is run first, and then the left. With lenses, it makes more sense if we read from left to right, so if you squint it looks a little like javascript: &lt;code&gt;fullPort = dbConfig.port&lt;&#x2F;code&gt;. Kind of. Just nod and let&#x27;s not mention this again.)&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s use them to look at the port!&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;port = view fullPortLens appData
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- port = 8080
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To choose a new port!&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;newApp = set fullPortLens &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;9090&lt;&#x2F;span&gt;&lt;span&gt; appData
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- newApp = AppConfig
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--  { value = Right 100
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--  , title = &amp;quot;Hello&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--  , dbConfig = DbConfig
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--    { ipAddress = &amp;quot;127.0.0.1&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--    , thePort = 9090
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--    }
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--  }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Or to increment the port by &lt;code&gt;1&lt;&#x2F;code&gt;, for some reason!&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span&gt;newApp = over fullPortLens (+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;) appData
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;-- newApp = AppConfig
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--  { value = Right 100
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--  , title = &amp;quot;Hello&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--  , dbConfig = DbConfig
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--    { ipAddress = &amp;quot;127.0.0.1&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--    , thePort = 8081
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--    }
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;--  }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Look at all the things we&#x27;ve done with not too much code. We are now surely mighty hackers. A small disclaimer, the family of things that &lt;code&gt;Lens&lt;&#x2F;code&gt; is a part of are called &lt;code&gt;Optics&lt;&#x2F;code&gt;, and &lt;code&gt;Lens&lt;&#x2F;code&gt; is only the beginning. Next time, we&#x27;ll look at another variation on &lt;code&gt;Lens&lt;&#x2F;code&gt; called &lt;code&gt;Prism&lt;&#x2F;code&gt;, and see how it let&#x27;s us interact with that &lt;code&gt;Either&lt;&#x2F;code&gt; type we&#x27;ve conveniently ignored in these examples.&lt;&#x2F;p&gt;
&lt;p&gt;Make sense? If not, why not &lt;a href=&quot;&#x2F;contact.html&quot;&gt;get in touch&lt;&#x2F;a&gt;?&lt;&#x2F;p&gt;
&lt;p&gt;Further reading:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;http:&#x2F;&#x2F;randycoulman.com&#x2F;blog&#x2F;2016&#x2F;07&#x2F;12&#x2F;thinking-in-ramda-lenses&#x2F;&quot;&gt;Thinking in Ramda: Lenses&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.schoolofhaskell.com&#x2F;school&#x2F;to-infinity-and-beyond&#x2F;pick-of-the-week&#x2F;a-little-lens-starter-tutorial&quot;&gt;A Little Lens Starter Tutorial&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
      </item>
      <item>
          <title>Why The Hell Should I Care About Total Functions?</title>
          <pubDate>Sun, 21 Oct 2018 00:00:00 +0000</pubDate>
          <author>Unknown</author>
          <link>https://danieljharvey.github.io/total-functions/</link>
          <guid>https://danieljharvey.github.io/total-functions/</guid>
          <description xml:base="https://danieljharvey.github.io/total-functions/">&lt;p&gt;I recently read a piece of useful advice on Twitter about total functions which I enjoyed. The advice (which I will definitely accurately source at some point in the future, lol) goes something like this:&lt;&#x2F;p&gt;
&lt;h4 id=&quot;you-start-making-your-functions-total-by-adding-maybe-but-then-you-move-onto-removing-maybe&quot;&gt;&quot;You start making your functions total by adding Maybe, but then you move onto removing Maybe.&quot;&lt;&#x2F;h4&gt;
&lt;p&gt;Like all my favourite advice this sounds like it doesn&#x27;t make any sense, so I figured I&#x27;d try and get to the bottom of this.&lt;&#x2F;p&gt;
&lt;p&gt;Firstly, to clarify, a total function is a function that:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Always returns some valid data (rather than just when it feels like doing so) and&lt;&#x2F;li&gt;
&lt;li&gt;Does not go into an infinite loop.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Let&#x27;s start with a non-total function. The canonical example is Head from the Haskell Prelude, which returns you the first item in a list, and looks something like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;head &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;head (x: xs) = x
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If we pass along a list like &lt;code&gt;[1,2,3]&lt;&#x2F;code&gt;, we get &lt;code&gt;1&lt;&#x2F;code&gt; back, all great.&lt;&#x2F;p&gt;
&lt;p&gt;But what if we pass it an empty list - &lt;code&gt;[]&lt;&#x2F;code&gt; ?&lt;&#x2F;p&gt;
&lt;p&gt;If you guessed &quot;giant heartbreaking error&quot;- then correct! Exactly the type of thing we were told would never ever happen again once we submitted to a life of functional purity. So what&#x27;s the deal here?&lt;&#x2F;p&gt;
&lt;p&gt;Well, firstly, some history - this function was there when Haskell was still growing up and learning what to do in the world, and it&#x27;s very difficult to go and change something that is used in a million libraries, so it&#x27;s still there but should be left well alone.&lt;&#x2F;p&gt;
&lt;p&gt;So what is the Proper Way To Do Things then?&lt;&#x2F;p&gt;
&lt;p&gt;It&#x27;s to use a total version of the function, of course! Perhaps something like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;safeHead &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Maybe &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;safeHead &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;[] &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing
&lt;&#x2F;span&gt;&lt;span&gt;safeHead (x: xs) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; x
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now what happens? If we pass it &lt;code&gt;[1,2,3]&lt;&#x2F;code&gt; we get &#x27;Just 1` back (the first item wrapped in a Maybe type). Great!&lt;&#x2F;p&gt;
&lt;p&gt;And what about if we use the empty list &lt;code&gt;[]&lt;&#x2F;code&gt; ? We get &lt;code&gt;Nothing&lt;&#x2F;code&gt;, which is certainly not an error.&lt;&#x2F;p&gt;
&lt;p&gt;So far, so total, great stuff.&lt;&#x2F;p&gt;
&lt;p&gt;However, our function is currently responsible for two things:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Checking the inputs are even correct&lt;&#x2F;li&gt;
&lt;li&gt;Getting the first value&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Ideally, we should be able to use types to make sure this doesn&#x27;t happen, and make validating that data Somebody Else&#x27;s Problem.&lt;&#x2F;p&gt;
&lt;p&gt;Therefore, let&#x27;s start by creating a new datatype...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NonEmpty&lt;&#x2F;span&gt;&lt;span&gt; a = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NonEmpty&lt;&#x2F;span&gt;&lt;span&gt; a [a]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It contains a first value and a list, therefore ensuring there is always a first value. What does this to do to our head function then?&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;nonEmptyHead &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;:: NonEmpty &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a
&lt;&#x2F;span&gt;&lt;span&gt;nonEmptyHead (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NonEmpty&lt;&#x2F;span&gt;&lt;span&gt; x _) = x
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Because the data type cannot be empty, our head function is guaranteed to return a value and is thus total.&lt;&#x2F;p&gt;
&lt;p&gt;But we&#x27;ve got a list! How do we get one of these new fangled data types? Let&#x27;s make a helper function...&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;haskell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-haskell &quot;&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;createNonEmpty &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;-&amp;gt; Maybe&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;NonEmpty &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;createNonEmpty &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;[] &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Nothing
&lt;&#x2F;span&gt;&lt;span&gt;createNonEmpty (x : xs) = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;Just&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NonEmpty&lt;&#x2F;span&gt;&lt;span&gt; x xs)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;A function like this allows us to turn a regular list into a NonEmpty list, by returning a &lt;code&gt;Just NonEmpty&lt;&#x2F;code&gt; for a list with one or more items, or a &lt;code&gt;Nothing&lt;&#x2F;code&gt; if the original List is empty. This still means we have a possibility of failure (expressed by the Maybe), however our &lt;code&gt;head&lt;&#x2F;code&gt; function expects that to be dealt with elsewhere before it gets involved with the data, and is thus total whilst always returning a value.&lt;&#x2F;p&gt;
&lt;p&gt;Like all somewhat trivial examples, perhaps this seems like a lot of work for nothing (plus the overhead of dealing with a new datatype) - however this concept of doing a piece of validation once, capturing it in the type, and then having a much simpler time dealing with it onwards is very powerful indeed.&lt;&#x2F;p&gt;
&lt;p&gt;(btw, Haskell actually has a NonEmpty list type, in &lt;code&gt;Data.List.NonEmpty&lt;&#x2F;code&gt;, but it&#x27;s more fun to make our own)&lt;&#x2F;p&gt;
&lt;p&gt;Anyway, that&#x27;s quite enough for now I feel.&lt;&#x2F;p&gt;
&lt;p&gt;Make sense? If not, &lt;a href=&quot;&#x2F;contact.html&quot;&gt;get in touch&lt;&#x2F;a&gt;!&lt;&#x2F;p&gt;
&lt;p&gt;Further reading:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;adamdrake.com&#x2F;are-your-functions-total.html&quot;&gt;Are your functions total?&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;http:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;base-4.12.0.0&#x2F;docs&#x2F;Data-Maybe.html&quot;&gt;Data.Maybe&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;semigroups-0.16.0.1&#x2F;docs&#x2F;Data-List-NonEmpty.html&quot;&gt;Data.List.NonEmpty&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</description>
      </item>
    </channel>
</rss>
