<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	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:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>No tengo la menor idea</title>
	<atom:link href="http://greayer.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://greayer.wordpress.com</link>
	<description>A place to collect together everything I&#039;ve got going on...</description>
	<lastBuildDate>Tue, 11 May 2010 14:54:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='greayer.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>No tengo la menor idea</title>
		<link>http://greayer.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://greayer.wordpress.com/osd.xml" title="No tengo la menor idea" />
	<atom:link rel='hub' href='http://greayer.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Wreckage 0.0.0</title>
		<link>http://greayer.wordpress.com/2010/05/10/wreckage-0-0-0/</link>
		<comments>http://greayer.wordpress.com/2010/05/10/wreckage-0-0-0/#comments</comments>
		<pubDate>Mon, 10 May 2010 21:00:22 +0000</pubDate>
		<dc:creator>robgreayer</dc:creator>
				<category><![CDATA[haskell]]></category>

		<guid isPermaLink="false">http://greayer.wordpress.com/?p=234</guid>
		<description><![CDATA[Haskell 98 (and Haskell 2010) has a &#8216;sort of&#8217; record system, algegraic data type with named fields. The (unreleased, but githubbed) Wreckage library is yet another attempt to implement a better record system within Haskell, without sacrificing some of the nice features of the named fields system. There are a few other &#8216;embedded&#8217; record systems [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=greayer.wordpress.com&amp;blog=7862297&amp;post=234&amp;subd=greayer&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Haskell 98 (and Haskell 2010) has a &#8216;sort of&#8217; record system, algegraic data type with named fields. The (unreleased, but githubbed) <a href="http://github.com/rgreayer/wreckage">Wreckage library</a> is yet another attempt to implement a better record system within Haskell, without sacrificing some of the nice features of the named fields system. There are a few other &#8216;embedded&#8217; record systems out there, such as <a href="http://hackage.haskell.org/package/records"><em>records</em></a>, <a href="http://hackage.haskell.org/package/has"><em>has</em></a>, and the well known <a href="http://hackage.haskell.org/package/HList"><em>hlist</em></a>. Wreckage&#8217;s novelty is in its use of <a href="http://greayer.wordpress.com/2010/01/28/record-selector-punning-with-type-level-strings-2/">type level strings</a>. It is implemented with generous support from type families (to implement things like <a href="http://greayer.wordpress.com/2009/11/15/sorting-type-heterogeneous-values-with-type-families/">type/value heterogeneous list sorting</a>), Template Haskell, Quasi Quotation, and other non-standard features too numerous to mention. Indeed it is a veritable train-wreck of Haskell extensions, hence the name (&#8216;wreck&#8217; being homonymous with &#8216;rec&#8217;, shorthand for &#8216;record&#8217;. Hilarious, I know).</p>
<p>There are three key features of ADTs with named fields that would be a shame to sacrifice.</p>
<div id="construction-with-field-names">
<h3><strong>Construction With Field Names</strong></h3>
<p>Given an ADT with named fields, e.g.</p>
<pre><code>    <span style="color:blue;font-weight:bold;">data</span> <span>Person</span> <span style="color:red;">=</span> <span>Person</span> <span style="color:red;">{</span>
        <span>name</span> <span style="color:red;">::</span> <span>String</span><span style="color:red;">,</span>
        <span>age</span> <span style="color:red;">::</span> <span>!</span><span>Int</span>
    <span style="color:red;">}</span></code></pre>
<p>we can build a Person value using the field names, instead of relying on their positions within the record. We can supply the fields in any order, and can leave out non-strict fields:</p>
<pre><code>    <span>jane</span> <span style="color:red;">=</span> <span>Person</span> <span style="color:red;">{</span> <span>age</span> <span style="color:red;">=</span> <span class="hs-num">21</span><span style="color:red;">,</span> <span>name</span> <span style="color:red;">=</span> <span style="color:teal;">"Jane Doe"</span> <span style="color:red;">}</span>
    <span>john</span> <span style="color:red;">=</span> <span>Person</span> <span style="color:red;">{</span> <span>age</span> <span style="color:red;">=</span> <span class="hs-num">30</span> <span style="color:red;">}</span></code></pre>
</div>
<div id="access-and-update-with-field-names">
<h3><strong>Access and Update with Field Names</strong></h3>
<p>Given the definitions above, we can access components of a Person value with a label:</p>
<pre><code>    <span>janesAge</span> <span style="color:red;">=</span> <span>age</span> <span>jane</span></code></pre>
<p>and, with a special syntax, can update the fields of a person value:</p>
<pre><code><span>jim</span> <span style="color:red;">=</span> <span>john</span> <span style="color:red;">{</span> <span>name</span> <span style="color:red;">=</span> <span style="color:teal;">"Jim Beam"</span> <span style="color:red;">}</span>
<span>olderJane</span> <span style="color:red;">=</span> <span>jane</span> <span style="color:red;">{</span> <span>age</span> <span style="color:red;">=</span> <span>age</span> <span>jane</span> <span>+</span> <span class="hs-num">5</span> <span style="color:red;">}</span></code></pre>
<p>Although the above syntax for update is convenient, it doesn&#8217;t compose well for &#8216;deep&#8217; update (which is a problem Wreckage tries to resolve, in the style of <a href="http://hackage.haskell.org/package/fclabels"><em>fclabels</em></a>).</p>
</div>
<div id="pattern-matching-with-field-names">
<h3><strong>Pattern Matching with Field Names</strong></h3>
<p>Field names work nicely with Haskell pattern matching:</p>
<pre><code>    <span>nameAndAge</span> <span style="color:red;">(</span><span>Person</span> <span style="color:red;">{</span> <span>name</span> <span style="color:red;">=</span> <span>n</span><span style="color:red;">,</span> <span>age</span> <span style="color:red;">=</span> <span>i</span> <span style="color:red;">}</span><span style="color:red;">)</span> <span style="color:red;">=</span> <span>n</span> <span>++</span> <span style="color:teal;">": "</span> <span>+</span> <span>show</span> <span>i</span></code></pre>
</div>
<div id="wreckage-0.0.0">
<h2>Wreckage 0.0.0</h2>
<p>Wreckage attempts to preserve these features, in spirit, while still implementing basic records &#8216;under the covers&#8217; as simple algebraic data types. An equivalent Wreckage record would be defined as follows:</p>
<pre><code>    <span>wreck</span> <span style="color:red;">[</span><span>d</span><span style="color:red;">|</span>
        <span style="color:blue;font-weight:bold;">data</span> <span>Person</span> <span style="color:red;">{</span>
            <span>name</span> <span style="color:red;">::</span> <span>String</span><span style="color:red;">,</span>
            <span>age</span> <span style="color:red;">::</span> <span>!</span><span>Int</span> <span style="color:red;">}</span>
        <span style="color:red;">|</span><span style="color:red;">]</span></code></pre>
<p>The declaration above creates a &#8216;Person&#8217; data type with two fields, along with a constructor &#8216;mkPerson&#8217;, and many (many) instances of various type classes that will make record construction and destruction &#8216;work&#8217;.</p>
<p>Before showing examples of how to use Wreckage records, I&#8217;ll expand the example to illustrate one key feature of wreckage:</p>
<pre><code>    <span>wreck</span> <span style="color:red;">[</span><span>d</span><span style="color:red;">|</span>
        <span style="color:blue;font-weight:bold;">data</span> <span>Pet</span> <span style="color:red;">{</span>
            <span>name</span> <span style="color:red;">::</span> <span>String</span><span style="color:red;">,</span>
            <span>age</span> <span style="color:red;">::</span> <span>!</span><span>Int</span>
        <span style="color:red;">}</span><span style="color:red;">|</span><span style="color:red;">]</span>

    <span>wreck</span> <span style="color:red;">[</span><span>d</span><span style="color:red;">|</span>
        <span style="color:blue;font-weight:bold;">data</span> <span>Person</span> <span style="color:red;">{</span>
            <span>name</span> <span style="color:red;">::</span> <span>String</span><span style="color:red;">,</span>
            <span>age</span> <span style="color:red;">::</span> <span>!</span><span>Int</span><span style="color:red;">,</span>
            <span>pet</span> <span style="color:red;">::</span> <span>Maybe</span> <span>Pet</span>
        <span style="color:red;">}</span><span style="color:red;">|</span><span style="color:red;">]</span></code></pre>
<p>As plain data declarations, these would be illegal in Haskell, because of the reuse of the names &#8216;name&#8217; and &#8216;age&#8217; between the two records. Wreckage lifts this restriction. Field names can be reused among records, regardless of what scope the wreckage record is declared in.</p>
<p>Now we can construct values of type Person and Pet:</p>
<pre><code>    <span>fido</span> <span style="color:red;">=</span> <span>mkPet</span> <span style="color:red;">(</span> <span>ℓname</span> <span>=:</span> <span style="color:teal;">"Fido"</span> <span>:*</span> <span>ℓage</span> <span>=:</span> <span class="hs-num">7</span> <span>:*</span> <span>()</span><span style="color:red;">)</span>
    <span>jane</span> <span style="color:red;">=</span> <span>mkPerson</span> <span style="color:red;">(</span> <span>ℓage</span> <span>=:</span> <span class="hs-num">21</span> <span>:*</span> <span>ℓname</span> <span style="color:red;">=</span> <span style="color:teal;">"Jane Doe"</span> <span>:*</span> <span>()</span><span style="color:red;">)</span></code></pre>
<p>The syntax for construction is slightly different, but has the same essential features: we need only specify strict fields at the time of construction (not specifying a strict field would be a type error), and can specify the fields in any order. The ℓ-notation (which works by leveraging a GHC compiler feature, rather than a Haskell language extension) will be explained fully later, but it introduces a <a href="http://greayer.wordpress.com/2010/01/28/record-selector-punning-with-type-level-strings-2/">type-level string</a>, which is used instead of a normal identifier to represent a field name.</p>
<p>Access and update of Wreckage records is straightforward if slightly different than with ADTs with named fields:</p>
<pre><code>    <span>janesAge</span> <span style="color:red;">=</span> <span>get</span> <span>ℓage</span> <span>jane</span>
    <span>janesPetsAge</span> <span style="color:red;">=</span> <span>get</span> <span style="color:red;">(</span><span>ℓpet</span><span>:-</span><span>ℓage</span><span style="color:red;">)</span> <span>jane</span>

    <span style="color:green;">-- change jane's pet's name</span>
    <span>jane'</span> <span style="color:red;">=</span> <span>set</span> <span style="color:red;">(</span><span>ℓpet</span><span>:-</span><span>ℓage</span><span style="color:red;">)</span> <span>jane</span> <span class="hs-num">8</span></code></pre>
<p>Fields can be composed, or chained, together for both access and update, as shown above. Multiple fields can be accessed simultaneously:</p>
<pre><code>    <span style="color:red;">(</span><span>nm</span> <span>:*</span> <span>age</span><span style="color:red;">)</span> <span style="color:red;">=</span> <span>get</span> <span style="color:red;">(</span><span>ℓname</span> <span>:*</span> <span>ℓage</span><span style="color:red;">)</span> <span>jane</span></code></pre>
<p>Without much effort upon the part of the Wreckage library, we can pattern match on Wreckage records using the View Patterns extension, implemented in GHC:</p>
<pre><code>    <span>nameAndAge</span> <span style="color:red;">(</span><span>get</span> <span style="color:red;">(</span><span>ℓname</span><span>:*</span><span>ℓage</span><span style="color:red;">)</span> <span style="color:red;">-&gt;</span> <span style="color:red;">(</span><span>n</span><span>:*</span><span>i</span><span style="color:red;">)</span><span style="color:red;">)</span> <span style="color:red;">=</span> <span>nm</span> <span>++</span> <span style="color:teal;">": "</span> <span>++</span> <span>show</span> <span>i</span></code></pre>
<p>Note that the &#8216;nameAndAge&#8217; function above works both on fido and on jane (i.e. on the Person type and on the Pet type). Or, indeed, on <strong>any other wreckage record type</strong>, declared in any module in any package, that happens to also have a <em>name</em> field and and <em>age</em> field. With other &#8216;embedded&#8217; record systems, an &#8216;age&#8217; field defined in one module will clash with an &#8216;age&#8217; field defined in another module, however the fields are defined.</p>
<p>Person and Pet are (under the covers) just algebraic data types (without field labels), so access and update ought to be about as efficient as with standard field labels.</p>
<div id="wreckage-and-extensability">
<h3><strong>Wreckage and extensability</strong></h3>
<p>Wreckage is intended to have a good story regarding exstensibility, implementing something like <a href="http://research.microsoft.com/en-us/um/people/daan/download/papers/scopedlabels.pdf">scoped labels</a>. Right now, the implementation is rudimentary, with the addition of just two operations: <code>extend</code> and <code>restrict</code> (under the covers extension works right now much like <a href="http://hackage.haskell.org/package/HList">HList</a>, but I intend future versions to work a bit differently). A Wreckage record can be extended with addtional fields:</p>
<pre><code>    <span>janeV2</span> <span style="color:red;">=</span> <span>jane</span> <span>`extend`</span> <span style="color:red;">(</span><span>ℓheight</span> <span>=:</span> <span class="hs-num">1.7</span><span style="color:red;">)</span>

    <span>janesHeight</span> <span style="color:red;">=</span> <span>get</span> <span>ℓheight</span> <span>janeV2</span></code></pre>
<p>A field can be removed from a record:</p>
<pre><code>    <span>janeV3</span> <span style="color:red;">=</span> <span>janeV2</span> <span>`restrict`</span> <span>ℓage</span>

    <span>s</span> <span style="color:red;">=</span> <span>nameAndAge</span> <span>janeV3</span> <span style="color:green;">-- type error!</span></code></pre>
<p>Records are &#8216;scoped&#8217; in that you can extend a record with a field name it already contains. The new field simply hides the existing field (until the field is removed via &#8216;restrict&#8217;).</p>
</div>
<div id="the-egregious-hack">
<h3><strong>The egregious hack</strong></h3>
<p>The ℓ notation used above &#8216;works&#8217; in GHC and GHCI through the use of the Haskell preprocessor feature of GHC. A program can be passed to GHC or GHCI, which is used to transform each .hs file just prior to compilation (and after CPP has possibly been run on the source):</p>
<pre><code>    ghci -F -cmdF prewreck</code></pre>
<p>The <em>prewreck</em> program (included with wreckage) transforms and input file by changing any ℓ sequence into a quasi-quotation. The quasi-quotation could be used directly, but the ℓ-notation is more readable. One day something <a href="http://article.gmane.org/gmane.comp.lang.haskell.glasgow.user/18108">almost as nice</a> might work out of the box with GHC.</p>
</div>
<div id="caveats">
<h3><strong>Caveats</strong></h3>
<p>Wreckage may have exposed a performance issue with the GHC compiler (6.12.1 or 6.12.2) in that there&#8217;s an exponential blowup of memory used by the compiler when trying to <em>optimize</em> (-O1) a wreckage client (no problem with the library itself). Turning off optimization (-O0) and clients compile quickly.</p>
<p>Wreckage doesn&#8217;t try to deal with polymorphic records, yet. There&#8217;s no technical difficulty with them, but the first implementation of the <code>wreck</code> function was made simpler by ignoring them, for now.</p>
</div>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/greayer.wordpress.com/234/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/greayer.wordpress.com/234/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/greayer.wordpress.com/234/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/greayer.wordpress.com/234/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/greayer.wordpress.com/234/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/greayer.wordpress.com/234/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/greayer.wordpress.com/234/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/greayer.wordpress.com/234/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/greayer.wordpress.com/234/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/greayer.wordpress.com/234/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/greayer.wordpress.com/234/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/greayer.wordpress.com/234/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/greayer.wordpress.com/234/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/greayer.wordpress.com/234/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=greayer.wordpress.com&amp;blog=7862297&amp;post=234&amp;subd=greayer&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://greayer.wordpress.com/2010/05/10/wreckage-0-0-0/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/236fba8deba62e3deb53189335900f35?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">robgreayer</media:title>
		</media:content>
	</item>
		<item>
		<title>Record Selector Punning with Type-Level Strings</title>
		<link>http://greayer.wordpress.com/2010/01/28/record-selector-punning-with-type-level-strings-2/</link>
		<comments>http://greayer.wordpress.com/2010/01/28/record-selector-punning-with-type-level-strings-2/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 04:16:50 +0000</pubDate>
		<dc:creator>robgreayer</dc:creator>
				<category><![CDATA[haskell]]></category>

		<guid isPermaLink="false">http://greayer.wordpress.com/?p=219</guid>
		<description><![CDATA[Haskell users occasionally complain about the language&#8217;s lack of a powerful record system. Programmers want exstensibilty, a different approach to the scope of field names, and a better update syntax than Haskell&#8217;s algebraic data types, enhanced with field labels, provide. There are some proposals, such as this one, for new Haskell record systems. There are [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=greayer.wordpress.com&amp;blog=7862297&amp;post=219&amp;subd=greayer&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Haskell users occasionally <a href="http://blog.omega-prime.co.uk/?p=6">complain</a> about the language&#8217;s lack of a powerful record system. Programmers want exstensibilty, a different approach to the scope of field names, and a better update syntax than Haskell&#8217;s algebraic data types, enhanced with field labels, provide.</p>
<p>There are some proposals, such as <a href="http://research.microsoft.com/en-us/um/people/daan/download/papers/scopedlabels.pdf">this one</a>, for new Haskell record systems. There are alternative record system <a href="http://cvs.haskell.org/Hugs/pages/hugsman/exts.html#sect7.2">implementations</a>, though none in Haskell&#8217;s <a href="http://haskell.org/ghc/">most popular compiler</a>. There are elaborate exploitations of Haskell&#8217;s powerful type system to create record systems (such as <a href="http://homepages.cwi.nl/~ralf/HList/paper.pdf">HList</a>) or even object oriented libraries (such as <a href="http://homepages.cwi.nl/~ralf/OOHaskell/paper.pdf">OO Haskell</a>). And there are hacks and kludges to work around one or more limitations of the existing record system. This post falls into this last category.</p>
<p>The referenced <a href="http://blog.omega-prime.co.uk/?p=6">complaint</a> points out that the scoping of Haskell record selectors can be annoying. If these were <em>real</em> records (based on expectations of records from other languages), something like this would work:</p>
<pre><code><span style="color:blue;font-weight:bold;">data</span> <span>User</span> <span style="color:red;">=</span> <span>User</span> <span style="color:red;">{</span>
        <span>name</span> <span style="color:red;">::</span> <span>String</span><span style="color:red;">,</span>
        <span>host</span> <span style="color:red;">::</span> <span>Host</span>
    <span style="color:red;">}</span>

<span style="color:blue;font-weight:bold;">data</span> <span>Host</span> <span style="color:red;">=</span> <span>Host</span> <span style="color:red;">{</span>
        <span>name</span> <span style="color:red;">::</span> <span>String</span><span style="color:red;">,</span>
        <span>address</span> <span style="color:red;">::</span> <span>String</span>
    <span style="color:red;">}</span></code></pre>
<p>but it doesn&#8217;t because of scoping problems: each of the record selectors is a function, and I now have two <code>name</code> functions of different types. One solution (valid Haskell98) is to stick these definitions in separate modules, and import them qualified (e.g. <code>import qualified Host.Host as H</code>). Every reference to Host&#8217;s <code>name</code> would become (something like) <code>H.name</code>, and every reference to User&#8217;s <code>name</code> would become <code>U.name</code>. This solution is unpalatable enough to some that there exists a Haskell language proposal for <a href="http://hackage.haskell.org/trac/haskell-prime/wiki/TypeDirectedNameResolution">type directed name resolution</a> which would solve specifically this problem, introducing some new syntax along the way. Another (much maligned) workaround for the problem is to use Haskell type classes to achieve record selector overloading. In the example above, the simplest approach would look something like:</p>
<pre><code><span style="color:blue;font-weight:bold;">data</span> <span>User</span> <span style="color:red;">=</span> <span>User</span> <span>String</span> <span>Host</span>
<span style="color:blue;font-weight:bold;">data</span> <span>Host</span> <span style="color:red;">=</span> <span>Host</span> <span>String</span> <span>String</span>

<span style="color:blue;font-weight:bold;">class</span> <span>HasName</span> <span>a</span> <span style="color:blue;font-weight:bold;">where</span>
    <span>name</span> <span style="color:red;">::</span> <span>a</span> <span style="color:red;">-&gt;</span> <span>String</span>

<span style="color:blue;font-weight:bold;">class</span> <span>HasHost</span> <span>a</span> <span style="color:blue;font-weight:bold;">where</span>
    <span>host</span> <span style="color:red;">::</span> <span>a</span> <span style="color:red;">-&gt;</span> <span>Host</span>

<span style="color:green;">-- etc.</span>
<span style="color:blue;font-weight:bold;">instance</span> <span>HasName</span> <span>User</span> <span style="color:blue;font-weight:bold;">where</span>
    <span>name</span> <span style="color:red;">(</span><span>User</span> <span>v</span> <span style="color:blue;font-weight:bold;">_</span><span style="color:red;">)</span> <span style="color:red;">=</span> <span>v</span>
<span style="color:blue;font-weight:bold;">instance</span> <span>HasName</span> <span>Host</span> <span style="color:blue;font-weight:bold;">where</span>
    <span>name</span> <span style="color:red;">(</span><span>Host</span> <span>v</span> <span style="color:blue;font-weight:bold;">_</span> <span style="color:red;">)</span> <span style="color:red;">=</span> <span>v</span></code></pre>
<p>To remove the monomorphic-ness of the record selector result type, I could use Haskell&#8217;s bright, shiny, relatively new associated types extension, or its hoary-but-still-not-standard multi-parameter type classes with functional dependencies extension. Here&#8217;s how I might enhance the above with functional dependencies:</p>
<pre><code><span style="color:blue;font-weight:bold;">class</span> <span>HasName</span> <span>a</span> <span>b</span> <span style="color:red;">|</span> <span>a</span> <span style="color:red;">-&gt;</span> <span>b</span> <span style="color:blue;font-weight:bold;">where</span>
    <span>name</span> <span style="color:red;">::</span> <span>a</span> <span style="color:red;">-&gt;</span> <span>b</span>

<span style="color:blue;font-weight:bold;">instance</span> <span>HasName</span> <span>User</span> <span>String</span> <span style="color:blue;font-weight:bold;">where</span>
    <span>name</span> <span style="color:red;">(</span><span>User</span> <span>v</span> <span style="color:blue;font-weight:bold;">_</span><span style="color:red;">)</span> <span style="color:red;">=</span> <span>v</span></code></pre>
<p>But still, for every record selector name, I need a type class. Further, even though I&#8217;d like to use records from different modules and packages together, seamlessly, I won&#8217;t be able to; I&#8217;ll end up with multiple versions of equivalent type classes &#8212; each author that wants to name a selector <code>name</code> will have to define a <code>HasName</code> type class, and the selector defined in that type class won&#8217;t be useful for pulling <code>name</code> values out of records defined by another author in another package. If this convention were followed universally, there&#8217;d be thousands of pitiful little type classes in different namespaces scattered across Haskell&#8217;s package space. Unpleasant.</p>
<p>So I&#8217;d like to contribute my (as far as I know) own variation on the type-class- driven approach, which, be it hacky, kludgy, or otherwise strange, at least doesn&#8217;t have the problem of creating thousands of incompatible type classes. Indeed, central to the solution is just one type class:</p>
<pre><code><span style="color:blue;font-weight:bold;">class</span> <span>Selection</span> <span>a</span> <span>b</span> <span style="color:blue;font-weight:bold;">where</span>
    <span style="color:blue;font-weight:bold;">type</span> <span>SelectionType</span> <span>a</span> <span>b</span>
    <span>select</span> <span style="color:red;">::</span> <span>b</span> <span style="color:red;">-&gt;</span> <span>a</span> <span style="color:red;">-&gt;</span> <span>SelectionType</span> <span>a</span> <span>b</span></code></pre>
<p>This class definition requires two exstensions to Haskell: <code>MultiParamTypeClasses</code> and <code>TypeFamilies</code>. Also central to the solution is the creation of a <em>type level alphabet</em> of characters. To represent a few letters I might define:</p>
<pre><code><span style="color:blue;font-weight:bold;">data</span> <span>Ca</span> <span style="color:red;">=</span> <span>Ca</span>
<span style="color:blue;font-weight:bold;">data</span> <span>Cb</span> <span style="color:red;">=</span> <span>Cb</span>
<span style="color:blue;font-weight:bold;">data</span> <span>Cc</span> <span style="color:red;">=</span> <span>Cc</span>
<span style="color:green;">-- etc.</span></code></pre>
<p>To represent <em>all</em> the letters of the alphabet (that can actually be used in Haskell identifiers), I can do something like this:</p>
<pre><code><span style="color:blue;font-weight:bold;">let</span> <span>f</span> <span>i</span> <span style="color:red;">=</span> <span style="color:blue;font-weight:bold;">let</span> <span>nm</span> <span style="color:red;">=</span> <span>mkName</span> <span>$</span> <span style="color:red;">(</span><span style="color:teal;">"C"</span> <span>++</span> <span>show</span> <span style="color:red;">(</span><span>fromEnum</span> <span>i</span><span style="color:red;">)</span><span style="color:red;">)</span> <span style="color:blue;font-weight:bold;">in</span> <span>dataD</span> <span style="color:red;">(</span><span>return</span> <span>[]</span><span style="color:red;">)</span> <span>nm</span> <span>[]</span> <span style="color:red;">[</span><span>normalC</span> <span>nm</span> <span>[]</span><span style="color:red;">]</span> <span>[]</span>
      <span style="color:blue;font-weight:bold;">in</span> <span>sequence</span> <span>$</span> <span>map</span> <span>f</span> <span>$</span> <span style="color:teal;">'\''</span><span>:</span><span style="color:red;">[</span> <span>c</span> <span style="color:red;">|</span> <span>c</span> <span style="color:red;">&lt;-</span> <span style="color:red;">[</span><span>minBound</span> <span style="color:red;">..</span> <span>maxBound</span><span style="color:red;">]</span><span style="color:red;">,</span> <span>isIDStart</span> <span>c</span> <span>||</span> <span>isIDContinue</span> <span>c</span> <span style="color:red;">]</span></code></pre>
<p>The above expression requires <code>TemplateHaskell</code> extension (supported only by <a href="http://haskell.org/ghc/">GHC</a>) and at least GHC 6.12.x. The above snippet also uses the <a href="http://hackage.haskell.org/package/unicode-properties">unicode-properties</a> package from <a href="http://hackage.haskell.org/">hackage</a>, and generates on the order of 90,000 new types. Ideally (for some values of &#8216;ideal&#8217;) I would generate a type for every possible character, and not filter by <code>isIDStart</code> and <code>isIDContinue</code>, but doing so generates a few million types, which seemed to make ghci unhappy; consequently, I limited my alphabet to characters relevant to mimicking haskell identifiers. (Note also, &#8216;a&#8217; becomes C97 rather than Ca).</p>
<p>A type level string is some a product of type level characters. The type-level string for &quot;hello&quot; would be <code>C150 :* C101 :* C108 :* C108 :* C111 :* ()</code>, where <code>:*</code> is a right-associative infix type constructor (requiring the <code>TypeOperators</code> extension) and <code>()</code> is used to mean an empty product/list.</p>
<p>Now I can define record selectors using type level strings and my single <code>Selection</code> type class. Defining one &#8216;by hand&#8217; for the &quot;name&quot; selector would look like:</p>
<pre><code><span style="color:blue;font-weight:bold;">instance</span> <span>Selection</span> <span>User</span> <span style="color:red;">(</span><span>C110</span> <span>:*</span> <span>C97</span> <span>:*</span> <span>C109</span> <span>:*</span> <span>C101</span> <span>:*</span> <span>()</span><span style="color:red;">)</span> <span style="color:blue;font-weight:bold;">where</span>
    <span style="color:blue;font-weight:bold;">type</span> <span>SelectionType</span> <span>User</span> <span style="color:red;">(</span><span>C110</span> <span>:*</span> <span>C97</span> <span>:*</span> <span>C109</span> <span>:*</span> <span>C101</span> <span>:*</span> <span>()</span><span style="color:red;">)</span> <span style="color:red;">=</span> <span>String</span>
    <span>select</span> <span style="color:blue;font-weight:bold;">_</span> <span style="color:red;">(</span><span>User</span> <span>v</span> <span style="color:blue;font-weight:bold;">_</span> <span style="color:blue;font-weight:bold;">_</span><span style="color:red;">)</span> <span style="color:red;">=</span> <span>v</span></code></pre>
<p>Of course, I wouldn&#8217;t want to define the above instance by hand; I&#8217;d use Template Haskell once again to create the instance. Template Haskell is capable, as of GHC 6.12.1, of generating a class instance with an associated type. If I go back to my original definition of <code>User</code>:</p>
<pre><code><span style="color:blue;font-weight:bold;">data</span> <span>User</span> <span style="color:red;">=</span> <span>User</span> <span style="color:red;">{</span>
    <span>name</span> <span style="color:red;">::</span> <span>String</span><span style="color:red;">,</span>
    <span>host</span> <span style="color:red;">::</span> <span>Host</span>
    <span style="color:red;">}</span></code></pre>
<p>I can write a Template Haskell function that takes the name of my type, determines the names of the desired selectors, and generates the necessary instances. I won&#8217;t present the (somewhat tedious) TH code, but a function can be written such as:</p>
<pre><code><span>mkSelectionInstances</span> <span style="color:red;">::</span> <span>Name</span> <span style="color:red;">-&gt;</span> <span style="color:green;">-- the name of the record type</span>
                        <span>Q</span> <span style="color:red;">[</span><span>Dec</span><span style="color:red;">]</span> <span style="color:green;">-- the Haskell declarations</span></code></pre>
<p>that will generate the required instances. It makes use of a <code>selToType</code> function which creates a selector name type given a selector name string:</p>
<pre><code><span>selToType</span> <span style="color:red;">::</span> <span>String</span> <span style="color:red;">-&gt;</span> <span>Q</span> <span>Type</span>
<span>selToType</span> <span>cs</span> <span style="color:red;">=</span> <span>foldl</span> <span>appT</span> <span style="color:red;">(</span><span>conT</span> <span style="color:teal;">'</span><span style="color:teal;">'</span><span>()</span><span style="color:red;">)</span> <span>$</span> <span>map</span> <span>f</span> <span>cs</span>
    <span style="color:blue;font-weight:bold;">where</span> <span>f</span> <span>c</span> <span style="color:red;">=</span> <span style="color:red;">(</span><span>conT</span> <span style="color:teal;">'</span><span style="color:teal;">'</span><span style="color:red;">(</span><span>:*</span><span style="color:red;">)</span><span style="color:red;">)</span> <span>`appT`</span> <span style="color:red;">(</span><span>conT</span> <span>$</span> <span>mkName</span> <span>$</span> <span style="color:teal;">"C"</span> <span>++</span> <span>show</span> <span style="color:red;">(</span><span>fromEnum</span> <span>c</span><span style="color:red;">)</span><span style="color:red;">)</span></code></pre>
<p>I&#8217;m still saddled with a few problems, perhaps the least of which is that I still cannot define two records in the same scope with the same selector names. But, because I am discarding (or at least not planning to use) the &#8216;real&#8217; selectors, I can adopt a convention for naming selectors that the <code>mkSelectionInstances</code> function will understand, and thus generate the selectors I really want.</p>
<p>For example, the convention could be &quot;when creating type-level selectors, ignore everything in the name from the last tick (&#8216;) mark to the end of the identifier&quot;. I would then define my records like this:</p>
<pre><code><span style="color:blue;font-weight:bold;">data</span> <span>User</span> <span style="color:red;">=</span> <span>User</span> <span style="color:red;">{</span>
        <span>name'u</span> <span style="color:red;">::</span> <span>String</span><span style="color:red;">,</span>
        <span>host'u</span> <span style="color:red;">::</span> <span>Host</span>
    <span style="color:red;">}</span>

<span style="color:blue;font-weight:bold;">data</span> <span>Host</span> <span style="color:red;">=</span> <span>Host</span> <span style="color:red;">{</span>
        <span>name'h</span> <span style="color:red;">::</span> <span>String</span><span style="color:red;">,</span>
        <span>address'h</span> <span style="color:red;">::</span> <span>String</span>
    <span style="color:red;">}</span>

<span>mkSelectionInstances</span> <span style="color:teal;">'</span><span style="color:teal;">'</span><span>User</span>
<span>mkSelectionInstances</span> <span style="color:teal;">'</span><span style="color:teal;">'</span><span>Host</span></code></pre>
<p>and <code>mkSelectionInstances</code> will create <code>name</code> and <code>host</code> selector instances for <code>User</code>, and <code>name</code> and <code>address</code> selector instances for <code>Host</code>. I could now define (&#8216;by hand&#8217;) aliases for the selectors:</p>
<pre><code><span>name</span> <span style="color:red;">=</span> <span>select</span> <span style="color:red;">(</span><span>C110</span> <span>:*</span> <span>C97</span> <span>:*</span> <span>C109</span> <span>:*</span> <span>C101</span> <span>:*</span> <span>()</span><span style="color:red;">)</span>
<span>host</span> <span style="color:red;">=</span> <span>select</span> <span style="color:red;">(</span><span>C104</span> <span>:*</span> <span>C111</span> <span>:*</span> <span>C115</span> <span>:*</span> <span>C116</span> <span>:*</span> <span>()</span><span style="color:red;">)</span>
<span>address</span> <span style="color:red;">=</span> <span>select</span> <span style="color:red;">(</span><span>C97</span> <span>:*</span> <span>C100</span> <span>:*</span> <span>C100</span> <span>:*</span> <span>C114</span> <span>:*</span> <span>C101</span> <span>:*</span> <span>C115</span> <span>:*</span> <span>C115</span> <span>:*</span> <span>()</span><span style="color:red;">)</span></code></pre>
<p><code>name</code> can now be used to select the name component of a <code>User</code> value or a <code>Host</code> value, or any other value of a type <code>t</code> with a <code>Selection t C110 :* C97 :* C109 :* C101 :* ())</code> instance, no matter where it has been defined:</p>
<pre><code><span>cory</span> <span style="color:red;">=</span> <span>Host</span> <span style="color:teal;">"cory.EECS.Berkeley.EDU"</span> <span class="hs-num">128.32</span><span>.</span><span class="hs-num">48.187</span>
<span>joe</span> <span style="color:red;">=</span> <span>User</span> <span style="color:teal;">"Joe"</span> <span>cory</span>

<span>joesName</span> <span style="color:red;">=</span> <span>name</span> <span>joe</span>
<span>joesHostName</span> <span style="color:red;">=</span> <span>name</span> <span>$</span> <span>host</span> <span>$</span> <span>joe</span></code></pre>
<p>Still, there is a problem: where <em>should</em> the the <code>name</code>, <code>host</code>, and <code>address</code> selectors be defined? I certainly don&#8217;t want to have to actually define them by hand, but if I generate them in the module where I generate the selector instances, I run into problems.</p>
<p>Assume I define name/host/address as above and export them, from module <code>A</code>. Another module, <code>B</code> defines similar selectors (<code>name</code> and <code>address</code>, say, and some other unrelated selectors). A third module <code>C</code> wants to use the records defined in modules <code>A</code> and <code>B</code>. It doesn&#8217;t matter whether it imports <code>name</code> and <code>address</code> from <code>A</code> or from <code>B</code>, but it must only import one definition. This restriction places an annoying burden on the author of <code>C</code>.</p>
<p>The approach is, nevertheless, one possible answer to the question of where to define identifiers representing my selectors. I&#8217;ve come up with two other answers, which I&#8217;ll explore below.</p>
<div id="alternative-1-import-identifiers-using-a-th-splice">
<h2>Alternative 1: &#8216;Import&#8217; identifiers using a TH splice</h2>
<p>Instead of defining the selector identifiers in the module that defines the record type(s) and the <code>Selector</code> instances for that type, use a TH function to &#8216;import&#8217; the selectors in the module where they are used. For example, if I have a module <code>C</code> that imports records from modules <code>A</code> and <code>B</code>, I&#8217;d do something like:</p>
<pre><code><span style="color:blue;font-weight:bold;">module</span> <span>C</span> <span style="color:blue;font-weight:bold;">where</span>

<span style="color:green;">-- the module that contains my selection class, etc.</span>
<span style="color:blue;font-weight:bold;">import</span> <span>Data</span><span>.</span><span>Record</span><span>.</span><span>Selection</span>
<span style="color:blue;font-weight:bold;">import</span> <span>A</span><span style="color:red;">(</span><span>User</span><span style="color:red;">(</span><span>User</span><span style="color:red;">)</span><span style="color:red;">,</span><span>Host</span><span style="color:red;">(</span><span>Host</span><span style="color:red;">)</span><span style="color:red;">)</span>
<span style="color:blue;font-weight:bold;">import</span> <span>B</span><span style="color:red;">(</span><span>Student</span><span style="color:red;">(</span><span>Student</span><span style="color:red;">)</span><span style="color:red;">)</span>

<span>importSelectors</span> <span style="color:red;">(</span><span>record</span><span style="color:red;">::</span><span>User</span><span style="color:red;">)</span> <span style="color:red;">(</span><span>record</span><span style="color:red;">::</span><span>Host</span><span style="color:red;">)</span> <span style="color:red;">(</span><span>record</span><span style="color:red;">::</span><span>Student</span><span style="color:red;">)</span></code></pre>
<p>I have one function, <code>importSelectors</code>, that, given a few record types, will generate the necessary selector function definitions, taking care not to generate a selector definition more than once if more than one of the specified records has a particular named selector. Even though <code>User</code> and <code>Host</code>, and perhaps <code>Student</code> each have a &#8216;name&#8217; selector, <code>importSelectors</code> must generate only one <code>name</code> function (<code>name = select (C110 :* C97 :* C109 :* C101 :* ())</code>).</p>
<p>For this magic to work, I need a couple more type classes:</p>
<pre><code><span style="color:blue;font-weight:bold;">class</span> <span>HasSelectors</span> <span>a</span> <span style="color:blue;font-weight:bold;">where</span>
    <span>record</span> <span style="color:red;">::</span> <span>a</span>
    <span>selectors</span> <span style="color:red;">::</span> <span>a</span> <span style="color:red;">-&gt;</span> <span style="color:red;">[</span><span>String</span><span style="color:red;">]</span>
    <span>record</span> <span style="color:red;">=</span> <span>undefined</span>

<span style="color:blue;font-weight:bold;">class</span> <span>Importer</span> <span>a</span> <span style="color:blue;font-weight:bold;">where</span>
    <span>importGen</span> <span style="color:red;">::</span> <span style="color:red;">[</span><span>String</span><span style="color:red;">]</span> <span style="color:red;">-&gt;</span> <span>a</span></code></pre>
<p><code>HasSelectors</code> has an instance for each record type, and allows my &#8216;importer&#8217; to get a list of selector names for each record. The <code>mkSelectionInstances</code> function would also generate the instance for this class, for a particular record. The <code>record</code> member of <code>HasSelectors</code> exists just to make the <code>importSelectors</code> call at the top of a module look nicer&#8230; I could just as easily write:</p>
<pre><code><span>importSelectors</span> <span style="color:red;">(</span><span>undefined</span><span style="color:red;">::</span><span>User</span><span style="color:red;">)</span> <span style="color:red;">(</span><span>undefined</span><span style="color:red;">::</span><span>Host</span><span style="color:red;">)</span> <span style="color:red;">(</span><span>undefined</span><span style="color:red;">::</span><span>Student</span><span style="color:red;">)</span></code></pre>
<p>The <code>Importer</code> has two instances (requiring <code>OverlappingInstances</code>):</p>
<pre><code><span style="color:blue;font-weight:bold;">instance</span> <span>Importer</span> <span style="color:red;">(</span><span>Q</span> <span style="color:red;">[</span><span>Dec</span><span style="color:red;">]</span><span style="color:red;">)</span> <span style="color:blue;font-weight:bold;">where</span>
    <span>importGen</span> <span>ss</span> <span style="color:red;">=</span> <span>liftM2</span> <span style="color:red;">(</span><span>++</span><span style="color:red;">)</span> <span style="color:red;">(</span><span>mapM</span> <span>gent</span> <span>ss'</span><span style="color:red;">)</span> <span style="color:red;">(</span><span>mapM</span> <span>gen</span> <span>ss'</span><span style="color:red;">)</span> <span style="color:blue;font-weight:bold;">where</span>
        <span>ss'</span> <span style="color:red;">=</span> <span>nub</span> <span>ss</span>
        <span>gent</span> <span>s</span> <span style="color:red;">=</span> <span>sigD</span> <span>nm</span> <span style="color:red;">[</span><span>t</span><span style="color:red;">|</span> <span>Selection</span> <span>a</span> <span>$</span><span>t</span> <span style="color:red;">=&gt;</span> <span>a</span> <span style="color:red;">-&gt;</span> <span>SelectionType</span> <span>a</span> <span>$</span><span>t</span> <span style="color:red;">|</span><span style="color:red;">]</span> <span style="color:blue;font-weight:bold;">where</span>
            <span>nm</span> <span style="color:red;">=</span> <span>mkName</span> <span>s</span>
            <span>t</span> <span style="color:red;">=</span> <span>return</span> <span>$</span> <span>selToType</span> <span>s</span>
        <span>gen</span> <span>s</span> <span style="color:red;">=</span> <span>valD</span> <span style="color:red;">(</span><span>varP</span> <span>nm</span><span style="color:red;">)</span> <span style="color:red;">(</span><span>normalB</span> <span style="color:red;">[</span><span>e</span><span style="color:red;">|</span><span>select</span> <span style="color:red;">(</span><span>undefined</span> <span style="color:red;">::</span> <span>$</span><span>t</span><span style="color:red;">)</span><span style="color:red;">|</span><span style="color:red;">]</span><span style="color:red;">)</span> <span>[]</span> <span style="color:blue;font-weight:bold;">where</span>
            <span>nm</span> <span style="color:red;">=</span> <span>mkName</span> <span>s</span>
            <span>t</span> <span style="color:red;">=</span> <span>return</span> <span>$</span> <span>selToType</span> <span>s</span>

<span style="color:blue;font-weight:bold;">instance</span> <span style="color:red;">(</span><span>HasSelectors</span> <span>a</span><span style="color:red;">,</span> <span>Importer</span> <span>b</span><span style="color:red;">)</span> <span style="color:red;">=&gt;</span> <span>Importer</span> <span style="color:red;">(</span><span>a</span> <span style="color:red;">-&gt;</span> <span>b</span><span style="color:red;">)</span> <span style="color:blue;font-weight:bold;">where</span>
    <span>importGen</span> <span>ss</span> <span style="color:red;">=</span> <span style="color:red;">(</span><span style="color:red;">\</span> <span>h</span> <span style="color:red;">-&gt;</span> <span>importGen</span> <span style="color:red;">(</span><span>selectors</span> <span>h</span> <span>++</span> <span>ss</span><span style="color:red;">)</span><span style="color:red;">)</span></code></pre>
<p>I&#8217;m not sure if there&#8217;s a name for this particular type class trick, enabling the definition of a function that takes an variable number of arguments, but I lifted it from the <a href="http://hackage.haskell.org/package/haxr">HaXR</a> package. The type classes allow, finally, the definition of <code>importSelectors</code>:</p>
<pre><code><span>importSelectors</span> <span style="color:red;">::</span> <span>Importer</span> <span>a</span> <span style="color:red;">=&gt;</span> <span>a</span>
<span>importSelectors</span> <span style="color:red;">=</span> <span>importGen</span> <span>[]</span></code></pre>
<p>Some minor issues remain &#8212; how, for example, should you define the selector functions if you want to use them in the same module where you define your records? Because of Template Haskell stage restrictions, I wouldn&#8217;t be able to make use of the generated <code>HasSelectors</code> instance in another Template Haskell splice. I could have <code>mkSelectionInstances</code> generate them, but not export them, but I could run into a problem if I also wanted to import other records, with overlapping selector names, into a module where I&#8217;m also defining records. Nevertheless, with a few tweaks for corner cases like these, this approach has potential.</p>
</div>
<div id="alernative-2-dont-create-identifiers-at-all">
<h2>Alernative 2: Don&#8217;t create identifiers at all!</h2>
<p>Instead, use a &#8216;literal&#8217; representing a selector. The Haskell <code>QuasiQuotes</code> extension allows us to create custom literals, and I can create a Quasi Quoter to represent my record selectors:</p>
<pre><code><span>selToExp</span> <span style="color:red;">::</span> <span>String</span> <span style="color:red;">-&gt;</span> <span>Q</span> <span>Exp</span>
<span>selToExp</span> <span>cs</span> <span style="color:red;">=</span> <span>return</span> <span style="color:red;">(</span><span>VarE</span> <span style="color:teal;">'</span><span>select</span> <span>`AppE`</span>
   <span style="color:red;">(</span><span>foldr</span> <span>AppE</span> <span style="color:red;">(</span><span>ConE</span> <span style="color:teal;">'</span><span>()</span><span style="color:red;">)</span> <span>$</span> <span>map</span> <span>f</span> <span>cs</span><span style="color:red;">)</span><span style="color:red;">)</span>
   <span style="color:blue;font-weight:bold;">where</span> <span>f</span> <span>c</span> <span style="color:red;">=</span> <span style="color:red;">(</span><span>ConE</span> <span style="color:teal;">'</span><span style="color:red;">(</span><span>:*</span><span style="color:red;">)</span><span style="color:red;">)</span> <span>`AppE`</span> <span style="color:red;">(</span><span>ConE</span> <span>$</span> <span>mkName</span> <span>$</span> <span style="color:teal;">"C"</span> <span>++</span> <span>show</span> <span style="color:red;">(</span><span>fromEnum</span> <span>c</span><span style="color:red;">)</span><span style="color:red;">)</span>

<span>selToPat</span> <span style="color:red;">::</span> <span>String</span> <span style="color:red;">-&gt;</span> <span>Q</span> <span>Pat</span>
<span>selToPat</span> <span style="color:blue;font-weight:bold;">_</span> <span style="color:red;">=</span> <span>fail</span> <span style="color:teal;">"sorry, unimplemented"</span>

<span>&#960;</span> <span style="color:red;">=</span> <span>QuasiQuoter</span> <span>selToExp</span> <span>selToPat</span></code></pre>
<p>I no longer have to worry about creating identifiers for my selectors. I have a whole family of literals available to me, wherever I import my Quasi Quoter:</p>
<pre><code><span>cory</span> <span style="color:red;">=</span> <span>Host</span> <span style="color:teal;">"cory.EECS.Berkeley.EDU"</span> <span class="hs-num">128.32</span><span>.</span><span class="hs-num">48.187</span>
<span>joe</span> <span style="color:red;">=</span> <span>User</span> <span style="color:teal;">"Joe"</span> <span>cory</span>

<span>joesName</span> <span style="color:red;">=</span> <span style="color:red;">[</span><span>$</span><span>&#960;</span><span style="color:red;">|</span><span>name</span><span style="color:red;">|</span><span style="color:red;">]</span> <span>joe</span>
<span>joesHostName</span> <span style="color:red;">=</span> <span style="color:red;">[</span><span>$</span><span>&#960;</span><span style="color:red;">|</span><span>name</span><span style="color:red;">|</span><span style="color:red;">]</span> <span>$</span> <span style="color:red;">[</span><span>$</span><span>&#960;</span><span style="color:red;">|</span><span>host</span><span style="color:red;">|</span><span style="color:red;">]</span> <span>$</span> <span>joe</span></code></pre>
<p>Now, I realize that <code>[$&#960;|name|]</code> is a bit more unwieldy than <code>name</code>. I have (in the case of <code>name</code>) four character of meaningful identifier and six characters of overhead, which is not convenient. But this overhead is purely syntactic, and suitable desugarer could (in the event that this idea becomes wildly popular) be created to make type level literals palatable. Perhaps &#8216;raw&#8217; type level strings could look like <code>&#171;name&#187;</code>, and my literal selectors could look like <code>&#8467;name</code> (a script &#8216;l&#8217; symbol prefix indicating that some sort of label is being introduced), such that the following properties always hold (forall strings <code>s</code>):</p>
<pre><code><span>&#8467;s</span> <span>==</span> <span>select</span> <span class="hs-sel">&#171;</span><span>s</span><span class="hs-sel">&#187;</span>
<span class="hs-sel">&#171;</span><span>s</span><span class="hs-sel">&#187;</span> <span>==</span> <span>$</span><span style="color:red;">(</span><span>selToExp</span> <span>s</span><span style="color:red;">)</span> <span style="color:red;">::</span> <span>$</span><span style="color:red;">(</span><span>selToType</span> <span>s</span><span style="color:red;">)</span></code></pre>
<p>Given this (unlikely to be implemented!) syntax sugar, my example would look like:</p>
<pre><code><span>cory</span> <span style="color:red;">=</span> <span>Host</span> <span style="color:teal;">"cory.EECS.Berkeley.EDU"</span> <span class="hs-num">128.32</span><span>.</span><span class="hs-num">48.187</span>
<span>joe</span> <span style="color:red;">=</span> <span>User</span> <span style="color:teal;">"Joe"</span> <span>cory</span>

<span>joesName</span> <span style="color:red;">=</span> <span>&#8467;name</span> <span>joe</span>
<span>joesHostName</span> <span style="color:red;">=</span> <span>&#8467;name</span> <span>$</span> <span>&#8467;host</span> <span>$</span> <span>joe</span></code></pre>
<p>By defining records this way, I&#8217;ve thrown away <em>some</em> of the utility of Haskell record selectors. As implemented above, the selectors are only useful as record destructors, whereas normal Haskell record selectors are useful also as (part of) record construction as well as record update. That&#8217;s not a particular problem for this approach: it could easily be combined with, say, <a href="http://hackage.haskell.org/package/fclabels">first-class labels</a>. <code>mkSelectionInstances</code> would need to be redefined a bit to produce instances that provide the right <code>SelectionType</code> (e.g. <code>User :-&gt; String</code> instead of <code>String</code> for the <code>name</code> selector, where <code>:-&gt;</code> is a first-class-label type constructor).</p>
<p>Pushing the idea of type level strings as labels a little further, it would be relatively easy to reimplement some HList features &#8212; specifically, the labels of its extensible record system &#8212; using type level strings. HList depends on type level naturals and a namespace type to define labels; type level strings could be used as easily. Algorithms written in terms of labels as defined above could (I think) be agnostic as to whether they operate on a record whose underlying representation is simple Haskell ADT or a (suitably reimplemted) HList.</p>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/greayer.wordpress.com/219/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/greayer.wordpress.com/219/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/greayer.wordpress.com/219/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/greayer.wordpress.com/219/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/greayer.wordpress.com/219/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/greayer.wordpress.com/219/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/greayer.wordpress.com/219/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/greayer.wordpress.com/219/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/greayer.wordpress.com/219/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/greayer.wordpress.com/219/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/greayer.wordpress.com/219/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/greayer.wordpress.com/219/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/greayer.wordpress.com/219/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/greayer.wordpress.com/219/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=greayer.wordpress.com&amp;blog=7862297&amp;post=219&amp;subd=greayer&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://greayer.wordpress.com/2010/01/28/record-selector-punning-with-type-level-strings-2/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/236fba8deba62e3deb53189335900f35?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">robgreayer</media:title>
		</media:content>
	</item>
		<item>
		<title>Sorting Type-Heterogeneous Values with Type Families</title>
		<link>http://greayer.wordpress.com/2009/11/15/sorting-type-heterogeneous-values-with-type-families/</link>
		<comments>http://greayer.wordpress.com/2009/11/15/sorting-type-heterogeneous-values-with-type-families/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 01:55:22 +0000</pubDate>
		<dc:creator>robgreayer</dc:creator>
				<category><![CDATA[haskell]]></category>

		<guid isPermaLink="false">http://greayer.wordpress.com/?p=179</guid>
		<description><![CDATA[In an ongoing project, I&#8217;ve needed to do a bit of Haskell type-hackery with type functions implemented with type families. One thing I&#8217;ve needed is a type-heterogeneous sort &#8212; more than a type level sort, but a function that takes a value of one product type, and produces a value of a different, sorted product [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=greayer.wordpress.com&amp;blog=7862297&amp;post=179&amp;subd=greayer&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In an ongoing project, I&#8217;ve needed to do a bit of Haskell type-hackery with <a href="http://haskell.org/haskellwiki/Simonpj/Talk:FunWithTypeFuns">type functions</a> implemented with <a href="http://www.cse.unsw.edu.au/~chak/papers/SPCS08.html">type families</a>. One thing I&#8217;ve needed is a type-heterogeneous sort &#8212; more than a type level sort, but a function that takes a value of one product type, and produces a value of a different, sorted product type. In doing so it needs to both sort the parameter type into a (potentially) different result type, as well as sort the parameter value into a result value (which of course needs to be of the result type). This works out to be a bit more complex than a pure type level sort (such as the quicksort implemented <a href="http://www.haskell.org/haskellwiki/Type_arithmetic#An_Advanced_Example_:_Type-Level_Quicksort">here</a>, using funcitonal dependencies, or the merge sort implmenented <a href="http://snipplr.com/view/18891/merge-sort/">here</a>, again using functional dependencies), as Haskell type classes have to be used to select the right value-level functions to use for each step of the sort process. It was an interesting puzzle to solve &#8212; at least for me, as type level programming isn&#8217;t my strong point! (To make the problem a bit easier, I implemented a simpler, if less efficient, sorting algorithm, but quick sort or merge sort should also work at the type/value level).</p>
<p>Given a type for tagged values, e.g.</p>
<pre><code>    <span style="color:green;">-- n is a phantom, type-level natural</span>
    <span style="color:blue;font-weight:bold;">newtype</span> <span>Tagged</span> <span>n</span> <span>t</span> <span style="color:red;">=</span> <span>Tagged</span> <span>t</span></code></pre>
<p>And a product type, e.g.</p>
<pre><code>    <span style="color:blue;font-weight:bold;">data</span> <span>a</span> <span>:*</span> <span>b</span> <span style="color:red;">=</span> <span>a</span> <span>:*</span> <span>b</span>
    <span style="color:blue;font-weight:bold;">infixr</span> <span class="hs-num">6</span> <span>:*</span></code></pre>
<p>I want to define tagged values such as:</p>
<pre><code>    <span>tv0</span> <span style="color:red;">::</span> <span>Tagged</span> <span>N0</span> <span>String</span>
    <span>tv0</span> <span style="color:red;">=</span> <span>Tagged</span> <span style="color:teal;">"hello"</span>
    <span>tv1</span> <span style="color:red;">::</span> <span>Tagged</span> <span>N1</span> <span>Int</span>
    <span>tv1</span> <span style="color:red;">=</span> <span>Tagged</span> <span class="hs-num">2</span>
    <span>tv2</span> <span style="color:red;">::</span> <span>Tagged</span> <span>N2</span> <span>Bool</span>
    <span>tv2</span> <span style="color:red;">=</span> <span>Tagged</span> <span>False</span>
    <span style="color:green;">-- etc.</span></code></pre>
<p>I want to be able to apply a function <code>sort</code> to an arbitrary product of these types:</p>
<pre><code>    <span>x</span> <span style="color:red;">=</span> <span>sort</span> <span style="color:red;">(</span> <span>tv2</span> <span>:*</span> <span>tv0</span> <span>:*</span> <span>tv1</span> <span style="color:red;">)</span>
    <span>y</span> <span style="color:red;">=</span> <span>sort</span> <span style="color:red;">(</span> <span>tv1</span> <span>:*</span> <span>tv2</span> <span>:*</span> <span>tv0</span> <span style="color:red;">)</span></code></pre>
<p>And the result should be the sorted product, i.e. <code>x == y &amp;&amp; x == Tagged &quot;hello&quot; :* Tagged 2 :* Tagged False</code>. Note that in the first application of sort, sort needs a type like:</p>
<pre><code>    <span>sort</span> <span style="color:red;">::</span> <span style="color:red;">(</span><span>Tagged</span> <span>N2</span> <span>Bool</span> <span>:*</span> <span>Tagged</span> <span>N0</span> <span>String</span> <span>:*</span> <span>Tagged</span> <span>N1</span> <span>Int</span><span style="color:red;">)</span> <span style="color:red;">-&gt;</span>
        <span style="color:red;">(</span><span>Tagged</span> <span>N0</span> <span>String</span> <span>:*</span> <span>Tagged</span> <span>N1</span> <span>Int</span> <span>:*</span> <span>Tagged</span> <span>N2</span> <span>Bool</span><span style="color:red;">)</span></code></pre>
<p>and in the second, it needs:</p>
<pre><code>    <span>sort</span> <span style="color:red;">::</span> <span style="color:red;">(</span><span>Tagged</span> <span>N1</span> <span>Int</span> <span>:*</span> <span>Tagged</span> <span>N2</span> <span>Bool</span> <span>:*</span> <span>Tagged</span> <span>N0</span> <span>String</span><span style="color:red;">)</span> <span style="color:red;">-&gt;</span>
        <span style="color:red;">(</span><span>Tagged</span> <span>N0</span> <span>String</span> <span>:*</span> <span>Tagged</span> <span>N1</span> <span>Int</span> <span>:*</span> <span>Tagged</span> <span>N2</span> <span>Bool</span><span style="color:red;">)</span></code></pre>
<p>It needs to work for arbitrarily long products of any Tagged type (with the constraint that the index <code>n</code> must be a type-level natural). My solution to the problem follows.</p>
<pre><code><span>&gt;</span> <span style="color:green;">{-# LANGUAGE EmptyDataDecls, TypeFamilies, UndecidableInstances,
&gt;              ScopedTypeVariables, OverlappingInstances, TypeOperators,
&gt;              FlexibleInstances, NoMonomorphismRestriction #-}</span>
<span>&gt;</span> <span style="color:blue;font-weight:bold;">module</span> <span>TSort</span> <span style="color:blue;font-weight:bold;">where</span>
</code></pre>
<p>I started with the usual definition for type level naturals (with some convenient aliases for a few of them):</p>
<pre><code><span>&gt;</span> <span style="color:blue;font-weight:bold;">data</span> <span>Zero</span>
<span>&gt;</span> <span style="color:blue;font-weight:bold;">data</span> <span>Succ</span> <span>n</span>
</code></pre>
<pre><code><span>&gt;</span> <span style="color:blue;font-weight:bold;">type</span> <span>N0</span> <span style="color:red;">=</span> <span>Zero</span>
<span>&gt;</span> <span style="color:blue;font-weight:bold;">type</span> <span>N1</span> <span style="color:red;">=</span> <span>Succ</span> <span>Zero</span>
<span>&gt;</span> <span style="color:blue;font-weight:bold;">type</span> <span>N2</span> <span style="color:red;">=</span> <span>Succ</span> <span>N1</span>
<span>&gt;</span> <span style="color:blue;font-weight:bold;">type</span> <span>N3</span> <span style="color:red;">=</span> <span>Succ</span> <span>N2</span>
<span>&gt;</span> <span style="color:blue;font-weight:bold;">type</span> <span>N4</span> <span style="color:red;">=</span> <span>Succ</span> <span>N3</span>
<span>&gt;</span> <span style="color:blue;font-weight:bold;">type</span> <span>N5</span> <span style="color:red;">=</span> <span>Succ</span> <span>N4</span>
<span>&gt;</span> <span style="color:blue;font-weight:bold;">type</span> <span>N6</span> <span style="color:red;">=</span> <span>Succ</span> <span>N5</span>
<span>&gt;</span> <span style="color:blue;font-weight:bold;">type</span> <span>N7</span> <span style="color:red;">=</span> <span>Succ</span> <span>N6</span>
<span>&gt;</span> <span style="color:blue;font-weight:bold;">type</span> <span>N8</span> <span style="color:red;">=</span> <span>Succ</span> <span>N7</span>
<span>&gt;</span> <span style="color:blue;font-weight:bold;">type</span> <span>N9</span> <span style="color:red;">=</span> <span>Succ</span> <span>N8</span>
<span>&gt;</span> <span style="color:blue;font-weight:bold;">type</span> <span>N10</span> <span style="color:red;">=</span> <span>Succ</span> <span>N9</span>
</code></pre>
<p>I also use type level booleans:</p>
<pre><code><span>&gt;</span> <span style="color:blue;font-weight:bold;">data</span> <span>TRUE</span>
<span>&gt;</span> <span style="color:blue;font-weight:bold;">data</span> <span>FALSE</span>
</code></pre>
<p>I need one inequality operator on naturals:</p>
<pre><code><span>&gt;</span> <span style="color:blue;font-weight:bold;">type</span> <span>family</span> <span>LessThan</span> <span>m</span> <span>n</span>
<span>&gt;</span> <span style="color:blue;font-weight:bold;">type</span> <span style="color:blue;font-weight:bold;">instance</span> <span>LessThan</span> <span>Zero</span> <span>Zero</span> <span style="color:red;">=</span> <span>FALSE</span>
<span>&gt;</span> <span style="color:blue;font-weight:bold;">type</span> <span style="color:blue;font-weight:bold;">instance</span> <span>LessThan</span> <span style="color:red;">(</span><span>Succ</span> <span>n</span><span style="color:red;">)</span> <span>Zero</span> <span style="color:red;">=</span> <span>FALSE</span>
<span>&gt;</span> <span style="color:blue;font-weight:bold;">type</span> <span style="color:blue;font-weight:bold;">instance</span> <span>LessThan</span> <span>Zero</span> <span style="color:red;">(</span><span>Succ</span> <span>n</span><span style="color:red;">)</span> <span style="color:red;">=</span> <span>TRUE</span>
<span>&gt;</span> <span style="color:blue;font-weight:bold;">type</span> <span style="color:blue;font-weight:bold;">instance</span> <span>LessThan</span> <span style="color:red;">(</span><span>Succ</span> <span>m</span><span style="color:red;">)</span> <span style="color:red;">(</span><span>Succ</span> <span>n</span><span style="color:red;">)</span> <span style="color:red;">=</span> <span>LessThan</span> <span>m</span> <span>n</span>
</code></pre>
<p>I need a type level if statement:</p>
<pre><code><span>&gt;</span> <span style="color:blue;font-weight:bold;">type</span> <span>family</span> <span>Cond</span> <span>c</span> <span>t</span> <span>f</span>
<span>&gt;</span> <span style="color:blue;font-weight:bold;">type</span> <span style="color:blue;font-weight:bold;">instance</span> <span>Cond</span> <span>TRUE</span> <span>t</span> <span>f</span> <span style="color:red;">=</span> <span>t</span>
<span>&gt;</span> <span style="color:blue;font-weight:bold;">type</span> <span style="color:blue;font-weight:bold;">instance</span> <span>Cond</span> <span>FALSE</span> <span>t</span> <span>f</span> <span style="color:red;">=</span> <span>f</span>
</code></pre>
<p>I need my tagged and product types:</p>
<pre><code><span>&gt;</span> <span style="color:blue;font-weight:bold;">newtype</span> <span>Tagged</span> <span>n</span> <span>a</span> <span style="color:red;">=</span> <span>Tagged</span> <span>a</span> <span style="color:blue;font-weight:bold;">deriving</span> <span style="color:red;">(</span><span>Show</span><span style="color:red;">,</span><span>Eq</span><span style="color:red;">)</span>
</code></pre>
<pre><code><span>&gt;</span> <span style="color:blue;font-weight:bold;">data</span> <span>a</span> <span>:*</span> <span>b</span> <span style="color:red;">=</span> <span>a</span> <span>:*</span> <span>b</span> <span style="color:blue;font-weight:bold;">deriving</span> <span style="color:red;">(</span><span>Show</span><span style="color:red;">,</span><span>Eq</span><span style="color:red;">)</span>
<span>&gt;</span> <span style="color:blue;font-weight:bold;">infixr</span> <span class="hs-num">6</span> <span>:*</span>
</code></pre>
<p>The general type of my sort is going to be something like (but not exactly):</p>
<pre><code>    <span>sort</span> <span style="color:red;">::</span> <span style="color:red;">(</span><span>Sortable</span> <span>a</span><span style="color:red;">)</span> <span style="color:red;">=&gt;</span> <span>a</span> <span style="color:red;">-&gt;</span> <span>Sorted</span> <span>a</span></code></pre>
<p>where <code>Sortable</code> is a type class and <code>Sorted</code> is a associated type of that type class. It didn&#8217;t work out to be quite that simple, but this is how I started thinking about the problem.</p>
<p>I envisioned the <code>Sortable</code> class as:</p>
<pre><code>    <span style="color:blue;font-weight:bold;">class</span> <span>Sortable</span> <span>a</span> <span style="color:blue;font-weight:bold;">where</span>
        <span style="color:blue;font-weight:bold;">type</span> <span>Sorted</span> <span>a</span>
        <span>sort</span> <span style="color:red;">::</span> <span>a</span> <span style="color:red;">-&gt;</span> <span>Sorted</span> <span>a</span></code></pre>
<p>Trying to create a simple instance of this type, for a pair of tagged values got me a bit bogged down:</p>
<pre><code>    <span style="color:blue;font-weight:bold;">instance</span> <span>Sortable</span> <span style="color:red;">(</span><span>Tagged</span> <span>m</span> <span>a</span> <span>:*</span> <span>Tagged</span> <span>n</span> <span>b</span><span style="color:red;">)</span> <span style="color:blue;font-weight:bold;">where</span>
        <span style="color:blue;font-weight:bold;">type</span> <span>Sorted</span> <span style="color:red;">(</span><span>Tagged</span> <span>m</span> <span>a</span> <span>:*</span> <span>Tagged</span> <span>n</span> <span>b</span><span style="color:red;">)</span> <span style="color:red;">=</span> <span>???</span>
        <span>sort</span> <span style="color:red;">=</span> <span>???</span></code></pre>
<p>The type of <code>Sorted</code> for this pair is conditional based on the value of the indices <code>m</code> and <code>n</code>, so I can write the type function like this:</p>
<pre><code>    <span style="color:blue;font-weight:bold;">type</span> <span>Sorted</span> <span style="color:red;">(</span><span>Tagged</span> <span>m</span> <span>a</span> <span>:*</span> <span>Tagged</span> <span>n</span> <span>b</span><span style="color:red;">)</span> <span style="color:red;">=</span>
        <span>Cond</span> <span style="color:red;">(</span><span>LessThan</span> <span>n</span> <span>m</span><span style="color:red;">)</span> <span style="color:green;">-- if n &lt; m</span>
            <span style="color:red;">(</span><span>Tagged</span> <span>n</span> <span>b</span> <span>:*</span> <span>Tagged</span> <span>m</span> <span>a</span><span style="color:red;">)</span> <span style="color:green;">-- then swap</span>
            <span style="color:red;">(</span><span>Tagged</span> <span>m</span> <span>a</span> <span>:*</span> <span>Tagged</span> <span>n</span> <span>b</span><span style="color:red;">)</span> <span style="color:green;">-- else don't</span></code></pre>
<p>but how to write the <code>sort</code> function? It can&#8217;t test the type and decide whether to swap the values. The key insight for solving this problem was that I needed some additional types to represent the operations I needed. For this simple reordering of a pair, I need two operations &#8211; <em>Swap</em>, to swap the elements of the pair if they are out of order, and <em>Id</em> (identity), to leave them alone if they are already in order:</p>
<pre><code>    <span style="color:blue;font-weight:bold;">data</span> <span>Swap</span> <span>x</span> <span>y</span> <span style="color:red;">=</span> <span>Swap</span> <span>x</span> <span>y</span>
    <span style="color:blue;font-weight:bold;">data</span> <span>Id</span> <span>a</span> <span style="color:red;">=</span> <span>Id</span> <span>a</span></code></pre>
<p>Each of these can be seen as implementing a single step in a sorting algorithm). Each will be an instance of a <code>Sorter</code> class:</p>
<pre><code><span>&gt;</span> <span style="color:blue;font-weight:bold;">class</span> <span>Sorter</span> <span>a</span> <span style="color:blue;font-weight:bold;">where</span>
<span>&gt;</span>     <span style="color:blue;font-weight:bold;">type</span> <span>Sorted</span> <span>a</span>
<span>&gt;</span>     <span style="color:blue;font-weight:bold;">type</span> <span>Unsorted</span> <span>a</span>
</code></pre>
<p><code>mkSort</code> &#8216;makes&#8217; a sort function from (Unsorted a -&gt; Sorted a) based on the type of its first argument, which is a proxy argument (its value is never examined).</p>
<pre><code><span>&gt;</span>     <span>mkSort</span> <span style="color:red;">::</span> <span>a</span> <span style="color:red;">-&gt;</span> <span>Unsorted</span> <span>a</span> <span style="color:red;">-&gt;</span> <span>Sorted</span> <span>a</span>
</code></pre>
<p>The <code>Swap</code> instance of <code>Sorter</code> just swaps the values in a pair:</p>
<pre><code><span>&gt;</span> <span style="color:blue;font-weight:bold;">instance</span> <span>Sorter</span> <span style="color:red;">(</span><span>Swap</span> <span>x</span> <span>y</span><span style="color:red;">)</span> <span style="color:blue;font-weight:bold;">where</span>
<span>&gt;</span>     <span style="color:blue;font-weight:bold;">type</span> <span>Sorted</span> <span style="color:red;">(</span><span>Swap</span> <span>x</span> <span>y</span><span style="color:red;">)</span> <span style="color:red;">=</span> <span>y</span> <span>:*</span> <span>x</span>
<span>&gt;</span>     <span style="color:blue;font-weight:bold;">type</span> <span>Unsorted</span> <span style="color:red;">(</span><span>Swap</span> <span>x</span> <span>y</span><span style="color:red;">)</span> <span style="color:red;">=</span> <span>x</span> <span>:*</span> <span>y</span>
<span>&gt;</span>     <span>mkSort</span> <span style="color:blue;font-weight:bold;">_</span> <span style="color:red;">(</span><span>x</span> <span>:*</span> <span>y</span><span style="color:red;">)</span> <span style="color:red;">=</span> <span>y</span> <span>:*</span> <span>x</span>
</code></pre>
<p>The <code>Id</code> instance of <code>Sorter</code> leaves its value alone:</p>
<pre><code><span>&gt;</span> <span style="color:blue;font-weight:bold;">instance</span> <span>Sorter</span> <span style="color:red;">(</span><span>Id</span> <span>a</span><span style="color:red;">)</span> <span style="color:blue;font-weight:bold;">where</span>
<span>&gt;</span>     <span style="color:blue;font-weight:bold;">type</span> <span>Sorted</span> <span style="color:red;">(</span><span>Id</span> <span>a</span><span style="color:red;">)</span> <span style="color:red;">=</span> <span>a</span>
<span>&gt;</span>     <span style="color:blue;font-weight:bold;">type</span> <span>Unsorted</span> <span style="color:red;">(</span><span>Id</span> <span>a</span><span style="color:red;">)</span> <span style="color:red;">=</span> <span>a</span>
<span>&gt;</span>     <span>mkSort</span> <span style="color:blue;font-weight:bold;">_</span> <span>v</span> <span style="color:red;">=</span> <span>v</span>
</code></pre>
<p>Now my <code>Sortable</code> class, instead of having a <code>sort</code> function, needs instead a function to produce a sorter value:</p>
<pre><code><span>&gt;</span> <span style="color:blue;font-weight:bold;">class</span> <span>Sortable</span> <span>a</span> <span style="color:blue;font-weight:bold;">where</span>
<span>&gt;</span>     <span style="color:blue;font-weight:bold;">type</span> <span>SorterType</span> <span>a</span>
<span>&gt;</span>     <span>sorter</span> <span style="color:red;">::</span> <span>a</span> <span style="color:red;">-&gt;</span> <span>SorterType</span> <span>a</span>
</code></pre>
<p>And the sort can be written in terms of functions from the two classes:</p>
<pre><code><span>&gt;</span> <span>sort</span> <span>v</span> <span style="color:red;">=</span> <span>mkSort</span> <span style="color:red;">(</span><span>sorter</span> <span>v</span><span style="color:red;">)</span> <span>v</span>
</code></pre>
<p>But still, how to implement the <code>sorter</code> function for my pair of tagged elements?</p>
<pre><code><span>&gt;</span> <span style="color:blue;font-weight:bold;">instance</span> <span>Sortable</span> <span style="color:red;">(</span><span>Tagged</span> <span>m</span> <span>a</span> <span>:*</span> <span>Tagged</span> <span>n</span> <span>b</span><span style="color:red;">)</span> <span style="color:blue;font-weight:bold;">where</span>
</code></pre>
<p>The type function for the <code>SorterType</code> is now:</p>
<pre><code><span>&gt;</span>     <span style="color:blue;font-weight:bold;">type</span> <span>SorterType</span> <span style="color:red;">(</span><span>Tagged</span> <span>m</span> <span>a</span> <span>:*</span> <span>Tagged</span> <span>n</span> <span>b</span><span style="color:red;">)</span> <span style="color:red;">=</span>
<span>&gt;</span>         <span>Cond</span> <span style="color:red;">(</span><span>LessThan</span> <span>n</span> <span>m</span><span style="color:red;">)</span>
<span>&gt;</span>             <span style="color:red;">(</span><span>Swap</span> <span style="color:red;">(</span><span>Tagged</span> <span>m</span> <span>a</span><span style="color:red;">)</span> <span style="color:red;">(</span><span>Tagged</span> <span>n</span> <span>b</span><span style="color:red;">)</span><span style="color:red;">)</span>
<span>&gt;</span>             <span style="color:red;">(</span><span>Id</span> <span style="color:red;">(</span><span>Tagged</span> <span>m</span> <span>a</span> <span>:*</span> <span>Tagged</span> <span>n</span> <span>b</span><span style="color:red;">)</span><span style="color:red;">)</span>
<span>&gt;</span>     <span>sorter</span> <span style="color:red;">=</span> <span>undefined</span> <span style="color:green;">-- ???</span>
</code></pre>
<p>As it turns out, the above implementation is sufficient. Since the <code>mkSort</code> function doesn&#8217;t examine its first argument &#8212; it&#8217;s merely a proxy &#8212; the sorter function doesn&#8217;t need to produce a value. So <code>sorter</code> can be left undefined &#8212; all its &#8216;work&#8217; is done at compile time, in producing the right type such that the instance of &#8216;mkSort&#8217; can be inferred (I have to admit, I discovered this accidentally while writing the sorting function. I left <code>sorter</code> undefined because I couldn&#8217;t think of how to define it, then wrote <code>sort</code> in terms of it, and accidentally ran it. It worked, my mind boggled, and then I figured out what what going on). Because no concrete <em>value</em> of either the <code>Id</code> or <code>Swap</code> types are ever needed, the actual definitions for these can be:</p>
<pre><code><span>&gt;</span> <span style="color:blue;font-weight:bold;">data</span> <span>Id</span> <span>a</span>
<span>&gt;</span> <span style="color:blue;font-weight:bold;">data</span> <span>Swap</span> <span>a</span> <span>b</span>
</code></pre>
<p>To sort something more than a pair, I need to be able to do more than just swap two types and values. A simple algorithm for sorting the product is to sort the &#8216;tail&#8217; of the product, then insert the head of the product into the sorted tail (this is insertion sort &#8212; not the most efficient, but simpler than merge sort or quick sort). For this I need two more operations, a <em>sort-then-insert</em> operation, and an <em>insert</em> operation (the sort-then-insert operation sorts its &#8216;tail&#8217; and then (if necessary) uses the insert operation to insert its head into the sorted tail). I need these operation types:</p>
<pre><code><span>&gt;</span> <span style="color:blue;font-weight:bold;">data</span> <span>SortInsert</span> <span>h</span> <span>t</span>
<span>&gt;</span> <span style="color:blue;font-weight:bold;">data</span> <span>Insert</span> <span>x</span> <span>y</span>
</code></pre>
<p>I need a new class similar to <code>Sortable</code>, which has a <code>InserterType</code> and a function to produce a <code>InserterType</code> from the instance type:</p>
<pre><code><span>&gt;</span> <span style="color:blue;font-weight:bold;">class</span> <span>Insertable</span> <span>a</span> <span style="color:blue;font-weight:bold;">where</span>
<span>&gt;</span>     <span style="color:blue;font-weight:bold;">type</span> <span>InserterType</span> <span>a</span>
<span>&gt;</span>     <span>inserter</span> <span style="color:red;">::</span> <span>a</span> <span style="color:red;">-&gt;</span> <span>InserterType</span> <span>a</span>
</code></pre>
<p>A insertable assumes that its tail is sorted. For a product of tagged values (more than a pair), the <code>Insertable</code> instance looks like this:</p>
<pre><code><span>&gt;</span> <span style="color:blue;font-weight:bold;">instance</span> <span>Insertable</span> <span style="color:red;">(</span><span>Tagged</span> <span>m</span> <span>a</span> <span>:*</span> <span>Tagged</span> <span>n</span> <span>b</span> <span>:*</span> <span>c</span><span style="color:red;">)</span> <span style="color:blue;font-weight:bold;">where</span>
</code></pre>
<p>The <code>InserterType</code> depends on whether the head element is less than the first element of the sorted tail. If it is, then the <code>InserterType</code> is the identity, otherwise it is an <code>Insert x y</code>:</p>
<pre><code><span>&gt;</span>     <span style="color:blue;font-weight:bold;">type</span> <span>InserterType</span> <span style="color:red;">(</span><span>Tagged</span> <span>m</span> <span>a</span> <span>:*</span> <span>Tagged</span> <span>n</span> <span>b</span> <span>:*</span> <span>c</span><span style="color:red;">)</span> <span style="color:red;">=</span>
<span>&gt;</span>              <span>Cond</span> <span style="color:red;">(</span><span>LessThan</span> <span>n</span> <span>m</span><span style="color:red;">)</span>
<span>&gt;</span>                  <span style="color:red;">(</span><span>Insert</span> <span style="color:red;">(</span><span>Tagged</span> <span>m</span> <span>a</span><span style="color:red;">)</span> <span style="color:red;">(</span><span>Tagged</span> <span>n</span> <span>b</span> <span>:*</span> <span>c</span><span style="color:red;">)</span><span style="color:red;">)</span>
<span>&gt;</span>                  <span style="color:red;">(</span><span>Id</span> <span style="color:red;">(</span><span>Tagged</span> <span>m</span> <span>a</span> <span>:*</span> <span>Tagged</span> <span>n</span> <span>b</span> <span>:*</span> <span>c</span><span style="color:red;">)</span><span style="color:red;">)</span>
</code></pre>
<p>As in the <code>sorter</code> function of a <code>Sortable</code>, we don&#8217;t need the <code>inserter</code> to do anything more than create a proxy argument:</p>
<pre><code><span>&gt;</span>     <span>inserter</span> <span style="color:red;">=</span> <span>undefined</span>
</code></pre>
<p>Inserting two elements is the same as sorting them:</p>
<pre><code><span>&gt;</span> <span style="color:blue;font-weight:bold;">instance</span> <span>Insertable</span> <span style="color:red;">(</span><span>Tagged</span> <span>m</span> <span>a</span> <span>:*</span> <span>Tagged</span> <span>n</span> <span>b</span><span style="color:red;">)</span> <span style="color:blue;font-weight:bold;">where</span>
<span>&gt;</span>     <span style="color:blue;font-weight:bold;">type</span> <span>InserterType</span> <span style="color:red;">(</span><span>Tagged</span> <span>m</span> <span>a</span> <span>:*</span> <span>Tagged</span> <span>n</span> <span>b</span><span style="color:red;">)</span> <span style="color:red;">=</span>
<span>&gt;</span>         <span>Cond</span> <span style="color:red;">(</span><span>LessThan</span> <span>n</span> <span>m</span><span style="color:red;">)</span> <span style="color:red;">(</span><span>Swap</span> <span style="color:red;">(</span><span>Tagged</span> <span>m</span> <span>a</span><span style="color:red;">)</span> <span style="color:red;">(</span><span>Tagged</span> <span>n</span> <span>b</span><span style="color:red;">)</span><span style="color:red;">)</span>
<span>&gt;</span>             <span style="color:red;">(</span><span>Id</span> <span style="color:red;">(</span><span>Tagged</span> <span>m</span> <span>a</span> <span>:*</span> <span>Tagged</span> <span>n</span> <span>b</span><span style="color:red;">)</span><span style="color:red;">)</span>
<span>&gt;</span>     <span>inserter</span> <span style="color:red;">=</span> <span>undefined</span>
</code></pre>
<p>A <code>Inserter</code> is similar to a sorter: it takes an value of an <code>Uninserted</code> type and produces a value of an <code>Inserted</code> type:</p>
<pre><code><span>&gt;</span> <span style="color:blue;font-weight:bold;">class</span> <span>Inserter</span> <span>a</span> <span style="color:blue;font-weight:bold;">where</span>
<span>&gt;</span>     <span style="color:blue;font-weight:bold;">type</span> <span>Inserted</span> <span>a</span>
<span>&gt;</span>     <span style="color:blue;font-weight:bold;">type</span> <span>Uninserted</span> <span>a</span>
</code></pre>
<p><code>mkInsert</code> &#8216;makes&#8217; a function <code>(Uninserted a -&gt; Inserted a)</code> from the instance type. The first argument again is just a proxy, it&#8217;s value never inspected:</p>
<pre><code><span>&gt;</span>     <span>mkInsert</span> <span style="color:red;">::</span> <span>a</span> <span style="color:red;">-&gt;</span> <span>Uninserted</span> <span>a</span> <span style="color:red;">-&gt;</span> <span>Inserted</span> <span>a</span>
</code></pre>
<p>The identity instance is straightforward:</p>
<pre><code><span>&gt;</span> <span style="color:blue;font-weight:bold;">instance</span> <span>Inserter</span> <span style="color:red;">(</span><span>Id</span> <span>a</span><span style="color:red;">)</span> <span style="color:blue;font-weight:bold;">where</span>
<span>&gt;</span>     <span style="color:blue;font-weight:bold;">type</span> <span>Inserted</span> <span style="color:red;">(</span><span>Id</span> <span>a</span><span style="color:red;">)</span> <span style="color:red;">=</span> <span>a</span>
<span>&gt;</span>     <span style="color:blue;font-weight:bold;">type</span> <span>Uninserted</span> <span style="color:red;">(</span><span>Id</span> <span>a</span><span style="color:red;">)</span> <span style="color:red;">=</span> <span>a</span>
<span>&gt;</span>     <span>mkInsert</span> <span style="color:blue;font-weight:bold;">_</span> <span>v</span> <span style="color:red;">=</span> <span>v</span>
</code></pre>
<p>The <code>Swap</code> instance of <code>Inserter</code> is essentially the same as the <code>Swap</code> instance of <code>Sorter</code>:</p>
<pre><code><span>&gt;</span> <span style="color:blue;font-weight:bold;">instance</span> <span>Inserter</span> <span style="color:red;">(</span><span>Swap</span> <span>a</span> <span>b</span><span style="color:red;">)</span> <span style="color:blue;font-weight:bold;">where</span>
<span>&gt;</span>    <span style="color:blue;font-weight:bold;">type</span> <span>Inserted</span> <span style="color:red;">(</span><span>Swap</span> <span>a</span> <span>b</span><span style="color:red;">)</span> <span style="color:red;">=</span> <span>b</span> <span>:*</span> <span>a</span>
<span>&gt;</span>    <span style="color:blue;font-weight:bold;">type</span> <span>Uninserted</span> <span style="color:red;">(</span><span>Swap</span> <span>a</span> <span>b</span><span style="color:red;">)</span> <span style="color:red;">=</span> <span>a</span> <span>:*</span> <span>b</span>
<span>&gt;</span>    <span>mkInsert</span> <span style="color:blue;font-weight:bold;">_</span> <span style="color:red;">(</span><span>a</span> <span>:*</span> <span>b</span><span style="color:red;">)</span> <span style="color:red;">=</span> <span>b</span> <span>:*</span> <span>a</span>
</code></pre>
<p>The <code>Insert</code> instance for a longer product has a rather convoluted context:</p>
<pre><code><span>&gt;</span> <span style="color:blue;font-weight:bold;">instance</span> <span style="color:red;">(</span><span>Insertable</span> <span style="color:red;">(</span><span>a</span> <span>:*</span> <span>c</span><span style="color:red;">)</span>
<span>&gt;</span>          <span style="color:red;">,</span> <span>Inserter</span> <span style="color:red;">(</span><span>InserterType</span> <span style="color:red;">(</span><span>a</span> <span>:*</span> <span>c</span><span style="color:red;">)</span><span style="color:red;">)</span>
<span>&gt;</span>          <span style="color:red;">,</span><span style="color:red;">(</span><span>a</span> <span>:*</span> <span>c</span><span style="color:red;">)</span> <span style="color:red;">~</span> <span>Uninserted</span> <span style="color:red;">(</span><span>InserterType</span> <span style="color:red;">(</span><span>a</span> <span>:*</span> <span>c</span><span style="color:red;">)</span><span style="color:red;">)</span>
<span>&gt;</span>          <span style="color:red;">)</span> <span style="color:red;">=&gt;</span> <span>Inserter</span> <span style="color:red;">(</span><span>Insert</span> <span>a</span> <span style="color:red;">(</span><span>b</span> <span>:*</span> <span>c</span><span style="color:red;">)</span><span style="color:red;">)</span> <span style="color:blue;font-weight:bold;">where</span>
</code></pre>
<p>The <code>Inserted</code> type is the first element of the tail followed by a inserted into the tail (via sort):</p>
<pre><code><span>&gt;</span>     <span style="color:blue;font-weight:bold;">type</span> <span>Inserted</span> <span style="color:red;">(</span><span>Insert</span> <span>a</span> <span style="color:red;">(</span><span>b</span> <span>:*</span> <span>c</span><span style="color:red;">)</span><span style="color:red;">)</span> <span style="color:red;">=</span> <span>b</span> <span>:*</span> <span>Inserted</span> <span style="color:red;">(</span><span>InserterType</span> <span style="color:red;">(</span><span>a</span> <span>:*</span> <span>c</span><span style="color:red;">)</span><span style="color:red;">)</span>
</code></pre>
<p>The uninserted type is simply the original (uninserted) product:</p>
<pre><code><span>&gt;</span>     <span style="color:blue;font-weight:bold;">type</span> <span>Uninserted</span> <span style="color:red;">(</span><span>Insert</span> <span>a</span> <span style="color:red;">(</span><span>b</span> <span>:*</span> <span>c</span><span style="color:red;">)</span><span style="color:red;">)</span> <span style="color:red;">=</span> <span>a</span> <span>:*</span> <span>b</span> <span>:*</span> <span>c</span>
</code></pre>
<p>And the insert function effectively swaps the first two elements of the product and inserts the (new) tail:</p>
<pre><code><span>&gt;</span>     <span>mkInsert</span> <span style="color:blue;font-weight:bold;">_</span> <span style="color:red;">(</span><span>x</span> <span>:*</span> <span>y</span> <span>:*</span> <span>z</span><span style="color:red;">)</span> <span style="color:red;">=</span> <span>y</span> <span>:*</span> <span>mkInsert</span> <span style="color:red;">(</span><span>inserter</span> <span>$</span> <span>x</span> <span>:*</span> <span>z</span><span style="color:red;">)</span> <span style="color:red;">(</span><span>x</span> <span>:*</span> <span>z</span><span style="color:red;">)</span>
</code></pre>
<p>The <code>Sorter</code> instance for a <code>SortInsert</code> that we need again has a convoluted context:</p>
<pre><code><span>&gt;</span> <span style="color:blue;font-weight:bold;">instance</span> <span style="color:red;">(</span><span>Sortable</span> <span style="color:red;">(</span><span>b</span> <span>:*</span> <span>c</span><span style="color:red;">)</span>
<span>&gt;</span>           <span style="color:red;">,</span><span>Unsorted</span> <span style="color:red;">(</span><span>SorterType</span> <span style="color:red;">(</span><span>b</span> <span>:*</span> <span>c</span><span style="color:red;">)</span><span style="color:red;">)</span> <span style="color:red;">~</span> <span style="color:red;">(</span><span>b</span> <span>:*</span> <span>c</span><span style="color:red;">)</span>
<span>&gt;</span>           <span style="color:red;">,</span><span>Uninserted</span> <span style="color:red;">(</span><span>InserterType</span> <span style="color:red;">(</span><span>a</span> <span>:*</span> <span>Sorted</span> <span style="color:red;">(</span><span>SorterType</span> <span style="color:red;">(</span><span>b</span> <span>:*</span> <span>c</span><span style="color:red;">)</span><span style="color:red;">)</span><span style="color:red;">)</span><span style="color:red;">)</span> <span style="color:red;">~</span>
<span>&gt;</span>                <span style="color:red;">(</span><span>a</span> <span>:*</span> <span>Sorted</span> <span style="color:red;">(</span><span>SorterType</span> <span style="color:red;">(</span><span>b</span> <span>:*</span> <span>c</span><span style="color:red;">)</span><span style="color:red;">)</span><span style="color:red;">)</span>
<span>&gt;</span>           <span style="color:red;">,</span><span>Sorter</span> <span style="color:red;">(</span><span>SorterType</span> <span style="color:red;">(</span><span>b</span> <span>:*</span> <span>c</span><span style="color:red;">)</span><span style="color:red;">)</span>
<span>&gt;</span>           <span style="color:red;">,</span><span>Insertable</span> <span style="color:red;">(</span><span>a</span> <span>:*</span> <span>Sorted</span> <span style="color:red;">(</span><span>SorterType</span> <span style="color:red;">(</span><span>b</span> <span>:*</span> <span>c</span><span style="color:red;">)</span><span style="color:red;">)</span><span style="color:red;">)</span>
<span>&gt;</span>           <span style="color:red;">,</span><span>Inserter</span> <span style="color:red;">(</span><span>InserterType</span> <span style="color:red;">(</span><span>a</span> <span>:*</span> <span>Sorted</span> <span style="color:red;">(</span><span>SorterType</span> <span style="color:red;">(</span><span>b</span> <span>:*</span> <span>c</span><span style="color:red;">)</span><span style="color:red;">)</span><span style="color:red;">)</span><span style="color:red;">)</span>
<span>&gt;</span>           <span style="color:red;">)</span> <span style="color:red;">=&gt;</span>
<span>&gt;</span>         <span>Sorter</span> <span style="color:red;">(</span><span>SortInsert</span> <span>a</span> <span style="color:red;">(</span><span>b</span> <span>:*</span> <span>c</span><span style="color:red;">)</span><span style="color:red;">)</span> <span style="color:blue;font-weight:bold;">where</span>
</code></pre>
<p>The type of the output of the sort is the type of the output of the head of the product inserted with the sorted tail:</p>
<pre><code><span>&gt;</span>     <span style="color:blue;font-weight:bold;">type</span> <span>Sorted</span> <span style="color:red;">(</span><span>SortInsert</span> <span>a</span> <span style="color:red;">(</span><span>b</span> <span>:*</span> <span>c</span><span style="color:red;">)</span><span style="color:red;">)</span> <span style="color:red;">=</span>
<span>&gt;</span>         <span>Inserted</span> <span style="color:red;">(</span><span>InserterType</span> <span style="color:red;">(</span><span>a</span> <span>:*</span> <span style="color:red;">(</span><span>Sorted</span> <span style="color:red;">(</span><span>SorterType</span> <span style="color:red;">(</span><span>b</span> <span>:*</span> <span>c</span><span style="color:red;">)</span><span style="color:red;">)</span><span style="color:red;">)</span><span style="color:red;">)</span><span style="color:red;">)</span>
</code></pre>
<p>The type of the input of the sort is the original (unsorted, uninserted) product:</p>
<pre><code><span>&gt;</span>     <span style="color:blue;font-weight:bold;">type</span> <span>Unsorted</span> <span style="color:red;">(</span><span>SortInsert</span> <span>a</span> <span style="color:red;">(</span><span>b</span> <span>:*</span> <span>c</span><span style="color:red;">)</span><span style="color:red;">)</span> <span style="color:red;">=</span> <span>a</span> <span>:*</span> <span>b</span> <span>:*</span> <span>c</span>
</code></pre>
<p>And the sort function is just the insert of the head into the tail after the tail is sorted:</p>
<pre><code><span>&gt;</span>     <span>mkSort</span> <span style="color:blue;font-weight:bold;">_</span> <span style="color:red;">(</span><span>a</span> <span>:*</span> <span>b</span> <span>:*</span> <span>c</span><span style="color:red;">)</span> <span style="color:red;">=</span> <span>mkInsert</span> <span style="color:red;">(</span><span>inserter</span> <span>preinsert</span><span style="color:red;">)</span> <span>preinsert</span>
<span>&gt;</span>         <span style="color:blue;font-weight:bold;">where</span> <span>preinsert</span> <span style="color:red;">=</span> <span>a</span> <span>:*</span> <span>sort</span> <span style="color:red;">(</span><span>b</span> <span>:*</span> <span>c</span><span style="color:red;">)</span>
</code></pre>
<p>Finally, the <code>Sortable</code> instance for an arbitrary product is:</p>
<pre><code><span>&gt;</span> <span style="color:blue;font-weight:bold;">instance</span> <span style="color:red;">(</span><span>Sortable</span> <span style="color:red;">(</span><span>b</span> <span>:*</span> <span>c</span><span style="color:red;">)</span><span style="color:red;">,</span> <span>Sorter</span> <span style="color:red;">(</span><span>SortInsert</span> <span>a</span> <span style="color:red;">(</span><span>b</span> <span>:*</span> <span>c</span><span style="color:red;">)</span><span style="color:red;">)</span><span style="color:red;">)</span> <span style="color:red;">=&gt;</span>
<span>&gt;</span>     <span>Sortable</span> <span style="color:red;">(</span><span>a</span> <span>:*</span> <span>b</span> <span>:*</span> <span>c</span><span style="color:red;">)</span> <span style="color:blue;font-weight:bold;">where</span>
<span>&gt;</span>     <span style="color:blue;font-weight:bold;">type</span> <span>SorterType</span> <span style="color:red;">(</span><span>a</span> <span>:*</span> <span>b</span> <span>:*</span> <span>c</span><span style="color:red;">)</span> <span style="color:red;">=</span> <span>SortInsert</span> <span>a</span> <span style="color:red;">(</span><span>b</span> <span>:*</span> <span>c</span><span style="color:red;">)</span>
<span>&gt;</span>     <span>sorter</span> <span style="color:red;">=</span> <span>undefined</span>
</code></pre>
<p>Given some tagged values, I can try out the sort function:</p>
<pre><code><span>&gt;</span> <span>t0</span> <span style="color:red;">::</span> <span>Tagged</span> <span>N0</span> <span>String</span>
<span>&gt;</span> <span>t0</span> <span style="color:red;">=</span> <span>Tagged</span> <span style="color:teal;">"hello"</span>
<span>&gt;</span> <span>t1</span> <span style="color:red;">::</span> <span>Tagged</span> <span>N1</span> <span>Bool</span>
<span>&gt;</span> <span>t1</span> <span style="color:red;">=</span> <span>Tagged</span> <span>True</span>
<span>&gt;</span> <span>t2</span> <span style="color:red;">::</span> <span>Tagged</span> <span>N2</span> <span>Int</span>
<span>&gt;</span> <span>t2</span> <span style="color:red;">=</span> <span>Tagged</span> <span class="hs-num">5</span>
<span>&gt;</span> <span>t3</span> <span style="color:red;">::</span> <span>Tagged</span> <span>N3</span> <span>String</span>
<span>&gt;</span> <span>t3</span> <span style="color:red;">=</span> <span>Tagged</span> <span style="color:teal;">"goodbye"</span>
</code></pre>
<pre><code>   *TSort1&gt; sort (t2 :* t0 :* t1 :* t3)
   Tagged "hello" :* (Tagged True :* (Tagged 5 :* Tagged "goodbye"))
   *TSort1&gt; sort (t3 :* t2 :* t1 :* t0)
   Tagged "hello" :* (Tagged True :* (Tagged 5 :* Tagged "goodbye"))
   *TSort1&gt;</code></pre>
<p>Beyond being an interesting puzzle to solve, there&#8217;s at least some use for such a function. For example, in a DSL that has operations that support named parameter association:</p>
<pre><code><span>&gt;</span> <span>rawConnect</span> <span style="color:red;">::</span> <span style="color:red;">(</span><span>Tagged</span> <span>N0</span> <span>String</span> <span>:*</span> <span>Tagged</span> <span>N1</span> <span>Int</span> <span>:*</span> <span>Tagged</span> <span>N2</span> <span>String</span> <span>:*</span>
<span>&gt;</span>      <span>Tagged</span> <span>N3</span> <span>String</span><span style="color:red;">)</span> <span style="color:red;">-&gt;</span> <span>IO</span> <span>()</span>
<span>&gt;</span> <span>rawConnect</span> <span style="color:red;">(</span><span>Tagged</span> <span>host</span> <span>:*</span> <span>Tagged</span> <span>port</span> <span>:*</span> <span>Tagged</span> <span>user</span> <span>:*</span> <span>Tagged</span> <span>pass</span><span style="color:red;">)</span> <span style="color:red;">=</span>
<span>&gt;</span>     <span>putStrLn</span> <span>$</span> <span style="color:teal;">"connecting to "</span> <span>++</span> <span>host</span> <span>++</span> <span style="color:teal;">" on port "</span> <span>++</span> <span>show</span> <span>port</span> <span>++</span>
<span>&gt;</span>        <span style="color:teal;">" with username "</span> <span>++</span> <span>user</span> <span>++</span> <span style="color:teal;">" and password "</span> <span>++</span> <span>pass</span>
<span>&gt;</span>
<span>&gt;</span> <span>connect</span> <span style="color:red;">=</span> <span>rawConnect</span> <span>.</span> <span>sort</span>
<span>&gt;</span>
<span>&gt;</span> <span style="color:red;">(</span><span>=:</span><span style="color:red;">)</span> <span style="color:red;">=</span> <span style="color:red;">(</span><span>$</span><span style="color:red;">)</span>
<span>&gt;</span> <span>host</span> <span style="color:red;">::</span> <span>String</span> <span style="color:red;">-&gt;</span> <span>Tagged</span> <span>N0</span> <span>String</span>
<span>&gt;</span> <span>host</span> <span style="color:red;">=</span> <span>Tagged</span>
<span>&gt;</span> <span>port</span> <span style="color:red;">::</span> <span>Int</span> <span style="color:red;">-&gt;</span> <span>Tagged</span> <span>N1</span> <span>Int</span>
<span>&gt;</span> <span>port</span> <span style="color:red;">=</span> <span>Tagged</span>
<span>&gt;</span> <span>user</span> <span style="color:red;">::</span> <span>String</span> <span style="color:red;">-&gt;</span> <span>Tagged</span> <span>N2</span> <span>String</span>
<span>&gt;</span> <span>user</span> <span style="color:red;">=</span> <span>Tagged</span>
<span>&gt;</span> <span>password</span> <span style="color:red;">::</span> <span>String</span> <span style="color:red;">-&gt;</span> <span>Tagged</span> <span>N3</span> <span>String</span>
<span>&gt;</span> <span>password</span> <span style="color:red;">=</span> <span>Tagged</span>
</code></pre>
<p>Now you can invoke the &#8216;connect&#8217; operation with parameters in no particular order:</p>
<pre><code><span>&gt;</span> <span>doit</span> <span style="color:red;">=</span> <span style="color:blue;font-weight:bold;">do</span>
<span>&gt;</span>    <span>putStrLn</span> <span style="color:teal;">"trying to connect!"</span>
<span>&gt;</span>    <span>connect</span> <span style="color:red;">(</span> <span>user</span> <span>=:</span> <span style="color:teal;">"admin"</span> <span>:*</span>
<span>&gt;</span>              <span>password</span> <span>=:</span> <span style="color:teal;">"pa55w0rd"</span> <span>:*</span>
<span>&gt;</span>              <span>port</span> <span>=:</span> <span class="hs-num">22</span> <span>:*</span>
<span>&gt;</span>              <span>host</span> <span>=:</span> <span style="color:teal;">"example.com"</span> <span style="color:red;">)</span>
<span>&gt;</span>    <span>putStrLn</span> <span style="color:teal;">"connected!"</span>
</code></pre>
<p>&#8230; which is at least mildly cool, for certain definitions of cool.</p>
<p><em>Brought to you by <a href="http://hackage.haskell.org/package/BlogLiterately">BlogLiterately</a> with generous support from <a href="http://http://johnmacfarlane.net/pandoc/">pandoc</a>, <a href="http://www.cs.york.ac.uk/fp/darcs/hscolour/">hscolour</a> and viewers like you.</em></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/greayer.wordpress.com/179/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/greayer.wordpress.com/179/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/greayer.wordpress.com/179/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/greayer.wordpress.com/179/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/greayer.wordpress.com/179/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/greayer.wordpress.com/179/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/greayer.wordpress.com/179/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/greayer.wordpress.com/179/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/greayer.wordpress.com/179/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/greayer.wordpress.com/179/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/greayer.wordpress.com/179/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/greayer.wordpress.com/179/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/greayer.wordpress.com/179/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/greayer.wordpress.com/179/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=greayer.wordpress.com&amp;blog=7862297&amp;post=179&amp;subd=greayer&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://greayer.wordpress.com/2009/11/15/sorting-type-heterogeneous-values-with-type-families/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/236fba8deba62e3deb53189335900f35?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">robgreayer</media:title>
		</media:content>
	</item>
		<item>
		<title>BlogLiterately v0.2</title>
		<link>http://greayer.wordpress.com/2009/11/03/blogliterately-v0-2/</link>
		<comments>http://greayer.wordpress.com/2009/11/03/blogliterately-v0-2/#comments</comments>
		<pubDate>Tue, 03 Nov 2009 15:04:32 +0000</pubDate>
		<dc:creator>robgreayer</dc:creator>
				<category><![CDATA[haskell]]></category>

		<guid isPermaLink="false">http://greayer.wordpress.com/?p=173</guid>
		<description><![CDATA[I&#8217;ve created a new version of BlogLiterately, now on hackage. The new version is quite similar to what I blogged about here, with some enhancements: it now supports categories. it now supports highlighting-kate, if you&#8217;ve installed Pandoc with highlighting support. you can highlight Haskell with hscolour using CSS classes or inline styles. you can highlight [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=greayer.wordpress.com&amp;blog=7862297&amp;post=173&amp;subd=greayer&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve created a new version of BlogLiterately, now on <a href="http://hackage.haskell.org/package/BlogLiterately-0.2">hackage</a>. The new version is quite similar to what I blogged about <a href="http://greayer.wordpress.com/2009/10/26/blogging-literately-in-haskell/">here</a>, with some enhancements:</p>
<ul>
<li>it now supports categories.</li>
<li>it now supports highlighting-kate, if you&#8217;ve installed <a href="http://johnmacfarlane.net/pandoc/" title="Pandoc">Pandoc</a> with highlighting support.</li>
<li>you can highlight Haskell with hscolour using CSS classes or inline styles.</li>
<li>you can highlight Haskell and non-Haskell with highlighting-kate (via Pandoc). (But only via CSS classes and a separate or embedded stylesheet, not via inline styles. So it does me personally no good!).</li>
</ul>
<p>I contemplate a future upgrade will allow highlighting-kate marked up code to have the CSS class-based styling &#8216;baked&#8217; into the HTML in a way similar to how I handle hscolour.</p>
<p>Anyone who enjoys this tool, thank the authors of <a href="http://johnmacfarlane.net/pandoc/" title="Pandoc">Pandoc</a>, <a href="http://www.cs.york.ac.uk/fp/darcs/hscolour/">hscolour</a>, <a href="http://www.cs.york.ac.uk/fp/HaXml/">HaXml</a>, <a href="http://www.haskell.org/haxr/">HaXR</a> and, of course, <a href="http://www.haskell.org/ghc/">GHC</a> and the <a href="http://www.haskell.org/haskellwiki/Haskell_Platform">Haskell Platform</a>, as this program is just a bit of glue that binds these much bigger pieces together for one simple application.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/greayer.wordpress.com/173/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/greayer.wordpress.com/173/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/greayer.wordpress.com/173/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/greayer.wordpress.com/173/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/greayer.wordpress.com/173/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/greayer.wordpress.com/173/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/greayer.wordpress.com/173/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/greayer.wordpress.com/173/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/greayer.wordpress.com/173/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/greayer.wordpress.com/173/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/greayer.wordpress.com/173/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/greayer.wordpress.com/173/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/greayer.wordpress.com/173/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/greayer.wordpress.com/173/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=greayer.wordpress.com&amp;blog=7862297&amp;post=173&amp;subd=greayer&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://greayer.wordpress.com/2009/11/03/blogliterately-v0-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/236fba8deba62e3deb53189335900f35?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">robgreayer</media:title>
		</media:content>
	</item>
		<item>
		<title>Blogging Literately in Haskell</title>
		<link>http://greayer.wordpress.com/2009/10/26/blogging-literately-in-haskell/</link>
		<comments>http://greayer.wordpress.com/2009/10/26/blogging-literately-in-haskell/#comments</comments>
		<pubDate>Tue, 27 Oct 2009 03:05:33 +0000</pubDate>
		<dc:creator>robgreayer</dc:creator>
				<category><![CDATA[haskell]]></category>

		<guid isPermaLink="false">http://greayer.wordpress.com/?p=92</guid>
		<description><![CDATA[I&#8217;ve made one blog post about Haskell, and found the processs to be too tedious to be tolerated. The wysiwyg tinyMCE editor that is avalaible for WordPress is useless for formatting code segments and editing directly in the raw HTML editor WordPress provides is agonizing. Figuring there must be a better way, I looked around [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=greayer.wordpress.com&amp;blog=7862297&amp;post=92&amp;subd=greayer&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve made one blog post about Haskell, and found the processs to be too tedious to be tolerated. The wysiwyg tinyMCE editor that is avalaible for WordPress is useless for formatting code segments and editing directly in the raw HTML editor WordPress provides is agonizing. Figuring there must be a better way, I looked around for an offline tool that would just work for me. I turned up a few editors, free and non-free, some which looked just horrid, a few almost reasonable, but none that would make the mix of Haskell and narrative as easy as it seems it ought to be. What I really want is an easy way to take Literate Haskell (you know, those seemingly innocuous text messages that are suddenly interrupted with ominous passages like:</p>
<pre><span>&gt;</span> <span style="color:green;">{-# LANGUAGE DeriveDataTypeable #-}</span>
<span>&gt;</span> <span style="color:blue;font-weight:bold;">module</span> <span>BlogLiterately</span> <span style="color:blue;font-weight:bold;">where</span>
</pre>
<p>for no immediately apparent reason) file, perhaps with simple <a href="http://daringfireball.net/projects/markdown/">markdown</a> encoded text commentary, and just magically upload it to my blog, with good HTML formatting of the markdown and Haskell sections.</p>
<p>Haskell, theoretically, has all sorts of libraries for doing all sorts of wonderful things. Perhaps it wouldn&#8217;t be to hard to mash something together from Haskell libraries that did precisely what I wanted.</p>
<p>Somehow I&#8217;d osmotically absorbed from the ether the knowlege that there was a Haskell-based tool around called <a href="http://johnmacfarlane.net/pandoc/" title="Pandoc">Pandoc</a>, by John MacFarlane, which deals with markdown, HTML and other formats. It is, like all good Haskelly things, available on <a href="http://hackage.haskell.org/packages/hackage.html">hackage</a>, both as a program and library. So it seemed like a good place to start.</p>
<pre><span>&gt;</span> <span style="color:blue;font-weight:bold;">import</span> <span>Text</span><span>.</span><span>Pandoc</span>
</pre>
<p>Looking over the documentation for Pandoc, I observed that although was it able to do wonderful things with loads of formats, it didn&#8217;t automatically do what I wanted with the actual Haskell source code. What I want is to make the source look like what Haskell code looks like as part of <a href="http://www.haskell.org/haddock/">haddock</a> documentation, especially the Haddock documentation available on Hackage. Haddock uses <a href="http://www.cs.york.ac.uk/fp/darcs/hscolour/">hscolour</a>, a tool by Malcolm Wallace, to format source listings.</p>
<pre><span>&gt;</span> <span style="color:blue;font-weight:bold;">import</span> <span>Language</span><span>.</span><span>Haskell</span><span>.</span><span>HsColour</span><span style="color:red;">(</span><span>hscolour</span><span style="color:red;">,</span><span>Output</span><span style="color:red;">(</span><span style="color:red;">..</span><span style="color:red;">)</span><span style="color:red;">)</span>
<span>&gt;</span> <span style="color:blue;font-weight:bold;">import</span> <span>Language</span><span>.</span><span>Haskell</span><span>.</span><span>HsColour</span><span>.</span><span>Colourise</span><span style="color:red;">(</span><span>defaultColourPrefs</span><span style="color:red;">)</span>
</pre>
<p>Assuming Pandoc and hscolour do what I want, I&#8217;d still need some way of actually publishing the blog post. With a bit of googliness I learned that my blog software supports something called the <a href="http://www.xmlrpc.com/metaWeblogApi">MetaWeblog</a> API, which is an XML-RPC-based protocol for interacting with blogs.</p>
<p>There&#8217;s a Haskell XML-RPC library, <a href="http://www.haskell.org/haxr/">HaXR</a>, by Bjorn Bringert, (on <a href="http://hackage.haskell.org/package/haxr">hackage</a>) which seems like it should be appropriate.</p>
<pre><span>&gt;</span> <span style="color:blue;font-weight:bold;">import</span> <span>Network</span><span>.</span><span>XmlRpc</span><span>.</span><span>Client</span>
<span>&gt;</span> <span style="color:blue;font-weight:bold;">import</span> <span>Network</span><span>.</span><span>XmlRpc</span><span>.</span><span>Internals</span>
</pre>
<p>And it works that out I&#8217;ll need some miscellaneous other stuff. Since I&#8217;m writing a command line tool, I&#8217;ll need to process the command line arguments, and Neil Mitchell&#8217;s <a href="http://community.haskell.org/~ndm/cmdargs/">CmdArgs</a> library ought to work for that:</p>
<pre><span>&gt;</span> <span style="color:blue;font-weight:bold;">import</span> <span>System</span><span>.</span><span>Console</span><span>.</span><span>CmdArgs</span>
</pre>
<p>I&#8217;m going to end up needing to parse and manipulate XHTML, so I&#8217;ll use Malcolm Wallace&#8217;s <a href="http://www.cs.york.ac.uk/fp/HaXml/">HaXml</a> XML combinators:</p>
<pre><span>&gt;</span> <span style="color:blue;font-weight:bold;">import</span> <span>Text</span><span>.</span><span>XML</span><span>.</span><span>HaXml</span>
<span>&gt;</span> <span style="color:blue;font-weight:bold;">import</span> <span>Text</span><span>.</span><span>XML</span><span>.</span><span>HaXml</span><span>.</span><span>Verbatim</span>
</pre>
<p>I&#8217;ll need to do some text IO, which I&#8217;ll use the UTF8 encoding for, leading to Eric Mertens&#8217; ubiquitous (until GHC 6.12!) <a href="http://hackage.haskell.org/package/utf8-string">utf8-string</a> library:</p>
<pre><span>&gt;</span> <span style="color:blue;font-weight:bold;">import</span> <span style="color:blue;font-weight:bold;">qualified</span> <span>System</span><span>.</span><span>IO</span><span>.</span><span>UTF8</span> <span style="color:blue;font-weight:bold;">as</span> <span>U</span>
</pre>
<p>And I&#8217;ll need a couple other bits and pieces:</p>
<pre><span>&gt;</span> <span style="color:blue;font-weight:bold;">import</span> <span>Control</span><span>.</span><span>Monad</span><span style="color:red;">(</span><span>liftM</span><span style="color:red;">,</span><span>unless</span><span style="color:red;">)</span>
<span>&gt;</span> <span style="color:blue;font-weight:bold;">import</span> <span>Text</span><span>.</span><span>XHtml</span><span>.</span><span>Transitional</span><span style="color:red;">(</span><span>showHtml</span><span style="color:red;">)</span>
<span>&gt;</span> <span style="color:blue;font-weight:bold;">import</span> <span>Text</span><span>.</span><span>ParserCombinators</span><span>.</span><span>Parsec</span>
</pre>
<p>The program I envision will read in a literate Haskell file, use Pandoc to parse it as markdown, then somehow find the code blocks in the (parsed) input, and use hscolour to transform those. Pandoc turns its input into a structure of type:</p>
<pre><span style="color:blue;font-weight:bold;">data</span> <span>Pandoc</span> <span style="color:red;">=</span> <span>Pandoc</span> <span>Meta</span> <span style="color:red;">[</span><span>Block</span><span style="color:red;">]</span></pre>
<p>where a <code>Block</code> (the interesting bit, for my purposes) looks like:</p>
<pre><span style="color:green;">-- | Block element.</span>
<span style="color:blue;font-weight:bold;">data</span> <span>Block</span>
    <span style="color:red;">=</span> <span>Plain</span> <span style="color:red;">[</span><span>Inline</span><span style="color:red;">]</span>        <span style="color:green;">-- ^ Plain text, not a paragraph</span>
    <span style="color:red;">|</span> <span>Para</span> <span style="color:red;">[</span><span>Inline</span><span style="color:red;">]</span>         <span style="color:green;">-- ^ Paragraph</span>
    <span style="color:red;">|</span> <span>CodeBlock</span> <span>Attr</span> <span>String</span> <span style="color:green;">-- ^ Code block (literal) with attributes </span>
    <span style="color:red;">|</span> <span>RawHtml</span> <span>String</span>        <span style="color:green;">-- ^ Raw HTML block (literal)</span>
    <span style="color:red;">|</span> <span>BlockQuote</span> <span style="color:red;">[</span><span>Block</span><span style="color:red;">]</span>    <span style="color:green;">-- ^ Block quote (list of blocks)</span>
    <span style="color:red;">|</span> <span>OrderedList</span> <span>ListAttributes</span> <span style="color:red;">[</span><span style="color:red;">[</span><span>Block</span><span style="color:red;">]</span><span style="color:red;">]</span> <span style="color:green;">-- ^ Ordered list (attributes</span>
                            <span style="color:green;">-- and a list of items, each a list of blocks)</span>
    <span style="color:red;">|</span> <span>BulletList</span> <span style="color:red;">[</span><span style="color:red;">[</span><span>Block</span><span style="color:red;">]</span><span style="color:red;">]</span>  <span style="color:green;">-- ^ Bullet list (list of items, each</span>
                            <span style="color:green;">-- a list of blocks)</span>
    <span style="color:red;">|</span> <span>DefinitionList</span> <span style="color:red;">[</span><span style="color:red;">(</span><span style="color:red;">[</span><span>Inline</span><span style="color:red;">]</span><span style="color:red;">,</span><span style="color:red;">[</span><span>Block</span><span style="color:red;">]</span><span style="color:red;">)</span><span style="color:red;">]</span>  <span style="color:green;">-- ^ Definition list </span>
                            <span style="color:green;">-- (list of items, each a pair of an inline list,</span>
                            <span style="color:green;">-- the term, and a block list)</span>
    <span style="color:red;">|</span> <span>Header</span> <span>Int</span> <span style="color:red;">[</span><span>Inline</span><span style="color:red;">]</span>   <span style="color:green;">-- ^ Header - level (integer) and text (inlines) </span>
    <span style="color:red;">|</span> <span>HorizontalRule</span>        <span style="color:green;">-- ^ Horizontal rule</span>
    <span style="color:red;">|</span> <span>Table</span> <span style="color:red;">[</span><span>Inline</span><span style="color:red;">]</span> <span style="color:red;">[</span><span>Alignment</span><span style="color:red;">]</span> <span style="color:red;">[</span><span>Double</span><span style="color:red;">]</span> <span style="color:red;">[</span><span style="color:red;">[</span><span>Block</span><span style="color:red;">]</span><span style="color:red;">]</span> <span style="color:red;">[</span><span style="color:red;">[</span><span style="color:red;">[</span><span>Block</span><span style="color:red;">]</span><span style="color:red;">]</span><span style="color:red;">]</span>  <span style="color:green;">-- ^ Table,</span>
                            <span style="color:green;">-- with caption, column alignments,</span>
                            <span style="color:green;">-- relative column widths, column headers</span>
                            <span style="color:green;">-- (each a list of blocks), and rows</span>
                            <span style="color:green;">-- (each a list of lists of blocks)</span>
    <span style="color:red;">|</span> <span>Null</span>                  <span style="color:green;">-- ^ Nothing</span>
    <span style="color:blue;font-weight:bold;">deriving</span> <span style="color:red;">(</span><span>Eq</span><span style="color:red;">,</span> <span>Read</span><span style="color:red;">,</span> <span>Show</span><span style="color:red;">,</span> <span>Typeable</span><span style="color:red;">,</span> <span>Data</span><span style="color:red;">)</span></pre>
<p>The literate Haskell that Pandoc finds in a file ends up in various <code>CodeBlock</code> elements of the <code>Pandoc</code> document. Other code can also wind up in <code>CodeBlock</code> elements &#8212; normal markdown formatted code. The literate Haskell code seems to be differentiated from other by the <code>Attr</code> component, which has the form:</p>
<pre><span style="color:blue;font-weight:bold;">type</span> <span>Attr</span> <span style="color:red;">=</span> <span style="color:red;">(</span><span>String</span><span style="color:red;">,</span> <span style="color:red;">[</span><span>String</span><span style="color:red;">]</span><span style="color:red;">,</span> <span style="color:red;">[</span><span style="color:red;">(</span><span>String</span><span style="color:red;">,</span> <span>String</span><span style="color:red;">)</span><span style="color:red;">]</span><span style="color:red;">)</span></pre>
<p>Experimentation reveals that <code>CodeBlock</code> elements that have an <code>Attr</code> of the form <code>(_,[&quot;sourceCode&quot;,&quot;haskell&quot;],_)</code> are literate Haskell code blocks, and other <code>CodeBlock</code> elements are the markdown code blocks. I want to syntax-highlight <strong>both</strong> kinds of code blocks, but when both get rendered to the output HTML, I want to preserve the literate Haskell as literate (it needs the prepended &gt; character). Actually, I want to do just a little bit more&#8230;</p>
<p>When writing a Literate Haskell File, I might want to include non-Haskell but nevertheless &#8216;code&#8217; examples in the text. Samples of how one might express the same thing in ML, or examples of how to run a program, etc. So not <strong>all</strong> code blocks should be colourised by hscolour, just specific Haskelly ones. Markdown doesn&#8217;t provide a way to express what kind of code a code block might be, but since I&#8217;m munging all the code blocks anyway, I can adopt a simple convention. If a code example looks like:</p>
<pre><code>
    [Haskell]
    foo :: String -&gt; String
</code></pre>
<p>it is a Haskell block. If it looks like something else, e.g.</p>
<pre><code>
    [C++]
    cout &lt;&lt; &quot;Hello World!&quot;;
</code></pre>
<p>or
<pre><code>
    [other]
    foo bar baz
</pre>
<p></code>
<p>etc., it is something else.</p>
<p><em>ed: It turns out that although Markdown doesn't provide a way to do this, Pandoc's extensions to Markdown do allow it, in a way fairly similar to what I've proposed. If I revise this further, I'll use the Pandoc convention. Thanks to John MacFarlane for pointing this out in the comments.</em></p>
<p>I can strip off the code type indicator from the beginning of each block as I examine them for code to colourise. I use Parsec to recognize the opening tag:</p>
<pre><span>&gt;</span> <span>unTag</span> <span style="color:red;">::</span> <span>String</span> <span style="color:red;">-&gt;</span> <span style="color:red;">(</span><span>String</span><span style="color:red;">,</span> <span>String</span><span style="color:red;">)</span>
<span>&gt;</span> <span>unTag</span> <span>s</span> <span style="color:red;">=</span> <span>either</span> <span style="color:red;">(</span><span>const</span> <span style="color:red;">(</span><span style="color:teal;">""</span><span style="color:red;">,</span><span>s</span><span style="color:red;">)</span><span style="color:red;">)</span> <span>id</span> <span>$</span> <span>parse</span> <span>tag</span> <span style="color:teal;">""</span> <span>s</span>
<span>&gt;</span>    <span style="color:blue;font-weight:bold;">where</span> <span>tag</span> <span style="color:red;">=</span> <span style="color:blue;font-weight:bold;">do</span>
<span>&gt;</span>              <span>tg</span> <span style="color:red;">&lt;-</span> <span>between</span> <span style="color:red;">(</span><span>char</span> <span style="color:teal;">'['</span><span style="color:red;">)</span> <span style="color:red;">(</span><span>char</span> <span style="color:teal;">']'</span><span style="color:red;">)</span> <span>$</span> <span>many</span> <span>$</span> <span>noneOf</span> <span style="color:teal;">"[]"</span>
<span>&gt;</span>              <span>skipMany</span> <span>$</span> <span>oneOf</span> <span style="color:teal;">" t"</span>
<span>&gt;</span>              <span style="color:red;">(</span><span>string</span> <span style="color:teal;">"rn"</span> <span>&lt;|&gt;</span> <span>string</span> <span style="color:teal;">"n"</span><span style="color:red;">)</span>
<span>&gt;</span>              <span>txt</span> <span style="color:red;">&lt;-</span> <span>many</span> <span>$</span> <span>anyToken</span>
<span>&gt;</span>              <span>eof</span>
<span>&gt;</span>              <span>return</span> <span style="color:red;">(</span><span>tg</span><span style="color:red;">,</span><span>txt</span><span style="color:red;">)</span>
</pre>
<p>To highlight the syntax using hscolour (which produces HTML), I'm going to need to transform the <code>String</code> from a <code>CodeBlock</code> element to a <code>String</code> suitable for the <code>RawHtml</code> element (because the hscolour library transforms Haskell text to HTML). Pandoc strips off the prepended &gt; characters from the literate Haskell, so I need to put them back, and also tell hscolour whether the source it is colouring is literate or not. The hscolour function looks like:</p>
<pre><span>hscolour</span> <span style="color:red;">::</span> <span>Output</span>      <span style="color:green;">-- ^ Output format.</span>
         <span style="color:red;">-&gt;</span> <span>ColourPrefs</span> <span style="color:green;">-- ^ Colour preferences (for formats that support them).</span>
         <span style="color:red;">-&gt;</span> <span>Bool</span>        <span style="color:green;">-- ^ Whether to include anchors.</span>
         <span style="color:red;">-&gt;</span> <span>Bool</span>        <span style="color:green;">-- ^ Whether output document is partial or complete.</span>
         <span style="color:red;">-&gt;</span> <span>String</span>	<span style="color:green;">-- ^ Title for output.</span>
         <span style="color:red;">-&gt;</span> <span>Bool</span>        <span style="color:green;">-- ^ Whether input document is literate haskell or not</span>
         <span style="color:red;">-&gt;</span> <span>String</span>      <span style="color:green;">-- ^ Haskell source code.</span>
         <span style="color:red;">-&gt;</span> <span>String</span>      <span style="color:green;">-- ^ Coloured Haskell source code.</span></pre>
<p>Hscolour supports a few different HTML-based <code>Output</code> formats: <code>HTML</code>, <code>CSS</code>, and <code>ICSS</code>. <code>HTML</code> is HTML with <code>font</code> tags, which are inherently evil and so I'll won't bother exploring this option. <code>CSS</code> is formatting using HTML <code>class</code> tags, which allows you the flexibility of using a separate stylesheet to control how hscolour-annotated code is style. <code>ICSS</code> is 'inline-CSS' a.k.a. HTML 'style' tags, which is really what I need for my WordPress blog, as I refuse to the pay $15 a year WordPress.com charge for the privilege of storing a stylesheet on their site. Unfortunately, hscolour's inline styling options are quite limited (very few colours, little control of fonts), but I've come up with a slightly more convoluted way of turning <code>CSS</code> coloured source into inline-css HTML. So I will start off by using hscolour in <code>CSS</code> mode to transform my source:</p>
<pre><span>&gt;</span> <span>colourIt</span> <span>literate</span> <span>srcTxt</span> <span style="color:red;">=</span>
<span>&gt;</span>     <span>hscolour</span> <span>CSS</span> <span>defaultColourPrefs</span> <span>False</span> <span>True</span> <span style="color:teal;">""</span> <span>literate</span> <span>src'</span>
<span>&gt;</span>     <span style="color:blue;font-weight:bold;">where</span> <span>src'</span> <span style="color:red;">|</span> <span>literate</span> <span style="color:red;">=</span> <span>prepend</span> <span>srcTxt</span>
<span>&gt;</span>                <span style="color:red;">|</span> <span>otherwise</span> <span style="color:red;">=</span> <span>srcTxt</span>
</pre>
<p>Prepending the literate Haskell markers on the source is trivial:</p>
<pre><span>&gt;</span> <span>prepend</span> <span>s</span> <span style="color:red;">=</span> <span>unlines</span> <span>$</span> <span>map</span> <span>p</span> <span>$</span> <span>lines</span> <span>s</span> <span style="color:blue;font-weight:bold;">where</span> <span>p</span> <span>s</span> <span style="color:red;">=</span> <span style="color:teal;">'&gt;'</span><span>:</span><span style="color:teal;">' '</span><span>:</span><span>s</span>
</pre>
<p>Hscolour uses HTML <code>span</code> elements and CSS classes like 'hs-keyword' or <code>hs-keyglyph</code> to markup Haskell code. What I want to do is take each marked <code>span</code> element and replace the <code>class</code> attribute with an inline <code>style</code> element that has the markup I want for that kind of source. I can capture the style posibilities with a type:</p>
<pre><span>&gt;</span> <span style="color:blue;font-weight:bold;">data</span> <span>StylePrefs</span> <span style="color:red;">=</span> <span>StylePrefs</span> <span style="color:red;">{</span>
<span>&gt;</span>     <span>keyword</span>  <span style="color:red;">::</span> <span>String</span><span style="color:red;">,</span>
<span>&gt;</span>     <span>keyglyph</span> <span style="color:red;">::</span> <span>String</span><span style="color:red;">,</span>
<span>&gt;</span>     <span>layout</span>   <span style="color:red;">::</span> <span>String</span><span style="color:red;">,</span>
<span>&gt;</span>     <span>comment</span>  <span style="color:red;">::</span> <span>String</span><span style="color:red;">,</span>
<span>&gt;</span>     <span>conid</span>    <span style="color:red;">::</span> <span>String</span><span style="color:red;">,</span>
<span>&gt;</span>     <span>varid</span>    <span style="color:red;">::</span> <span>String</span><span style="color:red;">,</span>
<span>&gt;</span>     <span>conop</span>    <span style="color:red;">::</span> <span>String</span><span style="color:red;">,</span>
<span>&gt;</span>     <span>varop</span>    <span style="color:red;">::</span> <span>String</span><span style="color:red;">,</span>
<span>&gt;</span>     <span>str</span>      <span style="color:red;">::</span> <span>String</span><span style="color:red;">,</span>
<span>&gt;</span>     <span>chr</span>      <span style="color:red;">::</span> <span>String</span><span style="color:red;">,</span>
<span>&gt;</span>     <span>number</span>   <span style="color:red;">::</span> <span>String</span><span style="color:red;">,</span>
<span>&gt;</span>     <span>cpp</span>      <span style="color:red;">::</span> <span>String</span><span style="color:red;">,</span>
<span>&gt;</span>     <span>selection</span> <span style="color:red;">::</span> <span>String</span><span style="color:red;">,</span>
<span>&gt;</span>     <span>variantselection</span> <span style="color:red;">::</span> <span>String</span><span style="color:red;">,</span>
<span>&gt;</span>     <span>definition</span> <span style="color:red;">::</span> <span>String</span>
<span>&gt;</span>     <span style="color:red;">}</span> <span style="color:blue;font-weight:bold;">deriving</span> <span style="color:red;">(</span><span>Read</span><span style="color:red;">,</span><span>Show</span><span style="color:red;">)</span>
</pre>
<p>Each field in the above type will contain the desired style for the class hscolour assigns to a span of code. A default style that produces something like what the source listings on Hackage look like is:</p>
<pre><span>&gt;</span> <span>defaultStylePrefs</span> <span style="color:red;">=</span> <span>StylePrefs</span> <span style="color:red;">{</span>
<span>&gt;</span>     <span>keyword</span>  <span style="color:red;">=</span> <span style="color:teal;">"color: blue; font-weight: bold;"</span>
<span>&gt;</span>   <span style="color:red;">,</span> <span>keyglyph</span> <span style="color:red;">=</span> <span style="color:teal;">"color: red;"</span>
<span>&gt;</span>   <span style="color:red;">,</span> <span>layout</span>   <span style="color:red;">=</span> <span style="color:teal;">"color: red;"</span>
<span>&gt;</span>   <span style="color:red;">,</span> <span>comment</span>  <span style="color:red;">=</span> <span style="color:teal;">"color: green;"</span>
<span>&gt;</span>   <span style="color:red;">,</span> <span>conid</span>    <span style="color:red;">=</span> <span style="color:teal;">""</span>
<span>&gt;</span>   <span style="color:red;">,</span> <span>varid</span>    <span style="color:red;">=</span> <span style="color:teal;">""</span>
<span>&gt;</span>   <span style="color:red;">,</span> <span>conop</span>    <span style="color:red;">=</span> <span style="color:teal;">""</span>
<span>&gt;</span>   <span style="color:red;">,</span> <span>varop</span>    <span style="color:red;">=</span> <span style="color:teal;">""</span>
<span>&gt;</span>   <span style="color:red;">,</span> <span>str</span>      <span style="color:red;">=</span> <span style="color:teal;">"color: teal;"</span>
<span>&gt;</span>   <span style="color:red;">,</span> <span>chr</span>      <span style="color:red;">=</span> <span style="color:teal;">"color: teal;"</span>
<span>&gt;</span>   <span style="color:red;">,</span> <span>number</span>   <span style="color:red;">=</span> <span style="color:teal;">""</span>
<span>&gt;</span>   <span style="color:red;">,</span> <span>cpp</span>      <span style="color:red;">=</span> <span style="color:teal;">""</span>
<span>&gt;</span>   <span style="color:red;">,</span> <span>selection</span> <span style="color:red;">=</span> <span style="color:teal;">""</span>
<span>&gt;</span>   <span style="color:red;">,</span> <span>variantselection</span> <span style="color:red;">=</span> <span style="color:teal;">""</span>
<span>&gt;</span>   <span style="color:red;">,</span> <span>definition</span> <span style="color:red;">=</span> <span style="color:teal;">""</span>
<span>&gt;</span>   <span style="color:red;">}</span>
</pre>
<p>I can read these preferences in from a file using the <code>Read</code> instance for <code>StylePrefs</code>. I could handle errors better, but this should work:</p>
<pre><span>&gt;</span> <span>getStylePrefs</span> <span style="color:teal;">""</span> <span style="color:red;">=</span> <span>return</span> <span>defaultStylePrefs</span>
<span>&gt;</span> <span>getStylePrefs</span> <span>fname</span> <span style="color:red;">=</span> <span>liftM</span> <span>read</span> <span style="color:red;">(</span><span>U</span><span>.</span><span>readFile</span> <span>fname</span><span style="color:red;">)</span>
</pre>
<p>Hscolour produces a <code>String</code> of HTML. To transform it, we need to parse it, manipulate it and then re-render it as a <code>String</code>. I'll use HaXml to do all of this:</p>
<pre><span>&gt;</span> <span>xformXml</span> <span style="color:red;">::</span> <span>StylePrefs</span> <span style="color:red;">-&gt;</span> <span>String</span> <span style="color:red;">-&gt;</span> <span>String</span>
<span>&gt;</span> <span>xformXml</span> <span>prefs</span> <span>s</span> <span style="color:red;">=</span>  <span>verbatim</span> <span>$</span> <span>filtDoc</span> <span style="color:red;">(</span><span>xmlParse</span> <span style="color:teal;">"input"</span> <span>s</span><span style="color:red;">)</span> <span style="color:blue;font-weight:bold;">where</span>
<span>&gt;</span>     <span style="color:green;">-- filter the document (an Hscoloured fragment of Haskell source)</span>
<span>&gt;</span>     <span>filtDoc</span> <span style="color:red;">(</span><span>Document</span> <span>p</span> <span>s</span> <span>e</span> <span>m</span><span style="color:red;">)</span> <span style="color:red;">=</span>  <span>c</span> <span style="color:blue;font-weight:bold;">where</span>
<span>&gt;</span>         <span style="color:red;">[</span><span>c</span><span style="color:red;">]</span> <span style="color:red;">=</span> <span>filts</span> <span style="color:red;">(</span><span>CElem</span> <span>e</span><span style="color:red;">)</span>
<span>&gt;</span>     <span style="color:green;">-- the filter is a fold of individual filters for each CSS class</span>
<span>&gt;</span>     <span>filts</span> <span style="color:red;">=</span> <span>foldXml</span> <span>$</span> <span>foldl</span> <span>o</span> <span>keep</span> <span style="color:red;">[</span>
<span>&gt;</span>             <span>filt</span> <span style="color:teal;">"keyword"</span> <span>keyword</span><span style="color:red;">,</span>
<span>&gt;</span>             <span>filt</span> <span style="color:teal;">"keyglyph"</span> <span>keyglyph</span><span style="color:red;">,</span>
<span>&gt;</span>             <span>filt</span> <span style="color:teal;">"layout"</span> <span>layout</span><span style="color:red;">,</span>
<span>&gt;</span>             <span>filt</span> <span style="color:teal;">"comment"</span> <span>comment</span><span style="color:red;">,</span>
<span>&gt;</span>             <span>filt</span> <span style="color:teal;">"conid"</span> <span>conid</span><span style="color:red;">,</span>
<span>&gt;</span>             <span>filt</span> <span style="color:teal;">"varid"</span> <span>varid</span><span style="color:red;">,</span>
<span>&gt;</span>             <span>filt</span> <span style="color:teal;">"conop"</span> <span>conop</span><span style="color:red;">,</span>
<span>&gt;</span>             <span>filt</span> <span style="color:teal;">"varop"</span> <span>varop</span><span style="color:red;">,</span>
<span>&gt;</span>             <span>filt</span> <span style="color:teal;">"str"</span> <span>str</span><span style="color:red;">,</span>
<span>&gt;</span>             <span>filt</span> <span style="color:teal;">"chr"</span> <span>chr</span><span style="color:red;">,</span>
<span>&gt;</span>             <span>filt</span> <span style="color:teal;">"num"</span> <span>number</span><span style="color:red;">,</span>
<span>&gt;</span>             <span>filt</span> <span style="color:teal;">"cpp"</span> <span>cpp</span><span style="color:red;">,</span>
<span>&gt;</span>             <span>filt</span> <span style="color:teal;">"sel"</span> <span>selection</span><span style="color:red;">,</span>
<span>&gt;</span>             <span>filt</span> <span style="color:teal;">"variantselection"</span> <span>variantselection</span><span style="color:red;">,</span>
<span>&gt;</span>             <span>filt</span> <span style="color:teal;">"definition"</span> <span>definition</span>
<span>&gt;</span>         <span style="color:red;">]</span>
<span>&gt;</span>     <span style="color:green;">-- an individual filter replaces the attributes of a tag with</span>
<span>&gt;</span>     <span style="color:green;">-- a style attribute when it has a specific 'class' attribute.</span>
<span>&gt;</span>     <span>filt</span> <span>lbl</span> <span>f</span> <span style="color:red;">=</span>
<span>&gt;</span>         <span>replaceAttrs</span> <span style="color:red;">[</span><span style="color:red;">(</span><span style="color:teal;">"style"</span><span style="color:red;">,</span><span>f</span> <span>prefs</span><span style="color:red;">)</span><span style="color:red;">]</span> <span>`when`</span>
<span>&gt;</span>             <span style="color:red;">(</span><span>attrval</span> <span>$</span> <span style="color:red;">(</span><span style="color:teal;">"class"</span><span style="color:red;">,</span><span>AttValue</span> <span style="color:red;">[</span><span>Left</span> <span style="color:red;">(</span><span style="color:teal;">"hs-"</span> <span>++</span> <span>lbl</span><span style="color:red;">)</span><span style="color:red;">]</span><span style="color:red;">)</span><span style="color:red;">)</span>
</pre>
<p>To completely colourise a <code>CodeBlock</code> we now can create a function that transforms a <code>CodeBlock</code> into a <code>RawHtml</code> block, where the content contains marked up Haskell (possibly with literate markers):</p>
<pre><span>&gt;</span> <span>colouriseCodeBlock</span> <span>prefs</span> <span style="color:red;">(</span><span>CodeBlock</span> <span>attr</span><span style="color:red;">@</span><span style="color:red;">(</span><span style="color:blue;font-weight:bold;">_</span><span style="color:red;">,</span><span>inf</span><span style="color:red;">,</span><span style="color:blue;font-weight:bold;">_</span><span style="color:red;">)</span> <span>s</span><span style="color:red;">)</span> <span style="color:red;">=</span>
<span>&gt;</span>     <span style="color:blue;font-weight:bold;">if</span> <span>tag</span> <span>==</span> <span style="color:teal;">"Haskell"</span> <span>||</span> <span>lit</span>
<span>&gt;</span>         <span style="color:blue;font-weight:bold;">then</span> <span>RawHtml</span> <span>$</span> <span>xformXml</span> <span>prefs</span> <span>$</span> <span>colourIt</span> <span>lit</span> <span>s'</span>
<span>&gt;</span>         <span style="color:blue;font-weight:bold;">else</span> <span>CodeBlock</span> <span>attr</span> <span>s'</span>
<span>&gt;</span>     <span style="color:blue;font-weight:bold;">where</span> <span style="color:red;">(</span><span>tag</span><span style="color:red;">,</span><span>s'</span><span style="color:red;">)</span> <span style="color:red;">=</span> <span>unTag</span> <span>s</span>
<span>&gt;</span>           <span>lit</span> <span style="color:red;">=</span> <span style="color:teal;">"sourceCode"</span> <span>`elem`</span> <span>inf</span> <span>&amp;&amp;</span> <span style="color:teal;">"haskell"</span> <span>`elem`</span> <span>inf</span>
<span>&gt;</span> <span>colouriseCodeBlock</span> <span style="color:blue;font-weight:bold;">_</span> <span>b</span> <span style="color:red;">=</span> <span>b</span>
</pre>
<p>And colourising a <code>Pandoc</code> document is simply:</p>
<pre><span>&gt;</span> <span>colourisePandoc</span> <span>prefs</span> <span style="color:red;">(</span><span>Pandoc</span> <span>m</span> <span>blocks</span><span style="color:red;">)</span> <span style="color:red;">=</span>
<span>&gt;</span>     <span>Pandoc</span> <span>m</span> <span>$</span> <span>map</span> <span style="color:red;">(</span><span>colouriseCodeBlock</span> <span>prefs</span><span style="color:red;">)</span> <span>blocks</span>
</pre>
<p>Transforming a complete input document string to an HTML output string:</p>
<pre><span>&gt;</span> <span>xformDoc</span> <span style="color:red;">::</span> <span>StylePrefs</span> <span style="color:red;">-&gt;</span> <span>String</span> <span style="color:red;">-&gt;</span> <span>String</span>
<span>&gt;</span> <span>xformDoc</span> <span>prefs</span> <span>s</span> <span style="color:red;">=</span>
<span>&gt;</span>     <span>showHtml</span>
<span>&gt;</span>     <span>$</span> <span>writeHtml</span> <span>writeOpts</span> <span style="color:green;">-- from Pandoc</span>
<span>&gt;</span>     <span>$</span> <span>colourisePandoc</span> <span>prefs</span>
<span>&gt;</span>     <span>$</span> <span>readMarkdown</span> <span>parseOpts</span> <span style="color:green;">-- from Pandoc</span>
<span>&gt;</span>     <span>$</span> <span>fixLineEndings</span> <span>s</span>
<span>&gt;</span>     <span style="color:blue;font-weight:bold;">where</span> <span>writeOpts</span> <span style="color:red;">=</span> <span>defaultWriterOptions</span> <span style="color:red;">{</span>
<span>&gt;</span>               <span>writerLiterateHaskell</span> <span style="color:red;">=</span> <span>True</span><span style="color:red;">,</span>
<span>&gt;</span>               <span>writerReferenceLinks</span> <span style="color:red;">=</span> <span>True</span> <span style="color:red;">}</span>
<span>&gt;</span>           <span>parseOpts</span> <span style="color:red;">=</span> <span>defaultParserState</span> <span style="color:red;">{</span>
<span>&gt;</span>               <span>stateLiterateHaskell</span> <span style="color:red;">=</span> <span>True</span> <span style="color:red;">}</span>
<span>&gt;</span>           <span style="color:green;">-- readMarkdown is picky about line endings</span>
<span>&gt;</span>           <span>fixLineEndings</span> <span>[]</span> <span style="color:red;">=</span> <span>[]</span>
<span>&gt;</span>           <span>fixLineEndings</span> <span style="color:red;">(</span><span style="color:teal;">'r'</span><span>:</span><span style="color:teal;">'n'</span><span>:</span><span>cs</span><span style="color:red;">)</span> <span style="color:red;">=</span> <span style="color:teal;">'n'</span><span>:</span><span>fixLineEndings</span> <span>cs</span>
<span>&gt;</span>           <span>fixLineEndings</span> <span style="color:red;">(</span><span>c</span><span>:</span><span>cs</span><span style="color:red;">)</span> <span style="color:red;">=</span> <span>c</span><span>:</span><span>fixLineEndings</span> <span>cs</span>
</pre>
<p>Now that I can transform a document, I need to be able to post the document to my blog. The metaWeblog API defines a <code>newPost</code> and <code>editPost</code> procedures that look like:</p>
<pre><code>metaWeblog.newPost (blogid, username, password, struct, publish) returns string
metaWeblog.editPost (postid, username, password, struct, publish) returns true
</code></pre>
<p>For my blog (a WordPress blog), the <code>blogid</code> is just <code>default</code>. The user name and password are simply strings, and <code>publish</code> is a flag indicating whether to load the post as a draft, or to make it public immediately. The <code>postid</code> is an identifier string which is assigned when you initially create a post. The interesting bit is the <code>struct</code> field, which is an XML-RPC structure defining the post along with some meta-data, like the title. All I need is to be able to provide the post text and a title, so I can create the right <code>struct</code> like so:</p>
<pre><span>&gt;</span> <span>mkPost</span> <span>title</span> <span>text</span> <span style="color:red;">=</span>
<span>&gt;</span>     <span style="color:red;">[</span><span style="color:red;">(</span><span style="color:teal;">"title"</span><span style="color:red;">,</span><span>title</span><span style="color:red;">)</span><span style="color:red;">,</span><span style="color:red;">(</span><span style="color:teal;">"description"</span><span style="color:red;">,</span><span>text</span><span style="color:red;">)</span><span style="color:red;">]</span>
</pre>
<p>The HaXR library exports a function for invoking XML-RPC procedures:</p>
<pre><span>remote</span> <span style="color:red;">::</span> <span>Remote</span> <span>a</span> <span style="color:red;">=&gt;</span>
    <span>String</span> <span style="color:green;">-- ^ Server URL. May contain username and password on</span>
           <span style="color:green;">--   the format username:password@ before the hostname.</span>
       <span style="color:red;">-&gt;</span> <span>String</span> <span style="color:green;">-- ^ Remote method name.</span>
       <span style="color:red;">-&gt;</span> <span>a</span>      <span style="color:green;">-- ^ Any function </span>
     <span style="color:green;">-- @(XmlRpcType t1, ..., XmlRpcType tn, XmlRpcType r) =&gt; </span>
                 <span style="color:green;">-- t1 -&gt; ... -&gt; tn -&gt; IO r@</span></pre>
<p>The funtion requires an URL and a method name, and returns a function of type <code>Remote a =&gt; a</code>. Based on the instances defined for <code>Remote</code>, any function with zero or more parameters in the class <code>XmlRpcType</code> and a return type of <code>XmlRpcType r =&gt; IO r</code> will work, which means you can simply 'feed' <code>remote</code> additional arguments as required by the remote procedure, and as long as you make the call in an IO context, it will typecheck. So to call the <code>metaWeblog.newPost</code> procedure, I can do something like:</p>
<pre><span>&gt;</span> <span>postIt</span> <span style="color:red;">::</span> <span>String</span> <span style="color:red;">-&gt;</span> <span>String</span> <span style="color:red;">-&gt;</span> <span>String</span> <span style="color:red;">-&gt;</span> <span>String</span> <span style="color:red;">-&gt;</span> <span>String</span> <span style="color:red;">-&gt;</span> <span>String</span> <span style="color:red;">-&gt;</span> <span>Bool</span> <span style="color:red;">-&gt;</span> <span>IO</span> <span>String</span>
<span>&gt;</span> <span>postIt</span> <span>url</span> <span>blogId</span> <span>user</span> <span>password</span> <span>title</span> <span>text</span> <span>publish</span> <span style="color:red;">=</span>
<span>&gt;</span>     <span>remote</span> <span>url</span> <span style="color:teal;">"metaWeblog.newPost"</span> <span>blogId</span> <span>user</span> <span>password</span> <span style="color:red;">(</span><span>mkPost</span> <span>title</span> <span>text</span><span style="color:red;">)</span> <span>publish</span>
</pre>
<p>To update (replace) a post, the function would be:</p>
<pre><span>&gt;</span> <span>updateIt</span> <span style="color:red;">::</span> <span>String</span> <span style="color:red;">-&gt;</span> <span>String</span> <span style="color:red;">-&gt;</span> <span>String</span> <span style="color:red;">-&gt;</span> <span>String</span> <span style="color:red;">-&gt;</span> <span>String</span> <span style="color:red;">-&gt;</span> <span>String</span> <span style="color:red;">-&gt;</span> <span>Bool</span> <span style="color:red;">-&gt;</span> <span>IO</span> <span>Bool</span>
<span>&gt;</span> <span>updateIt</span> <span>url</span> <span>postId</span> <span>user</span> <span>password</span> <span>title</span> <span>text</span> <span>publish</span> <span style="color:red;">=</span>
<span>&gt;</span>     <span>remote</span> <span>url</span> <span style="color:teal;">"metaWeblog.editPost"</span> <span>postId</span> <span>user</span> <span>password</span> <span style="color:red;">(</span><span>mkPost</span> <span>title</span> <span>text</span><span style="color:red;">)</span> <span>publish</span>
</pre>
<p>I've got most of the pieces in place -- I just need to turn it into a command line program. I can capture the command line controls in a type:</p>
<pre><span>&gt;</span> <span style="color:blue;font-weight:bold;">data</span> <span>BlogLiterately</span> <span style="color:red;">=</span> <span>BlogLiterately</span> <span style="color:red;">{</span>
<span>&gt;</span>        <span>style</span> <span style="color:red;">::</span> <span>String</span><span style="color:red;">,</span>    <span style="color:green;">-- name of a style file</span>
<span>&gt;</span>        <span>publish</span> <span style="color:red;">::</span> <span>Bool</span><span style="color:red;">,</span>    <span style="color:green;">-- an indication of whether the post should be</span>
<span>&gt;</span>                            <span style="color:green;">-- published, or loaded as a draft</span>
<span>&gt;</span>        <span>blogid</span> <span style="color:red;">::</span> <span>String</span><span style="color:red;">,</span>   <span style="color:green;">-- blog-specific identifier (e.g. for blogging</span>
<span>&gt;</span>                            <span style="color:green;">-- software handling multiple blogs)</span>
<span>&gt;</span>        <span>blog</span> <span style="color:red;">::</span> <span>String</span><span style="color:red;">,</span>     <span style="color:green;">-- blog xmlrpc URL</span>
<span>&gt;</span>        <span>user</span> <span style="color:red;">::</span> <span>String</span><span style="color:red;">,</span>     <span style="color:green;">-- blog user name</span>
<span>&gt;</span>        <span>password</span> <span style="color:red;">::</span> <span>String</span><span style="color:red;">,</span> <span style="color:green;">-- blog password</span>
<span>&gt;</span>        <span>title</span> <span style="color:red;">::</span> <span>String</span><span style="color:red;">,</span>    <span style="color:green;">-- post title</span>
<span>&gt;</span>        <span>file</span> <span style="color:red;">::</span> <span>String</span><span style="color:red;">,</span>     <span style="color:green;">-- file to post</span>
<span>&gt;</span>        <span>postid</span> <span style="color:red;">::</span> <span>String</span>    <span style="color:green;">-- id of a post to updated</span>
<span>&gt;</span>     <span style="color:red;">}</span> <span style="color:blue;font-weight:bold;">deriving</span> <span style="color:red;">(</span><span>Show</span><span style="color:red;">,</span><span>Data</span><span style="color:red;">,</span><span>Typeable</span><span style="color:red;">)</span>
</pre>
<p>And using CmdArgs, this bit of impure evil defines how the command line arguments work:</p>
<pre><span>&gt;</span> <span>bl</span> <span style="color:red;">=</span> <span>mode</span> <span>$</span> <span>BlogLiterately</span> <span style="color:red;">{</span>
<span>&gt;</span>     <span>style</span> <span style="color:red;">=</span> <span style="color:teal;">""</span> <span>&amp;=</span> <span>text</span> <span style="color:teal;">"Style Specification"</span> <span>&amp;</span> <span>typFile</span><span style="color:red;">,</span>
<span>&gt;</span>     <span>publish</span> <span style="color:red;">=</span> <span>def</span> <span>&amp;=</span> <span>text</span> <span style="color:teal;">"Publish post"</span><span style="color:red;">,</span>
<span>&gt;</span>     <span>blogid</span> <span style="color:red;">=</span> <span style="color:teal;">"default"</span> <span>&amp;=</span> <span>text</span> <span style="color:teal;">"Blog specific identifier"</span><span style="color:red;">,</span>
<span>&gt;</span>     <span>blog</span> <span style="color:red;">=</span> <span>def</span> <span>&amp;=</span> <span>argPos</span> <span>0</span> <span>&amp;</span> <span>typ</span> <span style="color:teal;">"URL"</span>
<span>&gt;</span>         <span>&amp;</span> <span>text</span> <span style="color:teal;">"URL of blog's xmlrpc address (e.g. http://example.com/blog/xmlrpc.php)"</span><span style="color:red;">,</span>
<span>&gt;</span>     <span>user</span> <span style="color:red;">=</span> <span>def</span> <span>&amp;=</span> <span>argPos</span> <span>1</span> <span>&amp;</span> <span>typ</span> <span style="color:teal;">"USER"</span> <span>&amp;</span> <span>text</span> <span style="color:teal;">"blog author's user name"</span> <span style="color:red;">,</span>
<span>&gt;</span>     <span>password</span> <span style="color:red;">=</span> <span>def</span> <span>&amp;=</span> <span>argPos</span> <span>2</span> <span>&amp;</span> <span>typ</span> <span style="color:teal;">"PASSWORD"</span> <span>&amp;</span> <span>text</span> <span style="color:teal;">"blog author's password"</span><span style="color:red;">,</span>
<span>&gt;</span>     <span>title</span> <span style="color:red;">=</span> <span>def</span> <span>&amp;=</span> <span>argPos</span> <span>3</span> <span>&amp;</span> <span>typ</span> <span style="color:teal;">"TITLE"</span><span style="color:red;">,</span>
<span>&gt;</span>     <span>file</span> <span style="color:red;">=</span> <span>def</span> <span>&amp;=</span>  <span>argPos</span> <span>4</span> <span>&amp;</span> <span>typ</span> <span style="color:teal;">"FILE"</span> <span>&amp;</span> <span>text</span> <span style="color:teal;">"literate haskell file"</span><span style="color:red;">,</span>
<span>&gt;</span>     <span>postid</span> <span style="color:red;">=</span> <span style="color:teal;">""</span> <span>&amp;=</span> <span>text</span> <span style="color:teal;">"Post to replace (if any)"</span><span style="color:red;">}</span>
</pre>
<p>The main blogging function uses the information captured in the <code>BlogLiterately</code> type to read the style preferences, read the input file and transform it, and post it to the blog:</p>
<pre><span>&gt;</span> <span>blogLiterately</span> <span style="color:red;">(</span><span>BlogLiterately</span> <span>style</span> <span>pub</span> <span>blogid</span> <span>url</span> <span>user</span> <span>pw</span> <span>title</span> <span>file</span> <span>postid</span><span style="color:red;">)</span> <span style="color:red;">=</span> <span style="color:blue;font-weight:bold;">do</span>
<span>&gt;</span>     <span>prefs</span> <span style="color:red;">&lt;-</span> <span>getStylePrefs</span> <span>style</span>
<span>&gt;</span>     <span>html</span> <span style="color:red;">&lt;-</span> <span>liftM</span> <span style="color:red;">(</span><span>xformDoc</span> <span>prefs</span><span style="color:red;">)</span> <span>$</span> <span>U</span><span>.</span><span>readFile</span> <span>file</span>
<span>&gt;</span>     <span style="color:blue;font-weight:bold;">if</span> <span>null</span> <span>postid</span>
<span>&gt;</span>         <span style="color:blue;font-weight:bold;">then</span> <span style="color:blue;font-weight:bold;">do</span>
<span>&gt;</span>             <span>postid</span> <span style="color:red;">&lt;-</span> <span>postIt</span> <span>url</span> <span>blogid</span> <span>user</span> <span>pw</span> <span>title</span> <span>html</span> <span>pub</span>
<span>&gt;</span>             <span>putStrLn</span> <span>$</span> <span style="color:teal;">"post Id: "</span> <span>++</span> <span>postid</span>
<span>&gt;</span>         <span style="color:blue;font-weight:bold;">else</span> <span style="color:blue;font-weight:bold;">do</span>
<span>&gt;</span>             <span>result</span> <span style="color:red;">&lt;-</span> <span>updateIt</span> <span>url</span> <span>postid</span> <span>user</span> <span>pw</span> <span>title</span> <span>html</span> <span>pub</span>
<span>&gt;</span>             <span>unless</span> <span>result</span> <span>$</span> <span>putStrLn</span> <span style="color:teal;">"update failed!"</span>
</pre>
<p>And the main program is simply:</p>
<pre><span>&gt;</span> <span>main</span> <span style="color:red;">=</span> <span>cmdArgs</span> <span style="color:teal;">"Blog Literately v0.1, (C) Robert Greayer 2009"</span> <span style="color:red;">[</span><span>bl</span><span style="color:red;">]</span> <span>&gt;&gt;=</span> <span>blogLiterately</span>
</pre>
<p>I can run it to get some help:</p>
<pre><code>$ ./BlogLiterately --help
Blog Literately v0.1, (C) Robert Greayer 2009

blogliterately [FLAG] URL USER PASSWORD TITLE FILE

  -? --help[=FORMAT]  Show usage information (optional format)
  -V --version        Show version information
  -v --verbose        Higher verbosity
  -q --quiet          Lower verbosity
  -s --style=FILE     Style Specification
     --publish        Publish post
  -b --blogid=VALUE   Blog specific identifier (default=default)
     --postid=VALUE   Post to replace (if any)
</code></pre>
<p>Which tells me I can actually upload a post something like:</p>
<pre><code>$ ./BlogLiterately http://greayer.wordpress.com/xmlrpc.php myuser mypass
    &quot;Blogging Literately in Haskell&quot; BlogLiterately.lhs
</code></pre>
<p>This is a great start for what I want. Handling of exceptions is non-existent; I simply cross my fingers and hope that the default error message will be self explanatory. I ought to also allow the author and categories for the document to be specified. But it works as is, and it's a tool I'd actuallly use (and did, to post this).</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/greayer.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/greayer.wordpress.com/92/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/greayer.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/greayer.wordpress.com/92/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/greayer.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/greayer.wordpress.com/92/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/greayer.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/greayer.wordpress.com/92/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/greayer.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/greayer.wordpress.com/92/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/greayer.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/greayer.wordpress.com/92/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/greayer.wordpress.com/92/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/greayer.wordpress.com/92/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=greayer.wordpress.com&amp;blog=7862297&amp;post=92&amp;subd=greayer&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://greayer.wordpress.com/2009/10/26/blogging-literately-in-haskell/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/236fba8deba62e3deb53189335900f35?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">robgreayer</media:title>
		</media:content>
	</item>
		<item>
		<title>Messing with First Class Labels</title>
		<link>http://greayer.wordpress.com/2009/10/14/messing-with-fclabels/</link>
		<comments>http://greayer.wordpress.com/2009/10/14/messing-with-fclabels/#comments</comments>
		<pubDate>Wed, 14 Oct 2009 19:48:32 +0000</pubDate>
		<dc:creator>robgreayer</dc:creator>
				<category><![CDATA[haskell]]></category>

		<guid isPermaLink="false">http://greayer.wordpress.com/?p=17</guid>
		<description><![CDATA[In my sandbox Haskell project, lslplus, I&#8217;ve got massive amounts of complex state which I manage using a few levels of State and Error monad transformers.  The biggest stateful piece of the application is the simulator, which is implemented as a stack of monads:  an ErrorT transformer wrapping StateT transformer that captures the statefulness of the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=greayer.wordpress.com&amp;blog=7862297&amp;post=17&amp;subd=greayer&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In my sandbox Haskell project, <a href="http://lslplus.sourceforge.net" target="_blank">lslplus</a>, I&#8217;ve got massive amounts of complex state which I manage using a few levels of State and Error monad transformers.  The biggest stateful piece of the application is the simulator, which is implemented as a stack of monads:  an ErrorT transformer wrapping StateT transformer that captures the statefulness of the simulation of the simulated &#8216;world&#8217;, which in turn wraps another ErrorT/StateT monad stack for the LSL interpreter.</p>
<p>I&#8217;m unsatisfied with the amount of boilerplate involved in creating functions to manipulate the state in the simulator.  The state is complex, with a top-level &#8216;World&#8217; data type composed of many other records, and collections of records, with ad hoc functions created to support &#8216;deep&#8217; modifications of this structure.  In some other places in the application I&#8217;ve generated state accessor functions using  Template Haskell, but this area of the application was written earlier, and I&#8217;ve wanted to try out one of the libraries available on <a href="http://hackage.haskell.org" target="_blank">Hackage</a>.  The ones I looked at were:</p>
<ul>
<li><a href="http://hackage.haskell.org/package/data-accessor">data-accessor</a></li>
<li><a href="http://hackage.haskell.org/package/lenses" target="_blank">lenses</a></li>
<li><a href="http://hackage.haskell.org/package/fclabels" target="_blank">fclabels</a></li>
</ul>
<p>My intent was to try more than one, but my impression of fclabels was that it really subsumes the functionality of both the data-accessor and lenses packages, so picked that one to try out &#8216;in anger&#8217;.</p>
<p>A first class label identifies a component of  a structure, allowing both access and update. Given a label <em>lbl</em>, a value of type <em>a :-&gt; b </em>(where <em>:-&gt; </em>is an infix type constructor), access and update look something like:</p>
<p><code>get lbl rec <span style="color:#008000;">-- </span><span style="color:#99cc00;"><span style="color:#008000;">get the value of a component of rec, identified by lbl</span><br />
</span>set lbl newVal rec <span style="color:#008000;">-- update a component of rec, identified by lbl</span></code></p>
<p><code><em> </em></code>The advantage is that first class labels themselves compose in a way that normal field-labels do not. Haskell field labels compose well for deep access to a record structure, but not for deep update of a record structure. E.g. given:<br />
<code><br />
<span style="color:#0000ff;"><strong>data</strong></span> Person <span style="color:#ff0000;">=</span> Person <span style="color:red;">{</span> name <span style="color:#ff0000;">::</span> Name, age <span style="color:#ff0000;">::</span> Double <span style="color:red;">}</span><br />
<strong><span style="color:#0000ff;">data</span></strong> Name <span style="color:#ff0000;">=</span> Name <span style="color:red;">{</span> forename <span style="color:#ff0000;">::</span> String, surname <span style="color:#ff0000;">::</span> String <span style="color:red;">}</span></code></p>
<p>you can access the surname of a person <em>p</em> with the expression <em>surname . name $ p</em>, but update requires something much more convoluted, e.g. given a surname <em>nm</em> you&#8217;d need to do something like: <em>p { name = (name p) { surname = nm }}</em>. Field labels don&#8217;t compose for update. With field labels converted to first class labels, though, the expressions become <em>get (surname.name)</em> and <em>set (surname.name) nm p</em> respectively. Nice.  (The composition operator here is from the base module <em><a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/Control-Category.html" target="_blank">Control.Category</a></em>.  It&#8217;s defined for instances of class Category, including (-&gt;) as well as (:-&gt;)).</p>
<p>With a slight change to my records —<code><br />
<strong><span style="color:#0000ff;">data</span></strong> Person <span style="color:#ff0000;">=</span> Person <span style="color:red;">{</span> _name <span style="color:#ff0000;">::</span> Name, _age <span style="color:#ff0000;">::</span> Double <span style="color:red;">}</span><br />
<strong><span style="color:#0000ff;">data</span></strong> Name <span style="color:#ff0000;">=</span> Name <span style="color:red;">{</span> _forename <span style="color:#ff0000;">::</span> String, _surname <span style="color:#ff0000;">::</span> String <span style="color:red;">}</span></code></p>
<p>the fclabels package will derive first class labels for me:<br />
<code><br />
<span style="color:#ff0000;">$(</span>mkLabels <span style="color:#ff0000;">[''</span>Name<span style="color:#ff0000;">,</span>''Person<span style="color:#ff0000;">])</span></code></p>
<p>The following labels are derived:<br />
<code><br />
name <span style="color:#ff0000;">::</span> Person <span style="color:#ff0000;">:-&gt;</span> Name<br />
age <span style="color:#ff0000;">::</span> Person <span style="color:#ff0000;">:-&gt;</span> Double<br />
surname <span style="color:#ff0000;">::</span> Name <span style="color:#ff0000;">:-&gt;</span> String<br />
forename <span style="color:#ff0000;">::</span> Name <span style="color:#ff0000;">:-&gt;</span> String</code></p>
<p>With this virtual dymo label maker, you can build your own labels:<br />
<code><br />
<span style="color:#008000;">-- labels for each element of a pair</span><br />
fstl <span style="color:#ff0000;">::</span> (a,b) <span style="color:#ff0000;">:-&gt;</span> a<br />
fstl <span style="color:#ff0000;">=</span> label fst <span style="color:#ff0000;">(\</span> x <span style="color:#ff0000;">(</span>_<span style="color:#ff0000;">,</span>y<span style="color:#ff0000;">)</span> <span style="color:#ff0000;">-&gt;</span> <span style="color:#ff0000;">(</span>x<span style="color:#ff0000;">,</span>y<span style="color:#ff0000;">))</span><br />
</code><code><br />
sndl <span style="color:#ff0000;">::</span> <span style="color:#ff0000;">(</span>a<span style="color:#ff0000;">,</span>b<span style="color:#ff0000;">)</span> <span style="color:#ff0000;">:-&gt;</span> b<br />
sndl <span style="color:#ff0000;">=</span> label snd <span style="color:#ff0000;">(\</span> y <span style="color:#ff0000;">(</span>x<span style="color:#ff0000;">,</span>_<span style="color:#ff0000;">)</span> <span style="color:#ff0000;">-&gt;</span> <span style="color:#ff0000;">(</span>x<span style="color:#ff0000;">,</span>y<span style="color:#ff0000;">))</span></code></p>
<p>which can come in handy.  But in looking at my own data types, I also run across things that look something like:<br />
<code><br />
<strong><span style="color:#0000ff;">data</span></strong> Item <span style="color:#ff0000;">=</span> Item <span style="color:#ff0000;">{</span> _cost <span style="color:#ff0000;">::</span> Int, _weight <span style="color:#ff0000;">::</span> Double <span style="color:#ff0000;">}</span><br />
<span style="color:#0000ff;"><strong>data</strong></span> Person <span style="color:#ff0000;">=</span> Person <span style="color:#ff0000;">{</span> _name <span style="color:#ff0000;">::</span> String, _age <span style="color:#ff0000;">::</span> Int, _possessions :: <strong><span style="color:#ff0000;">Map String Item</span></strong> <span style="color:red;">}</span><br />
<strong><span style="color:#0000ff;">data</span></strong> World <span style="color:#ff0000;">=</span> World <span style="color:#ff0000;">{</span> _day <span style="color:#ff0000;">::</span> Int<span style="color:#ff0000;">,</span> _people <span style="color:#ff0000;">:</span>: <span style="color:#333300;"><strong><span style="color:#ff0000;">Map String Person</span></strong> </span><span style="color:red;">}</span></code></p>
<p>My deep data structures often contain collections (in this simplified example, a couple of <em>Map</em>s). I do really want to be able to easily compose labels to allow access to particular components of collections in the same (or nearly the same) way as I can access other components of my structures. To compose my way from &#8216;World&#8217; to, say, a persons age, I need to go from &#8216;World&#8217; to a collection (Map String Person) to an element of a collection (Person) to a component of a person (age). The missing piece is the label for the element of a collection. The first approach that sprang to mind was:<br />
<code><br />
lm <span style="color:#ff0000;">::</span> Ord k <span style="color:#ff0000;">=&gt;</span> <span style="color:#ff0000;">(</span>k<span style="color:#ff0000;">,</span>Map k v<span style="color:#ff0000;">)</span> <span style="color:#ff0000;">:-&gt;</span> Maybe v</code></p>
<p>I discarded this because composing this properly would eventually lead to something like:<br />
<code><br />
foo <span style="color:#ff0000;">::</span> <span style="color:#ff0000;">(</span>String<span style="color:#ff0000;">,</span>World<span style="color:#ff0000;">)</span> <span style="color:#ff0000;">:-&gt;</span> Maybe Person<br />
</code><br />
which doesn&#8217;t work well with fclabel&#8217;s &#8216;getM&#8217; and &#8216;setM&#8217; functions — these expect the left hand side of the label arrow to be your state value, which in my case, going to be of type &#8216;World&#8217;, rather than (String,World). So I next tried something like:<br />
<code><br />
lm <span style="color:#ff0000;">::</span> Ord k <span style="color:#ff0000;">=&gt;</span> k <span style="color:#ff0000;">-&gt;</span> Map k v <span style="color:#ff0000;">:-&gt;</span> Maybe v<br />
</code><br />
The immediate question is what does an expression like <em>set (lm &#8220;foo&#8221;) Nothing coll</em>, where <em>coll</em> is some <em>Map String v</em>, mean? An interpretation of this label might be:<br />
<code><br />
lm k <span style="color:#ff0000;">=</span> label <span style="color:#ff0000;">(</span>lookup k<span style="color:#ff0000;">)</span> (\ v m <span style="color:#ff0000;">-&gt;</span> maybe <span style="color:#ff0000;">(</span>delete k m<span style="color:#ff0000;">)</span> <span style="color:#ff0000;">(\</span> v' <span style="color:#ff0000;">-&gt;</span> insert k v' m<span style="color:#ff0000;">)</span> v<span style="color:#ff0000;">)</span><br />
</code><br />
With this definition, <em>set (lm &#8220;foo&#8221;) Nothing coll</em> actually deletes element &#8220;foo&#8221; from collection <em>coll</em>. This makes sense, but, once composed further, say with a label for the &#8216;age&#8217; component of the &#8216;Person&#8217; type, you&#8217;d get a label:<br />
<code><br />
lbl <span style="color:#ff0000;">::</span> String <span style="color:#ff0000;">-&gt;</span> Map String Person <span style="color:#ff0000;">:-&gt;</span> Maybe Int<br />
</code><br />
With the above definition of lm, <em>set (lbl &#8220;foo&#8221;) Nothing coll</em> would also delete the underlying Person from the collection (setting the &#8216;age&#8217; of the person to &#8216;Nothing&#8217; deletes the person). This doesn&#8217;t seem like a desirable semantics. Another issue I found is that while you can create a label with this type (<em> a :-&gt; Maybe b</em>), you can&#8217;t extend this to an arbitrary monad &#8212; you can&#8217;t construct a label of (say) type <em>Monad m =&gt; String :-&gt; m Int</em> because you can&#8217;t create a function of type:</p>
<p><code><span style="color:blue;"><strong>forall</strong></span> m <span style="color:red;">.</span> Monad m <span style="color:red;">=&gt;</span> m Int -&gt; String <span style="color:red;">-&gt;</span> String</code></p>
<p>that depends on the value of its first parameter in any way<a title="other than being strict versus non-strict in it">[1]</a>. Much more useful are labels of type <em>Monad m =&gt; m a :-&gt; m b,</em> which work &#8216;forwards and backwards&#8217; for arbitrary monads, and solve the issue of the semantics of my Map label. The Map label becomes something like:<br />
<code><br />
lm <span style="color:#ff0000;">::</span> <span style="color:#ff0000;">(</span>MonadPlus m<span style="color:#ff0000;">,</span> Ord k<span style="color:#ff0000;">)</span> <span style="color:#ff0000;">=&gt;</span> String <span style="color:#ff0000;">-&gt;</span> m <span style="color:#ff0000;">(</span>Map k v<span style="color:#ff0000;">)</span> <span style="color:#ff0000;">:-&gt;</span> m v<br />
lm k <span style="color:red;">=</span> label getter setter <strong><span style="color:#0000ff;">where</span></strong> </code><code><br />
&nbsp;&nbsp;&nbsp;&nbsp;getter <span style="color:#ff0000;">=</span> lookup k <span style="color:#ff0000;">`</span>liftM<span style="color:#ff0000;">`</span> coll <span style="color:#ff0000;">&gt;&gt;=</span> maybe mzero return<br />
&nbsp;&nbsp;&nbsp;&nbsp;setter mv mc <span style="color:#ff0000;">=</span> mv <span style="color:#ff0000;">&gt;&gt;=</span> <span style="color:#ff0000;">\</span> v <span style="color:#ff0000;">-&gt;</span> mc <span style="color:#ff0000;">&gt;&gt;=</span> <span style="color:#ff0000;">\</span> c <span style="color:#ff0000;">-&gt;</span> return <span style="color:#ff0000;">$</span> insert k v c</code></p>
<p>Or, better, for my purposes (since I&#8217;m using an error monad):<br />
<code><br />
lm <span style="color:#ff0000;">::</span> <span style="color:#ff0000;">(</span>MonadError e m<span style="color:#ff0000;">,</span> Error e<span style="color:#ff0000;">,</span> Show k<span style="color:#ff0000;">,</span> Ord k<span style="color:#ff0000;">)</span> <span style="color:#ff0000;">=&gt;</span> k <span style="color:#ff0000;">-&gt;</span> m <span style="color:#ff0000;">(</span>Map k v<span style="color:#ff0000;">)</span> <span style="color:#ff0000;">:-&gt;</span> m v<br />
lm k <span style="color:#ff0000;">=</span> label getter setter <strong><span style="color:#0000ff;">where</span></strong><br />
&nbsp;&nbsp;&nbsp;&nbsp;getter mm <span style="color:#ff0000;">=</span> mm <span style="color:#ff0000;">&gt;&gt;=</span> <span style="color:#ff0000;">(</span>maybe <span style="color:#ff0000;">(</span>throwError <span style="color:#ff0000;">$</span> err k<span style="color:#ff0000;">)</span> return<span style="color:#ff0000;">)</span> <span style="color:#ff0000;">.</span> lookup k<br />
&nbsp;&nbsp;&nbsp;&nbsp;setter mv mm <span style="color:#ff0000;">=</span> mv <span style="color:#ff0000;">&gt;&gt;=</span> <span style="color:#ff0000;">\</span> v <span style="color:#ff0000;">-&gt;</span> insert k v <span style="color:#ff0000;">`</span>liftM<span style="color:#99ccff;">`</span> mm<br />
&nbsp;&nbsp;&nbsp;&nbsp;err k <span style="color:#ff0000;">=</span> strMsg <span style="color:#ff0000;">$</span> <span style="color:#008080;">"key "</span> <span style="color:#ff0000;">++</span> show k <span style="color:#ff0000;">++</span> <span style="color:#008080;">" not found"</span><br />
</code><br />
Setting with mzero / an error would have no effect on the collection, rather than delete an element. Of course, to compose with other labels, we need to lift the other labels into the monad:<br />
<code><br />
liftML <span style="color:#ff0000;">::</span> Monad m <span style="color:#ff0000;">=&gt;</span> a <span style="color:#ff0000;">:-&gt;</span> b <span style="color:#ff0000;">-&gt;</span> m a <span style="color:#ff0000;">:-&gt;</span> m b<br />
liftML l <span style="color:#ff0000;">=</span> label <span style="color:#ff0000;">(</span>liftM <span style="color:#ff0000;">$</span> get l<span style="color:#ff0000;">)</span> <span style="color:#ff0000;">(</span>liftM2 <span style="color:#ff0000;">$</span> set l<span style="color:#ff0000;">)</span></code></p>
<p>With this definition of <em>lm</em>, along with <em>liftML</em> I&#8217;m should be able to compose my way from &#8216;World&#8217; all the way to the _weight of an item. With explicit lifts, it looks something like:</p>
<p><code>worldWeight k1 k2 <span style="color:#ff0000;">=</span> liftML weight<span style="color:#ff0000;">.</span>lm k2<span style="color:#ff0000;">.</span>liftML possesions<span style="color:#ff0000;">.</span>lm k1<span style="color:#ff0000;">.</span>liftML people</code></p>
<p>To actually use the lifted labels, the fclabels-provided the &#8216;getM&#8217;, &#8216;setM/=:&#8217; and &#8216;modM&#8217; functions don&#8217;t work as expected. For example, with a label <em>l</em> of type <em>Monad m =&gt; m World :-&gt; m Int</em>, the type of <em>getM l</em> is <em>(Monad m, MonadState (m World) m1) =&gt; m1 (m Int)</em>. Some alternate versions are needed:<br />
<code><br />
<strong><span style="color:#0000ff;">import</span></strong> qualified Control<span style="color:#ff0000;">.</span>Monad<span style="color:#ff0000;">.</span>State <strong><span style="color:#0000ff;">as</span> </strong>SM<br />
...<br />
</code><br />
<code><br />
getM <span style="color:#ff0000;">::</span> MonadState s m <span style="color:#ff0000;">=&gt;</span> m s <span style="color:#ff0000;">:-&gt;</span> m b <span style="color:#ff0000;">-&gt;</span> m b<br />
getM l <span style="color:#ff0000;">=</span> get l SM<span style="color:#ff0000;">.</span>get<br />
setM <span style="color:#ff0000;">::</span> MonadState s m <span style="color:#ff0000;">=&gt;</span> m s <span style="color:#ff0000;">:-&gt;</span> m b <span style="color:#ff0000;">-&gt;</span> b <span style="color:#ff0000;">-&gt;</span> m <span style="color:#ff0000;">()</span><br />
setM l v <span style="color:#ff0000;">=</span> set l <span style="color:#ff0000;">(</span>return v) SM<span style="color:#ff0000;">.</span>get <span style="color:#ff0000;">&gt;&gt;=</span> SM.put<br />
modM <span style="color:#ff0000;">::</span> MonadState s m <span style="color:#ff0000;">=&gt;</span> m s <span style="color:#ff0000;">:-&gt;</span> m b <span style="color:#ff0000;">-&gt;</span> <span style="color:#ff0000;">(</span>b <span style="color:#ff0000;">-&gt;</span> b<span style="color:#ff0000;">)</span> <span style="color:#ff0000;">-&gt;</span> m <span style="color:#ff0000;">()</span><br />
modM l f <span style="color:#ff0000;">=</span> mod l <span style="color:#ff0000;">(</span>liftM f<span style="color:#ff0000;">)</span> SM<span style="color:#ff0000;">.</span>get <span style="color:#ff0000;">&gt;&gt;=</span> SM<span style="color:#ff0000;">.</span>put<br />
</code><code><br />
<span style="color:#0000ff;"><strong>infixr</strong></span> 7 <span style="color:#ff0000;">=:</span><br />
<span style="color:#ff0000;">(=:)</span> <span style="color:#ff0000;">=</span> setM</code></p>
<p>We can get rid of the explicit &#8216;liftML&#8217;s by augmenting the fclabel package&#8217;s mkLabels Template Haskell function:<br />
<code><br />
mkLabelsPlus <span style="color:#ff0000;">::</span> <span style="color:#ff0000;">[</span>Name] <span style="color:#ff0000;">-&gt;</span> Q <span style="color:#ff0000;">[</span>Dec<span style="color:#ff0000;">]</span><br />
mkLabelsPlus names <span style="color:#ff0000;">=</span> <strong><span style="color:#0000ff;">do</span></strong><br />
&nbsp;&nbsp;&nbsp;&nbsp;decs <span style="color:#ff0000;">&lt;-</span> mkLabels names<br />
&nbsp;&nbsp;&nbsp;&nbsp;decs2 <span style="color:#ff0000;">&lt;-</span> mapM liftDec decs<br />
&nbsp;&nbsp;&nbsp;&nbsp;return <span style="color:#ff0000;">(</span>decs <span style="color:#ff0000;">++</span> decs2<span style="color:#ff0000;">)</span><br />
<strong><span style="color:#0000ff;">where</span></strong> liftDec <span style="color:#ff0000;">&nbsp;&nbsp;&nbsp;&nbsp;(</span>FunD nm _<span style="color:#ff0000;">)</span> <span style="color:#ff0000;">=</span> funD <span style="color:#ff0000;">(</span>mkName <span style="color:#ff0000;">(</span>nameBase nm <span style="color:#ff0000;">++</span> <span style="color:#008080;">"M"</span><span style="color:#ff0000;">))</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#ff0000;">[</span>clause <span style="color:#ff0000;">[]</span> <span style="color:#ff0000;">(</span>normalB <span style="color:#ff0000;">$</span> <span style="color:#ff0000;">(</span>appE <span style="color:#ff0000;">(</span>varE <span style="color:#ff0000;">'</span>liftML<span style="color:#ff0000;">)</span> <span style="color:#ff0000;">(</span>varE nm<span style="color:#ff0000;">))) []]</span><br />
</code><br />
This function will generate the unlifted labels (e.g. &#8216;person&#8217;, &#8216;weight&#8217;) as well as lifted version of those labels, named with the ever-so-creative &#8216;M&#8217; suffix, (e.g. &#8216;personM&#8217;, &#8216;weightM&#8217;). Since I will mainly use my labels in a monadic context, the extra M&#8217;s are annoying, so an alternate TH function renames the &#8216;unlifted&#8217; labels (with a &#8216;U&#8217; suffix), and uses the simple names for the monadic labels:<br />
<code><br />
mkLabelsAlt <span style="color:#ff0000;">::</span> <span style="color:#ff0000;">[</span>Name<span style="color:#ff0000;">]</span> <span style="color:#ff0000;">-&gt;</span> Q <span style="color:#ff0000;">[</span>Dec<span style="color:#ff0000;">]</span><br />
mkLabelsAlt names <span style="color:#ff0000;">=</span> <strong><span style="color:#0000ff;">do</span></strong><br />
&nbsp;&nbsp;&nbsp;&nbsp;decs <span style="color:#ff0000;">&lt;-</span> mkLabels names<br />
&nbsp;&nbsp;&nbsp;&nbsp;decs2 <span style="color:#ff0000;">&lt;-</span> mapM liftDec decs<br />
&nbsp;&nbsp;&nbsp;&nbsp;return <span style="color:#ff0000;">(</span>map change decs <span style="color:#ff0000;">++</span> decs2<span style="color:#ff0000;">)</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;<strong><span style="color:#0000ff;">where</span></strong> liftDec <span style="color:#ff0000;">(</span>FunD nm _<span style="color:#ff0000;">)</span> = funD <span style="color:#ff0000;">(</span>mkName <span style="color:#ff0000;">(</span>nameBase nm<span style="color:#ff0000;">))</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#ff0000;">[</span>clause <span style="color:#ff0000;">[]</span> <span style="color:#ff0000;">(</span>normalB <span style="color:#ff0000;">$</span> <span style="color:#ff0000;">(</span>appE <span style="color:#ff0000;">(</span>varE <span style="color:#ff0000;">'</span>liftML<span style="color:#ff0000;">)</span> <span style="color:#ff0000;">(</span>varE <span style="color:#ff0000;">(</span>mkName <span style="color:#ff0000;">$</span> nameBase nm <span style="color:#ff0000;">++</span> <span style="color:#008080;">"U"</span><span style="color:#ff0000;">))))</span> <span style="color:#ff0000;">[]]</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;change <span style="color:#ff0000;">(</span>FunD nm x<span style="color:#ff0000;">)</span> = FunD <span style="color:#ff0000;">(</span>mkName <span style="color:#ff0000;">(</span>nameBase nm <span style="color:#ff0000;">++</span> <span style="color:#008080;">"U"</span><span style="color:#ff0000;">))</span> x</code></p>
<p>Now I can easily compose deep access to my state data:</p>
<p><code><br />
<span style="color:#008000;">-- set someones age to the price of their hat...</span><br />
f1 k <span style="color:#ff0000;">=</span> do<br />
&nbsp;&nbsp;&nbsp;&nbsp;hatCost <span style="color:#ff0000;">&lt;-</span> getM (cost<span style="color:#ff0000;">.</span>lm <span style="color:#008080;">"hat"</span><span style="color:#ff0000;">.</span>possessions<span style="color:#ff0000;">.</span>lm k<span style="color:#ff0000;">.</span>people<span style="color:#ff0000;">)</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;(age<span style="color:#ff0000;">.</span>lm k<span style="color:#ff0000;">.</span>people) <span style="color:#ff0000;">=:</span> floor hatCost</code><code><br />
<span style="color:#008000;">-- update or insert a person</span><br />
f2 k <span style="color:#ff0000;">=</span> (lm k<span style="color:#ff0000;">.</span>people<span style="color:#ff0000;">)</span> <span style="color:#ff0000;">=:</span> Person <span style="color:#ff0000;">{</span> _name <span style="color:#ff0000;">=</span> <span style="color:#008080;">"Fred"</span>, _age <span style="color:#ff0000;">=</span> 32<span style="color:#ff0000;">,</span> _possessions <span style="color:#ff0000;">=</span> M.empty <span style="color:#ff0000;">}</span><br />
<span style="color:#008000;">-- set k's age to 32</span><br />
f3 k <span style="color:#ff0000;">=</span> <span style="color:#ff0000;">(</span>age<span style="color:#ff0000;">.</span>lm k<span style="color:#ff0000;">.</span>people<span style="color:#ff0000;">)</span> <span style="color:#ff0000;">=:</span> 32<br />
</code><br />
I can run these in an ErrorT / State monad with a function like:<br />
<code><br />
run <span style="color:#ff0000;">::</span> ErrorT String <span style="color:#ff0000;">(</span>SM<span style="color:#ff0000;">.</span>State World<span style="color:#ff0000;">)</span> a <span style="color:#ff0000;">-&gt;</span> World <span style="color:#ff0000;">-&gt;</span> <span style="color:#ff0000;">(</span>Either String a<span style="color:#ff0000;">,</span> World<span style="color:#ff0000;">)</span><br />
run f <span style="color:#ff0000;">=</span> (runState <span style="color:#ff0000;">.</span> runErrorT<span style="color:#ff0000;">)</span> f</code></p>
<p>a quick test:</p>
<div style="font-family:monospace;font-size:8pt;padding-left:30px;">*FCLTest&gt; <strong>world</strong><br />
World {_timestamp = 0, _people = fromList [("person1",Person {_name = "Joe", _age = 17, _possessions = fromList [("camera",Item {_cost = 599.0, _weight = 250.0}),("ipod",Item {_cost = 195.0, _weight = 36.4})]}),(&#8220;person2&#8243;,Person {_name = &#8220;Zeke&#8221;, _age = <span style="color:#0000ff;"><strong>88</strong></span>, _possessions = fromList [("hat",Item {_cost = 65.0, _weight = 200.0})]})]}<br />
*FCLTest&gt; <strong>run (f1 &#8220;person2&#8243;) world</strong><br />
(Right (),World {_timestamp = 0, _people = fromList [("person1",Person {_name = "Joe", _age = 17, _possessions = fromList [("camera",Item {_cost = 599.0, _weight = 250.0}),("ipod",Item {_cost = 195.0, _weight = 36.4})]}),(&#8220;person2&#8243;,Person {_name = &#8220;Zeke&#8221;, _age = <strong><span style="color:#ff0000;">65</span></strong>, _possessions = fromList [("hat",Item {_cost = 65.0, _weight = 200.0})]})]})</div>
<p>With fclabels, some augmentation, and a small amount of Template Haskell hackery, it looks like I&#8217;ll be able to get rid an immense amount of boilerplate.  I&#8217;ll post some code-reduction stats once available.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/greayer.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/greayer.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/greayer.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/greayer.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/greayer.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/greayer.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/greayer.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/greayer.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/greayer.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/greayer.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/greayer.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/greayer.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/greayer.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/greayer.wordpress.com/17/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=greayer.wordpress.com&amp;blog=7862297&amp;post=17&amp;subd=greayer&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://greayer.wordpress.com/2009/10/14/messing-with-fclabels/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/236fba8deba62e3deb53189335900f35?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">robgreayer</media:title>
		</media:content>
	</item>
		<item>
		<title>Added a new blog</title>
		<link>http://greayer.wordpress.com/2009/07/08/added-a-new-blog/</link>
		<comments>http://greayer.wordpress.com/2009/07/08/added-a-new-blog/#comments</comments>
		<pubDate>Wed, 08 Jul 2009 14:06:44 +0000</pubDate>
		<dc:creator>robgreayer</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://greayer.wordpress.com/?p=15</guid>
		<description><![CDATA[I&#8217;m involved in another blog, the Harvard Farmers Market blog, dedicated to the farmers market in Harvard and to local food in New England in general.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=greayer.wordpress.com&amp;blog=7862297&amp;post=15&amp;subd=greayer&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m involved in another blog, the <a class="wpGallery" title="Harvard Farmers Market" href="http://harvardfarmersmarket.org/wordpress" target="_blank">Harvard Farmers Market </a>blog, dedicated to the farmers market in Harvard and to local food in New England in general.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/greayer.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/greayer.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/greayer.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/greayer.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/greayer.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/greayer.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/greayer.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/greayer.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/greayer.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/greayer.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/greayer.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/greayer.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/greayer.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/greayer.wordpress.com/15/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=greayer.wordpress.com&amp;blog=7862297&amp;post=15&amp;subd=greayer&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://greayer.wordpress.com/2009/07/08/added-a-new-blog/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/236fba8deba62e3deb53189335900f35?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">robgreayer</media:title>
		</media:content>
	</item>
		<item>
		<title>About this blog</title>
		<link>http://greayer.wordpress.com/2009/05/22/about-this-blog/</link>
		<comments>http://greayer.wordpress.com/2009/05/22/about-this-blog/#comments</comments>
		<pubDate>Fri, 22 May 2009 21:52:36 +0000</pubDate>
		<dc:creator>robgreayer</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[meta]]></category>

		<guid isPermaLink="false">http://greayer.wordpress.com/?p=4</guid>
		<description><![CDATA[I have content scattered here and there on the web (a blog about x, a website I help out with about y, some project I contribute to about z, and so on), but no central place to organize it.  This blog is intended to be that place.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=greayer.wordpress.com&amp;blog=7862297&amp;post=4&amp;subd=greayer&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I have content scattered here and there on the web (a blog about x, a website I help out with about y, some project I contribute to about z, and so on), but no central place to organize it.  This blog is intended to be that place.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/greayer.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/greayer.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/greayer.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/greayer.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/greayer.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/greayer.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/greayer.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/greayer.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/greayer.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/greayer.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/greayer.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/greayer.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/greayer.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/greayer.wordpress.com/4/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=greayer.wordpress.com&amp;blog=7862297&amp;post=4&amp;subd=greayer&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://greayer.wordpress.com/2009/05/22/about-this-blog/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/236fba8deba62e3deb53189335900f35?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">robgreayer</media:title>
		</media:content>
	</item>
	</channel>
</rss>
