<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>Inadvertent incorrectness</title>
    <subtitle>... but that doesn&#39;t matter, because I turn it into a sexy dance</subtitle>
    <link rel="self" type="application/atom+xml" href="https://new-blog.incorrectness.dk/blog/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://new-blog.incorrectness.dk/blog/"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2011-09-28T21:46:37+00:00</updated>
    <id>https://new-blog.incorrectness.dk/blog/atom.xml</id>
    <entry xml:lang="en">
        <title>Quality assessment of Haskell programs</title>
        <published>2011-09-28T21:46:37+00:00</published>
        <updated>2011-09-28T21:46:37+00:00</updated>
        
        <author>
          <name>
            Ken
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://new-blog.incorrectness.dk/blog/posts/quality-assessment-haskell-program/"/>
        <id>https://new-blog.incorrectness.dk/blog/posts/quality-assessment-haskell-program/</id>
        
        <content type="html" xml:base="https://new-blog.incorrectness.dk/blog/posts/quality-assessment-haskell-program/">&lt;p&gt;One of the greatest things about writing code in Haskell is the wonderful libraries (incidently, one of the worst things about writing code in Haskell is the libraries). In particular the libraries for assessing the quality of your own code. I’m especially found of:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;http://hackage.haskell.org/package/QuickCheck&quot;&gt;QuickCheck&lt;/a&gt; for a quick testing of the correctness of my code, by letting me specify the &lt;em&gt;properties&lt;/em&gt; I want to check, and taking care of generating random test-cases.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;http://hackage.haskell.org/package/criterion&quot;&gt;Criterion&lt;/a&gt; for checking the time performance of my code in a statistical sound manner. Taking care of dealing with the garbage collector and such like.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In this post I give a quick tour of how to use these libraries.&lt;/p&gt;
