<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>jaspervdj - Posts tagged haskell</title>
    <link href="http://jaspervdj.be/tags/haskell.xml" rel="self" />
    <link href="http://jaspervdj.be" />
    <id>http://jaspervdj.be/tags/haskell.xml</id>
    <author>
        <name>Jasper Van der Jeugt</name>
        <email>jaspervdj@gmail.com</email>
    </author>
    <updated>2013-05-08T00:00:00Z</updated>
    <entry>
    <title>OdHac</title>
    <link href="http://jaspervdj.be/posts/2013-05-08-odhac.html" />
    <id>http://jaspervdj.be/posts/2013-05-08-odhac.html</id>
    <published>2013-05-08T00:00:00Z</published>
    <updated>2013-05-08T00:00:00Z</updated>
    <summary type="html"><![CDATA[<h1 id="introduction">Introduction</h1>
<p>So, last thursday I escaped Belgium for a bit and travelled to <a href="https://www.youtube.com/watch?v=Yq_tDOFU5tY">Odessa</a> to attend <a href="http://www.haskell.org/haskellwiki/OdHac">OdHac</a>, a Haskell Hackathon organised there by <a href="http://ro-che.info/">Roman</a> and hosted by <a href="http://www.provectus-it.com/">Provectus IT</a>.</p>
<p>Odessa is a really sweet city. One thing I noticed is that while some of the buildings seem to be in a deplorable condition from the outside, they actually turn out to be very nice once you’re indoors! This was for example the case for the hostel I was staying in. The contrast is also seen elsewhere in the city, with brand new appartements next to ramshackle, desolated houses.</p>
<div class="figure">
<img src="/images/2013-05-08-odessa.jpg" alt="Picture by Simon Meier" /><p class="caption">Picture by Simon Meier</p>
</div>
<p>This is, however, not the case for the very centre of the city – around the famous <a href="http://en.wikipedia.org/wiki/Potemkin_Stairs">Potemkin Stairs</a>. This neighbourhood is absolutely amazing.</p>
<div class="figure">
<img src="/images/2013-05-08-potemkin-stairs.jpg" alt="Picture by some random Ukranian guy" /><p class="caption">Picture by some random Ukranian guy</p>
</div>
<h1 id="hakyll-at-odhac">Hakyll at OdHac</h1>
<p>At the Hackathon, I did a small presentation about <a href="http://jaspervdj.be/hakyll/">Hakyll</a> on friday, and some awesome people decided to help me improve Hakyll a bit this weekend. We were able to implement some exciting features.</p>
<div class="figure">
<img src="/images/2013-05-08-hakyll-team.jpg" alt="Working on Hakyll, picture by Roman Cheplyaka" /><p class="caption">Working on Hakyll, picture by Roman Cheplyaka</p>
</div>
<h2 id="if-else-conditionals-in-templates">If-else conditionals in templates</h2>
<p>We added some functionality to the Hakyll templates which you allows you to check if a certain value is defined. We chose to use the same syntax as Pandoc, to further ease integration:</p>
<pre><code>&lt;h1&gt;$title$&lt;/h1&gt;
$if(author)$
    &lt;em&gt;by $author$&lt;/em&gt;
$endif$</code></pre>
<p>We also support if-else-endif. On the plane back, I also took the time to implement <code>$foreach$</code> and <code>$partial$</code>.</p>
<h2 id="teasers">Teasers</h2>
<p>Teasers allow you to write a short introduction for a blogpost, and make it easy to reproduce this introduction on another page together with a “Read more” link. We decided to implement the convention used in Wordpress, which means you define your teasers using the following simple format:</p>
<pre><code>---
title: Some post
---

This is the introduction.

&lt;!--more--&gt;

The rest of the post...</code></pre>
<h2 id="pagination">Pagination</h2>
<p>We also started to work on a generic Pagination module. It’s not finished yet but it should be possible to clean this up and package it by the end of the week. I’m quite excited about this feature because lots of people have requested it in the past.</p>
<p>Additionally, we did some performance improvements and fixed a bug or two. All in all, a very productive and fun Hackathon! I hope to package up all these changes and push them to Hackage soon.</p>
<p>Thanks for contributing to Hakyll, Alexey Smirnov, Anton Dubovik, Dmitriy Shamatrin, Ivan Veselov and Pavel Poukh! And, of course, thanks for organizing, Roman!</p>]]></summary>
</entry>
<entry>
    <title>Hakyll 4.0</title>
    <link href="http://jaspervdj.be/posts/2013-01-16-hakyll-4.0.html" />
    <id>http://jaspervdj.be/posts/2013-01-16-hakyll-4.0.html</id>
    <published>2013-01-16T00:00:00Z</published>
    <updated>2013-01-16T00:00:00Z</updated>
    <summary type="html"><![CDATA[<p>After a few weeks of <a href="https://groups.google.com/d/topic/hakyll/eRKWz8bVB0w/discussion">beta status</a>, I’ve now released version 4.0 of the <a href="http://jaspervdj.be/hakyll">Hakyll</a> static site generator library. I’m really glad with this release, as from what I’ve found and heard, it makes many things a lot easier.</p>
<h1 id="main-changes">Main changes</h1>
<ul>
<li><p>The important <code>Compiler</code> type has been changed from <code>Arrow</code> to <code>Monad</code>: this makes it much easier to write custom compilers, as most Haskellers are more familiar with monads.</p></li>
<li><p>The template stays superficially the same, but it has grown much more powerful and flexible underneath.</p></li>
<li><p>Early and fast access to metadata makes things such as tags and pagination much easier.</p></li>
<li><p>All items (images, css…) can now have metadata associated. Metadata can no longer be manipulated, and this immutability should reduce the number of encountered bugs.</p></li>
<li><p>A <code>check</code> command has been added. This allows you to check that all internal (or external) links are still alive.</p></li>
</ul>
<h1 id="installation-migration">Installation, migration</h1>
<p>In order to install Hakyll 4, grab it from Hackage:</p>
<pre><code>cabal update
cabal install hakyll</code></pre>
<p>Here are some useful links:</p>
<ul>
<li>Website: <a href="http://jaspervdj.be/hakyll/">http://jaspervdj.be/hakyll/</a></li>
<li>Tutorial index: <a href="http://jaspervdj.be/hakyll/tutorials.html">http://jaspervdj.be/hakyll/tutorials.html</a></li>
<li>Migration guide: <a href="http://jaspervdj.be/hakyll/tutorials/hakyll-3-to-hakyll4-migration-guide.html">http://jaspervdj.be/hakyll/tutorials/hakyll-3-to-hakyll4-migration-guide.html</a></li>
</ul>
<p>All feedback is welcome as always.</p>]]></summary>
</entry>
<entry>
    <title>Wiggling Sums</title>
    <link href="http://jaspervdj.be/posts/2012-10-17-wiggling-sums.html" />
    <id>http://jaspervdj.be/posts/2012-10-17-wiggling-sums.html</id>
    <published>2012-10-17T00:00:00Z</published>
    <updated>2012-10-17T00:00:00Z</updated>
    <summary type="html"><![CDATA[<h1 id="the-problem">The problem</h1>
<p>The context of this problem is related to optimization problems: given some value, we want to produce a bunch of related values.</p>
<p>An example of where such an operation can be found is <code>shrink :: Arbitrary a =&gt; a -&gt; [a]</code>, found in the <a href="http://hackage.haskell.org/package/QuickCheck">QuickCheck</a> library.</p>
<p><a href="https://plus.google.com/115820041274148867209/posts">Alex</a> and I encountered an fun problem while working on something similar at <a href="http://www.tsurucapital.com/">Tsuru</a>. This blogpost is not really aimed at people who have just begun reading about Haskell as it contains little text and requires some intuition about sums and products (in the more general sense).</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="ot">{-# LANGUAGE FlexibleInstances #-}</span>
<span class="ot">&gt;</span> <span class="kw">import</span> Control.Applicative
<span class="ot">&gt;</span> <span class="kw">import</span> Data.Traversable</code></pre>
<p>We can capture our idea of related values in a typeclass:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">class</span> <span class="dt">Wiggle</span> a <span class="kw">where</span>
<span class="ot">&gt;     wiggle ::</span> a <span class="ot">-&gt;</span> [a]</code></pre>
<p>And define a simple instance for <code>Int</code> or <code>Double</code>:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">instance</span> <span class="dt">Wiggle</span> <span class="dt">Int</span> <span class="kw">where</span>
<span class="ot">&gt;</span>     wiggle x <span class="fu">=</span> [x <span class="fu">-</span> <span class="dv">1</span>, x, x <span class="fu">+</span> <span class="dv">1</span>]</code></pre>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">instance</span> <span class="dt">Wiggle</span> <span class="dt">Double</span> <span class="kw">where</span>
<span class="ot">&gt;</span>     wiggle x <span class="fu">=</span> <span class="kw">let</span> eps <span class="fu">=</span> <span class="dv">0</span><span class="fu">.</span><span class="dv">03</span> <span class="kw">in</span> [x <span class="fu">-</span> eps, x, x <span class="fu">+</span> eps]</code></pre>
<p>The interesting notion is to define instances for more general (combined) types. Given a tuple, we can wiggle it in two ways: either wiggle one of its components, or wiggle them both. Let’s express both notions using two simple newtypes <sup><a href="#fn1" class="footnoteRef" id="fnref1">1</a></sup>:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">newtype</span> <span class="dt">Product</span> a <span class="fu">=</span> <span class="dt">Product</span> {<span class="ot">unProduct ::</span> a}
<span class="ot">&gt;</span>     <span class="kw">deriving</span> (<span class="kw">Show</span>)</code></pre>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">instance</span> (<span class="dt">Wiggle</span> a, <span class="dt">Wiggle</span> b) <span class="ot">=&gt;</span> <span class="dt">Wiggle</span> (<span class="dt">Product</span> (a, b)) <span class="kw">where</span>
<span class="ot">&gt;</span>     wiggle (<span class="dt">Product</span> (x, y)) <span class="fu">=</span>
<span class="ot">&gt;</span>         [<span class="dt">Product</span> (x&#39;, y&#39;) <span class="fu">|</span> x&#39; <span class="ot">&lt;-</span> wiggle x, y&#39; <span class="ot">&lt;-</span> wiggle y]</code></pre>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">newtype</span> <span class="dt">Sum</span> a <span class="fu">=</span> <span class="dt">Sum</span> {<span class="ot">unSum ::</span> a}
<span class="ot">&gt;</span>     <span class="kw">deriving</span> (<span class="kw">Show</span>)</code></pre>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">instance</span> (<span class="dt">Wiggle</span> a, <span class="dt">Wiggle</span> b) <span class="ot">=&gt;</span> <span class="dt">Wiggle</span> (<span class="dt">Sum</span> (a, b)) <span class="kw">where</span>
<span class="ot">&gt;</span>     wiggle (<span class="dt">Sum</span> (x, y)) <span class="fu">=</span>
<span class="ot">&gt;</span>         [<span class="dt">Sum</span> (x&#39;, y) <span class="fu">|</span> x&#39; <span class="ot">&lt;-</span> wiggle x] <span class="fu">++</span>
<span class="ot">&gt;</span>         [<span class="dt">Sum</span> (x, y&#39;) <span class="fu">|</span> y&#39; <span class="ot">&lt;-</span> wiggle y]</code></pre>
<p>The same applies to structures such as lists. We can wiggle all elements of a list, or just a single one (if the list is non-empty). Both instances are reasonably straightforward to write.</p>
<p>The interesting question is if and how we can do it for a more general family of structures than lists? <code>Foldable</code>? <code>Traversable</code>?</p>
<p>A <code>Wiggle</code> instance for traversable products is not that hard:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">instance</span> (<span class="kw">Traversable</span> t, <span class="dt">Wiggle</span> a) <span class="ot">=&gt;</span> <span class="dt">Wiggle</span> (<span class="dt">Product</span> (t a)) <span class="kw">where</span>
<span class="ot">&gt;</span>     wiggle (<span class="dt">Product</span> xs) <span class="fu">=</span> <span class="fu">map</span> <span class="dt">Product</span> <span class="fu">$</span> traverse wiggle xs</code></pre>
<p>But how about the instance:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">instance</span> (<span class="kw">Traversable</span> t, <span class="dt">Wiggle</span> a) <span class="ot">=&gt;</span> <span class="dt">Wiggle</span> (<span class="dt">Sum</span> (t a)) <span class="kw">where</span>
<span class="ot">&gt;</span>     wiggle <span class="fu">=</span> wiggleSum</code></pre>
<h1 id="the-solution">The solution</h1>
<p>Is it possible? Can you come up with a nicer solution than we have?</p>
<p><input type="button" value="Click to reveal the solution"
    onclick="document.getElementById('solution').style.display='block';" /></p>
<div id="solution" style="display: none">

<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; wiggleSum ::</span> (<span class="kw">Traversable</span> t, <span class="dt">Wiggle</span> a) <span class="ot">=&gt;</span> <span class="dt">Sum</span> (t a) <span class="ot">-&gt;</span> [<span class="dt">Sum</span> (t a)]
<span class="ot">&gt;</span> wiggleSum <span class="fu">=</span> <span class="fu">map</span> <span class="dt">Sum</span> <span class="fu">.</span> <span class="fu">concatMap</span> sequenceA <span class="fu">.</span> rightOnce <span class="fu">.</span>
<span class="ot">&gt;</span>         traverse (\x <span class="ot">-&gt;</span> <span class="dt">Parent</span> (<span class="dt">Leaf</span> [x]) (<span class="dt">Leaf</span> (wiggle x))) <span class="fu">.</span> unSum</code></pre>
<p>In our solution, we use an auxiliary data structure, a non-empty binary tree with elements at the leaves.</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">data</span> <span class="dt">Tree</span> a
<span class="ot">&gt;</span>     <span class="fu">=</span> <span class="dt">Leaf</span> a
<span class="ot">&gt;</span>     <span class="fu">|</span> <span class="dt">Parent</span> (<span class="dt">Tree</span> a) (<span class="dt">Tree</span> a)
<span class="ot">&gt;</span>     <span class="kw">deriving</span> (<span class="kw">Show</span>)</code></pre>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">instance</span> <span class="kw">Functor</span> <span class="dt">Tree</span> <span class="kw">where</span>
<span class="ot">&gt;</span>     <span class="fu">fmap</span> f (<span class="dt">Leaf</span> x)     <span class="fu">=</span> <span class="dt">Leaf</span> (f x)
<span class="ot">&gt;</span>     <span class="fu">fmap</span> f (<span class="dt">Parent</span> x y) <span class="fu">=</span> <span class="dt">Parent</span> (<span class="fu">fmap</span> f x) (<span class="fu">fmap</span> f y)</code></pre>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">instance</span> <span class="kw">Applicative</span> <span class="dt">Tree</span> <span class="kw">where</span>
<span class="ot">&gt;</span>     pure <span class="fu">=</span> <span class="dt">Leaf</span>
<span class="ot">&gt;</span>     <span class="dt">Parent</span> f g <span class="fu">&lt;*&gt;</span> x          <span class="fu">=</span> <span class="dt">Parent</span> (f <span class="fu">&lt;*&gt;</span> x) (g <span class="fu">&lt;*&gt;</span> x)
<span class="ot">&gt;</span>     <span class="dt">Leaf</span> f     <span class="fu">&lt;*&gt;</span> <span class="dt">Leaf</span> x     <span class="fu">=</span> <span class="dt">Leaf</span> (f x)
<span class="ot">&gt;</span>     <span class="dt">Leaf</span> f     <span class="fu">&lt;*&gt;</span> <span class="dt">Parent</span> x y <span class="fu">=</span> <span class="dt">Parent</span> (f <span class="fu">&lt;$&gt;</span> x) (f <span class="fu">&lt;$&gt;</span> y)</code></pre>
<p>This utility finds the leftmost element of a tree:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; leftMost ::</span> <span class="dt">Tree</span> a <span class="ot">-&gt;</span> a
<span class="ot">&gt;</span> leftMost (<span class="dt">Leaf</span> x)     <span class="fu">=</span> x
<span class="ot">&gt;</span> leftMost (<span class="dt">Parent</span> x _) <span class="fu">=</span> leftMost x</code></pre>
<p>We look at all the leaves in the tree and their corresponding paths to the root node; we take only the leaves which have exactly one right edge on their path.</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; rightOnce ::</span> <span class="dt">Tree</span> a <span class="ot">-&gt;</span> [a]
<span class="ot">&gt;</span> rightOnce (<span class="dt">Leaf</span> _)     <span class="fu">=</span> []
<span class="ot">&gt;</span> rightOnce (<span class="dt">Parent</span> x y) <span class="fu">=</span> leftMost y <span class="fu">:</span> rightOnce x</code></pre>
<div></div></div>


<div class="footnotes">
<hr />
<ol>
<li id="fn1"><p>These newtypes are also defined in <code>Data.Monoid</code>. I defined them again here to avoid confusion: this code does not use the <code>Monoid</code> instance in any way.<a href="#fnref1">↩</a></p></li>
</ol>
</div>]]></summary>
</entry>
<entry>
    <title>Applicative, bidirectional serialization combinators</title>
    <link href="http://jaspervdj.be/posts/2012-09-07-applicative-bidirectional-serialization-combinators.html" />
    <id>http://jaspervdj.be/posts/2012-09-07-applicative-bidirectional-serialization-combinators.html</id>
    <published>2012-09-07T00:00:00Z</published>
    <updated>2012-09-07T00:00:00Z</updated>
    <summary type="html"><![CDATA[<h1 id="unrelated">Unrelated</h1>
<p><em>If you are only interested in the neat Haskell trick, skip this section.</em> It has been a while since I updated my personal blog here. There has been a lot going on in my life in the past few months, including exams, a breakup with my girlfriend and moving to Tokyo for an internship at <a href="http://www.tsurucapital.com/">Tsuru Capital</a>.</p>
<p>It has been really great so far, the work is interesting, so are the people, and we have a nice view from the office.</p>
<div class="figure">
<img src="/images/2012-09-07-view-from-tsuru.jpg" alt="View from the Tsuru Capital office" /><p class="caption">View from the Tsuru Capital office</p>
</div>
<h1 id="prologue">Prologue</h1>
<p>This blogpost is written in literate Haskell (<a href="https://github.com/jaspervdj/jaspervdj/blob/master/posts/2012-09-07-applicative-bidirectional-serialization-combinators.lhs">source here</a>), so you can drop it in a file, load it in <code>GHCi</code> and play around with it, should you feel like doing this.</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="ot">{-# LANGUAGE FlexibleInstances #-}</span>
<span class="ot">&gt;</span> <span class="ot">{-# LANGUAGE GADTs #-}</span>
<span class="ot">&gt;</span> <span class="ot">{-# LANGUAGE OverloadedStrings #-}</span>
<span class="ot">&gt;</span> <span class="ot">{-# LANGUAGE ScopedTypeVariables #-}</span>
<span class="ot">&gt;</span> <span class="ot">{-# LANGUAGE TypeSynonymInstances #-}</span>
<span class="ot">&gt;</span> <span class="kw">import</span> Control.Applicative
<span class="ot">&gt;</span> <span class="kw">import</span> Control.Monad
<span class="ot">&gt;</span> <span class="kw">import</span> Data.Aeson</code></pre>
<p>A lot of serialization libraries use a technique employing two typeclasses (or one typeclass with two methods) in order to convert data from and to some format. An example is the excellent <a href="http://hackage.haskell.org/package/aeson">aeson</a> library:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">data</span> <span class="dt">Food</span> <span class="fu">=</span> <span class="dt">Food</span>
<span class="ot">&gt;</span>     {<span class="ot"> foodName ::</span> <span class="dt">String</span>
<span class="ot">&gt;</span>     ,<span class="ot"> foodCost ::</span> <span class="dt">Int</span>
<span class="ot">&gt;</span>     } <span class="kw">deriving</span> (<span class="kw">Show</span>)</code></pre>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">instance</span> <span class="dt">ToJSON</span> <span class="dt">Food</span> <span class="kw">where</span>
<span class="ot">&gt;</span>     toJSON (<span class="dt">Food</span> name cost) <span class="fu">=</span> object
<span class="ot">&gt;</span>         [ <span class="st">&quot;name&quot;</span> <span class="fu">.=</span> name
<span class="ot">&gt;</span>         , <span class="st">&quot;cost&quot;</span> <span class="fu">.=</span> cost
<span class="ot">&gt;</span>         ]</code></pre>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">instance</span> <span class="dt">FromJSON</span> <span class="dt">Food</span> <span class="kw">where</span>
<span class="ot">&gt;</span>     parseJSON (<span class="dt">Object</span> obj) <span class="fu">=</span> <span class="dt">Food</span>
<span class="ot">&gt;</span>         <span class="fu">&lt;$&gt;</span> obj <span class="fu">.:</span> <span class="st">&quot;name&quot;</span>
<span class="ot">&gt;</span>         <span class="fu">&lt;*&gt;</span> obj <span class="fu">.:</span> <span class="st">&quot;cost&quot;</span>
<span class="ot">&gt;</span>     parseJSON _ <span class="fu">=</span> mzero</code></pre>
<p>Some other libraries using this technique are <a href="http://hackage.haskell.org/package/cassava">cassava</a>, <a href="http://hackage.haskell.org/package/postgresql-simple">postgresql-simple</a>, and <a href="http://hackage.haskell.org/package/binary">binary</a>.</p>
<p>While working on some new internal <a href="http://www.sqlite.org/">SQLite</a> bindings and utilities at Tsuru, I discovered another technique which is more concise, but keeps the nice <code>Applicative</code> interface. The code in this blogpost disregards performance and only supports the most basic of features in order to be easier to understand.</p>
<h1 id="primitive-types">Primitive types</h1>
<p>We start out by defining a typeclass for primitive serializable types, such as <code>Int</code> and <code>String</code>. We disregard performance, as said before, and we are also going to ignore proper error checking: we just serialize these primitive types to <code>String</code>s.</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">class</span> <span class="dt">Field</span> a <span class="kw">where</span>
<span class="ot">&gt;     fieldType ::</span> a <span class="ot">-&gt;</span> <span class="dt">String</span>  <span class="co">-- Should work with &#39;undefined&#39;</span>
<span class="ot">&gt;     fieldRead ::</span> <span class="dt">String</span> <span class="ot">-&gt;</span> a
<span class="ot">&gt;     fieldShow ::</span> a <span class="ot">-&gt;</span> <span class="dt">String</span></code></pre>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">instance</span> <span class="dt">Field</span> <span class="dt">String</span> <span class="kw">where</span>
<span class="ot">&gt;</span>     fieldType <span class="fu">=</span> <span class="fu">const</span> <span class="st">&quot;TEXT&quot;</span>
<span class="ot">&gt;</span>     fieldRead <span class="fu">=</span> <span class="fu">id</span>  <span class="co">-- We need proper escaping here</span>
<span class="ot">&gt;</span>     fieldShow <span class="fu">=</span> <span class="fu">id</span></code></pre>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">instance</span> <span class="dt">Field</span> <span class="dt">Int</span> <span class="kw">where</span>
<span class="ot">&gt;</span>     fieldType <span class="fu">=</span> <span class="fu">const</span> <span class="st">&quot;INTEGER&quot;</span>
<span class="ot">&gt;</span>     fieldRead <span class="fu">=</span> <span class="fu">read</span>
<span class="ot">&gt;</span>     fieldShow <span class="fu">=</span> <span class="fu">show</span></code></pre>
<p>These fields are sufficient to store some <code>Food</code>, so what else do we need? We need “instantiations” of these fields which actually represent a part of a record. This can be modeled as:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">data</span> <span class="dt">FieldInfo</span> t a <span class="fu">=</span> <span class="dt">FieldInfo</span>
<span class="ot">&gt;</span>     {<span class="ot"> fieldName    ::</span> <span class="dt">String</span>
<span class="ot">&gt;</span>     ,<span class="ot"> fieldExtract ::</span> t <span class="ot">-&gt;</span> a
<span class="ot">&gt;</span>     }</code></pre>
<p>Let’s give an example, we would map the cost of food as:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; costFieldInfo ::</span> <span class="dt">FieldInfo</span> <span class="dt">Food</span> <span class="dt">Int</span>
<span class="ot">&gt;</span> costFieldInfo <span class="fu">=</span> <span class="dt">FieldInfo</span> <span class="st">&quot;cost&quot;</span> foodCost</code></pre>
<p><em>This <code>costFieldInfo</code> is not used in the code below, it is just an example.</em></p>
<h1 id="gadts">GADTs</h1>
<p>In the case of an SQLite database, such a <code>FieldInfo</code> actually represent a column in our table. We will be building our table using <code>Applicative</code>, <em>without</em> actually evaluating anything yet. This seems related to <em>free monads</em> – determining the exact category-theoretical relation is left as an excercise for the mathematical-minded reader.</p>
<p>We just have constructors for each method of the applicative interface, plus an SQLite primitive to add a column to our table.</p>
<p>This requires the great <a href="http://www.haskell.org/haskellwiki/GADT">GADTs</a> extension, it is not possible to do this with Haskell 98 datatypes – the wiki page has more information.</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">data</span> <span class="dt">Table</span> t f <span class="kw">where</span>
<span class="ot">&gt;</span>     <span class="co">-- Applicative interface:</span>
<span class="ot">&gt;</span>     <span class="co">-- &lt;$&gt;, pure and &lt;*&gt;</span>
<span class="ot">&gt;</span>     <span class="dt">Map</span><span class="ot">  ::</span> (a <span class="ot">-&gt;</span> b) <span class="ot">-&gt;</span> <span class="dt">Table</span> t a <span class="ot">-&gt;</span> <span class="dt">Table</span> t b
<span class="ot">&gt;</span>     <span class="dt">Pure</span><span class="ot"> ::</span> a <span class="ot">-&gt;</span> <span class="dt">Table</span> t a
<span class="ot">&gt;</span>     <span class="dt">App</span><span class="ot">  ::</span> <span class="dt">Table</span> t (a <span class="ot">-&gt;</span> b) <span class="ot">-&gt;</span> <span class="dt">Table</span> t a <span class="ot">-&gt;</span> <span class="dt">Table</span> t b
<span class="ot">&gt;</span> 
<span class="ot">&gt;</span>     <span class="co">-- Primitives</span>
<span class="ot">&gt;</span>     <span class="dt">Column</span><span class="ot"> ::</span> <span class="dt">Field</span> a <span class="ot">=&gt;</span> <span class="dt">FieldInfo</span> t a <span class="ot">-&gt;</span> <span class="dt">Table</span> t a</code></pre>
<p>This makes the <code>Functor</code> and <code>Applicative</code> instances trivial to implement.</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">instance</span> <span class="kw">Functor</span> (<span class="dt">Table</span> t) <span class="kw">where</span>
<span class="ot">&gt;</span>     <span class="fu">fmap</span> <span class="fu">=</span> <span class="dt">Map</span></code></pre>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">instance</span> <span class="kw">Applicative</span> (<span class="dt">Table</span> t) <span class="kw">where</span>
<span class="ot">&gt;</span>     pure  <span class="fu">=</span> <span class="dt">Pure</span>
<span class="ot">&gt;</span>     (<span class="fu">&lt;*&gt;</span>) <span class="fu">=</span> <span class="dt">App</span></code></pre>
<p>This GADT allows us to model actual tables:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; foodTable ::</span> <span class="dt">Table</span> <span class="dt">Food</span> <span class="dt">Food</span>
<span class="ot">&gt;</span> foodTable <span class="fu">=</span> <span class="dt">Food</span>
<span class="ot">&gt;</span>     <span class="fu">&lt;$&gt;</span> <span class="dt">Column</span> (<span class="dt">FieldInfo</span> <span class="st">&quot;name&quot;</span> foodName)
<span class="ot">&gt;</span>     <span class="fu">&lt;*&gt;</span> <span class="dt">Column</span> (<span class="dt">FieldInfo</span> <span class="st">&quot;cost&quot;</span> foodCost)</code></pre>
<p>Let’s make some nicer syntax and write <code>foodTable</code> again:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; column ::</span> <span class="dt">Field</span> a <span class="ot">=&gt;</span> <span class="dt">String</span> <span class="ot">-&gt;</span> (t <span class="ot">-&gt;</span> a) <span class="ot">-&gt;</span> <span class="dt">Table</span> t a
<span class="ot">&gt;</span> column name extract <span class="fu">=</span> <span class="dt">Column</span> (<span class="dt">FieldInfo</span> name extract)</code></pre>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; foodTable&#39; ::</span> <span class="dt">Table</span> <span class="dt">Food</span> <span class="dt">Food</span>
<span class="ot">&gt;</span> foodTable&#39; <span class="fu">=</span> <span class="dt">Food</span>
<span class="ot">&gt;</span>     <span class="fu">&lt;$&gt;</span> column <span class="st">&quot;name&quot;</span> foodName
<span class="ot">&gt;</span>     <span class="fu">&lt;*&gt;</span> column <span class="st">&quot;cost&quot;</span> foodCost</code></pre>
<p>Lookin’ good!</p>
<h1 id="the-real-work">The real work</h1>
<p>While procrastrinating by creating GADT wrappers is fun, at one point we should write some actual implementation.</p>
<p>These implementations work by evaluating the trees we created with the different constructors for the GADT.</p>
<p>Our first method crawls the table tree and returns the name and type of each column:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; metaRecord ::</span> <span class="dt">Table</span> t t <span class="ot">-&gt;</span> [(<span class="dt">String</span>, <span class="dt">String</span>)]
<span class="ot">&gt;</span> metaRecord <span class="fu">=</span> go
<span class="ot">&gt;</span>   <span class="kw">where</span>
<span class="ot">&gt;     go ::</span> forall t a<span class="fu">.</span> <span class="dt">Table</span> t a <span class="ot">-&gt;</span> [(<span class="dt">String</span>, <span class="dt">String</span>)]
<span class="ot">&gt;</span>     go (<span class="dt">Map</span> _ t)   <span class="fu">=</span> go t
<span class="ot">&gt;</span>     go (<span class="dt">Pure</span> _ )   <span class="fu">=</span> []
<span class="ot">&gt;</span>     go (<span class="dt">App</span> t1 t2) <span class="fu">=</span> go t1 <span class="fu">++</span> go t2
<span class="ot">&gt;</span>     go (<span class="dt">Column</span> fi) <span class="fu">=</span> [(fieldName fi, fieldType (<span class="fu">undefined</span><span class="ot"> ::</span> a))]</code></pre>
<p>The second method is only slightly more complicated: instead of returning the type of each column, it calls our custom extract function for that column to get its value. We can then call <code>fieldShow</code> on that to do the final serialization, and what we get is the serialized name and value of each column.</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; toRecord ::</span> forall t<span class="fu">.</span> <span class="dt">Table</span> t t <span class="ot">-&gt;</span> t <span class="ot">-&gt;</span> [(<span class="dt">String</span>, <span class="dt">String</span>)]
<span class="ot">&gt;</span> toRecord tab x <span class="fu">=</span> go tab
<span class="ot">&gt;</span>   <span class="kw">where</span>
<span class="ot">&gt;     go ::</span> forall a<span class="fu">.</span> <span class="dt">Table</span> t a <span class="ot">-&gt;</span> [(<span class="dt">String</span>, <span class="dt">String</span>)]
<span class="ot">&gt;</span>     go (<span class="dt">Map</span> _ t)   <span class="fu">=</span> go t
<span class="ot">&gt;</span>     go (<span class="dt">Pure</span> _ )   <span class="fu">=</span> []
<span class="ot">&gt;</span>     go (<span class="dt">App</span> t1 t2) <span class="fu">=</span> go t1 <span class="fu">++</span> go t2
<span class="ot">&gt;</span>     go (<span class="dt">Column</span> fi) <span class="fu">=</span>
<span class="ot">&gt;</span>         [(fieldName fi, fieldShow <span class="fu">$</span> fieldExtract fi x)]</code></pre>
<p>The deserialization method is the hardest. It works by actually <em>evaluating</em> the tree we built. Because of our GADT use, we can do this in a type-safe way.</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; fromRecord ::</span> forall t<span class="fu">.</span> <span class="dt">Table</span> t t <span class="ot">-&gt;</span> [(<span class="dt">String</span>, <span class="dt">String</span>)] <span class="ot">-&gt;</span> t
<span class="ot">&gt;</span> fromRecord tab record <span class="fu">=</span> go tab
<span class="ot">&gt;</span>   <span class="kw">where</span>
<span class="ot">&gt;     go ::</span> forall a<span class="fu">.</span> <span class="dt">Table</span> t a <span class="ot">-&gt;</span> a
<span class="ot">&gt;</span>     go (<span class="dt">Map</span> f t)   <span class="fu">=</span> f (go t)
<span class="ot">&gt;</span>     go (<span class="dt">Pure</span> x)    <span class="fu">=</span> x
<span class="ot">&gt;</span>     go (<span class="dt">App</span> ft t)  <span class="fu">=</span> (go ft) (go t)
<span class="ot">&gt;</span>     go (<span class="dt">Column</span> fi) <span class="fu">=</span> <span class="kw">case</span> <span class="fu">lookup</span> (fieldName fi) record <span class="kw">of</span>
<span class="ot">&gt;</span>         <span class="kw">Nothing</span>  <span class="ot">-&gt;</span> <span class="fu">error</span> <span class="fu">$</span> <span class="st">&quot;Missing field: &quot;</span> <span class="fu">++</span> fieldName fi
<span class="ot">&gt;</span>         <span class="kw">Just</span> str <span class="ot">-&gt;</span> fieldRead str</code></pre>
<h1 id="utilities">Utilities</h1>
<p>We add a small utility function to pretty-print our records:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; printRecord ::</span> [(<span class="dt">String</span>, <span class="dt">String</span>)] <span class="ot">-&gt;</span> <span class="dt">IO</span> ()
<span class="ot">&gt;</span> printRecord <span class="fu">=</span> <span class="fu">putStrLn</span> <span class="fu">.</span> <span class="fu">unlines</span> <span class="fu">.</span> <span class="fu">map</span> (\(x, y) <span class="ot">-&gt;</span> x <span class="fu">++</span> <span class="st">&quot; = &quot;</span> <span class="fu">++</span> y)</code></pre>
<p>And a typeclass so we do not have to create an <code>xxxTable</code> function for each datatype:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">class</span> <span class="dt">HasTable</span> t <span class="kw">where</span>
<span class="ot">&gt;     table ::</span> <span class="dt">Table</span> t t</code></pre>
<h1 id="demo">Demo</h1>
<p>Now let’s implement food serialization for real: we only need to implement a <em>single</em> typeclass with a <em>single</em> method!</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">instance</span> <span class="dt">HasTable</span> <span class="dt">Food</span> <span class="kw">where</span>
<span class="ot">&gt;</span>     table <span class="fu">=</span> <span class="dt">Food</span>
<span class="ot">&gt;</span>         <span class="fu">&lt;$&gt;</span> column <span class="st">&quot;name&quot;</span> foodName
<span class="ot">&gt;</span>         <span class="fu">&lt;*&gt;</span> column <span class="st">&quot;cost&quot;</span> foodCost</code></pre>
<p>Some concrete deliciousness:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; ramen ::</span> <span class="dt">Food</span>
<span class="ot">&gt;</span> ramen <span class="fu">=</span> <span class="dt">Food</span> <span class="st">&quot;ラーメン&quot;</span> <span class="dv">800</span></code></pre>
<p>And a trivial demo:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; main ::</span> <span class="dt">IO</span> ()
<span class="ot">&gt;</span> main <span class="fu">=</span> <span class="kw">do</span>
<span class="ot">&gt;</span>     <span class="fu">putStrLn</span> <span class="st">&quot;Meta record (used in CREATE TABLE... etc.):&quot;</span>
<span class="ot">&gt;</span>     printRecord <span class="fu">$</span> metaRecord (<span class="ot">table ::</span> <span class="dt">Table</span> <span class="dt">Food</span> <span class="dt">Food</span>)
<span class="ot">&gt;</span> 
<span class="ot">&gt;</span>     <span class="fu">putStrLn</span> <span class="st">&quot;Serialized ramen:&quot;</span>
<span class="ot">&gt;</span>     printRecord (toRecord table ramen)
<span class="ot">&gt;</span> 
<span class="ot">&gt;</span>     <span class="fu">putStrLn</span> <span class="st">&quot;Deserialized sashimi:&quot;</span>
<span class="ot">&gt;</span>     <span class="fu">print</span> (fromRecord table [(<span class="st">&quot;name&quot;</span>, <span class="st">&quot;刺身&quot;</span>), (<span class="st">&quot;cost&quot;</span>, <span class="st">&quot;1200&quot;</span>)]<span class="ot"> ::</span> <span class="dt">Food</span>)</code></pre>
<h1 id="conclusion">Conclusion</h1>
<p>I think this is a nice use case of GADTs, and although it has a performance impact, it looks like it is worth it (for now). If it turns out that we can nicely generalize this code, we will probably release it on Hackage. But feel free to steal the idea, and comments are also welcome, of course!</p>
<p>Exercise for the reader: can you make <code>Table</code> a <code>Monad</code>? Why (not)?</p>]]></summary>
</entry>
<entry>
    <title>blaze-html 0.5</title>
    <link href="http://jaspervdj.be/posts/2012-04-21-blaze-html-0.5.html" />
    <id>http://jaspervdj.be/posts/2012-04-21-blaze-html-0.5.html</id>
    <published>2012-04-21T00:00:00Z</published>
    <updated>2012-04-21T00:00:00Z</updated>
    <summary type="html"><![CDATA[<p>After some work here at <a href="http://www.haskell.org/haskellwiki/DHD_UHac">UHac</a>, I’m releasing a new version of the <a href="http://jaspervdj.be/blaze">blaze-html</a> library. It has a number of changes, some of which slightly break compatibility with older versions of the library.</p>
<h1 id="blaze-markup-and-blaze-svg">blaze-markup and blaze-svg</h1>
<p>A few weeks ago, <a href="http://www.deepak.jois.name/">Deepak Jois</a> emailed me that he would like to create a <code>blaze-html</code> package for <a href="http://nl.wikipedia.org/wiki/Scalable_Vector_Graphics">SVG</a>. He started doing so by first simply copying the <code>blaze-html</code> code and creating <a href="https://github.com/deepakjois/blaze-svg">blaze-svg</a> from there.</p>
<p>Obviously, <code>blaze-html</code> and <code>blaze-svg</code> contained <em>a lot</em> of common code, so we (mostly Deepak, he really deserves all credit for this) extracted the shared code into a new <a href="https://github.com/jaspervdj/blaze-markup">blaze-markup</a> package, upon which both packages now depend.</p>
<h1 id="backwards-incompatible-changes">Backwards-incompatible changes</h1>
<p>This move introduced some backwards-incompatible changes, which we catch with deprecation warnings in a few places, but there have also been some module changes, which might break your code when you upgrade.</p>
<p>The <code>Text.Blaze</code> module no longer contains an <code>Html</code> type. Instead, it has a <code>Markup</code> type. However, in <code>Text.Blaze.Html</code>, we have</p>
<pre class="sourceCode haskell"><code class="sourceCode haskell"><span class="kw">type</span> <span class="dt">Html</span> <span class="fu">=</span> <span class="dt">Markup</span></code></pre>
<p>so the best way to upgrade is to change every <code>Text.Blaze</code> import into a <code>Text.Blaze.Html</code> import.</p>
<p>Additionally, the <code>renderHtml</code> methods have been renamed to <code>renderMarkup</code>. When you use a <code>renderHtml</code> method, you will see a deprecation warning. The best way to resolve this warning is to import e.g., <code>Text.Blaze.Html.Renderer.Utf8</code> instead of <code>Text.Blaze.Renderer.Utf8</code>.</p>
<h1 id="easier-creation-of-custom-nodes">Easier creation of custom nodes</h1>
<p><code>blaze-html</code> 0.5 introduces easy creation of custom nodes. In the following snippet, <code>test1</code> and <code>test2</code> generate the same HTML:</p>
<pre class="sourceCode haskell"><code class="sourceCode haskell"><span class="ot">{-# LANGUAGE OverloadedStrings #-}</span>
<span class="kw">import</span> Text.Blaze.Html (Html)
<span class="kw">import</span> Text.Blaze.Html5 (p)
<span class="kw">import</span> Text.Blaze.Internal

<span class="ot">test1 ::</span> <span class="dt">Html</span>
test1 <span class="fu">=</span> p <span class="st">&quot;Hello world&quot;</span>

<span class="ot">test2 ::</span> <span class="dt">Html</span>
test2 <span class="fu">=</span> customParent <span class="st">&quot;p&quot;</span> <span class="st">&quot;Hello world&quot;</span></code></pre>
<p>However, you should void usage of <code>customParent</code> (and <code>customLeaf</code>, <code>customAttribute</code>) whenever possible, which is why they are placed in the <code>Text.Blaze.Internal</code> module. They are <em>much</em> slower than their regular counterparts, and also less safe since they’re vulnerable to typos.</p>
<h1 id="simple-manipulations-of-the-html-tree">Simple manipulations of the HTML tree</h1>
<p>I’ve also decided I will add some very simple manipulations of the HTML tree when needed. For now, a single manipulation <code>contents</code> is included, since another package (to be released in the future) depends on it.</p>
<p>This very fast function takes the text content of the HTML tree, so it is available for rendering. Let’s look at a simple example:</p>
<pre class="sourceCode haskell"><code class="sourceCode haskell"><span class="ot">{-# LANGUAGE OverloadedStrings #-}</span>
<span class="kw">import</span> Text.Blaze.Html (Html, contents, (!))
<span class="kw">import</span> Text.Blaze.Html.Renderer.Utf8 (renderHtml)
<span class="kw">import</span> <span class="kw">qualified</span> Text.Blaze.Html5 <span class="kw">as</span> H
<span class="kw">import</span> <span class="kw">qualified</span> Text.Blaze.Html5.Attributes <span class="kw">as</span> A

<span class="ot">test ::</span> <span class="dt">Html</span>
test <span class="fu">=</span> <span class="kw">do</span>
    H.p <span class="st">&quot;Hello &quot;</span>
    H.img <span class="fu">!</span> A.src <span class="st">&quot;foo.png&quot;</span>
    H.p <span class="st">&quot;World!&quot;</span>

<span class="co">-- Renders: &lt;p&gt;Hello &lt;/p&gt;&lt;img src=\&quot;foo.png\&quot;&gt;&lt;p&gt;World!&lt;/p&gt;</span>
test1 <span class="fu">=</span> renderHtml test

<span class="co">-- Renders &quot;Hello World!&quot;</span>
test2 <span class="fu">=</span> renderHtml <span class="fu">$</span> contents test</code></pre>
<h1 id="installation">Installation</h1>
<p>You can install the new version just by running</p>
<pre><code>cabal update &amp;&amp; cabal install blaze-html</code></pre>
<p>You should definitely contact me if you run into any trouble, I’ll try to get back to you as fast as possible. In the meanwhile, I’m going to enjoy the rest of the Utrecht Hackathon!</p>]]></summary>
</entry>
<entry>
    <title>A bit of ST and a bit of IO: Designing a DCPU-16 emulator</title>
    <link href="http://jaspervdj.be/posts/2012-04-12-st-io-dcpu-16.html" />
    <id>http://jaspervdj.be/posts/2012-04-12-st-io-dcpu-16.html</id>
    <published>2012-04-12T00:00:00Z</published>
    <updated>2012-04-12T00:00:00Z</updated>
    <summary type="html"><![CDATA[<p>When <a href="http://www.mojang.com/notch/">Notch</a> announced that his upcoming game <a href="http://0x10c.com/">0x10c</a> would feature an in-game <a href="http://0x10c.com/doc/dcpu-16.txt">16 bit CPU</a>, the internet <a href="https://github.com/blog/1098-take-over-the-galaxy-with-github">went nuts</a> about it.</p>
<p>At this point, it seems that every self-respecting hacker has at least already implemented an optimizing assembler, a dissassembler, an emulator, a debugger, or even an <a href="https://github.com/jdiez17/0x42c/">operating system</a> or an <a href="https://github.com/krasin/llvm-dcpu16/">LLVM backend</a>.</p>
<p>I obviously couldn’t lag behind and took the opportunity to think about the design of such an <a href="https://github.com/jaspervdj/dcpu16-hs">emulator</a> in <a href="http://www.haskell.org/">Haskell</a>. This blogpost is the result of my search, and my experiments with different designs until I arrived at something which had all the nice properties I desired.</p>
<p>The full implementation of this design can be found <a href="https://github.com/jaspervdj/dcpu16-hs">here</a>.</p>
<h1 id="the-core-of-the-problem-load-and-store">The core of the problem: load and store</h1>
<p>When designing a system, it’s often a good idea to start from a minimal but functional core and work your way up from there – this is also the approach I have taken here.</p>
<p>We can create such a core system by defining the state of the emulator as a datatype with a few primitive operations on it. The state of the emulator can be represented as a <code>Memory</code> datatype. It’s a tiny wrapper around a mutable array, and has three basic operations:</p>
<pre class="sourceCode haskell"><code class="sourceCode haskell"><span class="kw">module</span> <span class="dt">Memory</span> <span class="kw">where</span>

<span class="kw">data</span> <span class="dt">Address</span>
    <span class="fu">=</span> <span class="dt">Pc</span>
    <span class="fu">|</span> <span class="dt">Sp</span>
    <span class="fu">|</span> <span class="dt">Ram</span> <span class="dt">Word16</span>
    <span class="co">-- And more...</span>

<span class="kw">data</span> <span class="dt">Memory</span> s <span class="fu">=</span> <span class="dt">Memory</span>  <span class="co">-- Exact definition omitted</span>

<span class="ot">new   ::</span> <span class="dt">ST</span> s (<span class="dt">Memory</span> s)
<span class="ot">load  ::</span> <span class="dt">Memory</span> s <span class="ot">-&gt;</span> <span class="dt">Address</span> <span class="ot">-&gt;</span> <span class="dt">ST</span> s <span class="dt">Word16</span>
<span class="ot">store ::</span> <span class="dt">Memory</span> s <span class="ot">-&gt;</span> <span class="dt">Address</span> <span class="ot">-&gt;</span> <span class="dt">Word16</span> <span class="ot">-&gt;</span> <span class="dt">ST</span> s ()</code></pre>
<p>Using the above <code>Address</code> datatype, we’re able to access and modify all of the values the emulator needs using two simple operations: <code>load</code> and <code>store</code>. The fact that we can access and modify, for example, the stack pointer (<code>Sp</code>) in the same way as we would modify a value in the RAM (<code>Ram 0x1000</code>) simplifies the implementation of the actual emulator. As an example, think of code like this:</p>
<pre class="sourceCode nasm"><code class="sourceCode nasm">SET <span class="kw">SP</span>, I
SET [<span class="bn">0x1000</span>], <span class="kw">SP</span></code></pre>
<p>This all runs in the <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/Control-Monad-ST.html">ST</a> monad. This monad is perhaps a bit of a strange beast, since it allows destructive updates: this means we can update words in the RAM of the CPU really fast. However, the <em>result remains pure</em> nonetheless: unlike the IO monad, code written in the ST monad can only modify state internal to the monad. This is guaranteed by the type system using the <code>Rank2Types</code> extension.</p>
<p>This is great because it allows us to have a really fast emulator, of which we can trivially say it is deterministic.</p>
<h1 id="a-necessary-abstraction">A necessary abstraction</h1>
<p>However, while the current specification does not include keyboard input and video output, this will certainly be added in the future. If we want to support that in our emulator, does it mean that we will eventually be forced to run our code in the <code>IO</code> monad, and that our brave efforts to build a deterministic emulator have been in vain?</p>
<p>Not exactly. Using a typeclass, it is possible to build an extremely useful abstraction layer which helps us with this problem.</p>
<pre class="sourceCode haskell"><code class="sourceCode haskell"><span class="kw">module</span> <span class="dt">Emulator.Monad</span> <span class="kw">where</span>

<span class="kw">class</span> <span class="kw">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">MonadEmulator</span> m <span class="kw">where</span>
<span class="ot">    load  ::</span> <span class="dt">Address</span> <span class="ot">-&gt;</span> m <span class="dt">Word16</span>
<span class="ot">    store ::</span> <span class="dt">Address</span> <span class="ot">-&gt;</span> <span class="dt">Word16</span> <span class="ot">-&gt;</span> m ()</code></pre>
<p>We can now implement most of the emulator with just the type <code>MonadEmulator m =&gt; m ()</code>, since we really only need <code>load</code> and <code>store</code>.</p>
<p>The following (overly simplified and incorrect) snippet illustrates that fact.</p>
<pre class="sourceCode haskell"><code class="sourceCode haskell"><span class="kw">module</span> <span class="dt">Emulator</span> <span class="kw">where</span>

<span class="kw">import</span> Emulator.Monad

<span class="ot">step ::</span> <span class="dt">MonadEmulator</span> m <span class="ot">=&gt;</span> m ()
step <span class="fu">=</span> <span class="kw">do</span>
    <span class="co">-- Load the next instruction (at PC) and execute it</span>
    pc    <span class="ot">&lt;-</span> load <span class="dt">Pc</span> 
    instr <span class="ot">&lt;-</span> load (<span class="dt">Ram</span> pc)
    store <span class="dt">Pc</span> (pc <span class="fu">+</span> <span class="dv">1</span>)
    execute instr</code></pre>
<h1 id="a-deterministic-implementation">A deterministic implementation</h1>
<p>We can write a simple, nice and pure implementation of this typeclass by using <code>ST</code>, as we previously discussed. We need access to the <code>Memory</code>, and for this purpose we can simply wrap a <code>ReaderT</code> around <code>ST</code>:</p>
<pre class="sourceCode haskell"><code class="sourceCode haskell"><span class="kw">module</span> <span class="dt">Emulator.Monad.ST</span> <span class="kw">where</span>

<span class="kw">import</span> Emulator.Monad
<span class="kw">import</span> Memory (Memory)
<span class="kw">import</span> <span class="kw">qualified</span> Memory <span class="kw">as</span> Memory

<span class="kw">newtype</span> <span class="dt">STEmulator</span> s a <span class="fu">=</span> <span class="dt">STEmulator</span> (<span class="dt">ReaderT</span> (<span class="dt">Memory</span> s) (<span class="dt">ST</span> s) a)
    <span class="kw">deriving</span> (<span class="kw">Monad</span>)

<span class="kw">instance</span> <span class="dt">MonadEmulator</span> (<span class="dt">STEmulator</span> s) <span class="kw">where</span>
    load address <span class="fu">=</span> <span class="dt">STEmulator</span> <span class="fu">$</span> <span class="kw">do</span>
        mem <span class="ot">&lt;-</span> ask
        lift <span class="fu">$</span> Memory.load mem address
    store address word <span class="fu">=</span> <span class="dt">STEmulator</span> <span class="fu">$</span> <span class="kw">do</span>
        mem <span class="ot">&lt;-</span> ask
        lift <span class="fu">$</span> Memory.store mem address word

<span class="ot">runSTEmulator ::</span> (forall s<span class="fu">.</span> <span class="dt">STEmulator</span> s a) <span class="ot">-&gt;</span> a
<span class="co">-- Implementation uses runST to produce a pure result:</span>
<span class="co">-- runST :: (forall s. ST s a) -&gt; a</span></code></pre>
<h1 id="and-a-bit-of-io">And a bit of IO</h1>
<p>As briefly discussed before, we want to be able to add keyboard input and video output. I’ll focus on video output for this blogpost, since it’s a little easier to explain. The specification for video output is not yet released, but it was possible to deduce a lot from some <a href="http://i.imgur.com/XIXc4.jpg">screenshots</a>.</p>
<p>The video memory starts at <code>0x8000</code>, and when we store characters at these addresses, they are displayed on the video terminal. Note that we don’t need to extend the <code>MonadEmulator</code> typeclass for this purpose: we already support writing values to <code>0x8000 + i</code>, because we can reach those addresses through the regular <code>store</code> operation, discussed above!</p>
<p>But actually displaying the video terminal is something we can’t do with our <code>STEmulator</code> – let’s add an <code>IOEmulator</code> as well.</p>
<p>The <code>stToIO</code> function from <code>Control.Monad.ST</code> allows us to reuse everything in the <code>Memory</code> module.</p>
<pre class="sourceCode haskell"><code class="sourceCode haskell"><span class="ot">stToIO ::</span> <span class="dt">ST</span> <span class="dt">RealWorld</span> a <span class="ot">-&gt;</span> <span class="dt">IO</span> a</code></pre>
<p>Note that this is <em>not</em> an unsafe function: converting <code>ST</code> to <code>IO</code> is always safe this way (converting <code>IO</code> to <code>ST</code>, however, is tricky business, but this is not needed for our emulator).</p>
<p>Almost exactly like our <code>STEmulator</code> is a <code>ReaderT</code> wrapper around <code>ST</code>, our <code>IOEmulator</code> starts out like a <code>ReaderT</code> wrapper around <code>IO</code>.</p>
<pre class="sourceCode haskell"><code class="sourceCode haskell"><span class="kw">module</span> <span class="dt">Emulator.Monad.IO</span> <span class="kw">where</span>

<span class="kw">import</span> Emulator.Monad
<span class="kw">import</span> Memory (Memory)
<span class="kw">import</span> <span class="kw">qualified</span> Memory <span class="kw">as</span> Memory

<span class="kw">newtype</span> <span class="dt">IOEmulator</span> a <span class="fu">=</span> <span class="dt">IOEmulator</span> (<span class="dt">ReaderT</span> (<span class="dt">Memory</span> <span class="dt">RealWorld</span>) <span class="dt">IO</span> a)
    <span class="kw">deriving</span> (<span class="kw">Monad</span>, <span class="dt">MonadIO</span>)

<span class="kw">instance</span> <span class="dt">MonadEmulator</span> <span class="dt">IOEmulator</span> <span class="kw">where</span>
    load address <span class="fu">=</span> <span class="dt">IOEmulator</span> <span class="fu">$</span> <span class="kw">do</span>
        mem <span class="ot">&lt;-</span> ask
        lift <span class="fu">$</span> stToIO <span class="fu">$</span> Memory.load mem address
    store address word <span class="fu">=</span> <span class="dt">IOEmulator</span> <span class="fu">$</span> <span class="kw">do</span>
        mem <span class="ot">&lt;-</span> ask
        lift <span class="fu">$</span> stToIO <span class="fu">$</span> Memory.store mem address word

<span class="ot">runIOEmulator ::</span> <span class="dt">IOEmulator</span> a <span class="ot">-&gt;</span> <span class="dt">IO</span> a
<span class="co">-- Implementation trivial, omitted</span></code></pre>
<p>If we now have some code for an emulator, we can run it in two ways:</p>
<pre class="sourceCode haskell"><code class="sourceCode haskell"><span class="co">-- Load a program, execute 1000 instructions, return the value in the RAM at</span>
<span class="co">-- position 0x1000.</span>
<span class="ot">test ::</span> <span class="dt">MonadEmulator</span> m <span class="ot">=&gt;</span> m <span class="dt">Word16</span>
test <span class="fu">=</span> <span class="kw">do</span>
    loadProgram [<span class="dv">0xa461</span>, <span class="dv">0x7001</span>, <span class="dv">0x8403</span> <span class="ot">{-# and more... #-}</span>]
    replicateM_ <span class="dv">1000</span> step
    load (<span class="dt">Ram</span> <span class="dv">0x1000</span>)

<span class="ot">t1 ::</span> <span class="dt">Word16</span>
t1 <span class="fu">=</span> runSTEmulator test

<span class="ot">t2 ::</span> <span class="dt">IO</span> <span class="dt">Word16</span>
t2 <span class="fu">=</span> runIOEmulator test</code></pre>
<p>Let’s not stop here yet, because with our current version of <code>IOEmulator</code>, all we’ve done is create an <em>impure</em> version of <code>STEmulator</code>, without any extra features!</p>
<p>Adding the desired features is relatively easy at this point: suppose we want to add support for the video terminal. We just modify the <code>store</code> implementation of <code>IOEmulator</code> a little:</p>
<pre class="sourceCode haskell"><code class="sourceCode haskell">store address word <span class="fu">=</span> <span class="dt">IOEmulator</span> <span class="fu">$</span> <span class="kw">do</span>
    mem <span class="ot">&lt;-</span> ask
    lift <span class="fu">$</span> stToIO <span class="fu">$</span> Memory.store mem address word
    lift <span class="fu">$</span> video address word  <span class="co">-- New line added here</span></code></pre>
<p>And a video terminal implementation!</p>
<pre class="sourceCode haskell"><code class="sourceCode haskell"><span class="ot">video ::</span> <span class="dt">Address</span> <span class="ot">-&gt;</span> <span class="dt">Word16</span> <span class="ot">-&gt;</span> <span class="dt">IO</span> ()
video (<span class="dt">Ram</span> address) word
    <span class="fu">|</span> address <span class="fu">&lt;</span> videoStart <span class="fu">=</span> <span class="fu">return</span> ()
    <span class="fu">|</span> address <span class="fu">&gt;=</span> videoEnd  <span class="fu">=</span> <span class="fu">return</span> ()
    <span class="fu">|</span> <span class="fu">otherwise</span>            <span class="fu">=</span> <span class="kw">do</span>
        <span class="co">-- Calculate row, column, character code and draw!</span>
        <span class="fu">return</span> ()
  <span class="kw">where</span>
    videoStart <span class="fu">=</span> <span class="dv">0x8000</span>
    videoEnd   <span class="fu">=</span> videoStart <span class="fu">+</span> rows <span class="fu">*</span> columns  <span class="co">-- Unknown yet?</span>
video _ _ <span class="fu">=</span> <span class="fu">return</span> ()</code></pre>
<h1 id="conclusion">Conclusion</h1>
<p><code>ST</code> is a wonderful tool for problems that require (local) destructive updates for performance reasons <sup><a href="#fn1" class="footnoteRef" id="fnref1">1</a></sup>. Additionally, we can easily convert it to <code>IO</code>, which allows us to simply pick one of the two using an abstraction layer: we can have deterministic results for tests, proper keyboard/video support, and the entire implementation of the actual CPU is shared code.</p>
<p>My thanks to <a href="http://www.itkovian.net/">Andy Georges</a> and <a href="http://twitter.com/nudded">Toon Willems</a> for proofreading and some corrections.</p>
<div class="footnotes">
<hr />
<ol>
<li id="fn1"><p>Although other, purely functional representations are possible as well, of course. These are however unlikely to match the speed of this solution.<a href="#fnref1">↩</a></p></li>
</ol>
</div>]]></summary>
</entry>
<entry>
    <title>digestive-functors for Snap users</title>
    <link href="http://jaspervdj.be/posts/2012-04-03-digestive-functors-for-snap-users.html" />
    <id>http://jaspervdj.be/posts/2012-04-03-digestive-functors-for-snap-users.html</id>
    <published>2012-04-03T00:00:00Z</published>
    <updated>2012-04-03T00:00:00Z</updated>
    <summary type="html"><![CDATA[<p>The past week there’s been a bit of work done on better integration between the <a href="https://github.com/jaspervdj/digestive-functors/">digestive-functors</a> library and the <a href="http://snapframework.com/">Snap framework</a>.</p>
<p><a href="http://dbpatterson.com/">Daniel Patterson</a> has been so kind to implement file uploads for the Snap backend of digestive-functors – this was previously only implemented in the <a href="http://happstack.com/">Happstack</a>. The great thing is that this implementation still has all flexible options Snap allows for file uploads.</p>
<p>For now, digestive-functors enforced you to use the <a href="http://jaspervdj.be/blaze">blaze-html</a> library, simply because no other frontend library was available. Snap users will be delighted to hear we’ve written a <a href="http://snapframework.com/docs/tutorials/heist">Heist</a> library now. Let’s take a look at an example. I’ll go over the forms rather quickly and focus on the Heist library. If this is the first time you read about digestive-functors, you might want to take a glance at <a href="https://github.com/jaspervdj/digestive-functors/blob/master/examples/tutorial.lhs">this tutorial</a> first.</p>
<p>First, we need some datatypes:</p>
<pre class="sourceCode haskell"><code class="sourceCode haskell"><span class="kw">data</span> <span class="dt">Date</span> <span class="fu">=</span> <span class="dt">Date</span>
    {<span class="ot"> dateDay   ::</span> <span class="dt">Int</span>
    ,<span class="ot"> dateMonth ::</span> <span class="dt">Int</span>
    ,<span class="ot"> dateYear  ::</span> <span class="dt">Int</span>
    } <span class="kw">deriving</span> (<span class="kw">Show</span>)

<span class="kw">data</span> <span class="dt">Sex</span> <span class="fu">=</span> <span class="dt">Female</span> <span class="fu">|</span> <span class="dt">Male</span>
    <span class="kw">deriving</span> (<span class="kw">Eq</span>, <span class="kw">Show</span>)

<span class="kw">data</span> <span class="dt">User</span> <span class="fu">=</span> <span class="dt">User</span>
    {<span class="ot"> userName      ::</span> <span class="dt">Text</span>
    ,<span class="ot"> userPassword  ::</span> <span class="dt">Text</span>
    ,<span class="ot"> userSex       ::</span> <span class="dt">Sex</span>
    ,<span class="ot"> userBirthdate ::</span> <span class="dt">Date</span>
    } <span class="kw">deriving</span> (<span class="kw">Show</span>)</code></pre>
<p>And then some forms:</p>
<pre class="sourceCode haskell"><code class="sourceCode haskell"><span class="ot">dateForm ::</span> <span class="kw">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Form</span> <span class="dt">Text</span> m <span class="dt">Date</span>
dateForm <span class="fu">=</span> check <span class="st">&quot;Not a valid date&quot;</span> validDate <span class="fu">$</span> <span class="dt">Date</span>
    <span class="fu">&lt;$&gt;</span> <span class="st">&quot;day&quot;</span>   <span class="fu">.:</span> stringRead <span class="st">&quot;Not a number&quot;</span> (<span class="kw">Just</span> <span class="dv">16</span>)
    <span class="fu">&lt;*&gt;</span> <span class="st">&quot;month&quot;</span> <span class="fu">.:</span> stringRead <span class="st">&quot;Not a number&quot;</span> (<span class="kw">Just</span> <span class="dv">6</span>)
    <span class="fu">&lt;*&gt;</span> <span class="st">&quot;year&quot;</span>  <span class="fu">.:</span> stringRead <span class="st">&quot;Not a number&quot;</span> (<span class="kw">Just</span> <span class="dv">1990</span>)
  <span class="kw">where</span>
    validDate (<span class="dt">Date</span> day month _) <span class="fu">=</span>
        day   <span class="fu">&gt;=</span> <span class="dv">1</span> <span class="fu">&amp;&amp;</span> day   <span class="fu">&lt;=</span> <span class="dv">31</span> <span class="fu">&amp;&amp;</span>
        month <span class="fu">&gt;=</span> <span class="dv">1</span> <span class="fu">&amp;&amp;</span> month <span class="fu">&lt;=</span> <span class="dv">12</span>

<span class="ot">userForm ::</span> <span class="kw">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Form</span> <span class="dt">Text</span> m <span class="dt">User</span>
userForm <span class="fu">=</span> <span class="dt">User</span>
    <span class="fu">&lt;$&gt;</span> <span class="st">&quot;name&quot;</span>      <span class="fu">.:</span> text (<span class="kw">Just</span> <span class="st">&quot;Jasper&quot;</span>)
    <span class="fu">&lt;*&gt;</span> <span class="st">&quot;password&quot;</span>  <span class="fu">.:</span> text <span class="kw">Nothing</span>
    <span class="fu">&lt;*&gt;</span> <span class="st">&quot;sex&quot;</span>       <span class="fu">.:</span> choice [(<span class="dt">Female</span>, <span class="st">&quot;Female&quot;</span>), (<span class="dt">Male</span>, <span class="st">&quot;Male&quot;</span>)] <span class="kw">Nothing</span>
    <span class="fu">&lt;*&gt;</span> <span class="st">&quot;birthdate&quot;</span> <span class="fu">.:</span> dateForm</code></pre>
<p>Because of the composable nature of digestive-functors, we will first write a template for the date form, and reuse that for the user form.</p>
<p>Let’s place the template for the date form in <code>snaplets/heist/date-form.tpl</code>. Almost all splices take a <code>ref</code> attribute, which tells the library what the field is for – by doing this, we automatically get the previous value if a form submission fails, etc. We can also easily pass arbitrary attributes to the input elements, like we do with <code>size</code> here.</p>
<pre class="sourceCode html"><code class="sourceCode html"><span class="kw">&lt;dfInputText</span><span class="ot"> ref=</span><span class="st">&quot;day&quot;</span><span class="ot"> size=</span><span class="st">&quot;2&quot;</span> <span class="kw">/&gt;</span>
/
<span class="kw">&lt;dfInputText</span><span class="ot"> ref=</span><span class="st">&quot;month&quot;</span><span class="ot"> size=</span><span class="st">&quot;2&quot;</span> <span class="kw">/&gt;</span>
/
<span class="kw">&lt;dfInputText</span><span class="ot"> ref=</span><span class="st">&quot;year&quot;</span><span class="ot"> size=</span><span class="st">&quot;4&quot;</span> <span class="kw">/&gt;</span></code></pre>
<p>There’s a bit more in <code>snaplets/heist/user-form.tpl</code>.</p>
<ul>
<li><p><code>dfForm</code> generates a <code>&lt;form&gt;</code> tag, and takes care of the <code>method</code> and <code>enctype</code> attributes for us – we just have to take care of <code>action</code>.</p></li>
<li><p>We use <code>dfChildErrorList</code> with <code>ref=&quot;&quot;</code> to generate a list of <em>all</em> errors. It is also possible to generate the errors next to the relevant fields, should you wish to do so.</p></li>
<li><p>There’s obviously more than <code>dfInputText</code> – we have <code>dfInputPassword</code> here, and <code>dfInputSelect</code> to generate a combobox.</p></li>
<li><p>In order to reuse the <code>date-form</code> template, we first use <code>dfSubView</code>. This changes the focus on the “form tree”, so our <code>date-form</code> template can use <code>month</code> instead of <code>birthdate.month</code>. The <code>apply</code> splice is a standard Heist splice.</p></li>
</ul>
<pre class="sourceCode html"><code class="sourceCode html"><span class="kw">&lt;dfForm</span><span class="ot"> action=</span><span class="st">&quot;/&quot;</span><span class="kw">&gt;</span>
    <span class="kw">&lt;dfChildErrorList</span><span class="ot"> ref=</span><span class="st">&quot;&quot;</span> <span class="kw">/&gt;</span>

    <span class="kw">&lt;dfLabel</span><span class="ot"> ref=</span><span class="st">&quot;name&quot;</span><span class="kw">&gt;</span>Name: <span class="kw">&lt;/dfLabel&gt;</span>
    <span class="kw">&lt;dfInputText</span><span class="ot"> ref=</span><span class="st">&quot;name&quot;</span> <span class="kw">/&gt;</span>
    <span class="kw">&lt;br&gt;</span>

    <span class="kw">&lt;dfLabel</span><span class="ot"> ref=</span><span class="st">&quot;password&quot;</span><span class="kw">&gt;</span>Password: <span class="kw">&lt;/dfLabel&gt;</span>
    <span class="kw">&lt;dfInputPassword</span><span class="ot"> ref=</span><span class="st">&quot;password&quot;</span> <span class="kw">/&gt;</span>
    <span class="kw">&lt;br&gt;</span>

    <span class="kw">&lt;dfLabel</span><span class="ot"> ref=</span><span class="st">&quot;sex&quot;</span><span class="kw">&gt;</span>Sex: <span class="kw">&lt;/dfLabel&gt;</span>
    <span class="kw">&lt;dfInputSelect</span><span class="ot"> ref=</span><span class="st">&quot;sex&quot;</span> <span class="kw">/&gt;</span>
    <span class="kw">&lt;br&gt;</span>

    Birthday:
    <span class="kw">&lt;dfSubView</span><span class="ot"> ref=</span><span class="st">&quot;birthdate&quot;</span><span class="kw">&gt;</span>
        <span class="kw">&lt;apply</span><span class="ot"> template=</span><span class="st">&quot;date-form&quot;</span> <span class="kw">/&gt;</span>
    <span class="kw">&lt;/dfSubView&gt;</span>
    <span class="kw">&lt;br&gt;</span>

    <span class="kw">&lt;dfInputSubmit</span><span class="ot"> value=</span><span class="st">&quot;Enter&quot;</span> <span class="kw">/&gt;</span>
<span class="kw">&lt;/dfForm&gt;</span></code></pre>
<p>The <code>digestive-functors-heist</code> library provides no <a href="http://snapframework.com/docs/tutorials/snaplets-tutorial">Snaplet</a> but integrates well with the concept. We just need to call <code>runForm</code> from the <code>digestive-functors-snap</code> library, and most is taken care of:</p>
<pre class="sourceCode haskell"><code class="sourceCode haskell"><span class="ot">form ::</span> <span class="dt">Handler</span> <span class="dt">App</span> <span class="dt">App</span> ()
form <span class="fu">=</span> <span class="kw">do</span>
    (view, result) <span class="ot">&lt;-</span> runForm <span class="st">&quot;form&quot;</span> userForm
    <span class="kw">case</span> result <span class="kw">of</span>
        <span class="kw">Just</span> x  <span class="ot">-&gt;</span> <span class="co">-- do something with the user in x...</span>
        <span class="kw">Nothing</span> <span class="ot">-&gt;</span> heistLocal (bindDigestiveSplices view) <span class="fu">$</span> render <span class="st">&quot;user-form&quot;</span></code></pre>
<p>I hope this quick glance at the library was useful. Any comments and criticism are welcome, as always. The full source code for this example can be found <a href="https://github.com/jaspervdj/digestive-functors/tree/master/examples">here</a> (<code>snap-heist.hs</code>, and the templates in <code>snaplets/heist/templates</code>).</p>]]></summary>
</entry>
<entry>
    <title>digestive-functors 0.3</title>
    <link href="http://jaspervdj.be/posts/2012-03-21-digestive-functors-0.3.html" />
    <id>http://jaspervdj.be/posts/2012-03-21-digestive-functors-0.3.html</id>
    <published>2012-03-21T00:00:00Z</published>
    <updated>2012-03-21T00:00:00Z</updated>
    <summary type="html"><![CDATA[<p>I’ve just released digestive-functors 0.3, which is a major rehaul of my formlets library. It has a number of great features which (as far as I know) have never been implemented for any formlets library.</p>
<p>This blogpost is very general, so some users might want to jump directly to the <a href="http://github.com/jaspervdj/digestive-functors/blob/master/examples/tutorial.lhs">tutorial</a>. Installation is through cabal: <code>cabal install digestive-functors</code>.</p>
<h1 id="what-are-formlets">What are formlets?</h1>
<p>In 2008, a paper was published, called <a href="http://groups.inf.ed.ac.uk/links/formlets/">“The Essence of Form Abstraction”</a>. The paper applied a well-known functional design pattern (Applicative Functors) to have clean way of creating HTML forms, which are <em>inherently composable</em>.</p>
<p>Let’s have a quick look at how this typically works. The following code is based on the <a href="http://hackage.haskell.org/cgi-bin/hackage-scripts/package/formlets">initial Haskell implementation</a> of formlets by the paper authors, later maintained by <a href="http://eidhof.nl/">Chris Eidhof</a> and others. We usually have a data structure we want to create a type for:</p>
<pre class="sourceCode haskell"><code class="sourceCode haskell"><span class="kw">data</span> <span class="dt">Date</span> <span class="fu">=</span> <span class="dt">Date</span> {<span class="ot">month ::</span> <span class="dt">Integer</span>,<span class="ot"> day ::</span> <span class="dt">Integer</span>} <span class="kw">deriving</span> (<span class="kw">Show</span>)</code></pre>
<p>…for which we create a form…</p>
<pre class="sourceCode haskell"><code class="sourceCode haskell"><span class="ot">validDate ::</span> <span class="dt">Date</span> <span class="ot">-&gt;</span> <span class="dt">Bool</span>
validDate (<span class="dt">Date</span> m d) <span class="fu">=</span> m <span class="ot">`elem`</span> [<span class="dv">1</span><span class="fu">..</span><span class="dv">12</span>] <span class="fu">&amp;&amp;</span> d <span class="ot">`elem`</span> [<span class="dv">1</span><span class="fu">..</span><span class="dv">31</span>]

<span class="ot">dateComponent ::</span> <span class="dt">FailingForm</span> <span class="dt">Date</span>
dateComponent <span class="fu">=</span> <span class="dt">Date</span> <span class="fu">&lt;$&gt;</span> inputIntegerF (<span class="kw">Just</span> <span class="dv">1</span>) <span class="fu">&lt;*&gt;</span> inputIntegerF (<span class="kw">Just</span> <span class="dv">16</span>)

<span class="ot">dateFull ::</span> <span class="dt">FailingForm</span> <span class="dt">Date</span>
dateFull <span class="fu">=</span> dateComponent <span class="ot">`check`</span> ensure validDate <span class="st">&quot;This is not a valid date&quot;</span></code></pre>
<p>And to illustrate the power of any formlets library, it’s composability, let’s see how you can easily reuse the <code>dateFull</code> form anywhere:</p>
<pre class="sourceCode haskell"><code class="sourceCode haskell"><span class="kw">data</span> <span class="dt">User</span> <span class="fu">=</span> <span class="dt">User</span> {<span class="ot">name ::</span> <span class="dt">String</span>,<span class="ot"> password ::</span> <span class="dt">String</span>,<span class="ot"> birthdate ::</span> <span class="dt">Date</span>}
    <span class="kw">deriving</span> (<span class="kw">Show</span>)

<span class="ot">userFull ::</span> <span class="dt">FailingForm</span> <span class="dt">User</span>
userFull <span class="fu">=</span> <span class="dt">User</span> <span class="fu">&lt;$&gt;</span> inputF <span class="kw">Nothing</span> <span class="fu">&lt;*&gt;</span> passwordF <span class="kw">Nothing</span> <span class="fu">&lt;*&gt;</span> dateFull</code></pre>
<h1 id="digestive-functors">digestive-functors</h1>
<p>While I was working on a few patches for the formlets library back in 2010, I noticed a few things were impossible to do using this library.</p>
<p>For example, For usability reasons, you might want to add <code>&lt;label&gt;</code> tags in your forms. These labels need to be associated with an input element using the <code>for</code> attribute – when this is the case, the user will be able to click the label instead of the (relatively small) checkbox. Demo:</p>
<form>
    
<label>Without <code>for</code> attribute:</label> <input type="checkbox" id="checkbox1" />
</form>
<form>
    
<label for="checkbox2">With <code>for</code> attribute:</label> <input type="checkbox" id="checkbox2" />
</form>

<p>Another thing is error handling. When evaluating a form using the original formlets library, you would get a list of errors, and the user would see something like this:</p>
<div style="color: red; text-weight: bold">
Cannot parse age
</div>
<form>
    
<label for="text1">Name:</label> <input type="text" id="text1" value="Jasper" />
</form>
<form>
    
<label for="text2">Age:</label> <input type="text" id="text2" value="hello" />
</form>

<p>While this is usually pretty clear, it is nicer when the library can actually associate the errors with input field(s), so you are able to show something like:</p>
<form>
    
<label for="text3">Name:</label> <input type="text" id="text3" value="Jasper" />
</form>
<form>
    
<label for="text4">Age:</label> <input type="text" id="text4" value="hello" /> <span style="color: red; text-weight: bold">Cannot parse age</span>
</form>

<p>Along with some other things, these were the practical improvements the digestive-functors library made in comparison to formlets.</p>
<h1 id="digestive-functors-0.3">digestive-functors 0.3</h1>
<p>However, one serious issue remained. When you write down a form in a formlets library, you specify the <em>HTML layout</em> as well as <em>the validation rules</em>. This is really a bad thing: sepataration of model and view is a well known goal in programming.</p>
<p>Separating the HTML layout and the validation rules would lead to a number of benefits:</p>
<ul>
<li><p>It would be possible to create multiple representations for a single form (a good example is a login form in the site header, and a login form in the page body, which you see find on many websites).</p></li>
<li><p>The code to specify the validation rules becomes smaller and easier to read.</p></li>
<li><p>It becomes easier to insert custom HTML code in between your form HTML.</p></li>
<li><p>You can rewrite the form using another HTML templating engine, e.g. blaze-html, Hamlet or Heist, without touching the validation rules.</p></li>
</ul>
<p>However, it does seem to come with a serious disadvantage as well:</p>
<ul>
<li>It seems impossible <sup><a href="#fn1" class="footnoteRef" id="fnref1">1</a></sup> to have a type-safe coupling between validation rules and HTML layout without losing flexibility or ease-of-use.</li>
</ul>
<p>In order to make the coupling between the validation rules and the HTML layout, digestive-functors-0.3 uses simple <code>Text</code> values. An example of some validation rules:</p>
<pre class="sourceCode haskell"><code class="sourceCode haskell">dateForm <span class="fu">=</span> check <span class="st">&quot;This is not a valid date&quot;</span> validDate <span class="fu">$</span> <span class="dt">Date</span>
    <span class="fu">&lt;$&gt;</span> <span class="st">&quot;month&quot;</span> <span class="fu">.:</span> stringRead <span class="st">&quot;Could not parse month&quot;</span>
    <span class="fu">&lt;*&gt;</span> <span class="st">&quot;day&quot;</span>   <span class="fu">.:</span> stringRead <span class="st">&quot;Could not parse day&quot;</span></code></pre>
<p>A major difference you can immediately notice is that we use <code>Text</code> labels (<code>&quot;month&quot;</code>, <code>&quot;day&quot;</code>) and use the custom <code>.:</code> operator to assign these to parts of our form. We will use these labels to refer to fields in the HTML layout.</p>
<p>Form composition is obviously still very important. Let’s give an example by implemening <code>userForm</code> using <code>dateForm</code>:</p>
<pre class="sourceCode haskell"><code class="sourceCode haskell">userForm <span class="fu">=</span> <span class="dt">User</span>
    <span class="fu">&lt;$&gt;</span> <span class="st">&quot;name&quot;</span>      <span class="fu">.:</span> string <span class="kw">Nothing</span>
    <span class="fu">&lt;*&gt;</span> <span class="st">&quot;password&quot;</span>  <span class="fu">.:</span> string <span class="kw">Nothing</span>
    <span class="fu">&lt;*&gt;</span> <span class="st">&quot;birthdate&quot;</span> <span class="fu">.:</span> dateForm</code></pre>
<p>One important difference between <code>userFull</code> and <code>userForm</code> is that we used <code>string</code> twice here – where the original one used <code>inputF</code> and <code>passwordF</code>. This is a result of the separation of concerns. After all, a password is just a string: it is up to the view to represent it as a password box.</p>
<p>Let’s look at the views now and write an HTML layout using e.g. blaze-html. The code for this is a bit verbose (HTML always is), but clear, and it’s possible to add some utility combinators for it:</p>
<pre class="sourceCode haskell"><code class="sourceCode haskell">dateView view <span class="fu">=</span> <span class="kw">do</span>
    errorList <span class="st">&quot;month&quot;</span> view
    label     <span class="st">&quot;month&quot;</span> view <span class="st">&quot;Month: &quot;</span>
    inputText <span class="st">&quot;month&quot;</span> view
    H.br

    errorList <span class="st">&quot;day&quot;</span> view
    label     <span class="st">&quot;day&quot;</span> view <span class="st">&quot;Day: &quot;</span>
    inputText <span class="st">&quot;day&quot;</span> view
    H.br</code></pre>
<p>Views are composable as well, which is very important. A developer might want to inline a view, e.g.:</p>
<pre class="sourceCode haskell"><code class="sourceCode haskell">userView view <span class="fu">=</span> <span class="kw">do</span>
    errorList <span class="st">&quot;name&quot;</span> view
    label     <span class="st">&quot;name&quot;</span> view <span class="st">&quot;Name: &quot;</span>
    inputText <span class="st">&quot;name&quot;</span> view
    H.br

    errorList     <span class="st">&quot;password&quot;</span> view
    label         <span class="st">&quot;password&quot;</span> view <span class="st">&quot;Password: &quot;</span>
    inputPassword <span class="st">&quot;password&quot;</span> view
    H.br

    errorList <span class="st">&quot;birthdate.month&quot;</span> view
    label     <span class="st">&quot;birthdate.month&quot;</span> view <span class="st">&quot;Month: &quot;</span>
    inputText <span class="st">&quot;birthdate.month&quot;</span> view
    H.br

    errorList <span class="st">&quot;birthdate.day&quot;</span> view
    label     <span class="st">&quot;birthdate.day&quot;</span> view <span class="st">&quot;Day: &quot;</span>
    inputText <span class="st">&quot;birthdate.day&quot;</span> view
    H.br</code></pre>
<p>A few things to note. While <code>&quot;name&quot;</code> and <code>&quot;password&quot;</code> are both of the same type (<code>String</code>) we chose to use a textbox for the former and a password box for the latter: this is a possibility we gain because of the separation we made. A (probably more useful) example is when the user has to choose between a number of options (e.g. Apples, Oranges or Bananas), we can decide in the view code whether we want to use a combobox or a set of radio buttons.</p>
<p>We use a <code>&quot;foo.bar&quot;</code> notation to refer to fields of “subforms”. This is useful when a designer wants a custom form layout, but it leads to duplication of code. To counter this, views are composable, just like forms!</p>
<pre class="sourceCode haskell"><code class="sourceCode haskell">userView view <span class="fu">=</span> <span class="kw">do</span>
    <span class="co">-- Name, password...</span>

    dateView <span class="fu">$</span> subView <span class="st">&quot;birthdate&quot;</span> view</code></pre>
<p>This concludes this blogpost about the digestive-functors 0.3 release.</p>
<p>Note that I have omitted types and other details – you can find everything in this <a href="http://github.com/jaspervdj/digestive-functors/blob/master/examples/tutorial.lhs">tutorial</a>. The digestive-functors library provides a very easy interface for writing these view libraries: you can basically query the previous input, errors, etc. for each field. This makes it very easy to add libraries for e.g. Hamlet or Heist (but I haven’t done so yet, if anyone is interested, contact me!).</p>
<div class="footnotes">
<hr />
<ol>
<li id="fn1"><p>I’ve thought about this for some time, and haven’t found a way to do it, and discovered many problems with the different approaches one could take. The reasoning behind these is outside of the scope of this blogpost, but I’d be happy to elaborate if anyone is interested.<a href="#fnref1">↩</a></p></li>
</ol>
</div>]]></summary>
</entry>
<entry>
    <title>Monads and Arrows: modelling a build system</title>
    <link href="http://jaspervdj.be/posts/2012-01-14-monads-arrows-build-systems.html" />
    <id>http://jaspervdj.be/posts/2012-01-14-monads-arrows-build-systems.html</id>
    <published>2012-01-14T00:00:00Z</published>
    <updated>2012-01-14T00:00:00Z</updated>
    <summary type="html"><![CDATA[<p>This is a recap of an <a href="/posts/2010-03-26-arrows-dependencies.html">older blogpost</a> of me. I decided to rewrite it after I wanted to link a friend to it, and I saw the blogpost clearly failed a bit in getting the point across. In this blogpost, I hope to correct that. It’s about a situation in which Monads fall short, but Arrows (and Applicative) prove to be very powerful. It assumes some basic familiarity with Monads, familiarity with Arrows is not necessary. This blogpost:</p>
<ol style="list-style-type: decimal">
<li>Proposes a simplistic build system model</li>
<li>Gives an implementation using Monads, and fails</li>
<li>Gives a working implementation using Arrow (and Applicative)</li>
</ol>
<h1 id="setup">Setup</h1>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="ot">{-# LANGUAGE Arrows #-}</span>
<span class="ot">&gt;</span> <span class="kw">import</span> Prelude <span class="kw">hiding</span> (id, (.))</code></pre>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">import</span> Control.Applicative (Applicative (..), (&lt;$&gt;))
<span class="ot">&gt;</span> <span class="kw">import</span> Control.Arrow (Arrow (..), returnA, (&gt;&gt;&gt;))
<span class="ot">&gt;</span> <span class="kw">import</span> Control.Category (Category (..))
<span class="ot">&gt;</span> <span class="kw">import</span> Control.Monad ((&lt;=&lt;))
<span class="ot">&gt;</span> <span class="kw">import</span> Control.Monad.State (StateT)
<span class="ot">&gt;</span> <span class="kw">import</span> System.Directory (doesFileExist, getModificationTime)</code></pre>
<p>This post uses incremental build systems (think <a href="http://www.gnu.org/software/make/">make</a> or <a href="http://ant.apache.org/">ant</a>) as an example. These systems allow you to specify commands which are only executed if the destination file is out-of-date. The reason I’m using this example is that it’s highly applicable in <a href="http://jaspervdj.be/hakyll">Hakyll</a>, a static site compiler which is one of my side projects.</p>
<p>Let’s use a bottom-up approach and first write a simple function to only do out-of-date builds. The <code>runBuild</code> function checks the modification times of the dependencies and the destination file, and based on that information, calls or doesn’t call the <code>IO String</code> workhorse. This is obviously very limited functionality, but it’s just an example.</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; runBuild ::</span> <span class="fu">FilePath</span>    <span class="co">-- ^ Destination</span>
<span class="ot">&gt;</span>          <span class="ot">-&gt;</span> [<span class="fu">FilePath</span>]  <span class="co">-- ^ Dependencies</span>
<span class="ot">&gt;</span>          <span class="ot">-&gt;</span> <span class="dt">IO</span> <span class="dt">String</span>   <span class="co">-- ^ Workhorse which produces output</span>
<span class="ot">&gt;</span>          <span class="ot">-&gt;</span> <span class="dt">IO</span> ()       <span class="co">-- ^ May or may not run the workhorse</span>
<span class="ot">&gt;</span> runBuild dest deps f <span class="fu">=</span> <span class="kw">do</span>
<span class="ot">&gt;</span>     exists       <span class="ot">&lt;-</span> doesFileExist dest
<span class="ot">&gt;</span>     depsModified <span class="ot">&lt;-</span> <span class="fu">mapM</span> getModificationTime deps
<span class="ot">&gt;</span>     <span class="kw">case</span> (exists, depsModified) <span class="kw">of</span>
<span class="ot">&gt;</span>         (<span class="kw">False</span>, _) <span class="ot">-&gt;</span> run
<span class="ot">&gt;</span>         (<span class="kw">True</span>, []) <span class="ot">-&gt;</span> dontRun
<span class="ot">&gt;</span>         (<span class="kw">True</span>, _)  <span class="ot">-&gt;</span> <span class="kw">do</span>
<span class="ot">&gt;</span>             destModified <span class="ot">&lt;-</span> getModificationTime dest
<span class="ot">&gt;</span>             <span class="kw">if</span> destModified <span class="fu">&lt;</span> <span class="fu">maximum</span> depsModified <span class="kw">then</span> run <span class="kw">else</span> dontRun
<span class="ot">&gt;</span>   <span class="kw">where</span>
<span class="ot">&gt;</span>     dontRun <span class="fu">=</span> <span class="fu">putStrLn</span> <span class="fu">$</span> <span class="st">&quot;Up to date: &quot;</span> <span class="fu">++</span> dest
<span class="ot">&gt;</span>     run <span class="fu">=</span> <span class="kw">do</span>
<span class="ot">&gt;</span>         <span class="fu">putStrLn</span> <span class="fu">$</span> <span class="st">&quot;Building &quot;</span> <span class="fu">++</span> dest
<span class="ot">&gt;</span>         <span class="fu">writeFile</span> dest <span class="fu">=&lt;&lt;</span> f</code></pre>
<p>Let’s implement the Unix <a href="http://en.wikipedia.org/wiki/Paste_%28Unix%29">paste</a> command. We first have a pure version:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; paste ::</span> <span class="dt">String</span> <span class="ot">-&gt;</span> <span class="dt">String</span> <span class="ot">-&gt;</span> <span class="dt">String</span>
<span class="ot">&gt;</span> paste x y <span class="fu">=</span> <span class="fu">unlines</span> <span class="fu">$</span> <span class="fu">zipWith</span> (\x&#39; y&#39; <span class="ot">-&gt;</span> x&#39; <span class="fu">++</span> <span class="st">&quot;\t&quot;</span> <span class="fu">++</span> y&#39;) (<span class="fu">lines</span> x) (<span class="fu">lines</span> y)</code></pre>
<p>And now we can apply our <code>runBuild</code> function:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; testBuild ::</span> <span class="dt">IO</span> ()
<span class="ot">&gt;</span> testBuild <span class="fu">=</span> runBuild <span class="st">&quot;test-io.txt&quot;</span> [<span class="st">&quot;rainbows.txt&quot;</span>, <span class="st">&quot;unicorns.txt&quot;</span>] <span class="fu">$</span> <span class="kw">do</span>
<span class="ot">&gt;</span>     x <span class="ot">&lt;-</span> <span class="fu">readFile</span> <span class="st">&quot;rainbows.txt&quot;</span>
<span class="ot">&gt;</span>     y <span class="ot">&lt;-</span> <span class="fu">readFile</span> <span class="st">&quot;unicorns.txt&quot;</span>
<span class="ot">&gt;</span>     <span class="fu">return</span> <span class="fu">$</span> paste x y</code></pre>
<p>This works fine, but the annoyance is that we manually have to specify our dependencies: this quickly becomes very tedious. Instead, our goal is to automate the dependency tracking. Haskell allows for many abstractions, so let’s have a look at how we can accomplish this.</p>
<h1 id="monads">Monads</h1>
<p>Let’s see if we can capture this behaviour in a Monad. If we declare our Monad as a simple datatype which holds the dependencies and the actual workhorse. we get something like:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">data</span> <span class="dt">BuildM</span> a <span class="fu">=</span> <span class="dt">BuildM</span> [<span class="fu">FilePath</span>] (<span class="dt">IO</span> a)</code></pre>
<p>Running is easy:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; runBuildM ::</span> <span class="fu">FilePath</span> <span class="ot">-&gt;</span> <span class="dt">BuildM</span> <span class="dt">String</span> <span class="ot">-&gt;</span> <span class="dt">IO</span> ()
<span class="ot">&gt;</span> runBuildM dest (<span class="dt">BuildM</span> deps f) <span class="fu">=</span> runBuild dest deps f</code></pre>
<p>And a <code>readFile</code> could be implemented like:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; readFileM ::</span> <span class="fu">FilePath</span> <span class="ot">-&gt;</span> <span class="dt">BuildM</span> <span class="dt">String</span>
<span class="ot">&gt;</span> readFileM path <span class="fu">=</span> <span class="dt">BuildM</span> [path] <span class="fu">$</span> <span class="fu">readFile</span> path</code></pre>
<p>However, problems arise when we try to pin down the Monad instance for this datatype.</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">instance</span> <span class="kw">Monad</span> <span class="dt">BuildM</span> <span class="kw">where</span>
<span class="ot">&gt;</span>     <span class="fu">return</span> x              <span class="fu">=</span> <span class="dt">BuildM</span> []   <span class="fu">$</span> <span class="fu">return</span> x
<span class="ot">&gt;</span>     (<span class="dt">BuildM</span> deps f) <span class="fu">&gt;&gt;=</span> g <span class="fu">=</span> <span class="dt">BuildM</span> deps <span class="fu">$</span> <span class="kw">do</span>
<span class="ot">&gt;</span>         <span class="co">-- Where do the dependencies of g&#39;s result go?</span>
<span class="ot">&gt;</span>         <span class="dt">BuildM</span> _ y <span class="ot">&lt;-</span> g <span class="fu">&lt;$&gt;</span> f
<span class="ot">&gt;</span>         y</code></pre>
<p>Clearly, this datatype doesn’t allow us to get <code>f</code>s dependencies in <code>mx &gt;&gt;= f</code>. We can write the following piece of code, but it won’t be correct, as it ignores the <code>&quot;unicorns.txt&quot;</code> dependency.</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; testBuildM ::</span> <span class="dt">IO</span> ()
<span class="ot">&gt;</span> testBuildM <span class="fu">=</span> runBuildM <span class="st">&quot;test-m.txt&quot;</span> <span class="fu">$</span> <span class="kw">do</span>
<span class="ot">&gt;</span>     x <span class="ot">&lt;-</span> readFileM <span class="st">&quot;rainbows.txt&quot;</span>
<span class="ot">&gt;</span>     y <span class="ot">&lt;-</span> readFileM <span class="st">&quot;unicorns.txt&quot;</span>
<span class="ot">&gt;</span>     <span class="fu">return</span> <span class="fu">$</span> paste x y</code></pre>
<p>Other datatypes are possible, e.g. one could also try something like:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">type</span> <span class="dt">BuildM&#39;</span> <span class="fu">=</span> <span class="dt">StateT</span> [<span class="fu">FilePath</span>] <span class="dt">IO</span></code></pre>
<p>This kind of definition leads to another problem: the <code>mx</code> in <code>mx &gt;&gt;= f</code> will always be executed, even if everything is up-to-date. This behaviour is inherently coupled to the use of Monads, consider code like this:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; testBuildM&#39; ::</span> <span class="dt">IO</span> ()
<span class="ot">&gt;</span> testBuildM&#39; <span class="fu">=</span> runBuildM <span class="st">&quot;test-m.txt&quot;</span> <span class="fu">$</span> <span class="kw">do</span>
<span class="ot">&gt;</span>     x <span class="ot">&lt;-</span> readFileM <span class="st">&quot;rainbows.txt&quot;</span>
<span class="ot">&gt;</span>     y <span class="ot">&lt;-</span> <span class="kw">if</span> <span class="fu">length</span> x <span class="fu">&gt;</span> <span class="dv">200</span> <span class="kw">then</span> readFileM <span class="st">&quot;unicorns.txt&quot;</span> <span class="kw">else</span> <span class="fu">return</span> <span class="st">&quot;&quot;</span>
<span class="ot">&gt;</span>     <span class="fu">return</span> <span class="fu">$</span> paste x y</code></pre>
<p>We <em>need</em> to evaluate x in order to determine the dependencies! This is not how a build system should work: the system should <em>not</em> inspect <code>x</code> and just add <code>&quot;unicorns.txt&quot;</code> as a dependency, regardless of the value of <code>x</code>. The fact that we can’t get around this makes it clear that Monads are not a good choice here.</p>
<h1 id="arrows">Arrows</h1>
<p>Two other possibilities will work well here: Arrows and Applicative. I’ll demonstrate the Arrow solution first, because it is a bit more generic <sup><a href="#fn1" class="footnoteRef" id="fnref1">1</a></sup>.</p>
<p>The datatype looks a lot like the one used for the Monad instance <sup><a href="#fn2" class="footnoteRef" id="fnref2">2</a></sup>:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">data</span> <span class="dt">BuildA</span> a b <span class="fu">=</span> <span class="dt">BuildA</span> [<span class="fu">FilePath</span>] (a <span class="ot">-&gt;</span> <span class="dt">IO</span> b)</code></pre>
<p>Running this build datatype is also straightforward <sup><a href="#fn3" class="footnoteRef" id="fnref3">3</a></sup>.</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; runBuildA ::</span> <span class="fu">FilePath</span> <span class="ot">-&gt;</span> <span class="dt">BuildA</span> () <span class="dt">String</span> <span class="ot">-&gt;</span> <span class="dt">IO</span> ()
<span class="ot">&gt;</span> runBuildA dest (<span class="dt">BuildA</span> deps f) <span class="fu">=</span> runBuild dest deps <span class="fu">$</span> f ()</code></pre>
<p>Arrows are a generalized version of functions, and can be used in a similar way. Each Arrow is also a Category, so we first need to declare a Category instance. In order to make our <code>BuildA</code> an Category, we need an identity operation, and function composition.</p>
<p>The <code>BuildA a a</code> identity operation is straightforward to implement: it obviously has no dependencies, it is a build step which does absolutely nothing. A composition of two build steps takes the sum of dependencies and composes the workhorses using <code>&lt;=&lt;</code> <sup><a href="#fn4" class="footnoteRef" id="fnref4">4</a></sup>:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">instance</span> <span class="dt">Category</span> <span class="dt">BuildA</span> <span class="kw">where</span>
<span class="ot">&gt;</span>     <span class="fu">id</span>                        <span class="fu">=</span> <span class="dt">BuildA</span> [] <span class="fu">return</span>
<span class="ot">&gt;</span>     <span class="dt">BuildA</span> d1 f <span class="fu">.</span> <span class="dt">BuildA</span> d2 g <span class="fu">=</span> <span class="dt">BuildA</span> (d1 <span class="fu">++</span> d2) (f <span class="fu">&lt;=&lt;</span> g)</code></pre>
<p>This is not enough to instantiate an Arrow, though. Two more methods need to be implemented: <code>arr</code> and <code>first</code>.</p>
<p><code>arr</code> is reasonably simple and allows the user to “lift” a pure function into an Arrow. For our example, this yields the type signature <code>arr :: (a -&gt; b) -&gt; BuildA a b</code> – the implementation is straightforward.</p>
<p>In order to allow the programmer to build computations using Arrows, a mechanism to pass variables through computations is needed. In our example, we have <code>first :: BuildA a b -&gt; BuildA (a, c) (b, c)</code>: it transforms a simple Arrow into an Arrow which carries an additional variable through the computation.</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">instance</span> <span class="dt">Arrow</span> <span class="dt">BuildA</span> <span class="kw">where</span>
<span class="ot">&gt;</span>     arr f                 <span class="fu">=</span> <span class="dt">BuildA</span> [] (<span class="fu">return</span> <span class="fu">.</span> f)
<span class="ot">&gt;</span>     first (<span class="dt">BuildA</span> deps f) <span class="fu">=</span> <span class="dt">BuildA</span> deps <span class="fu">$</span> \(x, y) <span class="ot">-&gt;</span> <span class="kw">do</span>
<span class="ot">&gt;</span>         x&#39; <span class="ot">&lt;-</span> f x
<span class="ot">&gt;</span>         <span class="fu">return</span> (x&#39;, y)</code></pre>
<p>Let’s write the Arrow version of <code>readFileM</code> which also automatically adds a dependency:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; readFileA ::</span> <span class="fu">FilePath</span> <span class="ot">-&gt;</span> <span class="dt">BuildA</span> () <span class="dt">String</span>
<span class="ot">&gt;</span> readFileA path <span class="fu">=</span> <span class="dt">BuildA</span> [path] <span class="fu">$</span> \() <span class="ot">-&gt;</span> <span class="fu">readFile</span> path</code></pre>
<p>Using <a href="http://www.haskell.org/arrows/syntax.html">Arrow notation</a>, we can now implement a (not very pretty) solution which does bear a lot of resemblance to <code>testBuildM</code>, with the difference that this version actually works with proper dependency management:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; testBuildA ::</span> <span class="dt">IO</span> ()
<span class="ot">&gt;</span> testBuildA <span class="fu">=</span> runBuildA <span class="st">&quot;test-a.txt&quot;</span> <span class="fu">$</span> proc () <span class="ot">-&gt;</span> <span class="kw">do</span>
<span class="ot">&gt;</span>     x <span class="ot">&lt;-</span> readFileA <span class="st">&quot;rainbows.txt&quot;</span> <span class="fu">-&lt;</span> ()
<span class="ot">&gt;</span>     y <span class="ot">&lt;-</span> readFileA <span class="st">&quot;unicorns.txt&quot;</span> <span class="fu">-&lt;</span> ()
<span class="ot">&gt;</span>     returnA <span class="fu">-&lt;</span> paste x y</code></pre>
<p>However, writing ugly code like this obviously isn’t the way we want to go. Arrow-based code can be made a whole lot prettier if you write as much code as possible as a processing Arrow. For example, we could write an Arrow-based variant of <code>paste</code> which processes a file by pasting another file next to it:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; pasteFileA ::</span> <span class="fu">FilePath</span> <span class="ot">-&gt;</span> <span class="dt">BuildA</span> <span class="dt">String</span> <span class="dt">String</span>
<span class="ot">&gt;</span> pasteFileA path <span class="fu">=</span> proc x <span class="ot">-&gt;</span> <span class="kw">do</span>
<span class="ot">&gt;</span>     y <span class="ot">&lt;-</span> readFileA path <span class="fu">-&lt;</span> ()
<span class="ot">&gt;</span>     returnA <span class="fu">-&lt;</span> paste x y</code></pre>
<p>With utilities like this, we can write a much prettier <code>testBuildA</code> which clearly demonstrates the processing approach. <code>&gt;&gt;&gt;</code> is left-to-right composition of Arrows, much like a flipped version of <code>.</code>:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; testBuildA&#39; ::</span> <span class="dt">IO</span> ()
<span class="ot">&gt;</span> testBuildA&#39; <span class="fu">=</span> runBuildA <span class="st">&quot;test-a.txt&quot;</span> <span class="fu">$</span>
<span class="ot">&gt;</span>     readFileA <span class="st">&quot;rainbows.txt&quot;</span> <span class="fu">&gt;&gt;&gt;</span>
<span class="ot">&gt;</span>     pasteFileA <span class="st">&quot;unicorns.txt&quot;</span></code></pre>
<h1 id="epilogue-applicative-functors">Epilogue: Applicative functors</h1>
<p>Arrow and Applicative show similar behaviour in many cases. For our example, we also could’ve chosen to implement our solution using Applicative instead of Arrow. I’ve chosen Arrow for two reasons:</p>
<ul>
<li>It is often more natural to model a building process as an Arrow.</li>
<li>We can actually write an Applicative instance for the <em>same</em> datatype, giving the user the freedom of choice!</li>
</ul>
<p>It’s a fun challenge to implement this Applicative instance for the <code>BuildA</code> datatype.</p>
<div onclick="$('#applicative').show(1000)">

<p><strong>Click here to reveal the solution.</strong></p>
<div></div></div>  <!-- HACKS -->

<div id='applicative' style='display: none'>

<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">instance</span> <span class="kw">Functor</span> (<span class="dt">BuildA</span> a) <span class="kw">where</span>
<span class="ot">&gt;</span>     <span class="fu">fmap</span> f (<span class="dt">BuildA</span> deps g) <span class="fu">=</span> <span class="dt">BuildA</span> deps (<span class="fu">fmap</span> f <span class="fu">.</span> g)
<span class="ot">&gt;</span> 
<span class="ot">&gt;</span> <span class="kw">instance</span> <span class="kw">Applicative</span> (<span class="dt">BuildA</span> a) <span class="kw">where</span>
<span class="ot">&gt;</span>     pure x                      <span class="fu">=</span> <span class="dt">BuildA</span> [] <span class="fu">$</span> <span class="fu">const</span> <span class="fu">$</span> <span class="fu">return</span> x
<span class="ot">&gt;</span>     <span class="dt">BuildA</span> d1 f <span class="fu">&lt;*&gt;</span> <span class="dt">BuildA</span> d2 g <span class="fu">=</span> <span class="dt">BuildA</span> (d1 <span class="fu">++</span> d2) <span class="fu">$</span> \x <span class="ot">-&gt;</span> f x <span class="fu">&lt;*&gt;</span> g x
<span class="ot">&gt;</span> 
<span class="ot">&gt; testBuildApp ::</span> <span class="dt">IO</span> ()
<span class="ot">&gt;</span> testBuildApp <span class="fu">=</span> runBuildA <span class="st">&quot;test-app.txt&quot;</span> <span class="fu">$</span>
<span class="ot">&gt;</span>     paste <span class="fu">&lt;$&gt;</span> readFileA <span class="st">&quot;rainbows.txt&quot;</span> <span class="fu">&lt;*&gt;</span> readFileA <span class="st">&quot;unicorns.txt&quot;</span></code></pre>
<p>Hey, you could even argue that this solution is more beatiful than our Arrow-based <code>testBuildA'</code>. However, this is due to the fact that our example is very simple – Applicative solutions tend to get quite complicated when more is involved.</p>
<div></div></div>

<p>I hope this blogpost made some of the advantages and disadvantages between Monad and Arrow clear. All comments and feedback are welcome, as always. Thanks to <a href="http://twitter.com/nudded">nudded</a> for proofreading.</p>
<div class="footnotes">
<hr />
<ol>
<li id="fn1"><p>More generic in kind: Arrow has a <code>* -&gt; * -&gt; *</code> kind, and Applicative has a <code>* -&gt; *</code> kind. This is important later on, because it means we can reuse our Arrow datatype for the Applicative solution.<a href="#fnref1">↩</a></p></li>
<li id="fn2"><p>The second field is in fact a <a href="http://en.wikipedia.org/wiki/Arrow_%28computer_science%29#Kleisli_arrows">Kleisli</a> arrow, almost a direct translation of the IO monad to the Arrow structure.<a href="#fnref2">↩</a></p></li>
<li id="fn3"><p>Note that another option is: <code>runBuildA :: FilePath -&gt; BuildA a String -&gt; a -&gt; IO ()</code>.<a href="#fnref3">↩</a></p></li>
<li id="fn4"><p><code>&lt;=&lt; :: Monad m =&gt; (b -&gt; m c) -&gt; (a -&gt; m b) -&gt; (a -&gt; m c)</code>, the spaceship operator from <code>Control.Monad</code>, right-to-left composition of monadic functions.<a href="#fnref4">↩</a></p></li>
</ol>
</div>]]></summary>
</entry>
<entry>
    <title>The composability of Iteratees</title>
    <link href="http://jaspervdj.be/posts/2011-12-26-the-composability-of-iteratees.html" />
    <id>http://jaspervdj.be/posts/2011-12-26-the-composability-of-iteratees.html</id>
    <published>2011-12-26T00:00:00Z</published>
    <updated>2011-12-26T00:00:00Z</updated>
    <summary type="html"><![CDATA[<h1 id="a-bit-of-context">A bit of context</h1>
<p>I’ve been working on a <a href="http://jaspervdj.be/websockets">websockets library</a> lately, and this blogpost is mostly based upon work on that library. The code given in this blogpost is <em>not</em> the code from the library, but a stand-alone simplification, to make it more accessible to readers.</p>
<p>This blogpost is based upon the <a href="http://hackage.haskell.org/package/enumerator">enumerator</a> library by John Millikin. Multiple implementations of the Iteratee concept exist, I chose this one because it’s currently the most popular implementation for the packages I work on (mostly web-related). Additionally, alternatives to iteratees are <a href="https://github.com/snoyberg/conduit">being developed</a> and I am certainly very curious as to how this will turn out.</p>
<p>Iteratees are often lauded because they offer great performance characteristics in comparison to lazy I/O. Another key feature provided by iteratees is composability (even for pure operations!) – on which I will focus this blogpost. An understanding of Iteratees is not required for this blogpost, and be aware that my goal is to merely spark your interest, not to fully explain them.</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">import</span> Blaze.ByteString.Builder (Builder)
<span class="ot">&gt;</span> <span class="kw">import</span> Data.ByteString (ByteString)
<span class="ot">&gt;</span> <span class="kw">import</span> Data.Enumerator ((=$))
<span class="ot">&gt;</span> <span class="kw">import</span> Data.Monoid (mappend, mempty)
<span class="ot">&gt;</span> <span class="kw">import</span> <span class="kw">qualified</span> Blaze.ByteString.Builder <span class="kw">as</span> BB
<span class="ot">&gt;</span> <span class="kw">import</span> <span class="kw">qualified</span> Data.Attoparsec <span class="kw">as</span> A
<span class="ot">&gt;</span> <span class="kw">import</span> <span class="kw">qualified</span> Data.Attoparsec.Enumerator <span class="kw">as</span> A
<span class="ot">&gt;</span> <span class="kw">import</span> <span class="kw">qualified</span> Data.ByteString <span class="kw">as</span> B
<span class="ot">&gt;</span> <span class="kw">import</span> <span class="kw">qualified</span> Data.ByteString.Lazy <span class="kw">as</span> BL
<span class="ot">&gt;</span> <span class="kw">import</span> <span class="kw">qualified</span> Data.Enumerator <span class="kw">as</span> E
<span class="ot">&gt;</span> <span class="kw">import</span> <span class="kw">qualified</span> Data.Enumerator.List <span class="kw">as</span> EL</code></pre>
<h1 id="the-problem">The problem</h1>
<p>Different versions of the WebSockets protocol exist. We obviously want to share as much code as possible in between these versions, to avoid duplication. We can define a common <code>Message</code> datatype which can be used for all versions:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">data</span> <span class="dt">Message</span>
<span class="ot">&gt;</span>     <span class="fu">=</span> <span class="dt">TextMessage</span> <span class="dt">BL.ByteString</span>
<span class="ot">&gt;</span>     <span class="fu">|</span> <span class="dt">PingMessage</span>
<span class="ot">&gt;</span>     <span class="fu">|</span> <span class="dt">CloseMessage</span>
<span class="ot">&gt;</span>     <span class="kw">deriving</span> (<span class="kw">Show</span>)</code></pre>
<p>Now, for each version, we want to be able to parse these messages. That means that:</p>
<ol style="list-style-type: decimal">
<li>we agree on a common interface (e.g. <code>ByteString -&gt; Message</code>);</li>
<li>we implement this interface for each version;</li>
<li>we choose the right implementation at runtime.</li>
</ol>
<p>The first WebSockets drafts (referred to Hybi00) offered a very simple message format. A parser could be implemented easily:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; parseMessageHybi00 ::</span> <span class="dt">A.Parser</span> <span class="dt">Message</span>
<span class="ot">&gt;</span> parseMessageHybi00 <span class="fu">=</span> <span class="fu">error</span> <span class="st">&quot;Omitted for brevity&quot;</span></code></pre>
<p>However – this is not the case for later versions of the protocol (referred to as Hybi10). These later versions introduced <em>frames</em>. This allows an web application to multiplex different messages onto the socket – more specifically, it can insert <em>control messages</em> (e.g. ping) in between messages with huge payloads. This increases the complexity of our server, since we have to demultiplex these frames again.</p>
<p>We have three types of frames in the simplification of the protocol:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">data</span> <span class="dt">FrameType</span> <span class="fu">=</span> <span class="dt">TextFrame</span> <span class="fu">|</span> <span class="dt">PingFrame</span> <span class="fu">|</span> <span class="dt">CloseFrame</span>
<span class="ot">&gt;</span>     <span class="kw">deriving</span> (<span class="kw">Show</span>)</code></pre>
<p>And a frame has a type, a flag indicating whether or not this is the last frame, and a payload.</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">data</span> <span class="dt">Frame</span> <span class="fu">=</span> <span class="dt">Frame</span> <span class="dt">FrameType</span> <span class="dt">Bool</span> <span class="dt">ByteString</span>
<span class="ot">&gt;</span>     <span class="kw">deriving</span> (<span class="kw">Show</span>)</code></pre>
<p>These are not hard to parse either:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; parseFrameHybi10 ::</span> <span class="dt">A.Parser</span> <span class="dt">Frame</span>
<span class="ot">&gt;</span> parseFrameHybi10 <span class="fu">=</span> <span class="fu">error</span> <span class="st">&quot;Omitted for brevity&quot;</span></code></pre>
<p>But if we want to have a common interface, we will need to access the result as a <code>Message</code>. Remember that, we want the Hybi00 and the Hybi10 implementations to offer <em>the same</em> interface: simply put, something like <code>ByteString -&gt; Message</code>.</p>
<p>In a more simple world, we would’ve been able to write a simple <code>[Frame] -&gt; Message</code> function. This is clearly not the case here, as many frames can produce many messages. <code>[Frame] -&gt; [Message]</code> looks better, but what do we do with leftover input which belongs to some next message? We clearly need a stateful demultiplexer.</p>
<h1 id="a-stateful-demultiplexer">A stateful demultiplexer</h1>
<p>Most demultiplexers are obviously stateful – the reason why I mentioned this here is because our demultiplexer will be <em>explicitly</em> stateful. Explicit state is not always a good thing: when this state impacts your entire application, it quickly becomes tedious to pass around and manage. But here, we can clearly restrict the statefulness to the demultiplexer.</p>
<p>In our simplification, <code>TextMessage</code> is the only kind of message that can be split up in frames. This simplifies our demultiplexer greatly: we only need to keep track of the leftover input. This is <em>not</em> the case for the actual implementation, so it’s probably a good idea to see the demultiplexer as some kind of black box, accessible only through the functions <code>emptyDemultiplexState</code> and <code>demultiplex</code>.</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">type</span> <span class="dt">DemultiplexState</span> <span class="fu">=</span> <span class="dt">Builder</span></code></pre>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; emptyDemultiplexState ::</span> <span class="dt">DemultiplexState</span>
<span class="ot">&gt;</span> emptyDemultiplexState <span class="fu">=</span> mempty</code></pre>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; demultiplex ::</span> <span class="dt">Frame</span> <span class="ot">-&gt;</span> <span class="dt">DemultiplexState</span> <span class="ot">-&gt;</span> ([<span class="dt">Message</span>], <span class="dt">DemultiplexState</span>)
<span class="ot">&gt;</span> demultiplex (<span class="dt">Frame</span> <span class="dt">CloseFrame</span> _ _)   ds <span class="fu">=</span> ([<span class="dt">CloseMessage</span>], ds)
<span class="ot">&gt;</span> demultiplex (<span class="dt">Frame</span> <span class="dt">PingFrame</span> _ _)    ds <span class="fu">=</span> ([<span class="dt">PingMessage</span>], ds)
<span class="ot">&gt;</span> demultiplex (<span class="dt">Frame</span> <span class="dt">TextFrame</span> fin bs) ds <span class="fu">=</span> <span class="kw">case</span> fin <span class="kw">of</span>
<span class="ot">&gt;</span>     <span class="kw">False</span> <span class="ot">-&gt;</span> ([], ds&#39;)
<span class="ot">&gt;</span>     <span class="kw">True</span>  <span class="ot">-&gt;</span> ([<span class="dt">TextMessage</span> <span class="fu">$</span> BB.toLazyByteString ds&#39;], emptyDemultiplexState)
<span class="ot">&gt;</span>   <span class="kw">where</span>
<span class="ot">&gt;</span>     ds&#39; <span class="fu">=</span> ds <span class="ot">`mappend`</span> BB.fromByteString bs</code></pre>
<p>Such a straightforward Haskell implementation is easy to read, debug and understand, but having to manually keep track of the state is surely a disadvantage… or not?</p>
<h1 id="a-common-interface">A common interface</h1>
<p>At a first sight, a disadvantage of such a stateful demultiplexer is that we clearly cannot use a simple interface such as:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">type</span> <span class="dt">Interface0</span> <span class="fu">=</span> <span class="dt">ByteString</span> <span class="ot">-&gt;</span> <span class="dt">Message</span></code></pre>
<p>nor can we use (superficially) more complex interfaces such as:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">type</span> <span class="dt">Interface1</span> <span class="fu">=</span> [<span class="dt">ByteString</span>] <span class="ot">-&gt;</span> [<span class="dt">Message</span>]</code></pre>
<p>precisely because we have to keep track of the <code>DemultiplexState</code> for the Hybi10 version. Well, we can easily solve this:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">type</span> <span class="dt">Interface2</span> <span class="fu">=</span>  <span class="dt">DemultiplexState</span>
<span class="ot">&gt;</span>                 <span class="ot">-&gt;</span> <span class="dt">ByteString</span> <span class="ot">-&gt;</span> ([<span class="dt">Message</span>], <span class="dt">DemultiplexState</span>)</code></pre>
<p>…given this interface, it is possible to write a correct implementation for Hybi00 as well as Hybi10: the Hybi00 implementation would simply ignore the <code>DemultiplexState</code>, and the Hybi10 implementation would use it as input for the <code>demultiplex</code> function.</p>
<p>This is clearly an example of when explicit state becomes tedious to manage: suppose we add another stateful component for some Hybi9000 implementation. Then, we would have to adapt our <code>Interface2</code> too also incorporate this state: it seems we’re going down a very dangerous road here.</p>
<h1 id="enter-iteratee">Enter Iteratee</h1>
<p>As the title and introduction of this blogpost suggested, there is a better way to solve this problem using Iteratees. I’m not going to go through all Iteratee concepts here, but you should be able to follow with a basic Haskell knowledge: let the types guide you.</p>
<p>An <code>Enumeratee x y m b</code> is a stream transformer which turns values of the type <code>x</code> to values of the type <code>y</code>. We’ll agree on such an interface (feel free to safely ignore the <code>m</code> and <code>b</code> parameters if you’re unfamiliar with Iteratees):</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">type</span> <span class="dt">Interface3</span> m b <span class="fu">=</span> <span class="dt">E.Enumeratee</span> <span class="dt">B.ByteString</span> <span class="dt">Message</span> m b</code></pre>
<p>The implementation for Hybi00 looks fairly easy:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; implHybi00 ::</span> <span class="kw">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Interface3</span> m b
<span class="ot">&gt;</span> implHybi00 <span class="fu">=</span> E.sequence <span class="fu">$</span> A.iterParser parseMessageHybi00</code></pre>
<p>The trick is that we turn our parser into an <code>Iteratee</code> using the <code>iterParser</code> function from the <a href="http://hackage.haskell.org/package/attoparsec-enumerator">attoparsec-enumerator</a> library. Such an <code>Iteratee</code> reads <code>ByteString</code>s as input, and produces our <code>Message</code>: by repeatedly applying this parser (using <code>sequence</code>), we obtain the stream transformer we wanted.</p>
<p>This is not very impressive, since a simple function could’ve be enough for the Hybi00 implementation. But let’s look at the Hybi10 implementation:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; implFramesHybi00 ::</span> <span class="kw">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">E.Enumeratee</span> <span class="dt">B.ByteString</span> <span class="dt">Frame</span> m b
<span class="ot">&gt;</span> implFramesHybi00 <span class="fu">=</span> E.sequence <span class="fu">$</span> A.iterParser parseFrameHybi10</code></pre>
<p>constructing a stream transformer from <code>ByteString</code> to <code>Frame</code> is similar to what we did for Hybi00. But we can implement an additional stream transformer:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; framesToMessages ::</span> <span class="kw">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">E.Enumeratee</span> <span class="dt">Frame</span> <span class="dt">Message</span> m b
<span class="ot">&gt;</span> framesToMessages <span class="fu">=</span> EL.concatMapAccum step emptyDemultiplexState
<span class="ot">&gt;</span>   <span class="kw">where</span>
<span class="ot">&gt;</span>     step ds frame <span class="fu">=</span> <span class="kw">let</span> (msgs, ds&#39;) <span class="fu">=</span> demultiplex frame ds <span class="kw">in</span> (ds&#39;, msgs)</code></pre>
<p>And compose these stream transformers to obtain our implementation:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; implHybi10 ::</span> <span class="kw">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Interface3</span> m b
<span class="ot">&gt;</span> implHybi10 <span class="fu">=</span> (implFramesHybi00 <span class="fu">=$</span>) <span class="fu">.</span> framesToMessages</code></pre>
<p>Mission completed – and we’ve successfully hidden our state management from the users of this interface.</p>
<h1 id="conclusion">Conclusion</h1>
<p>I’ve demonstrated that Iteratees allow you to write clear, concise and explicitly stateful Haskell code, which is still composable without having to manually manage this state in higher levels of your application. This is not a new thing: it is probably the essence of stream processing in Haskell. Yet Iteratees combine this with great performance and elegance, making it a very interesting feature.</p>]]></summary>
</entry>

</feed>
