<?xml version="1.0" ?><rss version="2.0">  <channel>    <title>apfeλmus</title>    <link>http://apfelmus.nfshost.com/</link>    <description>Dessert for Dining Philosophers</description>
    <language>en-us</language>
    <docs>http://cyber.law.harvard.edu/rss/rss.html</docs>
<item>
<title>The Operational Monad Tutorial</title>
<link>http://apfelmus.nfshost.com/articles/operational-monad.html</link>
<pubDate>Wed, 03 Feb 2010 15:11:43 +0100</pubDate>
<description><![CDATA[
<ul>
<li><a href="#introduction">Introduction</a></li>
<li><a href="#stack-machine---list-of-instructions">Stack Machine - List of Instructions?</a><ul>
<li><a href="#representation">Representation</a></li>
<li><a href="#concatenation-and-thoughts-on-the-interface">Concatenation and thoughts on the interface</a></li>
<li><a href="#interpreter">Interpreter</a></li>
<li><a href="#oops">Oops</a></li>
</ul></li>
<li><a href="#stack-machine---monad">Stack Machine - Monad</a><ul>
<li><a href="#representation-1">Representation</a><ul>
<li><a href="#return-types">Return types</a></li>
<li><a href="#binding-variables">Binding variables</a></li>
<li><a href="#empty-program">Empty program</a></li>
<li><a href="#the-fancy-list">The fancy list</a></li>
</ul></li>
<li><a href="#interpreter-1">Interpreter</a></li>
<li><a href="#concatenation-and-interface">Concatenation and interface</a></li>
</ul></li>
<li><a href="#what-weve-done-so-far">What we’ve done so far</a></li>
<li><a href="#multiple-interpreters">Multiple Interpreters</a><ul>
<li><a href="#random-numbers">Random Numbers</a></li>
</ul></li>
<li><a href="#monadic-parser-combinators">Monadic Parser Combinators</a><ul>
<li><a href="#primitives">Primitives</a></li>
<li><a href="#a-first-implementation">A first implementation</a></li>
<li><a href="#a-note-on-technique">A note on technique</a></li>
<li><a href="#depth-first">Depth-first</a></li>
<li><a href="#breadth-first">Breadth-first</a></li>
</ul></li>
<li><a href="#conclusion">Conclusion</a><ul>
<li><a href="#further-examples">Further Examples</a></li>
<li><a href="#connection-with-the-continuation-monad">Connection with the Continuation Monad</a></li>
<li><a href="#drawbacks">Drawbacks</a></li>
</ul></li>
<li><a href="#about-the-author">About the author</a></li>
</ul>
<p><em>This article was first published in <a href="http://themonadreader.wordpress.com/2010/01/26/issue-15/">issue 15</a> of <a href="http://themonadreader.wordpress.com/">The Monad.Reader</a>.</em></p>
<h1 id="introduction"><a href="#TOC">Introduction</a></h1>
<p>Another monad tutorial? Oh my god, why!? Fear not, this article is aimed at Haskellers who are already familiar with monads, though I have of course tried to keep the material as accessible as possible; the first two sections may serve as an initial introduction to monads and the monad laws for the brave.</p>
<p>In this tutorial, I would like to present monads from the viewpoint of <em><a href="http://www.cse.iitd.ernet.in/~sanjiva/opsem.ps">operational semantics</a></em> and how it makes designing and implementing new monads a piece of cake. Put differently, <code>s -&gt; (a,s)</code> is not the only way to implement the state monad and this tutorial aims to present a much more systematic way. I think it is still regrettably underused, hence this text.</p>
<p>The main idea is to view monads as a sequence of instructions to be executed by a machine, so that the task of implementing monads is equivalent to writing an interpreter. The introductory example will be a stack automaton, followed by a remark on a monad for random numbers. Then, to showcase the simplicity of this approach, we will implement backtracking parser combinators, culminating in a straightforward breadth-first implementation equivalent to <a href="http://www.cs.chalmers.se/~koen/pubs/entry-jfp04-parser.html" title="Koen Claessen. Parallel Parsing Processes">Claessen’s parallel parsing processes</a>.</p>
<p>For those in the know, I’m basically going to present the principles of <a href="http://web.cecs.pdx.edu/~cklin/papers/unimo-143.pdf" title="Chuan-kai Lin. Programming Monads Operationally with Unimo.">Chuan-kai Lin’s Unimo paper</a>. The approach is neither new nor unfamiliar; for example, <a href="http://citeseer.ist.psu.edu/hughes95design.html" title="John Hughes. The Design of a Pretty-printing Library.">John Hughes</a> already used it to derive the state monad. But until I read Lin’s paper, I did not understand how valuable it is when done systematically and in Haskell. <a href="http://hackage.haskell.org/package/MonadPrompt" title="Ryan Ingram&#39;s MonadPrompt package">Ryan Ingram’s <code>MonadPrompt</code></a> package is another recent formulation.</p>
<p>To encourage reuse, I have also released a package <a href="http://hackage.haskell.org/package/operational" title="Heinrich Apfelmus&#39; operational package"><code>operational</code></a> on <a href="http://hackage.haskell.org">hackage</a> which collects the generic bits of these ideas in a small library. For convenient study, the <a href="http://apfelmus.nfshost.com/articles/operational-monad/code.zip" title="Accompanying source code for &#39;The Operational Monad Tutorial&#39;">source code</a> from each section of this article is also available.</p>
<h1 id="stack-machine---list-of-instructions"><a href="#TOC">Stack Machine - List of Instructions?</a></h1>
<p>Our introductory example will be a stack machine, i.e. an imperative mini-language featuring two instructions <code>push</code> and <code>pop</code> for pushing and popping values onto and from a stack.</p>
<p>In other words, I have imperative programs like the following in mind:</p>
<pre><code>push 5; push 42; pop;</code></pre>
<p>Instructions are separated by semicolons. As shown in the following picture, this program first puts the number <code>5</code> on the stack, then puts the number <code>42</code> on top of the stack and proceeds to remove it again.</p>
<img src="http://apfelmus.nfshost.com/articles/operational-monad/stack.png" alt="Example stack program" width="400"/>
<p>How can we embed such programs into Haskell?</p>
<h2 id="representation"><a href="#TOC">Representation</a></h2>
<p>First we need some way of representing the program text, for instance as a list of instructions:</p>
<pre><code>type Program instr    = [instr]
type StackProgram     = Program StackInstruction
data StackInstruction = Push Int | Pop</code></pre>
<p>Our example is represented as</p>
<pre><code>example = Push 5 : Push 42 : Pop : []</code></pre>
<p>In a sense, the colon <code>(:)</code> for building lists takes the role of the semicolon for sequencing instructions.</p>
<h2 id="concatenation-and-thoughts-on-the-interface"><a href="#TOC">Concatenation and thoughts on the interface</a></h2>
<p>Note that this representation gives us a very convenient tool for assembling bigger programs from smaller subprograms: list concatenation <code>(++)</code>. For instance,</p>
<pre><code>exampleTwice = example ++ example
    = Push 5 : Push 42 : Pop : Push 5 : Push 42 : Pop : []</code></pre>
<p>is a program that executes <code>example</code> twice. Together with the empty program</p>
<pre><code>empty = []</code></pre>
<p>concatenation obeys the following three well-known laws:</p>
<pre><code> empty ++ is      =  is                  -- left unit
    is ++ empty   =  is                  -- right unit
(is ++ js) ++ ks  =  is ++ (js ++ ks)    -- associativity</code></pre>
<p>which seem almost too evident to be worth mentioning. For example, it is customary to leave out the parenthesis in the last line altogether.</p>
<p>Once accustomed to the notion of programs and <code>(++)</code> to combine them, the special case of single instructions and <code>(:)</code> for sequencing them is unnecessary. The user of our language does not care that we deem <code>push</code> and <code>pop</code> to be primitive operations but not, for example, the program</p>
<pre><code>replace a = Pop : Push a : []</code></pre>
<p>which replaces the topmost stack element with <code>a</code>; he is entirely content to be given two programs</p>
<pre><code>push :: Int -&gt; StackProgram
pop  :: StackProgram</code></pre>
<p>and two general combinators for building new ones</p>
<pre><code>empty :: StackProgram
(++)  :: StackProgram -&gt; StackProgram -&gt; StackProgram </code></pre>
<p>without any mention of the distinction between single instruction and compound program. Their difference is but an implementation detail.</p>
<h2 id="interpreter"><a href="#TOC">Interpreter</a></h2>
<p>Well, to be entirely content, the user also needs a way to run programs. In particular, we need to implement a function <code>interpret</code> that maps the program text to its intended meaning, here a function that transforms a stack of integers.</p>
<pre><code>type Stack a = [a]

interpret :: StackProgram -&gt; (Stack Int -&gt; Stack Int)</code></pre>
<p>The implementation follows the style of operational semantics: inspect the first instruction, change the stack accordingly, and recursively proceed with the remaining list of instructions <code>is</code>:</p>
<pre><code>interpret (Push a : is) stack = interpret is (a : stack)
interpret (Pop    : is) stack = interpret is (tail stack)
interpret []            stack = stack</code></pre>
<h2 id="oops"><a href="#TOC">Oops</a></h2>
<p>“All well and good, but why all the fuss with ‘monads’ then, when lists of instructions will do?” you may ask. Alas, the problem is of course that lists won’t do! We forgot something very important: our programs are completely unable to inspect values from the stack.</p>
<p>For instance, how to write a program that pops the two topmost values and pushes their sum onto the stack? Clearly, we want something like</p>
<pre><code>a &lt;- pop;
b &lt;- pop;
push (a+b);</code></pre>
<p>where each <code>pop</code> returns the element just removed and the arrow <code>&lt;-</code> binds it to a variable. But binding variables is simply impossible to express with our current representation of programs as lists of instructions.</p>
<h1 id="stack-machine---monad"><a href="#TOC">Stack Machine - Monad</a></h1>
<p>Well, if ordinary lists of instructions are not enough to represent programs that involve binding variables like</p>
<pre><code>a &lt;- pop; b &lt;- pop; push (a+b);</code></pre>
<p>then let’s invent some fancy kind of list of instructions that will! The following presentation will be in close analogy to the structure of the previous section.</p>
<h2 id="representation-1"><a href="#TOC">Representation</a></h2>
<h3 id="return-types"><a href="#TOC">Return types</a></h3>
<p>First, if we want to interpret <code>pop</code> as a function that returns something, we had better label it with the type of the value returned! Hence, instead of a plain type</p>
<pre><code>Pop :: StackInstruction</code></pre>
<p>we need an additional type argument</p>
<pre><code>Pop :: StackInstruction Int</code></pre>
<p>which indicates that the <code>Pop</code> instruction somehow returns a value of type <code>Int</code>.</p>
<p>For simplicity, we attribute a return type to <code>push</code> as well, even though it doesn’t really return anything. This can modeled just fine with the unit type <code>()</code>.</p>
<pre><code>Push 42 :: StackInstruction ()
Push    :: Int -&gt; StackInstruction ()</code></pre>
<p>Putting both together, our type of instructions will become</p>
<pre><code>data StackInstruction a where
    Pop  :: StackInstruction Int
    Push :: Int -&gt; StackInstruction ()</code></pre>
<p>If this syntax is alien to you: this is a <a href="http://www.haskell.org/haskellwiki/GADT">Generalized Algebraic Data Type</a> (GADT) which allows us to define a data type by declaring the types of its constructors directly. As of Haskell 2010, GADTs are not yet part of the language standard, but they are supported by <a href="http://www.haskell.org/ghc/">GHC</a>.</p>
<p>Like instructions, we also have to annotate programs with their return type, so that the definition for <code>StackProgram</code> becomes</p>
<pre><code>data Program instr a where ...

type StackProgram a = Program StackInstruction a</code></pre>
<p>As before, <code>instr</code> is the type of instructions, whereas <code>a</code> is the newly annotated return type.</p>
<h3 id="binding-variables"><a href="#TOC">Binding variables</a></h3>
<p>How to represent the binding of variables? Lambda abstractions will do the trick; imagine the following:</p>
<table class="noborder" style="width:30em;">
<tr><td width="50%">
take a binding
<td>
<code>a &lt;- pop; rest</code>
<tr><td>
turn the arrow to the right
<td>
<code>pop -&gt; a; rest</code>
<tr><td>
and use a lambda expression to move it past the semicolon
<td>
<code>pop; \a -&gt; rest</code>
</table>

<p>Voila, the last step can be represented in Haskell, with a constructor named <code>Then</code> taking the role of the semicolon:</p>
<pre><code>Pop `Then` \a -&gt; rest </code></pre>
<p>The idea is that <code>Then</code> plugs the value returned by <code>pop</code> into the variable <code>a</code>. By the way, this is akin to how <code>let</code> expressions can be expressed as lambda abstractions in Haskell:</p>
<pre><code>let a = foo in bar   &lt;=&gt;   (\a -&gt; bar) foo</code></pre>
<p>Anyway, our motivating example can now be represented as</p>
<pre><code>example2 = Pop `Then` (\a -&gt; Pop `Then`
                        (\b -&gt; Push (a+b) `Then` Return))</code></pre>
<p>where <code>Return</code> represents the empty program which we will discuss in a moment. Remember that parentheses around the lambda expressions are optional, so we can also write</p>
<pre><code>example2 = Pop `Then` \a -&gt;
           Pop `Then` \b -&gt;
           Push (a+b) `Then`
           Return</code></pre>
<p>It is instructive to think about the type of <code>Then</code>. It has to be</p>
<pre><code>Then :: instr a -&gt; (a -&gt; Program instr b) -&gt; Program instr b</code></pre>
<p>Except for the return type <code>a</code> in <code>instr a</code> and the lambda abstraction, this is entirely analogous to the “cons” operation <code>(:)</code> for lists.</p>
<h3 id="empty-program"><a href="#TOC">Empty program</a></h3>
<p>The empty program, corresponding to the empty list <code>[]</code>, is best represented by a constructor</p>
<pre><code>Return :: a -&gt; Program instr a</code></pre>
<p>that is not “entirely empty” but rather denotes a trivial instruction that just returns the given value <code>a</code> (hence the name). This is very useful, since we can now choose return values freely. For instance,</p>
<pre><code>example3 = Pop `Then` \a -&gt; Pop `Then` \b -&gt; Return (a*b)</code></pre>
<p>is a program that pops two values from the stack but whose return value is their product.</p>
<h3 id="the-fancy-list"><a href="#TOC">The fancy list</a></h3>
<p>Taking everything together, we obtain a fancy list of instructions, once again a GADT:</p>
<pre><code>data Program instr a where
    Then   :: instr a -&gt; (a -&gt; Program instr b) -&gt; Program instr b
    Return :: a -&gt; Program instr a</code></pre>
<p>And specialized to our stack machine language, we get</p>
<pre><code>type StackProgram a = Program StackInstruction a</code></pre>
<h2 id="interpreter-1"><a href="#TOC">Interpreter</a></h2>
<p>Before thinking thinking further about our new representation, let’s first write the interpreter to see the stack machine in action. This time, however, we are not interested in the final stack, only in the value returned.</p>
<pre><code>interpret :: StackProgram a -&gt; (Stack Int -&gt; a)
interpret (Push a `Then` is) stack     = interpret (is ()) (a:stack)
interpret (Pop    `Then` is) (b:stack) = interpret (is b ) stack
interpret (Return c)         stack     = c</code></pre>
<p>The implementation is like the previous one, except that now, we also have to pass the return values like <code>()</code> and <code>b</code> to the remaining instructions <code>is</code>.</p>
<p>Our example program executes as expected:</p>
<pre><code>GHCi&gt; interpret example3 [7,11]
77</code></pre>
<h2 id="concatenation-and-interface"><a href="#TOC">Concatenation and interface</a></h2>
<p>Just as with lists, we can build large programs by concatenating smaller subprograms. And as before, we don’t want the user to bother with the distinction between single instruction and compound program.</p>
<p>We begin with the latter: the function</p>
<pre><code>singleton :: instr a -&gt; Program instr a
singleton i = i `Then` Return</code></pre>
<p>takes the role of <code>\x -&gt; [x]</code> and helps us blur the line between program and instructions:</p>
<pre><code>pop  :: StackProgram Int
push :: Int -&gt; StackProgram ()

pop  = singleton Pop
push = singleton . Push</code></pre>
<p>Now, we define the concatenation operator (often dubbed “bind”) that glues two programs together:</p>
<pre><code>(&gt;&gt;=) :: Program i a -&gt; (a -&gt; Program i b) -&gt; Program i b
(Return a)    &gt;&gt;= js  = js a
(i `Then` is) &gt;&gt;= js  = i `Then` (\a -&gt; is a &gt;&gt;= js)</code></pre>
<p>Apart from the new symbol <code>(&gt;&gt;=)</code> and the new type signature, the purpose and implementation is entirely analogous to <code>(++)</code>. And as before, together with the empty program,</p>
<pre><code>return = Return</code></pre>
<p>it obeys three evident laws</p>
<pre><code>return a &gt;&gt;= is     = is a                        -- left unit
is &gt;&gt;= return       = is                          -- right unit
(is &gt;&gt;= js) &gt;&gt;= ks  = is &gt;&gt;= (\a -&gt; js a &gt;&gt;= ks)  -- associativity</code></pre>
<p>also called the <em>monad laws</em>. Since we need to pass return values, the laws are slightly different from the concatenation laws for ordinary lists, but their essence is the same.</p>
<p>The reason that these equations are called the “monad laws” is that any data type supporting two such operations and obeying the three laws is called a <em>monad</em>. In Haskell, monads are assembled in the type class <code>Monad</code>, so we’d have to make an instance</p>
<pre><code>instance Monad (Program instr) where
    (&gt;&gt;=)  = ...
    return = ...</code></pre>
<p>This is similar to lists which are said to constitute a <a href="http://haskell.org/haskellwiki/Monoid"><em>monoid</em></a>.</p>
<p>We conclude the first part of this tutorial by remarking that the <code>(&gt;&gt;=)</code> operator is the basis for many other functions that build big programs from small ones; these can be found in the <code>Control.Monad</code> module and are described <a href="************************">elsewhere</a>.</p>
<h1 id="what-weve-done-so-far"><a href="#TOC">What we’ve done so far</a></h1>
<p>Those familiar with the state monad will recognize that the whole stack machine was just</p>
<pre><code>State (Stack Int)</code></pre>
<p>in disguise. But surprisingly, we haven’t used the pattern <code>s -&gt; (a,s)</code> for threading state anywhere! Instead, we were able to implement the equivalent of</p>
<pre><code>evalState :: State s -&gt; (s -&gt; a)</code></pre>
<p>directly, even though the type <code>s -&gt; a</code> by itself is too “weak” to serve as an implementation of the state monad.</p>
<p>This is a very general phenomenon and it is of course the main benefit of the operational viewpoint and the new <code>Program instr a</code> type. No matter what we choose as interpreter function or instruction set, the monad laws for <code>(&gt;&gt;=)</code> and <code>return</code> will always hold, for they are entirely independent of these choices. This makes it much easier to define and implement new monads and the remainder of this article aims to give a taste of its power.</p>
<h1 id="multiple-interpreters"><a href="#TOC">Multiple Interpreters</a></h1>
<p>A first advantage of the operational approach is that it allows us to equip one and the same monad with multiple interpreters. We’ll demonstrate this flexibility with an example monad <code>Random</code> that expresses randomness and probability distributions.</p>
<p>The ability to write multiple interpreters is also very useful for implementing games, specifically to account for both human and computer opponents as well as replaying a game from a script. This is what prompted Ryan Ingram to write his <a href="http://hackage.haskell.org/package/MonadPrompt" title="Ryan Ingram&#39;s MonadPrompt package"><code>MonadPrompt</code> package</a>.</p>
<h2 id="random-numbers"><a href="#TOC">Random Numbers</a></h2>
<p>At the heart of random computations is a type <code>Random a</code> which denotes <em>random variables</em> taking values in <code>a</code>. Traditionally, the type <code>a</code> would be a numeric type like <code>Int</code>, so that <code>Random Int</code> denotes “random numbers”. But for the Haskell programmer, it is only natural to generalize it to any type <code>a</code>. This generalization is also very useful, because it reveals hidden structure: it turns out that <code>Random</code> is actually a monad.</p>
<p>There are two ways to implement this monad: one way is to interpret random variables as a recipe for creating pseudo-random values from a seed, which is commonly written</p>
<pre><code>type Random a = StdGen -&gt; (a,StdGen)</code></pre>
<p>The other is to view them as a probability distribution, as for example expressed in <a href="http://web.engr.oregonstate.edu/~erwig/pfp/" title="Martin Erwig, Steve Kollmansberger. Probabilistic Functional Programming.">probabilistic functional programming</a> as</p>
<pre><code>type Probability = Double
type Random a    = [(a,Probability)]</code></pre>
<p>Traditionally, we’d have to choose between one way or the other depending on the application. But with the operational approach, we can have our cake and eat it, too! The two ways of implementing random variables can be delegated to two different interpreter functions for one and the same monad <code>Random</code>.</p>
<p>For demonstration purposes, we represent <code>Random</code> as a language with just one instruction <code>uniform</code> that randomly selects an element from a list with uniform probability</p>
<pre><code>type Random a = Program RandomInstruction a

data RandomInstruction a where
    Uniform :: [a] -&gt; RandomInstruction a

uniform :: [a] -&gt; Random a
uniform = singleton . Uniform</code></pre>
<p>For example, a roll of a die is modeled as</p>
<pre><code>die :: Random Int
die = uniform [1..6]</code></pre>
<p>and the sum of two dice rolls is</p>
<pre><code>sum2Dies = die &gt;&gt;= \a -&gt; die &gt;&gt;= \b -&gt; return (a+b)</code></pre>
<p>Now, the two different interpretations are: sampling a random variable by generating pseudo-random values</p>
<pre><code>sample :: Random a -&gt; StdGen -&gt; (a,StdGen)
sample (Return a)             gen = (a,gen)
sample (Uniform xs `Then` is) gen = sample (is $ xs !! k) gen&#39;
    where (k,gen&#39;) = System.Random.randomR (0,length xs-1) gen</code></pre>
<p>and calculating its probability distribution</p>
<pre><code>distribution :: Random a -&gt; [(a,Probability)]
distribution (Return a)             = [(a,1)]
distribution (Uniform xs `Then` is) =
    [(a,p/n) | x &lt;- xs, (a,p) &lt;- distribution (is x)]
    where n = fromIntegral (length xs)</code></pre>
<p>Truth to be told, the <code>distribution</code> interpreter has a flaw, namely that it never tallies the probabilities of equal outcomes. That’s because this would require an additional <code>Eq a</code> constraint on the types of <code>return</code> and <code>(&gt;&gt;=)</code>, which is unfortunately not possible with the current <code>Monad</code> type class. A workaround for this known limitation can be found in the <code>norm</code> function from the <a href="http://web.engr.oregonstate.edu/~erwig/pfp/" title="Martin Erwig, Steve Kollmansberger. Probabilistic Functional Programming.">paper</a> on probabilistic functional programming.</p>
<h1 id="monadic-parser-combinators"><a href="#TOC">Monadic Parser Combinators</a></h1>
<p>Now, it is time to demonstrate that the operational viewpoint also makes the implementation of otherwise advanced monads a piece of cake. Our example will be <a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.2.4159" title="Graham Hutton, Erik Meijer. Monadic Parser Combinators.">monadic parser combinators</a> and for the remainder of this article, I will assume that you are somewhat familiar with them already. The goal will be to derive an implementation of <a href="http://www.cs.chalmers.se/~koen/pubs/entry-jfp04-parser.html" title="Koen Claessen. Parallel Parsing Processes">Koen Claessen’s ideas</a> from scratch.</p>
<h2 id="primitives"><a href="#TOC">Primitives</a></h2>
<p>At their core, monadic parser combinators are a monad <code>Parser</code> with just three primitives:</p>
<pre><code>symbol :: Parser Char
mzero  :: Parser a
mplus  :: Parser a -&gt; Parser a -&gt; Parser a</code></pre>
<p>which represent</p>
<ul>
<li>a parser that reads the next symbol from the input stream</li>
<li>a parser that never succeeds</li>
<li>a combinator that runs two parsers in parallel</li>
</ul>
<p>respectively. (The last two operations define the <code>MonadPlus</code> type class.) Furthermore, we need an interpreter, i.e. a function</p>
<pre><code>interpret :: Parser a -&gt; (String -&gt; [a])</code></pre>
<p>that runs the parser on the string and returns all successful parses.</p>
<p>The three primitives are enough to express virtually any parsing problem; here is an example of a parser <code>number</code> that recognizes integers:</p>
<pre><code>satisfies p = symbol &gt;&gt;= \c -&gt; if p c then return c else mzero 
many  p     = return [] `mplus` many1 p
many1 p     = liftM2 (:) p (many p) 
digit       = satisfies isDigit &gt;&gt;= \c -&gt; return (ord c - ord &#39;0&#39;)
number      = many1 digit &gt;&gt;= return . foldl (\x d -&gt; 10*x + d) 0</code></pre>
<h2 id="a-first-implementation"><a href="#TOC">A first implementation</a></h2>
<p>The instruction set for our parser language will of course consist of these three primitive operations:</p>
<pre><code>data ParserInstruction a where
    Symbol :: ParserInstruction Char
    MZero  :: ParserInstruction a
    MPlus  :: Parser a -&gt; Parser a -&gt; ParserInstruction a

type Parser a = Program ParserInstruction a</code></pre>
<p>A straightforward implementation of <code>interpret</code> looks like this:</p>
<pre><code>interpret :: Parser a -&gt; String -&gt; [a]
interpret (Return a)            s = if null s then [a] else []
interpret (Symbol    `Then` is) s = case s of
    c:cs -&gt; interpret (is c) cs
    []   -&gt; []
interpret (MZero     `Then` is) s = []
interpret (MPlus p q `Then` is) s =
    interpret (p &gt;&gt;= is) s ++ interpret (q &gt;&gt;= is) s</code></pre>
<p>For each instruction, we specify the intended effects, often calling <code>interpret</code> recursively on the remaining program <code>is</code>. In prose, the four cases are</p>
<ul>
<li><code>Return</code> at the end of a program will return a result if the input was parsed completely.</li>
<li><code>Symbol</code> reads a single character from the input stream if available and fails otherwise.</li>
<li><code>MZero</code> returns an empty result immediately.</li>
<li><code>MPlus</code> runs two parsers in parallel and collects their results.</li>
</ul>
<h2 id="a-note-on-technique"><a href="#TOC">A note on technique</a></h2>
<p>The cases for <code>MZero</code> and <code>MPlus</code> are a bit roundabout; the equations</p>
<pre><code>interpret mzero       = \s -&gt; []
interpret (mplus p q) = \s -&gt; interpret p s ++ interpret q s</code></pre>
<p>express our intention more plainly. Of course, these two equations do not constitute valid Haskell code for we may not pattern match on <code>mzero</code> or <code>mplus</code> directly. The only thing we may pattern match on is a constructor, for example like this</p>
<pre><code>interpret (Mplus p q `Then` is) = ...</code></pre>
<p>But even though our final Haskell code will have this form, this does not mean that jotting down the left hand side and thinking hard about the <code>...</code> is the best way to write Haskell code. No, we should rather use the full power of purely functional programming and use a more <em>calculational</em> approach, deriving the pattern matches from more evident equations like the ones above.</p>
<p>In this case, we can combine the two equations with the <code>MonadPlus</code> laws</p>
<pre><code>    mzero &gt;&gt;= m = mzero
mplus p q &gt;&gt;= m = mplus (p &gt;&gt;= m) (q &gt;&gt;= m)</code></pre>
<p>which specify how <code>mzero</code> and <code>mplus</code> interact with <code>(&gt;&gt;=)</code>, to derive the desired pattern match</p>
<pre><code>  interpret (Mplus p q `Then` is)
=   { definition of concatenation and mplus }
  interpret (mplus p q &gt;&gt;= is)
=   { MonadPlus law }
  interpret (mplus (p &gt;&gt;= is) (q &gt;&gt;= is))
=   { intended meaning }
  \s -&gt; interpret (p &gt;&gt;= is) s ++ interpret (q &gt;&gt;= is) s</code></pre>
<p>Now, in light of the first step of this derivation, I even suggest to forget about constructors entirely and instead regard</p>
<pre><code>interpret (mplus p q &gt;&gt;= is) = ...</code></pre>
<p>as “valid” Haskell code; after all, it is straightforwardly converted to a valid pattern match. In other words, it is once again beneficial to not distinguish between single instructions and compound programs, at least in notation.</p>
<h2 id="depth-first"><a href="#TOC">Depth-first</a></h2>
<p>Unfortunately, our first implementation has a potential space leak, namely in the case</p>
<pre><code>interpret (MPlus p q `Then` is) s =
    interpret (p &gt;&gt;= is) s ++ interpret (q &gt;&gt;= is) s</code></pre>
<p>The string <code>s</code> is shared by the recursive calls and has to be held in memory for a long time.</p>
<p>In particular, the implementation will try to parse <code>s</code> with the parser <code>p &gt;&gt;= is</code> first, and then backtrack to the beginning of <code>s</code> to parse it again with the second alternative <code>q &gt;&gt;= is</code>. That’s why this is called a <em>depth-first</em> or <em>backtracking</em> implementation. The string <code>s</code> has to be held in memory as long the second parser has not started yet.</p>
<h2 id="breadth-first"><a href="#TOC">Breadth-first</a></h2>
<p>To ameliorate the space leak, we would like to create a <em>breadth-first</em> implementation, one which does not try alternative parsers in sequence, but rather keeps a collection of all possible alternatives and advances them at once.</p>
<p>How to make this precise? The key idea is the following equation:</p>
<pre><code>  (symbol &gt;&gt;= is) `mplus` (symbol &gt;&gt;= js)
=  symbol &gt;&gt;= (\c -&gt; is c `mplus` js c)</code></pre>
<p>When the parsers on both sides of <code>mplus</code> are waiting for the next input symbol, we can group them together and make sure that the next symbol will be fetched only once from the input stream.</p>
<p>Clearly, this equation readily extends to more than two parsers, like for example</p>
<pre><code>  (symbol &gt;&gt;= is) `mplus` (symbol &gt;&gt;= js) `mplus` (symbol &gt;&gt;= ks)
=  symbol &gt;&gt;= (\c -&gt; is c `mplus` js c `mplus` ks c)</code></pre>
<p>and so on.</p>
<p>We want to use this equation as a function definition, mapping the left hand side to the right hand side. Of course, we can’t do so directly because the left hand side is not one of the four patterns we can match upon. But thanks to the <code>MonadPlus</code> laws, what we can do is to rewrite any parser into this form, namely with a function</p>
<pre><code>expand :: Parser a -&gt; [Parser a]
expand (MPlus p q `Then` is) = expand (p &gt;&gt;= is) ++
                               expand (q &gt;&gt;= is)
expand (MZero     `Then` is) = []
expand x                     = [x]</code></pre>
<p>The idea is that <code>expand</code> fulfills</p>
<pre><code>foldr mplus mzero . expand = id</code></pre>
<p>and thus turns a parser into a list of summands which we now can pattern match upon. In other words, this function expands parsers matching <code>mzero &gt;&gt;= is</code> and <code>mplus p q &gt;&gt;= is</code> until only summands of the form <code>symbol &gt;&gt;= is</code> and <code>return a</code> remain.</p>
<p>With the parser expressed as a big “sum”, we can now apply our key idea and group all summands of the form <code>symbol &gt;&gt;= is</code>; and we also have to take care of the other summands of the form <code>return a</code>. The following definition will do the right thing:</p>
<pre><code>interpret :: Parser a -&gt; String -&gt; [a]
interpret p = interpret&#39; (expand p)
    where
    interpret&#39; :: [Parser a] -&gt; String -&gt; [a]
    interpret&#39; ps []     = [a | Return a &lt;- ps]
    interpret&#39; ps (c:cs) = interpret&#39;
        [p | (Symbol `Then` is) &lt;- ps, p &lt;- expand (is c)] cs</code></pre>
<p>Namely, how to handle each of the summands depends on the input stream:</p>
<ul>
<li>If there are still input symbols to be consumed, then only the summands of the form <code>symbol &gt;&gt;= is</code> will proceed, the other parsers have ended prematurely.</li>
<li>If the input stream is empty, then only the parsers of the form <code>return x</code> have parsed the input correctly, and their results are to be returned.</li>
</ul>
<p>That’s it, this is our breadth-first interpreter, obtained by using laws and equations to rewrite instruction lists. It is equivalent to <a href="http://www.cs.chalmers.se/~koen/pubs/entry-jfp04-parser.html" title="Koen Claessen. Parallel Parsing Processes">Koen Claessen’s implementation</a>.</p>
<p>As an amusing last remark, I would like to mention that our calculations can be visualized as high school algebra if we ignore that <code>(&gt;&gt;=)</code> has to pass around variables, as shown in the following table:</p>
<table>
<thead>
<tr class="header">
<th align="left">Term</th>
<th align="left">Mathematical operation</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left"><code>return</code></td>
<td align="left"><span class="math">1</span></td>
</tr>
<tr class="even">
<td align="left"><code>(&gt;&gt;=)</code></td>
<td align="left"><span class="math"> × </span> multiplication</td>
</tr>
<tr class="odd">
<td align="left"><code>mzero</code></td>
<td align="left"><span class="math">0</span></td>
</tr>
<tr class="even">
<td align="left"><code>mplus</code></td>
<td align="left"><span class="math"> + </span> addition</td>
</tr>
<tr class="odd">
<td align="left"><code>symbol</code></td>
<td align="left"><span class="math"><em>x</em></span> indeterminate</td>
</tr>
</tbody>
</table>
<p>For example, our key idea corresponds to the distributive law</p>
<p><span class="math"><em>x</em> × <em>a</em> + <em>x</em> × <em>b</em> = <em>x</em> × (<em>a</em> + <em>b</em>)</span></p>
<p>and the monad and <code>MonadPlus</code> laws have well-known counterparts in algebra as well.</p>
<h1 id="conclusion"><a href="#TOC">Conclusion</a></h1>
<h2 id="further-examples"><a href="#TOC">Further Examples</a></h2>
<p>I hope I have managed to convincingly demonstrate the virtues of the operational viewpoint with my choice of examples.</p>
<p>There are many other advanced monads whose implementations also become clearer when approached this way, such as the <a href="http://www.haskell.org/haskellwiki/ListT_done_right">list monad transformer</a> (where the naive <code>m [a]</code> is known not to work), Oleg Kiselyov’s <a href="http://okmij.org/ftp/papers/LogicT.pdf"><code>LogicT</code></a>, Koen Claessen’s <a href="http://www.cs.chalmers.se/~koen/pubs/entry-jfp99-monad.html">poor man’s concurrency monad</a>, as well coroutines like Peter Thiemann’s ingenious <a href="http://www.informatik.uni-freiburg.de/~thiemann/WASH/draft.pdf">WASH</a> which includes a monad for tracking session state in a web server.</p>
<p>The <a href="http://hackage.haskell.org/package/operational" title="Heinrich Apfelmus&#39; operational package"><code>operational</code> package</a> includes a few of these examples.</p>
<h2 id="connection-with-the-continuation-monad"><a href="#TOC">Connection with the Continuation Monad</a></h2>
<p>Traditionally, the <em>continuation monad transformer</em></p>
<pre><code>data Cont m a = Cont { runCont :: forall b. (a -&gt; m b) -&gt; m b }</code></pre>
<p>has been used to implement these advanced monads. This is no accident; both approaches are capable of implementing any monad. In fact, they are almost the same thing: the continuation monad is the <a href="http://www.brics.dk/RS/07/7/BRICS-RS-07-7.pdf" title="Oliver Danvy, Kevin Millikin. Refunctionalization at work.">refunctionalization</a> of instructions as functions</p>
<pre><code>\k -&gt; interpret (Instruction `Then` k)</code></pre>
<p>But alas, I think that this unfortunate melange of instruction, interpreter and continuation does not explain or clarify what is going on; it is the algebraic data type <code>Program</code> that offers a clear notion of what a monad is and what it means to implement one. Hence, in my opinion, the algebraic data type should be the preferred way of presenting new monads and also of implementing them, at least before program optimizations.</p>
<p>Actually, <code>Program</code> is not a plain algebraic data type, it is a <em>generalized</em> algebraic data type. It seems to me that this is also the reason why the continuation monad has found more use, despite being conceptually more difficult: GADTs simply weren’t available in Haskell. I believe that the <code>Program</code> type is a strong argument to include GADTs into a future Haskell standard.</p>
<h2 id="drawbacks"><a href="#TOC">Drawbacks</a></h2>
<p>Compared to specialized implementations, like for example <code>s -&gt; (a,s)</code> for the state monad, the operational approach is not entirely without drawbacks.</p>
<p>First, the given implementation of <code>(&gt;&gt;=)</code> has the same quadratic running time problem as <code>(++)</code> when used in a left-associative fashion. Fortunately, this can be ameliorated with a different (fancy) list data type; the <a href="http://hackage.haskell.org/package/operational" title="Heinrich Apfelmus&#39; operational package"><code>operational</code> library</a> implements one.</p>
<p>Second, and this cannot be ameliorated, we lose laziness. The state monad represented as <code>s -&gt; (a,s)</code> can cope with some infinite programs like</p>
<pre><code>evalState (sequence . repeat . state $ \s -&gt; (s,s+1)) 0</code></pre>
<p>whereas the list of instructions approach has no hope of ever handling that, since only the very last <code>Return</code> instruction can return values.</p>
<p>I also think that this loss of laziness also makes <a href="http://citeseer.ist.psu.edu/590305" title="Levent Erk�k. Value Recursion in Monadic Computations.">value recursion</a> a la <code>MonadFix</code> very difficult.</p>
<h1 id="about-the-author"><a href="#TOC">About the author</a></h1>
<p>After some initial programming experience in Pascal, Heinrich Apfelmus picked up Haskell and purely functional programming just at the dawn of the new millenium. He has never looked back ever since, for he not only admires Haskell’s mathematical elegance, but also its practicality in personal life. For instance, he was always too lazy to tie knots, but that has changed and he now accepts shoe laces instead of velcro.</p>
<hr /><p><a href="https://flattr.com/thing/29608/apfelmus-website"><img src="http://api.flattr.com/button/flattr-badge-large.png" alt="Flattr this" title="Flattr this" border="0" /></a></p>
]]></description></item><item>
<title>Fun with Morse Code</title>
<link>http://apfelmus.nfshost.com/articles/fun-with-morse-code.html</link>
<pubDate>Tue, 01 Sep 2009 17:33:00 +0200</pubDate>
<description><![CDATA[
<ul>
<li><a href="#introduction">Introduction</a></li>
<li><a href="#a-simple-first-solution">A simple first solution</a></li>
<li><a href="#dichotomic-search">Dichotomic search</a></li>
<li><a href="#reverse-polish-notation">Reverse Polish Notation</a></li>
<li><a href="#deforestation">Deforestation</a><ul>
<li><a href="#chopping-trees">Chopping trees</a><ul>
<li><a href="#call-graph">Call graph</a></li>
<li><a href="#a-combinator">A combinator…</a></li>
<li><a href="#revealed">…revealed!</a></li>
</ul></li>
<li><a href="#a-chainsaw-called-catamorphism">A chainsaw called ‘catamorphism’</a><ul>
<li><a href="#derivation">Derivation</a></li>
<li><a href="#general-pattern">General pattern</a></li>
</ul></li>
</ul></li>
<li><a href="#where-to-go-from-here">Where to go from here</a><ul>
<li><a href="#cutting-words">Cutting words</a></li>
<li><a href="#polish-notation-and-parsing">Polish notation and parsing</a></li>
<li><a href="#comparing-to-c">Comparing to C</a></li>
</ul></li>
</ul>
<p><em>This article previously appeared in <a href="http://themonadreader.wordpress.com/2009/07/29/issue-14/">issue 14</a> of <a href="http://themonadreader.wordpress.com/">The Monad.Reader</a>.</em></p>
<h1 id="introduction"><a href="#TOC">Introduction</a></h1>
<p>In a <a href="http://www.reddit.com/r/programming/comments/7xjqb/who_can_write_the_smallesttidiestcleverest_morse/" title="Reddit user CharlieDancey asking for a clever morse code translator">post</a> to <a href="http://www.reddit.com/r/programming" title="Programming reddit">reddit</a>, user CharlieDancey presented a challenge to write a short and clever morse code decoder. Of course, what could be more clever than writing it in Haskell? ;-)</p>
<p>In the following, I’ll present a series of solutions that gradually include <a href="http://en.wikipedia.org/wiki/Dichotomic_search" title="Wikipedia on Dichotomic Search">dichotomic search</a> (the straightforward generalization of <a href="http://en.wikipedia.org/wiki/Binary_search">binary search</a>), <a href="http://en.wikipedia.org/wiki/Stack-based" title="Wikipedia on Stack-based Languagues">stack-based languages</a> or <a href="http://en.wikipedia.org/wiki/Reverse_Polish_notation" title="Wikipedia on Reverse Polish Notation">reverse polish notation</a>, and finally <a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.51.646" title="Andrew Gill, John Launchbury, Simon Peyton Jones. A short cut to deforestation. 1993">deforestation</a> (also called <a href="http://donsbot.wordpress.com/tag/fusion/" title="Don Stewart on Fusion">fusion</a>), an optimization technique specific to purely functional languages like Haskell.</p>
<p>For your convenience, source code for the individual sections is <a href="http://apfelmus.nfshost.com/articles/fun-with-morse-code/code.zip" title="Accompanying source code for &#39;Fun with Morse Code&#39;">available</a>.</p>
<h1 id="a-simple-first-solution"><a href="#TOC">A simple first solution</a></h1>
<p><a href="http://en.wikipedia.org/wiki/Morse_Code" title="Wikipedia on Morse Code">Morse code</a> was designed to be produced and read, or rather heard by humans, who have to memorize the following table:</p>
<p><a href="http://en.wikipedia.org/wiki/File:International_Morse_Code.svg"><img src="http://apfelmus.nfshost.com/articles/fun-with-morse-code/morse-table.png" alt="International Morse Code" width="400"/></a></p>
<p>To write a program that decodes sequences of dots and dashes like</p>
<pre><code>-- --- .-. ... .    -.-. --- -.. .</code></pre>
<p>into letters, we will have to store this table in some form. For instance, we could store each letter and corresponding morse code in a big <a href="http://en.wikipedia.org/wiki/Association_list" title="Wikipedia on Association List">association list</a></p>
<pre><code>type MorseCode = String

dict :: [(MorseCode, Char)]
dict =
    [(&quot;.-&quot;   , &#39;A&#39;),
     (&quot;-...&quot; , &#39;B&#39;),
     (&quot;-.-.&quot; , &#39;C&#39;),
     ...</code></pre>
<p>which we name <code>dict</code> because it’s a dictionary translating between the latin alphabet and morse code. To decode a letter, we simply browse through this list to determine whether it contains a given code of dots and dashes</p>
<pre><code>decodeLetter :: MorseCode -&gt; Char
decodeLetter code = maybe &#39; &#39; id $ lookup code dict</code></pre>
<p>Decoding a whole sequence of letters is done with</p>
<pre><code>decode = map decodeLetter . words</code></pre>
<p>so that we have for example</p>
<pre><code>&gt; decode &quot;-- --- .-. ...    . -.-. --- -.. .&quot;
&quot;MORSECODE&quot;</code></pre>
<p>Of course, association lists are a rather slow data structure. A <a href="http://en.wikipedia.org/wiki/Binary_search_tree" title="Wikipedia on Binary Search Tree">binary search tree</a>, <a href="http://en.wikipedia.org/wiki/Trie" title="Wikipedia on Trie">trie</a>, or <a href="http://en.wikipedia.org/wiki/Hash_table" title="Wikipedia on Hash Table">hash table</a> would be a better choice. But fortunately, it is very easy to change data structures in Haskell. All you have to do is to a qualified import of a different module, such as <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/containers/Data-Map.html" title="The &#39;Data.Map&#39; module">Data.Map</a>, and change the definition of <code>dict</code> to</p>
<pre><code>dict :: Data.Map.Map MorseCode Char
dict = Data.Map.fromList $
    [(&quot;.-&quot;   , &#39;A&#39;),
     (&quot;-...&quot; , &#39;B&#39;),
     (&quot;-.-.&quot; , &#39;C&#39;),
     ...</code></pre>
<p>and use <code>Data.Map.lookup</code> instead of <code>Data.List.lookup</code>.</p>
<h1 id="dichotomic-search"><a href="#TOC">Dichotomic search</a></h1>
<p>Faithfully replicating the morse code table is clear and preferable, but makes for quite large source code. So, let’s make an exception today and think of something <a href="http://en.wikiquote.org/wiki/Brian_Kernighan" title="Quotes by Brian Kernighan">as clever as we can</a>.</p>
<p>The idea is the following: whenever an encoded letter starts with a dash <code>'-'</code>, we know that it can’t be for example an A or E, because those start with a dot <code>'.'</code>. And if the next symbol is a dot, then it can’t be a G or M because their second symbol is a dash. With each symbol we read, more and more alternatives disappear until we are left with a single alternative. We can depict this with a binary tree:</p>
<p><a href="http://en.wikipedia.org/wiki/File:Morse_code_tree3.png"><img src="http://apfelmus.nfshost.com/articles/fun-with-morse-code/morse-tree.png" alt="Tree for deciphering morse code" width="550"/></a></p>
<p>At each node, the left subtree contains all the letters that can be obtained by adding a dot to the current letter, while the right subtree contains those letters that can be obtained with a dash. This is illustrated by means of a dotted line connected to the left subtree and a dashed line connected to the right subtree.</p>
<p>Now, to decode a letter, we simply start at the root of the tree and follow the dotted or dashed lines depending on the symbols read. For instance, to decode the sequence <code>&quot;-...&quot;</code>, we have to go right once and then left thrice, ending up at the letter B. This procedure is also called <a href="http://en.wikipedia.org/wiki/Dichotomic_search" title="Wikipedia on Dichotomic Search">dichotomic search</a> because at each point in our search for the right letter, we ask a yes/no question “Dot or dash?” (a <a href="http://en.wikipedia.org/wiki/Dichotomy" title="Wikipedia on Dichotomy">dichotomy</a>) that partitions the remaining search space into to disjoint parts.</p>
<p>Let’s implement this in Haskell. First, we assume that our dictionary is given as a tree</p>
<pre><code>data Tree a = Leaf
            | Branch { tag   :: a
                     , left  :: Tree a
                     , right :: Tree a }

dict :: Tree Char
dict = Branch &#39; &#39; (Branch &#39;E&#39; ... ) (Branch &#39;T&#39; ...)</code></pre>
<p>Of course, writing out the <code>dict</code> tree in full is going to be repetitive and boring, but let’s just imagine we have already done that. Then, decoding a letter by walking down the tree is simply a <a href="http://en.wikipedia.org/wiki/Fold_(higher-order_function)" title="Wikipedia on Fold">left fold</a> over the code word</p>
<pre><code>decodeLetter = tag . foldl (flip step) dict
    where
    step &#39;.&#39; = left
    step &#39;-&#39; = right</code></pre>
<p>In this case, the accumulated value of the fold is the current subtree, although the term “accumulate” is a bit misleading in that we don’t make the dictionary bigger; we make it smaller by passing to a subtree.</p>
<p>The curious reader may notice that we have actually implemented a <a href="http://en.wikipedia.org/wiki/Trie" title="Wikipedia on Trie">trie</a>.</p>
<h1 id="reverse-polish-notation"><a href="#TOC">Reverse Polish Notation</a></h1>
<p>Now, let’s reduce the source code needed for the dictionary tree. In Haskell, we’d have to write something like</p>
<pre><code>dict = Branch &#39; &#39; (Branch &#39;E&#39; ... ) (Branch &#39;T&#39; ...)</code></pre>
<p>Compared to the association list, we got rid of the dots <code>'.'</code> and dashes <code>'-'</code>, they have been made implicit in the structure of our tree. But we still need a lot of parenthesis and applications of the <code>Branch</code> function.</p>
<p>A clever way to get rid of parenthesis is <a href="http://en.wikipedia.org/wiki/Reverse_Polish_notation" title="Wikipedia on Reverse Polish Notation">reverse polish notation</a>, commonly used in <a href="http://en.wikipedia.org/wiki/Stack-based" title="Wikipedia on Stack-based Languagues">stack-based languages</a>. The idea is that a program in such a language is a sequence of instructions that pop and push values from a stack. For example,</p>
<pre><code>1 2 +</code></pre>
<p>is a program to calculate 1 plus 2. Reading from left to right, the first instruction is <code>1</code> which pushes the integer 1 onto the stack. The second instruction is <code>2</code>, pushing 2 onto the stack. And finally, the instruction <code>+</code> pops the two topmost integers from the stack and pushes their sum onto the stack. This procedure is visualized as follows:</p>
<img src="http://apfelmus.nfshost.com/articles/fun-with-morse-code/stack.png" alt="Execution of the stack-based program  1 2 +  " width="350"/>
<p>To see how that eliminates the need for parenthesis, take a look the program</p>
<pre><code>1 2 + 3 4 + +</code></pre>
<p>which calculates the sum (1+2)+(3+4).</p>
<p>To build our dictionary tree, we are going to devise a very similar language. Instead of integers, the stack will store whole trees. In analogy to the numerals, there will be an instruction for pushing a <code>Leaf</code> onto the stack; and in analogy to <code>+</code>, there is going to be an instruction for applying the <code>Branch</code> constructor to the two topmost subtrees.</p>
<p>Here’s the program for building the tree, stored as a string:</p>
<pre><code>program :: String
program = &quot;__5__4H___3VS__F___2 UI__L__+_ R__P___1JWAE&quot;
       ++ &quot;__6__=B__/_XD__C__YKN__7_Z__QG__8_ __9__0 OMT &quot;</code></pre>
<p>Each character represents one instruction. The underscore <code>'_'</code> pushes a <code>Leaf</code> onto the stack while all the other character push a <code>Branch</code> tagged with the character in question onto the stack. The interpreter for this tiny programming language can be written using a simple <a href="http://en.wikipedia.org/wiki/Fold_(higher-order_function)" title="Wikipedia on Fold">left fold</a>:</p>
<pre><code>dict = interpret program
where
    interpret         = head . foldl exec []
    exec xs       &#39;_&#39; = Leaf         : xs    -- push Leaf
    exec (x:y:xs)  c  = Branch c y x : xs    -- combine subtrees</code></pre>
<p>The stack <code>xs</code> is represented as a list of trees.</p>
<h1 id="deforestation"><a href="#TOC">Deforestation</a></h1>
<p>We have found a concise way to represent the morse code dictionary in source code, but cleverness does not end here. For instance, how about eliminating the tree entirely?</p>
<h2 id="chopping-trees"><a href="#TOC">Chopping trees</a></h2>
<h3 id="call-graph"><a href="#TOC">Call graph</a></h3>
<p>More precisely, it seems wasteful to construct the dictionary tree by creating leaves and connecting branches only to deconstruct them again when decoding letters. Wouldn’t it be more efficient to interpret the <a href="#dichotomic-search">morse code tree</a> as a <em>call graph</em> rather than as a data structure <code>Tree Char</code>?</p>
<p>In other words, imagine say a function <code>e</code> corresponding to the node labeled ‘E’ in the tree, and working as follows:</p>
<pre><code>e :: MorseCode -&gt; Char
e (&#39;.&#39;:ds) = i ds
e (&#39;-&#39;:ds) = a ds
e []       = &#39;E&#39;</code></pre>
<p>If the next symbol is a dot or dash, we proceed with the functions <code>i</code> or <code>a</code> corresponding to <code>'I'</code> and <code>'A'</code> respectively. And in case we have already reached the end of the code, we know that we have decoded an <code>'E'</code>. The functions <code>i</code> and <code>a</code> work just like <code>e</code>, except that they proceed with other letters; and the morse code tree becomes their <em>call graph</em>.</p>
<h3 id="a-combinator"><a href="#TOC">A combinator…</a></h3>
<p>Now, writing all these functions by hand would be most error-prone and tedious, they all look the same. But abstracting repetitive patterns is exactly where functional programming shines; we are to devise a combinator that automates the boring parts of the code for us.</p>
<p>What does this combinator look like? Well, the non-boring parts are the letter it represents and the two follow-up functions, so these will be parameters:</p>
<pre><code>combinator :: Char                   -- letter
           -&gt; (MorseCode -&gt; Char)    -- function for dot
           -&gt; (MorseCode -&gt; Char)    -- function for dash
           -&gt; (MorseCode -&gt; Char)    -- result function</code></pre>
<p>That’s all we need; we can implement</p>
<pre><code>combinator c x y = \code -&gt; case code of
    &#39;.&#39;:ds -&gt; x ds
    &#39;-&#39;:ds -&gt; y ds
    []     -&gt; c</code></pre>
<p>which allows us to write</p>
<pre><code>e = combinator &#39;E&#39; i a
i = combinator &#39;I&#39; s u
... etc ...</code></pre>
<h3 id="revealed"><a href="#TOC">…revealed!</a></h3>
<p>But wait, what have we done? The type signature of <code>combinator</code> looks exactly like that of</p>
<pre><code>Branch :: Char         -- letter
       -&gt; Tree Char    -- tree for dot
       -&gt; Tree Char    -- tree for dash
       -&gt; Tree Char    -- result tree</code></pre>
<p>but with the type <code>Tree Char</code> replaced by <code>(MorseCode -&gt; Char)</code>! In fact, let’s rename the combinator to lowercase <code>branch</code></p>
<pre><code>branch c x y = \code -&gt; case code of
    &#39;.&#39;:ds -&gt; x ds
    &#39;-&#39;:ds -&gt; y ds
    []     -&gt; c</code></pre>
<p>and observe that the tree of functions now reads</p>
<pre><code>e = branch &#39;E&#39; i a
i = branch &#39;I&#39; s u
... etc ...</code></pre>
<p>or</p>
<pre><code>e = branch &#39;E&#39; (branch &#39;I&#39; ... ) (branch &#39;A&#39; ...)</code></pre>
<p>if we inline the function definitions. But this is of course just the tree from the <a href="#dichotomic-search">section</a> on dichotomic search with each <code>Branch</code> replaced by <code>branch</code>!</p>
<p>In other words, <code>branch</code> is like a drop-in replacement for the constructor <code>Branch</code>. To implement the morse code tree directly as functions instead of as an algebraic data type, we simply replace every occurrence of <code>Branch</code> with <code>branch</code> in our previous code. Thus, we have a new implementation</p>
<pre><code>dict :: MorseCode -&gt; Char
dict = interpret program
    where
    interpret         = head . foldl exec []
    exec xs       &#39;_&#39; = leaf         : xs    -- push leaf
    exec (x:y:xs)  c  = branch c y x : xs    -- combine subtrees

decodeLetter = dict</code></pre>
<p>where</p>
<pre><code>leaf = undefined</code></pre>
<p>is a trivial replacement for the <code>Leaf</code> constructor.</p>
<h2 id="a-chainsaw-called-catamorphism"><a href="#TOC">A chainsaw called ‘catamorphism’</a></h2>
<h3 id="derivation"><a href="#TOC">Derivation</a></h3>
<p>To further understand how and why the replacement of <code>Branch</code> by <code>branch</code> works, it is instructive to derive it systematically from just the program text.</p>
<p>In particular, consider the implementation of <code>decodeLetter</code> from the <a href="#dichotomic-search">earlier section</a> on dichotomic search:</p>
<pre><code>decodeLetter :: MorseCode -&gt; Char
decodeLetter = tag . foldl (flip step) dict
    where
    step &#39;.&#39; = left
    step &#39;-&#39; = right</code></pre>
<p>In this formulation, the focus is on the recursion over the input string performed by <code>foldl</code>, neglecting the recursive descent into the dictionary tree. Let’s systematically rewrite this code to highlight the latter.</p>
<p>First, we interpret it as converting the dictionary tree into a function that decodes letters</p>
<pre><code>decodeLetter = decodeWith dict

decodeWith :: Tree Char -&gt; (MorseCode -&gt; Char)
decodeWith dict = tag . foldl (flip step) dict
    where
    step &#39;.&#39; = left
    step &#39;-&#39; = right</code></pre>
<p>Then, we turn the <code>foldl</code> into explicit recursion</p>
<pre><code>decodeWith dict []     = tag dict
decodeWith dict (d:ds) = decodeWith (step d dict) ds
    where
    step &#39;.&#39; = left
    step &#39;-&#39; = right</code></pre>
<p>and dissolve the <code>step</code> function into the pattern matching</p>
<pre><code>decodeWith dict []       = tag dict
decodeWith dict (&#39;.&#39;:ds) = decodeWith (left  dict) ds
decodeWith dict (&#39;-&#39;:ds) = decodeWith (right dict) ds</code></pre>
<p>We group the pattern matching on the input code into a case expression</p>
<pre><code>decodeWith dict = \code -&gt; case code of
    []       -&gt; tag dict
    (&#39;.&#39;:ds) -&gt; decodeWith (left  dict) ds
    (&#39;-&#39;:ds) -&gt; decodeWith (right dict) ds</code></pre>
<p>and finally, we replace the field selectors by pattern matching on the tree, also noting the case of <code>Leaf</code>:</p>
<pre><code>decodeWith Leaf           = undefined
decodeWith (Branch c x y) = \code -&gt; case code of
    []       -&gt; c
    (&#39;.&#39;:ds) -&gt; (decodeWith x) ds
    (&#39;-&#39;:ds) -&gt; (decodeWith y) ds</code></pre>
<p>The recursion on the tree is apparent now, <code>decodeWith</code> simply traverses both subtrees and combines the results. We can make this even more evident by appealing to the combinator we defined in the previous section:</p>
<pre><code>decodeWith Leaf           = leaf
decodeWith (Branch c x y) = branch c (decodeWith x) (decodeWith y)</code></pre>
<p>In other words, <code>decodeWith</code> takes a tree and simply substitutes each <code>Leaf</code> constructor with <code>leaf</code> and each <code>Branch</code> constructor with <code>branch</code>.</p>
<img src="http://apfelmus.nfshost.com/articles/fun-with-morse-code/decodeWith.png" alt="Substituting constructors  Branch  with functions  branch" width="500"/>
<p>Of course, first using <code>Branch</code> and then replacing it with <code>branch</code> is a waste; we should rather use <code>branch</code> from the start and thus cut away the intermediate tree. That’s exactly what we’ve done in the <a href="#revealed">previous section</a>!</p>
<h3 id="general-pattern"><a href="#TOC">General pattern</a></h3>
<p>Replacing constructors is a very general pattern, not restricted to binary trees. For instance, you probably know following function:</p>
<pre><code>foldr f z []     = z
foldr f z (x:xs) = x `f` foldr f z xs</code></pre>
<p>It’s the good old <a href="http://en.wikipedia.org/wiki/Fold_(higher-order_function)" title="Wikipedia on Fold">fold</a>! And at its heart, it just replaces the constructors of the list data type: <code>z</code> is the substitute for the empty list <code>[]</code>, and <code>f</code> is put in lieu of the <code>(:)</code> constructor.</p>
<p>In its honor, any function that substitutes constructors in such fashion is known as a <a href="http://en.wikipedia.org/wiki/Catamorphism" title="Wikipedia on Catamorphism">generalized fold</a>. A more exotic but commonly used alternative name is “<a href="http://en.wikipedia.org/wiki/Catamorphism" title="Wikipedia on Catamorphism">catamorphism</a>”. In other words, <code>decodeWith</code> is a catamorphism.</p>
<p>In this generality, the idea of <a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.51.646" title="Andrew Gill, John Launchbury, Simon Peyton Jones. A short cut to deforestation. 1993">deforestation</a> is that instead of first constructing a data structure and then chopping it up with a catamorphism, it is more efficient to saw the pieces properly at the time of creation. For example, creating a list and then adding all elements</p>
<pre><code>sum [1..100] = foldr (+) 0 (enumFromTo 1 100)</code></pre>
<p>is less efficient than adding the elements as they are created</p>
<pre><code>sumFromTo a b                            -- changes to enumFromTo
    | a &gt; b     = 0                      -- 0 replaces []
    | otherwise = a + sumFromTo (a+1) b  -- + replaces (:)</code></pre>
<p>Merging two functions in this fashion is also called <a href="http://donsbot.wordpress.com/tag/fusion/" title="Don Stewart on Fusion">fusion</a>.</p>
<p>Performing deforestation manually, as we did, sacrifices reusability: the morse code tree only exists as a call graph and cannot be printed out anymore, and <code>sumFromTo</code> is not nearly as useful as are <code>sum</code> and <code>enumFromTo</code> were.</p>
<p>Hence, the goal is to teach the compiler to <em>automatically</em> fuse catamorphisms with their structure creating counterparts, the so called <a href="http://en.wikipedia.org/wiki/Anamorphism" title="Wikipedia on Anamorphism">anamorphisms</a>. That’s what efforts like <a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.51.646" title="Andrew Gill, John Launchbury, Simon Peyton Jones. A short cut to deforestation. 1993">a short-cut to deforestation</a> and the recent <a href="http://www.cse.unsw.edu.au/~dons/papers/CLS07.html" title="Duncan Coutts, Roman Leshchinskiy, Don Stewart. Stream Fusion, From Lists to Streams to Nothing at All. 2007">stream fusion</a> are set out to do, yielding dramatic gains in efficiency while preserving the compositional style.</p>
<h1 id="where-to-go-from-here"><a href="#TOC">Where to go from here</a></h1>
<p>I hope you had fun doodling around with morse code; I certainly did. If you long for more, here a few suggestions:</p>
<h2 id="cutting-words"><a href="#TOC">Cutting words</a></h2>
<p>While we’re at it, the intermediate lists created by <code>words</code> in</p>
<pre><code>decode = map decodeLetter . words</code></pre>
<p>can be deforested as well and fused into the letter decoding. Try it yourself, or see the <a href="http://apfelmus.nfshost.com/articles/fun-with-morse-code/code.zip" title="Accompanying source code for &#39;Fun with Morse Code&#39;">example source code</a>.</p>
<h2 id="polish-notation-and-parsing"><a href="#TOC">Polish notation and parsing</a></h2>
<p><a href="http://en.wikipedia.org/wiki/Polish_notation" title="Wikipedia on Polish Notation">Polish notation</a>, the converse of reverse polish notation, puts function symbols before their arguments and is thus closer to the Haskell syntax. When the arities of the functions are known in advance, like 2 for <code>Branch</code> and 0 for <code>Leaf</code>, this notation doesn’t require parenthesis either. Write a <code>program</code> in polish notation and a corresponding tiny interpreter to create the morse code tree.</p>
<h2 id="comparing-to-c"><a href="#TOC">Comparing to C</a></h2>
<p>Since deforestation is supposed to make things faster, how about comparing our deforested morse code tree to say an implementation in C? But instead of doing benchmarks, let me give two general remarks on the machine representation.</p>
<p>First, one might think that the call graph we’ve built is compiled to function calls, with <code>e</code> calling <code>i</code> or <code>a</code> and each function having different machine code, just as we originally intended. But this is actually not the case. When defined with <code>branch</code>, the functions are represented as <a href="http://en.wikipedia.org/wiki/Closure_(computer_science)" title="Wikipedia on Closure">closures</a>, sharing the same machine code but carrying different records that store their free variables <code>c</code>, <code>x</code> and <code>y</code>. This is not very different from storing <code>c</code>, <code>x</code> and <code>y</code> in a <code>Branch</code> constructor. <a href="http://www.haskell.org/haskellwiki/Template_Haskell" title="Template Haskell">Template Haskell</a> or some kind of <a href="http://en.wikipedia.org/wiki/Partial_evaluation" title="Wikipedia on Partial Evaluation">partial evaluation</a> would be needed to hard-code the free variables into the executable. For more on the Haskell execution model, take a look at the <a href="http://hackage.haskell.org/trac/ghc/wiki/Commentary/Rts/HaskellExecution" title="GHC commentary on the Haskell Execution model">GHC commentary</a>.</p>
<p>Second, all our implementations decipher a letter by follow a chain of pointers: descending a tree means following pointers to deeper nodes, and making procedure calls means repeatedly jumping to different code parts. This of course raises questions of <a href="http://en.wikipedia.org/wiki/Locality_of_reference" title="Wikipedia on CPU Cache Locality">cache locality</a>, <a href="http://en.wikipedia.org/wiki/Branch_predictor" title="Wikipedia on Branch Prediction">branch prediction</a> or memory requirements for storing all these pointers.</p>
<p>In C, however, there is another technique available: instead of following a chain of pointers to get to the destination, the address of the final stop is calculated directly with pointer arithmetic. The following example illustrates this:</p>
<pre><code>#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
char buf[99], tree[] = &quot; ETIANMSURWDKGOHVF L PJBXCYZQ  &quot;;
int main() {
    while (scanf(&quot;%s&quot;, buf)) {
        int n, t=0;
        for(n=0; buf[n]!=0; n++)
            t = 2*t + 1 + (buf[n]&amp;1); /* compute destination */
        putchar(tree[t]);             /* fetch letter */
    }
}</code></pre>
<p>Here, the tree is represented as an array and paths to nodes are encoded as (zero-less) binary numbers. The program calculates the path <code>t</code> to the proper letter and then fetches it with an O(1) memory lookup. Compare also the <a href="http://www.reddit.com/r/programming/comments/7xjqb/who_can_write_the_smallesttidiestcleverest_morse/c07p22i" title="Solution in C by reddit user kayamon">morse code translator by reddit user kayamon</a>.</p>
<p>Since the number of memory accesses is kept to a minimum, this technique is the most efficient; but it is impossible to reproduce with algebraic data types alone. Fortunately, <a href="http://www.haskell.org/haskellwiki/Arrays" title="Array libraries in Haskell">arrays libraries</a> are readily available in Haskell as well.</p>
<p>The main drawback of this approach, and of arrays in general, is the lack of clarity; indices are notorious for being non-descript and messy. The raw index arithmetic in the example C code above sure seems like magic!</p>
<p>Such magic is best hidden behind a descriptive abstract data type. How about rewriting the example in Haskell such that <code>decodeLetter</code> looks exactly like the one in the <a href="#dichotomic-search">section on dichotomic search</a>? In other words, the abstract data type is to support the functions <code>left</code>, <code>right</code> and <code>tag</code>. One possible solution can be found in the <a href="http://apfelmus.nfshost.com/articles/fun-with-morse-code/code.zip" title="Accompanying source code for &#39;Fun with Morse Code&#39;">source code accompanying this article</a>.</p>
<hr /><p><a href="https://flattr.com/thing/29608/apfelmus-website"><img src="http://api.flattr.com/button/flattr-badge-large.png" alt="Flattr this" title="Flattr this" border="0" /></a></p>
]]></description></item><item>
<title>FRP - Demonstration of reactive-banana in the web browser with Ji</title>
<link>http://apfelmus.nfshost.com/blog/2012/10/31-frp-ji.html</link>
<pubDate>Wed, 31 Oct 2012 16:23:24 +0100</pubDate>
<description><![CDATA[
<p>My primary motivation for creating an FRP library was to write GUI programs. With the release of <a href="http://www.haskell.org/haskellwiki/Reactive-banana">reactive-banana</a> version 0.7, I consider FRP to be a mostly solved problem now, at least to the point where playing around with FRP in isolation does not yield significant insights anymore. It is time to return to my original motivation and focus on the interactions between real GUI programs and FRP. I’m sure that they are <a href="http://apfelmus.nfshost.com/blog/2012/03/29-frp-three-principles-bidirectional-gui.html">plenty of design patterns</a> waiting on the road towards the ultimate goal: writing GUI applications with just one line of source code!</p>
<p>Unfortunately, it appears that Haskell GUI libraries are mostly dormant these days. Both <a href="http://projects.haskell.org/gtk2hs/">Gtk2Hs</a> and <a href="http://www.haskell.org/haskellwiki/WxHaskell">wxHaskell</a> are tricky to install and haven’t seen any recent work on their API. I prefer not to bet on a horse that lies in the grass, regardless of whether it is just sleeping or whether it will never wake up again.</p>
<p>But there is one GUI framework that people are throwing serious efforts at: the web browser! Projects like <a href="https://github.com/valderman/haste-compiler">haste</a>, <a href="http://www.cs.uu.nl/wiki/UHC">uhc</a> and <a href="https://github.com/ghcjs/ghcjs">ghcjs</a> try to compile Haskell to JavaScript, alternative approaches to Haskell in the web browser are investigated with <a href="http://fay-lang.org/">fay</a> and <a href="https://github.com/chrisdone/ji">Ji</a>, and Haskell-like language like <a href="http://elm-lang.org/">elm</a> and <a href="http://roy.brianmckenna.org/">roy</a> are showing up.</p>
<p>It looks like these efforts are not ready for prime-time just yet, but I feel compelled to do my part and add fuel to their momentum. Thus, I have whipped up a small demonstration that uses reactive-banana with Ji to create a small GUI example. You can find the project on github in the folder <a href="https://github.com/HeinrichApfelmus/reactive-banana/tree/master/reactive-banana-ji">reactive-banana-ji</a>. Here a screenshot:</p>
<div class="figure">
<img src="http://apfelmus.nfshost.com/blog/2012/10/31-frp-ji/CurrencyConverter.png" /><p class="caption"></p>
</div>
<p>Typing in one input field will immediately update the other input field, i.e. data can flow in both directions. The <a href="https://github.com/HeinrichApfelmus/reactive-banana/tree/master/reactive-banana-ji/src/CurrencyConverter.hs">source code</a> is very simple, it just declares the value of each input field to be a transformation of the value of the other field. FRP takes care of the recursion and the automatic updates.</p>
<p>The example code still contains more administrative details than I’m happy with, like the <code>compile</code> function and the specialized <code>behaviorValue</code> functions. But that is because neither the DOM bindings, nor my reactive-banana glue have seen any serious API design efforts. The time is ripe for changing that!</p>
<p>It sure looks like 2013 is going to be the year of Haskell in the web browser!</p>
<div style="float:left">
<img src="https://github.com/HeinrichApfelmus/reactive-banana/raw/develop/banana.png">
</div>
<p>Howdy, cowboy, it’s advertisment time! If you like what I’m doing with reactive-banana, you can support me with a small donation: <a href="http://flattr.com/thing/384682/reactive-banana"><img src="http://api.flattr.com/button/flattr-badge-large.png" alt="Flattr this!" /></a>. (I stopped pestering people with my ¢2 worth status information at the end of every month, though, but that is perhaps for the best.)</p>
<hr /><p><a href="https://flattr.com/thing/29608/apfelmus-website"><img src="http://api.flattr.com/button/flattr-badge-large.png" alt="Flattr this" title="Flattr this" border="0" /></a></p>
]]></description></item><item>
<title>FRP - Dynamic event switching in reactive-banana-0.7</title>
<link>http://apfelmus.nfshost.com/blog/2012/09/03-frp-dynamic-event-switching-0-7.html</link>
<pubDate>Mon, 03 Sep 2012 10:48:21 +0200</pubDate>
<description><![CDATA[
<p>In this post, I am going to explain the API for dynamic event switching in my <a href="http://www.haskell.org/haskellwiki/Reactive-banana">reactive-banana library</a>, version 0.7.*.</p>
<p>In particular, I’m going to explain the new <code>Moment</code> type. Moreover, we’ll elucidate the connection to other approaches to dynamic event switching in Haskell, like the one taken by the <a href="http://hackage.haskell.org/package/sodium">sodium library</a>.</p>
<h2 id="what-is-dynamic-event-switching">What is dynamic event switching?</h2>
<p>The concept of “dynamic event switching” also goes by the name of “higher-order events”. The idea is that you can make an event that contains behaviors</p>
<pre><code>example :: Event (Behavior A)</code></pre>
<p>and use it to define a new behavior which acts like the behavior which has occurred last in the event. This is done with the <code>switchB</code> combinator, whose type and semantics ought to be</p>
<pre><code>switchB :: Behavior a -&gt; Event (Behavior a) -&gt; Behavior a
switchB b es = \time -&gt;
    last $ b : [b2 | (time2,b2) &lt;- es, time2 &lt; time]</code></pre>
<p>In other words, you “switch” to a new behavior whenever an event occurrence happens.</p>
<p>Unfortunately, for various fundamental reasons, the types above will have to be more sophisticated than indicated in the dummy examples above. The purpose of this post is to introduce and explain these more sophisticated types.</p>
<h2 id="what-is-it-useful-for">What is it useful for?</h2>
<p>Whenever you have some dynamic collection of events or behaviors, you likely want dynamic event switching.</p>
<p>For instance, imagine that you are programming GUI and want to allocate new widgets dynamically. This is what you need dynamic event switching for. The <a href="http://www.haskell.org/haskellwiki/Reactive-banana/Examples#bartab">BarTab.hs</a> example gives a demonstration. Essentially, you have a dynamic collection of text entry widgets which is used to input prices. The total price is calculated from these via the <code>switchB</code> combinator.</p>
<pre><code>eentries :: Event [Behavior Price]
eentries = accumE [] $
        (add &lt;$&gt; eClickAdd) `union` (remove &lt;$&gt; eClickRemove)
    where
    add  x xs = x:xs
    remove xs = drop 1 xs

etotal :: Event (Behavior Price)
etotal = (fmap sum . sequenceA) &lt;$&gt; eentries

btotal :: Behavior Price
btotal = switchB (pure 0) etotal</code></pre>
<p>Note, however, that there are many cases that look like they require dynamic event switching, but which actually don’t. One instance is the <a href="http://www.haskell.org/haskellwiki/Reactive-banana/Examples#asteroids">Asteroids.hs</a> example, which features a space ship and a collection of moving asteroids. You can model the asteroids as list of time-varying objects <code>[Behavior Asteroid]</code>, but it is equally possible to model them as a time-varying list of objects, <code>Behavior [Asteroid]</code>. The latter approach is entirely first-order, but can be less compositional than the former approach.</p>
<h2 id="how-does-it-work">How does it work?</h2>
<p>Now, on to the explanation of how dynamic event switching is done in reactive-banana.</p>
<h3 id="start-times">Start times</h3>
<p>The first key concept to understand is the notion of <em>start time</em>.</p>
<p>In reactive-banana, each event and behavior has a specific start time, which is indicated by the type parameter <code>t</code></p>
<pre><code>Event t a       -- event    starting at time t
Behavior t a    -- behavior starting at time t</code></pre>
<p>Now you know what this pesky type parameter <code>t</code> is used for. Of course, you will never plug a concrete time, like <code>5 o'clock</code>, into the types. Rather, the type parameter is just a dummy that enforces some typing discipline, but I strongly recommend to think of it as an actual clock time.</p>
<h3 id="the-moment-monad">The Moment monad</h3>
<p>The second key concept to understand is a new abstract type, namely the <em><code>Moment</code></em> type (constructor). Is is on par with the <code>Event</code> and <code>Behavior</code> abstractions, so we now have a “reactive trinity” of types instead of the old “reactive duopoly”.</p>
<p>Conceptually, the <code>Moment</code> type is very simple, to the point of being almost trivial. Namely, an expression</p>
<pre><code>x :: Moment T A      -- value of type  A  at time  T</code></pre>
<p>simply denotes a value of type <code>A</code> at the specific time <code>T</code>. In other words, it just tags an ordinary value with a specific time; the intention being that the tagged value is only “valid” at this particular <em>moment in time</em>. You also want to perform calculation with tagged values, that’s why <code>Moment t</code> is a monad.</p>
<p>Being able to tag values with clock times is nice and well, but the real magic only starts to happen when you <em>quantify</em> over the time parameter. Namely, an expression</p>
<pre><code>y :: forall t. Moment t A</code></pre>
<p>denotes a value of type <code>A</code> <em>for each</em> time <code>t</code>. Most importantly, this value can be <em>different</em> for different times <code>t</code>. In other words, this quantified version is extremely similar to a <code>Behavior</code>!</p>
<p>(For those who wonder about parametric polymorphism: best think of this as a dependent product, the parameter <code>t</code> is definitely not parametric, at least in spirit.)</p>
<p>The easiest use case for this quantified type is the <code>observeE</code> combinator</p>
<pre><code>observeE :: Event t (forall s. Moment s a) -&gt; Event t a</code></pre>
<p>Whenever the argument event occurs, it just unwraps the tag by setting <code>s</code> to the time of occurrence.</p>
<h3 id="variable-start-times-and-switching">Variable start times and switching</h3>
<p>With the <code>Moment</code> monad, it now becomes possible to express the notion of an <code>Event</code> or a <code>Behavior</code> with a <em>variable start time</em>. Namely, consider the type</p>
<pre><code>e :: forall t. Moment t (Event t A)</code></pre>
<p>Thanks to the <code>Moment</code> monad and the quantitifcation, this expression denotes an <code>Event</code> with start time <code>t</code> for each time <code>t</code>. Likewise, the expression</p>
<pre><code>b :: forall t. Moment t (Behavior t A)</code></pre>
<p>denotes a <code>Behavior</code> with variable start time.</p>
<p>The point of all this is that in reactive-banana, we may only switch between Events and Behaviors that have variable start times. In particular, the switching combinators have the types</p>
<pre><code>switchE :: Event t (forall s. Moment s (Event s a)) -&gt; Event t a

switchB :: Behavior t a
    -&gt; Event t (forall s. Moment s (Behavior s a))
    -&gt; Behavior t a</code></pre>
<p>For instance, <code>switchB</code> works like this: whenever the argument event occurs, its payload is a <code>Behavior</code> that starts at the time of occurrence <code>s</code>. This <code>Behavior</code> will be switched into the result until the next event occurs. In other words, the <code>switchB</code> combinator works exactly as described in the introduction, except that the types are slightly more elaborate.</p>
<p>Now you know how to <em>use</em> events and behaviors with variable start times, but I still have to tell you how to <em>construct</em> them. They are created by <em>trimming</em> existing events and behaviors. The relevant combinators are</p>
<pre><code>trimE :: Event t a
    -&gt; Moment t (forall s. Moment s (Event s a))

trimB :: Behavior t a
    -&gt; Moment t (forall s. Moment s (Behavior s a))</code></pre>
<p>Essentially, the <code>trimE</code> function just discards all event occurrences that happen before or on the new start time <code>s</code>. Similarly, the <code>trimB</code> combinator discards the history of the behavior that happens before the new start time <code>s</code>.</p>
<p>All these types are chosen such that you always have to trim an event or behavior before you can switch it in. This is necessary to ensure efficiency, i.e. the absence of time leaks, via the type system.</p>
<h3 id="forall-in-the-api">“forall” in the API</h3>
<p>One last thing: if you look at the <a href="http://hackage.haskell.org/packages/archive/reactive-banana/0.7.0.1/doc/html/Reactive-Banana-Switch.html">API documenation</a>, you will notice that the types are slightly different from what I just wrote. But fret not, this is just a harmless technical restriction having to do with <em><a href="http://www.haskell.org/ghc/docs/6.10.1/html/users_guide/other-type-extensions.html#impredicative-polymorphism">impredicative polymorphism</a></em>. (This doesn’t sound harmless at all, right? But be assured, it is as harmless as a baby banana.)</p>
<p>The thing is just that GHC currently can’t deal very well with <code>forall</code> inside type arguments, so I have made a new type, <code>AnyMoment</code>, to appease the gaelic Haskell gods. Read it like this:</p>
<pre><code>AnyMoment Event a     =  forall s. Moment s (Event s a)
AnyMoment Behavior a  =  forall s. Moment s (Behavior s a)
AnyMoment Identity a  =  forall s. Moment s a</code></pre>
<p>The <code>now</code> and <code>anyMoment</code> functions provide the corresponding mappings (isomorphisms).</p>
<h2 id="related-work">Related work</h2>
<p>I think it is very instructive to put reactive-banana’s approach to dynamic event switching into context and to clarify its connection to other approaches.</p>
<p>I learned about the importance of trimming from <a href="http://conal.net/blog/posts/trimming-inputs-in-functional-reactive-programming">a blog post by Conal Elliott</a>, where he writes that</p>
<blockquote>
<p>Failing to trim results in behavior that is incorrect and grossly inefficient. […] (the dreaded “space-time” leak)</p>
</blockquote>
<p>The key point of the approach presented here is to enforce trimming via the type system, thereby avoding time leaks statically.</p>
<p>The idea of using a type parameter to denote start times and to control trimming goes back to <a href="http://www.ioc.ee/~wolfgang/research/tfp-2009-paper.pdf">a paper by Wolfgang Jeltsch</a>. It follows Conal’s method in that trimming is tied to the occurrences of an existing event.</p>
<pre><code>trim :: Behavior b -&gt; Event a -&gt; Event (a, Behavior b)</code></pre>
<p>The key novelty in our approach is the <code>Moment</code> monad, which allows us to obtain trimmed behaviors and events as first-class objects, which is a lot more flexible. Of course, the results of <code>trimB</code> and <code>trimE</code> are wrapped in a <code>Moment t</code> monad, so you do have to specify when trimming should <em>start</em>, but it is no longer necessary to specify when trimming should <em>happen</em>.</p>
<p>Another first-class approach to trimming was <a href="http://sgate.emt.bme.hu/documents/patai/publications/PataiWFLP2010.pdf">introduced by Gergely Patai</a>. It has also been implemented by <a href="http://hackage.haskell.org/package/sodium">Stephen Blackheath in his sodium library</a>. They don’t use a type parameter to specify start times, so trimming has to be done “whenever it changes meaning”, which is whenever we accumulate state. For instance, the <code>accumE</code> combinator has the type</p>
<pre><code>accumE :: a -&gt; Event (a -&gt; a) -&gt; Reactive (Event a)</code></pre>
<p>where <code>Reactive</code> is a monad similar to the <code>forall t. Moment t</code> monad. In other words, events with variable start time are first-class and represented by the type <code>Reactive (Event a)</code>. Compared to this method, the advantage of our approach is that the accumulation combinators can retain their simple types as we have separate control over trimming via the <code>trimE</code> and <code>trimB</code> combinators. Of course, only time will tell whether this is actually an advantage in practice.</p>
<p>Apart from utility of the <code>Moment</code> type as a device for enforcing typing rules, I think that its <em>interpretation</em> in terms of clock times is so compelling that it probably has theoretical significance as well. <a href="http://cs.ioc.ee/~tarmo/tsem10/jeltsch-slides.pdf">Wolfgang Jeltsch has described</a> a Curry-Howards isomorphism from FRP to temporal linear logic, and I think that the <code>Moment</code> monad may enter this picture as well. It corresponds to the ability to <em>embed</em> the category of Haskell values into the category of temporal values. This is important if you want to use FRP within Haskell, otherwise you end up with a separate domain specific language. In other words, it allows us to mix temporal logic with standard lambda calculus.</p>
<hr />
<p>Alight, that’s everything I wanted to say about APIs for dynamic event switching today. As always, I appreciate your comments.</p>
<hr /><p><a href="https://flattr.com/thing/29608/apfelmus-website"><img src="http://api.flattr.com/button/flattr-badge-large.png" alt="Flattr this" title="Flattr this" border="0" /></a></p>
]]></description></item><item>
<title>FRP - Release of reactive-banana version 0.7</title>
<link>http://apfelmus.nfshost.com/blog/2012/08/26-frp-banana-0-7.html</link>
<pubDate>Sun, 26 Aug 2012 21:53:39 +0200</pubDate>
<description><![CDATA[
<p>It’s time to celebrate! I have just released version 0.7.0.0 of my <a href="http://www.haskell.org/haskellwiki/Reactive-banana">reactive-banana library</a> on <a href="http://hackage.haskell.org/package/reactive-banana">hackage</a>!</p>
<p>This version is a big step forward as it features <em>dynamic event switching</em>, which means that you can now make an event which contains other events and do useful things with it. The event will have a type like <code>Event (Event A)</code> and you can, for instance, dynamically switch between the inner events by using the new <code>switchE</code> combinator.</p>
<p>The <a href="https://github.com/HeinrichApfelmus/reactive-banana/blob/master/reactive-banana-wx/src/BarTab.hs">BarTab.hs</a> example demonstrates the new possibilities. It shows how to create new widgets and behaviors in reaction to event occurrences. When you click the “Add” button, a new edit widget will be added and it’s <code>Behavior</code> will contribute to the total sum.</p>
<div class="figure">
<img src="http://apfelmus.nfshost.com/blog/2012/08/26-frp-banana-0-7/bartab.png" /><p class="caption"></p>
</div>
<p>While dynamic event switching is great news, I also have to point out that version 0.7.0.0 of reactive-banana is a highly experimental release (for my standard of “highly experimental”). Dynamic event switching is not easy to get right, and while I have managed to avoid the dreaded time leaks, there are still major issues with garbage collection. They can be solved, but for now, I wanted to get my preliminary code out there, so that you can experiment with the library and send me your valuable feedback.</p>
<h2 id="api-elegance">API elegance</h2>
<p>Aside from efficiency, the other issue with dynamic event switching is that the API is rather complex. This is necessary if we want to void time leaks, but the whole thing does become a little less elegant than the original vision for FRP would have it.</p>
<p>(I will explain the new API in a subsequent post.)</p>
<p>Fortunately, there are several distinct ways to incorporate dynamic event switching into the API.</p>
<p>My “banana-nemesis”, the <a href="http://hackage.haskell.org/package/sodium">sodium</a> library, uses an <a href="http://sgate.emt.bme.hu/documents/patai/publications/PataiWFLP2010.pdf">approach first described by Gergely Patai</a>, which forces all accumulating combinators like <code>stepper</code> or <code>accumE</code> into a monad. This is conceptually simple, but I think the notational burden is quite high.</p>
<p>I have taken another approach, which keeps the first-order interface intact, but introduces some additional complexity when you want to use dynamic event switching. My approach was inspired by <a href="http://www.ioc.ee/~wolfgang/research/tfp-2009-paper.pdf">Wolfgang Jeltsch’s use of rank-2-polymorphism</a>, but goes beyond it and can now express significantly more programs.</p>
<p>I don’t know which of these two API approaches is more convenient to use. Since dynamic event switching is needed rather seldomly, I found it more important to present beginners with a simple first-order API, and to make the higher-order stuff entirely optional. Besides, I thought it would be a good idea to explore a different part of the design space than the sodium library already does. Give it a try and tell us how it works for you!</p>
<h2 id="changelog">Changelog</h2>
<p>Aside from the new module <code>Reactive.Banana.Switch</code>, the main change compared to the previous 0.6 release is that the <code>NetworkDescription</code> monad is now called <code>Moment</code> and it has a new constraint on the type parameter. You have to exchange the old type</p>
<pre><code>NetworkDescription t a</code></pre>
<p>for the new type</p>
<pre><code>Frameworks t =&gt; Moment t a</code></pre>
<p>everwhere.</p>
<p>Moreover, the current version can no longer be compiled with the <a href="http://uu-computerscience.github.com/uhc-js/">JavaScript backend</a> of the <a href="http://www.cs.uu.nl/wiki/UHC">Utrecht Haskell Compiler</a>. There is no fundamental obstacle to that, as I don’t use any significant GHC-specific extensions; I just haven’t put the time into making this compatible yet. But do note that future versions of reactive-banana will need GHC extensions like weak pointers, so that the problems with garbage collection can be resolved.</p>
<h2 id="advertisment">Advertisment</h2>
<div style="float:left">
<img src="https://github.com/HeinrichApfelmus/reactive-banana/raw/develop/banana.png">
</div>
<p>Howdy, cowboy, it’s advertisment time! If you like the reactive-banana project, you can support me with a small donation: <a href="http://flattr.com/thing/384682/reactive-banana"><img src="http://api.flattr.com/button/flattr-badge-large.png" alt="Flattr this!" /></a>. Furthermore, at the end of every month, all ardent supporters will receive ¢2 worth of status information and fruity puns! (This magnificent offer can be respectfully declined in your Flattr preferences.)</p>
<hr /><p><a href="https://flattr.com/thing/29608/apfelmus-website"><img src="http://api.flattr.com/button/flattr-badge-large.png" alt="Flattr this" title="Flattr this" border="0" /></a></p>
]]></description></item><item>
<title>FRP - Slides from the tutorial session in Halle</title>
<link>http://apfelmus.nfshost.com/blog/2012/07/15-frp-tutorial-slides.html</link>
<pubDate>Sun, 15 Jul 2012 13:50:55 +0200</pubDate>
<description><![CDATA[
<p>Hello everyone,</p>
<p>last Friday, I gave a tutorial session on functional reactive programming (FRP) with reactive-banana at the <a href="http://iba-cg.de/hal7.html">HaL 7 - Haskell in Halle/Saale</a> meeting.</p>
<p>The tutorial was split into two parts: first a 30 minutes talk where I introduced the basic concepts and then one hour of live demonstration, where I sat down in front of a computer and implemented some examples in a dialogue with the audience. People told me that they liked it very much, which makes me very happy.</p>
<p>For those who could not attend because they don’t speak German, I have now translated the <a href="https://github.com/HeinrichApfelmus/reactive-banana/blob/07f3e9bf44ec8c227442daef723f7e9682c5a342/reactive-banana/doc/tutorial-2012-07-en.pdf?raw=true">slides from the introductory talk</a> into English and added some annotations. I think they may be a good introduction to the <code>Behavior</code> and <code>Event</code> types.</p>
<p>Henning Thielemann also managed to organize audio and video recordings during the meeting, but it’s not clear whether the quality is alright. Post-processing will probably take a while. A big “thank you” to Henning and the program committee for organizing the meeting in the first place!</p>
<p>EDIT: The <a href="http://www.youtube.com/watch?v=bMK6nDh6LX0&amp;list=PL8C127F21AE8E9BFC&amp;index=1&amp;feature=plpp_Video">video recording of my tutorial</a> is now available on the <a href="http://iba-cg.de/hal7.html">meeting website</a>. It’s in German, though.</p>
<hr /><p><a href="https://flattr.com/thing/29608/apfelmus-website"><img src="http://api.flattr.com/button/flattr-badge-large.png" alt="Flattr this" title="Flattr this" border="0" /></a></p>
]]></description></item><item>
<title>FRP - Tutorial session on FRP with reactive-banana in Halle</title>
<link>http://apfelmus.nfshost.com/blog/2012/07/05-frp-reactive-banana-tutorial-session.html</link>
<pubDate>Thu, 05 Jul 2012 13:08:51 +0200</pubDate>
<description><![CDATA[
<p>Hello everyone,</p>
<p>I just wanted to remind you that I’m going to give a tutorial session on functional reactive programming with reactive-banana at the <a href="http://iba-cg.de/hal7.html">HaL 7 - Haskell in Halle/Saale</a> meeting.</p>
<ul>
<li><em>When?</em> 13 July 2012. 11:30 am</li>
<li><em>Where?</em> Martin-Luther-Universität Halle-Wittenberg, Institut für Informatik, Halle/Saale (Germany)</li>
<li><em>URL?</em> <a href="http://iba-cg.de/hal7.html">http://iba-cg.de/hal7.html</a></li>
</ul>
<p>In this tutorial session, I’m going to give a short introduction to FRP and then we’ll work through some examples. I encourage everyone to request particular examples from the <a href="http://haskell.org/haskellwiki/Reactive-banana/Examples">examples collection</a> or to suggest new ones during the tutorial.</p>
<p>If you want to participate, remember to <a href="http://sim.mathematik.uni-halle.de:8080/hal7/"><strong>register</strong> for the tutorial</a> as we only have enough space to accomodate 25 people per tutorial. There are other tutorials as well, so check out the program to see if there’s anything else you like. In particular, <a href="http://www.andres-loeh.de/">Andres Löh</a> is giving an introduction to GPU programming with the <a href="http://repa.ouroborus.net/">repa</a> and <a href="https://github.com/AccelerateHS/accelerate/">accelerate</a> libraries.</p>
<p>To get most out of my reactive-banana tutorial, make sure that you install wxHaskell beforehand, as this tends to be a bit difficult.</p>
<p>The session will be in German. Unfortunately, it probably won’t be recorded on video, but I can translate and post my introductory slides afterwards.</p>
<hr /><p><a href="https://flattr.com/thing/29608/apfelmus-website"><img src="http://api.flattr.com/button/flattr-badge-large.png" alt="Flattr this" title="Flattr this" border="0" /></a></p>
]]></description></item><item>
<title>Forklift - a pattern for performing monadic actions in a worker thread</title>
<link>http://apfelmus.nfshost.com/blog/2012/06/07-forklift.html</link>
<pubDate>Thu, 07 Jun 2012 16:28:49 +0200</pubDate>
<description><![CDATA[
<p><a href="http://www.scannedinavian.com/about/">Shae Erisson</a> wants to build a <a href="http://ghclive.wordpress.com/">browser-based GHC interpreter</a> for his Google Summer of Code project. This involves writing a web application to control a GHCi session. For the web server part, he’s using the pleasingly minimal <a href="http://hackage.haskell.org/package/scotty">scotty</a> package, whereas the Haskell interpreter functionality is provided by the <a href="http://hackage.haskell.org/package/hint">hint</a> package.</p>
<p>However, there is a problem: to keep track of various internals, each package uses a monad. But the two monads are incompatible! It is almost impossible to use one of them as a monad transformer on top of the other. How can we combine them anyway?</p>
<p>Andrew Farmer suggested instead to put the interpreter session into a different thread, the worker thread, and to use <a href="http://hackage.haskell.org/packages/archive/base/latest/doc/html/Control-Concurrent-MVar.html">MVars</a> for communication between the web server and the worker thread. And indeed, that is how similar projects like <a href="https://github.com/divipp/ActiveHs">active-hs</a> solve this problem as well.</p>
<p>However, I didn’t want to create a new communication protocal just for this situation, so I thought of a generic way to do this, which I want to call “the forklift pattern” and which I will now describe.</p>
<p>Your feedback is appreciated! It’s probably a good idea to release this on hackage, but I would like to iron out a few kinks beforehand. The preliminary source code can be found <a href="https://gist.github.com/2889080">as a gist</a>. Leave your comments below!</p>
<h1 id="the-forklift-pattern">The forklift pattern</h1>
<p>So, the situation is this: we have a monad <code>m</code> that, for example, represents an interpreter for Haskell expressions. We want to make its computations available in the <code>IO</code> monad and we can do so by running the monad <code>m</code> in a separate worker thread that communicates via MVars. For instance, in the case of the Haskell interpreter, we want to send Haskell expressions to the worker thread for evaluation.</p>
<p>The simple but key insight is this: what is the most general possible communication protocal for talking to the worker thread? Why, we can simply ask the worker thread to perform an <em>arbitrary</em> action of type <code>m a</code>! In other words, we simply put a complete monadic action into the MVar and have the worker thread execute it for us.</p>
<p>I will call this pattern the “forklift” pattern because we are <em>forking</em> a worker thread to <em>lift</em> arbitrary monadic actions. Nifty.</p>
<p>Here we go. The worker thread is represented by the data type</p>
<pre><code>data ForkLift m = ForkLift { requests :: Chan (m ()) }</code></pre>
<p>which just keeps track of a channel were you can send your monadic actions to. To create a new <code>ForkLift</code>, we fork a worker thread that will forever read values from the channel and execute them</p>
<pre><code>newForkLift :: MonadIO m
            =&gt; (m () -&gt; IO ()) -&gt; IO (ForkLift m)
newForkLift unlift = do
    channel &lt;- newChan
    let loop = forever . join . liftIO $ readChan channel
    forkIO $ unlift loop
    return $ ForkLift channel</code></pre>
<p>When we want to execute a monadic action, we can use the forklift to “carry it over to the IO monad”. Getting the result value back is a bit tricky due to type safety, but we can simply use another MVar and encode the sending back in the action itself.</p>
<pre><code>carry :: MonadIO m =&gt; ForkLift m -&gt; m a -&gt; IO a
carry forklift act = do
    ref &lt;- newEmptyMVar
    writeChan (requests forklift) $ do
        liftIO . putMVar ref =&lt;&lt; act
    takeMVar ref</code></pre>
<p>And here a quick test with the state monad transformer</p>
<pre><code>test :: IO ()
test = do
    state &lt;- newForkLift (flip evalStateT (0::Int))
    carry state $ modify (+10)
    carry state $ modify (+10)
    print =&lt;&lt; carry state get

-- &gt; test
-- 20</code></pre>
<p>Of course, the <code>evalStateT</code> function was always able to map the state monad to the <code>IO</code> monad, but the point here is that the state is <em>preserved</em> between different invocations of the <code>carry</code> function.</p>
<p>Before I can put this on hackage, there are a few rough edges that need to be smoothed out.</p>
<ol style="list-style-type: decimal">
<li>What happens when the worker thread dies because of an exception? At the moment, all calls to <code>carry</code> will deadlock. They should probably throw an exception instead, <code>WorkerGone</code>?</li>
<li>What about garbage collection? When the <code>ForkLift</code> goes out of scope, then the worker thread should also be killed. See my <a href="http://stackoverflow.com/questions/10871303/killing-a-thread-when-mvar-is-garbage-collected">StackOverflow question</a>.</li>
</ol>
<hr /><p><a href="https://flattr.com/thing/29608/apfelmus-website"><img src="http://api.flattr.com/button/flattr-badge-large.png" alt="Flattr this" title="Flattr this" border="0" /></a></p>
]]></description></item>  </channel></rss>