<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.2.1" --><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Code Commit</title>
	<link>http://www.codecommit.com/blog</link>
	<description>(permanently in beta)</description>
	<pubDate>Tue, 24 Mar 2009 07:00:14 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.1</generator>
	<language>en</language>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item>
		<title>The Magic Behind Parser Combinators</title>
		<link>http://feeds.codecommit.com/~r/codecommit/~3/P8VHEcXpgCQ/the-magic-behind-parser-combinators</link>
		<comments>http://www.codecommit.com/blog/scala/the-magic-behind-parser-combinators#comments</comments>
		<pubDate>Tue, 24 Mar 2009 07:00:00 +0000</pubDate>
		<dc:creator>Daniel Spiewak</dc:creator>
		
		<category><![CDATA[Scala]]></category>
<category>code</category><category>language</category><category>parsing</category><category>theory</category><category>tools</category>
		<guid isPermaLink="false">http://www.codecommit.com/blog/scala/the-magic-behind-parser-combinators</guid>
		<description><![CDATA[If you&#8217;re like me, one of the first things that attracted you to Scala was its parser combinators.&#160; Well, maybe that wasn&#8217;t the first thing for me, but it was pretty far up there.&#160; Parser combinators make it almost too easy to create a parser for a complex language without ever leaving the comfortable play-pen [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re like me, one of the first things that attracted you to Scala was its parser combinators.&nbsp; Well, maybe that wasn&#8217;t the <em>first</em> thing for me, but it was pretty far up there.&nbsp; Parser combinators make it almost too easy to create a parser for a complex language without ever leaving the comfortable play-pen afforded by Scala.&nbsp; Incidentally, if you aren&#8217;t familiar with the fundamentals of text parsing, context-free grammars and/or parser generators, then you might want to <a href="http://en.wikipedia.org/wiki/Context-free_grammar">do</a> <a href="http://en.wikipedia.org/wiki/LL_parsing">some</a> <a href="http://en.wikipedia.org/wiki/Recursive_descent">reading</a> before you continue with this article.</p>
<h3>Intro to Parser Combinators</h3>
<p>In most languages, the process of creating a text parser is usually an arduous and clumsy affair involving a parser generator (such as <a href="http://www.antlr.org/">ANTLR</a>, <a href="https://javacc.dev.java.net/">JavaCC</a>, <a href="http://beaver.sourceforge.net/">Beaver</a> or &lt;shamelessPlug&gt;<a href="http://www.cs.uwm.edu/~boyland/scala-bison.html">ScalaBison</a>&lt;/shamelessPlug&gt;) and (usually) a lexer generator such as <a href="http://jflex.de/">JFlex</a>.&nbsp; These tools do a very good job of generating sources for efficient and powerful parsers, but they aren&#8217;t exactly the easiest tools to use.&nbsp; They generally have a very steep learning curve, and due to their unique status as <a href="http://en.wikipedia.org/wiki/Compiler-compiler">compiler-compilers</a>, an unintuitive architecture.&nbsp; Additionally, these tools can be somewhat rigid, making it very difficult to implement unique or experimental features.&nbsp; For this reason alone, many real world compilers and interpreters (such as <code>javac</code>, <code>ruby</code>, <code>jruby</code> and <code>scalac</code>) actually use hand-written parsers.&nbsp; These are usually easier to tweak, but they can be very difficult to develop and test.&nbsp; Additionally, hand-written parsers have a tendency toward poor performance (think: the Scala compiler).</p>
<p>When creating a compiler in Scala, it is perfectly acceptable to make use of these conventional Java-generating tools like ANTLR or Beaver, but we do have other options.&nbsp; Parser combinators are a domain-specific language baked into the standard library.&nbsp; Using this internal DSL, we can create an instance of a parser for a given grammar using Scala methods and fields.&nbsp; What&#8217;s more, we can do this in a very declarative fashion.&nbsp; Thanks to the magic of DSLs, our sources will actually <em>look</em> like a plain-Jane context-free grammar for our language.&nbsp; This means that we get most of the benefits of a hand-written parser without losing the maintainability afforded by parser generators like bison.&nbsp; For example, here is a very simple grammar for a simplified Scala-like language, expressed in terms of parser combinators:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #006699; font-weight: bold;">object</span> SimpleScala <span style="color: #006699; font-weight: bold;">extends</span> RegexpParsers <span style="font-weight: bold;">&#123;</span>
&nbsp;
  <span style="color: #006699; font-weight: bold;">val</span> ID = <span style="color: #ff00cc;">&quot;&quot;</span><span style="color: #ff00cc;">&quot;[a-zA-Z]([a-zA-Z0-9]|_[a-zA-Z0-9])*&quot;</span><span style="color: #ff00cc;">&quot;&quot;</span>r
&nbsp;
  <span style="color: #006699; font-weight: bold;">val</span> NUM = <span style="color: #ff00cc;">&quot;&quot;</span><span style="color: #ff00cc;">&quot;[1-9][0-9]*&quot;</span><span style="color: #ff00cc;">&quot;&quot;</span>r
&nbsp;
  <span style="color: #006699; font-weight: bold;">def</span> program = clazz*
&nbsp;
  <span style="color: #006699; font-weight: bold;">def</span> classPrefix = <span style="color: #ff00cc;">&quot;class&quot;</span> ~ ID ~ <span style="color: #ff00cc;">&quot;(&quot;</span> ~ formals ~ <span style="color: #ff00cc;">&quot;)&quot;</span>
&nbsp;
  <span style="color: #006699; font-weight: bold;">def</span> classExt = <span style="color: #ff00cc;">&quot;extends&quot;</span> ~ ID ~ <span style="color: #ff00cc;">&quot;(&quot;</span> ~ actuals ~ <span style="color: #ff00cc;">&quot;)&quot;</span>
&nbsp;
  <span style="color: #006699; font-weight: bold;">def</span> clazz = classPrefix ~ <span style="color: #9966ff;">opt</span><span style="font-weight: bold;">&#40;</span>classExt<span style="font-weight: bold;">&#41;</span> ~ <span style="color: #ff00cc;">&quot;{&quot;</span> ~ <span style="font-weight: bold;">&#40;</span>member*<span style="font-weight: bold;">&#41;</span> ~ <span style="color: #ff00cc;">&quot;}&quot;</span>
&nbsp;
  <span style="color: #006699; font-weight: bold;">def</span> formals = <span style="color: #9966ff;">repsep</span><span style="font-weight: bold;">&#40;</span>ID ~ <span style="color: #ff00cc;">&quot;:&quot;</span> ~ ID, <span style="color: #ff00cc;">&quot;,&quot;</span><span style="font-weight: bold;">&#41;</span>
&nbsp;
  <span style="color: #006699; font-weight: bold;">def</span> actuals = expr*
&nbsp;
  <span style="color: #006699; font-weight: bold;">def</span> member = <span style="font-weight: bold;">&#40;</span>
      <span style="color: #ff00cc;">&quot;val&quot;</span> ~ ID ~ <span style="color: #ff00cc;">&quot;:&quot;</span> ~ ID ~ <span style="color: #ff00cc;">&quot;=&quot;</span> ~ expr
    | <span style="color: #ff00cc;">&quot;var&quot;</span> ~ ID ~ <span style="color: #ff00cc;">&quot;:&quot;</span> ~ ID ~ <span style="color: #ff00cc;">&quot;=&quot;</span> ~ expr
    | <span style="color: #ff00cc;">&quot;def&quot;</span> ~ ID ~ <span style="color: #ff00cc;">&quot;(&quot;</span> ~ formals ~ <span style="color: #ff00cc;">&quot;)&quot;</span> ~ <span style="color: #ff00cc;">&quot;:&quot;</span> ~ ID ~ <span style="color: #ff00cc;">&quot;=&quot;</span> ~ expr
    | <span style="color: #ff00cc;">&quot;def&quot;</span> ~ ID ~ <span style="color: #ff00cc;">&quot;:&quot;</span> ~ ID ~ <span style="color: #ff00cc;">&quot;=&quot;</span> ~ expr
    | <span style="color: #ff00cc;">&quot;type&quot;</span> ~ ID ~ <span style="color: #ff00cc;">&quot;=&quot;</span> ~ ID
  <span style="font-weight: bold;">&#41;</span>
&nbsp;
  <span style="color: #006699; font-weight: bold;">def</span> expr: Parser<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>Expr<span style="font-weight: bold;">&#93;</span></span> = factor ~ <span style="font-weight: bold;">&#40;</span>
      <span style="color: #ff00cc;">&quot;+&quot;</span> ~ factor
    | <span style="color: #ff00cc;">&quot;-&quot;</span> ~ factor
  <span style="font-weight: bold;">&#41;</span>*
&nbsp;
  <span style="color: #006699; font-weight: bold;">def</span> factor = term ~ <span style="font-weight: bold;">&#40;</span><span style="color: #ff00cc;">&quot;.&quot;</span> ~ ID ~ <span style="color: #ff00cc;">&quot;(&quot;</span> ~ actuals ~ <span style="color: #ff00cc;">&quot;)&quot;</span><span style="font-weight: bold;">&#41;</span>*
&nbsp;
  <span style="color: #006699; font-weight: bold;">def</span> term = <span style="font-weight: bold;">&#40;</span>
      <span style="color: #ff00cc;">&quot;(&quot;</span> ~ expr ~ <span style="color: #ff00cc;">&quot;)&quot;</span>
    | ID
    | NUM
  <span style="font-weight: bold;">&#41;</span>
<span style="font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>This is all valid and correct Scala.&nbsp; The <code>program</code> method returns an instance of <code>Parser[List[Class_]]</code>, assuming that <code>Class_</code> is the AST class representing a syntactic class in the language (and assuming that we had added all of the boiler-plate necessary AST generation).&nbsp; This <code>Parser</code> instance can then be used to process a <code>java.io.Reader</code>, producing some result if the input is valid, otherwise producing an error.</p>
<h3>How the Magic Works</h3>
<p>The really significant thing to notice here is that <code>program</code> is nothing special; just another method which returns an instance of class <code>Parser</code>.&nbsp; In fact, <em>all</em> of these methods return instances of <code>Parser</code>.&nbsp; Once you realize this, the magic behind all of this becomes quite a bit more obvious.&nbsp; However, to really figure it all out, we&#8217;re going to need to take a few steps back.</p>
<p>Conceptually, a <code>Parser</code> represents a very simple idea:</p>
<blockquote><p>Parsers are invoked upon an input stream.&nbsp; They will consume a certain number of tokens and then return a result along with the truncated stream.&nbsp; Alternatively, they will fail, producing an error message.</p></blockquote>
<p>Every <code>Parser</code> instance complies with this description.&nbsp; To be more concrete, consider the <code>keyword</code> parser (what I like to call the &#8220;<em>literal</em>&#8221; parser) which consumes a single well-defined token.&nbsp; For example (note that the <code>keyword</code> method is implicit in Scala&#8217;s implementation of parser combinators, which is why it doesn&#8217;t appear in the long example above):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #006699; font-weight: bold;">def</span> boring = <span style="color: #9966ff;">keyword</span><span style="font-weight: bold;">&#40;</span><span style="color: #ff00cc;">&quot;bore&quot;</span><span style="font-weight: bold;">&#41;</span></pre></td></tr></table></div>

<p>The <code>boring</code> method returns a value of type <code>Parser[String]</code>.&nbsp; That is, a parser which consumes input and somehow produces a <code>String</code> as a result (along with the truncated stream).&nbsp; This parser will either parse the characters b-o-r-e in that order, or it will fail.&nbsp; If it succeeds, it will return the string <code>"bore"</code> as a result along with a stream which is shortened by four characters.&nbsp; If it fails, it will return an error message along the lines of &#8220;<code>Expected 'bore', got 'Boer'</code>&#8220;, or something to that effect.</p>
<p>By itself, such a parser is really not very useful.&nbsp; After all, it&#8217;s easy enough to perform a little bit of <code>String</code> equality testing when looking for a well-defined token.&nbsp; The real power of parser combinators is in what happens when you start combining them together (hence the name).&nbsp; A few literal parsers combined in sequence can give us a phrase in our grammar, and a few of these sequences combined in a disjunction can give us the full power of a non-terminal with multiple productions.&nbsp; As it turns out, all we need is the literal parser (<code>keyword</code>) combined with these two types of combinators to express <em>any</em> LL(*) grammar.</p>
<p>Before we get into the combinators themselves, let&#8217;s build a framework.&nbsp; We will define <code>Parser[A]</code> as a function from <code>Stream[Character]</code> to <code>Result[A]</code>, where <code>Result[A]</code> has two implementations: <code>Success[A]</code> and <code>Failure</code>.&nbsp; The framework looks like the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #006699; font-weight: bold;">trait</span> Parser<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>+A<span style="font-weight: bold;">&#93;</span></span> <span style="color: #006699; font-weight: bold;">extends</span> <span style="font-weight: bold;">&#40;</span>Stream<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span><span style="color: #009966; font-weight: bold;">Character</span><span style="font-weight: bold;">&#93;</span></span>=&gt;Result<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>A<span style="font-weight: bold;">&#93;</span></span><span style="font-weight: bold;">&#41;</span>
&nbsp;
<span style="color: #006699; font-weight: bold;">sealed</span> <span style="color: #006699; font-weight: bold;">trait</span> Result<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>+A<span style="font-weight: bold;">&#93;</span></span>
&nbsp;
<span style="color: #006699; font-weight: bold;">case</span> <span style="color: #006699; font-weight: bold;">class</span> Success<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>+A<span style="font-weight: bold;">&#93;</span></span><span style="font-weight: bold;">&#40;</span>value: A, rem: Stream<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span><span style="color: #009966; font-weight: bold;">Character</span><span style="font-weight: bold;">&#93;</span></span><span style="font-weight: bold;">&#41;</span> <span style="color: #006699; font-weight: bold;">extends</span> Result<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>A<span style="font-weight: bold;">&#93;</span></span>
&nbsp;
<span style="color: #006699; font-weight: bold;">case</span> <span style="color: #006699; font-weight: bold;">class</span> <span style="color: #9966ff;">Failure</span><span style="font-weight: bold;">&#40;</span>msg: <span style="color: #0099ff; font-weight: bold;">String</span><span style="font-weight: bold;">&#41;</span> <span style="color: #006699; font-weight: bold;">extends</span> Result<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>Nothing<span style="font-weight: bold;">&#93;</span></span></pre></td></tr></table></div>

<p>Additionally, we must add a concrete parser, <code>keyword</code>, to handle our literals.&nbsp; For the sake of syntactic compatibility with Scala&#8217;s parser combinators, this parser will be defined within the <code>RegexpParsers</code> singleton object (despite the fact that we don&#8217;t really support regular expressions):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #006699; font-weight: bold;">object</span> RegexpParsers <span style="font-weight: bold;">&#123;</span>
  <span style="color: #006699; font-weight: bold;">implicit</span> <span style="color: #006699; font-weight: bold;">def</span> <span style="color: #9966ff;">keyword</span><span style="font-weight: bold;">&#40;</span>str: <span style="color: #0099ff; font-weight: bold;">String</span><span style="font-weight: bold;">&#41;</span> = <span style="color: #006699; font-weight: bold;">new</span> Parser<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span><span style="color: #0099ff; font-weight: bold;">String</span><span style="font-weight: bold;">&#93;</span></span> <span style="font-weight: bold;">&#123;</span>
    <span style="color: #006699; font-weight: bold;">def</span> <span style="color: #9966ff;">apply</span><span style="font-weight: bold;">&#40;</span>s: Stream<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span><span style="color: #009966; font-weight: bold;">Character</span><span style="font-weight: bold;">&#93;</span></span><span style="font-weight: bold;">&#41;</span> = <span style="font-weight: bold;">&#123;</span>
      <span style="color: #006699; font-weight: bold;">val</span> trunc = s take str.<span class="me1">length</span>
      <span style="color: #006699; font-weight: bold;">lazy</span> <span style="color: #006699; font-weight: bold;">val</span> errorMessage = <span style="color: #ff00cc;">&quot;Expected '%s' got '%s'&quot;</span>.<span style="color: #9966ff;">format</span><span style="font-weight: bold;">&#40;</span>str, trunc.<span class="me1">mkString</span><span style="font-weight: bold;">&#41;</span>
&nbsp;
      <span style="color: #006699; font-weight: bold;">if</span> <span style="font-weight: bold;">&#40;</span>trunc lengthCompare str.<span class="me1">length</span> != <span style="color: #ff0000;">0</span><span style="font-weight: bold;">&#41;</span> 
        <span style="color: #9966ff;">Failure</span><span style="font-weight: bold;">&#40;</span>errorMessage<span style="font-weight: bold;">&#41;</span>
      <span style="color: #006699; font-weight: bold;">else</span> <span style="font-weight: bold;">&#123;</span>
        <span style="color: #006699; font-weight: bold;">val</span> succ = trunc.<span class="me1">zipWithIndex</span> forall <span style="font-weight: bold;">&#123;</span>
          <span style="color: #006699; font-weight: bold;">case</span> <span style="font-weight: bold;">&#40;</span>c, i<span style="font-weight: bold;">&#41;</span> =&gt; c == <span style="color: #9966ff;">str</span><span style="font-weight: bold;">&#40;</span>i<span style="font-weight: bold;">&#41;</span>
        <span style="font-weight: bold;">&#125;</span>
&nbsp;
        <span style="color: #006699; font-weight: bold;">if</span> <span style="font-weight: bold;">&#40;</span>succ<span style="font-weight: bold;">&#41;</span> <span style="color: #9966ff;">Success</span><span style="font-weight: bold;">&#40;</span>str, s drop str.<span class="me1">length</span><span style="font-weight: bold;">&#41;</span>
        <span style="color: #006699; font-weight: bold;">else</span> <span style="color: #9966ff;">Failure</span><span style="font-weight: bold;">&#40;</span>errorMessage<span style="font-weight: bold;">&#41;</span>
      <span style="font-weight: bold;">&#125;</span>
    <span style="font-weight: bold;">&#125;</span>
  <span style="font-weight: bold;">&#125;</span>
<span style="font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>For those of you who are still a little uncomfortable with the more obscure higher-order utility methods in the Scala collections framework: don&#8217;t worry about it.&nbsp; While the above may be a bit obfuscated, there isn&#8217;t really a need to understand what&#8217;s going on at any sort of precise level.&nbsp; The important point is that this <code>Parser</code> defines an <code>apply</code> method which compares <code>str</code> to an equally-lengthed prefix from <code>s</code>, the input character <code>Stream</code>.&nbsp; At the end of the day, it returns either <code>Success</code> or <code>Failure</code>.</p>
<h4>The Sequential Combinator</h4>
<p>The first of the two combinators we need to look at is the sequence combinator.&nbsp; Conceptually, this combinator takes two parsers and produces a new parser which matches the first <em>and</em> the second in order.&nbsp; If either one of the parsers produces a <code>Failure</code>, then the entire sequence will fail.&nbsp; In terms of classical logic, this parser corresponds to the AND operation.&nbsp; The code for this parser is almost ridiculously simple:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #006699; font-weight: bold;">class</span> SequenceParser<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>+A, +B<span style="font-weight: bold;">&#93;</span></span><span style="font-weight: bold;">&#40;</span>l: =&gt;Parser<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>A<span style="font-weight: bold;">&#93;</span></span>, 
                             r: =&gt;Parser<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>B<span style="font-weight: bold;">&#93;</span></span><span style="font-weight: bold;">&#41;</span> <span style="color: #006699; font-weight: bold;">extends</span> Parser<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span><span style="font-weight: bold;">&#40;</span>A, B<span style="font-weight: bold;">&#41;</span><span style="font-weight: bold;">&#93;</span></span> <span style="font-weight: bold;">&#123;</span>
  <span style="color: #006699; font-weight: bold;">lazy</span> <span style="color: #006699; font-weight: bold;">val</span> left = l
  <span style="color: #006699; font-weight: bold;">lazy</span> <span style="color: #006699; font-weight: bold;">val</span> right = r
&nbsp;
  <span style="color: #006699; font-weight: bold;">def</span> <span style="color: #9966ff;">apply</span><span style="font-weight: bold;">&#40;</span>s: Stream<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span><span style="color: #009966; font-weight: bold;">Character</span><span style="font-weight: bold;">&#93;</span></span><span style="font-weight: bold;">&#41;</span> = <span style="color: #9966ff;">left</span><span style="font-weight: bold;">&#40;</span>s<span style="font-weight: bold;">&#41;</span> <span style="color: #006699; font-weight: bold;">match</span> <span style="font-weight: bold;">&#123;</span>
    <span style="color: #006699; font-weight: bold;">case</span> <span style="color: #9966ff;">Success</span><span style="font-weight: bold;">&#40;</span>a, rem<span style="font-weight: bold;">&#41;</span> =&gt; <span style="color: #9966ff;">right</span><span style="font-weight: bold;">&#40;</span>rem<span style="font-weight: bold;">&#41;</span> <span style="color: #006699; font-weight: bold;">match</span> <span style="font-weight: bold;">&#123;</span>
      <span style="color: #006699; font-weight: bold;">case</span> <span style="color: #9966ff;">Success</span><span style="font-weight: bold;">&#40;</span>b, rem<span style="font-weight: bold;">&#41;</span> =&gt; <span style="color: #9966ff;">Success</span><span style="font-weight: bold;">&#40;</span><span style="font-weight: bold;">&#40;</span>a, b<span style="font-weight: bold;">&#41;</span>, rem<span style="font-weight: bold;">&#41;</span>
      <span style="color: #006699; font-weight: bold;">case</span> f: Failure =&gt; f
    <span style="font-weight: bold;">&#125;</span>
&nbsp;
    <span style="color: #006699; font-weight: bold;">case</span> f: Failure =&gt; f
  <span style="font-weight: bold;">&#125;</span>
<span style="font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>This is literally just a parser which applies its <code>left</code> operand and then applies its <code>right</code> to whatever is left.&nbsp; As long as <em>both</em> parsers succeed, a composite <code>Success</code> will be produced containing a tuple of the left and right parser&#8217;s results.&nbsp; Note that Scala&#8217;s parser combinator framework actually yields an instance of the <code>~</code> case class from its sequence combinator.&nbsp; This is particularly convenient as it allows for a very nice syntax in pattern matching for semantic actions (extracting parse results).&nbsp; However, since we will not be dealing with the action combinators in this article, it seemed simpler to just use a tuple.</p>
<p>One item worthy of note is the fact that both <code>left</code> and <code>right</code> are evaluated lazily.&nbsp; This means that we don&#8217;t actually evaluate our constructor parameters until the parser is applied to a specific stream.&nbsp; This is very important as it allows us to define parsers with recursive rules.&nbsp; Recursion is really what separates context-free grammars from regular expressions.&nbsp; Without this laziness, any recursive rules would lead to an infinite loop in the parser construction.</p>
<p>Once we have the sequence combinator in hand, we can add a bit of syntax sugar to enable its use.&nbsp; All instances of <code>Parser</code> will define a <code>~</code> operator which takes a single operand and produces a <code>SequenceParser</code> which handles the receiver and the parameter in order:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #006699; font-weight: bold;">trait</span> Parser<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>+A<span style="font-weight: bold;">&#93;</span></span> <span style="color: #006699; font-weight: bold;">extends</span> <span style="font-weight: bold;">&#40;</span>Stream<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span><span style="color: #009966; font-weight: bold;">Character</span><span style="font-weight: bold;">&#93;</span></span>=&gt;Result<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>A<span style="font-weight: bold;">&#93;</span></span><span style="font-weight: bold;">&#41;</span> <span style="font-weight: bold;">&#123;</span>
  <span style="color: #006699; font-weight: bold;">def</span> ~<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>B<span style="font-weight: bold;">&#93;</span></span><span style="font-weight: bold;">&#40;</span>that: =&gt;Parser<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>B<span style="font-weight: bold;">&#93;</span></span><span style="font-weight: bold;">&#41;</span> = <span style="color: #006699; font-weight: bold;">new</span> <span style="color: #9966ff;">SequenceParser</span><span style="font-weight: bold;">&#40;</span><span style="color: #cc00cc;">this</span>, that<span style="font-weight: bold;">&#41;</span>
<span style="font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>With this modification to <code>Parser</code>, we can now create parsers which match arbitrary sequences of tokens.&nbsp; For example, our framework so far is more than sufficient to define the <code>classPrefix</code> parser from our earlier snippet (with the exception of the regular expression defined in <code>ID</code>, which we currently have no way of handling):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #006699; font-weight: bold;">def</span> classPrefix = <span style="color: #ff00cc;">&quot;class&quot;</span> ~ ID ~ <span style="color: #ff00cc;">&quot;(&quot;</span> ~ formals ~ <span style="color: #ff00cc;">&quot;)&quot;</span></pre></td></tr></table></div>

<h4>The Disjunctive Combinator</h4>
<p>This is a very academic name for a very simple concept.&nbsp; Let&#8217;s think about the framework so far.&nbsp; We have both literal parsers and sequential combinations thereof.&nbsp; Using this framework, we are capable of defining parsers which match arbitrary token strings.&nbsp; We can even define parsers which match <em>infinite</em> token sequences, simply by involving recursion:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #006699; font-weight: bold;">def</span> fearTheOnes: Parser<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span><span style="color: #0099ff; font-weight: bold;">Any</span><span style="font-weight: bold;">&#93;</span></span> = <span style="color: #ff00cc;">&quot;1&quot;</span> ~ fearTheOnes</pre></td></tr></table></div>

<p>Of course, this parser is absurd, since it only matches an infinite input consisting of the <code>'1'</code> character, but it does serve to illustrate that we have a reasonably powerful framework even in its current form.&nbsp; This also provides a nice example of how the lazy evaluation of sequence parsers is an absolutely essential feature.&nbsp; Without it, the <code>fearTheOnes</code> method would enter an infinite loop and would never return an instance of <code>Parser</code>.</p>
<p>However, for all its glitz, our framework is still somewhat impotent compared to &#8220;real&#8221; parser generators.&nbsp; It is almost trivial to derive a grammar which cannot be handled by our parser combinators.&nbsp; For example:</p>
<pre style="margin-left: 15px">e ::= '1' | '2'</pre>
<p>This grammar simply says &#8220;match either the <code>'1'</code> character, or the <code>'2'</code> character&#8221;.&nbsp; Unfortunately, our framework is incapable of defining a parser according to this rule.&nbsp; We have no facility for saying &#8220;either this or that&#8221;.&nbsp; This is where the disjunctive combinator comes into play.</p>
<p>In boolean logic, a disjunction is defined according to the following truth table:</p>
<table border="1" align="center" width="250">
<tr align="center">
<td><b>P</b></td>
<td><b>Q</b></td>
<td><b>P V Q</b></td>
</tr>
<tr align="center">
<td>T</td>
<td>T</td>
<td>T</td>
</tr>
<tr align="center">
<td>T</td>
<td>F</td>
<td>T</td>
</tr>
<tr align="center">
<td>F</td>
<td>T</td>
<td>T</td>
</tr>
<tr align="center">
<td>F</td>
<td>F</td>
<td>F</td>
</tr>
</table>
<p>In other words, the disjunction is true if one or both of its component predicates are true.&nbsp; This is exactly the sort of combinator we need to bring our framework to full LL(*) potential.&nbsp; We need to define a parser combinator which takes two parsers as parameters, trying each of them in order.&nbsp; If the first parser succeeds, we yield its value; otherwise, we try the second parser and return its <code>Result</code> (whether <code>Success</code> or <code>Failure</code>).&nbsp; Thus, our disjunctive combinator should yield a parser which succeeds if and only if one of its component parsers succeeds.&nbsp; This is very easily accomplished:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #006699; font-weight: bold;">class</span> DisParser<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>+A<span style="font-weight: bold;">&#93;</span></span><span style="font-weight: bold;">&#40;</span>left: Parser<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>A<span style="font-weight: bold;">&#93;</span></span>, right: Parser<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>A<span style="font-weight: bold;">&#93;</span></span><span style="font-weight: bold;">&#41;</span> <span style="color: #006699; font-weight: bold;">extends</span> Parser<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>A<span style="font-weight: bold;">&#93;</span></span> <span style="font-weight: bold;">&#123;</span>
  <span style="color: #006699; font-weight: bold;">def</span> <span style="color: #9966ff;">apply</span><span style="font-weight: bold;">&#40;</span>s: Stream<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span><span style="color: #009966; font-weight: bold;">Character</span><span style="font-weight: bold;">&#93;</span></span><span style="font-weight: bold;">&#41;</span> = <span style="color: #9966ff;">left</span><span style="font-weight: bold;">&#40;</span>s<span style="font-weight: bold;">&#41;</span> <span style="color: #006699; font-weight: bold;">match</span> <span style="font-weight: bold;">&#123;</span>
    <span style="color: #006699; font-weight: bold;">case</span> res: Success =&gt; res
    <span style="color: #006699; font-weight: bold;">case</span> _: Failure =&gt; <span style="color: #9966ff;">right</span><span style="font-weight: bold;">&#40;</span>s<span style="font-weight: bold;">&#41;</span>
  <span style="font-weight: bold;">&#125;</span>
<span style="font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>Once again, we can beautify the syntax a little bit by adding an operator to the <code>Parser</code> super-trait:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #006699; font-weight: bold;">trait</span> Parser<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>+A<span style="font-weight: bold;">&#93;</span></span> <span style="color: #006699; font-weight: bold;">extends</span> <span style="font-weight: bold;">&#40;</span>Stream<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span><span style="color: #009966; font-weight: bold;">Character</span><span style="font-weight: bold;">&#93;</span></span>=&gt;Result<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>A<span style="font-weight: bold;">&#93;</span></span><span style="font-weight: bold;">&#41;</span> <span style="font-weight: bold;">&#123;</span>
  <span style="color: #006699; font-weight: bold;">def</span> ~<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>B<span style="font-weight: bold;">&#93;</span></span><span style="font-weight: bold;">&#40;</span>that: =&gt;Parser<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>B<span style="font-weight: bold;">&#93;</span></span><span style="font-weight: bold;">&#41;</span> = <span style="color: #006699; font-weight: bold;">new</span> <span style="color: #9966ff;">SequenceParser</span><span style="font-weight: bold;">&#40;</span><span style="color: #cc00cc;">this</span>, that<span style="font-weight: bold;">&#41;</span>
&nbsp;
  <span style="color: #006699; font-weight: bold;">def</span> |<span style="font-weight: bold;">&#40;</span>that: Parser<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>A<span style="font-weight: bold;">&#93;</span></span><span style="font-weight: bold;">&#41;</span> = <span style="color: #006699; font-weight: bold;">new</span> <span style="color: #9966ff;">DisParser</span><span style="font-weight: bold;">&#40;</span><span style="color: #cc00cc;">this</span>, that<span style="font-weight: bold;">&#41;</span>
<span style="font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<h3>&#8230;is that all?</h3>
<p>It&#8217;s almost as if by magic, but the addition of the disjunctive combinator to the sequential actually turns our framework into something really special, capable of chewing through any LL(*) grammar.&nbsp; Just in case you don&#8217;t believe me, consider the grammar for the pure-untyped lambda calculus, expressed using our framework (<code>alph</code> definition partially elided for brevity):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #006699; font-weight: bold;">object</span> LambdaCalc <span style="color: #006699; font-weight: bold;">extends</span> RegexpParsers <span style="font-weight: bold;">&#123;</span>
&nbsp;
  <span style="color: #006699; font-weight: bold;">def</span> expr: Parser<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span><span style="color: #0099ff; font-weight: bold;">Any</span><span style="font-weight: bold;">&#93;</span></span> = term ~ <span style="font-weight: bold;">&#40;</span>expr | <span style="color: #ff00cc;">&quot;&quot;</span><span style="font-weight: bold;">&#41;</span>
&nbsp;
  <span style="color: #006699; font-weight: bold;">def</span> term = <span style="font-weight: bold;">&#40;</span>
      <span style="color: #ff00cc;">&quot;fn&quot;</span> ~ id ~ <span style="color: #ff00cc;">&quot;=&gt;&quot;</span> ~ expr
    | id
  <span style="font-weight: bold;">&#41;</span>
&nbsp;
  <span style="color: #006699; font-weight: bold;">val</span> alph = <span style="color: #ff00cc;">&quot;a&quot;</span>|<span style="color: #ff00cc;">&quot;b&quot;</span>|<span style="color: #ff00cc;">&quot;c&quot;</span>|...|<span style="color: #ff00cc;">&quot;X&quot;</span>|<span style="color: #ff00cc;">&quot;Y&quot;</span>|<span style="color: #ff00cc;">&quot;Z&quot;</span>
  <span style="color: #006699; font-weight: bold;">val</span> num = <span style="color: #ff00cc;">&quot;0&quot;</span>|<span style="color: #ff00cc;">&quot;1&quot;</span>|<span style="color: #ff00cc;">&quot;2&quot;</span>|<span style="color: #ff00cc;">&quot;3&quot;</span>|<span style="color: #ff00cc;">&quot;4&quot;</span>|<span style="color: #ff00cc;">&quot;5&quot;</span>|<span style="color: #ff00cc;">&quot;6&quot;</span>|<span style="color: #ff00cc;">&quot;7&quot;</span>|<span style="color: #ff00cc;">&quot;8&quot;</span>|<span style="color: #ff00cc;">&quot;9&quot;</span>
&nbsp;
  <span style="color: #006699; font-weight: bold;">def</span> id = alph ~ idSuffix
&nbsp;
  <span style="color: #006699; font-weight: bold;">def</span> idSuffix = <span style="font-weight: bold;">&#40;</span>
      <span style="font-weight: bold;">&#40;</span>alph | num<span style="font-weight: bold;">&#41;</span> ~ idSuffix
    | <span style="color: #ff00cc;">&quot;&quot;</span>
  <span style="font-weight: bold;">&#41;</span>
<span style="font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>While this grammar may seem a bit obfuscated, it is only because I had to avoid the use of regular expressions to define the <code>ID</code> rule.&nbsp; Instead, I used a combination of sequential and disjunctive combinators to produce a <code>Parser</code> which matches the desired pattern.&nbsp; Note that the &#8220;<code>...</code>&#8221; is not some special syntax, but rather my laziness and wish to avoid a code snippet 310 characters wide.</p>
<p>We can also use our framework to define some other, useful combinators such as <code>opt</code> and <code>*</code> (used in the initial example).  Specifically:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #006699; font-weight: bold;">trait</span> Parser<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>+A<span style="font-weight: bold;">&#93;</span></span> <span style="color: #006699; font-weight: bold;">extends</span> <span style="font-weight: bold;">&#40;</span>Stream<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span><span style="color: #009966; font-weight: bold;">Character</span><span style="font-weight: bold;">&#93;</span></span>=&gt;Result<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>A<span style="font-weight: bold;">&#93;</span></span><span style="font-weight: bold;">&#41;</span> <span style="font-weight: bold;">&#123;</span>
  ...
&nbsp;
  <span style="color: #006699; font-weight: bold;">def</span> *: Parser<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span><span style="color: #0099ff; font-weight: bold;">List</span><span style="font-weight: bold;">&#91;</span>A<span style="font-weight: bold;">&#93;</span></span><span style="font-weight: bold;">&#93;</span> = <span style="font-weight: bold;">&#40;</span>
      <span style="color: #cc00cc;">this</span> ~ * ^^ <span style="font-weight: bold;">&#123;</span> <span style="color: #006699; font-weight: bold;">case</span> <span style="font-weight: bold;">&#40;</span>a, b<span style="font-weight: bold;">&#41;</span> =&gt; a :: b <span style="font-weight: bold;">&#125;</span>
    | <span style="color: #ff00cc;">&quot;&quot;</span> ^^^ <span style="color: #66ccff; font-weight: bold;">Nil</span>
  <span style="font-weight: bold;">&#41;</span>
<span style="font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #006699; font-weight: bold;">object</span> RegexpParsers <span style="font-weight: bold;">&#123;</span>
  ...
&nbsp;
  <span style="color: #006699; font-weight: bold;">def</span> opt<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>A<span style="font-weight: bold;">&#93;</span></span><span style="font-weight: bold;">&#40;</span>p: Parser<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>A<span style="font-weight: bold;">&#93;</span></span><span style="font-weight: bold;">&#41;</span> = <span style="font-weight: bold;">&#40;</span>
      p ^^ <span style="font-weight: bold;">&#123;</span> <span style="color: #9966ff;">Some</span><span style="font-weight: bold;">&#40;</span>_<span style="font-weight: bold;">&#41;</span> <span style="font-weight: bold;">&#125;</span>
    | <span style="color: #ff00cc;">&quot;&quot;</span> ^^^ <span style="color: #66ccff; font-weight: bold;">None</span>
  <span style="font-weight: bold;">&#41;</span>
<span style="font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>Readers who have managed to stay awake to this point may notice that I&#8217;m actually cheating a bit in these definitions.&nbsp; Specifically, I&#8217;m using the <code>^^</code> and <code>^^^</code> combinators.&nbsp; These are the semantic action combinators which I promised to avoid discussing.&nbsp; However, for the sake of completeness, I&#8217;ll include the sources and leave you to figure out the rest:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #006699; font-weight: bold;">trait</span> Parser<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>+A<span style="font-weight: bold;">&#93;</span></span> <span style="color: #006699; font-weight: bold;">extends</span> <span style="font-weight: bold;">&#40;</span>Stream<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span><span style="color: #009966; font-weight: bold;">Character</span><span style="font-weight: bold;">&#93;</span></span>=&gt;Result<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>A<span style="font-weight: bold;">&#93;</span></span><span style="font-weight: bold;">&#41;</span> <span style="font-weight: bold;">&#123;</span> outer =&gt;
  ...
&nbsp;
  <span style="color: #006699; font-weight: bold;">def</span> ^^<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>B<span style="font-weight: bold;">&#93;</span></span><span style="font-weight: bold;">&#40;</span>f: A=&gt;B<span style="font-weight: bold;">&#41;</span> = <span style="color: #006699; font-weight: bold;">new</span> Parser<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>B<span style="font-weight: bold;">&#93;</span></span> <span style="font-weight: bold;">&#123;</span>
    <span style="color: #006699; font-weight: bold;">def</span> <span style="color: #9966ff;">apply</span><span style="font-weight: bold;">&#40;</span>s: Stream<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span><span style="color: #009966; font-weight: bold;">Character</span><span style="font-weight: bold;">&#93;</span></span><span style="font-weight: bold;">&#41;</span> = <span style="color: #9966ff;">outer</span><span style="font-weight: bold;">&#40;</span>s<span style="font-weight: bold;">&#41;</span> <span style="color: #006699; font-weight: bold;">match</span> <span style="font-weight: bold;">&#123;</span>
      <span style="color: #006699; font-weight: bold;">case</span> <span style="color: #9966ff;">Success</span><span style="font-weight: bold;">&#40;</span>v, rem<span style="font-weight: bold;">&#41;</span> =&gt; <span style="color: #9966ff;">Success</span><span style="font-weight: bold;">&#40;</span><span style="color: #9966ff;">f</span><span style="font-weight: bold;">&#40;</span>v<span style="font-weight: bold;">&#41;</span>, rem<span style="font-weight: bold;">&#41;</span>
      <span style="color: #006699; font-weight: bold;">case</span> f: Failure =&gt; f
    <span style="font-weight: bold;">&#125;</span>
  <span style="font-weight: bold;">&#125;</span>
&nbsp;
  <span style="color: #006699; font-weight: bold;">def</span> ^^^<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>B<span style="font-weight: bold;">&#93;</span></span><span style="font-weight: bold;">&#40;</span>v: =&gt;B<span style="font-weight: bold;">&#41;</span> = <span style="color: #006699; font-weight: bold;">new</span> Parser<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>B<span style="font-weight: bold;">&#93;</span></span> <span style="font-weight: bold;">&#123;</span>
    <span style="color: #006699; font-weight: bold;">def</span> <span style="color: #9966ff;">apply</span><span style="font-weight: bold;">&#40;</span>s: Stream<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span><span style="color: #009966; font-weight: bold;">Character</span><span style="font-weight: bold;">&#93;</span></span><span style="font-weight: bold;">&#41;</span> = <span style="color: #9966ff;">outer</span><span style="font-weight: bold;">&#40;</span>s<span style="font-weight: bold;">&#41;</span> <span style="color: #006699; font-weight: bold;">match</span> <span style="font-weight: bold;">&#123;</span>
      <span style="color: #006699; font-weight: bold;">case</span> <span style="color: #9966ff;">Success</span><span style="font-weight: bold;">&#40;</span>_, rem<span style="font-weight: bold;">&#41;</span> =&gt; <span style="color: #9966ff;">Success</span><span style="font-weight: bold;">&#40;</span>v, rem<span style="font-weight: bold;">&#41;</span>
      <span style="color: #006699; font-weight: bold;">case</span> f: Failure =&gt; f
    <span style="font-weight: bold;">&#125;</span>
  <span style="font-weight: bold;">&#125;</span>
<span style="font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>In short, these combinators are only interesting to people who want their parsers to give them a value upon completion (usually an AST).&nbsp; In short, just about any useful application of parser combinators will require these combinators, but since we&#8217;re not planning to use our framework for anything useful, there is really no need.</p>
<p>One really interesting parser from our first example which is worthy of attention is the <code>member</code> rule.&nbsp; If you recall, this was defined as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #006699; font-weight: bold;">def</span> member = <span style="font-weight: bold;">&#40;</span>
    <span style="color: #ff00cc;">&quot;val&quot;</span> ~ ID ~ <span style="color: #ff00cc;">&quot;:&quot;</span> ~ ID ~ <span style="color: #ff00cc;">&quot;=&quot;</span> ~ expr
  | <span style="color: #ff00cc;">&quot;var&quot;</span> ~ ID ~ <span style="color: #ff00cc;">&quot;:&quot;</span> ~ ID ~ <span style="color: #ff00cc;">&quot;=&quot;</span> ~ expr
  | <span style="color: #ff00cc;">&quot;def&quot;</span> ~ ID ~ <span style="color: #ff00cc;">&quot;(&quot;</span> ~ formals ~ <span style="color: #ff00cc;">&quot;)&quot;</span> ~ <span style="color: #ff00cc;">&quot;:&quot;</span> ~ ID ~ <span style="color: #ff00cc;">&quot;=&quot;</span> ~ expr
  | <span style="color: #ff00cc;">&quot;def&quot;</span> ~ ID ~ <span style="color: #ff00cc;">&quot;:&quot;</span> ~ ID ~ <span style="color: #ff00cc;">&quot;=&quot;</span> ~ expr
  | <span style="color: #ff00cc;">&quot;type&quot;</span> ~ ID ~ <span style="color: #ff00cc;">&quot;=&quot;</span> ~ ID
<span style="font-weight: bold;">&#41;</span></pre></td></tr></table></div>

<p>This is interesting for two reasons.&nbsp; First: we have multiple disjunctions handled in the same rule, showing that disjunctive parsers chain just as nicely as do sequential.&nbsp; But more importantly, our chain of disjunctions includes two parsers which have the same prefix (<code>"def" ~ ID</code>).&nbsp; In other words, if we attempt to parse an input of &#8220;<code>def a: B = 42</code>&#8220;, one of these deeply nested parsers will erroneously match the input for the first two tokens.</p>
<p>This grammatical feature forces us to implement some sort of backtracking within our parser combinators.&nbsp; Intuitively, the <code>"def" ~ ID</code> parser is going to successfully match &#8220;<code>def a</code>&#8220;, but the enclosing sequence (<code>"def" ~ ID ~ "("</code>) will fail as soon as the &#8220;<code>:</code>&#8221; token is reached.&nbsp; At this point, the parser has to take two steps back in the token stream and try again with another parser, in this case, <code>"def" ~ ID ~ ":" ~ ID ~ "=" ~ expr</code>.&nbsp; It is this feature which separates LL(*) parsing from LL(1) and LL(0).</p>
<p>The good news is that we already have this feature almost by accident.&nbsp; Well, obviously not by accident since I put some careful planning into this article, but at no point so far did we actually <em>set out</em> to implement backtracking, and yet it has somehow dropped into our collective lap.&nbsp; Consider once more the implementation of the disjunctive parser:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #006699; font-weight: bold;">class</span> DisParser<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>+A<span style="font-weight: bold;">&#93;</span></span><span style="font-weight: bold;">&#40;</span>left: Parser<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>A<span style="font-weight: bold;">&#93;</span></span>, right: Parser<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>A<span style="font-weight: bold;">&#93;</span></span><span style="font-weight: bold;">&#41;</span> <span style="color: #006699; font-weight: bold;">extends</span> Parser<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>A<span style="font-weight: bold;">&#93;</span></span> <span style="font-weight: bold;">&#123;</span>
  <span style="color: #006699; font-weight: bold;">def</span> <span style="color: #9966ff;">apply</span><span style="font-weight: bold;">&#40;</span>s: Stream<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span><span style="color: #009966; font-weight: bold;">Character</span><span style="font-weight: bold;">&#93;</span></span><span style="font-weight: bold;">&#41;</span> = <span style="color: #9966ff;">left</span><span style="font-weight: bold;">&#40;</span>s<span style="font-weight: bold;">&#41;</span> <span style="color: #006699; font-weight: bold;">match</span> <span style="font-weight: bold;">&#123;</span>
    <span style="color: #006699; font-weight: bold;">case</span> res: Success =&gt; res
    <span style="color: #006699; font-weight: bold;">case</span> _: Failure =&gt; <span style="color: #9966ff;">right</span><span style="font-weight: bold;">&#40;</span>s<span style="font-weight: bold;">&#41;</span>
  <span style="font-weight: bold;">&#125;</span>
<span style="font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>Notice what happens if <code>left</code> fails: it invokes the <code>right</code> parser passing the <em>same</em> <code>Stream</code> instance (<code>s</code>).&nbsp; Recall that <code>Stream</code> is immutable, meaning that there is nothing <code>left</code> can do which could possibly change the value of <code>s</code>.&nbsp; Each parser is merely grabbing characters from the head of the stream and then producing a <em>new</em> <code>Stream</code> which represents the remainder.&nbsp; Parsers farther up the line (like our disjunctive parser) are still holding a reference to the stream <em>prior</em> to these &#8220;removals&#8221;.&nbsp; This means that we don&#8217;t need to make any special effort to implement backtracking, it just falls out as a natural consequence of our use of the <code>Stream</code> data structure.&nbsp; Isn&#8217;t that nifty?</p>
<h3>Conclusion</h3>
<p>Parser combinators are an incredibly clever bit of functional programming.&nbsp; Every time I think about them, I am once again impressed by the ingenuity of their design and the simple elegance of their operation.&nbsp; The fact that two combinators and a single parser can encode the vast diversity of LL(*) grammars is simply mind-boggling.&nbsp; Despite their simplicity, parser combinators are capable of some very powerful parsing in a very clean and intuitive fashion.&nbsp; That to me is magical.</p>
<img src="http://feeds.feedburner.com/~r/codecommit/~4/P8VHEcXpgCQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codecommit.com/blog/scala/the-magic-behind-parser-combinators/feed</wfw:commentRss>
		<feedburner:origLink>http://www.codecommit.com/blog/scala/the-magic-behind-parser-combinators</feedburner:origLink></item>
		<item>
		<title>Interop Between Java and Scala</title>
		<link>http://feeds.codecommit.com/~r/codecommit/~3/W3DlbKROU-8/interop-between-java-and-scala</link>
		<comments>http://www.codecommit.com/blog/java/interop-between-java-and-scala#comments</comments>
		<pubDate>Mon, 09 Feb 2009 07:00:00 +0000</pubDate>
		<dc:creator>Daniel Spiewak</dc:creator>
		
		<category><![CDATA[Scala]]></category>

		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.codecommit.com/blog/java/interop-between-java-and-scala</guid>
		<description><![CDATA[Sometimes, the simplest things are the most difficult to explain.&#160; Scala&#8217;s interoperability with Java is completely unparalleled, even including languages like Groovy which tout their tight integration with the JVM&#8217;s venerable standard-bearer.&#160; However, despite this fact, there is almost no documentation (aside from chapter 29 in Programming in Scala) which shows how this Scala/Java integration [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes, the simplest things are the most difficult to explain.&nbsp; Scala&#8217;s interoperability with Java is completely unparalleled, even including languages like Groovy which tout their tight integration with the JVM&#8217;s venerable standard-bearer.&nbsp; However, despite this fact, there is almost no documentation (aside from chapter 29 in <em>Programming in Scala</em>) which shows how this Scala/Java integration works and where it can be used.&nbsp; So while it may not be the most exciting or theoretically interesting topic, I have taken it upon myself to fill the gap.</p>
<h3>Classes are Classes</h3>
<p>The first piece of knowledge you need about Scala is that Scala classes are real JVM classes.&nbsp; Consider the following snippets, the first in Java:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="java5"><span style="color: #006699; font-weight: bold;">public</span> <span style="color: #0099ff; font-weight: bold;">class</span> Person <span style="font-weight: bold;">&#123;</span>
    <span style="color: #006699; font-weight: bold;">public</span> String <span style="color: #9966ff;">getName</span><span style="font-weight: bold;">&#40;</span><span style="font-weight: bold;">&#41;</span> <span style="font-weight: bold;">&#123;</span>
        <span style="color: #006699; font-weight: bold;">return</span> <span style="color: #ff00cc;">&quot;Daniel Spiewak&quot;</span>;
    <span style="font-weight: bold;">&#125;</span>
<span style="font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>&#8230;and the second in Scala:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #006699; font-weight: bold;">class</span> Person <span style="font-weight: bold;">&#123;</span>
  <span style="color: #006699; font-weight: bold;">def</span> <span style="color: #9966ff;">getName</span><span style="font-weight: bold;">&#40;</span><span style="font-weight: bold;">&#41;</span> = <span style="color: #ff00cc;">&quot;Daniel Spiewak&quot;</span>
<span style="font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>Despite the very different syntax, both of these snippets will produce almost identical bytecode when compiled.&nbsp; Both will result in a single file, <code>Person.class</code>, which contains a default, no-args constructor and a public method, <code>getName()</code>, with return type <code>java.lang.String</code>.&nbsp; Both classes may be used from Scala:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #006699; font-weight: bold;">val</span> p = <span style="color: #006699; font-weight: bold;">new</span> <span style="color: #9966ff;">Person</span><span style="font-weight: bold;">&#40;</span><span style="font-weight: bold;">&#41;</span>
p.<span style="color: #9966ff;">getName</span><span style="font-weight: bold;">&#40;</span><span style="font-weight: bold;">&#41;</span>       <span style="color: #cc0000;">// =&gt; &quot;Daniel Spiewak&quot;</span></pre></td></tr></table></div>

<p>&#8230;and from Java:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="java5">Person p = <span style="color: #006699; font-weight: bold;">new</span> <span style="color: #9966ff;">Person</span><span style="font-weight: bold;">&#40;</span><span style="font-weight: bold;">&#41;</span>;
p.<span style="color: #9966ff;">getName</span><span style="font-weight: bold;">&#40;</span><span style="font-weight: bold;">&#41;</span>;      <span style="color: #ff8400;">// =&gt; &quot;Daniel Spiewak&quot;</span></pre></td></tr></table></div>

<p>In the case of either language, we can easily swap implementations of the <code>Person</code> class without making any changes to the call-site.&nbsp; In short, you can use Scala classes from Java (as well as Java classes from Scala) without ever even knowing that they were defined within another language.</p>
<p>This single property is the very cornerstone of Scala&#8217;s philosophy of bytecode translation.&nbsp; Wherever possible &mdash; and that being more often than not &mdash; Scala elements are translated into bytecode which <em>directly</em> corresponds to the equivalent feature in Java.&nbsp; Scala classes equate to Java classes, methods and fields within those classes become Java methods and fields.</p>
<p>This allows some pretty amazing cross-language techniques.&nbsp; For example, I can extend a Java class within Scala, overriding some methods.&nbsp; I can in turn extend this Scala class from within Java once again with everything working exactly as anticipated:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #006699; font-weight: bold;">class</span> MyAbstractButton <span style="color: #006699; font-weight: bold;">extends</span> JComponent <span style="font-weight: bold;">&#123;</span>
  <span style="color: #006699; font-weight: bold;">private</span> <span style="color: #006699; font-weight: bold;">var</span> pushed = <span style="color: #cc00cc;">false</span>
&nbsp;
  <span style="color: #006699; font-weight: bold;">def</span> <span style="color: #9966ff;">setPushed</span><span style="font-weight: bold;">&#40;</span>p: <span style="color: #009966; font-weight: bold;">Boolean</span><span style="font-weight: bold;">&#41;</span> <span style="font-weight: bold;">&#123;</span>
    pushed = p
  <span style="font-weight: bold;">&#125;</span>
&nbsp;
  <span style="color: #006699; font-weight: bold;">def</span> getPushed = pushed
&nbsp;
  <span style="color: #006699; font-weight: bold;">override</span> <span style="color: #006699; font-weight: bold;">def</span> <span style="color: #9966ff;">paintComponent</span><span style="font-weight: bold;">&#40;</span>g: Graphics<span style="font-weight: bold;">&#41;</span> <span style="font-weight: bold;">&#123;</span>
    <span style="color: #cc00cc;">super</span>.<span style="color: #9966ff;">paintComponent</span><span style="font-weight: bold;">&#40;</span>g<span style="font-weight: bold;">&#41;</span>
&nbsp;
    <span style="color: #cc0000;">// draw a button</span>
  <span style="font-weight: bold;">&#125;</span>
<span style="font-weight: bold;">&#125;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="java5"><span style="color: #006699; font-weight: bold;">public</span> <span style="color: #0099ff; font-weight: bold;">class</span> ProKitButton <span style="color: #006699; font-weight: bold;">extends</span> MyAbstractButton <span style="font-weight: bold;">&#123;</span>
    <span style="color: #ff8400;">// do something uniquely Apple-esque</span>
<span style="font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<h3>Traits are Interfaces</h3>
<p>This is probably the one interoperability note which is the <em>least</em> well-known.&nbsp; Scala&#8217;s traits are vastly more powerful than Java&#8217;s interfaces, often leading developers to the erroneous conclusion that they are incompatible.&nbsp; Specifically, traits allow method definitions, while interfaces must be purely-abstract.&nbsp; Yet, despite this significant distinction, Scala is still able to compile traits into interfaces at the bytecode level&#8230;with some minor enhancements.</p>
<p>The simplest case is when the trait only contains abstract members.&nbsp; For example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #006699; font-weight: bold;">trait</span> Model <span style="font-weight: bold;">&#123;</span>
  <span style="color: #006699; font-weight: bold;">def</span> value: <span style="color: #0099ff; font-weight: bold;">Any</span>
<span style="font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>If we look at the bytecode generated by compiling this trait, we will see that it is actually equivalent to the following Java definition:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="java5"><span style="color: #006699; font-weight: bold;">public</span> <span style="color: #0099ff; font-weight: bold;">interface</span> Model <span style="font-weight: bold;">&#123;</span>
    <span style="color: #006699; font-weight: bold;">public</span> Object <span style="color: #9966ff;">value</span><span style="font-weight: bold;">&#40;</span><span style="font-weight: bold;">&#41;</span>;
<span style="font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>Thus, we can declare traits in Scala and implement them as interfaces in Java classes:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="java5"><span style="color: #006699; font-weight: bold;">public</span> <span style="color: #0099ff; font-weight: bold;">class</span> StringModel <span style="color: #006699; font-weight: bold;">implements</span> Model <span style="font-weight: bold;">&#123;</span>
    <span style="color: #006699; font-weight: bold;">public</span> Object <span style="color: #9966ff;">value</span><span style="font-weight: bold;">&#40;</span><span style="font-weight: bold;">&#41;</span> <span style="font-weight: bold;">&#123;</span>
        <span style="color: #006699; font-weight: bold;">return</span> <span style="color: #ff00cc;">&quot;Hello, World!&quot;</span>;
    <span style="font-weight: bold;">&#125;</span>
<span style="font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>This is precisely equivalent to a Scala class which mixes-in the <code>Model</code> trait:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #006699; font-weight: bold;">class</span> StringModel <span style="color: #006699; font-weight: bold;">extends</span> Model <span style="font-weight: bold;">&#123;</span>
  <span style="color: #006699; font-weight: bold;">def</span> value = <span style="color: #ff00cc;">&quot;Hello, World!&quot;</span>
<span style="font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>Things start to get a little sticky when we have method definitions within our traits.&nbsp; For example, we could add a <code>printValue()</code> method to our <code>Model</code> trait:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #006699; font-weight: bold;">trait</span> Model <span style="font-weight: bold;">&#123;</span>
  <span style="color: #006699; font-weight: bold;">def</span> value: <span style="color: #0099ff; font-weight: bold;">Any</span>
&nbsp;
  <span style="color: #006699; font-weight: bold;">def</span> <span style="color: #9966ff;">printValue</span><span style="font-weight: bold;">&#40;</span><span style="font-weight: bold;">&#41;</span> <span style="font-weight: bold;">&#123;</span>
    <span style="color: #9966ff;">println</span><span style="font-weight: bold;">&#40;</span>value<span style="font-weight: bold;">&#41;</span>
  <span style="font-weight: bold;">&#125;</span>
<span style="font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>Obviously, we can&#8217;t directly translate this into <em>just</em> an interface; something else will be required.&nbsp; Scala solves this problem by introducing an ancillary class which contains all of the method definitions for a given trait.&nbsp; Thus, when we look at the translation for our modified <code>Model</code> trait, the result looks something like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="java5"><span style="color: #006699; font-weight: bold;">public</span> <span style="color: #0099ff; font-weight: bold;">interface</span> Model <span style="color: #006699; font-weight: bold;">extends</span> ScalaObject <span style="font-weight: bold;">&#123;</span>
    <span style="color: #006699; font-weight: bold;">public</span> Object <span style="color: #9966ff;">value</span><span style="font-weight: bold;">&#40;</span><span style="font-weight: bold;">&#41;</span>;
&nbsp;
    <span style="color: #006699; font-weight: bold;">public</span> <span style="color: #0099ff; font-weight: bold;">void</span> <span style="color: #9966ff;">printValue</span><span style="font-weight: bold;">&#40;</span><span style="font-weight: bold;">&#41;</span>;
<span style="font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #006699; font-weight: bold;">public</span> <span style="color: #0099ff; font-weight: bold;">class</span> Model$class <span style="font-weight: bold;">&#123;</span>
    <span style="color: #006699; font-weight: bold;">public</span> <span style="color: #006699; font-weight: bold;">static</span> <span style="color: #0099ff; font-weight: bold;">void</span> <span style="color: #9966ff;">printValue</span><span style="font-weight: bold;">&#40;</span>Model self<span style="font-weight: bold;">&#41;</span> <span style="font-weight: bold;">&#123;</span>
        System.<span class="me1">out</span>.<span style="color: #9966ff;">println</span><span style="font-weight: bold;">&#40;</span>self.<span style="color: #9966ff;">value</span><span style="font-weight: bold;">&#40;</span><span style="font-weight: bold;">&#41;</span><span style="font-weight: bold;">&#41;</span>;
    <span style="font-weight: bold;">&#125;</span>
<span style="font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>Thus, we can get the <em>effect</em> of Scala&#8217;s powerful mixin inheritance within Java by implementing the <code>Model</code> trait and delegating from the <code>printValue()</code> method to the <code>Model$class</code> implementation:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="java5"><span style="color: #006699; font-weight: bold;">public</span> <span style="color: #0099ff; font-weight: bold;">class</span> StringModel <span style="color: #006699; font-weight: bold;">implements</span> Model <span style="font-weight: bold;">&#123;</span>
    <span style="color: #006699; font-weight: bold;">public</span> Object <span style="color: #9966ff;">value</span><span style="font-weight: bold;">&#40;</span><span style="font-weight: bold;">&#41;</span> <span style="font-weight: bold;">&#123;</span>
        <span style="color: #006699; font-weight: bold;">return</span> <span style="color: #ff00cc;">&quot;Hello, World!&quot;</span>;
    <span style="font-weight: bold;">&#125;</span>
&nbsp;
    <span style="color: #006699; font-weight: bold;">public</span> <span style="color: #0099ff; font-weight: bold;">void</span> <span style="color: #9966ff;">printValue</span><span style="font-weight: bold;">&#40;</span><span style="font-weight: bold;">&#41;</span> <span style="font-weight: bold;">&#123;</span>
        Model$class.<span style="color: #9966ff;">printValue</span><span style="font-weight: bold;">&#40;</span><span style="color: #cc00cc;">this</span><span style="font-weight: bold;">&#41;</span>;
    <span style="font-weight: bold;">&#125;</span>
&nbsp;
    <span style="color: #ff8400;">// method missing here (see below)</span>
<span style="font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>It&#8217;s not perfect, but it allows us to use some of Scala&#8217;s more advanced trait-based functionality from within Java.&nbsp; Incidentally, the above code <em>does</em> compile without a problem.&nbsp; I wasn&#8217;t actually aware of this fact, but &#8220;<code>$</code>&#8221; is a legal character in Java identifiers, allowing interaction with some of Scala&#8217;s more interesting features.</p>
<p>There is, however, one little wrinkle that I&#8217;m conveniently side-stepping: the <code>$tag</code> method.&nbsp; This is a method defined within the <code>ScalaObject</code> trait designed to help optimize pattern matching.&nbsp; Unfortunately, it also means yet another abstract method which must be defined when implementing Scala traits which contain method definitions.&nbsp; The correct version of the <code>StringModel</code> class from above actually looks like the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="java5"><span style="color: #006699; font-weight: bold;">public</span> <span style="color: #0099ff; font-weight: bold;">class</span> StringModel <span style="color: #006699; font-weight: bold;">implements</span> Model <span style="font-weight: bold;">&#123;</span>
    <span style="color: #006699; font-weight: bold;">public</span> Object <span style="color: #9966ff;">value</span><span style="font-weight: bold;">&#40;</span><span style="font-weight: bold;">&#41;</span> <span style="font-weight: bold;">&#123;</span>
        <span style="color: #006699; font-weight: bold;">return</span> <span style="color: #ff00cc;">&quot;Hello, World!&quot;</span>;
    <span style="font-weight: bold;">&#125;</span>
&nbsp;
    <span style="color: #006699; font-weight: bold;">public</span> <span style="color: #0099ff; font-weight: bold;">void</span> <span style="color: #9966ff;">printValue</span><span style="font-weight: bold;">&#40;</span><span style="font-weight: bold;">&#41;</span> <span style="font-weight: bold;">&#123;</span>
        Model$class.<span style="color: #9966ff;">printValue</span><span style="font-weight: bold;">&#40;</span><span style="color: #cc00cc;">this</span><span style="font-weight: bold;">&#41;</span>;
    <span style="font-weight: bold;">&#125;</span>
&nbsp;
    <span style="color: #006699; font-weight: bold;">public</span> <span style="color: #0099ff; font-weight: bold;">int</span> $tag<span style="font-weight: bold;">&#40;</span><span style="font-weight: bold;">&#41;</span> <span style="font-weight: bold;">&#123;</span>
        <span style="color: #006699; font-weight: bold;">return</span> <span style="color: #ff0000;">0</span>;
    <span style="font-weight: bold;">&#125;</span>
<span style="font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>To be honest, I&#8217;m not sure what is the &#8220;correct&#8221; value to return from <code>$tag</code>.&nbsp; In this case, <code>0</code> is just a stub, and I&#8217;m guessing a safe one since <code>StringModel</code> is the only subtype of <code>Model</code>.&nbsp; Can anyone who knows more about the Scala compiler shed some light on this issue?</p>
<h3>Generics are, well&#8230;Generics</h3>
<p>Generics are (I think) probably the coolest and most well-done part of Scala&#8217;s Java interop.&nbsp; Anyone who has more than a passing familiarity with Scala will know that its type system is significantly more powerful than Java&#8217;s.&nbsp; Some of this power comes in the form of its type parameterization, which is vastly superior to Java&#8217;s generics.&nbsp; For example, type variance can be handled at declaration-site, rather than only call-site (as in Java):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #006699; font-weight: bold;">abstract</span> <span style="color: #006699; font-weight: bold;">class</span> List<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>+A<span style="font-weight: bold;">&#93;</span></span> <span style="font-weight: bold;">&#123;</span>
  ...
<span style="font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>The <code>+</code> notation prefixing the <code>A</code> type parameter on the <code>List</code> class means that <code>List</code> will vary covariantly with its parameter.&nbsp; In English, this means that <code>List[String]</code> is a subtype of <code>List[Any]</code> (because <code>String</code> is a subtype of <code>Any</code>).&nbsp; This is a very intuitive relationship, but one which Java is incapable of expressing.</p>
<p>Fortunately, Scala is able to exploit one of the JVM&#8217;s most maligned features to support things like variance and higher-kinds without sacrificing perfect Java interop.&nbsp; Thanks to type erasure, Scala generics can be compiled to Java generics without any loss of functionality on the Scala side.&nbsp; Thus, the Java translation of the <code>List</code> definition above would be as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="java5"><span style="color: #006699; font-weight: bold;">public</span> <span style="color: #006699; font-weight: bold;">abstract</span> <span style="color: #0099ff; font-weight: bold;">class</span> List&lt;A&gt; <span style="font-weight: bold;">&#123;</span>
    ...
<span style="font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>The variance annotation is gone, but Java wouldn&#8217;t be able to make anything of it anyway.&nbsp; The huge advantage to this translation scheme is it means that Java&#8217;s generics and Scala&#8217;s generics are one and the same at the bytecode level.&nbsp; Thus, Java can use generic Scala classes without a second thought:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="java5"><span style="color: #009966; font-weight: bold;">import</span> scala.<span class="me1">Tuple2</span>;
&nbsp;
...
<span class="me1">Tuple2</span>&lt;String, String&gt; me = <span style="color: #006699; font-weight: bold;">new</span> Tuple2&lt;String, String&gt;<span style="font-weight: bold;">&#40;</span><span style="color: #ff00cc;">&quot;Daniel&quot;</span>, <span style="color: #ff00cc;">&quot;Spiewak&quot;</span><span style="font-weight: bold;">&#41;</span>;</pre></td></tr></table></div>

<p>Obviously, this is a lot more verbose than the Scala equivalent, &#8220;<code>("Daniel", "Spiewak")</code>&#8220;, but at least it works.</p>
<h3>Operators are Methods</h3>
<p>One of the most obvious differences between Java and Scala is that Scala supports operator overloading.&nbsp; In fact, Scala supports a variant of operator overloading which is far stronger than anything offered by C++, C# or even Ruby.&nbsp; With very few exceptions, <em>any</em> symbol may be used to define a custom operator.&nbsp; This provides tremendous flexibility in DSLs and even your average, every-day API (such as <code>List</code> and <code>Map</code>).</p>
<p>Obviously, this particular language feature is not going to translate into Java quite so nicely.&nbsp; Java doesn&#8217;t support operator overloading of <em>any</em> variety, much less the über-powerful form defined by Scala.&nbsp; Thus, Scala operators must be compiled into an entirely non-symbolic form at the bytecode level, otherwise Java interop would be irreparably broken, and the JVM itself would be unable to swallow the result.</p>
<p>A good starting place for deciding on this translation is the way in which operators are declared in Scala: as methods.&nbsp; Every Scala operator (including unary operators like <code>!</code>) is defined as a method within a class:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #006699; font-weight: bold;">abstract</span> <span style="color: #006699; font-weight: bold;">class</span> List<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>+A<span style="font-weight: bold;">&#93;</span></span> <span style="font-weight: bold;">&#123;</span>
  <span style="color: #006699; font-weight: bold;">def</span> ::<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>B &gt;: A<span style="font-weight: bold;">&#93;</span></span><span style="font-weight: bold;">&#40;</span>e: B<span style="font-weight: bold;">&#41;</span> = ...
&nbsp;
  <span style="color: #006699; font-weight: bold;">def</span> +<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>B &gt;: A<span style="font-weight: bold;">&#93;</span></span><span style="font-weight: bold;">&#40;</span>e: B<span style="font-weight: bold;">&#41;</span> = ...
<span style="font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>Since Scala classes become Java classes and Scala methods become Java methods, the most obvious translation would be to take each operator method and produce a corresponding Java method with a heavily-translated name.&nbsp; In fact, this is exactly what Scala does.&nbsp; The above class will compile into the equivalent of this Java code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="java5"><span style="color: #006699; font-weight: bold;">public</span> <span style="color: #006699; font-weight: bold;">abstract</span> <span style="color: #0099ff; font-weight: bold;">class</span> List&lt;A&gt; <span style="font-weight: bold;">&#123;</span>
    <span style="color: #006699; font-weight: bold;">public</span> &lt;B <span style="color: #cc00cc;">super</span> A&gt; List&lt;B&gt; $colon$colon<span style="font-weight: bold;">&#40;</span>B e<span style="font-weight: bold;">&#41;</span> <span style="font-weight: bold;">&#123;</span> ... <span style="font-weight: bold;">&#125;</span>
&nbsp;
    <span style="color: #006699; font-weight: bold;">public</span> &lt;B <span style="color: #cc00cc;">super</span> A&gt; List&lt;B&gt; $plus<span style="font-weight: bold;">&#40;</span>B e<span style="font-weight: bold;">&#41;</span> <span style="font-weight: bold;">&#123;</span> ... <span style="font-weight: bold;">&#125;</span>
<span style="font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>Every allowable symbol in Scala&#8217;s method syntax has a corresponding translation of the form &#8220;<code>$</code><em>trans</em>&#8220;.&nbsp; A list of supported translations is one of those pieces of documentation that you would expect to find on the Scala website.&nbsp; However, alas, it is absent.&nbsp; The following is a table of all of the translations of which I am aware:</p>
<div align="center">
<table border="1">
<tbody>
<tr>
<td><b>Scala Operator</b></td>
<td><b>Compiles To</b></td>
</tr>
<tr>
<td><code>=</code></td>
<td><code>$eq</code></td>
</tr>
<tr>
<td><code>&gt;</code></td>
<td><code>$greater</code></td>
</tr>
<tr>
<td><code>&lt;</code></td>
<td><code>$less</code></td>
</tr>
<tr>
<td><code>+</code></td>
<td><code>$plus</code></td>
</tr>
<tr>
<td><code>-</code></td>
<td><code>$minus</code></td>
</tr>
<tr>
<td><code>*</code></td>
<td><code>$times</code></td>
</tr>
<tr>
<td><code>/</code></td>
<td><code>div</code></td>
</tr>
<tr>
<td><code>!</code></td>
<td><code>$bang</code></td>
</tr>
<tr>
<td><code>@</code></td>
<td><code>$at</code></td>
</tr>
<tr>
<td><code>#</code></td>
<td><code>$hash</code></td>
</tr>
<tr>
<td><code>%</code></td>
<td><code>$percent</code></td>
</tr>
<tr>
<td><code>^</code></td>
<td><code>$up</code></td>
</tr>
<tr>
<td><code>&amp;</code></td>
<td><code>$amp</code></td>
</tr>
<tr>
<td><code>~</code></td>
<td><code>$tilde</code></td>
</tr>
<tr>
<td><code>?</code></td>
<td><code>$qmark</code></td>
</tr>
<tr>
<td><code>|</code></td>
<td><code>$bar</code></td>
</tr>
<tr>
<td><code>\</code></td>
<td><code>$bslash</code></td>
</tr>
<tr>
<td><code>:</code></td>
<td><code>$colon</code></td>
</tr>
</tbody>
</table>
</div>
<p>Using this table, you should be able to derive the &#8220;real name&#8221; of any Scala operator, allowing its use from within Java.&nbsp; Of course, the idea solution would be if Java actually supported operator overloading and could use Scala&#8217;s operators directly, but somehow I doubt that will happen any time soon.</p>
<h3>Odds and Ends</h3>
<p>One final tidbit which might be useful: <code>@BeanProperty</code>.&nbsp; This is a special annotation which is essentially read by the Scala compiler to mean &#8220;generate a getter and setter for this field&#8221;:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #006699; font-weight: bold;">import</span> scala.<span class="me1">reflect</span>.<span class="me1">BeanProperty</span>
&nbsp;
<span style="color: #006699; font-weight: bold;">class</span> Person <span style="font-weight: bold;">&#123;</span>
  <span style="color: #02b902;">@BeanProperty</span>
  <span style="color: #006699; font-weight: bold;">var</span> name = <span style="color: #ff00cc;">&quot;Daniel Spiewak&quot;</span>
<span style="font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>The need for this annotation comes from the fact that Scala&#8217;s ever-convenient <code>var</code> and <code>val</code> declarations actually generate code which looks like the following (assuming no <code>@BeanProperty</code> annotation):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="java5"><span style="color: #ff8400;">// *without* @BeanProperty</span>
<span style="color: #006699; font-weight: bold;">public</span> <span style="color: #0099ff; font-weight: bold;">class</span> Person <span style="font-weight: bold;">&#123;</span>
    <span style="color: #006699; font-weight: bold;">private</span> String name = <span style="color: #ff00cc;">&quot;Daniel Spiewak&quot;</span>;
&nbsp;
    <span style="color: #006699; font-weight: bold;">public</span> String <span style="color: #9966ff;">name</span><span style="font-weight: bold;">&#40;</span><span style="font-weight: bold;">&#41;</span> <span style="font-weight: bold;">&#123;</span>
        <span style="color: #006699; font-weight: bold;">return</span> name;
    <span style="font-weight: bold;">&#125;</span>
&nbsp;
    <span style="color: #006699; font-weight: bold;">public</span> <span style="color: #0099ff; font-weight: bold;">void</span> name_$eq<span style="font-weight: bold;">&#40;</span>String name<span style="font-weight: bold;">&#41;</span> <span style="font-weight: bold;">&#123;</span>
        <span style="color: #cc00cc;">this</span>.<span class="me1">name</span> = name;
    <span style="font-weight: bold;">&#125;</span>
<span style="font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>This works well from Scala, but as you can see, Java-land is not quite paradise.&nbsp; While it is certainly feasible to use the <code>_$eq</code> syntax instead of the familiar <code>set</code>/<code>get</code>/<code>is</code> triumvirate, it is not an ideal situation.</p>
<p>Adding the <code>@BeanProperty</code> annotation (as we have done in the earlier Scala snippet) solves this problem by causing the Scala compiler to auto-generate more than one pair of methods for that particular field.&nbsp; Rather than just <code>value</code> and <code>value_$eq</code>, it will also generate the familiar <code>getValue</code> and <code>setValue</code> combination that all Java developers will know and love.&nbsp; Thus, the actual translation resulting from the <code>Person</code> class in Scala will be as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="java5"><span style="color: #006699; font-weight: bold;">public</span> <span style="color: #0099ff; font-weight: bold;">class</span> Person <span style="font-weight: bold;">&#123;</span>
    <span style="color: #006699; font-weight: bold;">private</span> String name = <span style="color: #ff00cc;">&quot;Daniel Spiewak&quot;</span>;
&nbsp;
    <span style="color: #006699; font-weight: bold;">public</span> String <span style="color: #9966ff;">name</span><span style="font-weight: bold;">&#40;</span><span style="font-weight: bold;">&#41;</span> <span style="font-weight: bold;">&#123;</span>
        <span style="color: #006699; font-weight: bold;">return</span> name;
    <span style="font-weight: bold;">&#125;</span>
&nbsp;
    <span style="color: #006699; font-weight: bold;">public</span> String <span style="color: #9966ff;">getName</span><span style="font-weight: bold;">&#40;</span><span style="font-weight: bold;">&#41;</span> <span style="font-weight: bold;">&#123;</span>
        <span style="color: #006699; font-weight: bold;">return</span> <span style="color: #9966ff;">name</span><span style="font-weight: bold;">&#40;</span><span style="font-weight: bold;">&#41;</span>;
    <span style="font-weight: bold;">&#125;</span>
&nbsp;
    <span style="color: #006699; font-weight: bold;">public</span> <span style="color: #0099ff; font-weight: bold;">void</span> name_$eq<span style="font-weight: bold;">&#40;</span>String name<span style="font-weight: bold;">&#41;</span> <span style="font-weight: bold;">&#123;</span>
        <span style="color: #cc00cc;">this</span>.<span class="me1">name</span> = name;
    <span style="font-weight: bold;">&#125;</span>
&nbsp;
    <span style="color: #006699; font-weight: bold;">public</span> <span style="color: #0099ff; font-weight: bold;">void</span> <span style="color: #9966ff;">setName</span><span style="font-weight: bold;">&#40;</span>String name<span style="font-weight: bold;">&#41;</span> <span style="font-weight: bold;">&#123;</span>
        name_$eq<span style="font-weight: bold;">&#40;</span>name<span style="font-weight: bold;">&#41;</span>;
    <span style="font-weight: bold;">&#125;</span>
<span style="font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>This merely provides a pair of delegates, but it does suffice to smooth out the mismatch between Java Bean-based frameworks and Scala&#8217;s elegant instance fields.</p>
<h3>Conclusion</h3>
<p>This has been a whirlwind, disjoint tour covering a fairly large slice of information on how to use Scala code from within Java.&nbsp; For the most part, things are all roses and fairy tales.&nbsp; Scala classes map precisely onto Java classes, generics work perfectly, and pure-abstract traits correspond directly to Java interfaces.&nbsp; Other areas where Scala is decidedly more powerful than Java (like operators) do tend to be a bit sticky, but there is <em>always</em> a way to make things work.</p>
<p>If you&#8217;re considering mixing Scala and Java sources within your project, I hope that this article has smoothed over some of the doubts you may have had regarding its feasibility.&nbsp; As David Pollack says, Scala is really &#8220;just another Java library&#8221;.&nbsp; Just stick <code>scala-library.jar</code> on your classpath and all of your Scala classes should be readily available within your Java application.&nbsp; And given how well Scala integrates with Java at the language level, what could be simpler?</p>
<img src="http://feeds.feedburner.com/~r/codecommit/~4/W3DlbKROU-8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codecommit.com/blog/java/interop-between-java-and-scala/feed</wfw:commentRss>
		<feedburner:origLink>http://www.codecommit.com/blog/java/interop-between-java-and-scala</feedburner:origLink></item>
		<item>
		<title>Hacking Buildr: Interactive Shell Support</title>
		<link>http://feeds.codecommit.com/~r/codecommit/~3/2E_dZTn8dF8/hacking-buildr-interactive-shell-support</link>
		<comments>http://www.codecommit.com/blog/java/hacking-buildr-interactive-shell-support#comments</comments>
		<pubDate>Mon, 12 Jan 2009 07:52:51 +0000</pubDate>
		<dc:creator>Daniel Spiewak</dc:creator>
		
		<category><![CDATA[Scala]]></category>

		<category><![CDATA[Ruby]]></category>

		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.codecommit.com/blog/java/hacking-buildr-interactive-shell-support</guid>
		<description><![CDATA[Last week, we looked at the unfortunately-unexplored topic of Scala/Java joint compilation.&#160; Specifically, we saw several different ways in which this functionality may be invoked covering a number of different tools.&#160; Among these tools was Buildr, a fast Ruby-based drop-in replacement for Maven with a penchant for simple configuration.&#160; In the article I mentioned that [...]]]></description>
			<content:encoded><![CDATA[<p>Last week, we looked at the unfortunately-unexplored topic of <a href="http://www.codecommit.com/blog/scala/joint-compilation-of-scala-and-java-sources">Scala/Java joint compilation</a>.&nbsp; Specifically, we saw several different ways in which this functionality may be invoked covering a number of different tools.&nbsp; Among these tools was Buildr, a fast Ruby-based drop-in replacement for Maven with a penchant for simple configuration.&nbsp; In the article I mentioned that Buildr doesn&#8217;t actually have support for the Scala joint compiler out of the box.&nbsp; In fact, this feature actually requires the use of a Buildr fork I&#8217;ve been using to experiment with different extensions.&nbsp; Among these extensions is a feature I&#8217;ve been wanting from Buildr for a long time: the ability to launch a pre-configured interactive shell.</p>
<p>For those coming from a primarily-Java background, the concept of an interactive shell may seem a bit foreign.&nbsp; Basically, an interactive shell &mdash; or <a href="http://en.wikipedia.org/wiki/REPL"><em>REPL</em></a>, as it is often called &mdash; is a line-by-line language interpreter which allows you to execute snippets of code with immediate result.&nbsp; This has been a common tool in the hands of dynamic language enthusiasts since the days of LISP, but has only recently found its way into the world of mainstream static languages such as Scala.</p>
<div style="text-align:center;"><img src="http://www.codecommit.com/blog/wp-content/uploads/2009/01/interactive-shells.png" alt="interactive-shells.png" border="0"/></div>
<p>One of the most useful applications of these tools is the testing of code (particularly frameworks) before the implementations are fully completed.&nbsp; For example, when working on <a href="http://github.com/djspiewak/scala-collections/tree/master/src/main/scala/com/codecommit/collection/Vector.scala">my port of Clojure&#8217;s <code>PersistentVector</code></a>, I would often spin up a Scala shell to quickly test one aspect or another of the class.&nbsp; As a minor productivity plug, <a href="http://www.zeroturnaround.com/javarebel/">JavaRebel</a> is a truly <em>invaluable</em> tool for development of this variety.</p>
<p>The problem with this pattern of work is it requires the interactive shell in question to be pre-configured to include the project&#8217;s output directory on the CLASSPATH.&nbsp; While this isn&#8217;t usually so bad, things can get very sticky when you&#8217;re working with a project which includes a large number of dependencies.&nbsp; It isn&#8217;t too unreasonable to imagine shell invocations stretching into the tens of lines, just to spin up a &#8220;quick and dirty&#8221; test tool.</p>
<p>Further complicating affairs is the fact that many projects do away with the notion of fixed dependency paths and simply allow tools like Maven or Buildr to manage the CLASSPATH entirely out of sight.&nbsp; In order to fire up a Scala shell for a project with any external dependencies, I must first manually read my <code>buildfile</code>, parsing out all of the artifacts in use.&nbsp; Then I have to grope about in my <code>~/.m2/repository</code> directory until I find the JARs in question.&nbsp; Needless to say, the productivity benefits of this technique become extremely suspect after the first or second expedition.</p>
<p>For this reason, I strongly believe that the launch of an interactive shell should be the responsibility of the tool managing the dependencies, rather than that of the developer.&nbsp; Note that Maven already has some support for shells in conjunction with certain languages (Scala among them), but it is as crude and verbose as the tool itself.&nbsp; What I really want is to be able to invoke the following command and have the appropriate shell launched with a pre-configured CLASSPATH.&nbsp; I shouldn&#8217;t have to worry about the language of my project, the location of my repository or even if the shell requires extra configuration on my platform.&nbsp; The idea is that everything should all work auto-magically:</p>
<pre style="margin-left: 15px;">$ buildr shell</pre>
<p>This is exactly what the <a href="http://github.com/djspiewak/buildr/tree/interactive-shell"><code>interactive-shell</code> branch of my Buildr fork</a> is designed to accomplish.&nbsp; Whenever the <code>shell</code> task is invoked, Buildr looked through the current project and attempts to guess the language involved.&nbsp; This guesswork is required for a number of other features, so Buildr is actually pretty accurate in this area.&nbsp; If the language in question is Groovy or Scala, then the desired shell is obvious.&nbsp; Java does not have an integrated shell, which means that the default behavior on a Java project would be to raise an error.</p>
<p>However, the benefits of interactive shells are not limited to just the latest-and-greatest languages.&nbsp; I often use a Scala shell with Java projects, and for certain things a JRuby shell as well (<code>jirb</code>).&nbsp; Thus, my interactive shell extension also provides a mechanism to allow users to override the default shell on a per-project basis:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby">define <span style="color: #ff00cc;">'my-project'</span> <span style="color: #006699; font-weight: bold;">do</span>
  shell.<span class="me1">using</span> <span style="color: #02b902;">:clj</span>
<span style="color: #006699; font-weight: bold;">end</span></pre></td></tr></table></div>

<p>With this configuration, regardless of the language used by the compiler for &#8220;my-project&#8221;, Buildr will launch the Clojure REPL whenever the <code>shell</code> task is invoked.&nbsp; The currently supported shells and their corresponding Buildr identifiers:</p>
<ul>
<li>Clojure&#8217;s REPL &mdash; <code>:clj</code></li>
<li>Groovy&#8217;s Shell &mdash; <code>:groovysh</code></li>
<li>JRuby&#8217;s IRB &mdash; <code>:jirb</code></li>
<li>Scala&#8217;s Shell &mdash; <code>:scala</code></li>
</ul>
<p>It is also possible to explicitly launch a specific shell.&nbsp; This is useful for situations where you might want to use the Scala shell for testing some things and the JRuby IRB for quickly prototyping other ideas (I do this a lot).&nbsp; The command to launch the JIRB shell in the context of <code>my-project</code> would be as follows:</p>
<pre style="margin-left: 15px;">$ buildr my-project:shell:jirb</pre>
<p>As a special value-added feature, all of these shells (except for Groovy&#8217;s, which is weird) will be automatically configured to use JavaRebel for the project compilation target classes if it can be automatically detected.&nbsp; This detection is performed by examining <code>REBEL_HOME</code>, <code>JAVA_REBEL</code>, <code>JAVAREBEL</code> and <code>JAVAREBEL_HOME</code> environment variables in order.&nbsp; If any one of these points to a directory which contains <code>javarebel.jar</code> or points directly to <code>javarebel.jar</code> itself, the configuration is assumed and the respective shell invocation is appropriately modified.</p>
<div style="text-align:center;"><img src="http://www.codecommit.com/blog/wp-content/uploads/2009/01/javarebel-integration.png" alt="javarebel-integration.png" border="0"/></div>
<p>Best of all, this support is implemented using a highly-extensible framework similar to Buildr&#8217;s own <code>Compiler</code> API.&nbsp; It&#8217;s very easy for plugin implementors or even average developers to simply drop-in a new shell provider, perhaps for an internal language or even some unexpected application.&nbsp; The core functionality of shell detection is integrated into Buildr itself, but this in no way hampers extensibility.&nbsp; For example, I could easily create a third-party <code>.rake</code> plugin for Buildr which added support for a whole new language (e.g. Haskell).&nbsp; In this plugin, I could also define a new shell provider which would be the default for projects using that language (e.g. GHCi).</p>
<h3>Open Question</h3>
<p>The good news is that this feature <a href="http://www.nabble.com/Interactive-Shell-Support-td21273331.html">has been discussed extensively</a> on the <code>buildr-user</code> mailing-list and the prevailing opinion seems to be that it should be folded into the main Buildr distribution.&nbsp; Exactly what form this will take has yet to be decided.&nbsp; The bad news is that there is still some dispute about a fundamental aspect of this feature&#8217;s operation.</p>
<p>The question revolves around what the exact behavior should be when the <code>shell</code> task is invoked.&nbsp; Should Buildr detect the project (or sub-project) you are in and automatically configure the shell&#8217;s CLASSPATH accordingly?&nbsp; This would give the interactive shell access to different classes depending on the current working directory.&nbsp; Alternatively, should there be one all-powerful shell per-<code>buildfile</code> configured at the root level?&nbsp; This would allow your shell to remain consistent throughout the project, regardless of your current directory.&nbsp; However, it would also mean that some configuration would be required in order to enable the functionality.&nbsp; (more details of this debate can be found <a href="http://www.nabble.com/Interactive-Shell-Support-td21273331.html">on the mailing-list</a>).</p>
<p>Additionally, what should the exact syntax be for invoking a specific shell?&nbsp; Rake 0.8 allows tasks to take parameters enclosed within square brackets.&nbsp; Thus, the syntax would be something more like the following:</p>
<pre style="margin-left: 15px">$ buildr collection:shell[jirb]</pre>
<p>In some sense, this is more logical since it reflects the fact that a single task, <code>shell</code>, is taking care of the work of invoking stuff.&nbsp; On the other hand, it&#8217;s a little less consistent with the rest of Buildr&#8217;s tasks, particularly things like &#8220;<code>test:TestClass</code>&#8221; and so on.&nbsp; This too is a matter which has yet to be settled.</p>
<p>All in all, this is a pretty experimental branch which is very open (and desirous) of outside input.&nbsp; How would you use a feature like this?&nbsp; Is there anything missing from what I have presented?&nbsp; What design path should be we take with regards to project-local vs global shell configurations?</p>
<p>If you feel like adding your voice to the chorus, feel free to leave a comment or (better yet) post a reply on the mailing-list thread.&nbsp; You&#8217;re also perfectly free to fork my remote branch at GitHub to better experiment with things yourself.&nbsp; The root of the whole plate of spaghetti is the <code>lib/buildr/shell.rb</code> file.&nbsp; <em>Bon appetit!</em></p>
<img src="http://feeds.feedburner.com/~r/codecommit/~4/2E_dZTn8dF8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codecommit.com/blog/java/hacking-buildr-interactive-shell-support/feed</wfw:commentRss>
		<feedburner:origLink>http://www.codecommit.com/blog/java/hacking-buildr-interactive-shell-support</feedburner:origLink></item>
		<item>
		<title>Gun for Hire (off topic)</title>
		<link>http://feeds.codecommit.com/~r/codecommit/~3/UgOIOjaqhTk/gun-for-hire-off-topic</link>
		<comments>http://www.codecommit.com/blog/java/gun-for-hire-off-topic#comments</comments>
		<pubDate>Wed, 07 Jan 2009 08:24:36 +0000</pubDate>
		<dc:creator>Daniel Spiewak</dc:creator>
		
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.codecommit.com/blog/java/gun-for-hire-off-topic</guid>
		<description><![CDATA[Just in case you thought Christmas was over, I have a late gift for the world: I&#8217;m available for hire!&#160; Ok, so maybe this wasn&#8217;t exactly the stocking-stuffer you were expecting, but it&#8217;s the thought that counts.
I&#8217;m announcing my availability for employment as a part-time developer.&#160; Those of you who follow this blog are probably [...]]]></description>
			<content:encoded><![CDATA[<p>Just in case you thought Christmas was over, I have a late gift for the world: I&#8217;m available for hire!&nbsp; Ok, so maybe this wasn&#8217;t exactly the stocking-stuffer you were expecting, but it&#8217;s the thought that counts.</p>
<p>I&#8217;m announcing my availability for employment as a part-time developer.&nbsp; Those of you who follow this blog are probably already familiar with my areas of expertise, so I don&#8217;t think there is a need to bore you with a rehash.&nbsp; Resume available on request!</p>
<p>Anyway, my preference would be a project where I get to use multiple different languages, particularly Scala and Clojure, but I&#8217;m flexible.&nbsp; If you think my skills would make a positive addition to your team, <a href="mailto:djspiewak@gmail.com">shoot me an email</a>!</p>
<img src="http://feeds.feedburner.com/~r/codecommit/~4/UgOIOjaqhTk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codecommit.com/blog/java/gun-for-hire-off-topic/feed</wfw:commentRss>
		<feedburner:origLink>http://www.codecommit.com/blog/java/gun-for-hire-off-topic</feedburner:origLink></item>
		<item>
		<title>Joint Compilation of Scala and Java Sources</title>
		<link>http://feeds.codecommit.com/~r/codecommit/~3/R-fakZfpLQQ/joint-compilation-of-scala-and-java-sources</link>
		<comments>http://www.codecommit.com/blog/scala/joint-compilation-of-scala-and-java-sources#comments</comments>
		<pubDate>Mon, 05 Jan 2009 08:00:00 +0000</pubDate>
		<dc:creator>Daniel Spiewak</dc:creator>
		
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://www.codecommit.com/blog/scala/joint-compilation-of-scala-and-java-sources</guid>
		<description><![CDATA[One of the features that the Groovy people like to flaunt is the joint compilation of .groovy and .java files.&#160; This is a fantastically powerful concept which (among other things) allows for circular dependencies between Java, Groovy and back again.&#160; Thus, you can have a Groovy class which extends a Java class which in turn [...]]]></description>
			<content:encoded><![CDATA[<p>One of the features that the Groovy people like to flaunt is the joint compilation of <code>.groovy</code> and <code>.java</code> files.&nbsp; This is a fantastically powerful concept which (among other things) allows for circular dependencies between Java, Groovy and back again.&nbsp; Thus, you can have a Groovy class which extends a Java class which in turn extends another Groovy class.</p>
<p>All this is old news, but what you may not know is the fact that Scala is capable of the same thing.&nbsp; The Scala/Java joint compilation mode is new in Scala 2.7.2, but despite the fact that this release has been out for more than two months, there is still a remarkable lack of tutorials and documentation regarding its usage.&nbsp; Hence, this post&#8230;</p>
<h3>Concepts</h3>
<p>For starters, you need to know a little bit about how joint compilation works, both in Groovy and in Scala.&nbsp; Our motivating example will be the following stimulating snippet:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #cc0000;">// foo.scala</span>
<span style="color: #006699; font-weight: bold;">class</span> Foo
&nbsp;
<span style="color: #006699; font-weight: bold;">class</span> Baz <span style="color: #006699; font-weight: bold;">extends</span> Bar</pre></td></tr></table></div>

<p>&#8230;and the Java class:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="java5"><span style="color: #ff8400;">// Bar.java</span>
<span style="color: #006699; font-weight: bold;">public</span> <span style="color: #0099ff; font-weight: bold;">class</span> Bar <span style="color: #006699; font-weight: bold;">extends</span> Foo <span style="font-weight: bold;">&#123;</span><span style="font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>If we try to compile <code>foo.scala</code> before <code>Bar.java</code>, the Scala compiler will issue a type error complaining that class <code>Bar</code> does not exist.&nbsp; Similarly, if we attempt the to compile <code>Bar.java</code> first, the Java compiler will whine about the lack of a <code>Foo</code> class.&nbsp; Now, there is actually a way to resolve this <em>particular</em> case (by splitting <code>foo.scala</code> into two separate files), but it&#8217;s easy to imagine other examples where the circular dependency is impossible to linearize.&nbsp; For the sake of example, let&#8217;s just assume that this circular dependency is a problem and cannot be handled piece-meal.</p>
<p>In order for this to work, either the Scala compiler will need to know about class <code>Bar</code> before its compilation, or vice versa.&nbsp; This implies that one of the compilers will need to be able to analyze sources which target the other.&nbsp; Since Scala is the language in question, it only makes sense that it be the accommodating one (rather than <code>javac</code>).</p>
<p>What <code>scalac</code> has to do is literally parse and analyze all of the Scala sources it is given in addition to any Java sources which may also be supplied.&nbsp; It doesn&#8217;t need to be a full fledged Java compiler, but it does have to know enough about the Java language to be able to produce an annotated structural AST for any Java source file.&nbsp; Once this AST is available, circular dependencies may be handled in exactly the same way as circular dependencies internal to Scala sources (because all Scala <em>and</em> all Java classes are available simultaneously to the compiler).</p>
<p>Once the analysis phase of <code>scalac</code> has blessed the Scala AST, all of the Java nodes may be discarded.&nbsp; At this point, circular dependencies have been resolved and all type errors have been handled.&nbsp; Thus, there is no need to carry around useless class information.&nbsp; Once <code>scalac</code> is done, both the <code>Foo</code> and the <code>Baz</code> classes will have produced resultant <code>Foo.class</code> and <code>Baz.class</code> output files.</p>
<p>However, we&#8217;re still not quite done yet.&nbsp; Compilation has successfully completed, but if we try to run the application, we will receive a <code>NoClassDefFoundError</code> due to the fact that the <code>Bar</code> class has not actually been compiled.&nbsp; Remember, <code>scalac</code> only <em>analyzed</em> it for the sake of the type checker, no actual bytecode was produced.&nbsp; <code>Bar</code> may even suffer from a compile error of some sort, as long as this error is within the method definitions, <code>scalac</code> isn&#8217;t going to catch it.</p>
<p>The final step is to invoke <code>javac</code> against the <code>.java</code> source files (the same ones we passed to <code>scalac</code>) adding <code>scalac</code>&#8217;s output directory to <code>javac</code>&#8217;s classpath.&nbsp; Thus, <code>javac</code> will be able to find the <code>Foo</code> class that we just compiled so as to successfully (hopefully) compile the <code>Bar</code> class.&nbsp; If all goes well, the final result will be three separate files: <code>Foo.class</code>, <code>Bar.class</code> and <code>Baz.class</code>.</p>
<h3>Usage</h3>
<p>Although the concepts are identical, Scala&#8217;s joint compilation works slightly differently from Groovy&#8217;s from a usage standpoint.&nbsp; More specifically: <code>scalac</code> does <em>not</em> automatically invoke <code>javac</code> on the specified <code>.java</code> sources.&nbsp; This means that you can perform &#8220;joint compilation&#8221; using <code>scalac</code>, but without invoking <code>javac</code> you will only receive the compiled Scala classes, the Java classes will be ignored (except by the type checker).&nbsp; This design has some nice benefits, but it does mean that we usually need at least one extra command in our compilation process.</p>
<p>All of the following usage examples assume that you have defined the earlier example in the following hierarchy:</p>
<ul>
<li>src</li>
<ul>
<li>main</li>
<ul>
<li>java</li>
<ul>
<li>Bar.java</li>
</ul>
<li>scala</li>
<ul>
<li>foo.scala</li>
</ul>
</ul>
</ul>
<li>target</li>
<ul>
<li>classes</li>
</ul>
</ul>
<h3>Command Line</h3>
<pre style="margin-left: 15px;"># include both .scala AND .java files
scalac -d target/classes src/main/scala/*.scala src/main/java/*.java

javac -d target/classes 
      -classpath $SCALA_HOME/lib/scala-library.jar:target/classes 
       src/main/java/*.java</pre>
<h3>Ant</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="xml"><span class="sc3"><span style="color: #0000ff;">&lt;target</span> <span style="color: #0000ff;">name=</span><span style="color: #ff00cc;">&quot;build&quot;</span><span style="color: #0000ff;">&gt;</span></span>
    <span class="sc3"><span style="color: #0000ff;">&lt;scalac</span> <span style="color: #0000ff;">srcdir=</span><span style="color: #ff00cc;">&quot;src/main&quot;</span> <span style="color: #0000ff;">destdir=</span><span style="color: #ff00cc;">&quot;target/classes&quot;</span><span style="color: #0000ff;">&gt;</span></span>
        <span class="sc3"><span style="color: #0000ff;">&lt;include</span> <span style="color: #0000ff;">name=</span><span style="color: #ff00cc;">&quot;scala/**/*.scala&quot;</span><span style="color: #0000ff;">/&gt;</span></span>
        <span class="sc3"><span style="color: #0000ff;">&lt;include</span> <span style="color: #0000ff;">name=</span><span style="color: #ff00cc;">&quot;scala/**/*.java&quot;</span><span style="color: #0000ff;">/&gt;</span></span>
    <span class="sc3"><span style="color: #0000ff;">&lt;/scalac</span><span style="color: #0000ff;">&gt;</span></span>
&nbsp;
    <span class="sc3"><span style="color: #0000ff;">&lt;javac</span> <span style="color: #0000ff;">srcdir=</span><span style="color: #ff00cc;">&quot;src/main/java&quot;</span> <span style="color: #0000ff;">destdir=</span><span style="color: #ff00cc;">&quot;${scala.library}:target/classes&quot;</span> 
           <span style="color: #0000ff;">classpath=</span><span style="color: #ff00cc;">&quot;target/classes&quot;</span><span style="color: #0000ff;">/&gt;</span></span>
<span class="sc3"><span style="color: #0000ff;">&lt;/target</span><span style="color: #0000ff;">&gt;</span></span></pre></td></tr></table></div>

<h3>Maven</h3>
<p>One thing you gotta love about Maven: it&#8217;s fairly low on configuration for certain common tasks.&nbsp; Given the above directory structure and the most recent version of the <code>maven-scala-plugin</code>, the following command should be sufficient for joint compilation:</p>
<pre style="margin-left: 15px;">mvn compile</pre>
<p>Unfortunately, there <a href="http://www.nabble.com/forum/ViewPost.jtp?post=20845683&#038;framed=y">have been some problems</a> reported with the default configuration and complex inter-dependencies between Scala and Java (and back again).&nbsp; I&#8217;m not a Maven&#8230;maven, so I can&#8217;t help too much, but as I understand things, this POM fragment seems to work well:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="xml"><span class="sc3"><span style="color: #0000ff;">&lt;plugin</span><span style="color: #0000ff;">&gt;</span></span>
    <span class="sc3"><span style="color: #0000ff;">&lt;groupId</span><span style="color: #0000ff;">&gt;</span></span>org.scala-tools<span class="sc3"><span style="color: #0000ff;">&lt;/groupId</span><span style="color: #0000ff;">&gt;</span></span>
    <span class="sc3"><span style="color: #0000ff;">&lt;artifactId</span><span style="color: #0000ff;">&gt;</span></span>maven-scala-plugin<span class="sc3"><span style="color: #0000ff;">&lt;/artifactId</span><span style="color: #0000ff;">&gt;</span></span>
&nbsp;
    <span class="sc3"><span style="color: #0000ff;">&lt;executions</span><span style="color: #0000ff;">&gt;</span></span>
        <span class="sc3"><span style="color: #0000ff;">&lt;execution</span><span style="color: #0000ff;">&gt;</span></span>
            <span class="sc3"><span style="color: #0000ff;">&lt;id</span><span style="color: #0000ff;">&gt;</span></span>compile<span class="sc3"><span style="color: #0000ff;">&lt;/id</span><span style="color: #0000ff;">&gt;</span></span>
            <span class="sc3"><span style="color: #0000ff;">&lt;goals</span><span style="color: #0000ff;">&gt;</span></span>
            <span class="sc3"><span style="color: #0000ff;">&lt;goal</span><span style="color: #0000ff;">&gt;</span></span>compile<span class="sc3"><span style="color: #0000ff;">&lt;/goal</span><span style="color: #0000ff;">&gt;</span></span>
            <span class="sc3"><span style="color: #0000ff;">&lt;/goals</span><span style="color: #0000ff;">&gt;</span></span>
            <span class="sc3"><span style="color: #0000ff;">&lt;phase</span><span style="color: #0000ff;">&gt;</span></span>compile<span class="sc3"><span style="color: #0000ff;">&lt;/phase</span><span style="color: #0000ff;">&gt;</span></span>
        <span class="sc3"><span style="color: #0000ff;">&lt;/execution</span><span style="color: #0000ff;">&gt;</span></span>
&nbsp;
        <span class="sc3"><span style="color: #0000ff;">&lt;execution</span><span style="color: #0000ff;">&gt;</span></span>
            <span class="sc3"><span style="color: #0000ff;">&lt;id</span><span style="color: #0000ff;">&gt;</span></span>test-compile<span class="sc3"><span style="color: #0000ff;">&lt;/id</span><span style="color: #0000ff;">&gt;</span></span>
            <span class="sc3"><span style="color: #0000ff;">&lt;goals</span><span style="color: #0000ff;">&gt;</span></span>
            <span class="sc3"><span style="color: #0000ff;">&lt;goal</span><span style="color: #0000ff;">&gt;</span></span>testCompile<span class="sc3"><span style="color: #0000ff;">&lt;/goal</span><span style="color: #0000ff;">&gt;</span></span>
            <span class="sc3"><span style="color: #0000ff;">&lt;/goals</span><span style="color: #0000ff;">&gt;</span></span>
            <span class="sc3"><span style="color: #0000ff;">&lt;phase</span><span style="color: #0000ff;">&gt;</span></span>test-compile<span class="sc3"><span style="color: #0000ff;">&lt;/phase</span><span style="color: #0000ff;">&gt;</span></span>
        <span class="sc3"><span style="color: #0000ff;">&lt;/execution</span><span style="color: #0000ff;">&gt;</span></span>
&nbsp;
        <span class="sc3"><span style="color: #0000ff;">&lt;execution</span><span style="color: #0000ff;">&gt;</span></span>
            <span class="sc3"><span style="color: #0000ff;">&lt;phase</span><span style="color: #0000ff;">&gt;</span></span>process-resources<span class="sc3"><span style="color: #0000ff;">&lt;/phase</span><span style="color: #0000ff;">&gt;</span></span>
            <span class="sc3"><span style="color: #0000ff;">&lt;goals</span><span style="color: #0000ff;">&gt;</span></span>
            <span class="sc3"><span style="color: #0000ff;">&lt;goal</span><span style="color: #0000ff;">&gt;</span></span>compile<span class="sc3"><span style="color: #0000ff;">&lt;/goal</span><span style="color: #0000ff;">&gt;</span></span>
            <span class="sc3"><span style="color: #0000ff;">&lt;/goals</span><span style="color: #0000ff;">&gt;</span></span>
        <span class="sc3"><span style="color: #0000ff;">&lt;/execution</span><span style="color: #0000ff;">&gt;</span></span>
    <span class="sc3"><span style="color: #0000ff;">&lt;/executions</span><span style="color: #0000ff;">&gt;</span></span>
<span class="sc3"><span style="color: #0000ff;">&lt;/plugin</span><span style="color: #0000ff;">&gt;</span></span>
&nbsp;
<span class="sc3"><span style="color: #0000ff;">&lt;plugin</span><span style="color: #0000ff;">&gt;</span></span>
    <span class="sc3"><span style="color: #0000ff;">&lt;artifactId</span><span style="color: #0000ff;">&gt;</span></span>maven-compiler-plugin<span class="sc3"><span style="color: #0000ff;">&lt;/artifactId</span><span style="color: #0000ff;">&gt;</span></span>
    <span class="sc3"><span style="color: #0000ff;">&lt;configuration</span><span style="color: #0000ff;">&gt;</span></span>
        <span class="sc3"><span style="color: #0000ff;">&lt;source</span><span style="color: #0000ff;">&gt;</span></span>1.5<span class="sc3"><span style="color: #0000ff;">&lt;/source</span><span style="color: #0000ff;">&gt;</span></span>
        <span class="sc3"><span style="color: #0000ff;">&lt;target</span><span style="color: #0000ff;">&gt;</span></span>1.5<span class="sc3"><span style="color: #0000ff;">&lt;/target</span><span style="color: #0000ff;">&gt;</span></span>
    <span class="sc3"><span style="color: #0000ff;">&lt;/configuration</span><span style="color: #0000ff;">&gt;</span></span>
<span class="sc3"><span style="color: #0000ff;">&lt;/plugin</span><span style="color: #0000ff;">&gt;</span></span></pre></td></tr></table></div>

<p>You can find more information on <a href="http://www.nabble.com/forum/ViewPost.jtp?post=20806619&#038;framed=y">the mailing-list thread</a>.</p>
<h3>Buildr</h3>
<p>Joint compilation for mixed Scala / Java projects has been <a href="http://issues.apache.org/jira/browse/BUILDR-136">a long-standing request of mine</a> in Buildr&#8217;s JIRA.&nbsp; However, because it&#8217;s not a high priority issue, the developers were never able to address it themselves.&nbsp; Of course, that doesn&#8217;t stop the rest of us from pitching in!</p>
<p>I had a little free time yesterday afternoon, so I decided to blow it by hacking out a quick implementation of joint Scala compilation in Buildr, based on its pre-existing support for joint compilation in Groovy projects.&nbsp; All of my work is available in <a href="http://github.com/djspiewak/buildr">my Buildr fork on GitHub</a>.&nbsp; This also includes some other unfinished goodies, so if you want only the joint compilation, clone just the <code>scala-joint-compilation</code> branch.</p>
<p>Once you have Buildr&#8217;s full sources, <code>cd</code> into the directory and enter the following command:</p>
<pre style="margin-left: 15px">rake setup install</pre>
<p>You may need to <code>gem install</code> a few packages.&nbsp; Further, the exact steps required may be slightly different on different platforms.&nbsp; You can find more details <a href="http://incubator.apache.org/buildr/contributing.html#working_with_source_code">on Buildr&#8217;s project page</a>.</p>
<p>With this highly-unstable version of Buildr installed on your unsuspecting system, you should now be able to make the following addition to your <code>buildfile</code> (assuming the directory structure given earlier):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby"><span style="color: #009966; font-weight: bold;">require</span> <span style="color: #ff00cc;">'buildr/scala'</span>
&nbsp;
<span style="color: #cc0000;"># rest of the file...</span></pre></td></tr></table></div>

<p>Just like Buildr&#8217;s joint compilation for Groovy, you must explicitly <code>require</code> the language, otherwise important things will break.&nbsp; With this slight modification, you should be able to build your project as per normal:</p>
<pre style="margin-left: 15px">buildr</pre>
<p>This support is so bleeding-edge, I don&#8217;t even think that it&#8217;s safe to call it &#8220;pre-alpha&#8221;.&nbsp; If you run into any problems, feel free to <a href="mailto:djspiewak@gmail.com">shoot me an email</a> or <a href="http://issues.apache.org/jira/browse/BUILDR-136">comment on the issue</a>.</p>
<h3>Conclusion</h3>
<p>Joint compilation of Java and Scala sources is a profound addition to the Scala feature list, making it significantly easier to use Scala alongside Java in pre-existing or future projects.&nbsp; With this support, it is finally possible to use Scala as a truly drop-in replacement for Java without modifying the existing infrastructure beyond the CLASSPATH.&nbsp; Hopefully this article has served to bring slightly more exposure to this feature, as well as provide some much-needed documentation on its use.</p>
<img src="http://feeds.feedburner.com/~r/codecommit/~4/R-fakZfpLQQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codecommit.com/blog/scala/joint-compilation-of-scala-and-java-sources/feed</wfw:commentRss>
		<feedburner:origLink>http://www.codecommit.com/blog/scala/joint-compilation-of-scala-and-java-sources</feedburner:origLink></item>
		<item>
		<title>What is Hindley-Milner?  (and why is it cool?)</title>
		<link>http://feeds.codecommit.com/~r/codecommit/~3/c-mlgmvvdro/what-is-hindley-milner-and-why-is-it-cool</link>
		<comments>http://www.codecommit.com/blog/scala/what-is-hindley-milner-and-why-is-it-cool#comments</comments>
		<pubDate>Mon, 29 Dec 2008 08:00:40 +0000</pubDate>
		<dc:creator>Daniel Spiewak</dc:creator>
		
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://www.codecommit.com/blog/scala/what-is-hindley-milner-and-why-is-it-cool</guid>
		<description><![CDATA[Anyone who has taken even a cursory glance at the vast menagerie of programming languages should have at least heard the phrase &#8220;Hindley-Milner&#8221;.&#160; F#, one of the most promising languages ever to emerge from the forbidding depths of Microsoft Research, makes use of this mysterious algorithm, as do Haskell, OCaml and ML before it.&#160; There [...]]]></description>
			<content:encoded><![CDATA[<p>Anyone who has taken even a cursory glance at the vast menagerie of programming languages should have at least heard the phrase &#8220;Hindley-Milner&#8221;.&nbsp; F#, one of the most promising languages ever to emerge from the forbidding depths of Microsoft Research, makes use of this mysterious algorithm, as do Haskell, OCaml and ML before it.&nbsp; There is even some research being undertaken to find a way to apply the power of HM to optimize dynamic languages like Ruby, JavaScript and Clojure.</p>
<p>However, despite widespread application of the idea, I have yet to see a decent layman&#8217;s-explanation for what the heck everyone is talking about.&nbsp; How does the magic actually work?&nbsp; Can you always trust the algorithm to infer the right types?&nbsp; Further, why is Hindley-Milner is better than (say) Java?&nbsp; So, while those of you who actually know what HM is are busy recovering from your recent aneurysm, the rest of us are going to try to figure this out.</p>
<h3>Ground Zero</h3>
<p>Functionally speaking, Hindley-Milner (or &#8220;Damas-Milner&#8221;) is an algorithm for inferring value types based on use.&nbsp; It literally formalizes the intuition that a type can be deduced by the functionality it supports.&nbsp; Consider the following bit of <em>psuedo</em>-Scala (not a flying toy):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #006699; font-weight: bold;">def</span> <span style="color: #9966ff;">foo</span><span style="font-weight: bold;">&#40;</span>s: <span style="color: #0099ff; font-weight: bold;">String</span><span style="font-weight: bold;">&#41;</span> = s.<span class="me1">length</span>
&nbsp;
<span style="color: #cc0000;">// note: no explicit types</span>
<span style="color: #006699; font-weight: bold;">def</span> <span style="color: #9966ff;">bar</span><span style="font-weight: bold;">&#40;</span>x, y<span style="font-weight: bold;">&#41;</span> = <span style="color: #9966ff;">foo</span><span style="font-weight: bold;">&#40;</span>x<span style="font-weight: bold;">&#41;</span> + y</pre></td></tr></table></div>

<p>Just looking at the definition of <code>bar</code>, we can easily see that its type <em>must</em> be <code>(String, Int)=>Int</code>.&nbsp; As humans, this is an easy thing for us to intuit.&nbsp; We simply look at the body of the function and see the two uses of the <code>x</code> and <code>y</code> parameters.&nbsp; <code>x</code> is being passed to <code>foo</code>, which expects a <code>String</code>.&nbsp; Therefore, <code>x</code> must be of type <code>String</code> for this code to compile.&nbsp; Furthermore, <code>foo</code> will return a value of type <code>Int</code>.&nbsp; The <code>+</code> method on class <code>Int</code> expects an <code>Int</code> parameter; thus, <code>y</code> must be of type <code>Int</code>.&nbsp; Finally, we know that <code>+</code> returns a new value of type <code>Int</code>, so there we have the return type of <code>bar</code>.</p>
<p>This process is almost exactly what Hindley-Milner does: it looks through the body of a function and computes a <em>constraint set</em> based on how each value is used.&nbsp; This is what we were doing when we observed that <code>foo</code> expects a parameter of type <code>String</code>.&nbsp; Once it has the constraint set, the algorithm completes the type reconstruction by unifying the constraints.&nbsp; If the expression is well-typed, the constraints will yield an unambiguous type at the end of the line.&nbsp; If the expression is not well-typed, then one (or more) constraints will be contradictory or merely unsatisfiable given the available types.</p>
<h3>Informal Algorithm</h3>
<p>The easiest way to see how this process works is to walk it through ourselves.&nbsp; The first phase is to derive the constraint set.&nbsp; We start by assigning each value (<code>x</code> and <code>y</code>) a <em>fresh</em> type (meaning &#8220;non-existent&#8221;).&nbsp; If we were to annotate <code>bar</code> with these type variables, it would look something like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #006699; font-weight: bold;">def</span> <span style="color: #9966ff;">bar</span><span style="font-weight: bold;">&#40;</span>x: X, y: Y<span style="font-weight: bold;">&#41;</span> = <span style="color: #9966ff;">foo</span><span style="font-weight: bold;">&#40;</span>x<span style="font-weight: bold;">&#41;</span> + y</pre></td></tr></table></div>

<p>The type names are not significant, the important restriction is that they do not collide with any &#8220;real&#8221; type.&nbsp; Their purpose is to allow the algorithm to unambiguously reference the yet-unknown type of each value.&nbsp; Without this, the constraint set cannot be constructed.</p>
<p>Next, we drill down into the body of the function, looking specifically for operations which impose some sort of type constraint.&nbsp; This is a depth-first traversal of the AST, which means that we look at operations with higher-precedence first.&nbsp; Technically, it doesn&#8217;t matter what order we look; I just find it easier to think about the process in this way.&nbsp; The first operation we come across is the dispatch to the <code>foo</code> method.&nbsp; We know that <code>foo</code> is of type <code>String=>Int</code>, and this allows us to add the following constraint to our set:</p>
<p><center><em>X</em>&nbsp; <img alt="\mapsto" src="http://alt1.artofproblemsolving.com/Forum/latexrender/pictures/2/2/3/22341972d49b9a3a22a4ef9220996604cf49ad12.gif"/>&nbsp; <code>String</code></center></p>
<p>The next operation we see is <code>+</code>, involving the <code>y</code> value.&nbsp; Scala treats all operators as method dispatch, so this expression actually means &#8220;<code>foo(x).+(y)</code>.&nbsp; We already know that <code>foo(x)</code> is an expression of type <code>Int</code> (from the type of <code>foo</code>), so we know that <code>+</code> is defined as a method on class <code>Int</code> with type <code>Int=>Int</code> (I&#8217;m actually being a bit hand-wavy here with regards to what we do and do not know, but that&#8217;s an unfortunate consequence of Scala&#8217;s object-oriented nature).&nbsp; This allows us to add another constraint to our set, resulting in the following:</p>
<p><center><em>X</em>&nbsp; <img alt="\mapsto" src="http://alt1.artofproblemsolving.com/Forum/latexrender/pictures/2/2/3/22341972d49b9a3a22a4ef9220996604cf49ad12.gif"/>&nbsp; <code>String</code><br/><br />
<em>Y</em>&nbsp; <img alt="\mapsto" src="http://alt1.artofproblemsolving.com/Forum/latexrender/pictures/2/2/3/22341972d49b9a3a22a4ef9220996604cf49ad12.gif"/>&nbsp; <code>Int</code></center></p>
<p>The final phase of the type reconstruction is to <em>unify</em> all of these constraints to come up with real types to substitute for the <em>X</em> and <em>Y</em> type variables.&nbsp; Unification is literally the process of looking at each of the constraints and trying to find a single type which satisfies them all.&nbsp; Imagine I gave you the following facts:</p>
<ul>
<li>Daniel is tall</li>
<li>Chris is tall</li>
<li>Daniel is red</li>
<li>Chris is blue</li>
</ul>
<p>Now, consider the following constraints:</p>
<p><center><em>Person1</em> is tall<br/><br />
<em>Person1</em> is red</center></p>
<p>Hmm, who do you suppose <em>Person1</em> might be?&nbsp; This process of combining a constraint set with some given facts can be mathematically formalized in the guise of unification.&nbsp; In the case of type reconstruction, just substitute &#8220;types&#8221; for &#8220;facts&#8221; and you&#8217;re golden.</p>
<p>In our case, the unification of our set of type constraints is fairly trivial.&nbsp; We have exactly one constraint per value (<code>x</code> and <code>y</code>), and both of these constraints map to concrete types.&nbsp; All we have to do is substitute &#8220;<code>String</code>&#8221; for &#8220;<em>X</em>&#8221; and &#8220;<code>Int</code>&#8221; for &#8220;<em>Y</em>&#8221; and we&#8217;re done.</p>
<p>To really see the power of unification, we need to look at a slightly more complex example.&nbsp; Consider the following function:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #006699; font-weight: bold;">def</span> <span style="color: #9966ff;">baz</span><span style="font-weight: bold;">&#40;</span>a, b<span style="font-weight: bold;">&#41;</span> = <span style="color: #9966ff;">a</span><span style="font-weight: bold;">&#40;</span>b<span style="font-weight: bold;">&#41;</span> :: b</pre></td></tr></table></div>

<p>This snippet defines a function, <code>baz</code>, which takes a function and some other parameter, invoking this function passing the second parameter and then &#8220;cons-ing&#8221; the result onto the second parameter itself.&nbsp; We can easily derive a constraint set for this function.&nbsp; As before, we start by coming up with type variables for each value.&nbsp; Note that in this case, we not only annotate the parameters but also the return type.&nbsp; I sort of skipped over this part in the earlier example since it only sufficed to make things more verbose.&nbsp; Technically, this type is always inferred in this way.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #006699; font-weight: bold;">def</span> <span style="color: #9966ff;">baz</span><span style="font-weight: bold;">&#40;</span>a: X, b: Y<span style="font-weight: bold;">&#41;</span>: Z = <span style="color: #9966ff;">a</span><span style="font-weight: bold;">&#40;</span>b<span style="font-weight: bold;">&#41;</span> :: b</pre></td></tr></table></div>

<p>The first constraint we should derive is that <code>a</code> must be a function which takes a value of type <em>Y</em> and returns some fresh type <em>Y&#8217;</em> (pronounced like &#8220;<em>why prime</em>&#8220;).&nbsp; Further, we know that <code>::</code> is a function on class <code>List[A]</code> which takes a new element <code>A</code> and produces a new <code>List[A]</code>.&nbsp; Thus, we know that <em>Y</em> and <em>Z</em> must both be <code>List[Y']</code>.&nbsp; Formalized in a constraint set, the result is as follows:</p>
<p><center><br />
<em>X</em>&nbsp; <img alt="\mapsto" src="http://alt1.artofproblemsolving.com/Forum/latexrender/pictures/2/2/3/22341972d49b9a3a22a4ef9220996604cf49ad12.gif"/>&nbsp; (<em>Y</em><code>=></code><em>Y&#8217;</em> )<br/></p>
<p><em>Y</em>&nbsp; <img alt="\mapsto" src="http://alt1.artofproblemsolving.com/Forum/latexrender/pictures/2/2/3/22341972d49b9a3a22a4ef9220996604cf49ad12.gif"/>&nbsp; <code>List[</code><em>Y&#8217;</em> <code>]</code><br/></p>
<p><em>Z</em>&nbsp; <img alt="\mapsto" src="http://alt1.artofproblemsolving.com/Forum/latexrender/pictures/2/2/3/22341972d49b9a3a22a4ef9220996604cf49ad12.gif"/>&nbsp; <code>List[</code><em>Y&#8217;</em> <code>]</code><br/><br />
</center></p>
<p>Now the unification is not so trivial.&nbsp; Critically, the <em>X</em> variable depends upon <em>Y</em>, which means that our unification will require at least one step:</p>
<p><center><br />
<em>X</em>&nbsp; <img alt="\mapsto" src="http://alt1.artofproblemsolving.com/Forum/latexrender/pictures/2/2/3/22341972d49b9a3a22a4ef9220996604cf49ad12.gif"/>&nbsp; ( <code>List[</code><em>Y&#8217;</em> <code>]</code><code>=></code><em>Y&#8217;</em> )<br/></p>
<p><em>Y</em>&nbsp; <img alt="\mapsto" src="http://alt1.artofproblemsolving.com/Forum/latexrender/pictures/2/2/3/22341972d49b9a3a22a4ef9220996604cf49ad12.gif"/>&nbsp; <code>List[</code><em>Y&#8217;</em> <code>]</code><br/></p>
<p><em>Z</em>&nbsp; <img alt="\mapsto" src="http://alt1.artofproblemsolving.com/Forum/latexrender/pictures/2/2/3/22341972d49b9a3a22a4ef9220996604cf49ad12.gif"/>&nbsp; <code>List[</code><em>Y&#8217;</em> <code>]</code><br/><br />
</center></p>
<p>This is the same constraint set as before, except that we have substituted the known mapping for <em>Y</em> into the mapping for <em>X</em>.&nbsp; This substitution allows us to eliminate <em>X</em>, <em>Y</em> and <em>Z</em> from our inferred types, resulting in the following typing for the <code>baz</code> function:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #006699; font-weight: bold;">def</span> <span style="color: #9966ff;">baz</span><span style="font-weight: bold;">&#40;</span>a: List<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>Y'<span style="font-weight: bold;">&#93;</span></span>=&gt;Y', b: List<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>Y'<span style="font-weight: bold;">&#93;</span></span><span style="font-weight: bold;">&#41;</span>: List<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>Y'<span style="font-weight: bold;">&#93;</span></span> = <span style="color: #9966ff;">a</span><span style="font-weight: bold;">&#40;</span>b<span style="font-weight: bold;">&#41;</span> :: b</pre></td></tr></table></div>

<p>Of course, this still isn&#8217;t valid.&nbsp; Even assuming that <code>Y'</code> were valid Scala syntax, the type checker would complain that no such type can be found.&nbsp; This situation actually arises surprisingly often when working with Hindley-Milner type reconstruction.&nbsp; Somehow, at the end of all the constraint inference and unification, we have a type variable &#8220;left over&#8221; for which there are no known constraints.</p>
<p>The solution is to treat this unconstrained variable as a type parameter.&nbsp; After all, if the parameter has no constraints, then we can just as easily substitute any type, including a generic.&nbsp; Thus, the final revision of the <code>baz</code> function adds an unconstrained type parameter &#8220;<code>A</code>&#8221; and substitutes it for all instances of <em>Y&#8217;</em> in the inferred types:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #006699; font-weight: bold;">def</span> baz<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>A<span style="font-weight: bold;">&#93;</span></span><span style="font-weight: bold;">&#40;</span>a: List<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>A<span style="font-weight: bold;">&#93;</span></span>=&gt;A, b: List<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>A<span style="font-weight: bold;">&#93;</span></span><span style="font-weight: bold;">&#41;</span>: List<span style="color: #9900cc;"><span style="font-weight: bold;">&#91;</span>A<span style="font-weight: bold;">&#93;</span></span> = <span style="color: #9966ff;">a</span><span style="font-weight: bold;">&#40;</span>b<span style="font-weight: bold;">&#41;</span> :: b</pre></td></tr></table></div>

<h3>Conclusion</h3>
<p>&#8230;and that&#8217;s all there is to it!&nbsp; Hindley-Milner is really no more complicated than all of that.&nbsp; One can easily imagine how such an algorithm could be used to perform far more complicated reconstructions than the trivial examples that we have shown.</p>
<p>Hopefully this article has given you a little more insight into how Hindley-Milner type reconstruction works under the surface.&nbsp; This variety of type inference can be of immense benefit, reducing the amount of syntax required for type safety down to the barest minimum.&nbsp; Our &#8220;<code>bar</code>&#8221; example actually started with (coincidentally) Ruby syntax and showed that it still had all the information we needed to verify type-safety.&nbsp; Just a bit of information you might want to keep around for the next time someone suggests that all statically typed languages are overly-verbose.</p>
<img src="http://feeds.feedburner.com/~r/codecommit/~4/c-mlgmvvdro" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codecommit.com/blog/scala/what-is-hindley-milner-and-why-is-it-cool/feed</wfw:commentRss>
		<feedburner:origLink>http://www.codecommit.com/blog/scala/what-is-hindley-milner-and-why-is-it-cool</feedburner:origLink></item>
		<item>
		<title>The Joy of Concatenative Languages Part 3: Kindly Types</title>
		<link>http://feeds.codecommit.com/~r/codecommit/~3/Xz4vZ2rzyCs/the-joy-of-concatenative-languages-part-3</link>
		<comments>http://www.codecommit.com/blog/cat/the-joy-of-concatenative-languages-part-3#comments</comments>
		<pubDate>Mon, 22 Dec 2008 09:00:00 +0000</pubDate>
		<dc:creator>Daniel Spiewak</dc:creator>
		
		<category><![CDATA[Cat]]></category>

		<guid isPermaLink="false">http://www.codecommit.com/blog/cat/the-joy-of-concatenative-languages-part-3</guid>
		<description><![CDATA[In parts one and two of this series, we dipped our toes into the fascinating world that is stack-based languages.&#160; By this point, you should be fairly familiar with how to construct simple algorithms using Cat (the language we have been working with) as well as the core terminology of the paradigm.&#160; In fact, with [...]]]></description>
			<content:encoded><![CDATA[<p>In parts <a href="http://www.codecommit.com/blog/cat/the-joy-of-concatenative-languages-part-1">one</a> and <a href="http://www.codecommit.com/blog/cat/the-joy-of-concatenative-languages-part-2">two</a> of this series, we dipped our toes into the fascinating world that is stack-based languages.&nbsp; By this point, you should be fairly familiar with how to construct simple algorithms using Cat (the language we have been working with) as well as the core terminology of the paradigm.&nbsp; In fact, with just the information given so far, you could probably go on to be productive with a real-world concatenative language like Factor.&nbsp; However, the interest does not just stop there&#8230;</p>
<p>One of the interesting challenges in programming language design is the construction of a type system.&nbsp; So as to clear up any possible misconception <em>before</em> it arises, this is how Pierce defines such a thing:</p>
<blockquote><p>A type system is a tractable syntactic method for proving the absence of certain program behaviors by classifying phrases according to the kinds of values they compute.</p></blockquote>
<p>For Java, which has a comparatively weak type system, this usually means preventing you from accidentally using a <code>String</code> as if it were an <code>int</code>.&nbsp; In other words, Java&#8217;s type system generally proves the absence of things like <code>NoSuchMethodError</code> and similar.&nbsp; C#, which has a slightly more-powerful type system, can also prove the absence of most <code>NullPointerException</code>(s) when code is written in a correct and idiomatic fashion.&nbsp; Scala goes even further with pattern matching&#8230;need I go on?&nbsp; The point is that type systems do different things in different languages, so the definition needs to be flexible enough to reflect that.</p>
<p>In this article, we&#8217;re going to look at how we can define a type system for a functional (meaning that we have quotations) concatenative language.&nbsp; In <a href="http://www.codecommit.com/blog/cat/the-joy-of-concatenative-languages-part-1#comment-4376">a comment on the first part of this series</a>, it was suggested that the task of typing stack-based languages is a fairly trivial one.&nbsp; This is true, but only to a certain point.&nbsp; As we will see, there are dragons lurking in the conceptual shadows, waiting for us to disturb their sleep.</p>
<h3>Simple Expressions</h3>
<p>Let&#8217;s start out with typing something simple.&nbsp; Consider the following program:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="cat">&nbsp;</pre></td></tr></table></div>

<p>For those of you reading the RSS, what you see between the previous paragraph and this is exactly what I intended to write: nothing at all.&nbsp; In a concatenative language, the empty program is usually considered to be valid.&nbsp; After all, it takes a stack as input and returns the <em>exact</em> same stack.&nbsp; We could replicate the semantics of this program by writing &#8220;<code>dup pop</code>&#8220;, but why bother?</p>
<p>The empty program has the following type:</p>
<pre>(->)</pre>
<p>Or, more properly:</p>
<pre>('A -> 'A)</pre>
<p>To the left of the <code>-&lt;</code> we have what I like to call the &#8220;input constraints&#8221;: what types must be on the stack coming into the program (or phrase).&nbsp; To the right of the arrow are the &#8220;output constraints&#8221;: what types will be on the stack when we&#8217;re done.&nbsp; For reasons which will become clear later on, <code>'A</code> in this case represents the whole input stack (regardless of what it contains).&nbsp; Since we never change anything on the stack (the program is, after all, empty), the output stack has whatever type the input stack was given.&nbsp; Another way of writing this type would be as follows:
<pre>* -> *</pre>
<p>This literally symbolizes our intuition that the empty program has no input <em>or</em> output constraints.&nbsp; However, this is somewhat less correct notationally since it implies that the input and output stacks are unrelated.&nbsp; In fact, I would go so far as to say that this notation is <em>wrong</em>.&nbsp; The only reason it is produced here is to serve as a memory aid.&nbsp; For the remainder of the article, we will be using Cat&#8217;s notation for types.</p>
<p>Let&#8217;s look at something a little less trivial.&nbsp; Consider the following program one word at a time:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="cat"><span style="color: #ff0000;">1</span> <span style="color: #ff0000;">2</span> <span style="color: #0099ff; font-weight: bold;">+</span></pre></td></tr></table></div>

<p>Remember that an integer literal (or any literal for that matter) is just a function which pushes a specific constant onto the stack.&nbsp; Let&#8217;s assign types based on what we <em>expect</em> the input/output constraints of these functions to be.&nbsp; <strong>Note:</strong> I will be using the colon (<code>:</code>) notation to denote a type.&nbsp; This isn&#8217;t conventional coming from C-land, but it is the gold standard of formal type theory:</p>
<pre>1 : ('A -> 'A Int)
2 : ('A -> 'A Int)
+ : ('A Int Int -> 'A Int)</pre>
<p>This is all very intuitive.&nbsp; Integer literals work on any stack and just produce that stack with a new <code>Int</code> pushed onto the top.&nbsp; Both <code>1</code> and <code>2</code> have the same type, which is a good sign that we&#8217;re on the right track.</p>
<p>The <code>+</code> word is a little more interesting.&nbsp; Its runtime semantics are as follows: pop two integers off the stack, add them together and then push the result back on.&nbsp; This word will not be able to execute without <em>both</em> integer values on the top of the stack.&nbsp; Thus, it only makes sense that its input constraints be some stack with two values of type <code>Int</code> at the top.&nbsp; Likewise, when we&#8217;re done, those two integers will be gone and a new <code>Int</code> will be pushed onto the remainder of the stack which was given to us.&nbsp; Remember that <code>'A</code> represents <em>any</em> stack, even if it is completely empty.</p>
<p>Coming back to our program, we can see that it is well typed by simply string together the types we have generated.&nbsp; Starting from the top (using <code>*</code> to symbolize the empty stack):</p>
<div align="center">
<table border="1">
<tr>
<td><b>Word</b></td>
<td><b>Input Stack</b></td>
<td><b>Output Stack</b></td>
</tr>
<tr>
<td align="center"><code>1</code></td>
<td><code>*</code></td>
<td><code>* Int</code></td>
</tr>
<tr>
<td align="center"><code>2</code></td>
<td><code>* Int</code></td>
<td><code>* Int Int</code></td>
</tr>
<tr>
<td align="center"><code>+</code></td>
<td><code>* Int Int</code></td>
<td><code>* Int</code></td>
</tr>
</table>
</div>
<p>Do you see how the input stack of each word matches the output stack of the previous?&nbsp; In this case, this sort of one-to-one matching indicates that the program is well-typed, producing a final stack with a single <code>Int</code> on it.&nbsp; If we actually run this program, we would see that the evaluation matches the assigned types.</p>
<h3>First-Order Functions</h3>
<p>This is fine for a simple addition program, but what if we throw functions into the mix?&nbsp; Consider the same program we just analyzed wrapped up within a function:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="cat"><span style="color: #006699; font-weight: bold;">define</span> addSome <span style="font-weight: bold;">&#123;</span>
  <span style="color: #ff0000;">1</span> <span style="color: #ff0000;">2</span> <span style="color: #0099ff; font-weight: bold;">+</span>
<span style="font-weight: bold;">&#125;</span>
&nbsp;
addSome</pre></td></tr></table></div>

<p>Here we define a function which has as a body the program we have already analyzed.&nbsp; Down at the bottom of our new program, we actually call this function.&nbsp; Here is the question: what type does the <code>addSome</code> word have?</p>
<p>To answer this question, look back at the table above and consider the <strong>Input Stack</strong> for the first word in concert with the <strong>Output Stack</strong> for the last.&nbsp; Putting these two types together yields the following type for the aggregated whole:</p>
<pre>1 2 + : ('A -> 'A Int)</pre>
<p>These words (or &#8220;phrase&#8221;) takes any stack as input, and then through some manipulation produces a single <code>Int</code> on top of that stack as a result.&nbsp; The stack may grow and shrink within the function, but at the end of the day, only the <code>Int</code> remains.&nbsp; As we would expect, this matches the runtime semantics perfectly.</p>
<p>Given the fact that the phrase &#8220;<code>1 2 +</code>&#8221; has the type <code>('A -> 'A Int)</code>, it is reasonable to assign that same type to the function which contains it.&nbsp; Thus, we can type-check the <code>addSome</code> program in a simple, one-row table:</p>
<div align="center">
<table border="1">
<tr>
<td><b>Word</b></td>
<td><b>Input Stack</b></td>
<td><b>Output Stack</b></td>
</tr>
<tr>
<td align="center"><code>addSome</code></td>
<td><code>*</code></td>
<td><code>* Int</code></td>
</tr>
</table>
</div>
<p>At the start of execution, the input stack to any program is <code>*</code>, or the empty stack.&nbsp; However, this is fine with our type checker, since the program has <code>'A</code> &mdash; or any stack &mdash; for its input parameters.</p>
<p>This is all so nice and intuitive, so let&#8217;s consider the case where we have a function which actually takes some parameters.&nbsp; Specifically, let&#8217;s consider the following definition:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="cat"><span style="color: #006699; font-weight: bold;">define</span> addTwice <span style="font-weight: bold;">&#123;</span>
  <span style="color: #0099ff; font-weight: bold;">+</span> <span style="color: #0099ff; font-weight: bold;">+</span>
<span style="font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>At runtime, this function will take three values off the stack and then add them all together.&nbsp; It is the Cat equivalent of the following in Scala:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #006699; font-weight: bold;">def</span> <span style="color: #9966ff;">addTwice</span><span style="font-weight: bold;">&#40;</span>a: <span style="color: #009966; font-weight: bold;">Int</span>, b: <span style="color: #009966; font-weight: bold;">Int</span>, c: <span style="color: #009966; font-weight: bold;">Int</span><span style="font-weight: bold;">&#41;</span> = a + b + c</pre></td></tr></table></div>

<p>The question is: how do we assign this (the Cat function) a type?&nbsp; As we have done before, let&#8217;s look at the types of the individual words:</p>
<pre>+ : ('A Int Int -> 'A Int)
+ : ('A Int Int -> 'A Int)</pre>
<p>Not much help there.&nbsp; Let&#8217;s try making a table:</p>
<div align="center">
<table border="1">
<tr>
<td><b>Word</b></td>
<td><b>Input Stack</b></td>
<td><b>Output Stack</b></td>
</tr>
<tr>
<td align="center"><code>+</code></td>
<td><code>* Int Int</code></td>
<td><code>* Int</code></td>
</tr>
<tr>
<td align="center"><code>+</code></td>
<td><code>* Int Int</code></td>
<td><code>* Int</code></td>
</tr>
</table>
</div>
<p>It&#8217;s tempting to look at this and just assign <code>addTwice</code> the type of <code>('A Int Int -> 'A Int)</code>.&nbsp; However, this would be a mistake.&nbsp; Notice the problem with our table above: the <strong>Input Stack</strong> type of the second word does not match the <strong>Output Stack</strong> of the first.&nbsp; In other words, this program does not immediately type-check.</p>
<p>The problem is the second word is accessing more of the stack than the first.&nbsp; We&#8217;re effectively &#8220;deferring&#8221; a parameter access until later in the function, rather than grabbing everything right away and threading the processing through from start to finish.&nbsp; This is a perfectly reasonable pattern, but it plays havoc with our naive type system.</p>
<p>The solution is to merge the input constraints across both words.&nbsp; The first word (<code>+</code>) requires two <code>Int</code>(s) to be on the top of the stack.&nbsp; When it is done, those <code>Int</code>(s) are gone and a single <code>Int</code> has taken their place.&nbsp; The second word (again <code>+</code>) <em>also</em> requires two <code>Int</code>(s) on the stack.&nbsp; We only have one that we know of (the output <code>Int</code> from the first word), so we must unify the constraints and merge things back &#8220;up the chain&#8221; as it were.&nbsp; In other words, our first word (<code>+</code>) will require not just two <code>Int</code>(s) on the stack but <em>three</em>: two for itself and one for the second word (<code>+</code>).&nbsp; Our corrected table will look something like the following:</p>
<div align="center">
<table border="1">
<tr>
<td><b>Word</b></td>
<td><b>Input Stack</b></td>
<td><b>Output Stack</b></td>
</tr>
<tr>
<td align="center"><code>+</code></td>
<td><code>* Int Int Int</code></td>
<td><code>* Int Int</code></td>
</tr>
<tr>
<td align="center"><code>+</code></td>
<td><code>* Int Int</code></td>
<td><code>* Int</code></td>
</tr>
</table>
</div>
<p>With this new table, all of the <strong>Input</strong> and <strong>Output</strong> stacks match, which means that the type is valid and can prove runtime evaluation.&nbsp; Thus, based on this whole song and dance, we can assign the following type:</p>
<pre>addTwice : ('A Int Int Int -> 'A Int)</pre>
<p>As expected, this function takes not two, but <em>three</em> <code>Int</code>(s) on the stack and returns the remainder of that stack with a new <code>Int</code> on top.</p>
<h4>Polymorphic Words</h4>
<p>One mildly-annoying issue that we have just skated over is the problem of polymorphism.&nbsp; Consider the following two programs:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="cat"><span style="color: #ff0000;">42</span> <span style="color: #009966; font-weight: bold;">pop</span></pre></td></tr></table></div>

<p>And this&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="cat"><span style="color: #ff00cc;">&quot;fourty-two&quot;</span> <span style="color: #009966; font-weight: bold;">pop</span></pre></td></tr></table></div>

<p>The question is: what type do we assign to <code>pop</code>?&nbsp; We can easily make the following two assertions:</p>
<pre>42           : ('A -> 'A Int)
"fourty-two" : ('A -> 'A String)</pre>
<p>If we attempt to use this information to type-check the first program (assuming that it is sound), we will arrive at the following type for <code>pop</code>:</p>
<pre>pop : ('A Int -> 'A)</pre>
<p>That&#8217;s intuitive, right?&nbsp; All that we&#8217;re doing here is taking the first value off of the stack (an <code>Int</code>, in the case of the first program) and throwing it away, returning the remainder of the stack.&nbsp; However, if we use this type, we will run into some serious troubles type-checking the second program:</p>
<div align="center">
<table border="1">
<tr>
<td align="center"><b>Word</b></td>
<td><b>Input Stack</b></td>
<td><b>Output Stack</b></td>
</tr>
<tr>
<td align="center"><code>"fourty-two"</code></td>
<td><code>*</code></td>
<td><code>* String</code></td>
</tr>
<tr>
<td align="center"><code>pop</code></td>
<td><code>* Int</code></td>
<td><code>*</code></td>
</tr>
</table>
</div>
<p>Since <code>pop</code> has type <code>('A Int -> 'A)</code> (as we asserted above), it is inapplicable to a stack with <code>String</code> on top.&nbsp; Note that we can&#8217;t just push these constraints &#8220;up the chain&#8221;, since it is a case of direct type mismatch, rather than a stack of insufficient depth.&nbsp; In short: we&#8217;re stuck.</p>
<p>The only way to solve this problem is to introduce the concept of parametric types.&nbsp; Literally, we need to define a type which can be <em>instantiated</em> against a given stack, regardless of what type happens to match the parameters in question.&nbsp; Java calls this concept &#8220;generics&#8221;.&nbsp; Rather than giving <code>pop</code> the overly-restrictive type of <code>('A Int -> 'A)</code>, we will instead allow the value on top of the stack to be of <em>any</em> type (not just <code>Int</code>):</p>
<pre>pop : ('A 'a -> 'A)</pre>
<p>Note the fact that <code>'A</code> and <code>'a</code> are very separate type variables in this snippet.&nbsp; <code>'A</code> represents the &#8220;rest of the stack&#8221;, while <code>'a</code> represents a specific type which just happens to be on top of the input stack.&nbsp; Using this new, more flexible type, we can produce tables for both of our earlier programs:</p>
<div align="center">
<table border="1">
<tr>
<td align="center"><b>Word</b></td>
<td><b>Input Stack</b></td>
<td><b>Output Stack</b></td>
</tr>
<tr>
<td align="center"><code>42</code></td>
<td><code>*</code></td>
<td><code>* Int</code></td>
</tr>
<tr>
<td align="center"><code>pop</code></td>
<td><code>* Int</code></td>
<td><code>*</code></td>
</tr>
</table>
</div>
<p>&nbsp;</p>
<div align="center">
<table border="1">
<tr>
<td align="center"><b>Word</b></td>
<td><b>Input Stack</b></td>
<td><b>Output Stack</b></td>
</tr>
<tr>
<td align="center"><code>"fourty-two"</code></td>
<td><code>*</code></td>
<td><code>* String</code></td>
</tr>
<tr>
<td align="center"><code>pop</code></td>
<td><code>* String</code></td>
<td><code>*</code></td>
</tr>
</table>
</div>
<p>Everything matches and the world is once again very happy.&nbsp; Note that we can also apply this parametric type concept to the slightly more interesting example of <code>dup</code>:</p>
<pre>dup : ('A 'a -> 'A 'a 'a)</pre>
<p>In other words, <code>dup</code> says that whatever type is on top of the stack when it starts, that type will be on top of the stack <em>twice</em> when it is finished.&nbsp; Just like <code>pop</code>, this type can be instantiated against any stack with at least one type, regardless of whether that type is <code>Int</code>, <code>String</code>, or anything else for that matter.</p>
<h3>Higher-Order Functions</h3>
<p>We&#8217;ve seen how to type-check simple phrases, as well as first-order functions with deferred stack access and the occasional polymorphic word.&nbsp; However, there is one particularly troublesome aspect of concatenative type systems which we have completely ignored: functions which take quotations off the stack.&nbsp; In other words: what type do we assign to <code>apply</code>?&nbsp; Consider the following function:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="cat"><span style="color: #006699; font-weight: bold;">define</span> trouble <span style="font-weight: bold;">&#123;</span>
  <span style="color: #009966; font-weight: bold;">apply</span>
<span style="font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>At runtime, <code>trouble</code> will pop a quotation and then evaluate it against the remainder of the stack.&nbsp; Intuitively, we need to have some way of representing the type of a quotation, but that&#8217;s not even the most serious problem.&nbsp; Somehow, we need to constrain the quotation to itself accept <em>exactly</em> the stack which remains after it is popped.&nbsp; We also need to find some way of capturing its output type in order to compute the final output type of <code>trouble</code>.</p>
<p>More concretely, we can make a first attempt at assigning a type for <code>trouble</code>.&nbsp; The underscores (<code>_</code>) illustrate an area where our type system is incapable of helping us:</p>
<pre>trouble : (_ (_ -> _) -> _)</pre>
<p>It&#8217;s very tempting to just throw an <code>'A</code> in there and be done with it, but the truth is that for this type expression, there is no &#8220;unused stack&#8221;.&nbsp; We don&#8217;t really know how much (or how little) of the stack will be used by the quotation; it could pop five elements, twenty or none at all.&nbsp; It literally needs access to the remainder of the input stack <em>in its entirety</em>, otherwise the expression is useless.&nbsp; Enter stack polymorphism&#8230;</p>
<p>Just as we needed a way to represent any <em>single</em> type in order to type-check <code>pop</code> and <code>dup</code>, we now need a way to represent any <em>stack</em> type in order to type-check <code>apply</code>.&nbsp; Fortunately, the answer is already nestled within our pre-established notation.&nbsp; Consider the type of <code>+</code>:</p>
<pre>+ : ('A Int Int -> 'A Int)</pre>
<p>We have been taking this to mean &#8220;any stack with two <code>Int</code>(s) on top resulting in that same stack with only one <code>Int</code>&#8220;.&nbsp; This is true, but we&#8217;re being a little hand-wavy about the meaning of &#8220;any stack&#8221; and how it relates to <code>'A</code>.&nbsp; When we really get down to it, what&#8217;s happening here is <code>'A</code> is being <em>instantiated</em> against a particular input stack, whatever that stack happens to be.&nbsp; When we were type-checking <code>+ +</code>, the first word instantiated <code>'A</code> not to mean the empty stack (<code>*</code>), but rather a stack with at least one <code>Int</code> on it.&nbsp; This was required to successfully type the second <code>+</code>.</p>
<p>We can very easily extend this notational convenience to represent generalized stack parameters.&nbsp; Rather than being instantiated to specific types, stack parameters are instantiated to some stack in its entirety.&nbsp; Just as with type parameters, wherever we see that instantiated stack parameter within a type expression, it will be replaced with whatever stack type it was assigned.&nbsp; Thus, we can assign <code>trouble</code> the following type:</p>
<pre>trouble : ('A ('A -> 'B) -> 'B)</pre>
<p>In other words, <code>trouble</code> takes some stack <em>A</em> which has a quotation on top.&nbsp; This quotation accepts stack <em>A</em> itself and returns some new stack <em>B</em>.&nbsp; Note that we don&#8217;t really know anything about <em>B</em>.&nbsp; It could be related to <em>A</em>, but it might not be.&nbsp; The final result of the whole expression is this new stack <em>B</em>.</p>
<p>This concept is remarkably powerful.&nbsp; With it in combination with the other types we have already examined, we can type check the entirety of Cat and be assured of the absence of type-mismatch and stack-underflow errors.&nbsp; Considering the fact that Cat is almost exactly as powerful as Joy, that&#8217;s a pretty impressive feat.</p>
<p>From a theoretical standpoint, things get even more interesting when we consider the type of the following function:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="cat"><span style="color: #006699; font-weight: bold;">define</span> <span style="color: #0099ff; font-weight: bold;">y</span> <span style="font-weight: bold;">&#123;</span>
  <span style="font-weight: bold;">&#91;</span><span style="color: #009966; font-weight: bold;">dup</span> <span style="color: #009966; font-weight: bold;">papply</span><span style="font-weight: bold;">&#93;</span> <span style="color: #009966; font-weight: bold;">swap</span> <span style="color: #009966; font-weight: bold;">compose</span> <span style="color: #009966; font-weight: bold;">dup</span> <span style="color: #009966; font-weight: bold;">apply</span>
<span style="font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>This has the following type:</p>
<pre>y : ('A ('A ('A -> 'B) -> 'B) -> 'B)</pre>
<p>As you may have guessed by the name, this is the <a href="http://en.wikipedia.org/wiki/Y-combinator">Y-combinator</a><sup><a href="#prt3-1">1</a></sup>, one of the most well-known mechanisms for producing recursion in a nameless system.&nbsp; Note that this definition looks a little different from the pure-untyped lambda calculus (call-by-name semantics):</p>
<div align="center">λf . (λx . f (x x)) (λx . f (x x))</div>
<p>What I&#8217;m trying to point out here is the fact that Cat is able to leverage its type system to assign a type to the Y-combinator.&nbsp; This is something which is literally impossible in System F, a typed form of lambda-calculus.&nbsp; In fact, the only way to type-check this function in a lambda-calculus-derivative system would be to add recursive types.&nbsp; Cat is able to get by with a very much non-recursive type definition, something which I find fascinating in the extreme.</p>
<p><strong>Update:</strong> The above paragraph is somewhat misleading.&nbsp; It turns out that Cat actually <i>does</i> use a recursive type under the surface to derive the non-recursive type for <code>y</code>.&nbsp; Specifically:</p>
<pre>dup papply : ('A ('B ('B self -> 'C) -> 'C) -> 'A ('B -> 'C))</pre>
<p>On a further theoretical note, the device in Cat&#8217;s type system which allows this power is in fact the stack type variable (e.g. <code>'A</code>).&nbsp; These stack types are conceptually quite similar to the type parameters we used in typing <code>pop</code> (e.g. <code>'a</code>), but still in a very separate domain.&nbsp; In fact, stack types have a different <em>kind</em> than regular types.&nbsp; This is not to say that Cat employs higher-kinds such as Scala&#8217;s (e.g. <code>* => *</code>), but it does have two very different type kinds: stacks and values.</p>
<p>And yet, it is not kinds in and of themselves which allows for the typing of the Y-combinator.&nbsp; F<sub>ω</sub> is essentially System F with higher-kinds, and yet it is still incapable of handling this tiny little expression.&nbsp; Most interesting indeed&#8230;</p>
<h3>Conclusion</h3>
<p>As you can see, type systems and concatenative languages do fit together nicely, but it takes a lot more effort than one would initially expect.&nbsp; While typing simple expressions is easy enough, the waters are muddied as soon as higher-order functions and even deferred stack access enters the mix.&nbsp; This is an extremely fertile area for research, where a lot of interesting ideas are being developed.&nbsp; For example, <a href="http://lambda-the-ultimate.org/node/3050#comment-44535">John Nowak&#8217;s 5th</a> attempts to apply a type system to the stack-based paradigm, but in a very different way than Cat.</p>
<p>I hope you enjoyed this mini-series of articles on concatenative languages.&nbsp; While they are a bit of a backwater in the programming language menagerie, I think that studying them can be a very instructive experience.&nbsp; Furthermore, there remain some problems that are very nicely expressed in languages like Cat while being extremely unwieldy in more conventional languages like Scala.&nbsp; Despite the obscurity of concatenative languages, it never hurts to have an extra language on hand, ready for those times when it really is the best tool for the job.</p>
<p><sup><a id="prt3-1">1</a></sup> Technically, this is a little different from the Y-combinator used in conventional lambda-calculus (it executes the quotation rather than returning a fixed-point).&nbsp; However, conceptually it is the same idea.</p>
<img src="http://feeds.feedburner.com/~r/codecommit/~4/Xz4vZ2rzyCs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codecommit.com/blog/cat/the-joy-of-concatenative-languages-part-3/feed</wfw:commentRss>
		<feedburner:origLink>http://www.codecommit.com/blog/cat/the-joy-of-concatenative-languages-part-3</feedburner:origLink></item>
		<item>
		<title>The Joy of Concatenative Languages Part 2: Innately Functional</title>
		<link>http://feeds.codecommit.com/~r/codecommit/~3/jzuF-VtClS4/the-joy-of-concatenative-languages-part-2</link>
		<comments>http://www.codecommit.com/blog/cat/the-joy-of-concatenative-languages-part-2#comments</comments>
		<pubDate>Mon, 15 Dec 2008 09:00:53 +0000</pubDate>
		<dc:creator>Daniel Spiewak</dc:creator>
		
		<category><![CDATA[Cat]]></category>

		<guid isPermaLink="false">http://www.codecommit.com/blog/cat/the-joy-of-concatenative-languages-part-2</guid>
		<description><![CDATA[In part one of this series, I introduced the concept of a stack-based language and in particular the syntax and rough ideas behind Cat.&#160; However, to anyone coming into concatenative land for the first time, my examples likely seemed both odd and unconvincing.&#160; After all, why would you ever use point-free programming when everyone else [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://www.codecommit.com/blog/cat/the-joy-of-concatenative-languages-part-1">part one of this series</a>, I introduced the concept of a stack-based language and in particular the syntax and rough ideas behind <a href="http://www.cat-language.com">Cat</a>.&nbsp; However, to anyone coming into concatenative land for the first time, my examples likely seemed both odd and unconvincing.&nbsp; After all, why would you ever use point-free programming when everyone else seems to be sold on the idea of name binding?&nbsp; More importantly, where do these languages fit in with our established menagerie of language paradigms?</p>
<p>The answer to the first question really depends on the situation.&nbsp; I personally think that the best motivation for concatenative languages is their syntax.&nbsp; If you want to create an internal DSL, there will be no language better suited to it than one which is concatenative, Cat, Factor or otherwise.&nbsp; This is because stack-oriented languages can get away with almost no syntax whatsoever.&nbsp; They say that Lisp is a syntax-free language, but this holds even more strongly for languages like Cat.&nbsp; Well, that and you don&#8217;t have to deal with all the parentheses&#8230;</p>
<p>The second question is (I think) the more interesting one: how do we classify these languages and what sort of methodologies should we apply?&nbsp; At first glance, Cat (and other languages like it) seem to be quite imperative in nature.&nbsp; After all, you have a single mutable stack that any function can modify.&nbsp; However, if you turn your head sideways and blink twice, you begin to realize that concatenative languages are really much closer to the functional side of the oyster.</p>
<p>Consider the following Cat program:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="cat"><span style="color: #006699; font-weight: bold;">define</span> plus <span style="font-weight: bold;">&#123;</span> <span style="color: #0099ff; font-weight: bold;">+</span> <span style="font-weight: bold;">&#125;</span>
<span style="color: #006699; font-weight: bold;">define</span> minus <span style="font-weight: bold;">&#123;</span> <span style="color: #0099ff; font-weight: bold;">-</span> <span style="font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #ff0000;">7</span> <span style="color: #ff0000;">2</span> <span style="color: #ff0000;">3</span>
plus minus</pre></td></tr></table></div>

<p>Trivial, but to the point.&nbsp; This program first adds the numbers <code>2</code> and <code>3</code>, then subtracts the result from <code>7</code>.&nbsp; Thus, the final result is a value of <code>2</code> on the stack.&nbsp; The only twist is that we have defined functions <code>plus</code> and <code>minus</code> to do the dirty work for us.&nbsp; This wasn&#8217;t strictly necessary, but I wanted to emphasize that <code>+</code> and <code>-</code> really are functions.&nbsp; We could express the exact same program in Scala:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #006699; font-weight: bold;">def</span> <span style="color: #9966ff;">plus</span><span style="font-weight: bold;">&#40;</span>a: <span style="color: #009966; font-weight: bold;">Int</span>, b: <span style="color: #009966; font-weight: bold;">Int</span><span style="font-weight: bold;">&#41;</span> = a + b
<span style="color: #006699; font-weight: bold;">def</span> <span style="color: #9966ff;">minus</span><span style="font-weight: bold;">&#40;</span>a: <span style="color: #009966; font-weight: bold;">Int</span>, b: <span style="color: #009966; font-weight: bold;">Int</span><span style="font-weight: bold;">&#41;</span> = a - b
&nbsp;
<span style="color: #9966ff;">minus</span><span style="font-weight: bold;">&#40;</span><span style="color: #ff0000;">7</span>, <span style="color: #9966ff;">plus</span><span style="font-weight: bold;">&#40;</span><span style="color: #ff0000;">2</span>, <span style="color: #ff0000;">3</span><span style="font-weight: bold;">&#41;</span><span style="font-weight: bold;">&#41;</span></pre></td></tr></table></div>

<p>Do you see how the consecutive invocations of <code>plus</code> and <code>minus</code> in Cat became composed invocations in Scala?&nbsp; This is where the term &#8220;concatenative language&#8221; derives from: the whole program is just a series of function compositions.&nbsp; <a href="http://en.wikipedia.org/wiki/Cat_(programming_language)#Semantics">Wikipedia&#8217;s article on Cat</a> has a very nice, mathematical description:</p>
<blockquote><p>Two adjacent terms in Cat imply the composition of functions that generate stacks, so the Cat program <code>f g</code> is equivalent to the mathematical expressions <img src="http://upload.wikimedia.org/math/a/5/a/a5a0406f40aa56d878c3c55c0f019d58.png"/> and <img src="http://upload.wikimedia.org/math/0/2/7/02795b8bf2da65c7ce93f65cdd14fe6d.png"/>, where <em>x</em> is the stack input to the expression.</p></blockquote>
<p>Strictly speaking, a concatenative language could be implemented without a stack, but such an implementation would likely be a bit harder to use than the average stack-based language.</p>
<p>Coming back to my original premise: concatenative languages are functional in nature.&nbsp; Absolutely <em>everything</em> in Cat is a function.&nbsp; Operators, words, even numeric literals like &#8220;<code>3</code>&#8221; are actually functions at the conceptual level.&nbsp; Additionally, Cat, Joy and Factor all offer a mechanism for treating functions as first-class values:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="cat"><span style="color: #ff0000;">2</span> <span style="color: #ff0000;">3</span>
<span style="font-weight: bold;">&#91;</span> <span style="color: #0099ff; font-weight: bold;">+</span> <span style="font-weight: bold;">&#93;</span>
<span style="color: #009966; font-weight: bold;">apply</span></pre></td></tr></table></div>

<p>The square-bracket (<code>[]</code>) syntax is representative of a quotation.&nbsp; Literally this mean &#8220;create a function of the enclosed words and place it as a value on the stack&#8221;.&nbsp; We can pop this function off the stack and invoke it by using the <code>apply</code> word.&nbsp; Incidentally, you may have noticed that this syntax is remarkably close to that which is used in <code>if</code> conditionals:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="cat"><span style="color: #ff0000;">5</span> <span style="color: #ff0000;">0</span> <span style="color: #0099ff; font-weight: bold;">&lt;</span>
<span style="font-weight: bold;">&#91;</span> <span style="color: #ff00cc;">&quot;strange math&quot;</span> <span style="font-weight: bold;">&#93;</span>
<span style="font-weight: bold;">&#91;</span> <span style="color: #ff00cc;">&quot;all is well&quot;</span> <span style="font-weight: bold;">&#93;</span>
<span style="color: #009966; font-weight: bold;">if</span></pre></td></tr></table></div>

<p>This syntax works because <code>if</code> isn&#8217;t <em>conceptually</em> a language primitive: it&#8217;s just another function which happens to take a boolean and two quotations off the stack.&nbsp; For the sake of efficiency, Cat does indeed implement <code>if</code> as a primitive, but this was a deliberate optimization rather than an implementation forced by the language design.&nbsp; Untyped Cat (see Part 3) is equivalent in power to the <a href="http://en.wikipedia.org/wiki/Lambda_calculus">pure-untyped lambda calculus</a>, and as our friend Alonzo Church showed us, <code>if</code>-style conditionals are easily accomplished:</p>
<div style="margin-left: 20pt">TRUE = λa . λb . a<br />
FALSE = λa . λb . b<br/></p>
<p>IF = λp . λt . λe . p t e</p></div>
<p>Yeah, maybe we&#8217;re drifting a bit off-point here&#8230;</p>
<h3>Higher-Order Programming</h3>
<p>So if Cat is just another functional programming language, then we should be able to implement all of those higher-order design patterns that we&#8217;ve come to know and love in languages like Scala and ML.&nbsp; To see how, let&#8217;s look at implementing some simple list manipulation functions in Cat.&nbsp; The easiest would be to start with <code>append</code>, which pops two lists off of the stack and pushes a new list which is the end-to-end concatenation of the two originals:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="cat"><span style="color: #006699; font-weight: bold;">define</span> append <span style="font-weight: bold;">&#123;</span>
  <span style="color: #009966; font-weight: bold;">empty</span>
  <span style="font-weight: bold;">&#91;</span> <span style="color: #009966; font-weight: bold;">pop</span> <span style="font-weight: bold;">&#93;</span>
  <span style="font-weight: bold;">&#91;</span>
    <span style="color: #009966; font-weight: bold;">uncons</span>
    <span style="font-weight: bold;">&#91;</span>append<span style="font-weight: bold;">&#93;</span> <span style="color: #009966; font-weight: bold;">dip</span>
    <span style="color: #009966; font-weight: bold;">cons</span>
  <span style="font-weight: bold;">&#93;</span>
  <span style="color: #009966; font-weight: bold;">if</span>
<span style="font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>This function first starts by checking to see if the top list is empty.&nbsp; If so, then just pop it off the stack and leave the other right where it is.&nbsp; Appending an empty list should always yield the original list.&nbsp; However, if the head list is <em>not</em> empty, then we need to work a bit.&nbsp; First, we decompose it into its tail and head, which are pushed onto the stack in order by the <code>uncons</code> function.&nbsp; Next, we need to recursively append the tail with our second list on the stack.&nbsp; However, the head of the list from <code>uncons</code> is in the way on top of the stack.&nbsp; We could use stack manipulation to move things around and get our lists up to the head of the stack, but <code>dip</code> provides us with a handy, higher-order shortcut.&nbsp; We temporarily remove the top of the stack, invoke the quotation &#8220;<code>[append]</code>&#8221; against the remainder and then push the old top back on top of the result.</p>
<p>The <code>dip</code> operation is surprisingly powerful, making it possible to completely live without either variables or multiple stacks.&nbsp; Any non-trivial Cat program will need to make use of this handy function at some level.</p>
<p>Once we have the old head and the new appended-list on the stack, all we need to do is put them back together using <code>cons</code>.&nbsp; This function leaves a new list on the stack in place of the old list and head element.&nbsp; This Cat program is almost precisely analogous to the following ML:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ml"><span style="color: #006699; font-weight: bold;">fun</span> append ls <span style="color: #cc00cc;">nil</span> = ls
  | append ls <span style="font-weight: bold;">&#40;</span>hd :: tail<span style="font-weight: bold;">&#41;</span> = hd :: <span style="font-weight: bold;">&#40;</span>append ls tail<span style="font-weight: bold;">&#41;</span></pre></td></tr></table></div>

<p>Personally, I find the ML a lot easier to read, but that&#8217;s just me.&nbsp; Obviously it&#8217;s a lot shorter, but as it turns out, our Cat implementation, while intuitive, was sub-optimal.&nbsp; Cat already implements <code>append</code> in the guise of the <code>cat</code> function, and it is far more concise than what I showed:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="cat"><span style="color: #006699; font-weight: bold;">define</span> <span style="color: #0099ff; font-weight: bold;">cat</span> <span style="font-weight: bold;">&#123;</span>
  <span style="color: #009966; font-weight: bold;">swap</span> <span style="font-weight: bold;">&#91;</span><span style="color: #009966; font-weight: bold;">cons</span><span style="font-weight: bold;">&#93;</span> <span style="color: #0099ff; font-weight: bold;">rfold</span>
<span style="font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>It&#8217;s almost frightening how short this is: only three words.&nbsp; It&#8217;s not as if <code>rfold</code> is doing anything mysterious either; it&#8217;s just a simple right-fold function that takes a list, an initial value and a quotation, producing a result by traversing the entire list.&nbsp; We can use something similar back in ML-land, achieving an implementation which is arguably equivalent in subjective elegance:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ml"><span style="color: #006699; font-weight: bold;">val</span> append = foldr <span style="font-weight: bold;">&#40;</span><span style="color: #006699; font-weight: bold;">op</span>::<span style="font-weight: bold;">&#41;</span></pre></td></tr></table></div>

<p>Moving on, we can also implement a <code>length</code> function in Cat, this time using <code>fold</code> to tighten things up:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="cat"><span style="color: #006699; font-weight: bold;">define</span> length <span style="font-weight: bold;">&#123;</span>
  <span style="color: #ff0000;">0</span> <span style="font-weight: bold;">&#91;</span> <span style="color: #009966; font-weight: bold;">pop</span> <span style="color: #ff0000;">1</span> <span style="color: #0099ff; font-weight: bold;">+</span> <span style="font-weight: bold;">&#93;</span> <span style="color: #0099ff; font-weight: bold;">fold</span>
<span style="font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>You&#8217;ll notice that we have to mess around a bit in the quotation in order to avoid the first &#8220;parameter&#8221;, the current element of the list (which we do not need).&nbsp; Expressing this in ML yields a very similar degree of cruft:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ml"><span style="color: #006699; font-weight: bold;">val</span> length = foldl <span style="font-weight: bold;">&#40;</span><span style="color: #006699; font-weight: bold;">fn</span> <span style="font-weight: bold;">&#40;</span>n, _<span style="font-weight: bold;">&#41;</span> =&gt; n + <span style="color: #ff0000;">1</span><span style="font-weight: bold;">&#41;</span> <span style="color: #ff0000;">0</span></pre></td></tr></table></div>

<h3>Conclusion</h3>
<p>The important take-away from this tangled morass of an article is the fact that Cat is a highly functional language, capable of easily keeping up with some of the stalwart champions of the paradigm.&nbsp; More significantly, this is a trait which is shared by <em>all</em> concatenative languages.&nbsp; Rather than throwing away all of the old wisdom learned in language design, stack-based languages build on it by providing an alternative view into the world of functions.</p>
<p>In the <a href="http://www.codecommit.com/blog/cat/the-joy-of-concatenative-languages-part-3">next (and final) article of the series</a>, we will take a brief look at the challenges of applying a type system to a concatenative language and the fascinating techniques used by Cat to achieve just that.</p>
<img src="http://feeds.feedburner.com/~r/codecommit/~4/jzuF-VtClS4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codecommit.com/blog/cat/the-joy-of-concatenative-languages-part-2/feed</wfw:commentRss>
		<feedburner:origLink>http://www.codecommit.com/blog/cat/the-joy-of-concatenative-languages-part-2</feedburner:origLink></item>
		<item>
		<title>The Joy of Concatenative Languages Part 1</title>
		<link>http://feeds.codecommit.com/~r/codecommit/~3/X7yyRB9CmbQ/the-joy-of-concatenative-languages-part-1</link>
		<comments>http://www.codecommit.com/blog/cat/the-joy-of-concatenative-languages-part-1#comments</comments>
		<pubDate>Mon, 08 Dec 2008 08:00:00 +0000</pubDate>
		<dc:creator>Daniel Spiewak</dc:creator>
		
		<category><![CDATA[Cat]]></category>

		<guid isPermaLink="false">http://www.codecommit.com/blog/cat/the-joy-of-concatenative-languages-part-1</guid>
		<description><![CDATA[Concatenative languages like Forth have been around for a long time.&#160; Hewlett-Packard famously employed a stack-based language called &#8220;RPL&#8221; on their HP-28 and HP-48 calculators, bringing the concept of Reverse Polish Notation to the mainstream&#8230;or as close to the mainstream as a really geeky toy can get.&#160; Surprisingly though, these languages have not seen serious [...]]]></description>
			<content:encoded><![CDATA[<p>Concatenative languages like Forth have been around for a long time.&nbsp; Hewlett-Packard famously employed a stack-based language called &#8220;RPL&#8221; on their HP-28 and HP-48 calculators, bringing the concept of Reverse Polish Notation to the mainstream&#8230;or as close to the mainstream as a really geeky toy can get.&nbsp; Surprisingly though, these languages have not seen serious adoption beyond the experimental and embedded device realms.&nbsp; And by &#8220;adoption&#8221;, I mean real programmers writing real code, not this whole <a href="http://en.wikipedia.org/wiki/JVM">interpreted bytecode nonsense</a>.</p>
<p>This is a shame, because stack-based languages have a remarkable number of things to teach us.&nbsp; Their superficial distinction from conventional programming languages very quickly gives way to a deep connection, particularly with functional languages.&nbsp; However, if we dig even deeper, we find that this similarity has its limits.&nbsp; There are some truly profound nuggets of truth waiting to be uncovered within these murky depths.&nbsp; Shall we?</p>
<p><em><strong>Trivial aside:</strong> I&#8217;m going to use the terms &#8220;concatenative&#8221; and &#8220;stack-based&#8221; interchangeably through the article.&nbsp; While these are most definitely related concepts, they are not exactly synonyms.&nbsp; Bear that in mind if you read anything more in-depth on the subject.</em></p>
<h3>The Basics</h3>
<p>Before we look at some of those &#8220;deeper truths of which I speak, it might be helpful to at least understand the fundamentals of stack-based programming.&nbsp; From Wikipedia:</p>
<blockquote><p>The concatenative or stack-based programming languages are ones in which the concatenation of two pieces of code expresses the composition of the functions they express. These languages use a stack to store the arguments and return values of operations.</p></blockquote>
<p>Er, right.&nbsp; I didn&#8217;t find that very helpful either.&nbsp; Let&#8217;s try again&#8230;</p>
<p>Stack-based programming languages all share a common element: an operand stack.&nbsp; Consider the following program:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="cat"><span style="color: #ff0000;">2</span></pre></td></tr></table></div>

<p>Yes, this is a real program.&nbsp; You can copy this code and run/compile it unmodified using most stack-based languages.&nbsp; However, for reasons which will become clear later in this series, I will be using <a href="http://www.cat-language.com">Cat</a> for most of my examples.&nbsp; Joy and Factor would both work well for the first two parts, but for part three we&#8217;re going to need some rather unique features.</p>
<p>Returning to our example: all this will do is take the numeric value of <code>2</code> and push it onto the operand stack.&nbsp; Since there are no further <em>words</em>, the program will exit.&nbsp; If we want, we can try something a little more interesting:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="cat"><span style="color: #ff0000;">2</span> <span style="color: #ff0000;">3</span> <span style="color: #0099ff; font-weight: bold;">+</span></pre></td></tr></table></div>

<p>This program first pushes 2 onto the stack, then 3, and finally it pops the top two values off of the stack, adds them together and pushes the result.&nbsp; Thus, when this program exits, the stack will only contain <code>5</code>.</p>
<p>We can mix and match these operations until we&#8217;re blue in the face, but it&#8217;s still not a terribly interesting language.&nbsp; What we really need is some sort of flow control.&nbsp; To do that, we need to understand quotations.&nbsp; Consider the following Scala program:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #006699; font-weight: bold;">val</span> plus = <span style="font-weight: bold;">&#123;</span> <span style="font-weight: bold;">&#40;</span>x: <span style="color: #009966; font-weight: bold;">Int</span>, y: <span style="color: #009966; font-weight: bold;">Int</span><span style="font-weight: bold;">&#41;</span> =&gt; x + y <span style="font-weight: bold;">&#125;</span>
<span style="color: #9966ff;">plus</span><span style="font-weight: bold;">&#40;</span><span style="color: #ff0000;">2</span>, <span style="color: #ff0000;">3</span><span style="font-weight: bold;">&#41;</span></pre></td></tr></table></div>

<p>Notice how rather than directly adding <code>2</code> and <code>3</code>, we first create a closure/lambda which encapsulates the operation.&nbsp; We then invoke this closure, passing <code>2</code> and <code>3</code> as arguments.&nbsp; We can emulate these exact semantics in Cat:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="cat"><span style="color: #ff0000;">2</span> <span style="color: #ff0000;">3</span>
<span style="font-weight: bold;">&#91;</span> <span style="color: #0099ff; font-weight: bold;">+</span> <span style="font-weight: bold;">&#93;</span>
<span style="color: #009966; font-weight: bold;">apply</span></pre></td></tr></table></div>

<p>The first line pushes <code>2</code> and <code>3</code> onto the stack.&nbsp; The second line uses square brackets to define a quotation, which is Cat&#8217;s version of a lambda.&nbsp; Note that it isn&#8217;t really a closure since there are no variables to en<em>close</em>.&nbsp; Joy and Factor also share this construct.&nbsp; Within the quotation we have a single word: <code>+</code>.&nbsp; The important thing is the quotation itself is what is put on the stack; the <code>+</code> word is not immediately executed.&nbsp; This is exactly how we declared <code>plus</code> in Scala.</p>
<p>The final line invokes the <code>apply</code> word.&nbsp; When this executes, it pops one value off the stack (which must be a quotation).&nbsp; It then executes this quotation, giving it access to the current stack.&nbsp; Since the quotation on the head of the stack consists of a single word, <code>+</code>, executing it will result in the next two elements being popped off (<code>2</code> and <code>3</code>) and the result (<code>5</code>) being pushed on.&nbsp; Exactly the same result as the earlier example and the exact same semantics as the Scala example, but a lot more concise.</p>
<p>Cat also provides a number of primitive operations which perform their dirty work directly on the stack.&nbsp; These operations are what make it possible to reasonably perform tasks without variables.&nbsp; The most important operations are as follows:</p>
<ul>
<li><code>swap</code> &mdash; exchanges the top two elements on the stack.&nbsp; Thus, <code>2 3 swap</code> results in a stack of &#8220;<code>3 2</code>&#8221; in that order.</li>
<li><code>pop</code> &mdash; drops the first element of the stack.</li>
<li><code>dup</code> &mdash; duplicates the first element and pushes the result onto the stack.&nbsp; Thus, <code>2 dup</code> results in a stack of &#8220;<code>2 2</code>&#8220;.</li>
<li><code>dip</code> &mdash; pops a quotation off the stack, temporarily removes the next item, executes the quotation against the remaining stack and then pushes the old head back on.&nbsp; Thus, <code>2 3 1 [ + ] dip</code> results in a stack of &#8220;<code>5 1</code>&#8220;.</li>
</ul>
<p>There are other primitives, but these are the big four.&nbsp; It is possible to emulate any control structure (such as <code>if</code>/<code>then</code>) just using the language shown so far.&nbsp; However, to do so would be pretty ugly and not very useful.&nbsp; Cat does provide some other operations to make life a little more interesting.&nbsp; Most significantly: functions and conditionals.&nbsp; A function is defined in the following way:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="cat"><span style="color: #006699; font-weight: bold;">define</span> plus <span style="font-weight: bold;">&#123;</span> <span style="color: #0099ff; font-weight: bold;">+</span> <span style="font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>Those coming from a programming background involving variables (that would be just about all of us) would probably look at this function and feel as if something is missing.&nbsp; The odd part of this is there is no need to declare parameters, all operands are on the stack anyway, so there&#8217;s no need to pass anything around explicitly.&nbsp; This is part of why concatenative languages are so extraordinarily concise.</p>
<p>Conditionals also look quite weird at first glance, but under the surface they are profoundly elegant:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="cat"><span style="color: #ff0000;">2</span> <span style="color: #ff0000;">3</span> plus    <span style="color: #cc0000;">// invoke the `plus` function</span>
<span style="color: #ff0000;">10</span> <span style="color: #0099ff; font-weight: bold;">&lt;</span>
<span style="font-weight: bold;">&#91;</span> <span style="color: #ff0000;">0</span> <span style="font-weight: bold;">&#93;</span>
<span style="font-weight: bold;">&#91;</span> <span style="color: #ff0000;">42</span> <span style="font-weight: bold;">&#93;</span>
<span style="color: #009966; font-weight: bold;">if</span></pre></td></tr></table></div>

<p>Naturally enough, this code pushes <code>0</code> onto the stack.&nbsp; The conditional for an <code>if</code> is just a boolean value pushed onto the stack.&nbsp; On top of that value, <code>if</code> will expect to find two quotations, one for the &#8220;then&#8221; branch and the other for the &#8220;else&#8221; branch.&nbsp; Since <code>5</code> is less than <code>10</code>, the boolean value will be <code>True</code>.&nbsp; The <code>if</code> function (and it could just as easily be a function) pops the quotations off of the stack as well as the boolean.&nbsp; Since the value is <code>True</code>, it discards the second quotation and executes the first, producing <code>0</code> on the stack.</p>
<p>I&#8217;ll leave you with the more complicated example of the factorial function:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="cat"><span style="color: #006699; font-weight: bold;">define</span> fac <span style="font-weight: bold;">&#123;</span>
  <span style="color: #009966; font-weight: bold;">dup</span> <span style="color: #ff0000;">0</span> <span style="color: #009966; font-weight: bold;">eq</span>
  <span style="font-weight: bold;">&#91;</span> <span style="color: #009966; font-weight: bold;">pop</span> <span style="color: #ff0000;">1</span> <span style="font-weight: bold;">&#93;</span>
  <span style="font-weight: bold;">&#91;</span> <span style="color: #009966; font-weight: bold;">dup</span> <span style="color: #ff0000;">1</span> <span style="color: #0099ff; font-weight: bold;">-</span> fac <span style="color: #0099ff; font-weight: bold;">*</span> <span style="font-weight: bold;">&#93;</span>
  <span style="color: #009966; font-weight: bold;">if</span>
<span style="font-weight: bold;">&#125;</span></pre></td></tr></table></div>

<p>Note that this isn&#8217;t even the most concise way of writing this, but it does the job.&nbsp; To see how, let&#8217;s look at how this will execute word-by-word (assuming an input of <code>4</code>):</p>
<table border="1">
<tr>
<td align="center"><b>Stack</b></td>
<td><b>Word</b></td>
</tr>
<tr>
<td><code>4</code></p>
<td><code>dup</code></td>
</tr>
<tr>
<td><code>4 4</code></p>
<td><code>0</code></td>
</tr>
<tr>
<td><code>4 4 0</code></p>
<td><code>eq</code></td>
</tr>
<tr>
<td><code>4 False</code></p>
<td><code>[ pop 1 ]</code></td>
</tr>
<tr>
<td><code>4 False [pop 1]</code></p>
<td><code>[ dup 1 - fac * ]</code></td>
</tr>
<tr>
<td><code>4 False [pop 1] [dup 1 - fac *]</code></p>
<td><code>if</code></td>
</tr>
<tr>
<td><code>4</code></p>
<td><code>dup</code></td>
</tr>
<tr>
<td><code>4 4</code></p>
<td><code>1</code></td>
</tr>
<tr>
<td><code>4 4 1</code></p>
<td><code>-</code></td>
</tr>
<tr>
<td><code>4 3</code></p>
<td><code>fac</code> <em>(assume magic recursion)</em></td>
</tr>
<tr>
<td><code>4 6</code></p>
<td><code>*</code></td>
</tr>
</table>
<p>The final result is <code>24</code>, a value which is left on the stack.&nbsp; Pretty nifty, eh?</p>
<h3>Conclusion</h3>
<p>You&#8217;ll notice this is a shorter post than I usually spew forth (no pun intended&#8230;this time).&nbsp; The reason being that I want this to be fairly easy to digest.&nbsp; Concatenative languages (and Cat in particular) are not all that difficult to digest.&nbsp; They are a slightly different way of thinking about programming, but as <a href="http://www.codecommit.com/blog/cat/the-joy-of-concatenative-languages-part-2">we will see in the next part</a>, not so different as it would seem.</p>
<p><strong>Note:</strong> Cat is written in C# and is available under the MIT License.&nbsp; Don&#8217;t fear the CLR though, Cat runs just fine under Mono.&nbsp; If you really want to experiment with no risk to yourself, a Javascript interpreter is available.</p>
<ul>
<li><a href="http://www.cat-language.com">Cat Language Website</a></li>
<li><a href="http://www.cat-language.com/interpreter.html">Interactive Javascript Interpreter for Cat</a></li>
</ul>
<img src="http://feeds.feedburner.com/~r/codecommit/~4/X7yyRB9CmbQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codecommit.com/blog/cat/the-joy-of-concatenative-languages-part-1/feed</wfw:commentRss>
		<feedburner:origLink>http://www.codecommit.com/blog/cat/the-joy-of-concatenative-languages-part-1</feedburner:origLink></item>
		<item>
		<title>Introduction to Automated Proof Verification with SASyLF</title>
		<link>http://feeds.codecommit.com/~r/codecommit/~3/X_sG5R3c2oE/introduction-to-automated-proof-verification-with-sasylf</link>
		<comments>http://www.codecommit.com/blog/scala/introduction-to-automated-proof-verification-with-sasylf#comments</comments>
		<pubDate>Mon, 01 Dec 2008 08:00:00 +0000</pubDate>
		<dc:creator>Daniel Spiewak</dc:creator>
		
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://www.codecommit.com/blog/scala/introduction-to-automated-proof-verification-with-sasylf</guid>
		<description><![CDATA[Doesn&#8217;t that title just get the blood pumping?&#160; Proof verification has a reputation for being an inordinately academic subject.&#160; In fact, even within scholarly (otherwise known as &#8220;unrealistically intelligent&#8220;) circles, the automated verification of proofs is known mainly as a complex, ugly and difficult task often not worth the effort.&#160; This is a shame really, [...]]]></description>
			<content:encoded><![CDATA[<p>Doesn&#8217;t that title just get the blood pumping?&nbsp; Proof verification has a reputation for being an inordinately academic subject.&nbsp; In fact, even within scholarly (otherwise known as &#8220;<em>unrealistically intelligent</em>&#8220;) circles, the automated verification of proofs is known mainly as a complex, ugly and difficult task often not worth the effort.&nbsp; This is a shame really, because rigorous proofs are at the very core of both mathematics and computer science.&nbsp; We are nothing without logic (paraphrased contrapositive from Descartes).&nbsp; Believe it or not, understanding basic proof techniques will be of tremendous aid to your cognitive process, even when working on slightly less ethereal problems such as how to get the freakin&#8217; login page to work properly.</p>
<p>Well, if you made it all the way to the second paragraph, then you either believe me when I say that this is legitimately useful (and cool!) stuff, or you&#8217;re just plain bored.&nbsp; Either way, read on as we commence our exciting journey into the land of rigorous proofs!</p>
<h3>SASyLF Crash Course</h3>
<p>If you&#8217;re at all familiar with the somewhat&#45;specialized field of proof verification, you probably know that <a href="http://www.sasylf.org">SASyLF</a> (pronounced &#8220;sassy elf&#8221;) is <em>not</em> the most widely used tool for the job.&nbsp; In fact, it may very well be the least well&#45;known.&nbsp; More commonly, proofs that require automatic verification are written in <a href="http://twelf.plparty.org/wiki/Main_Page">Twelf</a> or <a href="http://coq.inria.fr/">Coq</a>.&nbsp; Both of these are fine tools and capable of a lot more than SASyLF, but they can also be extremely difficult to use.&nbsp; One of the primary motivations behind SASyLF was to produce a tool which was easier to learn, had a higher level syntax (easier to read) and which gave more helpful error messages than Twelf.&nbsp; The main idea behind these convolutions was to produce a tool which was more suitable for use in the classroom.</p>
<p>The main design decision which sets SASyLF apart from Twelf is the way in which proofs are expressed.&nbsp; As I understand it, Twelf exploits <a href="http://en.wikipedia.org/wiki/Curry&#45;Howard_correspondence">Curry&#45;Howard correspondence</a> to represent proofs implicitly in the types of a functional program (<strong>update:</strong> this is incorrect; <a href="http://www.codecommit.com/blog/scala/introduction-to-automated-proof-verification-with-sasylf#comment-4373">see below</a>).&nbsp; While this can be very powerful, it&#8217;s not the most intuitive way to think about a proof.&nbsp; Eschewing this approach, SASyLF expresses proofs using <em>unification</em> (very similar to Prolog) and defines inference rules explicitly in a natural&#45;language style.</p>
<p>There are three main components to a SASyLF proof:</p>
<ul>
<li>Syntax</li>
<li>Judgments</li>
<li>Theorems/Lemmas</li>
</ul>
<p>Intuitively enough, the syntax section is where we express the grammar for the language used throughout our proof.&nbsp; This grammar is expressed very naturally using BNF, just as if we were defining the language mathematically for a hand&#45;written proof.&nbsp; Left&#45;recursion is allowed, as is right&#45;recursion, arbitrary symbols, ambiguity and so on.&nbsp; SASyLF&#8217;s parser is mind bogglingly powerful, capable of chewing threw just about any syntax you throw at it.&nbsp; The main restriction is that you cannot use parentheses, square brackets (<code>[]</code>), pipes (<code>|</code>) or periods (<code>.</code>) in your syntax.&nbsp; The pure&#45;untyped lambda calculus defined in SASyLF would look something like this:</p>
<div class="wp_syntax">
<table>
<tr>
<td class="line_numbers"></td>
<td class="code">
<pre class="sasylf"><span style="color: #000000;">t ::=<span style="color: #ff00cc;"> </span><span style="color: #ff00cc;">fn</span><span style="color: #ff00cc;"> </span><span style="color: #66ccff; font-weight: bold;">x</span><span style="color: #ff00cc;"> </span><span style="color: #ff00cc;">=</span><span style="color: #ff00cc;">&gt;</span><span style="color: #ff00cc;"> </span><span style="color: #ff00cc;">t</span><span style="color: #000000; font-weight: bold;">[</span><span style="color: #66ccff; font-weight: bold;">x</span><span style="color: #000000; font-weight: bold;">]</span>
<span style="color: #000000; font-weight: bold;"> </span><span style="color: #000000; font-weight: bold;"> </span><span style="color: #000000; font-weight: bold;"> </span><span style="color: #000000; font-weight: bold;"> </span><span style="color: #000000; font-weight: bold;">|</span><span style="color: #000000; font-weight: bold;"> </span><span style="color: #ff00cc;">t</span><span style="color: #ff00cc;"> </span><span style="color: #ff00cc;">t</span>
<span style="color: #000000; font-weight: bold;"> </span><span style="color: #000000; font-weight: bold;"> </span><span style="color: #000000; font-weight: bold;"> </span><span style="color: #000000; font-weight: bold;"> </span><span style="color: #000000; font-weight: bold;">|</span><span style="color: #000000; font-weight: bold;"> </span><span style="color: #66ccff; font-weight: bold;">x</span>
</span></pre>
</td>
</tr>
</table>
</div>
<p>I said we couldn&#8217;t use brackets, but that&#8217;s only because SASyLF assigns some special magic to these operators.&nbsp; In a nutshell, they allow the above definition of lambda calculus to ignore all of the issues associated with variable name freshness and context.&nbsp; For simplicity&#8217;s sake, that&#8217;s about as far as I&#8217;m going to go into these mysterious little thingies.</p>
<p>The judgments section is where we define our inference rules.&nbsp; Just as if we were defining these rules by hand, the syntax has the conditionals above a line of hyphens with the conclusion below.&nbsp; The label for the rule goes to the right of the &#8220;line&#8221;.&nbsp; What could be more natural?</p>
<div class="wp_syntax">
<table>
<tr>
<td class="line_numbers"></td>
<td class="code">
<pre class="sasylf"><span style="color: #000000;"><span style="color: #006699; font-weight: bold;">judgment</span><span style="color: #006699; font-weight: bold;"> </span><span style="color: #9900cc;">eval</span><span style="color: #006699; font-weight: bold;">:</span> t &#45;&gt; t

t1 &#45;&gt; t1&#8242;
<span style="color: #cc00cc;">&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;</span><span style="color: #cc00cc;"> </span><span style="color: #cc00cc;">E&#45;Beta1</span>
t1 t2 &#45;&gt; t1&#8242; t2
</span></pre>
</td>
</tr>
</table>
</div>
<p>The <code>judgment</code> syntax is what defines the syntax for the <code>&#45;&gt;</code> &#8220;operator&#8221;.&nbsp; Once SASyLF sees this, it knows that we may define rules of the form <code>t &#45;&gt; t</code>, where <code>t</code> is defined by the syntax section.&nbsp; Further on down, SASyLF sees our <code>E&#45;Beta1</code> rule.&nbsp; Each of the tokens within this rule (aside from <code>&#45;&gt;</code>) begins with &#8220;<code>t</code>&#8220;.&nbsp; From this, SASyLF is able to infer that we mean &#8220;a term as defined previously&#8221;.&nbsp; Thus, this rule is syntactically valid according to our evaluation judgment and the syntax given above.</p>
<p>Of course, theorems are where you will find the real meat of any proof (I&#8217;m using the word &#8220;proof&#8221; very loosely to mean the collection of proven theorems and lemmas which indicates some fact(s) about a language).&nbsp; SASyLF wouldn&#8217;t be a very complete proof verification system without support for some form of proving.&nbsp; Once again, the syntax is extremely natural language, almost to the point of being overly&#45;verbose.&nbsp; A simple theorem given the rules above plus a little would be to show that values cannot evaluate:</p>
<div class="wp_syntax">
<table>
<tr>
<td class="line_numbers"></td>
<td class="code">
<pre class="sasylf"><span style="color: #000000;"><span style="color: #006699; font-weight: bold;">theorem</span> <span style="color: #9900cc;">eval&#45;value&#45;implies&#45;contradiction:</span>
    <span style="color: #009966; font-weight: bold;">forall</span><span style="color: #009966; font-weight: bold;"> </span><span style="color: #9966ff;">e:</span> t &#45;&gt; t&#8217;
    <span style="color: #009966; font-weight: bold;">forall</span><span style="color: #009966; font-weight: bold;"> </span><span style="color: #9966ff;">v:</span> t value
    <span style="color: #009966; font-weight: bold;">exists</span> contradiction <span style="color: #000000; font-weight: bold;">.</span>

<span style="color: #9966ff;"> </span><span style="color: #9966ff;"> </span><span style="color: #9966ff;"> </span><span style="color: #9966ff;"> </span><span style="color: #9966ff;">_:</span><span style="color: #9966ff;"> </span>contradiction <span style="color: #000000; font-weight: bold;">by</span><span style="color: #000000; font-weight: bold;"> </span><span style="background: #ffffcc; color: #ff0066;">unproved</span>
<span style="color: #006699; font-weight: bold;">end theorem</span>
</span></pre>
</td>
</tr>
</table>
</div>
<p>Note that <code>contradiction</code> is not more SASyLF magic.&nbsp; We can actually define what it means to have a contradiction by adding the following lines to our judgment section:</p>
<div class="wp_syntax">
<table>
<tr>
<td class="line_numbers"></td>
<td class="code">
<pre class="sasylf"><span style="color: #000000;"><span style="color: #006699; font-weight: bold;">judgment</span><span style="color: #006699; font-weight: bold;"> </span><span style="color: #9900cc;">absurd</span><span style="color: #006699; font-weight: bold;">:</span> contradiction
</span></pre>
</td>
</tr>
</table>
</div>
<p>In other words, we can have a contradiction, but there are no rules which allow us to get it.&nbsp; In fact, the only way to have a contradiction is to somehow get SASyLF to the point where it sees that there are no cases which satisfy some set of proven facts (given the <code>forall</code> assumptions).&nbsp; If SASyLF cannot find any cases to satisfy some rules, it allows us to derive anything at all, including judgments which have no corresponding rules.</p>
<p>Readers who have yet to fall asleep will notice that I cleverly elided a portion of the &#8220;<code>theorem</code>&#8221; code snippet.&nbsp; That&#8217;s because there wasn&#8217;t really a way to prove that contradiction given the drastically abbreviated rules given in earlier samples.&nbsp; Instead of proving anything, I used a special SASyLF justification, <code>unproved</code>, which allows the derivation of any fact given no input (very useful for testing incomplete proofs).&nbsp; Lambda calculus isn&#8217;t much more complicated than what I showed, but it does require more than just an application context rule in its evaluation semantics.&nbsp; In order to get a taste for SASyLF&#8217;s proof syntax, we&#8217;re going to need to look at a much simpler language.</p>
<h3>Case Study: Integer Comparison</h3>
<p>For this case study, we&#8217;re going to be working with simple counting numbers which start with <code>0</code> and then proceed upwards, each value expressed as the successor of its previous value.&nbsp; Thus, the logical number 3 would be <code>s s s 0</code>.&nbsp; Not a very useful language in the real world, but much easier to deal with in the field of proof verification.&nbsp; The syntax for our natural numbers looks like this:</p>
<div class="wp_syntax">
<table>
<tr>
<td class="line_numbers"></td>
<td class="code">
<pre class="sasylf"><span style="color: #000000;">n ::=<span style="color: #ff00cc;"> </span><span style="color: #ff00cc;">0</span>
<span style="color: #000000; font-weight: bold;"> </span><span style="color: #000000; font-weight: bold;"> </span><span style="color: #000000; font-weight: bold;"> </span><span style="color: #000000; font-weight: bold;"> </span><span style="color: #000000; font-weight: bold;">|</span><span style="color: #000000; font-weight: bold;"> </span><span style="color: #ff00cc;">s</span><span style="color: #ff00cc;"> </span><span style="color: #ff00cc;">n</span>
</span></pre>
</td>
</tr>
</table>
</div>
<p>With this humble definition for <code>n</code>, we can go on to define the mathematical greater&#45;than comparison using two rules under a single judgment:</p>
<div class="wp_syntax">
<table>
<tr>
<td class="line_numbers"></td>
<td class="code">
<pre class="sasylf"><span style="color: #000000;"><span style="color: #006699; font-weight: bold;">judgment</span><span style="color: #006699; font-weight: bold;"> </span><span style="color: #9900cc;">gt</span><span style="color: #006699; font-weight: bold;">:</span> n &gt; n

<span style="color: #cc00cc;">&#45;&#45;&#45;&#45;&#45;&#45;&#45;</span><span style="color: #cc00cc;"> </span><span style="color: #cc00cc;">gt&#45;one</span>
s n &gt; n

n1 &gt; n2
<span style="color: #cc00cc;">&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;</span><span style="color: #cc00cc;"> </span><span style="color: #cc00cc;">gt&#45;more</span>
s n1 &gt; n2
</span></pre>
</td>
</tr>
</table>
</div>
<p>Believe it or not, this is all we need to do in terms of definition.&nbsp; The first rule says that the successor of any number is greater than that same number (3 &gt; 2).&nbsp; The second rule states that if we already have two numbers, one greater than the other (12 &gt; 4), then the successor of the greater number will still be greater than the lesser (13 &gt; 4).&nbsp; All very intuitive, but the real question is whether or not we can prove anything with these definitions.</p>
<h4>An Easy Lemma</h4>
<p>For openers, we can try something reasonably simple: prove that all non&#45;zero numbers are greater than zero.&nbsp; This is such a simple proof that we won&#8217;t even bother calling it a theorem, we will give it the lesser rank of &#8220;lemma&#8221;:</p>
<div class="wp_syntax">
<table>
<tr>
<td class="line_numbers"></td>
<td class="code">
<pre class="sasylf"><span style="color: #000000;"><span style="color: #006699; font-weight: bold;">lemma</span> <span style="color: #9900cc;">all&#45;gt&#45;zero:</span>
    <span style="color: #009966; font-weight: bold;">forall</span><span style="color: #009966; font-weight: bold;"> </span>n
    <span style="color: #009966; font-weight: bold;">exists</span> s n &gt; 0 <span style="color: #000000; font-weight: bold;">.</span>

<span style="color: #9966ff;">    </span><span style="color: #9966ff;">_:</span><span style="color: #9966ff;"> </span>s n &gt; 0 <span style="color: #000000; font-weight: bold;">by</span><span style="color: #000000; font-weight: bold;"> </span><span style="color: #0099ff; font-weight: bold;">induction</span><span style="color: #9966ff;"> </span><span style="color: #000000; font-weight: bold;">on </span><span style="color: #9966ff;">n:</span>
        <span style="color: #006699; font-weight: bold;">case</span> 0 <span style="color: #006699; font-weight: bold;">is</span>
<span style="color: #9966ff;">    </span><span style="color: #9966ff;">    </span><span style="color: #9966ff;">    </span><span style="color: #9966ff;">_:</span><span style="color: #9966ff;"> </span>s 0 &gt; 0 <span style="color: #000000; font-weight: bold;">by</span><span style="color: #000000; font-weight: bold;"> </span><span style="color: #0099ff; font-weight: bold;">rule</span><span style="color: #0099ff; font-weight: bold;"> </span><span style="color: #cc00cc;">gt&#45;one</span>
        <span style="color: #006699; font-weight: bold;">end case</span>

        <span style="color: #006699; font-weight: bold;">case</span> s n1 <span style="color: #006699; font-weight: bold;">is</span>
<span style="color: #9966ff;">    </span><span style="color: #9966ff;">    </span><span style="color: #9966ff;">    </span><span style="color: #9966ff;">g:</span><span style="color: #9966ff;"> </span>s n1 &gt; 0 <span style="color: #000000; font-weight: bold;">by</span><span style="color: #000000; font-weight: bold;"> </span><span style="color: #0099ff; font-weight: bold;">induction hypothesis</span><span style="color: #9966ff;"> </span><span style="color: #000000; font-weight: bold;">on </span><span style="color: #9966ff;">n1</span>
<span style="color: #9966ff;">    </span><span style="color: #9966ff;">    </span><span style="color: #9966ff;">    </span><span style="color: #9966ff;">_:</span><span style="color: #9966ff;"> </span>s s n1 &gt; 0 <span style="color: #000000; font-weight: bold;">by</span><span style="color: #000000; font-weight: bold;"> </span><span style="color: #0099ff; font-weight: bold;">rule</span><span style="color: #0099ff; font-weight: bold;"> </span><span style="color: #cc00cc;">gt&#45;more</span><span style="color: #0099ff; font-weight: bold;"> </span><span style="color: #000000; font-weight: bold;">on </span><span style="color: #9966ff;">g</span>
        <span style="color: #006699; font-weight: bold;">end case</span>
    <span style="color: #006699; font-weight: bold;">end induction</span>
<span style="color: #006699; font-weight: bold;">end lemma</span>
</span></pre>
</td>
</tr>
</table>
</div>
<p>In order to prove anything about <code>n</code>, we first need to &#8220;pull it apart&#8221; and find out what it&#8217;s made of.&nbsp; To do that, we&#8217;re going to use <code>induction</code>.&nbsp; We could also use <code>case analysis</code>, but that would only work if our proof didn&#8217;t require &#8220;recursion&#8221; (we&#8217;ll get to this in a minute).&nbsp; There are two cases as given by the syntax for <code>n</code>: when <code>n</code> is &#8220;<code>0</code>&#8220;, and when <code>n</code> is &#8220;<code>s n1</code>&#8220;, where <code>n1</code> is some other number.&nbsp; We must prove that <code>s n &gt; 0</code> for both of these cases individually, otherwise our proof is not valid.</p>
<p>The first case is easy.&nbsp; When <code>n</code> is 0, the proof is trivial using the rule <code>gt&#45;one</code>.&nbsp; Notice that within this case we are no longer proving <code>s n &gt; 0</code>, but rather <code>s 0 &gt; 0</code>.&nbsp; This is the huge win brought by SASyLF&#8217;s unification: <code>n</code> <em>is</em> &#8220;<code>0</code>&#8221; within this case.&nbsp; Anything we already know about <code>n</code>, we also know about <code>0</code>.&nbsp; When we apply the rule <code>gt&#45;one</code>, SASyLF sees that we are attempting to prove <code>s n &gt; n</code> where <code>n</code> is &#8220;<code>0</code>&#8220;.&nbsp; This is valid by the rule, so the verification passes.</p>
<p>The second case is where things get interesting.&nbsp; We have that <code>n</code> is actually <code>s n1</code>, but that doesn&#8217;t really get us too much closer to proving <code>s s n1 &gt; 0</code> (remember, unification).&nbsp; Fortunately, we <em>can</em> prove that <code>s n1 &gt; 0</code> because we&#8217;re writing a lemma at this very moment which prove that.&nbsp; This is like writing a function to sum all the values in a list: when the list is empty, the result is trivial; but when the list has contents, we must take the head and then add it to the sum of the tail as computed by&#8230;ourself.&nbsp; Induction is literally just recursion in logic.&nbsp; Interestingly enough, SASyLF is smart enough to look at all of the inductive cases in your proof and verify that they are valid.&nbsp; This is sort&#45;of the equivalent of a compiler looking at your code and telling you whether or not it will lead to an infinite loop.</p>
<p>To get that <code>s n1 &gt; 0</code>, we use the induction hypothesis, passing <code>n1</code> as the &#8220;parameter&#8221;.&nbsp; However, we&#8217;re not quite done yet.&nbsp; We need to prove that <code>s s n1 &gt; 0</code> in order to unify with our original target (<code>s n &gt; 0</code>).&nbsp; Fortunately, we already have a rule that allows us to prove the successor of a number retains its greater&#45;than status: <code>gt&#45;more</code>.</p>
<p>However, <code>gt&#45;more</code> has a condition in our definition.&nbsp; It requires that we already have some fact <code>n1 &gt; n2</code> in order to obtain <code>s n1 &gt; n2</code>.&nbsp; In our case, we already have this fact (<code>s n1 &gt; 0</code>), but we need to &#8220;pass&#8221; it to the rule.&nbsp; SASyLF allows us to do this by giving our facts labels.&nbsp; In this case, we have labeled the <code>s n1 &gt; 0</code> fact as &#8220;<code>g</code>&#8220;.&nbsp; We take this fact, pack it up and send it to <code>gt&#45;more</code> and it gives us back our final goal.</p>
<h4>A Slightly Harder Theorem</h4>
<p>A slightly more difficult task would be to prove that the successors of two numbers preserves their greater&#45;than relationship.&nbsp; Thus, if we know that 4 &gt; 3, we can prove that 5 &gt; 4.&nbsp; More formally:</p>
<div class="wp_syntax">
<table>
<tr>
<td class="line_numbers"></td>
<td class="code">
<pre class="sasylf"><span style="color: #000000;"><span style="color: #006699; font-weight: bold;">theorem</span> <span style="color: #9900cc;">gt&#45;implies&#45;gt&#45;succ:</span>
    <span style="color: #009966; font-weight: bold;">forall</span><span style="color: #009966; font-weight: bold;"> </span><span style="color: #9966ff;">g:</span> n1 &gt; n2
    <span style="color: #009966; font-weight: bold;">exists</span> s n1 &gt; s n2 <span style="color: #000000; font-weight: bold;">.</span>

<span style="color: #9966ff;">    </span><span style="color: #9966ff;">_:</span><span style="color: #9966ff;"> </span>s n1 &gt; s n2 <span style="color: #000000; font-weight: bold;">by</span><span style="color: #000000; font-weight: bold;"> </span><span style="background: #ffffcc; color: #ff0066;">unproved</span>
<span style="color: #006699; font-weight: bold;">end theorem</span>
</span></pre>
</td>
</tr>
</table>
</div>
<p>At first glance, this looks impossible since we don&#8217;t really have a rule dealing with <code>s n</code> on the right&#45;hand side of the <code>&gt;</code>&#45;sign.&nbsp; We can try to prove this one step at a time to see whether or not this intuition is correct.</p>
<p>Almost any lemma of interest is going to require induction, so immediately we jump to inducting on the only fact we have available: <code>g</code>.&nbsp; Note that this is different from what we had in the earlier example.&nbsp; Instead of getting the different syntactic cases, we&#8217;re looking at the the rules which would have allowed the input to be constructed.&nbsp; After all, whoever &#8220;called&#8221; our theorem will have needed to somehow prove that <code>n1 &gt; n2</code>, it would be helpful to know what facts they used to do that.&nbsp; SASyLF allows this using the <code>case rule</code> syntax.&nbsp; We start with the easy base case:</p>
<div class="wp_syntax">
<table>
<tr>
<td class="line_numbers"></td>
<td class="code">
<pre class="sasylf"><span style="color: #000000;"><span style="color: #9966ff;">_:</span><span style="color: #9966ff;"> </span>s n1 &gt; s n2 <span style="color: #000000; font-weight: bold;">by</span><span style="color: #000000; font-weight: bold;"> </span><span style="color: #0099ff; font-weight: bold;">induction</span><span style="color: #9966ff;"> </span><span style="color: #000000; font-weight: bold;">on </span><span style="color: #9966ff;">g:</span>
    <span style="color: #006699; font-weight: bold;">case rule</span>
<span style="color: #cc00cc;">    </span><span style="color: #cc00cc;">    </span><span style="color: #cc00cc;">&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;</span><span style="color: #cc00cc;"> </span><span style="color: #cc00cc;">gt&#45;one</span>
<span style="color: #9966ff;">    </span><span style="color: #9966ff;">    </span><span style="color: #9966ff;">_:</span><span style="color: #9966ff;"> </span>s n2 &gt; n2
    <span style="color: #006699; font-weight: bold;">is</span>
<span style="color: #9966ff;">    </span><span style="color: #9966ff;">    </span><span style="color: #9966ff;">_:</span><span style="color: #9966ff;"> </span>s s n2 &gt; s n2 <span style="color: #000000; font-weight: bold;">by</span><span style="color: #000000; font-weight: bold;"> </span><span style="color: #0099ff; font-weight: bold;">rule</span><span style="color: #0099ff; font-weight: bold;"> </span><span style="color: #cc00cc;">gt&#45;one</span>
    <span style="color: #006699; font-weight: bold;">end case</span>
<span style="color: #006699; font-weight: bold;">end induction</span>
</span></pre>
</td>
</tr>
</table>
</div>
<p>In this case, the term <code>_: s n2 &gt; n2</code> is unified with <code>n1 &gt; n2</code>.&nbsp; Thus, <code>n1</code> is actually &#8220;<code>s n2</code>&#8220;.&nbsp; This means that by unification, we are actually trying to prove <code>s s n2 > s n2</code>.&nbsp; Fortunately, we have a rule for that.&nbsp; If we let &#8220;<code>n</code>&#8221; be &#8220;<code>s n2</code>&#8220;, we can easily apply the rule <code>gt&#45;one</code> to produce the desired result.</p>
<p>The second case is a bit trickier.&nbsp; We start out by defining the case rule according to the inference rules given in the judgment section.&nbsp; The only case left is <code>gt&#45;more</code>, so we mindlessly copy/paste and correct the variables to suit our needs:</p>
<div class="wp_syntax">
<table>
<tr>
<td class="line_numbers"></td>
<td class="code">
<pre class="sasylf"><span style="color: #000000;"><span style="color: #006699; font-weight: bold;">case rule</span>
<span style="color: #9966ff;">    </span><span style="color: #9966ff;">g1:</span><span style="color: #9966ff;"> </span>n11 &gt; n2
<span style="color: #cc00cc;">    </span><span style="color: #cc00cc;">&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;</span><span style="color: #cc00cc;"> </span><span style="color: #cc00cc;">gt&#45;more</span>
<span style="color: #9966ff;">    </span><span style="color: #9966ff;">_:</span><span style="color: #9966ff;"> </span>s n11 &gt; n2
<span style="color: #006699; font-weight: bold;">is</span>
<span style="color: #9966ff;">    </span><span style="color: #9966ff;">_:</span><span style="color: #9966ff;"> </span>s s n11 &gt; s n2 <span style="color: #000000; font-weight: bold;">by</span><span style="color: #000000; font-weight: bold;"> </span><span style="background: #ffffcc; color: #ff0066;">unproved</span>
<span style="color: #006699; font-weight: bold;">end case</span>
</span></pre>
</td>
</tr>
</table>
</div>
<p>In this case, <code>n1</code> actually unifies with &#8220;<code>s n11</code>&#8220;.&nbsp; This is probably the most annoying aspect of SASyLF: all of the syntax is determined by token prefix, so <em>every</em> number has to start with <code>n</code>, occasionally making proofs a little difficult to follow.</p>
<p>At this point, we need to derive <code>s s n11 &gt; s n2</code>.&nbsp; Since the left and right side of the <code>&gt;</code> &#8220;operator&#8221; do not share a common sub&#45;term, the only rule which could possibly help us is <code>gt&#45;more</code>.&nbsp; In order to apply this rule, we will somehow need to derive <code>s n11 &gt; s n2</code> (remember, <code>gt&#45;more</code> takes a known greater&#45;than relationship and then tells us something about how the left&#45;successor relates to the right).&nbsp; We can reflect this &#8220;bottom&#45;up&#8221; step towards a proof in the following way:</p>
<div class="wp_syntax">
<table>
<tr>
<td class="line_numbers"></td>
<td class="code">
<pre class="sasylf"><span style="color: #000000;"><span style="color: #006699; font-weight: bold;">case rule</span>
<span style="color: #9966ff;">    </span><span style="color: #9966ff;">g1:</span><span style="color: #9966ff;"> </span>n11 &gt; n2
<span style="color: #cc00cc;">    </span><span style="color: #cc00cc;">&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;</span><span style="color: #cc00cc;"> </span><span style="color: #cc00cc;">gt&#45;more</span>
<span style="color: #9966ff;">    </span><span style="color: #9966ff;">_:</span><span style="color: #9966ff;"> </span>s n11 &gt; n2
<span style="color: #006699; font-weight: bold;">is</span>
<span style="color: #9966ff;">    </span><span style="color: #9966ff;">g:</span><span style="color: #9966ff;"> </span>s n11 &gt; s n2 <span style="color: #000000; font-weight: bold;">by</span><span style="color: #000000; font-weight: bold;"> </span><span style="background: #ffffcc; color: #ff0066;">unproved</span>
<span style="color: #9966ff;">    </span><span style="color: #9966ff;">_:</span><span style="color: #9966ff;"> </span>s s n11 &gt; s n2 <span style="color: #000000; font-weight: bold;">by</span><span style="color: #000000; font-weight: bold;"> </span><span style="color: #0099ff; font-weight: bold;">rule</span><span style="color: #0099ff; font-weight: bold;"> </span><span style="color: #cc00cc;">gt&#45;more</span><span style="color: #0099ff; font-weight: bold;"> </span><span style="color: #000000; font-weight: bold;">on </span><span style="color: #9966ff;">g</span>
<span style="color: #006699; font-weight: bold;">end case</span>
</span></pre>
</td>
</tr>
</table>
</div>
<p>At this point, SASyLF will warn us about the <code>unproved</code>, but it will happily pass the rest of our theorem.&nbsp; This technique for proof development is extremely handy in more complicated theorems.&nbsp; The ability to find out whether or not your logic is sound even before it is complete can be very reassuring (in this way you can avoid chasing entirely down the wrong logical path).</p>
<p>In order to make this whole thing work, we need to somehow prove <code>s n11 &gt; s n2</code>.&nbsp; Fortunately, we just so happen to be working on a theorem which could prove this if we could supply <code>n11 &gt; n2</code>.&nbsp; This fact is conveniently available with the label of &#8220;<code>g1</code>&#8220;.&nbsp; We feed this into the <code>induction hypothesis</code> to achieve our goal.&nbsp; The final theorem looks like this:</p>
<div class="wp_syntax">
<table>
<tr>
<td class="line_numbers"></td>
<td class="code">
<pre class="sasylf"><span style="color: #000000;"><span style="color: #006699; font-weight: bold;">theorem</span> <span style="color: #9900cc;">gt&#45;implies&#45;gt&#45;succ:</span>
    <span style="color: #009966; font-weight: bold;">forall</span><span style="color: #009966; font-weight: bold;"> </span><span style="color: #9966ff;">g:</span> n1 &gt; n2
    <span style="color: #009966; font-weight: bold;">exists</span> s n1 &gt; s n2 <span style="color: #000000; font-weight: bold;">.</span>

<span style="color: #9966ff;">    </span><span style="color: #9966ff;">_:</span><span style="color: #9966ff;"> </span>s n1 &gt; s n2 <span style="color: #000000; font-weight: bold;">by</span><span style="color: #000000; font-weight: bold;"> </span><span style="color: #0099ff; font-weight: bold;">induction</span><span style="color: #9966ff;"> </span><span style="color: #000000; font-weight: bold;">on </span><span style="color: #9966ff;">g:</span>
        <span style="color: #006699; font-weight: bold;">case rule</span>
<span style="color: #cc00cc;">    </span><span style="color: #cc00cc;">    </span><span style="color: #cc00cc;">    </span><span style="color: #cc00cc;">&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;</span><span style="color: #cc00cc;"> </span><span style="color: #cc00cc;">gt&#45;one</span>
<span style="color: #9966ff;">    </span><span style="color: #9966ff;">    </span><span style="color: #9966ff;">    </span><span style="color: #9966ff;">_:</span><span style="color: #9966ff;"> </span>s n2 &gt; n2
        <span style="color: #006699; font-weight: bold;">is</span>
<span style="color: #9966ff;">    </span><span style="color: #9966ff;">    </span><span style="color: #9966ff;">    </span><span style="color: #9966ff;">_:</span><span style="color: #9966ff;"> </span>s s n2 &gt; s n2 <span style="color: #000000; font-weight: bold;">by</span><span style="color: #000000; font-weight: bold;"> </span><span style="color: #0099ff; font-weight: bold;">rule</span><span style="color: #0099ff; font-weight: bold;"> </span><span style="color: #cc00cc;">gt&#45;one</span>
        <span style="color: #006699; font-weight: bold;">end case</span>

        <span style="color: #006699; font-weight: bold;">case rule</span>
<span style="color: #9966ff;">    </span><span style="color: #9966ff;">    </span><span style="color: #9966ff;">    </span><span style="color: #9966ff;">g1:</span><span style="color: #9966ff;"> </span>n11 &gt; n2
<span style="color: #cc00cc;">    </span><span style="color: #cc00cc;">    </span><span style="color: #cc00cc;">    </span><span style="color: #cc00cc;">&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;&#45;</span><span style="color: #cc00cc;"> </span><span style="color: #cc00cc;">gt&#45;more</span>
<span style="color: #9966ff;">    </span><span style="color: #9966ff;">    </span><span style="color: #9966ff;">    </span><span style="color: #9966ff;">_:</span><span style="color: #9966ff;"> </span>s n11 &gt; n2
        <span style="color: #006699; font-weight: bold;">is</span>
<span style="color: #9966ff;">    </span><span style="color: #9966ff;">    </span><span style="color: #9966ff;">    </span><span style="color: #9966ff;">g2:</span><span style="color: #9966ff;"> </span>s n11 &gt; s n2 <span style="color: #000000; font-weight: bold;">by</span><span style="color: #000000; font-weight: bold;"> </span><span style="color: #0099ff; font-weight: bold;">induction hypothesis</span><span style="color: #9966ff;"> </span><span style="color: #000000; font-weight: bold;">on </span><span style="color: #9966ff;">g1</span>
<span style="color: #9966ff;">    </span><span style="color: #9966ff;">    </span><span style="color: #9966ff;">    </span><span style="color: #9966ff;">_:</span><span style="color: #9966ff;"> </span>s s n11 &gt; s n2 <span style="color: #000000; font-weight: bold;">by</span><span style="color: #000000; font-weight: bold;"> </span><span style="color: #0099ff; font-weight: bold;">rule</span><span style="color: #0099ff; font-weight: bold;"> </span><span style="color: #cc00cc;">gt&#45;more</span><span style="color: #0099ff; font-weight: bold;"> </span><span style="color: #000000; font-weight: bold;">on </span><span style="color: #9966ff;">g2</span>
        <span style="color: #006699; font-weight: bold;">end case</span>
    <span style="color: #006699; font-weight: bold;">end induction</span>
<span style="color: #006699; font-weight: bold;">end theorem</span>
</span></pre>
</td>
</tr>
</table>
</div>
<h3>Conclusion</h3>
<p>I realize this was a bit of a deviation from my normal semi&#45;practical posts, but I think it was still a journey well worth taking.&nbsp; If you&#8217;re working as a serious developer in this industry, I strongly suggest that you find yourself a good formal language and/or type theory textbook (<a href="http://www.amazon.com/Types&#45;Programming&#45;Languages&#45;Benjamin&#45;Pierce/dp/0262162091/ref=pd_bbs_sr_1?ie=UTF8&#038;s=books&#038;qid=1228110398&#038;sr=8&#45;1">might I recommend?</a>) and follow it through the best that you can.&nbsp; The understanding of how languages are formally constructed and the mental circuits to create those proofs yourself will have a surprisingly powerful impact on your day&#45;to&#45;day programming.&nbsp; Knowing how the properties of a language are proven provides tremendous illumination into why that language is the way it is and somtimes how it can be made better.</p>
<p><b>Credit:</b> Examples in this post drawn rather unimaginatively from <a href="http://www.cs.uwm.edu/~boyland/">Dr. John Boyland&#8217;s</a> excellent course in type theory.</p>
<ul>
<li><a href="http://www.cs.cmu.edu/~aldrich/SASyLF/">SASyLF Main Page</a> at Carnegie Mellon University</li>
<li><a href="http://www.codecommit.com/blog/misc/math.slf">Download math.slf</a> (the full greater&#45;than example)</li>
<li><a href="http://www.cs.cmu.edu/~aldrich/SASyLF/fdpe08.pdf">Further reading&#8230;</a></li>
</ul>
<img src="http://feeds.feedburner.com/~r/codecommit/~4/X_sG5R3c2oE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codecommit.com/blog/scala/introduction-to-automated-proof-verification-with-sasylf/feed</wfw:commentRss>
		<feedburner:origLink>http://www.codecommit.com/blog/scala/introduction-to-automated-proof-verification-with-sasylf</feedburner:origLink></item>
	</channel>
</rss>
