<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[Paolo Capriotti's blog]]></title>
  <link href="http://pcapriotti.github.com/atom.xml" rel="self"/>
  <link href="http://pcapriotti.github.com/"/>
  <updated>2013-04-03T13:32:47+01:00</updated>
  <id>http://pcapriotti.github.com/</id>
  <author>
    <name><![CDATA[Paolo Capriotti]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Free Applicative Functors]]></title>
    <link href="http://pcapriotti.github.com/blog/2013/04/03/free-applicative-functors/"/>
    <updated>2013-04-03T12:54:00+01:00</updated>
    <id>http://pcapriotti.github.com/blog/2013/04/03/free-applicative-functors</id>
    <content type="html"><![CDATA[<p>After my post on <a href="http://pcapriotti.github.com/blog/2012/04/27/applicative-option-parser/">option parsers</a> with applicative functors,
I’ve been working on a paper to develop the idea of “free applicative”
contained in that post.</p>

<p>A draft of the paper, joint work with <a href="http://akaposi.web.elte.hu/">Ambrus Kaposi</a>, has been submitted to
<a href="http://www.icfpconference.org/icfp2013/">ICFP 2013</a>, and is available <a href="http://pcapriotti.github.com/assets/applicative.pdf">here</a>.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Families and fibrations]]></title>
    <link href="http://pcapriotti.github.com/blog/2013/02/20/families-and-fibrations/"/>
    <updated>2013-02-20T00:00:00+00:00</updated>
    <id>http://pcapriotti.github.com/blog/2013/02/20/families-and-fibrations</id>
    <content type="html"><![CDATA[<h2 id="introduction">Introduction</h2>

<p>The notion of family of “objects” indexed over an object of the same type is
ubiquitous is mathematics and computer science.</p>

<p>It appears everywhere in topology and algebraic geometry, in the form of
<em>bundles</em>, <em>covering maps</em>, or, more generally, <em>fibrations</em>.</p>

<p>In type theory, it is the fundamental idea captured by the notion of <em>dependent
type</em>, on which Martin-Löf intuitionistic type theory is based.</p>

<h2 id="definition">Definition</h2>

<p>Restricting ourselves to $\mathrm{Set}$, the category of sets, for the time
being (and ignoring issues of size), it is straightforward to give a formal
definition of what a <em>family</em> of sets is:</p>

<p>Given a set <code>A</code>, a family over <code>A</code> is a function from <code>A</code> to the objects of the
category of sets (or equivalently, on the other side of the adjunction, a
functor from <code>A</code> regarded as a discrete category to $\mathrm{Set}$).</p>

<p>This is a perfectly valid definition, but it has two problems:</p>

<ol>
  <li>
    <p>It can be awkward to work with functions between objects of different
“sorts” (like sets and universes).</p>
  </li>
  <li>
    <p>It is not clear how to generalize the idea to other categories, like
$\mathrm{Top}$ (the category of topological spaces and continuous maps), for
example. In fact, we would like a family of spaces to be “continuous” in
some sense, but in order for that to make sense, we would need to define a
topology on the class of topological spaces.</p>
  </li>
</ol>

<h2 id="display-maps">Display maps</h2>

<p>Fortunately, there is a very simple construction that helps bringing this
definition to a form which is much easier to work with.</p>

<p>Let’s start with a family of sets <code>B</code> over <code>A</code>, defined as above: <code>B : A →
Set</code>.</p>

<p>Define the “total space” of the family as the disjoint union (or dependent sum)
of all the sets of the family (I’ll use type theoretic notation from now on):</p>

<pre><code>E = Σ (a : A) . B a
</code></pre>

<p>The <em>fibration</em> (or <em>display map</em>) associated to the family <code>B</code> is defined to be the
first projection:</p>

<pre><code>proj₁ : E → A
</code></pre>

<p>So far, we haven’t done very much. The interesting observation is that we can
always recover a family of sets from any function <code>E → A</code>.</p>

<p>In fact, suppose that now <code>E</code> is any set, and <code>p : E → A</code> any function. We can
define a family of sets:</p>

<pre><code>B : A → Set
B a = p ⁻¹ a
</code></pre>

<p>as the function that associates to each point in <code>A</code>, its inverse image (or fiber)
in <code>E</code>.</p>

<p>It is now straightforward to check that these two mappings between families and
fibrations are inverses of one another.</p>

<p>Intuitively, given a family <code>B</code>, the corresponding fibration maps each element
of all possible sets in the family to its “index” in <code>A</code>. Viceversa, given a
fibration <code>p : E → A</code>, the corresponding family is just the family of fibers of
<code>p</code>.</p>

<p><a href="http://pcapriotti.github.com/pages/display/display.html">Here</a> is formalization in Agda of this correspondence as an
isomorphism between families and fibrations. This uses
<a href="https://github.com/pcapriotti/agda-base">agda-base</a> instead of the standard
library, as it needs univalence in order to make the isomorphism explicit.</p>

<h2 id="examples-of-constructions">Examples of constructions</h2>

<p>Once we understand how families and fibrations are really two views of the same
concept, we can look at a number of constructions for families, and check how
they look like in the world of fibrations.</p>

<h3 id="dependent-sum">Dependent sum</h3>

<p>The simplest construction is the total space:</p>

<pre><code>E = Σ (x : A). B x
</code></pre>

<p>As we already know, this corresponds to the domain of the associated fibration.</p>

<h3 id="dependent-product">Dependent product</h3>

<p>Given a family of sets <code>B</code> over <code>A</code>, a <em>choice function</em> is a function that
assigns to each element <code>x</code> of <code>A</code>, an element <code>y</code> of <code>B x</code>. This is called a
<em>dependent function</em> in type theory.</p>

<p>The corresponding notion for a fibration <code>p : E → A</code> is a function <code>s : A → E</code>
such that for each <code>x : A</code>, the index of <code>s x</code> is exactly <code>x</code>. In other words,
<code>p ∘ s ≡ id</code>, i.e. <code>s</code> is a <em>section</em> of <code>p</code>.</p>

<p>The set of such sections is called the dependent product of the family <code>B</code>.</p>

<h3 id="pullbacks">Pullbacks</h3>

<p>Let <code>A</code> and <code>A'</code> be two sets, and <code>B</code> a family over <code>A</code>. Suppose we have a function</p>

<pre><code>r : A' → A
</code></pre>

<p>We can easily define a family <code>B'</code> over <code>A'</code> by composing with <code>r</code>:</p>

<pre><code>B' : A' → Set
B' x = B (r x)
</code></pre>

<p>What does the fibration <code>p' : E' → A'</code> associated to <code>B'</code> look like in terms of
the fibration <code>p : E → A</code> associated to <code>B</code>?</p>

<p>Well, given an element <code>b</code> in the total space of <code>B'</code>, <code>b</code> is going to be in
<code>B' x</code> for some <code>x : A'</code>.  Since <code>B' x ≡ B (r x)</code> by definition, <code>b</code> can also
be regarded as an element of the total space of <code>B</code>. So we have a map <code>s : E' →
E</code>, and we can draw the following diagram:</p>

<script type="math/tex; mode=display">
\newcommand{\ra}[1]{\kern-1.5ex\xrightarrow{\ \ #1\ \ }\phantom{}\kern-1.5ex}
\newcommand{\ras}[1]{\kern-1.5ex\xrightarrow{\ \ \smash{#1}\ \ }\phantom{}\kern-1.5ex}
\newcommand{\da}[1]{\bigg\downarrow\raise.5ex\rlap{\scriptstyle#1}}
\begin{array}{c}
E' & \ra{s} & E \\
\da{p'} & & \da{p} \\
A' & \ra{r} & A \\
\end{array}
</script>

<p>The commutativity of this diagram follows from the immediate observation that
the index above <code>s b</code> is exactly <code>r x</code>.</p>

<p>Now, given elements <code>x : A'</code>, and <code>b : E</code>, saying that <code>p b ≡ r x</code> is
equivalent to saying that <code>b</code> is in <code>B (r x)</code>. In that case, <code>b</code> can be
regarded as an element of <code>B' x</code>, which means that there exists a <code>b'</code> in <code>E'</code>
such that <code>p' b' ≡ x</code> and <code>s b' ≡ b</code>.</p>

<p>What this says is that the above diagram is a pullback square.</p>

<h2 id="adjoints">Adjoints</h2>

<p>It is important to note that the previous constructions are related in
interesting ways.</p>

<p>Let’s look at a simple special case of the pullback construction, i.e. when <code>B</code>
is a trivial family of just one element. That means that the display map <code>p</code>
associated to <code>B</code> is the canonical map</p>

<pre><code>p : B → 1
</code></pre>

<p>So, if <code>A'</code> is any other type, we get that the pullback of <code>p</code> along the unique
map <code>r : A' → 1</code> is the product <code>B × A</code>.</p>

<p>This defines a functor</p>

<script type="math/tex; mode=display">
A^\ast : \mathrm{Set} → \mathrm{Set}/A
</script>

<p>where $\mathrm{Set}/A$ denotes the slice category of sets over <code>A</code>.
Furthermore, the dependent product and dependent sum constructions defined
above give rise to functors:</p>

<script type="math/tex; mode=display">
Σ_A, Π_A : \mathrm{Set}/A → \mathrm{Set}
</script>

<p>Now, it is clear that, given a fibration <code>p : X → A</code> and a set <code>Y</code>, functions
<code>X → Y</code> are the same as morphisms <code>X → Y × A</code> in the slice category. So $Σ_A$
is left adjoint to $A^\ast$.</p>

<p>Dually, functions from <code>Y</code> to the set of sections of <code>p</code> correspond to
functions <code>Y × A → X</code> in the slice category, thus giving an adjuction between
$A^*$ and $Π_A$.</p>

<p>So we have the following chain of adjunctions:</p>

<script type="math/tex; mode=display">
Σ_A \vdash A^* \vdash Π_A
</script>

<h2 id="conclusion">Conclusion</h2>

<p>The correspondence between indexed families and fibrations exemplified here
extends well beyond the category of sets, and can be abstracted using the
notions of Cartesian morphisms and fibred categories.</p>

<p>In type theory, it is useful to think of this correspondence when working with
models of dependently typed theories in locally cartesian closed categories,
and I hope that the examples given here show why slice categories and pullback
functors play an important role in that setting.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Continuation-based relative-time FRP]]></title>
    <link href="http://pcapriotti.github.com/blog/2012/06/04/continuation-based-relative-time-frp/"/>
    <updated>2012-06-04T19:18:00+01:00</updated>
    <id>http://pcapriotti.github.com/blog/2012/06/04/continuation-based-relative-time-frp</id>
    <content type="html"><![CDATA[<p>In a <a href="http://paolocapriotti.com/blog/2012/01/18/reinversion-of-control/">previous post</a> I showed how it is possible to write asynchronous code in a direct style using the <code>ContT</code> monad. Here, I&#8217;ll extend the idea further and present an implementation of a very simple FRP library based on continuations.</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="ot">{-# LANGUAGE DoRec, BangPatterns #-}</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> Control.Monad.IO.Class
<span class="ot">&gt;</span> <span class="kw">import</span> Data.IORef
<span class="ot">&gt;</span> <span class="kw">import</span> Data.Monoid
<span class="ot">&gt;</span> <span class="kw">import</span> Data.Void</code></pre>
<h2 id="monadic-events">Monadic events</h2>
<p>Let&#8217;s start by defining a callback-based <code>Event</code> type:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">newtype</span> <span class="dt">Event</span> a <span class="fu">=</span> <span class="dt">Event</span> {<span class="ot"> on ::</span> (a <span class="ot">-&gt;</span> <span class="dt">IO</span> ()) <span class="ot">-&gt;</span> <span class="dt">IO</span> <span class="dt">Dispose</span> }</code></pre>
<p>A value of type <code>Event a</code> represents a stream of values of type <code>a</code>, each occurring some time in the future. The <code>on</code> function connects a callback to an event, and returns an object of type <code>Dispose</code>, which can be used to disconnect from the event:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">newtype</span> <span class="dt">Dispose</span> <span class="fu">=</span> <span class="dt">Dispose</span> {<span class="ot"> dispose ::</span> <span class="dt">IO</span> () }
<span class="ot">&gt;</span> 
<span class="ot">&gt;</span> <span class="kw">instance</span> <span class="dt">Monoid</span> <span class="dt">Dispose</span> <span class="kw">where</span>
<span class="ot">&gt;</span>   mempty <span class="fu">=</span> <span class="dt">Dispose</span> (<span class="fu">return</span> ())
<span class="ot">&gt;</span>   mappend d1 d2 <span class="fu">=</span> <span class="dt">Dispose</span> <span class="fu">$</span> <span class="kw">do</span>
<span class="ot">&gt;</span>     dispose d1
<span class="ot">&gt;</span>     dispose d2</code></pre>
<p>The interesting thing about this <code>Event</code> type is that, like the simpler variant we defined in the above post, it forms a monad:</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">Event</span> <span class="kw">where</span></code></pre>
<p>First of all, given a value of type <code>a</code>, we can create an event occurring &quot;now&quot; and never again:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span>   <span class="fu">return</span> x <span class="fu">=</span> <span class="dt">Event</span> <span class="fu">$</span> \k <span class="ot">-&gt;</span> k x <span class="fu">&gt;&gt;</span> <span class="fu">return</span> mempty</code></pre>
<p>Note that the notion of &quot;time&quot; for an <code>Event</code> is relative.</p>
<p>All time-dependent notions about <code>Events</code> are formulated in terms of a particular &quot;zero&quot; time, but this origin of times is not explicitly specified.</p>
<p>This makes sense, because, even though the definition of <code>Event</code> uses the <code>IO</code> monad, an <code>Event</code> object, in itself, is an immutable value, and can be reused multiple times, possibly with different starting times.</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span>   e <span class="fu">&gt;&gt;=</span> f <span class="fu">=</span> <span class="dt">Event</span> <span class="fu">$</span> \k <span class="ot">-&gt;</span> <span class="kw">do</span>
<span class="ot">&gt;</span>     dref <span class="ot">&lt;-</span> newIORef mempty
<span class="ot">&gt;</span>     addD dref e <span class="fu">$</span> \x <span class="ot">-&gt;</span>
<span class="ot">&gt;</span>       addD dref (f x) k
<span class="ot">&gt;</span>     <span class="fu">return</span> <span class="fu">.</span> <span class="dt">Dispose</span> <span class="fu">$</span>
<span class="ot">&gt;</span>       readIORef dref <span class="fu">&gt;&gt;=</span> dispose
<span class="ot">&gt;</span> 
<span class="ot">&gt; addD ::</span> <span class="dt">IORef</span> <span class="dt">Dispose</span> <span class="ot">-&gt;</span> <span class="dt">Event</span> a <span class="ot">-&gt;</span> (a <span class="ot">-&gt;</span> <span class="dt">IO</span> ()) <span class="ot">-&gt;</span> <span class="dt">IO</span> ()
<span class="ot">&gt;</span> addD d e act <span class="fu">=</span> <span class="kw">do</span>
<span class="ot">&gt;</span>   d&#39; <span class="ot">&lt;-</span> on e act
<span class="ot">&gt;</span>   modifyIORef d (<span class="ot">`mappend`</span> d&#39;)</code></pre>
<p>The definition of <code>&gt;&gt;=</code> is slightly more involved.</p>
<p>We call the function <code>f</code> every time an event occurs, and we connect to the resulting event each time using the helper function <code>addD</code>, accumulating the corresponding <code>Dispose</code> object in an <code>IORef</code>.</p>
<p>The resulting <code>Dispose</code> object is a function that reads the <code>IORef</code> accumulator and calls <code>dispose</code> on that.</p>
<div class="figure">
<img src="http://pcapriotti.github.com/images/event_bind.png" alt="Monadic bind" /><p class="caption">Monadic bind</p>
</div>
<p>As the diagram shows, the resulting event <code>e &gt;&gt;= f</code> includes occurrences of all the events originating from the occurrences of the initial event <code>e</code>.</p>
<h2 id="event-union">Event union</h2>
<p>Classic FRP comes with a number of combinators to manipulate event streams. One of the most important is event union, which consists in merging two or more event streams into a single one.</p>
<p>In our case, event union can be implemented very easily as an <code>Alternative</code> instance:</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">Event</span> <span class="kw">where</span>
<span class="ot">&gt;</span>   <span class="fu">fmap</span> <span class="fu">=</span> liftM
<span class="ot">&gt;</span> 
<span class="ot">&gt;</span> <span class="kw">instance</span> <span class="kw">Applicative</span> <span class="dt">Event</span> <span class="kw">where</span>
<span class="ot">&gt;</span>   pure <span class="fu">=</span> <span class="fu">return</span>
<span class="ot">&gt;</span>   (<span class="fu">&lt;*&gt;</span>) <span class="fu">=</span> ap
<span class="ot">&gt;</span> 
<span class="ot">&gt;</span> <span class="kw">instance</span> <span class="dt">Alternative</span> <span class="dt">Event</span> <span class="kw">where</span>
<span class="ot">&gt;</span>   empty <span class="fu">=</span> <span class="dt">Event</span> <span class="fu">$</span> \_ <span class="ot">-&gt;</span> <span class="fu">return</span> mempty
<span class="ot">&gt;</span>   e1 <span class="fu">&lt;|&gt;</span> e2 <span class="fu">=</span> <span class="dt">Event</span> <span class="fu">$</span> \k <span class="ot">-&gt;</span> <span class="kw">do</span>
<span class="ot">&gt;</span>     d1 <span class="ot">&lt;-</span> on e1 k
<span class="ot">&gt;</span>     d2 <span class="ot">&lt;-</span> on e2 k
<span class="ot">&gt;</span>     <span class="fu">return</span> <span class="fu">$</span> d1 <span class="fu">&lt;&gt;</span> d2</code></pre>
<p>An empty <code>Event</code> never invokes its callback, and the union of two events is implemented by connecting a callback to both events simultaneously.</p>
<h2 id="other-combinators">Other combinators</h2>
<p>We need an extra primitive combinator in terms of which all other FRP combinators can be implemented using the <code>Monad</code> and <code>Alternative</code> instances.</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; once ::</span> <span class="dt">Event</span> a <span class="ot">-&gt;</span> <span class="dt">Event</span> a
<span class="ot">&gt;</span> once e <span class="fu">=</span> <span class="dt">Event</span> <span class="fu">$</span> \k <span class="ot">-&gt;</span> <span class="kw">do</span>
<span class="ot">&gt;</span>   rec d <span class="ot">&lt;-</span> on e <span class="fu">$</span> \x <span class="ot">-&gt;</span> <span class="kw">do</span>
<span class="ot">&gt;</span>              dispose d
<span class="ot">&gt;</span>              k x
<span class="ot">&gt;</span>   <span class="fu">return</span> d</code></pre>
<p>The <code>once</code> combinator truncates an event stream at its first occurrence. It can be used to implement a number of different combinators by recursion.</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; accumE ::</span> a <span class="ot">-&gt;</span> <span class="dt">Event</span> (a <span class="ot">-&gt;</span> a) <span class="ot">-&gt;</span> <span class="dt">Event</span> a
<span class="ot">&gt;</span> accumE x e <span class="fu">=</span> <span class="kw">do</span>
<span class="ot">&gt;</span>   f <span class="ot">&lt;-</span> once e
<span class="ot">&gt;</span>   <span class="kw">let</span> <span class="fu">!</span>x&#39; <span class="fu">=</span> f x
<span class="ot">&gt;</span>   pure x&#39; <span class="fu">&lt;|&gt;</span> accumE x&#39; e
<span class="ot">&gt;</span> 
<span class="ot">&gt; takeE ::</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">Event</span> a <span class="ot">-&gt;</span> <span class="dt">Event</span> a
<span class="ot">&gt;</span> takeE <span class="dv">0</span> _ <span class="fu">=</span> empty
<span class="ot">&gt;</span> takeE <span class="dv">1</span> e <span class="fu">=</span> once e
<span class="ot">&gt;</span> takeE n e <span class="fu">|</span> n <span class="fu">&gt;</span> <span class="dv">1</span> <span class="fu">=</span> <span class="kw">do</span>
<span class="ot">&gt;</span>   x <span class="ot">&lt;-</span> once e
<span class="ot">&gt;</span>   pure x <span class="fu">&lt;|&gt;</span> takeE (n <span class="fu">-</span> <span class="dv">1</span>) e
<span class="ot">&gt;</span> takeE _ _ <span class="fu">=</span> <span class="fu">error</span> <span class="st">&quot;takeE: n must be non-negative&quot;</span>
<span class="ot">&gt;</span> 
<span class="ot">&gt; dropE ::</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">Event</span> a <span class="ot">-&gt;</span> <span class="dt">Event</span> a
<span class="ot">&gt;</span> dropE n e <span class="fu">=</span> replicateM_ n (once e) <span class="fu">&gt;&gt;</span> e</code></pre>
<h2 id="behaviors-and-side-effects">Behaviors and side effects</h2>
<p>We address behaviors and side effects the same way, using <code>IO</code> actions, and a <code>MonadIO</code> instance for <code>Event</code>:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">instance</span> <span class="dt">MonadIO</span> <span class="dt">Event</span> <span class="kw">where</span>
<span class="ot">&gt;</span>   liftIO m <span class="fu">=</span> <span class="dt">Event</span> <span class="fu">$</span> \k <span class="ot">-&gt;</span> <span class="kw">do</span>
<span class="ot">&gt;</span>     m <span class="fu">&gt;&gt;=</span> k
<span class="ot">&gt;</span>     <span class="fu">return</span> mempty
<span class="ot">&gt;</span> 
<span class="ot">&gt;</span> <span class="kw">newtype</span> <span class="dt">Behavior</span> a <span class="fu">=</span> <span class="dt">Behavior</span> {<span class="ot"> valueB ::</span> <span class="dt">IO</span> a }
<span class="ot">&gt;</span> 
<span class="ot">&gt; getB ::</span> <span class="dt">Behavior</span> a <span class="ot">-&gt;</span> <span class="dt">Event</span> a
<span class="ot">&gt;</span> getB <span class="fu">=</span> liftIO <span class="fu">.</span> valueB</code></pre>
<p>Now we can implement something like the <code>apply</code> combinator in <a href="http://www.haskell.org/haskellwiki/Reactive-banana">reactive-banana</a>:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; apply ::</span> <span class="dt">Behavior</span> (a <span class="ot">-&gt;</span> b) <span class="ot">-&gt;</span> <span class="dt">Event</span> a <span class="ot">-&gt;</span> <span class="dt">Event</span> b
<span class="ot">&gt;</span> apply b e <span class="fu">=</span> <span class="kw">do</span>
<span class="ot">&gt;</span>   x <span class="ot">&lt;-</span> e
<span class="ot">&gt;</span>   f <span class="ot">&lt;-</span> getB b
<span class="ot">&gt;</span>   <span class="fu">return</span> <span class="fu">$</span> f x</code></pre>
<p>Events can also perform arbitrary <code>IO</code> actions, which is necessary to actually connect an <code>Event</code> to user-visible effects:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; log ::</span> <span class="kw">Show</span> a <span class="ot">=&gt;</span> <span class="dt">Event</span> a <span class="ot">-&gt;</span> <span class="dt">Event</span> ()
<span class="ot">&gt;</span> <span class="fu">log</span> e <span class="fu">=</span> e <span class="fu">&gt;&gt;=</span> liftIO <span class="fu">.</span> <span class="fu">print</span></code></pre>
<h2 id="executing-event-descriptions">Executing event descriptions</h2>
<p>An entire GUI program can be expressed as an <code>Event</code> value, usually by combining a number of basic events using the <code>Alternative</code> instance.</p>
<p>A complete program can be run with:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; runEvent ::</span> <span class="dt">Event</span> <span class="dt">Void</span> <span class="ot">-&gt;</span> <span class="dt">IO</span> ()
<span class="ot">&gt;</span> runEvent e <span class="fu">=</span> void <span class="fu">$</span> on e absurd
<span class="ot">&gt;</span> 
<span class="ot">&gt; runEvent_ ::</span> <span class="dt">Event</span> a <span class="ot">-&gt;</span> <span class="dt">IO</span> ()
<span class="ot">&gt;</span> runEvent_ <span class="fu">=</span> runEvent <span class="fu">.</span> (<span class="fu">&gt;&gt;</span> empty)</code></pre>
<h2 id="underlying-assumptions">Underlying assumptions</h2>
<p>For this simple system to work, events need to possess certain properties that guarantee that our implementations of the basic combinators make sense.</p>
<p>First of all, callbacks must be invoked sequentially, in the order of occurrence of their respective events.</p>
<p>Furthermore, we assume that callbacks for the same event (or simultaneous events) will be called in the order of connection.</p>
<p>Many event-driven frameworks provide those guarantees directly. For those that do not, a driver can be written converting underlying events to <code>Event</code> values satisfying the required ordering properties.</p>
<h2 id="conclusion">Conclusion</h2>
<p>It&#8217;s not immediately clear whether this approach can scale to real-world GUI applications.</p>
<p>Although the implementation presented here is quite simplistic, it could certainly be made more efficient by, for example, making <code>Dispose</code> stricter, or adding more information to <code>Event</code> to simplify some common special cases.</p>
<p>This continuation-based API is a lot more powerful than the usual FRP combinator set. The <code>Event</code> type combines the functionalities of both the classic <code>Event</code> and <code>Behavior</code> types, and it offers a wider interface (<code>Monad</code> rather than only <code>Applicative</code>).</p>
<p>On the other hand, it is a lot less safe, in a way, since it allows to freely mix <code>IO</code> actions with event descriptions, and doesn&#8217;t enforce a definite separation between the two. Libraries like reactive-banana do so by distinguishing beween &quot;network descriptions&quot; and events/behaviors.</p>
<p>Finally, there is really no sharing of intermediate events, so expensive computations occurring, say, inside an <code>accumE</code> can end up being unnecessarily performed more than once.</p>
<p>This is not just an implementation issue, but a consequence of the strict equality model that this FRP formulation employs. Even if two events are identical, they might not actually behave the same when they are used, because they are going to be &quot;activated&quot; at different times.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Pipes 2.0 vs pipes-core]]></title>
    <link href="http://pcapriotti.github.com/blog/2012/05/29/pipes-2.0-vs-pipes-core/"/>
    <updated>2012-05-29T16:37:00+01:00</updated>
    <id>http://pcapriotti.github.com/blog/2012/05/29/pipes-2.0-vs-pipes-core</id>
    <content type="html"><![CDATA[<p>With the release of <a href="http://hackage.haskell.org/package/pipes-2.0.0">pipes 2.0</a> by <a href="http://www.haskellforall.com/">Gabriel Gonzalez</a>, I feel it’s time
to address the question of whether my fork will eventually be merged or not.</p>

<p>The short answer is no, I will continue to maintain my separate incarnation
<a href="http://hackage.haskell.org/package/pipes-core">pipes-core</a>. In this post, I will discuss the reasoning behind this decision,
and hopefully explain the various trade-offs that the two libraries make.</p>

<h2 id="the-issue-with-termination">The issue with termination</h2>

<p><a href="http://hackage.haskell.org/package/pipes-1.0.2">pipes 1.0</a> can be quite accurately described as “composable monadic
stream processors”. “Composable” alludes to horizontal composition (i.e. the
<code>Category</code> instance), while “monadic” refers to vertical composition.</p>

<p>The existence of a <code>Monad</code> instance has a number of consequences, the most
important being the fact that pipes can carry a “return value”, and, in
particular, they can terminate.</p>

<p>The fact that pipes can terminate poses the greatest challenge when reasoning
about the properties of (horizontal) composition, but termination is also one
of the nicest features of pipes, so we want to deal with this difficulty
appropriately.</p>

<p>Termination implies that any pipe has to deal somehow with the fact that its
upstream pipe can exit before yielding a value, which basically means that an
<code>await</code> can fail.</p>

<p>Gabriel’s pipes address this issue by simply “propagating termination
downstream”. A pipe awaiting on a terminated pipe is forcibly terminated
itself, and the upstream return value is returned.</p>

<p>My <a href="http://paolocapriotti.com/blog/2012/02/02/guarded-pipes/">guarded pipes</a> idea (later integrated into pipes-core), proposes a new
primitive</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
</pre></td><td class="code"><pre><code class="hs"><span class="line"><span class="nf">tryAwait</span> <span class="ow">::</span> <span class="kt">Pipe</span> <span class="n">a</span> <span class="n">b</span> <span class="n">m</span> <span class="p">(</span><span class="kt">Maybe</span> <span class="n">a</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>that returns <code>Nothing</code> when upstream terminates before providing a value.</p>

<p>Using <code>tryAwait</code>, a pipe can then <em>handle</em> a failure caused by termination, and
either return a value, or use the upstream value (the latter can be
accomplished by simply awaiting again).</p>

<h2 id="exception-handling">Exception handling</h2>

<p>Once you realize that pipes should be able to handle failure on <code>await</code>, it
becomes very natural to extend the idea to other kinds of failure.</p>

<p>That’s exactly the rationale behind pipes-core. It introduces slightly more
involved primitives that take into account the fact that actions in the base
monad, as well as pipes themselves, can throw an exception at any time.</p>

<p>One very interesting consequence of built-in exception handling is that the
“guarded pipes” concept can be integrated seamlessly by introducing a special
<code>BrokenPipe</code> exception.</p>

<p>The exception handling implementation in pipes-core works in any monad, and
deals with asynchronous exceptions correctly. Of course, actual exceptions
thrown from Haskell code can only be caught when the base monad is <code>IO</code>.</p>

<h2 id="what-about-finalization">What about finalization?</h2>

<p>Since all the finalization primitives in <code>Control.Exception</code> are implemented on
top of exception handling primitives like <code>catch</code> and <code>mask</code>, I initially
believed that finalization would follow automatically from exception handling
capabilities in pipes.</p>

<p>Unfortunately, there is a fundamental operational difference between <code>IO</code> and
<code>Pipe</code>, which makes exception handling alone insufficient to guarantee
finalization of resources.</p>

<p>The problem is that some of the pipes in a pipeline are not guaranteed to be
executed at all. In fact, a pipe only plays a role in pipeline execution if its
downstream pipe awaits at some point (or if it is the last one).</p>

<p>The same applies to “portions” of pipes, so a pipe can execute partially, and
then be completely forgotten, even if no exceptional condition occurs.</p>

<p>After a number of failed attempts (including the broken <a href="http://hackage.haskell.org/package/pipes-core-0.0.1">0.0.1</a>
release of pipes-core), I realized that Gabriel’s <a href="http://www.haskellforall.com/2012/03/haskell-for-purists-pipe-finalization.html">finalizer passing</a> idea was
the right one, and used it to replace my flawed <code>ensure</code> primitive.</p>

<h2 id="balancing-safety-and-dynamicity">Balancing safety and dynamicity</h2>

<p>The question remains of how to guarantee that a pipe never awaits again after
its upstream terminated.</p>

<p>My solution is <strong>dynamic</strong>: if upstream terminated because of an exception
(that has been handled), just throw the exception again on await; if upstream
terminated normally, throw a <code>BrokenPipe</code> exception.</p>

<p>Gabriel’s solution is <strong>static</strong>: a pipe is not allowed to await again after
termination, and the invariant is enforced by the types.</p>

<p>The static solution has obvious advantages, but, on closer inspection, it
reveals a number of downsides:</p>

<ol>
  <li>It prevents <code>Pipe</code> from forming a <code>Monad</code>; the solution implemented in
<code>pipes</code> 2.0 is to separate the <code>Monad</code> instance from the <code>Category</code> instance,
and suggesting that the <code>Monad</code> instance should actually be replaced with
an <a href="http://hackage.haskell.org/package/indexed">indexed monad</a>.</li>
  <li>It doesn’t provide any exception handling mechanism, and doesn’t guarantee
that finalizers will be called in case any exception occurs. I imagine that
some sort of exception support could be layered on top of the current
solution, but I’m guessing it’s not going to be straightforward.</li>
  <li>Folds are not compositional. This can be clearly seen in the tutorial,
where <code>strict</code> is not defined in terms of <code>toList</code>.  With
pipes-core, you would simply have:</li>
</ol>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
</pre></td><td class="code"><pre><code class="hs"><span class="line"><span class="nf">strict</span> <span class="ow">=</span> <span class="n">consume</span> <span class="o">&gt;&gt;=</span> <span class="n">mapM</span> <span class="n">yield</span>
</span><span class="line"><span class="c1">-- note that toList is called consume in pipes-core</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<h2 id="whats-next-for-pipes-core">What’s next for pipes-core</h2>

<p>The current version of pipes-core already provides exception handling and
guaranteed finalization in the face of asynchronous exceptions. Things that
could be improved in its finalization support are:</p>

<ol>
  <li>Finalization is currently guaranteed, but not always prompt. When an
exception handler is provided, upstream finalization gets delayed
unnecessarily.</li>
  <li>It is not possible to prematurely force finalization. I haven’t yet seen an
example where this would be useful, but it would be nice to have it for
completeness.</li>
</ol>

<p>I think I know how these points can be addressed, and hopefully they will make
it into the next release.</p>

<p>For future releases, I’d like to focus on performance. Aside from
micro-optimizations, I can see two main areas that would benefit from
improvements: the <code>Monad</code> instance and the <code>Category</code> instance.</p>

<p>The current monadic bind unfortunately displays a quadratic behavior, since it
basically works like a naive list concatenation function. The <code>Codensity</code>
transformation should address that.</p>

<p>For the <code>Category</code> instance, it would be interesting to explore whether it is
possible to achieve some form of fusion of intermediate data structures,
similarly to classic <a href="http://www.cse.unsw.edu.au/~rl/publications/stream-fusion.html">stream fusion</a> for lists.</p>

<p>This is probably going to be more of a challenge, and will likely require some
significant restructuring, but the prospective benefits are enormous. There is
<a href="https://personal.cis.strath.ac.uk/robert.atkey/induction-with-effects.html">some</a> <a href="https://personal.cis.strath.ac.uk/robert.atkey/posts/2012-01-06-streams.html">research</a> on this topic and an <a href="https://github.com/michaelt/pipes-atkey-fusion">initial attempt</a> I plan to draw ideas
from.</p>

<p>My last point is about the absence of an <code>unawait</code> primitive for <code>Pipe</code>. There
has been quite a lot of discussion on this topic, but I remain unconvinced that
having builtin parsing capabilities is a good thing.</p>

<p>Whenever there is a need to chain unconsumed input, there are a few viable
options already:</p>

<ol>
  <li>Return leftover data, and add some manual wiring so that it’s passed to the
“next” pipe.</li>
  <li>Use <a href="https://github.com/pcapriotti/pipes-extra/blob/2caecb41705d335db80ce1c16a6d02134ed6a619/pipes-extra/Control/Pipe/PutbackPipe.hs"><code>PutbackPipe</code></a> from <a href="http://hackage.haskell.org/package/pipes-extra">pipes-extra</a>.</li>
  <li>Use an actual parser library and convert the parser to a <code>Pipe</code> (see
<a href="http://hackage.haskell.org/package/pipes-attoparsec">pipes-attoparsec</a>).</li>
</ol>

<p>In all the examples I have seen, however, pipes are composable enough that all
the special logic to deal with boundaries of chunked streams can be implemented
in a single “filter” pipe, and the rest of the pipeline can ignore the issue
altogether.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Applicative option parser]]></title>
    <link href="http://pcapriotti.github.com/blog/2012/04/27/applicative-option-parser/"/>
    <updated>2012-04-27T13:40:00+01:00</updated>
    <id>http://pcapriotti.github.com/blog/2012/04/27/applicative-option-parser</id>
    <content type="html"><![CDATA[<p>There are quite a few option parsing libraries on Hackage already, but they either depend on Template Haskell, or require some boilerplate. Although I have nothing against the use of Template Haskell in general, I&#8217;ve always found its use in this case particularly unsatisfactory, and I&#8217;m convinced that a more idiomatic solution should exist.</p>
<p>In this post, I present a proof of concept implementation of a library that allows you to define type-safe option parsers in Applicative style.</p>
<p>The only extension that we actually need is <code>GADT</code>, since, as will be clear in a moment, our definition of <code>Parser</code> requires existential quantification.</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="ot">{-# LANGUAGE GADTs #-}</span>
<span class="ot">&gt;</span> <span class="kw">import</span> Control.Applicative</code></pre>
<p>Let&#8217;s start by defining the <code>Option</code> type, corresponding to a concrete parser for a single option:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">data</span> <span class="dt">Option</span> a <span class="fu">=</span> <span class="dt">Option</span>
<span class="ot">&gt;</span>   {<span class="ot"> optName ::</span> <span class="dt">String</span>
<span class="ot">&gt;</span>   ,<span class="ot"> optParser ::</span> <span class="dt">String</span> <span class="ot">-&gt;</span> <span class="dt">Maybe</span> a
<span class="ot">&gt;</span>   }
<span class="ot">&gt;</span> 
<span class="ot">&gt;</span> <span class="kw">instance</span> <span class="kw">Functor</span> <span class="dt">Option</span> <span class="kw">where</span>
<span class="ot">&gt;</span>   <span class="fu">fmap</span> f (<span class="dt">Option</span> name p) <span class="fu">=</span> <span class="dt">Option</span> name (<span class="fu">fmap</span> f <span class="fu">.</span> p)
<span class="ot">&gt;</span> 
<span class="ot">&gt; optMatches ::</span> <span class="dt">Option</span> a <span class="ot">-&gt;</span> <span class="dt">String</span> <span class="ot">-&gt;</span> <span class="dt">Bool</span>
<span class="ot">&gt;</span> optMatches opt s <span class="fu">=</span> s <span class="fu">==</span> <span class="ch">&#39;-&#39;</span> <span class="fu">:</span> <span class="ch">&#39;-&#39;</span> <span class="fu">:</span> optName opt</code></pre>
<p>For simplicity, we only support &quot;long&quot; options with exactly 1 argument. The <code>optMatches</code> function checks if an option matches a string given on the command line.</p>
<p>We can now define the main <code>Parser</code> type:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">data</span> <span class="dt">Parser</span> a <span class="kw">where</span>
<span class="ot">&gt;</span>   <span class="dt">NilP</span><span class="ot"> ::</span> a <span class="ot">-&gt;</span> <span class="dt">Parser</span> a
<span class="ot">&gt;</span>   <span class="dt">ConsP</span><span class="ot"> ::</span> <span class="dt">Option</span> (a <span class="ot">-&gt;</span> b)
<span class="ot">&gt;</span>         <span class="ot">-&gt;</span> <span class="dt">Parser</span> a <span class="ot">-&gt;</span> <span class="dt">Parser</span> b
<span class="ot">&gt;</span> 
<span class="ot">&gt;</span> <span class="kw">instance</span> <span class="kw">Functor</span> <span class="dt">Parser</span> <span class="kw">where</span>
<span class="ot">&gt;</span>   <span class="fu">fmap</span> f (<span class="dt">NilP</span> x) <span class="fu">=</span> <span class="dt">NilP</span> (f x)
<span class="ot">&gt;</span>   <span class="fu">fmap</span> f (<span class="dt">ConsP</span> opt rest) <span class="fu">=</span> <span class="dt">ConsP</span> (<span class="fu">fmap</span> (f<span class="fu">.</span>) opt) rest
<span class="ot">&gt;</span> 
<span class="ot">&gt;</span> <span class="kw">instance</span> <span class="kw">Applicative</span> <span class="dt">Parser</span> <span class="kw">where</span>
<span class="ot">&gt;</span>   pure <span class="fu">=</span> <span class="dt">NilP</span>
<span class="ot">&gt;</span>   <span class="dt">NilP</span> f <span class="fu">&lt;*&gt;</span> p <span class="fu">=</span> <span class="fu">fmap</span> f p
<span class="ot">&gt;</span>   <span class="dt">ConsP</span> opt rest <span class="fu">&lt;*&gt;</span> p <span class="fu">=</span>
<span class="ot">&gt;</span>     <span class="dt">ConsP</span> (<span class="fu">fmap</span> <span class="fu">uncurry</span> opt) ((,) <span class="fu">&lt;$&gt;</span> rest <span class="fu">&lt;*&gt;</span> p)</code></pre>
<p>The <code>Parser</code> GADT resembles a heterogeneous list, with two constructors.</p>
<p>The <code>NilP r</code> constructor represents a &quot;null&quot; parser that doesn&#8217;t consume any arguments, and always returns <code>r</code> as a result.</p>
<p>The <code>ConsP</code> constructor is the combination of an <code>Option</code> returning a function, and an arbitrary parser returning an argument for that function. The combined parser applies the function to the argument and returns a result.</p>
<p>The definition of <code>(&lt;*&gt;)</code> probably needs some clarification. The variables involved have types:</p>
<div class='bogus-wrapper'>
<notextile>
<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='hs'><span class='line'><span class="nf">opt</span> <span class="ow">::</span> <span class="kt">Option</span> <span class="p">(</span><span class="n">a</span> <span class="ow">-&gt;</span> <span class="n">b</span> <span class="ow">-&gt;</span> <span class="n">c</span><span class="p">)</span>
</span><span class='line'><span class="nf">rest</span> <span class="ow">::</span> <span class="kt">Parser</span> <span class="n">a</span>
</span><span class='line'><span class="nf">p</span> <span class="ow">::</span> <span class="kt">Parser</span> <span class="n">b</span>
</span></code></pre></td></tr></table></div></figure>
</notextile>
</div>

<p>and we want to obtain a parser of type <code>Parser c</code>. So we <code>uncurry</code> the option, obtaining:</p>
<div class='bogus-wrapper'>
<notextile>
<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='hs'><span class='line'><span class="nf">fmap</span> <span class="n">uncurry</span> <span class="n">opt</span> <span class="ow">::</span> <span class="kt">Option</span> <span class="p">((</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span> <span class="ow">-&gt;</span> <span class="n">c</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>
</notextile>
</div>

<p>and compose it with a parser for the <code>(a, b)</code> pair, obtained by applying the <code>(&lt;*&gt;)</code> operator recursively:</p>
<div class='bogus-wrapper'>
<notextile>
<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='hs'><span class='line'><span class="p">(,)</span> <span class="o">&lt;$&gt;</span> <span class="n">rest</span> <span class="o">&lt;*&gt;</span> <span class="n">p</span> <span class="ow">::</span> <span class="kt">Parser</span> <span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>
</notextile>
</div>

<p>This is already enough to define some example parsers. Let&#8217;s first add a couple of convenience functions to help us create basic parsers:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; option ::</span> <span class="dt">String</span> <span class="ot">-&gt;</span> (<span class="dt">String</span> <span class="ot">-&gt;</span> <span class="dt">Maybe</span> a) <span class="ot">-&gt;</span> <span class="dt">Parser</span> a
<span class="ot">&gt;</span> option name p <span class="fu">=</span> <span class="dt">ConsP</span> (<span class="fu">fmap</span> <span class="fu">const</span> (<span class="dt">Option</span> name p)) (<span class="dt">NilP</span> ())</code></pre>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; optionR ::</span> <span class="kw">Read</span> a <span class="ot">=&gt;</span> <span class="dt">String</span> <span class="ot">-&gt;</span> <span class="dt">Parser</span> a
<span class="ot">&gt;</span> optionR name <span class="fu">=</span> option name p
<span class="ot">&gt;</span>   <span class="kw">where</span>
<span class="ot">&gt;</span>     p arg <span class="fu">=</span> <span class="kw">case</span> <span class="fu">reads</span> arg <span class="kw">of</span>
<span class="ot">&gt;</span>       [(r, <span class="st">&quot;&quot;</span>)] <span class="ot">-&gt;</span> <span class="kw">Just</span> r
<span class="ot">&gt;</span>       _       <span class="ot">-&gt;</span> <span class="kw">Nothing</span></code></pre>
<p>And a record to contain the result of our parser:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">data</span> <span class="dt">User</span> <span class="fu">=</span> <span class="dt">User</span>
<span class="ot">&gt;</span>   {<span class="ot"> userName ::</span> <span class="dt">String</span>
<span class="ot">&gt;</span>   ,<span class="ot"> userId ::</span> <span class="dt">Integer</span>
<span class="ot">&gt;</span>   } <span class="kw">deriving</span> <span class="kw">Show</span></code></pre>
<p>A parser for <code>User</code> is easily defined in applicative style:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; parser ::</span> <span class="dt">Parser</span> <span class="dt">User</span>
<span class="ot">&gt;</span> parser <span class="fu">=</span> <span class="dt">User</span> <span class="fu">&lt;$&gt;</span> option <span class="st">&quot;name&quot;</span> <span class="kw">Just</span> <span class="fu">&lt;*&gt;</span> optionR <span class="st">&quot;id&quot;</span></code></pre>
<p>To be able to actually use this parser, we need a &quot;run&quot; function:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; runParser ::</span> <span class="dt">Parser</span> a <span class="ot">-&gt;</span> [<span class="dt">String</span>] <span class="ot">-&gt;</span> <span class="dt">Maybe</span> (a, [<span class="dt">String</span>])
<span class="ot">&gt;</span> runParser (<span class="dt">NilP</span> x) args <span class="fu">=</span> <span class="kw">Just</span> (x, args)
<span class="ot">&gt;</span> runParser (<span class="dt">ConsP</span> _ _) [] <span class="fu">=</span> <span class="kw">Nothing</span>
<span class="ot">&gt;</span> runParser p (arg <span class="fu">:</span> args) <span class="fu">=</span>
<span class="ot">&gt;</span>   <span class="kw">case</span> stepParser p arg args <span class="kw">of</span>
<span class="ot">&gt;</span>     <span class="kw">Nothing</span> <span class="ot">-&gt;</span> <span class="kw">Nothing</span>
<span class="ot">&gt;</span>     <span class="kw">Just</span> (p&#39;, args&#39;) <span class="ot">-&gt;</span> runParser p&#39; args&#39;
<span class="ot">&gt;</span> 
<span class="ot">&gt; stepParser ::</span> <span class="dt">Parser</span> a <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> <span class="dt">Maybe</span> (<span class="dt">Parser</span> a, [<span class="dt">String</span>])
<span class="ot">&gt;</span> stepParser p arg args <span class="fu">=</span> <span class="kw">case</span> p <span class="kw">of</span>
<span class="ot">&gt;</span>   <span class="dt">NilP</span> _ <span class="ot">-&gt;</span> <span class="kw">Nothing</span>
<span class="ot">&gt;</span>   <span class="dt">ConsP</span> opt rest
<span class="ot">&gt;</span>     <span class="fu">|</span> optMatches opt arg <span class="ot">-&gt;</span> <span class="kw">case</span> args <span class="kw">of</span>
<span class="ot">&gt;</span>         [] <span class="ot">-&gt;</span> <span class="kw">Nothing</span>
<span class="ot">&gt;</span>         (value <span class="fu">:</span> args&#39;) <span class="ot">-&gt;</span> <span class="kw">do</span>
<span class="ot">&gt;</span>           f <span class="ot">&lt;-</span> optParser opt value
<span class="ot">&gt;</span>           <span class="fu">return</span> (<span class="fu">fmap</span> f rest, args&#39;)
<span class="ot">&gt;</span>     <span class="fu">|</span> <span class="fu">otherwise</span> <span class="ot">-&gt;</span> <span class="kw">do</span>
<span class="ot">&gt;</span>         (rest&#39;, args&#39;) <span class="ot">&lt;-</span> stepParser rest arg args
<span class="ot">&gt;</span>         <span class="fu">return</span> (<span class="dt">ConsP</span> opt rest&#39;, args&#39;)</code></pre>
<p>The idea is very simple: we take the first argument, and we go over each option of the parser, check if it matches, and if it does, we replace it with a <code>NilP</code> parser wrapping the result, consume the option and its argument from the argument list, then call <code>runParser</code> recursively.</p>
<p>Here is an example of <code>runParser</code> in action:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; ex1 ::</span> <span class="dt">Maybe</span> <span class="dt">User</span>
<span class="ot">&gt;</span> ex1 <span class="fu">=</span> <span class="fu">fst</span> <span class="fu">&lt;$&gt;</span> runParser parser [<span class="st">&quot;--name&quot;</span>, <span class="st">&quot;fry&quot;</span>, <span class="st">&quot;--id&quot;</span>, <span class="st">&quot;1&quot;</span>]
<span class="ot">&gt;</span> <span class="co">{- Just (User {userName = &quot;fry&quot;, userId = 1}) -}</span></code></pre>
<p>The order of arguments doesn&#8217;t matter:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; ex2 ::</span> <span class="dt">Maybe</span> <span class="dt">User</span>
<span class="ot">&gt;</span> ex2 <span class="fu">=</span> <span class="fu">fst</span> <span class="fu">&lt;$&gt;</span> runParser parser [<span class="st">&quot;--id&quot;</span>, <span class="st">&quot;2&quot;</span>, <span class="st">&quot;--name&quot;</span>, <span class="st">&quot;bender&quot;</span>]
<span class="ot">&gt;</span> <span class="co">{- Just (User {userName = &quot;bender&quot;, userId = 2}) -}</span></code></pre>
<p>Missing arguments will result in a parse error (i.e. <code>Nothing</code>). We don&#8217;t support default values but they are pretty easy to add.</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; ex3 ::</span> <span class="dt">Maybe</span> <span class="dt">User</span>
<span class="ot">&gt;</span> ex3 <span class="fu">=</span> <span class="fu">fst</span> <span class="fu">&lt;$&gt;</span> runParser parser [<span class="st">&quot;--name&quot;</span>, <span class="st">&quot;leela&quot;</span>]
<span class="ot">&gt;</span> <span class="co">{- Nothing -}</span></code></pre>
<p>I think the above <code>Parser</code> type represents a pretty clean and elegant solution to the option parsing problem. To make it actually usable, I would need to add a few more features (boolean flags, default values, a help generator) and improve error handling and performance (right now parsing a single option is quadratic in the size of the <code>Parser</code>), but it looks like a fun project.</p>
<p>Does anyone think it&#8217;s worth adding yet another option parser to Hackage?</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Monoidal instances for pipes]]></title>
    <link href="http://pcapriotti.github.com/blog/2012/02/04/monoidal-instances-for-pipes/"/>
    <updated>2012-02-04T12:00:00+00:00</updated>
    <id>http://pcapriotti.github.com/blog/2012/02/04/monoidal-instances-for-pipes</id>
    <content type="html"><![CDATA[<p>In this post, I&#8217;m going to introduce a new class of combinators for pipes, with an interesting categorical interpretation. I will be using the pipe implementation of my <a href="http://pcapriotti.wordpress.com/2012/02/02/an-introduction-to-guarded-pipes/">previous post</a>.</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="ot">{-# LANGUAGE MultiParamTypeClasses #-}</span>
<span class="ot">&gt;</span> <span class="ot">{-# LANGUAGE FlexibleInstances #-}</span>
<span class="ot">&gt;</span> <span class="ot">{-# LANGUAGE TypeFamilies #-}</span>
<span class="ot">&gt;</span> <span class="ot">{-# LANGUAGE GeneralizedNewtypeDeriving #-}</span>
<span class="ot">&gt;</span> <span class="kw">module</span> <span class="dt">Blog.Pipes.MonoidalInstances</span> <span class="kw">where</span>
<span class="ot">&gt;</span> 
<span class="ot">&gt;</span> <span class="kw">import</span> Blog.Pipes.Guarded <span class="kw">hiding</span> (groupBy)
<span class="ot">&gt;</span> <span class="kw">import</span> <span class="kw">qualified</span> Control.Arrow <span class="kw">as</span> A
<span class="ot">&gt;</span> <span class="kw">import</span> Control.Category
<span class="ot">&gt;</span> <span class="kw">import</span> Control.Categorical.Bifunctor
<span class="ot">&gt;</span> <span class="kw">import</span> Control.Category.Associative
<span class="ot">&gt;</span> <span class="kw">import</span> Control.Category.Braided
<span class="ot">&gt;</span> <span class="kw">import</span> Control.Category.Monoidal
<span class="ot">&gt;</span> <span class="kw">import</span> Control.Monad (forever)
<span class="ot">&gt;</span> <span class="kw">import</span> Control.Monad.Free
<span class="ot">&gt;</span> <span class="kw">import</span> Data.Maybe
<span class="ot">&gt;</span> <span class="kw">import</span> Data.Void
<span class="ot">&gt;</span> <span class="kw">import</span> Prelude <span class="kw">hiding</span> ((.), id, filter, until)</code></pre>
<p>When pipes were first released, some people noticed the lack of an <code>Arrow</code> instance. In fact, it is not hard to show that, even identifying pipes modulo some sort of observational equality, there is no <code>Arrow</code> instance that satisfies the arrow laws.</p>
<p>The problem, of course, is with <code>first</code>, because we already have a simple implementation of <code>arr</code>. If we try to implement <code>first</code> we immediately discover that there&#8217;s a problem with the <code>Yield</code> case:</p>
<pre class="sourceCode haskell"><code class="sourceCode haskell">first (<span class="dt">Yield</span> x c) <span class="fu">=</span> yield (x, <span class="fu">???</span>) <span class="fu">&gt;&gt;</span> first c</code></pre>
<p>Since ??? can be of any type, the only possible value is bottom, which of course we don&#8217;t want to introduce. Alternative definitions of <code>first</code> that alter the structure of a yielding pipe are not possible if we want to satisfy the law:</p>
<pre class="sourceCode haskell"><code class="sourceCode haskell">first p <span class="fu">&gt;+&gt;</span> pipe <span class="fu">fst</span> <span class="fu">==</span> pipe <span class="fu">fst</span> <span class="fu">&gt;+&gt;</span> p</code></pre>
<p>Concretely, the problem is that the cartesian product in the type of <code>first</code> forces a sort of &quot;synchronization point&quot; that doesn&#8217;t necessarily exist. This is better understood if we look at the type of <code>(***)</code>, of which <code>first</code> can be thought of as a special case:</p>
<pre class="sourceCode haskell"><code class="sourceCode haskell"><span class="ot">(***) ::</span> <span class="dt">Arrow</span> k <span class="ot">=&gt;</span> k a b <span class="ot">-&gt;</span> k a&#39; b&#39; <span class="ot">-&gt;</span> k (a, a&#39;) (b, b&#39;)

first <span class="fu">=</span> (<span class="fu">***</span> <span class="fu">id</span>)</code></pre>
<p>If the two input pipes yield at different times, there is no way to faithfully match their yielded values into a pair. There are hacks around that, but they don&#8217;t behave well compositionally, and exhibit either arbitrarily large space leaks or data loss.</p>
<p>This has been addressed before: stream processors, like those of the <a href="http://www.altocumulus.org/Fudgets/">Fudgets</a> library, being very similar to Pipes, have the same problem, and <a href="http://www.google.ie/url?sa=t&amp;rct=j&amp;q=prodarrow+ps&amp;source=web&amp;cd=1&amp;ved=0CB8QFjAA&amp;url=http%3A%2F%2Fwww.carlssonia.org%2Fogi%2Fpapers%2Farrows.ps.gz&amp;ei=WXgqT-6pK5CzhAfZ_9DWCg&amp;usg=AFQjCNESuOahh6WeEsPTJoHHUbO8J858mQ&amp;sig2=Kffi1WwxedHlbogGNdUFRg">some resolutions</a> have been proposed, although not entirely satisfactory.</p>
<h2 id="arrows-as-monoidal-categories">Arrows as monoidal categories</h2>
<p>It is well known within the Haskell community that Arrows correspond to so called Freyd categories, i.e. premonoidal categories with some extra structures.</p>
<p>Using the <code>Monoidal</code> class by Edward Kmett (now in the <code>categories</code> package on Hackage), we can try to make this idea precise.</p>
<p>Unfortunately, we have to use a newtype to avoid overlapping instances in the case of the Hask category:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">newtype</span> <span class="dt">ACat</span> a b c <span class="fu">=</span> <span class="dt">ACat</span> {<span class="ot"> unACat ::</span> a b c }
<span class="ot">&gt;</span>   <span class="kw">deriving</span> (<span class="dt">Category</span>, <span class="dt">A.Arrow</span>)</code></pre>
<p>First, cartesian products are a bifunctor in the category determined by an Arrow.</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">instance</span> <span class="dt">A.Arrow</span> a <span class="ot">=&gt;</span> <span class="dt">PFunctor</span> (,) (<span class="dt">ACat</span> a) (<span class="dt">ACat</span> a) <span class="kw">where</span>
<span class="ot">&gt;</span>   first <span class="fu">=</span> <span class="dt">ACat</span> <span class="fu">.</span> A.first <span class="fu">.</span> unACat
<span class="ot">&gt;</span> <span class="kw">instance</span> <span class="dt">A.Arrow</span> a <span class="ot">=&gt;</span> <span class="dt">QFunctor</span> (,) (<span class="dt">ACat</span> a) (<span class="dt">ACat</span> a) <span class="kw">where</span>
<span class="ot">&gt;</span>   second <span class="fu">=</span> <span class="dt">ACat</span> <span class="fu">.</span> A.second <span class="fu">.</span> unACat
<span class="ot">&gt;</span> <span class="kw">instance</span> <span class="dt">A.Arrow</span> a
<span class="ot">&gt;</span>       <span class="ot">=&gt;</span> <span class="dt">Bifunctor</span> (,) (<span class="dt">ACat</span> a) (<span class="dt">ACat</span> a) (<span class="dt">ACat</span> a) <span class="kw">where</span>
<span class="ot">&gt;</span>   bimap (<span class="dt">ACat</span> f) (<span class="dt">ACat</span> g) <span class="fu">=</span> <span class="dt">ACat</span> <span class="fu">$</span> f <span class="fu">A.***</span> g</code></pre>
<p>Now we can say that products are associative, using the associativity of products in Hask:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">instance</span> <span class="dt">A.Arrow</span> a <span class="ot">=&gt;</span> <span class="dt">Associative</span> (<span class="dt">ACat</span> a) (,) <span class="kw">where</span>
<span class="ot">&gt;</span>   associate <span class="fu">=</span> <span class="dt">ACat</span> <span class="fu">$</span> A.arr associate
<span class="ot">&gt;</span> <span class="kw">instance</span> <span class="dt">A.Arrow</span> a <span class="ot">=&gt;</span> <span class="dt">Disassociative</span> (<span class="dt">ACat</span> a) (,) <span class="kw">where</span>
<span class="ot">&gt;</span>   disassociate <span class="fu">=</span> <span class="dt">ACat</span> <span class="fu">$</span> A.arr disassociate</code></pre>
<p>Where we use the <code>Disassociative</code> instance to express the inverse of the associator. And finally, the Monoidal instance:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">Id</span> (<span class="dt">ACat</span> a) (,) <span class="fu">=</span> ()
<span class="ot">&gt;</span> <span class="kw">instance</span> <span class="dt">A.Arrow</span> a <span class="ot">=&gt;</span> <span class="dt">Monoidal</span> (<span class="dt">ACat</span> a) (,) <span class="kw">where</span>
<span class="ot">&gt;</span>   idl <span class="fu">=</span> <span class="dt">ACat</span> <span class="fu">$</span> A.arr idl
<span class="ot">&gt;</span>   idr <span class="fu">=</span> <span class="dt">ACat</span> <span class="fu">$</span> A.arr idr
<span class="ot">&gt;</span> <span class="kw">instance</span> <span class="dt">A.Arrow</span> a <span class="ot">=&gt;</span> <span class="dt">Comonoidal</span> (<span class="dt">ACat</span> a) (,) <span class="kw">where</span>
<span class="ot">&gt;</span>   coidl <span class="fu">=</span> <span class="dt">ACat</span> <span class="fu">$</span> A.arr coidl
<span class="ot">&gt;</span>   coidr <span class="fu">=</span> <span class="dt">ACat</span> <span class="fu">$</span> A.arr coidr</code></pre>
<p>Where, again, the duals are actually inverses. Also, products are symmetric:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">instance</span> <span class="dt">A.Arrow</span> a <span class="ot">=&gt;</span> <span class="dt">Braided</span> (<span class="dt">ACat</span> a) (,) <span class="kw">where</span>
<span class="ot">&gt;</span>   braid <span class="fu">=</span> <span class="dt">ACat</span> <span class="fu">$</span> A.arr braid
<span class="ot">&gt;</span> <span class="kw">instance</span> <span class="dt">A.Arrow</span> a <span class="ot">=&gt;</span> <span class="dt">Symmetric</span> (<span class="dt">ACat</span> a) (,)</code></pre>
<p>As you see, everything is trivially induced by the cartesian structure on Hask, since <code>A.arr</code> gives us an identity-on-objects functor. Note, however, that the <code>Bifunctor</code> instance is legitimate only if we assume a strong commutativity law for arrows:</p>
<pre class="sourceCode haskell"><code class="sourceCode haskell">first f <span class="fu">&gt;&gt;&gt;</span> second g <span class="fu">==</span> second g <span class="fu">&gt;&gt;&gt;</span> first f</code></pre>
<p>which we will, for the sake of simplicity.</p>
<h2 id="replacing-products-with-arbitrary-monoidal-structures">Replacing products with arbitrary monoidal structures</h2>
<p>Once we express the Arrow concept in terms of monoidal categories, it is easy to generalize it to arbitrary monoidal structures on Hask.</p>
<p>In particular, coproducts work particularly well in the category of pipes:</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> m
<span class="ot">&gt;</span>       <span class="ot">=&gt;</span> <span class="dt">PFunctor</span> <span class="dt">Either</span> (<span class="dt">PipeC</span> m r) (<span class="dt">PipeC</span> m r) <span class="kw">where</span>
<span class="ot">&gt;</span>   first <span class="fu">=</span> <span class="dt">PipeC</span> <span class="fu">.</span> firstP <span class="fu">.</span> unPipeC
<span class="ot">&gt;</span> 
<span class="ot">&gt; firstP ::</span> <span class="kw">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Pipe</span> a b m r
<span class="ot">&gt;</span>        <span class="ot">-&gt;</span> <span class="dt">Pipe</span> (<span class="dt">Either</span> a c) (<span class="dt">Either</span> b c) m r
<span class="ot">&gt;</span> firstP (<span class="dt">Pure</span> r) <span class="fu">=</span> <span class="fu">return</span> r
<span class="ot">&gt;</span> firstP (<span class="dt">Free</span> (<span class="dt">M</span> m)) <span class="fu">=</span> lift m <span class="fu">&gt;&gt;=</span> firstP</code></pre>
<p>Yielding a sum is now easy: just yield on the left component.</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> firstP (<span class="dt">Free</span> (<span class="dt">Yield</span> x c)) <span class="fu">=</span> yield (<span class="kw">Left</span> x) <span class="fu">&gt;&gt;</span> firstP c</code></pre>
<p>Awaiting is a little bit more involved, but still easy enough: receive left and null values normally, and act like an identity on the right.</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> firstP (<span class="dt">Free</span> (<span class="dt">Await</span> k)) <span class="fu">=</span> go
<span class="ot">&gt;</span>         <span class="kw">where</span>
<span class="ot">&gt;</span>           go <span class="fu">=</span> tryAwait
<span class="ot">&gt;</span>            <span class="fu">&gt;&gt;=</span> <span class="fu">maybe</span> (firstP <span class="fu">$</span> k <span class="kw">Nothing</span>)
<span class="ot">&gt;</span>                      (<span class="fu">either</span> (firstP <span class="fu">.</span> k <span class="fu">.</span> <span class="kw">Just</span>)
<span class="ot">&gt;</span>                              (\x <span class="ot">-&gt;</span> yield (<span class="kw">Right</span> x) <span class="fu">&gt;&gt;</span> go))</code></pre>
<p>And of course we have an analogous instance on the right:</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> m
<span class="ot">&gt;</span>       <span class="ot">=&gt;</span> <span class="dt">QFunctor</span> <span class="dt">Either</span> (<span class="dt">PipeC</span> m r) (<span class="dt">PipeC</span> m r) <span class="kw">where</span>
<span class="ot">&gt;</span>   second <span class="fu">=</span> <span class="dt">PipeC</span> <span class="fu">.</span> secondP <span class="fu">.</span> unPipeC
<span class="ot">&gt;</span> 
<span class="ot">&gt; secondP ::</span> <span class="kw">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Pipe</span> a b m r
<span class="ot">&gt;</span>         <span class="ot">-&gt;</span> <span class="dt">Pipe</span> (<span class="dt">Either</span> c a) (<span class="dt">Either</span> c b) m r
<span class="ot">&gt;</span> secondP (<span class="dt">Pure</span> r) <span class="fu">=</span> <span class="fu">return</span> r
<span class="ot">&gt;</span> secondP (<span class="dt">Free</span> (<span class="dt">M</span> m)) <span class="fu">=</span> lift m <span class="fu">&gt;&gt;=</span> secondP
<span class="ot">&gt;</span> secondP (<span class="dt">Free</span> (<span class="dt">Yield</span> x c)) <span class="fu">=</span> yield (<span class="kw">Right</span> x) <span class="fu">&gt;&gt;</span> secondP c
<span class="ot">&gt;</span> secondP (<span class="dt">Free</span> (<span class="dt">Await</span> k)) <span class="fu">=</span> go
<span class="ot">&gt;</span>         <span class="kw">where</span>
<span class="ot">&gt;</span>           go <span class="fu">=</span> tryAwait
<span class="ot">&gt;</span>            <span class="fu">&gt;&gt;=</span> <span class="fu">maybe</span> (secondP <span class="fu">$</span> k <span class="kw">Nothing</span>)
<span class="ot">&gt;</span>                      (<span class="fu">either</span> (\x <span class="ot">-&gt;</span> yield (<span class="kw">Left</span> x) <span class="fu">&gt;&gt;</span> go)
<span class="ot">&gt;</span>                              (secondP <span class="fu">.</span> k <span class="fu">.</span> <span class="kw">Just</span>))</code></pre>
<p>And a bifunctor instance obtained by composing <code>first</code> and <code>second</code> in arbitrary order:</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> m
<span class="ot">&gt;</span>       <span class="ot">=&gt;</span> <span class="dt">Bifunctor</span> <span class="dt">Either</span> (<span class="dt">PipeC</span> m r)
<span class="ot">&gt;</span>                    (<span class="dt">PipeC</span> m r) (<span class="dt">PipeC</span> m r) <span class="kw">where</span>
<span class="ot">&gt;</span>   bimap f g <span class="fu">=</span> first f <span class="fu">&gt;&gt;&gt;</span> second g</code></pre>
<p>At this point we can go ahead and define the remaining instances in terms of the identity-on-objects functor given by <code>pipe</code>:</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> m <span class="ot">=&gt;</span> <span class="dt">Associative</span> (<span class="dt">PipeC</span> m r) <span class="dt">Either</span> <span class="kw">where</span>
<span class="ot">&gt;</span>   associate <span class="fu">=</span> <span class="dt">PipeC</span> <span class="fu">$</span> pipe associate
<span class="ot">&gt;</span> <span class="kw">instance</span> <span class="kw">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Disassociative</span> (<span class="dt">PipeC</span> m r) <span class="dt">Either</span> <span class="kw">where</span>
<span class="ot">&gt;</span>   disassociate <span class="fu">=</span> <span class="dt">PipeC</span> <span class="fu">$</span> pipe disassociate
<span class="ot">&gt;</span> 
<span class="ot">&gt;</span> <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">Id</span> (<span class="dt">PipeC</span> m r) <span class="dt">Either</span> <span class="fu">=</span> <span class="dt">Void</span>
<span class="ot">&gt;</span> <span class="kw">instance</span> <span class="kw">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Monoidal</span> (<span class="dt">PipeC</span> m r) <span class="dt">Either</span> <span class="kw">where</span>
<span class="ot">&gt;</span>   idl <span class="fu">=</span> <span class="dt">PipeC</span> <span class="fu">$</span> pipe idl
<span class="ot">&gt;</span>   idr <span class="fu">=</span> <span class="dt">PipeC</span> <span class="fu">$</span> pipe idr
<span class="ot">&gt;</span> <span class="kw">instance</span> <span class="kw">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Comonoidal</span> (<span class="dt">PipeC</span> m r) <span class="dt">Either</span> <span class="kw">where</span>
<span class="ot">&gt;</span>   coidl <span class="fu">=</span> <span class="dt">PipeC</span> <span class="fu">$</span> pipe coidl
<span class="ot">&gt;</span>   coidr <span class="fu">=</span> <span class="dt">PipeC</span> <span class="fu">$</span> pipe coidr
<span class="ot">&gt;</span> 
<span class="ot">&gt;</span> <span class="kw">instance</span> <span class="kw">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Braided</span> (<span class="dt">PipeC</span> m r) <span class="dt">Either</span> <span class="kw">where</span>
<span class="ot">&gt;</span>   braid <span class="fu">=</span> <span class="dt">PipeC</span> <span class="fu">$</span> pipe braid
<span class="ot">&gt;</span> <span class="kw">instance</span> <span class="kw">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Symmetric</span> (<span class="dt">PipeC</span> m r) <span class="dt">Either</span></code></pre>
<h2 id="multiplicative-structures">Multiplicative structures</h2>
<p>There is still a little bit of extra structure that we might want to exploit. Since <code>PipeC m r</code> is a monoidal category, it induces a (pointwise) monoidal structure on its endofunctor category, so we can speak of monoid objects there. In particular, if the identity functor is a monoid, it means that we can define a &quot;uniform&quot; monoid structure for all the objects of our category, given in terms of natural transformations (i.e. polymorphic functions).</p>
<p>We can represent this specialized monoid structure with a type class (using kind polymorphism and appropriately generalized category-related type classes, it should be possible to unify this class with <code>Monoid</code> and even <code>Monad</code>, similarly to how it&#8217;s done <a href="http://www.jonmsterling.com/posts/2012-01-12-unifying-monoids-and-monads-with-polymorphic-kinds.html">here</a>):</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">class</span> <span class="dt">Monoidal</span> k p <span class="ot">=&gt;</span> <span class="dt">Multiplicative</span> k p <span class="kw">where</span>
<span class="ot">&gt;   unit ::</span> k (<span class="dt">Id</span> k p) a
<span class="ot">&gt;   mult ::</span> k (p a a) a</code></pre>
<p>Dually, we can have a sort of uniform coalgebra:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">class</span> <span class="dt">Comonoidal</span> k p <span class="ot">=&gt;</span> <span class="dt">Comultiplicative</span> k p <span class="kw">where</span>
<span class="ot">&gt;   counit ::</span> k a (<span class="dt">Id</span> k p)
<span class="ot">&gt;   comult ::</span> k a (p a a)</code></pre>
<p>The laws for those type classes are just the usual laws for a monoid in a (not necessarily strict) monoidal category:</p>
<pre class="sourceCode haskell"><code class="sourceCode haskell">first unit <span class="fu">.</span> mult <span class="fu">==</span> idl
second unit <span class="fu">.</span> mult <span class="fu">==</span> idr
mult <span class="fu">.</span> first mult <span class="fu">==</span> mult <span class="fu">.</span> second mult <span class="fu">.</span> associate

first counit <span class="fu">.</span> comult <span class="fu">==</span> coidl
second counit <span class="fu">.</span> comult <span class="fu">==</span> coidr
first diag <span class="fu">.</span> diag <span class="fu">==</span> disassociate <span class="fu">.</span> second diag <span class="fu">.</span> diag</code></pre>
<p>Now, products have a comultiplicative structure on Hask (as in every category with finite products), given by the terminal object and diagonal natural transformation:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">instance</span> <span class="dt">Comultiplicative</span> (<span class="ot">-&gt;</span>) (,) <span class="kw">where</span>
<span class="ot">&gt;</span>   counit <span class="fu">=</span> <span class="fu">const</span> ()
<span class="ot">&gt;</span>   comult x <span class="fu">=</span> (x, x)</code></pre>
<p>while coproducts have a multiplicative structure:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">instance</span> <span class="dt">Multiplicative</span> (<span class="ot">-&gt;</span>) <span class="dt">Either</span> <span class="kw">where</span>
<span class="ot">&gt;</span>   unit <span class="fu">=</span> absurd
<span class="ot">&gt;</span>   mult <span class="fu">=</span> <span class="fu">either</span> <span class="fu">id</span> <span class="fu">id</span></code></pre>
<p>that we can readily transport to <code>PipeC m r</code> using <code>pipe</code>:</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> m <span class="ot">=&gt;</span> <span class="dt">Multiplicative</span> (<span class="dt">PipeC</span> m r) <span class="dt">Either</span> <span class="kw">where</span>
<span class="ot">&gt;</span>   unit <span class="fu">=</span> <span class="dt">PipeC</span> <span class="fu">$</span> pipe absurd
<span class="ot">&gt;</span>   mult <span class="fu">=</span> <span class="dt">PipeC</span> <span class="fu">$</span> pipe mult</code></pre>
<p>Somewhat surprisingly, pipes also have a comultiplicative structure of their own:</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> m <span class="ot">=&gt;</span> <span class="dt">Comultiplicative</span> (<span class="dt">PipeC</span> m r) <span class="dt">Either</span> <span class="kw">where</span>
<span class="ot">&gt;</span>   counit <span class="fu">=</span> <span class="dt">PipeC</span> discard
<span class="ot">&gt;</span>   comult <span class="fu">=</span> <span class="dt">PipeC</span> <span class="fu">.</span> forever <span class="fu">$</span> <span class="kw">do</span>
<span class="ot">&gt;</span>     x <span class="ot">&lt;-</span> await
<span class="ot">&gt;</span>     yield (<span class="kw">Left</span> x)
<span class="ot">&gt;</span>     yield (<span class="kw">Right</span> x)</code></pre>
<h2 id="heterogeneous-metaprogramming">Heterogeneous metaprogramming</h2>
<p>All the combinators we defined can actually be used in practice, and the division in type classes certainly sheds some light on their structure and properties, but there&#8217;s actually something deeper going on here.</p>
<p>The fact that the standard <code>Arrow</code> class uses <code>(,)</code> as monoidal structure is not coincidental: Hask is a cartesian closed category, so to embed Haskell&#8217;s simply typed λ-calculus into some other category structure, we need at the very least a way to transport cartesian products, i.e. a premonoidal functor <span class="citation">[1]</span>.</p>
<p>However, as long as our monoidal structure is comultiplicative and symmetric, we can always recover a first-order fragment of <span class="math"><em>λ</em></span>-calculus inside the &quot;guest&quot; category, and we don&#8217;t even need an identity-on-objects functor <span class="citation">[2]</span>.</p>
<p>The idea is that we can use the monoidal structure of the guest category to represent contexts, where weakening is given by <code>counit</code>, contraction by <code>comult</code>, and exchange by <code>swap</code>.</p>
<p>There is an <a href="http://www.cs.berkeley.edu/~megacz/garrows/">experimental GHC branch</a> with a preprocessor which is able to translate expressions written in an arbitrary guest language into Haskell, given instances of appropriate type classes , which correspond exactly to the ones we have defined above.</p>
<h2 id="examples">Examples</h2>
<p>This exposition was pretty abstract, so we end with some examples.</p>
<p>We first need to define a few wrappers for our monoidal combinators, so we don&#8217;t have to deal with the <code>PipeC</code> newtype:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; split ::</span> <span class="kw">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Pipe</span> a (<span class="dt">Either</span> a a) m r
<span class="ot">&gt;</span> split <span class="fu">=</span> unPipeC comult
<span class="ot">&gt;</span> 
<span class="ot">&gt; join ::</span> <span class="kw">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Pipe</span> (<span class="dt">Either</span> a a) a m r
<span class="ot">&gt;</span> join <span class="fu">=</span> unPipeC mult
<span class="ot">&gt;</span> 
<span class="ot">&gt; (*+*) ::</span> <span class="kw">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Pipe</span> a b m r <span class="ot">-&gt;</span> <span class="dt">Pipe</span> a&#39; b&#39; m r
<span class="ot">&gt;</span>       <span class="ot">-&gt;</span> <span class="dt">Pipe</span> (<span class="dt">Either</span> a a&#39;) (<span class="dt">Either</span> b b&#39;) m r
<span class="ot">&gt;</span> f <span class="fu">*+*</span> g <span class="fu">=</span> unPipeC <span class="fu">$</span> bimap (<span class="dt">PipeC</span> f) (<span class="dt">PipeC</span> g)
<span class="ot">&gt;</span> 
<span class="ot">&gt; discardL ::</span> <span class="kw">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Pipe</span> (<span class="dt">Either</span> <span class="dt">Void</span> a) a m r
<span class="ot">&gt;</span> discardL <span class="fu">=</span> unPipeC idl
<span class="ot">&gt;</span> 
<span class="ot">&gt; discardR ::</span> <span class="kw">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Pipe</span> (<span class="dt">Either</span> a <span class="dt">Void</span>) a m r
<span class="ot">&gt;</span> discardR <span class="fu">=</span> unPipeC idr</code></pre>
<p>Now let&#8217;s write a <code>tee</code> combinator, similar to the tee command for shell pipes:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; tee ::</span> <span class="kw">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Pipe</span> a <span class="dt">Void</span> m r <span class="ot">-&gt;</span> <span class="dt">Pipe</span> a a m r
<span class="ot">&gt;</span> tee p <span class="fu">=</span> split <span class="fu">&gt;+&gt;</span> firstP p &gt;<span class="fu">+&gt;</span> discardL
<span class="ot">&gt;</span> 
<span class="ot">&gt; printer ::</span> <span class="kw">Show</span> a <span class="ot">=&gt;</span> <span class="dt">Pipe</span> a <span class="dt">Void</span> <span class="dt">IO</span> r
<span class="ot">&gt;</span> printer <span class="fu">=</span> forever <span class="fu">$</span> await <span class="fu">&gt;&gt;=</span> lift <span class="fu">.</span> <span class="fu">print</span>
<span class="ot">&gt;</span> 
<span class="ot">&gt; ex6 ::</span> <span class="dt">IO</span> ()
<span class="ot">&gt;</span> ex6 <span class="fu">=</span> <span class="kw">do</span>
<span class="ot">&gt;</span>   (sourceList [<span class="dv">1</span><span class="fu">..</span><span class="dv">5</span>] <span class="fu">&gt;+&gt;</span>
<span class="ot">&gt;</span>     tee printer <span class="fu">&gt;+&gt;</span>
<span class="ot">&gt;</span>     (fold (<span class="fu">+</span>) <span class="dv">0</span> <span class="fu">&gt;&gt;=</span> yield) <span class="fu">$$</span>
<span class="ot">&gt;</span>     printer)
<span class="ot">&gt;</span>   <span class="fu">return</span> ()
<span class="ot">&gt;</span> <span class="co">{- ex6 == mapM_ print [1,2,3,4,5,15] -}</span></code></pre>
<p>Another interesting exercise is reimplementing the <code>groupBy</code> combinator of the previous post:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; groupBy ::</span> <span class="kw">Monad</span> m <span class="ot">=&gt;</span> (a <span class="ot">-&gt;</span> a <span class="ot">-&gt;</span> <span class="dt">Bool</span>) <span class="ot">-&gt;</span> <span class="dt">Pipe</span> a [a] m r
<span class="ot">&gt;</span> groupBy p <span class="fu">=</span>
<span class="ot">&gt;</span>    <span class="co">-- split the stream in two</span>
<span class="ot">&gt;</span>    split <span class="fu">&gt;+&gt;</span>
<span class="ot">&gt;</span> 
<span class="ot">&gt;</span>    <span class="co">-- yield Nothing whenever (not (p x y))</span>
<span class="ot">&gt;</span>    <span class="co">-- for consecutive x y</span>
<span class="ot">&gt;</span>   ((consec <span class="fu">&gt;+&gt;</span>
<span class="ot">&gt;</span>     <span class="fu">filter</span> (<span class="fu">not</span> <span class="fu">.</span> <span class="fu">uncurry</span> p) <span class="fu">&gt;+&gt;</span>
<span class="ot">&gt;</span>     pipe (<span class="fu">const</span> <span class="kw">Nothing</span>)) <span class="fu">*+*</span>
<span class="ot">&gt;</span>   
<span class="ot">&gt;</span>   <span class="co">-- at the same time, let everything pass through</span>
<span class="ot">&gt;</span>   pipe <span class="kw">Just</span>) <span class="fu">&gt;+&gt;</span>
<span class="ot">&gt;</span> 
<span class="ot">&gt;</span>   <span class="co">-- now rejoin the two streams</span>
<span class="ot">&gt;</span>   join <span class="fu">&gt;+&gt;</span>
<span class="ot">&gt;</span>   
<span class="ot">&gt;</span>   <span class="co">-- then accumulate results until a Nothing is hit</span>
<span class="ot">&gt;</span>   forever (<span class="fu">until</span> isNothing <span class="fu">&gt;+&gt;</span>
<span class="ot">&gt;</span>            pipe fromJust <span class="fu">&gt;+&gt;</span>
<span class="ot">&gt;</span>            (consume <span class="fu">&gt;&gt;=</span> yield))
<span class="ot">&gt;</span> 
<span class="ot">&gt;</span> <span class="co">-- yield consecutive pairs of values</span>
<span class="ot">&gt; consec ::</span> <span class="kw">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Pipe</span> a (a, a) m r
<span class="ot">&gt;</span> consec <span class="fu">=</span> await <span class="fu">&gt;&gt;=</span> go
<span class="ot">&gt;</span>   <span class="kw">where</span>
<span class="ot">&gt;</span>     go x <span class="fu">=</span> await <span class="fu">&gt;&gt;=</span> \y <span class="ot">-&gt;</span> yield (x, y) <span class="fu">&gt;&gt;</span> go y
<span class="ot">&gt;</span> 
<span class="ot">&gt; ex7 ::</span> <span class="dt">IO</span> ()
<span class="ot">&gt;</span> ex7 <span class="fu">=</span> <span class="kw">do</span> (sourceList [<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">2</span>,<span class="dv">2</span>,<span class="dv">3</span>,<span class="dv">4</span>,<span class="dv">4</span>]
<span class="ot">&gt;</span>           <span class="fu">&gt;+&gt;</span> groupBy (<span class="fu">==</span>)
<span class="ot">&gt;</span>           <span class="fu">&gt;+&gt;</span> pipe <span class="fu">head</span>
<span class="ot">&gt;</span>            <span class="fu">$$</span> printer)
<span class="ot">&gt;</span>          <span class="fu">return</span> ()
<span class="ot">&gt;</span> <span class="co">{- ex7 == mapM_ print [1,2,3,4] -}</span></code></pre>
<h2 id="references">References</h2>
<p>[1] J. Power and E. Robinson, “Premonoidal categories and notions of computation,” <em>Mathematical. Structures in Comp. Sci.</em>, vol. 7, no. 5, pp. 453–468, oct 1997.</p>
<p>[2] A. Megacz, “Multi-Level Languages are Generalized Arrows,” <em>arXiv:1007.2885</em>, jul 2010.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[An introduction to guarded pipes]]></title>
    <link href="http://pcapriotti.github.com/blog/2012/02/02/guarded-pipes/"/>
    <updated>2012-02-02T12:00:00+00:00</updated>
    <id>http://pcapriotti.github.com/blog/2012/02/02/guarded-pipes</id>
    <content type="html"><![CDATA[<p><a href="http://hackage.haskell.org/package/pipes">Pipes</a> are a very simple but powerful abstraction which can be used to implement stream-based IO, in a very similar fashion to <a href="http://hackage.haskell.org/package/iteratee">iteratees</a> <a href="http://hackage.haskell.org/package/enumerator">and</a> <a href="http://hackage.haskell.org/package/iterIO">friends</a>, or <a href="http://hackage.haskell.org/package/conduit">conduits</a>. In this post, I introduce <strong>guarded pipes</strong>: a slight generalization of pipes which makes it possible to implement a larger class of combinators.</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="ot">{-# LANGUAGE NoMonomorphismRestriction #-}</span>
<span class="ot">&gt;</span> <span class="kw">module</span> <span class="dt">Blog.Pipes.Guarded</span> <span class="kw">where</span>
<span class="ot">&gt;</span> 
<span class="ot">&gt;</span> <span class="kw">import</span> Control.Category
<span class="ot">&gt;</span> <span class="kw">import</span> Control.Monad.Free
<span class="ot">&gt;</span> <span class="kw">import</span> Control.Monad.Identity
<span class="ot">&gt;</span> <span class="kw">import</span> Data.Maybe
<span class="ot">&gt;</span> <span class="kw">import</span> Data.Void
<span class="ot">&gt;</span> <span class="kw">import</span> Prelude <span class="kw">hiding</span> (id, (.), until, filter)</code></pre>
<p>The idea behind pipes is straightfoward: fix a base monad <code>m</code>, then construct the <a href="http://hackage.haskell.org/packages/archive/free/latest/doc/html/Control-Monad-Free.html">free monad</a> over a specific <code>PipeF</code> functor:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">data</span> <span class="dt">PipeF</span> a b m x <span class="fu">=</span> <span class="dt">M</span> (m x)
<span class="ot">&gt;</span>                    <span class="fu">|</span> <span class="dt">Yield</span> b x
<span class="ot">&gt;</span>                    <span class="fu">|</span> <span class="dt">Await</span> (<span class="dt">Maybe</span> a <span class="ot">-&gt;</span> x)
<span class="ot">&gt;</span> 
<span class="ot">&gt;</span> <span class="kw">instance</span> <span class="kw">Monad</span> m <span class="ot">=&gt;</span> <span class="kw">Functor</span> (<span class="dt">PipeF</span> a b m) <span class="kw">where</span>
<span class="ot">&gt;</span>   <span class="fu">fmap</span> f (<span class="dt">M</span> m) <span class="fu">=</span> <span class="dt">M</span> <span class="fu">$</span> liftM f m
<span class="ot">&gt;</span>   <span class="fu">fmap</span> f (<span class="dt">Yield</span> x c) <span class="fu">=</span> <span class="dt">Yield</span> x (f c)
<span class="ot">&gt;</span>   <span class="fu">fmap</span> f (<span class="dt">Await</span> k) <span class="fu">=</span> <span class="dt">Await</span> (f <span class="fu">.</span> k)
<span class="ot">&gt;</span> 
<span class="ot">&gt;</span> <span class="kw">type</span> <span class="dt">Pipe</span> a b m r <span class="fu">=</span> <span class="dt">Free</span> (<span class="dt">PipeF</span> a b m) r</code></pre>
<p>Generally speaking, a free monad can be thought of as an embedded language in CPS style: every summand of the base functor (<code>PipeF</code> in this case), is a primitive operation, while the <code>x</code> parameter represents the continuation at each step.</p>
<p>In the case of pipes, <code>M</code> corresponds to an effect in the base monad, <code>Yield</code> produces an output value, and <code>Await</code> blocks until it receives an input value, then passes it to its continuation. You can see that the <code>Await</code> continuation takes a <code>Maybe a</code> type: this is the only thing that distinguishes guarded pipes from regular pipes (as implemented in the <a href="http://hackage.haskell.org/package/pipes">pipes</a> package on Hackage). The idea is that <code>Await</code> will receive <code>Nothing</code> whenever the pipe runs out of input values. That will give it a chance to do some cleanup or yield extra outputs. Any additional <code>Await</code> after that point will terminate the pipe immediately.</p>
<p>We can write a simplistic list-based (strict) interpreter formalizing the semantics I just described:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; evalPipe ::</span> <span class="kw">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Pipe</span> a b m r <span class="ot">-&gt;</span> [a] <span class="ot">-&gt;</span> m [b]
<span class="ot">&gt;</span> evalPipe p xs <span class="fu">=</span> go <span class="kw">False</span> xs [] p</code></pre>
<p>The boolean parameter is going to be set to <code>True</code> as soon as we execute an <code>Await</code> with an empty input list.</p>
<p>A <code>Pure</code> value means that the pipe has terminated spontaneously, so we return the accumulated output list:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span>   <span class="kw">where</span>
<span class="ot">&gt;</span>     go _ _ ys (<span class="dt">Pure</span> r) <span class="fu">=</span> <span class="fu">return</span> (<span class="fu">reverse</span> ys)</code></pre>
<p>Execute inner monadic effects:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span>     go t xs ys (<span class="dt">Free</span> (<span class="dt">M</span> m)) <span class="fu">=</span> m <span class="fu">&gt;&gt;=</span> go t xs ys</code></pre>
<p>Save yielded values into the accumulator:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span>     go t xs ys (<span class="dt">Free</span> (<span class="dt">Yield</span> y c)) <span class="fu">=</span> go t xs (y <span class="fu">:</span> ys) c</code></pre>
<p>If we still have values in the input list, feed one to the continuation of an <code>Await</code> statement.</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span>     go t (x<span class="fu">:</span>xs) ys (<span class="dt">Free</span> (<span class="dt">Await</span> k)) <span class="fu">=</span> go t xs ys <span class="fu">$</span> k (<span class="kw">Just</span> x)</code></pre>
<p>If we run out of inputs, pass <code>Nothing</code> to the <code>Await</code> continuation&#8230;</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span>     go <span class="kw">False</span> [] ys (<span class="dt">Free</span> (<span class="dt">Await</span> k)) <span class="fu">=</span> go <span class="kw">True</span> [] ys (k <span class="kw">Nothing</span>)</code></pre>
<p>&#8230; but only the first time. If the pipe awaits again, terminate it.</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span>     go <span class="kw">True</span> [] ys (<span class="dt">Free</span> (<span class="dt">Await</span> _)) <span class="fu">=</span> <span class="fu">return</span> (<span class="fu">reverse</span> ys)</code></pre>
<p>To simplify the implementation of actual pipes, we define the following basic combinators:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; tryAwait ::</span> <span class="kw">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Pipe</span> a b m (<span class="dt">Maybe</span> a)
<span class="ot">&gt;</span> tryAwait <span class="fu">=</span> wrap <span class="fu">$</span> <span class="dt">Await</span> <span class="fu">return</span>
<span class="ot">&gt;</span> 
<span class="ot">&gt; yield ::</span> <span class="kw">Monad</span> m <span class="ot">=&gt;</span> b <span class="ot">-&gt;</span> <span class="dt">Pipe</span> a b m ()
<span class="ot">&gt;</span> yield x <span class="fu">=</span> wrap <span class="fu">$</span> <span class="dt">Yield</span> x (<span class="fu">return</span> ())
<span class="ot">&gt;</span> 
<span class="ot">&gt; lift ::</span> <span class="kw">Monad</span> m <span class="ot">=&gt;</span> m r <span class="ot">-&gt;</span> <span class="dt">Pipe</span> a b m r
<span class="ot">&gt;</span> lift <span class="fu">=</span> wrap <span class="fu">.</span> <span class="dt">M</span> <span class="fu">.</span> liftM <span class="fu">return</span></code></pre>
<p>and a couple of secondary combinators, very useful in practice. First, a pipe that consumes all input and never produces output:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; discard ::</span> <span class="kw">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Pipe</span> a b m r
<span class="ot">&gt;</span> discard <span class="fu">=</span> forever tryAwait</code></pre>
<p>then a simplified <code>await</code> primitive, that dies as soon as we stop feeding values to it.</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; await ::</span> <span class="kw">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Pipe</span> a b m a
<span class="ot">&gt;</span> await <span class="fu">=</span> tryAwait <span class="fu">&gt;&gt;=</span> <span class="fu">maybe</span> discard <span class="fu">return</span></code></pre>
<p>now we can write a very simple pipe that sums consecutive pairs of numbers:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; sumPairs ::</span> (<span class="kw">Monad</span> m, <span class="kw">Num</span> a) <span class="ot">=&gt;</span> <span class="dt">Pipe</span> a a m ()
<span class="ot">&gt;</span> sumPairs <span class="fu">=</span> forever <span class="fu">$</span> <span class="kw">do</span>
<span class="ot">&gt;</span>   x <span class="ot">&lt;-</span> await
<span class="ot">&gt;</span>   y <span class="ot">&lt;-</span> await
<span class="ot">&gt;</span>   yield <span class="fu">$</span> x <span class="fu">+</span> y</code></pre>
<p>we get:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; ex1 ::</span> [<span class="dt">Int</span>]
<span class="ot">&gt;</span> ex1 <span class="fu">=</span> runIdentity <span class="fu">$</span> evalPipe sumPairs [<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">3</span>,<span class="dv">4</span>]
<span class="ot">&gt;</span> <span class="co">{- ex1 == [3, 7] -}</span></code></pre>
<h2 id="composing-pipes">Composing pipes</h2>
<p>The usefulness of pipes, however, is not limited to being able to express list transformations as monadic computations using the <code>await</code> and <code>yield</code> primitives. In fact, it turns out that two pipes can be composed sequentially to create a new pipe.</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">infixl</span> <span class="dv">9</span> <span class="fu">&gt;+&gt;</span>
<span class="ot">&gt; (&gt;+&gt;) ::</span> <span class="kw">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Pipe</span> a b m r <span class="ot">-&gt;</span> <span class="dt">Pipe</span> b c m r <span class="ot">-&gt;</span> <span class="dt">Pipe</span> a c m r
<span class="ot">&gt;</span> (<span class="fu">&gt;+&gt;</span>) <span class="fu">=</span> go <span class="kw">False</span> <span class="kw">False</span>
<span class="ot">&gt;</span>   <span class="kw">where</span></code></pre>
<p>When implementing <code>evalPipe</code>, we needed a boolean parameter to signal upstream input exhaustion. This time, we need two boolean parameters, one for the input of the upstream pipe, and one for its output, i.e. the input of the downstream pipe. First, if downstream does anything other than waiting, we just let the composite pipe execute the same action:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span>     go _ _ p1 (<span class="dt">Pure</span> r) <span class="fu">=</span> <span class="fu">return</span> r
<span class="ot">&gt;</span>     go t1 t2 p1 (<span class="dt">Free</span> (<span class="dt">Yield</span> x c)) <span class="fu">=</span> yield x <span class="fu">&gt;&gt;</span> go t1 t2 p1 c
<span class="ot">&gt;</span>     go t1 t2 p1 (<span class="dt">Free</span> (<span class="dt">M</span> m)) <span class="fu">=</span> lift m <span class="fu">&gt;&gt;=</span> \p2 <span class="ot">-&gt;</span> go t1 t2 p1 p2</code></pre>
<p>then, if upstream is yielding and downstream is waiting, we can feed the yielded value to the downstream pipe and continue from there:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span>     go t1 t2 (<span class="dt">Free</span> (<span class="dt">Yield</span> x c)) (<span class="dt">Free</span> (<span class="dt">Await</span> k)) <span class="fu">=</span>
<span class="ot">&gt;</span>       go t1 t2 c <span class="fu">$</span> k (<span class="kw">Just</span> x)</code></pre>
<p>if downstream is waiting and upstream is running a monadic computation, just let upstream run and keep downstream waiting:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span>     go t1 t2 (<span class="dt">Free</span> (<span class="dt">M</span> m)) p2<span class="fu">@</span>(<span class="dt">Free</span> (<span class="dt">Await</span> _)) <span class="fu">=</span>
<span class="ot">&gt;</span>       lift m <span class="fu">&gt;&gt;=</span> \p1 <span class="ot">-&gt;</span> go t1 t2 p1 p2</code></pre>
<p>if upstream terminates while downstream is waiting, finalize downstream:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span>     go t1 <span class="kw">False</span> p1<span class="fu">@</span>(<span class="dt">Pure</span> _) (<span class="dt">Free</span> (<span class="dt">Await</span> k)) <span class="fu">=</span>
<span class="ot">&gt;</span>       go t1 <span class="kw">True</span> p1 (k <span class="kw">Nothing</span>)</code></pre>
<p>but if downstream awaits again, terminate the whole composite pipe:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span>     go _ <span class="kw">True</span> (<span class="dt">Pure</span> r) (<span class="dt">Free</span> (<span class="dt">Await</span> _)) <span class="fu">=</span> <span class="fu">return</span> r</code></pre>
<p>now, if both pipes are waiting, we keep the second pipe waiting and we feed whatever input we get to the first pipe. If the input is <code>Nothing</code>, we set the first boolean flag, so that next time the first pipe awaits, we can finalize the downstream pipe.</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span>     go <span class="kw">False</span> t2 (<span class="dt">Free</span> (<span class="dt">Await</span> k)) p2<span class="fu">@</span>(<span class="dt">Free</span> (<span class="dt">Await</span> _)) <span class="fu">=</span>
<span class="ot">&gt;</span>       tryAwait <span class="fu">&gt;&gt;=</span> \x <span class="ot">-&gt;</span> go (isNothing x) t2 (k x) p2
<span class="ot">&gt;</span>     go <span class="kw">True</span> <span class="kw">False</span> p1<span class="fu">@</span>(<span class="dt">Free</span> (<span class="dt">Await</span> _)) (<span class="dt">Free</span> (<span class="dt">Await</span> k)) <span class="fu">=</span>
<span class="ot">&gt;</span>       go <span class="kw">True</span> <span class="kw">True</span> p1 (k <span class="kw">Nothing</span>)
<span class="ot">&gt;</span>     go <span class="kw">True</span> <span class="kw">True</span> p1<span class="fu">@</span>(<span class="dt">Free</span> (<span class="dt">Await</span> _)) p2<span class="fu">@</span>(<span class="dt">Free</span> (<span class="dt">Await</span> _)) <span class="fu">=</span>
<span class="ot">&gt;</span>       tryAwait <span class="fu">&gt;&gt;=</span> \_ <span class="ot">-&gt;</span> <span class="co">{- unreachable -}</span> go <span class="kw">True</span> <span class="kw">True</span> p1 p2</code></pre>
<p>This composition can be shown to be associative (in a rather strong sense), with identity given by:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; idP ::</span> <span class="kw">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Pipe</span> a a m r
<span class="ot">&gt;</span> idP <span class="fu">=</span> forever <span class="fu">$</span> await <span class="fu">&gt;&gt;=</span> yield</code></pre>
<p>So we can define a <code>Category</code> instance:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">newtype</span> <span class="dt">PipeC</span> m r a b <span class="fu">=</span> <span class="dt">PipeC</span> {<span class="ot"> unPipeC ::</span> <span class="dt">Pipe</span> a b m r }
<span class="ot">&gt;</span> 
<span class="ot">&gt;</span> <span class="kw">instance</span> <span class="kw">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Category</span> (<span class="dt">PipeC</span> m r) <span class="kw">where</span>
<span class="ot">&gt;</span>   <span class="fu">id</span> <span class="fu">=</span> <span class="dt">PipeC</span> idP
<span class="ot">&gt;</span>   (<span class="dt">PipeC</span> p2) <span class="fu">.</span> (<span class="dt">PipeC</span> p1) <span class="fu">=</span> <span class="dt">PipeC</span> <span class="fu">$</span> p1 <span class="fu">&gt;+&gt;</span> p2</code></pre>
<h2 id="running-pipes">Running pipes</h2>
<p>A <strong>runnable pipe</strong>, also called <code>Pipeline</code>, is a pipe that doesn&#8217;t yield any value and doesn&#8217;t wait for any input. We can formalize this in the types as follows:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">type</span> <span class="dt">Pipeline</span> m r <span class="fu">=</span> <span class="dt">Pipe</span> () <span class="dt">Void</span> m r</code></pre>
<p>Disregarding bottom, calling <code>await</code> on such a pipe does not return any useful value, and yielding is impossible. Another way to think of <code>Pipeline</code> is as an arrow (in <code>PipeC</code>) from the terminal object to the initial object of Hask<sup><a href="#fn1" class="footnoteRef" id="fnref1">1</a></sup>.</p>
<p>Running a pipeline is straightforward:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; runPipe ::</span> <span class="kw">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Pipeline</span> m r <span class="ot">-&gt;</span> m r
<span class="ot">&gt;</span> runPipe (<span class="dt">Pure</span> r) <span class="fu">=</span> <span class="fu">return</span> r
<span class="ot">&gt;</span> runPipe (<span class="dt">Free</span> (<span class="dt">M</span> m)) <span class="fu">=</span> m <span class="fu">&gt;&gt;=</span> runPipe
<span class="ot">&gt;</span> runPipe (<span class="dt">Free</span> (<span class="dt">Await</span> k)) <span class="fu">=</span> runPipe <span class="fu">$</span> k (<span class="kw">Just</span> ())
<span class="ot">&gt;</span> runPipe (<span class="dt">Free</span> (<span class="dt">Yield</span> x c)) <span class="fu">=</span> absurd x</code></pre>
<p>where the impossibility of the last case is guaranteed by the types, unless of course the pipe introduced a bottom value at some point.</p>
<p>The three primitive operations <code>tryAwait</code>, <code>yield</code> and <code>lift</code>, together with pipe composition and the <code>runPipe</code> function above, are basically all we need to define most pipes and pipe combinators. For example, the simple pipe interpreter <code>evalPipe</code> can be easily rewritten in terms of these primitives:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; evalPipe&#39; ::</span> <span class="kw">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Pipe</span> a b m r <span class="ot">-&gt;</span> [a] <span class="ot">-&gt;</span> m [b]
<span class="ot">&gt;</span> evalPipe&#39; p xs <span class="fu">=</span> runPipe <span class="fu">$</span>
<span class="ot">&gt;</span>   (<span class="fu">mapM_</span> yield xs <span class="fu">&gt;&gt;</span> <span class="fu">return</span> []) <span class="fu">&gt;+&gt;</span>
<span class="ot">&gt;</span>   (p <span class="fu">&gt;&gt;</span> discard) <span class="fu">&gt;+&gt;</span>
<span class="ot">&gt;</span>   collect <span class="fu">id</span>
<span class="ot">&gt;</span>   <span class="kw">where</span>
<span class="ot">&gt;</span>     collect xs <span class="fu">=</span>
<span class="ot">&gt;</span>       tryAwait <span class="fu">&gt;&gt;=</span> <span class="fu">maybe</span> (<span class="fu">return</span> <span class="fu">$</span> xs [])
<span class="ot">&gt;</span>                          (\x <span class="ot">-&gt;</span> collect (xs <span class="fu">.</span> (x<span class="fu">:</span>)))</code></pre>
<p>Note that we use the <code>discard</code> pipe to turn the original pipe into an infinite one, so that the final return value will be taken from the final pipe.</p>
<h2 id="extra-combinators">Extra combinators</h2>
<p>The rich structure on pipes (category and monad) makes it really easy to define new higher-level combinators. For example, here are implementations of some of the combinators in Data.Conduit.List, translated to pipes:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> sourceList <span class="fu">=</span> <span class="fu">mapM_</span> yield
<span class="ot">&gt;</span> sourceNull <span class="fu">=</span> <span class="fu">return</span> ()
<span class="ot">&gt;</span> fold f z <span class="fu">=</span> go z
<span class="ot">&gt;</span>   <span class="kw">where</span>
<span class="ot">&gt;</span>     go x <span class="fu">=</span> tryAwait <span class="fu">&gt;&gt;=</span> <span class="fu">maybe</span> (<span class="fu">return</span> x) (go <span class="fu">.</span> f x)
<span class="ot">&gt;</span> consume <span class="fu">=</span> fold (\xs x <span class="ot">-&gt;</span> xs <span class="fu">.</span> (x<span class="fu">:</span>)) <span class="fu">id</span> <span class="fu">&gt;&gt;=</span> \xs <span class="ot">-&gt;</span> <span class="fu">return</span> (xs [])
<span class="ot">&gt;</span> sinkNull <span class="fu">=</span> discard
<span class="ot">&gt;</span> <span class="fu">take</span> n <span class="fu">=</span> (isolate n <span class="fu">&gt;&gt;</span> <span class="fu">return</span> []) <span class="fu">&gt;+&gt;</span> consume
<span class="ot">&gt;</span> <span class="fu">drop</span> n <span class="fu">=</span> replicateM n await <span class="fu">&gt;&gt;</span> idP
<span class="ot">&gt;</span> pipe f <span class="fu">=</span> forever <span class="fu">$</span> await <span class="fu">&gt;&gt;=</span> yield <span class="fu">.</span> f <span class="co">-- called map in conduit</span>
<span class="ot">&gt;</span> <span class="fu">concatMap</span> f <span class="fu">=</span> forever <span class="fu">$</span> await <span class="fu">&gt;&gt;=</span> <span class="fu">mapM_</span> yield <span class="fu">.</span> f
<span class="ot">&gt;</span> <span class="fu">until</span> p <span class="fu">=</span> go
<span class="ot">&gt;</span>   <span class="kw">where</span>
<span class="ot">&gt;</span>     go <span class="fu">=</span> await <span class="fu">&gt;&gt;=</span> \x <span class="ot">-&gt;</span> <span class="kw">if</span> p x <span class="kw">then</span> <span class="fu">return</span> () <span class="kw">else</span> yield x <span class="fu">&gt;&gt;</span> go
<span class="ot">&gt;</span> groupBy (<span class="fu">~=</span>) <span class="fu">=</span> p <span class="fu">&gt;+&gt;</span>
<span class="ot">&gt;</span>   forever (<span class="fu">until</span> isNothing <span class="fu">&gt;+&gt;</span>
<span class="ot">&gt;</span>            pipe fromJust <span class="fu">&gt;+&gt;</span>
<span class="ot">&gt;</span>            (consume <span class="fu">&gt;&gt;=</span> yield))
<span class="ot">&gt;</span>   <span class="kw">where</span> 
<span class="ot">&gt;</span>     <span class="co">-- the pipe p yields Nothing whenever the current item y</span>
<span class="ot">&gt;</span>     <span class="co">-- and the previous one x do not satisfy x ~= y, and behaves</span>
<span class="ot">&gt;</span>     <span class="co">-- like idP otherwise</span>
<span class="ot">&gt;</span>     p <span class="fu">=</span> await <span class="fu">&gt;&gt;=</span> \x <span class="ot">-&gt;</span> yield (<span class="kw">Just</span> x) <span class="fu">&gt;&gt;</span> go x
<span class="ot">&gt;</span>     go x <span class="fu">=</span> <span class="kw">do</span>
<span class="ot">&gt;</span>       y <span class="ot">&lt;-</span> await
<span class="ot">&gt;</span>       unless (x <span class="fu">~=</span> y) <span class="fu">$</span> yield <span class="kw">Nothing</span>
<span class="ot">&gt;</span>       yield <span class="fu">$</span> <span class="kw">Just</span> y
<span class="ot">&gt;</span>       go y
<span class="ot">&gt;</span> isolate n <span class="fu">=</span> replicateM_ n <span class="fu">$</span> await <span class="fu">&gt;&gt;=</span> yield
<span class="ot">&gt;</span> <span class="fu">filter</span> p <span class="fu">=</span> forever <span class="fu">$</span> <span class="fu">until</span> (<span class="fu">not</span> <span class="fu">.</span> p)</code></pre>
<p>To work with the equivalent of sinks, it is useful to define a source to sink composition operator:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">infixr</span> <span class="dv">2</span> <span class="fu">$$</span>
<span class="ot">&gt; ($$) ::</span> <span class="kw">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Pipe</span> () a m r&#39; <span class="ot">-&gt;</span> <span class="dt">Pipe</span> a <span class="dt">Void</span> m r <span class="ot">-&gt;</span> m (<span class="dt">Maybe</span> r)
<span class="ot">&gt;</span> p1 <span class="fu">$$</span> p2 <span class="fu">=</span> runPipe <span class="fu">$</span> (p1 <span class="fu">&gt;&gt;</span> <span class="fu">return</span> <span class="kw">Nothing</span>) <span class="fu">&gt;+&gt;</span> liftM <span class="kw">Just</span> p2</code></pre>
<p>which ignores the source return type, and just returns the sink return value, or <code>Nothing</code> if the source happens to terminate first. So we have, for example:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; ex2 ::</span> <span class="dt">Maybe</span> [<span class="dt">Int</span>]
<span class="ot">&gt;</span> ex2 <span class="fu">=</span> runIdentity <span class="fu">$</span> sourceList [<span class="dv">1</span><span class="fu">..</span><span class="dv">10</span>] <span class="fu">&gt;+&gt;</span> isolate <span class="dv">4</span> <span class="fu">$$</span> consume
<span class="ot">&gt;</span> <span class="co">{- ex2 == Just [1,2,3,4] -}</span>
<span class="ot">&gt;</span> 
<span class="ot">&gt; ex3 ::</span> <span class="dt">Maybe</span> [<span class="dt">Int</span>]
<span class="ot">&gt;</span> ex3 <span class="fu">=</span> runIdentity <span class="fu">$</span> sourceList [<span class="dv">1</span><span class="fu">..</span><span class="dv">10</span>] <span class="fu">$$</span> discard
<span class="ot">&gt;</span> <span class="co">{- ex3 == Nothing -}</span>
<span class="ot">&gt;</span> 
<span class="ot">&gt; ex4 ::</span> <span class="dt">Maybe</span> <span class="dt">Int</span>
<span class="ot">&gt;</span> ex4 <span class="fu">=</span> runIdentity <span class="fu">$</span> sourceList [<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">2</span>,<span class="dv">2</span>,<span class="dv">3</span>,<span class="dv">4</span>,<span class="dv">4</span>]
<span class="ot">&gt;</span>                 <span class="fu">&gt;+&gt;</span> groupBy (<span class="fu">==</span>)
<span class="ot">&gt;</span>                 <span class="fu">&gt;+&gt;</span> pipe <span class="fu">head</span>
<span class="ot">&gt;</span>                  <span class="fu">$$</span> fold (<span class="fu">+</span>) <span class="dv">0</span>
<span class="ot">&gt;</span> <span class="co">{- ex4 == Just 10 -}</span>
<span class="ot">&gt;</span> 
<span class="ot">&gt; ex5 ::</span> <span class="dt">Maybe</span> [<span class="dt">Int</span>]
<span class="ot">&gt;</span> ex5 <span class="fu">=</span> runIdentity <span class="fu">$</span> sourceList [<span class="dv">1</span><span class="fu">..</span><span class="dv">10</span>]
<span class="ot">&gt;</span>                 <span class="fu">&gt;+&gt;</span> <span class="fu">filter</span> (\x <span class="ot">-&gt;</span> x <span class="ot">`mod`</span> <span class="dv">3</span> <span class="fu">==</span> <span class="dv">0</span>)
<span class="ot">&gt;</span>                  <span class="fu">$$</span> consume
<span class="ot">&gt;</span> <span class="co">{- ex5 == Just [3, 6, 9] -}</span></code></pre>
<h2 id="pipes-in-practice">Pipes in practice</h2>
<p>You can find an implementation of guarded pipes in my <a href="https://github.com/pcapriotti/pipes-core">fork</a> of pipes. There is also a <a href="https://github.com/pcapriotti/pipes-extra">pipes-extra</a> repository where you can find some pipes to deal with chunked <code>ByteStream</code>s and utilities to convert conduits to pipes.</p>
<p>I hope to be able to merge this into the original pipes package once the guarded pipe concept has proven its worth. Without the <code>tryAwait</code> primitive, combinators like <code>fold</code> and <code>consume</code> cannot be implemented, nor even a simple stateful pipe like one to split a chunked input into lines. So I think there are enough benefits to justify a little extra complexity in the definition of composition.</p>
<div class="footnotes">
<hr />
<ol>
<li id="fn1"><p>In reality, Hask doesn&#8217;t have an initial object, and the terminal object is actually <code>Void</code>, because of non-strict semantics.<a href="#fnref1">↩</a></p></li>
</ol>
</div>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Reinversion of control with continuations]]></title>
    <link href="http://pcapriotti.github.com/blog/2012/01/18/reinversion-of-control/"/>
    <updated>2012-01-18T12:00:00+00:00</updated>
    <id>http://pcapriotti.github.com/blog/2012/01/18/reinversion-of-control</id>
    <content type="html"><![CDATA[<p>In my <a href="http://pcapriotti.wordpress.com/2012/01/10/from-event-driven-programming-to-frp/">last post</a> I mentioned how it is possible to achieve a form of “reinversion of control” by using (green) threads. Some commenters noted how this is effectively a solved problem, as demonstrated for example by Erlang, as well as the numerous variations on CSP currently gaining a lot of popularity.</p>

<p>I don’t disagree with that, but it’s just not the point of this series of posts. This is about understanding the computational structure of event-driven code, and see how it’s possible to transform it into a less awkward form <strong>without</strong> introducing concurrency (or at least not in the traditional sense of the term).</p>

<p>Using threads to solve what is essentially a control flow problem is cheating. And you pay in terms of increased complexity, and code which is harder to reason about, since you introduced a whole lot of interleaving opportunities and possible race conditions. Using a non-preemptive concurrency abstraction with manual yield directives (like my <a href="https://gist.github.com/1086172">Python gist</a> does) will solve that, but then you’d have to think of how to schedule your coroutines, so that is also not a complete solution.</p>

<h2 id="programmable-semicolons">Programmable semicolons</h2>

<p>To find an alternative to the multitask-based approach, let’s focus on two particular lines of the last example:</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
</pre></td><td class="code"><pre><code class="py"><span class="line"><span class="n">reply</span> <span class="o">=</span> <span class="n">start_request</span><span class="p">();</span>
</span><span class="line"><span class="n">get_data</span><span class="p">(</span><span class="n">reply</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>where I added an explicit semicolon at the end of the first line. A semicolon is an important component of an imperative program, even though, syntactically, it is often omitted in languages like Python. It corresponds to the <em>sequencing</em> operator: execute the instruction on the left side, then pass the result to the right side and execute that.</p>

<p>If the instruction on the left side corresponds to an asynchronous operation, we want to alter the meaning of sequencing. Given a sequence of statements of the form</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
</pre></td><td class="code"><pre><code class="c"><span class="line"><span class="n">x</span> <span class="o">=</span> <span class="n">A</span><span class="p">();</span> <span class="n">B</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>we want to interpret that as: call <code>A</code>, then return control back to the main loop; when <code>A</code> is finished, bind its result to <code>x</code>, then run <code>B</code>.</p>

<p>So what we want is to be able to override the sequencing operator: we want <strong>programmable semicolons</strong>.</p>

<h2 id="the-continuation-monad">The continuation monad</h2>

<p>Since it is often really useful to look at the types of functions to understand how exactly they fit together, we’ll leave Python and start focusing on Haskell for our running example.</p>

<p>We can make a very important observation immediately by looking at the type of the callback registration function that our framework offers, and try to interpret it in the context of controlled side effects (i.e. the <code>IO</code> monad). For Qt, it could look something like:</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
</pre></td><td class="code"><pre><code class="hs"><span class="line"><span class="nf">connect</span> <span class="ow">::</span> <span class="kt">Object</span> <span class="ow">-&gt;</span> <span class="kt">String</span> <span class="ow">-&gt;</span> <span class="p">(</span><span class="n">a</span> <span class="ow">-&gt;</span> <span class="kt">IO</span> <span class="nb">()</span><span class="p">)</span> <span class="ow">-&gt;</span> <span class="kt">IO</span> <span class="nb">()</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>to be used, for example, like this:</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
</pre></td><td class="code"><pre><code class="hs"><span class="line"><span class="nf">connect</span> <span class="n">httpReply</span> <span class="s">&quot;finished()&quot;</span> <span class="o">$</span> <span class="nf">\</span><span class="kr">_</span> <span class="ow">-&gt;</span> <span class="kr">do</span>
</span><span class="line">    <span class="n">putStrLn</span> <span class="s">&quot;request finished&quot;</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>so the first argument is the object, the second is the C++ signature of the
signal, and the third is a callback that will be invoked by the framework
whenever the specified signal is emitted. Now, we can get rid of all the noise
of actually connecting to a signal, and define a type representing just the act
of registering a callback.</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
</pre></td><td class="code"><pre><code class="hs"><span class="line"><span class="kr">newtype</span> <span class="kt">Event</span> <span class="n">a</span> <span class="ow">=</span> <span class="kt">Event</span> <span class="p">{</span> <span class="n">on</span> <span class="ow">::</span> <span class="p">(</span><span class="n">a</span> <span class="ow">-&gt;</span> <span class="kt">IO</span> <span class="nb">()</span><span class="p">)</span> <span class="ow">-&gt;</span> <span class="kt">IO</span> <span class="nb">()</span> <span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>Doesn’t that look familiar? It is <strong>exactly</strong> the <a href="http://hackage.haskell.org/packages/archive/mtl/latest/doc/html/Control-Monad-Cont.html">continuation monad transformer</a> applied to the <code>IO</code> monad! The usual monad instance for <code>ContT</code> perfectly captures the semantics we are looking for:</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
</pre></td><td class="code"><pre><code class="hs"><span class="line"><span class="kr">instance</span> <span class="kt">Monad</span> <span class="kt">Event</span> <span class="kr">where</span>
</span><span class="line">  <span class="n">return</span> <span class="n">x</span> <span class="ow">=</span> <span class="kt">Event</span> <span class="o">$</span> <span class="nf">\</span><span class="n">k</span> <span class="ow">-&gt;</span> <span class="n">k</span> <span class="n">x</span>
</span><span class="line">  <span class="n">e</span> <span class="o">&gt;&gt;=</span> <span class="n">f</span> <span class="ow">=</span> <span class="kt">Event</span> <span class="o">$</span> <span class="nf">\</span><span class="n">k</span> <span class="ow">-&gt;</span>
</span><span class="line">    <span class="n">on</span> <span class="n">e</span> <span class="o">$</span> <span class="nf">\</span><span class="n">x</span> <span class="ow">-&gt;</span>
</span><span class="line">      <span class="n">on</span> <span class="p">(</span><span class="n">f</span> <span class="n">x</span><span class="p">)</span> <span class="n">k</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>The return function simply calls the callback immediately with the provided value, no actual connection is performed. The bind operator represents our custom semicolon: we connect to the first event, and when that fires, we take the value it yielded, apply it to <code>f</code>, and connect to the resulting event.</p>

<p>Now we can actually translate the Python code of the previous example to Haskell:</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
</pre></td><td class="code"><pre><code class="hs"><span class="line"><span class="nf">ex</span> <span class="ow">::</span> <span class="kt">Event</span> <span class="nb">()</span>
</span><span class="line"><span class="nf">ex</span> <span class="ow">=</span> <span class="n">forever</span> <span class="o">$</span> <span class="kr">do</span>
</span><span class="line">  <span class="n">result</span> <span class="ow">&lt;-</span> <span class="n">untilRight</span> <span class="o">.</span> <span class="n">replicate</span> <span class="mi">2</span> <span class="o">$</span> <span class="kr">do</span>
</span><span class="line">    <span class="n">reply</span> <span class="ow">&lt;-</span> <span class="n">startRequest</span>
</span><span class="line">    <span class="n">either</span> <span class="p">(</span><span class="n">return</span> <span class="o">.</span> <span class="kt">Left</span><span class="p">)</span> <span class="p">(</span><span class="n">liftM</span> <span class="kt">Right</span> <span class="o">.</span> <span class="n">getData</span><span class="p">)</span> <span class="n">reply</span>
</span><span class="line">  <span class="n">either</span> <span class="n">handleError</span> <span class="n">displayData</span> <span class="n">result</span>
</span><span class="line">
</span><span class="line"><span class="nf">untilRight</span> <span class="ow">::</span> <span class="kt">Monad</span> <span class="n">m</span> <span class="ow">=&gt;</span> <span class="p">[</span><span class="n">m</span> <span class="p">(</span><span class="kt">Either</span> <span class="n">a</span> <span class="n">b</span><span class="p">)]</span> <span class="ow">-&gt;</span> <span class="n">m</span> <span class="p">(</span><span class="kt">Either</span> <span class="n">a</span> <span class="n">b</span><span class="p">)</span>
</span><span class="line"><span class="nf">untilRight</span> <span class="p">[</span><span class="n">m</span><span class="p">]</span> <span class="ow">=</span> <span class="n">m</span>
</span><span class="line"><span class="nf">untilRight</span> <span class="p">(</span><span class="n">m</span> <span class="kt">:</span> <span class="n">ms</span><span class="p">)</span> <span class="ow">=</span> <span class="n">m</span> <span class="o">&gt;&gt;=</span> <span class="n">either</span> <span class="p">(</span><span class="n">const</span> <span class="p">(</span><span class="n">untilRight</span> <span class="n">ms</span><span class="p">))</span> <span class="p">(</span><span class="n">return</span> <span class="o">.</span> <span class="kt">Right</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>Again, this could be cleaned up by adding some error reporting functionality into the monad stack.</p>

<p>Implementing the missing functions in terms of <code>connect</code> is straightforward. For example, <code>startRequest</code> will look something like this:</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
</pre></td><td class="code"><pre><code class="hs"><span class="line"><span class="nf">startRequest</span> <span class="ow">::</span> <span class="kt">Event</span> <span class="p">(</span><span class="kt">Either</span> <span class="kt">String</span> <span class="kt">Reply</span><span class="p">)</span>
</span><span class="line"><span class="nf">startRequest</span> <span class="ow">=</span> <span class="kt">Event</span> <span class="o">$</span> <span class="nf">\</span><span class="n">k</span> <span class="ow">-&gt;</span> <span class="kr">do</span>
</span><span class="line">  <span class="n">reply</span> <span class="ow">&lt;-</span> <span class="kt">AccessManager</span><span class="o">.</span><span class="n">get</span> <span class="s">&quot;http://example.net&quot;</span>
</span><span class="line">  <span class="n">connect</span> <span class="n">reply</span> <span class="s">&quot;finished()&quot;</span> <span class="o">$</span> <span class="nf">\</span><span class="kr">_</span> <span class="ow">-&gt;</span> <span class="n">k</span> <span class="p">(</span><span class="kt">Right</span> <span class="n">reply</span><span class="p">)</span>
</span><span class="line">  <span class="n">connect</span> <span class="n">reply</span> <span class="s">&quot;error(QString)&quot;</span> <span class="o">$</span> <span class="nf">\</span><span class="n">e</span> <span class="ow">-&gt;</span> <span class="n">k</span> <span class="p">(</span><span class="kt">Left</span> <span class="n">e</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>where I took the liberty of glossing over some irrelevant API details.</p>

<p>How do we run such a monad? Well, the standard <code>runContT</code> does the job:</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
</pre></td><td class="code"><pre><code class="hs"><span class="line"><span class="nf">runEvent</span> <span class="ow">::</span> <span class="kt">Event</span> <span class="nb">()</span> <span class="ow">-&gt;</span> <span class="kt">IO</span> <span class="nb">()</span>
</span><span class="line"><span class="nf">runEvent</span> <span class="n">e</span> <span class="ow">=</span> <span class="n">on</span> <span class="o">$</span> <span class="nf">\</span><span class="n">k</span> <span class="ow">-&gt;</span> <span class="n">return</span> <span class="nb">()</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>so</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
</pre></td><td class="code"><pre><code class="hs"><span class="line"><span class="nf">runEvent</span> <span class="n">ex</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>will run until the first connection, return control to the main loop, resume when an event occurs, and so on.</p>

<h2 id="conclusion">Conclusion</h2>

<p>I love the simplicity and elegance of this approach, but unfortunately, it is far from a complete solution. So far we have only dealt with “one-shot” events, but what happens when an event fires multiple times? Also, as this is still very imperative in nature, can we do better? Is it possible to employ a more functional style, with emphasis on composability?</p>

<p>I’ll leave the (necessarily partial) answers to those questions for a future post.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[From event-driven programming to FRP]]></title>
    <link href="http://pcapriotti.github.com/blog/2012/01/10/from-event-driven-programming-to-frp/"/>
    <updated>2012-01-10T12:00:00+00:00</updated>
    <id>http://pcapriotti.github.com/blog/2012/01/10/from-event-driven-programming-to-frp</id>
    <content type="html"><![CDATA[<h2 id="the-problem">The problem</h2>

<p>Most of modern programming is based on events. Event-driven frameworks are the
proven and true abstraction to express any kind of asynchronous and interactive
behavior, like GUIs or client-server architectures.</p>

<p>The core idea is <strong>inversion of control</strong>: the main loop is run by the
framework, users only have to register some form of “callbacks”, and the
framework will take care of calling them at the appropriate times.</p>

<p>This solves many issues that a straightforward imperative/procedural approach
would present, eliminates the need for any kind of polling, and creates all
sorts of opportunities for general-purpose optimizations inside the framework,
with no impact on the complexity of user code. All of this without introducing
any concurrency.</p>

<p>There are drawbacks, however. Event-driven code is hideous to write in most
languages, especially those lacking support for first class closures. More
importantly, event-driven code is extremely hard to reason about. The very
nature of this callback-based approach makes it impossible to use a functional
style, and even the simplest of interactions requires some form of mutable
state which has to be maintained across callback calls.</p>

<p>For example, suppose we want to write a little widget with a button. When the
button is pressed, a GET request is performed to some HTTP URL, and the result
is displayed in a message box. We need to implement a simple <strong>state machine</strong>
whose graph will look somewhat like this:</p>

<p><img src="http://pcapriotti.github.com/images/state_machine1.png" alt="State machine 1" /></p>

<p>Each state (except the initial one) corresponds to a callback. The transitions
are determined by the framework. To avoid starting more than one request at a
time, we will need to explicitly keep track of the current state.</p>

<p>Now let’s try to make a simple change to our program: suppose we want to retry
requests when they fail, but not more than once. Now the state machine becomes
more complicated, since we need to add extra nodes for the non-fatal error
condition.</p>

<p><img src="http://pcapriotti.github.com/images/state_machine2.png" alt="State machine 2" /></p>

<p>In our hypotetical event-driven code, we need to keep track of whether we
already encountered an error, check this flag at each callback to perform the
right action, and update it appropriately. Moreover, this time the code isn’t
even shaped exactly like the state machine, because we reuse the same callback
for multiple nodes. To test our code exhaustively, we need to trace every
possible path through the graph and reproduce it.</p>

<p>Now assume we want to allow simultaneous requests… you get the idea. The code
gets unwieldy pretty fast. Small changes in requirements have devastating
consequences in terms of the state graph. In practice, what happens most of the
times is that the state graph is kept implicit, which makes the code impossible
to test reliably, and consequently impossible to modify.</p>

<h2 id="towards-a-solution">Towards a solution</h2>

<p>A very simple but effective solution can be found by observing that state
graphs like those of the previous examples have a very clear interpretation
within the <a href="http://en.wikipedia.org/wiki/Operational_semantics">operational
semantics</a> of the
equivalent synchronous code.</p>

<p>A single forward transition from <code>A</code> to <code>B</code> can be simply modelled as the
sequence <code>A;B</code>, i.e. execute <code>A</code>, <strong>then</strong> execute <code>B</code>. Extra outward
transitions from a single node can be mapped to exceptions, while backward
arrows can be thought of as looping constructs.</p>

<p>Our second state machine can then be translated to the following pseudopython:</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
</pre></td><td class="code"><pre><code class="py"><span class="line"><span class="k">while</span> <span class="bp">True</span><span class="p">:</span>
</span><span class="line">    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="mi">2</span><span class="p">):</span>
</span><span class="line">        <span class="n">error</span> <span class="o">=</span> <span class="bp">None</span>
</span><span class="line">        <span class="k">try</span><span class="p">:</span>
</span><span class="line">            <span class="n">reply</span> <span class="o">=</span> <span class="n">start_request</span><span class="p">()</span>
</span><span class="line">            <span class="n">data</span> <span class="o">=</span> <span class="n">get_data</span><span class="p">(</span><span class="n">reply</span><span class="p">)</span>
</span><span class="line">            <span class="k">break</span>
</span><span class="line">        <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
</span><span class="line">            <span class="n">error</span> <span class="o">=</span> <span class="n">get_error</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>
</span><span class="line">    <span class="k">if</span> <span class="n">error</span><span class="p">:</span>
</span><span class="line">        <span class="n">handle_error</span><span class="p">(</span><span class="n">error</span><span class="p">)</span>
</span><span class="line">    <span class="k">else</span><span class="p">:</span>
</span><span class="line">        <span class="n">display_data</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>This code is straightforward. It could be made cleaner by splitting it up in a
couple of extra functions and removing the local state, but that’s beside the
point. Note how easy it is now to generalize to an arbitrary number of retries.</p>

<p>So the key observation is that we can transform asynchronous code into
synchronous-looking code, provided that we attach the correct semantics to
sequencing of operations, exceptions and loops.</p>

<p>Now the question becomes: is it possible to do so?</p>

<p>We could turn functions like <code>start_request</code> and <code>get_data</code> into blocking
operations that can throw. This will work <em>locally</em>, but it will break
asynchronicity, so it’s not an option.</p>

<p>One way to salvage this transformation is to run the code in its own thread.
Asynchronous operations will block, but won’t hang the main loop, and the rest
of the program will continue execution.</p>

<p>However, we need to be careful with the kind of threads that we use. Since we don’t
need (and don’t want!) to run multiple threads simultaneously, but we need to
spawn a thread for each asynchronous operation, we have to make sure that the
overhead is minimal, context switching is fast, and we’re not paying the cost
of scheduling and synchronization.</p>

<p><a href="https://gist.github.com/1086172">Here</a> you can find a sketched solution along
these lines that I wrote in python. It’s based on the greenlet library, which
provides cooperative multithreading.</p>

<p>In the next post I will talk about alternative solutions, as well as how to
extend the idea further, and make event-driven more declarative and less
procedural.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Effective Qt in ruby (part 3)]]></title>
    <link href="http://pcapriotti.github.com/blog/2010/12/06/effective-qt-in-ruby-part-3/"/>
    <updated>2010-12-06T12:00:00+00:00</updated>
    <id>http://pcapriotti.github.com/blog/2010/12/06/effective-qt-in-ruby-part-3</id>
    <content type="html"><![CDATA[<p>This is the third article in my series on writing Qt applications in ruby. I was planning to write about the declarative GUI system that I use in <a href="http://pcapriotti.github.com/kaya/">kaya</a>, but a comment on one of my previous posts motivated me to take a small detour, and illustrate a very simple technique to extend a qtruby application with C++ code.</p>

<p>So, suppose you need to expose a C++ function like:</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
</pre></td><td class="code"><pre><code class="cpp"><span class="line"><span class="kt">void</span> <span class="n">applyEffect</span><span class="p">(</span><span class="n">QImage</span><span class="o">*</span> <span class="n">img</span><span class="p">,</span> <span class="kt">float</span> <span class="n">arg</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>that takes a QImage, an argument, and applies a graphic effect, mutating the image in place.</p>

<p>Directly exposing this function to ruby using the extension API is not easy, because you need to extract a QImage pointer from the ruby object corresponding to the QImage, and that would require you to make assumptions on exactly how QObjects are wrapped by the ruby binding code, which is not ideal for a number of reasons.</p>

<p>Fortunately, there exists an elegant solution to this problem. First, you need to define your C++ function as a slot of some QObject. For example:</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
</pre></td><td class="code"><pre><code class="cpp"><span class="line"><span class="k">class</span> <span class="nc">Extensions</span> <span class="o">:</span> <span class="k">public</span> <span class="n">QObject</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line"><span class="n">Q_OBJECT</span>
</span><span class="line"><span class="k">public</span> <span class="nl">slots:</span>
</span><span class="line">  <span class="kt">void</span> <span class="n">applyEffect</span><span class="p">(</span><span class="n">QImage</span><span class="o">*</span> <span class="n">img</span><span class="p">,</span> <span class="kt">float</span> <span class="n">arg</span><span class="p">)</span> <span class="k">const</span><span class="p">;</span>
</span><span class="line"><span class="p">};</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>Then in your extension initialization function you can instantiate it with something like:</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
</pre></td><td class="code"><pre><code class="cpp"><span class="line"><span class="n">Extensions</span><span class="o">*</span> <span class="n">ext</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Extensions</span><span class="p">(</span><span class="n">QCoreApplication</span><span class="o">::</span><span class="n">instance</span><span class="p">());</span>
</span><span class="line"><span class="n">ext</span><span class="o">-&gt;</span><span class="n">setObjectName</span><span class="p">(</span><span class="s">&quot;__extensions__&quot;</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>And finally access it from ruby code and wrap it in a nicer package:</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
</pre></td><td class="code"><pre><code class="rb"><span class="line"><span class="vg">$ext</span> <span class="o">=</span> <span class="vg">$qApp</span><span class="o">.</span><span class="n">findChild</span><span class="p">(</span><span class="no">Qt</span><span class="o">::</span><span class="no">Object</span><span class="p">,</span> <span class="s2">&quot;__extensions__&quot;</span><span class="p">);</span>
</span><span class="line"><span class="k">class</span> <span class="nc">Qt</span><span class="o">::</span><span class="no">Image</span>
</span><span class="line">  <span class="k">def</span> <span class="nf">apply_effect</span><span class="p">(</span><span class="n">arg</span><span class="p">)</span>
</span><span class="line">    <span class="vg">$ext</span><span class="o">.</span><span class="n">applyEffect</span><span class="p">(</span><span class="nb">self</span><span class="p">,</span> <span class="n">arg</span><span class="p">)</span>
</span><span class="line">  <span class="k">end</span>
</span><span class="line"><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>This works because Qt allows you to call slots dynamically using runtime introspection of QObjects. It’s not as fast as a native function call, but in the context of a ruby method call, the additional cost should be pretty much negligible.</p>

<p>Of course, unless your extension is particularly large and complicated, you don’t need to create an Extension object for each of the functions you want to expose: you can add all of them as slots in a single Extension object, which is loaded at startup, and create a ruby-esque API for them directly in ruby code.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Effective Qt in ruby (part 2)]]></title>
    <link href="http://pcapriotti.github.com/blog/2010/09/24/effective-qt-in-ruby-part-2/"/>
    <updated>2010-09-24T12:23:00+01:00</updated>
    <id>http://pcapriotti.github.com/blog/2010/09/24/effective-qt-in-ruby-part-2</id>
    <content type="html"><![CDATA[<p>In the <a href="http://pcapriotti.github.com/blog/2010/06/25/effective-qt-in-ruby-part-1/">first part</a> of this series, I listed some of the reasons why you
should consider writing your Qt/KDE applications in ruby. This post details some
of the technical differences between writing Qt code in C++ and in ruby.</p>

<p>One of the first problems that pop up when starting a new Qt/KDE project in ruby
is how to use it in such a way that your code doesn’t end up being completely
unidiomatic. This can happen very easily if one tries to stick to the usual
conventions that apply when writing Qt code in C++.</p>

<p>If you take any piece of C++ code using Qt, you can very trivially translate it
into ruby. That works, and sometimes it’s useful, but writing code in this way
completely misses the point of using a dynamic language. You might as well write
directly in C++, and enjoy the improved performance.</p>

<p>So I believe it’s important to identify the baggage that Qt brings from its C++
roots, and eliminate it when using it from ruby. Here are some ideas to achieve
that.</p>

<h2 id="use-the-ruby-convention-for-method-names">Use the ruby convention for method names</h2>

<p>A minor point, but important for code readability.</p>

<p>Qt uses camel case for method names, while ruby methods are conventionally
written with underscores. Mixing the two styles inevitably results in an
unreadable mess, so the ruby convention should be used at all times.</p>

<p>Fortunately, QtRuby allows you to call C++ methods by spelling their name with
underscores, so it’s quite easy to achieve a satisfactory level of consistency
with minimum effort.</p>

<h2 id="never-declare-signals">Never declare signals</h2>

<p>The signal/slot mechanism is a very important Qt feature, because it allows to
work around the static nature of C++ by allowing dynamic calls to methods. You
won’t need that in ruby. For instance, you can use the standard observer library
to fire events and set callbacks. It’s completely dynamic and there’s no need to
define your signals beforehand.</p>

<h2 id="never-use-slots">Never use slots</h2>

<p>Slots are useless in ruby. QtRuby allows you to attach a block to a connect
call, and that is what you should always be using. Never use the SLOT function
with a C++ signature.</p>

<h2 id="avoid-c-signatures-altogether">Avoid C++ signatures altogether</h2>

<p>This seems impossible. It might be easy to use symbols (without using the
<code>SIGNAL</code> “macro”) to specify signals with no arguments, like</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
</pre></td><td class="code"><pre><code class="rb"><span class="line"><span class="n">button</span><span class="o">.</span><span class="n">on</span><span class="p">(</span><span class="ss">:clicked</span><span class="p">)</span> <span class="p">{</span> <span class="nb">puts</span> <span class="s2">&quot;hello world&quot;</span> <span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>but if a signal has arguments, and possibly overloads, specifying only its name
doesn’t seem to be enough to determine which particular overload we are
interested in.</p>

<p>Indeed, it’s not possible in general, but you can disambiguate using the block
arity for most overloaded signals, and add type annotations in those rare cases
where the arity is not enough.</p>

<p>Here is my <code>on</code> method, which accomplishes this:</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
<span class="line-number">20</span>
<span class="line-number">21</span>
<span class="line-number">22</span>
<span class="line-number">23</span>
<span class="line-number">24</span>
<span class="line-number">25</span>
<span class="line-number">26</span>
<span class="line-number">27</span>
<span class="line-number">28</span>
<span class="line-number">29</span>
<span class="line-number">30</span>
<span class="line-number">31</span>
<span class="line-number">32</span>
<span class="line-number">33</span>
<span class="line-number">34</span>
<span class="line-number">35</span>
<span class="line-number">36</span>
<span class="line-number">37</span>
<span class="line-number">38</span>
</pre></td><td class="code"><pre><code class="rb"><span class="line"><span class="k">def</span> <span class="nf">on</span><span class="p">(</span><span class="n">sig</span><span class="p">,</span> <span class="n">types</span> <span class="o">=</span> <span class="kp">nil</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">blk</span><span class="p">)</span>
</span><span class="line">  <span class="n">sig</span> <span class="o">=</span> <span class="no">Signal</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">sig</span><span class="p">,</span> <span class="n">types</span><span class="p">)</span>
</span><span class="line">  <span class="n">candidates</span> <span class="o">=</span> <span class="k">if</span> <span class="nb">is_a?</span> <span class="no">Qt</span><span class="o">::</span><span class="no">Object</span>
</span><span class="line">    <span class="n">signal_map</span><span class="o">[</span><span class="n">sig</span><span class="o">.</span><span class="n">symbol</span><span class="o">]</span>
</span><span class="line">  <span class="k">end</span>
</span><span class="line">  <span class="k">if</span> <span class="n">candidates</span>
</span><span class="line">    <span class="k">if</span> <span class="n">types</span>
</span><span class="line">      <span class="c1"># find candidate with the correct argument types</span>
</span><span class="line">      <span class="n">candidates</span> <span class="o">=</span> <span class="n">candidates</span><span class="o">.</span><span class="n">find_all</span><span class="p">{</span><span class="o">|</span><span class="n">s</span><span class="o">|</span> <span class="n">s</span><span class="o">[</span><span class="mi">1</span><span class="o">]</span> <span class="o">==</span> <span class="n">types</span> <span class="p">}</span>
</span><span class="line">    <span class="k">end</span>
</span><span class="line">    <span class="k">if</span> <span class="n">candidates</span><span class="o">.</span><span class="n">size</span> <span class="o">&gt;</span> <span class="mi">1</span>
</span><span class="line">      <span class="c1"># find candidate with the correct arity</span>
</span><span class="line">      <span class="n">arity</span> <span class="o">=</span> <span class="n">blk</span><span class="o">.</span><span class="n">arity</span>
</span><span class="line">      <span class="k">if</span> <span class="n">blk</span><span class="o">.</span><span class="n">arity</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span>
</span><span class="line">        <span class="c1"># take first</span>
</span><span class="line">        <span class="n">candidates</span> <span class="o">=</span> <span class="o">[</span><span class="n">candidates</span><span class="o">.</span><span class="n">first</span><span class="o">]</span>
</span><span class="line">      <span class="k">else</span>
</span><span class="line">        <span class="n">candidates</span> <span class="o">=</span> <span class="n">candidates</span><span class="o">.</span><span class="n">find_all</span><span class="p">{</span><span class="o">|</span><span class="n">s</span><span class="o">|</span> <span class="n">s</span><span class="o">[</span><span class="mi">1</span><span class="o">].</span><span class="n">size</span> <span class="o">==</span> <span class="n">arity</span> <span class="p">}</span>
</span><span class="line">      <span class="k">end</span>
</span><span class="line">    <span class="k">end</span>
</span><span class="line">    <span class="k">if</span> <span class="n">candidates</span><span class="o">.</span><span class="n">size</span> <span class="o">&gt;</span> <span class="mi">1</span>
</span><span class="line">      <span class="k">raise</span> <span class="s2">&quot;Ambiguous overload for </span><span class="si">#{</span><span class="n">sig</span><span class="si">}</span><span class="s2"> with arity </span><span class="si">#{</span><span class="n">arity</span><span class="si">}</span><span class="s2">&quot;</span>
</span><span class="line">    <span class="k">elsif</span> <span class="n">candidates</span><span class="o">.</span><span class="n">empty?</span>
</span><span class="line">      <span class="n">msg</span> <span class="o">=</span> <span class="k">if</span> <span class="n">types</span>
</span><span class="line">        <span class="s2">&quot;with types </span><span class="si">#{</span><span class="n">types</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s1">&#39; &#39;</span><span class="p">)</span><span class="si">}</span><span class="s2">&quot;</span>
</span><span class="line">      <span class="k">else</span>
</span><span class="line">        <span class="s2">&quot;with arity </span><span class="si">#{</span><span class="n">blk</span><span class="o">.</span><span class="n">arity</span><span class="si">}</span><span class="s2">&quot;</span>
</span><span class="line">      <span class="k">end</span>
</span><span class="line">      <span class="k">raise</span> <span class="s2">&quot;No overload for </span><span class="si">#{</span><span class="n">sig</span><span class="si">}</span><span class="s2"> </span><span class="si">#{</span><span class="n">msg</span><span class="si">}</span><span class="s2">&quot;</span>
</span><span class="line">    <span class="k">end</span>
</span><span class="line">    <span class="n">sign</span> <span class="o">=</span> <span class="no">SIGNAL</span><span class="p">(</span><span class="n">candidates</span><span class="o">.</span><span class="n">first</span><span class="o">[</span><span class="mi">0</span><span class="o">]</span><span class="p">)</span>
</span><span class="line">    <span class="n">connect</span><span class="p">(</span><span class="n">sign</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">blk</span><span class="p">)</span>
</span><span class="line">    <span class="no">SignalDisconnecter</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="nb">self</span><span class="p">,</span> <span class="n">sign</span><span class="p">)</span>
</span><span class="line">  <span class="k">else</span>
</span><span class="line">    <span class="n">observer</span> <span class="o">=</span> <span class="n">observe</span><span class="p">(</span><span class="n">sig</span><span class="o">.</span><span class="n">symbol</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">blk</span><span class="p">)</span>
</span><span class="line">    <span class="no">ObserverDisconnecter</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="nb">self</span><span class="p">,</span> <span class="n">observer</span><span class="p">)</span>
</span><span class="line">  <span class="k">end</span>
</span><span class="line"><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>The <code>Signal</code> class maintains the signal name and (optional) specified types. The
method lazily creates a <em>signal map</em> for each class, which maps symbols to C++
signatures, and proceeds to disambiguate among all the possibilities by using
types, or just the block arity, when no explicit types are provided.  If no
signal is found, or if the ambiguity could not be resolved, an exception is
thrown.</p>

<p>For example, the following line:</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
</pre></td><td class="code"><pre><code class="rb"><span class="line"><span class="n">combobox</span><span class="o">.</span><span class="n">on</span><span class="p">(</span><span class="ss">:current_index_changed</span><span class="p">,</span> <span class="o">[</span><span class="s2">&quot;int&quot;</span><span class="o">]</span><span class="p">)</span> <span class="p">{</span><span class="o">|</span><span class="n">i</span><span class="o">|</span> <span class="nb">self</span><span class="o">.</span><span class="n">index</span> <span class="o">=</span> <span class="n">i</span> <span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>is referring to <code>currentIndexChanged(int)</code> and not to the other possible signal
<code>currentIndexChanged(QString)</code>, because of the explicit type annotation.</p>

<p>The advantage of this trick is that I can write, for example:</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
</pre></td><td class="code"><pre><code class="rb"><span class="line"><span class="n">model</span><span class="o">.</span><span class="n">on</span><span class="p">(</span><span class="ss">:rows_about_to_be_inserted</span><span class="p">)</span> <span class="k">do</span> <span class="o">|</span><span class="nb">p</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="o">|</span>
</span><span class="line">  <span class="c1"># ...</span>
</span><span class="line"><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>without specifying any C++ signature, which in this case would be quite hefty:</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
</pre></td><td class="code"><pre><code class="cpp"><span class="line"><span class="n">rowsAboutToBeInserted</span><span class="p">(</span><span class="k">const</span> <span class="n">QModelIndex</span><span class="o">&amp;</span> <span class="n">parent</span><span class="p">,</span> <span class="kt">int</span> <span class="n">start</span><span class="p">,</span> <span class="kt">int</span> <span class="n">end</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<h2 id="conclusion">Conclusion</h2>

<p>QtRuby is an exceptional library, but to use it effectively you need to let go of some of the established practices of Qt programming in C++, and embrace the greater dynamicity of ruby.</p>

<p>In the next article I’ll show you how I tried to push this idea to the extreme with <em>AutoGUI</em>, a declarative GUI DSL built on top of QtRuby.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Effective Qt in ruby (part 1)]]></title>
    <link href="http://pcapriotti.github.com/blog/2010/06/25/effective-qt-in-ruby-part-1/"/>
    <updated>2010-06-25T12:00:00+01:00</updated>
    <id>http://pcapriotti.github.com/blog/2010/06/25/effective-qt-in-ruby-part-1</id>
    <content type="html"><![CDATA[<p>As some of you might know, I’m working on a generic board game platform
called <a href="http://pcapriotti.github.com/kaya">Kaya</a>. Kaya is a Qt/KDE-based
application to play <a href="http://en.wikipedia.org/wiki/Chess">chess</a>,
<a href="http://en.wikipedia.org/wiki/Shogi">shogi</a> and variants thereof, and
easily extensible to all sorts of board games
(<a href="http://en.wikipedia.org/wiki/Go_%28game%29">Go</a> is in the works, for
example). Kaya is written in ruby, and I have learned quite a few things
about writing non-trivial GUI applications in ruby while working on it,
so I decided to share some of my experience and code in the hope that it
might be useful to others, and possibly inspire other Qt/KDE developers
to try out ruby for their next project.</p>

<h2 id="advantages">Advantages</h2>

<p>Here is a list of what I think are the most important points that make
programming GUIs in ruby so much more productive than in C++. I’ll leave
out subjective arguments like “<em>it’s more fun</em>” or “<em>it has a nicer
syntax</em>” because I think that the actual facts are more than compelling
already.</p>

<h3 id="fast-prototyping">Fast Prototyping</h3>

<p>This is not specific to GUI programming. It is greatly aknowledged that
ruby is orders of magnitude more convenient than C++ for throwing quick
scripts together and in general for trying new ideas out. This turns out
to be very important in a GUI context as well. For example, it is very
easy to write setup code for a single component of your application so
that you can run it standalone and test it more efficiently. I have
found myself resorting to this kind of trick very often, and it
sometimes makes debugging a lot less painful. In C++, it would be
unreasonable to write a new application skeleton (and alter your build
scripts) just to test a new dialog you are developing. Another example
is “<em>fancy logging</em>”. If something breaks in the middle of complicated
or highly interactive code, sometimes printing to the console or setting
breakpoints doesn’t quite cut it. In these cases, it is a very good idea
to hack together a simple but powerful visualization tool to show the
internal state of the application in real time, and that is usually very
helpful to identify the issue. Again, since this is basically throwaway
code, ease of prototyping is very important.</p>

<h3 id="declarative-gui-definition">Declarative GUI Definition</h3>

<p>Ruby has very powerful metaprogramming facilities that make creating an
embedded DSL extremely straightforward. I use a simple mechanism in Kaya
that allows me to write things like:</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
</pre></td><td class="code"><pre><code class="rb"><span class="line"><span class="vi">@gui</span> <span class="o">=</span> <span class="no">KDE</span><span class="o">::</span><span class="n">autogui</span><span class="p">(</span><span class="ss">:engine_prefs</span><span class="p">,</span>
</span><span class="line">                    <span class="ss">:caption</span> <span class="o">=&gt;</span> <span class="no">KDE</span><span class="o">.</span><span class="n">i18n</span><span class="p">(</span><span class="s2">&quot;Configure Engines&quot;</span><span class="p">))</span> <span class="k">do</span> <span class="o">|</span><span class="n">g</span><span class="o">|</span>
</span><span class="line">  <span class="n">g</span><span class="o">.</span><span class="n">layout</span><span class="p">(</span><span class="ss">:type</span> <span class="o">=&gt;</span> <span class="ss">:horizontal</span><span class="p">)</span> <span class="k">do</span> <span class="o">|</span><span class="n">l</span><span class="o">|</span>
</span><span class="line">    <span class="n">l</span><span class="o">.</span><span class="n">list</span><span class="p">(</span><span class="ss">:list</span><span class="p">)</span>
</span><span class="line">    <span class="n">l</span><span class="o">.</span><span class="n">layout</span><span class="p">(</span><span class="ss">:type</span> <span class="o">=&gt;</span> <span class="ss">:vertical</span><span class="p">)</span> <span class="k">do</span> <span class="o">|</span><span class="n">buttons</span><span class="o">|</span>
</span><span class="line">      <span class="n">buttons</span><span class="o">.</span><span class="n">button</span><span class="p">(</span><span class="ss">:add_engine</span><span class="p">,</span>
</span><span class="line">                     <span class="ss">:text</span> <span class="o">=&gt;</span> <span class="no">KDE</span><span class="o">.</span><span class="n">i18nc</span><span class="p">(</span><span class="s2">&quot;engine&quot;</span><span class="p">,</span> <span class="s2">&quot;&amp;New...&quot;</span><span class="p">),</span>
</span><span class="line">                     <span class="ss">:icon</span> <span class="o">=&gt;</span> <span class="s1">&#39;list-add&#39;</span><span class="p">)</span>
</span><span class="line">      <span class="n">buttons</span><span class="o">.</span><span class="n">button</span><span class="p">(</span><span class="ss">:edit_engine</span><span class="p">,</span>
</span><span class="line">                     <span class="ss">:text</span> <span class="o">=&gt;</span> <span class="no">KDE</span><span class="o">.</span><span class="n">i18nc</span><span class="p">(</span><span class="s2">&quot;engine&quot;</span><span class="p">,</span> <span class="s2">&quot;&amp;Edit...&quot;</span><span class="p">),</span>
</span><span class="line">                     <span class="ss">:icon</span> <span class="o">=&gt;</span> <span class="s1">&#39;configure&#39;</span><span class="p">)</span>
</span><span class="line">      <span class="n">buttons</span><span class="o">.</span><span class="n">button</span><span class="p">(</span><span class="ss">:delete_engine</span><span class="p">,</span>
</span><span class="line">                     <span class="ss">:text</span> <span class="o">=&gt;</span> <span class="no">KDE</span><span class="o">.</span><span class="n">i18nc</span><span class="p">(</span><span class="s2">&quot;engine&quot;</span><span class="p">,</span> <span class="s2">&quot;&amp;Delete&quot;</span><span class="p">),</span>
</span><span class="line">                     <span class="ss">:icon</span> <span class="o">=&gt;</span> <span class="s1">&#39;list-remove&#39;</span><span class="p">)</span>
</span><span class="line">      <span class="n">buttons</span><span class="o">.</span><span class="n">stretch</span>
</span><span class="line">    <span class="k">end</span>
</span><span class="line">  <span class="k">end</span>
</span><span class="line"><span class="k">end</span>
</span><span class="line"><span class="n">setGUI</span><span class="p">(</span><span class="vi">@gui</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>I find it very convenient to
define GUIs at this slightly higher level of abstraction, plus you have
none of the boilerplate code typical of GUI construction in C++.
Defining GUIs in this way is so quick (and it’s easy to run them to
immediately see the results), that it makes tools like Qt Designer
completely redundant, in my opinion, except possibly when GUI design and
coding are done by different people.</p>

<h3 id="using-closures-for-slots">Using Closures for Slots</h3>

<p>Take a look at this simple example, and try to imagine how many lines of
code would be needed to implement it in C++:</p>

<div class="bogus-wrapper"><notextile><figure class="code"> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
</pre></td><td class="code"><pre><code class="rb"><span class="line"><span class="n">win</span><span class="o">.</span><span class="n">display</span><span class="o">.</span><span class="n">text</span> <span class="o">=</span> <span class="s2">&quot;0&quot;</span>
</span><span class="line"><span class="n">inc</span> <span class="o">=</span> <span class="mi">1</span>
</span><span class="line"><span class="no">Qt</span><span class="o">::</span><span class="no">Timer</span><span class="o">.</span><span class="n">every</span><span class="p">(</span><span class="mi">1000</span><span class="p">)</span> <span class="k">do</span>
</span><span class="line">  <span class="n">win</span><span class="o">.</span><span class="n">display</span><span class="o">.</span><span class="n">text</span> <span class="o">=</span> <span class="p">(</span><span class="n">win</span><span class="o">.</span><span class="n">display</span><span class="o">.</span><span class="n">text</span><span class="o">.</span><span class="n">to_i</span> <span class="o">+</span> <span class="n">inc</span><span class="p">)</span><span class="o">.</span><span class="n">to_s</span>
</span><span class="line"><span class="k">end</span>
</span><span class="line"><span class="n">win</span><span class="o">.</span><span class="n">button</span><span class="o">.</span><span class="n">on</span><span class="p">(</span><span class="ss">:clicked</span><span class="p">)</span> <span class="p">{</span> <span class="n">inc</span> <span class="o">=</span> <span class="o">-</span><span class="n">inc</span> <span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>Here <code>win.display</code> is a <code>QTextEdit</code> and
<code>win.button</code> is a <code>QPushButton</code>. What the example does is count up
seconds in the <code>QTextEdit</code>, and toggle between that and counting down
when the button is pressed. Pretty trivial, granted, but in C++ you
would need to define two slots to do that, plus keep track of the
direction in which you are counting by using a member variable. In ruby
you can just use local variables without littering the parent scope.</p>

<h3 id="toolkit-independence">Toolkit Independence</h3>

<p>If you write your application in C++, once you’ve decided that you are
going to use KDElibs (or Qt), that decision is pretty much set in stone.
You can’t even switch from KDE to pure Qt very easily, and it’s so hard
to support both environments in a single code base, to make it almost
completely not worth the effort. Of course, this might not seem that big
of an issue, given that KDE is now a lot more portable, but not many
people are running KDE SC on Windows or Mac at the moment, and if you
want to reach as many users as possible, having a Qt-only version of
your application is going to help a lot. So Kaya can currently run on
Qt-only as well as KDE. When in KDE mode, all the usual KDE goodies are
running under the hood: KPushButtons, KDialog, K-everything, and of
course KXMLGUI and all the good stuff that comes with it, but it can
switch to plain Qt classes and a simplistic replacement for the XML GUI
if you don’t have KDElibs installed.</p>

<h3 id="easier-automatic-testing">Easier Automatic Testing</h3>

<p>Dynamicity, mock objects, plus the whole culture of test-driven
development that characterizes the ruby ecosystem make it a lot easier
to devise automated tests for your application. Effective GUI testing is
still basically an open problem in software engineering, so don’t expect
it to be a piece of cake, but in my experience, you can definitely reach
a considerably higher test coverage with ruby than with C++.</p>

<h3 id="trivial-extensibility-through-plugins">Trivial Extensibility Through Plugins</h3>

<p>KDE really shines when it comes to making applications easily extensible
via scripts or plugins, but it can’t compare with ruby. The distinction
between plugins and user scripts is now nonexistent, and loading a
plugin is just a matter of calling the ruby <code>load</code> function. Creating a
sensibly extensible application still requires careful planning, of
course, but it’s a lot easier when you can directly expose application
functionality to plugins, without creating tons and tons of interfaces
for even the most trivial uses. In Kaya, the use of a dynamically typed
language turned out to be key: the flexibility required to make its
plugins easy enough to write without knowing much about the application
internals is pretty much impossible to achieve in a statically typed
context.</p>

<h2 id="disadvantages">Disadvantages</h2>

<p>Of course, like everything in software engineering, choosing ruby over
C++ for GUI development involves a number of tradeoffs.</p>

<h3 id="performance">Performance</h3>

<p>Everyone knows that ruby is slow. Painfully slow sometimes. That seems
to be improving with 1.9, but ruby isn’t going to get faster than C
anytime soon. Now, the good thing is that its poor performance is almost
always completely irrelevant. A typical GUI application is nowhere near
being CPU bound, and the few computationally intensive parts are usually
in the GUI library anyway. That said, if you have performance critical
sections in your application, you are better off writing them in C/C++
and accessing them from your ruby code using the ruby extension API. For
example, Kaya includes a small C++ extension to supplement the missing
blur functionality in Qt &lt; 4.6. Writing it and integrating it was
trivial.</p>

<h3 id="testing-is-essential">Testing is Essential</h3>

<p>Automated testing is very important for software written in any
language, but for dynamic languages you simply can’t do without it.
Untested code will inevitably contain silly typos and type errors which
will cause it to crash in your user’s faces, or in the best case will
make your testing sessions painful and slow. So you don’t have any
choice but to add lots and lots of unit tests, integration tests and the
like. Units tests for pure GUI code are not really effective, useful or
easy to write, but you can settle on smoke tests that will cover the
most common mistakes and be pretty confident that no silly errors will
pop up that a compiler would catch.</p>

<h2 id="conclusion">Conclusion</h2>

<p>I hope this gives a nice overview of the benefits you can get by
switching from C++ to a dynamic language for your KDE (or general GUI)
development. I used ruby as the main example, because that’s what I have
experience with, but most of the points probably apply to dynamic
languages in general (python, perl, clojure, etc). In the following
posts, I will dig a little bit more into Kaya’s code to show the kinds
of tricks that I employed to better exploit the advantages that I
discussed, and offer even more ways to get the most out of ruby for GUI
programming. Stay tuned!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Monads for Markov chains]]></title>
    <link href="http://pcapriotti.github.com/blog/2008/10/16/monads-for-markov-chains/"/>
    <updated>2008-10-16T12:00:00+01:00</updated>
    <id>http://pcapriotti.github.com/blog/2008/10/16/monads-for-markov-chains</id>
    <content type="html"><![CDATA[<p>Suppose you need to model a finite <a href="http://en.wikipedia.org/wiki/Markov_chain">Markov chain</a> in code. There are essentially two ways of doing that: one is to simply run a simulation of the Markov chain using a random number generator to obtain dice rolls and random cards from the decks, the other is to create a <a href="http://en.wikipedia.org/wiki/Stochastic_matrix">stochastic matrix</a> containing the transition probabilities for each pair of states. In this post I will show how a single monadic description of the Markov chain dynamics can be used to obtain both a simulator and the transition matrix.</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="co">{-# LANGUAGE MultiParamTypeClasses, </span>
<span class="co">&gt;     FlexibleInstances, </span>
<span class="co">&gt;     GeneralizedNewtypeDeriving #-}</span>
<span class="ot">&gt;</span> 
<span class="ot">&gt;</span> <span class="kw">import</span> Control.Arrow
<span class="ot">&gt;</span> <span class="kw">import</span> Control.Monad
<span class="ot">&gt;</span> <span class="kw">import</span> Control.Monad.State.Strict
<span class="ot">&gt;</span> <span class="kw">import</span> Data.Array
<span class="ot">&gt;</span> <span class="kw">import</span> Random</code></pre>
<p>Let&#8217;s start with an example of Markov chain and how we would like to be able to implement in Haskell. Consider a simplified version of the familiar <a href="http://en.wikipedia.org/wiki/Monopoly_(game)">Monopoly</a> game: there are 40 squares (numbered 0 to 39), you throw two 6-sided dice each turn, some special squares have particular effects (see below), if you get a double roll three times in a row, you go to jail. The special squares are: 30: go to jail 2, 17, 33: Community Chest 7, 22, 36: Chance Community Chest (CC) and Chance (CH) make you take a card from a deck and move to some other place depending on what&#8217;s written on the card. You will find the details on the code, so I won&#8217;t explain them here. This is of course a Markov chain, where the states can be represented by:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">type</span> <span class="dt">Square</span> <span class="fu">=</span> <span class="dt">Int</span>
<span class="ot">&gt;</span> <span class="kw">data</span> <span class="dt">GameState</span> <span class="fu">=</span> <span class="dt">GS</span> {
<span class="ot">&gt;       position ::</span> <span class="dt">Square</span>,
<span class="ot">&gt;       doubles ::</span> <span class="dt">Int</span> } <span class="kw">deriving</span> (<span class="kw">Eq</span>, <span class="kw">Ord</span>, <span class="kw">Show</span>)</code></pre>
<p>and a description of the game can be given in a monadic style like this:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; sGO ::</span> <span class="dt">Square</span>
<span class="ot">&gt;</span> sGO <span class="fu">=</span> <span class="dv">0</span>
<span class="ot">&gt;</span> 
<span class="ot">&gt; sJAIL ::</span> <span class="dt">Square</span>
<span class="ot">&gt;</span> sJAIL <span class="fu">=</span> <span class="dv">10</span>
<span class="ot">&gt;</span> 
<span class="ot">&gt; finalize ::</span> <span class="dt">Square</span> <span class="ot">-&gt;</span> <span class="dt">Game</span> <span class="dt">Square</span>
<span class="ot">&gt;</span> finalize n
<span class="ot">&gt;</span>     <span class="fu">|</span> n <span class="fu">==</span> <span class="dv">2</span> <span class="fu">||</span> n <span class="fu">==</span> <span class="dv">17</span> <span class="fu">||</span> n <span class="fu">==</span> <span class="dv">33</span> <span class="fu">=</span> cc n
<span class="ot">&gt;</span>     <span class="fu">|</span> n <span class="fu">==</span> <span class="dv">7</span> <span class="fu">||</span> n <span class="fu">==</span> <span class="dv">22</span> <span class="fu">||</span> n <span class="fu">==</span> <span class="dv">36</span> <span class="fu">=</span> ch n
<span class="ot">&gt;</span>     <span class="fu">|</span> n <span class="fu">==</span> <span class="dv">30</span> <span class="fu">=</span> <span class="fu">return</span> sJAIL
<span class="ot">&gt;</span>     <span class="fu">|</span> <span class="fu">otherwise</span> <span class="fu">=</span> <span class="fu">return</span> n
<span class="ot">&gt;</span> 
<span class="ot">&gt; cc ::</span> <span class="dt">Square</span> <span class="ot">-&gt;</span> <span class="dt">Game</span> <span class="dt">Square</span>
<span class="ot">&gt;</span> cc n <span class="fu">=</span> <span class="kw">do</span> i <span class="ot">&lt;-</span> choose (<span class="dv">1</span><span class="ot"> ::</span> <span class="dt">Int</span>, <span class="dv">16</span>)
<span class="ot">&gt;</span>           <span class="fu">return</span> <span class="fu">$</span> <span class="kw">case</span> i <span class="kw">of</span>
<span class="ot">&gt;</span>                      <span class="dv">1</span> <span class="ot">-&gt;</span> sGO
<span class="ot">&gt;</span>                      <span class="dv">2</span> <span class="ot">-&gt;</span> sJAIL
<span class="ot">&gt;</span>                      _ <span class="ot">-&gt;</span> n
<span class="ot">&gt;</span> 
<span class="ot">&gt; ch ::</span> <span class="dt">Square</span> <span class="ot">-&gt;</span> <span class="dt">Game</span> <span class="dt">Square</span>
<span class="ot">&gt;</span> ch n <span class="fu">=</span> <span class="kw">do</span> i <span class="ot">&lt;-</span> choose (<span class="dv">1</span><span class="ot"> ::</span> <span class="dt">Int</span>, <span class="dv">16</span>)
<span class="ot">&gt;</span>           <span class="fu">return</span> <span class="fu">$</span> <span class="kw">case</span> i <span class="kw">of</span>
<span class="ot">&gt;</span>                      <span class="dv">1</span> <span class="ot">-&gt;</span> sGO
<span class="ot">&gt;</span>                      <span class="dv">2</span> <span class="ot">-&gt;</span> sJAIL
<span class="ot">&gt;</span>                      <span class="dv">3</span> <span class="ot">-&gt;</span> <span class="dv">11</span>
<span class="ot">&gt;</span>                      <span class="dv">4</span> <span class="ot">-&gt;</span> <span class="dv">24</span>
<span class="ot">&gt;</span>                      <span class="dv">5</span> <span class="ot">-&gt;</span> <span class="dv">39</span>
<span class="ot">&gt;</span>                      <span class="dv">6</span> <span class="ot">-&gt;</span> <span class="dv">5</span>
<span class="ot">&gt;</span>                      <span class="dv">7</span> <span class="ot">-&gt;</span> nextR n
<span class="ot">&gt;</span>                      <span class="dv">8</span> <span class="ot">-&gt;</span> nextR n
<span class="ot">&gt;</span>                      <span class="dv">9</span> <span class="ot">-&gt;</span> nextU n
<span class="ot">&gt;</span>                      <span class="dv">10</span> <span class="ot">-&gt;</span> n <span class="fu">-</span> <span class="dv">3</span>
<span class="ot">&gt;</span>                      _ <span class="ot">-&gt;</span> n
<span class="ot">&gt;</span>     <span class="kw">where</span>
<span class="ot">&gt;</span>       nextR n <span class="fu">=</span> <span class="kw">let</span> n&#39; <span class="fu">=</span> n <span class="fu">+</span> <span class="dv">5</span>
<span class="ot">&gt;</span>                 <span class="kw">in</span> n&#39; <span class="fu">-</span> (n&#39; <span class="ot">`mod`</span> <span class="dv">5</span>)
<span class="ot">&gt;</span>       nextU n
<span class="ot">&gt;</span>           <span class="fu">|</span> n <span class="fu">&gt;=</span> <span class="dv">12</span> <span class="fu">&amp;&amp;</span> n <span class="fu">&lt;</span> <span class="dv">28</span> <span class="fu">=</span> <span class="dv">28</span>
<span class="ot">&gt;</span>           <span class="fu">|</span> <span class="fu">otherwise</span> <span class="fu">=</span> <span class="dv">12</span>
<span class="ot">&gt;</span> 
<span class="ot">&gt; roll ::</span> <span class="dt">Game</span> (<span class="dt">Int</span>, <span class="dt">Int</span>)
<span class="ot">&gt;</span> roll <span class="fu">=</span> <span class="kw">let</span> r1 <span class="fu">=</span> choose (<span class="dv">1</span>, <span class="dv">6</span>)
<span class="ot">&gt;</span>        <span class="kw">in</span> liftM2 (,) r1 r1
<span class="ot">&gt;</span> 
<span class="ot">&gt; markDouble ::</span> <span class="dt">Bool</span> <span class="ot">-&gt;</span> <span class="dt">Game</span> ()
<span class="ot">&gt;</span> markDouble <span class="kw">True</span> <span class="fu">=</span> modify <span class="fu">$</span> \s <span class="ot">-&gt;</span> s {
<span class="ot">&gt;</span>                     doubles <span class="fu">=</span> doubles s <span class="fu">+</span> <span class="dv">1</span> }
<span class="ot">&gt;</span> markDouble <span class="kw">False</span> <span class="fu">=</span> modify <span class="fu">$</span> \s <span class="ot">-&gt;</span> s {
<span class="ot">&gt;</span>                      doubles <span class="fu">=</span> <span class="dv">0</span>
<span class="ot">&gt;</span>                    }
<span class="ot">&gt;</span> 
<span class="ot">&gt; goTo ::</span> <span class="dt">Square</span> <span class="ot">-&gt;</span> <span class="dt">Game</span> ()
<span class="ot">&gt;</span> goTo n <span class="fu">=</span> <span class="kw">let</span> n&#39; <span class="fu">=</span> n <span class="ot">`mod`</span> <span class="dv">40</span>
<span class="ot">&gt;</span>          <span class="kw">in</span> modify <span class="fu">$</span> \s <span class="ot">-&gt;</span> s { position <span class="fu">=</span> n&#39; }
<span class="ot">&gt;</span> 
<span class="ot">&gt; game ::</span> <span class="dt">Game</span> ()
<span class="ot">&gt;</span> game <span class="fu">=</span> <span class="kw">do</span> n <span class="ot">&lt;-</span> liftM position get
<span class="ot">&gt;</span>           (a, b) <span class="ot">&lt;-</span> roll
<span class="ot">&gt;</span>           markDouble (a <span class="fu">==</span> b)
<span class="ot">&gt;</span>           d <span class="ot">&lt;-</span> liftM doubles get
<span class="ot">&gt;</span>           <span class="kw">if</span> d <span class="fu">==</span> <span class="dv">3</span>
<span class="ot">&gt;</span>            <span class="kw">then</span> <span class="kw">do</span> markDouble <span class="kw">False</span>
<span class="ot">&gt;</span>                    goTo sJAIL
<span class="ot">&gt;</span>            <span class="kw">else</span> <span class="kw">do</span> <span class="kw">let</span> n&#39; <span class="fu">=</span> n <span class="fu">+</span> a <span class="fu">+</span> b
<span class="ot">&gt;</span>                    n&#39;&#39; <span class="ot">&lt;-</span> finalize n&#39;
<span class="ot">&gt;</span>                    goTo n&#39;&#39;</code></pre>
<p>As you can see, <code>Game</code> is a state monad, with an additional function <code>choose</code> that gives us a random element of a range:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">class</span> <span class="dt">MonadState</span> s m <span class="ot">=&gt;</span> <span class="dt">MonadMC</span> s m <span class="kw">where</span>
<span class="ot">&gt;     choose ::</span> (<span class="kw">Enum</span> a) <span class="ot">=&gt;</span> (a, a) <span class="ot">-&gt;</span> m a</code></pre>
<p>This can be implemented very easily using the (strict) state monad and a random generator:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">newtype</span> <span class="dt">MCSim</span> s a <span class="fu">=</span> <span class="dt">MCSim</span> (<span class="dt">State</span> ([s], <span class="dt">StdGen</span>) a)
<span class="ot">&gt;</span>     <span class="kw">deriving</span> <span class="kw">Monad</span>
<span class="ot">&gt;</span> 
<span class="ot">&gt;</span> <span class="kw">instance</span> <span class="dt">MonadState</span> s (<span class="dt">MCSim</span> s) <span class="kw">where</span>
<span class="ot">&gt;</span>     get <span class="fu">=</span> <span class="dt">MCSim</span> <span class="fu">$</span> liftM (<span class="fu">head</span> <span class="fu">.</span> <span class="fu">fst</span>) get
<span class="ot">&gt;</span>     put x <span class="fu">=</span> <span class="dt">MCSim</span> <span class="fu">.</span> modify <span class="fu">$</span> \(xs, g) <span class="ot">-&gt;</span> (x <span class="fu">:</span> xs, g)
<span class="ot">&gt;</span> 
<span class="ot">&gt;</span> <span class="kw">instance</span> <span class="dt">MonadMC</span> s (<span class="dt">MCSim</span> s) <span class="kw">where</span>
<span class="ot">&gt;</span>     choose (a, b) <span class="fu">=</span> <span class="dt">MCSim</span> <span class="fu">$</span>
<span class="ot">&gt;</span>                     <span class="kw">do</span> (xs, g) <span class="ot">&lt;-</span> get
<span class="ot">&gt;</span>                        <span class="kw">let</span> bnds <span class="fu">=</span> (<span class="fu">fromEnum</span> a, <span class="fu">fromEnum</span> b)
<span class="ot">&gt;</span>                        <span class="kw">let</span> (y, g&#39;) <span class="fu">=</span> randomR bnds g
<span class="ot">&gt;</span>                        put (xs, g&#39;)
<span class="ot">&gt;</span>                        <span class="fu">return</span> <span class="fu">.</span> <span class="fu">toEnum</span> <span class="fu">$</span> y
<span class="ot">&gt;</span> 
<span class="ot">&gt;</span> <span class="co">-- type Game a = MCSim GameState a</span>
<span class="ot">&gt;</span> 
<span class="ot">&gt; runSim ::</span> <span class="dt">StdGen</span> <span class="ot">-&gt;</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> s <span class="ot">-&gt;</span> <span class="dt">MCSim</span> s () <span class="ot">-&gt;</span> [s]
<span class="ot">&gt;</span> runSim g n start m <span class="fu">=</span> <span class="fu">fst</span> <span class="fu">$</span> execState m&#39; ([start], g)
<span class="ot">&gt;</span>     <span class="kw">where</span>
<span class="ot">&gt;</span>       (<span class="dt">MCSim</span> m&#39;) <span class="fu">=</span> <span class="fu">foldr</span> (<span class="fu">&gt;&gt;</span>) (<span class="fu">return</span> ()) <span class="fu">$</span> <span class="fu">replicate</span> n m</code></pre>
<p>The <code>runSim</code> function runs the simulation and returns the list of visited states. This is already quite nice, but the best thing is that the <em>same</em> code can be used to create the transition matrix, just swapping in a new implementation of the <code>Game</code> type alias:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">newtype</span> <span class="dt">MC</span> s a <span class="fu">=</span> <span class="dt">MC</span> (s <span class="ot">-&gt;</span> [(s, <span class="dt">Double</span>, a)])
<span class="ot">&gt;</span> 
<span class="ot">&gt;</span> <span class="kw">instance</span> <span class="kw">Monad</span> (<span class="dt">MC</span> s) <span class="kw">where</span>
<span class="ot">&gt;</span>     <span class="fu">return</span> x <span class="fu">=</span> <span class="dt">MC</span> <span class="fu">$</span> \s <span class="ot">-&gt;</span> <span class="fu">return</span> (s, <span class="dv">1</span><span class="fu">.</span><span class="dv">0</span>, x)
<span class="ot">&gt;</span>     (<span class="dt">MC</span> m) <span class="fu">&gt;&gt;=</span> f <span class="fu">=</span> <span class="dt">MC</span> <span class="fu">$</span> \s <span class="ot">-&gt;</span>
<span class="ot">&gt;</span>                    <span class="kw">do</span> (s, p, x) <span class="ot">&lt;-</span> m s
<span class="ot">&gt;</span>                       <span class="kw">let</span> (<span class="dt">MC</span> m&#39;) <span class="fu">=</span> f x
<span class="ot">&gt;</span>                       (s&#39;, q, y) <span class="ot">&lt;-</span> m&#39; s
<span class="ot">&gt;</span>                       <span class="fu">return</span> (s&#39;, p <span class="fu">*</span> q, y)
<span class="ot">&gt;</span> 
<span class="ot">&gt;</span> <span class="kw">instance</span> <span class="dt">MonadState</span> s (<span class="dt">MC</span> s) <span class="kw">where</span>
<span class="ot">&gt;</span>     get <span class="fu">=</span> <span class="dt">MC</span> <span class="fu">$</span> \s <span class="ot">-&gt;</span> <span class="fu">return</span> (s, <span class="dv">1</span><span class="fu">.</span><span class="dv">0</span>, s)
<span class="ot">&gt;</span>     put x <span class="fu">=</span> <span class="dt">MC</span> <span class="fu">$</span> \s <span class="ot">-&gt;</span> <span class="fu">return</span> (x, <span class="dv">1</span><span class="fu">.</span><span class="dv">0</span>, ())
<span class="ot">&gt;</span> 
<span class="ot">&gt;</span> <span class="kw">instance</span> <span class="dt">MonadMC</span> s (<span class="dt">MC</span> s) <span class="kw">where</span>
<span class="ot">&gt;</span>     choose (a, b) <span class="fu">=</span> <span class="kw">let</span> r <span class="fu">=</span> [a<span class="fu">..</span>b]
<span class="ot">&gt;</span>                         p <span class="fu">=</span> <span class="fu">recip</span> <span class="fu">.</span> <span class="fu">fromIntegral</span> <span class="fu">.</span> <span class="fu">length</span> <span class="fu">$</span> r
<span class="ot">&gt;</span>                     <span class="kw">in</span> <span class="dt">MC</span> <span class="fu">$</span> \s <span class="ot">-&gt;</span> <span class="fu">map</span> (\x <span class="ot">-&gt;</span> (s, p, x)) r
<span class="ot">&gt;</span> 
<span class="ot">&gt;</span> <span class="kw">type</span> <span class="dt">Game</span> a <span class="fu">=</span> <span class="dt">MC</span> <span class="dt">GameState</span> a</code></pre>
<p>The idea is that we keep track of all possible destination states for a given state, with associated conditional probabilities. For those familiar with Eric Kidd&#8217;s <a href="http://www.randomhacks.net/articles/2007/02/21/refactoring-probability-distributions">series on probability monads</a>, this is basically:</p>
<p>type MC s a = StateT s (PerhapsT [] a)</p>
<p>Now, how to get a transition matrix from such a monad? Of course, we have to require that the states are <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data-Ix.html">indexable</a>:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; markov ::</span> <span class="kw">Ix</span> s <span class="ot">=&gt;</span>
<span class="ot">&gt;</span>           <span class="dt">MC</span> s () <span class="ot">-&gt;</span> (s, s) <span class="ot">-&gt;</span> <span class="dt">Array</span> (s, s) <span class="dt">Double</span>
<span class="ot">&gt;</span> markov (<span class="dt">MC</span> m) r <span class="fu">=</span> accumArray (<span class="fu">+</span>) <span class="dv">0</span><span class="fu">.</span><span class="dv">0</span> (double r) <span class="fu">$</span>
<span class="ot">&gt;</span>                 <span class="fu">range</span> r <span class="fu">&gt;&gt;=</span> transitions
<span class="ot">&gt;</span>     <span class="kw">where</span>
<span class="ot">&gt;</span>       mkAssoc s (s&#39;, p, _) <span class="fu">=</span> ((s, s&#39;), p)
<span class="ot">&gt;</span>       transitions s <span class="fu">=</span> <span class="fu">map</span> (mkAssoc s) <span class="fu">$</span> m s
<span class="ot">&gt;</span>     double (a, b) <span class="fu">=</span> ((a, a), (b, b))</code></pre>
<p>So we iterate over all states and use the probability values contained in the monad to fill in the array cells corresponding to the selected state pair. To actually apply this to our Monopoly example, we need to make <code>GameState</code> indexable:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; nextState ::</span> <span class="dt">GameState</span> <span class="ot">-&gt;</span> <span class="dt">GameState</span>
<span class="ot">&gt;</span> nextState (<span class="dt">GS</span> p d) <span class="fu">=</span> <span class="kw">if</span> d <span class="fu">==</span> <span class="dv">2</span>
<span class="ot">&gt;</span>                      <span class="kw">then</span> <span class="dt">GS</span> (p <span class="fu">+</span> <span class="dv">1</span>) <span class="dv">0</span>
<span class="ot">&gt;</span>                      <span class="kw">else</span> <span class="dt">GS</span> p (d <span class="fu">+</span> <span class="dv">1</span>)
<span class="ot">&gt;</span> 
<span class="ot">&gt;</span> <span class="kw">instance</span> <span class="kw">Ix</span> <span class="dt">GameState</span> <span class="kw">where</span>
<span class="ot">&gt;</span>     <span class="fu">range</span> (s1, s2) <span class="fu">=</span> <span class="fu">takeWhile</span> (<span class="fu">&lt;=</span> s2) <span class="fu">.</span>
<span class="ot">&gt;</span>                      <span class="fu">iterate</span> nextState <span class="fu">$</span> s1
<span class="ot">&gt;</span>     <span class="fu">index</span> (s1, s2) s <span class="fu">=</span>
<span class="ot">&gt;</span>         <span class="kw">let</span> poss <span class="fu">=</span> (position s1, position s2)
<span class="ot">&gt;</span>         <span class="kw">in</span> <span class="fu">index</span> poss (position s) <span class="fu">*</span> <span class="dv">3</span> <span class="fu">+</span>
<span class="ot">&gt;</span>            doubles s <span class="fu">-</span> doubles s1
<span class="ot">&gt;</span>     <span class="fu">inRange</span> (s1, s2) s <span class="fu">=</span> s1 <span class="fu">&lt;=</span> s <span class="fu">&amp;&amp;</span> s <span class="fu">&lt;=</span> s2
<span class="ot">&gt;</span>     <span class="fu">rangeSize</span> (s1, s2) <span class="fu">=</span> <span class="fu">index</span> (s1, s2) s2 <span class="fu">+</span> <span class="dv">1</span></code></pre>
<p>then finally we can try:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; monopoly ::</span> (<span class="dt">GameState</span>, <span class="dt">GameState</span>)
<span class="ot">&gt;</span> monopoly <span class="fu">=</span> (<span class="dt">GS</span> <span class="dv">0</span> <span class="dv">0</span>, <span class="dt">GS</span> <span class="dv">39</span> <span class="dv">2</span>)
<span class="ot">&gt;</span> 
<span class="ot">&gt; initialState ::</span> <span class="dt">Array</span> <span class="dt">GameState</span> <span class="dt">Double</span>
<span class="ot">&gt;</span> initialState <span class="fu">=</span> <span class="kw">let</span> n <span class="fu">=</span> <span class="fu">rangeSize</span> monopoly
<span class="ot">&gt;</span>                    p <span class="fu">=</span> <span class="fu">recip</span> <span class="fu">$</span> <span class="fu">fromIntegral</span> n
<span class="ot">&gt;</span>                <span class="kw">in</span> listArray monopoly <span class="fu">$</span> <span class="fu">replicate</span> n p
<span class="ot">&gt;</span> 
<span class="ot">&gt; statDistr ::</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> [(<span class="dt">GameState</span>, <span class="dt">Double</span>)]
<span class="ot">&gt;</span> statDistr n <span class="fu">=</span> <span class="kw">let</span> mat <span class="fu">=</span> markov game monopoly
<span class="ot">&gt;</span>                   distributions <span class="fu">=</span> <span class="fu">iterate</span> (<span class="fu">.*</span> mat)
<span class="ot">&gt;</span>                         initialState
<span class="ot">&gt;</span>                   st <span class="fu">=</span> distributions <span class="fu">!!</span> n
<span class="ot">&gt;</span>               <span class="kw">in</span> assocs st</code></pre>
<p>where .* is a simple vector-matrix multiplication function:</p>
<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt;</span> <span class="kw">infixl</span> <span class="dv">5</span> <span class="fu">.*</span>
<span class="ot">&gt; (.*) ::</span> (<span class="kw">Ix</span> i, <span class="kw">Num</span> a) <span class="ot">=&gt;</span>
<span class="ot">&gt;</span>            <span class="dt">Array</span> i a <span class="ot">-&gt;</span> <span class="dt">Array</span> (i, i) a <span class="ot">-&gt;</span> <span class="dt">Array</span> i a
<span class="ot">&gt;</span> (<span class="fu">.*</span>) x y <span class="fu">=</span> array resultBounds
<span class="ot">&gt;</span>               [(i, <span class="fu">sum</span> [x<span class="fu">!</span>k <span class="fu">*</span> y<span class="fu">!</span>(k,i) <span class="fu">|</span> k <span class="ot">&lt;-</span> <span class="fu">range</span> (l,u)])
<span class="ot">&gt;</span>                <span class="fu">|</span> i <span class="ot">&lt;-</span> <span class="fu">range</span> (l&#39;&#39;,u&#39;&#39;) ]
<span class="ot">&gt;</span>         <span class="kw">where</span> (l, u) <span class="fu">=</span> bounds x
<span class="ot">&gt;</span>               ((l&#39;, l&#39;&#39;), (u&#39;, u&#39;&#39;)) <span class="fu">=</span> bounds y
<span class="ot">&gt;</span>               resultBounds
<span class="ot">&gt;</span>                 <span class="fu">|</span> (l,u)<span class="fu">==</span>(l&#39;,u&#39;) <span class="fu">=</span> (l&#39;&#39;, u&#39;&#39;)
<span class="ot">&gt;</span>                 <span class="fu">|</span> <span class="fu">otherwise</span> <span class="fu">=</span> <span class="fu">error</span> <span class="st">&quot;.*: incompatible bounds&quot;</span></code></pre>
<p>Calling <code>statDistr 100</code> will return an association list of states with corresponding probability in an approximation of the stationary distribution, computed by applying the <a href="http://en.wikipedia.org/wiki/Power_method">power method</a> to the transition matrix. The number 100 is a pure guess, I don&#8217;t know how to estimate the number of iterations necessary for convergence, but that is out of the scope of this post, anyway.</p>
]]></content>
  </entry>
  
</feed>
