<?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>Playing With Pointers</title>
	<atom:link href="http://playingwithpointers.com/feed" rel="self" type="application/rss+xml" />
	<link>http://playingwithpointers.com</link>
	<description>&#34;The habit of expression leads to the search for something to express.&#34;</description>
	<lastBuildDate>Tue, 01 May 2012 09:50:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>An Interesting CPP Bug</title>
		<link>http://playingwithpointers.com/archives/792</link>
		<comments>http://playingwithpointers.com/archives/792#comments</comments>
		<pubDate>Sat, 31 Mar 2012 17:15:45 +0000</pubDate>
		<dc:creator>Sanjoy</dc:creator>
				<category><![CDATA[Digital Dope]]></category>
		<category><![CDATA[bugs]]></category>
		<category><![CDATA[c macros]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[macros]]></category>

		<guid isPermaLink="false">http://playingwithpointers.com/?p=792</guid>
		<description><![CDATA[The C preprocessor, being a dumb text-manipulating tool that does not understand the syntactic structure of the underlying program, is often a hiding place for subtle headache-inducing bugs. I've been bitten such bugs myself and one bug I had a particularly difficult debugging is often not mentioned in lists such as [1]. Can you spot [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">The C preprocessor, being a dumb text-manipulating tool that does not understand the syntactic structure of the underlying program, is often a hiding place for subtle headache-inducing bugs.  I've been bitten such bugs myself and one bug I had a particularly difficult debugging is often not mentioned in   lists such as [1].</p>
<p align="justify">Can you spot a bug in the following CPP macro?    Specifically, when is its behaviour ill-defined?</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#define FROBNICATE(__frobptr) do {              \
    struct frobnicable *fptr = (__frobptr);     \
    assert(fptr &amp;&amp; &quot;Can't frobnicate NULL!&quot;);   \
    frobnicate_init(fptr);                      \
    frobnicate_finish(fptr);                    \
  } while(0)</span></pre></div></div>

<p align="justify">One interesting thing I discovered about C while debugging this macro is that variable initializations like the   following are valid:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">int</span> foo <span style="color: #000080;">=</span> foo<span style="color: #008080;">;</span>
  <span style="color: #ff0000; font-style: italic;">/* foo now has an unspecified value. */</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p align="justify">which means the following piece of code won't  do what   is expected of it:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">struct</span> frobnicable <span style="color: #000040;">*</span>fptr <span style="color: #000080;">=</span> <span style="color: #0000dd;">malloc</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">struct</span> frobnicable<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  fptr<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>x <span style="color: #000080;">=</span> <span style="color: #0000dd;">42</span><span style="color: #008080;">;</span>
  FROBNICATE<span style="color: #008000;">&#40;</span>fptr<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p align="justify">The macro will expand to</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">do</span> <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">struct</span> frobnicable <span style="color: #000040;">*</span>fptr <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>fptr<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #ff0000; font-style: italic;">/* ... and so on ...  `fptr` is undefined in the rest of the 
   * macro expansion. */</span>
<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p align="justify">and the implications won't be pleasant.</p>
<p align="justify">I haven't yet discovered an elegant and effective way to deal with this issue.  The <code>struct frobnicable *fptr = (__frobptr)</code> is needed to prevent multiple evaluations of <code>__fptr</code>, so the only way I see of preventing this bug is by using an extremely unusual name for the <code>fptr</code> variable (or following a convention of prefixing such variable with some previously defined string).  But that is neither elegant nor foolproof.</p>
<p>--</p>
<p>[1] <a href="http://spin.atomicobject.com/2012/03/30/preprocessing-with-caution/">http://spin.atomicobject.com/2012/03/30/preprocessing-with-caution/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://playingwithpointers.com/archives/792/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Faking Dependent Types in C++</title>
		<link>http://playingwithpointers.com/archives/756</link>
		<comments>http://playingwithpointers.com/archives/756#comments</comments>
		<pubDate>Thu, 29 Mar 2012 09:07:00 +0000</pubDate>
		<dc:creator>Sanjoy</dc:creator>
				<category><![CDATA[Digital Dope]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[type theory]]></category>

		<guid isPermaLink="false">http://playingwithpointers.com/?p=756</guid>
		<description><![CDATA[It is possible to have some kind of dependent typing in C++ using templates. The "solution" isn't elegant or usable, but I found it mildly interesting so I'll try to explain it here. The C++ type system with templates is Turing complete, so that something like this can be done shouldn't be a surprise. The [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">It is possible to have some kind of dependent typing in C++ using templates.  The "solution" isn't elegant or usable, but I found it mildly interesting so I'll try to explain it here.  The C++ type system with templates is Turing complete, so that something like this can be done shouldn't be a surprise.  The entire source file is here [1].</p>
<p align="justify">We will need to convert <code>int</code>s to types and vice versa (we'll soon see why), so we might as well get that out of the way. One way to convert an <code>int</code> to a type is by exploiting the fact that templates are allowed to take integral values as parameters:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">&nbsp;
<span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">int</span> N<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">struct</span> Integer <span style="color: #008000;">&#123;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">typedef</span> Integer<span style="color: #000080;">&lt;</span><span style="color: #0000dd;">0</span><span style="color: #000080;">&gt;</span> Zero<span style="color: #008080;">;</span>
<span style="color: #0000ff;">typedef</span> Integer<span style="color: #000080;">&lt;</span><span style="color: #0000dd;">1</span><span style="color: #000080;">&gt;</span> One<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// ... and so on</span></pre></div></div>

<p align="justify">The issue with this is the lack of control over the types we're producing.  In this particular case, we want to prevent (at compile time) the creation of types corresponding to negative integers.  We do this by following Peano's definition of natural numbers:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> T<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">struct</span> S <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">typedef</span> T Previous<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">enum</span> <span style="color: #008000;">&#123;</span> ToInt <span style="color: #000080;">=</span> T<span style="color: #008080;">::</span><span style="color: #007788;">ToInt</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span> <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">struct</span> Z <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">enum</span> <span style="color: #008000;">&#123;</span> ToInt <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span> <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">int</span> N<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">struct</span> ToType <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">typedef</span> S<span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> ToType<span style="color: #000080;">&lt;</span>N <span style="color: #000040;">-</span> <span style="color: #0000dd;">1</span><span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">TypeValue</span> <span style="color: #000080;">&gt;</span> TypeValue<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;&gt;</span>
<span style="color: #0000ff;">struct</span> ToType<span style="color: #000080;">&lt;</span><span style="color: #0000dd;">0</span><span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">typedef</span> Z TypeValue<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #339900;">#define P(n) typename n::Previous</span>
<span style="color: #339900;">#define TO_INT(n) n::ToInt</span></pre></div></div>

<p align="justify">In this case, <code>S&lt;S&lt;S&lt;Z &gt; &gt; &gt;</code> represents <code>3</code> and it is obvious that using <code>S</code> and <code>Z</code>, it is possible to enumerate only positive integers.</p>
<p align="justify">Secondly, we want some mechanism which to use to "lift" normal integers into a corresponding type.  This is done by <code>ToType</code>.  The recursion is simple, for <code>0</code>, <code>ToType</code> "returns" <code>Z</code> and for <code>N</code>, it "returns" <code>S&lt;</code> the "return value" of <code>ToType&lt;N - 1&gt;</code> <code>&gt;</code>.  <code>P</code> and <code>TO_INT</code> are macros to save typing.</p>
<p align="justify">Note that we've not really <em>solved</em> the problem of creation of negative integers -- <code>ToType&lt;-50&gt;::TypeValue</code> will throw a "template depth exceeds maximum of XYZ" error and not a proper compile time error.  All this mechanism does is isolate the issue of negative numbers to within <code>ToType</code> so that the rest of the code can work with a cleaner representation.  I think this can be improved:</p>
<ul>
<li>Create a dummy <code>struct ZeroFlag</code> with one     <code>int</code> template     paramenter.</li>
<li>Specialize <code>ZeroFlag</code> on <code>0</code> and add a         static field <code>DummyF</code> to it.</li>
<li>Create a <code>struct Abs</code> that computes the absolute value of its <code>int</code> template parameter.</li>
<li>Add <code>typedef</code> <code>ZeroFlag&lt;Abs&lt;N&gt;::Value &gt;::DummyF</code> to <code>ToType</code>.</li>
</ul>
<p align="justify">This should give a compile time error on instantiating <code>ToType&lt;0&gt;</code>.  However, right now, we concentrate elsewhere.  Importantly, check the <code>Previous</code> typedef in <code>struct S</code>, which isn't present when we specialize in <code>struct Z</code> -- <code>Previous</code> will be our main weapon in generating compile time errors, as we shall see.</p>
<p align="justify">We now create a "linked list" which keeps track of the number of nodes in it at compile time:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// N denotes the size of the list here</span>
<span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> T, <span style="color: #0000ff;">typename</span> N<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">struct</span> List <span style="color: #008000;">&#123;</span>
  T data<span style="color: #008080;">;</span>
  List<span style="color: #000080;">&lt;</span>T, P<span style="color: #008000;">&#40;</span>N<span style="color: #008000;">&#41;</span><span style="color: #000080;">&gt;</span> next<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> T<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">struct</span> List<span style="color: #000080;">&lt;</span>T, Z<span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#123;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p align="justify">We don't need to have a pointer to a node since the <code>next</code> field always <em>points</em> to a <em>smaller</em> type -- there is no circularity (and hence no possibility of a loop in the linked list).  I'm not exactly sure of this, but I think, at least in terms of memory layout, <code>List&lt;100&gt;</code> should be equivalent to an array of <code>100</code> <code>int</code>s.</p>
<p align="justify">We can actually write up a <code>foldl</code> for this case.  First we declare the type of a generic function (we have to use <code>struct</code>s everywhere because C++ does not allow partial template specialization on functions):</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> A, <span style="color: #0000ff;">typename</span> B, <span style="color: #0000ff;">typename</span> C<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">struct</span> Func2 <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">typedef</span> C functionType<span style="color: #008000;">&#40;</span>A, B<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>Now, to fold a type <code>T</code>, we need a function of type <code>A->T->A</code> and an initial value of type <code>A</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> T, <span style="color: #0000ff;">typename</span> N<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">struct</span> List <span style="color: #008000;">&#123;</span>
  T data<span style="color: #008080;">;</span>
  List<span style="color: #000080;">&lt;</span>T, P<span style="color: #008000;">&#40;</span>N<span style="color: #008000;">&#41;</span><span style="color: #000080;">&gt;</span> next<span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> A<span style="color: #000080;">&gt;</span>
  A foldl<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">typename</span> Func2<span style="color: #000080;">&lt;</span>A, T, A<span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">functionType</span> f, A a<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">return</span> next.<span style="color: #007788;">foldl</span><span style="color: #008000;">&#40;</span>f, f<span style="color: #008000;">&#40;</span>a, data<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> T<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">struct</span> List<span style="color: #000080;">&lt;</span>T, Z<span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> A<span style="color: #000080;">&gt;</span>
  A foldl<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">typename</span> Func2<span style="color: #000080;">&lt;</span>A, T, A<span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">functionType</span> f, A a<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">return</span> a<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p align="justify">I was surprised how close C++ can get to Haskell at this point.</p>
<p align="justify">Right now we have a dependently typed (only in a loose sense of the term -- I've not had enough rigour with type systems to formally claim this) linked list in C++.  We can now write a "safe" sorting function using this:</p>
<p align="justify">(I've used a shitty algorithm here to focus on the things this post is actually supposed to focus on.)</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> T, <span style="color: #0000ff;">typename</span> N<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">struct</span> Sorter <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> sort<span style="color: #008000;">&#40;</span>List<span style="color: #000080;">&lt;</span>T, N<span style="color: #000080;">&gt;</span> <span style="color: #000040;">&amp;</span>list<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    List<span style="color: #000080;">&lt;</span>T, P<span style="color: #008000;">&#40;</span>N<span style="color: #008000;">&#41;</span><span style="color: #000080;">&gt;</span> rest <span style="color: #000080;">=</span> list.<span style="color: #007788;">next</span><span style="color: #008080;">;</span>
    Sorter<span style="color: #000080;">&lt;</span>T, P<span style="color: #008000;">&#40;</span>N<span style="color: #008000;">&#41;</span><span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">sort</span><span style="color: #008000;">&#40;</span>rest<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>list.<span style="color: #007788;">data</span> <span style="color: #000080;">&gt;</span> rest.<span style="color: #007788;">data</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
      std<span style="color: #008080;">::</span><span style="color: #007788;">swap</span><span style="color: #008000;">&#40;</span>rest.<span style="color: #007788;">data</span>, list.<span style="color: #007788;">data</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
      Sorter<span style="color: #000080;">&lt;</span>T, P<span style="color: #008000;">&#40;</span>N<span style="color: #008000;">&#41;</span><span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">sort</span><span style="color: #008000;">&#40;</span>rest<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
    list.<span style="color: #007788;">next</span> <span style="color: #000080;">=</span> rest<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// List of length 1 is already sorted.</span>
<span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> T<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">struct</span> Sorter<span style="color: #000080;">&lt;</span>T, S<span style="color: #000080;">&lt;</span>Z<span style="color: #000080;">&gt;</span> <span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> sort<span style="color: #008000;">&#40;</span>List<span style="color: #000080;">&lt;</span>T, S<span style="color: #000080;">&lt;</span>Z<span style="color: #000080;">&gt;</span> <span style="color: #000080;">&gt;</span> <span style="color: #000040;">&amp;</span>list<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p align="justify">The cute part about this code is, if the specialization <code>Sorter&lt;T, S&lt;Z&gt; &gt;</code> is removed you get a <em>compile time</em> error, since the compiler tries to sort a list of on element using the general template which then tries to sort a list of zero elements.  Since the general template tries to access the <code>Previous</code> field (via the <code>P</code> macro), you get a compile time error.  That the code compiles is "proof" that your algorithm will not try to walk the past of an empty linked list.</p>
<p align="justify">It is easy to create a <code>struct ListCreator</code> which creates a linked list of a specified length and printing is a matter of folding with the function </p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> T<span style="color: #000080;">&gt;</span>
std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span><span style="color: #000080;">&lt;</span>T<span style="color: #000080;">&gt;</span> accumulate<span style="color: #008000;">&#40;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span><span style="color: #000080;">&lt;</span>T<span style="color: #000080;">&gt;</span> in, T t<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
  in.<span style="color: #007788;">push_back</span><span style="color: #008000;">&#40;</span>t<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">return</span> in<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p align="justify">and an empty <code>std::vector</code> and print the resulting vector.  The exact code is in [1].</p>
<p align="justify">The entire list, the sorting function could all be implemented <em>inside</em> the template system, by the way.  However, in that case the elements of the list would all be baked in; in the present situation, we have the option of reading (a compile-time-constant obviously) number of entries from a file or from the user.  I think more interesting applications will be seen in "lifting" a partial amount of data and algorithms into the type system, providing a fuzzier boundary between what  programmers have to keep in their heads and what the type system proves for them.</p>
<p align="justify">For dessert, let's see how you can implement compile-time bound checking on the list.  First a template to assert the less-than relation:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> A, <span style="color: #0000ff;">typename</span> B<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">struct</span> Lt <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">typename</span> Lt<span style="color: #000080;">&lt;</span>P<span style="color: #008000;">&#40;</span>A<span style="color: #008000;">&#41;</span>, P<span style="color: #008000;">&#40;</span>B<span style="color: #008000;">&#41;</span><span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">Flag</span> Flag<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> B<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">struct</span> Lt<span style="color: #000080;">&lt;</span>Z, S<span style="color: #000080;">&lt;</span>B<span style="color: #000080;">&gt;</span> <span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">bool</span> Flag<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p align="justify">Essentially <code>A &lt; B</code> iff <code>Lt&lt;A, B&gt;::Flag</code> exists.  The non-existence of <code>Flag</code> can be made a compile-time error by <code>typedef</code>ing it in the template which requires the relation to be valid.  So, in this case, <code>Z</code> is less than all integers which can be written as <code>S&lt;A&gt;</code> for some <code>A</code> and <code>A</code> is less than <code>B</code> iff <code>A - 1</code> is less than <code>B -   1</code>.  Notice that these two axioms are both necessary and sufficient for comparing any two positive integers.  Also note how we are inductively asserting in the general case -- since we <code>typedef</code> <code>Lt&lt;P(A), P(B)&gt;::Flag</code> to <code>Flag</code>, <code>Lt&lt;A, B&gt;</code> will have <code>Flag</code> <code>typedef</code>ed iff <code>Lt&lt;P(A),     P(B)&gt;</code> has it <code>typedef</code>ed.  Stating a correct indexing operation is now trivial:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> T, <span style="color: #0000ff;">typename</span> N<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">struct</span> Idx<span style="color: #000080;">&lt;</span>T, N, Z<span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">typename</span> Lt<span style="color: #000080;">&lt;</span>Z, N<span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">Flag</span> Flag<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">static</span> T idx<span style="color: #008000;">&#40;</span>List<span style="color: #000080;">&lt;</span>T, N<span style="color: #000080;">&gt;</span> list<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">return</span> list.<span style="color: #007788;">data</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>--</p>
<p>[1] https://github.com/sanjoy/Snippets/blob/master/DependentTypes.cc</p>
]]></content:encoded>
			<wfw:commentRss>http://playingwithpointers.com/archives/756/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>TSA in the sky with diamonds</title>
		<link>http://playingwithpointers.com/archives/729</link>
		<comments>http://playingwithpointers.com/archives/729#comments</comments>
		<pubDate>Fri, 23 Mar 2012 10:34:19 +0000</pubDate>
		<dc:creator>Sanjoy</dc:creator>
				<category><![CDATA[Offline Bytes]]></category>
		<category><![CDATA[drugs]]></category>
		<category><![CDATA[kgp]]></category>
		<category><![CDATA[tsa]]></category>

		<guid isPermaLink="false">http://playingwithpointers.com/?p=729</guid>
		<description><![CDATA[An anti-drug article [1] was written and published by some IIT Kharagpur students in one of our campus newspapers. I think the article is rubbish and here's why. I think the article should, in principle, offend any self-respecting individual due to its tone and any rational person due to the "facts" it presents. From this [...]]]></description>
			<content:encoded><![CDATA[<p align="justify"><em>An anti-drug article [1] was written and published by some IIT Kharagpur students in one of our campus newspapers.  I   think the article is rubbish and here's why.</em></p>
<p align="justify">I think the article should, in principle, offend any self-respecting individual due to its tone and any rational person due to the "facts" it presents.  From this point on, I tacitly assume that people reading this anti-(anti-drug-rant) rant posses both   rationality and self-respect to some extent.</p>
<p align="justify">One interesting thing about the article is that after replacing "drug use" with "gay marriage" the reasons still remain "valid", and become strikingly similar to what anti-gay lobbyists routinely come up with.  The illegality of two men loving each other, the separation from normal society, "underground" gay groups and "the gateway effect".  One of the reasons routinely cited in support of anti-gay laws is that if we allow two women to marry, men will start sleeping with dogs and sisters will engage in incest.   Needless to say, such fears are as baseless as the gateway drug theory   [2] the authors talk about.</p>
<p align="justify">Perhaps the most objectionable aspect of this article the way the authors <em>instruct</em> their reader on the "right" way to lead their lives.  Whether someone spends their free time reading Shakespeare or worshipping bananas is entirely for themselves to decide.  Advice can only be given conditionally, "If you want to be an astronaut eating zombie then do the following ... ", or in this particular case "If you're giving advice to self-respecting people ...".  Unconditional advice speaks poorly of how the author view people around them even more poorly of TSA for publishing such an   article.</p>
<p align="justify">Getting down to the factual aspects:</p>
<p align="justify">It is true that most nations ban drugs, but more and more countries are taking steps towards legalization.  London [4] is re-thinking their drug laws and Portugal decriminalized drug use [5] over a decade ago.  In our own country we have licensed Bhang Shops [3].  Cocaine and heroin were both legal for several decades -- does it mean it was "okay" to use them back then?  The issue is, legality isn't always a good indicator of whether a particular activity is harmful or not.  Even practically, the only country I have heard of where such drug-laws are strictly enforced is the US of A, and I don't think the reader will need to be told that their notion of what to "fix" and what not to is a bit skewed.  Personally, no one in my social circle ever got into any legal trouble for smoking up   once in a while.</p>
<p align="justify">The authors mention legality again, and their argument is flawed for the reason already mentioned.  Going by this logic, DC++, anything but regular sex, homosexuals (in West Bengal)  should all be avoided.</p>
<p align="justify">Keeping secrets from some people is not the same as getting disowned from society.  Moreover, people choose friends and social circles based on their opinions, not the other way   around</p>
<p align="justify">You can't vote in this country if you mind sponsoring corruption. :) Plus, at least in West Bengal, marijuana is grown locally by extremely poor people.  If anything, by buying pot you're ensuring they have enough to eat for a day.</p>
<p align="justify">I agree with the authors on <em>one</em> thing and one thing only -- presenting a distorted picture of drug use, mentioning the good while leaving out the bad and risky is never ethical.  In fact, I find this article unethical for doing the exact opposite, which is just as   bad.</p>
<p align="justify">Fact is, however the authors try to pretend to be omniscient beings who transcend time and space, they're are just like the readers they're trying to advise and I don't think they know any better.  People have a right to choose what they do with their lives; giving out unsolicited advice is vulgar and  revolting.</p>
<p align="justify">Does this mean I condone drug usage?  Well, it doesn't matter what I think.  Drugs can ruin your life, be the most meaningful experience you take out of this phase of your life or just something you do once in a while.  Your life is yours to make and   destroy, and I don't get a say in it.</p>
<p align="justify">PS: This isn't a personal attack against anyone.<br />
<br />PPS: I don't contribute to any multiple-continent-spanning newspaper.</p>
<p>--</p>
<p>[1] <a href="http://www.scholarsavenue.org/2012/03/17/an-open-letter/">http://www.scholarsavenue.org/2012/03/17/an-open-letter/</a><br />
[2] <a href="http://en.wikipedia.org/wiki/Gateway">http://en.wikipedia.org/wiki/Gateway</a><br />
[3] <a href="http://en.wikipedia.org/wiki/Bhang">http://en.wikipedia.org/wiki/Bhang</a><br />
[4] <a href="http://www.telegraph.co.uk/news/uknews/law-and-order/4581743/Now-Home-Office-drugs-adviser-wants-to-downgrade-LSD-from-A-to-B.html">http://www.telegraph.co.uk/news/uknews/law-and-order/4581743/Now-Home-Office-drugs-adviser-wants-to-downgrade-LSD-from-A-to-B.html</a><br />
[5] <a href="http://www.huffingtonpost.com/2011/07/03/portugal-drug-laws-decriminalization-_n_889531.html">http://www.huffingtonpost.com/2011/07/03/portugal-drug-laws-decriminalization-_n_889531.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://playingwithpointers.com/archives/729/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Type-Safe Tic-tac-toe</title>
		<link>http://playingwithpointers.com/archives/716</link>
		<comments>http://playingwithpointers.com/archives/716#comments</comments>
		<pubDate>Sat, 10 Mar 2012 17:49:47 +0000</pubDate>
		<dc:creator>Sanjoy</dc:creator>
				<category><![CDATA[Digital Dope]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[tic-tac-toe]]></category>
		<category><![CDATA[type-system]]></category>

		<guid isPermaLink="false">http://playingwithpointers.com/?p=716</guid>
		<description><![CDATA[I recently had occasion to screw around Haskell's type system. The end goal was to produce a Tic-tac-toe game in which all type-correct moves are legal and where all illegal moves produce type errors. I decided call the following moves illegal attempts to move on a cell already occupied attempts to make a move on [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">I recently had occasion to screw around Haskell's type system.  The end goal was to produce a Tic-tac-toe game in which all type-correct moves are legal and where all illegal moves produce   type errors.  I decided call the following moves <em>illegal</em></p>
<ul>
<li>attempts to move on a cell already occupied</li>
<li>attempts to make a move on a game that has already been won</li>
</ul>
<p align="justify">While it does not make sense to compile the program into an executable the game is quite "playable" from the <tt>ghci</tt>   REPL.   The code is up on github [1].</p>
<p align="justify">In fact, Haskell's type system, with the <tt>UndecidableInstances</tt> extension is Turing complete, so it isn't a surprise that such a thing is possible.  But type level programming has its own set of rules and I had a lot of fun figuring things out in such a foreign domain (<em>Type-Level Instant Insanity</em> [2] was   extremely helpful).  This post is about some of the things I observed.</p>
<p><strong>Tuples are the type-level lists</strong></p>
<p align="justify">This seems obvious now that I say it, since a tuple is the most straightforward way to represent a list of types.  However, the direct way to do so is not very useful -- instead of encoding the types <tt>A</tt>, <tt>B</tt>, <tt>C</tt> as the tuple type <tt>(A, B, C)</tt> it is clearer to represent them using the usual recursive cons idiom: <tt>(A, (B, (C, ())))</tt>.  Here <tt>()</tt> is the <em>nil</em> element and <tt>cons X Y</tt> is <tt>(X, Y)</tt>.</p>
<p><strong>Functional dependencies</strong></p>
<p align="justify">Haskell normally doesn't allow typeclasses on more than one type.  Adding proper functional dependencies can fix this [3].  A multi-parameter typeclass is essentially a n-ary relation between types and a functional dependency constrains the relations that a multi-parameter typeclass can represent.  This has interesting   consequences when programming with types.</p>
<p align="justify">The functional dependency in <tt>X a b c | a b -> c</tt> constrains the relation it defines by forcing <tt>c</tt>  to be a function of <tt>a</tt> and <tt>b</tt>.  This can be used to defined type-level functions.  Specifically, a function that maps two types to a third type can be represented just like the typeclass <tt>X</tt> in the example above.  The <em>body</em> of the function can then be defined as a bunch of instance declarations.  If you're doing something like Peano arithmetic and representing, for instance, <tt>2</tt> by the type <tt>S (S ())</tt> (<tt>S</tt> is a type constructor here), you can implement addition as shown below:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #5d478b; font-style: italic;">{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies,
             FlexibleInstances, UndecidableInstances #-}</span>
&nbsp;
<span style="color: #06c; font-weight: bold;">data</span> S x
&nbsp;
<span style="color: #06c; font-weight: bold;">class</span> Add x y z <span style="color: #339933; font-weight: bold;">|</span> x y <span style="color: #339933; font-weight: bold;">-&gt;</span> z <span style="color: #06c; font-weight: bold;">where</span> add <span style="color: #339933; font-weight: bold;">::</span> x <span style="color: #339933; font-weight: bold;">-&gt;</span> y <span style="color: #339933; font-weight: bold;">-&gt;</span> z
<span style="color: #06c; font-weight: bold;">instance</span> Add <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span> x x <span style="color: #06c; font-weight: bold;">where</span> add <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">undefined</span>
<span style="color: #06c; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span>Add x y z<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=&gt;</span> Add <span style="color: green;">&#40;</span>S x<span style="color: green;">&#41;</span> y <span style="color: green;">&#40;</span>S z<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span> add <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">undefined</span></pre></div></div>

<p align="justify">The functional dependency in <tt>Add</tt> states that the <em>output</em> of <tt>Add</tt> is a function of the first two types, which is correct.  Note how we're building up the   induction.</p>
<p align="justify">I had a hard time figuring out how predicates like "two things are <em>foo</em> if they are not <em>bar</em>" are to be represented.  The reverse was easy, you use a context -- <tt>instance (Foo x, Foo y) => Bar x y</tt>, but there isn't any way in Haskell to say <tt>instance (NOT Foo x, Not Foo y) => Bar x y</tt>.  If you think about it, there is no reason to have such a feature either -- the only reason for saying "if <tt>x</tt> is in the typeclass <tt>X</tt> then it is also in the typeclass <tt>Y</tt>" is to be able to use <tt>X</tt>'s functions in defining <tt>Y</tt>'s, so having "<tt>x</tt> is not in the typeclass <tt>X</tt>" in the context is absolutely useless!  The solution (which I read in [2]) is to instead have such predicates "return" a type (which is a <em>value</em> in this weird world we're working in).  For better readability, defining two types, <tt>data T</tt> and <tt>data F</tt> for this purpose is useful.  [1], for instance, is littered with <tt>instance</tt>s like</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #5d478b; font-style: italic;">-- When a is X then it isn't Y and vice versa.  Silly example, but I</span>
<span style="color: #5d478b; font-style: italic;">-- think it demonstrates the point</span>
&nbsp;
<span style="color: #06c; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span>X a T<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=&gt;</span> Y a F
<span style="color: #06c; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span>X a F<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=&gt;</span> Y a T</pre></div></div>

<p><strong>Inverted Induction</strong></p>
<p align="justify">Finally, the way you write "function bodies" at the type level is a bit different.  Instead of breaking down a function into smaller bits and then recursion on those, you conditionally build smaller structures into larger structures.  For instance, to check if an element is in a list, you say "if the object <tt>o</tt> is in a list <tt>l</tt> or if <tt>x == o</tt>then it is in the list <tt>cons x l</tt> for all x".  It isn't any different really, once you get used   to it.</p>
<p align="justify">--</p>
<p>[1] <a href="https://raw.github.com/sanjoy/Snippets/master/TicTacToe.hs">https://raw.github.com/sanjoy/Snippets/master/TicTacToe.hs</a><br />
[2] <a href="http://www.haskell.org/wikiupload/d/dd/TMR-Issue8.pdf">http://www.haskell.org/wikiupload/d/dd/TMR-Issue8.pdf</a><br />
[3] <a href="http://www.haskell.org/haskellwiki/Functional_dependencies">http://www.haskell.org/haskellwiki/Functional_dependencies</a></p>
]]></content:encoded>
			<wfw:commentRss>http://playingwithpointers.com/archives/716/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[Proof] λ→ is normalizing</title>
		<link>http://playingwithpointers.com/archives/690</link>
		<comments>http://playingwithpointers.com/archives/690#comments</comments>
		<pubDate>Tue, 28 Feb 2012 16:21:20 +0000</pubDate>
		<dc:creator>Sanjoy</dc:creator>
				<category><![CDATA[Digital Dope]]></category>
		<category><![CDATA[Offline Bytes]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[programming language theory]]></category>
		<category><![CDATA[programming languages]]></category>
		<category><![CDATA[types]]></category>

		<guid isPermaLink="false">http://playingwithpointers.com/?p=690</guid>
		<description><![CDATA[I studied a proof today that I had been putting off for some time now; that a simply typed lambda calculus is normalizing -- every well-typed expression eventually reduces to a value. The proof is quite clever and that the exact wording of the proof in Types and Programming Languages is a bit terse so [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">I studied a proof today that I had been putting off for some time now; that a simply typed lambda calculus is normalizing -- every well-typed expression eventually reduces to a value.  The proof is quite clever and that the exact wording of the proof in <em>Types and Programming   Languages</em> is a bit terse so I think it will make for a good, juicy blog post.</p>
<p align="justify">In case you haven't already, you need to first read up (a little bit) on untyped and typed lambda calculus.  Try reading to the point where you can comfortably state why <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_cff40dd2d5b0f1bd4274511668d7fbda.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt=" \lambda x \cdot x x " /></span><script type='math/tex'> \lambda x \cdot x x </script> can not be well-typed.  I'll wait right here, I promise. :)</p>
<p align="justify">I'll use the symbol <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_8a8fecd1b89546843c402c2d2d3a7050.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" [x \mapsto s] t " /></span><script type='math/tex'> [x \mapsto s] t </script> to mean "replace instances of <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_6722c218a6f30869ef6886dc4b050a37.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt=" x " /></span><script type='math/tex'> x </script> by <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_793d6602f044affad0290fdc4f61ce36.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt=" s " /></span><script type='math/tex'> s </script> in the term <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_2f76c9194ebc4dbee0c1614dbdfa3c25.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt=" t " /></span><script type='math/tex'> t </script>".  We'll talk about a typed lambda calculus which has exactly one type <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_951196ca354c5c72b8356494f97c3b5d.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" A " /></span><script type='math/tex'> A </script>   and only one value of that type, <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_e49736f09a17efd3daec360132426f43.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt=" a " /></span><script type='math/tex'> a </script>.  We'll evaluate in normal order.</p>
<p align="justify">Firstly, note (informally) that all types will look like <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_e044410d710ae523902d0fd9d6241aeb.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" ((A \to A) \to (A \to A)) \to (A \to A) " /></span><script type='math/tex'> ((A \to A) \to (A \to A)) \to (A \to A) </script>.  It is not hard to see (or prove) that the set of types is isomorphic to a set of binary trees with every internal node a <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_6e748544e55cb51c43b00855b1bcb0ec.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt=" \to " /></span><script type='math/tex'> \to </script> and every leaf an <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_951196ca354c5c72b8356494f97c3b5d.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" A " /></span><script type='math/tex'> A </script>.  This is important since we'll do structural induction on the binary   tree corresponding to a type.</p>
<p align="justify">We now define a set of what are usually called reducibility candidates.  They are a predicate on terms of the lambda   calculus with the following conditions</p>
<ul>
<li><span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_8d606bfa8e461091ec7cdb0530a8ae6a.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" R_{A}(t) " /></span><script type='math/tex'> R_{A}(t) </script> if <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_2f76c9194ebc4dbee0c1614dbdfa3c25.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt=" t " /></span><script type='math/tex'> t </script> halts.</li>
<li><span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_5dcfbd4bd5ab5d1945179b623e8b3611.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" R_{T_{1} \to T_{2}}(t) " /></span><script type='math/tex'> R_{T_{1} \to T_{2}}(t) </script> <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_de1321be4dc7de01ce8599103bec8fce.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" \iff " /></span><script type='math/tex'> \iff </script> <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_2f76c9194ebc4dbee0c1614dbdfa3c25.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt=" t " /></span><script type='math/tex'> t </script> halts and <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_fdfd4060ae8ba2eca38fade94b9444b5.gif' style='vertical-align: middle; border: none; ' class='tex' alt="     R_{T_{1}}(s) \Rightarrow R_{T_{2}}(t s) " /></span><script type='math/tex'>     R_{T_{1}}(s) \Rightarrow R_{T_{2}}(t s) </script></li>
</ul>
<p align="justify">Since <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_3bcf3dbb506487c9de1eb167791ff8a7.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" R_T " /></span><script type='math/tex'> R_T </script> is a predicate, we can safely say   things like <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_6722c218a6f30869ef6886dc4b050a37.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt=" x " /></span><script type='math/tex'> x </script> is <em>in</em> <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_9708f55875aa1b4739a39e08f72e7391.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" R_A " /></span><script type='math/tex'> R_A </script> etc.</p>
<p align="justify">We aim to prove the following</p>
<ol>
<li>Every term in a set <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_9682275743feffecc76263b18b3fa4ad.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" R_{T} " /></span><script type='math/tex'> R_{T} </script> (for any type <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_39300282214603edc7b46a69b156bdcb.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt=" T " /></span><script type='math/tex'> T </script>) is normalizable.  Note that this follows from the definition of the     reducibility candidates</li>
<li>Every term of type <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_39300282214603edc7b46a69b156bdcb.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt=" T " /></span><script type='math/tex'> T </script> is in <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_9682275743feffecc76263b18b3fa4ad.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" R_{T} " /></span><script type='math/tex'> R_{T} </script>.</li>
</ol>
<p align="justify">These two, combined, will allow us to draw the conclusion that every well-typed term in our simply typed lambda   calculus halts.</p>
<p align="justify"><strong>Lemma 0: Every term in a reducibility     candidate halts</strong></p>
<p align="justify">This follows directly from the definition of a   reducibility candidate.</p>
<p align="justify"><strong>Lemma 1: Reduction does not change the     reducibility status of   a term</strong></p>
<p align="justify">Formally, we try to prove <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_7909e8ceb006d3647b0f06c075b95dbd.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" R_T(t) \wedge (t \to t') \iff R_T(t') " /></span><script type='math/tex'> R_T(t) \wedge (t \to t') \iff R_T(t') </script>.  For this, we use the structural induction on binary trees isomorphic to each type we talked about above.  There are two cases.</p>
<p align="justify"><u>T is the atom type</u></p>
<p align="justify">This is the <em>base case</em> for our       induction and also pretty obvious.  From <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_987276d257ba2e0b7f013d0d14b035f5.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" t \to t' " /></span><script type='math/tex'> t \to t' </script> we conclude <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_2f76c9194ebc4dbee0c1614dbdfa3c25.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt=" t " /></span><script type='math/tex'> t </script> halts iff <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_0a7a99751a9fd52baeebd2999e922979.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" t' " /></span><script type='math/tex'> t' </script> halts.  Since the only condition to be in <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_9708f55875aa1b4739a39e08f72e7391.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" R_A " /></span><script type='math/tex'> R_A </script> is halting, <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_b02099af2f120f0c96bf1683ced18413.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" R_A(t) \iff R_A(t') " /></span><script type='math/tex'> R_A(t) \iff R_A(t') </script>.</p>
<p align="justify"><u>T is a function type</u></p>
<p align="justify">Let <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_d1c91a16eb1b97390adea6bd1b56163c.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" T \equiv T_1 \to T_2 " /></span><script type='math/tex'> T \equiv T_1 \to T_2 </script>.  In this case, each <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_0a7a99751a9fd52baeebd2999e922979.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" t' " /></span><script type='math/tex'> t' </script> has to satisfy the additional condition <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_729b60702e51388ae5ca4589157b286e.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" \forall s \in R_{T_1} \cdot R_{T_2}(t'\;s)   " /></span><script type='math/tex'> \forall s \in R_{T_1} \cdot R_{T_2}(t'\;s)   </script>.  We begin by taking an arbitrary <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_793d6602f044affad0290fdc4f61ce36.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt=" s " /></span><script type='math/tex'> s </script> in <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_392b3f6003a41541ec5649e8a6203d77.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" R_{T_1} " /></span><script type='math/tex'> R_{T_1} </script>.</p>
<p align="justify">Notice how <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_b127bd2421ef2c59acd07a621f9311f2.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" t \; s \to t' \; s " /></span><script type='math/tex'> t \; s \to t' \; s </script> [1]?  Also notice how <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_2b2ab5d721bf76536fa5fbd981a975a4.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" T_2 " /></span><script type='math/tex'> T_2 </script> is a substructure of <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_94cd26c79ad2ef54ab5d91445d9440bc.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" T \equiv (T_1 \to T_2) " /></span><script type='math/tex'> T \equiv (T_1 \to T_2) </script>?  We can invoke structural induction here and say we already know <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_c02d930af1842dedba9988a034687505.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" R_{T_2}(t \; s) \Rightarrow R_{T_2}(t' \; s) " /></span><script type='math/tex'> R_{T_2}(t \; s) \Rightarrow R_{T_2}(t' \; s) </script> (<em>statement 0</em>).  Also, since we're given <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_016678743a8c28a1922bde6d921a350b.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" R_T(t) " /></span><script type='math/tex'> R_T(t) </script>, we know for all <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_793d6602f044affad0290fdc4f61ce36.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt=" s " /></span><script type='math/tex'> s </script> in <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_392b3f6003a41541ec5649e8a6203d77.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" R_{T_1} " /></span><script type='math/tex'> R_{T_1} </script>, <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_bcac02c9ccb7f067c92569a3bdafbab2.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt=" t \; s " /></span><script type='math/tex'> t \; s </script> is in <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_e2a3692ec0c0f4510c7b12f7075baec8.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" R_{T_2} " /></span><script type='math/tex'> R_{T_2} </script> (property of <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_3bcf3dbb506487c9de1eb167791ff8a7.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" R_T " /></span><script type='math/tex'> R_T </script>) (<em>statement 1</em>).  We can conclude from <em>statement 0</em> and <em>statement 1</em> that for any arbitrary <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_793d6602f044affad0290fdc4f61ce36.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt=" s " /></span><script type='math/tex'> s </script> in <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_392b3f6003a41541ec5649e8a6203d77.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" R_{T_1} " /></span><script type='math/tex'> R_{T_1} </script>, we have <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_ac3f0219654b9716608d2bec354e5cbd.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" R_{T_2} (t' \; s) " /></span><script type='math/tex'> R_{T_2} (t' \; s) </script>.  This is the extra condition we needed to show <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_0a7a99751a9fd52baeebd2999e922979.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" t' " /></span><script type='math/tex'> t' </script> is in <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_3bcf3dbb506487c9de1eb167791ff8a7.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" R_T " /></span><script type='math/tex'> R_T </script>.</p>
<p align="justify"><strong>Lemma 2: Every well typed term of type T is     in <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_3bcf3dbb506487c9de1eb167791ff8a7.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" R_T " /></span><script type='math/tex'> R_T </script> </strong></p>
<p align="justify">This is proved by proving a stronger assertion: if <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_0edac9bb7099e78c0a266ac339850ac8.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" x_1:T_1, ..., x_n:T_n \vdash t \; : \; T " /></span><script type='math/tex'> x_1:T_1, ..., x_n:T_n \vdash t \; : \; T </script> and <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_62d2c5ebab4d9bd525e0a5ef0aa4ae4d.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" \emptyset \vdash v_i : T_i " /></span><script type='math/tex'> \emptyset \vdash v_i : T_i </script> for all <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_72f8ab13f56f855e098e0ea6e73251c1.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt=" i " /></span><script type='math/tex'> i </script> then <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_b3d67e7988c42580add445fed8efbe57.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" R_T([x_1 \mapsto v_1] ... [x_n   \mapsto v_n]t) " /></span><script type='math/tex'> R_T([x_1 \mapsto v_1] ... [x_n   \mapsto v_n]t) </script>.  This is a stronger assertion because this also   applies to terms with free variables while, at the <em>top level</em>, we'll need to consider only closed terms.  The assertion will be invoked on open terms only via induction.</p>
<p align="justify">To prove this assertion, we'll invoke induction on the <em>size</em> of a term.  So, informally, <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_ae1771ca9e96c66ef89666c9a4b09089.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" t_1 \; t_2 " /></span><script type='math/tex'> t_1 \; t_2 </script> is   greater than either <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_f79826c4b070ff558ed05090af42a4fe.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" t_1 " /></span><script type='math/tex'> t_1 </script> or <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_3f03acdbd3d49d286e819fd28b8023a7.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" t_2 " /></span><script type='math/tex'> t_2 </script> and <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_4c276e2086089f622d2ba77beec9a544.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt=" \lambda a \cdot x " /></span><script type='math/tex'> \lambda a \cdot x </script> is greater than <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_6722c218a6f30869ef6886dc4b050a37.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt=" x " /></span><script type='math/tex'> x </script>.  We make a case by case analysis on the various forms <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_2f76c9194ebc4dbee0c1614dbdfa3c25.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt=" t " /></span><script type='math/tex'> t </script> can be in</p>
<p align="justify"><u>A variable</u></p>
<p align="justify">This is obvious, since <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_916a548c09eec5d99e6137cbdee46057.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" t = x_i " /></span><script type='math/tex'> t = x_i </script> for some <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_72f8ab13f56f855e098e0ea6e73251c1.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt=" i " /></span><script type='math/tex'> i </script>.</p>
<p align="justify"><u>An application</u></p>
<p align="justify">This too is easy.  Since <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_05640c3a603587d0c0efe71f27774c37.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" t \equiv t_1 \; t_2 " /></span><script type='math/tex'> t \equiv t_1 \; t_2 </script>, we invoke induction on <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_f79826c4b070ff558ed05090af42a4fe.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" t_1 " /></span><script type='math/tex'> t_1 </script> and <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_3f03acdbd3d49d286e819fd28b8023a7.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" t_2 " /></span><script type='math/tex'> t_2 </script> separately and then use  <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_25782625a309e8caf2ed9dc55b64158e.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" ([a \mapsto b]c) ([a \mapsto b]d) \equiv ([a \mapsto b] (c \; d)) " /></span><script type='math/tex'> ([a \mapsto b]c) ([a \mapsto b]d) \equiv ([a \mapsto b] (c \; d)) </script> to get our result.</p>
<p align="justify"><u>An abstraction</u></p>
<p align="justify">We know, from its form that <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_2f76c9194ebc4dbee0c1614dbdfa3c25.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt=" t " /></span><script type='math/tex'> t </script> is a  value.  The other thing left to prove is that, keeping types compatible, <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_c4a8b1ac47ba0a32b0a005bbb19c046c.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" \forall s \in R_S \cdot R_M(t \; s) " /></span><script type='math/tex'> \forall s \in R_S \cdot R_M(t \; s) </script>.  <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_2f76c9194ebc4dbee0c1614dbdfa3c25.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt=" t " /></span><script type='math/tex'> t </script> is of the form <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_df783232fa2bc2a15eef03cd94225dc6.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" \lambda x : S \cdot b " /></span><script type='math/tex'> \lambda x : S \cdot b </script>.  Let <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_640eab5a602373c658013fd743d6ad72.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" s \in R_S " /></span><script type='math/tex'> s \in R_S </script>.  We know, from lemma 0 that <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_793d6602f044affad0290fdc4f61ce36.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt=" s " /></span><script type='math/tex'> s </script> will reduce to a value <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_c4f4b9b0ab0a2eb771bf7decb3a53c8d.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt=" v " /></span><script type='math/tex'> v </script>.  From lemma 1, we get <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_e983cda19a160270362bc6f3f4b32067.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" R_S(v) " /></span><script type='math/tex'> R_S(v) </script>.  The condition we're trying to prove is <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_8c84669ebaaa780cd171e24e4dd9dc4a.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" ([x_1 \mapsto v_1]...[x_n \mapsto v_n]t)s \in R_P " /></span><script type='math/tex'> ([x_1 \mapsto v_1]...[x_n \mapsto v_n]t)s \in R_P </script>.  But this beta-reduces to <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_b6c12a13aa473c9eb458ce100398d58e.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" ([x_1 \mapsto v_1]...[x_n \mapsto v_n](t \; s)) " /></span><script type='math/tex'> ([x_1 \mapsto v_1]...[x_n \mapsto v_n](t \; s)) </script> <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_6e748544e55cb51c43b00855b1bcb0ec.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt=" \to " /></span><script type='math/tex'> \to </script> <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_df1cc77bff04eb9ab47d35cbc8dbeb07.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" ([x_1 \mapsto v_1]...[x_n \mapsto v_n](t \; v)) " /></span><script type='math/tex'> ([x_1 \mapsto v_1]...[x_n \mapsto v_n](t \; v)) </script> <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_6e748544e55cb51c43b00855b1bcb0ec.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt=" \to " /></span><script type='math/tex'> \to </script> <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_34ff311ffbfadc3b79587b7404fdf78f.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" ([x_1 \mapsto v_1]...[x_n \mapsto v_n]([x \mapsto v]b) " /></span><script type='math/tex'> ([x_1 \mapsto v_1]...[x_n \mapsto v_n]([x \mapsto v]b) </script>.  <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_301d251459701f60c27be3229f1c4122.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt=" b " /></span><script type='math/tex'> b </script> is a <em>smaller</em> term satisfying the requirements for this proposition, and we can say, via induction, <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_b1dacdb650daec4824294730e2243a45.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" R_P([x_1 \mapsto v_1]...[x_n \mapsto v_n]([x \mapsto v]b) " /></span><script type='math/tex'> R_P([x_1 \mapsto v_1]...[x_n \mapsto v_n]([x \mapsto v]b) </script>.  From lemma 1 we get <span class='MathJax_Preview'><img src='http://playingwithpointers.com/wp-content/plugins/latex/cache/tex_998e25d68247b6bb6fb5764eac755642.gif' style='vertical-align: middle; border: none; ' class='tex' alt=" R_P(([x_1 \mapsto v_1]...[x_n \mapsto   v_n]t)s) " /></span><script type='math/tex'> R_P(([x_1 \mapsto v_1]...[x_n \mapsto   v_n]t)s) </script>, which is what we wanted to prove.</p>
<p align="justify">--</p>
<ol>
<li>In normal order.  I haven't tried proving this in other evaluation strategies but I think I can go around by proving that the evaluation strategy does not matter in deciding the behaviour of a         program in this lambda calculus.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://playingwithpointers.com/archives/690/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Books, movies and music</title>
		<link>http://playingwithpointers.com/archives/669</link>
		<comments>http://playingwithpointers.com/archives/669#comments</comments>
		<pubDate>Thu, 02 Feb 2012 11:36:32 +0000</pubDate>
		<dc:creator>Sanjoy</dc:creator>
				<category><![CDATA[Offline Bytes]]></category>

		<guid isPermaLink="false">http://playingwithpointers.com/?p=669</guid>
		<description><![CDATA[I graduated out of high school an idiot. I hadn't read anything, seen anything or heard anything (Everybody anyone?). Now that I'm becoming moderately literate in such things, I think I can make a generalization -- the information content (sorry Dr. Shannon, I'm going to abuse that term here) of an art form is inversely [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">I graduated out of high school an idiot.  I hadn't read anything, seen anything or heard anything (<em>Everybody</em> anyone?).  Now that I'm becoming moderately literate in such things, I think I can make a generalization -- the information content (sorry Dr. Shannon, I'm going to abuse that term here) of an art form is   inversely proportional to the effect it has on me.</p>
<p align="justify">Contrast a book with a movie, for instance.  The information a book contains is certainly more abstract than the information a movie rams into my head.  I don't know how <em>Naoko</em> in <em>Norwegian Wood</em> looked like but <em>Martha</em> always conjures up the image of Helena Bontham Carter in a hat, smoking a freshly lit cigarette.   <em>Naoko</em>'s image is malleable and ethereal but <em>Martha</em> will always be a dark girl with curly hair (this is not to criticize <em>Fight Club</em>, I loved the movie).</p>
<p align="justify">I think this makes books more relatable, and a well written text hits me at a very base level.  Since the written word is so flexible and abstract, my mind can lift it up to a concrete representation that is very close to my sense of self.  A book can drive me to insanity, a movie cannot.</p>
<p align="justify">I think the same applies to music too -- I've always found solos by  <em>Pink Floyd</em> their most interesting work.  When a rapper talks about how he scored last night, there is only so much information my mind can add, there is only so much I can misinterpret.  When I hear the crazy slide-guitar in <em>Shine on you crazy diamond, VI-IX</em>, my mind is forced to conjure up something using the only tools it has at its disposal, my imagination and my experience.  And it always comes   up with something sinister.</p>
]]></content:encoded>
			<wfw:commentRss>http://playingwithpointers.com/archives/669/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>New JIT Interface for GDB</title>
		<link>http://playingwithpointers.com/archives/633</link>
		<comments>http://playingwithpointers.com/archives/633#comments</comments>
		<pubDate>Sun, 20 Nov 2011 12:51:27 +0000</pubDate>
		<dc:creator>Sanjoy</dc:creator>
				<category><![CDATA[Digital Dope]]></category>
		<category><![CDATA[debugger]]></category>
		<category><![CDATA[gdb]]></category>
		<category><![CDATA[jit]]></category>
		<category><![CDATA[jit interface]]></category>

		<guid isPermaLink="false">http://playingwithpointers.com/?p=633</guid>
		<description><![CDATA[I worked on GDB as a part of my internship at Igalia. The work I did is finally merged, and this blog post describes the problem that I tried to solve and the solution I implemented. The Problem Let's say A is a runtime function that sometimes gets called by JIT compiled code. When the [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">I worked on GDB as a part of my internship at <a href="http://igalia.com">Igalia</a>.  The work I did is finally merged, and this blog post describes the problem that I tried to solve and the solution I implemented.</p>
<h3>The Problem</h3>
<p align="justify">Let's say <code>A</code> is a runtime function that sometimes gets called by JIT compiled code.  When the control is   inside <code>A</code>, the stack may look something like this:</p>
<pre>
[  A  ]
[ Some JIT compiled function (X)  ]
[ Some other JIT compiled function (Y)  ]
[ Some function that launches JIT compiled code (B)  ]
[ Some function that does the JIT compilation (C)  ]
[   .... and so on ....  ]
[  First function for this thread == (D)  ]
</pre>
<p align="justify">Now, for GDB to be able to successfully unwind through the <code>X</code> and <code>Y</code> frames, it needs some extra information.  For regular functions (like <code>A</code>, <code>B</code>, <code>C</code> and <code>D</code>) this information is embedded in a section in the ELF file, using the DWARF encoding. However, <code>X</code> and <code>Y</code> don't have any such associated information since they were generated on the fly.  On trying to <code>backtrace</code> when the stack is in the state shown above you'll</p>
<ul>
<li>See a bunch of <code>??</code>s instead of <code>X</code> and         <code>Y</code>, since GDB does not have the debug symbols.</li>
<li>A possibly incorrect backtrace.  From what I understand, GDB can backtrace by interpreting the prologue, but only up to a point.</li>
</ul>
<p align="justify">The initial solution (still supported by GDB) was this:  whenever the JIT compiler compiles a block of code, have it emit the debug information to an  ELF file in memory.  Then have the JIT compiler notify GDB of these in-memory ELF files using a <a href="http://sourceware.org/gdb/onlinedocs/gdb/JIT-Interface.html">published     interface</a>.  This way, GDB knows how to display a pretty backtrace.</p>
<p align="justify">This solution (of generating ELF files in memory) is, however, not ideal.  ELF files are complex and hard to generate.  Generating ELF files bites even more if all that GDB needs to do is display a meaningful stack trace.  My job was to find a less complex   way to get the debugging information into GDB.</p>
<h3>The Solution</h3>
<p align="justify">It was Tom Tromey's idea that it probably makes more sense to load and use some sort of a plugin provided by the JIT vendors.  I too thought this idea was viable and decided to   implement it and see how well it worked.</p>
<p align="justify">The problem can be divided into two parts -- providing the debug symbols and providing the unwind information.  We don't deal with providing type information and other such more sophisticated information, since we're targeting people who just want to see a meaningful stack trace without putting in too much effort.  The ELF handling code is still in GDB and anyone wishing to do something more exciting than looking at a backtrace can generate   full-fledged ELF files.</p>
<p align="justify">For the debug symbols part, we</p>
<ol>
<li>Have the JIT compiler generate the debug symbol information in     any format it pleases.  GDB does not need to understand this format.</li>
<li>Have the plugin parse this debug information (since         it understands the format, being written by the JIT vendor) and emit the symbol tables GDB requires.</li>
</ol>
<p align="justify">In the name of a clean interface and better backward compatibility, (2) is done by passing a bunch of callback functions to the plugin, along with the data it is supposed to parse (the data that the JIT compiler passed to GDB).  The plugin then constructs the symbol tables using the callbacks it is given.  There are callbacks to create a new symbol table (<code>symtab_open</code>), to add a mapping from line numbers to PCs (<code>line_mapping_add</code>) among others.  All this is documented   in the header <code>jit-reader.h</code>.</p>
<p align="justify"><code>jit-reader.h</code> is installed in <code>{includdir}/gdb/</code>, where <code>{includedir}</code> is the system's default include directory.  This header is the interface between the plugin and GDB, and needs to be included in the plugin source file.  Because of where it is installed by default, usually a <code>#include     &lt;gdb/jitreader.h&gt;</code> is sufficient.</p>
<p align="justify">For the unwinding part we simply have the plugin provide an unwinder.  The unwinder too gets a bunch of callbacks, one to read the value of a register in the current frame, one to tell GDB (<em>write</em>) the value of a register in the previous frame (the one that was called immediately before), one to read some data off the target process' address space, one to generate a <em>frame id</em> (described in <code>jit-reader.h</code>) and so on.  The unwinder is especially easy to write if the JIT compiled code stores interesting things at constant offsets from the base register.  Pseudo code could   be </p>
<pre>
  READ_REGISTER(cb, AMD64_RBP, FramePtr);
  PreviousFP = READ_MEMORY_AT(FramePtr)
  PreviousPC = READ_MEMORY_AT(FramePtr + 8)

  WRITE_REGISTER(AMD64_RBP, PreviousFP)
  WRITE_REGISTER(AMD64_RA, PreviousPC)
</pre>
<p align="justify">Telling GDB the values of the frame pointer and the   program counter is enough for it to display a backtrace.</p>
<p align="justify">I've checked in the code, and it is available in the current trunk.  This was a very general bird's eye view of the situation, more information can be found in the GDB manual and the <code>jit-reader.h</code> file GDB installs.  Thanks to everyone on the GDB mailing list who sent in their valuable reviews.</p>
<p align="justify">Comments, suggestions and questions welcome.</p>
]]></content:encoded>
			<wfw:commentRss>http://playingwithpointers.com/archives/633/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Macros In Haskell</title>
		<link>http://playingwithpointers.com/archives/615</link>
		<comments>http://playingwithpointers.com/archives/615#comments</comments>
		<pubDate>Tue, 18 Oct 2011 15:33:03 +0000</pubDate>
		<dc:creator>Sanjoy</dc:creator>
				<category><![CDATA[Digital Dope]]></category>
		<category><![CDATA[fp]]></category>
		<category><![CDATA[functional programming]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[template haskell]]></category>

		<guid isPermaLink="false">http://playingwithpointers.com/?p=615</guid>
		<description><![CDATA[Haskell too has macros. The subsystem is called Template Haskell. It is a Lisp-like AST generation framework that comes as a GHC extension (that is, it isn't a part of the language as of now). This blog post is a (hopefully) simple introduction to TH, and how it can possibly be used. As usual, it [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">Haskell too has macros.  The subsystem is called <em>Template Haskell</em>.  It is a Lisp-like AST generation framework that comes as a GHC extension (that is, it isn't a part of the language as of now).  This blog post is a (hopefully) simple introduction to TH, and how it can possibly be used.  As usual, it is less of an instructive guide, and more like a chronicle of my own personal adventure.  Moderate competence in Haskell is required.</p>
<p align="justify">Unlike Lisp's macro system, TH is typed -- like everything else in Haskell.  You have typed constructors like <code>FunD</code> (function declaration) and <code>InfixP</code> which are used the build the AST of whatever form you wish to <em>splice</em> into your regular code.  You can find the current TH user manual at [1].</p>
<p align="justify">The AST constructors are in the module <code>Language.Haskell.TH</code>, which looks quite overwhelming at the first glance, and, as such, writing TH expressions from scratch can get quite difficult.  Thankfully, we also have a nice <code>runQ</code> function which allows us to write TH expressions without really grokking everything in the module.  I'll try to demonstrate this by getting TH to generate a small (read useless / toy) string matcher.</p>
<h3>Objective</h3>
<p align="justify">We'll try to generate a function with the type <code>String->Bool</code>.  The function's job will be to check if the <code>String</code> it receives belongs to a list of <code>String</code>s we provide at compile time.  To make things a little more interesting, we'll allow the strings we have at compile time to have a regex-like <code>"?"</code> in them, which can match any character.  As an example, if the list of <code>String</code>s   is <a name="input"></a><code>["asdf", "a?foo", "P"]</code>, the generated function should match <code>"asdf"</code>, <code>"P"</code>, <code>"amfoo"</code> but not <code>"afoo"</code>.  We don't address the issue of how to match a literal <code>"?"</code>.  Heck, out end product boils down to ten lines of code.</p>
<p align="justify">Once we're done writing our TH code, we should have a function <code>generateMatcher</code>, which we shall invoke from   the top level of a source file, like:</p>
<pre>
  generateMatcher "generatedF" ["asdf", "a?foo", "P"]
</pre>
<p align="justify">It will then generate the matcher, with the name <code>generatedF</code> and the behaviour we're just described.  It can then be called like a regular function.  So a source file can look like:</p>
<pre>
  generateMatcher "generatedF" ["asdf", "a?foo", "P"]

  main = do
    str < - getLine
    putStrLn $ show $ generatedF str
</pre>
<h3>Starting Off</h3>
<p align="justify">The first thing to think about is how the compiled code should look like.  Given the above problem instance, we could generate something like</p>
<p><a name="togenerate"></a>
  </pre>
<pre>
    generatedF ('a':'s':'d':'f':[]) = True
    generatedF ('a':_:'f':'o':'o':[]) = True
    generatedF ('P':[]) = True
    generatedF _ = False
  </pre>
<p align="justify">Haskell's pattern matcher is, AFAIK, intelligent enough to convert the cases into a tree structure and quickly jump to the correct clause.  Now the second question:  how should the <em>AST</em> look like?  Here we take the help of the <code>runQ</code> function.  First we start GHCi as <code>ghci -XTemplateHaskell</code> (since TH is not enabled by default).  Once in GHCi, we import the required module by <code>:m + Language.Haskell.TH</code>.  The scene is set!  We can now see the AST for <code>generatedF   ('a':'s':'d':'f':[]) = True</code>.  For that, we evaluate <code>runQ [d|generatedF   ('a':'s':'d':'f':[]) = True|]</code>.  The <code>[d| ... |]</code> is used to quote a <em>declaration</em>.  Detailed information (about this, and generally about TH) is in [1]. We will now see some (rather incomprehensible) output:</p>
<pre>
[FunD generatedF [Clause [InfixP (LitP (CharL 'a'))
GHC.Types.: (InfixP (LitP (CharL 's')) GHC.Types.:
(InfixP (LitP (CharL 'd')) GHC.Types.:
(InfixP (LitP (CharL 'f')) GHC.Types.:
(ConP GHC.Types.[] []))))] (NormalB
(ConE GHC.Bool.True)) []]]
</pre>
<p align="justify">A little indentation, of course, goes a long way in making things clearer (when does it not? :D):</p>
<p><a name="rawast"></a></p>
<pre>
[FunD
 generatedF [Clause
             [InfixP (LitP (CharL 'a'))
              GHC.Types.:
              (InfixP (LitP (CharL 's'))
               GHC.Types.:
               (InfixP (LitP (CharL 'd'))
                GHC.Types.:
                (InfixP (LitP (CharL 'f'))
                 GHC.Types.:
                 (ConP GHC.Types.[] []))))]
             (NormalB (ConE GHC.Bool.True)) []]]
  </pre>
<p align="justify">I usually use guesswork from this point, leaving it to Haskell's type system to correct my incompetence.  Haskell's AST has the convention of suffixing most data constructors with a letter which hints at what type it belongs to.  We can guess (and purely guess, I have not looked up the documentation) that <code>InfixP</code> is an infix pattern, a <code>LitP</code> is a literal pattern, a <code>NormalB</code> is a normal body, a <code>ConE</code> is a constructor expression, a <code>FunD</code> is a function declaration and so forth.  The AST for the match-anything clause is simpler:</p>
<pre>
  [FunD
   generatedF [Clause [WildP]
               (NormalB (ConE GHC.Bool.False)) []]]
</pre>
<p align="justify">You can guess what a <code>WildP</code> is, especially since it derives from a <code>_</code>.</p>
<h3>The core idea</h3>
<p align="justify">The basic idea behind TH is that allowing the user to splice in a bit of custom-generated AST into an otherwise normal Haskell program can lead to all kinds of awesomeness.  A <em>splicable</em> AST has to be typed as a <code>Q Exp</code>, a <code>Q Type</code> or a <code>Q   [Dec]</code>.  The last one stands for a list of declarations, which is the one we need right now.  To splice in an expression as an AST, we place it inside a <code>$( ... )</code>.  To keep things clean, top level declarations don't need the <code>$( ...   )</code> nesting -- anything of the type <code>Q [Dec]</code> at the top level of a source file is automatically interpreted correctly.  <code>Q</code> is a <code>Monad</code>, which is sometimes needed to generate unique identifiers, like Lisp's <code>gensym</code>.  We don't need that right now.</p>
<p align="justify">Getting back to the point, we need a function that takes the specified <a href="#input">input</a>, and outputs the AST of the <a href="#togenerate">code we want to generate</a>.  Since we need to generate something of the type <code>Q [Dec]</code>, and we will be given the name of the function to generate, and a list of   <code>String</code>s, there is at least one line we can write:</p>
<pre>
  generateMatcher :: String -> [String] -> Q [Dec]
</pre>
<p align="justify">We already have seen the <a href="#rawast">basic structure of the AST</a>.  We now write some code to generate it. We will generate only one <code>Dec</code> (i.e. the <code>[Dec]</code> in <code>Q [Dec]</code> will only have one element), corresponding to the one function we want to declare.  The <code>Dec</code> will be a <code>FunD</code> (just like in the AST <code>runQ</code> showed us), with multiple <code>Clause</code>s, each representing one pattern (which itself corresponds to one <code>String</code> we were given at compile time).</p>
<p align="justify">One thing though -- for some reason I don't really understand, while <code>runQ</code> prints them, things in the <code>GHC</code> module (like <code>GHC.Bool.True</code>,  <code>GHC.Types.:</code>) don't really work in a generated AST.  A <em>token</em> in TH is essentially a <code>Name</code>, which we can get using <code>mkName :: String -> Name</code> or by using the quote (<code>'</code>) operator.  So one way to get around the problem is to represent things like <code>GHC.Bool.True</code> as <code>Name</code>s -- this seem to work well in practice.  The quote operator does not seem to work for symbols -- we replace things like <code>GHC.Bool.True</code> with <code>'True</code> and things like <code>:</code> with <code>mkName ":"</code>.  We also need to use <code>mkName</code> for the functions' name, which needs to be a (surprise) <code>Name</code>.</p>
<p align="justify">Okay, phew!  Time to see some code that actually works.  With the little Haskell-foo I have, I wrote the following   piece of code:</p>
<pre>
generateMatcher name strs =
  return [FunD (mkName name) (map thMagic strs ++ baseCase)]
  where
    thMagic str = Clause [transform str] (NormalB (ConE 'True)) []
    baseCase = [Clause [WildP] (NormalB (ConE 'False)) []]
    transform [] = ConP (mkName "[]") []
    transform ('?':rest) =
      let colon = mkName ":" in (InfixP WildP colon $ transform rest)
    transform (x:rest) =
      InfixP (LitP (CharL x)) (mkName ":") $ transform rest
</pre>
<p align="justify">Ten lines, as promised.  I used <code>return</code> to lift the <code>[Dec]</code> into the <code>Q</code> monad.  Other than, most of it is a direct translation of the input we get into something that looks like the output <code>runQ</code> dumped on me.  The code is in <a href="https://github.com/sanjoy/Snippets/blob/master/GenerateMatcher.hs">my   snippets repo</a>, as usual.  TH does not, as of now, allow you to declare an expression and splice it in the same file.  You need to have, say, a <code>FooTH.hs</code> where you have all the functions for generating the AST and a <code>Foo.hs</code> where you actually use them.</p>
<h3>TL;DR</h3>
<p align="justify">This was a long and meandering post.  In short,   here are the two basic points I want to make:</p>
<ul>
<li>TH is awesome.  It can save a lot of time, if used properly.</li>
<li>Don't beat yourself up trying to write ASTs from scratch, play         around with <code>runQ</code>, steal and modify.</li>
</ul>
<p>[1] <a href="http://www.haskell.org/ghc/docs/latest/html/users_guide/template-haskell.html">http://www.haskell.org/ghc/docs/latest/html/users_guide/template-haskell.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://playingwithpointers.com/archives/615/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Threading A Graph</title>
		<link>http://playingwithpointers.com/archives/604</link>
		<comments>http://playingwithpointers.com/archives/604#comments</comments>
		<pubDate>Sun, 09 Oct 2011 09:13:12 +0000</pubDate>
		<dc:creator>Sanjoy</dc:creator>
				<category><![CDATA[Digital Dope]]></category>
		<category><![CDATA[gc]]></category>
		<category><![CDATA[graphs]]></category>

		<guid isPermaLink="false">http://playingwithpointers.com/?p=604</guid>
		<description><![CDATA[One operation that moving garbage collectors (among other things) have to concern themselves with is updating of references. Once you move object X from position P0 to P1, you'll also need to update all references to X accordingly. To be able to do this, you first need to somehow maintain a list of words that [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">One operation that moving garbage collectors (among other things) have to concern themselves with is updating of references.  Once you move object <em>X</em> from position <em>P<sub>0</sub></em> to <em>P<sub>1</sub></em>, you'll also need to update all references to X accordingly.  To be able to do this, you first need to somehow maintain a list of words that point to a specific object.  There are obvious ways of this, of course -- maintaining a hash-table that maps a object to a list of words that store references to it is one.  There is a more elegant way to do this, however; if the set of objects you're working with satisfies the following constraints:</p>
<p align="justify">
<ul>
<li>Each object has a (header or otherwise) pointer-sized field which stores some information that can be differentiated from a pointer.  On a system that is aligns words to two or four byte boundaries, we     could set the LSB of non-pointer data stored in the     field.  We'll call this field <em>info</em>.</li>
<li>Pointers to objects always point to the beginning of some object.      No pointing to a field stored somewhere in the middle.</li>
</ul>
<p align="justify">The algorithm is extremely simple and elegant.  All you have to do is this: every time you see a slot (a <em>field</em>, if you like) <em>S</em> point to an object <em>O</em>, change <em>O</em>s <em>info</em> field to point to slot <em>S</em> and set <em>S</em> to whatever value (pointer or non-pointer) <em>info</em> held before it was set to point to <em>S</em>.  If you try this out with pen and paper (I'm too lazy to draw a diagram here), you'll see that this simple action, when applied to every slot in the graph, transforms the predicate "Slots <em>S<sub>0</sub></em>, <em>S<sub>1</sub></em> and <em>S<sub>2</sub></em> point to object <em>O</em>" to "<em>O</em>'s <em>info</em> slot contains a pointer to <em>S<sub>2</sub></em>, <em>S<sub>2</sub></em> contains a pointer to <em>S<sub>1</sub></em>, <em>S<sub>1</sub></em> contains a pointer to <em>S<sub>0</sub></em> and <em>S<sub>0</sub></em> contains the data <em>O</em>'s <em>info</em> field originally held".  Since we can always distinguish a data word stored in <em>info</em> from a pointer, we can traverse this list and get a list of the slots.  The slots can then be filled with new location of the object.</p>
<p align="justify">This is one of the main concepts behind Jonker's compaction algorithm, except that it intelligently divides the compaction into two phases of updating forward pointers and updating   backward pointers while moving  objects.</p>
<p align="justify">I wrote a simple and tiny implementation of threading a graph, which you can find at <a href="https://github.com/sanjoy/Snippets/blob/master/Thread.cc">https://github.com/sanjoy/Snippets/blob/master/Thread.cc</a>.  Note how the main threading algorithm only takes thirteen lines of C++.</p>
]]></content:encoded>
			<wfw:commentRss>http://playingwithpointers.com/archives/604/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tree Traversal in O(1) Space</title>
		<link>http://playingwithpointers.com/archives/600</link>
		<comments>http://playingwithpointers.com/archives/600#comments</comments>
		<pubDate>Sat, 08 Oct 2011 12:52:45 +0000</pubDate>
		<dc:creator>Sanjoy</dc:creator>
				<category><![CDATA[Digital Dope]]></category>
		<category><![CDATA[gc]]></category>
		<category><![CDATA[graph]]></category>
		<category><![CDATA[O(1)]]></category>
		<category><![CDATA[thorelli]]></category>
		<category><![CDATA[traversal]]></category>
		<category><![CDATA[tree]]></category>

		<guid isPermaLink="false">http://playingwithpointers.com/?p=600</guid>
		<description><![CDATA[I've been reading some texts recently, and came across a very interesting way to traverse a graph, called pointer reversal. The idea is this -- instead of maintaining an explicit stack (of the places you've visited), you try to store the relevant information in the nodes themselves. One approach that works on directed graphs with [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">I've been reading some texts recently, and came across a very interesting way to traverse a graph, called pointer reversal.  The idea is this -- instead of maintaining an explicit stack (of the places you've visited), you try to store the relevant information in the nodes themselves.  One approach that works on directed graphs with two (outgoing) arcs per node is called the Deutsch-Schorr-Waite algorithm.  This was later extended by Thorelli to work for directed graphs with an unknown number of (outgoing) arcs   per node.</p>
<p align="justify">The Deutsch-Schorr-Waite algorithm does not require adding any extra fields to the nodes.  Adding fields to the node structure sort of spoils the fun -- you could, for instance, have a pointer pointing to the parent of every node making stackless traversal a cup of tea.  And you haven't really saved any space!  Thorelli's approach, however, requires adding a integer to each node that can hold at least the maximum   number of outgoing arcs a node can possibly have.  But it can traverse graphs with more than two outgoing arcs per node.  And you do save space here -- if you have three outgoing arcs (maximum) per node, you could do with just a two bit integer.  On x86, that's <a href="http://en.wikipedia.org/wiki/Data_structure_alignment" target="_blank">the last two bits of a pointer</a>.</p>
<p align="justify">Note that both the approaches are quite a bit more complicated than a <em>vanilla</em> DFS.  Nevertheless, there are applications where such characteristics (of consuming only constant space) becomes important.  One of the more visible ones is a GC.  A GC has to traverse a (potentially) very large and complicated graph of objects, and it can't use up too much memory (or call-stack space) when collecting   -- a time when memory is already a scarce resource.</p>
<p align="justify">I like implementing things, and while I did not implement the full algorithm proposed by Thorelli, I did implement one which traverses a <em>tree</em> in constant space.  What more, it does   not need the extra bits Thorelli's algorithm requires.</p>
<p align="justify">The code is here (I believe in making code talk instead of giving long, winded descriptions):   <a href="https://github.com/sanjoy/Snippets/blob/master/Traversal.cc">https://github.com/sanjoy/Snippets/blob/master/Traversal.cc</a>.</p>
<p align="justify">The snippets repo is an idea that I recently got -- I often code small things when I'm bored or want to try something out.  Having a public "snippets" repo where I upload all that smelt good.</p>
]]></content:encoded>
			<wfw:commentRss>http://playingwithpointers.com/archives/600/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 4.923 seconds -->
<!-- Cached page served by WP-Cache -->

