<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>LAMBDAPHONE &#187; EsotericLanguages</title>
	<atom:link href="http://coder.bsimmons.name/blog/tag/esotericlanguages/feed/" rel="self" type="application/rss+xml" />
	<link>http://coder.bsimmons.name/blog</link>
	<description>fragmentary ideas  ䷿  intellectual what-nots  ䷷  and haskell programming  ䷴</description>
	<lastBuildDate>Sun, 29 Jan 2012 17:24:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Designing a Module for Combinatory Logic in Haskell</title>
		<link>http://coder.bsimmons.name/blog/2010/09/designing-a-module-for-combinatory-logic-in-haskell/</link>
		<comments>http://coder.bsimmons.name/blog/2010/09/designing-a-module-for-combinatory-logic-in-haskell/#comments</comments>
		<pubDate>Sat, 04 Sep 2010 21:28:43 +0000</pubDate>
		<dc:creator>jberryman</dc:creator>
				<category><![CDATA[haskell]]></category>
		<category><![CDATA[combinator]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[EsotericLanguages]]></category>
		<category><![CDATA[NumericTypes]]></category>
		<category><![CDATA[release]]></category>
		<category><![CDATA[sequences]]></category>
		<category><![CDATA[theory]]></category>

		<guid isPermaLink="false">http://coder.bsimmons.name/blog/?p=471</guid>
		<description><![CDATA[<p>Like the <a href="http://en.wikipedia.org/wiki/Lambda_calculus">Lambda Calculus</a> (on which Lisp is based), Combinatory Logic is a formal system that can be used to model and study computation. What makes it fascinating is that it is as powerful as the (already simple) lambda&#8230; <a href="http://coder.bsimmons.name/blog/2010/09/designing-a-module-for-combinatory-logic-in-haskell/" class="read_more">   [ R E A D &#124; M O R E ]</a></p>]]></description>
			<content:encoded><![CDATA[<p>Like the <a href="http://en.wikipedia.org/wiki/Lambda_calculus">Lambda Calculus</a> (on which Lisp is based), Combinatory Logic is a formal system that can be used to model and study computation. What makes it fascinating is that it is as powerful as the (already simple) lambda calculus, but has no need for the variables that LC requires to perform substitution!</p>
<p>Here I show how we can use Haskell&#8217;s type classes and other language features to model the Combinator Calculus. The goals of this module were:</p>
<ol>
<li>     don&#8217;t force any one evaluation strategy </li>
<li>     allow users to define new combinators without exposing the implementation</li>
<li>    allow new combinators to be defined in terms of other already-defined combinators.</li>
<li>    discourage creation of non-sensical combinator expressions, or possible mis-use of the module</li>
<li>    hide existential types and anything else exotic</li>
</ol>
<p>If you want to play with it, you can do a:</p>
<blockquote><p>
<code>$> darcs get http://coder.bsimmons.name/code/CombinatorCalculus</code>
</p></blockquote>
<p><span id="more-471"></span></p>
<h2>A Quick Combinator Calculus Primer</h2>
<p>A Combinatory Logic expression consists of a sequence of <em>combinator terms</em> and <em>sub-expressions</em>. Combinator terms are like functions which consume some arguments (themselves combinators or sub-expressions), and return a particular re-arrangement of those arguments. When no term can consume enough arguments to be &#8220;evaluated&#8221; any further, the expression is said to be in <em>normal form</em>.</p>
<p>The <a href="http://en.wikipedia.org/wiki/SKI_combinator_calculus">SKI Combinator Calculus</a> is a system that uses only three combinators (&#8216;S&#8217;, &#8216;K&#8217; and &#8216;I&#8217;) and forms a Turing Complete system. This means we can express any computation with expressions that look like <code>SK(SSS)I(S(KS)K)I</code>. It can even server as the basis of a turing complete <a href="http://www.madore.org/~david/programs/unlambda/">programming language</a>!</p>
<p>It&#8217;s a really fascinating topic, and you can get a great overview from wikipedia&#8217;s excellent <a href="http://en.wikipedia.org/wiki/Combinatory_logic">Combinatory Logic article</a>.</p>
<h2>The Design of the Module</h2>
<p>All terms in combinatory logic take some arguments and form a new expression from them, but different combinators transform their arguments in different ways. </p>
<p>We can express this in Haskell by creating a typeclass with methods that describe the behavior of a combinator. Then we can make a type (corresponding to a specific Combinator term) an instance of our class to define its behavior.</p>
<h3>The <code>Combinator</code> Class</h3>
<p>Here is how we define our <code>Combinator</code> class:</p>
<p><blockquote class="vimblock"><br />
&nbsp;<span class="Comment">-- a Class for combinators. Its methods represent a combinator's behavior on </span><br />
&nbsp;<span class="Comment">-- its arguments:</span><br />
<span class="Type">class</span>&nbsp;(Show&nbsp;c)<span class="Statement">=&gt;</span>&nbsp;Combinator&nbsp;c&nbsp;<span class="Type">where</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;takesArgs&nbsp;<span class="Statement">::</span>&nbsp;c&nbsp;<span class="Statement">-&gt;</span>&nbsp;Int<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;applyArgs&nbsp;<span class="Statement">::</span>&nbsp;c&nbsp;<span class="Statement">-&gt;</span>&nbsp;[Term]&nbsp;<span class="Statement">-&gt;</span>&nbsp;Expr<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp; <span class="Comment">-- HIDDEN METHOD: EXPORTED AS A FUNCTION:</span><br />
&nbsp;&nbsp;&nbsp;&nbsp; <span class="Comment">-- if a combinator takes 0 args, we assume it can be evaluated and we</span><br />
&nbsp;&nbsp;&nbsp;&nbsp; <span class="Comment">-- say it is not in Normal Form:</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;isNormal&nbsp;<span class="Statement">::</span>&nbsp;c&nbsp;<span class="Statement">-&gt;</span>&nbsp;Bool<br />
&nbsp;&nbsp;&nbsp;&nbsp;isNormal&nbsp;<span class="Statement">=</span>&nbsp;(<span class="Statement">&gt;</span>&nbsp;<span class="Constant">0</span>)&nbsp;<span class="Statement">.</span>&nbsp;takesArgs<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp; <span class="Comment">-- HIDDEN METHOD: user defined combinators are placed in a black box:</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;toTerm&nbsp;<span class="Statement">::</span>&nbsp;c&nbsp;<span class="Statement">-&gt;</span>&nbsp;Term<br />
&nbsp;&nbsp;&nbsp;&nbsp;toTerm&nbsp;<span class="Statement">=</span>&nbsp;Term<br />
<br /></blockquote></p>
<p>The first two methods are the only ones exported to users of the module to define an instance of <code>Combinator</code>. <code>takesArgs</code> is used to indicate how many arguments this particular combinator requires before it can be evaluated. </p>
<p><code>applyArgs</code> actually performs the transformation of those arguments; it is given a list of terms (of length equal to `takesArgs`) and returns a new expression (type Expr).</p>
<p>The third method is used to check if an expression is in normal form. It is exported <em>as a regular function</em>, meaning that users can use it in their code but <em>cannot</em> use it in their own instance declarations. Instead the default value would apply.</p>
<p>The fourth method `toTerm` is used internally to encapsulate some Combinator class item in a `Term` data type. We use this when composing combinator expressions, in order to preserve the tree structure of the expression. (see below for more on this)</p>
<h3>The <code>Term</code> and <code>Expr</code> types</h3>
<p>The module defines two new datatypes, <em>both</em> of which are instances of `Combinator`.	</p>
<p>`Expr` is a <code>newtype</code> wrapper around <code>Seq Term</code>, but we don&#8217;t export the implementation so we can change this if we don&#8217;t want to use <a href="http://hackage.haskell.org/packages/archive/containers/0.2.0.1/doc/html/Data-Sequence.html">Sequences</a>:</p>
<p><blockquote class="vimblock"><br />
<span class="Type">newtype</span>&nbsp;Expr&nbsp;<span class="Statement">=</span>&nbsp;Expr&nbsp;{&nbsp;combSeq&nbsp;<span class="Statement">::</span>&nbsp;Seq&nbsp;Term&nbsp;}<br />
<br /></blockquote></p>
<p>It simply represents a CL expression as a sequence of terms. Making it an instance of `Combinator` expresses the notion that an expression can be thought of as a combinator term whenever the need arises. In fact the distinction is simply one of <a href="http://en.wikipedia.org/wiki/Evaluation_strategy">evaluation strategy</a>.</p>
<p>`Term` is a wrapper for Combinator terms and sub-expressions. It is mutually-recursive with `Expr` and the two together form the tree structure of a combinator expression:</p>
<p><blockquote class="vimblock"><br />
<span class="Type">data</span>&nbsp;Term&nbsp;<span class="Statement">=</span>&nbsp;forall&nbsp;c<span class="Statement">.</span>&nbsp;Combinator&nbsp;c&nbsp;<span class="Statement">=&gt;</span>&nbsp;Term&nbsp;{&nbsp;unbox&nbsp;<span class="Statement">::</span>&nbsp;c&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="Statement">|</span>&nbsp;SubExpr&nbsp;&nbsp; {&nbsp;subExpr&nbsp;<span class="Statement">::</span>&nbsp;Expr&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="Statement">|</span>&nbsp;NamedExpr&nbsp;{&nbsp;subExpr&nbsp;<span class="Statement">::</span>&nbsp;Expr,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name&nbsp;&nbsp;&nbsp;&nbsp;<span class="Statement">::</span>&nbsp;String&nbsp;}<br />
<br /></blockquote></p>
<p>All three constructors are hidden from the user. `SubExpr` simply holds an `Expr` type and represents a parenthesized sub-expression within the top level combinator sequence. `NamedExpr` is identical except that it also contains a String, which will be returned when `show` is called on that constructor. Here is the `Show` instance for `Term`:</p>
<p><blockquote class="vimblock"><br />
<span class="Type">instance</span>&nbsp;Show&nbsp;Term&nbsp;<span class="Type">where</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;show&nbsp;(NamedExpr&nbsp;_&nbsp;n)&nbsp;<span class="Statement">=</span>&nbsp;n<br />
&nbsp;&nbsp;&nbsp;&nbsp;show&nbsp;(SubExpr&nbsp;e)&nbsp;&nbsp;&nbsp;&nbsp; <span class="Statement">=</span>&nbsp;<span class="Constant">&quot;(&quot;</span><span class="Statement">++</span>&nbsp;show&nbsp;e&nbsp;<span class="Statement">++</span><span class="Constant">&quot;)&quot;</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;show&nbsp;(Term&nbsp;t)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="Statement">=</span>&nbsp;show&nbsp;t<br />
<br /></blockquote></p>
<p>The remaining constructor is the most interesting: the `Term` constructor houses an <a href="http://en.wikibooks.org/wiki/Haskell/Existentially_quantified_types">existentially-quantified type</a> and can hold <em>any</em> type value of the `Combinator` class. </p>
<p>Notice that the type variable does not appear on the left side of the <code>=</code> in the type declaration. What this means in practical terms is that the `Term` constructor becomes a &#8220;black box&#8221;; once a value is inside, the only things we can &#8220;do with it&#8221; are to apply polymorphic Combinator class functions and methods.</p>
<p>And that&#8217;s why we have the internal `toTerm` method; it allows us to wrap sub-expressions in the `SubExpr` constructor, preserving the tree structure of an expression as we compose it. Without it we would either need to export multiple functions for composing a term with an expression, an expression with an expression, a term with a term, and an expression with a term. Ugly!</p>
<h3>Exported functions</h3>
<p>Our types and classes allow us to do some neat stuff. As mentioned above, we can compose expressions using a single haskell infix function. Since all the nice ASCII combinators were taken (and since I&#8217;m the only one using this) I decided to go with a Unicode symbol, the middle dot, for composition:</p>
<p><blockquote class="vimblock"><br />
&nbsp;<span class="Comment">-- operator for composing combinator expressions. This is the Middle Dot, </span><br />
&nbsp;<span class="Comment">-- unicode character 00B7. It is easily inserted in vim using the digraph: </span><br />
&nbsp;<span class="Comment">--&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Ctrl-K '.' 'M'</span><br />
<span class="PreProc">infixl</span>&nbsp;<span class="Constant">7</span>&nbsp;·<br />
<br /></blockquote></p>
<p>&#8230;and the implementation:</p>
<p><blockquote class="vimblock"><br />
&nbsp;<span class="Comment">-- for composing Combinator expressions:</span><br />
(·)&nbsp;<span class="Statement">::</span>&nbsp;(Combinator&nbsp;c1,Combinator&nbsp;c2)<span class="Statement">=&gt;</span>&nbsp;c1&nbsp;<span class="Statement">-&gt;</span>&nbsp;c2&nbsp;<span class="Statement">-&gt;</span>&nbsp;Expr<br />
c1&nbsp;· (toTerm<span class="Statement">-&gt;</span>t2)&nbsp;<span class="Statement">=</span>&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp; <span class="Statement">case</span>&nbsp;toTerm&nbsp;c1&nbsp;<span class="Statement">of</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(SubExpr&nbsp;e)&nbsp;<span class="Statement">-&gt;</span>&nbsp;e&nbsp;<span class="Statement">`appendTerms`</span>&nbsp;singleton&nbsp;t2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="Comment">-- named expression or bare Term start a new sub-expression:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="Statement">-&gt;</span>&nbsp;Expr&nbsp;(singleton&nbsp;t1&nbsp;<span class="Statement">|&gt;</span>&nbsp;t2)<br />
<br /></blockquote></p>
<p>We also have the ability to define new combinators in terms of other combinators using the `named` function. Here is an example showing how `I` can be defined in terms of `S` and `K` and used to form a Combinator Calculus equivalent of the <a href="http://en.wikipedia.org/wiki/Church_numeral">Church Numeral</a> 2:</p>
<p><blockquote class="vimblock"><br />
<span class="lnr">60 </span>two&nbsp;<span class="Statement">=</span>&nbsp;S·(S·(K·S)·K)·cI<br />
<span class="lnr">61 </span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="Type">where</span>&nbsp;cI&nbsp;<span class="Statement">=</span>&nbsp;(S&nbsp;· K&nbsp;· K)&nbsp;&nbsp;<span class="Statement">`named`</span>&nbsp;<span class="Constant">&quot;I&quot;</span><br />
<br /></blockquote></p>
<h2>Playing with it</h2>
<p>The evaluation function exported in the module was written fairly hastily, but it is good enough to let us play with evaluating expressions.</p>
<p>I&#8217;ve run out of time on this post, but you can grab the darcs repo above and run the &#8216;main&#8217; function in `Examples.hs` and check out the source for more info. I&#8217;ll maybe show some cool examples in a later post.</p>
<p>Thanks for reading!</p>
]]></content:encoded>
			<wfw:commentRss>http://coder.bsimmons.name/blog/2010/09/designing-a-module-for-combinatory-logic-in-haskell/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The Gift of Inconsistency</title>
		<link>http://coder.bsimmons.name/blog/2010/07/the-gift-of-inconsistency/</link>
		<comments>http://coder.bsimmons.name/blog/2010/07/the-gift-of-inconsistency/#comments</comments>
		<pubDate>Thu, 22 Jul 2010 23:35:23 +0000</pubDate>
		<dc:creator>jberryman</dc:creator>
				<category><![CDATA[haskell]]></category>
		<category><![CDATA[EsotericLanguages]]></category>
		<category><![CDATA[LambdaCalculus]]></category>
		<category><![CDATA[laziness]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[theory]]></category>

		<guid isPermaLink="false">http://coder.bsimmons.name/blog/?p=453</guid>
		<description><![CDATA[<p>Computer science is a fascinating and maddening thing. Even the most seemingly-esoteric topics turn out to be fundamental. Go out to the fringes of CS and you find yourself smack in the middle of <a href="http://en.wikipedia.org/wiki/G%C3%B6del%27s_incompleteness_theorems#Minds_and_machines">Philosophy</a>. You try to understand&#8230; <a href="http://coder.bsimmons.name/blog/2010/07/the-gift-of-inconsistency/" class="read_more">   [ R E A D &#124; M O R E ]</a></p>]]></description>
			<content:encoded><![CDATA[<p>Computer science is a fascinating and maddening thing. Even the most seemingly-esoteric topics turn out to be fundamental. Go out to the fringes of CS and you find yourself smack in the middle of <a href="http://en.wikipedia.org/wiki/G%C3%B6del%27s_incompleteness_theorems#Minds_and_machines">Philosophy</a>. You try to understand a single point and you suddenly find yourself embracing everything. </p>
<p>So this post comes from that infinitely-recursive rabbit hole of wikipedia topics I&#8217;ve been falling down for the last few weeks, and you can probably expect a few more like this one; I&#8217;ll try to keep focused.</p>
<p>We begin (as do All Good Things) with the <a href="http://en.wikipedia.org/wiki/Untyped_lambda_calculus">Untyped Lambda Calculus</a>:<br />
<span id="more-453"></span></p>
<h2>The System</h2>
<p>The lambda calculus, invented by <a href="http://en.wikipedia.org/wiki/Alonzo_Church">Alonzo Church</a>, is a formal system for modeling functional computation and logic. It has an extraordinarily simple set of semantics, has no notion of data as a separate idea, and yet (as was proven later) is Turing complete. It serves as the skeleton and sinew of the Lisp and Scheme programming languages.</p>
<p>At its invention it seems that the system sat right at the crossroads of formal logic, mathematics and computational theory and could embrace all three fields. But it quickly became apparent that there were apparent flaws with the Lambda Calculus as a universal system.</p>
<h2>The Paradox</h2>
<p>In 1935 it was shown that the lambda calculus was <em>logically inconsistent</em> by the <a href="http://en.wikipedia.org/wiki/Kleene%E2%80%93Rosser_paradox">Kleene–Rosser paradox</a>. The &#8220;paradox&#8221; is an apparently self-contradictory lambda expression, and goes like this:</p>
<p>We have a lambda calculus expression we will call <code>'k'</code>, which takes an argument, applies it to to itself, and negates (the ¬ symbol) the result (for some undefined, logical definition of &#8220;negate&#8221;). </p>
<blockquote><p>
k = λx.( ¬ (x x))
</p></blockquote>
<p>Okay, no problem. But look what happens when we apply the function <code>'k'</code> to itself:</p>
<blockquote><p>
k k = λx.( ¬ (x x)) k<br />
k k = ¬ (k k)
</p></blockquote>
<p>After we reduce the expression, we seem to be saying that <code>(k k)</code> is equal to the opposite of itself, a self-contradictory statement.</p>
<p>To address this apparent shortcoming of his system, Church went on to define a <a href="http://en.wikipedia.org/wiki/Simply_typed_lambda_calculus">typed lambda calculus</a>. This new system differentiated between function types and simple terms in order to disallow these kinds of problematic expressions.</p>
<p>But as with many paradoxes, this one was really an issue of perspective&#8230;</p>
<h2>The Foundation</h2>
<p>Church&#8217;s new &#8220;Simply Typed Lambda Calculus&#8221; had an interesting property that the untyped lambda calculus didn&#8217;t have: the <a href="http://en.wikipedia.org/wiki/Strongly_normalizing">normalization property</a>.</p>
<p>This more restricted version of the Lambda Calculus was &#8220;strongly normalizing&#8221;, meaning that any expression could be reduced to some <em>normal form</em> (i.e. a form that cannot be reduced further) through some sequence of rewrite/reduction steps. In programming terms, this means that every expression in the <em>typed</em> lambda calculus is <em>guaranteed to terminate</em>.</p>
<p>That sounds pretty damn useful until you realize that this interesting property also makes Church&#8217;s new typed version of his system <em>not Turing complete</em>! </p>
<p>To prove this is so, consider this: imagine you encoded a program in the typed lambda calculus; it searched for (and halted) when it found an integer that was greater than two, and was <em>not</em> the sum of two primes. </p>
<p>Merely being able to encode such a function would imply the function halted, thus disproving <a href="http://en.wikipedia.org/wiki/Goldbach%27s_conjecture">Goldbach&#8217;s conjecture</a>, one of the great open problems of mathematics. All without even needing to evaluate said expression! The function referred to is of course not expressible in the typed lambda calculus.</p>
<p>It turns out that an apparent logical contradiction was actual the essential secret to computation.</p>
<h3>Embracing Inconsistency</h3>
<p>When you think of the LC as a model of computation, rather than a framework for logical assertions, the Kleene–Rosser paradox becomes what we might call the &#8220;Kleene–Rosser useless function&#8221;. </p>
<p>Let&#8217;s go ahead and express a nearly identical paradox in <a href="http://en.wikipedia.org/wiki/Haskell_%28programming_language%29">haskell</a>:</p>
<blockquote><p>
k :: (Num a) => a<br />
k = negate k
</p></blockquote>
<p>Haskell has no notion of mutability, so what we are saying here is &#8220;k is the negation of itself&#8221;. No logical fallacies or great existential questions here, just an infinite loop (and not a very useful one)! </p>
<p>Let&#8217;s express another similar &#8220;paradox&#8221;, one we can actually <em>use</em>:</p>
<blockquote><p>
babble :: [ String ]<br />
babble = &#8220;blah&#8221; : babble
</p></blockquote>
<p>How can a list be equal to itself with an extra element added? Doesn&#8217;t that imply a paradox? Well I know the runtime isn&#8217;t swayed by that argument, producing an infinite stream of &#8220;blah&#8221;s. We can even <em>use</em> this stream because of haskell&#8217;s lazy evaluation strategy.</p>
<p>But note: it isn&#8217;t laziness that lets us get away with defining paradoxes; it just allows us to make use of some more blatant paradoxical expressions without them blowing up in our faces as soon as we call them.</p>
<h3>Conclusion and Further Thoughts</h3>
<p>Interestingly, there are quite a number of non-turing complete programming languages (or language subsets) that have been created both for theoretical purposes and for practical use. <a href="http://en.wikipedia.org/wiki/Charity_%28programming_language%29">Here</a> <a href="http://en.wikipedia.org/wiki/BlooP_and_FlooP">are</a> <a href="http://www-fp.cs.st-andrews.ac.uk/hume/index.shtml">a few</a>. They seem to owe their existence and usefulness to an environment of turing complete systems.</p>
<p>So does this mean that formal logic is Turing Incomplete? Let me know your thoughts, and please let me know if I harmed any knowledge in the making of this post.</p>
]]></content:encoded>
			<wfw:commentRss>http://coder.bsimmons.name/blog/2010/07/the-gift-of-inconsistency/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Befunge-93 Interpreter on Hackage</title>
		<link>http://coder.bsimmons.name/blog/2010/05/befunge-93-interpreter-on-hackage/</link>
		<comments>http://coder.bsimmons.name/blog/2010/05/befunge-93-interpreter-on-hackage/#comments</comments>
		<pubDate>Thu, 20 May 2010 18:36:42 +0000</pubDate>
		<dc:creator>jberryman</dc:creator>
				<category><![CDATA[haskell]]></category>
		<category><![CDATA[EsotericLanguages]]></category>
		<category><![CDATA[hackage]]></category>
		<category><![CDATA[MonadTransformers]]></category>
		<category><![CDATA[release]]></category>

		<guid isPermaLink="false">http://coder.bsimmons.name/blog/?p=413</guid>
		<description><![CDATA[<p>I&#8217;ve fixed a bug related to upgrading GHC to version 6.12 (thanks to Cale and the folks on haskell-cafe who helped me with the issue) and got my <a href="http://en.wikipedia.org/wiki/Befunge">Befunge-93</a> interpreter up on hackage. The program is written in haskell&#8230; <a href="http://coder.bsimmons.name/blog/2010/05/befunge-93-interpreter-on-hackage/" class="read_more">   [ R E A D &#124; M O R E ]</a></p>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve fixed a bug related to upgrading GHC to version 6.12 (thanks to Cale and the folks on haskell-cafe who helped me with the issue) and got my <a href="http://en.wikipedia.org/wiki/Befunge">Befunge-93</a> interpreter up on hackage. The program is written in haskell (as usual). You should be able to get it soon with a:</p>
<blockquote><pre>$> cabal install Befunge93</pre>
</blockquote>
<p>If you want to read about how I designed it you can check out the source above, or take a look at my <a href="http://coder.bsimmons.name/blog/2010/01/a-befunge-93-interpreter/">previous blog post</a>.</p>
<p>Please report any bugs to me, and I&#8217;m also very interested in patches or suggestions for performance improvements if anyone ends up being interested in this program.</p>
<p><strong>EDIT</strong>: Here is the package page: <a href="http://hackage.haskell.org/package/Befunge93">http://hackage.haskell.org/package/Befunge93</a></p>
]]></content:encoded>
			<wfw:commentRss>http://coder.bsimmons.name/blog/2010/05/befunge-93-interpreter-on-hackage/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Befunge-93 Interpreter</title>
		<link>http://coder.bsimmons.name/blog/2010/01/a-befunge-93-interpreter/</link>
		<comments>http://coder.bsimmons.name/blog/2010/01/a-befunge-93-interpreter/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 21:13:50 +0000</pubDate>
		<dc:creator>jberryman</dc:creator>
				<category><![CDATA[haskell]]></category>
		<category><![CDATA[Array]]></category>
		<category><![CDATA[EsotericLanguages]]></category>
		<category><![CDATA[MonadTransformers]]></category>
		<category><![CDATA[release]]></category>
		<category><![CDATA[state]]></category>

		<guid isPermaLink="false">http://coder.bsimmons.name/blog/?p=302</guid>
		<description><![CDATA[<p><em>I just finished an initial release of an interpreter for the <a href="http://en.wikipedia.org/wiki/Befunge">befunge</a> programming language, based on the <a href="http://catseye.tc/projects/befunge93/">&#8217;93 spec</a>. The project was quite fun! My goal was to produce a well-designed program with performance that didn&#8217;t suck too</em>&#8230; <a href="http://coder.bsimmons.name/blog/2010/01/a-befunge-93-interpreter/" class="read_more">   [ R E A D &#124; M O R E ]</a></p>]]></description>
			<content:encoded><![CDATA[<p><em>I just finished an initial release of an interpreter for the <a href="http://en.wikipedia.org/wiki/Befunge">befunge</a> programming language, based on the <a href="http://catseye.tc/projects/befunge93/">&#8217;93 spec</a>. The project was quite fun! My goal was to produce a well-designed program with performance that didn&#8217;t suck too bad. Here are some highlights:</em></p>
<h2>Design</h2>
<p>I found that writing the core functionality of the interpreter took almost no time, once I settled on the approach I would take. I used a monad transformer for the first time, <code>StateT</code>: </p>
<p><blockquote class="vimblock"><br />
<span class="Type">type</span>&nbsp;REPL&nbsp;a&nbsp;<span class="Statement">=</span>&nbsp;StateT&nbsp;ProgramState&nbsp;<span class="Type">IO</span>&nbsp;a<br />
<br /></blockquote></p>
<p>This let me pass around the state of the computation in the <code>State</code> monad while doing IO actions. This made some potentially-awkward befunge commands really easy to implement.<br />
<span id="more-302"></span><br />
Here is an excerpt from the function <code>execute :: Char -> REPL ()</code> which reads the befunge character at our position and returns the code to execute:</p>
<p><blockquote class="vimblock"><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="Comment">-- Pop value and output as an integer. funge-98 spec calls for</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="Comment">-- integer to be followed by a space, so we'll do that too:</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="Constant">'.'</span>&nbsp;<span class="Statement">-&gt;</span>&nbsp;<span class="Statement">do</span>&nbsp;i&nbsp;<span class="Statement">&lt;-</span>&nbsp;pop<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; liftIO&nbsp;<span class="Statement">$</span>&nbsp;<span class="Identifier">putStr</span>&nbsp;<span class="Statement">$</span>&nbsp;<span class="Identifier">show</span>&nbsp;i&nbsp;<span class="Statement">++</span><span class="Constant">&quot; &quot;</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="Comment">-- Pop value and output as ASCII character</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="Constant">','</span>&nbsp;<span class="Statement">-&gt;</span>&nbsp;<span class="Statement">do</span>&nbsp;i&nbsp;<span class="Statement">&lt;-</span>&nbsp;pop<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; liftIO&nbsp;<span class="Statement">$</span>&nbsp;<span class="Identifier">putChar</span>&nbsp;<span class="Statement">$</span>&nbsp;chr&nbsp;i<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="Constant">'</span><span class="Special">\\</span><span class="Constant">'</span>&nbsp;<span class="Statement">-&gt;</span>&nbsp;<span class="Statement">do</span>&nbsp;(a,b)&nbsp;<span class="Statement">&lt;-</span>&nbsp;pop2<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;push&nbsp;a<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;push&nbsp;b<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="Constant">':'</span>&nbsp;<span class="Statement">-&gt;</span>&nbsp;<span class="Statement">do</span>&nbsp;a&nbsp;<span class="Statement">&lt;-</span>&nbsp;pop<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; replicateM_&nbsp;<span class="Constant">2</span>&nbsp;(push&nbsp;a)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="Comment">-- program flow commands: --</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="Constant">' '</span>&nbsp;<span class="Statement">-&gt;</span>&nbsp;<span class="Identifier">return</span>&nbsp;()&nbsp;&nbsp;&nbsp;&nbsp;<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="Constant">'&gt;'</span>&nbsp;<span class="Statement">-&gt;</span>&nbsp;setDirection&nbsp;right<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="Constant">'&lt;'</span>&nbsp;<span class="Statement">-&gt;</span>&nbsp;setDirection&nbsp;left&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="Constant">'^'</span>&nbsp;<span class="Statement">-&gt;</span>&nbsp;setDirection&nbsp;up<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="Constant">'v'</span>&nbsp;<span class="Statement">-&gt;</span>&nbsp;setDirection&nbsp;down<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="Constant">'?'</span>&nbsp;<span class="Statement">-&gt;</span>&nbsp;getRandomDirection&nbsp;<span class="Statement">&gt;&gt;=</span>&nbsp;setDirection&nbsp;<br />
<br /></blockquote></p>
<p>And here is the program state that gets passed in the monad:</p>
<p><blockquote class="vimblock"><br />
<span class="Type">data</span>&nbsp;ProgramState&nbsp;<span class="Statement">=</span>&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;ES&nbsp;{&nbsp;<span class="Comment">-- state of the code and stack:</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; code&nbsp;<span class="Statement">::</span>&nbsp;Code,&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stack&nbsp;<span class="Statement">::</span>&nbsp;Stack,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="Comment">-- state of program flow:</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; position&nbsp;<span class="Statement">::</span>&nbsp;Position,&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; direction&nbsp;<span class="Statement">::</span>&nbsp;Direction,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; haltBit&nbsp;<span class="Statement">::</span>&nbsp;<span class="Type">Bool</span>,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="Comment">-- random generator:</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; randGen&nbsp;<span class="Statement">::</span>&nbsp;StdGen,&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="Comment">-- errors or other messages we collect:</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; messages&nbsp;<span class="Statement">::</span>&nbsp;[<span class="Type">String</span>],<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="Comment">-- should we announce messages and warnings?:</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; verbose&nbsp;<span class="Statement">::</span>&nbsp;<span class="Type">Bool</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<br />
<br /></blockquote></p>
<p>I only use the accessor functions (as opposed to pattern matching) to reach into the state in my code, so it was easy to add another state parameter if I wanted to extend the functionality somewhere.</p>
<p>Finally here is the evaluation loop that ties the core of the interpreter together:</p>
<p><blockquote class="vimblock"><br />
evalLoop&nbsp;<span class="Statement">::</span>&nbsp;REPL&nbsp;()<br />
evalLoop&nbsp;<span class="Statement">=</span>&nbsp;<span class="Statement">do</span><br />
&nbsp;&nbsp;&nbsp;&nbsp; <span class="Comment">-- extract the command at our position &amp; execute it:</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;getCmd&nbsp;<span class="Statement">&gt;&gt;=</span>&nbsp;execute<br />
&nbsp;&nbsp;&nbsp;&nbsp; <span class="Comment">-- print messages in the queue (unless quiet), and clear it:</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;printMessages<br />
&nbsp;&nbsp;&nbsp;&nbsp; <span class="Comment">-- halt if @ command issued, else move and recurse:</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;halting&nbsp;<span class="Statement">&lt;-</span>&nbsp;gets&nbsp;haltBit<br />
&nbsp;&nbsp;&nbsp;&nbsp;unless&nbsp;halting&nbsp;(move&nbsp;<span class="Statement">&gt;&gt;</span>&nbsp;evalLoop)&nbsp;<br />
<br /></blockquote></p>
<p>I found the most time-consuming parts of this project were in the design decisions and behaviour tweaks related to holes in the &#8217;93 spec, and tracking down a few silly bugs that came from wrong assumptions about the spec. </p>
<p>Vital in debugging were the <a href="http://users.tkk.fi/~mniemenm/befunge/ccbi.html">CCBI interpreter</a> as well as the <a href="http://mearie.org/projects/pyfunge/">pyfunge interpreter.</a> It&#8217;s difficult to debug an interpreter when you don&#8217;t whether the befunge code you are testing on is buggy or whether your interpreter is!</p>
<p>One last detail that I am proud of is the test suite that I have integrated into my darcs repo. Darcs automatically runs the befunge-93 portion of the amazing <a href="http://iki.fi/matti.niemenmaa/befunge/mycology.html">mycology test suite</a> by Matti Niemenmaa on each commit, as well as two smaller <a href="http://www.phlamethrower.co.uk/befunge/#files">test befunge programs</a> from phlamethrower, testing that the interpreter hasn&#8217;t changed behavior.</p>
<h2>Play With It</h2>
<p>You can check out the full source for the program <a href="http://coder.bsimmons.name/code/Befunge/Befunge.hs">here</a>, or get my whole darcs repository with:</p>
<blockquote><p>
<code>$ darcs get --tag=v0.2 http://coder.bsimmons.name/code/Befunge</code>
</p></blockquote>
<p>And here are a few nice programs to try out, written by folks cleverer than I, and stolen from vsync&#8217;s funge stuff <a href="http://quadium.net/funge/downloads/bef93src/">here</a>.</p>
<p><em>aturley.bf</em> by Andrew Turley:</p>
<blockquote><pre>
>84*>:#v_55+"ude.ub@yelruta">:#,_@>188*+>\02p\12p\:22p#v_$    55+,1-         v
    ^  0 v +1\                   _^#-+*<>22g02g*"_@"*-!1- #v_v>
       >:>::3g: ,\188                  ^^               -1\g21\g22
<p3 \"_":<
________________________________@_________________________________^  p3\"@":<
</pre>
</p3></pre>
</blockquote>
<p><em>life.bf</em> by Dmitry M Litvinov:</p>
<blockquote><pre>
v>>31g> ::51gg:2v++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
9p BXY|-+<v3 *89<%+ *                                                      *   +
21 >98 *7^>+\-0|< + *                                                     *    +
*5 ^:+ 1pg15\,:< + *                                                     ***  +
10^  <>$25*,51g1v+                                                            +
-^ p< | -*46p15:+<+                                                            +
> 31^> 151p>92*4v+                                                            +
 ^_ ".",   ^ vp1< +                                                            +
>v >41p      >0 v+                                                            +
:5! vg-1g15-1g14< +                                                            +
+1-+>+41g1-51gg+v+                                                            +
1p-1vg+1g15-1g14< +                                                            +
g61g>+41g51g1-g+v+                                                            +
14*1v4+g+1g15g14< +                           * *                              +
5>^4>1g1+51g1-g+v+                           * *                              +
^ _^v4+gg15+1g14< +                           ***                              +
>v! >1g1+51g1+g+v+                                                            +
g8-v14/*25-*4*88< +                                                            +
19+>g51gg" "- v  +                                                            +
4*5  v< v-2:_3v+                                                            +
 >^   |!-3_$  v< -+                                                            +
^    < <      <|<+                                                         ***+
>g51gp ^ >51gp^>v+                                                            +
^14"+"< ^g14"!"<++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
</pre>
<p></v3></pre>
</blockquote>
<p><em>prime.bf</em> by Kalyna Zazelenchuk:</p>
<blockquote><pre>
222p35*89+*11p>133p                   >33g1+33p   22g33g- v>22g33g%#v_v
 o                                                        >|
  2                             v,,,,, ,,,,,.g22"is prime."< 1                            >    v^                             < ^_@#-g11g22p22+1g22,*25<,,,,,,,,,,,,,.g22"is not prime."<
</pre>
</pre>
</blockquote>
<p>There is still a lot that could be done, but I think I&#8217;m done with it for now. Thanks for looking!</p>
]]></content:encoded>
			<wfw:commentRss>http://coder.bsimmons.name/blog/2010/01/a-befunge-93-interpreter/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk
Database Caching 6/15 queries in 0.079 seconds using disk

Served from: coder.bsimmons.name @ 2012-02-05 11:48:17 -->
