<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Looking Out To Sea &#187; Computing</title>
	<atom:link href="http://www.dougalstanton.net/blog/index.php/category/computing/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dougalstanton.net/blog</link>
	<description></description>
	<lastBuildDate>Sat, 28 Jan 2012 17:49:36 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Corpse Reviver</title>
		<link>http://www.dougalstanton.net/blog/index.php/2012/01/25/corpse-reviver/</link>
		<comments>http://www.dougalstanton.net/blog/index.php/2012/01/25/corpse-reviver/#comments</comments>
		<pubDate>Thu, 26 Jan 2012 00:21:51 +0000</pubDate>
		<dc:creator>Dougal</dc:creator>
				<category><![CDATA[Blogging]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.dougalstanton.net/blog/index.php/2012/01/25/corpse-reviver/</guid>
		<description><![CDATA[This is one of those traditional &#8220;I&#8217;m going to start blogging again, I promise&#8221; posts. Essentially I&#8217;ve installed the WordPress app on my phone so I&#8217;ll try to use it from the train and so on. Which also means you&#8217;ll have to excuse the typos.

As if to start, this evening I went to a Glasgow [...]]]></description>
			<content:encoded><![CDATA[<p>This is one of those traditional &#8220;I&#8217;m going to start blogging again, I promise&#8221; posts. Essentially I&#8217;ve installed the WordPress app on my phone so I&#8217;ll try to use it from the train and so on. Which also means you&#8217;ll have to excuse the typos.</p>

<p>As if to start, this evening I went to a Glasgow TechMeetup. There are Edinburgh equivalents that I never attended so I don&#8217;t know how they compare. The format was:</p>

<ul>
<li>beer and pizza at 6.30. Next time I must remember a bottle opener!</li>
<li>a welcome about 7pm including an introduction from everyone in the room. There must have been about fifty people in the room so it took about half an hour!</li>
<li>a few minutes to chat amongst ourselves, particularly with anyone who sounded interesting or employable. </li>
<li>a talk about web design</li>
<li>a talk about http caching</li>
</ul>

<p>The first talk was pretty dull and I was ready to call it a day at that point. Thankfully I didn&#8217;t cos the second talk was well worth the entry fee, even if it was free. If you bump into serialseb he gives a great talk - funny and informative.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dougalstanton.net/blog/index.php/2012/01/25/corpse-reviver/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A look at Lightweight Static Capabilities (part 3)</title>
		<link>http://www.dougalstanton.net/blog/index.php/2011/01/23/a-look-at-lightweight-static-capabilities-part-3/</link>
		<comments>http://www.dougalstanton.net/blog/index.php/2011/01/23/a-look-at-lightweight-static-capabilities-part-3/#comments</comments>
		<pubDate>Sun, 23 Jan 2011 16:12:15 +0000</pubDate>
		<dc:creator>Dougal</dc:creator>
				<category><![CDATA[Computing]]></category>
		<category><![CDATA[Maths & Computer Science]]></category>

		<guid isPermaLink="false">http://www.dougalstanton.net/blog/?p=1328</guid>
		<description><![CDATA[This is the third part of my slow examination of Kiselyov and Shan&#8217;s Lightweight Static Capabilities paper. Start at the the beginning and then read part two to put this part into context.

Section 3 of the paper uses the same technique from Section 2 and adapts it to tackle a larger and more interesting problem: [...]]]></description>
			<content:encoded><![CDATA[<p>This is the third part of my slow examination of Kiselyov and Shan&#8217;s <em>Lightweight Static Capabilities</em> paper. Start <a href="http://www.dougalstanton.net/blog/index.php/2011/01/16/a-look-at-lightweight-static-capabilities-part-1/">at the the beginning</a> and then <a href="http://www.dougalstanton.net/blog/index.php/2011/01/18/a-look-at-lightweight-static-capabilities-part-2/">read part two</a> to put this part into context.</p>

<p>Section 3 of the paper uses the same technique from Section 2 and adapts it to tackle a larger and more interesting problem: array bound checking. To motivate the problem they follow in the footsteps of the dependent type literature by implementing binary search with statically-checked &#8220;safe&#8221; array lookups.</p>

<p>To explain the approach they use we&#8217;ll start with an unsafe implementation that uses runtime bounds checking on each index.</p>

<p><div>
<pre class="haskell">bsearch :: <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Ord"><span style="color: #cccc00; font-weight: bold;">Ord</span></a> a<span style="color: green;">&#41;</span> =&gt; <span style="color: green;">&#40;</span>a -&gt; a -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Ordering"><span style="color: #cccc00; font-weight: bold;">Ordering</span></a><span style="color: green;">&#41;</span> -&gt; a -&gt; Array a -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Maybe"><span style="color: #cccc00; font-weight: bold;">Maybe</span></a> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a>,a<span style="color: green;">&#41;</span>
bsearch cmp key arr = go <span style="color: red;">0</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:length"><span style="font-weight: bold;">length</span></a> arr - <span style="color: red;">1</span><span style="color: green;">&#41;</span>
  <span style="color: #06c; font-weight: bold;">where</span> go lo hi = <span style="color: #06c; font-weight: bold;">if</span> hi &gt;= lo
                      <span style="color: #06c; font-weight: bold;">then</span> <span style="color: #06c; font-weight: bold;">let</span> midpoint = lo + <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>hi - lo<span style="color: green;">&#41;</span>`<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:div"><span style="font-weight: bold;">div</span></a>`<span style="color: red;">2</span><span style="color: green;">&#41;</span>
                               curvalue = arr ! midpoint
                           <span style="color: #06c; font-weight: bold;">in</span> <span style="color: #06c; font-weight: bold;">case</span> cmp key curvalue <span style="color: #06c; font-weight: bold;">of</span>
                                  LT -&gt; look lo <span style="color: green;">&#40;</span>m - <span style="color: red;">1</span><span style="color: green;">&#41;</span>
                                  EQ -&gt; Just <span style="color: green;">&#40;</span>m, curvalue<span style="color: green;">&#41;</span>
                                  GT -&gt; look <span style="color: green;">&#40;</span>m + <span style="color: red;">1</span><span style="color: green;">&#41;</span> hi
                      <span style="color: #06c; font-weight: bold;">else</span> Nothing</pre>
</div></p>

<p>The index operation <code>(!)</code> checks at runtime to see if the supplied index is within the bounds defined by the array (we still have to catch an exception, which I haven&#8217;t shown here, but at least it won&#8217;t blow everything up). This check is not necessary if we can prove that the index will always be in range.</p>

<p>The general approach outlined in Section 2 was to brand &#8220;safe&#8221; operations and types which go together. Once a list had been branded <em>non-empty</em> (ie had type <code>FullList</code>) it could be freely passed around and later safely destructured into head and tail without worry.</p>

<p>In the case of lists the values were branded &#8220;safe&#8221; and the <code>head</code>/<code>tail</code> operations could only work on &#8220;safe&#8221; values. The <code>head</code> operation is analogous to indexing the 0th element in an array. If we were to extend the list approach to arrays we would need a <code>deref0</code> for the 0th element, <code>deref1</code> for the 1st element and so on. This is both unwieldy and needlessly restricts the type of indexing possible.</p>

<p>The solution proposed by the paper is a set of &#8220;safe&#8221; indexing operations which only work when both the array and the index are branded as being compatible. An index is compatible with an array if it is within that array&#8217;s bounds.</p>

<p><div>
<pre class="haskell">get :: BrandedArray b a -&gt; BrandedInt b -&gt; a</pre>
</div></p>

<p>The type variable <code>b</code> here is a phantom type which brands arrays and indices. The <code>get</code> operation can only be used if the same brand is applied to both array and the index.</p>

<p>Initially the only &#8220;safe&#8221; array indices are the offsets of the upper and lower element. The brands for an array are created alongside the upper and lower index in one operation, providing the assurance to the type system that whatever type <code>b</code> represents it is identical for these three values:</p>

<p><div>
<pre class="haskell">brand :: Array -&gt; <span style="color: green;">&#40;</span>BrandedArray b a, BrandedInt b, BrandedInt b<span style="color: green;">&#41;</span></pre>
</div></p>

<p>(In fact this is not the proper type signature. The proper signature is reproduced below, with existential quantification over the branding type <code>b</code> and continuations for passing/failing conditions. This signature is just a gentle introduction to get the idea across)</p>

<p>Further operations can be created with transformations on the provided indices: increments, decrements and midpoints.</p>

<p>Whatever <code>b</code> is here it&#8217;s the same type, but next time <code>brand</code> is called <code>b</code> may well be a different type. The type system can&#8217;t unify them since it doesn&#8217;t know the type &#8212; it&#8217;s never specified. So only indices created alongside the branded array can be used with it, since only in that situation can the type checker be sure that the type of <code>b</code> matches.</p>

<p>This seems a powerful use of phantom types and it&#8217;s one I&#8217;d never seen before. The paper uses the phrase &#8220;type eigenvariable&#8221; at this point, which I&#8217;m not familiar with but it&#8217;s such a nice phrase that I&#8217;ll have to keep an eye out for it.</p>

<p>As before the type constructors to enforce this safety are hidden and only accessible under specific and well-guarded conditions.</p>

<p><div>
<pre class="haskell"><span style="color: #5d478b; font-style: italic;">-- Not exported!</span>
<span style="color: #06c; font-weight: bold;">newtype</span> BrandedArray b a = BrandArray a
<span style="color: #06c; font-weight: bold;">newtype</span> BrandedInt   b i = BrandInt i
&nbsp;
brand :: Array -&gt; <span style="color: green;">&#40;</span><span style="color: #06c; font-weight: bold;">forall</span> b. <span style="color: green;">&#40;</span>BrandedArray b, BrandedInt b, BrandedInt b<span style="color: green;">&#41;</span> -&gt; w<span style="color: green;">&#41;</span>
               -&gt; w -&gt; w
brand arr cont stop = <span style="color: #06c; font-weight: bold;">if</span> lo &lt;= hi
                         <span style="color: #06c; font-weight: bold;">then</span> cont <span style="color: green;">&#40;</span>BrandArray, BrandInt lo, BrandInt hi<span style="color: green;">&#41;</span>
                         <span style="color: #06c; font-weight: bold;">else</span> stop</pre>
</div></p>

<p>The user provides functions to deal with the two cases that the bounds are valid and invalid. This same pattern follows through to other parts of the implementation: condition checking is done internally and the user provides functions to deal with various cases. This keeps some of the constraints (like the existential quantification over <code>b</code>, the brand type) wrapped up. If the user tries to subvert this the type checker objects:</p>

<p><div>
<pre>Inferred type is less polymorphic than expected
  Quantified type variable `b' escapes
In the second argument of `brand', namely `Just'
In the expression: brand testarray Just Nothing
In the definition of `it': it = brand testarray Just Nothing</pre>
</div></p>

<p>In order to implement the rest of binary search over arrays some arithmetic functions on indices are introduced. These functions exist inside the trusted kernel so some effort is made to ensure they are correct. They also require passing/failing functions for operations that may fail (such as finding the successor of a valid index: the successor may be out of bounds):</p>

<p><div>
<pre class="haskell"><span style="color: #5d478b; font-style: italic;">-- If i1 and i2 are within bounds then their midpoint</span>
<span style="color: #5d478b; font-style: italic;">-- is within bounds.</span>
bmiddle :: BrandedInt b -&gt; BrandedInt b -&gt; BrandedInt b
bmiddle i1 i2 = BrandInt <span style="color: green;">&#40;</span>i1 + <span style="color: green;">&#40;</span>i2 - i1<span style="color: green;">&#41;</span> `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:div"><span style="font-weight: bold;">div</span></a>` <span style="color: red;">2</span><span style="color: green;">&#41;</span>
&nbsp;
bsucc :: BrandedInt b -&gt; BrandedInt b
                      -&gt; <span style="color: green;">&#40;</span>BrandedInt b -&gt; w<span style="color: green;">&#41;</span>
                      -&gt; w -&gt; w
<span style="color: #5d478b; font-style: italic;">-- If i is less than the upper bound we use continue calculation</span>
<span style="color: #5d478b; font-style: italic;">-- with the inside else we fail with outside.</span>
bsucc <span style="color: green;">&#40;</span>BrandInt upper<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>BrandInt i<span style="color: green;">&#41;</span> inside outside = 
    <span style="color: #06c; font-weight: bold;">let</span> i' = i + <span style="color: red;">1</span>
    <span style="color: #06c; font-weight: bold;">in</span> <span style="color: #06c; font-weight: bold;">if</span> i' &lt;= upper <span style="color: #06c; font-weight: bold;">then</span> inside <span style="color: green;">&#40;</span>BrandInt i'<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">else</span> outside</pre>
</div></p>

<p>There is also a predecessor function which works analogously. First we need to rework the binary search for this style of programming:</p>

<p><div>
<pre class="haskell">bsearch :: <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Ord"><span style="color: #cccc00; font-weight: bold;">Ord</span></a> a<span style="color: green;">&#41;</span> =&gt; <span style="color: green;">&#40;</span>a -&gt; a -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Ordering"><span style="color: #cccc00; font-weight: bold;">Ordering</span></a><span style="color: green;">&#41;</span> -&gt; a -&gt; Array a -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Maybe"><span style="color: #cccc00; font-weight: bold;">Maybe</span></a> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a>,a<span style="color: green;">&#41;</span>
bsearch cmp key arr = brand arr find Nothing
  <span style="color: #06c; font-weight: bold;">where</span> find <span style="color: green;">&#40;</span>barr,lower,upper<span style="color: green;">&#41;</span> = go lower upper
    <span style="color: #06c; font-weight: bold;">where</span> go lo hi = <span style="color: #06c; font-weight: bold;">let</span> m = bmiddle lo hi
                         x = unsafeGet barr m
                     <span style="color: #06c; font-weight: bold;">in</span> <span style="color: #06c; font-weight: bold;">case</span> cmp key x <span style="color: #06c; font-weight: bold;">of</span>
                        LT -&gt; bpred lo m <span style="color: green;">&#40;</span>\i -&gt; go lo i<span style="color: green;">&#41;</span> Nothing
                        EQ -&gt; Just <span style="color: green;">&#40;</span>unbrand m, x<span style="color: green;">&#41;</span>
                        GT -&gt; bsucc hi m <span style="color: green;">&#40;</span>\i -&gt; go i hi<span style="color: green;">&#41;</span> Nothing</pre>
</div></p>

<p>On each iteration of the algorithm finding the value at the midpoint is always a safe operation provided the two arguments are safe. Incrementing or decrementing the upper/lower bound might fail if the index goes out of range, but this check can be considered part of the end condition, ie the search finishes unsuccessfully anyway if the index goes off the end because it&#8217;s an indication that the value we&#8217;re looking for is not stored.</p>

<p>The call to <code>unsafeGet</code> is, in fact, safe, since the index <code>m</code> is guaranteed to be in range for this array. No runtime checks are required over those needed to indicate the end condition. As before this problem is always dealt with formally using a similar mechanism for the empty-list problem, using <code>Strict</code> and <code>Lax</code> languages and mappings from one to the other. I&#8217;ll look at that next time.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dougalstanton.net/blog/index.php/2011/01/23/a-look-at-lightweight-static-capabilities-part-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A look at Lightweight Static Capabilities (part 2)</title>
		<link>http://www.dougalstanton.net/blog/index.php/2011/01/18/a-look-at-lightweight-static-capabilities-part-2/</link>
		<comments>http://www.dougalstanton.net/blog/index.php/2011/01/18/a-look-at-lightweight-static-capabilities-part-2/#comments</comments>
		<pubDate>Tue, 18 Jan 2011 17:24:37 +0000</pubDate>
		<dc:creator>Dougal</dc:creator>
				<category><![CDATA[Computing]]></category>
		<category><![CDATA[Maths & Computer Science]]></category>

		<guid isPermaLink="false">http://www.dougalstanton.net/blog/?p=1318</guid>
		<description><![CDATA[This is part 2 of a series of posts looking at the paper Lightweight Static Capabilities by Kiselyov and Shan. Part 1 can be found here.

The next section of the paper is more theoretical. The authors introduce two notional languages, Strict and Lax, and their typing mechanisms. Strict has a &#8220;fancy&#8221; type system which specifies [...]]]></description>
			<content:encoded><![CDATA[<p>This is part 2 of a series of posts looking at the paper Lightweight Static Capabilities by Kiselyov and Shan. <a href="http://www.dougalstanton.net/blog/index.php/2011/01/16/a-look-at-lightweight-static-capabilities-part-1/">Part 1 can be found here.</a></p>

<p>The next section of the paper is more theoretical. The authors introduce two notional languages, Strict and Lax, and their typing mechanisms. Strict has a &#8220;fancy&#8221; type system which specifies two list types (<code>List</code> and <code>List+</code>) and only the latter type can be deconstructed with <code>head</code> and <code>tail</code> functions. The <code>List+</code> type can only be created by a special <code>nonempty</code> conversion, which cannot be applied to empty lists.</p>

<p>The <code>Lax</code> language has no such advanced typing facility and will happily accept <code>nonempty(nil)</code> as a legal construct. This language is more like a traditional functional language with a possibly-empty list type. In terms of typable operations <code>Strict</code> defines a smaller set than <code>Lax</code>, and every legal <code>Strict</code> program is a legal <code>Lax</code> program too. The authors define a mapping from <code>Strict</code> to <code>Lax</code> programs to allow embedding a statically safe program in Haskell, ML, etc.</p>

<p>This embedding is ultimately what we always attempt to do when programming. The set of things which the compiler will accept is necessarily larger than the set of things which will get us the right answer. Type systems aim to constrain the &#8220;acceptable&#8221; set slightly though there is still plenty of scope for bugs. :-) Some programming languages make it easier to write programs which are dangerously but subtly wrong. Using the full capabilities of a language like C all the time is incredibly foolhardy: not every situation demands pointer arithmetic. If we can use the type system to wall off &#8220;dangerous&#8221; code then all the better. Another example is the use of IO actions or <code>unsafe*</code> operations in Haskell. These are often used only where necessary in the former case, or walled off in an opaque module in the latter case. This walling-off allows us to concentrate our verification efforts to areas which can cause the most problems.</p>

<p>The resulting &#8220;embedded&#8221; code is the <em>trusted kernel</em> mentioned in the previous post. The reduction rules defined for <code>Strict</code> and <code>Lax</code> are contained in this kernel. The reduction rules for the non-empty list case are:</p>

<p><div>
<pre>head (nonempty (cons E1 E2)) -&gt; E1
tail (nonempty (cons E1 E2)) -&gt; E2
indeed nil E1 E2             -&gt; E1
indeed (cons E E') E1 E2     -&gt; E2 (nonempty (cons E E'))</pre>
</div></p>

<p>The first two rules define the operation of <code>head</code> and <code>tail</code> on a non-empty list. These operations aren&#8217;t defined for empty lists. The second two rules show how the non-empty type is constructed if the list is truly non-empty. Notice the similarity between <code>indeed</code> and Haskell&#8217;s <code>maybe</code> function (with arguments rearranged):</p>

<p><div>
<pre class="haskell">indeed :: List a -&gt; b -&gt; <span style="color: green;">&#40;</span>FullList a -&gt; b<span style="color: green;">&#41;</span> -&gt; b
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:maybe"><span style="font-weight: bold;">maybe</span></a> :: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Maybe"><span style="color: #cccc00; font-weight: bold;">Maybe</span></a> a -&gt; b -&gt; <span style="color: green;">&#40;</span>a          -&gt; b<span style="color: green;">&#41;</span> -&gt; b</pre>
</div></p>

<p>The <code>maybe</code> function forces the user to deal with all cases in the option type by requiring functions and default values for each case. The <code>indeed</code> function goes one step further by not allowing any other way to take apart the list type &#8212; in order to use <code>head</code> and <code>tail</code> you have to convert the <code>List</code> to a <code>FullList</code> and only <code>indeed</code> can do that.</p>

<p>In the spirit of the <code>maybe</code> function for <code>Maybe</code> types and the <code>either</code> function for <code>Either</code> types it would seem sensible to have a <code>list</code> function for <code>List</code> types, which would take a function for each case suggested by a list. After a bit of reflection I think this is the <code>fold</code>, or rather <code>maybe</code> and <code>either</code> are specific folds.</p>

<p>Which all suggests that the toy example provided (reversing a linked list) can be safely recreated using a fold. If we look again at the safe version defined in the <a href="http://www.dougalstanton.net/blog/index.php/2011/01/16/a-look-at-lightweight-static-capabilities-part-1/">previous post</a> we see two cases we have to deal with:</p>

<p><div>
<pre class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse"><span style="font-weight: bold;">reverse</span></a> :: List a -&gt; List a -&gt; List a
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse"><span style="font-weight: bold;">reverse</span></a> xs acc = <span style="color: #06c; font-weight: bold;">case</span> full xs <span style="color: #06c; font-weight: bold;">of</span>
            Nothing -&gt; acc
            Just ls -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse"><span style="font-weight: bold;">reverse</span></a> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:tail"><span style="font-weight: bold;">tail</span></a> ls<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>cons <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:head"><span style="font-weight: bold;">head</span></a> ls<span style="color: green;">&#41;</span> acc<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Stripping away all the plumbing stuff that&#8217;s going on here, we have two vital bits:</p>

<ol>
<li>The return of the accumulator when the list is no longer non-empty, ie is empty.</li>
<li>Consing the head of the list onto the accumulator at each stage that the list is non-empty.</li>
</ol>

<p>Step 1 is done automatically by the fold: it lets you iterate over values without being concerned about them running out. Step 2 is just the <code>cons</code> operation we know, with the order of arguments reversed. To actually reverse a list we supply an empty accumulator.</p>

<p><div>
<pre class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse"><span style="font-weight: bold;">reverse</span></a> <span style="color: #06c; font-weight: bold;">in</span> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a> combiner acc <span style="color: #06c; font-weight: bold;">in</span>
  <span style="color: #06c; font-weight: bold;">where</span> combiner xs x = cons x xs
        acc = <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span></pre>
</div></p>

<p>We know we can define this simple function as a one-liner but not all functions are as simple. The next section looks at array indexing operations. The motivating example this time is binary search over arrays &#8212; checking that all indexing operations remain in-bounds and that these constraints are ensured by the type system. By contrast with list reversal a binary search deals with several separate elements (the array, the lower and upper index) which cannot be hidden completely if we are to allow general array operations.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dougalstanton.net/blog/index.php/2011/01/18/a-look-at-lightweight-static-capabilities-part-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>A look at Lightweight Static Capabilities (part 1)</title>
		<link>http://www.dougalstanton.net/blog/index.php/2011/01/16/a-look-at-lightweight-static-capabilities-part-1/</link>
		<comments>http://www.dougalstanton.net/blog/index.php/2011/01/16/a-look-at-lightweight-static-capabilities-part-1/#comments</comments>
		<pubDate>Sun, 16 Jan 2011 20:10:29 +0000</pubDate>
		<dc:creator>Dougal</dc:creator>
				<category><![CDATA[Computing]]></category>
		<category><![CDATA[Maths & Computer Science]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.dougalstanton.net/blog/?p=1310</guid>
		<description><![CDATA[I started to look at the Lightweight Static Capabilities paper over lunch on Friday. This is far too big an paper to consume in half an hour and I&#8217;m a long way from understanding it all. I thought the best way to increase my understanding was to write down my thoughts on and understanding of [...]]]></description>
			<content:encoded><![CDATA[<p>I started to look at the <a href="http://okmij.org/ftp/Computation/lightweight-dependent-typing.html#Lightweight" title="Lightweight static capabilities, Kiselyov and Shan">Lightweight Static Capabilities</a> paper over lunch on Friday. This is far too big an paper to consume in half an hour and I&#8217;m a long way from understanding it all. I thought the best way to increase my understanding was to write down my thoughts on and understanding of each section as I come to it. (The examples here are given in Haskell-like syntax, with fewer obscure operators. Conseuqently I&#8217;ve used the type name <code>List a</code> instead of <code>[a]</code> and <code>cons</code> instead of infix <code>:</code>.)</p>

<p>The area the paper looks at is labeled &#8220;lightweight dependent typing&#8221; &#8212; dependent typing without dependent types, essentially. Dependent type systems allow the programmer to specify types based on the values, so the programmer can ensure that lists will necessarily be non-empty and denominators necessarily non-zero, among other useful examples. The paper looks at ways of creating type safe &#8220;capabilities&#8221; without dependent types.</p>

<p>I will take the paper a little bit at a time, and as I read on I might have to correct previous misreadings/misinterpretations. The first motivating example given on page 2 provides a program to reverse a list, building up the result in an accumulator.</p>

<p><div>
<pre class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse"><span style="font-weight: bold;">reverse</span></a> :: List a -&gt; List a -&gt; List a
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse"><span style="font-weight: bold;">reverse</span></a> ls acc = <span style="color: #06c; font-weight: bold;">if</span> nil ls
                    <span style="color: #06c; font-weight: bold;">then</span> acc
                    <span style="color: #06c; font-weight: bold;">else</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse"><span style="font-weight: bold;">reverse</span></a> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:tail"><span style="font-weight: bold;">tail</span></a> ls<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>cons <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:head"><span style="font-weight: bold;">head</span></a> ls<span style="color: green;">&#41;</span> acc<span style="color: green;">&#41;</span></pre>
</div></p>

<p>The standard list destructors <code>head</code> and <code>tail</code> are partial functions. They are not defined for the empty list and will produce a runtime error if accidentally applied to the empty list. For this reverse function we can be reasonably certain that it won&#8217;t blow up at runtime, but it&#8217;s a very simple function and we can&#8217;t get the same assurances in all situations. Let&#8217;s call these standard <code>head</code> and <code>tail</code> functions by more explicit names:</p>

<p><div>
<pre class="haskell">unsafeHead :: List a -&gt; a
unsafeTail :: List a -&gt; List a</pre>
</div></p>

<p>In order to provide static guarantees that all uses of <code>head</code> and <code>tail</code> are safe a new type called <code>FullList</code> is introduced which is a rebranded standard list type. The key element is that <code>head</code> and <code>tail</code> can only operate on <code>FullList</code> types, and a guarantee is provided that values in this type are non-empty lists. With this static assurance we can actually use our unsafe functions from earlier:</p>

<p><div>
<pre class="haskell"><span style="color: #5d478b; font-style: italic;">-- unfull converts a FullList to an ordinary List type</span>
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:head"><span style="font-weight: bold;">head</span></a> :: FullList a -&gt; a
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:head"><span style="font-weight: bold;">head</span></a> = unsafeHead . unfull
&nbsp;
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:tail"><span style="font-weight: bold;">tail</span></a> :: FullList a -&gt; List a
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:tail"><span style="font-weight: bold;">tail</span></a> = unsafeTail . unfull</pre>
</div></p>

<p>The <code>FullList</code> type becomes a one-use token which the value has when it moves through the program. This token can only be created in one place (which we can lock inside a separate module). We can easily control under what conditions this token is assigned:</p>

<p><div>
<pre class="haskell"><span style="color: #06c; font-weight: bold;">module</span> <span style="color: green;">&#40;</span>FullList, unFull, full<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span>
&nbsp;
<span style="color: #06c; font-weight: bold;">newtype</span> FullList a = Full <span style="color: green;">&#123;</span> unFull :: List a <span style="color: green;">&#125;</span>
&nbsp;
full :: List a -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Maybe"><span style="color: #cccc00; font-weight: bold;">Maybe</span></a> <span style="color: green;">&#40;</span>FullList a<span style="color: green;">&#41;</span>
full ls = <span style="color: #06c; font-weight: bold;">if</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:null"><span style="font-weight: bold;">null</span></a> ls <span style="color: #06c; font-weight: bold;">then</span> Nothing <span style="color: #06c; font-weight: bold;">else</span> Just <span style="color: green;">&#40;</span>Full ls<span style="color: green;">&#41;</span></pre>
</div></p>

<p>To verify that <code>head</code> and <code>tail</code> are always &#8220;safe&#8221; we must only verify that a <code>FullList</code> is always non-empty. The paper goes deeper into the formal verification than I&#8217;m comfortable talking about (at least at the moment) but it should be reasonable to say that there is only one line of code, the definition of <code>full</code>, which needs examined.</p>

<p>This is the <em>kernel of trust</em> concept that the paper revolves around. Shrinking down this kernel means it&#8217;s easier to examine and verify; and from there the type system extends our trust to the rest of the program.</p>

<p>From the user perspective, the type system forces us to check that lists are not empty before invoking <code>head</code> or other unsafe list operations:</p>

<p><div>
<pre class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse"><span style="font-weight: bold;">reverse</span></a> :: List a -&gt; List a -&gt; List a
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse"><span style="font-weight: bold;">reverse</span></a> xs acc = <span style="color: #06c; font-weight: bold;">case</span> full xs <span style="color: #06c; font-weight: bold;">of</span>
            Nothing -&gt; acc
            Just ls -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse"><span style="font-weight: bold;">reverse</span></a> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:tail"><span style="font-weight: bold;">tail</span></a> ls<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>cons <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:head"><span style="font-weight: bold;">head</span></a> ls<span style="color: green;">&#41;</span> acc<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Ideally we&#8217;d be doing this <em>anyway</em> so there isn&#8217;t a greater burden &#8212; but it does provide us with compilation errors if we forget to do it. (From personal experience the use of an option type like <code>Maybe</code> makes it much harder to forget these things, even before getting the compiler error. And calling <code>fromJust</code> or other unsafe extraction method feels immoral.)</p>

<p>This takes us up to Section 2.1, with only one omission. The paper at this point switches from explicit wrapping and unwrapping of option types to continuation-passing style. The option types are straightforward but slow things down. Depending on how things go I might follow suit and use continuation-passing from here on, but it might prove a better learning experience to convert to option types. We&#8217;ll see when I come to write up the next section.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dougalstanton.net/blog/index.php/2011/01/16/a-look-at-lightweight-static-capabilities-part-1/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>hsflickr upload fails if tags list supplied is non-empty</title>
		<link>http://www.dougalstanton.net/blog/index.php/2011/01/07/hsflickr-upload-fails-if-tags-list-supplied-is-non-empty/</link>
		<comments>http://www.dougalstanton.net/blog/index.php/2011/01/07/hsflickr-upload-fails-if-tags-list-supplied-is-non-empty/#comments</comments>
		<pubDate>Fri, 07 Jan 2011 11:47:49 +0000</pubDate>
		<dc:creator>Dougal</dc:creator>
				<category><![CDATA[Bugs]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.dougalstanton.net/blog/index.php/2011/01/07/hsflickr-upload-fails-if-tags-list-supplied-is-non-empty/</guid>
		<description><![CDATA[My recent post on Flickr authorisation mentioned a problem I was having trying to upload images. I was receiving an &#8220;invalid signature&#8221; message but only when attempting to upload images. Other actions (looking for new uploads, searching for tags) were all performed without trouble.

The upload fails if you supply a non-empty list of tags to [...]]]></description>
			<content:encoded><![CDATA[<p>My recent post on Flickr authorisation mentioned a problem I was having trying to upload images. I was receiving an &#8220;invalid signature&#8221; message but only when attempting to upload images. Other actions (looking for new uploads, searching for tags) were all performed without trouble.</p>

<p>The upload fails if you supply a non-empty list of tags to the hsflickr library. In short, uploading images works like this:</p>

<p><div>
<pre class="haskell">uploadPhoto filename title description tags attributes</pre>
</div></p>

<p>The upload will fail if the <code>tags</code> argument is non-null. That is, supplying tags at the point you upload the image will result in an error. Thankfully the workaround is short:</p>

<p><div>
<pre class="haskell">photoID &lt;- uploadPhoto filename title description <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> attributes
addTags photoID tags</pre>
</div></p>

<p>Supply an empty list of tags for the initial upload, and when that succeeds use the resulting photo ID to set the real tags afterwards.</p>

<p>When I work out what the underlying issue is I&#8217;ll post a patch here (and punt one upstream).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dougalstanton.net/blog/index.php/2011/01/07/hsflickr-upload-fails-if-tags-list-supplied-is-non-empty/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>We&#8217;ve had &#8220;Blog post title&#8221;, now we have {{TITLE}}</title>
		<link>http://www.dougalstanton.net/blog/index.php/2010/12/03/weve-had-blog-post-title-now-we-have-title/</link>
		<comments>http://www.dougalstanton.net/blog/index.php/2010/12/03/weve-had-blog-post-title-now-we-have-title/#comments</comments>
		<pubDate>Fri, 03 Dec 2010 13:16:13 +0000</pubDate>
		<dc:creator>Dougal</dc:creator>
				<category><![CDATA[Bugs]]></category>
		<category><![CDATA[Humour]]></category>

		<guid isPermaLink="false">http://www.dougalstanton.net/blog/?p=1289</guid>
		<description><![CDATA[I feel like I&#8217;m just posting stupid screenshots at the moment, but this is the second time E-ON have done this to me so there&#8217;s no mercy.



Come on, form mail isn&#8217;t difficult! Get your act together, and check the damn things before you hit send.
]]></description>
			<content:encoded><![CDATA[<p>I feel like I&#8217;m just posting stupid screenshots at the moment, but this is the second time E-ON have done this to me so there&#8217;s no mercy.</p>

<p><a href="http://www.flickr.com/photos/thederelictpavilion/5229013674/" title="E-ON form email by Dougal Stanton, on Flickr"><img src="http://farm6.static.flickr.com/5088/5229013674_a2ef98f940_o.png" width="586" height="441" alt="E-ON form email" /></a></p>

<p>Come on, form mail isn&#8217;t difficult! Get your act together, and check the damn things before you hit send.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dougalstanton.net/blog/index.php/2010/12/03/weve-had-blog-post-title-now-we-have-title/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Taking stock of learning and beetroot cake</title>
		<link>http://www.dougalstanton.net/blog/index.php/2010/12/01/taking-stock-of-learning-and-beetroot-cake/</link>
		<comments>http://www.dougalstanton.net/blog/index.php/2010/12/01/taking-stock-of-learning-and-beetroot-cake/#comments</comments>
		<pubDate>Wed, 01 Dec 2010 15:46:31 +0000</pubDate>
		<dc:creator>Dougal</dc:creator>
				<category><![CDATA[Computing]]></category>
		<category><![CDATA[Food]]></category>
		<category><![CDATA[Maths & Computer Science]]></category>

		<guid isPermaLink="false">http://www.dougalstanton.net/blog/?p=1285</guid>
		<description><![CDATA[Severely snowed out today so SICP study group was cancelled. I&#8217;m using the time in the house to make stock with the bag full of lamb bones and bits that have been sitting in the freezer for many months. I think I will make some kind of soup with it later, preferably one with lots [...]]]></description>
			<content:encoded><![CDATA[<p>Severely snowed out today so SICP study group was cancelled. I&#8217;m using the time in the house to make stock with the bag full of lamb bones and bits that have been sitting in the freezer for many months. I think I will make some kind of soup with it later, preferably one with lots of chunky vegetables and other interesting bits. I&#8217;ve also got a bunch of beetroot in the fridge which I intend to make into <a href="http://www.bbcgoodfood.com/recipes/7663/blitzandbake-beetroot-and-chocolate-cake">beetroot and chocolate cake</a>, because it was so tasty last time I made it. (And I want to do it in a cooler oven since 190&deg;C blackened the outside without cooking through when I made it before. That was the only occasion when the skewer test has been useful to me.)</p>

<p>Back to the topic of the study group. Reading SICP is deceptively easy at times. Each step is a simple progression from the last, such that each idea seems obvious and trivial. Then suddenly some trivial new concept makes no sense at all and you find yourself backtracking through pages of explanation to find some firm handhold from which to start moving forward again. Most of the time I feel that I&#8217;m not learning anything but I realised today that some things which were not intuitive in the past are now familiar and natural. I was reading <a href="http://homepages.inf.ed.ac.uk/wadler/topics/monads.html#arrows-jfp">The Arrow Calculus</a> and realised that I could understand all of the notation and type rules for lambda calculus and arrows given. It was the environment stuff in particular that felt &#8220;obvious&#8221; in the way that it wouldn&#8217;t have in the past, and I&#8217;ve been doing a lot of interpreter writing and environment-jigging in recent weeks with SICP. It&#8217;s all coming together.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dougalstanton.net/blog/index.php/2010/12/01/taking-stock-of-learning-and-beetroot-cake/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Hurling dwarves over other dwarves: probably not legal</title>
		<link>http://www.dougalstanton.net/blog/index.php/2010/11/21/hurling-dwarves-over-other-dwarves-probably-not-legal/</link>
		<comments>http://www.dougalstanton.net/blog/index.php/2010/11/21/hurling-dwarves-over-other-dwarves-probably-not-legal/#comments</comments>
		<pubDate>Sun, 21 Nov 2010 23:26:27 +0000</pubDate>
		<dc:creator>Dougal</dc:creator>
				<category><![CDATA[Bugs]]></category>
		<category><![CDATA[Games]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.dougalstanton.net/blog/?p=1266</guid>
		<description><![CDATA[It&#8217;s been a quiet week on the programming front but I&#8217;ve made steady changes to Thud since committing the initial code to BitBucket ten days ago.

I&#8217;ve added some unit tests, which was a simple thing to do but not very easy to get right. Unit tests are a bit like Cluedo, in that you have [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been a quiet week on the programming front but I&#8217;ve made steady changes to Thud since committing the initial code to BitBucket ten days ago.</p>

<p>I&#8217;ve added some unit tests, which was a simple thing to do but not very easy to get right. Unit tests are a bit like Cluedo, in that you have to convince yourself of something very complicated with a few well-chosen questions. And it&#8217;s easy to forget your assumptions and miss something vital.</p>

<p>Just today I spotted something by playing with the game. I realised it was possible to hurl or shove a piece <em>over</em> another to reach the enemy on the far side. The logic only checked that the target was within reach but not that it was within proper line of sight. (This goes back to the original implementation too, so it&#8217;s not just a feature of my mucking about with the code that deals with the game rules and breaking something.) The rules are silent on whether this is even allowed (I suspect not) so I&#8217;ve disallowed it for now.</p>

<p>This is not the kind of bug I would have caught with unit tests because it would never have occurred to me to check for it. <em>Of course the butler didn&#8217;t commit the murder, that&#8217;s preposterous!</em> I only spotted it during play because I was purposely lining up foes next to each other in order to manually test their capture capabilities and realised that I could hurl a dwarf further than necessary&#8230; and the game allowed it.</p>

<p>So I&#8217;ve been repeatedly rewriting the file that deals with game rules so that scenarios like this become more obvious. I&#8217;m getting to the point where I&#8217;m happy with this small segment so I need to push on to other things.</p>

<p>As I mentioned on another post it&#8217;s nice having an issue tracker for the code so I can write down any ideas that occur to me, whether they&#8217;re short-term or long-term concerns. I think it&#8217;s time to look at some of the more medium-term goals now, such as network interaction, otherwise I&#8217;ll spend the rest of my days endlessly fiddling with the game rules.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dougalstanton.net/blog/index.php/2010/11/21/hurling-dwarves-over-other-dwarves-probably-not-legal/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Blog post title</title>
		<link>http://www.dougalstanton.net/blog/index.php/2010/11/16/blog-post-title/</link>
		<comments>http://www.dougalstanton.net/blog/index.php/2010/11/16/blog-post-title/#comments</comments>
		<pubDate>Tue, 16 Nov 2010 16:08:38 +0000</pubDate>
		<dc:creator>Dougal</dc:creator>
				<category><![CDATA[Bugs]]></category>
		<category><![CDATA[Humour]]></category>

		<guid isPermaLink="false">http://www.dougalstanton.net/blog/?p=1263</guid>
		<description><![CDATA[Description of screenshot included in blog post:


]]></description>
			<content:encoded><![CDATA[<p>Description of screenshot included in blog post:</p>

<p><img src="http://farm2.static.flickr.com/1295/5181536407_b151089204_z.jpg" class="show" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dougalstanton.net/blog/index.php/2010/11/16/blog-post-title/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Content decrement register</title>
		<link>http://www.dougalstanton.net/blog/index.php/2010/09/11/content-decrement-register/</link>
		<comments>http://www.dougalstanton.net/blog/index.php/2010/09/11/content-decrement-register/#comments</comments>
		<pubDate>Sat, 11 Sep 2010 22:17:41 +0000</pubDate>
		<dc:creator>Dougal</dc:creator>
				<category><![CDATA[Computing]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.dougalstanton.net/blog/?p=1161</guid>
		<description><![CDATA[This is going to seem like a point-out-the-obvious post, where I go to great pains to point out just how very wet water can be, but I assure you, this is new enough to me that I had to reach this conclusion.

Okay, so Scheme typically works on the everything-is-a-tuple philosophy. Other structures can be built [...]]]></description>
			<content:encoded><![CDATA[<p>This is going to seem like a point-out-the-obvious post, where I go to great pains to point out just how very wet water can be, but I assure you, this is new enough to me that I had to reach this conclusion.</p>

<p>Okay, so Scheme typically works on the everything-is-a-tuple philosophy. Other structures can be built out of nested or chained tuples. A tuple, or pair, looks like this:</p>

<p><div>
<pre class="scheme"><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span></pre>
</div></p>

<p>Immutable linked lists can be made by inserting one pair inside the second part of another pair, with an empty marker at the end.</p>

<p><div>
<pre class="scheme"><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span>,null<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre>
</div></p>

<p>The functions for accessing the left and right elements in a tuple are called, for historical reasons, <code>car</code> and <code>cdr</code>. Applying <code>car</code> to <code>(1,2)</code> gives you the 1, and applying <code>cdr</code> gives you the 2.</p>

<p>When applied to the linked list implementation, <code>car</code> and <code>cdr</code> are no longer really <em>left</em> and <em>right</em> but <em>head</em> and <em>tail</em>. And so for the past few months working on SICP I&#8217;ve been thinking about <code>car</code> as <code>head</code> and <code>cdr</code> as <code>tail</code> and mostly getting away with it.</p>

<p>But sooner or later the inaccuracy of this correspondence was going to become clear, and the other day it did. Single linked lists point from the head to the tail but not in the other direction. From each element you can delve deeper but you cannot move higher up the chain. An obvious reason for this is that there may not be a single &#8220;higher up&#8221; to link to. Immutable list tails can be shared.</p>

<p><a href="http://www.flickr.com/photos/mkmabus/3392683152/" title="Scheming by The Doctr, on Flickr"><img class="show" src="http://farm4.static.flickr.com/3471/3392683152_21c4f5044d.jpg" width="500" height="333" alt="Scheming" /></a></p>

<p>Double linked lists can be traversed in either direction. If you&#8217;ve only got two places to store something (left and right elements in a pair) and three things to store (the left side of the list, the right side of the list and current element) you need to double up somewhere. At which point it is necessary to remember that <code>cdr</code> isn&#8217;t the same thing as <code>tail</code>.</p>

<p>Which makes me wonder why the idiomatic way of traversing lists is with <code>car</code> and <code>cdr</code>? Why is the meaning of <code>tail</code> intentionally blurred with the meaning of <code>cdr</code>? SICP puts a lot of emphasis on abstraction of implementation from intent yet it still litters its list-manipulation code with <code>car</code> and <code>cdr</code>, references to the underlying representation that doesn&#8217;t have anything to do with the meaning of the lists at all.</p>

<p>Of course we may as well ask why <code>cdr</code>, an acronym for &#8220;Contents of the Decrement part of Register number&#8221;, itself is a reference to an implementation detail on a 1950s computer. I&#8217;m sure <em>left</em> and <em>right</em> or something slightly more meaningful wouldn&#8217;t have hurt.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dougalstanton.net/blog/index.php/2010/09/11/content-decrement-register/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

