<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Luca Bolognese&#039;s Blog</title>
	<atom:link href="http://lucabolognese.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://lucabolognese.wordpress.com</link>
	<description>x += x++;</description>
	<lastBuildDate>Sat, 12 Nov 2011 22:18:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='lucabolognese.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Luca Bolognese&#039;s Blog</title>
		<link>http://lucabolognese.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://lucabolognese.wordpress.com/osd.xml" title="Luca Bolognese&#039;s Blog" />
	<atom:link rel='hub' href='http://lucabolognese.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Adventure in parserland &#8211; parsing lambda expressions in F# &#8211; Part V</title>
		<link>http://lucabolognese.wordpress.com/2011/09/16/adventure-in-parserland-parsing-lambda-expressions-in-f-part-v/</link>
		<comments>http://lucabolognese.wordpress.com/2011/09/16/adventure-in-parserland-parsing-lambda-expressions-in-f-part-v/#comments</comments>
		<pubDate>Fri, 16 Sep 2011 06:57:00 +0000</pubDate>
		<dc:creator>lucabol</dc:creator>
				<category><![CDATA[F#]]></category>

		<guid isPermaLink="false">https://lucabolognese.wordpress.com/2011/09/16/adventure-in-parserland-parsing-lambda-expressions-in-f-part-v/</guid>
		<description><![CDATA[We are now going to look at a solution which is concise, efficient and gives sophisticated error messages. It is also less than 20 lines of code. We’ll be using FParsec. FParsec is a port of an Haskell library. It is a parser combinator library or, as I like to think of it, an internal [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lucabolognese.wordpress.com&amp;blog=11987081&amp;post=73&amp;subd=lucabolognese&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>We are now going to look at a solution which is concise, efficient and gives sophisticated error messages. It is also less than 20 lines of code. We’ll be using <a href="http://www.quanttec.com/fparsec/">FParsec</a>.</p>
<p>FParsec is a port of an Haskell library. It is a parser combinator library or, as I like to think of it, an internal <a href="http://www.google.co.uk/url?sa=t&amp;source=web&amp;cd=1&amp;ved=0CC0QFjAA&amp;url=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FDomain-specific_language&amp;ei=2uInTsvgG4LDhAfxrIzvCQ&amp;usg=AFQjCNFeEXnlId4QmC-faKR_2dF1paxZMA&amp;sig2=aCu7doIcVL0yFVlg3uA-pA">DSL</a> to build parsers in F#. My usual disclaimer: I’m not an expert in FParsec. It is likely that, if you are an expert, you can come up with more maintainable/efficient/elegant version of this parser.</p>
<p>The whole code is below:</p>
<pre class="code"><span style="color:blue;">let </span>ws = <span style="color:maroon;">&quot; \t\n&quot;
</span><span style="color:blue;">let </span>specialChars = <span style="color:maroon;">&quot;.)(\\\n&quot;

</span><span style="color:blue;">let </span>pWs = spaces
<span style="color:blue;">let </span>pName = manyChars (noneOf (ws + specialChars)) |&gt;&gt; EName

<span style="color:blue;">let </span>pExpr, pExprRef = createParserForwardedToRef&lt;Expression, Unit&gt;()

<span style="color:blue;">let </span>curry2 f a b = f(a,b)
<span style="color:blue;">let </span>pFunction = pchar <span style="color:maroon;">'\\' </span>&gt;&gt;. pipe2 pName (pchar <span style="color:maroon;">'.' </span>&gt;&gt;. pExpr) (curry2 Function)

<span style="color:blue;">let </span>pApplication = pchar <span style="color:maroon;">'(' </span>&gt;&gt;. pipe2 pExpr (pWs &gt;&gt;. pExpr) (curry2 Application)
                          .&gt;&gt; pWs .&gt;&gt; pchar <span style="color:maroon;">')'

</span><span style="color:blue;">do </span>pExprRef := pFunction &lt;|&gt; pApplication &lt;|&gt; pName

<span style="color:blue;">let </span>pExpressions = sepBy pExpr spaces1</pre>
<p><a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a></p>
<p>This mirrors pretty closely the grammar we are trying to parse. A program is a bunch of expressions separated by whitespaces.</p>
<p><span style="color:blue;">let </span>pExpressions = sepBy pExpr spaces1</p>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>sepBy is a combinator that takes a parser that defines what to parse and a parser that defines what the separator should be. And the separator should be one or more spaces …</p>
<p><em>pExpr</em> is either a function, an application or a name. the operator &lt;|&gt; is a choice combinator that tries each parser in order. It tries the right parser just if the left parser fails and it doesn’t consume input. So it doesn’t backtrack. If you need a backtracking parser, you’ll need to get acquainted with the <em>attempt</em> combinator.</p>
<pre class="code"><span style="color:blue;">do </span>pExprRef := pFunction &lt;|&gt; pApplication &lt;|&gt; pName</pre>
<p>A function starts with a ‘\’, then comes a name, a dot and an expression. </p>
<pre class="code"><span style="color:blue;">let </span>pFunction = pchar <span style="color:maroon;">'\\' </span>&gt;&gt;. pipe2 pName (pchar <span style="color:maroon;">'.' </span>&gt;&gt;. pExpr)                                                       (curry2 Function)</pre>
<p>I know that might look crazy (and maybe it is), but just bear with me. Someone , who I’m not sure I can name, once told me that functional programming is great to write code, but terrible to read it and debug it. The phrase stayed with me as containing a grain of truth. In any case, in the code above:</p>
<ul>
<li>&gt;&gt;. is a combinator that says “use the left parser, discard its value and then use the right one, returning its value”. Try to guess what .&gt;&gt; does … </li>
<li>pipe2 is a combinator that says “apply the first parser, the second parser and then call a function passing as parameters the values returned by the two parsers </li>
<li>curry2 is a function combinator that transform a function that takes a tuple to a function that takes the parameters as untupled </li>
</ul>
<pre class="code"><span style="color:blue;">let </span>curry2 f a b = f(a,b)</pre>
<p>An application works similarly, but differently …</p>
<pre class="code"><span style="color:blue;">let </span>pApplication = pchar <span style="color:maroon;">'(' </span>&gt;&gt;. pipe2 pExpr (pWs &gt;&gt;. pExpr) (curry2 Application)
                              .&gt;&gt; pWs .&gt;&gt; pchar <span style="color:maroon;">')'</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>The only difference is that now we have to consume the optional whitespaces and the ‘)’ at the end. A rule of thumb that I use is to use &gt;&gt;.&#160; to flow the result through and pipeX when I need more than one result.</p>
<p>The last thing is pName, which consume chars until it finds either a whitespace or a special char.</p>
<pre class="code"><span style="color:blue;">let </span>ws = <span style="color:maroon;">&quot; \t\n&quot;
</span><span style="color:blue;">let </span>specialChars = <span style="color:maroon;">&quot;.)(\\\n&quot;

</span><span style="color:blue;">let </span>pWs = spaces
<span style="color:blue;">let </span>pName = manyChars (noneOf (ws + specialChars)) |&gt;&gt; EName</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>And there you have it, a lexer, a parser all in 20 lines of code. I don’t like the code that I wrote above much. I’m sure I could refine it plenty and it probably contains some bugs, but it gives an idea of what is possible with FParsec.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/lucabolognese.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/lucabolognese.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/lucabolognese.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/lucabolognese.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/lucabolognese.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/lucabolognese.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/lucabolognese.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/lucabolognese.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/lucabolognese.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/lucabolognese.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/lucabolognese.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/lucabolognese.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/lucabolognese.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/lucabolognese.wordpress.com/73/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lucabolognese.wordpress.com&amp;blog=11987081&amp;post=73&amp;subd=lucabolognese&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://lucabolognese.wordpress.com/2011/09/16/adventure-in-parserland-parsing-lambda-expressions-in-f-part-v/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/075c2430e54d24df1a0498bc1e7b746c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">lucabol</media:title>
		</media:content>
	</item>
		<item>
		<title>Adventure in parserland &#8211; parsing lambda expressions in F# &#8211; Part IV</title>
		<link>http://lucabolognese.wordpress.com/2011/09/09/adventure-in-parserland-parsing-lambda-expressions-in-f-part-iv/</link>
		<comments>http://lucabolognese.wordpress.com/2011/09/09/adventure-in-parserland-parsing-lambda-expressions-in-f-part-iv/#comments</comments>
		<pubDate>Fri, 09 Sep 2011 06:09:00 +0000</pubDate>
		<dc:creator>lucabol</dc:creator>
				<category><![CDATA[F#]]></category>

		<guid isPermaLink="false">https://lucabolognese.wordpress.com/2011/09/09/adventure-in-parserland-parsing-lambda-expressions-in-f-part-iv/</guid>
		<description><![CDATA[Let’ now look at the parser. First let’s review the grammar: (* &#60;expression&#62; ::= &#60;name&#62; &#124; &#60;function&#62; &#124; &#60;application&#62; &#60;name&#62; ::= non­blank character sequence &#60;function&#62; ::= \ &#60;name&#62; . &#60;body&#62; &#60;body&#62; ::= &#60;expression&#62; &#60;application&#62; ::= ( &#60;function expression&#62; &#60;argument expression&#62; ) &#60;function expression&#62; ::= &#60;expression&#62; &#60;argument expression&#62; ::= &#60;expression&#62; *) And the data type to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lucabolognese.wordpress.com&amp;blog=11987081&amp;post=71&amp;subd=lucabolognese&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Let’ now look at the parser. First let’s review the grammar:</p>
<pre class="code">    <span style="color:green;">(*
        &lt;expression&gt; ::= &lt;name&gt; | &lt;function&gt; | &lt;application&gt;
        &lt;name&gt; ::= non­blank character sequence
        &lt;function&gt; ::= \ &lt;name&gt; . &lt;body&gt;
        &lt;body&gt; ::= &lt;expression&gt;
        &lt;application&gt; ::= ( &lt;function expression&gt; &lt;argument expression&gt; )
        &lt;function expression&gt; ::= &lt;expression&gt;
        &lt;argument expression&gt; ::= &lt;expression&gt;
    *)
</span></pre>
<p>And the data type to represent it:</p>
<pre class="code"><span style="color:blue;">type </span>Name = string
<span style="color:blue;">and </span>Body = Expression
<span style="color:blue;">and </span>Function = Name * Expression
<span style="color:blue;">and </span>FunctionExpression = Expression
<span style="color:blue;">and </span>ArgumentExpression = Expression
<span style="color:blue;">and </span>Expression =
| EName <span style="color:blue;">of </span>string
| Function <span style="color:blue;">of </span>Expression * Body
| Application <span style="color:blue;">of </span>FunctionExpression * ArgumentExpression
| EOT</pre>
<p>In essence, the data type need to store all the information needed for subsequent stages of computation (i.e. beta reductions and such). The closer it is to the grammar, the better. In this case it looks pretty close.</p>
<p>Remember what is the main goal of our parser:</p>
<pre class="code"><span style="color:blue;">let </span>parseTextReader: TextReader <span style="color:blue;">-&gt; </span>seq&lt;Expression&gt; =
                    textReaderToLazyList &gt;&gt; tokenStream &gt;&gt; parseExpressions</pre>
<p>We have already looked at <em>TextReaderToLazyList</em> and <em>tokenStream</em>. Now it is the time to look at <em>parseExpressions</em>. It’s goal is to&#160; parse the <em>LazyList&lt;Token&gt;</em> and return a sequence of expressions. The choice of returning a sequence at this point is to make the <em>parseTextReader</em>, which is the main function in the program, return a more ‘standard’ type.</p>
<pre class="code"><span style="color:blue;">and </span>parseExpressions tokens = seq {
   <span style="color:blue;">let </span>tokens = parseOptionalWs tokens
   <span style="color:blue;">let </span>expr, tokens = parseExpr tokens
   <span style="color:blue;">let </span>tokens = parseOptionalWs tokens
   <span style="color:blue;">match </span>expr <span style="color:blue;">with
    </span>| EOT   <span style="color:blue;">-&gt; yield </span>EOT
    | exp   <span style="color:blue;">-&gt; yield </span>exp; <span style="color:blue;">yield! </span>parseExpressions tokens }</pre>
<p><em>parseOtionalWs</em> simply skips ahead whatever whitespaces it finds.</p>
<pre class="code"><span style="color:blue;">and </span>parseOptionalWs tokens = <span style="color:blue;">match </span>tokens <span style="color:blue;">with
                                </span>| LazyList.Nil <span style="color:blue;">-&gt; </span>LazyList.empty
                                | LazyList.Cons(h, t) <span style="color:blue;">-&gt;
                                    match </span>h <span style="color:blue;">with
                                       </span>| Ws _ <span style="color:blue;">-&gt; </span>parseOptionalWs t
                                       | _ <span style="color:blue;">-&gt; </span>tokens</pre>
<p><em>parseExpr</em> is more interesting. It is the main switch that creates expression kinds.</p>
<pre class="code"><span style="color:blue;">let rec </span>parseExpr tokens = <span style="color:blue;">match </span>tokens <span style="color:blue;">with
                            </span>| LazyList.Nil <span style="color:blue;">-&gt; </span>EOT, LazyList.empty
                            | LazyList.Cons(h, t) <span style="color:blue;">-&gt;
                                match </span>h <span style="color:blue;">with
                                    </span>| EOF <span style="color:blue;">-&gt; </span>parseEOF tokens
                                    | Name _ <span style="color:blue;">-&gt; </span>parseName  tokens
                                    | Lambda <span style="color:blue;">-&gt; </span>parseFunction tokens
                                    | OpenParens <span style="color:blue;">-&gt; </span>parseApplication tokens
                                    | token <span style="color:blue;">-&gt; </span>errorAtStart <span style="color:maroon;">&quot;Expression&quot; </span>token</pre>
<p><em>parseEOF</em> is not.</p>
<pre class="code"><span style="color:blue;">and </span>parseEOF tokens = EOT, LazyList.empty</pre>
<p><em>parseName</em> just returns a <em>EName</em>, unwrapping it from Name.</p>
<pre class="code"><span style="color:blue;">and </span>parseName tokens = EName (head tokens |&gt; unwrapName), tail tokens</pre>
<p>Unwrap just unwraps it.</p>
<pre class="code"><span style="color:blue;">let </span>unwrapName = <span style="color:blue;">function
    </span>| Name(s) <span style="color:blue;">-&gt; </span>s
    | tok <span style="color:blue;">-&gt; </span>errorExpecting <span style="color:maroon;">&quot;a Name&quot; </span>&lt;| writeToken tok</pre>
<p><em>parseFunction</em> just conumes a Lambda, a name, a Dot token, a body (i.e. \x.x)and assembles them in a Function:</p>
<pre class="code"><span style="color:blue;">and </span>parseFunction tokens =
    <span style="color:blue;">let </span>tokens = consumeToken Lambda tokens
    <span style="color:blue;">let </span>name, tokens = parseName tokens
    <span style="color:blue;">let </span>tokens = consumeToken Dot tokens
    <span style="color:blue;">let </span>body, tokens = parseExpr tokens
    Function(name, body), tokens</pre>
<p><em>consumeToken</em> tries to consume a token generating an error if it doesn’t find it:</p>
<pre class="code"><span style="color:blue;">let </span>consumeToken token =
    genericConsumeToken (<span style="color:blue;">fun </span>token' _ <span style="color:blue;">-&gt; </span>errorExpecting (writeToken token') (writeToken token)) token</pre>
<p><em>genericConsumeToken</em> is just a generalization of the function above:</p>
<pre class="code"><span style="color:blue;">let </span>genericConsumeToken noMatch token = <span style="color:blue;">function
    </span>| LazyList.Nil <span style="color:blue;">-&gt; </span>LazyList.empty
    | LazyList.Cons(h, t) <span style="color:blue;">as </span>originalTokens <span style="color:blue;">-&gt;
        match </span>h <span style="color:blue;">with
        </span>| tok <span style="color:blue;">when </span>tok = token <span style="color:blue;">-&gt; </span>t
        | tok <span style="color:blue;">-&gt; </span>noMatch token originalTokens</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>The last thing left to consume is an application which is in this form (func args):</p>
<pre class="code"><span style="color:blue;">and </span>parseApplication tokens =
    <span style="color:blue;">let </span>tokens = consumeToken OpenParens tokens
    <span style="color:blue;">let </span>funExpr, tokens = parseExpr tokens
    <span style="color:blue;">let </span>tokens = parseOptionalWs tokens
    <span style="color:blue;">let </span>argExpr, tokens = parseExpr tokens
    <span style="color:blue;">let </span>tokens = consumeToken CloseParens tokens
    Application(funExpr, argExpr), tokens</pre>
<p>Various error and utility functions are defined below:</p>
<pre class="code"><span style="color:blue;">let </span>errorEOF expecting = failwith  (<span style="color:maroon;">&quot;Expected &quot; </span>+ expecting + <span style="color:maroon;">&quot;, got EOF&quot;</span>)
<span style="color:blue;">let </span>errorExpecting expecting gotToken = failwith (<span style="color:maroon;">&quot;Expected &quot; </span>+ expecting + <span style="color:maroon;">&quot;, got&quot; </span>+ gotToken)
<span style="color:blue;">let </span>errorAtStart expecting gotToken = failwith (<span style="color:maroon;">&quot;Expected &quot; </span>+ expecting + <span style="color:maroon;">&quot; which cannot start with&quot; </span>+ writeToken gotToken)

<span style="color:blue;">let </span>tail = LazyList.tail
<span style="color:blue;">let </span>head = LazyList.head</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>And that is the parser. All 100+ lines of it. As you can tell it is rather formulaic to go from a grammar to a lexer and a parser, which is why you shouldn’t do it, but instead let a tool generate the code for you given the grammar or use FParsec.</p>
<p>We have written 200+ code and I don’t think we can be too proud of our achievement. It is:</p>
<ul>
<li>Certainly buggy</li>
<li>Primitive in error handling</li>
<li>Not tail recursive (big text is likely to blow up our stack)</li>
<li>Probably inefficient</li>
</ul>
<p>So let’s look next at a better way to do it.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/lucabolognese.wordpress.com/71/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/lucabolognese.wordpress.com/71/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/lucabolognese.wordpress.com/71/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/lucabolognese.wordpress.com/71/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/lucabolognese.wordpress.com/71/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/lucabolognese.wordpress.com/71/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/lucabolognese.wordpress.com/71/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/lucabolognese.wordpress.com/71/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/lucabolognese.wordpress.com/71/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/lucabolognese.wordpress.com/71/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/lucabolognese.wordpress.com/71/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/lucabolognese.wordpress.com/71/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/lucabolognese.wordpress.com/71/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/lucabolognese.wordpress.com/71/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lucabolognese.wordpress.com&amp;blog=11987081&amp;post=71&amp;subd=lucabolognese&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://lucabolognese.wordpress.com/2011/09/09/adventure-in-parserland-parsing-lambda-expressions-in-f-part-iv/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/075c2430e54d24df1a0498bc1e7b746c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">lucabol</media:title>
		</media:content>
	</item>
		<item>
		<title>Adventure in parserland &#8211; parsing lambda expressions in F# &#8211; Part III</title>
		<link>http://lucabolognese.wordpress.com/2011/09/02/adventure-in-parserland-parsing-lambda-expressions-in-f-part-iii/</link>
		<comments>http://lucabolognese.wordpress.com/2011/09/02/adventure-in-parserland-parsing-lambda-expressions-in-f-part-iii/#comments</comments>
		<pubDate>Fri, 02 Sep 2011 15:25:00 +0000</pubDate>
		<dc:creator>lucabol</dc:creator>
				<category><![CDATA[F#]]></category>

		<guid isPermaLink="false">https://lucabolognese.wordpress.com/2011/09/02/adventure-in-parserland-parsing-lambda-expressions-in-f-part-iii/</guid>
		<description><![CDATA[Let’s start from the lexer. Remember, I wrote this code based on my memory of how a lexer ought to look like. I didn’t read again the relevant chapters in the Dragon book. But I think it came out all right after all. The tokenStream function we looked at last time takes a LazyList&#60;char&#62; and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lucabolognese.wordpress.com&amp;blog=11987081&amp;post=69&amp;subd=lucabolognese&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Let’s start from the lexer. Remember, I wrote this code based on my memory of how a lexer ought to look like. I didn’t read again the relevant chapters in <a href="http://en.wikipedia.org/wiki/Principles_of_Compiler_Design">the Dragon book</a>. But I think it came out all right after all.</p>
<p>The <em>tokenStream</em> function we looked at last time takes a <em>LazyList&lt;char&gt;</em> and returns a <em>LazyList&lt;Token&gt;</em>. It uses the unfold method on <em>LazyList</em> to call <em>matchToken</em> on each char until the stream is empty.</p>
<pre class="code"><span style="color:blue;">let rec </span>tokenStream chars =
    LazyList.unfold
        (<span style="color:blue;">fun </span>chList <span style="color:blue;">-&gt;
            match </span>chList <span style="color:blue;">with
            </span>| LazyList.Nil <span style="color:blue;">-&gt; </span>None
            | chList <span style="color:blue;">-&gt;
                let </span>token, chList' = matchToken chList
                Some(token, chList')
        )
        chars </pre>
<p>A token is what gets passed up to the parser to do syntactic analysis on. It is the vocabulary of our language. The lexer divide a phrase in words, the parser put together the words in a phrase. So, these are the words.</p>
<pre class="code"><span style="color:blue;">type </span>Token =
    | Name <span style="color:blue;">of </span>string
    | Dot
    | OpenParens
    | CloseParens
    | Lambda
    | Def
    | Ws <span style="color:blue;">of </span>string
    | NewLine
    | EOF</pre>
<p>Matching is a process whereas you try to return the token that you have read plus the list of characters yet to be read. Matching a Token is defined below:</p>
<pre class="code"><span style="color:blue;">let </span>matchToken = <span style="color:blue;">function
    </span>| LazyList.Nil                 <span style="color:blue;">-&gt; </span>EOF, LazyList.empty
    | LazyList.Cons(h, t) <span style="color:blue;">as </span>chars <span style="color:blue;">-&gt;
        match </span>h <span style="color:blue;">with
        </span>| ch <span style="color:blue;">when </span>isWs ch <span style="color:blue;">-&gt; </span>matchWs chars
        | ch <span style="color:blue;">when </span>isSpecialChar ch <span style="color:blue;">-&gt; </span>matchSpecialChar ch t
        | _ <span style="color:blue;">-&gt; </span>matchString chars</pre>
</p>
<p>A token is either nothing, a whitespace, a special char or anything else.</p>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Let’s look at what matching each one of them means.&#160; Matching whitespaces means consuming them and remembering what was consumed.</p>
<pre class="code"><span style="color:blue;">let </span>matchWs chars =
    <span style="color:blue;">let </span>value, remainingChars = matchSeriesOfChars isWs chars
    Ws value, remainingChars</pre>
<p><em>matchSeriesOfChars</em> takes a predicate and a <em>LazyList</em> of chars and returns the string composed of all the consecutive chars for which the predicate is true, plus, as always, the remaining chars to be matched. In this case the predicate returns true if the char is a whitespace.</p>
<p>To write <em>matchSeriesOfChars</em> I need a function that reverses a LazyList. Not having found such thing, I wrote it.</p>
<pre class="code"><span style="color:blue;">let </span>reversell l =
    <span style="color:blue;">let rec </span>go l' a = <span style="color:blue;">match </span>l', a <span style="color:blue;">with
                        </span>| LazyList.Nil, a <span style="color:blue;">-&gt; </span>a
                        | LazyList.Cons(h, t), a <span style="color:blue;">-&gt; </span>go t (LazyList.cons h a)
    go l LazyList.empty</pre>
<p>Then I wrote <em>matchSeriesOfChars</em>. The function uses an accumulator. It adds to the front whenever the predicate is true, it reverses it and translates it to a string (I could have reversed the string instead, it might have been better).</p>
<pre class="code"><span style="color:blue;">let </span>matchSeriesOfChars comparer chars =
    <span style="color:blue;">let rec </span>go result = <span style="color:blue;">function
        </span>| LazyList.Nil    <span style="color:blue;">-&gt; </span>charListToString(reversell result), LazyList.empty
        | LazyList.Cons(h, t) <span style="color:blue;">-&gt; if </span>comparer h <span style="color:blue;">then </span>go (LazyList.cons h result) t
                                 <span style="color:blue;">else </span>charListToString (reversell result), LazyList.cons h t
    go LazyList.empty chars</pre>
<p>These are&#160; predicates we’ll use later on to recognize characters:</p>
<pre class="code"><span style="color:blue;">let </span>isInString (ch: char) (s: string) = s.IndexOf(ch) &lt;&gt; -1
<span style="color:blue;">let </span>isWs (chr: char) = isInString chr wsChars
<span style="color:blue;">let </span>isNameChar (chr: char) = not (isInString chr (wsChars + specialChars))
<span style="color:blue;">let </span>isSpecialChar ch = isInString ch specialChars</pre>
<p><em>wsChar</em> and <em>specialChars</em> are defined below:</p>
<pre class="code">    <span style="color:blue;">let </span>wsChars = <span style="color:maroon;">&quot; \t&quot;</span></pre>
<pre class="code"><span style="color:blue;">    let </span>charTokens =
        Map.ofList [
            <span style="color:maroon;">'.' </span>, Dot
            <span style="color:maroon;">'(' </span>, OpenParens
            <span style="color:maroon;">')' </span>, CloseParens
            <span style="color:maroon;">'\\'</span>, Lambda
            <span style="color:maroon;">'\n'</span>, NewLine
         ]
    <span style="color:blue;">let </span>specialChars = charTokens |&gt; Map.fold (<span style="color:blue;">fun </span>s k v <span style="color:blue;">-&gt; </span>s + k.ToString()) <span style="color:maroon;">&quot;&quot;</span></pre>
<p><a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a></p>
<p>Getting back to the more important matching functions, matching a special character is defined as a simple lookup in the <em>charToken</em> map:</p>
<pre class="code"><span style="color:blue;">let </span>matchSpecialChar ch chars = Map.find ch charTokens, chars</pre>
<p>We are left with matchString, this simply matches the characters until it finds a char that cannot be part of a name. It then looks it up in a list of special strings. If it finds it, it returns it, otherwise it just returns the name.</p>
<pre class="code"><span style="color:blue;">let </span>stringTokens =
    Map.ofList [
        <span style="color:maroon;">&quot;Def&quot;</span>, Def
    ]</pre>
<pre class="code">
<span style="color:blue;">let </span>matchString chars =
    <span style="color:blue;">let </span>value, remainingChars = matchSeriesOfChars isNameChar chars
    <span style="color:blue;">let </span>specialString = Map.tryFind value stringTokens
    <span style="color:blue;">if </span>specialString.IsSome
        <span style="color:blue;">then </span>specialString.Value, remainingChars
        <span style="color:blue;">else </span>Name(value), remainingChars</pre>
<p>And we are done with the lexer, all of 100+ lines of it …<a href="http://11011.net/software/vspaste"></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/lucabolognese.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/lucabolognese.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/lucabolognese.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/lucabolognese.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/lucabolognese.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/lucabolognese.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/lucabolognese.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/lucabolognese.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/lucabolognese.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/lucabolognese.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/lucabolognese.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/lucabolognese.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/lucabolognese.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/lucabolognese.wordpress.com/69/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lucabolognese.wordpress.com&amp;blog=11987081&amp;post=69&amp;subd=lucabolognese&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://lucabolognese.wordpress.com/2011/09/02/adventure-in-parserland-parsing-lambda-expressions-in-f-part-iii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/075c2430e54d24df1a0498bc1e7b746c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">lucabol</media:title>
		</media:content>
	</item>
		<item>
		<title>Adventure in parserland &#8211; parsing lambda expressions in F# &#8211; Part II</title>
		<link>http://lucabolognese.wordpress.com/2011/08/26/adventure-in-parserland-parsing-lambda-expressions-in-f-part-ii/</link>
		<comments>http://lucabolognese.wordpress.com/2011/08/26/adventure-in-parserland-parsing-lambda-expressions-in-f-part-ii/#comments</comments>
		<pubDate>Fri, 26 Aug 2011 14:25:00 +0000</pubDate>
		<dc:creator>lucabol</dc:creator>
				<category><![CDATA[F#]]></category>

		<guid isPermaLink="false">https://lucabolognese.wordpress.com/2011/08/26/adventure-in-parserland-parsing-lambda-expressions-in-f-part-ii/</guid>
		<description><![CDATA[The parser starts simple with the following two functions to parse either a string or a file. I use the XXXReaders because I want to lazy read character by character. let parseString s = let reader = new StringReader(s) parseTextReader reader let parseFile fileName = let reader = new StreamReader(fileName: string) parseTextReader reader The whole [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lucabolognese.wordpress.com&amp;blog=11987081&amp;post=67&amp;subd=lucabolognese&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The parser starts simple with the following two functions to parse either a string or a file. I use the XXX<em>Readers</em> because I want to lazy read character by character.</p>
<pre class="code"><span style="color:blue;">let </span>parseString s =
    <span style="color:blue;">let </span>reader = <span style="color:blue;">new </span>StringReader(s)
    parseTextReader reader

<span style="color:blue;">let </span>parseFile fileName =
    <span style="color:blue;">let </span>reader = <span style="color:blue;">new </span>StreamReader(fileName: string)
    parseTextReader reader</pre>
<p>The whole parser is in the following two lines:</p>
<pre class="code"><span style="color:blue;">let </span>parseTextReader: TextReader <span style="color:blue;">-&gt; </span>seq&lt;Expression&gt; =
                    textReaderToLazyList &gt;&gt; tokenStream &gt;&gt; parseExpressions</pre>
<p>I need to specify the signature otherwise the compiler gets confused : wait, does it take a StringReader or a StreamReader? You better tell me!</p>
<p>The function is a composite of three functions applied in sequence:</p>
<ol>
<li>Translate a TextReader to a LazyList&lt;char&gt; </li>
<li>Translate a LazyList&lt;char&gt; to a LazyList&lt;Token&gt; (lexer) </li>
<li>Translate a LazyList&lt;Token&gt; to a LazyList&lt;Expression&gt; (parser) </li>
</ol>
<p>My usage of <em>LazyList</em> as the workhorse for the program is because I want to match on the head of the stream of chars/tokens in a lazy way.</p>
<p>I love it when a program naturally decomposes in such simple understandable pieces. I impute some of that to functional programming. For one reason or another, in my 15+ years of object oriented programming, I’ve rarely got to the core of a problem with such immediacy.</p>
<p>A sequence of operations likes the above would be lost in a protected overridden implementation of a base class somewhere (or something else equally long to pronounce). The beauty would be lost somewhere in the vast machinery required to support it.</p>
<p>In any case, <em>TextReaderToLazyList </em>is a trivial generator function that uses the unfold function of LazyList to read a character at the time.</p>
<pre class="code"><span style="color:blue;">let </span>textReaderToLazyList textReader = LazyList.unfold (<span style="color:blue;">fun </span>(ts:TextReader) <span style="color:blue;">-&gt;
    let </span>ch = ts.Read()
    <span style="color:blue;">if </span>ch = -1 <span style="color:blue;">then </span>None <span style="color:blue;">else </span>Some(char ch, ts)) textReader</pre>
<p>The next step is to look at either the lexer, going bottom up, or the parser, going top down.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/lucabolognese.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/lucabolognese.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/lucabolognese.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/lucabolognese.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/lucabolognese.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/lucabolognese.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/lucabolognese.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/lucabolognese.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/lucabolognese.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/lucabolognese.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/lucabolognese.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/lucabolognese.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/lucabolognese.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/lucabolognese.wordpress.com/67/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lucabolognese.wordpress.com&amp;blog=11987081&amp;post=67&amp;subd=lucabolognese&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://lucabolognese.wordpress.com/2011/08/26/adventure-in-parserland-parsing-lambda-expressions-in-f-part-ii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/075c2430e54d24df1a0498bc1e7b746c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">lucabol</media:title>
		</media:content>
	</item>
		<item>
		<title>Adventure in parserland &#8211; parsing lambda expressions in F# &#8211; Part I</title>
		<link>http://lucabolognese.wordpress.com/2011/08/19/adventure-in-parserland-parsing-lambda-expressions-in-f-part-i/</link>
		<comments>http://lucabolognese.wordpress.com/2011/08/19/adventure-in-parserland-parsing-lambda-expressions-in-f-part-i/#comments</comments>
		<pubDate>Fri, 19 Aug 2011 06:53:00 +0000</pubDate>
		<dc:creator>lucabol</dc:creator>
				<category><![CDATA[F#]]></category>

		<guid isPermaLink="false">https://lucabolognese.wordpress.com/2011/08/19/adventure-in-parserland-parsing-lambda-expressions-in-f-part-i/</guid>
		<description><![CDATA[This is part of my ‘things that I do in the empty spaces between one meeting and the next one, which might end up being vaguely interesting’. It is a lambda expression parser. The full source code is here. I actually have two versions of it: one written longhand and the other one written with [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lucabolognese.wordpress.com&amp;blog=11987081&amp;post=66&amp;subd=lucabolognese&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This is part of my ‘things that I do in the empty spaces between one meeting and the next one, which might end up being vaguely interesting’. It is a lambda expression parser.</p>
<p>The full source code is <a href="http://code.msdn.microsoft.com/Parsing-Lambda-Expressions-7ad5271f">here</a>.</p>
<p>I actually have two versions of it: one written longhand and the other one written with <a href="http://www.quanttec.com/fparsec/about/fparsec-vs-alternatives.html">FParsec</a>. Just to be clear: I’m no expert of either.</p>
<p>And just to be more clear: I think writing most parsers longhand in the way I am about to show is crazy. You either use FParsec or&#160; <a href="http://blogs.msdn.com/b/jomo_fisher/archive/2010/06/15/use-fslex-and-fsyacc-to-make-a-parser-in-f.aspx">fslex / fsyacc</a>.</p>
<p>I have a strong distaste for additional compilation steps. I think it lingers on from MFC project types of 15/20 years ago. I was one of these crazy folks that would generate the project, wrap the generated code (with some generalizations) in my own library and use that one from then on.</p>
<p>So I prefer FParsec. I’m ok rewriting <a href="http://en.wikipedia.org/wiki/Left_recursion">left recursive</a> rules and its performance has never been a problem for me. <a href="http://www.quanttec.com/fparsec/about/fparsec-vs-alternatives.html">Here</a> is a table that compares the different approaches.</p>
<p>But I started wondering about coding a a recursive descent parser for a simple grammar by hand, fully knowing the foolishness of the idea. Thanks to <a href="http://www.haskellers.com/user/pepeiborra">Jose</a> for code reviewing it.</p>
<p>The inspiration for the grammar comes from <a href="http://www.google.co.uk/url?sa=t&amp;source=web&amp;cd=4&amp;ved=0CDgQFjAD&amp;url=http%3A%2F%2Fwww.amazon.com%2FIntroduction-Functional-Programming-Calculus-International%2Fdp%2F0201178125&amp;ei=KoklTqzjF4GWhQf-w5XlCQ&amp;usg=AFQjCNGPzv_27nSNwctaEykBivq3N-I7Dg&amp;sig2=4iLu_nZnd8GlwPXpt8crMg">this book</a>.</p>
<pre class="code">    <span style="color:green;">(*
        &lt;expression&gt; ::= &lt;name&gt; | &lt;function&gt; | &lt;application&gt;
        &lt;name&gt; ::= non­blank character sequence
        &lt;function&gt; ::= \ &lt;name&gt; . &lt;body&gt;
        &lt;body&gt; ::= &lt;expression&gt;
        &lt;application&gt; ::= ( &lt;function expression&gt; &lt;argument expression&gt; )
        &lt;function expression&gt; ::= &lt;expression&gt;
        &lt;argument expression&gt; ::= &lt;expression&gt;
    *)
</span></pre>
<p>In English, an expression is either a name, a function or an application. A name is a bunch of characters (better defined in the code). A function is ‘\’, a name, ‘.’ and an expression. An application is ‘(‘, an expression, whitespaces, an expression and ‘)’.</p>
<p>Some testcases for the above grammar and the parsers written to parse it are below. It should be intuitive what this code does just by the name of the functions. Even it isn’t, check that the expressions symbol contains valid productions from the grammar above.</p>
<pre class="code"><span style="color:blue;">module </span>Test

<span style="color:blue;">open </span>Microsoft.FSharp.Collections
<span style="color:blue;">open </span>Xunit

<span style="color:blue;">open </span>LambdaEngine
<span style="color:blue;">open </span>Parser
<span style="color:blue;">open </span>Lexer
<span style="color:blue;">open </span>FParser

<span style="color:blue;">let </span>writeTokenStream stream = Seq.fold (<span style="color:blue;">fun </span>acc token <span style="color:blue;">-&gt; </span>acc + writeToken token) <span style="color:maroon;">&quot;&quot; </span>stream

<span style="color:blue;">let rec </span>writeExpr = <span style="color:blue;">function
        </span>| EName(s) <span style="color:blue;">-&gt; </span>s
        | Function(expr, body) <span style="color:blue;">-&gt; </span>writeToken Lambda + writeExpr expr + writeToken Dot + writeExpr body
        | Application(funExpr, argExpr) <span style="color:blue;">-&gt; </span>writeToken OpenParens + writeExpr funExpr + writeToken (Ws(<span style="color:maroon;">&quot; &quot;</span>))
                                            + writeExpr argExpr + writeToken CloseParens
        | EOT <span style="color:blue;">-&gt; </span><span style="color:maroon;">&quot;&quot;

</span><span style="color:blue;">let </span>tokenStreams = [
    <span style="color:maroon;">&quot;&quot;
    &quot;(\xs.xs \y.(y \x.y))&quot;
    &quot;(\xst.xst \y.(y  \x.y))&quot;
    &quot; &quot;
    &quot;x&quot;
    &quot;(x y)&quot;
    </span>]

<span style="color:blue;">let </span>expressions = [
    <span style="color:maroon;">&quot;&quot;
    &quot;(\x.x \y.(y \x.y))&quot;
    &quot;x&quot;
    &quot;(x y)&quot;
    </span>]

<span style="color:blue;">let </span>stringToCharList s =
    <span style="color:blue;">let </span>textReader = <span style="color:blue;">new </span>System.IO.StringReader(s)
    textReaderToLazyList textReader

[&lt;Fact&gt;]
<span style="color:blue;">let </span>testTokenizer () =
    <span style="color:blue;">let </span>testTokenStream s =
        <span style="color:blue;">let </span>stream = tokenStream &lt;| stringToCharList s
        <span style="color:blue;">let </span>s1 = writeTokenStream stream
        Assert.Equal(s, s1)
    tokenStreams |&gt; List.iter testTokenStream

<span style="color:blue;">let </span>testExpr parseFunction s =
    <span style="color:blue;">let </span>exprs = parseFunction s
    <span style="color:blue;">let </span>s1 = exprs |&gt; Seq.fold (<span style="color:blue;">fun </span>s expr <span style="color:blue;">-&gt; </span>s + writeExpr expr) <span style="color:maroon;">&quot;&quot;
    </span>Assert.Equal(s, s1)

[&lt;Fact&gt;]
<span style="color:blue;">let </span>testParser () = expressions |&gt; List.iter (testExpr parseString)

[&lt;Fact&gt;]
<span style="color:blue;">let </span>testFParser () = expressions |&gt; List.iter (testExpr fparseString)</pre>
<p>In the next instalment, we’ll start looking at the real code for the parser.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/lucabolognese.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/lucabolognese.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/lucabolognese.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/lucabolognese.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/lucabolognese.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/lucabolognese.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/lucabolognese.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/lucabolognese.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/lucabolognese.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/lucabolognese.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/lucabolognese.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/lucabolognese.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/lucabolognese.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/lucabolognese.wordpress.com/66/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lucabolognese.wordpress.com&amp;blog=11987081&amp;post=66&amp;subd=lucabolognese&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://lucabolognese.wordpress.com/2011/08/19/adventure-in-parserland-parsing-lambda-expressions-in-f-part-i/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/075c2430e54d24df1a0498bc1e7b746c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">lucabol</media:title>
		</media:content>
	</item>
		<item>
		<title>Write Yourself a Scheme in 48 Hours in F# &#8211; Part VII</title>
		<link>http://lucabolognese.wordpress.com/2011/08/12/write-yourself-a-scheme-in-48-hours-in-f-part-vii/</link>
		<comments>http://lucabolognese.wordpress.com/2011/08/12/write-yourself-a-scheme-in-48-hours-in-f-part-vii/#comments</comments>
		<pubDate>Fri, 12 Aug 2011 07:11:00 +0000</pubDate>
		<dc:creator>lucabol</dc:creator>
				<category><![CDATA[F#]]></category>

		<guid isPermaLink="false">https://lucabolognese.wordpress.com/2011/08/12/write-yourself-a-scheme-in-48-hours-in-f-part-vii/</guid>
		<description><![CDATA[Let’s talk about the environment now.&#160; This is the part of the interpreter that I like the least. It is a global variable and it contains a list of&#160; (string, LispVal) where the LispVal is mutable. type Env = (string * LispVal ref) list ref This is pretty bad. First of all, it immediately cuts [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lucabolognese.wordpress.com&amp;blog=11987081&amp;post=55&amp;subd=lucabolognese&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Let’s talk about the environment now.&#160; This is the part of the interpreter that I like the least. It is a global variable and it contains a list of&#160; (string, LispVal) where the LispVal is mutable.</p>
<pre class="code"><span style="color:blue;">type </span>Env = (string * LispVal ref) list ref</pre>
<p>This is pretty bad. First of all, it immediately cuts off any option of running interpreters in different threads. Moreover, it makes a lot of functions in the evaluator to have side effects. That makes it much harder to reason about them.</p>
<p>In a world where I am provided with infinite time and energy, I would change it. In this world, I won’t. If you try your hand at doing it, make sure that you pass all the testcases before declaring victory. The scope rules of Scheme are not all that obvious. A code reviewer called them the Italian scoping rules because he thought I got them wrong …</p>
<p>In any case, there isn’t much to the symbol table management.&#160; You can create an empty one:</p>
<pre class="code"><span style="color:blue;">let </span>nullEnv (): Env = ref List.empty</pre>
<p>Check if a variable is bound:</p>
<pre class="code"><span style="color:blue;">let </span>keyEq name (k, _) = name = k

<span style="color:blue;">let </span>isBound var (env: Env) = !env |&gt; List.exists (keyEq var)</pre>
<p>Get a variable out:</p>
<pre class="code"><span style="color:blue;">let </span>getVar var (env: Env) =
    <span style="color:blue;">let </span>result = !env |&gt; List.tryFind (keyEq var)
    <span style="color:blue;">match </span>result <span style="color:blue;">with
    </span>| None <span style="color:blue;">-&gt; </span>throw (UnboundVar(<span style="color:maroon;">&quot;Getting an unbound variable: &quot; </span>, var))
    | Some(_, r) <span style="color:blue;">-&gt; </span>!r</pre>
<p>Set the value of an existing variable:</p>
<pre class="code"><span style="color:blue;">let </span>setVar var value (env:Env) =
    <span style="color:blue;">let </span>result = !env |&gt; List.tryFind (keyEq var)
    <span style="color:blue;">match </span>result <span style="color:blue;">with
    </span>| Some(_, v) <span style="color:blue;">-&gt; </span>v := value ; value
    | None <span style="color:blue;">-&gt; </span>throw (UnboundVar(<span style="color:maroon;">&quot;Setting an unbound variable: &quot; </span>, var))</pre>
<p>Or define a new variable in the environment. Note that if the variable already exist, its value gets set.</p>
<pre class="code"><span style="color:blue;">let </span>define (env:Env) var value =
    <span style="color:blue;">let </span>result = !env |&gt; List.tryFind (keyEq var)
    <span style="color:blue;">match </span>result <span style="color:blue;">with
    </span>| Some(_, v) <span style="color:blue;">-&gt; </span>v := value ; value
    | None <span style="color:blue;">-&gt;
        </span>env := [var, ref value] @ !env; value</pre>
<pre class="code"><font face="Lucida Sans Unicode">You can also bind a list of (string, LispVal) to the environment by prepending it to the existing ones:</font></pre>
<pre class="code"><span style="color:blue;">let </span>bindVars bindings (env:Env) =
   ref ((bindings |&gt; List.map (<span style="color:blue;">fun </span>(n, v) <span style="color:blue;">-&gt; </span>n , ref v)) @ !env)</pre>
<p>Once you accept the evil of the global mutable variable scheme, these functions are easy enough.</p>
<p>The only piece left is error management. This is where my implementation differs from the Haskell version the most. In essence, I throw exception and catch them to report errors, while the Haskell version uses a monad to propagate the error information.</p>
<p>I have a <em>LispError</em> that represents everything that can go wrong:</p>
<pre class="code"><span style="color:blue;">type </span>LispError =
    | NumArgs <span style="color:blue;">of </span>int * LispVal list
    | TypeMismatch <span style="color:blue;">of </span>string * LispVal
    | ParseError <span style="color:blue;">of </span>string * FParsec.Error.ParserError
    | BadSpecialForm <span style="color:blue;">of </span>string * LispVal
    | NotFunction <span style="color:blue;">of </span>string * string
    | UnboundVar <span style="color:blue;">of </span>string * string
    | Default <span style="color:blue;">of </span>string
    | IOError <span style="color:blue;">of </span>string</pre>
<p>I wrap it in an exception:</p>
<pre class="code"><span style="color:blue;">exception </span>LispException <span style="color:blue;">of </span>LispError</pre>
<p>This is what I throw in various places in the code.</p>
<pre class="code"><span style="color:blue;">let </span>throw le = raise (LispException(le))</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>I then catch it at the outer layer:</p>
<pre class="code"><span style="color:blue;">let </span>evalString env expr =
    <span style="color:blue;">try
        </span>expr |&gt; readExpr |&gt; eval env
    <span style="color:blue;">with
    </span>| LispException(error) <span style="color:blue;">-&gt; </span>String (showError error)</pre>
<p>And display the error by using the below function:</p>
<pre class="code"><span style="color:blue;">let </span>showError = <span style="color:blue;">function
    </span>| NumArgs(expected, found) <span style="color:blue;">-&gt; </span><span style="color:maroon;">&quot;Expected &quot; </span>+ expected.ToString() + <span style="color:maroon;">&quot; args; found values &quot; </span>+ unwordsList found
    | TypeMismatch(expected, found) <span style="color:blue;">-&gt; </span><span style="color:maroon;">&quot;Invalid type: expected &quot; </span>+ expected + <span style="color:maroon;">&quot;, found &quot; </span>+ showVal found
    | ParseError(msg, _) <span style="color:blue;">-&gt; </span><span style="color:maroon;">&quot;Parse Errror&quot; </span>+ msg
    | BadSpecialForm(message, form) <span style="color:blue;">-&gt; </span>message + showVal form
    | NotFunction(message, func) <span style="color:blue;">-&gt; </span>message + func
    | UnboundVar(message, varName) <span style="color:blue;">-&gt; </span>message + varName
    | Default(message) <span style="color:blue;">-&gt; </span>message
    | IOError(message) <span style="color:blue;">-&gt; </span>message</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>And that’s all there is to it. I hope you guys and gals enjoyed this seven part extravagance. Cheers.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/lucabolognese.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/lucabolognese.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/lucabolognese.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/lucabolognese.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/lucabolognese.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/lucabolognese.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/lucabolognese.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/lucabolognese.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/lucabolognese.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/lucabolognese.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/lucabolognese.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/lucabolognese.wordpress.com/55/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/lucabolognese.wordpress.com/55/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/lucabolognese.wordpress.com/55/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lucabolognese.wordpress.com&amp;blog=11987081&amp;post=55&amp;subd=lucabolognese&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://lucabolognese.wordpress.com/2011/08/12/write-yourself-a-scheme-in-48-hours-in-f-part-vii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/075c2430e54d24df1a0498bc1e7b746c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">lucabol</media:title>
		</media:content>
	</item>
		<item>
		<title>Write Yourself a Scheme in 48 Hours in F# &#8211; Part VI</title>
		<link>http://lucabolognese.wordpress.com/2011/08/05/write-yourself-a-scheme-in-48-hours-in-f-part-vi/</link>
		<comments>http://lucabolognese.wordpress.com/2011/08/05/write-yourself-a-scheme-in-48-hours-in-f-part-vi/#comments</comments>
		<pubDate>Fri, 05 Aug 2011 07:08:00 +0000</pubDate>
		<dc:creator>lucabol</dc:creator>
				<category><![CDATA[F#]]></category>

		<guid isPermaLink="false">https://lucabolognese.wordpress.com/2011/08/06/write-yourself-a-scheme-in-48-hours-in-f-part-vi/</guid>
		<description><![CDATA[The evaluator takes as an input a LispVal. Where does it come from? There must be something that converts your textual input into it. That is the job of the parser. I have used FParsec to build my parser. FParsec is a fantastic library to build parsers. It is a perfect showcase of the composition [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lucabolognese.wordpress.com&amp;blog=11987081&amp;post=53&amp;subd=lucabolognese&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The evaluator takes as an input a <em>LispVal</em>. Where does it come from? There must be something that converts your textual input into it. That is the job of the parser.</p>
<p>I have used <a href="http://www.quanttec.com/fparsec/">FParsec</a> to build my parser. FParsec is a fantastic library to build parsers. It is a perfect showcase of the composition potential that functional code yields.&#160; </p>
<p>When you write an FParsec parser you compose many little parsers to create the one parser that works for your language.&#160; The resulting code looks very much like your language grammar, but you don’t need&#160; a separate code generation compilation step to produce it.</p>
<p>There is one element of ugliness in the syntax to create recursive parsers. You need to define two global variables that can be referred to before they are constructed. This is an artefact of how F# works. So you need a line in your code that looks like this:</p>
<pre class="code"><span style="color:blue;">let </span>parseExpr, parseExprRef : LispParser * LispParser ref = createParserForwardedToRef()</pre>
<p>With that piece of machinery out of the way, we can focus on the parser itself. Our goal here is to parse expressions and generate <em>LispVal</em>. We need a <em>LispParser</em> like the below (the second generic parameter is for advanced usage).</p>
<pre class="code"><span style="color:blue;">type </span>LispParser = Parser&lt;LispVal, unit&gt;</pre>
<p>We need to parse all the kind of expressions that the user can type. Notice in the below the use of a computation expression to simplify the syntax. Also note that lists and dotted lists look very much the same until you encounter the ‘.’ character. You could disambiguate the situation by extracting out the commonality in a separate kind of expression. I decided instead to instruct the parser to backtrack if it gets it wrong (<em>attempt</em>). This is slower, but keeps the code identical to our conceptual model. I value that greatly. </p>
<pre class="code"><span style="color:blue;">do </span>parseExprRef := parseAtom
                   &lt;|&gt; parseString
                   &lt;|&gt; parseNumber
                   &lt;|&gt; parseQuoted
                   &lt;|&gt; parse {
                           <span style="color:blue;">do! </span>chr <span style="color:maroon;">'('
                           </span><span style="color:blue;">let! </span>x = (attempt parseList) &lt;|&gt; parseDottedList
                           <span style="color:blue;">do! </span>chr <span style="color:maroon;">')'
                           </span><span style="color:blue;">return </span>x
                       }</pre>
<p>Let’s start from the top. Parsing an atom means parsing something that starts with a letter or symbol and continues with letters, symbols or digits. Also “#t” and “#f” can be resolved at parsing time.</p>
<pre class="code"><span style="color:blue;">let </span>parseAtom : LispParser = parse {
        <span style="color:blue;">let! </span>first = letter &lt;|&gt; symbol
        <span style="color:blue;">let! </span>rest = manyChars (letter &lt;|&gt; symbol &lt;|&gt; digit)
        <span style="color:blue;">return match </span>first.ToString() + rest <span style="color:blue;">with
               </span>| <span style="color:maroon;">&quot;#t&quot; </span><span style="color:blue;">-&gt; </span>Bool <span style="color:blue;">true
               </span>| <span style="color:maroon;">&quot;#f&quot; </span><span style="color:blue;">-&gt; </span>Bool <span style="color:blue;">false
               </span>| atom <span style="color:blue;">-&gt; </span>Atom atom
}</pre>
<p>A string is just a bunch of chars (except ‘\’) surrounded by ‘ ” ’.</p>
<pre class="code"><span style="color:blue;">let </span>parseString : LispParser = parse {
    <span style="color:blue;">do! </span>chr <span style="color:maroon;">'&quot;'
    </span><span style="color:blue;">let! </span>xs = manyChars (noneOf <span style="color:maroon;">&quot;\&quot;&quot;</span>)
    <span style="color:blue;">do! </span>chr <span style="color:maroon;">'&quot;'
    </span><span style="color:blue;">return </span>String(xs)
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>A number is just one or more digits. I am afraid we just support integers at this stage …</p>
<pre class="code"><span style="color:blue;">let </span>parseNumber : LispParser = many1Chars digit |&gt;&gt; (System.Int32.Parse &gt;&gt; Number)</pre>
<p>A quoted expression is jut a ‘\’ followed by an expression.</p>
<pre class="code"><span style="color:blue;">let </span>parseQuoted : LispParser = chr <span style="color:maroon;">'\'' </span>&gt;&gt;. parseExpr |&gt;&gt; <span style="color:blue;">fun </span>expr <span style="color:blue;">-&gt; </span>List [Atom <span style="color:maroon;">&quot;quote&quot;</span>; expr] </pre>
<p>A list is just a bunch of expressions separate by at least one space.</p>
<pre class="code"><span style="color:blue;">let </span>parseList : LispParser = sepBy parseExpr spaces1 |&gt;&gt; List</pre>
<p>A dotted list starts in the same way (hence the backtracking above), but then has a dot, one or more spaces and an expression.</p>
<pre class="code"><span style="color:blue;">let </span>parseDottedList : LispParser = parse {
    <span style="color:blue;">let! </span>head = endBy parseExpr spaces1
    <span style="color:blue;">let! </span>tail = chr <span style="color:maroon;">'.' </span>&gt;&gt;. spaces1 &gt;&gt;. parseExpr
    <span style="color:blue;">return </span>DottedList (head, tail)
}</pre>
<p>And here are a bunch of functions used throughout the code, presented here for completeness.</p>
<pre class="code">    <span style="color:blue;">let </span>spaces1 : LispParser&lt;unit&gt; = skipMany1 whitespace
    <span style="color:blue;">let </span>chr c = skipChar c
    <span style="color:blue;">let </span>endBy  p sep = many  (p .&gt;&gt; sep)

    <span style="color:blue;">let </span>symbol : LispParser&lt;char&gt; = anyOf <span style="color:maroon;">&quot;!$%&amp;|*+-/:&lt;=&gt;?@^_~#&quot;
</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>This is all the code you need to translate text to a <em>LispVal</em> to feed the evaluator. That is pretty impressive.</p>
<p>There is also a function to go the other way, from a LispVal to text. It is used in implementing the testcases and to print out diagnostics.</p>
<pre class="code">    <span style="color:blue;">let rec </span>showVal = <span style="color:blue;">function
        </span>| String contents <span style="color:blue;">-&gt; </span><span style="color:maroon;">&quot;\&quot;&quot; </span>+ contents + <span style="color:maroon;">&quot;\&quot;&quot;
        </span>| Atom name <span style="color:blue;">-&gt; </span>name
        | Number num <span style="color:blue;">-&gt; </span>num.ToString()
        | Bool t <span style="color:blue;">-&gt; if </span>t <span style="color:blue;">then </span><span style="color:maroon;">&quot;#t&quot; </span><span style="color:blue;">else </span><span style="color:maroon;">&quot;#f&quot;
        </span>| List l <span style="color:blue;">-&gt; </span><span style="color:maroon;">&quot;(&quot; </span>+ unwordsList l + <span style="color:maroon;">&quot;)&quot;
        </span>| DottedList (head, tail) <span style="color:blue;">-&gt; </span><span style="color:maroon;">&quot;(&quot; </span>+ unwordsList head + <span style="color:maroon;">&quot; . &quot; </span>+ showVal tail + <span style="color:maroon;">&quot;)&quot;
        </span>| PrimitiveFunc(_) <span style="color:blue;">-&gt; </span><span style="color:maroon;">&quot;&lt;primitive&gt;&quot;
        </span>| Port (_) <span style="color:blue;">-&gt; </span><span style="color:maroon;">&quot;&lt;IO port&gt;&quot;
        </span>| Func({ parms = parms; varargs = varargs; body = body; closure = closure }) <span style="color:blue;">-&gt;
                                                </span><span style="color:maroon;">&quot;(lambda (&quot; </span>+ unwordsList (parms |&gt; List.map (String)) +
                                                    (<span style="color:blue;">match </span>varargs <span style="color:blue;">with
                                                        </span>| None <span style="color:blue;">-&gt; </span><span style="color:maroon;">&quot;&quot;
                                                        </span>| Some(arg) <span style="color:blue;">-&gt; </span><span style="color:maroon;">&quot; . &quot; </span>+ arg) + <span style="color:maroon;">&quot;) ...)&quot;

    </span><span style="color:blue;">and
        </span>unwordsList = List.map showVal &gt;&gt; String.concat <span style="color:maroon;">&quot; &quot;

</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/lucabolognese.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/lucabolognese.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/lucabolognese.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/lucabolognese.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/lucabolognese.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/lucabolognese.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/lucabolognese.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/lucabolognese.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/lucabolognese.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/lucabolognese.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/lucabolognese.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/lucabolognese.wordpress.com/53/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/lucabolognese.wordpress.com/53/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/lucabolognese.wordpress.com/53/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lucabolognese.wordpress.com&amp;blog=11987081&amp;post=53&amp;subd=lucabolognese&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://lucabolognese.wordpress.com/2011/08/05/write-yourself-a-scheme-in-48-hours-in-f-part-vi/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/075c2430e54d24df1a0498bc1e7b746c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">lucabol</media:title>
		</media:content>
	</item>
		<item>
		<title>Write Yourself a Scheme in 48 Hours in F# &#8211; Part V</title>
		<link>http://lucabolognese.wordpress.com/2011/07/29/write-yourself-a-scheme-in-48-hours-part-iv-2/</link>
		<comments>http://lucabolognese.wordpress.com/2011/07/29/write-yourself-a-scheme-in-48-hours-part-iv-2/#comments</comments>
		<pubDate>Fri, 29 Jul 2011 07:26:00 +0000</pubDate>
		<dc:creator>lucabol</dc:creator>
				<category><![CDATA[F#]]></category>

		<guid isPermaLink="false">https://lucabolognese.wordpress.com/2011/07/30/write-yourself-a-scheme-in-48-hours-part-iv-2/</guid>
		<description><![CDATA[We have one loose end to tie in the evaluator: the primitive operators. These are things that the interpreter knows intrinsically. There is a list of them below. let rec primitives = [ &#34;+&#34;, numericBinop (+) &#34;-&#34;, numericBinop (-) &#34;*&#34;, numericBinop (*) &#34;/&#34;, numericBinop (/) &#34;mod&#34;, numericBinop (%) &#34;=&#34;, numBoolBinop (=) &#34;&#60;&#34;, numBoolBinop (&#60;) &#34;&#62;&#34;, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lucabolognese.wordpress.com&amp;blog=11987081&amp;post=48&amp;subd=lucabolognese&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>We have one loose end to tie in the evaluator: the primitive operators. These are things that the interpreter knows intrinsically. There is a list of them below.</p>
<pre class="code"><span style="color:blue;">let rec </span>primitives =
     [
        <span style="color:maroon;">&quot;+&quot;</span>,    numericBinop (+)
        <span style="color:maroon;">&quot;-&quot;</span>,    numericBinop (-)
        <span style="color:maroon;">&quot;*&quot;</span>,    numericBinop (*)
        <span style="color:maroon;">&quot;/&quot;</span>,    numericBinop (/)
        <span style="color:maroon;">&quot;mod&quot;</span>,  numericBinop (%)
        <span style="color:maroon;">&quot;=&quot;</span>,    numBoolBinop (=)
        <span style="color:maroon;">&quot;&lt;&quot;</span>,    numBoolBinop (&lt;)
        <span style="color:maroon;">&quot;&gt;&quot;</span>,    numBoolBinop (&gt;)
        <span style="color:maroon;">&quot;/=&quot;</span>,   numBoolBinop (&lt;&gt;)
        <span style="color:maroon;">&quot;&gt;=&quot;</span>,   numBoolBinop (&gt;=)
        <span style="color:maroon;">&quot;&lt;=&quot;</span>,   numBoolBinop (&lt;=)
        <span style="color:maroon;">&quot;&amp;&amp;&quot;</span>,   boolBoolBinop (&amp;&amp;)
        <span style="color:maroon;">&quot;||&quot;</span>,   boolBoolBinop (||)
        <span style="color:maroon;">&quot;string=?&quot;</span>,     strBoolBinop (=)
        <span style="color:maroon;">&quot;string&gt;?&quot;</span>,      strBoolBinop (&gt;)
        <span style="color:maroon;">&quot;string&lt;?&quot;</span>,      strBoolBinop (&lt;)
        <span style="color:maroon;">&quot;string&lt;=?&quot;</span>,    strBoolBinop (&lt;=)
        <span style="color:maroon;">&quot;string&gt;=?&quot;</span>,    strBoolBinop (&gt;=)
        <span style="color:maroon;">&quot;car&quot;</span>,  car
        <span style="color:maroon;">&quot;cdr&quot;</span>,  cdr
        <span style="color:maroon;">&quot;cons&quot;</span>, cons
        <span style="color:maroon;">&quot;eq?&quot;</span>, eqv
        <span style="color:maroon;">&quot;eqv?&quot;</span>, eqv
        <span style="color:maroon;">&quot;equal?&quot;</span>, equal

        <span style="color:green;">// IO primitives
        </span><span style="color:maroon;">&quot;apply&quot;</span>, applyProc
        <span style="color:maroon;">&quot;open-input-file&quot;</span>, makePort FileAccess.Read
        <span style="color:maroon;">&quot;open-output-file&quot;</span>, makePort FileAccess.Write
        <span style="color:maroon;">&quot;close-input-port&quot;</span>, closePort
        <span style="color:maroon;">&quot;close-output-port&quot;</span>, closePort
        <span style="color:maroon;">&quot;read&quot;</span>, readProc
        <span style="color:maroon;">&quot;write&quot;</span>, writeProc
        <span style="color:maroon;">&quot;read-contents&quot;</span>, readContents
        <span style="color:maroon;">&quot;read-all&quot;</span>, readAll
     ]</pre>
<p>Having seen the above list, it now becomes clearer why the <em>primitiveBindings</em> function was defined as such. It just binds these pairs into the environment.</p>
<pre class="code"><span style="color:blue;">let </span>primitiveBindings () =
    (nullEnv ()) |&gt; bindVars [ <span style="color:blue;">for </span>v, f <span style="color:blue;">in </span>primitives <span style="color:blue;">-&gt; </span>v, PrimitiveFunc f ] </pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p><em>numericBinop</em> unpacks the numbers, applies the provided operator and packs the result back in the Number.</p>
<pre class="code"><span style="color:blue;">let </span>numericBinop op parms =
    <span style="color:blue;">if </span>List.length parms &lt; 2
        <span style="color:blue;">then </span>throw &lt;| NumArgs(2, parms)
        <span style="color:blue;">else </span>parms |&gt; List.map unpackNum |&gt; foldl1 op |&gt; Number</pre>
<p>While we are at it, we can define <em>fold1 (</em>it tends to be&#160; useful)</p>
<pre class="code"><span style="color:blue;">let </span>foldl1 op = <span style="color:blue;">function
    </span>| h::t <span style="color:blue;">-&gt; </span>List.fold op h t
    | [] <span style="color:blue;">-&gt; </span>throw (Default(<span style="color:maroon;">&quot;Expected a not empty list, got an empty list&quot;</span>))</pre>
<p>The other XBinops work similarly …</p>
<pre class="code"><span style="color:blue;">let </span>boolBinop unpacker op args =
    <span style="color:blue;">match </span>args <span style="color:blue;">with
    </span>| [ left; right ] <span style="color:blue;">-&gt; </span>Bool (op (unpacker left) (unpacker right))
    | _ <span style="color:blue;">-&gt; </span>throw (NumArgs(2,args))

<span style="color:blue;">let </span>numBoolBinop = boolBinop unpackNum
<span style="color:blue;">let </span>strBoolBinop = boolBinop unpackStr
<span style="color:blue;">let </span>boolBoolBinop = boolBinop unpackBool</pre>
<p>We now have to look at the family of unpackers. They all work rather similarly. Notice Scheme making an effort to get a number out of a string and to get anything out of a list. Strong type folks won’t like that. Oh well, just remove these lines …</p>
<pre class="code"><span style="color:blue;">let rec </span>unpackNum = <span style="color:blue;">function
    </span>| Number n  <span style="color:blue;">-&gt; </span>n
    | String n  <span style="color:blue;">-&gt; let </span>success, result = System.Int32.TryParse n
                   <span style="color:blue;">if </span>success
                       <span style="color:blue;">then </span>result
                       <span style="color:blue;">else </span>throw (TypeMismatch(<span style="color:maroon;">&quot;number&quot;</span>, String n))
    | List [n]  <span style="color:blue;">-&gt; </span>unpackNum n
    | notNumber <span style="color:blue;">-&gt; </span>throw (TypeMismatch(<span style="color:maroon;">&quot;number&quot;</span>, notNumber))

<span style="color:blue;">let rec </span>unpackStr = <span style="color:blue;">function
    </span>| String s <span style="color:blue;">-&gt; </span>s
    | Number n <span style="color:blue;">-&gt; </span>n.ToString()
    | Bool b   <span style="color:blue;">-&gt; </span>b.ToString()
    | List [s]  <span style="color:blue;">-&gt; </span>unpackStr s
    | noString <span style="color:blue;">-&gt; </span>throw (TypeMismatch(<span style="color:maroon;">&quot;string&quot;</span>, noString))

<span style="color:blue;">let rec </span>unpackBool = <span style="color:blue;">function
    </span>| Bool b <span style="color:blue;">-&gt; </span>b
    | List [b]  <span style="color:blue;">-&gt; </span>unpackBool b
    | noBool <span style="color:blue;">-&gt; </span>throw (TypeMismatch(<span style="color:maroon;">&quot;boolean&quot;</span>, noBool))</pre>
<p>Now back to the list of primitive operators, there are the signature LISP operators <em>car</em>, <em>cdr</em> and <em>cons</em>. Just understanding the first line for each function should be enough to get an idea of what they do.</p>
<pre class="code"><span style="color:blue;">let </span>car = <span style="color:blue;">function
    </span>| [List (x :: _)] <span style="color:blue;">-&gt; </span>x
    | [DottedList (x :: _, _)] <span style="color:blue;">-&gt; </span>x
    | [badArg] <span style="color:blue;">-&gt; </span>throw (TypeMismatch(<span style="color:maroon;">&quot;pair&quot;</span>, badArg))
    | badArgList <span style="color:blue;">-&gt; </span>throw (NumArgs(1, badArgList))

<span style="color:blue;">let </span>cdr = <span style="color:blue;">function
    </span>| [List (x :: xs)] <span style="color:blue;">-&gt; </span>List xs
    | [DottedList ([xs], x)] <span style="color:blue;">-&gt; </span>x
    | [DottedList ((_ :: xs), x)] <span style="color:blue;">-&gt; </span>DottedList (xs, x)
    | [badArg] <span style="color:blue;">-&gt; </span>throw (TypeMismatch(<span style="color:maroon;">&quot;pair&quot;</span>, badArg))
    | badArgList <span style="color:blue;">-&gt; </span>throw (NumArgs(1, badArgList))

<span style="color:blue;">let </span>cons = <span style="color:blue;">function
    </span>| [x; List xs] <span style="color:blue;">-&gt; </span>List (x :: xs)
    | [x; DottedList (xs, xlast)] <span style="color:blue;">-&gt; </span>DottedList (x :: xs, xlast)
    | [x1; x2] <span style="color:blue;">-&gt; </span>DottedList([x1], x2)
    | badArgList <span style="color:blue;">-&gt; </span>throw (NumArgs(2, badArgList))</pre>
<p>We then need to work our way to implement <em>eqv</em> (aka <em>eq?</em> in Scheme). We first define a function that tests that two LispVal are the same. It should be pretty self explanatory (the list piece is kind of cute).</p>
<pre class="code">    <span style="color:blue;">let rec </span>eqvPrim e1 e2 =
        <span style="color:blue;">match </span>e1, e2 <span style="color:blue;">with
        </span>| (Bool b1, Bool b2) <span style="color:blue;">-&gt; </span>b1 = b2
        | (Number n1, Number n2) <span style="color:blue;">-&gt; </span>n1 = n2
        | (String s1, String s2) <span style="color:blue;">-&gt; </span>s1 = s2
        | (Atom a1, Atom a2) <span style="color:blue;">-&gt; </span>a1 = a2
        | (DottedList (xs, x), DottedList(ys, y)) <span style="color:blue;">-&gt; </span>eqvPrim (List (xs @ [x])) (List (ys @ [y]))
        | (List l1, List l2) <span style="color:blue;">-&gt; </span>l1.Length = l2.Length &amp;&amp; List.forall2 eqvPrim l1 l2
        | _ <span style="color:blue;">-&gt; false
</span></pre>
<p>Now we wrap the result in a <em>Bool</em>. Doing it this way avoid repeating the wrapping in each single line of <em>eqvPrim</em> (thanks to Tobias for spotting this refactoring).</p>
<pre class="code"><span style="color:blue;">let </span>eqv = <span style="color:blue;">function
          </span>| [e1; e2] <span style="color:blue;">-&gt; </span>Bool (eqvPrim e1 e2)
          | badArgList <span style="color:blue;">-&gt; </span>throw (NumArgs (2, badArgList))</pre>
<p><em>Equal?</em> checks if there is any unpacking scheme that can be used to test equality of the two elements of a two element list.</p>
<pre class="code"><span style="color:blue;">let </span>equal = <span style="color:blue;">function
    </span>| [arg1; arg2] <span style="color:blue;">-&gt;
        let </span>unpackEqual = numUnpackEq arg1 arg2 ||
                          strUnpackEq arg1 arg2 ||
                          boolUnpackEq arg1 arg2
        Bool (eqvPrim arg1 arg2 || unpackEqual)
    | argsList <span style="color:blue;">-&gt; </span>throw (NumArgs(2, argsList))</pre>
<p>We need to define equality of packed primitive types. We do it nicely below.</p>
<pre class="code"><span style="color:blue;">let </span>tryUnpacker (unpack : LispVal <span style="color:blue;">-&gt; </span>'a) (op : 'a <span style="color:blue;">-&gt; </span>'a <span style="color:blue;">-&gt; </span>bool) arg1 arg2 =
    <span style="color:blue;">try </span>op (unpack arg1) (unpack arg2) <span style="color:blue;">with </span>_ <span style="color:blue;">-&gt; false

let </span>numUnpackEq = tryUnpacker unpackNum (=)
<span style="color:blue;">let </span>strUnpackEq = tryUnpacker unpackStr (=)
<span style="color:blue;">let </span>boolUnpackEq = tryUnpacker unpackBool (=)</pre>
<p>The <em>apply</em> statement maps more or less directly to our <em>apply</em> function.</p>
<pre class="code">applyProc = <span style="color:blue;">function
            </span>| [func; List args] <span style="color:blue;">-&gt; </span>apply func args
            | func :: args <span style="color:blue;">-&gt; </span>apply func args
            | [] <span style="color:blue;">-&gt; </span>throw (Default(<span style="color:maroon;">&quot;Expecting a function, got an empty list&quot;</span>))</pre>
<p>And we are left with the I/O processing functions. We are simply wrapping a FileStream in a Port.</p>
<pre class="code">    <span style="color:blue;">let </span>makePort fileAccess = fileIOFunction (<span style="color:blue;">fun </span>fileName <span style="color:blue;">-&gt;
                                </span>File.Open(fileName,FileMode.OpenOrCreate, fileAccess) |&gt; Port)

    <span style="color:blue;">let </span>closePort = <span style="color:blue;">function
                    </span>| [Port(port)] <span style="color:blue;">-&gt; </span>port.Close() ; Bool <span style="color:blue;">true
                    </span>| _ <span style="color:blue;">-&gt; </span>Bool <span style="color:blue;">false
</span></pre>
<p>We then can read and write from it. Notice how the lack of arguments makes us do it from the standard Console.</p>
<pre class="code"><span style="color:blue;">let rec </span>readProc port =
    <span style="color:blue;">let </span>parseReader (reader:TextReader) = reader.ReadLine() |&gt; readExpr
    <span style="color:blue;">match </span>port <span style="color:blue;">with
       </span>| [] <span style="color:blue;">-&gt; </span>parseReader(System.Console.In)
       | [Port(port)] <span style="color:blue;">-&gt;
            use </span>reader = <span style="color:blue;">new </span>StreamReader(port)
            parseReader (reader)
       | args <span style="color:blue;">-&gt; </span>throw (NumArgs(1, args))

<span style="color:blue;">let </span>writeProc objPort =
    <span style="color:blue;">let </span>write obj (writer: TextWriter) = writer.Write(showVal obj) ; Bool <span style="color:blue;">true
    match </span>objPort <span style="color:blue;">with
    </span>| [obj] <span style="color:blue;">-&gt; </span>write obj (System.Console.Out)
    | [obj ; Port(port)] <span style="color:blue;">-&gt;
        use </span>writer = <span style="color:blue;">new </span>StreamWriter(port)
        write obj writer
    | args <span style="color:blue;">-&gt; </span>throw (NumArgs(1, args))</pre>
<p>There you go. A full evaluator in two blog posts!! Next up, the parser.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/lucabolognese.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/lucabolognese.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/lucabolognese.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/lucabolognese.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/lucabolognese.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/lucabolognese.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/lucabolognese.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/lucabolognese.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/lucabolognese.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/lucabolognese.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/lucabolognese.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/lucabolognese.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/lucabolognese.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/lucabolognese.wordpress.com/48/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lucabolognese.wordpress.com&amp;blog=11987081&amp;post=48&amp;subd=lucabolognese&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://lucabolognese.wordpress.com/2011/07/29/write-yourself-a-scheme-in-48-hours-part-iv-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/075c2430e54d24df1a0498bc1e7b746c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">lucabol</media:title>
		</media:content>
	</item>
		<item>
		<title>Write Yourself a Scheme in 48 Hours in F# &#8211; Part IV</title>
		<link>http://lucabolognese.wordpress.com/2011/07/22/write-yourself-a-scheme-in-48-hours-part-iv/</link>
		<comments>http://lucabolognese.wordpress.com/2011/07/22/write-yourself-a-scheme-in-48-hours-part-iv/#comments</comments>
		<pubDate>Fri, 22 Jul 2011 07:52:00 +0000</pubDate>
		<dc:creator>lucabol</dc:creator>
				<category><![CDATA[F#]]></category>

		<guid isPermaLink="false">https://lucabolognese.wordpress.com/2011/07/23/write-yourself-a-scheme-in-48-hours-part-iv/</guid>
		<description><![CDATA[It is the evaluator turn. It is a big file, let’s see if I can fit it in a single post. Aptly enough, the most important function is called eval. eval env = function &#124; String _ as v -&#62; v &#124; Number _ as v -&#62; v &#124; Bool _ as v -&#62; v [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lucabolognese.wordpress.com&amp;blog=11987081&amp;post=45&amp;subd=lucabolognese&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>It is the evaluator turn. It is a big file, let’s see if I can fit it in a single post.</p>
<p>Aptly enough, the most important function is called <em>eval</em>.</p>
<pre class="code">eval env = <span style="color:blue;">function
</span>| String _ <span style="color:blue;">as </span>v <span style="color:blue;">-&gt; </span>v
| Number _ <span style="color:blue;">as </span>v <span style="color:blue;">-&gt; </span>v
| Bool _ <span style="color:blue;">as </span>v <span style="color:blue;">-&gt; </span>v
| Atom var <span style="color:blue;">-&gt; </span>getVar var env
| List [Atom <span style="color:maroon;">&quot;quote&quot;</span>; v] <span style="color:blue;">-&gt; </span>v
| List [Atom <span style="color:maroon;">&quot;if&quot;</span>; pred; conseq; alt] <span style="color:blue;">-&gt; </span>evalIf env pred conseq alt
| List [Atom <span style="color:maroon;">&quot;load&quot;</span>; fileName] <span style="color:blue;">-&gt; </span>load [fileName] |&gt; List.map (eval env) |&gt; last
| List [Atom <span style="color:maroon;">&quot;set!&quot; </span>; Atom var ; form] <span style="color:blue;">-&gt; </span>env |&gt; setVar var (eval env form)
| List [Atom <span style="color:maroon;">&quot;define&quot;</span>; Atom var; form] <span style="color:blue;">-&gt; </span>define env var (eval env form)
| List (Atom <span style="color:maroon;">&quot;define&quot; </span>:: (List (Atom var :: parms) :: body)) <span style="color:blue;">-&gt;
    </span>makeNormalFunc env parms body |&gt; define env var
| List (Atom <span style="color:maroon;">&quot;define&quot; </span>:: (DottedList ((Atom var :: parms), varargs) :: body)) <span style="color:blue;">-&gt;
    </span>makeVarargs varargs env parms body |&gt; define env var
| List (Atom <span style="color:maroon;">&quot;lambda&quot; </span>:: (List parms :: body)) <span style="color:blue;">-&gt; </span>makeNormalFunc env parms body
| List (Atom <span style="color:maroon;">&quot;lambda&quot; </span>:: (DottedList(parms, varargs) :: body)) <span style="color:blue;">-&gt; </span>makeVarargs varargs env parms body
| List (Atom <span style="color:maroon;">&quot;lambda&quot; </span>:: ((Atom _) <span style="color:blue;">as </span>varargs :: body)) <span style="color:blue;">-&gt; </span>makeVarargs varargs env [] body
| List (func :: args) <span style="color:blue;">-&gt;
    let </span>f = eval env func
    <span style="color:blue;">let </span>argVals = List.map (eval env) args
    apply f argVals
| badForm <span style="color:blue;">-&gt; </span>throw (BadSpecialForm(<span style="color:maroon;">&quot;Unrecognized special form&quot;</span>, badForm))</pre>
<p>This is the core of the evaluator. It takes as an input the LispVal generated by the parser and an environment and returns a LispVal that is the result of the reduction. As a side effect, it occasionally modify the environment. I carefully crafted the previous phrase to maximize the discomfort&#160; of the functional programmers tuned in. Such fun <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>More seriously (?), here is what it does:</p>
<ul>
<li>If it is a String, Number of Bool, just return it </li>
<li>If it is an Atom, return its value </li>
<li>If it is a <em>quote</em>&#160; statement, return what is quoted (read your little schemer manual) </li>
<li>If it is an <em>if</em> statement, evaluate it (see below) </li>
<li>If it is a <em>load</em> statement, load the file (see below) and evaluate each expression using the current environment. Return the last expression in the file </li>
<li>If it is a <em>set!</em>, set the value of the passed variable to the evaluated form </li>
<li>If it is a <em>define</em>, do almost the same as above (except that you don’t throw if the variable doesn’t exist, but you create it) </li>
<li>If it is a <em>define</em>&#160; that defines a function (it has that shape), create a ‘function slot’ in the environment. That is a (functionName,&#160; FuncRecord) pair (see below) </li>
<li>If it is a <em>lambda</em>, return the FuncRecord that describe the inline function </li>
<li>If it is a function call, evaluate the expression that describe the function (yes, you can do that in Lisp!!), evaluate the arguments, and apply the function to the arguments </li>
<li>Otherwise, it must be a bad form, throw it back to the calling function to do something meaningful with it </li>
</ul>
<p>We have a bunch of ‘see below’ to take care of. We’ll look at them in order.</p>
<p>First the ‘if’ statement. If the evaluated predicate is Bool(True) evaluate the consequent, otherwise evaluate the alternative. For some reason, I wrote it the other way around.</p>
<pre class="code"><span style="color:blue;">and
    </span><span style="color:green;">// 1a. If the evaluation of the pred is false evaluate alt, else evaluate cons
    </span>evalIf env pred conseq alt =
        <span style="color:blue;">match </span>eval env pred <span style="color:blue;">with
        </span>| Bool(<span style="color:blue;">false</span>) <span style="color:blue;">-&gt; </span>eval env alt
        | _ <span style="color:blue;">-&gt; </span>eval env conseq</pre>
<p>Then there is the <em>load</em> function. It reads all the test and gets out the list of LispVal contained in it.</p>
<pre class="code"><span style="color:blue;">let </span>load = fileIOFunction (<span style="color:blue;">fun </span>fileName <span style="color:blue;">-&gt; </span>File.ReadAllText(fileName)
                                           |&gt; readExprList)</pre>
<p><em>ReadExprList</em> is part of the parser. We’ll look at it then. Sufficient here to say that it takes a <em>string</em> and returns a list of <em>LispVal</em>.</p>
<p><em>FileIOFunction</em> just encapsulates a common pattern in all the file access functions. I don’t like such mechanical factorization of methods, without any real reusability outside the immediate surroundings of the code. But I like repetition even less.</p>
<pre class="code"><span style="color:blue;">let </span>fileIOFunction func = <span style="color:blue;">function
    </span>| [String fileName] <span style="color:blue;">-&gt; </span>func (fileName)
    | [] <span style="color:blue;">-&gt; </span>throw (IOError(<span style="color:maroon;">&quot;No file name&quot;</span>))
    | args <span style="color:blue;">-&gt; </span>throw (NumArgs(1, args))</pre>
<p>A family of functions create FuncRecord given appropriate parameters. I seem to have lost memory of the contortions related to the last one. If I end up having to work again on this code, I’ll need to figure it out again. Note to myself, please comment this kind of code next time.</p>
<pre class="code"><span style="color:blue;">let </span>makeFunc varargs env parms body =
            Func ({parms = (List.map showVal parms); varargs = varargs; body = body; closure = env})
<span style="color:blue;">let </span>makeNormalFunc = makeFunc None
<span style="color:blue;">let </span>makeVarargs = showVal &gt;&gt; Some &gt;&gt; makeFunc</pre>
<p><em>apply</em> is the other workhorse function in the evaluator.&#160; The best way to understand it is to start from the bottom (where <em>bindVars</em> starts the line). We are binding the arguments and the variable arguments in the closure that has been passed in. We then evaluate the body. But the body is just a list of <em>LispVal</em>, so we just need to evaluate them in sequence and return the result of the last one.</p>
<pre class="code"><span style="color:blue;">and </span>apply func args =
    <span style="color:blue;">match </span>func <span style="color:blue;">with
    </span>| PrimitiveFunc(f) <span style="color:blue;">-&gt; </span>f args
    | Func ({parms = parms; varargs = varargs; body = body; closure = closure}) <span style="color:blue;">-&gt;
        let </span>invalidNonVarargs = args.Length &lt;&gt; parms.Length &amp;&amp; varargs.IsNone
        <span style="color:blue;">let </span>invalidVarargs = args.Length &lt; parms.Length &amp;&amp; varargs.IsSome  

        <span style="color:blue;">if </span>invalidVarargs || invalidNonVarargs
        <span style="color:blue;">then
            </span>throw (NumArgs(parms.Length, args))
        <span style="color:blue;">else
            let </span>remainingArgs = args |&gt; Seq.skip parms.Length |&gt; Seq.toList
            <span style="color:blue;">let </span>evalBody env = body |&gt; List.map (eval env) |&gt; last
            <span style="color:blue;">let rec </span>zip xs1 xs2 acc =
                <span style="color:blue;">match </span>xs1, xs2 <span style="color:blue;">with
                </span>| x1::xs1, x2::xs2 <span style="color:blue;">-&gt; </span>zip xs1 xs2 ((x1, x2)::acc)
                | _ <span style="color:blue;">-&gt; </span>acc
            <span style="color:blue;">let </span>bindVarArgs arg env =
                <span style="color:blue;">match </span>arg <span style="color:blue;">with
                </span>| Some(argName) <span style="color:blue;">-&gt; </span>bindVars [argName, (List remainingArgs)] env
                | None <span style="color:blue;">-&gt; </span>env
            bindVars (zip parms args []) closure
                |&gt; bindVarArgs varargs
                |&gt; evalBody
    | funcName <span style="color:blue;">-&gt; </span>throw (NotFunction(<span style="color:maroon;">&quot;Expecting a function, getting &quot;</span>, showVal funcName))</pre>
<p>This is enough for one post. Next time we’ll finish the evaluator.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/lucabolognese.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/lucabolognese.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/lucabolognese.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/lucabolognese.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/lucabolognese.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/lucabolognese.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/lucabolognese.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/lucabolognese.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/lucabolognese.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/lucabolognese.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/lucabolognese.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/lucabolognese.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/lucabolognese.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/lucabolognese.wordpress.com/45/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lucabolognese.wordpress.com&amp;blog=11987081&amp;post=45&amp;subd=lucabolognese&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://lucabolognese.wordpress.com/2011/07/22/write-yourself-a-scheme-in-48-hours-part-iv/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/075c2430e54d24df1a0498bc1e7b746c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">lucabol</media:title>
		</media:content>
	</item>
		<item>
		<title>Write Yourself a Scheme in 48 Hours in F# &#8211; Part III</title>
		<link>http://lucabolognese.wordpress.com/2011/07/15/write-yourself-a-scheme-in-48-hours-in-f-part-iii/</link>
		<comments>http://lucabolognese.wordpress.com/2011/07/15/write-yourself-a-scheme-in-48-hours-in-f-part-iii/#comments</comments>
		<pubDate>Fri, 15 Jul 2011 07:12:00 +0000</pubDate>
		<dc:creator>lucabol</dc:creator>
				<category><![CDATA[F#]]></category>

		<guid isPermaLink="false">https://lucabolognese.wordpress.com/2011/07/16/write-yourself-a-scheme-in-48-hours-in-f-part-iii/</guid>
		<description><![CDATA[Very often my code ends up having the following form: parse an input to create an intermediate data structure and evaluate the structure to produce an output. Strangely, many years ago, when my code was object oriented, that wasn’t the case. Or at least I wasn’t explicitly aware of it. When you write an interpreter [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lucabolognese.wordpress.com&amp;blog=11987081&amp;post=44&amp;subd=lucabolognese&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Very often my code ends up having the following form: parse an input to create an intermediate data structure and evaluate the structure to produce an output. Strangely, many years ago, when my code was object oriented, that wasn’t the case. Or at least I wasn’t explicitly aware of it.</p>
<p>When you write an interpreter or a compiler, things always work out like that, but I see the same pattern in almost everything I produce: from financial backtesting to chart libraries. Sometimes when, out of laziness or stupidity, I forego the intermediate structure, I end up in the end having to retrofit it in. Simply processing input and generating output at the same time rarely cuts it. But it is tempting because you get going pretty fast and I’m tricked into it occasionally.</p>
<p>Hence the first thing that I find myself reasoning about is often the particular form of such intermediate structure. In this case it looks like the following:</p>
<pre class="code"><span style="color:blue;">type </span>Env = (string * LispVal ref) list ref

<span style="color:blue;">and </span>FuncRecord = { parms: string list; varargs: string option; body: LispVal list; closure: Env}

<span style="color:blue;">and </span>LispVal =
    | Atom <span style="color:blue;">of </span>string
    | List <span style="color:blue;">of </span>LispVal list
    | DottedList <span style="color:blue;">of </span>LispVal list * LispVal
    | Number <span style="color:blue;">of </span>int
    | String <span style="color:blue;">of </span>string
    | Bool <span style="color:blue;">of </span>bool
    | PrimitiveFunc <span style="color:blue;">of </span>(LispVal list <span style="color:blue;">-&gt; </span>LispVal)
    | Func <span style="color:blue;">of </span>FuncRecord
    | Port <span style="color:blue;">of </span>System.IO.FileStream</pre>
<p>This <em>LispVal</em> structure has one constructor for each kind of expression (production) that is allowed in Scheme. Or at least that ones I support …</p>
<p>It is important that each one stores all the information that is necessary for the evaluator to evaluate the expression. No more, no less. Here is a brief description:</p>
<ul>
<li><strong>Atom</strong>: it is a kind of a constant in Scheme. This is probably the worst definition ever given for it. Please read about it in your little schemer book.</li>
<li><strong>List</strong>: is the main Scheme type. It represents a list of expressions.</li>
<li><strong>DottedList</strong>: this is the bizarre Scheme way to pass optional parameters</li>
<li><strong>Number</strong>: is a number <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  You will discover which kind of number when we talk about the parser</li>
<li><strong>String</strong> : is a string</li>
<li><strong>Bool</strong>: #t, #f</li>
<li><strong>PrimitiveFunc</strong>: is the representation for the primitive operators/functions that are burned into the interpreter. It is just a function that takes a list of LispVal and returns a LispVal</li>
<li><strong>Func</strong>: is a user defined function. Notice that the body of it is simply a list of LispVal. This is why LISP is so powerful. Also notice that a closure gets passed to it for the ‘captured’ variables.</li>
<li><strong>Port</strong>: is a slightly goofy representation of an in/out stream</li>
<li>Anything else (i.e. macros) is not supported, but this would be the first piece to change if they were.</li>
</ul>
<p>The only remaining code to address is: </p>
<p><span style="color:blue;">type </span>Env = (string * LispVal ref) list ref </p>
<p>This is the symbol table and it is ugly. It is not multithread safe either. But it works and it is close enough to the Haskell version so I decided to retain it. A proper code review would ‘strongly suggest’ rewriting the code to pass it around to each function instead of using ‘ref’ or using the state monad encapsulated in a computation expression. Any of these solutions is left as an exercise to the reader (use the testcases to validate that you get it right).</p>
<p>We could go in many different direction from here. We could talk about:</p>
<ul>
<li>The evaluator</li>
<li>The parser</li>
<li>The symbol table</li>
<li>Error handling</li>
</ul>
<p>To keep with the top – down approach I’ve been espousing. I’ll probably talk about the evaluator next.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/lucabolognese.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/lucabolognese.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/lucabolognese.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/lucabolognese.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/lucabolognese.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/lucabolognese.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/lucabolognese.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/lucabolognese.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/lucabolognese.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/lucabolognese.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/lucabolognese.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/lucabolognese.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/lucabolognese.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/lucabolognese.wordpress.com/44/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lucabolognese.wordpress.com&amp;blog=11987081&amp;post=44&amp;subd=lucabolognese&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://lucabolognese.wordpress.com/2011/07/15/write-yourself-a-scheme-in-48-hours-in-f-part-iii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/075c2430e54d24df1a0498bc1e7b746c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">lucabol</media:title>
		</media:content>
	</item>
	</channel>
</rss>
