<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"
    xmlns:dc="http://purl.org/dc/elements/1.1/">
    <channel>
        <title>Code and Co</title>
        <link>http://jozefg.bitbucket.org</link>
        <description><![CDATA[Articles on functional programming, PLT, types, and other geekery]]></description>
        <atom:link href="http://jozefg.bitbucket.org/rss.xml" rel="self"
                   type="application/rss+xml" />
        <lastBuildDate>Mon, 12 Oct 2015 00:00:00 UT</lastBuildDate>
        <item>
    <title>Runtime Tagging</title>
    <link>http://jozefg.bitbucket.org/posts/2015-10-12-tags.html</link>
    <description><![CDATA[<div class="info">
    Posted on October 12, 2015
    
</div>
<div class="info">
    
    Tags: <a href="/tags/sml.html">sml</a>
    
</div>

<p>In this post I’d just like to walk through some fun code, nothing particularly theory-y. The code I’d like to go through is a simple little module in ML that lets you easily construct “dynamic” types. This isn’t through the usual “really big sum of products” approach but instead is completely open and can be extended for every new defined type (at runtime).</p>
<h2 id="the-basic-idea">The Basic Idea</h2>
<p>The basic idea behind this trick hinges on how exceptions work in SML. Well, really it’s not about exceptions so much as what exceptions work with. In ML we can declare new exceptions like this</p>
<pre class="sml"><code>    exception Foo of tyarg</code></pre>
<p>and this gives us a new exception constructor <code>Foo</code> and we can raise and handle it like you would expect</p>
<pre class="sml"><code>    (raise (Foo 1)) handle Foo x =&gt; x</code></pre>
<p>But what’s particularly interesting is that <code>Foo</code> actually has a type. Really it’s just a constructor for a special type <code>exn</code>. This means we can do things like pass around exception constructors, apply them, etc, etc.</p>
<p><code>exn</code> is what we might call an extensible data type, we can extend it arbitrarily. We could imagine allowing users to define their own such types but in SML we’ve just go the one. The reason we even have this one is because it’s a great choice if you can only allow one type to be raised and handled.</p>
<p>What we’re going to do is use the fact that we can generate new extensions to <code>exn</code> at run time to create an <code>exn</code> based structure providing a way to implement “tags”. Once we have these tags we’ll be able to implement a pair of functions</p>
<pre class="sml"><code>    val tag   : &#39;a tag -&gt; &#39;a -&gt; dynamic
    val untag : &#39;a tag -&gt; dynamic -&gt; &#39;a option</code></pre>
<p>So tags let us “forget” the type of some expression and treat it as some dynamic blob to be recovered at some time in the future. Concretely, we’d like to implement this signature</p>
<pre class="sml"><code>    signature TAG =
    sig
      type dynamic
      type &#39;a tag

      val new    : unit -&gt; &#39;a tag
      val tag    : &#39;a tag -&gt; &#39;a -&gt; dynamic
      val untag  : &#39;a tag -&gt; dynamic -&gt; &#39;a option
    end</code></pre>
<h2 id="the-implementation">The Implementation</h2>
<p>So let’s start implementing the thing. First we need to decide what the type <code>dynamic</code> should be. I propose that it should be <code>exn</code>. The reason being that we can always extend <code>exn</code> in various ways so if we implement things with <code>dynamic = exn</code> we’ll have the ability to make <code>dynamic</code> “grow a new branch” to accommodate whatever we’re working with.</p>
<pre class="sml"><code>    structure Tag :&gt; TAG =
    struct
      type dynamic = exn
    end</code></pre>
<p>Ok, so what should <code>tag</code> be? Well it’s going to be type indexed obviously so that we can even talk about the signatures of <code>(un)tag</code>, but more importantly its purpose should be to tell us how to package something up into an <code>exn</code> so we can get it back out. The downside of this whole extensible data type thing is that if we forget about the constructor we used to make an <code>exn</code> it’s just lost forever! A <code>tag</code> will make sure that once we make a constructor to use with <code>dynamic</code> we won’t find ourselves with a <code>dynamic</code> and no way to inspect it.</p>
<p>The best way I can think of for doing this is to just back the <code>(un)tag</code> operations straight into the implementation of the type.</p>
<pre class="sml"><code>    structure Tag :&gt; TAG =
    struct
      type dynamic = exn
      type &#39;a tag  = {into : &#39;a -&gt; exn, out : exn -&gt; &#39;a option}
    end</code></pre>
<p>Now this makes it look like tags could perform arbitrary operations in the process of tagging and untagging, but really we’re going to implement it so it’s all very simple and efficient.</p>
<p>In particular, we’re now in a position to define our three core operators</p>
<pre class="sml"><code>    structure Tag :&gt; TAG =
    struct
      type dynamic = exn
      type &#39;a tag  = {into : &#39;a -&gt; exn, out : exn -&gt; &#39;a option}

      fun new () : &#39;a tag =
        let
          exception Fresh of &#39;a
        in
          { into = Fresh
          , out = fn e =&gt;
              case e of Fresh a =&gt; SOME a | _ =&gt; NONE
          }
        end

      fun tag {into, out} = into
      fun untag {into, out} = out
    end</code></pre>
<p>Now tag and untag are pretty simple because we basically implemented them up in <code>new</code> so let’s look carefully at that. We start by first minting a new constructor for <code>exn</code>. We know that this will not clash with any other exception in existence, no one else can raise it or handle it unless we explicitly give them this constructor. Now while we have access to it, we bundle the constructor into the <code>tag</code> record we’re making.</p>
<p><code>into</code> is quite easy to implement because it’s just constructor application. <code>out</code> is also straightforward, all we do is pattern match to see if the given <code>exn</code> is correct. All we do in the actual matching bit is see if we’ve been given something made with our <code>Fresh</code> constructor and return the included <code>a</code> if we did. The handling everything else is important, otherwise this would explode horribly every time we failed to untag something.</p>
<p>And there’s a nice way of implementing the same sort of run time typing you get in dynamic languages in SML. One nice advantage of this over the usual</p>
<pre class="sml"><code>    datatype dynamic = INT of int | STRING of string | ...</code></pre>
<p>approach is we can always extend our <code>dynamic</code> with user defined types. So we can do something like</p>
<pre class="sml"><code>    datatype foo = Foo of int
    val fooTag = Tag.new () : foo tag
    val d = Tag.tag fooTag (Foo 2)
    val SOME (Foo 2) = Tag.untag fooTag d</code></pre>
<h2 id="wrap-up">Wrap Up</h2>
<p>There you go, this is just a very short post on a very short piece of code that let’s us do something fun. Some nice things you can do now</p>
<ul>
<li>Use the inherently recursive nature of <code>dynamic</code> to write an infinite loop without direct recursion</li>
<li>Do the same thing, but without using <code>exn</code> and using the generative effect of allocating a reference instead</li>
<li>etc</li>
</ul>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Mon, 12 Oct 2015 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2015-10-12-tags.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Two Different Flavors of Type Theory</title>
    <link>http://jozefg.bitbucket.org/posts/2015-09-27-flavors.html</link>
    <description><![CDATA[<div class="info">
    Posted on September 27, 2015
    
</div>
<div class="info">
    
    Tags: <a href="/tags/types.html">types</a>
    
</div>

<p>So summer seems to be about over. I’m very happy with mine, I learned quite a lot. In particular over the last few months I’ve been reading and fiddling with a different kind of type theory than I was used to: computational type theory. This is the type theory that underlies Nuprl (or <a href="http://www.jonprl.org">JonPRL</a> cough cough).</p>
<p>One thing that stood out to me was that you could do all these absolutely crazy things in this system that seemed impossible after 3 years of Coq and Agda. In this post I’d like to sketch some of the philosophical differences between CTT and a type theory more in the spirit of CiC.</p>
<h2 id="formal-type-theory-and-props-as-types-1">Formal Type Theory and Props-as-Types #1</h2>
<p>First things first, let’s go over the more familiar notion of type theory. To develop one of these type theories you start by discussing some syntax. You lay out the syntax for some types and some terms</p>
<pre><code>A ::= Σ x : A. A | Π x : A. A | ⊤ | ⊥ | ...
M ::= M M | λ x : A. M | &lt;M, M&gt; | π₁ M | ⋆ | ...</code></pre>
<p>And now we want to describe the all important <code>M : A</code> relation. This tells us that some term has some type. It’s is inductively defined from a finite set of inferences. Ideally, it’s even decidable for philosophical reasons I’ve never cared too much about. In fact, it’s this relation that really governs our whole type theory, everything else is going to stem from this.</p>
<p>As an afterthought, we may decide that we want to identify certain terms which other terms this is called definitional equality. It’s another inductively defined (and decidable) judgment <code>M ≡ N : A</code>. Two quick things to note here</p>
<ol type="1">
<li>Definitional equality is completely arbitrary; it exists in the way it does because we defined it that way and for no other reason</li>
<li>The complexity of proving <code>M ≡ N : A</code> is independent of the complexity of <code>A</code></li>
</ol>
<p>The last point is some concern because it means that equality for functions is never going to be right for what we want. We have this uniformly complex judgment <code>M ≡ N : A</code> but when <code>A = Π x : B. C</code> the complexity <em>should</em> be greater and dependent on the complexity of <code>B</code> and <code>C</code>. That’s how it works in math after all, equality at functions is defined pointwise, something we can’t really do here if <code>≡</code> is to be decidable or just be of the same complexity no matter the type.</p>
<p>Now we can do lots of things with our theory. One thing we almost always want to do is now go back and build an operational semantics for our terms. This operational semantics should be some judgment <code>M ↦ M</code> with the property that <code>M ↦ N</code> will imply that <code>M ≡ N</code>. This gives us some computational flavor in our type theory and lets us run the pieces of syntax we carved out with <code>M : A</code>.</p>
<p>But these terms that we’ve written down aren’t really programs. They’re just serializations of the collections of rules we’ve applied to prove a proposition. There’s no ingrained notion of “running” an <code>M</code> since it’s built on after the fact. What we have instead is this <code>≡</code> relation which just specifies which symbols we consider equivalent but even it is was defined arbitrarily. There’s no reason we <code>≡</code> needs to be a reasonable term rewriting system or anything. If we’re good at our jobs it will be, sometimes (HoTT) it’s not completely clear what that computation system is even though we’re working to find it. So I’d describe a (good) formal type theory as an axiomatic system like any other that we can add a computational flavor to.</p>
<p>This leads to the first interpretation of the props-as-types correspondence. This states that the inductively defined judgments of a logic give rise to a type theory whose terms are proof terms for those same inductively defined judgments. It’s an identification of similar looking syntactic systems. It’s useful to be sure if you want to develop a formal type theory, but it gives us less insight into the computational nature of a logic because we’ve reflected into a type theory which we have no reason to suspect has a reasonable computational characterization.</p>
<h2 id="behaviouralcomputational-type-theory-and-props-as-types-2">Behavioural/Computational Type Theory and Props-as-Types #2</h2>
<p>Now we can look at a second flavor of type theory. In this setting the way we order our system is very different. We start with an programming language, a collection of terms and an untyped evaluation relation between them. We don’t necessarily care about all of what’s in the language. As we define types later we’ll say things like “Well, the system has to include at least X” but we don’t need to exhaustively specify all of the system. It follows that we have actually no clue when defining the type theory how things compute. They just compute <em>somehow</em>. We don’t really even want the system to be strongly normalizing, it’s perfectly valid to take the lambda calculus or Perl (PerlPRL!).</p>
<p>So we have some terms and <code>↦</code>, on top of this we start by defining a notion of equality between terms. This equality is purely computational and has no notion of types yet (like <code>M ≡ N : A</code>) because we have no types yet. This equality is sometimes denoted <code>~</code>, we usually define it as <code>M ~ N</code> if and only if <code>M ↦ O(Ms)</code> if and only if <code>N ↦ O(Ns)</code> and if they terminate than <code>Ms ~ Ns</code>. By this I mean that two terms are the same if they compute in the same way, either by diverging or running to the same value built from <code>~</code> equal components. For more on this, you could read <a href="http://www.nuprl.org/KB/show.php?ShowPub=Howe89">Howe’s paper</a>.</p>
<p>So now we still have a type theory with no types.. To fix this we go off an define inferences to answer three questions.</p>
<ol type="1">
<li>What other values denote types equal to it? (<code>A = B</code>)</li>
<li>What values are in the type? (<code>a ∈ A</code>)</li>
<li>What values are considered equal <strong>at that type</strong>? (<code>a = b ∈ A</code>)</li>
</ol>
<p>The first questions is usually answered in a boring way, for instance, we would say that <code>Π x : A. B = Π x : A'. B'</code> if we know that <code>A = A'</code> and <code>B = B'</code> under the assumption that we have some <code>x ∈ A</code>. We then specify two and three. There we just give the rules for demonstrating that some value, which is a program existing entirely independently of the type we’re building, is in the type. Continuing with functions, we might state that</p>
<pre><code>  e x ∈ B (x ∈ A)
———————————————————
  e ∈ Π x : A. B</code></pre>
<p>Here I’m using <code>_ (_)</code> as syntax for a hypothetical judgment, we have to know that <code>e ∈ B</code> under the assumption that we know that <code>x ∈ A</code>. Next we have to decide what it means for two values to be equal as functions. We’re going to do this behaviourally, by specifying that they behave as equal programs when used as functions. Since we use functions by applying them all we have to do is specify that they behave equally on application</p>
<pre><code> v x = v&#39; x ∈ B (x ∈ A)
————————————————————————
  v = v&#39; ∈ Π x : A. B</code></pre>
<p>Equality is determined on a per type basis. Furthermore, it’s allowed to use the equality of smaller types in its definition. This means that when defining equality for <code>Π x : A. B</code> we get to use the equalities for <code>A</code> and <code>B</code>! We make no attempt to maintain either decidability or uniform complexity in the collections of terms specified by <code>_ = _ ∈ _</code> as we did with <code>≡</code>. As another example, let’s have a look at the equality type.</p>
<pre><code> A = A&#39;  a = a&#39; ∈ A  b = b&#39; ∈ A
 ————————————————————————————————
    I(a; b; A) = I(a&#39;; b&#39;; A&#39;)


   a = b ∈ A
 ——————————————
 ⋆ ∈ I(a; b; A)

     a = b ∈ A
 ——————————————————
 ⋆ = ⋆ ∈ I(a; b; A)</code></pre>
<p>Things to notice here, first off the various rules depend on the rules governing membership and equality in <code>A</code> as we should expect. Secondly, <code>⋆</code> (the canonical occupant of <code>I(...)</code>) has no type information. There’s no way to reconstruct whatever reasoning went into proving <code>a = b ∈ A</code> because there’s no computational content in it. The thing on the left of the <code>∈</code> only describes the portions of our proof that involve computation and equalities in computational type theory are always computationally trivial. Therefore, they get the same witness no matter the proof, no matter the types involved. Finally, the infamous equality reflection rule is really just the principle of inversion that we’re allowed to use in reasoning about hypothetical judgments.</p>
<p>This leads us to the second cast of props-as-types. This one states that constructive proof has computational character. Every proof that we write in a logic like this gives us back an (untyped) program which we can run as appropriate for the theorem we’ve proven. This is the idea behind Kleene’s realizability model. Similar to what we’d do with a logical relation we define what each type means by defining the class of appropriate programs that fit its specification. For example, we defined functions to be the class of things that apply and proofs of equality are ⋆ when the equality is true and there are no proofs when it’s false. Another way of phrasing this correspondence is types-as-specs. Types are used to identify a collection of terms that may be used in some particular way instead of merely specifying the syntax of their terms. To read a bit more about this see <a href="http://www.nuprl.org/documents/Allen/lics87.html">Stuart Allen</a> and <a href="https://www.cs.uoregon.edu/research/summerschool/summer10/lectures/Harper-JSC92.pdf">Bob Harper’s</a> work on the do a good job of explaining how this plays out for type theory.</p>
<h2 id="building-proof-assistants">Building Proof Assistants</h2>
<p>A lot of the ways we actually interact with type theories is not on the blackboard but through some proof assistant which mechanizes the tedious aspects of using a type theory. For formal type theory this is particularly natural. It’s decidable whether <code>M : A</code> holds so the user just writes a term and says “Hey this is a proof of <code>A</code>” and the computer can take care of all the work of checking it. This is the basic experience we get with Coq, Agda, Idris, and others. Even <code>≡</code> is handled without us thinking about it.</p>
<p>With computational type theory life is a little sadder. We can’t just write terms like we would for a formal type theory because <code>M ∈ A</code> isn’t decidable! We need to help guide the computer through the process of validating that our term is well typed. This is the price we pay for having an exceptionally rich notion of <code>M = N ∈ A</code> and <code>M ∈ A</code>, there isn’t a snowball’s chance in hell of it being decidable <a href="#fn1" class="footnote-ref" id="fnref1"><sup>1</sup></a>. To make this work we switch gears and instead of trying to construct terms we start working with what’s called a program refinement logic, a PRL. A PRL is basically a sequent calculus with a central judgment of</p>
<pre><code>H ≫ A ◁ e</code></pre>
<p>This is going to be set up so that <code>H ⊢ e ∈ A</code> holds, but there’s a crucial difference. With <code>∈</code> everything was an input. To mechanize it we would write a function accepting a context and two terms and checking whether one is a member of the other. With <code>H ≫ A ◁ e</code> only <code>H</code> and <code>A</code> are inputs, <code>e</code> should be thought of as an output. What we’ll do with this judgment is work with a tactic language to construct a derivation of <code>H ≫ A</code> without even really thinking with that <code>◁ e</code> and the system will use our proof to <em>construct the term for us</em>. So in Agda when I want to write a sorting function what I might do is say</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb6-1" data-line-number="1">    sort <span class="ot">:</span> List Nat <span class="ot">→</span> List Nat</a>
<a class="sourceLine" id="cb6-2" data-line-number="2">    sort xs <span class="ot">=</span> <span class="ot">...</span></a></code></pre></div>
<p>I just give the definition and Agda is going to do the grunt work to make sure that I don’t apply a nat to a string or something equally nutty. In a system like (Jon|Nu|Meta|λ)prl what we do instead is define the type that our sorting function ought to have and use tactics to prove the existence of a realizer for it. By default we don’t really specify what exactly that realizer. For example, if I was writing JonPRL maybe I’d say</p>
<pre class="jonprl"><code>    ||| Somehow this says a list of nats is a sorted version of another
    Operator sorting : (0; 0).

    Theorem sort : [(xs : List Nat) {ys : List Nat | is-sorting(ys; xs)}] {
      ||| Tactics go here.
    }</code></pre>
<p>I specify a sufficiently strong type so that if I can construct a realizer for it then I clearly have constructed a sorting algorithm. Of course we have tactics which let us say things “I want to use <em>this</em> realizer” and then we have to go off and show that the candidate realizer is a validate realizer. In that situation we’re actually acting as a type checker, constructing a derivation implying <code>e ∈ A</code>.</p>
<h2 id="wrap-up">Wrap Up</h2>
<p>Well, that’s this summer in a nutshell. Before I finish I had one more possible look on things. Computational type theory is not concerned with something being provable in an axiomatic system, rather it’s about describing constructions. Brouwer’s core idea is that a proof is a mental construction and computational type theory is a system for proving that a particular a computable process actually builds the correct object. It’s a translation of Brouwer’s notion of proof into terms a computer scientist might be interested in.</p>
<section class="footnotes">
<hr />
<ol>
<li id="fn1"><p>To be clear, this is the chance of the snowball not melting. Not the snowball’s chances of being able to decide whether or not <code>M ∈ A</code> holds. Though I suppose they’re roughly the same.<a href="#fnref1" class="footnote-back">↩</a></p></li>
</ol>
</section>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Sun, 27 Sep 2015 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2015-09-27-flavors.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Type is not in Type</title>
    <link>http://jozefg.bitbucket.org/posts/2015-08-26-type-in-type.html</link>
    <description><![CDATA[<div class="info">
    Posted on August 26, 2015
    
</div>
<div class="info">
    
    Tags: <a href="/tags/jonprl.html">jonprl</a>, <a href="/tags/types.html">types</a>, <a href="/tags/haskell.html">haskell</a>
    
</div>

<p>I was reading a <a href="https://typesandkinds.wordpress.com/category/dependent-types/haskell-dependent-types/">recent proposal</a> to merge types and kinds in Haskell to start the transition to dependently typed Haskell. One thing that caught my eye as I was reading it was that this proposal adds <code>* :: *</code> to the type system. This is of some significance because it means that once this is fully realized, Haskell will be inconsistent (as a logic) in a new way! Of course, this isn’t a huge deal since Haskell is already woefully inconsistent with</p>
<ul>
<li><code>unsafePerformIO</code></li>
<li>Recursive bindings</li>
<li>Recursive types</li>
<li>Exceptions</li>
<li>…</li>
</ul>
<p>So it’s not like we’ll be entering new territory here. All that it means is that there’s a new way to inhabit every type in Haskell. If you were using Haskell as a proof assistant you were already in for a rude awakening I’m afraid :)</p>
<p>This is an issue of significance though for languages like Idris or Agda where such a thing would actually render proofs useless. Famously, Martin-Löf’s original type theory did have <code>Type : Type</code> (or <code>* :: *</code> in Haskell spelling) and Girard managed to derive a contradiction (Girard’s paradox). I’ve always been told that the particulars of this construction are a little bit complicated but to remember that <code>Type : Type</code> is bad.</p>
<p>In this post I’d like to prove that <code>Type : Type</code> is a contradiction in <a href="http://github.com/jonsterling/jonprl">JonPRL</a>. This is a little interesting because in most proof assistants this would work in two steps</p>
<ol type="1">
<li>Hack the compiler to add the rule <code>Type : Type</code></li>
<li>Construct a contradiction and check it with the modified compiler</li>
</ol>
<p><em>OK to be fair, in something like Agda you could use the compiler hacking they’ve already done and just say <code>{-# OPTIONS --set-in-set  #-}</code> or whatever the flag is. The spirit of the development is the same though</em></p>
<p>In JonPRL, I’m just going to prove this as a regular implication. We have a proposition which internalizes membership and I’ll demonstrate <code>not(member(U{i}; U{i}))</code> is provable (<code>U{i}</code> is how we say <code>Type</code> in JonPRL). It’s the same logic as we had before.</p>
<h2 id="background-on-jonprl">Background on JonPRL</h2>
<p>Before we can really get to the proof we want to talk about, we should go through some of the more advanced features of JonPRL we need to use.</p>
<p>JonPRL is a little different than most proof assistants, for example We can define a type of all closed terms in our language and whose equality is purely computational. This type is <code>base</code>. To prove that <code>=(a; b; base)</code> holds you have to prove <code>ceq(a; b)</code>, the finest grain equality in JonPRL. Two terms are <code>ceq</code> if they</p>
<ol type="1">
<li>Both diverge, or</li>
<li>Run to the same outermost form and have <code>ceq</code> components</li>
</ol>
<p>What’s particularly exciting is that you can substitute any term for any other term <code>ceq</code> to it, no matter at what type it’s being used and under what hypotheses. In fact, the <code>reduce</code> tactic (which performs beta reductions) can conceptually be thought of as substituting a bunch of terms for their weak-head-normal forms which are <code>ceq</code> to the original terms. The relevant literature behind this is found in Doug Howe’s “Equality in a Lazy Computation System”. There’s more in JonPRL in this regard, we also have the asymmetric version of <code>ceq</code> (called <code>approx</code>) but we won’t need it today.</p>
<p>Next, let’s talk about the image type. This is a type constructor with the following formation rule:</p>
<pre><code> H ⊢ A : U{i}        H ⊢ f : base
 —————————————————————————————————
      H ⊢ image(A; f) : U{i}</code></pre>
<p>So here <code>A</code> is a type and <code>f</code> is anything. Things are going to be equal <code>image</code> if we can prove that they’re of the form <code>f w</code> and <code>f w'</code> where <code>w = w' ∈ A</code>. So <code>image</code> gives us the codomain (range) of a function. What’s pretty crazy about this is that it’s not just the range of some function <code>A → B</code>, we don’t really need a whole new type for that. It’s the range of <em>literally any closed term we can apply</em>. We can take the range of the Y combinator over pi types. We can take the range of <code>lam(x. ⊥)</code> over <code>unit</code>, anything we want!</p>
<p>This construct lets us define some really incredible things as a user of JonPRL. For example, the “squash” of a type is supposed to be a type which is occupied by <code>&lt;&gt;</code> (and only <code>&lt;&gt;</code>) if and only if there was an occupant of the original type. You can define these in HoTT with higher inductive types. Or, you can define these in this type theory as</p>
<pre class="jonprl"><code>    Operator squash : (0).
    [squash(A)] =def= [image(A; lam(x. &lt;&gt;))]</code></pre>
<p><code>x ∈ squash(A)</code> if and only if we can construct an <code>a</code> so that <code>a ∈ A</code> and <code>lam(x. &lt;&gt;) a ~ x</code>. Clearly <code>x</code> must be <code>&lt;&gt;</code> and we can construct such an <code>a</code> if and only if <code>A</code> is nonempty.</p>
<p>We can also define the set-union of two types. Something is supposed to be in the set union if and only if it’s in one or the other. Two define such a thing with an image type we have</p>
<pre class="jonprl"><code>    Operator union : (0).
    [union(A; B)] =def= [image((x : unit + unit) * decide(x; _.A; _.B); lam(x.snd(x)))]</code></pre>
<p>This one is a bit more complicated. The domain of things we’re applying our function to this time is</p>
<pre class="jonprl"><code>    (x : unit + unit) * decide(x; _.A; _.B)</code></pre>
<p>This is a dependent pair, sometimes called a Σ type. The first component is a boolean; if it is <code>true</code> the second component is of type <code>A</code>, and otherwise it’s of type <code>B</code>. So for every term of type <code>A</code> or <code>B</code>, there’s a term of this Σ type. In fact, we can recover that original term of type <code>A</code> or <code>B</code> by just grabbing the second component of the term! We don’t have to worry about the type of such an operation because we’re not creating something with a function type, just something in <code>base</code>.</p>
<p><code>union</code>s let us define an absolutely critical admissible rule in our system. JonPRL has this propositional reflection of the equality judgment and membership, but in Martin-Löf’s type theory, membership is non-negatable. By this I mean that if we have some <code>a</code> so that <code>a = a ∈ A</code> doesn’t hold, we won’t be able to prove <code>=(a; a; A) -&gt; void</code>. See in order to prove such a thing we first have to prove that <code>=(a; b; A) -&gt; void</code> is a type, which means proving that <code>=(a; a; A)</code> is a type.</p>
<p>In order to prove that <code>=(a; b; A)</code> is a proposition we have to prove <code>=(a; a; A)</code>, <code>=(b; b; A)</code>, and <code>=(A; A; U{i})</code>. The process of proving these will actually also show that the corresponding judgments, <code>a ∈ A</code>, <code>b ∈ A</code>, and <code>A ∈ U{i}</code> hold.</p>
<p>However, in the case that <code>a</code> and <code>b</code> are the same term this is just the same as proving <code>=(a; b; A)</code>! So <code>=(a; a; A)</code> is a proposition only if it’s true. However, we can add a rule that says that <code>=(a; b; A)</code> is a proposition if <code>a = a ∈ (A ∪ base)</code> and similarly for <code>b</code>! This fixes our negatibility issue because we can just prove that <code>=(a; a; base)</code>, something that may be true even if <code>a</code> is not equal in <code>A</code>. Before having a function take a <code>member(...)</code> was useless (<code>member(a; A)</code> is just thin sugar for <code>=(a; a; A)</code>! <code>member(a; A)</code> is a proposition if and only if <code>a = a ∈ A</code> holds, in other words, it’s a proposition if and only if it’s true! With this new rule, we can prove <code>member(a; A)</code> is a proposition if <code>A ∈ U{i}</code> and <code>a ∈ base</code>, a much weaker set of conditions that are almost always true. We can apply this special rule in JonPRL with <code>eq-eq-base</code> instead of just <code>eq-cd</code> like the rest of our equality rules.</p>
<h2 id="the-main-result">The Main Result</h2>
<p>Now let’s actually begin proving Russell’s paradox. To start with some notation.</p>
<pre class="jonprl"><code>    Infix 20 &quot;∈&quot; := member.
    Infix 40 &quot;~&quot; := ceq.
    Infix 60 &quot;∪&quot; := bunion.
    Prefix 40 &quot;¬&quot; := not.</code></pre>
<p>This let’s us say <code>a ∈ b</code> instead of <code>member(a; b)</code>. JonPRL recently grew this ability to add transparent notation to terms, it makes our theorems a <em>lot</em> prettier.</p>
<p>Next we define the central term to our proof:</p>
<pre class="jonprl"><code>    Operator Russell : ().
    [Russell] =def= [{x : U{i} | ¬ (x ∈ x)}]</code></pre>
<p>Here we’ve defined <code>Russell</code> as shorthand for a subset type, in particular a subset of <code>U{i}</code> (the universe of types). <code>x ∈ Russell</code> if <code>x ∈ U{i}</code> and <code>¬ (x ∈ x)</code>. Now normally we won’t be able to prove that this is a type (specifically <code>x ∈ x</code> is going to be a problem), but in our case we’ll have some help from an assumption that <code>U{i} ∈ U{i}</code>.</p>
<p>Now we begin to define a small set of tactics that we’ll want. These tactics are really where the fiddly bits of using JonPRL’s tactic system come into play. If you’re just reading this for the intuition as to why <code>Type ∈ Type</code> is bad just skip this. You’ll still understand the construction even if you don’t understand these bits of the proof.</p>
<p>First we have a tactic which finds an occurrence of <code>H : A + B</code> in the context and eliminate it. This gives us two goals, one with an <code>A</code> and one with a <code>B</code>. To do this we use match, which gives us something like <code>match goal with</code> in Coq.</p>
<pre class="jonprl"><code>    Tactic break-plus {
      @{ [H : _ + _ |- _] =&gt; elim &lt;H&gt;; thin &lt;H&gt; }
    }.</code></pre>
<p>Note the syntax <code>[H : ... |- ...]</code> to match on a sequent. In particular here we just have <code>_ + _</code> and <code>_</code>. Next we have a tactic <code>bunion-eq-right</code>. It’s to help us work with <code>bunion</code>s (unions). Basically it turns <code>=(M; N; bunion(A; B))</code> into</p>
<pre class="jonprl"><code>    =(lam(x.snd(x)) &lt;&lt;&gt;, M&gt;; lam(x.snd(x)) &lt;&lt;&gt;, N&gt;; bunion(A; B))</code></pre>
<p>This is actually helpful because it turns out that once we unfold <code>bunion</code> we have to prove that <code>M</code> and <code>N</code> are in an image type, remember that <code>bunion</code> is just a thin layer of sugar on top of image types. In order to prove something is in the image type it needs to be of the form <code>f a</code> where <code>f</code> in our case is <code>lam(x. snd(x))</code>.</p>
<p>This is done with</p>
<pre class="jonprl"><code>    Tactic bunion-eq-right {
      @{ [|- =(M; N; L ∪ R)] =&gt;
           csubst [M ~ lam(x. snd(x)) &lt;inr(&lt;&gt;), M&gt;] [h.=(h;_;_)];
           aux { unfold &lt;snd&gt;; reduce; auto };
           csubst [N ~ lam(x. snd(x)) &lt;inr(&lt;&gt;), N&gt;] [h.=(_;h;_)];
           aux { unfold &lt;snd&gt;; reduce; auto };
      }
    }.</code></pre>
<p>The key here is <code>csubst</code>. It takes a <code>ceq</code> as its first argument and a “targeting”. It then tries to replace each occurrence of the left side of the equality with the right. To find each occurrence the targeting maps a variable to each occurrence. We’re allowed to use wildcards in the targeting as well. It also relegates actually proving the equality into a new subgoal. It’s easy enough to prove so we demonstrate it with <code>aux {unfold &lt;snd&gt;; reduce; auto}</code>.</p>
<p>We only need to apply this tactic after <code>eq-eq-base</code>, this applies that rule I mentioned earlier about proving equalities to be well-formed in a much more liberal environment. Therefore we wrap those two tactics into one more convenient package.</p>
<pre class="jonprl"><code>    Tactic eq-base-tac {
      @{ [|- =(=(M; N; A); =(M&#39;; N&#39;; A&#39;); _)] =&gt;
           eq-eq-base; auto;
           bunion-eq-right; unfold &lt;bunion&gt;
       }
    }.</code></pre>
<p>There is one last tactic in this series, this one to prove that <code>member(X; X) ∈ U{i'}</code> is well-formed (a type). It starts by unfolding <code>member</code> into <code>=(=(X; X; X); =(X; X; X); U{i})</code> and then applying the new tactic. Then we do other things. These things aren’t pretty. I suggest we just ignore them.</p>
<pre class="jonprl"><code>    Tactic impredicativity-wf-tac {
      unfold &lt;member&gt;; eq-base-tac;
      eq-cd; ?{@{[|- =(_; _; base)] =&gt; auto}};
      eq-cd @i&#39;; ?{break-plus}; reduce; auto
    }.</code></pre>
<p>Finally we have a tactic to prove that if we have <code>not(P)</code> and <code>P</code> existing in the context proves <code>void</code>. This is another nice application match</p>
<pre class="jonprl"><code>    Tactic contradiction {
      unfold &lt;not implies&gt;;
      @{ [H : P -&gt; void, H&#39; : P |- void] =&gt;
           elim &lt;H&gt; [H&#39;];
           unfold &lt;member&gt;;
           auto
       }
    }.</code></pre>
<p>We start by unfolding <code>not</code> and <code>implies</code>. This gives us <code>P -&gt; void</code> and <code>P</code>. From there, we just apply one to the other giving us a void as we wanted.</p>
<p>We’re now ready to prove our theorem. We start with</p>
<pre class="jonprl"><code>    Theorem type-not-in-type : [¬ (U{i} ∈ U{i})] {
    }.</code></pre>
<p>We now have the main subgoal</p>
<pre><code>Remaining subgoals:

[main] ⊢ not(member(U{i}; U{i}))</code></pre>
<p>We can start by unfold <code>not</code> and <code>implies</code>. Remember that <code>not</code> isn’t a built in thing, it’s just sugar. By unfolding it we get the more primitive form, something that actually apply the <code>intro</code> tactic to.</p>
<pre class="jonprl"><code>    {
      unfold &lt;not implies&gt;; intro
    }</code></pre>
<p>Once unfolded, we’d get a goal along the lines of <code>member(U{i}; U{i}) -&gt; void</code>. We immediately apply <code>intro</code> to this though. Now we have two subgoals; one is the result of applying <code>intro</code>, namely a hypothesis <code>x : member(U{i}; U{i})</code> and a goal <code>void</code>. The second subgoal is the “well-formedness” obligation.</p>
<p>We have to prove that <code>member(U{i}; U{i})</code> is a type in order to apply the <code>intro</code> tactic. This is a crucial difference between Coq-like systems and these proof-refinement logics. The process of demonstrating that what you’re proving is a proposition is intermingled with actually constructing the proof. It means you get to apply all the normal mathematical tools you have for proving things to be true in order to prove that they’re types. This gives us a lot of flexibility, but at the cost of sometimes annoying subgoals. They’re annotated with <code>[aux]</code> (as opposed to <code>[main]</code>). This means we can target them all at once using with the <code>aux</code> tactics.</p>
<p>To summarize that whole paragraph as JonPRL would say it, our proof state is</p>
<pre><code>[main]
1. x : member(U{i}; U{i})
⊢ void

[aux] ⊢ member(member(U{i}; U{i}); U{i&#39;})</code></pre>
<p>Let’s get rid of that auxiliary subgoal using that <code>impredictivity-wf-tac</code>, this subgoal is in fact exactly what it was made for.</p>
<pre class="jonprl"><code>    {
      unfold &lt;not implies&gt;; intro
      aux { impredicativity-wf-tac };
    }</code></pre>
<p>This picks off that <code>[aux]</code> goal leaving us with just</p>
<pre><code>[main]
1. x : member(U{i}; U{i})
⊢ void</code></pre>
<p>Now we need to prove some lemmas. They state that <code>Russell</code> is actually a type. This is possible to do here and only here because we’ll need to actually use <code>x</code> in the process of proving this. It’s a very nice example of what explicitly proving well-formedness can give you! After all, the process of demonstrating that <code>Russell</code> is a type is nontrivial and only possible in this hypothetical context, rather than just hoping that JonPRL is clever enough to figure that out for itself we get to demonstrate it locally.</p>
<p>We’re going to use the <code>assert</code> tactic to get these lemmas. This lets us state a term, prove it as a subgoal and use it as a hypothesis in the main goal. If you’re logically minded, it’s cut.</p>
<pre class="jonprl"><code>    {
      unfold &lt;not implies&gt;; intro;
      aux { impredicativity-wf-tac };

      assert [Russell ∈ U{i}] &lt;russell-wf&gt;;
    }</code></pre>
<p>The thing in angle brackets is the name it will get in our hypothetical context for the main goal. This leaves us with two subgoals. The <code>aux</code> one being the assertion and the <code>main</code> one being allowed to assume it.</p>
<pre><code>[aux]
1. x : member(U{i}; U{i})
⊢ member(Russell; U{i})

[main]
1. x : member(U{i}; U{i})
2. russell-wf : member(Russell; U{i})
⊢ void</code></pre>
<p>We can prove this by basically working our way towards using <code>impredicativity-wf-tac</code>. We’ll use <code>aux</code> again to target the <code>aux</code> subgoal. We’ll start by unfolding everything and applying <code>eq-cd</code>.</p>
<pre class="jonprl"><code>    {
      unfold &lt;not implies&gt;; intro;
      aux { impredicativity-wf-tac };

      assert [Russell ∈ U{i}] &lt;russell-wf&gt;;
      aux {
        unfold &lt;member Russell&gt;; eq-cd; auto;
      };
    }</code></pre>
<p><em>Remember that <code>Russell</code> is <code>{x : U{i} | ¬ (x ∈ x)}</code></em></p>
<p>We just applied <code>eq-cd</code> to a subset type (<code>Russell</code>), so we get two subgoals. One says that <code>U{i}</code> is a type, one says that if <code>x ∈ U{i}</code> then <code>¬ (x ∈ x)</code> is also a type. In essence this just says that a subset type is a type if both components are types. The former goal is quite straightforward so we applied <code>auto</code> and take care of it. Now we have one new subgoal to handle</p>
<pre><code>[main]
1. x : =(U{i}; U{i}; U{i})
2. x&#39; : U{i}
⊢ =(not(member(x&#39;; x&#39;)); not(member(x&#39;; x&#39;)); U{i})

[main]
1. x : member(U{i}; U{i})
2. russell-wf : member(Russell; U{i})
⊢ void</code></pre>
<p>The second subgoal is just the rest of the proof, and the first subgoal is what we want to handle. It says that if we have a type <code>x</code>, then <code>not(member(x; x))</code> is a type (albeit in ugly notation). To prove this we have to unfold <code>not</code>. So we’ll do this and apply <code>eq-cd</code> again.</p>
<pre class="jonprl"><code>    {
      unfold &lt;not implies&gt;; intro;
      aux { impredicativity-wf-tac };

      assert [Russell ∈ U{i}] &lt;russell-wf&gt;;
      aux {
        unfold &lt;member Russell&gt;; eq-cd; auto;
        unfold &lt;not implies&gt;; eq-cd; auto;
      };
    }</code></pre>
<p>Remember that <code>not(P)</code> desugars to <code>P -&gt; void</code>. Applying <code>eq-cd</code> is going to give us two subgoals, <code>P</code> is a type and <code>void</code> is a type. However, <code>member(void; U{i})</code> is pretty easy to prove, so we apply <code>auto</code> again which takes care of one of our two new goals. Now we just have</p>
<pre><code>[main]
1. x : =(U{i}; U{i}; U{i})
2. x&#39; : U{i}
⊢ =(member(x&#39;; x&#39;); member(x&#39;; x&#39;); U{i})

[main]
1. x : member(U{i}; U{i})
2. russell-wf : member(Russell; U{i})
⊢ void</code></pre>
<p>Now we’re getting to the root of the issue. We’re trying to prove that <code>member(x'; x')</code> is a type. This is happily handled by <code>impredicativity-wf-tac</code> which will use our assumption that <code>U{i} ∈ U{i}</code> because it’s smart like that.</p>
<pre class="jonprl"><code>    {
      unfold &lt;not implies&gt;; intro;
      aux { impredicativity-wf-tac };

      assert [Russell ∈ U{i}] &lt;russell-wf&gt;;
      aux {
        unfold &lt;member Russell&gt;; eq-cd; auto;
        unfold &lt;not implies&gt;; eq-cd; auto;
        impredicativity-wf-tac
      };
    }</code></pre>
<p>Now we just have that main goal with the assumption <code>russell-wf</code> added.</p>
<pre><code>[main]
1. x : member(U{i}; U{i})
2. russell-wf : member(Russell; U{i})
⊢ void</code></pre>
<p>Now we have a similar well-formedness goal to assert and prove. We want to prove that <code>∈(Russell; Russell)</code> is a type. This is easier though; we can prove it easily using <code>impredicativity-wf-tac</code>.</p>
<pre class="jonprl"><code>    {
      unfold &lt;not implies&gt;; intro;
      aux { impredicativity-wf-tac };

      assert [Russell ∈ U{i}] &lt;russell-wf&gt;;
      aux {
        unfold &lt;member Russell&gt;; eq-cd; auto;
        unfold &lt;not implies&gt;; eq-cd; auto;
        impredicativity-wf-tac
      };

      assert [(Russell ∈ Russell) ∈ U{i}] &lt;russell-in-russell-wf&gt;;
      aux { impredicativity-wf-tac; cum @i; auto };
    }</code></pre>
<p>That <code>cum @i</code> is a quirk of <code>impredicativity-wf-tac</code>. It basically means that instead of proving <code>=(...; ...; U{i'})</code> we can prove <code>=(...; ...; U{i})</code> since <code>U{i}</code> is a universe below <code>U{i'}</code> and all universes are cumulative.</p>
<p>Our goal is now</p>
<pre><code>[main]
1. x : member(U{i}; U{i})
2. russell-wf : member(Russell; U{i})
3. russell-in-russell-wf : member(member(Russell; Russell); U{i})
⊢ void</code></pre>
<p>Ok, so now the reasoning can start now that we have all these well-formedness lemmas. Our proof sketch is basically as follows</p>
<ol type="1">
<li>Prove that <code>Russell ∈ Russell</code> is false. This is because if <code>Russell</code> <em>was</em> in <code>Russell</code> then by definition of <code>Russell</code> it isn’t in <code>Russell</code>.</li>
<li>Since <code>not(Russell ∈ Russell)</code> holds, then <code>Russell ∈ Russell</code> holds.</li>
<li>Hilarity ensues.</li>
</ol>
<p>Here’s the first assertion:</p>
<pre class="jonprl"><code>    {
      unfold &lt;not implies&gt;; intro;
      aux { impredicativity-wf-tac };

      assert [Russell ∈ U{i}] &lt;russell-wf&gt;;
      aux {
        unfold &lt;member Russell&gt;; eq-cd; auto;
        unfold &lt;not implies&gt;; eq-cd; auto;
        impredicativity-wf-tac
      };

      assert [(Russell ∈ Russell) ∈ U{i}] &lt;russell-in-russell-wf&gt;;
      aux { impredicativity-wf-tac; cum @i; auto };

      assert [¬ (Russell ∈ Russell)] &lt;not-russell-in-russell&gt;;
    }</code></pre>
<p>Here are our subgoals:</p>
<pre><code>[aux]
1. x : member(U{i}; U{i})
2. russell-wf : member(Russell; U{i})
3. russell-in-russell-wf : member(member(Russell; Russell); U{i})
⊢ not(member(Russell; Russell))

[main]
1. x : member(U{i}; U{i})
2. russell-wf : member(Russell; U{i})
3. russell-in-russell-wf : member(member(Russell; Russell); U{i})
4. russell-not-in-russell : not(member(Russell; Russell))
⊢ void</code></pre>
<p>We want to prove that first one. To start, let’s unfold that <code>not</code> and move <code>member(Russell; Russell)</code> to the hypothesis and use it to prove <code>void</code>. We do this with <code>intro</code>.</p>
<pre class="jonprl"><code>    {
      unfold &lt;not implies&gt;; intro;
      aux { ... };

      assert [Russell ∈ U{i}] &lt;russell-wf&gt;;
      aux { ... };

      assert [(Russell ∈ Russell) ∈ U{i}] &lt;russell-in-russell-wf&gt;;
      aux { ... };

      assert [¬ (Russell ∈ Russell)] &lt;not-russell-in-russell&gt;;
      aux {
        unfold &lt;not implies&gt;;
        intro @i; aux {assumption};
      }
    }</code></pre>
<p>Notice that the well-formedness goal that <code>intro</code> generated is handled by our assumption! After all, it’s just <code>member(Russell; Russell) ∈ U{i}</code>, we already proved it. Now our subgoals look like this</p>
<pre><code>[main]
1. x : member(U{i}; U{i})
2. russell-wf : member(Russell; U{i})
3. russell-in-russell-wf : member(member(Russell; Russell); U{i})
4. x&#39; : member(Russell; Russell)
⊢ void

[main]
1. x : member(U{i}; U{i})
2. russell-wf : member(Russell; U{i})
3. russell-in-russell-wf : member(member(Russell; Russell); U{i})
4. russell-not-in-russell : not(member(Russell; Russell))
⊢ void</code></pre>
<p>Here’s our clever plan</p>
<ol type="1">
<li>Since <code>Russell ∈ Russell</code>, there’s an <code>X : Russell</code> so that <code>ceq(Russell; X)</code> holds</li>
<li>Since <code>X : Russell</code>, we can unfold it to say that <code>X : {x ∈ U{i} | ¬ (x ∈ x)}</code></li>
<li>We can apply the elimination principle for subset types to <code>X</code> and derive that <code>¬ (X ∈ X)</code></li>
<li>Rewriting by <code>ceq(Russell; X)</code> gives <code>¬ (Russell; Russell)</code></li>
<li>Now we have a contradiction</li>
</ol>
<p>Let’s start explaining this to JonPRL by introducing that <code>X</code> (here called <code>R</code>). We’ll assert an <code>R : Russell</code> such that <code>R ~ Russell</code>. We do this using dependent pairs (here written <code>(x : A) * B(x)</code>).</p>
<pre class="jonprl"><code>    {
      unfold &lt;not implies&gt;; intro;
      aux { ... };

      assert [Russell ∈ U{i}] &lt;russell-wf&gt;;
      aux { ... };

      assert [(Russell ∈ Russell) ∈ U{i}] &lt;russell-in-russell-wf&gt;;
      aux { ... };

      assert [¬ (Russell ∈ Russell)] &lt;not-russell-in-russell&gt;;
      aux {
        unfold &lt;not implies&gt;;
        intro @i; aux {assumption};
        assert [(R : Russell) * R ~ Russell] &lt;R-with-prop&gt;;
        aux {
          intro [Russell] @i; auto
        };
      }
    }</code></pre>
<p>We’ve proven this by <code>intro</code>. For proving dependent products we provide an explicit witness for the first component. Basically to prove <code>(x : A) * B(x)</code> we say <code>intro [Foo]</code>. We then have a goal <code>Foo ∈ A</code> and <code>B(Foo)</code>. Since subgoals are fully independent of each other, we have to give the witness for the first component upfront. It’s a little awkward, Jon’s <a href="https://github.com/jonsterling/dependency-in-refinement-logics">working on it</a> :).</p>
<p>In this case we use <code>intro [Russell]</code>. After this we have to prove that this witness has type <code>Russell</code> and then prove the second component holds. Happily, <code>auto</code> takes care of both of these obligations so <code>intro [Russell] @i; auto</code> handles it all.</p>
<p>Now we promptly eliminate this pair. It gives us two new facts, that <code>R : Russell</code> and <code>R ~ Russell</code> hold.</p>
<pre class="jonprl"><code>    {
      unfold &lt;not implies&gt;; intro;
      aux { ... };

      assert [Russell ∈ U{i}] &lt;russell-wf&gt;;
      aux { ... };

      assert [(Russell ∈ Russell) ∈ U{i}] &lt;russell-in-russell-wf&gt;;
      aux { ... };

      assert [¬ (Russell ∈ Russell)] &lt;not-russell-in-russell&gt;;
      aux {
        unfold &lt;not implies&gt;;
        intro @i; aux {assumption};
        assert [(R : Russell) * R ~ Russell] &lt;R-with-prop&gt;;
        aux {
          intro [Russell] @i; auto
        };

        elim &lt;R-with-prop&gt;; thin &lt;R-with-prop&gt;
      }
    }</code></pre>
<p>This leaves our goal as</p>
<pre><code>[main]
1. x : member(U{i}; U{i})
2. russell-wf : member(Russell; U{i})
3. russell-in-russell-wf : member(member(Russell; Russell); U{i})
4. x&#39; : member(Russell; Russell)
5. s : Russell
6. t : ceq(s; Russell)
⊢ void

[main]
1. x : member(U{i}; U{i})
2. russell-wf : member(Russell; U{i})
3. russell-in-russell-wf : member(member(Russell; Russell); U{i})
4. russell-not-in-russell : not(member(Russell; Russell))
⊢ void</code></pre>
<p>Now let’s invert on the hypothesis that <code>s : Russell</code>; we want to use it to conclude that <code>¬ (s ∈ s)</code> holds since that will give us <code>¬ (R ∈ R)</code>.</p>
<pre class="jonprl"><code>    {
      unfold &lt;not implies&gt;; intro;
      aux { ... };

      assert [Russell ∈ U{i}] &lt;russell-wf&gt;;
      aux { ... };

      assert [(Russell ∈ Russell) ∈ U{i}] &lt;russell-in-russell-wf&gt;;
      aux { ... };

      assert [¬ (Russell ∈ Russell)] &lt;not-russell-in-russell&gt;;
      aux {
        unfold &lt;not implies&gt;;
        intro @i; aux {assumption};
        assert [(R : Russell) * R ~ Russell] &lt;R-with-prop&gt;;
        aux {
          intro [Russell] @i; auto
        };

        elim &lt;R-with-prop&gt;; thin &lt;R-with-prop&gt;;
        unfold &lt;Russell&gt;; elim #5;
      }
    }</code></pre>
<p>Now that we’ve unfolded all of those <code>Russell</code>s our goal is a little bit harder to read, remember to mentally substitute <code>{x : U{i} | not(member(x; x))}</code> as <code>Russell</code>.</p>
<pre><code>[main]
1. x : member(U{i}; U{i})
2. russell-wf : member({x:U{i} | not(member(x; x))}; U{i})
3. russell-in-russell-wf : member(member({x:U{i} | not(member(x; x))}; {x:U{i} | not(member(x; x))}); U{i})
4. x&#39; : member({x:U{i} | not(member(x; x))}; {x:U{i} | not(member(x; x))})
5. s : {x:U{i} | not(member(x; x))}
6. x&#39;&#39; : U{i}
7. [t&#39;] : not(member(x&#39;&#39;; x&#39;&#39;))
8. t : ceq(x&#39;&#39;; {x:U{i} | not(member(x; x))})
⊢ void

[main]
1. x : member(U{i}; U{i})
2. russell-wf : member(Russell; U{i})
3. russell-in-russell-wf : member(member(Russell; Russell); U{i})
4. russell-not-in-russell : not(member(Russell; Russell))
⊢ void</code></pre>
<p>Now we use #7 to derive that <code>not(member(Russell; Russell))</code> holds.</p>
<pre class="jonprl"><code>    {
      unfold &lt;not implies&gt;; intro;
      aux { ... };

      assert [Russell ∈ U{i}] &lt;russell-wf&gt;;
      aux { ... };

      assert [(Russell ∈ Russell) ∈ U{i}] &lt;russell-in-russell-wf&gt;;
      aux { ... };

      assert [¬ (Russell ∈ Russell)] &lt;not-russell-in-russell&gt;;
      aux {
        unfold &lt;not implies&gt;;
        intro @i; aux {assumption};
        assert [(R : Russell) * R ~ Russell] &lt;R-with-prop&gt;;
        aux {
          intro [Russell] @i; auto
        };

        elim &lt;R-with-prop&gt;; thin &lt;R-with-prop&gt;;
        unfold &lt;Russell&gt;; elim #5;

        assert [¬ member(Russell; Russell)];
        aux {
          unfold &lt;Russell&gt;;
        };
      }
    }</code></pre>
<p>This leaves us with 3 subgoals, the first one being the assertion.</p>
<pre><code>[aux]
1. x : member(U{i}; U{i})
2. russell-wf : member({x:U{i} | not(member(x; x))}; U{i})
3. russell-in-russell-wf : member(member({x:U{i} | not(member(x; x))}; {x:U{i} | not(member(x; x))}); U{i})
4. x&#39; : member({x:U{i} | not(member(x; x))}; {x:U{i} | not(member(x; x))})
5. s : {x:U{i} | not(member(x; x))}
6. x&#39;&#39; : U{i}
7. [t&#39;] : not(member(x&#39;&#39;; x&#39;&#39;))
8. t : ceq(x&#39;&#39;; {x:U{i} | not(member(x; x))})
⊢ not(member({x:U{i} | not(member(x; x))}; {x:U{i} | not(member(x; x))}))

[main]
1. x : member(U{i}; U{i})
2. russell-wf : member({x:U{i} | not(member(x; x))}; U{i})
3. russell-in-russell-wf : member(member({x:U{i} | not(member(x; x))}; {x:U{i} | not(member(x; x))}); U{i})
4. x&#39; : member({x:U{i} | not(member(x; x))}; {x:U{i} | not(member(x; x))})
5. s : {x:U{i} | not(member(x; x))}
6. x&#39;&#39; : U{i}
7. [t&#39;] : not(member(x&#39;&#39;; x&#39;&#39;))
8. t : ceq(x&#39;&#39;; {x:U{i} | not(member(x; x))})
9. H : not(member(Russell; Russell))
⊢ void

[main]
1. x : member(U{i}; U{i})
2. russell-wf : member(Russell; U{i})
3. russell-in-russell-wf : member(member(Russell; Russell); U{i})
4. russell-not-in-russell : not(member(Russell; Russell))
⊢ void</code></pre>
<p>Now to prove this, what we need to do is substitute the unfolded <code>Russell</code> for <code>x''</code>; from there it’s immediate by assumption. We perform the substitution with <code>chyp-subst</code>. This takes a direction in which to substitute, which hypothesis to use, and another targeting telling us where to apply the substitution.</p>
<pre class="jonprl"><code>    {
      unfold &lt;not implies&gt;; intro;
      aux { ... };

      assert [Russell ∈ U{i}] &lt;russell-wf&gt;;
      aux { ... };

      assert [(Russell ∈ Russell) ∈ U{i}] &lt;russell-in-russell-wf&gt;;
      aux { ... };

      assert [¬ (Russell ∈ Russell)] &lt;not-russell-in-russell&gt;;
      aux {
        unfold &lt;not implies&gt;;
        intro @i; aux {assumption};
        assert [(R : Russell) * R ~ Russell] &lt;R-with-prop&gt;;
        aux {
          intro [Russell] @i; auto
        };

        elim &lt;R-with-prop&gt;; thin &lt;R-with-prop&gt;;
        unfold &lt;Russell&gt;; elim #5;

        assert [¬ member(Russell; Russell)];
        aux {
          unfold &lt;Russell&gt;;
          chyp-subst ← #8 [h. ¬ (h ∈ h)];
        };
      }
    }</code></pre>
<p>This leaves us with a much more tractable goal.</p>
<pre><code>[main]
1. x : member(U{i}; U{i})
2. russell-wf : member({x:U{i} | not(member(x; x))}; U{i})
3. russell-in-russell-wf : member(member({x:U{i} | not(member(x; x))}; {x:U{i} | not(member(x; x))}); U{i})
4. x&#39; : member({x:U{i} | not(member(x; x))}; {x:U{i} | not(member(x; x))})
5. s : {x:U{i} | not(member(x; x))}
6. x&#39;&#39; : U{i}
7. [t&#39;] : not(member(x&#39;&#39;; x&#39;&#39;))
8. t : ceq(x&#39;&#39;; {x:U{i} | not(member(x; x))})
⊢ not(member(x&#39;&#39;; x&#39;&#39;))

[main]
1. x : member(U{i}; U{i})
2. russell-wf : member({x:U{i} | not(member(x; x))}; U{i})
3. russell-in-russell-wf : member(member({x:U{i} | not(member(x; x))}; {x:U{i} | not(member(x; x))}); U{i})
4. x&#39; : member({x:U{i} | not(member(x; x))}; {x:U{i} | not(member(x; x))})
5. s : {x:U{i} | not(member(x; x))}
6. x&#39;&#39; : U{i}
7. [t&#39;] : not(member(x&#39;&#39;; x&#39;&#39;))
8. t : ceq(x&#39;&#39;; {x:U{i} | not(member(x; x))})
9. H : not(member(Russell; Russell))
⊢ void

[main]
1. x : member(U{i}; U{i})
2. russell-wf : member(Russell; U{i})
3. russell-in-russell-wf : member(member(Russell; Russell); U{i})
4. russell-not-in-russell : not(member(Russell; Russell))
⊢ void</code></pre>
<p>We’d like to just apply <code>assumption</code> but it’s not immediately applicable due to some technically details (basically we can only apply an assumption in a proof irrelevant context but we have to unfold <code>Russell</code> and introduce it to demonstrate that it’s irrelevant). So just read what’s left as a (very) convoluted <code>assumption</code>.</p>
<pre class="jonprl"><code>    {
      unfold &lt;not implies&gt;; intro;
      aux { ... };

      assert [Russell ∈ U{i}] &lt;russell-wf&gt;;
      aux { ... };

      assert [(Russell ∈ Russell) ∈ U{i}] &lt;russell-in-russell-wf&gt;;
      aux { ... };

      assert [¬ (Russell ∈ Russell)] &lt;not-russell-in-russell&gt;;
      aux {
        unfold &lt;not implies&gt;;
        intro @i; aux {assumption};
        assert [(R : Russell) * R ~ Russell] &lt;R-with-prop&gt;;
        aux {
          intro [Russell] @i; auto
        };

        elim &lt;R-with-prop&gt;; thin &lt;R-with-prop&gt;;
        unfold &lt;Russell&gt;; elim #5;

        assert [¬ (Russell; Russell)];
        aux {
          unfold &lt;Russell&gt;;
          chyp-subst ← #8 [h. ¬ (h ∈ h)];
          unfold &lt;not implies&gt;
          intro; aux { impredicativity-wf-tac };
          contradiction
        };
      }
    }</code></pre>
<p>Now we’re almost through this assertion, our subgoals look like this (pay attention to 9 and 4)</p>
<pre><code>[main]
1. x : member(U{i}; U{i})
2. russell-wf : member({x:U{i} | not(member(x; x))}; U{i})
3. russell-in-russell-wf : member(member({x:U{i} | not(member(x; x))}; {x:U{i} | not(member(x; x))}); U{i})
4. x&#39; : member({x:U{i} | not(member(x; x))}; {x:U{i} | not(member(x; x))})
5. s : {x:U{i} | not(member(x; x))}
6. x&#39;&#39; : U{i}
7. [t&#39;] : not(member(x&#39;&#39;; x&#39;&#39;))
8. t : ceq(x&#39;&#39;; {x:U{i} | not(member(x; x))})
9. H : not(member(Russell; Russell))
⊢ void

[main]
1. x : member(U{i}; U{i})
2. russell-wf : member(Russell; U{i})
3. russell-in-russell-wf : member(member(Russell; Russell); U{i})
4. russell-not-in-russell : not(member(Russell; Russell))
⊢ void</code></pre>
<p>Once we unfold that <code>Russell</code> we have an immediate contradiction so <code>unfold &lt;Russell&gt;; contradiction</code> solves it.</p>
<pre class="jonprl"><code>    {
      unfold &lt;not implies&gt;; intro;
      aux { ... };

      assert [Russell ∈ U{i}] &lt;russell-wf&gt;;
      aux { ... };

      assert [(Russell ∈ Russell) ∈ U{i}] &lt;russell-in-russell-wf&gt;;
      aux { ... };

      assert [¬ (Russell ∈ Russell)] &lt;not-russell-in-russell&gt;;
      aux {
        unfold &lt;not implies&gt;;
        intro @i; aux {assumption};
        assert [(R : Russell) * R ~ Russell] &lt;R-with-prop&gt;;
        aux {
          intro [Russell] @i; auto
        };

        elim &lt;R-with-prop&gt;; thin &lt;R-with-prop&gt;;
        unfold &lt;Russell&gt;; elim #5;

        assert [¬ (Russell; Russell)];
        aux {
          unfold &lt;Russell&gt;;
          chyp-subst ← #8 [h. ¬ (h ∈ h)];
          unfold &lt;not implies&gt;;
          intro; aux { impredicativity-wf-tac };
          contradiction
        };

        unfold &lt;Russell&gt;; contradiction
      }
    }</code></pre>
<p>This takes care of this subgoal, so now we’re back on the main goal. This time though we have an extra hypothesis which will provide the leverage we need to prove our next assertion.</p>
<pre><code>[main]
1. x : member(U{i}; U{i})
2. russell-wf : member(Russell; U{i})
3. russell-in-russell-wf : member(member(Russell; Russell); U{i})
4. russell-not-in-russell : not(member(Russell; Russell))
⊢ void</code></pre>
<p>Now we’re going to claim that <code>Russell</code> is in fact a member of <code>Russell</code>. This will follow from the fact that we’ve proved already that <code>Russell</code> isn’t in <code>Russell</code> (yeah, it seems pretty paradoxical already).</p>
<pre class="jonprl"><code>    {
      unfold &lt;not implies&gt;; intro;
      aux { ... };

      assert [Russell ∈ U{i}] &lt;russell-wf&gt;;
      aux { ... };

      assert [(Russell ∈ Russell) ∈ U{i}] &lt;russell-in-russell-wf&gt;;
      aux { ... };

      assert [¬ (Russell ∈ Russell)] &lt;not-russell-in-russell&gt;;
      aux { ... };

      assert [Russell ∈ Russell];
   }</code></pre>
<p>Giving us</p>
<pre><code>[aux]
1. x : member(U{i}; U{i})
2. russell-wf : member(Russell; U{i})
3. russell-in-russell-wf : member(member(Russell; Russell); U{i})
4. russell-not-in-russell : not(member(Russell; Russell))
⊢ member(Russell; Russell)

[main]
1. x : member(U{i}; U{i})
2. russell-wf : member(Russell; U{i})
3. russell-in-russell-wf : member(member(Russell; Russell); U{i})
4. russell-not-in-russell : not(member(Russell; Russell))
5. H : member(Russell; Russell)
⊢ void</code></pre>
<p>Proving this is pretty straightforward, we only have to demonstrate that <code>not(Russell ∈ Russell)</code> and <code>Russell ∈ U{i}</code>, both of which we have as assumptions. The rest of the proof is just more well-formedness goals.</p>
<p>First we unfold everything and apply <code>eq-cd</code>. This gives us 3 subgoals, the first two are <code>Russell ∈ U{i}</code> and <code>¬(Russell ∈ Russell)</code>. Since we have these as assumptions we’ll use <code>main {assumption}</code>. That will target both these goals and prove them immediately. Here by using <code>main</code> we avoid applying this to the well-formedness goal, which in this case actually <em>isn’t</em> the assumption.</p>
<pre class="jonprl"><code>    {
      unfold &lt;not implies&gt;; intro
      aux { ... };

      assert [Russell ∈ U{i}] &lt;russell-wf&gt;;
      aux { ... };

      assert [(Russell ∈ Russell) ∈ U{i}] &lt;russell-in-russell-wf&gt;;
      aux { ... };

      assert [¬ (Russell ∈ Russell)] &lt;not-russell-in-russell&gt;;
      aux { ... };

      assert [Russell ∈ Russell];
      aux {
        unfold &lt;member Russell&gt;; eq-cd;
        unfold &lt;member&gt;;

        main { assumption };
      };
    }</code></pre>
<p>This just leaves us with one awful well-formedness goal requiring us to prove that <code>not(=(x; x; x))</code> is a type if <code>x</code> is a type. We actually proved something similar back when we prove that <code>Russell</code> was well-formed. The proof is the same as then, just unfold, <code>eq-cd</code> and <code>impredicativity-wf-tac</code>. We use <code>?{!{auto}}</code> to only apply <code>auto</code> in a subgoal where it immediately proves it. Here <code>?{}</code> says “run this or do nothing” and <code>!{}</code> says “run this, if it succeeds stop, if it does anything else, fail”. This is not an interesting portion of the proof, don’t burn too many cycles trying to figure this out.</p>
<pre class="jonprl"><code>    {
      unfold &lt;not implies&gt;; intro
      aux { ... };

      assert [Russell ∈ U{i}] &lt;russell-wf&gt;;
      aux { ... };

      assert [(Russell ∈ Russell) ∈ U{i}] &lt;russell-in-russell-wf&gt;;
      aux { ... };

      assert [¬ (Russell ∈ Russell)] &lt;not-russell-in-russell&gt;;
      aux { ... };

      assert [Russell ∈ Russell] &lt;russell-in-russell&gt;;
      aux {
        unfold &lt;member Russell&gt;; eq-cd;
        unfold &lt;member&gt;;

        main { assumption };
        unfold &lt;not implies&gt;; eq-cd; ?{!{auto}};
        impredicativity-wf-tac;
      };
    }</code></pre>
<p>Now we just have the final subgoal to prove. We’re actually in a position to do so now.</p>
<pre><code>[main]
1. x : member(U{i}; U{i})
2. russell-wf : member(Russell; U{i})
3. russell-in-russell-wf : member(member(Russell; Russell); U{i})
4. russell-not-in-russell : not(member(Russell; Russell))
5. russell-in-russell : member(Russell; Russell)
⊢ void</code></pre>
<p>Now that we’ve shown <code>P</code> and <code>not(P)</code> hold at the same time all we need to do is apply <code>contradiction</code> and we’re done.</p>
<pre class="jonprl"><code>    Theorem type-not-in-type [¬ (U{i} ∈ U{i})] {
      unfold &lt;not implies&gt;; intro
      aux { ... };

      assert [Russell ∈ U{i}] &lt;russell-wf&gt;;
      aux { ... };

      assert [(Russell ∈ Russell) ∈ U{i}] &lt;russell-in-russell-wf&gt;;
      aux { ... };

      assert [¬ (Russell ∈ Russell)] &lt;not-russell-in-russell&gt;;
      aux { ... };

      assert [Russell ∈ Russell] &lt;russell-in-russell&gt;;
      aux { ... };

      contradiction
    }.</code></pre>
<p>And there you have it, a complete proof of Russell’s paradox fully formalized in JonPRL! We actually proved a slightly stronger result than just that the type of types cannot be in itself, we proved that at any point in the hierarchy of universes (the first of which is <code>Type</code>/<code>*</code>/whatever) if you tie it off, you’ll get a contradiction.</p>
<h2 id="wrap-up">Wrap Up</h2>
<p>I hope you found this proof interesting. Even if you’re not at all interested in JonPRL, it’s nice to see that allowing one to have <code>U{i} ∈ U{i}</code> or <code>* :: *</code> gives you the ability to have a type like <code>Russell</code> and with it, inhabit <code>void</code>. I also find it especially pleasing that we can prove something like this in JonPRL; it’s growing up so fast.</p>
<p><em>Thanks to Jon for greatly improving the original proof we had</em></p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Wed, 26 Aug 2015 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2015-08-26-type-in-type.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Solving Recursive Equations</title>
    <link>http://jozefg.bitbucket.org/posts/2015-08-14-solve-domains.html</link>
    <description><![CDATA[<div class="info">
    Posted on August 14, 2015
    
</div>
<div class="info">
    
    Tags: <a href="/tags/types.html">types</a>
    
</div>

<p>I wanted to write about something related to all the stuff I’ve been reading for research lately. I decided to talk about a super cool trick in a field called domain theory. It’s a method of generating a solution to a large class of recursive equations.</p>
<p>In order to go through this idea we’ve got some background to cover. I wanted to make this post readable even if you haven’t read too much domain theory (you do need to know what a functor/colimit is though, nothing crazy though). We’ll start with a whirlwind tutorial of the math behind domain theory. From there we’ll transform the problem of finding a solution to an equation into something categorically tractable. Finally, I’ll walk through the construction of a solution.</p>
<p>I decided not to show an example of applying this technique to model a language because that would warrant its own post, hopefully I’ll write about that soon :)</p>
<h2 id="basic-domain-theory">Basic Domain Theory</h2>
<p>The basic idea with domain theory comes from a simple problem. Suppose we want to model the lambda calculus. We want a collection of mathematical objects <code>D</code> so that we can treat element of <code>D</code> as a function <code>D -&gt; D</code> and each function <code>D -&gt; D</code> as an element of <code>D</code>. To see why this is natural, remember that we want to turn each program <code>E</code> into <code>d ∈ D</code>. If <code>E = λ x. E'</code> then we need to turn the function <code>e ↦ [e/x]E'</code> into a term. This means <code>D → D</code> needs to be embeddable in <code>D</code>. On the other hand, we might have <code>E = E' E''</code> in which case we need to turn <code>E'</code> into a function <code>D → D</code> so that we can apply it. This means we need to be able to embed <code>D</code> into <code>D → D</code>.</p>
<p>After this we can turn a lambda calculus program into a specific element of <code>D</code> and reason about its properties using the ambient mathematical tools for <code>D</code>. This is semantics, understanding programs by studying their meaning in some mathematical structure. In our specific case that structure is <code>D</code> with the isomorphism <code>D ≅ D → D</code>. However, there’s an issue! We know that <code>D</code> can’t just be a set because then there cannot be such an isomorphism! In the case where <code>D ≅ N</code>, then <code>D → D ≅ R</code> and there’s a nice proof by diagonalization that such an isomorphism cannot exist.</p>
<p>So what can we do? We know there are only countably many programs, but we’re trying to state that there exists an isomorphism between our programs (countable) and functions on them (uncountable). Well the issue is that we don’t really mean <em>all</em> functions on <code>D</code>, just the ones we can model as lambda terms. For example, the function which maps all divergent programs to <code>1</code> and all terminating ones to <code>0</code> need not be considered because there’s no lambda term for it! How do we consider “computable” functions though? It’s not obvious since we define computable functions using the lambda calculus, what we’re trying to model here. Let’s set aside this question for a moment.</p>
<p>Another question is how do we handle this program: <code>(λ x. x x) (λ x. x x)</code>? It doesn’t have a value after all! It doesn’t behave like a normal mathematical function because applying it to something doesn’t give us back a new term, it just runs forever! To handle this we do something really clever. We stop considering <em>just</em> a collection of terms and instead look at terms with an ordering relation <code>⊑</code>! The idea is that ⊑ represents definedness. A program which runs to a value is more defined than a program which just loops forever. Similarly, two functions behave the same on all inputs except for <code>0</code> where one loops we could say one is more defined than the other. What we’ll do is define ⊑ abstractly and then model programs into sets with such a relation defined upon them. In order to build up this theory we need a few definitions</p>
<p>A partially ordered set (poset) is a set <code>A</code> and a binary relation <code>⊑</code> where</p>
<ol type="1">
<li><code>a ⊑ a</code></li>
<li><code>a ⊑ b</code> and <code>b ⊑ c</code> implies <code>a ⊑ c</code></li>
<li><code>a ⊑ b</code> and <code>b ⊑ a</code> implies <code>a = b</code></li>
</ol>
<p>We often just denote the pair <code>&lt;A, ⊑&gt;</code> as <code>A</code> when the ordering is clear. With a poset <code>A</code>, of particular interest are chains in it. A chain is collection of elements <code>aᵢ</code> so that <code>aᵢ ⊑ aⱼ</code> if <code>i ≤ j</code>. For example, in the partial order of natural numbers and <code>≤</code>, a chain is just a run of ascending numbers. Another fundamental concept is called a least upper bound (lub). A lub of a subset <code>P ⊆ A</code> is an element of <code>x ∈ A</code> so that <code>y ∈ P</code> implies <code>y ⊑ x</code> and if this property holds for some <code>z</code> also in <code>A</code>, then <code>x ⊑ z</code>. So a least upper bound is just the smallest thing bigger than the subset. This isn’t always guaranteed to exist, for example, in our poset of natural numbers <code>N</code>, the subset <code>N</code> has no upper bounds at all! When such a lub does exist, we denote it with <code>⊔P</code>. Some partial orders have an interesting property, all chains in them have least upper bounds. We call this posets <em>complete</em> partial orders or cpos.</p>
<p>For example while <code>N</code> isn’t a cpo, <code>ω</code> (the natural numbers + an element greater than all of them) <em>is</em>! As a quick puzzle, can you show that all finite partial orders are in fact CPOs?</p>
<p>We can define a number of basic constructions on cpos. The most common is the “lifting” operation which takes a cpo <code>D</code> and returns <code>D⊥</code>, a cpo with a least element <code>⊥</code>. A cpo with such a least element is called “pointed” and I’ll write that as cppo (complete pointed partial order). Another common example, given two cppos, <code>D</code> and <code>E</code>, we can construct <code>D ⊗ E</code>. An element of this cppo is either <code>⊥</code> or <code>&lt;l, r&gt;</code> where <code>l ∈ D - {⊥}</code> and <code>r ∈ E - {⊥}</code>. This is called the smash product because it “smashes” the ⊥s out of the components. Similarly, there’s smash sums <code>D ⊕ E</code>.</p>
<p>The next question is the classic algebraic question to ask about a structure: what are the interesting functions on it? We’ll in particular be interested in functions which preserve the ⊑ relation and the taking of lub’s on chains. For this we have two more definitions:</p>
<ol type="1">
<li>A function is monotone if <code>x ⊑ y</code> implies <code>f(x) ⊑ f(y)</code></li>
<li>A function is continuous if it is monotone and for all chains <code>C</code>, <code>⊔ f(P) = f(⊔ P)</code>.</li>
</ol>
<p>Notably, the collection of cppos and continuous functions form a category! This is because clearly <code>x ↦ x</code> is continuous and the composition of two continuous functions is continuous. This category is called <code>Cpo</code>. It’s here that we’re going to do most of our interesting constructions.</p>
<p>Finally, we have to discuss one important construction on <code>Cpo</code>: <code>D → E</code>. This is the set of continuous functions from <code>D</code> to <code>E</code>. The ordering on this is pointwise, meaning that <code>f ⊑ g</code> if for all <code>x ∈ D</code>, <code>f(x) ⊑ g(x)</code>. This is a cppo where <code>⊥</code> is <code>x ↦ ⊥</code> and all the lubs are determined pointwise.</p>
<p>This gives us most of the mathematics we need to do the constructions we’re going to want, to demonstrate something cool here’s a fun theorem which turns out to be incredibly useful: Any continuous function <code>f : D → D</code> on a cppo <code>D</code> has a least fixed point.</p>
<p>To construct this least point we need to find an <code>x</code> so that <code>x = f(x)</code>. To do this, note first that <code>x ⊑ f(x)</code> by definition and by the monotonicity of <code>f</code>: <code>f(x) ⊑ f(y)</code> if <code>x ⊆ y</code>. This means that the collection of elements <code>fⁱ(⊥)</code> forms a chain with the ith element being the ith iteration of <code>f</code>! Since <code>D</code> is a cppo, this chain has an upper bound: <code>⊔ fⁱ(⊥)</code>. Moreover, <code>f(⊔ fⁱ(⊥)) = ⊔ f(fⁱ(⊥))</code> by the continuity of <code>f</code>, but <code>⊔ fⁱ(⊥) = ⊥ ⊔ (⊔ f(fⁱ(⊥))) = ⊔ f(fⁱ(⊥))</code> so this is a fixed point! The proof that it’s a least fixed point is elided because typesetting in markdown is a bit of a bother.</p>
<p>So there you have it, very, very basic domain theory. I can now answer the question we weren’t sure about before, the slogan is “computable functions are continuous functions”.</p>
<h2 id="solving-recursive-equations-in-cpo">Solving Recursive Equations in <code>Cpo</code></h2>
<p>So now we can get to the result showing domain theory incredibly useful. Remember our problem before? We wanted to find a collection <code>D</code> so that</p>
<pre><code>D ≅ D → D</code></pre>
<p>However it wasn’t clear how to do this due to size issues. In <code>Cpo</code> however, we can absolutely solve this. This huge result was due to Dana Scott. First, we make a small transformation to the problem that’s very common in these scenarios. Instead of trying to solve this equation (something we don’t have very many tools for) we’re going to instead look for the fixpoint of this functor</p>
<pre><code>F(X) = X → X</code></pre>
<p>The idea here is that we’re going to prove that all well behaved endofunctors on Cpo have fixpoints. By using this viewpoint we get all the powerful tools we normally have for reasoning about functors in category theory. However, there’s a problem: the above isn’t a functor! It has both positive and negative occurrences of <code>X</code> so it’s neither a co nor contravariant functor. To handle this we apply another clever trick. Let’s not look at endofunctors, but rather functors <code>Cpoᵒ × Cpo → Cpo</code> (I believe this should be attributed to Freyd). This is a binary functor which is covariant in the second argument and contravariant in the first. We’ll use the first argument everywhere there’s a negative occurrence of <code>X</code> and the second for every positive occurrence. Take note: we need things to be contravariant in the first argument because we’re using that first argument negatively: if we didn’t do that we wouldn’t have a functor.</p>
<p>Now we have</p>
<pre><code>F(X⁻, X⁺) = X⁻ → X⁺</code></pre>
<p>This is functorial. We can also always recover the original map simply by diagonalizing: <code>F(X) = F(X, X)</code>. We’ll now look for an object <code>D</code> so that <code>F(D, D) ≅ D</code>. Not quite a fixed point, but still equivalent to the equation we were looking at earlier.</p>
<p>Furthermore, we need one last critical property, we want <code>F</code> to be locally continuous. This means that the maps on morphisms determined by <code>F</code> should be continuous so <code>F(⊔ P, g) = ⊔ F(P, g)</code> and vice-versa (here <code>P</code> is a set of functions). Note that such morphisms have an ordering because they belong to the pointwise ordered cppo we talked about earlier.</p>
<p>We have one final thing to set up before this proof: what about if there’s multiple non-isomorphic solutions to <code>F</code>? We want a further coherence condition that’s going to provide us with 2 things</p>
<ol type="1">
<li>An ability to uniquely determine a solution</li>
<li>A powerful proof technique that isolates us from the particulars of the construction</li>
</ol>
<p>What we want is called minimal invariance. Suppose we have a <code>D</code> and an <code>i : D ≅ F(D, D)</code>. This is the minimal invariant solution if and only if the least fixed point of <code>f(e) = i⁻ ∘ F(e, e) ∘ i</code> is <code>id</code>. In other words, we want it to be the case that</p>
<pre><code>d = ⊔ₓ fˣ(⊥)(d) (d ∈ D)</code></pre>
<p>I mentally picture this as saying that the isomorphism is set up so that for any particular <code>d</code> we choose, if we apply <code>i</code>, <code>fmap</code> over it, apply <code>i</code> again, repeat and repeat, eventually this process will halt and we’ll run out of things to <code>fmap</code> over. It’s a sort of a statement that each <code>d ∈ D</code> is “finite” in a very, <em>very</em> handwavy sense. Don’t worry if that didn’t make much sense, it’s helpful to me but it’s just my intuition. This property has some interesting effects though: it means that if we find such a <code>D</code> then <code>(D, D)</code> is going to be both the initial algebra and final coalgebra of <code>F</code>.</p>
<p>Without further ado, let’s prove that every locally continuous functor <code>F</code>. We start by defining the following</p>
<pre><code>D₀ = {⊥}
Dᵢ  = F(Dᵢ₋₁, Dᵢ₋₁)</code></pre>
<p>This gives us a chain of cppos that gradually get larger. How do we show that they’re getting larger? By defining an section from <code>Dᵢ</code> to <code>Dⱼ</code> where <code>j = i + 1</code>. A section is a function <code>f</code> which is paired with a (unique) function <code>f⁰</code> so that <code>f⁰f = id</code> and <code>ff⁰ ⊑ id</code>. In other words, <code>f</code> embeds its domain into the codomain and <code>f⁰</code> tells us how to get it out. Putting something in and taking it out is a round trip. Since the codomain may be bigger though taking something out and putting it back only <em>approximates</em> a round trip. Our sections are defined thusly</p>
<pre><code>s₀ = x ↦ ⊥         r₀ = x ↦ ⊥
sᵢ  = F(rᵢ₋₁, sᵢ₋₁)   rᵢ = F(rᵢ₋₁, sᵢ₋₁)</code></pre>
<p>It would be very instructive to work out that these definitions are actually sections and retractions. Since type-setting this subscripts is a little rough, if it’s clear from context I’ll just write <code>r</code> and <code>s</code>. Now we’ve got this increasing chain, we define an interesting object</p>
<pre><code> D = {x ∈ Πᵢ Dᵢ | x.(i-1) = r(x.i)}</code></pre>
<p>In other words, <code>D</code> is the collection of infinitely large pairs. Each component if from one of those <code>Dᵢ</code>s above and they cohere with each other so using <code>s</code> and <code>r</code> to step up the chain takes you from one component to the next. Next we define a way to go from a single <code>Dᵢ</code> to a <code>D</code>: <code>upᵢ : Dᵢ → D</code> where</p>
<pre><code>upᵢ(x).j =  x    if i = j
         | rᵈ(x) if i - j = d &gt; 0
         | sᵈ(x) if j - i = d &gt; 0</code></pre>
<p>Interestingly, note that <code>πᵢ ∘ upᵢ = id</code> (easy proof) and that <code>upᵢ ∘ πᵢ ⊑ id</code> (slightly harder proof). This means that we’ve got more sections lying around: every <code>Dᵢ</code> can be fed into <code>D</code>. Consider the following diagram</p>
<pre><code>    s      s      s
D0 ——&gt; D1 ——&gt; D2 ——&gt; ...</code></pre>
<p>I claim that <code>D</code> is the colimit to this diagram where the collection of arrows mapping into it are given with <code>upᵢ</code>. Seeing this is a colimit follows from the fact that <code>πᵢ ∘ upᵢ</code> is just <code>id</code>. Specifically, suppose we have some object <code>C</code> and a family of morphisms <code>cᵢ : Dᵢ → C</code> which commute properly with <code>s</code>. We need to find a unique morphism <code>h</code> so that <code>cᵢ = h ∘ upᵢ</code>. Define <code>h</code> as <code>⊔ᵢ cᵢπᵢ</code>. Then</p>
<pre><code>h ∘ upⱼ = (⊔j&lt;i cᵢsʲrʲ) ⊔ cᵢ ⊔ (⊔j&gt;i cᵢrʲsʲ) = (⊔j&lt;i cᵢsʲrʲ) ⊔ cᵢ</code></pre>
<p>The last step follows from the fact that <code>rʲsʲ = id</code>. Furthermore, <code>sʲrʲ ⊑ id</code> so <code>cᵢsʲrʲ ⊑ cᵢ</code> so that whole massive term just evaluates to <code>cᵢ</code> as required. So we have a colimit. Notice that if we apply <code>F</code> to each <code>Dᵢ</code> in the diagram we end up with a new diagram.</p>
<pre><code>    s      s      s
D1 ——&gt; D2 ——&gt; D3 ——&gt; ...</code></pre>
<p><code>D</code> is still the colimit (all we’ve done is shift the diagram over by one) but by identical reasoning to <code>D</code> being a colimit, so is <code>F(D, D)</code>. This means we have a unique isomorphism <code>i : D ≅ F(D, D)</code>. The fact that <code>i</code> is the minimal invariant follows from the properties we get from the fact that <code>i</code> comes from a colimit.</p>
<p>With this construction we can construct our model of the lambda calculus simply by finding the minimal invariant of the locally continuous functor <code>F(D⁻, D⁺) = D⁻ → D⁺</code> (it’s worth proving it’s locally continuous). Our denotation is defined as <code>[e]ρ ∈ D</code> where <code>e</code> is a lambda term and <code>ρ</code> is a map of the free variables of <code>e</code> to other elements of <code>D</code>. This is inductively defined as</p>
<pre><code>[λx. e]ρ = i⁻(d ↦ [e]ρ[x ↦ d])
[e e&#39;]ρ = i([e]ρ)([e&#39;]ρ)
[x]ρ = ρ(x)</code></pre>
<p>Notice here that for the two main constructions we just use <code>i</code> and <code>i⁻</code> to fold and unfold the denotations to treat them as functions. We could go on to prove that this denotation is sound and complete but that’s something for another post.</p>
<h2 id="wrap-up">Wrap Up</h2>
<p>That’s the main result I wanted to demonstrate. With this single proof we can actually model a very large class of programming languages into <code>Cpo</code>. Hopefully I’ll get around to showing how we can pull a similar trick with a relational structure on <code>Cpo</code> in order to prove full abstraction. This is nicely explained in Andrew Pitt’s “Relational Properties of Domains”.</p>
<p>If you’re interested in domain theory I learned from Gunter’s “Semantics of Programming Languages” book and recommend it.</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Fri, 14 Aug 2015 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2015-08-14-solve-domains.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Learn Type Theory</title>
    <link>http://jozefg.bitbucket.org/posts/2015-08-14-learn-tt.html</link>
    <description><![CDATA[<div class="info">
    Posted on August 14, 2015
    
</div>
<div class="info">
    
    Tags: <a href="/tags/types.html">types</a>
    
</div>

<p><em>I’ve been trying to write a blog post to this effect for a while now, hopefully this one will stick. I intend for this to be a bit more open-ended than most of my other posts, if you’re interested in seeing the updated version look <a href="https://www.github.com/jozefg/learn-tt">here</a>. Pull requests/issues are more than welcome on the repository. I hope you learn something from this.</em></p>
<p>Lots of people seem curious about type theory but it’s not at all clear how to go from no math background to understanding “Homotopical Patch Theory” or whatever the latest cool paper is. In this repository I’ve gathered links to some of the resources I’ve personally found helpful.</p>
<h2 id="reading-advice">Reading Advice</h2>
<p>I strongly urge you to start by reading one or more of the textbooks immediately below. They give a nice self-contained introduction and a foundation for understanding the papers that follow. Don’t get hung up on any particular thing, it’s always easier to skim the first time and read closely on a second pass.</p>
<h2 id="the-resources">The Resources</h2>
<h3 id="textbooks">Textbooks</h3>
<ul>
<li><p>Practical Foundations of Programming Languages (PFPL)</p>
<p>I reference this more than any other book. It’s a very wide ranging survey of programming languages that assumes very little background knowledge. A lot people prefer the next book I mention but I think PFPL does a better job explaining the foundations it works from and then covers more topics I find interesting.</p>
<ul>
<li><a href="http://www.cs.cmu.edu/~rwh/plbook/1sted-revised.pdf">Online copy</a></li>
<li><a href="http://www.amazon.com/Practical-Foundations-Programming-Languages-Professor/dp/1107029570/ref=sr_1_1?ie=UTF8&amp;qid=1439346858&amp;sr=8-1&amp;keywords=practical+foundations+for+programming+languages">Dead-tree copy</a></li>
</ul></li>
<li><p>Types and Programming Languages (TAPL)</p>
<p>Another very widely used introductory book (the one I learned with). It’s good to read in conjunction with PFPL as they emphasize things differently. Notably, this includes descriptions of type inference which PFPL lacks and TAPL lacks most of PFPL’s descriptions of concurrency/interesting imperative languages. Like PFPL this is very accessible and well written.</p>
<ul>
<li><a href="http://www.cis.upenn.edu/~bcpierce/tapl/">Online supplements</a></li>
<li><a href="https://mitpress.mit.edu/books/types-and-programming-languages">Dead-tree copy</a></li>
</ul></li>
<li><p>Advanced Topics in Types and Programming Languages (ATTAPL)</p>
<p>Don’t feel the urge to read this all at once. It’s a bunch of fully independent but excellent chapters on a bunch of different topics. Read what looks interesting, save what doesn’t. It’s good to have in case you ever need to learn more about one of the subjects in a pinch.</p>
<ul>
<li><a href="http://www.cis.upenn.edu/~bcpierce/attapl/">Online supplements</a></li>
<li><a href="http://www.amazon.com/exec/obidos/ASIN/0262162288/benjamcpierce">Dead-tree copy</a></li>
</ul></li>
</ul>
<h3 id="proof-assistants">Proof Assistants</h3>
<p>One of the fun parts of taking in an interest in type theory is that you get all sorts of fun new programming languages to play with. Some major proof assistants are</p>
<ul>
<li><p>Coq</p>
<p>Coq is one of the more widely used proof assistants and has the best introductory material by far in my opinion.</p>
<ul>
<li><a href="https://coq.inria.fr/">Official site</a></li>
<li><a href="http://www.cis.upenn.edu/~bcpierce/sf/current/index.html">Software Foundations</a></li>
<li><a href="http://adam.chlipala.net/cpdt/">Certified Programming with Dependent Types</a></li>
<li><a href="https://hal.inria.fr/inria-00076024/document">The paper on the calculus of constructions</a></li>
<li><a href="http://www.cs.cmu.edu/~fp/papers/mfps89.pdf">The paper on the calculus of inductive constructions</a> (What Coq is based on)</li>
</ul></li>
<li><p>Agda</p>
<p>Agda is in many respects similar to Coq, but is a smaller language overall. It’s relatively easy to learn Agda after Coq so I recommend doing that. Agda has some really interesting advanced constructs like induction-recursion.</p>
<ul>
<li><a href="http://wiki.portal.chalmers.se/agda/pmwiki.php">Official site</a></li>
<li><a href="http://www.cse.chalmers.se/~ulfn/papers/afp08/tutorial.pdf">Tutorial</a></li>
<li><a href="http://wiki.portal.chalmers.se/agda/pmwiki.php?n=ReferenceManual.RecordsTutorial">Records tutorial</a></li>
<li><a href="https://github.com/pigworker/MetaprogAgda">Conor McBride’s</a> <a href="https://github.com/pigworker/CS410-14">fun Agda code</a></li>
</ul></li>
<li><p>Idris</p>
<p>It might not be fair to put Idris in a list of “proof assistants” since it really wants to be a proper programming language. It’s one of the first serious attempts at writing a programming language with dependent types <em>for actual programming</em> though.</p>
<ul>
<li><a href="http://idris-lang.org/">Official site</a></li>
<li><a href="http://docs.idris-lang.org/en/latest/tutorial/index.html#tutorial-index">Quick tutorial</a></li>
<li><a href="https://www.youtube.com/watch?v=O1t4xJzrOng">A list of talks on Idris</a></li>
<li><a href="https://www.youtube.com/watch?v=dP2imvL92sY">David Christiansen’s cool talk</a></li>
</ul></li>
<li><p>Twelf</p>
<p>Twelf is by far the simplest system in this list, it’s the absolute minimum a language can have and still be dependently typed. All of this makes it easy to pick up, but there are very few users and not a lot of introductory material which makes it a bit harder to get started with. It does scale up to serious use though.</p>
<ul>
<li><a href="http://twelf.org/">Official site</a></li>
<li><a href="http://twelf.org/wiki/Tutorials">Wiki Tutorials</a></li>
<li><a href="http://jozefg.bitbucket.org/posts/2015-02-28-twelf.html">My tutorial</a></li>
<li><a href="http://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.21.5854">The paper on LF, the underlying system of Twelf</a></li>
</ul></li>
</ul>
<h3 id="type-theory">Type Theory</h3>
<ul>
<li><p>The Works of Per Martin-Löf</p>
<p>Per Martin-Löf has contributed a <em>ton</em> to the current state of dependent type theory. So much so that it’s impossible to escape his influence. His papers on Martin-Löf Type Theory (he called it Intuitionistic Type Theory) are seminal.</p>
<p>If you’re confused by the papers above read the book in the next entry and try again. The book doesn’t give you as good a feel for the various flavors of MLTT (which spun off into different areas of research) but is easier to follow.</p>
<ul>
<li><a href="https://github.com/michaelt/martin-lof/blob/master/pdfs/An-Intuitionistic-Theory-of-Types-1972.pdf?raw=true">1972</a></li>
<li><a href="https://github.com/michaelt/martin-lof/blob/master/pdfs/Constructive-mathematics-and-computer-programming-1982.pdf?raw=true">1979</a></li>
<li><a href="https://github.com/michaelt/martin-lof/blob/master/pdfs/Bibliopolis-Book-retypeset-1984.pdf?raw=true">1984</a></li>
</ul></li>
<li><p>Programming In Martin-Löf’s Type Theory</p>
<p>It’s good to read the original papers and here things from the horses mouth, but Martin-Löf is much smarter than us and it’s nice to read other people explanations of his material. A group of people at Chalmer’s have elaborated it into a book.</p>
<ul>
<li><a href="http://www.cse.chalmers.se/research/group/logic/book/book.pdf">Online link</a></li>
</ul></li>
<li><p>The Works of John Reynold’s</p>
<p>John Reynold’s works are similarly impressive and always a pleasure to read.</p>
<ul>
<li><a href="http://www.cse.chalmers.se/edu/year/2010/course/DAT140_Types/Reynolds_typesabpara.pdf">Types, Abstraction and Parametric Polymorphism</a> (Parametricity for System F)</li>
<li><a href="http://www.cs.cmu.edu/~jcr/seplogic.pdf">A Logic For Shared Mutable State</a></li>
<li><a href="http://www.cs.cmu.edu/afs/cs.cmu.edu/project/fox-19/member/jcr/www15818As2011/cs818A3-11.html">Course notes on separation logic</a></li>
<li><a href="http://www.cs.cmu.edu/~jcr/cs819-00.html">Course notes on denotational semantics</a></li>
</ul></li>
<li><p>Computational Type Theory</p>
<p>While most dependent type theories (like the ones found in Coq, Agda, Idris..) are based on Martin-Löf later intensional type theories, computational type theory is different. It’s a direct descendant of his extensional type theory that has been heavily developed and forms the basis of NuPRL nowadays. The resources below describe the various parts of how CTT works.</p>
<ul>
<li><a href="https://github.com/jonsterling/type-theory-and-its-meaning-explanations">Type Theory and its Meaning Explanations</a></li>
<li><a href="http://www.cs.cornell.edu/Info/Projects/NuPrl/documents/Allen/lics87.html">A Non-Type-Theoretic Definition of Martin-Löf’s Types</a></li>
<li><a href="https://www.cs.uoregon.edu/research/summerschool/summer10/lectures/Harper-JSC92.pdf">Constructing a type system over operational semantics</a> (Similar to the above, they’re helpful to read together)</li>
<li><a href="http://www.nuprl.org/documents/Howe/EqualityinLazy.html">Equality in Lazy Computation System</a> (of general interest)</li>
<li><a href="http://www.nuprl.org/documents/Constable/naive.pdf">Naive Computational Type Theory</a></li>
<li><a href="http://www.nuprl.org/documents/Allen/05-jal-final.pdf">Innovations in CTT using NuPRL</a></li>
</ul></li>
<li><p>Homotopy Type Theory</p>
<p>A new exciting branch of type theory. This exploits the connection between homotopy theory and type theory by treating types as spaces. It’s the subject of a lot of active research but has some really nice introductory resources even now.</p>
<ul>
<li><a href="http://homotopytypetheory.org/book/">The HoTT book</a></li>
<li><a href="https://github.com/RobertHarper/hott-notes">Student’s Notes on HoTT</a></li>
</ul></li>
</ul>
<h3 id="proof-theory">Proof Theory</h3>
<ul>
<li><p>Frank Pfenning’s Lecture Notes</p>
<p>Over the years, Frank Pfenning has accumulated lecture notes that are nothing short of heroic. They’re wonderful to read and almost as good as being in one of his lectures.</p>
<ul>
<li><a href="http://www.cs.cmu.edu/~fp/courses/15317-f09/">Introductory Course</a></li>
<li><a href="http://www.cs.cmu.edu/~fp/courses/15816-s12/">Linear Logic</a></li>
<li><a href="http://www.cs.cmu.edu/~fp/courses/15816-s10/">Modal Logic</a></li>
</ul></li>
</ul>
<h3 id="category-theory">Category Theory</h3>
<p>Learning category theory is necessary to understand some parts of type theory. If you decide to study categorical semantics, realizability, or domain theory eventually you’ll have to buckledown and learn a little at least. It’s actually really cool math so no harm done!</p>
<ul>
<li><p>Category Theory for Computer Scientists</p>
<p>This is the absolute smallest introduction to category theory you can find that’s still useful for a computer scientist. It’s very light on what it demands for prior knowledge of pure math but doesn’t go into too much depth.</p>
<ul>
<li><a href="http://repository.cmu.edu/cgi/viewcontent.cgi?article=2846&amp;context=compsci">Early version available online</a></li>
<li><a href="https://mitpress.mit.edu/index.php?q=books/basic-category-theory-computer-scientists">Dead-tree version</a></li>
</ul></li>
<li><p>Category Theory</p>
<p>One of the better introductory books to category theory in my opinion. It’s notable in assuming relatively little mathematical background and for covering quite a lot of ground in a readable way.</p>
<ul>
<li><a href="http://www.amazon.com/Category-Theory-Oxford-Logic-Guides/dp/0199237182/ref=sr_1_1?ie=UTF8&amp;qid=1439348930&amp;sr=8-1&amp;keywords=awodey+category+theory">Dead-tree version</a></li>
</ul></li>
<li><p>Ed Morehouse’s Category Theory Lecture Notes</p>
<p>Another valuable piece of reading are these lecture notes. They cover a lot of the same areas as “Category Theory” so they can help to reinforce what you learned there as well giving you some of the author’s perspective on how to think about these things.</p>
<ul>
<li><a href="http://www.cs.cmu.edu/~edmo/research/notes/intro_categorical_semantics.pdf">Online copy</a></li>
</ul></li>
</ul>
<h3 id="other-goodness">Other Goodness</h3>
<ul>
<li><p>Gunter’s “Semantics of Programming Language”</p>
<p>While I’m not as big a fan of some of the earlier chapters, the math presented in this book is absolutely top-notch and gives a good understanding of how some cool fields (like domain theory) work.</p>
<ul>
<li><a href="http://www.amazon.com/Semantics-Programming-Languages-Structures-Foundations/dp/0262071436/ref=sr_1_1?ie=UTF8&amp;qid=1439349219&amp;sr=8-1&amp;keywords=gunter+semantics+of+programming+languages">Dead-tree version</a></li>
</ul></li>
<li><p>OPLSS</p>
<p>The Oregon Programming Languages Summer School is a 2 week long bootcamp on PLs held annually at the university of Oregon. It’s a wonderful event to attend but if you can’t make it they record all their lectures anyways! They’re taught be a variety of lecturers but they’re all world class researchers.</p>
<ul>
<li><a href="https://www.cs.uoregon.edu/research/summerschool/summer12/curriculum.html">2012</a></li>
<li><a href="https://www.cs.uoregon.edu/research/summerschool/summer13/curriculum.html">2013</a></li>
<li><a href="https://www.cs.uoregon.edu/research/summerschool/summer14/curriculum.html">2014</a></li>
<li><a href="https://www.cs.uoregon.edu/research/summerschool/summer15/curriculum.html">2015</a></li>
</ul></li>
</ul>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Fri, 14 Aug 2015 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2015-08-14-learn-tt.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Coinduction in JonPRL for Low Low Prices</title>
    <link>http://jozefg.bitbucket.org/posts/2015-07-17-conats-for-cheap.html</link>
    <description><![CDATA[<div class="info">
    Posted on July 17, 2015
    
</div>
<div class="info">
    
    Tags: <a href="/tags/jonprl.html">jonprl</a>
    
</div>

<p>So as a follow up to my prior <a href="/posts/2015-07-06-jonprl.html">tutorial on JonPRL</a> I wanted to demonstrate a nice example of <a href="http://www.jonprl.org">JonPRL</a> being used to prove something</p>
<ol type="1">
<li>Interesting</li>
<li><p>Unreasonably difficult in Agda or the like</p>
<p><em>I think I’m asking to be shown up when I say stuff like this…</em></p></li>
</ol>
<p>I would like to implement the conatural numbers in JonPRL but without a notion of general coinductive <em>or</em> even inductive types. Just the natural numbers. The fun bit is that we’re basically going to lift the definition of a coinductively defined set straight out of set theory into JonPRL!</p>
<h2 id="math-stuff">Math Stuff</h2>
<p>First, let’s go through some math. How can we formalize the notion of an coinductively defined type as we’re used to in programming languages? Recall that something is coinductively if it contains all terms so that we can eliminate the term according to the elimination form for our type. For example, Martin-Lof has proposed we view functions (Π-types) as coinductively defined. That is,</p>
<pre><code>x : A ⊢ f(x) : B(x)
————————————————————
 f : Π x : A. B(x)</code></pre>
<p>In particular, there’s no assertion that <code>f</code> needs to be a lambda, just that <code>f(x)</code> is defined and belongs to the right type. This view of “if we can use it, it’s in the type” applies to more than just functions. Let’s suppose we have a type with the following elimination form</p>
<pre><code>L : List  M : A  x : Nat, y : List : A
——————————————————————————————————————
      case(L; M; x.y.N) : A</code></pre>
<p>This is more familiar to Haskellers as</p>
<pre><code>case L of
  [] -&gt; M
  x :: y -&gt; N</code></pre>
<p>Now if we look at the coinductively defined type built from this elimination rule we have not finite lists, but streams! There’s nothing in this elimination rule that specifies that the list be finite in length for it to terminate. All we need to be able to do is evaluate the term to either a <code>::</code> of a <code>Nat</code> and a <code>List</code> or <code>nil</code>. This means that</p>
<pre><code>fix x. cons(0; x) : List</code></pre>
<p>Let’s now try to formalize this by describing what it means to build a coinductively type up as a set of terms. In particular the types we’re interested in here are algebraic ones, constructed from sums and products.</p>
<p>Now unfortunately I’m going to be a little handwavy. I’m going to act is if we’ve worked out a careful set theoretic semantics for this programming language (like the one that exists for MLTT). This means that All the equations you see here are across sets and that these sets contain programs so that <code>⊢ e : τ</code> means that <code>e ∈ τ</code> where <code>τ</code> on the right is a set.</p>
<p>Well we start with some equation of the form</p>
<pre><code>Φ = 1 + Φ</code></pre>
<p>This particular equation a is actually how we would go about defining the natural numbers. If I write it in a more Haskellish notation we’d have</p>
<pre><code>data Φ = Zero | Succ Φ</code></pre>
<p>Next, we transform this into a function. This step is a deliberate move so we can start applying the myriad tools we know of for handling this equation.</p>
<pre><code>Φ(X) = 1 + X</code></pre>
<p>We now want to find some <code>X</code> so that <code>Φ(X) = X</code>. If we can do this, then I claim that <code>X</code> is a solution to the equation given above since</p>
<pre><code>X = Φ(X)
X = 1 + X</code></pre>
<p>precisely mirrors the equation we had above. Such an <code>X</code> is called a “fixed point” of the function <code>Φ</code>. However, there’s a catch: there may well be more than one fixed point of a function! Which one do we choose? The key is that we want the coinductively defined version. Coinduction means that we should always be able to examine a term in our type and its outermost form should be <code>1 + ???</code>. Okay, let’s optimistically start by saying that <code>X</code> is <code>⊤</code> (the collection of all terms).</p>
<p>Ah okay, this isn’t right. This works only so long as we don’t make any observations about a term we claim is in this type. The minute we pattern match, we might have found we claimed a function was in our type! I have not yet managed to pay my rent by saying “OK, here’s the check… but don’t try to <em>use</em> it and it’s rent”. So perhaps we should try something else. Okay, so let’s not say <code>⊤</code>, let’s say</p>
<pre><code>X = ⊤ ⋂ Φ(⊤)</code></pre>
<p>Now, if <code>t ∈ X</code>, we know that <code>t ∈ 1 + ???</code>. This means that if we run <code>e ∈ X</code>, we’ll get the correct outermost form. However, this code is still potentially broken:</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb10-1" data-line-number="1">    <span class="kw">case</span> e <span class="kw">of</span></a>
<a class="sourceLine" id="cb10-2" data-line-number="2">      <span class="dt">Inl</span> _ <span class="ot">-&gt;</span> <span class="fu">...</span></a>
<a class="sourceLine" id="cb10-3" data-line-number="3">      <span class="dt">Inr</span> x <span class="ot">-&gt;</span> <span class="kw">case</span> e <span class="kw">of</span></a>
<a class="sourceLine" id="cb10-4" data-line-number="4">                 <span class="dt">Inl</span> _ <span class="ot">-&gt;</span> <span class="fu">...</span></a>
<a class="sourceLine" id="cb10-5" data-line-number="5">                 <span class="dt">Inr</span> _ <span class="ot">-&gt;</span> <span class="fu">...</span></a></code></pre></div>
<p>This starts off as being well typed, but as we evaluate, it may actually become ill typed. If we claimed that this was a fixed point to our language, our language would be type-unsafe. This is an unappealing quality in a type theory.</p>
<p>Okay, so that didn’t work. What if we fixed this code by doing</p>
<pre><code>X = ⊤ ⋂ Φ(⊤) ⋂ Φ(Φ(⊤))</code></pre>
<p>Now this fixes the above code, but can you imagine a snippet of code where this still gets stuck? So each time we intersect <code>X</code> with <code>Φ(X)</code> we get a new type which behaves like the real fixed point so long as we only observe <code>n + 1</code> times where <code>X</code> behaves like the fixed point for <code>n</code> observations. Well, we can only make finitely many observations so let’s just iterate such an intersection</p>
<pre><code>X = ⋂ₙ Φⁿ(⊤)</code></pre>
<p>So if <code>e ∈ X</code>, then no matter how many times we pattern match and examine the recursive component of <code>e</code> we know that it’s still in <code>⋂ₙ Φⁿ(⊤)</code> and therefore still in <code>X</code>! In fact, it’s easy to prove that this is the case with two lemmas</p>
<ol type="1">
<li>If <code>X ⊆ Y</code> then <code>Φ(X) ⊆ Φ(Y)</code></li>
<li>If I have a collection <code>S</code> of sets, then <code>⋂ Φ(S) = Φ(⋂ S)</code> where we define <code>Φ</code> on a collection of sets by applying <code>Φ</code> to each component.</li>
</ol>
<p>These two properties state the monotonicity and cocontinuity of <code>Φ</code>. In fact, cocontinuity should imply monotonicity (can you see how?). We then may show that</p>
<pre><code>Φ(⋂ₙ Φⁿ(⊤)) = ⋂ₙ Φ(Φⁿ(⊤))
             = ⊤ ⋂ (⋂ₙ Φ(Φⁿ(⊤)))
             = ⋂ₙ Φⁿ(⊤)</code></pre>
<p>As desired.</p>
<h2 id="the-code">The Code</h2>
<p>Now that we have some idea of how to formalize coinduction, can we port this to JonPRL? Well, we have natural numbers and we can take the intersection of types… Seems like a start. Looking at that example, we first need to figure out what <code>⊤</code> corresponds to. It should include all programs, which sounds like the type <code>base</code> in JonPRL. However, it also should be the case that <code>x = y ∈ ⊤</code> for all <code>x</code> and <code>y</code>. For that we need an interesting trick:</p>
<pre class="jonprl"><code>    Operator top : ().
    [top] =def= [isect(void; _.void)].</code></pre>
<p>In prettier notation,</p>
<pre><code>top ≙ ⋂ x : void. void</code></pre>
<p>Now <code>x ∈ top</code> if <code>x ∈ void</code> for all <code>_ ∈ void</code>. Hey wait a minute… No such <code>_</code> exists so the if is always satisfied vacuously. Ok, that’s good. Now <code>x = y ∈ top</code> if for all <code>_ ∈ void</code>, <code>x = y ∈ void</code>. Since no such <code>_</code> exists again, all things are in fact equal in <code>void</code>. We can even prove this within JonPRL</p>
<pre class="jonprl"><code>    Theorem top-is-top :
      [isect(base; x.
       isect(base; y.
       =(x; y; top)))] {
      unfold &lt;top&gt;; auto
    }.</code></pre>
<p>This proof is really just:</p>
<ol type="1">
<li>Unfold all the definitions.</li>
<li>Hey! There’s a <code>x : void</code> in my context! Tell me more about that.</li>
</ol>
<p>Now the fact that <code>x ∈ top</code> is a trivial corollary since our theorem tells us that <code>x = x ∈ top</code> and the former is just sugar for the latter. With this defined, we can now write down a general operator for coinduction!</p>
<pre class="jonprl"><code>    Operator corec : (1).
    [corec(F)] =def= [isect(nat; n. natrec(n; top; _.x. so_apply(F;x)))].</code></pre>
<p>To unpack this, <code>corec</code> takes one argument which binds one variable. We then intersect the type <code>natrec(n; top; _.x.so_apply(F;x))</code> for all <code>n ∈ nat</code>. That <code>natrec</code> construct is really saying <code>Fⁿ(⊤)</code>, it’s just a little obscured. Especially since we have to use <code>so_apply</code>, a sort of “meta-application” which lets us apply a term binding a variable to another term. This should look familiar, it’s just how we defined fixed point of a <code>Φ</code>!</p>
<p>For a fun demo, let’s define an <code>F</code> so that <code>cofix(F)</code> will give us the conatural numbers. I know that the natural numbers come from the least fixed point of <code>X ↦ 1 + X</code> (because I said so above, so it must be so) so let’s define that.</p>
<pre class="jonprl"><code>    Operator conatF : (0).
    [conatF(X)] =def= [+(unit; X)].</code></pre>
<p>This is just that <code>X ↦ 1 + X</code> I wrote above in JonPRL land instead of math notation. Next we need to actually define conatural numbers using <code>corec</code>.</p>
<pre class="jonprl"><code>    Operator conat : ().
    [conat] =def= [corec(R. conatF(R))].</code></pre>
<p>Now I’ve defined this, but that’s no fun unless we can actual build some terms so that <code>member(X; conat)</code>. Specifically I want to prove two things to start</p>
<ol type="1">
<li><code>member(czero; conat)</code></li>
<li><code>fun(member(M; conat); _.member(csucc(M); conat))</code></li>
</ol>
<p>This states that <code>conat</code> is closed under some zero and successor operations. Now what should those operations be? Remember what I said before, that we had this correspondence?</p>
<pre><code>X    ↦   1   +   X
Nat     Zero   Suc X</code></pre>
<p>Now remember that <code>conat</code> is <code>isect(nat; n....)</code> and when constructing a member of <code>isect</code> we’re not allowed to mention that <code>n</code> in it (as opposed to <code>fun</code> where we do exactly that). So that means <code>czero</code> has to be a member of <code>top</code> and <code>sum(unit; ...)</code>. The <code>top</code> bit is easy, everything is in <code>top</code>! That diagram above suggests <code>inl</code> of <em>something</em> in unit</p>
<pre class="jonprl"><code>    Operator czero : ().
    [czero] =def= [inl(&lt;&gt;)].</code></pre>
<p>So now we want to prove that this in fact in <code>conat</code>.</p>
<pre class="jonprl"><code>    Theorem zero-wf : [member(czero; conat)] {

    }.</code></pre>
<p>Okay loading this into JonPRL gives</p>
<pre><code>⊢ czero ∈ conat</code></pre>
<p>From there we start by unfolding all the definitions</p>
<pre class="jonprl"><code>    {
       unfold &lt;czero conat conatF corec top&gt;
    }</code></pre>
<p>This gives us back the desugared goal</p>
<pre><code>⊢ inl(&lt;&gt;) ∈ ⋂n ∈ nat. natrec(n; top; _.x.+(unit; x))</code></pre>
<p>Next let’s apply all the obvious introductions so that we’re in a position to try to prove things</p>
<pre class="jonprl"><code>    unfold &lt;czero conat conatF corec top&gt;; auto</code></pre>
<p>This gives us back</p>
<pre><code>1. [n] : nat
⊢ inl(&lt;&gt;) = inl(&lt;&gt;) ∈ natrec(n; top; _.x.+(unit; x))</code></pre>
<p>Now we’re stuck. We want to show <code>inl</code> is in something, but we’re never going to be able to do that until we can reduce that <code>natrec(n; top; _.x.+(unit; x))</code> to a canonical form. Since it’s stuck on <code>n</code>, let’s induct on that <code>n</code>. After that, let’s immediately reduce.</p>
<pre class="jonprl"><code>    {
      unfold &lt;czero conat conatF corec top&gt;; auto; elim #1; reduce
    }</code></pre>
<p>Now we have to cases, the base and inductive case.</p>
<pre><code>1. [n] : nat
⊢ inl(&lt;&gt;) = inl(&lt;&gt;) ∈ top


1. [n] : nat
2. n&#39; : nat
3. ih : inl(&lt;&gt;) = inl(&lt;&gt;) ∈ natrec(n&#39;; top; _.x.+(unit; x))
⊢ inl(&lt;&gt;) = inl(&lt;&gt;) ∈ +(unit; natrec(n&#39;; top; _.x.+(unit; x)))</code></pre>
<p>Now that we have canonical terms on the right of the <code>∈</code>m, let’s let <code>auto</code> handle the rest.</p>
<pre class="jonprl"><code>    Theorem zero-wf : [member(czero; conat)] {
      unfold &lt;czero conat conatF corec top&gt;; auto; elim #1; reduce; auto
    }.</code></pre>
<p>So now we have proven that <code>czero</code> is in the correct type. Let’s figure out <code>csucc</code>? Going by our noses, <code>inl</code> corresponded to <code>czero</code> and our diagram says that <code>inr</code> should correspond to <code>csucc</code>. This gives us</p>
<pre class="jonprl"><code>    Operator csucc : (0).
    [csucc(M)] =def= [inr(M)].</code></pre>
<p>Now let’s try to prove the corresponding theorem for <code>csucc</code></p>
<pre class="jonprl"><code>    Theorem succ-wf : [isect(conat; x. member(csucc(x); conat))] {
    }.</code></pre>
<p>Now we’re going to start off this proof like we did with our last one. Unfold everything, apply the introduction rules, and induct on <code>n</code>.</p>
<pre class="jonprl"><code>    {
      unfold &lt;csucc conat conatF corec top&gt;; auto; elim #2; reduce
    }</code></pre>
<p>Like before, we now have two subgoals:</p>
<pre><code>1. [x] : ⋂n ∈ nat. natrec(n; ⋂_ ∈ void. void; _.x.+(unit; x))
2. [n] : nat
⊢ inr(x) = inr(x) ∈ ⋂_ ∈ void. void


1. [x] : ⋂n ∈ nat. natrec(n; ⋂_ ∈ void. void; _.x.+(unit; x))
2. [n] : nat
3. n&#39; : nat
4. ih : inr(x) = inr(x) ∈ natrec(n&#39;; ⋂_ ∈ void. void; _.x.+(unit; x))
⊢ inr(x) = inr(x) ∈ +(unit; natrec(n&#39;; ⋂_ ∈ void. void; _.x.+(unit; x)))</code></pre>
<p>The first one looks pretty easy, that’s just <code>foo ∈ top</code>, I think <code>auto</code> should handle that.</p>
<pre class="jonprl"><code>    {
      unfold &lt;csucc conat conatF corec top&gt;; auto; elim #2; reduce;
      auto
    }</code></pre>
<p>This just leaves one goal to prove</p>
<pre><code>1. [x] : ⋂n ∈ nat. natrec(n; ⋂_ ∈ void. void; _.x.+(unit; x))
2. [n] : nat
3. n&#39; : nat
4. ih : inr(x) = inr(x) ∈ natrec(n&#39;; ⋂_ ∈ void. void; _.x.+(unit; x))
⊢ x = x ∈ natrec(n&#39;; ⋂_ ∈ void. void; _.x.+(unit; x))</code></pre>
<p>Now, as it turns out, this is nice and easy: look at what our first assumption says! Since <code>x ∈ isect(nat; n.Foo)</code> and our goal is to show that <code>x ∈ Foo(n')</code> this should be as easy as another call to <code>elim</code>.</p>
<pre class="jonprl"><code>    {
      unfold &lt;csucc conat conatF corec top&gt;; auto; elim #2; reduce;
      auto; elim #1 [n&#39;]; auto
    }</code></pre>
<p>Note that the <code>[n']</code> bit there lets us supply the term we wish to substitute for <code>n</code> while eliminating. This leaves us here:</p>
<pre><code>1. [x] : ⋂n ∈ nat. natrec(n; ⋂_ ∈ void. void; _.x.+(unit; x))
2. [n] : nat
3. n&#39; : nat
4. ih : inr(x) = inr(x) ∈ natrec(n&#39;; ⋂_ ∈ void. void; _.x.+(unit; x))
5. y : natrec(n&#39;; ⋂_ ∈ void. void; _.x.+(unit; x))
6. z : y = x ∈ natrec(n&#39;; ⋂_ ∈ void. void; _.x.+(unit; x))
⊢ x = x ∈ natrec(n&#39;; ⋂_ ∈ void. void; _.x.+(unit; x))</code></pre>
<p>Now a small hiccup: we know that <code>y = x</code> is in the right type. so <code>x = x</code> in the right type. But how do we prove this? The answer is to substitute all occurrences of <code>x</code> for <code>y</code>. This is written</p>
<pre class="jonprl"><code>    {
      unfold &lt;csucc conat conatF corec top&gt;; auto; elim #2; reduce;
      auto; elim #1 [n&#39;]; auto;
      hyp-subst ← #6 [h.=(h; h; natrec(n&#39;; isect(void; _.void); _.x.+(unit;x)))];
    }</code></pre>
<p>There are three arguments here, a direction to substitute, an index telling us which hypothesis to use as the equality to substitute with and finally, a term <code>[h. ...]</code>. The idea with this term is that each occurrence of <code>h</code> tells us where we want to substitute. In our case we used <code>h</code> in two places: both where we use <code>x</code>, and the direction says to replace the right hand side of the equality with the left side of the equality.</p>
<p>Actually running this gives</p>
<pre><code>1. [x] : ⋂n ∈ nat. natrec(n; ⋂_ ∈ void. void; _.x.+(unit; x))
2. [n] : nat
3. n&#39; : nat
4. ih : inr(x) = inr(x) ∈ natrec(n&#39;; ⋂_ ∈ void. void; _.x.+(unit; x))
5. y : natrec(n&#39;; ⋂_ ∈ void. void; _.x.+(unit; x))
6. z : y = x ∈ natrec(n&#39;; ⋂_ ∈ void. void; _.x.+(unit; x))
⊢ y = y ∈ natrec(n&#39;; ⋂_ ∈ void. void; _.x.+(unit; x))


1. [x] : ⋂n ∈ nat. natrec(n; ⋂_ ∈ void. void; _.x.+(unit; x))
2. [n] : nat
3. n&#39; : nat
4. ih : inr(x) = inr(x) ∈ natrec(n&#39;; ⋂_ ∈ void. void; _.x.+(unit; x))
5. y : natrec(n&#39;; ⋂_ ∈ void. void; _.x.+(unit; x))
6. z : y = x ∈ natrec(n&#39;; ⋂_ ∈ void. void; _.x.+(unit; x))
7. h : natrec(n&#39;; ⋂_ ∈ void. void; _.x.+(unit; x))
⊢ h = h ∈ natrec(n&#39;; ⋂_ ∈ void. void; _.x.+(unit; x)) ∈ U{i}</code></pre>
<p>The first goal is the result of our substitution and it’s trivial; <code>auto</code> will handle this now. The second goal is a little strange. It basically says that the result of our substitution is still a well-formed type. JonPRL’s thought process is something like this</p>
<blockquote>
<p>You said you were substituting for things of this type here. However, I know that just because <code>x : A</code> doesn’t mean we’re using it in all those spots as if it has type <code>A</code>. What if you substitute things equal in top (always equal) for when they’re being used as functions! This would let us prove that <code>zero ∈ Π(...)</code> or something silly. Convince me that when we fill in those holes with <em>something</em> of the type you mentioned, the goal is still a type (in a universe).</p>
</blockquote>
<p>However, these well-formedness goals usually go away with auto. This completes our theorem in fact.</p>
<pre class="jonprl"><code>    Theorem succ-wf : [isect(conat; x. member(csucc(x); conat))] {
      unfold &lt;csucc conat conatF corec top&gt;; auto; elim #2; reduce;
      auto; elim #1 [n&#39;]; auto;
      hyp-subst ← #6 [h.=(h; h; natrec(n&#39;; isect(void; _.void); _.x.+(unit;x)))];
      auto
    }.</code></pre>
<p>Okay so we now have something kind of number-ish, with zero and successor. But in order to demonstrate that this is the conatural numbers there’s one big piece missing.</p>
<h2 id="the-clincher">The Clincher</h2>
<p>The thing that distinguishes the <em>co</em>natural numbers from the inductive variety is the fact that we include infinite terms. In particular, I want to show that Ω (infinitely many <code>csucc</code>s) belongs in our type.</p>
<p>In order to say Ω in JonPRL we need recursion. Specifically, we want to write</p>
<pre class="jonprl"><code>    [omega] =def= [csucc(omega)].</code></pre>
<p>But this doesn’t work! Instead, we’ll define the Y combinator and say</p>
<pre class="jonprl"><code>    Operator omega : ().
    [omega] =def= [Y(x.csucc(x))].</code></pre>
<p>So what should this <code>Y</code> be? Well the standard definition of Y is</p>
<pre><code>Y(F) = (λ x. F (x x)) (λ x. F (x x))</code></pre>
<p>Excitingly, we can just say that in JonPRL; remember that we have a full untyped computation system after all!</p>
<pre class="jonprl"><code>    Operator Y : (1).
    [Y(f)] =def= [ap(lam(x.so_apply(f;ap(x;x)));lam(x.so_apply(f;ap(x;x))))].</code></pre>
<p>This is more or less a direct translation, we occasionally use <code>so_apply</code> for the reasons I explained above. As a fun thing, try to prove that this is a fixed point, eg that</p>
<pre class="jonprl"><code>    isect(U{i}; A. isect(fun(A; _.A); f . ceq(Y(f); ap(f; Y(f)))))</code></pre>
<p>The complete proof is in the JonPRL repo under <code>example/computational-equality.jonprl</code>. Anyways, we now want to prove</p>
<pre class="jonprl"><code>    Theorem omega-wf : [member(omega; conat)] {

    }.</code></pre>
<p>Let’s start with the same prelude</p>
<pre class="jonprl"><code>    {
      *{unfold &lt;csucc conat conatF corec top omega Y&gt;}; auto; elim #1;
    }</code></pre>
<p>Two goals just like before</p>
<pre><code>1. [n] : nat
⊢ (λx. inr(x[x]))[λx. inr(x[x])] = (λx. inr(x[x]))[λx. inr(x[x])] ∈ natrec(zero; ⋂_ ∈ void. void; _.x.+(unit; x))


1. [n] : nat
2. n&#39; : nat
3. ih : (λx. inr(x[x]))[λx. inr(x[x])] = (λx. inr(x[x]))[λx. inr(x[x])] ∈ natrec(n&#39;; ⋂_ ∈ void. void; _.x.+(unit; x))
⊢ (λx. inr(x[x]))[λx. inr(x[x])] = (λx. inr(x[x]))[λx. inr(x[x])] ∈ natrec(succ(n&#39;); ⋂_ ∈ void. void; _.x.+(unit; x))</code></pre>
<p>The goals start to get fun now. I’ve also carefully avoided using <code>reduce</code> ike we did before. The reason is simple, if we reduce in the second goal, our <code>ih</code> will reduce as well and we’ll end up completely stuck in a few steps (try it and see). So instead we’re going to finesse it a bit.</p>
<p>First let’s take care of that first goal. We can tell JonPRL to apply some tactics to just the first goal with the <code>focus</code> tactic</p>
<pre class="jonprl"><code>    {
      *{unfold &lt;csucc conat conatF corec top omega Y&gt;}; auto; elim #1;
      focus 0 #{reduce 1; auto};
    }</code></pre>
<p>Here <code>reduce 1</code> says “reduce by only one step” since really omega will diverge if we just let it run. This takes care of the first goal leaving just</p>
<pre><code>1. [n] : nat
2. n&#39; : nat
3. ih : (λx. inr(x[x]))[λx. inr(x[x])] = (λx. inr(x[x]))[λx. inr(x[x])] ∈ natrec(n&#39;; ⋂_ ∈ void. void; _.x.+(unit; x))
⊢ (λx. inr(x[x]))[λx. inr(x[x])] = (λx. inr(x[x]))[λx. inr(x[x])] ∈ natrec(succ(n&#39;); ⋂_ ∈ void. void; _.x.+(unit; x))</code></pre>
<p>Here’s the proof sketch for what’s left</p>
<ol type="1">
<li>Reduce the goal by one step but <em>carefully avoid touching the ih</em></li>
<li>Step everything by one</li>
<li>The result follows by intro and assumption</li>
</ol>
<p>You can stop here or you can see how we actually do this. It’s somewhat tricky. The basic complication is that there’s no built-in tactic for 1. Instead we use a new type called <code>ceq</code> which is “computational equality”. It ranges between two terms, no types involved here. It’s designed to work thusly if <code>ceq(a; b)</code>, either</p>
<ol type="1">
<li><code>a</code> and <code>b</code> run to weak-head normal form (canonical verifications) with the same outermost form, and all the inner operands are <code>ceq</code></li>
<li><code>a</code> and <code>b</code> both diverge</li>
</ol>
<p>So if <code>ceq(a; b)</code> then <code>a</code> and <code>b</code> “run the same”. What’s a really cool upshot of this is that if <code>ceq(a; b)</code> then if <code>a = a ∈ A</code> and <code>b = b ∈ A</code> then <code>a = b ∈ A</code>! <code>ceq</code> is the strictest equality in our system and we can rewrite with it absolutely everywhere without regards to types. Proving this requires showing the above definition forms a congruence (two things are related if their subcomponents are related).</p>
<p>This was a <em>big deal</em> because until Doug Howe came up with this proof NuPRL/CTT was awash with rules trying to specify this idea chunk by chunk and showing those rules were valid wasn’t trivial. Actually, you should <a href="http://www.nuprl.org/documents/Howe/EqualityinLazy.html">read that paper</a>: it’s 6 pages and the proof concept comes up a lot.</p>
<p>So, in order to do 1. we’re going to say “the goal and the goal if we step it twice are computationally equal” and then use this fact to substitute for the stepped version. The tactic to use here is called <code>csubst</code>. It takes two arguments</p>
<ol type="1">
<li>The <code>ceq</code> we’re asserting</li>
<li>Another <code>h.</code> term to guide the rewrite</li>
</ol>
<pre class="jonprl"><code>    {
      *{unfold &lt;csucc conat conatF corec top omega Y&gt;}; auto; elim #1;
      focus 0 #{reduce 1; auto};
      csubst [ceq(ap(lam(x.inr(ap(x;x))); lam(x.inr(ap(x;x))));
                  inr(ap(lam(x.inr(ap(x;x))); lam(x.inr(ap(x;x))))))]
         [h.=(h;h; natrec(succ(n&#39;); isect(void; _. void); _.x.+(unit; x)))];
    }</code></pre>
<p>This leaves us with two goals</p>
<pre><code>1. [n] : nat
2. n&#39; : nat
3. ih : (λx. inr(x[x]))[λx. inr(x[x])] = (λx. inr(x[x]))[λx. inr(x[x])] ∈ natrec(n&#39;; ⋂_ ∈ void. void; _.x.+(unit; x))
⊢ ceq((λx. inr(x[x]))[λx. inr(x[x])]; inr((λx. inr(x[x]))[λx. inr(x[x])]))


1. [n] : nat
2. n&#39; : nat
3. ih : (λx. inr(x[x]))[λx. inr(x[x])] = (λx. inr(x[x]))[λx. inr(x[x])] ∈ natrec(n&#39;; ⋂_ ∈ void. void; _.x.+(unit; x))
⊢ inr((λx. inr(x[x]))[λx. inr(x[x])]) = inr((λx. inr(x[x]))[λx. inr(x[x])]) ∈ natrec(succ(n&#39;); ⋂_ ∈ void. void; _.x.+(unit; x))</code></pre>
<p>Now we have two goals. The first is that <code>ceq</code> proof obligation. The second is our goal post-substitution. The first one can easily be dispatched by <code>step</code>. <code>step</code> let’s us prove <code>ceq</code> by saying</p>
<ol type="1">
<li><code>ceq(a; b)</code> holds if</li>
<li><code>a</code> steps to <code>a'</code> in one step</li>
<li><code>ceq(a'; b)</code></li>
</ol>
<p>This will leave us with <code>ceq(X; X)</code> which <code>auto</code> can handle. The second term is.. massive. But also simple. We just need to step it once and we suddenly have <code>inr(X) = inr(X) ∈ sum(_; A)</code> where <code>X = X ∈ A</code> is our assumption! So that can also be handled by <code>auto</code> as well. That means we need to run <code>step</code> on the first goal, <code>reduce 1</code> on the second, and <code>auto</code> on both.</p>
<pre class="jonprl"><code>    Theorem omega-wf : [member(omega; conat)] {
      unfolds; unfold &lt;omega Y&gt;; auto; elim #1;
      focus 0 #{reduce 1; auto};
      csubst [ceq(ap(lam(x.inr(ap(x;x))); lam(x.inr(ap(x;x))));
                  inr(ap(lam(x.inr(ap(x;x))); lam(x.inr(ap(x;x))))))]
             [h.=(h;h; natrec(succ(n&#39;); isect(void; _. void); _.x.+(unit; x)))];
      [step, reduce 1]; auto
    }.</code></pre>
<p>And we’ve just proved that <code>omega ∈ conat</code>, a term that is certainly the canonical (heh) example of coinduction in my mind.</p>
<h2 id="wrap-up">Wrap Up</h2>
<p>Whew, I actually meant for this to be a short blog post but that didn’t work out so well. Hopefully this illustrated a cool trick in computer science (intersect your way to coinduction) and in JonPRL.</p>
<p>Funnily enough before this was written no one had actually realized you could do coinduction in JonPRL. I’m still somewhat taken with the fact that a <em>very</em> minimal proof assistant like JonPRL is powerful enough to let you do this by giving you such general purpose tools as family intersection and a full computation system to work with. Okay that’s enough marketing from me.</p>
<p>Cheers.</p>
<p><em>Huge thanks to Jon Sterling for the idea on how to write this code and then touching up the results</em></p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Fri, 17 Jul 2015 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2015-07-17-conats-for-cheap.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>A Basic Tutorial on JonPRL</title>
    <link>http://jozefg.bitbucket.org/posts/2015-07-06-jonprl.html</link>
    <description><![CDATA[<div class="info">
    Posted on July  6, 2015
    
</div>
<div class="info">
    
    Tags: <a href="/tags/jonprl.html">jonprl</a>, <a href="/tags/types.html">types</a>
    
</div>

<p><em>JonPRL switched to ASCII syntax so I’ve updated this post accordingly</em></p>
<p>I was just over at OPLSS for the last two weeks. While there I finally met <a href="http://www.jonmsterling.com/">Jon Sterling</a> in person. What was particularly fun is that for that last few months he’s been creating a proof assistant called <a href="https://github.com/jonsterling/JonPRL">JonPRL</a> in the spirit of Nuprl. As it turns out, it’s quite a fun project to work on so I’ve implemented a few features in it over the last couple of days and learned more or less how it works.</p>
<p>Since there’s basically no documentation on it besides the readme and of course the compiler so I thought I’d write down some of the stuff I’ve learned. There’s also a completely separate post on the underlying type theory for Nuprl and JonPRL that’s very interesting in its own right but this isn’t it. Hopefully I’ll get around to scribbling something about that because it’s really quite clever.</p>
<p>Here’s the layout of this tutorial</p>
<ul>
<li>First we start with a whirlwind tutorial. I’ll introduce the basic syntax and we’ll go through some simple proofs together</li>
<li>I’ll then dive into some of the rational behind JonPRL’s theory. This should help you understand why some things work how they do</li>
<li>I’ll show off a few of JonPRL’s more unique features and (hopefully) interest you enough to start fiddling on your own</li>
</ul>
<h2 id="getting-jonprl">Getting JonPRL</h2>
<p>JonPRL is pretty easy to build and install and having it will make this post more enjoyable. You’ll need <code>smlnj</code> since JonPRL is currently written in SML. This is available in most package managers (including homebrew) otherwise just grab the binary from the <a href="http://www.smlnj.org/">website</a>. After this the following commands should get you a working executable</p>
<ul>
<li><code>git clone ssh://git@github.com/jonsterling/jonprl</code></li>
<li><code>cd jonprl</code></li>
<li><code>git submodule init</code></li>
<li><code>git submodule update</code></li>
<li><code>make</code> (This is excitingly fast to run)</li>
<li><code>make test</code> (If you’re doubtful)</li>
</ul>
<p>You should now have an executable called <code>jonprl</code> in the <code>bin</code> folder. There’s no prelude for jonprl so that’s it. You can now just feed it files like any reasonable compiler and watch it spew (currently difficult-to-decipher) output at you.</p>
<p>If you’re interested in actually writing JonPRL code, you should probably install David Christiansen’s Emacs <a href="https://github.com/david-christiansen/jonprl-mode">mode</a>. Now that we’re up and running, let’s actually figure out how the language works</p>
<h2 id="the-different-languages-in-jonprl">The Different Languages in JonPRL</h2>
<p>JonPRL is composed of really 3 different sorts of mini-languages</p>
<ul>
<li>The term language</li>
<li>The tactic language</li>
<li>The language of commands to the proof assistant</li>
</ul>
<p>In Coq, these roughly correspond to Gallina, Ltac, and Vernacular respectively.</p>
<h3 id="the-term-language">The Term Language</h3>
<p>The term language is an untyped language that contains a number of constructs that should be familiar to people who have been exposed to dependent types before. The actual concrete syntax is composed of 3 basic forms:</p>
<ul>
<li>We can apply an “operator” (I’ll clarify this in a moment) with <code>op(arg1; arg2; arg3)</code>.</li>
<li>We have variables with <code>x</code>.</li>
<li>And we have abstraction with <code>x.e</code>. JonPRL has one construct for binding <code>x.e</code> built into its syntax, that things like <code>lam</code> or <code>fun</code> are built off of.</li>
</ul>
<p>An operator in this context is really anything you can imagine having a node in an AST for a language. So something like lam is an operator, as is <code>if</code> or <code>pair</code> (corresponding to <code>(,)</code> in Haskell). Each operator has a piece of information associated with it, called its arity. This arity tells you how many arguments an operator takes and how many variables <code>x.y.z. ...</code> each is allowed to bind. For example, with lam has an arity is written <code>(1)</code> since it takes 1 argument which binds 1 variable. Application (<code>ap</code>) has the arity <code>(0; 0)</code>. It takes 2 arguments neither of which bind a variable.</p>
<p>So as mentioned we have functions and application. This means we could write <code>(lamx.x) y</code> in JonPRL as <code>ap(lam(x.x); y)</code>. The type of functions is written with <code>fun</code>. Remember that JonPRL’s language has a notion of dependence so the arity is <code>(0; 1)</code>. The construct <code>fun(A; x.B)</code> corresponds to <code>(x : A) → B</code> in Agda or <code>forall (x : A), B</code> in Coq.</p>
<p>We also have dependent sums as well (<code>prod</code>s). In Agda you would write <code>(M , N)</code> to introduce a pair and <code>prod A lam x → B</code> to type it. In JonPRL you have <code>pair(M; N)</code> and <code>prod(A; x.B)</code>. To inspect a <code>prod</code> we have <code>spread</code> which let’s us eliminate a pair pair. Eg <code>spread(0; 2)</code> and you give it a <code>prod</code> in the first spot and <code>x.y.e</code> in the second. It’ll then replace <code>x</code> with the first component and <code>y</code> with the second. Can you think of how to write <code>fst</code> and <code>snd</code> with this?</p>
<p>There’s sums, so <code>inl(M)</code>, <code>inr(N)</code> and <code>+(A; B)</code> corresponds to <code>Left</code>, <code>Right</code>, and <code>Either</code> in Haskell. For case analysis there’s <code>decide</code> which has the arity <code>(0; 1; 1)</code>. You should read <code>decide(M; x.N; y.P)</code> as something like</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb1-1" data-line-number="1">    <span class="kw">case</span> <span class="dt">E</span> <span class="kw">of</span></a>
<a class="sourceLine" id="cb1-2" data-line-number="2">      <span class="dt">Left</span> x <span class="ot">-&gt;</span> <span class="dt">L</span></a>
<a class="sourceLine" id="cb1-3" data-line-number="3">      <span class="dt">Right</span> y <span class="ot">-&gt;</span> <span class="dt">R</span></a></code></pre></div>
<p>In addition we have <code>unit</code> and <code>&lt;&gt;</code> (pronounced axe for axiom usually). Neither of these takes any arguments so we write them just as I have above. They correspond to Haskell’s type- and value-level <code>()</code> respectively. Finally there’s <code>void</code> which is sometimes called <code>false</code> or <code>empty</code> in theorem prover land.</p>
<p>You’ll notice that I presented a bunch of types as if they were normal terms in this section. That’s because in this untyped computation system, types <em>are literally</em> just terms. There’s no typing relation to distinguish them yet so they just float around exactly as if they were lam or something! I call them types because I’m thinking of later when we have a typing relation built on top of this system but for now there are really just terms. It was still a little confusing for me to see <code>fun(unit; _.unit)</code> in a language without types, so I wanted to make this explicit.</p>
<p>Now we can introduce some more exotic terms. Later, we’re going to construct some rules around them that are going to make it behave that way we might expect but for now, they are just suggestively named constants.</p>
<ul>
<li><code>U{i}</code>, the ith level universe used to classify all types that can be built using types other than <code>U{i}</code> or higher. It’s closed under terms like <code>fun</code> and it contains all the types of smaller universes</li>
<li><code>=(0; 0; 0)</code> this is equality between two terms at a type. It’s a proposition that’s going to precisely mirror what’s going on later in the type theory with the equality judgment</li>
<li><code>member(0; 0)</code> this is just like <code>=</code> but internalizes membership in a type into the system. Remember that normally “This has that type” is a judgment but with this term we’re going to have a propositional counterpart to use in theorems.</li>
</ul>
<p>In particular it’s important to distinguish the difference between <code>∈</code> the judgment and member the term. There’s nothing inherent in <code>member</code> above that makes it behave like a typing relation as you might expect. It’s on equal footing with <code>flibbertyjibberty(0; 0; 0)</code>.</p>
<p>This term language contains the full untyped lambda calculus so we can write all sorts of fun programs like</p>
<pre class="jonprl"><code>    lam(f.ap(lam(x.ap(f;(ap(x;x)))); lam(x.ap(f;(ap(x;x)))))</code></pre>
<p>which is just the Y combinator. In particular this means that there’s no reason that every term in this language should normalize to a value. There are plenty of terms in here that diverge and in principle, there’s nothing that rules out them doing even stranger things than that. We really only depend on them being deterministic, that <code>e ⇒ v</code> and <code>e ⇒ v'</code> implies that <code>v = v'</code>.</p>
<h3 id="tactics">Tactics</h3>
<p>The other big language in JonPRL is the language of tactics. Luckily, this is very familiarly territory if you’re a Coq user. Unluckily, if you’ve never heard of Coq’s tactic mechanism this will seem completely alien. As a quick high level idea for what tactics are:</p>
<p>When we’re proving something in a proof assistant we have to deal with a lot of boring mechanical details. For example, when proving <code>A → B → A</code> I have to describe that I want to introduce the <code>A</code> and the <code>B</code> into my context, then I have to suggest using that <code>A</code> the context as a solution to the goal. Bleh. All of that is pretty obvious so let’s just get the computer to do it! In fact, we can build up a DSL of composable “proof procedures” or /tactics/ to modify a particular goal we’re trying to prove so that we don’t have to think so much about the low level details of the proof being generated. In the end this DSL will generate a proof term (or derivation in JonPRL) and we’ll check that so we never have to trust the actual tactics to be sound.</p>
<p>In Coq this is used to great effect. In particular see Adam Chlipala’s <a href="http://adam.chlipala.net/cpdt/">book</a> to see incredibly complex theorems with one-line proofs thanks to tactics.</p>
<p>In JonPRL the tactic system works by modifying a sequent of the form <code>H ⊢ A</code> (a goal). Each time we run a tactic we get back a list of new goals to prove until eventually we get to trivial goals which produce no new subgoals. This means that when trying to prove a theorem in the tactic language we never actually see the resulting evidence generated by our proof. We just see this list of <code>H ⊢ A</code>s to prove and we do so with tactics.</p>
<p>The tactic system is quite simple, to start we have a number of basic tactics which are useful no matter what goal you’re attempting to prove</p>
<ul>
<li><code>id</code> a tactic which does nothing</li>
<li><code>t1; t2</code> this runs the <code>t1</code> tactic and runs <code>t2</code> on any resulting subgoals</li>
<li><code>*{t}</code> this runs <code>t</code> as long as <code>t</code> does <em>something</em> to the goal. If <code>t</code> ever fails for whatever reason it merely stops running, it doesn’t fail itself</li>
<li><code>?{t}</code> tries to run <code>t</code> once. If <code>t</code> fails nothing happens</li>
<li><code>!{t}</code> runs <code>t</code> and if <code>t</code> does anything besides complete the proof it fails. This means that <code>!{id}</code> for example will always fail.</li>
<li><code>t1 | t2</code> runs <code>t1</code> and if it fails it runs <code>t2</code>. Only one of the effects for <code>t1</code> and <code>t2</code> will be shown.</li>
<li><code>t; [t1, ..., tn]</code> first runs <code>t</code> and then runs tactic <code>ti</code> on subgoal <code>i</code>th subgoal generated by <code>t</code></li>
<li><code>trace &quot;some words&quot;</code> will print <code>some words</code> to standard out. This is useful when trying to figure out why things haven’t gone your way.</li>
<li><code>fail</code> is the opposite of <code>id</code>, it just fails. This is actually quite useful for forcing backtracking and one could probably implement a makeshift <code>!{}</code> as <code>t; fail</code>.</li>
</ul>
<p>It’s helpful to see this as a sort of tree, a tactic takes one goal to a list of a subgoals to prove so we can imagine <code>t</code> as this part of a tree</p>
<pre><code>      H
      |
———–————————— (t)
H&#39;  H&#39;&#39;  H&#39;&#39;&#39;</code></pre>
<p>If we have some tactic <code>t2</code> then <code>t; t2</code> will run <code>t</code> and then run <code>t2</code> on <code>H</code>, <code>H'</code>, and <code>H''</code>. Instead we could have <code>t; [t1, t2, t3]</code> then we’ll run <code>t</code> and (assuming it succeeds) we’ll run <code>t1</code> on <code>H'</code>, <code>t2</code> on <code>H''</code>, and <code>t3</code> on <code>H'''</code>. This is actually how things work under the hood, composable fragments of trees :)</p>
<p>Now those give us a sort of bedrock for building up scripts of tactics. We also have a bunch of tactics that actually let us manipulate things we’re trying to prove. The 4 big ones to be aware of are</p>
<ul>
<li><code>intro</code></li>
<li><code>elim #NUM</code></li>
<li><code>eq-cd</code></li>
<li><code>mem-cd</code></li>
</ul>
<p>The basic idea is that <code>intro</code> modifies the <code>A</code> part of the goal. If we’re looking at a function, so something like <code>H ⊢ fun(A; x.B)</code>, this will move that <code>A</code> into the context, leaving us with <code>H, x : A ⊢ B</code>.</p>
<p>If you’re familiar with sequent calculus <code>intro</code> runs the appropriate right rule for the goal. If you’re not familiar with sequent calculus <code>intro</code> looks at the outermost operator of the <code>A</code> and runs a rule that applies when that operator is to the right of a the <code>⊢</code>.</p>
<p>Now one tricky case is what should <code>intro</code> do if you’re looking at a prod? Well now things get a bit dicey. We’d might expect to get two subgoals if we run <code>intro</code> on <code>H ⊢ prod(A; x.B)</code>, one which proves <code>H ⊢ A</code> and one which proves <code>H ⊢ B</code> or something, but what about the fact that <code>x.B</code> <em>depends</em> on whatever the underlying realizer (that’s the program extracted from) the proof of <code>H ⊢ A</code>! Further, Nuprl and JonPRL are based around extract-style proof systems. These mean that a goal shouldn’t depend on the particular piece of evidence proving of another goal. So instead we have to tell <code>intro</code> up front what we want the evidence for <code>H ⊢ A</code> to be is so that the <code>H ⊢ B</code> section may use it.</p>
<p>To do this we just give intro an argument. For example say we’re proving that <code>· ⊢ prod(unit; x.unit)</code>, we run <code>intro [&lt;&gt;]</code> which gives us two subgoals <code>· ⊢ member(&lt;&gt;; unit)</code> and <code>· ⊢ unit</code>. Here the <code>[]</code> let us denote the realizer we’re passing to <code>intro</code>. In general any term arguments to a tactic will be wrapped in <code>[]</code>s. So the first goal says “OK, you said that this was your realizer for <code>unit</code>, but is it actually a realizer for <code>unit</code>?” and the second goal substitutes the given realizer into the second argument of <code>prod</code>, <code>x.unit</code>, and asks us to prove that. Notice how here we have to prove <code>member(&lt;&gt;; unit)</code>? This is where that weird <code>member</code> type comes in handy. It let’s us sort of play type checker and guide JonPRL through the process of type checking. This is actually very crucial since type checking in Nuprl and JonPRL is undecidable.</p>
<p>Now how do we actually go about proving <code>member(&lt;&gt;; unit)</code>? Well here <code>mem-cd</code> has got our back. This tactic transforms <code>member(A; B)</code> into the equivalent form <code>=(A; A; B)</code>. In JonPRL and Nuprl, types are given meaning by how we interpret the equality of their members. In other words, if you give me a type you have to say</p>
<ol type="1">
<li>What canonical terms are in that terms</li>
<li>What it means for two canonical members to be equal</li>
</ol>
<p>Long ago, Stuart Allen realized we could combine the two by specifying a <em>partial</em> equivalence relation for a type. In this case rather than having a separate notion of membership we check to see if something is equal to itself under the PER because when it is that PER behaves like a normal equivalence relation! So in JonPRL <code>member</code> is actually just a very thin layer of sugar around <code>=</code> which is really the core defining notion of typehood. To handle <code>=</code> we have <code>eq-cd</code> which does clever things to handle most of the obvious cases of equality.</p>
<p>Finally, we have <code>elim</code>. Just like <code>intro</code> let us simplify things on the right of the ⊢, <code>elim</code> let’s us eliminate something on the left. So we tell <code>elim</code> to “eliminate” the nth item in the context (they’re numbered when JonPRL prints them) with <code>elim #n</code>.</p>
<p>Just like with anything, it’s hard to learn all the tactics without experimenting (though a complete list can be found with <code>jonprl --list-tactics</code>). Let’s go look at the command language so we can actually prove some theorems.</p>
<h3 id="commands">Commands</h3>
<p>So in JonPRL there are only 4 commands you can write at the top level</p>
<ul>
<li><code>Operator</code></li>
<li><code>[oper] =def= [term]</code> (A definition)</li>
<li><code>Tactic</code></li>
<li><code>Theorem</code></li>
</ul>
<p>The first three of these let us customize and extend the basic suite of operators and tactics JonPRL comes with. The last actually lets us state and prove theorems.</p>
<p>The best way to see these things is by example so we’re going to build up a small development in JonPRL. We’re going to show that products are monoid with <code>unit</code> up to some logical equivalence. There are a lot of proofs involved here</p>
<ol type="1">
<li><code>prod(unit; A)</code> entails <code>A</code></li>
<li><code>prod(A; unit)</code> entails <code>A</code></li>
<li><code>A</code> entails <code>prod(unit; A)</code></li>
<li><code>A</code> entails <code>prod(A; unit)</code></li>
<li><code>prod(A; prod(B; C))</code> entails <code>prod(prod(A; B); C)</code></li>
<li><code>prod(prod(A; B); C)</code> entails <code>prod(A; prod(B; C))</code></li>
</ol>
<p>I intend to prove 1, 2, and 5. The remaining proofs are either very similar or fun puzzles to work on. We could also prove that all the appropriate entailments are inverses and then we could say that everything is up to isomorphism.</p>
<p>First we want a new snazzy operator to signify nondependent products since writing <code>prod(A; x.B)</code> is kind of annoying. We do this using operator</p>
<pre class="jonprl"><code>    Operator prod : (0; 0).</code></pre>
<p>This line declares <code>prod</code> as a new operator which takes two arguments binding zero variables each. Now we really want JonPRL to know that <code>prod</code> is sugar for <code>prod</code>. To do this we use <code>=def=</code> which gives us a way to desugar a new operator into a mess of existing ones.</p>
<pre class="jonprl"><code>    [prod(A; B)] =def= [prod(A; _.B)].</code></pre>
<p>Now we can change any occurrence of <code>prod(A; B)</code> for <code>prod(A; _.B)</code> as we’d like. Okay, so we want to prove that we have a monoid here. What’s the first step? Let’s verify that <code>unit</code> is a left identity for <code>prod</code>. This entails proving that for all types <code>A</code>, <code>prod(unit; A) ⊃ A</code> and <code>A ⊃ prod(unit; A)</code>. Let’s prove these as separate theorems. Translating our first statement into JonPRL we want to prove</p>
<pre class="jonprl"><code>    fun(U{i}; A.
    fun(prod(unit; A); _.
    A))</code></pre>
<p>In Agda notation this would be written</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb7-1" data-line-number="1">    <span class="ot">(</span>A <span class="ot">:</span> <span class="dt">Set</span><span class="ot">)</span> <span class="ot">→</span> <span class="ot">(_</span> <span class="ot">:</span> prod<span class="ot">(</span>unit<span class="ot">;</span> A<span class="ot">))</span> <span class="ot">→</span> A</a></code></pre></div>
<p>Let’s prove our first theorem, we start by writing</p>
<pre class="jonprl"><code>    Theorem left-id1 :
      [fun(U{i}; A.
       fun(prod(unit; A); _.
       A))] {
      id
    }.</code></pre>
<p>This is the basic form of a theorem in JonPRL, a name, a term to prove, and a tactic script. Here we have <code>id</code> as a tactic script, which clearly doesn’t prove our goal. When we run JonPRL on this file (C-c C-l if you’re in Emacs) you get back</p>
<pre><code>[XXX.jonprl:8.3-9.1]: tactic &#39;COMPLETE&#39; failed with goal:
⊢ funA ∈ U{i}. (prod(unit; A)) =&gt; A

Remaining subgoals:
⊢ funA ∈ U{i}. (prod(unit; A)) =&gt; A</code></pre>
<p>So focus on that <code>Remaining subgoals</code> bit, that’s what we have left to prove, it’s our current goal. Now you may notice that this outputted goal is a lot prettier than our syntax! That’s because currently in JonPRL the input and outputted terms may not match, the latter is subject to pretty printing. In general this is great because you can read your remaining goals, but it does mean copying and pasting is a bother. There’s nothing to the left of that ⊢ yet so let’s run the only applicable tactic we know. Delete that <code>id</code> and replace it with</p>
<pre class="jonprl"><code>    {
      intro
    }.</code></pre>
<p>The goal now becomes</p>
<p>Remaining subgoals:</p>
<pre><code>1. A : U{i}
⊢ (prod(unit; A)) =&gt; A

⊢ U{i} ∈ U{i&#39;}</code></pre>
<p>Two ⊢s means two subgoals now. One looks pretty obvious, <code>U{i'}</code> is just the universe above <code>U{i}</code> (so that’s like Set₁ in Agda) so it should be the case that <code>U{i} ∈ U{i'}</code> by definition! So the next tactic should be something like <code>[???, mem-cd; eq-cd]</code>. Now what should that ??? be? Well we can’t use <code>elim</code> because there’s one thing in the context now (<code>A : U{i}</code>), but it doesn’t help us really. Instead let’s run <code>unfold &lt;prod&gt;</code>. This is a new tactic that’s going to replace that <code>prod</code> with the definition that we wrote earlier.</p>
<pre class="jonprl"><code>    {
      intro; [unfold &lt;prod&gt;, mem-cd; eq-cd]
    }</code></pre>
<p>Notice here that <code>,</code> behinds less tightly than <code>;</code> which is useful for saying stuff like this. This gives us</p>
<pre><code>Remaining subgoals:

1. A : U{i}
⊢ (unit × A) =&gt; A</code></pre>
<p>We run intro again</p>
<pre class="jonprl"><code>    {
      intro; [unfold &lt;prod&gt;, mem-cd; eq-cd]; intro
    }</code></pre>
<p>Now we are in a similar position to before with two subgoals.</p>
<pre class="jonprl"><code>    Remaining subgoals:

    1. A : U{i}
    2. _ : unit × A
    ⊢ A


    1. A : U{i}
    ⊢ unit × A ∈ U{i}</code></pre>
<p>The first subgoal is really what we want to be proving so let’s put a pin in that momentarily. Let’s get rid of that second subgoal with a new helpful tactic called <code>auto</code>. It runs <code>eq-cd</code>, <code>mem-cd</code> and <code>intro</code> repeatedly and is built to take care of boring goals just like this!</p>
<pre class="jonprl"><code>    {
      intro; [unfold &lt;prod&gt;, mem-cd; eq-cd]; intro; [id, auto]
    }</code></pre>
<p>Notice that we used what is a pretty common pattern in JonPRL, to work on one subgoal at a time we use <code>[]</code>’s and <code>id</code>s everywhere except where we want to do work, in this case the second subgoal.</p>
<p>Now we have</p>
<pre><code>Remaining subgoals:

1. A : U{i}
2. _ : unit × A
⊢ A</code></pre>
<p>Cool! Having a pair of <code>unit × A</code> really ought to mean that we have an <code>A</code> so we can use <code>elim</code> to get access to it.</p>
<pre class="jonprl"><code>    {
      intro; [unfold &lt;prod&gt;, mem-cd; eq-cd]; intro; [id, auto];
      elim #2
    }</code></pre>
<p>This gives us</p>
<pre><code>Remaining subgoals:

1. A : U{i}
2. _ : unit × A
3. s : unit
4. t : A
⊢ A</code></pre>
<p>We’ve really got the answer now, #4 is precisely our goal. For this situations there’s <code>assumption</code> which is just a tactic which succeeds if what we’re trying to prove is in our context already. This will complete our proof</p>
<pre class="jonprl"><code>    Theorem left-id1 :
      [fun(U{i}; A.
       fun(prod(unit; A); _.
       A))] {
      intro; [unfold &lt;prod&gt;, mem-cd; eq-cd]; intro; [id, auto];
      elim #2; assumption
    }.</code></pre>
<p>Now we know that <code>auto</code> will run all of the tactics on the first line except <code>unfold &lt;prod&gt;</code>, so what we just <code>unfold &lt;prod&gt;</code> first and run <code>auto</code>? It ought to do all the same stuff.. Indeed we can shorten our whole proof to <code>unfold &lt;prod&gt;; auto; elim #2; assumption</code>. With this more heavily automated proof, proving our next theorem follows easily.</p>
<pre class="jonprl"><code>    Theorem right-id1 :
      [fun(U{i}; A.
       fun(prod(A; unit); _.
       A))] {
      unfold &lt;prod&gt;; auto; elim #2; assumption
    }.</code></pre>
<p>Next, we have to prove associativity to complete the development that <code>prod</code> is a monoid. The statement here is a bit more complex.</p>
<pre class="jonprl"><code>    Theorem assoc :
      [fun(U{i}; A.
       fun(U{i}; B.
       fun(U{i}; C.
       fun(prod(A; prod(B;C)); _.
       prod(prod(A;B); C)))))] {
      id
    }.</code></pre>
<p>In Agda notation what I’ve written above is</p>
<div class="sourceCode" id="cb23"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb23-1" data-line-number="1">    assoc <span class="ot">:</span> <span class="ot">(</span>A B C <span class="ot">:</span> <span class="dt">Set</span><span class="ot">)</span> <span class="ot">→</span> A × <span class="ot">(</span>B × C<span class="ot">)</span> <span class="ot">→</span> <span class="ot">(</span>A × B<span class="ot">)</span> × C</a>
<a class="sourceLine" id="cb23-2" data-line-number="2">    assoc <span class="ot">=</span> ?</a></code></pre></div>
<p>Let’s kick things off with <code>unfold &lt;prod&gt;; auto</code> to deal with all the boring stuff we had last time. In fact, since <code>x</code> appears in several nested places we’d have to run <code>unfold</code> quite a few times. Let’s just shorten all of those invocations into <code>*{unfold &lt;prod&gt;}</code></p>
<pre class="jonprl"><code>    {
      *{unfold &lt;prod&gt;}; auto
    }</code></pre>
<p>This leaves us with the state</p>
<p>Remaining subgoals:</p>
<pre><code>1. A : U{i}
2. B : U{i}
3. C : U{i}
4. _ : A × B × C
⊢ A


1. A : U{i}
2. B : U{i}
3. C : U{i}
4. _ : A × B × C
⊢ B


1. A : U{i}
2. B : U{i}
3. C : U{i}
4. _ : A × B × C
⊢ C</code></pre>
<p>In each of those goals we need to take apart the 4th hypothesis so let’s do that</p>
<pre class="jonprl"><code>    {
      *{unfold &lt;prod&gt;}; auto; elim #4
    }</code></pre>
<p>This leaves us with 3 subgoals still</p>
<pre><code>1. A : U{i}
2. B : U{i}
3. C : U{i}
4. _ : A × B × C
5. s : A
6. t : B × C
⊢ A


1. A : U{i}
2. B : U{i}
3. C : U{i}
4. _ : A × B × C
5. s : A
6. t : B × C
⊢ B


1. A : U{i}
2. B : U{i}
3. C : U{i}
4. _ : A × B × C
5. s : A
6. t : B × C
⊢ C</code></pre>
<p>The first subgoal is pretty easy, <code>assumption</code> should handle that. In the other two we want to eliminate 6 and <em>then</em> we should be able to apply assumption. In order to deal with this we use <code>|</code> to encode that disjunction. In particular we want to run <code>assumption</code> OR <code>elim #6; assumption</code> leaving us with</p>
<pre class="jonprl"><code>    {
      *{unfold &lt;prod&gt;}; auto; elim #4; (assumption | elim #6; assumption)
    }</code></pre>
<p>This completes the proof!</p>
<pre class="jonprl"><code>    Theorem assoc :
      [fun(U{i}; A.
       fun(U{i}; B.
       fun(U{i}; C.
       fun(prod(A; prod(B;C)); _.
       prod(prod(A;B); C)))))] {
      *{unfold &lt;prod&gt;}; auto; elim #4; (assumption | elim #6; assumption)
    }.</code></pre>
<p>As a fun puzzle, what needs to change in this proof to prove we can associate the other way?</p>
<h2 id="what-on-earth-did-we-just-do">What on earth did we just do!?</h2>
<p>So we just proved a theorem.. but what really just happened? I mean how did we go from “Here we have an untyped computation system which types just behaving as normal terms” to “Now apply <code>auto</code> and we’re done!”. In this section I’d like to briefly sketch the path from untyped computation to theorems.</p>
<p>The path looks like this</p>
<ul>
<li><p>We start with our untyped language and its notion of computation</p>
<p>We already discussed this in great depth before.</p></li>
<li><p>We define a judgment <code>a = b ∈ A</code>.</p>
<p>This is a judgment, not a term in that language. It exists in whatever metalanguage we’re using. This judgment is defined across 3 terms in our untyped language (I’m only capitalizing <code>A</code> out of convention). This is supposed to represent that <code>a</code> and <code>b</code> are equal elements of type <code>A</code>. This also gives meaning to typehood: something is a type in CTT precisely when we know what the partial equivalence relation defined by <code>- = - ∈ A</code> on canonical values is.</p>
<p>Notice here that I said <em>partial</em>. It isn’t the case that <code>a = b ∈ A</code> presupposes that we know that <code>a : A</code> and <code>b : A</code> because we don’t have a notion of <code>:</code> yet!</p>
<p>In some sense this is where we depart from a type theory like Coq or Agda’s. We have programs already and on top of them we define this 3 part judgment which interacts which computation in a few ways I’m not specifying. In Coq, we would specify <em>one</em> notion of equality, generic over all types, and separately specify a typing relation.</p></li>
<li><p>From here we can define the normal judgments of Martin Lof’s type theory. For example, <code>a : A</code> is <code>a = a ∈ A</code>. We recover the judgment <code>A type</code> with <code>A = A ∈ U</code> (where <code>U</code> here is a universe).</p>
<p>This means that inhabiting a universe <code>A = A ∈ U</code>, isn’t necessarily inductively defined but rather negatively generated. We specify some condition a term must satisfy to occupy a universe.</p></li>
</ul>
<p>Hypothetical judgments are introduced in the same way they would be in Martin-Lof’s presentations of type theory. The idea being that <code>H ⊢ J</code> if <code>J</code> is evident under the assumption that each term in <code>H</code> has the appropriate type and furthermore that <code>J</code> is functional (respects equality) with respect to what <code>H</code> contains. This isn’t really a higher order judgment, but it will be defined in terms of a higher order hypothetical judgment in the metatheory.</p>
<p>With this we have something that walks and quacks like normal type theory. Using the normal tools of our metatheory we can formulate proofs of <code>a : A</code> and do normal type theory things. This whole development is building up what is called “Computational Type Theory”. The way this diverges from Martin-Lof’s extensional type theory is subtle but it does directly descend from Martin-Lof’s famous 1979 paper <a href="http://www.cse.chalmers.se/~peterd/papers/MartinL%C3%B6f1979.pdf">“Constructive Mathematics and Computer Programming”</a> (which you should read. Instead of my blog post).</p>
<p>Now there’s one final layer we have to consider, the PRL bit of JonPRL. We define a new judgment, <code>H ⊢ A [ext a]</code>. This is judgment is cleverly set up so two properties hold</p>
<ul>
<li><code>H ⊢ A [ext a]</code> should entail that <code>H ⊢ a : A</code> or <code>H ⊢ a = a ∈ A</code></li>
<li>In <code>H ⊢ A [ext a]</code>, <code>a</code> is an output and <code>H</code> and <code>A</code> are inputs. In particular, this implies that in any inference for this judgment, the subgoals may not use <code>a</code> in their <code>H</code> and <code>A</code>.</li>
</ul>
<p>This means that <code>a</code> is completely determined by <code>H</code> and <code>A</code> which justifies my use of the term output. I mean this in the sense of Twelf and logic programming if that’s a more familiar phrasing. It’s this judgment that we see in JonPRL! Since that <code>a</code> is output we simply hide it, leaving us with <code>H ⊢ A</code> as we saw before. When we prove something with tactics in JonPRL we’re generating a <em>derivation</em>, a tree of inference rules which make <code>H ⊢ A</code> evident for our particular <code>H</code> and <code>A</code>! These rules aren’t really programs though, they don’t correspond one to one with proof terms we may run like they would in Coq. The computational interpretation of our program is bundled up in that <code>a</code>.</p>
<p>To see what I mean here we need a little bit more machinery. Specifically, let’s look at the rules for the equality around the proposition <code>=(a; b; A)</code>. Remember that we have a term <code>&lt;&gt;</code> lying around,</p>
<pre><code>     a = b ∈ A
————————————————————
&lt;&gt; = &lt;&gt; ∈ =(a; b; A)</code></pre>
<p>So the only member of <code>=(a; b; A)</code> is <code>&lt;&gt;</code> <strong>if</strong> <code>a = b ∈ A</code> actually holds. First off, notice that <code>&lt;&gt; : A</code> and <code>&lt;&gt; : B</code> doesn’t imply that <code>A = B</code>! In another example, <code>lam(x. x) ∈ fun(A; _.A)</code> for all <code>A</code>! This is a natural consequence of separating our typing judgment from our programming language. Secondly, there’s not really any computation in the <code>e</code> of <code>H ⊢ =(a; b; A) (e)</code>. After all, in the end the only thing <code>e</code> could be so that <code>e : =(a; b; A)</code> is <code>&lt;&gt;</code>! However, there is potentially quite a large derivation involved in showing <code>=(a; b; A)</code>! For example, we might have something like this</p>
<pre><code>x : =(A; B; U{i}); y : =(b; a; A) ⊢ =(a; b; B)
———————————————————————————————————————————————— Substitution
x : =(A; B; U{i}); y : =(b; a; A) ⊢ =(a; b; A)
———————————————————————————————————————————————— Symmetry
x : =(A; B; U{i}); y : =(b; a; A) ⊢ =(b; a; A)
———————————————————————————————————————————————— Assumption</code></pre>
<p>Now we write derivations of this sequent up side down, so the thing we want to show starts on top and we write each rule application and subgoal below it (AI people apparently like this?). Now this was quite a derivation, but if we fill in the missing <code>[ext e]</code> for this derivation from the bottom up we get this</p>
<pre><code>x : =(A; B; U{i}); y : =(b; a; A) ⊢ =(a; b; B)
———————————————————————————————————————————————— Substitution [ext &lt;&gt;]
x : =(A; B; U{i}); y : =(b; a; A) ⊢ =(a; b; A)
———————————————————————————————————————————————— Symmetry     [ext &lt;&gt;]
x : =(A; B; U{i}); y : =(b; a; A) ⊢ =(b; a; A)
———————————————————————————————————————————————— Assumption   [ext x]</code></pre>
<p>Notice how at the bottom there was some computational content (That <code>x</code> signifies that we’re accessing a variable in our context) but than we throw it away right on the next line! That’s because we find that no matter what the extract was that let’s us derive <code>=(b; a; A)</code>, the only realizer it could possible generate is <code>&lt;&gt;</code>. Remember our conditions, if we can make evident the fact that <code>b = a ∈ A</code> then <code>&lt;&gt; ∈ =(b; a; A)</code>. Because we somehow managed to prove that <code>b = a ∈ A</code> holds, we’re entitled to just use <code>&lt;&gt;</code> to realize our proof. This means that despite our somewhat tedious derivation and the bookkeeping that we had to do to generate that program, that program reflects <em>none</em> of it.</p>
<p>This is why type checking in JonPRL is woefully undecidable: in part, the realizers that we want to type check contain none of the helpful hints that proof terms in Coq would. This also means that extraction from JonPRL proofs is built right into the system and we can actually generate cool and useful things! In Nuprl-land, folks at Cornell actually write proofs and use this realizers to run real software. From what Bob Constable said at OPLSS they can actually get these programs to run fast (within 5x of naive C code).</p>
<p>So to recap, in JonPRL we</p>
<ul>
<li>See <code>H ⊢ A</code></li>
<li>Use tactics to generate a derivation of this judgment</li>
<li>Once this derivation is generated, we can extract the computational content as a program in our untyped system</li>
</ul>
<p>In fact, we can see all of this happen if you call JonPRL from the command line <em>or</em> hit C-c C-c in emacs! On our earlier proof we see</p>
<pre><code>Operator prod : (0; 0).
⸤prod(A; B)⸥ ≝ ⸤A × B⸥.

Theorem left-id1 : ⸤⊢ funA ∈ U{i}. (prod(unit; A)) =&gt; A⸥ {
  fun-intro(A.fun-intro(_.prod-elim(_; _.t.t); prod⁼(unit⁼; _.hyp⁼(A))); U⁼{i})
} ext {
  lam_. lam_. spread(_; _.t.t)
}.

Theorem right-id1 : ⸤⊢ funA ∈ U{i}. (prod(A; unit)) =&gt; A⸥ {
  fun-intro(A.fun-intro(_.prod-elim(_; s._.s); prod⁼(hyp⁼(A); _.unit⁼)); U⁼{i})
} ext {
  lam_. lam_. spread(_; s._.s)
}.

Theorem assoc : ⸤⊢ funA ∈ U{i}. funB ∈ U{i}. funC ∈ U{i}. (prod(A; prod(B; C))) =&gt; prod(prod(A; B); C)⸥ {
  fun-intro(A.fun-intro(B.fun-intro(C.fun-intro(_.independent-prod-intro(independent-prod-intro(prod-elim(_;
  s.t.prod-elim(t; _._.s)); prod-elim(_; _.t.prod-elim(t;
  s&#39;._.s&#39;))); prod-elim(_; _.t.prod-elim(t; _.t&#39;.t&#39;)));
  prod⁼(hyp⁼(A); _.prod⁼(hyp⁼(B); _.hyp⁼(C)))); U⁼{i}); U⁼{i});
  U⁼{i})
} ext {
  lam_. lam_. lam_. lam_. ⟨⟨spread(_; s.t.spread(t; _._.s)), spread(_; _.t.spread(t; s&#39;._.s&#39;))⟩, spread(_; _.t.spread(t; _.t&#39;.t&#39;))⟩
}.</code></pre>
<p>Now we can see that those <code>Operator</code> and <code>≝</code> bits are really what we typed with <code>=def=</code> and <code>Operator</code> in JonPRL, what’s interesting here are the theorems. There’s two bits, the derivation and the extract or realizer.</p>
<pre><code>{
  derivation of the sequent · ⊢ A
} ext {
  the program in the untyped system extracted from our derivation
}</code></pre>
<p>We can move that derivation into a different proof assistant and check it. This gives us all the information we need to check that JonPRL’s reasoning and helps us not trust <em>all</em> of JonPRL (I wrote some of it so I’d be a little scared to trust it :). We can also see the computational bit of our proof in the extract. For example, the computation involved in taking <code>A × unit → A</code> is just <code>lam_. lam_. spread(_; s._.s)</code>! This is probably closer to what you’ve seen in Coq or Idris, even though I’d say the derivation is probably more similar in spirit (just ugly and beta normal). That’s because the extract need not have any notion of typing or proof, it’s just the computation needed to produce a witness of the appropriate type. This means for a really tricky proof of equality, your extract might just be <code>&lt;&gt;</code>! Your derivation however will always exactly reflect the complexity of your proof.</p>
<h2 id="killer-features">Killer features</h2>
<p>OK, so I’ve just dumped about 50 years worth of hard research in type theory into your lap which is best left to ruminate for a bit. However, before I finish up this post I wanted to do a little bit of marketing so that you can see why one might be interested in JonPRL (or Nuprl). Since we’ve embraced this idea of programs first and types as PERs, we can define some really strange types completely seamlessly. For example, in JonPRL there’s a type <code>⋂(A; x.B)</code>, it behaves a lot like <code>fun</code> but with one big difference, the definition of <code>- = - ∈ ⋂(A; x.B)</code> looks like this</p>
<pre><code>a : A ⊢ e = e&#39; ∈ [a/x]B
————————————————————————
   e = e&#39; ∈ ⋂(A; x.B)</code></pre>
<p>Notice here that <code>e</code> and <code>e'</code> <em>may not use</em> <code>a</code> anywhere in their bodies. That is, they have to be in <code>[a/x]B</code> without knowing anything about <code>a</code> and without even having access to it.</p>
<p>This is a pretty alien concept that turned out to be new in logic as well (it’s called “uniform quantification” I believe). It turns out to be very useful in PRL’s because it lets us declare things in our theorems without having them propogate into our witness. For example, we could have said</p>
<pre class="jonprl"><code>    Theorem right-id1 :
          [⋂(U{i}; A.
           fun(prod(A; unit); _.
           A))] {
          unfold &lt;prod&gt;; auto; elim #2; assumption
        }.</code></pre>
<p>With the observation that our realizer doesn’t need to depend on <code>A</code> at all (remember, no types!). Then the extract of this theorem is</p>
<pre><code>lamx. spread(x; s._.s)</code></pre>
<p>There’s no spurious <code>lam _. ...</code> at the beginning! Even more wackily, we can define subsets of an existing type since realizers need not have unique types</p>
<pre><code>e = e&#39; ∈ A  [e/x]P  [e&#39;/x]P
————————————————————————————
  e = e&#39; ∈ subset(A; x.P)</code></pre>
<p>And in JonPRL we can now say things like “all odd numbers” by just saying <code>subset(nat; n. ap(odd; n))</code>. In intensional type theories, these types are hard to deal with and still the subject of open research. In CTT they just kinda fall out because of how we thought about types in the first place. Quotients are a similarly natural conception (just define a new type with a stricter PER) but JonPRL currently lacks them (though they shouldn’t be hard to add..).</p>
<p>Finally, if you’re looking for one last reason to dig into **PRL, the fact that we’ve defined all our equalities extensionally means that several very useful facts just fall right out of our theory</p>
<pre class="jonprl"><code>    Theorem fun-ext :
      [⋂(U{i}; A.
       ⋂(fun(A; _.U{i}); B.
       ⋂(fun(A; a.ap(B;a)); f.
       ⋂(fun(A; a.ap(B;a)); g.

       ⋂(fun(A; a.=(ap(f; a); ap(g; a); ap(B; a))); _.
       =(f; g; fun(A; a.ap(B;a))))))))] {
      auto; ext; ?{elim #5 [a]}; auto
    }.</code></pre>
<p>This means that two functions are equal in JonPRL if and only if they map equal arguments to equal output. This is quite pleasant for formalizing mathematics for example.</p>
<h2 id="wrap-up">Wrap Up</h2>
<p>Whew, we went through a lot! I didn’t intend for this to be a full tour of JonPRL, just a taste of how things sort of hang together and maybe enough to get you looking through the examples. Speaking of which, JonPRL comes with quite a few <a href="https://github.com/jonsterling/JonPRL/tree/master/example">examples</a> which are going to make a lot more sense now.</p>
<p>Additionally, you may be interested in the documentation in the README which covers most of the primitive operators in JonPRL. As for an exhaustive list of tactics, <a href="https://github.com/jonsterling/JonPRL/issues/83">well…</a>.</p>
<p>Hopefully I’ll be writing about JonPRL again soon. Until then, I hope you’ve learned something cool :)</p>
<p><em>A huge thanks to David Christiansen and Jon Sterling for tons of helpful feedback on this</em></p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Mon, 06 Jul 2015 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2015-07-06-jonprl.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Proving Cut Admissibility in Twelf</title>
    <link>http://jozefg.bitbucket.org/posts/2015-06-29-cut.html</link>
    <description><![CDATA[<div class="info">
    Posted on June 29, 2015
    
</div>
<div class="info">
    
    Tags: <a href="/tags/twelf.html">twelf</a>, <a href="/tags/types.html">types</a>
    
</div>

<p>Veering wildly onto the theory side compared to my last post, I’d like to look at some more Twelf code today. Specifically, I’d like to prove a fun theorem called cut admissibility (or elimination) for a particular logic: a simple intuitionistic propositional sequent calculus. I chucked the code for this <a href="http://github.com/jozefg/cut.elf">over here</a>.</p>
<h2 id="background">Background</h2>
<p>If those words didn’t make any sense, here’ an incomplete primer on what we’re doing here. First of all we’re working with a flavor of logic called “sequent calculus”. Sequent calculus describes a class of logics characterized by using studying “sequents”, a sequent is just an expression <code>Γ ⊢ A</code> saying “<code>A</code> is true under the assumption that the set of propositions, <code>Γ</code>, are true”. A sequent calculus defines a couple things</p>
<ol type="1">
<li><p>What exactly <code>A</code> is, a calculus defines what propositions it talks about</p>
<p>For us we’re only interested in a few basic connectives, so our calculus can talk about <code>true</code>, <code>false</code>, <code>A</code> and <code>B</code> (<code>A ∧ B</code>), <code>A</code> or <code>B</code> (<code>A ∨ B</code>), and <code>A</code> implies <code>B</code> (<code>A ⇒ B</code>).</p></li>
<li><p>Rules for inferring <code>Γ ⊢ A</code> holds. We can use these inference rules to build up proofs of things in our system.</p>
<p>In sequent calculus there are two sorts of inference rules, left and right. A left rule takes a fact that we know and let’s us reason backwards to other things we must know hold. A right rule let’s us take the thing we’re trying to prove and instead prove smaller, simpler things.</p>
<p>More rules will follow in the Twelf code but for a nice example consider the left and right rules for <code>∧</code>,</p>
<pre><code>  Γ, A, B ⊢ C
 ———————————————
  Γ, A ∧ B ⊢ C

 Γ ⊢ A    Γ ⊢ B
 ———————————————
    Γ ⊢ A ∧ B</code></pre>
<p>The left rule says if we know that <code>A ∧ B</code> is true, we can take it apart and try to prove our goal with assumptions that <code>A</code> and <code>B</code> are true. The right rule says to prove that <code>A ∧ B</code> is true we need to prove <code>A</code> is true and <code>B</code> is true. A proof in this system is a true of these rules just like you’d expect in a type theory or natural deduction.</p></li>
</ol>
<p>We also tacitly assume a bunch of boring rules called structural rules about our sequents hold, so that we can freely duplicate, drop and swap assumptions in <code>Γ</code>. For a less awful introduction to sequent calculus Frank Pfenning has some <a href="http://www.cs.cmu.edu/~fp/courses/15317-f09/lectures/09-seqcalc.pdf">good notes</a>.</p>
<p>Now we want to prove a particular (meta)theorem about sequent calculus</p>
<ol type="1">
<li>if <code>Γ ⊢ A</code></li>
<li>and <code>Γ, A ⊢ B</code></li>
<li>then <code>Γ ⊢ B</code></li>
</ol>
<p>This theorem means a couple different things for example, our system is consistent and our system also admits lemmas. As it turns out, proving this theorem is hard. The basic complication is that we don’t know what form either of the first two proofs.</p>
<h2 id="the-twelf-stuff">The Twelf Stuff</h2>
<p>We now formalize our sequent calculus in Twelf. First we declare a type and some constants to represent propositions.</p>
<pre class="twelf"><code>    prop  : type.

    =&gt;    : prop -&gt; prop -&gt; prop. %infix right 4 =&gt;.
    true  : prop.
    false : prop.
    /\    : prop -&gt; prop -&gt; prop. %infix none 5 /\.
    \/    : prop -&gt; prop -&gt; prop. %infix none 5 \/.</code></pre>
<p>Notice here that we use <code>infix</code> to let us write <code>A /\ B =&gt; C</code>. Having specified these we now define what a proof is in this system. This is structured a little differently than you’d be led to believe from the above. We have an explicit type <code>proof</code> which is inhabited by “proof terms” which serve as a nice shortcut to those trees generated by inference rules. Finally, we don’t explicitly represent <code>Γ</code>, instead we have this thing called <code>hyp</code> which is used to represent a hypothesis in <code>Γ</code>. Left rules manipulate use these hypotheses and introduce new ones. Pay attention to <code>/‌\/l</code> and <code>/‌\/r</code> since you’ve seen the handwritten equivalents.</p>
<pre class="twelf"><code>    proof   : type.
    hyp     : type.

    init    : hyp -&gt; proof.

    =&gt;/r    : (hyp -&gt; proof) -&gt; proof.
    =&gt;/l    : (hyp -&gt; proof) -&gt; proof -&gt; hyp -&gt; proof.

    true/r  : proof.

    false/l : hyp -&gt; proof.

    /\/r    : proof -&gt; proof -&gt; proof.
    /\/l    : (hyp -&gt; hyp -&gt; proof) -&gt; hyp -&gt; proof.

    \//r1   : proof -&gt; proof.
    \//r2   : proof -&gt; proof.
    \//l    : (hyp -&gt; proof) -&gt; (hyp -&gt; proof) -&gt; hyp -&gt; proof.</code></pre>
<p>The right rules are at least a little intuitive, the left rules are peculiar. Essentially we have a weird CPS-y feel going on here, to decompose a hypothesis you hand the <code>hyp</code> to the constant along with a continuation which <em>takes the hypotheses you should get out of the decomposition</em>. For example for <code>‌\/</code> we have to right rules (think <code>Left</code> and <code>Right</code>), then one left rule which takes two continuations and one <code>hyp</code> (think <code>either</code>). Finally, that <code>init</code> thing is the only way to actually take a hypothesis and use it as a proof.</p>
<p>We now want to unite these two pieces of syntax with a typing judgment letting us say that a <code>proof</code> proves some particular <code>prop</code>.</p>
<pre class="twelf"><code>    of         : proof -&gt; prop -&gt; type.
    hof        : hyp   -&gt; prop -&gt; type.

    of/init    : of (init H) A
                  &lt;- hof H A.

    of/=&gt;/r    : of (=&gt;/r F) (A =&gt; B)
                  &lt;- ({h} hof h A -&gt; of (F h) B).
    of/=&gt;/l    : of (=&gt;/l C Arg F) U
                  &lt;- hof F (A =&gt; B)
                  &lt;- of Arg A
                  &lt;- ({h} hof h B -&gt; of (C h) U).

    of/true/r  : of true/r true.

    of/false/l : of (false/l H) A
                  &lt;- hof H false.

    of//\/r    : of (/\/r R L) (A /\ B)
                  &lt;- of L A
                  &lt;- of R B.
    of//\/l    : of (/\/l C H) U
                  &lt;- hof H (A /\ B)
                  &lt;- ({h}{h&#39;} hof h A -&gt; hof h&#39; B -&gt; of (C h h&#39;) U).

    of/\//r1   : of (\//r1 L) (A \/ B)
                  &lt;- of L A.
    of/\//r2   : of (\//r2 R) (A \/ B)
                  &lt;- of R B.
    of/\//l    : of (\//l R L H) C
                  &lt;- hof H (A \/ B)
                  &lt;- ({h} hof h A -&gt; of (L h) C)
                  &lt;- ({h} hof h B -&gt; of (R h) C).</code></pre>
<p>In order to handle hypotheses we have this <code>hof</code> judgment which handles typing various <code>hyp</code>s. We introduce it just like we introduce <code>hyp</code>s in those continuation-y things for left rules. Sorry for dumping so much code on you all at once: it’s just a lot of machinery we need to get working in order to actually start formalizing cut.</p>
<p>I would like to point out a few things about this formulation of sequent calculus though. First off, it’s very Twelf-y, we use the LF context to host the context of our logic using HOAS. We also basically just have <code>void</code> as the type of hypothesis! Look, there’s no way to construct a hypothesis, let alone a typing derivation <code>hof</code>! The idea is that we’ll just wave our hands at Twelf and say &quot;consider our theorem in a context with <code>hyp</code>s and <code>hof</code>s with</p>
<pre class="twelf"><code>    %block someHofs : some {A : prop} block {h : hyp}{deriv : hof h A}.</code></pre>
<p>In short, Twelf is nifty.</p>
<h2 id="the-theorem">The Theorem</h2>
<p>Now we’re almost in a position to state cut admissibility, we want to say something like</p>
<pre class="twelf"><code>    cut : of Lemma A
            -&gt; ({h} hyp h A -&gt; of (F h) B)
            -&gt; of ??? B</code></pre>
<p>But what should that ??? be? We could just say “screw it it’s something” and leave it as an output of this lemma but experimentally (an hour of teeth gnashing later) it’s absolutely not worth the pain. Instead let’s do something clever.</p>
<p>Let’s first define an untyped version of <code>cut</code> which works just across proofs without mind to typing derivations. We can’t declare this total because it’s just not going to work for ill-typed things, we can give it a mode though (it’s not needed) just as mechanical documentation.</p>
<pre class="twelf"><code>    cut : proof -&gt; (hyp -&gt; proof) -&gt; proof -&gt; type.
    %mode cut +A +B -C.</code></pre>
<p>The goal here is we’re going to state our main theorem as</p>
<pre class="twelf"><code>    of/cut : {A} of P A
              -&gt; ({h} hof h A -&gt; of (F h) B)
              -&gt; cut P F C
              -&gt; of C B
              -&gt; type.
    %mode of/cut +A +B +C -D -E.</code></pre>
<p>Leaving that derivation of <code>cut</code> as an output. This let’s us produce not just a random term but instead a proof that that term makes “sense” somehow along with a proof that it’s well typed.</p>
<p><code>cut</code> is going to mirror the structure of <code>of/cut</code> so we now need to figure out how we’re going to structure our proof. It turns out a rather nice way to do this is to organize our cuts into 4 categories. The first one are “principle” cuts, they’re the ones where we have a right rule as our lemma and we immediately decompose that lemma in the other term with the corresponding left rule. This is sort of the case that we drive towards everywhere and it’s where the substitution bit happens.</p>
<p>First we have some simple cases</p>
<pre class="twelf"><code>    trivial : cut P&#39; ([x] P) P.
    p/init1 : cut (init H) ([x] P x) (P H).
    p/init2 : cut P ([x] init x) P.</code></pre>
<p>In <code>trivial</code> we don’t use the hypothesis at all so we’re just “strengthening” here. <code>p/init1</code> and <code>p/init2</code> deal with the <code>init</code> rule on the left or right side of the cut, if it’s on the left we have a hypothesis of the appropriate type so we just apply the function. If it’s on the left we have a proof of the appropriate type so we just return that. In the more interesting cases we have the principle cuts for some specific connectives.</p>
<pre class="twelf"><code>    p/=&gt;   : cut (=&gt;/r F) ([x] =&gt;/l ([y] C y x) (Arg x) x) Out&#39;
              &lt;- ({y} cut (=&gt;/r F) (C y) (C&#39; y))
              &lt;- cut (=&gt;/r F) Arg Arg&#39;
              &lt;- cut Arg&#39; F Out
              &lt;- cut Out C&#39; Out&#39;.
    p//\   : cut (/\/r R L) ([x] /\/l ([y][z] C y z x) x) Out&#39;
              &lt;- ({x}{y} cut (/\/r R L) (C x y) (C&#39; x y))
              &lt;- ({x} cut R (C&#39; x) (Out x))
              &lt;- cut L Out Out&#39;.
    p/\//1 : cut (\//r1 L) ([x] \//l ([y] C2 y x) ([y] C1 y x) x) Out
              &lt;- ({x} cut (\//r1 L) (C1 x) (C1&#39; x))
              &lt;- cut L C1&#39; Out.
    p/\//2 : cut (\//r2 L) ([x] \//l ([y] C2 y x) ([y] C1 y x) x) Out
              &lt;- ({x} cut (\//r2 L) (C2 x) (C2&#39; x))
              &lt;- cut L C2&#39; Out.</code></pre>
<p>Let’s take a closer look at <code>p/=&gt;</code>, the principle cut for <code>=&gt;</code>. First off, our inputs are <code>=&gt;/r F</code> and <code>([x] =&gt;/l ([y] C y x) (Arg x) x)</code>. The first one is just a “function” that we’re supposed to substitute into the second. Now the second is comprised of a continuation and an argument. Notice that both of these depend on <code>x</code>! In order to handle this the first two lines of the proof</p>
<pre class="twelf"><code>           &lt;- ({y} cut (=&gt;/r F) (C y) (C&#39; y))
           &lt;- cut (=&gt;/r F) Arg Arg&#39;</code></pre>
<p>Are to remove that dependence. We get back a <code>C'</code> and an <code>Arg'</code> which doesn’t use the <code>hyp</code> (<code>x</code>). In order to do this we just recurse and cut the <code>=&gt;/r F</code> out of them. Notice that both the type and the thing we’re substituting are the same size, what decreases here is what we’re substituting into. Now we’re ready to actually do some work. First we need to get a term representing the application of <code>F</code> to <code>Arg'</code>. This is done with cut since it’s just substitution</p>
<pre class="twelf"><code>              &lt;- cut Arg&#39; F Out</code></pre>
<p>But this isn’t enough, we don’t need the result of the application, we need the result of the continuation! So we have to cut the output of the application through the continuation</p>
<pre class="twelf"><code>              &lt;- cut Out C&#39; Out&#39;.</code></pre>
<p>This code is kinda complicated. The typed version of this took me an hour since after 2am I am charitably called an idiot. However this same general pattern holds with all the principle cuts</p>
<ol type="1">
<li>The subterms of the target could depend on what we’re substituting for, so get rid of that dependence with a recursive call.</li>
<li>Get the “result”, which is just the term corresponding to the hypothesis the continuation is expecting. This is pretty trivial in all cases except <code>=&gt;</code> since it’s just lying about in an input.</li>
<li>Get the actual result by cutting 2. through the continuation.</li>
</ol>
<p>Try to work through the case for <code>/\</code> now</p>
<pre class="twelf"><code>    p//\   : cut (/\/r R L) ([x] /\/l ([y][z] C y z x) x) Out&#39;
              &lt;- ({x}{y} cut (/\/r R L) (C x y) (C&#39; x y))
              &lt;- ({x} cut R (C&#39; x) (Out x))
              &lt;- cut L Out Out&#39;.</code></pre>
<p>After principle cuts we really just have a number of boring cases whose job it is to recurse. The first of these is called rightist substitution because it comes up if the term on the right (the part <em>using</em> the lemma) has a right rule first. This means we have to hunt in the subterms to go find where we’re actually using the lemma.</p>
<pre class="twelf"><code>    r/=&gt; : cut P ([x] (=&gt;/r ([y] F y x))) (=&gt;/r ([y] F&#39; y))
            &lt;- ({x} cut P (F x) (F&#39; x)).
    r/true : cut P ([x] true/r) true/r.
    r//\ : cut P ([x] /\/r (R x) (L x)) (/\/r R&#39; L&#39;)
            &lt;- cut P L L&#39;
            &lt;- cut P R R&#39;.
    r/\/1 : cut P ([x] \//r1 (L x)) (\//r1 L&#39;)
             &lt;- cut P L L&#39;.
    r/\/2 : cut P ([x] \//r2 (R x)) (\//r2 R&#39;)
             &lt;- cut P R R&#39;.</code></pre>
<p>Nothing here should be surprising keeping in mind that all we’re doing here is recursing. The next set of cuts is called leftist substitution. Here we are actually recursing on the term we’re trying to substitute.</p>
<pre class="twelf"><code>    l/=&gt;    : cut (=&gt;/l ([y] C y) Arg H) ([x] P x) (=&gt;/l ([x] C&#39; x) Arg H)
               &lt;- ({x} cut (C x) P (C&#39; x)).
    l/false : cut (false/l H) P (false/l H).
    l//\    : cut (/\/l ([x][y] C x y) H) P (/\/l ([x][y] C&#39; x y) H)
               &lt;- ({x}{y} cut (C x y) P (C&#39; x y)).
    l/\/    : cut (\//l ([y] R  y) ([y] L  y) H) ([x] P x)
                  (\//l ([y] R&#39; y) ([y] L&#39; y) H)
               &lt;- ({x} cut (L x) P (L&#39; x))
               &lt;- ({x} cut (R x) P (R&#39; x)).</code></pre>
<p>It’s the same game but just a different target, we’re now recursing on the continuation because that’s where we somehow created a proof of <code>A</code>. This means that on <code>l/=&gt;</code> we’re substation left term which has three parts</p>
<ol type="1">
<li>A continuation, <code>hyp B</code> to <code>proof</code> of <code>C</code></li>
<li>An argument of type <code>A</code></li>
<li>A hypothesis of type <code>A -&gt; B</code></li>
</ol>
<p>Now we’re only interesting in how we created that proof of <code>C</code>, that’s the only relevant part of this substitution. The output of this case is going to have that left rule, <code>=&gt;/l ??? Arg H</code> so we have where <code>???</code> is a replacement of <code>C</code> that we get by cutting <code>C</code> through <code>P</code> “pointwise”. This comes through on the recursive call</p>
<pre class="twelf"><code>               &lt;- ({x} cut (C x) P (C&#39; x)).</code></pre>
<p>For one more case, consider the left rule for <code>\/</code></p>
<pre class="twelf"><code>    l/\/    : cut (\//l R L H) P</code></pre>
<p>We start by trying to cut a left rule into <code>P</code> so we need to produce a left rule in the output with different continuations, something like</p>
<pre class="twelf"><code>               (\//l R&#39; L&#39; H)</code></pre>
<p>Now what should <code>R'</code> and <code>L'</code> be? In order to produce them we’ll throw up a pi so we can get <code>L x</code>, a proof with the appropriate type to cut again. With that, we can recurse and get back the new continuation we want.</p>
<pre class="twelf"><code>               &lt;- ({x} cut (L x) P (L&#39; x))
               &lt;- ({x} cut (R x) P (R&#39; x)).</code></pre>
<p>There’s one last class of cuts to worry about, think about the cases we’ve covered so far</p>
<ol type="1">
<li>The left term is a left rule (leftist substitution)</li>
<li>The right term is a right rule (rightist substitution)</li>
<li>The terms match up and we substitute</li>
</ol>
<p>So what happens if we have a left rule on the right and a right rule on the left, but they don’t “match up”. By this I mean that the left rule in that right term works on a different hypothesis than the one that the function it’s wrapped in provides. In this case we just have to recurse some more</p>
<pre class="twelf"><code>    lr/=&gt; : cut P ([x] =&gt;/l ([y] C y x) (Arg x) H) (=&gt;/l C&#39; Arg&#39; H)
             &lt;- cut P Arg Arg&#39;
             &lt;- ({y} cut P (C y) (C&#39; y)).
    lr//\ : cut P ([x] /\/l ([y][z] C y z x) H) (/\/l C&#39; H)
             &lt;- ({y}{z} cut P (C y z) (C&#39; y z)).
    lr/\/ : cut P ([x] \//l ([y] R y x) ([y] L y x) H) (\//l R&#39; L&#39; H)
             &lt;- ({y} cut P (L y) (L&#39; y))
             &lt;- ({y} cut P (R y) (R&#39; y)).</code></pre>
<p>When we have such an occurrence we just do like we did with right rules.</p>
<p>Okay, now that we’ve handled all of these cases we’re ready to type the damn thing.</p>
<pre class="twelf"><code>    of/cut : {A} of P A
              -&gt; ({h} hof h A -&gt; of (F h) B)
              -&gt; cut P F C
              -&gt; of C B
              -&gt; type.
    %mode of/cut +A +B +C -D -E.</code></pre>
<p>Honestly this is less exciting than you’d think. We’ve really done all the creative work in constructing the <code>cut</code> type family. All that’s left to do is check that this is correct. As an example, here’s a case that exemplifies how we verify all left-right commutative cuts.</p>
<pre class="twelf"><code>    - : of/cut _ P ([x][h] of/=&gt;/l ([y][yh] C y yh x h) (A x h) H)
         (lr/=&gt; CC CA) (of/=&gt;/l C&#39; A&#39; H)
         &lt;- of/cut _ P A CA A&#39;
         &lt;- ({y}{yh} of/cut _ P (C y yh) (CC y) (C&#39; y yh)).</code></pre>
<p>We start by trying to show that</p>
<pre class="twelf"><code>    lr/=&gt; : cut P ([x] =&gt;/l ([y] C y x) (Arg x) H) (=&gt;/l C&#39; Arg&#39; H)
             &lt;- cut P Arg Arg&#39;
             &lt;- ({y} cut P (C y) (C&#39; y)).</code></pre>
<p>Is type correct. To do this we have a derivation <code>P</code> that the left term is well-typed. Notice that I’ve overloaded <code>P</code> here, in the rule <code>lr/=&gt;</code> <code>P</code> was a term and now it’s a typing derivation for that term. Next we have a typing derivation for <code>[x] =&gt;/l ([y] C y x) (Arg x) H</code>. This is a function which takes two arguments. <code>x</code> is a hypothesis, the same as in <code>lr/=&gt;</code>, however now we have <code>h</code> which is a <code>hof</code> derivation that <code>h</code> has a type. There’s only one way to type a usage of the left rule for <code>=&gt;</code>, with <code>of/=&gt;/l</code> so we have that next.</p>
<p>Finally, our output is on the next line in two parts. First we have a derivation for <code>cut</code> showing how to construct the “cut out term” in this case. Next we have a new typing derivation that again uses <code>of/=&gt;/l</code>. Notice that both of these depend on some terms we get from the recursive calls here.</p>
<p>Since we’ve gone through all the cases already and done all the thinking, I’m not going to reproduce it all here. The intuition for how cut works is really best given by the untyped version with the understand that we check that it’s correct with this theorem as we did above.</p>
<h2 id="wrap-up">Wrap Up</h2>
<p>To recap here’s what we did</p>
<ol type="1">
<li>Define sequent calculus’ syntax</li>
<li>Define typing rules across it</li>
<li>Define how an untyped version of cut works across it</li>
<li>Validate the correctness of cut by</li>
</ol>
<p>Hope that was a little fun, cheers!</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Mon, 29 Jun 2015 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2015-06-29-cut.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Examining Hackage: pipes</title>
    <link>http://jozefg.bitbucket.org/posts/2015-06-01-pipes.html</link>
    <description><![CDATA[<div class="info">
    Posted on June  1, 2015
    
</div>
<div class="info">
    
    Tags: <a href="/tags/haskell.html">haskell</a>
    
</div>

<p>It’s been a while since I did one of these “read a package and write about it” posts. Part of this is that it turns out that most software is awful and writing about code I read just makes me grumpy. However I found something nice to write about! In this post I’d like to close a somewhat embarrassing gap in my knowledge: we’re going to walk through streaming library.</p>
<p>I know that both lists and lazy-IO are kind of.. let’s say fragile but have neglected learning one of these fancy libraries that aim to solve those problems. Today we’ll be looking at one of those libraries, pipes!</p>
<p>Pipes provides one core type <code>Proxy</code> and a few operations on it, like <code>await</code> and <code>yield</code>. We can pair together a pipeline of operations which can send data to their neighbors and request more data from them as they need them. With these coroutine like structures we can nicely implement efficient, streaming computations.</p>
<h2 id="getting-the-code">Getting The Code</h2>
<p>As always this starts by getting our hands on the code with the</p>
<pre><code>~ $ cabal get pipes
~ $ cd pipes-4.1.5/</code></pre>
<p>Now from here we can query all the available files to see what we’re up against</p>
<pre><code>~/pipes-4.1.5 $ wc -l **/*.hs | sort -nr
  4796 total
  1513 src/Pipes/Tutorial.hs
   854 src/Pipes/Core.hs
   836 src/Pipes/Prelude.hs
   517 src/Pipes.hs
   380 src/Pipes/Lift.hs
   272 tests/Main.hs
   269 src/Pipes/Internal.hs
    85 benchmarks/PreludeBench.hs
    68 benchmarks/LiftBench.hs
     2 Setup.hs</code></pre>
<p>So the first thing I notice is that there’s this great honking module called <code>Pipes.Tutorial</code> which houses a brief introduction to the pipes package. I skimmed this before starting but it doesn’t really seem to explain the implementation details.. If you don’t really know what pipes is, <a href="http://hackage.haskell.org/package/pipes-4.1.5/docs/Pipes-Tutorial.html">read this tutorial now</a>. After doing so you have exactly my knowledge of pipes!</p>
<p>The next interesting module here is <code>Pipes.Internal</code>. I’ve found that <code>.Internal</code> modules seem to house the fundamental bits of the package so we’ll start there.</p>
<h2 id="pipes.internal">Pipes.Internal</h2>
<p>This module starts with an emphatic warning</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb3-1" data-line-number="1">    <span class="co">{-| This is an internal module, meaning that it is unsafe to</span></a>
<a class="sourceLine" id="cb3-2" data-line-number="2"><span class="co">        import unless you understand the risks. -}</span></a></code></pre></div>
<p>So this seems like a perfect place to start without really understanding this library :D It exports a few different functions and one type:</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" data-line-number="1">    <span class="kw">module</span> <span class="dt">Pipes.Internal</span> (</a>
<a class="sourceLine" id="cb4-2" data-line-number="2">        <span class="co">-- * Internal</span></a>
<a class="sourceLine" id="cb4-3" data-line-number="3">          <span class="dt">Proxy</span>(<span class="fu">..</span>)</a>
<a class="sourceLine" id="cb4-4" data-line-number="4">        , unsafeHoist</a>
<a class="sourceLine" id="cb4-5" data-line-number="5">        , observe</a>
<a class="sourceLine" id="cb4-6" data-line-number="6">        , <span class="dt">X</span></a>
<a class="sourceLine" id="cb4-7" data-line-number="7">        , closed</a>
<a class="sourceLine" id="cb4-8" data-line-number="8">        ) <span class="kw">where</span></a></code></pre></div>
<p>I recognize one of those types: <code>Proxy</code> as the central type behind the whole pipes concept, it is the type of component in the pipe line. Let’s look at how it’s actually implemented</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb5-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">Proxy</span> a&#39; a b&#39; b m r</a>
<a class="sourceLine" id="cb5-2" data-line-number="2">        <span class="fu">=</span> <span class="dt">Request</span> a&#39; (a  <span class="ot">-&gt;</span> <span class="dt">Proxy</span> a&#39; a b&#39; b m r )</a>
<a class="sourceLine" id="cb5-3" data-line-number="3">        <span class="fu">|</span> <span class="dt">Respond</span> b  (b&#39; <span class="ot">-&gt;</span> <span class="dt">Proxy</span> a&#39; a b&#39; b m r )</a>
<a class="sourceLine" id="cb5-4" data-line-number="4">        <span class="fu">|</span> <span class="dt">M</span>          (m    (<span class="dt">Proxy</span> a&#39; a b&#39; b m r))</a>
<a class="sourceLine" id="cb5-5" data-line-number="5">        <span class="fu">|</span> <span class="dt">Pure</span>    r</a></code></pre></div>
<p>So two of those constructors, <code>M</code> and <code>Pure</code>, look pretty vanilla. The first one let’s us lift an action in the underlying monad <code>m</code>, into <code>Proxy</code>. It’s a little bit weird instead of having <code>M (m r)</code> we instead have <code>M (m (Proxy ...))</code> however this doesn’t seem like a big deal because we have <code>Pure</code> to promote an <code>r</code> to a <code>Proxy .... r</code>. So we can lift some <code>m r</code> to <code>Proxy a' a b' b m r</code> with <code>M . fmap Pure</code>. It’s still not clear to me why this is a benefit though.</p>
<p>The first two constructors are really cool though, <code>Request</code> and <code>Respond</code>. The first thing that pops into my head is that this looks like a sort of free-monad pattern. Look how we’ve got</p>
<ol type="1">
<li>The piece of data a user should input to an action (and <code>Request</code> and <code>Respond</code> are definitely actions)</li>
<li>This continuation for a second argument which takes a term of the type returned by the action to another piece of pipe</li>
</ol>
<p>This would make a lot of sense, free monad transformers nicely give rise to coroutines which are very much in line with pipes. Because of this free monad like shape, I expect that the monad instance will be like free monads and behave “like substitution”. We should chase down the leaves of this <code>Proxy</code> (including under lambdas) and replace each <code>Pure r</code> with <code>f r</code> for <code>&gt;&gt;=</code> and <code>Pure (f a)</code> for <code>fmap</code>.</p>
<p>To check if we’re right, we go down one line</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb6-1" data-line-number="1">    <span class="kw">instance</span> <span class="dt">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Functor</span> (<span class="dt">Proxy</span> a&#39; a b&#39; b m) <span class="kw">where</span></a>
<a class="sourceLine" id="cb6-2" data-line-number="2">        fmap f p0 <span class="fu">=</span> go p0 <span class="kw">where</span></a>
<a class="sourceLine" id="cb6-3" data-line-number="3">            go p <span class="fu">=</span> <span class="kw">case</span> p <span class="kw">of</span></a>
<a class="sourceLine" id="cb6-4" data-line-number="4">                <span class="dt">Request</span> a&#39; fa  <span class="ot">-&gt;</span> <span class="dt">Request</span> a&#39; (\a  <span class="ot">-&gt;</span> go (fa  a ))</a>
<a class="sourceLine" id="cb6-5" data-line-number="5">                <span class="dt">Respond</span> b  fb&#39; <span class="ot">-&gt;</span> <span class="dt">Respond</span> b  (\b&#39; <span class="ot">-&gt;</span> go (fb&#39; b&#39;))</a>
<a class="sourceLine" id="cb6-6" data-line-number="6">                <span class="dt">M</span>          m   <span class="ot">-&gt;</span> <span class="dt">M</span> (m <span class="fu">&gt;&gt;=</span> \p&#39; <span class="ot">-&gt;</span> return (go p&#39;))</a>
<a class="sourceLine" id="cb6-7" data-line-number="7">                <span class="dt">Pure</span>    r      <span class="ot">-&gt;</span> <span class="dt">Pure</span> (f r)</a></code></pre></div>
<p>This looks like what I had in mind, we run down <code>p</code> and in the first 3 branches we recurse. I’ll admit it looks a little intimidating but after staring at it for a bit I realized that the first 3 lines are all just variations on <code>fmap go</code>! Indeed, we can rewrite this to</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb7-1" data-line-number="1">            go p <span class="fu">=</span> <span class="kw">case</span> p <span class="kw">of</span></a>
<a class="sourceLine" id="cb7-2" data-line-number="2">                <span class="dt">Request</span> a&#39; fa  <span class="ot">-&gt;</span> <span class="dt">Request</span> a&#39; (fmap go fa)</a>
<a class="sourceLine" id="cb7-3" data-line-number="3">                <span class="dt">Respond</span> b  fb&#39; <span class="ot">-&gt;</span> <span class="dt">Respond</span> b  (fmap go fb&#39;)</a>
<a class="sourceLine" id="cb7-4" data-line-number="4">                <span class="dt">M</span>          m   <span class="ot">-&gt;</span> <span class="dt">M</span> (fmap go m)</a>
<a class="sourceLine" id="cb7-5" data-line-number="5">                <span class="dt">Pure</span>    r      <span class="ot">-&gt;</span> <span class="dt">Pure</span> (f r)</a></code></pre></div>
<p>This makes the idea a bit clearer in my mind. Let’s look at the applicative instance next!</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb8-1" data-line-number="1">    <span class="kw">instance</span> <span class="dt">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Applicative</span> (<span class="dt">Proxy</span> a&#39; a b&#39; b m) <span class="kw">where</span></a>
<a class="sourceLine" id="cb8-2" data-line-number="2">        pure      <span class="fu">=</span> <span class="dt">Pure</span></a>
<a class="sourceLine" id="cb8-3" data-line-number="3">        pf <span class="fu">&lt;*&gt;</span> px <span class="fu">=</span> go pf <span class="kw">where</span></a>
<a class="sourceLine" id="cb8-4" data-line-number="4">            go p <span class="fu">=</span> <span class="kw">case</span> p <span class="kw">of</span></a>
<a class="sourceLine" id="cb8-5" data-line-number="5">                <span class="dt">Request</span> a&#39; fa  <span class="ot">-&gt;</span> <span class="dt">Request</span> a&#39; (\a  <span class="ot">-&gt;</span> go (fa  a ))</a>
<a class="sourceLine" id="cb8-6" data-line-number="6">                <span class="dt">Respond</span> b  fb&#39; <span class="ot">-&gt;</span> <span class="dt">Respond</span> b  (\b&#39; <span class="ot">-&gt;</span> go (fb&#39; b&#39;))</a>
<a class="sourceLine" id="cb8-7" data-line-number="7">                <span class="dt">M</span>          m   <span class="ot">-&gt;</span> <span class="dt">M</span> (m <span class="fu">&gt;&gt;=</span> \p&#39; <span class="ot">-&gt;</span> return (go p&#39;))</a>
<a class="sourceLine" id="cb8-8" data-line-number="8">                <span class="dt">Pure</span>    f      <span class="ot">-&gt;</span> fmap f px</a>
<a class="sourceLine" id="cb8-9" data-line-number="9">        (<span class="fu">*&gt;</span>) <span class="fu">=</span> (<span class="fu">&gt;&gt;</span>)</a></code></pre></div>
<p>First note that <code>pure = Pure</code> which isn’t a stunner just from the naming. In <code>&lt;*&gt;</code> we have the same sort of pattern as in <code>fmap</code>. We race down the “function” side of <code>&lt;*&gt;</code> and whenever we reach a <code>Pure</code> we have a function from <code>a -&gt; b</code>, with that function we call <code>fmap</code> on the structure on the “argument” side. So we’re kind of gluing that <code>px</code> onto that <code>pf</code> by changing each <code>Pure f</code> to <code>fmap f px</code>.</p>
<p>Finally we have the monad instance. Of course the <code>return</code> implementation is the same as for <code>pure</code> but <code>(&gt;&gt;=) = _bind</code> so the implementation of <code>_bind</code> has been chucked out of the instance itself. It turns out there’s a good reason for that: <code>_bind</code> has a bunch of rewrite rules attached to it.</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb9-1" data-line-number="1">    _bind</a>
<a class="sourceLine" id="cb9-2" data-line-number="2"><span class="ot">        ::</span> <span class="dt">Monad</span> m</a>
<a class="sourceLine" id="cb9-3" data-line-number="3">        <span class="ot">=&gt;</span> <span class="dt">Proxy</span> a&#39; a b&#39; b m r</a>
<a class="sourceLine" id="cb9-4" data-line-number="4">        <span class="ot">-&gt;</span> (r <span class="ot">-&gt;</span> <span class="dt">Proxy</span> a&#39; a b&#39; b m r&#39;)</a>
<a class="sourceLine" id="cb9-5" data-line-number="5">        <span class="ot">-&gt;</span> <span class="dt">Proxy</span> a&#39; a b&#39; b m r&#39;</a>
<a class="sourceLine" id="cb9-6" data-line-number="6">    p0 <span class="ot">`_bind`</span> f <span class="fu">=</span> go p0 <span class="kw">where</span></a>
<a class="sourceLine" id="cb9-7" data-line-number="7">        go p <span class="fu">=</span> <span class="kw">case</span> p <span class="kw">of</span></a>
<a class="sourceLine" id="cb9-8" data-line-number="8">            <span class="dt">Request</span> a&#39; fa  <span class="ot">-&gt;</span> <span class="dt">Request</span> a&#39; (\a  <span class="ot">-&gt;</span> go (fa  a ))</a>
<a class="sourceLine" id="cb9-9" data-line-number="9">            <span class="dt">Respond</span> b  fb&#39; <span class="ot">-&gt;</span> <span class="dt">Respond</span> b  (\b&#39; <span class="ot">-&gt;</span> go (fb&#39; b&#39;))</a>
<a class="sourceLine" id="cb9-10" data-line-number="10">            <span class="dt">M</span>          m   <span class="ot">-&gt;</span> <span class="dt">M</span> (m <span class="fu">&gt;&gt;=</span> \p&#39; <span class="ot">-&gt;</span> return (go p&#39;))</a>
<a class="sourceLine" id="cb9-11" data-line-number="11">            <span class="dt">Pure</span>    r      <span class="ot">-&gt;</span> f r</a></code></pre></div>
<p>Now excitingly the implementation of <code>bind</code> is almost exactly what we had before! Now instead of <code>Pure f -&gt; fmap f px</code> it’s <code>Pure r -&gt; f r</code> so we have something more like substitution than gluing.</p>
<p>Now that <code>Proxy</code> is a monad, we can make it a monad transformer!</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb10-1" data-line-number="1">    <span class="kw">instance</span> <span class="dt">MonadTrans</span> (<span class="dt">Proxy</span> a&#39; a b&#39; b) <span class="kw">where</span></a>
<a class="sourceLine" id="cb10-2" data-line-number="2">        lift m <span class="fu">=</span> <span class="dt">M</span> (m <span class="fu">&gt;&gt;=</span> \r <span class="ot">-&gt;</span> return (<span class="dt">Pure</span> r))</a></code></pre></div>
<p>So we need to take an <code>m a</code> and return <code>Proxy a' a b' b m a</code>, we want to use <code>M :: m (Proxy a' a b' b m a)</code> but we have an <code>m a</code>, by <code>fmap</code>ing <code>Pure</code> we’re good to go.</p>
<p>From here on out it’s just a series of not so exciting MTL instances so we’ll skip those.. There’s a couple interesting things left though! Before we get to them recall the monad transformer laws</p>
<ol type="1">
<li><code>return  = lift . return</code></li>
<li><code>m &gt;&gt;= f = lift m &gt;&gt;= (lift . f)</code></li>
</ol>
<p>In other words, <code>lift</code> should “commute” with the two operations of the monad type class. This isn’t actually true by default with <code>Proxy</code>, for example</p>
<pre><code>return a = Pure a
lift (return a) = M (fmap Pure (return a)) = M (return (Pure a))</code></pre>
<p>To solve this we have <code>observe</code>. This function is supposed to normalize a <code>Proxy</code> so that these laws hold.</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb12-1" data-line-number="1"><span class="ot">    observe ::</span> <span class="dt">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Proxy</span> a&#39; a b&#39; b m r <span class="ot">-&gt;</span> <span class="dt">Proxy</span> a&#39; a b&#39; b m r</a>
<a class="sourceLine" id="cb12-2" data-line-number="2">    observe p0 <span class="fu">=</span> <span class="dt">M</span> (go p0) <span class="kw">where</span></a>
<a class="sourceLine" id="cb12-3" data-line-number="3">        go p <span class="fu">=</span> <span class="kw">case</span> p <span class="kw">of</span></a>
<a class="sourceLine" id="cb12-4" data-line-number="4">            <span class="dt">Request</span> a&#39; fa  <span class="ot">-&gt;</span> return (<span class="dt">Request</span> a&#39; (\a  <span class="ot">-&gt;</span> observe (fa  a )))</a>
<a class="sourceLine" id="cb12-5" data-line-number="5">            <span class="dt">Respond</span> b  fb&#39; <span class="ot">-&gt;</span> return (<span class="dt">Respond</span> b  (\b&#39; <span class="ot">-&gt;</span> observe (fb&#39; b&#39;)))</a>
<a class="sourceLine" id="cb12-6" data-line-number="6">            <span class="dt">M</span>          m&#39;  <span class="ot">-&gt;</span> m&#39; <span class="fu">&gt;&gt;=</span> go</a>
<a class="sourceLine" id="cb12-7" data-line-number="7">            <span class="dt">Pure</span>    r      <span class="ot">-&gt;</span> return (<span class="dt">Pure</span> r)</a></code></pre></div>
<p>Note that <code>go</code> takes a <code>Proxy a' a b' b m r</code> and returns <code>m (Proxy a' a b' b m r)</code>. By doing this, we can route stick everything in <code>m</code> with <code>return</code> except for <code>M m'</code> which we just unwrap and keep going. This means <code>return (Pure a) = go (Pure a)</code> which is what is required for the monad transformer laws to hold.</p>
<p>Finally, the last thing in this file is <code>X</code> which is used to represent the type for communication that cannot happen. So if we have a pipe at the beginning of the pipeline, it shouldn’t be able to ask for input from another pipe.</p>
<div class="sourceCode" id="cb13"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb13-1" data-line-number="1">    <span class="kw">newtype</span> <span class="dt">X</span> <span class="fu">=</span> <span class="dt">X</span> <span class="dt">X</span></a>
<a class="sourceLine" id="cb13-2" data-line-number="2"></a>
<a class="sourceLine" id="cb13-3" data-line-number="3"><span class="ot">    closed ::</span> <span class="dt">X</span> <span class="ot">-&gt;</span> a</a>
<a class="sourceLine" id="cb13-4" data-line-number="4">    closed (<span class="dt">X</span> x) <span class="fu">=</span> closed x</a></code></pre></div>
<p>And there are no non-bottom expressions which occupy this type so we’re good. Now that we’ve seen the internal implementation of most of <code>Proxy</code> we can go look at the infrastructure pipes provides around this. Again going by the names, now that we’ve covered the internals it makes sense to move onto <code>Pipes.Core</code>.</p>
<h2 id="pipes.core">Pipes.Core</h2>
<p><code>Pipes.Core</code> seems much closer to the actual user interface of the library, we can see that it exports a bunch of familiar sounding names:</p>
<div class="sourceCode" id="cb14"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb14-1" data-line-number="1">    <span class="kw">module</span> <span class="dt">Pipes.Core</span> (</a>
<a class="sourceLine" id="cb14-2" data-line-number="2">          <span class="dt">Proxy</span></a>
<a class="sourceLine" id="cb14-3" data-line-number="3">        , runEffect</a>
<a class="sourceLine" id="cb14-4" data-line-number="4">        , respond</a>
<a class="sourceLine" id="cb14-5" data-line-number="5">        , (<span class="fu">/&gt;/</span>)</a>
<a class="sourceLine" id="cb14-6" data-line-number="6">        , (<span class="fu">//&gt;</span>)</a>
<a class="sourceLine" id="cb14-7" data-line-number="7">        , request</a>
<a class="sourceLine" id="cb14-8" data-line-number="8">        , (\<span class="fu">&gt;</span>\)</a>
<a class="sourceLine" id="cb14-9" data-line-number="9">        , (<span class="fu">&gt;</span>\\)</a>
<a class="sourceLine" id="cb14-10" data-line-number="10">        , push</a>
<a class="sourceLine" id="cb14-11" data-line-number="11">        , (<span class="fu">&gt;~&gt;</span>)</a>
<a class="sourceLine" id="cb14-12" data-line-number="12">        , (<span class="fu">&gt;&gt;~</span>)</a>
<a class="sourceLine" id="cb14-13" data-line-number="13">        , pull</a>
<a class="sourceLine" id="cb14-14" data-line-number="14">        , (<span class="fu">&gt;+&gt;</span>)</a>
<a class="sourceLine" id="cb14-15" data-line-number="15">        , (<span class="fu">+&gt;&gt;</span>)</a>
<a class="sourceLine" id="cb14-16" data-line-number="16">        , reflect</a>
<a class="sourceLine" id="cb14-17" data-line-number="17">        , <span class="dt">X</span></a>
<a class="sourceLine" id="cb14-18" data-line-number="18">        , <span class="dt">Effect</span></a>
<a class="sourceLine" id="cb14-19" data-line-number="19">        , <span class="dt">Producer</span></a>
<a class="sourceLine" id="cb14-20" data-line-number="20">        , <span class="dt">Pipe</span></a>
<a class="sourceLine" id="cb14-21" data-line-number="21">        , <span class="dt">Consumer</span></a>
<a class="sourceLine" id="cb14-22" data-line-number="22">        , <span class="dt">Client</span></a>
<a class="sourceLine" id="cb14-23" data-line-number="23">        , <span class="dt">Server</span></a>
<a class="sourceLine" id="cb14-24" data-line-number="24">        , <span class="dt">Effect&#39;</span></a>
<a class="sourceLine" id="cb14-25" data-line-number="25">        , <span class="dt">Producer&#39;</span></a>
<a class="sourceLine" id="cb14-26" data-line-number="26">        , <span class="dt">Consumer&#39;</span></a>
<a class="sourceLine" id="cb14-27" data-line-number="27">        , <span class="dt">Client&#39;</span></a>
<a class="sourceLine" id="cb14-28" data-line-number="28">        , <span class="dt">Server&#39;</span></a>
<a class="sourceLine" id="cb14-29" data-line-number="29">        , (\<span class="fu">&lt;</span>\)</a>
<a class="sourceLine" id="cb14-30" data-line-number="30">        , (<span class="fu">/&lt;/</span>)</a>
<a class="sourceLine" id="cb14-31" data-line-number="31">        , (<span class="fu">&lt;~&lt;</span>)</a>
<a class="sourceLine" id="cb14-32" data-line-number="32">        , (<span class="fu">~&lt;&lt;</span>)</a>
<a class="sourceLine" id="cb14-33" data-line-number="33">        , (<span class="fu">&lt;+&lt;</span>)</a>
<a class="sourceLine" id="cb14-34" data-line-number="34">        , (<span class="fu">&lt;</span>\\)</a>
<a class="sourceLine" id="cb14-35" data-line-number="35">        , (<span class="fu">//&lt;</span>)</a>
<a class="sourceLine" id="cb14-36" data-line-number="36">        , (<span class="fu">&lt;&lt;+</span>)</a>
<a class="sourceLine" id="cb14-37" data-line-number="37">        , closed</a>
<a class="sourceLine" id="cb14-38" data-line-number="38">        ) <span class="kw">where</span></a></code></pre></div>
<p>Now a few of those we’ve seen before, namely <code>Proxy</code>, <code>X</code>, and <code>closed</code>. Notice that <code>Proxy</code> is exported abstractly here so we can’t write code which violates the monad transformer laws using this module.</p>
<p>The first new function is called <code>runEffect</code>, but it has the type</p>
<div class="sourceCode" id="cb15"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb15-1" data-line-number="1">    <span class="dt">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Effect</span> m r <span class="ot">-&gt;</span> m r</a></code></pre></div>
<p>Which sounds great! I however have no clue what an effect is so let’s dig around the type exports first. There are a few type synonyms here</p>
<div class="sourceCode" id="cb16"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb16-1" data-line-number="1">    <span class="kw">type</span> <span class="dt">Effect</span> <span class="fu">=</span> <span class="dt">Proxy</span> <span class="dt">X</span> () () <span class="dt">X</span></a>
<a class="sourceLine" id="cb16-2" data-line-number="2">    <span class="kw">type</span> <span class="dt">Producer</span> b <span class="fu">=</span> <span class="dt">Proxy</span> <span class="dt">X</span> () () b</a>
<a class="sourceLine" id="cb16-3" data-line-number="3">    <span class="kw">type</span> <span class="dt">Pipe</span> a b <span class="fu">=</span> <span class="dt">Proxy</span> () a () b</a>
<a class="sourceLine" id="cb16-4" data-line-number="4">    <span class="kw">type</span> <span class="dt">Consumer</span> a <span class="fu">=</span> <span class="dt">Proxy</span> () a () <span class="dt">X</span></a>
<a class="sourceLine" id="cb16-5" data-line-number="5">    <span class="kw">type</span> <span class="dt">Client</span> a&#39; a <span class="fu">=</span> <span class="dt">Proxy</span> a&#39; a () <span class="dt">X</span></a>
<a class="sourceLine" id="cb16-6" data-line-number="6">    <span class="kw">type</span> <span class="dt">Server</span> b&#39; b <span class="fu">=</span> <span class="dt">Proxy</span> <span class="dt">X</span> () b&#39; b</a>
<a class="sourceLine" id="cb16-7" data-line-number="7">    <span class="kw">type</span> <span class="dt">Effect&#39;</span> m r <span class="fu">=</span> forall x&#39; x y&#39; y <span class="fu">.</span> <span class="dt">Proxy</span> x&#39; x y&#39; y m r</a>
<a class="sourceLine" id="cb16-8" data-line-number="8">    <span class="kw">type</span> <span class="dt">Producer&#39;</span> b m r <span class="fu">=</span> forall x&#39; x <span class="fu">.</span> <span class="dt">Proxy</span> x&#39; x () b m r</a>
<a class="sourceLine" id="cb16-9" data-line-number="9">    <span class="kw">type</span> <span class="dt">Consumer&#39;</span> a m r <span class="fu">=</span> forall y&#39; y <span class="fu">.</span> <span class="dt">Proxy</span> () a y&#39; y m r</a>
<a class="sourceLine" id="cb16-10" data-line-number="10">    <span class="kw">type</span> <span class="dt">Server&#39;</span> b&#39; b m r <span class="fu">=</span> forall x&#39; x <span class="fu">.</span> <span class="dt">Proxy</span> x&#39; x b&#39; b m r</a>
<a class="sourceLine" id="cb16-11" data-line-number="11">    <span class="kw">type</span> <span class="dt">Client&#39;</span> a&#39; a m r <span class="fu">=</span> forall y&#39; y <span class="fu">.</span> <span class="dt">Proxy</span> a&#39; a y&#39; y m r</a></code></pre></div>
<p>Even though this looks like a lot, about half of these are actually duplicates which just use <code>-XRankNTypes</code> instead of explicitly using <code>X</code>. An <code>Effect</code> as seen above is <code>Proxy X () () X</code>.. I had to double check this but proxy takes 6 type arguments, here they are in order</p>
<ol type="1">
<li><code>a'</code> is the type of things that we can send up a <code>Request</code></li>
<li><code>a</code> is the type of things which a request will return</li>
<li><code>b'</code> is what we may be <em>sent</em> to respond to</li>
<li><code>b</code> is what we may respond with</li>
<li><code>m</code> is the underlying monad we may use for effects</li>
<li><code>r</code> is the return value</li>
</ol>
<p>So an <code>Effect</code> can only request things if it can produce an <code>X</code>, and it will get back a <code>()</code> from its requests, and it can only respond with an <code>X</code> and will get back a <code>()</code> after responding. Since we can never produce an <code>X</code> an <code>Effect</code> can never request or respond.</p>
<p>Similarly, a <code>Producer</code> can <code>respond</code> to things with <code>b</code>s, but it will only ever get back a <code>()</code> after a response and it can never <code>request</code> something. A <code>Consumer</code> is the dual, never responding but can <code>request</code>, it can only hand the code responding a <code>()</code> though.</p>
<p>Also in there are <code>Client</code>s and <code>Server</code>s which seem to be like a <code>Consumer</code> and a <code>Producer</code> but that can actually send meaningful messages with a <code>request</code> and receive something interesting with a <code>respond</code> instead of just <code>()</code>.</p>
<p>Okay, with these type synonyms in mind let’s go look at some code! Since an <code>Effect</code> can’t request or respond, it’s really equivalent to just some monadic action.</p>
<div class="sourceCode" id="cb17"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb17-1" data-line-number="1"><span class="ot">    runEffect ::</span> <span class="dt">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Effect</span> m r <span class="ot">-&gt;</span> m r</a>
<a class="sourceLine" id="cb17-2" data-line-number="2">    runEffect <span class="fu">=</span> go</a>
<a class="sourceLine" id="cb17-3" data-line-number="3">      <span class="kw">where</span></a>
<a class="sourceLine" id="cb17-4" data-line-number="4">        go p <span class="fu">=</span> <span class="kw">case</span> p <span class="kw">of</span></a>
<a class="sourceLine" id="cb17-5" data-line-number="5">            <span class="dt">Request</span> v _ <span class="ot">-&gt;</span> closed v</a>
<a class="sourceLine" id="cb17-6" data-line-number="6">            <span class="dt">Respond</span> v _ <span class="ot">-&gt;</span> closed v</a>
<a class="sourceLine" id="cb17-7" data-line-number="7">            <span class="dt">M</span>       m   <span class="ot">-&gt;</span> m <span class="fu">&gt;&gt;=</span> go</a>
<a class="sourceLine" id="cb17-8" data-line-number="8">            <span class="dt">Pure</span>    r   <span class="ot">-&gt;</span> return r</a></code></pre></div>
<p>This let’s us write <code>runEffect</code> which just uses the absurdity of producing a <code>v :: X</code> in order to turn a <code>Proxy</code> into an <code>m</code>.</p>
<p><code>runEffect</code> is also the first function we’ve seen to actually escape the <code>Proxy</code> monad as well! It let’s us convert a self-contained pipeline into just an effect which should mean it comes up basically everywhere, just like <code>runStateT</code>.</p>
<p>Since the <code>Proxy</code> monad is abstract, we need some functions to actually be able to request things. Thus we have <code>respond</code></p>
<div class="sourceCode" id="cb18"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb18-1" data-line-number="1"><span class="ot">    respond ::</span> <span class="dt">Monad</span> m <span class="ot">=&gt;</span> a <span class="ot">-&gt;</span> <span class="dt">Proxy</span> x&#39; x a&#39; a m a&#39;</a>
<a class="sourceLine" id="cb18-2" data-line-number="2">    respond a <span class="fu">=</span> <span class="dt">Respond</span> a <span class="dt">Pure</span></a></code></pre></div>
<p>This is actually pretty trivial, we have a constructor after all whose job it is to <code>Respond</code> to things so we just use that with the <code>a</code> we have as a response. Since we have no interesting continuation yet, but we need something of type <code>a' -&gt; Proxy x' x a' a m a'</code> we just use <code>Pure</code>. This should be very familiar to users of free monads (remember that <code>Pure</code> = <code>return</code>)!</p>
<p>Next is something interesting, we’ve seen a lot of ways of manipulating a pipe, but never actually a way of combining two pipes so that they interact, our next function does that.</p>
<div class="sourceCode" id="cb19"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb19-1" data-line-number="1">    (<span class="fu">/&gt;/</span>)</a>
<a class="sourceLine" id="cb19-2" data-line-number="2"><span class="ot">        ::</span> <span class="dt">Monad</span> m</a>
<a class="sourceLine" id="cb19-3" data-line-number="3">        <span class="ot">=&gt;</span> (a <span class="ot">-&gt;</span> <span class="dt">Proxy</span> x&#39; x b&#39; b m a&#39;)</a>
<a class="sourceLine" id="cb19-4" data-line-number="4">        <span class="ot">-&gt;</span> (b <span class="ot">-&gt;</span> <span class="dt">Proxy</span> x&#39; x c&#39; c m b&#39;)</a>
<a class="sourceLine" id="cb19-5" data-line-number="5">        <span class="ot">-&gt;</span> (a <span class="ot">-&gt;</span> <span class="dt">Proxy</span> x&#39; x c&#39; c m a&#39;)</a>
<a class="sourceLine" id="cb19-6" data-line-number="6">    (fa <span class="fu">/&gt;/</span> fb) a <span class="fu">=</span> fa a <span class="fu">//&gt;</span> fb</a></code></pre></div>
<p>Here we have two arguments, both functions to pipelines and we return a pipeline as output. Notice here that the first <code>Proxy</code> is something which is going to <code>respond</code> with things of type <code>b</code> and expect something of type <code>b'</code> in return and our second function is going to map <code>b</code>s to a <code>Proxy</code> which returns a <code>b'</code>. This means we can replace each <code>Respond</code> in the first with a call to the second function and pipe the output into our continuation for that <code>Respond</code>. Indeed this matches up with the return type so I anticipate that it what shall happen. However, this function shells out to another right below it so we’ll have to look at it to confirm.</p>
<div class="sourceCode" id="cb20"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb20-1" data-line-number="1">    (<span class="fu">//&gt;</span>)</a>
<a class="sourceLine" id="cb20-2" data-line-number="2"><span class="ot">        ::</span> <span class="dt">Monad</span> m</a>
<a class="sourceLine" id="cb20-3" data-line-number="3">        <span class="ot">=&gt;</span>       <span class="dt">Proxy</span> x&#39; x b&#39; b m a&#39;</a>
<a class="sourceLine" id="cb20-4" data-line-number="4">        <span class="ot">-&gt;</span> (b <span class="ot">-&gt;</span> <span class="dt">Proxy</span> x&#39; x c&#39; c m b&#39;)</a>
<a class="sourceLine" id="cb20-5" data-line-number="5">        <span class="ot">-&gt;</span>       <span class="dt">Proxy</span> x&#39; x c&#39; c m a&#39;</a>
<a class="sourceLine" id="cb20-6" data-line-number="6">    p0 <span class="fu">//&gt;</span> fb <span class="fu">=</span> go p0</a>
<a class="sourceLine" id="cb20-7" data-line-number="7">      <span class="kw">where</span></a>
<a class="sourceLine" id="cb20-8" data-line-number="8">        go p <span class="fu">=</span> <span class="kw">case</span> p <span class="kw">of</span></a>
<a class="sourceLine" id="cb20-9" data-line-number="9">            <span class="dt">Request</span> x&#39; fx  <span class="ot">-&gt;</span> <span class="dt">Request</span> x&#39; (\x <span class="ot">-&gt;</span> go (fx x))</a>
<a class="sourceLine" id="cb20-10" data-line-number="10">            <span class="dt">Respond</span> b  fb&#39; <span class="ot">-&gt;</span> fb b <span class="fu">&gt;&gt;=</span> \b&#39; <span class="ot">-&gt;</span> go (fb&#39; b&#39;)</a>
<a class="sourceLine" id="cb20-11" data-line-number="11">            <span class="dt">M</span>          m   <span class="ot">-&gt;</span> <span class="dt">M</span> (m <span class="fu">&gt;&gt;=</span> \p&#39; <span class="ot">-&gt;</span> return (go p&#39;))</a>
<a class="sourceLine" id="cb20-12" data-line-number="12">            <span class="dt">Pure</span>       a   <span class="ot">-&gt;</span> <span class="dt">Pure</span> a</a></code></pre></div>
<p>The interesting line here is <code>Respond b fb' -&gt; ...</code> which does exactly what I thought it ought to (I feel clever). In that line we run the function we have in the second argument with the data the first argument was <code>Respond</code>ing with. We sort of “intercept” a message intended for downstream and just handle it right there. Since we do this for all things <code>Respond</code>ing with <code>b</code>s we now only respond with <code>c</code>s hence the change in type. It doesn’t effect the upstream type, but we can now take something producing values and transform them to instead run some other computation (perhaps producing something else).</p>
<p>In a limited case we can do something like</p>
<div class="sourceCode" id="cb21"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb21-1" data-line-number="1"><span class="ot">    intercept ::</span> <span class="dt">Monad</span> m</a>
<a class="sourceLine" id="cb21-2" data-line-number="2">              <span class="ot">=&gt;</span> (b <span class="ot">-&gt;</span> c)</a>
<a class="sourceLine" id="cb21-3" data-line-number="3">              <span class="ot">-&gt;</span> <span class="dt">Proxy</span> a&#39; a b&#39; b m r</a>
<a class="sourceLine" id="cb21-4" data-line-number="4">              <span class="ot">-&gt;</span> <span class="dt">Proxy</span> a&#39; a b&#39; c m r</a>
<a class="sourceLine" id="cb21-5" data-line-number="5">    intercept f p <span class="fu">=</span> p <span class="fu">//&gt;</span> respond <span class="fu">.</span> f</a></code></pre></div>
<p>Cool! Now up next seems to be the dual of what we’ve just looked at.</p>
<div class="sourceCode" id="cb22"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb22-1" data-line-number="1"><span class="ot">    request ::</span> <span class="dt">Monad</span> m <span class="ot">=&gt;</span> a&#39; <span class="ot">-&gt;</span> <span class="dt">Proxy</span> a&#39; a y&#39; y m a</a>
<a class="sourceLine" id="cb22-2" data-line-number="2">    request a&#39; <span class="fu">=</span> <span class="dt">Request</span> a&#39; <span class="dt">Pure</span></a></code></pre></div>
<p>This is just what we had with <code>respond</code> but using <code>Request</code> instead. Similarly we ahve a counterpart for <code>/&gt;/</code>. It again shells out to a similar, pointful, function <code>&gt;\\</code></p>
<div class="sourceCode" id="cb23"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb23-1" data-line-number="1">    (\<span class="fu">&gt;</span>\)</a>
<a class="sourceLine" id="cb23-2" data-line-number="2"><span class="ot">        ::</span> <span class="dt">Monad</span> m</a>
<a class="sourceLine" id="cb23-3" data-line-number="3">        <span class="ot">=&gt;</span> (b&#39; <span class="ot">-&gt;</span> <span class="dt">Proxy</span> a&#39; a y&#39; y m b)</a>
<a class="sourceLine" id="cb23-4" data-line-number="4">        <span class="ot">-&gt;</span> (c&#39; <span class="ot">-&gt;</span> <span class="dt">Proxy</span> b&#39; b y&#39; y m c)</a>
<a class="sourceLine" id="cb23-5" data-line-number="5">        <span class="ot">-&gt;</span> (c&#39; <span class="ot">-&gt;</span> <span class="dt">Proxy</span> a&#39; a y&#39; y m c)</a>
<a class="sourceLine" id="cb23-6" data-line-number="6">    (fb&#39; \<span class="fu">&gt;</span>\ fc&#39;) c&#39; <span class="fu">=</span> fb&#39; <span class="fu">&gt;</span>\\ fc&#39; c&#39;</a>
<a class="sourceLine" id="cb23-7" data-line-number="7"></a>
<a class="sourceLine" id="cb23-8" data-line-number="8">    (<span class="fu">&gt;</span>\\)</a>
<a class="sourceLine" id="cb23-9" data-line-number="9"><span class="ot">        ::</span> <span class="dt">Monad</span> m</a>
<a class="sourceLine" id="cb23-10" data-line-number="10">        <span class="ot">=&gt;</span> (b&#39; <span class="ot">-&gt;</span> <span class="dt">Proxy</span> a&#39; a y&#39; y m b)</a>
<a class="sourceLine" id="cb23-11" data-line-number="11">        <span class="ot">-&gt;</span>        <span class="dt">Proxy</span> b&#39; b y&#39; y m c</a>
<a class="sourceLine" id="cb23-12" data-line-number="12">        <span class="ot">-&gt;</span>        <span class="dt">Proxy</span> a&#39; a y&#39; y m c</a>
<a class="sourceLine" id="cb23-13" data-line-number="13">    fb&#39; <span class="fu">&gt;</span>\\ p0 <span class="fu">=</span> go p0</a>
<a class="sourceLine" id="cb23-14" data-line-number="14">      <span class="kw">where</span></a>
<a class="sourceLine" id="cb23-15" data-line-number="15">        go p <span class="fu">=</span> <span class="kw">case</span> p <span class="kw">of</span></a>
<a class="sourceLine" id="cb23-16" data-line-number="16">            <span class="dt">Request</span> b&#39; fb  <span class="ot">-&gt;</span> fb&#39; b&#39; <span class="fu">&gt;&gt;=</span> \b <span class="ot">-&gt;</span> go (fb b)</a>
<a class="sourceLine" id="cb23-17" data-line-number="17">            <span class="dt">Respond</span> x  fx&#39; <span class="ot">-&gt;</span> <span class="dt">Respond</span> x (\x&#39; <span class="ot">-&gt;</span> go (fx&#39; x&#39;))</a>
<a class="sourceLine" id="cb23-18" data-line-number="18">            <span class="dt">M</span>          m   <span class="ot">-&gt;</span> <span class="dt">M</span> (m <span class="fu">&gt;&gt;=</span> \p&#39; <span class="ot">-&gt;</span> return (go p&#39;))</a>
<a class="sourceLine" id="cb23-19" data-line-number="19">            <span class="dt">Pure</span>       a   <span class="ot">-&gt;</span> <span class="dt">Pure</span> a</a></code></pre></div>
<p>I’d expect that this function does sort of what the other did before. It’ll take <code>Request</code>s and “answer” them inline by replacing it with a call to the other function. In fact, when you think about what the hell is the difference between a request and a response? They’re completely symmetric! They both transmit information sending one type in one direction and one type in another. So we should have exactly the same code that just happens to use <code>Request</code> instead of <code>Respond</code>. which is indeed what we have.</p>
<p>The only real difference here is in the argument order which hints at the fact that we’re going to break symmetry sooner or later, it just hasn’t happened yet.</p>
<p>Next up is</p>
<div class="sourceCode" id="cb24"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb24-1" data-line-number="1"><span class="ot">    push ::</span> <span class="dt">Monad</span> m <span class="ot">=&gt;</span> a <span class="ot">-&gt;</span> <span class="dt">Proxy</span> a&#39; a a&#39; a m r</a>
<a class="sourceLine" id="cb24-2" data-line-number="2">    push <span class="fu">=</span> go</a>
<a class="sourceLine" id="cb24-3" data-line-number="3">      <span class="kw">where</span></a>
<a class="sourceLine" id="cb24-4" data-line-number="4">        go a <span class="fu">=</span> <span class="dt">Respond</span> a (\a&#39; <span class="ot">-&gt;</span> <span class="dt">Request</span> a&#39; go)</a></code></pre></div>
<p><code>push</code> takes a seed <code>a</code> and chucks it down the pipeline. Once it gets a response, it throws it up the pipeline with <code>Request</code> and when it gets a response (something of type <code>a</code>) it starts the whole process over again. Now the process starts by sending values down, there’s no reason why we can’t do the reverse and start by asking for a value</p>
<div class="sourceCode" id="cb25"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb25-1" data-line-number="1"><span class="ot">    pull ::</span> <span class="dt">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Proxy</span> a&#39; a a&#39; a m r</a>
<a class="sourceLine" id="cb25-2" data-line-number="2">    pull <span class="fu">=</span> go</a>
<a class="sourceLine" id="cb25-3" data-line-number="3">      <span class="kw">where</span></a>
<a class="sourceLine" id="cb25-4" data-line-number="4">        go a&#39; <span class="fu">=</span> <span class="dt">Request</span> a&#39; (\a <span class="ot">-&gt;</span> <span class="dt">Respond</span> a go)</a></code></pre></div>
<p>which conveniently is right near by. Now <code>push</code> and <code>pull</code> each give rise to a form of composition which takes two <code>Proxy</code>s and glues them together. The first is</p>
<div class="sourceCode" id="cb26"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb26-1" data-line-number="1">    (<span class="fu">&gt;~&gt;</span>)</a>
<a class="sourceLine" id="cb26-2" data-line-number="2"><span class="ot">        ::</span> <span class="dt">Monad</span> m</a>
<a class="sourceLine" id="cb26-3" data-line-number="3">        <span class="ot">=&gt;</span> (_a <span class="ot">-&gt;</span> <span class="dt">Proxy</span> a&#39; a b&#39; b m r)</a>
<a class="sourceLine" id="cb26-4" data-line-number="4">        <span class="ot">-&gt;</span> ( b <span class="ot">-&gt;</span> <span class="dt">Proxy</span> b&#39; b c&#39; c m r)</a>
<a class="sourceLine" id="cb26-5" data-line-number="5">        <span class="ot">-&gt;</span> (_a <span class="ot">-&gt;</span> <span class="dt">Proxy</span> a&#39; a c&#39; c m r)</a></code></pre></div>
<p>This takes two <code>Proxy</code>s which can communicate with each other and gives back a <code>Proxy</code> which has internalized this dialogue. This shells out to the pointful version, <code>&gt;&gt;~</code></p>
<div class="sourceCode" id="cb27"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb27-1" data-line-number="1">    (<span class="fu">&gt;&gt;~</span>)</a>
<a class="sourceLine" id="cb27-2" data-line-number="2"><span class="ot">        ::</span> <span class="dt">Monad</span> m</a>
<a class="sourceLine" id="cb27-3" data-line-number="3">        <span class="ot">=&gt;</span>       <span class="dt">Proxy</span> a&#39; a b&#39; b m r</a>
<a class="sourceLine" id="cb27-4" data-line-number="4">        <span class="ot">-&gt;</span> (b <span class="ot">-&gt;</span> <span class="dt">Proxy</span> b&#39; b c&#39; c m r)</a>
<a class="sourceLine" id="cb27-5" data-line-number="5">        <span class="ot">-&gt;</span>       <span class="dt">Proxy</span> a&#39; a c&#39; c m r</a>
<a class="sourceLine" id="cb27-6" data-line-number="6">    p <span class="fu">&gt;&gt;~</span> fb <span class="fu">=</span> <span class="kw">case</span> p <span class="kw">of</span></a>
<a class="sourceLine" id="cb27-7" data-line-number="7">        <span class="dt">Request</span> a&#39; fa  <span class="ot">-&gt;</span> <span class="dt">Request</span> a&#39; (\a <span class="ot">-&gt;</span> fa a <span class="fu">&gt;&gt;~</span> fb)</a>
<a class="sourceLine" id="cb27-8" data-line-number="8">        <span class="dt">Respond</span> b  fb&#39; <span class="ot">-&gt;</span> fb&#39; <span class="fu">+&gt;&gt;</span> fb b</a>
<a class="sourceLine" id="cb27-9" data-line-number="9">        <span class="dt">M</span>          m   <span class="ot">-&gt;</span> <span class="dt">M</span> (m <span class="fu">&gt;&gt;=</span> \p&#39; <span class="ot">-&gt;</span> return (p&#39; <span class="fu">&gt;&gt;~</span> fb))</a>
<a class="sourceLine" id="cb27-10" data-line-number="10">        <span class="dt">Pure</span>       r   <span class="ot">-&gt;</span> <span class="dt">Pure</span> r</a></code></pre></div>
<p>For this code we walk down the tree and recurse in all cases except where we have a <code>Response</code>. This should send some information to that function we got as an argument and then use that response to continue, so we want some way of taking a <code>Proxy b' b c' c m r</code> and a <code>b' -&gt; Proxy a' a b' b m r</code> and giving back a <code>Proxy a' a c' c m r</code>. This looks like the exact dual to <code>&gt;&gt;~</code> and indeed is the equivalent in the <code>pull</code> version.</p>
<div class="sourceCode" id="cb28"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb28-1" data-line-number="1">    (<span class="fu">+&gt;&gt;</span>)</a>
<a class="sourceLine" id="cb28-2" data-line-number="2"><span class="ot">        ::</span> <span class="dt">Monad</span> m</a>
<a class="sourceLine" id="cb28-3" data-line-number="3">        <span class="ot">=&gt;</span> (b&#39; <span class="ot">-&gt;</span> <span class="dt">Proxy</span> a&#39; a b&#39; b m r)</a>
<a class="sourceLine" id="cb28-4" data-line-number="4">        <span class="ot">-&gt;</span>        <span class="dt">Proxy</span> b&#39; b c&#39; c m r</a>
<a class="sourceLine" id="cb28-5" data-line-number="5">        <span class="ot">-&gt;</span>        <span class="dt">Proxy</span> a&#39; a c&#39; c m r</a>
<a class="sourceLine" id="cb28-6" data-line-number="6">    fb&#39; <span class="fu">+&gt;&gt;</span> p <span class="fu">=</span> <span class="kw">case</span> p <span class="kw">of</span></a>
<a class="sourceLine" id="cb28-7" data-line-number="7">        <span class="dt">Request</span> b&#39; fb  <span class="ot">-&gt;</span> fb&#39; b&#39; <span class="fu">&gt;&gt;~</span> fb</a>
<a class="sourceLine" id="cb28-8" data-line-number="8">        <span class="dt">Respond</span> c  fc&#39; <span class="ot">-&gt;</span> <span class="dt">Respond</span> c (\c&#39; <span class="ot">-&gt;</span> fb&#39; <span class="fu">+&gt;&gt;</span> fc&#39; c&#39;)</a>
<a class="sourceLine" id="cb28-9" data-line-number="9">        <span class="dt">M</span>          m   <span class="ot">-&gt;</span> <span class="dt">M</span> (m <span class="fu">&gt;&gt;=</span> \p&#39; <span class="ot">-&gt;</span> return (fb&#39; <span class="fu">+&gt;&gt;</span> p&#39;))</a>
<a class="sourceLine" id="cb28-10" data-line-number="10">        <span class="dt">Pure</span>       r   <span class="ot">-&gt;</span> <span class="dt">Pure</span> r</a></code></pre></div>
<p>This does the exact opposite of <code>&gt;&gt;~</code>. It walks around recursing until we get to a <code>Request</code>, this should transfer control up to that function <code>b' -&gt; Proxy ...</code> and it does by calling <code>&gt;&gt;~</code>. So these two operators <code>&gt;&gt;+</code> and <code>&gt;&gt;~</code> work together to join up to <code>Proxy</code> functions by using one to answer the other’s <code>Request</code> and <code>Respond</code>s. The symmetry breaking here is who should we inspect “first” so to speak. If we start with the upstream one than the second one is only run when a value is <code>push</code>-ed down to it and if we start with the former we only run the upstream version when we <code>pull</code> something from it. Nifty.</p>
<p>One thing to note, what happens when one of these <code>Proxy</code>s give up and <code>return</code>? This potential situation is reflected in the fact that both of these <code>Proxy</code>s must return an <code>r</code>. Therefore, whenever one of these returns and we’re currently running it (the upstream for <code>&gt;&gt;~</code>, downstream for <code>&gt;&gt;+</code>) we can just return the value and be done with the whole thing. In this sense composing a <code>Proxy</code> has this short circuiting property, at any point in the pipeline you can just give up and <code>return</code> something!</p>
<p>Remember before how I was ranting about how <code>Request</code> and <code>Respond</code> were really the same damn thing, it turns out I’m not the only one who thought that</p>
<div class="sourceCode" id="cb29"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb29-1" data-line-number="1"><span class="ot">    reflect ::</span> <span class="dt">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Proxy</span> a&#39; a b&#39; b m r <span class="ot">-&gt;</span> <span class="dt">Proxy</span> b b&#39; a a&#39; m r</a>
<a class="sourceLine" id="cb29-2" data-line-number="2">    reflect <span class="fu">=</span> go</a>
<a class="sourceLine" id="cb29-3" data-line-number="3">      <span class="kw">where</span></a>
<a class="sourceLine" id="cb29-4" data-line-number="4">        go p <span class="fu">=</span> <span class="kw">case</span> p <span class="kw">of</span></a>
<a class="sourceLine" id="cb29-5" data-line-number="5">            <span class="dt">Request</span> a&#39; fa  <span class="ot">-&gt;</span> <span class="dt">Respond</span> a&#39; (\a  <span class="ot">-&gt;</span> go (fa  a ))</a>
<a class="sourceLine" id="cb29-6" data-line-number="6">            <span class="dt">Respond</span> b  fb&#39; <span class="ot">-&gt;</span> <span class="dt">Request</span> b  (\b&#39; <span class="ot">-&gt;</span> go (fb&#39; b&#39;))</a>
<a class="sourceLine" id="cb29-7" data-line-number="7">            <span class="dt">M</span>          m   <span class="ot">-&gt;</span> <span class="dt">M</span> (m <span class="fu">&gt;&gt;=</span> \p&#39; <span class="ot">-&gt;</span> return (go p&#39;))</a>
<a class="sourceLine" id="cb29-8" data-line-number="8">            <span class="dt">Pure</span>    r      <span class="ot">-&gt;</span> <span class="dt">Pure</span> r</a></code></pre></div>
<p>Looking at the type here is really telling, all we do to switch the upstream and downstream ends is swap the constructors <code>Request</code> and <code>Respond</code>! That actually wraps up the core of pipes, the rest is just a bunch of synonyms with the arguments flipped!</p>
<p>Now that we’ve finished up <code>Pipes.Core</code> it’s not clear where to go so I decided to go look at the top level <code>Pipes</code> module since between the <code>.Internal</code> and <code>.Core</code> modules we should have covered a lot of it. It turns out the top level <em>only</em> imports those two modules so we can now go through that!</p>
<h2 id="pipes">Pipes</h2>
<p>Really the top level package <code>Pipes</code> just re exports some stuff and defines some thin layers of the rest</p>
<div class="sourceCode" id="cb30"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb30-1" data-line-number="1">    <span class="kw">module</span> <span class="dt">Pipes</span> (</a>
<a class="sourceLine" id="cb30-2" data-line-number="2">          <span class="dt">Proxy</span></a>
<a class="sourceLine" id="cb30-3" data-line-number="3">        , <span class="dt">X</span></a>
<a class="sourceLine" id="cb30-4" data-line-number="4">        , <span class="dt">Effect</span></a>
<a class="sourceLine" id="cb30-5" data-line-number="5">        , <span class="dt">Effect&#39;</span></a>
<a class="sourceLine" id="cb30-6" data-line-number="6">        , runEffect</a>
<a class="sourceLine" id="cb30-7" data-line-number="7">        , <span class="dt">Producer</span></a>
<a class="sourceLine" id="cb30-8" data-line-number="8">        , <span class="dt">Producer&#39;</span></a>
<a class="sourceLine" id="cb30-9" data-line-number="9">        , yield</a>
<a class="sourceLine" id="cb30-10" data-line-number="10">        , for</a>
<a class="sourceLine" id="cb30-11" data-line-number="11">        , (<span class="fu">~&gt;</span>)</a>
<a class="sourceLine" id="cb30-12" data-line-number="12">        , (<span class="fu">&lt;~</span>)</a>
<a class="sourceLine" id="cb30-13" data-line-number="13">        , <span class="dt">Consumer</span></a>
<a class="sourceLine" id="cb30-14" data-line-number="14">        , <span class="dt">Consumer&#39;</span></a>
<a class="sourceLine" id="cb30-15" data-line-number="15">        , await</a>
<a class="sourceLine" id="cb30-16" data-line-number="16">        , (<span class="fu">&gt;~</span>)</a>
<a class="sourceLine" id="cb30-17" data-line-number="17">        , (<span class="fu">~&lt;</span>)</a>
<a class="sourceLine" id="cb30-18" data-line-number="18">        , <span class="dt">Pipe</span></a>
<a class="sourceLine" id="cb30-19" data-line-number="19">        , cat</a>
<a class="sourceLine" id="cb30-20" data-line-number="20">        , (<span class="fu">&gt;-&gt;</span>)</a>
<a class="sourceLine" id="cb30-21" data-line-number="21">        , (<span class="ot">&lt;-</span><span class="fu">&lt;</span>)</a>
<a class="sourceLine" id="cb30-22" data-line-number="22">        , <span class="dt">ListT</span>(<span class="fu">..</span>)</a>
<a class="sourceLine" id="cb30-23" data-line-number="23">        , runListT</a>
<a class="sourceLine" id="cb30-24" data-line-number="24">        , <span class="dt">Enumerable</span>(<span class="fu">..</span>)</a>
<a class="sourceLine" id="cb30-25" data-line-number="25">        , next</a>
<a class="sourceLine" id="cb30-26" data-line-number="26">        , each</a>
<a class="sourceLine" id="cb30-27" data-line-number="27">        , every</a>
<a class="sourceLine" id="cb30-28" data-line-number="28">        , discard</a>
<a class="sourceLine" id="cb30-29" data-line-number="29">        , <span class="kw">module</span> <span class="dt">Control.Monad</span></a>
<a class="sourceLine" id="cb30-30" data-line-number="30">        , <span class="kw">module</span> <span class="dt">Control.Monad.IO.Class</span></a>
<a class="sourceLine" id="cb30-31" data-line-number="31">        , <span class="kw">module</span> <span class="dt">Control.Monad.Trans.Class</span></a>
<a class="sourceLine" id="cb30-32" data-line-number="32">        , <span class="kw">module</span> <span class="dt">Control.Monad.Morph</span></a>
<a class="sourceLine" id="cb30-33" data-line-number="33">        , <span class="kw">module</span> <span class="dt">Data.Foldable</span></a>
<a class="sourceLine" id="cb30-34" data-line-number="34">        )</a></code></pre></div>
<p>Now what haven’t we seen, the first thing is this <code>yield</code> construct which turns out to be a snazzier name for <code>respond</code> with a nicer type.</p>
<div class="sourceCode" id="cb31"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb31-1" data-line-number="1"><span class="ot">    yield ::</span> <span class="dt">Monad</span> m <span class="ot">=&gt;</span> a <span class="ot">-&gt;</span> <span class="dt">Producer&#39;</span> a m ()</a>
<a class="sourceLine" id="cb31-2" data-line-number="2">    yield <span class="fu">=</span> respond</a></code></pre></div>
<p>Similarly, <code>for</code> is just a synonym <code>(//&gt;)</code> (first joiner we went through) and <code>~&gt;</code> is the point free version. On the other end we have stuff overlaying <code>request</code> and friends but they’re not quite symmetric</p>
<div class="sourceCode" id="cb32"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb32-1" data-line-number="1"><span class="ot">    await ::</span> <span class="dt">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Consumer&#39;</span> a m a</a>
<a class="sourceLine" id="cb32-2" data-line-number="2">    await <span class="fu">=</span> request ()</a>
<a class="sourceLine" id="cb32-3" data-line-number="3"></a>
<a class="sourceLine" id="cb32-4" data-line-number="4">    (<span class="fu">&gt;~</span>)</a>
<a class="sourceLine" id="cb32-5" data-line-number="5"><span class="ot">        ::</span> <span class="dt">Monad</span> m</a>
<a class="sourceLine" id="cb32-6" data-line-number="6">        <span class="ot">=&gt;</span> <span class="dt">Proxy</span> a&#39; a y&#39; y m b</a>
<a class="sourceLine" id="cb32-7" data-line-number="7">        <span class="ot">-&gt;</span> <span class="dt">Proxy</span> () b y&#39; y m c</a>
<a class="sourceLine" id="cb32-8" data-line-number="8">        <span class="ot">-&gt;</span> <span class="dt">Proxy</span> a&#39; a y&#39; y m c</a>
<a class="sourceLine" id="cb32-9" data-line-number="9">    p1 <span class="fu">&gt;~</span> p2 <span class="fu">=</span> (\() <span class="ot">-&gt;</span> p1) <span class="fu">&gt;</span>\\ p2</a></code></pre></div>
<p>So we need to cope with the fact <code>request</code> can actually transfer interesting data down as well as up, in the basic case though we just assume that we’re dealing with <code>()</code>s. Also note that <code>&gt;~</code> is biased to the downstream <code>Proxy</code>, it starts by running it and whenever we actually request something (by sending up a <code>()</code>) we run <code>p1</code>. This function lets us compose <code>Proxy</code>s, not functions to <code>Proxy</code>s so that’s one nice effect.</p>
<p>Finally, we see our first example of a pipe</p>
<div class="sourceCode" id="cb33"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb33-1" data-line-number="1"><span class="ot">    cat ::</span> <span class="dt">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Pipe</span> a a m r</a>
<a class="sourceLine" id="cb33-2" data-line-number="2">    cat <span class="fu">=</span> pull ()</a></code></pre></div>
<p><code>cat</code> works by requesting something upstream immediately and passing it downstream. Nothing interesting except that it combines great with other <code>Proxy</code>s. Say for example we have a random number generator, we can easily create a <code>Proxy</code> producing random numbers with</p>
<div class="sourceCode" id="cb34"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb34-1" data-line-number="1">    randoms <span class="fu">=</span> lift getRandomNumber <span class="fu">&gt;~</span> cat</a></code></pre></div>
<p>we use <code>&gt;~</code> to replace each <code>request</code> in <code>cat</code> with a call to <code>getRandomNumber</code> which will be immediately pushed downstream. Similarly, we can use <code>cat</code> to push everything into some computation. If we want to debug a pipe by just printing everything we could say</p>
<div class="sourceCode" id="cb35"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb35-1" data-line-number="1">    printAll <span class="fu">=</span> for cat (lift <span class="fu">.</span> print)</a></code></pre></div>
<p>So <code>cat</code> is a nice way of lifting something to work across <code>Proxy</code>s of values if nothing else.</p>
<p>Next is the common case of composing to <code>Proxy</code>s,</p>
<div class="sourceCode" id="cb36"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb36-1" data-line-number="1">    (<span class="fu">&gt;-&gt;</span>)</a>
<a class="sourceLine" id="cb36-2" data-line-number="2"><span class="ot">        ::</span> <span class="dt">Monad</span> m</a>
<a class="sourceLine" id="cb36-3" data-line-number="3">        <span class="ot">=&gt;</span> <span class="dt">Proxy</span> a&#39; a () b m r</a>
<a class="sourceLine" id="cb36-4" data-line-number="4">        <span class="ot">-&gt;</span> <span class="dt">Proxy</span> () b c&#39; c m r</a>
<a class="sourceLine" id="cb36-5" data-line-number="5">        <span class="ot">-&gt;</span> <span class="dt">Proxy</span> a&#39; a c&#39; c m r</a>
<a class="sourceLine" id="cb36-6" data-line-number="6">    p1 <span class="fu">&gt;-&gt;</span> p2 <span class="fu">=</span> (\() <span class="ot">-&gt;</span> p1) <span class="fu">+&gt;&gt;</span> p2</a></code></pre></div>
<p><code>&gt;-&gt;</code> makes it easy to join up to <code>Proxy</code>s that don’t send any interesting data “up” with requests. <code>&gt;-&gt;</code> starts by running <code>p2</code> using <code>+&gt;&gt;</code> and whenever <code>p2</code> requests something it goes and runs <code>p1</code> for a while. This function lets us connect a <code>Pipe</code> to <code>Pipe</code> or <code>Producer</code> to <code>Consumer</code> for example.</p>
<p>Finally, we wrap up this module with the definition of <code>ListT</code> inside it. Using <code>Producer</code> we can define a nonbroken version of <code>ListT</code></p>
<div class="sourceCode" id="cb37"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb37-1" data-line-number="1">    <span class="kw">newtype</span> <span class="dt">ListT</span> m a <span class="fu">=</span> <span class="dt">Select</span> {<span class="ot"> enumerate ::</span> <span class="dt">Producer</span> a m () }</a>
<a class="sourceLine" id="cb37-2" data-line-number="2"></a>
<a class="sourceLine" id="cb37-3" data-line-number="3">    <span class="kw">instance</span> (<span class="dt">Monad</span> m) <span class="ot">=&gt;</span> <span class="dt">Functor</span> (<span class="dt">ListT</span> m) <span class="kw">where</span></a>
<a class="sourceLine" id="cb37-4" data-line-number="4">        fmap f p <span class="fu">=</span> <span class="dt">Select</span> (for (enumerate p) (\a <span class="ot">-&gt;</span> yield (f a)))</a>
<a class="sourceLine" id="cb37-5" data-line-number="5"></a>
<a class="sourceLine" id="cb37-6" data-line-number="6">    <span class="kw">instance</span> (<span class="dt">Monad</span> m) <span class="ot">=&gt;</span> <span class="dt">Applicative</span> (<span class="dt">ListT</span> m) <span class="kw">where</span></a>
<a class="sourceLine" id="cb37-7" data-line-number="7">        pure a <span class="fu">=</span> <span class="dt">Select</span> (yield a)</a>
<a class="sourceLine" id="cb37-8" data-line-number="8">        mf <span class="fu">&lt;*&gt;</span> mx <span class="fu">=</span> <span class="dt">Select</span> (</a>
<a class="sourceLine" id="cb37-9" data-line-number="9">            for (enumerate mf) (\f <span class="ot">-&gt;</span></a>
<a class="sourceLine" id="cb37-10" data-line-number="10">            for (enumerate mx) (\x <span class="ot">-&gt;</span></a>
<a class="sourceLine" id="cb37-11" data-line-number="11">            yield (f x) ) ) )</a>
<a class="sourceLine" id="cb37-12" data-line-number="12"></a>
<a class="sourceLine" id="cb37-13" data-line-number="13">    <span class="kw">instance</span> (<span class="dt">Monad</span> m) <span class="ot">=&gt;</span> <span class="dt">Monad</span> (<span class="dt">ListT</span> m) <span class="kw">where</span></a>
<a class="sourceLine" id="cb37-14" data-line-number="14">        return a <span class="fu">=</span> <span class="dt">Select</span> (yield a)</a>
<a class="sourceLine" id="cb37-15" data-line-number="15">        m <span class="fu">&gt;&gt;=</span> f  <span class="fu">=</span> <span class="dt">Select</span> (for (enumerate m) (\a <span class="ot">-&gt;</span> enumerate (f a)))</a>
<a class="sourceLine" id="cb37-16" data-line-number="16">        fail _   <span class="fu">=</span> mzero</a></code></pre></div>
<p>What’s kinda nifty here is we just use a <code>Producer</code> returning a <code>()</code> to represent our list. Here we can use <code>for</code> to access every <code>yield x</code> which corresponds to our “list” having an entry <code>x</code>! From there this is really just the standard set of instances for a list! In particular <code>&gt;&gt;=</code> is <code>mapConcat</code> for producers. That about wraps up this module and I’ll end the blog post with it.</p>
<h2 id="wrap-up">Wrap Up</h2>
<p>I didn’t actually go through all of <code>pipes</code> here, just the “core operations” which everything else is built on top of. In particular, I urge you to go read how <code>Pipes.Prelude</code> is implemented. Just like implementing the Haskell prelude is a good exercise the same is true of pipes.</p>
<p>It turned out that <code>pipes</code> isn’t all that awful on the inside, it’s a library built around a specific free-monad like structure which a couple different methods of joining two computations together. In particular there were a few different notions of composition which really define pipes</p>
<ol type="1">
<li>Sequential composition: running one machine and then another is given with <code>&gt;&gt;=</code></li>
<li>Take one computation and replace all its <code>Respond</code>s with another function using <code>//&gt;</code> or <code>for</code> in non-infix speak</li>
<li>Take one computation and replace all its <code>Request</code>s with another function using <code>&gt;\\</code></li>
<li>Take two computations and compose them together so a request from one is satisfied by running the other until a respond using <code>&gt;&gt;+</code> and <code>&gt;&gt;~</code></li>
</ol>
<p>Hope you learned as much as I did, cheers.</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Mon, 01 Jun 2015 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2015-06-01-pipes.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Compiling a Lazy Language in 1,000 words</title>
    <link>http://jozefg.bitbucket.org/posts/2015-05-19-compiling.html</link>
    <description><![CDATA[<div class="info">
    Posted on May 19, 2015
    
</div>
<div class="info">
    
    Tags: <a href="/tags/compilers.html">compilers</a>, <a href="/tags/haskell.html">haskell</a>
    
</div>

<p>I’m a fan of articles like <a href="http://maryrosecook.com/blog/post/git-in-six-hundred-words">this one</a> which set out to explain a really complicated subject in 600 words or less. I wanted to write one with a similar goal for compiling a language like Haskell. To help with this I’ve broken down what most compilers for a lazy language do into 5 different phases and spent 200 words explaining how they work. This isn’t really intended to be a tutorial on how to implement a compiler, I just want to make it less magical.</p>
<p>I assume that you know how a lazy functional language looks (this isn’t a tutorial on Haskell) and a little about how your machine works since I make a few references to how some lower level details are compiled. These will make more sense if you know such things, but they’re not necessary.</p>
<p>And the word-count-clock starts… now.</p>
<h2 id="parsing">Parsing</h2>
<p>Our interactions with compilers usually involve treating them as a huge function from string to string. We give them a string (our program) and it gives us back a string (the compiled code). However, on the inside the compiler does all sorts of stuff to that string we gave it and most of those operations are inconvenient to do as string operations. In the first part of the compiler, we convert the string into an abstract syntax tree. This is a data structure in the compiler which represents the string, but in</p>
<ol type="1">
<li>A more abstract way, it doesn’t have details such as whitespace or comments</li>
<li>A more convenient way, it let’s the compiler perform the operations it wants efficiently</li>
</ol>
<p>The process of going String -&gt; AST is called “parsing”. It has a lot of (kinda stuffy IMO) theory behind it. This is the only part of the compiler where the syntax actually matters and is usually the smallest part of the compiler.</p>
<p>Examples:</p>
<ul>
<li><a href="https://github.com/purescript/purescript/blob/master/src/Language/PureScript/Parser/">Purescript</a></li>
<li><a href="https://github.com/elm-lang/elm-compiler/tree/master/src/Parse">Elm</a></li>
</ul>
<h2 id="type-checking">Type Checking</h2>
<p>Now that we’ve constructed an abstract syntax tree we want to make sure that the program “makes sense”. Here “make sense” just means that the program’s types are correct. The process for checking that a program type checks involves following a bunch of rules of the form “A has type T if B has type T1 and C has type…”. All of these rules together constitute the type system for our language. As an example, in Haskell <code>f a</code> has the type <code>T2</code> if <code>f</code> has the type <code>T1 -&gt; T2</code> and <code>a</code> has the type <code>T1</code>.</p>
<p>There’s a small wrinkle in this story though: most languages require some type inference. This makes things 10x harder because we have to figure the types of everything as we go! Type inference isn’t even possible in a lot of languages and some clever contortions are often needed to be inferrable.</p>
<p>However, once we’ve done all of this the program is correct enough to compile. Past type checking, if the compiler raises an error it’s a compiler bug.</p>
<p>Examples:</p>
<ul>
<li><a href="/posts/2015-02-28-type-inference.html">A type inferencer for Mini-ML</a></li>
</ul>
<h2 id="optimizationssimplifications">Optimizations/Simplifications</h2>
<p>Now that we’re free of the constraints of having to report errors to the user things really get fun in the compiler. Now we start simplifying the language by converting a language feature into a mess of other, simpler language features. Sometimes we convert several features into specific instances of one more general feature. For example, we might convert our big fancy pattern language into a simpler one by elaborating each <code>case</code> into a bunch of nested <code>case</code>s.</p>
<p>Each time we remove a feature we end up with a slightly different language. This progression of languages in the compiler are called the “intermediate languages” (ILs). Each of these ILs have their own AST as well! In a good compiler we’ll have a lot of ILs as it makes the compiler much more maintainable.</p>
<p>An important part of choosing an IL is making it amenable to various optimizations. When the compiler is working with each IL it applies a set of optimizations to the program. For example</p>
<ol type="1">
<li>Constant folding, converting <code>1 + 1</code> to <code>2</code> during compile time</li>
<li>Inlining, copy-pasting the body of smaller functions where they’re called</li>
<li>Fusion, turning multiple passes over a datastructure into a single one</li>
</ol>
<p>Examples:</p>
<ul>
<li><a href="http://www.cs.tufts.edu/~nr/cs257/archive/luc-maranget/jun08.pdf">Pattern matching</a></li>
<li><a href="http://www.cs.cornell.edu/talc/papers/tal-popl.pdf">A nice demonstration of many ILs</a></li>
</ul>
<h2 id="spineless-tagless-and-generally-wimpy-il">Spineless, Tagless, and Generally Wimpy IL</h2>
<p>At some point in the compiler, we have to deal with the fact we’re compiling a lazy language. One nice way is to use a spineless, tagless, graph machine (STG machine).</p>
<p>How an STG machine works is a little complicated but here’s the gist</p>
<ul>
<li>An expression becomes a closure/thunk, a bundling of code to compute the expressoin and the data it needs. These closure may depend on several arguments being supplied</li>
<li>We have a stack for arguments and another for continuations. A continuation is some code which takes the value returned from an expression and does something with it, like pattern match on it</li>
<li>To evaluate an expression we push the arguments it needs onto the stack and “enter” the corresponding closure, running the code in it</li>
<li>When the expression has evaluated itself it will pop the next continuation off the stack and give it the resulting value</li>
</ul>
<p>During this portion of the compiler, we’d transform out last IL into a C-like language which actually works in terms of pushing, popping, and entering closures.</p>
<p>The key idea here that makes laziness work is that a closure defers work! It’s not a value, it’s a recipe for how to compute a value when we need it. Also note, all calls are tail calls since function calls are just a special case of entering a closure.</p>
<p>Another really beautiful idea in the STG machine is that closures evaluate themselves. This means closures present a uniform interface no matter what, all the details are hidden in that bundled up code. (I’m totally out of words to say this, but screw it it’s really cool).</p>
<p>Examples:</p>
<ul>
<li><a href="/posts/2014-10-28-stg.html">My writeup</a></li>
<li><a href="http://blog.ezyang.com/2011/04/the-haskell-heap/">ezyang’s (better) writeup</a></li>
<li><a href="http://research.microsoft.com/apps/pubs/default.aspx?id=67083">The paper</a></li>
<li><a href="http://research.microsoft.com/en-us/um/people/simonpj/papers/eval-apply/index.htm">Another paper</a></li>
</ul>
<h2 id="code-generation">Code Generation</h2>
<p>Finally, after converting to compiling STG machine we’re ready to output the target code. This bit is very dependent on what exactly we’re targeting.</p>
<p>If we’re targeting assembly, we have a few things to do. First, we have to switch from using variables to registers. This process is called register allocation and we basically slot each variable into an available register. If we run out, we store variables in memory and load it in as we need it.</p>
<p>In addition to register allocation, we have to compile those C-like language constructs to assembly. This means converting procedures into a label and some instructions, pattern matches into something like a jump table and so on. This is also where we’d apply low-level, bit-twiddling optimizations.</p>
<p>Examples:</p>
<ul>
<li><a href="http://www.stephendiehl.com/llvm/">LLVM Code Generation</a></li>
<li><a href="http://www.cs.princeton.edu/~appel/modern/ml/">Any good compilers book</a></li>
</ul>
<h2 id="conclusion">Conclusion</h2>
<p>Okay, clock off.</p>
<p>Hopefully that was helpful even if you don’t care that much about lazy languages (most of these ideas apply in any compiler). In particular, I hope that you now believe me when I say that lazy languages aren’t magical. In fact, the worry of how to implement laziness only really came up in one section of the compiler!</p>
<p>Now I have a question for you dear reader, what should I elaborate on? With summer ahead, I’ll have some free time soon. Is there anything else that you would like to see written about? (Just not parsing please)</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Tue, 19 May 2015 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2015-05-19-compiling.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>A Proof of Church Rosser in Twelf</title>
    <link>http://jozefg.bitbucket.org/posts/2015-05-05-confluence.html</link>
    <description><![CDATA[<div class="info">
    Posted on May  5, 2015
    
</div>
<div class="info">
    
    Tags: <a href="/tags/twelf.html">twelf</a>, <a href="/tags/types.html">types</a>
    
</div>

<p>An important property in any term rewriting system, a system of rules for saying one term can be rewritten into another, is called confluence. In a term rewriting system more than one rule may apply at a time, confluence states that it doesn’t matter in what order we apply these rules. In other words, there’s some sort of diamond property in our system</p>
<pre><code>                 Starting Term
                    /     \
                   /       \
          Rule 1  /         \ Rule 2
                 /           \
                /             \
               B               C
                \              /
         A bunch \     of     / rules later
                  \          /
                   \        /
                    \      /
                 Same end point</code></pre>
<p>In words (and not a crappy ascii picture)</p>
<ol type="1">
<li>Suppose we have some term <code>A</code></li>
<li>The system lets us rewrite <code>A</code> to <code>B</code></li>
<li>The system lets us rewrite <code>A</code> to <code>C</code></li>
</ol>
<p>Then two things hold</p>
<ol type="1">
<li>The system lets us rewrite <code>B</code> to <code>D</code> in some number of rewrites</li>
<li>The system lets us rewrite <code>C</code> to <code>D</code> with a different series of rewrites</li>
</ol>
<p>In the specific case of lambda calculus, confluence is referred to as the “Church-Rosser Theorem”. This theorem has several important corollaries, including that the normal forms of any lambda term is unique. To see this, remember that a normal form is always “at the bottom” of diamonds like the one we drew above. This means that if some term had multiple steps to take, they all must converge before one of them reaches a normal form. If any of them did hit a normal form first, they couldn’t complete the diamond.</p>
<h2 id="proving-church-rosser">Proving Church-Rosser</h2>
<p>In this post I’d like to go over a proof of the Church Rosser theorem in Twelf, everyone’s favorite mechanized metalogic. To follow along if you don’t know Twelf, perhaps some shameless <a href="/posts/2015-02-28-twelf.html">self</a> <a href="/posts/2015-03-07-worlds.html">linking</a> will help.</p>
<p>We need to start by actually defining lambda calculus. In keeping with Twelf style, we laugh at those restricted by the bounds of inductive types and use higher order abstract syntax to get binding for free.</p>
<pre class="twelf"><code>    term : type.
    ap   :  term -&gt; term  -&gt; term.
    lam  : (term -&gt; term) -&gt; term.</code></pre>
<p>We have to constructors, <code>ap</code>, which applies one term to another. The interesting one here is <code>lam</code> which embeds the LF function space, <code>term -&gt; term</code> into <code>term</code>. This actually makes sense because <code>term</code> isn’t an inductive type, just a type family with a few members. There’s no underlying induction principle with which we can derive contradictions. To be perfectly honest I’m not sure how the proof of soundness of something like Twelf %total mechanism proceeds. If a reader is feeling curious, I believe <a href="http://repository.cmu.edu/cgi/viewcontent.cgi?article=2238&amp;context=compsci">this</a> is the appropriate paper to read.</p>
<p>With this, something like <code>λx. x x</code> as <code>lam [x] ap x x</code>.</p>
<p>Now on to evaluation. We want to talk about things as a term rewriting system, so we opt for a small step evaluation approach.</p>
<pre class="twelf"><code>    step     : term -&gt; term -&gt; type.
    step/b   : step (ap (lam F) A) (F A).
    step/ap1 : step (ap F A) (ap F&#39; A)
                &lt;- step F F&#39;.
    step/ap2 : step (ap F A) (ap F A&#39;)
                &lt;- step A A&#39;.
    step/lam : step (lam [x] M x) (lam [x] M&#39; x)
                &lt;- ({x} step (M x) (M&#39; x)).

    step* : term -&gt; term -&gt; type.
    step*/z : step* A A.
    step*/s : step* A C
               &lt;- step A B
               &lt;- step* B C.</code></pre>
<p>We start with the 4 sorts of steps you can make in this system. 3 of them are merely “if you can step somewhere else, you can pull the rewrite out”, I’ve heard these referred to as compatibility rules. This is what <code>ap1</code>, <code>ap2</code> and <code>lam</code> do, <code>lam</code> being the most interesting since it deals with going under a binder. Finally, the main rule is <code>step/b</code> which defines beta reduction. Note that HOAS gives us this for free as application.</p>
<p>Finally, <code>step*</code> is for a series of steps. We either have no steps, or a step followed by another series of steps. Now we want to prove a couple theorems about our system. These are mostly the lifting of the “compatibility rules” up to working on <code>step*</code>s. The first is the lifting of <code>ap1</code>.</p>
<pre class="twelf"><code>     step*/left : step* F F&#39; -&gt; step* (ap F A) (ap F&#39; A) -&gt; type.
     %mode +{F : term} +{F&#39; : term} +{A : term} +{In : step* F F&#39;}
     -{Out : step* (ap F A) (ap F&#39; A)} (step*/left In Out).

     - : step*/left step*/z step*/z.
     - : step*/left (step*/s S* S) (step*/s S&#39;* (step/ap1 S))
          &lt;- step*/left S* S&#39;*.

     %worlds (lam-block) (step*/left _ _).
     %total (T) (step*/left T _).</code></pre>
<p><em>Note, the mode specification I’m using a little peculiar. It needs to be this verbose because otherwise <code>A</code> mode-errors. Type inference is peculiar.</em></p>
<p>The theorem says that if <code>F</code> steps to <code>F'</code> in several steps, for all <code>A</code>, <code>ap F A</code> steps to <code>ap F' A</code> in many steps. The actual proof is quite boring, we just recurse and apply <code>step/ap1</code> until everything type checks.</p>
<p>Note that the world specification for <code>step*/left</code> is a little strange. We use the block <code>lam-block</code> because later one of our theorem needs this. The block is just</p>
<pre><code>%block lam-block : block {x : term}.</code></pre>
<p>We need to annotate this on all our theorems because Twelf’s world subsumption checker isn’t convinced that <code>lam-block</code> can subsume the empty worlds we check some of our theorems in. Ah well.</p>
<p>Similarly to <code>step*/left</code> there is <code>step*/right</code>. The proof is 1 character off so I won’t duplicate it.</p>
<pre class="twelf"><code>    step*/right : step* A A&#39; -&gt; step* (ap F A) (ap F A&#39;) -&gt; type.</code></pre>
<p>Finally, we have <code>step/lam</code>, the lifting of the compatibility rule for lambdas. This one is a little more fun since it actually works by pattern matching on functions.</p>
<pre class="twelf"><code>     step*/lam : ({x} step* (F x) (F&#39; x))
                  -&gt; step* (lam F) (lam F&#39;)
                  -&gt; type.
     %mode step*/lam +A -B.

     - : step*/lam ([x] step*/z) step*/z.
     - : step*/lam ([x] step*/s (S* x) (S x))
          (step*/s S&#39;* (step/lam S))
          &lt;- step*/lam S* S&#39;*.

     %worlds (lam-block) (step*/lam _ _).
     %total (T) (step*/lam T _).</code></pre>
<p>What’s fun here is that we’re inducting on a dependent function. So the first case matches <code>[x] step*/z</code> and the second <code>[x] step*/s (S* x) (S x)</code>. Other than that we just use <code>step/lam</code> to lift up <code>S</code> and recurse to lift up <code>S*</code> in the second case.</p>
<p>We need one final (more complicated) lemma about substitution. It states that if <code>A</code> steps to <code>A'</code>, then <code>F A</code> steps to <code>F A'</code> in many steps for all <code>F</code>. This proceeds by induction on the derivation that <code>A</code> steps to <code>A'</code>. First off, here’s the formal statement in Twelf</p>
<p><em>This is the lemma that actually needs the world with <code>lam-block</code>s</em></p>
<pre class="twelf"><code>    subst : {F} step A A&#39; -&gt; step* (F A) (F A&#39;) -&gt; type.
    %mode subst +A +B -C.</code></pre>
<p>Now the actual proof. The first two cases are for constant functions and the identity function</p>
<pre class="twelf"><code>    - : subst ([x] A) S step*/z.
    - : subst ([x] x) S (step*/s step*/z S).</code></pre>
<p>In the case of the constant functions the results of <code>F A</code> and <code>F A'</code> are the same so we don’t need to step at all. In the case of the identity function we just step with the step from <code>A</code> to <code>A'</code>.</p>
<p>In the next case, we deal with nested lambdas.</p>
<pre class="twelf"><code>     - : subst ([x] lam ([y] F y x)) S S&#39;*
          &lt;- ({y} subst (F y) S (S* y))
          &lt;- step*/lam S* S&#39;*.</code></pre>
<p>Here we recurse, but we carefully do this under a pi type. The reason for doing this is because we’re recursing on the open body of the inner lambda. This has a free variable and we need a pi type in order to actually apply <code>F</code> to something to get at the body. Otherwise this just uses <code>step*/lam</code> to lift the step across the body to the step across lambdas.</p>
<p>Finally, application.</p>
<pre class="twelf"><code>     - : subst ([x] ap (F x) (A x)) S S*
          &lt;- subst F S F*
          &lt;- subst A S A*
          &lt;- step*/left F* S1*
          &lt;- step*/right A* S2*
          &lt;- join S1* S2* S*.</code></pre>
<p>This looks complicated, but isn’t so bad. We first recurse, and then use various compatibility lemmas to actually plumb the results of the recursive calls to the right parts of the final term. Since there are two individual pieces of stepping, one for the argument and one for the function, we use <code>join</code> to slap them together.</p>
<p>With this, we’ve got all our lemmas</p>
<pre class="twelf"><code>    %worlds (lam-block) (subst _ _ _).
    %total (T) (subst T _ _).</code></pre>
<h2 id="the-main-theorem">The Main Theorem</h2>
<p>Now that we have all the pieces in place, we’re ready to state and prove confluence. Here’s our statement in Twelf</p>
<pre class="twelf"><code>    confluent : step A B -&gt; step A C -&gt; step* B D -&gt; step* C D -&gt; type.
    %mode confluent +A +B -C -D.</code></pre>
<p>Unfortunately, there’s a bit of a combinatorial explosion with this. There are approximately 3 * 3 * 3 + 1 = 10 cases for this theorem. And thanks to the lemmas we’ve proven, they’re all boring.</p>
<p>First we have the cases where <code>step A B</code> is a <code>step/ap1</code>.</p>
<pre class="twelf"><code>     - : confluent (step/ap1 S1) (step/ap1 S2) S1&#39;* S2&#39;*
          &lt;- confluent S1 S2 S1* S2*
          &lt;- step*/left S1* S1&#39;*
          &lt;- step*/left S2* S2&#39;*.
     - : confluent (step/ap1 S1) (step/ap2 S2)
          (step*/s step*/z (step/ap2 S2))
          (step*/s step*/z (step/ap1 S1)).
     - : confluent (step/ap1 (step/lam F) : step (ap _ A) _) step/b
          (step*/s step*/z step/b) (step*/s step*/z (F A)).</code></pre>
<p>In the first case, we have two <code>ap1</code>s. We recurse on the smaller <code>S1</code> and <code>S2</code> and then immediately use one of our lemmas to lift the results of the recursive call, which step the function part of the the <code>ap</code> we’re looking at, to work across the whole <code>ap</code> term. In the second case there, we’re stepping the function in one, and the argument in the other. In order to bring these to a common term we just apply the first step to the resulting term of the second step and vice versa. This means that we’re doing something like this</p>
<pre><code>                 F A
                /   \
           S1  /     \ S2
              /       \
            F&#39; A     F  A&#39;
              \       /
           S2  \     /  S1
                \   /
                F&#39; A&#39;</code></pre>
<p>This clearly commutes so this case goes through. For the final case, we’re applying a lambda to some term so we can beta reduce. On one side we step the body of the lambda some how, and on the other we immediately substitute. Now we do something clever. What is a proof that <code>lam A</code> steps to <code>lam B</code>? It’s a proof that for any <code>x</code>, <code>A x</code> steps to <code>B x</code>. In fact, it’s just a function from <code>x</code> to such a step <code>A x</code> to <code>B x</code>. So we have that lying around in <code>F</code>. So to step from the beta-reduced term <code>G A</code> to <code>G' A</code> all we do is apply <code>F</code> to <code>A</code>! The other direction is just beta-reducing <code>ap (lam G') A</code> to the desired <code>G' A</code>.</p>
<p>In the next set of cases we deal with <code>ap2</code>!</p>
<pre class="twelf"><code>     - : confluent (step/ap2 S1) (step/ap2 S2) S1&#39;* S2&#39;*
          &lt;- confluent S1 S2 S1* S2*
          &lt;- step*/right S1* S1&#39;*
          &lt;- step*/right S2* S2&#39;*.
     - : confluent (step/ap2 S1) (step/ap1 S2)
          (step*/s step*/z (step/ap1 S2))
          (step*/s step*/z (step/ap2 S1)).
     - : confluent (step/ap2 S) (step/b : step (ap (lam F) _) _)
          (step*/s step*/z step/b) S1*
          &lt;- subst F S S1*.</code></pre>
<p>The first two cases are almost identical to what we’ve seen before. The key difference here is in the third case. This is again where we’re stepping something on one side and beta-reducing on the other. We can’t use the nice free stepping provided by <code>F</code> here since we’re stepping the argument, not the function. For this we appeal to <code>subst</code> which let’s us step <code>F A</code> to <code>F A'</code> using <code>S1*</code> exactly as required. The other direction is trivial just like it was in the <code>ap1</code> case, we just have to step <code>ap (lam F) A'</code> to <code>F A'</code> which is done with beta reduction.</p>
<p>I’m not going to detail the cases to do with <code>step/b</code> as the first argument because they’re just mirrors of the cases we’ve looked at before. That only leaves us with one more case, the case for <code>step/lam</code>.</p>
<pre class="twelf"><code>     - : confluent (step/lam F1) (step/lam F2) F1&#39;* F2&#39;*
          &lt;- ({x} confluent (F1 x) (F2 x) (F1* x) (F2* x))
          &lt;- step*/lam F1* F1&#39;*
          &lt;- step*/lam F2* F2&#39;*.</code></pre>
<p>This is just like all the other “diagonal” cases, like <code>confluent (ap1 S1) (ap1 S2) ...</code>. We first recurse (this time using a pi to unbind the body of the lambda) and then use compatibility rules in order to get something we can give back from <code>confluent</code>. And with this, we can actually prove that lambda calculus is confluent.</p>
<pre class="twelf"><code>    %worlds (lam-block) (confluent _ _ _ _).
    %total (T) (confluent T _ _ _).</code></pre>
<h2 id="wrap-up">Wrap Up</h2>
<p>We went through a fairly significant proof here, but the end results were interesting at least. One nice thing this proof illustrates is how well HOAS lets us encode these proofs. It’s a very Twelf-y approach to use lambdas to represent bindings. All in all, it’s a fun proof.</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Tue, 05 May 2015 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2015-05-05-confluence.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Bracket Abstraction: The Smallest PL You've Ever Seen</title>
    <link>http://jozefg.bitbucket.org/posts/2015-05-01-brackets.html</link>
    <description><![CDATA[<div class="info">
    Posted on May  1, 2015
    
</div>
<div class="info">
    
    Tags: <a href="/tags/types.html">types</a>, <a href="/tags/haskell.html">haskell</a>
    
</div>

<p>It’s well known that lambda calculus is an extremely small, Turing Complete language. In fact, most programming languages over the last 5 years have grown some (typed and or broken) embedding of lambda calculus with aptly named lambdas.</p>
<p>This is wonderful and everything but lambda calculus is actually a little complicated. It’s centred around binding and substituting for variables, while this is elegant it’s a little difficult to formalize mathematically. It’s natural to wonder whether we can avoid dealing with variables by building up all our lambda terms from a special privileged few.</p>
<p>These systems (sometimes called combinator calculi) are quite pleasant to model formally, but how do we know that our system is complete? In this post I’d like to go over translating any lambda calculus program into a particular combinator calculus, SK calculus.</p>
<h2 id="what-is-sk-combinator-calculus">What is SK Combinator Calculus?</h2>
<p>SK combinator calculus is a language with exactly 3 types of expressions.</p>
<ol type="1">
<li>We can apply one term to another, <code>e e</code>,</li>
<li>We have one term <code>s</code></li>
<li>We another term <code>k</code></li>
</ol>
<p>Besides the obvious ones, there are two main rules for this system:</p>
<ol type="1">
<li><code>s a b c</code> = <code>(a c) (b c)</code></li>
<li><code>k a b</code> = <code>a</code></li>
</ol>
<p>And that’s it. What makes SK calculus so remarkable is how minimal it is. We now show that it’s Turing complete by translating lambda calculus into it.</p>
<h2 id="bracket-abstraction">Bracket Abstraction</h2>
<p>First things first, let’s just define how to represent both SK calculus and lambda calculus in our Haskell program.</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb1-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">Lam</span> <span class="fu">=</span> <span class="dt">Var</span> <span class="dt">Int</span> <span class="fu">|</span> <span class="dt">Ap</span> <span class="dt">Lam</span> <span class="dt">Lam</span> <span class="fu">|</span> <span class="dt">Lam</span> <span class="dt">Lam</span></a>
<a class="sourceLine" id="cb1-2" data-line-number="2">    <span class="kw">data</span> <span class="dt">SK</span>  <span class="fu">=</span> <span class="dt">S</span> <span class="fu">|</span> <span class="dt">K</span> <span class="fu">|</span> <span class="dt">SKAp</span> <span class="dt">SK</span> <span class="dt">SK</span></a></code></pre></div>
<p>Now we begin by defining a translation from a simplified lambda calculus to SK calculus. This simplified calculus is just SK supplemented with variables. By defining this step, the actual transformation becomes remarkably crisp.</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb2-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">SKH</span> <span class="fu">=</span> <span class="dt">Var&#39;</span> <span class="dt">Int</span> <span class="fu">|</span> <span class="dt">S&#39;</span> <span class="fu">|</span> <span class="dt">K&#39;</span> <span class="fu">|</span> <span class="dt">SKAp&#39;</span> <span class="dt">SKH</span> <span class="dt">SKH</span></a></code></pre></div>
<p>Note that while <code>SKH</code> has variables, but no way to bind them. In order to remove a variable, we have <code>bracket</code>. <code>bracket</code> has the property that replacing <code>Var 0</code> in a term, <code>e</code>, with a term, <code>e'</code>, is the same as <code>SKAp (bracket e) e'</code>.</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb3-1" data-line-number="1">    <span class="co">-- Remove one variable</span></a>
<a class="sourceLine" id="cb3-2" data-line-number="2"><span class="ot">    bracket ::</span> <span class="dt">SKH</span> <span class="ot">-&gt;</span> <span class="dt">SKH</span></a>
<a class="sourceLine" id="cb3-3" data-line-number="3">    bracket (<span class="dt">Var&#39;</span> <span class="dv">0</span>) <span class="fu">=</span> <span class="dt">SKAp&#39;</span> (<span class="dt">SKAp&#39;</span> <span class="dt">S&#39;</span> <span class="dt">K&#39;</span>) <span class="dt">K&#39;</span></a>
<a class="sourceLine" id="cb3-4" data-line-number="4">    bracket (<span class="dt">Var&#39;</span> i) <span class="fu">=</span> <span class="dt">Var&#39;</span> (i <span class="fu">-</span> <span class="dv">1</span>)</a>
<a class="sourceLine" id="cb3-5" data-line-number="5">    bracket (<span class="dt">SKAp&#39;</span> l r) <span class="fu">=</span> <span class="dt">SKAp&#39;</span> (<span class="dt">SKAp&#39;</span> <span class="dt">S&#39;</span> (bracket l)) (bracket r)</a>
<a class="sourceLine" id="cb3-6" data-line-number="6">    bracket x <span class="fu">=</span> x</a></code></pre></div>
<p>If we’re at <code>Var 0</code> we replace the variable with the term <code>s k k</code>. This has the property that <code>(s k k) A = A</code>. It’s traditional to abbreviate <code>s k k</code> as <code>i</code> (leading to the name SKI calculus) but <code>i</code> is strictly unnecessary as we can see.</p>
<p>If we’re at an application, we do something really clever. We have two terms which both have a free variable, so we bracket them and use <code>S</code> to supply the free variable to both of them! Remember that</p>
<pre><code>s (bracket A) (bracket B) C = ((bracket A) C) ((bracket B) C)</code></pre>
<p>which is exactly what we require by the specification of <code>bracket</code>.</p>
<p>Now that we have a way to remove free variables from an <code>SKH</code> term, we can close off a term with no free variables to give back a normal <code>SK</code> term.</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb5-1" data-line-number="1"><span class="ot">    close ::</span> <span class="dt">SKH</span> <span class="ot">-&gt;</span> <span class="dt">SK</span></a>
<a class="sourceLine" id="cb5-2" data-line-number="2">    close (<span class="dt">Var&#39;</span> _) <span class="fu">=</span> error <span class="st">&quot;Not closed&quot;</span></a>
<a class="sourceLine" id="cb5-3" data-line-number="3">    close <span class="dt">S&#39;</span> <span class="fu">=</span> <span class="dt">S</span></a>
<a class="sourceLine" id="cb5-4" data-line-number="4">    close <span class="dt">K&#39;</span> <span class="fu">=</span> <span class="dt">K</span></a>
<a class="sourceLine" id="cb5-5" data-line-number="5">    close (<span class="dt">SKAp&#39;</span> l r) <span class="fu">=</span> <span class="dt">SKAp</span> (close l) (close r)</a></code></pre></div>
<p>Now our translator can be written nicely.</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb6-1" data-line-number="1"><span class="ot">    l2h ::</span> <span class="dt">Lam</span> <span class="ot">-&gt;</span> <span class="dt">SKH</span></a>
<a class="sourceLine" id="cb6-2" data-line-number="2">    l2h (<span class="dt">Var</span> i) <span class="fu">=</span> <span class="dt">Var&#39;</span> i</a>
<a class="sourceLine" id="cb6-3" data-line-number="3">    l2h (<span class="dt">Ap</span> l r) <span class="fu">=</span> <span class="dt">SKAp&#39;</span> (l2h l) (l2h r)</a>
<a class="sourceLine" id="cb6-4" data-line-number="4">    l2h (<span class="dt">Lam</span> h) <span class="fu">=</span> bracket (l2h h)</a>
<a class="sourceLine" id="cb6-5" data-line-number="5"></a>
<a class="sourceLine" id="cb6-6" data-line-number="6"><span class="ot">    translate ::</span> <span class="dt">Lam</span> <span class="ot">-&gt;</span> <span class="dt">SK</span></a>
<a class="sourceLine" id="cb6-7" data-line-number="7">    translate <span class="fu">=</span> close <span class="fu">.</span> l2h</a></code></pre></div>
<p><code>l2h</code> is the main worker in this function. It works across <code>SKH</code>’s because it needs to deal with open terms during the translation. However, during the process we repeatedly call bracket so every time we go under a binder we call <code>bracket</code> afterwards, removing the free variable we just introduced.</p>
<p>This means that if we call <code>l2h</code> on a closed lambda term we get back a closed <code>SKH</code> term. This justifies using <code>close</code> after the toplevel call to <code>l2h</code> in <code>translate</code> which wraps up our conversion.</p>
<p>For funsies I decided to translate the Y combinator and got back this mess</p>
<pre><code>(s ((s ((s s) ((s k) k))) ((s ((s s) ((s ((s s) k)) k))) ((s ((s s) k)) k))))
((s ((s s) ((s k) k))) ((s ((s s) ((s ((s s) k)) k))) ((s ((s s) k)) k)))</code></pre>
<p>Completely useless, but kinda fun to look at. More interestingly, the canonical nonterminating lambda term is <code>λx. x x</code> which gives back <code>s i i</code>, much more readable.</p>
<h2 id="wrap-up">Wrap Up</h2>
<p>Now that we’ve performed this translation we have a very nice proof of the turing completeness of SK calculus. This has some nice upshots, folks who study things like realizability models of constructive logics use Partial Combinatory Algebras a model of computation. This is essentially an algebraic model of SK calculus.</p>
<p>If nothing else, it’s really quite crazy that such a small language is possible of simulating any computable function across numbers.</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Fri, 01 May 2015 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2015-05-01-brackets.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Compiling With CPS</title>
    <link>http://jozefg.bitbucket.org/posts/2015-04-30-cps.html</link>
    <description><![CDATA[<div class="info">
    Posted on April 30, 2015
    
</div>
<div class="info">
    
    Tags: <a href="/tags/compilers.html">compilers</a>, <a href="/tags/haskell.html">haskell</a>
    
</div>

<p>Hello folks. It’s been a busy month so I haven’t had much a chance to write but I think now’s a good time to talk about another compiler related subject: continuation passing style conversion.</p>
<p>When you’re compiling a functional languages (in a sane way) your compiler mostly consists of phases which run over the AST and simplify it. For example in a language with pattern matching, it’s almost certainly the case that we can write something like</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb1-1" data-line-number="1">    <span class="kw">case</span> x <span class="kw">of</span></a>
<a class="sourceLine" id="cb1-2" data-line-number="2">      (<span class="dv">1</span>, <span class="dv">2</span>) <span class="ot">-&gt;</span> <span class="dt">True</span></a>
<a class="sourceLine" id="cb1-3" data-line-number="3">      (_, _) <span class="ot">-&gt;</span> <span class="dt">False</span></a></code></pre></div>
<p>Wonderfully concise code. However, it’s hard to compile nested patterns like that. In the compiler, we might simplify this to</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb2-1" data-line-number="1">    <span class="kw">case</span> x <span class="kw">of</span></a>
<a class="sourceLine" id="cb2-2" data-line-number="2">     (a, b) <span class="ot">-&gt;</span> <span class="kw">case</span> a <span class="kw">of</span></a>
<a class="sourceLine" id="cb2-3" data-line-number="3">                 <span class="dv">1</span> <span class="ot">-&gt;</span> <span class="kw">case</span> b <span class="kw">of</span></a>
<a class="sourceLine" id="cb2-4" data-line-number="4">                        <span class="dv">2</span> <span class="ot">-&gt;</span> <span class="dt">True</span></a>
<a class="sourceLine" id="cb2-5" data-line-number="5">                        _ <span class="ot">-&gt;</span> <span class="dt">False</span></a>
<a class="sourceLine" id="cb2-6" data-line-number="6">                 _ <span class="ot">-&gt;</span> <span class="dt">False</span></a></code></pre></div>
<p><em>note to future me, write a pattern matching compiler</em></p>
<p>We’ve transformed our large nested pattern into a series of simpler, unnested patterns. The benefit here is that this maps more straight to a series of conditionals (or jumps).</p>
<p>Now one of the biggest decisions in any compiler is what to do with expressions. We want to get rid of complicated nested expressions because chances are our compilation target doesn’t support them. In my <a href="http://jozefg.bitbucket.org/posts/2015-03-24-pcf.html">second to last post</a> we transformed a functional language into something like SSA. In this post, we’re going to walk through a different intermediate representation: CPS.</p>
<h2 id="what-is-cps">What is CPS</h2>
<p>CPS is a restriction of how a functional language works. In CPS we don’t have nested expressions anymore. We instead have a series of lets which telescope out and each binds a “flat” expressions. This is the process of “removing expressions” from our language. A compiler probably is targeting something with a much weaker notion of expressions (like assembly) and so we change our tree like structure into something more linear.</p>
<p>Additionally, no functions return. Instead, they take a continuation and when they’re about to return they instead pass their value to it. This means that conceptually, all functions are transformed from <code>a -&gt; b</code> to <code>(a, b -&gt; void) -&gt; void</code>. Logically, this is actually a reasonable thing to do. This corresponds to mapping a proposition <code>b</code> to <code>¬ ¬ b</code>. What’s cool here is that since each function call calls a continuation instead of returning its result, we can imagine that each function just transferring control over to some other part of the program instead of returning. This leads to a very slick and efficient way of implementing CPSed function calls as we’ll see.</p>
<p>This means we’d change something like</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb3-1" data-line-number="1">    fact n <span class="fu">=</span> <span class="kw">if</span> n <span class="fu">==</span> <span class="dv">0</span> <span class="kw">then</span> <span class="dv">1</span> <span class="kw">else</span> n <span class="fu">*</span> fact (n <span class="fu">-</span> <span class="dv">1</span>)</a></code></pre></div>
<p>into</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" data-line-number="1">    fact n k <span class="fu">=</span></a>
<a class="sourceLine" id="cb4-2" data-line-number="2">      <span class="kw">if</span> n <span class="fu">==</span> <span class="dv">0</span></a>
<a class="sourceLine" id="cb4-3" data-line-number="3">      <span class="kw">then</span> k <span class="dv">1</span></a>
<a class="sourceLine" id="cb4-4" data-line-number="4">      <span class="kw">else</span> <span class="kw">let</span> n&#39; <span class="fu">=</span> n <span class="fu">-</span> <span class="dv">1</span> <span class="kw">in</span></a>
<a class="sourceLine" id="cb4-5" data-line-number="5">           fact n&#39; (\r <span class="ot">-&gt;</span></a>
<a class="sourceLine" id="cb4-6" data-line-number="6">                          <span class="kw">let</span> r&#39; <span class="fu">=</span> n <span class="fu">*</span> r <span class="kw">in</span></a>
<a class="sourceLine" id="cb4-7" data-line-number="7">                          k r&#39;)</a></code></pre></div>
<p>To see what’s going on here we</p>
<ol type="1">
<li>Added an extra argument to <code>fact</code>, its return continuation</li>
<li>In the first branch, we pass the result to the continuation instead of returning it</li>
<li>In the next branch, we lift the nested expression <code>n - 1</code> into a flat let binding</li>
<li>We add an extra argument to the recursive call, the continuation</li>
<li>In this continuation, we apply multiply the result of the recursive call by <code>n</code> (Note here that we did close over <code>n</code>, this lambda is a real lambda)</li>
<li>Finally, we pass the final result to the original continuation <code>k</code>.</li>
</ol>
<p>The only tree-style-nesting here comes from the top <code>if</code> expression, everything else is completely linear.</p>
<p>Let’s formalize this process by converting Simply Typed Lambda Calculus (STLC) to CPS form.</p>
<h2 id="stlc-to-cps">STLC to CPS</h2>
<p>First things first, we specify an AST for normal STLC.</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb5-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">Tp</span> <span class="fu">=</span> <span class="dt">Arr</span> <span class="dt">Tp</span> <span class="dt">Tp</span> <span class="fu">|</span> <span class="dt">Int</span> <span class="kw">deriving</span> <span class="dt">Show</span></a>
<a class="sourceLine" id="cb5-2" data-line-number="2"></a>
<a class="sourceLine" id="cb5-3" data-line-number="3">    <span class="kw">data</span> <span class="dt">Op</span> <span class="fu">=</span> <span class="dt">Plus</span> <span class="fu">|</span> <span class="dt">Minus</span> <span class="fu">|</span> <span class="dt">Times</span> <span class="fu">|</span> <span class="dt">Divide</span></a>
<a class="sourceLine" id="cb5-4" data-line-number="4"></a>
<a class="sourceLine" id="cb5-5" data-line-number="5">    <span class="co">-- The Tp in App refers to the return type, it&#39;s helpful later</span></a>
<a class="sourceLine" id="cb5-6" data-line-number="6">    <span class="kw">data</span> <span class="dt">Exp</span> a <span class="fu">=</span> <span class="dt">App</span> (<span class="dt">Exp</span> a) (<span class="dt">Exp</span> a) <span class="dt">Tp</span></a>
<a class="sourceLine" id="cb5-7" data-line-number="7"></a>
<a class="sourceLine" id="cb5-8" data-line-number="8">               <span class="fu">|</span> <span class="dt">Lam</span> <span class="dt">Tp</span> (<span class="dt">Scope</span> () <span class="dt">Exp</span> a)</a>
<a class="sourceLine" id="cb5-9" data-line-number="9">               <span class="fu">|</span> <span class="dt">Num</span> <span class="dt">Int</span></a>
<a class="sourceLine" id="cb5-10" data-line-number="10">                 <span class="co">-- No need for binding here since we have Minus</span></a>
<a class="sourceLine" id="cb5-11" data-line-number="11">               <span class="fu">|</span> <span class="dt">IfZ</span> (<span class="dt">Exp</span> a) (<span class="dt">Exp</span> a) (<span class="dt">Exp</span> a)</a>
<a class="sourceLine" id="cb5-12" data-line-number="12">               <span class="fu">|</span> <span class="dt">Binop</span> <span class="dt">Op</span> (<span class="dt">Exp</span> a) (<span class="dt">Exp</span> a)</a>
<a class="sourceLine" id="cb5-13" data-line-number="13">               <span class="fu">|</span> <span class="dt">Var</span> a</a></code></pre></div>
<p>We’ve supplemented our lambda calculus with natural numbers and some binary operations because it makes things a bit more fun. Additionally, we’re using bound to deal with bindings for lambdas. This means there’s a terribly boring monad instance lying around that I won’t bore you with.</p>
<p>To convert to CPS, we first need to figure out how to convert our types. Since CPS functions never return we want them to go to <code>Void</code>, the unoccupied type. However, since our language doesn’t allow <code>Void</code> outside of continuations, and doesn’t allow functions that don’t go to <code>Void</code>, let’s bundle them up into one new type <code>Cont a</code> which is just a function from <code>a -&gt; Void</code>. However, this presents us with a problem, how do we turn an <code>Arr a b</code> into this style of function? It seems like our function should take two arguments, <code>a</code> and <code>b -&gt; Void</code> so that it can produce a <code>Void</code> of its own. However, this requires products since currying isn’t possible with the restriction that all functions return <code>Void</code>! Therefore, we supplement our CPS language with pairs and projections for them.</p>
<p>Now we can write the AST for CPS types and a conversion between <code>Tp</code> and it.</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb6-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">CTp</span> <span class="fu">=</span> <span class="dt">Cont</span> <span class="dt">CTp</span> <span class="fu">|</span> <span class="dt">CInt</span> <span class="fu">|</span> <span class="dt">CPair</span> <span class="dt">CTp</span> <span class="dt">CTp</span></a>
<a class="sourceLine" id="cb6-2" data-line-number="2"></a>
<a class="sourceLine" id="cb6-3" data-line-number="3"><span class="ot">    cpsTp ::</span> <span class="dt">Tp</span> <span class="ot">-&gt;</span> <span class="dt">CTp</span></a>
<a class="sourceLine" id="cb6-4" data-line-number="4">    cpsTp (<span class="dt">Arr</span> l r) <span class="fu">=</span> <span class="dt">Cont</span> <span class="fu">$</span> <span class="dt">CPair</span> (cpsTp l) (<span class="dt">Cont</span> (cpsTp r))</a>
<a class="sourceLine" id="cb6-5" data-line-number="5">    cpsTp <span class="dt">Int</span> <span class="fu">=</span> <span class="dt">CInt</span></a></code></pre></div>
<p>The only interesting thing here is how we translate function types, but we talked about that above. Now for expressions.</p>
<p>We want to define a new data type that encapsulates the restrictions of CPS. In order to do this we factor out our data types into “flat expressions” and “CPS expressions”. Flat expressions are things like values and variables while CPS expressions contain things like “Jump to this continuation” or “Branch on this flat expression”. Finally, there’s let expressions to perform various operations on expressions.</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb7-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">LetBinder</span> a <span class="fu">=</span> <span class="dt">OpBind</span> <span class="dt">Op</span> (<span class="dt">FlatExp</span> a) (<span class="dt">FlatExp</span> a)</a>
<a class="sourceLine" id="cb7-2" data-line-number="2">                     <span class="fu">|</span> <span class="dt">ProjL</span> a</a>
<a class="sourceLine" id="cb7-3" data-line-number="3">                     <span class="fu">|</span> <span class="dt">ProjR</span> a</a>
<a class="sourceLine" id="cb7-4" data-line-number="4">                     <span class="fu">|</span> <span class="dt">Pair</span> (<span class="dt">FlatExp</span> a) (<span class="dt">FlatExp</span> a)</a>
<a class="sourceLine" id="cb7-5" data-line-number="5"></a>
<a class="sourceLine" id="cb7-6" data-line-number="6">    <span class="kw">data</span> <span class="dt">FlatExp</span> a <span class="fu">=</span> <span class="dt">CNum</span> <span class="dt">Int</span> <span class="fu">|</span> <span class="dt">CVar</span> a <span class="fu">|</span> <span class="dt">CLam</span> <span class="dt">CTp</span> a (<span class="dt">CExp</span> a)</a>
<a class="sourceLine" id="cb7-7" data-line-number="7"></a>
<a class="sourceLine" id="cb7-8" data-line-number="8">    <span class="kw">data</span> <span class="dt">CExp</span> a <span class="fu">=</span> <span class="dt">Let</span> a (<span class="dt">LetBinder</span> a) (<span class="dt">CExp</span> a)</a>
<a class="sourceLine" id="cb7-9" data-line-number="9">                <span class="fu">|</span> <span class="dt">CIf</span> (<span class="dt">FlatExp</span> a) (<span class="dt">CExp</span> a) (<span class="dt">CExp</span> a)</a>
<a class="sourceLine" id="cb7-10" data-line-number="10">                <span class="fu">|</span> <span class="dt">Jump</span> (<span class="dt">FlatExp</span> a) (<span class="dt">FlatExp</span> a)</a>
<a class="sourceLine" id="cb7-11" data-line-number="11">                <span class="fu">|</span> <span class="dt">Halt</span> (<span class="dt">FlatExp</span> a)</a></code></pre></div>
<p><code>Let</code>s let us bind the results of a few “primitive operations” across values and variables to a fresh variable. This is where things like “incrementing a number” happen. Additionally, in order to create a pair or access its elements we need to us a <code>Let</code>.</p>
<p>Notice that here application is spelled <code>Jump</code> hinting that it really is just a <code>jmp</code> and not dealing with the stack in any way. They’re all jumps we can not overflow the stack as would be an issue with a normal calling convention. To seal of the chain of function calls we have <code>Halt</code>, it takes a <code>FlatExp</code> and returns it as the result of the program.</p>
<p>Expressions here are also parameterized over variables but we can’t use bound with them (for reasons that deserve a blogpost-y rant :). Because of this we settle for just ensuring that each <code>a</code> is globally unique.</p>
<p>So now instead of having a bunch of nested <code>Exp</code>s, we have flat expressions which compute exactly one thing and linearize the tree of expressions into a series of flat ones with let binders. It’s still not quite “linear” since both lambdas and if branches let us have something tree-like.</p>
<p>We can now define conversion to CPS with one major helper function</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb8-1" data-line-number="1"><span class="ot">    cps ::</span> (<span class="dt">Eq</span> a, <span class="dt">Enum</span> a)</a>
<a class="sourceLine" id="cb8-2" data-line-number="2">        <span class="ot">=&gt;</span> <span class="dt">Exp</span> a</a>
<a class="sourceLine" id="cb8-3" data-line-number="3">        <span class="ot">-&gt;</span> (<span class="dt">FlatExp</span> a <span class="ot">-&gt;</span> <span class="dt">Gen</span> a (<span class="dt">CExp</span> a))</a>
<a class="sourceLine" id="cb8-4" data-line-number="4">        <span class="ot">-&gt;</span> <span class="dt">Gen</span> a (<span class="dt">CExp</span> a)</a></code></pre></div>
<p>This takes an expression, a “continuation” and produces a <code>CExp</code>. We have some monad-gen stuff going on here because we need unique variables. The “continuation” is an actual Haskell function. So our function breaks an expression down to a <code>FlatExp</code> and then feeds it to the continuation.</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb9-1" data-line-number="1">    cps (<span class="dt">Var</span> a) c <span class="fu">=</span> c (<span class="dt">CVar</span> a)</a>
<a class="sourceLine" id="cb9-2" data-line-number="2">    cps (<span class="dt">Num</span> i) c <span class="fu">=</span> c (<span class="dt">CNum</span> i)</a></code></pre></div>
<p>The first two cases are easy since variables and numbers are already flat expressions, they go straight into the continuation.</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb10-1" data-line-number="1">    cps (<span class="dt">IfZ</span> i t e) c <span class="fu">=</span> cps i <span class="fu">$</span> \ic <span class="ot">-&gt;</span> <span class="dt">CIf</span> ic <span class="fu">&lt;$&gt;</span> cps t c <span class="fu">&lt;*&gt;</span> cps e c</a></code></pre></div>
<p>For <code>IfZ</code> we first recurse on the <code>i</code>. Then once we have a flattened computation representing <code>i</code>, we use <code>CIf</code> and recurse.</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb11-1" data-line-number="1">    cps (<span class="dt">Binop</span> op l r) c <span class="fu">=</span></a>
<a class="sourceLine" id="cb11-2" data-line-number="2">      cps l <span class="fu">$</span> \fl <span class="ot">-&gt;</span></a>
<a class="sourceLine" id="cb11-3" data-line-number="3">      cps r <span class="fu">$</span> \fr <span class="ot">-&gt;</span></a>
<a class="sourceLine" id="cb11-4" data-line-number="4">      gen <span class="fu">&gt;&gt;=</span> \out <span class="ot">-&gt;</span></a>
<a class="sourceLine" id="cb11-5" data-line-number="5">      <span class="dt">Let</span> out (<span class="dt">OpBind</span> op fl fr) <span class="fu">&lt;$&gt;</span> c (<span class="dt">CVar</span> out)</a></code></pre></div>
<p>Like before, we use <code>cps</code> to recurse on the left and right sides of the expression. This gives us two flat expressions which we use with <code>OpBind</code> to compute the result and bind it to <code>out</code>. Now that we have a variable for the result we just toss it to the continuation.</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb12-1" data-line-number="1">    cps (<span class="dt">Lam</span> tp body) c <span class="fu">=</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb12-2" data-line-number="2">      [pairArg, newCont, newArg] <span class="ot">&lt;-</span> replicateM <span class="dv">3</span> gen</a>
<a class="sourceLine" id="cb12-3" data-line-number="3">      <span class="kw">let</span> body&#39; <span class="fu">=</span> instantiate1 (<span class="dt">Var</span> newArg) body</a>
<a class="sourceLine" id="cb12-4" data-line-number="4">      cbody <span class="ot">&lt;-</span> cps body&#39; (return <span class="fu">.</span> <span class="dt">Jump</span> (<span class="dt">CVar</span> newCont))</a>
<a class="sourceLine" id="cb12-5" data-line-number="5">      c (<span class="dt">CLam</span> (cpsTp tp) pairArg</a>
<a class="sourceLine" id="cb12-6" data-line-number="6">         <span class="fu">$</span> <span class="dt">Let</span> newArg  (<span class="dt">ProjL</span> pairArg)</a>
<a class="sourceLine" id="cb12-7" data-line-number="7">         <span class="fu">$</span> <span class="dt">Let</span> newCont (<span class="dt">ProjR</span> pairArg)</a>
<a class="sourceLine" id="cb12-8" data-line-number="8">         <span class="fu">$</span> cbody)</a></code></pre></div>
<p>Converting a lambda is a little bit more work. It needs to take a pair so a lot of the work is projecting out the left component (the argument) and the right component (the continuation). With those two things in hand we recurse in the body using the continuation supplied as an argument. The actual code makes this process look a little out of order. Remember that we only use <code>cbody</code> once we’ve bound the projections to <code>newArg</code> and <code>pairArg</code> respectively.</p>
<div class="sourceCode" id="cb13"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb13-1" data-line-number="1">    cps (<span class="dt">App</span> l r tp) c <span class="fu">=</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb13-2" data-line-number="2">      arg <span class="ot">&lt;-</span> gen</a>
<a class="sourceLine" id="cb13-3" data-line-number="3">      cont <span class="ot">&lt;-</span> <span class="dt">CLam</span> (cpsTp tp) arg <span class="fu">&lt;$&gt;</span> c (<span class="dt">CVar</span> arg)</a>
<a class="sourceLine" id="cb13-4" data-line-number="4">      cps l <span class="fu">$</span> \fl <span class="ot">-&gt;</span></a>
<a class="sourceLine" id="cb13-5" data-line-number="5">        cps r <span class="fu">$</span> \fr <span class="ot">-&gt;</span></a>
<a class="sourceLine" id="cb13-6" data-line-number="6">        gen <span class="fu">&gt;&gt;=</span> \pair <span class="ot">-&gt;</span></a>
<a class="sourceLine" id="cb13-7" data-line-number="7">        return <span class="fu">$</span> <span class="dt">Let</span> pair (<span class="dt">Pair</span> fr cont) (<span class="dt">Jump</span> fl (<span class="dt">CVar</span> pair))</a></code></pre></div>
<p>For application we just create a lambda for the current continuation. We then evaluate the left and right sides of the application using recursive calls. Now that we have a function to jump to, we create a pair of the argument and the continuation and bind it to a name. From there, we just jump to <code>fl</code>, the function. Turning the continuation into a lambda is a little strange, it’s also we needed an annotation for <code>App</code>. The lambda uses the return type of the application and constructs a continuation that maps <code>a</code> to <code>c a</code>. Note that <code>c a</code> is a Haskell expressions with the type <code>CExp a</code>.</p>
<div class="sourceCode" id="cb14"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb14-1" data-line-number="1"><span class="ot">    convert ::</span> <span class="dt">Exp</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">CExp</span> <span class="dt">Int</span></a>
<a class="sourceLine" id="cb14-2" data-line-number="2">    convert <span class="fu">=</span> runGen <span class="fu">.</span> flip cps (return <span class="fu">.</span> <span class="dt">Halt</span>)</a></code></pre></div>
<p>With this, we’ve written a nice little compiler pass to convert expressions into their CPS forms. By doing this we’ve “eliminated expressions”. Everything is now flat and evaluation basically proceeds by evaluating one small computation and using the result to compute another and another.</p>
<p>There’s still some things left to compile out before this is machine code though</p>
<ul>
<li>Closures - these can be converted to explicitly pass records with closure conversion</li>
<li>Hoist lambdas out of nested scope - this gets rid of anonymous functions, something we don’t have in C or assembly</li>
<li>Make allocation explicit - Allocate a block of memory for a group of let statements and have them explicitly move the results of their computations to it</li>
<li>Register allocation - Cleverly choose whether to store some particular variable in a register or load it in as needed.</li>
</ul>
<p>Once we’ve done these steps we’ve basically written a compiler. However, they’re all influenced by the fact that we’ve compiled out expressions and (really) function calls with our conversion to CPS, it makes the process much much simpler.</p>
<h2 id="wrap-up">Wrap Up</h2>
<p>CPS conversion is a nice alternative to something like STG machines for lazy languages or SSA for imperative ones. As far as I’m aware the main SML interpreter (SML/NJ) compiles code in this way. As does Ur/Web if I’m not crazy. Additionally, the course entitled “Higher Order, Typed Compilation” which is taught here at CMU uses CPS conversion to make compiling SML really quite pleasant.</p>
<p>In fact, someone (Andrew Appel?) once wrote a paper that noted that SSA and CPS are actually the same. The key difference is that in SSA we merge multiple blocks together using the phi function. In CPS, we just let multiple source blocks jump to the same destination block (continuation). You can see this in our conversion of <code>IfZ</code> to CPS, instead of using <code>phi</code> to merge in the two branches, they both just use the continuation to jump to the remainder of the program. It makes things a little simpler because no one person needs to worry about</p>
<p>Finally, if you’re compiling a language like Scheme with call/cc, using CPS conversion makes the whole thing completely trivial. All you do is define <code>call/cc</code> at the CPS level as</p>
<pre><code>call/cc (f, c) = f ((λ (x, c&#39;) → c x), c)</code></pre>
<p>So instead of using the continuation supplied to us in the expression we give to <code>f</code>, we use the one for the whole <code>call/cc</code> invocation! This causes us to not return into the body of <code>f</code> but instead to carry on the rest of the program as if <code>f</code> had returned whatever value <code>x</code> is. This is how my old Scheme compiler did things, I put figuring out how to implement <code>call/cc</code> off for a week before I realized it was a 10 minute job!</p>
<p>Hope this was helpful!</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Thu, 30 Apr 2015 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2015-04-30-cps.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>SML for Haskellers</title>
    <link>http://jozefg.bitbucket.org/posts/2015-04-24-sml-for-haskellers.html</link>
    <description><![CDATA[<div class="info">
    Posted on April 24, 2015
    
</div>
<div class="info">
    
    Tags: <a href="/tags/sml.html">sml</a>, <a href="/tags/haskell.html">haskell</a>
    
</div>

<p>Inspired by ezyang’s <a href="http://blog.ezyang.com/2010/10/ocaml-for-haskellers/">OCaml for Haskellers</a> I decided to write something similar for SML. If you already know OCaml I also recommend <a href="http://adam.chlipala.net/mlcomp/">Adam Chlipala’s guide</a></p>
<p>I’ll follow mostly the same structure as Edward’s article so we’ll have</p>
<pre><code>{- Haskell -}
(* SML *)</code></pre>
<h2 id="what-do-they-have-in-common">What Do They Have in Common</h2>
<p>SML and Haskell have quite a lot in common</p>
<p>Common types:</p>
<pre><code>()   | Int | Integer    | Char | Bool | String | Double | (A,  B,  C)
unit | int | IntInf.int | char | bool | string | real   | (A * B * C)</code></pre>
<p>Literals:</p>
<pre><code>() | 1 | &#39;a&#39;  | True | &quot;hello&quot; | 3.14 | (1, 2, 3)
() | 1 | #&#39;a&#39; | true | &quot;hello&quot; | 3.14 | (1, 2, 3)</code></pre>
<p>Common operators</p>
<pre><code>== | /= | not | &amp;&amp;      | ||     | ++ | !!
=  | &lt;&gt; | not | andalso | orelse | ^  | String.sub</code></pre>
<p>Type variables:</p>
<pre><code>a  b
&#39;a &#39;AnythingGoes</code></pre>
<p>Function application:</p>
<pre><code>f x y z
f x y z</code></pre>
<p>Lambdas:</p>
<pre><code>\x -&gt; ...
fn x =&gt; ...</code></pre>
<p>If:</p>
<pre><code>if True then 1 else 0
if true then 1 else 0</code></pre>
<p>Pattern matching</p>
<pre><code>case x of
  Nothing -&gt; ...
  Just a -&gt; ...

case x of
   NONE =&gt; ...
 | SOME a =&gt; ...</code></pre>
<p>Top level functions support pattern matching in both:</p>
<pre><code>factorial 0 = 1
factorial n = n * factorial (n - 1)

fun factorial 0 = 1
  | factorial n = n * factorial (n - 1)</code></pre>
<p>Top level bindings can be declared without the sugar for currying as well.</p>
<pre><code>f = \x -&gt; \y -&gt; x
val f = fn x =&gt; fn y =&gt; x</code></pre>
<p>We can have top level patterns in both as well:</p>
<pre><code> (a, b) = (1, 2)
 val (a, b) = (1, 2)</code></pre>
<p>Type synonyms:</p>
<pre><code>type Three a b = (a, a, b)
type (&#39;a, &#39;b) three = &#39;a * &#39;a * &#39;b</code></pre>
<p>Data types:</p>
<pre><code>data List a = Cons a (List a) | Nil
datatype &#39;a list = Cons of &#39;a * &#39;a list | Nil</code></pre>
<p>Notice that in ML data type constructors can only take on argument. This means they often end up taking a tuple (or record). They are however normal functions unlike in OCaml.</p>
<p>Type annotations:</p>
<pre><code>f :: a -&gt; a
f x = x

fun f (x : &#39;a) : &#39;a = x</code></pre>
<p>Type annotations for expressions:</p>
<pre><code>(1 + 1 :: Int)
(1 + 1 :  int)</code></pre>
<p>Let bindings:</p>
<pre><code>let x = 1     in x + x
let val x = 1 in x + x end</code></pre>
<p>Declare a new mutable reference:</p>
<pre><code>newIORef True
ref true</code></pre>
<p>Modify a mutable reference:</p>
<pre><code>setIORef r False
r := false</code></pre>
<p>Read a mutable reference:</p>
<pre><code>readIORef r
! r</code></pre>
<p>Making exceptions:</p>
<pre><code>data MyExn = Exn String; instance Exception ... where
exception Exn of string</code></pre>
<p>Raising an exception:</p>
<pre><code>throw (Exn &quot;uh oh&quot;)
raise Exn &quot;uh oh&quot;</code></pre>
<p>Catching an exception:</p>
<pre><code>catch e $ \(Exn s) -&gt; s
e handle Exn s =&gt; s</code></pre>
<p>Since SML isn’t a purely functional language, none of the last couple of constructs listed live in anything monadic like their Haskell siblings. The type of <code>r := false</code> is just <code>unit</code>, not <code>IO ()</code> or something.</p>
<h2 id="what-is-sml-missing">What Is SML Missing</h2>
<p>Aside from the obvious things, like SML being strict so it’s missing pervasive lazy evaluation, SML is missing some things from Haskell.</p>
<p>the biggest gap I stumble across in SML is the lack of higher kinded polymorphism:</p>
<pre><code>data Fix f = Fix (f (Fix f))
datatype &#39;f fix = Fix of (&#39;f fix) &#39;f (* Urk, syntax error *)</code></pre>
<p>Even applying a type variable is a syntax error! As this might suggest to you, SML’s type system is much simpler than what we have in Haskell. It doesn’t have a notion of type families, GADTs, fancy kinds, data type promotion, etc, etc. SML is really limited to the areas of the Haskell type system you’d be accustomed to after reading Learn You A Haskell! Just algebraic data types, functions, and polymorphism.</p>
<p>Aside from this, SML doesn’t have guards, nor a lot of syntactic sugar that Haskell has. A nice exception to this is lambda cases, which is written</p>
<pre><code>fn 0 =&gt; 1
 | 1 =&gt; 2
 | n =&gt; 0</code></pre>
<p>Additionally, SML doesn’t have significant indentation which means that occasionally awkward parenthesis is necessary. For example</p>
<pre><code>case x of
   true  =&gt; (case !r of
              x =&gt; x + 1)
 | false =&gt; (r := 1; 2)</code></pre>
<p>The parenthesis are mandatory.</p>
<p>On the stranger side, SML has records (discussed later) but they don’t have a functional updating operation. This is a pain to be honest. Also related, SML has a somewhat nasty habit of allowing for ad-hoc overloading in the way most languages do: certain expressions are “blessed” with unutterable types that must be inferred from context. There are only a few of these, <code>+</code>, <code>*</code>, and record accessors being among them. I’m personally not a huge fan, but in practice this is almost never an issue.</p>
<p>Finally ML doesn’t have Haskell-style type classes. I don’t miss them, some people would.</p>
<h2 id="what-is-haskell-missing-in-comparison">What Is Haskell Missing (in Comparison)</h2>
<p>Aside from the obvious things, like Haskell being lazy so it’s missing pervasive eager evaluation, SML does have a couple of interesting things.</p>
<p>Of course SML has actual modules. I’ve <a href="/posts/2015-01-08-modules.html">explained a bit about them earlier</a>. This alone is reason enough to write some ML. Additionally, SML has a saner notion of records. Records are a type in and of themselves. This means we can have something like</p>
<pre class="sml"><code>    type coord = {x : int, y : int}</code></pre>
<p>However, since this is just a type synonym we don’t actually need to declare it. Accessors are written <code>#x</code> to access the field <code>x</code> from a record. SML doesn’t have a very advanced record system so <code>#x</code> isn’t typeable. It’s overloaded to access a field from some record and the concrete record must be inferrable from context. This often means that while we <em>can</em> have free floating records, the inference woes make us want to wrap them in constructors like so</p>
<pre><code>data coord = Coord of {x : int, y : int}</code></pre>
<p>This has the nice upshot that record accessors aren’t horrible broken with multiple constructors. Let’s say we had</p>
<pre><code>datatype user = Person {firstName : string, lastName : string}
              | Robot  {owner : string, guid : int}</code></pre>
<p>We can’t apply <code>#firstName</code> to an expression of type <code>user</code>. It’s ill-typed since <code>user</code> isn’t a record, it has a constructor which <em>contains a record</em>. In order to apply <code>#firstName</code> we have to pattern match first.</p>
<p>Finally, SML has a real, honest to goodness specification. In fact, SML is so well specified it’s been <a href="https://github.com/SMLFamily/The-Mechanization-of-Standard-ML">completely mechanized</a>. There is an actual mechanized proof that SML is typesafe. The practical up shot of this is that SML is rock solid. There’s a definitive right answer to what a program should do and that answer is “whatever that one true implementation does”. In fact, there are actually a lot of SML compilers and they’re all reasonably compliant. Two noteworthy ones</p>
<ol type="1">
<li>SML/NJ - An interactive system for SML. This provides a REPL and is what we use at CMU for our introduction to functional programming courses.</li>
<li>Mlton - A whole program optimizing compiler. Mlton produces stupidly fast code but is significantly slower for compilation.</li>
</ol>
<p>Since SML is fully standardized, I general develop with NJ and eventually feed the program into mlton if I intend the thing to run fast.</p>
<p>Also, modules are amazing, have I mentioned modules yet?</p>
<h2 id="wrap-up">Wrap Up</h2>
<p>So now that we’ve gone through most of the basic syntactic constructs of SML, most ML code should be readable. This is great because there’s some interesting pieces of ML code to read. In particular, these wonderful books are written with ML</p>
<ul>
<li>Purely Functional Data Structures</li>
<li>Compiling With Continuations</li>
<li>Modern Compiler Construction in ML</li>
</ul>
<p>I recommend all three of these books heartily. If you’re looking to learn about compilers, the last one in particular is the best introduction I’m aware of. The second one is an in depth look at a trick for compiling strict functional language.</p>
<p>Other general books on ML if you decide you want to give SML a more serious look</p>
<ul>
<li>ML for the Working Programmer</li>
<li>Elements of ML Programming</li>
<li>Programming in Standard ML</li>
</ul>
<p>The course I’m TAing currently is based around the last one and it’s freely available online which is nice.</p>
<p>Cheers,</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Fri, 24 Apr 2015 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2015-04-24-sml-for-haskellers.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Value vs Monomorphism Restriction</title>
    <link>http://jozefg.bitbucket.org/posts/2015-03-27-unsafe.html</link>
    <description><![CDATA[<div class="info">
    Posted on March 27, 2015
    
</div>
<div class="info">
    
    Tags: <a href="/tags/sml.html">sml</a>, <a href="/tags/haskell.html">haskell</a>
    
</div>

<p>I’m taking the undergraduate course on programming languages at CMU. For the record, I still get really excited about the novelty of taking a class (at school!) on programming languages. I’m easily made happy.</p>
<p>We started talking about System F and before long we touched on the value restriction. Specifically, how most people think of the value restriction incorrectly. To understand why this is, let’s first define the value restriction since it’s probably new to you if you don’t use SML.</p>
<h2 id="the-value-restriction">The Value Restriction</h2>
<p>In SML there are value level declarations just like in Haskell. We can write things like</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode ocaml"><code class="sourceCode ocaml"><a class="sourceLine" id="cb1-1" data-line-number="1">    <span class="kw">val</span> x = <span class="dv">1</span></a>
<a class="sourceLine" id="cb1-2" data-line-number="2">    <span class="kw">val</span> y = x + <span class="dv">1</span></a></code></pre></div>
<p>and we end up with <code>x</code> bound to <code>1</code> and <code>y</code> bound to <code>2</code>. Note that SML is strict so these bindings are evaluated right as we reach them. Also like in Haskell, SML has polymorphism, so we can write <code>map</code></p>
<div class="sourceCode" id="cb2"><pre class="sourceCode ocaml"><code class="sourceCode ocaml"><a class="sourceLine" id="cb2-1" data-line-number="1">   <span class="kw">fun</span> map f [] = []</a>
<a class="sourceLine" id="cb2-2" data-line-number="2">     | map f (x :: xs) = f x :: map f xs</a></code></pre></div>
<p>And it gets the type <code>('a -&gt; 'b) -&gt; ('a list -&gt; 'b list)</code>. Aside from minor syntatic differences, this is pretty much identical to what we’d write in Haskell. The value restriction concerns the intersection of these two things. In SML, the following should not compile under the standard</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode ocaml"><code class="sourceCode ocaml"><a class="sourceLine" id="cb3-1" data-line-number="1">    <span class="kw">val</span> x = rev []</a></code></pre></div>
<p>This is because SML requires that all polymorphic <code>val</code> bindings be values! In practice all implementations will do something besides this but we’ll just focus on what the standard says. Now the reason for this value restriction is widely misunderstood. Most people believe that the value restrictions</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode ocaml"><code class="sourceCode ocaml"><a class="sourceLine" id="cb4-1" data-line-number="1">    <span class="kw">val</span> r  = <span class="dt">ref</span> NONE</a>
<a class="sourceLine" id="cb4-2" data-line-number="2">    <span class="kw">val</span> () = r := SOME <span class="dv">1</span></a>
<a class="sourceLine" id="cb4-3" data-line-number="3">    <span class="kw">val</span> _  = case !r <span class="kw">of</span></a>
<a class="sourceLine" id="cb4-4" data-line-number="4">                 SOME s =&gt; s</a>
<a class="sourceLine" id="cb4-5" data-line-number="5">               | NONE   =&gt; <span class="st">&quot;&quot;</span></a></code></pre></div>
<p>This seems to illustrate a pretty big issue for SML! We’re filling in polymorphic reference with one type and unboxing it with a different one! Clearly this would segfault without the value restriction. However, there’s a catch.</p>
<p>SML is based on System F (did you really think I could get through a blog post without some theory?) which is sometimes called the “polymorphic lambda calculus”. It’s the minimal language with polymorphism and functions. In this language there’s a construct for making polymorphic things: Λ.</p>
<p>In this language we write polymorphism explicitly by saying <code>Λ τ. e</code> which has the type <code>∀ t. T</code>. So for example we write the identity function as</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode ocaml"><code class="sourceCode ocaml"><a class="sourceLine" id="cb5-1" data-line-number="1">    id ≡ Λ τ. λ x : τ. x</a>
<a class="sourceLine" id="cb5-2" data-line-number="2">    () = id[<span class="dt">unit</span>] ()</a></code></pre></div>
<p>Now SML (and vanilla Haskell) have a limited subset of the power of Λ. Specifically all these lambdas have to appear at the start of a toplevel term. Meaning that they have to be of the form</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode ocaml"><code class="sourceCode ocaml"><a class="sourceLine" id="cb6-1" data-line-number="1">    <span class="kw">val</span> x = Λ α. Λ β. ... e</a></code></pre></div>
<p>This is called “prenex” form and is what makes type inference for SML possible. Now since we don’t show anyone the hidden Λs it doesn’t make sense to show them the type application that comes with them and SML infers and adds those for us too. What’s particularly interesting is that SML is often formalized as having this property: values start with Λ and are implicitly applied to the appropriate types where used. Even more interesting, how do you suppose we should evaluate a Λ? What for example, should this code do</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode ocaml"><code class="sourceCode ocaml"><a class="sourceLine" id="cb7-1" data-line-number="1">    <span class="kw">val</span> x  = Λ τ. <span class="dt">raise</span>[τ] Fail <span class="co">(* Meaning raise an exception and say</span></a>
<a class="sourceLine" id="cb7-2" data-line-number="2"><span class="co">                                  we have type τ *)</span></a>
<a class="sourceLine" id="cb7-3" data-line-number="3">    <span class="kw">val</span> () = print <span class="st">&quot;I made it here&quot;</span></a>
<a class="sourceLine" id="cb7-4" data-line-number="4">    <span class="kw">val</span> () = x[<span class="dt">unit</span>]</a></code></pre></div>
<p>It seems clear that Λ should be evaluated just like how we evaluate λ, when we apply it. So I’d (and the formalization of SML) would expect this to print <code>&quot;I made it here&quot;</code> <em>before</em> throwing that exception. This might now surprise you just by parallels with code like this</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode ocaml"><code class="sourceCode ocaml"><a class="sourceLine" id="cb8-1" data-line-number="1">    <span class="kw">val</span> x  = fn () =&gt; <span class="dt">raise</span>[τ] Fail</a>
<a class="sourceLine" id="cb8-2" data-line-number="2">    <span class="kw">val</span> () = print <span class="st">&quot;I made it here&quot;</span></a>
<a class="sourceLine" id="cb8-3" data-line-number="3">    <span class="kw">val</span> () = x ()</a></code></pre></div>
<p>However, what about when those lambdas are implicit? In the actual source language of ML our code snippet would be</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode ocaml"><code class="sourceCode ocaml"><a class="sourceLine" id="cb9-1" data-line-number="1">    <span class="kw">val</span> x  = <span class="dt">raise</span> Fail</a>
<a class="sourceLine" id="cb9-2" data-line-number="2">    <span class="kw">val</span> () = print <span class="st">&quot;I made it here&quot;</span></a>
<a class="sourceLine" id="cb9-3" data-line-number="3">    <span class="kw">val</span> () = x[<span class="dt">unit</span>]</a></code></pre></div>
<p>Uhoh, this really looks like it ought to throw an exception but it apparently doesn’t! More worringly, what about when we have something like</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode ocaml"><code class="sourceCode ocaml"><a class="sourceLine" id="cb10-1" data-line-number="1">    <span class="kw">fun</span> die ()  = <span class="dt">raise</span> Fail</a>
<a class="sourceLine" id="cb10-2" data-line-number="2">    <span class="kw">val</span> x = die ()</a>
<a class="sourceLine" id="cb10-3" data-line-number="3">    <span class="kw">val</span> () = print <span class="st">&quot;Made it here&quot;</span></a></code></pre></div>
<p>Since <code>x</code> is never specialized, this doesn’t even throw an error! Yikes! Clearly this is a little confusing. It is however, type safe. Consider our original motivation for the value restriction. With explicit type application</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode ocaml"><code class="sourceCode ocaml"><a class="sourceLine" id="cb11-1" data-line-number="1">    <span class="kw">val</span> r  = Λ τ. <span class="dt">ref</span>[τ] NONE</a>
<a class="sourceLine" id="cb11-2" data-line-number="2">    <span class="kw">val</span> () = r[<span class="dt">int</span>] := SOME <span class="dv">1</span></a>
<a class="sourceLine" id="cb11-3" data-line-number="3">    <span class="kw">val</span> _  = case !(r[<span class="dt">string</span>]) <span class="kw">of</span></a>
<a class="sourceLine" id="cb11-4" data-line-number="4">                 SOME s =&gt; s</a>
<a class="sourceLine" id="cb11-5" data-line-number="5">               | NONE   =&gt; <span class="st">&quot;&quot;</span></a></code></pre></div>
<p>Since the body of this function is run every time we do something with <code>r</code>, we’re just creating a whole bunch of new references in this code! There’s no type safety failure since <code>!(r[string])</code> returns a fresh ref cell, completely different from the one we modified on the line above! This code always runs the <code>NONE</code> case. In fact, if this did the wrong thing it’s just a canary in the coal mine, a symptom of the fact that our system evaluates under (big) lambda binders.</p>
<p>So the value restriction is really not at all about type safety, it’s about comprehensibility. Mostly since the fact that a polymorphic expression is evaluated at usage rather than location is really strange. Most documentation seems to be wrong about this, everyone here seems agree that this is unfortunate but such is life.</p>
<h2 id="the-monomorphism-restriction">The Monomorphism Restriction</h2>
<p>Now let’s talk about the monomorphism restriction. This is better understood but still worth recapping. In Haskell we have type classes. They let us overload function to behave differently on different types. Everyone’s favoriate example is the type class for numbers which let’s us write</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb12-1" data-line-number="1"><span class="ot">    fact ::</span> (<span class="dt">Eq</span> a, <span class="dt">Num</span> a) <span class="ot">=&gt;</span> a <span class="ot">-&gt;</span> a</a>
<a class="sourceLine" id="cb12-2" data-line-number="2">    fact <span class="dv">0</span> <span class="fu">=</span> <span class="dv">1</span></a>
<a class="sourceLine" id="cb12-3" data-line-number="3">    fact n <span class="fu">=</span> n <span class="fu">*</span> fact (n <span class="fu">-</span> <span class="dv">1</span>)</a></code></pre></div>
<p>And this works for all numbers, not just <code>int</code> or something. Under the hood, this works by passing a record of functions like <code>*</code>, <code>fromInteger</code>, and <code>-</code> to make the code work. That <code>=&gt;</code> is really just a sort of function arrow that happens to only take particular “implicit” records as an argument.</p>
<p>Now what do you suppose the most polymorphic type this code is?</p>
<div class="sourceCode" id="cb13"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb13-1" data-line-number="1">    x <span class="fu">=</span> fact <span class="dv">10000</span></a></code></pre></div>
<p>It could potentially work on all numbers so it gets the type</p>
<div class="sourceCode" id="cb14"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb14-1" data-line-number="1"><span class="ot">    x ::</span> (<span class="dt">Num</span> a, <span class="dt">Eq</span> a) <span class="ot">=&gt;</span> a</a></code></pre></div>
<p>However this is really like a function! This means that <code>fact :: Integer</code> and <code>fact :: Int</code> evaluate that computation twice. In fact each time we call <code>fact</code> we supply a new record and end up evaluating again. This is very costly and also very surprising to most folks. After all, why should something that looks like a normal number evaluate every time we use it! The monomorphism restriction is essentially</p>
<ol type="1">
<li>If you have a binding</li>
<li>Whose type is <code>(C1, C2 ...) =&gt; t</code></li>
<li>And has <em>no arguments</em> to the left of the <code>=</code></li>
<li>Don’t generalize it</li>
</ol>
<p>This is intended to keep us from the surprise of evaluating a seemingly fully reduced term twice.</p>
<p>Sound familiar? Just like with the value restriction the whole point of the monomorphism restriction is to prevent a hidden function, either type abstraction or type constraints, from causing us to silently and dangerously duplicate work. While neither of them are essential to type safety: without it some really simple looking pieces of code become exponential.</p>
<h2 id="wrap-up">Wrap Up</h2>
<p>That about covers things. It turns out that both of these restrictions are just patches to cover some surprising areas of the semantics but both are easily understood when you look at the elaborated version. I deliberately went a bit faster through the monomorphism restriction since quite a lot of ink has already been spilled on the subject and unlike the value restriction, most of it is correct :)</p>
<p>As one final note, the way that Haskell handles the monomorphism restriction is precisely how OCaml handles the value restriction: weak polymorphism. Both of them mark the type variables they refuse to generalize as weak type variables. Whenever we first instantiate them to something we go back and retroactively modify the definition to pretend we had used this type all along. In this way, we only evaluate things once but can handle a lot of simple cases of binding a value and using it once.</p>
<p>The more you know.</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Fri, 27 Mar 2015 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2015-03-27-unsafe.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>A Tiny Compiler For A Typed Higher Order Language</title>
    <link>http://jozefg.bitbucket.org/posts/2015-03-24-pcf.html</link>
    <description><![CDATA[<div class="info">
    Posted on March 24, 2015
    
</div>
<div class="info">
    
    Tags: <a href="/tags/compilers.html">compilers</a>, <a href="/tags/types.html">types</a>, <a href="/tags/haskell.html">haskell</a>
    
</div>

<p>Hi folks, the last week or so I was a little tired of schoolwork so I decided to scratch out some fun code. The end result is an <a href="http://github.com/jozefg/pcf">extremely small compiler</a> for a typed, higher order functional language called PCF to C. In this post I’ll explain attempt to explain the whole thing, from front to back :)</p>
<h2 id="whats-pcf">What’s PCF</h2>
<p>First things first, it’s important to define the language we’re compiling. The language, PCF short for “partial computable functions”, is an extremely small language you generally find in a book on programming languages, it originates with Plotkin if I’m not mistaken.</p>
<p>PCF is based around 3 core elements: natural numbers, functions (closures), and general recursion. There are two constants for creating numbers, <code>Zero</code> and <code>Suc</code>. <code>Zero</code> is self explanatory and <code>Suc e</code> is the successor of the natural number <code>e</code> evaluates to. In most programming languages this just means <code>Suc e = 1 + e</code> but <code>+</code> isn’t a primitive in PCF (we can define it as a normal function).</p>
<p>For functions, we have lambdas like you’d find in any functional language. Since PCF includes no polymorphism it’s necessary to annotate the function’s argument with it’s type.</p>
<p>Finally, the weird bit: recursion. In PCF we write recursive things with <code>fix x : τ in e</code>. Here we get to use <code>x</code> in <code>e</code> and we should understand that <code>x</code> “stands for” the whole expression, <code>fix ...</code>. As an example, here’s how we define <code>+</code>.</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb1-1" data-line-number="1">    plus <span class="fu">=</span></a>
<a class="sourceLine" id="cb1-2" data-line-number="2">          fix rec <span class="fu">:</span> nat <span class="ot">-&gt;</span> nat <span class="ot">-&gt;</span> nat <span class="kw">in</span></a>
<a class="sourceLine" id="cb1-3" data-line-number="3">            λ m <span class="fu">:</span> nat<span class="fu">.</span></a>
<a class="sourceLine" id="cb1-4" data-line-number="4">            λ n <span class="fu">:</span> nat<span class="fu">.</span></a>
<a class="sourceLine" id="cb1-5" data-line-number="5">              ifz m {</a>
<a class="sourceLine" id="cb1-6" data-line-number="6">                  <span class="dt">Zero</span>  <span class="ot">=&gt;</span> n</a>
<a class="sourceLine" id="cb1-7" data-line-number="7">                <span class="fu">|</span> <span class="dt">Suc</span> x <span class="ot">=&gt;</span> <span class="dt">Suc</span> (rec x n)</a>
<a class="sourceLine" id="cb1-8" data-line-number="8">              }</a></code></pre></div>
<h2 id="now-lets-compile-it">Now Let’s Compile It</h2>
<p>Now compilation is broken up into a bunch of phases and intermediate languages. Even in this small of a compiler there are 3 (count-em) languages so along with the source and target language there are 5 different languages running around inside of this compiler. Each phase with the exception of typechecking is just translating one intermediate language (IL) into another and in the process making one small modification to the program as a whole.</p>
<h3 id="the-ast">The AST</h3>
<p>This compiler starts with an AST, I have no desire to write a parser for this because parsers make me itchy. Here’s the AST</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb2-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">Ty</span> <span class="fu">=</span> <span class="dt">Arr</span> <span class="dt">Ty</span> <span class="dt">Ty</span></a>
<a class="sourceLine" id="cb2-2" data-line-number="2">            <span class="fu">|</span> <span class="dt">Nat</span></a>
<a class="sourceLine" id="cb2-3" data-line-number="3">            <span class="kw">deriving</span> <span class="dt">Eq</span></a>
<a class="sourceLine" id="cb2-4" data-line-number="4"></a>
<a class="sourceLine" id="cb2-5" data-line-number="5">    <span class="kw">data</span> <span class="dt">Exp</span> a <span class="fu">=</span> <span class="dt">V</span> a</a>
<a class="sourceLine" id="cb2-6" data-line-number="6">               <span class="fu">|</span> <span class="dt">App</span> (<span class="dt">Exp</span> a) (<span class="dt">Exp</span> a)</a>
<a class="sourceLine" id="cb2-7" data-line-number="7">               <span class="fu">|</span> <span class="dt">Ifz</span> (<span class="dt">Exp</span> a) (<span class="dt">Exp</span> a) (<span class="dt">Scope</span> () <span class="dt">Exp</span> a)</a>
<a class="sourceLine" id="cb2-8" data-line-number="8">               <span class="fu">|</span> <span class="dt">Lam</span> <span class="dt">Ty</span> (<span class="dt">Scope</span> () <span class="dt">Exp</span> a)</a>
<a class="sourceLine" id="cb2-9" data-line-number="9">               <span class="fu">|</span> <span class="dt">Fix</span> <span class="dt">Ty</span> (<span class="dt">Scope</span> () <span class="dt">Exp</span> a)</a>
<a class="sourceLine" id="cb2-10" data-line-number="10">               <span class="fu">|</span> <span class="dt">Suc</span> (<span class="dt">Exp</span> a)</a>
<a class="sourceLine" id="cb2-11" data-line-number="11">               <span class="fu">|</span> <span class="dt">Zero</span></a>
<a class="sourceLine" id="cb2-12" data-line-number="12">               <span class="kw">deriving</span> (<span class="dt">Eq</span>, <span class="dt">Functor</span>, <span class="dt">Foldable</span>, <span class="dt">Traversable</span>)</a></code></pre></div>
<p>What’s interesting here is that our AST uses <code>bound</code> to manage variables. Unfortunately there really isn’t time to write both a bound tutorial <em>and</em> a PCF compiler one. I’ve written about using bound before <a href="lambdapi">here</a> otherwise you can just check out the official <a href="http://hackage.haskell.org/package/bound-1.0.4/docs/Bound.html">docs</a>. The important bits here are that <code>Scope () ...</code> binds one variable and that <code>a</code> stands for the free variables in an expression. 3 constructs bind variables here, <code>Ifz</code> for pattern matching, <code>Fix</code> for recursive bindings, and <code>Lam</code> for the argument. Note also that <code>Fix</code> and <code>Lam</code> both must be annotated with a type otherwise stuff like <code>fix x in x</code> and <code>fn x =&gt; x</code> are ambiguous.</p>
<h3 id="type-checking">Type Checking</h3>
<p>First up is type checking. This should be familiar to most people we’ve written a type checker before since PCF is simply typed. We simply have a <code>Map</code> of variables to types. Since we want to go under binders defined using <code>Scope</code> we’ll have to use <code>instantiate</code>. However this demands we be able to create fresh free variables so we don’t accidentally cause clashes. To prevent this we use <a href="http://hackage.haskell.org/package/monad-gen">monad-gen</a> to generate fresh free variables.</p>
<p>To warm up, here’s a helper function to check that an expression has a particular type. This uses the more general <code>typeCheck</code> function which actually produces the type of an expression.</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb3-1" data-line-number="1">    <span class="kw">type</span> <span class="dt">TyM</span> a <span class="fu">=</span> <span class="dt">MaybeT</span> (<span class="dt">Gen</span> a)</a>
<a class="sourceLine" id="cb3-2" data-line-number="2"></a>
<a class="sourceLine" id="cb3-3" data-line-number="3"><span class="ot">    assertTy ::</span> (<span class="dt">Enum</span> a, <span class="dt">Ord</span> a) <span class="ot">=&gt;</span> <span class="dt">M.Map</span> a <span class="dt">Ty</span> <span class="ot">-&gt;</span> <span class="dt">Exp</span> a <span class="ot">-&gt;</span> <span class="dt">Ty</span> <span class="ot">-&gt;</span> <span class="dt">TyM</span> a ()</a>
<a class="sourceLine" id="cb3-4" data-line-number="4">    assertTy env e t <span class="fu">=</span> (<span class="fu">==</span> t) <span class="fu">&lt;$&gt;</span> typeCheck env e <span class="fu">&gt;&gt;=</span> guard</a></code></pre></div>
<p>This type checks the variable in an environment (something that stores the types of all of the free variables). Once it receives that it compares it to the type we expected and chucks the resulting boolean into guard. This code is used in places like <code>Ifz</code> where we happen to know that the first expression has the type <code>Nat</code>.</p>
<p>Now on to the main code, <code>typeCheck</code></p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" data-line-number="1"><span class="ot">    typeCheck ::</span> (<span class="dt">Enum</span> a, <span class="dt">Ord</span> a) <span class="ot">=&gt;</span> <span class="dt">M.Map</span> a <span class="dt">Ty</span> <span class="ot">-&gt;</span> <span class="dt">Exp</span> a <span class="ot">-&gt;</span> <span class="dt">TyM</span> a <span class="dt">Ty</span></a>
<a class="sourceLine" id="cb4-2" data-line-number="2">    typeCheck _   <span class="dt">Zero</span> <span class="fu">=</span> return <span class="dt">Nat</span></a>
<a class="sourceLine" id="cb4-3" data-line-number="3">    typeCheck env (<span class="dt">Suc</span> e) <span class="fu">=</span> assertTy env e <span class="dt">Nat</span> <span class="fu">&gt;&gt;</span> return <span class="dt">Nat</span></a></code></pre></div>
<p>The first two cases for <code>typeCheck</code> are nice and straightforward. All we if we get a <code>Zero</code> then it has type <code>Nat</code>. If we get a <code>Suc e</code> we assert that <code>e</code> is an integer and then the whole thing has the type <code>Nat</code>.</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb5-1" data-line-number="1">    typeCheck env (<span class="dt">V</span> a) <span class="fu">=</span> <span class="dt">MaybeT</span> <span class="fu">.</span> return <span class="fu">$</span> M.lookup a env</a></code></pre></div>
<p>For variables we just look things up in the environment. Since this returns a <code>Maybe</code> it’s nice and easy to just jam it into our <code>MaybeT</code>.</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb6-1" data-line-number="1">    typeCheck env (<span class="dt">App</span> f a) <span class="fu">=</span> typeCheck env f <span class="fu">&gt;&gt;=</span> \<span class="kw">case</span></a>
<a class="sourceLine" id="cb6-2" data-line-number="2">      <span class="dt">Arr</span> fTy tTy <span class="ot">-&gt;</span> assertTy env a fTy <span class="fu">&gt;&gt;</span> return tTy</a>
<a class="sourceLine" id="cb6-3" data-line-number="3">      _ <span class="ot">-&gt;</span> mzero</a></code></pre></div>
<p>Application is a little more interesting. We recurse over the function and make sure it has an actual function type. If it does, we assert the argument has the argument type and return the domain. If it doesn’t have a function type, we just fail.</p>
<pre><code>    typeCheck env (Lam t bind) = do
      v &lt;- gen
      Arr t &lt;$&gt; typeCheck (M.insert v t env) (instantiate1 (V v) bind)
    typeCheck env (Fix t bind) = do
      v &lt;- gen
      assertTy (M.insert v t env) (instantiate1 (V v) bind) t
      return t</code></pre>
<p>Type checking lambdas and fixpoints is quite similar. In both cases we generate a fresh variable to unravel the binder with. We know what type this variable is supposed to have because we required explicit annotations so we add that to the map constituting our environment. Here’s where they diverge.</p>
<p>For a fixpoint we want to make sure that the body has the type as we said it would so we use <code>assertTy</code>. For a lambda we infer the body type and return a function from the given argument type to the body type.</p>
<pre><code>    typeCheck env (Ifz i t e) = do
      assertTy env i Nat
      ty &lt;- typeCheck env t
      v &lt;- gen
      assertTy (M.insert v Nat env) (instantiate1 (V v) e) ty
      return ty</code></pre>
<p>For <code>Ifz</code> we want to ensure that we actually are casing on a <code>Nat</code> so we use <code>assertTy</code>. Next we figure out what type the zero branch returns and make sure that the else branch has the same type.</p>
<p>All in all this type checker is not particularly fascinating since all we have are simple types. Things get a bit more interesting with <a href="2015-02-28-type-inference.html">polymorphism</a>. I’d suggest looking at that if you want to see a more interesting type checker.</p>
<h3 id="closure-conversion">Closure Conversion</h3>
<p>Now for our first interesting compilation phase, closure conversion. In this phase we make closures explicit by annotating lambdas and fixpoints with the variables that they close over. Those variables are then explicitly bound in the scope of the lambda. With these changes, our new syntax tree looks like this</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb9-1" data-line-number="1">    <span class="co">-- Invariant, Clos only contains VCs, can&#39;t be enforced statically due</span></a>
<a class="sourceLine" id="cb9-2" data-line-number="2">    <span class="co">-- to annoying monad instance</span></a>
<a class="sourceLine" id="cb9-3" data-line-number="3">    <span class="kw">type</span> <span class="dt">Clos</span> a <span class="fu">=</span> [<span class="dt">ExpC</span> a]</a>
<a class="sourceLine" id="cb9-4" data-line-number="4"></a>
<a class="sourceLine" id="cb9-5" data-line-number="5">    <span class="kw">data</span> <span class="dt">ExpC</span> a <span class="fu">=</span> <span class="dt">VC</span> a</a>
<a class="sourceLine" id="cb9-6" data-line-number="6">                <span class="fu">|</span> <span class="dt">AppC</span> (<span class="dt">ExpC</span> a) (<span class="dt">ExpC</span> a)</a>
<a class="sourceLine" id="cb9-7" data-line-number="7">                <span class="fu">|</span> <span class="dt">LamC</span> <span class="dt">Ty</span> (<span class="dt">Clos</span> a) (<span class="dt">Scope</span> <span class="dt">Int</span> <span class="dt">ExpC</span> a)</a>
<a class="sourceLine" id="cb9-8" data-line-number="8">                <span class="fu">|</span> <span class="dt">FixC</span> <span class="dt">Ty</span> (<span class="dt">Clos</span> a) (<span class="dt">Scope</span> <span class="dt">Int</span> <span class="dt">ExpC</span> a)</a>
<a class="sourceLine" id="cb9-9" data-line-number="9">                <span class="fu">|</span> <span class="dt">IfzC</span> (<span class="dt">ExpC</span> a) (<span class="dt">ExpC</span> a) (<span class="dt">Scope</span> () <span class="dt">ExpC</span> a)</a>
<a class="sourceLine" id="cb9-10" data-line-number="10">                <span class="fu">|</span> <span class="dt">SucC</span> (<span class="dt">ExpC</span> a)</a>
<a class="sourceLine" id="cb9-11" data-line-number="11">                <span class="fu">|</span> <span class="dt">ZeroC</span></a>
<a class="sourceLine" id="cb9-12" data-line-number="12">                <span class="kw">deriving</span> (<span class="dt">Eq</span>, <span class="dt">Functor</span>, <span class="dt">Foldable</span>, <span class="dt">Traversable</span>)</a></code></pre></div>
<p>The interesting parts are the additions of <code>Clos</code> and the fact that the <code>Scope</code> for a lambda and a fixpoint now binds an arbitrary number of variables instead of just one. Here if a lambda or fixpoint binds <code>n</code> variables, the first <code>n - 1</code> are stored in the <code>Clos</code> and the last one is the “argument”. Closure conversion is thus just the process of converting an <code>Exp</code> to an <code>ExpC</code>.</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb10-1" data-line-number="1"><span class="ot">    closConv ::</span> <span class="dt">Ord</span> a <span class="ot">=&gt;</span> <span class="dt">Exp</span> a <span class="ot">-&gt;</span> <span class="dt">Gen</span> a (<span class="dt">ExpC</span> a)</a>
<a class="sourceLine" id="cb10-2" data-line-number="2">    closConv (<span class="dt">V</span> a) <span class="fu">=</span> return (<span class="dt">VC</span> a)</a>
<a class="sourceLine" id="cb10-3" data-line-number="3">    closConv <span class="dt">Zero</span> <span class="fu">=</span> return <span class="dt">ZeroC</span></a>
<a class="sourceLine" id="cb10-4" data-line-number="4">    closConv (<span class="dt">Suc</span> e) <span class="fu">=</span> <span class="dt">SucC</span> <span class="fu">&lt;$&gt;</span> closConv e</a>
<a class="sourceLine" id="cb10-5" data-line-number="5">    closConv (<span class="dt">App</span> f a) <span class="fu">=</span> <span class="dt">AppC</span> <span class="fu">&lt;$&gt;</span> closConv f <span class="fu">&lt;*&gt;</span> closConv a</a>
<a class="sourceLine" id="cb10-6" data-line-number="6">    closConv (<span class="dt">Ifz</span> i t e) <span class="fu">=</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb10-7" data-line-number="7">      v <span class="ot">&lt;-</span> gen</a>
<a class="sourceLine" id="cb10-8" data-line-number="8">      e&#39; <span class="ot">&lt;-</span> abstract1 v <span class="fu">&lt;$&gt;</span> closConv (instantiate1 (<span class="dt">V</span> v) e)</a>
<a class="sourceLine" id="cb10-9" data-line-number="9">      <span class="dt">IfzC</span> <span class="fu">&lt;$&gt;</span> closConv i <span class="fu">&lt;*&gt;</span> closConv t <span class="fu">&lt;*&gt;</span> return e&#39;</a></code></pre></div>
<p>Most of the cases here are just recursing and building things back up applicatively. There’s the moderately interesting case where we instantiate the else branch of an <code>Ifz</code> with a fresh variable and <em>then</em> recurse, but the interesting cases are for fixpoints and lambdas. Since they’re completely identical we only present the case for <code>Fix</code>.</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb11-1" data-line-number="1">    closConv (<span class="dt">Fix</span> t bind) <span class="fu">=</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb11-2" data-line-number="2">      v <span class="ot">&lt;-</span> gen</a>
<a class="sourceLine" id="cb11-3" data-line-number="3">      body <span class="ot">&lt;-</span> closConv (instantiate1 (<span class="dt">V</span> v) bind)</a>
<a class="sourceLine" id="cb11-4" data-line-number="4">      <span class="kw">let</span> freeVars <span class="fu">=</span> S.toList <span class="fu">.</span> S.delete v <span class="fu">$</span> foldMap S.singleton body</a>
<a class="sourceLine" id="cb11-5" data-line-number="5">          rebind v&#39; <span class="fu">=</span> elemIndex v&#39; freeVars <span class="fu">&lt;|&gt;</span></a>
<a class="sourceLine" id="cb11-6" data-line-number="6">                      (guard (v&#39; <span class="fu">==</span> v) <span class="fu">*&gt;</span> (<span class="dt">Just</span> <span class="fu">$</span> length freeVars))</a>
<a class="sourceLine" id="cb11-7" data-line-number="7">      return <span class="fu">$</span> <span class="dt">FixC</span> t (map <span class="dt">VC</span> freeVars) (abstract rebind body)</a></code></pre></div>
<p>There’s a lot going on here but it boils down into three parts.</p>
<ol start="0" type="1">
<li>Recurse under the binder</li>
<li>Gather all the free variables in the body</li>
<li>Rebind the body together so that all the free variables map to their position in the closure and the argument is <code>n</code> where <code>n</code> is the number of free variables.</li>
</ol>
<p>The first is accomplished in much the same way as in the above cases. To gather the number of free variables all we need to is use the readily available notion of a monoid on sets. The whole process is just <code>foldMap S.singleton</code>! There’s one small catch: we don’t want to put the argument into the list of variables we close over so we carefully delete it from the closure. We then convert it to a list which gives us an actual <code>Clos a</code>. Now for the third step we have <code>rebind</code>.</p>
<p><code>rebind</code> maps a free variable to <code>Maybe Int</code>. It maps a free variable to it’s binding occurrence it has one here. This boils down to using <code>elemIndex</code> to look up somethings position in the <code>Clos</code> we just built up. We also have a special case for when the variable we’re looking at is the “argument” of the function we’re fixing. In this case we want to map it to the last thing we’re binding, which is just <code>length n</code>. To capture the “try this and then that” semantics we use the alternative instance for <code>Maybe</code> which works wonderfully.</p>
<p>With this, we’ve removed implicit closures from our language: one of the passes on our way to C.</p>
<h3 id="lambda-lifting">Lambda Lifting</h3>
<p>Next up we remove both fixpoints and lambdas from being expressions. We want them to have an explicit binding occurrence because we plan to completely remove them from expressions soon. In order to do this, we define a language with lambdas and fixpoints explicitly declared in let expressions. The process of converting from <code>ExpC</code> to this new language is called “lambda lifting” because we’re lifting things into let bindings.</p>
<p>Here’s our new language.</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb12-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">BindL</span> a <span class="fu">=</span> <span class="dt">RecL</span> <span class="dt">Ty</span> [<span class="dt">ExpL</span> a] (<span class="dt">Scope</span> <span class="dt">Int</span> <span class="dt">ExpL</span> a)</a>
<a class="sourceLine" id="cb12-2" data-line-number="2">                 <span class="fu">|</span> <span class="dt">NRecL</span> <span class="dt">Ty</span> [<span class="dt">ExpL</span> a] (<span class="dt">Scope</span> <span class="dt">Int</span> <span class="dt">ExpL</span> a)</a>
<a class="sourceLine" id="cb12-3" data-line-number="3">                 <span class="kw">deriving</span> (<span class="dt">Eq</span>, <span class="dt">Functor</span>, <span class="dt">Foldable</span>, <span class="dt">Traversable</span>)</a>
<a class="sourceLine" id="cb12-4" data-line-number="4">    <span class="kw">data</span> <span class="dt">ExpL</span> a <span class="fu">=</span> <span class="dt">VL</span> a</a>
<a class="sourceLine" id="cb12-5" data-line-number="5">                <span class="fu">|</span> <span class="dt">AppL</span> (<span class="dt">ExpL</span> a) (<span class="dt">ExpL</span> a)</a>
<a class="sourceLine" id="cb12-6" data-line-number="6">                <span class="fu">|</span> <span class="dt">LetL</span> [<span class="dt">BindL</span> a] (<span class="dt">Scope</span> <span class="dt">Int</span> <span class="dt">ExpL</span> a)</a>
<a class="sourceLine" id="cb12-7" data-line-number="7">                <span class="fu">|</span> <span class="dt">IfzL</span> (<span class="dt">ExpL</span> a) (<span class="dt">ExpL</span> a) (<span class="dt">Scope</span> () <span class="dt">ExpL</span> a)</a>
<a class="sourceLine" id="cb12-8" data-line-number="8">                <span class="fu">|</span> <span class="dt">SucL</span> (<span class="dt">ExpL</span> a)</a>
<a class="sourceLine" id="cb12-9" data-line-number="9">                <span class="fu">|</span> <span class="dt">ZeroL</span></a>
<a class="sourceLine" id="cb12-10" data-line-number="10">                <span class="kw">deriving</span> (<span class="dt">Eq</span>, <span class="dt">Functor</span>, <span class="dt">Foldable</span>, <span class="dt">Traversable</span>)</a></code></pre></div>
<p>Much here is the same except we’ve romved both lambdas and fixpoints and replaced them with <code>LetL</code>. <code>LetL</code> works over bindings which are either recursive (<code>Fix</code>) or nonrecursive (<code>Lam</code>). Lambda lifting in this compiler is rather simplistic in how it lifts lambdas: we just boost everything one level up and turn</p>
<div class="sourceCode" id="cb13"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb13-1" data-line-number="1">    λ (x <span class="fu">:</span> τ)<span class="fu">.</span> <span class="fu">...</span></a></code></pre></div>
<p>into</p>
<div class="sourceCode" id="cb14"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb14-1" data-line-number="1">    <span class="kw">let</span> foo <span class="fu">=</span> λ (x <span class="fu">:</span> τ)<span class="fu">.</span> <span class="fu">...</span></a>
<a class="sourceLine" id="cb14-2" data-line-number="2">    <span class="kw">in</span> foo</a></code></pre></div>
<p>Just like before, this procedure is captured by transforming an <code>ExpC</code> into an <code>ExpL</code>.</p>
<div class="sourceCode" id="cb15"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb15-1" data-line-number="1"><span class="ot">    llift ::</span> <span class="dt">Eq</span> a <span class="ot">=&gt;</span> <span class="dt">ExpC</span> a <span class="ot">-&gt;</span> <span class="dt">Gen</span> a (<span class="dt">ExpL</span> a)</a>
<a class="sourceLine" id="cb15-2" data-line-number="2">    llift (<span class="dt">VC</span> a) <span class="fu">=</span> return (<span class="dt">VL</span> a)</a>
<a class="sourceLine" id="cb15-3" data-line-number="3">    llift <span class="dt">ZeroC</span> <span class="fu">=</span> return <span class="dt">ZeroL</span></a>
<a class="sourceLine" id="cb15-4" data-line-number="4">    llift (<span class="dt">SucC</span> e) <span class="fu">=</span> <span class="dt">SucL</span> <span class="fu">&lt;$&gt;</span> llift e</a>
<a class="sourceLine" id="cb15-5" data-line-number="5">    llift (<span class="dt">AppC</span> f a) <span class="fu">=</span> <span class="dt">AppL</span> <span class="fu">&lt;$&gt;</span> llift f <span class="fu">&lt;*&gt;</span> llift a</a>
<a class="sourceLine" id="cb15-6" data-line-number="6">    llift (<span class="dt">IfzC</span> i t e) <span class="fu">=</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb15-7" data-line-number="7">      v <span class="ot">&lt;-</span> gen</a>
<a class="sourceLine" id="cb15-8" data-line-number="8">      e&#39; <span class="ot">&lt;-</span> abstract1 v <span class="fu">&lt;$&gt;</span> llift (instantiate1 (<span class="dt">VC</span> v) e)</a>
<a class="sourceLine" id="cb15-9" data-line-number="9">      <span class="dt">IfzL</span> <span class="fu">&lt;$&gt;</span> llift i <span class="fu">&lt;*&gt;</span> llift t <span class="fu">&lt;*&gt;</span> return e&#39;</a></code></pre></div>
<p>Just like in <code>closConv</code> we start with a lot of very boring and trivial “recurse and build back up” cases. These handle everything but the cases where we actually convert constructs into a <code>LetL</code>.</p>
<p>Once again, the interesting cases are pretty much identical. Let’s look at the case for <code>LamC</code> for variety.</p>
<div class="sourceCode" id="cb16"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb16-1" data-line-number="1">    llift (<span class="dt">LamC</span> t clos bind) <span class="fu">=</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb16-2" data-line-number="2">      vs <span class="ot">&lt;-</span> replicateM (length clos <span class="fu">+</span> <span class="dv">1</span>) gen</a>
<a class="sourceLine" id="cb16-3" data-line-number="3">      body <span class="ot">&lt;-</span> llift <span class="fu">$</span> instantiate (<span class="dt">VC</span> <span class="fu">.</span> (<span class="fu">!!</span>) vs) bind</a>
<a class="sourceLine" id="cb16-4" data-line-number="4">      clos&#39; <span class="ot">&lt;-</span> mapM llift clos</a>
<a class="sourceLine" id="cb16-5" data-line-number="5">      <span class="kw">let</span> bind&#39; <span class="fu">=</span> abstract (flip elemIndex vs) body</a>
<a class="sourceLine" id="cb16-6" data-line-number="6">      return (<span class="dt">LetL</span> [<span class="dt">NRecL</span> t clos&#39; bind&#39;] trivLetBody)</a></code></pre></div>
<p>Here we first generate a bunch of fresh variables and unbind the body of our lambda. We then recurse on it. We also have to recurse across all of our closed over arguments but since those are variables we know that should be pretty trivial (why do we know this?). Once we’ve straightened out the body and the closure all we do is transform the lambda into a trivial let expression as shown above. Here <code>trivLetBody</code> is.</p>
<div class="sourceCode" id="cb17"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb17-1" data-line-number="1"><span class="ot">    trivLetBody ::</span> <span class="dt">Scope</span> <span class="dt">Int</span> <span class="dt">ExpL</span> a</a>
<a class="sourceLine" id="cb17-2" data-line-number="2">    trivLetBody <span class="fu">=</span> fromJust <span class="fu">.</span> closed <span class="fu">.</span> abstract (const <span class="fu">$</span> <span class="dt">Just</span> <span class="dv">0</span>) <span class="fu">$</span> <span class="dt">VL</span> ()</a></code></pre></div>
<p>Which is just a body that returns the first thing bound in the let. With this done, we’ve pretty much transformed our expression language to C. In order to get rid of the nesting, we want to make one more simplification before we actually generate C.</p>
<h3 id="c-with-expression">C-With-Expression</h3>
<p>C-With-Expressions is our next intermediate language. It has no notion of nested functions or of fixpoints. I suppose now I should finally fess up to why I keep talking about fixpoints and functions as if they’re the same and why this compiler is handling them identically. The long and short of it is that fixpoints are really a combination of a “fixed point combinator” and a function. Really when we say</p>
<div class="sourceCode" id="cb18"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb18-1" data-line-number="1">    fix x <span class="fu">:</span> τ <span class="kw">in</span> <span class="fu">...</span></a></code></pre></div>
<p>It’s as if we had sayed</p>
<div class="sourceCode" id="cb19"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb19-1" data-line-number="1">    <span class="dt">F</span> (λ x <span class="fu">:</span> τ<span class="fu">.</span> <span class="fu">...</span>)</a></code></pre></div>
<p>Where <code>F</code> is a magical constant with the type</p>
<div class="sourceCode" id="cb20"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb20-1" data-line-number="1">    <span class="dt">F</span><span class="ot"> ::</span> (a <span class="ot">-&gt;</span> a) <span class="ot">-&gt;</span> a</a></code></pre></div>
<p><code>F</code> calculates the fixpoint of a function. This means that <code>f (F f) = F f</code>. This formula underlies all recursive bindings (in Haskell too!). In the compiler we basically compile a <code>Fix</code> to a closure (the runtime representation of a function) and pass it to a C function <code>fixedPoint</code> which actually calculates the fixed point. Now it might seem dubious that a function has a fixed point. After all, it would seem that there’s no <code>x</code> so that <code>(λ (x : nat). suc x) = x</code> right? Well the key is to think of these functions as not ranging over just values in our language, but a domain where infinite loops (bottom values) are also represented. In the above equation, the solution is that <code>x</code> should be bottom, an infinite loop. That’s why</p>
<div class="sourceCode" id="cb21"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb21-1" data-line-number="1">    fix x <span class="fu">:</span> nat <span class="kw">in</span> suc x</a></code></pre></div>
<p>should loop! There’s actual some wonderful math going on here about how computable functions are continuous functions over a domain and that we can always calculate the least fixed point of them in this manner. The curious reader is encouraged to check out domain theory.</p>
<p>Anyways, so that’s why I keep handling fixpoints and lambdas in the same way, because to me a fixpoint <em>is</em> a lambda + some magic. This is going to become very clear in C-With-Expressions (<code>FauxC</code> from now on) because we’re going to promote both sorts of let bindings to the same thing, a <code>FauxC</code> toplevel function. Without further ado, here’s the next IL.</p>
<div class="sourceCode" id="cb22"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb22-1" data-line-number="1">    <span class="co">-- Invariant: the Integer part of a FauxCTop is a globally unique</span></a>
<a class="sourceLine" id="cb22-2" data-line-number="2">    <span class="co">-- identifier that will be used as a name for that binding.</span></a>
<a class="sourceLine" id="cb22-3" data-line-number="3">    <span class="kw">type</span> <span class="dt">NumArgs</span> <span class="fu">=</span> <span class="dt">Int</span></a>
<a class="sourceLine" id="cb22-4" data-line-number="4">    <span class="kw">data</span> <span class="dt">BindTy</span> <span class="fu">=</span> <span class="dt">Int</span> <span class="fu">|</span> <span class="dt">Clos</span> <span class="kw">deriving</span> <span class="dt">Eq</span></a>
<a class="sourceLine" id="cb22-5" data-line-number="5"></a>
<a class="sourceLine" id="cb22-6" data-line-number="6">    <span class="kw">data</span> <span class="dt">FauxCTop</span> a <span class="fu">=</span> <span class="dt">FauxCTop</span> <span class="dt">Integer</span> <span class="dt">NumArgs</span> (<span class="dt">Scope</span> <span class="dt">Int</span> <span class="dt">FauxC</span> a)</a>
<a class="sourceLine" id="cb22-7" data-line-number="7">                    <span class="kw">deriving</span> (<span class="dt">Eq</span>, <span class="dt">Functor</span>, <span class="dt">Foldable</span>, <span class="dt">Traversable</span>)</a>
<a class="sourceLine" id="cb22-8" data-line-number="8">    <span class="kw">data</span> <span class="dt">BindFC</span> a <span class="fu">=</span> <span class="dt">NRecFC</span> <span class="dt">Integer</span> [<span class="dt">FauxC</span> a]</a>
<a class="sourceLine" id="cb22-9" data-line-number="9">                  <span class="fu">|</span> <span class="dt">RecFC</span> <span class="dt">BindTy</span> <span class="dt">Integer</span> [<span class="dt">FauxC</span> a]</a>
<a class="sourceLine" id="cb22-10" data-line-number="10">                  <span class="kw">deriving</span> (<span class="dt">Eq</span>, <span class="dt">Functor</span>, <span class="dt">Foldable</span>, <span class="dt">Traversable</span>)</a>
<a class="sourceLine" id="cb22-11" data-line-number="11">    <span class="kw">data</span> <span class="dt">FauxC</span> a <span class="fu">=</span> <span class="dt">VFC</span> a</a>
<a class="sourceLine" id="cb22-12" data-line-number="12">                 <span class="fu">|</span> <span class="dt">AppFC</span> (<span class="dt">FauxC</span> a) (<span class="dt">FauxC</span> a)</a>
<a class="sourceLine" id="cb22-13" data-line-number="13">                 <span class="fu">|</span> <span class="dt">IfzFC</span> (<span class="dt">FauxC</span> a) (<span class="dt">FauxC</span> a) (<span class="dt">Scope</span> () <span class="dt">FauxC</span> a)</a>
<a class="sourceLine" id="cb22-14" data-line-number="14">                 <span class="fu">|</span> <span class="dt">LetFC</span> [<span class="dt">BindFC</span> a] (<span class="dt">Scope</span> <span class="dt">Int</span> <span class="dt">FauxC</span> a)</a>
<a class="sourceLine" id="cb22-15" data-line-number="15">                 <span class="fu">|</span> <span class="dt">SucFC</span> (<span class="dt">FauxC</span> a)</a>
<a class="sourceLine" id="cb22-16" data-line-number="16">                 <span class="fu">|</span> <span class="dt">ZeroFC</span></a>
<a class="sourceLine" id="cb22-17" data-line-number="17">                 <span class="kw">deriving</span> (<span class="dt">Eq</span>, <span class="dt">Functor</span>, <span class="dt">Foldable</span>, <span class="dt">Traversable</span>)</a></code></pre></div>
<p>The big difference is that we’ve lifted things out of let bindings. They now contain references to some global function instead of actually having the value right there. We also tag fixpoints as either fixing an <code>Int</code> or a <code>Clos</code>. The reasons for this will be apparent in a bit.</p>
<p>Now for the conversion. We don’t just have a function from <code>ExpL</code> to <code>FauxC</code> because we also want to make note of all the nested lets we’re lifting out of the program. Thus we use <code>WriterT</code> to gather a lift of toplevel functions as we traverse the program. Other than that this is much like what we’ve seen before.</p>
<pre><code>    type FauxCM a = WriterT [FauxCTop a] (Gen a)

    fauxc :: ExpL Integer -&gt; FauxCM Integer (FauxC Integer)
    fauxc (VL a) = return (VFC a)
    fauxc (AppL f a) = AppFC &lt;$&gt; fauxc f &lt;*&gt; fauxc a
    fauxc ZeroL = return ZeroFC
    fauxc (SucL e) = SucFC &lt;$&gt; fauxc e
    fauxc (IfzL i t e) = do
      v &lt;- gen
      e&#39; &lt;- abstract1 v &lt;$&gt; fauxc (instantiate1 (VL v) e)
      IfzFC &lt;$&gt; fauxc i &lt;*&gt; fauxc t &lt;*&gt; return e&#39;</code></pre>
<p>In the first couple cases we just recurse. as we’ve seen before. Things only get interesting once we get to <code>LetL</code></p>
<div class="sourceCode" id="cb24"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb24-1" data-line-number="1">    fauxc (<span class="dt">LetL</span> binds e) <span class="fu">=</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb24-2" data-line-number="2">      binds&#39; <span class="ot">&lt;-</span> mapM liftBinds binds</a>
<a class="sourceLine" id="cb24-3" data-line-number="3">      vs <span class="ot">&lt;-</span> replicateM (length binds) gen</a>
<a class="sourceLine" id="cb24-4" data-line-number="4">      body <span class="ot">&lt;-</span> fauxc <span class="fu">$</span> instantiate (<span class="dt">VL</span> <span class="fu">.</span> (<span class="fu">!!</span>) vs) e</a>
<a class="sourceLine" id="cb24-5" data-line-number="5">      <span class="kw">let</span> e&#39; <span class="fu">=</span> abstract (flip elemIndex vs) body</a>
<a class="sourceLine" id="cb24-6" data-line-number="6">      return (<span class="dt">LetFC</span> binds&#39; e&#39;)</a></code></pre></div>
<p>In this case we recurse with the function <code>liftBinds</code> across all the bindings, then do what we’ve done before and unwrap the body of the let and recurse in it. So the meat of this transformation is in <code>liftBinds</code>.</p>
<div class="sourceCode" id="cb25"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb25-1" data-line-number="1">      <span class="kw">where</span> liftBinds (<span class="dt">NRecL</span> t clos bind) <span class="fu">=</span> lifter <span class="dt">NRecFC</span> clos bind</a>
<a class="sourceLine" id="cb25-2" data-line-number="2">            liftBinds (<span class="dt">RecL</span> t clos bind) <span class="fu">=</span> lifter (<span class="dt">RecFC</span> <span class="fu">$</span> bindTy t) clos bind</a>
<a class="sourceLine" id="cb25-3" data-line-number="3">            lifter bindingConstr clos bind <span class="fu">=</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb25-4" data-line-number="4">              guid <span class="ot">&lt;-</span> gen</a>
<a class="sourceLine" id="cb25-5" data-line-number="5">              vs <span class="ot">&lt;-</span> replicateM (length clos <span class="fu">+</span> <span class="dv">1</span>) gen</a>
<a class="sourceLine" id="cb25-6" data-line-number="6">              body <span class="ot">&lt;-</span> fauxc <span class="fu">$</span> instantiate (<span class="dt">VL</span> <span class="fu">.</span> (<span class="fu">!!</span>) vs) bind</a>
<a class="sourceLine" id="cb25-7" data-line-number="7">              <span class="kw">let</span> bind&#39; <span class="fu">=</span> abstract (flip elemIndex vs) body</a>
<a class="sourceLine" id="cb25-8" data-line-number="8">              tell [<span class="dt">FauxCTop</span> guid (length clos <span class="fu">+</span> <span class="dv">1</span>) bind&#39;]</a>
<a class="sourceLine" id="cb25-9" data-line-number="9">              bindingConstr guid <span class="fu">&lt;$&gt;</span> mapM fauxc clos</a>
<a class="sourceLine" id="cb25-10" data-line-number="10">            bindTy (<span class="dt">Arr</span> _ _) <span class="fu">=</span> <span class="dt">Clos</span></a>
<a class="sourceLine" id="cb25-11" data-line-number="11">            bindTy <span class="dt">Nat</span> <span class="fu">=</span> <span class="dt">Int</span></a></code></pre></div>
<p>To lift a binding all we do is generate a globally unique identifier for the toplevel. Once we have that we that we can unwrap the particular binding we’re looking at. This is going to comprise the body of the <code>TopC</code> function we’re building. Since we need it to be <code>FauxC</code> code as well we recurse on it. No we have a bunch of faux-C code for the body of the toplevel function. We then just repackage the body up into a binding (a <code>FauxCTop</code> needs one) and use <code>tell</code> to make a note of it. Once we’ve done that we return the stripped down let binding that just remembers the guid that we created for the toplevel function.</p>
<p>In an example, this code transformers</p>
<div class="sourceCode" id="cb26"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb26-1" data-line-number="1">    <span class="kw">let</span> x <span class="fu">=</span> λ (x <span class="fu">:</span> τ)<span class="fu">.</span> <span class="fu">...</span> <span class="kw">in</span></a>
<a class="sourceLine" id="cb26-2" data-line-number="2">      <span class="fu">...</span> x <span class="fu">...</span></a></code></pre></div>
<p>into</p>
<div class="sourceCode" id="cb27"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb27-1" data-line-number="1">    <span class="dt">TOP</span> <span class="fu">=</span> λ (x <span class="fu">:</span> τ)<span class="fu">.</span> <span class="fu">...</span></a>
<a class="sourceLine" id="cb27-2" data-line-number="2">    <span class="kw">let</span> x <span class="fu">=</span> <span class="dt">TOP</span> <span class="kw">in</span></a>
<a class="sourceLine" id="cb27-3" data-line-number="3">      <span class="fu">...</span> x <span class="fu">...</span></a></code></pre></div>
<p>With this done our language is now 80% of the way to C!</p>
<h3 id="converting-to-ssa-ish-c">Converting To SSA-ish C</h3>
<p>Converting our faux-C language to actual C has one complication: C doesn’t have <code>let</code> expressions. Given this, we have to flatten out a faux-C expression so we can turn a let expression into a normal C declaration. This conversion is <em>almost</em> a conversion to single static assignment form, SSA. I say almost because there’s precisely one place where we break the single assignment discipline. This is just because it seemed rather pointless to me to introduce an SSA IL with φ just so I could compile it to C. YMMV.</p>
<p>This is what LLVM uses for its intermediate language and because of this I strongly suspect regearing this compiler to target LLVM should be pretty trivial.</p>
<p>Now we’re using a library called <a href="http://hackage.haskell.org/package/c-dsl">c-dsl</a> to make generating the C less painful, but there’s still a couple of things we’d like to add. First of all, all our names our integers so we have <code>i2e</code> and <code>i2d</code> for converting an integer into a C declaration or an expression.</p>
<div class="sourceCode" id="cb28"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb28-1" data-line-number="1"><span class="ot">    i2d ::</span> <span class="dt">Integer</span> <span class="ot">-&gt;</span> <span class="dt">CDeclr</span></a>
<a class="sourceLine" id="cb28-2" data-line-number="2">    i2d <span class="fu">=</span> fromString <span class="fu">.</span> (<span class="ch">&#39;_&#39;</span><span class="fu">:</span>) <span class="fu">.</span> show</a>
<a class="sourceLine" id="cb28-3" data-line-number="3"></a>
<a class="sourceLine" id="cb28-4" data-line-number="4"><span class="ot">    i2e ::</span> <span class="dt">Integer</span> <span class="ot">-&gt;</span> <span class="dt">CExpr</span></a>
<a class="sourceLine" id="cb28-5" data-line-number="5">    i2e <span class="fu">=</span> var <span class="fu">.</span> fromString <span class="fu">.</span> (<span class="ch">&#39;_&#39;</span><span class="fu">:</span>) <span class="fu">.</span> show</a></code></pre></div>
<p>We also have a shorthand for the type of all expression in our generated C code.</p>
<div class="sourceCode" id="cb29"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb29-1" data-line-number="1"><span class="ot">    taggedTy ::</span> <span class="dt">CDeclSpec</span></a>
<a class="sourceLine" id="cb29-2" data-line-number="2">    taggedTy <span class="fu">=</span> <span class="dt">CTypeSpec</span> <span class="st">&quot;tagged_ptr&quot;</span></a></code></pre></div>
<p>Finally, we have our writer monad and helper function for implementing the SSA conversion. We write C99 block items and use <code>tellDecl</code> binding an expression to a fresh variable and then we return this variable.</p>
<div class="sourceCode" id="cb30"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb30-1" data-line-number="1">    <span class="kw">type</span> <span class="dt">RealCM</span> <span class="fu">=</span> <span class="dt">WriterT</span> [<span class="dt">CBlockItem</span>] (<span class="dt">Gen</span> <span class="dt">Integer</span>)</a>
<a class="sourceLine" id="cb30-2" data-line-number="2"></a>
<a class="sourceLine" id="cb30-3" data-line-number="3"><span class="ot">    tellDecl ::</span> <span class="dt">CExpr</span> <span class="ot">-&gt;</span> <span class="dt">RealCM</span> <span class="dt">CExpr</span></a>
<a class="sourceLine" id="cb30-4" data-line-number="4">    tellDecl e <span class="fu">=</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb30-5" data-line-number="5">      i <span class="ot">&lt;-</span> gen</a>
<a class="sourceLine" id="cb30-6" data-line-number="6">      tell [<span class="dt">CBlockDecl</span> <span class="fu">$</span> decl taggedTy (i2d i) <span class="fu">$</span> <span class="dt">Just</span> e]</a>
<a class="sourceLine" id="cb30-7" data-line-number="7">      return (i2e i)</a></code></pre></div>
<p>Next we have the conversion procedure. Most of this is pretty straightforward because we shell out to calls in the runtime system for all the hardwork. We have the following RTS functions</p>
<ul>
<li><code>mkZero</code>, create a zero value</li>
<li><code>inc</code>, increment an integer value</li>
<li><code>dec</code>, decrement an integer value</li>
<li><code>apply</code>, apply a closure to an argument</li>
<li><code>mkClos</code>, make a closure with a closing over some values</li>
<li><code>EMPTY</code>, an empty pointer, useful for default values</li>
<li><code>isZero</code>, check if something is zero</li>
<li><code>fixedPoint</code>, find the fixed point of function</li>
<li><code>INT_SIZE</code>, the size of the runtime representation of an integer</li>
<li><code>CLOS_SIZE</code>, the size of the runtime representation of a closure</li>
</ul>
<p>Most of this code is therefore just converting the expression to SSA form and using the RTS functions to shell do the appropriate computation at each step. Note that c-dsl provides a few overloaded string instances and so to generate the C code to apply a function we just use <code>&quot;foo&quot;#[1, &quot;these&quot;, &quot;are&quot;, &quot;arguments&quot;]</code>.</p>
<p>The first few cases for conversion are nice and straightforward.</p>
<div class="sourceCode" id="cb31"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb31-1" data-line-number="1"><span class="ot">    realc ::</span> <span class="dt">FauxC</span> <span class="dt">CExpr</span> <span class="ot">-&gt;</span> <span class="dt">RealCM</span> <span class="dt">CExpr</span></a>
<a class="sourceLine" id="cb31-2" data-line-number="2">    realc (<span class="dt">VFC</span> e) <span class="fu">=</span> return e</a>
<a class="sourceLine" id="cb31-3" data-line-number="3">    realc (<span class="dt">AppFC</span> f a) <span class="fu">=</span> (<span class="st">&quot;apply&quot;</span> <span class="fu">#</span>) <span class="fu">&lt;$&gt;</span> mapM realc [f, a] <span class="fu">&gt;&gt;=</span> tellDecl</a>
<a class="sourceLine" id="cb31-4" data-line-number="4">    realc <span class="dt">ZeroFC</span> <span class="fu">=</span> tellDecl <span class="fu">$</span> <span class="st">&quot;mkZero&quot;</span> <span class="fu">#</span> []</a>
<a class="sourceLine" id="cb31-5" data-line-number="5">    realc (<span class="dt">SucFC</span> e) <span class="fu">=</span> realc e <span class="fu">&gt;&gt;=</span> tellDecl <span class="fu">.</span> (<span class="st">&quot;inc&quot;</span><span class="fu">#</span>) <span class="fu">.</span> (<span class="fu">:</span>[])</a></code></pre></div>
<p>We take advantage of the fact that <code>realc</code> returns it’s result and we can almost make this look like the applicative cases we had before. One particularly slick case is how <code>Suc</code> works. We compute the value of <code>e</code> and apply the result to <code>suc</code>. We then feed this expression into <code>tellDecl</code> which binds it to a fresh variable and returns the variable. Haskell is pretty slick.</p>
<div class="sourceCode" id="cb32"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb32-1" data-line-number="1">    realc (<span class="dt">IfzFC</span> i t e) <span class="fu">=</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb32-2" data-line-number="2">      outi <span class="ot">&lt;-</span> realc i</a>
<a class="sourceLine" id="cb32-3" data-line-number="3">      deci <span class="ot">&lt;-</span> tellDecl (<span class="st">&quot;dec&quot;</span> <span class="fu">#</span> [outi])</a>
<a class="sourceLine" id="cb32-4" data-line-number="4">      <span class="kw">let</span> e&#39; <span class="fu">=</span> instantiate1 (<span class="dt">VFC</span> deci) e</a>
<a class="sourceLine" id="cb32-5" data-line-number="5">      (outt, blockt) <span class="ot">&lt;-</span> lift <span class="fu">.</span> runWriterT <span class="fu">$</span> (realc t)</a>
<a class="sourceLine" id="cb32-6" data-line-number="6">      (oute, blocke) <span class="ot">&lt;-</span> lift <span class="fu">.</span> runWriterT <span class="fu">$</span> (realc e&#39;)</a>
<a class="sourceLine" id="cb32-7" data-line-number="7">      out <span class="ot">&lt;-</span> tellDecl <span class="st">&quot;EMPTY&quot;</span></a>
<a class="sourceLine" id="cb32-8" data-line-number="8">      <span class="kw">let</span> branch b tempOut <span class="fu">=</span></a>
<a class="sourceLine" id="cb32-9" data-line-number="9">            <span class="dt">CCompound</span> [] (b <span class="fu">++</span> [<span class="dt">CBlockStmt</span> <span class="fu">.</span> liftE <span class="fu">$</span> out <span class="ot">&lt;-</span><span class="fu">-</span> tempOut]) undefNode</a>
<a class="sourceLine" id="cb32-10" data-line-number="10">          ifStat <span class="fu">=</span></a>
<a class="sourceLine" id="cb32-11" data-line-number="11">            cifElse (<span class="st">&quot;isZero&quot;</span><span class="fu">#</span>[outi]) (branch blockt outt) (branch blocke oute)</a>
<a class="sourceLine" id="cb32-12" data-line-number="12">      tell [<span class="dt">CBlockStmt</span> ifStat]</a>
<a class="sourceLine" id="cb32-13" data-line-number="13">      return out</a></code></pre></div>
<p>In this next case we’re translating <code>Ifz</code>. For this we obviously need to compute the value of <code>i</code>. We do that by recursing and storing the result in <code>outi</code>. Now we want to be able to use 1 less than the value of <code>i</code> in case we go into the successor branch. This is done by calling <code>dec</code> on <code>outi</code> and storing it for later.</p>
<p>Next we do something a little odd. We recurse on the branches of <code>Ifz</code> but we definitely don’t want to compute both of them! So we can’t just use a normal recursive call. If we did they’d be added to the block we’re building up in the writer monad. So we use <code>lift . runWriterT</code> to give us back the blocks <em>without</em> adding them to the current one we’re building. Now it’s just a matter of generating the appropriate <code>if</code> statement.</p>
<p>To do this we add one instruction to the end of both branches, to assign to some output variable. This ensures that no matter which branch we go down we’ll end up the result in one place. This is also the one place where we are no longer doing SSA. Properly speaking we should write this with a φ but who has time for that? :)</p>
<p>Finally we build add the if statement and the handful of declarations that precede it to our block. Now for the last case.</p>
<div class="sourceCode" id="cb33"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb33-1" data-line-number="1">    realc (<span class="dt">LetFC</span> binds bind) <span class="fu">=</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb33-2" data-line-number="2">      bindings <span class="ot">&lt;-</span> mapM goBind binds</a>
<a class="sourceLine" id="cb33-3" data-line-number="3">      realc <span class="fu">$</span> instantiate (<span class="dt">VFC</span> <span class="fu">.</span> (bindings <span class="fu">!!</span>)) bind</a>
<a class="sourceLine" id="cb33-4" data-line-number="4">      <span class="kw">where</span> sizeOf <span class="dt">Int</span> <span class="fu">=</span> <span class="st">&quot;INT_SIZE&quot;</span></a>
<a class="sourceLine" id="cb33-5" data-line-number="5">            sizeOf <span class="dt">Clos</span> <span class="fu">=</span> <span class="st">&quot;CLOS_SIZE&quot;</span></a>
<a class="sourceLine" id="cb33-6" data-line-number="6">            goBind (<span class="dt">NRecFC</span> i cs) <span class="fu">=</span></a>
<a class="sourceLine" id="cb33-7" data-line-number="7">              (<span class="st">&quot;mkClos&quot;</span> <span class="fu">#</span>) <span class="fu">&lt;$&gt;</span> (i2e i <span class="fu">:</span>) <span class="fu">.</span> (fromIntegral (length cs) <span class="fu">:</span>)</a>
<a class="sourceLine" id="cb33-8" data-line-number="8">                           <span class="fu">&lt;$&gt;</span> mapM realc cs</a>
<a class="sourceLine" id="cb33-9" data-line-number="9">                           <span class="fu">&gt;&gt;=</span> tellDecl</a>
<a class="sourceLine" id="cb33-10" data-line-number="10">            goBind (<span class="dt">RecFC</span> t i cs) <span class="fu">=</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb33-11" data-line-number="11">              f <span class="ot">&lt;-</span> (<span class="st">&quot;mkClos&quot;</span> <span class="fu">#</span>) <span class="fu">&lt;$&gt;</span> (i2e i <span class="fu">:</span>) <span class="fu">.</span> (fromIntegral (length cs) <span class="fu">:</span>)</a>
<a class="sourceLine" id="cb33-12" data-line-number="12">                                <span class="fu">&lt;$&gt;</span> mapM realc cs</a>
<a class="sourceLine" id="cb33-13" data-line-number="13">                                <span class="fu">&gt;&gt;=</span> tellDecl</a>
<a class="sourceLine" id="cb33-14" data-line-number="14">              tellDecl (<span class="st">&quot;fixedPoint&quot;</span><span class="fu">#</span>[f, sizeOf t])</a></code></pre></div>
<p>For our last case we have to deal with lets. For this we simply traverse all the bindings which are now flat and then flatten the expression under the binder. When we <code>mapM</code> over the bindings we actually get back a list of all the expressions each binding evaluated to. This is perfect for use with <code>instantiate</code> making the actual toplevel function quite pleasant. <code>goBind</code> is slightly less so.</p>
<p>In the nonrecursive case all we have to do is create a closure. So <code>goBind</code> of a nonrecursive binding shells out to <code>mkClos</code>. This <code>mkClos</code> is applied to the number of closed over expressions as well as all the closed over expressions. This is because <code>mkClos</code> is variadic. Finally we shove the result into <code>tellDecl</code> as usual. For a recursive call there’s a slight difference, namely after doing all of that we apply <code>fixedPoint</code> to the output <em>and</em> to the size of the type of the thing we’re fixing. This is why we kept types around for these bindings! With them we can avoid dragging the size with every value since we know it statically.</p>
<p>Next, we have a function for converting a faux C function into an actual function definition. This is the function that we use <code>realc</code> in.</p>
<pre class="haskel"><code>    topc :: FauxCTop CExpr -&gt; Gen Integer CFunDef
    topc (FauxCTop i numArgs body) = do
      binds &lt;- gen
      let getArg = (!!) (args (i2e binds) numArgs)
      (out, block) &lt;- runWriterT . realc $ instantiate getArg body
      return $
        fun [taggedTy] (&#39;_&#39; : show i) [decl taggedTy $ ptr (i2d binds)] $
          CCompound [] (block ++ [CBlockStmt . creturn $ out]) undefNode
      where indexArg binds i = binds ! fromIntegral i
            args binds na = map (VFC . indexArg binds) [0..na - 1]</code></pre>
<p>This isn’t the most interesting function. We have one array of arguments to our C function, and then we unbind the body of the FauxC function by indexing into this array. It’s not explicitly stated in the code but the array contains the closed over expressions for the first n - 1 entries and the nth is the actual argument to the function. This is inline with how the variables are actually bound in the body of the function which makes unwrapping the body to index into the argument array very simple. We then call <code>realc</code> which transforms our faux-c expression into a block of actual C code. We add one last statement to the end of the block that returns the final outputted variable. All that’s left to do is bind it up into a C function and call it a day.</p>
<h3 id="putting-it-all-together">Putting It All Together</h3>
<p>Finally, at the end of it all we have a function from expression to <code>Maybe CTranslUnit</code>, a C program.</p>
<div class="sourceCode" id="cb35"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb35-1" data-line-number="1"><span class="ot">    compile ::</span> <span class="dt">Exp</span> <span class="dt">Integer</span> <span class="ot">-&gt;</span> <span class="dt">Maybe</span> <span class="dt">CTranslUnit</span></a>
<a class="sourceLine" id="cb35-2" data-line-number="2">    compile e <span class="fu">=</span> runGen <span class="fu">.</span> runMaybeT <span class="fu">$</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb35-3" data-line-number="3">      assertTy M.empty e <span class="dt">Nat</span></a>
<a class="sourceLine" id="cb35-4" data-line-number="4">      funs <span class="ot">&lt;-</span> lift <span class="fu">$</span> pipe e</a>
<a class="sourceLine" id="cb35-5" data-line-number="5">      return <span class="fu">.</span> transUnit <span class="fu">.</span> map export <span class="fu">$</span> funs</a>
<a class="sourceLine" id="cb35-6" data-line-number="6">      <span class="kw">where</span> pipe e <span class="fu">=</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb35-7" data-line-number="7">              simplified <span class="ot">&lt;-</span> closConv e <span class="fu">&gt;&gt;=</span> llift</a>
<a class="sourceLine" id="cb35-8" data-line-number="8">              (main, funs) <span class="ot">&lt;-</span> runWriterT <span class="fu">$</span> fauxc simplified</a>
<a class="sourceLine" id="cb35-9" data-line-number="9">              i <span class="ot">&lt;-</span> gen</a>
<a class="sourceLine" id="cb35-10" data-line-number="10">              <span class="kw">let</span> topMain <span class="fu">=</span> <span class="dt">FauxCTop</span> i <span class="dv">1</span> (abstract (const <span class="dt">Nothing</span>) main)</a>
<a class="sourceLine" id="cb35-11" data-line-number="11">                  funs&#39; <span class="fu">=</span> map (i2e <span class="fu">&lt;$&gt;</span>) (funs <span class="fu">++</span> [topMain])</a>
<a class="sourceLine" id="cb35-12" data-line-number="12">              (<span class="fu">++</span> [makeCMain i]) <span class="fu">&lt;$&gt;</span> mapM topc funs&#39;</a>
<a class="sourceLine" id="cb35-13" data-line-number="13">            makeCMain entry <span class="fu">=</span></a>
<a class="sourceLine" id="cb35-14" data-line-number="14">              fun [intTy] <span class="st">&quot;main&quot;</span>[] <span class="fu">$</span> hBlock [<span class="st">&quot;call&quot;</span><span class="fu">#</span>[i2e entry]]</a></code></pre></div>
<p>This combines all the previous compilation passes together. First we typecheck and ensure that the program is a <code>Nat</code>. Then we closure convert it and immediately lambda lift. This simplified program is then fed into <code>fauxc</code> giving a <code>fauxc</code> expression for main and a bunch of functions called by main. We wrap up the main expression in a function that ignores all it’s arguments. We then map <code>realc</code> over all of these fauxc functions. This gives us actual C code. Finally, we take on a trivial C main to call the generated code and return the whole thing.</p>
<p>And that’s our PCF compiler.</p>
<h2 id="wrap-up">Wrap Up</h2>
<p>Well if you’ve made it this far congratulations. We just went through a full compiler from a typed higher order language to C. Along the way we ended up implementing</p>
<ol type="1">
<li>A Type Checker</li>
<li>Closure Conversion</li>
<li>Lambda Lifting</li>
<li>Conversion to Faux-C</li>
<li>SSA Conversion</li>
</ol>
<p>If you’d like to fiddle a bit more, some fun project might be</p>
<ol type="1">
<li>Writing type checkers for all the intermediate languages. They’re all typeable except perhaps Faux-C</li>
<li>Implement compilation to LLVM instead of C. As I said before, this shouldn’t be awful</li>
</ol>
<p>Cheers,</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Tue, 24 Mar 2015 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2015-03-24-pcf.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Worlds in Twelf</title>
    <link>http://jozefg.bitbucket.org/posts/2015-03-07-worlds.html</link>
    <description><![CDATA[<div class="info">
    Posted on March  7, 2015
    
</div>
<div class="info">
    
    Tags: <a href="/tags/twelf.html">twelf</a>, <a href="/tags/types.html">types</a>
    
</div>

<p>In this post I wanted to focus on one particular thing in Twelf: <code>%worlds</code> declarations. They seems to be the most mysterious. I’ve had a couple people tell me that they just blindly stick <code>%worlds () (x _ _ _)</code> before every total and pray which is a little concerning..</p>
<p>In this post hopefully we’ll remove some of the “compile-n-pray” from using Twelf code.</p>
<h2 id="what-is-worlds">What is %worlds</h2>
<p>In Twelf we’re interested in proving theorems. These theorems are basically proven by some chunk of code that looks like this.</p>
<pre class="twelf"><code>    my-cool-tyfam : with -&gt; some -&gt; cool -&gt; args -&gt; type.
    %mode my-cool-tyfam +A +B +C -D.

    some         : ... -&gt; my-cool-tyfam A B C D.
    constructors : ... -&gt; my-cool-tyfam A B C D.

    %worlds (...) (my-cool-tyfam _ _ _ _).
    %total (T) (my-cool-tyfam T _ _ _).</code></pre>
<p>What’s interesting here is the 3 directives we needed</p>
<ul>
<li><code>%mode</code> to specify which arguments of the type family are universally quantified and which are existentially qualified in our theorem. This specifies the “direction” of the type family, + arguments are inputs and - arguments are outputs.</li>
<li><code>%total</code> which actually goes and proves the theorem by induction on the canonical forms of the term in the parens.</li>
<li><code>%worlds</code> which specifies the set of contexts to check the totality in. Note that a world is simply a set of contexts.</li>
</ul>
<p>The one we’re interested in talking about here is <code>%worlds</code>. Everything we want to call <code>%total</code> has to have on of these and as mentioned above it specifies the contexts to check the theorem in. Remember that total is proven by induction over the canonical forms. One of the canonical forms for every type is off the form</p>
<blockquote>
<p>For some <code>x : ty ∈ Γ</code>, then <code>x</code> is a canonical form of <code>ty</code>.</p>
</blockquote>
<p>This is a little different than in other languages. We could usually just invert upon something in the context. That’s not the case in Twelf, we have to handle variables parametrically (this is critical to admitting HOAS and similar). This means that means we have to extremely careful about what’s in Γ lest we accidentally introduce something canonical form of <code>ty</code> without any additional information about it. The worlds specification tells us about the forms <code>Γ</code> can take. Twelf allows us to specify sets of contexts that are “regular”.</p>
<p>So for example remember how <code>plus</code> might be defined.</p>
<pre class="twelf"><code>    plus : nat -&gt; nat -&gt; nat -&gt; type.
    %mode plus +N +M -P.

    plus/z : plus z N N.
    plus/s : plus N M P -&gt; plus (s N) M (s P).</code></pre>
<p>This is total in the empty context. If we added some <code>b : nat</code> to our context then we have no way of showing it is either a <code>s</code> or a <code>z</code>! This means that there’s a missing case for variables of type <code>nat</code> in our code. In order to exclude this impossible case we just assert that we only care about <code>plus</code>’s totality in the empty context. This is what the <code>%worlds</code> specification for <code>plus</code> stipulates</p>
<pre class="twelf"><code>    %worlds () (plus _ _ _).</code></pre>
<p>should be read as “<code>plus</code> should only be considered in the empty context” so the only canonical forms of <code>plus</code> are those specified as constants in our signature. This sort of specification is what we want for most vanilla uses of Twelf.</p>
<p>For most cases we want to be proving theorems in the empty context because we do nothing to extend the context in our constructors. That’s not to say that we <em>can’t</em> specify some nonempty world. We can specify a world where there is a <code>b : nat</code>, but if such a <code>b</code> must appear we have a derivation <code>{a} plus b a z</code>. This way when Twelf goes to check the canonical forms case for something in our context, <code>b : nat</code>, it knows that there’s a derivation that precisely matches what we need. I’ll circle back to this in a second, but first we have to talk about how to specify fancier worlds.</p>
<h2 id="block-and-fancier-worlds">%block and Fancier Worlds</h2>
<p>In Twelf there’s some special syntax for specifying worlds. Basically we can specify a template for some part of the world, called a block. A world declaration is just a conglomeration of blocks and Twelf will interpret this as a world of contexts in which each block may appear zero or more times.</p>
<p>In Twelf code we specify a block with the following syntax</p>
<pre class="twelf"><code>    %block block_name : block {a : ty} ... {b : ty&#39;}.</code></pre>
<p>This specifies that if there is an <code>a : ty</code> in the context, it’s going to be accompanied by a bunch of other stuff including a <code>b : ty'</code>. Some blocks are pretty trivial. For example, if we wanted to allow <code>plus</code> to be defined in a context with some <code>a : nat</code> in the context we might say</p>
<pre class="twelf"><code>    %block random_nat : block {b : nat}.
    %worlds (random_nat) (plus _ _ _).</code></pre>
<p>This doesn’t work though. If we ask Twelf to check totality it’ll get angry and say</p>
<pre><code>Coverage error --- missing cases:
{#random_nat:{b:nat}} {X1:nat} {X2:nat} |- plus #random_nat_b X1 X2.</code></pre>
<p>In human,</p>
<blockquote>
<p>You awful person Danny! You’re missing the case where you have to random integers and the random natural number <code>b</code> from the random_nat block and we want to compute <code>plus b X X'</code>.</p>
</blockquote>
<p>Now there are a few things to do here. The saner person would probably just say “Oh, I clearly don’t want to try to prove this theorem in a nonempty context”. <em>Or</em> we can wildly add things to our context in order to patch this hole. In this case, we need some proof that about adding <code>b</code> to other stuff. Let’s supplement our block</p>
<pre><code>    %block random_nat : block {b : nat}{_:{a} plus b a z}</code></pre>
<p>Such a context is pretty idiotic though since there isn’t a natural number that can satisfy it. It is however enough to sate the totality checker.</p>
<pre class="twelf"><code>    %total (T) (plus T _ _).</code></pre>
<p>For a non contrived for example let’s discuss where interesting worlds come into play: with higher order abstract syntax. When we use HOAS we end up embedding the LF function space in our terms. This is important because it means as we go to prove theorems about it we end up recursing on a term <em>under</em> an honest to goodness LF lambda. This means we extend the context at some points in our proof and we can’t just prove theorems in the empty context!</p>
<p>To see this in action here’s an embedding of the untyped lambda calculus in LF</p>
<pre class="twelf"><code>    term : type.
    lam  : (term -&gt; term) -&gt; term.
    app  : term -&gt; term -&gt; term.</code></pre>
<p>Now let’s say we want to determine how many binders are in a lambda term. We start by defining our relation</p>
<pre class="twelf"><code>    nbinds : term -&gt; nat -&gt; type.
    %mode nbinds +T -N.</code></pre>
<p>We set this type family up so that it has one input (the term) and one output (a nat representing the number of binders). We have two cases to deal with here</p>
<pre class="twelf"><code>    nbinds/lam : nbinds (lam F) (s N)
                  &lt;- ({x : term} nbinds (F x) N).
    nbinds/app : nbinds (app F A) O
                  &lt;- nbinds F N1
                  &lt;- nbinds A N2
                  &lt;- plus N1 N2 O.</code></pre>
<p>In the <code>lam</code> case we recurse under the binder. This is the interesting thing here, we stick the recurse call under a pi binder. This gives us access to some term <code>x</code> which we apply the LF function two. This code in effect says &quot;If for all terms <code>F</code> has <code>N</code> binders then <code>lam F</code> has <code>N + 1</code> binders. The <code>app</code> case just sums the two binders.</p>
<p>We can try to world check this in only the empty context but this fails with</p>
<pre><code>Error:
While checking constant nbinds/lam:
World violation for family nbinds: {x:term} &lt;/: 1</code></pre>
<p>This says that even though we promised never to extend the LF context we did just that! To fix this we must have a fancier world. We create a block which just talks about adding a term to the context.</p>
<pre class="twelf"><code>    %block nbinds_block : block {x : term}.
    %worlds (nbinds_block) (nbinds _ _).</code></pre>
<p>This world checks but there’s another issue lurking about. Let’s try to ask Twelf to prove totality.</p>
<pre class="twelf"><code>    %total (T) (nbinds T _).</code></pre>
<p>This spits out the error message</p>
<pre><code>Coverage error --- missing cases:
{#nbinds_block:{x:term}} {X1:nat} |- nbinds #nbinds_block_x X1.</code></pre>
<p>This is the same error as before! Now that we’ve extended our context with a term we need to somehow be able to tell Twelf the height of that term. This smacks of the slightly fishy type of <code>nbinds/lam</code>: it’s meaning is that <code>F x</code> has the height <code>N</code> for <em>any</em> term <code>x</code>. This seems a little odd, why doesn’t the height of a functions body depend on its argument? We really ought to be specifying that whatever this <code>x</code> is, we know its height is <code>z</code>. This makes our new code</p>
<pre class="twelf"><code>    nbinds/lam : nbinds (lam F) (s N)
                 &lt;- ({x : term}{_ : nbinds x z} nbinds (F x) N).</code></pre>
<p>Now we specify that the height of <code>x</code> is zero. This means we have to change our block to</p>
<pre class="twelf"><code>    %block nbinds_block : block {x : term}{_ : nbinds x z}.</code></pre>
<p>With this modification else everything goes through unmodified. For fun, we can ask Twelf to actually compute some toy examples.</p>
<pre class="twelf"><code>    %solve deriv : nbinds (lam ([x] (lam [y] x))) N.</code></pre>
<p>This gives back that <code>deriv : nbinds (lam ([x] lam ([y] x))) (s (s z))</code> as we’d hope. It’s always fun to run our proofs.</p>
<h2 id="conclusion">Conclusion</h2>
<p>Hopefully that clears up some of the mystery of worlds in Twelf. Happily this doesn’t come up for a lot of simple uses of Twelf. As far as I know the entire constructive logic course at CMU sidesteps the issue with a quick “Stick <code>%worlds () (...)</code> before each totality check”.</p>
<p>It is completely invaluable if you’re doing anything under binders which turns out to be necessary for most interesting proofs about languages with binders. If nothing else, the more you know..</p>
<p>Those who enjoyed this post might profit from Dan Licata and Bob Harper’s paper on <a href="http://www.cs.cmu.edu/~drl/pubs/hl07mechanizing/hl07mechanizing.pdf">mechanizing metatheory</a>.</p>
<p>Cheers,</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Sat, 07 Mar 2015 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2015-03-07-worlds.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>An Explanation of Type Inference for ML/Haskell</title>
    <link>http://jozefg.bitbucket.org/posts/2015-02-28-type-inference.html</link>
    <description><![CDATA[<div class="info">
    Posted on February 28, 2015
    
</div>
<div class="info">
    
    Tags: <a href="/tags/sml.html">sml</a>, <a href="/tags/haskell.html">haskell</a>, <a href="/tags/types.html">types</a>
    
</div>

<p>A couple of days ago I wrote a <a href="http://github.com/jozefg/hm">small implementation of a type inferencer</a> for a mini ML language. It turns out there are very few explanations of how to do this properly and the ones that exist tend to be the really naive, super exponential algorithm. I wrote the algorithm in SML but nothing should be unfamiliar to the average Haskeller.</p>
<p>Type inference breaks down into essentially 2 components</p>
<ol type="1">
<li>Constraint Generation</li>
<li>Unification</li>
</ol>
<p>We inspect the program we’re trying to infer a type for and generate a bunch of statements (constraints) which are of the form</p>
<blockquote>
<p>This type is equal to this type</p>
</blockquote>
<p>These types have “unification variables” in them. These <strong>aren’t normal ML/Haskell type variables</strong>. They’re generated by the compiler, for the compiler, and will eventually be filled in with either</p>
<ol type="1">
<li>A rigid polymorphic variable</li>
<li>A normal concrete type</li>
</ol>
<p>They should be thought of as holes in an otherwise normal type. For example, if we’re looking at the expression</p>
<pre class="sml"><code>   f a</code></pre>
<p>We first just say that <code>f : 'f</code> where <code>'f</code> is one of those unification variables I mentioned. Next we say that <code>a : 'a</code>. Since we’re apply <code>f</code> to <code>a</code> we can generate the constraints that</p>
<pre><code>&#39;f ~ &#39;x -&gt; &#39;y
&#39;a ~ &#39;x</code></pre>
<p>Since we can only apply things with of the form <code>_ -&gt; _</code>. We then unify these constraints to produce <code>f : 'a -&gt; 'x</code> and <code>a : 'a</code>. We’d then using the surrounding constraints to produce more information about what exactly <code>'a</code> and <code>'x</code> might be. If this was all the constraints we had we’d then “generalize” <code>'a</code> and <code>'x</code> to be normal type variables, making our expression have the type <code>x</code> where <code>f : a -&gt; x</code> and <code>a : a</code>.</p>
<p>Now onto some specifics</p>
<h2 id="set-up">Set Up</h2>
<p>In order to actually talk about type inference we first have to define our language. We have the abstract syntax tree:</p>
<pre class="sml"><code>    type tvar = int
    local val freshSource = ref 0 in
    fun fresh () : tvar =
        !freshSource before freshSource := !freshSource + 1
    end


    datatype monotype = TBool
                      | TArr of monotype * monotype
                      | TVar of tvar
    datatype polytype = PolyType of int list * monotype

    datatype exp = True
                 | False
                 | Var of int
                 | App of exp * exp
                 | Let of exp * exp
                 | Fn of exp
                 | If of exp * exp * exp</code></pre>
<p>First we have type variables which are globally unique integers. To give us a method for actually producing them we have <code>fresh</code> which uses a ref-cell to never return the same result twice. This is probably surprising to Haskellers: SML isn’t purely functional and frankly this is less noisy than using something like <code>monad-gen</code>.</p>
<p>From there we have mono-types. These are normal ML types without any polymorphism. There are type/unification variables, booleans, and functions. Polytypes are just monotypes with an extra <code>forall</code> at the front. This is where we get polymorphism from. A polytype binds a number of type variables, stored in this representation as an int list. There is one ambiguity here, when looking at a variable it’s not clear whether it’s supposed to be a type variable (bound in a forall) and a unification variable. The idea is that we never ever inspect a type bound under a forall <em>except</em> when we’re converting it to a monotype with fresh unification variables in place of all of the bound variables. Thus, when inferring a type, every variable we come across is a unification variable.</p>
<p>Finally, we have expressions. Aside form the normal constants, we have variables, lambdas, applications, and if. The way we represent variables here is with <a href="http://www.wikiwand.com/en/De_Bruijn_index">DeBruijn variables</a>. A variable is a number that tells you how many binders are between it and where it was bound. For example, <code>const</code> would be written <code>Fn (Fn (Var 1))</code> in this representation.</p>
<p>With this in mind we define some helpful utility functions. When type checking, we have a context full of information. The two facts we know are</p>
<pre class="sml"><code>    datatype info = PolyTypeVar of polytype
                  | MonoTypeVar of monotype

    type context = info list</code></pre>
<p>Where the ith element of a context indicates the piece of information we know about the ith DeBruijn variable. We’ll also need to substitute a type variable for a type. We also want to be able to find out all the free variables in a type.</p>
<pre class="sml"><code>    fun subst ty&#39; var ty =
        case ty of
            TVar var&#39; =&gt; if var = var&#39; then ty&#39; else TVar var&#39;
          | TArr (l, r) =&gt; TArr (subst ty&#39; var l, subst ty&#39; var r)
          | TBool =&gt; TBool

    fun freeVars t =
        case t of
            TVar v =&gt; [v]
          | TArr (l, r) =&gt; freeVars l @ freeVars r
          | TBool =&gt; []</code></pre>
<p>Both of these functions just recurse over types and do some work at the variable case. Note that <code>freeVars</code> can contain duplicates, this turns out not to be important in all cases except one: <code>generalizeMonoType</code>. The basic idea is that given a monotype with a bunch of unification variables and a surrounding context, figure out which variables can be bound up in a polymorphic type. If they don’t appear in the surrounding context, we generalize them by binding them in a new poly type’s forall spot.</p>
<pre class="sml"><code>    fun dedup [] = []
      | dedup (x :: xs) =
        if List.exists (fn y =&gt; x = y) xs
        then dedup xs
        else x :: dedup xs

    fun generalizeMonoType ctx ty =
        let fun notMem xs x = List.all (fn y =&gt; x &lt;&gt; y) xs
            fun free (MonoTypeVar m) = freeVars m
              | free (PolyTypeVar (PolyType (bs, m))) =
                List.filter (notMem bs) (freeVars m)

            val ctxVars = List.concat (List.map free ctx)
            val polyVars = List.filter (notMem ctxVars) (freeVars ty)
        in PolyType (dedup polyVars, ty) end</code></pre>
<p>Here the bulk of the code is deciding whether or not a variable is free in the surrounding context using <code>free</code>. It looks at a piece of info to determine what variables occur in it. We then accumulate all of these variables into <code>cxtVars</code> and use this list to decide what to generalize.</p>
<p>Next we need to take a polytype to a monotype. This is the specialization of a polymorphic type that we love and use when we use <code>map</code> on a function from <code>int -&gt; double</code>. This works by taking each bound variable and replacing it with a fresh unification variables. This is nicely handled by folds!</p>
<pre class="sml"><code>    fun mintNewMonoType (PolyType (ls, ty)) =
        foldl (fn (v, t) =&gt; subst (TVar (fresh ())) v t) ty ls</code></pre>
<p>Last but not least, we have a function to take a context and a variable and give us a monotype which corresponds to it. This may produce a new monotype if we think the variable has a polytype.</p>
<pre class="sml"><code>    exception UnboundVar of int
    fun lookupVar var ctx =
        case List.nth (ctx, var) handle Subscript =&gt; raise UnboundVar var of
            PolyTypeVar pty =&gt; mintNewMonoType pty
          | MonoTypeVar mty =&gt; mty</code></pre>
<p>For the sake of nice error messages, we also throw <code>UnboundVar</code> instead of just subscript in the error case. Now that we’ve gone through all of the utility functions, on to unification!</p>
<h2 id="unification">Unification</h2>
<p>A large part of this program is basically “I’ll give you a list of constraints and you give me the solution”. The program to solve these proceeds by pattern matching on the constraints.</p>
<p>In the empty case, we have no constraints so we give back the empty solution.</p>
<pre class="sml"><code>    fun unify [] = []</code></pre>
<p>In the next case we actually have to look at what constraint we’re trying to solve.</p>
<pre class="sml"><code>      | unify (c :: constrs) =
        case c of</code></pre>
<p>If we’re lucky, we’re just trying to unify <code>TBool</code> with <code>TBool</code>, this does nothing since these types have no variables and are equal. In this case we just recurse.</p>
<pre class="sml"><code>       (TBool, TBool) =&gt; unify constrs</code></pre>
<p>If we’ve got two function types, we just constrain their domains and ranges to be the same and continue on unifying things.</p>
<pre class="sml"><code>     | (TArr (l, r), TArr (l&#39;, r&#39;)) =&gt; unify ((l, l&#39;) :: (r, r&#39;) :: constrs)</code></pre>
<p>Now we have to deal with finding a variable. We definitely want to avoid adding <code>(TVar v, TVar v)</code> to our solution, so we’ll have a special case for trying to unify two variables.</p>
<pre class="sml"><code>     | (TVar i, TVar j) =&gt;
       if i = j
       then unify constrs
       else addSol i (TVar j) (unify (substConstrs (TVar j) i constrs))</code></pre>
<p>This is our first time actually adding something to our solution so there’s several new elements here. The first is this function <code>addSol</code>. It’s defined as</p>
<pre class="sml"><code>    fun addSol v ty sol = (v, applySol sol ty) :: sol</code></pre>
<p>So in order to make sure our solution is internally consistent it’s important that whenever we add a type to our solution we first apply the solution to it. This ensures that we can substitute a variable in our solution for its corresponding type and not worry about whether we need to do something further. Additionally, whenever we add a new binding we substitute for it in the constraints we have left to ensure we never have a solution which is just inconsistent. This prevents us from unifying <code>v ~ TBool</code> and <code>v ~ TArr(TBool, TBool)</code> in the same solution! The actual code for doing this is that <code>substConstr (TVar j) i constrs</code> bit.</p>
<p>The next case is the general case for unifying a variable with some type. It looks very similar to this one.</p>
<pre class="sml"><code>     | ((TVar i, ty) | (ty, TVar i)) =&gt;
       if occursIn i ty
       then raise UnificationError c
       else addSol i ty (unify (substConstrs ty i constrs))</code></pre>
<p>Here we have the critical <code>occursIn</code> check. This checks to see if a variable appears in a type and prevents us from making erroneous unifications like <code>TVar a ~ TArr (TVar a, TVar a)</code>. This occurs check is actually very easy to implement</p>
<pre class="sml"><code>    fun occursIn v ty = List.exists (fn v&#39; =&gt; v = v&#39;) (freeVars ty)</code></pre>
<p>Finally we have one last case: the failure case. This is the catch-all case for if we try to unify two things that are obviously incompatible.</p>
<pre class="sml"><code>     | _ =&gt; raise UnificationError c</code></pre>
<p>All together, that code was</p>
<pre class="sml"><code>    fun applySol sol ty =
        foldl (fn ((v, ty), ty&#39;) =&gt; subst ty v ty&#39;) ty sol
    fun applySolCxt sol cxt =
        let fun applyInfo i =
                case i of
                    PolyTypeVar (PolyType (bs, m)) =&gt;
                    PolyTypeVar (PolyType (bs, (applySol sol m)))
                  | MonoTypeVar m =&gt; MonoTypeVar (applySol sol m)
        in map applyInfo cxt end

    fun addSol v ty sol = (v, applySol sol ty) :: sol

    fun occursIn v ty = List.exists (fn v&#39; =&gt; v = v&#39;) (freeVars ty)

    fun unify ([] : constr list) : sol = []
      | unify (c :: constrs) =
        case c of
            (TBool, TBool) =&gt; unify constrs
          | (TVar i, TVar j) =&gt;
            if i = j
            then unify constrs
            else addSol i (TVar j) (unify (substConstrs (TVar j) i constrs))
          | ((TVar i, ty) | (ty, TVar i)) =&gt;
            if occursIn i ty
            then raise UnificationError c
            else addSol i ty (unify (substConstrs ty i constrs))
          | (TArr (l, r), TArr (l&#39;, r&#39;)) =&gt;
            unify ((l, l&#39;) :: (r, r&#39;) :: constrs)
          | _ =&gt; raise UnificationError c</code></pre>
<h2 id="constraint-generation">Constraint Generation</h2>
<p>The other half of this algorithm is the constraint generation part. We generate constraints and use <code>unify</code> to turn them into solutions. This boils down to two functoins. The first is to glue together solutions.</p>
<pre class="sml"><code>    fun &lt;+&gt; (sol1, sol2) =
        let fun notInSol2 v = List.all (fn (v&#39;, _) =&gt; v &lt;&gt; v&#39;) sol2
            val sol1&#39; = List.filter (fn (v, _) =&gt; notInSol2 v) sol1
        in
            map (fn (v, ty) =&gt; (v, applySol sol1 ty)) sol2 @ sol1&#39;
        end
    infixr 3 &lt;+&gt;</code></pre>
<p>Given two solutions we figure out which things don’t occur in the in the second solution. Next, we apply solution 1 everywhere in the second solution, giving a consistent solution wihch contains everything in <code>sol2</code>, finally we add in all the stuff not in <code>sol2</code> but in <code>sol1</code>. This doesn’t check to make sure that the solutions are actually consistent, this is done elsewhere.</p>
<p>Next is the main function here <code>constrain</code>. This actually generates solution and type given a context and an expression. The first few cases are nice and simple</p>
<pre class="sml"><code>    fun constrain ctx True = (TBool, [])
      | constrain ctx False = (TBool, [])
      | constrain ctx (Var i) = (lookupVar i ctx, [])</code></pre>
<p>In these cases we don’t infer any constraints, we just figure out types based on information we know previously. Next for <code>Fn</code> we generate a fresh variable to represent the arguments type and just constrain the body.</p>
<pre class="sml"><code>      | constrain ctx (Fn body) =
        let val argTy = TVar (fresh ())
            val (rTy, sol) = constrain (MonoTypeVar argTy :: ctx) body
        in (TArr (applySol sol argTy, rTy), sol) end</code></pre>
<p>Once we have the solution for the body, we apply it to the argument type which might replace it with a concrete type if the constraints we inferred for the body demand it. For <code>If</code> we do something similar except we add a few constraints of our own to solve.</p>
<pre class="sml"><code>      | constrain ctx (If (i, t, e)) =
        let val (iTy, sol1) = constrain ctx i
            val (tTy, sol2) = constrain (applySolCxt sol1 ctx) t
            val (eTy, sol3) = constrain (applySolCxt (sol1 &lt;+&gt; sol2) ctx) e
            val sol = sol1 &lt;+&gt; sol2 &lt;+&gt; sol3
            val sol = sol &lt;+&gt; unify [ (applySol sol iTy, TBool)
                                    , (applySol sol tTy, applySol sol eTy)]
        in
            (tTy, sol)
        end</code></pre>
<p>Notice how we apply each solution to the context for the next thing we’re constraining. This is how we ensure that each solution will be consistent. Once we’ve generated solutions to the constraints in each of the subterms, we smash them together to produce the first solution. Next, we ensure that the subcomponents have the right type by generating a few constraints to ensure that <code>iTy</code> is a bool and that <code>tTy</code> and <code>eTy</code> (the types of the branches) are both the same. We have to carefully apply the <code>sol</code> to each of these prior to unifying them to make sure our solution stays consistent.</p>
<p>This is practically the same as what the <code>App</code> case is</p>
<pre class="sml"><code>      | constrain ctx (App (l, r)) =
        let val (domTy, ranTy) = (TVar (fresh ()), TVar (fresh ()))
            val (funTy, sol1) = constrain ctx l
            val (argTy, sol2) = constrain (applySolCxt sol1 ctx) r
            val sol = sol1 &lt;+&gt; sol2
            val sol = sol &lt;+&gt; unify [(applySol sol funTy,
                                      applySol sol (TArr (domTy, ranTy)))
                                    , (applySol sol argTy, applySol sol domTy)]
        in (ranTy, sol) end</code></pre>
<p>The only real difference here is that we generate different constraints: we make sure we’re applying a function whose domain is the same as the argument type.</p>
<p>The most interesting case here is <code>Let</code>. This implements let generalization which is how we actually get polymorphism. After inferring the type of the thing we’re binding we generalize it, giving us a poly type to use in the body of let. The key to generalizing it is that <code>generalizeMonoType</code> we had before.</p>
<pre class="sml"><code>      | constrain ctx (Let (e, body)) =
        let val (eTy, sol1) = constrain ctx e
            val ctx&#39; = applySolCxt sol1 ctx
            val eTy&#39; = generalizeMonoType ctx&#39; (applySol sol1 eTy)
            val (rTy, sol2) = constrain (PolyTypeVar eTy&#39; :: ctx&#39;) body
        in (rTy, sol1 &lt;+&gt; sol2) end</code></pre>
<p>We do pretty much everything we had before except now we carefully ensure to apply the solution we get for the body to the context and then to generalize the type with respect to that new context. This is how we actually get polymorphism, it will assign a proper polymorphic type to the argument.</p>
<p>That wraps up constraint generation. Now all that’s left to see if the overall driver for type inference.</p>
<pre class="sml"><code>    fun infer e =
        let val (ty, sol) = constrain [] e
        in generalizeMonoType [] (applySol sol ty) end
    end</code></pre>
<p>So all we do is infer and generalize a type! And there you have it, that’s how ML and Haskell do type inference.</p>
<h2 id="wrap-up">Wrap Up</h2>
<p>Hopefully that clears up a little of the magic of how type inference works. The next challenge is to figure out how to do type inference on a language with patterns and ADTs! This is actually quite fun, pattern checking involves synthesizing a type from a pattern which needs something like linear logic to handle pattern variables correctly.</p>
<p>With this we’re actually a solid 70% of the way to building a type checker to SML. Until I have more free time though, I leave this as an exercise to the curious reader.</p>
<p>Cheers,</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Sat, 28 Feb 2015 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2015-02-28-type-inference.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>A Twelf Introduction</title>
    <link>http://jozefg.bitbucket.org/posts/2015-02-28-twelf.html</link>
    <description><![CDATA[<div class="info">
    Posted on February 28, 2015
    
</div>
<div class="info">
    
    Tags: <a href="/tags/twelf.html">twelf</a>, <a href="/tags/types.html">types</a>
    
</div>

<p>For the last 3 or so weeks I’ve been writing a bunch of Twelf code for my research (hence my flat-lined github punch card). Since it’s actually a lot of fun I thought I’d share a bit about Twelf.</p>
<h2 id="what-is-twelf">What Is Twelf</h2>
<p>Since Twelf isn’t a terribly well known language it’s worth stating what exactly it is we’re talking about. Twelf is a proof assistant. It’s based on a logic called LF (similarly to how Coq is based on CiC).</p>
<p>Twelf is less powerful than some other proof assistants but by limiting some of its power it’s wonderfully suited to proving certain types of theorems. In particular, Twelf admits true “higher order abstract syntax” (don’t worry if you don’t know what this means) this makes it great for formalizing programming languages with variable bindings.</p>
<p>In short, Twelf is a proof assistant which is very well suited for defining and proving things about programming languages.</p>
<h2 id="getting-twelf">Getting Twelf</h2>
<p>It’s much more fun to follow along a tutorial if you actually have a Twelf installation to try out the code. You can download and compile the <a href="http://twelf.org/wiki/Download">sources to Twelf</a> with SML/NJ or Mlton. You could also use <a href="https://github.com/standardml/smackage">smackage</a> to get the compiler.</p>
<p>Once you’ve compiled the thing you should be left with a binary <code>twelf-server</code>. This is your primary way of interacting with the Twelf system. There’s quite a slick Emacs interface to smooth over this process. If you’ve installed twelf into a directory <code>~/twelf/</code> all you need is the incantation</p>
<pre class="elisp"><code>    (setq twelf-root &quot;~/twelf/&quot;)
    (load (concat twelf-root &quot;emacs/twelf-init.el&quot;))</code></pre>
<p>Without further ado, let’s look at some Twelf code.</p>
<h2 id="some-code">Some Code</h2>
<p>When writing Twelf code we encode the thing that we’re studying, the object language, as a bunch of type families and constructors in Twelf. This means that when we edit a Twelf file we’re just writing signatures.</p>
<p>For example, if we want to encode natural numbers we’d write something like</p>
<pre class="twelf"><code>    nat : type.
    z   : nat.
    s   : nat -&gt; nat.</code></pre>
<p>This is an LF signature, we declare a series of constants with <code>NAME : TYPE.</code>. Note the period at the end of each declaration. First we start by declaring a type for natural numbers called <code>nat</code> with <code>nat : type.</code> Here <code>type</code> is the base kind of all types in Twelf. Next we go on to declare what the values of type <code>nat</code> are.</p>
<p>In this case there are two constructors for <code>nat</code>. We either have zero, <code>z</code>, or the successor of another value of type <code>nat</code>, <code>s</code>. This gives us a canonical forms lemma for natural numbers: All values of type <code>nat</code> are either</p>
<ul>
<li><code>z</code></li>
<li><code>s N</code> for some value <code>N : nat</code></li>
</ul>
<p>Later on, we’ll justify the proofs we write with this lemma.</p>
<p>Anyways, now that we’ve encoded the natural numbers I wanted to point out a common point of confusion about Twelf. We’re not writing programs to be run. We’re writing programs exclusively for the purpose of typechecking. Heck, we’re not even writing programs at the term level! We’re just writing a bunch of constants out with their types! More than this even, Twelf is defined so that you can only write canonical forms. This means that if you write something in your program, it has to be in normal form, fully applied! In PL speak it has to be β-normal and η-long. This precludes actually writing programs for the sake of reducing them. You’re never going to write a web server in Twelf, you even be writing “Hello World”. You might use it to verify the language your writing them in though.</p>
<p>Now that we’ve gotten the awkward bit out the way, let’s now define a Twelf encoding of a judgment. We want to encode the judgment <code>+</code> which is given by the following rules</p>
<pre><code>—————————
z + n = n

   m + n = p
———————————————
s(m) + n = s(p)</code></pre>
<p>In the rest of the world we have this idea that propositions are types. In twelf, we’re worried about defining logics and systems, so we have the metatheoretic equivalent: judgments are types.</p>
<p>So we define a type family <code>plus</code>.</p>
<pre class="twelf"><code>    plus : nat -&gt; nat -&gt; nat -&gt; type</code></pre>
<p>So <code>plus</code> is a type indexed over 3 natural numbers. This is our first example of dependent types: <code>plus</code> is a type which <em>depends</em> on 3 terms. Now we can list out how to construct a derivation of <code>plus</code>. This means that inference rules in a meta theory corresponds to constants in Twelf as well.</p>
<pre class="twelf"><code>    plus/z : {n : nat} plus z n n</code></pre>
<p>This is some new syntax, in Twelf <code>{NAME : TYPE} TYPE</code> is a dependent function type, a pi type. This notation is awfully similar to Agda and Idris if you’re familiar with them. This means that this constructor takes a natural number, <code>n</code> and returns a derivation that <code>plus z n n</code>. The fact that the return type depends on what <code>nat</code> we supply is why this needs a dependent type.</p>
<p>In fact, this is such a common pattern that Twelf has sugar for it. If we write an unbound capital variable name Twelf will automagically introduce a binder <code>{N : ...}</code> at the front of our type. We can thus write our inference rules as</p>
<pre class="twelf"><code>    plus/z : plus z N N
    plus/s : plus N M P -&gt; plus (s N) M (s P)</code></pre>
<p>These rules together with our declaration of <code>plus</code>. In fact, there’s something kinda special about these two rules. We know that for any term <code>n : nat</code> which is in canonical form, there should be an applicable rule. In Twelf speak, we say that this type family is total.</p>
<p>We can ask Twelf to check this fact for us by saying</p>
<pre class="twelf"><code>    plus : nat -&gt; nat -&gt; nat -&gt; type.
    %mode plus +N +M -P.

    plus/z : plus z N N.
    plus/s : plus N M P -&gt; plus (s N) M (s P).

    %worlds () (plus _ _ _).
    %total (N) (plus N _ _).</code></pre>
<p>We want to show that for all terms <code>n, m : nat</code> in canonical form, there is a term <code>p</code> in canonical form so that <code>plus n m p</code>. This sort of theorem is what we’d call a ∀∃-theorem. This is literally because it’s a theorem of the form “∀ something. ∃ something. so that something”. These are the sort of thing that Twelf can help us prove.</p>
<p>Here’s the workflow for writing one of these proofs in Twelf</p>
<ol type="1">
<li>Write out the type family</li>
<li>Write out a <code>%mode</code> specification to say what is bound in the ∀ and what is bound in the ∃.</li>
<li>Write out the different constants in our type family</li>
<li>Specify the context to check our proof in with <code>%worlds</code>, usually we want to say the empty context, <code>()</code></li>
<li>Ask Twelf to check that we’ve created a proof according to the mode with <code>%total</code> where the <code>N</code> specifies what to induct on.</li>
</ol>
<p>In our case we have a case for each canonical form of <code>nat</code> so our type family is total. This means that our theorem passes. Hurray!</p>
<p>Believe it or not this is what life is like in Twelf land. All the code I’ve written these last couple of weeks is literally type signatures and 5 occurrences of <code>%total</code>. What’s kind of fun is how unreasonably effective a system this is for proving things.</p>
<p>Let’s wrap things up by proving one last theorem, if <code>plus A B N</code> and <code>plus A B M</code> both have derivations, then we should be able to show that <code>M</code> and <code>N</code> are the same. Let’s start by defining what it means for two natural numbers to be the same.</p>
<pre class="twelf"><code>    nat-eq : nat -&gt; nat -&gt; type.
    nat-eq/r : nat-eq N N.
    nat-eq/s : nat-eq N M -&gt; nat-eq (s N) (s M).</code></pre>
<p>I’ve purposefully defined this so it’s amenable to our proof, but it’s still a believable formulation of equality. It’s reflexive and if <code>N</code> is equal to <code>M</code>, then <code>s N</code> is equal to <code>s M</code>. Now we can actually state our proof.</p>
<pre class="twelf"><code>    plus-fun : plus N M P -&gt; plus N M P&#39; -&gt; nat-eq P P&#39; -&gt; type.
    %mode plus-fun +A +B -C.</code></pre>
<p>Our theorem says if you give us two derivations of <code>plus</code> with the same arguments, we can prove that the outputs are equal. There are two cases we have to cover for our induction so there are two constructors for this type family.</p>
<pre class="twelf"><code>    plus-fun/z : plus-fun plus/z plus/z nat-eq/r.
    plus-fun/s : plus-fun (plus/s L) (plus/s R) (nat-eq/s E)
                  &lt;- plus-fun L R E.</code></pre>
<p>A bit of syntactic sugar here, I used the backwards arrow which is identical to the normal <code>-&gt;</code> except its arguments are flipped. Finally, we ask Twelf to check that we’ve actually proven something here.</p>
<pre class="twelf"><code>    %worlds () (plus-fun _ _ _).
    %total (P) (plus-fun P _ _).</code></pre>
<p>And there you have it, some actual theorem we’ve mechanically checked using Twelf.</p>
<h2 id="wrap-up">Wrap Up</h2>
<p>I wanted to keep this short, so now that we’ve covered Twelf basics I’ll just refer you to one of the more extensive tutorials. You may be interested in</p>
<ul>
<li><a href="http://twelf.org/wiki/Proving_metatheorems_with_Twelf">Proving Metatheorems with Twelf</a></li>
<li><a href="http://twelf.org/wiki/Summer_school_2008">The OPLSS course on Twelf</a></li>
</ul>
<p>If you’re interested in learning a bit more about the nice mathematical foundations for LF you should check out <a href="http://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.21.5854">“The LF Paper”</a>.</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Sat, 28 Feb 2015 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2015-02-28-twelf.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Notes on Proof Theory: Part 1</title>
    <link>http://jozefg.bitbucket.org/posts/2015-02-11-proof-theory1.html</link>
    <description><![CDATA[<div class="info">
    Posted on February 11, 2015
    
</div>
<div class="info">
    
    Tags: <a href="/tags/types.html">types</a>
    
</div>

<p>I write a lot about types. Up until now however, I’ve only made passing references to the thing I’ve actually been studying in most of my free time lately: proof theory. Now I have a good reason for this: the proof theory I’m interested in is undeniably intertwined with type theory and computer science as a whole. In fact, you occasionally see someone draw the triangle</p>
<pre><code>           Type Theory
          /           \
         /             \
 Proof Theory ---- Category Theory</code></pre>
<p>Which nicely summarizes the lay of the land in the world I’m interested in. People will often pick up something will understood on one corner of the triangle and drag it off to another, producing a flurry of new ideas and papers. It’s all very exciting and leads to really cool stuff. I think the most talked about example lately is homotopy type theory which drags a mathematical structure (weak infinite groupoids) and hoists off to type theory!</p>
<p>If you read the [unprofessional, mostly incorrect, and entirely more fun to read] blog posts on these subjects you’ll find most of the lip service is paid to category theory and type theory with poor proof theory shunted off to the side.</p>
<p>In this post, I’d like to jot down my notes on Frank Pfenning’s introduction to proof theory materials to change that in some small way.</p>
<h2 id="what-is-proof-theory">What is Proof Theory</h2>
<p>The obvious question is just “What is proof theory?”. The answer is that proof theory is the study of proofs. In this world we study proofs as first class mathematical objects which we prove interesting things about. This is the branch of math that formalizes our handwavy notion of a proof into a precise object governed by rules.</p>
<p>We can then prove things like &quot;Given a proof that <code>Γ ⊢ A</code> and another derivation of <code>Γ, A ⊢ B</code>, then we can produce a derivation of <code>Γ ⊢ B</code>. Such a theorem is utterly crazy unless we can formalize what it means to derive something.</p>
<p>From this we grow beautiful little sets of rules and construct derivations with them. Later, we can drag these derivations off to type theory and use them to model all sorts of wonderful phenomena. My most recent personal example was when folks noticed that the rules for modal logic perfectly capture what the semantics of static pointers ought to be.</p>
<p>So in short, proof theory is devoted to answering that question that every single one of your math classes dodged</p>
<blockquote>
<p>Professor, what exactly is a proof?</p>
</blockquote>
<h2 id="basic-building-blocks">Basic Building Blocks</h2>
<p>In every logic that we’ll study we’ll keep circling back to two core objects: judgments and propositions. The best explanation of judgments I’ve read comes from Frank Pfenning</p>
<blockquote>
<p>A judgment is something we may know, that is, an object of knowledge. A judgment is evident if we in fact know it.</p>
</blockquote>
<p>So judgments are the things we’ll structure our logic around. You’ve definitely heard of one judgment: <code>A true</code>. This judgment signifies whether or not some proposition <code>A</code> is true. Judgments can be much fancier though: we might have a whole bunch of judgments like <code>n even</code>, <code>A possible</code> or <code>A resource</code>.</p>
<p>These judgments act across various syntactic objects. In particular, from our point of view we’ll understand the meaning of a proposition by the ways we can prove it, that is the proofs that <code>A true</code> is evident.</p>
<p>We prove a judgment <code>J</code> through inference rules. An inference rule takes the form</p>
<pre><code>J₁ J₂ .... Jₓ
—————————————
     J</code></pre>
<p>Which should be read as “When <code>J₁</code>, <code>J₂</code> … and <code>Jₓ</code> hold, then so does <code>J</code>”. Here the things above the line are premises and the ones below are conclusions. What we’ll do is define a bunch of these inference rules and use them to construct proofs of judgments. For example, we might have the inference rules</p>
<pre><code>             n even
 ——————    ————————————
 0 even    S(S(n)) even</code></pre>
<p>for the judgment <code>n even</code>. We can then form proofs to show that <code>n even</code> holds for some particular <code>n</code>.</p>
<pre><code>       ——————
       0 even
    ————————————
    S(S(0)) even
 ——————————————————
 S(S(S(S(0)))) even</code></pre>
<p>This tree for example is evidence that <code>4 even</code> holds. We apply second inference rule to <code>S(S(S(S(0))))</code> first. This leaves us with one premise to show, <code>S(S(0)) even</code>. For this we repeat the process and end up with the new premise that <code>0 even</code>. For this we can apply the first inference rule which has no premises completing our proof.</p>
<p>One judgment we’ll often see is <code>A prop</code>. It simply says that <code>A</code> is a well formed proposition, not necessarily true but syntactically well formed. This judgment is defined inductively over the structure of <code>A</code>. An example judgment would be</p>
<pre><code>A prop  B prop
——————————————
  A ∧ B prop</code></pre>
<p>Which says that <code>A ∧ B</code> (A and B) is a well formed proposition if and only if <code>A</code> and <code>B</code> are! We can imagine a whole bunch of these rules</p>
<pre><code>                A prop B prop
——————  ——————  ————————————— ...
⊤ prop  ⊥ prop    A ∨ B prop</code></pre>
<p>that lay out the propositions of our logic. This doesn’t yet tell us how prove any of these propositions to be true, but it’s a start. After we formally specify what sentences are propositions in our logic we need to discuss how to prove that one is true. We do this with a different judgment <code>A true</code> which is once again defined inductively.</p>
<p>For example, we might want to give meaning to the proposition <code>A ∧ B</code>. To do this we define its meaning through the inference rules for proving that <code>A ∧ B true</code>. In this case, we have the rule</p>
<pre><code>A true  B true
—————————————— (∧ I)
  A ∧ B true</code></pre>
<p>I claim that this defines the meaning of <code>∧</code>: to prove a conjunction to be true we must prove its left and right halves. The rather proof-theoretic thing we’ve done here is said that the meaning of something is what we use to prove it. This is sometimes called the “verificationist perspective”. Finally, note that I annotated this rule with the name <code>∧ I</code> simply for convenience to refer it.</p>
<p>Now that we know what <code>A ∧ B</code> means, what does have a proof of it imply? Well we should be able to “get out what we put in” which would mean we’d have two inference rules</p>
<pre><code>A ∧ B true    A ∧ B true
——————————    ——————————
  A true        B true</code></pre>
<p>We’ll refer to these rules as <code>∧ E1</code> and <code>∧ E2</code> respectively.</p>
<p>Now for a bit of terminology, rules that let us “introduce” new proofs of propositions are introduction rules. Once we have a proof, we can use it to construct other proofs. The rules for how we do that are called elimination rules. That’s why I’ve been adding I’s and E’s to the ends of our rule names.</p>
<p>How do we convince ourselves that these rules are correct with respect to our understanding of <code>∧</code>? This question leads us to our first sort of proofs-about-proofs we’ll make.</p>
<h2 id="local-soundness-and-completeness">Local Soundness and Completeness</h2>
<p>What we want to say is that the introduction and elimination rules match up. This should mean that anytime we prove something with an by an introduction rule followed by an elimination rule, we should be able to rewrite to avoid this duplication. This also hints that the rules aren’t too powerful: we can’t prove anything with the elimination rules that we didn’t have a proof for at some point already.</p>
<p>For <code>∧</code> this proof looks like this</p>
<pre><code>  D  E
  –  –
  A  B            D
 —————— ∧I   ⇒  ————
  A ∧ B           A
 —————— ∧E 1
    A</code></pre>
<p>So whenever we introduce a ∧ and then eliminate it with <code>∧ E1</code> we can always rewrite our proof to not use the elimination rules. Here notice that D and E range over <em>derivations</em> in this proof. They represent a chain of rule applications that let us produce an <code>A</code> or <code>B</code> in the end. Note I got a bit lazy and started omitting the <code>true</code> judgments, this is something I’ll do a lot since it’s mostly unambiguous.</p>
<p>The proof for <code>∧E2</code> is similar.</p>
<pre><code>  D  E
  –  –
  A  B            E
  ————— ∧I   ⇒  ————
  A ∧ B           B
  ————— ∧E 2
    B</code></pre>
<p>Given this we say that the elimination rules for ∧ are “locally sound”. That is, when used immediately after an elimination rule they don’t let us produce anything truly new.</p>
<p>Next we want to show that if we have a proof of <code>A ∧ B</code>, the elimination rules give us enough information that we can pick the proof apart and produce a reassembled <code>A ∧ B</code>.</p>
<pre><code>           D           D
         ————–       ————–
  D      A ∧ B       A ∧ B
————— ⇒ —————∧E1   ——————∧E2
A ∧ B      A           B
         ———————————————— ∧I
               A ∧ B</code></pre>
<p>This somewhat confusion derivation takes our original proof of <code>A ∧ B</code> and pulls it apart into proof of <code>A</code> and <code>B</code> and uses these to assemble a new proof of <code>A ∧ B</code>. This means that our elimination rules give us all the information we put in so we say their locally complete.</p>
<p>The two of these properties combined, local soundness and completeness are how we show that an elimination rule is balanced with its introduction rule.</p>
<p>If you’re more comfortable with programming languages (I am) our local soundness property is equivalent to stating that</p>
<pre><code>fst (a, b) ≡ a
snd (a, b) ≡ b</code></pre>
<p>And local completeness is that</p>
<pre><code>a ≡ (fst a, snd a)</code></pre>
<p>The first equations are reductions and the second is expansion. These actually correspond the eta and beta rules we expect a programming language to have! This is a nice little example of why proof theory is useful, it gives a systematic way to define some parts of the behavior of a program. Given the logic a programming language gives rise to we can double check that all rules are locally sound and complete which gives us confidence our language isn’t horribly broken.</p>
<h2 id="hypothetical-judgments">Hypothetical Judgments</h2>
<p>Before I wrap up this post I wanted to talk about one last important concept in proof theory: judgments with hypotheses. This is best illustrated by trying to write the introduction and elimination rules for “implies” or “entailment”, written <code>A ⊃ B</code>.</p>
<p>Clearly <code>A ⊃ B</code> is supposed to mean we can prove <code>B true</code> assume <code>A true</code> to be provable. In other words, we can construct a derivation of the form</p>
<pre><code> A true
 ——————
   .
   .
   .
 ——————
 B true</code></pre>
<p>We can notate our rules then as</p>
<pre><code> —————— u
 A true
 ——————
   .
   .
   .
 ——————
 B true           A ⊃ B    A
 —————————— u     ——————————
 A ⊃ B true         B true</code></pre>
<p>This notation is a bit clunky, so we’ll opt for a new one: <code>Γ ⊢ J</code>. In this notation <code>Γ</code> is some list of judgments we assume to hold and <code>J</code> is the thing we want to show holds. Generally we’ll end up with the rule</p>
<pre><code>J ∈ Γ
—————
Γ ⊢ J</code></pre>
<p>Which captures the fact that Γ contains assumptions we may or may not use to prove our goal. This specific rule may vary depending on how we want express how assumptions work in our logic (substructural logics spring to mind here). For our purposes, this is the most straightforward characterization of how this ought to work.</p>
<p>Our hypothetical judgments come with a few rules which we call “structural rules”. They modify the structure of judgment, rather than any particular proposition we’re trying to prove.</p>
<pre><code>Weakening
  Γ ⊢ J
—————————
Γ, Γ&#39; ⊢ J

Contraction
Γ, A, A, Γ&#39; ⊢ J
———————————————
 Γ, A, Γ&#39; ⊢ J

Exchange
Γ&#39; = permute(Γ)   Γ&#39; ⊢ A
————————————————————————
        Γ ⊢ A</code></pre>
<p>Finally, we get a substitution principle. This allows us to eliminate some of the assumptions we made to prove a theorem.</p>
<pre><code>Γ ⊢ A   Γ, A ⊢ B
————————————————
     Γ ⊢ B</code></pre>
<p>These 5 rules define meaning to our hypothetical judgments. We can restate our formulation of entailment with less clunky notation then as</p>
<pre><code>A prop  B prop
——————————————
  A ⊃ B prop

Γ, A ⊢ B      Γ ⊢ A ⊃ B    Γ ⊢ A
—————————     ——————————————————
Γ ⊢ A ⊃ B           Γ ⊢ B</code></pre>
<p>One thing in particular to note here is that entailment actually internalizes the notion of hypothetical judgments into our logic. This the aspect of it that made it behave so differently then the other connectives we looked at.</p>
<p>As an exercise to the reader: prove the local soundness and completeness of these rules.</p>
<h2 id="conclusion">Conclusion</h2>
<p>In this post we’ve layed out a bunch of rules and I’ve hinted that a bunch more are possible. When put together these rules define a logic using “natural deduction”, a particular way of specifying proofs that uses inference rules rather than axioms or something entirely different.</p>
<p>Hopefully I’ve inspired you to poke a bit further into proof theory, in that case I heartily recommend <a href="https://www.cs.uoregon.edu/research/summerschool/summer12/curriculum.html">Frank Pfenning’s lectures</a> at the Oregon Summer School for Programming Languages.</p>
<p>Cheers,</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Wed, 11 Feb 2015 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2015-02-11-proof-theory1.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Observations about -XStaticPointers</title>
    <link>http://jozefg.bitbucket.org/posts/2015-01-27-modal-logic-in-haskell.html</link>
    <description><![CDATA[<div class="info">
    Posted on January 27, 2015
    
</div>
<div class="info">
    
    Tags: <a href="/tags/haskell.html">haskell</a>, <a href="/tags/types.html">types</a>
    
</div>

<p>For those who haven’t heard, GHC 7.10 is making a brave foray into the exciting world of distributed computing. To this end, it’s made a new language extension called <code>-XStaticPointers</code> to support Cloud Haskell in a pleasant, first class manner.</p>
<p>If you haven’t heard of static pointers before now, it’s worth glancing through the nice <a href="https://ocharles.org.uk/blog/guest-posts/2014-12-23-static-pointers.html">tutorial</a> from ocharles’ 24 days of $(Haskell Related Thing).</p>
<p>The long and short of it is that <code>-XStaticPointers</code> gives us this new keyword <code>static</code>. We apply static to an expression and if there are no closured variables (to be formalized momentarily) then we get back a <code>StaticPtr a</code>. This gives us a piece of data that we can&quot; serialize and ship over the wire because it has no dependencies.</p>
<p>Now to expand upon this “no closured variables”. A thing can only be fed to <code>static</code> if the free variables in the expression are all top level variables. This forbids us from writing something like</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb1-1" data-line-number="1"><span class="ot">    foo ::</span> <span class="dt">StaticPtr</span> a</a>
<a class="sourceLine" id="cb1-2" data-line-number="2">    foo a <span class="fu">=</span> static a</a></code></pre></div>
<p>Now in all honesty, I’m not super interested in Cloud Haskell. It’s not my area of expertise and I’m already terrified of trying to do things on one machine. What does interest me a lot though is this notion of having “I have no free variables” in the type of an expression. It’s an invariant we didn’t really have before in Haskell.</p>
<p>In fact, as I looked more closely it reminded me of something called box from modal logic.</p>
<h2 id="a-quick-summary-of-modal-logic">A Quick Summary of Modal Logic</h2>
<p>I’m not trying to give you a full understanding of modal logic, just a brief taste.</p>
<p>Modal logic extends our vanilla logic (in Haskell land this is <a href="/posts/2015-01-09-constructivism.html">constructive logic</a>) with modalities. Modalities are little prefixes we tack on the front of a proposition to qualify its meaning slightly.</p>
<p>For example we might say something like</p>
<blockquote>
<p>If it is possible that it is raining, then I will need an umbrella.</p>
</blockquote>
<p>Here we used then modality <code>possible</code> to indicate we’re not assuming that it <em>is</em> snowing, only that it’s conceivable that it is. Because I’m a witch and will melt in the rain even the possibility of death raining from the sky will force me to pack my umbrella.</p>
<p>To formalize this a bit, we have our inductive definition of propositions</p>
<pre><code>P = ⊥
  | ⊤
  | P ∧ P
  | P ∨ P
  | P ⇒ P
  | □ P</code></pre>
<p>This is the syntax of a particular modal logic with one modality. Everything looks quite normal up until the last proposition form, which is the “box” modality applied to some proposition.</p>
<p>The box modality (the one we really care about for later) means “necessarily”. I almost think of it is a truthier truth if you can buy that. □ forbids us from using any hypotheses saying something like <code>A is true</code> inside of it. Since it represents a higher standard of proof we can’t use the weaker notion that <code>A is true</code>! The rule for creating a box looks like this to the first approximation</p>
<pre><code> • ⊢ A
———————
Γ ⊢ □ A</code></pre>
<p>So in order to prove a box something under a set of assumptions Γ, we have to prove it assuming <em>none</em> of those assumptions. In fact, we find that this is a slightly overly restrictive form for this judgment, we know that if we have a <code>□ A</code> we proved it without assumptions so if we have to introduce a <code>□ B</code> we should be able to use the assumption that <code>A is true</code> for this proof because we know we can construct one without any assumptions and could just copy paste that in.</p>
<p>This causes us to create a second context, one of the hypotheses that <code>A is valid</code>, usually notated with a Δ. We then get the rules</p>
<pre><code>   Δ; • ⊢ A          Δ; Γ ⊢ A valid     A valid ∈ Δ
———————————————      ——————————————   ———————————————
Δ; Γ ⊢ □ A true       Δ; Γ ⊢ A true    Δ; Γ ⊢ A valid


Δ; Γ ⊢ □ A   Δ, A valid; Γ ⊢ B
——————————————————————————————
         Δ; Γ ⊢ B</code></pre>
<p>What you should take away from these scary looking symbols is</p>
<ol type="1">
<li><code>A valid</code> is much stronger than <code>A true</code></li>
<li>Anything inside a □ can depend on valid stuff, but not true stuff</li>
<li><code>□ A true</code> is the same as <code>A valid</code>.</li>
</ol>
<p>This presentation glosses over a fair amount, if your so inclined I’d suggest looking at Frank Pfenning’s <a href="http://www.cs.cmu.edu/~fp/courses/15816-s10/">lecture notes</a> from his class entitled “Modal Logic”. These actually go at a reasonable pace and introduce the groundwork for someone who isn’t super familiar with logic.</p>
<p>Now that we’ve established that there is an interesting theoretical backing for modal logic, I’m going to drop it on the floor and look at what Haskell actually gives us.</p>
<h2 id="that-who-cares-bit">That “Who Cares” Bit</h2>
<p>Okay, so how does this pertain to <code>StaticPtr</code>? Well I noticed that just like how box drops hypotheses that are “merely true”, <code>static</code> drops variables that are introduced by our local context!</p>
<p>This made me think that perhaps <code>StaticPtr</code>s are a useful equivalent to the □ modality! This shouldn’t be terribly surprising for PL people, indeed the course I linked to above expressly mentions □ to notate “movable code”. What’s really exciting about this is that there are a lot more applications of □ then just movable code! We can use it to notate staged computation for example.</p>
<p>Alas however, it was not to be. Static pointers are missing one essential component that makes them unsuitable for being □, we can’t eliminate them properly. In modal logic, we have a rule that lets other boxes depend on the contents of some box. The elimination rule is much stronger than just “If you give me a <code>□ A</code>, I’ll give you an <code>A</code>” because it’s much harder to construct a <code>□ A</code> in the first place! It’s this asymmetry that makes static pointers not quite kosher. With static pointers there isn’t a notion that we can take one static pointer and use it in another.</p>
<p>For example, we can’t write</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb5-1" data-line-number="1"><span class="ot">    applyS ::</span> <span class="dt">StaticPtr</span> a <span class="ot">-&gt;</span> <span class="dt">StaticPtr</span> (a <span class="ot">-&gt;</span> b) <span class="ot">-&gt;</span> <span class="dt">StaticPtr</span> b</a>
<a class="sourceLine" id="cb5-2" data-line-number="2">    applyS sa sf <span class="fu">=</span> static (deRefStaticPtr sf (deRefStaticPtr sa))</a></code></pre></div>
<p>My initial reaction was that <code>-XStaticPointers</code> is missing something, perhaps a notion of a “static pattern”. This would let us say something like</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb6-1" data-line-number="1"><span class="ot">    applyS ::</span> <span class="dt">StaticPtr</span> a <span class="ot">-&gt;</span> <span class="dt">StaticPtr</span> (a <span class="ot">-&gt;</span> b) <span class="ot">-&gt;</span> <span class="dt">StaticPtr</span> b</a>
<a class="sourceLine" id="cb6-2" data-line-number="2">    applyS sa sf <span class="fu">=</span></a>
<a class="sourceLine" id="cb6-3" data-line-number="3">      <span class="kw">let</span> static f <span class="fu">=</span> sf</a>
<a class="sourceLine" id="cb6-4" data-line-number="4">          static a <span class="fu">=</span> sa</a>
<a class="sourceLine" id="cb6-5" data-line-number="5">      <span class="kw">in</span> static (f a)</a></code></pre></div>
<p>So this <code>static</code> pattern in a keyword would allow us to hoist a variable into the realm of things we’re allowed to leave free in a static pointer.</p>
<p>This makes sense from my point of view, but less so from that of Cloud Haskell. The whole point of static pointers is to show a computation is dependency free after all, static patterns introduce a (limited) set of dependencies on the thunk that make our lives complicated. It’s not obvious to me how to desugar things so that static patterns can be compiled how we want them to be, it looks like it would require some runtime code generation which is a no-no for Haskell.</p>
<p>My next thought was that maybe <code>Closure</code> was the answer, but that doesn’t actually work either! We can introduce a closure from an arbitrary serializable term which is exactly what we <em>don’t</em> want from a model of □! Remember, we want to model closed terms so allowing us to accept an arbitrary term defeats the point.</p>
<p>It’s painfully clear that <code>StaticPtr</code>s are very nearly □s, but not quite! Whatever <code>Box</code> ends up being, we’d want the following interface</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb7-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">Box</span> a</a>
<a class="sourceLine" id="cb7-2" data-line-number="2"></a>
<a class="sourceLine" id="cb7-3" data-line-number="3"><span class="ot">    intoBox ::</span> <span class="dt">StaticPtr</span> a <span class="ot">-&gt;</span> <span class="dt">Box</span> a</a>
<a class="sourceLine" id="cb7-4" data-line-number="4"><span class="ot">    closeBox ::</span> <span class="dt">Box</span> (a <span class="ot">-&gt;</span> b) <span class="ot">-&gt;</span> <span class="dt">Box</span> a <span class="ot">-&gt;</span> <span class="dt">Box</span> b</a>
<a class="sourceLine" id="cb7-5" data-line-number="5"><span class="ot">    outBox ::</span> <span class="dt">Box</span> a <span class="ot">-&gt;</span> a</a></code></pre></div>
<p>The key difference from <code>StaticPtr</code>’s being <code>closeBox</code>. Basically this gives us a way to say “I have something that’s closed except for one dependency” and we can fill that dependency with some other closed term.</p>
<p>This turns something like</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb8-1" data-line-number="1">    <span class="kw">let</span> static x <span class="fu">=</span> sx <span class="kw">in</span> static y</a></code></pre></div>
<p>into</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb9-1" data-line-number="1">    intoBox (static (\x <span class="ot">-&gt;</span> y)) <span class="ot">`closeBox`</span> intoBox sx</a></code></pre></div>
<p>If you read the tutorial, you’ll notice that this is most of the implementation of <code>Closure</code>! Following our noses we define</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb10-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">Box</span> a <span class="kw">where</span></a>
<a class="sourceLine" id="cb10-2" data-line-number="2">      <span class="dt">Pure</span><span class="ot">  ::</span> <span class="dt">StaticPtr</span> a <span class="ot">-&gt;</span> <span class="dt">Box</span> a</a>
<a class="sourceLine" id="cb10-3" data-line-number="3">      <span class="dt">Close</span><span class="ot"> ::</span> <span class="dt">Box</span> (a <span class="ot">-&gt;</span> b) <span class="ot">-&gt;</span> <span class="dt">Box</span> a <span class="ot">-&gt;</span> <span class="dt">Box</span> b</a></code></pre></div>
<p>This is literally the dumbest implementation of <code>Box</code> I think is possible, but it actually works just fine.</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb11-1" data-line-number="1">    intoBox <span class="fu">=</span> <span class="dt">Pure</span></a>
<a class="sourceLine" id="cb11-2" data-line-number="2">    closeBox <span class="fu">=</span> <span class="dt">Close</span></a>
<a class="sourceLine" id="cb11-3" data-line-number="3"></a>
<a class="sourceLine" id="cb11-4" data-line-number="4"><span class="ot">    outBox ::</span> <span class="dt">Box</span> a <span class="ot">-&gt;</span> a</a>
<a class="sourceLine" id="cb11-5" data-line-number="5">    outBox (<span class="dt">Pure</span> a) <span class="fu">=</span> deRefStaticPtr a</a>
<a class="sourceLine" id="cb11-6" data-line-number="6">    outBox (<span class="dt">Close</span> f a) <span class="fu">=</span> outBox f (outBox a)</a></code></pre></div>
<p>which would seem to be modal logic in Haskell.</p>
<h2 id="wrap-up">Wrap Up</h2>
<p>To be honest, I’m not sure yet how this is useful. I’m kinda swamped with coursework at the moment (new semester at CMU) but it seems like a new and fun thing to play with.</p>
<p>I’ve stuck the code at <a href="http://github.com/jozefg/modal">jozefg/modal</a> if you want to play with it. Fair warning that it only compiles with GHC &gt;= 7.10 because we need static pointers.</p>
<p>Finally, since the idea of modalities for sendable code is not a new one, I should leave these links</p>
<ul>
<li><a href="http://www.cs.cmu.edu/~tom7/papers/modal-types-for-mobile-code.pdf">Tom Murphy VII’s PHD on the subject</a></li>
<li><a href="http://www.cs.cmu.edu/~rwh/papers/s5/short.pdf">A much shorter paper on the same</a></li>
<li><a href="http://www.cs.cmu.edu/~fp/papers/popl96.pdf">Using modalities indicate staging</a></li>
</ul>
<p>Cheers.</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Tue, 27 Jan 2015 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2015-01-27-modal-logic-in-haskell.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Why Constructive Logic</title>
    <link>http://jozefg.bitbucket.org/posts/2015-01-09-constructivism.html</link>
    <description><![CDATA[<div class="info">
    Posted on January  9, 2015
    
</div>
<div class="info">
    
    Tags: <a href="/tags/types.html">types</a>
    
</div>

<p>Continuing on my quest of writing about my poorly thought out comments, let’s talk about constructive logic. A lot of people in and around the Haskell/FP community will make statements like</p>
<blockquote>
<p>The Curry-Howard isomorphism means that you’re proving things in constructive logic.</p>
</blockquote>
<p>Usually absent from these remarks is a nice explanation of <em>why</em> constructive logic matches up with the programming we know and love.</p>
<p>In this post I’d like to highlight what constructive logic is intended to capture and why this corresponds so nicely with programming.</p>
<h2 id="a-bit-of-history">A Bit of History</h2>
<p>First things first, let’s discuss the actual origin of constructive logic. It starts with a mathematician and philosopher named Brouwer. He was concerned trying to give an answer to the question “What does it mean to know something to be true” where something is defined as a mathematical proposition.</p>
<p>He settled on the idea of proof being a sort of subjective and personal thing. I know something is true if and only if I can formulate some intuitive proof of it. When viewed this way, the proof I scribble down on paper doesn’t actually validate something’s truthfulness. It’s merely a serialization of my thought process for validating its truthfulness.</p>
<p>Notice that this line of reasoning doesn’t actually specify a precise definition of what verifying something intuitively means. I interpret this idea as something slightly more meta then any single formal system. Rather, when looking a formal system, you ought to verify that its axioms are admissible by your own intuition and then you may go on to accept proofs built off of these axioms.</p>
<p>Now after Brouwer started talking about these ideas Arend Heyting decided to try to write down a logic that captured this notion of “proof is intuition”. The result was this thing called intuitionistic logic. This logic is part of a broader family of logics called “constructive logics”.</p>
<h2 id="constructive-logic">Constructive Logic</h2>
<p>The core idea of constructive logic is replacing the notion of truth found in classical logic with an intuitionist version. In a classical logic each proposition is either true or false, regardless of what we know about it.</p>
<p>In our new constructive system, a formula cannot be assigned either until we have direct evidence of it. It’s not that there’s a magical new boolean value, {true, false, i-don’t-know}, it’s just not a meaningful question to ask. It doesn’t make sense in these logics to say “<code>A</code> is true” without having a proof of <code>A</code>. There isn’t necessarily this Platonic notion of truthfulness, just things we as logicians can prove. This is sometimes why constructive logic is called “logic for humans”.</p>
<p>The consequences of dealing with things in this way can be boils down to a few things. For example, we now know that</p>
<ol type="1">
<li>If <code>∃x. A(x)</code> can be proven, then there is some term <em>which we can readily produce</em> <code>t</code> so that <code>A(t)</code> is provable</li>
<li>If <code>A ∨ B</code> can be proven then either <code>A</code> or <code>B</code> is provable and we know which. (note that ∨ is the symbol for OR)</li>
</ol>
<p>These make sense when you realize that <code>∃x. A(x)</code> can only be proven if we have a direct example of it. We can’t indirectly reason that it really ought to exist or merely claim that it must be true in one of a set of cases. We actually need to introduce it by proving an example of it. When our logic enforces this of course we can produce that example!</p>
<p>The same goes for <code>A ∨ B</code>, in our logic the only way to prove <code>A ∨ B</code> is to either provide a proof of <code>A</code> or provide a proof of <code>B</code>. If this is the only way to build a <code>∨</code> we can always just point to how it was introduced!</p>
<p>If we extend this to and, <code>∧</code>: The only way to prove <code>A ∧ B</code> is to prove both <code>A</code> and <code>B</code>. If this is the only way to get to a proof of <code>A ∧ B</code> then of course we can get a proof of <code>A</code> from <code>A ∧ B</code>. <code>∧</code> is just behaving like a pair of proofs.</p>
<p>All of this points at one thing: our logic is structured so that we can only prove something when we directly prove it, that’s the spirit of Brouwer’s intuitionism that we’re trying to capture.</p>
<p>There are a lot of different incarnations of constructive logic, in fact pretty much every logic has a constructive cousin. They all share this notion of “We need a direct proof to be true” however. One thing to note that is that some constructive logics conflict a bit with intuitionism. While intuitionism might have provided some of the basis for constructive logics gradually people have poked and pushed the boundaries away from <em>just</em> Brouwer’s intuitionism. For example both Markov’s principle and Church’s thesis state something about <em>all</em> computable functions. While they may be reasonable statements we can’t give a satisfactory proof for them. This is a little confusing I know and I’m only going to talk about constructive logics that Brouwer would approve of.</p>
<p>I encourage the curious reader to poke further at this, it’s rather cool math.</p>
<h2 id="who-on-earth-cares">Who on Earth Cares?</h2>
<p>Now while constructive logic probably sounds reasonable, if weird, it doesn’t immediately strike me as particularly useful! Indeed, the main reason why computer science cares about constructivism is because we all use it already.</p>
<p>To better understand this, let’s talk about the Curry-Howard isomorphism. It’s that thing that wasn’t really invented by either Curry or Howard and some claim isn’t best seen as an isomorphism, naming is hard. The Curry-Howard isomorphism states that there’s a mapping from a type to a logical proposition and from a program to a proof.</p>
<p>To show some of the mappings for types</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb1-1" data-line-number="1">    <span class="dt">CH</span>(<span class="dt">Either</span> a b) <span class="fu">=</span> <span class="dt">CH</span>(a) ∨ <span class="dt">CH</span>(b)</a>
<a class="sourceLine" id="cb1-2" data-line-number="2">    <span class="dt">CH</span>((a, b))     <span class="fu">=</span> <span class="dt">CH</span>(a) ∧ <span class="dt">CH</span>(b)</a>
<a class="sourceLine" id="cb1-3" data-line-number="3">    <span class="dt">CH</span>( () )       <span class="fu">=</span> ⊤ <span class="co">-- True</span></a>
<a class="sourceLine" id="cb1-4" data-line-number="4">    <span class="dt">CH</span>(<span class="dt">Void</span>)       <span class="fu">=</span> ⊥ <span class="co">-- False</span></a>
<a class="sourceLine" id="cb1-5" data-line-number="5">    <span class="dt">CH</span>(a <span class="ot">-&gt;</span> b)     <span class="fu">=</span> <span class="dt">CH</span>(a) <span class="ot">→</span> <span class="dt">CH</span>(b)</a></code></pre></div>
<p>So a program with the type <code>(a, b)</code> is really a proof that <code>a ∧ b</code> is true. Here the truthfulness of a proposition really means that the corresponding type can be occupied by a program.</p>
<p>Now, onto why this logic we get is constructive. Recall our two conditions for a logic being constructive, first is that if <code>∃x. A(x)</code> is provable then there’s a specific <code>t</code> where <code>A(t)</code> is provable.</p>
<p>Under the Curry Howard isomorphism, ∃ is mapped to existential types (I wonder how that got its name :). That means that a proof of <code>∃x. A(x)</code> is something like</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb2-1" data-line-number="1">    <span class="co">-- Haskell ex. syntax is a bit gnaryl :/</span></a>
<a class="sourceLine" id="cb2-2" data-line-number="2">    <span class="kw">data</span> <span class="dt">Exists</span> f <span class="fu">=</span> forall x<span class="fu">.</span> <span class="dt">Exists</span> f x</a>
<a class="sourceLine" id="cb2-3" data-line-number="3"></a>
<a class="sourceLine" id="cb2-4" data-line-number="4"><span class="ot">    ourProof ::</span> <span class="dt">Exists</span> <span class="dt">F</span></a>
<a class="sourceLine" id="cb2-5" data-line-number="5">    ourProof <span class="fu">=</span> <span class="fu">...</span></a></code></pre></div>
<p>Now we know the only way to construct an <code>Exists F</code> is to use the constructor <code>Exists</code>. This constructor means that there is at least one specific type for which we could prove <code>f x</code>. We can also easily produce this term as well!</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb3-1" data-line-number="1"><span class="ot">    isProof ::</span> <span class="dt">Exists</span> f <span class="ot">-&gt;</span> (f x <span class="ot">-&gt;</span> c) <span class="ot">-&gt;</span> c</a>
<a class="sourceLine" id="cb3-2" data-line-number="2">    isProof (<span class="dt">Exists</span> x) cont <span class="fu">=</span> cont x</a></code></pre></div>
<p>We can always access the specific “witness” we used to construct this <code>Exists</code> type with pattern matching.</p>
<p>The next law is similar. If we have a proof of <code>a ∨ b</code> we’re supposed to immediately be able to produce a proof of <code>a</code> or a proof of <code>b</code>.</p>
<p>In programming terms, if we have a program <code>Either a b</code> we’re supposed to be able to immediately tell whether this returns <code>Right</code> or <code>Left</code>! We can make some argument that one of these must be possible to construct but we’re not sure which since we have to be able to actually run this program! If we evaluate a program with the type <code>Either a b</code> we’re guaranteed to get either <code>Left a</code> or <code>Right b</code>.</p>
<h2 id="the-self-sacrificing-definition-of-constructive-logic">The Self-Sacrificing Definition of Constructive Logic</h2>
<p>There are a few explanations of constructive logic that basically describe it as “Classical logic - the law of excluded middle”. More verbosely, a constructive logic is just one that forbids</p>
<ol type="1">
<li><code>∀ A. A ∨ ¬ A</code> being provable (the law of excluded middle, LEM)</li>
<li><code>∀ A. ¬ (¬ A) → A</code> being provable (the law of double negation)</li>
</ol>
<p>I carefully chose the words “being provable” because we can easily introduce these as a hypothesis to a proof and still have a sound system. Indeed this is not uncommon when working in Coq or Agda. They’re just not a readily available tool. Looking at them, this should be apparent as they both let us prove something without directly proving it.</p>
<p>This isn’t really a defining aspect of constructivism, just a natural consequence. If we need a proof of <code>A</code> to show <code>A</code> to be true if we admit <code>A ∨ ¬ A</code> by default it defeats the point. We can introduce <code>A</code> merely by showing <code>¬ (¬ A)</code> which isn’t a proof of <code>A</code>! Just a proof that it really ought to be true.</p>
<p>In programming terms this is saying we can’t write these two functions.</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">Void</span></a>
<a class="sourceLine" id="cb4-2" data-line-number="2"></a>
<a class="sourceLine" id="cb4-3" data-line-number="3"><span class="ot">    doubleNeg ::</span> ((a <span class="ot">-&gt;</span> <span class="dt">Void</span>) <span class="ot">-&gt;</span> <span class="dt">Void</span>) <span class="ot">-&gt;</span> a</a>
<a class="sourceLine" id="cb4-4" data-line-number="4">    doubleNeg <span class="fu">=</span> <span class="fu">...</span></a>
<a class="sourceLine" id="cb4-5" data-line-number="5"></a>
<a class="sourceLine" id="cb4-6" data-line-number="6"><span class="ot">    lem ::</span> <span class="dt">Either</span> a (a <span class="ot">-&gt;</span> <span class="dt">Void</span>)</a>
<a class="sourceLine" id="cb4-7" data-line-number="7">    lem <span class="fu">=</span> <span class="fu">...</span></a></code></pre></div>
<p>For the first one we have to choices, either we use this <code>(a -&gt; Void) -&gt; Void</code> term we’re given or we construct an <code>a</code> without it. Constructing an arbitrary <code>a</code> without the function is just equivalent to <code>forall a. a</code> which we know to be unoccupied. That means we have to use <code>(a -&gt; Void) -&gt; Void</code> which means we have to build an <code>a -&gt; Void</code>. We have no way of doing something interesting with that supplied <code>a</code> however so we’re completely stuck! The story is similar with <code>lem</code>.</p>
<p>In a lot of ways this definition strikes me in the same way that describing functional programming as</p>
<blockquote>
<p>Oh it’s just programming where you don’t have variables or objects.</p>
</blockquote>
<p>Or static typing as</p>
<blockquote>
<p>It’s just dynamic typed programming where you can’t write certain correct programs</p>
</blockquote>
<p>I have a strong urge to say “Well.. yes but no!”.</p>
<h2 id="wrap-up">Wrap Up</h2>
<p>Hopefully this helps clarify what exactly people mean when they say Haskell corresponds to a constructive logic or programs are proofs. Indeed this constructivism gives rise to a really cool thing called “proof relevant mathematics”. This is mathematics done purely with constructive proofs. One of the latest ideas to trickle from mathematics to computers is homotopy type theory where we take a proof relevant look at identity types.</p>
<p>Before I wrap up I wanted to share one funny little thought I heard. Constructive mathematics has found a home in automated proof systems. Imagine Brouwer’s horror at hearing we do “intuitionist” proofs that no one will ever look at or try to understand beyond some random mechanical proof assistant!</p>
<p><em>Thanks to Jon Sterling and Darryl McAdams for the advice and insight</em></p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Fri, 09 Jan 2015 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2015-01-09-constructivism.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>A Crash Course on ML Modules</title>
    <link>http://jozefg.bitbucket.org/posts/2015-01-08-modules.html</link>
    <description><![CDATA[<div class="info">
    Posted on January  8, 2015
    
</div>
<div class="info">
    
    Tags: <a href="/tags/sml.html">sml</a>, <a href="/tags/haskell.html">haskell</a>
    
</div>

<p>I was having lunch with a couple of Haskell programmers the other day and the subject of the ML family came up. I’ve been writing a lot of ML lately and mentioned that I thought *ML was well worth learning for the average Haskeller. When pressed why the best answer I could come up with was “Well.. clean language, Oh! And an awesome module system” which wasn’t my exactly most compelling response.</p>
<p>I’d like to outline a bit of SML module system here to help substantiate why looking at an ML is A Good Thing. All the code here should be translatable to OCaml if that’s more your taste.</p>
<h2 id="concepts">Concepts</h2>
<p>In ML languages modules are a well thought out portion of the language. They aren’t just “Oh we need to separate these names… modules should work”. Like any good language they have methods for abstraction and composition. Additionally, like any good part of an ML language, modules have an expressive type language for mediating how composition and abstraction works.</p>
<p>So to explain how this module system functions as a whole, we’ll cover 3 subjects</p>
<ol type="1">
<li>Structures</li>
<li>Signatures</li>
<li>Functors</li>
</ol>
<p>Giving a cursory overview of what each thing is and how it might be used.</p>
<h2 id="structures">Structures</h2>
<p>Structures are the values in the module language. They are how we actually create a module. The syntax for them is</p>
<pre class="sml"><code>    struct
      fun flip f x y = f y x
      datatype &#39;a list = Con of (&#39;a * &#39;a list) | Nil
      ...
    end</code></pre>
<p><em>A quick note to Haskellers, in ML types are lower case and type variables are written with ’s. Type constructors are applied “backwards” so <code>List a</code> is <code>'a list</code>.</em></p>
<p>So they’re just a bunch of a declarations stuffed in between a <code>struct</code> and <code>end</code>. This is a bit useless if we can’t bind it to a name. For that there’s</p>
<pre class="sml"><code>    structure M = struct val x = 1 end</code></pre>
<p>And now we have a new module <code>M</code> with a single member, <code>x : int</code>. This is just like binding a variable in the term language except a “level up” if you like. We can use this just like you would use modules in any other language.</p>
<pre class="sml"><code>    val x&#39; = M.x + 1</code></pre>
<p>Since <code>struct ... end</code> can contain any list of declarations we can nest module bindings.</p>
<pre class="sml"><code>    structure M&#39; =
      struct
        structure NestedM = M
      end</code></pre>
<p>And access this using <code>.</code>.</p>
<pre class="sml"><code>    val sum = M&#39;.NestedM.x + M.x</code></pre>
<p>As you can imagine, it would get a bit tedious if we needed to <code>.</code> our way to every single module access. For that we have <code>open</code> which just dumps a module’s exposed contents into our namespace. What’s particularly interesting about <code>open</code> is that it is a “normal” declaration and can be nested with <code>let</code>.</p>
<pre class="sml"><code>    fun f y =
      let open M in
        x + y
      end</code></pre>
<p>OCaml has gone a step further and added special syntax for small opens. The “local opens” would turn our code into</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode ocaml"><code class="sourceCode ocaml"><a class="sourceLine" id="cb7-1" data-line-number="1">    <span class="kw">let</span> f y = M.(x + y)</a></code></pre></div>
<p>This already gives us a lot more power than your average module system. Structures basically encapsulate what we’d expect in a module system, but</p>
<ol type="1">
<li>Structures =/= files</li>
<li>Structures can be bound to names</li>
<li>Structures can be nested</li>
</ol>
<p>Up next is a look at what sort of type system we can impose on our language of structures.</p>
<h2 id="signatures">Signatures</h2>
<p>Now for the same reason we love types in the term language (safety, readability, insert-semireligious-rant) we’d like them in the module language. Happily ML comes equipped with a feature called signatures. Signature values look a lot like structures</p>
<pre class="sml"><code>    sig
      val x : int
      datatype &#39;a list = Cons of (&#39;a * &#39;a list) | Nil
    end</code></pre>
<p>So a signature is a list of declarations <em>without</em> any implementations. We can list algebraic data types, other modules, and even functions and values but we won’t provide any actual code to run them. I like to think of signatures as what most documentation rendering tools show for a module.</p>
<p>As we had with structures, signatures can be given names.</p>
<pre class="sml"><code>    signature MSIG = sig val x : int end</code></pre>
<p>On their own signatures are quite useless, the whole point is that we can apply them to modules after all! To do this we use <code>:</code> just like in the term language.</p>
<pre class="sml"><code>    structure M : MSIG = struct val x = 1 end</code></pre>
<p>When compiled, this will check that <code>M</code> has at least the field <code>x : int</code> inside its structure. We can apply signatures retroactively to both module variables and structure values themselves.</p>
<pre class="sml"><code>    structure M : MSIG = struct val x = 1 end : MSIG</code></pre>
<p>One interesting feature of signatures is the ability to leave certain types abstract. For example, when implementing a map the actual implementation of the core data type doesn’t belong in the signature.</p>
<pre class="sml"><code>    signature MAP =
      sig
        type key
        type &#39;a table

        val empty : &#39;a table
        val insert : key -&gt; &#39;a -&gt; &#39;a table -&gt; &#39;a table
        val lookup : key -&gt; &#39;a table -&gt; &#39;a option
      end</code></pre>
<p>Notice that the type of keys and tables are left abstract. When someone applies a signature they can do so in two ways, weak or strong ascription. Weak ascription (<code>:</code>) means that the constructors of abstract types are still accessible, but the signature <em>does</em> hide all unrelated declarations in the module. Strong ascription (<code>:&gt;</code>) makes the abstract types actually abstract.</p>
<p>Every once in a while we need to modify a signature. We can do this with the keywords <code>where type</code>. For example, we might implement a specialization of <code>MAP</code> for integer keys and want our signature to express this</p>
<pre class="sml"><code>    structure IntMap :&gt; MAP where type key = int =
      struct ... end</code></pre>
<p>This incantation leaves the type of the table abstract but specializes the keys to an int.</p>
<p>Last but not least, let’s talk about abstraction in module land.</p>
<h2 id="functors">Functors</h2>
<p>Last but not least let’s talk about the “killer feature” of ML module systems: functors. Functors are the “lifting” of functions into the module language. A functor is a function that maps modules with a certain signature to functions of a different signature.</p>
<p>Jumping back to our earlier example of maps, the equivalent in Haskell land is <code>Data.Map</code>. The big difference is that Haskell gives us maps for all keys that implement <code>Ord</code>. Our signature doesn’t give us a clear way to associate all these different modules, one for each <code>Ord</code>erable key, that are really the same thing. We can represent this relationship in SML with</p>
<pre class="sml"><code>    signature ORD =
      sig
        type t
        val compare : t * t -&gt; order
      end

    functor RBTree (O : ORD) : MAP where type key = O.t =
      struct
        open O
        ....
      end</code></pre>
<p>Which reads as “For any module implementing <code>Ord</code>, I can give you a module implementing <code>MAP</code> which keys of type <code>O.t</code>”. We can then instantiate these</p>
<pre class="sml"><code>    structure IntOrd =
      struct
        type t = int
        val compare = Int.compare end
      end
    structure IntMap = RBTree(IntOrd)</code></pre>
<p>Sadly SML’s module language isn’t higher order. This means we can’t assign functors a type (there isn’t an equivalent of <code>-&gt;</code>) and we can’t pass functors to functors. Even with this restriction functors are tremendously useful.</p>
<p>One interesting difference between SML and OCaml is how functors handle abstract types. Specifically, is it the case that</p>
<pre><code>F(M).t = F(M).t</code></pre>
<p>In SML the answer is (surprisingly) no! Applying a functor generates brand new abstract types. This is actually beneficial when you remember SML and OCaml aren’t pure. For example you might write a functor for handling symbol tables and internally use a mutable symbol table. One nifty trick would be to keep of type of symbols abstract. If you only give back a symbol upon registering something in the table, this would mean that all symbols a user can supply are guaranteed to correspond to some entry.</p>
<p>This falls apart however if functors are extensional. Consider the following REPL session</p>
<pre class="sml"><code>    &gt; structure S1 = SymbolTable(WhateverParameters)
    &gt; structure S2 = SymbolTable(WhateverParameters)
    &gt; val key = S1.register &quot;I&#39;m an entry&quot;
    &gt; S2.lookup key
    Error: no such key!</code></pre>
<p>This will not work if <code>S1</code> and <code>S2</code> have separate key types.</p>
<p>To my knowledge, the general conclusion is that generative functors (ala SML) are good for impure code, but applicative functors (ala OCaml and BackPack) really shine with pure code.</p>
<h2 id="wrap-up">Wrap Up</h2>
<p>We’ve covered a lot of ground in this post. This wasn’t an exhaustive tour of every feature of ML module systems, but hopefully I got the jist across.</p>
<p>If there’s one point to take home: In a lot of languages modules are clearly a bolted on construction. They’re something added on later to fix “that library problem” and generally consist of the same “module &lt;-&gt; file” and “A module imports others to bring them into scope”. In ML that’s simply not the case. The module language is a rich, well thought out thing with it’s own methods of abstraction, composition, and even a notion of types!</p>
<p>I wholeheartedly recommend messing around a bit with OCaml or SML to see how having these things impacts your thought process. I think you’ll be pleasantly surprised.</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Thu, 08 Jan 2015 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2015-01-08-modules.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Examining Hackage: folds</title>
    <link>http://jozefg.bitbucket.org/posts/2014-12-27-folds.html</link>
    <description><![CDATA[<div class="info">
    Posted on December 27, 2014
    
</div>
<div class="info">
    
    Tags: <a href="/tags/haskell.html">haskell</a>
    
</div>

<p>In keeping with the rest of the “Examining Hackage” series I’d like to go through the source <code>folds</code> package today. We’ll try to go through most of the code in an attempt to understand what exactly <code>folds</code> does and how it does it. To be honest, I hadn’t actually heard of this one until someone mentioned it to me on /r/haskell but it looks pretty cool. It also has the word “comonadic” in the description, how can I resist?</p>
<p>It’s similar to Gabriel’s <code>foldl</code> library, but it also seems to provide a wider suite of types folds. In retrospect, folds has a general framework for talking about types of folds and composing them where as <code>foldl</code> defines only 2 types of folds, but defines a whole heap of prebuilt (left) folds.</p>
<h2 id="poking-around">Poking Around</h2>
<p>After grabbing the source and looking at the files we see that <code>folds</code> is actually reasonable large</p>
<pre><code>~$ cabal get folds &amp;&amp; cd folds-0.6.2 &amp;&amp; ag -g &quot;hs$&quot;
    src/Data/Fold.hs
    src/Data/Fold/L.hs
    src/Data/Fold/L&#39;.hs
    src/Data/Fold/Class.hs
    src/Data/Fold/M1.hs
    src/Data/Fold/L1.hs
    src/Data/Fold/R.hs
    src/Data/Fold/Internal.hs
    src/Data/Fold/L1&#39;.hs
    src/Data/Fold/R1.hs
    src/Data/Fold/M.hs
    Setup.lhs
    tests/hlint.hs</code></pre>
<p>One that jumps out at me is <code>Internal</code> since it likely doesn’t depend on anything. We’ll start there.</p>
<h2 id="internal">Internal</h2>
<p>Looking at the top gives a hint for what we’re in for</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb2-1" data-line-number="1">    <span class="ot">{-# LANGUAGE FlexibleContexts #-}</span></a>
<a class="sourceLine" id="cb2-2" data-line-number="2">    <span class="ot">{-# LANGUAGE UndecidableInstances #-}</span></a>
<a class="sourceLine" id="cb2-3" data-line-number="3">    <span class="ot">{-# LANGUAGE ScopedTypeVariables #-}</span></a>
<a class="sourceLine" id="cb2-4" data-line-number="4">    <span class="ot">{-# LANGUAGE DeriveDataTypeable #-}</span></a>
<a class="sourceLine" id="cb2-5" data-line-number="5">    <span class="kw">module</span> <span class="dt">Data.Fold.Internal</span></a>
<a class="sourceLine" id="cb2-6" data-line-number="6">      ( <span class="dt">SnocList</span>(<span class="fu">..</span>)</a>
<a class="sourceLine" id="cb2-7" data-line-number="7">      , <span class="dt">SnocList1</span>(<span class="fu">..</span>)</a>
<a class="sourceLine" id="cb2-8" data-line-number="8">      , <span class="dt">List1</span>(<span class="fu">..</span>)</a>
<a class="sourceLine" id="cb2-9" data-line-number="9">      , <span class="dt">Maybe&#39;</span>(<span class="fu">..</span>), maybe&#39;</a>
<a class="sourceLine" id="cb2-10" data-line-number="10">      , <span class="dt">Pair&#39;</span>(<span class="fu">..</span>)</a>
<a class="sourceLine" id="cb2-11" data-line-number="11">      , <span class="dt">N</span>(<span class="fu">..</span>)</a>
<a class="sourceLine" id="cb2-12" data-line-number="12">      , <span class="dt">Tree</span>(<span class="fu">..</span>)</a>
<a class="sourceLine" id="cb2-13" data-line-number="13">      , <span class="dt">Tree1</span>(<span class="fu">..</span>)</a>
<a class="sourceLine" id="cb2-14" data-line-number="14">      , <span class="dt">An</span>(<span class="fu">..</span>)</a>
<a class="sourceLine" id="cb2-15" data-line-number="15">      , <span class="dt">Box</span>(<span class="fu">..</span>)</a>
<a class="sourceLine" id="cb2-16" data-line-number="16">      ) <span class="kw">where</span></a></code></pre></div>
<p>This module seems to be mostly a bunch of (presumably useful) data types + their instances for <code>Foldable</code>, <code>Functor</code>, and <code>Traversable</code>. Since all 3 of these are simple enough you can actually just derive them I’ll elide them in most cases.</p>
<p>First up is <code>SnocList</code>, if the name didn’t give it away it is a backwards list (snoc is cons backwards)</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb3-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">SnocList</span> a <span class="fu">=</span> <span class="dt">Snoc</span> (<span class="dt">SnocList</span> a) a <span class="fu">|</span> <span class="dt">Nil</span></a>
<a class="sourceLine" id="cb3-2" data-line-number="2">      <span class="kw">deriving</span> (<span class="dt">Eq</span>,<span class="dt">Ord</span>,<span class="dt">Show</span>,<span class="dt">Read</span>,<span class="dt">Typeable</span>,<span class="dt">Data</span>)</a></code></pre></div>
<p>Then we have the boilerplatey instances for <code>Functor</code> and <code>Foldable</code>. What’s a bit odd is that both <code>foldl</code> and <code>foldMap</code> are implemented where we only need <code>foldl</code>. Presumably this is because just <code>foldMap</code> gives worse performance but that’s a little disappointing.</p>
<p>Next is <code>SnocList1</code> and <code>List1</code> which are quite similar.</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">SnocList1</span> a <span class="fu">=</span> <span class="dt">Snoc1</span> (<span class="dt">SnocList1</span> a) a <span class="fu">|</span> <span class="dt">First</span> a</a>
<a class="sourceLine" id="cb4-2" data-line-number="2">      <span class="kw">deriving</span> (<span class="dt">Eq</span>,<span class="dt">Ord</span>,<span class="dt">Show</span>,<span class="dt">Read</span>,<span class="dt">Typeable</span>,<span class="dt">Data</span>)</a>
<a class="sourceLine" id="cb4-3" data-line-number="3"></a>
<a class="sourceLine" id="cb4-4" data-line-number="4">    <span class="kw">data</span> <span class="dt">List1</span> a <span class="fu">=</span> <span class="dt">Cons1</span> a (<span class="dt">List1</span> a) <span class="fu">|</span> <span class="dt">Last</span> a</a></code></pre></div>
<p>If you’ve never seen this before, notice how instead of <code>Nil</code> we have a constructor which requires an element. This means that no matter how we construct a list we need to supply at least element. Among other things this means that <code>head</code> would be safe.</p>
<p>We also have a couple strict structures. Notice that these cannot be functors since they break <code>fmap f . fmap g = fmap (f . g)</code> (why?). We have</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb5-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">Maybe&#39;</span> a <span class="fu">=</span> <span class="dt">Nothing&#39;</span> <span class="fu">|</span> <span class="dt">Just&#39;</span> <span class="fu">!</span>a</a>
<a class="sourceLine" id="cb5-2" data-line-number="2"></a>
<a class="sourceLine" id="cb5-3" data-line-number="3">    <span class="kw">data</span> <span class="dt">Pair&#39;</span> a b <span class="fu">=</span> <span class="dt">Pair&#39;</span> <span class="fu">!</span>a <span class="fu">!</span>b</a></code></pre></div>
<p>And we have the obvious instance for <code>Foldable Maybe'</code> and <code>Monoid (a, b)</code>. Now it may seem a little silly to define these types, but from experience I can say anything that makes strictness a bit more explicit is wonderfully helpful. Now we can just use <code>seq</code> on a <code>Pair'</code> and know that both components will be forced.</p>
<p>Next we define a type for trees. One thing I noticed was the docs mentioned that this type reflects the structure of a <code>foldMap</code></p>
<div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb6-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">Tree</span> a</a>
<a class="sourceLine" id="cb6-2" data-line-number="2">      <span class="fu">=</span> <span class="dt">Zero</span></a>
<a class="sourceLine" id="cb6-3" data-line-number="3">      <span class="fu">|</span> <span class="dt">One</span> a</a>
<a class="sourceLine" id="cb6-4" data-line-number="4">      <span class="fu">|</span> <span class="dt">Two</span> (<span class="dt">Tree</span> a) (<span class="dt">Tree</span> a)</a>
<a class="sourceLine" id="cb6-5" data-line-number="5">      <span class="kw">deriving</span> (<span class="dt">Eq</span>,<span class="dt">Ord</span>,<span class="dt">Show</span>,<span class="dt">Read</span>,<span class="dt">Typeable</span>,<span class="dt">Data</span>)</a></code></pre></div>
<p>When we <code>foldMap</code> each <code>One</code> should be an element of the original collection. From there we can <code>fmap</code> with the <code>map</code> part of <code>foldMap</code>, and we can imagine traversing the tree and replacing <code>Two l r</code> with <code>l &lt;&gt; r</code>, each <code>Zero</code> with <code>mempty</code>, and each <code>One a</code> with a.</p>
<p>So that’s rather nifty. On top of this we have <code>Foldable</code>, <code>Traversable</code>, and <code>Functor</code> instances.</p>
<p>We also have <code>Tree1</code> which is similar but elides the <code>Zero</code></p>
<div class="sourceCode" id="cb7"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb7-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">Tree1</span> a <span class="fu">=</span> <span class="dt">Bin1</span> (<span class="dt">Tree1</span> a) (<span class="dt">Tree1</span> a) <span class="fu">|</span> <span class="dt">Tip1</span> a</a></code></pre></div>
<p>As you’d expect, this implements the same type classes as <code>Tree</code>.</p>
<p>Now is where things get a bit weird. First up is a type for reifying monoids using <code>reflection</code>. I actually was thinking about doing a post on it and then I discovered Austin Seipp has done an <a href="https://www.fpcomplete.com/user/thoughtpolice/using-reflection">outstanding one</a>. So we have this <code>N</code> type with the definition</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb8-1" data-line-number="1">    <span class="kw">newtype</span> <span class="dt">N</span> a s <span class="fu">=</span> <span class="dt">N</span> {<span class="ot"> runN ::</span> a }</a>
<a class="sourceLine" id="cb8-2" data-line-number="2">      <span class="kw">deriving</span> (<span class="dt">Eq</span>,<span class="dt">Ord</span>,<span class="dt">Show</span>,<span class="dt">Read</span>,<span class="dt">Typeable</span>,<span class="dt">Data</span>)</a></code></pre></div>
<p>Now with reflection there are two key components, there’s the type class instance floating around and a fresh type <code>s</code> that keys it. If we have <code>s</code> then we can easily demand a specific instance with <code>reflect (Proxy :: Proxy s)</code>. That’s exactly what we do here. We can create a monoid instance using this trick with</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb9-1" data-line-number="1">    <span class="kw">instance</span> <span class="dt">Reifies</span> s (a <span class="ot">-&gt;</span> a <span class="ot">-&gt;</span> a, a) <span class="ot">=&gt;</span> <span class="dt">Monoid</span> (<span class="dt">N</span> a s) <span class="kw">where</span></a>
<a class="sourceLine" id="cb9-2" data-line-number="2">      mempty <span class="fu">=</span> <span class="dt">N</span> <span class="fu">$</span> snd <span class="fu">$</span> reflect (<span class="dt">Proxy</span><span class="ot"> ::</span> <span class="dt">Proxy</span> s)</a>
<a class="sourceLine" id="cb9-3" data-line-number="3">      mappend (<span class="dt">N</span> a) (<span class="dt">N</span> b) <span class="fu">=</span> <span class="dt">N</span> <span class="fu">$</span> fst (reflect (<span class="dt">Proxy</span><span class="ot"> ::</span> <span class="dt">Proxy</span> s)) a b</a></code></pre></div>
<p>So at each point we use our <code>s</code> to grab the tuple of monoid operations we expect to be around and use them in the obvious manner. The only reason I could imagine doing this is if we had a structure which we want to use as a monoid in a number of different ways. I suppose we also could have just passed the dictionary around but maybe this was extremely ugly. We shall see later I suppose.</p>
<p>Last comes two data types I do not understand at all. There’s <code>An</code> and <code>Box</code>. The look extremely boring.</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb10-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">Box</span> a <span class="fu">=</span> <span class="dt">Box</span> a</a>
<a class="sourceLine" id="cb10-2" data-line-number="2">    <span class="kw">newtype</span> <span class="dt">An</span> a <span class="fu">=</span> <span class="dt">An</span> a</a></code></pre></div>
<p>Their instances are the same everywhere as well.. I have no clue what these are for. Grepping shows they are used though so hopefully this mystery will become clearer as we go.</p>
<h2 id="class">Class</h2>
<p>Going in order of the module DAG gives us <code>Data.Fold.Class.hs</code>. This exports two type classes and one function</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb11-1" data-line-number="1">    <span class="kw">module</span> <span class="dt">Data.Fold.Class</span></a>
<a class="sourceLine" id="cb11-2" data-line-number="2">      ( <span class="dt">Scan</span>(<span class="fu">..</span>)</a>
<a class="sourceLine" id="cb11-3" data-line-number="3">      , <span class="dt">Folding</span>(<span class="fu">..</span>)</a>
<a class="sourceLine" id="cb11-4" data-line-number="4">      , beneath</a>
<a class="sourceLine" id="cb11-5" data-line-number="5">      ) <span class="kw">where</span></a></code></pre></div>
<p>One thing that worries me a little is that this imports <code>Control.Lens</code> which I don’t understand nearly as well as I’d like to.. We’ll see how this turns out.</p>
<p>Our first class is</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb12-1" data-line-number="1">    <span class="kw">class</span> <span class="dt">Choice</span> p <span class="ot">=&gt;</span> <span class="dt">Scan</span> p <span class="kw">where</span></a>
<a class="sourceLine" id="cb12-2" data-line-number="2"><span class="ot">      prefix1 ::</span> a <span class="ot">-&gt;</span> p a b <span class="ot">-&gt;</span> p a b</a>
<a class="sourceLine" id="cb12-3" data-line-number="3"><span class="ot">      postfix1 ::</span> p a b <span class="ot">-&gt;</span> a <span class="ot">-&gt;</span> p a b</a>
<a class="sourceLine" id="cb12-4" data-line-number="4"><span class="ot">      run1 ::</span> a <span class="ot">-&gt;</span> p a b <span class="ot">-&gt;</span> b</a>
<a class="sourceLine" id="cb12-5" data-line-number="5"><span class="ot">      interspersing ::</span> a <span class="ot">-&gt;</span> p a b <span class="ot">-&gt;</span> p a b</a></code></pre></div>
<p>So right away we notice this is a subclass of <code>Choice</code> which is in turn a subclass of <a href="https://www.fpcomplete.com/user/liyang/profunctors"><code>Profunctor</code></a>. <code>Choice</code> captures the ability to pull an <code>Either</code> through our profunctor.</p>
<div class="sourceCode" id="cb13"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb13-1" data-line-number="1"><span class="ot">    left&#39; ::</span> p a b <span class="ot">-&gt;</span> p (<span class="dt">Either</span> a c) (<span class="dt">Either</span> b c)</a>
<a class="sourceLine" id="cb13-2" data-line-number="2"><span class="ot">    right&#39; ::</span> p a b <span class="ot">-&gt;</span> p (<span class="dt">Either</span> c a) (<span class="dt">Either</span> c b)</a></code></pre></div>
<p>Note that we can’t do this with ordinary profunctors since we’d need a function from <code>Either a c -&gt; a</code> which isn’t complete.</p>
<p>Back to <code>Scan p</code>. <code>Scan p</code> takes a profunctor which apparently represents our folds. We then can prefix the input we supply, postfix the input we supply, and run our fold on a single element of input. This is a bit weird to me, I’m not sure if the intention is to write something like</p>
<div class="sourceCode" id="cb14"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb14-1" data-line-number="1"><span class="ot">    foldList ::</span> <span class="dt">Scan</span> p <span class="ot">=&gt;</span> [a] <span class="ot">-&gt;</span> p a b <span class="ot">-&gt;</span> b</a>
<a class="sourceLine" id="cb14-2" data-line-number="2">    foldList [x] <span class="fu">=</span> run1 x</a>
<a class="sourceLine" id="cb14-3" data-line-number="3">    foldList (x <span class="fu">:</span> xs) <span class="fu">=</span> foldList xs <span class="fu">.</span> prefix1 x</a></code></pre></div>
<p>or something else entirely. Additionally this doesn’t really conform to my intuition of what a scan is. I’d expect a scan to produce all of the intermediate output involved in folding. At this point, with no instances in scope, it’s a little tricky to see what’s supposed to be happening here.</p>
<p>There are a bunch of default-signature based implementations of these methods if your type implements <code>Foldable</code>. Since this is the next type class in the module let’s look at that and then skip back to the defaults.</p>
<div class="sourceCode" id="cb15"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb15-1" data-line-number="1">    <span class="kw">class</span> <span class="dt">Scan</span> p <span class="ot">=&gt;</span> <span class="dt">Folding</span> p <span class="kw">where</span></a>
<a class="sourceLine" id="cb15-2" data-line-number="2"><span class="ot">      prefix ::</span> <span class="dt">Foldable</span> t <span class="ot">=&gt;</span> t a <span class="ot">-&gt;</span> p a b <span class="ot">-&gt;</span> p a b</a>
<a class="sourceLine" id="cb15-3" data-line-number="3"><span class="ot">      prefixOf ::</span> <span class="dt">Fold</span> s a <span class="ot">-&gt;</span> s <span class="ot">-&gt;</span> p a b <span class="ot">-&gt;</span> p a b</a>
<a class="sourceLine" id="cb15-4" data-line-number="4"><span class="ot">      postfix ::</span> <span class="dt">Foldable</span> t <span class="ot">=&gt;</span> p a b <span class="ot">-&gt;</span> t a <span class="ot">-&gt;</span> p a b</a>
<a class="sourceLine" id="cb15-5" data-line-number="5"><span class="ot">      postfixOf ::</span> <span class="dt">Fold</span> s a <span class="ot">-&gt;</span> p a b <span class="ot">-&gt;</span> s <span class="ot">-&gt;</span> p a b</a>
<a class="sourceLine" id="cb15-6" data-line-number="6"><span class="ot">      run ::</span> <span class="dt">Foldable</span> t <span class="ot">=&gt;</span> t a <span class="ot">-&gt;</span> p a b <span class="ot">-&gt;</span> b</a>
<a class="sourceLine" id="cb15-7" data-line-number="7"><span class="ot">      runOf ::</span> <span class="dt">Fold</span> s a <span class="ot">-&gt;</span> s <span class="ot">-&gt;</span> p a b <span class="ot">-&gt;</span> b</a>
<a class="sourceLine" id="cb15-8" data-line-number="8"><span class="ot">      filtering ::</span> (a <span class="ot">-&gt;</span> <span class="dt">Bool</span>) <span class="ot">-&gt;</span> p a b <span class="ot">-&gt;</span> p a b</a></code></pre></div>
<p>At this point I looked at a few of the types and my first thought was “Oh dammit lens..” but it’s actually not so bad! The first thing to do is ignore the <code>*Of</code> functions which work across lens’s <code>Fold</code> type. There seems to be a nice pair for each “running” function where it can work across a <code>Foldable</code> container or lens’s notion of a fold.</p>
<div class="sourceCode" id="cb16"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb16-1" data-line-number="1"><span class="ot">      prefix ::</span> <span class="dt">Foldable</span> t <span class="ot">=&gt;</span> t a <span class="ot">-&gt;</span> p a b <span class="ot">-&gt;</span> p a b</a>
<a class="sourceLine" id="cb16-2" data-line-number="2"><span class="ot">      postfix ::</span> <span class="dt">Foldable</span> t <span class="ot">=&gt;</span> p a b <span class="ot">-&gt;</span> t a <span class="ot">-&gt;</span> p a b</a>
<a class="sourceLine" id="cb16-3" data-line-number="3"><span class="ot">      run ::</span> <span class="dt">Foldable</span> t <span class="ot">=&gt;</span> t a <span class="ot">-&gt;</span> p a b <span class="ot">-&gt;</span> b</a></code></pre></div>
<p>The first two functions let us create a new fold that will accept some input and supplement it with a bunch of other inputs. <code>prefix</code> gives the supplemental input followed by the new input and <code>postfix</code> does the reverse. We can actually supply input and run the whole thing with <code>run</code>.</p>
<p>All of these are defined with <code>folded</code> from lens which reifies a foldable container into a <code>Fold</code>. so <code>foo = fooOf folded</code> is the default implementation for all of these. Now for the corresponding fold functions I’m reading them as “If you give me a lens to treat <code>s</code> as a container that I can get elements from and a fold, I’ll feed the elements of <code>s</code> into the fold.”</p>
<p>The types are tricky, but this type class seems to capture what it means to run a fold across some type of structure.</p>
<p>Now that we’ve seen how <code>An</code> comes in handy. It’s used as a single object <code>Foldable</code> container. Since it’s newtyped, this should basically run the same as just passing a single element in.</p>
<div class="sourceCode" id="cb17"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb17-1" data-line-number="1">    prefix1 <span class="fu">=</span> prefix <span class="fu">.</span> <span class="dt">An</span></a>
<a class="sourceLine" id="cb17-2" data-line-number="2">    run1 <span class="fu">=</span> run <span class="fu">.</span> <span class="dt">An</span></a>
<a class="sourceLine" id="cb17-3" data-line-number="3">    postfix1 p <span class="fu">=</span> postfix p <span class="fu">.</span> <span class="dt">An</span></a></code></pre></div>
<p>So a <code>Scan</code> here apparently means a fold over a single element at a time. Still not sure why this is deserving of the name <code>Scan</code> but there you are.</p>
<p>Last but not least we have a notion of dragging a fold through an optic with <code>beneath</code>.</p>
<div class="sourceCode" id="cb18"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb18-1" data-line-number="1"><span class="ot">    beneath ::</span> <span class="dt">Profunctor</span> p <span class="ot">=&gt;</span> <span class="dt">Optic</span> p <span class="dt">Identity</span> s t a b <span class="ot">-&gt;</span> p a b <span class="ot">-&gt;</span> p s t</a>
<a class="sourceLine" id="cb18-2" data-line-number="2">    beneath l f <span class="fu">=</span> runIdentity <span class="fu">#.</span> l (<span class="dt">Identity</span> <span class="fu">#.</span> f)</a></code></pre></div>
<p>Those <code>#.</code>’s are like <code>lmap</code>s but only work when the function we apply is a “runtime identity”. Basically this means we should be able to tell whether or not we applied the function or just used <code>unsafeCoerce</code> when running the code. Otherwise all we do is set up our fold <code>f</code> to work across <code>Identity</code> and feed it into the optic.</p>
<h2 id="concrete-implementations">Concrete Implementations</h2>
<p>Now a lot of the rest of the code is implementing those two type classes we went over. To figure out where all these implementations are I just ran</p>
<pre><code>~$ cabal repl
  &gt; :info Scan
  ....
  instance Scan R1 -- Defined at src/Data/Fold/R1.hs:25:10
  instance Scan R -- Defined at src/Data/Fold/R.hs:27:10
  instance Scan M1 -- Defined at src/Data/Fold/M1.hs:25:10
  instance Scan M -- Defined at src/Data/Fold/M.hs:33:10
  instance Scan L1&#39; -- Defined at src/Data/Fold/L1&#39;.hs:24:10
  instance Scan L1 -- Defined at src/Data/Fold/L1.hs:25:10
  instance Scan L&#39; -- Defined at src/Data/Fold/L&#39;.hs:33:10
  instance Scan L -- Defined at src/Data/Fold/L.hs:33:10</code></pre>
<p>Looking at the names, I really don’t want to go through each of these with this much detail. Instead I’ll skip all the <code>*1</code>’s and go over <code>R</code>, <code>L'</code>, and <code>M</code> to get a nice sampling of the sort of folds we get.</p>
<h3 id="r.hs">R.hs</h3>
<p>Up first is <code>R.hs</code>. This defines the first type for a fold we’ve seen.</p>
<div class="sourceCode" id="cb20"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb20-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">R</span> a b <span class="fu">=</span> forall r<span class="fu">.</span> <span class="dt">R</span> (r <span class="ot">-&gt;</span> b) (a <span class="ot">-&gt;</span> r <span class="ot">-&gt;</span> r) r</a></code></pre></div>
<p>Reading this as “a right fold from <code>a</code> to <code>b</code>” we notice a few parts here. It looks like that existential <code>r</code> encodes our fold’s inner state and <code>r -&gt; b</code> maps the current state into the result of the fold. That leaves <code>a -&gt; r -&gt; r</code> as the stepping function. All in all this doesn’t look <em>too</em> different from</p>
<div class="sourceCode" id="cb21"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb21-1" data-line-number="1"><span class="ot">    foldAndPresent ::</span> (a <span class="ot">-&gt;</span> r <span class="ot">-&gt;</span> r) <span class="ot">-&gt;</span> r <span class="ot">-&gt;</span> (r <span class="ot">-&gt;</span> b) <span class="ot">-&gt;</span> [a] <span class="ot">-&gt;</span> b</a>
<a class="sourceLine" id="cb21-2" data-line-number="2">    foldAndPresent f z p <span class="fu">=</span> p <span class="fu">.</span> foldr f z</a></code></pre></div>
<p>The rest of this module is devoted to making a <em>lot</em> of instances for <code>R</code>. Some of these are really uninteresting like <code>Bind</code>, but quite a few are enlightening. To start with, <code>Profunctor</code>.</p>
<div class="sourceCode" id="cb22"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb22-1" data-line-number="1">    <span class="kw">instance</span> <span class="dt">Profunctor</span> <span class="dt">R</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb22-2" data-line-number="2">      dimap f g (<span class="dt">R</span> k h z) <span class="fu">=</span> <span class="dt">R</span> (g <span class="fu">.</span> k) (h <span class="fu">.</span> f) z</a>
<a class="sourceLine" id="cb22-3" data-line-number="3">      rmap g (<span class="dt">R</span> k h z) <span class="fu">=</span> <span class="dt">R</span> (g <span class="fu">.</span> k) h z</a>
<a class="sourceLine" id="cb22-4" data-line-number="4">      lmap f (<span class="dt">R</span> k h z) <span class="fu">=</span> <span class="dt">R</span> k (h <span class="fu">.</span> f) z</a></code></pre></div>
<p>This should more or less by what you expect since it’s really the only the way to get the types to fit together. We fit the map from <code>b -&gt; d</code> onto the presentation piece of the fold and stick the map from <code>a -&gt; c</code> onto the stepper so it can take the new pieces of input.</p>
<p>Next we have the instance for <code>Choice</code>.</p>
<div class="sourceCode" id="cb23"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb23-1" data-line-number="1">    <span class="kw">instance</span> <span class="dt">Choice</span> <span class="dt">R</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb23-2" data-line-number="2">      left&#39; (<span class="dt">R</span> k h z) <span class="fu">=</span> <span class="dt">R</span> (_Left <span class="fu">%~</span> k) step (<span class="dt">Left</span> z) <span class="kw">where</span></a>
<a class="sourceLine" id="cb23-3" data-line-number="3">        step (<span class="dt">Left</span> x) (<span class="dt">Left</span> y) <span class="fu">=</span> <span class="dt">Left</span> (h x y)</a>
<a class="sourceLine" id="cb23-4" data-line-number="4">        step (<span class="dt">Right</span> c) _ <span class="fu">=</span> <span class="dt">Right</span> c</a>
<a class="sourceLine" id="cb23-5" data-line-number="5">        step _ (<span class="dt">Right</span> c) <span class="fu">=</span> <span class="dt">Right</span> c</a>
<a class="sourceLine" id="cb23-6" data-line-number="6"></a>
<a class="sourceLine" id="cb23-7" data-line-number="7">      right&#39; (<span class="dt">R</span> k h z) <span class="fu">=</span> <span class="dt">R</span> (_Right <span class="fu">%~</span> k) step (<span class="dt">Right</span> z) <span class="kw">where</span></a>
<a class="sourceLine" id="cb23-8" data-line-number="8">        step (<span class="dt">Right</span> x) (<span class="dt">Right</span> y) <span class="fu">=</span> <span class="dt">Right</span> (h x y)</a>
<a class="sourceLine" id="cb23-9" data-line-number="9">        step (<span class="dt">Left</span> c) _ <span class="fu">=</span> <span class="dt">Left</span> c</a>
<a class="sourceLine" id="cb23-10" data-line-number="10">        step _ (<span class="dt">Left</span> c) <span class="fu">=</span> <span class="dt">Left</span> c</a></code></pre></div>
<p>This was slightly harder for me to read, but it helps to remember that here <code>_Left %~</code> and <code>_Right %~</code> are just mapping over the left and right sides of an <code>Either</code>. That clears up the presentation bit. For the initial state, when we’re pulling our computation through the left side we wrap it in a <code>Left</code>, when we’re pulling it through the right, we wrap it in <code>Right</code>.</p>
<p>The interesting bit is the new <code>step</code> function. It short circuits if either our state or our new value is the wrong side of an <code>Either</code> otherwise it just applies our stepping function and wraps it back up as an <code>Either</code>.</p>
<p>In addition to being a profunctor, <code>R</code> is also a monad and comonad as well as a whole bunch of more finely grained classes built around those two. I’ll just show the <code>Monad</code> <code>Applicative</code>, and <code>Comonad</code> instance here.</p>
<div class="sourceCode" id="cb24"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb24-1" data-line-number="1">    <span class="kw">instance</span> <span class="dt">Applicative</span> (<span class="dt">R</span> a) <span class="kw">where</span></a>
<a class="sourceLine" id="cb24-2" data-line-number="2">      pure b <span class="fu">=</span> <span class="dt">R</span> (\() <span class="ot">-&gt;</span> b) (\_ () <span class="ot">-&gt;</span> ()) ()</a>
<a class="sourceLine" id="cb24-3" data-line-number="3">      <span class="dt">R</span> xf bxx xz <span class="fu">&lt;*&gt;</span> <span class="dt">R</span> ya byy yz <span class="fu">=</span> <span class="dt">R</span></a>
<a class="sourceLine" id="cb24-4" data-line-number="4">        (\(<span class="dt">Pair&#39;</span> x y) <span class="ot">-&gt;</span> xf x <span class="fu">$</span> ya y)</a>
<a class="sourceLine" id="cb24-5" data-line-number="5">        (\b <span class="fu">~</span>(<span class="dt">Pair&#39;</span> x y) <span class="ot">-&gt;</span> <span class="dt">Pair&#39;</span> (bxx b x) (byy b y))</a>
<a class="sourceLine" id="cb24-6" data-line-number="6">        (<span class="dt">Pair&#39;</span> xz yz)</a>
<a class="sourceLine" id="cb24-7" data-line-number="7"></a>
<a class="sourceLine" id="cb24-8" data-line-number="8">    <span class="kw">instance</span> <span class="dt">Comonad</span> (<span class="dt">R</span> a) <span class="kw">where</span></a>
<a class="sourceLine" id="cb24-9" data-line-number="9">      extract (<span class="dt">R</span> k _ z) <span class="fu">=</span> k z</a>
<a class="sourceLine" id="cb24-10" data-line-number="10">      duplicate (<span class="dt">R</span> k h z) <span class="fu">=</span> <span class="dt">R</span> (<span class="dt">R</span> k h) h z</a>
<a class="sourceLine" id="cb24-11" data-line-number="11"></a>
<a class="sourceLine" id="cb24-12" data-line-number="12">    <span class="kw">instance</span> <span class="dt">Monad</span> (<span class="dt">R</span> a) <span class="kw">where</span></a>
<a class="sourceLine" id="cb24-13" data-line-number="13">      return b <span class="fu">=</span> <span class="dt">R</span> (\() <span class="ot">-&gt;</span> b) (\_ () <span class="ot">-&gt;</span> ()) ()</a>
<a class="sourceLine" id="cb24-14" data-line-number="14">      m <span class="fu">&gt;&gt;=</span> f <span class="fu">=</span> <span class="dt">R</span> (\xs a <span class="ot">-&gt;</span> run xs (f a)) (<span class="fu">:</span>) [] <span class="fu">&lt;*&gt;</span> m</a></code></pre></div>
<p>Looking at the <code>Comonad</code> instance nesting a fold within a fold doesn’t change the accumulator, only the presentation. A nested fold is one that runs and returns a <em>new</em> fold which is identical except the starting state is the result of the old fold.</p>
<p>The <code>&lt;*&gt;</code> operator here is kind of nifty. First off it zips both folds together using the strict <code>Pair'</code>. Finally when we get to the presentation stage we map the final state for the left which gives us a function, and the final state for the right maps to its argument. Applying these two gives us our final result.</p>
<p>Notice that there’s some craziness happening with irrefutable patterns. When we call this function we won’t attempt to force the second argument until <code>bxx</code> forces <code>x</code> or <code>byy</code> forces <code>y</code>. This is important because it makes sure that <code>&lt;*&gt;</code> preserves short circuiting.</p>
<p>The monad instance has a suitably boring <code>return</code> and <code>&gt;&gt;=</code> is a bit odd. We have one machine which accumulates all the elements it’s given in a list, this is an “identity fold” of sorts. From there our presentation function returns a lambda which expects an <code>a</code> and runs <code>f a</code> with all the input we’ve saved. We combine this with <code>m</code> by running it in parallel with <code>&lt;*&gt;</code> and feeding the result of <code>m</code> back into the lambda generated by the right.</p>
<p>Now we’re finally in a position to define our <code>Scan</code> and <code>Folding</code> instances. Since the <code>Scan</code> instance can be determined from the <code>Folding</code> one I’ll show <code>Folding</code>.</p>
<div class="sourceCode" id="cb25"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb25-1" data-line-number="1">    <span class="kw">instance</span> <span class="dt">Folding</span> <span class="dt">R</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb25-2" data-line-number="2">      run t (<span class="dt">R</span> k h z)     <span class="fu">=</span> k (foldr h z t)</a>
<a class="sourceLine" id="cb25-3" data-line-number="3">      prefix s            <span class="fu">=</span> extend (run s)</a>
<a class="sourceLine" id="cb25-4" data-line-number="4">      postfix t s         <span class="fu">=</span> run s (duplicate t)</a>
<a class="sourceLine" id="cb25-5" data-line-number="5"></a>
<a class="sourceLine" id="cb25-6" data-line-number="6">      runOf l s (<span class="dt">R</span> k h z) <span class="fu">=</span> k (foldrOf l h z s)</a>
<a class="sourceLine" id="cb25-7" data-line-number="7">      prefixOf l s        <span class="fu">=</span> extend (runOf l s)</a>
<a class="sourceLine" id="cb25-8" data-line-number="8">      postfixOf l t s     <span class="fu">=</span> runOf l s (duplicate t)</a>
<a class="sourceLine" id="cb25-9" data-line-number="9">      filtering p (<span class="dt">R</span> k h z) <span class="fu">=</span> <span class="dt">R</span> k (\a r <span class="ot">-&gt;</span> <span class="kw">if</span> p a <span class="kw">then</span> h a r <span class="kw">else</span> r) z</a></code></pre></div>
<p>It took some time, but I understand how this works! The first thing to notice is that actually running a fold just relies on the <code>foldr</code> we have from <code>Foldable</code>. Postfixing a fold is particularly slick with right folds. Remember that <code>z</code> represents the accumulated state for the remainder of the items in our sequence.</p>
<p>Therefore, to postfix a number of elements all we need do is run the fold on the container we’re given and store the results as the new initial state. This is precisely what happens with <code>run s (duplicate t)</code>.</p>
<p>Now <code>prefix</code> is the inefficient one here. To prefix an element we want to change how presentation works. Instead of just using the default presentation function, we actually want to take the final state we get and run the fold <em>again</em> using this prefixing sequence and then presenting the result. For this we have another helpful comonandic function, <code>extend</code>. This leaks because it holds on to the sequence a lot longer than it needs to.</p>
<p>The rest of these functions are basically the same thing except maybe postfixing (ha) a function with <code>Of</code> here and there.</p>
<h2 id="l.hs">L’.hs</h2>
<p>Next up is (strict) left folds. As with right folds this module is just a data type and a bunch of instances for it.</p>
<div class="sourceCode" id="cb26"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb26-1" data-line-number="1">    forall r<span class="fu">.</span> <span class="dt">L&#39;</span> (r <span class="ot">-&gt;</span> b) (r <span class="ot">-&gt;</span> a <span class="ot">-&gt;</span> r) r</a></code></pre></div>
<p>One thing that surprised me here was that our state <code>r</code> isn’t stored strictly! That’s a bit odd but presumably there’s a good reason for this. Now all the instances for <code>L'</code> are the same as those for <code>R</code> up to isomorphism because the types are well.. isomorphic.</p>
<p>The real difference comes in the instances for <code>Scan</code> and <code>Folding</code>. Remember how <code>Folding R</code> used <code>foldr</code>, well here we just use <code>foldl'</code>. This has the upshot that all the strictness and whatnot is handled entirely by the foldable instance!</p>
<div class="sourceCode" id="cb27"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb27-1" data-line-number="1">    <span class="kw">instance</span> <span class="dt">Folding</span> <span class="dt">L&#39;</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb27-2" data-line-number="2">      run t (<span class="dt">L&#39;</span> k h z)     <span class="fu">=</span> k <span class="fu">$!</span> foldl&#39; h z t</a>
<a class="sourceLine" id="cb27-3" data-line-number="3">      prefix s             <span class="fu">=</span> run s <span class="fu">.</span> duplicate</a>
<a class="sourceLine" id="cb27-4" data-line-number="4">      postfix t s          <span class="fu">=</span> extend (run s) t</a>
<a class="sourceLine" id="cb27-5" data-line-number="5"></a>
<a class="sourceLine" id="cb27-6" data-line-number="6">      runOf l s (<span class="dt">L&#39;</span> k h z) <span class="fu">=</span> k <span class="fu">$!</span> foldlOf&#39; l h z s</a>
<a class="sourceLine" id="cb27-7" data-line-number="7">      prefixOf l s         <span class="fu">=</span> runOf l s <span class="fu">.</span> duplicate</a>
<a class="sourceLine" id="cb27-8" data-line-number="8">      postfixOf l t s      <span class="fu">=</span> extend (runOf l s) t</a>
<a class="sourceLine" id="cb27-9" data-line-number="9">      filtering p (<span class="dt">L&#39;</span> k h z) <span class="fu">=</span> <span class="dt">L&#39;</span> k (\r a <span class="ot">-&gt;</span> <span class="kw">if</span> p a <span class="kw">then</span> h r a <span class="kw">else</span> r) z</a></code></pre></div>
<p>So everywhere we had <code>foldr</code> we have <code>foldl'</code>. The other interesting switch is that our definitions of <code>prefix</code> and <code>postfix</code> are almost perfectly swapped! This actually makes perfect sense when you think about it. In a left fold the state is propagating from the beginning to the end versus a right fold where it propagates from the end to the beginning! So to prefix something when folding to the left we add it to the initial state and when postfixing we use the presentation function to take our final state and continue to fold with it.</p>
<p>If you check above, you’ll find this to be precisely the opposite of what we had for right folds and since they both have the same comonad instance, we can swap the two implementations.</p>
<p>In fact, having read the implementation for right folds I’m noticing that almost everything in this file is <em>so</em> close to what we had before. It really seems like there is a clever abstraction just waiting to break out.</p>
<h2 id="m.hs">M.hs</h2>
<p>Now that we’ve seen how left and right folds are more or less the same, let’s try something completely different! <code>M.hs</code> captures the notion of a <code>foldMap</code> and looks pretty different than what we’ve seen before.</p>
<p>First things first, here’s the type in question.</p>
<div class="sourceCode" id="cb28"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb28-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">M</span> a b <span class="fu">=</span> forall m<span class="fu">.</span> <span class="dt">M</span> (m <span class="ot">-&gt;</span> b) (a <span class="ot">-&gt;</span> m) (m <span class="ot">-&gt;</span> m <span class="ot">-&gt;</span> m) m</a></code></pre></div>
<p>We still have a presentation function <code>m -&gt; b</code>, and we still have an internal state <code>m</code>. However, we also have a conversion function to map our inputted values onto the values we know how to fold together and we have a tensor operation <code>m -&gt; m -&gt; m</code>.</p>
<p>Now as before we have a profunctor instance</p>
<div class="sourceCode" id="cb29"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb29-1" data-line-number="1">    <span class="kw">instance</span> <span class="dt">Profunctor</span> <span class="dt">M</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb29-2" data-line-number="2">      dimap f g (<span class="dt">M</span> k h m e) <span class="fu">=</span> <span class="dt">M</span> (g<span class="fu">.</span>k) (h<span class="fu">.</span>f) m e</a>
<a class="sourceLine" id="cb29-3" data-line-number="3">      rmap g (<span class="dt">M</span> k h m e) <span class="fu">=</span> <span class="dt">M</span> (g<span class="fu">.</span>k) h m e</a>
<a class="sourceLine" id="cb29-4" data-line-number="4">      lmap f (<span class="dt">M</span> k h m e) <span class="fu">=</span> <span class="dt">M</span> k (h<span class="fu">.</span>f) m e</a></code></pre></div>
<p>Which might start to look familiar from what we’ve seen so far. Next we have a <code>Choice</code> instance which is still a little intimidating.</p>
<div class="sourceCode" id="cb30"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb30-1" data-line-number="1">    <span class="kw">instance</span> <span class="dt">Choice</span> <span class="dt">M</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb30-2" data-line-number="2">      left&#39; (<span class="dt">M</span> k h m z) <span class="fu">=</span> <span class="dt">M</span> (_Left <span class="fu">%~</span> k) (_Left <span class="fu">%~</span> h) step (<span class="dt">Left</span> z) <span class="kw">where</span></a>
<a class="sourceLine" id="cb30-3" data-line-number="3">        step (<span class="dt">Left</span> x) (<span class="dt">Left</span> y) <span class="fu">=</span> <span class="dt">Left</span> (m x y)</a>
<a class="sourceLine" id="cb30-4" data-line-number="4">        step (<span class="dt">Right</span> c) _ <span class="fu">=</span> <span class="dt">Right</span> c</a>
<a class="sourceLine" id="cb30-5" data-line-number="5">        step _ (<span class="dt">Right</span> c) <span class="fu">=</span> <span class="dt">Right</span> c</a>
<a class="sourceLine" id="cb30-6" data-line-number="6"></a>
<a class="sourceLine" id="cb30-7" data-line-number="7">      right&#39; (<span class="dt">M</span> k h m z) <span class="fu">=</span> <span class="dt">M</span> (_Right <span class="fu">%~</span> k) (_Right <span class="fu">%~</span> h) step (<span class="dt">Right</span> z) <span class="kw">where</span></a>
<a class="sourceLine" id="cb30-8" data-line-number="8">        step (<span class="dt">Right</span> x) (<span class="dt">Right</span> y) <span class="fu">=</span> <span class="dt">Right</span> (m x y)</a>
<a class="sourceLine" id="cb30-9" data-line-number="9">        step (<span class="dt">Left</span> c) _ <span class="fu">=</span> <span class="dt">Left</span> c</a>
<a class="sourceLine" id="cb30-10" data-line-number="10">        step _ (<span class="dt">Left</span> c) <span class="fu">=</span> <span class="dt">Left</span> c</a></code></pre></div>
<p>As before we use prisms and <code>%~</code> to drag our presentation and conversion functions into <code>Either</code>, similarly our starting state is wrapped in the appropriate constructor and we define a new stepping function with similar characteristic’s to what we’ve seen before.</p>
<p>As before, we’ve got a wonderful world of monads and comonads to dive into now. We’ll start with monads here to mix it up.</p>
<div class="sourceCode" id="cb31"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb31-1" data-line-number="1">    <span class="kw">instance</span> <span class="dt">Applicative</span> (<span class="dt">M</span> a) <span class="kw">where</span></a>
<a class="sourceLine" id="cb31-2" data-line-number="2">      pure b <span class="fu">=</span> <span class="dt">M</span> (\() <span class="ot">-&gt;</span> b) (\_ <span class="ot">-&gt;</span> ()) (\() () <span class="ot">-&gt;</span> ()) ()</a>
<a class="sourceLine" id="cb31-3" data-line-number="3">      <span class="dt">M</span> xf bx xx xz <span class="fu">&lt;*&gt;</span> <span class="dt">M</span> ya by yy yz <span class="fu">=</span> <span class="dt">M</span></a>
<a class="sourceLine" id="cb31-4" data-line-number="4">        (\(<span class="dt">Pair&#39;</span> x y) <span class="ot">-&gt;</span> xf x <span class="fu">$</span> ya y)</a>
<a class="sourceLine" id="cb31-5" data-line-number="5">        (\b <span class="ot">-&gt;</span> <span class="dt">Pair&#39;</span> (bx b) (by b))</a>
<a class="sourceLine" id="cb31-6" data-line-number="6">        (\(<span class="dt">Pair&#39;</span> x1 y1) (<span class="dt">Pair&#39;</span> x2 y2) <span class="ot">-&gt;</span> <span class="dt">Pair&#39;</span> (xx x1 x2) (yy y1 y2))</a>
<a class="sourceLine" id="cb31-7" data-line-number="7">        (<span class="dt">Pair&#39;</span> xz yz)</a>
<a class="sourceLine" id="cb31-8" data-line-number="8"></a>
<a class="sourceLine" id="cb31-9" data-line-number="9">    <span class="kw">instance</span> <span class="dt">Monad</span> (<span class="dt">M</span> a) <span class="kw">where</span></a>
<a class="sourceLine" id="cb31-10" data-line-number="10">      return <span class="fu">=</span> pure</a>
<a class="sourceLine" id="cb31-11" data-line-number="11">      m <span class="fu">&gt;&gt;=</span> f <span class="fu">=</span> <span class="dt">M</span> (\xs a <span class="ot">-&gt;</span> run xs (f a)) <span class="dt">One</span> <span class="dt">Two</span> <span class="dt">Zero</span> <span class="fu">&lt;*&gt;</span> m</a></code></pre></div>
<p>Our <code>return</code>/<code>pure</code> just instantiates a trivial fold that consumes <code>()</code>s and outputs the value we gave it. For <code>&lt;*&gt;</code> we run both machines strictly next to each other and apply the final result of one to the final result of the other.</p>
<p>Bind creates a new fold that creates a tree. This tree contains every input fed to it as it’s folding and stores each merge a node in the tree. While we run this, we also run the original <code>m</code> we were given. Finally, when we reach the end, we apply <code>f</code> to the result of <code>m</code> and run this over the tree we’ve created which is foldable. If you remember back to the comment of <code>Tree a</code> capturing <code>foldMap</code> this is what was meant by it: we’re using a tree to suspend a <code>foldMap</code> until we’re in a position to run it.</p>
<p>Now for comonad.</p>
<div class="sourceCode" id="cb32"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb32-1" data-line-number="1">    <span class="kw">instance</span> <span class="dt">Comonad</span> (<span class="dt">M</span> a) <span class="kw">where</span></a>
<a class="sourceLine" id="cb32-2" data-line-number="2">      extract (<span class="dt">M</span> k _ _ z) <span class="fu">=</span> k z</a>
<a class="sourceLine" id="cb32-3" data-line-number="3">      duplicate (<span class="dt">M</span> k h m z) <span class="fu">=</span> <span class="dt">M</span> (\n <span class="ot">-&gt;</span> <span class="dt">M</span> (k <span class="fu">.</span> m n) h m z) h m z</a></code></pre></div>
<p>We can be pleasantly surprised that most of this code is the same. Extraction grabs our current state and presents it. Duplication creates a fold which will run and return a new fold. This new fold has the same initial state as the original fold, but when it goes to present its results it will merge it with the final state of the outer fold. This is very different from before and I suspect it will significantly impact our <code>Folding</code> instance.</p>
<div class="sourceCode" id="cb33"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb33-1" data-line-number="1">    <span class="kw">instance</span> <span class="dt">Folding</span> <span class="dt">M</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb33-2" data-line-number="2">      run s (<span class="dt">M</span> k h m (<span class="ot">z ::</span> m)) <span class="fu">=</span> reify (m, z) <span class="fu">$</span></a>
<a class="sourceLine" id="cb33-3" data-line-number="3">        \ (<span class="ot">_ ::</span> <span class="dt">Proxy</span> s) <span class="ot">-&gt;</span> k <span class="fu">$</span> runN (foldMap (<span class="dt">N</span> <span class="fu">#.</span> h)<span class="ot"> s ::</span> <span class="dt">N</span> m s)</a>
<a class="sourceLine" id="cb33-4" data-line-number="4">      prefix s (<span class="dt">M</span> k h m (<span class="ot">z ::</span> m)) <span class="fu">=</span> reify (m, z) <span class="fu">$</span></a>
<a class="sourceLine" id="cb33-5" data-line-number="5">        \ (<span class="ot">_ ::</span> <span class="dt">Proxy</span> s) <span class="ot">-&gt;</span> <span class="kw">case</span> runN (foldMap (<span class="dt">N</span> <span class="fu">#.</span> h)<span class="ot"> s ::</span> <span class="dt">N</span> m s) <span class="kw">of</span></a>
<a class="sourceLine" id="cb33-6" data-line-number="6">          x <span class="ot">-&gt;</span> <span class="dt">M</span> (\y <span class="ot">-&gt;</span> k (m x y)) h m z</a>
<a class="sourceLine" id="cb33-7" data-line-number="7">      postfix (<span class="dt">M</span> k h m (<span class="ot">z ::</span> m)) s <span class="fu">=</span> reify (m, z) <span class="fu">$</span></a>
<a class="sourceLine" id="cb33-8" data-line-number="8">        \ (<span class="ot">_ ::</span> <span class="dt">Proxy</span> s) <span class="ot">-&gt;</span> <span class="kw">case</span> runN (foldMap (<span class="dt">N</span> <span class="fu">#.</span> h)<span class="ot"> s ::</span> <span class="dt">N</span> m s) <span class="kw">of</span></a>
<a class="sourceLine" id="cb33-9" data-line-number="9">          y <span class="ot">-&gt;</span> <span class="dt">M</span> (\x <span class="ot">-&gt;</span> k (m x y)) h m z</a>
<a class="sourceLine" id="cb33-10" data-line-number="10">      filtering p (<span class="dt">M</span> k h m z) <span class="fu">=</span> <span class="dt">M</span> k (\a <span class="ot">-&gt;</span> <span class="kw">if</span> p a <span class="kw">then</span> h a <span class="kw">else</span> z) m z</a></code></pre></div>
<p>This was a little intimidating so I took the liberty of ignoring <code>*Of</code> functions which are pretty much the same as what we have here.</p>
<p>To run a fold we use <code>foldMap</code>, but <code>foldMap</code> wants to work over monoids and we only have <code>z</code> and <code>m</code>. To promote this to a type class we use <code>reify</code> and <code>N</code>. Remember <code>N</code> from way back when? It’s the data type that uses reflection to yank a tuple out of our context and treat it as a monoid instance. In all of this code we use <code>reify</code> to introduce a tuple to our environment and <code>N</code> as a pseudo-monoid that uses <code>m</code> and <code>z</code>.</p>
<p>with this in mind, this code uses <code>N #. h</code> which uses the normal conversion function to introduce something into the <code>N</code> monoid. Then <code>foldMap</code> takes care of the rest and all we need do is call <code>runN</code> to extract the results.</p>
<p><code>prefix</code> and <code>postfix</code> are actually markedly similar. They both start by running the fold over the supplied structure which reduces it to an <code>m</code>. From there, we create a new fold which is identical in all respects except the presentation function. The new presentation function uses <code>m</code> to combine the pre/post-fixed result with the new result. If we’re postfixing, the postfixed result is on the right, if we’re prefixing, the left.</p>
<p>What’s particularly stunning is that neither of these leak! We don’t need to hold onto the structure in our new fold so we can prefix and postfix in constant memory.</p>
<h2 id="fold.hs">Fold.hs</h2>
<p>Now that we’ve gone through a bunch of instances of <code>Folding</code> and <code>Scanning</code>, we’re in a position to actually look at what <code>Data.Fold</code> exports.</p>
<div class="sourceCode" id="cb34"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb34-1" data-line-number="1">    <span class="kw">module</span> <span class="dt">Data.Fold</span></a>
<a class="sourceLine" id="cb34-2" data-line-number="2">      ( <span class="dt">Scan</span>(<span class="fu">..</span>)</a>
<a class="sourceLine" id="cb34-3" data-line-number="3">      , <span class="dt">Folding</span>(<span class="fu">..</span>)</a>
<a class="sourceLine" id="cb34-4" data-line-number="4">      , beneath</a>
<a class="sourceLine" id="cb34-5" data-line-number="5">      , <span class="dt">L1</span>(<span class="fu">..</span>)  <span class="co">-- lazy Mealy machine</span></a>
<a class="sourceLine" id="cb34-6" data-line-number="6">      , <span class="dt">L1&#39;</span>(<span class="fu">..</span>) <span class="co">-- strict Mealy machine</span></a>
<a class="sourceLine" id="cb34-7" data-line-number="7">      , <span class="dt">M1</span>(<span class="fu">..</span>) <span class="co">-- semigroup reducer</span></a>
<a class="sourceLine" id="cb34-8" data-line-number="8">      , <span class="dt">R1</span>(<span class="fu">..</span>) <span class="co">-- reversed lazy Mealy machine</span></a>
<a class="sourceLine" id="cb34-9" data-line-number="9">      , <span class="dt">L</span>(<span class="fu">..</span>) <span class="co">-- lazy Moore machine</span></a>
<a class="sourceLine" id="cb34-10" data-line-number="10">      , <span class="dt">L&#39;</span>(<span class="fu">..</span>) <span class="co">-- strict Moore machine</span></a>
<a class="sourceLine" id="cb34-11" data-line-number="11">      , <span class="dt">M</span>(<span class="fu">..</span>) <span class="co">-- monoidal reducer</span></a>
<a class="sourceLine" id="cb34-12" data-line-number="12">      , <span class="dt">R</span>(<span class="fu">..</span>) <span class="co">-- reversed lazy Moore machine</span></a>
<a class="sourceLine" id="cb34-13" data-line-number="13">      , <span class="dt">AsRM1</span>(<span class="fu">..</span>)</a>
<a class="sourceLine" id="cb34-14" data-line-number="14">      , <span class="dt">AsL1&#39;</span>(<span class="fu">..</span>)</a>
<a class="sourceLine" id="cb34-15" data-line-number="15">      , <span class="dt">AsRM</span>(<span class="fu">..</span>)</a>
<a class="sourceLine" id="cb34-16" data-line-number="16">      , <span class="dt">AsL&#39;</span>(<span class="fu">..</span>)</a>
<a class="sourceLine" id="cb34-17" data-line-number="17">      ) <span class="kw">where</span></a></code></pre></div>
<p>So aside from the folds we’ve examined before, there are 4 new classes, <code>AsRM[1]</code>, and <code>AsL[1]'</code>. We’ll look at the non-1 versions.</p>
<div class="sourceCode" id="cb35"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb35-1" data-line-number="1">    <span class="kw">class</span> <span class="dt">AsRM1</span> p <span class="ot">=&gt;</span> <span class="dt">AsRM</span> p <span class="kw">where</span></a>
<a class="sourceLine" id="cb35-2" data-line-number="2"><span class="ot">      asM ::</span> p a b <span class="ot">-&gt;</span> <span class="dt">M</span> a b</a>
<a class="sourceLine" id="cb35-3" data-line-number="3"><span class="ot">      asR ::</span> p a b <span class="ot">-&gt;</span> <span class="dt">R</span> a b</a></code></pre></div>
<p>So this class covers the class of <code>p</code>’s that know how to convert themselves to middle and right folds. Most of these instances are what you’d expect if you’ve ever done the “write <code>foldl</code> as <code>foldr</code>” trick or similar shenanigans.</p>
<p>For <code>M</code></p>
<div class="sourceCode" id="cb36"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb36-1" data-line-number="1">    <span class="kw">instance</span> <span class="dt">AsRM</span> <span class="dt">M</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb36-2" data-line-number="2">      asR (<span class="dt">M</span> k h m z) <span class="fu">=</span> <span class="dt">R</span> k (m<span class="fu">.</span>h) z</a>
<a class="sourceLine" id="cb36-3" data-line-number="3">      asM <span class="fu">=</span> id</a></code></pre></div>
<p><code>asM</code> is trivially identity and since <code>m</code> is expected to be associative we don’t really care that <code>R</code> is going to associate it strictly to the right. We just glue <code>h</code> onto the front to map the next piece of input into something we know how to merge.</p>
<p>Next is <code>R</code></p>
<div class="sourceCode" id="cb37"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb37-1" data-line-number="1">    <span class="kw">instance</span> <span class="dt">AsRM</span> <span class="dt">R</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb37-2" data-line-number="2">      asM (<span class="dt">R</span> k h z) <span class="fu">=</span> <span class="dt">M</span> (\f <span class="ot">-&gt;</span> k (f z)) h (<span class="fu">.</span>) id</a>
<a class="sourceLine" id="cb37-3" data-line-number="3">      asR <span class="fu">=</span> id</a></code></pre></div>
<p>For right folds we do something a bit different. We transform each value into a function of type <code>m -&gt; m</code> which is the back half of a folding function. We can compose these associatively with <code>.</code> since they are just functions. Finally, when we need to present this, we apply this giant pipeline to the initial state and present the result. Notice here how we took a nonassociative function and bludgeoned it into associativity by partially applying it.</p>
<p>For <code>L'</code> we do something similar</p>
<div class="sourceCode" id="cb38"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb38-1" data-line-number="1">    <span class="kw">instance</span> <span class="dt">AsRM</span> <span class="dt">L&#39;</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb38-2" data-line-number="2">      asR (<span class="dt">L&#39;</span> k h z) <span class="fu">=</span> <span class="dt">R</span> (\f <span class="ot">-&gt;</span> k (f z)) (\b g x <span class="ot">-&gt;</span> g <span class="fu">$!</span> h x b) id</a>
<a class="sourceLine" id="cb38-3" data-line-number="3">      asM <span class="fu">=</span> asM <span class="fu">.</span> asR</a></code></pre></div>
<p>We once again build up a pipeline of functions to make everything associative and apply it at the end. We can’t just use <code>.</code> though for composition because we need to force intermediate results. That’s why you see <code>\b g x -&gt; g $! h x b</code>, it’s just strict composition.</p>
<p>It makes sense that we’d bundle right and monoidal folds together because every right fold can be converted to a monoidal and every monoidal fold to a right. That means that every time we can satisfy one of these functions we can build the second.</p>
<p>This isn’t the case for left folds because we can’t convert a monoidal or right fold to a left one. For the people who are dubious of this, <code>foldl</code> doesn’t let us capture the same amount of laziness we need. I forgot about this too and subsequently hung my machine trying to prove Edward Kmett wrong.</p>
<p>This means that the <code>AsL'</code> is a fairly boring class,</p>
<div class="sourceCode" id="cb39"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb39-1" data-line-number="1">    <span class="kw">class</span> (<span class="dt">AsRM</span> p, <span class="dt">AsL1&#39;</span> p) <span class="ot">=&gt;</span> <span class="dt">AsL&#39;</span> p <span class="kw">where</span></a>
<a class="sourceLine" id="cb39-2" data-line-number="2"><span class="ot">      asL&#39; ::</span> p a b <span class="ot">-&gt;</span> <span class="dt">L&#39;</span> a b</a>
<a class="sourceLine" id="cb39-3" data-line-number="3"></a>
<a class="sourceLine" id="cb39-4" data-line-number="4">    <span class="kw">instance</span> <span class="dt">AsL&#39;</span> <span class="dt">L</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb39-5" data-line-number="5">      asL&#39; (<span class="dt">L</span> k h z) <span class="fu">=</span> <span class="dt">L&#39;</span> (\(<span class="dt">Box</span> r) <span class="ot">-&gt;</span> k r) (\(<span class="dt">Box</span> r) a <span class="ot">-&gt;</span> <span class="dt">Box</span> (h r a)) (<span class="dt">Box</span> z)</a></code></pre></div>
<p>Now we finally see the point of <code>Box</code>, it’s designed to stubbornly block attempts at making its contents strict. You can see this because all the instance for <code>L</code> does is wrap everything in <code>Box</code>es! Since <code>L'</code> is the same as <code>L</code> with some extra <code>seq</code>s, we can use <code>Box</code> to nullify those attempts at strictness and give us a normal left fold.</p>
<p>That’s it! We’re done!</p>
<h2 id="wrap-up">Wrap Up</h2>
<p>Now that we’ve gone through a few concrete implementations and the overall structures in this package hopefully this has come together for you. I must say, I’m really quite surprised at how effectively comonadic operations can capture compositional folds. I’m certainly going to make an effort to use this package or Gabriel’s <a href="http://hackage.haskell.org/package/foldl">foldl</a> a bit more in my random “tiny Haskell utility programs”.</p>
<p>If you’re as entranced by these nice little folding libraries as I am, I’d recommend</p>
<ul>
<li><a href="http://www.haskellforall.com/2013/08/composable-streaming-folds.html">Gabriel’s post</a></li>
<li><a href="https://www.fpcomplete.com/user/edwardk/cellular-automata/part-2">Ed Kmett’s post</a></li>
<li><a href="http://squing.blogspot.com/2008/11/beautiful-folding.html">Max Rabkin’s post</a></li>
</ul>
<p>Trivia fact: this is the longest article out of all 52 posts on Code &amp; Co.</p>
<p><em>Update: I decided it might be helpful to write <a href="http://github.com/jozefg/folds-common">some utility folds</a> for folds. I figured this might be interesting to some.</em></p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Sat, 27 Dec 2014 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2014-12-27-folds.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Examining Hackage: operational</title>
    <link>http://jozefg.bitbucket.org/posts/2014-12-25-operational.html</link>
    <description><![CDATA[<div class="info">
    Posted on December 25, 2014
    
</div>
<div class="info">
    
    Tags: <a href="/tags/haskell.html">haskell</a>
    
</div>

<p>In this installment of “jozefg is confused by other people’s code” we turn to <code>operational</code>. This is a package that’s a little less known than I’d like. It provides a monad for transforming an ADT of instructions, a monad that can be used with <code>do</code> notation and separates out interpretation.</p>
<p>Most people familiar with free monads are wondering what the difference is between operational’s approach and using <a href="http://www.haskellforall.com/2012/06/you-could-have-invented-free-monads.html">free monads</a>. Going into this, I have no clue. Hopefully this will become clear later on.</p>
<h2 id="diving-into-the-source">Diving Into The Source</h2>
<p>Let’s get started shall we</p>
<pre><code>~$ cabal get operational</code></pre>
<p>Happily enough, there’s just one (small) file so we’ll go through that.</p>
<p>To start with <code>Control.Monad.Operational</code> exports</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb2-1" data-line-number="1">    <span class="kw">module</span> <span class="dt">Control.Monad.Operational</span> (</a>
<a class="sourceLine" id="cb2-2" data-line-number="2">        <span class="dt">Program</span>, singleton, <span class="dt">ProgramView</span>, view,</a>
<a class="sourceLine" id="cb2-3" data-line-number="3">        interpretWithMonad,</a>
<a class="sourceLine" id="cb2-4" data-line-number="4">        <span class="dt">ProgramT</span>, <span class="dt">ProgramViewT</span>(<span class="fu">..</span>), viewT,</a>
<a class="sourceLine" id="cb2-5" data-line-number="5">        liftProgram,</a>
<a class="sourceLine" id="cb2-6" data-line-number="6">        ) <span class="kw">where</span></a></code></pre></div>
<p>Like with most “provides a single monad” packages, I’m most interested in how <code>Program</code> works. Looking at this, we see that it’s just a synonym</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb3-1" data-line-number="1">    <span class="kw">type</span> <span class="dt">Program</span> instr <span class="fu">=</span> <span class="dt">ProgramT</span> instr <span class="dt">Identity</span></a></code></pre></div>
<p>Just like the mtl, this is defined in terms of a transformer. So what’s this transformer?</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">ProgramT</span> instr m a <span class="kw">where</span></a>
<a class="sourceLine" id="cb4-2" data-line-number="2">        <span class="dt">Lift</span><span class="ot">   ::</span> m a <span class="ot">-&gt;</span> <span class="dt">ProgramT</span> instr m a</a>
<a class="sourceLine" id="cb4-3" data-line-number="3">        <span class="dt">Bind</span><span class="ot">   ::</span> <span class="dt">ProgramT</span> instr m b <span class="ot">-&gt;</span> (b <span class="ot">-&gt;</span> <span class="dt">ProgramT</span> instr m a)</a>
<a class="sourceLine" id="cb4-4" data-line-number="4">               <span class="ot">-&gt;</span> <span class="dt">ProgramT</span> instr m a</a>
<a class="sourceLine" id="cb4-5" data-line-number="5">        <span class="dt">Instr</span><span class="ot">  ::</span> instr a <span class="ot">-&gt;</span> <span class="dt">ProgramT</span> instr m a</a></code></pre></div>
<p>So <code>ProgramT</code> is a GADT, this is actually important because <code>Bind</code> has an existential type variable: <code>b</code>. Otherwise this is really just a plain tree, I assume <code>(&gt;&gt;=) = Bind</code> and <code>return = Lift . return</code> in the monad instance for this. And finally we can see that instructions are also explicitly supported with <code>Instr</code>.</p>
<p>We can confirm that the <code>Monad</code> instance is as boring as we’d expect with</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb5-1" data-line-number="1">    <span class="kw">instance</span> <span class="dt">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Monad</span> (<span class="dt">ProgramT</span> instr m) <span class="kw">where</span></a>
<a class="sourceLine" id="cb5-2" data-line-number="2">        return <span class="fu">=</span> <span class="dt">Lift</span> <span class="fu">.</span> return</a>
<a class="sourceLine" id="cb5-3" data-line-number="3">        (<span class="fu">&gt;&gt;=</span>)  <span class="fu">=</span> <span class="dt">Bind</span></a>
<a class="sourceLine" id="cb5-4" data-line-number="4"></a>
<a class="sourceLine" id="cb5-5" data-line-number="5">    <span class="kw">instance</span> <span class="dt">MonadTrans</span> (<span class="dt">ProgramT</span> instr) <span class="kw">where</span></a>
<a class="sourceLine" id="cb5-6" data-line-number="6">        lift   <span class="fu">=</span> <span class="dt">Lift</span></a>
<a class="sourceLine" id="cb5-7" data-line-number="7"></a>
<a class="sourceLine" id="cb5-8" data-line-number="8">    <span class="kw">instance</span> <span class="dt">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Functor</span> (<span class="dt">ProgramT</span> instr m) <span class="kw">where</span></a>
<a class="sourceLine" id="cb5-9" data-line-number="9">        fmap   <span class="fu">=</span> liftM</a>
<a class="sourceLine" id="cb5-10" data-line-number="10"></a>
<a class="sourceLine" id="cb5-11" data-line-number="11">    <span class="kw">instance</span> <span class="dt">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Applicative</span> (<span class="dt">ProgramT</span> instr m) <span class="kw">where</span></a>
<a class="sourceLine" id="cb5-12" data-line-number="12">        pure   <span class="fu">=</span> return</a>
<a class="sourceLine" id="cb5-13" data-line-number="13">        (<span class="fu">&lt;*&gt;</span>)  <span class="fu">=</span> ap</a></code></pre></div>
<p>So clearly there’s no interesting computation happening here. Looking at the export list again, we see that there’s a helpful combinator <code>singleton</code> for building up these <code>Program[T]</code>s since they’re kept abstract.</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb6-1" data-line-number="1"><span class="ot">    singleton ::</span> instr a <span class="ot">-&gt;</span> <span class="dt">ProgramT</span> instr m a</a>
<a class="sourceLine" id="cb6-2" data-line-number="2">    singleton <span class="fu">=</span> <span class="dt">Instr</span></a></code></pre></div>
<p>Which once again is very boring.</p>
<p>So this is a lot like free monads it seems since neither one of these actually does much in its monad instance. Indeed the equivalent with free monads would be</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb7-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">Free</span> f a <span class="fu">=</span> <span class="dt">Pure</span> a <span class="fu">|</span> <span class="dt">Free</span> (f (<span class="dt">Free</span> f a))</a>
<a class="sourceLine" id="cb7-2" data-line-number="2">    <span class="kw">instance</span> <span class="dt">Functor</span> f <span class="ot">=&gt;</span> <span class="dt">Monad</span> (<span class="dt">Free</span> f) <span class="kw">where</span></a>
<a class="sourceLine" id="cb7-3" data-line-number="3">      return <span class="fu">=</span> <span class="dt">Pure</span></a>
<a class="sourceLine" id="cb7-4" data-line-number="4">      <span class="dt">Pure</span> a <span class="fu">&gt;&gt;=</span> f <span class="fu">=</span> f a</a>
<a class="sourceLine" id="cb7-5" data-line-number="5">      (<span class="dt">Free</span> a) <span class="fu">&gt;&gt;=</span> f <span class="fu">=</span> <span class="dt">Free</span> (fmap (<span class="fu">&gt;&gt;=</span> f) a)</a>
<a class="sourceLine" id="cb7-6" data-line-number="6"></a>
<a class="sourceLine" id="cb7-7" data-line-number="7"><span class="ot">    singleton ::</span> <span class="dt">Functor</span> f <span class="ot">=&gt;</span> f a <span class="ot">-&gt;</span> <span class="dt">Free</span> f a</a>
<a class="sourceLine" id="cb7-8" data-line-number="8">    singleton <span class="fu">=</span> <span class="dt">Free</span> <span class="fu">.</span> fmap <span class="dt">Pure</span></a></code></pre></div>
<p>The obvious differences is that</p>
<ol type="1">
<li><code>Free</code> requires a functor while <code>Program</code> doesn’t</li>
<li><code>Free</code>s monad instance automatically guarantees laws</li>
</ol>
<p>2 is the bigger one for me. <code>Free</code> has a tighter set of constraints on its <code>f</code> so it can guarantee the monad laws. This is clearly false with <code>Program</code> since <code>return a &gt;&gt;= f</code> introduces an extra <code>Bind</code> instead of just giving <code>f a</code>.</p>
<p>This would explain why <code>ProgramT</code> is kept abstract, it’s hopelessly broken just to expose it in its raw form. Instead what we have to do is somehow partially normalize it before we present it to the user.</p>
<p>Indeed that’s exactly what <code>ProgramViewT</code> is representing. It’s a simpler data type</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb8-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">ProgramViewT</span> instr m a <span class="kw">where</span></a>
<a class="sourceLine" id="cb8-2" data-line-number="2">        <span class="dt">Return</span><span class="ot"> ::</span> a <span class="ot">-&gt;</span> <span class="dt">ProgramViewT</span> instr m a</a>
<a class="sourceLine" id="cb8-3" data-line-number="3"><span class="ot">        (:&gt;&gt;=) ::</span> instr b</a>
<a class="sourceLine" id="cb8-4" data-line-number="4">               <span class="ot">-&gt;</span> (b <span class="ot">-&gt;</span> <span class="dt">ProgramT</span> instr m a)</a>
<a class="sourceLine" id="cb8-5" data-line-number="5">               <span class="ot">-&gt;</span> <span class="dt">ProgramViewT</span> instr m a</a></code></pre></div>
<p>This apparently “compiles” a <code>Program</code> so that everything is either binding an instruction or a pure value. What’s interesting is that this seems to get rid of all <code>Lift</code>’s as well.</p>
<p>How do we produce one of these? Well that seems to be <code>viewT</code>’s job.</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb9-1" data-line-number="1"><span class="ot">    viewT ::</span> <span class="dt">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">ProgramT</span> instr m a <span class="ot">-&gt;</span> m (<span class="dt">ProgramViewT</span> instr m a)</a>
<a class="sourceLine" id="cb9-2" data-line-number="2">    viewT (<span class="dt">Lift</span> m)                <span class="fu">=</span> m <span class="fu">&gt;&gt;=</span> return <span class="fu">.</span> <span class="dt">Return</span></a>
<a class="sourceLine" id="cb9-3" data-line-number="3">    viewT ((<span class="dt">Lift</span> m)     <span class="ot">`Bind`</span> g) <span class="fu">=</span> m <span class="fu">&gt;&gt;=</span> viewT <span class="fu">.</span> g</a>
<a class="sourceLine" id="cb9-4" data-line-number="4">    viewT ((m <span class="ot">`Bind`</span> g) <span class="ot">`Bind`</span> h) <span class="fu">=</span> viewT (m <span class="ot">`Bind`</span> (\x <span class="ot">-&gt;</span> g x <span class="ot">`Bind`</span> h))</a>
<a class="sourceLine" id="cb9-5" data-line-number="5">    viewT ((<span class="dt">Instr</span> i)    <span class="ot">`Bind`</span> g) <span class="fu">=</span> return (i <span class="fu">:&gt;&gt;=</span> g)</a>
<a class="sourceLine" id="cb9-6" data-line-number="6">    viewT (<span class="dt">Instr</span> i)               <span class="fu">=</span> return (i <span class="fu">:&gt;&gt;=</span> return)</a></code></pre></div>
<p>Note that this function returns an <code>m (ProgramViewT instr m a)</code>, not just a plain <code>ProgramViewT</code>. This makes sense because we have to get rid of the lifts. What I think is particularly interesting here is that the 2nd and 3rd cases are just the monad laws!</p>
<p>The second one says binding to a computation is just applying the function to it in the obvious manner. The third re-associates bind in a way guaranteed by the monad laws.</p>
<p>This means that while <code>ProgramT</code> isn’t going to satisfy the monad laws, we can’t tell because all the things said to be equal by the monad laws will compile to the same view. Terribly clever stuff.</p>
<p>The rest of the module is mostly boring stuff like <code>Monad*</code> instances. The last interesting functions is <code>interpretWithMonad</code></p>
<div class="sourceCode" id="cb10"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb10-1" data-line-number="1"><span class="ot">    interpretWithMonad ::</span> forall instr m b<span class="fu">.</span></a>
<a class="sourceLine" id="cb10-2" data-line-number="2">        <span class="dt">Monad</span> m <span class="ot">=&gt;</span> (forall a<span class="fu">.</span> instr a <span class="ot">-&gt;</span> m a) <span class="ot">-&gt;</span> (<span class="dt">Program</span> instr b <span class="ot">-&gt;</span> m b)</a>
<a class="sourceLine" id="cb10-3" data-line-number="3">    interpretWithMonad f <span class="fu">=</span> eval <span class="fu">.</span> view</a>
<a class="sourceLine" id="cb10-4" data-line-number="4">        <span class="kw">where</span></a>
<a class="sourceLine" id="cb10-5" data-line-number="5"><span class="ot">        eval ::</span> forall a<span class="fu">.</span> <span class="dt">ProgramView</span> instr a <span class="ot">-&gt;</span> m a</a>
<a class="sourceLine" id="cb10-6" data-line-number="6">        eval (<span class="dt">Return</span> a) <span class="fu">=</span> return a</a>
<a class="sourceLine" id="cb10-7" data-line-number="7">        eval (m <span class="fu">:&gt;&gt;=</span> k) <span class="fu">=</span> f m <span class="fu">&gt;&gt;=</span> interpretWithMonad f <span class="fu">.</span> k</a></code></pre></div>
<p>This nicely highlights how you’re supposed to write an interpreter for a <code>Program</code>. <code>eval</code> handles the two cases of the <code>view</code> using the mapping to a monad we provided and <code>view</code> handles actually compiling the program into these two cases. All in all, not too shabby.</p>
<h2 id="surprise-there-were-docs-the-whole-time">Surprise, There Were Docs The Whole Time!</h2>
<p>Now I assume that most people didn’t actually download the source to operational, but you really should! Inside you’ll find a whole directory, <code>doc</code>. It contains a few markdown files with explanations and references to the appropriate papers as well as a couple examples of actually building things with <code>operational</code>.</p>
<p>Now that you understand how the current implementation works, you should be able to understand most of what is being said there.</p>
<h2 id="wrap-up">Wrap Up</h2>
<p>So <code>operational</code> illustrates a neat trick I rather like: using modularity to provide an <code>O(1)</code> implementation of <code>&gt;&gt;=</code> and hide its rule breaking with a view.</p>
<p>This package also drops the positivity requirement that <code>Free</code> implies with its functor constraint. Which I suppose means you could have</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb11-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">Foo</span> a <span class="kw">where</span></a>
<a class="sourceLine" id="cb11-2" data-line-number="2">      <span class="dt">Bar</span><span class="ot"> ::</span> (a <span class="ot">-&gt;</span> <span class="fu">...</span>) <span class="ot">-&gt;</span> <span class="dt">Bar</span> a</a></code></pre></div>
<p>Which is potentially useful.</p>
<p>Last but not least, <code>operational</code> really exemplifies having a decent amount of documentation even though there’s only ~100 lines of code. I think the ratio of documentation : code is something like 3 : 1 which I really appreciate.</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Thu, 25 Dec 2014 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2014-12-25-operational.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>What Are Impredicative Types?</title>
    <link>http://jozefg.bitbucket.org/posts/2014-12-23-impredicative.html</link>
    <description><![CDATA[<div class="info">
    Posted on December 23, 2014
    
</div>
<div class="info">
    
    Tags: <a href="/tags/haskell.html">haskell</a>, <a href="/tags/types.html">types</a>
    
</div>

<p>So the results from Stephen’s <a href="http://www.stephendiehl.com/posts/poll.html">poll</a> are in! Surprisingly, impredicative types topped out the list of type system extensions people want to talk about so I figured I can get the ball rolling.</p>
<p>First things first, all the Haskell code will need the magical incantation</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb1-1" data-line-number="1">    <span class="ot">{-# LANGUAGE ImpredicativeTypes #-}</span></a></code></pre></div>
<h2 id="what-is-impredicative-polymorphism">What Is Impredicative Polymorphism</h2>
<p>We have a lot of extensions that make polymorphism more flexible in Haskell, <code>RankNTypes</code> and <code>Rank2Types</code> spring to mind. However, one important feature lacking is “first class polymorphism”.</p>
<p>With impredicative polymorphism <code>forall</code>’s become a normal type like any other. We can embed them in structures, toss them into polymorphic functions, and generally treat them like any other type.</p>
<p>Readers with a mathematical background will wonder why these are called “impredicative” types then. The idea is that since we can have polymorphic types embedded in other structures, we could have something like</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb2-1" data-line-number="1">    <span class="kw">type</span> <span class="dt">T</span> <span class="fu">=</span> (<span class="dt">Int</span>, forall a<span class="fu">.</span> a <span class="ot">-&gt;</span> <span class="dt">Int</span>)</a></code></pre></div>
<p>That <code>a</code> could assume any time <em>including <code>T</code></em>. So each type definition can quantify over itself which nicely corresponds to the mathematical notion of impredicativity.</p>
<p>One simple example where this might come up is when dealing with lenses. Remember lenses have the type</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb3-1" data-line-number="1">    <span class="kw">type</span> <span class="dt">Lens</span> <span class="co">{- viciously -}</span> s t a b <span class="fu">=</span> forall f<span class="fu">.</span> (a <span class="ot">-&gt;</span> f b) <span class="ot">-&gt;</span> s <span class="ot">-&gt;</span> f t</a></code></pre></div>
<p>If we were to embed lenses in let’s say a tuple,</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" data-line-number="1">    <span class="kw">type</span> <span class="dt">TLens</span> a b <span class="fu">=</span> (<span class="dt">Lens</span> a a (a, b) (a, b), <span class="dt">Lens</span> b b (a, b) (a, b))</a>
<a class="sourceLine" id="cb4-2" data-line-number="2"></a>
<a class="sourceLine" id="cb4-3" data-line-number="3"><span class="ot">    foo ::</span> <span class="dt">TLens</span> <span class="dt">Int</span> <span class="dt">Bool</span></a>
<a class="sourceLine" id="cb4-4" data-line-number="4">    foo <span class="fu">=</span> (_1, _2)</a></code></pre></div>
<p>We’d need impredicative types because suddenly a polymorphic type has appeared within a structure.</p>
<h2 id="why-no-one-uses-it">Why No One Uses It</h2>
<p>Now that we’ve seen how amazing impredicative polymorphism, let’s talk about how no one uses it. There are two main reasons</p>
<ol type="1">
<li>GHC’s support for impredicative types is fragile at best and broken at worst</li>
<li>Avoiding the need for impredicative types is very straightforward</li>
</ol>
<p>Reason 1 isn’t exactly a secret. In fact, SPJ has stated a number of times that he’d like to deprecate the extension since it’s very hard to maintain with everything else going on.</p>
<p>As it stands right now, our only choice is more or less to type check a program and add type signatures when GHC decides to instantiate our beautiful polymorphic type with fresh monomorphic type variables.</p>
<p>For this reason alone, impredicative types aren’t really the most useful thing. The final nail in the coffin is that we can easily make things more reliable by using newtypes. In lens for example we avoid impredicativity with</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb5-1" data-line-number="1">    <span class="kw">newtype</span> <span class="dt">ScopedLens</span> s t a b <span class="fu">=</span></a>
<a class="sourceLine" id="cb5-2" data-line-number="2">      <span class="dt">ScopedLens</span> {<span class="ot">getScopedLens ::</span> <span class="dt">Lens</span> s t a b}</a></code></pre></div>
<p>This means that instead of impredicative types we just need rank N types, which are much more polished.</p>
<h2 id="wrap-up">Wrap Up</h2>
<p>Well, I’m sorry to be the bearer of bad news for those who filled out <code>-XImpredicativeTypes</code> on the poll, but there you are.</p>
<p>To end on a positive note however, I do know of two example of where impredicative types did save the day. I’ve used impredicative type <a href="https://gist.github.com/jozefg/d790c0cd09714cc55a5c">exactly once</a> to handle church lists properly. Lennart Augustson’s <a href="http://augustss.blogspot.com/2011/07/impredicative-polymorphism-use-case-in.html">Python DSL</a> makes heavy use of them to present a unified face for variables.</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Tue, 23 Dec 2014 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2014-12-23-impredicative.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Notes on Parametricity</title>
    <link>http://jozefg.bitbucket.org/posts/2014-12-22-parametricity.html</link>
    <description><![CDATA[<div class="info">
    Posted on December 22, 2014
    
</div>
<div class="info">
    
    Tags: <a href="/tags/types.html">types</a>, <a href="/tags/notes.html">notes</a>
    
</div>

<p>I like types. If you haven’t figured this out from my blog I really don’t know where you’ve been looking :) If you’ve ever talked to me in real life about why I like types, chances are I mentioned ease of reasoning and correctness.</p>
<p>Instead of showing how to prove parametricity I’d like to show how to rigorously apply parametricity. So we’ll be a step above handwaving and a step below actually proving everything correct.</p>
<h2 id="what-is-parametricity">What is Parametricity</h2>
<p>At a high level parametricity is about the behavior of well typed terms. It basically says that when we have more polymorphic types, there are fewer programs that type check. For example, the type</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb1-1" data-line-number="1"><span class="ot">    const ::</span> a <span class="ot">-&gt;</span> b <span class="ot">-&gt;</span> a</a></code></pre></div>
<p>Tells us everything we need to know about <code>const</code>. It returns it’s first argument. In fact, if it returns anything (non-bottom) at all, it simply <em>must</em> be its first argument!</p>
<p>Parametricity isn’t limited to simple cases like this however, it can be used to prove that the type</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb2-1" data-line-number="1">    forall c<span class="fu">.</span> c <span class="ot">-&gt;</span> (a <span class="ot">-&gt;</span> c <span class="ot">-&gt;</span> c) <span class="ot">-&gt;</span> c</a></code></pre></div>
<p>Is completely isomorphic to <code>[a]</code>!</p>
<p>We can use parametricity to prove free theorems, like if <code>map id = id</code> then <code>map f . map g = map (f . g)</code>.</p>
<p>These are non-obvious properties and yet parametricity gives us the power to prove all of them without even looking at the implementation of these functions. That’s pretty cool!</p>
<h2 id="handwavy-parametricity">Handwavy Parametricity</h2>
<p>In order to get an idea of how to use parametricity, let’s do some handwavy proofs to get some intuition for how parametricity works.</p>
<p>Start with <code>id</code>.</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb3-1" data-line-number="1"><span class="ot">    id ::</span> a <span class="ot">-&gt;</span> a</a></code></pre></div>
<p>We know right away that <code>id</code> takes some value of type <code>a</code> and returns another value <code>a</code>. Most people would safely guess that the returned value is the one we fed it.</p>
<p>In fact, we can kinda see that this is the <em>only</em> thing it could do. If it didn’t, then somehow it’d have to create a value of type <code>a</code>, but we know that that’s impossible! (Yeah, yeah, I know, bottom. Look the other way for now)</p>
<p>Similarly, if <code>map id</code> is just <code>id</code>, then we know that <code>map</code> isn’t randomly dropping some elements of our list. Since <code>map</code> isn’t removing elements, in order to take an <code>a</code> to a <code>b</code>, <code>map</code> has to be applying <code>f</code> to each element! Since that’s true, we can clearly see that</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" data-line-number="1">    map f <span class="fu">.</span> map g <span class="fu">=</span> map (f <span class="fu">.</span> g)</a></code></pre></div>
<p>because we know that applying <code>f</code> and then applying <code>g</code> is the same as apply <code>f</code> and <code>g</code> at the same time!</p>
<p>Now these handwavy statements are all based on one critical point. No matter how we instantiate a type variable, the behaviour we get is related. Instantiating something to <code>Bool</code> or <code>Int</code> doesn’t change the fundamental behaviour about what we’re instantiated.</p>
<h2 id="background">Background</h2>
<p>Before we can formally define parametricity we need to flesh out a few things. First things first, we need to actually specify the language we’re working in. For our purposes, we’ll just deal with pure System F.</p>
<pre><code>ty ::= v                [Type Variables]
     | ty -&gt; ty         [Function Types]
     | forall v. ty     [Universal Quantification]
     | Bool             [Booleans]

exp ::= v               [Variables]
      | exp exp         [Application]
      | λv : ty -&gt; exp  [Abstraction]
      | Λv -&gt; exp       [Type Abstraction]
      | exp[ty]         [Type Application]
      | true            [Boolean]
      | false           [Boolean]</code></pre>
<p>The only real notable feature of our language is that all polymorphism is explicit. In order to have a full polymorphic type we have to use a “big lambda” Λ. This acts just like a normal lambda except instead of abstracting over a term this abstracts over a type.</p>
<p>For example the full term for the identity function is</p>
<pre><code>id = Λ A -&gt; \x : A -&gt; x</code></pre>
<p>From here we can explicitly specialize a polymorphic type with type application.</p>
<pre><code>id[Bool] true</code></pre>
<p>Aside from this, the typing rules for this language are pretty much identical to Haskell’s. In the interest of brevity I’ll elide them.</p>
<h2 id="actual-parametricity">Actual Parametricity</h2>
<p>Now that we have our language, let’s talk about what we’re interested in proving. Our basic goal is to show that two expressions <code>e1</code> and <code>e2</code> are equal. However, we don’t want to use a <code>==</code> sort of equality. We really mean that they can’t be distinguished by our programs. That for all programs with a “hole”, filling that hole with <code>e1</code> or <code>e2</code> will produce identical results. This is called “observational equivalence” usually and notated with <code>≅</code>.</p>
<p>This is a bit more general than just <code>==</code>, for example it let’s us say that <code>flip const () ≅ id</code>. Now let’s define another notion of equality, logical equivalence.</p>
<p>This logical equivalence is an attempt to define equality without just saying “running everything produces the same result”. It turns out it’s really really hard to prove things that aren’t syntactically equivalent will always produce the same result!</p>
<p>Our logical equivalence <code>~</code> is defined in a context <code>η : δ ↔ δ'</code>. The reason for this is that our terms may have free type variables and we need to know how to deal with them. Each δ maps the free types in the types of our terms to a concrete types and η is a relationship for comparing <code>δ(v)</code> with <code>δ'(v)</code>.</p>
<p>Put less scarily, <code>η</code> is a set of rules that say how to compare two terms when the have both are of type <code>v</code>. This is an important part of our logical relation: it deals with open terms, terms with free variables.</p>
<p>Now η isn’t composed of just any relationship between terms, it has to be “admissible”. Admissibility means that for some relation R, two conditions hold</p>
<ol type="1">
<li>If <code>e R e'</code> and <code>d ⇒ e</code> and <code>d' ⇒ e'</code>, then <code>d R d'</code></li>
<li>If <code>e R e'</code> and <code>d ≅ e</code> and <code>d' ≅ e'</code>, then <code>d R d'</code></li>
</ol>
<p>The first rule means that <code>R</code> is closed under evaluation and the second says that <code>R</code> respects observational equivalence.</p>
<p>Now we define our logical equivalence in some context δ to be</p>
<ol type="1">
<li>When <code>e, e' : τ</code>, <code>e ~ e' [η]</code> if <code>e δ(t) e'</code></li>
<li>When <code>e, e' : Bool</code>, <code>e ~₂ e' [η]</code> if <code>e ⇓ v</code> and <code>e' ⇓ v</code></li>
<li>When <code>f, g : a → b</code>, <code>f ~ g [η]</code> if when <code>a ~ b [η]</code>, <code>f a ~ g b [η]</code></li>
<li>When <code>e e' : ∀ v. t</code>, <code>e ~ e' [η]</code><br />
if <code>R : p ↔ p'</code>, <code>e[p] ~ e'[p'] [η[v ↦ R]]</code></li>
</ol>
<p>Now this rule has 4 cases, one for each type. That’s the first critical bit of this relation, we’re talking about things by the structure of the type, not the value itself.</p>
<p>Now with this in mind we can state the full parametricity theorem.</p>
<blockquote>
<p>For all expressions e and mappings η, <code>e ~ e [η]</code></p>
</blockquote>
<p>That’s it! Now this is only really useful when we’re talking about polymorphic type, then parametricity states that for any admissible relation <code>R</code>, two different instantiations are related.</p>
<p>While I won’t go into how to prove it, another important results we’ll use for proofs with parametricity is that <code>(∀η. e ~ e' [η]) ⇔ e ≅ e'</code>.</p>
<h2 id="applying-parametricity">Applying Parametricity</h2>
<p>Now that I’ve said exactly what parametricity is, I’d like to step through a few proofs. The goal here is to illustrate how we can use this to prove some interesting properties.</p>
<p>First we just have to prove the classic result that any <code>f : forall a. a -&gt; a</code> is equivalent to <code>id = Λa. λx : a. x</code>.</p>
<p>To prove this we need to show <code>f ~ id [η]</code>. For this we need to show that for any admissible relation <code>R</code> between <code>τ</code> and <code>τ'</code>, then <code>f[τ] ~ λx : τ'. x [η[a ↦ R]</code>. Stepping this one more time we end up with the goal that <code>e R e'</code> then <code>f[τ] e ~ e' ⇔ f[τ] e R e'</code></p>
<p>Now this is where things get tricky and where we can apply parametricity. We know by definition that <code>f ~ f [η]</code>. We then choose a new relation <code>S : τ' ↔ τ'</code> where <code>d S d'</code> if and only <code>d ≅ e'</code> and <code>d' ≅ e'</code>. Exercise to the reader: show admissibility.</p>
<p>From here we know that <code>f[τ] ~ f[τ] [η[a ↦ R]]</code> and since <code>e S e</code> then <code>f[τ] e ~ f[τ] e</code> which implies <code>f[τ] e S f[τ] e</code>. This means that <code>f[τ] e ≅ e</code>. From our note above, <code>f[τ] e ~ e</code> and by transitivity we have <code>f[τ] e R e'</code>.</p>
<p>Now we can prove something similar, that <code>(f : a → b → a) ≅ const</code>. The proof is very similar,</p>
<pre><code> f ~ const [η]
 f[τ][ν] ~ const[τ&#39;][ν&#39;] [η[a ↦ R][b ↦ S]]
 f[τ][ν] a b ~ a&#39; [η[a ↦ R][b ↦ S]] where a R a&#39;</code></pre>
<p>Now we need to show that <code>f a b ≅ a</code>. For this we define <code>T</code> to be an admissible relationship where <code>d T d'</code> if and only if <code>d ≅ a ≅ d'</code>. From here we also define <code>U</code> to be an admissible relation where <code>a U b</code> if and only if <code>a ~ b</code>.</p>
<p>Now we know that <code>f ~ f [η]</code> and so</p>
<pre><code>f[τ][ν] ~ f[τ&#39;][ν&#39;] [η[a ↦ T][b ↦ U]]`</code></pre>
<p>And since <code>a T a</code> and <code>b U b</code>, we know that</p>
<pre><code>f[τ][ν] a b ~ f[τ&#39;][ν&#39;] a b [η[a ↦ T][b ↦ U]]</code></pre>
<p>this means that <code>f a b ≅ a</code> and completes our proofs. Hopefully this reinforces the idea of using parametricity and admissible relationships to produces our properties.</p>
<p>Now for something a bit trickier. Church numerals are a classic idea from lambda calculus where</p>
<pre><code> 0 ≡ λs. λz. z
 1 ≡ λs. λz. s z
 2 ≡ λs. λz. s (s z)</code></pre>
<p>And so on. In terms of types,</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb12-1" data-line-number="1">    <span class="kw">type</span> <span class="dt">Nat</span> <span class="fu">=</span> forall c<span class="fu">.</span> (c <span class="ot">→</span> c) <span class="ot">→</span> c <span class="ot">→</span> c</a></code></pre></div>
<p>Now intuitively from this type it seems obvious that this only allows us to apply the first argument <code>n</code> types to the second, like a church numeral. Because of this we want to claim that we can compose the first argument with itself <code>n</code> times before applying it to the second or for all <code>c : Nat</code>, there exists an <code>n</code> so that <code>compose n ≡ c</code>.</p>
<p>To prove this we proceed as before and we end up with</p>
<pre><code> compose[τ] s z ~ c[τ&#39;] s&#39; z&#39; [η[c ↦ R]]</code></pre>
<p>Now we define a new relation <code>S</code> where</p>
<ol type="1">
<li><code>a S b</code> if <code>a ≅ z' ≅ b</code></li>
<li><code>a S b</code> if <code>n S n'</code> and <code>a ≅ s' n</code> and <code>b ≅ s' n'</code></li>
</ol>
<p>Now we know that <code>c[τ'] s' z' S c[τ'] s' z'</code> so by inversion on this we can determine that <code>n</code> applications of <code>s'</code> followed by <code>z'</code>.</p>
<p>Set the <code>n</code> for compose to this new <code>n</code>. From here our result follows by induction on <code>n</code>.</p>
<p>This proof means there’s a mapping from <code>c</code> to <code>n</code>. The curious reader is encouraged to show this is an invertible mapping and complete the proof of isomorphism.</p>
<h2 id="a-note-on-free-theorems">A Note on Free Theorems</h2>
<p>Now most people in the Haskell community have heard the term “free theorem”, what they may not realize is that free theorems are a direct result of parametricity.</p>
<p>In fact, if you read Wadler’s original paper sections 5 and onwards establish parametricity. What’s interesting here is that Wadler opts to establish it in a similar way to how Reynolds did. He first defines a mathematical structure called a “type frame”.</p>
<p>This structure lets us map a program in something like System F or Haskell into pure math functions. From there it defines relationships in a similar way to our logical relation and shows it’s reflexive.</p>
<p>I didn’t opt for this route because</p>
<ol type="1">
<li>Denotational semantics scare me a bit</li>
<li>Type frames need more math to make sense</li>
</ol>
<p>It’s still definitely worth <a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.38.9875">reading</a> for the curious though.</p>
<h2 id="wrap-up">Wrap Up</h2>
<p>Now that we’ve defined parametricity and established a few theorems for it, I hope you can start to see the advantage of types to guide our programs. General enough types can give us assurances without every even looking at the code in question.</p>
<p>Aren’t types cool?</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Mon, 22 Dec 2014 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2014-12-22-parametricity.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Treating Programs like Vending Machines</title>
    <link>http://jozefg.bitbucket.org/posts/2014-12-19-bisim.html</link>
    <description><![CDATA[<div class="info">
    Posted on December 19, 2014
    
</div>
<div class="info">
    
    Tags: <a href="/tags/haskell.html">haskell</a>, <a href="/tags/types.html">types</a>
    
</div>

<p>Proving things about programs is quite hard. In order to make it simpler, we often lie a bit. We do this quite a lot in Haskell when we say things like “assuming everything terminates” or “for all sane values”. Most of the time, this is alright. We sometimes need to leave the universe of terminating things, though, whenever we want to prove things about streams or other infinite structures.</p>
<p>In fact, once we step into the wondrous world of the infinite we can’t rely on structural induction anymore. Remember that induction relies on the “well foundedness” of the thing we’re inducting upon, meaning that there can only be finitely many things smaller than any object. However there is an infinite descending chain of things smaller than an infinite structure! For some intuition here, something like <code>foldr</code> (which behaves just like structural induction) may not terminate on an infinite list.</p>
<p>This is quite a serious issue since induction was one of our few solid tools for proof. We can replace it though with a nifty trick called coinduction which gives rise to a useful notion of equality with bisimulation.</p>
<h2 id="vending-machines">Vending Machines</h2>
<p>Before we get to proving programs correct, let’s start with proving something simpler. The equivalence of two simple machines. These machines (A and B) have 3 buttons. Each time we push a button the machine reconfigures itself. A nice real world example of such machines would be vending machines. We push a button for coke and out pops a (very dusty) can of coke and the machine is now slightly different.</p>
<p>Intuitively, we might say that two vending machines are equivalent if and only if our interactions with them can’t distinguish one from the other. That is to say, pushing the same buttons on one gives the same output and leaves both machines in equivalent states.</p>
<p>To formalize this, we first need to formalize our notion of a vending machine. A vending machine is a comprised set of states. These states are connected by arrows labeled with a transition. We’ll refer to the start of a transition as its domain and its target as the codomain. This group of transitions and states is called a labeled transition system (LTS) properly.</p>
<p>To recap how this all relates back to vending machines</p>
<ol type="1">
<li>A state is a particular vending machine at a moment in time</li>
<li>A transition between <code>A</code> and <code>B</code> would mean we could push a button on <code>A</code> and wind up with <code>B</code></li>
<li>The label of such a transition is the delicious sugary drink produced by pushing the button</li>
</ol>
<p>Notice that this view pays no attention to all the mechanics going on behind the scenes of pushing a button, only the end result of the button push. We refer to the irrelevant stuff as the “internal state” of the vending machine.</p>
<p>Let’s consider a relation <code>R</code> with <code>A R B</code> if and only if</p>
<ol type="1">
<li>There exists a function <code>f</code> from transitions from A to transitions from B so that <code>x</code> and <code>f(x)</code> have the same label.</li>
<li>Further, if <code>A R B</code> and <code>A</code> has a transition <code>x</code>, then the codomain of <code>x</code> is related to the codomain of <code>f(x)</code>.</li>
<li>There is a <code>g</code> satisfying 1. and 2., but from transitions from B to transitions from A.</li>
</ol>
<p>This definition sets out to capture the notion that two states are related if we can’t distinguish between them. The fancy term for such a relation is a bisimulation. Now our notion of equivalence is called bisimilarity and denoted <code>~</code>, it is the union of all bisimulations.</p>
<p>Now how could we prove that <code>A ~ B</code>? Since <code>~</code> is the union of all bisimulations, all we need to is construct a bisimulation so that <code>A R B</code> and hey presto, they’re bisimilar.</p>
<p>To circle back to vending machine terms, if for every button on machine <code>A</code> there’s a button on <code>B</code> that produces the same drink and leaves us with related machines then <code>A</code> and <code>B</code> are the same.</p>
<h2 id="from-vending-machines-to-programs">From Vending Machines to Programs</h2>
<p>It’s all very well and good that we can talk about the equality of labeled transition systems, but we really want to talk about programs and pieces of data. How can we map our ideas about LTSs into programs?</p>
<p>Let’s start with everyone’s favorite example, finite and infinite lists. We define our domain of states to be</p>
<pre><code>L(A) = {nil} ∪ {cons(n, xs) | n ∈ ℕ ∧ xs ∈ A}</code></pre>
<p>We have to define this as a function over <code>A</code> which represents the tail of the list which means this definition isn’t recursive! It’s equivalent to</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb2-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">ListF</span> a <span class="fu">=</span> <span class="dt">Cons</span> <span class="dt">Int</span> a <span class="fu">|</span> <span class="dt">Nil</span></a></code></pre></div>
<p>What we want here is a <em>fixed point</em> of <code>L</code>, an element <code>X</code> so that <code>L(X) = X</code>. This is important because it means</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb3-1" data-line-number="1"><span class="ot">    cons ::</span> ℕ <span class="ot">→</span> <span class="dt">X</span> <span class="ot">→</span> <span class="dt">L</span>(<span class="dt">X</span>)</a>
<a class="sourceLine" id="cb3-2" data-line-number="2"><span class="ot">    cons ::</span> ℕ <span class="ot">→</span> <span class="dt">L</span>(<span class="dt">X</span>) <span class="ot">→</span> <span class="dt">L</span>(<span class="dt">X</span>)</a></code></pre></div>
<p>Which is just the type we’d expect cons to have. There’s still a snag here, what fixed point do we want? How do we know one even exists? I’d prefer to not delve into the math behind this (see TAPL’s chapter on infinite types) but the gist of it is, if for any function <code>F</code></p>
<ol type="1">
<li><code>F</code> is monotone so that <code>x ⊆ y ⇒ F(x) ⊆ F(y)</code></li>
<li><code>F</code> is cocontinuous so that <code>∩ₓF(x) = F(∩ₓ x)</code></li>
</ol>
<p>Then there exists an <code>X = F(X)</code> which is greater or equal to all other fixpoints. The proof of this isn’t too hard, I encourage the curious reader to <a href="https://en.wikipedia.org/wiki/Knaster%E2%80%93Tarski_theorem">go and have a look</a>. Furthermore, poking around why we need cocontinuity is enlightening, it captures the notion of “nice” lazy functions. If you’ve looked at any domain theory, it’s similar to why we need continuity for least fixed pointed (inductive) functions.</p>
<p>This greatest fixed point what we get with Haskell’s recursive types and that’s what we want to model. What’s particularly interesting is that the greatest fixed point includes infinite data which is very different than the least fixed point which is what we usually prefer to think about when dealing with things like F-algebras and proofs by induction.</p>
<p>Now anyways, to show <code>L</code> has a fixed point we have to show it’s monotone. If <code>X ⊆ Y</code> then <code>L(X) ⊆ L(Y)</code> because <code>x ∈ L(X)</code> means <code>x = nil ∈ L(Y)</code> or <code>x = cons(h, t)</code>, but since <code>t ∈ X ⊆ Y</code> then <code>cons(h, t) ∈ L(Y)</code>. Cocontinuity is left as an exercise to the reader.</p>
<p>So <code>L</code> has a greatest fixed point: <code>X</code>. Let’s define an LTS with states being <code>L(X)</code> and with the transitions <code>cons(a, x) → x</code> labeled by <code>a</code>. What does bisimilarity mean in this context? Well <code>nil ~ nil</code> since neither have any transitions. <code>cons(h, t) ~ cons(h', t')</code> if and only if <code>h = h'</code> and <code>t ~ t'</code>. That sounds a lot like how equality works!</p>
<p>Demonstrate this let’s define two lists</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" data-line-number="1">    foo <span class="fu">=</span> cons(<span class="dv">1</span>, foo)</a>
<a class="sourceLine" id="cb4-2" data-line-number="2">    bar <span class="fu">=</span> cons(<span class="dv">1</span>, cons(<span class="dv">1</span>, bar))</a></code></pre></div>
<p>Let’s prove that <code>foo ~ bar</code>. Start by defining a relation <code>R</code> with <code>foo R bar</code>. Now we must show that each transition from <code>foo</code> can be matched with one from <code>bar</code>, since there’s only one from each this is easy. There’s a transition from <code>foo → foo</code> labeled by <code>1</code> and a transition from <code>bar → cons(1, bar)</code> also labeled by one. Here lies some trouble though, since we don’t know that <code>foo R cons(1, bar)</code>, only that <code>foo R bar</code>. We can easily extend <code>R</code> with <code>foo R cons(1, bar)</code> though and now things are smooth sailing. The mapping of transitions for this new pair is identical to what we had before and since we know that <code>foo R bar</code>, our proof is finished.</p>
<p>To see the portion of the LTS our proof was about</p>
<pre><code> foo                  bar
  | 1                  | 1
 foo             cons(1, bar)
  | 1                  | 1
 foo                  bar</code></pre>
<p>and our bisimulation <code>R</code> is just given by <code>{(foo, bar), (foo, cons(1, bar))}</code>.</p>
<p>Now that we’ve seen that we can map our programs into LTSs and apply our usual tricks there, let’s formalize this a bit.</p>
<h2 id="a-more-precise-formulation-of-coinduction">A More Precise Formulation of Coinduction</h2>
<p>First, what exactly is [co]induction? Coinduction is a proof principle for proving something about elements of the greatest fixed point of a function, <code>F</code>. We can prove that the greatest fixed point, <code>X</code>, is the union of all the sets <code>Y</code> so that <code>Y ⊆ F(Y)</code>.</p>
<p>If we can prove that there exists an <code>Y ⊆ F(Y)</code> that captures our desired proposition then we know that <code>Y ⊆ gfp(F)</code>. That is the principle of coinduction. Unlike the principle of induction we don’t get proofs about <em>all</em> members of a set, rather we get proves that there exists members which satisfy this property. It also should look very similar to how we proved things about <code>~</code>.</p>
<p>So now that we’ve defined coinduction across a function, what functions do we want to actually plop into this? We already now what we want for lists,</p>
<pre><code>List(A) = {nil} ∪ {cons(h, t) | h ∈ X ∧ t ∈ A}</code></pre>
<p>But what about everything else? Well, we do know that each value is introduced by a rule. These rules are always of the form</p>
<pre><code>Some Premises Here
——————————————————
 conclusion here</code></pre>
<p>So for example, for lists we have</p>
<pre><code>——————————————
 nil ∈ List(A)

    h ∈ H   t ∈ A
————————————–————————
 cons(h, t) ∈ List(A)</code></pre>
<p>Now our rules can be converted into a function with a form like</p>
<pre><code> F(A) = ∪ᵣ {conclusion | premises}</code></pre>
<p>So for lists this gives</p>
<pre><code>F(A) = {nil} ∪ {cons(h, t) | h ∈ H ∧ t ∈ A}</code></pre>
<p>as expected. We can imagine generalizing this to other things, like trees for example</p>
<pre><code>————————–—————
leaf ∈ Tree(A)

x ∈ H    l ∈ A    r ∈ A
————————–—————————————–
 node(h, l, r) ∈ Tree(A)

Tree(A) = {leaf} ∪ {node(h, l, r) | x ∈ H ∧ l ∈ A ∧ r ∈ A}</code></pre>
<p>Now the most common thing we want to prove is some notion of equability. This is harder then it seems because the usual notions of equality don’t work.</p>
<p>Instead we can apply bisimulation. Our approach is the same, we define a criteria for what it means to be a bisimulation across a certain type and define <code>~</code> as the union of all bisimulations. On lists we wanted the heads to be equal and the tails to be bisimilar, but what about on trees? We can take the same systematic approach we did before by considering what an LTS on trees would look like. <code>leaf</code> has no information contained in it and therefore no transitions. <code>node(a, l, r)</code> should have two transitions, left or right. Both of these give you a subtree contained by this node. What should they be labeled with? We can follow our intuitions from lists and label them both with <code>a</code>.</p>
<p>This leaves us with the following definition, <code>R</code> is a bisimulation on trees if and only if</p>
<ul>
<li><code>leaf R leaf</code></li>
<li><code>node(a, l, r) R node(a', l', r')</code> if and only if <code>a = a'</code> ∧ <code>l R l'</code> and <code>r R r'</code></li>
</ul>
<p>So to prove an equality between trees, all we must do is provide such an <code>R</code> and then we know <code>R ⊆ ~</code>!</p>
<p>This describes how we deal with coinductive things in general really. We define what it means for a relation to partially capture what we’re talking about (like a bisimulation) and then the full thing is the union of all of these different views! Sortedness could be expressed as a unitary relation <code>S</code> where</p>
<ul>
<li><code>nil ∈ S</code></li>
<li><code>cons(a, xs) ∈ S → xs ∈ S ∧ Just a ≤ head xs</code></li>
</ul>
<p>the <code>sorted</code> predicate is the union of all such relations!</p>
<h2 id="dithering-about-duality">Dithering about Duality</h2>
<p>I’d like to reel off some pithy dualities between induction and coinduction</p>
<ul>
<li>Coinduction is about observation, induction is about construction</li>
<li>Coinduction proves existentials, induction proves universals</li>
<li>Coinduction is proving your property is a subset of a group, induction is about proving a group is a subset of your property</li>
</ul>
<h2 id="wrap-up">Wrap Up</h2>
<p>So that about wraps up this post.</p>
<p>We’ve seen how infinite structures demand a fundamentally different approach to proofs then finite ones. It’s not all puppies and rainbows though, considering how we managed to spend nearly 300 lines talking about it, coinduction is a lot less intuitive. It is however, our only choice if we want to have “real proofs” in Haskell (terms and conditions may apply).</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Fri, 19 Dec 2014 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2014-12-19-bisim.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Cooking λΠ 3 ways</title>
    <link>http://jozefg.bitbucket.org/posts/2014-12-17-variables.html</link>
    <description><![CDATA[<div class="info">
    Posted on December 17, 2014
    
</div>
<div class="info">
    
    Tags: <a href="/tags/haskell.html">haskell</a>, <a href="/tags/compilers.html">compilers</a>
    
</div>

<p>After my last post, I didn’t quite feel like ending there. I was a little dissatisfied with how binding was handled in the type checker, the odd blend of HOAS, GUIDs, and DeBruijn variables was… unique.</p>
<p>In the post I explore 3 versions of the same code</p>
<ol type="1">
<li>The original method</li>
<li>Using <code>bound</code> to handle all binding</li>
<li>Full HOAS</li>
</ol>
<p>There’s a lot of code in this post, enough that I think it’s worth hosting the code on its own. You can find it on <a href="http://github.com/jozefg/cooked-pi">github</a> and <a href="http://bitbucket.org/jozefg/cooked-pi">bitbucket</a>.</p>
<h2 id="the-original">The Original</h2>
<p>I’ve already described most of the original method <a href="/posts/2014-11-22-bidir.md">here</a>. To recap</p>
<ol type="1">
<li>Values were HOAS</li>
<li>Terms were DeBruijn</li>
<li>To bridge the gap, we had “free constants” randomly generated</li>
</ol>
<p>The issue I had with this is we almost got the worst of all 3 worlds! We were constantly bumping a counter to keep up with the free constants we needed to generate. We had to muddy up the types of values with <em>another</em> notion of free constants so we could actually inspect variables under HOAS binders! And finally, we had to do the painful and tedious substitutions on DeBruijn terms.</p>
<p>On the other hand, if you’d never used any of those binding schemes together, you too can go triple or nothing and try to understand that code :)</p>
<p>What I really wanted was to unify how I represented values and terms. I still wanted a clearly correct notion of equality, but in this way I could probably dodge at least two of the above.</p>
<p>The obvious thing to do would be to stick with DeBruijn variables and just instantiate free variables with constants. This is ugly, but it’s moderately less horrible if we use a library to help us with the process.</p>
<h2 id="bound"><code>bound</code></h2>
<p>So my first stab at this approach was with Edward Kmett’s <a href="http://hackage.haskell.org/package/bound">bound</a>. For those who aren’t familiar with this library, it centers around the data type <code>Scope</code>. <code>Scope b f a</code> binds variables of type <code>b</code> in the structure <code>f</code> with free variables of type <code>a</code>. The assumption is that <code>f</code> will be a monad which represents our AST.</p>
<p>Further, <code>f</code> is parameterized over variables, it doesn’t attempt to distinguish between bound and free ones however. This means that <code>&gt;&gt;=</code> corresponds to substitution. Then what <code>Scope</code> does is instantiate these variables to <code>B b a</code> which is precisely equivalent to <code>Either b a</code>.</p>
<p>What this results in is that each free variable is a different type from bound ones. <code>Scope</code> provides various functions for instantiating bound variables and abstracting over free ones. That’s <code>bound</code> in a nutshell.</p>
<p>It’s a bit easier to grok this by example, here’s our calculus ported to use <code>Scope</code></p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb1-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">Expr</span> a <span class="fu">=</span> <span class="dt">Var</span> a</a>
<a class="sourceLine" id="cb1-2" data-line-number="2">                <span class="fu">|</span> <span class="dt">App</span> (<span class="dt">Expr</span> a) (<span class="dt">Expr</span> a)</a>
<a class="sourceLine" id="cb1-3" data-line-number="3">                <span class="fu">|</span> <span class="dt">Annot</span> (<span class="dt">Expr</span> a) (<span class="dt">Expr</span> a)</a>
<a class="sourceLine" id="cb1-4" data-line-number="4">                <span class="fu">|</span> <span class="dt">ETrue</span></a>
<a class="sourceLine" id="cb1-5" data-line-number="5">                <span class="fu">|</span> <span class="dt">EFalse</span></a>
<a class="sourceLine" id="cb1-6" data-line-number="6">                <span class="fu">|</span> <span class="dt">Bool</span></a>
<a class="sourceLine" id="cb1-7" data-line-number="7">                <span class="fu">|</span> <span class="dt">Star</span></a>
<a class="sourceLine" id="cb1-8" data-line-number="8">                <span class="fu">|</span> <span class="dt">Pi</span> (<span class="dt">Expr</span> a) (<span class="dt">Scope</span> () <span class="dt">Expr</span> a)</a>
<a class="sourceLine" id="cb1-9" data-line-number="9">                <span class="fu">|</span> <span class="dt">Lam</span> (<span class="dt">Scope</span> () <span class="dt">Expr</span> a)</a>
<a class="sourceLine" id="cb1-10" data-line-number="10">                <span class="fu">|</span> <span class="dt">C</span> <span class="dt">String</span></a>
<a class="sourceLine" id="cb1-11" data-line-number="11">                <span class="kw">deriving</span>(<span class="dt">Functor</span>, <span class="dt">Eq</span>)</a></code></pre></div>
<p>So the first major difference is that our polarization between inferrable and checkable terms is gone! This wasn’t something I was happy about, but in order to use <code>Scope</code> we need a monad instance and we can’t define two mutually dependent monad instances without a function from <code>CExpr -&gt; IExpr</code>, something that clearly doesn’t exist.</p>
<p>Since each binder can only bind one variable at a time, we represent the newly bound variable as just <code>()</code>. This would be more complicated if we supported patterns or something similar.</p>
<p>Now in addition to just this, we also need a bunch of boilerplate to define some type class instances for <code>Scope</code>’s benefit.</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb2-1" data-line-number="1">    <span class="kw">instance</span> <span class="dt">Eq1</span> <span class="dt">Expr</span> <span class="kw">where</span> (<span class="fu">==#</span>) <span class="fu">=</span> (<span class="fu">==</span>)</a>
<a class="sourceLine" id="cb2-2" data-line-number="2">    <span class="kw">instance</span> <span class="dt">Applicative</span> <span class="dt">Expr</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb2-3" data-line-number="3">      pure <span class="fu">=</span> return</a>
<a class="sourceLine" id="cb2-4" data-line-number="4">      (<span class="fu">&lt;*&gt;</span>) <span class="fu">=</span> ap</a>
<a class="sourceLine" id="cb2-5" data-line-number="5">    <span class="kw">instance</span> <span class="dt">Monad</span> <span class="dt">Expr</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb2-6" data-line-number="6">      return <span class="fu">=</span> <span class="dt">Var</span></a>
<a class="sourceLine" id="cb2-7" data-line-number="7">      <span class="dt">Var</span> a <span class="fu">&gt;&gt;=</span> f <span class="fu">=</span> f a</a>
<a class="sourceLine" id="cb2-8" data-line-number="8">      (<span class="dt">App</span> l r) <span class="fu">&gt;&gt;=</span> f <span class="fu">=</span> <span class="dt">App</span> (l <span class="fu">&gt;&gt;=</span> f) (r <span class="fu">&gt;&gt;=</span> f)</a>
<a class="sourceLine" id="cb2-9" data-line-number="9">      <span class="dt">ETrue</span> <span class="fu">&gt;&gt;=</span> _ <span class="fu">=</span> <span class="dt">ETrue</span></a>
<a class="sourceLine" id="cb2-10" data-line-number="10">      <span class="dt">EFalse</span> <span class="fu">&gt;&gt;=</span> _ <span class="fu">=</span> <span class="dt">EFalse</span></a>
<a class="sourceLine" id="cb2-11" data-line-number="11">      <span class="dt">Bool</span> <span class="fu">&gt;&gt;=</span> _ <span class="fu">=</span> <span class="dt">Bool</span></a>
<a class="sourceLine" id="cb2-12" data-line-number="12">      <span class="dt">Star</span> <span class="fu">&gt;&gt;=</span> _ <span class="fu">=</span> <span class="dt">Star</span></a>
<a class="sourceLine" id="cb2-13" data-line-number="13">      <span class="dt">C</span> s <span class="fu">&gt;&gt;=</span> _ <span class="fu">=</span> <span class="dt">C</span> s</a>
<a class="sourceLine" id="cb2-14" data-line-number="14">      <span class="dt">Annot</span> l r <span class="fu">&gt;&gt;=</span> f <span class="fu">=</span> <span class="dt">Annot</span> (l <span class="fu">&gt;&gt;=</span> f) (r <span class="fu">&gt;&gt;=</span> f)</a>
<a class="sourceLine" id="cb2-15" data-line-number="15">      <span class="dt">Pi</span> l s <span class="fu">&gt;&gt;=</span> f <span class="fu">=</span> <span class="dt">Pi</span> (l <span class="fu">&gt;&gt;=</span> f) (s <span class="fu">&gt;&gt;&gt;=</span> f)</a>
<a class="sourceLine" id="cb2-16" data-line-number="16">      <span class="dt">Lam</span> e <span class="fu">&gt;&gt;=</span> f <span class="fu">=</span> <span class="dt">Lam</span> (e <span class="fu">&gt;&gt;&gt;=</span> f)</a></code></pre></div>
<p>That weird <code>&gt;&gt;&gt;=</code> is just <code>&gt;&gt;=</code> that works through <code>Scope</code>s. It’s a little bit frustrating that we need this somewhat boilerplate-y monad instance, but I think the results might be worth it.</p>
<p>From here we completely forgo an explicit <code>Val</code> type. We’re completely scrapping that whole HOAS and <code>VConst</code> ordeal. Instead we’ll just trust <code>Scope</code>’s clever <code>Eq</code> instance to handle alpha conversion. We do need to implement normalization though</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb3-1" data-line-number="1">    <span class="kw">type</span> <span class="dt">Val</span> <span class="fu">=</span> <span class="dt">Expr</span></a>
<a class="sourceLine" id="cb3-2" data-line-number="2"></a>
<a class="sourceLine" id="cb3-3" data-line-number="3"><span class="ot">    nf ::</span> <span class="dt">Expr</span> a <span class="ot">-&gt;</span> <span class="dt">Val</span> a</a>
<a class="sourceLine" id="cb3-4" data-line-number="4">    nf <span class="fu">=</span> \<span class="kw">case</span></a>
<a class="sourceLine" id="cb3-5" data-line-number="5">      (<span class="dt">Annot</span> e t) <span class="ot">-&gt;</span> nf e <span class="co">-- Important, nf&#39;d data throws away annotations</span></a>
<a class="sourceLine" id="cb3-6" data-line-number="6">      (<span class="dt">Lam</span> e) <span class="ot">-&gt;</span> <span class="dt">Lam</span> (toScope <span class="fu">.</span> nf <span class="fu">.</span> fromScope <span class="fu">$</span> e)</a>
<a class="sourceLine" id="cb3-7" data-line-number="7">      (<span class="dt">Pi</span> l r) <span class="ot">-&gt;</span> <span class="dt">Pi</span> (nf l) (toScope <span class="fu">.</span> nf <span class="fu">.</span> fromScope <span class="fu">$</span> r)</a>
<a class="sourceLine" id="cb3-8" data-line-number="8">      (<span class="dt">App</span> l r) <span class="ot">-&gt;</span></a>
<a class="sourceLine" id="cb3-9" data-line-number="9">        <span class="kw">case</span> l <span class="kw">of</span></a>
<a class="sourceLine" id="cb3-10" data-line-number="10">         <span class="dt">Lam</span> f <span class="ot">-&gt;</span> nf (instantiate1 r f)</a>
<a class="sourceLine" id="cb3-11" data-line-number="11">         l&#39; <span class="ot">-&gt;</span> <span class="dt">App</span> l&#39; (nf r)</a>
<a class="sourceLine" id="cb3-12" data-line-number="12">      e <span class="ot">-&gt;</span> e</a></code></pre></div>
<p>What’s interestingly different is actual work is shifted from within the higher order binders we had before into the case expression in <code>App</code>.</p>
<p>It’s also worth mentioning the few bound specifics here. <code>toScope</code> and <code>fromScope</code> expose the underlying <code>f (V b a)</code> that a <code>Scope</code> is hiding. We’re then can polymorphically recur (eat your heart out sml) over the now unbound variables and continue on our way.</p>
<p>Again, notice that I’ve defined nothing to do with substitution or scoping, this is all being handled by bound.</p>
<p>Now our actual type checker is still essentially identical. We’re still using <code>monad-gen</code> to generate unique variable names, it’s just that now <code>bound</code> handles the messy substitution. The lack of distinction between inferrable, checkable, and normalized terms did trip me up once our twice though.</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">Env</span> <span class="fu">=</span> <span class="dt">Env</span> {<span class="ot"> localVars ::</span> <span class="dt">M.Map</span> <span class="dt">Int</span> (<span class="dt">Val</span> <span class="dt">Int</span>)</a>
<a class="sourceLine" id="cb4-2" data-line-number="2">                   ,<span class="ot"> constants  ::</span> <span class="dt">M.Map</span> <span class="dt">String</span> (<span class="dt">Val</span> <span class="dt">Int</span>) }</a>
<a class="sourceLine" id="cb4-3" data-line-number="3">    <span class="kw">type</span> <span class="dt">TyM</span> <span class="fu">=</span> <span class="dt">ReaderT</span> <span class="dt">Env</span> (<span class="dt">GenT</span> <span class="dt">Int</span> <span class="dt">Maybe</span>)</a>
<a class="sourceLine" id="cb4-4" data-line-number="4"></a>
<a class="sourceLine" id="cb4-5" data-line-number="5"><span class="ot">    unbind ::</span> (<span class="dt">MonadGen</span> a m, <span class="dt">Functor</span> m, <span class="dt">Monad</span> f) <span class="ot">=&gt;</span> <span class="dt">Scope</span> () f a <span class="ot">-&gt;</span> m (a, f a)</a>
<a class="sourceLine" id="cb4-6" data-line-number="6">    unbind scope <span class="fu">=</span> ((,) <span class="fu">&lt;*&gt;</span> flip instantiate1 scope <span class="fu">.</span> return) <span class="fu">&lt;$&gt;</span> gen</a>
<a class="sourceLine" id="cb4-7" data-line-number="7"></a>
<a class="sourceLine" id="cb4-8" data-line-number="8"><span class="ot">    unbindWith ::</span> <span class="dt">Monad</span> f <span class="ot">=&gt;</span> a <span class="ot">-&gt;</span> <span class="dt">Scope</span> () f a <span class="ot">-&gt;</span> f a</a>
<a class="sourceLine" id="cb4-9" data-line-number="9">    unbindWith <span class="fu">=</span> instantiate1 <span class="fu">.</span> return</a>
<a class="sourceLine" id="cb4-10" data-line-number="10"></a>
<a class="sourceLine" id="cb4-11" data-line-number="11"><span class="ot">    inferType ::</span> <span class="dt">Expr</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">TyM</span> (<span class="dt">Val</span> <span class="dt">Int</span>)</a>
<a class="sourceLine" id="cb4-12" data-line-number="12">    inferType (<span class="dt">Var</span> i) <span class="fu">=</span> asks (M.lookup i <span class="fu">.</span> localVars) <span class="fu">&gt;&gt;=</span> maybe mzero return</a>
<a class="sourceLine" id="cb4-13" data-line-number="13">    inferType (<span class="dt">C</span> s) <span class="fu">=</span> asks (M.lookup s <span class="fu">.</span> constants) <span class="fu">&gt;&gt;=</span> maybe mzero return</a>
<a class="sourceLine" id="cb4-14" data-line-number="14">    inferType <span class="dt">ETrue</span> <span class="fu">=</span> return <span class="dt">Bool</span></a>
<a class="sourceLine" id="cb4-15" data-line-number="15">    inferType <span class="dt">EFalse</span> <span class="fu">=</span> return <span class="dt">Bool</span></a>
<a class="sourceLine" id="cb4-16" data-line-number="16">    inferType <span class="dt">Bool</span> <span class="fu">=</span> return <span class="dt">Star</span></a>
<a class="sourceLine" id="cb4-17" data-line-number="17">    inferType <span class="dt">Star</span> <span class="fu">=</span> return <span class="dt">Star</span></a>
<a class="sourceLine" id="cb4-18" data-line-number="18">    inferType (<span class="dt">Lam</span> _) <span class="fu">=</span> mzero <span class="co">-- We can only check lambdas</span></a>
<a class="sourceLine" id="cb4-19" data-line-number="19">    inferType (<span class="dt">Annot</span> e ty) <span class="fu">=</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb4-20" data-line-number="20">      checkType ty <span class="dt">Star</span></a>
<a class="sourceLine" id="cb4-21" data-line-number="21">      <span class="kw">let</span> v <span class="fu">=</span> nf ty</a>
<a class="sourceLine" id="cb4-22" data-line-number="22">      v <span class="fu">&lt;$</span> checkType e v</a>
<a class="sourceLine" id="cb4-23" data-line-number="23">    inferType (<span class="dt">App</span> f a) <span class="fu">=</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb4-24" data-line-number="24">      ty <span class="ot">&lt;-</span> inferType f</a>
<a class="sourceLine" id="cb4-25" data-line-number="25">      <span class="kw">case</span> ty <span class="kw">of</span></a>
<a class="sourceLine" id="cb4-26" data-line-number="26">       <span class="dt">Pi</span> aTy body <span class="ot">-&gt;</span> nf (<span class="dt">App</span> (<span class="dt">Lam</span> body) a) <span class="fu">&lt;$</span> checkType a aTy</a>
<a class="sourceLine" id="cb4-27" data-line-number="27">       _ <span class="ot">-&gt;</span> mzero</a>
<a class="sourceLine" id="cb4-28" data-line-number="28">    inferType (<span class="dt">Pi</span> t s) <span class="fu">=</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb4-29" data-line-number="29">      checkType t <span class="dt">Star</span></a>
<a class="sourceLine" id="cb4-30" data-line-number="30">      (newVar, s&#39;) <span class="ot">&lt;-</span> unbind s</a>
<a class="sourceLine" id="cb4-31" data-line-number="31">      local (\e <span class="ot">-&gt;</span> e{localVars <span class="fu">=</span> M.insert newVar (nf t) <span class="fu">$</span> localVars e}) <span class="fu">$</span></a>
<a class="sourceLine" id="cb4-32" data-line-number="32">        <span class="dt">Star</span> <span class="fu">&lt;$</span> checkType s&#39; <span class="dt">Star</span></a>
<a class="sourceLine" id="cb4-33" data-line-number="33"></a>
<a class="sourceLine" id="cb4-34" data-line-number="34"><span class="ot">    checkType ::</span> <span class="dt">Expr</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">Val</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">TyM</span> ()</a>
<a class="sourceLine" id="cb4-35" data-line-number="35">    checkType (<span class="dt">Lam</span> s) (<span class="dt">Pi</span> t ts) <span class="fu">=</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb4-36" data-line-number="36">      (newVar, s&#39;) <span class="ot">&lt;-</span> unbind s</a>
<a class="sourceLine" id="cb4-37" data-line-number="37">      local (\e <span class="ot">-&gt;</span> e{localVars <span class="fu">=</span> M.insert newVar (nf t) <span class="fu">$</span> localVars e}) <span class="fu">$</span></a>
<a class="sourceLine" id="cb4-38" data-line-number="38">        checkType s&#39; (nf <span class="fu">$</span> unbindWith newVar ts)</a>
<a class="sourceLine" id="cb4-39" data-line-number="39">    checkType e t <span class="fu">=</span> inferType e <span class="fu">&gt;&gt;=</span> guard <span class="fu">.</span> (<span class="fu">==</span> t)</a></code></pre></div>
<p>I defined two helper functions <code>unbind</code> and <code>unbindWith</code> which both ease the process of opening a scope and introducing a new free variable. I actually split these off into a <a href="bound-gen">tiny library</a>, but I haven’t uploaded it to hackage yet.</p>
<ol type="1">
<li>Code size decreased by ~50 lines</li>
<li>No more explicit substitution</li>
<li>All the annoying plumbing is in the monad instance which is pretty mechanical</li>
<li>We did lose the really nice separation of terms we had before though :(</li>
</ol>
<p>I suppose that 4. would be a nonissue for a lot of people who don’t care about bidirectional type checkers.</p>
<h2 id="hoas">HOAS</h2>
<p>Higher order abstract syntax is a really nifty trick. The idea is that Haskell already has a perfectly good notion of variables and substitution lying around! Let’s just use that. We represent our functions with actual <code>-&gt;</code>s and we don’t have a constructor for variables anymore.</p>
<p>The only issue is that Haskell doesn’t let us inspect the bodies of functions. We need to do this, however, for a type checker! To deal with this we dirty our AST a bit and add in <code>IGen</code>’s, placeholders for where normal Haskell variables would normally go. Our new AST looks like this</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb5-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">Expr</span> <span class="fu">=</span> <span class="dt">App</span> <span class="dt">Expr</span> <span class="dt">Expr</span></a>
<a class="sourceLine" id="cb5-2" data-line-number="2">              <span class="fu">|</span> <span class="dt">Annot</span> <span class="dt">Expr</span> <span class="dt">Expr</span></a>
<a class="sourceLine" id="cb5-3" data-line-number="3">              <span class="fu">|</span> <span class="dt">ETrue</span></a>
<a class="sourceLine" id="cb5-4" data-line-number="4">              <span class="fu">|</span> <span class="dt">EFalse</span></a>
<a class="sourceLine" id="cb5-5" data-line-number="5">              <span class="fu">|</span> <span class="dt">Bool</span></a>
<a class="sourceLine" id="cb5-6" data-line-number="6">              <span class="fu">|</span> <span class="dt">Star</span></a>
<a class="sourceLine" id="cb5-7" data-line-number="7">              <span class="fu">|</span> <span class="dt">Pi</span> <span class="dt">Expr</span> (<span class="dt">Expr</span> <span class="ot">-&gt;</span> <span class="dt">Expr</span>)</a>
<a class="sourceLine" id="cb5-8" data-line-number="8">              <span class="fu">|</span> <span class="dt">Lam</span> (<span class="dt">Expr</span> <span class="ot">-&gt;</span> <span class="dt">Expr</span>)</a>
<a class="sourceLine" id="cb5-9" data-line-number="9">              <span class="fu">|</span> <span class="dt">C</span> <span class="dt">String</span></a>
<a class="sourceLine" id="cb5-10" data-line-number="10">              <span class="fu">|</span> <span class="dt">IGen</span> <span class="dt">Int</span></a>
<a class="sourceLine" id="cb5-11" data-line-number="11"></a>
<a class="sourceLine" id="cb5-12" data-line-number="12">    <span class="kw">type</span> <span class="dt">NF</span> <span class="fu">=</span> <span class="dt">Expr</span></a></code></pre></div>
<p>Notice how both <code>Pi</code> and <code>Lam</code> have functions embedded in them. Now normalization is actually quite slick because functions are easy to work with in Haskell</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb6-1" data-line-number="1"><span class="ot">    nf ::</span> <span class="dt">Expr</span> <span class="ot">-&gt;</span> <span class="dt">NF</span></a>
<a class="sourceLine" id="cb6-2" data-line-number="2">    nf <span class="dt">ETrue</span> <span class="fu">=</span> <span class="dt">ETrue</span></a>
<a class="sourceLine" id="cb6-3" data-line-number="3">    nf <span class="dt">EFalse</span> <span class="fu">=</span> <span class="dt">EFalse</span></a>
<a class="sourceLine" id="cb6-4" data-line-number="4">    nf <span class="dt">Bool</span> <span class="fu">=</span> <span class="dt">Bool</span></a>
<a class="sourceLine" id="cb6-5" data-line-number="5">    nf <span class="dt">Star</span> <span class="fu">=</span> <span class="dt">Star</span></a>
<a class="sourceLine" id="cb6-6" data-line-number="6">    nf (<span class="dt">C</span> s) <span class="fu">=</span> <span class="dt">C</span> s</a>
<a class="sourceLine" id="cb6-7" data-line-number="7">    nf (<span class="dt">IGen</span> i) <span class="fu">=</span> <span class="dt">IGen</span> i</a>
<a class="sourceLine" id="cb6-8" data-line-number="8">    nf (<span class="dt">Annot</span> l _) <span class="fu">=</span> nf l</a>
<a class="sourceLine" id="cb6-9" data-line-number="9">    nf (<span class="dt">Pi</span> t f) <span class="fu">=</span> <span class="dt">Pi</span> (nf t) (nf <span class="fu">.</span> f)</a>
<a class="sourceLine" id="cb6-10" data-line-number="10">    nf (<span class="dt">Lam</span> f) <span class="fu">=</span> <span class="dt">Lam</span> (nf <span class="fu">.</span> f)</a>
<a class="sourceLine" id="cb6-11" data-line-number="11">    nf (<span class="dt">App</span> l r) <span class="fu">=</span> <span class="kw">case</span> nf l <span class="kw">of</span></a>
<a class="sourceLine" id="cb6-12" data-line-number="12">      <span class="dt">Lam</span> f <span class="ot">-&gt;</span> nf <span class="fu">.</span> f <span class="fu">$</span> l</a>
<a class="sourceLine" id="cb6-13" data-line-number="13">      l&#39; <span class="ot">-&gt;</span> <span class="dt">App</span> l&#39; (nf r)</a></code></pre></div>
<p>This is actually quite similar to the <code>Val</code> type we started with. That was also used HOAS and we end up with a similarly structured normalization.</p>
<p>For the same reason, the equivalence checking procedure is pretty much the same thing</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb7-1" data-line-number="1"><span class="ot">    eqTerm ::</span> <span class="dt">NF</span> <span class="ot">-&gt;</span> <span class="dt">NF</span> <span class="ot">-&gt;</span> <span class="dt">Bool</span></a>
<a class="sourceLine" id="cb7-2" data-line-number="2">    eqTerm l r <span class="fu">=</span> runGenWith (successor s) (<span class="dt">IGen</span> <span class="dv">0</span>) <span class="fu">$</span> go l r</a>
<a class="sourceLine" id="cb7-3" data-line-number="3">      <span class="kw">where</span> s (<span class="dt">IGen</span> i) <span class="fu">=</span> <span class="dt">IGen</span> (i <span class="fu">+</span> <span class="dv">1</span>)</a>
<a class="sourceLine" id="cb7-4" data-line-number="4">            s _ <span class="fu">=</span> error <span class="st">&quot;Impossible!&quot;</span></a>
<a class="sourceLine" id="cb7-5" data-line-number="5">            go <span class="dt">Star</span> <span class="dt">Star</span> <span class="fu">=</span> return <span class="dt">True</span></a>
<a class="sourceLine" id="cb7-6" data-line-number="6">            go <span class="dt">Bool</span> <span class="dt">Bool</span> <span class="fu">=</span> return <span class="dt">True</span></a>
<a class="sourceLine" id="cb7-7" data-line-number="7">            go <span class="dt">ETrue</span> <span class="dt">ETrue</span> <span class="fu">=</span> return <span class="dt">True</span></a>
<a class="sourceLine" id="cb7-8" data-line-number="8">            go <span class="dt">EFalse</span> <span class="dt">EFalse</span> <span class="fu">=</span> return <span class="dt">True</span></a>
<a class="sourceLine" id="cb7-9" data-line-number="9">            go (<span class="dt">Annot</span> l r) (<span class="dt">Annot</span> l&#39; r&#39;) <span class="fu">=</span> (<span class="fu">&amp;&amp;</span>) <span class="fu">&lt;$&gt;</span> go l l&#39; <span class="fu">&lt;*&gt;</span> go r r&#39;</a>
<a class="sourceLine" id="cb7-10" data-line-number="10">            go (<span class="dt">App</span> l r) (<span class="dt">App</span> l&#39; r&#39;) <span class="fu">=</span> (<span class="fu">&amp;&amp;</span>) <span class="fu">&lt;$&gt;</span> go l l&#39; <span class="fu">&lt;*&gt;</span> go r r&#39;</a>
<a class="sourceLine" id="cb7-11" data-line-number="11">            go (<span class="dt">Pi</span> t f) (<span class="dt">Pi</span> t&#39; g) <span class="fu">=</span></a>
<a class="sourceLine" id="cb7-12" data-line-number="12">              (<span class="fu">&amp;&amp;</span>) <span class="fu">&lt;$&gt;</span> go t t&#39; <span class="fu">&lt;*&gt;</span> (gen <span class="fu">&gt;&gt;=</span> \v <span class="ot">-&gt;</span> go (f v) (g v))</a>
<a class="sourceLine" id="cb7-13" data-line-number="13">            go (<span class="dt">IGen</span> i) (<span class="dt">IGen</span> j) <span class="fu">=</span> return (i <span class="fu">==</span> j)</a>
<a class="sourceLine" id="cb7-14" data-line-number="14">            go _ _ <span class="fu">=</span> return <span class="dt">False</span></a></code></pre></div>
<p>In fact, the only differences are that</p>
<ol type="1">
<li>There are a few more cases, even though they won’t ever be called</li>
<li>We don’t need that horrible top level <code>Enum</code> instance</li>
</ol>
<p>The only reason for two is that the amazing maintainer of <code>monad-gen</code> (hi!) rejiggered some the library to not be so <code>Enum</code> dependent.</p>
<p>Now from here our type checker is basically what we had before. In the interest of saving time, I’ll highlight the interesting bits: the constructors that bind variables.</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb8-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">Env</span> <span class="fu">=</span> <span class="dt">Env</span> {<span class="ot"> localVars ::</span> <span class="dt">M.Map</span> <span class="dt">Int</span> <span class="dt">NF</span></a>
<a class="sourceLine" id="cb8-2" data-line-number="2">                   ,<span class="ot"> constants ::</span> <span class="dt">M.Map</span> <span class="dt">String</span> <span class="dt">NF</span> }</a>
<a class="sourceLine" id="cb8-3" data-line-number="3">    <span class="kw">type</span> <span class="dt">TyM</span> <span class="fu">=</span> <span class="dt">GenT</span> <span class="dt">Int</span> (<span class="dt">ReaderT</span> <span class="dt">Env</span> <span class="dt">Maybe</span>)</a>
<a class="sourceLine" id="cb8-4" data-line-number="4"></a>
<a class="sourceLine" id="cb8-5" data-line-number="5"><span class="ot">    inferType ::</span> <span class="dt">Expr</span> <span class="ot">-&gt;</span> <span class="dt">TyM</span> <span class="dt">NF</span></a>
<a class="sourceLine" id="cb8-6" data-line-number="6">    inferType (<span class="dt">Pi</span> t f) <span class="fu">=</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb8-7" data-line-number="7">      checkType t <span class="dt">Star</span></a>
<a class="sourceLine" id="cb8-8" data-line-number="8">      <span class="kw">let</span> t&#39; <span class="fu">=</span> nf t</a>
<a class="sourceLine" id="cb8-9" data-line-number="9">      i <span class="ot">&lt;-</span> gen</a>
<a class="sourceLine" id="cb8-10" data-line-number="10">      local (\e <span class="ot">-&gt;</span> e{localVars <span class="fu">=</span> M.insert i t&#39; <span class="fu">$</span> localVars e}) <span class="fu">$</span></a>
<a class="sourceLine" id="cb8-11" data-line-number="11">        <span class="dt">Star</span> <span class="fu">&lt;$</span> checkType (f <span class="fu">$</span> <span class="dt">IGen</span> i) <span class="dt">Star</span></a>
<a class="sourceLine" id="cb8-12" data-line-number="12"></a>
<a class="sourceLine" id="cb8-13" data-line-number="13"><span class="ot">    checkType ::</span> <span class="dt">Expr</span> <span class="ot">-&gt;</span> <span class="dt">NF</span> <span class="ot">-&gt;</span> <span class="dt">TyM</span> ()</a>
<a class="sourceLine" id="cb8-14" data-line-number="14">    checkType (<span class="dt">Lam</span> f) (<span class="dt">Pi</span> t g) <span class="fu">=</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb8-15" data-line-number="15">      i <span class="ot">&lt;-</span> gen</a>
<a class="sourceLine" id="cb8-16" data-line-number="16">      <span class="kw">let</span> t&#39; <span class="fu">=</span> nf t</a>
<a class="sourceLine" id="cb8-17" data-line-number="17">          rTy <span class="fu">=</span> nf (g <span class="fu">$</span> <span class="dt">IGen</span> i)</a>
<a class="sourceLine" id="cb8-18" data-line-number="18">      local (\e <span class="ot">-&gt;</span> e{localVars <span class="fu">=</span> M.insert i t&#39; <span class="fu">$</span> localVars e}) <span class="fu">$</span></a>
<a class="sourceLine" id="cb8-19" data-line-number="19">        checkType (f <span class="fu">$</span> <span class="dt">IGen</span> i) rTy</a></code></pre></div>
<p>At this point you may have started to notice the pattern, the only real difference here is that substitution is completely free. Otherwise, I don’t really have much to say about HOAS.</p>
<h2 id="wrap-up">Wrap Up</h2>
<p>In conclusion, I think we can all agree that the original version of this type checker was unpleasant to say the least. It did considerably improve with <code>bound</code> mostly because the normalize-and-compare equivalence checking is really easy since <code>bound</code> handles alpha conversion. On the other hand, actually doing work beneath a binder is a bit of a pain since we have to take care to never unwrap a binder with a previously bound variable. We handled this with a hacky little trick with <code>monad-gen</code>, but a permanent and clean solution still seems hard.</p>
<p>We can avoid this fully by hitching a ride on Haskell’s variables and substitution using HOAS, this is wonderful until it’s not. The issue is that comparing functions for equality is still a pain so we ended up with an equivalence check much like what we had in the original version.</p>
<p>In the future it’d be interesting to try this with <code>unbound</code>, a library in the same domain as <code>bound</code> with a very different approach.</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Wed, 17 Dec 2014 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2014-12-17-variables.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Examining Hackage: concurrent-supply</title>
    <link>http://jozefg.bitbucket.org/posts/2014-11-26-conc-supply.html</link>
    <description><![CDATA[<div class="info">
    Posted on November 26, 2014
    
</div>
<div class="info">
    
    Tags: <a href="/tags/haskell.html">haskell</a>
    
</div>

<p>It’s been a while since I posted about some code I’ve been reading, but today I found a little gem: <a href="http://hackage.haskell.org/package/concurrent-supply">concurrent-supply</a>. This package sets out to provide fast way to generate unique identifiers in a way that’s splittable and supports concurrency.</p>
<p>What’s particularly cool about this package is that the code is only about ~100 lines and a goodly chunk of that is pragramas to tell GHC to actually inline trivial functions.</p>
<p>The API is just 5 functions</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb1-1" data-line-number="1">    <span class="kw">type</span> <span class="dt">Supply</span></a>
<a class="sourceLine" id="cb1-2" data-line-number="2"><span class="ot">    newSupply ::</span> <span class="dt">IO</span> <span class="dt">Supply</span></a>
<a class="sourceLine" id="cb1-3" data-line-number="3"><span class="ot">    freshId ::</span> <span class="dt">Supply</span> <span class="ot">-&gt;</span> (<span class="dt">Int</span>, <span class="dt">Supply</span>)</a>
<a class="sourceLine" id="cb1-4" data-line-number="4"><span class="ot">    splitSupply ::</span> <span class="dt">Supply</span> <span class="ot">-&gt;</span> (<span class="dt">Supply</span>, <span class="dt">Supply</span>)</a>
<a class="sourceLine" id="cb1-5" data-line-number="5"></a>
<a class="sourceLine" id="cb1-6" data-line-number="6">    freshId<span class="fu">#</span><span class="ot"> ::</span> <span class="dt">Supply</span> <span class="ot">-&gt;</span> (<span class="fu">#</span> <span class="dt">Int</span>, <span class="dt">Supply</span> <span class="fu">#</span>)</a>
<a class="sourceLine" id="cb1-7" data-line-number="7">    splitSupply<span class="fu">#</span><span class="ot"> ::</span> <span class="dt">Supply</span> <span class="ot">-&gt;</span> (<span class="fu">#</span> <span class="dt">Int</span>, <span class="dt">Supply</span> <span class="fu">#</span>)</a></code></pre></div>
<p><code>Supply</code> is the type for well.. supplies of fresh integers. We can grab an <code>Int</code> out of a supply producing a new supply as well. We can also split a supply so that we have two new supplies that will produce disjoint identifiers.</p>
<p>The idea here is that we can have supplies that are used from multiple concurrent threads and they won’t ever</p>
<ol type="1">
<li>Duplicate identifiers between supply</li>
<li>Hammer on the same supply and destroy all our concurrency</li>
</ol>
<p>It does go without saying that eventually we run out of ints, so I suppose if you sit and prod a supply for a very long time, <em>something</em> bad will happen.</p>
<p>With that in mind, let’s take a look at the imports for <code>Control.Concurrent.Supply</code>.</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb2-1" data-line-number="1">    <span class="kw">import</span> <span class="dt">Data.Hashable</span></a>
<a class="sourceLine" id="cb2-2" data-line-number="2">    <span class="kw">import</span> <span class="dt">Data.IORef</span></a>
<a class="sourceLine" id="cb2-3" data-line-number="3">    <span class="kw">import</span> <span class="dt">Data.Functor</span> ((&lt;$&gt;))</a>
<a class="sourceLine" id="cb2-4" data-line-number="4">    <span class="kw">import</span> <span class="dt">Data.Monoid</span></a>
<a class="sourceLine" id="cb2-5" data-line-number="5">    <span class="kw">import</span> <span class="dt">GHC.IO</span> (unsafeDupablePerformIO, unsafePerformIO)</a>
<a class="sourceLine" id="cb2-6" data-line-number="6">    <span class="kw">import</span> <span class="dt">GHC.Types</span> (<span class="dt">Int</span>(..))</a>
<a class="sourceLine" id="cb2-7" data-line-number="7">    <span class="kw">import</span> <span class="dt">GHC.Prim</span> (<span class="dt">Int</span>#)</a></code></pre></div>
<p>So you can see that some interesting stuff is going to happen, we have both unboxed ints, and <code>unsafe*PerformIO</code>s. As a quick review, <code>unsafeDupablePerformIO</code> is for <code>IO</code> actions which are okay being forced at the same time by different threads which <code>unsafePerformIO</code> is a little bit more modest and ensures we only force things from one thread at a time.</p>
<p>With this in mind, the code starts with the classic definition of streams in Haskell.</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb3-1" data-line-number="1">    <span class="kw">infixr</span> <span class="dv">5</span> <span class="fu">:-</span></a>
<a class="sourceLine" id="cb3-2" data-line-number="2">    <span class="kw">data</span> <span class="dt">Stream</span> a <span class="fu">=</span> a <span class="fu">:-</span> <span class="dt">Stream</span> a</a></code></pre></div>
<p>This is followed with some rather a few definitions,</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" data-line-number="1">    <span class="kw">instance</span> <span class="dt">Functor</span> <span class="dt">Stream</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb4-2" data-line-number="2">      fmap f (a <span class="fu">:-</span> as) <span class="fu">=</span> f a <span class="fu">:-</span> fmap f as</a>
<a class="sourceLine" id="cb4-3" data-line-number="3"></a>
<a class="sourceLine" id="cb4-4" data-line-number="4"><span class="ot">    extract ::</span> <span class="dt">Stream</span> a <span class="ot">-&gt;</span> a</a>
<a class="sourceLine" id="cb4-5" data-line-number="5">    extract (a <span class="fu">:-</span> _) <span class="fu">=</span> a</a>
<a class="sourceLine" id="cb4-6" data-line-number="6"></a>
<a class="sourceLine" id="cb4-7" data-line-number="7"><span class="ot">    units ::</span> <span class="dt">Stream</span> ()</a>
<a class="sourceLine" id="cb4-8" data-line-number="8">    units <span class="fu">=</span> () <span class="fu">:-</span> units</a>
<a class="sourceLine" id="cb4-9" data-line-number="9">    <span class="ot">{-# NOINLINE units #-}</span></a></code></pre></div>
<p>Do note that <code>units</code> won’t be inlined, this is unfortunately important when we’re thinking about with unsafe functions.</p>
<p>Now on top of streams we can define a rather important type, blocks.</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb5-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">Block</span> <span class="fu">=</span> <span class="dt">Block</span> <span class="dt">Int</span> <span class="fu">!</span>(<span class="dt">Stream</span> <span class="dt">Block</span>)</a>
<a class="sourceLine" id="cb5-2" data-line-number="2"></a>
<a class="sourceLine" id="cb5-3" data-line-number="3">    <span class="kw">instance</span> <span class="dt">Eq</span> <span class="dt">Block</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb5-4" data-line-number="4">      <span class="dt">Block</span> a (<span class="dt">Block</span> b _ <span class="fu">:-</span> _) <span class="fu">==</span> <span class="dt">Block</span> c (<span class="dt">Block</span> d _ <span class="fu">:-</span> _) <span class="fu">=</span> a <span class="fu">==</span> c <span class="fu">&amp;&amp;</span> b <span class="fu">==</span> d</a>
<a class="sourceLine" id="cb5-5" data-line-number="5"></a>
<a class="sourceLine" id="cb5-6" data-line-number="6">    <span class="kw">instance</span> <span class="dt">Ord</span> <span class="dt">Block</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb5-7" data-line-number="7">      <span class="dt">Block</span> a (<span class="dt">Block</span> b _ <span class="fu">:-</span> _) <span class="ot">`compare`</span> <span class="dt">Block</span> c (<span class="dt">Block</span> d _ <span class="fu">:-</span> _) <span class="fu">=</span> compare a c <span class="ot">`mappend`</span> compare b d</a>
<a class="sourceLine" id="cb5-8" data-line-number="8"></a>
<a class="sourceLine" id="cb5-9" data-line-number="9">    <span class="kw">instance</span> <span class="dt">Show</span> <span class="dt">Block</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb5-10" data-line-number="10">      showsPrec d (<span class="dt">Block</span> a (<span class="dt">Block</span> b _ <span class="fu">:-</span> _)) <span class="fu">=</span> showParen (d <span class="fu">&gt;=</span> <span class="dv">10</span>) <span class="fu">$</span></a>
<a class="sourceLine" id="cb5-11" data-line-number="11">        showString <span class="st">&quot;Block &quot;</span> <span class="fu">.</span> showsPrec <span class="dv">10</span> a <span class="fu">.</span> showString <span class="st">&quot; (Block &quot;</span></a>
<a class="sourceLine" id="cb5-12" data-line-number="12">                            <span class="fu">.</span> showsPrec <span class="dv">10</span> b <span class="fu">.</span> showString <span class="st">&quot; ... :- ...)&quot;</span></a>
<a class="sourceLine" id="cb5-13" data-line-number="13"></a>
<a class="sourceLine" id="cb5-14" data-line-number="14">    <span class="kw">instance</span> <span class="dt">Hashable</span> <span class="dt">Block</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb5-15" data-line-number="15">      hashWithSalt s (<span class="dt">Block</span> a (<span class="dt">Block</span> b _ <span class="fu">:-</span> _)) <span class="fu">=</span> s <span class="ot">`hashWithSalt`</span> a <span class="ot">`hashWithSalt`</span> b</a></code></pre></div>
<p>So a block is an integer and an infinite number of other blocks. Notice that block identity is purely determined by the first two ints. This is contingent on the fact that all blocks are made with</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb6-1" data-line-number="1"><span class="ot">    blockSize ::</span> <span class="dt">Int</span></a>
<a class="sourceLine" id="cb6-2" data-line-number="2">    blockSize <span class="fu">=</span> <span class="dv">1024</span></a>
<a class="sourceLine" id="cb6-3" data-line-number="3">    <span class="ot">{-# INLINE blockSize #-}</span></a>
<a class="sourceLine" id="cb6-4" data-line-number="4"></a>
<a class="sourceLine" id="cb6-5" data-line-number="5">    <span class="co">-- Minimum size to be worth splitting a supply rather than</span></a>
<a class="sourceLine" id="cb6-6" data-line-number="6">    <span class="co">-- just CAS&#39;ing twice to avoid multiple subsequent biased splits</span></a>
<a class="sourceLine" id="cb6-7" data-line-number="7"><span class="ot">    blockCounter ::</span> <span class="dt">IORef</span> <span class="dt">Int</span></a>
<a class="sourceLine" id="cb6-8" data-line-number="8">    blockCounter <span class="fu">=</span> unsafePerformIO (newIORef <span class="dv">0</span>)</a>
<a class="sourceLine" id="cb6-9" data-line-number="9">    <span class="ot">{-# NOINLINE blockCounter #-}</span></a>
<a class="sourceLine" id="cb6-10" data-line-number="10"></a>
<a class="sourceLine" id="cb6-11" data-line-number="11"><span class="ot">    modifyBlock ::</span> a <span class="ot">-&gt;</span> <span class="dt">IO</span> <span class="dt">Int</span></a>
<a class="sourceLine" id="cb6-12" data-line-number="12">    modifyBlock _ <span class="fu">=</span></a>
<a class="sourceLine" id="cb6-13" data-line-number="13">      atomicModifyIORef blockCounter <span class="fu">$</span> \ i <span class="ot">-&gt;</span></a>
<a class="sourceLine" id="cb6-14" data-line-number="14">        <span class="kw">let</span> i&#39; <span class="fu">=</span> i <span class="fu">+</span> blockSize <span class="kw">in</span> i&#39; <span class="ot">`seq`</span> (i&#39;, i)</a>
<a class="sourceLine" id="cb6-15" data-line-number="15">    <span class="ot">{-# NOINLINE modifyBlock #-}</span></a>
<a class="sourceLine" id="cb6-16" data-line-number="16"></a>
<a class="sourceLine" id="cb6-17" data-line-number="17"><span class="ot">    gen ::</span> a <span class="ot">-&gt;</span> <span class="dt">Block</span></a>
<a class="sourceLine" id="cb6-18" data-line-number="18">    gen x <span class="fu">=</span> <span class="dt">Block</span> (unsafeDupablePerformIO (modifyBlock x)) (gen <span class="fu">&lt;$&gt;</span> units)</a>
<a class="sourceLine" id="cb6-19" data-line-number="19">    <span class="ot">{-# NOINLINE gen #-}</span></a>
<a class="sourceLine" id="cb6-20" data-line-number="20"></a>
<a class="sourceLine" id="cb6-21" data-line-number="21"><span class="ot">    newBlock ::</span> <span class="dt">IO</span> <span class="dt">Block</span></a>
<a class="sourceLine" id="cb6-22" data-line-number="22">    newBlock <span class="fu">=</span> return <span class="fu">$!</span> gen ()</a>
<a class="sourceLine" id="cb6-23" data-line-number="23">    <span class="ot">{-# NOINLINE newBlock #-}</span></a></code></pre></div>
<p>This is the first bit of unsafe code, so let’s look at what’s going on. We have a normal constant <code>blockSize</code> which represents something, it’s not immediately clear what yet. There’s a global mutable variable <code>blockCounter</code> starting from zero. From there, we have <code>gen</code> which creates a block by making a thunk which unsafely bumps the block counter by 1024, returning its previous size. To get the stream of blocks we <code>fmap</code> <code>units</code>.</p>
<p>It’s worth wondering why we need this polymorphic argument. I’m reasonable certain it’s to prevent GHC from being clever and sharing that <code>(unsafeDupablePerformIO ...)</code> between blocks. That would be very bad. It might not do that if we where to use <code>()</code> instead of <code>a</code> but there’s no reason a future optimization (if it doesn’t exist already) wouldn’t figure out that there’s only one possible result type and reduce the whole thing to a CAF.</p>
<p>Now a <code>newBlock</code> wraps all this unsafe updating in <code>IO</code> and returns the application of <code>gen ()</code>.</p>
<p>So what does all of this mean? Well each block thunk is going to have its own unique ID, separated by 1024 and only claimed whenever we actually force its first component. We have this gnarly chunk of mutable shared memory that we only ever modify with <code>atomicModifyIORef</code>, we actually touch it whenever we inspect the first thunk in a <code>Block</code>. What’s particularly interesting is that this can happen in pure code! By putting off this costly operation as long as possible we amortize the cost of all that contention.</p>
<p>Now we also have to support split, luckily it’s easy to split blocks since we have an infinite number of them nested!</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb7-1" data-line-number="1">    splitBlock<span class="fu">#</span><span class="ot"> ::</span> <span class="dt">Block</span> <span class="ot">-&gt;</span> (<span class="fu">#</span> <span class="dt">Block</span>, <span class="dt">Block</span> <span class="fu">#</span>)</a>
<a class="sourceLine" id="cb7-2" data-line-number="2">    splitBlock<span class="fu">#</span> (<span class="dt">Block</span> i (x <span class="fu">:-</span> xs)) <span class="fu">=</span> (<span class="fu">#</span> x, <span class="dt">Block</span> i xs <span class="fu">#</span>)</a></code></pre></div>
<p>It becomes a bit clearer now why we can completely determine blocks by their “first two” elements. The head is completely unique to each sequence so we know at minimum that if <code>i == j</code> in <code>Block i xs</code> and <code>Block j ys</code> then either <code>xs</code> or <code>ys</code> is the tail of the other. This is an invariant we maintain throughout the code not exposing <code>Block</code> and by ensuring we never <code>:-</code> any new ones onto its internal stream. If these streams have the same head (also unique) then they must be the same sequence so the original blocks are equivalent. Nifty.</p>
<p>Now this still isn’t quite enough, we need one final data type: <code>Supply</code></p>
<div class="sourceCode" id="cb8"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb8-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">Supply</span> <span class="fu">=</span> <span class="dt">Supply</span> <span class="ot">{-# UNPACK #-}</span> <span class="fu">!</span><span class="dt">Int</span> <span class="ot">{-# UNPACK #-}</span> <span class="fu">!</span><span class="dt">Int</span> <span class="dt">Block</span></a>
<a class="sourceLine" id="cb8-2" data-line-number="2">        <span class="kw">deriving</span> (<span class="dt">Eq</span>,<span class="dt">Ord</span>,<span class="dt">Show</span>)</a>
<a class="sourceLine" id="cb8-3" data-line-number="3"></a>
<a class="sourceLine" id="cb8-4" data-line-number="4"><span class="ot">    blockSupply ::</span> <span class="dt">Block</span> <span class="ot">-&gt;</span> <span class="dt">Supply</span></a>
<a class="sourceLine" id="cb8-5" data-line-number="5">    blockSupply (<span class="dt">Block</span> i bs) <span class="fu">=</span> <span class="dt">Supply</span> i (i <span class="fu">+</span> blockSize <span class="fu">-</span> <span class="dv">1</span>) (extract bs)</a>
<a class="sourceLine" id="cb8-6" data-line-number="6">    <span class="ot">{-# INLINE blockSupply #-}</span></a></code></pre></div>
<p>A supply should be seen almost an iterator over a chunk of a number line. We know that each block is 1024 away from each other and a supply is almost an iterator from the blocks starting value over the next 1023 elements. We know that <code>Supply</code>s could intersect because the blocks are spaced this far apart.</p>
<p>Once we run out of those elements though, we need to get more. For this we have another block hidden in the back of the supply. It’s kept lazily so that it won’t fire of its first thunk to go bump our global store. When we run out of things to enumerate we call <code>blockSupply</code>, which will force <code>i</code> which will go bother the global counter for another chunk of 1024 unique values.</p>
<p>With this understanding, <code>splitSupply</code> and <code>freshId</code> are quite easy.</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb9-1" data-line-number="1">    <span class="co">-- | An unboxed version of freshId</span></a>
<a class="sourceLine" id="cb9-2" data-line-number="2">    freshId<span class="fu">#</span><span class="ot"> ::</span> <span class="dt">Supply</span> <span class="ot">-&gt;</span> (<span class="fu">#</span> <span class="dt">Int</span><span class="fu">#</span>, <span class="dt">Supply</span> <span class="fu">#</span>)</a>
<a class="sourceLine" id="cb9-3" data-line-number="3">    freshId<span class="fu">#</span> (<span class="dt">Supply</span> i<span class="fu">@</span>(<span class="dt">I</span><span class="fu">#</span> i<span class="fu">#</span>) j b)</a>
<a class="sourceLine" id="cb9-4" data-line-number="4">      <span class="fu">|</span> i <span class="fu">/=</span> j <span class="fu">=</span> (<span class="fu">#</span> i<span class="fu">#</span>, <span class="dt">Supply</span> (i <span class="fu">+</span> <span class="dv">1</span>) j b <span class="fu">#</span>)</a>
<a class="sourceLine" id="cb9-5" data-line-number="5">      <span class="fu">|</span> otherwise <span class="fu">=</span> (<span class="fu">#</span> i<span class="fu">#</span>, blockSupply b <span class="fu">#</span>)</a>
<a class="sourceLine" id="cb9-6" data-line-number="6">    <span class="ot">{-# INLINE freshId# #-}</span></a>
<a class="sourceLine" id="cb9-7" data-line-number="7"></a>
<a class="sourceLine" id="cb9-8" data-line-number="8">    <span class="co">-- | An unboxed version of splitSupply</span></a>
<a class="sourceLine" id="cb9-9" data-line-number="9">    splitSupply<span class="fu">#</span><span class="ot"> ::</span> <span class="dt">Supply</span> <span class="ot">-&gt;</span> (<span class="fu">#</span> <span class="dt">Supply</span>, <span class="dt">Supply</span> <span class="fu">#</span>)</a>
<a class="sourceLine" id="cb9-10" data-line-number="10">    splitSupply<span class="fu">#</span> (<span class="dt">Supply</span> i k b) <span class="fu">=</span> <span class="kw">case</span> splitBlock<span class="fu">#</span> b <span class="kw">of</span></a>
<a class="sourceLine" id="cb9-11" data-line-number="11">        (<span class="fu">#</span> bl, br <span class="fu">#</span>)</a>
<a class="sourceLine" id="cb9-12" data-line-number="12">          <span class="fu">|</span> k <span class="fu">-</span> i <span class="fu">&gt;=</span> minSplitSupplySize</a>
<a class="sourceLine" id="cb9-13" data-line-number="13">          , j <span class="ot">&lt;-</span> i <span class="fu">+</span> div (k <span class="fu">-</span> i) <span class="dv">2</span> <span class="ot">-&gt;</span></a>
<a class="sourceLine" id="cb9-14" data-line-number="14">            (<span class="fu">#</span> <span class="dt">Supply</span> i j bl, <span class="dt">Supply</span> (j <span class="fu">+</span> <span class="dv">1</span>) k br <span class="fu">#</span>)</a>
<a class="sourceLine" id="cb9-15" data-line-number="15">          <span class="fu">|</span> <span class="dt">Block</span> x (l <span class="fu">:-</span> r <span class="fu">:-</span> _) <span class="ot">&lt;-</span> bl</a>
<a class="sourceLine" id="cb9-16" data-line-number="16">          , y <span class="ot">&lt;-</span> x <span class="fu">+</span> div blockSize <span class="dv">2</span></a>
<a class="sourceLine" id="cb9-17" data-line-number="17">          , z <span class="ot">&lt;-</span> x <span class="fu">+</span> blockSize <span class="fu">-</span> <span class="dv">1</span> <span class="ot">-&gt;</span></a>
<a class="sourceLine" id="cb9-18" data-line-number="18">            (<span class="fu">#</span> <span class="dt">Supply</span> x (y <span class="fu">-</span> <span class="dv">1</span>) l, <span class="dt">Supply</span> y z r <span class="fu">#</span>)</a>
<a class="sourceLine" id="cb9-19" data-line-number="19">    <span class="ot">{-# INLINE splitSupply# #-}</span></a></code></pre></div>
<p><code>freshId#</code> is more or less what we’d expect for an iterator. It returns the lower bound and returns the new supply with the lower bound bumped by one. Notice how cheap this is. In particular, since we haven’t forced <code>b</code> anywhere we’ve just copied a couple of words. The expensive bit is when we actually run out of values in our range, in this case we return our final value and force operation to produce a new supply. This goes off and hammers on <code>blockCounter</code>. Happily we only end up doing this 1/1024th of the time.</p>
<p><code>splitSupply#</code> is a bit more complicated. When we go to split a supply we’re going to partition its range of values into two separate ranges. However, we want to watch out for splitting extremely small ranges. In this case, it’s slightly more efficient to just bite the bullet and incur the cost of hitting the <code>blockCounter</code>.</p>
<p>The way we determine this is to split the block <code>b</code>, giving us two new blocks. If we have more in the current set of ids then <code>minimumSplitSize</code> all we give the two blocks to two new supplies, each with one half of the original range.</p>
<p>If the block size is indeed two small, we poke the first block in the pair. This causes it to go hammer <code>blockCounter</code> and from there we divide the range we got back into two and return these smaller supplies over the new range. Notice that we’ve completely tossed the remaining elements in the supply on the floor since there weren’t that many. More interestingly, we completely ignored the second result of our split! The idea is that the most expensive operation we can do here is force that first thunk in a block. However, is long as we don’t force their first components blocks are dirt cheap! Hence it’s cheaper to accept that we only get half of <code>blockSize</code> on each <code>Supply</code> but we only had to perform one CAS to get them.</p>
<p>So now that we’ve done all of that, all that’s left in the module is the paper-thin wrappers over these functions so we don’t always have to use unboxed tuples</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb10-1" data-line-number="1">    <span class="co">-- | Obtain a fresh Id from a Supply.</span></a>
<a class="sourceLine" id="cb10-2" data-line-number="2"><span class="ot">    freshId ::</span> <span class="dt">Supply</span> <span class="ot">-&gt;</span> (<span class="dt">Int</span>, <span class="dt">Supply</span>)</a>
<a class="sourceLine" id="cb10-3" data-line-number="3">    freshId s <span class="fu">=</span> <span class="kw">case</span> freshId<span class="fu">#</span> s <span class="kw">of</span></a>
<a class="sourceLine" id="cb10-4" data-line-number="4">      (<span class="fu">#</span> i, s&#39; <span class="fu">#</span>) <span class="ot">-&gt;</span> (<span class="dt">I</span><span class="fu">#</span> i, s&#39;)</a>
<a class="sourceLine" id="cb10-5" data-line-number="5">    <span class="ot">{-# INLINE freshId #-}</span></a>
<a class="sourceLine" id="cb10-6" data-line-number="6"></a>
<a class="sourceLine" id="cb10-7" data-line-number="7">    <span class="co">-- | Split a supply into two supplies that will return disjoint identifiers</span></a>
<a class="sourceLine" id="cb10-8" data-line-number="8"><span class="ot">    splitSupply ::</span> <span class="dt">Supply</span> <span class="ot">-&gt;</span> (<span class="dt">Supply</span>, <span class="dt">Supply</span>)</a>
<a class="sourceLine" id="cb10-9" data-line-number="9">    splitSupply s <span class="fu">=</span> <span class="kw">case</span> splitSupply<span class="fu">#</span> s <span class="kw">of</span></a>
<a class="sourceLine" id="cb10-10" data-line-number="10">      (<span class="fu">#</span> l, r <span class="fu">#</span>) <span class="ot">-&gt;</span> (l, r)</a>
<a class="sourceLine" id="cb10-11" data-line-number="11">    <span class="ot">{-# INLINE splitSupply #-}</span></a></code></pre></div>
<p>And that’s all. I’ll hope this illustrated a fairly unique mix of laziness in side effects to help reduce contention for a difficult concurrent problem.</p>
<p>Cheers</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Wed, 26 Nov 2014 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2014-11-26-conc-supply.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Bidirectional Type Checkers for λ→ and λΠ</title>
    <link>http://jozefg.bitbucket.org/posts/2014-11-22-bidir.html</link>
    <description><![CDATA[<div class="info">
    Posted on November 22, 2014
    
</div>
<div class="info">
    
    Tags: <a href="/tags/haskell.html">haskell</a>, <a href="/tags/types.html">types</a>, <a href="/tags/compilers.html">compilers</a>
    
</div>

<p>This week I learned that my clever trick for writing a type checker actually has a proper name: bidirectional type checking. In this post I’ll explain what exactly that is and we’ll use it to write a few fun type checkers.</p>
<p>First of all, let’s talk about one of the fundamental conflicts when designing a statically typed language: how much information need we demand from the user? Clearly we can go too far in either direction. Even people who are supposedly against type inference support at least <em>some</em> inference. I’m not aware of a language that requires you to write something like</p>
<pre><code>my_function((my_var : int) + (1 : int) : int) : string</code></pre>
<p>Clearly inferring the types of some expressions are necessary. On the other hand, if we leave out <em>all</em> type annotations then it becomes a lot harder for a human reader to figure out what’s going on! I at least, need to see signatures for top level functions or I become grumpy.</p>
<p>So inside a type checker we always have two sort of processes</p>
<ol type="1">
<li>I know this must have the type T, I’ll check to make sure this is the case</li>
<li>I have no idea what the type of this expression is, I’ll examine the expression to figure it out</li>
</ol>
<p>In a bidirectional type checker, we acknowledge these two phases by explicitly separating the type checker into two functions</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb2-1" data-line-number="1"><span class="ot">    inferType ::</span> <span class="dt">Expr</span> <span class="ot">-&gt;</span> <span class="dt">Maybe</span> <span class="dt">Type</span></a>
<a class="sourceLine" id="cb2-2" data-line-number="2"><span class="ot">    checkType ::</span> <span class="dt">Type</span> <span class="ot">-&gt;</span> <span class="dt">Expr</span> <span class="ot">-&gt;</span> <span class="dt">Maybe</span> ()</a></code></pre></div>
<p>Our type checker thus has two directions, one where we use the type to validate the expression (the type flows in) or we synthesize the type form the expression (the type flows out). That’s all that this is!</p>
<p>It turns out that a technique like this is surprisingly robust. It handles everything from subtyping to simple dependent types! To see how this actually plays out I think it’d be best to just dive in and do something with it.</p>
<h2 id="laying-out-our-language">Laying Out Our Language</h2>
<p>Now when we’re building a bidirectional type checker we really want our AST to explicitly indicate inferrable vs checkable types. Clearly the parser might not care so much about this distinction, but prior to type checking it’s helpful to create this polarized tree.</p>
<p>For a simple language you can imagine</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb3-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">Ty</span> <span class="fu">=</span> <span class="dt">Bool</span></a>
<a class="sourceLine" id="cb3-2" data-line-number="2">            <span class="fu">|</span> <span class="dt">Arr</span> <span class="dt">Ty</span> <span class="dt">Ty</span></a>
<a class="sourceLine" id="cb3-3" data-line-number="3">            <span class="kw">deriving</span>(<span class="dt">Eq</span>, <span class="dt">Show</span>)</a>
<a class="sourceLine" id="cb3-4" data-line-number="4"></a>
<a class="sourceLine" id="cb3-5" data-line-number="5">    <span class="kw">data</span> <span class="dt">IExpr</span> <span class="fu">=</span> <span class="dt">Var</span> <span class="dt">Int</span></a>
<a class="sourceLine" id="cb3-6" data-line-number="6">               <span class="fu">|</span> <span class="dt">App</span> <span class="dt">IExpr</span> <span class="dt">CExpr</span></a>
<a class="sourceLine" id="cb3-7" data-line-number="7">               <span class="fu">|</span> <span class="dt">Annot</span> <span class="dt">CExpr</span> <span class="dt">Ty</span></a>
<a class="sourceLine" id="cb3-8" data-line-number="8">               <span class="fu">|</span> <span class="dt">If</span> <span class="dt">CExpr</span> <span class="dt">IExpr</span> <span class="dt">IExpr</span></a>
<a class="sourceLine" id="cb3-9" data-line-number="9">               <span class="fu">|</span> <span class="dt">ETrue</span></a>
<a class="sourceLine" id="cb3-10" data-line-number="10">               <span class="fu">|</span> <span class="dt">EFalse</span></a>
<a class="sourceLine" id="cb3-11" data-line-number="11"></a>
<a class="sourceLine" id="cb3-12" data-line-number="12">    <span class="kw">data</span> <span class="dt">CExpr</span> <span class="fu">=</span> <span class="dt">Lam</span> <span class="dt">CExpr</span></a>
<a class="sourceLine" id="cb3-13" data-line-number="13">               <span class="fu">|</span> <span class="dt">CI</span> <span class="dt">IExpr</span></a></code></pre></div>
<p>This is just simply typed lambda calculus with booleans. We’re using DeBruijn indices so we need not specify a variable for <code>Lam</code>. The <code>IExpr</code> type is for expressions we can <em>infer</em> types for, while <code>CExpr</code> is for types we can <em>check</em>.</p>
<p>Much this isn’t checking, we can always infer the types of variables, inferring the types of lambdas is hard, etc. Something worth noting is <code>CI</code>. For any inferrable type, we can make it checkable by inferring a type and checking that it’s equal to what we expected. This is actually how Haskell works, GHC is just inferring type without bothering with your signature and then just checks you were right in the first place!</p>
<p>Now that we’ve separated out our expressions, we can easily define our type checker.</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" data-line-number="1">    <span class="kw">type</span> <span class="dt">Env</span> <span class="fu">=</span> [<span class="dt">Ty</span>]</a>
<a class="sourceLine" id="cb4-2" data-line-number="2"></a>
<a class="sourceLine" id="cb4-3" data-line-number="3"><span class="ot">    (?!) ::</span> [a] <span class="ot">-&gt;</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">Maybe</span> a</a>
<a class="sourceLine" id="cb4-4" data-line-number="4">    xs <span class="fu">?!</span> i <span class="fu">=</span> <span class="kw">if</span> i <span class="fu">&lt;</span> length xs <span class="kw">then</span> <span class="dt">Just</span> (xs <span class="fu">!!</span> i) <span class="kw">else</span> <span class="dt">Nothing</span></a>
<a class="sourceLine" id="cb4-5" data-line-number="5"></a>
<a class="sourceLine" id="cb4-6" data-line-number="6"><span class="ot">    inferType ::</span> <span class="dt">Env</span> <span class="ot">-&gt;</span> <span class="dt">IExpr</span> <span class="ot">-&gt;</span> <span class="dt">Maybe</span> <span class="dt">Ty</span></a>
<a class="sourceLine" id="cb4-7" data-line-number="7">    inferType env (<span class="dt">Var</span> i) <span class="fu">=</span> env <span class="fu">?!</span> i</a>
<a class="sourceLine" id="cb4-8" data-line-number="8">    inferType env (<span class="dt">App</span> l r) <span class="fu">=</span></a>
<a class="sourceLine" id="cb4-9" data-line-number="9">      <span class="kw">case</span> inferType env l <span class="kw">of</span></a>
<a class="sourceLine" id="cb4-10" data-line-number="10">       <span class="dt">Just</span> (<span class="dt">Arr</span> lTy rTy) <span class="ot">-&gt;</span> checkType env r lTy <span class="fu">&gt;&gt;</span> return rTy</a>
<a class="sourceLine" id="cb4-11" data-line-number="11">       _ <span class="ot">-&gt;</span> <span class="dt">Nothing</span></a>
<a class="sourceLine" id="cb4-12" data-line-number="12">    inferType env (<span class="dt">Annot</span> e an) <span class="fu">=</span> checkType env e an <span class="fu">&gt;&gt;</span> return an</a>
<a class="sourceLine" id="cb4-13" data-line-number="13">    inferType _ <span class="dt">ETrue</span> <span class="fu">=</span> return <span class="dt">Bool</span></a>
<a class="sourceLine" id="cb4-14" data-line-number="14">    inferType _ <span class="dt">EFalse</span> <span class="fu">=</span> return <span class="dt">Bool</span></a>
<a class="sourceLine" id="cb4-15" data-line-number="15">    inferType env (<span class="dt">If</span> i t e) <span class="fu">=</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb4-16" data-line-number="16">      checkType env i <span class="dt">Bool</span></a>
<a class="sourceLine" id="cb4-17" data-line-number="17">      lTy <span class="ot">&lt;-</span> inferType env t</a>
<a class="sourceLine" id="cb4-18" data-line-number="18">      rTy <span class="ot">&lt;-</span> inferType env e</a>
<a class="sourceLine" id="cb4-19" data-line-number="19">      guard (lTy <span class="fu">==</span> rTy)</a>
<a class="sourceLine" id="cb4-20" data-line-number="20">      return lTy</a>
<a class="sourceLine" id="cb4-21" data-line-number="21"></a>
<a class="sourceLine" id="cb4-22" data-line-number="22"><span class="ot">    checkType ::</span> <span class="dt">Env</span> <span class="ot">-&gt;</span> <span class="dt">CExpr</span> <span class="ot">-&gt;</span> <span class="dt">Ty</span> <span class="ot">-&gt;</span> <span class="dt">Maybe</span> ()</a>
<a class="sourceLine" id="cb4-23" data-line-number="23">    checkType env (<span class="dt">Lam</span> ce) (<span class="dt">Arr</span> l r) <span class="fu">=</span> checkType (l <span class="fu">:</span> env) ce r</a>
<a class="sourceLine" id="cb4-24" data-line-number="24">    checkType env (<span class="dt">CI</span> e) t <span class="fu">=</span> inferType env e <span class="fu">&gt;&gt;=</span> guard <span class="fu">.</span> (t <span class="fu">==</span>)</a>
<a class="sourceLine" id="cb4-25" data-line-number="25">    checkType _ _ _ <span class="fu">=</span> <span class="dt">Nothing</span></a></code></pre></div>
<p>So our type checker doesn’t have many surprises in it. The environment is easy to maintain since DeBruijn indices are easily stored in a list.</p>
<p>Now that we’ve seen how a bidirectional type checker more or less works, let’s kick it up a notch.</p>
<h2 id="type-checking-dependent-types">Type Checking Dependent Types</h2>
<p>Type checking a simple dependently typed language is actually not nearly as bad as you’d expect. The first thing to realize is that since dependent types have only one syntactic category.</p>
<p>We maintain the distinction between inferrable and checkable values, resulting in</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb5-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">IExpr</span> <span class="fu">=</span> <span class="dt">Var</span> <span class="dt">Int</span></a>
<a class="sourceLine" id="cb5-2" data-line-number="2">               <span class="fu">|</span> <span class="dt">App</span> <span class="dt">IExpr</span> <span class="dt">CExpr</span></a>
<a class="sourceLine" id="cb5-3" data-line-number="3">               <span class="fu">|</span> <span class="dt">Annot</span> <span class="dt">CExpr</span> <span class="dt">CExpr</span></a>
<a class="sourceLine" id="cb5-4" data-line-number="4">               <span class="fu">|</span> <span class="dt">ETrue</span></a>
<a class="sourceLine" id="cb5-5" data-line-number="5">               <span class="fu">|</span> <span class="dt">EFalse</span></a>
<a class="sourceLine" id="cb5-6" data-line-number="6">               <span class="fu">|</span> <span class="dt">Bool</span></a>
<a class="sourceLine" id="cb5-7" data-line-number="7">               <span class="fu">|</span> <span class="dt">Star</span> <span class="co">-- New stuff starts here</span></a>
<a class="sourceLine" id="cb5-8" data-line-number="8">               <span class="fu">|</span> <span class="dt">Pi</span> <span class="dt">CExpr</span> <span class="dt">CExpr</span></a>
<a class="sourceLine" id="cb5-9" data-line-number="9">               <span class="fu">|</span> <span class="dt">Const</span> <span class="dt">String</span></a>
<a class="sourceLine" id="cb5-10" data-line-number="10">               <span class="fu">|</span> <span class="dt">Free</span> <span class="dt">Int</span></a>
<a class="sourceLine" id="cb5-11" data-line-number="11">               <span class="kw">deriving</span> (<span class="dt">Eq</span>, <span class="dt">Show</span>, <span class="dt">Ord</span>)</a>
<a class="sourceLine" id="cb5-12" data-line-number="12"></a>
<a class="sourceLine" id="cb5-13" data-line-number="13">    <span class="kw">data</span> <span class="dt">CExpr</span> <span class="fu">=</span> <span class="dt">Lam</span> <span class="dt">CExpr</span></a>
<a class="sourceLine" id="cb5-14" data-line-number="14">               <span class="fu">|</span> <span class="dt">CI</span> <span class="dt">IExpr</span></a>
<a class="sourceLine" id="cb5-15" data-line-number="15">               <span class="kw">deriving</span> (<span class="dt">Eq</span>, <span class="dt">Show</span>, <span class="dt">Ord</span>)</a></code></pre></div>
<p>So you can see we’ve added 4 new expressions, all inferrable. <code>Star</code> is just the kind of types as it is in Haskell. <code>Pi</code> is the dependent function type, it’s like <code>Arr</code>, except the return type can <em>depend</em> on the supplied value.</p>
<p>For example, you can imagine a type like</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb6-1" data-line-number="1"><span class="ot">    replicate ::</span> (n <span class="fu">:</span> <span class="dt">Int</span>) <span class="ot">-&gt;</span> a <span class="ot">-&gt;</span> <span class="dt">List</span> n a</a></code></pre></div>
<p>Which says something like “give me an integer <code>n</code> and a value and I’ll give you back a list of length <code>n</code>”.</p>
<p>Interestingly, we’ve introduce constants. These are necessary simply because without them this language is unbelievable boring. Constants would be defined in the environment and they represent constant, irreducible terms. You should think of them almost like constructors in Haskell. For example, one can imagine that 3 constants</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb7-1" data-line-number="1">    <span class="dt">Nat</span><span class="ot"> ::</span> <span class="dt">Star</span></a>
<a class="sourceLine" id="cb7-2" data-line-number="2">    <span class="dt">Zero</span><span class="ot"> ::</span> <span class="dt">Nat</span></a>
<a class="sourceLine" id="cb7-3" data-line-number="3">    <span class="dt">Succ</span><span class="ot"> ::</span> (_ <span class="fu">:</span> <span class="dt">Nat</span>) <span class="ot">-&gt;</span> <span class="dt">Nat</span></a></code></pre></div>
<p>Which serve to define the natural numbers.</p>
<p>Last but not least, we’ve added “free variables” as an explicit</p>
<p>Now an important piece of a type checker is comparing types for equality, in STLC, equivalent types are syntactically equal so that was solved with <code>deriving Eq</code>. Here we need a bit more subtlety. Indeed, now we need to check arbitrary expressions for equality! This is hard. We’ll reduce things as much as possible and then just check syntactic equality. This means that <code>if True then a else b</code> would equal <code>a</code> as we’d hope, but <code>\x -&gt; if x then a else a</code> wouldn’t.</p>
<p>Now in order to facilitate this check we’ll define a type for fully reduced expressions. Since we’re only interested in checking equality on these terms we can toss the inferrable vs checkable division out the window.</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb8-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">VConst</span> <span class="fu">=</span> <span class="dt">CAp</span> <span class="dt">VConst</span> <span class="dt">Val</span></a>
<a class="sourceLine" id="cb8-2" data-line-number="2">                <span class="fu">|</span> <span class="dt">CVar</span> <span class="dt">String</span></a>
<a class="sourceLine" id="cb8-3" data-line-number="3">                <span class="fu">|</span> <span class="dt">CFree</span> <span class="dt">Int</span></a>
<a class="sourceLine" id="cb8-4" data-line-number="4"></a>
<a class="sourceLine" id="cb8-5" data-line-number="5">    <span class="kw">data</span> <span class="dt">Val</span> <span class="fu">=</span> <span class="dt">VStar</span></a>
<a class="sourceLine" id="cb8-6" data-line-number="6">             <span class="fu">|</span> <span class="dt">VBool</span></a>
<a class="sourceLine" id="cb8-7" data-line-number="7">             <span class="fu">|</span> <span class="dt">VTrue</span></a>
<a class="sourceLine" id="cb8-8" data-line-number="8">             <span class="fu">|</span> <span class="dt">VFalse</span></a>
<a class="sourceLine" id="cb8-9" data-line-number="9">             <span class="fu">|</span> <span class="dt">VConst</span> <span class="dt">VConst</span></a>
<a class="sourceLine" id="cb8-10" data-line-number="10">             <span class="fu">|</span> <span class="dt">VArr</span> <span class="dt">Val</span> <span class="dt">Val</span></a>
<a class="sourceLine" id="cb8-11" data-line-number="11">             <span class="fu">|</span> <span class="dt">VPi</span> <span class="dt">Val</span> (<span class="dt">Val</span> <span class="ot">-&gt;</span> <span class="dt">Val</span>)</a>
<a class="sourceLine" id="cb8-12" data-line-number="12">             <span class="fu">|</span> <span class="dt">VLam</span> (<span class="dt">Val</span> <span class="ot">-&gt;</span> <span class="dt">Val</span>)</a>
<a class="sourceLine" id="cb8-13" data-line-number="13">             <span class="fu">|</span> <span class="dt">VGen</span> <span class="dt">Int</span></a></code></pre></div>
<p>Now since we have constants we can have chains of application that we can’t reduce, that’s what <code>VConst</code> is. Notice that this handles the case of just having a constant nicely.</p>
<p>The value dichotomy uses a nice trick from the “Simple Easy!” paper, we use HOAS to have functions that reduce <em>themselves</em> when applied. The downside of this is that we need <code>VGen</code> to peek inside the now opaque <code>VLam</code> and <code>VPi</code>. The idea is we’ll generate a unique <code>Int</code> and apply the functions to <code>VGen i</code>.</p>
<p>Now in order to conveniently generate these fresh integers I used <code>monad-gen</code> (it’s not self promotion if it’s useful :). Equality checking comes to</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb9-1" data-line-number="1">    <span class="co">-- *Whistle and fidget with hands*</span></a>
<a class="sourceLine" id="cb9-2" data-line-number="2">    <span class="kw">instance</span> <span class="dt">Enum</span> <span class="dt">Val</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb9-3" data-line-number="3">      toEnum <span class="fu">=</span> <span class="dt">VGen</span></a>
<a class="sourceLine" id="cb9-4" data-line-number="4">      fromEnum _ <span class="fu">=</span> error <span class="st">&quot;You&#39;re a bad person.&quot;</span></a>
<a class="sourceLine" id="cb9-5" data-line-number="5"></a>
<a class="sourceLine" id="cb9-6" data-line-number="6"><span class="ot">    eqTerm ::</span> <span class="dt">Val</span> <span class="ot">-&gt;</span> <span class="dt">Val</span> <span class="ot">-&gt;</span> <span class="dt">Bool</span></a>
<a class="sourceLine" id="cb9-7" data-line-number="7">    eqTerm l r <span class="fu">=</span> runGen <span class="fu">$</span> go l r</a>
<a class="sourceLine" id="cb9-8" data-line-number="8">      <span class="kw">where</span> go <span class="dt">VStar</span> <span class="dt">VStar</span> <span class="fu">=</span> return <span class="dt">True</span></a>
<a class="sourceLine" id="cb9-9" data-line-number="9">            go <span class="dt">VBool</span> <span class="dt">VBool</span> <span class="fu">=</span> return <span class="dt">True</span></a>
<a class="sourceLine" id="cb9-10" data-line-number="10">            go <span class="dt">VTrue</span> <span class="dt">VTrue</span> <span class="fu">=</span> return <span class="dt">True</span></a>
<a class="sourceLine" id="cb9-11" data-line-number="11">            go <span class="dt">VFalse</span> <span class="dt">VFalse</span> <span class="fu">=</span> return <span class="dt">True</span></a>
<a class="sourceLine" id="cb9-12" data-line-number="12">            go (<span class="dt">VArr</span> f a) (<span class="dt">VArr</span> f&#39; a&#39;) <span class="fu">=</span> (<span class="fu">&amp;&amp;</span>) <span class="fu">&lt;$&gt;</span> go f f&#39; <span class="fu">&lt;*&gt;</span> go a a&#39;</a>
<a class="sourceLine" id="cb9-13" data-line-number="13">            go (<span class="dt">VLam</span> f) (<span class="dt">VLam</span> g) <span class="fu">=</span> gen <span class="fu">&gt;&gt;=</span> \v <span class="ot">-&gt;</span> go (f v) (g v)</a>
<a class="sourceLine" id="cb9-14" data-line-number="14">            go (<span class="dt">VPi</span> f) (<span class="dt">VPi</span> g) <span class="fu">=</span> gen <span class="fu">&gt;&gt;=</span> \v <span class="ot">-&gt;</span> go (f v) (g v)</a>
<a class="sourceLine" id="cb9-15" data-line-number="15">            go (<span class="dt">VGen</span> i) (<span class="dt">VGen</span> j) <span class="fu">=</span> return (i <span class="fu">==</span> j)</a>
<a class="sourceLine" id="cb9-16" data-line-number="16">            go (<span class="dt">VConst</span> c) (<span class="dt">VConst</span> c&#39;) <span class="fu">=</span> <span class="kw">case</span> (c, c&#39;) <span class="kw">of</span></a>
<a class="sourceLine" id="cb9-17" data-line-number="17">              (<span class="dt">CVar</span> v, <span class="dt">CVar</span> v&#39;) <span class="ot">-&gt;</span> return (v <span class="fu">==</span> v&#39;)</a>
<a class="sourceLine" id="cb9-18" data-line-number="18">              (<span class="dt">CAp</span> f a, <span class="dt">CAp</span> f&#39; a&#39;) <span class="ot">-&gt;</span></a>
<a class="sourceLine" id="cb9-19" data-line-number="19">                (<span class="fu">&amp;&amp;</span>) <span class="fu">&lt;$&gt;</span> go (<span class="dt">VConst</span> f) (<span class="dt">VConst</span> f&#39;) <span class="fu">&lt;*&gt;</span> go a a&#39;</a>
<a class="sourceLine" id="cb9-20" data-line-number="20">              _ <span class="ot">-&gt;</span> return <span class="dt">False</span></a>
<a class="sourceLine" id="cb9-21" data-line-number="21">            go _ _ <span class="fu">=</span> return <span class="dt">False</span></a></code></pre></div>
<p>Basically we just recurse and return true or false at the leaves.</p>
<p>Now that we know how to check equality of values, we actually need to map terms into those values. This involves basically writing a little interpreter.</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb10-1" data-line-number="1"><span class="ot">    inf ::</span> [<span class="dt">Val</span>] <span class="ot">-&gt;</span> <span class="dt">IExpr</span> <span class="ot">-&gt;</span> <span class="dt">Val</span></a>
<a class="sourceLine" id="cb10-2" data-line-number="2">    inf _ <span class="dt">ETrue</span> <span class="fu">=</span> <span class="dt">VTrue</span></a>
<a class="sourceLine" id="cb10-3" data-line-number="3">    inf _ <span class="dt">EFalse</span> <span class="fu">=</span> <span class="dt">VFalse</span></a>
<a class="sourceLine" id="cb10-4" data-line-number="4">    inf _ <span class="dt">Bool</span> <span class="fu">=</span> <span class="dt">VBool</span></a>
<a class="sourceLine" id="cb10-5" data-line-number="5">    inf _ <span class="dt">Star</span> <span class="fu">=</span> <span class="dt">VStar</span></a>
<a class="sourceLine" id="cb10-6" data-line-number="6">    inf _ (<span class="dt">Free</span> i) <span class="fu">=</span> <span class="dt">VConst</span> (<span class="dt">CFree</span> i)</a>
<a class="sourceLine" id="cb10-7" data-line-number="7">    inf _ (<span class="dt">Const</span> s) <span class="fu">=</span> <span class="dt">VConst</span> (<span class="dt">CVar</span> s)</a>
<a class="sourceLine" id="cb10-8" data-line-number="8">    inf env (<span class="dt">Annot</span> e _) <span class="fu">=</span> cnf env e</a>
<a class="sourceLine" id="cb10-9" data-line-number="9">    inf env (<span class="dt">Var</span> i) <span class="fu">=</span> env <span class="fu">!!</span> i</a>
<a class="sourceLine" id="cb10-10" data-line-number="10">    inf env (<span class="dt">Pi</span> l r) <span class="fu">=</span> <span class="dt">VPi</span> (cnf env l) (\v <span class="ot">-&gt;</span> cnf (v <span class="fu">:</span> env) r)</a>
<a class="sourceLine" id="cb10-11" data-line-number="11">    inf env (<span class="dt">App</span> l r) <span class="fu">=</span></a>
<a class="sourceLine" id="cb10-12" data-line-number="12">      <span class="kw">case</span> inf env l <span class="kw">of</span></a>
<a class="sourceLine" id="cb10-13" data-line-number="13">       <span class="dt">VLam</span> f <span class="ot">-&gt;</span> f (cnf env r)</a>
<a class="sourceLine" id="cb10-14" data-line-number="14">       <span class="dt">VConst</span> c <span class="ot">-&gt;</span> <span class="dt">VConst</span> <span class="fu">.</span> <span class="dt">CAp</span> c <span class="fu">$</span> cnf env r</a>
<a class="sourceLine" id="cb10-15" data-line-number="15">       _ <span class="ot">-&gt;</span> error <span class="st">&quot;Impossible: evaluated ill-typed expression&quot;</span></a>
<a class="sourceLine" id="cb10-16" data-line-number="16"></a>
<a class="sourceLine" id="cb10-17" data-line-number="17"><span class="ot">    cnf ::</span> [<span class="dt">Val</span>] <span class="ot">-&gt;</span> <span class="dt">CExpr</span> <span class="ot">-&gt;</span> <span class="dt">Val</span></a>
<a class="sourceLine" id="cb10-18" data-line-number="18">    cnf env (<span class="dt">CI</span> e) <span class="fu">=</span> inf env e</a>
<a class="sourceLine" id="cb10-19" data-line-number="19">    cnf env (<span class="dt">Lam</span> c) <span class="fu">=</span> <span class="dt">VLam</span> <span class="fu">$</span> \v <span class="ot">-&gt;</span> cnf (v <span class="fu">:</span> env) c</a></code></pre></div>
<p>The interesting cases are for <code>Lam</code>, <code>Pi</code>, and <code>App</code>. For <code>App</code> we actually do reductions wherever we can, otherwise we know that we’ve just got a constant so we slap that on the front. <code>Lam</code> and <code>Pi</code> are basically the same, they wrap the evaluation of the body in a function and evaluate it based on whatever is fed in. This is critical, otherwise <code>App</code>’s reductions get much more complicated.</p>
<p>We need one final thing. You may have noticed that all <code>Val</code>’s are closed, there’s no free DeBruijn variables. This means that when we go under a binder we can’t type check open terms since we’re representing types as values and the term we’re checking shares variables with its type.</p>
<p>This means that our type checker when it goes under a binder is going to substitute the now free variable for a fresh <code>Free i</code>. Frankly, this kinda sucks. I poked about for a better solution but this is what “Simple Easy!” does too..</p>
<p>To do these substitutions we have</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb11-1" data-line-number="1"><span class="ot">    ibind ::</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">IExpr</span> <span class="ot">-&gt;</span> <span class="dt">IExpr</span> <span class="ot">-&gt;</span> <span class="dt">IExpr</span></a>
<a class="sourceLine" id="cb11-2" data-line-number="2">    ibind i e (<span class="dt">Var</span> j) <span class="fu">|</span> i <span class="fu">==</span> j <span class="fu">=</span> e</a>
<a class="sourceLine" id="cb11-3" data-line-number="3">    ibind i e (<span class="dt">App</span> l r) <span class="fu">=</span> <span class="dt">App</span> (ibind i e l) (cbind i e r)</a>
<a class="sourceLine" id="cb11-4" data-line-number="4">    ibind i e (<span class="dt">Annot</span> l r) <span class="fu">=</span> <span class="dt">Annot</span> (cbind i e l) (cbind i e r)</a>
<a class="sourceLine" id="cb11-5" data-line-number="5">    ibind i e (<span class="dt">Pi</span> l r) <span class="fu">=</span> <span class="dt">Pi</span> (cbind i e l) (cbind i e r)</a>
<a class="sourceLine" id="cb11-6" data-line-number="6">    ibind _ _ e  <span class="fu">=</span> e <span class="co">-- Non recursive cases</span></a>
<a class="sourceLine" id="cb11-7" data-line-number="7"></a>
<a class="sourceLine" id="cb11-8" data-line-number="8"><span class="ot">    cbind ::</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">IExpr</span> <span class="ot">-&gt;</span> <span class="dt">CExpr</span> <span class="ot">-&gt;</span> <span class="dt">CExpr</span></a>
<a class="sourceLine" id="cb11-9" data-line-number="9">    cbind i e (<span class="dt">Lam</span> b) <span class="fu">=</span> <span class="dt">Lam</span> (cbind (i <span class="fu">+</span> <span class="dv">1</span>) e b)</a>
<a class="sourceLine" id="cb11-10" data-line-number="10">    cbind i e (<span class="dt">CI</span> c) <span class="fu">=</span> <span class="dt">CI</span> (ibind i e c)</a></code></pre></div>
<p>This was a bit more work than I anticipated, but now we’re ready to actually write the type checker!</p>
<p>Since we’re doing bidirectional type checking, we’re once again going to have two functions, <code>inferType</code> and <code>checkType</code>. Our environments is now a record</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb12-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">Env</span> <span class="fu">=</span> <span class="dt">Env</span> {<span class="ot"> localVar ::</span> <span class="dt">M.Map</span> <span class="dt">Int</span> <span class="dt">Val</span></a>
<a class="sourceLine" id="cb12-2" data-line-number="2">                   ,<span class="ot"> constant ::</span> <span class="dt">M.Map</span> <span class="dt">String</span> <span class="dt">Val</span> }</a></code></pre></div>
<p>The inferring stage is mostly the same</p>
<div class="sourceCode" id="cb13"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb13-1" data-line-number="1"><span class="ot">    inferType ::</span> <span class="dt">Env</span> <span class="ot">-&gt;</span> <span class="dt">IExpr</span> <span class="ot">-&gt;</span> <span class="dt">GenT</span> <span class="dt">Int</span> <span class="dt">Maybe</span> <span class="dt">Val</span></a>
<a class="sourceLine" id="cb13-2" data-line-number="2">    inferType _ (<span class="dt">Var</span> _) <span class="fu">=</span> lift <span class="dt">Nothing</span> <span class="co">-- The term is open</span></a>
<a class="sourceLine" id="cb13-3" data-line-number="3">    inferType (<span class="dt">Env</span> _ m) (<span class="dt">Const</span> s) <span class="fu">=</span> lift <span class="fu">$</span> M.lookup s m</a>
<a class="sourceLine" id="cb13-4" data-line-number="4">    inferType (<span class="dt">Env</span> m _) (<span class="dt">Free</span> i) <span class="fu">=</span> lift <span class="fu">$</span> M.lookup i m</a>
<a class="sourceLine" id="cb13-5" data-line-number="5">    inferType _ <span class="dt">ETrue</span> <span class="fu">=</span> return <span class="dt">VBool</span></a>
<a class="sourceLine" id="cb13-6" data-line-number="6">    inferType _ <span class="dt">EFalse</span> <span class="fu">=</span> return <span class="dt">VBool</span></a>
<a class="sourceLine" id="cb13-7" data-line-number="7">    inferType _ <span class="dt">Bool</span> <span class="fu">=</span> return <span class="dt">VStar</span></a>
<a class="sourceLine" id="cb13-8" data-line-number="8">    inferType _ <span class="dt">Star</span> <span class="fu">=</span> return <span class="dt">VStar</span></a>
<a class="sourceLine" id="cb13-9" data-line-number="9">    inferType env (<span class="dt">Annot</span> e ty) <span class="fu">=</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb13-10" data-line-number="10">      checkType env ty <span class="dt">VStar</span></a>
<a class="sourceLine" id="cb13-11" data-line-number="11">      <span class="kw">let</span> v <span class="fu">=</span> cnf [] ty</a>
<a class="sourceLine" id="cb13-12" data-line-number="12">      checkType env e v <span class="fu">&gt;&gt;</span> return v</a>
<a class="sourceLine" id="cb13-13" data-line-number="13">    inferType env (<span class="dt">App</span> f a) <span class="fu">=</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb13-14" data-line-number="14">      ty <span class="ot">&lt;-</span> inferType env f</a>
<a class="sourceLine" id="cb13-15" data-line-number="15">      <span class="kw">case</span> ty <span class="kw">of</span></a>
<a class="sourceLine" id="cb13-16" data-line-number="16">       <span class="dt">VPi</span> aTy body <span class="ot">-&gt;</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb13-17" data-line-number="17">         checkType env a aTy</a>
<a class="sourceLine" id="cb13-18" data-line-number="18">         return (body <span class="fu">$</span> cnf [] a)</a>
<a class="sourceLine" id="cb13-19" data-line-number="19">       _ <span class="ot">-&gt;</span> lift <span class="dt">Nothing</span></a>
<a class="sourceLine" id="cb13-20" data-line-number="20">    inferType env (<span class="dt">Pi</span> ty body) <span class="fu">=</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb13-21" data-line-number="21">      checkType env ty <span class="dt">VStar</span></a>
<a class="sourceLine" id="cb13-22" data-line-number="22">      i <span class="ot">&lt;-</span> gen</a>
<a class="sourceLine" id="cb13-23" data-line-number="23">      <span class="kw">let</span> v <span class="fu">=</span> cnf [] ty</a>
<a class="sourceLine" id="cb13-24" data-line-number="24">          env&#39; <span class="fu">=</span> env{locals <span class="fu">=</span> M.insert i v (locals env)}</a>
<a class="sourceLine" id="cb13-25" data-line-number="25">      checkType env&#39; (cbind <span class="dv">0</span> (<span class="dt">Free</span> i) body) <span class="dt">VStar</span></a>
<a class="sourceLine" id="cb13-26" data-line-number="26">      return <span class="dt">VStar</span></a></code></pre></div>
<p>The biggest difference is that now we have to compute some types on the fly. For example in <code>Annot</code> we check that we are in fact annotating with a type, then we reduce it to a value. This order is critical! Remember that <code>cnf</code> requires well typed terms.</p>
<p>Beyond this there are two interesting cases, there’s <code>App</code> which nicely illustrates what a pi type means and <code>Pi</code> which demonstrates how to deal with a binder.</p>
<p>For <code>App</code> we start in the same way, we grab the (function) type of the function. We can then check that the argument has the right type. To produce the output type however, we have to normalize the argument as far as we can and then feed it to <code>body</code> which computes the return type. Remember that if there’s some free variable in <code>a</code> then it’ll just be represented as <code>VConst (CFree ...)</code>.</p>
<p><code>Pi</code> checks that we’re quantifying over a type first off. From there it generates a fresh free variable and updates the environment before recursing. We use <code>cbind</code> to replace all occurrences of the now unbound variable for an explicit <code>Free</code>.</p>
<p><code>checkType</code> is pretty trivial after this. <code>Lam</code> is almost identical to <code>Pi</code> and <code>CI</code> is just <code>eqTerm</code>.</p>
<div class="sourceCode" id="cb14"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb14-1" data-line-number="1"><span class="ot">    checkType ::</span> <span class="dt">Env</span> <span class="ot">-&gt;</span> <span class="dt">CExpr</span> <span class="ot">-&gt;</span> <span class="dt">Val</span> <span class="ot">-&gt;</span> <span class="dt">GenT</span> <span class="dt">Int</span> <span class="dt">Maybe</span> ()</a>
<a class="sourceLine" id="cb14-2" data-line-number="2">    checkType env (<span class="dt">CI</span> e) v <span class="fu">=</span> inferType env e <span class="fu">&gt;&gt;=</span> guard <span class="fu">.</span> eqTerm v</a>
<a class="sourceLine" id="cb14-3" data-line-number="3">    checkType env (<span class="dt">Lam</span> ce) (<span class="dt">VPi</span> argTy body) <span class="fu">=</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb14-4" data-line-number="4">      i <span class="ot">&lt;-</span> gen</a>
<a class="sourceLine" id="cb14-5" data-line-number="5">      <span class="kw">let</span> ce&#39; <span class="fu">=</span> cbind <span class="dv">0</span> (<span class="dt">Free</span> i) ce</a>
<a class="sourceLine" id="cb14-6" data-line-number="6">          env&#39; <span class="fu">=</span> env{locals <span class="fu">=</span> M.insert i argTy (locals env)}</a>
<a class="sourceLine" id="cb14-7" data-line-number="7">      checkType env&#39; ce&#39; (body <span class="fu">$</span> <span class="dt">VConst</span> (<span class="dt">CFree</span> i))</a>
<a class="sourceLine" id="cb14-8" data-line-number="8">    checkType _ _ _ <span class="fu">=</span> lift <span class="dt">Nothing</span></a></code></pre></div>
<p>And that’s it!</p>
<h2 id="wrap-up">Wrap Up</h2>
<p>So let’s circle back to where we started: bidirectional type checking! Hopefully we’ve seen how structuring a type checker around these two core functions yields something quite pleasant.</p>
<p>What makes this really interesting though is how well it scales. You can use this style type checker to handle subtyping, [dependent] pattern matching, heaps and tons of interesting features.</p>
<p>At 400 lines though, I think I’ll stop here :)</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Sat, 22 Nov 2014 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2014-11-22-bidir.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Functors and Recursion</title>
    <link>http://jozefg.bitbucket.org/posts/2014-11-19-recursion.html</link>
    <description><![CDATA[<div class="info">
    Posted on November 19, 2014
    
</div>
<div class="info">
    
    Tags: <a href="/tags/haskell.html">haskell</a>
    
</div>

<p>One of the common pieces of folklore in the functional programming community is how one can cleanly formulate recursive types with category theory. Indeed, using a few simple notions we can build a coherent enough explanation to derive some concrete benefits.</p>
<p>In this post I’ll outline how one thinks of recursive types and then we’ll discuss some of the practical ramifications of such thoughts.</p>
<h2 id="precursor">Precursor</h2>
<p>I’m assuming the reader is familiar with some basic notions from category theory. Specifically familiarity with the definitions of categories and functors.</p>
<p>Let’s talk about endofunctors, which are functors whose domain and codomain are the same. <em>spoiler: These are the ones we care about in Haskell</em>. An interesting notion that comes from endofunctors is that of algebras. An algebra in this sense is a pair of an object <code>C</code>, and a map <code>F C → C</code>. Here <code>F</code> is called the “signature” and <code>C</code> is called the carrier.</p>
<p>If you curious about why these funny terms, in abstract algebra we deal with algebras which are comprised of a set of distinguished elements, functions, and axioms called the signature. From there we look at sets (called carriers) which satisfy the specification. We can actually cleverly rearrange the specification for something like a group into an endofunctor! It’s out of scope for this post, but interesting if algebras your thing.</p>
<p>Now we can in fact define a category for F-algebras. in such a category an object is <code>α : F A → A</code> and each arrow is a triplet.</p>
<ul>
<li>normal arrow <code>f : A → B</code></li>
<li>An F-algebra <code>α : F A → A</code></li>
<li>Another F-algebra <code>β : F B → B</code></li>
</ul>
<p>So that <code>f ∘ α = β ∘ F f</code>. In picture form</p>
<pre><code>         F f
F A ———————————————–→ F B
 |                    |
 |                    |
 | α                  | β
 ↓                    ↓
 A —————————————————→ B
           f</code></pre>
<p>commutes. I generally elide the fact that we’re dealing with triplets and instead focus on the arrow, since that’s the interesting bit.</p>
<p>Now that we’ve established F-algebras, we glance at one more thing. There’s one more concept we need, the notion of initial objects. An initial object is an… object, <code>I</code> in a category so that for any object <code>C</code></p>
<pre><code>          f
 I - - - - - - - - → C</code></pre>
<p>So that <code>f</code> is unique.</p>
<p>Now what we’re interested in investigating is the initial object in the category of F-algebras. That’d mean that</p>
<pre><code>           α
F I ————————————————–→ I
 |                     |
 |
 | F λ                 | λ
 |
 ↓                     ↓
F C —————————————————→ C</code></pre>
<p>Commutes only for a unique λ.</p>
<h2 id="a-list-is-just-an-initial-object-in-the-category-of-f-algebras.">A List is just an Initial Object in the Category of F-Algebras.</h2>
<p><em>What’s the problem?</em></p>
<p>Now, remembering that we’re actually trying to understand recursive types, how can we fit the two together? We can think of recursive types as solutions to certain equations. In fact, our types are what are called the <em>least fixed point</em> solutions. Let’s say we’re looking at <code>IntList</code>. We can imagine it defined as</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">IntList</span> <span class="fu">=</span> <span class="dt">Cons</span> <span class="dt">Int</span> <span class="dt">IntList</span> <span class="fu">|</span> <span class="dt">Nil</span></a></code></pre></div>
<p>We can in fact, factor out the recursive call in <code>Cons</code> and get</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb5-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">IntList</span> a <span class="fu">=</span> <span class="dt">Cons</span> <span class="dt">Int</span> a <span class="fu">|</span> <span class="dt">Nil</span></a>
<a class="sourceLine" id="cb5-2" data-line-number="2">                   <span class="kw">deriving</span> <span class="dt">Functor</span></a></code></pre></div>
<p>Now we can represent a list of length 3 as something like</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb6-1" data-line-number="1">    <span class="kw">type</span> <span class="dt">ThreeList</span> <span class="fu">=</span> <span class="dt">IntList</span> (<span class="dt">IntList</span> (<span class="dt">IntList</span> <span class="dt">Void</span>))</a></code></pre></div>
<p>Which is all well and good, but we really want arbitrary length list. We want a solution to the equation that</p>
<pre><code>X = IntList X</code></pre>
<p>We can view such a type as a set <code>{EmptyList, OneList, TwoList, ThreeList ... }</code>. Now how can we actually go about saying this? Well we need to take a fixed point of the equation! This is easy enough in Haskell since Haskell’s type system is unsound.</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb8-1" data-line-number="1">    <span class="co">-- Somewhere, somehow, a domain theorist is crying.</span></a>
<a class="sourceLine" id="cb8-2" data-line-number="2">    <span class="kw">data</span> <span class="dt">FixedPoint</span> f <span class="fu">=</span> <span class="dt">Fix</span> {<span class="ot">unfix ::</span> f (<span class="dt">FixedPoint</span> f)}</a></code></pre></div>
<p>Now we can regain our normal representation of lists with</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb9-1" data-line-number="1">    <span class="kw">type</span> <span class="dt">List</span> <span class="fu">=</span> <span class="dt">FixedPoint</span> <span class="dt">IntList</span></a></code></pre></div>
<p>To see how this works</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb10-1" data-line-number="1"><span class="ot">    out ::</span> <span class="dt">FixedPoint</span> <span class="dt">IntList</span> <span class="ot">-&gt;</span> [<span class="dt">Int</span>]</a>
<a class="sourceLine" id="cb10-2" data-line-number="2">    out (<span class="dt">Fix</span> f) <span class="fu">=</span> <span class="kw">case</span> fmap out f <span class="kw">of</span></a>
<a class="sourceLine" id="cb10-3" data-line-number="3">                    <span class="dt">Nil</span> <span class="ot">-&gt;</span> []</a>
<a class="sourceLine" id="cb10-4" data-line-number="4">                    <span class="dt">Cons</span> a b <span class="ot">-&gt;</span> a <span class="fu">:</span> b</a>
<a class="sourceLine" id="cb10-5" data-line-number="5"></a>
<a class="sourceLine" id="cb10-6" data-line-number="6"><span class="ot">    in ::</span> [<span class="dt">Int</span>] <span class="ot">-&gt;</span> <span class="dt">FixedPoint</span> <span class="dt">IntList</span></a>
<a class="sourceLine" id="cb10-7" data-line-number="7">    <span class="kw">in</span> [] <span class="fu">=</span> <span class="dt">Nil</span></a>
<a class="sourceLine" id="cb10-8" data-line-number="8">    <span class="kw">in</span> (x <span class="fu">:</span> xs) <span class="fu">=</span> <span class="dt">Fix</span> (<span class="dt">Cons</span> x (<span class="kw">in</span> xs))</a></code></pre></div>
<p>Now this transformation is interesting for one reason in particular, <code>IntList</code> is a functor. Because of this, we can formulate an F-algebra for <code>IntList</code>.</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb11-1" data-line-number="1">    <span class="kw">type</span> <span class="dt">ListAlg</span> a <span class="fu">=</span> <span class="dt">IntList</span> a <span class="ot">-&gt;</span> a</a></code></pre></div>
<p>Now we consider what the initial object in this category would be. It’d be something <code>I</code> so that we have a function</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb12-1" data-line-number="1"><span class="ot">    cata ::</span> <span class="dt">Listalg</span> a <span class="ot">-&gt;</span> (<span class="dt">I</span> <span class="ot">-&gt;</span> a) <span class="co">-- Remember that I -&gt; a is an arrow in F-Alg</span></a>
<a class="sourceLine" id="cb12-2" data-line-number="2"><span class="ot">    cata ::</span> (<span class="dt">List</span> a <span class="ot">-&gt;</span> a) <span class="ot">-&gt;</span> <span class="dt">I</span> <span class="ot">-&gt;</span> a</a>
<a class="sourceLine" id="cb12-3" data-line-number="3"><span class="ot">    cata ::</span> (<span class="dt">Either</span> () (a, <span class="dt">Int</span>) <span class="ot">-&gt;</span> a) <span class="ot">-&gt;</span> <span class="dt">I</span> <span class="ot">-&gt;</span> a</a>
<a class="sourceLine" id="cb12-4" data-line-number="4"><span class="ot">    cata ::</span> (() <span class="ot">-&gt;</span> a) <span class="ot">-&gt;</span> ((a, <span class="dt">Int</span>) <span class="ot">-&gt;</span> a) <span class="ot">-&gt;</span> <span class="dt">I</span> <span class="ot">-&gt;</span> a</a>
<a class="sourceLine" id="cb12-5" data-line-number="5"><span class="ot">    cata ::</span> a <span class="ot">-&gt;</span> (<span class="dt">Int</span> <span class="ot">-&gt;</span> a <span class="ot">-&gt;</span> a) <span class="ot">-&gt;</span> <span class="dt">I</span> <span class="ot">-&gt;</span> a</a>
<a class="sourceLine" id="cb12-6" data-line-number="6"><span class="ot">    cata ::</span> (<span class="dt">Int</span> <span class="ot">-&gt;</span> a <span class="ot">-&gt;</span> a) <span class="ot">-&gt;</span> a <span class="ot">-&gt;</span> <span class="dt">I</span> <span class="ot">-&gt;</span> a</a></code></pre></div>
<p>Now that looks sort of familiar, what’s the type of <code>foldr</code> again?</p>
<div class="sourceCode" id="cb13"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb13-1" data-line-number="1"><span class="ot">    foldr ::</span> (a <span class="ot">-&gt;</span> b <span class="ot">-&gt;</span> b) <span class="ot">-&gt;</span> b <span class="ot">-&gt;</span> [a] <span class="ot">-&gt;</span> a</a>
<a class="sourceLine" id="cb13-2" data-line-number="2"><span class="ot">    foldr ::</span> (<span class="dt">Int</span> <span class="ot">-&gt;</span> a <span class="ot">-&gt;</span> a) <span class="ot">-&gt;</span> a <span class="ot">-&gt;</span> [<span class="dt">Int</span>] <span class="ot">-&gt;</span> a</a></code></pre></div>
<p>So the arrow we get from the initiality of <code>I</code> is precisely the same as <code>foldr</code>! This leads us to believe that maybe the initial object for F-algebras in Haskell is just the least fixed point, just as <code>[Int]</code> is the least fixed point for <code>IntList</code>.</p>
<p>To confirm this, let’s generalize a few of our definitions from before</p>
<div class="sourceCode" id="cb14"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb14-1" data-line-number="1">    <span class="kw">type</span> <span class="dt">Alg</span> f a <span class="fu">=</span> f a <span class="ot">-&gt;</span> a</a>
<a class="sourceLine" id="cb14-2" data-line-number="2">    <span class="kw">data</span> <span class="dt">Fix</span> f <span class="fu">=</span> <span class="dt">Fix</span> {<span class="ot">unfix ::</span> f (<span class="dt">Fix</span> f)}</a>
<a class="sourceLine" id="cb14-3" data-line-number="3"></a>
<a class="sourceLine" id="cb14-4" data-line-number="4">    <span class="kw">type</span> <span class="dt">Init</span> f <span class="fu">=</span> <span class="dt">Alg</span> f (<span class="dt">Fix</span> f)</a>
<a class="sourceLine" id="cb14-5" data-line-number="5"></a>
<a class="sourceLine" id="cb14-6" data-line-number="6"><span class="ot">    cata ::</span> <span class="dt">Functor</span> f <span class="ot">=&gt;</span> <span class="dt">Alg</span> f a <span class="ot">-&gt;</span> <span class="dt">Fix</span> f <span class="ot">-&gt;</span> a</a>
<a class="sourceLine" id="cb14-7" data-line-number="7">    cata f <span class="fu">=</span> f <span class="fu">.</span> fmap (cata f) <span class="fu">.</span> unfix</a></code></pre></div>
<p><em>Exercise, draw out the reduction tree for <code>cata</code> on lists</em></p>
<p>Our suspicion is confirmed, the fixed point of an functor is indeed the initial object. Further more, we can easily show that initial objects are unique up to isomorphism (exercise!) so anything that can implement <code>cata</code> is isomorphic to the original, recursive definition we were interested in.</p>
<h2 id="when-the-dust-settles">When The Dust Settles</h2>
<p>Now that we’ve gone and determined a potentially interesting fact about recursive types, how can we use this knowledge? Well let’s start with a few things, first is that we can define a truly generic fold function now:</p>
<div class="sourceCode" id="cb15"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb15-1" data-line-number="1"><span class="ot">    fold ::</span> <span class="dt">Functor</span> f <span class="ot">=&gt;</span> (f a <span class="ot">-&gt;</span> a) <span class="ot">-&gt;</span> <span class="dt">Fix</span> f <span class="ot">-&gt;</span> a</a></code></pre></div>
<p>This delegates all the messy details of how one actually thinks about handling the “shape” of the container we’re folding across by relegating it to the collapsing function <code>f a -&gt; a</code>.</p>
<p>While this may seem like a small accomplishment, it does mean that we can build off it to create data type generic programs that can be fitted into our existing world.</p>
<p>For example, what about mutual recursion. Fold captures the notion of recurring across one list in a rather slick way, however, recurring over two in lockstep involves a call to zip and other fun and games. How can we capture this with <code>cata</code>?</p>
<p>We’d imagine that the folding functions for such a scenario would have the type</p>
<div class="sourceCode" id="cb16"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb16-1" data-line-number="1">    f (a, b) <span class="ot">-&gt;</span> a</a>
<a class="sourceLine" id="cb16-2" data-line-number="2">    f (a, b) <span class="ot">-&gt;</span> b</a></code></pre></div>
<p>From here we can build</p>
<div class="sourceCode" id="cb17"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb17-1" data-line-number="1"><span class="ot">    muto ::</span> (f (a, b) <span class="ot">-&gt;</span> a) <span class="ot">-&gt;</span> (f (a, b) <span class="ot">-&gt;</span> b) <span class="ot">-&gt;</span> <span class="dt">Fix</span> f <span class="ot">-&gt;</span> (a, b)</a>
<a class="sourceLine" id="cb17-2" data-line-number="2">    muto f g <span class="fu">=</span> cata ((,) <span class="fu">&lt;$&gt;</span> f <span class="fu">&lt;*&gt;</span> g)</a></code></pre></div>
<p>Similarly we can build up <a href="https://hackage.haskell.org/package/recursion-schemes-4.1/docs/Data-Functor-Foldable.html#g:3">oodles</a> of combinators for dealing with folding all built on top of <code>cata</code>!</p>
<p>That unfortunately sounds like a lot of work! We can shamelessly free-load of the hard work of others thanks to hackage though. In particular, the package <code>recursion-schemes</code> has built up a nice little library for dealing with initial algebras. There’s only one big twist between what we’ve laid out and what it does.</p>
<p>One of the bigger stumbling blocks for our library was changing the nice recursive definition of a type into the functorfied version. Really it’s not realistic to write all your types this way. To help simplify the process <code>recursion-schemes</code> provides a type family called <code>Base</code> which takes a type and returns its functorfied version. We can imagine something like</p>
<div class="sourceCode" id="cb18"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb18-1" data-line-number="1">    <span class="kw">data</span> <span class="kw">instance</span> <span class="dt">Base</span> [a] b <span class="fu">=</span> <span class="dt">Cons</span> a b <span class="fu">|</span> <span class="dt">Nil</span></a></code></pre></div>
<p>This simplifies the process of actually using all these combinators we’re building. To use recursion-schemes, all you need to is define such an instance and write <code>project :: t -&gt; Base t t</code>. After that it’s all kittens and recursion.</p>
<h2 id="wrap-up">Wrap Up</h2>
<p>So dear reader, where are we left? We’ve got a new interesting formulation of recursive types that yields some interesting results and power. There’s one interesting chunk we’ve neglected though: what does unfolding look like?</p>
<p>It turns out there’s a good story for this as well, unfolding is the operation (anamorphism) defined by a terminal object in a category. A terminal object is the precise dual of an initial one. You can notice this all in recursion-schemes which features <code>ana</code> as well as <code>cata</code>.</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Wed, 19 Nov 2014 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2014-11-19-recursion.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>The Guts of a Spineless Machine</title>
    <link>http://jozefg.bitbucket.org/posts/2014-10-28-stg.html</link>
    <description><![CDATA[<div class="info">
    Posted on October 28, 2014
    
</div>
<div class="info">
    
    Tags: <a href="/tags/haskell.html">haskell</a>, <a href="/tags/c.html">c</a>
    
</div>

<p>It’s fairly well known that Haskell is a bit um.. different from how stock hardware sees the world. I’m not aware of too many processors that have decided that immutability and higher order functions are the right way to go.</p>
<p>Compiling Haskell and its ilk, however, does have one interesting wrinkle on top of the normal problem: laziness. Laziness stands completely at odds with how most everything else works. Moreover, whether or not you think it’s the right default, it’s an interesting question of how to efficiently compile some evaluation strategy other than call by value or name.</p>
<p>To this end, people have built a lot of abstract machines that lazy languages could target. These machines can be mapped easily to what the hardware wants and transitively, we can get our compiler. Most of these work by “graph reduction” (that’s the G in STG) and the latest incarnation of these graph machines is the spineless tagless graph machine which lies at the heart of GHC and a few other compilers.</p>
<p>In this post, I’d like to go over how exactly the STG machine actually works. Turns out it’s pretty cool!</p>
<h3 id="core-concepts">Core Concepts</h3>
<p>The basic idea behind a compiler intent on going the STG route is something like</p>
<ol type="1">
<li>.. front end stuff ..</li>
<li>Translate IL to STG language</li>
<li>Compile STG language to C/ASM/LLVM/Javascript</li>
</ol>
<p>In GHC case I understand the pipeline is something like</p>
<ol type="1">
<li>Parsing</li>
<li>Type checking</li>
<li>Desugaring + a few bobs and bits</li>
<li>Translation to core</li>
<li>Lion share of optimization</li>
<li>Translation to STG language</li>
<li>STG language to C–</li>
<li>C– to assembly or llvm</li>
</ol>
<p>We’re really concerned with parts 6 and 7 here. First things first, let’s lay out what’s exactly in the STG language. It’s a tiny functional language that looks a bit like Haskell or Core, with a few restrictions. A program is simply a series of bindings, much like Haskell. The top levels look something like</p>
<pre><code>f = {x y z} flag {a b c} -&gt; ...</code></pre>
<p>You should read this for now as <code>f = \a b c -&gt; ...</code>. The first set of variables and the flag correspond to some stuff we’ll discuss later.</p>
<p>Inside the <code>...</code> we can write most of what you would expect from Haskell. We have let[rec] bindings, case expressions, application, constructors, literals, and primitives. There is a caveat though: first off all, constructor applications must be fully saturated. This isn’t unlike OCaml or something where you can’t just treat a constructor as a function with an arbitrary name. We would write</p>
<pre><code>\a -&gt; Just a</code></pre>
<p>instead of just <code>Just</code>. Another bit of trickiness: our language has no lambdas! So we can’t even write the above. Instead if we had something like</p>
<pre><code> map Just [1, 2, 3]</code></pre>
<p>We’d have to write</p>
<pre><code> let f   = \a -&gt; Just a
     l&#39;&#39; = 3 : nil
     l&#39;  = 2 : l&#39;&#39;
     l   = 1 : l&#39;
 in map f l</code></pre>
<p>The reason for the awkward <code>l''</code> series is that we’re only allowed to apply constructors and functions to atoms (literals and variables).</p>
<p>One other noteworthy feature of STG is that we have primitive operations. They need to be fully saturated, just like constructors, but they work across unboxed things. For example there would probably be something like <code>+#</code> which adds to unboxed integers. To work with these we also have unboxed literals, <code>1#</code>, <code>2#</code>, so on and so on.</p>
<p>Now, despite all these limitations placed on STG, it’s still a pretty stinking high level language. There’s letrec, higher order functions, a lot of the normal stuff we’d expect in a functional language. This means it’s not actually to hard to compile something like Haskell or Core to STG (I didn’t say “compile efficiently”).</p>
<p>As an example, let’s look at translating factorial into STG language. We start with</p>
<pre><code>f :: Int -&gt; Int
f i = case i of
  0 -&gt; 1
  i -&gt; i * (f (i - 1))</code></pre>
<p>Now the first step is we change the binding form</p>
<pre><code>f = {} n {i} -&gt; ...</code></pre>
<p>The case expressions clause can remain the same, we’re already casing on an atom</p>
<pre><code>case i of
  (MkInt# i#) -&gt; ...</code></pre>
<p>Now comes the first big change, our boxed integers are going to get in the way here, so the case expression strips away the constructor leaving us with an unboxed integer. We can similarly refactor the body to make evaluation order explicit</p>
<pre><code> case i of
   MkInt i# -&gt;
     case i# -# 1# of
       dec# -&gt;
         let dec = \{dec#} u {} -&gt; MkInt dec#
         in case fact dec of
              MkInt rest# -&gt;
                case i# * rest# of
                  result# -&gt; MkInt result#</code></pre>
<p>Notice how the <code>case</code> expressions here are used to make the evaluation of various expressions explicit and <code>let</code> was used to create a new thing to evaluate.</p>
<p>Now we can see what those extra {}’s were for. They notate the free variables for a thunk. Remember how we can have all sorts of closures and it can make for some really nice code? Well the machine doesn’t exactly support those naively. What we need to do and note the variables that we close over explicitly and then generate code that will store these free variables with the value that closes over them. This pair is more or less what is called a “closure” for the rest of this post.</p>
<p>Actually, I’ll sometimes use “thunk” as another descriptor for this pair. This is because closures in STG land do quite a lot! In particular, they are used to represent the fundamental unit of lazy code, not just closing over variables. We’ll have closures that actually don’t close over anything! This would be a bit strange, but each “thunk” in Haskell land is going to become a closure in STG-ville. The notion of forcing a thunk in Haskell is analogous to evaluating an STG closure and creating a thunk is creating a new closure. This is helpful to keep in mind as we examine the rest of the machine.</p>
<p><code>dec</code> for example has a free variable <code>dec#</code> and it exists to box that result for the recursive call to factorial. We use <code>case</code> expressions to get evaluation. Most programs thus become chains of <code>case</code>’s and <code>let</code> alternating between creating thunks and actually doing work.</p>
<p>That <code>u</code> in between the {}’s in <code>dec</code> was also important. It’s the update flag. Remember how in Haskell we don’t want to force the same thunk twice. If I say</p>
<pre><code>let i = 1 + 1 in i + i</code></pre>
<p>We should only evaluate <code>1 + 1</code> once. That means that the thunk <code>i</code> will have to be mutated to not evaluate <code>1 + 1</code> twice. The update flag signifies the difference between thunks that we want to update and thunks that we don’t. For example, if we replaced the thunk for <code>+</code> with the first result it returned, we’d be mighty surprised. Suddenly <code>1 + 1 + 1</code> is just 2!</p>
<p>The <code>u</code> flag says “yes, I’m just a normal expression that should be updated” and the n flag says the opposite.</p>
<p>That about wraps up our discussion of the STG language, let’s talk about how to implement it now.</p>
<h3 id="semantics">Semantics</h3>
<p>This language wouldn’t be much good if it didn’t lend itself to an easy implementation, indeed we find that the restrictions we placed upon the language prove to be invaluable for its compilation (almost like they were designed that way!).</p>
<p>In order to decide how best to implement it, we first define the formal semantics for our language, which operates on a tuple of 6 things:</p>
<ol type="1">
<li>The code - the instruction we’re currently executing</li>
<li>The argument stack - A stack of integers or pointers to closures</li>
<li>The return stack - A stack of continuations</li>
<li>The update stack - A stack of update frames</li>
<li>The heap - A map from addresses to closures</li>
<li>The environment - A map from names to addresses of toplevel closures</li>
</ol>
<p>A code is more or less the current thing we’re attempting to do. It’s either</p>
<ol type="1">
<li><code>Eval e p</code> - evaluate an expression in an environment (<code>p</code>)</li>
<li><code>Enter a</code> - Enter a closure</li>
<li><code>ReturnCon c ws</code> - Return a constructor applied to some arguments</li>
<li><code>ReturnInt</code> - Return an integer</li>
</ol>
<p>Now the idea is we’re going to “unroll” our computations into pushing things onto the continuation stack and entering closures. We start with the code <code>Eval main {}</code>. That is to say, we start by running <code>main</code>. Then if we’re looking at a <code>case</code> we do something really clever</p>
<pre><code> EVAL(case expr of {pat1 -&gt; expr1; ...}, p) as rs us h o</code></pre>
<p>becomes</p>
<pre><code>EVAL (expr, p) as ({pat1 -&gt; expr1; ...} : rs) us h o</code></pre>
<p>That is to say, we just push the pattern matching on to the continuation stack and evaluate the expression.</p>
<p>At some point we’ll get to a “leaf” in our expression. That is random literal (a number) or constructor. At this point we make use of our continuation stack</p>
<pre><code>EVAL (C ws, p) as ((...; c vs -&gt; expr; ...) : rs) us h o
ReturnCon (C ws) as ((...; c vs -&gt; expr; ...) : rs) us h o
EVAL (expr, p[vs -&gt; ws]) as rs us h o</code></pre>
<p>So our pattern matching is rolled into <code>ReturnCon</code>. <code>ReturnCon</code> will just look on top of the return stack looking for a continuation which wants its constructor and evaluate its expression, mapping the constructor’s variables to the pattern’s variables.</p>
<p>The story is similar for literals</p>
<pre><code>EVAL (Int i, p) as ((...; c vs -&gt; expr; ...) : rs) us h o
ReturnInt i as ((...; i -&gt; expr; ...) : rs) us h o
EVAL (expr, p) as rs us h o</code></pre>
<p>Another phase is how we handle let’s and letrec’s. In this phase instead of dealing with continuations, we allocate more thunks onto the heap.</p>
<pre><code>EVAL ((let x = {fs} f {xs} -&gt; e; ... in expr), p) as rs us h o
EVAL e p&#39; as us h&#39; o</code></pre>
<p>So as we’d expect, evaluating a let expression does indeed go and evaluate the body of the let expression, but changes up the environment in which we evaluate them. We have</p>
<pre><code>p&#39; = p[x -&gt; Addr a, ...]
h&#39; = h[a -&gt; ({fs} f {xs} -&gt; e) p fs, ...]</code></pre>
<p>In words “the new environment contains a binding for <code>x</code> to some address <code>a</code>. The heap is extended with an address <code>a</code> with a closure <code>{fs} f {xs} -&gt; ...</code> where the free variables come from <code>p</code>”. The definition for letrec is identical except the free variables come from <code>p'</code> allowing for recursion.</p>
<p>So the STG machine allocates things in lets, adds continuations with case, and jumps to continuation on values.</p>
<p>Now we also have to figure out applications.</p>
<pre><code>EVAL (f xs, p) as rs us h o
ENTER a (values of xs ++ as) rs us h o</code></pre>
<p>where the value of <code>f</code> is <code>Addr a</code>. So we push all the arguments (remember they’re atoms and therefore trivial to evaluate) on to the argument stack and enter the closure of the function.</p>
<p>How do we actually enter a closure? Well we know that our closures are of the form</p>
<pre><code>({fs} f {vs} -&gt; expr) frees</code></pre>
<p>If we have enough arguments to run the closure (length vs &gt; length of argument stack), then we can just <code>EVAL expr [vs -&gt; take (length vs) as, fs -&gt; frees]</code>. This might not be the case in something like Haskell though, we have partial application. So what do we do in this case?</p>
<p>What we want is to somehow get something that’s our closure but also knows about however many arguments we actually supplied it. Something like</p>
<pre><code>({fs ++ supplied} f {notSupplied} -&gt; expr) frees ++ as</code></pre>
<p>where <code>supplied ++ notSupplied = vs</code>. This updating of a closure is half of what our update stack <code>us</code> is for. The other case is when we <em>do</em> actually enter the closure, but <code>f = u</code> so we’re going to want to update it. If this is the case we add an update from to the stack <code>(as, rs, a)</code> where <code>as</code> is the argument stack, <code>rs</code> is the return stack, and <code>a</code> is the closure which should be updated. Once we’ve pushed this frame, we promptly empty the argument stack and return stack.</p>
<p>We then add the following rules to the definition of <code>ReturnCon</code></p>
<pre><code>ReturnCon c ws {} {} (as, rs, a) : us h o
ReturnCon c ws as rs us h&#39; o</code></pre>
<p>where <code>h'</code> is the new heap that’s replaced our old closure at <code>a</code> with our new, spiffy, updated closure</p>
<pre><code>h&#39; = h[a -&gt; ({vs} n {} -&gt; c vs) ws]</code></pre>
<p>So that’s what happens when we go to update a closure. But what about partial application?</p>
<pre><code>Enter a as {} (asU, rs, aU) : us h o
Enter a (as ++ asU) rs us h&#39; o</code></pre>
<p>where</p>
<pre><code>h a = ({vs} n {xs} -&gt; expr) frees
h&#39; = h [aU -&gt; ((vs ++ bound) n xs -&gt; e) (frees ++ as)]</code></pre>
<p>This is a simplified rule from what’s actually used, but gives some intuition to what’s happening: we’re minting a new closure in which we use the arguments we’ve just bound and that’s what the result of our update is.</p>
<h3 id="compiling-this">Compiling This</h3>
<p>Now that we have some idea of how this is going to work, what does this actually become on the machine?</p>
<p>The original paper by SPJ suggests an “interpreter” approach to compilation. In other words, we actually almost directly map the semantics to C and call it compiled. There’s a catch though, we’d like to represent the body of closures as C functions since they’re well.. functions. However, since all we do is enter closures and jump around to things till the cows come home, it had damn well better be fast. C function calls aren’t built to be that fast. Instead the paper advocates a tiny trampolining-esque approach.</p>
<p>When something wants to enter a closure, it merely returns it and our main loop becomes</p>
<pre><code> while(1){cont = (*cont)();}</code></pre>
<p>Which won’t stackoverflow. In reality, more underhanded tricks are applied to make the performance suck less, but for we’ll ignore such things.</p>
<p>In our compiled results there will be 2 stacks, not the 3 found in our abstract machine. In the first stack (A-stack) there are pointer things and the B-stack has non-pointers. This are monitored by two variables/registers <code>SpA</code> and <code>SpB</code>which keep track of the heights of the two stacks. Then compilation becomes reasonably straightforward.</p>
<p>An application pushes the arguments onto appropriate stacks, adjusts Sp*, and enters the function. A let block allocates each of the bound variables, then the body. Entering a closure simply jumps to the closure’s code pointer. This is actually quite nifty. All the work of figuring out exactly what <code>Enter</code> will do (updates, continuation jiggering) is left to the closure itself.</p>
<p>A case expression is a bit more complicated since a continuation’s representation involves boxing up the local environment for each branch. Once that’s bundled away, we represent a continuation as a simple code pointer. It is in charge of scrutinizing the argument stack and selecting an alternative and then running the appropriate code. This is a lot of work, and, unless I’m crazy, we’ll need two types of bound variables for each branch (really just ptr/non-ptr). The selection of an alternative would be represented as a C switch, letting all sorts of trickery with jump tables be done by the C compiler.</p>
<p>In order to return a value, we do something clever. We take a constructor and point a global variable at its constructor closure, containing its values and jump to the continuation. The continuation can then peek and poke at this global variable to bind things as needed for the alternatives. There is potentially a massive speedup by returning through registers, but this is dangerously close to work.</p>
<p>From here, primitive operations can be compiled to statements/instructions in whatever environment we’re targeting. In C for example we’d just use the normal <code>+</code> to add our unboxed integers.</p>
<p>The last beast to slay is updates. We represent update frames by pointers to argument stacks and a pointer to a closure. That means that the act of updating is merely saving <code>Sp*</code> in an update form, clobbering them, and then jumping into the appropriate closure. We push the update form onto stack B and keep on going.</p>
<p>I realize that this is a glancing overview and I’m eliding a lot of the tricky details, but hopefully this is sufficient to understand a bit about what’s going on at an intuitive level.</p>
<h3 id="wrap-up">Wrap Up</h3>
<p>So now that you’ve put all the effort to get through this post, I get to tell you it’s all lies! In reality GHC has applied all manner of tricks and hacks to get fast performance out of the STG model. To be honest I’m not sure where I should point to that explains these tricks because well… I have no idea what they are.</p>
<p>I can point to</p>
<ul>
<li><a href="http://research.microsoft.com/~simonpj/papers/spineless-tagless-gmachine.ps.gz">SPJ’s original paper</a></li>
<li><a href="https://ghc.haskell.org/trac/ghc/wiki/Commentary/Compiler/GeneratedCode">The Relevant GHC Wiki Page</a></li>
</ul>
<p>If you have any suggestions for other links I’d love to add them!</p>
<p><em>Thanks Chris Ganas for proof reading</em></p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Tue, 28 Oct 2014 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2014-10-28-stg.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Notes on Focusing</title>
    <link>http://jozefg.bitbucket.org/posts/2014-10-27-focusing.html</link>
    <description><![CDATA[<div class="info">
    Posted on October 27, 2014
    
</div>
<div class="info">
    
    Tags: <a href="/tags/types.html">types</a>, <a href="/tags/notes.html">notes</a>
    
</div>

<p>I’ve been spending a lot of time whacking my head on focusing literature. I’d like to jot down some intuition around what a focused system is and how it relates to the rest of the world. I’m going to steer clear of actually proving things but I will point out where a proof would be needed.</p>
<h3 id="what-is-focusing">What Is Focusing</h3>
<p>In a nutshell, focusing is a strategy to create proofs that minimizes the amount of choices available at each step. Focusing is thus amenable to mechanization since a computer is very good at applying a lot of deterministic procedures but incredibly bad at nondeterministic choice.</p>
<p>Now when we set out to define a focused system we usually do something like</p>
<ol type="1">
<li>Formalize our logical framework with natural deduction</li>
<li>Translate our framework into a sequent calculus</li>
<li>Transform our sequent calculus into a focused variant</li>
</ol>
<p>At each of these steps there’s a proof that says something like “System 2 is sound and complete with respect to System 1”. We can then chain these proofs together to get that we can transform any nonfocused proof into a focused one (focalization) and the reverse (de-focalization).</p>
<p>In order to actually carry out these proofs there’s a fair amount of work and pain. Usually we’ll need something like cut elimination and/or identity expansion.</p>
<h3 id="groundwork">Groundwork</h3>
<p>Now before we go on to define an example logic, let’s notice a few things. First off, in sequent calculus there are left and right rules. Left rules decompose known facts into other known facts while right rules transform our goal. There’s also an identity sequent which more or less just states</p>
<pre><code> A is an atom
 —————————————
   Γ, A → A</code></pre>
<p>This is a bit boring though.</p>
<p>Now certain rules are invertible: their conclusion implies their premise in addition to the reverse. For example if I said you must prove <code>A ∧ B</code> clearly we’ll have to prove both <code>A</code> and <code>B</code> in order to prove <code>A ∧ B</code>; there’s no alternative set of rule applications that let us circumvent proving <code>A</code> and <code>B</code>.</p>
<p>This means that if we were mechanically trying to prove something of the form <code>A ∧ B</code> we can immediately apply the right rule that decomposes <code>∧</code> into 2 goals.</p>
<p>We can these sort of rules invertible or asynchronous. Dually, there are rules that when applied transform our goal into something impossible to prove. Consider <code>⊥ ∨ ⊤</code>, clearly apply the rule that transforms this into <code>⊥</code> would be a bad idea!</p>
<p>Now if we begin classifying all the left and write rules we’ll notice that the tend to all into 2 categories</p>
<ul>
<li>Things with invertible left rules and noninvertible right rules</li>
<li>Things with noninvertible left rules and invertible right rules</li>
</ul>
<p>We dub the first group “positive” things and the second “negative” things. This is called polarization and isn’t strictly necessary but greatly simplifies a lot of our system.</p>
<p>Now there are a few things that could be considered both positive and negative. For example we can consider <code>∧</code> as positive with</p>
<pre><code>  Γ → A⁺  Γ → B⁺
 ———————————————
   Γ → A⁺ ∧ B⁺

   Γ, A⁺, B⁺ → C
 —————————————————
   Γ, A⁺ ∧ B⁺ → C</code></pre>
<p>In this case, the key determiner for the polarity of ∧ comes from its subcomponents. We can just treat ∧ as positive along with its subcomponents and with an appropriate dual ∧⁻, our proof system will still be complete.</p>
<p>As a quick example, implication <code>⊃</code> is negative. the right rule</p>
<pre><code> Γ, A → B
——————————
Γ → A ⊃ B</code></pre>
<p>While its left rule isn’t</p>
<pre><code> Γ, A ⊃ B → A  Γ, B, A ⊃ B → C
 ——————————————————————————————
         Γ, A ⊃ B → C</code></pre>
<p>Since we could easily have something like <code>⊥ ⊃ ⊤</code> but using this rule would entail (heh) proving <code>⊥</code>! Urk. If our system applied this rules remorselessly, we’d quickly end up in a divergent proof search.</p>
<h3 id="an-actual-focused-system">An Actual Focused System</h3>
<p><em>Do note that these typing rules are straight out of Rob Simmons’ paper, linked below</em></p>
<p>Now that we’ve actually seen some examples of invertible rules and polarized connectives, let’s see how this all fits into a coherent system. There is one critical change we must make to the structure of our judgments: an addition to the form <code>_ → _</code>. Instead of just an unordered multiset on the left, in order to properly do inversion we change this to <code>Γ; Ω ⊢ A</code> where Ω is an ordered list of propositions we intend to focus on.</p>
<p>Furthermore, since we’re dealing with a polarized calculus, we occasionally want to view positive things as negative and vice versa. For this we have shifts, ↓ and ↑. When we’re focusing on some proposition and we reach a shift, we pop out of the focused portion of our judgment.</p>
<p>Our system is broken up into 3 essentially separate judgments. In this judgment we basically apply as many invertible rules as many places as we can.</p>
<pre><code> Γ, A⁻; Q ⊢ U
——————————————
Γ; ↓A⁻, Q ⊢ U

Γ; A⁺, Ω ⊢ U  Γ; B+; Ω ⊢ U
———————————————————————————
    Γ; A⁺ ∨ B⁺, Ω ⊢ U

  Γ; A⁺, B⁺, Ω ⊢ U
————————————————————
  Γ; A⁺ ∧ B⁺, Ω ⊢ U

——————————————
 Γ; ⊥, Ω ⊢ U</code></pre>
<p>We first look at how to break down Ω into simpler forms. The idea is that we’re going to keep going till there’s nothing left in Ω. Ω can only contain positive propositions so eventually we’ll decompose everything to shifts (which we move into Γ) ⊤+ (which we just drop on the floor) or ⊥ (which means we’re done). These are all invertible rules to we can safely apply them eagerly and we won’t change the provability of our goal.</p>
<p>Once we’ve moved everything out of Ω we can make a choice. If <code>U</code> is “stable” meaning that we can’t break it down further easily, we can pick a something negative out of our context and focus on it</p>
<pre><code>   Γ; [A⁻] ⊢ U
  ————————————–
  Γ, A⁻; • ⊢ U</code></pre>
<p>This pops us into the next judgment in our calculus. However, if U is not stable, then we have to decompose it further as well.</p>
<pre><code>  Γ; • ⊢ A⁺
——————————————
  Γ; • ⊢ ↑ A⁺

———————————
 Γ; • ⊢ ⊤⁻

  Γ; A⁺ ⊢ B⁻
—————————————
Γ; • ⊢ A⁺ ⊃ B⁻

Γ; • ⊢ A⁻   Γ; • ⊢ B⁻
—————————————————————
   Γ; • ⊢ A⁻ ∧ B⁻</code></pre>
<p>If we have a negative connective at the top level we can decompose that further, leaving us with a strictly smaller goal. Finally, we may reach a positive proposition with nothing in Ω. In this case we focus on the right.</p>
<pre><code>  Γ ⊢ [A⁺]
———————————
 Γ; • ⊢ A⁺</code></pre>
<p>Now we’re in a position to discuss these two focused judgments. If we focus on the right we decompose positive connectives</p>
<pre><code>——————————
 Γ ⊢ [⊤⁺]

Γ; • ⊢ A⁻
—————————
Γ ⊢ ↓ A⁻

   Γ ⊢ [A⁺]
—————————————
 Γ ⊢ [A⁺ ∨ B⁺]

   Γ ⊢ [B⁺]
—————————————
 Γ ⊢ [A⁺ ∨ B⁺]

Γ ⊢ [A⁺]   Γ ⊢ [B⁺]
———————————————————
   Γ ⊢ [A⁺ ∧ B⁺]</code></pre>
<p>These judgments follow the ones we’ve already seen. If we encounter a shift, we stop focusing. Otherwise we decompose the topmost positive connective. Now looking at these, you should see that sometimes these rules we’ll lead us to a “mistake”. Imagine if we applied the 4th rule to <code>⊤ ∨ ⊥</code>! This is why these rules are segregated into a separate judgment.</p>
<p>In this judgment’s dual we essentially apply the exact same rules to the left of the turnstile and on negative connectives.</p>
<pre><code>  Γ; A⁺ ⊢ U
————————————
Γ; [↑A⁺] ⊢ U

Γ ⊢ [A⁺]   Γ; [B⁻] ⊢ U
——————————————————————
  Γ; [A⁺ ⊃ B⁻] ⊢ U

   Γ; [A⁻] ⊢ U
—————————————————
 Γ; [A⁻ ∧ B⁻] ⊢ U

   Γ; [B⁻] ⊢ U
—————————————————
 Γ; [A⁻ ∧ B⁻] ⊢ U</code></pre>
<p>That wraps up our focused system. The idea is now we have this much more limited system which can express the same things our original, unfocused system could. A computer can be easily programmed to do a focused search since there’s much less backtracking everywhere leading to fewer rules being applicable at each step. I think Pfenning has referred to this as removing most of the “don’t-care” nondeterminism from our rules.</p>
<h3 id="wrap-up">Wrap Up</h3>
<p>I’m going to wrap up the post here. Proving focalization or even something like cut elimination is quite fiddly and I have no desire at all to try to transcribe it (painfully) into markdown and get it wrong in the process.</p>
<p>Instead, now that you have some idea of what focusing is about, go read Rob Simmons’ <a href="http://www.cs.cmu.edu/~rjsimmon/drafts/focus.pdf">paper</a>. It provides a clear account of proving everything necessary prove a focused system is complete and sound with respect to its unfocused counterpart.</p>
<p>Cheers</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Mon, 27 Oct 2014 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2014-10-27-focusing.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Update on Old Projects</title>
    <link>http://jozefg.bitbucket.org/posts/2014-10-24-github.html</link>
    <description><![CDATA[<div class="info">
    Posted on October 24, 2014
    
</div>
<div class="info">
    
    Tags: <a href="/tags/haskell.html">haskell</a>, <a href="/tags/personal.html">personal</a>
    
</div>

<p>All though most people I talk to know me for my blog, I do occasionally actually write software instead of just talking about it :)</p>
<p>Sadly, as a mercurial user most of my stuff has languished with on bitbucket. I’ve had a few people tell me that this is annoying for various reasons. Yesterday, I finally got around to fixing that!</p>
<p>As of yesterday, all of my interesting projects are mirrored on [github][my-github]. I’m still using mercurial but thanks to the lovely git-hg tool this is not an issue. You can fork, pull-request, or generally peek and poke as you please. From my end all of these actions look like nice mercurial changesets so I can continue to live under my rock where I don’t need to understand Git.</p>
<p>As a quick list of what haskell code is up there now</p>
<ul>
<li><a href="http://github.com/jozefg/c-dsl">c-dsl</a></li>
<li><a href="http://github.com/jozefg/c_of_scheme">c_of_scheme</a></li>
<li><a href="http://github.com/jozefg/ds-kanren">ds-kanren</a></li>
<li><a href="http://github.com/jozefg/generic-church">generic-church</a></li>
<li><a href="http://github.com/jozefg/hasquito">hasquito</a></li>
<li><a href="http://github.com/jozefg/hlf">hlf</a></li>
<li><a href="http://github.com/jozefg/monad-gen">monad-gen</a></li>
<li><a href="http://github.com/jozefg/reified-records">reified-records</a></li>
<li><a href="http://github.com/jozefg/blog">this blog</a></li>
</ul>
<p>Which I think includes every project I’ve blogged about here as well as a few others. Sorry it took so long!</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Fri, 24 Oct 2014 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2014-10-24-github.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Notes on Quotients Types</title>
    <link>http://jozefg.bitbucket.org/posts/2014-10-17-quotients.html</link>
    <description><![CDATA[<div class="info">
    Posted on October 17, 2014
    
</div>
<div class="info">
    
    Tags: <a href="/tags/types.html">types</a>, <a href="/tags/notes.html">notes</a>
    
</div>

<p>Lately I’ve been reading a lot of type theory literature. In effort to help my future self, I’m going to jot down a few thoughts on quotient types, the subject of some recent google-fu.</p>
<h2 id="but-why">But Why!</h2>
<p>The problem quotient types are aimed at solving is actually a very common one. I’m sure at some point or another you’ve used a piece of data you’ve wanted to compare for equality. Additionally, that data properly needed some work to determine whether it was equal to another piece.</p>
<p>A simple example might would be representing rational numbers. A rational number is a fraction of two integers, so let’s just say</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb1-1" data-line-number="1">    <span class="kw">type</span> <span class="dt">Rational</span> <span class="fu">=</span> (<span class="dt">Integer</span>, <span class="dt">Integer</span>)</a></code></pre></div>
<p>Now all is well, we can define a <code>Num</code> instance and what not. But what about equality? Clearly we want equivalent fractions to be equal. That should mean that <code>(2, 4) = (1, 2)</code> since they both represent the same number.</p>
<p>Now our implementation has a sticky point, clearly this isn’t the case on its own! What we really want to say is “<code>(2, 4) = (1, 2)</code> up to trivial rejiggering”.</p>
<p>Haskell’s own <code>Rational</code> type solves this by not exposing a raw tuple. It still exists under the hood, but we only expose smart constructors that will reduce our fractions as far as possible.</p>
<p>This is displeasing from a dependently typed setting however, we want to be able to formally prove the equality of some things. This “equality modulo normalization” leaves us with a choice. Either we can really provide a function which is essentially</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb2-1" data-line-number="1">    foo <span class="ot">:</span> <span class="ot">(</span>a b <span class="ot">:</span> Rational<span class="ot">)</span></a>
<a class="sourceLine" id="cb2-2" data-line-number="2">        <span class="ot">-&gt;</span> Either <span class="ot">(</span>reduce a <span class="ot">=</span> reduce b<span class="ot">)</span> <span class="ot">(</span>reduce a /= reduce b<span class="ot">)</span></a></code></pre></div>
<p>This doesn’t really help us though, there’s no way to express that <code>a</code> should be observationally equivalent to <code>b</code>. This is a problem seemingly as old as dependent types: How can we have a simple representation of equality that captures all the structure we want and none that we don’t.</p>
<p>Hiding away the representation of rationals certainly buys us something, we can use a smart constructor to ensure things are normalized. From there we could potentially prove a (difficult) theorem which essentially states that</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb3-1" data-line-number="1">    =-with-norm <span class="ot">:</span> <span class="ot">(</span>a b c d <span class="ot">:</span> Integer<span class="ot">)</span></a>
<a class="sourceLine" id="cb3-2" data-line-number="2">                <span class="ot">-&gt;</span> a * d <span class="ot">=</span> b * c <span class="ot">-&gt;</span> mkRat a b <span class="ot">=</span> mkRat c d</a></code></pre></div>
<p>This still leaves us with some woes however, now a lot of computations become difficult to talk about since we’ve lost the helpful notion that <code>denominator o mkRat a = id</code> and similar. The lack of transparency shifts a lot of the burden of proof onto the code privy to the internal representation of the type, the only place where we know enough to prove such things.</p>
<p>Really what we want to say is “Hey, just forget about a bit of the structure of this type and just consider things to be identical up to <code>R</code>”. Where <code>R</code> is some equivalence relation, eg</p>
<ol type="1">
<li><code>a R a</code></li>
<li><code>a R b</code> implies <code>b R a</code></li>
<li><code>a R b</code> and <code>b R c</code> implies <code>a R c</code></li>
</ol>
<p>If you’re a mathematician, this should sound similar. It’s a lot like how we can take a set and partition it into equivalence classes. This operation is sometimes called “quotienting a set”.</p>
<p>For our example above, we really mean that our rational is a type quotiented by the relation <code>(a, b) R (c, d)</code> iff <code>a * c = b * d</code>.</p>
<p>Some other things that could potentially use quotienting</p>
<ul>
<li>Sets</li>
<li>Maps</li>
<li>Integers</li>
<li>Lots of Abstract Types</li>
</ul>
<p>Basically anything where we want to hide some of the implementation details that are irrelevant for their behavior.</p>
<h2 id="more-than-handwaving">More than Handwaving</h2>
<p>Now that I’ve spent some time essentially waving my hand about quotient types what are they? Clearly we need a rule that goes something like</p>
<pre><code> Γ ⊢ A type, E is an equivalence relation on A
———————————————–———————————————————————————————
        Γ ⊢ A // E type</code></pre>
<p>Along with the typing rule</p>
<pre><code>    Γ ⊢ a : A
——————————————————
  Γ ⊢ a : A // E</code></pre>
<p>So all members of the original type belong to the quotiented type, and finally</p>
<pre><code>  Γ ⊢ a : A, Γ ⊢ b : A, Γ ⊢ a E b
–——————————————–——————————————————
         Γ ⊢ a ≡ b : A // E</code></pre>
<p>Notice something important here, that <code>≡</code> is the fancy shmancy judgmental equality baked right into the language. This calls into question decidability. It seems that <code>a E b</code> could involve some non-trivial proof terms.</p>
<p>More than that, in a constructive, proof relevant setting things can be a bit trickier than they seem. We can’t just define a quotient to be the same type with a different equivalence relation, since that would imply some icky things.</p>
<p>To illustrate this problem, imagine we have a predicate <code>P</code> on a type <code>A</code> where <code>a E b</code> implies <code>P a ⇔ P b</code>. If we just redefine the equivalence relation on quotes, <code>P</code> would not be a wellformed predicate on <code>A // E</code>, since <code>a ≡ b : A // E</code> doesn’t mean that <code>P a ≡ P b</code>. This would be unfortunate.</p>
<p>Clearly some subtler treatment of this is needed. To that end I found <a href="http://www.nuprl.org/documents/Nogin/QuotientTypes_02.pdf">this paper</a> discussing some of the handling of NuRPL’s quotients enlightening.</p>
<h2 id="how-nuprl-does-it">How NuPRL Does It</h2>
<p>The paper I linked to is a discussion on how to think about quotients in terms of other type theory constructs. In order to do this we need a few things first.</p>
<p>The first thing to realize is that NuPRL’s type theory is different than what you are probably used to. We don’t have this single magical global equality. Instead, we define equality inductively across the type. This notion means that our equality judgment doesn’t have to be natural in the type it works across. It can do specific things at each case. Perhaps the most frequent is that we can have functional extensionality.</p>
<pre><code>f = g ⇔ ∀ a. f a = g a</code></pre>
<p>Okay, so now that we’ve tossed aside the notion of a single global equality, what else is new? Well something new is the lens through which many people look at NuRPL’s type theory: PER semantics. Remember that PER is a relationship satisfying</p>
<ol type="1">
<li><code>a R b → then b R a</code></li>
<li><code>a R b ∧ b R c → a R c</code></li>
</ol>
<p>In other words, a PER is an equivalence relationship that isn’t necessarily reflexive at all points.</p>
<p>The idea is to view types not as some opaque “thingy” but instead to be partial equivalence relations across the set of untyped lambda calculus terms. Inductively defined equality falls right out of this idea since we can just define <code>a ≡ b : A</code> to be equivalent to <code>(a, b) ∈ A</code>.</p>
<p>Now another problem rears it head, what does <code>a : A</code> mean? Well even though we’re dealing with PERs, but it’s quite reasonable to say something is a member of a type if it’s reflexive. That is to say each relation is a full equivalence relation for the things we call members of that type. So we can therefore define <code>a : A</code> to be <code>(a, a) ∈ A</code>.</p>
<p>Another important constraint, in order for a type family to be well formed, it needs to respect the equality of the type it maps across. In other words, for all <code>B : A → Type</code>, we have <code>(a, a') ∈ A' ⇒ (B a = B a') ∈ U</code>. This should seem on par with how we defined function equality and we call this “type functionality”.</p>
<p>Let’s all touch on another concept: squashed types. The idea is to take a type and throw away all information other than whether or not it’s occupied. There are two basic types of squashing, extensional or intensional. In the intensional we consider two squashed things equal if and only if the types they’re squashing are equal</p>
<pre><code>     A = B
  ————————————
   [A] = [B]</code></pre>
<p>Now we can also consider only the behavior of the squashed type, the extensional view. Since the only behavior of a squashed type is simply existing, our extensional squash type has the equivalence</p>
<pre><code>   ∥A∥ ⇔ ∥B∥
   ————————–
    ∥A∥ = ∥B∥</code></pre>
<p>Now aside from this, the introduction of these types are basically the same: if we can prove that a type is occupied, we can grab a squashed type. Similarly, when we eliminate a type all we get is the trivial occupant of the squashed type, called •.</p>
<pre><code>    Γ ⊢ A
   ———————
   Γ ⊢ [A]

    Γ, x : |A|, Δ[̱•] ⊢ C[̱•]
  ——————————————————————————
    Γ, x : |A|, Δ[x] ⊢ C[x]</code></pre>
<p>What’s interesting is that when proving an equality judgment, we can unsquash obth of these types. This is only because NuRPL’s equality proofs computationally trivial.</p>
<p>Now with all of that out of the way, I’d like to present two typing rules. First</p>
<pre><code>  Γ ⊢ A ≡ A&#39;;  Γ, x : A, y : A ⊢ E[x; y] = E&#39;[x; y]; E and E&#39; are PERS
  ————————————————————————————————————————————————————————————————————
                      Γ ⊢ A ‌// E ≡ A&#39; // E&#39;</code></pre>
<p>In English, two quotients are equal when the types and their quotienting relations are equal.</p>
<pre><code> Γ, u : x ≡ y ∈ (A // E), v :  ∥x E y∥, Δ[u] ⊢ C [u]
 ———————————————————————————————————————————————————–
       Γ, u : x ≡ y ∈ (A // E), Δ[u] ⊢ C [u]</code></pre>
<p>There are a few new things here. The first is that we have a new <code>Δ [u]</code> thing. This is a result of dependent types, can have things in our context that depend on <code>u</code> and so to indicate that we “split” the context, with <code>Γ, u, Δ</code> and apply the depend part of the context <code>Δ</code> to the variable it depends on <code>u</code>.</p>
<p>Now the long and short of this is that when we’re of this is that when we’re trying to use an equivalence between two terms in a quotient, we only get the squashed term. This done mean that we only need to provide a squash to get equality in the first place though</p>
<pre><code>Γ ⊢ ∥ x E y  ∥; Γ ⊢ x : A; Γ ⊢ y : A
——————————————————————————————————–
      Γ ⊢ x ≡ y : A // E</code></pre>
<p>Remember that we can trivially form an <code>∥ A ∥</code> from <code>A</code>’.</p>
<p>Now there’s just one thing left to talk about, using our quotiented types. To do this the paper outlines one primitive elimination rule and defines several others.</p>
<pre><code>Γ, x : A, y : A, e : x E y, a : ND, Δ[ndₐ{x;y}] ⊢ |C[ndₐ{x;y}]|
——————————————————————————————————————————————————————————————–
               Γ, x : A // E, Δ[x] ⊢ |C[x]|</code></pre>
<p><code>ND</code> is a admittedly odd type that’s supposed to represent nondeterministic choice. It has two terms, <code>tt</code> and <code>ff</code> and they’re considered “equal” under <code>ND</code>. However, <code>nd</code> returns its first argument if it’s fed <code>tt</code> and the second if it is fed <code>ff</code>. Hence, nondeterminism.</p>
<p>Now in our rule we use this to indicate that if we’re eliminating some quotiented type we can get <em>any</em> value that’s considered equal under <code>E</code>. We can only be assured that when we eliminate a quotiented type, it will be related by the equivalence relation to <code>x</code>. This rule captures this notion by allowing us to randomly choose some <code>y : A</code> so that <code>x E y</code>.</p>
<p>Overall, this rule simply states that if <code>C</code> is occupied for any term related to <code>x</code>, then it is occupied for <code>C[x]</code>.</p>
<h2 id="wrap-up">Wrap up</h2>
<p>As with my last post, here’s some questions for the curious reader to pursue</p>
<ul>
<li>What elimination rules can we derive from the above?</li>
<li>If we’re of proving equality can we get more expressive rules?</li>
<li>What would an extensional quotient type look like?</li>
<li>Why would we want intensional or extensional?</li>
<li>How can we express quotient types with higher inductive types from HoTT</li>
</ul>
<p>The last one is particularly interesting.</p>
<p><em>Thanks to Jon Sterling for proof reading</em></p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Fri, 17 Oct 2014 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2014-10-17-quotients.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Notes on Abstract and Existential Types</title>
    <link>http://jozefg.bitbucket.org/posts/2014-09-29-abstraction-existentials.html</link>
    <description><![CDATA[<div class="info">
    Posted on September 29, 2014
    
</div>
<div class="info">
    
    Tags: <a href="/tags/haskell.html">haskell</a>, <a href="/tags/types.html">types</a>, <a href="/tags/notes.html">notes</a>
    
</div>

<p>I’m part of a paper reading club at CMU. Last week we talked about a classic paper, <a href="http://theory.stanford.edu/~jcm/papers/mitch-plotkin-88.pdf">Abstract Types have Existential Type</a>. The concept described in this paper is interesting and straightforward. Sadly some of the notions and comparisons made in the paper are starting to show their age. I thought it might be fun to give a tldr using Haskell.</p>
<p>The basic idea is that when we have an type with an abstract implementation some functions upon it, it’s really an existential type.</p>
<h2 id="some-haskell-code">Some Haskell Code</h2>
<p>To exemplify this let’s define an abstract type (in Haskell)</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb1-1" data-line-number="1">    <span class="kw">module</span> <span class="dt">Stack</span> (<span class="dt">Stack</span>, empty, push, pop) <span class="kw">where</span></a>
<a class="sourceLine" id="cb1-2" data-line-number="2">    <span class="kw">newtype</span> <span class="dt">Stack</span> a <span class="fu">=</span> <span class="dt">Stack</span> [a]</a>
<a class="sourceLine" id="cb1-3" data-line-number="3"></a>
<a class="sourceLine" id="cb1-4" data-line-number="4"><span class="ot">    empty ::</span> <span class="dt">Stack</span> a</a>
<a class="sourceLine" id="cb1-5" data-line-number="5">    empty <span class="fu">=</span> <span class="dt">Stack</span> []</a>
<a class="sourceLine" id="cb1-6" data-line-number="6"></a>
<a class="sourceLine" id="cb1-7" data-line-number="7"><span class="ot">    push ::</span> a <span class="ot">-&gt;</span> <span class="dt">Stack</span> a <span class="ot">-&gt;</span> <span class="dt">Stack</span> a</a>
<a class="sourceLine" id="cb1-8" data-line-number="8">    push a (<span class="dt">Stack</span> xs) <span class="fu">=</span> <span class="dt">Stack</span> (a <span class="fu">:</span> xs)</a>
<a class="sourceLine" id="cb1-9" data-line-number="9"></a>
<a class="sourceLine" id="cb1-10" data-line-number="10"><span class="ot">    pop ::</span> <span class="dt">Stack</span> a <span class="ot">-&gt;</span> <span class="dt">Maybe</span> a</a>
<a class="sourceLine" id="cb1-11" data-line-number="11">    pop (<span class="dt">Stack</span> []) <span class="fu">=</span> <span class="dt">Nothing</span></a>
<a class="sourceLine" id="cb1-12" data-line-number="12">    pop (<span class="dt">Stack</span> (x <span class="fu">:</span> xs)) <span class="fu">=</span> <span class="dt">Just</span> x</a>
<a class="sourceLine" id="cb1-13" data-line-number="13"></a>
<a class="sourceLine" id="cb1-14" data-line-number="14"><span class="ot">    shift ::</span> <span class="dt">Stack</span> a <span class="ot">-&gt;</span> <span class="dt">Maybe</span> (<span class="dt">Stack</span> a)</a>
<a class="sourceLine" id="cb1-15" data-line-number="15">    shift (<span class="dt">Stack</span> []) <span class="fu">=</span> <span class="dt">Nothing</span></a>
<a class="sourceLine" id="cb1-16" data-line-number="16">    shift (<span class="dt">Stack</span> (x <span class="fu">:</span> xs)) <span class="fu">=</span> <span class="dt">Just</span> (<span class="dt">Stack</span> xs)</a></code></pre></div>
<p>Now we could import this module and use its operations:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb2-1" data-line-number="1">    <span class="kw">import</span> <span class="dt">Stack</span></a>
<a class="sourceLine" id="cb2-2" data-line-number="2"></a>
<a class="sourceLine" id="cb2-3" data-line-number="3">    main <span class="fu">=</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb2-4" data-line-number="4">      <span class="kw">let</span> s <span class="fu">=</span> push <span class="dv">1</span> <span class="fu">.</span> push <span class="dv">2</span> <span class="fu">.</span> push <span class="dv">3</span> <span class="fu">$</span> empty</a>
<a class="sourceLine" id="cb2-5" data-line-number="5">      print (pop s)</a></code></pre></div>
<p>What we couldn’t do however, is pattern match on stacks to take advantage of its internal structure. We can only build new operations out of combinations of the exposed API. The classy terminology would be to say that <code>Stack</code> is abstract.</p>
<p>This is all well and good, but what does it <em>mean</em> type theoretically? If we want to represent Haskell as a typed calculus it’d be a shame to have to include Haskell’s (under powered) module system to talk about abstract types.</p>
<p>After all, we’re not really thinking about modules as so much as hiding some details. That sounds like something our type system should be able to handle without having to rope in modules. By isolating the concept of abstraction in our type system, we might be able to more deeply understand and reason about code that uses abstract types.</p>
<p>This is in fact quite possible, let’s rephrase our definition of <code>Stack</code></p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb3-1" data-line-number="1">    <span class="kw">module</span> <span class="dt">Stack</span> (<span class="dt">Stack</span>, <span class="dt">StackOps</span>(<span class="fu">..</span>), ops) <span class="kw">where</span></a>
<a class="sourceLine" id="cb3-2" data-line-number="2"></a>
<a class="sourceLine" id="cb3-3" data-line-number="3">    <span class="kw">newtype</span> <span class="dt">Stack</span> a <span class="fu">=</span> <span class="dt">Stack</span> [a]</a>
<a class="sourceLine" id="cb3-4" data-line-number="4"></a>
<a class="sourceLine" id="cb3-5" data-line-number="5">    <span class="kw">data</span> <span class="dt">StackOps</span> a <span class="fu">=</span> <span class="dt">StackOps</span> {<span class="ot"> empty ::</span> <span class="dt">Stack</span> a</a>
<a class="sourceLine" id="cb3-6" data-line-number="6">                               ,<span class="ot"> push  ::</span> a <span class="ot">-&gt;</span> <span class="dt">Stack</span> a <span class="ot">-&gt;</span> <span class="dt">Stack</span> a</a>
<a class="sourceLine" id="cb3-7" data-line-number="7">                               ,<span class="ot"> pop   ::</span> <span class="dt">Stack</span> a <span class="ot">-&gt;</span> <span class="dt">Maybe</span> a</a>
<a class="sourceLine" id="cb3-8" data-line-number="8">                               ,<span class="ot"> shift ::</span> <span class="dt">Stack</span> a <span class="ot">-&gt;</span> <span class="dt">Maybe</span> (<span class="dt">Stack</span> a) }</a>
<a class="sourceLine" id="cb3-9" data-line-number="9"><span class="ot">    ops ::</span> <span class="dt">StackOps</span></a>
<a class="sourceLine" id="cb3-10" data-line-number="10">    ops <span class="fu">=</span> <span class="fu">...</span></a></code></pre></div>
<p>Now that we’ve lumped all of our operations into one record, our module is really only exports a type name, and a record of data. We could take a step further still,</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" data-line-number="1">    <span class="kw">module</span> <span class="dt">Stack</span> (<span class="dt">Stack</span>, <span class="dt">StackOps</span>(<span class="fu">..</span>), ops) <span class="kw">where</span></a>
<a class="sourceLine" id="cb4-2" data-line-number="2"></a>
<a class="sourceLine" id="cb4-3" data-line-number="3">    <span class="kw">newtype</span> <span class="dt">Stack</span> a <span class="fu">=</span> <span class="dt">Stack</span> [a]</a>
<a class="sourceLine" id="cb4-4" data-line-number="4"></a>
<a class="sourceLine" id="cb4-5" data-line-number="5">    <span class="kw">data</span> <span class="dt">StackOps</span> s a <span class="fu">=</span> <span class="dt">StackOps</span> {<span class="ot"> empty ::</span> s a</a>
<a class="sourceLine" id="cb4-6" data-line-number="6">                                 ,<span class="ot"> push  ::</span> a <span class="ot">-&gt;</span> s a <span class="ot">-&gt;</span> s a</a>
<a class="sourceLine" id="cb4-7" data-line-number="7">                                 ,<span class="ot"> pop   ::</span> s a <span class="ot">-&gt;</span> <span class="dt">Maybe</span> a</a>
<a class="sourceLine" id="cb4-8" data-line-number="8">                                 ,<span class="ot"> shift ::</span> s a <span class="ot">-&gt;</span> <span class="dt">Maybe</span> (s a) }</a>
<a class="sourceLine" id="cb4-9" data-line-number="9"><span class="ot">    ops ::</span> <span class="dt">StackOps</span> <span class="dt">Stack</span></a>
<a class="sourceLine" id="cb4-10" data-line-number="10">    ops <span class="fu">=</span> <span class="fu">...</span></a></code></pre></div>
<p>Now the only thing that needs to know the internals of <code>Stack</code>. It seems like we could really just smush the definition into <code>ops</code>, why should the rest of the file see our private definition.</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb5-1" data-line-number="1">    <span class="kw">module</span> <span class="dt">Stack</span> (<span class="dt">StackOps</span>(<span class="fu">..</span>), ops) <span class="kw">where</span></a>
<a class="sourceLine" id="cb5-2" data-line-number="2"></a>
<a class="sourceLine" id="cb5-3" data-line-number="3">    <span class="kw">data</span> <span class="dt">StackOps</span> s a <span class="fu">=</span> <span class="dt">StackOps</span> {<span class="ot"> empty ::</span> s a</a>
<a class="sourceLine" id="cb5-4" data-line-number="4">                                 ,<span class="ot"> push  ::</span> a <span class="ot">-&gt;</span> s a <span class="ot">-&gt;</span> s a</a>
<a class="sourceLine" id="cb5-5" data-line-number="5">                                 ,<span class="ot"> pop   ::</span> s a <span class="ot">-&gt;</span> <span class="dt">Maybe</span> a</a>
<a class="sourceLine" id="cb5-6" data-line-number="6">                                 ,<span class="ot"> shift ::</span> s a <span class="ot">-&gt;</span> <span class="dt">Maybe</span> (s a) }</a>
<a class="sourceLine" id="cb5-7" data-line-number="7"><span class="ot">    ops ::</span> <span class="dt">StackOps</span> <span class="fu">???</span></a>
<a class="sourceLine" id="cb5-8" data-line-number="8">    ops <span class="fu">=</span> <span class="fu">...</span></a></code></pre></div>
<p>Now what should we fill in <code>???</code> with? It’s some type, but it’s meant to be chosen by the callee, not the caller. Does that sound familiar? Existential types to the rescue!</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb6-1" data-line-number="1">    <span class="ot">{-# LANGUAGE PolyKinds, KindSignatures, ExistentialQuantification #-}</span></a>
<a class="sourceLine" id="cb6-2" data-line-number="2">    <span class="kw">module</span> <span class="dt">Stack</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb6-3" data-line-number="3"></a>
<a class="sourceLine" id="cb6-4" data-line-number="4">    <span class="kw">data</span> <span class="dt">Packed</span> (<span class="ot">f ::</span> k <span class="ot">-&gt;</span> k&#39; <span class="ot">-&gt;</span> <span class="fu">*</span>) a <span class="fu">=</span> forall s<span class="fu">.</span> <span class="dt">Pack</span> (f s a)</a>
<a class="sourceLine" id="cb6-5" data-line-number="5"></a>
<a class="sourceLine" id="cb6-6" data-line-number="6">    <span class="kw">data</span> <span class="dt">StackOps</span> s a <span class="fu">=</span> <span class="dt">StackOps</span> {<span class="ot"> empty ::</span> s a</a>
<a class="sourceLine" id="cb6-7" data-line-number="7">                                 ,<span class="ot"> push  ::</span> a <span class="ot">-&gt;</span> s a <span class="ot">-&gt;</span> s a</a>
<a class="sourceLine" id="cb6-8" data-line-number="8">                                 ,<span class="ot"> pop   ::</span> s a <span class="ot">-&gt;</span> <span class="dt">Maybe</span> a</a>
<a class="sourceLine" id="cb6-9" data-line-number="9">                                 ,<span class="ot"> shift ::</span> s a <span class="ot">-&gt;</span> <span class="dt">Maybe</span> (s a) }</a>
<a class="sourceLine" id="cb6-10" data-line-number="10"><span class="ot">    ops ::</span> <span class="dt">Packed</span> <span class="dt">StackOps</span></a>
<a class="sourceLine" id="cb6-11" data-line-number="11">    ops <span class="fu">=</span> <span class="dt">Pack</span> <span class="fu">...</span></a></code></pre></div>
<p>The key difference here is <code>Packed</code>. It lets us take a type function and instantiate it with some type variable and hide our choice from the user. This means that we can even drop the whole <code>newtype</code> from the implementation of <code>ops</code></p>
<div class="sourceCode" id="cb7"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb7-1" data-line-number="1"><span class="ot">    ops ::</span> <span class="dt">Packed</span> <span class="dt">StackOps</span></a>
<a class="sourceLine" id="cb7-2" data-line-number="2">    ops <span class="fu">=</span> <span class="dt">Pack</span> <span class="fu">$</span> <span class="dt">StackOps</span> { empty <span class="fu">=</span> []</a>
<a class="sourceLine" id="cb7-3" data-line-number="3">                          , push  <span class="fu">=</span> (<span class="fu">:</span>)</a>
<a class="sourceLine" id="cb7-4" data-line-number="4">                          , pop   <span class="fu">=</span> fmap fst <span class="fu">.</span> uncons</a>
<a class="sourceLine" id="cb7-5" data-line-number="5">                          , shift <span class="fu">=</span> fmap snd <span class="fu">.</span> uncons }</a>
<a class="sourceLine" id="cb7-6" data-line-number="6">      <span class="kw">where</span> uncons [] <span class="fu">=</span> <span class="dt">Nothing</span></a>
<a class="sourceLine" id="cb7-7" data-line-number="7">            uncons (x <span class="fu">:</span> xs) <span class="fu">=</span> <span class="dt">Just</span> (x, xs)</a></code></pre></div>
<p>Now that we’ve eliminated the <code>Stack</code> definition from the top level, we can actually just drop the notion that this is in a separate module.</p>
<p>One thing that strikes me as unpleasant is how <code>Packed</code> is defined, we must jump through some hoops to support <code>StackOps</code> being polymorphic in two arguments, not just one.</p>
<p>We could get around this with higher rank polymorphism and making the <em>fields</em> more polymorphic while making the type less so. We could also just wish for type level lambdas or something. Even some of the recent type level lens stuff could be aimed at making a general case definition of <code>Packed</code>.</p>
<p>From the client side this definition isn’t actually so unpleasant to use either.</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb8-1" data-line-number="1">    <span class="ot">{-# LANGUAGE RecordWildCards #-}</span></a>
<a class="sourceLine" id="cb8-2" data-line-number="2"></a>
<a class="sourceLine" id="cb8-3" data-line-number="3"><span class="ot">    someAdds ::</span> <span class="dt">Packed</span> <span class="dt">Stack</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">Maybe</span> <span class="dt">Int</span></a>
<a class="sourceLine" id="cb8-4" data-line-number="4">    someAdds (<span class="dt">Pack</span> <span class="dt">Stack</span>{<span class="fu">..</span>}) <span class="fu">=</span> pop (push <span class="dv">1</span> empty)</a></code></pre></div>
<p>With record wild cards, there’s very little boilerplate to introduce our record into scope. Now we might wonder about using a specific instance rather than abstracting over all possible instantiations.</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb9-1" data-line-number="1"><span class="ot">    someAdds ::</span> <span class="dt">Packed</span> <span class="dt">Stack</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">Maybe</span> <span class="dt">Int</span></a>
<a class="sourceLine" id="cb9-2" data-line-number="2">    someAdds <span class="fu">=</span></a>
<a class="sourceLine" id="cb9-3" data-line-number="3">      <span class="kw">let</span> (<span class="dt">Pack</span> <span class="dt">Stack</span>{<span class="fu">..</span>}) <span class="fu">=</span> ops <span class="kw">in</span></a>
<a class="sourceLine" id="cb9-4" data-line-number="4">        pop (push <span class="dv">1</span> empty)</a></code></pre></div>
<p>The resulting error message is amusing :)</p>
<p>Now we might wonder if we gain anything concrete from this. Did all those language extensions actually do something useful?</p>
<p>Well one mechanical transformation we can make is that we can change our existential type into a CPS-ed higher rank type.</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb10-1" data-line-number="1"><span class="ot">    unpackPacked ::</span> (forall s<span class="fu">.</span> f s a <span class="ot">-&gt;</span> r) <span class="ot">-&gt;</span> <span class="dt">Packed</span> f a <span class="ot">-&gt;</span> r</a>
<a class="sourceLine" id="cb10-2" data-line-number="2">    unpackPacked cont (<span class="dt">Pack</span> f) <span class="fu">=</span> cont f</a>
<a class="sourceLine" id="cb10-3" data-line-number="3"></a>
<a class="sourceLine" id="cb10-4" data-line-number="4"><span class="ot">    someAdds&#39; ::</span> <span class="dt">Stack</span> s <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">Maybe</span> <span class="dt">Int</span></a>
<a class="sourceLine" id="cb10-5" data-line-number="5">    someAdds&#39; <span class="dt">Stack</span>{<span class="fu">..</span>} <span class="fu">=</span> pop (push <span class="dv">1</span> empty)</a>
<a class="sourceLine" id="cb10-6" data-line-number="6"></a>
<a class="sourceLine" id="cb10-7" data-line-number="7"><span class="ot">    someAdds ::</span> <span class="dt">Packed</span> <span class="dt">Stack</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">Maybe</span> <span class="dt">Int</span></a>
<a class="sourceLine" id="cb10-8" data-line-number="8">    someAdds <span class="fu">=</span> unpackPacked someAdds&#39;</a></code></pre></div>
<p>Now we’ve factored out the unpacking of existentials into a function called <code>unpack</code>. This takes a continuation which is parametric in the existential variable, <code>s</code>.</p>
<p>Now our body of <code>someAdds</code> becomes <code>someAdds</code>, but notice something very interesting here, now <code>s</code> is a normal universally quantified type variable. This means we can apply some nice properties we already have used, eg parametricity.</p>
<p>This is a nice effect of translating things to core constructs, all the tools we already have figured out can suddenly be applied.</p>
<h2 id="wrap-up">Wrap Up</h2>
<p>Now that we’ve gone through transforming our abstract types in existential ones you can final appreciate at least one more thing: the subtitle on <a href="http://existentialtype.wordpress.com/">Bob Harper’s blog</a>. You can’t say you didn’t learn something useful :)</p>
<p>I wanted to keep this post short and sweet. In doing this I’m going to some of the more interesting questions we could ask. For the curious reader, I leave you with these</p>
<ul>
<li>How can we use type classes to prettify our examples?</li>
<li>What can we do to generalize <code>Packed</code>?</li>
<li>How does this pertain to modules? Higher order modules?</li>
<li>How would you implement “sharing constraints” in this model?</li>
<li>What happens when we translate existentials to dependent products?</li>
</ul>
<p>Cheers.</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Mon, 29 Sep 2014 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2014-09-29-abstraction-existentials.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Introduction to Dependent Types: Off, Off to Agda Land</title>
    <link>http://jozefg.bitbucket.org/posts/2014-09-21-what-are-dep-types-2.html</link>
    <description><![CDATA[<div class="info">
    Posted on September 21, 2014
    
</div>
<div class="info">
    
    Tags: <a href="/tags/agda.html">agda</a>, <a href="/tags/types.html">types</a>
    
</div>

<p><em>First, an apology. Sorry this has take so long to push out. I’ve just started my first semester at Carnegie Mellon. I fully intend to keep blogging, but it’s taken a little while to get my feet under me. Happy readings :)</em></p>
<p>In this second post of my “intro to dependent types” series we’re going on a whirlwind tour of Agda. Specifically we’re going to look at translating our faux-Haskell from the last post into honest to goodness typecheckable Agda.</p>
<p>There are 2 main reasons to go through the extra work of using a real language rather than pseudo-code</p>
<ol type="1">
<li>This is typecheckable. I can make sure that all the i’s are dotted and t’s crossed.</li>
<li>It’s a lot cleaner We’re only using the core of Agda so it’s more or less a very stripped down Haskell with a much more expressive but simpler type system.</li>
</ol>
<p>With that in mind let’s dive in!</p>
<h3 id="whats-the-same">What’s the Same</h3>
<p>There’s quite a bit of shared syntax between Agda and Haskell, so a Haskeller can usually guess what’s going on.</p>
<p>In Agda we still give definitions in much the same way (single <code>:</code> though)</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb1-1" data-line-number="1">    thingy <span class="ot">:</span> Bool</a>
<a class="sourceLine" id="cb1-2" data-line-number="2">    thingy <span class="ot">=</span> true</a></code></pre></div>
<p>where as in Haskell we’d say</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb2-1" data-line-number="1"><span class="ot">    name ::</span> <span class="dt">Type</span></a>
<a class="sourceLine" id="cb2-2" data-line-number="2">    name <span class="fu">=</span> val</a></code></pre></div>
<p>In fact, we even get Haskell’s nice syntactic sugar for functions.</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb3-1" data-line-number="1">    function <span class="ot">:</span> A <span class="ot">-&gt;</span> B <span class="ot">-&gt;</span> <span class="ot">...</span> <span class="ot">-&gt;</span> C</a>
<a class="sourceLine" id="cb3-2" data-line-number="2">    function a b <span class="ot">...</span> <span class="ot">=</span> c</a></code></pre></div>
<p>Will desugar to a lambda.</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb4-1" data-line-number="1">    function <span class="ot">:</span> A <span class="ot">-&gt;</span> B <span class="ot">-&gt;</span> <span class="ot">...</span> <span class="ot">-&gt;</span> C</a>
<a class="sourceLine" id="cb4-2" data-line-number="2">    function <span class="ot">=</span> <span class="ot">\</span>a b <span class="ot">...</span> <span class="ot">-&gt;</span> c</a></code></pre></div>
<p>One big difference between Haskell and Agda is that, due to Agda’s more expressive type system, type inference is woefully undecidable. Those top level signatures are not optional sadly. Some DT language work a little harder than Agda when it comes to inference, but for a beginner this is a bit of a feature: you learn what the actual (somewhat scary) types are.</p>
<p>And of course, you always give type signatures in Haskell I’m sure :)</p>
<p>Like Haskell function application is whitespace and functions are curried</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb5-1" data-line-number="1">    <span class="co">-- We could explicitly add parens</span></a>
<a class="sourceLine" id="cb5-2" data-line-number="2">    <span class="co">-- foo : A -&gt; (B -&gt; C)</span></a>
<a class="sourceLine" id="cb5-3" data-line-number="3">    foo <span class="ot">:</span> A <span class="ot">-&gt;</span> B <span class="ot">-&gt;</span> C</a>
<a class="sourceLine" id="cb5-4" data-line-number="4">    foo <span class="ot">=</span> <span class="ot">...</span></a>
<a class="sourceLine" id="cb5-5" data-line-number="5"></a>
<a class="sourceLine" id="cb5-6" data-line-number="6">    a <span class="ot">:</span> A</a>
<a class="sourceLine" id="cb5-7" data-line-number="7">    a <span class="ot">=</span> <span class="ot">...</span></a>
<a class="sourceLine" id="cb5-8" data-line-number="8"></a>
<a class="sourceLine" id="cb5-9" data-line-number="9">    bar <span class="ot">:</span> B <span class="ot">-&gt;</span> C</a>
<a class="sourceLine" id="cb5-10" data-line-number="10">    bar <span class="ot">=</span> foo a</a></code></pre></div>
<p>Even the data type declarations should look familiar, they’re just like GADTs syntactically.</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb6-1" data-line-number="1">    <span class="kw">data</span> Bool <span class="ot">:</span> <span class="dt">Set</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb6-2" data-line-number="2">      true  <span class="ot">:</span> Bool</a>
<a class="sourceLine" id="cb6-3" data-line-number="3">      false <span class="ot">:</span> Bool</a></code></pre></div>
<p>Notice that we have this new <code>Set</code> thing lurking in our code. <code>Set</code> is just the kind of normal types, like <code>*</code> in Haskell. In Agda there’s actually an infinite tower of these <code>Bool : Set : Set1 : Set2 ...</code>, but won’t concern ourselves with anything beyond <code>Set</code>. It’s also worth noting that Agda doesn’t require any particular casing for constructors, traditionally they’re lower case.</p>
<p>Pattern matching in Agda is pretty much identical to Haskell. We can define something like</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb7-1" data-line-number="1">    not <span class="ot">:</span> Bool <span class="ot">-&gt;</span> Bool</a>
<a class="sourceLine" id="cb7-2" data-line-number="2">    not true  <span class="ot">=</span> false</a>
<a class="sourceLine" id="cb7-3" data-line-number="3">    not false <span class="ot">=</span> true</a></code></pre></div>
<p>One big difference between Haskell and Agda is that pattern matching <strong>must</strong> be exhaustive. Nonexhaustiveness is a compiler error in Agda.</p>
<p>This brings me to another point worth mentioning. Remember that structural induction I mentioned the other day? Agda only allows recursion when the terms we recurse on are “smaller”.</p>
<p>In other words, all Agda functions are defined by structural induction. This together with the exhaustiveness restriction means that Agda programs are “total”. In other words all Agda programs reduce to a single value, they never crash or loop forever.</p>
<p>This can occasionally cause pain though since not all recursive functions are modelled nicely by structural induction! A classic example is merge sort. The issue is that in merge sort we want to say something like</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb8-1" data-line-number="1">    mergeSort <span class="ot">:</span> List Nat <span class="ot">-&gt;</span> List Nat</a>
<a class="sourceLine" id="cb8-2" data-line-number="2">    mergeSort [] <span class="ot">=</span> []</a>
<a class="sourceLine" id="cb8-3" data-line-number="3">    mergeSort <span class="ot">(</span>x :: []<span class="ot">)</span> <span class="ot">=</span> x :: []</a>
<a class="sourceLine" id="cb8-4" data-line-number="4">    mergeSort xs <span class="ot">=</span> <span class="kw">let</span> <span class="ot">(</span>l, r<span class="ot">)</span> <span class="ot">=</span> split xs <span class="kw">in</span></a>
<a class="sourceLine" id="cb8-5" data-line-number="5">                     merge <span class="ot">(</span>mergeSort l, mergeSort r<span class="ot">)</span></a></code></pre></div>
<p>But wait, how would the typechecker know that <code>l</code> and <code>r</code> are strictly smaller than <code>xs</code>? In fact, they might not be! We know that the length of <code>length xs &gt; 1</code>, but convincing the typechecker of that fact is a pain! In fact, without elaborate trickery, Agda will reject this definition.</p>
<p>So, apart from these restriction for totality Agda has pretty much been a stripped down Haskell. Let’s start seeing what Agda offers over Haskell.</p>
<h3 id="dependent-types">Dependent Types</h3>
<p>There wouldn’t be much point in writing Agda if it didn’t have dependent types. In fact the two mechanisms that comprise our dependent types translate wonderfully into Agda.</p>
<p>First we had pi types, remember those?</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb9-1" data-line-number="1"><span class="ot">    foo ::</span> (<span class="ot">a ::</span> <span class="dt">A</span>) <span class="ot">-&gt;</span> <span class="dt">B</span></a>
<a class="sourceLine" id="cb9-2" data-line-number="2">    foo a <span class="fu">=</span> <span class="fu">...</span></a></code></pre></div>
<p>Those translate almost precisely into Agda, where we’d write</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb10-1" data-line-number="1">    foo <span class="ot">:</span> <span class="ot">(</span>a <span class="ot">:</span> A<span class="ot">)</span> <span class="ot">-&gt;</span> B</a></code></pre></div>
<p>The only difference is the colons! In fact, Agda’s pi types are far more general than what we’d discussed previously. The extra generality comes from what we allow <code>A</code> to be. In our previous post, <code>A</code> was always some normal type with the kind <code>*</code> (<code>Set</code> in Agda). In Agda though, we allow <code>A</code> to be <code>Set</code> itself. In Haskell syntax that would be something like</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb11-1" data-line-number="1"><span class="ot">    foo ::</span> (<span class="ot">a ::</span> <span class="fu">*</span>) <span class="ot">-&gt;</span> <span class="dt">B</span></a></code></pre></div>
<p>What could <code>a</code> be then? Well anything with the kind <code>*</code> is a type, like <code>Bool</code>, <code>()</code>, or <code>Nat</code>. So that <code>a</code> is like a normal type variable in Haskell</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb12-1" data-line-number="1"><span class="ot">    foo ::</span> forall a<span class="fu">.</span> <span class="dt">B</span></a></code></pre></div>
<p>In fact, when we generalize pi types like this, they generalize parametric polymorphism. This is kind of like how we use “big lambdas” in System F to write out polymorphism explicitly.</p>
<p>Here’s a definition for the identity function in Agda.</p>
<div class="sourceCode" id="cb13"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb13-1" data-line-number="1">    id <span class="ot">:</span> <span class="ot">(</span>A <span class="ot">:</span> <span class="dt">Set</span><span class="ot">)</span> <span class="ot">-&gt;</span> A <span class="ot">-&gt;</span> A</a>
<a class="sourceLine" id="cb13-2" data-line-number="2">    id A a <span class="ot">=</span> a</a></code></pre></div>
<p>This is how we actually do all parametric polymorphism in Agda, as a specific use of pi types. This comes from the idea that types are also “first class”. We can pass them around and use them as arguments to functions, even dependent arguments :)</p>
<p>Now our other dependently typed mechanism was our generalized generalized algebraic data types. These also translate nicely to Agda.</p>
<div class="sourceCode" id="cb14"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb14-1" data-line-number="1">    <span class="kw">data</span> Foo <span class="ot">:</span> Bool <span class="ot">-&gt;</span> <span class="dt">Set</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb14-2" data-line-number="2">      Bar <span class="ot">:</span> Foo True</a></code></pre></div>
<p>We indicate that we’re going to index our data on something the same way we would in Haskell++, by adding it to the type signature on the top of the data declaration.</p>
<p>Agda’s GGADTs also allow us to us to add “parameters” instead of indices. These are things which the data type may use, but each constructor handles uniformly without inspecting it.</p>
<p>For example a list type depends on the type of it’s elements, but it doesn’t poke further at the type or value of those elements. They’re handled “parametrically”.</p>
<p>In Agda a list would be defined as</p>
<div class="sourceCode" id="cb15"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb15-1" data-line-number="1">    <span class="kw">data</span> List <span class="ot">(</span>A <span class="ot">:</span> <span class="dt">Set</span><span class="ot">)</span> <span class="ot">:</span> <span class="dt">Set</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb15-2" data-line-number="2">      nil  <span class="ot">:</span> List A</a>
<a class="sourceLine" id="cb15-3" data-line-number="3">      cons <span class="ot">:</span> A <span class="ot">-&gt;</span> List A <span class="ot">-&gt;</span> List A</a></code></pre></div>
<p>If your wondering what on earth the difference is, don’t worry! You’ve already in fact used parametric/non-parametric type arguments in Haskell. In Haskell a normal algebraic type can just take several type variables and can’t try to do clever things depending on what the argument is. For example, our definition of lists</p>
<div class="sourceCode" id="cb16"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb16-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">List</span> a <span class="fu">=</span> <span class="dt">Cons</span> a (<span class="dt">List</span> a) <span class="fu">|</span> <span class="dt">Nil</span></a></code></pre></div>
<p>can’t do something different if <code>a</code> is <code>Int</code> instead of <code>Bool</code> or something like that. That’s not the case with GADTs though, there we can do clever things like</p>
<div class="sourceCode" id="cb17"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb17-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">List</span><span class="ot"> ::</span> <span class="fu">*</span> <span class="ot">-&gt;</span> <span class="fu">*</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb17-2" data-line-number="2">      <span class="dt">IntOnlyCons</span><span class="ot"> ::</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">List</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">List</span> <span class="dt">Int</span></a>
<a class="sourceLine" id="cb17-3" data-line-number="3">      <span class="fu">...</span></a></code></pre></div>
<p>Now we’re not treating our type argument opaquely, we can figure things out about it depending on what constructor our value uses! That’s the core of the difference between parameters in indices in Agda.</p>
<p>Next let’s talk about modules. Agda’s prelude is absolutely tiny. By tiny I mean essentially non-existant. Because of this I’m using the Agda standard library heavily and to import something in Agda we’d write</p>
<pre><code>import Foo.Bar.Baz</code></pre>
<p>This isn’t the same as a Haskell import though. By default, imports in Agda import a qualified name to use. To get a Haskell style import we’ll use the special shortcut</p>
<pre><code>open import Foo.Bar</code></pre>
<p>which is short for</p>
<pre><code>import Foo.Bar
open Bar</code></pre>
<p>Because Agda’s prelude is so tiny we’ll have to import things like booleans, numbers, and unit. These are all things defined in the standard library, not even the core language. Expect any Agda code we write to make heavy use of the standard library and begin with a lot of imports.</p>
<p>Finally, Agda’s names are somewhat.. unique. Agda and it’s standard library are unicode heavy, meaning that instead of unit we’d type ⊤ and instead of <code>Void</code> we’d use ⊥. Which is pretty nifty, but it does take some getting used to. If you’re familiar with LaTeX, the Emacs mode for Agda allows LaTeX style entry. For example ⊥ can be entered as <code>\bot</code>.</p>
<p>The most common unicode name we’ll use is ℕ. This is just the type of natural numbers as their defined in <code>Data.Nat</code>.</p>
<h3 id="a-few-examples">A Few Examples</h3>
<p>Now that we’ve seen what dependent types look like in Agda, let’s go over a few examples of their use.</p>
<p>First let’s import a few things</p>
<div class="sourceCode" id="cb21"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb21-1" data-line-number="1">    <span class="kw">open</span> <span class="kw">import</span> Data<span class="ot">.</span>Nat</a>
<a class="sourceLine" id="cb21-2" data-line-number="2">    <span class="kw">open</span> <span class="kw">import</span> Data<span class="ot">.</span>Bool</a></code></pre></div>
<p>Now we can define a few simple Agda functions just to get a feel for how that looks.</p>
<div class="sourceCode" id="cb22"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb22-1" data-line-number="1">    not <span class="ot">:</span> Bool <span class="ot">-&gt;</span> Bool</a>
<a class="sourceLine" id="cb22-2" data-line-number="2">    not true  <span class="ot">=</span> false</a>
<a class="sourceLine" id="cb22-3" data-line-number="3">    not false <span class="ot">=</span> true</a>
<a class="sourceLine" id="cb22-4" data-line-number="4"></a>
<a class="sourceLine" id="cb22-5" data-line-number="5">    and <span class="ot">:</span> Bool <span class="ot">-&gt;</span> Bool <span class="ot">-&gt;</span> Bool</a>
<a class="sourceLine" id="cb22-6" data-line-number="6">    and true b  <span class="ot">=</span> b</a>
<a class="sourceLine" id="cb22-7" data-line-number="7">    and false <span class="ot">_</span> <span class="ot">=</span> false</a>
<a class="sourceLine" id="cb22-8" data-line-number="8"></a>
<a class="sourceLine" id="cb22-9" data-line-number="9">    or <span class="ot">:</span> Bool <span class="ot">-&gt;</span> Bool <span class="ot">-&gt;</span> Bool</a>
<a class="sourceLine" id="cb22-10" data-line-number="10">    or false b <span class="ot">=</span> b</a>
<a class="sourceLine" id="cb22-11" data-line-number="11">    or true  <span class="ot">_</span> <span class="ot">=</span> true</a></code></pre></div>
<p>As you can see defining functions is mostly identical to Haskell, we just pattern match and the top level and go from there.</p>
<p>We can define recursive functions just like in Haskell</p>
<pre class="adga"><code>    plus : ℕ -&gt; ℕ -&gt; ℕ
    plus (suc n) m = suc (plus n m)
    plus zero    m = m</code></pre>
<p>Now with Agda we can use our data types to encode “proofs” of sorts.</p>
<p>For example</p>
<div class="sourceCode" id="cb24"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb24-1" data-line-number="1">    <span class="kw">data</span> IsEven <span class="ot">:</span> ℕ <span class="ot">-&gt;</span> <span class="dt">Set</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb24-2" data-line-number="2">      even-z <span class="ot">:</span> IsEven zero</a>
<a class="sourceLine" id="cb24-3" data-line-number="3">      even-s  <span class="ot">:</span> <span class="ot">(</span>n <span class="ot">:</span> Nat<span class="ot">)</span> <span class="ot">-&gt;</span> IsEven n <span class="ot">-&gt;</span> IsEven <span class="ot">(</span>suc <span class="ot">(</span>suc n<span class="ot">))</span></a></code></pre></div>
<p>Now this inductively defines what it means for a natural number to be even so that if <code>Even n</code> exists then <code>n</code> must be even. We can also state oddness</p>
<div class="sourceCode" id="cb25"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb25-1" data-line-number="1">    <span class="kw">data</span> IsOdd <span class="ot">:</span> ℕ <span class="ot">-&gt;</span> <span class="dt">Set</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb25-2" data-line-number="2">      odd-o <span class="ot">:</span> IsOdd <span class="ot">(</span>suc zero<span class="ot">)</span></a>
<a class="sourceLine" id="cb25-3" data-line-number="3">      odd-s <span class="ot">:</span> <span class="ot">(</span>n <span class="ot">:</span> ℕ<span class="ot">)</span> <span class="ot">-&gt;</span> IsOdd n <span class="ot">-&gt;</span> IsOdd <span class="ot">(</span>suc <span class="ot">(</span>suc n<span class="ot">))</span></a></code></pre></div>
<p>Now we can construct a decision procedure which produces either a proof of evenness or oddness for all natural numbers.</p>
<div class="sourceCode" id="cb26"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb26-1" data-line-number="1">    <span class="kw">open</span> <span class="kw">import</span> Data<span class="ot">.</span>Sum <span class="co">-- The same thing as Either in Haskell; ⊎ is just Either</span></a>
<a class="sourceLine" id="cb26-2" data-line-number="2"></a>
<a class="sourceLine" id="cb26-3" data-line-number="3">    evenOrOdd <span class="ot">:</span> <span class="ot">(</span>n <span class="ot">:</span> ℕ<span class="ot">)</span> <span class="ot">-&gt;</span> Odd n ⊎ Even n</a></code></pre></div>
<p>So we’re setting out to construct a function that, given any <code>n</code>, builds up an appropriate term showing it is either even or odd.</p>
<p>The first two cases of this function are kinda the base cases of this recurrence.</p>
<div class="sourceCode" id="cb27"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb27-1" data-line-number="1">    evenOrOdd zero <span class="ot">=</span> inj₁ even-z</a>
<a class="sourceLine" id="cb27-2" data-line-number="2">    evenOrOdd <span class="ot">(</span>suc zero<span class="ot">)</span> <span class="ot">=</span> inj₂ odd-o</a></code></pre></div>
<p>So if we’re given zero or one, return the base case of <code>IsEven</code> or <code>IsOdd</code> as appropriate. Notice that instead of <code>Left</code> or <code>Right</code> as constructors we have <code>inj₁</code> and <code>inj₂</code>. They serve exactly the same purpose, just with a shinier unicode name.</p>
<p>Now our next step would be to handle the case where we have</p>
<div class="sourceCode" id="cb28"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb28-1" data-line-number="1">    evenOrOdd <span class="ot">(</span>suc <span class="ot">(</span>suc n<span class="ot">))</span> <span class="ot">=</span> ?</a></code></pre></div>
<p>Our code is going to be like the Haskell code</p>
<div class="sourceCode" id="cb29"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb29-1" data-line-number="1">    <span class="kw">case</span> evenOrOdd n <span class="kw">of</span></a>
<a class="sourceLine" id="cb29-2" data-line-number="2">      <span class="dt">Left</span> evenProof <span class="ot">-&gt;</span> <span class="dt">Left</span> (<span class="dt">EvenS</span> evenProof)</a>
<a class="sourceLine" id="cb29-3" data-line-number="3">      <span class="dt">Right</span> oddProof <span class="ot">-&gt;</span> <span class="dt">Right</span> (<span class="dt">OddS</span>  oddProof)</a></code></pre></div>
<p>In words, we’ll recurse and inspect the result, if we get an even proof we’ll build a bigger even proof and if we can an odd proof we’ll build a bigger odd proof.</p>
<p>In Agda we’ll use the <code>with</code> keyword. This allows us to “extend” the current pattern matching by adding an expression to the list of expressions we’re pattern matching on.</p>
<div class="sourceCode" id="cb30"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb30-1" data-line-number="1">    evenOrOdd <span class="ot">(</span>suc <span class="ot">(</span>suc n<span class="ot">))</span> <span class="kw">with</span> evenOrOdd n</a>
<a class="sourceLine" id="cb30-2" data-line-number="2">    evenOrOdd <span class="ot">(</span>suc <span class="ot">(</span>suc n<span class="ot">))</span> <span class="ot">|</span> inj₁ x <span class="ot">=</span> ?</a>
<a class="sourceLine" id="cb30-3" data-line-number="3">    evenOrOdd <span class="ot">(</span>suc <span class="ot">(</span>suc n<span class="ot">))</span> <span class="ot">|</span> inj₂ y <span class="ot">=</span> ?</a></code></pre></div>
<p>Now we add our new expression to use for matching by saying <code>... with evenOrOdd n</code>. Then we list out the next set of possible patterns.</p>
<p>From here the rest of the function is quite straightforward.</p>
<div class="sourceCode" id="cb31"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb31-1" data-line-number="1">    evenOrOdd <span class="ot">(</span>suc <span class="ot">(</span>suc n<span class="ot">))</span> <span class="ot">|</span> inj₁ x <span class="ot">=</span> inj₁ <span class="ot">(</span>even-s n x<span class="ot">)</span></a>
<a class="sourceLine" id="cb31-2" data-line-number="2">    evenOrOdd <span class="ot">(</span>suc <span class="ot">(</span>suc n<span class="ot">))</span> <span class="ot">|</span> inj₂ y <span class="ot">=</span> inj₂ <span class="ot">(</span>odd-s n y<span class="ot">)</span></a></code></pre></div>
<p>Notice that we had to duplicate the whole <code>evenOrOdd (suc (suc n))</code> bit of the match? It’s a bit tedious so Agda provides some sugar. If we replace that portion of the match with <code>...</code> Agda will just automatically reuse the pattern we had when we wrote <code>with</code>.</p>
<p>Now our whole function looks like</p>
<div class="sourceCode" id="cb32"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb32-1" data-line-number="1">    evenOrOdd <span class="ot">:</span> <span class="ot">(</span>n <span class="ot">:</span> ℕ<span class="ot">)</span> <span class="ot">-&gt;</span> IsEven n ⊎ IsOdd n</a>
<a class="sourceLine" id="cb32-2" data-line-number="2">    evenOrOdd zero <span class="ot">=</span> inj₁ even-z</a>
<a class="sourceLine" id="cb32-3" data-line-number="3">    evenOrOdd <span class="ot">(</span>suc zero<span class="ot">)</span> <span class="ot">=</span> inj₂ odd-o</a>
<a class="sourceLine" id="cb32-4" data-line-number="4">    evenOrOdd <span class="ot">(</span>suc <span class="ot">(</span>suc n<span class="ot">))</span> <span class="kw">with</span> evenOrOdd n</a>
<a class="sourceLine" id="cb32-5" data-line-number="5">    <span class="ot">...</span> <span class="ot">|</span> inj₁ x <span class="ot">=</span> inj₁ <span class="ot">(</span>even-s n x<span class="ot">)</span></a>
<a class="sourceLine" id="cb32-6" data-line-number="6">    <span class="ot">...</span> <span class="ot">|</span> inj₂ y <span class="ot">=</span> inj₂ <span class="ot">(</span>odd-s n y<span class="ot">)</span></a></code></pre></div>
<p>How can we improve this? Well notice that that <code>suc (suc n)</code> case involved unpacking our <code>Either</code> and than immediately repacking it, this looks like something we can abstract over.</p>
<div class="sourceCode" id="cb33"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb33-1" data-line-number="1">    bimap <span class="ot">:</span> <span class="ot">(</span>A B C D <span class="ot">:</span> <span class="dt">Set</span><span class="ot">)</span> <span class="ot">-&gt;</span> <span class="ot">(</span>A <span class="ot">-&gt;</span> C<span class="ot">)</span> <span class="ot">-&gt;</span> <span class="ot">(</span>B <span class="ot">-&gt;</span> D<span class="ot">)</span> <span class="ot">-&gt;</span> A ⊎ B <span class="ot">-&gt;</span> C ⊎ D</a>
<a class="sourceLine" id="cb33-2" data-line-number="2">    bimap A B C D f g <span class="ot">(</span>inj₁ x<span class="ot">)</span> <span class="ot">=</span> inj₁ <span class="ot">(</span>f x<span class="ot">)</span></a>
<a class="sourceLine" id="cb33-3" data-line-number="3">    bimap A B C D f g <span class="ot">(</span>inj₂ y<span class="ot">)</span> <span class="ot">=</span> inj₂ <span class="ot">(</span>g y<span class="ot">)</span></a></code></pre></div>
<p>If we gave <code>bimap</code> a more Haskellish siganture</p>
<div class="sourceCode" id="cb34"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb34-1" data-line-number="1"><span class="ot">    bimap ::</span> forall a b c d<span class="fu">.</span> (a <span class="ot">-&gt;</span> c) <span class="ot">-&gt;</span> (b <span class="ot">-&gt;</span> d) <span class="ot">-&gt;</span> <span class="dt">Either</span> a b <span class="ot">-&gt;</span> <span class="dt">Either</span> c d</a></code></pre></div>
<p>One interesting point to notice is that the <em>type</em> arguments in the Agda function (<code>A</code> and <code>B</code>) also appeared in the normal argument pattern! This is because we’re using the normal pi type mechanism for parametric polymorphism, so we’ll actually end up explicitly passing and receiving the types we quantify over. This messed with me quite a bit when I first starting learning DT languages, take a moment and convince yourself that this makes sense.</p>
<p>Now that we have <code>bimap</code>, we can use it to simplify our <code>evenOrOdd</code> function.</p>
<div class="sourceCode" id="cb35"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb35-1" data-line-number="1">    evenOrOdd <span class="ot">:</span> <span class="ot">(</span>n <span class="ot">:</span> ℕ<span class="ot">)</span> <span class="ot">-&gt;</span> IsEven n ⊎ IsOdd n</a>
<a class="sourceLine" id="cb35-2" data-line-number="2">    evenOrOdd zero <span class="ot">=</span> inj₁ even-z</a>
<a class="sourceLine" id="cb35-3" data-line-number="3">    evenOrOdd <span class="ot">(</span>suc zero<span class="ot">)</span> <span class="ot">=</span> inj₂ odd-o</a>
<a class="sourceLine" id="cb35-4" data-line-number="4">    evenOrOdd <span class="ot">(</span>suc <span class="ot">(</span>suc n<span class="ot">))</span> <span class="ot">=</span></a>
<a class="sourceLine" id="cb35-5" data-line-number="5">      bimap <span class="ot">(</span>IsEven n<span class="ot">)</span> <span class="ot">(</span>IsOdd n<span class="ot">)</span></a>
<a class="sourceLine" id="cb35-6" data-line-number="6">            <span class="ot">(</span>IsEven <span class="ot">(</span>suc <span class="ot">(</span>suc n<span class="ot">)))</span> <span class="ot">(</span>IsOdd <span class="ot">(</span>suc <span class="ot">(</span>suc n<span class="ot">)))</span></a>
<a class="sourceLine" id="cb35-7" data-line-number="7">            <span class="ot">(</span>even-s n<span class="ot">)</span> <span class="ot">(</span>odd-s n<span class="ot">)</span> <span class="ot">(</span>evenOrOdd n<span class="ot">)</span></a></code></pre></div>
<p>We’ve gotten rid of the explicit <code>with</code>, but at the cost of all those explicit type arguments! Those are both gross and obvious. Agda can clearly deduce what <code>A</code>, <code>B</code>, <code>C</code> and <code>D</code> should be from the arguments and what the return type must be. In fact, Agda provides a convenient mechanism for avoiding this boilerplate. If we simply insert <code>_</code> in place of an argument, Agda will try to guess it from the information it has about the other arguments and contexts. Since these type arguments are so clear from context, Agda can guess them all</p>
<div class="sourceCode" id="cb36"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb36-1" data-line-number="1">    evenOrOdd <span class="ot">:</span> <span class="ot">(</span>n <span class="ot">:</span> ℕ<span class="ot">)</span> <span class="ot">-&gt;</span> IsEven n ⊎ IsOdd n</a>
<a class="sourceLine" id="cb36-2" data-line-number="2">    evenOrOdd zero <span class="ot">=</span> inj₁ even-z</a>
<a class="sourceLine" id="cb36-3" data-line-number="3">    evenOrOdd <span class="ot">(</span>suc zero<span class="ot">)</span> <span class="ot">=</span> inj₂ odd-o</a>
<a class="sourceLine" id="cb36-4" data-line-number="4">    evenOrOdd <span class="ot">(</span>suc <span class="ot">(</span>suc n<span class="ot">))</span> <span class="ot">=</span></a>
<a class="sourceLine" id="cb36-5" data-line-number="5">      bimap <span class="ot">_</span> <span class="ot">_</span> <span class="ot">_</span> <span class="ot">_</span> <span class="ot">(</span>even-s n<span class="ot">)</span> <span class="ot">(</span>odd-s n<span class="ot">)</span> <span class="ot">(</span>evenOrOdd n<span class="ot">)</span></a></code></pre></div>
<p>Now at least the code fits on one line! This also raises something interesting, the types are so strict that Agda can actually figure out parts of our programs for us! I’m not sure about you but at this point in time my brain mostly melted :) Because of this I’ll try to avoid using <code>_</code> and other mechanisms for Agda writing programs for us where I can. The exception of course being situations like the above where it’s necessary for readabilities sake.</p>
<p>One important exception to that rule is for parameteric polymorphism. It’s a royal pain to pass around types explicitly everywhere. We’re going to use an Agda feature called “implicit arguments”. You should think of these as arguments for which the <code>_</code> is inserted for it. So instead of writing</p>
<div class="sourceCode" id="cb37"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb37-1" data-line-number="1">    foo <span class="ot">_</span> zero zero</a></code></pre></div>
<p>We could write</p>
<div class="sourceCode" id="cb38"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb38-1" data-line-number="1">    foo zero zero</a></code></pre></div>
<p>This more closely mimicks what Haskell does for its parametric polymorphism. To indicate we want something to be an implicit argument, we just wrap it in <code>{}</code> instead of <code>()</code>. So for example, we could rewrite <code>bimap</code> as</p>
<div class="sourceCode" id="cb39"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb39-1" data-line-number="1">    bimap <span class="ot">:</span> <span class="ot">{</span>A B C D <span class="ot">:</span> <span class="dt">Set</span><span class="ot">}</span> <span class="ot">-&gt;</span> <span class="ot">(</span>A <span class="ot">-&gt;</span> C<span class="ot">)</span> <span class="ot">-&gt;</span> <span class="ot">(</span>B <span class="ot">-&gt;</span> D<span class="ot">)</span> <span class="ot">-&gt;</span> A ⊎ B <span class="ot">-&gt;</span> C ⊎ D</a>
<a class="sourceLine" id="cb39-2" data-line-number="2">    bimap f g <span class="ot">(</span>inj₁ x<span class="ot">)</span> <span class="ot">=</span> inj₁ <span class="ot">(</span>f x<span class="ot">)</span></a>
<a class="sourceLine" id="cb39-3" data-line-number="3">    bimap f g <span class="ot">(</span>inj₂ y<span class="ot">)</span> <span class="ot">=</span> inj₂ <span class="ot">(</span>g y<span class="ot">)</span></a></code></pre></div>
<p>To avoid all those underscores.</p>
<p>Another simple function we’ll write is that if we can construct an <code>IsOdd n</code>, we can build an <code>IsEven (suc n)</code>.</p>
<div class="sourceCode" id="cb40"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb40-1" data-line-number="1">    oddSuc <span class="ot">:</span> <span class="ot">(</span>n <span class="ot">:</span> ℕ<span class="ot">)</span> <span class="ot">-&gt;</span> IsOdd n <span class="ot">-&gt;</span> IsEven <span class="ot">(</span>suc n<span class="ot">)</span></a></code></pre></div>
<p>Now this function has two arguments, a number and a term showing that that number is odd. To write this function we’ll actually recurse on the <code>IsOdd</code> term.</p>
<div class="sourceCode" id="cb41"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb41-1" data-line-number="1">    oddSuc <span class="ot">.</span><span class="dv">1</span> odd-o <span class="ot">=</span> even-s zero even-z</a>
<a class="sourceLine" id="cb41-2" data-line-number="2">    oddSuc <span class="ot">.(</span>suc <span class="ot">(</span>suc n<span class="ot">))</span> <span class="ot">(</span>odd-s n p<span class="ot">)</span> <span class="ot">=</span> even-s <span class="ot">(</span>suc n<span class="ot">)</span> <span class="ot">(</span>oddSuc n p<span class="ot">)</span></a></code></pre></div>
<p>Now if we squint hard and ignore those <code>.</code> terms, this looks much like we’d expect. We build the <code>Even</code> starting from <code>even-s zero even-z</code>. From there we just recurse and talk on a <code>even-s</code> constructor to scale the <code>IsEven</code> term up by two.</p>
<p>There’s a weird thing going on here though, those <code>.</code> patterns. Those are a nifty little idea in Agda that pattern matching on one thing might <em>force</em> another term to be some value. If we know that our <code>IsOdd n</code> is <code>odd-o</code> <code>n</code> <strong>must</strong> be <code>suc zero</code>. Anything else would just be completely incorrect. To notate these patterns Agda forces you to prefix them with <code>.</code>. You should read <code>.Y</code> as “because of X, this <em>must</em> be Y”.</p>
<p>This isn’t an optional choice though, as <code>.</code> patterns may do several wonky things. The most notable is that they often use pattern variables nonlinearly, notice that <code>n</code> appeared twice in our second pattern clause. Without the <code>.</code> this would be very illegal.</p>
<p>As an exercise to the reader, try to write</p>
<div class="sourceCode" id="cb42"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb42-1" data-line-number="1">    evenSuc <span class="ot">:</span> <span class="ot">(</span>n <span class="ot">:</span> ℕ<span class="ot">)</span> <span class="ot">-&gt;</span> IsEven n <span class="ot">-&gt;</span> IsOdd <span class="ot">(</span>suc n<span class="ot">)</span></a></code></pre></div>
<h3 id="wrap-up">Wrap Up</h3>
<p>That wraps up this post which came out much longer than I expected. We’ve now covered enough basics to actually discuss meaningful dependently typed programs. That’s right, we can finally kiss natural numbers good bye in the next post!</p>
<p>Next time we’ll cover writing a small program but interesting program and use dependent types to assure ourselves of it’s correctness.</p>
<p>As always, please comment with any questions :)</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Sun, 21 Sep 2014 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2014-09-21-what-are-dep-types-2.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Introduction to Dependent Types: Haskell on Steroids</title>
    <link>http://jozefg.bitbucket.org/posts/2014-08-25-dep-types-part-1.html</link>
    <description><![CDATA[<div class="info">
    Posted on August 25, 2014
    
</div>
<div class="info">
    
    Tags: <a href="/tags/haskell.html">haskell</a>, <a href="/tags/types.html">types</a>
    
</div>

<p>I’d like to start another series of blog posts. This time on something that I’ve wanted to write about for a while, dependent types.</p>
<p>There’s a noticeable lack of accessible materials introducing dependent types at a high level aimed at functional programmers. That’s what this series sets out help fill. Therefore, if you’re a Haskell programmer and don’t understand something, it’s a bug! Please comment so I can help make this a more useful resource for you :)</p>
<p>There are four parts to this series, each answering one question</p>
<ol type="1">
<li>What are dependent types?</li>
<li>What does a dependently typed language look like?</li>
<li>What does it feel like to write programs with dependent types?</li>
<li>What does it mean to “prove” something?</li>
</ol>
<p>So first things first, what are dependent types? Most people by now have heard the unhelpful quick answer</p>
<blockquote>
<p>A dependent type is a type that depends on a value, not just other types.</p>
</blockquote>
<p>But that’s not helpful! What does this actually look like? To try to understand this we’re going to write some Haskell code that pushes us as close as we can get to dependent types in Haskell.</p>
<h2 id="kicking-ghc-in-the-teeth">Kicking GHC in the Teeth</h2>
<p>Let’s start with the flurry of extensions we need</p>
<pre><code>{-# LANGUAGE DataKinds            #-}
{-# LANGUAGE KindSignatures       #-}
{-# LANGUAGE GADTs                #-}
{-# LANGUAGE TypeFamilies         #-}
{-# LANGUAGE UndecidableInstances #-}</code></pre>
<p>Now our first definition is a standard formulation of natural numbers</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb2-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">Nat</span> <span class="fu">=</span> <span class="dt">Z</span> <span class="fu">|</span> <span class="dt">S</span> <span class="dt">Nat</span></a></code></pre></div>
<p>Here <code>Z</code> represents 0 and <code>S</code> means <code>+ 1</code>. So you should read <code>S Z</code> as 1, <code>S (S Z)</code> as 2 and so on and so on.</p>
<p>If you’re having some trouble, this function to convert an <code>Int</code> to a <code>Nat</code> might help</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb3-1" data-line-number="1">    <span class="co">-- Naively assume n &gt;= 0</span></a>
<a class="sourceLine" id="cb3-2" data-line-number="2"><span class="ot">    toNat ::</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">Nat</span></a>
<a class="sourceLine" id="cb3-3" data-line-number="3">    toNat <span class="dv">0</span> <span class="fu">=</span> <span class="dt">Z</span></a>
<a class="sourceLine" id="cb3-4" data-line-number="4">    toNat n <span class="fu">=</span> <span class="dt">S</span> (toNat <span class="fu">$</span> n <span class="fu">-</span> <span class="dv">1</span>)</a></code></pre></div>
<p>We can use this definition to formulate addition</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" data-line-number="1"><span class="ot">    plus ::</span> <span class="dt">Nat</span> <span class="ot">-&gt;</span> <span class="dt">Nat</span> <span class="ot">-&gt;</span> <span class="dt">Nat</span></a>
<a class="sourceLine" id="cb4-2" data-line-number="2">    plus <span class="dt">Z</span> n     <span class="fu">=</span> n</a>
<a class="sourceLine" id="cb4-3" data-line-number="3">    plus (<span class="dt">S</span> n) m <span class="fu">=</span> <span class="dt">S</span> (plus n m)</a></code></pre></div>
<p>This definition proceeds by “structural induction”. That’s a scary word that pops up around dependent types. It’s not all that complicated, all that it means is that we use recursion only on <em>strictly smaller terms</em>.</p>
<p>There is a way to formally define smaller, if a term is a constructor applied to several (recursive) arguments. Any argument to the constructor is strictly smaller than the original terms. In a strict language if we restrict ourselves to only structural recursion we’re guaranteed that our function will terminate. This isn’t quite the case in Haskell since we have infinite structures.</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb5-1" data-line-number="1"><span class="ot">    toInt ::</span> <span class="dt">Nat</span> <span class="ot">-&gt;</span> <span class="dt">Int</span></a>
<a class="sourceLine" id="cb5-2" data-line-number="2">    toInt (<span class="dt">S</span> n) <span class="fu">=</span> <span class="dv">1</span> <span class="fu">+</span> toInt n</a>
<a class="sourceLine" id="cb5-3" data-line-number="3">    toInt <span class="dt">Z</span>     <span class="fu">=</span> <span class="dv">0</span></a>
<a class="sourceLine" id="cb5-4" data-line-number="4"></a>
<a class="sourceLine" id="cb5-5" data-line-number="5">    bigNumber <span class="fu">=</span> <span class="dt">S</span> bigNumber</a>
<a class="sourceLine" id="cb5-6" data-line-number="6"></a>
<a class="sourceLine" id="cb5-7" data-line-number="7">    main <span class="fu">=</span> print (toInt bigNumber) <span class="co">-- Uh oh!</span></a></code></pre></div>
<p>Often people will cheerfully ignore this part of Haskell when talking about reasoning with Haskell and I’ll stick to that tradition (for now).</p>
<p>Now back to the matter at hand. Since our definition of <code>Nat</code> is quite straightforward, it get’s promoted to the <a href="/posts/2014-02-10-types-kinds-and-sorts.html">kind</a> level by <code>DataKinds</code>.</p>
<p>Now we can “reflect” values back up to this new kind with a second GADTed definition of natural numbers.</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb6-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">RNat</span><span class="ot"> ::</span> <span class="dt">Nat</span> <span class="ot">-&gt;</span> <span class="fu">*</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb6-2" data-line-number="2">      <span class="dt">RZ</span><span class="ot"> ::</span> <span class="dt">RNat</span> <span class="dt">Z</span></a>
<a class="sourceLine" id="cb6-3" data-line-number="3">      <span class="dt">RS</span><span class="ot"> ::</span> <span class="dt">RNat</span> n <span class="ot">-&gt;</span> <span class="dt">RNat</span> (<span class="dt">S</span> n)</a></code></pre></div>
<p>Now, let’s precisely specify the somewhat handwavy term “reflection”. I’m using it in the imprecise sense meaning that we’ve lifted a value into something isomorphic at the type level. Later we’ll talk about reflection precisely mean lifting a value into the type level. That’s currently not possible since we can’t have values in our types!</p>
<p>What on earth could that be useful for? Well with this we can do something fancy with the definition of addition.</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb7-1" data-line-number="1">    <span class="kw">type</span> family <span class="dt">Plus</span> n<span class="ot"> m ::</span> <span class="dt">Nat</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb7-2" data-line-number="2">      <span class="dt">Plus</span> <span class="dt">Z</span> n     <span class="fu">=</span> n</a>
<a class="sourceLine" id="cb7-3" data-line-number="3">      <span class="dt">Plus</span> (<span class="dt">S</span> n) m <span class="fu">=</span> <span class="dt">S</span> (<span class="dt">Plus</span> n m)</a></code></pre></div>
<p>Now we’ve reflected our definition of addition to the type family. More than that, what we’ve written above is fairly obviously correct. We can now force our value level definition of addition to respect this type family</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb8-1" data-line-number="1"><span class="ot">    plus&#39; ::</span> <span class="dt">RNat</span> n <span class="ot">-&gt;</span> <span class="dt">RNat</span> m <span class="ot">-&gt;</span> <span class="dt">RNat</span> (<span class="dt">Plus</span> n m)</a>
<a class="sourceLine" id="cb8-2" data-line-number="2">    plus&#39; <span class="dt">RZ</span> n     <span class="fu">=</span> n</a>
<a class="sourceLine" id="cb8-3" data-line-number="3">    plus&#39; (<span class="dt">RS</span> n) m <span class="fu">=</span> <span class="dt">RS</span> (plus&#39; n m)</a></code></pre></div>
<p>Now if we messed up this definition we’d get a type error!</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb9-1" data-line-number="1"><span class="ot">    plus&#39; ::</span> <span class="dt">RNat</span> n <span class="ot">-&gt;</span> <span class="dt">RNat</span> m <span class="ot">-&gt;</span> <span class="dt">RNat</span> (<span class="dt">Plus</span> n m)</a>
<a class="sourceLine" id="cb9-2" data-line-number="2">    plus&#39; <span class="dt">RZ</span> n     <span class="fu">=</span> n</a>
<a class="sourceLine" id="cb9-3" data-line-number="3">    plus&#39; (<span class="dt">RS</span> n) m <span class="fu">=</span> plus&#39; n m <span class="co">-- Unification error! n ~ S n</span></a></code></pre></div>
<p>Super! We know have types that express strict guarantees about our program. But how useable is this?</p>
<p>To put it to the test, let’s try to write some code that reads to integers for standard input and prints their sum.</p>
<p>We can easily do this with our normal <code>plus</code></p>
<div class="sourceCode" id="cb10"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb10-1" data-line-number="1"><span class="ot">    readNat ::</span> <span class="dt">IO</span> <span class="dt">Nat</span></a>
<a class="sourceLine" id="cb10-2" data-line-number="2">    readNat <span class="fu">=</span> toNat <span class="fu">&lt;$&gt;</span> readLn</a>
<a class="sourceLine" id="cb10-3" data-line-number="3"></a>
<a class="sourceLine" id="cb10-4" data-line-number="4"><span class="ot">    main ::</span> <span class="dt">IO</span> ()</a>
<a class="sourceLine" id="cb10-5" data-line-number="5">    main <span class="fu">=</span> plus <span class="fu">&lt;$&gt;</span> readNat <span class="fu">&lt;*&gt;</span> readNat</a></code></pre></div>
<p>Easy as pie! But what about <code>RNat</code>, how can we convert a <code>Nat</code> to an <code>RNat</code>? Well we could try something with type classes I guess</p>
<pre><code>class Reify a where
  type N
  reify :: a -&gt; RNat N</code></pre>
<p>But wait, that doesn’t work since we can only have once instance for all <code>Nat</code>s. What if we did the opposite</p>
<pre><code>class Reify (n :: Nat) where
  nat :: RNat n -&gt; Nat</code></pre>
<p>This let’s us go in the other direction.. but that doesn’t help us! In fact there’s no obvious way to propagate runtime values back into the types. We’re stuck.</p>
<h2 id="ghc-with-iron-dentures">GHC with Iron Dentures</h2>
<p>Now, if we could add some magical extension to GHC could we write something like above program? Yes of course! The key idea is to not reflect up our types with data kinds, but rather just allow the values to exist in the types on their own.</p>
<p>For these I propose two basic ideas</p>
<ol type="1">
<li>A special reflective function type</li>
<li>Lifting expressions into types</li>
</ol>
<p>For our special function types, we allow the return <em>type</em> to use the supplied <em>value</em>. These are called pi types. We’ll give this the following syntax</p>
<pre><code>(x :: A) -&gt; B x</code></pre>
<p>Where <code>A :: *</code> and <code>B :: A -&gt; *</code> are some sort of type. Notice that that <code>A</code> in <code>B</code>’s kind isn’t the data kind promoted version, but just the goodness to honest normal value.</p>
<p>Now in order to allow <code>B</code> to actually make use of it’s supplied value, our second idea let’s normal types be indexed on values! Just like how GADTs can be indexed on types. We’ll call these GGADTs.</p>
<p>So let’s define a new version of <code>RNat</code></p>
<div class="sourceCode" id="cb14"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb14-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">RNat</span><span class="ot"> ::</span> <span class="dt">Nat</span> <span class="ot">-&gt;</span> <span class="fu">*</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb14-2" data-line-number="2">      <span class="dt">RZ</span><span class="ot"> ::</span> <span class="dt">RNat</span> <span class="dt">Z</span></a>
<a class="sourceLine" id="cb14-3" data-line-number="3">      <span class="dt">RS</span><span class="ot"> ::</span> <span class="dt">RNat</span> n <span class="ot">-&gt;</span> <span class="dt">RNat</span> (<span class="dt">S</span> n)</a></code></pre></div>
<p>This looks exactly like what we had before, but our semantics are different now. Those <code>Z</code>’s and <code>S</code>’s are meant to represent actual values, not members of some kind. There’s no promoting types to singleton kinds anymore, just plain old values being held in fancier types.</p>
<p>Because we can depend on normal values, we don’t even have to use our simple custom natural numbers.</p>
<div class="sourceCode" id="cb15"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb15-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">RInt</span><span class="ot"> ::</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="fu">*</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb15-2" data-line-number="2">      <span class="dt">RZ</span><span class="ot"> ::</span> <span class="dt">RInt</span> <span class="dv">0</span></a>
<a class="sourceLine" id="cb15-3" data-line-number="3">      <span class="dt">RS</span><span class="ot"> ::</span> <span class="dt">RInt</span> n <span class="ot">-&gt;</span> <span class="dt">RInt</span> (<span class="dv">1</span> <span class="fu">+</span> n)</a></code></pre></div>
<p>Notice that we allowed our types to call functions, like <code>+</code>. This can potentially be undecidable, something that we’ll address later.</p>
<p>Now we can write our function with a combination of these two ideas</p>
<div class="sourceCode" id="cb16"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb16-1" data-line-number="1"><span class="ot">    toRInt ::</span> (<span class="ot">n ::</span> <span class="dt">Int</span>) <span class="ot">-&gt;</span> <span class="dt">RInt</span> n</a>
<a class="sourceLine" id="cb16-2" data-line-number="2">    toRInt <span class="dv">0</span> <span class="fu">=</span> <span class="dt">RZ</span></a>
<a class="sourceLine" id="cb16-3" data-line-number="3">    toRInt n <span class="fu">=</span> <span class="dt">RS</span> (toRInt <span class="fu">$</span> n <span class="fu">-</span> <span class="dv">1</span>)</a></code></pre></div>
<p>Notice how we used pi types to change the return type dependent on the input <em>value</em>. Now we can feed this any old value, including ones we read from standard input.</p>
<div class="sourceCode" id="cb17"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb17-1" data-line-number="1">    main <span class="fu">=</span> print <span class="fu">.</span> toInt <span class="fu">$</span> plus&#39; <span class="fu">&lt;$&gt;</span> fmap toRInt readLn <span class="fu">&lt;*&gt;</span> fmap toRInt readLn</a></code></pre></div>
<p>Now, one might wonder how the typechecker could possibly know how to handle such things, after all how could it know what’ll be read from stdin!</p>
<p>The answer is that it doesn’t. When a value is reflected to the type level we can’t do anything with it. For example, if we had a type like</p>
<div class="sourceCode" id="cb18"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb18-1" data-line-number="1">    (<span class="ot">n ::</span> <span class="dt">Int</span>) <span class="ot">-&gt;</span> (<span class="kw">if</span> n <span class="fu">==</span> <span class="dv">0</span> <span class="kw">then</span> <span class="dt">Bool</span> <span class="kw">else</span> ())</a></code></pre></div>
<p>Then we would have to pattern match on <code>n</code> at the value level to propagate information about <code>n</code> back to the type level.</p>
<p>If we did something like</p>
<div class="sourceCode" id="cb19"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb19-1" data-line-number="1"><span class="ot">    foo ::</span> (<span class="ot">n ::</span> <span class="dt">Int</span>) <span class="ot">-&gt;</span> (<span class="kw">if</span> n <span class="fu">==</span> <span class="dv">0</span> <span class="kw">then</span> <span class="dt">Bool</span> <span class="kw">else</span> ())</a>
<a class="sourceLine" id="cb19-2" data-line-number="2">    foo n <span class="fu">=</span> <span class="kw">case</span> n <span class="kw">of</span></a>
<a class="sourceLine" id="cb19-3" data-line-number="3">      <span class="dv">0</span> <span class="ot">-&gt;</span> <span class="dt">True</span></a>
<a class="sourceLine" id="cb19-4" data-line-number="4">      _ <span class="ot">-&gt;</span> ()</a></code></pre></div>
<p>Then the typechecker would see that we’re matching on <code>n</code>, so if we get into the <code>0 -&gt; ...</code> branch then <code>n</code> must be <code>0</code>. It can then reduce the return type to <code>if 0 == 0 then Bool else ()</code> and finally <code>Bool</code>. A very important thing to note here is that the typechecker <em>doesn’t evaluate the program</em>. It’s examining the function in isolation of all other values. This means we sometimes have to hold its hand to ensure that it can figure out that all branches have the correct type.</p>
<p>This means that when we use pi types we often have to pattern match on our arguments in order to help the typechecker figure out what’s going on.</p>
<p>To make this clear, let’s play the typechecker for this function. I’m reverting to the <code>Nat</code> type since it’s nicer for pattern matching.</p>
<div class="sourceCode" id="cb20"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb20-1" data-line-number="1"><span class="ot">    toRNat ::</span> (<span class="ot">n ::</span> <span class="dt">Nat</span>) <span class="ot">-&gt;</span> <span class="dt">RNat</span> n</a>
<a class="sourceLine" id="cb20-2" data-line-number="2">    toRNat <span class="dt">Z</span> <span class="fu">=</span> <span class="dt">RZ</span> <span class="co">-- We know that n is `Z` in this branch</span></a>
<a class="sourceLine" id="cb20-3" data-line-number="3">    toRNat (<span class="dt">S</span> n) <span class="fu">=</span> <span class="dt">RS</span> (toRNat n <span class="co">{- This has the type RNat n&#39; -}</span>)</a>
<a class="sourceLine" id="cb20-4" data-line-number="4"></a>
<a class="sourceLine" id="cb20-5" data-line-number="5"><span class="ot">    p ::</span> (<span class="ot">n ::</span> <span class="dt">Nat</span>) <span class="ot">-&gt;</span> (<span class="ot">m ::</span> <span class="dt">Int</span>) <span class="ot">-&gt;</span> <span class="dt">RNat</span> (plus n m)</a>
<a class="sourceLine" id="cb20-6" data-line-number="6">    p <span class="dt">Z</span> m     <span class="fu">=</span> toRNat m</a>
<a class="sourceLine" id="cb20-7" data-line-number="7">    p (<span class="dt">S</span> n) m <span class="fu">=</span> <span class="dt">RS</span> (toRNat n m)</a></code></pre></div>
<p>First the type checker goes through <code>toRNat</code>.</p>
<p>In the first branch we have <code>n</code> equals <code>Z</code>, so <code>RZ</code> trivially typechecks. Next we have the case <code>S n</code>.</p>
<ul>
<li>We know that <code>toRNat n</code> has the type <code>RNat n'</code> by induction</li>
<li>We also know that <code>S n' = n</code>.</li>
<li>Therefore <code>RS</code> builds us a term of type <code>RNat n</code>.</li>
</ul>
<p>Now for <code>p</code>. We start in much the same manner.</p>
<p>if we enter the <code>p Z m</code> case</p>
<ul>
<li>we know that <code>n</code> is <code>Z</code>.</li>
<li>we can reduce <code>plus n m</code> since <code>plus Z m</code> is by definition equal to <code>m</code> Look at the definition of <code>plus</code> to confirm this).</li>
<li>We know how to produce <code>RNat m</code> easily since we have a function <code>toRNat :: (n :: Nat) -&gt; RNat n</code>.</li>
<li>We can apply this to <code>m</code> and the resulting term has the type <code>RNat m</code>.</li>
</ul>
<p>In the <code>RS</code> case we know that we’re trying to produce a term of type <code>RNat (plus (S n) m)</code>.</p>
<ul>
<li>Now since we know that the constructor for the first argument of <code>plus</code>, we can reduce <code>plus (S n) m</code> to <code>S (plus n m)</code> by the definition of <code>plus</code>.</li>
<li>We’re looking to build a term of type <code>plus n m</code> and that’s as simple as a recursive call.</li>
<li>From here we just need to apply <code>RS</code> to give us <code>S (plus n m)</code></li>
<li>As we previously noted <code>S (plus n m)</code> is equal to <code>plus (S n) m</code></li>
</ul>
<p>Notice how as we stepped through this as the typechecker we never needed to do any arbitrary reductions. We only ever reduce definitions when we have the outer constructor (WHNF) of one of the arguments.</p>
<p>While I’m not actually proposing adding <code>{-# LANGUAGE PiTypes #-}</code> to GHC, it’s clear that with only a few orthogonal editions to system F we can get some seriously cool types.</p>
<h2 id="wrap-up">Wrap Up</h2>
<p>Believe or not we’ve just gone through two of the most central concepts in dependent types</p>
<ul>
<li>Indexed type families (GGADTs)</li>
<li>Dependent function types (Pi types)</li>
</ul>
<p>Not so bad was it? :) From here we’ll look in the next post how to translate our faux Haskell into actual Agda code. From there we’ll go through a few more detailed examples of pi types and GGADTs by poking through some of the Agda standard library.</p>
<p>Thanks for reading, I must run since I’m late for class. It’s an FP class ironically enough.</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Mon, 25 Aug 2014 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2014-08-25-dep-types-part-1.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Equality is Hard</title>
    <link>http://jozefg.bitbucket.org/posts/2014-08-06-equality.html</link>
    <description><![CDATA[<div class="info">
    Posted on August  6, 2014
    
</div>
<div class="info">
    
    Tags: <a href="/tags/types.html">types</a>
    
</div>

<p>Equality seems like one of the simplest things to talk about in a theorem prover. After all, the notion of equality is something any small child can intuitively grasp. The sad bit is, while it’s quite easy to hand-wave about, how equality is formalized seems to be a rather complex topic.</p>
<p>In this post I’m going to attempt to cover a few of the main different means of “equality proofs” or identity types and the surrounding concepts. I’m opting for a slightly more informal approach in the hopes of covering more ground.</p>
<h2 id="definitional-equality">Definitional Equality</h2>
<p>This is not really an equality type per say, but it’s worth stating explicitly what definitional equality is since I must refer to it several times throughout this post.</p>
<p>Two terms <code>A</code> and <code>B</code> are definitional equal is a judgment notated</p>
<pre><code>Γ ⊢ A ≡ B</code></pre>
<p>This is <em>not</em> a user level proof but rather a primitive, untyped judgment in the meta-theory of the language itself. The typing rules of the language will likely include a rule along the lines of</p>
<pre><code>Γ ⊢ A ≡ B, Γ ⊢ x : A
————————————————————–
     Γ ⊢ x : B</code></pre>
<p>So this isn’t an identity type you would prove something with, but a much more magical notion that two things are completely the same to the typechecker.</p>
<p>Now in most type theories we have a slightly more powerful notion of definitional equality where not only are <code>x ≡ y</code> if <code>x</code> is <code>y</code> only by definition but also by computation.</p>
<p>So in Coq for example</p>
<pre><code>(2 + 2) ≡ 4</code></pre>
<p>Even though “definitionally” these are entirely separate entities. In most theories, definitionally equal means “inlining all definitions and with normalization”, but not all.</p>
<p>In type theories that distinguish between the two, the judgment that when normalized <code>x</code> is <code>y</code> is called judgmental equality. I won’t distinguish between the two further because most don’t, but it’s worth noting that they can be seen as separate concepts.</p>
<h2 id="propositional-equality">Propositional Equality</h2>
<p>This is the sort of equality that we’ll spend the rest of our time discussing. Propositional equality is a particular type constructor with the type/kind</p>
<pre><code>Id : (A : Set) → A → A → Type</code></pre>
<p>We should be able to prove a number of definitions like</p>
<pre><code>reflexivity  : (A : Set)(x     : A) → Id x x
symmetry     : (A : Set)(x y   : A) → Id x y → Id y x
transitivity : (A : Set)(x y z : A) → Id x y → Id y z → Id x z</code></pre>
<p>This is an entirely separate issue from definitional equality since propositional equality is a concept that users can hypothesis about.</p>
<p>One very important difference is that we can make proofs like</p>
<pre><code>sanity : Id 1 2 → ⊥</code></pre>
<p>Since the identity proposition is a type family which can be used just like any other proposition. This is in stark contrast to definitional equality which a user can’t even normally utter!</p>
<h2 id="intensional">Intensional</h2>
<p>This is arguably the simplest form of equality. Identity types are just normal inductive types with normal induction principles. The most common is equality given by Martin Lof</p>
<pre><code>data Id (A : Set) : A → A → Type where
   Refl : (x : A) → Id x x</code></pre>
<p>This yields a simple induction principle</p>
<pre><code>id-ind : (P : (x y : A) → Id x y → Type)
       → ((x : A) → P x x (Refl x))
       → (x y : A)(p : Id x y) → P x y p</code></pre>
<p>In other words, if we can prove that <code>P</code> holds for the reflexivity case, than <code>P</code> holds for any <code>x</code> and <code>y</code> where <code>Id x y</code>.</p>
<p>We can actually phrase <code>Id</code> in a number of ways, including</p>
<pre><code>data Id (A : Set)(x : A) : A → Set where
  Refl : Id x x</code></pre>
<p>This really makes a difference in the resulting induction principle</p>
<pre><code>j : (A : Set)(x : A)(P : (y : A) → Id x y → Set)
  → P x Refl
  → (y : A)(p : Id x y) → P y p</code></pre>
<p>This clearly turned out a bit differently! In particular now <code>P</code> is only parametrized over <em>one</em> value of <code>A</code>, <code>y</code>. This particular elimination is traditionally named <code>j</code>.</p>
<p>These alternative phrasings can have serious impacts on proofs that use them. It also has even more subtle effects on things like heterogeneous equality which we’ll discuss later.</p>
<p>The fact that this only relies on simple inductive principles is also a win for typechecking. Equality/substitution fall straight out of how normal inductive types are handled! This also means that we can keep decidability within reason.</p>
<p>The price we pay of course is that this is much more painful to work with. An intensional identity type means the burden of constructing our equality proofs falls on users. Furthermore, we lose the ability to talk about observational equality.</p>
<p>Observational equality is the idea that two “thingies” are indistinguishable by any test.</p>
<p>It’s clear that we can prove that if <code>Id x y</code>, then <code>f x = f y</code>, but it’s less clear how to go the other way and prove something like</p>
<pre><code>fun_ext : (A B : Set)(f g : A → B)
         → ((x : A) → Id (f x) (g x)) → Id f g
fun_ext f g p = ??</code></pre>
<p>Even though this is clearly desirable. If we know that <code>f</code> and <code>g</code> behave exactly the same way, we’d like our equality to be able to state that. However, we don’t know that <code>f</code> and <code>g</code> are <em>constructed</em> the same way, making this impossible to prove.</p>
<p>This can be introduced as an axiom but to maintain our inductively defined equality type we have to sacrifice one of the following</p>
<ol type="1">
<li>Coherence</li>
<li>Inductive types</li>
<li>Extensionality</li>
<li>Decidability</li>
</ol>
<p>Some this has been avoided by regarding equality as an induction over the <em>class</em> of types as in Martin Lof’s intuitionist type theory.</p>
<p>In the type theory that we’ve outlined, this isn’t expressible sadly.</p>
<h2 id="definitional-extensional">Definitional + Extensional</h2>
<p>Some type theories go a different route to equality, giving us back the extensionality in the process. One of those type theories is extensional type theory.</p>
<p>In the simplest formulation, we have intensional type theory with a new rule, reflection</p>
<pre><code>Γ ⊢ p : Id x y
——————————–————
  Γ ⊢ x ≡ y</code></pre>
<p>This means that our normal propositional equality can be shoved <em>back</em> into the more magical definitional equality. This gives us a lot more power, all the typecheckers magic and support of definitional equality can be used with our equality types!</p>
<p>It isn’t all puppies an kittens though, arbitrary reflection can also make things undecidable in general. For example Martin Lof’s system is undecidable with extensional equality.</p>
<p>It’s worth noting that no extensional type theory is implemented this way. Instead they’ve taken a different approach to defining types themselves!</p>
<p>In this model of ETT types are regarded as a partial equivalence relation (PER) over unityped (untyped if you want to get in a flamewar) lambda calculus terms.</p>
<p>These PERs precisely reflect the extensional equality at that “type” and we then check membership by reflexivity. So <code>a : T</code> is synonymous with <code>(a, a) ∈ T</code>. Notice that since we are dealing with a PER, we know that <code>∀ a. (a, a) ∈ T</code> need not hold. This is reassuring, otherwise we’d be able to prove that every type was inhabited by every term!</p>
<p>The actual NuRPL&amp;friends theory is a little more complicated than that. It’s not entirely dependent on PERs and allows a few different ways of introducing types, but I find that PERs are a helpful idea.</p>
<h2 id="propositional-extensionality">Propositional Extensionality</h2>
<p>This is another flavor of extensional type theory which is really just intensional type theory plus some axioms.</p>
<p>We can arrive at this type theory in a number of ways, the simplest is to add axiom K</p>
<pre><code>k : (A : Set)(x : A)(P : (x : A) → Id x x → Type)
  → P x (Refl x) → (p : Id x x) → P x p</code></pre>
<p>This says that if we can prove that for any property <code>P</code>, <code>P x (Refl x)</code> holds, then it holds for any proof that <code>Id x x</code>. This is subtly different than straightforward induction on <code>Id</code> because here we’re not proving that a property parameterized over two different values of <code>A</code>, but only one.</p>
<p>This is horribly inconsistent in something like homotopy type theory but lends a bit of convenience to theories where we don’t give <code>Id</code> as much meaning.</p>
<p>Using <code>k</code> we can prove that for any <code>p q : Id x y</code>, then <code>Id p q</code>. In Agda notation</p>
<div class="sourceCode" id="cb14"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb14-1" data-line-number="1">    prop <span class="ot">:</span> <span class="ot">(</span>A <span class="ot">:</span> <span class="dt">Set</span><span class="ot">)(</span>x y <span class="ot">:</span> A<span class="ot">)(</span>p q <span class="ot">:</span> x ≡ y<span class="ot">)</span></a>
<a class="sourceLine" id="cb14-2" data-line-number="2">         <span class="ot">→</span> p ≡ q</a>
<a class="sourceLine" id="cb14-3" data-line-number="3">    prop A x <span class="ot">.</span>x refl q <span class="ot">=</span> k A P <span class="ot">(λ</span> <span class="ot">_</span> <span class="ot">→</span> refl<span class="ot">)</span> x q</a>
<a class="sourceLine" id="cb14-4" data-line-number="4">      <span class="kw">where</span> P <span class="ot">:</span> <span class="ot">(</span>x <span class="ot">:</span> A<span class="ot">)</span> <span class="ot">→</span> x ≡ x <span class="ot">→</span> <span class="dt">Set</span></a>
<a class="sourceLine" id="cb14-5" data-line-number="5">            P <span class="ot">_</span> p <span class="ot">=</span> refl ≡ p</a></code></pre></div>
<p>This can be further refined to show that that we can eliminate all proofs that <code>Id x x</code> are <code>Refl x</code></p>
<div class="sourceCode" id="cb15"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb15-1" data-line-number="1">    rec <span class="ot">:</span> <span class="ot">(</span>A <span class="ot">:</span> <span class="dt">Set</span><span class="ot">)(</span>P <span class="ot">:</span> A <span class="ot">→</span> <span class="dt">Set</span><span class="ot">)(</span>x y <span class="ot">:</span> A<span class="ot">)(</span>p <span class="ot">:</span> P x<span class="ot">)</span> <span class="ot">→</span> x ≡ y <span class="ot">→</span> P y</a>
<a class="sourceLine" id="cb15-2" data-line-number="2">    rec A P x <span class="ot">.</span>x p refl <span class="ot">=</span> p</a>
<a class="sourceLine" id="cb15-3" data-line-number="3"></a>
<a class="sourceLine" id="cb15-4" data-line-number="4">    rec-refl-is-useless <span class="ot">:</span> <span class="ot">(</span>A <span class="ot">:</span> <span class="dt">Set</span><span class="ot">)(</span>P <span class="ot">:</span> A <span class="ot">→</span> <span class="dt">Set</span><span class="ot">)(</span>x <span class="ot">:</span> A<span class="ot">)</span></a>
<a class="sourceLine" id="cb15-5" data-line-number="5">                        <span class="ot">→</span> <span class="ot">(</span>p <span class="ot">:</span> P x<span class="ot">)(</span>eq <span class="ot">:</span> x ≡ x<span class="ot">)</span> <span class="ot">→</span> p ≡ rec A P x x p eq</a>
<a class="sourceLine" id="cb15-6" data-line-number="6">    rec-refl-is-useless A P x p eq <span class="kw">with</span> prop A x x eq refl</a>
<a class="sourceLine" id="cb15-7" data-line-number="7">    rec-refl-is-useless A P x p <span class="ot">.</span>refl <span class="ot">|</span> refl <span class="ot">=</span> refl</a></code></pre></div>
<p>This form of extensional type theory still leaves a clear distinction between propositional equality and definitional equality by avoiding a reflection rule. However, with <code>rec-refl-is–useless</code> we can do much of the same things, whenever we have something that matches on an equality proof we can just remove it.</p>
<p>We essentially have normal propositional equality, but with the knowledge that things can only be equal in 1 way, up to propositional equality!</p>
<h2 id="heterogeneous-equality">Heterogeneous Equality</h2>
<p>The next form of equality we’ll talk about is slightly different than previous ones. Heterogeneous equality is designed to co-exist in some other type theory and supplement the existing form of equality.</p>
<p>Heterogeneous equality is most commonly defined with John Major equality</p>
<div class="sourceCode" id="cb16"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb16-1" data-line-number="1">    <span class="kw">data</span> JMeq <span class="ot">:</span> <span class="ot">(</span>A B <span class="ot">:</span> <span class="dt">Set</span><span class="ot">)</span> <span class="ot">→</span> A <span class="ot">→</span> B <span class="ot">→</span> <span class="dt">Set</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb16-2" data-line-number="2">      JMrefl <span class="ot">:</span> <span class="ot">(</span>A <span class="ot">:</span> <span class="dt">Set</span><span class="ot">)(</span>x <span class="ot">:</span> A<span class="ot">)</span> <span class="ot">→</span> JMeq A A x x</a></code></pre></div>
<p>This is termed after a British politician since while it promises that any two terms can be equal regardless of their class (type), only two things from the same class can ever be equal.</p>
<p>Now remember how earlier I’d mentioned that how we phrase these inductive equality types can have a huge impact? We’ll here we can see that because the above definition doesn’t typecheck in Agda!</p>
<p>That’s because Agda is predicative, meaning that a type constructor can’t quantify over the same universe it occupies. We can however, cleverly phrase <code>JMeq</code> so to avoid this</p>
<div class="sourceCode" id="cb17"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb17-1" data-line-number="1">    <span class="kw">data</span> JMeq <span class="ot">(</span>A <span class="ot">:</span> <span class="dt">Set</span><span class="ot">)</span> <span class="ot">:</span> <span class="ot">(</span>B <span class="ot">:</span> <span class="dt">Set</span><span class="ot">)</span> <span class="ot">→</span> A <span class="ot">→</span> B <span class="ot">→</span> <span class="dt">Set</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb17-2" data-line-number="2">      JMrefl <span class="ot">:</span> <span class="ot">(</span>a <span class="ot">:</span> A<span class="ot">)</span> <span class="ot">→</span> JMeq A A a a</a></code></pre></div>
<p>Now the constructor avoids quantifying over <code>Set</code> and therefore fits inside the same universe as <code>A</code> and <code>B</code>.</p>
<p><code>JMeq</code> is usually paired with an axiom to reflect heterogeneous equality back into our normal equality proof.</p>
<pre><code>reflect : (A : Set)(x y : A) → JMeq x y → Id x y</code></pre>
<p>This reflection doesn’t look necessary, but arises for similar reasons that dictate that <code>k</code> is unprovable.</p>
<p>It looks like this heterogeneous equality is a lot more trouble than it’s worth at first. It really shines when we’re working with terms that we <em>know</em> must be the same, but require pattern matching or other jiggering to prove.</p>
<p>If you’re looking for a concrete example, look no further than <a href="https://synrc.com/publications/cat/Temp/Logic/Observational.pdf">Observational Equality Now!</a>. This paper gives allows observational equality to be jammed into a principally intensional system!</p>
<h2 id="wrap-up">Wrap Up</h2>
<p>So this has been a whirlwind tour through a lot of different type theories. I partially wrote this to gather some of this information in one (free) place. If there’s something here missing that you’d like to see added, feel free to comment or email me.</p>
<p><em>Thanks to Jon Sterling for proof reading and many subtle corrections :)</em></p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Wed, 06 Aug 2014 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2014-08-06-equality.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Many Shades of Halting Oracles</title>
    <link>http://jozefg.bitbucket.org/posts/2014-07-30-many-shades-of-halting.html</link>
    <description><![CDATA[<div class="info">
    Posted on July 30, 2014
    
</div>
<div class="info">
    
    Tags: <a href="/tags/types.html">types</a>
    
</div>

<p>I’m going to a take a quick break from arguing with people on the internet to talk about a common point of confusion with theorem provers.</p>
<p>People will often state things like “A program in Coq never diverges” or that “we must prove that X halts”. To an outsider, that sounds impossible! After all, isn’t the halting problem undecidable?</p>
<p>Now the thing to realize is that while <em>yes</em> the halting problem is undecidable, we’re not solving it. The halting problem essentially states</p>
<blockquote>
<p>For an arbitrary turing machine P. There is no algorithm guaranteed to terminate that will return true if P halts and false if it diverges.</p>
</blockquote>
<p>In theorem provers, we cleverly avoid this road block with two simple tricks. I’m going to discuss these in the context of Coq but these ideas generalize between most theorem provers.</p>
<h3 id="being-negative">Being Negative</h3>
<p>A program in Coq <em>must</em> halt. To do otherwise would introduce a logical inconsistency. So to enforce this we need to statically decide whether some program halts.</p>
<p>We just said that this is impossible though! To escape this paradox Coq opts for a simple idea: reject good programs.</p>
<p>Rather than guaranteeing to return true for every good program, we state that we’ll definitely reject all bad programs and then some.</p>
<p>For example, this termination checker would be logically consistent</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb1-1" data-line-number="1"><span class="ot">    terminates ::</span> <span class="dt">CoqProgram</span> <span class="ot">-&gt;</span> <span class="dt">Bool</span></a>
<a class="sourceLine" id="cb1-2" data-line-number="2">    terminates _ <span class="fu">=</span> <span class="dt">False</span></a></code></pre></div>
<p>It’d be useless of course, but consistent. Coq therefore accepts a certain set of programs which are known to terminate. For example, ones that limit themselves only to guarded coinduction or structural induction.</p>
<h3 id="getting-our-hands-dirty">Getting Our Hands Dirty</h3>
<p>While it may be impossible to decide the termination of an arbitrary program, it’s certainly possible to prove the termination of a specific program.</p>
<p>When Coq’s heuristics fail, we can always resort to manually proving that our code will terminate. This may not be pleasant, but it’s certainly doable. By lifting the burden of Coq, we go from “constructing arbitrary proof of termination” to “checking arbitrary proof of termination”, which is decidable.</p>
<p>In Coq we can do this will <a href="http://adam.chlipala.net/cpdt/html/GeneralRec.html">well founded recursion</a>. Simply put, well founded recursion means that we shift from using only term “size” to decide what’s a smaller recursive call to any nice binary relation. If you’re not interested in Coq specifically, you can check out your preferred proof assistants formalization of well founded recursion.</p>
<p>To this end, we define a relation for some type <code>A : Set</code>, <code>R : A -&gt; A -&gt; Prop</code>. Read <code>R x y</code> as <code>x</code> is smaller than <code>y</code>.</p>
<p>Now we must show that this relation preserves some definition of “sanity”. This should mean that if when a function receives <code>x</code>, for any <code>y</code> so that <code>R y x</code>, we should be able to recurse on y. This should also mean that there’s no infinite stack of terms so that <code>R x y</code>, <code>R z x</code>, <code>R w z</code> …. because this would mean we could recurse infinitely. To capture this idea, we must prove <code>well_founded A R</code>. What’s this “well founded” thing you say?</p>
<p>Well it’s just</p>
<pre><code>Definition well_founded A R := forall a : A, Acc R a</code></pre>
<p>This <code>Acc</code> thing means “accessible”,</p>
<pre><code>Inductive Acc (A : Type) (R : A -&gt; A -&gt; Prop) (x : A) : Prop :=
    Acc_intro : (forall y : A, R y x -&gt; Acc R y) -&gt; Acc R x</code></pre>
<p>So something is accessible in <code>R</code> if everything less than it is also accessible.</p>
<p>We can easily prove that if <code>R</code> is <code>well_founded</code> there is no infinite chain that could lead us to infinite recursion.</p>
<pre><code>Section founded.
  Variable A : Set.
  Variable R : A -&gt; A -&gt; Prop.

  Variable well_founded : well_founded R.

  CoInductive stream :=
  | Cons : A -&gt; stream -&gt; stream.

  CoInductive tower_of_bad : stream -&gt; Prop :=
    OnTop : forall x y rest,
            R y x -&gt;
            tower_of_bad (Cons y rest) -&gt;
            tower_of_bad (Cons x (Cons y rest)).

  Lemma never_on_top :
    forall x, forall rest, ~ tower_of_bad (Cons x rest).
    intro; induction (well_founded x); inversion 1; try subst;
    match goal with
        [H : context[~ _] |- _ ] =&gt; eapply H; eauto
    end.
  Qed.

  Theorem no_chains :
    forall xs, ~ tower_of_bad xs.
    destruct 1; eapply never_on_top; eauto.
  Qed.
End founded.</code></pre>
<p>We’re using a powerful trick in <code>never_on_top</code>, we’re inducting upon <code>Acc</code>! This is the key to using well founded recursion. By inducting upon the <code>Acc</code> instead of one of the terms of our function, we can easily recurse on any subterm <code>y</code>, if <code>R y x</code>.</p>
<p>This is handed to us by the lovely <code>Fix</code> (uppercase).</p>
<pre><code>Fix : well_founded R -&gt;
    forall P : A -&gt; Type,
      (forall x : A, (forall y : A, R y x -&gt; P y) -&gt; P x) -&gt;
      forall x : A, P x</code></pre>
<p>So <code>Fix</code> is the better, cooler version of structural recursion that we were after. It lets us recurse on any <code>y</code> where <code>R y x</code>.</p>
<p>So in some sense, you can view Coq’s Fixpoint as just a specialization of <code>Fix</code> where <code>R x y</code> means that <code>x</code> is a subterm of <code>y</code>.</p>
<h2 id="wrap-up">Wrap Up</h2>
<p>So in conclusion, theorem provers don’t do the impossible. Rather they have a small battery of tricks to cheat the impossible general case and simplify common cases.</p>
<p>Back to the internet I go.</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Wed, 30 Jul 2014 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2014-07-30-many-shades-of-halting.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>A Tutorial on Church Representations</title>
    <link>http://jozefg.bitbucket.org/posts/2014-07-19-church-tutorial.html</link>
    <description><![CDATA[<div class="info">
    Posted on July 19, 2014
    
</div>
<div class="info">
    
    Tags: <a href="/tags/haskell.html">haskell</a>, <a href="/tags/types.html">types</a>
    
</div>

<p>I’ve written <a href="/posts/2014-03-06-church.html">a</a> <a href="/posts/2014-03-07-church-the-sequel.html">few</a> <a href="/posts/2014-03-10-revenge-of-churchrep.html">times</a> about church representations, but never aimed at someone who’d never heard of what a church representation is. In fact, it doesn’t really seem like too many people have!</p>
<p>In this post I’d like to fix that :)</p>
<h2 id="what-is-a-church-representation">What is a Church Representation</h2>
<p>Simply put, a church representation (CR) is a way of representing a piece of concrete data with a function. The CR can be used through an identical way to the concrete data, but it’s comprised entirely of functions.</p>
<p>They where originally described by Alanzo Church as a way of modeling all data in lambda calculus, where all we have is functions.</p>
<h2 id="tuples">Tuples</h2>
<p>The simplest CR I’ve found is that of a tuples.</p>
<p>Let’s first look at our basic tuple API</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb1-1" data-line-number="1">    <span class="kw">type</span> <span class="dt">Tuple</span> a b <span class="fu">=</span> <span class="fu">...</span></a>
<a class="sourceLine" id="cb1-2" data-line-number="2"><span class="ot">    mkTuple ::</span> a <span class="ot">-&gt;</span> b <span class="ot">-&gt;</span> <span class="dt">Tuple</span> a b</a>
<a class="sourceLine" id="cb1-3" data-line-number="3"><span class="ot">    fst     ::</span> <span class="dt">Tuple</span> a b <span class="ot">-&gt;</span> a</a>
<a class="sourceLine" id="cb1-4" data-line-number="4"><span class="ot">    snd     ::</span> <span class="dt">Tuple</span> a b <span class="ot">-&gt;</span> b</a></code></pre></div>
<p>Now this is trivially implemented with <code>(,)</code></p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb2-1" data-line-number="1">    <span class="kw">type</span> <span class="dt">Tuple</span> a b <span class="fu">=</span> (a, b)</a>
<a class="sourceLine" id="cb2-2" data-line-number="2">    mkTuple <span class="fu">=</span> (,)</a>
<a class="sourceLine" id="cb2-3" data-line-number="3">    fst     <span class="fu">=</span> Prelude.fst</a>
<a class="sourceLine" id="cb2-4" data-line-number="4">    snd     <span class="fu">=</span> Prelude.snd</a></code></pre></div>
<p>The church representation preserves the interface, but changes all the underlying implementations.</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb3-1" data-line-number="1">    <span class="kw">type</span> <span class="dt">Tuple</span> a b <span class="fu">=</span> forall c<span class="fu">.</span> (a <span class="ot">-&gt;</span> b <span class="ot">-&gt;</span> c) <span class="ot">-&gt;</span> c</a></code></pre></div>
<p>There’s our church pair, notice that it’s only comprised of <code>-&gt;</code>. It also makes use of higher rank types. This means that a <code>Tuple a b</code> can be applied to function producing <em>any</em> <code>c</code> and it must return something of that type.</p>
<p>Let’s look at how the rest of our API is implemented</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" data-line-number="1">    mkTuple a b <span class="fu">=</span> \f <span class="ot">-&gt;</span> f a b</a>
<a class="sourceLine" id="cb4-2" data-line-number="2">    fst tup     <span class="fu">=</span> tup (\a _ <span class="ot">-&gt;</span> a)</a>
<a class="sourceLine" id="cb4-3" data-line-number="3">    snd tup     <span class="fu">=</span> tup (\_ b <span class="ot">-&gt;</span> b)</a></code></pre></div>
<p>And that’s it!</p>
<p>It’s helpful to step through some reductions here</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb5-1" data-line-number="1">    fst (mkTuple <span class="dv">1</span> <span class="dv">2</span>)</a>
<a class="sourceLine" id="cb5-2" data-line-number="2">    fst (\f <span class="ot">-&gt;</span> f <span class="dv">1</span> <span class="dv">2</span>)</a>
<a class="sourceLine" id="cb5-3" data-line-number="3">    (\f <span class="ot">-&gt;</span> f <span class="dv">1</span> <span class="dv">2</span>) (\a _ <span class="ot">-&gt;</span> a)</a>
<a class="sourceLine" id="cb5-4" data-line-number="4">    (\a _ <span class="ot">-&gt;</span> a) <span class="dv">1</span> <span class="dv">2</span></a>
<a class="sourceLine" id="cb5-5" data-line-number="5">    <span class="dv">1</span></a></code></pre></div>
<p>And for <code>snd</code></p>
<div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb6-1" data-line-number="1">    snd (mkTuple <span class="dt">True</span> <span class="dt">False</span>)</a>
<a class="sourceLine" id="cb6-2" data-line-number="2">    fst (\f <span class="ot">-&gt;</span> f <span class="dt">True</span> <span class="dt">False</span>)</a>
<a class="sourceLine" id="cb6-3" data-line-number="3">    (\f <span class="ot">-&gt;</span> f <span class="dt">True</span> <span class="dt">False</span>) (\_ b <span class="ot">-&gt;</span> b)</a>
<a class="sourceLine" id="cb6-4" data-line-number="4">    (\_ b <span class="ot">-&gt;</span> b) <span class="dt">True</span> false</a>
<a class="sourceLine" id="cb6-5" data-line-number="5">    <span class="dt">False</span></a></code></pre></div>
<p>So we can see that these are clearly morally equivalent. The only real question here is whether, for each CR tuple there exists a normal tuple. This isn’t immediately apparent since the function type for the CR looks a lot more general. In fact, the key to this proof lies in the <code>forall c</code> part, this extra polymorphism let’s us use a powerful technique called “parametricity” to prove that they’re equivalent.</p>
<p>I won’t actually go into such a proof now since it’s not entirely relevant, but it’s worth noting that both <code>(,)</code> and <code>Tuple</code> are completely isomorphic.</p>
<p>To convert between them is pretty straightforward</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb7-1" data-line-number="1"><span class="ot">    isoL ::</span> <span class="dt">Tuple</span> a b <span class="ot">-&gt;</span> (a, b)</a>
<a class="sourceLine" id="cb7-2" data-line-number="2">    isoL tup <span class="fu">=</span> tup (,)</a>
<a class="sourceLine" id="cb7-3" data-line-number="3"></a>
<a class="sourceLine" id="cb7-4" data-line-number="4"><span class="ot">    isoR ::</span> (a, b) <span class="ot">-&gt;</span> <span class="dt">Tuple</span> a b</a>
<a class="sourceLine" id="cb7-5" data-line-number="5">    isoR (a, b) <span class="fu">=</span> \f <span class="ot">-&gt;</span> f a b</a></code></pre></div>
<p>Now that we have an idea of how to church representations “work” let’s go through a few more examples to start to see a pattern.</p>
<h2 id="booleans">Booleans</h2>
<p>Booleans have the simplest API of all</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb8-1" data-line-number="1">    <span class="kw">type</span> <span class="dt">Boolean</span> <span class="fu">=</span> <span class="fu">...</span></a>
<a class="sourceLine" id="cb8-2" data-line-number="2"><span class="ot">    true  ::</span> <span class="dt">Boolean</span></a>
<a class="sourceLine" id="cb8-3" data-line-number="3"><span class="ot">    false ::</span> <span class="dt">Boolean</span></a>
<a class="sourceLine" id="cb8-4" data-line-number="4"><span class="ot">    test  ::</span> <span class="dt">Boolean</span> <span class="ot">-&gt;</span> a <span class="ot">-&gt;</span> a <span class="ot">-&gt;</span> a</a></code></pre></div>
<p>We can build all other boolean operations on <code>test</code></p>
<div class="sourceCode" id="cb9"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb9-1" data-line-number="1">    a <span class="fu">&amp;&amp;</span> b <span class="fu">=</span> test a b false</a>
<a class="sourceLine" id="cb9-2" data-line-number="2">    a <span class="fu">||</span> b <span class="fu">=</span> test a true b</a>
<a class="sourceLine" id="cb9-3" data-line-number="3">    when t e <span class="fu">=</span> test t e (return ())</a></code></pre></div>
<p>This API is quite simple to implement with <code>Bool</code>,</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb10-1" data-line-number="1">    <span class="kw">type</span> <span class="dt">Boolean</span> <span class="fu">=</span> <span class="dt">Bool</span></a>
<a class="sourceLine" id="cb10-2" data-line-number="2"></a>
<a class="sourceLine" id="cb10-3" data-line-number="3">    true  <span class="fu">=</span> <span class="dt">True</span></a>
<a class="sourceLine" id="cb10-4" data-line-number="4">    false <span class="fu">=</span> <span class="dt">False</span></a>
<a class="sourceLine" id="cb10-5" data-line-number="5">    test b t e <span class="fu">=</span> <span class="kw">if</span> b <span class="kw">then</span> t <span class="kw">else</span> e</a></code></pre></div>
<p>But how could we represent this with functions? The answer stems from <code>test</code>,</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb11-1" data-line-number="1">    <span class="kw">type</span> <span class="dt">Boolean</span> <span class="fu">=</span> forall a<span class="fu">.</span> a <span class="ot">-&gt;</span> a <span class="ot">-&gt;</span> a</a></code></pre></div>
<p>Clever readers will notice this is almost identical to <code>test</code>, a boolean get’s two arguments and returns one or the other.</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb12-1" data-line-number="1">    true  <span class="fu">=</span> \a _ <span class="ot">-&gt;</span> a</a>
<a class="sourceLine" id="cb12-2" data-line-number="2">    false <span class="fu">=</span> \_ b <span class="ot">-&gt;</span> b</a>
<a class="sourceLine" id="cb12-3" data-line-number="3">    test b t e <span class="fu">=</span> b t e</a></code></pre></div>
<p>We can write an isomorphism between <code>Bool</code> and <code>Boolean</code> as well</p>
<div class="sourceCode" id="cb13"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb13-1" data-line-number="1"><span class="ot">    isoL ::</span> <span class="dt">Bool</span> <span class="ot">-&gt;</span> <span class="dt">Boolean</span></a>
<a class="sourceLine" id="cb13-2" data-line-number="2">    isoL b <span class="fu">=</span> <span class="kw">if</span> b <span class="kw">then</span> true <span class="kw">else</span> false</a>
<a class="sourceLine" id="cb13-3" data-line-number="3"></a>
<a class="sourceLine" id="cb13-4" data-line-number="4"><span class="ot">    isoR ::</span> <span class="dt">Boolean</span> <span class="ot">-&gt;</span> <span class="dt">Bool</span></a>
<a class="sourceLine" id="cb13-5" data-line-number="5">    isoR b <span class="fu">=</span> test b <span class="dt">True</span> <span class="dt">False</span></a></code></pre></div>
<h2 id="lists">Lists</h2>
<p>Now let’s talk about lists. One of the interesting things is lists are the first recursive data type we’ve dealt with so far.</p>
<p>Defining the API for lists isn’t entirely clear either. We want a small set of functions that can easily cover <em>any</em> conceivable operations for a list.</p>
<p>The simplest way to do this is to realize that we can do exactly 3 things with lists.</p>
<ol type="1">
<li>Make an empty list</li>
<li>Add a new element to the front of an existing list</li>
<li>Pattern match on them</li>
</ol>
<p>We can represent this with 3 functions</p>
<div class="sourceCode" id="cb14"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb14-1" data-line-number="1">    <span class="kw">type</span> <span class="dt">List</span> a <span class="fu">=</span> <span class="fu">...</span></a>
<a class="sourceLine" id="cb14-2" data-line-number="2"></a>
<a class="sourceLine" id="cb14-3" data-line-number="3"><span class="ot">    nil   ::</span> <span class="dt">List</span> a</a>
<a class="sourceLine" id="cb14-4" data-line-number="4"><span class="ot">    cons  ::</span> a <span class="ot">-&gt;</span> <span class="dt">List</span> a <span class="ot">-&gt;</span> <span class="dt">List</span> a</a>
<a class="sourceLine" id="cb14-5" data-line-number="5"><span class="ot">    match ::</span> <span class="dt">List</span> a <span class="ot">-&gt;</span> b <span class="ot">-&gt;</span> (a <span class="ot">-&gt;</span> <span class="dt">List</span> a <span class="ot">-&gt;</span> b) <span class="ot">-&gt;</span> b</a></code></pre></div>
<p>If <code>match</code> looks confusing just remember that</p>
<div class="sourceCode" id="cb15"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb15-1" data-line-number="1">    f list <span class="fu">=</span> match list g h</a></code></pre></div>
<p>Is really the same as</p>
<div class="sourceCode" id="cb16"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb16-1" data-line-number="1">    f []       <span class="fu">=</span> g</a>
<a class="sourceLine" id="cb16-2" data-line-number="2">    f (x <span class="fu">:</span> xs) <span class="fu">=</span> h x xs</a></code></pre></div>
<p>In this way <code>match</code> is just the pure functional version of pattern matching. We can actually simplify the API by realizing that rather than this awkward <code>match</code> construct, we can use something cleaner.</p>
<p><code>foldr</code> forms a much more pleasant API to work with since it’s really the most primitive form of “recursing” on a list.</p>
<div class="sourceCode" id="cb17"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb17-1" data-line-number="1"><span class="ot">    match ::</span> <span class="dt">List</span> a <span class="ot">-&gt;</span> (a <span class="ot">-&gt;</span> <span class="dt">List</span> a <span class="ot">-&gt;</span> b) <span class="ot">-&gt;</span> b <span class="ot">-&gt;</span> b</a>
<a class="sourceLine" id="cb17-2" data-line-number="2">    match list f b <span class="fu">=</span> fst <span class="fu">$</span> foldr list worker (b, nil)</a>
<a class="sourceLine" id="cb17-3" data-line-number="3">      <span class="kw">where</span> worker x (b, xs) <span class="fu">=</span> (f x xs, cons x xs)</a></code></pre></div>
<p>The especially nice thing about <code>foldr</code> is that it doesn’t mention <code>List a</code> in its two “destruction” functions, all the recursion is handled in the implementation.</p>
<p>We can implement CR lists trivially using <code>foldr</code></p>
<div class="sourceCode" id="cb18"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb18-1" data-line-number="1">    <span class="kw">type</span> <span class="dt">List</span> a <span class="fu">=</span> forall b<span class="fu">.</span> (a <span class="ot">-&gt;</span> b <span class="ot">-&gt;</span> b) <span class="ot">-&gt;</span> b <span class="ot">-&gt;</span> b</a>
<a class="sourceLine" id="cb18-2" data-line-number="2"></a>
<a class="sourceLine" id="cb18-3" data-line-number="3">    nil <span class="fu">=</span> \ _ nil <span class="ot">-&gt;</span> nil</a>
<a class="sourceLine" id="cb18-4" data-line-number="4">    cons x xs <span class="fu">=</span> \ cons nil <span class="ot">-&gt;</span> x <span class="ot">`cons`</span> xs cons nil</a>
<a class="sourceLine" id="cb18-5" data-line-number="5">    foldr list cons nil <span class="fu">=</span> list cons nil</a></code></pre></div>
<p>Notice that we handle the recursion in the list type by having a <code>b</code> as an argument? This is similar to how the accumulator to <code>foldr</code> gets the processed tail of the list. This is a common technique for handling recursion in our church representations.</p>
<p>Last but not least, the isomorphism arises from <code>foldr (:) []</code>,</p>
<div class="sourceCode" id="cb19"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb19-1" data-line-number="1"><span class="ot">    isoL ::</span> <span class="dt">List</span> a <span class="ot">-&gt;</span> [a]</a>
<a class="sourceLine" id="cb19-2" data-line-number="2">    isoL l <span class="fu">=</span> l (<span class="fu">:</span>) []</a>
<a class="sourceLine" id="cb19-3" data-line-number="3"></a>
<a class="sourceLine" id="cb19-4" data-line-number="4"><span class="ot">    isoR ::</span> [a] <span class="ot">-&gt;</span> <span class="dt">List</span> a</a>
<a class="sourceLine" id="cb19-5" data-line-number="5">    isoR l f z <span class="fu">=</span> foldr f z l</a></code></pre></div>
<h2 id="either">Either</h2>
<p>The last case that we’ll look at is <code>Either</code>. Like <code>Pair</code>, <code>Either</code> has 3 different operations.</p>
<div class="sourceCode" id="cb20"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb20-1" data-line-number="1">    <span class="kw">type</span> <span class="dt">Or</span> a b <span class="fu">=</span> <span class="fu">...</span></a>
<a class="sourceLine" id="cb20-2" data-line-number="2"><span class="ot">    inl ::</span> a <span class="ot">-&gt;</span> <span class="dt">Or</span> a b</a>
<a class="sourceLine" id="cb20-3" data-line-number="3"><span class="ot">    inr ::</span> b <span class="ot">-&gt;</span> <span class="dt">Or</span> a b</a>
<a class="sourceLine" id="cb20-4" data-line-number="4"></a>
<a class="sourceLine" id="cb20-5" data-line-number="5"><span class="ot">    or ::</span> <span class="dt">Or</span> a b <span class="ot">-&gt;</span> (a <span class="ot">-&gt;</span> c) <span class="ot">-&gt;</span> (b <span class="ot">-&gt;</span> c)  <span class="ot">-&gt;</span> c</a></code></pre></div>
<p>This is pretty easy to implement with <code>Either</code></p>
<div class="sourceCode" id="cb21"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb21-1" data-line-number="1">    <span class="kw">type</span> <span class="dt">Or</span> a b <span class="fu">=</span> <span class="dt">Either</span> a b</a>
<a class="sourceLine" id="cb21-2" data-line-number="2">    inl <span class="fu">=</span> <span class="dt">Left</span></a>
<a class="sourceLine" id="cb21-3" data-line-number="3">    inr <span class="fu">=</span> <span class="dt">Right</span></a>
<a class="sourceLine" id="cb21-4" data-line-number="4"></a>
<a class="sourceLine" id="cb21-5" data-line-number="5">    or (<span class="dt">Left</span> a)  f g <span class="fu">=</span> f a</a>
<a class="sourceLine" id="cb21-6" data-line-number="6">    or (<span class="dt">Right</span> b) f g <span class="fu">=</span> g b</a></code></pre></div>
<p>Once again, the trick to encoding this as a function falls right out of the API. In this case we use the type of <code>or</code></p>
<div class="sourceCode" id="cb22"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb22-1" data-line-number="1">     <span class="kw">type</span> <span class="dt">Or</span> a b <span class="fu">=</span> forall c<span class="fu">.</span> (a <span class="ot">-&gt;</span> c) <span class="ot">-&gt;</span> (b <span class="ot">-&gt;</span> c) <span class="ot">-&gt;</span> c</a>
<a class="sourceLine" id="cb22-2" data-line-number="2"></a>
<a class="sourceLine" id="cb22-3" data-line-number="3">    inl a <span class="fu">=</span> \f g <span class="ot">-&gt;</span> f a</a>
<a class="sourceLine" id="cb22-4" data-line-number="4">    inr b <span class="fu">=</span> \f g <span class="ot">-&gt;</span> g a</a>
<a class="sourceLine" id="cb22-5" data-line-number="5"></a>
<a class="sourceLine" id="cb22-6" data-line-number="6">    or x <span class="fu">=</span> x</a></code></pre></div>
<p>Last but not least, let’s quickly rattle off our isomorphism.</p>
<div class="sourceCode" id="cb23"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb23-1" data-line-number="1"><span class="ot">    isoL ::</span> <span class="dt">Or</span> a b <span class="ot">-&gt;</span> <span class="dt">Either</span> a b</a>
<a class="sourceLine" id="cb23-2" data-line-number="2">    isoL o <span class="fu">=</span> o <span class="dt">Left</span> <span class="dt">Right</span></a>
<a class="sourceLine" id="cb23-3" data-line-number="3"></a>
<a class="sourceLine" id="cb23-4" data-line-number="4">    isoR<span class="ot"> o ::</span> <span class="dt">Either</span> a b <span class="ot">-&gt;</span> <span class="dt">Or</span> a b</a>
<a class="sourceLine" id="cb23-5" data-line-number="5">    isoR o <span class="fu">=</span> or o</a></code></pre></div>
<h2 id="the-pattern">The Pattern</h2>
<p>So now we can talk about the underlying pattern in CRs. First remember that for any type <code>T</code>, we have a list of <code>n</code> distinct constructors <code>T1</code> <code>T2</code> <code>T3</code>…<code>Tn</code>. Each of the constructors has a <code>m</code> fields <code>T11</code>, <code>T12</code>, <code>T13</code>…</p>
<p>Now the church representation of such a type <code>T</code> is</p>
<div class="sourceCode" id="cb24"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb24-1" data-line-number="1">    forall c<span class="fu">.</span>  (<span class="dt">T11</span> <span class="ot">-&gt;</span> <span class="dt">T12</span> <span class="ot">-&gt;</span> <span class="dt">T13</span> <span class="ot">-&gt;</span> <span class="fu">..</span> <span class="ot">-&gt;</span> c)</a>
<a class="sourceLine" id="cb24-2" data-line-number="2">            <span class="ot">-&gt;</span> (<span class="dt">T21</span> <span class="ot">-&gt;</span> <span class="dt">T22</span> <span class="ot">-&gt;</span> <span class="dt">T23</span> <span class="ot">-&gt;</span> <span class="fu">..</span> <span class="ot">-&gt;</span> c)</a>
<a class="sourceLine" id="cb24-3" data-line-number="3">            <span class="fu">...</span></a>
<a class="sourceLine" id="cb24-4" data-line-number="4">            <span class="ot">-&gt;</span> (<span class="dt">Tn1</span> <span class="ot">-&gt;</span> <span class="dt">Tn2</span> <span class="ot">-&gt;</span> <span class="dt">Tn3</span> <span class="ot">-&gt;</span> <span class="fu">..</span> <span class="ot">-&gt;</span> c)</a>
<a class="sourceLine" id="cb24-5" data-line-number="5">            <span class="ot">-&gt;</span> c</a></code></pre></div>
<p>This pattern doesn’t map quite as nicely to recursive types. Here we have to take the extra step of substituting all occurrences of <code>T</code> for <code>c</code> in our resulting church representation.</p>
<p>This is actually such a pleasant pattern to work with that I’ve written a <a href="http://hackage.haskell.org/package/generic-church">library</a> for automatically reifying a type between its church representation and concrete form.</p>
<h2 id="wrap-up">Wrap Up</h2>
<p>Hopefully you now understand <em>what</em> a church representation is. It’s worth noting that a lot of stuff Haskellers stumble upon daily are really church representations in disguise.</p>
<p>My favorite example is <code>maybe</code>, this function takes a success and failure continuation with a <code>Maybe</code> and produces a value. With a little bit of imagination, one can realize that this is really just a function mapping a <code>Maybe</code> to a church representation!</p>
<p>If you’re thinking that CRs are pretty cool! Now might be a time to take a look at one of my previous <a href="/posts/2014-03-06-church.html">posts</a> on deriving them automagically.</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Sat, 19 Jul 2014 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2014-07-19-church-tutorial.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Examining Hackage: extensible-effects</title>
    <link>http://jozefg.bitbucket.org/posts/2014-07-15-reading-extensible-effects.html</link>
    <description><![CDATA[<div class="info">
    Posted on July 15, 2014
    
</div>
<div class="info">
    
    Tags: <a href="/tags/haskell.html">haskell</a>
    
</div>

<p>I had a few people tell me after my <a href="/posts/2014-07-10-reading-logict.html">last post</a> that they would enjoy a write up on reading <a href="http://hackage.haskell.org/package/extensible-effects">extensible-effects</a> so here goes.</p>
<p>I’m going to document my process of reading through and understanding how extensible-effects is implemented. Since this is a fairly large library (about 1k) of code, we’re not going over <em>all</em> of it. Rather we’re just reviewing the core modules and enough of the extra ones to get a sense for how everything is implemented.</p>
<p>If you’re curious or still have questions, the modules that we don’t cover should serve as a nice place for further exploration.</p>
<h2 id="which-modules">Which Modules</h2>
<p>extensible-effects comes with quite a few modules, my find query reveals</p>
<pre><code>$ find src -name &quot;*.hs&quot;
  src/Data/OpenUnion1.hs
  src/Control/Eff/Reader/Strict.hs
  src/Control/Eff/Reader/Lazy.hs
  src/Control/Eff/Fresh.hs
  src/Control/Eff/Cut.hs
  src/Control/Eff/Exception.hs
  src/Control/Eff/State/Strict.hs
  src/Control/Eff/State/Lazy.hs
  src/Control/Eff/Writer/Strict.hs
  src/Control/Eff/Writer/Lazy.hs
  src/Control/Eff/Coroutine.hs
  src/Control/Eff/Trace.hs
  src/Control/Eff/Choose.hs
  src/Control/Eff/Lift.hs
  src/Control/Eff.hs
  src/Control/Eff/Reader/Strict.hs</code></pre>
<p>Whew! Well I’m going to take a leap and assume that extensible-effects is similar to the mtl in the sense that there are a few core modules, an then a bunch of “utility” modules. So there’s <code>Control.Monad.Trans</code> and then <code>Control.Monad.State</code> and a bunch of other implementations of <code>MonadTrans</code>.</p>
<p>If we assume extensible-effects is formatted like this, then we need to look at</p>
<ol type="1">
<li>Data.OpenUnion1</li>
<li>Control.Monad.Eff</li>
</ol>
<p>And maybe a few other modules to get a feel for how to use these two. I’ve added <code>Data.OpenUnion1</code> because it’s imported by <code>Control.Monad.Eff</code> so is presumably important.</p>
<p>Since <code>Data.OpenUnion1</code> is at the top of our dependency DAG, we’ll start with it.</p>
<h2 id="data.openunion1">Data.OpenUnion1</h2>
<p>So we’re starting with Data.OpenUnion1. If the authors of this code have stuck to normal Haskell naming conventions, that’s an open union of type <em>constructors</em>, stuff with the kind <code>* -&gt; *</code>.</p>
<p>Happily, this module has an export list so we can at least see what’s public.</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb2-1" data-line-number="1">    <span class="kw">module</span> <span class="dt">Data.OpenUnion1</span>( <span class="dt">Union</span> (<span class="fu">..</span>)</a>
<a class="sourceLine" id="cb2-2" data-line-number="2">                          , <span class="dt">SetMember</span></a>
<a class="sourceLine" id="cb2-3" data-line-number="3">                          , <span class="dt">Member</span></a>
<a class="sourceLine" id="cb2-4" data-line-number="4">                          , (<span class="fu">:&gt;</span>)</a>
<a class="sourceLine" id="cb2-5" data-line-number="5">                          , inj</a>
<a class="sourceLine" id="cb2-6" data-line-number="6">                          , prj</a>
<a class="sourceLine" id="cb2-7" data-line-number="7">                          , prjForce</a>
<a class="sourceLine" id="cb2-8" data-line-number="8">                          , decomp</a>
<a class="sourceLine" id="cb2-9" data-line-number="9">                          , unsafeReUnion</a>
<a class="sourceLine" id="cb2-10" data-line-number="10">                          ) <span class="kw">where</span></a></code></pre></div>
<p>So we’re looking at a data type <code>Union</code>, which we export everything for. Two type classes <code>SetMember</code> and <code>Member</code>, a type operator <code>:&gt;</code>, and a handful of functions, most likely to work with <code>Union</code>.</p>
<p>So let’s figure out exactly what this union thing is</p>
<pre><code>data Union r v = forall t. (Functor t, Typeable1 t) =&gt; Union (t v)</code></pre>
<p>So <code>Union r v</code> is just a wrapper around some of functor applied to <code>v</code>. This seems a little odd, what’s this <code>r</code> thing? The docs hint that <code>Member t r</code> should always hold.</p>
<p><code>Member</code> is a type class of two parameters with no members. In fact, <code>grep</code>ing the entire source reveals that the entire definition and instances for <code>Member</code> in this code base is</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" data-line-number="1">    <span class="kw">infixr</span> <span class="dv">1</span> <span class="fu">:&gt;</span></a>
<a class="sourceLine" id="cb4-2" data-line-number="2">    <span class="kw">data</span> ((<span class="ot">a ::</span> <span class="fu">*</span> <span class="ot">-&gt;</span> <span class="fu">*</span>) <span class="fu">:&gt;</span> b)</a>
<a class="sourceLine" id="cb4-3" data-line-number="3"></a>
<a class="sourceLine" id="cb4-4" data-line-number="4">    <span class="kw">class</span> <span class="dt">Member</span> t r</a>
<a class="sourceLine" id="cb4-5" data-line-number="5">    <span class="kw">instance</span> <span class="dt">Member</span> t (t <span class="fu">:&gt;</span> r)</a>
<a class="sourceLine" id="cb4-6" data-line-number="6">    <span class="kw">instance</span> <span class="dt">Member</span> t r <span class="ot">=&gt;</span> <span class="dt">Member</span> t (t&#39; <span class="fu">:&gt;</span> r)</a></code></pre></div>
<p>So this makes it a bit clearer, <code>:&gt;</code> acts like a type level cons and <code>Member</code> just checks for membership!</p>
<p>Now <code>Union</code> makes a bit more sense, especially in light of the <code>inj</code> function</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb5-1" data-line-number="1"><span class="ot">    inj ::</span> (<span class="dt">Functor</span> t, <span class="dt">Typeable1</span> t, <span class="dt">Member</span> t r) <span class="ot">=&gt;</span> t v <span class="ot">-&gt;</span> <span class="dt">Union</span> r v</a>
<a class="sourceLine" id="cb5-2" data-line-number="2">    inj <span class="fu">=</span> <span class="dt">Union</span></a></code></pre></div>
<p>So <code>Union</code> takes some <code>t</code> in <code>r</code> and hides it away in an existential applied to <code>v</code>. Now this is kinda like having a great nested bunch of <code>Either</code>s with every <code>t</code> applied to <code>v</code>.</p>
<p>Dual to <code>inj</code>, we can define a projection from a <code>Union</code> to some <code>t</code> in <code>r</code>. This will need to return something wrapped in <code>Maybe</code> since we don’t know which member of <code>r</code> our <code>Union</code> is wrapping.</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb6-1" data-line-number="1"><span class="ot">    prj ::</span> (<span class="dt">Typeable1</span> t, <span class="dt">Member</span> t r) <span class="ot">=&gt;</span> <span class="dt">Union</span> r v <span class="ot">-&gt;</span> <span class="dt">Maybe</span> (t v)</a>
<a class="sourceLine" id="cb6-2" data-line-number="2">    prj (<span class="dt">Union</span> v) <span class="fu">=</span> runId <span class="fu">&lt;$&gt;</span> gcast1 (<span class="dt">Id</span> v)</a></code></pre></div>
<p><code>prj</code> does some evil <code>Typeable</code> casts, but this is necessary since we’re throwing away all our type information with that existential. That <code>Id</code> <code>runId</code> pair is needed since <code>gcast1</code> has the type</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb7-1" data-line-number="1">    <span class="co">-- In our case, `c ~ Id`</span></a>
<a class="sourceLine" id="cb7-2" data-line-number="2"><span class="ot">    gcast1 ::</span> (<span class="dt">Typeable</span> t&#39;, <span class="dt">Typeable</span> t) <span class="ot">=&gt;</span> c (t a) <span class="ot">-&gt;</span> <span class="dt">Maybe</span> (c (t&#39; a))</a></code></pre></div>
<p>They’re just defined as</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb8-1" data-line-number="1">    <span class="kw">newtype</span> <span class="dt">Id</span> a <span class="fu">=</span> <span class="dt">Id</span> {<span class="ot"> runId ::</span> a }</a>
<a class="sourceLine" id="cb8-2" data-line-number="2">      <span class="kw">deriving</span> <span class="dt">Typeable</span></a></code></pre></div>
<p>so just like <code>Control.Monad.Identity</code>.</p>
<p>Now let’s try to figure out what this <code>SetMember</code> thing is.</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb9-1" data-line-number="1">    <span class="kw">class</span> <span class="dt">Member</span> t r <span class="ot">=&gt;</span> <span class="dt">SetMember</span> set (<span class="ot">t ::</span> <span class="fu">*</span> <span class="ot">-&gt;</span> <span class="fu">*</span>) r <span class="fu">|</span> r set <span class="ot">-&gt;</span> t</a>
<a class="sourceLine" id="cb9-2" data-line-number="2">    <span class="kw">instance</span> <span class="dt">SetMember</span> set t r <span class="ot">=&gt;</span> <span class="dt">SetMember</span> set t (t&#39; <span class="fu">:&gt;</span> r)</a></code></pre></div>
<p>This is unhelpful, all we have is the recursive step with no base case! Resorting to grep reveals that our base case is defined in <code>Control.Eff.Lift</code> so we’ll temporarily put this class off until then.</p>
<p>Now the rest of the file is defining a few functions to operate over <code>Union</code>s.</p>
<p>First up is an unsafe “forced” version of <code>prj</code>.</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb10-1" data-line-number="1">    <span class="kw">infixl</span> <span class="dv">4</span> <span class="fu">&lt;?&gt;</span></a>
<a class="sourceLine" id="cb10-2" data-line-number="2"></a>
<a class="sourceLine" id="cb10-3" data-line-number="3"><span class="ot">    (&lt;?&gt;) ::</span> <span class="dt">Maybe</span> a <span class="ot">-&gt;</span> a <span class="ot">-&gt;</span> a</a>
<a class="sourceLine" id="cb10-4" data-line-number="4">    <span class="dt">Just</span> a <span class="fu">&lt;?&gt;</span> _ <span class="fu">=</span> a</a>
<a class="sourceLine" id="cb10-5" data-line-number="5">    _ <span class="fu">&lt;?&gt;</span> a <span class="fu">=</span> a</a>
<a class="sourceLine" id="cb10-6" data-line-number="6"></a>
<a class="sourceLine" id="cb10-7" data-line-number="7"><span class="ot">    prjForce ::</span> (<span class="dt">Typeable1</span> t, <span class="dt">Member</span> t r) <span class="ot">=&gt;</span> <span class="dt">Union</span> r v <span class="ot">-&gt;</span> (t v <span class="ot">-&gt;</span> a) <span class="ot">-&gt;</span> a</a>
<a class="sourceLine" id="cb10-8" data-line-number="8">    prjForce u f <span class="fu">=</span> f <span class="fu">&lt;$&gt;</span> prj u <span class="fu">&lt;?&gt;</span> error <span class="st">&quot;prjForce with an invalid type&quot;</span></a></code></pre></div>
<p><code>prjForce</code> is really exactly what it says on the label, it’s a version of <code>prj</code> that throws an exception if we’re in the wrong state of <code>Union</code>.</p>
<p>Next is a way of unsafely rejiggering the type level list that <code>Union</code> is indexed over.</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb11-1" data-line-number="1"><span class="ot">    unsafeReUnion ::</span> <span class="dt">Union</span> r w <span class="ot">-&gt;</span> <span class="dt">Union</span> t w</a>
<a class="sourceLine" id="cb11-2" data-line-number="2">    unsafeReUnion (<span class="dt">Union</span> v) <span class="fu">=</span> <span class="dt">Union</span> v</a></code></pre></div>
<p>We need this for our last function, <code>decom</code>. This function partially unfolds our <code>Union</code> into an <code>Either</code></p>
<div class="sourceCode" id="cb12"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb12-1" data-line-number="1"><span class="ot">    decomp ::</span> <span class="dt">Typeable1</span> t <span class="ot">=&gt;</span> <span class="dt">Union</span> (t <span class="fu">:&gt;</span> r) v <span class="ot">-&gt;</span> <span class="dt">Either</span> (<span class="dt">Union</span> r v) (t v)</a>
<a class="sourceLine" id="cb12-2" data-line-number="2">    decomp u <span class="fu">=</span> <span class="dt">Right</span> <span class="fu">&lt;$&gt;</span> prj u <span class="fu">&lt;?&gt;</span> <span class="dt">Left</span> (unsafeReUnion u)</a></code></pre></div>
<p>This provides a way to actually do some sort of induction on <code>r</code> by breaking out each type piece by piece with some absurd case for when we don’t have <code>a :&gt; b</code>.</p>
<p>That about wraps up this little <code>Union</code> library, let’s move on to see how this is actually used.</p>
<h2 id="control.eff">Control.Eff</h2>
<p>Now let’s talk about the core of extensible-effects, <code>Control.Eff</code>. As always we’ll start by taking a look at the export list</p>
<div class="sourceCode" id="cb13"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb13-1" data-line-number="1">    <span class="kw">module</span> <span class="dt">Control.Eff</span>(</a>
<a class="sourceLine" id="cb13-2" data-line-number="2">                        <span class="dt">Eff</span> (<span class="fu">..</span>)</a>
<a class="sourceLine" id="cb13-3" data-line-number="3">                      , <span class="dt">VE</span> (<span class="fu">..</span>)</a>
<a class="sourceLine" id="cb13-4" data-line-number="4">                      , <span class="dt">Member</span></a>
<a class="sourceLine" id="cb13-5" data-line-number="5">                      , <span class="dt">SetMember</span></a>
<a class="sourceLine" id="cb13-6" data-line-number="6">                      , <span class="dt">Union</span></a>
<a class="sourceLine" id="cb13-7" data-line-number="7">                      , (<span class="fu">:&gt;</span>)</a>
<a class="sourceLine" id="cb13-8" data-line-number="8">                      , inj</a>
<a class="sourceLine" id="cb13-9" data-line-number="9">                      , prj</a>
<a class="sourceLine" id="cb13-10" data-line-number="10">                      , prjForce</a>
<a class="sourceLine" id="cb13-11" data-line-number="11">                      , decomp</a>
<a class="sourceLine" id="cb13-12" data-line-number="12">                      , send</a>
<a class="sourceLine" id="cb13-13" data-line-number="13">                      , admin</a>
<a class="sourceLine" id="cb13-14" data-line-number="14">                      , run</a>
<a class="sourceLine" id="cb13-15" data-line-number="15">                      , interpose</a>
<a class="sourceLine" id="cb13-16" data-line-number="16">                      , handleRelay</a>
<a class="sourceLine" id="cb13-17" data-line-number="17">                      , unsafeReUnion</a>
<a class="sourceLine" id="cb13-18" data-line-number="18">                      ) <span class="kw">where</span></a></code></pre></div>
<p>So right away we can see that we’re exporting stuff <code>Data.Union1</code> as well as several new things, including the infamous <code>Eff</code>.</p>
<p>The first definition we come across in this module is <code>VE</code>. <code>VE</code> is either a simple value or a <code>Union</code> applied to a <code>VE</code>!</p>
<div class="sourceCode" id="cb14"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb14-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">VE</span> r w <span class="fu">=</span> <span class="dt">Val</span> w <span class="fu">|</span> <span class="dt">E</span> <span class="fu">!</span>(<span class="dt">Union</span> r (<span class="dt">VE</span> r w))</a></code></pre></div>
<p>Right away we notice that “pure value or X” pattern we see with free monads and other abstractions over effects.</p>
<p>We also include a quick function to try to extract a pure value form <code>Val</code>s</p>
<div class="sourceCode" id="cb15"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb15-1" data-line-number="1"><span class="ot">    fromVal ::</span> <span class="dt">VE</span> r w <span class="ot">-&gt;</span> w</a>
<a class="sourceLine" id="cb15-2" data-line-number="2">    fromVal (<span class="dt">Val</span> w) <span class="fu">=</span> w</a>
<a class="sourceLine" id="cb15-3" data-line-number="3">    fromVal _ <span class="fu">=</span> error <span class="st">&quot;extensible-effects: fromVal was called on a non-terminal effect.&quot;</span></a></code></pre></div>
<p>Now we’ve finally reached the definition of <code>Eff</code>!</p>
<div class="sourceCode" id="cb16"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb16-1" data-line-number="1">    <span class="kw">newtype</span> <span class="dt">Eff</span> r a <span class="fu">=</span> <span class="dt">Eff</span> {<span class="ot"> runEff ::</span> forall w<span class="fu">.</span> (a <span class="ot">-&gt;</span> <span class="dt">VE</span> r w) <span class="ot">-&gt;</span> <span class="dt">VE</span> r w }</a></code></pre></div>
<p>So <code>Eff</code> bears a striking resemblance to <code>Cont</code>. There are two critical differences though, first is that we specialize our return type to something constructed with <code>VE r</code>. The second crucial difference is that by universally quantifying over <code>w</code> we sacrifice a lot of the power of <code>Cont</code>, including <code>callCC</code>!</p>
<p>Next in <code>Control.Eff</code> is the instances for <code>Eff</code></p>
<div class="sourceCode" id="cb17"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb17-1" data-line-number="1">    <span class="kw">instance</span> <span class="dt">Functor</span> (<span class="dt">Eff</span> r) <span class="kw">where</span></a>
<a class="sourceLine" id="cb17-2" data-line-number="2">        fmap f m <span class="fu">=</span> <span class="dt">Eff</span> <span class="fu">$</span> \k <span class="ot">-&gt;</span> runEff m (k <span class="fu">.</span> f)</a>
<a class="sourceLine" id="cb17-3" data-line-number="3">        <span class="ot">{-# INLINE fmap #-}</span></a>
<a class="sourceLine" id="cb17-4" data-line-number="4"></a>
<a class="sourceLine" id="cb17-5" data-line-number="5">    <span class="kw">instance</span> <span class="dt">Applicative</span> (<span class="dt">Eff</span> r) <span class="kw">where</span></a>
<a class="sourceLine" id="cb17-6" data-line-number="6">        pure <span class="fu">=</span> return</a>
<a class="sourceLine" id="cb17-7" data-line-number="7">        (<span class="fu">&lt;*&gt;</span>) <span class="fu">=</span> ap</a>
<a class="sourceLine" id="cb17-8" data-line-number="8"></a>
<a class="sourceLine" id="cb17-9" data-line-number="9">    <span class="kw">instance</span> <span class="dt">Monad</span> (<span class="dt">Eff</span> r) <span class="kw">where</span></a>
<a class="sourceLine" id="cb17-10" data-line-number="10">        return x <span class="fu">=</span> <span class="dt">Eff</span> <span class="fu">$</span> \k <span class="ot">-&gt;</span> k x</a>
<a class="sourceLine" id="cb17-11" data-line-number="11">        <span class="ot">{-# INLINE return #-}</span></a>
<a class="sourceLine" id="cb17-12" data-line-number="12"></a>
<a class="sourceLine" id="cb17-13" data-line-number="13">        m <span class="fu">&gt;&gt;=</span> f <span class="fu">=</span> <span class="dt">Eff</span> <span class="fu">$</span> \k <span class="ot">-&gt;</span> runEff m (\v <span class="ot">-&gt;</span> runEff (f v) k)</a>
<a class="sourceLine" id="cb17-14" data-line-number="14">        <span class="ot">{-# INLINE (&gt;&gt;=) #-}</span></a></code></pre></div>
<p>Notice that these are all really identical to <code>Cont</code>s instances. <code>Functor</code> adds a function to the head of the continuation. <code>Monad</code> dereferences <code>m</code> and feeds the result into <code>f</code>. Exactly as with <code>Cont</code>.</p>
<p>Next we can look at our primitive function for handling effects</p>
<div class="sourceCode" id="cb18"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb18-1" data-line-number="1"><span class="ot">    send ::</span> (forall w<span class="fu">.</span> (a <span class="ot">-&gt;</span> <span class="dt">VE</span> r w) <span class="ot">-&gt;</span> <span class="dt">Union</span> r (<span class="dt">VE</span> r w)) <span class="ot">-&gt;</span> <span class="dt">Eff</span> r a</a>
<a class="sourceLine" id="cb18-2" data-line-number="2">    send f <span class="fu">=</span> <span class="dt">Eff</span> (<span class="dt">E</span> <span class="fu">.</span> f)</a></code></pre></div>
<p>I must admit, this tripped me up for a while. Here’s how I read it, “provide a function, which when given a continuation for the rest of the program expecting an <code>a</code>, produces a side effecting <code>VE r w</code> and we’ll map that into <code>Eff</code>”.</p>
<p>Remember how <code>Union</code> holds functors? Well each of our effects must act like as a functor and wrap itself in that union. By being open, we get the “extensible” in extensible-effects.</p>
<p>Next we look at how to remove effects once they’ve been added to our set of effects. In mtl-land, this is similar to the collection of <code>runFooT</code> functions that are used to gradually strip a layer of transformers away.</p>
<p>The first step towards this is to transform the CPS-ed effectful computation <code>Eff</code>, into a more manageable form, <code>VE</code></p>
<div class="sourceCode" id="cb19"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb19-1" data-line-number="1"><span class="ot">    admin ::</span> <span class="dt">Eff</span> r w <span class="ot">-&gt;</span> <span class="dt">VE</span> r w</a>
<a class="sourceLine" id="cb19-2" data-line-number="2">    admin (<span class="dt">Eff</span> m) <span class="fu">=</span> m <span class="dt">Val</span></a></code></pre></div>
<p>This is a setup step so that we can traverse the “tree” of effects that our <code>Eff</code> monad built up for us.</p>
<p>Next, we know that we can take an <code>Eff</code> with <em>no</em> effects and unwrap it into a pure value. This is the “base case” for running an effectful computation.</p>
<div class="sourceCode" id="cb20"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb20-1" data-line-number="1"><span class="ot">    run ::</span> <span class="dt">Eff</span> () w <span class="ot">-&gt;</span> w</a>
<a class="sourceLine" id="cb20-2" data-line-number="2">    run <span class="fu">=</span> fromVal <span class="fu">.</span> admin</a></code></pre></div>
<p>Concerned readers may notice that we’re using a partial function, this is OK since the <code>E</code> case is “morally impossible” since there is no <code>t</code> so that <code>Member t ()</code> holds.</p>
<p>Next is the function to remove just one effect from an <code>Eff</code></p>
<div class="sourceCode" id="cb21"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb21-1" data-line-number="1"><span class="ot">    handleRelay ::</span> <span class="dt">Typeable1</span> t</a>
<a class="sourceLine" id="cb21-2" data-line-number="2">                <span class="ot">=&gt;</span> <span class="dt">Union</span> (t <span class="fu">:&gt;</span> r) v <span class="co">-- ^ Request</span></a>
<a class="sourceLine" id="cb21-3" data-line-number="3">                <span class="ot">-&gt;</span> (v <span class="ot">-&gt;</span> <span class="dt">Eff</span> r a)   <span class="co">-- ^ Relay the request</span></a>
<a class="sourceLine" id="cb21-4" data-line-number="4">                <span class="ot">-&gt;</span> (t v <span class="ot">-&gt;</span> <span class="dt">Eff</span> r a) <span class="co">-- ^ Handle the request of type t</span></a>
<a class="sourceLine" id="cb21-5" data-line-number="5">                <span class="ot">-&gt;</span> <span class="dt">Eff</span> r a</a>
<a class="sourceLine" id="cb21-6" data-line-number="6">    handleRelay u loop h <span class="fu">=</span> either passOn h <span class="fu">$</span> decomp u</a>
<a class="sourceLine" id="cb21-7" data-line-number="7">      <span class="kw">where</span> passOn u&#39; <span class="fu">=</span> send (<span class="fu">&lt;$&gt;</span> u&#39;) <span class="fu">&gt;&gt;=</span> loop</a></code></pre></div>
<p>Next to <code>send</code>, this function gave me the most trouble. The trick was to realize that that <code>decomp</code> will leave us in two cases.</p>
<ol type="1">
<li>Some effect producing a <code>v</code>, <code>Union r v</code></li>
<li>A <code>t</code> producing a <code>v</code>, <code>t v</code></li>
</ol>
<p>If we have a <code>t v</code>, then we’re all set since we know exactly how to map that to a <code>Eff r a</code> with <code>h</code>.</p>
<p>Otherwise we need to take this effect, add it back into our computation. <code>send (&lt;$&gt; u')</code> takes the rest of the computation, that continuation and feeds it the <code>v</code> that we know our effects produce. This gives us the type <code>Eff r v</code>, where that outer <code>Eff r</code> contains our most recent effect as well as everything else. Now to convert this to a <code>Eff r a</code> we need to transform that <code>v</code> to an <code>a</code>. The only way to do that is to use the supplied <code>loop</code> function so we just bind to that.</p>
<p>Last but not least is a function to modify an effect somewhere in our effectful computation. A <code>grep</code> reveals will see this later with things like <code>local</code> from <code>Control.Eff.Reader</code> for example.</p>
<p>To do this we want something like <code>handleRelay</code> but without removing <code>t</code> from <code>r</code>. We also need to generalize the type so that <code>t</code> can be <em>anywhere</em> in our. Otherwise we’ll have to prematurally solidify our stack of effects to use something like <code>modify</code>.</p>
<div class="sourceCode" id="cb22"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb22-1" data-line-number="1"><span class="ot">    interpose ::</span> (<span class="dt">Typeable1</span> t, <span class="dt">Functor</span> t, <span class="dt">Member</span> t r)</a>
<a class="sourceLine" id="cb22-2" data-line-number="2">              <span class="ot">=&gt;</span> <span class="dt">Union</span> r v</a>
<a class="sourceLine" id="cb22-3" data-line-number="3">              <span class="ot">-&gt;</span> (v <span class="ot">-&gt;</span> <span class="dt">Eff</span> r a)</a>
<a class="sourceLine" id="cb22-4" data-line-number="4">              <span class="ot">-&gt;</span> (t v <span class="ot">-&gt;</span> <span class="dt">Eff</span> r a)</a>
<a class="sourceLine" id="cb22-5" data-line-number="5">              <span class="ot">-&gt;</span> <span class="dt">Eff</span> r a</a>
<a class="sourceLine" id="cb22-6" data-line-number="6">    interpose u loop h <span class="fu">=</span> maybe (send (<span class="fu">&lt;$&gt;</span> u) <span class="fu">&gt;&gt;=</span> loop) h <span class="fu">$</span> prj u</a></code></pre></div>
<p>Now this is almost identical to <code>handleRelay</code> except instead of using <code>decomp</code> which will split off <code>t</code> and only works when <code>r ~ t :&gt; r'</code>, we use <code>prj</code>! This gives us a <code>Maybe</code> and since the type of <code>u</code> doesn’t need to change we just recycle that for the <code>send (&lt;$&gt; u) &gt;&gt;= loop</code> sequence.</p>
<p>That wraps up the core of extensible-effects, and I must admit that when writing this I was still quite confused as to actually <em>use</em> <code>Eff</code> to implement new effects. Reading a few examples really helped clear things up for me.</p>
<h2 id="control.eff.state">Control.Eff.State</h2>
<p>The <code>State</code> monad has always been the sort of classic monad example so I suppose we’ll start here.</p>
<div class="sourceCode" id="cb23"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb23-1" data-line-number="1">    <span class="kw">module</span> <span class="dt">Control.Eff.State.Lazy</span>( <span class="dt">State</span> (<span class="fu">..</span>)</a>
<a class="sourceLine" id="cb23-2" data-line-number="2">                                 , get</a>
<a class="sourceLine" id="cb23-3" data-line-number="3">                                 , put</a>
<a class="sourceLine" id="cb23-4" data-line-number="4">                                 , modify</a>
<a class="sourceLine" id="cb23-5" data-line-number="5">                                 , runState</a>
<a class="sourceLine" id="cb23-6" data-line-number="6">                                 , evalState</a>
<a class="sourceLine" id="cb23-7" data-line-number="7">                                 , execState</a>
<a class="sourceLine" id="cb23-8" data-line-number="8">                                 ) <span class="kw">where</span></a></code></pre></div>
<p>So we’re <em>not</em> reusing the <code>State</code> from <code>Control.Monad.State</code> but providing our own. It looks like</p>
<div class="sourceCode" id="cb24"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb24-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">State</span> s w <span class="fu">=</span> <span class="dt">State</span> (s <span class="ot">-&gt;</span> s) (s <span class="ot">-&gt;</span> w)</a></code></pre></div>
<p>So what is this supposed to do? Well that <code>s -&gt; w</code> looks a continuation of sorts, it takes the state <code>s</code>, and produces the resulting value. The <code>s -&gt; s</code> looks like something that <code>modify</code> should use.</p>
<p>Indeed this is the case</p>
<div class="sourceCode" id="cb25"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb25-1" data-line-number="1"><span class="ot">    modify ::</span> (<span class="dt">Typeable</span> s, <span class="dt">Member</span> (<span class="dt">State</span> s) r) <span class="ot">=&gt;</span> (s <span class="ot">-&gt;</span> s) <span class="ot">-&gt;</span> <span class="dt">Eff</span> r ()</a>
<a class="sourceLine" id="cb25-2" data-line-number="2">    modify f <span class="fu">=</span> send <span class="fu">$</span> \k <span class="ot">-&gt;</span> inj <span class="fu">$</span> <span class="dt">State</span> f <span class="fu">$</span> \_ <span class="ot">-&gt;</span> k ()</a>
<a class="sourceLine" id="cb25-3" data-line-number="3"></a>
<a class="sourceLine" id="cb25-4" data-line-number="4"><span class="ot">    put ::</span> (<span class="dt">Typeable</span> e, <span class="dt">Member</span> (<span class="dt">State</span> e) r) <span class="ot">=&gt;</span> e <span class="ot">-&gt;</span> <span class="dt">Eff</span> r ()</a>
<a class="sourceLine" id="cb25-5" data-line-number="5">    put <span class="fu">=</span> modify <span class="fu">.</span> const</a></code></pre></div>
<p>we grab the continuation from <code>send</code> and add a <code>State</code> effect on top which uses our modification function <code>s</code>. The continuation that <code>State</code> takes ignores the value it’s passed, the current state, and instead feeds the program computation the <code>()</code> it’s expecting.</p>
<p><code>get</code> is defined in a similar manner, but instead of modifying the state, we use State’s continuation to feed the program the current state.</p>
<div class="sourceCode" id="cb26"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb26-1" data-line-number="1"><span class="ot">    get ::</span> (<span class="dt">Typeable</span> e, <span class="dt">Member</span> (<span class="dt">State</span> e) r) <span class="ot">=&gt;</span> <span class="dt">Eff</span> r e</a>
<a class="sourceLine" id="cb26-2" data-line-number="2">    get <span class="fu">=</span> send (inj <span class="fu">.</span> <span class="dt">State</span> id)</a></code></pre></div>
<p>So we grab the continuation, feed it to a <code>State id</code> which won’t modify the state, and then inject that into our open union of effects.</p>
<p>Now that we have the API for working with states, let’s look at how to remove that effect.</p>
<div class="sourceCode" id="cb27"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb27-1" data-line-number="1"><span class="ot">    runState ::</span> <span class="dt">Typeable</span> s</a>
<a class="sourceLine" id="cb27-2" data-line-number="2">             <span class="ot">=&gt;</span> s                     <span class="co">-- ^ Initial state</span></a>
<a class="sourceLine" id="cb27-3" data-line-number="3">             <span class="ot">-&gt;</span> <span class="dt">Eff</span> (<span class="dt">State</span> s <span class="fu">:&gt;</span> r) w  <span class="co">-- ^ Effect incorporating State</span></a>
<a class="sourceLine" id="cb27-4" data-line-number="4">             <span class="ot">-&gt;</span> <span class="dt">Eff</span> r (s, w)          <span class="co">-- ^ Effect containing final state and a return value</span></a>
<a class="sourceLine" id="cb27-5" data-line-number="5">    runState s0 <span class="fu">=</span> loop s0 <span class="fu">.</span> admin <span class="kw">where</span></a>
<a class="sourceLine" id="cb27-6" data-line-number="6">     loop s (<span class="dt">Val</span> x) <span class="fu">=</span> return (s, x)</a>
<a class="sourceLine" id="cb27-7" data-line-number="7">     loop s (<span class="dt">E</span> u)   <span class="fu">=</span> handleRelay u (loop s) <span class="fu">$</span></a>
<a class="sourceLine" id="cb27-8" data-line-number="8">                           \(<span class="dt">State</span> t k) <span class="ot">-&gt;</span> <span class="kw">let</span> s&#39; <span class="fu">=</span> t s</a>
<a class="sourceLine" id="cb27-9" data-line-number="9">                                           <span class="kw">in</span> loop s&#39; (k s&#39;)</a></code></pre></div>
<p><code>runState</code> first preps our effect to be pattern matched on with <code>admin</code>. We then start <code>loop</code> with the initial state.</p>
<p><code>loop</code> has two components, if we have run into a value, then we don’t interpret any effects, just stick the state and value together and <code>return</code> them.</p>
<p>If we do have an effect, we use <code>handleRelay</code> to split out the <code>State s</code> from our effects. To handle the case where we get a <code>VE w</code>, we just <code>loop</code> with the current state. However, if we get a <code>State t k</code>, we update the state with <code>t</code> and pass the continuation <code>k</code>.</p>
<p>From <code>runState</code> <code>evalState</code> and <code>execState</code>.</p>
<div class="sourceCode" id="cb28"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb28-1" data-line-number="1"><span class="ot">    evalState ::</span> <span class="dt">Typeable</span> s <span class="ot">=&gt;</span> s <span class="ot">-&gt;</span> <span class="dt">Eff</span> (<span class="dt">State</span> s <span class="fu">:&gt;</span> r) w <span class="ot">-&gt;</span> <span class="dt">Eff</span> r w</a>
<a class="sourceLine" id="cb28-2" data-line-number="2">    evalState s <span class="fu">=</span> fmap snd <span class="fu">.</span> runState s</a>
<a class="sourceLine" id="cb28-3" data-line-number="3"></a>
<a class="sourceLine" id="cb28-4" data-line-number="4"><span class="ot">    execState ::</span> <span class="dt">Typeable</span> s <span class="ot">=&gt;</span> s <span class="ot">-&gt;</span> <span class="dt">Eff</span> (<span class="dt">State</span> s <span class="fu">:&gt;</span> r) w <span class="ot">-&gt;</span> <span class="dt">Eff</span> r s</a>
<a class="sourceLine" id="cb28-5" data-line-number="5">    execState s <span class="fu">=</span> fmap fst <span class="fu">.</span> runState s</a></code></pre></div>
<p>That wraps up the interface for <code>Control.Eff.State</code>. The nice bit is this makes it a lot clearer how to use <code>send</code>, <code>handleRelay</code> and a few other functions from the core.</p>
<h2 id="control.eff.reader">Control.Eff.Reader</h2>
<p>Now we’re on to <code>Reader</code>. The interesting thing here is that <code>local</code> highlights how to use <code>interpose</code> properly.</p>
<p>As always, we start by looking at what exactly this module provides</p>
<div class="sourceCode" id="cb29"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb29-1" data-line-number="1">    <span class="kw">module</span> <span class="dt">Control.Eff.Reader.Lazy</span>( <span class="dt">Reader</span> (<span class="fu">..</span>)</a>
<a class="sourceLine" id="cb29-2" data-line-number="2">                                  , ask</a>
<a class="sourceLine" id="cb29-3" data-line-number="3">                                  , local</a>
<a class="sourceLine" id="cb29-4" data-line-number="4">                                  , reader</a>
<a class="sourceLine" id="cb29-5" data-line-number="5">                                  , runReader</a>
<a class="sourceLine" id="cb29-6" data-line-number="6">                                  ) <span class="kw">where</span></a></code></pre></div>
<p>The definition of <code>Reader</code> is refreshingly simple</p>
<div class="sourceCode" id="cb30"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb30-1" data-line-number="1">    <span class="kw">newtype</span> <span class="dt">Reader</span> e v <span class="fu">=</span> <span class="dt">Reader</span> (e <span class="ot">-&gt;</span> v)</a></code></pre></div>
<p>Keen readers will note that this is just half of the <code>State</code> definition which makes sense; <code>Reader</code> is half of <code>State</code>.</p>
<p><code>ask</code> is defined almost identically to <code>get</code></p>
<div class="sourceCode" id="cb31"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb31-1" data-line-number="1"><span class="ot">    ask ::</span> (<span class="dt">Typeable</span> e, <span class="dt">Member</span> (<span class="dt">Reader</span> e) r) <span class="ot">=&gt;</span> <span class="dt">Eff</span> r e</a>
<a class="sourceLine" id="cb31-2" data-line-number="2">    ask <span class="fu">=</span> send (inj <span class="fu">.</span> <span class="dt">Reader</span>)</a></code></pre></div>
<p>We just feed the continuation for the program into <code>Reader</code>. A simple wrapper over this gives our equivalent of <code>reads</code></p>
<div class="sourceCode" id="cb32"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb32-1" data-line-number="1"><span class="ot">    reader ::</span> (<span class="dt">Typeable</span> e, <span class="dt">Member</span> (<span class="dt">Reader</span> e) r) <span class="ot">=&gt;</span> (e <span class="ot">-&gt;</span> a) <span class="ot">-&gt;</span> <span class="dt">Eff</span> r a</a>
<a class="sourceLine" id="cb32-2" data-line-number="2">    reader f <span class="fu">=</span> f <span class="fu">&lt;$&gt;</span> ask</a></code></pre></div>
<p>Next up is <code>local</code>, which is the most interesting bit of this module.</p>
<div class="sourceCode" id="cb33"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb33-1" data-line-number="1"><span class="ot">    local ::</span> (<span class="dt">Typeable</span> e, <span class="dt">Member</span> (<span class="dt">Reader</span> e) r)</a>
<a class="sourceLine" id="cb33-2" data-line-number="2">          <span class="ot">=&gt;</span> (e <span class="ot">-&gt;</span> e)</a>
<a class="sourceLine" id="cb33-3" data-line-number="3">          <span class="ot">-&gt;</span> <span class="dt">Eff</span> r a</a>
<a class="sourceLine" id="cb33-4" data-line-number="4">          <span class="ot">-&gt;</span> <span class="dt">Eff</span> r a</a>
<a class="sourceLine" id="cb33-5" data-line-number="5">    local f m <span class="fu">=</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb33-6" data-line-number="6">      e <span class="ot">&lt;-</span> f <span class="fu">&lt;$&gt;</span> ask</a>
<a class="sourceLine" id="cb33-7" data-line-number="7">      <span class="kw">let</span> loop (<span class="dt">Val</span> x) <span class="fu">=</span> return x</a>
<a class="sourceLine" id="cb33-8" data-line-number="8">          loop (<span class="dt">E</span> u) <span class="fu">=</span> interpose u loop (\(<span class="dt">Reader</span> k) <span class="ot">-&gt;</span> loop (k e))</a>
<a class="sourceLine" id="cb33-9" data-line-number="9">      loop (admin m)</a></code></pre></div>
<p>So <code>local</code> starts by grabbing the view of the environment we’re interested in, <code>e</code>. From there we define our worker function which looks a lot like <code>runState</code>. The key difference is that instead of using <code>handleRelay</code> we use <code>interpose</code> to replace each <code>Reader</code> effect with the appropriate environment. Remember that <code>interpose</code> is not going to remove <code>Reader</code> from the set of effects, just update each <code>Reader</code> effect in the current computation.</p>
<p>Finally, we simply rejigger the computation with <code>admin</code> and feed it to <code>loop</code>.</p>
<p>In fact, this is very similar to how <code>runReader</code> works!</p>
<div class="sourceCode" id="cb34"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb34-1" data-line-number="1"><span class="ot">    runReader ::</span> <span class="dt">Typeable</span> e <span class="ot">=&gt;</span> <span class="dt">Eff</span> (<span class="dt">Reader</span> e <span class="fu">:&gt;</span> r) w <span class="ot">-&gt;</span> e <span class="ot">-&gt;</span> <span class="dt">Eff</span> r w</a>
<a class="sourceLine" id="cb34-2" data-line-number="2">    runReader m e <span class="fu">=</span> loop (admin m)</a>
<a class="sourceLine" id="cb34-3" data-line-number="3">      <span class="kw">where</span></a>
<a class="sourceLine" id="cb34-4" data-line-number="4">        loop (<span class="dt">Val</span> x) <span class="fu">=</span> return x</a>
<a class="sourceLine" id="cb34-5" data-line-number="5">        loop (<span class="dt">E</span> u) <span class="fu">=</span> handleRelay u loop (\(<span class="dt">Reader</span> k) <span class="ot">-&gt;</span> loop (k e))</a></code></pre></div>
<h2 id="control.eff.lift">Control.Eff.Lift</h2>
<p>Now between <code>Control.Eff.Reader</code> and <code>Control.Eff.State</code> I felt I had a pretty good handle on most of what I’d read in extensible-effects. There was just one remaining loose end: <code>SetMember</code>. Don’t remember what that was? It was a class in <code>Data.OpenUnion1</code> that was conspicuously absent of detail or use.</p>
<p>I finally found where it seemed to be used! In <code>Control.Eff.Lift</code>.</p>
<p>First let’s poke at the exports of his module</p>
<div class="sourceCode" id="cb35"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb35-1" data-line-number="1">    <span class="kw">module</span> <span class="dt">Control.Eff.Lift</span>( <span class="dt">Lift</span> (<span class="fu">..</span>)</a>
<a class="sourceLine" id="cb35-2" data-line-number="2">                           , lift</a>
<a class="sourceLine" id="cb35-3" data-line-number="3">                           , runLift</a>
<a class="sourceLine" id="cb35-4" data-line-number="4">                           ) <span class="kw">where</span></a></code></pre></div>
<p>This module is designed to lift an arbitrary monad into the world of effects. There’s a caveat though, since monads aren’t necessarily commutative, the order in which we run them in is very important. Imagine for example the difference between <code>IO (m a)</code> and <code>m (IO a)</code>.</p>
<p>So to ensure that <code>Eff</code> can support lifted monads we have to do some evil things. First we must require that we never have to lifted monads and we always run the monad last. This is a little icky but it’s usefulness outweighs such ugliness.</p>
<p>To ensure condition 1, we need <code>SetMember</code>.</p>
<div class="sourceCode" id="cb36"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb36-1" data-line-number="1">    <span class="kw">instance</span> <span class="dt">SetMember</span> <span class="dt">Lift</span> (<span class="dt">Lift</span> m) (<span class="dt">Lift</span> m <span class="fu">:&gt;</span> ())</a></code></pre></div>
<p>So we define a new instance of <code>SetMember</code>. Basically this says that any <code>Lift</code> is a <code>SetMember ... r</code> iff <code>Lift m</code> is the last item in <code>r</code>.</p>
<p>To ensure condition number two we define <code>runLift</code> with the more restrictive type</p>
<div class="sourceCode" id="cb37"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb37-1" data-line-number="1"><span class="ot">    runLift ::</span> (<span class="dt">Monad</span> m, <span class="dt">Typeable1</span> m) <span class="ot">=&gt;</span> <span class="dt">Eff</span> (<span class="dt">Lift</span> m <span class="fu">:&gt;</span> ()) w <span class="ot">-&gt;</span> m w</a></code></pre></div>
<p>We can now look into exactly how <code>Lift</code> is defined.</p>
<div class="sourceCode" id="cb38"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb38-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">Lift</span> m v <span class="fu">=</span> forall a<span class="fu">.</span> <span class="dt">Lift</span> (m a) (a <span class="ot">-&gt;</span> v)</a></code></pre></div>
<p>So this <code>Lift</code> acts sort of like a “suspended bind”. We postpone actually binding the monad and simulate doing so with a continuation <code>a -&gt; v</code>.</p>
<p>We can define our one operation with <code>Lift</code>, <code>lift</code>.</p>
<div class="sourceCode" id="cb39"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb39-1" data-line-number="1"><span class="ot">    lift ::</span> (<span class="dt">Typeable1</span> m, <span class="dt">SetMember</span> <span class="dt">Lift</span> (<span class="dt">Lift</span> m) r) <span class="ot">=&gt;</span> m a <span class="ot">-&gt;</span> <span class="dt">Eff</span> r a</a>
<a class="sourceLine" id="cb39-2" data-line-number="2">    lift m <span class="fu">=</span> send (inj <span class="fu">.</span> <span class="dt">Lift</span> m)</a></code></pre></div>
<p>This works by suspending the rest of the program in a our faux binding to be unwrapped later in <code>runLift</code>.</p>
<div class="sourceCode" id="cb40"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb40-1" data-line-number="1"><span class="ot">    runLift ::</span> (<span class="dt">Monad</span> m, <span class="dt">Typeable1</span> m) <span class="ot">=&gt;</span> <span class="dt">Eff</span> (<span class="dt">Lift</span> m <span class="fu">:&gt;</span> ()) w <span class="ot">-&gt;</span> m w</a>
<a class="sourceLine" id="cb40-2" data-line-number="2">    runLift m <span class="fu">=</span> loop (admin m) <span class="kw">where</span></a>
<a class="sourceLine" id="cb40-3" data-line-number="3">     loop (<span class="dt">Val</span> x) <span class="fu">=</span> return x</a>
<a class="sourceLine" id="cb40-4" data-line-number="4">     loop (<span class="dt">E</span> u) <span class="fu">=</span> prjForce u <span class="fu">$</span> \(<span class="dt">Lift</span> m&#39; k) <span class="ot">-&gt;</span> m&#39; <span class="fu">&gt;&gt;=</span> loop <span class="fu">.</span> k</a></code></pre></div>
<p>The one interesting difference between this function and the rest of the run functions we’ve seen is that here we use <code>prjForce</code>. The reason for this is that we know that <code>r</code> is just <code>Lift m :&gt; ()</code>. This drastically simplifies the process and means all we’re essentially doing is transforming each <code>Lift</code> into <code>&gt;&gt;=</code>.</p>
<p>That wraps up our tour of the module and with it, extensible-effects.</p>
<h2 id="wrap-up">Wrap Up</h2>
<p>This post turned out a lot longer than I’d expected, but I think it was worth it. We’ve gone through the coroutine/continuation based core of extensible-effects and walked through a few different examples of how to actually use them.</p>
<p>If you’re still having some trouble putting the pieces together, the rest of extensible effects is a great collection of useful examples of building effects.</p>
<p>I hope you had as much fun as I did with this one!</p>
<p><em>Thanks to Erik Rantapaa a much longer post than I led him to believe</em></p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Tue, 15 Jul 2014 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2014-07-15-reading-extensible-effects.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Examining Hackage: logict</title>
    <link>http://jozefg.bitbucket.org/posts/2014-07-10-reading-logict.html</link>
    <description><![CDATA[<div class="info">
    Posted on July 10, 2014
    
</div>
<div class="info">
    
    Tags: <a href="/tags/haskell.html">haskell</a>
    
</div>

<p>One of my oldest habits with programming is reading other people’s code. I’ve been doing it almost since I started programming. For the last two years that habit has been focused on Hackage. Today I was reading the source code to the “logic programming monad” provided by <a href="http://hackage.haskell.org/package/logict"><code>logict</code></a> and wanted to blog about how I go about reading new Haskell code.</p>
<p>This time the code was pretty tiny, <code>find . -name *.hs | xargs wc -l</code> reveals two files with just under 400 lines of code! <code>logict</code> also only has two dependencies, base and the mtl, so there’s not a big worry of unfamiliar libraries.</p>
<h2 id="setting-up">Setting Up</h2>
<p>It’s a lot easier to read this post if you have the source for logict on hand. To grab it, use <code>cabal get</code>. My setup is something like</p>
<pre><code>~ $ cabal get logict
~ $ cd logict-0.6.0.2
~/logict-0.6.0.2 $ cabal sandbox init
~/logict-0.6.0.2 $ cabal install --only-dependencies</code></pre>
<h2 id="poking-around">Poking Around</h2>
<p>I’m somewhat ashamed to admit that I use pretty primitive tooling for exploring a new codebase, it’s <code>grep</code> and <code>find</code> all the way! If you use a fancy IDE, perhaps you can just skip this section and take a moment to sit back and feel high-tech.</p>
<p>First things first is to figure out what Haskell files are here. It can be different than what’s listed on Hackage since often libraries don’t export external files.</p>
<pre><code>~/logict-0.6.0.2 $ find . -name *.hs
  ./dist/build/autogen/Paths_logict.hs
  ./Control/Monad/Logic.hs
  ./Control/Monad/Logic/Class.hs</code></pre>
<p>Alright, there’s two source file and one sitting in dist. The dist one is almost certainly just cabal auto-gened stuff that we don’t care about.</p>
<p>It also appears that there’s no <code>src</code> directory and every module is publicly exported! This means that we only have two modules to worry about.</p>
<p>The next thing to figure out is which to read first. In this case the choice is simple: greping for imports with</p>
<pre><code>grep &quot;import&quot; -r Control</code></pre>
<p>reveals that <code>Control.Monad.Logic</code> imports <code>Control.Monad.Logic.Class</code> so we start with <code>*.Class</code>.</p>
<h2 id="reading-control.monad.logic.class">Reading <code>Control.Monad.Logic.Class</code></h2>
<p>Alright! Now it’s actually time to start reading code.</p>
<p>The first thing that jumps out is the export list</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" data-line-number="1">    <span class="kw">module</span> <span class="dt">Control.Monad.Logic.Class</span> (<span class="dt">MonadLogic</span>(<span class="fu">..</span>), reflect, lnot) <span class="kw">where</span></a></code></pre></div>
<p>Alright, so we’re exporting everything from a class <code>MonadLogic</code>, as well as two functions <code>reflect</code> and <code>lnot</code>. Let’s go figure out what <code>MonadLogic</code> is.</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb5-1" data-line-number="1">    <span class="kw">class</span> (<span class="dt">MonadPlus</span> m) <span class="ot">=&gt;</span> <span class="dt">MonadLogic</span> m <span class="kw">where</span></a>
<a class="sourceLine" id="cb5-2" data-line-number="2"><span class="ot">      msplit     ::</span> m a <span class="ot">-&gt;</span> m (<span class="dt">Maybe</span> (a, m a))</a>
<a class="sourceLine" id="cb5-3" data-line-number="3"><span class="ot">      interleave ::</span> m a <span class="ot">-&gt;</span> m a <span class="ot">-&gt;</span> m a</a>
<a class="sourceLine" id="cb5-4" data-line-number="4"><span class="ot">      (&gt;&gt;-)      ::</span> m a <span class="ot">-&gt;</span> (a <span class="ot">-&gt;</span> m b) <span class="ot">-&gt;</span> m b</a>
<a class="sourceLine" id="cb5-5" data-line-number="5"><span class="ot">      ifte       ::</span> m a <span class="ot">-&gt;</span> (a <span class="ot">-&gt;</span> m b) <span class="ot">-&gt;</span> m b <span class="ot">-&gt;</span> m b</a>
<a class="sourceLine" id="cb5-6" data-line-number="6"><span class="ot">      once       ::</span> m a <span class="ot">-&gt;</span> m a</a></code></pre></div>
<p>The fact that this depends on <code>MonadPlus</code> is pretty significant. Since most classes don’t require this I’m going to assume that it’s fairly key to either the implementation of some of these methods or to using them. Similar to how <code>Monoid</code> is critical to <code>Writer</code>.</p>
<p>The docs make it pretty clear what each member of this class does</p>
<ul>
<li><p><code>msplit</code></p>
<p>Take a local computation and split it into it’s first result and another computation that computes the rest.</p></li>
<li><p><code>interleave</code></p>
<p>This is the key difference between <code>MonadLogic</code> and <code>[]</code>. <code>interleave</code> gives fair choice between two computation. This means that every result that appears in finitely many applications of <code>msplit</code> for some <code>a</code> and <code>b</code>, will appear in finitely many applications of <code>msplit</code> to <code>interleave a b</code>.</p></li>
<li><p><code>&gt;&gt;-</code></p>
<p><code>&gt;&gt;-</code> is similar to <code>interleave</code>. Consider some code like</p>
<pre><code>  (a &gt;&gt;= k) `mplus` (b &gt;&gt;= k)</code></pre>
<p>This is equivalent to <code>mplus a b &gt;&gt;= k</code>, but has different characteristics since <code>&gt;&gt;=</code> might never terminate. <code>&gt;&gt;-</code> is described as “considering both sides of the disjunction”.</p>
<p>I have absolutely no idea what that means.. hopefully it’ll be clearer once we look at some implementations.</p></li>
<li><p><code>ifte</code></p>
<p>This is the equivalent of Prolog’s soft cut. We poke a logical computation and if it <em>can</em> succeed at all, then we feed it into the success computation, otherwise we’ll feed return the failure case.</p></li>
<li><p><code>once</code></p>
<p><code>once</code> is clever combinator to prevent backtracking. It will grab the first result from a computation, wrap it up and return it. This prevents backtracking further on the original computation.</p></li>
</ul>
<p>Now the docs also state that everything is derivable from <code>msplit</code>. These implementations look like</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb7-1" data-line-number="1">    interleave m1 m2 <span class="fu">=</span> msplit m1 <span class="fu">&gt;&gt;=</span></a>
<a class="sourceLine" id="cb7-2" data-line-number="2">                        maybe m2 (\(a, m1&#39;) <span class="ot">-&gt;</span> return a <span class="ot">`mplus`</span> interleave m2 m1&#39;)</a>
<a class="sourceLine" id="cb7-3" data-line-number="3"></a>
<a class="sourceLine" id="cb7-4" data-line-number="4">    m <span class="fu">&gt;&gt;-</span> f <span class="fu">=</span> <span class="kw">do</span> (a, m&#39;) <span class="ot">&lt;-</span> maybe mzero return <span class="fu">=&lt;&lt;</span> msplit m</a>
<a class="sourceLine" id="cb7-5" data-line-number="5">                 interleave (f a) (m&#39; <span class="fu">&gt;&gt;-</span> f)</a>
<a class="sourceLine" id="cb7-6" data-line-number="6"></a>
<a class="sourceLine" id="cb7-7" data-line-number="7">    ifte t th el <span class="fu">=</span> msplit t <span class="fu">&gt;&gt;=</span> maybe el (\(a,m) <span class="ot">-&gt;</span> th a <span class="ot">`mplus`</span> (m <span class="fu">&gt;&gt;=</span> th))</a>
<a class="sourceLine" id="cb7-8" data-line-number="8"></a>
<a class="sourceLine" id="cb7-9" data-line-number="9">    once m <span class="fu">=</span> <span class="kw">do</span> (a, _) <span class="ot">&lt;-</span> maybe mzero return <span class="fu">=&lt;&lt;</span> msplit m</a>
<a class="sourceLine" id="cb7-10" data-line-number="10">                return a</a></code></pre></div>
<p>The first thing I notice looking at interleave is that it kinda looks like</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb8-1" data-line-number="1"><span class="ot">    interleave&#39; ::</span> [a] <span class="ot">-&gt;</span> [a] <span class="ot">-&gt;</span> [a]</a>
<a class="sourceLine" id="cb8-2" data-line-number="2">    interleave&#39; (x<span class="fu">:</span>xs) ys <span class="fu">=</span> x <span class="fu">:</span> interleave&#39; ys xs</a>
<a class="sourceLine" id="cb8-3" data-line-number="3">    interleave _ ys       <span class="fu">=</span> ys</a></code></pre></div>
<p>This makes sense, since this will fairly split between <code>xs</code> and <code>ys</code> just like <code>interleave</code> is supposed to. Here <code>msplit</code> is like pattern matching, <code>mplus</code> is <code>:</code>, and we have to sprinkle some <code>return</code> in there for kicks and giggles.</p>
<p>Now about this mysterious <code>&gt;&gt;-</code>, the biggest difference is that each <code>f a</code> is <code>interleaved</code>, rather than <code>mplus</code>-ed. This should mean that it can be fairly split between our first result, <code>f a</code> and the rest of them <code>m' &gt;&gt;- f</code>. Now if we can do something like</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb9-1" data-line-number="1">    (m <span class="fu">&gt;&gt;-</span> f) <span class="ot">`interleave`</span> (m&#39; <span class="fu">&gt;&gt;-</span> f)</a></code></pre></div>
<p>Should have nice and fair behavior.</p>
<p>The next two are fairly clear, <code>ifte</code> splits it’s computation, and if it can it feeds the whole stinking thing <code>return a</code>mplus<code>m'</code> to the success computation, otherwise it just returns the failure computation. Nothing stunning.</p>
<p><code>once</code> is my favorite function. To prevent backtracking all we do is grab the first result and <code>return</code> it.</p>
<p>So that takes care of <code>MonadTrans</code>. The next thing to worry about are these two functions <code>reflect</code> and <code>lnot</code>.</p>
<p><code>reflect</code> confirms my suspicion that the dual of <code>msplit</code> is <code>mplus (return a) m'</code>.</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb10-1" data-line-number="1"><span class="ot">    reflect ::</span> <span class="dt">MonadLogic</span> m <span class="ot">=&gt;</span> <span class="dt">Maybe</span> (a, m a) <span class="ot">-&gt;</span> m a</a>
<a class="sourceLine" id="cb10-2" data-line-number="2">    reflect <span class="dt">Nothing</span> <span class="fu">=</span> mzero</a>
<a class="sourceLine" id="cb10-3" data-line-number="3">    reflect (<span class="dt">Just</span> (a, m)) <span class="fu">=</span> return a <span class="ot">`mplus`</span> m</a></code></pre></div>
<p>The next function <code>lnot</code> negates a logical computation. Now, this is a little misleading because the negated computation either produces one value, <code>()</code>, or is <code>mzero</code> and produces nothing. This is easily accomplished with <code>ifte</code> and <code>once</code></p>
<div class="sourceCode" id="cb11"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb11-1" data-line-number="1"><span class="ot">    lnot ::</span> <span class="dt">MonadLogic</span> m <span class="ot">=&gt;</span> m a <span class="ot">-&gt;</span> m ()</a>
<a class="sourceLine" id="cb11-2" data-line-number="2">    lnot m <span class="fu">=</span> ifte (once m) (const mzero) (return ())</a></code></pre></div>
<p>That takes care of most of this file. What’s left is a bunch of instances for monad transformers for <code>MonadTrans</code>. There’s nothing to interesting in them so I won’t talk about them here. It might be worth glancing at the code if you’re interested.</p>
<p>One slightly odd thing I’m noticing is that each class implements <em>all</em> the methods, rather than just <code>msplit</code>. This seems a bit odd.. I guess the default implementations are significantly slower? Perhaps some benchmarking is in order.</p>
<h2 id="control.monad.logic">Control.Monad.Logic</h2>
<p>Now that we’ve finished with Control.Monad.Logic.Class, let’s move on to the main file.</p>
<p>Now we finally see the definition of <code>LogicT</code></p>
<div class="sourceCode" id="cb12"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb12-1" data-line-number="1">    <span class="kw">newtype</span> <span class="dt">LogicT</span> m a <span class="fu">=</span></a>
<a class="sourceLine" id="cb12-2" data-line-number="2">        <span class="dt">LogicT</span> {<span class="ot"> unLogicT ::</span> forall r<span class="fu">.</span> (a <span class="ot">-&gt;</span> m r <span class="ot">-&gt;</span> m r) <span class="ot">-&gt;</span> m r <span class="ot">-&gt;</span> m r }</a></code></pre></div>
<p>I have no idea how this works, but I’m guessing that this is a church version of <code>[a]</code> specialized to some <code>m</code>. Remember that the church version of <code>[a]</code> is</p>
<div class="sourceCode" id="cb13"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb13-1" data-line-number="1">    <span class="kw">type</span> <span class="dt">CList</span> a <span class="fu">=</span> forall r<span class="fu">.</span> (a <span class="ot">-&gt;</span> r <span class="ot">-&gt;</span> r) <span class="ot">-&gt;</span> r <span class="ot">-&gt;</span> r</a></code></pre></div>
<p>Now what’s interesting here is that the church version is strongly connected to how CPSed code works. We could than imagine that <code>mplus</code> works like <code>cons</code> for church lists and yields more and more results. But again, this is just speculation.</p>
<p>This suspicion is confirmed by the functions to extract values out of a <code>LogicT</code> computation</p>
<div class="sourceCode" id="cb14"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb14-1" data-line-number="1"><span class="ot">    observeT ::</span> <span class="dt">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">LogicT</span> m a <span class="ot">-&gt;</span> m a</a>
<a class="sourceLine" id="cb14-2" data-line-number="2">    observeT lt <span class="fu">=</span> unLogicT lt (const <span class="fu">.</span> return) (fail <span class="st">&quot;No answer.&quot;</span>)</a>
<a class="sourceLine" id="cb14-3" data-line-number="3"></a>
<a class="sourceLine" id="cb14-4" data-line-number="4"><span class="ot">    observeAllT ::</span> <span class="dt">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">LogicT</span> m a <span class="ot">-&gt;</span> m [a]</a>
<a class="sourceLine" id="cb14-5" data-line-number="5">    observeAllT m <span class="fu">=</span> unLogicT m (liftM <span class="fu">.</span> (<span class="fu">:</span>)) (return [])</a>
<a class="sourceLine" id="cb14-6" data-line-number="6"></a>
<a class="sourceLine" id="cb14-7" data-line-number="7"><span class="ot">    observeManyT ::</span> <span class="dt">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">LogicT</span> m a <span class="ot">-&gt;</span> m [a]</a>
<a class="sourceLine" id="cb14-8" data-line-number="8">    observeManyT n m</a>
<a class="sourceLine" id="cb14-9" data-line-number="9">        <span class="fu">|</span> n <span class="fu">&lt;=</span> <span class="dv">0</span> <span class="fu">=</span> return []</a>
<a class="sourceLine" id="cb14-10" data-line-number="10">        <span class="fu">|</span> n <span class="fu">==</span> <span class="dv">1</span> <span class="fu">=</span> unLogicT m (\a _ <span class="ot">-&gt;</span> return [a]) (return [])</a>
<a class="sourceLine" id="cb14-11" data-line-number="11">        <span class="fu">|</span> otherwise <span class="fu">=</span> unLogicT (msplit m) sk (return [])</a>
<a class="sourceLine" id="cb14-12" data-line-number="12">     <span class="kw">where</span></a>
<a class="sourceLine" id="cb14-13" data-line-number="13">     sk <span class="dt">Nothing</span> _ <span class="fu">=</span> return []</a>
<a class="sourceLine" id="cb14-14" data-line-number="14">     sk (<span class="dt">Just</span> (a, m&#39;)) _ <span class="fu">=</span> (a<span class="fu">:</span>) <span class="ot">`liftM`</span> observeManyT (n<span class="fu">-</span><span class="dv">1</span>) m&#39;</a></code></pre></div>
<p><code>observeT</code> grabs the <code>a</code> from the success continuation and if no result is returned than it will evaluate <code>fail &quot;No Answer</code> which looks like the failure continuation! Looks like out suspicion is confirmed, we’re dealing with monadic church lists or some other permutation of those buzzwords.</p>
<p>Somehow in a package partially designed by Oleg I’m not surprised to find continuations :)</p>
<p><code>observeAllT</code> is quite similar, notice that we take advantage of the fact that <code>r</code> is universally quantified to instantiate it to <code>a</code>. This quantification is also used in <code>observeManyT</code>. This quantification also prevents any <code>LogicT</code> from taking advantage of the return type to do evil things with returning random values that happen to match the return type. This is what’s possible with <code>ContT</code> for example.</p>
<p>Now we have the standard specialization and smart constructor for the non-transformer version.</p>
<div class="sourceCode" id="cb15"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb15-1" data-line-number="1">    <span class="kw">type</span> <span class="dt">Logic</span> <span class="fu">=</span> <span class="dt">LogicT</span> <span class="dt">Identity</span></a>
<a class="sourceLine" id="cb15-2" data-line-number="2"></a>
<a class="sourceLine" id="cb15-3" data-line-number="3"><span class="ot">    logic ::</span> (forall r<span class="fu">.</span> (a <span class="ot">-&gt;</span> r <span class="ot">-&gt;</span> r) <span class="ot">-&gt;</span> r <span class="ot">-&gt;</span> r) <span class="ot">-&gt;</span> <span class="dt">Logic</span> a</a>
<a class="sourceLine" id="cb15-4" data-line-number="4">    logic f <span class="fu">=</span> <span class="dt">LogicT</span> <span class="fu">$</span> \k <span class="ot">-&gt;</span> <span class="dt">Identity</span> <span class="fu">.</span></a>
<a class="sourceLine" id="cb15-5" data-line-number="5">                             f (\a <span class="ot">-&gt;</span> runIdentity <span class="fu">.</span> k a <span class="fu">.</span> <span class="dt">Identity</span>) <span class="fu">.</span></a>
<a class="sourceLine" id="cb15-6" data-line-number="6">                             runIdentity</a></code></pre></div>
<p>Look familiar? Now we can inject real church lists into a <code>Logic</code> computation. I suppose this shouldn’t be surprising since <code>[a]</code> functions like a slightly broken <code>Logic a</code>, without any sharing or soft cut.</p>
<p>Now we repeat all the <code>observe*</code> functions for <code>Logic</code>, I’ll omit these since they’re implementations are exactly as you’d expect and not interesting.</p>
<p>Next we have a few type class instances</p>
<div class="sourceCode" id="cb16"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb16-1" data-line-number="1">    <span class="kw">instance</span> <span class="dt">Functor</span> (<span class="dt">LogicT</span> f) <span class="kw">where</span></a>
<a class="sourceLine" id="cb16-2" data-line-number="2">        fmap f lt <span class="fu">=</span> <span class="dt">LogicT</span> <span class="fu">$</span> \sk fk <span class="ot">-&gt;</span> unLogicT lt (sk <span class="fu">.</span> f) fk</a>
<a class="sourceLine" id="cb16-3" data-line-number="3"></a>
<a class="sourceLine" id="cb16-4" data-line-number="4">    <span class="kw">instance</span> <span class="dt">Applicative</span> (<span class="dt">LogicT</span> f) <span class="kw">where</span></a>
<a class="sourceLine" id="cb16-5" data-line-number="5">        pure a <span class="fu">=</span> <span class="dt">LogicT</span> <span class="fu">$</span> \sk fk <span class="ot">-&gt;</span> sk a fk</a>
<a class="sourceLine" id="cb16-6" data-line-number="6">        f <span class="fu">&lt;*&gt;</span> a <span class="fu">=</span> <span class="dt">LogicT</span> <span class="fu">$</span> \sk fk <span class="ot">-&gt;</span> unLogicT f (\g fk&#39; <span class="ot">-&gt;</span> unLogicT a (sk <span class="fu">.</span> g) fk&#39;) fk</a>
<a class="sourceLine" id="cb16-7" data-line-number="7"></a>
<a class="sourceLine" id="cb16-8" data-line-number="8">    <span class="kw">instance</span> <span class="dt">Alternative</span> (<span class="dt">LogicT</span> f) <span class="kw">where</span></a>
<a class="sourceLine" id="cb16-9" data-line-number="9">        empty <span class="fu">=</span> <span class="dt">LogicT</span> <span class="fu">$</span> \_ fk <span class="ot">-&gt;</span> fk</a>
<a class="sourceLine" id="cb16-10" data-line-number="10">        f1 <span class="fu">&lt;|&gt;</span> f2 <span class="fu">=</span> <span class="dt">LogicT</span> <span class="fu">$</span> \sk fk <span class="ot">-&gt;</span> unLogicT f1 sk (unLogicT f2 sk fk)</a>
<a class="sourceLine" id="cb16-11" data-line-number="11"></a>
<a class="sourceLine" id="cb16-12" data-line-number="12">    <span class="kw">instance</span> <span class="dt">Monad</span> (<span class="dt">LogicT</span> m) <span class="kw">where</span></a>
<a class="sourceLine" id="cb16-13" data-line-number="13">        return a <span class="fu">=</span> <span class="dt">LogicT</span> <span class="fu">$</span> \sk fk <span class="ot">-&gt;</span> sk a fk</a>
<a class="sourceLine" id="cb16-14" data-line-number="14">        m <span class="fu">&gt;&gt;=</span> f <span class="fu">=</span> <span class="dt">LogicT</span> <span class="fu">$</span> \sk fk <span class="ot">-&gt;</span> unLogicT m (\a fk&#39; <span class="ot">-&gt;</span> unLogicT (f a) sk fk&#39;) fk</a>
<a class="sourceLine" id="cb16-15" data-line-number="15">        fail _ <span class="fu">=</span> <span class="dt">LogicT</span> <span class="fu">$</span> \_ fk <span class="ot">-&gt;</span> fk</a></code></pre></div>
<p>It helps for reading this if you expand <code>sk</code> to “success continuation” and <code>fk</code> to “fail computation”. Since we’re dealing with church lists I suppose you could also use <code>cons</code> and <code>nil</code>.</p>
<p>What’s particularly interesting to me here is that there are <em>no</em> constraints on <code>m</code> for these type class declarations! Let’s go through them one at a time.</p>
<p><code>Functor</code> is usually pretty mechanical, and this is no exception. Here we just have to change <code>a -&gt; m r -&gt; m r</code> to <code>b -&gt; m r -&gt; m r</code>. This is trivial just by composing the success computation with <code>f</code>.</p>
<p><code>Applicative</code> is similar. <code>pure</code> just lifts a value into the church equivalent of a singleton list, <code>[a]</code>. <code>&lt;*&gt;</code> is a little bit more meaty, we first unwrap <code>f</code> to it’s underlying function <code>g</code>, and composes it with out successes computation for <code>a</code>. Notice that this is very similar to how <code>Cont</code> works, continuation passing style is necessary with church representations.</p>
<p>Now <code>return</code> and <code>fail</code> are pretty straightforward. Though this is interesting because since pattern matching calls <code>fail</code>, we can just do something like</p>
<div class="sourceCode" id="cb17"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb17-1" data-line-number="1">    <span class="kw">do</span></a>
<a class="sourceLine" id="cb17-2" data-line-number="2">      <span class="dt">Just</span> a <span class="ot">&lt;-</span> m</a>
<a class="sourceLine" id="cb17-3" data-line-number="3">      <span class="dt">Just</span> b <span class="fu">&lt;</span>  n</a>
<a class="sourceLine" id="cb17-4" data-line-number="4">      return <span class="fu">$</span> a <span class="fu">+</span> b</a></code></pre></div>
<p>And we’ll run <code>n</code> and <code>m</code> until we get a <code>Just</code> value.</p>
<p>As for <code>&gt;&gt;=</code>, it’s implementation is very similar to <code>&lt;*&gt;</code>. We unwrap <code>m</code> and then feed the unwrapped <code>a</code> into <code>f</code> and run that with our success computations.</p>
<p>We’re only going to talk about one more instance for <code>LogicT</code>, <code>MonadLogic</code>, there are a few others but they’re mostly for MTL use and not too interesting.</p>
<div class="sourceCode" id="cb18"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb18-1" data-line-number="1">    <span class="kw">instance</span> (<span class="dt">Monad</span> m) <span class="ot">=&gt;</span> <span class="dt">MonadLogic</span> (<span class="dt">LogicT</span> m) <span class="kw">where</span></a>
<a class="sourceLine" id="cb18-2" data-line-number="2">        msplit m <span class="fu">=</span> lift <span class="fu">$</span> unLogicT m ssk (return <span class="dt">Nothing</span>)</a>
<a class="sourceLine" id="cb18-3" data-line-number="3">         <span class="kw">where</span> ssk a fk <span class="fu">=</span> return <span class="fu">$</span> <span class="dt">Just</span> (a, (lift fk <span class="fu">&gt;&gt;=</span> reflect))</a></code></pre></div>
<p>We’re only implementing <code>msplit</code> here, which strikes me as a bit odd since we implemented everything before. We also actually need <code>Monad m</code> here so that we can use <code>LogicT</code>’s <code>MonadTrans</code> instance.</p>
<p>To split a <code>LogicT</code>, we run a special success computation and return <code>Nothing</code> if failure is ever called. Now there’s one more clever trick here, since we can choose what the <code>r</code> is in <code>m r</code>, we choose it to be <code>Maybe (a, LogicT m a)</code>! That way we can take the failure case, which essentially is just the tail of the list, and push it into <code>reflect</code>.</p>
<p>This confused me a bit so I wrote the equivalent version for church lists, where <code>msplit</code> is just <code>uncons</code>.</p>
<div class="sourceCode" id="cb19"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb19-1" data-line-number="1">    <span class="ot">{-# LANGUAGE RankNTypes #-}</span></a>
<a class="sourceLine" id="cb19-2" data-line-number="2"></a>
<a class="sourceLine" id="cb19-3" data-line-number="3">    <span class="kw">newtype</span> <span class="dt">CList</span> a <span class="fu">=</span> <span class="dt">CList</span> {<span class="ot">runCList ::</span> forall r<span class="fu">.</span> (a <span class="ot">-&gt;</span> r <span class="ot">-&gt;</span> r) <span class="ot">-&gt;</span> r <span class="ot">-&gt;</span> r}</a>
<a class="sourceLine" id="cb19-4" data-line-number="4"></a>
<a class="sourceLine" id="cb19-5" data-line-number="5"><span class="ot">    cons ::</span> a <span class="ot">-&gt;</span> <span class="dt">CList</span> a <span class="ot">-&gt;</span> <span class="dt">CList</span> a</a>
<a class="sourceLine" id="cb19-6" data-line-number="6">    cons a (<span class="dt">CList</span> list) <span class="fu">=</span> <span class="dt">CList</span> <span class="fu">$</span> \cs nil <span class="ot">-&gt;</span> cs a (list cs nil)</a>
<a class="sourceLine" id="cb19-7" data-line-number="7"></a>
<a class="sourceLine" id="cb19-8" data-line-number="8"><span class="ot">    nil ::</span> <span class="dt">CList</span> a</a>
<a class="sourceLine" id="cb19-9" data-line-number="9">    nil <span class="fu">=</span> <span class="dt">CList</span> <span class="fu">$</span> \cons nil <span class="ot">-&gt;</span> nil</a>
<a class="sourceLine" id="cb19-10" data-line-number="10"></a>
<a class="sourceLine" id="cb19-11" data-line-number="11"><span class="ot">    head ::</span> <span class="dt">CList</span> a <span class="ot">-&gt;</span> <span class="dt">Maybe</span> a</a>
<a class="sourceLine" id="cb19-12" data-line-number="12">    head list <span class="fu">=</span> runCList list (const <span class="fu">.</span> <span class="dt">Just</span>) <span class="dt">Nothing</span></a>
<a class="sourceLine" id="cb19-13" data-line-number="13"></a>
<a class="sourceLine" id="cb19-14" data-line-number="14"><span class="ot">    uncons ::</span> <span class="dt">CList</span> a <span class="ot">-&gt;</span> <span class="dt">Maybe</span> (a, <span class="dt">CList</span> a)</a>
<a class="sourceLine" id="cb19-15" data-line-number="15">    uncons (<span class="dt">CList</span> list) <span class="fu">=</span> list skk <span class="dt">Nothing</span></a>
<a class="sourceLine" id="cb19-16" data-line-number="16">      <span class="kw">where</span> skk a rest <span class="fu">=</span> <span class="dt">Just</span> (a, maybe nil (uncurry cons) rest)</a></code></pre></div>
<p>Now it’s a bit clearer what’s going on, <code>skk</code> just pairs up the head of the list with the rest. However, since the tail of the list has the type <code>m (Maybe (a, LogicT m a))</code>, we lift it back into the <code>LogicT</code> monad and use <code>reflect</code> to smush it back into a good church list.</p>
<p>That about covers Control.Monad.Logic</p>
<h2 id="wrap-up">Wrap Up</h2>
<p>I’ve never tried sharing these readings before so I hope you enjoyed it. If this receives some positive feedback I’ll do something similar with another package, I’m leaning towards extensible-effects.</p>
<p>If you’re interested in doing this yourself, I highly recommend it! I’ve learned a <em>lot</em> about practical engineering with Haskell, as well as really clever and elegant Haskell code.</p>
<p>One thing I’ve always enjoyed about the Haskell ecosystem is that some of the most interesting code is often quite easy to read given some time.</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Thu, 10 Jul 2014 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2014-07-10-reading-logict.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Dissecting crush</title>
    <link>http://jozefg.bitbucket.org/posts/2014-07-09-dissecting-crush.html</link>
    <description><![CDATA[<div class="info">
    Posted on July  9, 2014
    
</div>
<div class="info">
    
    Tags: <a href="/tags/coq.html">coq</a>, <a href="/tags/types.html">types</a>
    
</div>

<p>For almost a year and half now I’ve been referencing one particular book on Coq, <a href="http://adam.chlipala.net/cpdt/">Certified Programming with Dependent Types</a>. CPDT is a literate program on building practical things with Coq.</p>
<p>One of the main ideas of CPDT is that proofs ought to be fully automated. This means that a proof should be primarily a logic program (Ltac) which constructs some boring and large proof term. To this end, CPDT has a bunch of Ltac “tactics” for constructing such logic programs.</p>
<p>Since CPDT is a program, there’s actual working source for each of these tactics. It occurred to me today that in my 18 months of blinking uncomprehendingly at CPDT, I’ve never read its source for these tactics.</p>
<p>In this post, we’ll dissect how CPDT’s main tactic for automation, <code>crush</code>, actually works. In the process, we’ll get the chance to explore some nice, compositional, ltac engineering as well as a whole host of useful tricks.</p>
<h2 id="the-code">The Code</h2>
<p>The first step to figuring out of <code>crush</code> works is actually finding where it’s defined.</p>
<p>After downloading the source to CPDT I ran</p>
<pre><code>grep &quot;Ltac crush :=&quot; -r .</code></pre>
<p>And found in <code>src/CpdtTactics</code>, line 205</p>
<pre><code>Ltac crush := crush&#39; false fail.</code></pre>
<p>Glancing at <code>crush'</code>, I’ve noticed that it pulls in almost every tactic in <code>CpdtTactics</code>. Therefore, we’ll start at the top of this file and work our way done, dissecting each tactic as we go.</p>
<p>Incidentally, since CpdtTactics is an independent file, if you’re confused about something firing up your coq dev environment of choice and trying things out with <code>Goal</code> inline works nicely.</p>
<p>Starting from the top, our first tactic is <code>inject</code>.</p>
<pre><code>Ltac inject H := injection H; clear H; intros; try subst.</code></pre>
<p>This is just a quick wrapper around <code>injection</code>, which also does the normal operations one wants after calling <code>injection</code>. It clears the original hypothesis and brings our new equalities into our environment so future tactics can use them. It also tries to swap out any variables with our new equalities using <code>subst</code>. Notice the <code>try</code> wrapper since <code>subst</code> is one of those few tactics that will fail if it can’t do anything useful.</p>
<p>Next up is</p>
<pre><code>Ltac appHyps f :=
  match goal with
    | [ H : _ |- _ ] =&gt; f H
  end.</code></pre>
<p><code>appHyps</code> makes use of the backtracking nature of <code>match goal with</code>. It’ll apply <code>f</code> to every hypothesis in the current environment and stop once it find a hypothesis <code>f</code> works with.</p>
<p>Now we get to some combinators for working with hypothesis.</p>
<pre><code>Ltac inList x ls :=
  match ls with
    | x =&gt; idtac
    | (_, x) =&gt; idtac
    | (?LS, _) =&gt; inList x LS
  end.</code></pre>
<p><code>inList</code> takes a faux-list of hypothesis and looks for an occurrence of a particular lemma <code>x</code>. When it finds it we just run <code>idtac</code> which does nothing. In the case were we can’t match <code>x</code> anywhere, <code>inList</code> will just fail with the standard “No matching clause” message.</p>
<p>Next we have the equivalent of <code>appHyps</code> for tupled lists</p>
<pre><code>Ltac app f ls :=
  match ls with
    | (?LS, ?X) =&gt; f X || app f LS || fail 1
    | _ =&gt; f ls
  end.</code></pre>
<p>This works exactly like <code>appHyps</code> but instead of looking through the proofs environment, we’re looking through <code>ls</code>. It has the same “keep the first result that works” semantics too. One thing that confused me was the <code>_ =&gt; f ls</code> clause of this tactic. Remember that with our tupled lists we don’t have a “nil” member. But rather the equivalent of</p>
<pre><code>A :: B :: C :: Nil</code></pre>
<p>is</p>
<pre><code>((A, B), C)</code></pre>
<p>So when we don’t have a pair, <code>ls</code> itself is the last hypothesis in our list. As a corollary of this, there is no obvious “empty” tupled list, only one with a useless last hypothesis.</p>
<p>Next we have <code>all</code>, which runs <code>f</code> on <em>every</em> member in <code>f ls</code>.</p>
<pre><code>Ltac all f ls :=
  match ls with
    | (?LS, ?X) =&gt; f X; all f LS
    | (_, _) =&gt; fail 1
    | _ =&gt; f ls
  end.</code></pre>
<p>Careful readers will notice that instead of <code>f X || ...</code> we use <code>;</code>. Additionally, if the first clause fails and the second clause matches, that means that either <code>f X</code> or <code>all f LS</code> failed. In this case we backtrack all the way back out of this clause. This should mean that this is a “all or nothing” tactic. It will either not fail on all members of <code>ls</code> or nothing at all will happen.</p>
<p>Now we get to the first <em>big</em> tactic</p>
<pre><code>Ltac simplHyp invOne :=
  let invert H F :=
    inList F invOne;
      (inversion H; fail)
      || (inversion H; [idtac]; clear H; try subst) in

  match goal with
    | [ H : ex _ |- _ ] =&gt; destruct H
    | [ H : ?F ?X = ?F ?Y |- ?G ] =&gt;
      (assert (X = Y); [ assumption | fail 1 ])
      || (injection H;
        match goal with
          | [ |- X = Y -&gt; G ] =&gt;
            try clear H; intros; try subst
        end)
    | [ H : ?F ?X ?U = ?F ?Y ?V |- ?G ] =&gt;
      (assert (X = Y); [ assumption
        | assert (U = V); [ assumption | fail 1 ] ])
      || (injection H;
        match goal with
          | [ |- U = V -&gt; X = Y -&gt; G ] =&gt;
            try clear H; intros; try subst
        end)

    | [ H : ?F _ |- _ ] =&gt; invert H F
    | [ H : ?F _ _ |- _ ] =&gt; invert H F
    | [ H : ?F _ _ _ |- _ ] =&gt; invert H F
    | [ H : ?F _ _ _ _ |- _ ] =&gt; invert H F
    | [ H : ?F _ _ _ _ _ |- _ ] =&gt; invert H F

    | [ H : existT _ ?T _ = existT _ ?T _ |- _ ] =&gt; generalize (inj_pair2 _ _ _ _ _ H); clear H
    | [ H : existT _ _ _ = existT _ _ _ |- _ ] =&gt; inversion H; clear H
    | [ H : Some _ = Some _ |- _ ] =&gt; injection H; clear H
  end.</code></pre>
<p>Wow, just a little bit bigger than what we’ve been working with so far.</p>
<p>The first small chunk of <code>simpleHyp</code> is a tactic for doing clever inversion using the tuple list <code>invOne</code>.</p>
<pre><code> invert H F :=
   inList F invOne;
   (inversion H; fail)
     || (inversion H; [idtac]; clear H; try subst)</code></pre>
<p>Here <code>H</code> is a hypothesis that we’re thinking about inverting on and <code>F</code> is the head symbol of <code>H</code>. First we run the <code>inList</code> predicate, meaning that we don’t invert upon anything that we don’t want to. If the head symbol of <code>H</code> is something worth inverting upon we try two different types of inversion.</p>
<p>In the first case <code>inversion H; fail</code> we’re just looking for an “easy proof” where inverting <code>H</code> immediately dispatches the current goal. In the second case <code>inversion H; [idtac]; clear H; try subst</code>, we invert upon <code>H</code> iff it only generates 1 subgoal. Remember that <code>[t | t' | t'']</code> is a tactic that runs <code>t</code> on the first subgoal, t’ on the second, and so on. If the number of goals don’t match, <code>[]</code> will fail. So <code>[idtac]</code> is just a clever way of saying “there’s only one new subgoal”. Next we get rid of the hypothesis we just inverted on (it’s not useful now, and we don’t want to try inverting it again) and see if any substitutions are applicable.</p>
<p>Alright! Now let’s talk about the massive <code>match goal with</code> going on in <code>simplHyp</code>.</p>
<p>The first branch is</p>
<pre><code>    | [ H : ex _ |- _ ] =&gt; destruct H</code></pre>
<p>This just looks for a hypothesis with an existential (remember that <code>ex</code> is what <code>exists</code> desugars to). If we find one, we introduce a new variable to our environment and instantiate H with it. The fact that this doesn’t recursively call <code>simplHyp</code> probably means that we want to do something like <code>repeat simplHyp</code> to ensure this is applied everywhere.</p>
<p>Next we look at simplifying hypothesis where injection applies. There are two almost identical branches, one for constructors of two parameters, one for one. Let’s look at the latter since it’s slightly simpler.</p>
<pre><code>    | [ H : ?F ?X = ?F ?Y |- ?G ] =&gt;
      (assert (X = Y); [ assumption | fail 1 ])
      || (injection H;
        match goal with
          | [ |- X = Y -&gt; G ] =&gt;
            try clear H; intros; try subst
        end)</code></pre>
<p>This looks for an equality over a constructor <code>F</code>. This branch is looking to prove that <code>X = Y</code>, a fact deducible from the injectiveness of F.</p>
<p>The way that we go about doing this is actually quite a clever ltac trick though. First we assert <code>X = Y</code>, this will generate to subgoals, the first that <code>X = Y</code> (shocker) and the second is the current goal <code>G</code>, with the new hypothesis that <code>X = Y</code>. We attempt to prove that <code>X = Y</code> by <code>assumption</code>. If this works, than we already trivially can deduce <code>X = Y</code> so there’s no point in doing all that <code>injection</code> stuff so we <code>fail 1</code> and bomb out of the whole branch.</p>
<p>If <code>assumption</code> fails we’ll jump to the other side of the <code>||</code>s and actually use <code>injection</code>. We only run <code>injection</code> if it generates a proof that <code>X = Y</code> in which case we do the normal cleanup with trying to clear our original fact and do some substitution.</p>
<p>The next part is fairly straightforward, we make use of that <code>invert</code> tactic and run it over facts we have floating around in our environment</p>
<pre><code>    | [ H : ?F _ |- _ ] =&gt; invert H F
    | [ H : ?F _ _ |- _ ] =&gt; invert H F
    | [ H : ?F _ _ _ |- _ ] =&gt; invert H F
    | [ H : ?F _ _ _ _ |- _ ] =&gt; invert H F
    | [ H : ?F _ _ _ _ _ |- _ ] =&gt; invert H F</code></pre>
<p>Notice that we can now use the match to grab the leading symbol for <code>H</code> so we only invert upon hypothesis that we think will be useful.</p>
<p>Next comes a bit of axiom-fu</p>
<pre><code>    | [ H : existT _ ?T _ = existT _ ?T _ |- _ ] =&gt;
        generalize (inj_pair2 _ _ _ _ _ H); clear H</code></pre>
<p><code>inj_pair2</code> is function that lives in the Coq standard library and has the type</p>
<pre><code>forall (U : Type) (P : U -&gt; Type) (p : U) (x y : P p),
       existT P p x = existT P p y -&gt; x = y</code></pre>
<p>This relies on <code>eq_rect_eq</code> so it’s just a little bit dodgy for something like HoTT where we give more rope to <code>=</code> than just <code>refl</code>.</p>
<p>This particular branch of the match is quite straightforward though. Once we see an equality between two witnesses for the same existential type, we just <code>generalize</code> the equality between their proofs into our goal.</p>
<p>If this fails however, we’ll fall back to standard inversion with</p>
<pre><code>    | [ H : existT _ _ _ = existT _ _ _ |- _ ] =&gt; inversion H; clear H</code></pre>
<p>Finally, we have one last special case branch for <code>Some</code>. This is because the branches above will fail when phased with a polymorphic constructor</p>
<pre><code>    | [ H : Some _ = Some _ |- _ ] =&gt; injection H; clear H</code></pre>
<p>Nothing exciting going on there.</p>
<p>So that wraps up <code>simplHyp</code>. It’s just a conglomeration of useful stuff to do to constructors in our hypothesis.</p>
<p>Onwards we go! Next is a simple tactic for automatically rewriting with a hypothesis</p>
<pre><code>Ltac rewriteHyp :=
  match goal with
    | [ H : _ |- _ ] =&gt; rewrite H by solve [ auto ]
  end.</code></pre>
<p>like most of the other tactics we saw earlier, this will hunt for an <code>H</code> where this works and then stop. The <code>by solve [auto]</code> will run <code>solve [auto]</code> against all the hypothesis that the <code>rewrite</code> generates and ensure that <code>auto</code> solves all the new goals. This prevents a rewrite from going and introducing obviously false facts as goals for a rewrite that made no sense.</p>
<p>We can combine this with <code>autorewrite</code> with two simple tactics</p>
<pre><code>Ltac rewriterP := repeat (rewriteHyp; autorewrite with core in *).
Ltac rewriter := autorewrite with core in *; rewriterP.</code></pre>
<p>This just repeatedly rewrite with <code>autorewrite</code> and <code>rewriteHyp</code> as long as they can. Worth noticing here how we can use <code>repeat</code> to make these smaller tactics modify <em>all</em> applicable hypothesis rather than just one.</p>
<p>Next up is an innocent looking definition that frightens me a little bit</p>
<pre><code>Definition done (T : Type) (x : T) := True.</code></pre>
<p>What frightens me about this is that Adam calls this “devious”.. and when <em>he</em> calls something clever or devious I’m fairly certain I’d never be able to come up with it :)</p>
<p>What this actually appears to do is provide a simple way to “stick” something into an environment. We can trivially prove <code>done T x</code> for any <code>T</code> and <code>x</code> but having this in an environment also gives us a proposition <code>T</code> and a ready made proof of it <code>x</code>! This is useful for tactics since we can do something like</p>
<pre><code>assert (done SomethingUseful usefulPrf) by constructor</code></pre>
<p>and viola! Global state without hurting anything.</p>
<p>We use these in the next tactic, <code>instr</code>.</p>
<pre><code>Ltac inster e trace :=
  match type of e with
    | forall x : _, _ =&gt;
      match goal with
        | [ H : _ |- _ ] =&gt;
          inster (e H) (trace, H)
        | _ =&gt; fail 2
      end
    | _ =&gt;
      match trace with
        | (_, _) =&gt;
          match goal with
            | [ H : done (trace, _) |- _ ] =&gt;
              fail 1
            | _ =&gt;
              let T := type of e in
                match type of T with
                  | Prop =&gt;
                    generalize e; intro;
                      assert (done (trace, tt)) by constructor
                  | _ =&gt;
                    all ltac:(fun X =&gt;
                      match goal with
                        | [ H : done (_, X) |- _ ] =&gt; fail 1
                        | _ =&gt; idtac
                      end) trace;
                    let i := fresh &quot;i&quot; in (pose (i := e);
                      assert (done (trace, i)) by constructor)
                end
          end
      end
  end.</code></pre>
<p>Another big one!</p>
<p>This match is a little different than the previous ones. It’s not a match goal but a <code>match type of ... with</code>. This is used to examine one particular hypothesis’ type and match over that.</p>
<p>This particular <code>match</code> has two branches. The first deals with the case where we have uninstantiated universally quantified variables.</p>
<pre><code> | forall x : _, _ =&gt;
    match goal with
      | [ H : _ |- _ ] =&gt;
        inster (e H) (trace, H)
      | _ =&gt; fail 2
    end</code></pre>
<p>If our hypothesis does, we randomly grab a hypothesis, instantiate <code>e</code> with it, add <code>H</code> to the trace list, and then recurse.</p>
<p>If there isn’t a hypothesis, then we fail out of the toplevel match and exit the tactic.</p>
<p>Now the next branch is where the real work happens</p>
<pre><code>  | _ =&gt;
    match trace with
      | (_, _) =&gt;
        match goal with
          | [ H : done (trace, _) |- _ ] =&gt;
            fail 1
          | _ =&gt;
            let T := type of e in
              match type of T with
                | Prop =&gt;
                  generalize e; intro;
                    assert (done (trace, tt)) by constructor
                | _ =&gt;
                  all ltac:(fun X =&gt;
                    match goal with
                      | [ H : done (_, X) |- _ ] =&gt; fail 1
                      | _ =&gt; idtac
                    end) trace;
                  let i := fresh &quot;i&quot; in (pose (i := e);
                    assert (done (trace, i)) by constructor)
              end
         end
      end</code></pre>
<p>We first chekc to make sure that <code>trace</code> isn’t empty. If this is the case, then we know that we instantiated <code>e</code> with at least <em>something</em>. If we have, we snoop around to see if there’s a <code>done</code> in our environment with the same trace. If this is the case, we know that we’ve done an identical instantiation of <code>e</code> before hand so we backtrack to try another one.</p>
<p>Otherwise, we look to see what <code>e</code> was instantiated too. If it was a simple <code>Prop</code>, we just stick a <code>done</code> record of this instantiation into our environment and add our new instantiated <code>e</code> back in with <code>generalize</code>. If <code>e</code> isn’t a proof, we do the same thing. In this case, however, we must also double check that the things we used to instantiate <code>e</code> with aren’t results of <code>inster</code> as well otherwise our combination of backtracking/instantiating can lead to an infinite loop.</p>
<p>Since this tactic generates a bunch of <code>done</code>’s that are otherwise useless, a tactic to clear them is helpful.</p>
<pre><code>Ltac un_done :=
  repeat match goal with
           | [ H : done _ |- _ ] =&gt; clear H
         end.</code></pre>
<p>Hopefully by this point this isn’t too confusing. All this tactic does is loop through the environment and clear all <code>done</code>s.</p>
<p>Now, finally, we’ve reached <code>crush'</code>.</p>
<pre><code>Ltac crush&#39; lemmas invOne :=
  let sintuition := simpl in *; intuition; try subst;
    repeat (simplHyp invOne; intuition; try subst); try congruence in

  let rewriter := autorewrite with core in *;
    repeat (match goal with
              | [ H : ?P |- _ ] =&gt;
                match P with
                  | context[JMeq] =&gt; fail 1
                  | _ =&gt; rewrite H by crush&#39; lemmas invOne
                end
            end; autorewrite with core in *) in

    (sintuition; rewriter;
      match lemmas with
        | false =&gt; idtac            | _ =&gt;
          (** Try a loop of instantiating lemmas... *)
          repeat ((app ltac:(fun L =&gt; inster L L) lemmas
          (** ...or instantiating hypotheses... *)
            || appHyps ltac:(fun L =&gt; inster L L));
          (** ...and then simplifying hypotheses. *)
          repeat (simplHyp invOne; intuition)); un_done
      end;
      sintuition; rewriter; sintuition;
      try omega; try (elimtype False; omega)).</code></pre>
<p><code>crush'</code> is really broken into 3 main components.</p>
<p>First is a simple tactic <code>sintuition</code></p>
<pre><code>sintuition := simpl in *; intuition; try subst;
    repeat (simplHyp invOne; intuition; try subst); try congruence</code></pre>
<p>So this first runs the normal set of “generally useful tactics” and then breaks out some of first custom tactics. This essentially will act like a souped-up version of <code>intuition</code> and solve goals that are trivially solvable with straightforward inversions and reductions.</p>
<p>Next there’s a more powerful version of <code>rewriter</code></p>
<pre><code>rewriter := autorewrite with core in *;
    repeat (match goal with
              | [ H : ?P |- _ ] =&gt;
                match P with
                  | context[JMeq] =&gt; fail 1
                  | _ =&gt; rewrite H by crush&#39; lemmas invOne
                end
            end; autorewrite with core in *)</code></pre>
<p>This is almost identical to what we have above but instead of solving side conditions with <code>solve [auto]</code>, we use <code>crush'</code> to hopefully deal with a larger number of possible rewrites.</p>
<p>Finally, we have the main loop of <code>crush'</code>.</p>
<pre><code>(sintuition; rewriter;
  match lemmas with
    | false =&gt; idtac
    | _ =&gt;
      repeat ((app ltac:(fun L =&gt; inster L L) lemmas
        || appHyps ltac:(fun L =&gt; inster L L));
      repeat (simplHyp invOne; intuition)); un_done
  end;
  sintuition; rewriter; sintuition;
try omega; try (elimtype False; omega)).</code></pre>
<p>Here we run the <code>sintuition</code> and <code>rewriter</code> and then get to work with the lemmas we supplied in <code>lemmas</code>.</p>
<p>The first branch is just a match on <code>false</code>, which we use like a nil. Since we have no hypothesis we don’t do anything new.</p>
<p>If we do have lemmas, we try instantiating both them and our hypothesis as many times as necessary and then repeatedly simplify the results. This loop will ensure that we make full use of bot our supplied lemmas and the surrounding environment.</p>
<p>Finally, we make another few passes with <code>rewriter</code> and <code>sintuition</code> attempting to dispatch our goal using our new, instantiated and simplified environment.</p>
<p>As a final bonus, if we <em>still</em> haven’t dispatched our goal, we’ll run <code>omega</code> to attempt to solve a Presburger arithmetic. On the off chance that we have something <code>omega</code> can be contradictory, we also try <code>elimType false; omega</code> to try to exploit such a contradiction.</p>
<p>So all <code>crush</code> does is call this tactic with no lemmas (<code>false</code>) and no suggestions to invert upon (<code>fail</code>). There you have it, and it only took 500 lines to get here.</p>
<h2 id="wrap-up">Wrap Up</h2>
<p>So that’s it, hopefully you got a few useful Ltac trick out of reading this. I certainly did writing it :)</p>
<p>If you enjoyed these tactics, there’s a more open-source version of these tactics, on the <a href="http://adam.chlipala.net/cpdt/">CPDT website</a>. It might also interest you to read the rest of <code>CpdtTactics.v</code> since it has some useful gems like <code>dep_destruct</code>.</p>
<p>Last but not least, if you haven’t read CPDT itself and you’ve made it this far, go read it! It’s available as either dead-tree or online. I still reference it regularly so I at least find it useful. It’s certainly better written than this post :)</p>
<p><i>Note, all the code I’ve shown in this post is from CPDT and is licensed under ANCND license. I’ve removed some comments from the code where they wouldn’t render nicely with them.</i></p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Wed, 09 Jul 2014 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2014-07-09-dissecting-crush.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Some Useful Agda</title>
    <link>http://jozefg.bitbucket.org/posts/2014-06-28-real-world-agda.html</link>
    <description><![CDATA[<div class="info">
    Posted on June 28, 2014
    
</div>
<div class="info">
    
    Tags: <a href="/tags/agda.html">agda</a>, <a href="/tags/types.html">types</a>
    
</div>

<p>I’ve been using Agda for a few months now. I’ve always meant to figure out how it handles IO but never have.</p>
<p>Today I decided to change that! So off I went to the related Agda <a href="http://wiki.portal.chalmers.se/agda/pmwiki.php?n=ReferenceManual2.Compilation">wiki page</a>. So hello world in Agda apparently looks like this</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb1-1" data-line-number="1">    <span class="kw">open</span> <span class="kw">import</span> IO</a>
<a class="sourceLine" id="cb1-2" data-line-number="2"></a>
<a class="sourceLine" id="cb1-3" data-line-number="3">    main <span class="ot">=</span> run <span class="ot">(</span>putStrLn <span class="st">&quot;test&quot;</span><span class="ot">)</span></a></code></pre></div>
<p>The first time I tried running this I got an error about an <code>IO.FFI</code>, if you get this you need to go into your standard library and run <code>cabal install</code> in the <code>ffi</code> folder.</p>
<p>Now, on to what this actually does. Like Haskell, Agda has an <code>IO</code> monad. In fact, near as I can tell this isn’t a coincidence at all, Agda’s primitive IO seems to be a direct call to Haskell’s IO.</p>
<p>Unlike Haskell, Agda has two IO monads, a “raw” primitive one and a higher level pure one found in <code>IO.agda</code>. What few docs there are make it clear that you are not intended to write the “primitive IO”.</p>
<p>Instead, one writes in this higher level <code>IO</code> monad and then uses a function called <code>run</code> which converts everything to the primitive IO.</p>
<p>So one might ask: what exactly is this strange <code>IO</code> monad and how does it actually provide <code>return</code> and <code>&gt;&gt;=</code>? Well the docs don’t actually seem to exist so poking about the source reveals</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb2-1" data-line-number="1">    <span class="kw">data</span> IO <span class="ot">{</span>a<span class="ot">}</span> <span class="ot">(</span>A <span class="ot">:</span> <span class="dt">Set</span> a<span class="ot">)</span> <span class="ot">:</span> <span class="dt">Set</span> <span class="ot">(</span>suc a<span class="ot">)</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb2-2" data-line-number="2">      lift   <span class="ot">:</span> <span class="ot">(</span>m <span class="ot">:</span> Prim<span class="ot">.</span>IO A<span class="ot">)</span> <span class="ot">→</span> IO A</a>
<a class="sourceLine" id="cb2-3" data-line-number="3">      return <span class="ot">:</span> <span class="ot">(</span>x <span class="ot">:</span> A<span class="ot">)</span> <span class="ot">→</span> IO A</a>
<a class="sourceLine" id="cb2-4" data-line-number="4">      <span class="ot">_</span>&gt;&gt;=<span class="ot">_</span>  <span class="ot">:</span> <span class="ot">{</span>B <span class="ot">:</span> <span class="dt">Set</span> a<span class="ot">}</span> <span class="ot">(</span>m <span class="ot">:</span> ∞ <span class="ot">(</span>IO B<span class="ot">))</span> <span class="ot">(</span>f <span class="ot">:</span> <span class="ot">(</span>x <span class="ot">:</span> B<span class="ot">)</span> <span class="ot">→</span> ∞ <span class="ot">(</span>IO A<span class="ot">))</span> <span class="ot">→</span> IO A</a>
<a class="sourceLine" id="cb2-5" data-line-number="5">      <span class="ot">_</span>&gt;&gt;<span class="ot">_</span>   <span class="ot">:</span> <span class="ot">{</span>B <span class="ot">:</span> <span class="dt">Set</span> a<span class="ot">}</span> <span class="ot">(</span>m₁ <span class="ot">:</span> ∞ <span class="ot">(</span>IO B<span class="ot">))</span> <span class="ot">(</span>m₂ <span class="ot">:</span> ∞ <span class="ot">(</span>IO A<span class="ot">))</span> <span class="ot">→</span> IO A</a></code></pre></div>
<p>Wow.. I don’t know about you, but this was a bit different than I was expecting.</p>
<p>So this actually just forms a syntax tree! There’s something quite special about this tree though, those ∞ annotations mean that it’s a “coinductive” tree. So we can construct infinite <code>IO</code> tree. Otherwise it’s just a normal tree.</p>
<p>Right below that in the source is the definition of <code>run</code></p>
<div class="sourceCode" id="cb3"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb3-1" data-line-number="1">    <span class="pp">{-# NO_TERMINATION_CHECK #-}</span></a>
<a class="sourceLine" id="cb3-2" data-line-number="2">    run <span class="ot">:</span> <span class="ot">∀</span> <span class="ot">{</span>a<span class="ot">}</span> <span class="ot">{</span>A <span class="ot">:</span> <span class="dt">Set</span> a<span class="ot">}</span> <span class="ot">→</span> IO A <span class="ot">→</span> Prim<span class="ot">.</span>IO A</a>
<a class="sourceLine" id="cb3-3" data-line-number="3">    run <span class="ot">(</span>lift m<span class="ot">)</span>   <span class="ot">=</span> m</a>
<a class="sourceLine" id="cb3-4" data-line-number="4">    run <span class="ot">(</span>return x<span class="ot">)</span> <span class="ot">=</span> Prim<span class="ot">.</span>return x</a>
<a class="sourceLine" id="cb3-5" data-line-number="5">    run <span class="ot">(</span>m  &gt;&gt;= f<span class="ot">)</span> <span class="ot">=</span> Prim<span class="ot">._</span>&gt;&gt;=<span class="ot">_</span> <span class="ot">(</span>run <span class="ot">(</span>♭ m <span class="ot">))</span> <span class="ot">λ</span> x <span class="ot">→</span> run <span class="ot">(</span>♭ <span class="ot">(</span>f x<span class="ot">))</span></a>
<a class="sourceLine" id="cb3-6" data-line-number="6">    run <span class="ot">(</span>m₁ &gt;&gt; m₂<span class="ot">)</span> <span class="ot">=</span> Prim<span class="ot">._</span>&gt;&gt;=<span class="ot">_</span> <span class="ot">(</span>run <span class="ot">(</span>♭ m₁<span class="ot">))</span> <span class="ot">λ</span> <span class="ot">_</span> <span class="ot">→</span> run <span class="ot">(</span>♭ m₂<span class="ot">)</span></a></code></pre></div>
<p>So here’s where the evilness comes in! We can loop forever transforming our <code>IO</code> into a <code>Prim.IO</code>.</p>
<p>Now I had never used Agda’s coinductive features before and if you haven’t either than they’re not terribly complicated.</p>
<p><code>∞</code> is a prefix operator that stands for a “coinductive computation” which is roughly a thunk. <code>♯</code> is a prefix operator that delays a computation and <code>♭</code> forces it.</p>
<p>There are reasonably complex rules that govern what qualifies as a “safe” way to force things. Guarded recursion seems to always work though. So we can write something like</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb4-1" data-line-number="1">    <span class="kw">open</span> <span class="kw">import</span> Coinduction</a>
<a class="sourceLine" id="cb4-2" data-line-number="2">    <span class="kw">open</span> <span class="kw">import</span> Data<span class="ot">.</span>Unit</a>
<a class="sourceLine" id="cb4-3" data-line-number="3"></a>
<a class="sourceLine" id="cb4-4" data-line-number="4">    <span class="kw">data</span> Cothingy <span class="ot">(</span>A <span class="ot">:</span> <span class="dt">Set</span><span class="ot">)</span> <span class="ot">:</span> <span class="dt">Set</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb4-5" data-line-number="5">      conil  <span class="ot">:</span> Cothingy A</a>
<a class="sourceLine" id="cb4-6" data-line-number="6">      coCons <span class="ot">:</span> A <span class="ot">→</span> ∞ <span class="ot">(</span>Cothingy A<span class="ot">)</span> <span class="ot">→</span> Cothingy A</a>
<a class="sourceLine" id="cb4-7" data-line-number="7"></a>
<a class="sourceLine" id="cb4-8" data-line-number="8">    lotsa-units <span class="ot">:</span> Cothingy ⊤</a>
<a class="sourceLine" id="cb4-9" data-line-number="9">    lotsa-units <span class="ot">=</span> coCons tt <span class="ot">(</span>♯ lotsa-units<span class="ot">)</span></a></code></pre></div>
<p>Now using ♯ we can actually construct programs with infinite output.</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb5-1" data-line-number="1">    forever <span class="ot">:</span> IO ⊤</a>
<a class="sourceLine" id="cb5-2" data-line-number="2">    forever <span class="ot">=</span> ♯ putStrLn <span class="st">&quot;Hi&quot;</span> &gt;&gt; ♯ forever</a>
<a class="sourceLine" id="cb5-3" data-line-number="3"></a>
<a class="sourceLine" id="cb5-4" data-line-number="4">    main <span class="ot">=</span> run forever</a></code></pre></div>
<p>This when run will output “Hi” forever. This is actually quite pleasant when you think about it! You can view you’re resulting computation as a normal, first class data structure and then reify it to actual computations with <code>run</code>.</p>
<p>So with all of this figured out, I wanted to write a simple program in Agda just to make sure that I got it all.</p>
<h2 id="fizzbuzz">FizzBuzz</h2>
<p>I decided to write the fizz-buzz program. For those unfamiliar, the specification of the program is</p>
<blockquote>
<p>For each of the numbers 0 to 100, if the number is divisible by 3 print fizz, if it’s divisible by 5 print buzz, if it’s divisible by both print fizzbuzz. Otherwise just print the number.</p>
</blockquote>
<p>This program is pretty straightforward. First, the laundry list of imports</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb6-1" data-line-number="1">    <span class="kw">module</span> fizzbuzz <span class="kw">where</span></a>
<a class="sourceLine" id="cb6-2" data-line-number="2"></a>
<a class="sourceLine" id="cb6-3" data-line-number="3">    <span class="kw">import</span> Data<span class="ot">.</span>Nat        as N</a>
<a class="sourceLine" id="cb6-4" data-line-number="4">    <span class="kw">import</span> Data<span class="ot">.</span>Nat<span class="ot">.</span>DivMod as N</a>
<a class="sourceLine" id="cb6-5" data-line-number="5">    <span class="kw">import</span> Data<span class="ot">.</span>Nat<span class="ot">.</span>Show   as N</a>
<a class="sourceLine" id="cb6-6" data-line-number="6">    <span class="kw">import</span> Data<span class="ot">.</span>Bool       as B</a>
<a class="sourceLine" id="cb6-7" data-line-number="7">    <span class="kw">import</span> Data<span class="ot">.</span>Fin        as F</a>
<a class="sourceLine" id="cb6-8" data-line-number="8">    <span class="kw">import</span> Data<span class="ot">.</span>Unit       as U</a>
<a class="sourceLine" id="cb6-9" data-line-number="9">    <span class="kw">import</span> Data<span class="ot">.</span>String     as S</a>
<a class="sourceLine" id="cb6-10" data-line-number="10">    <span class="kw">open</span> <span class="kw">import</span> Data<span class="ot">.</span>Product <span class="kw">using</span> <span class="ot">(_</span>,<span class="ot">_</span> <span class="ot">;</span> <span class="ot">_</span>×<span class="ot">_)</span></a>
<a class="sourceLine" id="cb6-11" data-line-number="11">    <span class="kw">open</span> <span class="kw">import</span> IO</a>
<a class="sourceLine" id="cb6-12" data-line-number="12">    <span class="kw">open</span> <span class="kw">import</span> Coinduction</a>
<a class="sourceLine" id="cb6-13" data-line-number="13">    <span class="kw">open</span> <span class="kw">import</span> Relation<span class="ot">.</span>Nullary</a>
<a class="sourceLine" id="cb6-14" data-line-number="14">    <span class="kw">open</span> <span class="kw">import</span> Function</a></code></pre></div>
<p>This seems to be the downside of finely grained modules.. Tons and tons of imports.</p>
<p>Now we need a function which takes to ℕs and returns true if the first mod the second is zero.</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb7-1" data-line-number="1">    congruent <span class="ot">:</span> N<span class="ot">.</span>ℕ <span class="ot">→</span> N<span class="ot">.</span>ℕ <span class="ot">→</span> B<span class="ot">.</span>Bool</a>
<a class="sourceLine" id="cb7-2" data-line-number="2">    congruent n N<span class="ot">.</span>zero    <span class="ot">=</span> B<span class="ot">.</span>false</a>
<a class="sourceLine" id="cb7-3" data-line-number="3">    congruent n <span class="ot">(</span>N<span class="ot">.</span>suc m<span class="ot">)</span> <span class="kw">with</span> N<span class="ot">._</span>≟<span class="ot">_</span> <span class="dv">0</span> $ F<span class="ot">.</span>toℕ <span class="ot">(</span>N<span class="ot">._</span>mod<span class="ot">_</span> n <span class="ot">(</span>N<span class="ot">.</span>suc m<span class="ot">)</span> <span class="ot">{</span>U<span class="ot">.</span>tt<span class="ot">})</span></a>
<a class="sourceLine" id="cb7-4" data-line-number="4">    <span class="ot">...</span> <span class="ot">|</span> yes <span class="ot">_</span> <span class="ot">=</span> B<span class="ot">.</span>true</a>
<a class="sourceLine" id="cb7-5" data-line-number="5">    <span class="ot">...</span> <span class="ot">|</span> no  <span class="ot">_</span> <span class="ot">=</span> B<span class="ot">.</span>false</a></code></pre></div>
<p>Now from here we can combine this into the actual worker for the program</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb8-1" data-line-number="1"></a>
<a class="sourceLine" id="cb8-2" data-line-number="2">    <span class="ot">_</span>and<span class="ot">_</span> <span class="ot">:</span> <span class="ot">{</span>A B <span class="ot">:</span> <span class="dt">Set</span><span class="ot">}</span> <span class="ot">→</span> A <span class="ot">→</span> B <span class="ot">→</span> A × B</a>
<a class="sourceLine" id="cb8-3" data-line-number="3">    <span class="ot">_</span>and<span class="ot">_</span> <span class="ot">=</span> <span class="ot">_</span>,<span class="ot">_</span></a>
<a class="sourceLine" id="cb8-4" data-line-number="4"></a>
<a class="sourceLine" id="cb8-5" data-line-number="5">    fizzbuzz <span class="ot">:</span> N<span class="ot">.</span>ℕ <span class="ot">→</span> S<span class="ot">.</span>String</a>
<a class="sourceLine" id="cb8-6" data-line-number="6">    fizzbuzz N<span class="ot">.</span>zero    <span class="ot">=</span> <span class="st">&quot;fizzbuzz&quot;</span></a>
<a class="sourceLine" id="cb8-7" data-line-number="7">    fizzbuzz n <span class="kw">with</span> congruent n <span class="dv">3</span> and congruent n <span class="dv">5</span></a>
<a class="sourceLine" id="cb8-8" data-line-number="8">    <span class="ot">...</span> <span class="ot">|</span> B<span class="ot">.</span>true  , B<span class="ot">.</span>true   <span class="ot">=</span> <span class="st">&quot;fizzbuzz&quot;</span></a>
<a class="sourceLine" id="cb8-9" data-line-number="9">    <span class="ot">...</span> <span class="ot">|</span> B<span class="ot">.</span>true  , B<span class="ot">.</span>false  <span class="ot">=</span> <span class="st">&quot;fizz&quot;</span></a>
<a class="sourceLine" id="cb8-10" data-line-number="10">    <span class="ot">...</span> <span class="ot">|</span> B<span class="ot">.</span>false , B<span class="ot">.</span>true   <span class="ot">=</span> <span class="st">&quot;buzz&quot;</span></a>
<a class="sourceLine" id="cb8-11" data-line-number="11">    <span class="ot">...</span> <span class="ot">|</span> B<span class="ot">.</span>false , B<span class="ot">.</span>false  <span class="ot">=</span> N<span class="ot">.</span>show n</a></code></pre></div>
<p>Now all that’s left is the <code>IO</code> glue</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb9-1" data-line-number="1">    worker <span class="ot">:</span> N<span class="ot">.</span>ℕ <span class="ot">→</span> IO U<span class="ot">.</span>⊤</a>
<a class="sourceLine" id="cb9-2" data-line-number="2">    worker N<span class="ot">.</span>zero    <span class="ot">=</span> putStrLn $ fizzbuzz N<span class="ot">.</span>zero</a>
<a class="sourceLine" id="cb9-3" data-line-number="3">    worker <span class="ot">(</span>N<span class="ot">.</span>suc n<span class="ot">)</span> <span class="ot">=</span> ♯ worker n &gt;&gt; ♯ putStrLn <span class="ot">(</span>fizzbuzz $ N<span class="ot">.</span>suc n<span class="ot">)</span></a>
<a class="sourceLine" id="cb9-4" data-line-number="4"></a>
<a class="sourceLine" id="cb9-5" data-line-number="5">    main <span class="ot">=</span> run $ worker <span class="dv">100</span></a></code></pre></div>
<p>There. A somewhat real, IO based program written in Agda. It only took me 8 months to figure out how to write it :)</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Sat, 28 Jun 2014 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2014-06-28-real-world-agda.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Teaching Python with a Raspberry Pi</title>
    <link>http://jozefg.bitbucket.org/posts/2014-06-23-teaching-with-minecraft.html</link>
    <description><![CDATA[<div class="info">
    Posted on June 23, 2014
    
</div>
<div class="info">
    
    Tags: <a href="/tags/teaching.html">teaching</a>
    
</div>

<p>This last week I’ve been volunteering at a summer camp. This camp is aimed at kids ages 8 to 12 and teaches the basics of Python!</p>
<p>I wanted to write down some of my thoughts and experiences on the whole process.</p>
<h2 id="the-curriculum">The Curriculum</h2>
<p>The curriculum for the camp was based around 3 key components</p>
<ul>
<li>Python</li>
<li>Raspberry Pis</li>
<li>Minecraft</li>
</ul>
<p>The camp was spread over 4 days, each 3 hours. Each day introduced more of Python with more sophisticated programs. Each program actually interacted with minecraft, building structures, modifying worlds, and doing cool visible things. We’ll talk later about how this was possible.</p>
<p>Going into the camp, the expected schedule was something like</p>
<ol type="1">
<li>Introduce the Pi, show how to run Python scripts from terminal</li>
<li>Introduce the basics of Python, mostly variables and conditionals</li>
<li>Apply these basics with the minecraft API</li>
<li>Introduce loops, apply this with a few more advanced programs</li>
</ol>
<p>In hindsight, this curriculum was a tad bit unrealistic, but what curriculum isn’t.</p>
<h2 id="the-staff">The Staff</h2>
<p>This was the first time the camp was run, so the staff was a little inexperienced.</p>
<p>I was the only person familiar with programming but had never taught young children before, and the two payed staff members were used to teaching basic science camps but had never taught anything CS-ish. This meant that a lot of this was a learning experience for us as much as the kids.</p>
<h2 id="the-children">The Children</h2>
<p>The camp was over-capacity with 14 children. None of them has ever programmed before per-se. But two had done some basic HTML layout and one 10 year old was quite familiar with unix after 2 years of running various Linux distributions (I was impressed).</p>
<p>The unfortunate fact was that since the camp was marketed as teaching with Minecraft, a lot of the kids just showed up to play Minecraft. This was anticipated but still a little saddening.</p>
<h2 id="day-1">Day 1</h2>
<p>On day 1, we get everyone set up with their own Pi, we also included</p>
<ul>
<li>A cheap monitor</li>
<li>A very cheap mouse</li>
<li>A keyboard</li>
</ul>
<p>Getting this all set up for 14 kids was a lot smoother than anticipated. The only hitch was the SD cards we’d purchased were a lot cheaper than anticipated so we burned through maybe 5 cards that we just couldn’t get a Pi to boot with.</p>
<p>We got everyone successfully to a desktop in about 30 minutes.</p>
<p>The Pis were running a custom operating system called Raspbian. This OS is very verbose during boot time and shows the entire log from booting up rather than just displaying an innocent little loading graphic.</p>
<p>Quite a few of the kids were curious about what was going on so we explained how little about how OS’s work. It was pretty awesome to see kids being interested in what steps a kernel went through.</p>
<p>Sadly I’m not a super knowledgeable person when it comes to OS’s. In light of this I’ve ordered a book or two on the subject, something that’s been on my todo list for a while now. I should be better prepared for questions next time.</p>
<p>Now once we got everyone up and running we had people order 2 programs, LXTerminal and Minecraft. This is when we had some fun trying to explain what exactly a terminal is.</p>
<p>I eventually started simply saying</p>
<blockquote>
<p>LXTerminal is a program that let’s you run other programs. It’s like a text interface so that you can do what you normally do by clicking with typing.</p>
<p>Almost all Unix computers, like OS X and Raspbian, have the same way of entering stuff into terminals.</p>
</blockquote>
<p>From here we had everyone run <code>cd play</code>. Luckily a group of volunteer engineers had sat down and written a bunch of programs to do various things in Minecraft. The first one everyone started with just built a grid of stone blocks.</p>
<p>We then started explaining how to run things with the <code>python</code> program. This turned out to be a bit more of a struggle than anticipated since typing and spelling are more difficult than anticipated.</p>
<p>We had a lot of people doing things like</p>
<pre><code> $ pyton grid.py
 $ pythongrid.py
 $ grid.py
 $ python grid.py # Finally</code></pre>
<p>I really wish we had an overhead project to show everyone written examples on a teacher machine. This was a big problem as time went on, simply saying things out loud is not a sufficient method for communicating about programs.</p>
<p>Now, once this ran there was a satisfying “Whoooaaaa” when everyone saw that this command had modified the game right before their eyes!</p>
<p>Some people quickly started trying to use this to speed up their building by automatically creating walls for themselves rather than doing it by hand. This was exactly the response we were looking for and it was clear this was starting to spark some interest in programming.</p>
<p>Finally we had everyone open up IDLE. We used IDLE for all our editing purposes for exactly two reasons</p>
<ol type="1">
<li>It’s dead simple to use</li>
<li>It’s preinstalled</li>
</ol>
<p>Everyone opened up <code>grid.py</code> and had a look at the source code. The code for <code>grid.py</code> was roughly</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb2-1" data-line-number="1">    <span class="im">import</span> minecraft</a>
<a class="sourceLine" id="cb2-2" data-line-number="2">    <span class="im">import</span> block</a>
<a class="sourceLine" id="cb2-3" data-line-number="3"></a>
<a class="sourceLine" id="cb2-4" data-line-number="4">    mc <span class="op">=</span> minecraft.Minecraft.create() <span class="co"># Our connection to Minecraft</span></a>
<a class="sourceLine" id="cb2-5" data-line-number="5"></a>
<a class="sourceLine" id="cb2-6" data-line-number="6">    <span class="kw">def</span> buildWall(size, <span class="bu">type</span> <span class="op">=</span> block.STONE):</a>
<a class="sourceLine" id="cb2-7" data-line-number="7">        pos <span class="op">=</span> mc.player.getPos()</a>
<a class="sourceLine" id="cb2-8" data-line-number="8">        pos.x <span class="op">+=</span> <span class="dv">3</span></a>
<a class="sourceLine" id="cb2-9" data-line-number="9"></a>
<a class="sourceLine" id="cb2-10" data-line-number="10">        <span class="cf">for</span> x <span class="kw">in</span> <span class="bu">range</span>(size):</a>
<a class="sourceLine" id="cb2-11" data-line-number="11">            <span class="cf">for</span> y <span class="kw">in</span> <span class="bu">range</span>(size):</a>
<a class="sourceLine" id="cb2-12" data-line-number="12">                <span class="co"># Set block at these coordinates to type</span></a>
<a class="sourceLine" id="cb2-13" data-line-number="13">                mc.setBlock(pos.x <span class="op">+</span> x, pos.y <span class="op">+</span> y, pos.z, <span class="bu">type</span>)</a>
<a class="sourceLine" id="cb2-14" data-line-number="14"></a>
<a class="sourceLine" id="cb2-15" data-line-number="15">    <span class="cf">if</span> <span class="va">__name__</span> <span class="op">=</span> <span class="st">&quot;__main__&quot;</span>:</a>
<a class="sourceLine" id="cb2-16" data-line-number="16">        buildWall(<span class="dv">5</span>)</a></code></pre></div>
<p>We get a pretty nice high level API to minecraft, and the code is quite simple. Keep in mind, we have taught exactly 0 python at this point.</p>
<p>Next we explained that we could change <code>buildWall(5)</code> to <code>buildWall(6)</code> and our program would make a bigger wall! Again an overhead was sorely missed at this point since it was very hard to explain exactly where we were talking about, even in such small code.</p>
<p>Most people than started modifying the code trying to build as big a wall as possible. This was also the point at which our first syntax errors started up.</p>
<p>Since I was the only person in the room who understood what they meant there was a fair bit of running around. I have to give a lot of credit to the two staff members who essentially learned the basics of Python syntax by me yelling it to them across the room!</p>
<p><code>grid.py</code> also included some code to generate a grid with different blocks. This was another huge success since kids could try to spell different words in their grid of blocks. I’ve omitted it from the above snippet since frankly I don’t remember it.</p>
<p>This took up most of the first day, since everyone also got a 30 minute snack breaks (don’t you miss snack breaks?).</p>
<h2 id="day-2">Day 2</h2>
<p>The next day we were actually aiming to teach some programming! This had a script written already by the engineers who’d written the code we’d used yesterday, but upon consulting the script I found</p>
<ol type="1">
<li>Teach variables</li>
<li>Explain what a value is</li>
<li>Explain if’s</li>
<li>Questions</li>
</ol>
<p>Uh oh. So I ended up writing a few notes down the night before, we didn’t have access to any sort of projector so a lot of my explanations consisted of scribbling on a giant (2’ by 3’) post it note.</p>
<p>This had distinctly mixed results. As I’d expected most kids couldn’t pick up the fundamentals of programming in an hour! This was OK though since the rest of the day was spent messing around with a simple program</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb3-1" data-line-number="1">    <span class="co"># chat.py</span></a>
<a class="sourceLine" id="cb3-2" data-line-number="2">    <span class="im">import</span> minecraft</a>
<a class="sourceLine" id="cb3-3" data-line-number="3"></a>
<a class="sourceLine" id="cb3-4" data-line-number="4">    mc <span class="op">=</span> minecraft.Minecraft.create()</a>
<a class="sourceLine" id="cb3-5" data-line-number="5"></a>
<a class="sourceLine" id="cb3-6" data-line-number="6">    message <span class="op">=</span> <span class="st">&quot;Hello Chat&quot;</span></a>
<a class="sourceLine" id="cb3-7" data-line-number="7"></a>
<a class="sourceLine" id="cb3-8" data-line-number="8">    mc.putToChat(message)</a></code></pre></div>
<p>And we used this to introduce the fundamentals of Python. For kids that were progressing faster, we challenged them to write more complicated programs like</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb4-1" data-line-number="1">    <span class="im">import</span> minecraft</a>
<a class="sourceLine" id="cb4-2" data-line-number="2"></a>
<a class="sourceLine" id="cb4-3" data-line-number="3">    mc <span class="op">=</span> minecraft.Minecraft.create()</a>
<a class="sourceLine" id="cb4-4" data-line-number="4"></a>
<a class="sourceLine" id="cb4-5" data-line-number="5">    message <span class="op">=</span> <span class="st">&quot;&quot;</span></a>
<a class="sourceLine" id="cb4-6" data-line-number="6"></a>
<a class="sourceLine" id="cb4-7" data-line-number="7">    <span class="cf">if</span> <span class="dv">1</span> <span class="op">+</span> <span class="dv">1</span> <span class="op">&lt;</span> <span class="dv">2</span>:</a>
<a class="sourceLine" id="cb4-8" data-line-number="8">        message <span class="op">=</span> <span class="st">&quot;Hello&quot;</span></a>
<a class="sourceLine" id="cb4-9" data-line-number="9">    <span class="cf">else</span>:</a>
<a class="sourceLine" id="cb4-10" data-line-number="10">        message <span class="op">=</span> <span class="st">&quot;Goodbye&quot;</span></a>
<a class="sourceLine" id="cb4-11" data-line-number="11"></a>
<a class="sourceLine" id="cb4-12" data-line-number="12">    mc.putToChat(message)</a></code></pre></div>
<p>Not surprisingly, this was really hard to grasp for our kids. This was when the class started to fragment a bit, some kids were getting this and really doing awesome while some were having a harder time with all the new information.</p>
<p>If I had a chance to do this again, I’d definitely split the class into two groups, one for people who were up and running with basic concepts to build some programs together with one instructor. The other two could then stay and give one to one help slowly but surely. This would prevent us from leaving anyone behind.</p>
<p>In reality I’d say we had about 5 kids who were understanding what was going on and 8 who were lost. No one had yet given up on programming luckily, so we were still more or less OK.</p>
<h2 id="day-3">Day 3</h2>
<p>Going into this day I knew it wasn’t going to be easy</p>
<ol type="1">
<li>We were starting to lose a bit of interest since it’s getting later in the week</li>
<li>Some kids were falling behind others</li>
</ol>
<p>with this in mind, we went about introducing a few new prewritten programs that built cubes! I’ll leave it to your imagination how this worked, it’s pretty similar to <code>grid.py</code>.</p>
<p>For the kids who were really clicking, I challenged some of them to explain parts of the code to me. In this context I taught a few kids about <code>for</code> loops. It’s a bit tricky to explain how they work since I didn’t want to explain what an iterable was. Remember, we hadn’t talked about any OO aspects of Python.</p>
<p>I introduced them to loops as something to the effect of</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb5-1" data-line-number="1">    <span class="cf">for</span> VAR <span class="kw">in</span> <span class="bu">range</span>(NUMBER):</a>
<a class="sourceLine" id="cb5-2" data-line-number="2">        STMT</a>
<a class="sourceLine" id="cb5-3" data-line-number="3">        STMT</a>
<a class="sourceLine" id="cb5-4" data-line-number="4">        ...</a></code></pre></div>
<p>With the explanation that</p>
<blockquote>
<p>A loop means we run that list of statements once for each number between 0 and <code>NUMBER</code> - 1 with <code>VAR</code> first being 0, then 1, then 2 and so on.</p>
</blockquote>
<p>This seemed to click with most of them so quite a few got the hang of how loops worked.</p>
<p>I’d actually prefer I’d built some sort of abstraction like</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb6-1" data-line-number="1">    <span class="kw">def</span> allPairs(<span class="op">*</span>dims):</a>
<a class="sourceLine" id="cb6-2" data-line-number="2">        ...</a></code></pre></div>
<p>which returned an iterable (generator?) that had a list of all pairs possible within the given set of numbers. This would eliminate the need to talk about nested loops, which were a confusing subject for most people.</p>
<p>The tricky bit is that while I was hopping from person to person, the slower moving campers where playing with <code>cube.py</code> all on their own and not trying to understand the whole thing but still use it.</p>
<p>This worked surprisingly well actually, we were challenging kids to think about how to combine <code>grid.py</code> and <code>cube.py</code> to build things without ever laying a block by hand. Sadly a few kids just abandoned the effort and started playing Minecraft. This was not unexpected but still a little sad.</p>
<p>To keep things going, I wrote a little program which built a cube where the inside was filled with one thing and the outside was another. This meant that kids could build an upside down volcano or a waterfall.</p>
<p>Unfortunately, to get this to all the kids we had to hand write it on giant post-it notes and they had to manually type it. This is another case where we desperately needed a projector.</p>
<p>So the third day wasn’t nearly as structured as day 2, it was really a day when kids experimented and we tried to push kids individually. This actually seemed to be a great help since a few more kids had some breakthroughs on day 2 materials.</p>
<h2 id="day-4">Day 4</h2>
<p>Now, on the final day we opted to try something a little different.</p>
<p>We first tried networking the Raspberry Pis since kids had been asking to do this since day 1. Despite being able to get this working in prep time, we had some technical issues that prevented us from getting it working during the actual camp, very frustrating.</p>
<p>After the kids snack break, we went into a different room with no computers and put up a post it with the title “Steps for Writing Code”</p>
<ol type="1">
<li>Define Our Problem</li>
<li>Brainstorm Solutions</li>
<li>Compare Solutions and Choose One</li>
<li>Implement Solution</li>
<li>Test Implementation</li>
</ol>
<p>Now experts will notice the missing step 5.5, “swear profusely while implementation doesn’t work”. We will of course include this in a second level camp for teaching programming :)</p>
<p>Now I told them that their goal was to create a program which built a “sphere”. I put the quotes there since minecraft is built from blocks and doesn’t have a smooth sphere but you can get pretty close with bigger and bigger spheres.</p>
<p>So we went on to step 1. and everyone struggled to define what exactly a sphere was and how one ought to decide what “build it” meant.</p>
<p>We eventually settled on our problem being to build a sphere where</p>
<ol type="1">
<li>A sphere is a collection of all blocks within a certain distance, D, from the center</li>
<li>To “build” a sphere meant we’d place the center 3 + D blocks in front of us and we’d color all blocks in our sphere to stone.</li>
</ol>
<p>Next came the lively discussion on how to actually go about doing this.</p>
<p>After about 5 minutes, we had a lot of hand-wavy solutions but not actual concrete procedure for doing this so I tossed out a hint.</p>
<p>I stated that if someone needed a procedure for finding the space between two blocks, I will implement a function <code>dist</code> so that</p>
<pre><code>dist(x, y)</code></pre>
<p>would return the distance between the <code>x</code> block and <code>y</code> block in three dimensions.</p>
<p>Now the solutions got a lot closer, people started listing steps of what to do. I encouraged them to treat me like the computer and give me directions. I would then walk around and “color” carpet squares. This seemed to demonstrate which solutions weren’t quite precise enough.</p>
<p>Eventually, we ended on a simple solution</p>
<blockquote>
<p>Figure out the center by adding D + 3 to the current position. For each square in the grid S, if dist(S, Center) &lt; D, color S</p>
</blockquote>
<p>Very simple, very inefficient but correct. I then started talking about pseudo-code and turning this into a more executable form.</p>
<p>The kids who understood loops jumped in and we ended with something like</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb8-1" data-line-number="1">    pos <span class="op">=</span> mc.player.getPos()</a>
<a class="sourceLine" id="cb8-2" data-line-number="2">    pos.x <span class="op">+=</span> <span class="dv">3</span></a>
<a class="sourceLine" id="cb8-3" data-line-number="3">    <span class="cf">for</span> square <span class="kw">in</span> fullWorld():</a>
<a class="sourceLine" id="cb8-4" data-line-number="4">        <span class="cf">if</span> dist(square, pos) <span class="op">&lt;=</span> D:</a>
<a class="sourceLine" id="cb8-5" data-line-number="5">            mc.setBlock(square.x, square.y, square.z, block.STONE)</a></code></pre></div>
<p>I let them off the hook here and wrote the rest of the code for them while they took a brief break.</p>
<p>We then adjourned into the computer room and got started testing! We had just enough time to have everyone gather round while we built a Death Star on the teachers machine (I was the fastest typist).</p>
<p>Quite a few of the kids where interested in buying their own Pis and continuing on their own so we gave everyone their SD cards and directions on how to acquire a Raspberry Pi. I also gave out my emails to a few of the kids who wanted to make sure they had someone answer questions when they were setting up their Pis.</p>
<h2 id="recap">Recap</h2>
<p>So dear reader, where are we left?</p>
<p>Well the place that ran this camp is running more. I’m not sure if they’re full, but if you’re a parent or interested kid, please email me at [jozefg AT cmu.edu].</p>
<p>If you’re thinking that you want to run one of these camps yourself, do it! I only have 4 pieces of advice</p>
<ol type="1">
<li><p>Error on being concise and simple rather than comprehensive</p>
<p>You’re not going to teach someone to program in 4 days. You can however, make someone hate programming forever in 4 days! If they kids want more information, they’ll ask.</p>
<p>I guarantee that you’ll end up flooding the kids with too much information if you try to be comprehensive.</p></li>
<li><p>Always run this with more than one adult present</p>
<p>Otherwise you’ll end up spending the whole camp chasing after kids to fix issues and everyone else will be bored.</p>
<p>It’s always good to have more than one adult who knows Python too! You can do it with just one I’ve discovered. It is less than ideal however.</p></li>
<li><p>Have a good space, with a projector!</p>
<p>Projectors are great. So great that I’m very seriously considering buying one for the next 2 iterations of this camp.</p></li>
<li><p>Inspire kids to want to learn more!</p>
<p>That’s the whole point! You’ll never teach anything if you’re fighting the kids. Make this fun and don’t sweat it if you feel like you’re not covering as much material as you’d like. This isn’t a class, there’s no exam at the end, it’s supposed to be fun!</p></li>
</ol>
<p>If anyone has any more specific questions on this camp, please comment below and I’ll respond as soon as I can.</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Mon, 23 Jun 2014 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2014-06-23-teaching-with-minecraft.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Grokking recursion-schemes: Part 2</title>
    <link>http://jozefg.bitbucket.org/posts/2014-06-14-like-recursion-but-cooler-2.html</link>
    <description><![CDATA[<div class="info">
    Posted on June 14, 2014
    
</div>
<div class="info">
    
    Tags: <a href="/tags/haskell.html">haskell</a>
    
</div>

<p>In this post I’d like to talk about the second half of recusion-schemes. <a href="posts/2014-05-19-like-recursion-but-cooler.html">Previously</a> we’d talked about catamorphisms and friends. These all focused on “destroying” a datastructure by collapsing it layer by layer.</p>
<p>We’re now going to talk about the opposite: anamorphisms. Anamorphisms are just like generalized versions of <code>unfoldr</code>.</p>
<h3 id="getting-anamorphisms">Getting Anamorphisms</h3>
<p>To demonstrate how to start anamorphisms, we’ll create our custom list again.</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb1-1" data-line-number="1">    <span class="ot">{-# LANGUAGE DeriveFunctor #-}</span></a>
<a class="sourceLine" id="cb1-2" data-line-number="2">    <span class="kw">data</span> <span class="dt">MyList</span> a  <span class="fu">=</span> <span class="dt">MyCons</span> a (<span class="dt">MyList</span> a) <span class="fu">|</span> <span class="dt">MyNil</span></a>
<a class="sourceLine" id="cb1-3" data-line-number="3">    <span class="kw">data</span> <span class="dt">ListB</span> a b <span class="fu">=</span> <span class="dt">BCons</span> a b           <span class="fu">|</span> <span class="dt">BNil</span></a>
<a class="sourceLine" id="cb1-4" data-line-number="4">                   <span class="kw">deriving</span> <span class="dt">Functor</span></a></code></pre></div>
<p>Now we create an instance of the type class <code>Unfoldable</code> (shocker I know)</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb2-1" data-line-number="1">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">Base</span> (<span class="dt">MyList</span> a) <span class="fu">=</span> <span class="dt">BList</span> a</a>
<a class="sourceLine" id="cb2-2" data-line-number="2"></a>
<a class="sourceLine" id="cb2-3" data-line-number="3">    <span class="kw">instance</span> <span class="dt">Unfoldable</span> (<span class="dt">MyList</span> a) <span class="kw">where</span></a>
<a class="sourceLine" id="cb2-4" data-line-number="4">      embed (<span class="dt">BCons</span> a b) <span class="fu">=</span> <span class="dt">MyCons</span> a b</a>
<a class="sourceLine" id="cb2-5" data-line-number="5">      embed <span class="dt">BNil</span>        <span class="fu">=</span> <span class="dt">MyNil</span></a></code></pre></div>
<p>That’s it! We define the dual to <code>Foldable</code>’s <code>project</code>, <code>embed</code>. This just defines how to take the datastructure that we’ve built up and stick it back into our list.</p>
<h3 id="using-anamorphisms">Using Anamorphisms</h3>
<p>Now, let’s actually start writing some anamorphisms. The simplest example of an unfolding I can think of is <code>between</code>. <code>between</code> takes two boundaries and then creates a list of values between the high and the low, (low, high).</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb3-1" data-line-number="1">    <span class="fu">&gt;</span> enum <span class="dv">1</span> <span class="dv">5</span></a>
<a class="sourceLine" id="cb3-2" data-line-number="2">      [<span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">4</span>]</a>
<a class="sourceLine" id="cb3-3" data-line-number="3">    <span class="fu">&gt;</span> enum <span class="ch">&#39;a&#39;</span> <span class="ch">&#39;c&#39;</span></a>
<a class="sourceLine" id="cb3-4" data-line-number="4">      <span class="st">&quot;b&quot;</span></a>
<a class="sourceLine" id="cb3-5" data-line-number="5">    <span class="fu">&gt;</span> enum <span class="dt">False</span> <span class="dt">False</span></a>
<a class="sourceLine" id="cb3-6" data-line-number="6">      [<span class="dt">False</span>]</a></code></pre></div>
<p>To make this more fun, we’ll return <code>MyList a</code> instead of just <code>[a]</code> since it’ll make it easier to show off recursion-schemes. I’ll explain how to generate <code>[a]</code>’s momentarily.</p>
<p>Now it’s pretty obvious the type of <code>between</code> should be something like</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" data-line-number="1"><span class="ot">    between ::</span> (<span class="dt">Eq</span> a, <span class="dt">Enum</span> a) <span class="ot">=&gt;</span> a <span class="ot">-&gt;</span> a <span class="ot">-&gt;</span> <span class="dt">MyList</span> a</a></code></pre></div>
<p>We could write this with simple, boring recursion</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb5-1" data-line-number="1">    between a b <span class="fu">|</span> a <span class="fu">==</span> b    <span class="fu">=</span> <span class="dt">MyNil</span></a>
<a class="sourceLine" id="cb5-2" data-line-number="2">                <span class="fu">|</span> otherwise <span class="fu">=</span> (succ a) <span class="ot">`MyCons`</span> enum (succ a) b</a></code></pre></div>
<p>But this is exactly what we were avoiding! Let’s rewrite this to use an anamorphism. The type of <code>ana</code> (our anamorphism implementation) is</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb6-1" data-line-number="1"><span class="ot">    ana ::</span> (a <span class="ot">-&gt;</span> <span class="dt">Base</span> t a) <span class="ot">-&gt;</span> a <span class="ot">-&gt;</span> t</a></code></pre></div>
<p>This is the almost the exact opposite of <code>cata :: (Base t a -&gt; a) -&gt; t -&gt; a</code>. So instead of tearing the structure down layer by layer, we build it <em>up</em> layer by layer.</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb7-1" data-line-number="1">    between low high <span class="fu">=</span> ana builder low</a>
<a class="sourceLine" id="cb7-2" data-line-number="2">      <span class="kw">where</span> builder a <span class="fu">=</span> <span class="fu">???</span></a></code></pre></div>
<p>where <code>builder</code> is takes an <code>a</code> and returns the either <code>BCons (succ a) (succ a)</code> or <code>BNil</code> if <code>a == high</code>. This is trivial to implement</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb8-1" data-line-number="1">    between low high <span class="fu">=</span> ana builder low</a>
<a class="sourceLine" id="cb8-2" data-line-number="2">      <span class="kw">where</span> builder a <span class="fu">|</span> a <span class="fu">==</span> b    <span class="fu">=</span> <span class="dt">BNil</span></a>
<a class="sourceLine" id="cb8-3" data-line-number="3">                      <span class="fu">|</span> otherwise <span class="fu">=</span> join <span class="dt">BCons</span> (succ a) <span class="co">-- from Control.Monad</span></a></code></pre></div>
<p>That’s it! <code>builder</code> captures the essence of how we build up the list, one cons at a time.</p>
<p>Now, as promised here’s how to actually implement it so it returns <code>[a]</code>’s.</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb9-1" data-line-number="1">    between low high <span class="fu">=</span> ana builder low</a>
<a class="sourceLine" id="cb9-2" data-line-number="2">      <span class="kw">where</span> builder a <span class="fu">|</span> a <span class="fu">==</span> b    <span class="fu">=</span> <span class="dt">Nil</span></a>
<a class="sourceLine" id="cb9-3" data-line-number="3">                      <span class="fu">|</span> otherwise <span class="fu">=</span> join <span class="dt">Cons</span> (succ a)</a></code></pre></div>
<p>recursion-schemes defines the type instance for <code>[a]</code> with two constructor <code>Cons</code> and <code>Nil</code> that behave precisely like <code>BCons</code> and <code>BNil</code>. However, <code>Cons</code> and <code>Nil</code> are defined using some type families magic that makes them invisible in the documentation (I found them by reading the source). They exist I promise :)</p>
<p>Now, I said before this was just a generalized version of <code>unfoldr</code>, let’s look at the type of <code>unfoldr</code>.</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb10-1" data-line-number="1">    Data.List.unfoldr<span class="ot"> ::</span> (b <span class="ot">-&gt;</span> <span class="dt">Maybe</span> (a, b)) <span class="ot">-&gt;</span> b <span class="ot">-&gt;</span> [a]</a></code></pre></div>
<p>So <code>unfoldr</code> takes our seed value, <code>b</code>, and splits it into either a value and another seed, or nothing. Sound familiar? Look again at <code>Cons</code>, <code>Cons</code> is a value <code>a</code>, and the next seed <code>b</code>! Furthermore <code>Nil</code> is completely ismorphic to <code>Nothing</code> here.</p>
<p>Now <code>ana</code> generalizes upon <code>unfoldr</code> since we don’t need to represent everything as either 1 terminator, <code>Nothing</code>, or one builder, <code>(a, b)</code>.</p>
<p>We could imagine something like</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb11-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">RedBlack</span> a <span class="fu">=</span> <span class="dt">Red</span>   a (<span class="dt">RedBlack</span> a) (<span class="dt">RedBlack</span> a)</a>
<a class="sourceLine" id="cb11-2" data-line-number="2">                    <span class="fu">|</span> <span class="dt">Black</span> a (<span class="dt">RedBlack</span> a) (<span class="dt">RedBlack</span> a)</a>
<a class="sourceLine" id="cb11-3" data-line-number="3">                    <span class="fu">|</span> <span class="dt">Leaf</span></a></code></pre></div>
<p>Now <code>ana</code> could handle the fact that we can now “build” new seeds in two ways, with <code>RedB</code> or <code>BlackB</code>!</p>
<h3 id="building-stuff-up-to-tear-it-down">Building Stuff Up to Tear It Down</h3>
<p>One of the most common patterns in Haskell is to create some intermediate data structure and immediately use it.</p>
<p>This is kinda like smashing an anamorphism and a catamorphism together into one. This has a name: a hylomorphism, <code>hylo</code> in recursion-schemes.</p>
<p>It turns out that this is one of the most useful applications of anamorphisms!</p>
<p>As a fun example, Daniel Wagner <a href="http://mathlesstraveled.com/2008/01/07/recounting-the-rationals-part-ii-fractions-grow-on-trees/">blogged</a> about how we can generate an infinite list of all rational numbers. The key to this is an infinite binary tree where each node is a rational number <code>p/q</code> and it’s two children are <code>(p + q) / q</code> and <code>p / (p + q)</code>.</p>
<p>We can build this binary tree with <code>ana</code>.</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb12-1" data-line-number="1">    <span class="kw">import</span> <span class="dt">GHC.Real</span></a>
<a class="sourceLine" id="cb12-2" data-line-number="2"></a>
<a class="sourceLine" id="cb12-3" data-line-number="3">    <span class="kw">data</span> <span class="dt">Bin</span> a    <span class="fu">=</span> <span class="dt">Node</span> a (<span class="dt">Bin</span> a) (<span class="dt">Bin</span> a)</a>
<a class="sourceLine" id="cb12-4" data-line-number="4">    <span class="kw">data</span> <span class="dt">BBin</span> a b <span class="fu">=</span> <span class="dt">NodeB</span> a b b <span class="kw">deriving</span> <span class="dt">Functor</span></a>
<a class="sourceLine" id="cb12-5" data-line-number="5"></a>
<a class="sourceLine" id="cb12-6" data-line-number="6">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">Base</span> (<span class="dt">Bin</span> a) <span class="fu">=</span> <span class="dt">BBin</span> a</a>
<a class="sourceLine" id="cb12-7" data-line-number="7"></a>
<a class="sourceLine" id="cb12-8" data-line-number="8">    <span class="kw">instance</span> <span class="dt">Unfoldable</span> (<span class="dt">Bin</span> a) <span class="kw">where</span></a>
<a class="sourceLine" id="cb12-9" data-line-number="9">      embed (<span class="dt">NodeB</span> a l r) <span class="fu">=</span> <span class="dt">Node</span> a l r</a>
<a class="sourceLine" id="cb12-10" data-line-number="10">    <span class="kw">instance</span> <span class="dt">Foldable</span> (<span class="dt">Bin</span> a) <span class="kw">where</span></a>
<a class="sourceLine" id="cb12-11" data-line-number="11"></a>
<a class="sourceLine" id="cb12-12" data-line-number="12"></a>
<a class="sourceLine" id="cb12-13" data-line-number="13"><span class="ot">    rats ::</span> <span class="dt">Bin</span> <span class="dt">Rational</span></a>
<a class="sourceLine" id="cb12-14" data-line-number="14">    rats <span class="fu">=</span> ana builder (<span class="dv">1</span> <span class="fu">%</span> <span class="dv">1</span>)</a>
<a class="sourceLine" id="cb12-15" data-line-number="15">      <span class="kw">where</span> builder r<span class="fu">@</span>(p <span class="fu">:%</span> q) <span class="fu">=</span> <span class="dt">NodeB</span> r ((p <span class="fu">+</span> q) <span class="fu">%</span> q) (p <span class="fu">%</span> (p <span class="fu">+</span> q))</a></code></pre></div>
<p>We can collapse it into a list with <code>cata</code></p>
<div class="sourceCode" id="cb13"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb13-1" data-line-number="1"><span class="ot">    collapse ::</span> <span class="dt">Bin</span> a <span class="ot">-&gt;</span> [a]</a>
<a class="sourceLine" id="cb13-2" data-line-number="2">    collapse <span class="fu">=</span> cata folder</a>
<a class="sourceLine" id="cb13-3" data-line-number="3">     <span class="kw">where</span> folder (<span class="dt">NodeB</span> a l r)         <span class="fu">=</span> a <span class="fu">:</span> interleave l r</a>
<a class="sourceLine" id="cb13-4" data-line-number="4">           interleave (x <span class="fu">:</span> xs) (y <span class="fu">:</span> ys) <span class="fu">=</span> x <span class="fu">:</span> y <span class="fu">:</span> interleave xs ys</a></code></pre></div>
<p>The work horse here is <code>interleave</code> which just describes how to safely combine two infinite lists.</p>
<p>Now we can combine the process of building up our binary tree and generating a list into one cool transformation</p>
<div class="sourceCode" id="cb14"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb14-1" data-line-number="1"><span class="ot">    allRats ::</span> [<span class="dt">Rational</span>]</a>
<a class="sourceLine" id="cb14-2" data-line-number="2">    allRats <span class="fu">=</span> hylo folder builder (<span class="dv">1</span> <span class="fu">%</span> <span class="dv">1</span>)</a>
<a class="sourceLine" id="cb14-3" data-line-number="3">      <span class="kw">where</span> folder (<span class="dt">NodeB</span> a l r)         <span class="fu">=</span> a <span class="fu">:</span> interleave l r</a>
<a class="sourceLine" id="cb14-4" data-line-number="4">            interleave (x <span class="fu">:</span> xs) (y <span class="fu">:</span> ys) <span class="fu">=</span> x <span class="fu">:</span> y <span class="fu">:</span> interleave xs ys</a>
<a class="sourceLine" id="cb14-5" data-line-number="5">            builder r<span class="fu">@</span>(p <span class="fu">:%</span> q)           <span class="fu">=</span> <span class="dt">NodeB</span> r ((p <span class="fu">+</span> q) <span class="fu">%</span> q) (p <span class="fu">%</span> (p <span class="fu">+</span> q))</a></code></pre></div>
<p>There you are! As a challenge to the reader, figure out what index a number <code>p/q</code> will appear in this list (it will only occur once).</p>
<p>If you found this math intersting, check out <a href="http://www.cs.ox.ac.uk/jeremy.gibbons/publications/rationals.pdf">this paper</a>.</p>
<p>A few other people have shown off this pattern, one of my favorites being <a href="http://fho.f12n.de/posts/2014-05-07-dont-fear-the-cat.html">merge sort as a hylomorphism</a>.</p>
<h2 id="a-recap">A Recap</h2>
<p>We’ve now covered the core elements of the <code>recursion-schemes</code> library, but I’m not quite done with this blog series. I’m planning on one more post detailing my attempt to actually use recursion-schemes in a real project: a scheme compiler.</p>
<p>I think it would make the post more interesting though if the next post didn’t just include an example of “stuff I find cool”, so, if you have any particular example of cleaning up some code using recursion-schemes, please let me know! I’d love to share any and all examples I can find since that’s been the best way I’ve found to actually grok <code>recurion-schemes</code>.</p>
<p>If you’re interested in sharing, either comment or email me at jozefg [at] cmu.edu.</p>
<p><em>Thanks to tel for proof reading</em></p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Sat, 14 Jun 2014 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2014-06-14-like-recursion-but-cooler-2.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Overview of A Scheme Compiler</title>
    <link>http://jozefg.bitbucket.org/posts/2014-06-07-c-of-scheme.html</link>
    <description><![CDATA[<div class="info">
    Posted on June  7, 2014
    
</div>
<div class="info">
    
    Tags: <a href="/tags/haskell.html">haskell</a>, <a href="/tags/compilers.html">compilers</a>
    
</div>

<p>For the last few months I’ve been spending a fair amount of time on a fun little Scheme to C compiler, <a href="http://bitbucket.org/jozefg/c_of_scheme"><code>c_of_scheme</code></a>.</p>
<p>In this post I’ll outline the high level overview of <code>c_of_scheme</code> and in future posts detail the specifics of each component.</p>
<h2 id="modules">Modules</h2>
<p><code>c_of_scheme</code> is divided into 11 modules: 2 utility modules, 6 modules which each handle one step of compilation, a module with definitions of ASTs, a driver, and of course Main.</p>
<p>First, let’s discuss the utility modules, <code>Utils.Gen</code> and <code>Utils.Error</code>. <code>Gen</code> defines a monad, <code>Gen</code>. This is used to generate unique integers to be used as identifiers. For example:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb1-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">Var</span> <span class="fu">=</span> <span class="dt">Name</span> <span class="dt">String</span> <span class="fu">|</span> <span class="dt">Gen</span> <span class="dt">Integer</span></a>
<a class="sourceLine" id="cb1-2" data-line-number="2"></a>
<a class="sourceLine" id="cb1-3" data-line-number="3"><span class="ot">    genVar ::</span> <span class="dt">Gen</span> <span class="dt">Var</span></a>
<a class="sourceLine" id="cb1-4" data-line-number="4">    genVar <span class="fu">=</span> <span class="dt">Gen</span> <span class="fu">&lt;$&gt;</span> gen</a></code></pre></div>
<p>Other stages of the compiler (continuation passing style, closure conversion, and lambda lifting) need lots of temporaries so this is used throughout the compiler.</p>
<p><code>Gen</code> also comes with a monad transformer that implements a handful of useful MTL type classes. Overall, nothing too stunning.</p>
<p>The other uninteresting utility module is <code>Error</code>, this is just a wrapper around <code>Either</code> with a few functions for throwing errors and good pretty printing of errors. This is used internally to signal a major internal error.<!-- could just end this here? --></p>
<p>The precise interface is given by a set of functions <code>failRW</code>, <code>failCPS</code>, <code>failClos</code>, etc., which correspond to each stage of compilation. These generate lovely pretty printed error messages for each stage. This will become clearer as we go over each phase individually and it’s clear what needs to signal failure.</p>
<p>A module that’s worth mentioning that’s not a compilation stage but not quite a utility module is <code>AST</code>. This defines the various abstract syntax trees and primops for our representation of Scheme. This also defines the compiler monad, which combines our error monad with <code>Gen</code> and some other bits and bobs useful for our compiler. More on <code>AST</code> in future posts.</p>
<h2 id="stages-of-compilation">Stages of Compilation</h2>
<p>Now let’s actually go over the individual phases of compilation.</p>
<h3 id="parsing-parser.hs">Parsing (Parser.hs)</h3>
<p>This is the least interesting phase of compilation.. I personally just dislike parsing so I don’t have much to say about this.</p>
<p>A legal Scheme program is a list of definitions, we don’t currently allow top level expressions. We also don’t currently support the usual define sugar for functions.</p>
<p>The parser uses Parsec because I just happen to know the Parsec API, ironically because of <a href="http://en.wikibooks.org/wiki/Write_Yourself_a_Scheme_in_48_Hours">this</a>. If anyone cares enough to write a proper lexer and/or parser or something, I’m more than happy to help!</p>
<h3 id="rewrite-top-levels-rewritetoplevel.hs">Rewrite Top Levels (RewriteTopLevel.hs)</h3>
<p>This phase is a little peculiar. It exists because we’re targeting C and C has a fairly annoying restriction on what it allows top levels to initialized to.</p>
<p>In C, we can’t write something like</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode c"><code class="sourceCode c"><a class="sourceLine" id="cb2-1" data-line-number="1">    <span class="dt">int</span> c = <span class="dv">1</span> + <span class="dv">1</span> + <span class="dv">1</span>;</a></code></pre></div>
<p>but in our dialect of Scheme, this is the only way to write interesting computations! This phase of compilation rewrites top levels (Shocking!) to match the C definition of top levels.</p>
<p>This is done by changing each definition to an <code>Init</code>, this will later turn into a C declaration without initialization. Next we create a new function, our main function, that is a series of assignments which pair each top level definition to its initializer.</p>
<p>For example</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode scheme"><code class="sourceCode scheme"><a class="sourceLine" id="cb3-1" data-line-number="1">    (<span class="ex">define</span><span class="fu"> foo </span><span class="dv">1</span>)</a>
<a class="sourceLine" id="cb3-2" data-line-number="2">    (<span class="ex">define</span><span class="fu"> bar </span><span class="dv">2</span>)</a>
<a class="sourceLine" id="cb3-3" data-line-number="3">    (<span class="ex">define</span><span class="fu"> quux </span>(<span class="op">+</span> foo bar))</a>
<a class="sourceLine" id="cb3-4" data-line-number="4"></a>
<a class="sourceLine" id="cb3-5" data-line-number="5">    (<span class="ex">define</span><span class="fu"> </span>_ (<span class="kw">display</span> quux))</a></code></pre></div>
<p>will become</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode scheme"><code class="sourceCode scheme"><a class="sourceLine" id="cb4-1" data-line-number="1">    (init foo)</a>
<a class="sourceLine" id="cb4-2" data-line-number="2">    (init bar)</a>
<a class="sourceLine" id="cb4-3" data-line-number="3">    (init quux)</a>
<a class="sourceLine" id="cb4-4" data-line-number="4">    (init _)</a>
<a class="sourceLine" id="cb4-5" data-line-number="5"></a>
<a class="sourceLine" id="cb4-6" data-line-number="6">    (<span class="ex">define</span><span class="fu"> magical-main</span></a>
<a class="sourceLine" id="cb4-7" data-line-number="7">       (<span class="kw">lambda</span> ()</a>
<a class="sourceLine" id="cb4-8" data-line-number="8">          (set! foo <span class="dv">1</span>)</a>
<a class="sourceLine" id="cb4-9" data-line-number="9">          (set! bar <span class="dv">1</span>)</a>
<a class="sourceLine" id="cb4-10" data-line-number="10">          (set! quux (<span class="op">+</span> foo bar))</a>
<a class="sourceLine" id="cb4-11" data-line-number="11">          (set! _ (<span class="kw">display</span> quux))))</a></code></pre></div>
<p>where <code>magical-main</code> will be the first thing called in the generated code.</p>
<p>A caveat, we turn <code>(define foo (lambda (..) ...))</code> into something different since it’s more efficient to directly convert these to functions.</p>
<h3 id="continuations-passing-style-conversion-cps.hs">Continuations Passing Style Conversion (CPS.hs)</h3>
<p>This is the first interesting bit of compilation, CPS is a style where each function call is a tail call. Here’s an example non-CPS code converted to CPS.</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode scheme"><code class="sourceCode scheme"><a class="sourceLine" id="cb5-1" data-line-number="1">    (<span class="ex">define</span><span class="fu"> foo</span></a>
<a class="sourceLine" id="cb5-2" data-line-number="2">       (<span class="kw">lambda</span> (y)</a>
<a class="sourceLine" id="cb5-3" data-line-number="3">          (<span class="op">+</span> <span class="dv">1</span> y)))</a>
<a class="sourceLine" id="cb5-4" data-line-number="4"></a>
<a class="sourceLine" id="cb5-5" data-line-number="5">     (<span class="ex">define</span><span class="fu"> foo-cps</span></a>
<a class="sourceLine" id="cb5-6" data-line-number="6">       (<span class="kw">lambda</span> (cont x y)</a>
<a class="sourceLine" id="cb5-7" data-line-number="7">         ((<span class="kw">lambda</span> (+&#39;)</a>
<a class="sourceLine" id="cb5-8" data-line-number="8">            ((<span class="kw">lambda</span> (one)</a>
<a class="sourceLine" id="cb5-9" data-line-number="9">               ((<span class="kw">lambda</span> (x&#39;)</a>
<a class="sourceLine" id="cb5-10" data-line-number="10">                  ((<span class="kw">lambda</span> (result)</a>
<a class="sourceLine" id="cb5-11" data-line-number="11">                     (cont result))</a>
<a class="sourceLine" id="cb5-12" data-line-number="12">                   (+&#39; one x&#39;)))</a>
<a class="sourceLine" id="cb5-13" data-line-number="13">                x))</a>
<a class="sourceLine" id="cb5-14" data-line-number="14">             <span class="dv">1</span>))</a>
<a class="sourceLine" id="cb5-15" data-line-number="15">          <span class="op">+</span>)))</a></code></pre></div>
<p>Notice how with the CPS’ed version we’ve actually made evaluation order explicit and have removed non-primitive expressions.</p>
<p>CPS.hs converts the AST to use CPS. We’ll detail this process later but for now I’ll mention one more interesting tidbit.</p>
<p>CPS.hs is also where we implement call/cc! In fact it’s trivial to do. All we do as add the declaration for</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode scheme"><code class="sourceCode scheme"><a class="sourceLine" id="cb6-1" data-line-number="1">    (<span class="ex">define</span><span class="fu"> call/cc</span></a>
<a class="sourceLine" id="cb6-2" data-line-number="2">       (<span class="kw">lambda</span> (c f)</a>
<a class="sourceLine" id="cb6-3" data-line-number="3">          (f c</a>
<a class="sourceLine" id="cb6-4" data-line-number="4">             (<span class="kw">lambda</span> (ignored x) (c x)))))</a></code></pre></div>
<h3 id="optimizations-optimizecps.hs">Optimizations (OptimizeCPS.hs)</h3>
<p>This module implements the simple optimizations we perform. For now this is limited to simple inlining and constant folding, but this should improve in the future.</p>
<p>These optimizations are implemented quite pleasantly with recursion schemes.</p>
<h3 id="closure-conversion-lambda-lifting-closureconvert.hs">Closure Conversion + Lambda Lifting (ClosureConvert.hs)</h3>
<p>This is the most difficult phase of compilation, for me anyways. In concept it’s quite simple though.</p>
<p>The idea is that we take the implicit closure “argument” that all scheme procedures take and make it explicit. To this end we add three new primops, <code>NewClos</code>, <code>ReadClos</code>, and <code>WriteClos</code>. These do much what you would expect and let us treat closures opaquely as first class values.</p>
<p>Next we change each procedure to take an extra argument, its closure, and change closed over variables to be selected from this closure. Finally we change each lambda to be paired with its closure when constructed.</p>
<p>This sounded pretty feasible to me on paper, but in practice it seems to be the greatest source of bugs in <code>c_of_scheme</code>. It finally seems to work nicely now so I’ll be sure to blog about it soon.</p>
<h3 id="code-generation-codegen.hs">Code Generation (CodeGen.hs)</h3>
<p>This is the final stop in our compilation pipeline - we generate C code.</p>
<p>To do this we use one of <a href="http://hackage.haskell.org/package/c-dsl">my libraries</a>. This is actually quite a simple step in the compiler since closure-converted, CPS-ed code is quite close to C.</p>
<p>Some of the details that code generation handles:</p>
<ul>
<li>Interfacing to the runtime system</li>
<li>Generating the main method</li>
<li>Generating declarations for all the variables used in our intermediate language</li>
<li>Mapping the Scheme variables to appropriate C names</li>
</ul>
<p>While this might sound daunting, this isn’t actually so bad.</p>
<h3 id="driver-driver.hs">Driver (Driver.hs)</h3>
<p>While I might not write a post on it, <code>Driver</code> is my personal favorite module. It glues together all of the previous compilation phases and provides a bunch of nice high level functions like <code>compileScheme</code>.</p>
<p>The reason I like it so much is that all the code in it is a very nice, clean example of composing components as good old functions.</p>
<p>If you’re looking to understand <code>c_of_scheme</code>’s particular implementation, I’d urge you to start with <code>Driver</code>. It’ll provide a bit of an intuition from what goes to where.</p>
<h2 id="the-runtime-system">The Runtime System</h2>
<p>Currently <code>c_of_scheme</code> has an incredibly naive runtime system. Mostly because it’s being written by an incredibly naive C programmer (hi!).</p>
<p>I already <a href="posts/2014-05-05-i-used-c-correctly.html">wrote</a> about the most interesting bit of the RTS: tail calls.</p>
<p>I plan on talking a bit about the RTS in the context of code generation (since it’d be impossible not to), and perhaps a post on <code>c_of_scheme</code>’s simple little mark and sweep GC.</p>
<h2 id="wrap-up">Wrap Up</h2>
<p>So that’s the high level overview of <code>c_of_scheme</code>, I think the compiler is best exemplified by one particular function in <code>Driver.hs</code>:</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb7-1" data-line-number="1"><span class="ot">    compileScheme ::</span> [<span class="dt">SDec</span> <span class="dt">UserPrim</span>] <span class="ot">-&gt;</span> <span class="dt">Compiler</span> [<span class="dt">CExtDecl</span>]</a>
<a class="sourceLine" id="cb7-2" data-line-number="2">    compileScheme <span class="fu">=</span> addPrimops <span class="fu">&gt;=&gt;</span> makeMain <span class="fu">&gt;=&gt;</span> cpsify <span class="fu">&gt;=&gt;</span> optimizeCPS <span class="fu">&gt;=&gt;</span> closConvert <span class="fu">&gt;=&gt;</span> codegen</a>
<a class="sourceLine" id="cb7-3" data-line-number="3">      <span class="kw">where</span> addPrimops <span class="fu">=</span> return <span class="fu">.</span> (<span class="fu">++</span>prims)</a></code></pre></div>
<p>This chains together all the phases of compilation into one big old function from the Scheme AST to the C one.</p>
<p>Now, if you’re really interested in <code>c_of_scheme</code>, go ahead and grab the source with</p>
<pre><code>hg clone ssh://hg@bitbucket.org/jozefg/c_of_scheme</code></pre>
<p>I do use mercurial so you can also grab a zip from bitbucket if you’re unwilling to use mercurial for one command :)</p>
<p>I should have posts about each specific phase of compilation up in Real Soon Now. I’ll edit with a list of links to posts below as they are written.</p>
<p><em>Thanks to <span class="citation" data-cites="tylerholien">@tylerholien</span> for proofreading</em></p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Sat, 07 Jun 2014 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2014-06-07-c-of-scheme.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Grokking recursion-scheme: Part 1</title>
    <link>http://jozefg.bitbucket.org/posts/2014-05-19-like-recursion-but-cooler.html</link>
    <description><![CDATA[<div class="info">
    Posted on May 19, 2014
    
</div>
<div class="info">
    
    Tags: <a href="/tags/haskell.html">haskell</a>
    
</div>

<p>This post is a little different than the rest of my blog, I’m not nearly as competent with <a href="https://hackage.haskell.org/package/recursion-schemes">recursion-schemes</a> as I want to be and I don’t understand them fully (yet). This isn’t entirely complete, but I hope it will provide a useful intuition for how to work with some of the lower ends of recursion-schemes and some idea of how to get into the higher end. I’ll be reading this again in two weeks once I’ve forgotten all of this (again). You’ve been warned…</p>
<h3 id="why-bother">Why Bother?</h3>
<p>First, let’s talk about why anyone would care about using a library like recursion-schemes.</p>
<p>Remember back in the good old days when all a programmer was <code>goto</code> and guts? And everyone hated it? We’re at a not dissimilar place in Haskell. Well, it’s not nearly so bad nowadays, however, our principle form of control flow is recursion and really we mostly use recursion in a raw, unprincipled way.</p>
<p>However, we’re starting to move away from it. Do these look familiar?</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb1-1" data-line-number="1"><span class="ot">    foldr ::</span> (a <span class="ot">-&gt;</span> b <span class="ot">-&gt;</span> b) <span class="ot">-&gt;</span> b <span class="ot">-&gt;</span> [a] <span class="ot">-&gt;</span> b</a>
<a class="sourceLine" id="cb1-2" data-line-number="2">    foldr f nil (x <span class="fu">:</span> xs) <span class="fu">=</span> x <span class="ot">`f`</span> foldr f nil xs</a>
<a class="sourceLine" id="cb1-3" data-line-number="3">    foldr f nil []       <span class="fu">=</span> nil</a></code></pre></div>
<p><code>foldr</code> is all about abstracting away raw recursion! <code>foldr</code> is great in this way since it covers a surprisingly large cover of cases</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb2-1" data-line-number="1"><span class="ot">    map ::</span> (a <span class="ot">-&gt;</span> b) <span class="ot">-&gt;</span> [a] <span class="ot">-&gt;</span> [b]</a>
<a class="sourceLine" id="cb2-2" data-line-number="2">    map f <span class="fu">=</span> foldr ((<span class="fu">:</span>) <span class="fu">.</span> f) []</a>
<a class="sourceLine" id="cb2-3" data-line-number="3"></a>
<a class="sourceLine" id="cb2-4" data-line-number="4"><span class="ot">    filter ::</span> (a <span class="ot">-&gt;</span> <span class="dt">Bool</span>) <span class="ot">-&gt;</span> [a] <span class="ot">-&gt;</span> [a]</a>
<a class="sourceLine" id="cb2-5" data-line-number="5">    filter p <span class="fu">=</span> foldr (\x rest <span class="ot">-&gt;</span> <span class="kw">if</span> p x <span class="kw">then</span> x <span class="fu">:</span> rest <span class="kw">else</span> rest) []</a></code></pre></div>
<p>Turns out you can implement quite a lot of Data.List with <code>foldr</code> and poor judgment.</p>
<p>However, this isn’t good enough. For example, I do a lot of work with compilers and therefore spend a lot of time doing transformations on trees. I want something like <code>foldr</code> to deal with this.</p>
<p>recursion-schemes is one such option. It’s a way of generalizing these uniform transformations on structures and it’s expanded to cover <em>a lot</em> transformations.</p>
<h3 id="on-to-recursion-schemes">On to recursion-schemes</h3>
<p>Now that we know that recursion-schemes is solving a useful problem, let’s get into actually using it. First, we can install it off of hackage</p>
<pre><code>cabal install recursion-schemes</code></pre>
<p>And import everything with</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" data-line-number="1">    <span class="ot">{-# LANGUAGE TypeFamilies, DeriveFunctor #-}</span></a>
<a class="sourceLine" id="cb4-2" data-line-number="2">    <span class="kw">import</span> <span class="dt">Data.Functor.Foldable</span></a></code></pre></div>
<p>Let’s get started by seeing how recursion-schemes covers <code>foldr</code></p>
<p>First, we define our own custom list</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb5-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">MyList</span> a <span class="fu">=</span> <span class="dt">MyCons</span> a (<span class="dt">MyList</span> a) <span class="fu">|</span> <span class="dt">MyNil</span></a></code></pre></div>
<p>Next, we define another type of list, with the recursion factored out</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb6-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">BList</span> a b <span class="fu">=</span> <span class="dt">BCons</span> a b <span class="fu">|</span> <span class="dt">BNil</span></a>
<a class="sourceLine" id="cb6-2" data-line-number="2">         <span class="kw">deriving</span> <span class="dt">Functor</span></a></code></pre></div>
<p>Here <code>b</code> is the recursive bit of <code>BList</code> factored out into an explicit parameter. So</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb7-1" data-line-number="1">    <span class="dt">MyList</span> a <span class="fu">~</span> <span class="dt">BList</span> a (<span class="dt">BList</span> a (<span class="dt">BList</span> a <span class="fu">....</span>))</a></code></pre></div>
<p>The fancy term for this would be to say that <code>List a</code> is the “fixed point” for <code>BList a</code>.</p>
<p>Now we can actually use recursion-schemes</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb8-1" data-line-number="1">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">Base</span> (<span class="dt">List</span> a) <span class="fu">=</span> <span class="dt">BList</span> a</a>
<a class="sourceLine" id="cb8-2" data-line-number="2"></a>
<a class="sourceLine" id="cb8-3" data-line-number="3">    <span class="kw">instance</span> <span class="dt">Foldable</span> (<span class="dt">List</span> a) <span class="kw">where</span></a>
<a class="sourceLine" id="cb8-4" data-line-number="4">      project (<span class="dt">MyCons</span> a b) <span class="fu">=</span> <span class="dt">BCons</span> a b</a>
<a class="sourceLine" id="cb8-5" data-line-number="5">      project <span class="dt">MyNil</span>        <span class="fu">=</span> <span class="dt">BNil</span></a></code></pre></div>
<p>And we’re done. So to understand what’s going on we need to talk about another data type and a little math.</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb9-1" data-line-number="1">    <span class="kw">newtype</span> <span class="dt">Fix</span> f a <span class="fu">=</span> <span class="dt">Fix</span> {<span class="ot">unFix ::</span> f (<span class="dt">Fix</span> f a)}</a></code></pre></div>
<p>Remember before how I mentioned how <code>MyList</code> is the fixed point of <code>BList</code>? Well <code>Fix</code> let’s us exploit this fact. In particular</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb10-1" data-line-number="1"><span class="ot">    out ::</span> <span class="dt">Fix</span> (<span class="dt">BList</span> a) <span class="ot">-&gt;</span> <span class="dt">MyList</span> a</a>
<a class="sourceLine" id="cb10-2" data-line-number="2">    out (<span class="dt">Fix</span> (<span class="dt">BCons</span> a rest)) <span class="fu">=</span> <span class="dt">MyCons</span> a (out rest)</a>
<a class="sourceLine" id="cb10-3" data-line-number="3"></a>
<a class="sourceLine" id="cb10-4" data-line-number="4"><span class="ot">    into ::</span> <span class="dt">MyList</span> a <span class="ot">-&gt;</span> <span class="dt">Fix</span> (<span class="dt">BList</span> a)</a>
<a class="sourceLine" id="cb10-5" data-line-number="5">    into (<span class="dt">MyCons</span> a rest) <span class="fu">=</span> <span class="dt">Fix</span> (<span class="dt">BCons</span> a <span class="fu">$</span> into rest)</a></code></pre></div>
<p>So we could write either <code>BList</code> or <code>MyList</code> for all our data types, but the <code>BList</code> version is really a pain to write since everything is wrapped in <code>Fix</code>. Unfortunately though, it’s much easier to write generic code for stuff of the form <code>Fix (f a)</code>.</p>
<p>To solve this recursion-schemes has the type class <code>Base</code> where we map the recursive data type to its non-recursive friend. Then, in <code>project</code> we define how to.. well.. project the recursive into a partially unfolded equivalent.</p>
<p>With just those two steps, we get a large chunk of recursion-schemes operations for our data type!</p>
<h3 id="just-what-did-we-get">Just What Did We Get?</h3>
<p>Now this was the part I really had trouble with in recursion-schemes the names of the functions for <code>Foldable</code> are… opaque if you’re not familiar with the terminology.</p>
<p>The most basic one is <code>cata</code>, which is the “catamorphism” across our data type. I’m not going to trouble you with why we call it a catamorphism, but just remember that it’s the souped-up version of <code>foldr</code>.</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb11-1" data-line-number="1"><span class="ot">    foldr ::</span> (a <span class="ot">-&gt;</span> b <span class="ot">-&gt;</span> b)          <span class="ot">-&gt;</span> b <span class="ot">-&gt;</span> [a]    <span class="ot">-&gt;</span> b</a>
<a class="sourceLine" id="cb11-2" data-line-number="2"><span class="ot">    foldr ::</span> ((a, b) <span class="ot">-&gt;</span> b)          <span class="ot">-&gt;</span> b <span class="ot">-&gt;</span> [a]    <span class="ot">-&gt;</span> b</a>
<a class="sourceLine" id="cb11-3" data-line-number="3"><span class="ot">    cata  ::</span> (<span class="dt">Fix</span> <span class="dt">BList</span> a <span class="ot">-&gt;</span> b)     <span class="ot">-&gt;</span> <span class="dt">List</span> a      <span class="ot">-&gt;</span> b</a>
<a class="sourceLine" id="cb11-4" data-line-number="4"><span class="ot">    cata  ::</span> (<span class="dt">Base</span> (<span class="dt">List</span> a) a <span class="ot">-&gt;</span> b) <span class="ot">-&gt;</span> <span class="dt">List</span> a      <span class="ot">-&gt;</span> b</a>
<a class="sourceLine" id="cb11-5" data-line-number="5"><span class="ot">    cata  ::</span> (<span class="dt">Base</span> t b <span class="ot">-&gt;</span> b)        <span class="ot">-&gt;</span> t           <span class="ot">-&gt;</span> b</a></code></pre></div>
<p>And we can use it the same way!</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb12-1" data-line-number="1"><span class="ot">    map ::</span> (a <span class="ot">-&gt;</span> b) <span class="ot">-&gt;</span> <span class="dt">List</span> a <span class="ot">-&gt;</span> <span class="dt">List</span> b</a>
<a class="sourceLine" id="cb12-2" data-line-number="2">    map f <span class="fu">=</span> cata mapper</a>
<a class="sourceLine" id="cb12-3" data-line-number="3">      <span class="kw">where</span> mapper (<span class="dt">BCons</span> a b) <span class="fu">=</span> f a <span class="ot">`MyCons`</span> b</a>
<a class="sourceLine" id="cb12-4" data-line-number="4">            mapper <span class="dt">BNil</span>        <span class="fu">=</span> <span class="dt">MyNil</span></a>
<a class="sourceLine" id="cb12-5" data-line-number="5"></a>
<a class="sourceLine" id="cb12-6" data-line-number="6"></a>
<a class="sourceLine" id="cb12-7" data-line-number="7"><span class="ot">    myfilter ::</span> (a <span class="ot">-&gt;</span> <span class="dt">Bool</span>) <span class="ot">-&gt;</span> <span class="dt">List</span> a <span class="ot">-&gt;</span> <span class="dt">List</span> a</a>
<a class="sourceLine" id="cb12-8" data-line-number="8">    myfilter p <span class="fu">=</span> cata filterer</a>
<a class="sourceLine" id="cb12-9" data-line-number="9">      <span class="kw">where</span> filterer (<span class="dt">BCons</span> a b) <span class="fu">=</span> <span class="kw">if</span> p a <span class="kw">then</span> a <span class="ot">`MyCons`</span> b <span class="kw">else</span> b</a>
<a class="sourceLine" id="cb12-10" data-line-number="10">            filterer <span class="dt">BNil</span>        <span class="fu">=</span> <span class="dt">MyNil</span></a></code></pre></div>
<p>Now we can all tell people that we’ve written map using a catamorphism.</p>
<p>Careful readers will notice one big difference between <code>foldr</code> and <code>cata</code>: <code>cata</code> doesn’t take a seed! Indeed with <code>foldr</code> we replace all the constructors of our list with the function <code>f</code>, so</p>
<div class="sourceCode" id="cb13"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb13-1" data-line-number="1">    <span class="dv">1</span> <span class="fu">:</span> <span class="dv">2</span> <span class="fu">:</span> <span class="dv">3</span> <span class="fu">:</span> <span class="dv">4</span> <span class="fu">:</span> []</a>
<a class="sourceLine" id="cb13-2" data-line-number="2">    <span class="dv">1</span> <span class="ot">`f`</span> <span class="dv">2</span> <span class="ot">`f`</span> <span class="dv">3</span> <span class="ot">`f`</span> <span class="dv">4</span> <span class="ot">`f`</span> seed</a></code></pre></div>
<p>This doesn’t generalize well though, what if we have a type with a constructor of 3 arguments? Or 5? To avoid this problem, recursion-schemes takes a clever approach.</p>
<p>Remember that <code>BList</code> factors out recursion? <code>cata</code> works by collapsing a sublist recursively and sticking the slot back into the slot of the original list. So we actually have something like</p>
<div class="sourceCode" id="cb14"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb14-1" data-line-number="1">    <span class="dt">BCons</span> <span class="dv">1</span> (<span class="dt">BCons</span> <span class="dv">2</span> (<span class="dt">BCons</span> <span class="dv">3</span> (<span class="dt">BCons</span> <span class="dv">4</span> <span class="dt">BNil</span>)))</a>
<a class="sourceLine" id="cb14-2" data-line-number="2">    <span class="dt">BCons</span> <span class="dv">1</span> (f (<span class="dt">BCons</span> <span class="dv">2</span> (f (<span class="dt">BCons</span> <span class="dv">3</span> (f (<span class="dt">BCons</span> <span class="dv">4</span> (f <span class="dt">Nil</span>)))))))</a></code></pre></div>
<p>Now <code>f</code> has to handle all possible cases of our constructor, so it handles both the seed value and the collapsing case! And this generalizing beautifully by just delegating all the constructor specific work to <code>f</code> this is how it’s possible to derive <code>cata</code> practically for free.</p>
<p>Now, since recursion-schemes already has an instance for <code>[a]</code>, I’ll dispense with <code>MyList</code> since it’s a bit clunky.</p>
<p>Our foldable instance gives us quite a bit more than just <code>foldr</code> however! We also get this function <code>para</code>, short for “paramorphisms”. A paramorphism is like a fold, but also gives a “snapshot” of the structure at the point we’re folding. So if we wanted to sum each tail of a list, we could do something like</p>
<div class="sourceCode" id="cb15"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb15-1" data-line-number="1"><span class="ot">    sumTails ::</span> <span class="dt">Num</span> a <span class="ot">=&gt;</span> [a] <span class="ot">-&gt;</span> [a]</a>
<a class="sourceLine" id="cb15-2" data-line-number="2">    sumTails <span class="fu">=</span> para summer</a>
<a class="sourceLine" id="cb15-3" data-line-number="3">      <span class="kw">where</span> summer (<span class="dt">Cons</span> a (list, rest)) <span class="fu">=</span> a <span class="fu">+</span> sum list <span class="fu">:</span> rest</a>
<a class="sourceLine" id="cb15-4" data-line-number="4">            summer <span class="dt">Nil</span>                   <span class="fu">=</span> []</a></code></pre></div>
<p>This could be useful for example, if you’re doing any context dependent operations on a structure. Later, I’ll try to include some more practical examples of a paramorphism (I never thought I’d say those words).</p>
<p>Now recursion-schemes includes <em>generalized</em> versions of all of these but I’m not brave enough to try to explain them right now.</p>
<h3 id="a-real-example">A Real Example</h3>
<p>Before we wrap this post up, let’s demonstrate an actual useful example of recursion-schemes.</p>
<p>We’re going to implement trivial constant folding in a made up language I’ll call Foo.</p>
<p>The AST for Foo is something like</p>
<div class="sourceCode" id="cb16"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb16-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">Op</span> <span class="fu">=</span> <span class="dt">Plus</span> <span class="fu">|</span> <span class="dt">Sub</span> <span class="fu">|</span> <span class="dt">Mult</span> <span class="fu">|</span> <span class="dt">Div</span></a>
<a class="sourceLine" id="cb16-2" data-line-number="2"></a>
<a class="sourceLine" id="cb16-3" data-line-number="3">    <span class="kw">data</span> <span class="dt">Foo</span> <span class="fu">=</span> <span class="dt">Num</span> <span class="dt">Int</span>           <span class="co">-- Numeric literals</span></a>
<a class="sourceLine" id="cb16-4" data-line-number="4">             <span class="fu">|</span> <span class="dt">String</span> <span class="dt">String</span>     <span class="co">-- String literals</span></a>
<a class="sourceLine" id="cb16-5" data-line-number="5">             <span class="fu">|</span> <span class="dt">Binop</span> <span class="dt">Op</span> <span class="dt">Foo</span> <span class="dt">Foo</span>  <span class="co">-- Primitive operation</span></a>
<a class="sourceLine" id="cb16-6" data-line-number="6">             <span class="fu">|</span> <span class="dt">Fun</span> <span class="dt">String</span> <span class="dt">Foo</span>    <span class="co">-- Lambda/Abstraction over terms</span></a>
<a class="sourceLine" id="cb16-7" data-line-number="7">             <span class="fu">|</span> <span class="dt">App</span> <span class="dt">Foo</span> <span class="dt">Foo</span>       <span class="co">-- Application</span></a>
<a class="sourceLine" id="cb16-8" data-line-number="8">             <span class="fu">|</span> <span class="dt">Var</span> <span class="dt">String</span>        <span class="co">-- Variables</span></a>
<a class="sourceLine" id="cb16-9" data-line-number="9">             <span class="kw">deriving</span> <span class="dt">Show</span></a></code></pre></div>
<p>Now we want our trivial constant folding to reduce something like <code>Binop Plus (Num 1) (Num 2)</code> to just <code>Num 3</code>. Let’s first formalize this by writing a quick little reducer</p>
<div class="sourceCode" id="cb17"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb17-1" data-line-number="1"><span class="ot">    compute ::</span> <span class="dt">Op</span> <span class="ot">-&gt;</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">Int</span></a>
<a class="sourceLine" id="cb17-2" data-line-number="2">    compute <span class="dt">Plus</span> <span class="fu">=</span> (<span class="fu">+</span>)</a>
<a class="sourceLine" id="cb17-3" data-line-number="3">    compute <span class="dt">Sub</span>  <span class="fu">=</span> (<span class="fu">-</span>)</a>
<a class="sourceLine" id="cb17-4" data-line-number="4">    compute <span class="dt">Mult</span> <span class="fu">=</span> (<span class="fu">*</span>)</a>
<a class="sourceLine" id="cb17-5" data-line-number="5">    compute <span class="dt">Div</span>  <span class="fu">=</span> div</a>
<a class="sourceLine" id="cb17-6" data-line-number="6"></a>
<a class="sourceLine" id="cb17-7" data-line-number="7"><span class="ot">    reduce ::</span> <span class="dt">Foo</span> <span class="ot">-&gt;</span> <span class="dt">Foo</span></a>
<a class="sourceLine" id="cb17-8" data-line-number="8">    reduce (<span class="dt">Binop</span> op (<span class="dt">Num</span> a) (<span class="dt">Num</span> b)) <span class="fu">=</span> <span class="dt">Num</span> <span class="fu">$</span> compute op a b <span class="co">-- The reduction</span></a>
<a class="sourceLine" id="cb17-9" data-line-number="9">    reduce a                          <span class="fu">=</span> a</a></code></pre></div>
<p>So we compute all constant expressions and leave everything else alone. This is pretty simple, but how can we apply it to <em>every</em> element in our AST? Well, time to break out recursion-schemes</p>
<div class="sourceCode" id="cb18"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb18-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">FooB</span> a <span class="fu">=</span> <span class="dt">NumB</span> <span class="dt">Int</span></a>
<a class="sourceLine" id="cb18-2" data-line-number="2">                <span class="fu">|</span> <span class="dt">StringB</span> <span class="dt">String</span></a>
<a class="sourceLine" id="cb18-3" data-line-number="3">                <span class="fu">|</span> <span class="dt">BinopB</span> <span class="dt">Op</span> a a</a>
<a class="sourceLine" id="cb18-4" data-line-number="4">                <span class="fu">|</span> <span class="dt">FunB</span> <span class="dt">String</span> a</a>
<a class="sourceLine" id="cb18-5" data-line-number="5">                <span class="fu">|</span> <span class="dt">App</span> a a</a>
<a class="sourceLine" id="cb18-6" data-line-number="6">                <span class="fu">|</span> <span class="dt">Var</span> <span class="dt">String</span></a>
<a class="sourceLine" id="cb18-7" data-line-number="7">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">Base</span> <span class="dt">Foo</span> <span class="fu">=</span> <span class="dt">FooB</span></a>
<a class="sourceLine" id="cb18-8" data-line-number="8"></a>
<a class="sourceLine" id="cb18-9" data-line-number="9">    <span class="kw">instance</span> <span class="dt">Foldable</span> <span class="dt">Foo</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb18-10" data-line-number="10">      project (<span class="dt">Num</span> a)        <span class="fu">=</span> <span class="dt">NumB</span> a</a>
<a class="sourceLine" id="cb18-11" data-line-number="11">      project (<span class="dt">String</span> a)     <span class="fu">=</span> <span class="dt">StringB</span> a</a>
<a class="sourceLine" id="cb18-12" data-line-number="12">      project (<span class="dt">Binop</span> op a b) <span class="fu">=</span> <span class="dt">BinopB</span> op a b</a>
<a class="sourceLine" id="cb18-13" data-line-number="13">      project (<span class="dt">Fun</span> v a)      <span class="fu">=</span> <span class="dt">FunB</span> v a</a>
<a class="sourceLine" id="cb18-14" data-line-number="14">      project (<span class="dt">App</span> a b)      <span class="fu">=</span> <span class="dt">AppB</span> a b</a>
<a class="sourceLine" id="cb18-15" data-line-number="15">      project (<span class="dt">Var</span> a)        <span class="fu">=</span> <span class="dt">VarB</span> a</a>
<a class="sourceLine" id="cb18-16" data-line-number="16"></a>
<a class="sourceLine" id="cb18-17" data-line-number="17">    <span class="co">-- reverse of project</span></a>
<a class="sourceLine" id="cb18-18" data-line-number="18"><span class="ot">    rProject ::</span> <span class="dt">Base</span> <span class="dt">Foo</span> <span class="dt">Foo</span> <span class="ot">-&gt;</span> <span class="dt">Foo</span></a>
<a class="sourceLine" id="cb18-19" data-line-number="19">    rProject (<span class="dt">NumB</span> a)        <span class="fu">=</span> <span class="dt">Num</span> a</a>
<a class="sourceLine" id="cb18-20" data-line-number="20">    rProject (<span class="dt">StringB</span> a)     <span class="fu">=</span> <span class="dt">String</span> a</a>
<a class="sourceLine" id="cb18-21" data-line-number="21">    rProject (<span class="dt">BinopB</span> op a b) <span class="fu">=</span> <span class="dt">Binop</span> op a b</a>
<a class="sourceLine" id="cb18-22" data-line-number="22">    rProject (<span class="dt">FunB</span> v a)      <span class="fu">=</span> <span class="dt">Fun</span> v a</a>
<a class="sourceLine" id="cb18-23" data-line-number="23">    rProject (<span class="dt">AppB</span> a b)      <span class="fu">=</span> <span class="dt">App</span> a b</a>
<a class="sourceLine" id="cb18-24" data-line-number="24">    rProject (<span class="dt">VarB</span> a)        <span class="fu">=</span> <span class="dt">Var</span> a</a></code></pre></div>
<p>And let’s rewrite <code>reduce</code> to use <code>FooB</code> instead of <code>Foo</code></p>
<div class="sourceCode" id="cb19"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb19-1" data-line-number="1"><span class="ot">    reduce ::</span> <span class="dt">Base</span> <span class="dt">Foo</span> <span class="dt">Foo</span> <span class="ot">-&gt;</span> <span class="dt">Foo</span></a>
<a class="sourceLine" id="cb19-2" data-line-number="2">    reduce (<span class="dt">Fix</span> (<span class="dt">BinopB</span> op (<span class="dt">Num</span> a) (<span class="dt">Num</span> b))) <span class="fu">=</span> <span class="dt">Num</span> <span class="fu">$</span> compute op a b <span class="co">-- The reduction</span></a>
<a class="sourceLine" id="cb19-3" data-line-number="3">    reduce a                                 <span class="fu">=</span> rProject a</a></code></pre></div>
<p>So this entire traversal now just becomes</p>
<div class="sourceCode" id="cb20"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb20-1" data-line-number="1"><span class="ot">    constFold ::</span> <span class="dt">Foo</span> <span class="ot">-&gt;</span> <span class="dt">Foo</span></a>
<a class="sourceLine" id="cb20-2" data-line-number="2">    constFold <span class="fu">=</span> cata reduce</a></code></pre></div>
<p>Now we can test our simple optimization</p>
<div class="sourceCode" id="cb21"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb21-1" data-line-number="1">    test <span class="fu">=</span> <span class="dt">Binop</span> <span class="dt">Plus</span> (<span class="dt">Num</span> <span class="dv">1</span>) (<span class="dt">Binop</span> <span class="dt">Mult</span> (<span class="dt">Num</span> <span class="dv">2</span>) (<span class="dt">Num</span> <span class="dv">3</span>))</a>
<a class="sourceLine" id="cb21-2" data-line-number="2">    optimized <span class="fu">=</span> constFold test</a>
<a class="sourceLine" id="cb21-3" data-line-number="3">    main <span class="fu">=</span> print optimized</a></code></pre></div>
<p>As we’d hope, this prints out <code>Num 7</code>!</p>
<p>This seems like a lot of work but don’t forget, now that we’ve taught recursion-schemes how to do traversals, we get all of this for free. For example, let’s now write a function to grab all the free variables of an expression.</p>
<p>As before, let’s start by writing the simple worker function for this traversal.</p>
<div class="sourceCode" id="cb22"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb22-1" data-line-number="1"><span class="ot">     freeVar ::</span> <span class="dt">Base</span> <span class="dt">Foo</span> [<span class="dt">String</span>] <span class="ot">-&gt;</span> [<span class="dt">String</span>]</a>
<a class="sourceLine" id="cb22-2" data-line-number="2">     freeVar (<span class="dt">NumB</span> _)         <span class="fu">=</span> []</a>
<a class="sourceLine" id="cb22-3" data-line-number="3">     freeVar (<span class="dt">StringB</span> _)      <span class="fu">=</span> []</a>
<a class="sourceLine" id="cb22-4" data-line-number="4">     freeVar (<span class="dt">VarB</span> s)         <span class="fu">=</span> [s]</a>
<a class="sourceLine" id="cb22-5" data-line-number="5">     freeVar (<span class="dt">BinopB</span> _ v1 v2) <span class="fu">=</span> v1 <span class="fu">++</span> v2</a>
<a class="sourceLine" id="cb22-6" data-line-number="6">     freeVar (<span class="dt">AppB</span> v1 v2)     <span class="fu">=</span> v1 <span class="fu">++</span> v2</a>
<a class="sourceLine" id="cb22-7" data-line-number="7">     freeVar (<span class="dt">FunB</span> v vs)      <span class="fu">=</span> delete v vs</a></code></pre></div>
<p>Now the full traversal is trivial!</p>
<div class="sourceCode" id="cb23"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb23-1" data-line-number="1"><span class="ot">    freeIn ::</span> <span class="dt">Foo</span> <span class="ot">-&gt;</span> [<span class="dt">String</span>]</a>
<a class="sourceLine" id="cb23-2" data-line-number="2">    freeIn <span class="fu">=</span> cata freeVars</a></code></pre></div>
<p>As we’d hope, this traversal is much easier to write than the first one. You can imagine that the boilerplate of writing <code>FooB</code> and <code>project</code> is amortized over each traversal, making it much easier to write subsequent traversals once we’ve gone through the trouble of actually laying down the foundation.</p>
<h3 id="whats-next">What’s Next?</h3>
<p>So far I’ve discussed part of the <code>Foldable</code> half of the recursion-schemes library. In my next post I’ll cover anamorphisms and <code>Unfoldable</code>, the dual of what we’ve talked about here.</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Mon, 19 May 2014 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2014-05-19-like-recursion-but-cooler.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Getting Proper Tail Calls Out of C</title>
    <link>http://jozefg.bitbucket.org/posts/2014-05-05-i-used-c-correctly.html</link>
    <description><![CDATA[<div class="info">
    Posted on May  5, 2014
    
</div>
<div class="info">
    
    Tags: <a href="/tags/c.html">c</a>, <a href="/tags/compilers.html">compilers</a>
    
</div>

<p>While I don’t exactly love <em>writing</em> C, it has a lot to offer as a compilation target. Its got lots of smart compilers that can target just about every platform I’ve ever heard of and tons of others, its got the ability to mess with low level aspects of itself, and C’s got some nice high level abstractions like functions.</p>
<p>One big issue I have with it as a target is that its function calls suck. I’m usually compiling a functional language where tail call optimization is imperative (heh) and C makes this a lot harder than it should.</p>
<p>This post illustrates how I currently beat C into actually generating proper tail calls. Most of the code from this post is straight from <a href="http://www.bitbucket.org/jozefg/c_of_scheme">c_of_scheme</a>. If you’re having trouble understanding some function than there may actually be documentation for it in the source :)</p>
<p>The first step involves something called continuation passing style. The idea here is that reify the implicit “continuation” for each expression to an explicitly function.</p>
<p>So we’ll turn something like</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode scheme"><code class="sourceCode scheme"><a class="sourceLine" id="cb1-1" data-line-number="1">    (<span class="op">+</span> <span class="dv">1</span> (* <span class="dv">2</span> <span class="dv">2</span>))</a></code></pre></div>
<p>Into something like</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode scheme"><code class="sourceCode scheme"><a class="sourceLine" id="cb2-1" data-line-number="1">    (<span class="kw">lambda</span> (k)</a>
<a class="sourceLine" id="cb2-2" data-line-number="2">       ((<span class="kw">lambda</span> (mult-result)</a>
<a class="sourceLine" id="cb2-3" data-line-number="3">          (k (<span class="op">+</span> <span class="dv">1</span> mult-result)))</a>
<a class="sourceLine" id="cb2-4" data-line-number="4">         (* <span class="dv">2</span> <span class="dv">2</span>)))</a></code></pre></div>
<p>Notice how now the order of evaluation is completely determined by how we pass things around? We pass each result along the chain of continuations and every non-primitive function becomes a tail call.</p>
<p>This has one more very important effect, none of these function calls will return. We’re going to pass control off to each continuation and the very last function will exit the program. This means that as soon as we call a continuation, we can nuke the stack and every function call has become identical to calling a continuation.</p>
<p>C actually has a similar notion to this and when we run this code through closure conversion and lambda lifting (a subject that’s worth of its own rant) we’ll end up with functions that look something like</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode c"><code class="sourceCode c"><a class="sourceLine" id="cb3-1" data-line-number="1">    <span class="dt">void</span> _gen1(scm_t arg, scm_t cont, ...){</a>
<a class="sourceLine" id="cb3-2" data-line-number="2">       scm_apply(cont, scm_add(scm_t arg, <span class="dv">1</span>));</a>
<a class="sourceLine" id="cb3-3" data-line-number="3">    }</a></code></pre></div>
<p>It’s worth a mention that <code>scm_apply</code> will unwrap the continuation and actually apply it since it’s just a normal function. We know that the call to <code>scm_apply</code> will never return. We can tell C this with <code>__attribute__((noreturn))</code>. Theoretically this also enables the use of something much like TCO: once the last function is called, we can reuse the stack frame of <code>_gen1</code> and if the function actually returns despite our promises simply segfault (hooray for C).</p>
<p>Unfortunately, GCC doesn’t seem to do this on its own in my case. So I cried for a little bit and offered it many flags in the hopes that it would be merciful and just do it for me but it didn’t. And now I can actually illustrate how I did this manually.</p>
<p>It turns out this is possible to do with only a tiny impact on the generated code from the compiler and a bit of monkeying with <code>scm_apply</code>. First, I’ll explain how <code>scm_apply</code> looks normally.</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode c"><code class="sourceCode c"><a class="sourceLine" id="cb4-1" data-line-number="1">    <span class="dt">void</span> scm_apply(<span class="dt">int</span> i, scm_t f, ...) {</a>
<a class="sourceLine" id="cb4-2" data-line-number="2">      <span class="dt">int</span> x;</a>
<a class="sourceLine" id="cb4-3" data-line-number="3">      <span class="dt">va_list</span> va;</a>
<a class="sourceLine" id="cb4-4" data-line-number="4">      scm_t *arg_list = malloc(<span class="kw">sizeof</span>(scm_t) * i + <span class="dv">1</span>);</a>
<a class="sourceLine" id="cb4-5" data-line-number="5">      va_start(va, f);</a>
<a class="sourceLine" id="cb4-6" data-line-number="6">      <span class="cf">for</span>(x = <span class="dv">1</span>; x &lt; i+<span class="dv">1</span>; ++x){</a>
<a class="sourceLine" id="cb4-7" data-line-number="7">        arg_list[x] = va_arg(va, scm_t);</a>
<a class="sourceLine" id="cb4-8" data-line-number="8">      }</a>
<a class="sourceLine" id="cb4-9" data-line-number="9">      <span class="cf">if</span>(f-&gt;state != <span class="dv">4</span>){</a>
<a class="sourceLine" id="cb4-10" data-line-number="10">        printf(<span class="st">&quot;Attempted to apply nonfunction</span><span class="sc">\n</span><span class="st">&quot;</span>);</a>
<a class="sourceLine" id="cb4-11" data-line-number="11">        exit(<span class="dv">1</span>);</a>
<a class="sourceLine" id="cb4-12" data-line-number="12">      } <span class="cf">else</span> {</a>
<a class="sourceLine" id="cb4-13" data-line-number="13">        arg_list[<span class="dv">0</span>] = f-&gt;val.scm_lam.clos;</a>
<a class="sourceLine" id="cb4-14" data-line-number="14">        f-&gt;val.scm_lam.fun(arg_list);</a>
<a class="sourceLine" id="cb4-15" data-line-number="15">      }</a>
<a class="sourceLine" id="cb4-16" data-line-number="16">    }</a></code></pre></div>
<p>Note that <code>scm_t</code> is a pointer to a discriminated union in C to fake the dynamic types found in Scheme.</p>
<p>So the first bit is just the varargs goo to extract the arguments given to <code>scm_apply</code>. Once we have all of those in an array, we look at the <code>state</code> field of <code>f</code>, our function. If it’s not 4, then we don’t really have a function so we complain loudly and exit. Otherwise we just get the actual function pointer out of <code>f</code> and call it.</p>
<p>This is a little tricky to read if you’re not familiar with the DU’s in C, but there’s nothing exactly earth shattering in there.</p>
<p>Now, since every function call is going through <code>scm_apply</code>, we add a global ticker to count how many function calls have gone through there</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode c"><code class="sourceCode c"><a class="sourceLine" id="cb5-1" data-line-number="1">    <span class="dt">static</span> <span class="dt">int</span> stack_frames;</a>
<a class="sourceLine" id="cb5-2" data-line-number="2">    ...</a>
<a class="sourceLine" id="cb5-3" data-line-number="3">    <span class="dt">void</span> scm_apply(<span class="dt">int</span> i, scm_t f, ...) {</a>
<a class="sourceLine" id="cb5-4" data-line-number="4">        ...</a>
<a class="sourceLine" id="cb5-5" data-line-number="5">        <span class="cf">else</span> {</a>
<a class="sourceLine" id="cb5-6" data-line-number="6">            ++stack_frames;</a>
<a class="sourceLine" id="cb5-7" data-line-number="7">            ....</a>
<a class="sourceLine" id="cb5-8" data-line-number="8">        }</a>
<a class="sourceLine" id="cb5-9" data-line-number="9">    }</a></code></pre></div>
<p>Now we know just <em>how</em> quickly we’re burning through the available stack space.</p>
<p>Next we need to add a special case of <code>scm_apply</code> which we’ll call <code>scm_init</code>. It looks like this</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode c"><code class="sourceCode c"><a class="sourceLine" id="cb6-1" data-line-number="1">    <span class="dt">void</span> scm_init(lam_t f){</a>
<a class="sourceLine" id="cb6-2" data-line-number="2">       stack_frames = <span class="dv">0</span>;</a>
<a class="sourceLine" id="cb6-3" data-line-number="3">       scm_apply(<span class="dv">0</span>, mkLam(scm_top_clos, f)); <span class="co">// Call main</span></a>
<a class="sourceLine" id="cb6-4" data-line-number="4">    }</a></code></pre></div>
<p>All this does is initialize <code>stack_frames</code> and call <code>scm_apply</code>. We can modify the codegen so that the <code>main</code> function is passed to <code>scm_init</code>. We know that this main function will take no arguments in <code>c_of_scheme</code> for reasons that aren’t entirely relevant to this post.</p>
<p>OK, so now is the magic and like all good C magic, it starts by including <code>setjmp</code>.</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode c"><code class="sourceCode c"><a class="sourceLine" id="cb7-1" data-line-number="1">    <span class="pp">#include </span><span class="im">&lt;setjmp.h&gt;</span></a></code></pre></div>
<p>Now we add 3 more global variables (please don’t hate me)</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode c"><code class="sourceCode c"><a class="sourceLine" id="cb8-1" data-line-number="1">    <span class="dt">static</span> scm_t  current_fun;</a>
<a class="sourceLine" id="cb8-2" data-line-number="2">    <span class="dt">static</span> scm_t* current_args;</a>
<a class="sourceLine" id="cb8-3" data-line-number="3">    <span class="dt">static</span> jmp_buf env;</a></code></pre></div>
<p>Now we modify <code>scm_apply</code> so that if we’re at a depth of 100 function calls or more we stick the current function and arguments into these global variables and <code>longjmp</code> with <code>env</code>!</p>
<p>Now we need a good place to <code>longjmp</code> to, the place where <code>env</code> points to. This is what <code>scm_init</code>, we know that it’s called almost immediately so it’s relatively “low” on the stack. So <code>scm_init</code> now becomes</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode c"><code class="sourceCode c"><a class="sourceLine" id="cb9-1" data-line-number="1">    <span class="dt">void</span> scm_init(lam_t f){</a>
<a class="sourceLine" id="cb9-2" data-line-number="2">      stack_frames = <span class="dv">0</span>;</a>
<a class="sourceLine" id="cb9-3" data-line-number="3"></a>
<a class="sourceLine" id="cb9-4" data-line-number="4">      <span class="cf">if</span>(setjmp(env)){</a>
<a class="sourceLine" id="cb9-5" data-line-number="5">         stack_frames = <span class="dv">0</span>;</a>
<a class="sourceLine" id="cb9-6" data-line-number="6">         current_fun-&gt;val.scm_lam.fun(current_args);</a>
<a class="sourceLine" id="cb9-7" data-line-number="7">      }</a>
<a class="sourceLine" id="cb9-8" data-line-number="8">      scm_apply(<span class="dv">0</span>, mkLam(scm_top_clos, f)); <span class="co">// Call main</span></a>
<a class="sourceLine" id="cb9-9" data-line-number="9">    }</a></code></pre></div>
<p>Notice that we do know error checking and just go straight into calling the next function after a <code>longjmp</code>. In order to set up <code>current_fun</code> and <code>current_args</code> correctly <code>scm_apply</code> must be modified</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode c"><code class="sourceCode c"><a class="sourceLine" id="cb10-1" data-line-number="1">    <span class="dt">void</span> scm_apply(<span class="dt">int</span> i, scm_t f, ...) {</a>
<a class="sourceLine" id="cb10-2" data-line-number="2">      <span class="dt">int</span> x;</a>
<a class="sourceLine" id="cb10-3" data-line-number="3">      <span class="dt">va_list</span> va;</a>
<a class="sourceLine" id="cb10-4" data-line-number="4">      scm_t *arg_list = malloc(<span class="kw">sizeof</span>(scm_t) * i + <span class="dv">1</span>);</a>
<a class="sourceLine" id="cb10-5" data-line-number="5">      va_start(va, f);</a>
<a class="sourceLine" id="cb10-6" data-line-number="6">      <span class="cf">for</span>(x = <span class="dv">1</span>; x &lt; i+<span class="dv">1</span>; ++x){</a>
<a class="sourceLine" id="cb10-7" data-line-number="7">        arg_list[x] = va_arg(va, scm_t);</a>
<a class="sourceLine" id="cb10-8" data-line-number="8">      }</a>
<a class="sourceLine" id="cb10-9" data-line-number="9">      <span class="cf">if</span>(f-&gt;state != <span class="dv">4</span>){</a>
<a class="sourceLine" id="cb10-10" data-line-number="10">        printf(<span class="st">&quot;Attempted to apply nonfunction</span><span class="sc">\n</span><span class="st">&quot;</span>);</a>
<a class="sourceLine" id="cb10-11" data-line-number="11">        exit(<span class="dv">1</span>);</a>
<a class="sourceLine" id="cb10-12" data-line-number="12">      } <span class="cf">else</span> {</a>
<a class="sourceLine" id="cb10-13" data-line-number="13">        arg_list[<span class="dv">0</span>] = f-&gt;val.scm_lam.clos;</a>
<a class="sourceLine" id="cb10-14" data-line-number="14"></a>
<a class="sourceLine" id="cb10-15" data-line-number="15">        <span class="cf">if</span>(stack_frames &gt;= <span class="dv">100</span>){</a>
<a class="sourceLine" id="cb10-16" data-line-number="16">          <span class="co">// Transfer continuation up</span></a>
<a class="sourceLine" id="cb10-17" data-line-number="17">          current_fun     = f;</a>
<a class="sourceLine" id="cb10-18" data-line-number="18">          current_args    = arg_list;</a>
<a class="sourceLine" id="cb10-19" data-line-number="19">          longjmp(env, <span class="dv">1</span>);</a>
<a class="sourceLine" id="cb10-20" data-line-number="20">        }</a>
<a class="sourceLine" id="cb10-21" data-line-number="21">        ++stack_frames;</a>
<a class="sourceLine" id="cb10-22" data-line-number="22">        f-&gt;val.scm_lam.fun(arg_list);</a>
<a class="sourceLine" id="cb10-23" data-line-number="23">      }</a>
<a class="sourceLine" id="cb10-24" data-line-number="24">    }</a></code></pre></div>
<p>This meant that now when we’ve applied 100 functions, we jump back to <code>scm_init</code>, demolishing all those unused stack frames and keep going.</p>
<p>There it is, that’s my minimally invasive technique for tail calls in C. From what I’ve heard this is also used by Chicken Scheme.</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Mon, 05 May 2014 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2014-05-05-i-used-c-correctly.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>You Could Have Invented GHC.Generics</title>
    <link>http://jozefg.bitbucket.org/posts/2014-04-25-you-could-have.html</link>
    <description><![CDATA[<div class="info">
    Posted on April 25, 2014
    
</div>
<div class="info">
    
    Tags: <a href="/tags/haskell.html">haskell</a>
    
</div>

<p>In Haskell right now there seem to be two main approaches to data-generic programming. There’s the whole <code>Typeable</code>/<code>Data</code> approaches which is a bit magic. Lately however, there has been a new kid on the block, GHC.Generics.</p>
<p>In this post we’ll step through the intuition for the library and (hopefully) help shed some light on why it exists and how to use it.</p>
<h2 id="boilerplate">Boilerplate</h2>
<p>Let’s imagine you, our young and brilliant Haskell hacker cranking out some code. You’ve probably gone the typesafe route and have lots and lots of types to encode invariants.</p>
<p>However, this proliferation of types is cramping your style a bit, you’re forced to create a new function over each type which seems to do exactly the same thing!</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb1-1" data-line-number="1"><span class="ot">    mapFoo  ::</span> (a <span class="ot">-&gt;</span> b) <span class="ot">-&gt;</span> <span class="dt">Foo</span> a  <span class="ot">-&gt;</span> <span class="dt">Foo</span> b</a>
<a class="sourceLine" id="cb1-2" data-line-number="2"><span class="ot">    mapBar  ::</span> (a <span class="ot">-&gt;</span> b) <span class="ot">-&gt;</span> <span class="dt">Bar</span> a  <span class="ot">-&gt;</span> <span class="dt">Bar</span> b</a>
<a class="sourceLine" id="cb1-3" data-line-number="3"><span class="ot">    mapQuux ::</span> (a <span class="ot">-&gt;</span> b) <span class="ot">-&gt;</span> <span class="dt">Quux</span> a <span class="ot">-&gt;</span> <span class="dt">Quux</span> b</a></code></pre></div>
<p>But, we’re clever enough to notice that this is obviously just <code>fmap</code>! So we can scrap all of this with <code>fmap</code> and <code>-XDeriveFunctor</code>.</p>
<p>But, what about other functions. There are a lot of things that are basically mechanical to define over each type. Serialization, field selection, and so on and so on. Each of these operations have something in common; they deal with the <em>structure</em> of the types rather than the actual representation of it.</p>
<p>Selecting the first fields from</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb2-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">Foo</span> a <span class="fu">=</span> <span class="dt">Foo</span> a a a</a>
<a class="sourceLine" id="cb2-2" data-line-number="2">    <span class="kw">data</span> <span class="dt">Bar</span> a <span class="fu">=</span> <span class="dt">Bar</span> a a a</a></code></pre></div>
<p>is almost identical! The only difference is in the name. So, let’s figure out a way to talk about the structure of our types.</p>
<h2 id="dissecting-an-algebraic-type">Dissecting an Algebraic Type</h2>
<p>Now, when we go to dissect some type <code>data Foo = ...</code> we have two things to consider</p>
<ol type="1">
<li>A list of constructors</li>
<li>A list of fields for each constructor</li>
</ol>
<p>Let’s start with (2) since it’s simpler. For types that are of the form</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb3-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">SomeType</span> <span class="fu">=</span> <span class="dt">OneConstructor</span> field1 field2 field3 <span class="fu">...</span></a></code></pre></div>
<p>we can almost think of them as really, really big tuples.</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" data-line-number="1">    <span class="kw">type</span> <span class="dt">SomeType&#39;</span> <span class="fu">=</span> (field1, field2, field3, <span class="fu">...</span>)</a></code></pre></div>
<p>But, since we want to encode different numbers of fields in just one type, let’s transform this further into</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb5-1" data-line-number="1">    <span class="kw">type</span> <span class="dt">SomeType&#39;&#39;</span> <span class="fu">=</span> (field1, (field2, (field3, <span class="fu">...</span>)))</a></code></pre></div>
<p>There we have it, we can encode lists of fields as a deeply nested group of tuples.</p>
<p>We can now imagine something like</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb6-1" data-line-number="1">    <span class="ot">{-# LANGUAGE TypeFamilies #-}</span></a>
<a class="sourceLine" id="cb6-2" data-line-number="2">    <span class="kw">type</span> family <span class="dt">TupleForm</span> a</a>
<a class="sourceLine" id="cb6-3" data-line-number="3"></a>
<a class="sourceLine" id="cb6-4" data-line-number="4">    <span class="kw">data</span> <span class="dt">Foo</span> a <span class="fu">=</span> <span class="dt">Foo</span> a a</a>
<a class="sourceLine" id="cb6-5" data-line-number="5">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">TupleForm</span> (<span class="dt">Foo</span> a) <span class="fu">=</span> (a, a)</a>
<a class="sourceLine" id="cb6-6" data-line-number="6"></a>
<a class="sourceLine" id="cb6-7" data-line-number="7">    <span class="kw">data</span> <span class="dt">Bar</span> a <span class="fu">=</span> <span class="dt">Bar</span> a a</a>
<a class="sourceLine" id="cb6-8" data-line-number="8">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">TupleForm</span> (<span class="dt">Bar</span> a) <span class="fu">=</span> (a, a)</a>
<a class="sourceLine" id="cb6-9" data-line-number="9"></a>
<a class="sourceLine" id="cb6-10" data-line-number="10">    <span class="kw">class</span> <span class="dt">Tuple</span> a <span class="kw">where</span></a>
<a class="sourceLine" id="cb6-11" data-line-number="11"><span class="ot">       toT ::</span> a <span class="ot">-&gt;</span> <span class="dt">TupleForm</span> a</a>
<a class="sourceLine" id="cb6-12" data-line-number="12"><span class="ot">       fromT ::</span> <span class="dt">TupleForm</span> a <span class="ot">-&gt;</span> a</a>
<a class="sourceLine" id="cb6-13" data-line-number="13"></a>
<a class="sourceLine" id="cb6-14" data-line-number="14">    <span class="kw">instance</span> <span class="dt">Tuple</span> (<span class="dt">Foo</span> a) <span class="kw">where</span></a>
<a class="sourceLine" id="cb6-15" data-line-number="15">     <span class="fu">...</span></a>
<a class="sourceLine" id="cb6-16" data-line-number="16">    <span class="kw">instance</span> <span class="dt">Tuple</span> (<span class="dt">Bar</span> a) <span class="kw">where</span></a>
<a class="sourceLine" id="cb6-17" data-line-number="17">     <span class="fu">...</span></a></code></pre></div>
<p>Now we can write generic functions by only writing them for the <code>TupleForm</code> of <code>Foo</code> and <code>Bar</code>. For example,</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb7-1" data-line-number="1"><span class="ot">    gfst ::</span> (<span class="dt">TupleForm</span> a <span class="fu">~</span> (b, c), <span class="dt">Tuple</span> a) <span class="ot">=&gt;</span> a <span class="ot">-&gt;</span> b</a>
<a class="sourceLine" id="cb7-2" data-line-number="2">    gfst <span class="fu">=</span> fst <span class="fu">.</span> toT</a></code></pre></div>
<p>Now that we understand fields, let’s move on to constructors!</p>
<p>A list constructors is the dual to a list of fields, representing OR rather than AND. We can make a bit of a leap from this to thinking that our representations of the two should be dual. So what would be the dual of <code>(a, b)</code>? Why that would be <code>Either a b</code>!</p>
<p>This means for a type</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb8-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">SomeType</span> <span class="fu">=</span> <span class="dt">Bar</span> <span class="dt">Int</span> <span class="fu">|</span> <span class="dt">Baz</span> <span class="dt">Char</span> <span class="fu">|</span> <span class="dt">Quux</span> ()</a>
<a class="sourceLine" id="cb8-2" data-line-number="2">    <span class="kw">type</span> <span class="dt">SomeType&#39;</span> <span class="fu">=</span> <span class="dt">Either</span> <span class="dt">Int</span> (<span class="dt">Either</span> <span class="dt">Char</span> ())</a></code></pre></div>
<p>This covers almost every case, we just need to make sure we represent no argument constructors as constructors of one argument: <code>()</code>. Take a moment to think why.</p>
<h2 id="a-procedure-for-reifying">A Procedure for Reifying</h2>
<p>Let’s now outline an algorithm for turning some arbitrary type to the corresponding generic version.</p>
<p>For a type C, with constructors C1, C2, C3.. and fields C1^1, C1^2, C2^1…</p>
<ol type="1">
<li>Change each set Cx^* to the <code>TupleForm</code>, call this <code>TupleForm</code> Tx</li>
<li>Nest the <code>Tx</code>’s in <code>Either</code>’s, <code>Either T1 (Either T2 (Either T3 ...))</code></li>
</ol>
<p>And that’s it, let’s practice on some data types to check that it works.</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb9-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">Test</span> <span class="fu">=</span> <span class="dt">Foo</span> <span class="dt">Int</span> <span class="dt">Char</span> <span class="fu">|</span> <span class="dt">Bar</span> <span class="dt">Int</span> <span class="dt">Bool</span> <span class="dt">Char</span> <span class="fu">|</span> <span class="dt">Quux</span></a>
<a class="sourceLine" id="cb9-2" data-line-number="2">    <span class="kw">type</span> <span class="dt">Test&#39;</span> <span class="fu">=</span> <span class="dt">Either</span> (<span class="dt">Int</span>, <span class="dt">Char</span>) (<span class="dt">Either</span> (<span class="dt">Int</span>, (<span class="dt">Bool</span>, <span class="dt">Char</span>)) ())</a>
<a class="sourceLine" id="cb9-3" data-line-number="3"></a>
<a class="sourceLine" id="cb9-4" data-line-number="4">    <span class="kw">data</span> <span class="dt">Maybe</span> a  <span class="fu">=</span> <span class="dt">Just</span> a <span class="fu">|</span> <span class="dt">Nothing</span></a>
<a class="sourceLine" id="cb9-5" data-line-number="5">    <span class="kw">type</span> <span class="dt">Maybe&#39;</span> a <span class="fu">=</span> <span class="dt">Either</span> a ()</a></code></pre></div>
<p>So we can see that this transformation is pretty mechanical! There’s one hiccup though: what do we do with recursive types?</p>
<p>We’ll handle it the same way that GHC.Generics does, we just don’t transform the recursive arguments into the generic representation lest we end up with an infinite tree.</p>
<p>So <code>[a]</code> should look like</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb10-1" data-line-number="1">    <span class="kw">type</span> <span class="dt">List</span> a <span class="fu">=</span> <span class="dt">Either</span> (a, [a]) ()</a></code></pre></div>
<h2 id="building-a-library">Building a Library</h2>
<p>Now if we want to build this into a library, we’d like to provide a few of our own data types rather than hijacking <code>Either</code> and <code>(,)</code>.</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb11-1" data-line-number="1">    <span class="ot">{-# LANGUAGE TypeOperators #-}</span></a>
<a class="sourceLine" id="cb11-2" data-line-number="2"></a>
<a class="sourceLine" id="cb11-3" data-line-number="3">    <span class="kw">data</span> (<span class="fu">:*:</span>) a b <span class="fu">=</span> a <span class="fu">:*:</span> b       <span class="co">-- Like (,) a b</span></a>
<a class="sourceLine" id="cb11-4" data-line-number="4">    <span class="kw">data</span> (<span class="fu">:+:</span>) a b <span class="fu">=</span> <span class="dt">InL</span> a <span class="fu">|</span> <span class="dt">InR</span> b <span class="co">-- Like Either a b</span></a>
<a class="sourceLine" id="cb11-5" data-line-number="5">    <span class="kw">data</span> <span class="dt">U</span>         <span class="fu">=</span> <span class="dt">U</span>             <span class="co">-- Like (), U is for Unit</span></a></code></pre></div>
<p>Now all our transformation are the same, but the results are prettier thanks to the type level operators</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb12-1" data-line-number="1">    <span class="kw">type</span> <span class="dt">List</span>   a <span class="fu">=</span> (a <span class="fu">:*:</span> [a]) <span class="fu">:+:</span> <span class="dt">U</span></a>
<a class="sourceLine" id="cb12-2" data-line-number="2">    <span class="kw">type</span> <span class="dt">Maybe&#39;</span> a <span class="fu">=</span> a <span class="fu">:+:</span> <span class="dt">U</span></a></code></pre></div>
<p>Now to facilitate generic programming, we’ll lug one more parameter through each of these constructors and add another two types to wrap meta information and constants respectively</p>
<div class="sourceCode" id="cb13"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb13-1" data-line-number="1">    <span class="kw">data</span> (<span class="fu">:*:</span>) a b p <span class="fu">=</span> a p <span class="fu">:*:</span> b p           <span class="co">-- Like (,) a b</span></a>
<a class="sourceLine" id="cb13-2" data-line-number="2">    <span class="kw">data</span> (<span class="fu">:+:</span>) a b p <span class="fu">=</span> <span class="dt">L1</span> (a p) <span class="fu">|</span> <span class="dt">R1</span> (b p) <span class="co">-- Like Either a b</span></a>
<a class="sourceLine" id="cb13-3" data-line-number="3">    <span class="kw">data</span> <span class="dt">U</span>         p <span class="fu">=</span> <span class="dt">U</span>                     <span class="co">-- Like (), U is for Unit</span></a>
<a class="sourceLine" id="cb13-4" data-line-number="4"></a>
<a class="sourceLine" id="cb13-5" data-line-number="5">    <span class="kw">newtype</span> <span class="dt">M1</span> i c f p <span class="fu">=</span> <span class="dt">M1</span> (f p) <span class="co">-- i and c are meta info</span></a>
<a class="sourceLine" id="cb13-6" data-line-number="6">    <span class="kw">newtype</span> <span class="dt">K1</span> i c   p <span class="fu">=</span> <span class="dt">K1</span> c</a></code></pre></div>
<p>Now because we’re expecting all our arguments to <code>(:*:)</code> and <code>(:+:)</code> to be of kind <code>* -&gt; *</code> we use <code>K1</code> to wrap a normal type like <code>Int</code> so that it can take an argument.</p>
<p><code>M1</code> is a bit odd, it’s used to store information about our data entirely in phantom types. We can imagine having a bunch of types that represent different things, like whether the tree of constructors represents such and such data type or what constructor we’re dealing with. It’s not terribly relevant to the rest of this post, but useful in some odd cases.</p>
<p>Now we can repeat our transformation we’d discussed earlier just using the new constructors instead. We can imagine wrapping up this whole class like this</p>
<div class="sourceCode" id="cb14"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb14-1" data-line-number="1">    <span class="kw">class</span> <span class="dt">Generic</span> a <span class="kw">where</span></a>
<a class="sourceLine" id="cb14-2" data-line-number="2">      <span class="kw">type</span> family <span class="dt">Rep</span><span class="ot"> a ::</span> <span class="fu">*</span> <span class="ot">-&gt;</span> <span class="fu">*</span></a>
<a class="sourceLine" id="cb14-3" data-line-number="3"><span class="ot">      to   ::</span> a       <span class="ot">-&gt;</span> <span class="dt">Rep</span> a</a>
<a class="sourceLine" id="cb14-4" data-line-number="4"><span class="ot">      from ::</span> <span class="dt">Rep</span> a p <span class="ot">-&gt;</span> a</a></code></pre></div>
<p>This is very much in the spirit of our <code>Tuple</code> type class, but now our type family returns something of type <code>* -&gt; *</code> to leave room for our extra <code>p</code> parameter</p>
<h2 id="the-real-deal">The Real Deal</h2>
<p>As clever readers will have noticed, the above type class is precisely what <code>GHC.Generics</code> exports! We have successfully reached full circle and now have arrived at <code>GHC.Generics'</code> API.</p>
<p>The only difference between us and GHC.Generics is their <code>Generic</code> class can be derived almost identically to our algorithm. The only slight difference is rather than a “list” of <code>:*:</code>’s or <code>:+:</code>’s they make a tree, this makes little difference to most programs however.</p>
<p>To wrap things up, let’s finish by showcasing making a simple generic debugging dumper.</p>
<p>To begin with, we’ll define a class <code>GDump</code> and will make instances for the GHC.Generics types</p>
<div class="sourceCode" id="cb15"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb15-1" data-line-number="1">    <span class="kw">class</span> <span class="dt">GDump</span> a <span class="kw">where</span></a>
<a class="sourceLine" id="cb15-2" data-line-number="2"><span class="ot">       gdump ::</span> a <span class="ot">-&gt;</span> <span class="dt">String</span></a>
<a class="sourceLine" id="cb15-3" data-line-number="3"></a>
<a class="sourceLine" id="cb15-4" data-line-number="4">    <span class="kw">instance</span> <span class="dt">GDump</span> (<span class="dt">U1</span> p) <span class="kw">where</span></a>
<a class="sourceLine" id="cb15-5" data-line-number="5">      gdump <span class="dt">U1</span> <span class="fu">=</span> <span class="st">&quot;()&quot;</span></a>
<a class="sourceLine" id="cb15-6" data-line-number="6"></a>
<a class="sourceLine" id="cb15-7" data-line-number="7">    <span class="kw">instance</span> <span class="dt">Show</span> c <span class="ot">=&gt;</span> <span class="dt">GDump</span> (<span class="dt">K1</span> i c p) <span class="kw">where</span></a>
<a class="sourceLine" id="cb15-8" data-line-number="8">      gdump (<span class="dt">K1</span> c) <span class="fu">=</span> show c</a>
<a class="sourceLine" id="cb15-9" data-line-number="9"></a>
<a class="sourceLine" id="cb15-10" data-line-number="10">    <span class="kw">instance</span> (<span class="dt">GDump</span> (f p), <span class="dt">GDump</span> (g p)) <span class="ot">=&gt;</span> <span class="dt">GDump</span> ((<span class="fu">:*:</span>) f g p) <span class="kw">where</span></a>
<a class="sourceLine" id="cb15-11" data-line-number="11">      gdump (a <span class="fu">:*:</span> b) <span class="fu">=</span> <span class="st">&quot;(&quot;</span> <span class="fu">++</span> gdump a <span class="fu">++</span> <span class="st">&quot; :*: &quot;</span> <span class="fu">++</span> gdump b <span class="fu">++</span> <span class="st">&quot;)&quot;</span></a>
<a class="sourceLine" id="cb15-12" data-line-number="12"></a>
<a class="sourceLine" id="cb15-13" data-line-number="13">    <span class="kw">instance</span> (<span class="dt">GDump</span> (f p), <span class="dt">GDump</span> (g p)) <span class="ot">=&gt;</span> <span class="dt">GDump</span> ((<span class="fu">:+:</span>) f g p) <span class="kw">where</span></a>
<a class="sourceLine" id="cb15-14" data-line-number="14">      gdump (<span class="dt">L1</span> a) <span class="fu">=</span> <span class="st">&quot;(Left  &quot;</span> <span class="fu">++</span> gdump a <span class="fu">++</span> <span class="st">&quot;)&quot;</span></a>
<a class="sourceLine" id="cb15-15" data-line-number="15">      gdump (<span class="dt">R1</span> a) <span class="fu">=</span> <span class="st">&quot;(Right &quot;</span> <span class="fu">++</span> gdump a <span class="fu">++</span> <span class="st">&quot;)&quot;</span></a>
<a class="sourceLine" id="cb15-16" data-line-number="16"></a>
<a class="sourceLine" id="cb15-17" data-line-number="17">    <span class="kw">instance</span> (<span class="dt">GDump</span> (f p)) <span class="ot">=&gt;</span> <span class="dt">GDump</span> (<span class="dt">M1</span> a b f p) <span class="kw">where</span></a>
<a class="sourceLine" id="cb15-18" data-line-number="18">      gdump (<span class="dt">M1</span> f) <span class="fu">=</span> gdump f</a></code></pre></div>
<p>And now we can create a class for “normal” values and use <code>-XDefaultSignatures</code> to give the default implementation a <code>Generic</code> constraint</p>
<div class="sourceCode" id="cb16"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb16-1" data-line-number="1">    <span class="kw">class</span> <span class="dt">Dump</span> a <span class="kw">where</span></a>
<a class="sourceLine" id="cb16-2" data-line-number="2"><span class="ot">      dump ::</span> a <span class="ot">-&gt;</span> <span class="dt">String</span></a>
<a class="sourceLine" id="cb16-3" data-line-number="3"></a>
<a class="sourceLine" id="cb16-4" data-line-number="4">      default<span class="ot"> dump ::</span> (<span class="dt">Generic</span> a, <span class="dt">GDump</span> (<span class="dt">Rep</span> a ())) <span class="ot">=&gt;</span> a <span class="ot">-&gt;</span> <span class="dt">String</span></a>
<a class="sourceLine" id="cb16-5" data-line-number="5">      dump a <span class="fu">=</span> gdump (from&#39; a)</a>
<a class="sourceLine" id="cb16-6" data-line-number="6">        <span class="kw">where</span><span class="ot"> from&#39; ::</span> <span class="dt">Generic</span> a <span class="ot">=&gt;</span> a <span class="ot">-&gt;</span> <span class="dt">Rep</span> a ()</a>
<a class="sourceLine" id="cb16-7" data-line-number="7">              from&#39; <span class="fu">=</span> from  <span class="co">-- A hack to stop the type checker from whining about p</span></a></code></pre></div>
<p>And now we can just use this default implementation.</p>
<div class="sourceCode" id="cb17"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb17-1" data-line-number="1">    <span class="kw">instance</span> <span class="dt">Show</span> a <span class="ot">=&gt;</span> <span class="dt">Dump</span> (<span class="dt">Maybe</span> a)</a></code></pre></div>
<p>Using this we can print suitably boring representations of generic types, for free!</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Fri, 25 Apr 2014 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2014-04-25-you-could-have.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Continuations and Exceptions</title>
    <link>http://jozefg.bitbucket.org/posts/2014-04-14-either-and-conts.html</link>
    <description><![CDATA[<div class="info">
    Posted on April 14, 2014
    
</div>
<div class="info">
    
    Tags: <a href="/tags/haskell.html">haskell</a>
    
</div>

<p>Continuations are useful things. They provide a nice way to manually handle control flow. This fact makes them very useful for compilers, an often used alternative to SSA is an intermediate language in which every function call is a tail-call and every expression has been converted to continuation passing style.</p>
<p>Often however, this isn’t enough. In a language which exceptions, we don’t just have a single continuation. Since every expression can either do one of two things.</p>
<ol type="1">
<li>Continue the rest of the program normally</li>
<li>Throw an exception and run an alternative program, the exception handler</li>
</ol>
<p>To represent this, we can imagine having two continuations. Instead of</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb1-1" data-line-number="1">    <span class="kw">newtype</span> <span class="dt">Cont</span> r a <span class="fu">=</span> <span class="dt">Cont</span> {<span class="ot">runCont ::</span> (a <span class="ot">-&gt;</span> r) <span class="ot">-&gt;</span> r}</a></code></pre></div>
<p>We have</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb2-1" data-line-number="1">    <span class="ot">{-# LANGUAGE DeriveFunctor #-}</span></a>
<a class="sourceLine" id="cb2-2" data-line-number="2">    <span class="kw">import</span> <span class="dt">Control.Monad</span></a>
<a class="sourceLine" id="cb2-3" data-line-number="3"></a>
<a class="sourceLine" id="cb2-4" data-line-number="4">    <span class="kw">newtype</span> <span class="dt">Throws</span> r e a <span class="fu">=</span> <span class="dt">Throws</span> {<span class="ot">runThrows ::</span> (e <span class="ot">-&gt;</span> r) <span class="ot">-&gt;</span> (a <span class="ot">-&gt;</span> r) <span class="ot">-&gt;</span> r}</a>
<a class="sourceLine" id="cb2-5" data-line-number="5">    <span class="kw">deriving</span> (<span class="dt">Functor</span>)</a></code></pre></div>
<p>Now we have two continuations, where <code>e -&gt; r</code> represents the composition of exception handlers.</p>
<p>We can write a trivial monad instance similar to <code>Cont</code></p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb3-1" data-line-number="1">    <span class="kw">instance</span> <span class="dt">Monad</span> (<span class="dt">Throws</span> r e) <span class="kw">where</span></a>
<a class="sourceLine" id="cb3-2" data-line-number="2">      return a <span class="fu">=</span> <span class="dt">Throws</span> <span class="fu">$</span> \ex cont <span class="ot">-&gt;</span> cont a</a>
<a class="sourceLine" id="cb3-3" data-line-number="3">      (<span class="dt">Throws</span> c) <span class="fu">&gt;&gt;=</span> f <span class="fu">=</span> <span class="dt">Throws</span> <span class="fu">$</span> \ ex cont <span class="ot">-&gt;</span></a>
<a class="sourceLine" id="cb3-4" data-line-number="4">        c ex <span class="fu">$</span> \a <span class="ot">-&gt;</span> runThrows (f a) e cont</a></code></pre></div>
<p>So <code>&gt;&gt;=</code> maintains the exception handler between computations and otherwise acts exactly like <code>Cont</code>.</p>
<p>To actually take advantage of our exception handlers, we need two things, a <code>throw</code> and <code>catch</code> like pair of function. Let’s start with <code>throw</code> since it’s easiest.</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" data-line-number="1"><span class="ot">    throw ::</span> e <span class="ot">-&gt;</span> <span class="dt">Throws</span> r e a</a>
<a class="sourceLine" id="cb4-2" data-line-number="2">    throw e <span class="fu">=</span> <span class="dt">Throws</span> <span class="fu">$</span> \ex cont <span class="ot">-&gt;</span> ex e</a></code></pre></div>
<p>This is pretty straightforward, when we’re given an exception an to throw, we simply feed it to our exception handler continuation. Since care what value <code>cont</code> needs, we can universally quantify over <code>a</code>.</p>
<p>Next up is <code>handle</code>, we’ll represent an exception handler as a function from <code>e -&gt; Maybe a</code>. If we return <code>Nothing</code>, we can’t handle the exception at this level and we’ll just pass it to the existing exception handler.</p>
<p>So our <code>handle</code> is</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb5-1" data-line-number="1"><span class="ot">    handle ::</span> <span class="dt">Throws</span> r e a <span class="ot">-&gt;</span> (e <span class="ot">-&gt;</span> <span class="dt">Maybe</span> a) <span class="ot">-&gt;</span> <span class="dt">Throws</span> r e a</a>
<a class="sourceLine" id="cb5-2" data-line-number="2">    handle (<span class="dt">Throws</span> rest) handler <span class="fu">=</span> <span class="dt">Throws</span> <span class="fu">$</span> \ex cont <span class="ot">-&gt;</span></a>
<a class="sourceLine" id="cb5-3" data-line-number="3">      rest (\e <span class="ot">-&gt;</span> maybe (ex e) cont (handler e)) cont</a></code></pre></div>
<p>Notice the clever bit here, each handler actually contains both the success and failure continuations! If we can’t handle the exception we fail otherwise we can resume exactly where we were before.</p>
<p>No post would be complete without a demonstration!</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb6-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">Ex</span> <span class="fu">=</span> <span class="dt">Boom</span> <span class="fu">|</span> <span class="dt">Kaboom</span> <span class="fu">|</span> <span class="dt">Splat</span> <span class="dt">String</span></a>
<a class="sourceLine" id="cb6-2" data-line-number="2">            <span class="kw">deriving</span> <span class="dt">Show</span></a>
<a class="sourceLine" id="cb6-3" data-line-number="3"></a>
<a class="sourceLine" id="cb6-4" data-line-number="4">    throwEx1 <span class="fu">=</span> throw <span class="dt">Boom</span></a>
<a class="sourceLine" id="cb6-5" data-line-number="5">    throwEx2 <span class="fu">=</span> throw <span class="dt">Kaboom</span></a>
<a class="sourceLine" id="cb6-6" data-line-number="6">    throwEx3 <span class="fu">=</span> throw (<span class="dt">Splat</span> <span class="st">&quot;splat&quot;</span>)</a>
<a class="sourceLine" id="cb6-7" data-line-number="7">    test <span class="fu">=</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb6-8" data-line-number="8">      result <span class="ot">&lt;-</span> handle throwEx1 <span class="fu">$</span> \e <span class="ot">-&gt;</span> <span class="kw">case</span> e <span class="kw">of</span></a>
<a class="sourceLine" id="cb6-9" data-line-number="9">        <span class="dt">Boom</span> <span class="ot">-&gt;</span> <span class="dt">Just</span> <span class="st">&quot;foo&quot;</span></a>
<a class="sourceLine" id="cb6-10" data-line-number="10">        _    <span class="ot">-&gt;</span> <span class="dt">Nothing</span></a>
<a class="sourceLine" id="cb6-11" data-line-number="11">      result2 <span class="ot">&lt;-</span> handle throwEx2 <span class="fu">$</span> \e <span class="ot">-&gt;</span> <span class="kw">case</span> e <span class="kw">of</span></a>
<a class="sourceLine" id="cb6-12" data-line-number="12">        <span class="dt">Boom</span>   <span class="ot">-&gt;</span> <span class="dt">Just</span> <span class="st">&quot;what&quot;</span></a>
<a class="sourceLine" id="cb6-13" data-line-number="13">        <span class="dt">Kaboom</span> <span class="ot">-&gt;</span> <span class="dt">Just</span> <span class="st">&quot;huh?&quot;</span></a>
<a class="sourceLine" id="cb6-14" data-line-number="14">        _      <span class="ot">-&gt;</span> <span class="dt">Nothing</span></a>
<a class="sourceLine" id="cb6-15" data-line-number="15">      result3 <span class="ot">&lt;-</span> handle throwEx3 <span class="fu">$</span> \e <span class="ot">-&gt;</span> <span class="kw">case</span> e <span class="kw">of</span></a>
<a class="sourceLine" id="cb6-16" data-line-number="16">        <span class="dt">Splat</span> s <span class="ot">-&gt;</span> <span class="dt">Just</span> s</a>
<a class="sourceLine" id="cb6-17" data-line-number="17">        _       <span class="ot">-&gt;</span> <span class="dt">Nothing</span></a>
<a class="sourceLine" id="cb6-18" data-line-number="18">      return (unwords [result, result2, result3])</a></code></pre></div>
<p>We can run this with</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb7-1" data-line-number="1">    runThrows (error <span class="fu">.</span> (<span class="st">&quot;Toplevel fail &quot;</span><span class="fu">++</span>)) test</a></code></pre></div>
<p>which returns</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb8-1" data-line-number="1">    <span class="st">&quot;foo huh? splat&quot;</span></a></code></pre></div>
<p>So our exceptions do in fact, work :) ## A Note on <code>Either</code> Now we already have a perfectly good system of monadic exception like thing in the form of <code>Either</code>.</p>
<p>It might be interesting to note that what we’ve written is in fact isomorphic to <code>Either</code>. <code>(e -&gt; r) -&gt; (a -&gt; r) -&gt; r</code> is just the church representation of <code>Either e a</code>.</p>
<p>We can even go all the way and change <code>Throws</code> to</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb9-1" data-line-number="1">    <span class="kw">newtype</span> <span class="dt">Throws</span> e a <span class="fu">=</span> <span class="dt">Throws</span> {<span class="ot">runThrows ::</span> forall r<span class="fu">.</span> (e <span class="ot">-&gt;</span> r) <span class="ot">-&gt;</span> (a <span class="ot">-&gt;</span> r) <span class="ot">-&gt;</span> r}</a></code></pre></div>
<p>So there you are, an interesting realization that one of the classic representations of a language like SML is in fact a really complicated version of <code>Either</code> :)</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Mon, 14 Apr 2014 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2014-04-14-either-and-conts.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Bargain Priced Coroutines</title>
    <link>http://jozefg.bitbucket.org/posts/2014-04-08-bargain-coroutines.html</link>
    <description><![CDATA[<div class="info">
    Posted on April  8, 2014
    
</div>
<div class="info">
    
    Tags: <a href="/tags/haskell.html">haskell</a>
    
</div>

<p>The other day I was reading the 19th issue of the Monad.Reader and there was a fascinating post on coroutines.</p>
<p>While reading some of the code I noticed that it, like most things in Haskell, can be reduced to 5 lines with a library that Edward Kmett has written.</p>
<p>Consider the type of a trampoline as described in this article</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb1-1" data-line-number="1">    <span class="kw">newtype</span> <span class="dt">Trampoline</span> m a <span class="fu">=</span> <span class="dt">Tramp</span> {<span class="ot">runTramp ::</span> m (<span class="dt">Either</span> (<span class="dt">Tramp</span> m a) a)}</a></code></pre></div>
<p>So a trampoline is a monadic computation of some sort returning either a result, <code>a</code>, or another computation to run to get the rest.</p>
<p>Now this looks strikingly familiar. A computation returning <code>Trampoline m a</code> is really a computation returning a tree of <code>Tramp m a</code>’s terminating in a pure value.</p>
<p>This sounds like a free monad!</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb2-1" data-line-number="1">    <span class="kw">import</span> <span class="dt">Control.Monad.Trans.Free</span></a>
<a class="sourceLine" id="cb2-2" data-line-number="2">    <span class="kw">import</span> <span class="dt">Control.Monad.Identity</span></a>
<a class="sourceLine" id="cb2-3" data-line-number="3"></a>
<a class="sourceLine" id="cb2-4" data-line-number="4">    <span class="kw">type</span> <span class="dt">Trampoline</span> <span class="fu">=</span> <span class="dt">FreeT</span> <span class="dt">Identity</span></a></code></pre></div>
<p>Recall that <code>FreeT</code> is defined as</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb3-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">FreeF</span> f a b <span class="fu">=</span> <span class="dt">Pure</span> a <span class="fu">|</span> <span class="dt">Free</span> (f b)</a>
<a class="sourceLine" id="cb3-2" data-line-number="2">    <span class="kw">data</span> <span class="dt">FreeT</span> f m a <span class="fu">=</span> <span class="dt">FreeT</span> (m (<span class="dt">FreeF</span> f a (<span class="dt">FreeT</span> f m a)))</a></code></pre></div>
<p>This is isomorphic to what we where looking at before. As an added bonus, we’ve saved the tedium of defining our own monad and applicative instance for <code>Trampoline</code>.</p>
<p>We can now implement <code>bounce</code> and <code>pause</code> to define our trampolines. <code>bounce</code> must take a computation and unwrap it by one level, leaving either a value or another computation.</p>
<p>This is just a matter of rejiggering the <code>FreeF</code> into an <code>Either</code></p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" data-line-number="1"><span class="ot">    bounce ::</span> <span class="dt">Functor</span> m <span class="ot">=&gt;</span> <span class="dt">Trampoline</span> m a <span class="ot">-&gt;</span> m (<span class="dt">Either</span> (<span class="dt">Trampoline</span> m a) a)</a>
<a class="sourceLine" id="cb4-2" data-line-number="2">    bounce <span class="fu">=</span> fmap toEither <span class="fu">.</span> runFreeT</a>
<a class="sourceLine" id="cb4-3" data-line-number="3">      <span class="kw">where</span> toEither (<span class="dt">Pure</span> a) <span class="fu">=</span> <span class="dt">Right</span> a</a>
<a class="sourceLine" id="cb4-4" data-line-number="4">            toEither (<span class="dt">Free</span> m) <span class="fu">=</span> <span class="dt">Left</span> <span class="fu">$</span> runIdentity m</a></code></pre></div>
<p><code>pause</code> requires some thought, the trick is to realize that if we wrap a computation in one layer of <code>Free</code> when unwrapped by <code>bounce</code> we’ll get the rest of the computation.</p>
<p>Therefore,</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb5-1" data-line-number="1"><span class="ot">    pause ::</span> <span class="dt">Monad</span> m <span class="ot">=&gt;</span> <span class="dt">Trampoline</span> m ()</a>
<a class="sourceLine" id="cb5-2" data-line-number="2">    pause <span class="fu">=</span> <span class="dt">FreeT</span> <span class="fu">$</span> return (<span class="dt">Free</span> <span class="fu">.</span> <span class="dt">Identity</span> <span class="fu">$</span> return ())</a></code></pre></div>
<p>So that’s 6 lines of code for trampolines. Let’s move on to generators.</p>
<p>A generator doesn’t yield just another computation, it yields a pair of a computation and a freshly generated value. We can account for this by changing that <code>Identity</code> functor.</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb6-1" data-line-number="1">    <span class="kw">type</span> <span class="dt">Generator</span> c <span class="fu">=</span> <span class="dt">FreeT</span> ((,) c)</a></code></pre></div>
<p>Again we get free functor, applicative and monad instances. We two functions, <code>yield</code> and <code>runGen</code>. Yield is going to take one value and stick it into the first element of the pair.</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb7-1" data-line-number="1"><span class="ot">    yield ::</span> <span class="dt">Monad</span> m <span class="ot">=&gt;</span> g <span class="ot">-&gt;</span> <span class="dt">Generator</span> g m ()</a>
<a class="sourceLine" id="cb7-2" data-line-number="2">    yield g <span class="fu">=</span> <span class="dt">FreeT</span> <span class="fu">.</span> return <span class="fu">$</span> <span class="dt">Free</span> (g, return ())</a></code></pre></div>
<p>This just sticks a good old boring <code>m ()</code> in the second element of the pair.</p>
<p>Now <code>runGen</code> should take a generator and produce a <code>m (Maybe c, Generator c m a)</code>. This can be done again by pattern matching on the underlying <code>FreeF</code>.</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb8-1" data-line-number="1"><span class="ot">    runGen ::</span> (<span class="dt">Monad</span> m, <span class="dt">Functor</span> m) <span class="ot">=&gt;</span> <span class="dt">Generator</span> g m a <span class="ot">-&gt;</span> m (<span class="dt">Maybe</span> g, <span class="dt">Generator</span> g m a)</a>
<a class="sourceLine" id="cb8-2" data-line-number="2">    runGen <span class="fu">=</span> fmap toTuple <span class="fu">.</span> runFreeT</a>
<a class="sourceLine" id="cb8-3" data-line-number="3">      <span class="kw">where</span> toTuple (<span class="dt">Pure</span> a)         <span class="fu">=</span> (<span class="dt">Nothing</span>, return a)</a>
<a class="sourceLine" id="cb8-4" data-line-number="4">            toTuple (<span class="dt">Free</span> (g, rest)) <span class="fu">=</span> (<span class="dt">Just</span> g, rest)</a></code></pre></div>
<p>Now, last but not least, let’s build consumers. These wait for a value rather than generating one, so <code>-&gt;</code> looks like the right functor.</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb9-1" data-line-number="1">    <span class="kw">type</span> <span class="dt">Consumer</span> c <span class="fu">=</span> <span class="dt">FreeT</span> ((<span class="ot">-&gt;</span>) c)</a></code></pre></div>
<p>Now we want <code>await</code> and <code>runCon</code>. <code>await</code> to wait for a value and <code>runCon</code> to supply one. These are both fairly mechanical.</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb10-1" data-line-number="1"><span class="ot">    runConsumer ::</span> <span class="dt">Monad</span> m <span class="ot">=&gt;</span> c <span class="ot">-&gt;</span> <span class="dt">Consumer</span> c m a <span class="ot">-&gt;</span> m a</a>
<a class="sourceLine" id="cb10-2" data-line-number="2">    runConsumer c <span class="fu">=</span> (<span class="fu">&gt;&gt;=</span> go) <span class="fu">.</span> runFreeT</a>
<a class="sourceLine" id="cb10-3" data-line-number="3">      <span class="kw">where</span> go (<span class="dt">Pure</span> a) <span class="fu">=</span> return a</a>
<a class="sourceLine" id="cb10-4" data-line-number="4">            go (<span class="dt">Free</span> f) <span class="fu">=</span> runConsumer c <span class="fu">$</span> f c</a>
<a class="sourceLine" id="cb10-5" data-line-number="5"></a>
<a class="sourceLine" id="cb10-6" data-line-number="6"><span class="ot">    runCon ::</span> (<span class="dt">Monad</span> m, <span class="dt">Functor</span> m)</a>
<a class="sourceLine" id="cb10-7" data-line-number="7">        <span class="ot">=&gt;</span> <span class="dt">Maybe</span> c</a>
<a class="sourceLine" id="cb10-8" data-line-number="8">        <span class="ot">-&gt;</span> <span class="dt">Consumer</span> c m a</a>
<a class="sourceLine" id="cb10-9" data-line-number="9">        <span class="ot">-&gt;</span> m (<span class="dt">Either</span> a (<span class="dt">Consumer</span> c m a))</a>
<a class="sourceLine" id="cb10-10" data-line-number="10">    runCon food c <span class="fu">=</span> runFreeT c <span class="fu">&gt;&gt;=</span> go</a>
<a class="sourceLine" id="cb10-11" data-line-number="11">      <span class="kw">where</span> go (<span class="dt">Pure</span> a) <span class="fu">=</span> return <span class="fu">.</span> <span class="dt">Left</span> <span class="fu">$</span> a</a>
<a class="sourceLine" id="cb10-12" data-line-number="12">            go (<span class="dt">Free</span> f) <span class="fu">=</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb10-13" data-line-number="13">              result <span class="ot">&lt;-</span> runFreeT <span class="fu">$</span> f food</a>
<a class="sourceLine" id="cb10-14" data-line-number="14">              return <span class="fu">$</span> <span class="kw">case</span> result <span class="kw">of</span></a>
<a class="sourceLine" id="cb10-15" data-line-number="15">                <span class="dt">Pure</span> a <span class="ot">-&gt;</span> <span class="dt">Left</span>                   <span class="fu">$</span> a</a>
<a class="sourceLine" id="cb10-16" data-line-number="16">                free   <span class="ot">-&gt;</span> <span class="dt">Right</span> <span class="fu">.</span> <span class="dt">FreeT</span> <span class="fu">.</span> return <span class="fu">$</span> free</a></code></pre></div>
<p><code>runCon</code> is a bit more complex than I’d like. This is to essentially ensure that if we had some code like</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb11-1" data-line-number="1">    <span class="dt">Just</span> a <span class="ot">&lt;-</span> await</a>
<a class="sourceLine" id="cb11-2" data-line-number="2">    lift <span class="fu">$</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb11-3" data-line-number="3">      foo</a>
<a class="sourceLine" id="cb11-4" data-line-number="4">      bar</a>
<a class="sourceLine" id="cb11-5" data-line-number="5">      baz</a>
<a class="sourceLine" id="cb11-6" data-line-number="6">    <span class="dt">Just</span> b <span class="ot">&lt;-</span> await</a></code></pre></div>
<p>We want <code>foo</code>, <code>bar</code>, and <code>baz</code> to run with just one <code>await</code>. You’d expect that we’d run as much as possible with each call to <code>runCon</code>. Thus we unwrap not one, but two layers of our <code>FreeT</code> and run them, then rewrap the lower layer. The trick is that we make sure never to duplicate side effects by using good old <code>return</code>.</p>
<p>We can sleep easy that this is sound since <code>return a &gt;&gt;= f</code> is <code>f a</code> by the monad laws. Thus, our call to <code>return</code> can’t do anything detectable or too interesting.</p>
<p>While this is arguably more intuitive, I don’t particularly like it so we can instead write</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb12-1" data-line-number="1"><span class="ot">    runCon ::</span> (<span class="dt">Monad</span> m, <span class="dt">Functor</span> m)</a>
<a class="sourceLine" id="cb12-2" data-line-number="2">        <span class="ot">=&gt;</span> <span class="dt">Maybe</span> c</a>
<a class="sourceLine" id="cb12-3" data-line-number="3">        <span class="ot">-&gt;</span> <span class="dt">Consumer</span> c m a</a>
<a class="sourceLine" id="cb12-4" data-line-number="4">        <span class="ot">-&gt;</span> m (<span class="dt">Either</span> a (<span class="dt">Consumer</span> c m a))</a>
<a class="sourceLine" id="cb12-5" data-line-number="5">    runCon food <span class="fu">=</span> fmap go <span class="fu">.</span> runFreeT</a>
<a class="sourceLine" id="cb12-6" data-line-number="6">      <span class="kw">where</span> go (<span class="dt">Pure</span> a) <span class="fu">=</span> <span class="dt">Left</span> a</a>
<a class="sourceLine" id="cb12-7" data-line-number="7">            go (<span class="dt">Free</span> f) <span class="fu">=</span> <span class="dt">Right</span> (f food)</a></code></pre></div>
<p>Much simpler, but now our above example wouldn’t run <code>foo</code> and friends until the <em>second</em> call of <code>runCon</code>.</p>
<p>Now we can join generators to consumers in a pretty naive way,</p>
<div class="sourceCode" id="cb13"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb13-1" data-line-number="1"><span class="ot">    (&gt;~&gt;) ::</span> (<span class="dt">Functor</span> m, <span class="dt">Monad</span> m) <span class="ot">=&gt;</span> <span class="dt">Generator</span> c m () <span class="ot">-&gt;</span> <span class="dt">Consumer</span> c m a <span class="ot">-&gt;</span> m a</a>
<a class="sourceLine" id="cb13-2" data-line-number="2">    gen <span class="fu">&gt;~&gt;</span> con <span class="fu">=</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb13-3" data-line-number="3">      (cMay, rest) <span class="ot">&lt;-</span> runGen gen</a>
<a class="sourceLine" id="cb13-4" data-line-number="4">      <span class="kw">case</span> cMay <span class="kw">of</span></a>
<a class="sourceLine" id="cb13-5" data-line-number="5">        <span class="dt">Nothing</span> <span class="ot">-&gt;</span> starve con</a>
<a class="sourceLine" id="cb13-6" data-line-number="6">        <span class="dt">Just</span> c  <span class="ot">-&gt;</span> runCon c con <span class="fu">&gt;&gt;=</span> use rest</a>
<a class="sourceLine" id="cb13-7" data-line-number="7">      <span class="kw">where</span> use _    (<span class="dt">Left</span> a)  <span class="fu">=</span> return a</a>
<a class="sourceLine" id="cb13-8" data-line-number="8">            use rest (<span class="dt">Right</span> c) <span class="fu">=</span> rest <span class="fu">&gt;~&gt;</span> c</a></code></pre></div>
<p>And now we can use it!</p>
<div class="sourceCode" id="cb14"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb14-1" data-line-number="1"><span class="ot">    addGen ::</span> <span class="dt">Generator</span> <span class="dt">Int</span> <span class="dt">IO</span> ()</a>
<a class="sourceLine" id="cb14-2" data-line-number="2">    addGen <span class="fu">=</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb14-3" data-line-number="3">      lift <span class="fu">$</span> putStrLn <span class="st">&quot;Yielding 1&quot;</span></a>
<a class="sourceLine" id="cb14-4" data-line-number="4">      yield <span class="dv">1</span></a>
<a class="sourceLine" id="cb14-5" data-line-number="5">      lift <span class="fu">$</span> putStrLn <span class="st">&quot;Yielding 2&quot;</span></a>
<a class="sourceLine" id="cb14-6" data-line-number="6">      yield <span class="dv">2</span></a>
<a class="sourceLine" id="cb14-7" data-line-number="7"></a>
<a class="sourceLine" id="cb14-8" data-line-number="8"><span class="ot">    addCon ::</span> <span class="dt">Consumer</span> <span class="dt">Int</span> <span class="dt">IO</span> ()</a>
<a class="sourceLine" id="cb14-9" data-line-number="9">    addCon <span class="fu">=</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb14-10" data-line-number="10">      lift <span class="fu">$</span> putStrLn <span class="st">&quot;Waiting for a...&quot;</span></a>
<a class="sourceLine" id="cb14-11" data-line-number="11">      <span class="dt">Just</span> a <span class="ot">&lt;-</span> await</a>
<a class="sourceLine" id="cb14-12" data-line-number="12">      lift <span class="fu">$</span> putStrLn <span class="st">&quot;Waiting for b...&quot;</span></a>
<a class="sourceLine" id="cb14-13" data-line-number="13">      <span class="dt">Just</span> b <span class="ot">&lt;-</span> await</a>
<a class="sourceLine" id="cb14-14" data-line-number="14">      lift <span class="fu">.</span> print <span class="fu">$</span> a <span class="fu">+</span> b</a>
<a class="sourceLine" id="cb14-15" data-line-number="15"></a>
<a class="sourceLine" id="cb14-16" data-line-number="16">    main <span class="fu">=</span> addGen <span class="fu">&gt;~&gt;</span> addCon</a></code></pre></div>
<p>When run this prints</p>
<pre><code>    Yielding 1
    Waiting for a...
    Yielding 2
    Waiting for b...
    3</code></pre>
<p>Now, this all falls out of playing with what functor we give to <code>FreeT</code>. So far, we’ve gotten trampolines out of <code>Identity</code>, generators out of <code>(,) a</code>, and consumers out of <code>(-&gt;) a</code>.</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Tue, 08 Apr 2014 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2014-04-08-bargain-coroutines.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Church Representations: Part 3</title>
    <link>http://jozefg.bitbucket.org/posts/2014-03-10-revenge-of-churchrep.html</link>
    <description><![CDATA[<div class="info">
    Posted on March 10, 2014
    
</div>
<div class="info">
    
    Tags: <a href="/tags/haskell.html">haskell</a>, <a href="/tags/types.html">types</a>
    
</div>

<p>To conclude my recent spat of posts on <a href="/posts/2014-03-06-church.html">church</a> <a href="/posts/2014-03-07-church-the-sequel.html">representations</a> I’d like to write one more.</p>
<p>My last two posts have focused on taking a random value and turning it into a nifty function that we can use in place of the value. In this post I’ll show to invert the process and given a function, return a value.</p>
<p>This requires a lot more intricate type level programming, so let’s start by turning on our slew of language extensions and importing a few libraries.</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb1-1" data-line-number="1">    <span class="ot">{-# LANGUAGE TypeFamilies,          TypeOperators,     UndecidableInstances #-}</span></a>
<a class="sourceLine" id="cb1-2" data-line-number="2">    <span class="ot">{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, FlexibleContexts     #-}</span></a>
<a class="sourceLine" id="cb1-3" data-line-number="3">    <span class="ot">{-# LANGUAGE ScopedTypeVariables,   PolyKinds,         DataKinds            #-}</span></a>
<a class="sourceLine" id="cb1-4" data-line-number="4">    <span class="kw">import</span> <span class="dt">Data.Proxy</span></a>
<a class="sourceLine" id="cb1-5" data-line-number="5">    <span class="kw">import</span> <span class="dt">GHC.Generics</span></a></code></pre></div>
<p>We’ll start by defining a few useful type families across type level lists (<code>-XDataKinds</code> has promoted <code>[]</code> for us).</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb2-1" data-line-number="1">    <span class="kw">type</span> family <span class="dt">Head</span> (<span class="ot">xs ::</span> [k])<span class="ot"> ::</span> k</a>
<a class="sourceLine" id="cb2-2" data-line-number="2">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">Head</span> (x &#39;<span class="fu">:</span> xs) <span class="fu">=</span> x</a>
<a class="sourceLine" id="cb2-3" data-line-number="3">    <span class="kw">type</span> family <span class="dt">Tail</span> (<span class="ot">xs ::</span> [k])<span class="ot"> ::</span> [k]</a>
<a class="sourceLine" id="cb2-4" data-line-number="4">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">Tail</span> (x &#39;<span class="fu">:</span> xs) <span class="fu">=</span> xs</a>
<a class="sourceLine" id="cb2-5" data-line-number="5"></a>
<a class="sourceLine" id="cb2-6" data-line-number="6"><span class="ot">    pHead ::</span> <span class="dt">Proxy</span> xs <span class="ot">-&gt;</span> <span class="dt">Proxy</span> (<span class="dt">Head</span> xs)</a>
<a class="sourceLine" id="cb2-7" data-line-number="7">    pHead <span class="fu">=</span> reproxy</a>
<a class="sourceLine" id="cb2-8" data-line-number="8"><span class="ot">    pTail ::</span> <span class="dt">Proxy</span> xs <span class="ot">-&gt;</span> <span class="dt">Proxy</span> (<span class="dt">Tail</span> xs)</a>
<a class="sourceLine" id="cb2-9" data-line-number="9">    pTail <span class="fu">=</span> reproxy</a>
<a class="sourceLine" id="cb2-10" data-line-number="10"></a>
<a class="sourceLine" id="cb2-11" data-line-number="11">    <span class="kw">type</span> family <span class="dt">Append</span> (<span class="ot">xs ::</span> [k]) (<span class="ot">ys ::</span> [k])<span class="ot"> ::</span> [k]</a>
<a class="sourceLine" id="cb2-12" data-line-number="12">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">Append</span> &#39;[] ys <span class="fu">=</span> ys</a>
<a class="sourceLine" id="cb2-13" data-line-number="13">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">Append</span> (x &#39;<span class="fu">:</span> xs) ys <span class="fu">=</span> x &#39;<span class="fu">:</span> <span class="dt">Append</span> xs ys</a>
<a class="sourceLine" id="cb2-14" data-line-number="14"></a>
<a class="sourceLine" id="cb2-15" data-line-number="15">    <span class="kw">type</span> family <span class="dt">Reverse</span> (<span class="ot">xs ::</span> [k])<span class="ot"> ::</span> [k]</a>
<a class="sourceLine" id="cb2-16" data-line-number="16">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">Reverse</span> &#39;[] <span class="fu">=</span> &#39;[]</a>
<a class="sourceLine" id="cb2-17" data-line-number="17">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">Reverse</span> (x &#39;<span class="fu">:</span> xs) <span class="fu">=</span> <span class="dt">Append</span> (<span class="dt">Reverse</span> xs) (x &#39;<span class="fu">:</span> &#39;[])</a></code></pre></div>
<p>There’s not too much of interest here, but it’s worth noting the syntax for type level lists <code>'[]</code> and <code>':</code>. Additionally, I’ve made these functions polykinded for reasons that will become apparent momentarily.</p>
<h2 id="a-high-level-outline">A High Level Outline</h2>
<p>Since the actual procedure for this is much more complex than going to a <code>Church</code>, it’s useful to have a high level overview of how we plan on doing this.</p>
<p>The 10,000 foot idea is something like this</p>
<ol type="1">
<li>Take in the church representation which has the form <code>(a -&gt; r) -&gt; (b -&gt; c -&gt; r) -&gt; r -&gt; r</code> or similar</li>
<li>Apply the constructors of the type we’re interested in to the church representation</li>
<li>Let the representation do the actual work of constructing the value</li>
</ol>
<p>Our first major hiccup is that we don’t actually have access to the constructors. Instead we’ll traverse the generic representation of our type, and produce a type level list of “breadcrumbs” (I’ll explain more shortly) that can be fed to a typeclass to yield a function which takes either a <code>U1 p</code>, <code>K1 a t p</code>, or some large combination of <code>:*:</code>’s and returns our value. This serves as our makeshift constructor.</p>
<p>However, this alone isn’t enough since we need to provide a function of type <code>a -&gt; b -&gt; r</code> not <code>M1 foo bar (K1 baz a) :*: M1 foo bar (K1 quux a) -&gt; r</code>. We need something akin to the ultimate “uncurry” that’ll take in a large product type component by component and build it up. In order to do this, we’ll introduce a “shell” of a product type where each node is <code>undefined</code>, then as we get each new argument, we’ll fill in the corresponding <code>undefined</code> in the product type.</p>
<p>This isn’t the nicest solution, since we’re playing with fire by tossing around <code>undefined</code>, but it’s much simpler than the alternatives which is to either somehow convert a tree of product types into a flat constructor or make each field of the product type maybe and step by step fill in each field <em>and</em> change the type of the product type.</p>
<h2 id="the-gory-details">The Gory Details</h2>
<p>Without further delay, let’s dive in! First let’s create our type level representation of the “bread-crumbs” we’ll use to represent the type’s structure</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb3-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">Traverse</span> a <span class="fu">=</span> <span class="dt">Meta</span> a a a <span class="fu">|</span> <span class="dt">InL</span> a a <span class="fu">|</span> <span class="dt">InR</span> a a <span class="fu">|</span> <span class="dt">Term</span> a</a></code></pre></div>
<p>In our use case, we’ll specialize <code>a</code> to be <code>*</code>, the kind of simple haskell types.</p>
<p>Now the way to think about this is as a series of directions, each element of the list a specific instruction on how to navigate the type.</p>
<ul>
<li><code>Meta</code> represents a type wrapped in an <code>M1</code> constructor so that <code>M1 a b f p</code> corresponds to <code>Meta a b p</code></li>
<li><code>InL</code> and <code>InR</code> represent going left or right in a <code>:+:</code> or <code>:*:</code>. Going left at <code>(l :*: r) p</code> is <code>InL (r p) p</code></li>
<li><code>Term</code> is the endpoint of our directions, it holds some leaf in the “tree” we’re considering</li>
</ul>
<p>Now with this in mind, here’s how we can construct all possible paths in our types</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" data-line-number="1">    <span class="kw">type</span> family <span class="dt">MakePaths</span> v (<span class="ot">m ::</span> [<span class="dt">Traverse</span> <span class="fu">*</span>]) (<span class="ot">r ::</span> [ [<span class="dt">Traverse</span> <span class="fu">*</span>] ])<span class="ot"> ::</span> [[<span class="dt">Traverse</span> <span class="fu">*</span>] ]</a>
<a class="sourceLine" id="cb4-2" data-line-number="2">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">MakePaths</span> ((<span class="fu">:+:</span>) l r p) s all <span class="fu">=</span></a>
<a class="sourceLine" id="cb4-3" data-line-number="3">      <span class="dt">Append</span> (<span class="dt">MakePaths</span> (l p) (<span class="dt">InL</span> (r p) p &#39;<span class="fu">:</span> s) &#39;[])</a>
<a class="sourceLine" id="cb4-4" data-line-number="4">              (<span class="dt">Append</span> (<span class="dt">MakePaths</span> (r p) (<span class="dt">InR</span> (l p) p &#39;<span class="fu">:</span> s) &#39;[]) all)</a>
<a class="sourceLine" id="cb4-5" data-line-number="5">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">MakePaths</span> (<span class="dt">M1</span> a b f p) s all  <span class="fu">=</span></a>
<a class="sourceLine" id="cb4-6" data-line-number="6">      <span class="dt">MakePaths</span> (f p) (<span class="dt">Meta</span> a b p &#39;<span class="fu">:</span> s) all</a>
<a class="sourceLine" id="cb4-7" data-line-number="7">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">MakePaths</span> (<span class="dt">K1</span> a t p) s all    <span class="fu">=</span>  <span class="dt">Reverse</span> (<span class="dt">Term</span> (<span class="dt">K1</span> a t p) &#39;<span class="fu">:</span> s)    &#39;<span class="fu">:</span> all</a>
<a class="sourceLine" id="cb4-8" data-line-number="8">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">MakePaths</span> (<span class="dt">U1</span> p) s all        <span class="fu">=</span>  <span class="dt">Reverse</span> (<span class="dt">Term</span> (<span class="dt">U1</span> p) &#39;<span class="fu">:</span> s)        &#39;<span class="fu">:</span> all</a>
<a class="sourceLine" id="cb4-9" data-line-number="9">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">MakePaths</span> ((<span class="fu">:*:</span>) l r p) s all <span class="fu">=</span>  <span class="dt">Reverse</span> (<span class="dt">Term</span> ((<span class="fu">:*:</span>) l r p) &#39;<span class="fu">:</span> s) &#39;<span class="fu">:</span> all</a></code></pre></div>
<p>This traverse each path, maintaining a stack of all the current stuff seen as <code>s</code> previous paths as <code>all</code>. Similarly, we can also reconstruct the original type given a path</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb5-1" data-line-number="1">    <span class="kw">type</span> family <span class="dt">ReconstructPath</span> (<span class="ot">t ::</span> [<span class="dt">Traverse</span> <span class="fu">*</span>])</a>
<a class="sourceLine" id="cb5-2" data-line-number="2">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">ReconstructPath</span> (<span class="dt">InL</span> r p  &#39;<span class="fu">:</span> rest) <span class="fu">=</span></a>
<a class="sourceLine" id="cb5-3" data-line-number="3">      (<span class="dt">WithoutParam</span> (<span class="dt">ReconstructPath</span> rest) <span class="fu">:+:</span> <span class="dt">WithoutParam</span> r) p</a>
<a class="sourceLine" id="cb5-4" data-line-number="4">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">ReconstructPath</span> (<span class="dt">InR</span> l p  &#39;<span class="fu">:</span> rest) <span class="fu">=</span></a>
<a class="sourceLine" id="cb5-5" data-line-number="5">      (<span class="dt">WithoutParam</span> l <span class="fu">:+:</span> <span class="dt">WithoutParam</span> (<span class="dt">ReconstructPath</span> rest)) p</a>
<a class="sourceLine" id="cb5-6" data-line-number="6">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">ReconstructPath</span> (<span class="dt">Meta</span> a b p &#39;<span class="fu">:</span> rest) <span class="fu">=</span></a>
<a class="sourceLine" id="cb5-7" data-line-number="7">      <span class="dt">M1</span> a b (<span class="dt">WithoutParam</span> (<span class="dt">ReconstructPath</span> rest)) p</a>
<a class="sourceLine" id="cb5-8" data-line-number="8">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">ReconstructPath</span> (<span class="dt">Term</span> a     &#39;<span class="fu">:</span> &#39;[])  <span class="fu">=</span> a</a></code></pre></div>
<p>Now it should be clear why we need to store the <code>p</code> and the other side of <code>:*:</code>s and <code>:+:</code>s, without them we couldn’t possible reconstruct the type.</p>
<p>We need one final type family before we can start doing real work, this needs to take a path and return the type that the path leads to, in other words the <code>Term a</code> at the end of our list.</p>
<p>Extracting this is pretty mechanical</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb6-1" data-line-number="1">    <span class="kw">type</span> family <span class="dt">PathArg</span> (<span class="ot">t ::</span> [<span class="dt">Traverse</span> <span class="fu">*</span>])</a>
<a class="sourceLine" id="cb6-2" data-line-number="2">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">PathArg</span> (<span class="dt">Term</span> a     &#39;<span class="fu">:</span> &#39;[] ) <span class="fu">=</span> a</a>
<a class="sourceLine" id="cb6-3" data-line-number="3">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">PathArg</span> (<span class="dt">Meta</span> a b p &#39;<span class="fu">:</span> rest) <span class="fu">=</span> <span class="dt">PathArg</span> rest</a>
<a class="sourceLine" id="cb6-4" data-line-number="4">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">PathArg</span> (<span class="dt">InR</span> l p    &#39;<span class="fu">:</span> rest) <span class="fu">=</span> <span class="dt">PathArg</span> rest</a>
<a class="sourceLine" id="cb6-5" data-line-number="5">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">PathArg</span> (<span class="dt">InL</span> r p    &#39;<span class="fu">:</span> rest) <span class="fu">=</span> <span class="dt">PathArg</span> rest</a></code></pre></div>
<p>We only need to explicitly pattern match on <code>Meta</code>’s and others because type families don’t have the same convenient “fall through” semantics as normal Haskell functions.</p>
<p>Notice that we expect our list to be terminated by a <code>Term a ': '[]</code>, if they aren’t it represents a bug in <code>MakePaths</code> and will fail at compile time.</p>
<p>Now we can actually start writing some transformations, first let’s define a function that takes a <code>[Traverse *]</code> and the corresponding <code>PathArg</code> and returns our type</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb7-1" data-line-number="1">    <span class="kw">class</span> <span class="dt">GPath</span> (<span class="ot">p ::</span> [<span class="dt">Traverse</span> <span class="fu">*</span>]) <span class="kw">where</span></a>
<a class="sourceLine" id="cb7-2" data-line-number="2"><span class="ot">      path ::</span> <span class="dt">Proxy</span> p <span class="ot">-&gt;</span> <span class="dt">PathArg</span> p <span class="ot">-&gt;</span> <span class="dt">ReconstructPath</span> p</a>
<a class="sourceLine" id="cb7-3" data-line-number="3">    <span class="kw">instance</span> <span class="dt">GPath</span> (<span class="dt">Term</span> a &#39;<span class="fu">:</span> &#39;[]) <span class="kw">where</span></a>
<a class="sourceLine" id="cb7-4" data-line-number="4">      path _ <span class="fu">=</span> id</a>
<a class="sourceLine" id="cb7-5" data-line-number="5">    <span class="kw">instance</span> ((<span class="dt">WithoutParam</span> (<span class="dt">ReconstructPath</span> rest)) p <span class="fu">~</span> <span class="dt">ReconstructPath</span> rest, <span class="dt">GPath</span> rest)</a>
<a class="sourceLine" id="cb7-6" data-line-number="6">             <span class="ot">=&gt;</span> <span class="dt">GPath</span> (<span class="dt">InR</span> r p &#39;<span class="fu">:</span> rest) <span class="kw">where</span></a>
<a class="sourceLine" id="cb7-7" data-line-number="7">      path p a <span class="fu">=</span> <span class="dt">R1</span> <span class="fu">$</span> path (pTail p) a</a>
<a class="sourceLine" id="cb7-8" data-line-number="8">    <span class="kw">instance</span> ((<span class="dt">WithoutParam</span> (<span class="dt">ReconstructPath</span> rest)) p <span class="fu">~</span> <span class="dt">ReconstructPath</span> rest, <span class="dt">GPath</span> rest)</a>
<a class="sourceLine" id="cb7-9" data-line-number="9">             <span class="ot">=&gt;</span> <span class="dt">GPath</span> (<span class="dt">InL</span> l p &#39;<span class="fu">:</span> rest) <span class="kw">where</span></a>
<a class="sourceLine" id="cb7-10" data-line-number="10">      path p a <span class="fu">=</span> <span class="dt">L1</span> <span class="fu">$</span> path (pTail p) a</a>
<a class="sourceLine" id="cb7-11" data-line-number="11">    <span class="kw">instance</span> ((<span class="dt">WithoutParam</span> (<span class="dt">ReconstructPath</span> rest)) p <span class="fu">~</span> <span class="dt">ReconstructPath</span> rest, <span class="dt">GPath</span> rest)</a>
<a class="sourceLine" id="cb7-12" data-line-number="12">             <span class="ot">=&gt;</span> <span class="dt">GPath</span> (<span class="dt">Meta</span> a b p &#39;<span class="fu">:</span> rest) <span class="kw">where</span></a>
<a class="sourceLine" id="cb7-13" data-line-number="13">      path p a <span class="fu">=</span> <span class="dt">M1</span> <span class="fu">$</span> path (pTail p) a</a></code></pre></div>
<p>This code rolls out pretty similarly to how <code>ReconstructPath</code> works, the only difference is at the end of it all we stick the <code>PathArg</code> we’ve been lugging around with us into the appropriate “slot” in our type.</p>
<p>Now we just need to take a series of paths and somehow give them to the church representation with <code>path</code> to mimic constructors</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb8-1" data-line-number="1">    <span class="kw">class</span> <span class="dt">GBuild</span> (<span class="ot">paths ::</span> [[<span class="dt">Traverse</span> <span class="fu">*</span>] ])f r <span class="kw">where</span></a>
<a class="sourceLine" id="cb8-2" data-line-number="2"><span class="ot">      build ::</span> <span class="dt">Proxy</span> paths <span class="ot">-&gt;</span> f <span class="ot">-&gt;</span> r</a>
<a class="sourceLine" id="cb8-3" data-line-number="3"></a>
<a class="sourceLine" id="cb8-4" data-line-number="4">    <span class="co">-- | Unit case. This represents constructors with no arguments</span></a>
<a class="sourceLine" id="cb8-5" data-line-number="5">    <span class="kw">instance</span> (<span class="dt">ReconstructPath</span> x <span class="fu">~</span> r, <span class="dt">GPath</span> x, <span class="dt">GBuild</span> xs f&#39; r, <span class="dt">PathArg</span> x <span class="fu">~</span> <span class="dt">U1</span> p)</a>
<a class="sourceLine" id="cb8-6" data-line-number="6">             <span class="ot">=&gt;</span> <span class="dt">GBuild</span> (x &#39;<span class="fu">:</span> xs) (r <span class="ot">-&gt;</span> f&#39;) r <span class="kw">where</span></a>
<a class="sourceLine" id="cb8-7" data-line-number="7">      build p f <span class="fu">=</span> build (pTail p) <span class="fu">$</span> f (path (pHead p) <span class="dt">U1</span>)</a>
<a class="sourceLine" id="cb8-8" data-line-number="8">    <span class="kw">instance</span> (<span class="fu">???</span>) <span class="ot">=&gt;</span> <span class="dt">GBuild</span> (x &#39;<span class="fu">:</span> xs) ((f <span class="ot">-&gt;</span> g) <span class="ot">-&gt;</span> f&#39;) r <span class="kw">where</span></a>
<a class="sourceLine" id="cb8-9" data-line-number="9">      build p f <span class="fu">=</span> build (pTail p) <span class="fu">$</span> f (<span class="fu">???</span> <span class="fu">$</span> path (pHead p))</a>
<a class="sourceLine" id="cb8-10" data-line-number="10">    <span class="kw">instance</span> <span class="dt">GBuild</span> &#39;[] r r <span class="kw">where</span></a>
<a class="sourceLine" id="cb8-11" data-line-number="11">      build _ f <span class="fu">=</span> f</a></code></pre></div>
<p>So <code>build</code> takes a list of paths by <code>Proxy</code>, the church representation <code>f</code>, and returns our result <code>r</code>, the last instance is the simplest, it represents once we’ve fully applied the church representation and can now just return it.</p>
<p>The next simplest one is the first one, where we have a constructor with no arguments so the <code>PathArg</code> is <code>U1 p</code>. Since we can trivially construct a <code>U1 p</code>, we just do so and give it to <code>path</code> ourselves, this creates an <code>r</code> that we can give to the church representation. Recall that for a <code>Church a r</code>, no argument constructors of <code>a</code> are simply represented with <code>r</code>.</p>
<p>Now the tricky bit, in the second instance we must fill in <code>???</code> with something that takes a product type and converts it to something that will swallow and fill in our product type step by step.</p>
<p>Our first step in doing this is to create a generic way of creating “empty” product types</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb9-1" data-line-number="1">    <span class="kw">class</span> <span class="dt">GEmpty</span> a <span class="kw">where</span></a>
<a class="sourceLine" id="cb9-2" data-line-number="2"><span class="ot">      empty ::</span> a</a>
<a class="sourceLine" id="cb9-3" data-line-number="3">    <span class="kw">instance</span> <span class="dt">GEmpty</span> (<span class="dt">U1</span> p) <span class="kw">where</span></a>
<a class="sourceLine" id="cb9-4" data-line-number="4">      empty <span class="fu">=</span> <span class="dt">U1</span></a>
<a class="sourceLine" id="cb9-5" data-line-number="5">    <span class="kw">instance</span> <span class="dt">GEmpty</span> (<span class="dt">K1</span> a t p) <span class="kw">where</span></a>
<a class="sourceLine" id="cb9-6" data-line-number="6">      empty <span class="fu">=</span> <span class="dt">K1</span> (error <span class="st">&quot;Error! The impossible has happened.&quot;</span>)</a>
<a class="sourceLine" id="cb9-7" data-line-number="7">    <span class="kw">instance</span> <span class="dt">GEmpty</span> (f p) <span class="ot">=&gt;</span> <span class="dt">GEmpty</span> (<span class="dt">M1</span> a b f p) <span class="kw">where</span></a>
<a class="sourceLine" id="cb9-8" data-line-number="8">      empty <span class="fu">=</span> <span class="dt">M1</span> empty</a>
<a class="sourceLine" id="cb9-9" data-line-number="9">    <span class="kw">instance</span> (<span class="dt">GEmpty</span> (l p), <span class="dt">GEmpty</span> (r p)) <span class="ot">=&gt;</span> <span class="dt">GEmpty</span> ((<span class="fu">:*:</span>) l r p) <span class="kw">where</span></a>
<a class="sourceLine" id="cb9-10" data-line-number="10">      empty <span class="fu">=</span> empty <span class="fu">:*:</span> empty</a></code></pre></div>
<p>It’s pretty clear how this works. Everything except <code>K1</code>’s are filled in with <code>empty</code>’s and <code>K1</code> is filled in with something equivalent to <code>undefined</code>.</p>
<p>Now we can write a type family to give us the paths to each <code>K1</code> in a product type</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb10-1" data-line-number="1">    <span class="kw">type</span> family <span class="dt">MakeProdPaths</span> v (<span class="ot">m ::</span> [<span class="dt">Traverse</span> <span class="fu">*</span>]) (<span class="ot">r ::</span> [ [<span class="dt">Traverse</span> <span class="fu">*</span>] ])<span class="ot"> ::</span> [[<span class="dt">Traverse</span> <span class="fu">*</span>] ]</a>
<a class="sourceLine" id="cb10-2" data-line-number="2">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">MakeProdPaths</span> (<span class="dt">K1</span> a t p) s all    <span class="fu">=</span> <span class="dt">Reverse</span> (<span class="dt">Term</span> (<span class="dt">K1</span> a t p) &#39;<span class="fu">:</span> s) &#39;<span class="fu">:</span> all</a>
<a class="sourceLine" id="cb10-3" data-line-number="3">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">MakeProdPaths</span> (<span class="dt">M1</span> a b f p) s all  <span class="fu">=</span> <span class="dt">MakeProdPaths</span> (f p) (<span class="dt">Meta</span> a b p &#39;<span class="fu">:</span> s) all</a>
<a class="sourceLine" id="cb10-4" data-line-number="4">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">MakeProdPaths</span> ((<span class="fu">:*:</span>) l r p) s all <span class="fu">=</span></a>
<a class="sourceLine" id="cb10-5" data-line-number="5">      <span class="dt">Append</span> (<span class="dt">MakeProdPaths</span> (l p) (<span class="dt">InL</span> (r p) p &#39;<span class="fu">:</span> s) &#39;[])</a>
<a class="sourceLine" id="cb10-6" data-line-number="6">              (<span class="dt">Append</span> (<span class="dt">MakeProdPaths</span> (r p) (<span class="dt">InR</span> (l p) p &#39;<span class="fu">:</span> s) &#39;[]) all)</a></code></pre></div>
<p>This should strike the reader as very similar to <code>MakePaths</code> because it is. In fact the only real difference is that we only accept <code>K1</code>’s in nodes and that we’re branching on <code>:*:</code>’s.</p>
<p>Just like <code>MakePaths</code>, we’ll want a corresponding type class to take a path and a value and fill in the corresponding bit of our structure.</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb11-1" data-line-number="1">    <span class="kw">class</span> <span class="dt">GUpdate</span> (<span class="ot">path ::</span> [<span class="dt">Traverse</span> <span class="fu">*</span>]) a <span class="kw">where</span></a>
<a class="sourceLine" id="cb11-2" data-line-number="2"><span class="ot">      update ::</span> <span class="dt">Proxy</span> path <span class="ot">-&gt;</span> a <span class="ot">-&gt;</span> <span class="dt">PathArg</span> path <span class="ot">-&gt;</span> a</a>
<a class="sourceLine" id="cb11-3" data-line-number="3">    <span class="kw">instance</span> <span class="dt">GUpdate</span> (<span class="dt">Term</span> (<span class="dt">K1</span> a t p) &#39;<span class="fu">:</span> &#39;[]) (<span class="dt">K1</span> a t p) <span class="kw">where</span></a>
<a class="sourceLine" id="cb11-4" data-line-number="4">      update _ _ a <span class="fu">=</span> a</a>
<a class="sourceLine" id="cb11-5" data-line-number="5">    <span class="kw">instance</span> <span class="dt">GUpdate</span> rest (l p) <span class="ot">=&gt;</span> <span class="dt">GUpdate</span> (<span class="dt">InL</span> (r p) p &#39;<span class="fu">:</span> rest) ((<span class="fu">:*:</span>) l r p) <span class="kw">where</span></a>
<a class="sourceLine" id="cb11-6" data-line-number="6">      update p (l <span class="fu">:*:</span> r) a <span class="fu">=</span> update (pTail p) l a <span class="fu">:*:</span> r</a>
<a class="sourceLine" id="cb11-7" data-line-number="7">    <span class="kw">instance</span> <span class="dt">GUpdate</span> rest (r p) <span class="ot">=&gt;</span> <span class="dt">GUpdate</span> (<span class="dt">InR</span> (l p) p &#39;<span class="fu">:</span> rest) ((<span class="fu">:*:</span>) l r p) <span class="kw">where</span></a>
<a class="sourceLine" id="cb11-8" data-line-number="8">      update p (l <span class="fu">:*:</span> r) a <span class="fu">=</span> l <span class="fu">:*:</span> update (pTail p) r a</a>
<a class="sourceLine" id="cb11-9" data-line-number="9">    <span class="kw">instance</span> <span class="dt">GUpdate</span> rest (f p) <span class="ot">=&gt;</span> <span class="dt">GUpdate</span> (<span class="dt">Meta</span> a b p &#39;<span class="fu">:</span> rest) (<span class="dt">M1</span> a b f p) <span class="kw">where</span></a>
<a class="sourceLine" id="cb11-10" data-line-number="10">      update p (<span class="dt">M1</span> f) a <span class="fu">=</span> <span class="dt">M1</span> (update (pTail p) f a)</a></code></pre></div>
<p>Unlike <code>GPath</code>, we take in a structure instead of updating it instead of creating an entirely new one. Notice that we’re careful to never be strict in any leaves of the structure since we intend to use this with <code>GEmpty</code> and all those leaves will blow up if poked.</p>
<p>Now for the really clever bit, we can use <code>update</code> to create a function which will take in an argument and the corresponding path to fill it in in the structure.</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb12-1" data-line-number="1">    <span class="kw">type</span> family <span class="dt">Fill</span> (<span class="ot">paths ::</span> [[<span class="dt">Traverse</span> <span class="fu">*</span>] ]) r</a>
<a class="sourceLine" id="cb12-2" data-line-number="2">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">Fill</span> (x &#39;<span class="fu">:</span> xs) r <span class="fu">=</span> <span class="dt">StripK</span> (<span class="dt">PathArg</span> x) <span class="ot">-&gt;</span> <span class="dt">Fill</span> xs r</a>
<a class="sourceLine" id="cb12-3" data-line-number="3">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">Fill</span> &#39;[] r <span class="fu">=</span> r</a>
<a class="sourceLine" id="cb12-4" data-line-number="4"></a>
<a class="sourceLine" id="cb12-5" data-line-number="5">    <span class="kw">class</span> <span class="dt">GFill</span> (<span class="ot">paths ::</span> [[<span class="dt">Traverse</span> <span class="fu">*</span>] ]) a <span class="kw">where</span></a>
<a class="sourceLine" id="cb12-6" data-line-number="6"><span class="ot">      fill ::</span> <span class="dt">Proxy</span> paths <span class="ot">-&gt;</span> (a <span class="ot">-&gt;</span> r) <span class="ot">-&gt;</span> a <span class="ot">-&gt;</span> <span class="dt">Fill</span> paths r</a>
<a class="sourceLine" id="cb12-7" data-line-number="7">    <span class="kw">instance</span> <span class="dt">GFill</span> &#39;[] a <span class="kw">where</span></a>
<a class="sourceLine" id="cb12-8" data-line-number="8">      fill _ f a <span class="fu">=</span> f a</a>
<a class="sourceLine" id="cb12-9" data-line-number="9">    <span class="kw">instance</span> (<span class="dt">PathArg</span> x <span class="fu">~</span> <span class="dt">K1</span> m t p, <span class="dt">StripK</span> (<span class="dt">PathArg</span> x) <span class="fu">~</span> t, <span class="dt">GUpdate</span> x a, <span class="dt">GFill</span> xs a) <span class="ot">=&gt;</span></a>
<a class="sourceLine" id="cb12-10" data-line-number="10">             <span class="dt">GFill</span> (x &#39;<span class="fu">:</span> xs) a <span class="kw">where</span></a>
<a class="sourceLine" id="cb12-11" data-line-number="11">      fill p f a <span class="fu">=</span> \x <span class="ot">-&gt;</span> fill (pTail p) f <span class="fu">$</span> update (pHead p) a (<span class="dt">K1</span> x)</a></code></pre></div>
<p><code>Fill</code> represents the type of these functions. While most of this code is as one would expect, notice that we take a continuation <code>a -&gt; r</code> and drag it through <code>fill</code> and finally apply it once we’ve filled in all the leaves. There is a good reason for this, we intend to use this with <code>path</code>, but we can’t compose them since <code>fill</code> takes varying amounts of arguments. Instead, we opt for a bit of continuation passing style and through <code>path</code> into <code>fill</code> and let <code>fill</code> call it where appropriate.</p>
<p>Now we can fill in that last bit of <code>GBuild</code></p>
<div class="sourceCode" id="cb13"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb13-1" data-line-number="1">    <span class="kw">instance</span> ((f <span class="ot">-&gt;</span> g) <span class="fu">~</span> <span class="dt">Fill</span> (<span class="dt">MakeProdPaths</span> (<span class="dt">PathArg</span> x) &#39;[] &#39;[]) r,</a>
<a class="sourceLine" id="cb13-2" data-line-number="2">              <span class="dt">ReconstructPath</span> x <span class="fu">~</span> r, <span class="dt">GEmpty</span> (<span class="dt">PathArg</span> x), <span class="dt">GBuild</span> xs f&#39; r,</a>
<a class="sourceLine" id="cb13-3" data-line-number="3">              (<span class="dt">GFill</span> (<span class="dt">MakeProdPaths</span> (<span class="dt">PathArg</span> x) &#39;[] &#39;[]) (<span class="dt">PathArg</span> x)),</a>
<a class="sourceLine" id="cb13-4" data-line-number="4">              <span class="dt">GPath</span> x)</a>
<a class="sourceLine" id="cb13-5" data-line-number="5">             <span class="ot">=&gt;</span> <span class="dt">GBuild</span> (x &#39;<span class="fu">:</span> xs) ((f <span class="ot">-&gt;</span> g) <span class="ot">-&gt;</span> f&#39;) r <span class="kw">where</span></a>
<a class="sourceLine" id="cb13-6" data-line-number="6">      build p f <span class="fu">=</span> build (pTail p) <span class="fu">$</span> f (fill (prod p) (path (pHead p)) empty)</a>
<a class="sourceLine" id="cb13-7" data-line-number="7">        <span class="kw">where</span><span class="ot"> prod ::</span> forall xs<span class="fu">.</span> <span class="dt">Proxy</span> xs <span class="ot">-&gt;</span> <span class="dt">Proxy</span> (<span class="dt">MakeProdPaths</span> (<span class="dt">PathArg</span> (<span class="dt">Head</span> xs)) &#39;[] &#39;[])</a>
<a class="sourceLine" id="cb13-8" data-line-number="8">              prod _ <span class="fu">=</span> <span class="dt">Proxy</span></a></code></pre></div>
<p>And now we’re almost done! We can wrap all of this up into one function</p>
<div class="sourceCode" id="cb14"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb14-1" data-line-number="1"><span class="ot">    fromChurch ::</span> forall a<span class="fu">.</span> (<span class="dt">Generic</span> a,</a>
<a class="sourceLine" id="cb14-2" data-line-number="2">                       <span class="dt">GBuild</span> (<span class="dt">MakePaths</span> (<span class="dt">Rep</span> a ()) &#39;[] &#39;[])</a>
<a class="sourceLine" id="cb14-3" data-line-number="3">                              (<span class="dt">Church</span> a (<span class="dt">Rep</span> a ()))</a>
<a class="sourceLine" id="cb14-4" data-line-number="4">                              (<span class="dt">Rep</span> a ()))</a>
<a class="sourceLine" id="cb14-5" data-line-number="5">                  <span class="ot">=&gt;</span> <span class="dt">Church</span> a (<span class="dt">Rep</span> a ()) <span class="ot">-&gt;</span> a</a>
<a class="sourceLine" id="cb14-6" data-line-number="6">    fromChurch c <span class="fu">=</span> to <span class="fu">$</span> (build p<span class="ot"> c ::</span> <span class="dt">Rep</span> a ())</a>
<a class="sourceLine" id="cb14-7" data-line-number="7">      <span class="kw">where</span><span class="ot"> p ::</span> <span class="dt">Proxy</span> (<span class="dt">MakePaths</span> (<span class="dt">Rep</span> a ()) &#39;[] &#39;[])</a>
<a class="sourceLine" id="cb14-8" data-line-number="8">            p <span class="fu">=</span> <span class="dt">Proxy</span></a></code></pre></div>
<p>And that’s it! Automatic reconstruction of types from church representations! To demonstrate</p>
<pre><code>&gt; fromChurch (\nothing just -&gt; just True) :: Maybe Bool
   Just True
&gt; fromChurch (\f -&gt; f &#39;a&#39; &quot;foo&quot;) :: (Char, String)
   (&#39;a&#39;, &quot;foo&quot;)</code></pre>
<p>Thanks for reading through this (rather dense) series of posts! Most of the code can be found bundled into a package: <a href="http://bitbucket.org/jozefg/generic-church">generic-church</a>.</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Mon, 10 Mar 2014 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2014-03-10-revenge-of-churchrep.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Church Representations: Part 2</title>
    <link>http://jozefg.bitbucket.org/posts/2014-03-07-church-the-sequel.html</link>
    <description><![CDATA[<div class="info">
    Posted on March  7, 2014
    
</div>
<div class="info">
    
    Tags: <a href="/tags/haskell.html">haskell</a>, <a href="/tags/types.html">types</a>
    
</div>

<p>In the <a href="/posts/2014-03-06-church.html">last</a> post, we discussed some of the type families needed to transform a type equipped with a <code>Generic</code> instance into a church representation.</p>
<p>In this post, we’ll go over the type class prolog needed to actually mechanically transform values between these types.</p>
<p>To start with, we’ll need a bit of boilerplate. Here’s our language extensions and imports.</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb1-1" data-line-number="1">    <span class="ot">{-# LANGUAGE TypeFamilies,          TypeOperators,     UndecidableInstances #-}</span></a>
<a class="sourceLine" id="cb1-2" data-line-number="2">    <span class="ot">{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, FlexibleContexts     #-}</span></a>
<a class="sourceLine" id="cb1-3" data-line-number="3">    <span class="ot">{-# LANGUAGE ScopedTypeVariables,   RankNTypes                              #-}</span></a>
<a class="sourceLine" id="cb1-4" data-line-number="4">    <span class="kw">import</span> <span class="dt">GHC.Generics</span></a>
<a class="sourceLine" id="cb1-5" data-line-number="5">    <span class="kw">import</span> <span class="dt">Data.Proxy</span></a></code></pre></div>
<p>Now our first order of business is to write a function to take a <code>GHC.Generic</code>s value and transform it into the corresponding value occupying the type returned by <code>StripMeta</code>.</p>
<p>To do this we rely on type classes</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb2-1" data-line-number="1">    <span class="kw">class</span> <span class="dt">GStripMeta</span> a <span class="kw">where</span></a>
<a class="sourceLine" id="cb2-2" data-line-number="2"><span class="ot">      stripMeta ::</span> a <span class="ot">-&gt;</span> <span class="dt">StripMeta</span> a</a>
<a class="sourceLine" id="cb2-3" data-line-number="3">    <span class="kw">instance</span> <span class="dt">GStripMeta</span> (f p) <span class="ot">=&gt;</span> <span class="dt">GStripMeta</span> (<span class="dt">M1</span> a b f p) <span class="kw">where</span></a>
<a class="sourceLine" id="cb2-4" data-line-number="4">      stripMeta (<span class="dt">M1</span> f) <span class="fu">=</span> stripMeta f</a>
<a class="sourceLine" id="cb2-5" data-line-number="5">    <span class="kw">instance</span> <span class="dt">GStripMeta</span> (<span class="dt">K1</span> a t p) <span class="kw">where</span></a>
<a class="sourceLine" id="cb2-6" data-line-number="6">      stripMeta <span class="fu">=</span> id</a>
<a class="sourceLine" id="cb2-7" data-line-number="7">    <span class="kw">instance</span> <span class="dt">GStripMeta</span> (<span class="dt">U1</span> p) <span class="kw">where</span></a>
<a class="sourceLine" id="cb2-8" data-line-number="8">      stripMeta <span class="fu">=</span> id</a>
<a class="sourceLine" id="cb2-9" data-line-number="9">    <span class="kw">instance</span> (<span class="dt">GStripMeta</span> (l p), <span class="dt">GStripMeta</span> (r p),</a>
<a class="sourceLine" id="cb2-10" data-line-number="10">              (<span class="dt">WithoutParam</span> (<span class="dt">StripMeta</span> (l p))) p <span class="fu">~</span> <span class="dt">StripMeta</span> (l p),</a>
<a class="sourceLine" id="cb2-11" data-line-number="11">              (<span class="dt">WithoutParam</span> (<span class="dt">StripMeta</span> (r p))) p <span class="fu">~</span> <span class="dt">StripMeta</span> (r p)) <span class="ot">=&gt;</span></a>
<a class="sourceLine" id="cb2-12" data-line-number="12">             <span class="dt">GStripMeta</span> ((<span class="fu">:*:</span>) l r p) <span class="kw">where</span></a>
<a class="sourceLine" id="cb2-13" data-line-number="13">      stripMeta (l <span class="fu">:*:</span> r) <span class="fu">=</span> stripMeta l <span class="fu">:*:</span> stripMeta r</a>
<a class="sourceLine" id="cb2-14" data-line-number="14">    <span class="kw">instance</span> (<span class="dt">GStripMeta</span> (l p), <span class="dt">GStripMeta</span> (r p),</a>
<a class="sourceLine" id="cb2-15" data-line-number="15">              (<span class="dt">WithoutParam</span> (<span class="dt">StripMeta</span> (l p))) p <span class="fu">~</span> <span class="dt">StripMeta</span> (l p),</a>
<a class="sourceLine" id="cb2-16" data-line-number="16">              (<span class="dt">WithoutParam</span> (<span class="dt">StripMeta</span> (r p))) p <span class="fu">~</span> <span class="dt">StripMeta</span> (r p)) <span class="ot">=&gt;</span></a>
<a class="sourceLine" id="cb2-17" data-line-number="17">             <span class="dt">GStripMeta</span> ((<span class="fu">:+:</span>) l r p) <span class="kw">where</span></a>
<a class="sourceLine" id="cb2-18" data-line-number="18">      stripMeta (<span class="dt">L1</span> l) <span class="fu">=</span> <span class="dt">L1</span> <span class="fu">$</span> stripMeta l</a>
<a class="sourceLine" id="cb2-19" data-line-number="19">      stripMeta (<span class="dt">R1</span> r) <span class="fu">=</span> <span class="dt">R1</span> <span class="fu">$</span> stripMeta r</a></code></pre></div>
<p>This does some type class prolog to traverse a GHC.Generics value and systematically throw out the <code>M1</code> annotations. An important technique that I make heavy use of is the <code>~</code> equality constraints. These let us assert that two types are equivalent and the type checker will attempt to verify this once it’s selected the appropriate instance later. Other than that there’s not too much of interest here so let’s move on to something with more substance.</p>
<p>Our <code>ToList</code> type family requires quite a bit of work to automagically do with a type family. The base cases are pretty straightforward</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb3-1" data-line-number="1">    <span class="kw">class</span> <span class="dt">GList</span> a r <span class="kw">where</span></a>
<a class="sourceLine" id="cb3-2" data-line-number="2"><span class="ot">      toList ::</span> <span class="dt">Maybe</span> a <span class="ot">-&gt;</span> r <span class="ot">-&gt;</span> <span class="dt">ToList</span> a r</a>
<a class="sourceLine" id="cb3-3" data-line-number="3">    <span class="kw">instance</span> (<span class="dt">WithoutParam</span> r) p <span class="fu">~</span> r <span class="ot">=&gt;</span> <span class="dt">GList</span> (<span class="dt">U1</span> p) r <span class="kw">where</span></a>
<a class="sourceLine" id="cb3-4" data-line-number="4">      toList <span class="dt">Nothing</span>  r <span class="fu">=</span> <span class="dt">R1</span> r</a>
<a class="sourceLine" id="cb3-5" data-line-number="5">      toList (<span class="dt">Just</span> a) _ <span class="fu">=</span> <span class="dt">L1</span> a</a>
<a class="sourceLine" id="cb3-6" data-line-number="6">    <span class="kw">instance</span> (<span class="dt">WithoutParam</span> r) p <span class="fu">~</span> r <span class="ot">=&gt;</span> <span class="dt">GList</span> (<span class="dt">K1</span> a t p) r <span class="kw">where</span></a>
<a class="sourceLine" id="cb3-7" data-line-number="7">      toList <span class="dt">Nothing</span>  r <span class="fu">=</span> <span class="dt">R1</span> r</a>
<a class="sourceLine" id="cb3-8" data-line-number="8">      toList (<span class="dt">Just</span> a) _ <span class="fu">=</span> <span class="dt">L1</span> a</a>
<a class="sourceLine" id="cb3-9" data-line-number="9">    <span class="kw">instance</span> (<span class="dt">WithoutParam</span> r) p <span class="fu">~</span> r <span class="ot">=&gt;</span> <span class="dt">GList</span> ((l <span class="fu">:*:</span> r&#39;) p) r <span class="kw">where</span></a>
<a class="sourceLine" id="cb3-10" data-line-number="10">      toList <span class="dt">Nothing</span>  r <span class="fu">=</span> <span class="dt">R1</span> r</a>
<a class="sourceLine" id="cb3-11" data-line-number="11">      toList (<span class="dt">Just</span> a) _ <span class="fu">=</span> <span class="dt">L1</span> a</a></code></pre></div>
<p>The trick here is that if we get a <code>Nothing</code>, it means that somewhere in the process of choosing the list we’ve already found state our sum type is in and all we do is pass things of to <code>R1</code>. Otherwise, we shove the value we receive into <code>L1</code>.</p>
<p>Now the tricky bit is the <code>:+:</code> instance which must walk along the spine of our tree flattening things as it goes.</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" data-line-number="1">    <span class="kw">instance</span> (<span class="dt">GList</span> (l p) (<span class="dt">ToList</span> (r&#39; p) r), <span class="dt">GList</span> (r&#39; p) r) <span class="ot">=&gt;</span></a>
<a class="sourceLine" id="cb4-2" data-line-number="2">             <span class="dt">GList</span> ((l <span class="fu">:+:</span> r&#39;) p) r <span class="kw">where</span></a>
<a class="sourceLine" id="cb4-3" data-line-number="3">      toList (<span class="dt">Just</span> sum<span class="fu">@</span>(<span class="dt">L1</span> l)) r <span class="fu">=</span> toList (<span class="dt">Just</span> l) (toList (rNot sum) r)</a>
<a class="sourceLine" id="cb4-4" data-line-number="4">        <span class="kw">where</span><span class="ot"> rNot ::</span> forall l r p<span class="fu">.</span> (l <span class="fu">:+:</span> r) p <span class="ot">-&gt;</span> <span class="dt">Maybe</span> (r p)</a>
<a class="sourceLine" id="cb4-5" data-line-number="5">              rNot _ <span class="fu">=</span> <span class="dt">Nothing</span></a>
<a class="sourceLine" id="cb4-6" data-line-number="6">      toList (<span class="dt">Just</span> sum<span class="fu">@</span>(<span class="dt">R1</span> r&#39;)) r <span class="fu">=</span> toList (lNot sum) (toList (<span class="dt">Just</span> r&#39;) r)</a>
<a class="sourceLine" id="cb4-7" data-line-number="7">        <span class="kw">where</span><span class="ot"> lNot ::</span> forall l r p<span class="fu">.</span> (l <span class="fu">:+:</span> r) p <span class="ot">-&gt;</span> <span class="dt">Maybe</span> (l p)</a>
<a class="sourceLine" id="cb4-8" data-line-number="8">              lNot _ <span class="fu">=</span> <span class="dt">Nothing</span></a>
<a class="sourceLine" id="cb4-9" data-line-number="9">      toList m r <span class="fu">=</span> toList (lNot m) (toList (rNot m) r)</a>
<a class="sourceLine" id="cb4-10" data-line-number="10">        <span class="kw">where</span><span class="ot"> lNot ::</span> forall l r p<span class="fu">.</span> <span class="dt">Maybe</span> ((<span class="fu">:+:</span>) l r p) <span class="ot">-&gt;</span> <span class="dt">Maybe</span> (l p)</a>
<a class="sourceLine" id="cb4-11" data-line-number="11">              lNot _ <span class="fu">=</span> <span class="dt">Nothing</span></a>
<a class="sourceLine" id="cb4-12" data-line-number="12"><span class="ot">              rNot ::</span> forall l r p<span class="fu">.</span> <span class="dt">Maybe</span> ((<span class="fu">:+:</span>) l r p) <span class="ot">-&gt;</span> <span class="dt">Maybe</span> (r p)</a>
<a class="sourceLine" id="cb4-13" data-line-number="13">              rNot _ <span class="fu">=</span> <span class="dt">Nothing</span></a></code></pre></div>
<p>We’re just plugging in the appropriate <code>l</code> and <code>r</code> into <code>toList l (toList r rest)</code>. if both we have <code>Just (L1 l)</code> then we put in <code>Nothing</code> for <code>r</code>, similarly for <code>Just (R1 r)</code> and if we have <code>Nothing</code> then both are filled in as <code>Nothing</code>s.</p>
<p>Notice that we have to a few hoops using <code>lNot</code> and <code>rNot</code>. Otherwise GHC will complain that type classes aren’t injective and it’s not sure how to handle <code>Nothing :: Maybe a</code>. However, a bit of explicit hand holding takes care of this.</p>
<p>For our next type class hacking we need to actually add one type family that I forgot in our last post.</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb5-1" data-line-number="1">    <span class="kw">type</span> family <span class="dt">ToListProd</span> v rest</a>
<a class="sourceLine" id="cb5-2" data-line-number="2">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">ToListProd</span> ((<span class="fu">:*:</span>) l r&#39; p) r <span class="fu">=</span> <span class="dt">ToListProd</span> (l p) (<span class="dt">ToListProd</span> (r&#39; p) r)</a>
<a class="sourceLine" id="cb5-3" data-line-number="3">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">ToListProd</span> (<span class="dt">K1</span> a t p)     r <span class="fu">=</span> (<span class="dt">K1</span> a t     <span class="fu">:*:</span> <span class="dt">WithoutParam</span> r) p</a>
<a class="sourceLine" id="cb5-4" data-line-number="4">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">ToListProd</span> (<span class="dt">U1</span> p)         r <span class="fu">=</span> <span class="dt">U1</span> p <span class="co">-- since U1 is never in `:*:`&#39;s.</span></a></code></pre></div>
<p>This is isomorphic to <code>ToList</code> but instead of restructuring <code>:+:</code>’s, it moves around <code>:*:</code>’s. The corresponding type class for this almost identical to <code>GList</code></p>
<div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb6-1" data-line-number="1">    <span class="kw">class</span> <span class="dt">GListProd</span> a r <span class="kw">where</span></a>
<a class="sourceLine" id="cb6-2" data-line-number="2"><span class="ot">      toListProd ::</span> a <span class="ot">-&gt;</span> r <span class="ot">-&gt;</span> <span class="dt">ToListProd</span> a r</a>
<a class="sourceLine" id="cb6-3" data-line-number="3">    <span class="kw">instance</span> (<span class="dt">WithoutParam</span> r) p <span class="fu">~</span> r <span class="ot">=&gt;</span> <span class="dt">GListProd</span> (<span class="dt">U1</span> p) r <span class="kw">where</span></a>
<a class="sourceLine" id="cb6-4" data-line-number="4">      toListProd <span class="fu">=</span> const <span class="co">-- Throw away the rest which must be ListTerm</span></a>
<a class="sourceLine" id="cb6-5" data-line-number="5">    <span class="kw">instance</span> (<span class="dt">WithoutParam</span> r) p <span class="fu">~</span> r <span class="ot">=&gt;</span> <span class="dt">GListProd</span> (<span class="dt">K1</span> a t p) r <span class="kw">where</span></a>
<a class="sourceLine" id="cb6-6" data-line-number="6">      toListProd <span class="fu">=</span> (<span class="fu">:*:</span>)</a>
<a class="sourceLine" id="cb6-7" data-line-number="7">    <span class="kw">instance</span> (<span class="dt">GListProd</span> (l p) (<span class="dt">ToListProd</span> (r&#39; p) r), <span class="dt">GListProd</span> (r&#39; p) r) <span class="ot">=&gt;</span></a>
<a class="sourceLine" id="cb6-8" data-line-number="8">             <span class="dt">GListProd</span> ((<span class="fu">:*:</span>) l r&#39; p) r <span class="kw">where</span></a>
<a class="sourceLine" id="cb6-9" data-line-number="9">      toListProd (l <span class="fu">:*:</span> r) rest <span class="fu">=</span> toListProd l (toListProd r rest)</a></code></pre></div>
<p>The only notable difference here is that we don’t have a <code>Maybe a</code> since with products both sides our present. This makes the whole thing much simpler.</p>
<p>No we’re ready to proceed to the actual transformation type classes.</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb7-1" data-line-number="1">    <span class="kw">class</span> <span class="dt">GChurchProd</span> a <span class="kw">where</span></a>
<a class="sourceLine" id="cb7-2" data-line-number="2"><span class="ot">      prod ::</span> <span class="dt">Proxy</span> r <span class="ot">-&gt;</span> a <span class="ot">-&gt;</span> <span class="dt">ChurchProd</span> a r <span class="ot">-&gt;</span> r <span class="co">-- Proxy needed for GChurchSum</span></a>
<a class="sourceLine" id="cb7-3" data-line-number="3">    <span class="kw">instance</span> <span class="dt">GChurchProd</span> (<span class="dt">U1</span> p) <span class="kw">where</span></a>
<a class="sourceLine" id="cb7-4" data-line-number="4">      prod _ _ f <span class="fu">=</span> f</a>
<a class="sourceLine" id="cb7-5" data-line-number="5">    <span class="kw">instance</span> <span class="dt">GChurchProd</span> (<span class="dt">K1</span> a t p) <span class="kw">where</span></a>
<a class="sourceLine" id="cb7-6" data-line-number="6">      prod _ (<span class="dt">K1</span> r) f <span class="fu">=</span> f r</a>
<a class="sourceLine" id="cb7-7" data-line-number="7">    <span class="kw">instance</span> <span class="dt">GChurchProd</span> (r p) <span class="ot">=&gt;</span> <span class="dt">GChurchProd</span> ((<span class="fu">:*:</span>) (<span class="dt">K1</span> a t) r p) <span class="kw">where</span></a>
<a class="sourceLine" id="cb7-8" data-line-number="8">      prod p (<span class="dt">K1</span> l <span class="fu">:*:</span> r) f <span class="fu">=</span> prod p r (f l)</a>
<a class="sourceLine" id="cb7-9" data-line-number="9"></a>
<a class="sourceLine" id="cb7-10" data-line-number="10">    <span class="kw">class</span> <span class="dt">Swallow</span> a <span class="kw">where</span></a>
<a class="sourceLine" id="cb7-11" data-line-number="11"><span class="ot">      swallow ::</span> <span class="dt">Proxy</span> a <span class="ot">-&gt;</span> c <span class="ot">-&gt;</span> <span class="dt">ChurchSum</span> a c</a>
<a class="sourceLine" id="cb7-12" data-line-number="12">    <span class="kw">instance</span> <span class="dt">Swallow</span> (<span class="dt">ListTerm</span> p) <span class="kw">where</span></a>
<a class="sourceLine" id="cb7-13" data-line-number="13">      swallow _ c <span class="fu">=</span> c</a>
<a class="sourceLine" id="cb7-14" data-line-number="14">    <span class="kw">instance</span> <span class="dt">Swallow</span> (r p) <span class="ot">=&gt;</span> <span class="dt">Swallow</span> ((<span class="fu">:+:</span>) l r p) <span class="kw">where</span></a>
<a class="sourceLine" id="cb7-15" data-line-number="15">      swallow p c <span class="fu">=</span> \_ <span class="ot">-&gt;</span> swallow (right p) c</a>
<a class="sourceLine" id="cb7-16" data-line-number="16">        <span class="kw">where</span><span class="ot"> right ::</span> forall l r p<span class="fu">.</span> <span class="dt">Proxy</span> ((<span class="fu">:+:</span>) l r p) <span class="ot">-&gt;</span> <span class="dt">Proxy</span> (r p)</a>
<a class="sourceLine" id="cb7-17" data-line-number="17">              right _ <span class="fu">=</span> <span class="dt">Proxy</span></a></code></pre></div>
<p>In <code>GChurchProd</code> we take a value and the corresponding product eliminator and eliminate it. Believe it or not this is essentially the workhorse of this entire library. We’re threading a <code>Proxy r</code> through there which will come in handy when we start to use this in <code>GChurchSum</code>.</p>
<p><code>Swallow</code> is a bit odd. It represents a situation where we’ve already used <code>prod</code> to produce our result and now need to eat the rest of the supplied arguments. Note again the use of <code>Proxy</code> to keep track of types, it’s a useful little library!</p>
<p>Now, finally, <code>GChurchSum</code></p>
<div class="sourceCode" id="cb8"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb8-1" data-line-number="1">    <span class="kw">class</span> <span class="dt">GChurchSum</span> a r <span class="kw">where</span></a>
<a class="sourceLine" id="cb8-2" data-line-number="2"><span class="ot">      elim ::</span> <span class="dt">Proxy</span> r <span class="ot">-&gt;</span> a <span class="ot">-&gt;</span> <span class="dt">ChurchSum</span> a r <span class="co">-- Proxy because type inference is stubborn</span></a>
<a class="sourceLine" id="cb8-3" data-line-number="3"></a>
<a class="sourceLine" id="cb8-4" data-line-number="4">    <span class="kw">instance</span> (<span class="dt">GListProd</span> (l p) (<span class="dt">ListTerm</span> ()), <span class="dt">GChurchProd</span> (<span class="dt">ToListProd</span> (l p) (<span class="dt">ListTerm</span> ())),</a>
<a class="sourceLine" id="cb8-5" data-line-number="5">              <span class="dt">GChurchSum</span> (r&#39; p) r, <span class="dt">Swallow</span> (r&#39; p)) <span class="ot">=&gt;</span></a>
<a class="sourceLine" id="cb8-6" data-line-number="6">             <span class="dt">GChurchSum</span> ((<span class="fu">:+:</span>) l r&#39; p) r <span class="kw">where</span></a>
<a class="sourceLine" id="cb8-7" data-line-number="7">      elim p sum<span class="fu">@</span>(<span class="dt">L1</span> l) <span class="fu">=</span> \f <span class="ot">-&gt;</span></a>
<a class="sourceLine" id="cb8-8" data-line-number="8">        swallow (right sum) (prod p (toListProd l (<span class="dt">ListTerm</span><span class="ot"> ::</span> <span class="dt">ListTerm</span> ())) f)</a>
<a class="sourceLine" id="cb8-9" data-line-number="9">        <span class="kw">where</span><span class="ot"> right ::</span> forall l r p<span class="fu">.</span> (<span class="fu">:+:</span>) l r p <span class="ot">-&gt;</span> <span class="dt">Proxy</span> (r p)</a>
<a class="sourceLine" id="cb8-10" data-line-number="10">              right _ <span class="fu">=</span> <span class="dt">Proxy</span></a>
<a class="sourceLine" id="cb8-11" data-line-number="11">      elim p (<span class="dt">R1</span> r) <span class="fu">=</span> \_ <span class="ot">-&gt;</span> elim p r</a>
<a class="sourceLine" id="cb8-12" data-line-number="12">    <span class="kw">instance</span> <span class="dt">GChurchSum</span> (<span class="dt">ListTerm</span> p) r <span class="kw">where</span></a>
<a class="sourceLine" id="cb8-13" data-line-number="13">      elim _ _ <span class="fu">=</span> error <span class="st">&quot;Malformed generic instance&quot;</span></a></code></pre></div>
<p>Now the <code>elim</code> instance for <code>ListTerm</code> can never be called, this is guarenteed by the definition of <code>toList</code> since a type must occupy one state prior to <code>ToList</code>, we’ll never end up with <code>ListTerm</code>.</p>
<p>Otherwise if we get an <code>L1</code> then we’re at the actual value our sum type is in so we produce an <code>r</code> and swallow the rest of our arguments, notice that this is where we actual transform a leaf into a <code>ToListProd</code> and this is reflected in our constraints. Otherwise we ignore the irrelevant eliminator and recurse!</p>
<p>To put it all together</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb9-1" data-line-number="1"><span class="ot">    from&#39; ::</span> <span class="dt">Generic</span> a <span class="ot">=&gt;</span> a <span class="ot">-&gt;</span> <span class="dt">Rep</span> a ()</a>
<a class="sourceLine" id="cb9-2" data-line-number="2">    from&#39; <span class="fu">=</span> from</a>
<a class="sourceLine" id="cb9-3" data-line-number="3"></a>
<a class="sourceLine" id="cb9-4" data-line-number="4"><span class="ot">    toChurch ::</span> forall a r<span class="fu">.</span></a>
<a class="sourceLine" id="cb9-5" data-line-number="5">                (<span class="dt">Generic</span> a, <span class="dt">GStripMeta</span> (<span class="dt">Rep</span> a ()),</a>
<a class="sourceLine" id="cb9-6" data-line-number="6">                 <span class="dt">GList</span> (<span class="dt">StripMeta</span> (<span class="dt">Rep</span> a ())) (<span class="dt">ListTerm</span> ()),</a>
<a class="sourceLine" id="cb9-7" data-line-number="7">                 <span class="dt">GChurchSum</span> (<span class="dt">ToList</span> (<span class="dt">StripMeta</span> (<span class="dt">Rep</span> a ())) (<span class="dt">ListTerm</span> ())) r) <span class="ot">=&gt;</span></a>
<a class="sourceLine" id="cb9-8" data-line-number="8">                a <span class="ot">-&gt;</span> <span class="dt">Church</span> a r</a>
<a class="sourceLine" id="cb9-9" data-line-number="9">    toChurch <span class="fu">=</span> elim p <span class="fu">.</span> flip toList (<span class="dt">ListTerm</span><span class="ot"> ::</span> <span class="dt">ListTerm</span> ()) <span class="fu">.</span> <span class="dt">Just</span> <span class="fu">.</span> stripMeta <span class="fu">.</span> from&#39;</a>
<a class="sourceLine" id="cb9-10" data-line-number="10">      <span class="kw">where</span> p <span class="fu">=</span> <span class="dt">Proxy</span><span class="ot"> ::</span> <span class="dt">Proxy</span> r</a></code></pre></div>
<p>And we’re done! Using this we can reify a type to it’s church representation. We can mostly ignore that scary looking constraints on <code>toChurch</code> since they <em>should</em> be true by construction.</p>
<p>As a demo</p>
<pre><code>&gt; toChurch [1, 2, 3] True (\_ _ -&gt;  False)
False
&gt; toChurch [] True (\_ _ -&gt;  False)
True</code></pre>
<p>The <code>True</code> corresponds to the <code>[]</code> list case and the function represents <code>(:)</code>. The current <code>True (\_ _ -&gt; False)</code> actually computes <code>null</code>.</p>
<p><em>Edit, added GListProd</em></p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Fri, 07 Mar 2014 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2014-03-07-church-the-sequel.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Church Representations</title>
    <link>http://jozefg.bitbucket.org/posts/2014-03-06-church.html</link>
    <description><![CDATA[<div class="info">
    Posted on March  6, 2014
    
</div>
<div class="info">
    
    Tags: <a href="/tags/haskell.html">haskell</a>, <a href="/tags/types.html">types</a>
    
</div>

<p>A project I’ve been playing with lately is generalizing <code>maybe</code>. Now on the surface that sounds well.. boring. But there’s actually some interesting concepts buried in here.</p>
<h2 id="lambda-the-almighty">Lambda The Almighty</h2>
<p>Let’s start by specifying what I mean when I say “generalize”. When we look at <code>maybe</code>, the type gives us a pretty strong clue on how to implement it</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb1-1" data-line-number="1"><span class="ot">    maybe ::</span> b <span class="ot">-&gt;</span> (a <span class="ot">-&gt;</span> b) <span class="ot">-&gt;</span> <span class="dt">Maybe</span> a <span class="ot">-&gt;</span> b</a>
<a class="sourceLine" id="cb1-2" data-line-number="2">    maybe nothingCase justCase <span class="dt">Nothing</span>  <span class="fu">=</span> nothingCase</a>
<a class="sourceLine" id="cb1-3" data-line-number="3">    maybe nothingCase justCase (<span class="dt">Just</span> a) <span class="fu">=</span> justCase a</a></code></pre></div>
<p>Each argument of corresponds to a different case of the sum type <code>Maybe</code>.</p>
<p>There’s actually a name for this idea, it’s encoding a data type within functions: Church Encoding.</p>
<p>Let’s rattle of some examples:</p>
<p>Tuples</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb2-1" data-line-number="1">    <span class="ot">{-# LANGUAGE RankNTypes #-}</span></a>
<a class="sourceLine" id="cb2-2" data-line-number="2">    <span class="co">-- We&#39;ll call a tuple a Product because it</span></a>
<a class="sourceLine" id="cb2-3" data-line-number="3">    <span class="co">-- looks like the cartesion *product* of two types</span></a>
<a class="sourceLine" id="cb2-4" data-line-number="4">    <span class="kw">type</span> <span class="dt">Product</span> a b <span class="fu">=</span> forall c<span class="fu">.</span> (a <span class="ot">-&gt;</span> b <span class="ot">-&gt;</span> c) <span class="ot">-&gt;</span> c</a>
<a class="sourceLine" id="cb2-5" data-line-number="5"><span class="ot">    pair ::</span> a <span class="ot">-&gt;</span> b <span class="ot">-&gt;</span> <span class="dt">Product</span> a b</a>
<a class="sourceLine" id="cb2-6" data-line-number="6">    pair a b <span class="fu">=</span> \destruct <span class="ot">-&gt;</span> destruct a b</a>
<a class="sourceLine" id="cb2-7" data-line-number="7"></a>
<a class="sourceLine" id="cb2-8" data-line-number="8">    <span class="co">-- We can easily make fst and snd</span></a>
<a class="sourceLine" id="cb2-9" data-line-number="9"><span class="ot">    fst ::</span> <span class="dt">Product</span> a b <span class="ot">-&gt;</span> a</a>
<a class="sourceLine" id="cb2-10" data-line-number="10">    fst p <span class="fu">=</span> p <span class="fu">$</span> \a b <span class="ot">-&gt;</span> a</a>
<a class="sourceLine" id="cb2-11" data-line-number="11"><span class="ot">    snd ::</span> <span class="dt">Product</span> a b <span class="ot">-&gt;</span> b</a>
<a class="sourceLine" id="cb2-12" data-line-number="12">    snd p <span class="fu">=</span> p <span class="fu">$</span> \a b <span class="ot">-&gt;</span> b</a></code></pre></div>
<p>We can do <code>Either</code> as well</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb3-1" data-line-number="1">    <span class="co">-- We&#39;ll call Either a &quot;sum&quot; because it</span></a>
<a class="sourceLine" id="cb3-2" data-line-number="2">    <span class="co">-- looks like the disjoint union (sum) of two types</span></a>
<a class="sourceLine" id="cb3-3" data-line-number="3">    <span class="kw">type</span> <span class="dt">Sum</span> a b <span class="fu">=</span> forall c<span class="fu">.</span> (a <span class="ot">-&gt;</span> c) <span class="ot">-&gt;</span> (b <span class="ot">-&gt;</span> c) <span class="ot">-&gt;</span> c</a>
<a class="sourceLine" id="cb3-4" data-line-number="4"><span class="ot">    inl ::</span> a <span class="ot">-&gt;</span> <span class="dt">Sum</span> a b</a>
<a class="sourceLine" id="cb3-5" data-line-number="5">    inl a <span class="fu">=</span> \l r <span class="ot">-&gt;</span> l a</a>
<a class="sourceLine" id="cb3-6" data-line-number="6"></a>
<a class="sourceLine" id="cb3-7" data-line-number="7"><span class="ot">    inr ::</span> b <span class="ot">-&gt;</span> <span class="dt">Sum</span> a b</a>
<a class="sourceLine" id="cb3-8" data-line-number="8">    inr b <span class="fu">=</span> \l r <span class="ot">-&gt;</span> r b</a></code></pre></div>
<p>Look familiar? That’s just <code>Data.Either.either</code>! Now if we squint at these we can imagine building up more complex types from these building blocks</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">AFew</span> <span class="fu">=</span> <span class="dt">NoVal</span> <span class="fu">|</span> <span class="dt">OneVal</span> <span class="dt">Int</span> <span class="fu">|</span> <span class="dt">ThrVal</span> <span class="dt">Int</span> <span class="dt">Bool</span> <span class="dt">String</span></a>
<a class="sourceLine" id="cb4-2" data-line-number="2"></a>
<a class="sourceLine" id="cb4-3" data-line-number="3">    <span class="kw">type</span> <span class="dt">ChurchAFew</span> <span class="fu">=</span></a>
<a class="sourceLine" id="cb4-4" data-line-number="4">      <span class="dt">Sum</span> () (<span class="dt">Sum</span> <span class="dt">Int</span> (<span class="dt">Prod</span> <span class="dt">Int</span> (<span class="dt">Prod</span> <span class="dt">Bool</span> <span class="dt">String</span>)))</a>
<a class="sourceLine" id="cb4-5" data-line-number="5"></a>
<a class="sourceLine" id="cb4-6" data-line-number="6"><span class="ot">    noVal ::</span> <span class="dt">ChurchAFew</span></a>
<a class="sourceLine" id="cb4-7" data-line-number="7">    noVal <span class="fu">=</span> inl ()</a>
<a class="sourceLine" id="cb4-8" data-line-number="8"></a>
<a class="sourceLine" id="cb4-9" data-line-number="9"><span class="ot">    oneVal ::</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">ChurchAFew</span></a>
<a class="sourceLine" id="cb4-10" data-line-number="10">    oneVal i <span class="fu">=</span> inr (inl i)</a>
<a class="sourceLine" id="cb4-11" data-line-number="11"></a>
<a class="sourceLine" id="cb4-12" data-line-number="12"><span class="ot">    thrVal ::</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">Bool</span> <span class="ot">-&gt;</span> <span class="dt">String</span> <span class="ot">-&gt;</span> <span class="dt">ChurchAFew</span></a>
<a class="sourceLine" id="cb4-13" data-line-number="13">    thrVal i b s <span class="fu">=</span> inr <span class="fu">.</span> inr <span class="fu">.</span> pair i <span class="fu">$</span> pair b s</a></code></pre></div>
<p>And now pattern matching more or less falls out for free from <code>ChurchAFew</code>. Since it’s a function we transform</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb5-1" data-line-number="1">    <span class="kw">case</span> foo <span class="kw">of</span></a>
<a class="sourceLine" id="cb5-2" data-line-number="2">      <span class="dt">NoVal</span>        <span class="ot">-&gt;</span> <span class="fu">...</span></a>
<a class="sourceLine" id="cb5-3" data-line-number="3">      <span class="dt">OneVal</span> i     <span class="ot">-&gt;</span> <span class="fu">...</span></a>
<a class="sourceLine" id="cb5-4" data-line-number="4">      <span class="dt">ThrVal</span> i b s <span class="ot">-&gt;</span> <span class="fu">...</span></a>
<a class="sourceLine" id="cb5-5" data-line-number="5">    <span class="co">-- becomes</span></a>
<a class="sourceLine" id="cb5-6" data-line-number="6">    foo (\() <span class="ot">-&gt;</span> <span class="fu">...</span>) (\p <span class="ot">-&gt;</span> p (\i <span class="ot">-&gt;</span> <span class="fu">...</span>) (\p1 <span class="ot">-&gt;</span> p1 (\i p2 <span class="ot">-&gt;</span> <span class="fu">...</span>)))</a></code></pre></div>
<p>Such is the power of lambda! And there are all sorts of fun side effects of pattern matching being a function, most of it to do with nicer composition with point-free functions.</p>
<h2 id="but-its-boring">But It’s Boring!</h2>
<p>There’s a drawback here, boilerplate! We have to essentially duplicate all our data declarations, extra boilerplate for generating accessors, and then two functions to map back and forth between our representations.</p>
<p>As a programmer I’m far too lazy to write all that!</p>
<p>Whenever we start to think of terms of sum and product types it’s time to turn to GHC.Generics. It’s a library that provides a type class to reify our normal types into explicit sums and products and back again.</p>
<p>The first thing we have to do is write a type level function to reify a GHC.Generics representation to the appropriate type.</p>
<p>For example <code>Maybe Int</code> has the following representation</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb6-1" data-line-number="1">    <span class="dt">M1</span></a>
<a class="sourceLine" id="cb6-2" data-line-number="2">        <span class="dt">D</span></a>
<a class="sourceLine" id="cb6-3" data-line-number="3">        <span class="dt">GHC.Generics.D1Maybe</span></a>
<a class="sourceLine" id="cb6-4" data-line-number="4">        (<span class="dt">M1</span> <span class="dt">C</span> <span class="dt">GHC.Generics.C1_0Maybe</span> <span class="dt">U1</span></a>
<a class="sourceLine" id="cb6-5" data-line-number="5">         <span class="fu">:+:</span> <span class="dt">M1</span> <span class="dt">C</span> <span class="dt">GHC.Generics.C1_1Maybe</span> (<span class="dt">M1</span> <span class="dt">S</span> <span class="dt">NoSelector</span> (<span class="dt">K1</span> <span class="dt">R</span> <span class="dt">Int</span>)))</a></code></pre></div>
<p>We can strip out all the <code>M1</code> meta information since we don’t really care leaving</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb7-1" data-line-number="1">    <span class="dt">U1</span> <span class="fu">:+:</span> <span class="dt">K1</span> <span class="dt">R</span> <span class="dt">Int</span></a></code></pre></div>
<p>Not so bad! Let’s start by writing a type level function (type family) to get rid the <code>M1</code> constructors</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb8-1" data-line-number="1">    <span class="ot">{-# LANGUAGE TypeFamilies, TypeOperators, UndecidableInstances, RankNTypes #-}</span></a>
<a class="sourceLine" id="cb8-2" data-line-number="2"></a>
<a class="sourceLine" id="cb8-3" data-line-number="3">    <span class="kw">import</span> <span class="dt">GHC.Generics</span></a>
<a class="sourceLine" id="cb8-4" data-line-number="4"></a>
<a class="sourceLine" id="cb8-5" data-line-number="5">    <span class="co">-- | Remove the extra `p` parameter that GHC.Generics</span></a>
<a class="sourceLine" id="cb8-6" data-line-number="6">    <span class="co">-- lugs through every constructor</span></a>
<a class="sourceLine" id="cb8-7" data-line-number="7">    <span class="kw">type</span> family <span class="dt">WithoutParam</span><span class="ot"> v ::</span> <span class="fu">*</span> <span class="ot">-&gt;</span> <span class="fu">*</span></a>
<a class="sourceLine" id="cb8-8" data-line-number="8">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">WithoutParam</span> ((<span class="fu">:+:</span>) l r p) <span class="fu">=</span> l <span class="fu">:+:</span> r</a>
<a class="sourceLine" id="cb8-9" data-line-number="9">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">WithoutParam</span> ((<span class="fu">:*:</span>) l r p) <span class="fu">=</span> l <span class="fu">:*:</span> r</a>
<a class="sourceLine" id="cb8-10" data-line-number="10">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">WithoutParam</span> (<span class="dt">U1</span> p)        <span class="fu">=</span> <span class="dt">U1</span></a>
<a class="sourceLine" id="cb8-11" data-line-number="11">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">WithoutParam</span> (<span class="dt">K1</span> a t p)    <span class="fu">=</span> <span class="dt">K1</span> a t</a>
<a class="sourceLine" id="cb8-12" data-line-number="12"></a>
<a class="sourceLine" id="cb8-13" data-line-number="13">    <span class="co">-- | Strip out `M1` tags</span></a>
<a class="sourceLine" id="cb8-14" data-line-number="14">    <span class="kw">type</span> family <span class="dt">StripMeta</span><span class="ot"> v ::</span> <span class="fu">*</span></a>
<a class="sourceLine" id="cb8-15" data-line-number="15">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">StripMeta</span> (<span class="dt">M1</span> a b f p)  <span class="fu">=</span> <span class="dt">StripMeta</span> (f p)</a>
<a class="sourceLine" id="cb8-16" data-line-number="16">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">StripMeta</span> (<span class="dt">K1</span> a t p)    <span class="fu">=</span> <span class="dt">K1</span> a t p</a>
<a class="sourceLine" id="cb8-17" data-line-number="17">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">StripMeta</span> ((<span class="fu">:+:</span>) l r p) <span class="fu">=</span></a>
<a class="sourceLine" id="cb8-18" data-line-number="18">      (<span class="fu">:+:</span>) (<span class="dt">WithoutParam</span> (<span class="dt">StripMeta</span> (l p))) (<span class="dt">WithoutParam</span> (<span class="dt">StripMeta</span> (r p))) p</a>
<a class="sourceLine" id="cb8-19" data-line-number="19">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">StripMeta</span> ((<span class="fu">:*:</span>) l r p) <span class="fu">=</span></a>
<a class="sourceLine" id="cb8-20" data-line-number="20">      (<span class="fu">:*:</span>) (<span class="dt">WithoutParam</span> (<span class="dt">StripMeta</span> (l p))) (<span class="dt">WithoutParam</span> (<span class="dt">StripMeta</span> (r p))) p</a>
<a class="sourceLine" id="cb8-21" data-line-number="21">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">StripMeta</span> (<span class="dt">U1</span> p)        <span class="fu">=</span> <span class="dt">U1</span> p</a></code></pre></div>
<p>As we can see these type families are well.. pretty terrible. But they work! Next we can actually do some real work. We need to take a product type with one or more members and turn it into a function.</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb9-1" data-line-number="1">    <span class="kw">type</span> family <span class="dt">ChurchProd</span> v<span class="ot"> c ::</span> <span class="fu">*</span></a>
<a class="sourceLine" id="cb9-2" data-line-number="2">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">ChurchProd</span> (<span class="dt">K1</span> a t p) c    <span class="fu">=</span> t <span class="ot">-&gt;</span> c</a>
<a class="sourceLine" id="cb9-3" data-line-number="3">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">ChurchProd</span> (<span class="dt">U1</span> p)     c    <span class="fu">=</span> () <span class="ot">-&gt;</span> c</a>
<a class="sourceLine" id="cb9-4" data-line-number="4">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">ChurchProd</span> ((<span class="fu">:*:</span>) l r p) c <span class="fu">=</span></a>
<a class="sourceLine" id="cb9-5" data-line-number="5">      <span class="dt">ChurchProd</span> (l p) (<span class="dt">ChurchProd</span> (r p) c)</a></code></pre></div>
<p>So here we have a type family with two parameters, the term and the “out” type. These take <code>a :*: b :*: c</code> to <code>a -&gt; b -&gt; c -&gt; ...</code>. This is important because GHC.Generics represents things like a list where the <code>(:)</code> equivalent is <code>:+:</code> and the each leaf is product or unit type.</p>
<p>Now at least we can run</p>
<pre><code>&gt; :kind! ChurchProd (StripMeta (Rep (Int, Bool) ())) Char
ChurchProd (StripMeta (Rep (Int, Bool) ())) Char :: *
= Int -&gt; Bool -&gt; Char</code></pre>
<p>As it happens, I told a small fib, GHC.Generics doesn’t make quite a list. In fact it makes a tree! We can rejigger things to a list though</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb11-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">ListTerm</span> p <span class="co">-- The list terminator</span></a>
<a class="sourceLine" id="cb11-2" data-line-number="2"></a>
<a class="sourceLine" id="cb11-3" data-line-number="3">    <span class="kw">type</span> family <span class="dt">ToList</span> v<span class="ot"> rest ::</span> <span class="fu">*</span></a>
<a class="sourceLine" id="cb11-4" data-line-number="4">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">ToList</span> ((<span class="fu">:+:</span>) l r&#39; p) r <span class="fu">=</span> <span class="dt">ToList</span> (l p) (<span class="dt">ToList</span> (r&#39; p) r)</a>
<a class="sourceLine" id="cb11-5" data-line-number="5">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">ToList</span> (<span class="dt">K1</span> a t p)     r <span class="fu">=</span> (<span class="dt">K1</span> a t     <span class="fu">:+:</span> <span class="dt">WithoutParam</span> r) p</a>
<a class="sourceLine" id="cb11-6" data-line-number="6">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">ToList</span> ((<span class="fu">:*:</span>) l r&#39; p) r <span class="fu">=</span> ((l <span class="fu">:*:</span> r&#39;) <span class="fu">:+:</span> <span class="dt">WithoutParam</span> r) p</a>
<a class="sourceLine" id="cb11-7" data-line-number="7">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">ToList</span> (<span class="dt">U1</span> p)         r <span class="fu">=</span> (<span class="dt">U1</span>         <span class="fu">:+:</span> <span class="dt">WithoutParam</span> r) p</a></code></pre></div>
<p>Now the final piece, we need to write a function which “folds” over a tree of <code>:+:</code> and produces a function <code>(a -&gt; c) -&gt; (b -&gt; c) -&gt; ... -&gt; c</code></p>
<div class="sourceCode" id="cb12"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb12-1" data-line-number="1">    <span class="kw">type</span> family <span class="dt">ChurchSum</span> v<span class="ot"> c ::</span> <span class="fu">*</span></a>
<a class="sourceLine" id="cb12-2" data-line-number="2">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">ChurchSum</span> ((<span class="fu">:+:</span>) l r p) c <span class="fu">=</span> <span class="dt">ChurchProd</span> (l p) c <span class="ot">-&gt;</span> <span class="dt">ChurchSum</span> (r p) c</a>
<a class="sourceLine" id="cb12-3" data-line-number="3">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">ChurchSum</span> (<span class="dt">ListTerm</span> p) c <span class="fu">=</span> c</a>
<a class="sourceLine" id="cb12-4" data-line-number="4"></a>
<a class="sourceLine" id="cb12-5" data-line-number="5"></a>
<a class="sourceLine" id="cb12-6" data-line-number="6">    <span class="co">-- A driver type for the whole thing</span></a>
<a class="sourceLine" id="cb12-7" data-line-number="7">    <span class="kw">type</span> <span class="dt">Church</span> t <span class="fu">=</span> forall c<span class="fu">.</span> <span class="dt">ChurchSum</span> (<span class="dt">ToList</span> (<span class="dt">StripMeta</span> (<span class="dt">Rep</span> t ())) (<span class="dt">ListTerm</span> ())) c</a></code></pre></div>
<p>And there we go! As a quick test</p>
<div class="sourceCode" id="cb13"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb13-1" data-line-number="1">    <span class="ot">{-# LANGUAGE DeriveGeneric #-}</span></a>
<a class="sourceLine" id="cb13-2" data-line-number="2">    <span class="kw">data</span> <span class="dt">AFew</span> <span class="fu">=</span> <span class="dt">S1</span> <span class="dt">Int</span> <span class="dt">Int</span> <span class="dt">Int</span> <span class="fu">|</span> <span class="dt">S2</span> <span class="dt">Bool</span> <span class="dt">Char</span> <span class="fu">|</span> <span class="dt">S3</span> <span class="dt">String</span> <span class="fu">|</span> <span class="dt">S4</span></a>
<a class="sourceLine" id="cb13-3" data-line-number="3">              <span class="kw">deriving</span> <span class="dt">Generic</span></a></code></pre></div>
<p>And now</p>
<pre><code>&gt; kind! Church AFew
(Int -&gt; Int -&gt; Int -&gt; c)
  -&gt; (Bool -&gt; Char -&gt; c)
  -&gt; ([Char] -&gt; c)
  -&gt; (() -&gt; c)
  -&gt; c</code></pre>
<p>Tada! Now we’ve automated the generation of types for church representations. In the next post we’ll actually go about populating them.</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Thu, 06 Mar 2014 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2014-03-06-church.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Types and Kinds and Sorts, Oh My!</title>
    <link>http://jozefg.bitbucket.org/posts/2014-02-10-types-kinds-and-sorts.html</link>
    <description><![CDATA[<div class="info">
    Posted on February 10, 2014
    
</div>
<div class="info">
    
    Tags: <a href="/tags/haskell.html">haskell</a>, <a href="/tags/types.html">types</a>
    
</div>

<p>One subject that most introductory Haskell books fail to address is kinds. This means that when most intermediate Haskellers start looking at Haskell extensions they’re flummoxed by <code>DataKinds</code>.</p>
<p>This post aims to introduce intermediate haskellers to kinds and sorts as well as how they enter into the world of Haskell programming.</p>
<p>Think about an expression in Haskell, if it’s well formed, then we can assign it a type: <code>1 :: Int</code>, <code>&quot;foo&quot; :: String</code>, and <code>Just () :: Maybe ()</code> for example.</p>
<p>Now this has all sorts of lovely benefits like ensuring we can’t write insane expressions like <code>&quot;foo&quot; + 2</code>. However, none of those benefits seem to extend to the types themselves.</p>
<p>We have functions at the type level, consider</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb1-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">Cons</span> a b <span class="fu">=</span> <span class="dt">Pair</span> a b</a></code></pre></div>
<p><code>Cons</code> looks like the type level equivalent of <code>Pair</code>. But how do we ensure that these actually work? What if we wrote</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb2-1" data-line-number="1"><span class="ot">    foo ::</span> <span class="dt">Cons</span> <span class="dt">Maybe</span> <span class="dt">Either</span></a>
<a class="sourceLine" id="cb2-2" data-line-number="2">    foo <span class="fu">=</span> <span class="fu">???</span></a></code></pre></div>
<p>This makes no sense however, there is no value whose type is <code>Maybe</code>.</p>
<p>This hints that we want something corresponding to a type system at the type level. Something to ensure that all the types we write make some sort of sense. For example, let’s call the “type” of types things values occupy, <code>*</code>. So <code>Int :: *</code>, as well as <code>String</code>, <code>[a]</code>, and many others. Now it seems that <code>Cons</code> takes in two types, <code>a :: *</code> and <code>b :: *</code>, and returns another type, <code>Cons a b :: *</code>. This is notated <code>* -&gt; * -&gt; *</code>.</p>
<p>Now let’s rattle off some other examples</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb3-1" data-line-number="1">    <span class="dt">Maybe</span><span class="ot">  ::</span> <span class="fu">*</span> <span class="ot">-&gt;</span> <span class="fu">*</span>      <span class="co">-- Maybe takes a type and returns another</span></a>
<a class="sourceLine" id="cb3-2" data-line-number="2">    <span class="dt">Either</span><span class="ot"> ::</span> <span class="fu">*</span> <span class="ot">-&gt;</span> <span class="fu">*</span> <span class="ot">-&gt;</span> <span class="fu">*</span> <span class="co">-- Either takes two types and returns another</span></a>
<a class="sourceLine" id="cb3-3" data-line-number="3">    <span class="dt">StateT</span><span class="ot"> ::</span> (<span class="fu">*</span> <span class="ot">-&gt;</span> <span class="fu">*</span>) <span class="ot">-&gt;</span> <span class="fu">*</span> <span class="ot">-&gt;</span> <span class="fu">*</span></a></code></pre></div>
<p>Notice that there’s something interesting about <code>StateT</code>, it takes a <em>function</em> of type onto type. It’s a type level parallel of a higher order function!</p>
<p>This is what <em>kinds</em> are all about, kinds are the type of types! Indeed, Haskell even notates the kind of types that values occupy as <code>*</code> as well. We can read something like <code>Int :: *</code> as <em><code>Int</code> has the kind <code>*</code></em>.</p>
<p><code>StateT</code> is what’s called a higher <em>kinded</em> type, it’s the type level version of higher order functions.</p>
<p>Now the step is to ask, what’s the “type” of a kind? <code>* :: ???</code> the answer is, a <em>sort</em>. However, Haskell doesn’t talk of sorts and conceptually only has one, <code>BOX</code>. It is occasionally helpful to think as if <code>BOX</code> existed, but we can’t actually state this in Haskell. This means that while the sorts <em>conceptually exist</em>, we can’t really do much of anything with them. Perhaps in the future Haskell will grow a few more extensions to enable talking about sorts, until then though, we’ll focus on kinds.</p>
<p>Now back to kinds, what does Haskell have in terms of a kind system</p>
<ul>
<li>By default, we have two kind constructors, <code>(-&gt;)</code> and <code>*</code>.</li>
<li>With <code>-XKindSignatures</code> we can actually utter kinds, eg <code>a :: *</code>.</li>
<li>With <code>-XDataKinds</code> we can define our own kinds just like we can with types.</li>
<li>With <code>-XTypeFamilies</code> we can write type level functions.</li>
<li>With <code>-XPolyKinds</code> we have parametric polymorphism at the kind level.</li>
</ul>
<h3 id="data-kinds">Data Kinds</h3>
<p>The motiviation for <code>DataKind</code>s is that Haskell’s vanilla kind system is well… boring. It doesn’t really help us since we can’t write our own kinds and types to occupy these kinds.</p>
<p>Let’s take a simple example using GADTs and red-black binary trees. For the sake of brevity I’ll leave the reader to take a moment and <a href="http://www.haskell.org/haskellwiki/GADTs_for_dummies">learn</a> about GADTs if necessary.</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" data-line-number="1">    <span class="ot">{-# LANGUAGE KindSignatures, GADTs, EmptyDataDecls #-}</span></a>
<a class="sourceLine" id="cb4-2" data-line-number="2">    <span class="co">{- No DataKinds -}</span></a>
<a class="sourceLine" id="cb4-3" data-line-number="3"></a>
<a class="sourceLine" id="cb4-4" data-line-number="4">    <span class="kw">data</span> <span class="dt">Black</span> <span class="co">-- This is what EmptyDataDecls allows,</span></a>
<a class="sourceLine" id="cb4-5" data-line-number="5">    <span class="kw">data</span> <span class="dt">Red</span>   <span class="co">-- types with no constructors</span></a>
<a class="sourceLine" id="cb4-6" data-line-number="6"></a>
<a class="sourceLine" id="cb4-7" data-line-number="7">    <span class="kw">data</span> <span class="dt">Tree</span><span class="ot"> ::</span> <span class="fu">*</span> <span class="ot">-&gt;</span> <span class="fu">*</span> <span class="ot">-&gt;</span> <span class="fu">*</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb4-8" data-line-number="8">      <span class="dt">Leaf</span><span class="ot">  ::</span> <span class="dt">Tree</span> a <span class="dt">Black</span></a>
<a class="sourceLine" id="cb4-9" data-line-number="9">      <span class="dt">NodeR</span><span class="ot"> ::</span> a <span class="ot">-&gt;</span> <span class="dt">Tree</span> a <span class="dt">Black</span> <span class="ot">-&gt;</span> <span class="dt">Tree</span> a <span class="dt">Black</span> <span class="ot">-&gt;</span> <span class="dt">Tree</span> a <span class="dt">Red</span></a>
<a class="sourceLine" id="cb4-10" data-line-number="10">      <span class="dt">NodeB</span><span class="ot"> ::</span> a <span class="ot">-&gt;</span> <span class="dt">Tree</span> a c     <span class="ot">-&gt;</span> <span class="dt">Tree</span> a c&#39;    <span class="ot">-&gt;</span> <span class="dt">Tree</span> a <span class="dt">Black</span></a></code></pre></div>
<p>Here we’re attempting to model the fact that in a red-black binary tree, a red node has black children and a black node has either red or black children.</p>
<p>However this doesn’t model it correctly, we’d like to make it impossible to state nonsense like</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb5-1" data-line-number="1"><span class="ot">    crazy ::</span> <span class="dt">Tree</span> a <span class="dt">Int</span></a>
<a class="sourceLine" id="cb5-2" data-line-number="2">    crazy <span class="fu">=</span> undefined</a></code></pre></div>
<p>The problem here is that the kind of <code>Tree</code> is <code>* -&gt; * -&gt; *</code>. We don’t really mean that a tree be colored by any type of kind <code>*</code>! We really want to limit it so that we can only color a tree with <code>Red</code> and <code>Black</code>.</p>
<p>Enter <code>DataKinds</code></p>
<div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb6-1" data-line-number="1">    <span class="ot">{-# LANGUAGE KindSignatures, DataKinds, GADTs #-}</span></a>
<a class="sourceLine" id="cb6-2" data-line-number="2"></a>
<a class="sourceLine" id="cb6-3" data-line-number="3">    <span class="kw">data</span> <span class="dt">Color</span> <span class="fu">=</span> <span class="dt">Red</span> <span class="fu">|</span> <span class="dt">Black</span></a>
<a class="sourceLine" id="cb6-4" data-line-number="4"></a>
<a class="sourceLine" id="cb6-5" data-line-number="5">    <span class="kw">data</span> <span class="dt">Tree</span><span class="ot"> ::</span> <span class="fu">*</span> <span class="ot">-&gt;</span> <span class="dt">Color</span> <span class="ot">-&gt;</span> <span class="fu">*</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb6-6" data-line-number="6">      <span class="dt">Leaf</span><span class="ot">  ::</span> <span class="dt">Tree</span> a <span class="dt">Black</span></a>
<a class="sourceLine" id="cb6-7" data-line-number="7">      <span class="dt">NodeR</span><span class="ot"> ::</span> a <span class="ot">-&gt;</span> <span class="dt">Tree</span> a <span class="dt">Black</span> <span class="ot">-&gt;</span> <span class="dt">Tree</span> a <span class="dt">Black</span> <span class="ot">-&gt;</span> <span class="dt">Tree</span> a <span class="dt">Red</span></a>
<a class="sourceLine" id="cb6-8" data-line-number="8">      <span class="dt">NodeB</span><span class="ot"> ::</span> a <span class="ot">-&gt;</span> <span class="dt">Tree</span> a c     <span class="ot">-&gt;</span> <span class="dt">Tree</span> a c&#39;    <span class="ot">-&gt;</span> <span class="dt">Tree</span> a <span class="dt">Black</span></a></code></pre></div>
<p>Now if we attempted</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb7-1" data-line-number="1"><span class="ot">    foo ::</span> <span class="dt">Tree</span> a <span class="dt">Int</span></a>
<a class="sourceLine" id="cb7-2" data-line-number="2">    foo <span class="fu">=</span> undefined</a></code></pre></div>
<p>We’ll get a kind error! This is a simple example of how we can leverage the kind system to rule out illegal programs.</p>
<p>Let’s attempt to encode a more complex property, that there are exactly the same number of black nodes below every node. To start we’ll need a type level encoding of numbers</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb8-1" data-line-number="1"><span class="kw">data</span> <span class="dt">Nat</span> <span class="fu">=</span> <span class="dt">Z</span> <span class="fu">|</span> <span class="dt">S</span> <span class="dt">Nat</span></a></code></pre></div>
<p>These are called peano numbers, <code>Z</code> is zero and <code>S</code> is equivalent to <code>+1</code>, so 2 is <code>S (S Z)</code>. Now we can integrate these into our tree.</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb9-1" data-line-number="1">    <span class="ot">{-# LANGUAGE KindSignatures, DataKinds, GADTs #-}</span></a>
<a class="sourceLine" id="cb9-2" data-line-number="2">    <span class="kw">data</span> <span class="dt">Tree</span><span class="ot"> ::</span> <span class="fu">*</span> <span class="ot">-&gt;</span> <span class="dt">Color</span> <span class="ot">-&gt;</span> <span class="dt">Nat</span> <span class="ot">-&gt;</span> <span class="fu">*</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb9-3" data-line-number="3">      <span class="dt">Leaf</span><span class="ot">  ::</span> <span class="dt">Tree</span> a <span class="dt">Black</span> <span class="dt">Z</span></a>
<a class="sourceLine" id="cb9-4" data-line-number="4">      <span class="dt">NodeR</span><span class="ot"> ::</span> a <span class="ot">-&gt;</span> <span class="dt">Tree</span> a <span class="dt">Black</span> n <span class="ot">-&gt;</span> <span class="dt">Tree</span> a <span class="dt">Black</span> n <span class="ot">-&gt;</span> <span class="dt">Tree</span> a <span class="dt">Red</span>   n</a>
<a class="sourceLine" id="cb9-5" data-line-number="5">      <span class="dt">NodeB</span><span class="ot"> ::</span> a <span class="ot">-&gt;</span> <span class="dt">Tree</span> a c n     <span class="ot">-&gt;</span> <span class="dt">Tree</span> a c&#39; n    <span class="ot">-&gt;</span> <span class="dt">Tree</span> a <span class="dt">Black</span> (<span class="dt">S</span> n)</a></code></pre></div>
<p>Taking a moment to examine this, we see that a <code>Leaf</code> has 0 black nodes below it, which makes perfect sense. Then nodes take two trees of identical height and either adds one to the height if the node is black or leaves it the same.</p>
<p>Now if we attempted to create an unbalanced tree</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb10-1" data-line-number="1">    unbalanced <span class="fu">=</span> <span class="dt">NodeB</span> () <span class="dt">Leaf</span> (<span class="dt">NodeB</span> () <span class="dt">Leaf</span> <span class="dt">Leaf</span>)</a></code></pre></div>
<p>We get a type error!</p>
<p>Hopefully this clears up what kinds are and how we can leverage them to statically check some properties of our programs.</p>
<p>For the curious reader I encourage you to look at <code>PolyKinds</code> and <code>TypeFamilies</code>, these let you express some very sophisticated programs at the type level in Haskell. If this really tickles your fancy, perhaps make the leap to Agda, Idris, or Coq to enjoy full dependent types.</p>
<p><em>Thanks to GlenH7 and JimmyHoffa on thewhiteboard and byorgey on #haskell for proof reading</em></p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Mon, 10 Feb 2014 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2014-02-10-types-kinds-and-sorts.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Optimizing a Trie</title>
    <link>http://jozefg.bitbucket.org/posts/2014-01-28-trie.html</link>
    <description><![CDATA[<div class="info">
    Posted on January 28, 2014
    
</div>
<div class="info">
    
    Tags: <a href="/tags/haskell.html">haskell</a>
    
</div>

<p>The other day I stumbled across some of old code for a trie. Being the hardworking and responsible student that I am, I immediately dropped all my homework to try and squeeze some performance out of it.</p>
<p>The code I ended up with was something like</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb1-1" data-line-number="1">    <span class="ot">{-# LANGUAGE ViewPatterns, OverloadedStrings #-}</span></a>
<a class="sourceLine" id="cb1-2" data-line-number="2">    <span class="kw">import</span> <span class="dt">Data.List</span></a>
<a class="sourceLine" id="cb1-3" data-line-number="3">    <span class="kw">import</span> <span class="dt">Data.Maybe</span></a>
<a class="sourceLine" id="cb1-4" data-line-number="4">    <span class="kw">import</span> <span class="kw">qualified</span> <span class="dt">Data.Map.Strict</span> <span class="kw">as</span> <span class="dt">M</span></a>
<a class="sourceLine" id="cb1-5" data-line-number="5">    <span class="kw">import</span> <span class="dt">Data.Map.Strict</span> ((!))</a>
<a class="sourceLine" id="cb1-6" data-line-number="6">    <span class="kw">import</span> <span class="kw">qualified</span> <span class="dt">Data.ByteString.Char8</span> <span class="kw">as</span> <span class="dt">B</span></a>
<a class="sourceLine" id="cb1-7" data-line-number="7"></a>
<a class="sourceLine" id="cb1-8" data-line-number="8">    <span class="kw">newtype</span> <span class="dt">Trie</span> <span class="fu">=</span> <span class="dt">Node</span> {<span class="ot">subs ::</span> <span class="dt">M.Map</span> <span class="dt">Char</span> <span class="dt">Trie</span>}</a>
<a class="sourceLine" id="cb1-9" data-line-number="9">      <span class="kw">deriving</span> (<span class="dt">Eq</span>, <span class="dt">Show</span>, <span class="dt">Ord</span>)</a>
<a class="sourceLine" id="cb1-10" data-line-number="10"></a>
<a class="sourceLine" id="cb1-11" data-line-number="11"><span class="ot">    add ::</span> <span class="dt">B.ByteString</span> <span class="ot">-&gt;</span> <span class="dt">Trie</span> <span class="ot">-&gt;</span> <span class="dt">Trie</span></a>
<a class="sourceLine" id="cb1-12" data-line-number="12">    add (B.uncons <span class="ot">-&gt;</span> <span class="dt">Just</span> (c, s)) (<span class="dt">Node</span> subs) <span class="fu">=</span> <span class="dt">Node</span></a>
<a class="sourceLine" id="cb1-13" data-line-number="13">                                               <span class="fu">.</span> with</a>
<a class="sourceLine" id="cb1-14" data-line-number="14">                                               <span class="fu">.</span> maybe (<span class="dt">Node</span> M.empty) id</a>
<a class="sourceLine" id="cb1-15" data-line-number="15">                                               <span class="fu">$</span> M.lookup c subs</a>
<a class="sourceLine" id="cb1-16" data-line-number="16">      <span class="kw">where</span> with next <span class="fu">=</span> M.insert c (add s next) subs</a>
<a class="sourceLine" id="cb1-17" data-line-number="17"></a>
<a class="sourceLine" id="cb1-18" data-line-number="18">    add _ trie <span class="fu">=</span> trie</a>
<a class="sourceLine" id="cb1-19" data-line-number="19"></a>
<a class="sourceLine" id="cb1-20" data-line-number="20"><span class="ot">    build ::</span> [<span class="dt">B.ByteString</span>] <span class="ot">-&gt;</span> <span class="dt">Trie</span></a>
<a class="sourceLine" id="cb1-21" data-line-number="21">    build <span class="fu">=</span> foldl&#39; (flip add) (<span class="dt">Node</span> M.empty)</a>
<a class="sourceLine" id="cb1-22" data-line-number="22"></a>
<a class="sourceLine" id="cb1-23" data-line-number="23"><span class="ot">    contains ::</span> <span class="dt">B.ByteString</span> <span class="ot">-&gt;</span> <span class="dt">Trie</span> <span class="ot">-&gt;</span> <span class="dt">Bool</span></a>
<a class="sourceLine" id="cb1-24" data-line-number="24">    contains (B.uncons <span class="ot">-&gt;</span> <span class="dt">Just</span> (c, s)) (<span class="dt">Node</span> subs) <span class="fu">=</span> M.member c subs <span class="fu">&amp;&amp;</span> contains s next</a>
<a class="sourceLine" id="cb1-25" data-line-number="25">      <span class="kw">where</span> next <span class="fu">=</span> subs <span class="fu">!</span> c</a>
<a class="sourceLine" id="cb1-26" data-line-number="26">    contains _ _ <span class="fu">=</span> <span class="dt">True</span></a></code></pre></div>
<p>and so I decided to test it with a simple main</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb2-1" data-line-number="1">    main <span class="fu">=</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb2-2" data-line-number="2">      trie <span class="ot">&lt;-</span> (build <span class="fu">.</span> B.lines) <span class="ot">`fmap`</span> B.readFile <span class="st">&quot;/usr/share/dict/words&quot;</span></a>
<a class="sourceLine" id="cb2-3" data-line-number="3">      print <span class="fu">$</span> contains <span class="st">&quot;zebra&quot;</span> trie</a></code></pre></div>
<p>and compiled it with the standard optimization flags</p>
<pre><code>&gt; ghc -O2 -fllvm trie.hs
&gt; time ./trie
True

real 0m0.964s
user 0m0.896s
sys  0m0.059s</code></pre>
<p>So pretty reasonably fast for about 500k words. Then to see if I could kick this in the teeth by forcing the entire trie I ran</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" data-line-number="1">    main <span class="fu">=</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb4-2" data-line-number="2">      words <span class="ot">&lt;-</span> B.lines <span class="ot">`fmap`</span> B.readFile <span class="st">&quot;/usr/share/dict/words&quot;</span></a>
<a class="sourceLine" id="cb4-3" data-line-number="3">      print <span class="fu">$</span> all (flip contains <span class="fu">$</span> build words) words</a></code></pre></div>
<p>And again I ran it</p>
<pre><code>&gt; ghc -O2 -fllvm trie.hs
&gt; time ./trie
True

real 0m1.557s
user 0m1.427s
sys  0m0.119s</code></pre>
<p>Now this is weird. It seems that such building the trie is such a huge bottleneck that the building it takes around .9 seconds and querying it 500k times is only .6 seconds.</p>
<p>Now the next logical step was to see if maybe this was due to a build up of garbage that was dominating my time. A quick run with <code>+RTS -s</code> gives tells me that about 36% of my time is GC on both. This isn’t shocking since I’m building up a ton of data over a relatively short period of time.</p>
<p>So it’s not GC, and both operations are obviously <code>O(n)</code> (remembering that each map has 256 or less entries). So the only difference between them is that <code>add</code> allocates memory, a new map, and <code>contains</code> doesn’t. So let’s confirm our suspicions with a quick round of profiling.</p>
<pre><code> &gt; ghc -O2 -fllvm -prof -auto-all trie.hs
 &gt; ./trie</code></pre>
<p>generates <code>trie.prof</code> with the profiling information for our runs.</p>
<p>They both inform us that about 97% of time and memory is spent in <code>add</code>. Now this seems odd, since our time difference between the runs was significant. I hypothesize that the reason for this is that when we make 500k queries we’re forcing the entire trie which is lazy. Indeed looking out the output from <code>+RTS -s</code> confirms that the 500k output allocates 1.69 mb of memory while only one query allocates 80 mb of memory.</p>
<p>So the bottleneck here is that allocation is slow as a dog. More to follow on optimizing this.</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Tue, 28 Jan 2014 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2014-01-28-trie.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Faking Existentials with Rank N Types</title>
    <link>http://jozefg.bitbucket.org/posts/2014-01-14-existential.html</link>
    <description><![CDATA[<div class="info">
    Posted on January 14, 2014
    
</div>
<div class="info">
    
    Tags: <a href="/tags/haskell.html">haskell</a>, <a href="/tags/types.html">types</a>
    
</div>

<p>GHC has a language extension call <code>ExistentialQuantification</code>. This lets users write “existential” types. There are lots of good explanations of what an existential type is, but to briefly summarize: an existential type allows the callee to choose the type versus the caller.</p>
<p>As an example, if we have</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb1-1" data-line-number="1">    <span class="ot">{-# LANGUAGE ExistentialQuantification #-}</span></a>
<a class="sourceLine" id="cb1-2" data-line-number="2">    <span class="kw">data</span> <span class="dt">NotExist</span> a <span class="fu">=</span> <span class="dt">NotExist</span> a</a>
<a class="sourceLine" id="cb1-3" data-line-number="3">    <span class="kw">data</span> <span class="dt">Exists</span> <span class="fu">=</span> forall a<span class="fu">.</span> <span class="dt">Show</span> a <span class="ot">=&gt;</span> <span class="dt">Exists</span> a</a>
<a class="sourceLine" id="cb1-4" data-line-number="4"></a>
<a class="sourceLine" id="cb1-5" data-line-number="5"><span class="ot">    normal ::</span> <span class="dt">Show</span> a <span class="ot">=&gt;</span> <span class="dt">NotExist</span> a</a>
<a class="sourceLine" id="cb1-6" data-line-number="6">    normal <span class="fu">=</span> <span class="dt">NotExist</span> undefined</a>
<a class="sourceLine" id="cb1-7" data-line-number="7"></a>
<a class="sourceLine" id="cb1-8" data-line-number="8"><span class="ot">    exists ::</span> <span class="dt">Exists</span></a>
<a class="sourceLine" id="cb1-9" data-line-number="9">    exists <span class="fu">=</span> <span class="dt">Exists</span> <span class="st">&quot;The callee chose this&quot;</span></a></code></pre></div>
<p>With <code>normal</code> the caller gets to choose what <code>a</code> is and so we have to use <code>undefined</code>, otherwise we’d have to construct an arbitrary <code>Show a =&gt; a</code>.</p>
<p>With <code>exists</code>, the callee get’s to choose what is boxed up in <code>Exists</code>, the caller can’t control anything about it.</p>
<p>Now, in logic existentials correspond to propositions like <code>∃ x∈ℕ. x &gt; 0 ∧ x &lt; 2</code> or in English, “There exists an <code>x</code> such that <code>x</code> &gt; 0 and <code>x</code> &lt; 2”. Normal haskell types like <code>NotExists</code> correspond more to propositions like <code>∀ x∈ℕ. x &lt; x + 1</code>.</p>
<p>Interestingly, we can actually define <code>∃</code> in terms of <code>∀</code>.</p>
<pre><code>∃ x∈A. P(x) = ∀ Q. (∀ c∈A. P(c) → Q) → Q</code></pre>
<p>In English, the proposition that “There exists an <code>x</code> in <code>A</code> so that <code>P(x)</code>” is equivalent to “For all propositions <code>Q</code>, if for all <code>c</code>, <code>P(c)</code> implies <code>Q</code>, then <code>Q</code>.”</p>
<p>This can be translated to Haskell!</p>
<p>We’ll need to enable rank n types since our definition for <code>∃</code> uses nested <code>∀</code>s. Additionally, we’ll use constraint kinds and impredicative polymorphism since we’ll want to pass around typeclass constraints and store polymorphic values in lists.</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb3-1" data-line-number="1">    <span class="ot">{-# LANGUAGE RankNTypes, ConstraintKinds #-}</span></a>
<a class="sourceLine" id="cb3-2" data-line-number="2">    <span class="ot">{-# LANGUAGE KindSignatures, ImpredicativeTypes #-}</span></a>
<a class="sourceLine" id="cb3-3" data-line-number="3">    <span class="kw">import</span> <span class="dt">GHC.Prim</span> (<span class="dt">Constraint</span>)</a>
<a class="sourceLine" id="cb3-4" data-line-number="4"></a>
<a class="sourceLine" id="cb3-5" data-line-number="5">    <span class="kw">type</span> <span class="dt">Exists</span> c <span class="fu">=</span> forall x<span class="fu">.</span> (forall a<span class="fu">.</span> c a <span class="ot">=&gt;</span> a <span class="ot">-&gt;</span> x) <span class="ot">-&gt;</span> x</a></code></pre></div>
<p>Here <code>P(x)</code> becomes the proposition <code>c a =&gt; a</code>. Proving this “proposition” is done by providing a value of type <code>a</code>, this is sometimes called “witnessing”.</p>
<p>If this jump has left you baffled, try doing a bit of research on the “Curry Howard Isomorphism” and remembering that the type <code>c a =&gt; a</code> is really the same as the pair <code>(Dict, a)</code> where <code>Dict</code> is the record of all the function in the typeclass <code>c</code>.</p>
<p>With this intuition (terrible pun for constructionists) we can write a function to construct a <code>Exists c</code> given an <code>c a =&gt; a</code>.</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" data-line-number="1"><span class="ot">    exists ::</span> c a <span class="ot">=&gt;</span> a <span class="ot">-&gt;</span> <span class="dt">Exists</span> c</a>
<a class="sourceLine" id="cb4-2" data-line-number="2">    exists witness cont <span class="fu">=</span> cont witness</a></code></pre></div>
<p>Now we can actually write some code using this</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb5-1" data-line-number="1">    <span class="co">-- This needs impredicative polymorphism</span></a>
<a class="sourceLine" id="cb5-2" data-line-number="2"><span class="ot">    showables ::</span> [<span class="dt">Exists</span> <span class="dt">Show</span>]</a>
<a class="sourceLine" id="cb5-3" data-line-number="3">    showables <span class="fu">=</span> [ exists <span class="st">&quot;string&quot;</span></a>
<a class="sourceLine" id="cb5-4" data-line-number="4">                , exists <span class="ch">&#39;c&#39;</span></a>
<a class="sourceLine" id="cb5-5" data-line-number="5">                , exists ()</a>
<a class="sourceLine" id="cb5-6" data-line-number="6">                , exists <span class="dt">True</span>]</a></code></pre></div>
<p>So now we’ve got a list of <code>Exists Show</code>s so let’s figure out how to use it. Let’s write a function that takes a function <code>forall a. c a =&gt; a -&gt; b</code> and returns a function <code>Exists c -&gt; b</code>.</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb6-1" data-line-number="1"><span class="ot">    withExists ::</span> (forall a<span class="fu">.</span> c a <span class="ot">=&gt;</span> a <span class="ot">-&gt;</span> b) <span class="ot">-&gt;</span> <span class="dt">Exists</span> c <span class="ot">-&gt;</span> b</a>
<a class="sourceLine" id="cb6-2" data-line-number="2">    withExists cont existential <span class="fu">=</span> existential cont</a></code></pre></div>
<p>Now we can write</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb7-1" data-line-number="1">    main <span class="fu">=</span> mapM_ (withExists print) showables</a></code></pre></div>
<p>Which outputs</p>
<pre><code>&quot;string&quot;
&#39;c&#39;
()
True</code></pre>
<p>Just as expected!</p>
<p>And there you have it, existential types cobbled together from a few other extensions.</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Tue, 14 Jan 2014 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2014-01-14-existential.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Some Fun Dependently Typed Programs</title>
    <link>http://jozefg.bitbucket.org/posts/2013-12-24-favorite-dependent-types.html</link>
    <description><![CDATA[<div class="info">
    Posted on December 24, 2013
    
</div>
<div class="info">
    
    Tags: <a href="/tags/agda.html">agda</a>, <a href="/tags/types.html">types</a>
    
</div>

<p>I’ve been playing with Agda lately and decided to translate my two favorite dependently typed programs from Coq to Agda. Here are the results.</p>
<h2 id="variadic-functions">Variadic Functions</h2>
<p>Variadic functions are hard to do right. Especially since with <code>fun a b c d e</code> it isn’t clear if <code>fun a b c d</code> is supposed to be a call to a variadic function which returns a function, or whether the whole thing is one function call, or just a type error.</p>
<p>Now it’d be nice if we could say something like</p>
<pre><code>add 7 1 2 3 4 5 6 7</code></pre>
<p>In other words, tell the variadic function how many arguments we’ll give it at runtime. This is marginally less flexible than just listing them off, but not by much.</p>
<p>Now how could we encode this? Our type would <em>depend</em> on the value we pass it! So we could write something like</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb2-1" data-line-number="1">    <span class="kw">open</span> <span class="kw">import</span> Data<span class="ot">.</span>Nat</a>
<a class="sourceLine" id="cb2-2" data-line-number="2"></a>
<a class="sourceLine" id="cb2-3" data-line-number="3">    var<span class="ot">_</span>ty <span class="ot">:</span> ℕ <span class="ot">-&gt;</span> <span class="dt">Set</span> <span class="ot">-&gt;</span> <span class="dt">Set</span> <span class="ot">-&gt;</span> <span class="dt">Set</span></a>
<a class="sourceLine" id="cb2-4" data-line-number="4">    var<span class="ot">_</span>ty <span class="dv">0</span> <span class="ot">_</span>  ret    <span class="ot">=</span> ret</a>
<a class="sourceLine" id="cb2-5" data-line-number="5">    var<span class="ot">_</span>ty <span class="ot">(</span>suc a<span class="ot">)</span> t r <span class="ot">=</span> t <span class="ot">-&gt;</span> var<span class="ot">_</span>ty a t r</a></code></pre></div>
<p>So a call to <code>var_ty</code> returns a function of <code>n</code> arguments of type <code>t</code> and returns an <code>r</code>.</p>
<p>From there it’s simple to write our variadic sum,</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb3-1" data-line-number="1">    var<span class="ot">_</span>sum&#39; <span class="ot">:</span> <span class="ot">(</span>n <span class="ot">:</span> ℕ<span class="ot">)</span> <span class="ot">-&gt;</span> ℕ <span class="ot">-&gt;</span> var<span class="ot">_</span>ty n ℕ ℕ</a>
<a class="sourceLine" id="cb3-2" data-line-number="2">    var<span class="ot">_</span>sum&#39; <span class="dv">0</span> cur       <span class="ot">=</span> cur</a>
<a class="sourceLine" id="cb3-3" data-line-number="3">    var<span class="ot">_</span>sum&#39; <span class="ot">(</span>suc a<span class="ot">)</span> cur <span class="ot">=</span> <span class="ot">\</span>x <span class="ot">-&gt;</span> var<span class="ot">_</span>sum&#39; a <span class="ot">(</span>cur + x<span class="ot">)</span></a>
<a class="sourceLine" id="cb3-4" data-line-number="4"></a>
<a class="sourceLine" id="cb3-5" data-line-number="5">    var<span class="ot">_</span>sum <span class="ot">:</span> <span class="ot">(</span>n <span class="ot">:</span> ℕ<span class="ot">)</span> <span class="ot">-&gt;</span> var<span class="ot">_</span>ty n ℕ ℕ</a>
<a class="sourceLine" id="cb3-6" data-line-number="6">    var<span class="ot">_</span>sum n <span class="ot">=</span> var<span class="ot">_</span>sum&#39; n <span class="dv">0</span></a></code></pre></div>
<p>And <code>var_sum 3 1 2 3</code> evaluates to <code>6</code>, kinda nifty.</p>
<h2 id="heterogeneous-lists">Heterogeneous lists</h2>
<p>Functional languages are commonplace in functional programming, but they’re almost always homogeneous, meaning the list has only one type of element.</p>
<p>It’d be nice to store multiple types in the same list, and indeed we can do this with a bit of dependent-type-foo</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb4-1" data-line-number="1">    <span class="kw">open</span> <span class="kw">import</span> Data<span class="ot">.</span>List</a>
<a class="sourceLine" id="cb4-2" data-line-number="2"></a>
<a class="sourceLine" id="cb4-3" data-line-number="3">    <span class="kw">data</span> HList <span class="ot">:</span> List <span class="dt">Set</span> <span class="ot">-&gt;</span> <span class="dt">Set1</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb4-4" data-line-number="4">      []  <span class="ot">:</span> HList []</a>
<a class="sourceLine" id="cb4-5" data-line-number="5">      <span class="ot">_</span>∷<span class="ot">_</span> <span class="ot">:</span> <span class="ot">{</span>A <span class="ot">:</span> <span class="dt">Set</span><span class="ot">}{</span>xs <span class="ot">:</span> List <span class="dt">Set</span><span class="ot">}</span> <span class="ot">-&gt;</span> A <span class="ot">-&gt;</span> HList xs <span class="ot">-&gt;</span> HList <span class="ot">(</span>A ∷ xs<span class="ot">)</span></a></code></pre></div>
<p>So we attach a list of types to our <code>HList</code> and the element at <code>i</code> has the type of this list at <code>i</code>. The interesting bit is the definition of <code>∷</code>. It takes an implicit type <code>A</code>, and a list of types <code>xs</code>. It then takes an <code>A</code> and an <code>HList xs</code> and returns the new updated <code>HList</code>.</p>
<p>This is quite similar to <code>(a, (b, (c, ())))</code> in Haskell, but the dependent types make it much more pleasant to use. For example, we can now write</p>
<pre><code>foo : HList _
foo = 1 ∷ true ∷ &quot;foo&quot; ∷ tt ∷ []</code></pre>
<p>Not too shabby. This is much more pleasant to use than the Haskell equivalent, nested tuples. For example, to write a length function for tuples in Haskell, you’d have to say something like</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb6-1" data-line-number="1">    <span class="kw">class</span> <span class="dt">HasLength</span> a <span class="kw">where</span></a>
<a class="sourceLine" id="cb6-2" data-line-number="2">      <span class="fu">...</span></a>
<a class="sourceLine" id="cb6-3" data-line-number="3">    <span class="kw">instance</span> <span class="dt">HasLength</span> b <span class="ot">=&gt;</span> <span class="dt">HasLength</span> (a, b) <span class="kw">where</span></a>
<a class="sourceLine" id="cb6-4" data-line-number="4">      <span class="fu">...</span></a></code></pre></div>
<p>And rely on some typeclass prolog. Compare this to the equivalent</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode agda"><code class="sourceCode agda"><a class="sourceLine" id="cb7-1" data-line-number="1">    hlength <span class="ot">:</span> <span class="ot">{</span>xs <span class="ot">:</span> List <span class="dt">Set</span><span class="ot">}</span> <span class="ot">-&gt;</span> HList xs <span class="ot">-&gt;</span> ℕ</a>
<a class="sourceLine" id="cb7-2" data-line-number="2">    hlength <span class="ot">{</span>xs<span class="ot">}</span> <span class="ot">_</span> <span class="ot">=</span> length xs</a></code></pre></div>
<p>Since we have a nice flat list of all the types in our structure, it’s much simpler to work with.</p>
<p>That’s all for now, I’ll probably rant more about agda in the future.</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Tue, 24 Dec 2013 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2013-12-24-favorite-dependent-types.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Sieves in Haskell: Part 2</title>
    <link>http://jozefg.bitbucket.org/posts/2013-12-17-haskell-sieves.html</link>
    <description><![CDATA[<div class="info">
    Posted on December 17, 2013
    
</div>
<div class="info">
    
    Tags: <a href="/tags/haskell.html">haskell</a>
    
</div>

<p>So in my last post about sieves in Haskell, I’d mentioned that there is a purely functional approach infinite, lazy sieves. In the post I’ll explain how to construct these.</p>
<p>The first step to implementing the lazy version is to realize we need 2 kinds of laziness,</p>
<ol type="1">
<li>Laziness in the actual list</li>
<li>Laziness in how we cross things off</li>
</ol>
<p>This is important because in order to maintain an efficient sieve, we must cross off all multiples of a number when we see the number, otherwise we degrade to trial division.</p>
<p>However, this seems impossible since well, our list is infinitely long. We can work around this though by using a form of iterators.</p>
<p>Specifically, we’ll have a function like this</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb1-1" data-line-number="1">    <span class="kw">import</span> <span class="kw">qualified</span> <span class="dt">Data.IntMap.Strict</span> <span class="kw">as</span> <span class="dt">M</span></a>
<a class="sourceLine" id="cb1-2" data-line-number="2">    <span class="kw">import</span> <span class="dt">Data.List</span></a>
<a class="sourceLine" id="cb1-3" data-line-number="3"><span class="ot">    sieve&#39; ::</span> [<span class="dt">Int</span>] <span class="ot">-&gt;</span> <span class="dt">M.IntMap</span> [<span class="dt">Int</span>] <span class="ot">-&gt;</span> [<span class="dt">Int</span>]</a></code></pre></div>
<p>And in that <code>IntMap</code>, we store a list of prime factors of that number. We use these as “iterators”. So when we get to a number <code>n</code>, if <code>n</code> has a list of primes associated with them, insert each prime <code>p</code> at <code>p + n</code> in the map and don’t add <code>n</code> to our list. If <code>n</code> has no prime factors, then it is clearly prime so we add it to our list and add <code>n</code> as a prime factor of <code>2 * n</code>.</p>
<p>In Haskell code</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb2-1" data-line-number="1">    sieve&#39; (n<span class="fu">:</span>ns) m <span class="fu">=</span></a>
<a class="sourceLine" id="cb2-2" data-line-number="2">      <span class="kw">case</span> M.lookup n m <span class="kw">of</span></a>
<a class="sourceLine" id="cb2-3" data-line-number="3">        <span class="dt">Nothing</span> <span class="ot">-&gt;</span> n <span class="fu">:</span> sieve&#39; ns (M.insertWith (<span class="fu">++</span>) (<span class="dv">2</span> <span class="fu">*</span> n) [n] m)</a>
<a class="sourceLine" id="cb2-4" data-line-number="4">        <span class="dt">Just</span> ps <span class="ot">-&gt;</span> sieve&#39; ns <span class="fu">$</span> foldl&#39; insertPrime (M.delete n m) ps</a>
<a class="sourceLine" id="cb2-5" data-line-number="5">      <span class="kw">where</span> insertPrime m p <span class="fu">=</span> M.insertWith (<span class="fu">++</span>) (n <span class="fu">+</span> p) [p] m</a></code></pre></div>
<p>And then to drive this, we can just use</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb3-1" data-line-number="1"><span class="ot">    sieve ::</span> [<span class="dt">Int</span>]</a>
<a class="sourceLine" id="cb3-2" data-line-number="2">    sieve <span class="fu">=</span> sieve&#39; [<span class="dv">2</span><span class="fu">..</span>] M.empty</a>
<a class="sourceLine" id="cb3-3" data-line-number="3"></a>
<a class="sourceLine" id="cb3-4" data-line-number="4">    main <span class="fu">=</span> sum <span class="fu">.</span> takeWhile (<span class="fu">&lt;</span><span class="dv">2000000</span>) <span class="fu">$</span> sieve</a></code></pre></div>
<p>And there you have it. This code is certainly cleaner than the <code>ST</code> version and decently performant, clocking in at 1.5 seconds. This is a little unfair to this version though since the <code>ST</code> code takes advantage of unboxed types which isn’t possible here. Additionally, this version is strictly more general, creating an infinite list rather than a finite one.</p>
<p>I’ll updated with a fairer benchmark once I have time to redo the <code>ST</code> code.</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Tue, 17 Dec 2013 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2013-12-17-haskell-sieves.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Colleges and My Not-So-Distant Future</title>
    <link>http://jozefg.bitbucket.org/posts/2013-12-15-major-life-news.html</link>
    <description><![CDATA[<div class="info">
    Posted on December 15, 2013
    
</div>
<div class="info">
    
    Tags: <a href="/tags/personal.html">personal</a>
    
</div>

<p>This is a short but excited post.</p>
<p>I’m a senior in high school and that means colleges. This weekend has been a rather interesting one, I’ve been accepted to two schools I’ve always dreamed about, MIT and CMU.</p>
<p>I was absolutely shocked. I’ve been wanting the chance to go to a school like either of them since I was 8. These last 9 or so months have seen me wobbling back and forth between hopelessness, confidence, and excitement over the idea of attending. The fact that both want me and that I’m actually getting to go is humbling, terrifying, and making me a little teary-eyed all at the same time.</p>
<p>I’ve decided I’ll be attending Carnegie Mellon University next fall. It wasn’t an easy choice by any stretch; blood, sweat and tears were shed over it, but I’m happy with my decision.</p>
<p>I’d like to take a moment to extend my sincerest thanks to any and everyone who has helped me get through what’s proving to be a long 4 years. It means the world to me.</p>
<p>In particular, I’ll never be able to thank those who have given me the chance to work at labs at the U of M and MIT enough. They’ve taken me a few steps closer to my dreams and I know that without them I’d have never been accepted.</p>
<p>Now of course, I have to make it 4 years at CMU :)</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Sun, 15 Dec 2013 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2013-12-15-major-life-news.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Sieves in Haskell</title>
    <link>http://jozefg.bitbucket.org/posts/2013-12-03-primes-in-haskell.html</link>
    <description><![CDATA[<div class="info">
    Posted on December  3, 2013
    
</div>
<div class="info">
    
    Tags: <a href="/tags/haskell.html">haskell</a>
    
</div>

<p>The other day I was answering a question on StackOverflow and decided the solution was worth talking about.</p>
<p>It was particularly interesting because it illustrated something: Haskell makes a darn good imperative language.</p>
<p>What do I mean? This statement seems absurd, Haskell has no notion of state! How could it be used for imperative programming?</p>
<p>Well thanks to monads and <code>do</code> notation, we’re going to translate the following Python code to Haskell</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb1-1" data-line-number="1">    <span class="kw">def</span> sieve(n):</a>
<a class="sourceLine" id="cb1-2" data-line-number="2">        nums <span class="op">=</span> [<span class="va">True</span> <span class="cf">for</span> _ <span class="kw">in</span> <span class="bu">range</span>(n)]</a>
<a class="sourceLine" id="cb1-3" data-line-number="3">        nums[<span class="dv">0</span>] <span class="op">=</span> <span class="va">False</span></a>
<a class="sourceLine" id="cb1-4" data-line-number="4">        nums[<span class="dv">1</span>] <span class="op">=</span> <span class="va">False</span></a>
<a class="sourceLine" id="cb1-5" data-line-number="5">        <span class="cf">for</span> i <span class="kw">in</span> <span class="bu">range</span>(n):</a>
<a class="sourceLine" id="cb1-6" data-line-number="6">            <span class="cf">if</span> nums[i]:</a>
<a class="sourceLine" id="cb1-7" data-line-number="7">               <span class="cf">for</span> mul <span class="kw">in</span> <span class="bu">range</span>(i<span class="op">*</span><span class="dv">2</span>, n, i):</a>
<a class="sourceLine" id="cb1-8" data-line-number="8">                   nums[mul] <span class="op">=</span> <span class="va">False</span></a>
<a class="sourceLine" id="cb1-9" data-line-number="9">        <span class="cf">return</span> [i <span class="cf">for</span> i, v <span class="kw">in</span> <span class="bu">enumerate</span>(nums) <span class="cf">if</span> v]</a></code></pre></div>
<p>This is the sieve of Eratosthenes, it works like this</p>
<ol type="1">
<li>Write out the nums 0 - n</li>
<li>Cross off 0 and 1</li>
<li>For the next not crossed off number, cross of its multiples</li>
<li>Repeat 3 until the end of the list</li>
<li>The remaining numbers are primes</li>
</ol>
<p>So in Python, we write this with a mutable list, <code>nums</code>. Then we just loop through each index and cross of as we go along! It’s a pretty straightforward translation.</p>
<p>Now we could write this in pure Haskell,</p>
<pre><code>    sieve n = go 2 $ False : False : replicate (n-2) True
      where go = ...</code></pre>
<p>But this is incredibly awkward and inefficient, since updating an element takes both linear time and space. We could opt for a clever solution</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb3-1" data-line-number="1">    primes <span class="fu">=</span> go [<span class="dv">2</span><span class="fu">..</span>]</a>
<a class="sourceLine" id="cb3-2" data-line-number="2">      <span class="kw">where</span> go (p<span class="fu">:</span>rest) <span class="fu">=</span> p <span class="fu">:</span> [r <span class="fu">|</span> r <span class="ot">&lt;-</span> rest, r <span class="ot">`mod`</span> p <span class="fu">/=</span> <span class="dv">0</span>]</a>
<a class="sourceLine" id="cb3-3" data-line-number="3">    sieve <span class="fu">=</span> flip takeWhile primes <span class="fu">.</span> (<span class="fu">&lt;</span>)</a></code></pre></div>
<p>But this is still very, very inefficient. And in fact, it’s not even a sieve! it’s called trial division.</p>
<p>So, what’s a functional programmer to do.. Well, let’s start by cheating!</p>
<h2 id="the-st-monad">The ST Monad</h2>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" data-line-number="1">    <span class="kw">import</span> <span class="dt">Data.Vector.Unboxed</span> <span class="kw">hiding</span> (forM_)</a>
<a class="sourceLine" id="cb4-2" data-line-number="2">    <span class="kw">import</span> <span class="dt">Data.Vector.Unboxed.Mutable</span></a>
<a class="sourceLine" id="cb4-3" data-line-number="3">    <span class="kw">import</span> <span class="dt">Control.Monad.ST</span> (runST)</a>
<a class="sourceLine" id="cb4-4" data-line-number="4">    <span class="kw">import</span> <span class="dt">Control.Monad</span> (forM_, when)</a>
<a class="sourceLine" id="cb4-5" data-line-number="5">    <span class="kw">import</span> <span class="dt">Prelude</span> <span class="kw">hiding</span> (read)</a></code></pre></div>
<p>This laundry list of imports gives us access to something pretty cool: mutable state in Haskell. <code>ST</code> is short for State Thread and acts as a safe wrapper around <code>IO</code>. It’ll let us imperative code, mutate things, but then force us to present a pure interface!</p>
<p>The purity trick is actually quite clever, it’s done by providing an escape hatch out of <code>ST</code> with the type</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb5-1" data-line-number="1"><span class="ot">    runST ::</span> (forall s<span class="fu">.</span> <span class="dt">ST</span> s a) <span class="ot">-&gt;</span> a</a></code></pre></div>
<p>So we can only escape <code>ST</code> when this phantom <code>s</code> is universally quantified. This forces you handle the <code>s</code>, the state, opaquely which prevents us from doing anything unsafe.</p>
<p>Next we can use <code>Data.Vector.Unboxed.Mutable</code> to create unboxed mutable vectors.</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb6-1" data-line-number="1"><span class="ot">    new ::</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">ST</span> s (<span class="dt">MVector</span> s a)</a></code></pre></div>
<p>And now all of this leads to</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb7-1" data-line-number="1"><span class="ot">    sieve ::</span> <span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">Vector</span> <span class="dt">Bool</span></a>
<a class="sourceLine" id="cb7-2" data-line-number="2">    sieve n <span class="fu">=</span> runST <span class="fu">$</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb7-3" data-line-number="3">      vec <span class="ot">&lt;-</span> new (n <span class="fu">+</span> <span class="dv">1</span>) <span class="co">-- Create the mutable vector</span></a>
<a class="sourceLine" id="cb7-4" data-line-number="4">      set vec <span class="dt">True</span>       <span class="co">-- Set all the elements to True</span></a>
<a class="sourceLine" id="cb7-5" data-line-number="5">      write vec <span class="dv">1</span> <span class="dt">False</span>  <span class="co">-- One isn&#39;t a prime</span></a>
<a class="sourceLine" id="cb7-6" data-line-number="6">      forM_ [<span class="dv">2</span><span class="fu">..</span>n] <span class="fu">$</span> \ i <span class="ot">-&gt;</span> <span class="kw">do</span> <span class="co">-- Loop for i from 2 to n</span></a>
<a class="sourceLine" id="cb7-7" data-line-number="7">        val <span class="ot">&lt;-</span> read vec i <span class="co">-- read the value at i</span></a>
<a class="sourceLine" id="cb7-8" data-line-number="8">        when val <span class="fu">$</span> <span class="co">-- if the value is true, set all its multiples to false</span></a>
<a class="sourceLine" id="cb7-9" data-line-number="9">          forM_ [<span class="dv">2</span><span class="fu">*</span>i, <span class="dv">3</span><span class="fu">*</span>i <span class="fu">..</span> n] <span class="fu">$</span> \j <span class="ot">-&gt;</span> write vec j <span class="dt">False</span></a>
<a class="sourceLine" id="cb7-10" data-line-number="10">      freeze vec <span class="co">-- return the immutable vector</span></a></code></pre></div>
<p>Using this we can easily sum the first 10k primes (project Euler 10)</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb8-1" data-line-number="1">    main <span class="fu">=</span> print <span class="fu">.</span> ifoldl&#39; summer <span class="dv">0</span> <span class="fu">$</span> sieve <span class="dv">2000000</span></a>
<a class="sourceLine" id="cb8-2" data-line-number="2">      <span class="kw">where</span> summer s i b <span class="fu">=</span> <span class="kw">if</span> b <span class="kw">then</span> i <span class="fu">+</span> s <span class="kw">else</span> s</a></code></pre></div>
<p>So there you have the “cheater” way. But darn it it’s fast, that sums the first 10,000 primes in around 0.2 seconds.</p>
<p>In my next post, I’ll explain a lazy functional way to do this to produce an infinite sieve.</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Tue, 03 Dec 2013 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2013-12-03-primes-in-haskell.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Sports for Geeks</title>
    <link>http://jozefg.bitbucket.org/posts/2013-11-29-sports.html</link>
    <description><![CDATA[<div class="info">
    Posted on November 29, 2013
    
</div>
<div class="info">
    
    Tags: <a href="/tags/opinions.html">opinions</a>
    
</div>

<p>Over the last few months, I have been acquiring various tidbits of knowledge to do with sports. This has been essential since my girlfriend likes football.</p>
<p>Here is a brief summary of what I’ve learned:</p>
<h2 id="dannys-neanderthal-level-guide-to-sports">Danny’s Neanderthal-Level Guide to Sports</h2>
<ol type="1">
<li>Football players don’t wear “tights”</li>
<li>It’s never OK to say “This is pointless”</li>
<li>Touchdowns are good</li>
<li>Flags are bad</li>
<li>Unless it’s against the other team</li>
<li>The ref is dumb. Always</li>
<li>Penalties are bad</li>
<li>Unless it’s against the other team</li>
<li>The other team is barely human</li>
<li>It is acceptable to dance upon completing a “touch-down”</li>
<li>Field goals are good</li>
<li>Smiling is bad</li>
<li>Upon fumbling, a player is dead to us</li>
<li>It is acceptable to shoot the quarter back following an interception</li>
<li>Make-up for fans is OK if it’s the teams colors</li>
<li>Beer hats are ingenious precisely within a stadium</li>
<li>Shirts are always optional in a stadium. Even in November. Or when you’re overweight.</li>
<li>the-team-we-don’t-like (TTWDL) will never win. Ever.</li>
<li>TTWDL is synonymous with devil.</li>
<li>People who support TTWDL are the devil’s children</li>
<li>the-team-we-like (TTWL) are meant to win. Any other outcome is not possible</li>
<li>The quarter-back is always hot</li>
<li>An injury on the TTWL is unspeakably horrible</li>
<li>If you yell at the TV, they can hear you</li>
<li>Some teams just need to be put out of their misery early in the season</li>
<li>It is never OK to say “it’s just a game”</li>
<li>Football players are not just big kids who never grew up</li>
<li>If someone is “first pick” then they are good and we want then on our team</li>
<li>“Our team” is an appropriate saying for a team that we like even though we are in no way associated with that team</li>
<li>Fantasy football isn’t silly or at all like Dungeons and Dragons and other pretend games</li>
</ol>
<p>This is all I got so far.</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Fri, 29 Nov 2013 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2013-11-29-sports.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Fixpoints and Iso-recursive Types</title>
    <link>http://jozefg.bitbucket.org/posts/2013-11-09-iso-recursive-types.html</link>
    <description><![CDATA[<div class="info">
    Posted on November  9, 2013
    
</div>
<div class="info">
    
    Tags: <a href="/tags/haskell.html">haskell</a>, <a href="/tags/types.html">types</a>
    
</div>

<p>Let’s imagine a world where Haskell didn’t have recursive functions. The Haskell committee simply left them out by mistake. Could Haskell still be Turing complete?</p>
<p>Well we need some method of arbitrary recursion, let’s try what we’d do in lambda calculus: fixpoints. In particular, we want a function like this</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb1-1" data-line-number="1"><span class="ot">    fix ::</span> (a <span class="ot">-&gt;</span> a) <span class="ot">-&gt;</span> a</a></code></pre></div>
<h3 id="what-are-fixpoints">What are fixpoints?</h3>
<p>So we pass in a function <code>f</code>, and it will return to us a value <code>a</code> so that <code>f a = a</code>. Why is this useful? Imagine we encode recursive functions like this</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb2-1" data-line-number="1"><span class="ot">    factorial ::</span> (<span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">Int</span>)</a>
<a class="sourceLine" id="cb2-2" data-line-number="2">              <span class="ot">-&gt;</span> <span class="dt">Int</span></a>
<a class="sourceLine" id="cb2-3" data-line-number="3">              <span class="ot">-&gt;</span> <span class="dt">Int</span></a>
<a class="sourceLine" id="cb2-4" data-line-number="4">    factorial self <span class="dv">0</span> <span class="fu">=</span> <span class="dv">1</span></a>
<a class="sourceLine" id="cb2-5" data-line-number="5">    factorial self n <span class="fu">=</span> n <span class="fu">*</span> self (n<span class="fu">-</span><span class="dv">1</span>)</a></code></pre></div>
<p>So we pass along recursion through this extra function. Well here factorial is really a function of type</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb3-1" data-line-number="1"><span class="ot">    factorial ::</span> (<span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">Int</span>) <span class="ot">-&gt;</span> (<span class="dt">Int</span> <span class="ot">-&gt;</span> <span class="dt">Int</span>)</a></code></pre></div>
<p>And we want to fill it like this</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" data-line-number="1">    completeFactorial n <span class="fu">=</span> factorial (factorial (factorial (factorial <span class="fu">...</span>))) n</a></code></pre></div>
<p>Now when we take the fixpoint, we find an <code>a</code> where <code>factorial a = a</code>. This means that</p>
<pre><code>factorial a = factorial (factorial a) = factorial (factorial (factorial ...)))</code></pre>
<p>So with fixpoints, we get that infinitely long chain that we wanted.</p>
<h3 id="fixpoints-with-recursion">Fixpoints with recursion</h3>
<p>If we allowed ourselves recursion for a moment then <code>fix</code> is easy</p>
<pre><code>fix f = let x = f x in x</code></pre>
<p>This looks silly, but in fact, if <code>f</code> isn’t strict in <code>x</code>, than <code>x</code> can be non-bottom. And if <code>f</code> is strict than by definition, <code>f ⊥ = ⊥</code> so <code>⊥</code> is a fixpoint.</p>
<h3 id="fixpoints-without-recursion">Fixpoints without recursion</h3>
<p>Now we can actually still write a fixpoint-finding function without recursion. The most famous one is the y-combinator. In lambda calculus, we’d write this as</p>
<pre><code>Y = λf . (λx . f (x x)) (λx . f (x x))</code></pre>
<p>And we want to show that <code>Y f = f (Y f)</code></p>
<pre><code>Y f = f (Y f)
(λx . f (x x)) (λx . f (x x)) = f ((λx . f (x x)) (λx . f (x x)))
f((λx . f (x x)) (λx . f (x x))) = f ((λx . f (x x)) (λx . f (x x)))</code></pre>
<p>With simple beta reduction, they’re equal.</p>
<h4 id="fixpoints-in-haskell">Fixpoints in Haskell</h4>
<p>Now in Haskell, this doesn’t work. We could try</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb9-1" data-line-number="1">    fix f <span class="fu">=</span> (\x <span class="ot">-&gt;</span> f (x x)) (\x <span class="ot">-&gt;</span> f (x x))</a></code></pre></div>
<p>But what is <code>x</code>’s type? Well it’s a function so</p>
<pre><code>x :: a -&gt; b</code></pre>
<p>And <code>x</code>’s first argument is <code>x</code>, so</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb11-1" data-line-number="1">    <span class="kw">type</span> <span class="dt">T</span> <span class="fu">=</span> <span class="dt">T</span> <span class="ot">-&gt;</span> b</a>
<a class="sourceLine" id="cb11-2" data-line-number="2">    <span class="kw">type</span> <span class="dt">T</span> <span class="fu">=</span> μ<span class="dt">R</span><span class="fu">.</span> <span class="dt">R</span> <span class="ot">-&gt;</span> b <span class="co">-- This means the same as the above</span></a>
<a class="sourceLine" id="cb11-3" data-line-number="3"><span class="ot">    x ::</span> <span class="dt">T</span></a></code></pre></div>
<p>But this isn’t legal! We can’t have infinite types like that. Don’t despair though, we’re going to use the magic of iso-recursive types.</p>
<p>What are iso-recursive types? Well they’re like (equi-)recursive types, but they provide two operations, <code>fold</code> and <code>unfold</code>.</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb12-1" data-line-number="1"><span class="ot">    unfold ::</span> μ<span class="dt">X</span><span class="fu">.</span> <span class="dt">T</span> <span class="ot">-&gt;</span> [μ<span class="dt">X</span><span class="fu">.</span> <span class="dt">T</span><span class="fu">/</span><span class="dt">X</span>]<span class="dt">T</span></a>
<a class="sourceLine" id="cb12-2" data-line-number="2"><span class="ot">    fold   ::</span> [μ<span class="dt">X</span><span class="fu">.</span> <span class="dt">T</span><span class="fu">/</span><span class="dt">T</span>]<span class="dt">T</span> <span class="ot">-&gt;</span> μ<span class="dt">X</span><span class="fu">.</span> <span class="dt">T</span></a></code></pre></div>
<p>Where <code>[foo/bar]baz</code> means, &quot;substitute all occurrences of <code>bar</code> with <code>foo</code> in <code>baz</code>. When trying to unify iso-recursive types, we don’t consider a type equal to an unfolding of that type. This makes type inference considerably easier since we’re requiring the user to explicitly fold and unfold types.</p>
<p>We can write these in Haskell</p>
<div class="sourceCode" id="cb13"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb13-1" data-line-number="1">    <span class="kw">newtype</span> <span class="dt">Mu</span> f <span class="fu">=</span> <span class="dt">Mu</span> {<span class="ot">unMu ::</span> f (<span class="dt">Mu</span> f)}</a>
<a class="sourceLine" id="cb13-2" data-line-number="2">    unfold <span class="fu">=</span> unMu</a>
<a class="sourceLine" id="cb13-3" data-line-number="3">    fold   <span class="fu">=</span> <span class="dt">Mu</span></a></code></pre></div>
<p>Now we can write the type of <code>x</code></p>
<div class="sourceCode" id="cb14"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb14-1" data-line-number="1">    <span class="kw">newtype</span> <span class="dt">X&#39;</span> b a <span class="fu">=</span> {<span class="ot">unX ::</span> a <span class="ot">-&gt;</span> b}</a>
<a class="sourceLine" id="cb14-2" data-line-number="2">    <span class="kw">type</span> <span class="dt">X</span> a <span class="fu">=</span> <span class="dt">Mu</span> (<span class="dt">X&#39;</span> a)</a></code></pre></div>
<p>Take a moment to think about this, mentally unfolding we have</p>
<pre><code>X a
Mu (X&#39; a)
Mu X&#39; -&gt; a
(Mu X&#39; -&gt; a) -&gt; a
...</code></pre>
<p>There we go! Now for that y combinator</p>
<div class="sourceCode" id="cb16"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb16-1" data-line-number="1">    unfold&#39; <span class="fu">=</span> unX  <span class="fu">.</span> unfold</a>
<a class="sourceLine" id="cb16-2" data-line-number="2">    fold&#39;   <span class="fu">=</span> fold <span class="fu">.</span> <span class="dt">X&#39;</span></a>
<a class="sourceLine" id="cb16-3" data-line-number="3">    y f <span class="fu">=</span> (\x <span class="ot">-&gt;</span> f (unfold&#39; x x)) <span class="fu">$</span> fold&#39; (\x <span class="ot">-&gt;</span> f (unfold&#39; x x))</a></code></pre></div>
<p>and finally</p>
<div class="sourceCode" id="cb17"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb17-1" data-line-number="1">    fix <span class="fu">=</span> y</a></code></pre></div>
<p>And to test it</p>
<div class="sourceCode" id="cb18"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb18-1" data-line-number="1">    f <span class="fu">=</span> fix factorial <span class="co">-- From the way before</span></a>
<a class="sourceLine" id="cb18-2" data-line-number="2">    main <span class="fu">=</span> mapM_ (print <span class="fu">.</span> f) [<span class="dv">1</span><span class="fu">..</span><span class="dv">5</span>]</a></code></pre></div>
<p>prints</p>
<pre><code>1
2
6
24
120</code></pre>
<p>Which means we’re successful, we’ve added back recursion to Haskell!</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Sat, 09 Nov 2013 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2013-11-09-iso-recursive-types.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Learn You Some Category Theory</title>
    <link>http://jozefg.bitbucket.org/posts/2013-10-22-category-theory-in-haskell.html</link>
    <description><![CDATA[<div class="info">
    Posted on October 22, 2013
    
</div>
<div class="info">
    
    Tags: <a href="/tags/math.html">math</a>
    
</div>

<h3 id="introduction">Introduction</h3>
<p>Hi!</p>
<p>This is the first in a series of posts on basic category theory in Haskell. they require no knowledge of category theory, but I expect the reader to be familiar with Haskell. I will make various parallels to set theory, but they can be skipped in favor of the corresponding Haskell code.</p>
<h3 id="categories">Categories</h3>
<p>What is a category?</p>
<p>A category is a collection of 2 things: objects and arrows. We leave the idea of an object abstract, it’s just a “thing”. It varies from category to category, in some it will be sets, in some monoids, whatever, but it’s the core “building block” of that category.</p>
<p>Arrows are different “things” that go from objects to object, we’d write them like <code>f : A -&gt; B</code> to mean that <code>f</code> is an arrow from object <code>A</code> to object <code>B</code>.</p>
<p>Think of this like a directed graph, objects are nodes and arrows are lines. In fact as we go, we’ll talk about parts of categories just like this, as diagrams.</p>
<p>Let’s do some examples, let’s say objects are sets. So</p>
<pre><code>A = {1, 2}
B = {3, 4}</code></pre>
<p>Then arrows would be functions from set to set.</p>
<pre><code>f 1 = 2
f 2 = 4</code></pre>
<p>Another example: Haskell. Objects would be types and arrows would be functions</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb3-1" data-line-number="1">    <span class="kw">type</span> <span class="dt">A</span> <span class="fu">=</span> <span class="dt">Int</span></a>
<a class="sourceLine" id="cb3-2" data-line-number="2">    <span class="kw">type</span> <span class="dt">B</span> <span class="fu">=</span> <span class="dt">Int</span></a>
<a class="sourceLine" id="cb3-3" data-line-number="3"></a>
<a class="sourceLine" id="cb3-4" data-line-number="4"><span class="ot">    f ::</span> <span class="dt">A</span> <span class="ot">-&gt;</span> <span class="dt">B</span></a>
<a class="sourceLine" id="cb3-5" data-line-number="5">    f x <span class="fu">=</span> x <span class="fu">+</span> <span class="dv">1</span></a></code></pre></div>
<p>Now for some group of objects and arrows to be a category, a few conditions have to hold</p>
<ul>
<li><p>There must be an identity arrow for each object</p>
<pre><code>idA : A -&gt; A
idB : B -&gt; B</code></pre></li>
<li><p>There must be an operation to compose arrows, I use <code>.</code> for this</p>
<pre><code>f : A -&gt; B
g : B -&gt; C
g . f : A -&gt; C</code></pre></li>
<li><p>Identities and composition have to play nice,</p>
<pre><code>f . id = f
id . f = f</code></pre></li>
</ul>
<p>And viola! If we can show these few things, it’s a category!</p>
<h4 id="examples">Examples</h4>
<p>First let’s do the category of sets, which we call <code>Set</code>,</p>
<ol type="1">
<li>Objects are sets</li>
<li>Arrows are functions from set to set</li>
<li>Identity arrows are just identity functions</li>
<li>Arrow composition is function composition</li>
<li>It’s trivial To see that our identity arrows satisfy these conditions</li>
</ol>
<p>Next is Haskell, this category is called <code>Hask</code>.</p>
<ol type="1">
<li>Objects are types</li>
<li>Arrows are functions</li>
<li>The identity arrows are all given by <code>id</code></li>
<li>Arrow composition is just <code>.</code></li>
<li>We know that <code>id . f = f . id = f</code> in Haskell</li>
</ol>
<p>There you have it, our start into the wide world of category theory.</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Tue, 22 Oct 2013 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2013-10-22-category-theory-in-haskell.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Representable Functors</title>
    <link>http://jozefg.bitbucket.org/posts/2013-10-21-representable-functors.html</link>
    <description><![CDATA[<div class="info">
    Posted on October 21, 2013
    
</div>
<div class="info">
    
    Tags: <a href="/tags/haskell.html">haskell</a>, <a href="/tags/math.html">math</a>
    
</div>

<p>Representable functors are a powerful tool in category theory. As it turns out, they’re pretty useful in Haskell as well. Here’s a few examples of what they are and how to use them</p>
<p>First, some definition. We’re interested in <code>Hask</code> which the category where objects are types and arrows are functions. A representable functor for us, is a special functor from <code>Hask -&gt; Hask</code> (an endofunctor). Now when we apply category theory to Haskell, we also pretend <code>Hask</code> is <code>Set</code> (The category of sets). There’s a type of functor called a <code>hom-functor</code>. It’s a functor that looks like this</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb1-1" data-line-number="1">    <span class="kw">newtype</span> <span class="dt">Hom</span> a <span class="fu">=</span> (<span class="ot">-&gt;</span>) a</a>
<a class="sourceLine" id="cb1-2" data-line-number="2">    <span class="co">-- Hom a b = a -&gt; b</span></a></code></pre></div>
<p>Now, a <code>Hom</code> implements <code>Functor</code> like this</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb2-1" data-line-number="1">    <span class="kw">instance</span> <span class="dt">Functor</span> <span class="dt">Hom</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb2-2" data-line-number="2">        fmap f hom <span class="fu">=</span> f <span class="fu">.</span> hom</a></code></pre></div>
<p>In other words, <code>Hom a</code> takes an object <code>b</code> to the set of all morphisms <code>a -&gt; b</code>. It takes an arrow <code>b -&gt; c</code> to the function <code>Hom a b -&gt; Hom a c</code> using composition. Nothing stunning yet.</p>
<p>Now consider some arbitrary functor <code>F</code>. Suppose there exists an object <code>a</code> so that <code>F</code> is isomorphic to <code>Hom a</code>. What would this look like?</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb3-1" data-line-number="1">    <span class="kw">type</span> family <span class="dt">Obj</span> (<span class="ot">f ::</span> <span class="fu">*</span> <span class="ot">-&gt;</span> <span class="fu">*</span>)<span class="ot"> ::</span> <span class="fu">*</span></a>
<a class="sourceLine" id="cb3-2" data-line-number="2">    <span class="kw">class</span> <span class="dt">Functor</span> f <span class="ot">=&gt;</span> <span class="dt">HomIso</span> f <span class="kw">where</span></a>
<a class="sourceLine" id="cb3-3" data-line-number="3"><span class="ot">      toHom ::</span> f a <span class="ot">-&gt;</span> <span class="dt">Hom</span> (<span class="dt">Obj</span> f) a</a>
<a class="sourceLine" id="cb3-4" data-line-number="4"><span class="ot">      toF   ::</span> <span class="dt">Hom</span> (<span class="dt">Obj</span> f) a <span class="ot">-&gt;</span> f a</a></code></pre></div>
<p>And we have the laws that</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" data-line-number="1">    toHom <span class="fu">.</span> toF <span class="fu">=</span> id</a>
<a class="sourceLine" id="cb4-2" data-line-number="2">    toF <span class="fu">.</span> toHom <span class="fu">=</span> id</a></code></pre></div>
<p>Then <code>f</code> is a representable functor. From now on, I will refer to <code>HomIso</code> as <code>Repr</code> to emphasize this. The simplest representable functor is of course <code>Hom a</code>.</p>
<p>Let’s notice some useful properties of representable functors.</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb5-1" data-line-number="1"><span class="ot">    lookup ::</span> <span class="dt">Repr</span> f <span class="ot">=&gt;</span> f a <span class="ot">-&gt;</span> <span class="dt">Obj</span> f <span class="ot">-&gt;</span> a</a>
<a class="sourceLine" id="cb5-2" data-line-number="2">    lookup <span class="fu">=</span> toHom</a></code></pre></div>
<p>Our functor can look things up! Cool! Let’s use this idea to guide us to finding some simple representable functors. Let’s look at a trivial case</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb6-1" data-line-number="1">    <span class="kw">newtype</span> <span class="dt">Identity</span> a <span class="fu">=</span> <span class="dt">Identity</span> {<span class="ot">runIdentity ::</span> a}</a>
<a class="sourceLine" id="cb6-2" data-line-number="2">                       <span class="kw">deriving</span>(<span class="dt">Eq</span>, <span class="dt">Show</span>, <span class="dt">Functor</span>)</a>
<a class="sourceLine" id="cb6-3" data-line-number="3"></a>
<a class="sourceLine" id="cb6-4" data-line-number="4">    <span class="kw">newtype</span> <span class="dt">Unit</span> <span class="fu">=</span> <span class="dt">Unit</span></a>
<a class="sourceLine" id="cb6-5" data-line-number="5">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">Obj</span> <span class="dt">Identity</span> <span class="fu">=</span> <span class="dt">Unit</span></a>
<a class="sourceLine" id="cb6-6" data-line-number="6"></a>
<a class="sourceLine" id="cb6-7" data-line-number="7">    <span class="kw">instance</span> <span class="dt">Repr</span> <span class="dt">Identity</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb6-8" data-line-number="8">      toHom (<span class="dt">Identity</span> a) <span class="fu">=</span> const a</a>
<a class="sourceLine" id="cb6-9" data-line-number="9">      toF f <span class="fu">=</span> <span class="dt">Identity</span> <span class="fu">$</span> f <span class="dt">Unit</span></a></code></pre></div>
<p>Since <code>Identity</code> has only one value, <code>Unit</code> indexes it exactly. A more complicated example</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb7-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">Prod</span> a <span class="fu">=</span> <span class="dt">Prod</span> a a</a>
<a class="sourceLine" id="cb7-2" data-line-number="2">                <span class="kw">deriving</span>(<span class="dt">Eq</span>, <span class="dt">Show</span>, <span class="dt">Functor</span>)</a>
<a class="sourceLine" id="cb7-3" data-line-number="3"></a>
<a class="sourceLine" id="cb7-4" data-line-number="4">    <span class="kw">data</span> <span class="dt">Two</span> <span class="fu">=</span> <span class="dt">InL</span> <span class="fu">|</span> <span class="dt">InR</span></a>
<a class="sourceLine" id="cb7-5" data-line-number="5">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">Obj</span> <span class="dt">Prod</span> <span class="fu">=</span> <span class="dt">Two</span></a>
<a class="sourceLine" id="cb7-6" data-line-number="6"></a>
<a class="sourceLine" id="cb7-7" data-line-number="7">    <span class="kw">instance</span> <span class="dt">Repr</span> <span class="dt">Prod</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb7-8" data-line-number="8">      toHom (<span class="dt">Prod</span> a _) <span class="dt">InL</span>  <span class="fu">=</span> a</a>
<a class="sourceLine" id="cb7-9" data-line-number="9">      toHom (<span class="dt">Prod</span> _ a) <span class="dt">InR</span> <span class="fu">=</span> b</a>
<a class="sourceLine" id="cb7-10" data-line-number="10">      toF hom <span class="fu">=</span> <span class="dt">Prod</span> (hom <span class="dt">InL</span>) (hom <span class="dt">InR</span>)</a></code></pre></div>
<p>This is all quite well, but what about infinite data structures? This is Haskell! we want those too.</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb8-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">Forever</span> a <span class="fu">=</span> <span class="dt">Cons</span> a (<span class="dt">Forever</span> a)</a>
<a class="sourceLine" id="cb8-2" data-line-number="2">                   <span class="kw">deriving</span> (<span class="dt">Functor</span>)</a>
<a class="sourceLine" id="cb8-3" data-line-number="3"></a>
<a class="sourceLine" id="cb8-4" data-line-number="4">    <span class="kw">data</span> <span class="dt">Nat</span> <span class="fu">=</span> <span class="dt">Z</span> <span class="fu">|</span> <span class="dt">S</span> <span class="dt">Nat</span></a>
<a class="sourceLine" id="cb8-5" data-line-number="5">    <span class="kw">type</span> <span class="kw">instance</span> <span class="dt">Obj</span> <span class="dt">Forever</span> <span class="fu">=</span> <span class="dt">Nat</span></a>
<a class="sourceLine" id="cb8-6" data-line-number="6"></a>
<a class="sourceLine" id="cb8-7" data-line-number="7">    <span class="kw">instance</span> <span class="dt">Repr</span> <span class="dt">Forever</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb8-8" data-line-number="8">      toHom (<span class="dt">Cons</span> a as) <span class="dt">Z</span> <span class="fu">=</span> a</a>
<a class="sourceLine" id="cb8-9" data-line-number="9">      toHom (<span class="dt">Cons</span> a as) (<span class="dt">S</span> n) <span class="fu">=</span> toHom as n</a>
<a class="sourceLine" id="cb8-10" data-line-number="10"></a>
<a class="sourceLine" id="cb8-11" data-line-number="11">      toF f <span class="fu">=</span> cs z</a>
<a class="sourceLine" id="cb8-12" data-line-number="12">        <span class="kw">where</span> cs n <span class="fu">=</span> <span class="dt">Cons</span> (f n) (cs (<span class="dt">S</span> n))</a></code></pre></div>
<p>Since <code>Forever</code> goes, well, forever. It can be keyed with natural numbers, which we represent here with <code>Nat</code>. Then <code>toHom</code> is classic recursion and <code>toF</code> is classic co-recursion.</p>
<p>There are tons more of these, but hopefully now you’re getting the idea. Here’s another cool thought</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb9-1" data-line-number="1"><span class="ot">    switch ::</span> (<span class="dt">Repr</span> f, <span class="dt">Functor</span> g) <span class="ot">=&gt;</span> g (f a) <span class="ot">-&gt;</span> f (g a)</a>
<a class="sourceLine" id="cb9-2" data-line-number="2">    switch g <span class="fu">=</span> toF <span class="fu">$</span> \obj <span class="ot">-&gt;</span> fmap (<span class="fu">$</span> obj) hom</a>
<a class="sourceLine" id="cb9-3" data-line-number="3">      <span class="kw">where</span> hom <span class="fu">=</span> fmap toHom g</a></code></pre></div>
<p>Wait a moment, what if <code>f</code> and <code>g</code> where both <code>Repr</code> instances? Then</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb10-1" data-line-number="1">    switch <span class="fu">.</span> switch <span class="fu">=</span> id</a></code></pre></div>
<p>Neat! We can use representable functors to switch around functors.</p>
<p>Now, what about applicatives, can we use a representative functor to build one?</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb11-1" data-line-number="1">    <span class="co">-- To keep type classes from getting confused</span></a>
<a class="sourceLine" id="cb11-2" data-line-number="2">    <span class="kw">newtype</span> <span class="dt">Wrap</span> f a <span class="fu">=</span> <span class="dt">Wrap</span> {<span class="ot">unWrap ::</span> f a}</a>
<a class="sourceLine" id="cb11-3" data-line-number="3"></a>
<a class="sourceLine" id="cb11-4" data-line-number="4">    <span class="kw">instance</span> (<span class="dt">Repr</span> f) <span class="ot">=&gt;</span> <span class="dt">Applicative</span> (<span class="dt">Wrap</span> f) <span class="kw">where</span></a>
<a class="sourceLine" id="cb11-5" data-line-number="5">      pure    <span class="fu">=</span> toF <span class="fu">.</span> const</a>
<a class="sourceLine" id="cb11-6" data-line-number="6">      f <span class="fu">&lt;*&gt;</span> a <span class="fu">=</span> toF <span class="fu">$</span> \obj <span class="ot">-&gt;</span> toHom f obj <span class="fu">$</span> toHom a obj</a></code></pre></div>
<p>So we can actually build out applicatives from a representable functor. How about monads?</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb12-1" data-line-number="1">    <span class="kw">instance</span> (<span class="dt">Repr</span> f) <span class="ot">=&gt;</span> <span class="dt">Monad</span> (<span class="dt">Wrap</span> f) <span class="kw">where</span></a>
<a class="sourceLine" id="cb12-2" data-line-number="2">      return <span class="fu">=</span> toF <span class="fu">.</span> const</a>
<a class="sourceLine" id="cb12-3" data-line-number="3">      m <span class="fu">&gt;&gt;=</span> f <span class="fu">=</span> toF <span class="fu">$</span> \obj <span class="ot">-&gt;</span> (<span class="fu">$</span>obj) <span class="fu">.</span> toHom <span class="fu">.</span> f <span class="fu">$</span> toHom m obj</a></code></pre></div>
<p>Notice how these are working? The functor and monad are defined “pointwise”. Basically we’re applying each function at a “point” in our functor’s underlying structure and then peeking at the result at that point.</p>
<p>If we translate this into <code>Forever</code> functor, our applicative instance would correspond to taking a stream of functions, and zipping it with a stream of values. Our monad instance would do the same, and select the point in the same position in the resulting list. That’s why we often refer to these as zippy monads.</p>
<p>Well hopefully I’ve convinced you that representable functors are interesting, remember, we were able to build all of this from a simple isomorphism with <code>Hom</code>. Cool right?</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Mon, 21 Oct 2013 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2013-10-21-representable-functors.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Please Don't Learn Category Theory to Learn Haskell</title>
    <link>http://jozefg.bitbucket.org/posts/2013-10-14-please-dont-learn-cat-theory.html</link>
    <description><![CDATA[<div class="info">
    Posted on October 14, 2013
    
</div>
<div class="info">
    
    Tags: <a href="/tags/haskell.html">haskell</a>, <a href="/tags/opinions.html">opinions</a>
    
</div>

<p>I spend a lot of time talking about Haskell. Trying to get imperative programmers to learn Haskell is hard, people are stubborn and often have misconceptions about functional programming.</p>
<p>One of the most common excuses I hear is</p>
<blockquote>
<p><strong>I don’t know enough math to learn Haskell, don’t you need to know category theory?</strong></p>
</blockquote>
<p>No! In fact, please don’t go learn category theory to learn Haskell. Why not?</p>
<p>The Haskell standard library makes shallow use of quite deep and complex ideas. Yes it uses the words Monad, Functor, and Category. But you don’t need to have any idea what these words mean to use them. In fact, try substituting</p>
<pre><code> Monad    -&gt; FuzzyWuzzy
 Functor  -&gt; Banana
 Category -&gt; Cheerios</code></pre>
<p>And you’ll still be able to learn/use Haskell just fine. In fact, there’s a lovely series of <a href="http://blog.tmorris.net/posts/20-intermediate-haskell-exercises/">problems</a> that do just this.</p>
<blockquote>
<p><strong>But wait, why did they even bother with the names then?</strong></p>
</blockquote>
<p>Because that’s where the idea comes from. The abstractions in Haskell are sometimes inspired by math. There’s no argument there. And the people who designed Haskell decided they weren’t going to pretend they didn’t use math. But just like how Ruby was inspired by Lisp and Smalltalk, you don’t need to learn the source of some abstraction to enjoy it.</p>
<p>In fact, if you want to see a bunch of category theory inspired abstractions, check out some of Edward Kmett’s libraries. They’re just littered with big-n-scary words. However, you can still use <code>lens</code> (a super useful library) without understanding what it means to “downstar a functor into a profunctor”. And tons do.</p>
<blockquote>
<p><strong>Is there any point in learning category theory then?</strong></p>
</blockquote>
<p>I’d say so, it’s a cool piece of mathematics to start with. And who knows, plenty of people find it useful to look to category theory for reasoning about abstractions.</p>
<p>I decided to pick up a few books in June and have been thoroughly enjoying it. Not because it suddenly made me better at Haskell but because I like math and it provides a good language for talking about some concepts.</p>
<p>Who knows, maybe you’re a budding category theorist.</p>
<blockquote>
<p><strong>What do I need to know before Haskell then?</strong></p>
</blockquote>
<p>Well um, not much. In fact, the less you know the better! I came to Haskell from primarily imperative languages (C, Perl, Java) and the biggest problem I had was trying not to write Perl in Haskell.</p>
<p>The only thing I found terribly helpful was already understanding what a type was. And I think that can be picked up pretty quickly.</p>
<p>Good luck :)</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Mon, 14 Oct 2013 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2013-10-14-please-dont-learn-cat-theory.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>forall Means All!</title>
    <link>http://jozefg.bitbucket.org/posts/2013-10-06-forall-means-all.html</link>
    <description><![CDATA[<div class="info">
    Posted on October  6, 2013
    
</div>
<div class="info">
    
    Tags: <a href="/tags/haskell.html">haskell</a>, <a href="/tags/types.html">types</a>
    
</div>

<p>It seems like the every week on Stack Overflow there’s at least two questions about higher rank polymorphism (RankNTypes). So here’s a brief description of what they are and how to use them.</p>
<p>First, to turn them on</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb1-1" data-line-number="1">    <span class="ot">{-# LANGUAGE RankNTypes #-}</span></a></code></pre></div>
<p>Now to write one</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb2-1" data-line-number="1"><span class="ot">     demo ::</span> (forall a<span class="fu">.</span> a <span class="ot">-&gt;</span> <span class="dt">Int</span>) <span class="ot">-&gt;</span> <span class="dt">Int</span></a>
<a class="sourceLine" id="cb2-2" data-line-number="2">     demo f <span class="fu">=</span> f <span class="ch">&#39;a&#39;</span> <span class="fu">+</span> f <span class="dt">True</span></a></code></pre></div>
<p>Now that <code>forall</code> means “this function is polymorphic and can be applied to any argument”. Notice we can’t do this without rank N types,</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb3-1" data-line-number="1"><span class="ot">    uhoh ::</span> (a <span class="ot">-&gt;</span> <span class="dt">Int</span>) <span class="ot">-&gt;</span> <span class="dt">Int</span></a>
<a class="sourceLine" id="cb3-2" data-line-number="2">    uhoh f <span class="fu">=</span> f <span class="ch">&#39;a&#39;</span> <span class="fu">+</span> f <span class="dt">True</span> <span class="co">-- Error cannot unify &#39;a&#39; with Char</span></a></code></pre></div>
<p>Here’s how not to use it</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" data-line-number="1">    demo id</a></code></pre></div>
<p><code>id</code> here unifies with <code>Int -&gt; Int</code> which isn’t the necessary <code>forall a. a -&gt; Int</code>. To use it,</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb5-1" data-line-number="1">    demo (const <span class="dv">1</span>)</a></code></pre></div>
<p>Now this seems pretty clear right? It’s just to make it possible to pass polymorphic functions to other functions. Easy peasy :)</p>
<p>Now what does this mean?</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb6-1" data-line-number="1">    <span class="kw">data</span> <span class="dt">Tricky</span> <span class="fu">=</span> <span class="dt">Tricky</span> (forall x<span class="fu">.</span> x <span class="ot">-&gt;</span> x)</a></code></pre></div>
<p>Well you’d be right if you realized that the only sane instantiation of this is</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb7-1" data-line-number="1">    t <span class="fu">=</span> <span class="dt">Tricky</span> id</a></code></pre></div>
<p>We need a function</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb8-1" data-line-number="1"><span class="ot">    arg ::</span> a <span class="ot">-&gt;</span> a</a></code></pre></div>
<p>It’s pretty clear that the only sane version of <code>arg</code> is <code>id</code>.</p>
<p>A harder one,</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb9-1" data-line-number="1">    <span class="kw">type</span> <span class="dt">ReallyTricky</span> a b <span class="fu">=</span> forall f<span class="fu">.</span> <span class="dt">Functor</span> f <span class="ot">=&gt;</span> (a <span class="ot">-&gt;</span> b) <span class="ot">-&gt;</span> f a <span class="ot">-&gt;</span> f b</a></code></pre></div>
<p>That’s right it just means that anything of type <code>ReallyTricky</code> knows how to take some arbitrary function, and lift it into <em>any</em> functor. And the caller gets to choose which one.</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb10-1" data-line-number="1"><span class="ot">    t ::</span> <span class="dt">ReallyTricky</span></a>
<a class="sourceLine" id="cb10-2" data-line-number="2">    t <span class="fu">=</span> fmap</a></code></pre></div>
<p>That’s it! Just remember that <code>forall</code> is universal quantification. This means that you have to be able to support all possible instationations of that variable and the caller will choose which one.</p>
<p>Now suppose you want it the other way, you choose the instantiation and the caller has to handle it generically. Then you want <code>existential</code> quantification. A subject for another post.</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Sun, 06 Oct 2013 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2013-10-06-forall-means-all.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Naive Map Isn't So Naive</title>
    <link>http://jozefg.bitbucket.org/posts/2013-10-01-lazy-recursion.html</link>
    <description><![CDATA[<div class="info">
    Posted on October  1, 2013
    
</div>
<div class="info">
    
    Tags: <a href="/tags/haskell.html">haskell</a>
    
</div>

<p>One of the most beloved functions in functional programming languages is <code>map</code>. It can be defined like this:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb1-1" data-line-number="1"><span class="ot">    map ::</span> (a <span class="ot">-&gt;</span> b) <span class="ot">-&gt;</span> [a] <span class="ot">-&gt;</span> [b]</a>
<a class="sourceLine" id="cb1-2" data-line-number="2">    map f (x<span class="fu">:</span>xs) <span class="fu">=</span> f x <span class="fu">:</span> map f xs</a>
<a class="sourceLine" id="cb1-3" data-line-number="3">    map _ []     <span class="fu">=</span> []</a></code></pre></div>
<p>However in a lot of languages, writing <code>map</code> like this is a no-no. It’s not tail recursive! For example in OCaml</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode ocaml"><code class="sourceCode ocaml"><a class="sourceLine" id="cb2-1" data-line-number="1">    # <span class="kw">let</span> <span class="kw">rec</span> my_map f = <span class="kw">function</span></a>
<a class="sourceLine" id="cb2-2" data-line-number="2">       | [] -&gt; []</a>
<a class="sourceLine" id="cb2-3" data-line-number="3">       | x :: xs -&gt; f x :: my_map f xs</a>
<a class="sourceLine" id="cb2-4" data-line-number="4"></a>
<a class="sourceLine" id="cb2-5" data-line-number="5">    # my_map ((+) <span class="dv">1</span>) list_from_0_to_5000000</a>
<a class="sourceLine" id="cb2-6" data-line-number="6">    ... Wait a bit ...</a>
<a class="sourceLine" id="cb2-7" data-line-number="7">    Error: Stackoverflow</a></code></pre></div>
<p>Urk! That’s annoying. The problem is that we have to make a recursive function call that can’t be compiled down to a loop (tail recursion). Well, let’s look at how <code>map</code> is defined in Haskell to avoid this problem</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb3-1" data-line-number="1">    <span class="co">-- In Base</span></a>
<a class="sourceLine" id="cb3-2" data-line-number="2"><span class="ot">    map ::</span> (a <span class="ot">-&gt;</span> b) <span class="ot">-&gt;</span> [a] <span class="ot">-&gt;</span> [b]</a>
<a class="sourceLine" id="cb3-3" data-line-number="3">    map f (x<span class="fu">:</span>xs) <span class="fu">=</span> f x <span class="fu">:</span> map f xs</a>
<a class="sourceLine" id="cb3-4" data-line-number="4">    map _ []     <span class="fu">=</span> []</a></code></pre></div>
<p>Wait, isn’t this bad? We just saw how this isn’t tail recursive!</p>
<p>The thing is, in Haskell things are lazy. <code>map (+1) [1..10000]</code> returns a thunk. Inside that thunk is something like this</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" data-line-number="1">    (<span class="fu">:</span>) <span class="dv">1</span> {thunk to get rest <span class="kw">of</span> list}</a></code></pre></div>
<p>So this takes constant space! After all, none of those extra stack frames are used because <code>:</code> doesn’t evaluate its arguments. This means that a lot of not tail recursive functions in Haskell still take constant space, however, you have to be careful about consuming the results.</p>
<p>Take for example <code>sum</code>.</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb5-1" data-line-number="1"><span class="ot">    sum ::</span> [<span class="dt">Integer</span>] <span class="ot">-&gt;</span> <span class="dt">Integer</span></a>
<a class="sourceLine" id="cb5-2" data-line-number="2">    sum (x<span class="fu">:</span>xs) <span class="fu">=</span> x <span class="fu">+</span> sum xs</a>
<a class="sourceLine" id="cb5-3" data-line-number="3">    sum []     <span class="fu">=</span> <span class="dv">0</span></a></code></pre></div>
<p>Now this also isn’t tail recursive, but there’s a problem: <code>+</code> is strict. By this I mean that to evaluate <code>a+b</code> you must first evaluate <code>a</code> and then <code>b</code>. This means that to evaluate <code>x + sum xs</code> we have to evaluate <code>sum xs</code>.</p>
<p>Urk, now we’re building up a big pile of expressions, something like</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb6-1" data-line-number="1">    a <span class="fu">+</span> sum (b<span class="fu">:</span>c<span class="fu">:</span>d<span class="fu">:</span>e<span class="fu">:</span>[])</a>
<a class="sourceLine" id="cb6-2" data-line-number="2">    a <span class="fu">+</span> (b <span class="fu">+</span> sum (c<span class="fu">:</span>d<span class="fu">:</span>e<span class="fu">:</span>[]))</a>
<a class="sourceLine" id="cb6-3" data-line-number="3">    a <span class="fu">+</span> (b <span class="fu">+</span> (c <span class="fu">+</span> sum (d<span class="fu">:</span>e<span class="fu">:</span>[])))</a>
<a class="sourceLine" id="cb6-4" data-line-number="4">    a <span class="fu">+</span> (b <span class="fu">+</span> (c <span class="fu">+</span> (d <span class="fu">+</span> sum (e<span class="fu">:</span>[]))))</a>
<a class="sourceLine" id="cb6-5" data-line-number="5">    a <span class="fu">+</span> (b <span class="fu">+</span> (c <span class="fu">+</span> (d <span class="fu">+</span> (e <span class="fu">+</span> <span class="dv">0</span>))))</a></code></pre></div>
<p>Now we can see why this will blow up, it’s building up a huge expression before we can evaluate anything. Hi stack overflow.</p>
<p>Now this is when we do want the tail recursive function like <code>foldl'</code> to keep things in constant space.</p>
<h3 id="conclusion">Conclusion</h3>
<p>When you construct something with <code>:</code> for example, it’s possible to evaluate the head without evaluating the tail. Similarly with most constructors. With things like this, it’s possible to keep naive recursion in constant space.</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Tue, 01 Oct 2013 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2013-10-01-lazy-recursion.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Logic and Continuations</title>
    <link>http://jozefg.bitbucket.org/posts/2013-09-12-five-things-I-hate.html</link>
    <description><![CDATA[<div class="info">
    Posted on September 12, 2013
    
</div>
<div class="info">
    
    Tags: <a href="/tags/haskell.html">haskell</a>
    
</div>

<p>In Haskell there’s a monad known as <code>Cont</code>. Most people don’t use it, but it’s pretty cool. The basic idea is that</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb1-1" data-line-number="1">     <span class="dt">Cont</span> r a <span class="fu">=</span> <span class="dt">Cont</span> {<span class="ot">runCont ::</span> (a <span class="ot">-&gt;</span> r) <span class="ot">-&gt;</span> r}</a></code></pre></div>
<p>The intuition being that that <code>(a -&gt; r)</code> term is the “rest of the program”. You feed it the type it is expecting and it will happily run the rest of your computation.</p>
<h3 id="deriving-monad-cont-r">Deriving Monad (Cont r)</h3>
<p><code>return</code> is easy</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb2-1" data-line-number="1">    return a <span class="fu">=</span> <span class="dt">Cont</span> (<span class="fu">$</span>a)</a></code></pre></div>
<p>or</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb3-1" data-line-number="1">    return a <span class="fu">=</span> <span class="dt">Cont</span> <span class="fu">$</span> \c <span class="ot">-&gt;</span> c a</a></code></pre></div>
<p>Bind is a little trickier</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" data-line-number="1">    (<span class="dt">Cont</span> c) <span class="fu">&gt;&gt;=</span> f <span class="fu">=</span> <span class="dt">Cont</span> (\rest <span class="ot">-&gt;</span> c <span class="fu">$</span> \a <span class="ot">-&gt;</span> runCont (f a) rest)</a></code></pre></div>
<p>Think of this as feeding the continuation <code>c :: (a -&gt; r) -&gt; r</code> a function made by with a continuation <code>runCont . f :: a -&gt; (b -&gt; r) -&gt; r</code> and <code>rest :: (b -&gt; r)</code> to make something of type <code>a -&gt; r</code>.</p>
<p>Yeah it hurts your head a little.</p>
<p>Now let’s talk about seeing into the future.</p>
<h3 id="back-to-the-future">Back to the Future</h3>
<p>First things first, to conform with how the MTL does stuff</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb5-1" data-line-number="1">     cont <span class="fu">=</span> <span class="dt">Cont</span></a></code></pre></div>
<p>Because in the real <code>Control.Monad.Cont</code>, there’s a monad transformer and a type synonym</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb6-1" data-line-number="1">     <span class="kw">type</span> <span class="dt">Cont</span> r a <span class="fu">=</span> <span class="dt">ContT</span> r <span class="dt">Identity</span> a</a></code></pre></div>
<p>or something like that.</p>
<p>Let’s try a simple application of <code>Cont</code>. Suppose we have a function of type <code>[a -&gt; Bool] -&gt; [a] -&gt; [a]</code> and we want to return the longest list of <code>a</code>s that satisfies one of the predicates in our list. Yeah it’s contrived but I’ll show you a more realistic example in a second:</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb7-1" data-line-number="1">     longest preds as <span class="fu">=</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb7-2" data-line-number="2">       p <span class="ot">&lt;-</span> selectPred preds</a>
<a class="sourceLine" id="cb7-3" data-line-number="3">       return (filter p as)</a></code></pre></div>
<p>Now we just need to define <code>selectPred</code> so that it can “know” which predicate will return the longest list</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb8-1" data-line-number="1">    selectPred ps <span class="fu">=</span> cont <span class="fu">$</span> \c <span class="ot">-&gt;</span> maxBy length <span class="fu">.</span> map c <span class="fu">$</span> ps</a></code></pre></div>
<p>That’s it. It’s actually running the program with each possible value and then returns the best result. Cool right?</p>
<p>Note that it’s kind of important that things are pure here. If you have an <code>unsafePerformIO</code> and you start backtracking things get hairy. However, since you can toss around <code>IO</code>s without evaluating them, eg</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb9-1" data-line-number="1">     const <span class="dv">1</span> (print <span class="st">&quot;foo&quot;</span>)</a></code></pre></div>
<p>Doesn’t print <code>foo</code> or anything, you can have <code>ContT</code> layered over IO.</p>
<h4 id="logic-framework">Logic Framework</h4>
<p>Now let’s use this to create a simple framework for non-deterministic logic programming. Some skeleton code:</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb10-1" data-line-number="1">    <span class="ot">{-# LANGUAGE RankNTypes #-}</span></a>
<a class="sourceLine" id="cb10-2" data-line-number="2">    <span class="kw">newtype</span> <span class="dt">Logic</span> a <span class="fu">=</span> <span class="dt">Logic</span> {<span class="ot">runLogic ::</span> forall r<span class="fu">.</span>  <span class="dt">Cont</span> (<span class="dt">Maybe</span> r) a}</a>
<a class="sourceLine" id="cb10-3" data-line-number="3">    <span class="kw">instance</span> <span class="dt">Monad</span> <span class="dt">Logic</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb10-4" data-line-number="4">      return a <span class="fu">=</span> <span class="dt">Logic</span> <span class="fu">$</span> return a</a>
<a class="sourceLine" id="cb10-5" data-line-number="5">      (<span class="dt">Logic</span> c) <span class="fu">&gt;&gt;=</span> f  <span class="fu">=</span> <span class="dt">Logic</span> <span class="fu">$</span> c <span class="fu">&gt;&gt;=</span> runLogic <span class="fu">.</span> f</a></code></pre></div>
<p>The <code>RankNTypes</code> basically says a logical computation can’t make assumptions about the result, which is pretty reasonable. The monad instance is just relying on the underlying <code>Cont</code> instance. Now we want three functions:</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb11-1" data-line-number="1"><span class="ot">    amb ::</span> [a] <span class="ot">-&gt;</span> <span class="dt">Logic</span> a</a>
<a class="sourceLine" id="cb11-2" data-line-number="2"><span class="ot">    disconj ::</span> <span class="dt">Logic</span> a <span class="ot">-&gt;</span> <span class="dt">Logic</span> a <span class="ot">-&gt;</span> <span class="dt">Logic</span> a</a>
<a class="sourceLine" id="cb11-3" data-line-number="3"><span class="ot">    backtrack ::</span> <span class="dt">Logic</span> ()</a></code></pre></div>
<p>where <code>backtrack</code> backtracks to the nearest <code>amb</code> and tries the next element and <code>disconj</code> simply joins together two propositions and chooses an element from one that won’t fail (return <code>Nothing</code>).</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb12-1" data-line-number="1">    backtrack <span class="fu">=</span> <span class="dt">Logic</span> (cont <span class="fu">$</span> const <span class="dt">Nothing</span>)</a>
<a class="sourceLine" id="cb12-2" data-line-number="2">    disconj (<span class="dt">Logic</span> a) (<span class="dt">Logic</span> b) <span class="fu">=</span> <span class="dt">Logic</span> (cont <span class="fu">$</span> \c <span class="ot">-&gt;</span> runCont a c <span class="ot">`mplus`</span> runCont b c)</a>
<a class="sourceLine" id="cb12-3" data-line-number="3">    amb as <span class="fu">=</span> <span class="dt">Logic</span> (cont <span class="fu">$</span> \c <span class="ot">-&gt;</span> join <span class="fu">.</span> find isJust <span class="fu">.</span> map (c<span class="fu">$</span>) <span class="fu">$</span> as)</a></code></pre></div>
<p>Note: with RankNTypes weird things can happen with <code>.</code> for example, using <code>Logic . cont</code> is ill typed presumably because GHC restricts the <code>Cont</code> being fed to <code>Logic</code>.</p>
<p>Now these actually map nicely to an existing typeclass.</p>
<div class="sourceCode" id="cb13"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb13-1" data-line-number="1">     <span class="kw">instance</span> <span class="dt">MonadPlus</span> <span class="dt">Logic</span> <span class="kw">where</span></a>
<a class="sourceLine" id="cb13-2" data-line-number="2">       mzero <span class="fu">=</span> backtrack</a>
<a class="sourceLine" id="cb13-3" data-line-number="3">       mplus <span class="fu">=</span> disconj</a></code></pre></div>
<p>Also helpful is</p>
<div class="sourceCode" id="cb14"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb14-1" data-line-number="1"><span class="ot">    evaluate ::</span> <span class="dt">Logic</span> a <span class="ot">-&gt;</span> <span class="dt">Maybe</span> a</a>
<a class="sourceLine" id="cb14-2" data-line-number="2">    evaluate <span class="fu">=</span> flip runCont <span class="dt">Just</span> <span class="fu">.</span> runLogic</a></code></pre></div>
<p>So let’s try it out:</p>
<div class="sourceCode" id="cb15"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb15-1" data-line-number="1">     main <span class="fu">=</span> print <span class="fu">.</span> evaluate <span class="fu">$</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb15-2" data-line-number="2">       a <span class="ot">&lt;-</span> amb [<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>]<span class="ot"> ::</span> <span class="dt">Logic</span> <span class="dt">Integer</span></a>
<a class="sourceLine" id="cb15-3" data-line-number="3">       b <span class="ot">&lt;-</span> amb [<span class="dv">4</span>, <span class="dv">5</span>, <span class="dv">6</span>]</a>
<a class="sourceLine" id="cb15-4" data-line-number="4">       when (a <span class="fu">+</span> b <span class="fu">/=</span> <span class="dv">9</span>) mzero</a>
<a class="sourceLine" id="cb15-5" data-line-number="5">       return (a, b)</a></code></pre></div>
<p>and perhaps a helpful combinator</p>
<div class="sourceCode" id="cb16"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb16-1" data-line-number="1">     assert <span class="fu">=</span> flip when mzero</a></code></pre></div>
<p>makes</p>
<div class="sourceCode" id="cb17"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb17-1" data-line-number="1">     main <span class="fu">=</span> print <span class="fu">.</span> evaluate <span class="fu">$</span> <span class="kw">do</span></a>
<a class="sourceLine" id="cb17-2" data-line-number="2">       a <span class="ot">&lt;-</span> amb <span class="st">&quot;floor&quot;</span></a>
<a class="sourceLine" id="cb17-3" data-line-number="3">       b <span class="ot">&lt;-</span> amb <span class="st">&quot;bar&quot;</span></a>
<a class="sourceLine" id="cb17-4" data-line-number="4">       assert (a<span class="fu">==</span>b)</a>
<a class="sourceLine" id="cb17-5" data-line-number="5">       return (a, b)</a></code></pre></div>
<p>And there you have it, using continuations we have created a logic DSL. The interesting bit is that each <code>amb</code> is actually running the code multiple times and “seeing into the future”. Once it has which element will actually return a desirable result it pops it back. Nifty.</p>
<h3 id="an-exercise-to-the-reader">An exercise to the reader</h3>
<p>A useful exercise is to add 1 of 2 combinators</p>
<div class="sourceCode" id="cb18"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb18-1" data-line-number="1"><span class="ot">    cut ::</span> <span class="dt">Logic</span> ()</a>
<a class="sourceLine" id="cb18-2" data-line-number="2"><span class="ot">    interleave ::</span> <span class="dt">Logic</span> a <span class="ot">-&gt;</span> <span class="dt">Logic</span> b <span class="ot">-&gt;</span> <span class="dt">Logic</span> (a, b)</a></code></pre></div>
<p><code>cut</code> doesn’t backtrack. To implement this, you’ll have to use <code>callCC</code> and pass the escape continuation around and call it from <code>cut</code> if something tries to backtrack past it.</p>
<p><code>interleave</code> is also cool, it’s fair disjunction. Our current setup can’t handle</p>
<div class="sourceCode" id="cb19"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb19-1" data-line-number="1">     a <span class="ot">&lt;-</span> amb [<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">4</span>, <span class="dv">5</span>]</a>
<a class="sourceLine" id="cb19-2" data-line-number="2">     b <span class="ot">&lt;-</span> amb [<span class="dv">1</span><span class="fu">..</span>]</a>
<a class="sourceLine" id="cb19-3" data-line-number="3">     assert ( a <span class="fu">==</span> <span class="dv">2</span> )</a></code></pre></div>
<p>It’ll get stuck fiddling with the value of <code>b</code>! With <code>interleave</code> we’d type</p>
<div class="sourceCode" id="cb20"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb20-1" data-line-number="1">     (a, b) <span class="ot">&lt;-</span> interleave (amb [<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>]) (amb [<span class="dv">1</span><span class="fu">..</span>])</a></code></pre></div>
<p>and it will give us pairs “fairly” by returning pairs so that the probability of <code>(n, m)</code> being returned when <code>n</code> is <code>a</code> elements into the first computation and <code>m</code> is <code>b</code> elements into the second is <code>k / (a + b)</code>.</p>
<p>Good luck!</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Thu, 12 Sep 2013 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2013-09-12-five-things-I-hate.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Teens and Functional Programming</title>
    <link>http://jozefg.bitbucket.org/posts/2013-09-08-teens-and-fp.html</link>
    <description><![CDATA[<div class="info">
    Posted on September  8, 2013
    
</div>
<div class="info">
    
    Tags: <a href="/tags/opinions.html">opinions</a>
    
</div>

<p>As a teenager who spends most of his time programming, I spend a lot of time interacting/teaching other teens interested in programming.</p>
<p>One thing that’s always bothered me is the lack of fellow teenage functional programmers. I’ve been wondering whether this is simply chance? Or is there something that makes functional programming bad for beginners? Especially teenage ones.</p>
<p>Well first let’s formally define what I mean by a functional language, the definition is always a bit fuzzy. When I refer to a functional language, I mean a language that:</p>
<ul>
<li>Has primarily immutable data (Convention or enforced)</li>
<li>Functions as first class values</li>
<li>Functions are primarily “pure functions”</li>
</ul>
<p>Notice that I left out</p>
<ul>
<li>Type Systems</li>
<li>Algebraic data types + Pattern matching</li>
<li>Purity</li>
</ul>
<h3 id="immutability">Immutability</h3>
<p>Perhaps immutability is the problem. It’s certainly weird to experienced imperative programmers. But what about for beginners?</p>
<p>I’d argue immutability is actually pretty natural for a new programmer. It means variables are just names for data. No more of that confusing “a variable is like a box that you can put values in” explanation. Especially once you get into subtleties like indirection mutation looks less appealing.</p>
<p>In the first course for computer science students at the University of Minnesota, there is a whole quiz filled with problems like this in Python:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb1-1" data-line-number="1">    <span class="co"># What is the output of</span></a>
<a class="sourceLine" id="cb1-2" data-line-number="2">    a <span class="op">=</span> [<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>]</a>
<a class="sourceLine" id="cb1-3" data-line-number="3">    b <span class="op">=</span> a[<span class="dv">1</span>:]</a>
<a class="sourceLine" id="cb1-4" data-line-number="4">    b[<span class="dv">1</span>] <span class="op">=</span> <span class="dv">4</span></a>
<a class="sourceLine" id="cb1-5" data-line-number="5">    <span class="bu">print</span> a</a></code></pre></div>
<p>Once you have a whole quiz devoted to a topic, it’s safe to say that it’s confusing.</p>
<p>This class also taught Scheme, when it came time to explain <code>let</code>, the professor simply said</p>
<blockquote>
<p>This is <code>let</code>.</p>
<pre><code>(let ((a 1))
    (+ 1 a))</code></pre>
<p>Just substitute <code>a</code> for 1 within the parens.</p>
</blockquote>
<p>and that was that.</p>
<p>I’d posit that the reason is that with immutability you can simply substitute a name for its value and have unchanged semantics. With mutable variables, a variable is more than just it’s value.</p>
<p>Now that’s not to say immutability doesn’t get weird eventually, purely functional data structures do sometimes require some mental gymnastics, but there is certainly not more mental overhead than with imperative programming’s pervasive mutation.</p>
<h3 id="first-class-functions">First Class Functions</h3>
<p>I find it hard to believe that first class functions are the problem since they’re not that unique anymore. Python, Ruby, and JavaScript all have them and they’re hugely popular with beginners.</p>
<p>The other thing is that you can largely ignore them until you need them. No one will make you use map, you could write the stupid repetitive recursion out every time. Additionally, many languages provide some sort of construct to let you avoid <code>map</code>, <code>filter</code>, or whatever. For example, in Haskell</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb3-1" data-line-number="1">    filter even <span class="fu">.</span> map (<span class="fu">+</span><span class="dv">1</span>) <span class="fu">$</span> [<span class="dv">1</span><span class="fu">..</span><span class="dv">10</span>]</a>
<a class="sourceLine" id="cb3-2" data-line-number="2">    [x <span class="fu">+</span> <span class="dv">1</span> <span class="fu">|</span> x <span class="ot">&lt;-</span> [<span class="dv">1</span><span class="fu">..</span><span class="dv">10</span>], even x]</a></code></pre></div>
<p>Perfect for a beginner. In fact, Python stole these for precisely this reason.</p>
<p>Finally, they actually alleviate a lot of complexity. Look at anonymous classes and the strategy pattern. The whole thing is a very large, ugly hack for dealing with the lack of first class functions.</p>
<h3 id="pure-functions">Pure Functions</h3>
<p>This one isn’t too hard to argue. If you have some function <code>f</code>, if you call it</p>
<pre><code>a = f(1);
b = f(1);</code></pre>
<p>You’d really expect it to give you back the same thing twice. This has what people have come to expect from math. It’s much the same argument that I made for immutability, with a pure function there’s all sorts of nice assumptions you can make about it, once again a function application is just becomes a name for the resulting value. This is different than a function call in python where the actual computation is important because it has side effects.</p>
<p>I said “primarily pure” because some things are really impure, the classic example being <code>readLine</code>.</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode python"><code class="sourceCode python"><a class="sourceLine" id="cb5-1" data-line-number="1">    <span class="bu">print</span> <span class="st">&quot;Enter your age:&quot;</span><span class="op">;</span></a>
<a class="sourceLine" id="cb5-2" data-line-number="2">    age <span class="op">=</span> readLine()<span class="op">;</span></a>
<a class="sourceLine" id="cb5-3" data-line-number="3">    <span class="bu">print</span> <span class="st">&quot;Enter your height:&quot;</span><span class="op">;</span></a>
<a class="sourceLine" id="cb5-4" data-line-number="4">    height <span class="op">=</span> readLine()<span class="op">;</span></a></code></pre></div>
<p>Now if we’d hope that <code>age</code> and <code>height</code> contain different values. In Haskell we have monads for this, but those are notoriously hard to understand. Instead, pragmatic impurity is probably the best course for a beginner’s language. Much like Scheme.</p>
<h2 id="but-objects">But Objects!</h2>
<p>Now I know that someone is thinking, “But object orientation!!”. To them I say, you’re right: for some set of problems object orientation is a better model for a problem. But it’s a far smaller set of problems than you’d think.</p>
<p>It’s important for a beginner to be exposed to multiple paradigms, but I see no reason why the first paradigm shouldn’t be functional. In fact, I’ve outlined several reasons why it <em>should</em> be functional.</p>
<hr />
<h1 id="but-which-one">But Which One?</h1>
<p>So if you’re some random teen about to start functional programming, which language would you choose?</p>
<p>Some of the more popular languages that fit my definition of functional:</p>
<ul>
<li>Scheme/Racket</li>
<li>Clojure</li>
<li>Haskell</li>
<li>Erlang</li>
<li>Scala</li>
<li>SML</li>
<li>OCaml</li>
<li>F#</li>
</ul>
<p>Notice that most of the common “pseudo-functional” languages (JavaScript, Ruby, etc) fail the first constraint of immutability. I also chose to leave off some of the more research oriented languages (Coq, Agda, etc) because we’re talking about beginners here.</p>
<p>Now for a language to be good for a beginner it has to</p>
<ul>
<li>Not have an overly complicated type system</li>
<li>Have an implementation with good error messages</li>
<li>Have lots of libraries, particularly web/game frameworks</li>
<li>A batteries included standard library to get up and running with</li>
<li>Have good community support <strong>for newcomers</strong></li>
</ul>
<h3 id="haskell">Haskell</h3>
<p>We certainly can agree that Haskell is not a beginner’s language. Now Haskell is the language I write 99% of my code in and I’m saying this.</p>
<p>The type system, laziness, and monadic IO concept are all very daunting to a beginner. It doesn’t help that some of the error messages GHC produces are well… opaque. It’s not a bad language, but it’s not one that I would suggest for starting with.</p>
<h3 id="smlocaml">SML/OCaml</h3>
<p>Now SML and OCaml are both fine languages. But they don’t have the infrastructure to support a lot of teenage programmers.</p>
<p>They’re missing the game/GUI/web frameworks. They’re missing the tools. They’re missing the libraries. It’s just not there.</p>
<p>Other than that though, I see no reason why OCaml in particular wouldn’t make quite a reasonable language to start with. Pragmatically functional, reasonable type system, and strict semantics.</p>
<p>But the core language isn’t enough to make it a good first choice.</p>
<h3 id="scala">Scala</h3>
<p>Scala, like Haskell, is a very nice language that definitely fails the simplicity criteria. The type system is just as sophisticated as Haskell’s but with worse inference you have to be more explicit.</p>
<p>Again, it’s just not a language for a beginner from what I’ve seen.</p>
<p>Though with access to the java ecosystem and a very active community it nicely passes every other criteria.</p>
<h3 id="erlang">Erlang</h3>
<p>Erlang may be a nice language for a beginner. Once again I’ll admit ignorance and leave it to someone else to comment on it’s suitability.</p>
<p>I suspect that the focus on concurrency will make it a bit less intuitive to a beginner who doesn’t have any interest in those issues.</p>
<h3 id="racketscheme">Racket/Scheme</h3>
<p>Scheme on its own just fails the library support. It’s good for a classroom but for the demanding teenage hacker, the lack of game/gui frameworks is just killer.</p>
<p>Racket is a different story. Racket actually has a good set of libraries for GUI’s and games. It’s simple enough for a beginner. It has a good community for beginners, being made by educators.</p>
<p>In fact, the only problem I see with Racket vs Python is just the lack of hype. There isn’t the same marketing going on for Racket as Python, but there certainly could be.</p>
<p>Racket is what I recommend to people who are interested in starting with functional programming.</p>
<h3 id="clojure">Clojure</h3>
<p>I think Clojure is another strong choice. It’s just as simple as Racket and a much better community behind it.</p>
<p>It’s got all of Java’s libraries for games and several web frameworks of it’s own. In particular I’d love to see someone write a really slick DSL for minecraft in Clojure. It would be a great hook to say “Hey, come learn Clojure because it makes writing minecraft mods trivial”.</p>
<p>Maybe in the future I’ll start recommending Clojure instead of Racket. It looks like it’s got a brighter future with the much stronger community drive behind it.</p>
<hr />
<h1 id="conclusion">Conclusion</h1>
<p>So where does this leave us? Well I think the answer to the original question is clear. Functional programming isn’t the problem, functional languages are the problem.</p>
<p>There isn’t a clear analog to something like Python or Ruby in the functional programming world. I think it’s a legitimate niche for a language to try to fill too.</p>
<p>Perhaps I’ve overlooked something, but right now I think functional programming has got a ways to go making it more accessible to beginners. And I think it’s definitely worth the effort since a lot of the marketing points of FP, easier reasoning, simplified concepts, etc are all excellent for beginners.</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Sun, 08 Sep 2013 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2013-09-08-teens-and-fp.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Leaving Go</title>
    <link>http://jozefg.bitbucket.org/posts/2013-08-23-leaving-go.html</link>
    <description><![CDATA[<div class="info">
    Posted on August 23, 2013
    
</div>
<div class="info">
    
    Tags: <a href="/tags/go.html">go</a>, <a href="/tags/opinions.html">opinions</a>
    
</div>

<p>I’ve been using Go since November and I’ve decided that it’s time to give it up for my hobby projects. I’d still be happy to use it professionally, but I find that programming in Go isn’t “fun” in the same way that Python, Haskell, or Lisp is.</p>
<h2 id="go-the-good">Go, The Good</h2>
<p>The best part about Go isn’t actually Go. The community and infrastructure around it are excellent. The command line <code>go</code> tool really is nice.</p>
<p>By far my favorite part is <code>go get</code>. Package management is something that many a community has failed to address but Go seems to have handled it nicely.</p>
<p>This isn’t shocking I suppose. Go was definitely made by engineers to solve a very real world problem. I haven’t used Go for a project with 10 or 20 people but I suspect it would scale wonderfully.</p>
<p>On the squishier side, Go’s community is reasonably friendly. No newbies got their heads bitten off as far as I could see.</p>
<h2 id="go-the-not-so-good">Go, The Not So Good</h2>
<p>While the community for Go is great, the language is ehhh. Unfortunately, when I’m working on hobby projects, this is 80% of my concern. VB has good support, but I’m not hacking it.</p>
<p>The two main issues I have with Go are</p>
<ol type="1">
<li>The Type System</li>
<li>Extensibility</li>
</ol>
<h3 id="the-type-system">The Type System</h3>
<p>Go’s type system is well… lacking as it stands right now. The main problem is that Go provides no safe system for polymorphism.</p>
<p>I’ll give you a trivial example, define a generic absolute value function in Go.</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode go"><code class="sourceCode go"><a class="sourceLine" id="cb1-1" data-line-number="1">     <span class="kw">func</span> abs(x ???) ???{</a>
<a class="sourceLine" id="cb1-2" data-line-number="2">         ???</a>
<a class="sourceLine" id="cb1-3" data-line-number="3">    }</a></code></pre></div>
<p>Now what are those <code>???</code> supposed to be? Well, we have no notion of parametric polymorphism so our only choice is subtyping polymorphism.</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode go"><code class="sourceCode go"><a class="sourceLine" id="cb2-1" data-line-number="1">     <span class="kw">func</span> abs(x <span class="kw">interface</span>{}) <span class="kw">interface</span>{} {</a>
<a class="sourceLine" id="cb2-2" data-line-number="2">         ???</a>
<a class="sourceLine" id="cb2-3" data-line-number="3">    }</a></code></pre></div>
<p>So now that we’ve just taken all our lovely, optimization friendly type information and thrown it away, let’s manually get it back!</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode go"><code class="sourceCode go"><a class="sourceLine" id="cb3-1" data-line-number="1">    <span class="kw">type</span> Top <span class="kw">interface</span>{}</a>
<a class="sourceLine" id="cb3-2" data-line-number="2">    <span class="kw">func</span> abs(x Top) Top {</a>
<a class="sourceLine" id="cb3-3" data-line-number="3">        <span class="kw">switch</span> x.(<span class="kw">type</span>){</a>
<a class="sourceLine" id="cb3-4" data-line-number="4">	        <span class="kw">case</span> <span class="dt">int32</span>:</a>
<a class="sourceLine" id="cb3-5" data-line-number="5">    		    <span class="kw">if</span> x.(<span class="dt">int32</span>) &lt; <span class="dv">0</span> {</a>
<a class="sourceLine" id="cb3-6" data-line-number="6">			        <span class="kw">return</span> -x.(<span class="dt">int32</span>)</a>
<a class="sourceLine" id="cb3-7" data-line-number="7">		        } <span class="kw">else</span> {</a>
<a class="sourceLine" id="cb3-8" data-line-number="8">    			    <span class="kw">return</span> x.(<span class="dt">int32</span>)</a>
<a class="sourceLine" id="cb3-9" data-line-number="9">		        }</a>
<a class="sourceLine" id="cb3-10" data-line-number="10">	        <span class="kw">case</span> <span class="dt">int64</span>:</a>
<a class="sourceLine" id="cb3-11" data-line-number="11">        		<span class="kw">if</span> x.(<span class="dt">int64</span>) &lt; <span class="dv">0</span> {</a>
<a class="sourceLine" id="cb3-12" data-line-number="12">			        <span class="kw">return</span> -x.(<span class="dt">int64</span>)</a>
<a class="sourceLine" id="cb3-13" data-line-number="13">		        } <span class="kw">else</span> {</a>
<a class="sourceLine" id="cb3-14" data-line-number="14">        			<span class="kw">return</span> x.(<span class="dt">int64</span>)</a>
<a class="sourceLine" id="cb3-15" data-line-number="15">		        }</a>
<a class="sourceLine" id="cb3-16" data-line-number="16">	        <span class="kw">case</span> <span class="dt">float32</span>:</a>
<a class="sourceLine" id="cb3-17" data-line-number="17">        		<span class="kw">if</span> x.(<span class="dt">float32</span>) &lt; <span class="dv">0</span> {</a>
<a class="sourceLine" id="cb3-18" data-line-number="18">			        <span class="kw">return</span> -x.(<span class="dt">float32</span>)</a>
<a class="sourceLine" id="cb3-19" data-line-number="19">		        } <span class="kw">else</span> {</a>
<a class="sourceLine" id="cb3-20" data-line-number="20">        			<span class="kw">return</span> x.(<span class="dt">float32</span>)</a>
<a class="sourceLine" id="cb3-21" data-line-number="21">		        }</a>
<a class="sourceLine" id="cb3-22" data-line-number="22">	        <span class="kw">case</span> <span class="dt">float64</span>:</a>
<a class="sourceLine" id="cb3-23" data-line-number="23">        		<span class="kw">if</span> x.(<span class="dt">float64</span>) &lt; <span class="dv">0</span> {</a>
<a class="sourceLine" id="cb3-24" data-line-number="24">			        <span class="kw">return</span> -x.(<span class="dt">float32</span>)</a>
<a class="sourceLine" id="cb3-25" data-line-number="25">		        } <span class="kw">else</span> {</a>
<a class="sourceLine" id="cb3-26" data-line-number="26">                            <span class="kw">return</span> x.(<span class="dt">float64</span>)</a>
<a class="sourceLine" id="cb3-27" data-line-number="27">		        }</a>
<a class="sourceLine" id="cb3-28" data-line-number="28">            }</a>
<a class="sourceLine" id="cb3-29" data-line-number="29">        <span class="kw">return</span> <span class="ot">nil</span></a>
<a class="sourceLine" id="cb3-30" data-line-number="30">    }</a></code></pre></div>
<p>Holy boilerplate batman! And using this means we are forced to stick a cast right in the middle of our perfectly safe code.</p>
<p>By the way, there’s an error in the above code? Did you catch it? It’s tricky because with all this code duplication you tend to just skim over the boilerplate and miss the nasty runtime errors.</p>
<p>A type system that regularly requires casts is just gross, it’s a sign that the type system isn’t expressive enough to describe a problem.</p>
<p>What would happen if we wrote this in Haskell?</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode haskell"><code class="sourceCode haskell"><a class="sourceLine" id="cb4-1" data-line-number="1"><span class="ot">    abs ::</span> <span class="dt">Num</span> a <span class="ot">=&gt;</span> a <span class="ot">-&gt;</span> a</a>
<a class="sourceLine" id="cb4-2" data-line-number="2">    abs a <span class="fu">=</span> <span class="kw">if</span> a <span class="fu">&lt;</span> <span class="dv">0</span> <span class="kw">then</span> <span class="fu">-</span>a <span class="kw">else</span> a</a></code></pre></div>
<p>See the difference? And the Haskell version is extensible and cast free, it’ll work for any user defined types.</p>
<p>Now let’s be fair to Go, we can try this</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode go"><code class="sourceCode go"><a class="sourceLine" id="cb5-1" data-line-number="1">     <span class="kw">type</span> Abser <span class="kw">interface</span>{</a>
<a class="sourceLine" id="cb5-2" data-line-number="2">         <span class="kw">func</span> Negate() Abser</a>
<a class="sourceLine" id="cb5-3" data-line-number="3">         <span class="kw">func</span> LtZero() Abser</a>
<a class="sourceLine" id="cb5-4" data-line-number="4">     }</a>
<a class="sourceLine" id="cb5-5" data-line-number="5">     <span class="kw">func</span> Abs2(x Abser) Abser{</a>
<a class="sourceLine" id="cb5-6" data-line-number="6">         <span class="kw">if</span> x.LtZero() {</a>
<a class="sourceLine" id="cb5-7" data-line-number="7">             <span class="kw">return</span> x.Negate()</a>
<a class="sourceLine" id="cb5-8" data-line-number="8">         }</a>
<a class="sourceLine" id="cb5-9" data-line-number="9">         <span class="kw">return</span> x</a>
<a class="sourceLine" id="cb5-10" data-line-number="10">     }</a></code></pre></div>
<p>But this still isn’t close to Haskell’s version for several reasons, the biggest one for me is that this version takes in some <code>Abser</code> and returns some <code>Abser</code>. Are those the same underlying implementations? Who knows!</p>
<p>So we still have an unsafe cast in there just to use it because we have no way of statically verifying that we’re getting the same underlying type back.</p>
<p>This kills any chance of safely composing functions that take in different interfaces, for example, if we had a function over <code>int32</code>s, we couldn’t do <code>someFunc(abs(x))</code> because we’d have to stick our cast in there, <code>someFunc(abs(x).(int32))</code>. Now we’re just asking for trouble when there’s some error in the function that leads to a casting failure.</p>
<p>Doing this safely in Go looks like this,</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode go"><code class="sourceCode go"><a class="sourceLine" id="cb6-1" data-line-number="1">    newX, err := abs(x).(<span class="dt">int32</span>)</a>
<a class="sourceLine" id="cb6-2" data-line-number="2">    <span class="kw">if</span> err != <span class="ot">nil</span> {</a>
<a class="sourceLine" id="cb6-3" data-line-number="3">        fmt.Println(<span class="st">&quot;Darn it!&quot;</span>)</a>
<a class="sourceLine" id="cb6-4" data-line-number="4">        <span class="co">// Handle errors</span></a>
<a class="sourceLine" id="cb6-5" data-line-number="5">    }</a>
<a class="sourceLine" id="cb6-6" data-line-number="6">    someFunc(newX)</a></code></pre></div>
<p>Now if that doesn’t grind on you I really don’t know what would.</p>
<p>I don’t mind dynamic typing and the possibility of runtime errors, Python is fun to program in just like Haskell. But Go is imposing all the pain of static typing with pretty much none of the benefits.</p>
<p>The response of the Go community is “Abs is a 2 line function, just do it inline or per type” to which I respond: I want to define a generic algorithm, or datastructure, or really anything reasonably complex!</p>
<p>When I started Go, I thought this was just me missing a few clever tricks for how to properly utilize Go, I’m not so sure anymore. The entire Go math library requires casts to <code>float64</code>s to use, using a stack in Go requires casts from <code>interface{}</code>.</p>
<p>Coming from Haskell and Coq, this is not something I should have to put up with in 2013.</p>
<h3 id="extensibility">Extensibility</h3>
<p>Consider the keyword <code>range</code>. It’s a deeply magical keyword that only works inside <code>for</code> loops on Go’s primitive data structures.</p>
<p>I like writing compilers so I end up dealing a lot with trees. I often want to have <code>range</code> traverse my AST? Tough, ain’t gonna happen!</p>
<p>This is just one example of many</p>
<ul>
<li>Only Go’s primitive types may by parameterized over other types</li>
<li>Only magical primitive functions may return 1 or 2 arguments depending on context</li>
<li>Only magical primitives have real parametric polymorphism</li>
<li>Only Go’s primitives may have infix operators</li>
<li>and on and on and on!</li>
</ul>
<p>These are all hitting the same problem, Go is not extensible. There simply isn’t a way to define a type and expect it to be as pleasant to use as a slice.</p>
<p>This apparently doesn’t bother Go’s maintainers, presumably because they designed Go and deal with problems which slices, maps, and chans model beautifully. For the rest of the world, it’s a pain in the butt.</p>
<p>Guy Steele gave a wonderful talk about “growing a language”. The core idea was to start with a small but very extensible language and allow <em>users</em> to determine which features are added.</p>
<p>The idea is that there’s simply no way that any group of designers could imagine how people will want to use their language so making it easy to extend solves the problem wonderfully.</p>
<p>In Lisp, CLOS (Common Lisp Object System) was originally a library. It was a user defined abstraction that was so popular it was ported into the standard.</p>
<p>Go is just the opposite. Any user defined abstractions are painfully, obnoxiously obvious. Go developers seems to consider this a “Good Thing”. On one hand it does aid code readability. On the other, it really limits what Go’s pleasant to use for.</p>
<p>As a trivial case study. Imagine we wanted to use Go for some form of scientific computing. We’d need some sort of Bignum type because <code>int64</code> ain’t gonna hack it. In Python or Haskell, here’s how you add 2 bignums,</p>
<pre><code>a + b</code></pre>
<p>Here’s how you do it in Go,</p>
<pre><code>a.Add(b)</code></pre>
<p>Ok, it’s only a few characters, big deal. Now what does this do?</p>
<pre><code>b.Mul(b).Sub(big.NewInt(4).Mul(a).Mul(c))</code></pre>
<p>Or in Haskell</p>
<pre><code>b*b - 4 * a * c</code></pre>
<p>Which would you rather write? More importantly, which would you rather read?</p>
<p>I can’t help but feel like Go was designed with only problems the designers were facing in mind. This is great for them, but calling Go a general purpose language should mean that it’s nice to use for other sorts of problems too.</p>
<p>The argument of “it makes the code hard to read” seems a bit odd to me. Bad devs write bad code, but that doesn’t mean you should make it hard for good ones to write clean, concise code.</p>
<h2 id="conclusion">Conclusion</h2>
<p>I’m really sad to have written this actually. I wanted to like Go a lot. I wanted a fast, compiled replacement for stuff I write in C right now. But Go is not that language. Shame.</p>
<p>Thank you to the Go team for all the hard work on the project and best of luck.</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Fri, 23 Aug 2013 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2013-08-23-leaving-go.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>
<item>
    <title>Blog Re-Init</title>
    <link>http://jozefg.bitbucket.org/posts/2013-08-23-blog-re-init.html</link>
    <description><![CDATA[<div class="info">
    Posted on August 23, 2013
    
</div>
<div class="info">
    
    Tags: <a href="/tags/meta.html">meta</a>
    
</div>

<p>After a lot of hair-pulling and teeth grinding. I’ve decided to move my blog from Blogger to Bitbucket + Hakyll. Blogger is an excellent platform, but I missed a few things,</p>
<ul>
<li>Easy vcs (I like hg)</li>
<li>Nice, painless editing</li>
<li>Easy customization</li>
</ul>
<p>Using Bitbucket, it’s trivial to use mercurial on my blog. Now I have a full revision history of every edit I make to my posts. No more losing my last 4 paragraphs because my laptop died.</p>
<p>It also means that I can (and am!) typing this post in Emacs. Personally I like markdown so I can use Markdown and Flyspell modes for writing blog posts!</p>
<p>Finally, I really don’t enjoy the process of trying to customize Blogger. For example adding syntax highlighting for Haskell was an uphill battle from the start and 9 months of blogging later, I’m still not happy with the results. Adding the equivalent in Hakyll took me 10 minutes.</p>
<p>My only problems now are</p>
<ol type="1">
<li>Setting up comments here</li>
<li>Routing all links to previous posts back to blogger</li>
</ol>
<p>Then I’m all set!</p>
<div id="disqus_thread"></div>
          <script type="text/javascript">
            var disqus_shortname = 'codeco';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
          </script>
          <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
          <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
]]></description>
    <pubDate>Fri, 23 Aug 2013 00:00:00 UT</pubDate>
    <guid>http://jozefg.bitbucket.org/posts/2013-08-23-blog-re-init.html</guid>
    <dc:creator>Danny Gratzer</dc:creator>
</item>

    </channel>
</rss>
