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

<channel>
	<title>Intuitionistically Uncertain &#187; Programming</title>
	<atom:link href="http://hircus.wordpress.com/category/technology/programming-languages/feed/" rel="self" type="application/rss+xml" />
	<link>http://hircus.wordpress.com</link>
	<description>Michel explores computing and assorted gadgetries</description>
	<lastBuildDate>Sun, 25 Oct 2009 20:13:21 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='hircus.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/1bdf5df04841b40a29e5697ed558bd72?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Intuitionistically Uncertain &#187; Programming</title>
		<link>http://hircus.wordpress.com</link>
	</image>
			<item>
		<title>Logic programming on the JVM</title>
		<link>http://hircus.wordpress.com/2009/10/19/logic-programming-on-the-jvm/</link>
		<comments>http://hircus.wordpress.com/2009/10/19/logic-programming-on-the-jvm/#comments</comments>
		<pubDate>Mon, 19 Oct 2009 06:34:19 +0000</pubDate>
		<dc:creator>Michel</dc:creator>
				<category><![CDATA[Lua]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[Scheme]]></category>
		<category><![CDATA[minikanren-scala]]></category>

		<guid isPermaLink="false">http://hircus.wordpress.com/?p=296</guid>
		<description><![CDATA[Just a quick post (the time is rather late) to note that my port of the Mini Kanren logic programming system to Scala is now available for download (and bug reports) on GitHub; for the scaladoc API documentation and a presentation discussing the porting effort, visit the project homepage.
It has an almost-complete numerical stack &#8212; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=296&subd=hircus&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Just a quick post (the time is rather late) to note that my port of the <a href="http://kanren.sourceforge.net/">Mini Kanren</a> logic programming system to <a href="http://scala-lang.org/">Scala</a> is now available for download (and bug reports) on <a href="http://github.com/hircus/minikanren-scala">GitHub</a>; for the scaladoc API documentation and a presentation discussing the porting effort, visit the <a href="http://hircus.multics.org/kanren">project homepage</a>.</p>
<p>It has an almost-complete numerical stack &#8212; the missing arithmetic relations are not coded yet because I was documenting and/or getting larger test cases to work &#8212; and likewise with list-processing support. The years-old stack overflow problem I initially attributed (when taking a programming language course, and having no time to debug thoroughly) to Java&#8217;s lack of TCO turns out to be fixable by some judicious call-by-name optimizations.</p>
<p>Having just discovered <a href="http://metalua.luaforge.net/">metalua</a> and its macro goodness, I&#8217;d say that after bringing Kanren to the JVM, the next step would be to have a C-embeddable Kanren written in metalua. There might be some performance snags, though &#8212; the Scala port is currently about 3x slower than Mini Kanren running on Petite Chez Scheme.</p>
<p>Technorati Tags: <a class="performancingtags" href="http://technorati.com/tag/kanren" rel="tag">kanren</a>, <a class="performancingtags" href="http://technorati.com/tag/mini-kanren" rel="tag">mini-kanren</a>, <a class="performancingtags" href="http://technorati.com/tag/functional" rel="tag">functional</a>, <a class="performancingtags" href="http://technorati.com/tag/logic" rel="tag">logic</a>, <a class="performancingtags" href="http://technorati.com/tag/programming" rel="tag">programming</a>, <a class="performancingtags" href="http://technorati.com/tag/lua" rel="tag">lua</a>, <a class="performancingtags" href="http://technorati.com/tag/scala" rel="tag">scala</a></p>
<div class="zemanta-pixie"><img class="zemanta-pixie-img" alt="" src="http://img.zemanta.com/pixy.gif?x-id=1f5a4ad7-fe86-8f71-a72a-a3718137c291" /></div>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/hircus.wordpress.com/296/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/hircus.wordpress.com/296/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/hircus.wordpress.com/296/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/hircus.wordpress.com/296/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/hircus.wordpress.com/296/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/hircus.wordpress.com/296/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/hircus.wordpress.com/296/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/hircus.wordpress.com/296/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/hircus.wordpress.com/296/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/hircus.wordpress.com/296/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=296&subd=hircus&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://hircus.wordpress.com/2009/10/19/logic-programming-on-the-jvm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3ac78be5e356078c3d5a126840934538?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hircus</media:title>
		</media:content>

		<media:content url="http://img.zemanta.com/pixy.gif?x-id=1f5a4ad7-fe86-8f71-a72a-a3718137c291" medium="image" />
	</item>
		<item>
		<title>OOP in Lua: abstract methods</title>
		<link>http://hircus.wordpress.com/2009/10/18/oop-in-lua-abstract-methods/</link>
		<comments>http://hircus.wordpress.com/2009/10/18/oop-in-lua-abstract-methods/#comments</comments>
		<pubDate>Mon, 19 Oct 2009 02:37:21 +0000</pubDate>
		<dc:creator>Michel</dc:creator>
				<category><![CDATA[FLOSS]]></category>
		<category><![CDATA[Lua]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[lua-abc]]></category>

		<guid isPermaLink="false">http://hircus.wordpress.com/2009/10/18/oop-in-lua-abstract-methods/</guid>
		<description><![CDATA[I discovered Python&#8217;s delightful abc module (introduced in PEP 3119) a while back, and have been wondering how a similar functionality could be introduced to other dynamic languages (e.g. Lua and Ruby).
Being more familiar with the former, over the weekend I decided to try and write an equivalent module. Two problems came to mind:

Python uses [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=293&subd=hircus&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I discovered Python&#8217;s delightful <strong>abc</strong> module (introduced in <a href="http://www.python.org/dev/peps/pep-3119/">PEP 3119</a>) a while back, and have been wondering how a similar functionality could be introduced to other dynamic languages (e.g. Lua and Ruby).</p>
<p>Being more familiar with the former, over the weekend I decided to try and write an equivalent module. Two problems came to mind:</p>
<ul>
<li>Python uses an <strong>@abstractmethod</strong> decorator to mark an otherwise-normal function definition. While a decorator pattern can be used in Lua (though not a standard practice), this Python decorator works by setting an attribute in the function it decorates:<br />
<blockquote><p><tt>funcobj.__isabstractmethod__ = True</tt></p></blockquote>
<p>This is not an option in Lua, because functions don&#8217;t have individual metatables.</li>
<li>Python has a standard way of doing OOP, and the enforcement that an instantiated class does not contain any abstract method is done in the <strong>__new__</strong> method of the ABCMeta metaclass. Contrast to Lua, where OOP is normally done in an ad-hoc manner &#8212; and even the <a href="http://loop.luaforge.net/">LOOP library</a> provides several OOP inheritance mechanisms.</li>
</ul>
<p>The solution I adopted is delightfully simple: the <strong>abc</strong> module provides two functions: an <em>amethod</em> function that throws an exception no matter what arguments it is passed, and a <em>verify</em> function that takes a classname, and reflects on the members (using <em>loop.cached.allmembers</em> if possible, falling back to <em>pairs</em> if loop is not installed) and check if any of them is equal to <em>amethod</em>.</p>
<p>This works even on simple tables, though it shifts the responsibility a bit to the programmer: unit tests should be used to verify each created class. As a fallback, attempts to use the abstract methods will fail at runtime, but that&#8217;s taking dynamism a bit too far&#8230;</p>
<p>Still working out how to get the Pythonic behavior (instantiating a class with some abstract members should fail) when using LOOP. Meanwhile, <a href="http://github.com/hircus/lua-abc">try it out</a> for yourself and make sure to file bug reports!</p>
<p>Technorati Tags: <a class="performancingtags" rel="tag" href="http://technorati.com/tag/lua">lua</a>, <a class="performancingtags" rel="tag" href="http://technorati.com/tag/python">python</a>, <a class="performancingtags" rel="tag" href="http://technorati.com/tag/oop">oop</a>, <a class="performancingtags" rel="tag" href="http://technorati.com/tag/abc">abc</a>, <a class="performancingtags" rel="tag" href="http://technorati.com/tag/abstract-methods">abstract-methods</a>, <a class="performancingtags" rel="tag" href="http://technorati.com/tag/virtual-functions">virtual-functions</a>, <a class="performancingtags" rel="tag" href="http://technorati.com/tag/interfaces">interfaces</a>, <a class="performancingtags" rel="tag" href="http://technorati.com/tag/traits">traits</a></p>
<div class="zemanta-pixie"><img class="zemanta-pixie-img" src="http://img.zemanta.com/pixy.gif?x-id=7c6bde0c-b2e3-8cc5-8668-246976b4bf99" alt="" /></div>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/hircus.wordpress.com/293/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/hircus.wordpress.com/293/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/hircus.wordpress.com/293/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/hircus.wordpress.com/293/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/hircus.wordpress.com/293/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/hircus.wordpress.com/293/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/hircus.wordpress.com/293/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/hircus.wordpress.com/293/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/hircus.wordpress.com/293/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/hircus.wordpress.com/293/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=293&subd=hircus&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://hircus.wordpress.com/2009/10/18/oop-in-lua-abstract-methods/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3ac78be5e356078c3d5a126840934538?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hircus</media:title>
		</media:content>

		<media:content url="http://img.zemanta.com/pixy.gif?x-id=7c6bde0c-b2e3-8cc5-8668-246976b4bf99" medium="image" />
	</item>
		<item>
		<title>[PYTHON] Find of the day: B+ Tree-based lists</title>
		<link>http://hircus.wordpress.com/2009/10/10/python-find-of-the-day-b-tree-based-lists/</link>
		<comments>http://hircus.wordpress.com/2009/10/10/python-find-of-the-day-b-tree-based-lists/#comments</comments>
		<pubDate>Sat, 10 Oct 2009 22:43:08 +0000</pubDate>
		<dc:creator>Michel</dc:creator>
				<category><![CDATA[Fedora]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://hircus.wordpress.com/2009/10/10/python-find-of-the-day-b-tree-based-lists/</guid>
		<description><![CDATA[I was looking at implementing Clojure&#8217;s persistent data structures on other languages &#8212; being able to assume that collections are immutable make writing concurrent programs much easier, since these collections can be shared without locking.
While looking if this has been done before, I came across a rejected Python Enhancement Proposal:

The common case for list operations [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=291&subd=hircus&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I was looking at implementing Clojure&#8217;s persistent data structures on other languages &#8212; being able to assume that collections are immutable make writing concurrent programs much easier, since these collections can be shared without locking.</p>
<p>While looking if this has been done before, I came across a rejected <a href="http://www.python.org/dev/peps/pep-3128/">Python Enhancement Proposal</a>:<br />
<blockquote>
<p>The common case for list operations is on small lists.  The current array-based list implementation excels at small lists due to the strong locality of reference and infrequency of memory allocation operations.  However, an array takes O(n) time to insert and delete elements, which can become problematic as the list gets large.</p>
<p>This PEP introduces a new data type, the BList, that has array-like and tree-like aspects.  It enjoys the same good performance on small lists as the existing array-based implementation, but offers superior asymptotic performance for most operations.  This PEP proposes replacing the makes two mutually exclusive proposals for including the BList type in Python:</p>
<ol class="arabic simple">
<li>Add it to the collections module, or</li>
<li>Replace the existing list type</li>
</ol>
</blockquote>
<p>It is currently rejected, but could be added to the collections module if there is sufficient outside interest. This is not quite the immutable vector from Clojure, but close enough: one merely needs to subclass it, and modify the setters (__setitem__, __setslice__, etc.) to first copy the collection and then operate on the copy. Copy-on-write ensures that only the changed leaf is actually copied, plus the internal nodes on the path leading to the leaf.</p>
<p>I&#8217;ve packaged this for Fedora (<a href="https://bugzilla.redhat.com/show_bug.cgi?id=528288">review request</a>). Anyone cares to review it?</p>
<p>Technorati Tags: <a class="performancingtags" href="http://technorati.com/tag/fedora" rel="tag">fedora</a>, <a class="performancingtags" href="http://technorati.com/tag/linux" rel="tag">linux</a>, <a class="performancingtags" href="http://technorati.com/tag/python" rel="tag">python</a>, <a class="performancingtags" href="http://technorati.com/tag/data-structures" rel="tag">data-structures</a></p>
<div class="zemanta-pixie"><img class="zemanta-pixie-img" alt="" src="http://img.zemanta.com/pixy.gif?x-id=8746fd35-9208-8e43-9f60-262576171ae3" /></div>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/hircus.wordpress.com/291/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/hircus.wordpress.com/291/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/hircus.wordpress.com/291/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/hircus.wordpress.com/291/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/hircus.wordpress.com/291/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/hircus.wordpress.com/291/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/hircus.wordpress.com/291/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/hircus.wordpress.com/291/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/hircus.wordpress.com/291/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/hircus.wordpress.com/291/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=291&subd=hircus&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://hircus.wordpress.com/2009/10/10/python-find-of-the-day-b-tree-based-lists/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3ac78be5e356078c3d5a126840934538?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hircus</media:title>
		</media:content>

		<media:content url="http://img.zemanta.com/pixy.gif?x-id=8746fd35-9208-8e43-9f60-262576171ae3" medium="image" />
	</item>
		<item>
		<title>ContactBot: noiselessly telling people where you are</title>
		<link>http://hircus.wordpress.com/2009/09/29/contactbot-noiselessly-telling-people-where-you-are/</link>
		<comments>http://hircus.wordpress.com/2009/09/29/contactbot-noiselessly-telling-people-where-you-are/#comments</comments>
		<pubDate>Tue, 29 Sep 2009 08:46:00 +0000</pubDate>
		<dc:creator>Michel</dc:creator>
				<category><![CDATA[FLOSS]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Twitter]]></category>

		<guid isPermaLink="false">http://hircus.wordpress.com/2009/09/29/contactbot-noiselessly-telling-people-where-you-are/</guid>
		<description><![CDATA[Ever been in a situation where you are abroad for a short period of time, or you forgot your mobile phone charger, or otherwise not reachable at your normal phone number? I&#8217;ve been there, and it can be annoying to handle, unless you&#8217;re the kind of person who keeps lists (and even then, text messages [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=274&subd=hircus&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Ever been in a situation where you are abroad for a short period of time, or you forgot your mobile phone charger, or otherwise not reachable at your normal phone number? I&#8217;ve been there, and it can be annoying to handle, unless you&#8217;re the kind of person who keeps lists (and even then, text messages ain&#8217;t free!). Or worse, you have several mobile phones, and you juggle them around, and people don&#8217;t know which one you are on right now&#8230;</p>
<p>There&#8217;s Google Voice, and other forwarding services, but that would require giving people a different number; one whose voice quality is probably not as good. And if the new number is only needed temporarily, that seems like an overkill.</p>
<p>Enter <a href="http://github.com/hircus/contactbot">ContactBot</a>. It provides a Twitter bot; you can tell it your phone number and location, and your friends (people <i>you</i> follow, not people who follow you) can then query the bot for your whereabouts.</p>
<p>The test bot is <a href="http://twitter.com/hircus_contact">@hircus_contact</a>. It is rate-limited at the basic 150 messages per hour, so if you want to heavily test it, or do not want to give me your phone number (I run the bot, after all!), feel free to check out the code and run your own bot!</p>
<p>Note that the bot does not currently add you back, so you&#8217;d have to wait until I personally add you before you can send direct messages. Should take less than a day.</p>
<p>TODO: timezone support, so the bot can warn your tactless friends not to call you at 3 a.m., documentation, and test cases. And online help.</p>
<p>Current command set:
<ul>
<li>D hircus_contact set phone 555-5555</li>
<li>D hircus_contact set location New York, NY</li>
<li>D hircus_contact phone ma_cherie</li>
<li>D hircus_contact location ma_cherie</li>
</ul>
<p>Technorati Tags: <a class="performancingtags" href="http://technorati.com/tag/bots" rel="tag">bots</a>, <a class="performancingtags" href="http://technorati.com/tag/contacts" rel="tag">contacts</a>, <a class="performancingtags" href="http://technorati.com/tag/python" rel="tag">python</a>, <a class="performancingtags" href="http://technorati.com/tag/software" rel="tag">software</a>, <a class="performancingtags" href="http://technorati.com/tag/twitter" rel="tag">twitter</a></p>
<div class="zemanta-pixie"><img class="zemanta-pixie-img" alt="" src="http://img.zemanta.com/pixy.gif?x-id=9511a794-7ec1-8681-b8d9-12eb0c7cc496" /></div>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/hircus.wordpress.com/274/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/hircus.wordpress.com/274/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/hircus.wordpress.com/274/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/hircus.wordpress.com/274/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/hircus.wordpress.com/274/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/hircus.wordpress.com/274/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/hircus.wordpress.com/274/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/hircus.wordpress.com/274/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/hircus.wordpress.com/274/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/hircus.wordpress.com/274/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=274&subd=hircus&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://hircus.wordpress.com/2009/09/29/contactbot-noiselessly-telling-people-where-you-are/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3ac78be5e356078c3d5a126840934538?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hircus</media:title>
		</media:content>

		<media:content url="http://img.zemanta.com/pixy.gif?x-id=9511a794-7ec1-8681-b8d9-12eb0c7cc496" medium="image" />
	</item>
		<item>
		<title>A new compiler, with static analyzer to boot</title>
		<link>http://hircus.wordpress.com/2009/09/10/a-new-compiler-with-static-analyzer-to-boot/</link>
		<comments>http://hircus.wordpress.com/2009/09/10/a-new-compiler-with-static-analyzer-to-boot/#comments</comments>
		<pubDate>Thu, 10 Sep 2009 19:49:46 +0000</pubDate>
		<dc:creator>Michel</dc:creator>
				<category><![CDATA[Fedora]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://hircus.wordpress.com/2009/09/10/a-new-compiler-with-static-analyzer-to-boot/</guid>
		<description><![CDATA[The LLVM Compiler Infrastructure Project is getting ready for their 2.6 release later this month, and for the first time, the Clang compiler front-end is considered ready enough that a snapshot of it is provided, together with the LLVM snapshot itself.This is of great interest for several reasons. Firstly, Obj-C support in GCC has always [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=257&subd=hircus&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>The <a href="http://llvm.org/">LLVM</a> Compiler Infrastructure Project is getting ready for their 2.6 release later this month, and for the first time, the <a href="http://clang.llvm.org">Clang</a> compiler front-end is considered ready enough that a snapshot of it is provided, together with the LLVM snapshot itself.<br /><span id="more-257"></span><br />This is of great interest for several reasons. Firstly, Obj-C support in GCC has always been rather tenuous: NeXT, and later Apple, rather dislike that compiler suite&#8217;s licensing provision, and as a result the Obj-C runtime is rather neglected. New language features (e.g. Obj-C 2.0, with its garbage collection) will likely not see the light of day.</p>
<p>Secondly, between its support for a myriad of language front-ends, and its venerable age, GCC is not as easily extendable as it could have been. Hard to integrate it with a modern IDE, as Apple is noticing.</p>
<p>And lastly, LLVM IR is a *beautiful* assembly language to write to. Arbitrary-precision integer arithmetic? Check. Functions? Check. Support for various calling conventions? Check. Infinite SSA registers? Check.</p>
<p>Actually, that&#8217;s not even the last point. Clang also comes with a <a href="http://clang-analyzer.llvm.org/">static analyzer</a>, that lets you hook it into a GCC compilation process &#8212; amusingly, it does not seem to work yet when using Clang as the compiler. I&#8217;d be happy to stand corrected on this, though. It provides readable warnings in HTML!</p>
<p>And now all this is yours to test! If you are on Rawhide, install as such:</p>
<pre>
$ yum install llvm   # to install LLVM itself
$ yum install clang  # to install the clang compiler
</pre>
<p>Or get clang and clang-analyzer together by using</p>
<pre>&lt;br /&gt;$ yum install clang-analyzer&lt;br /&gt;</pre>
<p>Fedora 11 users will have to wait until slightly after 2.6 final is out. We are trying to get OpenGTL packaged, and at this point it does not build against LLVM 2.6 due to API changes.Technorati Tags: <a class="performancingtags" href="http://technorati.com/tag/compiler" rel="tag">compiler</a>, <a class="performancingtags" href="http://technorati.com/tag/fedora" rel="tag">fedora</a>, <a class="performancingtags" href="http://technorati.com/tag/linux" rel="tag">linux</a>, <a class="performancingtags" href="http://technorati.com/tag/llvm" rel="tag">llvm</a>, <a class="performancingtags" href="http://technorati.com/tag/programming" rel="tag">programming</a></p>
<div class="zemanta-pixie"><img class="zemanta-pixie-img" alt="" src="http://img.zemanta.com/pixy.gif?x-id=72ec622d-218c-83b9-8504-122c1c73a256" /></div>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/hircus.wordpress.com/257/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/hircus.wordpress.com/257/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/hircus.wordpress.com/257/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/hircus.wordpress.com/257/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/hircus.wordpress.com/257/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/hircus.wordpress.com/257/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/hircus.wordpress.com/257/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/hircus.wordpress.com/257/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/hircus.wordpress.com/257/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/hircus.wordpress.com/257/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=257&subd=hircus&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://hircus.wordpress.com/2009/09/10/a-new-compiler-with-static-analyzer-to-boot/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3ac78be5e356078c3d5a126840934538?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hircus</media:title>
		</media:content>

		<media:content url="http://img.zemanta.com/pixy.gif?x-id=72ec622d-218c-83b9-8504-122c1c73a256" medium="image" />
	</item>
		<item>
		<title>Python tail-call optimization, done right</title>
		<link>http://hircus.wordpress.com/2008/06/21/python-tail-call-optimization-done-right/</link>
		<comments>http://hircus.wordpress.com/2008/06/21/python-tail-call-optimization-done-right/#comments</comments>
		<pubDate>Sat, 21 Jun 2008 18:08:11 +0000</pubDate>
		<dc:creator>Michel</dc:creator>
				<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://hircus.wordpress.com/2008/06/21/python-tail-call-optimization-done-right/</guid>
		<description><![CDATA[ It occured to me this morning to revisit the issue of tail-call-optmization using function decorators in Python. Last time I checked, the working trick involves stack inspection (works only in CPython) and throwing an exception whenever a tail call is detected. In short: non-portable and slow. I posted an enhancement here that allows for [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=227&subd=hircus&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><div> It occured to me this morning to revisit the issue of tail-call-optmization using function decorators in Python. Last time I checked, the working trick involves stack inspection (works only in CPython) and throwing an exception whenever a tail call is detected. In short: non-portable and slow. I posted <a href="http://hircus.wordpress.com/2006/03/02/python-tail-call-decorator/">an enhancement</a> here that allows for mutual recursion (function A tail-calling function B tail-calling function A &#8230;), but it did not occur to me that the stack inspection hack, clever as it is, could be improved on.</p>
<p>Improved on it has: Miguel Perez is reporting that his solution runs pretty much as fast as normal looping. Supports mutual recursion and is completely portable too.</p>
</p></div>
<table cellpadding="0" cellspacing="0" width="100%" style="font-family:arial;color:#333333;background:#ffffff;border:solid 4px #e5e5e5;width:100%;clear:left;margin:12px 0;">
<tr>
<td valign="top">
<table cellpadding="0" cellspacing="0" width="100%" class="CM_CTB_Content_Wrap" style="background-color:#ffffff;margin:0;padding:0;">
<tr>
<td valign="top">
<table cellpadding="0" cellspacing="0" width="100%" style="border-bottom:solid 1px #dcdcdc;white-space:nowrap;margin-bottom:8px;background-color:#eeeeee;background-image:url(http://clipmarks.com/images/source-bg.gif);background-repeat:repeat-x;height:24px;line-height:24px;vertical-align:middle;padding-bottom:4px;color:#666666;font-size:10px;">
<tr>
<td valign="top"><a href="http://clipmarks.com/clip-to-blog/" title="clipmarks' clip-to-blog"><img src="http://content.clipmarks.com/blog_icon/5e0b9106-9353-47d8-a185-a9db888200bc/05F53D18-6896-4275-901A-4194F4D18B4C/" alt="" width="19" height="19" border="0" style="vertical-align:middle;display:inline;border:none;float:none;margin:0 4px;" /></a>clipped from <a title="http://groups.google.com/group/comp.lang.python/msg/9b047d1392f2b8ec" href="http://groups.google.com/group/comp.lang.python/msg/9b047d1392f2b8ec">groups.google.com</a></td>
</tr>
</table>
<table cellpadding="0" cellspacing="0" width="100%" style="text-align:left;background:transparent;border:none;margin:4px 0 8px;padding:0 8px;">
<tr>
<td valign="top"><!-- CLIPPED FROM: http://groups.google.com/group/comp.lang.python/msg/9b047d1392f2b8ec --><FONT face="Courier, Monospaced">Please critique this tail call optimizing decorator I&#8217;ve written. I&#8217;ve tried to fix the pitfalls of other proposed decorators, and the result is this one that supports mutual recursion, does not use exceptions, stack inspection or any implementation-dependent hack, and is pretty short and fast &#8211; the fastest out of the ones I could find and try. In fact, in tail-recursive environments I tested the impact of using the decorator is difficult to even measure, as the extra time the decorator takes to run is probably saved by the better use of cache memory. The only caveat is that if used in a function that&#8217;s not called in a tail-recursive fashion, bad things will happen.</FONT></td>
</tr>
</table>
</td>
</tr>
</table>
<div style="margin:0 6px 6px 4px;">
<table style="font-size:11px;border-spacing:0;padding:0;" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td style="background:transparent;border-width:0;padding:0;">&nbsp;</td>
<td align="right" style="background:transparent;width:107px;border-width:0;padding:0;" width="107"><a href="http://clipmarks.com/share/05F53D18-6896-4275-901A-4194F4D18B4C/blog/" title="blog or email this clip"><img src="http://content8.clipmarks.com/images/c2b-foot.png" border="0" alt="blog it" width="107" height="17" style="border-width:0;margin:0;padding:0;" /></a></td>
</tr>
</table>
</div>
</td>
</tr>
</table>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/hircus.wordpress.com/227/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/hircus.wordpress.com/227/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/hircus.wordpress.com/227/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/hircus.wordpress.com/227/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/hircus.wordpress.com/227/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/hircus.wordpress.com/227/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/hircus.wordpress.com/227/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/hircus.wordpress.com/227/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/hircus.wordpress.com/227/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/hircus.wordpress.com/227/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/hircus.wordpress.com/227/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/hircus.wordpress.com/227/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=227&subd=hircus&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://hircus.wordpress.com/2008/06/21/python-tail-call-optimization-done-right/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3ac78be5e356078c3d5a126840934538?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hircus</media:title>
		</media:content>

		<media:content url="http://content.clipmarks.com/blog_icon/5e0b9106-9353-47d8-a185-a9db888200bc/05F53D18-6896-4275-901A-4194F4D18B4C/" medium="image" />

		<media:content url="http://content8.clipmarks.com/images/c2b-foot.png" medium="image">
			<media:title type="html">blog it</media:title>
		</media:content>
	</item>
		<item>
		<title>C types 101</title>
		<link>http://hircus.wordpress.com/2008/06/08/c-types-101/</link>
		<comments>http://hircus.wordpress.com/2008/06/08/c-types-101/#comments</comments>
		<pubDate>Mon, 09 Jun 2008 01:12:17 +0000</pubDate>
		<dc:creator>Michel</dc:creator>
				<category><![CDATA[FLOSS]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://hircus.wordpress.com/2008/06/08/c-types-101/</guid>
		<description><![CDATA[ I was cleaning up the code of an application that I&#8217;m packaging for Fedora, and was Googling for information on size_t (in the code, a size_t variable was being printed as a normal integer (%d), which triggered a compiler warning, and I forgot what the relevant option is. Ended up finding it in printf&#8217;s [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=223&subd=hircus&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><div> I was cleaning up the code of an application that I&#8217;m packaging for Fedora, and was Googling for information on <strong>size_t</strong> (in the code, a size_t variable was being printed as a normal integer (<tt>%d</tt>), which triggered a compiler warning, and I forgot what the relevant option is. Ended up finding it in printf&#8217;s manpage) when I discovered this rather well-written gem. </div>
<table cellpadding="0" cellspacing="0" width="100%" style="font-family:arial;color:#333333;background:#ffffff;border:solid 4px #e5e5e5;width:100%;clear:left;margin:12px 0;">
<tr>
<td valign="top">
<table cellpadding="0" cellspacing="0" width="100%" class="CM_CTB_Content_Wrap" style="background-color:#ffffff;margin:0;padding:0;">
<tr>
<td valign="top">
<table cellpadding="0" cellspacing="0" width="100%" style="border-bottom:solid 1px #dcdcdc;white-space:nowrap;margin-bottom:8px;background-color:#eeeeee;background-image:url('http://clipmarks.com/images/source-bg.gif');background-repeat:repeat-x;height:24px;line-height:24px;vertical-align:middle;padding-bottom:4px;color:#666666;font-size:10px;">
<tr>
<td valign="top"><a href="http://clipmarks.com/clip-to-blog/" title="clipmarks' clip-to-blog"><img src="http://content.clipmarks.com/blog_icon/fb0e31ea-247b-4396-b879-55723cb0754c/7AF356FF-F474-45F5-AE3E-808FDE1D80F8/" alt="" width="19" height="19" border="0" style="vertical-align:middle;display:inline;border:none;float:none;margin:0 4px;" /></a>clipped from <a title="http://www.embedded.com/columns/programmingpointers/200900195" href="http://www.embedded.com/columns/programmingpointers/200900195">www.embedded.com</a></td>
</tr>
</table>
<table cellpadding="0" cellspacing="0" width="100%" style="text-align:left;background:transparent;border:none;margin:4px 0 8px;padding:0 8px;">
<tr>
<td valign="top"><!-- CLIPPED FROM: http://www.embedded.com/columns/programmingpointers/200900195 --><P><FONT size="3" face="Verdana" color="#003366"><B>Using size_t appropriately can improve the portability, efficiency, or readability of your code. Maybe even all three.</B></FONT></p>
<p> <P><br />
</P><P><br />
Numerous functions in the Standard C library accept arguments or return values that represent object sizes in bytes. For example, the lone argument in <FONT size="2" face="Courier"><B>malloc(n)</B></FONT> specifies the size of the object to be allocated, and the last argument in <FONT size="2" face="Courier"><B>memcpy(s1, s2, n)</B></FONT> specifies the size of the object to be copied. The return value of <FONT size="2" face="Courier"><B>strlen(s)</B></FONT> yields the length of (the number of characters in) null-terminated character array s excluding the null character, which isn&#8217;t exactly the size of <FONT size="2" face="Courier"><B>s</B></FONT>, but it&#8217;s in the ballpark.</p>
<p> <P><br />
</P><P><br />
You might reasonably expect these parameters and return types that represent sizes to be declared with type <FONT size="2" face="Courier"><B>int</B></FONT> (possibly <FONT size="2" face="Courier"><B>long</B></FONT> and/or <FONT size="2" face="Courier"><B>unsigned</B></FONT>), but they aren&#8217;t. Rather, the C standard declares them as type <FONT size="2" face="Courier"><B>size_t</B></FONT>. According to the standard, the declaration for <FONT size="2" face="Courier"><B>malloc</B></FONT> should appear in <FONT size="2" face="Courier"><B>&lt;stdlib.h&gt;</B></FONT> as something equivalent to:</p>
<p> <P><br />
</P><P><br />
</P><PRE><CODE><FONT size="2" face="Courier"><B>void *malloc(<SPAN>size_t</SPAN> n);</B></FONT></CODE></PRE></td>
</tr>
</table>
</td>
</tr>
</table>
<div style="margin:0 6px 6px 4px;">
<table style="font-size:11px;border-spacing:0;padding:0;" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td style="background:transparent;border-width:0;padding:0;">&nbsp;</td>
<td align="right" style="background:transparent;width:107px;border-width:0;padding:0;" width="107"><a href="http://clipmarks.com/share/7AF356FF-F474-45F5-AE3E-808FDE1D80F8/blog/" title="blog or email this clip"><img src="http://content9.clipmarks.com/images/c2b-foot.png" border="0" alt="blog it" width="107" height="17" style="border-width:0;margin:0;padding:0;" /></a></td>
</tr>
</table>
</div>
</td>
</tr>
</table>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/hircus.wordpress.com/223/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/hircus.wordpress.com/223/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/hircus.wordpress.com/223/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/hircus.wordpress.com/223/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/hircus.wordpress.com/223/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/hircus.wordpress.com/223/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/hircus.wordpress.com/223/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/hircus.wordpress.com/223/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/hircus.wordpress.com/223/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/hircus.wordpress.com/223/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/hircus.wordpress.com/223/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/hircus.wordpress.com/223/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=223&subd=hircus&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://hircus.wordpress.com/2008/06/08/c-types-101/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3ac78be5e356078c3d5a126840934538?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hircus</media:title>
		</media:content>

		<media:content url="http://content.clipmarks.com/blog_icon/fb0e31ea-247b-4396-b879-55723cb0754c/7AF356FF-F474-45F5-AE3E-808FDE1D80F8/" medium="image" />

		<media:content url="http://content9.clipmarks.com/images/c2b-foot.png" medium="image">
			<media:title type="html">blog it</media:title>
		</media:content>
	</item>
		<item>
		<title>Programming languages, in a nutshell</title>
		<link>http://hircus.wordpress.com/2007/12/04/programming-languages-in-a-nutshell/</link>
		<comments>http://hircus.wordpress.com/2007/12/04/programming-languages-in-a-nutshell/#comments</comments>
		<pubDate>Tue, 04 Dec 2007 08:21:22 +0000</pubDate>
		<dc:creator>Michel</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://hircus.wordpress.com/2007/12/04/programming-languages-in-a-nutshell/</guid>
		<description><![CDATA[Task: List the programming languages you consider important or interesting. Describe each of them in one sentence.


C
High-level assembler


Lua
Table-based, functional and embeddable


Python
Great RAD language crippled by dogma


Scala
Java done (almost) right, with Erlang- and Haskell-inspired features


Posting this from the new N810 using its built-in keyboard. Typing those tags was painful, and as a result, more languages will [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=216&subd=hircus&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Task: <span style='font-style:italic;'>List the programming languages you consider important or interesting. Describe each of them in one sentence.</span></p>
<table>
<tr>
<td>C</td>
<td>High-level assembler</td>
</tr>
<tr>
<td>Lua</td>
<td>Table-based, functional and embeddable</td>
</tr>
<tr>
<td>Python</td>
<td>Great RAD language crippled by dogma</td>
</tr>
<tr>
<td>Scala</td>
<td>Java done (almost) right, with Erlang- and Haskell-inspired features</td>
</tr>
</table>
<p>Posting this from the new N810 using its built-in keyboard. Typing those tags was painful, and as a result, more languages will be added to the table tomorrow &#8212; from the workstation.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/hircus.wordpress.com/216/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/hircus.wordpress.com/216/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/hircus.wordpress.com/216/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/hircus.wordpress.com/216/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/hircus.wordpress.com/216/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/hircus.wordpress.com/216/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/hircus.wordpress.com/216/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/hircus.wordpress.com/216/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/hircus.wordpress.com/216/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/hircus.wordpress.com/216/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/hircus.wordpress.com/216/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/hircus.wordpress.com/216/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=216&subd=hircus&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://hircus.wordpress.com/2007/12/04/programming-languages-in-a-nutshell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3ac78be5e356078c3d5a126840934538?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hircus</media:title>
		</media:content>
	</item>
		<item>
		<title>Wide Finder: OCaml and JoCaml</title>
		<link>http://hircus.wordpress.com/2007/10/17/wide-finder-ocaml-and-jocaml/</link>
		<comments>http://hircus.wordpress.com/2007/10/17/wide-finder-ocaml-and-jocaml/#comments</comments>
		<pubDate>Wed, 17 Oct 2007 17:40:48 +0000</pubDate>
		<dc:creator>Michel</dc:creator>
				<category><![CDATA[ML]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://hircus.wordpress.com/2007/10/17/wide-finder-ocaml-and-jocaml/</guid>
		<description><![CDATA[Spent last night getting a crash course in using OCaml to do non-functional things (hash tables, file I/O, regular expressions) and the result is now up.
The JoCaml version does the file-partitioning trick used in the C++ implementation, with each finder workers being run inside a JoCaml channel; the channels share a single lock so they [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=214&subd=hircus&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Spent last night getting a crash course in using OCaml to do non-functional things (hash tables, file I/O, regular expressions) and the result <a href="http://hircus.org/widefinder/widefinder-ml.tar.bz2">is now up</a>.</p>
<p>The JoCaml version does the file-partitioning trick used in the C++ implementation, with each finder workers being run inside a JoCaml channel; the channels share a single lock so they can update the hash table serially.</p>
<p>Interestingly, current implementation does not get a speed-up from the input file being cached (Ilmari&#8217;s <a href="http://fhtr.blogspot.com/2007/10/wide-finder-ocaml.html">wf.ml</a> does). Will have to peruse his to see what&#8217;s slowing things down.</p>
<p>Lesson: not all techniques for processing a file line-wise are equally good!</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/hircus.wordpress.com/214/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/hircus.wordpress.com/214/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/hircus.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/hircus.wordpress.com/214/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/hircus.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/hircus.wordpress.com/214/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/hircus.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/hircus.wordpress.com/214/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/hircus.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/hircus.wordpress.com/214/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/hircus.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/hircus.wordpress.com/214/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=214&subd=hircus&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://hircus.wordpress.com/2007/10/17/wide-finder-ocaml-and-jocaml/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3ac78be5e356078c3d5a126840934538?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hircus</media:title>
		</media:content>
	</item>
		<item>
		<title>Wide Finder: C++ update</title>
		<link>http://hircus.wordpress.com/2007/10/16/wide-finder-c-update/</link>
		<comments>http://hircus.wordpress.com/2007/10/16/wide-finder-c-update/#comments</comments>
		<pubDate>Tue, 16 Oct 2007 21:15:21 +0000</pubDate>
		<dc:creator>Michel</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://hircus.wordpress.com/2007/10/16/wide-finder-c-update/</guid>
		<description><![CDATA[Talked with a colleague about the slow single-threaded performance of my Wide Finder implementation, and we narrowed it down to two possibilities:

Boost regular expression is not compiled?
C++ strings have higher overhead than null-terminated c_str

First point can be ruled out: Boost compiles regular expressions when you assign them. Second point &#8212; well, reading in the file [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=213&subd=hircus&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Talked with a colleague about the slow single-threaded performance of my Wide Finder implementation, and we narrowed it down to two possibilities:</p>
<ul>
<li>Boost regular expression is not compiled?</li>
<li>C++ strings have higher overhead than null-terminated <tt>c_str</tt></li>
</ul>
<p>First point can be ruled out: Boost compiles regular expressions when you assign them. Second point &#8212; well, reading in the file using <tt>std::getline</tt> turns out to consume the bulk of time.</p>
<p>I&#8217;ve reorganized the code a bit, using a multimap rather than a vector to rank the URLs by count, with no effect on speed. With two and four threads on a dual-core Intel notebook, the performance is at least on par with Ruby.</p>
<p><a href="http://girtby.net/archives/2007/10/9/wide-finder-in-c">Alastair Rankine</a> has a C++ implementation that is slightly faster, but uses Boost memory-mapped IO that I avoided for the same reason he put as caveat: that it will not scale to files that are too large. Which Tim&#8217;s log file might well be. Again, that is not significantly faster than the Ruby code.</p>
<p>Moral of the question: Perl and Ruby can be faster than C++! The C implementations out there are blindingly fast, but the way they do regular expression handling are really painful.</p>
<p>Will turn my (limited) spare time to doing a clean JoCaml implementation &#8212; it might not be faster but it definitely will look cleaner!</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/hircus.wordpress.com/213/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/hircus.wordpress.com/213/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/hircus.wordpress.com/213/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/hircus.wordpress.com/213/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/hircus.wordpress.com/213/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/hircus.wordpress.com/213/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/hircus.wordpress.com/213/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/hircus.wordpress.com/213/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/hircus.wordpress.com/213/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/hircus.wordpress.com/213/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/hircus.wordpress.com/213/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/hircus.wordpress.com/213/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=213&subd=hircus&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://hircus.wordpress.com/2007/10/16/wide-finder-c-update/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3ac78be5e356078c3d5a126840934538?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hircus</media:title>
		</media:content>
	</item>
		<item>
		<title>JoCaml</title>
		<link>http://hircus.wordpress.com/2007/10/15/jocaml/</link>
		<comments>http://hircus.wordpress.com/2007/10/15/jocaml/#comments</comments>
		<pubDate>Mon, 15 Oct 2007 23:36:20 +0000</pubDate>
		<dc:creator>Michel</dc:creator>
				<category><![CDATA[ML]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://hircus.wordpress.com/2007/10/15/jocaml/</guid>
		<description><![CDATA[After turning in the C/C++ monster (cleanest C code I reckon it is possible to write, thus the total lack of memory-mapped I/O and other optimizations), I turned my attention to picking a better implementation language.
Requirements:

Functional
Good support for threading
If possible, support for distributed computing

As it turns out, JoCaml fits the bill perfectly. It&#8217;s an extension [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=212&subd=hircus&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>After turning in the C/C++ monster (cleanest C code I reckon it is possible to write, thus the total lack of memory-mapped I/O and other optimizations), I turned my attention to picking a better implementation language.</p>
<p>Requirements:</p>
<ul>
<li>Functional</li>
<li>Good support for threading</li>
<li>If possible, support for distributed computing</li>
</ul>
<p>As it turns out, JoCaml fits the bill perfectly. It&#8217;s an extension of Ocaml, so it combines a rich library with a familiar syntax (not to me, but having used both Scheme and Haskell, how different can it be) &#8212; and a very nice process calculus!</p>
<p>Example: this is a concurrent stack that blocks if there is no input available</p>
<blockquote><p>
let new_stack () =<br />
  def state (s) &amp; push (v) = state (v::s) &amp; reply to push<br />
    or state (x::s) &amp; pop () = state s &amp; reply x to pop in<br />
  spawn state([]);<br />
  pop, push<br />
;;
</p></blockquote>
<p>This defines a private <tt>state</tt> channel, and then export the <tt>pop</tt> and <tt>push</tt> synchronous channels (that to the user behave just like ordinary functions)</p>
<p>and this is how you use it:</p>
<blockquote><p>
let pop, push = new_stack ();;<br />
spawn echo(pop());;<br />
push(1);;
</p></blockquote>
<p>Note that the <tt>echo</tt> channel will block, since pop can&#8217;t return a value until the stack contains something! This value is then pushed into the stack and &#8216;1&#8242; printed.</p>
<p>More of this at the <a href="http://jocaml.infria.fr/">JoCaml site</a>. And, as it turns out, there already is <a href="http://fhtr.blogspot.com/2007/10/wide-finder-ocaml.html">a JoCaml implementation of the Wide Finder</a>, by Ilmari Heikkinen. Will have to grok the finer details from him.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/hircus.wordpress.com/212/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/hircus.wordpress.com/212/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/hircus.wordpress.com/212/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/hircus.wordpress.com/212/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/hircus.wordpress.com/212/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/hircus.wordpress.com/212/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/hircus.wordpress.com/212/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/hircus.wordpress.com/212/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/hircus.wordpress.com/212/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/hircus.wordpress.com/212/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/hircus.wordpress.com/212/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/hircus.wordpress.com/212/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=212&subd=hircus&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://hircus.wordpress.com/2007/10/15/jocaml/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3ac78be5e356078c3d5a126840934538?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hircus</media:title>
		</media:content>
	</item>
		<item>
		<title>Tim Bray&#8217;s Wide Finder: a minimalist implementation</title>
		<link>http://hircus.wordpress.com/2007/10/12/tim-brays-wide-finder-a-minimalist-implementation/</link>
		<comments>http://hircus.wordpress.com/2007/10/12/tim-brays-wide-finder-a-minimalist-implementation/#comments</comments>
		<pubDate>Fri, 12 Oct 2007 23:45:21 +0000</pubDate>
		<dc:creator>Michel</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[boost]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[pthreads]]></category>
		<category><![CDATA[tim-bray]]></category>
		<category><![CDATA[wide-finder]]></category>

		<guid isPermaLink="false">http://hircus.wordpress.com/2007/10/12/tim-brays-wide-finder-a-minimalist-implementation/</guid>
		<description><![CDATA[Several weeks ago, Tim Bray posted his Wide Finder project: take the Ruby script that parses an Apache log file and report the top 10 hits, and parallelize it in your language of choice.
It occurred to me a while back that this is a perfect job for a C multi-process program, taking advantage of Linux&#8217;s [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=211&subd=hircus&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Several weeks ago, Tim Bray posted his Wide Finder project: take the Ruby script that parses an Apache log file and report the top 10 hits, and parallelize it in your language of choice.</p>
<p>It occurred to me a while back that this is a perfect job for a C multi-process program, taking advantage of Linux&#8217;s cheap copy-on-write <tt>fork</tt>, if not for the need to merge the result. So it would probably be easier to write it using the <tt>pthreads</tt> library instead. One would want to reduce inter-thread communication as much as possible, though.</p>
<p>I did not have time to touch the code until today, but now it&#8217;s done. Two key insights:</p>
<ul>
<li>The input file can be partitioned cleanly into multiple chunks of roughly identical sizes without communication, as long as each thread follows the same protocol. i.e. a common chunk size is used, and all but the first thread needs to check if the character before their starting offset is the end-of-line. If not, they need to skip until the end of line, yielding that line to the previous thread.</li>
<li>When merging the hash maps, the first thread to acquire the lock does not need to insert its items one by one, but can just set the main hash map to point to its own</li>
</ul>
<p>On a data set size of 200MB, with a single thread, performs slightly slower as Ruby by wall clock:</p>
<p>Ruby timing<br />
<code><br />
2.99user 1.88system 0:05.26elapsed 92%CPU (0avgtext+0avgdata 0maxresident)k<br />
152inputs+0outputs (3major+853minor)pagefaults 0swaps<br />
3.04user 1.84system 0:05.33elapsed 91%CPU (0avgtext+0avgdata 0maxresident)k<br />
0inputs+0outputs (0major+856minor)pagefaults 0swaps<br />
2.84user 2.01system 0:05.19elapsed 93%CPU (0avgtext+0avgdata 0maxresident)k<br />
0inputs+0outputs (0major+856minor)pagefaults 0swaps<br />
</code></p>
<p>C++ timing, one thread<br />
<code><br />
2.70user 5.21system 0:07.95elapsed 99%CPU (0avgtext+0avgdata 0maxresident)k<br />
0inputs+0outputs (0major+655minor)pagefaults 0swaps<br />
2.78user 5.14system 0:07.96elapsed 99%CPU (0avgtext+0avgdata 0maxresident)k<br />
0inputs+0outputs (0major+655minor)pagefaults 0swaps<br />
2.72user 5.12system 0:07.90elapsed 99%CPU (0avgtext+0avgdata 0maxresident)k<br />
0inputs+0outputs (0major+654minor)pagefaults 0swaps<br />
</code></p>
<p>With two threads, though, it runs faster, almost at Ruby speed.</p>
<p>C++ timing, two threads<br />
<code><br />
3.33user 5.40system 0:05.51elapsed 158%CPU (0avgtext+0avgdata 0maxresident)k<br />
0inputs+0outputs (0major+675minor)pagefaults 0swaps<br />
3.40user 5.33system 0:05.86elapsed 148%CPU (0avgtext+0avgdata 0maxresident)k<br />
0inputs+0outputs (0major+672minor)pagefaults 0swaps<br />
3.41user 5.36system 0:05.60elapsed 156%CPU (0avgtext+0avgdata 0maxresident)k<br />
0inputs+0outputs (0major+674minor)pagefaults 0swaps<br />
</code></p>
<p>So, as Tim observed, the problem is not entirely IO-bound. More testing is needed, but will probably need to be done on a machine with faster IO (and more CPUs). Like Tim&#8217;s new Niagara T2 testbed.</p>
<p>Will edit this post later &#8212; need to run now, and wanted to get this out as soon as possible. Code is available  <a href="http://hircus.org/widefinder/">here</a>&#8230; now I just need to find Tim&#8217;s email, since his blog is down. Argh!</p>
<p><b>Update:</b> switched to the new <tt>std::tr1::unsorted_map</tt>. Performance seems identical with a single thread, but slightly higher (within margin of error) on two threads &#8212; which makes sense: a multithreaded run exercises the map more, because each thread&#8217;s map has to be merged into the final map.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/hircus.wordpress.com/211/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/hircus.wordpress.com/211/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/hircus.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/hircus.wordpress.com/211/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/hircus.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/hircus.wordpress.com/211/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/hircus.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/hircus.wordpress.com/211/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/hircus.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/hircus.wordpress.com/211/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/hircus.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/hircus.wordpress.com/211/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=211&subd=hircus&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://hircus.wordpress.com/2007/10/12/tim-brays-wide-finder-a-minimalist-implementation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3ac78be5e356078c3d5a126840934538?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hircus</media:title>
		</media:content>
	</item>
		<item>
		<title>Stream module for Lua</title>
		<link>http://hircus.wordpress.com/2006/11/18/stream-module-for-lua/</link>
		<comments>http://hircus.wordpress.com/2006/11/18/stream-module-for-lua/#comments</comments>
		<pubDate>Sat, 18 Nov 2006 18:06:41 +0000</pubDate>
		<dc:creator>Michel</dc:creator>
				<category><![CDATA[Lua]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://hircus.wordpress.com/2006/11/18/stream-module-for-lua/</guid>
		<description><![CDATA[&#8220;Be not afraid of greatness: some are born great, some achieve greatness and some have greatness thrust upon them&#8221;.
- (Shakespeare, W., Twelfth Night, Quote Act II, Scene V).
&#8220;Some men are born mediocre, some men achieve mediocrity, and some men have mediocrity thrust upon them. With Major Major it had been all three&#8221;.
- (Heller, J., Catch-22, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=192&subd=hircus&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><blockquote><p><em>&#8220;Be not afraid of greatness: some are born great, some achieve greatness and some have greatness thrust upon them&#8221;.</em><br />
- (<strong>Shakespeare, W.</strong>, <em>Twelfth Night</em>, Quote Act II, Scene V).</p></blockquote>
<blockquote><p><em>&#8220;Some men are born mediocre, some men achieve mediocrity, and some men have mediocrity thrust upon them. With Major Major it had been all three&#8221;.</em><br />
- (<strong>Heller, J.</strong>, <em>Catch-22</em>, Quote Page 94 (paperback)).</p></blockquote>
<blockquote><p><em>&#8220;Some languages are born lazy, some languages achieve laziness, and some languages have laziness thrust upon them&#8221;.</em><br />
- (<strong>Salim, M.</strong>, unpublished)</p>
<p>So you can probably guess what this is about. A stream library for Lua, that allows you to do things like running the Sieve of Eratosthenes without specifying an upper bound, lazily filtering the set of <em>all</em> positive integers as you go along.</p>
<p><span id="more-192"></span><br />
Witness the use of the library:</p>
<blockquote>
<pre>
require("stream")

function ints(n)
   return {
      head = function () return n end,
      tail = function () return ints(n+1) end }
end

function primes (sints)
   local curprime = false
   local tailthunk = false
   local fhead =
      function ()
	 if curprime then return curprime
	 else
	    curprime = sints.head()
	    tailthunk = sints.tail
	    return curprime
	 end
      end
   local ftail =
      function ()
	 if (not tailthunk) then
	    fhead()
	 end
	 return primes(
		 stream.filter(function(n) return not (n%curprime == 0) end,
			       tailthunk() ))
      end
   return {head=fhead, tail=ftail}
end

-- print the first 100 primes
stream.print(100, primes(ints(2)))</pre>
</blockquote>
<p>A stream is a table containing a head and a tail; both are thunks, so you call them with no arguments to obtain the value. Note the caching that avoids recomputation, similar caching is done in the functions provided by the stream library (like stream.filter)</p>
<p>Code available <a href="http://hircus.org/lua/stream.lua">on hircus.org</a>.</p>
<p>And some benchmarks:</p>
<table border="1">
<tr align="center">
<th></th>
<th width="35%">Lua</th>
<th width="35%">Haskell</th>
</tr>
</table>
<p>The Haskell code was run using GHC&#8217;s &#8216;runhaskell&#8217;, which does not precompile the code, to be fair to Lua which is interpreted. Not bad when put up against a language that is lazy by design (and thus optimized for it)! Timing taken using the &#8216;time&#8217; utility on an Intel Core 2 Duo 1.66 GHz laptop which was mostly idle.</p></blockquote>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/hircus.wordpress.com/192/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/hircus.wordpress.com/192/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/hircus.wordpress.com/192/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/hircus.wordpress.com/192/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/hircus.wordpress.com/192/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/hircus.wordpress.com/192/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/hircus.wordpress.com/192/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/hircus.wordpress.com/192/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/hircus.wordpress.com/192/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/hircus.wordpress.com/192/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/hircus.wordpress.com/192/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/hircus.wordpress.com/192/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=192&subd=hircus&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://hircus.wordpress.com/2006/11/18/stream-module-for-lua/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3ac78be5e356078c3d5a126840934538?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hircus</media:title>
		</media:content>
	</item>
		<item>
		<title>Effectively functional: swapping tree leaves with streams</title>
		<link>http://hircus.wordpress.com/2006/04/18/effectively-functional-swapping-tree-leaves-with-streams/</link>
		<comments>http://hircus.wordpress.com/2006/04/18/effectively-functional-swapping-tree-leaves-with-streams/#comments</comments>
		<pubDate>Tue, 18 Apr 2006 20:40:00 +0000</pubDate>
		<dc:creator>Michel</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scheme]]></category>

		<guid isPermaLink="false">http://hircus.wordpress.com/2006/04/18/effectively-functional-swapping-tree-leaves-with-streams/</guid>
		<description><![CDATA[(Thanks to Roshan for posing the task)
Given two binary trees, and a depth-first-search traversal ordering of the leaf nodes, produce two new trees with the same structures as the originals, but with the leaf nodes swapped. If there are more leaf nodes in one tree than the other, the remaining leaf nodes are copied unchanged.
So [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=189&subd=hircus&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>(Thanks to <a href="http://www.thinkingms.com/pensieve">Roshan</a> for posing the task)</p>
<p>Given two binary trees, and a depth-first-search traversal ordering of the leaf nodes, produce two new trees with the same structures as the originals, but with the leaf nodes swapped. If there are more leaf nodes in one tree than the other, the remaining leaf nodes are copied unchanged.</p>
<p>So (swap-leaves &#8216;(1 . (2 . 3)) &#8216;(a . ((b . c) . d)))<br />
=&gt; &#8216;((a . (b . c)) &#8216;(1 . ((2 . 3) . d))</p>
<p><span id="more-189"></span><br />
Waiving the requirement that the program be completely functional, my original thought was <em>I could probably just modify my tree walker to do this</em>. This <tt>treewalk</tt> procedure takes a tree and produces a stream of leaf node values. Changing it so that it returns a pair of the value, and a setter procedure that can modify the value in place, is trivial: the recursive auxiliary procedure for treewalk is given the parent node, so it can use <tt>set-car!</tt> or <tt>set-cdr!</tt> (depending on which side of the parent node this leaf node is) to update the parent:</p>
<blockquote>
<pre>
(define treewalk
  (lambda (t)
    (if (not (pair? t))
        (stream-cons
          `(t . (lambda (v)
                  (error "Cannot modify singleton")))
          empty-stream)
        (stream-append (treewalk^ (car t) #t t)
                       (treewalk^ (cdr t) #f t)))))

(define treewalk^
  (lambda (t left? parent)
    (if (not (pair? t))
        (stream-cons `(,t . ,(if left?
                               (lambda (v)
                                 (set-car! parent v))
                               (lambda (v)
                                 (set-cdr! parent v))))
                     empty-stream)
        (stream-append (treewalk^ (car t) #t t)
                       (treewalk^ (cdr t) #f t)))))</pre>
</blockquote>
<p>The swapping is then just a matter of calling the setter for node1 with the value of node2, and vice-versa; this generalizes to swapping between multiple trees as well.</p>
<blockquote>
<pre>(define treewalk
  (lambda (t)
    (if (not (pair? t))
        (stream-cons `(t . (lambda (v)
                             (error "Cannot modify singleton")))
                     empty-stream)
        (stream-append (treewalk^ (car t) #t t)
                       (treewalk^ (cdr t) #f t)))))

(define treewalk^
  (lambda (t left? parent)
    (if (not (pair? t))
        (stream-cons `(,t . ,(if left?
                               (lambda (v)
                                 (set-car! parent v))
                               (lambda (v)
                                 (set-cdr! parent v))))
                     empty-stream)
        (stream-append (treewalk^ (car t) #t t)
                       (treewalk^ (cdr t) #f t)))))</pre>
</blockquote>
<p>But this modifies existing trees in place, a big no-no in functional programming land (though imperative programmers live with this every day). Is there a way to do this in, if not a fully functional way, then in a way that the side-effects are confined? (I mentioned to Roshan that this is a dual of Haskell&#8217;s monads: it <em>behaves functionally</em> even though it is imperative; monads gave the illusion of imperative behaviour even though they are fully functional. He pointed out that internally the state monad is optimized into imperative calls as well, so the comparison is even more interesting)</p>
<p>Instead of modifying existing trees in place, we want to copy the trees. But copying, naively done, would involve traversing the trees again, and that would be inefficient. The copying should be done <em>Lazily</em> as needed, and with streams, that means doing it while you&#8217;re walking the tree and collecting the nodes!</p>
<p>In short, at every internal node,<br />
- create a new node, which has as its children the children of the original node;<br />
- update the <em>clone</em> of the parent, instead of the parent, so that its car or cdr points to this new node<br />
- the setter procedures for each leaf nodes modify these cloned nodes instead of the originals</p>
<blockquote>
<pre>...
(let ([copy (cons (car t) (cdr t))])
  (if left?
    (set-car! copy-parent copy)
    (set-cdr! copy-parent copy))
  (stream-append (treewalk-copy^ (car t) #t copy)
                 (treewalk-copy^ (cdr t) #f copy))
...</pre>
</blockquote>
<p>The main <tt>treewalk</tt> procedure now has to return the cloned tree as well, since at each subsequent stage of the walk only the direct parent is known, not the entire tree.</p>
<blockquote>
<pre>...
(let ([copy (cons (car t) (cdr t))])
       (if left?
           (set-car! copy-parent copy)
           (set-cdr! copy-parent copy))
  (stream-append (treewalk-copy^ (car t) #t copy)
                 (treewalk-copy^ (cdr t) #f copy))
...</pre>
</blockquote>
<p>Code, including stream implementation, available <a href="http://www.cs.indiana.edu/~msalim/scheme/swap-leaves.scm">here</a></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/hircus.wordpress.com/189/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/hircus.wordpress.com/189/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/hircus.wordpress.com/189/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/hircus.wordpress.com/189/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/hircus.wordpress.com/189/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/hircus.wordpress.com/189/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/hircus.wordpress.com/189/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/hircus.wordpress.com/189/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/hircus.wordpress.com/189/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/hircus.wordpress.com/189/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/hircus.wordpress.com/189/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/hircus.wordpress.com/189/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=189&subd=hircus&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://hircus.wordpress.com/2006/04/18/effectively-functional-swapping-tree-leaves-with-streams/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3ac78be5e356078c3d5a126840934538?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hircus</media:title>
		</media:content>
	</item>
		<item>
		<title>LiteratePrograms</title>
		<link>http://hircus.wordpress.com/2006/03/04/literateprograms/</link>
		<comments>http://hircus.wordpress.com/2006/03/04/literateprograms/#comments</comments>
		<pubDate>Sat, 04 Mar 2006 11:55:00 +0000</pubDate>
		<dc:creator>Michel</dc:creator>
				<category><![CDATA[Pedagogy]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://hircus.wordpress.com/2006/03/04/literateprograms/</guid>
		<description><![CDATA[This being mid-term week for course I am a TA for, the lab sections I taught yesterday were made optional, and so apart from handling several questions on exam questions and assignments, I had some free time.
That time was spent reading Lambda the Ultimate, which was how I noticed the LiteratePrograms wiki. Based on Wikipedia&#8217;s [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=188&subd=hircus&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>This being mid-term week for course I am a TA for, the lab sections I taught yesterday were made optional, and so apart from handling several questions on exam questions and assignments, I had some free time.</p>
<p>That time was spent reading <a href="http://lambda-the-ultimate.org/">Lambda the Ultimate</a>, which was how I <a href="http://lambda-the-ultimate.org/node/1336">noticed</a> the <a href="http://literateprograms.org/LiteratePrograms:Welcome">LiteratePrograms</a> wiki. Based on Wikipedia&#8217;s MediaWiki, with added feature from the noweb literate programming system, it is to collaborative programming what other wikis are to collaborative writing. You can declare code blocks, and if the language is supported get syntax highlighting for free; the code is automatically packaged into a zip archive everytime someone hits the download link.</p>
<p>Check it out. But if you&#8217;re teaching a programming course, don&#8217;t mention it to your students <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/hircus.wordpress.com/188/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/hircus.wordpress.com/188/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/hircus.wordpress.com/188/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/hircus.wordpress.com/188/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/hircus.wordpress.com/188/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/hircus.wordpress.com/188/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/hircus.wordpress.com/188/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/hircus.wordpress.com/188/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/hircus.wordpress.com/188/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/hircus.wordpress.com/188/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/hircus.wordpress.com/188/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/hircus.wordpress.com/188/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=188&subd=hircus&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://hircus.wordpress.com/2006/03/04/literateprograms/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3ac78be5e356078c3d5a126840934538?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hircus</media:title>
		</media:content>
	</item>
		<item>
		<title>Python tail-call decorator</title>
		<link>http://hircus.wordpress.com/2006/03/02/python-tail-call-decorator/</link>
		<comments>http://hircus.wordpress.com/2006/03/02/python-tail-call-decorator/#comments</comments>
		<pubDate>Thu, 02 Mar 2006 22:36:00 +0000</pubDate>
		<dc:creator>Michel</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://hircus.wordpress.com/2006/03/02/python-tail-call-decorator/</guid>
		<description><![CDATA[Two days ago, this tail call decorator
 by Crutcher Dunnavant was mentioned on LtU. It&#8217;s a clever little trick: the decorator returns a new function that, when called, checks if its grandparent
is itself. This would mean that a function declared to be tail-call-optimized is called from another tail-called-optimized function, and so the caller&#8217;s frame in [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=187&subd=hircus&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Two days ago, this <a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/474088">tail call decorator<br />
</a> by Crutcher Dunnavant was <a href="http://lambda-the-ultimate.org/node/1331">mentioned</a> on LtU. It&#8217;s a clever little trick: the decorator returns a new function that, when called, checks if its <em>grandparent</em><br />
is itself. This would mean that a function declared to be tail-call-optimized is called from another tail-called-optimized function, and so the caller&#8217;s frame in the stack can be wiped. How does it do this? The function <em>throws</em> an exception. This is caught by the grandparent, which then calls the saved function with the arguments passed in the exception.</p>
<p>This is the <a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/474088">original </a>implementation:</p>
<blockquote>
<pre>

#!/usr/bin/env python2.4
# This program shows off a python decorator(
# which implements tail call optimization. It
# does this by throwing an exception if it is
# it's own grandparent, and catching such
# exceptions to recall the stack.

import sys

class TailRecurseException:
  def __init__(self, args, kwargs):
    self.args = args
    self.kwargs = kwargs

def tail_call_optimized(g):
  """
  This function decorates a function with tail call
  optimization. It does this by throwing an exception
  if it is it's own grandparent, and catching such
  exceptions to fake the tail call optimization.

  This function fails if the decorated
  function recurses in a non-tail context.
  """
  def func(*args, **kwargs):
    f = sys._getframe()
    if f.f_back and f.f_back.f_back         and f.f_back.f_back.f_code == f.f_code:
      raise TailRecurseException(args, kwargs)
    else:
      while 1:
        try:
          return g(*args, **kwargs)
        except TailRecurseException, e:
          args = e.args
          kwargs = e.kwargs
  func.__doc__ = g.__doc__
  return func

@tail_call_optimized
def factorial(n, acc=1):
  "calculate a factorial"
  if n == 0:
    return acc
  return factorial(n-1, n*acc)

print factorial(10000)
# prints a big, big number,
# but doesn't hit the recursion limit.

@tail_call_optimized
def fib(i, current = 0, next = 1):
  if i == 0:
    return current
  else:
    return fib(i - 1, next, current + next)

print fib(10000)
# also prints a big number,
# but doesn't hit the recursion limit.</pre>
</blockquote>
<p>There is a subtle bug: after handling the exception, <em>func</em> then calls its own saved version of  <em>g</em>, not the <em>g</em> of the <em>func</em> that throws the exception. Modifying <em>func</em> and the <strong>TailRecurseException</strong> class so that the function to be called can be passed in the exception solves this nicely:</p>
<blockquote>
<pre>
class TailRecurseException:
  def __init__(self, g, args, kwargs):
    self.g = g
    self.args = args
    self.kwargs = kwargs

  ...
  def func(*args, **kwargs):
    f = sys._getframe()
    if f.f_back and f.f_back.f_back         and f.f_back.f_back.f_code == f.f_code:
      raise TailRecurseException(g, args, kwargs)
    else:
      newg = g
      while 1:
        try:
          return newg(*args, **kwargs)
        except TailRecurseException, e:
          newg = e.g
          args = e.args
          kwargs = e.kwargs
  ...</pre>
</blockquote>
<p>Throwing an exception just to handle the required stack manipulation because the interpreter won&#8217;t handle it is grossly inefficient, granted, and requiring the programmer to declare functions as optimizable is not be ideal, but still a really neat trick.</p>
<p>As a side note, if a function can be called in tail and non-tail position, a non-decorated version should be made available, otherwise bad things can happen:</p>
<blockquote>
<pre>
@tail_call_optimized
def add1(n):
  return (n+1)

@tail_call_optimized
def add2(n):
  return add1(add1(n))</pre>
</blockquote>
<p>This returns 41.</p>
<p>Modified version, and test cases, available <a href="http://www.cs.indiana.edu/%7Emsalim/python/tailcall2.py">here</a> [IUCS] (backup copy <a href="http://hircus.org/python/tailcall2_py.txt">here</a>).</p>
<p><span style="font-weight:bold;">Update</span>: added <a href="http://lambda-the-ultimate.org/node/1331#comment-15183">isTailCall</a> by jorend. Now it is safe to optimize every function (though for efficiency reason you&#8217;d only optimize the ones that make tail calls).</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/hircus.wordpress.com/187/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/hircus.wordpress.com/187/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/hircus.wordpress.com/187/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/hircus.wordpress.com/187/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/hircus.wordpress.com/187/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/hircus.wordpress.com/187/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/hircus.wordpress.com/187/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/hircus.wordpress.com/187/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/hircus.wordpress.com/187/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/hircus.wordpress.com/187/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/hircus.wordpress.com/187/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/hircus.wordpress.com/187/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=187&subd=hircus&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://hircus.wordpress.com/2006/03/02/python-tail-call-decorator/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3ac78be5e356078c3d5a126840934538?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hircus</media:title>
		</media:content>
	</item>
		<item>
		<title>Whorfianism in PL design</title>
		<link>http://hircus.wordpress.com/2006/02/12/whorfianism-in-pl-design/</link>
		<comments>http://hircus.wordpress.com/2006/02/12/whorfianism-in-pl-design/#comments</comments>
		<pubDate>Sun, 12 Feb 2006 19:34:00 +0000</pubDate>
		<dc:creator>Michel</dc:creator>
				<category><![CDATA[Linguistics]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://hircus.wordpress.com/2006/02/12/whorfianism-in-pl-design/</guid>
		<description><![CDATA[While the aptness of the Whorfian hypothesis is still a highly-contentious issue when it comes to natural languages, the hypothesis more or less confirms itself again and again in the realms of programming languages. Certain programming styles are simply more suited to certain languages: you don&#8217;t try to use higher-order functions in C, after all; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=186&subd=hircus&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>While the aptness of the Whorfian hypothesis is still a highly-contentious issue when it comes to natural languages, the hypothesis more or less confirms itself again and again in the realms of programming languages. Certain programming styles are simply more suited to certain languages: you don&#8217;t try to use higher-order functions in C, after all; the lack of nested functions make this idea unworkable.</p>
<p>Which makes languages like <a href="http://scala.epfl.ch/">Scala</a> exciting. It&#8217;s fully functional, and also fully object-oriented.Integers are objects. Functions? Also objects. The List ADT familiar to functional programmer is now an Object, with methods such as fold and filter defined on it (which makes the syntax a bit confusing, but not if you come from Ruby. Or your first language is japanese. But I digress)</p>
<p>Best of both worlds? Well, not quite. The new Scala compiler looks promising, but it still trips up on this simple mutual-recursion example:</p>
<blockquote>
<pre>
object RecEvenOdd {
  def rec_oddp(n: Int): Boolean =
    { if (n == 0) false else rec_evenp(n-1) }
  def rec_evenp(n: Int): Boolean =
    { if (n == 0 ) true else rec_oddp(n-1) }
  def rec_oddevenp(flag: Boolean, n: Int): Boolean = {
    if (n == 0) flag else rec_oddevenp(!flag, n-1)
  }

  def main(args: Array[String]): unit = {
    val n: Int = Integer.parseInt(args(0));
    System.out.println("recursive: " + n + ": " +       rec_oddevenp(true, n));
    System.out.println("mutual rec: " + n + ": " +       rec_evenp(n));
  }
}</pre>
</blockquote>
<p>The former gets detected as a tail call and optimized. The second? No such luck.. yet.</p>
<p>Of further note is the implementation of streams: Scala implements the &#8220;odd&#8221; style of lazy programming (Wadler et al., <a href="http://cm.bell-labs.com/who/wadler/papers/lazyinstrict/lazyinstrict.ps.gz"><em>How to add laziness to a strict language without even being odd</em></a>); the provided implementation is very similar to the reference implementation of streams for Scheme in <a href="http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-24.html">SICP</a>. The following program will thus unnecessarily compute the square root of -1:</p>
<blockquote>
<pre>
object WadlerTest {
  def countdown(n: Int): Stream[Int] =
    Stream.cons(n, countdown(n-1));
  def sqrt(n: Double): Double = {
    System.out.println("sqrt: called with " + n);
    Math.sqrt(n);
  }
  def main(args: Array[String]): unit = {
    countdown(4) map { n =&gt; sqrt(n) } take(5) print
  }
}</pre>
</blockquote>
<p>The syntax can also be bizarre at times, a motley of Java-isms and proper FP style. For example,</p>
<pre>{ n =&gt; sqrt(n) }</pre>
<p>is η-reducible to sqrt, but Scala would not compile it with this definition of sqrt. Rewriting  sqrt thus:</p>
<blockquote>
<pre>
  def sqrt = { n: Double =&gt;
  ...</pre>
</blockquote>
<p>now sqrt can be passed to map &#8211; but the automatic cast from Int to Double is no longer performed.  Eek.</p>
<p>Moral of the story? I&#8217;m learning Haskell.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/hircus.wordpress.com/186/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/hircus.wordpress.com/186/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/hircus.wordpress.com/186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/hircus.wordpress.com/186/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/hircus.wordpress.com/186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/hircus.wordpress.com/186/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/hircus.wordpress.com/186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/hircus.wordpress.com/186/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/hircus.wordpress.com/186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/hircus.wordpress.com/186/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/hircus.wordpress.com/186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/hircus.wordpress.com/186/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=186&subd=hircus&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://hircus.wordpress.com/2006/02/12/whorfianism-in-pl-design/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3ac78be5e356078c3d5a126840934538?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hircus</media:title>
		</media:content>
	</item>
		<item>
		<title>Static (lexical) vs dynamic scoping</title>
		<link>http://hircus.wordpress.com/2005/06/02/static-lexical-vs-dynamic-scoping/</link>
		<comments>http://hircus.wordpress.com/2005/06/02/static-lexical-vs-dynamic-scoping/#comments</comments>
		<pubDate>Thu, 02 Jun 2005 22:20:00 +0000</pubDate>
		<dc:creator>Michel</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Scheme]]></category>

		<guid isPermaLink="false">http://hircus.wordpress.com/2005/06/02/static-lexical-vs-dynamic-scoping/</guid>
		<description><![CDATA[Eric and I were discussing scoping in Scheme and Python earlier today, our third over the past few weeks &#8211; and we finally nailed it shut. The first time he brought up dynamic scoping in Common Lisp and how Prof. Friedman dislikes it; the second was on how Python appears to have dynamic scoping (which [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=180&subd=hircus&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Eric and I were discussing scoping in Scheme and Python earlier today, our third over the past few weeks &#8211; and we finally nailed it shut. The first time he brought up dynamic scoping in Common Lisp and how Prof. Friedman dislikes it; the second was on how Python appears to have dynamic scoping (which turns out to be true, pre-Python 2.2), and now, thanks to Wikipedia, I think we have it right.</p>
<p>Provided Eric gets the H211 Introduction to Programming (Honors) class, which is in Python, and I get the C211 Introduction to Programming (Scheme), our discussion should stand us in good steed, though funnily today I played the Python guy and he played the Scheme one.</p>
<p>I&#8217;m going to show the examples, both in Scheme and Python; the first one in each section would appear to show that the language in question features dynamic scoping, which is incorrect as both actually do lexical scoping.</p>
<p>Scheme:<br />
Bad:</p>
<pre>
(let ((pi 3.1415))
 (define area
  (lambda (r)
   (* pi r r)))
 (display (area 10))
 (newline)
 (set! pi 3)
 (display (area 10)))</pre>
<p>Good:</p>
<pre>
(let ((pi 3.1415))
 (define area
  (lambda (r)
   (* pi r r)))
 (display (area 10))
 (newline)
 (let ((pi 3))
  (display (area 10))))</pre>
<p>Python:<br />
Bad:</p>
<pre>
pi = 3.1415
def area(r):
 return pi*r*r
area(10)
pi = 3
area(10)</pre>
<p>Good:</p>
<pre>
pi = 3.1415
pi_holder = 10
def create_area():
 pi_holder = pi  # local pi_holder, different from pi_holder outside
 def area(r):
  return pi_holder*r*r
 return area
area = create_area()
area(10)    # 314.15
pi_holder    # Still 10
pi_holder = 3
area(10)    # Still 314.15</pre>
<p>The above works, but is a bit problematic. I introduced <tt>pi_holder=10</tt> to show that, (1), pi_holder inside of <tt>create_area()</tt> is a local variable; (2), that this local pi_holder is the one that is in <tt>area</tt>&#8217;s scope, and thus changing the value of pi_holder does not affect it.</p>
<p>Isn&#8217;t it easier to just do pi = pi ? Well, that does not work. My initial hunch was that Python reads the LHS of the expression, decided pi has been redeclared as a local variable, and thus since it&#8217;s not been initialized it got confused trying to assign it the value of itself. But it&#8217;s actually worse; this code does not work either:</p>
<pre>
x = 42
def local_var_test():
 temp = x
 print temp  # 42
 x = temp
 print x   # 42?</pre>
<p>Surprise! Python won&#8217;t let you do that either. Take out the last two lines and the code works, though. Basically, if in the block a variable is declared <em>anywhere</em>, it is a local variable everywhere in that block, and trying to refer to a variable declared in the surrounding scope, even before the local declaration, will fail.</p>
<p>But this is where default parameters come in handy. A better way to rewrite the clunky code above is as follows:</p>
<pre>
pi = 3.1415
def create_area(pi = pi):
 def area(r):
  return pi*r*r
 return area
area = create_area()
area(10)    #314.15
pi = 3
area(10)    #314.15</pre>
<p>So Python has static scoping after all. The thing to bear in mind is that Scheme functions are named closures, while Python functions inherit the surrounding scope, so to freeze the variables you depend on you have to wrap your function definition inside another function that copies in the values you need into its local variables.</p>
<p>References:</p>
<ul>
<li><a href="http://en.wikipedia.org/wiki/Python_programming_language#Closures">Python programming language (Wikipedia): Closures</a></li>
<li><a href="http://en.wikipedia.org/wiki/Scope_%28programming%29">Scope (Wikipedia)</a></li>
</ul>
<p>And the funny thing is, I started the day trying to find good dynamic languages that run on the Java platform (platform envy, I guess, since .NET more prominently touts its language neutrality). Sun&#8217;s finally catching up, though &#8211; Tim Bray <a href="http://www.tbray.org/ongoing/When/200x/2005/03/15/Coyote">wrote</a> a few months back about the <a href="https://coyote.dev.java.net/">Coyote</a> project to support dynamic languages in Sun&#8217;s open source IDE, NetBeans, and pointed to an interesting Sun-developed scripting language, <a href="https://pnuts.dev.java.net/">Pnuts</a>. Which reminded me of Groovy and Boo.</p>
<p>Googling for <em>groovy boo .net</em> &#8211; Groovy being a Ruby-like scripting language for Java that received a lot of attention a few months ago, and then taken some flak over its development model, and Boo being the Python-like language for .NET &#8211; yields this <a href="http://developers.slashdot.org/article.pl?sid=05/03/30/1932209&amp;tid=156&amp;tid=8">very interesting Slashdot discussion</a> that led me to such intriguing functional OO languages as <a href="http://scala.epfl.ch/">Scala</a> and <a href="http://nice.sourceforge.net">Nice</a>. .NET fans do not get to have all the fun!</p>
<p>Groovy, on the other hand, seems <a href="http://www.pyrasun.com/mike/mt/archives/2005/01/09/20.57.06/">rather</a> <a href="http://www.manageability.org/blog/stuff/what-is-wrong-with-groovy/view">disappointing</a>. Oh well. Scala looks more like Haskell, but with dynamic type inference (like Boo).. yay!</p>
<p><strong>Update</strong> &#8211; <em>2005/06/06</em></p>
<p>Realized a few days ago, but haven&#8217;t gotten round to posting about it, that I was unfairly comparing Scheme and Python, and that Python methods <em>are</em> closures in themselves. Note:</p>
<pre>
pi = 3.1415
def area(r):
 return pi*r*r
print area(10)  # 314.15
def test():
 pi = 3
 print area(10) # 314.15
test()</pre>
<p>In the earlier example, overriding the value of pi with <tt>pi = 3</tt> is the equivalent of doing <tt>(set! pi 3)</tt> in Scheme, i.e. it will change the value of the variable that both the top-level pi, which is the one that area knows. In a dynamic scope, which uses a stack to figure out which value assignment should apply, <tt>pi = 3</tt> would affect the call to <tt>area</tt> just after it.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/hircus.wordpress.com/180/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/hircus.wordpress.com/180/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/hircus.wordpress.com/180/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/hircus.wordpress.com/180/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/hircus.wordpress.com/180/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/hircus.wordpress.com/180/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/hircus.wordpress.com/180/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/hircus.wordpress.com/180/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/hircus.wordpress.com/180/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/hircus.wordpress.com/180/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/hircus.wordpress.com/180/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/hircus.wordpress.com/180/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=hircus.wordpress.com&blog=561873&post=180&subd=hircus&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://hircus.wordpress.com/2005/06/02/static-lexical-vs-dynamic-scoping/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3ac78be5e356078c3d5a126840934538?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hircus</media:title>
		</media:content>
	</item>
	</channel>
</rss>