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

<channel>
	<title>/dev/collective</title>
	<atom:link href="http://www.hvergi.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.hvergi.net</link>
	<description>Just another programming weblog</description>
	<pubDate>Tue, 18 Nov 2008 12:15:39 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
	<language>en</language>
			<item>
		<title>Icelandic government math</title>
		<link>http://www.hvergi.net/2008/11/icelandic-government-math/</link>
		<comments>http://www.hvergi.net/2008/11/icelandic-government-math/#comments</comments>
		<pubDate>Tue, 18 Nov 2008 12:14:46 +0000</pubDate>
		<dc:creator>Arnar</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.hvergi.net/?p=39</guid>
		<description><![CDATA[A good friend sent this to me just now: The Icelandic government seems to use their own rules for arithmetic. From the Letter of Intent they sent to IMF:

The collapse of the banking system has left us with considerable external
financing needs. We estimate this need to be about $25 billion during the period from now
and [...]]]></description>
			<content:encoded><![CDATA[<p>A good friend sent this to me just now: The Icelandic government seems to use their own rules for arithmetic. From the <a href="http://www.forsaetisraduneyti.is/media/Skyrslur/LOI.pdf">Letter of Intent</a> they sent to IMF:</p>

<p style="padding-left: 30px;"><strong>The collapse of the banking system has left us with considerable external
financing needs.</strong> We estimate this need to be about $25 billion during the period from now
and until the end of 2010. Of this, about $19 billion are composed by arrears on obligations
of the three intervened private banks as well as financing earmarked for payments in relation
to foreign deposits, leaving a cash financing need of $5 billion.</p>

<p>In case you missed it - it says that <strong>25 - 19 = 5</strong>.</p>

<p><em>(note: this post makes no judgement of what amounts are needed, just pointing out the math, and yes - I realize it is probably a rounding error)</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.hvergi.net/2008/11/icelandic-government-math/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Generating legal subsets of a dependency graph</title>
		<link>http://www.hvergi.net/2008/10/generating-legal-subsets-of-a-dependency-graph/</link>
		<comments>http://www.hvergi.net/2008/10/generating-legal-subsets-of-a-dependency-graph/#comments</comments>
		<pubDate>Mon, 13 Oct 2008 20:32:32 +0000</pubDate>
		<dc:creator>Arnar</dc:creator>
		
		<category><![CDATA[Haskell]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Algorithms]]></category>

		<category><![CDATA[Computer Science]]></category>

		<category><![CDATA[Graphs]]></category>

		<guid isPermaLink="false">http://www.hvergi.net/?p=36</guid>
		<description><![CDATA[This post is literate Haskell

Update: Almost immediately after posting this, I started receiving helpful
comments that perhaps introducing multithreading is an overkill here. While
I do like the notion of asynchronous message passing via MVars, the comments
do have a strong point. Instead of changing the post, and thereby doing the
commenters a disservice by invalidating their comments, I [...]]]></description>
			<content:encoded><![CDATA[<p><em>This post is literate Haskell</em></p>

<p><strong>Update:</strong> Almost immediately after posting this, I started receiving helpful
comments that perhaps introducing multithreading is an overkill here. While
I do like the notion of asynchronous message passing via MVars, the comments
do have a strong point. Instead of changing the post, and thereby doing the
commenters a disservice by invalidating their comments, I just put <a href="http://gist.github.com/16690">a non-threaded
version of this on gist.github</a>. Indeed, it is
simpler than what is found on this page and even a little bit faster :)</p>

<p>I lurk on several mailing lists, one of them being the <a href="http://mochikit.com/">MochiKit</a> list.
One thing recently discussed there is a tool to let users of
MochiKit create their own customized version of it. This entails a GUI that
allows a user to select which module he or she needs, and the webapp takes
care of including all the dependencies as well.</p>

<p>This led me to a fun exercise. Say we have a <em><a href="http://en.wikipedia.org/wiki/Dependency_graph">dependency graph</a></em>, which by
nature must by a <a href="http://en.wikipedia.org/wiki/Directed_acyclic_graph">directed acyclic graph</a> (DAG), otherwise you could have
circular dependencies. Now, if you have <em>N</em> modules with no interdependencies,
then there are <em>2^N</em> possible ways of choosing a subset. However, if you
restrict yourself only to those subsets that <em>work</em>, i.e. those where all
dependencies of included modules are also included, then that puts a limit
on this number.</p>

<p>For example, if you have two modules <em>Base</em> and <em>Sub</em>, and the latter depends
on the former, then you would not allow the set containing just <em>Sub</em>. In
mathematical terms, you only allow <em>closed</em> subsets of modules, where closed
means:</p>

<p><em>A subset S of modules is closed if and only if the following holds:</em></p>

<ul>
<li><em>If M is a module in S, and M depends on module P, then P is also in S</em></li>
</ul>

<p>MochiKit contains around 15 modules and their dependency graph looks like this:</p>

<p><img src="http://www.hvergi.net/wp-content/uploads/2008/10/mkdeps.png" alt="MochiKit dependencies diagram" class="alignnone wp-image-37" /></p>

<p>I wanted to know if it was feasible to pre-generate all <em>legal</em> combinations
of modules, and the main factor of that is exactly how many are there. Clearly
there are fewer than the roughly 16.000 which there would be if there were no
dependencies. Generating all dependency-closed subsets seemed like a very
classical graph theoretical problem so someone must have written an algorithm
for it. I asked my friend and professor, <a href="http://www.ru.is/kennarar/mmh/">Magnús Már</a>, for some terms to put
into Google. Instead he gave me a hint on how to solve this.</p>

<p>The hint is this: To every closed subset of modules, there is a distinct
<em><a href="http://en.wikipedia.org/wiki/Antichain">anti-chain</a></em> of modules that represents that set. An anti-chain of modules is
a collection of modules such that no two depend on each other, neither directly
nor indirectly.</p>

<p>For example, say we want the modules <em>Logging</em>, <em>DOM</em>, <em>Style</em>, <em>Selector</em> and <em>Visual</em>. 
That means we have to take all the modules in the green area below. The anti-chain
for this set of modules are the blue modules.</p>

<p><img src="http://www.hvergi.net/wp-content/uploads/2008/10/mkantichain.png" alt="Antichain visualization" width="500" height="352" class="alignnone size-full wp-image-38" /></p>

<p>You can see how the set of the blue modules is the smallest set of modules needed to
<em>pull in</em> all the modules that we want.</p>

<p>Since there is a one-to-one correspondence between anti-chains and legal subsets,
then we need only find the anti-chains and we can generate the subsets.</p>

<p>First some preliminaries,</p>


<div class="wp_syntax"><div class="code"><pre class="haskell"><span style="color: #999;">&gt;</span> <span style="color: #06c; font-weight: bold;">module</span> Main <span style="color: #06c; font-weight: bold;">where</span>
<span style="color: #999;">&gt;</span> 
<span style="color: #999;">&gt;</span> <span style="color: #06c; font-weight: bold;">import</span> Control<span style="color: #999;">.</span>Concurrent <span style="color: #777;">&#40;</span>forkIO<span style="color: #777;">&#41;</span>
<span style="color: #999;">&gt;</span> <span style="color: #06c; font-weight: bold;">import</span> Control<span style="color: #999;">.</span>Concurrent<span style="color: #999;">.</span>MVar
<span style="color: #999;">&gt;</span> <span style="color: #06c; font-weight: bold;">import</span> Data<span style="color: #999;">.</span><span style="color: #295585;">Maybe</span> <span style="color: #777;">&#40;</span>fromJust<span style="color: #777;">&#41;</span>
<span style="color: #999;">&gt;</span> <span style="color: #06c; font-weight: bold;">import</span> Data<span style="color: #999;">.</span>List <span style="color: #777;">&#40;</span>nub, intersect<span style="color: #777;">&#41;</span></pre></div></div>


<p>We then set up our data as a simple list of pairs, where the first element
is the module name and the second is a list of its dependencies. This list
must be ordered in <em><a href="http://en.wikipedia.org/wiki/Topological_order">topological order</a></em>, meaning that a module may not appear
before any of its dependencies. This is easy to do by hand by looking at the
above picture.</p>


<div class="wp_syntax"><div class="code"><pre class="haskell"><span style="color: #999;">&gt;</span> dependencies <span style="color: #999;">=</span> <span style="color: #777;">&#91;</span>
<span style="color: #999;">&gt;</span>  <span style="color: #777;">&#40;</span><span style="background-color: #e9e9e9;">&quot;Base&quot;</span>,<span style="color: #777;">&#91;</span><span style="color: #777;">&#93;</span><span style="color: #777;">&#41;</span>,
<span style="color: #999;">&gt;</span>  <span style="color: #777;">&#40;</span><span style="background-color: #e9e9e9;">&quot;DateTime&quot;</span>,<span style="color: #777;">&#91;</span><span style="background-color: #e9e9e9;">&quot;Base&quot;</span><span style="color: #777;">&#93;</span><span style="color: #777;">&#41;</span>,
<span style="color: #999;">&gt;</span>  <span style="color: #777;">&#40;</span><span style="background-color: #e9e9e9;">&quot;Format&quot;</span>,<span style="color: #777;">&#91;</span><span style="background-color: #e9e9e9;">&quot;Base&quot;</span><span style="color: #777;">&#93;</span><span style="color: #777;">&#41;</span>,
<span style="color: #999;">&gt;</span>  <span style="color: #777;">&#40;</span><span style="background-color: #e9e9e9;">&quot;Iter&quot;</span>,<span style="color: #777;">&#91;</span><span style="background-color: #e9e9e9;">&quot;Base&quot;</span><span style="color: #777;">&#93;</span><span style="color: #777;">&#41;</span>,
<span style="color: #999;">&gt;</span>  <span style="color: #777;">&#40;</span><span style="background-color: #e9e9e9;">&quot;Async&quot;</span>,<span style="color: #777;">&#91;</span><span style="background-color: #e9e9e9;">&quot;Base&quot;</span><span style="color: #777;">&#93;</span><span style="color: #777;">&#41;</span>,
<span style="color: #999;">&gt;</span>  <span style="color: #777;">&#40;</span><span style="background-color: #e9e9e9;">&quot;DOM&quot;</span>,<span style="color: #777;">&#91;</span><span style="background-color: #e9e9e9;">&quot;Base&quot;</span><span style="color: #777;">&#93;</span><span style="color: #777;">&#41;</span>,
<span style="color: #999;">&gt;</span>  <span style="color: #777;">&#40;</span><span style="background-color: #e9e9e9;">&quot;Style&quot;</span>,<span style="color: #777;">&#91;</span><span style="background-color: #e9e9e9;">&quot;Base&quot;</span><span style="color: #777;">&#93;</span><span style="color: #777;">&#41;</span>,
<span style="color: #999;">&gt;</span>  <span style="color: #777;">&#40;</span><span style="background-color: #e9e9e9;">&quot;Color&quot;</span>,<span style="color: #777;">&#91;</span><span style="background-color: #e9e9e9;">&quot;DOM&quot;</span>, <span style="background-color: #e9e9e9;">&quot;Style&quot;</span><span style="color: #777;">&#93;</span><span style="color: #777;">&#41;</span>,
<span style="color: #999;">&gt;</span>  <span style="color: #777;">&#40;</span><span style="background-color: #e9e9e9;">&quot;Logging&quot;</span>,<span style="color: #777;">&#91;</span><span style="background-color: #e9e9e9;">&quot;Base&quot;</span><span style="color: #777;">&#93;</span><span style="color: #777;">&#41;</span>,
<span style="color: #999;">&gt;</span>  <span style="color: #777;">&#40;</span><span style="background-color: #e9e9e9;">&quot;LoggingPane&quot;</span>,<span style="color: #777;">&#91;</span><span style="background-color: #e9e9e9;">&quot;Logging&quot;</span><span style="color: #777;">&#93;</span><span style="color: #777;">&#41;</span>,
<span style="color: #999;">&gt;</span>  <span style="color: #777;">&#40;</span><span style="background-color: #e9e9e9;">&quot;Selector&quot;</span>,<span style="color: #777;">&#91;</span><span style="background-color: #e9e9e9;">&quot;DOM&quot;</span>, <span style="background-color: #e9e9e9;">&quot;Style&quot;</span><span style="color: #777;">&#93;</span><span style="color: #777;">&#41;</span>,
<span style="color: #999;">&gt;</span>  <span style="color: #777;">&#40;</span><span style="background-color: #e9e9e9;">&quot;Signal&quot;</span>,<span style="color: #777;">&#91;</span><span style="background-color: #e9e9e9;">&quot;DOM&quot;</span>, <span style="background-color: #e9e9e9;">&quot;Style&quot;</span><span style="color: #777;">&#93;</span><span style="color: #777;">&#41;</span>,
<span style="color: #999;">&gt;</span>  <span style="color: #777;">&#40;</span><span style="background-color: #e9e9e9;">&quot;Visual&quot;</span>,<span style="color: #777;">&#91;</span><span style="background-color: #e9e9e9;">&quot;Color&quot;</span><span style="color: #777;">&#93;</span><span style="color: #777;">&#41;</span>,
<span style="color: #999;">&gt;</span>  <span style="color: #777;">&#40;</span><span style="background-color: #e9e9e9;">&quot;DragAndDrop&quot;</span>,<span style="color: #777;">&#91;</span><span style="background-color: #e9e9e9;">&quot;Iter&quot;</span>,<span style="background-color: #e9e9e9;">&quot;Signal&quot;</span>,<span style="background-color: #e9e9e9;">&quot;Visual&quot;</span><span style="color: #777;">&#93;</span><span style="color: #777;">&#41;</span>,
<span style="color: #999;">&gt;</span>  <span style="color: #777;">&#40;</span><span style="background-color: #e9e9e9;">&quot;Sortable&quot;</span>,<span style="color: #777;">&#91;</span><span style="background-color: #e9e9e9;">&quot;DragAndDrop&quot;</span><span style="color: #777;">&#93;</span><span style="color: #777;">&#41;</span>
<span style="color: #999;">&gt;</span>  <span style="color: #777;">&#93;</span>
<span style="color: #999;">&gt;</span> 
<span style="color: #999;">&gt;</span> <span style="color: #70a321; font-style: italic;">-- For convenience, derive the list of module names </span>
<span style="color: #999;">&gt;</span> modules <span style="color: #999;">=</span> <span style="font-weight: bold;">map</span> <span style="font-weight: bold;">fst</span> dependencies</pre></div></div>


<p>The first thing we need to do is to generate all anti-chains. We will do this
in a dedicated thread and feed the anti-chains as we find them, one by one,
on an <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/Control-Concurrent-MVar.html">MVar</a>. Note that the <code>MVar</code> holds a <code>Maybe</code> value, this is so that we can
indicate the end by sending <code>Nothing</code>.</p>

<p>The algorithm is simple backtracking through recursion, 
we start with the empty set which is always
an anti-chain. At each step, we do the following.</p>

<ol>
<li><p>We check if the candidate element is dependent on any element in the anti-chain
already. If it is not, we can add it and recurse.</p></li>
<li><p>Since the anti-chain is still valid without the element, we also recurse
on the unchanged anti-chain but discard the candidate element.</p></li>
</ol>

<p>The recursion bottoms up when there are no more candidate elements to consider.
Note that the topological order of 
candidates is crucial here.</p>

<p>The helper function <code>accepts</code> determines if a candidate is allowed in a chain.
This is simply done by <em>closing</em> the candidate (see later) and seeing if there
is an overlap with the current anti-chain.</p>


<div class="wp_syntax"><div class="code"><pre class="haskell"><span style="color: #999;">&gt;</span> antichains <span style="color: #999;">::</span> MVar <span style="color: #777;">&#40;</span><span style="color: #295585;">Maybe</span> <span style="color: #777;">&#91;</span><span style="color: #295585;">String</span><span style="color: #777;">&#93;</span><span style="color: #777;">&#41;</span> <span style="color: #999;">-&gt;</span> <span style="color: #295585;">IO</span> <span style="color: #777;">&#40;</span><span style="color: #777;">&#41;</span>
<span style="color: #999;">&gt;</span> antichains channel <span style="color: #999;">=</span> antichains' <span style="color: #777;">&#91;</span><span style="color: #777;">&#93;</span> modules <span style="color: #999;">&gt;&gt;</span> putMVar channel Nothing
<span style="color: #999;">&gt;</span>     <span style="color: #06c; font-weight: bold;">where</span>
<span style="color: #999;">&gt;</span>       antichains' <span style="color: #999;">::</span> <span style="color: #777;">&#91;</span><span style="color: #295585;">String</span><span style="color: #777;">&#93;</span> <span style="color: #999;">-&gt;</span> <span style="color: #777;">&#91;</span><span style="color: #295585;">String</span><span style="color: #777;">&#93;</span> <span style="color: #999;">-&gt;</span> <span style="color: #295585;">IO</span> <span style="color: #777;">&#40;</span><span style="color: #777;">&#41;</span>
<span style="color: #999;">&gt;</span>       antichains' ac <span style="color: #777;">&#91;</span><span style="color: #777;">&#93;</span> <span style="color: #999;">=</span> putMVar channel <span style="color: #777;">&#40;</span>Just ac<span style="color: #777;">&#41;</span> <span style="color: #999;">&gt;&gt;</span> <span style="font-weight: bold;">return</span> <span style="color: #777;">&#40;</span><span style="color: #777;">&#41;</span>
<span style="color: #999;">&gt;</span>       antichains' ac <span style="color: #777;">&#40;</span>candidate:rest<span style="color: #777;">&#41;</span> <span style="color: #999;">=</span>
<span style="color: #999;">&gt;</span>           <span style="color: #06c; font-weight: bold;">do</span> <span style="color: #06c; font-weight: bold;">if</span> ac `accepts` candidate
<span style="color: #999;">&gt;</span>                 <span style="color: #06c; font-weight: bold;">then</span> antichains' <span style="color: #777;">&#40;</span>candidate:ac<span style="color: #777;">&#41;</span> rest
<span style="color: #999;">&gt;</span>                 <span style="color: #06c; font-weight: bold;">else</span> <span style="font-weight: bold;">return</span> <span style="color: #777;">&#40;</span><span style="color: #777;">&#41;</span>
<span style="color: #999;">&gt;</span>              antichains' ac rest
<span style="color: #999;">&gt;</span>       chain `accepts` candidate <span style="color: #999;">=</span> <span style="font-weight: bold;">null</span> <span style="color: #999;">$</span> intersect <span style="color: #777;">&#40;</span>closure <span style="color: #777;">&#91;</span>candidate<span style="color: #777;">&#93;</span><span style="color: #777;">&#41;</span> chain</pre></div></div>


<p>Above we mentioned <em>closing</em> a module, or more precisely a set of modules. This
means to take a set of modules and adding all modules needed to make it closed
with respect to dependencies. This is used both in the <code>accepts</code> helper above
but also to map the anti-chains to what we ultimately want, the closed sets.</p>


<div class="wp_syntax"><div class="code"><pre class="haskell"><span style="color: #999;">&gt;</span> closure <span style="color: #999;">::</span> <span style="color: #777;">&#91;</span><span style="color: #295585;">String</span><span style="color: #777;">&#93;</span> <span style="color: #999;">-&gt;</span> <span style="color: #777;">&#91;</span><span style="color: #295585;">String</span><span style="color: #777;">&#93;</span>
<span style="color: #999;">&gt;</span> closure ms <span style="color: #999;">=</span> 
<span style="color: #999;">&gt;</span>     <span style="color: #06c; font-weight: bold;">let</span> missing <span style="color: #999;">=</span> nub <span style="color: #999;">$</span> <span style="font-weight: bold;">filter</span> <span style="color: #777;">&#40;</span><span style="font-weight: bold;">not</span> <span style="color: #999;">.</span> <span style="font-weight: bold;">flip</span> <span style="font-weight: bold;">elem</span> ms<span style="color: #777;">&#41;</span> 
<span style="color: #999;">&gt;</span>                   <span style="color: #999;">$</span> <span style="font-weight: bold;">concatMap</span> <span style="color: #777;">&#40;</span>fromJust <span style="color: #999;">.</span> <span style="font-weight: bold;">flip</span> <span style="font-weight: bold;">lookup</span> dependencies<span style="color: #777;">&#41;</span> ms
<span style="color: #999;">&gt;</span>     <span style="color: #06c; font-weight: bold;">in</span>
<span style="color: #999;">&gt;</span>       <span style="color: #06c; font-weight: bold;">if</span> <span style="font-weight: bold;">null</span> missing
<span style="color: #999;">&gt;</span>          <span style="color: #06c; font-weight: bold;">then</span> ms
<span style="color: #999;">&gt;</span>          <span style="color: #06c; font-weight: bold;">else</span> closure <span style="color: #777;">&#40;</span>ms <span style="color: #999;">++</span> missing<span style="color: #777;">&#41;</span></pre></div></div>


<p>The anti-chain generator above feeds the anti-chains to an <code>MVar</code>. The following
IO action is run in a second thread and it eats off the anti-chains from the
<code>MVar</code>, closes them up and prints. We use a second <code>MVar</code> to notify the main
thread when we are done.</p>


<div class="wp_syntax"><div class="code"><pre class="haskell"><span style="color: #999;">&gt;</span> printer <span style="color: #999;">::</span> MVar <span style="color: #777;">&#40;</span><span style="color: #777;">&#41;</span> <span style="color: #999;">-&gt;</span> MVar <span style="color: #777;">&#40;</span><span style="color: #295585;">Maybe</span> <span style="color: #777;">&#91;</span><span style="color: #295585;">String</span><span style="color: #777;">&#93;</span><span style="color: #777;">&#41;</span> <span style="color: #999;">-&gt;</span> <span style="color: #295585;">IO</span> <span style="color: #777;">&#40;</span><span style="color: #777;">&#41;</span>
<span style="color: #999;">&gt;</span> printer stop in<span style="color: #999;">_</span> <span style="color: #999;">=</span> 
<span style="color: #999;">&gt;</span>     <span style="color: #06c; font-weight: bold;">do</span> v <span style="color: #999;">&lt;-</span> takeMVar in<span style="color: #999;">_</span>
<span style="color: #999;">&gt;</span>        <span style="color: #06c; font-weight: bold;">case</span> v <span style="color: #06c; font-weight: bold;">of</span>
<span style="color: #999;">&gt;</span>          Just xs <span style="color: #999;">-&gt;</span> <span style="font-weight: bold;">putStrLn</span> <span style="color: #999;">$</span> <span style="font-weight: bold;">show</span> <span style="color: #999;">$</span> closure xs
<span style="color: #999;">&gt;</span>          Nothing <span style="color: #999;">-&gt;</span> putMVar stop <span style="color: #777;">&#40;</span><span style="color: #777;">&#41;</span>
<span style="color: #999;">&gt;</span>        printer stop in<span style="color: #999;">_</span></pre></div></div>


<p>Finally, to tie it all together, the main action creates the <code>MVars</code> for
communication and sparks separate threads for the anti-chain generation and
for the printing.</p>


<div class="wp_syntax"><div class="code"><pre class="haskell"><span style="color: #999;">&gt;</span> main <span style="color: #999;">::</span> <span style="color: #295585;">IO</span> <span style="color: #777;">&#40;</span><span style="color: #777;">&#41;</span>
<span style="color: #999;">&gt;</span> main <span style="color: #999;">=</span> <span style="color: #06c; font-weight: bold;">do</span> chan <span style="color: #999;">&lt;-</span> newEmptyMVar
<span style="color: #999;">&gt;</span>           stop <span style="color: #999;">&lt;-</span> newEmptyMVar
<span style="color: #999;">&gt;</span>           forkIO <span style="color: #999;">$</span> printer stop chan
<span style="color: #999;">&gt;</span>           forkIO <span style="color: #999;">$</span> antichains chan
<span style="color: #999;">&gt;</span>           takeMVar stop
<span style="color: #999;">&gt;</span>           <span style="font-weight: bold;">return</span> <span style="color: #777;">&#40;</span><span style="color: #777;">&#41;</span></pre></div></div>


<p>That&#8217;s it! The result: For the dependency graph of MochiKit above, there are
<em>only</em> 817 legal combinations of modules. Actually - 816 if you discount the
empty one :)</p>

<p>I&#8217;ll leave it up to someone else to answer the original question about feasibility
of generating and storing that number of files.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hvergi.net/2008/10/generating-legal-subsets-of-a-dependency-graph/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Two handy Ubiquity commands</title>
		<link>http://www.hvergi.net/2008/10/two-handy-ubiquity-commands/</link>
		<comments>http://www.hvergi.net/2008/10/two-handy-ubiquity-commands/#comments</comments>
		<pubDate>Wed, 08 Oct 2008 12:46:38 +0000</pubDate>
		<dc:creator>Arnar</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.hvergi.net/?p=35</guid>
		<description><![CDATA[This blog has been very quiet, I guess our contributors are pretty busy with school work. Or at least they were. :) Since I&#8217;m pretty busy with mine, here are two ultra short tips.

Ubiquity is an ultra-handy (and geek friendly) tool for Firefox that allows for text commands. Today I created two Ubiquity commands that [...]]]></description>
			<content:encoded><![CDATA[<p>This blog has been very quiet, I guess our contributors are pretty busy with school work. Or at least they <a href="http://twitter.com/gudmundur/statuses/935676856">were</a>. :) Since I&#8217;m pretty busy with mine, here are two ultra short tips.</p>

<p><a href="http://labs.mozilla.com/2008/08/introducing-ubiquity/">Ubiquity</a> is an ultra-handy (and geek friendly) tool for Firefox that allows for text commands. Today I created two Ubiquity commands that I find rather handy - especially for redditors :)</p>

