<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>Lambdalog</title>
        <link>http://lambdalog.seanseefried.com</link>
        <description><![CDATA[RSS feed for Lambdalog]]></description>
        <atom:link href="http://lambdalog.seanseefried.com/rss.xml" rel="self"
                   type="application/rss+xml" />
        <lastBuildDate>Mon, 16 May 2011 00:00:00 UT</lastBuildDate>
        <item>
    <title>Reifying Type Classes with GADTs</title>
    <link>http://lambdalog.seanseefried.com/posts/2011-05-16-reifying-type-classes-with-gadts.html</link>
    <description><![CDATA[<p>This year I’ve been contributing to <a href="http://hackage.haskell.org/package/accelerate">Accelerate</a>, an embedded language for regular, multi-dimensional array computations targetting high-performance back-ends such as NVIDIA GPUs. Even a cursory glance of the code base shows that it uses a number of advanced Haskell extensions including <em>Generalized Algebraic Data Types</em> (<a href="http://www.haskell.org/haskellwiki/GADT">GADTs</a>) and <a href="http://www.haskell.org/haskellwiki/GHC/Indexed_types">type families</a>.</p>
<p>Earlier this year I learned a technique which seems to be part of the Haskell folklore but not necessarily well known: reifying types belonging to a type class into a GADT value. A typical use-case for this technique is using a type class defined in a library. Either you don’t have access to the source code (or for reasons of modularity/API stability you don’t want to touch it) but, nevertheless, you want to add a method to the type class. For argument’s sake let’s call this type class <code>C</code>.</p>
<p>The standard solution would be to declare a new type class, let’s call it <code>D</code>, with a super-class constraint on class <code>C</code>. (e.g. <code>class C a =&gt; D a</code>). You would then have to write one instance of class <code>D</code> for each existing instance of class <code>C</code>. Not only could this be a lot of work, you could run into the same problem of needing to extend class <code>D</code> further down the track (perhaps necessitating the declaration of class <code>E</code>).</p>
<p>What if there were a way to allow class <code>C</code> to be arbitrarily extensible? This being a blog post on the topic, it should come as no surprise that there is, with one caveat: the class must be <em>closed</em>. That is, the instances that have been written are complete and will not need to be changed in the future.</p>
<p>I’ll demonstrate how to reify types into values of a GADT using an example. Say we have a class, <code>Counter</code>, with a single method <code>inc</code> to increment a value.</p>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="ot">{-# LANGUAGE GADTs #-}</span><br /><br /><span class="kw">class</span> <span class="dt">Counter</span> a <span class="kw">where</span><br /><span class="ot">  inc </span><span class="ot">::</span> a <span class="ot">-&gt;</span> a</code></pre>
<p>Here are all the instances we will ever write for this class.</p>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="kw">data</span> <span class="dt">CounterR</span> a <span class="kw">where</span><br />  <span class="dt">CounterRint</span><span class="ot">  </span><span class="ot">::</span> <span class="dt">CounterR</span> <span class="dt">Int</span><br />  <span class="dt">CounterRchar</span><span class="ot"> </span><span class="ot">::</span> <span class="dt">CounterR</span> <span class="dt">Char</span><br />  <span class="dt">CounterRlist</span><span class="ot"> </span><span class="ot">::</span> <span class="dt">CounterR</span> a <span class="ot">-&gt;</span> <span class="dt">CounterR</span> [a]<br />  <span class="dt">CounterRpair</span><span class="ot"> </span><span class="ot">::</span> <span class="dt">CounterR</span> a <span class="ot">-&gt;</span> <span class="dt">CounterR</span> b <span class="ot">-&gt;</span> <span class="dt">CounterR</span> (a,b)<br /><br /><span class="kw">class</span> <span class="dt">Counter</span> a <span class="kw">where</span><br /><span class="ot">  inc </span><span class="ot">::</span> a <span class="ot">-&gt;</span> a<br /><br /><span class="kw">instance</span> <span class="dt">Counter</span> <span class="dt">Int</span> <span class="kw">where</span><br />  inc a <span class="fu">=</span> a <span class="fu">+</span> <span class="dv">1</span><br /><br /><span class="kw">instance</span> <span class="dt">Counter</span> <span class="dt">Char</span> <span class="kw">where</span><br />  inc a <span class="fu">=</span> <span class="fu">chr</span> (<span class="fu">ord</span> a <span class="fu">+</span> <span class="dv">1</span>)<br /><br /><span class="kw">instance</span> <span class="dt">Counter</span> a <span class="ot">=&gt;</span> <span class="dt">Counter</span> [a] <span class="kw">where</span><br />  inc <span class="kw">as</span> <span class="fu">=</span> <span class="fu">map</span> inc <span class="kw">as</span><br /><br /><span class="kw">instance</span> (<span class="dt">Counter</span> a, <span class="dt">Counter</span> b) <span class="ot">=&gt;</span> <span class="dt">Counter</span> (a,b) <span class="kw">where</span><br />  inc (a,b) <span class="fu">=</span> (inc a, inc b)</code></pre>
<p>Although the instances are fixed, we wish to allow others to effectively add new methods to the class in the future. We can do this by declaring a GADT that <a href="http://en.wikipedia.org/wiki/Reification_(computer_science)">reifies</a> the instance types. By convention we name this data structure by appending the character <code>R</code> to the class name.</p>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="kw">data</span> <span class="dt">CounterR</span> a <span class="kw">where</span><br />  <span class="dt">CounterRint</span><span class="ot">  </span><span class="ot">::</span> <span class="dt">CounterR</span> <span class="dt">Int</span><br />  <span class="dt">CounterRchar</span><span class="ot"> </span><span class="ot">::</span> <span class="dt">CounterR</span> <span class="dt">Char</span><br />  <span class="dt">CounterRlist</span><span class="ot"> </span><span class="ot">::</span> <span class="dt">CounterR</span> a <span class="ot">-&gt;</span> <span class="dt">CounterR</span> [a]<br />  <span class="dt">CounterRpair</span><span class="ot"> </span><span class="ot">::</span> <span class="dt">CounterR</span> a <span class="ot">-&gt;</span> <span class="dt">CounterR</span> b <span class="ot">-&gt;</span> <span class="dt">CounterR</span> (a,b)</code></pre>
<p>Please note the following properties of this declaration:</p>
<ul>
<li><p>There is one constructor for each instance of class <code>Counter</code>.</p></li>
<li><p>Every constraint on the <code>Counter</code> class in an instance declaration becomes a parameter of the corresponding GADT constructor. e.g. <code>instance (Counter a, Counter b) =&gt; Counter (a,b)</code> becomes <code>CounterRpair :: CounterR a -&gt; CounterR b -&gt; CounterR (a,b)</code>.</p></li>
</ul>
<p>We now need to add a new method signature to class <code>Counter</code> and provide an implementation for it in each of the instances. By convention this method is called <code>counterR</code>; that is, the name of the class with the first letter lower-cased and suffixed with character ‘R’.</p>
<p>Our module so far looks like this:</p>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="ot">{-# LANGUAGE GADTs, FlexibleInstances, UndecidableInstances #-}</span><br /><span class="kw">module</span> <span class="dt">Counter</span> <span class="kw">where</span><br /><br /><span class="kw">import</span> <span class="dt">Data.Char</span><br /><br /><span class="kw">data</span> <span class="dt">CounterR</span> a <span class="kw">where</span><br />  <span class="dt">CounterRint</span><span class="ot">  </span><span class="ot">::</span> <span class="dt">CounterR</span> <span class="dt">Int</span><br />  <span class="dt">CounterRchar</span><span class="ot"> </span><span class="ot">::</span> <span class="dt">CounterR</span> <span class="dt">Char</span><br />  <span class="dt">CounterRlist</span><span class="ot"> </span><span class="ot">::</span> <span class="dt">CounterR</span> a <span class="ot">-&gt;</span> <span class="dt">CounterR</span> [a]<br />  <span class="dt">CounterRpair</span><span class="ot"> </span><span class="ot">::</span> <span class="dt">CounterR</span> a <span class="ot">-&gt;</span> <span class="dt">CounterR</span> b <span class="ot">-&gt;</span> <span class="dt">CounterR</span> (a,b)<br /><br /><span class="kw">class</span> <span class="dt">Counter</span> a <span class="kw">where</span><br /><span class="ot">  inc </span><span class="ot">::</span> a <span class="ot">-&gt;</span> a<br /><span class="ot">  counterR </span><span class="ot">::</span> <span class="dt">CounterR</span> a<br /><br /><span class="kw">instance</span> <span class="dt">Counter</span> <span class="dt">Int</span> <span class="kw">where</span><br />  inc a <span class="fu">=</span> a <span class="fu">+</span> <span class="dv">1</span><br />  counterR <span class="fu">=</span> <span class="dt">CounterRint</span><br /><br /><span class="kw">instance</span> <span class="dt">Counter</span> <span class="dt">Char</span> <span class="kw">where</span><br />  inc a <span class="fu">=</span> <span class="fu">chr</span> (<span class="fu">ord</span> a <span class="fu">+</span> <span class="dv">1</span>)<br />  counterR <span class="fu">=</span> <span class="dt">CounterRchar</span><br /><br /><span class="kw">instance</span> <span class="dt">Counter</span> a <span class="ot">=&gt;</span> <span class="dt">Counter</span> [a] <span class="kw">where</span><br />  inc <span class="kw">as</span> <span class="fu">=</span> <span class="fu">map</span> inc <span class="kw">as</span><br />  counterR <span class="fu">=</span> <span class="dt">CounterRlist</span> counterR<br /><br /><span class="kw">instance</span> (<span class="dt">Counter</span> a, <span class="dt">Counter</span> b) <span class="ot">=&gt;</span> <span class="dt">Counter</span> (a,b) <span class="kw">where</span><br />  inc (a,b) <span class="fu">=</span> (inc a, inc b)<br />  counterR <span class="fu">=</span> <span class="dt">CounterRpair</span> counterR counterR</code></pre>
<p>Note that definition of <code>counterR</code> for each instance follows a very simple pattern. It is simply the application of the appropriate constructor to the appropriate number of calls to </code>counterR</code>.</p>
<p>It’s important to be clear that values of <code>CounterR a</code> don’t represent <em>values</em> of type <code>a</code>, they represent the <em>type</em> <code>a</code> itself.</p>
<p>For instance a value of type</p>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="dt">CounterR</span> (([<span class="dt">Int</span>], <span class="dt">Int</span>), (<span class="dt">Int</span>, <span class="dt">Char</span>))</code></pre>
<p>reifies to the value</p>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="dt">CounterRpair</span><br />  (<span class="dt">CounterRpair</span> (<span class="dt">CounterRlist</span> <span class="dt">CounterRint</span>) <span class="dt">CounterRint</span>))<br />  (<span class="dt">CounterRpair</span> <span class="dt">CounterRint</span> <span class="dt">CounterRchar</span>)</code></pre>
<p>We can now write new methods on any type that is a member of class <code>Counter</code>. I’ll show you with an example. Say we now want to write a function that decrements instead of incrementing. We can write this as follows, but please note that the function takes the representation of the type <em>and</em> the value as an argument.</p>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="ot">dec </span><span class="ot">::</span> <span class="dt">CounterR</span> a <span class="ot">-&gt;</span> a <span class="ot">-&gt;</span> a<br />dec <span class="dt">CounterRint</span> a                    <span class="fu">=</span> a <span class="fu">-</span> <span class="dv">1</span><br />dec <span class="dt">CounterRchar</span> a                   <span class="fu">=</span> <span class="fu">chr</span> (<span class="fu">ord</span> a <span class="fu">-</span> <span class="dv">1</span>)<br />dec (<span class="dt">CounterRlist</span> incRa) <span class="kw">as</span>          <span class="fu">=</span> <span class="fu">map</span> (dec incRa) <span class="kw">as</span><br />dec (<span class="dt">CounterRpair</span> incRa incRb) (a,b) <span class="fu">=</span> (dec incRa a, dec incRb b)</code></pre>
<p>There are only a few things to note:</p>
<ul>
<li>If <code>dec</code> were a method of class <code>Counter</code> then you would need to define it in each instance. Here you provide each implementation as a case of the <code>dec</code> function.</li>
<li>Since <code>dec</code> requires a value of type <code>CounterR</code> each recursive call to <code>dec</code> requires that you pass an appropriate value of type <code>CounterR</code>. Fortunately, the appropriate value is precisely one of those pattern matched on the left hand side.</li>
</ul>
<p>As I noted at the beginning. This technique is not new, just relatively unknown. A <a href="http://lambda-the-ultimate.org/node/3837">paper</a> by Oliveira and Sulzmann suggests unifying the two concepts. Also, GADTs have been used as an <a href="http://repetae.net/computer/jhc/jhc-reify-typeclass.html">implementation technique</a> in <a href="http://repetae.net/computer/jhc/">JHC</a> instead of the normal <a href="http://homepages.inf.ed.ac.uk/wadler/papers/class/class.ps.gz">dictionary passing</a> implementation.</p>]]></description>
    <pubDate>Mon, 16 May 2011 00:00:00 UT</pubDate>
    <guid>http://lambdalog.seanseefried.com/posts/2011-05-16-reifying-type-classes-with-gadts.html</guid>
</item>
<item>
    <title>Context sensitive aliases for git</title>
    <link>http://lambdalog.seanseefried.com/posts/2011-05-21-context-sensitive-aliases-for-git.html</link>
    <description><![CDATA[<p>As developers many of us use git. Recently Peteris Krumins wrote a post about git aliases. The basic idea was to make aliases such as</p>
<pre class="sourceCode"><code class="sourceCode bash"><span class="kw">alias</span> <span class="ot">gs=</span><span class="st">'git status'</span></code></pre>
<p>for common git commands. However, we can already see a problem. The command for GhostScript is gs. What we really need is context sensitive aliases so that the command gs means git status when one is inside a git repository and gs everywhere else in the directory hierarchy. I whipped up a nice little bash script to generate context sensitive aliases.</p>
<pre class="sourceCode"><code class="sourceCode bash"><span class="co">#!/bin/bash</span><br /><br /><span class="kw">if </span>[ <span class="ot">$#</span> -lt 2 ]; <span class="kw">then</span><br />  <span class="kw">exit</span> 1<br /><span class="kw">fi</span>;<br /><br /><span class="ot">ALIAS_NAME=$1</span><br /><span class="kw">shift</span><br /><span class="ot">CMD=$1</span><br /><span class="kw">shift</span><br /><span class="kw">(</span>git rev-parse --is-inside-work-tree <span class="kw">&gt;</span> /dev/null <span class="kw">2&gt;&amp;1</span><span class="kw">)</span><br /><span class="kw">if </span>[ <span class="ot">$?</span> -eq 0 ]; <span class="kw">then</span><br />  <span class="ot">$CMD</span> <span class="st">&quot;</span><span class="ot">$@</span><span class="st">&quot;</span><br /><span class="kw">else</span><br />  <span class="ot">$ALIAS_NAME</span> <span class="st">&quot;</span><span class="ot">$@</span><span class="st">&quot;</span><br /><span class="kw">fi</span></code></pre>
<p>By checking the return status of git status one can decide whether to call one command or another. You can then define the aliases in a .bashrc (or similar) configuration file.</p>
<pre class="sourceCode"><code class="sourceCode bash"><span class="kw">alias</span> <span class="ot">ga=</span><span class="st">'git-alias ga   &quot;git add&quot;'</span><br /><span class="kw">alias</span> <span class="ot">gds=</span><span class="st">'git-alias gds &quot;git diff --staged&quot;'</span><br /><span class="kw">alias</span> <span class="ot">ga=</span><span class="st">'git-alias ga  &quot;git add&quot;'</span><br /><span class="kw">alias</span> <span class="ot">gp=</span><span class="st">'git-alias gp  &quot;git push&quot;'</span><br /><span class="kw">alias</span> <span class="ot">gl=</span><span class="st">'git-alias gl  &quot;git log&quot;'</span><br /><span class="kw">alias</span> <span class="ot">gs=</span><span class="st">'git-alias gs  &quot;git status&quot;'</span><br /><span class="kw">alias</span> <span class="ot">gd=</span><span class="st">'git-alias gd &quot;git diff&quot;'</span><br /><span class="kw">alias</span> <span class="ot">gc=</span><span class="st">'git-alias gc &quot;git commit&quot;'</span><br /><span class="kw">alias</span> <span class="ot">gm=</span><span class="st">'git-alias gm &quot;git commit -m&quot;'</span><br /><span class="kw">alias</span> <span class="ot">gma=</span><span class="st">'git-alias gma &quot;git commit -am&quot;'</span><br /><span class="kw">alias</span> <span class="ot">gb=</span><span class="st">'git-alias gb &quot;git branch&quot;'</span><br /><span class="kw">alias</span> <span class="ot">gc=</span><span class="st">'git-alias gc &quot;git checkout&quot;'</span><br /><span class="kw">alias</span> <span class="ot">gra=</span><span class="st">'git-alias gra &quot;git remote add&quot;'</span><br /><span class="kw">alias</span> <span class="ot">grr=</span><span class="st">'git-alias grr &quot;git remote rm&quot;'</span><br /><span class="kw">alias</span> <span class="ot">gpu=</span><span class="st">'git-alias gpu &quot;git pull&quot;'</span><br /><span class="kw">alias</span> <span class="ot">gcl=</span><span class="st">'git-alias gcl &quot;git clone&quot;'</span></code></pre>
<p>The idea of context sensitive aliases is by no means limited to git. It could be used anywhere where you have a way of detecting whether a property (e.g. whether you’re in a repository or not) holds within a directory hierarchy.</p>]]></description>
    <pubDate>Sat, 21 May 2011 00:00:00 UT</pubDate>
    <guid>http://lambdalog.seanseefried.com/posts/2011-05-21-context-sensitive-aliases-for-git.html</guid>
</item>
<item>
    <title>Generic dot products</title>
    <link>http://lambdalog.seanseefried.com/posts/2011-06-27-generic-dot-products.html</link>
    <description><![CDATA[<p><a href="/static/files/generic-dot-products.pdf">Companion slides</a></p>
<p>This is the first in a series of posts about program derivation. In particular, I am attempting to derive a matrix multiplication algorithm that runs efficiently on parallel architectures such as GPUs.</p>
<p>As I mentioned in an earlier <a href="/posts/2011-05-16-reifying-type-classes-with-gadts.html">post</a>, I’ve been contributing to the <a href="http://hackage.haskell.org/package/accelerate">Accelerate</a> project. The Accelerate EDSL defines various <em>parallel primitives</em> such as <code>map</code>, <code>fold</code>, and <code>scan</code> (and many more).</p>
<p>The <code>scan</code> primitive (also known as <em>all-prefix-sums</em>) is quite famous because it is useful in a wide range of parallel algorithms and, at first glance, one could be forgiven for thinking it is not amenable to parallelisation. However, a well-known <a href="http://www.cs.cmu.edu/~scandal/html-papers/short/short.html">work efficient</a> algorithm for <code>scan</code> was popularised by Guy Blelloch which performs \(O(n)\) work.</p>
<p>The algorithm is undeniably clever. Looking at it, it is not at all obvious how one might have gone about developing it oneself. A <a href="http://conal.net/blog/posts/deriving-list-scans/">recent</a> <a href="http://conal.net/blog/posts/deriving-parallel-tree-scans/">series</a> <a href="http://conal.net/blog/posts/composable-parallel-scanning/">of</a> <a href="http://conal.net/blog/posts/parallel-tree-scanning-by-composition/">posts</a> by <a href="http://conal.net">Conal Elliott</a> set out to redress this situation by attempting to <em>derive</em> the algorithm from a high level specification. His success has inspired me to follow a similar process to derive a work efficient matrix multiplication algorithm.</p>
<p>The process I am following is roughly as follows:</p>
<ul>
<li><p>generalise the concept of matrix multiplication to data structures other than lists or arrays.</p></li>
<li><p>develop a generic implementation that relies, as far as possible, on reusable algebraic machinery in type classes such as <a href="http://hackage.haskell.org/packages/archive/base/latest/doc/html/Prelude.html#t:Functor">Functor</a>, <a href="http://hackage.haskell.org/packages/archive/base/latest/doc/html/Control-Applicative.html">Applicative</a>, <a href="http://hackage.haskell.org/packages/archive/base/latest/doc/html/Data-Foldable.html">Foldable</a> and <a href="http://hackage.haskell.org/packages/archive/base/latest/doc/html/Data-Traversable.html">Traversable</a>.</p></li>
<li><p>use this generic implementation as a <em>specification</em> to derive an efficient algorithm. To call it a hunch that the underlying data structure is going to be tree-like is an understatement.</p></li>
</ul>
<p>This post is a preamble. It develops a <em>generic dot product</em> implementation that will serve as a specification for the derivation of an efficient algorithm in a later post.</p>
<h1 id="background">Background</h1>
<p>In order to understand this post I highly recommend that you read <a href="http://strictlypositive.org/">Conor McBride</a> and <a href="http://www.soi.city.ac.uk/~ross/">Ross Paterson</a>’s paper: <a href="http://www.soi.city.ac.uk/~ross/papers/Applicative.pdf">Applicative programming with effects</a>. A basic grasp of linear algebra would also be helpful.</p>
<h1 id="what-is-a-dot-product">What is a dot product?</h1>
<p>In mathematics the <em>dot product</em> is usually defined on vectors. Given two vectors of length \(n\), \((a_1, \dots, a_n)\) and \((b_1, \dots, b_n)\) the dot product is defined as:</p>
<p>\(a_1 b_1 + \dots + a_n b_n\)</p>
<p>or without the use of the pernicious “\(\dots\)”:</p>
<p>\(\sum_{i=1}^{n}{a_i b_i}\)</p>
<p>The implementation for lists is fairly straightforward.</p>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="ot">dot </span><span class="ot">::</span> <span class="kw">Num</span> a <span class="ot">=&gt;</span> [a] <span class="ot">-&gt;</span> [a] <span class="ot">-&gt;</span> a<br />dot xs ys <span class="fu">=</span> <span class="fu">foldl</span> (<span class="fu">+</span>) <span class="dv">0</span> (<span class="fu">zipWith</span> (<span class="fu">*</span>) xs ys)</code></pre>
<p>This definition will work just fine on two lists of different length, owing to the definition of <code>zipWith</code>.</p>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="fu">zipWith</span><span class="ot"> </span><span class="ot">::</span> (a <span class="ot">-&gt;</span> b <span class="ot">-&gt;</span> c) <span class="ot">-&gt;</span> [a] <span class="ot">-&gt;</span> [b] <span class="ot">-&gt;</span> [c]<br /><span class="fu">zipWith</span> f [] _ <span class="fu">=</span> []<br /><span class="fu">zipWith</span> f _ [] <span class="fu">=</span> []<br /><span class="fu">zipWith</span> f (x<span class="fu">:</span>xs) (y<span class="fu">:</span>ys) <span class="fu">=</span> f x y <span class="fu">:</span> <span class="fu">zipWith</span> f xs ys</code></pre>
<p>This is fine for lists but will become problematic later when we look at other data structures.</p>
<p>There is no reason that this definition shouldn’t be extended to other data structures such as \(n\)-dimensional arrays or even trees. Let’s look at how we might define dot products on trees.</p>
<h1 id="dot-product-on-trees">Dot product on <code>Tree</code>s</h1>
<p>We define trees as follows (however it does not really matter whether only the leaves contain numbers or whether branch nodes can too):</p>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="kw">data</span> <span class="dt">Tree</span> a <span class="fu">=</span> <span class="dt">Leaf</span> a <span class="fu">|</span> <span class="dt">Branch</span> (<span class="dt">Tree</span> a) (<span class="dt">Tree</span> a)</code></pre>
<p>For the sake of succinctness, I will represent trees using nested pairs denoted with curly braces. e.g. <code>Branch (Leaf 1) (Leaf 2)</code> becomes <code>{1,2}</code>, <code>Branch (Leaf 1) (Branch (Leaf 2) (Leaf 3))</code> becomes <code>{1,{2,3}}</code>.</p>
<p>What should be the dot product of <code>{1,{2,3}}</code> and <code>{4,{5,6}}</code>? A reasonable answer would be <code>1 * 4 + 2 * 5 + 3 * 6 == 32</code>. For each leaf in the first tree find the corresponding leaf in the second tree, multiply them together and then sum all the results together.</p>
<p>This definition relies on the two trees having the same shape. To see why let’s see if we can we define a function in the style of <code>zipWith</code> for trees. Unfortunately, this is problematic.</p>
<pre class="sourceCode"><code class="sourceCode haskell">zipWithT f (<span class="dt">Leaf</span> a) (<span class="dt">Leaf</span> b)           <span class="fu">=</span> <span class="dt">Leaf</span> (f a b)<br />zipWithT f (<span class="dt">Branch</span> s t) (<span class="dt">Branch</span> s' t') <span class="fu">=</span> <span class="dt">Branch</span> (zipWithT f s s')<br />                                                (zipWithT f t t')<br />zipWithT f (<span class="dt">Leaf</span> a) (<span class="dt">Branch</span> s' t')     <span class="fu">=</span> <span class="co">{- ? -}</span> <span class="fu">undefined</span><br />zipWithT f (<span class="dt">Branch</span> s t) (<span class="dt">Leaf</span> b)       <span class="fu">=</span> <span class="co">{- ? -}</span> <span class="fu">undefined</span></code></pre>
<p>There’s a problem with the last two cases. While I won’t go so far as to say that there is no definition we could provide, it’s clear that there are a number of choices that could be taken. In each case one needs to take an arbitrary element from the <code>Branch</code> argument and apply function <code>f</code> to it and the <code>Leaf</code> argument.</p>
<p>Even if there is a definition that makes reasonable sense can we say whether it’s possible to provide a <code>zipWith</code>-like definition for an arbitrary data structure?</p>
<p>An alternative is to modify our data structures to contain types that represent the <em>shape</em> of the data structure. We can then define <code>dot</code> such that it must take two arguments of exactly the same shape.</p>
<h1 id="data-structures-with-shapes">Data structures with shapes</h1>
<p>I’ll illustrate this approach with vectors first, before moving onto trees. Vectors are just lists with their length encode into their type.</p>
<h2 id="vectors">Vectors</h2>
<p>First, we add some essentials to the top of our module.</p>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="ot">{-# LANGUAGE GADTs, EmptyDataDecls, FlexibleInstances, DeriveFunctor, DeriveFoldable #-}</span><br /><span class="ot">{-# LANGUAGE ScopedTypeVariables, FlexibleContexts, UndecidableInstances #-}</span></code></pre>
<p>Now we define two new data types, <code>Z</code> and <code>S</code>, representing <a href="http://en.wikipedia.org/wiki/Peano_axioms">Peano numbers</a>. Both data types are empty since we will never be using their values.</p>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="kw">data</span> <span class="dt">Z</span><br /><span class="kw">data</span> <span class="dt">S</span> n</code></pre>
<p>Now vectors.</p>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="kw">infixr</span> <span class="dv">5</span> <span class="ot">`Cons`</span><br /><span class="kw">data</span> <span class="dt">Vec</span> n a <span class="kw">where</span><br />  <span class="dt">Nil</span><span class="ot">  </span><span class="ot">::</span> <span class="dt">Vec</span> <span class="dt">Z</span> a<br />  <span class="dt">Cons</span><span class="ot"> </span><span class="ot">::</span> a <span class="ot">-&gt;</span> <span class="dt">Vec</span> n a <span class="ot">-&gt;</span> <span class="dt">Vec</span> (<span class="dt">S</span> n) a</code></pre>
<p>If you haven’t seen these data types before it’s worth noting that you can define <em>total</em> (vs <em>partial</em>) versions of <code>head</code> and <code>tail</code>. Trying to take the head of an empty vector simply doesn’t type check.</p>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="ot">headVec </span><span class="ot">::</span> <span class="dt">Vec</span> (<span class="dt">S</span> n) a <span class="ot">-&gt;</span> a<br />headVec (<span class="dt">Cons</span> x _) <span class="fu">=</span> x<br /><br /><span class="ot">tailVec </span><span class="ot">::</span> <span class="dt">Vec</span> (<span class="dt">S</span> n) a <span class="ot">-&gt;</span> <span class="dt">Vec</span> n a<br />tailVec (<span class="dt">Cons</span> _ xs) <span class="fu">=</span> xs</code></pre>
<p>With can now define <code>zipWithV</code>.</p>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="ot">zipWithV </span><span class="ot">::</span> (a <span class="ot">-&gt;</span> b <span class="ot">-&gt;</span> c) <span class="ot">-&gt;</span> <span class="dt">Vec</span> n a <span class="ot">-&gt;</span> <span class="dt">Vec</span> n b <span class="ot">-&gt;</span> <span class="dt">Vec</span> n c<br />zipWithV f <span class="dt">Nil</span> <span class="dt">Nil</span> <span class="fu">=</span> <span class="dt">Nil</span><br />zipWithV f (<span class="dt">Cons</span> x xs) (<span class="dt">Cons</span> y ys) <span class="fu">=</span> f x y <span class="ot">`Cons`</span> zipWithV f xs ys</code></pre>
<p>Unfortunately, GHC’s type checker does not detect that a case such as the one below is impossible. (In fact, if your warnings are turned up high enough GHC will warn that two patterns are missing in the definition above.)</p>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="co">-- Although this pattern match is impossible GHC's type checker</span><br /><span class="co">-- won't complain</span><br />zipWithV f (<span class="dt">Cons</span> x xs) <span class="dt">Nil</span> <span class="fu">=</span> <span class="co">{- something -}</span> <span class="fu">undefined</span></code></pre>
<h2 id="trees">Trees</h2>
<p>The <em>length</em> of a tree is not quite a meaningful enough representation. Instead we represent its <em>shape</em> as a nested tuples of the unit (<code>()</code>) type.</p>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="kw">data</span> <span class="dt">Tree</span> sh a <span class="kw">where</span><br />  <span class="dt">Leaf</span><span class="ot">   </span><span class="ot">::</span> a <span class="ot">-&gt;</span> <span class="dt">Tree</span> () a<br />  <span class="dt">Branch</span><span class="ot"> </span><span class="ot">::</span> <span class="dt">Tree</span> m a <span class="ot">-&gt;</span> <span class="dt">Tree</span> n a <span class="ot">-&gt;</span> <span class="dt">Tree</span> (m,n) a</code></pre>
<p>For example:</p>
<pre class="sourceCode"><code class="sourceCode haskell">{<span class="dv">1</span>,{<span class="dv">2</span>,<span class="dv">3</span>}}<span class="ot"> </span><span class="ot">::</span> <span class="dt">Tree</span> ((),((),())) <span class="dt">Integer</span></code></pre>
<p>The new definition of <code>zipWithT</code> only differs in its type.</p>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="ot">zipWithT </span><span class="ot">::</span> (a <span class="ot">-&gt;</span> b <span class="ot">-&gt;</span> c) <span class="ot">-&gt;</span> <span class="dt">Tree</span> sh a <span class="ot">-&gt;</span> <span class="dt">Tree</span> sh b <span class="ot">-&gt;</span> <span class="dt">Tree</span> sh c<br />zipWithT f (<span class="dt">Leaf</span> a)     (<span class="dt">Leaf</span> b)       <span class="fu">=</span> <span class="dt">Leaf</span> (f a b)<br />zipWithT f (<span class="dt">Branch</span> s t) (<span class="dt">Branch</span> s' t') <span class="fu">=</span> <span class="dt">Branch</span> (zipWithT f s s')<br />                                                (zipWithT f t t')</code></pre>
<p>Now finish off the definitions:</p>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="ot">foldlT </span><span class="ot">::</span> (a <span class="ot">-&gt;</span> b <span class="ot">-&gt;</span> a) <span class="ot">-&gt;</span> a <span class="ot">-&gt;</span> <span class="dt">Tree</span> sh b <span class="ot">-&gt;</span> a<br />foldlT f z (<span class="dt">Leaf</span> a)     <span class="fu">=</span> f z a<br />foldlT f z (<span class="dt">Branch</span> s t) <span class="fu">=</span> foldlT f (foldlT f z s) t<br /><br /><span class="ot">dotT </span><span class="ot">::</span> <span class="kw">Num</span> a <span class="ot">=&gt;</span> <span class="dt">Tree</span> sh a <span class="ot">-&gt;</span> <span class="dt">Tree</span> sh a <span class="ot">-&gt;</span> a<br />dotT t1 t2 <span class="fu">=</span> foldlT (<span class="fu">+</span>) <span class="dv">0</span> (zipWithT (<span class="fu">*</span>) t1 t2</code></pre>
<h1 id="generalising-to-arbitrary-data-structures">Generalising to arbitrary data structures</h1>
<p>Any seasoned Haskell veteran knows the utility of type classes such as <code>Functor</code>, <code>Applicative</code>, and <code>Foldable</code>. We have now seen that a dot product is essentially a <code>zipWith</code> followed by a <code>fold</code>. (It makes little difference whether its a left or right fold).</p>
<p>Since <code>zipWith</code> is really just <code>liftA2</code> (found in module <code>Control.Applicative</code>) on the <code>ZipList</code> data structure. This leads us to the following definition:</p>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="ot">dot </span><span class="ot">::</span> (<span class="kw">Num</span> a, <span class="dt">Foldable</span> f, <span class="dt">Applicative</span> f) <span class="ot">=&gt;</span> f a <span class="ot">-&gt;</span> f a <span class="ot">-&gt;</span> a<br />dot x y <span class="fu">=</span> <span class="fu">foldl</span> (<span class="fu">+</span>) <span class="dv">0</span> (liftA2 (<span class="fu">*</span>) x y)</code></pre>
<p>This function requires instances for <code>Functor</code>, <code>Foldable</code> and <code>Applicative</code>. Given that instances for the first two type classes are both easy to write (and in some cases derivable using Haskell’s <code>deriving</code> syntax), I will only discuss <code>Applicative</code> instances in this post. (The instances for vectors and shape-encoded trees are left as an exercise for the reader.)</p>
<p>One might reasonably wonder, must the two arguments to <code>dot</code> have the same shape as before? It turns out that, yes, they do and for similar reasons. I’ll demonstrate the point by looking at how to define <code>Applicative</code> instances for lists, vectors and trees.</p>
<h2 id="lists">Lists</h2>
<p>The default <code>Applicative</code> instance for lists is unsuitable for a generic dot product. However, the <code>Applicative</code> instance on its wrapper type <a href="http://hackage.haskell.org/packages/archive/base/latest/doc/html/Control-Applicative.html#v:ZipList"><code>ZipList</code></a> is adequate but has an unsatisfying definition for <code>pure</code> (to say the least).</p>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="kw">instance</span> <span class="dt">Applicative</span> <span class="dt">ZipList</span> <span class="kw">where</span><br />  pure x <span class="fu">=</span> <span class="dt">ZipList</span> (<span class="fu">repeat</span> x)<br />  <span class="dt">ZipList</span> fs <span class="fu">&lt;*&gt;</span> <span class="dt">ZipList</span> xs <span class="fu">=</span> <span class="dt">ZipList</span> (<span class="fu">zipWith</span> <span class="fu">id</span> fs xs)</code></pre>
<p>Of course, this is necessary for lists since we can’t guarantee that two lists of the same length will be applied together. How else would you define <code>pure</code> to make it work on an arbitrary length lists <code>xs</code>?</p>
<pre class="sourceCode"><code class="sourceCode haskell">(<span class="fu">+</span>) <span class="fu">&lt;$&gt;</span> (pure <span class="dv">1</span>) <span class="fu">&lt;*&gt;</span> (<span class="dt">ZipList</span> xs)</code></pre>
<p>The definition of <code>pure</code> is much more satisfying for vectors.</p>
<h2 id="vectors-1">Vectors</h2>
<p>Obviously we want a similar definition for <code>pure</code> as for lists (<code>ZipList</code>). But we don’t want to produce an infinite list, just one of the appropriate length.</p>
<p>Defining the <code>Applicative</code> instance for vectors leads us to an interesting observation which holds true in general. For any data structure which encodes its own shape:</p>
<ol style="list-style-type: decimal">
<li>You need one instance of <code>Applicative</code> for each constructor of the data type.</li>
<li>The instance heads must mirror the types of the constructors.</li>
</ol>
<p>In the code below there are two instances and each instance head closely mirrors the data constructor’s type. e.g. <code>Cons :: a -&gt; Vec n a -&gt; Vec (S n) a</code> mirrors <code>instance Applicative (Vec n) =&gt; Applicative (Vec (S n))</code>.</p>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="kw">instance</span> <span class="dt">Applicative</span> (<span class="dt">Vec</span> <span class="dt">Z</span>) <span class="kw">where</span><br />  pure _                            <span class="fu">=</span> <span class="dt">Nil</span><br />  <span class="dt">Nil</span> <span class="fu">&lt;*&gt;</span> <span class="dt">Nil</span>                       <span class="fu">=</span> <span class="dt">Nil</span><br /><br /><span class="kw">instance</span> <span class="dt">Applicative</span> (<span class="dt">Vec</span> n) <span class="ot">=&gt;</span> <span class="dt">Applicative</span> (<span class="dt">Vec</span> (<span class="dt">S</span> n)) <span class="kw">where</span><br />  pure a                            <span class="fu">=</span> a <span class="ot">`Cons`</span> pure a<br />  (fa <span class="ot">`Cons`</span> fas) <span class="fu">&lt;*&gt;</span> (a <span class="ot">`Cons`</span> <span class="kw">as</span>) <span class="fu">=</span> fa a <span class="ot">`Cons`</span> (fas <span class="fu">&lt;*&gt;</span> <span class="kw">as</span>)</code></pre>
<p>That’s it. Function <code>pure</code> will produce a vector of just the right length.</p>
<h2 id="trees-1">Trees</h2>
<p>Unlike the case for lists, it’s hard to define an <code>Applicative</code> instance for non-shape-encoded trees. Let’s have a look.</p>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="kw">instance</span> <span class="dt">Applicative</span> <span class="dt">Tree</span> <span class="kw">where</span><br />  pure a <span class="fu">=</span> <span class="dt">Leaf</span> a<br />  (<span class="dt">Leaf</span> fa) <span class="fu">&lt;*&gt;</span> (<span class="dt">Leaf</span> b) <span class="fu">=</span> <span class="dt">Leaf</span> (fa b)<br />  (<span class="dt">Branch</span> fa fb) <span class="fu">&lt;*&gt;</span> (<span class="dt">Branch</span> a b) <span class="fu">=</span> <span class="dt">Branch</span> (fa <span class="fu">&lt;*&gt;</span> a) (fb <span class="fu">&lt;*&gt;</span> b)<br />  (<span class="dt">Leaf</span> fa) <span class="fu">&lt;*&gt;</span> (<span class="dt">Branch</span> a b) <span class="fu">=</span> <span class="co">{- ? -}</span> <span class="fu">undefined</span><br />  (<span class="dt">Branch</span> fa fb) <span class="fu">&lt;*&gt;</span> (<span class="dt">Leaf</span> a) <span class="fu">=</span> <span class="co">{- ? -}</span> <span class="fu">undefined</span></code></pre>
<p>This problem has been <a href="http://www.haskell.org/pipermail/beginners/2010-March/003856.html">noticed</a> before on the <tt>Haskell-beginners</tt> mailing list. The <a href="http://www.haskell.org/pipermail/beginners/2010-March/003857.html">response</a> is interesting because it mentions the “unpleasant property of returning infinite tree[s]”; the same problem we had with lists!</p>
<p>With shape-encoded trees this is not a problem. Function <code>pure</code> produces a tree of the appropriate shape. Also, note how the head of the second instance mirrors the definition of the <code>Branch</code> constructor (:: Tree m a -&gt; Tree n a -&gt; Tree (m,n) a)</p>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="kw">instance</span> <span class="dt">Applicative</span> (<span class="dt">Tree</span> ()) <span class="kw">where</span><br />  pure a                          <span class="fu">=</span> <span class="dt">Leaf</span> a<br />  <span class="dt">Leaf</span> fa <span class="fu">&lt;*&gt;</span> <span class="dt">Leaf</span> a              <span class="fu">=</span> <span class="dt">Leaf</span> (fa a)<br /><br /><span class="kw">instance</span> (<span class="dt">Applicative</span> (<span class="dt">Tree</span> m), <span class="dt">Applicative</span> (<span class="dt">Tree</span> n))<br />         <span class="ot">=&gt;</span> <span class="dt">Applicative</span> (<span class="dt">Tree</span> (m,n)) <span class="kw">where</span><br />  pure a                          <span class="fu">=</span> <span class="dt">Branch</span> (pure a) (pure a)<br />  (<span class="dt">Branch</span> fs ft) <span class="fu">&lt;*&gt;</span> (<span class="dt">Branch</span> s t) <span class="fu">=</span> <span class="dt">Branch</span> (fs <span class="fu">&lt;*&gt;</span> s) (ft <span class="fu">&lt;*&gt;</span> t)</code></pre>
<h1 id="arbitrary-binary-associative-operators.">Arbitrary binary associative operators.</h1>
<p>Phew, that’s it. We now have an implementation for <code>dot</code> that will work on an arbitrary data structure as long as one can define <code>Functor</code>, <code>Foldable</code> and <code>Applicative</code> instances. We have also learned that it is a good idea to encode the data structure’s shape in its type so that <code>Applicative</code> instances can be defined. (This will be important later on when we want to take the transpose of generic matrices, but I’m getting ahead of myself.)</p>
<p>But what if you want to use binary associative operators other than addition and multiplication for the dot product? This is easy using Haskell’s <code>Monoid</code> type class, and it plays nicely with the <code>Foldable</code> type class. In fact, it allows us to omit any mention of identity elements using the method <code>fold:: (Foldable t, Monoid m) =&gt; t m -&gt; m</code>. We define an even more generic dot product as follows:</p>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="ot">dotGen </span><span class="ot">::</span> (<span class="dt">Foldable</span> f, <span class="dt">Applicative</span> f, <span class="dt">Monoid</span> p, <span class="dt">Monoid</span> s)<br />       <span class="ot">=&gt;</span> (a <span class="ot">-&gt;</span> p, p <span class="ot">-&gt;</span> a) <span class="ot">-&gt;</span> (a <span class="ot">-&gt;</span> s, s<span class="ot">-&gt;</span> a) <span class="ot">-&gt;</span> f a <span class="ot">-&gt;</span> f a <span class="ot">-&gt;</span> a<br />dotGen (pinject, pproject) (sinject, sproject) x y <span class="fu">=</span><br />   sproject <span class="fu">.</span> fold <span class="fu">.</span> <span class="fu">fmap</span> (sinject <span class="fu">.</span> pproject) <span class="fu">$</span> liftA2 mappend px py<br />  <span class="kw">where</span><br />    px <span class="fu">=</span> <span class="fu">fmap</span> pinject x<br />    py <span class="fu">=</span> <span class="fu">fmap</span> pinject y</code></pre>
<p>This function takes two pairs of functions for injecting into and projecting from monoids. We can then define our original <code>dot</code> function using the existing <code>Sum</code> and <code>Product</code> wrapper types.</p>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="ot">dot </span><span class="ot">::</span> (<span class="kw">Num</span> a, <span class="dt">Foldable</span> f, <span class="dt">Applicative</span> f) <span class="ot">=&gt;</span> f a <span class="ot">-&gt;</span> f a <span class="ot">-&gt;</span> a<br />dot <span class="fu">=</span> dotGen (<span class="dt">Product</span>, getProduct) (<span class="dt">Sum</span>, getSum)</code></pre>
<h1 id="in-the-next-episode...">In the next episode…</h1>
<p>In my next post we will consider <em>generic matrix multiplication</em>. This operation is defined over arbitrary collections of collections of numbers and, naturally, makes use of our generic dot product. Until then, adios.</p>
<h1 id="slides">Slides</h1>
<p>On 17 Nov 2011 I gave a talk at <a href="http://fp-syd.ouroborus.net/">fp-syd</a> about this work. You can find the slides <a href="/static/files/generic-dot-products.pdf">here</a>.</p>]]></description>
    <pubDate>Mon, 27 Jun 2011 00:00:00 UT</pubDate>
    <guid>http://lambdalog.seanseefried.com/posts/2011-06-27-generic-dot-products.html</guid>
</item>
<item>
    <title>Haskell GADTs in Scala</title>
    <link>http://lambdalog.seanseefried.com/posts/2011-11-22-gadts-in-scala.html</link>
    <description><![CDATA[<!-- 
I've had some really cool ideas for this post. Wouldn't it be cool if all the code
I wrote in this module were actually real code? Wouldn't it be cool if I could insert
expressions that one might put into GHCi and have that execute and be inserted into the
post?

Anything to be compiled should be in the follow tags.

~~~{.haskell .code}
~~~

Parameters to the compiler should be in a HTML comment and look something like:

ghc-params: --make

-->

<p>This is an updated version of an earlier post. Owing to a comment by Jed Wesley-Smith I restructured this post somewhat to introduce two techniques for programming with <a href="gadts">GADTs</a> in <a href="scala">Scala</a>. Thanks also go to <a href="http://blog.tmorris.net/">Tony Morris</a>.</p>
<p>First we’ll start with a fairly canonical example of why GADTs are useful in Haskell.</p>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="ot">{-# LANGUAGE GADTs #-}</span><br /><span class="kw">module</span> <span class="dt">Exp</span> <span class="kw">where</span><br /><br /><span class="kw">data</span> <span class="dt">Exp</span> a <span class="kw">where</span><br />  <span class="dt">LitInt</span><span class="ot">  </span><span class="ot">::</span> <span class="dt">Int</span>                        <span class="ot">-&gt;</span> <span class="dt">Exp</span> <span class="dt">Int</span><br />  <span class="dt">LitBool</span><span class="ot"> </span><span class="ot">::</span> <span class="dt">Bool</span>                       <span class="ot">-&gt;</span> <span class="dt">Exp</span> <span class="dt">Bool</span><br />  <span class="dt">Add</span><span class="ot">     </span><span class="ot">::</span> <span class="dt">Exp</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">Exp</span> <span class="dt">Int</span>         <span class="ot">-&gt;</span> <span class="dt">Exp</span> <span class="dt">Int</span><br />  <span class="dt">Mul</span><span class="ot">     </span><span class="ot">::</span> <span class="dt">Exp</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">Exp</span> <span class="dt">Int</span>         <span class="ot">-&gt;</span> <span class="dt">Exp</span> <span class="dt">Int</span><br />  <span class="dt">Cond</span><span class="ot">    </span><span class="ot">::</span> <span class="dt">Exp</span> <span class="dt">Bool</span> <span class="ot">-&gt;</span> <span class="dt">Exp</span> a <span class="ot">-&gt;</span> <span class="dt">Exp</span> a <span class="ot">-&gt;</span> <span class="dt">Exp</span> a<br />  <span class="dt">EqE</span><span class="ot">     </span><span class="ot">::</span> <span class="kw">Eq</span> a <span class="ot">=&gt;</span> <span class="dt">Exp</span> a <span class="ot">-&gt;</span> <span class="dt">Exp</span> a     <span class="ot">-&gt;</span> <span class="dt">Exp</span> <span class="dt">Bool</span><br /><br /><span class="ot">eval </span><span class="ot">::</span> <span class="dt">Exp</span> a <span class="ot">-&gt;</span> a<br />eval e <span class="fu">=</span> <span class="kw">case</span> e <span class="kw">of</span><br />  <span class="dt">LitInt</span> i       <span class="ot">-&gt;</span> i<br />  <span class="dt">LitBool</span> b      <span class="ot">-&gt;</span> b<br />  <span class="dt">Add</span> e e'       <span class="ot">-&gt;</span> eval e <span class="fu">+</span> eval e'<br />  <span class="dt">Mul</span> e e'       <span class="ot">-&gt;</span> eval e <span class="fu">*</span> eval e'<br />  <span class="dt">Cond</span> b thn els <span class="ot">-&gt;</span> <span class="kw">if</span> eval b <span class="kw">then</span> eval thn <span class="kw">else</span> eval els<br />  <span class="dt">EqE</span> e e'       <span class="ot">-&gt;</span> eval e <span class="fu">==</span> eval e'</code></pre>
<p>Here we have defined a data structure that represents the abstract syntax tree (AST) of a very simple arithmetic language. Notice that it ensures terms are well-typed. For instance something like the following just doesn’t type check.</p>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="dt">LitInt</span> <span class="dv">1</span> <span class="ot">`Add`</span> <span class="dt">LitBool</span> <span class="kw">True</span> <span class="co">-- this expression does not type check</span></code></pre>
<p>I have also provided a function <code>eval</code> that evaluates terms in this language.</p>
<p>In Scala it is quite possible to define data structures which have the same properties as a GADT declaration in Haskell. You can do this with <em>case classes</em> as follows.</p>
<pre class="sourceCode"><code class="sourceCode scala"><span class="kw">abstract</span> <span class="kw">class</span> Exp[A] <br /><br /><span class="kw">case</span> <span class="kw">class</span> <span class="fu">LitInt</span>(i: Int)                                     <span class="kw">extends</span> Exp[Int]<br /><span class="kw">case</span> <span class="kw">class</span> <span class="fu">LitBool</span>(b: Boolean)                                <span class="kw">extends</span> Exp[Boolean]<br /><span class="kw">case</span> <span class="kw">class</span> <span class="fu">Add</span>(e1: Exp[Int], e2: Exp[Int])                    <span class="kw">extends</span> Exp[Int]<br /><span class="kw">case</span> <span class="kw">class</span> <span class="fu">Mul</span>(e1: Exp[Int], e2: Exp[Int])                    <span class="kw">extends</span> Exp[Int]<br /><span class="kw">case</span> <span class="kw">class</span> Cond[A](b: Exp[Boolean], thn: Exp[A], els: Exp[A]) <span class="kw">extends</span> Exp[A]<br /><span class="kw">case</span> <span class="kw">class</span> Eq[A](e1: Exp[A], e2: Exp[A])                      <span class="kw">extends</span> Exp[Boolean]</code></pre>
<p>But how do we implement <code>eval</code>. You might think that the following code would work. I mean, it looks like the Haskell version, right?</p>
<pre class="sourceCode"><code class="sourceCode scala"><span class="kw">abstract</span> <span class="kw">class</span> Exp[A] {<br />  <span class="kw">def</span> eval = <span class="kw">this</span> <span class="kw">match</span> {<br />    <span class="kw">case</span> <span class="fu">LitInt</span>(i)       =&gt; i<br />    <span class="kw">case</span> <span class="fu">LitBool</span>(b)      =&gt; b<br />    <span class="kw">case</span> <span class="fu">Add</span>(e1, e2)     =&gt; e1.<span class="fu">eval</span> + e2.<span class="fu">eval</span><br />    <span class="kw">case</span> <span class="fu">Mul</span>(e1, e2)     =&gt; e1.<span class="fu">eval</span> * e2.<span class="fu">eval</span><br />    <span class="kw">case</span> <span class="fu">Cond</span>(b,thn,els) =&gt; <span class="kw">if</span> ( b.<span class="fu">eval</span> ) { thn.<span class="fu">eval</span> } <span class="kw">else</span> { els.<span class="fu">eval</span> }<br />    <span class="kw">case</span> <span class="fu">Eq</span>(e1,e2)       =&gt; e1.<span class="fu">eval</span> == e2.<span class="fu">eval</span><br />  }<br /><br />}<br /><br /><span class="kw">case</span> <span class="kw">class</span> <span class="fu">LitInt</span>(i: Int)                                     <span class="kw">extends</span> Exp[Int]<br /><span class="kw">case</span> <span class="kw">class</span> <span class="fu">LitBool</span>(b: Boolean)                                <span class="kw">extends</span> Exp[Boolean]<br /><span class="kw">case</span> <span class="kw">class</span> <span class="fu">Add</span>(e1: Exp[Int], e2: Exp[Int])                    <span class="kw">extends</span> Exp[Int]<br /><span class="kw">case</span> <span class="kw">class</span> <span class="fu">Mul</span>(e1: Exp[Int], e2: Exp[Int])                    <span class="kw">extends</span> Exp[Int]<br /><span class="kw">case</span> <span class="kw">class</span> Cond[A](b: Exp[Boolean], thn: Exp[A], els: Exp[A]) <span class="kw">extends</span> Exp[A]<br /><span class="kw">case</span> <span class="kw">class</span> Eq[A](e1: Exp[A], e2: Exp[A])                      <span class="kw">extends</span> Exp[Boolean]</code></pre>
<p>Unfortunately for us, this doesn’t work. The Scala compiler is unable to instantiate the type <code>Exp[A]</code> to more specific ones (such as <code>LitInt</code> which extends <code>Exp[Int]</code>)</p>
<pre><code>3: constructor cannot be instantiated to expected type;
  found   : FailedExp.LitInt
  required: FailedExp.Exp[A]
    case LitInt(i)       =&gt; i
        ^
</code></pre>
<p>There are two solutions to this problem.</p>
<h1 id="solution-1-the-object-oriented-way">Solution 1: The object-oriented way</h1>
<p>You must write <code>eval</code> the object-oriented way. The definition of <code>eval</code> gets spread over each of the sub-classes of <code>Exp[A]</code>.</p>
<pre class="sourceCode"><code class="sourceCode scala"><span class="kw">abstract</span> <span class="kw">class</span> Exp[A] {<br />  <span class="kw">def</span> eval: A<br />}<br /><br /><span class="kw">case</span> <span class="kw">class</span> <span class="fu">LitInt</span>(i: Int)                                     <span class="kw">extends</span> Exp[Int] {<br />  <span class="kw">def</span> eval = i<br />}<br /><br /><span class="kw">case</span> <span class="kw">class</span> <span class="fu">LitBool</span>(b: Boolean)                                <span class="kw">extends</span> Exp[Boolean] {<br />  <span class="kw">def</span> eval = b<br />}<br /><br /><span class="kw">case</span> <span class="kw">class</span> <span class="fu">Add</span>(e1: Exp[Int], e2: Exp[Int])                    <span class="kw">extends</span> Exp[Int] {<br />  <span class="kw">def</span> eval = e1.<span class="fu">eval</span> + e2.<span class="fu">eval</span><br />}<br /><span class="kw">case</span> <span class="kw">class</span> <span class="fu">Mul</span>(e1: Exp[Int], e2: Exp[Int])                    <span class="kw">extends</span> Exp[Int] {<br />  <span class="kw">def</span> eval = e1.<span class="fu">eval</span> * e2.<span class="fu">eval</span><br />}<br /><span class="kw">case</span> <span class="kw">class</span> Cond[A](b: Exp[Boolean], thn: Exp[A], els: Exp[A]) <span class="kw">extends</span> Exp[A] {<br />  <span class="kw">def</span> eval = <span class="kw">if</span> ( b.<span class="fu">eval</span> ) { thn.<span class="fu">eval</span> } <span class="kw">else</span> { els.<span class="fu">eval</span> }<br />}<br /><span class="kw">case</span> <span class="kw">class</span> Eq[A](e1: Exp[A], e2: Exp[A])                      <span class="kw">extends</span> Exp[Boolean] {<br />  <span class="kw">def</span> eval = e1.<span class="fu">eval</span> == e2.<span class="fu">eval</span><br />}</code></pre>
<h1 id="solution-2-the-functional-haskell-like-way">Solution 2: The functional Haskell-like way</h1>
<p>Personally I don’t like the OO style as much as the Haskell-like style. However, it turns out that you can program in that style by using a companion object.</p>
<pre class="sourceCode"><code class="sourceCode scala"><span class="kw">object</span> Exp {<br />  <span class="kw">def</span> evalAny[A](e: Exp[A]): A = e <span class="kw">match</span> {<br />    <span class="kw">case</span> <span class="fu">LitInt</span>(i)         =&gt; i<br />    <span class="kw">case</span> <span class="fu">LitBool</span>(b)        =&gt; b<br />    <span class="kw">case</span> <span class="fu">Add</span>(e1, e2)       =&gt; e1.<span class="fu">eval</span> + e2.<span class="fu">eval</span><br />    <span class="kw">case</span> <span class="fu">Mul</span>(e1, e2)       =&gt; e1.<span class="fu">eval</span> * e2.<span class="fu">eval</span><br />    <span class="kw">case</span> <span class="fu">Cond</span>(b, thn, els) =&gt; <span class="kw">if</span> (b.<span class="fu">eval</span>) { thn.<span class="fu">eval</span> } <span class="kw">else</span> { els.<span class="fu">eval</span> }<br />    <span class="kw">case</span> <span class="fu">Eq</span>(e1, e2)        =&gt; e1.<span class="fu">eval</span> == e2.<span class="fu">eval</span><br />  }<br />}<br /><br /><span class="kw">abstract</span> <span class="kw">class</span> Exp[A] {<br />  <span class="kw">def</span> eval: A = Exp.<span class="fu">evalAny</span>(<span class="kw">this</span>)<br />}<br /><br /><span class="kw">case</span> <span class="kw">class</span> <span class="fu">LitInt</span>(i: Int)                                     <span class="kw">extends</span> Exp[Int]<br /><span class="kw">case</span> <span class="kw">class</span> <span class="fu">LitBool</span>(b: Boolean)                                <span class="kw">extends</span> Exp[Boolean]<br /><span class="kw">case</span> <span class="kw">class</span> <span class="fu">Add</span>(e1: Exp[Int], e2: Exp[Int])                    <span class="kw">extends</span> Exp[Int]<br /><span class="kw">case</span> <span class="kw">class</span> <span class="fu">Mul</span>(e1: Exp[Int], e2: Exp[Int])                    <span class="kw">extends</span> Exp[Int]<br /><span class="kw">case</span> <span class="kw">class</span> Cond[A](b: Exp[Boolean], thn: Exp[A], els: Exp[A]) <span class="kw">extends</span> Exp[A]<br /><span class="kw">case</span> <span class="kw">class</span> Eq[A](e1: Exp[A], e2: Exp[A])                      <span class="kw">extends</span> Exp[Boolean]</code></pre>
<p>Ah, much better. But why does this work when the previous style doesn’t? The problem is that the constructors are not polymorphic. In Haskell-speak the type is:</p>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="dt">LitInt</span><span class="ot"> </span><span class="ot">::</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">Exp</span> <span class="dt">Int</span></code></pre>
<p>not</p>
<pre class="sourceCode"><code class="sourceCode haskell"><span class="dt">LitInt</span><span class="ot"> </span><span class="ot">::</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">Exp</span> a</code></pre>
<p>The second solution is subtly different. Method <code>evalAny</code> is polymorphic but its type is instantiated to that of the value of whatever it is called on. For instance <code>evalAny</code> when called on <code>LitInt(42)</code> equates type variable <code>A</code> with <code>Int</code>. It can then correctly deduce that it does indeed take a value of <code>Exp[Int]</code> and produce a value of <code>Int</code>.</p>
<!--
~~~{.haskell}
~~~
-->

<!--
~~~{.scala}
Here's some scala code
~~~
-->




]]></description>
    <pubDate>Tue, 22 Nov 2011 00:00:00 UT</pubDate>
    <guid>http://lambdalog.seanseefried.com/posts/2011-11-22-gadts-in-scala.html</guid>
</item>

    </channel> 
</rss>