&lt;h3 id=&quot;getting-the-libraries&quot;&gt;Getting the libraries&lt;/h3&gt;
&lt;p&gt;Our first step is to install QuickCheck and Criterion:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;$&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; cabal install criterion quickcheck&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we are ready to go.&lt;/p&gt;
&lt;h3 id=&quot;backstory&quot;&gt;Backstory&lt;/h3&gt;
&lt;p&gt;The standard list-based implementation of quicksort that you so often see, seems ripe for some optimisations:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;quicksort&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; :: Ord&lt;/span&gt;&lt;span&gt; a&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; [a]&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span&gt; [a]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;quicksort &lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;[]&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;     =&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; []&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;quicksort (p&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;/span&gt;&lt;span&gt;xs)&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span&gt; quicksort lesser &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;++&lt;/span&gt;&lt;span&gt; [p]&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; ++&lt;/span&gt;&lt;span&gt; quicksort greater&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   lesser  &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;/span&gt;&lt;span&gt; [ x &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;|&lt;/span&gt;&lt;span&gt; x &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; xs, x &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span&gt; p]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   greater &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;/span&gt;&lt;span&gt; [ x &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;|&lt;/span&gt;&lt;span&gt; x &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; xs, x &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;=&lt;/span&gt;&lt;span&gt; p]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;One obvious optimisation could be to only traverse &lt;code&gt;xs&lt;/code&gt; once, when partitioning the elements into lesser and greater (and equal) than &lt;code&gt;p&lt;/code&gt;. Here is one implementation that uses &lt;code&gt;foldl&#39;&lt;/code&gt; for maximum performance (we hope):&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;kquicksort&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; :: Ord&lt;/span&gt;&lt;span&gt; a&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; [a]&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span&gt; [a]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;kquicksort &lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;[]&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;     =&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; []&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;kquicksort (p&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;/span&gt;&lt;span&gt;xs)&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span&gt; kquicksort lesser &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;++&lt;/span&gt;&lt;span&gt; equal &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;++&lt;/span&gt;&lt;span&gt; kquicksort greater&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        (lesser,equal,greater)&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span&gt; foldl&amp;#39; part (&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;[]&lt;/span&gt;&lt;span&gt;,[p],&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;[]&lt;/span&gt;&lt;span&gt;) xs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        part (l,e,g) x &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;          case&lt;/span&gt;&lt;span&gt; compare x p &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;            LT&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span&gt; (x &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;/span&gt;&lt;span&gt; l, e, g)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;            GT&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span&gt; (l, e, x &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;/span&gt;&lt;span&gt; g)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;            EQ&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span&gt; (l, x &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;/span&gt;&lt;span&gt; e, g)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;checking-some-properties&quot;&gt;Checking some properties&lt;/h3&gt;
&lt;p&gt;The basic usage of QuickCheck is to specify the properties you care about as ordinary Haskell functions, and then you use the &lt;code&gt;quickCheck&lt;/code&gt; function to check the properties. In the following I use &lt;code&gt;QC&lt;/code&gt; as the qualified name for values from the QuickCheck library.&lt;/p&gt;
&lt;p&gt;The first property we want to check is that &lt;code&gt;kquicksort&lt;/code&gt; returns a sorted result. To check this property we also define a helper predicate &lt;code&gt;sorted&lt;/code&gt; that checks that a list is sorted:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;sorted&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; :: Ord&lt;/span&gt;&lt;span&gt; a&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; [a]&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt; Bool&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;sorted (x1&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;/span&gt;&lt;span&gt;x2&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;/span&gt;&lt;span&gt;xs)&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span&gt; x1 &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span&gt; x2 &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span&gt; (sorted &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;$&lt;/span&gt;&lt;span&gt; x2&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;/span&gt;&lt;span&gt;xs)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;sorted _          &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; True&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;prop_sorted&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; :: Ord&lt;/span&gt;&lt;span&gt; a&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; [a]&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt; Bool&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;prop_sorted xs &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;/span&gt;&lt;span&gt; sorted &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;$&lt;/span&gt;&lt;span&gt; kquicksort xs&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The next properties that we want to check could be that &lt;code&gt;kquicksort&lt;/code&gt; is idempotent and if it is given an ordered argument it doesn’t mess it up:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;prop_idempotent&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; :: Ord&lt;/span&gt;&lt;span&gt; a&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; [a]&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt; Bool&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;prop_idempotent xs &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;/span&gt;&lt;span&gt; kquicksort xs &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;==&lt;/span&gt;&lt;span&gt; (kquicksort &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;$&lt;/span&gt;&lt;span&gt; kquicksort xs)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;prop_ordered&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; :: Ord&lt;/span&gt;&lt;span&gt; a&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; QC.&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;OrderedList&lt;/span&gt;&lt;span&gt; a&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt; Bool&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;prop_ordered (&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;QC.&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;Ordered&lt;/span&gt;&lt;span&gt; xs)&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span&gt; xs &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;==&lt;/span&gt;&lt;span&gt; kquicksort xs&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For &lt;code&gt;prop_ordered&lt;/code&gt; we use the type class &lt;code&gt;OrderedList&lt;/code&gt; to specify that this property should only hold for arguments that are sorted.&lt;/p&gt;
&lt;p&gt;Now we can use the &lt;code&gt;quickCheck&lt;/code&gt; function to check all our properties:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;checkAll &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;= do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;  QC.&lt;/span&gt;&lt;span&gt;quickCheck (prop_sorted &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;Int&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt; Bool&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;  QC.&lt;/span&gt;&lt;span&gt;quickCheck (prop_idempotent &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;Int&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt; Bool&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;  QC.&lt;/span&gt;&lt;span&gt;quickCheck (prop_ordered &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; QC.&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;OrderedList Int -&amp;gt; Bool&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The type constraints on all the properties are necessary, because that is what makes overloading works, so that &lt;code&gt;quickCheck&lt;/code&gt; can generate random test-cases. Alternatively we can give our properties a less general non-polymorphic type. However, by keeping the more general type for our properties we can check them for other types as well. For instance, we might want to check it for a custom type, like the following type &lt;code&gt;Colour&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;data Colour =&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; Red&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; Green&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; Blue&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;            deriving&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;Eq&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; Show&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; Ord&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Before we can use QuickCheck with the &lt;code&gt;Colour&lt;/code&gt; type, we need to specify how to generate random values of this type. That is, make it an instance of the &lt;code&gt;Arbitrary&lt;/code&gt; type class:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;instance&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; QC.&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;Arbitrary Colour where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  arbitrary &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; QC.&lt;/span&gt;&lt;span&gt;elements [&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;Red&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; Green&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; Blue&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, we can check the &lt;code&gt;prop_sorted&lt;/code&gt; property by adding a simple type constraint:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;QC.&lt;/span&gt;&lt;span&gt;quickCheck (prop_sorted &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;Colour&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt; Bool&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&quot;checking-the-performance&quot;&gt;Checking the performance&lt;/h3&gt;
&lt;p&gt;Once we have convinced our-self the code could be correct, we can start worrying about performance. Thus, we turn to Criterion. When doing performance testing there are all sorts of gnarly details that can go wrong and ruin our experiments: we forget to force the lazy evaluation and thus measure the wrong thing, the running time of function we try to measure is to short to get a meaningful reading of with clock resolution on the computer we use, the garbage collector might introduce noise from one sampling into another, we might be using the computer for something while we run the experiments which could introduce noise, and so on.&lt;/p&gt;
&lt;p&gt;In the following I use &lt;code&gt;C&lt;/code&gt; as the qualified name for values from the Criterion library.&lt;/p&gt;
&lt;p&gt;Criterion takes care of all these concerns, and just present us with a simple interface where the only thing we need to specify is which functions we want to run, on what data, and how much we want the result to be evaluated. Thus, to benchmark &lt;code&gt;kquicksort&lt;/code&gt; on the list &lt;code&gt;[20, 10, 30]&lt;/code&gt; and get a fully evaluated result, we first use the &lt;code&gt;nf&lt;/code&gt; (for normal form) function from Criterion to get something &lt;code&gt;Benchmarkable&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;C.&lt;/span&gt;&lt;span&gt;nf kquicksort [&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;20&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 10&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 30&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When we have something benchmarkable, we use the &lt;code&gt;bench&lt;/code&gt; function to label it and turn it into a &lt;code&gt;Benchmark&lt;/code&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;C.&lt;/span&gt;&lt;span&gt;bench &lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;kquickcheck on short list&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; $&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; C.&lt;/span&gt;&lt;span&gt;nf kquicksort [&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;20&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 10&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 30&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When we have a list of benchmark we can hand it over to &lt;code&gt;defaultMain&lt;/code&gt; which makes an excellent &lt;code&gt;main&lt;/code&gt; body for us that will allow us to configure our benchmark from the command-line without recompiling the program. Without further ado:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;makeList&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; :: Int -&amp;gt;&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;Int&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;makeList n &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; QC.&lt;/span&gt;&lt;span&gt;unGen (&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;QC.&lt;/span&gt;&lt;span&gt;vector n) (&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;R.&lt;/span&gt;&lt;span&gt;mkStdGen &lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;42&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 25&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;main&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; :: IO&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;main &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;= do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  checkAll&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;  let&lt;/span&gt;&lt;span&gt; sizes &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 10000&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 20000&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 50000&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 75000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;              ,&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 100000&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 250000&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 500000&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 1000000&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;  let&lt;/span&gt;&lt;span&gt; inputs &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;/span&gt;&lt;span&gt; [(n, makeList n)&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; |&lt;/span&gt;&lt;span&gt; n &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; sizes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;  let&lt;/span&gt;&lt;span&gt; benchmarks &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; C.&lt;/span&gt;&lt;span&gt;bench (name &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;++&lt;/span&gt;&lt;span&gt; show n)&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; $&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; C.&lt;/span&gt;&lt;span&gt;nf sort ns&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;                   |&lt;/span&gt;&lt;span&gt; (n, ns)&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;      &amp;lt;-&lt;/span&gt;&lt;span&gt; inputs,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                     (name, sort)&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span&gt; [(&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;quicksort &amp;quot;&lt;/span&gt;&lt;span&gt;,  quicksort),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                                      (&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;kquicksort &amp;quot;&lt;/span&gt;&lt;span&gt;, kquicksort)]]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;  C.&lt;/span&gt;&lt;span&gt;defaultMain benchmarks&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the helper function &lt;code&gt;makeList&lt;/code&gt; I have reused the generator framework from QuickCheck for generating some random test data for my benchmark. For simple integer lists as we have here it is a bit overkill, but for more complex input it can be nice.&lt;/p&gt;
&lt;h3 id=&quot;the-complete-program&quot;&gt;The complete program&lt;/h3&gt;
&lt;p&gt;If you want to find out if &lt;code&gt;kquicksort&lt;/code&gt; really is faster than &lt;code&gt;quicksort&lt;/code&gt; is here the complete program.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;import qualified&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Criterion.Main&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; as&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; C&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;import qualified&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Test.QuickCheck&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; as&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; QC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;import qualified&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Test.QuickCheck.Gen&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; as&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; QC&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Data.List&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;foldl&amp;#39;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;import qualified&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; System.Random&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; as&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; R&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;quicksort&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; :: Ord&lt;/span&gt;&lt;span&gt; a&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; [a]&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span&gt; [a]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;quicksort &lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;[]&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;     =&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; []&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;quicksort (p&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;/span&gt;&lt;span&gt;xs)&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span&gt; quicksort lesser &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;++&lt;/span&gt;&lt;span&gt; [p]&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; ++&lt;/span&gt;&lt;span&gt; quicksort greater&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        lesser  &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;/span&gt;&lt;span&gt; [ x &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;|&lt;/span&gt;&lt;span&gt; x &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; xs, x &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span&gt; p]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        greater &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;/span&gt;&lt;span&gt; [ x &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;|&lt;/span&gt;&lt;span&gt; x &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; xs, x &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;=&lt;/span&gt;&lt;span&gt; p]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;kquicksort&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; :: Ord&lt;/span&gt;&lt;span&gt; a&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; [a]&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span&gt; [a]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;kquicksort &lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;[]&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;     =&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; []&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;kquicksort (p&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;/span&gt;&lt;span&gt;xs)&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span&gt; kquicksort lesser &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;++&lt;/span&gt;&lt;span&gt; equal &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;++&lt;/span&gt;&lt;span&gt; kquicksort greater&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        (lesser,equal,greater)&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span&gt; foldl&amp;#39; part (&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;[]&lt;/span&gt;&lt;span&gt;,[p],&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;[]&lt;/span&gt;&lt;span&gt;) xs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        part (l,e,g) x &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;          case&lt;/span&gt;&lt;span&gt; compare x p &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;of&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;            LT&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span&gt; (x &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;/span&gt;&lt;span&gt; l, e, g)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;            GT&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span&gt; (l, e, x &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;/span&gt;&lt;span&gt; g)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;            EQ&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span&gt; (l, x &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;/span&gt;&lt;span&gt; e, g)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;sorted&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; :: Ord&lt;/span&gt;&lt;span&gt; a&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; [a]&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt; Bool&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;sorted (x1&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;/span&gt;&lt;span&gt;x2&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;/span&gt;&lt;span&gt;xs)&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span&gt; x1 &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span&gt; x2 &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span&gt; (sorted &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;$&lt;/span&gt;&lt;span&gt; x2&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;/span&gt;&lt;span&gt;xs)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;sorted _          &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; True&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;prop_sorted&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; :: Ord&lt;/span&gt;&lt;span&gt; a&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; [a]&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt; Bool&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;prop_sorted xs &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;/span&gt;&lt;span&gt; sorted &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;$&lt;/span&gt;&lt;span&gt; kquicksort xs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;prop_idempotent&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; :: Ord&lt;/span&gt;&lt;span&gt; a&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span&gt; [a]&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt; Bool&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;prop_idempotent xs &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;/span&gt;&lt;span&gt; kquicksort xs &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;==&lt;/span&gt;&lt;span&gt; (kquicksort &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;$&lt;/span&gt;&lt;span&gt; kquicksort xs)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;prop_ordered&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; :: Ord&lt;/span&gt;&lt;span&gt; a&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; QC.&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;OrderedList&lt;/span&gt;&lt;span&gt; a&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt; Bool&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;prop_ordered (&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;QC.&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;Ordered&lt;/span&gt;&lt;span&gt; xs)&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;/span&gt;&lt;span&gt; xs &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;==&lt;/span&gt;&lt;span&gt; kquicksort xs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;data Colour =&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; Red&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; Green&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; Blue&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;            deriving&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;Eq&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; Show&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; Ord&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;instance&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; QC.&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;Arbitrary Colour where&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  arbitrary &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; QC.&lt;/span&gt;&lt;span&gt;elements [&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;Red&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; Green&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; Blue&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;checkAll &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;= do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;  QC.&lt;/span&gt;&lt;span&gt;quickCheck (prop_sorted &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;Int&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt; Bool&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;  QC.&lt;/span&gt;&lt;span&gt;quickCheck (prop_sorted &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;Colour&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt; Bool&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;  QC.&lt;/span&gt;&lt;span&gt;quickCheck (prop_idempotent &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;Int&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt; Bool&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;  QC.&lt;/span&gt;&lt;span&gt;quickCheck (prop_ordered &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; QC.&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;OrderedList Int -&amp;gt; Bool&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;makeList&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; :: Int -&amp;gt;&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;Int&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;makeList n &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; QC.&lt;/span&gt;&lt;span&gt;unGen (&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;QC.&lt;/span&gt;&lt;span&gt;vector n) (&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;R.&lt;/span&gt;&lt;span&gt;mkStdGen &lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;42&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 25&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;main&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; :: IO&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;main &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;= do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  checkAll&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;  let&lt;/span&gt;&lt;span&gt; sizes &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 10000&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 20000&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 50000&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 75000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;              ,&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 100000&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 250000&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 500000&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 1000000&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;  let&lt;/span&gt;&lt;span&gt; inputs &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;/span&gt;&lt;span&gt; [(n, makeList n)&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; |&lt;/span&gt;&lt;span&gt; n &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span&gt; sizes]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;  let&lt;/span&gt;&lt;span&gt; benchmarks &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; C.&lt;/span&gt;&lt;span&gt;bench (name &lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;++&lt;/span&gt;&lt;span&gt; show n)&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; $&lt;/span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; C.&lt;/span&gt;&lt;span&gt;nf sort ns&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;                   |&lt;/span&gt;&lt;span&gt; (n, ns)&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;      &amp;lt;-&lt;/span&gt;&lt;span&gt; inputs,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                     (name, sort)&lt;/span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span&gt; [(&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;quicksort &amp;quot;&lt;/span&gt;&lt;span&gt;,  quicksort),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                                      (&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;kquicksort &amp;quot;&lt;/span&gt;&lt;span&gt;, kquicksort)]]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;  C.&lt;/span&gt;&lt;span&gt;defaultMain benchmarks&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Compile it with the command-line&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;$&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; ghc&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; -O3 -W --make&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; Quicksort.hs&lt;/span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; -o&lt;/span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; Quicksort&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;</content>
        
    </entry>
    <entry xml:lang="en">
        <title>I Am Going to JAOO 2008 As A F# Expert</title>
        <published>2008-09-11T20:01:45+00:00</published>
        <updated>2008-09-11T20:01:45+00:00</updated>
        
        <author>
          <name>
            Ken
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://new-blog.incorrectness.dk/blog/posts/i-am-going-to-jaoo-2008-as-a-f-expert/"/>
        <id>https://new-blog.incorrectness.dk/blog/posts/i-am-going-to-jaoo-2008-as-a-f-expert/</id>
        
        <content type="html" xml:base="https://new-blog.incorrectness.dk/blog/posts/i-am-going-to-jaoo-2008-as-a-f-expert/">&lt;p&gt;Microsoft Denmark have invited me to participate in &lt;a rel=&quot;external&quot; href=&quot;http://jaoo.dk&quot;&gt;JAOO 2008&lt;/a&gt;. If I in return spend some time in the Microsoft stand demoing &lt;a rel=&quot;external&quot; href=&quot;http://fsharp.org/&quot;&gt;F#&lt;/a&gt; and answering questions about F# and functional programming in general.&lt;/p&gt;
&lt;p&gt;When &lt;a rel=&quot;external&quot; href=&quot;http://blogs.msdn.com/mesmann&quot;&gt;Martin Esmann&lt;/a&gt; (Microsoft Academic Developer Evangelist) approached me, I told him that I&#39;d be happy to show up, but I&#39;m not using F# from within Visual Studio. In fact, I&#39;m usually not even using Microsoft&#39;s .NET implementation. I&#39;m a happy &lt;a rel=&quot;external&quot; href=&quot;http://www.mono-project.com&quot;&gt;Mono&lt;/a&gt; user. To Martin&#39;s credit his initial reaction was that it would be cool that I demoed F# using Linux and Mono in the Microsoft stand.&lt;/p&gt;
&lt;p&gt;However, after talking it over with his colleges, Martin got back to me and told me that they thought that using Linux and Mono to demo F# would send to mixed a message. I agree with that. So unfortunately I won&#39;t be demoing Mono in a Microsoft stand this time. But I&#39;ll of course bring my own laptop, and might show F# on Mono and Linux if there are questions about portability. Maybe I should contact the Mono guys and ask for a T-shirt, so that I silently can invite questions about Mono ;-)&lt;/p&gt;
&lt;p&gt;Time to practice some F# demos.  Any suggestions for what would be effective? I&#39;m planning to show of the brand new Units of Measure and also sequence comprehensions.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Getting Ready for the ICFP 2008 Programming Contest</title>
        <published>2008-07-11T13:30:18+00:00</published>
        <updated>2008-07-11T13:30:18+00:00</updated>
        
        <author>
          <name>
            Ken
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://new-blog.incorrectness.dk/blog/posts/getting-ready-for-the-icfp-2008-programming-contest/"/>
        <id>https://new-blog.incorrectness.dk/blog/posts/getting-ready-for-the-icfp-2008-programming-contest/</id>
        
        <content type="html" xml:base="https://new-blog.incorrectness.dk/blog/posts/getting-ready-for-the-icfp-2008-programming-contest/">&lt;p&gt;I&#39;m getting ready to participate in &lt;a rel=&quot;external&quot; href=&quot;http://web.cecs.pdx.edu/~sheard/2008IcfpContest/&quot;&gt;the eleventh ICFP programming contest&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;So far everything works like a charm, KVM can run &lt;a rel=&quot;external&quot; href=&quot;http://web.cecs.pdx.edu/~sheard/2008IcfpContest/live-cd.html&quot;&gt;the LiveCD for the contest&lt;/a&gt; without a problem:
&lt;code&gt;kvm -cdrom ICFPCD15.iso &amp;amp;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;I hope I&#39;ll be able to spend more time on the contest compared to &lt;a href=&quot;https://new-blog.incorrectness.dk/blog/posts/icfp-contest-2006-team-kfl/&quot;&gt;last time I participated&lt;/a&gt;.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Decoding Morse Code With F# Comprehensions</title>
        <published>2007-11-09T11:31:30+00:00</published>
        <updated>2007-11-09T11:31:30+00:00</updated>
        
        <author>
          <name>
            Ken
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://new-blog.incorrectness.dk/blog/posts/decoding-morse-code-with-f-comprehensions/"/>
        <id>https://new-blog.incorrectness.dk/blog/posts/decoding-morse-code-with-f-comprehensions/</id>
        
        <content type="html" xml:base="https://new-blog.incorrectness.dk/blog/posts/decoding-morse-code-with-f-comprehensions/">&lt;p&gt;In my last post I showed how to &lt;a href=&quot;https://new-blog.incorrectness.dk/blog/posts/morse-code-decoding-with-python-list-comprehensions/&quot;&gt;decode morse code in Python using list comprehensions&lt;/a&gt;. In this post I show how to do it in &lt;a rel=&quot;external&quot; href=&quot;http://fsharp.org/&quot;&gt;F#&lt;/a&gt; instead.&lt;/p&gt;
&lt;p&gt;First using list comprehensions:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;let codes =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    [(&amp;quot;A&amp;quot;,&amp;quot;.-&amp;quot;);   (&amp;quot;B&amp;quot;,&amp;quot;-...&amp;quot;); (&amp;quot;C&amp;quot;,&amp;quot;-.-.&amp;quot;); (&amp;quot;D&amp;quot;,&amp;quot;-..&amp;quot;); (&amp;quot;E&amp;quot;,&amp;quot;.&amp;quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;     (&amp;quot;F&amp;quot;,&amp;quot;..-.&amp;quot;); (&amp;quot;G&amp;quot;,&amp;quot;--.&amp;quot;);  (&amp;quot;H&amp;quot;,&amp;quot;....&amp;quot;); (&amp;quot;I&amp;quot;,&amp;quot;..&amp;quot;);  (&amp;quot;J&amp;quot;,&amp;quot;.---&amp;quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;     (&amp;quot;K&amp;quot;,&amp;quot;-.-&amp;quot;);  (&amp;quot;L&amp;quot;,&amp;quot;.-..&amp;quot;); (&amp;quot;M&amp;quot;,&amp;quot;--&amp;quot;);   (&amp;quot;N&amp;quot;,&amp;quot;-.&amp;quot;);  (&amp;quot;O&amp;quot;,&amp;quot;---&amp;quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;     (&amp;quot;P&amp;quot;,&amp;quot;.--.&amp;quot;); (&amp;quot;Q&amp;quot;,&amp;quot;--.-&amp;quot;); (&amp;quot;R&amp;quot;,&amp;quot;.-.&amp;quot;);  (&amp;quot;S&amp;quot;,&amp;quot;...&amp;quot;); (&amp;quot;T&amp;quot;,&amp;quot;-&amp;quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;     (&amp;quot;U&amp;quot;,&amp;quot;..-&amp;quot;);  (&amp;quot;V&amp;quot;,&amp;quot;...-&amp;quot;); (&amp;quot;W&amp;quot;,&amp;quot;.--&amp;quot;);  (&amp;quot;X&amp;quot;,&amp;quot;-..-&amp;quot;);(&amp;quot;Y&amp;quot;,&amp;quot;-.--&amp;quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;     (&amp;quot;Z&amp;quot;,&amp;quot;--..&amp;quot;)]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;let rec decode input =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    if input = &amp;quot;&amp;quot; then [&amp;quot;&amp;quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    else [ for c, code in codes when input.StartsWith(code)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;           for rest in decode(input.Substring(String.length code))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;           -&amp;gt; c + rest ]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As it can be seen the code is almost identical to the Python code. Incidentally, I could not find a function equivalent to Python&#39;s &lt;code&gt;startswith&lt;/code&gt; method in the O&#39;Caml standard library (without using regular expressions). Fortunately F# came with one from the .NET library.&lt;/p&gt;
&lt;p&gt;Much to my the surprise the compiled F# (running on Mono 1.2.4) is 4 times slower that the Python code. I then rewrote the program to use sequence comprehensions:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;let rec decode input =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    if input = &amp;quot;&amp;quot; then { -&amp;gt; &amp;quot;&amp;quot; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    else { for c, code in codes when input.StartsWith(code)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;           for rest in decode(input.Substring(String.length code))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;           -&amp;gt; c + rest }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This version runs faster, and uses only a constant amount of memory. Still the Python version is three and half times faster.&lt;/p&gt;
&lt;p&gt;I then tried to run the programs on an other computer with Microsoft&#39;s .NET implementation. This improved the F# running times a lot. However, they are still 40% to 80% slower than the Python version.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://new-blog.incorrectness.dk/blog/wp-content/fs-vs-python-morse.png&quot; alt=&quot;Chart of F# vs Python on morse code decoding&quot; /&gt;&lt;/p&gt;
&lt;p&gt;My current guess is that Python is much better at handling strings than .NET.&lt;/p&gt;
&lt;p&gt;The actual numbers:&lt;/p&gt;
&lt;table border=&quot;1&quot; &gt;
&lt;tbody &gt;
&lt;tr &gt;
&lt;p&gt;Linux, Mono
Vista, MS .NET&lt;/p&gt;
&lt;/tr&gt;
&lt;tr &gt;
&lt;td &gt;Program
&lt;/td&gt;
Time
sec
Ratio
Time
sec
Ratio
&lt;/tr&gt;
&lt;tr &gt;
`morse.py`
&lt;td &gt;7.94 ± 0.05
&lt;/td&gt;
&lt;td &gt;1
&lt;/td&gt;
&lt;td &gt;11.03 ± 0.03
&lt;/td&gt;
&lt;td &gt;1
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr &gt;
`morse.fs`
&lt;td &gt;33.07 ± 0.19
&lt;/td&gt;
&lt;td &gt;4.17
&lt;/td&gt;
&lt;td &gt;19.65 ± 0.33
&lt;/td&gt;
&lt;td &gt;1.78
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr &gt;
`morse-seq.fs`
&lt;td &gt;28.1 ± 0.59
&lt;/td&gt;
&lt;td &gt;3.54
&lt;/td&gt;
&lt;td &gt;16.12 ± 0.36
&lt;/td&gt;
&lt;td &gt;1.46
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
**Update 2007-11-12 Added test files:**
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Python code: &lt;a rel=&quot;external&quot; href=&quot;https://new-blog.incorrectness.dk/blog/downloads/morse-py.txt&quot;&gt;&lt;code&gt;morse.py&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;F# code using list comprehensions: &lt;a rel=&quot;external&quot; href=&quot;https://new-blog.incorrectness.dk/blog/downloads/morse.fs&quot;&gt;&lt;code&gt;morse.fs&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;F# code using sequence comprehensions: &lt;a rel=&quot;external&quot; href=&quot;https://new-blog.incorrectness.dk/blog/downloads/morse-seq.fs&quot;&gt;&lt;code&gt;morse-seq.fs&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Morse Code Decoding With Python List Comprehensions</title>
        <published>2007-09-19T20:16:44+00:00</published>
        <updated>2007-09-19T20:16:44+00:00</updated>
        
        <author>
          <name>
            Ken
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://new-blog.incorrectness.dk/blog/posts/morse-code-decoding-with-python-list-comprehensions/"/>
        <id>https://new-blog.incorrectness.dk/blog/posts/morse-code-decoding-with-python-list-comprehensions/</id>
        
        <content type="html" xml:base="https://new-blog.incorrectness.dk/blog/posts/morse-code-decoding-with-python-list-comprehensions/">&lt;p&gt;As a small exercise for getting up to speed with &lt;a rel=&quot;external&quot; href=&quot;http://python.org&quot;&gt;Python&lt;/a&gt; I decided to solve &lt;a rel=&quot;external&quot; href=&quot;http://www.rubyquiz.com/quiz121.html&quot;&gt;ruby quiz #121&lt;/a&gt;, which is to to write a function that finds all possible decodings of a string of &lt;a rel=&quot;external&quot; href=&quot;http://en.wikipedia.org/wiki/Morse_code&quot;&gt;Morse codes&lt;/a&gt; without letter- and word-separators.  Given the nature of the problem I decided to use python&#39;s list comprehensions for the solution.&lt;/p&gt;
&lt;p&gt;Without further ado here is the code I ended up with:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#!/usr/bin/env python&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;import string&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;letters = [(&amp;#39;A&amp;#39;,&amp;quot;.-&amp;quot;),   (&amp;#39;B&amp;#39;,&amp;quot;-...&amp;quot;), (&amp;#39;C&amp;#39;,&amp;quot;-.-.&amp;quot;), (&amp;#39;D&amp;#39;,&amp;quot;-..&amp;quot;), (&amp;#39;E&amp;#39;,&amp;quot;.&amp;quot;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;           (&amp;#39;F&amp;#39;,&amp;quot;..-.&amp;quot;), (&amp;#39;G&amp;#39;,&amp;quot;--.&amp;quot;),  (&amp;#39;H&amp;#39;,&amp;quot;....&amp;quot;), (&amp;#39;I&amp;#39;,&amp;quot;..&amp;quot;),  (&amp;#39;J&amp;#39;,&amp;quot;.---&amp;quot;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;           (&amp;#39;K&amp;#39;,&amp;quot;-.-&amp;quot;),  (&amp;#39;L&amp;#39;,&amp;quot;.-..&amp;quot;), (&amp;#39;M&amp;#39;,&amp;quot;--&amp;quot;),   (&amp;#39;N&amp;#39;,&amp;quot;-.&amp;quot;),  (&amp;#39;O&amp;#39;,&amp;quot;---&amp;quot;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;           (&amp;#39;P&amp;#39;,&amp;quot;.--.&amp;quot;), (&amp;#39;Q&amp;#39;,&amp;quot;--.-&amp;quot;), (&amp;#39;R&amp;#39;,&amp;quot;.-.&amp;quot;),  (&amp;#39;S&amp;#39;,&amp;quot;...&amp;quot;), (&amp;#39;T&amp;#39;,&amp;quot;-&amp;quot;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;           (&amp;#39;U&amp;#39;,&amp;quot;..-&amp;quot;),  (&amp;#39;V&amp;#39;,&amp;quot;...-&amp;quot;), (&amp;#39;W&amp;#39;,&amp;quot;.--&amp;quot;),  (&amp;#39;X&amp;#39;,&amp;quot;-..-&amp;quot;),(&amp;#39;Y&amp;#39;,&amp;quot;-.--&amp;quot;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;           (&amp;#39;Z&amp;#39;,&amp;quot;--..&amp;quot;)]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;def decode(input):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    if input == &amp;quot;&amp;quot; :&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        return [&amp;quot;&amp;quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    else:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        return [ letter + remaining&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                 for letter, code in letters if input.startswith(code)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                 for remaining in decode(input[len(code):]) ]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;# Some Testing code&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;def test(s, code):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    if s in decode(code):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        print code + &amp;quot; can be decoded as &amp;quot; + s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    else:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        print code + &amp;quot; can NOT be decoded as &amp;quot; + s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;test(&amp;quot;SOFIA&amp;quot;, &amp;quot;...---..-....-&amp;quot;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;test(&amp;quot;SOPHIA&amp;quot;, &amp;quot;...---..-....-&amp;quot;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;test(&amp;quot;EUGENIA&amp;quot;, &amp;quot;...---..-....-&amp;quot;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Interesting, my solution is rather similar to &lt;a rel=&quot;external&quot; href=&quot;http://patricklogan.blogspot.com/2007/09/list-comprehensions.html&quot;&gt;Patrick Logan&#39;s Erlang solution&lt;/a&gt;. And I find it simpler to understand than the &lt;a rel=&quot;external&quot; href=&quot;http://www.haskell.org/haskellwiki/Haskell_Quiz/Morse_Code&quot;&gt;Haskell solutions at the HaskellWiki.&lt;/a&gt;&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Recursive Descent Parsers in C#</title>
        <published>2006-09-08T20:46:16+00:00</published>
        <updated>2006-09-08T20:46:16+00:00</updated>
        
        <author>
          <name>
            Ken
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://new-blog.incorrectness.dk/blog/posts/recursive-descent-parsers-in-c/"/>
        <id>https://new-blog.incorrectness.dk/blog/posts/recursive-descent-parsers-in-c/</id>
        
        <content type="html" xml:base="https://new-blog.incorrectness.dk/blog/posts/recursive-descent-parsers-in-c/">&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;http://www.dina.kvl.dk/~sestoft&quot;&gt;Peter Sestoft&lt;/a&gt; and I have written a &lt;a rel=&quot;external&quot; href=&quot;http://www.itu.dk/people/kfl/parsernotes.pdf&quot;&gt;note about how to write scanners and parsers in C#&lt;/a&gt;. The note is based on earlier versions for SML and Java.&lt;/p&gt;
&lt;p&gt;The note contains an thorough introduction to grammars on Backus--Naur form (BNF).  This includes a description of properties your grammar should have so that it can be mechanically translated to a program.  And also some prescriptions about how to transform your grammar so that it has the desired properties.  In technical terms, the note describe how you can check that your grammar is an LL(1) grammar, and if your grammar is not an LL(1) grammar, we give your some tricks that will usually transform the grammar into an LL(1) grammar.&lt;/p&gt;
&lt;p&gt;The parsers you write using our method are recursive descent parsers.  For the scanners, however, we just use an add-hoc method.  Both parsers and scanners makes good use of the .NET framework.  For instance, the scanners creates a token stream from a &lt;code&gt;TextReader&lt;/code&gt;.  Hence, the scanners can be used to scan both files and strings.  Likewise, a token stream is represented as &lt;code&gt;IEnumerable&amp;lt;Token&amp;gt;&lt;/code&gt; and scanners uses &lt;code&gt;yield&lt;/code&gt; to create this token stream.  Thus, creating the token stream lazily.&lt;/p&gt;
&lt;p&gt;To give an example, the simplest scanner presented in the note is the following scanner:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;using TokenStream = System.Collections.Generic.IEnumerator&amp;lt;Token&amp;gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;class ZeroOneScan : IScanner {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  public TokenStream Scan(TextReader reader) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    while ( reader.Peek() != -1 ) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      if ( Char.IsWhiteSpace((char) reader.Peek()) )&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        reader.Read();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      else&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        switch(reader.Read()) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        case &amp;#39;-&amp;#39;: yield return Token.FromKind(Kind.SUB); break;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        case &amp;#39;0&amp;#39;: yield return Token.FromKind(Kind.ZERO); break;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        case &amp;#39;1&amp;#39;: yield return Token.FromKind(Kind.ONE); break;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        default: throw new ApplicationException(&amp;quot;Illegal character&amp;quot;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    yield return Token.FromKind(Kind.EOF);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I find this use of &lt;code&gt;yield&lt;/code&gt; quite elegant.&lt;/p&gt;
&lt;p&gt;Working with this note and getting my name on it has special meaning to me.  A precursor note for SML was actually the note Peter used when he taught me for the first time many years ago (on my second semester at university). Over the years, I have returned to the note many times when I have needed to parse a small language that did not warrant the use of a parser generator, or when a generated parser would have been inconvenient to use because the text to be scanned did not come from a file stream (modern parser generators will not generate parsers with this problem).&lt;/p&gt;
&lt;p&gt;The note ends a bit to early, in my opinion.  I would like extend the note to cover Extended BNF.  And I would also like to cover parser combinators.  Well, one day when time permits...&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>ICFP Contest 2006, Team KFL</title>
        <published>2006-07-27T11:37:39+00:00</published>
        <updated>2006-07-27T11:37:39+00:00</updated>
        
        <author>
          <name>
            Ken
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://new-blog.incorrectness.dk/blog/posts/icfp-contest-2006-team-kfl/"/>
        <id>https://new-blog.incorrectness.dk/blog/posts/icfp-contest-2006-team-kfl/</id>
        
        <content type="html" xml:base="https://new-blog.incorrectness.dk/blog/posts/icfp-contest-2006-team-kfl/">&lt;blockquote&gt;_In 1967, during excavation for the construction of a new shopping center in Monroeville, Pennsylvania, workers uncovered a vault containing a cache of ancient scrolls. Most were severely damaged, but those that could be recovered confirmed the existence of a secret society long suspected to have been active in the region around the year 200 BC._
&lt;p&gt;_Based on a translation of these documents, we now know that the society, the Cult of the Bound Variable, was devoted to the careful study of computation, over two millennia before the invention of the digital computer.
&lt;a rel=&quot;external&quot; href=&quot;http://www.boundvariable.org/task.shtml&quot;&gt;...&lt;/a&gt;
_&lt;/blockquote&gt;&lt;/p&gt;
&lt;p&gt;Like &lt;a href=&quot;https://new-blog.incorrectness.dk/blog/posts/team-what-a-summer-party-round-1/&quot;&gt;last year&lt;/a&gt; the prospects for my participation in the &lt;a rel=&quot;external&quot; href=&quot;http://www.boundvariable.org&quot;&gt;ICFP Contest&lt;/a&gt; was not looking good. None of my team mates from last year&#39;s team seemed to be able to participate, and neither did I myself. The weekend of the contest was packed with family business. And on top of that, when the weekend arrived I was sick Friday night and Saturday.&lt;/p&gt;
&lt;p&gt;However, Sunday evening I had some free time and I decided that I would take a crack a the contest just to see what it was about. Judging from the discussion mailing-list it sounded quite fun and interesting. The first phase of the contest task was to implement a 14-instruction virtual machine called UM and when that was running you should use it for running the provided codex for the operating system UMIX.&lt;/p&gt;
&lt;p&gt;So I registered my team KFL and started to implement my UM in SML. The first thing I did was to implement an instruction decoder that could translate a 32-bit word into an SML datatype. Then I wrote a function that read in a file of 32-bit words encoded in big-endian as four 8-bit words each. And then &lt;code&gt;map&lt;/code&gt;ed my decode function over the Vector of words. For this task the SML Basis Library really shined:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;fun readFile filename =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    let val dev = BinIO.openIn filename&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        val all = BinIO.inputAll dev before BinIO.closeIn dev&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        val words = Vector.tabulate(Word8Vector.length all div 4,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                                    fn i =&amp;gt; Word32.fromLarge(PackWord32Big.subVec(all,i)))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    in  Vector.map decode words&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Time spend: 1 hour.&lt;/p&gt;
&lt;p&gt;Unfortunately, this did not work. My decoding function failed after 1675 instructions or so, complaining about illegal instructions. And indeed the 32-bit word it complained about did not seem to encode a legal instruction. I tried to reimplement the conversion from 8-bit words to 32-bit words, in case &lt;code&gt;PackWord32Big&lt;/code&gt; worked different than I thought. But I still got the same error. Thus, I gave up and went to bed.&lt;/p&gt;
&lt;p&gt;Time spend: 2 hours.&lt;/p&gt;
&lt;p&gt;Monday morning I had to see to some other things first, but then I had some time to spend on the contest. Even after I had slept on the problem I still couldn&#39;t figure out what was wrong. So I asked my colleague &lt;a rel=&quot;external&quot; href=&quot;http://itu.dk/people/panic&quot;&gt;Arne&lt;/a&gt; if he had 10 minutes to help me debug my program. I explained him the problem, showed him my code (actually my debug output, and then we looked at the codex in a hex-editor. He confirmed that based on my explanation, my program appeared to be working correctly, and it looked as if there was an illegal instruction in the codex, if all instructions really was encoded a single 32-word. Hence, one or more of my assumptions had to wrong (it was easy to rule out that the codex was wrong, because more than a hundred teams were able to run the codex). Then it occurred to me, the codex was not required to &lt;em&gt;only&lt;/em&gt; contain valid instructions, maybe the code would jump over damaged parts of the codex and part of the contest would be to repair the codex. Thus, I changed my code to only decode instructions on demand, and kept the whole program as an array of 32-bit words. Lo and behold the machine was able to start running the codex! However it failed in the self-check the codex performed. After some debugging I found one place where I used the name of an register (registers in the UM are named by integers) as a value rather than using the value contained in the register. Now my UM was able to run the codex and the SANDmark (a debug and benchmark suite provided by the contest managers).&lt;/p&gt;
&lt;p&gt;Time spend: 2 hours.&lt;/p&gt;
&lt;p&gt;My first version ran the SANDmark in a bit more than 18 minutes (14 min user and 4 min sys) , 768 seconds user time according to MLton&#39;s profiler. Which was not to bad but I&#39;d seen on the discussion list, that other participants had UMs that ran the SANDmark in a couple of minutes. Thus, I decided to profile my UM to see where the time was spend. To my surprise the top function in the profile was my decode function, a function that took a 32-bit word and translates it to an SML datatype. Here are the first few lines of &lt;code&gt;decode&lt;/code&gt; together with the helper function &lt;code&gt;standardRegs&lt;/code&gt; that fetches out the register names:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;fun standardRegs w =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    let open Word32&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        val A = (w &amp;lt;&amp;lt; 0w23) &amp;gt;&amp;gt; 0w29&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        val B = (w &amp;lt;&amp;lt; 0w26) &amp;gt;&amp;gt; 0w29&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        val C = andb(w, 0w7)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    in (toInt A, toInt B, toInt C)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    end&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;fun decode w =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    let open Word32&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        val opr = w &amp;gt;&amp;gt; 0w28&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    in case opr of&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;           0w0  =&amp;gt; CMove(standardRegs w)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;         | 0w1  =&amp;gt; ARead(standardRegs w)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;         | 0w2  =&amp;gt; AWrite(standardRegs w)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;         ...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And the top of my interpreter loop looked like this:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      while true do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;             case spin() of&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                 CMove(A,B,C) =&amp;gt; if $C = 0w0 then ()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                                 else A &amp;lt; - $B&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;               | ARead(A,B,C) =&amp;gt; A &amp;lt; - $$B sub (W32.toInt($C))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;               | AWrite(A,B,C) =&amp;gt; Array.update($$A, W32.toInt($B), $C)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;               ...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Where &lt;code&gt;spin&lt;/code&gt; is the function that reads the current word at the program counter, updates the program counter, decodes the word, and return the instruction. But how could 19% of the time be spend in the &lt;code&gt;decode&lt;/code&gt;. I moved the call to decode from spin to my interpreter loop to aid the MLton optimizers:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      while true do&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;             case decode(spin()) of&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This made the SANDmark 5 minutes faster wall clock time, that is 13 minutes. Or in MLton profiler time 529 seconds. 30% improvement just for moving a function around. Not bad.&lt;/p&gt;
&lt;p&gt;Time spend: 30 minutes.&lt;/p&gt;
&lt;p&gt;After this optimization my UM was fast enough that I thought I&#39;d try to solve some of the puzzles. So I logged into the UMIX OS using the guest account and started to poke around and collect points. The first real puzzle was to fix a password cracker written in a weird Basic dialect that used roman numerals instead of decimal notation for integer literals (including for the line numbers).&lt;/p&gt;
&lt;p&gt;Time spend: 1½ hour. Collected points 230.&lt;/p&gt;
&lt;p&gt;Then I had to go home, and while I cooked dinner (I was baking pita bread, and while the dough was rising I had time to hack) I was able to write an improved password cracker---in this weird roman numerals Basic: &lt;a rel=&quot;external&quot; href=&quot;https://new-blog.incorrectness.dk/blog/images/hack2.bas&quot;&gt;hack2.bas&lt;/a&gt;. This gained me an other 100 points, just before the contest ended (the contest ended at 18:00 in &lt;a rel=&quot;external&quot; href=&quot;http://www.timeanddate.com/library/abbreviations/timezones/eu/cest.html&quot;&gt;CEST&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;Time spend 45 min. Collected points in total 330.&lt;/p&gt;
&lt;p&gt;All in all not bad to make 330 points after spending only seven hours and 45 minutes of rather fragmented time.&lt;/p&gt;
&lt;p&gt;After dinner I was able to gain an other 35 points by writing a list reversal program in a graphical 2D language: &lt;a rel=&quot;external&quot; href=&quot;https://new-blog.incorrectness.dk/blog/images/rev.2d&quot;&gt;rev.2d&lt;/a&gt;. It took half an hour or so.&lt;/p&gt;
&lt;p&gt;The setup for the contest was absolutely amazing and most entertaining. My account of it here does not do it justice. An incredible amount of work must have gone into the preparation of it. I&#39;m looking forward for the final debriefing from the Contest Organizers.&lt;/p&gt;
&lt;p&gt;Yesterday, I tried for fun to optimize my UM program a bit more. Programs running on the UM are able to allocate and free arrays. In my original implementation I used a ref to a functional Red-Black tree to keep track of the mapping from UM-pointers to arrays. I know, not the best choice of data structure, but I was just trying to get a &quot;good enough&quot; UM up and running. From the profile it was obvious that lots of time and memory was spend on keeping the Red-Black trees balanced. Thus, I replaced this code with an array, and a free-list for reusing UM-pointers (32-bit words). Thus, my code for managing the &quot;heap&quot; when from 12 lines of code (not counting the code in the Red-Black tree library) to 28 lines of code. This small changed made the SANDmark run in 4 minutes wall clock(175 seconds of MLton profiler time) an improvement of almost 67%. Looking at the profile, I could see that &lt;code&gt;decode&lt;/code&gt; was again on top of the list (using 42% of the time). Thus, I decided to inline &lt;code&gt;decode&lt;/code&gt; and deforest the instruction datatype by hand. This made my code 68 lines smaller, and the SANDmark ran in 2.50 minutes (134 seconds of MLton profiler time), 23% improvement. Almost four times faster than the UM I participated in the contest with. Time spend 1½ hour.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Code for the UM I participated with: &lt;a rel=&quot;external&quot; href=&quot;https://new-blog.incorrectness.dk/blog/images/um2.sml&quot;&gt;um2.sml&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Code with improve heap handling: &lt;a rel=&quot;external&quot; href=&quot;https://new-blog.incorrectness.dk/blog/images/um3.sml&quot;&gt;um3.sml&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Code with inlined decode function: &lt;a rel=&quot;external&quot; href=&quot;https://new-blog.incorrectness.dk/blog/images/um4.sml&quot;&gt;um4.sml&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Theory of evolution</title>
        <published>2006-01-05T16:15:03+00:00</published>
        <updated>2006-01-05T16:15:03+00:00</updated>
        
        <author>
          <name>
            Ken
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://new-blog.incorrectness.dk/blog/posts/theory-of-evolution/"/>
        <id>https://new-blog.incorrectness.dk/blog/posts/theory-of-evolution/</id>
        
        <content type="html" xml:base="https://new-blog.incorrectness.dk/blog/posts/theory-of-evolution/">&lt;p&gt;&lt;em&gt;There is no theory of evolution. Just a list of creatures Chuck Norris has allowed to live.&lt;/em&gt;&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Refactoring SML Quiz, Part 2</title>
        <published>2005-12-21T21:41:43+00:00</published>
        <updated>2005-12-21T21:41:43+00:00</updated>
        
        <author>
          <name>
            Ken
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://new-blog.incorrectness.dk/blog/posts/refactoring-sml-quiz-part-2/"/>
        <id>https://new-blog.incorrectness.dk/blog/posts/refactoring-sml-quiz-part-2/</id>
        
        <content type="html" xml:base="https://new-blog.incorrectness.dk/blog/posts/refactoring-sml-quiz-part-2/">&lt;p&gt;The answer to yesterdays quiz is: Yes, types are necessary for lambda-lifting refactoring. Namely, if the lifted function contains an overloaded operator such as, e.g., &lt;code&gt;+&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;For example, given the program:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;fun foo x =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    let fun add y = x + y&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    in  add 5.0 end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;where we want to lift the function &lt;code&gt;add&lt;/code&gt;.  Our first attempt might be to transform the program into something like (refactoring is a source code to source code transformation):&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;fun add x y = x + y&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;fun foo x = add x 5.0&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;However, this might not compile with every SML compiler.  It depends on how much local context the compiler uses to resolve overloading.&lt;/p&gt;
&lt;p&gt;Moscow ML compiles the transformed program above just fine.  But Moscow ML complains about the following version:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;fun add x y = x + y&amp;lt;strong&amp;gt;;&amp;lt;/strong&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;fun foo x = add x 5.0&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Notice the extra semi-colon after the declaration of &lt;code&gt;add&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Update 2005-12-22:&lt;/strong&gt;
Just for good messure.  Here is one possible result of refactoring with necessary type information:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;fun add (x : real) y = x + y&amp;lt;strong&amp;gt;;&amp;lt;/strong&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;fun foo x = add x 5.0&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I believe that only captured variables needs to be typed.&lt;/p&gt;
&lt;p&gt;Also note, that overloading is not the only problem.  It is possible to construct a similar example using records, but I&#39;ll leave that to the reader (unless there is a popular and desperate demand, then I can provide an example).&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Refactoring SML Quiz, Part 1</title>
        <published>2005-12-20T21:55:26+00:00</published>
        <updated>2005-12-20T21:55:26+00:00</updated>
        
        <author>
          <name>
            Ken
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://new-blog.incorrectness.dk/blog/posts/refactoring-sml-quiz/"/>
        <id>https://new-blog.incorrectness.dk/blog/posts/refactoring-sml-quiz/</id>
        
        <content type="html" xml:base="https://new-blog.incorrectness.dk/blog/posts/refactoring-sml-quiz/">&lt;p&gt;Yesterday, I discussed with some students who are implementing an SML plug-in for eclipse, whether types are necessary for a lambda-lifting refactoring for SML.&lt;/p&gt;
&lt;p&gt;So today&#39;s quiz is simply: Are types necessary for lambda-lifting refactoring in SML? Why/Why not?&lt;/p&gt;
&lt;p&gt;Remember, the refactoring works on valid SML programs, and after the transformation the program should still be valid.&lt;/p&gt;
&lt;p&gt;Example:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;fun foo x =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    let fun bar y = (x,y)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    in bar x end&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;is transformed/refactored to&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;fun bar x y = (x,y)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;fun foo x = bar x x&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I&#39;ll give the answer tomorrow.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Implementing the generic IEnumerable interface</title>
        <published>2005-07-25T13:05:46+00:00</published>
        <updated>2005-07-25T13:05:46+00:00</updated>
        
        <author>
          <name>
            Ken
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://new-blog.incorrectness.dk/blog/posts/implementing-the-generic-ienumerable-interface/"/>
        <id>https://new-blog.incorrectness.dk/blog/posts/implementing-the-generic-ienumerable-interface/</id>
        
        <content type="html" xml:base="https://new-blog.incorrectness.dk/blog/posts/implementing-the-generic-ienumerable-interface/">&lt;p&gt;Say you want to implement a class that implements the &lt;code&gt;IEnumerable&lt;/code&gt; interface in C#.  Then you have two choices, either to implement the old-style non-generic &lt;code&gt;IEnumerable&lt;/code&gt; interface or you can implements the generic &lt;code&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/code&gt; interface.  Given those choices we of course want to implement the new generic version of the interface.  Because otherwise lots of boxing and unboxing will happen when &lt;code&gt;T&lt;/code&gt; is a value type, with the non-generic version you will not be able to conveniently use the &lt;code&gt;Current&lt;/code&gt; property of your enumerator, and for general type-safety goodness.&lt;/p&gt;
&lt;p&gt;Thus, you set out to implement the generic version.  For example, let say we want to implement a class that enumerates all the integers staring from a given offset.  First we might try this:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;using System.Collections.Generic;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;class Ints : IEnumerable&amp;lt;int&amp;gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    private readonly int offset;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    public Ints(int o) { offset = o; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    public IEnumerator&amp;lt;int&amp;gt; GetEnumerator() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        int i = offset;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        while( true ) yield return i++;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But then the compiler complains:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;error CS0535: &amp;#39;Ints&amp;#39; does not implement interface member &amp;#39;System.Collections.IEnumerable.GetEnumerator()&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Thank you for letting us know nice compiler.  But we don&#39;t want &lt;code&gt;Ints&lt;/code&gt; to implement the non-generic interface.  We want it to implement the generic interface.&lt;/p&gt;
&lt;p&gt;Reading up on the documentation will reveal that the generic interface inherits from the non-generic interface. What a wonderful design.  Thankfully we can make a general work-around for this design flaw in the library.  Just add a non-generic method that calls the generic method:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Once the author of &lt;a rel=&quot;external&quot; href=&quot;http://www.dina.kvl.dk/~sestoft/csharpprecisely/&quot;&gt;my favorite C# book&lt;/a&gt; returns from vacation. I&#39;ll complain to him that the section about &lt;code&gt;IEnumerable&lt;/code&gt; didn&#39;t make it clear that if you want to implement the generic interface you also have to implement the non-generic interface.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; It turns out that &lt;a rel=&quot;external&quot; href=&quot;http://www.dina.kvl.dk/~sestoft/csharpprecisely/errata.html&quot;&gt;the errata&lt;/a&gt; to &lt;a rel=&quot;external&quot; href=&quot;http://www.dina.kvl.dk/~sestoft/csharpprecisely&quot;&gt;C# Precisely&lt;/a&gt; already mentions this curriousity.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Team “What A Summer Party”, Round 1</title>
        <published>2005-06-27T20:25:46+00:00</published>
        <updated>2005-06-27T20:25:46+00:00</updated>
        
        <author>
          <name>
            Ken
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://new-blog.incorrectness.dk/blog/posts/team-what-a-summer-party-round-1/"/>
        <id>https://new-blog.incorrectness.dk/blog/posts/team-what-a-summer-party-round-1/</id>
        
        <content type="html" xml:base="https://new-blog.incorrectness.dk/blog/posts/team-what-a-summer-party-round-1/">&lt;p&gt;I have hangovers.&lt;/p&gt;
&lt;p&gt;Not from partying, but from lack of sleep and bad nutrition.  The contest was lots of fun.  Despite that the end-result for our team was rather disappointing.  Below the description I wrote for our team.&lt;/p&gt;
&lt;blockquote&gt;**The Story Of Team &quot;What A Summer Party&quot;**
&lt;p&gt;&lt;strong&gt;Round 1&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The prospect for the ICFP contest was looking bleak.  The net was down and the annual faculty summer party started an hour before the contest.  Thus, Ken, Martin, and Henning went to the party and soon forgot all about the contest.  The BBQ was sizzling hot and the beers were cold, life was good.&lt;/p&gt;
&lt;p&gt;Then suddenly Carsten crashed the party and dumped a huge pile of print-outs with the frightening title &quot;Cops &amp;amp; Robbers, The ICFP 2005 Programming Contest Task&quot;.  From that moment the easy life stopped. We started to gulp down huge amounts of candy, chips, and various beverages while we discussed strategies and suggestions for implementation details.  Henning had tried to refuse to participate on the team, giving hollow-sounding excuses like: 3-week old baby, twins with ear infections, and exam preparation.  But he was sucked in by the exciting discussion, and after a frantic programming session where he tried to hammer out a parser in zero time, his laptop lost the keys &quot;P&quot; and &quot;L&quot;.  The rest of us piled useful library modules into the repository.&lt;/p&gt;
&lt;p&gt;Saturday, none of us spend much time on the contest.  Instead, we spend some quality time with the family, travelled to the other end of the country to party with old friends from high school, and attended unavoidable social obligations.  Henning, against all expectations, made a surprise virtual appearance and offered an almost working parser, this gave him a seat on the team, despite that it was against his wishes.&lt;/p&gt;
&lt;p&gt;Sunday, we realised that we didn&#39;t have a working cop nor a working robber.  We worked hard and was able to make our first submission with working (but non-moving) cop and robber.  And a few hours after the submission the first working robber appeared.  This robber was looting the banks like crazy (easy because the cops still didn&#39;t move) and there was much rejoicing.&lt;/p&gt;
&lt;p&gt;Monday, we just panicked.  We managed to put all the pieces for the cop together, and made the final submission just two minutes after deadline (which according the contest mailing list should be acceptable).  However, there was no end to the disappointment when we realised that the fancy cop we had assembled in the last minute still didn&#39;t move.  After half an hour we had a stupid cop who just walk from bank to bank (making illegal moves along the way, but as we have learned from the movies that is OK for a cop to do, as long as he has good intentions).  This, rather dense, cop still managed to catch our super robber.  As I said, there was no end to the disappointment.&lt;/p&gt;
&lt;p&gt;It is a good thing that there is a round two in two weeks....&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The Team consists of the follow persons:&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;Ken Friis Larsen (Release Manager and Storyteller)&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;Martin Elsman (Protector of Good and Right)&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;Carsten Varming (Evil Mastermind)&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;Henning Niss (Cheerleader and Parser constructor)&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;/blockquote&gt;
&lt;p&gt;Thanks for a fun weekend guys.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Cops and Robbers</title>
        <published>2005-06-25T09:11:06+00:00</published>
        <updated>2005-06-25T09:11:06+00:00</updated>
        
        <author>
          <name>
            Ken
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://new-blog.incorrectness.dk/blog/posts/cops-and-robbers/"/>
        <id>https://new-blog.incorrectness.dk/blog/posts/cops-and-robbers/</id>
        
        <content type="html" xml:base="https://new-blog.incorrectness.dk/blog/posts/cops-and-robbers/">&lt;p&gt;Live update from the life of Ken. I&#39;m currently participating in the &lt;a rel=&quot;external&quot; href=&quot;http://icfpc.plt-scheme.org&quot;&gt;ICFP 2005 Contest&lt;/a&gt; on a team of colleagues from the &lt;a rel=&quot;external&quot; href=&quot;http://www.itu.dk&quot;&gt;IT University&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The task in the contest is to make robot brains for cop robots and robber robots.  We have a bunch of wild ideas, but it is by no means clear (to me) if we&#39;ll be able to make a competitive entry.&lt;/p&gt;
&lt;p&gt;More later...&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Get Perpendicular</title>
        <published>2005-04-15T19:43:52+00:00</published>
        <updated>2005-04-15T19:43:52+00:00</updated>
        
        <author>
          <name>
            Ken
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://new-blog.incorrectness.dk/blog/posts/get-perpendicular/"/>
        <id>https://new-blog.incorrectness.dk/blog/posts/get-perpendicular/</id>
        
        <content type="html" xml:base="https://new-blog.incorrectness.dk/blog/posts/get-perpendicular/">&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;http://www.hitachigst.com/hdd/research/recording_head/pr/PerpendicularAnimation.html&quot;&gt;Wow&lt;/a&gt;!&lt;/p&gt;
&lt;p&gt;Next time I have to explain somthing technical I want to do something like that.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Review: The Inmates are  Running the Asylum</title>
        <published>2005-03-23T21:28:06+00:00</published>
        <updated>2005-03-23T21:28:06+00:00</updated>
        
        <author>
          <name>
            Ken
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://new-blog.incorrectness.dk/blog/posts/review-the-inmates-are-running-the-asylum/"/>
        <id>https://new-blog.incorrectness.dk/blog/posts/review-the-inmates-are-running-the-asylum/</id>
        
        <content type="html" xml:base="https://new-blog.incorrectness.dk/blog/posts/review-the-inmates-are-running-the-asylum/">&lt;p&gt;I have seen Alan Cooper&#39;s book &lt;a rel=&quot;external&quot; href=&quot;http://www.cooper.com/content/insights/cooper_books.asp#TIARTA&quot;&gt;The Inmates Are Running The Asylum&lt;/a&gt; recommended many places and I finally got around to read it myself.&lt;/p&gt;
&lt;p&gt;Overall I liked the book.  It is not too long and Alan Cooper is a really good and entertaining writer.  However, Alan Cooper spends almost half the book ranting over programmers at large and how bad programmers are at designing user interfaces.  But if you are able to take the excessive ranting as entertaining in the style of old-school stand-up comedy (at least, that is how I took it) then Alan Cooper offers some good insights.&lt;/p&gt;
&lt;p&gt;The main insights I took with me from the book are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Design Matters&lt;/strong&gt; Design is one of the pillars of what make users (customers) lust for you product and makes them super-loyal.  Design does not relate solely to visual design but also interaction design.  The prime example of a master of design is Apple.  An other example is Palm PDAs, for years they were technically behind the Windows CE based PDAs both in hardware and software capabilities.  Still, the Palms were able to sit on 70% of the market or so.  Mainly because the interaction design on the Palms was much better designed. (I don&#39;t know the current status of the PDA market, as I no longer use a PDA.)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Homo Logicus&lt;/strong&gt; Programmers are different from most &quot;normal&quot; people, so different that they are a different species than  &lt;em&gt;Homo sapiens&lt;/em&gt;.  One of the main traits of Homo Logicus is that they wants control, and accepts complexity as trade-off, whereas Homo sapiens wants simplicity, and accepts less control as a trade-off.  I think that this is important to remember as a Homo logicus when you make programs for Homo sapiens.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Goal-Directed Design and Personas&lt;/strong&gt; For me, the most useful part of the book is that Alan Cooper describe a &lt;a rel=&quot;external&quot; href=&quot;http://www.cooper.com/content/why_cooper/the_process.asp&quot;&gt;method&lt;/a&gt; for making good interaction design.  Part of this method is that you should define a set of Personas who are the ones you design the program for, usually there will be just one main persona for which you optimise your design. &lt;em&gt;Personas&lt;/em&gt; are fictive persons often precisely described: age, gender, profession, favourite ice-cream flavor, etc.. Personas makes perfect sense to me.  Just as most (non-fiction?) writers write to a specific person to make the writing process easier and to write to a specific person also gives better results. The goal-directed part means that the design should take offset in the Personas&#39; goals, not in a specific process or task.  Again, this makes perfect sense.  To start with a persona&#39;s goal can lead to untraditional and better solutions.  Likewise, to be goal-directed also makes it easier to make judgement call about what functionality should be (easily) available in the program.  So that it is the functionality that the user needs to solve her goal which is available and not a dog&#39;s breakfast of all the functionality in the program.  Remember the Homo sapiens are in majority.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;The Elastic User&lt;/strong&gt; If you don&#39;t have Personas then it extremely easy as a programmer to make &quot;the user&quot; elastic.  This means, that sometimes you&#39;ll take extra care and make a context sensitive help systems with carefully written pedagogical texts, for instance, because otherwise &quot;the user&quot; can get lost and confused.  Later you decide that &quot;the user&quot; will want to be able to customise the colours and fonts of each individual UI element.  The reason for the elasticity of &quot;the user&quot; is that when you don&#39;t design for a specific persona,  &quot;the user&quot; have to play the role as many different groups of users, and it is easy to lose track of who you are designing for.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For me these are the main insights (that I can remember now), but there are lots of more good stuff in the book and I can wholehearted recommend it.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>GADTs in C++</title>
        <published>2005-03-03T21:18:43+00:00</published>
        <updated>2005-03-03T21:18:43+00:00</updated>
        
        <author>
          <name>
            Ken
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://new-blog.incorrectness.dk/blog/posts/gadt-in-c/"/>
        <id>https://new-blog.incorrectness.dk/blog/posts/gadt-in-c/</id>
        
        <content type="html" xml:base="https://new-blog.incorrectness.dk/blog/posts/gadt-in-c/">&lt;p&gt;My good friends &lt;a rel=&quot;external&quot; href=&quot;http://research.microsoft.com/~crusso&quot;&gt;Claudio Russo&lt;/a&gt; and &lt;a rel=&quot;external&quot; href=&quot;http://research.microsoft.com/~akenn&quot;&gt;Andrew Kennedy&lt;/a&gt; have been kind enough to send me a draft paper about Generalized Algebraic Data Types (GADTs) and Object-Oriented Programming.  GADTs generalize the datatypes of ML and Haskell by permitting constructors to produce different type-instantiations of the same datatype.  One of Andrew and Claudio&#39;s examples is a slightly modified version of Peter Sestoft&#39;s example of &lt;a rel=&quot;external&quot; href=&quot;http://www.dina.kvl.dk/~sestoft/gcsharp/index.html#expr&quot;&gt;representing abstract syntax trees for typed expressions&lt;/a&gt; in Generic C#.  To get a better feeling of the difference between the generics in C#/Java and the generics in C++ I have rewritten the the example in C++.&lt;/p&gt;
&lt;p&gt;To recap the example: we want to represent abstract syntax trees for a tiny language of simple expressions. Futhermore, we want representation to be type safe.  That is, we do not want to be able to represent the nonsense expression &quot;&lt;code&gt;true + 42&lt;/code&gt;&quot;.  We shall represent the abstract syntax trees the standard OO way with an abstract base class &lt;code&gt;Exp&lt;/code&gt; for the general type of expressions and the concreate subclasses for each node type. To enforce the type safety of our embedded language we use generics.  That is, a value with type &lt;code&gt;Exp&amp;lt;R&amp;gt;&lt;/code&gt; is an abstract syntax tree for an expression that evaluates to a value of type &lt;code&gt;R&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;First the base class.&lt;/p&gt;
&lt;p&gt;Peter&#39;s Generic C# version:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;abstract class Exp&amp;lt;R&amp;gt; {   // R = result type&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  abstract public R eval();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;C++ version:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;template&amp;lt; typename R &amp;gt;  // R = result type&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;class Exp {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;public:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  virtual R eval() const = 0;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  virtual ~Exp() {}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;};&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that in C++ we need to remember to define a virtual destructor because we plan to inherit from &lt;code&gt;Exp&lt;/code&gt; and we also want to be able to dynamically allocate subclasses of &lt;code&gt;Exp&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Next up is the class for literals.&lt;/p&gt;
&lt;p&gt;Peter&#39;s Generic C# version:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;class Lit&amp;lt;R&amp;gt; : Exp&amp;lt;R&amp;gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  private readonly R v;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  public Lit(R v) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    this.v = v;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  public override R eval() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    return v;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;C++ version:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;template&amp;lt; typename R &amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;class Lit : public Exp&amp;lt;R&amp;gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;public:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  virtual R eval() const {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    return val;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  explicit Lit(R val_) : val(val_) {}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;private:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  R const val;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;};&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;No surprises here.&lt;/p&gt;
&lt;p&gt;But for binary expressions like plus, for instances, it starts to get a bit more tricky.  Here I&#39;ll use a simpler version of the class for representing plus nodes than the version Peter uses.&lt;/p&gt;
&lt;p&gt;Generic C# version:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;class Plus : Exp&amp;lt;int&amp;gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  private readonly Exp&amp;lt;int&amp;gt; lhs, rhs;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  public Plus(Exp&amp;lt;int&amp;gt; lhs, Exp&amp;lt;int&amp;gt; rhs) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    this.lhs = lhs; this.rhs = rhs;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  public override int eval() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    return lhs.eval() + rhs.eval();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;First cut at a C++ version:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;class Plus : public Exp&amp;lt;int&amp;gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;public:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  virtual int eval() const {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    return lhs.eval() + rhs.eval();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  explicit Plus(Exp&amp;lt;int&amp;gt; const lhs_, Exp&amp;lt;int&amp;gt; const rhs_)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    : lhs(lhs_), rhs(rhs_)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  {}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;private:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  Exp&amp;lt;int&amp;gt; lhs, rhs;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;};&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Alas, this is not valid C++ because we are not allowed to declare fields and parameters of type &lt;code&gt;Exp&amp;lt;int&amp;gt;&lt;/code&gt; because this class has an abstract virtual function, namely &lt;code&gt;eval&lt;/code&gt; (and besides if we could, for example by modifying the class &lt;code&gt;Exp&lt;/code&gt;, the code would not do what we expects).  These problems has nothing to do with the generics in C++, they are just the usual C++ OO quirks.  And the solution is straightforward: we must use a pointer to values of type &lt;code&gt;Exp&lt;/code&gt;.  But then we have to make decisions about ownership: should we make a deep copy of subexpressions? should we share subexpressions? and if so who should take take of freeing resources? should we use ref counting? or what? Here we shall just use one of the the wonderful smart pointers from &lt;a rel=&quot;external&quot; href=&quot;http://boost.org/&quot;&gt;Boost&lt;/a&gt;: &lt;code&gt;shared_ptr&lt;/code&gt; which will take care of the refcounting and deletion of expressions.   Thus, first we make a couple of convenient typedefs for integer expressions and shared pointers to integer expressions (and similar for boolean expressions):&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;typedef Exp&amp;lt; int &amp;gt; IntExp;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;typedef boost::shared_ptr&amp;lt; IntExp &amp;gt; IntExpPtr;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then the class for plus nodes looks like this:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;class Plus : public IntExp {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;public:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  virtual int eval() const {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    return lhs-&amp;gt;eval() + rhs-&amp;gt;eval();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  explicit Plus(IntExpPtr const &amp;amp; lhs_, IntExpPtr const &amp;amp; rhs_)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    : lhs(lhs_), rhs(rhs_)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  {}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;private:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  IntExpPtr lhs, rhs;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;};&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and a wrapper for constructing shared pointers for new plus expressions:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;IntExpPtr plus(IntExpPtr const &amp;amp; lhs, IntExpPtr const &amp;amp; rhs) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  return IntExpPtr( new Plus( lhs, rhs ) );&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Similar to the &lt;code&gt;Plus&lt;/code&gt; class we can define a class for representing condtional expressions:&lt;/p&gt;
&lt;p&gt;Generic C# version:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;class Cond&amp;lt;R&amp;gt; : Exp&amp;lt;R&amp;gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  private readonly E&amp;lt;bool&amp;gt; cond;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  private readonly E&amp;lt;R&amp;gt; truth, falsehood;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  public Cond(E&amp;lt;bool&amp;gt; cond, E&amp;lt;R&amp;gt; truth, E&amp;lt;R&amp;gt; falsehood) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    this.cond = cond; this.truth = truth; this.falsehood = falsehood;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  public override R eval() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    return cond.eval() ? truth.eval() : falsehood.eval();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;C++ version:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;template &amp;lt; typename R &amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;class Cond : public Exp&amp;lt;R&amp;gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;public:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  typedef boost::shared_ptr&amp;lt; Exp&amp;lt;R&amp;gt; &amp;gt; SubExpPtr;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  virtual R eval() const {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    return cond-&amp;gt;eval() ? truth-&amp;gt;eval() : falsehood-&amp;gt;eval();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  explicit Cond(BoolExpPtr const &amp;amp; cond_,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                SubExpPtr const &amp;amp; truth_,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                SubExpPtr const &amp;amp; falsehood_) :&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    cond(cond_), truth(truth_), falsehood(falsehood_)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  {}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;private:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  BoolExpPtr cond;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  SubExpPtr truth, falsehood;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;};&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The interesting things to note about this class are that &lt;code&gt;Cond&lt;/code&gt; contains subexpressions of different types, that &lt;code&gt;Cond&lt;/code&gt; is polymorphic in the result type, that &lt;code&gt;Cond&lt;/code&gt; enforces that the two branches of a condtional expression must have the same type, and the only one of the branchs is evaluated based on the evaluation of the condition.&lt;/p&gt;
&lt;p&gt;Using these classes we can define a function that builds an expression for calculating the &lt;em&gt;n&lt;/em&gt;&#39;th fibonacci number:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;IntExpPtr fib(int n) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  IntExpPtr fib0( lit(0) ), fib1( lit(1) );&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  switch ( n ) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  case 0: return fib0;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  case 1: return fib1;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  default:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    for(int i = 1; i != n; ++i) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      IntExpPtr tmp( fib1 );&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      fib1 = plus( fib0, fib1 );&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      fib0 = tmp;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    return fib1;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This function builds an expression that takes linear, &lt;em&gt;O(n)&lt;/em&gt;, space, but it will use exponential time, &lt;em&gt;O(2n)&lt;/em&gt;, to be evaluated.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;
Non-surprising this GADT example can be translated more or less straight forward from generinc C# to C++.  The things that are a bit tedious are just the normal C++ oddities and has nothing to do with generics.  In fact, we would have had the exact same problems without generics.&lt;/p&gt;
&lt;p&gt;Next up is to try and translate a few more of Claudio and Andrews examples.  The statically typed &lt;code&gt;printf&lt;/code&gt; and the typed LR parsing looks nifty.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Review: The Art of Interactive Design</title>
        <published>2005-02-28T20:50:35+00:00</published>
        <updated>2005-02-28T20:50:35+00:00</updated>
        
        <author>
          <name>
            Ken
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://new-blog.incorrectness.dk/blog/posts/review-the-art-of-interactive-design/"/>
        <id>https://new-blog.incorrectness.dk/blog/posts/review-the-art-of-interactive-design/</id>
        
        <content type="html" xml:base="https://new-blog.incorrectness.dk/blog/posts/review-the-art-of-interactive-design/">&lt;p&gt;I have read &lt;a rel=&quot;external&quot; href=&quot;http://www.erasmatazz.com/&quot;&gt;Chris Crawford&#39;s&lt;/a&gt; book &lt;em&gt;The Art of Interactive Design: A Euphonious and Illuminating Guide to Building Successful Software&lt;/em&gt;.  This book was quite a bit of an eye-opener for me. Not that I completely agree nor disagree with Chris Crawford, but mostly because it helped me articulate some of my thoughts. Also, while Chris Crawford writes about the design of computer interaction, I think that he explains many useful concepts that can also be used for programming language design and library interface design.&lt;/p&gt;
&lt;p&gt;Chris Crawford&#39;s main message with the book is that a new kind of &quot;programmer&quot; is needed.  Namely, &lt;em&gt;interaction designers&lt;/em&gt;. And he thinks that the interaction designers should be recruited from the Arts and Humanities.&lt;/p&gt;
&lt;p&gt;Some of my favorite concepts from the book are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;The Definition of Interaction:&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;interaction: a cyclic process in which two actors alternately listen, think, and speak.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Or in a more computer science jargon we can replace listen, think, and speak with input, process, and output.  I think this definition is spot on, and it has helped me to think about interaction problems using this definition.  And like for humans a polite and pleasent computer program is thoughtful, carefully listens, and doesn&#39;t hold back important information.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;A Criterion for Interactive Excellence:&lt;/strong&gt; Crawford gives a nice succinct formular for what is excelent interaction:&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;interactive excellence&lt;/em&gt; = &lt;em&gt;accessible states&lt;/em&gt; / &lt;em&gt;conceivable states&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;While the formula from a strictly matematical hair-splitting point of view does not make sense.  I  think it succintly describes what I have found frustrating--but havn&#39;t been able to articulate--about some computer games and a certain office suite.  Namely, that I get some idea about what ought to be possible (&lt;em&gt;conceivable&lt;/em&gt;) but for some reason I couldn&#39;t make the damn program do it (not &lt;em&gt;accessible&lt;/em&gt;).&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Colour-to-dirt ratio:&lt;/strong&gt; A concept originating from game design: Every new feature that a game designer adds to a game increases the game&#39;s colourfulness.  But every new feature also entails a certain amount of bureaucratic dirt. And a good game designer will ask herself: &quot;Is the colour I&#39;m adding with this new feature worth all the dirt it is bringing in?&quot;.  The example Chris Crawford gives is adding stock speculation to Monopoly.  An example in programming language terms could be if we considered to add call-cc to SML.  This would definately add colourfulness to the language, lots of features would be possible to add as libraries, for example, lightweight threads.  However, call-cc also adds lots of &quot;dirt&quot;, certain optimizations are no longer vailid, for instance.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;**Educational software should be based on simulation:**I could not agree more.  For many topics, simulation is a really good way of facilitating the learner to formulte her own theory about how something works.  This is one of the reasons I work at &lt;a rel=&quot;external&quot; href=&quot;http://www.laerdal.dk/&quot;&gt;Laerdal Sophus&lt;/a&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;What I don&#39;t like about the book is that Chris Crawford is beating  some points to dead, back to life, and then dead again.  In particular I think he is completely overdramatising the &lt;em&gt;two culture problem&lt;/em&gt; which is the conflict between Arts/Humanities (AH) and Science/Engineering (SE). Perhaps it is because I&#39;m a SE male who have all the power and is the source of the problem (according to Chris Crawford) and not part of the solution.  Perhaps it is because the two culture problem is bigger in the US than in Denmark, which I do think is the case. I have seen references to this conflict in serveral US movies and series, and have sometimes wondered about it before I saw the problem described for the forst time in Chris&#39; book.  However, I think is poor that the only suggestion Chris Crawford has for the SE male who wants to make successful interation design is to show more interest in more kinds of art.  I guess I just don&#39;t get it.&lt;/p&gt;
&lt;p&gt;Nevertheless I really like the book and Chris Crawford&#39;s in-your-face writing style. A sample of Chris Crawford&#39;s writing can be found on &lt;a rel=&quot;external&quot; href=&quot;http://www.gamedev.net&quot;&gt;GameDev.net&lt;/a&gt; as a &lt;a rel=&quot;external&quot; href=&quot;http://www.gamedev.net/reference/design/features/crawford/&quot;&gt;featured article&lt;/a&gt;.  The sample is a chapter from one of his other books on computer game design, and it gives a good illustration of his writing style, and is a good read to boot.&lt;/p&gt;
&lt;p&gt;[I feel I have (had?) much more to say about &lt;em&gt;The Art of Interactive Design&lt;/em&gt;, but this reveiw have been sitting in my Drafts queue for almost two months now.  So here your have it.  Maybe there will be a part two later...]&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>SQLite for Moscow ML</title>
        <published>2005-01-27T20:27:16+00:00</published>
        <updated>2005-01-27T20:27:16+00:00</updated>
        
        <author>
          <name>
            Ken
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://new-blog.incorrectness.dk/blog/posts/sqlite-for-moscow-ml/"/>
        <id>https://new-blog.incorrectness.dk/blog/posts/sqlite-for-moscow-ml/</id>
        
        <content type="html" xml:base="https://new-blog.incorrectness.dk/blog/posts/sqlite-for-moscow-ml/">&lt;p&gt;&lt;strong&gt;Stop The Press!&lt;/strong&gt; &lt;a rel=&quot;external&quot; href=&quot;http://www.itu.dk/people/hniss&quot;&gt;Henning&lt;/a&gt; has started to make a binding of &lt;a rel=&quot;external&quot; href=&quot;http://sqlite.org&quot;&gt;SQLite&lt;/a&gt; for &lt;a rel=&quot;external&quot; href=&quot;http://www.dina.kvl.dk/~sestoft/mosml.html&quot;&gt;Moscow ML&lt;/a&gt;.  This absolutelly great news. I&#39;m looking so much forward to play with this binding (hint, hint, Henning).&lt;/p&gt;
&lt;p&gt;At &lt;a rel=&quot;external&quot; href=&quot;http://www.laerdal.dk/dummynode.asp?subnodeid=8609321&quot;&gt;work&lt;/a&gt; we are using &lt;a rel=&quot;external&quot; href=&quot;http://sqlite.org&quot;&gt;SQLite&lt;/a&gt; with great success. I think that &lt;a rel=&quot;external&quot; href=&quot;http://sqlite.org&quot;&gt;SQLite&lt;/a&gt; fills an important, but somewhat overlooked, niche: a small, efficient, and easy to embed relational database with fairly complete support for SQL.&lt;/p&gt;
&lt;p&gt;I hope that &lt;a rel=&quot;external&quot; href=&quot;http://www.itu.dk/people/hniss&quot;&gt;Henning&lt;/a&gt; plans to relase it under a license, so that it might be possible to use the binding in a closed source program...&lt;/p&gt;
&lt;p&gt;Together with &lt;a rel=&quot;external&quot; href=&quot;http://mgtk.sf.net&quot;&gt;mGTK&lt;/a&gt; this binding will open up lots of interesting possibilities.  For instance, Henning talked about using the binding for poking into the &lt;a rel=&quot;external&quot; href=&quot;http://www.gnome.org/projects/beagle/&quot;&gt;Beagle&lt;/a&gt; database.  Nifty stuff.&lt;/p&gt;
&lt;p&gt;**Update 2005-01-28:**Henning has given me access to his &lt;a rel=&quot;external&quot; href=&quot;http://www.abridgegame.org/darcs/&quot;&gt;darcs&lt;/a&gt; repository for the binding.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Lambda-DAG</title>
        <published>2005-01-20T21:27:18+00:00</published>
        <updated>2005-01-20T21:27:18+00:00</updated>
        
        <author>
          <name>
            Ken
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://new-blog.incorrectness.dk/blog/posts/fanboy/"/>
        <id>https://new-blog.incorrectness.dk/blog/posts/fanboy/</id>
        
        <content type="html" xml:base="https://new-blog.incorrectness.dk/blog/posts/fanboy/">&lt;p&gt;Yesterday I attended a talk by &lt;a rel=&quot;external&quot; href=&quot;http://www.cc.gatech.edu/%7Eshivers&quot;&gt;Olin Shivers&lt;/a&gt; about a clever way to &lt;a rel=&quot;external&quot; href=&quot;http://www.itu.dk/research/funtechs/coplas/talks/2005-01-19.html&quot;&gt;representing lambda terms as DAGs&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://new-blog.incorrectness.dk/blog/images/olin-lambda-dag.jpeg&quot; alt=&quot;Olin Shivers talking about representing Lambda terms as DAGs&quot; /&gt;&lt;br /&gt;
&lt;em&gt;Olin talking about uplinks. (Yes, I mainly took the picture to test my new camera mobile phone.)&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;It was a nifty technique Olin presented clever but simple. After the talk you had a feeling that you completely understood the technique, and it seemed useful for a large class of problems. The main drawback is that the data structure is ephemeral (that is, not persistent), which means that when you perform an update you destructively update the term.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>My backup script works</title>
        <published>2004-12-15T22:37:08+00:00</published>
        <updated>2004-12-15T22:37:08+00:00</updated>
        
        <author>
          <name>
            Ken
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://new-blog.incorrectness.dk/blog/posts/my-backup-script-works/"/>
        <id>https://new-blog.incorrectness.dk/blog/posts/my-backup-script-works/</id>
        
        <content type="html" xml:base="https://new-blog.incorrectness.dk/blog/posts/my-backup-script-works/">&lt;p&gt;&lt;a href=&quot;https://new-blog.incorrectness.dk/blog/posts/shared-snapshots/&quot;&gt;Earlier&lt;/a&gt; I wrote about my home-rolled backup script.  Yesterday, I got the unfortunate optunity to test for real if the script worked as inteded, as my home partition&#39;s filesystem was corrupted.&lt;/p&gt;
&lt;p&gt;I&#39;m happy to report that the script worked perfectly.  I got all my data back without any data loss :-D  The thing that took the longest time was to convince myself that no data was lost and that the backup really was better than the filesystem that had been fixed by fsck.&lt;/p&gt;
&lt;p&gt;The only thing which is nagging me is that the corruption appears to have happened right after I took the backup.  I just hope that my script isn&#39;t to be blamed.  But how could it? I run it as an normal user...&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>That is the Xmas spirit</title>
        <published>2004-12-10T21:52:14+00:00</published>
        <updated>2004-12-10T21:52:14+00:00</updated>
        
        <author>
          <name>
            Ken
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://new-blog.incorrectness.dk/blog/posts/that-is-the-xmas-spirit/"/>
        <id>https://new-blog.incorrectness.dk/blog/posts/that-is-the-xmas-spirit/</id>
        
        <content type="html" xml:base="https://new-blog.incorrectness.dk/blog/posts/that-is-the-xmas-spirit/">&lt;p&gt;Today Maria took care of most of the Xmas present for the children we know. We left the presents in the trunk of our car while we had dinner, so that Kamille would see them. After Kamille was put to bed I went out to get the presents.  What I found was our car with the smacked window to trunk and no presents.  Nice.&lt;/p&gt;
&lt;p&gt;The burglars only got a bunch of children presents, mostly for younger children.  Thus, they didn&#39;t get anything of high value, they even left the car radio.&lt;/p&gt;
&lt;p&gt;Now we properly won&#39;t have time to go out an buy new presents and send them to our friends overseas in time for Xmas. Way, way, &lt;em&gt;way&lt;/em&gt; annoying. We are not looking forward to all the insurance hassle and so on.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>mGTK pre-release 0.93</title>
        <published>2004-12-01T22:47:47+00:00</published>
        <updated>2004-12-01T22:47:47+00:00</updated>
        
        <author>
          <name>
            Ken
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://new-blog.incorrectness.dk/blog/posts/mgtk-pre-release-093/"/>
        <id>https://new-blog.incorrectness.dk/blog/posts/mgtk-pre-release-093/</id>
        
        <content type="html" xml:base="https://new-blog.incorrectness.dk/blog/posts/mgtk-pre-release-093/">&lt;p&gt;Tonight Henning and managed to make a release of mGTK.  We call it a prerelease becase we have not written any documentation, the release has only been tested on my laptop (running Debian Sarge/Sid), and we have not made a Windows port yet.  Still, now you can &lt;a rel=&quot;external&quot; href=&quot;http://prdownloads.sourceforge.net/mgtk/mgtk-pre-0.93.tar.gz?download&quot;&gt;download it from Sourceforge&lt;/a&gt; and play with it.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Search Engines</title>
        <published>2004-11-20T19:34:48+00:00</published>
        <updated>2004-11-20T19:34:48+00:00</updated>
        
        <author>
          <name>
            Ken
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://new-blog.incorrectness.dk/blog/posts/search-engines/"/>
        <id>https://new-blog.incorrectness.dk/blog/posts/search-engines/</id>
        
        <content type="html" xml:base="https://new-blog.incorrectness.dk/blog/posts/search-engines/">&lt;p&gt;Would it not be great if &lt;a rel=&quot;external&quot; href=&quot;http://citeseer.ist.psu.edu&quot;&gt;Citeseer&lt;/a&gt; was more like &lt;a rel=&quot;external&quot; href=&quot;http://amazon.com&quot;&gt;Amazon&lt;/a&gt;?&lt;/p&gt;
&lt;p&gt;Don&#39;t get me wrong I think that &lt;a rel=&quot;external&quot; href=&quot;http://citeseer.ist.psu.edu&quot;&gt;Citeseer&lt;/a&gt; is an extreamly valuable tool, and I would not like to be without it.  I just think that it has potential to be much more useful. Imagine how nice it would be if when you looked at paper you also got a small section &quot;&lt;code&gt;Researchers who found this paper interesting also liked:&lt;/code&gt;&quot;. Likewise, it woulde be nice if you could get a page of recommondations after you had browsed around for a while.  Of course &lt;a rel=&quot;external&quot; href=&quot;http://amazon.com&quot;&gt;Amazon&lt;/a&gt; has better data because they can more reliably track if people liked an item (if &quot;like&quot; is defined as &quot;bought&quot;:-)), and I don&#39;t have much confidence in rating systems.&lt;/p&gt;
&lt;p&gt;While on the subject of search eninges.  I&#39;ve played around with &lt;a rel=&quot;external&quot; href=&quot;http://scholar.google.com&quot;&gt;Google Scholar&lt;/a&gt;. I don&#39;t think it can replace &lt;a rel=&quot;external&quot; href=&quot;http://citeseer.ist.psu.edu&quot;&gt;Citeseer&lt;/a&gt;, yet, but it is a great supplement.  It is also interesting to compare the different results you get from &lt;a rel=&quot;external&quot; href=&quot;http://citeseer.ist.psu.edu&quot;&gt;Citeseer&lt;/a&gt; and &lt;a rel=&quot;external&quot; href=&quot;http://scholar.google.com&quot;&gt;Google Scholar&lt;/a&gt; if you feed them the same search phrase. Based on a few tests it looks like &lt;a rel=&quot;external&quot; href=&quot;http://scholar.google.com&quot;&gt;Google Scholar&lt;/a&gt; is better for finding interesting papers, which you can then feed into &lt;a rel=&quot;external&quot; href=&quot;http://citeseer.ist.psu.edu&quot;&gt;Citeseer&lt;/a&gt; to get more details.  Anyway, I&#39;m looking forward to use &lt;a rel=&quot;external&quot; href=&quot;http://scholar.google.com&quot;&gt;Google Scholar&lt;/a&gt; in anger for my next paper.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Broke all Permalinks</title>
        <published>2004-11-05T21:42:56+00:00</published>
        <updated>2004-11-05T21:42:56+00:00</updated>
        
        <author>
          <name>
            Ken
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://new-blog.incorrectness.dk/blog/posts/broke-all-permalinks/"/>
        <id>https://new-blog.incorrectness.dk/blog/posts/broke-all-permalinks/</id>
        
        <content type="html" xml:base="https://new-blog.incorrectness.dk/blog/posts/broke-all-permalinks/">&lt;p&gt;I just upgraded &lt;a rel=&quot;external&quot; href=&quot;http://wordpress.org&quot;&gt;WordPress&lt;/a&gt;, and apparently my Permalink rewrite setup no longer works, my apologies.  I&#39;ll probably break them again in near future, be warned.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>A Unicode editor for Michael</title>
        <published>2004-11-02T20:44:56+00:00</published>
        <updated>2004-11-02T20:44:56+00:00</updated>
        
        <author>
          <name>
            Ken
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://new-blog.incorrectness.dk/blog/posts/a-unicode-editor-for-michael/"/>
        <id>https://new-blog.incorrectness.dk/blog/posts/a-unicode-editor-for-michael/</id>
        
        <content type="html" xml:base="https://new-blog.incorrectness.dk/blog/posts/a-unicode-editor-for-michael/">&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;http://corollary.artefact.org.nz&quot;&gt;Michael&lt;/a&gt;  &lt;a rel=&quot;external&quot; href=&quot;http://corollary.artefact.org.nz/archives/000465.html&quot;&gt;claims&lt;/a&gt; that he does not have an editor that can handle Unicode.&lt;/p&gt;
&lt;p&gt;Thus, Henning and I whipped up an &lt;a rel=&quot;external&quot; href=&quot;http://cvs.sourceforge.net/viewcvs.py/mgtk/mgtk/examples/editor.sml?rev=1.9&amp;amp;view=auto&quot;&gt;editor&lt;/a&gt; using &lt;a rel=&quot;external&quot; href=&quot;http://mgtk.sourceforge.net&quot;&gt;mGTK&lt;/a&gt; that can handle Unicode.  Oh, and did I mention that you can it compile with either &lt;a rel=&quot;external&quot; href=&quot;http://www.dina.kvl.dk/~sestoft/mosml.html&quot;&gt;Moscow ML&lt;/a&gt; or &lt;a rel=&quot;external&quot; href=&quot;http://mlton.org&quot;&gt;MLton&lt;/a&gt; without changing the source?&lt;/p&gt;
&lt;p&gt;The real story is of course that I we were trying to build a &quot;real&quot; application using &lt;a rel=&quot;external&quot; href=&quot;http://mgtk.sourceforge.net&quot;&gt;mGTK&lt;/a&gt;, and the editor example shows that the &lt;a rel=&quot;external&quot; href=&quot;http://gtk.org&quot;&gt;gtk+&lt;/a&gt; widgets handle Unicode fine. Wereas I&#39;m not sure that SML handles Unicode &quot;fine&quot;, String.size does not return the number of characters but the number of bytes.  But at least TextIO does not mess up the bytes (in &lt;a rel=&quot;external&quot; href=&quot;http://www.dina.kvl.dk/~sestoft/mosml.html&quot;&gt;Moscow ML&lt;/a&gt; at least, didn&#39;t test with &lt;a rel=&quot;external&quot; href=&quot;http://mlton.org&quot;&gt;MLton&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;Oh, and I used &lt;code&gt;file&lt;/code&gt; and &lt;code&gt;gedit&lt;/code&gt; to check that the file I saved really was Unicode.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>More mGTK Progress</title>
        <published>2004-10-28T20:58:06+00:00</published>
        <updated>2004-10-28T20:58:06+00:00</updated>
        
        <author>
          <name>
            Ken
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://new-blog.incorrectness.dk/blog/posts/more-mgtk-progress/"/>
        <id>https://new-blog.incorrectness.dk/blog/posts/more-mgtk-progress/</id>
        
        <content type="html" xml:base="https://new-blog.incorrectness.dk/blog/posts/more-mgtk-progress/">&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;http://www.itu.dk/people/hniss&quot;&gt;Henning&lt;/a&gt; and I have been working hard (leisure time hard, that is) on &lt;a rel=&quot;external&quot; href=&quot;http://mgtk.sourceforge.net&quot;&gt;mGTK&lt;/a&gt; since my last &lt;a href=&quot;https://new-blog.incorrectness.dk/blog/posts/mgtk-progress/&quot;&gt;mGTK progress report&lt;/a&gt;. We have ported all the interesting &lt;a rel=&quot;external&quot; href=&quot;http://gtk-sharp.sourceforge.net/&quot;&gt;Gtk#&lt;/a&gt; examples from &lt;a rel=&quot;external&quot; href=&quot;http://usefulinc.com/edd/books/mono-notebook&quot;&gt;Mono: A Developer&#39;s Notebook&lt;/a&gt; Chapter 4.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://new-blog.incorrectness.dk/blog/images/mgtk-20041028.png&quot; alt=&quot;screenshot of four mGTK examples&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Currently the only missing bits are better support for the lower levels of the library stack most notably GDK, but also Glib, ATK, and Pango.  Which means that there are no pretty monkeys and the drag and drop example does not compile, yet. The source code for the examples can be found in the &lt;a rel=&quot;external&quot; href=&quot;http://cvs.sourceforge.net/viewcvs.py/mgtk/mgtk/examples/&quot;&gt;mGTK CVS&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Next up, is polish of the bindings generator for &lt;a rel=&quot;external&quot; href=&quot;http://www.mlton.org&quot;&gt;MLton&lt;/a&gt; which is currently not as well as the &lt;a rel=&quot;external&quot; href=&quot;http://www.dina.kvl.dk/~sestoft/mosml.html&quot;&gt;Moscow ML&lt;/a&gt; generator.  The only thing missing is support for out-arguments.  However, Henning is on the case, so the problems should be solved within days(?!?). A release is sooo close. That fells really good because it has taken three years to come this far.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>callcc for Moscow ML</title>
        <published>2004-10-05T20:32:18+00:00</published>
        <updated>2004-10-05T20:32:18+00:00</updated>
        
        <author>
          <name>
            Ken
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://new-blog.incorrectness.dk/blog/posts/callcc-for-moscow-ml/"/>
        <id>https://new-blog.incorrectness.dk/blog/posts/callcc-for-moscow-ml/</id>
        
        <content type="html" xml:base="https://new-blog.incorrectness.dk/blog/posts/callcc-for-moscow-ml/">&lt;p&gt;The last few days I have tried to recreate my implementation of &lt;code&gt;callcc&lt;/code&gt; for &lt;a rel=&quot;external&quot; href=&quot;http://www.dina.kvl.dk/~sestoft/mosml.html&quot;&gt;Moscow ML&lt;/a&gt;.  My original (incomplete) implementation was lost last fall when my old laptop was stolen (or it is still lurking somewhere in mess which is my old backup system).&lt;/p&gt;
&lt;p&gt;The code &lt;em&gt;almost&lt;/em&gt; works. For example, the following code, which just ignores the continuation value &lt;code&gt;k&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;3 + callcc (fn k =&amp;gt; 2 + 1);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Gives &lt;code&gt;6&lt;/code&gt;, as it should.&lt;/p&gt;
&lt;p&gt;However, the following more interesting example, which actually uses the continuation value:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;3 + callcc (fn k =&amp;gt; 2 + throw (k, 1));&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Gives &lt;code&gt;67299815&lt;/code&gt; and not &lt;code&gt;4&lt;/code&gt;. Ouch!&lt;/p&gt;
&lt;p&gt;Likewise, given the following declaration of &lt;code&gt;multiply&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;fun multiply ints =&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    callcc (fn ret =&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    let&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        fun mult nil = 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          | mult (0::_) = throw (ret, 0)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          | mult (n::ns) = n * mult ns&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    in&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	mult ints&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    end)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The expression:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;multiply [1,2,3,4,0,5];&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;evaluates to &lt;code&gt;67299810&lt;/code&gt; and not &lt;code&gt;0&lt;/code&gt;. Double ouch!!&lt;/p&gt;
&lt;p&gt;My guess is that I somehow mess things up when I restore the stack, and some pointer ends up on top of the stack.  But I&#39;m too tired to debug this futher tonight.&lt;/p&gt;
&lt;p&gt;One nifty feature of my code (if I can get it to work) is that it is implemented using the &lt;code&gt;Dynlib&lt;/code&gt; library.  Which means that &lt;strong&gt;no&lt;/strong&gt; changes are needed to the original Moscow ML code, it is all in a seperate library.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Half a year</title>
        <published>2004-09-24T20:05:37+00:00</published>
        <updated>2004-09-24T20:05:37+00:00</updated>
        
        <author>
          <name>
            Ken
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://new-blog.incorrectness.dk/blog/posts/half-a-year/"/>
        <id>https://new-blog.incorrectness.dk/blog/posts/half-a-year/</id>
        
        <content type="html" xml:base="https://new-blog.incorrectness.dk/blog/posts/half-a-year/">&lt;p&gt;These days it is six months since I started working at Laerdal Sophus.  It is amazing how quickly that time has passed. So far it has been just as educational and fun as I had hoped for.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Ida has left the country</title>
        <published>2004-09-21T21:12:11+00:00</published>
        <updated>2004-09-21T21:12:11+00:00</updated>
        
        <author>
          <name>
            Ken
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://new-blog.incorrectness.dk/blog/posts/ida-has-left-the-country/"/>
        <id>https://new-blog.incorrectness.dk/blog/posts/ida-has-left-the-country/</id>
        
        <content type="html" xml:base="https://new-blog.incorrectness.dk/blog/posts/ida-has-left-the-country/">&lt;p&gt;Today my littlesister left Danmark  to travel around in New Zealand for three months.  I already miss her.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>The birth of Naja</title>
        <published>2004-09-21T21:08:48+00:00</published>
        <updated>2004-09-21T21:08:48+00:00</updated>
        
        <author>
          <name>
            Ken
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://new-blog.incorrectness.dk/blog/posts/the-birth-of-naja/"/>
        <id>https://new-blog.incorrectness.dk/blog/posts/the-birth-of-naja/</id>
        
        <content type="html" xml:base="https://new-blog.incorrectness.dk/blog/posts/the-birth-of-naja/">&lt;p&gt;A bit delayed, but here is the account of Naja&#39;s birth.&lt;/p&gt;
&lt;p&gt;Maria started to have contractions Monday (the 13&#39;th) morning.  While the contractions where painful, they where also short of of duration and irregular in frequency.  In the evening Kamille showed the first symptoms of chickenpox.&lt;/p&gt;
&lt;p&gt;Tuesday morning Kamille was showing quite a few chickenpox.  Maria continued to have irregular and short of duration contractions during the day.&lt;/p&gt;
&lt;p&gt;The night to Wednesday was horrible: Maria&#39;s contractions got worse and so did Kamille&#39;s chickenpox.  Thankfully, my parents was able to come and pick up Kamille in the morning.&lt;/p&gt;
&lt;p&gt;At 11 o&#39;clock Wednesday (the 15&#39;th) the water broke and the contractions got worse, longer in duration, and with higher frequency.  At 12:30 we went to the hopital and at 15:10 Naja was born.   The birth went well with no complications, and the midwife and midwife student were really nice.&lt;/p&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;http://pictures.friislarsen.net/naja-birth/img003.jpeg.html&quot;&gt;&lt;img src=&quot;https://new-blog.incorrectness.dk/blog/images/img_0354-thumbnail.jpg&quot; alt=&quot;Naja latched on to Maria&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Naja latched on to Maria without any problems and started to suck away immediately, within 10 mins after the birth. Naja&#39;s weight was 3806g and she was 54cm long.&lt;/p&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;http://pictures.friislarsen.net/naja-birth/img006.jpeg.html&quot;&gt;&lt;img src=&quot;https://new-blog.incorrectness.dk/blog/images/img_0357-thumbnail.jpg&quot; alt=&quot;Naja and Maria&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;At six o&#39;clock Kamille, our parents and siblings came to greet Naja welcome to the world.  Kamille demanded right away to hold Naja, and that wish was granted.&lt;/p&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;http://pictures.friislarsen.net/naja-birth/img013.jpeg.html&quot;&gt;&lt;img src=&quot;https://new-blog.incorrectness.dk/blog/images/img_0365-thumbnail.jpg&quot; alt=&quot;Kamille and Naja&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Maria, Naja, and I spend the night at the hospital in a so-called &quot;family-room&quot; (Kamille wasn&#39;t allowed at that ward because of the chickenpox.  At 9:30 Thursday morning we left the hospital and I picked Kamille up at my parents.  Dispite the chickenpox Kamille had an enourmous reserve of love for her littlesister.  The first few days was a bit stressful because of the chickenpox (Naja was an angel).  On saturday the chikenpox finally got better, and so did family life.&lt;/p&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;http://pictures.friislarsen.net/naja-birth/img039.jpeg.html&quot;&gt;&lt;img src=&quot;https://new-blog.incorrectness.dk/blog/images/img_0391-thumbnail.jpg&quot; alt=&quot;Kamille kissing Naja&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I have uploaded an &lt;a rel=&quot;external&quot; href=&quot;http://pictures.friislarsen.net/naja-birth/&quot;&gt;album&lt;/a&gt; with 43 pictures from the birth.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Tucking in early…</title>
        <published>2004-09-13T19:58:44+00:00</published>
        <updated>2004-09-13T19:58:44+00:00</updated>
        
        <author>
          <name>
            Ken
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://new-blog.incorrectness.dk/blog/posts/tucking-in-early/"/>
        <id>https://new-blog.incorrectness.dk/blog/posts/tucking-in-early/</id>
        
        <content type="html" xml:base="https://new-blog.incorrectness.dk/blog/posts/tucking-in-early/">&lt;p&gt;We are tucking in early tonight.  Because, judging the signs, we will have to get up during the night....&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Update 2004-09-14:&lt;/strong&gt; No we did not need to get up during the night.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Changing website</title>
        <published>2004-09-12T20:31:36+00:00</published>
        <updated>2004-09-12T20:31:36+00:00</updated>
        
        <author>
          <name>
            Ken
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://new-blog.incorrectness.dk/blog/posts/changing-website/"/>
        <id>https://new-blog.incorrectness.dk/blog/posts/changing-website/</id>
        
        <content type="html" xml:base="https://new-blog.incorrectness.dk/blog/posts/changing-website/">&lt;p&gt;It is just a small change, yet it feels like a big step.  This evening I made a new (sketchy) &lt;a rel=&quot;external&quot; href=&quot;http://ken.friislarsen.net&quot;&gt;webpage&lt;/a&gt; in the friislarsen.net domain, and redirected my old page at the &lt;a rel=&quot;external&quot; href=&quot;http://www.itu.dk&quot;&gt;IT University&lt;/a&gt; to the new page.&lt;/p&gt;
&lt;p&gt;Now I just need to fill the new page with some content.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Shared snapshots</title>
        <published>2004-09-08T20:52:23+00:00</published>
        <updated>2004-09-08T20:52:23+00:00</updated>
        
        <author>
          <name>
            Ken
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://new-blog.incorrectness.dk/blog/posts/shared-snapshots/"/>
        <id>https://new-blog.incorrectness.dk/blog/posts/shared-snapshots/</id>
        
        <content type="html" xml:base="https://new-blog.incorrectness.dk/blog/posts/shared-snapshots/">&lt;p&gt;Yesterday I bought an external hard-disk for backups.  To make the backups I&#39;ve rolled my own script (&lt;a rel=&quot;external&quot; href=&quot;https://new-blog.incorrectness.dk/blog/images/snapshot.sh&quot;&gt;snapshot.sh&lt;/a&gt;) that uses &lt;code&gt;rsync&lt;/code&gt; to make a snapshot of my home-partition and transfer it to the external hard-disk.  The interesting part is, that the script takes advantage of the &lt;code&gt;rsync&lt;/code&gt; option &lt;code&gt;--link-dest&lt;/code&gt; to share a large part of the snapshots using hardlinks.&lt;/p&gt;
&lt;p&gt;So after making three snapshots &lt;code&gt;du -sh&lt;/code&gt; tells us that the snapshots takes up 25.3 Gb of disk space:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;8.5G    /mnt/external/vips-backup/snapshot.0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;8.4G    /mnt/external/vips-backup/snapshot.1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;8.4G    /mnt/external/vips-backup/snapshot.2&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;However, &lt;code&gt;df -h&lt;/code&gt; tells us that only 8.6 Gb of the external hard-disk is used:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;/dev/sda1             187G  8.6G  179G   5% /mnt/external&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Nifty, and with just 40 lines of bash scripting.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Pictures from Kamille’s birthday</title>
        <published>2004-09-02T19:43:32+00:00</published>
        <updated>2004-09-02T19:43:32+00:00</updated>
        
        <author>
          <name>
            Ken
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://new-blog.incorrectness.dk/blog/posts/pictures-from-kamilles-birthday/"/>
        <id>https://new-blog.incorrectness.dk/blog/posts/pictures-from-kamilles-birthday/</id>
        
        <content type="html" xml:base="https://new-blog.incorrectness.dk/blog/posts/pictures-from-kamilles-birthday/">&lt;p&gt;I have uploaded a new photo album with pictures from &lt;a rel=&quot;external&quot; href=&quot;http://pictures.friislarsen.net/kamille-3-years&quot;&gt;Kamille&#39;s birthday&lt;/a&gt; and &lt;a rel=&quot;external&quot; href=&quot;http://pictures.friislarsen.net/for-hanna-and-zoe&quot;&gt;two short movies&lt;/a&gt; with Kamille saying &quot;thank you&quot; to her friends from Utah who sent her a lovely dress.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Now with Java</title>
        <published>2004-09-01T20:58:21+00:00</published>
        <updated>2004-09-01T20:58:21+00:00</updated>
        
        <author>
          <name>
            Ken
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://new-blog.incorrectness.dk/blog/posts/now-with-java/"/>
        <id>https://new-blog.incorrectness.dk/blog/posts/now-with-java/</id>
        
        <content type="html" xml:base="https://new-blog.incorrectness.dk/blog/posts/now-with-java/">&lt;p&gt;Following up on the benchmark from yesterday, I&#39;ve produced a Java version of the queens benchmark (&lt;a rel=&quot;external&quot; href=&quot;https://new-blog.incorrectness.dk/blog/images/queens.java&quot;&gt;queens.java&lt;/a&gt;).  Below is the updated graph. I have a suspision that the reason Mono and Java is performing so bad is not only due to exceptions, but rather it has something to do with garbage collection.&lt;/p&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;https://new-blog.incorrectness.dk/blog/images/queens-graph-v2.png&quot;&gt;&lt;img src=&quot;https://new-blog.incorrectness.dk/blog/images/queens-graph-v2-small.png&quot; alt=&quot;Graph showing running times for the different executables&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I also tried to improve the C++ version based on some hints I got from Asger and Søren (colleagues at Laerdal Sophus).  The C++ improvements worked really well when we tested them at work with Visual Studio 6.0.  But the improvements had absolutely no effect when compiling with gcc 3.4.1. Either gcc is very smart or very stupid. Something suggest the later as a quick test reveals that gcc version 3.4.1 is producing worse code than version 3.3.4, which in turn is producing worse code than version 3.2.3 .  I guess that I&#39;ll have to install Windows and the free Microsoft compilers, so that I can compare them against gcc and Mono.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>A hand full of Queens</title>
        <published>2004-08-31T21:52:03+00:00</published>
        <updated>2004-08-31T21:52:03+00:00</updated>
        
        <author>
          <name>
            Ken
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://new-blog.incorrectness.dk/blog/posts/a-hand-full-of-queens/"/>
        <id>https://new-blog.incorrectness.dk/blog/posts/a-hand-full-of-queens/</id>
        
        <content type="html" xml:base="https://new-blog.incorrectness.dk/blog/posts/a-hand-full-of-queens/">&lt;p&gt;Or: &lt;strong&gt;SML/Moscow ML is faster than C++/GCC&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Inspired by a remark by &lt;a rel=&quot;external&quot; href=&quot;http://www.dina.dk/~sestoft&quot;&gt;Peter&lt;/a&gt; about the abysmal performance of exceptions on the .NET platform.  I decided to check it for myself with a micro-benchmark. I also decided to check the performance of C++ exceptions, just for the fun of it.&lt;/p&gt;
&lt;p&gt;The micro-benchmark is to find a solution to the N-queen problem using backtracking, with exceptions used as backtracking. I tested four different languages (SML, O&#39;Caml, C#, and C++) and five different language implementations (&lt;a rel=&quot;external&quot; href=&quot;http://mlton.org&quot;&gt;MLton&lt;/a&gt;, &lt;a rel=&quot;external&quot; href=&quot;http://www.dina.dk/~sestoft/mosml.html&quot;&gt;Moscow ML&lt;/a&gt;, &lt;a rel=&quot;external&quot; href=&quot;http://caml.inria.fr/ocaml&quot;&gt;O&#39;Caml&lt;/a&gt;, &lt;a rel=&quot;external&quot; href=&quot;http://mono-project.com&quot;&gt;Mono&lt;/a&gt;, and &lt;a rel=&quot;external&quot; href=&quot;http://gcc.gnu.org/&quot;&gt;GCC&lt;/a&gt;). The results are a bit surprising, and definately deserves further investigation. The graph below shows the running times for the five different executables:&lt;/p&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;https://new-blog.incorrectness.dk/blog/images/queens-graph.png&quot;&gt;&lt;img src=&quot;https://new-blog.incorrectness.dk/blog/images/queens-graph-small.png&quot; alt=&quot;Graph showing running times for the different executables&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;From the graph we see that O&#39;Caml and MLton are way faster than the rest (and O&#39;Caml has a small edge compared to MLton), Moscow ML is faster than Mono, and my C++ program is slow as a slug. I&#39;ve tried various small tweaks to make the C++ version faster, but nothing really helps. For instance, at first we might notice that there are a tiny algorithmic difference between the C++ version and the other versions, namely the order in which a new position is compared with the already determined positions. But changing this order have no real effect (I&#39;ve tried to change C++ program in various ways, and I tried to change the other programs). Still, the C++ performance is so bad that it looks suspicious. I&#39;m open to suggestions for how to improve the C++ program.&lt;/p&gt;
&lt;p&gt;Now I just have to test the SML version with the MLKit and SML/NJ, test the C# version with Microsoft&#39;s runtime, test the C++ version with Visual C++ and Intel&#39;s C++ compiler, and make a Java version of the benchmark and test it with ...  Any volunteers?&lt;/p&gt;
&lt;p&gt;The programs used in the benchmark are here: &lt;a rel=&quot;external&quot; href=&quot;https://new-blog.incorrectness.dk/blog/images/queens.sml&quot;&gt;queens.sml&lt;/a&gt;, &lt;a rel=&quot;external&quot; href=&quot;https://new-blog.incorrectness.dk/blog/images/queens.ml&quot;&gt;queens.ml&lt;/a&gt;, &lt;a rel=&quot;external&quot; href=&quot;https://new-blog.incorrectness.dk/blog/images/queens.cs&quot;&gt;queens.cs&lt;/a&gt; (ran &lt;code&gt;mono&lt;/code&gt; with option &lt;code&gt;--optimize=all&lt;/code&gt;) , and &lt;a rel=&quot;external&quot; href=&quot;https://new-blog.incorrectness.dk/blog/images/queens.cpp&quot;&gt;queens.cpp&lt;/a&gt; (compiled with option &lt;code&gt;-O3&lt;/code&gt;).&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>mGTK progress</title>
        <published>2004-08-26T22:16:51+00:00</published>
        <updated>2004-08-26T22:16:51+00:00</updated>
        
        <author>
          <name>
            Ken
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://new-blog.incorrectness.dk/blog/posts/mgtk-progress/"/>
        <id>https://new-blog.incorrectness.dk/blog/posts/mgtk-progress/</id>
        
        <content type="html" xml:base="https://new-blog.incorrectness.dk/blog/posts/mgtk-progress/">&lt;p&gt;Tonight I decided to try and port an example from &lt;a rel=&quot;external&quot; href=&quot;http://www.oreilly.com/catalog/monoadn&quot;&gt;Mono: A Developer&#39;s Notebook&lt;/a&gt; to the upcoming release of &lt;a rel=&quot;external&quot; href=&quot;http://mgtk.sf.net&quot;&gt;mGTK&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;By joining forces with &lt;a rel=&quot;external&quot; href=&quot;http://www.itu.dk/people/hniss&quot;&gt;Henning&lt;/a&gt;  we were able to compile the example for the following screenshot:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://new-blog.incorrectness.dk/blog/images/signup.png&quot; alt=&quot;mGTK screenshot&quot; /&gt;&lt;/p&gt;
&lt;p&gt;The email entry-box is updated dynamically when you type something in the two other entry boxes. Mnemonics works, that is, if you press ALT-f the focus jumps to the entry-box for the first name and the text is selected. And all that in less than 100 lines of SML code.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>My brother graduated</title>
        <published>2004-08-25T19:41:04+00:00</published>
        <updated>2004-08-25T19:41:04+00:00</updated>
        
        <author>
          <name>
            Ken
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://new-blog.incorrectness.dk/blog/posts/my-brother-graduated/"/>
        <id>https://new-blog.incorrectness.dk/blog/posts/my-brother-graduated/</id>
        
        <content type="html" xml:base="https://new-blog.incorrectness.dk/blog/posts/my-brother-graduated/">&lt;p&gt;Today my brother had his viva for his masters in Economics. And I&#39;m proud to report that he passed with flying colours. His disseration was on &lt;em&gt;Risk attitude and portfolio theory&lt;/em&gt; (my translation).&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Visitor pattern visited</title>
        <published>2004-08-24T21:18:01+00:00</published>
        <updated>2004-08-24T21:18:01+00:00</updated>
        
        <author>
          <name>
            Ken
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://new-blog.incorrectness.dk/blog/posts/visitor-pattern-visited/"/>
        <id>https://new-blog.incorrectness.dk/blog/posts/visitor-pattern-visited/</id>
        
        <content type="html" xml:base="https://new-blog.incorrectness.dk/blog/posts/visitor-pattern-visited/">&lt;p&gt;Hmm, my first blog entry. To get started, I&#39;ll just write about a book I&#39;m reading these days.&lt;/p&gt;
&lt;p&gt;I have a confession to make: I have been guilty of discarding some of the OOP patterns as &quot;just papering over the weaknesses of OOP languages&quot;.  For example, the &lt;em&gt;Visitor pattern&lt;/em&gt; has been discarded as &quot;it is just fold&quot;. However, lately I&#39;ve been reading a few C++ books (because my new job involves a lot C++ coding).  One of the books I&#39;m reading (for the second time) is Andrei Alexandrescu&#39;s book &lt;a rel=&quot;external&quot; href=&quot;http://moderncppdesign.com/book/main.html&quot;&gt;Modern C++ Design&lt;/a&gt;, which is about how to implement patterns using C++ templates.  In the introduction of the chapter about the Visitor pattern Alexandrescu first write:&lt;/p&gt;
&lt;blockquote&gt;To do this [_enhance the functionality of a given class hierarchy_], you can either add new classes or add new virtual member functions.  Adding new classes is easy ... [_snip_] In contrast, adding new virtual functions is difficult. To be able to manipulate objects polymorphically (via pointers to the root class), you must add virtual member functions to the root class, and possibly to many other classes in the hierarchy.  This is a major operation. ... [_snip_] In a nutshell, from a dependency standpoint, new classes are easy to add, and new virtual member functions are difficult to add.&lt;/blockquote&gt;
&lt;p&gt;At this point I has nodding to myself and thinking something along the lines: &quot;What a clear description of what is wrong with OO programming languages&quot;.  These problems with OO languages are of couse well known, it is, for example, similar to what Xavier Leroy said in his invited talk at ICFP&#39;99. I was just using Alexandrescu to confirm my prejudices.  But Alexandrescu wents on:&lt;/p&gt;
&lt;blockquote&gt;Visitor trades the advantage you don&#39;t care about [_to add new classes with ease_] for an advantage you need [_to add new virtual functions to a hierachy easily_].&lt;/blockquote&gt;
&lt;p&gt;This is the important insight.  And it an insight that I&#39;ve completely overlooked until now. I think that this way of looking at programming patterns is useful: look for advantages you don&#39;t need and see if you can trade them for advantages you need.  I also think that this is useful viewpoint for type systems and programming languages in general. For example, in C/C++ you have the advantage that you have full control over memory management, and this advantage can in some situations be used for writing efficient programs.  But sometimes you are willing to give up that advantage for the advantage of automatically handling of memory management (i.e., a garbage collector).&lt;/p&gt;
&lt;p&gt;Back to Alexandrescu. His book also made me realise that the Visitor pattern probably corresponds more to a &lt;code&gt;case&lt;/code&gt;-expression than to a fold-function, at least Alexandrescu doesn&#39;t give a concrete example that handles recursion.  Perhaps, I should have paid more attention when I read the GoF. All in all, I can recommend Alexandrescu&#39;s book as a fine supplement to the GoF (if you are into C++ and don&#39;t mind templates).&lt;/p&gt;
</content>
        
    </entry>
</feed>