<h3>Automatic Markdown links</h3>

<p>I write this blog in the <a href="http://daringfireball.net/projects/markdown/">Markdown</a> markup format, which is the same format that is used for <a href="http://www.reddit.com/">reddit</a> comments (which I have been known to make occasionally). When writing blog posts, I often find that I mention a word or a phrase that is naturally turned into a link. This means I have to open a new tab, Google the word or phrase - open the relevant page and copy the URL into my post or comment, making sure it is in the correct Markdown format for links. Now, Ubiquity is very apt at solving these problems, so I wrote a command that allows me to do one of two things:</p>

<ol>
<li><p>I write a phrase, say &#8220;Reykjavik University&#8221;. I then select that phrase, invoke ubiquity (in my case it is Option+Space) and type &#8220;mdlink&#8221; and hit enter. This performs a Google search on the phrase, picks the first result and uses its URL to make the phrase a proper Markdown link, replacing the selected text:  <code>[Reykjavik University](http://www.ru.is/)</code></p></li>
<li><p>Instead of typing the phrase and selecting it, I can also just invoke Ubuiquity and type &#8220;mdlink Reykjavik University&#8221; directly in the prompt - with the same result.</p></li>
</ol>

<p>The command can be found and installed from <a href="http://gist.github.com/">Gist.github</a>. Notice the (very cool) feature of Gist that it recognizes Ubiquity commands and inserts the appropriate markup to make FF offer you to install it.</p>

