<?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>Code Commit &#187; Cat</title>
	<atom:link href="http://www.codecommit.com/blog/category/cat/feed" rel="self" type="application/rss+xml" />
	<link>http://www.codecommit.com/blog</link>
	<description>(permanently in beta)</description>
	<lastBuildDate>Mon, 07 Jun 2010 07:00:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<atom:link rel='hub' href='http://www.codecommit.com/blog/?pushpress=hub'/>
		<item>
		<title>The Joy of Concatenative Languages Part 3: Kindly Types</title>
		<link>http://www.codecommit.com/blog/cat/the-joy-of-concatenative-languages-part-3</link>
		<comments>http://www.codecommit.com/blog/cat/the-joy-of-concatenative-languages-part-3#comments</comments>
		<pubDate>Mon, 22 Dec 2008 09:00:00 +0000</pubDate>
		<dc:creator>Daniel Spiewak</dc:creator>
				<category><![CDATA[Cat]]></category>

		<guid isPermaLink="false">http://www.codecommit.com/blog/cat/the-joy-of-concatenative-languages-part-3</guid>
		<description><![CDATA[In parts one and two of this series, we dipped our toes into the fascinating world that is stack-based languages.&#160; By this point, you should be fairly familiar with how to construct simple algorithms using Cat (the language we have been working with) as well as the core terminology of the paradigm.&#160; In fact, with [...]]]></description>
			<content:encoded><![CDATA[<p>In parts <a href="http://www.codecommit.com/blog/cat/the-joy-of-concatenative-languages-part-1">one</a> and <a href="http://www.codecommit.com/blog/cat/the-joy-of-concatenative-languages-part-2">two</a> of this series, we dipped our toes into the fascinating world that is stack-based languages.&nbsp; By this point, you should be fairly familiar with how to construct simple algorithms using Cat (the language we have been working with) as well as the core terminology of the paradigm.&nbsp; In fact, with just the information given so far, you could probably go on to be productive with a real-world concatenative language like Factor.&nbsp; However, the interest does not just stop there&#8230;</p>
<p>One of the interesting challenges in programming language design is the construction of a type system.&nbsp; So as to clear up any possible misconception <em>before</em> it arises, this is how Pierce defines such a thing:</p>
<blockquote><p>A type system is a tractable syntactic method for proving the absence of certain program behaviors by classifying phrases according to the kinds of values they compute.</p></blockquote>
<p>For Java, which has a comparatively weak type system, this usually means preventing you from accidentally using a <code>String</code> as if it were an <code>int</code>.&nbsp; In other words, Java&#8217;s type system generally proves the absence of things like <code>NoSuchMethodError</code> and similar.&nbsp; C#, which has a slightly more-powerful type system, can also prove the absence of most <code>NullPointerException</code>(s) when code is written in a correct and idiomatic fashion.&nbsp; Scala goes even further with pattern matching&#8230;need I go on?&nbsp; The point is that type systems do different things in different languages, so the definition needs to be flexible enough to reflect that.</p>
<p>In this article, we&#8217;re going to look at how we can define a type system for a functional (meaning that we have quotations) concatenative language.&nbsp; In <a href="http://www.codecommit.com/blog/cat/the-joy-of-concatenative-languages-part-1#comment-4376">a comment on the first part of this series</a>, it was suggested that the task of typing stack-based languages is a fairly trivial one.&nbsp; This is true, but only to a certain point.&nbsp; As we will see, there are dragons lurking in the conceptual shadows, waiting for us to disturb their sleep.</p>
<h3>Simple Expressions</h3>
<p>Let&#8217;s start out with typing something simple.&nbsp; Consider the following program:</p>

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

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

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="cat"><span style="color: #cb0710;">1</span> <span style="color: #cb0710;">2</span> <span style="color: #857d1f;">+</span></pre></td></tr></table></div>

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

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="cat"><span style="color: #140dcc;">define</span> addSome <span class="br0">&#123;</span>
  <span style="color: #cb0710;">1</span> <span style="color: #cb0710;">2</span> <span style="color: #857d1f;">+</span>
<span class="br0">&#125;</span>
&nbsp;
addSome</pre></td></tr></table></div>

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

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="cat"><span style="color: #140dcc;">define</span> addTwice <span class="br0">&#123;</span>
  <span style="color: #857d1f;">+</span> <span style="color: #857d1f;">+</span>
<span class="br0">&#125;</span></pre></td></tr></table></div>

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

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">def</span> <span style="color: #2e7c0f;">addTwice</span><span class="br0">&#40;</span>a: <span style="color: #800080;">Int</span>, b: <span style="color: #800080;">Int</span>, c: <span style="color: #800080;">Int</span><span class="br0">&#41;</span> = a + b + c</pre></td></tr></table></div>

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

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="cat"><span style="color: #cb0710;">42</span> <span style="color: #800080;">pop</span></pre></td></tr></table></div>

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

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="cat"><span style="color: #cb0710;">&quot;fourty-two&quot;</span> <span style="color: #800080;">pop</span></pre></td></tr></table></div>

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

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="cat"><span style="color: #140dcc;">define</span> trouble <span class="br0">&#123;</span>
  <span style="color: #800080;">apply</span>
<span class="br0">&#125;</span></pre></td></tr></table></div>

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

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="cat"><span style="color: #140dcc;">define</span> <span style="color: #857d1f;">y</span> <span class="br0">&#123;</span>
  <span class="br0">&#91;</span><span style="color: #800080;">dup</span> <span style="color: #800080;">papply</span><span class="br0">&#93;</span> <span style="color: #800080;">swap</span> <span style="color: #800080;">compose</span> <span style="color: #800080;">dup</span> <span style="color: #800080;">apply</span>
<span class="br0">&#125;</span></pre></td></tr></table></div>

<p>This has the following type:</p>
<pre>y : ('A ('A ('A -> 'B) -> 'B) -> 'B)</pre>
<p>As you may have guessed by the name, this is the <a href="http://en.wikipedia.org/wiki/Y-combinator">Y-combinator</a><sup><a href="#prt3-1">1</a></sup>, one of the most well-known mechanisms for producing recursion in a nameless system.&nbsp; Note that this definition looks a little different from the pure-untyped lambda calculus (call-by-name semantics):</p>
<div align="center">λf . (λx . f (x x)) (λx . f (x x))</div>
<p>What I&#8217;m trying to point out here is the fact that Cat is able to leverage its type system to assign a type to the Y-combinator.&nbsp; This is something which is literally impossible in System F, a typed form of lambda-calculus.&nbsp; In fact, the only way to type-check this function in a lambda-calculus-derivative system would be to add recursive types.&nbsp; Cat is able to get by with a very much non-recursive type definition, something which I find fascinating in the extreme.</p>
<p><strong>Update:</strong> The above paragraph is somewhat misleading.&nbsp; It turns out that Cat actually <i>does</i> use a recursive type under the surface to derive the non-recursive type for <code>y</code>.&nbsp; Specifically:</p>
<pre>dup papply : ('A ('B ('B self -> 'C) -> 'C) -> 'A ('B -> 'C))</pre>
<p>On a further theoretical note, the device in Cat&#8217;s type system which allows this power is in fact the stack type variable (e.g. <code>'A</code>).&nbsp; These stack types are conceptually quite similar to the type parameters we used in typing <code>pop</code> (e.g. <code>'a</code>), but still in a very separate domain.&nbsp; In fact, stack types have a different <em>kind</em> than regular types.&nbsp; This is not to say that Cat employs higher-kinds such as Scala&#8217;s (e.g. <code>* => *</code>), but it does have two very different type kinds: stacks and values.</p>
<p>And yet, it is not kinds in and of themselves which allows for the typing of the Y-combinator.&nbsp; F<sub>ω</sub> is essentially System F with higher-kinds, and yet it is still incapable of handling this tiny little expression.&nbsp; Most interesting indeed&#8230;</p>
<h3>Conclusion</h3>
<p>As you can see, type systems and concatenative languages do fit together nicely, but it takes a lot more effort than one would initially expect.&nbsp; While typing simple expressions is easy enough, the waters are muddied as soon as higher-order functions and even deferred stack access enters the mix.&nbsp; This is an extremely fertile area for research, where a lot of interesting ideas are being developed.&nbsp; For example, <a href="http://lambda-the-ultimate.org/node/3050#comment-44535">John Nowak&#8217;s 5th</a> attempts to apply a type system to the stack-based paradigm, but in a very different way than Cat.</p>
<p>I hope you enjoyed this mini-series of articles on concatenative languages.&nbsp; While they are a bit of a backwater in the programming language menagerie, I think that studying them can be a very instructive experience.&nbsp; Furthermore, there remain some problems that are very nicely expressed in languages like Cat while being extremely unwieldy in more conventional languages like Scala.&nbsp; Despite the obscurity of concatenative languages, it never hurts to have an extra language on hand, ready for those times when it really is the best tool for the job.</p>
<p><sup><a id="prt3-1">1</a></sup> Technically, this is a little different from the Y-combinator used in conventional lambda-calculus (it executes the quotation rather than returning a fixed-point).&nbsp; However, conceptually it is the same idea.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codecommit.com/blog/cat/the-joy-of-concatenative-languages-part-3/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>The Joy of Concatenative Languages Part 2: Innately Functional</title>
		<link>http://www.codecommit.com/blog/cat/the-joy-of-concatenative-languages-part-2</link>
		<comments>http://www.codecommit.com/blog/cat/the-joy-of-concatenative-languages-part-2#comments</comments>
		<pubDate>Mon, 15 Dec 2008 09:00:53 +0000</pubDate>
		<dc:creator>Daniel Spiewak</dc:creator>
				<category><![CDATA[Cat]]></category>

		<guid isPermaLink="false">http://www.codecommit.com/blog/cat/the-joy-of-concatenative-languages-part-2</guid>
		<description><![CDATA[In part one of this series, I introduced the concept of a stack-based language and in particular the syntax and rough ideas behind Cat.&#160; However, to anyone coming into concatenative land for the first time, my examples likely seemed both odd and unconvincing.&#160; After all, why would you ever use point-free programming when everyone else [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://www.codecommit.com/blog/cat/the-joy-of-concatenative-languages-part-1">part one of this series</a>, I introduced the concept of a stack-based language and in particular the syntax and rough ideas behind <a href="http://www.cat-language.com">Cat</a>.&nbsp; However, to anyone coming into concatenative land for the first time, my examples likely seemed both odd and unconvincing.&nbsp; After all, why would you ever use point-free programming when everyone else seems to be sold on the idea of name binding?&nbsp; More importantly, where do these languages fit in with our established menagerie of language paradigms?</p>
<p>The answer to the first question really depends on the situation.&nbsp; I personally think that the best motivation for concatenative languages is their syntax.&nbsp; If you want to create an internal DSL, there will be no language better suited to it than one which is concatenative, Cat, Factor or otherwise.&nbsp; This is because stack-oriented languages can get away with almost no syntax whatsoever.&nbsp; They say that Lisp is a syntax-free language, but this holds even more strongly for languages like Cat.&nbsp; Well, that and you don&#8217;t have to deal with all the parentheses&#8230;</p>
<p>The second question is (I think) the more interesting one: how do we classify these languages and what sort of methodologies should we apply?&nbsp; At first glance, Cat (and other languages like it) seem to be quite imperative in nature.&nbsp; After all, you have a single mutable stack that any function can modify.&nbsp; However, if you turn your head sideways and blink twice, you begin to realize that concatenative languages are really much closer to the functional side of the oyster.</p>
<p>Consider the following Cat program:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="cat"><span style="color: #140dcc;">define</span> plus <span class="br0">&#123;</span> <span style="color: #857d1f;">+</span> <span class="br0">&#125;</span>
<span style="color: #140dcc;">define</span> minus <span class="br0">&#123;</span> <span style="color: #857d1f;">-</span> <span class="br0">&#125;</span>
&nbsp;
<span style="color: #cb0710;">7</span> <span style="color: #cb0710;">2</span> <span style="color: #cb0710;">3</span>
plus minus</pre></td></tr></table></div>

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

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">def</span> <span style="color: #2e7c0f;">plus</span><span class="br0">&#40;</span>a: <span style="color: #800080;">Int</span>, b: <span style="color: #800080;">Int</span><span class="br0">&#41;</span> = a + b
<span style="color: #140dcc;">def</span> <span style="color: #2e7c0f;">minus</span><span class="br0">&#40;</span>a: <span style="color: #800080;">Int</span>, b: <span style="color: #800080;">Int</span><span class="br0">&#41;</span> = a - b
&nbsp;
<span style="color: #2e7c0f;">minus</span><span class="br0">&#40;</span><span style="color: #cb0710;">7</span>, <span style="color: #2e7c0f;">plus</span><span class="br0">&#40;</span><span style="color: #cb0710;">2</span>, <span style="color: #cb0710;">3</span><span class="br0">&#41;</span><span class="br0">&#41;</span></pre></td></tr></table></div>

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

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="cat"><span style="color: #cb0710;">2</span> <span style="color: #cb0710;">3</span>
<span class="br0">&#91;</span> <span style="color: #857d1f;">+</span> <span class="br0">&#93;</span>
<span style="color: #800080;">apply</span></pre></td></tr></table></div>

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

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="cat"><span style="color: #cb0710;">5</span> <span style="color: #cb0710;">0</span> <span style="color: #857d1f;">&lt;</span>
<span class="br0">&#91;</span> <span style="color: #cb0710;">&quot;strange math&quot;</span> <span class="br0">&#93;</span>
<span class="br0">&#91;</span> <span style="color: #cb0710;">&quot;all is well&quot;</span> <span class="br0">&#93;</span>
<span style="color: #800080;">if</span></pre></td></tr></table></div>

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

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="cat"><span style="color: #140dcc;">define</span> append <span class="br0">&#123;</span>
  <span style="color: #800080;">empty</span>
  <span class="br0">&#91;</span> <span style="color: #800080;">pop</span> <span class="br0">&#93;</span>
  <span class="br0">&#91;</span>
    <span style="color: #800080;">uncons</span>
    <span class="br0">&#91;</span>append<span class="br0">&#93;</span> <span style="color: #800080;">dip</span>
    <span style="color: #800080;">cons</span>
  <span class="br0">&#93;</span>
  <span style="color: #800080;">if</span>
<span class="br0">&#125;</span></pre></td></tr></table></div>

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

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ml"><span style="color: #140dcc;">fun</span> append ls <span style="color: #a40d14;">nil</span> = ls
  | append ls <span class="br0">&#40;</span>hd :: tail<span class="br0">&#41;</span> = hd :: <span class="br0">&#40;</span>append ls tail<span class="br0">&#41;</span></pre></td></tr></table></div>

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

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="cat"><span style="color: #140dcc;">define</span> <span style="color: #857d1f;">cat</span> <span class="br0">&#123;</span>
  <span style="color: #800080;">swap</span> <span class="br0">&#91;</span><span style="color: #800080;">cons</span><span class="br0">&#93;</span> <span style="color: #857d1f;">rfold</span>
<span class="br0">&#125;</span></pre></td></tr></table></div>

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

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ml"><span style="color: #140dcc;">val</span> append = foldr <span class="br0">&#40;</span><span style="color: #140dcc;">op</span>::<span class="br0">&#41;</span></pre></td></tr></table></div>

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

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="cat"><span style="color: #140dcc;">define</span> length <span class="br0">&#123;</span>
  <span style="color: #cb0710;">0</span> <span class="br0">&#91;</span> <span style="color: #800080;">pop</span> <span style="color: #cb0710;">1</span> <span style="color: #857d1f;">+</span> <span class="br0">&#93;</span> <span style="color: #857d1f;">fold</span>
<span class="br0">&#125;</span></pre></td></tr></table></div>

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

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ml"><span style="color: #140dcc;">val</span> length = foldl <span class="br0">&#40;</span><span style="color: #140dcc;">fn</span> <span class="br0">&#40;</span>n, _<span class="br0">&#41;</span> =&gt; n + <span style="color: #cb0710;">1</span><span class="br0">&#41;</span> <span style="color: #cb0710;">0</span></pre></td></tr></table></div>

<h3>Conclusion</h3>
<p>The important take-away from this tangled morass of an article is the fact that Cat is a highly functional language, capable of easily keeping up with some of the stalwart champions of the paradigm.&nbsp; More significantly, this is a trait which is shared by <em>all</em> concatenative languages.&nbsp; Rather than throwing away all of the old wisdom learned in language design, stack-based languages build on it by providing an alternative view into the world of functions.</p>
<p>In the <a href="http://www.codecommit.com/blog/cat/the-joy-of-concatenative-languages-part-3">next (and final) article of the series</a>, we will take a brief look at the challenges of applying a type system to a concatenative language and the fascinating techniques used by Cat to achieve just that.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codecommit.com/blog/cat/the-joy-of-concatenative-languages-part-2/feed</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>The Joy of Concatenative Languages Part 1</title>
		<link>http://www.codecommit.com/blog/cat/the-joy-of-concatenative-languages-part-1</link>
		<comments>http://www.codecommit.com/blog/cat/the-joy-of-concatenative-languages-part-1#comments</comments>
		<pubDate>Mon, 08 Dec 2008 08:00:00 +0000</pubDate>
		<dc:creator>Daniel Spiewak</dc:creator>
				<category><![CDATA[Cat]]></category>

		<guid isPermaLink="false">http://www.codecommit.com/blog/cat/the-joy-of-concatenative-languages-part-1</guid>
		<description><![CDATA[Concatenative languages like Forth have been around for a long time.&#160; Hewlett-Packard famously employed a stack-based language called &#8220;RPL&#8221; on their HP-28 and HP-48 calculators, bringing the concept of Reverse Polish Notation to the mainstream&#8230;or as close to the mainstream as a really geeky toy can get.&#160; Surprisingly though, these languages have not seen serious [...]]]></description>
			<content:encoded><![CDATA[<p>Concatenative languages like Forth have been around for a long time.&nbsp; Hewlett-Packard famously employed a stack-based language called &#8220;RPL&#8221; on their HP-28 and HP-48 calculators, bringing the concept of Reverse Polish Notation to the mainstream&#8230;or as close to the mainstream as a really geeky toy can get.&nbsp; Surprisingly though, these languages have not seen serious adoption beyond the experimental and embedded device realms.&nbsp; And by &#8220;adoption&#8221;, I mean real programmers writing real code, not this whole <a href="http://en.wikipedia.org/wiki/JVM">interpreted bytecode nonsense</a>.</p>
<p>This is a shame, because stack-based languages have a remarkable number of things to teach us.&nbsp; Their superficial distinction from conventional programming languages very quickly gives way to a deep connection, particularly with functional languages.&nbsp; However, if we dig even deeper, we find that this similarity has its limits.&nbsp; There are some truly profound nuggets of truth waiting to be uncovered within these murky depths.&nbsp; Shall we?</p>
<p><em><strong>Trivial aside:</strong> I&#8217;m going to use the terms &#8220;concatenative&#8221; and &#8220;stack-based&#8221; interchangeably through the article.&nbsp; While these are most definitely related concepts, they are not exactly synonyms.&nbsp; Bear that in mind if you read anything more in-depth on the subject.</em></p>
<h3>The Basics</h3>
<p>Before we look at some of those &#8220;deeper truths of which I speak, it might be helpful to at least understand the fundamentals of stack-based programming.&nbsp; From Wikipedia:</p>
<blockquote><p>The concatenative or stack-based programming languages are ones in which the concatenation of two pieces of code expresses the composition of the functions they express. These languages use a stack to store the arguments and return values of operations.</p></blockquote>
<p>Er, right.&nbsp; I didn&#8217;t find that very helpful either.&nbsp; Let&#8217;s try again&#8230;</p>
<p>Stack-based programming languages all share a common element: an operand stack.&nbsp; Consider the following program:</p>

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

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

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="cat"><span style="color: #cb0710;">2</span> <span style="color: #cb0710;">3</span> <span style="color: #857d1f;">+</span></pre></td></tr></table></div>

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

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">val</span> plus = <span class="br0">&#123;</span> <span class="br0">&#40;</span>x: <span style="color: #800080;">Int</span>, y: <span style="color: #800080;">Int</span><span class="br0">&#41;</span> =&gt; x + y <span class="br0">&#125;</span>
<span style="color: #2e7c0f;">plus</span><span class="br0">&#40;</span><span style="color: #cb0710;">2</span>, <span style="color: #cb0710;">3</span><span class="br0">&#41;</span></pre></td></tr></table></div>

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

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="cat"><span style="color: #cb0710;">2</span> <span style="color: #cb0710;">3</span>
<span class="br0">&#91;</span> <span style="color: #857d1f;">+</span> <span class="br0">&#93;</span>
<span style="color: #800080;">apply</span></pre></td></tr></table></div>

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

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="cat"><span style="color: #140dcc;">define</span> plus <span class="br0">&#123;</span> <span style="color: #857d1f;">+</span> <span class="br0">&#125;</span></pre></td></tr></table></div>

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

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="cat"><span style="color: #cb0710;">2</span> <span style="color: #cb0710;">3</span> plus    <span style="color: #999999;">// invoke the `plus` function</span>
<span style="color: #cb0710;">10</span> <span style="color: #857d1f;">&lt;</span>
<span class="br0">&#91;</span> <span style="color: #cb0710;">0</span> <span class="br0">&#93;</span>
<span class="br0">&#91;</span> <span style="color: #cb0710;">42</span> <span class="br0">&#93;</span>
<span style="color: #800080;">if</span></pre></td></tr></table></div>

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

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="cat"><span style="color: #140dcc;">define</span> fac <span class="br0">&#123;</span>
  <span style="color: #800080;">dup</span> <span style="color: #cb0710;">0</span> <span style="color: #800080;">eq</span>
  <span class="br0">&#91;</span> <span style="color: #800080;">pop</span> <span style="color: #cb0710;">1</span> <span class="br0">&#93;</span>
  <span class="br0">&#91;</span> <span style="color: #800080;">dup</span> <span style="color: #cb0710;">1</span> <span style="color: #857d1f;">-</span> fac <span style="color: #857d1f;">*</span> <span class="br0">&#93;</span>
  <span style="color: #800080;">if</span>
<span class="br0">&#125;</span></pre></td></tr></table></div>

<p>Note that this isn&#8217;t even the most concise way of writing this, but it does the job.&nbsp; To see how, let&#8217;s look at how this will execute word-by-word (assuming an input of <code>4</code>):</p>
<table border="1">
<tr>
<td align="center"><b>Stack</b></td>
<td><b>Word</b></td>
</tr>
<tr>
<td><code>4</code></p>
<td><code>dup</code></td>
</tr>
<tr>
<td><code>4 4</code></p>
<td><code>0</code></td>
</tr>
<tr>
<td><code>4 4 0</code></p>
<td><code>eq</code></td>
</tr>
<tr>
<td><code>4 False</code></p>
<td><code>[ pop 1 ]</code></td>
</tr>
<tr>
<td><code>4 False [pop 1]</code></p>
<td><code>[ dup 1 - fac * ]</code></td>
</tr>
<tr>
<td><code>4 False [pop 1] [dup 1 - fac *]</code></p>
<td><code>if</code></td>
</tr>
<tr>
<td><code>4</code></p>
<td><code>dup</code></td>
</tr>
<tr>
<td><code>4 4</code></p>
<td><code>1</code></td>
</tr>
<tr>
<td><code>4 4 1</code></p>
<td><code>-</code></td>
</tr>
<tr>
<td><code>4 3</code></p>
<td><code>fac</code> <em>(assume magic recursion)</em></td>
</tr>
<tr>
<td><code>4 6</code></p>
<td><code>*</code></td>
</tr>
</table>
<p>The final result is <code>24</code>, a value which is left on the stack.&nbsp; Pretty nifty, eh?</p>
<h3>Conclusion</h3>
<p>You&#8217;ll notice this is a shorter post than I usually spew forth (no pun intended&#8230;this time).&nbsp; The reason being that I want this to be fairly easy to digest.&nbsp; Concatenative languages (and Cat in particular) are not all that difficult to digest.&nbsp; They are a slightly different way of thinking about programming, but as <a href="http://www.codecommit.com/blog/cat/the-joy-of-concatenative-languages-part-2">we will see in the next part</a>, not so different as it would seem.</p>
<p><strong>Note:</strong> Cat is written in C# and is available under the MIT License.&nbsp; Don&#8217;t fear the CLR though, Cat runs just fine under Mono.&nbsp; If you really want to experiment with no risk to yourself, a Javascript interpreter is available.</p>
<ul>
<li><a href="http://www.cat-language.com">Cat Language Website</a></li>
<li><a href="http://www.cat-language.com/interpreter.html">Interactive Javascript Interpreter for Cat</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.codecommit.com/blog/cat/the-joy-of-concatenative-languages-part-1/feed</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>
