<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.1" -->
<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/"
	>

<channel>
	<title>cdiggins.com</title>
	<link>http://cdiggins.com</link>
	<description>Christopher Diggins</description>
	<pubDate>Fri, 25 Apr 2008 15:02:43 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.1</generator>
	<language>en</language>
			<item>
		<title>Some things I am doing</title>
		<link>http://cdiggins.com/2008/04/25/some-things-i-am-doing/</link>
		<comments>http://cdiggins.com/2008/04/25/some-things-i-am-doing/#comments</comments>
		<pubDate>Fri, 25 Apr 2008 15:02:43 +0000</pubDate>
		<dc:creator>cdiggins</dc:creator>
		
		<category><![CDATA[Everything]]></category>

		<guid isPermaLink="false">http://cdiggins.com/2008/04/25/some-things-i-am-doing/</guid>
		<description><![CDATA[Here are some of the things I have been up to:

Looking at using Cat as a replacement for the SECD machine to express operational semantics of Heron (see http://lambda-the-ultimate.org/node/2787)
Developing the Heron to Cat interpreter (sorry no demos yet). This requires the introduction of local definitions in Cat to emulate labels and gotos.
Figuring out how a [...]]]></description>
			<content:encoded><![CDATA[<p>Here are some of the things I have been up to:</p>
<ul>
<li>Looking at using Cat as a replacement for the SECD machine to express operational semantics of Heron (see <a href="http://lambda-the-ultimate.org/node/2787">http://lambda-the-ultimate.org/node/2787</a>)</li>
<li>Developing the Heron to Cat interpreter (sorry no demos yet). This requires the introduction of local definitions in Cat to emulate labels and gotos.</li>
<li>Figuring out how a simple transform for Cat code so that a value can be passed from function to function on the top of the stack.</li>
<li>Trying to figure out how to fix the type system to allow proper handling of nested forall polymorphism without annotations. <a href="http://www.church-project.org/reports/Kfo+Wel:POPL-1999.html">System-I looks promising for this task</a>,  but it is very complex. There has got to be a better system. I am seriously considering embedding the full Lambda Calculus, or possibly even Cat itself, directly in the type-system and saying sayanora to decidability.</li>
<li>Wondering why my recent blog post on <a href="http://dobbscodetalk.com/index.php?option=com_myblog&amp;show=Composition-vs-Inheritance.html&amp;Itemid=29">Composition vs Inheritance</a> received such a lukewarm reception on Reddit. It seems to me that interface forwarding (or delegation as I called it) is an incredibly important feature, but no one cares.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://cdiggins.com/2008/04/25/some-things-i-am-doing/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Objects in Cat</title>
		<link>http://cdiggins.com/2008/04/18/objects-in-cat/</link>
		<comments>http://cdiggins.com/2008/04/18/objects-in-cat/#comments</comments>
		<pubDate>Fri, 18 Apr 2008 14:26:35 +0000</pubDate>
		<dc:creator>cdiggins</dc:creator>
		
		<category><![CDATA[Everything]]></category>

		<guid isPermaLink="false">http://cdiggins.com/2008/04/18/objects-in-cat/</guid>
		<description><![CDATA[I have been thinking a lot about how to extend the Cat type system so that I can handle objects. The big challenge is to do this in a way that doesn&#8217;t add any significant complexity to the language. This is particularly hard to do for a language as small and tight as Cat.
Previously I [...]]]></description>
			<content:encoded><![CDATA[<p>I have been thinking a lot about how to extend the Cat type system so that I can handle objects. The big challenge is to do this in a way that doesn&#8217;t add any significant complexity to the language. This is particularly hard to do for a language as small and tight as Cat.</p>
<p>Previously I was thinking about starting with tuples. Not only are tuples nice on their own, but the type of an object is essentially a tuple. There is one simple way to add tuples, which is to represent them as dynamically constructed functions. E.g. ( -&gt; ( -&gt; int string)) which would be a tuple containing an int and a string. The challenge occurs when I try to get values in and out of that function? I want something like: &#8220;get0&#8243;, &#8220;get1&#8243;, etc. which gets the value from the appropriate slot. This is trivial to code, but the problem is that I can&#8217;t express the type of &#8220;getN&#8221; in a general enough manner. Consider</p>
<blockquote><p>get0 : (( -&gt; &#8216;A &#8216;b) -&gt; &#8216;b)<br />
get1 : (( -&gt; &#8216;A &#8216;b &#8216;c) -&gt; &#8216;b)<br />
get2 : (( -&gt; &#8216;A &#8216;b &#8216;c &#8216;d) -&gt; &#8216;b)<br />
&#8230;</p></blockquote>
<p>This works fine, but we can only write so many of these (perhaps a dozen or so) before they start to become unmanageable. The problem then becomes that we would have to limit the number of field accessors, and hence the number of fields to a relatively small and arbitrary number. That is extremely limited, and nearly useless in practice when we can easily imagine objects with hundreds of fields or more. It will also put a horrible strain on the type reconstruction and checking algorithms.</p>
<blockquote><p>Sidebar: I will probably limit the number of fields to 255 anyway for several reasons:</p>
<ul>
<li>there has to be some stated upper limit</li>
<li>too many fields represents a horrible flaw in the design of OO software</li>
<li>it is is trivial to extend the 255 limit using aggregation (putting objects in objects)</li>
<li>Cat is designed to run on small memory systems, so being able to represent the index of a field with a single byte is appealing.</li>
</ul>
</blockquote>
<p>There is another subtle problem (or benefit depending on your point of view) and that is that this approach automatically provides structural subtyping. In other words two types that are structured the same will always be treated as exactly the same type. While this is useful in some circumstances, it is not good to paint ourselves in to such a corner. It is very important for software modularity and correctness to be able to construct two different types with similar structure when needed. </p>
<p>The following is an entirely different approach that I am now seriously considering using. First I will approach the problem of objects and use those to build tuples.  </p>
<blockquote><p>object IntString { x : int; y : string; }<br />
6 &#8220;the answer is &#8221; IntString.new x.get 7 * x.set<br />
y.get write x.get writeln</p></blockquote>
<p>So what is the type of the constructors, setters, and getters? Well I propose that the types are:</p>
<blockquote><p>IntString.new : (int string -&gt; IntString)<br />
x.get : (&#8217;o -&gt; &#8216;o &#8216;o.x)<br />
x.set : (&#8217;o &#8216;o.x -&gt; &#8216;o)<br />
y.get : (&#8217;o -&gt; &#8216;o &#8216;o.y)<br />
y.set : (&#8217;o &#8216;o.y -&gt; &#8216;o)</p></blockquote>
<p>So what has happened is that the type of &#8216;o.x is treated as an unknown variable, that is expanded once &#8216;o is known. Internally the type reconstruction algorithm simply has to create an object to represent the IntString, and track it.</p>
<p>So far I am very pleased with this approach, because the overall changes to the type system are very minimal, and the everything looks like it will work fine.</p>
<p>The next step is to allow fields to be added incrementally. I am thinking that the syntax may be something like:</p>
<blockquote><p>30 &#8220;answer&#8221; IntString.new <br />
12 z.add<br />
x.get z.get +</p></blockquote>
<p>So the question is what the type will be off &#8220;z.add&#8221;? Perhaps something like:</p>
<blockquote><p>z.add : (&#8217;o &#8216;x -&gt; &#8216;o+z)</p></blockquote>
<p>This &#8216;o+z just indicates that the result is a new type with a field named &#8220;z&#8221;. The type checker will have to manage the type internally. So to make this a fully compatible duck-typing system, such as the kind found in prototype based object-oriented languages like JavaScript, the requirement is simply that any object can be used where any other object is used, and that if a field is requested whose type is not known at compile-time, we return a value of type &#8220;any&#8221;, with the special value &#8220;undefined&#8221; used to indicate failure to find the field. </p>
]]></content:encoded>
			<wfw:commentRss>http://cdiggins.com/2008/04/18/objects-in-cat/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Cat Programming Language version 1.0 beta 4</title>
		<link>http://cdiggins.com/2008/04/16/cat-programming-language-version-10-beta-4/</link>
		<comments>http://cdiggins.com/2008/04/16/cat-programming-language-version-10-beta-4/#comments</comments>
		<pubDate>Wed, 16 Apr 2008 12:51:11 +0000</pubDate>
		<dc:creator>cdiggins</dc:creator>
		
		<category><![CDATA[Everything]]></category>

		<guid isPermaLink="false">http://cdiggins.com/2008/04/16/cat-programming-language-version-10-beta-4/</guid>
		<description><![CDATA[Cat version 1.0 beta 4 was just released, download available here. New features inlude:

static type checking of all primitives
fixed type inference of &#8220;quote&#8221; function
reintroduced &#8220;self&#8221; types to indicate a function accepting itself as an argument (e.g. define m { dup apply }, m : (&#8217;A (&#8217;A self -&#62; &#8216;B) -&#62; &#8216;B))
added define symbol (/d:NOGRAPHICS) to remove [...]]]></description>
			<content:encoded><![CDATA[<p>Cat version 1.0 beta 4 was just released, <a href="http://code.google.com/p/cat-language/downloads/list">download available here</a>. New features inlude:</p>
<ul>
<li>static type checking of all primitives</li>
<li>fixed type inference of &#8220;quote&#8221; function</li>
<li>reintroduced &#8220;self&#8221; types to indicate a function accepting itself as an argument (e.g. define m { dup apply }, m : (&#8217;A (&#8217;A self -&gt; &#8216;B) -&gt; &#8216;B))</li>
<li>added define symbol (/d:NOGRAPHICS) to remove graphics code in specific builds. This should allow Cat to work properly using Mono on Mac, see &#8220;mono_cat_no_graphics.exe&#8221; and &#8220;cat_mono_no_graphics.bat&#8221; for build options</li>
</ul>
<p>Other recent news includes:</p>
<ul>
<li>graphics capabilities added to the <a href="http://www.cat-language.com/interpreter.html">online Cat interpreter</a></li>
<li>Cat article appears on <a href="http://www.ddj.com/architect/207200779">Doctor Dobbs Journal</a></li>
</ul>
<p><strong>About Cat</strong> </p>
<p><a href="http://www.cat-language.com/">Cat</a> is a stack-based programming language that is distinct in that it allows functions to be constructed and pushed on to the stack in a type-safe manner. Cat has potential applications in computer science education, and as an intermediate language for optimization, verificatino, program transformation, and automated translation.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdiggins.com/2008/04/16/cat-programming-language-version-10-beta-4/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Open-Source Cat Interpreter (v1.2) in Javascript Online</title>
		<link>http://cdiggins.com/2008/04/15/open-source-cat-interpreter-v12-in-javascript-online/</link>
		<comments>http://cdiggins.com/2008/04/15/open-source-cat-interpreter-v12-in-javascript-online/#comments</comments>
		<pubDate>Tue, 15 Apr 2008 16:48:40 +0000</pubDate>
		<dc:creator>cdiggins</dc:creator>
		
		<category><![CDATA[Everything]]></category>

		<guid isPermaLink="false">http://cdiggins.com/2008/04/15/open-source-cat-interpreter-v12-in-javascript-online/</guid>
		<description><![CDATA[This is to announce that I have released version 1.2 of the online Cat interpreter. There are some bug fixes (e.g. &#8220;dup&#8221; now properly copies lists) and I have introduced primitive graphics capabilities, along with a new tool-tip help text to the various predefined instructions. Have fun, and feel free to reuse the source code, it [...]]]></description>
			<content:encoded><![CDATA[<p>This is to announce that I have released version 1.2 of the <a href="http://www.cat-language.com/interpreter.html">online Cat interpreter</a>. There are some bug fixes (e.g. &#8220;dup&#8221; now properly copies lists) and I have introduced primitive graphics capabilities, along with a new tool-tip help text to the various predefined instructions. Have fun, and feel free to reuse the source code, it is public domain!</p>
]]></content:encoded>
			<wfw:commentRss>http://cdiggins.com/2008/04/15/open-source-cat-interpreter-v12-in-javascript-online/feed/</wfw:commentRss>
		</item>
		<item>
		<title>SVN Version of Cat does type checking!</title>
		<link>http://cdiggins.com/2008/04/14/svn-version-of-cat-does-type-checking/</link>
		<comments>http://cdiggins.com/2008/04/14/svn-version-of-cat-does-type-checking/#comments</comments>
		<pubDate>Mon, 14 Apr 2008 07:13:29 +0000</pubDate>
		<dc:creator>cdiggins</dc:creator>
		
		<category><![CDATA[Everything]]></category>

		<guid isPermaLink="false">http://cdiggins.com/2008/04/14/svn-version-of-cat-does-type-checking/</guid>
		<description><![CDATA[I finally just finished integrating type checking back into the Cat compiler. This is only available in the version of Cat most recently checked into the SVN repository so feel free to pull it out: http://code.google.com/p/cat-language/source/checkout.
So Cat supports &#8220;self&#8221; types again which are references to enclosing functions. They are relatively rare in functions, but one [...]]]></description>
			<content:encoded><![CDATA[<p>I finally just finished integrating type checking back into the Cat compiler. This is only available in the version of Cat most recently checked into the SVN repository so feel free to pull it out: <a href="http://code.google.com/p/cat-language/source/checkout">http://code.google.com/p/cat-language/source/checkout</a>.</p>
<p>So Cat supports &#8220;self&#8221; types again which are references to enclosing functions. They are relatively rare in functions, but one example is the m combinator, defined as &#8220;dup apply&#8221; which has the type:</p>
<blockquote><p>m : (&#8217;A (&#8217;A self -&gt; &#8216;B) -&gt; &#8216;B).</p></blockquote>
<p>Anyway, I can&#8217;t sum up how happy I am to have this working with a minimum amount of pain.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdiggins.com/2008/04/14/svn-version-of-cat-does-type-checking/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Cat Programming Language Release v1.0 beta 3</title>
		<link>http://cdiggins.com/2008/04/13/cat-programming-language-release-v10-beta-3/</link>
		<comments>http://cdiggins.com/2008/04/13/cat-programming-language-release-v10-beta-3/#comments</comments>
		<pubDate>Sun, 13 Apr 2008 05:45:34 +0000</pubDate>
		<dc:creator>cdiggins</dc:creator>
		
		<category><![CDATA[Everything]]></category>

		<guid isPermaLink="false">http://cdiggins.com/2008/04/13/cat-programming-language-release-v10-beta-3/</guid>
		<description><![CDATA[This is an announcement of a new release of the Cat programming language. Recent changes include:

automatic loading of the standard library
481 unit-tests (#test_all)
documentation of over 400 primitive and standard-library instructions
various minor bug fixes to the standard library
searching for definitions matching a particular prefix, using &#8220;prefix&#8221; #defmatch

Cat is a functional stack-based language based on Joy. Cat [...]]]></description>
			<content:encoded><![CDATA[<p>This is an announcement of a <a href="http://code.google.com/p/cat-language/downloads/list">new release</a> of the Cat programming language. Recent changes include:</p>
<ul>
<li>automatic loading of the standard library</li>
<li>481 unit-tests (#test_all)</li>
<li>documentation of over <a href="http://www.cat-language.com/primitives.html">400 primitive and standard-library instructions</a></li>
<li>various minor bug fixes to the standard library</li>
<li>searching for definitions matching a particular prefix, using &#8220;prefix&#8221; #defmatch</li>
</ul>
<p>Cat is a functional stack-based language based on Joy. Cat is designed for several different purposes, but one of my key intentions was for education. Stack based languages can be very interesting for teaching computer science concepts because a student can see each step of a computation, and better understand how a computer operates.</p>
<p>In an effort to make Cat a suitable language for teaching programming I wanted to recapture the spirit of the <a href="http://en.wikipedia.org/wiki/Logo_%28programming_language%29">Logo language</a> in Cat. I found that when learning to program it was not only fun to write drawing programs, but that seeing the results of any mistakes I made helped me to understand what went wrong, and learn faster.</p>
<p>To do this I have included a turtle graphics library. The following screenshot is a demonstration of Cat in action:</p>
<p><a href="http://cdiggins.com/wp-content/uploads/2008/04/triangles-in-a-circle.PNG" title="Triangles in a Circle"><img src="http://cdiggins.com/wp-content/uploads/2008/04/triangles-in-a-circle.PNG" alt="Triangles in a Circle" /></a></p>
<p>The program that generated this is:</p>
<p>&gt;&gt; ow [20 tr pu 40 fd pd 40 draw_triangle] 20 repeat</p>
<p>These instructions are:</p>
<ul>
<li>ow - open a window</li>
<li>tr - turn right</li>
<li>pu - pen up</li>
<li>fd - forward</li>
<li>pd - pen down</li>
<li>repeat - repeats a function a number of times</li>
<li>[&#8230;] - pushes a function onto the stack</li>
</ul>
<p>If you are interested in using Cat to teach programming, please <a href="mailto:cdiggins@gmail.com">let me know</a> if I can help in any way. I&#8217;m very interested in trying to make it easier to teach programming to people.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdiggins.com/2008/04/13/cat-programming-language-release-v10-beta-3/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Expressing Local Assignment without Side-Effects using Fold (can you do this in Haskell?)</title>
		<link>http://cdiggins.com/2008/04/11/expressing-local-assignment-without-side-effects-using-fold-can-you-do-this-in-haskell/</link>
		<comments>http://cdiggins.com/2008/04/11/expressing-local-assignment-without-side-effects-using-fold-can-you-do-this-in-haskell/#comments</comments>
		<pubDate>Fri, 11 Apr 2008 18:01:43 +0000</pubDate>
		<dc:creator>cdiggins</dc:creator>
		
		<category><![CDATA[Everything]]></category>

		<guid isPermaLink="false">http://cdiggins.com/2008/04/11/expressing-local-assignment-without-side-effects-using-fold-can-you-do-this-in-haskell/</guid>
		<description><![CDATA[I have been talking lately on my blog and at Lambda-the-Ultimate.org about how a while loop that modifies local values can be expressed as the composition of a sequence of pure functions (e.g. without side-effects) by writing it in a stack-based language. To demonstrate this I have been using the following Java example:

int process(int[] xs)  [...]]]></description>
			<content:encoded><![CDATA[<p>I have been talking lately <a href="http://cdiggins.com/2008/04/10/local-assignment-can-be-pure-functional/">on my blog</a> and at <a href="http://lambda-the-ultimate.org/node/2761">Lambda-the-Ultimate.org</a> about how a while loop that modifies local values can be expressed as the composition of a sequence of pure functions (e.g. without side-effects) by writing it in a stack-based language. To demonstrate this I have been using the following Java example:</p>
<blockquote>
<pre>int process(int[] xs)  {   int zeros = 0;   int sum = 0;   int i=0;   while (i &lt; xs.Size) {     if (xs[i] == 0)       zeros++;     sum += xs[i];     ++i;   }   return sum - zeros; }</pre>
</blockquote>
<p>Now I have heard frequently that the above code is considered to have side-effects because of the local assignments to variables. However, the mapping to pure-functional stack-based code (here shown in Cat) is very straightforward:</p>
<blockquote>
<pre>define process {   // top argument = xs   0 // zeros   0 // sum   0 // i   [     dig3 // bring xs to top of local stack     dupd swap get_at eqz // xs[i] == 0       [[inc] dip3] // inc zeros       []     if     dupd swap get_at // tmp = xs[i]     [bury3] dip // put xs to bottom of local stack     swap [add_int] dip // sum += tmp     inc // ++i   ]   [     dig3 count [bury3] dip // tmp = xs.Size     dupd lt_int // i &lt; tmp   ]   while   pop // remove i   swap sub_int // sum-zeros }</pre>
</blockquote>
<p>What I think is much more interesting is that we can rewrite the above code very succinctly using a fold instruction as follows:</p>
<blockquote>
<pre>define process {
    0 swap 0 [[[inc] dip2] ifeqz +] fold swap sub_int
}</pre>
</blockquote>
<p>What happens in the code example is we place a zero below the list argument (representing the xs variable) and the fold function happily accesses it (increments it to be specific) without violating type-safety and remaining entirely free of side-effects.</p>
<p>The Cat version of fold is different than that of foldl in Haskell because it accepts a tail-polymorphic argument (as opposed to simply a binary function). In other words the function argument can accept any number of arguments (say N + 1), as long as it returns N results whose types match those of the first N arguments. In Cat the type is expressed as:</p>
<blockquote>
<pre>fold : ('A list 'b ('A 'b 'c -&gt; 'A 'b) -&gt; 'A 'b)</pre>
</blockquote>
<p>Type variable names starting with capital letters (e.g. &#8216;A) are type-vector variables and refer to &#8220;the rest of the stack&#8221;. These kinds of type variables are sometimes called &#8220;row variables&#8221;, and this kind of polymorphism is called &#8220;row polymorphism&#8221;. I view the ability to return multiple results as the primary advantage of a stack language.</p>
<p>What happens in the code example above is we place a zero below the list argument that represents the xs variable and the fold function happily accesses it (increments it to be specific) during each iteration without violating type-safety, and all the while remaining entirely side-effect free. The reason it is not a side-effect is because the type of the fold function is automatically widened to encompass the fact that it accesses an additional value on the stack.</p>
<p>Now I have been looking for a way to do this in Haskell, and I can&#8217;t find a way to do it in a general manner unless I package all local variables in a list, and have the fold function return all local arguments as a list. This is a rather laborious step, and is not practical for a compiler. Cat seems to be a win here, because it closely resembles the final assembly code that is to be generated.</p>
<p>I am hoping that maybe some of the Haskell hackers out there may rise to the challenge of providing a simple implementation in Haskell in the very possible case that I am mistaken. </p>
]]></content:encoded>
			<wfw:commentRss>http://cdiggins.com/2008/04/11/expressing-local-assignment-without-side-effects-using-fold-can-you-do-this-in-haskell/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Local Assignment can be Pure Functional</title>
		<link>http://cdiggins.com/2008/04/10/local-assignment-can-be-pure-functional/</link>
		<comments>http://cdiggins.com/2008/04/10/local-assignment-can-be-pure-functional/#comments</comments>
		<pubDate>Thu, 10 Apr 2008 17:46:25 +0000</pubDate>
		<dc:creator>cdiggins</dc:creator>
		
		<category><![CDATA[Everything]]></category>

		<guid isPermaLink="false">http://cdiggins.com/2008/04/10/local-assignment-can-be-pure-functional/</guid>
		<description><![CDATA[In a functional stack-based langauge, it is quite natural to handle local assignment with destroying referential transparency. The following is from a recent post I made to Lambda-the-Ultimate.org in order to ascertain the wider thinking on the problem.
Okay, this might seem like an odd question, but is there contention on whether or not a while loop that performs assignment on [...]]]></description>
			<content:encoded><![CDATA[<p>In a functional stack-based langauge, it is quite natural to handle local assignment with destroying referential transparency. The following is from a <a href="http://lambda-the-ultimate.org/node/2761">recent post I made to Lambda-the-Ultimate.org</a> in order to ascertain the wider thinking on the problem.</p>
<blockquote><p>Okay, this might seem like an odd question, but is there contention on whether or not a while loop that performs assignment on local function variables is pure functional? For example the following Java code:</p>
<pre>int process(int[] xs)  {</pre>
<pre>   int zeros = 0;</pre>
<pre>   int sum = 0;</pre>
<pre>   int i=0;</pre>
<pre>   while (i &lt; xs.Size) {</pre>
<pre>     if (xs[i] == 0)</pre>
<pre>       zeros++;</pre>
<pre>     sum += xs[i];</pre>
<pre>     ++i;</pre>
<pre>   }</pre>
<pre>   return sum - zeros;</pre>
<pre> }</pre>
<p>Now I have heard frequently that the above code is considered to have side-effects because of the local assignments to variables. However, the mapping to pure-functional stack-based code (here shown in Cat) is very straightforward:</p>
<pre>define process {</pre>
<pre>   // top argument = xs</pre>
<pre>   0 // zeros</pre>
<pre>   0 // sum</pre>
<pre>   0 // i</pre>
<pre>   [</pre>
<pre>     dig3 // bring xs to top of local stack</pre>
<pre>     dupd swap get_at eqz // xs[i] == 0</pre>
<pre>       [[inc] dip3] // inc zeros</pre>
<pre>       []</pre>
<pre>     if</pre>
<pre>     dupd swap get_at // tmp = xs[i]</pre>
<pre>     [bury3] dip // put xs to bottom of local stack</pre>
<pre>     swap [add_int] dip // sum += tmp</pre>
<pre>     inc // ++i</pre>
<pre>   ]</pre>
<pre>   [</pre>
<pre>     dig3 count [bury3] dip // tmp = xs.Size</pre>
<pre>     dupd lt_int // i &lt; tmp</pre>
<pre>   ]</pre>
<pre>   while</pre>
<pre>   pop // remove i</pre>
<pre>   swap sub_int // sum-zeros</pre>
<pre> }</pre>
<p>So, my problem is that I am not sure whether this is old news or new news. Any feedback would be appreciated.</p></blockquote>
<p>For those interested, the type of &#8220;while&#8221; in Cat is (&#8217;A (&#8217;A -&gt; &#8216;A) (&#8217;A -&gt; &#8216;A bool) -&gt; &#8216;A). In other words, while takes a stack with two functions on the top, the first one is a predicate and the next one is the loop body. If the predicate returns true, the loop body is executed, and the process is repeated. The loop body takes the rest of the stack as an argument, and returns it in the same state. The predicate is the same, but in addition pushes a boolean value.</p>
<p>If you really feel like geeking out then we can define while from core primitives using the &#8220;m&#8221; recursive combinator (m == dup apply) as follows:</p>
<pre>define m_while : ('A ('A -&gt; 'A) ('A -&gt; 'A bool) -&gt; 'A)  

{{  

  desc:  

    A while function written using the m (a.k.a. U) combinator  

  test:  

    in: 1 5 [[2 mul_int] dip dec] [is_neqz] m_while pop  

    out: 32  

}}  

{
  // [$A] [$B]  

  [dip swap] papply      // [$A] [[$B] dip swap]  

  swap               // [[$B] dip swap] [$A]  

  [dip m] papply     // [[$B] dip swap] [[$A] swap m]  

  quote compose      // [[$B] dip swap [[$A] dip m]]  

  [[pop] if] compose // [[$B] dip swap [[$A] dip m] [pop] if]  

  m
}</pre>
<p>To be completely forthright there is a small bug in the &#8220;m_while&#8221; code. The actual inferred type is &#8220;(&#8217;A (&#8217;B -&gt; &#8216;C) (&#8217;A -&gt; &#8216;B bool) -&gt; &#8216;B)&#8221;. This is because my type-inference engine can not currently handle recursive types properly (it used to, honest!). This is just a question of finding the time to sit down and fix it, at least theoretically there are no hurdles preventing one from performing inference of equi-recursive types.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdiggins.com/2008/04/10/local-assignment-can-be-pure-functional/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Reversing a List in 3 Instructions</title>
		<link>http://cdiggins.com/2008/04/07/reversing-a-list-in-3-instructions/</link>
		<comments>http://cdiggins.com/2008/04/07/reversing-a-list-in-3-instructions/#comments</comments>
		<pubDate>Mon, 07 Apr 2008 05:25:38 +0000</pubDate>
		<dc:creator>cdiggins</dc:creator>
		
		<category><![CDATA[Everything]]></category>

		<guid isPermaLink="false">http://cdiggins.com/2008/04/07/reversing-a-list-in-3-instructions/</guid>
		<description><![CDATA[Cat is my functional stack-based language based on Joy. One of the interesting characteristics Cat shares with Joy is an incredible succinctness, which rivals that of array-oriented languages such as K or Q. A demonstration of this is how list reversal can be achieved with just three instructions:
nil [cons] fold 
You would use it like this:
&#62;&#62; [1 2 [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.cat-language.com">Cat</a> is my functional stack-based language based on <a href="http://www.latrobe.edu.au/philosophy/phimvt/joy.html">Joy</a>. One of the interesting characteristics Cat shares with Joy is an incredible succinctness, which rivals that of array-oriented languages such as K or Q. A demonstration of this is how list reversal can be achieved with just three instructions:</p>
<blockquote><p>nil [cons] fold </p></blockquote>
<p>You would use it like this:</p>
<blockquote><p>&gt;&gt; [1 2 3 4] list nil [cons] fold <br />
stack: (4 3 2 1)</p></blockquote>
<p>One of the advantages of stack-based languages is that they are executed left to right. This means we can enter each instruction on separate lines which is useful for pedgogical purposes because we can see the stack after each instruction.</p>
<p>The first thing that happens is that we push a function literal (called a quotation) on to the stack:</p>
<blockquote><p>&gt;&gt; [1 2 3 4]<br />
stack: [1 2 3 4]</p></blockquote>
<p>This pushes a function on the stack that if executed would push the values 1,2,3 and 4. Next we call the &#8220;list&#8221; instruction</p>
<blockquote><p>&gt;&gt; list<br />
stack: (1 2 3 4)</p></blockquote>
<p>This converts a quotation into a list. We can convert any function into a list as long as it doesn&#8217;t pop any values off of the stack. The resulting list contains the values that would be generated if we executed the function on any empty stack.</p>
<p>The next thing we do is push an empty list on the stack using the &#8220;nil&#8221; instruction.</p>
<blockquote><p>&gt;&gt; nil<br />
stack: (1 2 3 4) ()</p></blockquote>
<p>Now we push another quotation containing the &#8220;cons&#8221; instruction on the stack.</p>
<blockquote><p>&gt;&gt; [cons]<br />
stack: (1 2 3 4) () [cons]</p></blockquote>
<p>The &#8220;cons&#8221; instruction is similar to the Lisp/Scheme instruction that appends a value to a list. For example &#8220;[1 2 3] list 4 cons&#8221; is the same as &#8220;[1 2 3 4] list&#8221;. You may notice then that the front of a list is the rightmost value. This is in keeping with the postfix notation of Cat.</p>
<p>The final instruction is the &#8220;fold&#8221; instruction.</p>
<blockquote><p>&gt;&gt; fold<br />
stack: (4 3 2 1)</p></blockquote>
<p>The Cat &#8220;fold&#8221; instruction, which is similar to the &#8220;<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v%3Afoldl">foldl</a>&#8221; function in Haskell, takes a binary function off of the stack, an initial accumulator value, and a list. The binary function is applied successively to each element in the list and the accumulated value. On each iteration the accumulated value is updated with the result of applying the function. Another example of using the fold function is when summing the values in a list (e.g. &#8220;[1 2 3 4] list 0 [+] fold&#8221;).</p>
<p>In future posts I will go into more detail about the Cat &#8220;fold&#8221; function, how it is constructed, and how it differs from the Haskell &#8220;foldl&#8221; function. For now I hope you will <a href="http://code.google.com/p/cat-language/downloads/list">download the interpreter</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdiggins.com/2008/04/07/reversing-a-list-in-3-instructions/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Cat version 1.0 beta 1</title>
		<link>http://cdiggins.com/2008/04/06/cat-version-10-beta-1/</link>
		<comments>http://cdiggins.com/2008/04/06/cat-version-10-beta-1/#comments</comments>
		<pubDate>Sun, 06 Apr 2008 16:22:14 +0000</pubDate>
		<dc:creator>cdiggins</dc:creator>
		
		<category><![CDATA[Everything]]></category>

		<guid isPermaLink="false">http://cdiggins.com/2008/04/06/cat-version-10-beta-1/</guid>
		<description><![CDATA[I have just released version 1.0 beta 1 of Cat at http://code.google.com/p/cat-language/downloads/list. So what does &#8216;beta 1&#8242; mean for Cat? It means that virtually all of the final functionality is there. I have run it through a pretty comprehensive test suite (477 unit tests). I have replaced many of the predefined primitives and replaced them with library [...]]]></description>
			<content:encoded><![CDATA[<p>I have just released version 1.0 beta 1 of Cat at <a href="http://code.google.com/p/cat-language/downloads/list">http://code.google.com/p/cat-language/downloads/list</a>. So what does &#8216;beta 1&#8242; mean for Cat? It means that virtually all of the final functionality is there. I have run it through a pretty comprehensive test suite (477 unit tests). I have replaced many of the predefined primitives and replaced them with library functions. I have also added tail-call optimization. This means that recursive calls in tail-position will not use superflous stack-space, so it makes recursive programming idioms more practical. </p>
<p>A number of primitives now have their arguments swapped, and some don&#8217;t reproduce a copy of their argument, so this will probably break some existing code. A new feature for debugging is the &#8220;#trace&#8221; meta-command. This allows the programmer to follow commands as they are executed. Internally the code is now much cleaner. It compiles without warnings on Mono, and a lot of superflous code has been removed. The code should be much easier for people to read and modify (e.g. only one kind of list instead of 20 different ones). I also removed infinite lists (i.e. generators) from Cat for the time being, but I may reintroduce them in later versions.</p>
<p>There are some known issues still outstanding, which will be fixed in later beta releases. The biggest outstanding issue is that the type-inference engine works for only correctly typed phrases. That is,, it will produce garbage rather than an error when given an ill-typed phrase. This is not a huge problem, the fix is nearly ready, it will just be a question of finding the time to integrate and test it. The rewriting rules are also not yet tested. While the rewriting system itself is very stable, the rules included in the library will still likely have some bugs in them.</p>
<p>The documentation hasn&#8217;t been updated yet to reflect the current version. Hopefully by the middle of this week it will be ready. I am in the process of setting up a mechanism that will allow me to auto-generate much of the specification from the implementation.</p>
<p>I look forward to hearing your comments on the new version, and I would particularly enjoy hearing about how you are using Cat. Feel free to drop me a line at <a href="mailto:cdiggins@gmail.com">cdiggins@gmail.com</a> or make a post to the discussion group (<a href="http://groups.google.com/group/catlanguage">http://groups.google.com/group/catlanguage</a>) to introduce yourself.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdiggins.com/2008/04/06/cat-version-10-beta-1/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