<p><strong><a href="http://gist.github.com/15512">Follow this link to see and/or install the &#8220;Markdown link&#8221; command</a></strong></p>

<h3>jQuery injection</h3>

<p>When browsing, I often find the need to do it programmatically, executing Javascript in the context of a 3rd party webpage. For this it is very handy to have something like <a href="http://jquery.com/">jQuery</a> on hand and thus I&#8217;ve used a <a href="http://www.learningjquery.com/2006/12/jquerify-bookmarklet">bookmarklet</a> to inject jQuery into any web-page. That makes it very easy to do fancy stuff (like <a href="http://www.reddit.com/r/WTF/comments/75vh6/i_guess_he_doesnt_like_black_people/c05rdzm">down-voting every comment by a reddit user</a>) through the <a href="http://getfirebug.com/">Firebug</a> JS console.</p>

<p>I converted the bookmarklet linked above into a simple Ubiquity command, allowing for quicker access to the &#8220;Inject jQuery&#8221; operation. You can get the bookmarklet on its <a href="http://gist.github.com/">gist.github</a> entry:</p>

<p><strong><a href="http://gist.github.com/15506">Follow this link to see and/or install the &#8220;Inject jQuery&#8221; command</a></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.hvergi.net/2008/10/two-handy-ubiquity-commands/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Parsing annotated postfix operators with Haskell</title>
		<link>http://www.hvergi.net/2008/07/parsing-annotated-postfix-operators-with-haskell/</link>
		<comments>http://www.hvergi.net/2008/07/parsing-annotated-postfix-operators-with-haskell/#comments</comments>
		<pubDate>Wed, 23 Jul 2008 23:08:36 +0000</pubDate>
		<dc:creator>Arnar</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Haskell]]></category>

		<category><![CDATA[Parsing]]></category>

		<guid isPermaLink="false">http://www.hvergi.net/?p=30</guid>
		<description><![CDATA[I want to show you a neat trick method when constructing parsers with Haskell and Parsec.

While writing a parser for CCS, a process description language (don&#8217;t worry too much about it), I found I needed to parse constructs with a suffix operator. To provide a little context, the relevant grammar is this.

P ::= 0 &#124; [...]]]></description>
			<content:encoded><![CDATA[<p>I want to show you a neat <del>trick</del> method when constructing parsers with <a href="http://www.haskell.org/">Haskell</a> and <a href="http://www.haskell.org/haskellwiki/Parsec">Parsec</a>.</p>

<p>While writing a parser for <a href="http://en.wikipedia.org/wiki/Calculus_of_Communicating_Systems">CCS</a>, a process description language (don&#8217;t worry too much about it), I found I needed to parse constructs with a suffix operator. To provide a little context, the relevant grammar is this.</p>

<p><code>P ::= 0 | a.P | ... | P[f] | P \ S</code></p>

<p>where &#8230; indicates I&#8217;ve omitted some irrelevant stuff. Here, <code>P</code> is a <em>process</em> - the entity CCS is meant to describe. <code>a</code> is a so-called <em>action label</em>, just think of it as a generic label. <code>a.P</code> stands for a process that can <em>perform an a action</em> and then become the process specified after the dot. <code>0</code> (zero) stands for a special process that cannot do anything, <em>the deadlocked process</em>.</p>

<p><code>f</code> is a mapping from such labels to labels, called a <em>relabelling</em> and is commonly specified with a list of pairs <code>b/a,d/c,...</code> which indicates that <code>a</code> maps to <code>b</code>, <code>c</code> to <code>d</code> etc. <code>S</code> is a set of such labels, represented in the normal manner with curly braces <code>{a,b,c,...}</code>. The construct <code>P \ S</code> is called a <em>restriction</em>, informally it means <code>P</code> cannot perform any of the actions in <code>S</code>.</p>

<p>In summary,</p>

<ul>
<li><code>a.a.0</code> is a process that can <em>do</em> two <code>a</code> actions and then stop</li>
<li><code>P [b/a]</code> is whatever <code>P</code> is, except any <code>a</code> performed by <code>P</code> will be changed to a <code>b</code></li>
<li><code>P \ {a,b}</code> is whatever <code>P</code> is, except <code>a</code> or <code>b</code> actions are not allowed</li>
</ul>

<p>Now, <code>[f]</code> and <code>\ S</code> can be considered <em>postfix unary</em> operators on processes, with some additional structure, the <code>f</code> and the <code>S</code>. Since they are postfix there is no need to define their relative precedence, their order is enough. I.e.</p>

<p><code>a.0 [b/a] \ {b} [d/c,e/d] \ {e,g}</code></p>

<p>is unambiguous. Our Haskell ADT to represent the abstract syntax tree looks like this,</p>


<div class="wp_syntax"><div class="code"><pre class="haskell"><span style="color: #06c; font-weight: bold;">type</span> Action <span style="color: #999;">=</span> <span style="color: #295585;">String</span>
<span style="color: #06c; font-weight: bold;">type</span> Relabeling <span style="color: #999;">=</span> Map<span style="color: #999;">.</span>Map Action Action
<span style="color: #06c; font-weight: bold;">type</span> Restriction <span style="color: #999;">=</span> Set<span style="color: #999;">.</span>Set Action
&nbsp;
<span style="color: #06c; font-weight: bold;">data</span> Process
    <span style="color: #999;">=</span> Null
    <span style="color: #999;">|</span> Prefix Action Process
    <span style="color: #999;">|</span> <span style="color: #999;">...</span>
    <span style="color: #999;">|</span> Relabel Process Relabeling
    <span style="color: #999;">|</span> Restrict Process Restriction
    <span style="color: #06c; font-weight: bold;">deriving</span> <span style="color: #777;">&#40;</span><span style="color: #295585;">Eq</span>, <span style="color: #295585;">Show</span>, <span style="color: #295585;">Ord</span><span style="color: #777;">&#41;</span></pre></div></div>


<p>We want the example above to be parsed into this value (indented for clarity),</p>


<div class="wp_syntax"><div class="code"><pre class="haskell">Restrict <span style="color: #777;">&#40;</span>
    Relabel <span style="color: #777;">&#40;</span>
        Restrict <span style="color: #777;">&#40;</span>
            Relabel <span style="color: #777;">&#40;</span>
                Prefix <span style="background-color: #e9e9e9;">&quot;a&quot;</span> Null
            <span style="color: #777;">&#41;</span> <span style="color: #777;">&#40;</span>Map<span style="color: #999;">.</span>fromList <span style="color: #777;">&#91;</span><span style="color: #777;">&#40;</span><span style="background-color: #e9e9e9;">&quot;a&quot;</span>,<span style="background-color: #e9e9e9;">&quot;b&quot;</span><span style="color: #777;">&#41;</span><span style="color: #777;">&#93;</span><span style="color: #777;">&#41;</span>
        <span style="color: #777;">&#41;</span> <span style="color: #777;">&#40;</span>Set<span style="color: #999;">.</span>fromList <span style="color: #777;">&#91;</span><span style="background-color: #e9e9e9;">&quot;b&quot;</span><span style="color: #777;">&#93;</span><span style="color: #777;">&#41;</span>
    <span style="color: #777;">&#41;</span> <span style="color: #777;">&#40;</span>Map<span style="color: #999;">.</span>fromList <span style="color: #777;">&#91;</span><span style="color: #777;">&#40;</span><span style="background-color: #e9e9e9;">&quot;c&quot;</span>,<span style="background-color: #e9e9e9;">&quot;d&quot;</span><span style="color: #777;">&#41;</span>,<span style="color: #777;">&#40;</span><span style="background-color: #e9e9e9;">&quot;d&quot;</span>,<span style="background-color: #e9e9e9;">&quot;e&quot;</span><span style="color: #777;">&#41;</span><span style="color: #777;">&#93;</span>
<span style="color: #777;">&#41;</span> <span style="color: #777;">&#40;</span>Set<span style="color: #999;">.</span>fromList <span style="color: #777;">&#91;</span><span style="background-color: #e9e9e9;">&quot;e&quot;</span>, <span style="background-color: #e9e9e9;">&quot;g&quot;</span><span style="color: #777;">&#93;</span><span style="color: #777;">&#41;</span></pre></div></div>


<p>My initial attempt at writing a parser for these constructs was ugly and complex. I was trying in one rule to parse a bunch of relabellings and restrictions and construct the appropriate value from the process that had already been parsed, cluttering the rules for other constructs with this logic. I thus went to the best documentation of Haskell, the <code>#haskell</code>irc channel, and asked for opinions. <a href="http://cpoucet.wordpress.com/">Christophe Poucet</a> (vincenz) showed me the light and pointed out that a parser could well return a function,</p>

<p><pre>
April 13th, 2008
15:24 &lt; vincenz> Arnar: what about...
15:24 &lt; vincenz> process_adapter :== relabeling | restriction
15:24 &lt; vincenz> and have those return the type
15:24 &lt; vincenz> Process -> Process
</pre></p>

<p>And therein lies the <em>trick</em>. Instead of constructing the correct value, I simply parse a sequence of <em>adapters</em>, each of which can be a relabeling or a restriction, in each step returning the function that wraps a process in the relevant constructors.</p>


<div class="wp_syntax"><div class="code"><pre class="haskell">relabeling <span style="color: #999;">::</span> Parser Relabeling
relabeling <span style="color: #999;">=</span> <span style="color: #06c; font-weight: bold;">do</span> rs <span style="color: #999;">&lt;-</span> brackets <span style="color: #999;">$</span> commaSep1 rename
                <span style="font-weight: bold;">return</span> <span style="color: #999;">$</span> Map<span style="color: #999;">.</span>fromList rs
                <span style="color: #06c; font-weight: bold;">where</span>
                  rename <span style="color: #999;">::</span> Parser <span style="color: #777;">&#40;</span>Action,Action<span style="color: #777;">&#41;</span>
                  rename <span style="color: #999;">=</span> <span style="color: #06c; font-weight: bold;">do</span> s1 <span style="color: #999;">&lt;-</span> lexeme action
                              symbol <span style="background-color: #e9e9e9;">&quot;/&quot;</span>
                              s2 <span style="color: #999;">&lt;-</span> lexeme action
                              <span style="font-weight: bold;">return</span> <span style="color: #777;">&#40;</span>s2,s1<span style="color: #777;">&#41;</span>
&nbsp;
restriction <span style="color: #999;">::</span> Parser Restriction
restriction <span style="color: #999;">=</span> <span style="color: #06c; font-weight: bold;">do</span> symbol <span style="background-color: #e9e9e9;">&quot;<span style="">\\</span>&quot;</span>
                 ss <span style="color: #999;">&lt;-</span> braces <span style="color: #999;">$</span> commaSep1 action
                 <span style="font-weight: bold;">return</span> <span style="color: #999;">$</span> Set<span style="color: #999;">.</span>fromList ss
&nbsp;
process<span style="color: #999;">_</span>adapter <span style="color: #999;">::</span> Parser <span style="color: #777;">&#40;</span>Process <span style="color: #999;">-&gt;</span> Process<span style="color: #777;">&#41;</span>
process<span style="color: #999;">_</span>adapter <span style="color: #999;">=</span> <span style="color: #06c; font-weight: bold;">do</span> restr <span style="color: #999;">&lt;-</span> restriction
                     <span style="font-weight: bold;">return</span> <span style="color: #777;">&#40;</span>\p <span style="color: #999;">-&gt;</span> Restrict p restr<span style="color: #777;">&#41;</span>
                  <span style="color: #999;">&lt;|&gt;</span>
                  <span style="color: #06c; font-weight: bold;">do</span> rel <span style="color: #999;">&lt;-</span> relabeling
                     <span style="font-weight: bold;">return</span> <span style="color: #777;">&#40;</span>\p <span style="color: #999;">-&gt;</span> Relabel p rel<span style="color: #777;">&#41;</span></pre></div></div>


<p>Very simple, very clean. Now, parsing a process followed by a sequence of such <em>adapters</em> is trivial. We then <em>feed</em> the process through the adapter functions in order, i.e.</p>


<div class="wp_syntax"><div class="code"><pre class="haskell">adaptedProcess <span style="color: #999;">::</span> Parser Process
adaptedProcess <span style="color: #999;">=</span> <span style="color: #06c; font-weight: bold;">do</span> p <span style="color: #999;">&lt;-</span> simpleProcess
                    adapters <span style="color: #999;">&lt;-</span> many process<span style="color: #999;">_</span>adapter
                    <span style="font-weight: bold;">return</span> <span style="color: #999;">$</span> <span style="font-weight: bold;">foldr</span> <span style="color: #777;">&#40;</span><span style="font-weight: bold;">flip</span> <span style="color: #777;">&#40;</span><span style="color: #999;">.</span><span style="color: #777;">&#41;</span><span style="color: #777;">&#41;</span> <span style="font-weight: bold;">id</span> adapters p</pre></div></div>


<p>I especially like that last line, <code>foldr (flip (.)) id adapters p</code>. Remember <code>adapters</code> has the type <code>[Process -&gt; Process]</code>, i.e. a list of adapters. If this list is <code>[f1,f2,...,fN]</code>, then</p>

<p><code>foldr (flip (.)) id adapters</code></p>

<p>will give us the composition of these in the correct order, namely</p>

<p><code>\x -&gt; fN ( ... (f2 (f1 (id x))) ...)</code></p>

<p>All that&#8217;s left to do is to apply this on the original process and out pops a correctly wrapped one.</p>

<p>I think this is very neat and it showcases the power of first-class functions. If you don&#8217;t see the beauty here, feel absolutely free to write me off as eccentric ;o)</p>

<p>You can find the complete code and a PDF writeup of the CCS project <a href="http://www.hvergi.net/arnar/code/haskell-ccs/">right here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hvergi.net/2008/07/parsing-annotated-postfix-operators-with-haskell/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Playing with Haskell&#8217;s lazy lists</title>
		<link>http://www.hvergi.net/2008/07/playing-with-haskells-lazy-lists/</link>
		<comments>http://www.hvergi.net/2008/07/playing-with-haskells-lazy-lists/#comments</comments>
		<pubDate>Mon, 21 Jul 2008 17:43:40 +0000</pubDate>
		<dc:creator>Arnar</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Haskell]]></category>

		<category><![CDATA[Math]]></category>

		<guid isPermaLink="false">http://www.hvergi.net/?p=29</guid>
		<description><![CDATA[I love Haskell. I love many things about it, but one of its main attractions for me is that it is lazy. I guess I like that because I&#8217;m lazy myself.

Lazyness in Haskell means that we get infinite lists (or streams) for free - i.e. they don&#8217;t need any special handling over finite lists. Granted, [...]]]></description>
			<content:encoded><![CDATA[<p>I love <a href="http://www.haskell.org/">Haskell</a>. I love many things about it, but one of its main attractions for me is that it is <em>lazy</em>. I guess I like that because I&#8217;m lazy myself.</p>

<p>Lazyness in Haskell means that we get infinite lists (or <em>streams</em>) for free - i.e. they don&#8217;t need any special handling over finite lists. Granted, <a href="http://www.python.org/dev/peps/pep-0255/">other</a> <a href="http://www.gnu.org/software/mit-scheme/documentation/mit-scheme-ref/Streams.html">languages</a> have infinite lists, but in Haskell any regular list has the natural potential of being infinite so we don&#8217;t need any specific syntax or constructors for them.</p>

<p>So, when I read about a Rutgers graduate student named <a href="http://www.math.rutgers.edu/~erowland/">Eric Rowland</a> that recently <a href="http://recursed.blogspot.com/2008/07/rutgers-graduate-student-finds-new.html">discovered a simple prime-number generating formula</a>, I decided to try it out in Haskell. Turns out it is extremely simple.</p>

<p>The generating formula is based on the sequence <em>a(n)</em> that you get by setting <em>a</em>(1) = 7 and then for <em>n</em> ≥ 2:</p>

<p><em>a</em>(<em>n</em>) = <em>a</em>(<em>n</em>-1) + gcd(<em>n</em>,<em>a</em>(<em>n</em>-1))</p>

<p>In Haskell, running in interactive mode (GHCi), we define this by</p>


<div class="wp_syntax"><div class="code"><pre class="haskell">Prelude<span style="color: #999;">&gt;</span> <span style="color: #06c; font-weight: bold;">let</span> a' <span style="color: #999;">=</span> <span style="color: #777;">&#40;</span><span style="color: red;">1</span>,<span style="color: red;">7</span><span style="color: #777;">&#41;</span> : <span style="color: #777;">&#40;</span><span style="font-weight: bold;">map</span> <span style="color: #777;">&#40;</span> \<span style="color: #777;">&#40;</span>n,x<span style="color: #777;">&#41;</span> <span style="color: #999;">-&gt;</span> <span style="color: #777;">&#40;</span>n<span style="color: red;">+1</span>, x <span style="color: #999;">+</span> <span style="color: #777;">&#40;</span><span style="font-weight: bold;">gcd</span> <span style="color: #777;">&#40;</span>n<span style="color: red;">+1</span><span style="color: #777;">&#41;</span> x<span style="color: #777;">&#41;</span><span style="color: #777;">&#41;</span><span style="color: #777;">&#41;</span> a'<span style="color: #777;">&#41;</span>
Prelude<span style="color: #999;">&gt;</span> <span style="font-weight: bold;">take</span> <span style="color: red;">10</span> a'
<span style="color: #777;">&#91;</span><span style="color: #777;">&#40;</span><span style="color: red;">1</span>,<span style="color: red;">7</span><span style="color: #777;">&#41;</span>,<span style="color: #777;">&#40;</span><span style="color: red;">2</span>,<span style="color: red;">8</span><span style="color: #777;">&#41;</span>,<span style="color: #777;">&#40;</span><span style="color: red;">3</span>,<span style="color: red;">9</span><span style="color: #777;">&#41;</span>,<span style="color: #777;">&#40;</span><span style="color: red;">4</span>,<span style="color: red;">10</span><span style="color: #777;">&#41;</span>,<span style="color: #777;">&#40;</span><span style="color: red;">5</span>,<span style="color: red;">15</span><span style="color: #777;">&#41;</span>,<span style="color: #777;">&#40;</span><span style="color: red;">6</span>,<span style="color: red;">18</span><span style="color: #777;">&#41;</span>,<span style="color: #777;">&#40;</span><span style="color: red;">7</span>,<span style="color: red;">19</span><span style="color: #777;">&#41;</span>,<span style="color: #777;">&#40;</span><span style="color: red;">8</span>,<span style="color: red;">20</span><span style="color: #777;">&#41;</span>,<span style="color: #777;">&#40;</span><span style="color: red;">9</span>,<span style="color: red;">21</span><span style="color: #777;">&#41;</span>,<span style="color: #777;">&#40;</span><span style="color: red;">10</span>,<span style="color: red;">22</span><span style="color: #777;">&#41;</span><span style="color: #777;">&#93;</span>
Prelude<span style="color: #999;">&gt;</span> <span style="color: #06c; font-weight: bold;">let</span> a <span style="color: #999;">=</span> <span style="font-weight: bold;">map</span> <span style="font-weight: bold;">snd</span> a'
Prelude<span style="color: #999;">&gt;</span> <span style="font-weight: bold;">take</span> <span style="color: red;">23</span> a
<span style="color: #777;">&#91;</span><span style="color: red;">7</span>,<span style="color: red;">8</span>,<span style="color: red;">9</span>,<span style="color: red;">10</span>,<span style="color: red;">15</span>,<span style="color: red;">18</span>,<span style="color: red;">19</span>,<span style="color: red;">20</span>,<span style="color: red;">21</span>,<span style="color: red;">22</span>,<span style="color: red;">33</span>,<span style="color: red;">36</span>,<span style="color: red;">37</span>,<span style="color: red;">38</span>,<span style="color: red;">39</span>,<span style="color: red;">40</span>,<span style="color: red;">41</span>,<span style="color: red;">42</span>,<span style="color: red;">43</span>,<span style="color: red;">44</span>,<span style="color: red;">45</span>,<span style="color: red;">46</span>,<span style="color: red;">69</span><span style="color: #777;">&#93;</span></pre></div></div>


<p>Note that each element of a&#8217; is a tuple of the index <em>n</em> and the value <em>a(n)</em>. By taking differences between successive values of this sequence, you always get either the number 1 or a prime number (proving this is Rowland&#8217;s contribution).</p>


<div class="wp_syntax"><div class="code"><pre class="haskell">Prelude<span style="color: #999;">&gt;</span> <span style="color: #06c; font-weight: bold;">let</span> d <span style="color: #999;">=</span> <span style="font-weight: bold;">zipWith</span> <span style="color: #777;">&#40;</span><span style="color: #999;">-</span><span style="color: #777;">&#41;</span> <span style="color: #777;">&#40;</span><span style="font-weight: bold;">tail</span> a<span style="color: #777;">&#41;</span> a
Prelude<span style="color: #999;">&gt;</span> <span style="font-weight: bold;">take</span> <span style="color: red;">23</span> d
<span style="color: #777;">&#91;</span><span style="color: red;">1</span>,<span style="color: red;">1</span>,<span style="color: red;">1</span>,<span style="color: red;">5</span>,<span style="color: red;">3</span>,<span style="color: red;">1</span>,<span style="color: red;">1</span>,<span style="color: red;">1</span>,<span style="color: red;">1</span>,<span style="color: red;">11</span>,<span style="color: red;">3</span>,<span style="color: red;">1</span>,<span style="color: red;">1</span>,<span style="color: red;">1</span>,<span style="color: red;">1</span>,<span style="color: red;">1</span>,<span style="color: red;">1</span>,<span style="color: red;">1</span>,<span style="color: red;">1</span>,<span style="color: red;">1</span>,<span style="color: red;">1</span>,<span style="color: red;">23</span>,<span style="color: red;">3</span><span style="color: #777;">&#93;</span></pre></div></div>


<p>So far, so good. Now in one step we remove the 1-s by using <code>filter</code> and get rid of duplicates by using <code>nub</code> (which is defined in the <code>List</code> module).</p>


<div class="wp_syntax"><div class="code"><pre class="haskell">Prelude<span style="color: #999;">&gt;</span> :m <span style="color: #999;">+</span>List
<span style="color: #06c; font-weight: bold;">Prelude</span> List<span style="color: #999;">&gt;</span> <span style="color: #06c; font-weight: bold;">let</span> primes <span style="color: #999;">=</span> nub <span style="color: #999;">$</span> <span style="font-weight: bold;">filter</span> <span style="color: #777;">&#40;</span><span style="color: #999;">/=</span> <span style="color: red;">1</span><span style="color: #777;">&#41;</span> d
<span style="color: #06c; font-weight: bold;">Prelude</span> List<span style="color: #999;">&gt;</span> <span style="font-weight: bold;">take</span> <span style="color: red;">23</span> primes
<span style="color: #777;">&#91;</span><span style="color: red;">5</span>,<span style="color: red;">3</span>,<span style="color: red;">11</span>,<span style="color: red;">23</span>,<span style="color: red;">47</span>,<span style="color: red;">101</span>,<span style="color: red;">7</span>,<span style="color: red;">13</span>,<span style="color: red;">233</span>,<span style="color: red;">467</span>,<span style="color: red;">941</span>,<span style="color: red;">1889</span>,<span style="color: red;">3779</span>,<span style="color: red;">7559</span>,<span style="color: red;">15131</span>,<span style="color: red;">53</span>,<span style="color: red;">30323</span>,<span style="color: red;">60647</span>,<span style="color: red;">121403</span>,<span style="color: red;">242807</span>,<span style="color: red;">19</span>,<span style="color: red;">37</span>,<span style="color: red;">17</span><span style="color: #777;">&#93;</span></pre></div></div>


<p>Neat! The list <code>primes</code> is of course infinite and we could pluck the values off it as long as we like, and Haskell&#8217;s laziness will make sure values are only computed on demand. <a href="http://www.cs.uwaterloo.ca/journals/JIS/VOL11/Rowland/rowland21.html">Rowland&#8217;s paper</a> contains the details of the math.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hvergi.net/2008/07/playing-with-haskells-lazy-lists/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Adobe Lightroom 2 beta</title>
		<link>http://www.hvergi.net/2008/07/adobe-lightroom-2-beta/</link>
		<comments>http://www.hvergi.net/2008/07/adobe-lightroom-2-beta/#comments</comments>
		<pubDate>Wed, 09 Jul 2008 22:55:39 +0000</pubDate>
		<dc:creator>Arnar</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[Photography]]></category>

		<guid isPermaLink="false">http://www.hvergi.net/?p=26</guid>
		<description><![CDATA[I know this is not programming, but one has to go out once in a while. Of course, after going outside, once I&#8217;m home I&#8217;m right back on the computer (my wife will be happy to tell you this).

I went on the Golden circle excursion with the ICALP attendees today. This is the &#8220;standard&#8221; tour [...]]]></description>
			<content:encoded><![CDATA[<p>I know this is not programming, but one has to go out once in a while. Of course, after going outside, once I&#8217;m home I&#8217;m right back on the computer (my wife will be happy to tell you this).</p>

<p>I went on the <a href="http://en.wikipedia.org/wiki/Golden_Circle_(Iceland)"><em>Golden circle</em></a> excursion with the ICALP attendees today. This is the &#8220;standard&#8221; tour given to most visitors and involves visiting the area around <a href="http://en.wikipedia.org/wiki/Geysir">Geysir</a>, <a href="http://en.wikipedia.org/wiki/Gullfoss">Gullfoss</a> and <a href="http://en.wikipedia.org/wiki/%C3%9Eingvellir">Þingvellir</a>. After I got back I started selecting photos to put on the <a href="http://picasaweb.google.com/icalp2008">ICALP photo-webpage</a>. I&#8217;ve been trying out Adobe&#8217;s new beta of <a href="http://labs.adobe.com/technologies/lightroom/">Lightroom</a> and I must say I like it. It has all the same features as the previous version, but feels considerably snappier.</p>

<p>One new feature is selective retouching, i.e. you paint a mask and can make some exposure/color changes to selected areas. Of course, performance goes out the window as soon as you do that but it is quite powerful and saves you from starting Photoshop.</p>

<p>While at Gullfoss, I took the following picture of Bláfell, a mountain north-east of there (if I&#8217;m incorrect and you know better, please correct me in the comments). The picture straight from the camera wasn&#8217;t that great&#8230;</p>

<p style="text-align: center;"><a href="http://www.hvergi.net/wp-content/uploads/2008/07/icalp2009-09-001.jpg"><img class="alignnone size-full wp-image-27 aligncenter" title="Bláfell, unedited" src="http://www.hvergi.net/wp-content/uploads/2008/07/icalp2009-09-001.jpg" alt="" width="500" height="333" /></a></p>

<p style="text-align: left;">&#8230; so I played with it for about a minute in Lightroom&#8230;</p>

<p style="text-align: center;"><a href="http://www.hvergi.net/wp-content/uploads/2008/07/icalp2009-09-001-2.jpg"><img class="alignnone size-full wp-image-28 aligncenter" title="Bláfell, edited" src="http://www.hvergi.net/wp-content/uploads/2008/07/icalp2009-09-001-2.jpg" alt="Edited version" width="500" height="333" /></a></p>

<p style="text-align: left;">While it is still not great, I think it is quite good less than 60 seconds of work. The correctios I made were exposure correction, removing the dirt from the lens with the spot tool and then I made the mountain and sky a little more pronounced by upping the saturation and using the <em>clarity</em> slider (which I think is just the <a href="http://nyfalls.com/article-photoshop-high-pass-sharpening.html">high-pass sharpening technique</a>).</p>

<p style="text-align: left;">So.. <a href="http://labs.adobe.com/technologies/lightroom/">try it out yourself</a> if you like photography.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hvergi.net/2008/07/adobe-lightroom-2-beta/feed/</wfw:commentRss>
		</item>
		<item>
		<title>ICALP&#8217;08, pre-conference workshops and first day</title>
		<link>http://www.hvergi.net/2008/07/icalp08-day1/</link>
		<comments>http://www.hvergi.net/2008/07/icalp08-day1/#comments</comments>
		<pubDate>Tue, 08 Jul 2008 19:50:02 +0000</pubDate>
		<dc:creator>Arnar</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[Computer Science]]></category>

		<guid isPermaLink="false">http://www.hvergi.net/?p=24</guid>
		<description><![CDATA[So ICALP 2008 is on. I&#8217;m fortunate enough that it is held at my school (Reykjavik University) and that I was offered the chance to sit in on the talks in return for helping a little with the organization and administration.

The activities started on Friday with the DYNAMO 2008 PhD school. I didn&#8217;t attend many [...]]]></description>
			<content:encoded><![CDATA[<p>So <a href="http://www.ru.is/icalp08/">ICALP 2008</a> is on. I&#8217;m fortunate enough that it is held at my school (Reykjavik University) and that I was offered the chance to sit in on the talks in return for helping a little with the organization and administration.</p>

<p>The activities started on Friday with the <a href="http://www.liafa.jussieu.fr/~pierref/COST/Training_School_2008.html">DYNAMO 2008</a> PhD school. I didn&#8217;t attend many of the lectures, but the ones I did were very interesting. To name one, <a href="http://cgi.di.uoa.gr/~elias/">Elias Koutsoupias</a> gave informative talks on Game Theory and its use for example in online auctions.</p>

<p>Yesterday, Sunday, there were several workshops. I mostly attended <a href="http://homepages.inf.ed.ac.uk/bklin/SOS2008/">SOS&#8217;08</a>, a workshop on Structural Operational Semantics. <a href="http://www.lix.polytechnique.fr/Labo/Dale.Miller/">Dale Miller</a>, a hard-core logician, gave an excellent invited talk on formalizing SOS specifications in logic and although I didn&#8217;t understand half of it I somehow found it very interesting. His slides are available <a href="http://www.lix.polytechnique.fr/Labo/Dale.Miller/papers/sos08.pdf">at his website</a>.</p>

<p>Today was the first day of the conference proper and started off with an invited talk by Google&#8217;s <a href="http://mysliceofpizza.blogspot.com/">S. Muthukrishnan</a>, where he explained how Google runs auctions to decide what ads are displayed along with search results. Attendance to the talk was excellent, and while I had my hands full with other duties, I can safely say it was interesting on many levels.</p>

<p>After lunch I mostly attended track B, on logic, semantics and theory of programming. I&#8217;m afraid I can&#8217;t say much of merit about the talks as I had to do my best to understand them properly myself. I can say that this first day of ICALP went very smoothly in my opinion. As a bonus, the weather turned out to be very nice so in the afternoon and during the welcome reception, people could stand outside and enjoy the fresh air.</p>

<p>For photos from ICALP, check out our <a href="http://picasaweb.google.com/icalp2008">Picasa web album</a> (updated at the end of each day). For more authoritative information on some of the talks, check out MohammadReza&#8217;s guest posts on <a href="http://processalgebra.blogspot.com/">Luca&#8217;s blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.hvergi.net/2008/07/icalp08-day1/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Parsing JSON with Scala</title>
		<link>http://www.hvergi.net/2008/06/parsing-json-with-scala/</link>
		<comments>http://www.hvergi.net/2008/06/parsing-json-with-scala/#comments</comments>
		<pubDate>Thu, 26 Jun 2008 16:42:00 +0000</pubDate>
		<dc:creator>Guðmundur Bjarni</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<category><![CDATA[JSON]]></category>

		<category><![CDATA[Parsing]]></category>

		<category><![CDATA[Progrmaming]]></category>

		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://www.hvergi.net/?p=22</guid>
		<description><![CDATA[In the previous post, Arnar wrote about how to do a JSON parser in Haskell using parsec. He also mentioned that I had shown him a similar parser written in Scala, but I must admit that I stole it from the upcoming book Programming in Scala. The book is very good and I recommend it [...]]]></description>
			<content:encoded><![CDATA[<p>In the <a href="http://www.hvergi.net/2008/06/parsing-json-with-haskell/">previous post</a>, Arnar wrote about how to do a JSON parser in <em>Haskell</em> using <em>parsec</em>. He also mentioned that I had shown him a similar parser written in <a href="http://www.scala-lang.org">Scala</a>, but I must admit that I stole it from the upcoming book <a href="http://www.artima.com/shop/forsale">Programming in Scala</a>. The book is very good and I recommend it to anyone that is interested in learning Scala.</p>

<p>Before I get on with the post, I&#8217;m gonna spend a few words on explaining Scala. It is a strictly typed language with a very powerful type inference system. Scala runs on the Java Virtual Machine, a port or the .NET CLR is available. It is a mix between functional and object oriented languages and is <em>almost</em> fully interoperable with Java. Scala replaces some of the constructs and mechanisms that you normally have in Java with more general things. Rather than using anonymous classes you have closures, there are no interfaces, but you have traits and so on. Scala also adds implicit conversions, operator overloading, mutable and immutable types and some more goodies.</p>

<p>Scala access to all of the class libraries that come with Java and everything you throw into its classpath. It also comes with its own <a href="http://www.scala-lang.org/docu/files/api/index.html">class library</a> which has collections, tuples, parsers, xml types, etc etc. Relevant for this post, maps are written as <code>Map(key -&gt; value)</code>, tuples as <code>(first, second, third)</code> and lists as <code>List(1, 2, 3)</code>.</p>

<p>The first thing that is going to look weird is that the type of things appears after the name or definition and there is a keyword in front.</p>


<div class="wp_syntax"><div class="code"><pre class="scala"><span style="color: #000000; font-weight: bold;">val</span> i <span style="color: #FFAA00;">=</span> <span style="color: #cc66cc;">3</span>
<span style="color: #000000; font-weight: bold;">val</span> j<span style="color: #FFAA00;">:</span> <span style="color: #993333;">Int</span> <span style="color: #FFAA00;">=</span> i
&nbsp;
<span style="color: #000000; font-weight: bold;">def</span> hello<span style="color: #FFAA00;">:</span> <span style="color: #aaaadd;">String</span> <span style="color: #FFAA00;">=</span> <span style="color: #ff0000;">&quot;Hello world&quot;</span></pre></div></div>


<p>The <code>val</code> keyword placed in front of the variables is to tell the compiler that an immutable value is coming up and <code>var</code> is the mutable counterpart. Methods are defined with the <code>def</code> keyword. The first two lines show the type inference system at work, since you have the number <code>3</code> it can only be an integer. If there is any doubt about the type during compilation, the compiler will complain and exit. The same goes for the return type of the &#8220;hello&#8221; method, the type String is specified but can be omitted. Since the method is a oneliner the <code>= "Hello world"</code> is a short hand for <code>{ return "Hello world" }</code>.</p>

<p>Back to the JSON parser, the parsing combinator package has to be imported</p>


<div class="wp_syntax"><div class="code"><pre class="scala"><span style="color: #000000; font-weight: bold;">import</span> scala.<span style="color: #006600;">util</span>.<span style="color: #006600;">parsing</span>.<span style="color: #006600;">combinator</span>.<span style="color: #FFAA00;">_</span></pre></div></div>


<p>In Scala <code>_</code> is a sequential any reference, or like in this case as <code>*</code> in Java imports. The parser itself is a token based parser, and extends from a trait called <code>JavaTokenParsers</code> which includes parsing for identifiers, numbers and strings.</p>


<div class="wp_syntax"><div class="code"><pre class="scala"><span style="color: #000000; font-weight: bold;">class</span> JSON <span style="color: #000000; font-weight: bold;">extends</span> JavaTokenParsers <span style="color: #66cc66;">&#123;</span> 
        <span style="color: #000000; font-weight: bold;">def</span> obj<span style="color: #FFAA00;">:</span> Parser<span style="color: #66cc66;">&#91;</span>Map<span style="color: #66cc66;">&#91;</span><span style="color: #aaaadd;">String</span>, <span style="color: #993333;">Any</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #FFAA00;">=</span> 
                <span style="color: #ff0000;">&quot;{&quot;</span>~<span style="color: #FFAA00;">&gt;</span> repsep<span style="color: #66cc66;">&#40;</span>member, <span style="color: #ff0000;">&quot;,&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #FFAA00;">&lt;</span>~<span style="color: #ff0000;">&quot;}&quot;</span> ^^ <span style="color: #66cc66;">&#40;</span>Map<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> ++ <span style="color: #FFAA00;">_</span><span style="color: #66cc66;">&#41;</span> 
&nbsp;
        <span style="color: #000000; font-weight: bold;">def</span> arr<span style="color: #FFAA00;">:</span> Parser<span style="color: #66cc66;">&#91;</span><span style="color: #aaaadd;">List</span><span style="color: #66cc66;">&#91;</span><span style="color: #993333;">Any</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #FFAA00;">=</span> 
                <span style="color: #ff0000;">&quot;[&quot;</span>~<span style="color: #FFAA00;">&gt;</span> repsep<span style="color: #66cc66;">&#40;</span>value, <span style="color: #ff0000;">&quot;,&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #FFAA00;">&lt;</span>~<span style="color: #ff0000;">&quot;]&quot;</span> 
&nbsp;
        <span style="color: #000000; font-weight: bold;">def</span> member<span style="color: #FFAA00;">:</span> Parser<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd;">String</span>, <span style="color: #993333;">Any</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #FFAA00;">=</span> 
                stringLiteral~<span style="color: #ff0000;">&quot;:&quot;</span>~value ^^ 
                        <span style="color: #66cc66;">&#123;</span> <span style="color: #b1b100;">case</span> name~<span style="color: #ff0000;">&quot;:&quot;</span>~value <span style="color: #FFAA00;">=&gt;</span> <span style="color: #66cc66;">&#40;</span>name, value<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#125;</span> 
&nbsp;
        <span style="color: #000000; font-weight: bold;">def</span> value<span style="color: #FFAA00;">:</span> Parser<span style="color: #66cc66;">&#91;</span><span style="color: #993333;">Any</span><span style="color: #66cc66;">&#93;</span> <span style="color: #FFAA00;">=</span> <span style="color: #66cc66;">&#40;</span> 
                obj 
                | arr 
                | stringLiteral 
                | floatingPointNumber ^^ <span style="color: #66cc66;">&#40;</span><span style="color: #FFAA00;">_</span>.<span style="color: #006600;">toInt</span><span style="color: #66cc66;">&#41;</span> 
                | <span style="color: #ff0000;">&quot;null&quot;</span> ^^ <span style="color: #66cc66;">&#40;</span>x <span style="color: #FFAA00;">=&gt;</span> <span style="color: #b13366;">null</span><span style="color: #66cc66;">&#41;</span> 
                | <span style="color: #ff0000;">&quot;true&quot;</span> ^^ <span style="color: #66cc66;">&#40;</span>x <span style="color: #FFAA00;">=&gt;</span> <span style="color: #b13366;">true</span><span style="color: #66cc66;">&#41;</span> 
                | <span style="color: #ff0000;">&quot;false&quot;</span> ^^ <span style="color: #66cc66;">&#40;</span>x <span style="color: #FFAA00;">=&gt;</span> <span style="color: #b13366;">false</span><span style="color: #66cc66;">&#41;</span> 
                <span style="color: #66cc66;">&#41;</span> 
<span style="color: #66cc66;">&#125;</span></pre></div></div>


<p>The root of the parse tree is defined in the method <code>value</code> which returns a <code>Parser[Any]</code>. Since the short hand notation is being used and it expects are <code>Parser[Any]</code>, it uses implicit conversions for the <code>Parser</code> class and from there you get the <code>( ... | ... )</code> notation which provides you with parser alternatives. The weird <code>^^</code> thing is result conversions, so <code>floatingPointNumber ^^ (_.toInt)</code> means, take that floating number and return it as an integer using the wildcard <code>_</code>. <code>repsep</code> is a shorthand for interleaved repitition. Lastly the wavy dudes with <code>~</code>, <code>&lt;~</code> and <code>~&gt;</code> denotes sequential composition and with the less and more than symbols it keeps to the left or right.</p>

<p>To run the parser and do something meaningful with it we read in a file pass it on to the parser and print the output</p>


<div class="wp_syntax"><div class="code"><pre class="scala"><span style="color: #000000; font-weight: bold;">import</span> java.<span style="color: #006600;">io</span>.<span style="color: #006600;">FileReader</span> 
&nbsp;
<span style="color: #000000; font-weight: bold;">object</span> JSONTest <span style="color: #000000; font-weight: bold;">extends</span> JSON <span style="color: #66cc66;">&#123;</span> 
        <span style="color: #000000; font-weight: bold;">def</span> main<span style="color: #66cc66;">&#40;</span>args<span style="color: #FFAA00;">:</span> <span style="color: #aaaadd;">Array</span><span style="color: #66cc66;">&#91;</span><span style="color: #aaaadd;">String</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> 
                <span style="color: #000000; font-weight: bold;">val</span> reader <span style="color: #FFAA00;">=</span> <span style="color: #000000; font-weight: bold;">new</span> FileReader<span style="color: #66cc66;">&#40;</span>args<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> 
                println<span style="color: #66cc66;">&#40;</span>parseAll<span style="color: #66cc66;">&#40;</span>value, reader<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> 
        <span style="color: #66cc66;">&#125;</span> 
<span style="color: #66cc66;">&#125;</span></pre></div></div>


<p>Different from Java, Scala does not have static types per se but allows you to do static like things by using a <code>object</code> instead of a class. Also notice how the FileReader is being imported directly from the Java class libraries.</p>

<p>After we&#8217;ve compiled the code and run it on the same file JSON file as Arnar provided in his example, we are presented with</p>

<p><pre>
[22.2] parsed: 
    Map(&#8221;glossary&#8221; -> 
        Map(&#8221;title&#8221; -> &#8220;example glossary&#8221;, &#8220;GlossDiv&#8221; -> 
            Map(&#8221;title&#8221; -> &#8220;S&#8221;, 
                &#8220;GlossList&#8221; -> 
                Map(&#8221;GlossEntry&#8221; -> 
                    Map(&#8221;Acronym&#8221; -> &#8220;SGML&#8221;, 
                        &#8220;Abbrev&#8221; -> &#8220;ISO 8879:1986&#8243;, 
                        &#8220;GlossSee&#8221; -> &#8220;markup&#8221;, 
                        &#8220;GlossTerm&#8221; -> &#8220;Standard Generalized Markup Language&#8221;, 
                        &#8220;ID&#8221; -> &#8220;SGML&#8221;, 
                        &#8220;GlossDef&#8221; -> 
                            Map(&#8221;para&#8221; -> 
                                &#8220;A meta-markup language, used to create markup 
                                languages such as DocBook.&#8221;, 
                                &#8220;GlossSeeAlso&#8221; -> List(&#8221;GML&#8221;, &#8220;XML&#8221;)), 
                        &#8220;SortAs&#8221; -> &#8220;SGML&#8221;)))))
</pre></p>

<p>I am to lazy to format this output nicely, but you get the picture. :) I noticed also that this JSON parser does not handle comments, which I leave as an exercise to my good readers.</p>

<p>I have been meaning to write something on Scala for quite some time now and now I was forced to since Arnar mentioned my crazy ranting. In the coming weeks I am hopefully going to show you some more niceness of Scala, such as implicit conversions, XML types, its functional nature, etc.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hvergi.net/2008/06/parsing-json-with-scala/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Parsing JSON with Haskell</title>
		<link>http://www.hvergi.net/2008/06/parsing-json-with-haskell/</link>
		<comments>http://www.hvergi.net/2008/06/parsing-json-with-haskell/#comments</comments>
		<pubDate>Thu, 26 Jun 2008 15:15:00 +0000</pubDate>
		<dc:creator>Arnar</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Haskell]]></category>

		<category><![CDATA[JSON]]></category>

		<category><![CDATA[Parsing]]></category>

		<guid isPermaLink="false">http://www.hvergi.net/?p=21</guid>
		<description><![CDATA[Note: This post is literate Haskell, you can copy the whole text to a .lhs file and compile it with GHC.

A friend of mine was showing me a very simple and elegant parser for
JSON, written in Scala. As an excercise, I decided
to write one in Haskell, using the Parsec
parser combinator library. Here&#8217;s a quick walkthrough.

First [...]]]></description>
			<content:encoded><![CDATA[<p><em>Note: This post is literate Haskell, you can copy the whole text to a .lhs file and compile it with GHC.</em></p>

<p><a href="http://twitter.com/gudmundur">A friend of mine</a> was showing me a very <a href="http://www.hvergi.net/2008/06/parsing-json-with-scala/">simple and elegant parser</a> for
<a href="http://www.json.org/">JSON</a>, written in <a href="http://www.scala-lang.org/">Scala</a>. As an excercise, I decided
to write one in <a href="http://www.haskell.org/">Haskell</a>, using the <a href="http://legacy.cs.uu.nl/daan/parsec.html">Parsec</a>
parser combinator library. Here&#8217;s a quick walkthrough.</p>

<p>First we need some imports,</p>


<div class="wp_syntax"><div class="code"><pre class="haskell"><span style="color: #999;">&gt;</span> <span style="color: #06c; font-weight: bold;">import</span> <span style="color: #06c; font-weight: bold;">qualified</span> Data<span style="color: #999;">.</span>Map <span style="color: #06c; font-weight: bold;">as</span> Map
<span style="color: #999;">&gt;</span> <span style="color: #06c; font-weight: bold;">import</span> Control<span style="color: #999;">.</span><span style="color: #295585;">Monad</span> <span style="color: #777;">&#40;</span>liftM<span style="color: #777;">&#41;</span>
<span style="color: #999;">&gt;</span> <span style="color: #06c; font-weight: bold;">import</span> Text<span style="color: #999;">.</span>ParserCombinators<span style="color: #999;">.</span>Parsec
<span style="color: #999;">&gt;</span> <span style="color: #06c; font-weight: bold;">import</span> <span style="color: #06c; font-weight: bold;">qualified</span> Text<span style="color: #999;">.</span>ParserCombinators<span style="color: #999;">.</span>Parsec<span style="color: #999;">.</span>Token <span style="color: #06c; font-weight: bold;">as</span> T
<span style="color: #999;">&gt;</span> <span style="color: #06c; font-weight: bold;">import</span> Text<span style="color: #999;">.</span>ParserCombinators<span style="color: #999;">.</span>Parsec<span style="color: #999;">.</span>Language</pre></div></div>


<p>Now, to represent a JSON value, we use an algebraic data type. For simplicity, we&#8217;ll only worry about
integers and not floating point numbers. The definition of the type is simple, to be concise we use single
letter constructor names (not a good idea if you want to publish a proper library).</p>


<div class="wp_syntax"><div class="code"><pre class="haskell"><span style="color: #999;">&gt;</span> <span style="color: #06c; font-weight: bold;">data</span> JSON <span style="color: #999;">=</span> S <span style="color: #295585;">String</span> 
<span style="color: #999;">&gt;</span>           <span style="color: #999;">|</span> I <span style="color: #295585;">Integer</span> 
<span style="color: #999;">&gt;</span>           <span style="color: #999;">|</span> B <span style="color: #295585;">Bool</span> 
<span style="color: #999;">&gt;</span>           <span style="color: #999;">|</span> L <span style="color: #777;">&#91;</span>JSON<span style="color: #777;">&#93;</span> 
<span style="color: #999;">&gt;</span>           <span style="color: #999;">|</span> O <span style="color: #777;">&#40;</span>Map<span style="color: #999;">.</span>Map <span style="color: #295585;">String</span> JSON<span style="color: #777;">&#41;</span>
<span style="color: #999;">&gt;</span>           <span style="color: #06c; font-weight: bold;">deriving</span> <span style="color: #295585;">Show</span></pre></div></div>


<p>Now, to make our life somewhat easier, we&#8217;ll make use of the facilities in the <code>ParsecToken</code> module. This module
provides us with ways to create functions for generic <em>lexers</em>, which deal correctly with whitespace (and comments)
in a language. The first step is to create a language definition, in our case it only specifies how JSON comments
are delimited. From the definition we create a token parser which we call <code>lexer</code>.</p>


<div class="wp_syntax"><div class="code"><pre class="haskell"><span style="color: #999;">&gt;</span> ldef <span style="color: #999;">=</span> emptyDef <span style="color: #777;">&#123;</span> commentStart <span style="color: #999;">=</span> <span style="background-color: #e9e9e9;">&quot;/*&quot;</span>
<span style="color: #999;">&gt;</span>                 , commentEnd <span style="color: #999;">=</span> <span style="background-color: #e9e9e9;">&quot;*/&quot;</span>
<span style="color: #999;">&gt;</span>                 , commentLine <span style="color: #999;">=</span> <span style="background-color: #e9e9e9;">&quot;//&quot;</span>
<span style="color: #999;">&gt;</span>                 <span style="color: #777;">&#125;</span>
<span style="color: #999;">&gt;</span> lexer <span style="color: #999;">=</span> T<span style="color: #999;">.</span>makeTokenParser ldef</pre></div></div>


<p>We can now pass <code>lexer</code> to <a href="http://legacy.cs.uu.nl/daan/download/parsec/parsec.html#ParsecToken">the convenience functions of ParsecToken</a>.
As is common with such parsers, we rebind their names in our module to spare us referring to <code>lexer</code>
every time we use them.</p>


<div class="wp_syntax"><div class="code"><pre class="haskell"><span style="color: #999;">&gt;</span> symbol        <span style="color: #999;">=</span> T<span style="color: #999;">.</span>symbol        lexer
<span style="color: #999;">&gt;</span> stringLiteral <span style="color: #999;">=</span> T<span style="color: #999;">.</span>stringLiteral lexer
<span style="color: #999;">&gt;</span> integer       <span style="color: #999;">=</span> T<span style="color: #999;">.</span>integer       lexer
<span style="color: #999;">&gt;</span> squares       <span style="color: #999;">=</span> T<span style="color: #999;">.</span>squares       lexer
<span style="color: #999;">&gt;</span> braces        <span style="color: #999;">=</span> T<span style="color: #999;">.</span>braces        lexer
<span style="color: #999;">&gt;</span> commaSep      <span style="color: #999;">=</span> T<span style="color: #999;">.</span>commaSep      lexer</pre></div></div>


<p>And now the parser itself. The basic element is a JSON value. From now on I&#8217;ll include the type
signatures to make things easier to follow, but they are of course not necessary.</p>


<div class="wp_syntax"><div class="code"><pre class="haskell"><span style="color: #999;">&gt;</span> value <span style="color: #999;">::</span> Parser JSON
<span style="color: #999;">&gt;</span> value <span style="color: #999;">=</span> liftM S stringLiteral
<span style="color: #999;">&gt;</span>     <span style="color: #999;">&lt;|&gt;</span> liftM I integer
<span style="color: #999;">&gt;</span>     <span style="color: #999;">&lt;|&gt;</span> liftM B boolean
<span style="color: #999;">&gt;</span>     <span style="color: #999;">&lt;|&gt;</span> liftM L list
<span style="color: #999;">&gt;</span>     <span style="color: #999;">&lt;|&gt;</span> liftM O object</pre></div></div>


<p>A value is simply a choice between each of the basic types, a string, an integer, a boolean, a list of values
or an object (which is simply a mapping from strings to values). We defer each to its own parser, and simply
lift the value constructors to the parser monad.
We get parsing of strings and integers completely for free from the ParsecToken module, but the other three we
need to handle ourselves. The boolean case is simple,</p>


<div class="wp_syntax"><div class="code"><pre class="haskell"><span style="color: #999;">&gt;</span> boolean <span style="color: #999;">::</span> Parser <span style="color: #295585;">Bool</span>
<span style="color: #999;">&gt;</span> boolean <span style="color: #999;">=</span> <span style="color: #06c; font-weight: bold;">do</span> symbol <span style="background-color: #e9e9e9;">&quot;true&quot;</span>
<span style="color: #999;">&gt;</span>              <span style="font-weight: bold;">return</span> True
<span style="color: #999;">&gt;</span>       <span style="color: #999;">&lt;|&gt;</span> <span style="color: #06c; font-weight: bold;">do</span> symbol <span style="background-color: #e9e9e9;">&quot;false&quot;</span>
<span style="color: #999;">&gt;</span>              <span style="font-weight: bold;">return</span> False</pre></div></div>


<p>Surprisingly, the list is even simpler, due to the helpers from ParsecToken.</p>


<div class="wp_syntax"><div class="code"><pre class="haskell"><span style="color: #999;">&gt;</span> list <span style="color: #999;">::</span> Parser <span style="color: #777;">&#91;</span>JSON<span style="color: #777;">&#93;</span>
<span style="color: #999;">&gt;</span> list <span style="color: #999;">=</span> squares <span style="color: #999;">$</span> commaSep value</pre></div></div>


<p>Parsing objects is also fairly simple, basically just like list except that instead of parsing a
comma seperated list of values, we parse a comma seperated list of key/value pairs. Such a pair is handled
by the nested <code>keyValue</code> parser.</p>


<div class="wp_syntax"><div class="code"><pre class="haskell"><span style="color: #999;">&gt;</span> object <span style="color: #999;">::</span> Parser <span style="color: #777;">&#40;</span>Map<span style="color: #999;">.</span>Map <span style="color: #295585;">String</span> JSON<span style="color: #777;">&#41;</span>
<span style="color: #999;">&gt;</span> object <span style="color: #999;">=</span> liftM Map<span style="color: #999;">.</span>fromList <span style="color: #999;">$</span> braces <span style="color: #999;">$</span> commaSep keyValue
<span style="color: #999;">&gt;</span>          <span style="color: #06c; font-weight: bold;">where</span>
<span style="color: #999;">&gt;</span>              keyValue <span style="color: #999;">=</span> <span style="color: #06c; font-weight: bold;">do</span> key <span style="color: #999;">&lt;-</span> stringLiteral
<span style="color: #999;">&gt;</span>                            symbol <span style="background-color: #e9e9e9;">&quot;:&quot;</span>
<span style="color: #999;">&gt;</span>                            val <span style="color: #999;">&lt;-</span> value
<span style="color: #999;">&gt;</span>                            <span style="font-weight: bold;">return</span> <span style="color: #777;">&#40;</span>key, val<span style="color: #777;">&#41;</span></pre></div></div>


<p>And that&#8217;s it! To test this easily, we finally add a main action that simply parses a JSON value from
standard input.</p>


<div class="wp_syntax"><div class="code"><pre class="haskell"><span style="color: #999;">&gt;</span> main <span style="color: #999;">=</span> <span style="font-weight: bold;">getContents</span> <span style="color: #999;">&gt;&gt;=</span> parseTest value</pre></div></div>


<h3>Testing the parser</h3>

<p>A simple test, including a comment in the middle:</p>

<p><pre>
arnarb-macbook:tmp arnarb$ runhaskell json.hs 
[1,"two",false,{"one":1, /* a comment here */ "two":2, "list":[]}]
&lt; here I pressed ctrl-d to insert an EOF, rest is output >
L [I 1,S "two",B False,O (fromList [("list",L []),(&#8221;one&#8221;,I 1),(&#8221;two&#8221;,I 2)])]
</pre></p>

<p>As an more complex test, we run it on the first <a href="http://www.json.org/example.html">example from json.org</a> (newlines
and indent added to the output for clarity).</p>

<p><pre>
arnarb-macbook:tmp arnarb$ runhaskell json.hs 
{
  "glossary": {
    "title": "example glossary",
    "GlossDiv": {
      "title": "S",
      "GlossList": {
        "GlossEntry": {
          "ID": "SGML",
          "SortAs": "SGML",
          "GlossTerm": "Standard Generalized Markup Language",
          "Acronym": "SGML",
          "Abbrev": "ISO 8879:1986",
          "GlossDef": {
            "para": "A meta-markup language, used to create markup languages such as DocBook.",
            "GlossSeeAlso": ["GML", "XML"]
          },
          &#8220;GlossSee&#8221;: &#8220;markup&#8221;
        }
      }
    }
  }
}
&lt; here I pressed ctrl-d to insert an EOF, rest is output >
O (fromList [
    ("glossary",O (fromList [
        ("GlossDiv",O (fromList [
            ("GlossList",O (fromList [
                ("GlossEntry",O (fromList [
                    ("Abbrev",S "ISO 8879:1986"),
                    ("Acronym",S "SGML"),
                    ("GlossDef",O (fromList [
                        ("GlossSeeAlso",L [S "GML",S "XML"]),
                        (&#8221;para&#8221;,S &#8220;A meta-markup language, used to create markup languages such as DocBook.&#8221;)
                    ])),
                    (&#8221;GlossSee&#8221;,S &#8220;markup&#8221;),
                    (&#8221;GlossTerm&#8221;,S &#8220;Standard Generalized Markup Language&#8221;),
                    (&#8221;ID&#8221;,S &#8220;SGML&#8221;),
                    (&#8221;SortAs&#8221;,S &#8220;SGML&#8221;)
                ]))
            ])),
            (&#8221;title&#8221;,S &#8220;S&#8221;)
        ])),
        (&#8221;title&#8221;,S &#8220;example glossary&#8221;)
    ]))
])
</pre></p>

<p>Finally, we should note that many people
<a href="http://snippets.dzone.com/posts/show/3660">have</a>
<a href="http://www.tom.sfc.keio.ac.jp/%7Esakai/d/data/200604/JSON.hs">done</a>
<a href="http://hackage.haskell.org/cgi-bin/hackage-scripts/package/json">this</a>
<a href="http://hackage.haskell.org/cgi-bin/hackage-scripts/package/RJson">before</a> :)</p>

<h3>Edits:</h3>

<ul>
<li>Added link to Scala version.</li>
<li>Small revisisons, thanks to <code>kpreid</code> on <code>#haskell</code> for pointing out <code>braces</code> and <code>squares</code>.</li>
<li>Stop lugging <code>lexer around</code>, thanks to Paczesiowa on <a href="http://www.reddit.com/info/6p3dv/comments/">reddit</a> for useful suggestions.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.hvergi.net/2008/06/parsing-json-with-haskell/feed/</wfw:commentRss>
		</item>
		<item>
		<title>The need to know</title>
		<link>http://www.hvergi.net/2008/06/the-need-to-know/</link>
		<comments>http://www.hvergi.net/2008/06/the-need-to-know/#comments</comments>
		<pubDate>Thu, 12 Jun 2008 11:38:18 +0000</pubDate>
		<dc:creator>Einar</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.hvergi.net/?p=18</guid>
		<description><![CDATA[Earlier this week, Arnar wrote about silly form validation techniques. He specifically mentioned problems with providing Icelandic phone numbers in forms, which fail validation due to the lack of area or city codes. I agree with Arnar that form validation is surprisingly hard to get right and if not done properly it can annoy your [...]]]></description>
			<content:encoded><![CDATA[<p>Earlier this week, Arnar wrote about <a href="http://www.hvergi.net/2008/06/whats-in-a-phone-number/">silly form validation techniques</a>. He specifically mentioned problems with providing Icelandic phone numbers in forms, which fail validation due to the lack of area or city codes. I agree with Arnar that form validation is surprisingly hard to get right and if not done properly it can annoy your users, possibly to the point where they cancel their registration.</p>

<p>However, in addition to improving the form validation code, I suggest (and I am by no means the first to propose this) that we stop requesting so much information in user registration forms, and focus on information that is relevant to the application. For instance, it is very rational to ask for a username and a password that the user wishes to have. It is also rational to ask for an email address which can be used to verify the registration, as well as provide a way to handle forgotten passwords.</p>

<p>On the other hand, I see no reason to require phone numbers, age and so on. In Arnar&#8217;s post there is a screen shot from Apple, where they fail to validate the phone number. Why does Apple require you to register your phone number in the first place? What if I do not, under any circumstances, want Apple to call me? If they really need to reach me, they can do so via e-mail. I&#8217;m also curious how often Apple actually uses this information, has anyone received a phone call from Apple after signing up for an Apple ID?</p>

<p>The form that led to this post is the account creation form at <a href="http://www.digg.com">Digg</a>. I saw an article that I wanted to bump up, an action that requires a Digg account. However, after I viewed the registration form I decided not to sign up.</p>

<p>In addition to the standerd username, password and email address, Digg requires you to provide your first and last name, gender, birthday, and full address. They provide no rational explanation for requesting this information, so I can only assume that they use it to improve their profiling. If I ever feel so strongly about a submitted story on Digg that I feel compelled to sign up, I will most certainly register with false information.</p>

<p>So to wrap this up: request only the information you need in order to provide the service. It leads to simpler form validation, better privacy and less annoyed users. Sounds like a win/win situation to me.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hvergi.net/2008/06/the-need-to-know/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
