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

<channel>
	<title>Reenigne blog &#187; language</title>
	<atom:link href="http://www.reenigne.org/blog/category/computer/language/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.reenigne.org/blog</link>
	<description>Stuff I think about</description>
	<lastBuildDate>Mon, 17 Oct 2011 23:00:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Strings and localization in Unity</title>
		<link>http://www.reenigne.org/blog/strings-and-localization-in-unity/</link>
		<comments>http://www.reenigne.org/blog/strings-and-localization-in-unity/#comments</comments>
		<pubDate>Tue, 11 Oct 2011 23:00:32 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[language]]></category>

		<guid isPermaLink="false">http://www.reenigne.org/blog/?p=1608</guid>
		<description><![CDATA[PHP is a pretty bad and ugly language in many ways, but it does have some advantages - because it's so ubiquitous for web development, a lot of effort has been put into getting it to run fast and including many useful libraries. Another advantage is its string manipulation, which is what I want to [...]]]></description>
			<content:encoded><![CDATA[<p>PHP is a pretty bad and ugly language in many ways, but it does have some advantages - because it's so ubiquitous for web development, a lot of effort has been put into getting it to run fast and including many useful libraries. Another advantage is its string manipulation, which is what I want to talk about today.</p>
<p>PHP has a nice feature whereby if you have a variable called $foo (all scalars in php start with the "$" sigil) and you want to output a string containing that value, you can just write:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;The current foo value is <span style="color: #006699; font-weight: bold;">$foo</span> units.&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In other words, you don't have to close the string to insert a variable into it. If this feature didn't exist you'd have to write:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;The current foo value is &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$foo</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; units.&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>as in C++:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">cout <span style="color: #339933;">&lt;&lt;</span> <span style="color: #0000ff;">&quot;The current foo value is &quot;</span> <span style="color: #339933;">&lt;&lt;</span> foo <span style="color: #339933;">&lt;&lt;</span> <span style="color: #0000ff;">&quot; units.&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>which doesn't seem like much more typing, but when you're writing web pages (which are full of these inserts) it really adds up and makes the result much more pleasant to use. I was skeptical at first but after having written some PHP code I want to steal this feature for my own language. Being a statically typed language, Unity also inserts .toString() calls when the inserted variable is not of String type.</p>
<p>Inserting more complex expressions is also possible. Though I think PHP's syntax for this is a bit confusing:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #0000ff;">&quot;&lt;div class='logged_in_as'&gt;Logged in as: {<span style="color: #006699; font-weight: bold;">$user-&gt;link</span>()}&lt;/div&gt;&quot;</span></pre></div></div>

<p>Much better to have a single insertion character and allow what follows to be either a variable name or a parenthesized expression:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #FF0000;">&quot;&lt;div class='logged_in_as'&gt;Logged in as: $(user.link())&lt;/div&gt;&quot;</span></pre></div></div>

<p>The trouble with including literal strings like this in your program, though, is that sooner or later you're going to want to translate it into other (human) languages. Current GNU C/C++ best practices for internationalizable software suggest enclosing UI strings in a gettext macro (_("foo")) which adds that string to a table and replaces the string itself with an index into that table - then localization is just a matter of replacing the table. At Microsoft we avoided putting UI strings in source code altogether, instead putting them in an .rc file and making up a macro name to refer to the string's index. That was painful, because to add a string we had to change three files - the source file (relating code to macro name), the header file (relating macro name to index) and the resource file (relating macro name to English text). I suppose there might have been economic forces resisting change to this system - Microsoft software is translated into so many languages that adding a UI string is quite expensive - you have to pay to have it translated a lot of times. Similarly with changing a UI string.</p>
<p>Both these methods also constrain how you need to phrase your UI strings. The canonical example is that you can't write:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span>_<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%i file%s copied&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> n<span style="color: #339933;">,</span> n<span style="color: #339933;">==</span><span style="color: #0000dd;">1</span> <span style="color: #339933;">?</span> <span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #339933;">:</span> <span style="color: #ff0000;">&quot;s&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>because <a href="http://www.gnu.org/s/hello/manual/gettext/Plural-forms.html">the rules for localization vary from language to language</a>. Translating would involve changing the code as well as the contents of the string. Thus such pieces of UI usually get phrased as:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span>_<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Files copied: %i&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>instead.</p>
<p>I want to include a localization system right in the Unity compiler which solves all these problems. The high level aims:</p>
<ul>
<li>It should be easy to add a string with inserts - as easy as it is in PHP. Modifying strings should be similarly easy - no editing multiple files, no _(), no making up names for the strings or finding the next available ID number.</li>
<li>The system should keep track of strings between compiles so that they can be consistently associated with their translations.</li>
<li>Translators should be able to permute, duplicate, add, remove and modify inserts as appropriate for the target language.</li>
<li>Software designers should not have to compromise their designs in the original language to make it possible to translate them into other languages - in other words there is no need to explicitly internationalize strings - it happens automatically (of course, there are non-string internationalization issues which are not in scope here, such as <a href="http://blogs.msdn.com/b/oldnewthing/archive/2005/11/29/497864.aspx">staying far, far away from maps</a>.</li>
</ul>
<p>To accomplish this, I think the best way is to have a tool (or perhaps a special mode of the compiler) which modifies the source files it processes. This is generally considered a bad thing but in this case I think it's the best solution. The one and only change it makes is to add an identifying number to the start of each string. The insert system gives a handy way to do this, so it might change:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #FF0000;">&quot;&lt;div class='logged_in_as'&gt;Logged in as: $(user.link())&lt;/div&gt;&quot;</span></pre></div></div>

<p>to:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #FF0000;">&quot;$(/*12345*/)&lt;div class='logged_in_as'&gt;Logged in as: $(user.link())&lt;/div&gt;&quot;</span></pre></div></div>

<p>If you're a programmer changing this string, you now have a choice - you can delete the "$(/*12345*/)" part, causing the translators to consider this a brand new string to be translated from scratch, or you can leave it alone, causing the translators to consider it a modification rather than a deletion and addition. The IDs are only ever added by this tool - no programmer should ever have to try to figure out which is the next available number by hand. It might be a good idea for the version control system to be made aware of this system so that if two strings are added with the same number on different branches and the branches are then merged, the merge system knows how to change one of the IDs automatically (everywhere that it appears). Similar empty inserts with comments can be used to leave notes for translators, such as telling them the context in which a particular string is used.</p>
<p>For non-UI strings, the programmer and/or translator can change the ID to a dummy so that it's clear to all concerned that the string does not need to be translated:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #FF0000;">&quot;$(/**/)&lt;html&gt;&quot;</span></pre></div></div>

<p>The interface for translators should be something like this. The tool creates one file (the translations file) for each generated binary for each language the software is translated into. This file contains one line per string in the original source, and that line contains the original string (including ID and comments), the translated string, and any notes from the translator. The compiler then reads this generated file back in to generate translation files for each language/binary combination. The translator can either edit this file directly or use some kind of tool, but either way it just gets checked back into the version control system.</p>
<p>The tool also checks for differences between the translations file and the source, and tells the translator which strings have been added and changed (including where the string appears in the original source, so the translator can look up the context). Because the translation file contains the inserts as source (the variable name or expression after the $) the translator is free to change this code to make sense for other languages. To handle the plural case, the string might be written as:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">print<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;$(/*120*/)$n file$(n == 1 ? &quot;</span><span style="color: #ff0000;">&quot; : &quot;</span>s<span style="color: #ff0000;">&quot;) copied&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>and the translator can then change anything inside the outermost double-quotes to make the string make sense for the target language. That might even involve calling out to external code to handle complicated cases:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #ff0000;">&quot;$(/*120*/)$n file$(n == 1 ? &quot;</span><span style="color: #ff0000;">&quot; : &quot;</span>s<span style="color: #ff0000;">&quot;) copied&quot;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;$n $(locale.plural(n, &quot;</span>plik<span style="color: #ff0000;">&quot;)) kopiowane&quot;</span></pre></div></div>

<p>An important difference here is that the compiled locale file can now contain code as well as data. This may introduce testing difficulties, but it's necessary to provide this "no compromise to end user experience" goal. If necessary, site policy could dictate the software internationalization rules we usually use now, or some middle ground like ensuring all locale methods are pure functions returning in bounded time, so they can't adversely affect other parts of the software.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.reenigne.org/blog/strings-and-localization-in-unity/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Templates and Kinds in Unity</title>
		<link>http://www.reenigne.org/blog/templates-and-kinds-in-unity/</link>
		<comments>http://www.reenigne.org/blog/templates-and-kinds-in-unity/#comments</comments>
		<pubDate>Mon, 10 Oct 2011 23:00:54 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[language]]></category>

		<guid isPermaLink="false">http://www.reenigne.org/blog/?p=1597</guid>
		<description><![CDATA[Programmers work with values like 4 and "hydroxylic". We also generally work with variables like count and acid. Each variable holds precisely one value. But in many languages, not every variable can hold any value. In a statically typed language, count might be able to hold 4 and acid might be able to hold "hydroxylic", [...]]]></description>
			<content:encoded><![CDATA[<p>Programmers work with values like <strong>4</strong> and <strong>"hydroxylic"</strong>. We also generally work with variables like <strong>count</strong> and <strong>acid</strong>. Each variable holds precisely one value. But in many languages, not every variable can hold any value. In a statically typed language, <strong>count</strong> might be able to hold <strong>4</strong> and <strong>acid</strong> might be able to hold <strong>"hydroxylic"</strong>, but not the other way around. That quality of variables and values that determines which objects can fit into which boxes is called type. So <strong>count</strong> might have a type of <strong>Integer</strong> and <strong>acid</strong> might have a type of <strong>String</strong>. Then if you have a function called <strong>square</strong> which multiplies its argument by itself and returns the result, it makes sense to say <strong>square(count)</strong> but not to say <strong>square(acid)</strong> - the latter is an error which is detected and flagged as quickly as possible (when the program is compiled, if not when it's typed in in the first place). On this blog, as a notational convention, types (and more generally type constructors, see below) start with a capital letter and variables and values don't. I think this is a useful convention, and some languages (<a href="http://www.reenigne.org/blog/cases">including the language I am writing</a>) enforce it.</p>
<p>A type constructor is a generalization of a type. It's either a type itself (which can be used to declare variables) or a template type constructor, a "function" of sorts which, when given another type constructor, yields a third type constructor. C++, C# and Java programmers are familiar with these "functions" - they're called templates or generics. A good example of a template type constructor is <strong>Array</strong>. You give it a type like <strong>String</strong> and you get back <strong>Array&lt;String&gt;</strong> (i.e. an array of strings). You can also have <strong>Array&lt;Array&lt;String&gt;&gt;</strong> if you're feeling adventurous. A type constructor can have multiple arguments, so you might have <strong>Pair&lt;Integer, String&gt;</strong> which (through the magic of something called <a href="http://en.wikipedia.org/wiki/Currying">currying</a>) is the same thing as <strong>Pair&lt;Integer&gt;&lt;String&gt;</strong>.</p>
<p>What if you have a template type constructor with a parameter that is not a type but is itself a template type constructor? Well, you can do that too - say you have a type constructor called <strong>FooCollection</strong> and you pass it <strong>Array</strong> (note, not <strong>Array&lt;Integer&gt;</strong> or <strong>Array&lt;String&gt;</strong>, which are types, just plain <strong>Array</strong>) and you get back <strong>FooCollection&lt;Array&gt;</strong> which might be implemented in terms of <strong>Array&lt;Foo&gt;</strong>. Similarly, <strong>FooCollection&lt;List&gt;</strong> might be implemented in terms of <strong>List&lt;Foo&gt;</strong>.</p>
<p>So if the type of the argument of <strong>square</strong> is <strong>Integer</strong>, how do we talk about the argument of <strong>FooCollection</strong>. We can't say "the type of the argument of <strong>FooCollection</strong>" because what it accepts isn't a type - a type constructor can't have a type, type is a kind of type constructor. And in fact that is exactly the word we use instead: "the kind of the argument of <strong>FooCollection</strong> is <strong>&lt;&gt;</strong>." Other examples of kinds are (nothing, i.e. the empty string) which is the kind of types like <strong>Integer</strong> and <strong>String</strong> (no angle brackets follow them), <strong>&lt;&gt;&lt;&gt;</strong> aka <strong>&lt;,&gt;</strong> (which is the kind of <strong>Pair</strong> - i.e. you need to give it two types to get back a type) and <strong>&lt;&lt;&gt;&gt;</strong> (which is the kind of <strong>FooCollection</strong> - i.e. you need to give it a type constructor of kind <strong>&lt;&gt;</strong> to get back a type). The <a href="http://en.wikipedia.org/wiki/Kind_(type_theory)">wikipedia article on kinds</a> uses a different notation further removed from the C++-inspired one I use here, but the two are equivalent and the transformation from one to the other is purely mechanical.</p>
<p>Incidentally, you can refer to kinds in C++ as well - instead of the empty string for the kind of types, C++ uses "class" (a rather confusing overload of the word - distinct from "a structured type with default accessibility private") or "typename" which is a bit better. Instead of <strong>&lt;&gt;</strong>, C++ uses "template&lt;typename&gt;", instead of <strong>&lt;&gt;&lt;&gt;</strong>, C++ uses "template&lt;typename, typename&gt;" (C++ has no currying) and instead of <strong>&lt;&lt;&gt;&gt;</strong> C++ uses "template&lt;template&lt;typename&gt;&gt;" (a so-called "template template parameter" - one of the more obscure areas of C++).</p>
<p>So we've gone from values to types to kinds, what comes next in the sequence? ("Sort?" that already has another meaning in computer science though.) Suppose you had a function which accepts a kind and returns another kind. My angle brackets here have that effect - they take a list of kinds and return a kind which is the kind of a type constructor that accepts type constructors of the listed kinds and returns a type. What a confusing sentence! Fortunately nobody to my knowledge has found a non-trivial use for anything one level up from kind. I say "fortunately" not just because it's really confusing but because we've run out of notation! If you start with a lower case letter to signify a value argument and an upper case letter to signify a type constructor argument, how do you signify a kind argument? Also, if we'd need to use different brackets (I almost wrote "a different kind of brackets" which would further confuse things - writing about this stuff is hard!) to distinguish kind functions from value functions and type constructor functions, and all the ASCII brackets already have other meanings in my language.</p>
<p>Anyway, this is the sort of thing that you bump your head against when you write compilers for languages which support templates. Fortunately, if you're just writing normal applications in this language you'll probably never need to be concerned with it. Until you do. At which point just doing the most obvious thing should work fine without any fuss. I think it's fun to think about though.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.reenigne.org/blog/templates-and-kinds-in-unity/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>No exceptions</title>
		<link>http://www.reenigne.org/blog/no-exceptions/</link>
		<comments>http://www.reenigne.org/blog/no-exceptions/#comments</comments>
		<pubDate>Thu, 29 Sep 2011 23:00:11 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[humour]]></category>
		<category><![CDATA[language]]></category>

		<guid isPermaLink="false">http://www.reenigne.org/blog/?p=1564</guid>
		<description><![CDATA[Recently I was out riding my bike and I saw a car sporting this bumper sticker: All I could think was that the owner of that car must really like to use return codes for their error handling.]]></description>
			<content:encoded><![CDATA[<p>Recently I was out riding my bike and I saw a car sporting this bumper sticker:<br />
<img class="aligncenter" src="http://www.reenigne.org/misc/no_exceptions.jpg" alt="God Bless Everyone - No Exceptions!" /><br />
All I could think was that the owner of that car must really like to use return codes for their error handling.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.reenigne.org/blog/no-exceptions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting rid of GOTOs</title>
		<link>http://www.reenigne.org/blog/getting-rid-of-gotos/</link>
		<comments>http://www.reenigne.org/blog/getting-rid-of-gotos/#comments</comments>
		<pubDate>Mon, 26 Sep 2011 23:00:50 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[language]]></category>

		<guid isPermaLink="false">http://www.reenigne.org/blog/?p=1551</guid>
		<description><![CDATA[It's well known that it's possible to change a program that uses GOTOs into one that uses loops and conditionals if you're allowed to duplicate code or add more variables. One very easy way is just to change the program into a state machine, with one state for each label and a big loop around [...]]]></description>
			<content:encoded><![CDATA[<p>It's well known that it's possible to change a program that uses GOTOs into one that uses loops and conditionals if you're allowed to duplicate code or add more variables. One very easy way is just to change the program into a state machine, with one state for each label and a big loop around the whole thing. But under what conditions can you eliminate GOTOs if you can't do either?</p>
<p>The "Multiple break" example in <a href="http://www.reenigne.org/blog/more-keywords">the "More keywords" blog post</a> is a piece of code that can't be de-GOTOized in C. I'm wondering if there are any examples of pieces of code that can't be de-GOTOized without multiple break, extra variables or code duplication. If I understand the abstract of <a href="http://dl.acm.org/citation.cfm?id=362337&#038;dl=ACM&#038;coll=DL&#038;CFID=38916776&#038;CFTOKEN=92062495">Peterson 1973</a> correctly I think there aren't - that multiple break is enough to make GOTO redundant (but I can't find a non-paywalled copy of that article to check, and I don't care enough to spend $15 to find out).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.reenigne.org/blog/getting-rid-of-gotos/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using templates when you don&#039;t really need to</title>
		<link>http://www.reenigne.org/blog/using-templates-when-you-dont-really-need-to/</link>
		<comments>http://www.reenigne.org/blog/using-templates-when-you-dont-really-need-to/#comments</comments>
		<pubDate>Sun, 25 Sep 2011 23:00:57 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[language]]></category>

		<guid isPermaLink="false">http://www.reenigne.org/blog/?p=1549</guid>
		<description><![CDATA[Templated C++ classes are a bit nicer to use than non-templated classes, because one can define the entire class within its declaration without having to make sure that the types it uses are defined first (this check is done at instantiation time for template classes, but at parse time for non-template classes). I have found [...]]]></description>
			<content:encoded><![CDATA[<p>Templated C++ classes are a bit nicer to use than non-templated classes, because one can define the entire class within its declaration without having to make sure that the types it uses are defined first (this check is done at instantiation time for template classes, but at parse time for non-template classes). I have found myself making classes templates when they don't really need to be just so that I don't have to define member functions outside the declaration - i.e. when CTemplate<T> doesn't actually use T for anything, and CTemplate is just used as "typedef CTemplate<void> C;". T may be used as the template parameter to other classes defined this way, though.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.reenigne.org/blog/using-templates-when-you-dont-really-need-to/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Is Haskell too abstract?</title>
		<link>http://www.reenigne.org/blog/is-haskell-too-abstract/</link>
		<comments>http://www.reenigne.org/blog/is-haskell-too-abstract/#comments</comments>
		<pubDate>Sat, 24 Sep 2011 23:00:54 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[language]]></category>

		<guid isPermaLink="false">http://www.reenigne.org/blog/?p=1547</guid>
		<description><![CDATA[The Haskell programming language implements a lot of very cool ideas (many of which I want to liberate for my programming language). However, Haskell seems to have a reputation for being a very difficult language to learn. The IO monad seems to be one particular sticking point, but this didn't seem to be particularly difficult [...]]]></description>
			<content:encoded><![CDATA[<p>The Haskell programming language implements a lot of very cool ideas (many of which I want to liberate for my programming language). However, Haskell seems to have a reputation for being a very difficult language to learn. The IO monad seems to be one particular sticking point, but this didn't seem to be particularly difficult to me - it's just a clever little hack (I just read lots of "How I came to understand the IO monad" accounts until I got it).</p>
<p>Another sticking point seems to be there's a lot of stuff written about Haskell in the form of academic papers (with all the academic jargon that entails). That shouldn't be a surprise (since it's a language with origins in academia which seems to be only reluctantly escaping to industry), but it is kind of interesting that there's a sort of language barrier between academia and industry - what the former calls "deforestation hylomorphism" might be called "tree flattening" by the latter, for example.</p>
<p>But I think the real problem is something shared by other functional languages - it's just a different level of abstraction than we're used to thinking about. In imperative programming languages programmers can think "okay, what do I want the machine to do here?" and write that code. In functional programming languages one instead has to think about what the inputs and outputs are, write functions to perform those transformations and trust that the compiler will optimize it well (to a first approximation). It's much more like doing mathematics than imperative programming. Compilers will do all sorts of crazy things to turn those pure functions into high-performance imperative code. So when things are inevitably too slow, it's not obvious why (since the relationship between the generated code and the source code is very complicated) and it's difficult to understand how to make it faster.</p>
<p>Functional programming languages do have some interesting advantages over imperative languages, though - it's much easier to reason about pure functions than about imperative programs with all their attendant state, which means lots more interesting automatic optimizations are possible and (increasingly importantly) the code can be automatically parallelized so it takes maximum advantage of modern multi-core CPUs.</p>
<p>I'm not sure which side to place my money on. I suspect both imperative and functional paradigms will continue to exist for the foreseeable future. On the imperative side: the low-level OS and runtime components must can't be written using functional languages, and optimizations and multi-core abstractions for imperative languages will continue to improve. On the functional side, compilers will require less and less hand-holding to generate good code and will generate better code than imperative compilers for more and more situations.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.reenigne.org/blog/is-haskell-too-abstract/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>When is your program compiled?</title>
		<link>http://www.reenigne.org/blog/when-is-your-program-compiled/</link>
		<comments>http://www.reenigne.org/blog/when-is-your-program-compiled/#comments</comments>
		<pubDate>Thu, 22 Sep 2011 23:00:15 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[language]]></category>

		<guid isPermaLink="false">http://www.reenigne.org/blog/?p=1540</guid>
		<description><![CDATA[When we're all using languages which can compile code at runtime it's going to be more important to think about when your code is being compiled. Take a fractal plotter for example - one in which users can supply a formula to be iterated. Obviously we'd like to do some compilation here instead of just [...]]]></description>
			<content:encoded><![CDATA[<p>When we're all using languages <a href="http://www.reenigne.org/blog/compiler-compilers">which can compile code at runtime</a> it's going to be more important to think about when your code is being compiled. Take a fractal plotter for example - one in which users can supply a formula to be iterated. Obviously we'd like to do some compilation here instead of just interpreting the formula, as the latter would be very slow. But how often should we recompile it? The more often we recompile, the more information we have available to us and the more optimizations we will be able to use. For example, if we just compile when the formula changes, we would have to use arithmetic operators which can work with any precision, but if we recompile whenever the precision changes we can unroll those arithmetic operators and make the code much faster (especially for low precisions). On the other hand, recompiling does have some overhead so we probably wouldn't want to recompile for each pixel. Though for some formulae that might actually be helpful - if we can hoist a test from per-iteration loop to the per-pixel loop and the iteration count is high it might be worth it.</p>
<p>One possibility might be to give the code-generation library the freedom to compile whenever it likes, so it can try various things and <a href="http://www.reenigne.org/blog/progressively-optimizing-compiler">run with what works best</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.reenigne.org/blog/when-is-your-program-compiled/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A weird thing in Haskell</title>
		<link>http://www.reenigne.org/blog/a-weird-thing-in-haskell/</link>
		<comments>http://www.reenigne.org/blog/a-weird-thing-in-haskell/#comments</comments>
		<pubDate>Wed, 21 Sep 2011 23:00:59 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[language]]></category>
		<category><![CDATA[maths]]></category>

		<guid isPermaLink="false">http://www.reenigne.org/blog/?p=1537</guid>
		<description><![CDATA[Here's something odd I noticed while playing around with the Haskell programming language. Sometimes, a==b does not imply f(a)==f(b). Look: &#62; 1/0 Infinity &#62; 1/&#40;-0&#41; -Infinity &#62; 0==&#40;-0&#41; True &#62; &#40;1/0&#41;==&#40;1/&#40;-0&#41;&#41; False]]></description>
			<content:encoded><![CDATA[<p>Here's something odd I noticed while playing around with the Haskell programming language. Sometimes, a==b does not imply f(a)==f(b). Look:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #339933; font-weight: bold;">&gt;</span> <span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">/</span><span style="color: red;">0</span>
Infinity
<span style="color: #339933; font-weight: bold;">&gt;</span> <span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">/</span><span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">0</span><span style="color: green;">&#41;</span>
<span style="color: #339933; font-weight: bold;">-</span>Infinity
<span style="color: #339933; font-weight: bold;">&gt;</span> <span style="color: red;">0</span><span style="color: #339933; font-weight: bold;">==</span><span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">0</span><span style="color: green;">&#41;</span>
True
<span style="color: #339933; font-weight: bold;">&gt;</span> <span style="color: green;">&#40;</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">/</span><span style="color: red;">0</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">==</span><span style="color: green;">&#40;</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">/</span><span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">0</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
False</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.reenigne.org/blog/a-weird-thing-in-haskell/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Thoughts on ActionScript</title>
		<link>http://www.reenigne.org/blog/thoughts-on-actionscript/</link>
		<comments>http://www.reenigne.org/blog/thoughts-on-actionscript/#comments</comments>
		<pubDate>Thu, 15 Sep 2011 23:35:10 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[language]]></category>

		<guid isPermaLink="false">http://www.reenigne.org/blog/?p=1500</guid>
		<description><![CDATA[Declaring variables "var i:int" instead of "int i" is a nice experiment, but I don't think it works. When I need to turn an assignment into a declaration or vice-versa, I now need to do it in two places (before and after the variable name) instead of just one. That this gives an error about [...]]]></description>
			<content:encoded><![CDATA[<p>Declaring variables "var i:int" instead of "int i" is a nice experiment, but I don't think it works. When I need to turn an assignment into a declaration or vice-versa, I now need to do it in two places (before and after the variable name) instead of just one.</p>
<p>That this gives an error about redeclaring i is just plain dumb:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">  <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>; i <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">10</span>; ++i<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> foo<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span>; <span style="color: #66cc66;">&#125;</span>
  <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>; i <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">10</span>; ++i<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> bar<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span>; <span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>It means I have to declare all my variables at the top, old-fashioned-C-style, or spend ages fixing up the declarations whenever I move code around.</p>
<p>I hate it when compilers enforce a "one class per file, with the same name" rule. I'd rather have my source files organized for the convenience of humans, not for the convenience of machines, thank you very much - if I have a bunch of small related classes, it's much better to have them in the same file.</p>
<p>No stack objects means that if you want to want to write a function that returns multiple values (e.g. a function that multiplies a matrix by a vector and returns a vector) you have to allocate an object to put the vector components in, which is very slow. For <a href="http://www.reenigne.org/blog/map-reprojection">reproject</a> I ended up just doing everything with plain values in a small number of functions - in C++ the code could be much more well-layered.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.reenigne.org/blog/thoughts-on-actionscript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Progressively optimizing compiler</title>
		<link>http://www.reenigne.org/blog/progressively-optimizing-compiler/</link>
		<comments>http://www.reenigne.org/blog/progressively-optimizing-compiler/#comments</comments>
		<pubDate>Wed, 31 Aug 2011 23:00:35 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[computer]]></category>
		<category><![CDATA[language]]></category>

		<guid isPermaLink="false">http://www.reenigne.org/blog/?p=1441</guid>
		<description><![CDATA[Normally when compiling a program, you tell the compiler how much optimization you want and what the input files are, it goes off and does its thing and comes back when it's done. Sometimes I think it might be nice if one instead told the compiler how much time to spend doing the compiling. It [...]]]></description>
			<content:encoded><![CDATA[<p>Normally when compiling a program, you tell the compiler how much optimization you want and what the input files are, it goes off and does its thing and comes back when it's done. Sometimes I think it might be nice if one instead told the compiler how much time to spend doing the compiling. It would then do the absolute minimum to make a working binary, and then gradually do more and more optimizations until the timer ran out. This would make the time it takes to rebuild things much more predictable. One downside is that the performance of the resulting program would depend on unpredictable things like how busy the build machine was. Another is that it's probably more efficient for a compiler to decide upfront what the optimizations should be rather than making lots of intermediate binaries of different optimization levels.</p>
<p>However, this sort of thing might be more useful as a run-time process - i.e. a JIT compiler. The system can monitor which bits of the code are being run most often (this is very easy to do - just interrupt at a random time and see what code is being run) and concentrate optimization efforts on those parts. The compilation can continue (gradually making the program faster and faster) until the point of diminishing returns is reached. I understand there's a <a href="http://en.wikipedia.org/wiki/HotSpot">Java Virtual Machine</a> which can do this.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.reenigne.org/blog/progressively-optimizing-compiler/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

