<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Alonzo</title>
    <description>Alonzo is a Java software project to code a Lambda Calculus interpreter. Alongside the code lives this blog dedicated to writing and teaching about what goes into writing the interpreter, and interesting coding choices.
</description>
    <link>/alonzo-java/</link>
    <atom:link href="/alonzo-java/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Fri, 30 Oct 2015 00:00:23 -0700</pubDate>
    <lastBuildDate>Fri, 30 Oct 2015 00:00:23 -0700</lastBuildDate>
    <generator>Jekyll v2.5.3</generator>
    
      <item>
        <title>Format-String All the Things!</title>
        <description>&lt;p&gt;I’ve picked up a practice recently: any time I need to provide a message (typically for errors or logging), I make sure the function in question takes &lt;code&gt;varargs&lt;/code&gt; and then pass them to &lt;a href=&quot;http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#format-java.lang.String-java.lang.Object...-&quot;&gt;&lt;code&gt;String.format&lt;/code&gt;&lt;/a&gt;. Most of the time I provide a message, I eventually want to use a format-string and it’s just lame to have to call &lt;code&gt;String.format&lt;/code&gt; every time.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;k&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;UnexpectedIssueException&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Could not invoke test method \&amp;quot;%s\&amp;quot;.&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;testName&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p class=&quot;github_sample_ref&quot;&gt;&lt;small&gt;&lt;a href=&quot;https://github.com/alexleighton/alonzo-java/blob/1f0b04e71923642f3b8d654390591970c7632d57//tst/alonzo/unit/RunnableTest.java#L84-L84&quot;&gt;/tst/alonzo/unit/RunnableTest.java&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;

</description>
        <pubDate>Thu, 29 Oct 2015 00:00:00 -0700</pubDate>
        <link>/alonzo-java/brief/2015/10/29/format-strings.html</link>
        <guid isPermaLink="true">/alonzo-java/brief/2015/10/29/format-strings.html</guid>
        
        <category>brief</category>
        
        <category>format-strings</category>
        
        <category>exceptions</category>
        
        
        <category>brief</category>
        
      </item>
    
      <item>
        <title>Validate.notNull</title>
        <description>&lt;p&gt;I’ve added a utility class for Validating preconditions, heavily inspired by the &lt;a href=&quot;https://commons.apache.org/proper/commons-lang/javadocs/api-3.4/org/apache/commons/lang3/Validate.html#notNull(T,%20java.lang.String,%20java.lang.Object...)&quot;&gt;&lt;code&gt;Validate&lt;/code&gt;&lt;/a&gt; class found in &lt;a href=&quot;https://commons.apache.org/proper/commons-lang/&quot;&gt;Apache Commons&lt;/a&gt;.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;cm&quot;&gt;/**&lt;/span&gt;
