<?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/"
	>

<channel>
	<title>Aidan Delaney's weblog</title>
	<atom:link href="http://blogs.linux.ie/balor/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.linux.ie/balor</link>
	<description>We eat cats whilst you code.</description>
	<lastBuildDate>Thu, 02 Feb 2012 09:22:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.4</generator>
		<item>
		<title>Clutter-box2dmm bindings updated to 0.12.1</title>
		<link>http://blogs.linux.ie/balor/2012/02/02/clutter-box2dmm-bindings-updated-to-0-12-1/</link>
		<comments>http://blogs.linux.ie/balor/2012/02/02/clutter-box2dmm-bindings-updated-to-0-12-1/#comments</comments>
		<pubDate>Thu, 02 Feb 2012 09:22:39 +0000</pubDate>
		<dc:creator>balor</dc:creator>
				<category><![CDATA[Gnome]]></category>

		<guid isPermaLink="false">http://blogs.linux.ie/balor/?p=436</guid>
		<description><![CDATA[I submitted a patch to Gnome bugzilla last night that updates the clutter-box2d C++ bindings so that they build against the current git master version of clutter-box2d. The patch is a little of a mess. However, this morning, I think the patch is a little less of a mess than I thought last night. Mainly [...]]]></description>
			<content:encoded><![CDATA[<p>I submitted a patch to Gnome bugzilla last night that updates the clutter-box2d C++ bindings so that they build against the current git master version of clutter-box2d.  The patch is a little of a mess.  However, this morning, I think the patch is a little less of a mess than I thought last night.  Mainly because I managed to do the following on the train into work</p>
<p><iframe width="450" height="253" src="http://www.youtube.com/embed/im6VHxDvlSA?fs=1&#038;feature=oembed" frameborder="0" allowfullscreen></iframe></p>
<p>Simply build one of the examples in the clutter-box2dmm folder.</p>
<p>I now have Fedora 15 &amp; 16 packages for cluttermm, clutter-box2d and will have clutter-box2dmm later today.  I&#8217;ll push these clutter-box2d* packages upstream to Fedora later, but I won&#8217;t hold my breath to see them approved.  The approval process is a little unwieldy.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.linux.ie/balor/2012/02/02/clutter-box2dmm-bindings-updated-to-0-12-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pipelines for visual programming languages</title>
		<link>http://blogs.linux.ie/balor/2011/11/01/pipelines-for-visual-programming-languages/</link>
		<comments>http://blogs.linux.ie/balor/2011/11/01/pipelines-for-visual-programming-languages/#comments</comments>
		<pubDate>Tue, 01 Nov 2011 22:32:01 +0000</pubDate>
		<dc:creator>balor</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Gnome]]></category>

		<guid isPermaLink="false">http://blogs.linux.ie/balor/?p=420</guid>
		<description><![CDATA[As a long time Unix user I&#8217;ve always found pilelines to be very useful. They allow you to build up extremely complex functions by sticking together much less complex parts. On Linux we put together commands and pipe the output into the next command. If you ask the question &#8220;How many mp3 files are there [...]]]></description>
			<content:encoded><![CDATA[<p>As a long time Unix user I&#8217;ve always found pilelines to be very useful.  They allow you to build up extremely complex functions by sticking together much less complex parts.  On Linux we put together commands and pipe the output into the next command.  If you ask the question &#8220;How many mp3 files are there in this directory?&#8221; you can say &#8220;Ah, I know how to list all mp3 files, and I know how to count the number of lines in a list&#8221; then the answer becomes<br />
<code><br />
$ ls -l *.mp3 | wc --lines<br />
</code><br />
Pipelines can become arbitrarily complex.  For example, the pipeline<br />
<code><br />
$ cat /var/log/maillog | grep "status=sent" | grep -v phoric | wc --lines<br />
</code><br />
counts the number of lines in my maillog that contain the text &#8220;status=sent&#8221; and omit the text &#8220;phoric&#8221; (my hostname).  There is usually someone smart enough to write a shorter pipeline than what you&#8217;ve written, but that&#8217;s not the point.  The point is that pipelines allow us to construct complex functionality by chaining together small programs.</p>
<p>If I were an academic (and I am), I&#8217;d probably generalise.  I&#8217;d say somthing like, &#8220;pipelines have a source, they have many filter and translate actions and finally an action&#8221;.  This, I believe, is a reasonable generalisation.  In a pipeline, we often take some data, strip out the bits we don&#8217;t need, translate it into another format, maybe filter some more stuff and then finally do something.  Such processes can be (relatively) easily turned into a visual programming language.  The web application <a href="http://ifttt.com/recipes?sort=popular">if this, then that</a> is a simple visual programming language.  Apple&#8217;s <a href="http://en.wikipedia.org/wiki/Automator">Automator</a> is a more general visual programming language for such pipelines on OS X.  And, going back to text-based pipelines, Window&#8217;s Powershell does away with much of the faffing about with text processing found in a Unix pipeline (by encapsulating the faffing in objects)<br />
<code><br />
Get-ChildItem C:\Scripts | Where-Object {$_.Length -gt 200KB} | Sort-Object Length<br />
</code><br />
The powershell example is <a href="http://technet.microsoft.com/en-us/library/ee176927.aspx">stolen from technet.microsoft.com</a>.</p>
<p>Let&#8217;s say I have an application area in mind.  It&#8217;s a technical area with highly complex theory such that we can expect the individual components of our pipeline to contain complex functionality.  Lets take the world of audio and video processing.  The words are technical, we can expect to <em>mux</em> (i.e. multiplex) and <em>demux</em> audio.  And we can also expect to transcode it.  Decoding, say, a flac stream and re-encoding it as a vorbis stream requires complex algorithms.  Furthermore, the language is generally well understood by geeks, like you.  So, how do I do exactly that. Open a flac file, decode it, re-encode it as a vorbis stream and save it to a file.  Fortunatly, a pipeline based toolset for audio (and video) already exists.  We&#8217;re going to look at <a href="http://en.wikipedia.org/wiki/Gstreamer">GStreamer</a>.  It&#8217;s effectively a domain-specific programming language that could be easily implemented as a visual programming language (it previously has been, but the visual side wasn&#8217;t of use to anyone who could actually understand the domain).  We want to look at how GStreamer makes constructing complex pipelines easy.</p>
<p>The following pipeline is the start of an answer our problem (it intentionally doesn&#8217;t work):<br />
<code><br />
$ gst-launch filesrc location=song.flac ! flacdec ! vorbisenc ! filesink location=song.ogg<br />
</code><br />
In the above example we construct the pipeline, where ! is the pipe symbol and launch it with <code>gst-launch</code>.  Each of the pipeline elements, on their own, are simple to understand.  A <code>filesrc</code> element reads a file at the specified <code>location</code>.  A <code>flacdec</code> element decodes a flac stream.  A <code>vorbisenc</code> element encodes something in to vorbis and a <code>filesink</code> element stores a stream back into a file.  The above pipeline produces the error<br />
<code><br />
WARNING: erroneous pipeline: could not link flacdec0 to vorbisenc0<br />
</code><br />
This is <strong>brilliant</strong>.  A major difference between a GStreamer pipeline and a Unix pipeline is that I get static checking.  It can tell me, from the structure of the pipeline, whether the type of data produced by each element, can be consumed by the following element.  This is brilliant because it saves time.</p>
<p>Imagine that my pipeline was much more complicated.  Imagine that the pipeline encoded a long, high-quality, video (taking several hours) and then tried to do something specific with the audio stream.  As a Unix pipeline it would look like<br />
<code><br />
$ cat video.raw | encodevid | modifyaudio &gt; video.webm<br />
</code><br />
We would have to run this pipeline before we saw passing of data from <code>encodevid</code> to <code>decodevid</code> failed.  So having some static checking <em>a-priori</em> (I&#8217;m an acadmeic, I&#8217;m allowed to use Latin) could save us a lot of time.  Static checking, like in a programming language, also ensures that we are passing the right data into an element that handles that data.</p>
<p>Ok, so what&#8217;s a working pipeline for the above question?<br />
<code><br />
$ gst-launch filesrc location=song.flac ! flacdec ! audioconvert ! audioresample ! vorbisenc ! oggmux ! filesink location=song.ogg<br />
</code><br />
Note: there&#8217;s a much shorter pipeline, but this one makes most of the stages explicit.  I was missing the <code>audioconvert</code><code>, the </code><code>audioresample</code> and the <code>filesink</code> steps.  Once again, this is a very complex domain.  Each of the elements in this pipeline are more simple to understand than the entire pipeline.</p>
<p>Let&#8217;s do something magic.  Take the original flac file, convert it to 8-bit audio and re-encode it as a vorbis stream in an ogg container.  Why?  Well this is the point of pipelines.  Someone might want to do this sometime.  The pipeline architecture allows you to combine elements in ways that other people may not have considered, or simply don&#8217;t need.<br />
<code><br />
$ gst-launch filesrc location=song.flac ! flacdec ! audioconvert ! audioresample ! capsfilter caps=audio/x-raw-int,channels=2,width=8,depth=8 ! audioconvert ! vorbisenc ! oggmux ! filesink location=song.ogg<br />
</code></p>
<p>I think that GStreamer is a well desiged, well implemented pipeline architecture for a complex domain.  So how does it achieve what it does?</p>
<p>Each of the elements in GStreamer advertises its capabilities.  Each element <a href="http://gstreamer.freedesktop.org/data/doc/gstreamer/head/manual/html/section-caps-api.html">advertises the capabilities</a> of what it accepts (eg: raw audio with sample freq &gt; 8hHz and &lt; 44kHz) and the capabilities of what it produces (eg: audio bit stream conforming to the vorbis spec).  You can then do static checking on pipelines by ensuring the capabilities of what is produced by one element are a subset of those consumed by the following element.  It&#039;s very neat.</p>
<p>How might you retrofit this into an existing set of complex Unix applications?  You could add a <code>--caps</code> flag (or <code>-caps</code> if you insist on BSD style) to each of your applications.  If you have no source, wrap them in a shell script that takes <code>--caps</code>.  Make the caps option outupt the input and output capabilities of your pipeline elements.  Bonus hipster points if this is outputted in JSON.  Then write a <code>my_static_checker</code> that takes your pipeline as a string, checks it statically a-la-GStreamer (I&#8217;m allowed to abuse French, I&#8217;m an academic) and then executes the pipeline.</p>
<p>Why!  Now we&#8217;re in a position to write a very domain-specific visual programming language.  This could be useful in a lot of cases.  Particularly in the case of at least one person I&#8217;ll be emailing this to.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.linux.ie/balor/2011/11/01/pipelines-for-visual-programming-languages/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Recording presentations</title>
		<link>http://blogs.linux.ie/balor/2011/09/26/recording-presentations/</link>
		<comments>http://blogs.linux.ie/balor/2011/09/26/recording-presentations/#comments</comments>
		<pubDate>Mon, 26 Sep 2011 14:23:36 +0000</pubDate>
		<dc:creator>balor</dc:creator>
				<category><![CDATA[Gnome]]></category>
		<category><![CDATA[Teaching]]></category>

		<guid isPermaLink="false">http://blogs.linux.ie/balor/?p=416</guid>
		<description><![CDATA[I have had a need to record lecture presentations. To this end I&#8217;ve hacked up some software which (a) takes a feed from a webcam, and (b) takes a PDF/ODF presentation and combines them into a WebM file. For the code: git clone git://gitorious.org/lecturec/lecturec.git A simple overview:]]></description>
			<content:encoded><![CDATA[<p>I have had a need to record lecture presentations.  To this end I&#8217;ve hacked up some software which (a) takes a feed from a webcam, and (b) takes a PDF/ODF presentation and combines them into a WebM file.</p>
<p>For the code:<br />
<code><br />
git clone git://gitorious.org/lecturec/lecturec.git<br />
</code></p>
<p>A simple overview:</p>
<p><object width="450" height="253"><param name="movie" value="http://www.youtube.com/v/u8Q9fuJY6Ig?version=3"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/u8Q9fuJY6Ig?version=3" type="application/x-shockwave-flash" width="450" height="253" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.linux.ie/balor/2011/09/26/recording-presentations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Inkscape, textext and pixelated fonts</title>
		<link>http://blogs.linux.ie/balor/2011/07/04/inkscape-textext-and-pixelated-fonts/</link>
		<comments>http://blogs.linux.ie/balor/2011/07/04/inkscape-textext-and-pixelated-fonts/#comments</comments>
		<pubDate>Mon, 04 Jul 2011 22:37:02 +0000</pubDate>
		<dc:creator>balor</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://blogs.linux.ie/balor/?p=413</guid>
		<description><![CDATA[I use Inkscape a lot. Particularly for drawing diagrams for my research. I use the TexText plugin for Inkscape to allow me to embed arbitrary LaTeX in documents. However, recently I&#8217;ve had an issue with it. TexText uses pdf2svg to convert PDFs to SVGs to embed into an Inkscape project. Under the hood it runs [...]]]></description>
			<content:encoded><![CDATA[<p>I use <a href="http://www.inkscape.org">Inkscape</a> a lot.  Particularly for drawing diagrams for my research.  I use the <a href="http://pav.iki.fi/software/textext/">TexText</a> plugin for Inkscape to allow me to embed arbitrary LaTeX in documents.  However, recently I&#8217;ve had an issue with it.</p>
<p>TexText uses <a href="http://www.cityinthesky.co.uk/opensource/pdf2svg">pdf2svg</a> to convert PDFs to SVGs to embed into an Inkscape project.  Under the hood it runs pdflatex on the LaTeX fragment you provide, then it converts this to an SVG with a tight bounding box.  This has been generating horribly pixelated fonts for me.  The output of X+y=z is the following:<br /><a href="http://blogs.linux.ie/balor/files/2011/07/pixels.png"><img src="http://blogs.linux.ie/balor/files/2011/07/pixels.png" alt="Garbled output from pdf2svg" width="50" height="8" class="alignnone size-full wp-image-414" /></a></p>
<p>Having looked into it there are two ways to solve the problem</p>
<ul>
<li>The quick and simple way is to \usepackage{lmodern} at the top of your LaTeX preamble input to TexText &#8211; this forces latex to use type1 (scalable) fonts as opposed to type3 (bitmap) fonts.</li>
<li>The longer term way is to maintain textext for our research group to use.  This will involve dropping all backends other than the pdf2svg one.  Simply for the reason that I have neither the resources nor inclination to support multiple backends.  Each supported backend is a deep mine of bugs and I&#8217;m comfortable with the pdf2svg/poppler/cairo mine.</li>
</ul>
<p>I do wonder though, why LaTeX prefers type3 fonts over their type1 brethren.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.linux.ie/balor/2011/07/04/inkscape-textext-and-pixelated-fonts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>FOlder &#8211; straightforward forms</title>
		<link>http://blogs.linux.ie/balor/2011/06/28/folder-straightforward-forms/</link>
		<comments>http://blogs.linux.ie/balor/2011/06/28/folder-straightforward-forms/#comments</comments>
		<pubDate>Tue, 28 Jun 2011 21:22:57 +0000</pubDate>
		<dc:creator>balor</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Haskell]]></category>

		<guid isPermaLink="false">http://blogs.linux.ie/balor/?p=403</guid>
		<description><![CDATA[So I&#8217;ve got a problem. Every year I have to gather together a set of paper based forms and put them in a folder to hand to someone for QA processing. Most of these forms start off in some word-processing package, but by the time that they require review by a colleague and multiple signatures [...]]]></description>
			<content:encoded><![CDATA[<p>So I&#8217;ve got a problem.  Every year I have to gather together a set of paper based forms and put them in a folder to hand to someone for QA processing.  Most of these forms start off in some word-processing package, but by the time that they require review by a colleague and multiple signatures they end up on paper &#8211; it&#8217;s simply easier.  But I&#8217;m not too good with paper and I&#8217;m even worse with uncodified processes that I follow once a year.  Surely there&#8217;s a way to codify a process of handling documents?  Yeah, there are plenty of tools that do it.  And here&#8217;s another&#8230;</p>
<p>FOlder gives you a web based methods of creating, and writing documents.  It&#8217;s based on the amazing <a href="http://www.aloha-editor.org/">Aloha-Editor</a> and the fantastic <a href="http://happstack.com/index.html">Happstack</a>.  So the technical plumbing is thoroughbred, using a (soon-to-be) distributed (guaranteed) ACID data store.  It&#8217;s missing three major features</p>
<ul>
<li>Authentication (coming in version 0.1),</li>
<li>Collating the created files in a folder (simple fix in version 0.2),</li>
<li>Digital signatures (straightforward to implement, hard to devise a good UI).</li>
</ul>
<p>and one minor feature</p>
<ul>
<li>upload PDFs which are viewable using <a href="http://andreasgal.com/2011/06/15/pdf-js/">PDF.js</a>.
</li>
</ul>
<p>You can find the source code <a href="http://darcsden.com/AidanDelaney/FOlder">here</a>.  I&#8217;m happy to accept patches!</p>
<p><strong>Update:</strong><br />
A quick hacked video on YouTube (uploaded in WebM so it shouldn&#8217;t require flash)</p>
<p><object width="450" height="278"><param name="movie" value="http://www.youtube.com/v/jGQPfi-DaW8?version=3"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/jGQPfi-DaW8?version=3" type="application/x-shockwave-flash" width="450" height="278" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p><a class="FlattrButton" href="http://darcsden.com/AidanDelaney/FOlder"></a><br />
<a href="http://flattr.com/thing/331345/FOlder" target="_blank"><br />
<img src="http://api.flattr.com/button/flattr-badge-large.png" alt="Flattr this" border="0" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.linux.ie/balor/2011/06/28/folder-straightforward-forms/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Three Haskell web frameworks</title>
		<link>http://blogs.linux.ie/balor/2011/06/04/three-haskell-web-frameworks/</link>
		<comments>http://blogs.linux.ie/balor/2011/06/04/three-haskell-web-frameworks/#comments</comments>
		<pubDate>Sat, 04 Jun 2011 20:52:41 +0000</pubDate>
		<dc:creator>balor</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Haskell]]></category>

		<guid isPermaLink="false">http://blogs.linux.ie/balor/?p=395</guid>
		<description><![CDATA[This model-view-controller pattern has been kicking around for quite some time now. It&#8217;s a nice way of separating concerns in many applications including web apps. And, a modern web-framework should both let you separate concerns and, ideally, bridge the gap between your programming model and data storage model. Some really nice people have done this [...]]]></description>
			<content:encoded><![CDATA[<p>This <a href="http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller">model-view-controller</a> pattern has been kicking around for quite some time now.  It&#8217;s a nice way of separating concerns in many applications including web apps.  And, a modern web-framework should both let you separate concerns and, ideally, bridge the gap between your programming model and data storage model.  Some really nice people have done this in <a href="http://rubyonrails.org/">Ruby</a> and <a href="https://www.djangoproject.com/">Python</a> and <a href="http://turbogears.org/">Python</a> and &#8230;. well you get the picture.  If you like a programming language there are probably several modern web-frameworks for it.  So can we ask that question about Haskell?  How would a modern web-framework work in a language that allows <a href="http://www.haskell.org/ghc/docs/7.0.3/html/users_guide/lang-parallel.html">easy parallelisation, straightforward concurrency and even software transactional memory</a>.  Here&#8217;s my brief take on three Haskell web frameworks, with the caveat that I&#8217;ve only used one in anger (unsurprisingly my favourite one).</p>
<p><a href="http://snapframework.com">Snap Framework</a> has been getting a lot of press lately.  It&#8217;s <strong>fast</strong>.  Faster than node.js but if you look at the documentation it appears that it uses a similar model to node.js i.e. a fast event loop.  Snap provides a templating language called Heist.  It has a nice method of handling RESTful queries (code <a href="http://snapframework.com/docs/tutorials/snap-api">stolen from here</a>)<br />
<code><br />
site :: Application ()<br />
site = route [ ("/",            index)<br />
             , ("/echo/:stuff", echo)<br />
             ]<br />
        serveDirectory "resources/static"<br />
</code><br />
Which just fires things off to specific handlers.  Nice.  And, just for good measure, I&#8217;ve stolen <a href="http://snapframework.com/docs/tutorials/heist">this heist snippet</a> too<br />
<code><br />
&lt;html&gt;<br />
    &lt;head&gt;<br />
      &lt;title&gt;Home Page&lt;/title&gt;<br />
    &lt;/head&gt;<br />
    &lt;body&gt;<br />
      &lt;h1&gt;Home Page&lt;/h1&gt;<br />
      &lt;apply template="nav"/&gt;<br />
      &lt;p&gt;Welcome to our home page&lt;/p&gt;<br />
    &lt;/body&gt;<br />
  &lt;/html&gt;<br />
</code><br />
Snap leaves the choice of a storage backend up to the developer.  They&#8217;ve got good reasons for this, however if doesn&#8217;t endear me.  I&#8217;m happer with smart people doing all the integration work and giving me something to use.</p>
<p><a href="http://www.yesodweb.com">Yesod</a> gives you the choice of using either sqlite or postgres as a backing store.  And it&#8217;ll do automagic migration of database schemas.  Defining a datatype for storage is simple<br />
<code><br />
mkPersist [$persist|<br />
    Entry<br />
        title String<br />
        day Day Desc<br />
        content Html'<br />
        deriving<br />
    |]<br />
</code><br />
Again, this code is stolen from the <a href="http://www.yesodweb.com/page/screencasts">very excellent tutorial</a>.  But this is looking straightforward, I get a Haskell datatype that&#8217;s persisted to a standard, trustworthy relational data store.  Now, the people who are writing Snap seem to be scary smart and like to make fast things even faster, but this guy who develops Yesod seems to want to give me all the tools I need to just make a web app.  It&#8217;s worth looking at his screencasts to see how easy OpenID and Facebook authentication is in a Yesod app.  Furthermore, Yesod provides Hamlet, Cassius and Julius &#8211; respectively HTML, CSS and JavaScript templating languages.  Julius, in particular, seems to have been designed with jQuery in mind.  This, again, makes sense to me. Someone is giving me great tools to produce web apps under the standard use-case i.e. nice RESTful blingy things.  You can even see that the <a href="http://www.yesodweb.com/page/screencasts">Yesod query routing</a> is simple<br />
<code><br />
mkYesod "Blog" [$parseRoutes|<br />
/ RootR GET<br />
/entry/#EntryId EntryR GET<br />
/admin AdminR EntryCrud defaultCrud<br />
|]<br />
</code></p>
<p>And finally we get to <a href="http://happstack.com/">Happstack</a>.  Happstack is the most mature of the three and features a <strong>wonderful</strong> data store called <a href="http://acid-state.seize.it/">MACID</a>.  MACID has <a href="http://en.wikipedia.org/wiki/ACID">acid</a> super powers but it <strong>distributes</strong>.  I get to store any Haskell structure I want in a secure, distributed data-store!  Representing reasonably complex information is easy<br />
<code><br />
$(deriveAll [''Show, ''Eq, ''Ord, ''Default]<br />
  [d|<br />
      -- |ConferenceSubmission: a paper submitted to the conference<br />
      data ConferenceSubmission = ConferenceSubmission<br />
          { author  :: String<br />
          , title :: String<br />
          , date    :: ClockTime<br />
          , email :: String<br />
          }</p>
<p>      -- |Conference: a list of ConferenceSubmission<br />
      newtype Conference = Conference { conferenceSubmissions :: [ConferenceSubmission] }<br />
   |])<br />
</code><br />
Routing is a little hairy, but you get a lot of power from it.  And it&#8217;s nicely monadic.<br />
<code><br />
appHandler =<br />
    do decodeBody (defaultBodyPolicy "/tmp/" 4096 4096 4096) -- decode the request body if present.<br />
       msum [ methodM GET &gt;&gt; renderIndex -- matches /<br />
              , renderSubmissions =&lt;&lt; conferenceHandler<br />
            ]<br />
conferenceHandler :: ServerPartT IO (HSP XML)<br />
conferenceHandler =<br />
  dir &quot;submissions&quot; $ msum [postSubmission, getEntries]        -- RESTful /submissions<br />
</code><br />
Happstack uses <a href="http://happstack.com/docs/crashcourse/Templates.html">Blaze or HSP</a> or many other options for its templating language.  Again, Blaze and HSP both play well with jQuery.</p>
<p>I have a fun project to play with over the next few months.  I think Happstack is what I&#8217;ll be using.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.linux.ie/balor/2011/06/04/three-haskell-web-frameworks/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Graphviz and the functional graph library</title>
		<link>http://blogs.linux.ie/balor/2011/05/29/graphviz-and-the-functional-graph-library/</link>
		<comments>http://blogs.linux.ie/balor/2011/05/29/graphviz-and-the-functional-graph-library/#comments</comments>
		<pubDate>Sun, 29 May 2011 20:20:24 +0000</pubDate>
		<dc:creator>balor</dc:creator>
				<category><![CDATA[Haskell]]></category>

		<guid isPermaLink="false">http://blogs.linux.ie/balor/?p=391</guid>
		<description><![CDATA[A friend of mine asked how to display a Haskell Functional Graph Library (FGL) graph using graphviz. Turns out it&#8217;s really straightforward. Martin Erwig (the author of FGL) considered this when writing the library $ ghci &#62; let g=Data.Graph.Inductive.Example.dag4 Use one of the example graphs so that we can print it out. Then &#62; import [...]]]></description>
			<content:encoded><![CDATA[<p>A friend of mine asked how to display a Haskell Functional Graph Library (FGL) graph using graphviz.  Turns out it&#8217;s really straightforward.  <a href="http://web.engr.oregonstate.edu/~erwig">Martin Erwig</a> (the author of FGL) considered this when writing the library</p>
<pre>
$ ghci
&gt; let g=Data.Graph.Inductive.Example.dag4
</pre>
<pre>
Use one of the example graphs so that we can print it out.  Then
</pre>
<pre>
&gt; import Data.Graph.Inductive.Graphviz
&gt; graphviz g "Foo" (100.0, 100.0) (100, 100) Portrait
</pre>
<p>where the documentation for the graphviz function is <a href="http://hackage.haskell.org/packages/archive/fgl/5.4.2.3/doc/html/Data-Graph-Inductive-Graphviz.html">here</a></p>
<p>Of course, you can do much more complex stuff with the graphviz package&#8230;but that&#8217;s for another day.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.linux.ie/balor/2011/05/29/graphviz-and-the-functional-graph-library/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Further homage to QuickCheck</title>
		<link>http://blogs.linux.ie/balor/2011/05/20/further-homage-to-quickcheck/</link>
		<comments>http://blogs.linux.ie/balor/2011/05/20/further-homage-to-quickcheck/#comments</comments>
		<pubDate>Fri, 20 May 2011 11:55:22 +0000</pubDate>
		<dc:creator>balor</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://blogs.linux.ie/balor/?p=388</guid>
		<description><![CDATA[Lets suppose you&#8217;ve got two similar recursive types that differ only in their &#8220;payload&#8221;. For example, you want a binary tree of boolean operations to store a Char and another similar tree to store a set of Char. Let&#8217;s call them Tree and STree. Something like in the following, non-working, code data Tree = And [...]]]></description>
			<content:encoded><![CDATA[<p>Lets suppose you&#8217;ve got two similar recursive types that differ only in their &#8220;payload&#8221;.  For example, you want a binary tree of boolean operations to store a Char and another similar tree to store a set of Char.  Let&#8217;s call them Tree and STree.  Something like in the following, non-working, code</p>
<pre>
data Tree = And Tree Tree
  | Or Tree Tree
  | Leaf Char

data STree = And STree STree
  | Or STree STree
  | Leaf (Set Char)
</pre>
<p>We&#8217;re repeating a lot of boiler plate here.  It&#8217;d be great if I could parameterise the tree, which I can.</p>
<pre>
data MTree t = And (MTree t) (MTree t)
  | Or (MTree t) (MTree t)
  | Leaf t
</pre>
<p>This is great, it removes boilerplate, and gives me less code to test.</p>
<p>Take our Tree as defined above.  It&#8217;s easy to create an Arbitrary Tree.  The following code creates an arbitrary tree with a maximum branch depth (generally for efficiency reasons you don&#8217;t want test trees that are either (arbitrary :: Int) or infinitely deep)</p>
<pre><font color="Green"><u>import</u></font> Test<font color="Cyan">.</font>QuickCheck
<font color="Green"><u>import</u></font> Control<font color="Cyan">.</font>Monad

<font color="Green"><u>data</u></font> Tree <font color="Red">=</font> And Tree Tree
  <font color="Red">|</font> Or Tree Tree
  <font color="Red">|</font> Leaf Char
  <font color="Green"><u>deriving</u></font> Show

<font color="Blue">arbitraryTree</font> <font color="Red">::</font> Int <font color="Red">-&gt;</font> Gen Tree
<font color="Blue">arbitraryTree</font> <font color="Magenta">0</font>        <font color="Red">=</font>
  <font color="Green"><u>do</u></font>
    c <font color="Red">&lt;-</font> arbitrary
    return <font color="Cyan">(</font>Leaf c<font color="Cyan">)</font>
<font color="Blue">arbitraryTree</font> maxDepth <font color="Red">=</font>
  <font color="Green"><u>do</u></font>
    t <font color="Red">&lt;-</font> oneof <font color="Red">[</font>arbitraryTree <font color="Magenta">0</font><font color="Cyan">,</font>
                liftM2 And <font color="Cyan">(</font>arbitraryTree depth'<font color="Cyan">)</font> <font color="Cyan">(</font>arbitraryTree depth'<font color="Cyan">)</font><font color="Cyan">,</font>
                liftM2 Or <font color="Cyan">(</font>arbitraryTree depth'<font color="Cyan">)</font> <font color="Cyan">(</font>arbitraryTree depth'<font color="Cyan">)</font><font color="Red">]</font>
    return t
  <font color="Green"><u>where</u></font>
    depth' <font color="Red">=</font> maxDepth <font color="Blue"><i>-</i></font><font color="Magenta">1</font>

<font color="Green"><u>instance</u></font> Arbitrary Tree <font color="Green"><u>where</u></font>
  arbitrary <font color="Red">=</font> <font color="Green"><u>do</u></font>
    maxDepth <font color="Red">&lt;-</font> choose <font color="Cyan">(</font><font color="Magenta">1</font><font color="Cyan">,</font> <font color="Magenta">10</font><font color="Cyan">)</font>
    t <font color="Red">&lt;-</font> arbitraryTree maxDepth
    return t
</pre>
<p>So how do you create an arbitrary MTree?  The following code makes it all work.</p>
<pre><font color="Green"><u>import</u></font> Test<font color="Cyan">.</font>QuickCheck
<font color="Green"><u>import</u></font> Control<font color="Cyan">.</font>Monad

<font color="Green"><u>data</u></font> MTree t <font color="Red">=</font> And <font color="Cyan">(</font>MTree t<font color="Cyan">)</font> <font color="Cyan">(</font>MTree t<font color="Cyan">)</font>
  <font color="Red">|</font> Or <font color="Cyan">(</font>MTree t<font color="Cyan">)</font> <font color="Cyan">(</font>MTree t<font color="Cyan">)</font>
  <font color="Red">|</font> Leaf t
  <font color="Green"><u>deriving</u></font> Show

<font color="Blue">arbitraryMTree</font> <font color="Red">::</font> Arbitrary t <font color="Red">=&gt;</font> Int <font color="Red">-&gt;</font> Gen <font color="Cyan">(</font>MTree t<font color="Cyan">)</font>
<font color="Blue">arbitraryMTree</font> <font color="Magenta">0</font>        <font color="Red">=</font>
  <font color="Green"><u>do</u></font>
    c <font color="Red">&lt;-</font> arbitrary
    return <font color="Cyan">(</font>Leaf c<font color="Cyan">)</font>
<font color="Blue">arbitraryMTree</font> maxDepth <font color="Red">=</font>
  <font color="Green"><u>do</u></font>
    tr <font color="Red">&lt;-</font> oneof <font color="Red">[</font>arbitraryMTree <font color="Magenta">0</font><font color="Cyan">,</font>
                liftM2 And <font color="Cyan">(</font>arbitraryMTree depth'<font color="Cyan">)</font> <font color="Cyan">(</font>arbitraryMTree depth'<font color="Cyan">)</font><font color="Cyan">,</font>
                liftM2 Or <font color="Cyan">(</font>arbitraryMTree depth'<font color="Cyan">)</font> <font color="Cyan">(</font>arbitraryMTree depth'<font color="Cyan">)</font><font color="Red">]</font>
    return tr
  <font color="Green"><u>where</u></font>
    depth' <font color="Red">=</font> maxDepth <font color="Blue"><i>-</i></font><font color="Magenta">1</font>

<font color="Green"><u>instance</u></font> Arbitrary t <font color="Red">=&gt;</font> Arbitrary <font color="Cyan">(</font>MTree t<font color="Cyan">)</font> <font color="Green"><u>where</u></font>
  arbitrary <font color="Red">=</font> <font color="Green"><u>do</u></font>
    maxDepth <font color="Red">&lt;-</font> choose <font color="Cyan">(</font><font color="Magenta">1</font><font color="Cyan">,</font> <font color="Magenta">10</font><font color="Cyan">)</font>
    tr <font color="Red">&lt;-</font> arbitraryMTree maxDepth
    return tr
</pre>
<p>This is remarkably neat.  QuickCheck, for me, is the killer feature of functional programming!</p>
<p>Returning to the <a href="http://blogs.linux.ie/balor/2011/05/17/creating-arbitrary-foo-for-quickcheck/">Squad example</a> from the other day.  We can create Arbitrary instances of Squad.</p>
<pre>
import Data.Set
import Data.List
import Test.QuickCheck
import Control.Monad

data Squad a = Squad { players :: (Set a), team :: a}

instance (Ord a, Arbitrary a) =&gt; Arbitrary (Squad a) where
  arbitrary =
    do
    ps &lt;- listOf1 arbitrary
    t &lt;- elements ps
    return (Squad (fromList ps) t)
</pre>
<p>But now I have a complication (and this is where the Squad metaphor breaks down, but please bare with me).  Suppose I want a Squad of players where I can in some instances choose a subset of them to play, and in other instances only choose one of them to play.  So the Squad still constains a set of players, but the team can sometimes be a subset of the players, and sometimes is a single player.  We&#8217;ve got to allow instances of TypeSynonyms:</p>
<pre><font color="Blue"><i>{-# LANGUAGE TypeSynonymInstances #-}</i></font>
<font color="Green"><u>import</u></font> Data<font color="Cyan">.</font>Set
<font color="Green"><u>import</u></font> Data<font color="Cyan">.</font>List
<font color="Green"><u>import</u></font> Test<font color="Cyan">.</font>QuickCheck
<font color="Green"><u>import</u></font> Control<font color="Cyan">.</font>Monad

<font color="Green"><u>data</u></font> Squad a b <font color="Red">=</font> Squad <font color="Cyan">{</font> players <font color="Red">::</font> <font color="Cyan">(</font>Set a<font color="Cyan">)</font><font color="Cyan">,</font> team <font color="Red">::</font> b<font color="Cyan">}</font>
<font color="Green"><u>type</u></font> SetSquad <font color="Red">=</font> Squad Char <font color="Cyan">(</font>Set Char<font color="Cyan">)</font>
<font color="Green"><u>type</u></font> SingleSquad <font color="Red">=</font> Squad Char Char

<font color="Green"><u>instance</u></font> Arbitrary SetSquad <font color="Green"><u>where</u></font>
  arbitrary <font color="Red">=</font>
    <font color="Green"><u>do</u></font>
    ps <font color="Red">&lt;-</font> listOf1 arbitrary
    t <font color="Red">&lt;-</font> elements <font color="Cyan">(</font>subsequences ps<font color="Cyan">)</font>
    return <font color="Cyan">(</font>Squad <font color="Cyan">(</font>fromList ps<font color="Cyan">)</font> <font color="Cyan">(</font>fromList t<font color="Cyan">)</font><font color="Cyan">)</font>

<font color="Green"><u>instance</u></font> Arbitrary SingleSquad <font color="Green"><u>where</u></font>
  arbitrary <font color="Red">=</font>
    <font color="Green"><u>do</u></font>
    ps <font color="Red">&lt;-</font> listOf1 arbitrary
    t <font color="Red">&lt;-</font> elements ps
    return <font color="Cyan">(</font>Squad <font color="Cyan">(</font>fromList ps<font color="Cyan">)</font> t<font color="Cyan">)</font>
</pre>
<p>Which is awesome.</p>
<p>Mad props to my homeboy <a href="http://koweycode.blogspot.com/">Eric</a> (I&#8217;ve been told that&#8217;s how one addresses a USian) for listening to my overtired ramblings last night, and for pointing me at numerous ways to implement what I wanted to.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.linux.ie/balor/2011/05/20/further-homage-to-quickcheck/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Recursive types in QuickCheck</title>
		<link>http://blogs.linux.ie/balor/2011/05/18/recursive-types-in-quickcheck/</link>
		<comments>http://blogs.linux.ie/balor/2011/05/18/recursive-types-in-quickcheck/#comments</comments>
		<pubDate>Wed, 18 May 2011 19:55:26 +0000</pubDate>
		<dc:creator>balor</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://blogs.linux.ie/balor/?p=385</guid>
		<description><![CDATA[I want to build arbitrary sized trees of Compound data Compound = And Compound Compound &#124; Or Compound Compound &#124; Product Compound Compound &#124; Not Compound &#124; Leaf { diagram :: Unitary} deriving (Show, Eq) However, I want to limit to a max depth, and I want to have each branch to be of an [...]]]></description>
			<content:encoded><![CDATA[<p>I want to build arbitrary sized trees of <code>Compound</code></p>
<pre>
<font color="Green"><u>data</u></font> Compound <font color="Red">=</font> And Compound Compound
  <font color="Red">|</font> Or Compound Compound
  <font color="Red">|</font> Product Compound Compound
  <font color="Red">|</font> Not Compound
  <font color="Red">|</font> Leaf <font color="Cyan">{</font> diagram <font color="Red">::</font> Unitary<font color="Cyan">}</font> <font color="Green"><u>deriving</u></font> <font color="Cyan">(</font>Show<font color="Cyan">,</font> Eq<font color="Cyan">)</font>
</pre>
<p>However, I want to limit to a max depth, and I want to have each branch to be of an arbitrary depth.</p>
<pre>
<font color="Blue">mkBinary</font> <font color="Red">::</font> <font color="Cyan">(</font>Compound <font color="Red">-&gt;</font> Compound <font color="Red">-
&gt;</font> Compound<font color="Cyan">)</font> <font color="Red">-&gt;</font> Int <font color="Red">-&gt;</font> Gen Compound
<font color="Blue">mkBinary</font> f depth <font color="Red">=</font>
  liftM2 f <font color="Cyan">(</font>mkCompound depth'<font color="Cyan">)</font> <font color="Cyan">(</font>mkCompound depth'<font color="Cyan">)</font>
  <font color="Green"><u>where</u></font>
    depth' <font color="Red">=</font> depth <font color="Blue"><i>-</i></font><font color="Magenta">1</font>

<font color="Blue">mkNot</font> <font color="Red">::</font> Int <font color="Red">-&gt;</font> Gen Compound
<font color="Blue">mkNot</font> depth <font color="Red">=</font>
  liftM Not <font color="Cyan">(</font>mkCompound <font color="Cyan">(</font>depth<font color="Blue"><i>-</i></font><font color="Magenta">1</font><font>)</font><font color="Cyan">)</font>

<font color="Blue">mkLeaf</font> <font color="Red">::</font> Gen Compound
<font color="Blue">mkLeaf</font> <font color="Red">=</font> liftM Leaf arbitrary

<font color="Blue">mkCompound</font> <font color="Red">::</font> Int <font color="Red">-&gt;</font> Gen Compound
<font color="Blue">mkCompound</font> <font color="Magenta">0</font> <font color="Red">=</font> mkLeaf
<font color="Blue">mkCompound</font> depth <font color="Red">=</font> oneof <font color="Red">[</font>mkLeaf<font color="Cyan">,</font> mkBinary And depth, mkBinary Or depth<font color="Cyan">,</font> mkBinary Product depth<font color="Cyan">,</font> mkNot depth<font color="Red">]

</font><font color="Green"><u>instance</u></font> Arbitrary Compound <font color="Green"><u>where</u></font>
  arbitrary <font color="Red">=</font>
    <font color="Green"><u>do</u></font> depth <font color="Red">&lt;-</font> choose <font color="Cyan">(</font><font color="Magenta">0</font><font color="Cyan">,</font> <font color="Magenta">4</font><font color="Cyan">)</font>
       d <font color="Red">&lt;-</font> mkCompound depth
       return d
</pre>
<p>QuickCheck can do it.  It&#8217;s nice.  Use it.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.linux.ie/balor/2011/05/18/recursive-types-in-quickcheck/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating Arbitrary Foo for QuickCheck</title>
		<link>http://blogs.linux.ie/balor/2011/05/17/creating-arbitrary-foo-for-quickcheck/</link>
		<comments>http://blogs.linux.ie/balor/2011/05/17/creating-arbitrary-foo-for-quickcheck/#comments</comments>
		<pubDate>Tue, 17 May 2011 14:48:19 +0000</pubDate>
		<dc:creator>balor</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Teaching]]></category>

		<guid isPermaLink="false">http://blogs.linux.ie/balor/?p=377</guid>
		<description><![CDATA[I always write semi-automated tests, whatever language I&#8217;m using. In Haskell my preferred framework is QuickCheck, which goes beyond what an XUnit framework is capable of. I come to QuickCheck as a convert from the old religion of Object Oriented programming. However much I&#8217;ve adopted functional practices I find myself, in the darkest of times, [...]]]></description>
			<content:encoded><![CDATA[<p>I always write <a href="http://blog.ezyang.com/2011/02/semi-automatic-testing/">semi-automated tests</a>, whatever language I&#8217;m using.  In Haskell my preferred framework is QuickCheck, which goes beyond what an XUnit framework is capable of.  I come to QuickCheck as a convert from the old religion of Object Oriented programming.  However much I&#8217;ve adopted functional practices I find myself, in the darkest of times, falling back on idioms from a past life.  Recently I&#8217;ve been testing code that looks like the following:<br />
<code><br />
data Squad = Squad { players :: Set Char,<br />
                                 team :: Set Char }<br />
</code><br />
So what I&#8217;ve got is some type which is composed of a set and some other set which is a subset of the original.  I hope the Squad metaphor is reasonable.  In many sports you&#8217;ll have a panel of players, called the squad, and then for any given game you&#8217;ll have a team which is a subset of the players in your squad.  To simplify this problem, each player is identified by a single character rather than a name.  Now, we don&#8217;t have a <a href="http://en.wikipedia.org/wiki/Dependent_type">dependent</a> type system here.  Just plain old Haskell.  So the constraint that &#8220;team is a subset of players&#8221; is not enforced by the type system.</p>
<p>Now I want to tell Haskell to create Arbitrary instances of Squad.<br />
<code><br />
randSubset :: Ord a =&gt; Set a -&gt; Gen (Set a)<br />
randSubset xs = do {<br />
        mask &lt;- vectorOf (Set.size xs) arbitrary;<br />
        return  (Set.fromList [ e | (e,True) &lt;- zip (Set.toList xs) mask ]) }</p>
<p>instance Arbitrary Squad where<br />
  arbitrary = do {<br />
    players &lt;- listOf1 (arbitrary :: Gen Char);<br />
    team &lt;- randSubset (Set.fromList players);<br />
    return (Squad (Set.fromList players) team))}<br />
</code><br />
The smarts here is in <code>randSubset</code>, but listOf1 ensures that there&#8217;s at least one player in our squad.  I was struggling when playing around with a random number generator, to try and pull out random elements from a Set.  Such thinking is a false idol.  With some guidance from the only <a href="http://koweycode.blogspot.com/">person</a> on the planet to be 90% Real Jedi I realised you can just use the <a href="http://hackage.haskell.org/packages/archive/QuickCheck/latest/doc/html/Test-QuickCheck-Gen.html">common generator combinators</a> as provided by QuickCheck.</p>
<p>So the fragment<br />
<code><br />
do {<br />
  mask &lt;- vectorOf (Set.size xs) arbitrary;<br />
  return  (Set.fromList [ e | (e,True) &lt;- zip (Set.toList xs) mask ])}<br />
</code><br />
asks for an arbitrary list of boolean values where the list is the same length as the size of the input Set.  Then where the ith element of the list is true, the ith element of the set is considered to be in the subset, or omitted otherwise.  It&#8217;s a nice idiom, useful anywhere you&#8217;re playing with sets that aren&#8217;t quite arbitrary themselves, but are constrained to be a subset of another.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.linux.ie/balor/2011/05/17/creating-arbitrary-foo-for-quickcheck/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

