<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Martijn's Journal</title>
	
	<link>http://martijn.van.steenbergen.nl/journal</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Thu, 01 Jul 2010 23:07:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/MedeaMelana" /><feedburner:info uri="medeamelana" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Master’s Diploma</title>
		<link>http://feedproxy.google.com/~r/MedeaMelana/~3/zoOuOTdIHzQ/</link>
		<comments>http://martijn.van.steenbergen.nl/journal/2010/07/02/masters-diploma/#comments</comments>
		<pubDate>Thu, 01 Jul 2010 23:04:51 +0000</pubDate>
		<dc:creator>Martijn</dc:creator>
				<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Life]]></category>

		<guid isPermaLink="false">http://martijn.van.steenbergen.nl/journal/?p=501</guid>
		<description><![CDATA[
Today Chris Eidhof, Sebastiaan Visser and I got our master&#8217;s diplomas, all on Haskell-related generic programming subjects. The diploma speeches, given by Andres Löh, Johan Jeuring, and José Pedro Magalhães, were very flattering.   The picture above, taken by my sister Tamar, shows yours truly signing his diploma.
Next week I start working full-time at [...]]]></description>
			<content:encoded><![CDATA[<img class="dbg_single" src="http://martijn.van.steenbergen.nl/galleries/singles/DSC_1675.jpg" alt="DSC_1675.jpg" />
<p>Today Chris Eidhof, Sebastiaan Visser and I got our master&#8217;s diplomas, all on Haskell-related generic programming subjects. The diploma speeches, given by Andres Löh, Johan Jeuring, and José Pedro Magalhães, were very flattering. <img src='http://martijn.van.steenbergen.nl/journal/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  The picture above, taken by my sister Tamar, shows yours truly signing his diploma.</p>
<p>Next week I start working full-time at <a href="http://q42.nl/">Q42</a> in The Hague. I hope to move a bit closer to work somewhere in the next few months. Right now I spend over 3 hours travelling each day to get to and from work; that has to change. <img src='http://martijn.van.steenbergen.nl/journal/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<img src="http://feeds.feedburner.com/~r/MedeaMelana/~4/zoOuOTdIHzQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://martijn.van.steenbergen.nl/journal/2010/07/02/masters-diploma/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://martijn.van.steenbergen.nl/journal/2010/07/02/masters-diploma/</feedburner:origLink></item>
		<item>
		<title>Generically adding position information to a datatype</title>
		<link>http://feedproxy.google.com/~r/MedeaMelana/~3/TMe9drEi_1E/</link>
		<comments>http://martijn.van.steenbergen.nl/journal/2010/06/24/generically-adding-position-information-to-a-datatype/#comments</comments>
		<pubDate>Thu, 24 Jun 2010 12:06:31 +0000</pubDate>
		<dc:creator>Martijn</dc:creator>
				<category><![CDATA[Haskell]]></category>

		<guid isPermaLink="false">http://martijn.van.steenbergen.nl/journal/?p=482</guid>
		<description><![CDATA[Every now and then datatype fixpoints come up, especially in the context of generic programming:

newtype Fix f = In { out :: f (Fix f) }

Most explanations of this datatype I have read or heard start with this definition and then proceed to explain it, using various examples. In today&#8217;s post I will also introduce [...]]]></description>
			<content:encoded><![CDATA[<p>Every now and then datatype fixpoints come up, especially in the context of generic programming:</p>
<pre>
newtype Fix f = In { out :: f (Fix f) }
</pre>
<p>Most explanations of this datatype I have read or heard start with this definition and then proceed to explain it, using various examples. In today&#8217;s post I will also introduce you to this datatype, but I want to take a different approach: I will show you a problem to which the Fix datatype is the natural solution, <em>deriving</em> its definition along the way.</p>
<p>The Haskell code in this post does not use very advanced features: there are no type functions or even type classes, only datatypes and parameters. If you are familiar with datatypes, type parameters and their syntax, it should not be hard to follow. If you have any questions, feel free to post them!</p>
<h2>The problem</h2>
<p>Let&#8217;s take our trusty old friend the arithmetic expression datatype:</p>
<pre>
data BareExpr
  = Num Int
  | Add BareExpr BareExpr
  | Sub BareExpr BareExpr
  | Mul BareExpr BareExpr
  | Div BareExpr BareExpr
</pre>
<p>I&#8217;ve called it <code>BareExpr</code> here for a reason: we are going to change it in such a way that we can also store position information in it, resulting in type <code>PosExpr</code>, so that when a <code>PosExpr</code> is produced by a parser, we can trace back where in the original source code the tree nodes were. This is useful in various applications. For example, compilers that output error messages generally provide position information about where the error occurred exactly. It is also useful in tools that need to understand text selections in the source code, such as editors that feature refactoring.</p>
<p>Adding position information to a single datatype is not very difficult. After we have done so for <code>BareExpr</code>, we will look at the real problem: how to do this for <em>any</em> datatype.</p>
<h2>Expressions with positions</h2>
<p>There are several ways to add position information to a datatype. In our case we will couple every <em>occurrence</em> of <code>BareExpr</code> with a location. Let&#8217;s call the type of locations <code>SrcSpan</code> and the annotated version of the expression datatype <code>PosExpr</code>:</p>
<pre>
type PosExpr = (SrcSpan, PosExpr’)
data PosExpr'
  = Num Intr’
  | Add PosExpr PosExpr
  | Sub PosExpr PosExpr
  | Mul PosExpr PosExpr
  | Div PosExpr PosExpr
</pre>
<p>In a series of steps, we will reach our final solution.</p>
<h2>Step 1: Capture the similarities between BareExpr and PosExpr</h2>
<p><code>BareExpr</code> and <code>PosExpr'</code> are very similar: they both contain five constructors, and each constructor has the same number of fields. Can we capture this structure somehow? Yes, we can: the two types only differ in the types of their recursive positions, and in a very regular way. We can do here what we would do in any similar case: make the parts that differ arguments, and then express the original entities in terms of this new, general entity by providing specific arguments.</p>
<p>Haskell allows us to do that with datatypes: simply introduce a new type argument <code>r</code>. We call the resulting type <code>ExprF</code>:</p>
<pre>
data ExprF r
  = Num Int
  | Add r r
  | Sub r r
  | Mul r r
  | Div r r
</pre>
<p>The <code>F</code> in <code>ExprF</code> stands for functor, and such a datatype is usually called a <em>base functor</em>. Base functors determine the shape of the top level of a tree, but the shape of their children is determined by the type argument.</p>
<h2>Step 2: Express BareExpr in terms of ExprF</h2>
<p>Now we need to recover <code>BareExpr</code> and <code>PosExpr</code> by expressing them in terms of <code>ExprF</code>. For <code>BareExpr</code>, we want the child positions of <code>ExprF</code> also to be bare expressions. This leads to an infinite type:</p>
<pre>
BareExpr ~ ExprF (ExprF (ExprF ...))
</pre>
<p>This says that to get bare expressions back, we want to take <code>ExprF</code> and have its children be <code>ExprF</code>s again, and those children&#8217;s children to be <code>ExprF</code>s again, and so on. In Haskell we can encode infinite types by introducing new datatypes (we reuse the name <code>BareExpr</code> here):</p>
<pre>
newtype BareExpr = BareExpr { runBareExpr :: ExprF BareExpr }
</pre>
<p>If you repeatedly expand this definition, you will see that it results in the infinite type above.</p>
<h2>Step 3: Express PosExpr in terms of ExprF</h2>
<p>For <code>PosExpr</code> we can think of a similar infinite type:</p>
<pre>
PosExpr ~ (SrcSpan, ExprF (SrcSpan, ExprF ...))
</pre>
<p>Again, we write this down using a new datatype:</p>
<pre>
newtype PosExpr = PosExpr  { runPosExpr  :: (SrcSpan, ExprF PosExpr) }
</pre>
<h2>Step 4: Generalize BareExpr</h2>
<p>Currently <code>BareExpr</code> works only for the <code>ExprF</code> shape. Let&#8217;s create such a &#8216;bare&#8217; version for any shape instead of just <code>ExprF</code>s. We can do this by making the base functor an argument:</p>
<pre>
newtype BareExpr f = BareExpr { runBareExpr :: f (BareExpr f) }
</pre>
<p>On the right-hand side, we have replaced <code>ExprF</code> by the argument <code>f</code>. In step 2 we supplied the type we were defining as argument to <code>ExprF</code>; in this new version we do the same to <code>f</code>, but since this new version has a type argument, we need to supply this argument in the recursive position as well.</p>
<p>But&#8230; this datatype is no longer specific for arithmetic expressions, so the name <code>BareExpr</code> is not very appropriate. In fact, the type we have just defined is the famous <code>Fix</code> disguised under a different name!</p>
<pre>
newtype Fix f = In { out :: f (Fix f) }
</pre>
<p>So now you know what <code>Fix</code> does: it takes a base functor, such as <code>ExprF</code>, and recursively applies it to itself, creating a tree that is of the same shape at every level.</p>
<p>Our new definition of <code>BareExpr</code> doesn&#8217;t need to introduce any new datatypes but can now be a simple type synonym:</p>
<pre>
type BareExpr = Fix ExprF
</pre>
<h2>Step 5: Generalize PosExpr</h2>
<p>For <code>PosExpr</code> we can make two generalizations. The first is to not just store source locations, but allow any type of annotation:</p>
<pre>
newtype AnnExpr x = AnnExpr { runAnnExpr :: (x, ExprF (AnnExpr x)) }
</pre>
<p>The second is similar to the one we made to <code>BareExpr</code>: have it work for any base functor instead of just <code>ExprF</code>s:</p>
<pre>
newtype AnnFix x f = AnnFix { runAnnFix	:: (x, f (AnnFix x f)) }
</pre>
<p>To recover <code>PosExpr</code>, we give <code>AnnFix</code> the two appropriate type arguments:</p>
<pre>
type PosExpr = AnnFix SrcSpan ExprF
</pre>
<h2>Step 6: Express AnnFix in terms of Fix</h2>
<p>The <code>Fix</code> type captured the idea of take a functor and applying it to itself recursively. <code>AnnFix</code> does something similar. Can we perhaps express <code>AnnFix</code> in terms of <code>Fix</code> to make this explicit?</p>
<p>It turns out we can, if we introduce a helper datatype <code>Ann</code>:</p>
<pre>
data Ann x f a = Ann x (f a)
type AnnFix x f	= Fix (Ann x f)
</pre>
<p><code>Ann</code> couples an annotation <code>x</code> with a functor value. It&#8217;s kind of a tuple type, lifted to a higher order on the right side.</p>
<h2>Summing up</h2>
<p>We have seen many (intermediate) definitions of datatypes, but in the end only two of them matter:</p>
<pre>
newtype Fix f     = In { out :: f (Fix f) }
data    Ann x f a = Ann x (f a)
</pre>
<p>And of course, we have our expression example expressed in terms of these two building blocks:</p>
<pre>
type BareExpr = Fix ExprF
type PosExpr  = Fix (Ann SrcSpan ExprF)
</pre>
<p>With just these two building blocks, we can express generically annotated trees and unannotated trees. What is the point of generalizing this far? Well, by making these types not specific to a particular tree shape (such as <code>ExprF</code>), you can build all sorts of tools that work on many kinds of trees. In <a href="http://martijn.van.steenbergen.nl/projects/Selections.pdf">my Masters thesis</a> I explore this concept further, developing parser combinators that automatically insert the position information for you at the appropriate places, catamorphisms that automatically couple errors with position information, conversions between text selections and tree selections and a couple of other things.</p>
<h2>Read more</h2>
<p>If you&#8217;re interested in datatype fixpoints and would like to know more, here is a collection of interesting tutorials, applications and papers:</p>
<ul>
<li>Mark Dominus explains <a href="http://blog.plover.com/prog/springschool95-2.html">data Mu f = In (f (Mu f)) </a></il>
<li><a href="http://www.haskell.org/haskellwiki/Indirect_composite">Indirect composite</a> on the Haskell wiki</li>
<li>A <a href="http://knol.google.com/k/edward-kmett/catamorphisms">more formal description</a> by Edward Kmett</li>
<li>The classic 1990 <a href="http://homepages.inf.ed.ac.uk/wadler/papers/free-rectypes/free-rectypes.txt">Recursive types for free!</a> by Philip Wadler
<li>Another classic: the 1991 <a href="http://wwwhome.cs.utwente.nl/~fokkinga/mmf91m.ps">Functional Programming with Bananas, Lenses, Envelopes and Barbed Wire</a> by Erik Meijer, Maarten Fokkinga and Ross Paterson
</ul>
<img src="http://feeds.feedburner.com/~r/MedeaMelana/~4/TMe9drEi_1E" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://martijn.van.steenbergen.nl/journal/2010/06/24/generically-adding-position-information-to-a-datatype/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://martijn.van.steenbergen.nl/journal/2010/06/24/generically-adding-position-information-to-a-datatype/</feedburner:origLink></item>
		<item>
		<title>Üetliberg</title>
		<link>http://feedproxy.google.com/~r/MedeaMelana/~3/cu06mHLKVng/</link>
		<comments>http://martijn.van.steenbergen.nl/journal/2010/03/20/uetliberg/#comments</comments>
		<pubDate>Sat, 20 Mar 2010 10:43:29 +0000</pubDate>
		<dc:creator>Martijn</dc:creator>
				<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Trips]]></category>

		<guid isPermaLink="false">http://martijn.van.steenbergen.nl/journal/?p=479</guid>
		<description><![CDATA[Erik, Tom, Sebas, Chris, Sjoerd and I are in Zürich, attending the Haskell Hackathon. Yesterday we climbed the Üetliberg, a mountain (hill?) right next to the city. The view from there is spectacular; we&#8217;re especially liking the snow-topped mountains in the background.
Right now we&#8217;re all in the Google HQ, enjoying hacking in a spacious room [...]]]></description>
			<content:encoded><![CDATA[<p>Erik, Tom, Sebas, Chris, Sjoerd and I are in Zürich, attending the <a href="http://www.haskell.org/haskellwiki/ZuriHac">Haskell Hackathon</a>. Yesterday we climbed the Üetliberg, a mountain (hill?) right next to the city. The view from there is spectacular; we&#8217;re especially liking the snow-topped mountains in the background.</p>
<p>Right now we&#8217;re all in the Google HQ, enjoying hacking in a spacious room with a fridge full of drinks. Thanks, Google!</p>
<table cellspacing="0" cellpadding="0" class="dbg_gallery">
<tr>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/zurich-uetliberg/DSC_0640.jpg" rel="lightbox[zurich-uetliberg]" title="Erik, Tom, Sebas, Chris and Sjoerd.">
<img src="http://martijn.van.steenbergen.nl/thumbnails/zurich-uetliberg/DSC_0640.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/zurich-uetliberg/DSC_0640.jpg"/></a></div></div>
<div class="dbg_caption">Erik, Tom, Sebas, Chris and Sjoerd.</div>
</td>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/zurich-uetliberg/DSC_0648.jpg" rel="lightbox[zurich-uetliberg]" title="River Limmat seen from the Lindenhof. In the background us the Predigerkirche.">
<img src="http://martijn.van.steenbergen.nl/thumbnails/zurich-uetliberg/DSC_0648.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/zurich-uetliberg/DSC_0648.jpg"/></a></div></div>
<div class="dbg_caption">River Limmat seen from the Lindenhof. In the background us the Predigerkirche.</div>
</td>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/zurich-uetliberg/DSC_0654.jpg" rel="lightbox[zurich-uetliberg]" title="Z&uuml;rich Gro&szlig;m&uuml;nster seen from the Lindenhof.">
<img src="http://martijn.van.steenbergen.nl/thumbnails/zurich-uetliberg/DSC_0654.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/zurich-uetliberg/DSC_0654.jpg"/></a></div></div>
<div class="dbg_caption">Z&uuml;rich Gro&szlig;m&uuml;nster seen from the Lindenhof.</div>
</td>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/zurich-uetliberg/DSC_0657.jpg" rel="lightbox[zurich-uetliberg]" title="...">
<img src="http://martijn.van.steenbergen.nl/thumbnails/zurich-uetliberg/DSC_0657.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/zurich-uetliberg/DSC_0657.jpg"/></a></div></div>
<div class="dbg_caption">...</div>
</td>
</tr>
<tr>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/zurich-uetliberg/DSC_0665.jpg" rel="lightbox[zurich-uetliberg]" title="A brook in the forests around the &Uuml;etliberg.">
<img src="http://martijn.van.steenbergen.nl/thumbnails/zurich-uetliberg/DSC_0665.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/zurich-uetliberg/DSC_0665.jpg"/></a></div></div>
<div class="dbg_caption">A brook in the forests around the &Uuml;etliberg.</div>
</td>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/zurich-uetliberg/DSC_0669.jpg" rel="lightbox[zurich-uetliberg]" title="">
<img src="http://martijn.van.steenbergen.nl/thumbnails/zurich-uetliberg/DSC_0669.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/zurich-uetliberg/DSC_0669.jpg"/></a></div></div>
<div class="dbg_caption"></div>
</td>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/zurich-uetliberg/DSC_0676.jpg" rel="lightbox[zurich-uetliberg]" title="Sitting on top of a ruin on the slope of the &Uuml;etliberg.">
<img src="http://martijn.van.steenbergen.nl/thumbnails/zurich-uetliberg/DSC_0676.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/zurich-uetliberg/DSC_0676.jpg"/></a></div></div>
<div class="dbg_caption">Sitting on top of a ruin on the slope of the &Uuml;etliberg.</div>
</td>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/zurich-uetliberg/DSC_0679.jpg" rel="lightbox[zurich-uetliberg]" title="">
<img src="http://martijn.van.steenbergen.nl/thumbnails/zurich-uetliberg/DSC_0679.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/zurich-uetliberg/DSC_0679.jpg"/></a></div></div>
<div class="dbg_caption"></div>
</td>
</tr>
<tr>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/zurich-uetliberg/DSC_0686.jpg" rel="lightbox[zurich-uetliberg]" title="">
<img src="http://martijn.van.steenbergen.nl/thumbnails/zurich-uetliberg/DSC_0686.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/zurich-uetliberg/DSC_0686.jpg"/></a></div></div>
<div class="dbg_caption"></div>
</td>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/zurich-uetliberg/DSC_0693.jpg" rel="lightbox[zurich-uetliberg]" title="View from the top of the mountain, near the station.">
<img src="http://martijn.van.steenbergen.nl/thumbnails/zurich-uetliberg/DSC_0693.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/zurich-uetliberg/DSC_0693.jpg"/></a></div></div>
<div class="dbg_caption">View from the top of the mountain, near the station.</div>
</td>
</tr>
</table>

<img src="http://feeds.feedburner.com/~r/MedeaMelana/~4/cu06mHLKVng" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://martijn.van.steenbergen.nl/journal/2010/03/20/uetliberg/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://martijn.van.steenbergen.nl/journal/2010/03/20/uetliberg/</feedburner:origLink></item>
		<item>
		<title>Colors in GHCi</title>
		<link>http://feedproxy.google.com/~r/MedeaMelana/~3/S-2S1j2QNKY/</link>
		<comments>http://martijn.van.steenbergen.nl/journal/2010/02/27/colors-in-ghci/#comments</comments>
		<pubDate>Sat, 27 Feb 2010 11:10:47 +0000</pubDate>
		<dc:creator>Martijn</dc:creator>
				<category><![CDATA[Haskell]]></category>

		<guid isPermaLink="false">http://martijn.van.steenbergen.nl/journal/?p=467</guid>
		<description><![CDATA[Whenever you load a Haskell file into GHCi, it will either tell you your modules loaded fine, or that there were some errors. Sebas came with the awesome idea of coloring these messages green and red, respectively. Here is how to do this:
Create a script that looks like this:

#!/usr/bin/env bash

GREEN=`echo -e '\033[92m'`
RED=`echo -e '\033[91m'`
RESET=`echo -e [...]]]></description>
			<content:encoded><![CDATA[<p>Whenever you load a Haskell file into GHCi, it will either tell you your modules loaded fine, or that there were some errors. <a href="http://twitter.com/sfvisser">Sebas</a> came with the awesome idea of coloring these messages green and red, respectively. Here is how to do this:</p>
<p>Create a script that looks like this:</p>
<pre style="overflow: auto; margin-right: 120px">
#!/usr/bin/env bash

GREEN=`echo -e '\033[92m'`
RED=`echo -e '\033[91m'`
RESET=`echo -e '\033[0m'`

/usr/bin/ghci "${@}" |\
  sed "s/^Failed, modules loaded:/${RED}&#038;${RESET}/g;s/^Ok, modules loaded:/${GREEN}&#038;${RESET}/g"
</pre>
<p>If your <code>ghci</code> is not located in <code>/usr/bin</code>, change the path in the script accordingly. If you want, you can name your script <code>ghci</code> so that it takes over the original one. Just make sure its location appears in your <code>PATH</code> variable before the location of the true <code>ghci</code>.</p>
<p>If all goes well, you should now see colors whenever you load your modules:</p>
<pre style="background-color: #333; color: #a3bd84;">
% ghci Sirenial.Merge
...
<span style="color:#fd3a1e">Failed, modules loaded:</span> Sirenial.Query.
</pre>
<p>And then when the bug has been fixed:</p>
<pre style="background-color: #333; color: #a3bd84;">
% ghci Sirenial.Merge
...
<span style="color:#2fe424">Ok, modules loaded:</span> Sirenial.Merge, Sirenial.Query.
</pre>
<p>This has only been tested on Terminal.app in Snow Leopard. If it doesn&#8217;t work for your system, please leave a comment, with—if possible—a fix.</p>
<p>There is a small issue: sometimes <code>sed</code> delays the colored parts a bit, causing your prompt to be printed <em>before</em> the success or error message. Again, if you know a fix, please comment.</p>
<img src="http://feeds.feedburner.com/~r/MedeaMelana/~4/S-2S1j2QNKY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://martijn.van.steenbergen.nl/journal/2010/02/27/colors-in-ghci/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://martijn.van.steenbergen.nl/journal/2010/02/27/colors-in-ghci/</feedburner:origLink></item>
		<item>
		<title>Sneeuw at Warande</title>
		<link>http://feedproxy.google.com/~r/MedeaMelana/~3/hBRqHg3yNP8/</link>
		<comments>http://martijn.van.steenbergen.nl/journal/2009/12/21/sneeuw-at-warande/#comments</comments>
		<pubDate>Sun, 20 Dec 2009 23:07:47 +0000</pubDate>
		<dc:creator>Martijn</dc:creator>
				<category><![CDATA[Life]]></category>

		<guid isPermaLink="false">http://martijn.van.steenbergen.nl/journal/?p=458</guid>
		<description><![CDATA[
With the whole of the Netherlands covered in snow, the traffic is completely jammed. But that didn&#8217;t bother my flatmates and me, because we stayed home all day. In the afternoon we rolled the biggest snowball in the street; at the end it reached a meter in diameter and it took three of us to [...]]]></description>
			<content:encoded><![CDATA[<img class="dbg_single" src="http://martijn.van.steenbergen.nl/galleries/singles/DSC_0309.jpg" alt="DSC_0309.jpg" />
<p>With the whole of the Netherlands covered in snow, the traffic is completely jammed. But that didn&#8217;t bother my flatmates and me, because we stayed home all day. In the afternoon we rolled the biggest snowball in the street; at the end it reached a meter in diameter and it took three of us to push it around. We guessed it weighed approximately 250 kg at that point.</p>
<p>While <a href="http://www.ah.nl/recepten/recept?id=8478">dinner</a> was cooking I went outside again to take some pictures.</p>
<table cellspacing="0" cellpadding="0" class="dbg_gallery">
<tr>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/sneeuw-warande/DSC_0296.jpg" rel="lightbox[sneeuw-warande]" title="Our giant snowball. At the end it took 3 people to push it around.">
<img src="http://martijn.van.steenbergen.nl/thumbnails/sneeuw-warande/DSC_0296.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/sneeuw-warande/DSC_0296.jpg"/></a></div></div>
<div class="dbg_caption">Our giant snowball. At the end it took 3 people to push it around.</div>
</td>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/sneeuw-warande/DSC_0297.jpg" rel="lightbox[sneeuw-warande]" title="">
<img src="http://martijn.van.steenbergen.nl/thumbnails/sneeuw-warande/DSC_0297.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/sneeuw-warande/DSC_0297.jpg"/></a></div></div>
<div class="dbg_caption"></div>
</td>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/sneeuw-warande/DSC_0301.jpg" rel="lightbox[sneeuw-warande]" title="">
<img src="http://martijn.van.steenbergen.nl/thumbnails/sneeuw-warande/DSC_0301.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/sneeuw-warande/DSC_0301.jpg"/></a></div></div>
<div class="dbg_caption"></div>
</td>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/sneeuw-warande/DSC_0305.jpg" rel="lightbox[sneeuw-warande]" title="">
<img src="http://martijn.van.steenbergen.nl/thumbnails/sneeuw-warande/DSC_0305.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/sneeuw-warande/DSC_0305.jpg"/></a></div></div>
<div class="dbg_caption"></div>
</td>
</tr>
<tr>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/sneeuw-warande/DSC_0309.jpg" rel="lightbox[sneeuw-warande]" title="In the background is the orange-lit road.">
<img src="http://martijn.van.steenbergen.nl/thumbnails/sneeuw-warande/DSC_0309.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/sneeuw-warande/DSC_0309.jpg"/></a></div></div>
<div class="dbg_caption">In the background is the orange-lit road.</div>
</td>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/sneeuw-warande/DSC_0313.jpg" rel="lightbox[sneeuw-warande]" title="">
<img src="http://martijn.van.steenbergen.nl/thumbnails/sneeuw-warande/DSC_0313.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/sneeuw-warande/DSC_0313.jpg"/></a></div></div>
<div class="dbg_caption"></div>
</td>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/sneeuw-warande/DSC_0316.jpg" rel="lightbox[sneeuw-warande]" title="Looking up at the snowy foliage. Some stars are visible in the sky.">
<img src="http://martijn.van.steenbergen.nl/thumbnails/sneeuw-warande/DSC_0316.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/sneeuw-warande/DSC_0316.jpg"/></a></div></div>
<div class="dbg_caption">Looking up at the snowy foliage. Some stars are visible in the sky.</div>
</td>
</tr>
</table>

<img src="http://feeds.feedburner.com/~r/MedeaMelana/~4/hBRqHg3yNP8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://martijn.van.steenbergen.nl/journal/2009/12/21/sneeuw-at-warande/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://martijn.van.steenbergen.nl/journal/2009/12/21/sneeuw-at-warande/</feedburner:origLink></item>
		<item>
		<title>Midwinter Fair 2009</title>
		<link>http://feedproxy.google.com/~r/MedeaMelana/~3/PqV4THPvFoo/</link>
		<comments>http://martijn.van.steenbergen.nl/journal/2009/12/13/midwinter-fair-2009/#comments</comments>
		<pubDate>Sun, 13 Dec 2009 22:55:33 +0000</pubDate>
		<dc:creator>Martijn</dc:creator>
				<category><![CDATA[Events]]></category>

		<guid isPermaLink="false">http://martijn.van.steenbergen.nl/journal/?p=454</guid>
		<description><![CDATA[Vandaag nam de Midwinter Fair 2009 plaats in het Archeon in Alphen aan den Rijn. Muziek, dans, markt, vechtshows, ambachten, lezingen en speltoernooien kwamen bijeen op deze zeer succesvolle en gezellige dag. Beter weer hadden de bezoekers zich niet kunnen wensen: winters koud, blauwe lucht en prachtige zonsopkomst en -ondergang.





Erica van Brenk, Lies Sommer en [...]]]></description>
			<content:encoded><![CDATA[<p>Vandaag nam de Midwinter Fair 2009 plaats in het Archeon in Alphen aan den Rijn. Muziek, dans, markt, vechtshows, ambachten, lezingen en speltoernooien kwamen bijeen op deze zeer succesvolle en gezellige dag. Beter weer hadden de bezoekers zich niet kunnen wensen: winters koud, blauwe lucht en prachtige zonsopkomst en -ondergang.</p>
<table cellspacing="0" cellpadding="0" class="dbg_gallery">
<tr>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/midwinterfair2009/DSC_9841.jpg" rel="lightbox[midwinterfair2009]" title="Erica van Brenk, Lies Sommer en Marco van Asperen van de folkband Orfeo.">
<img src="http://martijn.van.steenbergen.nl/thumbnails/midwinterfair2009/DSC_9841.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/midwinterfair2009/DSC_9841.jpg"/></a></div></div>
<div class="dbg_caption">Erica van Brenk, Lies Sommer en Marco van Asperen van de folkband Orfeo.</div>
</td>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/midwinterfair2009/DSC_9857.jpg" rel="lightbox[midwinterfair2009]" title="Lies Sommer bespeelt de draailier; in de achtergrond Marco van Asperen op de gitaar.">
<img src="http://martijn.van.steenbergen.nl/thumbnails/midwinterfair2009/DSC_9857.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/midwinterfair2009/DSC_9857.jpg"/></a></div></div>
<div class="dbg_caption">Lies Sommer bespeelt de draailier; in de achtergrond Marco van Asperen op de gitaar.</div>
</td>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/midwinterfair2009/DSC_9866.jpg" rel="lightbox[midwinterfair2009]" title="Paul van de folkband Orfeo op de contrabas.">
<img src="http://martijn.van.steenbergen.nl/thumbnails/midwinterfair2009/DSC_9866.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/midwinterfair2009/DSC_9866.jpg"/></a></div></div>
<div class="dbg_caption">Paul van de folkband Orfeo op de contrabas.</div>
</td>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/midwinterfair2009/DSC_9876.jpg" rel="lightbox[midwinterfair2009]" title="Er werd flink gedanst in de kloosterzaal van het Archeon.">
<img src="http://martijn.van.steenbergen.nl/thumbnails/midwinterfair2009/DSC_9876.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/midwinterfair2009/DSC_9876.jpg"/></a></div></div>
<div class="dbg_caption">Er werd flink gedanst in de kloosterzaal van het Archeon.</div>
</td>
</tr>
<tr>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/midwinterfair2009/DSC_9877.jpg" rel="lightbox[midwinterfair2009]" title="E&eacute;n van de vele marktkraampjes.">
<img src="http://martijn.van.steenbergen.nl/thumbnails/midwinterfair2009/DSC_9877.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/midwinterfair2009/DSC_9877.jpg"/></a></div></div>
<div class="dbg_caption">E&eacute;n van de vele marktkraampjes.</div>
</td>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/midwinterfair2009/DSC_9881.jpg" rel="lightbox[midwinterfair2009]" title="Modellen in het zwart en wit tonen graag hun mooie kleding.">
<img src="http://martijn.van.steenbergen.nl/thumbnails/midwinterfair2009/DSC_9881.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/midwinterfair2009/DSC_9881.jpg"/></a></div></div>
<div class="dbg_caption">Modellen in het zwart en wit tonen graag hun mooie kleding.</div>
</td>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/midwinterfair2009/DSC_9898.jpg" rel="lightbox[midwinterfair2009]" title="Laag, warm zonlicht valt op de muren van deze schuur.">
<img src="http://martijn.van.steenbergen.nl/thumbnails/midwinterfair2009/DSC_9898.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/midwinterfair2009/DSC_9898.jpg"/></a></div></div>
<div class="dbg_caption">Laag, warm zonlicht valt op de muren van deze schuur.</div>
</td>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/midwinterfair2009/DSC_9901.jpg" rel="lightbox[midwinterfair2009]" title="Tussen de huizen in &eacute;&eacute;n van de straten in het Archeon.">
<img src="http://martijn.van.steenbergen.nl/thumbnails/midwinterfair2009/DSC_9901.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/midwinterfair2009/DSC_9901.jpg"/></a></div></div>
<div class="dbg_caption">Tussen de huizen in &eacute;&eacute;n van de straten in het Archeon.</div>
</td>
</tr>
<tr>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/midwinterfair2009/DSC_9908.jpg" rel="lightbox[midwinterfair2009]" title="Verkoop van stenen in een winkeltje.">
<img src="http://martijn.van.steenbergen.nl/thumbnails/midwinterfair2009/DSC_9908.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/midwinterfair2009/DSC_9908.jpg"/></a></div></div>
<div class="dbg_caption">Verkoop van stenen in een winkeltje.</div>
</td>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/midwinterfair2009/DSC_9913.jpg" rel="lightbox[midwinterfair2009]" title="">
<img src="http://martijn.van.steenbergen.nl/thumbnails/midwinterfair2009/DSC_9913.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/midwinterfair2009/DSC_9913.jpg"/></a></div></div>
<div class="dbg_caption"></div>
</td>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/midwinterfair2009/DSC_9972.jpg" rel="lightbox[midwinterfair2009]" title="Vuurspuwers zorgen voor een spetterende vuurshow.">
<img src="http://martijn.van.steenbergen.nl/thumbnails/midwinterfair2009/DSC_9972.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/midwinterfair2009/DSC_9972.jpg"/></a></div></div>
<div class="dbg_caption">Vuurspuwers zorgen voor een spetterende vuurshow.</div>
</td>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/midwinterfair2009/DSC_9991.jpg" rel="lightbox[midwinterfair2009]" title="Vuurspuwers zorgen voor een spetterende vuurshow.">
<img src="http://martijn.van.steenbergen.nl/thumbnails/midwinterfair2009/DSC_9991.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/midwinterfair2009/DSC_9991.jpg"/></a></div></div>
<div class="dbg_caption">Vuurspuwers zorgen voor een spetterende vuurshow.</div>
</td>
</tr>
<tr>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/midwinterfair2009/DSC_0083.jpg" rel="lightbox[midwinterfair2009]" title="Met brandende pijlen werd getracht een haard aan te steken.">
<img src="http://martijn.van.steenbergen.nl/thumbnails/midwinterfair2009/DSC_0083.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/midwinterfair2009/DSC_0083.jpg"/></a></div></div>
<div class="dbg_caption">Met brandende pijlen werd getracht een haard aan te steken.</div>
</td>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/midwinterfair2009/DSC_0028.jpg" rel="lightbox[midwinterfair2009]" title="De haard vat vlam en vuurpijlen schieten omhoog.">
<img src="http://martijn.van.steenbergen.nl/thumbnails/midwinterfair2009/DSC_0028.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/midwinterfair2009/DSC_0028.jpg"/></a></div></div>
<div class="dbg_caption">De haard vat vlam en vuurpijlen schieten omhoog.</div>
</td>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/midwinterfair2009/DSC_0091.jpg" rel="lightbox[midwinterfair2009]" title="E&eacute;n van de winkeltjes.">
<img src="http://martijn.van.steenbergen.nl/thumbnails/midwinterfair2009/DSC_0091.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/midwinterfair2009/DSC_0091.jpg"/></a></div></div>
<div class="dbg_caption">E&eacute;n van de winkeltjes.</div>
</td>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/midwinterfair2009/DSC_0095.jpg" rel="lightbox[midwinterfair2009]" title="Het verlichte klooster bij het vallen van de avond.">
<img src="http://martijn.van.steenbergen.nl/thumbnails/midwinterfair2009/DSC_0095.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/midwinterfair2009/DSC_0095.jpg"/></a></div></div>
<div class="dbg_caption">Het verlichte klooster bij het vallen van de avond.</div>
</td>
</tr>
<tr>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/midwinterfair2009/DSC_0102.jpg" rel="lightbox[midwinterfair2009]" title="Maca&eacute;l van de Keltische folkband Rapalje.">
<img src="http://martijn.van.steenbergen.nl/thumbnails/midwinterfair2009/DSC_0102.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/midwinterfair2009/DSC_0102.jpg"/></a></div></div>
<div class="dbg_caption">Maca&eacute;l van de Keltische folkband Rapalje.</div>
</td>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/midwinterfair2009/DSC_0110.jpg" rel="lightbox[midwinterfair2009]" title="Dieb en William van de Keltische folkband Rapalje.">
<img src="http://martijn.van.steenbergen.nl/thumbnails/midwinterfair2009/DSC_0110.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/midwinterfair2009/DSC_0110.jpg"/></a></div></div>
<div class="dbg_caption">Dieb en William van de Keltische folkband Rapalje.</div>
</td>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/midwinterfair2009/DSC_0114.jpg" rel="lightbox[midwinterfair2009]" title="William van de Keltische folkband Rapalje.">
<img src="http://martijn.van.steenbergen.nl/thumbnails/midwinterfair2009/DSC_0114.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/midwinterfair2009/DSC_0114.jpg"/></a></div></div>
<div class="dbg_caption">William van de Keltische folkband Rapalje.</div>
</td>
</tr>
</table>

<img src="http://feeds.feedburner.com/~r/MedeaMelana/~4/PqV4THPvFoo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://martijn.van.steenbergen.nl/journal/2009/12/13/midwinter-fair-2009/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://martijn.van.steenbergen.nl/journal/2009/12/13/midwinter-fair-2009/</feedburner:origLink></item>
		<item>
		<title>GADTs in Haskell 98</title>
		<link>http://feedproxy.google.com/~r/MedeaMelana/~3/xO7P33WBabM/</link>
		<comments>http://martijn.van.steenbergen.nl/journal/2009/11/12/gadts-in-haskell-98/#comments</comments>
		<pubDate>Thu, 12 Nov 2009 11:30:40 +0000</pubDate>
		<dc:creator>Martijn</dc:creator>
				<category><![CDATA[Haskell]]></category>

		<guid isPermaLink="false">http://martijn.van.steenbergen.nl/journal/?p=447</guid>
		<description><![CDATA[Last time we saw how some type classes can be reified to datatypes, transformed in arbitrary ways and then transformed back again to polymorphic types. I briefly mentioned that for some type classes you need GADTs rather than classical ADTs.
The dual to this story holds as well: many (if not all) GADTs can be represented [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://martijn.van.steenbergen.nl/journal/2009/10/18/transforming-polymorphic-values/">Last time</a> we saw how some type classes can be reified to datatypes, transformed in arbitrary ways and then transformed back again to polymorphic types. I briefly mentioned that for some type classes you need GADTs rather than classical ADTs.</p>
<p>The dual to this story holds as well: many (if not all) GADTs can be represented as type classes. To see how, take a look at the standard GADT example:</p>
<pre>
data Term :: * -> * where
  Lit  :: Int        -> Term Int
  Inc  :: Term Int   -> Term Int
  IsZ  :: Term Int   -> Term Bool
  If   :: Term Bool  -> Term a    -> Term a     -> Term a
  Pair :: Term a     -> Term b    -> Term (a,b)
  Fst  :: Term (a,b) -> Term a
  Snd  :: Term (a,b) -> Term b
</pre>
<p>Each constructor result type is indexed by the type of the construct it represents. This example is explained in detail in <a href="http://www.cis.upenn.edu/~dimitriv/wobbly/gadt.pdf">Simple Unification-based Type Inference for GADTs</a> by Simon Peyton Jones et al.</p>
<p>Writing <code>Term</code> like this allows the following well-typed evaluation function:</p>
<pre>
eval :: Term a -> a
eval term = case term of
  Lit n    -> n
  Inc t    -> eval t + 1
  IsZ t    -> eval t == 0
  If c x y -> if eval c then eval x else eval y
  Pair x y -> (eval x, eval y)
  Fst t    -> fst (eval t)
  Snd t    -> snd (eval t)
</pre>
<p>To convert a GADT <code>G</code> to a type class, create a new type class <code>GC</code> with one type parameter <code>g</code>. This type parameter <code>g</code> will have the same kind as <code>G</code>, and its arguments will have the same semantics as those of the <code>G</code>. Then for each constructor create one function in the type class with the exact same type, replacing every occurrence of <code>G</code> by <code>g</code>.</p>
<p>If we follow this recipe for <code>Term</code> we get:</p>
<pre>
class TermC term where
  lit  :: Int         -> term Int
  inc  :: term Int    -> term Int
  isZ  :: term Int    -> term Bool
  if_  :: term Bool   -> term a    -> term a      -> term a
  pair :: term a      -> term b    -> term (a, b)
  fst_ :: term (a, b) -> term a
  snd_ :: term (a, b) -> term b
</pre>
<p>I&#8217;ve used the same names as the constructors except I made them lowercase. I appended <code>_</code> to some to avoid name clashes (<code>fst</code>, <code>snd</code>) or lexical errors (<code>if</code>).</p>
<p>Of course, the <a href="http://martijn.van.steenbergen.nl/journal/2009/10/18/transforming-polymorphic-values/">free instance</a> of <code>TermC</code> is <code>Term</code>.</p>
<p>Now the recipe doesn&#8217;t make any assumptions about the GADT in question, but I don&#8217;t know if this is right, because you can do some powerful things with GADTs. The two special things in datatypes I can think of—class constraints and existentially quantified variables—are no problem in type classes. Also the thing that makes GADTs truly special—custom type indices in constructor result types—have always been allowed in type classes. If I have overlooked something, please let me know and leave a comment.</p>
<p>However, perhaps the more important question is: can we do everything with <code>TermC</code> that we could do with <code>Term</code>? Well, we can certainly implement <code>eval</code>: this becomes a newtype with a corresponding <code>instance TermC</code>:</p>
<pre>
newtype Eval a = Eval { evalC :: a }

instance TermC Eval where
  lit       = Eval
  inc t     = Eval $ evalC t + 1
  isZ t     = Eval $ evalC t == 0
  if_ c x y = Eval $ if evalC c then evalC x else evalC y
  pair x y  = Eval $ (evalC x, evalC y)
  fst_ t    = Eval $ fst (evalC t)
  snd_ t    = Eval $ snd (evalC t)
</pre>
<p>The instance is very, very similar to the original evaluation function. Only instead of recursively calling <code>eval</code>, we call <code>evalC</code> and wrap the result in an <code>Eval</code> constructor. The type of <code>evalC :: Eval a -> a</code> is also similar to <code>eval :: Term a -> a</code>.</p>
<p>What I like most about encoding terms and their evaluation this way is that all the code is Haskell 98!</p>
<p>But not everything is this easy. The step evaluator on the <a href="http://www.haskell.org/haskellwiki/?oldid=29127">Haskell wiki page on GADTs</a>, for example, uses deep pattern matching and as we saw last time this is a lot easier to do on datatypes than in class instances. In fact, I don&#8217;t know if this particular example is possible to translate at all, and if it is, it&#8217;s certainly not going to be Haskell 98.</p>
<p>Another thing GADTs are useful for is witnesses in families of types. Again, I don&#8217;t know if it is possible to translate this use case to class instances.</p>
<p>Summing up, any GADT can be translated to a type class, but not all uses of GADTs have obvious translations. The standard GADT example, however, is trivial to translate. For that reason, it is probably not the most interesting choice as standard example of GADTs.</p>
<img src="http://feeds.feedburner.com/~r/MedeaMelana/~4/xO7P33WBabM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://martijn.van.steenbergen.nl/journal/2009/11/12/gadts-in-haskell-98/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://martijn.van.steenbergen.nl/journal/2009/11/12/gadts-in-haskell-98/</feedburner:origLink></item>
		<item>
		<title>Transforming polymorphic values</title>
		<link>http://feedproxy.google.com/~r/MedeaMelana/~3/UmmZgyHYIbI/</link>
		<comments>http://martijn.van.steenbergen.nl/journal/2009/10/18/transforming-polymorphic-values/#comments</comments>
		<pubDate>Sun, 18 Oct 2009 17:03:08 +0000</pubDate>
		<dc:creator>Martijn</dc:creator>
				<category><![CDATA[Haskell]]></category>

		<guid isPermaLink="false">http://martijn.van.steenbergen.nl/journal/?p=424</guid>
		<description><![CDATA[
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE DeriveDataTypeable #-}

module Arith where

import Data.Generics
import Control.Applicative

Recently there&#8217;s been a bit of talk in the Haskell café about what a DSL is. Robert Atkey gave the nice answer that you can often capture a DSL in one or more type classes, and implementations of this DSL then correspond to instances of [...]]]></description>
			<content:encoded><![CDATA[<pre>
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE DeriveDataTypeable #-}

module Arith where

import Data.Generics
import Control.Applicative
</pre>
<p>Recently there&#8217;s been a bit of talk in the Haskell café about <a href="http://thread.gmane.org/gmane.comp.lang.haskell.cafe/64474">what a DSL is</a>. Robert Atkey gave the <a href="http://thread.gmane.org/gmane.comp.lang.haskell.cafe/64474/focus=64494">nice answer</a> that you can often capture a DSL in one or more type classes, and implementations of this DSL then correspond to instances of these type classes.</p>
<p>Arithmetic expressions with variables, for example, can be captured by this type:</p>
<pre>
type Arith = forall a. (Num a, Var a) => a
</pre>
<p>The <code>Num</code> constraint allows the use of integers and the basic operators, while <code>Var</code> allows the use of variables:</p>
<pre>
class Var a where
  var :: String -> a
</pre>
<p>I would like to show you the various things you can do with a value of type <code>Arith</code>.</p>
<h2>Example <code>Arith</code> values</h2>
<p>Before we do that, however, let&#8217;s think about what this definition of <code>Arith</code> <em>really</em> means. It&#8217;s not any specific type yet, such as <code>Int</code> or <code>Float</code>. Rather, it&#8217;s an expression composed entirely of functions from the <code>Num</code> and <code>Var</code> type classes. Since <code>fromInteger</code> and <code>var</code> are the only functions from those classes that don&#8217;t take recursive values as arguments, calls to those two functions will necessarily make up the leaves of any <code>Arith</code> tree—provided it is finite. Here are some example values:</p>
<pre>
example1 :: Arith
example1 = 1 + 2 * 3

example2 :: Arith
example2 = 2 * var "x" + 0 * var "y"
</pre>
<p>Note that the integer literals in these examples are automatically expanded to calls to <code>fromInteger</code>. To keep the examples somewhat smaller, we will ignore the <code>negate</code>, <code>abs</code> and <code>signum</code> functions from the <code>Num</code> type class. We will also ignore <code>Num</code>&#8217;s superclasses <code>Eq</code> and <code>Show</code>, providing empty instances where necessary; they mess up some of our examples.</p>
<h2>Evaluation as instance</h2>
<p>Now, given values of type <code>Arith</code>, what can we do with them? One thing we can think of is to combine them into a bigger expression:</p>
<pre>
example3 :: Arith
example3 = 6 + example1 - 3 * example2
</pre>
<p>Can we inspect them, or make them smaller? Well, the type is polymorphic, so we can narrow it to a concrete type. We don&#8217;t know any types yet that are instances of both <code>Num</code> and <code>Var</code>, however, so let&#8217;s make one! We&#8217;ll go for one that evaluates the expression, yielding an integer given an environment:</p>
<pre>
newtype Eval = Eval { runEval :: Env -> Integer }
type    Env  = [(String, Integer)]

instance Show Eval
instance Eq Eval

instance Num Eval where
  Eval f + Eval g = Eval (liftA2 (+) f g)
  Eval f * Eval g = Eval (liftA2 (*) f g)
  Eval f - Eval g = Eval (liftA2 (-) f g)
  fromInteger n   = Eval (const n)

instance Var Eval where
  var name = Eval $ \e -> case lookup name e of
    Nothing -> error ("unbound variable: " ++ name)
    Just x -> x
</pre>
<p>Now we can evaluate <code>Arith</code>s simply by calling <code>runExpr</code> on them:</p>
<pre>
*Arith> runEval example1 []
7
*Arith> runEval example2 [("x", 6), ("y", 7)]
12
</pre>
<p>This method of writing functions on <code>Arith</code>s is very much like writing catamorphisms: the tree is reduced to a value, and operands have already had the catamorphisms applied to them recursively, so any computation you want to run on the tree in this way has to be compositional.</p>
<h2>The free instance</h2>
<p>Let&#8217;s do something slightly more complicated: <a href="http://en.wikipedia.org/wiki/Constant_folding">constant folding</a>. The idea of constant folding is to eliminate trivial uses of the operators. <code>x * 0</code>, for example, can be reduced to <code>0</code>, regardless of <code>x</code>&#8217;s value. Other examples are multiplication by 1 and subtraction of 0. Also, if an expression doesn&#8217;t use any variables, we can evaluate it right away.</p>
<p>What type should our constant folding function have? We would expect something like this:</p>
<pre>
foldConstants :: Arith -> Arith
</pre>
<p>Can we write this function as a newtype with instances, too? This turns out to be a lot more difficult. For example, in the case of + we want to inspect the left-hand and right-hand sides to check whether they are zero. But the operands are of type <code>Arith</code>, so we would have to write helper functions such as <code>isZero :: Arith -> Bool</code> which in turn have to be written with new newtypes and instances. This quickly gets out hand. We are running into problems because our catamorphism isn&#8217;t regular anymore.</p>
<p>It would be nice if we could do a &#8216;deep&#8217; inspection of the operands, like we can do when writing functions on normal datatypes. So let&#8217;s do that: make a normal datatype and write our <code>foldConstants</code> on that type.</p>
<pre>
data ReifyArith
  = Add ReifyArith ReifyArith
  | Mul ReifyArith ReifyArith
  | Sub ReifyArith ReifyArith
  | Int Integer
  | Var String
  deriving (Eq, Show, Data, Typeable)

instance Num ReifyArith where
  (+) = Add
  (*) = Mul
  (-) = Sub
  fromInteger = Int

instance Var ReifyArith where
  var = Var
</pre>
<p>I&#8217;ve called this datatype <code>ReifyArith</code> because it reifies an <code>Arith</code> value, turning it into something concrete and tangible. I like to call <code>ReifyArith</code> the <em>free instance</em>, because you can write such a datatype for any combination of classes that adhere to the requirements Robert Atkey mentioned in his email, either as an ADT or a GADT.</p>
<h2>From <code>ReifyArith</code> back to <code>Arith</code></h2>
<p>The nice thing about the free instance is that we do not lose any information: we can easily go back to the polymorphic version of our expressions, like so:</p>
<pre>
generify :: ReifyArith -> Arith
generify expr = case expr of
  Add x y      -> generify x + generify y
  Mul x y      -> generify x * generify y
  Sub x y      -> generify x - generify y
  Int n        -> fromInteger n
  Var name     -> var name
</pre>
<p>Using this technique, we can manipulate <code>Arith</code> values in arbitrary ways. One thing I particularly like is that the <code>ReifyArith</code> is entirely internal to our module and doesn&#8217;t need to be exposed to the outside world.</p>
<h2>Constant folding</h2>
<p>Now that we have a normal datatype, we can write our constant folding transformation. We implement it as a function that does only one step of the rewriting and then use generic programming (<a href="http://www.cs.vu.nl/boilerplate/">Scrap Your Boilerplate</a> in this case) to apply it recursively to our tree. This is why we also derived <code>Data</code> and <code>Typeable</code> for <code>ReifyArith</code>.</p>
<pre>
step :: ReifyArith -> ReifyArith
step expr = case expr of
  -- Reduce computations on integer literals
  Add (Int x) (Int y) -> Int (x + y)
  Mul (Int x) (Int y) -> Int (x * y)
  Sub (Int x) (Int y) -> Int (x - y)

  -- Rewrite rules based on 0 and 1
  Add (Int 0) y -> y
  Add x (Int 0) -> x
  Mul (Int 0) _ -> 0
  Mul _ (Int 0) -> 0
  Mul (Int 1) y -> y
  Mul x (Int 1) -> x
  Sub x (Int 0) -> x

  -- Catch all other cases
  _ -> expr

foldConstants :: Arith -> Arith
foldConstants expr = generify (everywhere (mkT step) expr)
</pre>
<p>Let&#8217;s see if it works:</p>
<pre>
*Arith> foldConstants example1 :: ReifyArith
Int 7
*Arith> foldConstants example2 :: ReifyArith
Mul (Int 2) (Var "x")
</pre>
<p>Indeed, in the first case the transformation is able to fold the entire expression to a single integer literal, and in the second case it has completely eliminated the second term.</p>
<h2>Conclusion</h2>
<p>We have seen that although polymorphic values look abstract and intangible, they are in fact easily manipulated. If a regular, compositional catamorphism is required, the function can be written as instances of the relevant datatypes. If a more complicated, non-regular scheme is required, we can reify the value as a concrete datatype and define the function on that.</p>
<p>Many thanks to <a href="http://w3future.com/">Sjoerd Visscher</a> for valuable feedback!</p>
<img src="http://feeds.feedburner.com/~r/MedeaMelana/~4/UmmZgyHYIbI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://martijn.van.steenbergen.nl/journal/2009/10/18/transforming-polymorphic-values/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		<feedburner:origLink>http://martijn.van.steenbergen.nl/journal/2009/10/18/transforming-polymorphic-values/</feedburner:origLink></item>
		<item>
		<title>Context Synonyms</title>
		<link>http://feedproxy.google.com/~r/MedeaMelana/~3/60i6JB9Fa3o/</link>
		<comments>http://martijn.van.steenbergen.nl/journal/2009/10/11/context-synonyms/#comments</comments>
		<pubDate>Sun, 11 Oct 2009 16:49:49 +0000</pubDate>
		<dc:creator>Martijn</dc:creator>
				<category><![CDATA[Haskell]]></category>

		<guid isPermaLink="false">http://martijn.van.steenbergen.nl/journal/?p=416</guid>
		<description><![CDATA[Sometimes a function&#8217;s type class context grows so big that you would like to group the various type class constraints together and give it a sensible name, especially if that big context is used many times in your module. Consider for example the fold function from module Generics.MultiRec.FoldAlgK:

fold :: (Fam phi, HFunctor phi (PF phi), [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes a function&#8217;s type class context grows so big that you would like to group the various type class constraints together and give it a sensible name, especially if that big context is used many times in your module. Consider for example the <code>fold</code> function from module <code><a href="http://hackage.haskell.org/packages/archive/multirec/0.4/doc/html/Generics-MultiRec-FoldAlgK.html">Generics.MultiRec.FoldAlgK</a></code>:</p>
<pre>
fold :: (Fam phi, HFunctor phi (PF phi), Fold (PF phi)) =>
  Algebra phi r -> phi ix -> ix -> r
</pre>
<p>Context synonyms aim to make it possible to give long contexts a name and reuse it throughout modules. Using a context synonym, the example above can be rewritten to:</p>
<pre>
context FoldFam phi = (Fam phi, HFunctor phi (PF phi), Fold (PF phi))
fold :: FoldFam phi => Algebra phi r -> phi ix -> ix -> r
</pre>
<p>Note that context synonyms are reminiscent of <a href="http://repetae.net/recent/out/classalias.html">class aliases</a>, but that proposal is a lot more involved than this one. Here we are only aiming to give long contexts a convenient short name.</p>
<p>During the <a href="http://www.haskell.org/haskellwiki/Hac5">fifth Haskell Hackathon</a>, a team of programmers (including myself) set out to implement an extension to GHC to make exactly this possible. We were aware of a trick that makes context synonyms already somewhat possible: create a new class and make the right-hand side of the context synonym the superclass constraints of the new type class:</p>
<pre>
class    (Fam phi, HFunctor phi (PF phi), Fold (PF phi)) => FoldFam phi
</pre>
<p>The problem with this approach, however, is that types for which you would like to use the <code>FoldFam</code> constraint are not automatically instances of this new class. Only last week it dawned on me that you can remedy this by supplying one big general instance that makes <em>every</em> type an instance:</p>
<pre>
instance (Fam phi, HFunctor phi (PF phi), Fold (PF phi)) => FoldFam phi
</pre>
<p>Such an instance is sometimes used as in <code>instance Monad f => Applicative f where ...</code>, but this doesn&#8217;t have the desired effect in Haskell. Our <code>FoldFam</code> instance, however, is <em>exactly</em> what we want: match any type with that instance, then check the constraints. This also means we cannot add any more specific instances, which is also what we want.</p>
<p>So it seems we were trying to implement a feature that already sort of exists. To make things worse, Erik pointed out that this use of type classes and instances is documented literally in the <a href="http://www.haskell.org/ghc/docs/6.0/html/users_guide/type-extensions.html#UNDECIDABLE-INSTANCES">GHC docs</a> as old as version 6.0! If only we had known during the Hackathon.</p>
<img src="http://feeds.feedburner.com/~r/MedeaMelana/~4/60i6JB9Fa3o" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://martijn.van.steenbergen.nl/journal/2009/10/11/context-synonyms/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://martijn.van.steenbergen.nl/journal/2009/10/11/context-synonyms/</feedburner:origLink></item>
		<item>
		<title>Heidetuin</title>
		<link>http://feedproxy.google.com/~r/MedeaMelana/~3/v1_zXUb4qA0/</link>
		<comments>http://martijn.van.steenbergen.nl/journal/2009/10/04/heidetuin/#comments</comments>
		<pubDate>Sun, 04 Oct 2009 12:29:36 +0000</pubDate>
		<dc:creator>Martijn</dc:creator>
				<category><![CDATA[Exploring]]></category>

		<guid isPermaLink="false">http://martijn.van.steenbergen.nl/journal/?p=413</guid>
		<description><![CDATA[
There&#8217;s a very pretty piece of forest in Driebergen called the Heidetuin (heath garden). Whenever I&#8217;m in the neighbourhood I pass through it because it&#8217;s so pretty, especially when the sun is out.



























































]]></description>
			<content:encoded><![CDATA[<img class="dbg_single" src="http://martijn.van.steenbergen.nl/galleries/singles/DSC_8136.jpg" alt="DSC_8136.jpg" />
<p>There&#8217;s a very pretty piece of forest in Driebergen called the <em>Heidetuin</em> (heath garden). Whenever I&#8217;m in the neighbourhood I pass through it because it&#8217;s so pretty, especially when the sun is out.</p>
<table cellspacing="0" cellpadding="0" class="dbg_gallery">
<tr>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/heidetuin/DSC_8130.jpg" rel="lightbox[heidetuin]" title="">
<img src="http://martijn.van.steenbergen.nl/thumbnails/heidetuin/DSC_8130.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/heidetuin/DSC_8130.jpg"/></a></div></div>
<div class="dbg_caption"></div>
</td>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/heidetuin/DSC_8134.jpg" rel="lightbox[heidetuin]" title="">
<img src="http://martijn.van.steenbergen.nl/thumbnails/heidetuin/DSC_8134.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/heidetuin/DSC_8134.jpg"/></a></div></div>
<div class="dbg_caption"></div>
</td>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/heidetuin/DSC_8136.jpg" rel="lightbox[heidetuin]" title="">
<img src="http://martijn.van.steenbergen.nl/thumbnails/heidetuin/DSC_8136.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/heidetuin/DSC_8136.jpg"/></a></div></div>
<div class="dbg_caption"></div>
</td>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/heidetuin/DSC_8138.jpg" rel="lightbox[heidetuin]" title="">
<img src="http://martijn.van.steenbergen.nl/thumbnails/heidetuin/DSC_8138.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/heidetuin/DSC_8138.jpg"/></a></div></div>
<div class="dbg_caption"></div>
</td>
</tr>
<tr>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/heidetuin/DSC_8139.jpg" rel="lightbox[heidetuin]" title="">
<img src="http://martijn.van.steenbergen.nl/thumbnails/heidetuin/DSC_8139.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/heidetuin/DSC_8139.jpg"/></a></div></div>
<div class="dbg_caption"></div>
</td>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/heidetuin/DSC_8145.jpg" rel="lightbox[heidetuin]" title="">
<img src="http://martijn.van.steenbergen.nl/thumbnails/heidetuin/DSC_8145.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/heidetuin/DSC_8145.jpg"/></a></div></div>
<div class="dbg_caption"></div>
</td>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/heidetuin/DSC_8149.jpg" rel="lightbox[heidetuin]" title="">
<img src="http://martijn.van.steenbergen.nl/thumbnails/heidetuin/DSC_8149.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/heidetuin/DSC_8149.jpg"/></a></div></div>
<div class="dbg_caption"></div>
</td>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/heidetuin/DSC_8154.jpg" rel="lightbox[heidetuin]" title="">
<img src="http://martijn.van.steenbergen.nl/thumbnails/heidetuin/DSC_8154.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/heidetuin/DSC_8154.jpg"/></a></div></div>
<div class="dbg_caption"></div>
</td>
</tr>
<tr>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/heidetuin/DSC_8157.jpg" rel="lightbox[heidetuin]" title="">
<img src="http://martijn.van.steenbergen.nl/thumbnails/heidetuin/DSC_8157.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/heidetuin/DSC_8157.jpg"/></a></div></div>
<div class="dbg_caption"></div>
</td>
<td>
<div class="dbg_picture_outer"><div class="dbg_picture_inner"><a href="http://martijn.van.steenbergen.nl/galleries/heidetuin/DSC_8163.jpg" rel="lightbox[heidetuin]" title="">
<img src="http://martijn.van.steenbergen.nl/thumbnails/heidetuin/DSC_8163.jpg" alt="http://martijn.van.steenbergen.nl/thumbnails/heidetuin/DSC_8163.jpg"/></a></div></div>
<div class="dbg_caption"></div>
</td>
</tr>
</table>

<img src="http://feeds.feedburner.com/~r/MedeaMelana/~4/v1_zXUb4qA0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://martijn.van.steenbergen.nl/journal/2009/10/04/heidetuin/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://martijn.van.steenbergen.nl/journal/2009/10/04/heidetuin/</feedburner:origLink></item>
	</channel>
</rss>