&lt;span class=&quot;cm&quot;&gt; * Throws {@link NullPointerException} if the given object is null, returning the given object&lt;/span&gt;
&lt;span class=&quot;cm&quot;&gt; * if it is not null. The thrown NullPointerException will contain a message created by passing&lt;/span&gt;
&lt;span class=&quot;cm&quot;&gt; * msg and args to {@link String#format(String, Object...)}.&lt;/span&gt;
&lt;span class=&quot;cm&quot;&gt; * @param obj The object to check against null.&lt;/span&gt;
&lt;span class=&quot;cm&quot;&gt; * @param msg The format string to use as the exception message if obj is null.&lt;/span&gt;
&lt;span class=&quot;cm&quot;&gt; * @param args The arguments to msg for use if obj is null.&lt;/span&gt;
&lt;span class=&quot;cm&quot;&gt; * @return obj if it is not null.&lt;/span&gt;
&lt;span class=&quot;cm&quot;&gt; * @throws NullPointerException if obj is null.&lt;/span&gt;
&lt;span class=&quot;cm&quot;&gt; */&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;notNull&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;NullPointerException&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p class=&quot;github_sample_ref&quot;&gt;&lt;small&gt;&lt;a href=&quot;https://github.com/alexleighton/alonzo-java/blob/a9f345db226c3f434487495b58d8a24fd378f007//src/alonzo/common/Validate.java#L12-L27&quot;&gt;/src/alonzo/common/Validate.java&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;Semantic correctness is an important part of software design for me. I use strong typing because it’s a cheap (cheaper than finding them at runtime) way of discovering and preventing bugs. In keeping with this ideology, I believe in failing fast. Discovering a bug or error during the running of a program as soon as it appears makes debugging easier (due to quicker root-causing). Ensuring that objects are constructed correctly or that objects to be used abide by expectations makes writing logic around them easier.&lt;/p&gt;

&lt;p&gt;I use Validate to ensure that expected preconditions hold, or else fail immediately. Typically I will use Validate assertions to guarantee that an object is correct on construction, or else fail construction. This makes it harder to pass around broken objects, another aspect of failing fast. So far I’ve only needed to check for null. However, it is highly likely that I will be adding to this class in the future.&lt;/p&gt;
</description>
        <pubDate>Tue, 27 Oct 2015 00:00:00 -0700</pubDate>
        <link>/alonzo-java/brief/2015/10/27/validate-not-null.html</link>
        <guid isPermaLink="true">/alonzo-java/brief/2015/10/27/validate-not-null.html</guid>
        
        <category>brief</category>
        
        <category>preconditions</category>
        
        
        <category>brief</category>
        
      </item>
    
      <item>
        <title>An Intuitive Formatter</title>
        <description>&lt;div class=&quot;commit&quot;&gt;
  &lt;a href=&quot;https://github.com/alexleighton/alonzo-java/tree/1f0b04e71923642f3b8d654390591970c7632d57&quot;&gt;
    This post is based on code found in &lt;code&gt;1f0b04e7192&lt;/code&gt;
  &lt;/a&gt;
&lt;/div&gt;

&lt;p&gt;One of the first things I do when I define the syntax for a new language is implement a formatter — code to produce a textual representation of expressions in the language. Humans are intensely visual, so having a way to look at the structures you are constructing in code is a great way of aiding understanding. Further, since this a programming language we’re creating, the ability to print expressions is necessary for the interpreter. If you haven’t read &lt;a href=&quot;/alonzo-java/lambda-calculus/2015/10/23/lambda-calculus-intro.html&quot;&gt;yesterday’s post&lt;/a&gt; and are unfamiliar with the lambda calculus, I recommend reading it before proceeding.&lt;/p&gt;

&lt;h2 id=&quot;formatting&quot;&gt;Formatting&lt;/h2&gt;

&lt;p&gt;The interface our formatter needs to implement is as follows: given an &lt;a href=&quot;https://docs.oracle.com/javase/8/docs/api/java/io/OutputStream.html&quot;&gt;&lt;code&gt;OutputStream&lt;/code&gt;&lt;/a&gt;, output a string representation of the given &lt;a href=&quot;https://github.com/alexleighton/alonzo-java/blob/1f0b04e71923642f3b8d654390591970c7632d57/src/alonzo/ast/ASTNode.java&quot;&gt;&lt;code&gt;ASTNode&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;cm&quot;&gt;/**&lt;/span&gt;
&lt;span class=&quot;cm&quot;&gt; * Format the given {@code ASTNode}, outputting to the given {@code OutputStream}. All character&lt;/span&gt;
&lt;span class=&quot;cm&quot;&gt; * data is formatted to {@code out} using the {@link Charset} this object was constructed with.&lt;/span&gt;
&lt;span class=&quot;cm&quot;&gt; * @param out  The {@code OutputStream} to format {@code root} to.&lt;/span&gt;
&lt;span class=&quot;cm&quot;&gt; * @param root The {@code ASTNode} to format.&lt;/span&gt;
&lt;span class=&quot;cm&quot;&gt; * @throws IOException if {@code out} had an issue being written to.&lt;/span&gt;
&lt;span class=&quot;cm&quot;&gt; */&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;abstract&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OutputStream&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ASTNode&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;root&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IOException&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p class=&quot;github_sample_ref&quot;&gt;&lt;small&gt;&lt;a href=&quot;https://github.com/alexleighton/alonzo-java/blob/1f0b04e71923642f3b8d654390591970c7632d57//src/alonzo/ast/fmt/Formatter.java#L26-L33&quot;&gt;/src/alonzo/ast/fmt/Formatter.java&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;

&lt;h2 id=&quot;formatting-var&quot;&gt;Formatting Var&lt;/h2&gt;

&lt;p&gt;Remember that the definition of a variable (&lt;a href=&quot;https://github.com/alexleighton/alonzo-java/blob/1f0b04e71923642f3b8d654390591970c7632d57/src/alonzo/ast/Var.java&quot;&gt;&lt;code&gt;Var&lt;/code&gt;&lt;/a&gt;) is a sequence of characters, not including the punctuation used for other expression types:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;var&amp;gt; ::= [^()λ. ]+
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The test for our formatter is likewise simple. When we have a variable containing a name, the string representation should just be the name. &lt;small&gt;For now, ignore that we’re testing 2 different formatters here, this post only concerns the recursive formatter.&lt;/small&gt;&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;nd&quot;&gt;@Test&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fmtVar&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Var&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;testVariableName&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;assertEquals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;testVariableName&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;iterFmt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;assertEquals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;testVariableName&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;recFmt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p class=&quot;github_sample_ref&quot;&gt;&lt;small&gt;&lt;a href=&quot;https://github.com/alexleighton/alonzo-java/blob/1f0b04e71923642f3b8d654390591970c7632d57//tst/alonzo/ast/fmt/FormatterTest.java#L19-L25&quot;&gt;/tst/alonzo/ast/fmt/FormatterTest.java&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;Given the quite simple definition of &lt;code&gt;Var&lt;/code&gt;, formatting it is a one-liner:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;root&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;instanceof&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Var&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Var&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;root&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()));&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p class=&quot;github_sample_ref&quot;&gt;&lt;small&gt;&lt;a href=&quot;https://github.com/alexleighton/alonzo-java/blob/1f0b04e71923642f3b8d654390591970c7632d57//src/alonzo/ast/fmt/RecursiveFormatter.java#L27-L29&quot;&gt;/src/alonzo/ast/fmt/RecursiveFormatter.java&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;

&lt;h2 id=&quot;formatting-fun&quot;&gt;Formatting Fun&lt;/h2&gt;

&lt;p&gt;Formatting functions is a bit more tricky than formatting variables. Notice the recursive definition of a function as being an expression that contains another expression:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;function-definition&amp;gt; ::= &#39;(λ&#39; &amp;lt;var&amp;gt; &#39;.&#39; &amp;lt;expr&amp;gt; &#39;)&#39;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;But first, the test. Straightforward, as usual:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;nd&quot;&gt;@Test&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fmtFun&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Fun&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Fun&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Var&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;var&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Var&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;body&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;assertEquals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;(λvar.body)&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;iterFmt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;assertEquals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;(λvar.body)&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;recFmt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p class=&quot;github_sample_ref&quot;&gt;&lt;small&gt;&lt;a href=&quot;https://github.com/alexleighton/alonzo-java/blob/1f0b04e71923642f3b8d654390591970c7632d57//tst/alonzo/ast/fmt/FormatterTest.java#L27-L33&quot;&gt;/tst/alonzo/ast/fmt/FormatterTest.java&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;The recursive definition of &lt;a href=&quot;https://github.com/alexleighton/alonzo-java/blob/1f0b04e71923642f3b8d654390591970c7632d57/src/alonzo/ast/Fun.java&quot;&gt;&lt;code&gt;Fun&lt;/code&gt;&lt;/a&gt; makes defining the formatter in a recursive manner a really natural and intuitive choice. So, to format a function, we output the punctuation of the function, then recursively call format on the substructures: the parameter and the body of the function:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;formatFun&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OutputStream&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Fun&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fun&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IOException&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;(λ&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fun&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;.&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fun&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;)&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p class=&quot;github_sample_ref&quot;&gt;&lt;small&gt;&lt;a href=&quot;https://github.com/alexleighton/alonzo-java/blob/1f0b04e71923642f3b8d654390591970c7632d57//src/alonzo/ast/fmt/RecursiveFormatter.java#L40-L46&quot;&gt;/src/alonzo/ast/fmt/RecursiveFormatter.java&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;

&lt;h2 id=&quot;formatting-app&quot;&gt;Formatting App&lt;/h2&gt;

&lt;p&gt;Function application (&lt;a href=&quot;https://github.com/alexleighton/alonzo-java/blob/1f0b04e71923642f3b8d654390591970c7632d57/src/alonzo/ast/App.java&quot;&gt;&lt;code&gt;App&lt;/code&gt;&lt;/a&gt;) is formatted in much the same way function was. Again, &lt;code&gt;App&lt;/code&gt; is recursively defined as being composed of other expressions:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;function-application&amp;gt; ::= &#39;(&#39; &amp;lt;expr&amp;gt; &#39; &#39; &amp;lt;expr&amp;gt; &#39;)&#39;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The test for formatting &lt;code&gt;App&lt;/code&gt; makes use of all the other &lt;code&gt;ASTNode&lt;/code&gt;s, asking the formatter to format an application of the identity function to a free variable &lt;code&gt;y&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;nd&quot;&gt;@Test&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fmtApp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;App&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;App&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Fun&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Var&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;x&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Var&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;x&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)),&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Var&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;y&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;assertEquals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;((λx.x) y)&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;iterFmt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;assertEquals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;((λx.x) y)&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;recFmt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p class=&quot;github_sample_ref&quot;&gt;&lt;small&gt;&lt;a href=&quot;https://github.com/alexleighton/alonzo-java/blob/1f0b04e71923642f3b8d654390591970c7632d57//tst/alonzo/ast/fmt/FormatterTest.java#L35-L41&quot;&gt;/tst/alonzo/ast/fmt/FormatterTest.java&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;The implementation is very similar to &lt;code&gt;Fun&lt;/code&gt;, outputting the relevant punctuation, then recursively calling format on the function and argument substructures.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;formatApp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;OutputStream&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;App&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IOException&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;(&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot; &amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;argument&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bytes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;)&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p class=&quot;github_sample_ref&quot;&gt;&lt;small&gt;&lt;a href=&quot;https://github.com/alexleighton/alonzo-java/blob/1f0b04e71923642f3b8d654390591970c7632d57//src/alonzo/ast/fmt/RecursiveFormatter.java#L48-L54&quot;&gt;/src/alonzo/ast/fmt/RecursiveFormatter.java&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;

&lt;h2 id=&quot;putting-it-all-together&quot;&gt;Putting It All Together&lt;/h2&gt;

&lt;p&gt;With all the pieces of &lt;a href=&quot;https://github.com/alexleighton/alonzo-java/blob/1f0b04e71923642f3b8d654390591970c7632d57//src/alonzo/ast/fmt/RecursiveFormatter.java&quot;&gt;&lt;code&gt;RecursiveFormatter&lt;/code&gt;&lt;/a&gt; together, we can format arbitrarily complicated lambda calculus expressions. I’ve added a “complicated” test to the suite showing that our formatter can format the &lt;a href=&quot;https://en.wikipedia.org/wiki/Fixed-point_combinator#Fixed_point_combinators_in_lambda_calculus&quot;&gt;Y-combinator&lt;/a&gt;:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;nd&quot;&gt;@Test&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fmtComplicated&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Fun&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sub&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Fun&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Var&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;x&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;App&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Var&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;f&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;App&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Var&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;x&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Var&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;x&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))));&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Fun&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ycombinator&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Fun&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Var&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;f&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;App&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sub&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sub&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;assertEquals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;(λf.((λx.(f (x x))) (λx.(f (x x)))))&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;iterFmt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ycombinator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;assertEquals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;(λf.((λx.(f (x x))) (λx.(f (x x)))))&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;recFmt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ycombinator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p class=&quot;github_sample_ref&quot;&gt;&lt;small&gt;&lt;a href=&quot;https://github.com/alexleighton/alonzo-java/blob/1f0b04e71923642f3b8d654390591970c7632d57//tst/alonzo/ast/fmt/FormatterTest.java#L43-L50&quot;&gt;/tst/alonzo/ast/fmt/FormatterTest.java&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Next time on Alonzo&lt;/strong&gt;&lt;/em&gt;: they taught you not to use recursion in Java; what to do when your stack gets blown?&lt;br /&gt;
Stay tuned to find out.&lt;/p&gt;
</description>
        <pubDate>Sat, 24 Oct 2015 00:00:00 -0700</pubDate>
        <link>/alonzo-java/formatting/2015/10/24/intuitive-formatting.html</link>
        <guid isPermaLink="true">/alonzo-java/formatting/2015/10/24/intuitive-formatting.html</guid>
        
        <category>lambda-calculus</category>
        
        <category>syntax</category>
        
        <category>formatting</category>
        
        
        <category>formatting</category>
        
      </item>
    
      <item>
        <title>Introduction to the Lambda Calculus</title>
        <description>&lt;div class=&quot;commit&quot;&gt;
  &lt;a href=&quot;https://github.com/alexleighton/alonzo-java/tree/989223b15bb40f8a7461019a31469752a11ea9bf&quot;&gt;
    This post is based on code found in &lt;code&gt;989223b15bb&lt;/code&gt;
  &lt;/a&gt;
&lt;/div&gt;

&lt;p&gt;The &lt;a href=&quot;https://en.wikipedia.org/wiki/Lambda_calculus&quot;&gt;Lambda Calculus&lt;/a&gt; (λ-calculus) is a theoretical programming language originally used as part of an investigation into the foundations of mathematics. &lt;a href=&quot;https://en.wikipedia.org/wiki/Alonzo_Church&quot;&gt;Alonzo Church&lt;/a&gt; (the namesake of this project) later published a paper on just the computational part of the λ-calculus. Our project here is to implement a full-featured interpreter for this simple programming language.&lt;/p&gt;

&lt;h2 id=&quot;syntax&quot;&gt;Syntax&lt;/h2&gt;

&lt;p&gt;The λ-calculus has a very simple syntax, making it a great first programming language to implement. There are only 3 forms in the expression-based syntax:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;expr&amp;gt; ::= &amp;lt;var&amp;gt; | &amp;lt;function-definition&amp;gt; | &amp;lt;function-application&amp;gt;

&amp;lt;function-definition&amp;gt; ::= &#39;(λ&#39; &amp;lt;var&amp;gt; &#39;.&#39; &amp;lt;expr&amp;gt; &#39;)&#39;

&amp;lt;function-application&amp;gt; ::= &#39;(&#39; &amp;lt;expr&amp;gt; &#39; &#39; &amp;lt;expr&amp;gt; &#39;)&#39;

&amp;lt;var&amp;gt; ::= [^()λ. ]+
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This basic &lt;a href=&quot;https://en.wikipedia.org/wiki/Backus%E2%80%93Naur_Form&quot;&gt;BNF&lt;/a&gt; description of the syntax can be read as an expression is defined as the following 3 forms. First, an expression can be a &lt;code&gt;&amp;lt;var&amp;gt;&lt;/code&gt;, which is defined as a series of characters not including any of &lt;code&gt;&#39;(&#39;&lt;/code&gt;, &lt;code&gt;&#39;)&#39;&lt;/code&gt;, &lt;code&gt;&#39;λ&#39;&lt;/code&gt;, &lt;code&gt;&#39;.&#39;&lt;/code&gt;, or &lt;code&gt;&#39; &#39;&lt;/code&gt;. Second, an expression can be a &lt;code&gt;&amp;lt;function-definition&amp;gt;&lt;/code&gt;, which consists of the characters &lt;code&gt;&#39;(λ&#39;&lt;/code&gt;, followed by a &lt;code&gt;&amp;lt;var&amp;gt;&lt;/code&gt;, followed by the character &lt;code&gt;&#39;.&#39;&lt;/code&gt;, followed by an &lt;code&gt;&amp;lt;expr&amp;gt;&lt;/code&gt;, followed by the character &lt;code&gt;&#39;)&#39;&lt;/code&gt;. Lastly, an expression can be a &lt;code&gt;&amp;lt;function-application&amp;gt;&lt;/code&gt;, which consists of the character &lt;code&gt;&#39;(&#39;&lt;/code&gt;, followed by an &lt;code&gt;&amp;lt;expr&amp;gt;&lt;/code&gt;, followed by the character &lt;code&gt;&#39; &#39;&lt;/code&gt;, followed by an &lt;code&gt;&amp;lt;expr&amp;gt;&lt;/code&gt;, followed by the character &lt;code&gt;&#39;)&#39;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This definition comprises the entirety of the syntax of the language. When we get to defining the parser for this language, we will very likely extend this definition to include whitespace between many of the syntax pieces, as extra whitespace enhances readability for humans. For now though, this definition will suffice for giving you a feeling for the different forms the language can take.&lt;/p&gt;

&lt;h3 id=&quot;variable-reference&quot;&gt;Variable Reference&lt;/h3&gt;

&lt;p&gt;A λ-calculus variable reference is similar in nature to the variable references you are used to in more mainstream programming languages. Like Java for instance:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;varName&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Value = &amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;varName&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In the code example above, we see the assignment of the value &lt;code&gt;5&lt;/code&gt; to the variable &lt;code&gt;varName&lt;/code&gt;, and then a reference to the variable on the next line where &lt;code&gt;varName&lt;/code&gt; is concatenated to the string &lt;code&gt;&quot;Value = &quot;&lt;/code&gt;. Like the second line in the Java example, a variable reference syntax form indicates a reference to a variable that was defined “previously” in the λ-calculus program.&lt;/p&gt;

&lt;p&gt;We represent a λ-calculus variable like so:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;cm&quot;&gt;/**&lt;/span&gt;
&lt;span class=&quot;cm&quot;&gt; * Variable reference. An {@link ASTNode} representing a referenced variable.&lt;/span&gt;
&lt;span class=&quot;cm&quot;&gt; */&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Var&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ASTNode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Var&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p class=&quot;github_sample_ref&quot;&gt;&lt;small&gt;&lt;a href=&quot;https://github.com/alexleighton/alonzo-java/blob/989223b15bb40f8a7461019a31469752a11ea9bf//src/alonzo/ast/Var.java#L3-L18&quot;&gt;/src/alonzo/ast/Var.java&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;Our &lt;code&gt;Var&lt;/code&gt; has a container for the textual name of the variable, and a way of accessing that name. A key implementation detail here is that I’ve made &lt;code&gt;Var&lt;/code&gt; an immutable object, in order to allow referencing the object anywhere (even appearing multiple times in the same tree).&lt;/p&gt;

&lt;h3 id=&quot;function-definition&quot;&gt;Function Definition&lt;/h3&gt;

&lt;p&gt;The Lambda Calculus gets its name from the lambda (λ) symbol in the definition of a function. This is also where many mainstream functions get the name &lt;code&gt;lambda&lt;/code&gt; for referring to an anonymous-function-as-a-value. Functions in the λ-calculus are a simplified version of the functions you are used to in other languages. These functions only have one parameter, with the body of the function being another expression. Simplicity is the reason for the season with the λ-calculus.&lt;/p&gt;

&lt;p&gt;Multi-parameter functions can be simulated: &lt;code&gt;int foo(int x, int y, int z) { return z; }&lt;/code&gt; becomes &lt;code&gt;(λx.(λy.(λz.z)))&lt;/code&gt; in the λ-calculus. As can be seen in the example, λ-calculus functions are anonymous (not named), and a function of three parameters is instead a function of one parameter, returning a function of one parameter, returning a function of one parameter, returning the final result.&lt;/p&gt;

&lt;p&gt;We represent a λ-calculus function as an immutable container for the &lt;code&gt;Var&lt;/code&gt; parameter, and the &lt;code&gt;ASTNode&lt;/code&gt; body:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;cm&quot;&gt;/**&lt;/span&gt;
&lt;span class=&quot;cm&quot;&gt; * Function definition. An {@link ASTNode} representing the definition of a function with one&lt;/span&gt;
&lt;span class=&quot;cm&quot;&gt; * parameter.&lt;/span&gt;
&lt;span class=&quot;cm&quot;&gt; */&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Fun&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ASTNode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ASTNode&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Fun&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ASTNode&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;parameter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;body&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Var&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ASTNode&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p class=&quot;github_sample_ref&quot;&gt;&lt;small&gt;&lt;a href=&quot;https://github.com/alexleighton/alonzo-java/blob/989223b15bb40f8a7461019a31469752a11ea9bf//src/alonzo/ast/Fun.java#L3-L24&quot;&gt;/src/alonzo/ast/Fun.java&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;

&lt;h3 id=&quot;function-application&quot;&gt;Function Application&lt;/h3&gt;

&lt;p&gt;Function application is the third form a λ-calculus expression can take. It represents the application of a function to one argument. In Java this looks like &lt;code&gt;someFunction(someArgument)&lt;/code&gt;. In the λ-calculus it looks like &lt;code&gt;(someFunction someArgument)&lt;/code&gt;. For the sake of removing ambiguity, the application is parenthesized, as “&lt;code&gt;expr1 expr2 expr3&lt;/code&gt;” could mean &lt;code&gt;expr3&lt;/code&gt; is the argument to &lt;code&gt;expr2&lt;/code&gt; and the result is the argument to &lt;code&gt;expr1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We represent a λ-calculus function application as an immutable container for the &lt;code&gt;ASTNode&lt;/code&gt; function, and the &lt;code&gt;ASTNode&lt;/code&gt; argument:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;cm&quot;&gt;/**&lt;/span&gt;
&lt;span class=&quot;cm&quot;&gt; * Function application. An {@link ASTNode} representing the application of a function to one&lt;/span&gt;
&lt;span class=&quot;cm&quot;&gt; * argument.&lt;/span&gt;
&lt;span class=&quot;cm&quot;&gt; */&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;App&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ASTNode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ASTNode&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ASTNode&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;argument&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;App&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ASTNode&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ASTNode&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;argument&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;argument&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;argument&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ASTNode&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ASTNode&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;argument&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;argument&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p class=&quot;github_sample_ref&quot;&gt;&lt;small&gt;&lt;a href=&quot;https://github.com/alexleighton/alonzo-java/blob/989223b15bb40f8a7461019a31469752a11ea9bf//src/alonzo/ast/App.java#L3-L25&quot;&gt;/src/alonzo/ast/App.java&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;The reason both the left and right sides of the application are &lt;code&gt;ASTNode&lt;/code&gt;s is that, due to the semantics of the language, the left side may only become a function through runtime interpretation of the program.&lt;/p&gt;

&lt;h2 id=&quot;semantics&quot;&gt;Semantics&lt;/h2&gt;

&lt;p&gt;Computation in the λ-calculus is achieved through variable substitution and structural interpretation of the resulting functions. What I mean here is that computation is performed by substituting expressions for variables where possible until there are no more substitutions possible, and then inspecting the resulting expression structure to get an “answer”. We’ll talk more about the semantics of the language once it’s time to implement evaluation. For now, here’s an example program and the result of evaluating it:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;((λn.(λf.(λx.(n ((n f) x))))) (λf.(λx.x)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;evaluates to:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(λf.(λx.(f x)))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here, we see the “successor” function &lt;code&gt;(λn.(λf.(λx.(n ((n f) x)))))&lt;/code&gt; given the argument &lt;code&gt;(λf.(λx.x))&lt;/code&gt;, the number 0. The result &lt;code&gt;(λf.(λx.(f x)))&lt;/code&gt; is the number 1. Don’t stress out too much on how these particular representations of &lt;code&gt;+1&lt;/code&gt; and &lt;code&gt;0&lt;/code&gt; were picked, just learn from this as a real-world example of a small λ-calculus program. We will cover these representations and evaluation strategies later. Suffice it to say that we take the &lt;code&gt;0&lt;/code&gt; function and substitute it in the &lt;code&gt;+1&lt;/code&gt; function everywhere the parameter &lt;code&gt;n&lt;/code&gt; is, and continue substituting until we get &lt;code&gt;1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To be continued…&lt;/p&gt;
</description>
        <pubDate>Fri, 23 Oct 2015 00:00:00 -0700</pubDate>
        <link>/alonzo-java/lambda-calculus/2015/10/23/lambda-calculus-intro.html</link>
        <guid isPermaLink="true">/alonzo-java/lambda-calculus/2015/10/23/lambda-calculus-intro.html</guid>
        
        <category>lambda-calculus</category>
        
        <category>syntax</category>
        
        
        <category>lambda-calculus</category>
        
      </item>
    
      <item>
        <title>Ignoring Tests</title>
        <description>&lt;div class=&quot;commit&quot;&gt;
  &lt;a href=&quot;https://github.com/alexleighton/alonzo-java/tree/fd9028f3c3841be02fa6d4b225d670a41cae8268&quot;&gt;
    This post is based on code found in &lt;code&gt;fd9028f3c38&lt;/code&gt;
  &lt;/a&gt;
&lt;/div&gt;

&lt;p&gt;In the course of making a formatter for our lambda calculus terms, I stumbled on a situation where the ability to ignore a test was needed. Let’s see how to add “ignored” tests to our testing framework.&lt;/p&gt;

&lt;p&gt;Similar to JUnit, I’ve added an &lt;a href=&quot;https://github.com/alexleighton/alonzo-java/blob/fd9028f3c3841be02fa6d4b225d670a41cae8268/tst/alonzo/unit/Ignore.java&quot;&gt;&lt;code&gt;@Ignore&lt;/code&gt;&lt;/a&gt; annotation for marking tests as ignored. Next, we need to a new kind of TestResult:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;cm&quot;&gt;/**&lt;/span&gt;
&lt;span class=&quot;cm&quot;&gt; * Objects representing an ignored test result for given tests.&lt;/span&gt;
&lt;span class=&quot;cm&quot;&gt; */&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Ignored&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TestResult&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Ignored&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;testName&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kd&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;testName&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p class=&quot;github_sample_ref&quot;&gt;&lt;small&gt;&lt;a href=&quot;https://github.com/alexleighton/alonzo-java/blob/fd9028f3c3841be02fa6d4b225d670a41cae8268//tst/alonzo/unit/TestResult.java#L145-L152&quot;&gt;/tst/alonzo/unit/TestResult.java&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/alexleighton/alonzo-java/blob/fd9028f3c3841be02fa6d4b225d670a41cae8268/tst/alonzo/unit/RunnableTest.java&quot;&gt;&lt;code&gt;RunnableTest&lt;/code&gt;&lt;/a&gt; needs to be changed in order to understand ignored tests. When making the change, I chose to not construct the test class instance or run the test method, opting for an early return with no work done. Further, as this addition made &lt;a href=&quot;https://github.com/alexleighton/alonzo-java/blob/fd9028f3c3841be02fa6d4b225d670a41cae8268/tst/alonzo/unit/RunnableTest.java#L66&quot;&gt;&lt;code&gt;RunnableTest.get()&lt;/code&gt;&lt;/a&gt; too large for my taste, I refactored construction of the test class instance into a method.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;isIgnoredTest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TestResult&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;ignore&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;testName&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Object&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;instance&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;constructTestInstance&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p class=&quot;github_sample_ref&quot;&gt;&lt;small&gt;&lt;a href=&quot;https://github.com/alexleighton/alonzo-java/blob/fd9028f3c3841be02fa6d4b225d670a41cae8268//tst/alonzo/unit/RunnableTest.java#L67-L71&quot;&gt;/tst/alonzo/unit/RunnableTest.java&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;isIgnoredTest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getAnnotation&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Ignore&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p class=&quot;github_sample_ref&quot;&gt;&lt;small&gt;&lt;a href=&quot;https://github.com/alexleighton/alonzo-java/blob/fd9028f3c3841be02fa6d4b225d670a41cae8268//tst/alonzo/unit/RunnableTest.java#L89-L91&quot;&gt;/tst/alonzo/unit/RunnableTest.java&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;Finally, at the top level of &lt;a href=&quot;https://github.com/alexleighton/alonzo-java/blob/fd9028f3c3841be02fa6d4b225d670a41cae8268/tst/alonzo/unit/TestRunner.java#L35&quot;&gt;&lt;code&gt;TestRunner&lt;/code&gt;&lt;/a&gt;, all that’s needed is a simple collection of ignored test results and the addition of an ignored section in the overall result output.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TestResult&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ignored&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;results&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;isIgnored&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Collectors&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;toList&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p class=&quot;github_sample_ref&quot;&gt;&lt;small&gt;&lt;a href=&quot;https://github.com/alexleighton/alonzo-java/blob/fd9028f3c3841be02fa6d4b225d670a41cae8268//tst/alonzo/unit/TestRunner.java#L46-L47&quot;&gt;/tst/alonzo/unit/TestRunner.java&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;A good test of any software system is how easy it is to adjust the system incrementally. I’m pleased with how easy it was to adjust our testing framework in this way.&lt;/p&gt;

</description>
        <pubDate>Sun, 26 Jul 2015 00:00:00 -0700</pubDate>
        <link>/alonzo-java/testing/2015/07/26/ignoring-tests.html</link>
        <guid isPermaLink="true">/alonzo-java/testing/2015/07/26/ignoring-tests.html</guid>
        
        <category>unit</category>
        
        <category>testing</category>
        
        
        <category>testing</category>
        
      </item>
    
      <item>
        <title>Unit Testing Framework Foundations</title>
        <description>&lt;div class=&quot;commit&quot;&gt;
  &lt;a href=&quot;https://github.com/alexleighton/alonzo-java/tree/add2fe75a054079bad27765233a1bd566d7a3ebe&quot;&gt;
    This post is based on code found in &lt;code&gt;add2fe75a05&lt;/code&gt;
  &lt;/a&gt;
&lt;/div&gt;

&lt;h2 id=&quot;testing&quot;&gt;Testing&lt;/h2&gt;

&lt;p&gt;I’m a big fan of automated testing, for many reasons. It provides the easiest and most reliable way to see if your code is working, aside from strong static typing. Further, in my experience, driving interface design from the perspective of making it easy to test tends to make for good interfaces.&lt;/p&gt;

&lt;p&gt;In order to test our interpreter, it will be helpful to have a testing framework which will automate the process of running tests, and collecting and presenting test results. This isn’t strictly necessary, as we could just ad-hoc design our tests, with a &lt;code&gt;println&lt;/code&gt; indicating success here and a &lt;code&gt;resultCode&lt;/code&gt; indicating failure there. I think we’ll have more confidence in our results if they’re all delivered the same way, through the same testing mechanisms.&lt;/p&gt;

&lt;p&gt;Now if you’ve ever designed a testing framework, you’ll have quickly realized that like every other piece of software, having automated tests to ensure your testing framework still works is important. But we’ve got a bit of a quandary. Testing your testing framework seems valuable, but also like we’re getting into a “turtles all the way down” situation. At some point, you have to trust that the code you’re writing is correct. In our case, I started with a dumb test to be driven by the framework, essentially testing correctness of the testing framework manually.&lt;/p&gt;

&lt;p&gt;The first test:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;FooTest&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;nd&quot;&gt;@Test&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;firstTest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;assertEquals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Foo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;bar&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p class=&quot;github_sample_ref&quot;&gt;&lt;small&gt;&lt;a href=&quot;https://github.com/alexleighton/alonzo-java/blob/add2fe75a054079bad27765233a1bd566d7a3ebe//tst/alonzo/FooTest.java#L6-L12&quot;&gt;/tst/alonzo/FooTest.java&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;The call to the test runner:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;TestRunner&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;withTest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FooTest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p class=&quot;github_sample_ref&quot;&gt;&lt;small&gt;&lt;a href=&quot;https://github.com/alexleighton/alonzo-java/blob/add2fe75a054079bad27765233a1bd566d7a3ebe//tst/alonzo/TestingMain.java#L7-L11&quot;&gt;/tst/alonzo/TestingMain.java&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;I started with these two classes before writing any of the testing framework code. In general, I believe in the value of test-driven development. Without going too much into TDD and its positives and negatives, here’s a benefit I’ve found from practicing TDD. Driving the code from a test usually results in a better, more usable interface. So, in this instance, I wrote the test the way I wanted to write a test, and wrote the code to run the test in a way that seemed easy to run tests. Honestly, this top-down thinking is one of the hardest things for new developers to really grok and practice. It is key in choosing your abstractions well and in writing interfaces that you actually would want to use.&lt;/p&gt;

&lt;p&gt;So far, the architecture of the testing framework is relatively simple. The &lt;a href=&quot;https://github.com/alexleighton/alonzo-java/blob/add2fe75a054079bad27765233a1bd566d7a3ebe/tst/alonzo/unit/TestRunner.java&quot;&gt;&lt;code&gt;TestRunner&lt;/code&gt;&lt;/a&gt; collects tests, then runs them, collecting the &lt;a href=&quot;https://github.com/alexleighton/alonzo-java/blob/add2fe75a054079bad27765233a1bd566d7a3ebe/tst/alonzo/unit/TestResult.java&quot;&gt;&lt;code&gt;TestResults&lt;/code&gt;&lt;/a&gt;, and finally displaying those results. In order to run the tests, the &lt;code&gt;TestRunner&lt;/code&gt; makes use of the &lt;a href=&quot;https://github.com/alexleighton/alonzo-java/blob/add2fe75a054079bad27765233a1bd566d7a3ebe/tst/alonzo/unit/RunnableTest.java&quot;&gt;&lt;code&gt;RunnableTest&lt;/code&gt;&lt;/a&gt; class, which does all the heavy lifting. It searches the class for any methods labeled with the &lt;a href=&quot;https://github.com/alexleighton/alonzo-java/blob/add2fe75a054079bad27765233a1bd566d7a3ebe/tst/alonzo/unit/Test.java&quot;&gt;&lt;code&gt;@Test&lt;/code&gt;&lt;/a&gt; annotation, and the constructor. &lt;code&gt;RunnableTest&lt;/code&gt; implements the &lt;a href=&quot;https://docs.oracle.com/javase/8/docs/api/java/util/function/Supplier.html&quot;&gt;&lt;code&gt;Supplier&amp;lt;TestResult&amp;gt;&lt;/code&gt;&lt;/a&gt; interface, by running the wrapped test and constructing a result. &lt;code&gt;RunnableTest&lt;/code&gt; works in concert with the static methods in &lt;a href=&quot;https://github.com/alexleighton/alonzo-java/blob/add2fe75a054079bad27765233a1bd566d7a3ebe/tst/alonzo/unit/Assert.java&quot;&gt;&lt;code&gt;Assert&lt;/code&gt;&lt;/a&gt; which throw &lt;a href=&quot;https://github.com/alexleighton/alonzo-java/blob/add2fe75a054079bad27765233a1bd566d7a3ebe/tst/alonzo/unit/AssertionFailureException.java&quot;&gt;&lt;code&gt;AssertionFailureException&lt;/code&gt;&lt;/a&gt; to indicate a test failure.&lt;/p&gt;

&lt;p&gt;Now we have a testing framework with which to test the code we write for the interpreter.&lt;/p&gt;
</description>
        <pubDate>Sun, 19 Jul 2015 00:00:00 -0700</pubDate>
        <link>/alonzo-java/testing/2015/07/19/unit-testing.html</link>
        <guid isPermaLink="true">/alonzo-java/testing/2015/07/19/unit-testing.html</guid>
        
        <category>unit</category>
        
        <category>testing</category>
        
        
        <category>testing</category>
        
      </item>
    
      <item>
        <title>Welcome &amp; Introduction</title>
        <description>&lt;h2 id=&quot;welcome&quot;&gt;Welcome&lt;/h2&gt;

&lt;p&gt;On this blog you will find posts that discuss the project. I will be detailing the design choices, highlighting interesting code, and generally helping anyone interested in the Lambda Calculus understand the language and how to write an interpreter for it.&lt;/p&gt;

&lt;p&gt;We’ll be starting from scratch with this project. Partly to satisfy anyone’s curiosity about &lt;em&gt;all&lt;/em&gt; of the pieces, and partly because I detest the Java package managers I’ve run across. So, all bets are off — if we need it, we’ll build it.&lt;/p&gt;

&lt;p&gt;Here are the things I’m taking for granted:&lt;/p&gt;

&lt;h2 id=&quot;program-dependencies&quot;&gt;Program Dependencies&lt;/h2&gt;

&lt;h3 id=&quot;make&quot;&gt;Make&lt;/h3&gt;

&lt;p&gt;I am using the &lt;em&gt;old-as-the-hills&lt;/em&gt; Make for coordinating the building, testing, and cleaning of the project. Yes, I agree, there are better build tools out there. However, I think Make wins for simplicity and ubiquity. Not using a Java package manager means that all code will be in source rather than compiled format, so linking dependencies together can be left to javac.&lt;/p&gt;

&lt;h3 id=&quot;java-8&quot;&gt;Java 8&lt;/h3&gt;

&lt;p&gt;I am using the newest version of Java and the JDK because I want to get more familiar with the new semantics and libraries in Java 8 (yay first-class functions!). My other motivation for using Java is to show what it looks like to implement a functional language (the Lambda Calculus) in an imperative language. Often you’ll see folks implement a toy functional language in another functional language. Some people find this to be cheating in a way, as certain functional languages (OCaml, Haskell) seem better suited to constructing compilers and interpreters.&lt;/p&gt;
</description>
        <pubDate>Sun, 12 Jul 2015 00:00:00 -0700</pubDate>
        <link>/alonzo-java/misc/2015/07/12/welcome.html</link>
        <guid isPermaLink="true">/alonzo-java/misc/2015/07/12/welcome.html</guid>
        
        <category>introduction</category>
        
        
        <category>misc</category>
        
      </item>
    
  </channel>
</rss>

