<?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>Whiley</title>
	<atom:link href="http://whiley.org/feed/" rel="self" type="application/rss+xml" />
	<link>http://whiley.org</link>
	<description>A Programming Language with Extended Static Checking</description>
	<lastBuildDate>Mon, 16 Apr 2012 21:00:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Termination of Flow Typing in Whiley</title>
		<link>http://whiley.org/2012/04/17/termination-of-flow-typing-in-whiley/</link>
		<comments>http://whiley.org/2012/04/17/termination-of-flow-typing-in-whiley/#comments</comments>
		<pubDate>Mon, 16 Apr 2012 21:00:14 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Examples]]></category>
		<category><![CDATA[Implementation]]></category>
		<category><![CDATA[Types]]></category>
		<category><![CDATA[Typing]]></category>

		<guid isPermaLink="false">http://whiley.org/?p=3881</guid>
		<description><![CDATA[<p>Whiley uses flow typing to give it the look-and-feel of a dynamically typed language (see this page for more on flow typing).  In short, flow typing means that variables can have different types at different program points.  For example:</p> define Node as { int data, Tree left, Tree right } define Tree as null <span style="color:#777"> . . . &#8594; Read More: <a href="http://whiley.org/2012/04/17/termination-of-flow-typing-in-whiley/">Termination of Flow Typing in Whiley</a></span>]]></description>
			<content:encoded><![CDATA[<p>Whiley uses <em>flow typing</em> to give it the look-and-feel of a dynamically typed language (see <a href="http://whiley.org/guide/typing/flow-typing/">this page</a> for more on flow typing).  In short, flow typing means that variables can have different types at different program points.  For example:</p>
<pre class="brush: whiley;">
define Node as { int data, Tree left, Tree right }
define Tree as null | Node

// Insert item into tree
Tree insert(Tree tree, int item):
    if tree is null:
        return {
          data: item,
          left: null,
          right: null
        }
    else if item &lt; tree.data:
        tree.left = insert(tree.left,item)
    else:
        tree.right = insert(tree.right,item)
    // done
    return tree
</pre>
<p>Here, Whiley&#8217;s flow typing system automatically <em>retypes</em> the variable <code>tree</code> to <code>Node</code> on the false branch of the condition <code>tree is null</code>.  Thus, we can safely use <code>tree.left</code> and <code>tree.right</code> without having to explicitly cast <code>tree</code> to a <code>Node</code>.  In this way, we see that variable <code>tree</code> has different types at different points.  In this case, it always has one of the following types: <code>Tree</code>, <code>Node</code> or <code>null</code> (the latter being its type on the true branch of the condition <code>tree is null</code>).</p>
<p>In the above example, the different types of tree are related &#8212; i.e. <code>Node</code> and <code>null</code> are both subtypes of <code>Tree</code>.   However, Whiley&#8217;s flow typing system can also work with unrelated types.  A more involved example, based on my Chess benchmark, illustrates:</p>
<pre class="brush: whiley;">
define LongPos as { int col, int row }
define LongMove as {
  Piece piece,
  LongPos from,
  LongPos to
}

define ShortPos as {int row} | {int col}
define ShortMove as {
  Piece piece,
  ShortPos from,
  LongPos to
}

// Find matching pieces on the board.
[Pos] find(Piece p, Board b):
    ...

// Intersect destination with possible moves of matching pieces.
[Pos] narrow(ShortMove m, [Pos] ms, Board b):
    ...

// Convert move in short notation into long notation.
LongMove convert(ShortMove m, Board b) throws Error:
    matches = find(m.piece,b)
    matches = narrow(m.to,matches,b)
    if |matches| != 1:
        throw Error(&quot;invalid move&quot;)
    m.from = matches[0]
    return m
</pre>
<p>This code converts moves expressed in <em>short algebraic notation</em> into <em>long algebraic notation</em> (see <a href="http://en.wikipedia.org/wiki/Algebraic_notation_(chess)">this</a> for more).  In the short notation, moves are given in an abbreviated (and potentially ambiguous) form with only the destination square given.  For example, a move <code>Nf6</code> indicates a Knight moving to square <code>f6</code>.  If the player has only one knight, then the move must refer to it.  However, if there are two Knights, the system must determine which it is.  This is done by <code>narrow()</code> above, which intersects the destination with the possible moves of the matching pieces.  In the case of multiple matches, an <code>Error</code> is thrown.  The notation also permits explicit disambiguation by providing either the <em>rank</em> or <em>file</em> of the given piece.  For example, <code>Neg3</code> indicates the Knight on file <code>e</code> moves to square <code>g3</code>.</p>
<p>Flow typing is crucial in type checking the above fragment.  The critical issue resolves around the statement <code>m.from = matches[0]</code>.  This retypes variable <code>m</code> from <code>ShortMove</code> to <code>LongMove</code>, allowing the subsequent statement <code>return m</code> to be type checked.  In this case, there is no subtyping relationship between <code>ShortMove</code> and <code>LongMove</code> &#8212; they are essentially unrelated.</p>
<h2>A Problem of Termination</h2>
<p>We now come to the main topic of this post.  Consider the following very simple example:</p>
<pre class="brush: whiley;">
define Rec as int | { Rec f }

Rec loopy(int x, int y):
  z = { f : x }
  while x &lt; y:
     z.f = z
     x = x + 1
  return z
</pre>
<p>This program currently causes the Whiley Compiler to enter an infinite loop! <em>But, why is that?</em> Well, the key is that flow typing is implemented in the style of a <a href="http://en.wikipedia.org/wiki/Data-flow_analysis">dataflow analysis</a>.  Roughly speaking, the algorithm employs an <em>environment</em> which maps variables to their current type.  It then processes each statement updating the environment as it proceeds.  The following illustrates the process for the first two statements above:</p>
<pre class="brush: whiley;">
Rec loopy(int x, int y):
  // x-&gt;int, y-&gt;int
  z = { f : x }
  // x=&gt;int, y=&gt;int, z=&gt;{int f}
  ...
</pre>
<p>In many ways, this is the natural way to implement the flow typing algorithm and follows, for example, the approach used in the JVM bytecode verifier (perhaps the most widely used example of flow typing).</p>
<p><em>So, why does the above short program not terminate?</em> Well, a dataflow analysis handles loops by iterating until a <em>fixed-point</em> is reached.  This is done by first propagating through the loop body to update the environment, like so:</p>
<pre class="brush: whiley;">
  // x=&gt;int, y=&gt;int, z=&gt;{int f}
  while x &lt; y:
      // x=&gt;int, y=&gt;int, z=&gt;{int f}
      z.f = z
      // x=&gt;int, y=&gt;int, z=&gt;{{int f] f}
      x = x + 1
      // x=&gt;int, y=&gt;int, z=&gt;{{int f] f}
</pre>
<p>At this point, it merges the new environment with that originally going into the loop and repeats the process:</p>
<pre class="brush: whiley;">
  // x=&gt;int, y=&gt;int, z=&gt;{int f}
  while x &lt; y:
      // x=&gt;int, y=&gt;int, z=&gt;{{int f}|int f}
      z.f = z
      // x=&gt;int, y=&gt;int, z=&gt;{{{int f}|int f} f}
      x = x + 1
      // x=&gt;int, y=&gt;int, z=&gt;{{{int f}|int f} f}
</pre>
<p>And then repeats again:</p>
<pre class="brush: whiley;">
  // x=&gt;int, y=&gt;int, z=&gt;{int f}
  while x &lt; y:
      // x=&gt;int, y=&gt;int, z=&gt;{{{int f}|int f} f}
      z.f = z
      // x=&gt;int, y=&gt;int, z=&gt;{{{{int f}|int f} f} f}
      x = x + 1
      // x=&gt;int, y=&gt;int, z=&gt;{{{{int f}|int f} f} f}
</pre>
<p>And so on, until there is no change in the environment (i.e. the fixed point is reached).  For a typical dataflow analysis, this should happen within a few iterations. However, for the above flow typing algorithm, <em>this process will never terminate because it is constructing successively larger types for variable <code>z</code></em>.  </p>
<p>In fact, we can obtain a valid flow-typing for our simple program as the following illustrates:</p>
<pre class="brush: whiley;">
Rec loopy(int x, int y):
  // x-&gt;int, y-&gt;int
  z = { f : x }
  // x=&gt;int, y=&gt;int, z=&gt;{int f}
  while x &lt; y:
      // x=&gt;int, y=&gt;int, z=&gt;{Rec f}
      z.f = z
      // x=&gt;int, y=&gt;int, z=&gt;{Rec f}
      x = x + 1
   // x=&gt;int, y=&gt;int, z=&gt;{Rec f}
   return z
</pre>
<p>The key is that assigning <code>{Rec f}</code> to field <code>f</code> gives the type <code>{{Rec f} f}</code> &#8212; which is a subtype of <code>{Rec f}</code>.  Hence, the above typing is valid (albeit conservative).  The problem is how to go about finding this typing, given that the standard dataflow approach does not succeed.</p>
<p>At this point, I&#8217;m not going to say too much more.  That&#8217;s because I&#8217;ve written up a <a href="http://www.ecs.vuw.ac.nz/~djp/files/ECSTR12-10.pdf">detailed account of the solution in a technical report</a>.  The key idea is to use a <em>constraint-based</em> approach, rather than a dataflow approach.  Whilst this is slightly more complex, it has one important advantage: <em>the algorithm always terminates!</em></p>
]]></content:encoded>
			<wfw:commentRss>http://whiley.org/2012/04/17/termination-of-flow-typing-in-whiley/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Are Checked Exceptions Always Caused by I/O?</title>
		<link>http://whiley.org/2012/04/10/are-checked-exceptions-always-caused-by-io/</link>
		<comments>http://whiley.org/2012/04/10/are-checked-exceptions-always-caused-by-io/#comments</comments>
		<pubDate>Mon, 09 Apr 2012 22:00:11 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Interest]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Exceptions]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://whiley.org/?p=3689</guid>
		<description><![CDATA[<p>Recently, I&#8217;ve had the pleasure of working with Eclipse and trying to build a plugin.  On the whole, I have to confess, I find that Eclipse is an extremely well-designed and considered piece of software.  The biggest problem, I suppose, is that it is designed for a wide variety of tasks and this means <span style="color:#777"> . . . &#8594; Read More: <a href="http://whiley.org/2012/04/10/are-checked-exceptions-always-caused-by-io/">Are Checked Exceptions Always Caused by I/O?</a></span>]]></description>
			<content:encoded><![CDATA[<p>Recently, I&#8217;ve had the pleasure of working with Eclipse and trying to build a plugin.  On the whole, I have to confess,<em> I find that Eclipse is an extremely well-designed and considered piece of software</em>.  The biggest problem, I suppose, is that it is designed for a wide variety of tasks and this means you spend a lot of time deciphering some rather abstract abstractions!  And, sure, the documentation could be better &#8212; but on the whole it is pretty good.</p>
<p>Anyway, this post is not really about Eclipse plugins.  One of things I had to do, was integrate my own build framework into that of Eclipse.  This immediately caused problems because of the Eclipse <a href="http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fcore%2Fruntime%2FCoreException.html">CoreException</a>.  This <a href="http://en.wikipedia.org/wiki/Exception_handling#Checked_exceptions">checked exception</a> is required on a large number of methods.  The problem is that my build framework requires the standard <code>java.io.IOException</code> on several methods, but <code>CoreException</code> does not extend this.  Since I don&#8217;t want my build framework to depend on Eclipse, I&#8217;m forced into doing something unusual.  For example, tunneling <code>CoreException</code>s through my framework as <code>IOException</code>s and vice-versa through Eclipse.  One could perhaps argue that <code>CoreException</code> should (or should not) implement <code>IOException</code> &#8212; <em>but, that&#8217;s not what this post is really about</em>.</p>
<p><em>So, what is this post about?</em> Well, this issue got me thinking: <em>are checked exceptions always caused by I/O?</em></p>
<p>Let&#8217;s try a simple thought experiment to get us thinking about this.  Imagine a program running on bare metal <em>which does not perform any I/O whatsoever</em>.  The question is: <em>in what ways can this program go wrong (i.e. crash)?</em> There are some obvious ones:</p>
<ol>
<li><strong>Segmentation Fault (or similar)</strong>.  For example, it tried to dereference a <code>null</code> pointer, divide by zero or access an array out-of-bounds, etc.</li>
<li><strong>Out-of-memory.</strong> The program ran out of memory and e.g. dumped its core.</li>
<li><strong>Infinite Loop.</strong> The program entered into some kind of infinite loop and continued forever.</li>
<li><strong>Deadlock.</strong> The program managed to enter some kind of dead lock (or even a <a href="http://en.wikipedia.org/wiki/Deadlock#Livelock">live lock</a>) and failed to make progress.</li>
</ol>
<p>Maybe there are some others, but this mostly covers it (remember, I/O is not permitted so we can&#8217;t e.g. block indefinitely on a socket).  <em>I want to argue that none of these things should cause a checked exception.</em> You might think that&#8217;s obvious because e.g. in Java none of these things is a checked exception (e.g. <code>NullPointerException</code>, <code>ArithmeticException</code>, <code>OutOfMemoryError</code>, etc, are unchecked).  But, as usual, it is more subtle than it first looks&#8230;</p>
<p><em>So, how could the above things cause a checked exception? </em>Well, the programmer will obviously try and protect against them happening.  For any given function, there are two main ways this can play out:</p>
<ul>
<li>In some cases, he/she will simply conclude that: <em>if the function is written correctly and called with valid arguments, an error could never happen</em> (e.g. divide-by-zero, out-of-bounds, infinite loop, etc). In these cases, he/she generally won&#8217;t to do anything specific (except perhaps a little bit of argument checking).</li>
<li>In other cases, he/she will conclude that: <em>this function must accept all inputs, but some of them are incorrect</em>.  A good example is a <code>parse(String)</code> function which parses a string and e.g. converts it into a data structure. In the case of a malformed string, it must report a <code>SyntaxError</code>.  The programmer may decide to implement this as a checked exception, so that any callers of the function are forced to acknowledge this possibility.</li>
</ul>
<p>These two cases may seem similar, but there is an important difference: in one case, our function only accepts &#8220;correct&#8221; inputs; in the other, it must also accept &#8220;incorrect&#8221; inputs.  Still, this seems fairly straightforward &#8230;</p>
<p>Consider again our thought experiment in the context of the <code>parse(String)</code> function, which <code>throws SyntaxError</code> (a checked exception).  Here&#8217;s the line of reasoning: Q) <em>Under what circumstances could a <code>SyntaxError</code> be thrown?</em> A) If the input string was invalid;  Q) <em>How could the input string be invalid?</em> A) If the programmer constructed an invalid string; Q) <em>How could the programmer have constructed an invalid string?</em> A) <strong>If he/she had made a programming error</strong>.  The thing is, since there is no I/O, the programmer has complete control over exactly what strings are constructed.  If an invalid string was constructed, it is because the programmer made a mistake somewhere.  <em>That&#8217;s the only possibility</em>.</p>
<p>If you&#8217;ve made it this far, great!  Hopefully, you&#8217;re starting to get the idea: <strong>without I/O, there&#8217;s really no need for checked exceptions</strong>.  The problem, of course, is that we do have I/O.  But, actually, it&#8217;s worse than that.  I won&#8217;t complain about having to write <code>throws IOException</code> on a function which can <code>throw</code> an <code>IOException</code>.  The problem is having to write it on a function which definitely <em>cannot</em> throw an <code>IOException</code>.  For example, I might call <code>parse()</code> from a setting where the input string is always syntactically valid.  But, I cannot tell the compiler this &#8212; so I&#8217;ll probably have to catch the <code>SyntaxError</code> and just discard it.  Maybe that&#8217;s not so bad, but it&#8217;s definitely one of the reasons checked exceptions have a bad reputation.</p>
<p>Finally, the other reason people get upset with checked exceptions is when they are used incorrectly.  Based on my above analysis, my conclusion is that Eclipse&#8217;s <code>CoreException</code> should extend <code>IOException</code>.  In fact, I think probably all checked exceptions should extend <code>IOException</code> &#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://whiley.org/2012/04/10/are-checked-exceptions-always-caused-by-io/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Test to Code Ratio</title>
		<link>http://whiley.org/2012/03/08/test-to-code-ratio/</link>
		<comments>http://whiley.org/2012/03/08/test-to-code-ratio/#comments</comments>
		<pubDate>Wed, 07 Mar 2012 21:00:09 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Interest]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Implementation]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://whiley.org/?p=3735</guid>
		<description><![CDATA[<p>I&#8217;ve just been watching the following talk over on InfoQ: Software Quality &#8212; You know it when you see it.  Thanks to Craig over at SoftViz for pointing me to it.  The talk is quite interesting, with the focus being primarily around using innovative visualizations of software to gauge quality.</p> <p>But, that&#8217;s not what <span style="color:#777"> . . . &#8594; Read More: <a href="http://whiley.org/2012/03/08/test-to-code-ratio/">Test to Code Ratio</a></span>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve just been watching the following talk over on InfoQ: <a href="http://www.infoq.com/presentations/Software-Quality-You-Know-It-when-You-See-It">Software Quality &#8212; You know it when you see it</a>.  Thanks to Craig over at <a href="http://softvis.wordpress.com/">SoftViz</a> for pointing me to it.  The talk is quite interesting, with the focus being primarily around using innovative visualizations of software to gauge quality.</p>
<p><em>But, that&#8217;s not what I want to talk about</em>.  Rather, there was one thing in particular the presenter said which I found intriguing.  He was talking about the <em>test-to-code-ratio &#8212;</em> the number of <em>Lines of Production Code (LPC)</em> versus the number of <em>Lines of Test Code (LTC)</em>.  A ratio of e.g. 1:4 indicates that, for every Line of Production Code, there are 4 Lines of Test Code.</p>
<p>Now, here&#8217;s the thing: <em>the presenter claimed that for Java (and possibly .Net), the ratio should be roughly 1:1, where as for Ruby it should be around 1:2 or even 1:3</em>.  I should emphasise that he based these claims on the research of others (although it&#8217;s not clear exactly who).  And, he went on to discuss the reason for the higher ratio required for Ruby (and other dynamically typed languages): <em>that the greater expressivity of these languages makes it harder to write tests for them.</em></p>
<p>If you follow this blog at all, you&#8217;ll know I&#8217;m a fan of static typing.  In fact, my language, <a href="http://whiley.org">Whiley</a>, is about going even further along that spectrum.  One of the main advantages claimed by proponents is that static typing catches errors ahead of time.  In contrast, many detractors claim that, since static typing only catches a small class of error, you still have to rigorously test your code anyway &#8212; so why burden yourself with static types?  Naturally, then, the above claim about the test-to-code ratio of Java versus Ruby leads to the question: <em>in looking at the test-to-code ratio, are we also looking at the trade-off between static and dynamic types? </em>Because, if we are, then it might seem to indicate that, actually, static typing does quite a lot for us.</p>
<p>But, obviously, it&#8217;s not that simple.  For example, it could well be that Ruby programs are, on average, significantly shorter than their equivalent Java programs.  If this ratio was, say, 1:3 (that is, Java programs are three times longer than Ruby programs) then the burden of having to write more tests wouldn&#8217;t seem so bad&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://whiley.org/2012/03/08/test-to-code-ratio/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Whiley v0.3.14 Released!</title>
		<link>http://whiley.org/2012/03/07/whiley-v0-3-14-released/</link>
		<comments>http://whiley.org/2012/03/07/whiley-v0-3-14-released/#comments</comments>
		<pubDate>Tue, 06 Mar 2012 21:17:39 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Wyjc]]></category>

		<guid isPermaLink="false">http://whiley.org/?p=3781</guid>
		<description><![CDATA[<p>Finally, it&#8217;s time for yet another release.  The main change with this release has been a reworking of the compiler framework into a more serious build system called, unsurprisingly, the Whiley Builder System (wybs).  This has helped integration with both Ant and Eclipse.  However, there remain a few aspects of it that need improvement, <span style="color:#777"> . . . &#8594; Read More: <a href="http://whiley.org/2012/03/07/whiley-v0-3-14-released/">Whiley v0.3.14 Released!</a></span>]]></description>
			<content:encoded><![CDATA[<p>Finally, it&#8217;s time for yet another release.  The main change with this release has been a reworking of the compiler framework into a more serious build system called, unsurprisingly, the Whiley Builder System (wybs).  This has helped integration with both Ant and Eclipse.  However, there remain a few aspects of it that need improvement, most notable performance and dependencies.  Nevertheless, I&#8217;m very pleased with how the structure of the Whiley compiler is progressing.</p>
<h2>ChangeLog</h2>
<ul>
<li>Deployed the new Whiley Build System, which provides a generic framework for managing namespaces and package roots.  This is currently lacking good support for dependency tracking, but it should be easy enough to extend to support this.</li>
<li>Fixed quite a few bugs, and have identified quite a few more to be fixed.</li>
</ul>
<center>
<div class="wpfilebase-attachment">
 <div class="wpfilebase-rightcol">
  <div class="wpfilebase-filetitle">
   <a href="http://whiley.org/download/jar/wyjc-v0.3.14.jar" title="Download wyjc-v0.3.14">wyjc-v0.3.14</a><br />
      Created on March 6, 2012.  84 Downloads, 1.1 MB.<br/>
      BSD License
  </div>
 </div>
 <div style="clear: both;"></div>
</div></center>
<center>
<div class="wpfilebase-attachment">
 <div class="wpfilebase-rightcol">
  <div class="wpfilebase-filetitle">
   <a href="http://whiley.org/download/wdk/wdk-src-v0.3.14.tgz" title="Download wdk-src-v0.3.14">wdk-src-v0.3.14</a><br />
      Created on March 6, 2012.  90 Downloads, 2.2 MB.<br/>
      BSD License
  </div>
 </div>
 <div style="clear: both;"></div>
</div></center>
]]></content:encoded>
			<wfw:commentRss>http://whiley.org/2012/03/07/whiley-v0-3-14-released/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Commonwealth Bank of Australia (CBA) denies problem with Leap Year?</title>
		<link>http://whiley.org/2012/03/05/commonwealth-bank-of-australia-cba-denies-problem-with-leap-year/</link>
		<comments>http://whiley.org/2012/03/05/commonwealth-bank-of-australia-cba-denies-problem-with-leap-year/#comments</comments>
		<pubDate>Mon, 05 Mar 2012 06:00:54 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Interest]]></category>
		<category><![CDATA[Software Bugs]]></category>

		<guid isPermaLink="false">http://whiley.org/?p=3773</guid>
		<description><![CDATA[<p>It&#8217;s 8:30am on the 29th Februrary, 2012.  After a large number of complaints, ATM and Eftpos services are finally restored for the Commonwealth Bank of Australia (CBA).  See this, this and this for more details.</p> <p>Why is this interesting? Well, CBA is denying it had anything to do with the leap year.  Just a <span style="color:#777"> . . . &#8594; Read More: <a href="http://whiley.org/2012/03/05/commonwealth-bank-of-australia-cba-denies-problem-with-leap-year/">Commonwealth Bank of Australia (CBA) denies problem with Leap Year?</a></span>]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s 8:30am on the 29th Februrary, 2012.  After a large number of complaints, ATM and Eftpos services are finally restored for the Commonwealth Bank of Australia (CBA).  See <a href="http://media.smh.com.au/news/national-news/commbank-atms-crash-nationwide-3081850.html">this</a>, <a href="http://thewall.com.au/topics/67438-cba-atms-eftpos-restored-after-big-crash">this</a> and <a href="http://www.canberratimes.com.au/business/commbank-atms-crash-nationwide-20120229-1u1q9.html">this</a> for more details.</p>
<p><em>Why is this interesting?</em> Well, CBA is <a href="http://finance.ninemsn.com.au/newsbusiness/8427402/commonwealth-bank-denies-atm-glitch-caused-by-leap-year">denying it had anything to do with the leap year</a>.  Just a coincidence, apparently.</p>
<p>The thing is, Eftpos services for the New World and Pak&#8217;n'Save supermarkets in New Zealand did crash because of the leap year (see <a href="http://www.wanganuichronicle.co.nz/news/february-29-knocks-out-eftpos/1290368/">this</a> and <a href="http://www.stuff.co.nz/manawatu-standard/6498652/Eftpos-glitch-hits-supermarkets">this</a>).  <em>So, do we really believe CBA?</em> I&#8217;ll let you to decide &#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://whiley.org/2012/03/05/commonwealth-bank-of-australia-cba-denies-problem-with-leap-year/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Jobs versus Allen</title>
		<link>http://whiley.org/2012/03/05/jobs-versus-allen/</link>
		<comments>http://whiley.org/2012/03/05/jobs-versus-allen/#comments</comments>
		<pubDate>Mon, 05 Mar 2012 03:49:24 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Book Reviews]]></category>
		<category><![CDATA[Apple]]></category>
		<category><![CDATA[Books]]></category>
		<category><![CDATA[Microsoft]]></category>

		<guid isPermaLink="false">http://whiley.org/?p=3759</guid>
		<description><![CDATA[<p> <p></p> <p>I&#8217;ve recently finished Paul Allen&#8217;s excellent memoir entitled &#8220;Idea Man&#8221; and the recent biography of Steve Jobs by Walter Isaacson. Obviously, I didn&#8217;t read them at the same time &#8230; but one after there other! But, they were a fascinating read taken together, as they cover some of the same story from <span style="color:#777"> . . . &#8594; Read More: <a href="http://whiley.org/2012/03/05/jobs-versus-allen/">Jobs versus Allen</a></span>]]></description>
			<content:encoded><![CDATA[<p><center><br />
<table>
<tr>
<td><img class="size-full wp-image-3761" title="paul_allen" src="http://whiley.org/wp-content/uploads/2012/03/paul_allen.jpg" alt="" width="183" height="275" />
</td>
<td>
<img class="size-full wp-image-3762" title="steve_jobs" src="http://whiley.org/wp-content/uploads/2012/03/steve_jobs.jpg" alt="" width="182" height="277" />
</td>
</tr>
</table>
<p></center></p>
<p>I&#8217;ve recently finished Paul Allen&#8217;s excellent memoir entitled <a href="http://www.paulallen.com/">&#8220;Idea Man&#8221;</a> and the recent biography of Steve Jobs by <a href="http://en.wikipedia.org/wiki/Walter_Isaacson">Walter Isaacson</a>.  Obviously, I didn&#8217;t read them at the same time &#8230; but one after there other!  But, they were a fascinating read taken together, as they cover some of the same story from different angles.  I particularly enjoyed Paul Allen&#8217;s hacking stories from when he was a young budding programmer &#8230; they reminded me of good times!  And, understanding Steve Jobs has helped me understand Apple and what it stands for a lot more.  I&#8217;m still undecided whether I think open or closed platforms are the way forward, but obviously Apple has managed some pretty impressive stuff recently &#8230; so I guess we&#8217;ll see!</p>
]]></content:encoded>
			<wfw:commentRss>http://whiley.org/2012/03/05/jobs-versus-allen/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Type Aliasing in Java?</title>
		<link>http://whiley.org/2012/03/02/type-aliasing-in-java/</link>
		<comments>http://whiley.org/2012/03/02/type-aliasing-in-java/#comments</comments>
		<pubDate>Thu, 01 Mar 2012 13:00:31 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Implementation]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Structural Subtyping]]></category>

		<guid isPermaLink="false">http://whiley.org/?p=3691</guid>
		<description><![CDATA[<p>A problem I often encounter in Java is that I want to say &#8220;these two things are the same&#8221;, but Java won&#8217;t let me. Suppose I want to maintain an int[] array which is always sorted in my program. So, whenever I get one of these things, I can rely on it being sorted. Here&#8217;s <span style="color:#777"> . . . &#8594; Read More: <a href="http://whiley.org/2012/03/02/type-aliasing-in-java/">Type Aliasing in Java?</a></span>]]></description>
			<content:encoded><![CDATA[<p>A problem I often encounter in Java is that I want to say &#8220;these two things are the same&#8221;, but Java won&#8217;t let me. Suppose I want to maintain an <code>int[]</code> array which is always sorted in my program.  So, whenever I get one of these things, I can rely on it being sorted.  Here&#8217;s what I want it to look like:</p>
<pre class="brush: java;">
// find lowest index of matching entry, or return -1 if no match
int findLowest(sorted arr, int value) {
   int index = Arrays.binarySearch(arr,value)
   if(index &lt; 0) { return -1; } // no match
   // matched, so find lowest index
   index = index - 1;
   while(index &gt;= 0 &amp;&amp; arr[index] == value) {
       index = index - 1;
   }
   return index + 1;
}
</pre>
<p>I often find a method like this useful because, when there are multiple <code>int</code>s of the same value, <code>Arrays.binarySearch()</code> doesn&#8217;t guarantee which is found.</p>
<p>Of course, the above isn&#8217;t legal Java though!  That&#8217;s because there is no way to alias a name (e.g. <code>sorted</code>) with a type (e.g. <code>int[]</code>).  Now hold on, you say: you can just use <code>int[]</code> above, instead of <code>sorted</code>, and it will work fine!  True.  But, I want to use <code>sorted</code> to better document my program.  In C, you could use a <code>typedef</code>for this, and I&#8217;ve often found that useful.</p>
<p>So, overall, I think allowing such &#8220;type aliases&#8221; in Java would help: firstly, by providing better documentation; secondly, by allowing them to be type checked.  Now, there are quite a few gritty details to work out, such as the actual syntax for declaring a type alias.  I&#8217;m not proposing a JSR or anything here, but how about this:</p>
<pre class="brush: java;">
alias sorted of int[]

// find lowest index of matching entry, or return -1 if no match
int findLowest(sorted arr, int value) {
   ...
}
</pre>
<p>Seems simple enough.  Now, if we want aliases to be type checked, then we need a way to turn a given type (e.g. <code>int[]</code>) into an alias (e.g. <code>sorted</code>).  Well, I suppose a plain-old cast could do the job:</p>
<pre class="brush: java;">
alias sorted of int[]

// find lowest index of matching entry, or return -1 if no match
int findLowest(sorted arr, int value) {
   ...
}

sorted createSorted(int[] unsorted) {
   Arrays.sort(unsorted);
   return (sorted) unsorted;
}
</pre>
<p>Those type theorists amongst you will immediately raise your arms: <em>what if we subsequently update unsorted and break the sorting guarantee?</em> Well, yup, that could happen.  And, without something like <a href="http://en.wikipedia.org/wiki/Uniqueness_type">uniqueness types</a>, there&#8217;s not much we can do.  But, perhaps we can just live with it, given that we&#8217;re already living with similar issues related to generics and erasure.</p>
<p>Anyhow, it&#8217;s just a thought.  And, I&#8217;m obviously not the first person to some with this idea (see e.g. <a href="http://stackoverflow.com/questions/683533/type-aliases-for-java-generics">this</a>, <a href="http://stackoverflow.com/questions/5604390/how-do-i-create-some-variable-type-alias-in-java">this</a> and <a href="http://www.comp.lancs.ac.uk/~ss/java2c/diff/typedef">this</a>).</p>
]]></content:encoded>
			<wfw:commentRss>http://whiley.org/2012/03/02/type-aliasing-in-java/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>A Problem of Decoupling?</title>
		<link>http://whiley.org/2012/02/29/a-problem-of-decoupling/</link>
		<comments>http://whiley.org/2012/02/29/a-problem-of-decoupling/#comments</comments>
		<pubDate>Tue, 28 Feb 2012 20:14:43 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[Implementation]]></category>

		<guid isPermaLink="false">http://whiley.org/?p=3651</guid>
		<description><![CDATA[<p>Recently, I&#8217;ve been working on improving the core framework that underpins the Whiley compiler.  This provides a platform for reading/writing files of specified content in a structured fashion.  Like Java, Whiley provides a hierarchical namespace in which names live and can be imported by others.  Let&#8217;s consider a simple example:</p> package zlib.core import Console <span style="color:#777"> . . . &#8594; Read More: <a href="http://whiley.org/2012/02/29/a-problem-of-decoupling/">A Problem of Decoupling?</a></span>]]></description>
			<content:encoded><![CDATA[<p>Recently, I&#8217;ve been working on improving the core framework that underpins the Whiley compiler.  This provides a platform for reading/writing files of specified content in a structured fashion.  Like Java, Whiley provides a hierarchical namespace in which names live and can be imported by others.  Let&#8217;s consider a simple example:</p>
<pre class="brush: whiley;">
package zlib.core

import Console from whiley.lang.System
import zlib.util.BitBuffer
</pre>
<p>Here, we have two modules that must exist in the global namespace: <code>whiley.lang.System</code> and <code>zlib.util.BitBuffer</code>.  As with Java, they co-exist in the same namespace, but do not necessarily originate from the same physical location (i.e. <code>whiley.lang.System</code> is located in the <code>wyrt.jar</code>, whilst <code>zlib.util.BitBuffer</code> is in a file system directory somewhere).</p>
<p>The Whiley compiler takes care of this through the <code>Path.ID</code> and <code>Path.Root</code> abstractions.  A <code>Path.ID</code> represents a hierarchical name in the global namespace (e.g. <code>whiley.lang.System</code>); a <code>Path.Root</code> represents a physical location which forms the root of a name hierarchy (e.g. a <code>jar</code> file or a directory).  Thus, the global namespace is made up from multiple roots and, to find a given item, we traverse them looking for it (we&#8217;ll ignore the possibility of collisions for simplicity).  To illustrate, here&#8217;s the (slightly simplified) <code>Path.ID</code> interface:</p>
<pre class="brush: java;">
public interface ID {

  /**
   * Get number of components in this ID.
   * ...
   */
  public int size();

  /**
   * Return the component at a given index.
   * ...
   */
  public String get(int index);

  /**
   * Get last component of this path ID.
   * ...
   */
  public String last();

  /**
   * Get parent of this path ID.
   * ...
   */
  public ID parent();

  /**
   * Append component onto end of this id.
   * ...
   */
  public ID append(String component);
}
</pre>
<p>This all seems simple enough, right?  <em>Well, yeah it is!</em></p>
<p><em>So, what&#8217;s up? </em> Well, the thing is, the Whiley compiler is not the first system to address this problem!  Eclipse, for example, adopts a similar approach through the <code>IPath</code> interface.  A cut-down version of this interface is:</p>
<pre class="brush: java;">
public interface IPath {
  /**
   * Returns the specified segment of this path, ...
   * ...
   */
  public String segment(int index);

  /**
   * Returns the last segment of this path, ...
   * ...
   */
  public String lastSegment();

  /**
   * Returns the number of segments in this path.
   * ...
   */
  public int segmentCount();

  /**
   * Returns whether this path is a prefix of the given path.
   * ...
   */
  public int isPrefixOf(IPath path);

  /**
   * Return absolute path with segments and device id.
   * ...
   */
  public IPath makeAbsolute(IPath path);

  ...
}
</pre>
<p>Hopefully, you&#8217;ll notice both similarity and difference between the <code>Path.ID</code> and <code>IPath</code> interfaces.  In fact, <code>IPath</code> has quite a few more methods which are not present in <code>Path.ID</code>.  However, it should be clear that the functionality described by <code>Path.ID</code> is a subset of that described by <code>IPath</code> (albeit with slightly different names).</p>
<p>Obviously, I want to integrate the Whiley compiler with Eclipse (i.e. make an Eclipse plugin for Whiley).  At the same time, I want to reuse as much of Eclipse&#8217;s functionality as possible within my plugin &#8212; otherwise, I&#8217;m just adding more bloat to an already bloated system, and potentially compromising the effectiveness of my plugin.  I&#8217;m prepared to go to some lengths to enable this, even to the point of changing my <code>Path.ID</code> interface to bring it more inline with <code>IPath</code>; however, <em>I&#8217;m not prepared to make the Whiley Compiler depend upon Eclipse</em> &#8212; that is, no <code>import org.eclipse..*</code> statements in the Whiley compiler.</p>
<p><em>How can I do this?</em> Well, the obvious solution is to provide an <a href="http://en.wikipedia.org/wiki/Adapter_pattern">Adaptor</a> in the plugin which implements <code>Path.ID</code> and wraps an <code>IPath</code>.  This probably requires adding a <a href="http://en.wikipedia.org/wiki/Abstract_factory_pattern">Factory</a> interface (e.g. <code>Path.Factory</code>) for creating <code>Path.ID</code> instances, since the Whiley compiler needs the ability to construct <code>Path.ID</code> instances and test for their validity.</p>
<p><em>The adaptor works, right?</em> Yeah, it does.  But, it amounts to layering one abstraction on top of another <em>when they&#8217;re really the same thing</em>.  It would be nice if there was a  mechanism for binding abstractions together.  For example, in the Eclipse Plugin, it would let me say: <em>let an <code>IPath</code> be a valid <code>Path.ID</code> with the following binding between names</em>.  But, perhaps that&#8217;s too much wishful thinking&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://whiley.org/2012/02/29/a-problem-of-decoupling/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Writing a PNG Decoder in Whiley!</title>
		<link>http://whiley.org/2012/02/18/writing-a-png-decoder-in-whiley/</link>
		<comments>http://whiley.org/2012/02/18/writing-a-png-decoder-in-whiley/#comments</comments>
		<pubDate>Fri, 17 Feb 2012 23:20:45 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Examples]]></category>
		<category><![CDATA[Interest]]></category>

		<guid isPermaLink="false">http://whiley.org/?p=3606</guid>
		<description><![CDATA[<p>Over the last few days, I have been writing GIF and PNG decoders in Whiley.  These form part of an image manipulation benchmark which I&#8217;m planning to use for experimenting with the compiler.  The PNG decoder, in particular, was rather interesting.  My implementation is based directly off the PNG Specification, ISO/IEC 15948:2003 (E). The PNG <span style="color:#777"> . . . &#8594; Read More: <a href="http://whiley.org/2012/02/18/writing-a-png-decoder-in-whiley/">Writing a PNG Decoder in Whiley!</a></span>]]></description>
			<content:encoded><![CDATA[<p>Over the last few days, I have been writing GIF and PNG decoders in Whiley.  These form part of an image manipulation benchmark which I&#8217;m planning to use for experimenting with the compiler.  The PNG decoder, in particular, was rather interesting.  My implementation is based directly off the <strong><a href="http://www.w3.org/TR/2003/REC-PNG-20031110/">PNG Specification, </a></strong><strong><a href="http://www.w3.org/TR/2003/REC-PNG-20031110/">ISO/IEC 15948:2003 (E)</a>. </strong>The PNG format uses the <a href="http://en.wikipedia.org/wiki/DEFLATE">DEFLATE algorithm</a> for decompression (as used by e.g. GZip).  This was convenient since I&#8217;d already implemented DEFLATE in Whiley!</p>
<p>You can find the complete code for my PNG decoder <a href="https://github.com/DavePearce/wybench/tree/master/sequential/convert/imagelib/png">here</a>.  To compile it, you&#8217;ll probably need to checkout the latest <a href="https://github.com/DavePearce/Whiley/tree/devel">development snapshot of the Whiley compiler</a>.  At the moment, it doesn&#8217;t run too fast either &#8230; but that&#8217;s mostly because arithmetic in Whiley is unbounded and, hence, relatively slow. Eventually, I will optimise most of this overhead away.</p>
<h2>Filtering</h2>
<p>The PNG format is interesting because it employs filtering at the scanline level.  This means it applies one of five possible functions to each scanline in order to increase the amount of order and, hence, improve the compression ratio.  For some reason, I got particularly stuck debugging this part.  The bugs, however, produced some interesting effects:<br />
<center><br />
<table>
<tbody>
<tr>
<td>
<p><div id="attachment_3609" class="wp-caption alignnone" style="width: 160px"><a href="http://whiley.org/wp-content/uploads/2012/02/medium-grayscale.png"><img class="size-full wp-image-3609 " title="Original Image" src="http://whiley.org/wp-content/uploads/2012/02/medium-grayscale.png" alt="" width="150" height="177" /></a><p class="wp-caption-text">(original)</p></div></td>
<td>
<p><div id="attachment_3610" class="wp-caption alignnone" style="width: 160px"><a href="http://whiley.org/wp-content/uploads/2012/02/img-nofiltering.png"><img class="size-full wp-image-3610 " title="No Filtering" src="http://whiley.org/wp-content/uploads/2012/02/img-nofiltering.png" alt="" width="150" height="177" /></a><p class="wp-caption-text">(without filtering)</p></div></td>
<td>
<p><div id="attachment_3616" class="wp-caption alignnone" style="width: 160px"><a href="http://whiley.org/wp-content/uploads/2012/02/img-second-bug.png"><img class="size-full wp-image-3616 " title="Bug in Paeth Predictor" src="http://whiley.org/wp-content/uploads/2012/02/img-second-bug.png" alt="" width="150" height="177" /></a><p class="wp-caption-text">(bug in Paeth Predictor)</p></div></td>
</tr>
</tbody>
</table>
<p></center><br />
On the left, we see the original image.  In the middle, we see the image produced by my decoder without filtering being applied.  On the right, we see the image produced by my decoder with a bug in the <a href="http://en.wikipedia.org/wiki/Portable_Network_Graphics#Filtering">Paeth Predictor</a> filtering function.  The filters are based around adding the value of a previous pixel (e.g. to the left or above) to the current pixel.  As you can see on the right, one mistake early on in a scanline is then propagated along the scanline producing a variety of different effects!</p>
<h2>Constraints</h2>
<p>Another interesting aspect of the PNG format are the constraints imposed on its data values.  The following excerpt from the spec defines constraints on the PNG header:</p>
<blockquote><p>The IHDR chunk shall be the first chunk in the PNG datastream. It contains:</p>
<table summary="This table defines the IHDR chunk">
<tbody>
<tr>
<td>Width</td>
<td>4 bytes</td>
</tr>
<tr>
<td>Height</td>
<td>4 bytes</td>
</tr>
<tr>
<td>Bit depth</td>
<td>1 byte</td>
</tr>
<tr>
<td>Colour type</td>
<td>1 byte</td>
</tr>
<tr>
<td>Compression method</td>
<td>1 byte</td>
</tr>
<tr>
<td>Filter method</td>
<td>1 byte</td>
</tr>
<tr>
<td>Interlace method</td>
<td>1 byte</td>
</tr>
</tbody>
</table>
<p>Width and height give the image dimensions in pixels. They are PNG four-byte unsigned integers. Zero is an invalid value.</p>
<p>Bit depth is a single-byte integer giving the number of bits per sample or per palette index (not per pixel). Valid values are 1, 2, 4, 8, and 16, although not all values are allowed for all colour types. See 6.1: Colour types and values.</p>
<p>Colour type is a single-byte integer that defines the PNG image type. Valid values are 0, 2, 3, 4, and 6.</p>
<p>Bit depth restrictions for each colour type are imposed to simplify implementations and to prohibit combinations that do not compress well.</p></blockquote>
<p>These constraints are particularly interesting because, in Whiley, <em>I can represent them directly in the code</em>.  The following illustrates the PNG header abstraction:</p>
<pre class="brush: whiley;">
public define ValidColorDepths as [
 {1,2,4,8,16}, // GREYSCALE
 {},           // (empty)
 {8,16},       // TRUECOLOR
 ...
]

public define IHDR as { // Image Header
  u32 width,
  u32 height,
  BitDepth bitDepth,
  ColorType colorType,
  ...
} where width &gt; 0 &amp;&amp; height &gt; 0 &amp;&amp; bitDepth in ValidColorDepths[colorType]
</pre>
<p>Here, we can see the constraints on the <code>width</code>, <code>height</code> and <code>bitdepth</code> are encoded directly into the program.  Currently, violation of these constraints results in a runtime assertion failure.  In the future, the aim is to check them at compile time.</p>
<h2>Conclusion</h2>
<p>It&#8217;s been fun learning about the PNG and GIF file formats.  And, of course, writing realistic benchmarks provides extremely helpful feedback on the language design.  All up, I&#8217;m very pleased to see that writing programs in Whiley was an enjoyable experience!  There&#8217;s a long way to go yet though, as writing these benchmarks has uncovered a bunch of compiler bugs for me to work on over the coming weeks and months &#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://whiley.org/2012/02/18/writing-a-png-decoder-in-whiley/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Design of the Whiley Compiler (Wyc) Front-End</title>
		<link>http://whiley.org/2012/02/13/design-of-the-whiley-compiler-wyc-front-end/</link>
		<comments>http://whiley.org/2012/02/13/design-of-the-whiley-compiler-wyc-front-end/#comments</comments>
		<pubDate>Sun, 12 Feb 2012 21:49:18 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://whiley.org/?p=3576</guid>
		<description><![CDATA[<p>Designing the front-end of the Whiley Compiler (Wyc) is a somewhat delicate and complicated issue.  I have iterated on this a few times, and still not found a solution I&#8217;m happy with.  It&#8217;s important, because it ultimately determines what is possible in terms of language syntax and what is not.  That is, when certain <span style="color:#777"> . . . &#8594; Read More: <a href="http://whiley.org/2012/02/13/design-of-the-whiley-compiler-wyc-front-end/">Design of the Whiley Compiler (Wyc) Front-End</a></span>]]></description>
			<content:encoded><![CDATA[<p>Designing the front-end of the Whiley Compiler (Wyc) is a somewhat delicate and complicated issue.  I have iterated on this a few times, and still not found a solution I&#8217;m happy with.  It&#8217;s important, because it ultimately determines what is possible in terms of language syntax and what is not.  That is, when certain events occur in the front-end determines whether or not I can pull off certain &#8220;tricks&#8221; to simplify my syntax or not.</p>
<p>In an effort to help me get a good handle on the problem, I&#8217;m going to try and enumerate the main issues here.  With any luck, the answer will just pop out &#8230;</p>
<h2>Overview</h2>
<p>The main task of the front-end is to convert Whiley source code into the Wyil intermediate bytecode.  The key aspects of Wyil are:</p>
<ol>
<li>Wyil is an unstructured intermediate language, whose programs are composed of bytecodes (similar in many ways to Java Bytecode).</li>
<li>All names used in Wyil bytecodes are fully resolved (i.e. they include appropriate package and module identifiers).</li>
<li>All Wyil bytecodes are associated with an appropriate type.  For example, the type of a <code>length</code> bytecode must be an effective collection (i.e. an effective list, set, dictionary or string).</li>
<li>All constraints should be expanded (or, at least, partially expanded)</li>
</ol>
<p>As an example, consider this Whiley program for summing a list of naturals:</p>
<pre class="brush: whiley;">
import nat from whiley.lang.Int

public int sum([nat] list):
    r = 0
    for i in list:
        r = r + i
    return r
</pre>
<p>This is compiled into the following Wyil bytecodes:</p>
<pre class="brush: whiley;">
public int sum([int] list):
requires:
    load 0 : [int]
    forall 2 : [int]
        load 2 : int
        const 0 : int
        ifge goto exitlab : int
        fail &quot;constraint not satisfied&quot;
    .exitlab
body:
    var r, i
    const 0 : int
    store r : int
    load list : [int]
    forall i : [int]
        load r : int
        load i : int
        add : int
        store r : int
    load r : int
    return : int
</pre>
<p>We can see that the type <code>nat</code> has been expanded to <code>int</code> by looking at its definition in <code>whiley.lang.Int</code>.   The requires clause represents the expanded constraints generated from <code>[nat]</code> &#8212; namely, that every element of <code>list</code> should be non-negative.</p>
<h3>Resolution</h3>
<p>An important aspect of converting Whiley source into Wyil is <em>resolution</em>.  This is about determining what a given name in a source file actually corresponds to by looking it up in the WHILEYPATH following the given import statements.  As part of this, types and constants must be expanded into fully resolved types and constants.  For example, consider this:</p>
<p>Light.java:</p>
<pre class="brush: whiley;">
define Light as {
  Point point,
  Colour colour
}

...
</pre>
<p>Scene.java:</p>
<pre class="brush: whiley;">
import Light

define Scene as {
 [Light] lights,
 [Object] objects
}

...

private Colour colourAt(Light light, Point pt):
   ...
</pre>
<p>In order to fully resolve the type <code>Scene</code>, we must resolve the type <code>Light</code>.  This involves, firstly, determining what module <code>Light</code> is defined is (by following the <code>import</code> trail) and, secondly, what <code>Light</code>&#8216;s fully resolved type is.  Similarly, we must trawl the source code of functions (e.g. <code>colourAt</code>) and resolved any names referred to (in this case, the type of parameter <code>light</code>).</p>
<p>Resolution is an inherently global task, since types obviously may be defined in terms of types in other modules.  Furthermore, types may be recursive across modules and modules may have dependencies going in both direction (e.g. type <code>X</code> in one module refers to type <code>Y</code> in another, whilst type <code>Z</code> in the latter refers to a type in the first, etc).</p>
<h3>Flow Typing</h3>
<p>The second major task faced in converting Whiley source into Wyil bytecodes is that of <em>flow typing</em>.  This is the process of determining a type for every expression and statement in the program, including all intermediate expressions used.  During this process some ambiguity between different expressions with the same syntax will be resolved (e.g. <code>x+y</code> can be either arithmetic addition, or set union &#8212; and we can only determine this when we know the types of <code>x</code> and <code>y</code>).</p>
<p>Looking at a similar example to before, we can see roughly how flow typing proceeds:</p>
<pre class="brush: whiley;">
real sum([real] list):
   r = 0           // r =&gt; int, based on type of constant 0
   for v in list:  // v =&gt; real, based on type of list
      r = r + v   // r =&gt; real, based on type of operands
   return r       // r =&gt; real|int, based on type of r after loop
</pre>
<p>The key difficulty with flow-typing is that is an inherently flow-sensitive activity, which is particularly problematic in the presence of exceptions and retyping expressions (i.e. those involving <code>is</code>).</p>
<h2>Two Differing Approaches</h2>
<p>One of the key design issues in the compiler front-end is the choice of when to convert from the source-level AST into the Wyil bytecodes.  There are essentially two main approaches:</p>
<ol>
<li><strong>(Late-Generation)</strong> Here, flow-typing is performed directly on the AST which is subsequently converted into Wyil bytecodes.</li>
<li><strong>(Early-Generation)</strong> Here, the AST is first converted into temporary Wyil bytecodes which are then flow-typed to produced the final bytecodes.</li>
</ol>
<p>For some reason, the choice of which approach to take has caused me a lot of headaches.  I believe either can be made to work.  The issue, of course, is in finding the simplest most coherent approach.  This always seems to save me time in the long run.  One reason for this is that generation is intertwined with <em>constraint expansion</em> (that is, generating Wyil bytecodes corresponding to source-level constraints).  The main trade-offs are:</p>
<ul>
<li><strong>Late-Generation. </strong> This is probably the most logical approach.  However, it is harder to properly flow-type the AST because of retyping (i.e. <code>is</code>) expressions.  On the other hand, there is potential to support retyping of complex retyping (e.g. involving records and possibly lists).  This approach also requires an awkward abstraction which I refer to the as &#8220;unconstrained types&#8221;.</li>
<li><strong>Early-Generation.</strong> This is awkward because it requires special abstract bytecodes to encode ambiguous expressions which can only be resolved when types are known (the trickiest being related to the various kinds of method invocation).  However, flow-typing is much simplified because it operates on the unstructured intermediate language where flow-sensitive analysis is easier.  There is also no need to support the notion of an &#8220;unconstrained type&#8221;.  Another downs-side of this approach is that it relies on Wyil bytecodes being able to encode nominal types.  However, I want to support this anyway in order to simplify debugging Wyil code.</li>
</ul>
<p>The issue of unconstrained types is slighty odd.  It stems from a separation from expanding types and expanding constraints.  For example, consider this:</p>
<pre class="brush: whiley;">
define nat as int where $ &gt;= 0

int f(nat|[int] x):
    if x is nat:
        return 0
    else:
        return |x|
</pre>
<p>Currently, this program should (in fact) fail compilation because <code>x</code> must have type <code>int|[int]</code> on the false branch.  The notion of unconstrained types handles this by replaced all constrained types with <code>void</code> before computing the type difference on the false branch.  If the constrained was expanded before type checking this function, then there would be no need for an unconstrained type as it would be implicit.  Things get particularly awkward when we consider types in compiled libraries, since one cannot retro-actively determine the unconstrained type.  Instead, for every type, we&#8217;ll have to embed the unconstrained version as well.</p>
<p>Ok, that&#8217;s it for now.  I&#8217;m not sure how much this is helping me, but I probably need to add more of the subtle trade-offs as I remember them.</p>
]]></content:encoded>
			<wfw:commentRss>http://whiley.org/2012/02/13/design-of-the-whiley-compiler-wyc-front-end/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Whiley v0.3.13 Released!</title>
		<link>http://whiley.org/2012/01/24/whiley-v0-3-13-released/</link>
		<comments>http://whiley.org/2012/01/24/whiley-v0-3-13-released/#comments</comments>
		<pubDate>Tue, 24 Jan 2012 03:45:24 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Implementation]]></category>
		<category><![CDATA[Syntax]]></category>
		<category><![CDATA[Wyjc]]></category>

		<guid isPermaLink="false">http://whiley.org/?p=3325</guid>
		<description><![CDATA[<p>Well, it&#8217;s been almost two months in the making, but here&#8217;s the next release of Whiley.  Quite of lot of changes, although there remain significant issues to resolve &#8212; particularly with the front-end.</p> ChangeLog Fixed outstanding problem with list and set types related to type tests.  More specifically, on the negative branch of a <span style="color:#777"> . . . &#8594; Read More: <a href="http://whiley.org/2012/01/24/whiley-v0-3-13-released/">Whiley v0.3.13 Released!</a></span>]]></description>
			<content:encoded><![CDATA[<p>Well, it&#8217;s been almost two months in the making, but here&#8217;s the next release of Whiley.  Quite of lot of changes, although there remain significant issues to resolve &#8212; particularly with the front-end.</p>
<h2>ChangeLog</h2>
<ul>
<li>Fixed outstanding problem with list and set types related to type tests.  More specifically, on the negative branch of a type test the correct type was not always being calculated.</li>
<li>Added support for open records.</li>
<li>Added a CONTRIBUTORS file for recording project contributors.  This is based on the <a href="http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=blob;f=Documentation/SubmittingPatches">developers certificate of origin</a> used by the Linux Kernel.</li>
<li>Changed syntax for processes to be now &#8220;references&#8221;.  This avoids conflicting the name with OS processes.  So, e.g. type <code>process T</code> becomes <code>ref T</code>.</li>
<li>Changed syntax for dereferncing objects to use <code>*</code> and <code>-&gt;</code>, similar to C.</li>
<li>Changed syntax for maps from e.g. <code>{1-&gt;2}</code> to <code>{1=&gt;2}</code>.  This is to avoid conflict with <code>-&gt;</code> for dereferencing.</li>
<li>Changed syntax for processes to syntax for references.  So, e.g. type <code>process {int x}</code> becomes <code>ref {int x}</code>.  The motivation for this choice is simply to avoid confusion with OS processes.</li>
<li>Changed Wyil bytecode to support various &#8220;effective&#8221; types (e.g. sets, lists, dictionaries).  These enable more efficient operation across a union of similarly kinded types.  For example, we can index into a list of type [int]|[bool] without having to coerce into a list of type <code>[int|bool]</code>.</li>
<li>Worked hard to get constraints being expanded properly again.  This is working pretty well now for the most part.</li>
<li>Added support for Maven compilation (thanks Lee).</li>
</ul>
<center>
<div class="wpfilebase-attachment">
 <div class="wpfilebase-rightcol">
  <div class="wpfilebase-filetitle">
   <a href="http://whiley.org/download/wdk/wdk-src-v0.3.13.tgz" title="Download wdk-src-v0.3.13">wdk-src-v0.3.13</a><br />
      Created on January 24, 2012.  100 Downloads, 2.3 MB.<br/>
      BSD License
  </div>
 </div>
 <div style="clear: both;"></div>
</div></center>
<center>
<div class="wpfilebase-attachment">
 <div class="wpfilebase-rightcol">
  <div class="wpfilebase-filetitle">
   <a href="http://whiley.org/download/jar/wyjc-v0.3.13.jar" title="Download wyjc-v0.3.13">wyjc-v0.3.13</a><br />
      Created on January 24, 2012.  110 Downloads, 1.1 MB.<br/>
      BSD License
  </div>
 </div>
 <div style="clear: both;"></div>
</div></center>
<center>
<div class="wpfilebase-attachment">
 <div class="wpfilebase-rightcol">
  <div class="wpfilebase-filetitle">
   <a href="http://whiley.org/download/bench/wybench-v0.1.6.tgz" title="Download wybench-v0.1.6">wybench-v0.1.6</a><br />
      Created on January 24, 2012.  95 Downloads, 5.8 MB.<br/>
      BSD License
  </div>
 </div>
 <div style="clear: both;"></div>
</div></center>
]]></content:encoded>
			<wfw:commentRss>http://whiley.org/2012/01/24/whiley-v0-3-13-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Connecting the Dots on the Future of Programming Languages</title>
		<link>http://whiley.org/2012/01/18/connecting-the-dots-on-the-future-of-programming-languages/</link>
		<comments>http://whiley.org/2012/01/18/connecting-the-dots-on-the-future-of-programming-languages/#comments</comments>
		<pubDate>Wed, 18 Jan 2012 01:15:24 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Interest]]></category>
		<category><![CDATA[Concurrency]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Video]]></category>

		<guid isPermaLink="false">http://whiley.org/?p=3505</guid>
		<description><![CDATA[<p>Yesterday, I serendipitously came across two things which got me thinking about the future of programming languages:</p> The first was an excellent article entitled &#8220;Welcome to the Hardware Jungle&#8221; by Herb Sutter. This article is about the coming advent our multicore overlords. Whilst this might sound like something you&#8217;ve heard before, it&#8217;s actually well <span style="color:#777"> . . . &#8594; Read More: <a href="http://whiley.org/2012/01/18/connecting-the-dots-on-the-future-of-programming-languages/">Connecting the Dots on the Future of Programming Languages</a></span>]]></description>
			<content:encoded><![CDATA[<p>Yesterday, I serendipitously came across two things which got me thinking about the future of programming languages:</p>
<ol>
<li>The first was an excellent article entitled <a href="http://herbsutter.com/welcome-to-the-jungle/">&#8220;Welcome to the Hardware Jungle&#8221;</a> by Herb Sutter.  This article is about the coming advent our multicore overlords.  Whilst this might sound like something you&#8217;ve heard before, it&#8217;s actually well worth the read.  His argument is that heterogeneous massively-multicore computing is fast becoming the norm, and there is no turning back.  I found the article quite scary, as I can&#8217;t imagine programming in the extreme environment suggested.  I also have to question whether everyday applications will really benefit from massive multicore.  But, clearly, I can see that quite a few will.</li>
<li>The second was the following (short) youtube video of <a href="http://en.wikipedia.org/wiki/Simon Peyton Jones" target="_top" >Simon Peyton Jones</a> and <a href="http://en.wikipedia.org/wiki/Erik Meijer" target="_top" >Erik Meijer</a> discussing the space of programming languages:
<p><center><iframe width="420" height="315" src="http://www.youtube.com/embed/iSmkqocn0oQ?feature=player_embedded" frameborder="0" allowfullscreen></iframe></center></p>
<p> Simon talks about how both imperative and functional programming languages are trying to reach some kind of &#8220;Nirvana&#8221;, but coming at it from different directions.</li>
</ol>
<p>Now, the question is: <em>how do they connect together?</em> Well, essentially, I think the &#8220;Nirvana&#8221; space the Simon talks about is exactly the space we need to deal with the Hardware Jungle.  In other words, I think it&#8217;s the space most suitable for distributed and parallel computing.</p>
<p><em>What do we know about this “Nirvana” space?</em> In the video, Simon talks about <em>safe</em> and <em>unsafe</em> languages. He says explicitly that safe means having limited or highly controlled <a href="http://en.wikipedia.org/wiki/Side_effect_%28computer_science%29">side-effects</a>. Languages which are unsafe by his categorisation include Java, C#, C, and the majority of languages traditionally thought of as imperative or object oriented. They are unsafe because one cannot reason about the result of any given method in isolation from others. That is, methods may read/write shared state (via the heap) and, to reason about them, we must know: (1) what shared state is actually accessed; (2) what value it has when it is accessed. These three requirements make it very difficult know what’s going on, particularly if something else could modify the state at any point. More importantly, I believe, <em>is that these requirements make it very difficult for the compiler to reason about what’s going on</em>.</p>
<p><em>What does this have to do with the Hardware Jungle?</em>  Well, I believe there are two important points here:</p>
<ol>
<li>To move the right data for a given computation onto the right core, we must know exactly what state that computation will access.</li>
<li>For massively multi-core systems, Humans will not be capable of optimally mapping resources onto cores.  We will increasingly rely on sophisticated algorithms to do this for us.  Typically, such algorithms will be embedded in the compiler and/or runtime system.</li>
</ol>
<p>These two points taken together imply it must be possible to automatically determine what state a given computation will access.  <em>The easiest way of doing this is to aggressively restrict the state that can be accessed by requiring <a href="http://en.wikipedia.org/wiki/Functional_programming#Pure_functions">pure functions</a></em>.  Furthermore, I believe it is not sufficient to rely on the programmer to ensure his/her functions are pure &#8212; the compiler must do this for us.  This is because race-conditions are already notoriously difficult to debug <em>and in the Hardware Jungle things will get seriously crazy</em>.</p>
<p>This leads me to the final and, I think, most important question:</p>
<blockquote><p>
Which mainstream programming languages currently support pure functions and/or other mechanisms for aggressively limiting side-effects?
</p></blockquote>
<p><a href="http://en.wikipedia.org/wiki/Haskell (programming language)" target="_top" >Haskell</a> is clearly one example, <a href="http://en.wikipedia.org/wiki/D (programming language)" target="_top" >D</a> is another.  <em>But, what else?</em>  </p>
]]></content:encoded>
			<wfw:commentRss>http://whiley.org/2012/01/18/connecting-the-dots-on-the-future-of-programming-languages/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>Three Rules for Programming Language Syntax?</title>
		<link>http://whiley.org/2012/01/11/three-rules-for-programming-language-syntax/</link>
		<comments>http://whiley.org/2012/01/11/three-rules-for-programming-language-syntax/#comments</comments>
		<pubDate>Wed, 11 Jan 2012 00:49:04 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Other]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[SmallTalk]]></category>
		<category><![CDATA[Syntax]]></category>

		<guid isPermaLink="false">http://whiley.org/?p=3455</guid>
		<description><![CDATA[<p>I&#8217;m always pondering the question: what makes good programming language syntax? One thing occuring to me is that many languages often ignore the HCI aspect.  For me, it&#8217;s a given that the purpose of a programming language is to simplify the programmer&#8217;s life, not the other way around.</p> <p>So, I thought of a few <span style="color:#777"> . . . &#8594; Read More: <a href="http://whiley.org/2012/01/11/three-rules-for-programming-language-syntax/">Three Rules for Programming Language Syntax?</a></span>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m always pondering the question: <em>what makes good programming language syntax?</em> One thing occuring to me is that many languages often ignore the <a href="http://en.wikipedia.org/wiki/Human-computer interaction" target="_top" >HCI</a> aspect.  For me, it&#8217;s a given that <em>the purpose of a programming language is to simplify the programmer&#8217;s life, not the other way around</em>.</p>
<p>So, I thought of a few simple rules:</p>
<ol>
<li><strong>Syntax should explain.</strong> The purpose of syntax is to help explain a program to a human.  In particular, structure in the program should be made explicit through syntax.</li>
<li><strong>Syntax should be concise.</strong> The converse to (1) is that syntax should always add to the explanation.  Syntax which does not add value simply obscures the explanation.</li>
<li><strong>Syntax should conform.</strong> The are many things people learn from everyday life, and we should not force them to unlearn these things.  Doing so can confuse the explanation, especially for non-experts.</li>
</ol>
<p>I&#8217;m sure there are plenty of others you can think of.  I pick on these because I see them being broken constantly.  Let&#8217;s illustrate with some of my pet peeves:</p>
<h3>Colons in Type Declarations?</h3>
<p>There are plenty of programming languages which use colons in type declarations (<a href="http://en.wikipedia.org/wiki/ML (programming language)" target="_top" >ML is a classic example</a>).  It goes something like this:</p>
<pre class="brush: whiley;">
fun f (0 : int) : int = ...
</pre>
<p>For me, this breaks rule (2) because those colons do not add value.  The C-family of languages is evidence that we can happily live without them.  That&#8217;s not to say that C-like declaration syntax is the &#8220;one true way&#8221;; only that it demonstrates we don&#8217;t need those colons!</p>
<h3>Function Call Braces: To Be or Not To Be?</h3>
<p>Some programming languages require braces around the arguments to a function call, whilst others (e.g. <a href="http://en.wikipedia.org/wiki/Haskell (programming language)" target="_top" >Haskell</a>) don&#8217;t.  Consider this:</p>
<pre class="brush: whiley;">

(f (g h) 1 2) y 3
</pre>
<p>What can you tell about the invocation structure from this line? <span style="text-decoration: line-through;"> Not much, unless you happen to know from memory exactly what arguments each function takes.</span> This violates rule (3) since functions are normally expressed with braces in mathematics.</p>
<p>Now, how about this:</p>
<pre class="brush: whiley;">

f(g(h),1,2)(y,3)
</pre>
<p>Personally, I prefer this style.  However, it would be fair to say that it violates rule (2) since the comma&#8217;s are not strictly necessary.  I suppose, in fact, we have a contradiction between (2) and (3) in this case, since commas are generally used to deliniate lists in written English.</p>
<h3>Why Don&#8217;t we Teach Polish Notation at School?</h3>
<p><em>(answer: because we don&#8217;t)</em></p>
<p>Some programming languages require mathematical expressions be expressed in <a href="http://en.wikipedia.org/wiki/Polish notation" target="_top" >Polish notation</a> (a.k.a. prefix notation).  For example, in <a href="http://en.wikipedia.org/wiki/Lisp (programming language)" target="_top" >Lisp</a>, we write <code>(+ 1 2)</code> to mean <code>1+2</code>. This violates rule (3), since most people have learned mathematics at school where the usual convention applies.</p>
<p>Similarly in <a href="http://en.wikipedia.org/wiki/Smalltalk" target="_top" >SmallTalk</a>, the expression <code>2+3*4</code> is equivalent to <code>(2+3)*4</code> rather than <code>2+(3*4)</code>, as might be expected.  Again, this violates rule (3) and is particularly insidious because it&#8217;s rather subtle.</p>
<p>The point here is not to say that e.g. one notation is <em>fundamentally better</em> than the other.  Just that, we learn one in school, so we should stick with that.</p>
<h2>Conclusion</h2>
<p>Well, if you made it this far, great!  Hopefully there was some food for thought, and maybe you can suggest some other examples&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://whiley.org/2012/01/11/three-rules-for-programming-language-syntax/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>Merchants of Doubt</title>
		<link>http://whiley.org/2011/12/27/merchants-of-doubt/</link>
		<comments>http://whiley.org/2011/12/27/merchants-of-doubt/#comments</comments>
		<pubDate>Tue, 27 Dec 2011 00:29:57 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Book Reviews]]></category>
		<category><![CDATA[Books]]></category>
		<category><![CDATA[Econmomics]]></category>

		<guid isPermaLink="false">http://whiley.org/?p=3414</guid>
		<description><![CDATA[<p style="text-align: center;"></p> <p>I&#8217;ve just finished reading this book, which I have to say was really good.  The book is about how a handful of rogue scientists deliberately spread disinformation on a range of key issues, including tobacco, acid rain, the ozone hole and climate change.  Their key strategy was to argue the science <span style="color:#777"> . . . &#8594; Read More: <a href="http://whiley.org/2011/12/27/merchants-of-doubt/">Merchants of Doubt</a></span>]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><img class="aligncenter" style="border: 0pt none;" title="Merchants of Doubt" src="http://www.desmogblog.com/sites/beta.desmogblog.com/files/blogimages/Merchants%20of%20Doubt.jpg" alt="" width="300" height="300" /></p>
<p>I&#8217;ve just finished reading this book, which I have to say was really good.  The book is about how a handful of rogue scientists deliberately spread disinformation on a range of key issues, including tobacco, acid rain, the ozone hole and climate change.  Their key strategy was to argue the science was inconclusive (even when it really wasn&#8217;t), that the scientists involved had some ulterior motive and, of course, to make such claims without evidence.  I found the stories both disturbing and eye-opening, especially with the underhand tactics being employed.  The key protaganists are <a href="http://en.wikipedia.org/wiki/Fred Singer" target="_top" >Fred Singer</a>, <a href="http://en.wikipedia.org/wiki/Frederick Seitz" target="_top" >Frederick Seitz</a>, <a href="http://en.wikipedia.org/wiki/William Nierenberg" target="_top" >William Nierenberg</a>, and many others.  These people have a lot to answer for.</p>
<p>Perhaps the most interesting question is: <em>why?</em> Why do former scientists (who made good contributions in their field) go on the warpath to undermine science itself?  As a scientist myself, it seems unbelievable.  However, I think the book hits the nail on the head: they are <a href="http://en.wikipedia.org/wiki/Market fundamentalism" target="_top" >free-market fundamentalists</a>.  This view holds that the so-called <a href="http://en.wikipedia.org/wiki/Laissez faire" target="_top" >Laissez faire</a> (i.e. do nothing) approach to regulation is always the right approach.  Alan Greenspan is perhaps the most notable free-market fundamentalist of recent times &#8230; and look where that ended up!</p>
<p>Anyhow, definitely worth a read.</p>
]]></content:encoded>
			<wfw:commentRss>http://whiley.org/2011/12/27/merchants-of-doubt/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Whiley Talk at Wellington JUG (VIDEO)</title>
		<link>http://whiley.org/2011/12/22/whiley-talk-wellington-ju/</link>
		<comments>http://whiley.org/2011/12/22/whiley-talk-wellington-ju/#comments</comments>
		<pubDate>Thu, 22 Dec 2011 00:03:23 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Examples]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Syntax]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Video]]></category>

		<guid isPermaLink="false">http://whiley.org/?p=3407</guid>
		<description><![CDATA[<p>Last month, the Wellington Java User Group was kind enough to invite me to give a talk on Whiley.  The talk is a general introduction to Whiley, including the syntax, some issues related to implementation and inter-operation with Java.  The talk was video and, finally, after some faffing around I&#8217;ve uploaded it onto YouTube <span style="color:#777"> . . . &#8594; Read More: <a href="http://whiley.org/2011/12/22/whiley-talk-wellington-ju/">Whiley Talk at Wellington JUG (VIDEO)</a></span>]]></description>
			<content:encoded><![CDATA[<p>Last month, the Wellington Java User Group was kind enough to invite me to give a talk on Whiley.  The talk is a general introduction to Whiley, including the syntax, some issues related to implementation and inter-operation with Java.  The talk was video and, finally, after some faffing around I&#8217;ve uploaded it onto YouTube here:<br />
<center><iframe width="560" height="315" src="http://www.youtube.com/embed/t-9epw8nhXU" frameborder="0" allowfullscreen></iframe></center><br />
Anyway, enjoy!</p>
<p><strong>UPDATE:</strong> Thanks to Evan for suggesting upload the slides as well &#8212; <a href="http://whiley.org/wp-content/uploads/2011/12/JUG2011-Whiley.pdf">you can get them here</a>!</p>
]]></content:encoded>
			<wfw:commentRss>http://whiley.org/2011/12/22/whiley-talk-wellington-ju/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Efficient Value Semantics for Whiley</title>
		<link>http://whiley.org/2011/12/13/efficient-value-semantics-for-whiley/</link>
		<comments>http://whiley.org/2011/12/13/efficient-value-semantics-for-whiley/#comments</comments>
		<pubDate>Tue, 13 Dec 2011 00:34:44 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Implementation]]></category>
		<category><![CDATA[Value Semantics]]></category>
		<category><![CDATA[Wyjc]]></category>

		<guid isPermaLink="false">http://whiley.org/?p=3354</guid>
		<description><![CDATA[<p>The latest release of the Whiley compiler (v0.3.12) includes an optimisation for passing compound structures (e.g. lists, sets and records) by value.  This is really important because all compound structures in Whiley have value semantics, meaning they are always passed by value.  In fact, Whiley does not support references or pointers as found in <span style="color:#777"> . . . &#8594; Read More: <a href="http://whiley.org/2011/12/13/efficient-value-semantics-for-whiley/">Efficient Value Semantics for Whiley</a></span>]]></description>
			<content:encoded><![CDATA[<p>The latest release of the Whiley compiler (v0.3.12) includes an optimisation for passing compound structures (e.g. lists, sets and records) by value.  This is really important because all compound structures in Whiley have <em>value semantics</em>, meaning they are always passed by value.  In fact, Whiley does not support references or pointers as found in other languages (e.g. Java, C, etc).  This means Whiley is really more of a <a href="http://en.wikipedia.org/wiki/Functional programming" target="_top" >functional programming language</a> than an <a href="http://en.wikipedia.org/wiki/Imperative programming" target="_top" >imperative language</a>.  As we&#8217;ll see, compared to other functional languages, Whiley is a little unusual as it supports <em>imperative updates</em> to compound structures.</p>
<h2>An Example</h2>
<p><em>So, what does this all mean?</em> Well, let&#8217;s have a look at an examples:</p>
<pre class="brush: whiley;">
[real] normalise([real] data, real max):
    for i in 0..|data|:
        data[i] = data[i] / max
    return data

void ::main(System sys):
    original = [1.2,2.3,4.5]
    normalised = normalise(original,0.5)
    sys.out.println(original)
    sys.out.println(normalised)
</pre>
<p>This function updates a list of real values in-place using a typical (imperative) list assignment of the form <code>data[i] = ...</code>.  In Java, where variables of array type are references to array objects, this would simultaneously update the callers version of this array as well.  In Whiley, however, this is not the case.  The list assignment inside <code>normalise()</code> does not modify the <code>original</code> list in <code>main()</code>.  This is because the list is <em>passed-by-value</em> in the true sense of the word.  You can think of this as meaning that <em>the whole list is copied when the normalise function is called</em> to prevent any interference between caller and callee.</p>
<h2>(In)efficiency?</h2>
<p><em>Ok, but that all that copying must be crazily inefficient! </em>Well, not so.  This is because, whilst the semantics of Whiley dictate pass-by-value for compound structures, the underlying implementation actually passes them by reference.  Furthermore, it clones them lazily with reference counting being employed to reduce the situations where a clone is actually required.  Before explaining how the reference counting works, let&#8217;s look at some actual data first:</p>
<table>
<tbody>
<tr>
<th>Name</th>
<th>Description</th>
<th>LOC</th>
<th># Clones</th>
<th>%</th>
<th>Time</th>
</tr>
<tr>
<td>Jasm</td>
<td>A Java bytecode disassembler.</td>
<td>2333</td>
<td>12878 / 29968</td>
<td>43.0%</td>
<td>2.1s</td>
</tr>
<tr>
<td>Gunzip</td>
<td>An implementation of DEFLATE.</td>
<td>815</td>
<td>873 / 140561</td>
<td>0.62%</td>
<td>1.6s</td>
</tr>
<tr>
<td>Chess</td>
<td>Validates chess games in short algebraic notation.</td>
<td>784</td>
<td>6438 / 416116</td>
<td>1.6%</td>
<td>1.4s</td>
</tr>
<tr>
<td>Calculator</td>
<td>An arithmetic expression evaluator.</td>
<td>225</td>
<td>0 / 81527</td>
<td>0.0%</td>
<td>8.8s</td>
</tr>
</tbody>
</table>
<p><em>What is this data showing us?</em> Well, &#8220;# Clones&#8221; shows the number of clones actually performed versus the number that a naive implementation of value semantics would have performed.   The naive version clones whenever an argument is passed or returned from a function, whenever a list element is assigned, etc.  It represents an upper bound on the number of clones required.  So, for example, looking at the Gunzip benchmark we see that only 873 clones were required out of a maximum of 140561.  This indicates that, in the vast majority of cases, value semantics does not actually result in many extra clones.</p>
<p><em>But the results for Jasm look quite bad?</em> Well, yes.  But, actually, they&#8217;re not too bad if we consider the cause.  Essentially, the Jasm benchmark consists of an inner loop for decoding bytecodes.  Roughly speaking, it looks like this:</p>
<pre class="brush: whiley;">
([Bytecode],int) decode([byte] data, int pos):
    opcode = Byte.toUnsignedInt(data[pos])
    kind,fmt,type = decodeTable[opcode]
    switch fmt:
        case FMT_EMPTY,
            ....
        case FMT_I8:
            idx = Byte.toUnsignedInt(data[pos+1..pos+2])
            return {offset: pos-14, op: opcode},pos+2
        case FMT_I16:
            idx = Byte.toUnsignedInt(data[pos+3..pos+1])
            return {offset: pos-14, op: opcode},pos+3
        case FMT_VARIDX:
            index = Byte.toUnsignedInt(data[pos+1..pos+2])
            return VarIndex(pos-14, opcode, index),pos+2
        ...
</pre>
<p>What we see is that the loop first reads the opcode byte and then, based on this, reads zero or more additional bytes  (e.g. <code>data[pos+1..pos+2]</code>).  This sublist operation is the root cause of almost every  clone reported in the above table for Jasm.  In fact, it&#8217;s not a clone of the entire <code>data</code> list; rather, it just copies those few bytes being read into a new list &#8212; meaning it&#8217;s not really very expensive.</p>
<h2>Implementation</h2>
<p><em>So, how does the reference counting work?</em> One of the key optimisations is knowledge of when a variable is no longer <em>live</em>.  For example:</p>
<pre class="brush: whiley;">
[int] f([int] xs):
   ys = xs // ref count not increased
   ys[0] = 1
   return ys
</pre>
<p>Here, the variable <code>xs</code> is dead after the assignment to <code>ys</code>.  Therefore, we don&#8217;t need to increase the reference count of the object it refers to as, effectively, ownership is transferred from <code>xs</code> to <code>ys</code>.  If the reference count of <code>xs</code> on entry was 1, then it will still be 1 at the assignment <code>ys[0] = 1</code> and, hence, the assignment can be performed in place.  Now, let&#8217;s consider a more detailed example:</p>
<pre class="brush: whiley;">
  [int] f([int] z):
      z[0] = 1 // Object #2 created
      return z

  [int] g():
      x = [1,2,3] // Object #1 created
      y = f(x)
      return x + y
</pre>
<p>On Line 6,  Object #1 (which represents the constructed list) has an initial reference count of one.  This does not change when it is assigned to <code>x</code>.  Its reference count is then increased by one on Line 7, as <code>x</code> is used in the invocation expression and <em>remains live afterward</em>.  On entry to <code>f()</code>, parameter <code>z</code> refers to Object #1, which now has reference count two. Therefore, the list assignment on Line 2 creates an entirely new object before updating it.  It also decrements the reference count of Object #1.  On Line 8, <code>x</code> still refers to Object #1 (now with reference count one) and, hence, the append is performed in place without cloning.</p>
<h2>Conclusion</h2>
<p>Reference counting is a critical optimisation to improving the performance of Whiley programs.  The latest version of the compiler (finally) supports this, although there is still room for considerable improvement.  In fact, a good starting point is the following paper:</p>
<ul>
<li>Staged Static Techniques to Efficiently Implement Array Copy Semantics in a MATLAB JIT Compiler, N. Lameed and L. Hendren.  In <em>Proceedings of the Conference on Compiler Construction</em>, 2011. [<a href="http://www.sable.mcgill.ca/mclab/mcvm/mcvmcc2011.pdf">PDF</a>]</li>
</ul>
<p>Anyway, that&#8217;s all folks!</p>
]]></content:encoded>
			<wfw:commentRss>http://whiley.org/2011/12/13/efficient-value-semantics-for-whiley/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Final should be Default for Classes in Java</title>
		<link>http://whiley.org/2011/12/06/final-should-be-default-for-classes-in-java/</link>
		<comments>http://whiley.org/2011/12/06/final-should-be-default-for-classes-in-java/#comments</comments>
		<pubDate>Tue, 06 Dec 2011 03:12:38 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Other]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://whiley.org/?p=3331</guid>
		<description><![CDATA[<p>We were having an interesting discussion the other day, and the issue of final classes came up.  For some reason, it suddenly occurred to me that all classes should be final by default. That is, classes should be implicitly final, rather than requiring an explicit declaration.  For example, the following should be considered invalid <span style="color:#777"> . . . &#8594; Read More: <a href="http://whiley.org/2011/12/06/final-should-be-default-for-classes-in-java/">Final should be Default for Classes in Java</a></span>]]></description>
			<content:encoded><![CDATA[<p>We were having an interesting discussion the other day, and the issue of <code>final</code> classes came up.  For some reason, it suddenly occurred to me <em>that all classes should be final by default</em>. That is, classes should be <em>implicitly</em> final, rather than requiring an <em>explicit</em> declaration.  For example, the following should be considered invalid in my opinion:</p>
<pre class="brush: java;">
class Parent { ... }
class Child extends Parent {... } // invalid: parent implicitly final
</pre>
<p>In place of <code>final</code> we would have another modifier, say <code>open</code>.  This would allow us to extend classes like so:</p>
<pre class="brush: java;">
open class Parent { ... }
class Child extends Parent { ... } // valid: parent explicitly open
</pre>
<p>Now, the question is: <em>why do I think final should be the default?</em> It&#8217;s got nothing to do with performance.  The following quotes from Josh Bloch&#8217;s excellent talk on API design (see [<a href="http://www.youtube.com/watch?v=aAb7hSCtvGw">1</a>][<a href="http://www.infoq.com/presentations/effective-api-design">2</a>][<a href="http://www.javaworld.com/javaworld/jw-01-2002/jw-0104-bloch.html">3</a>]) gives us a clue:</p>
<blockquote><p>&#8220;When in doubt, leave it out&#8221;</p>
<p>&#8220;You can always add, but you can never remove&#8221;</p></blockquote>
<p><em>What does this mean?</em> If you&#8217;re not sure whether a function should be included, then don&#8217;t include it.  That&#8217;s because, once you&#8217;ve included a function in a public API, people will depend upon it and you&#8217;ll have to maintain it.  If it&#8217;s badly designed, you&#8217;re stuck with it.  Sure, you can try and deprecate it &#8212; but you&#8217;ll probably end up keeping it forever anyway.</p>
<p><em>What has all this got to do with final classes?</em> Well, a non-final class can be extended of course!  Any <code>public</code> or <code>protected</code> methods can be overridden and <code>protected</code> fields read/written.   More importantly, you cannot reverse the decision &#8212; i.e. once a <code>public</code> non-final class, always a <code>public</code> non-final class.  In contrast, when using <code>final</code> as the default for classes, you can reverse your decision &#8212; i.e. you can always open, but you can never close.</p>
<p><em>This observation is hardly rocket science!</em>  Josh Bloch in his (also excellent) book <em>Effective Java</em> states<em> &#8220;you should make each class or member as inaccessible as possible&#8221;</em>.  For example, you should always prefer <code>private</code> to <code>protected</code> fields. For some reason though, he doesn&#8217;t extend this to include preferring <code>final</code> to non-<code>final</code> classes.</p>
<p>And, it looks like there are at least <a href="http://cafe.elharo.com/blogroll/final-good/">some like-minded people out there</a> and, of course, <a href="http://www.ibm.com/developerworks/java/library/j-jtp1029/index.html">there are those who think differently</a> &#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://whiley.org/2011/12/06/final-should-be-default-for-classes-in-java/feed/</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
		<item>
		<title>Whiley v0.3.12 Released!</title>
		<link>http://whiley.org/2011/12/02/whiley-v0-3-12-released/</link>
		<comments>http://whiley.org/2011/12/02/whiley-v0-3-12-released/#comments</comments>
		<pubDate>Fri, 02 Dec 2011 03:59:03 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Ant]]></category>
		<category><![CDATA[Implementation]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Wyclipse]]></category>
		<category><![CDATA[Wyjc]]></category>

		<guid isPermaLink="false">http://whiley.org/?p=3312</guid>
		<description><![CDATA[<p>Well, crikey, what a long time since the last release.  Things haven&#8217;t changed a whole lot, apart from various bug fixes.  Probably the most interesting update is the inclusion of reference counting of compound structures to enable in-place updates and prevent unnecessary cloning.  This leads to some nice performance improvements.  Quite a bit of <span style="color:#777"> . . . &#8594; Read More: <a href="http://whiley.org/2011/12/02/whiley-v0-3-12-released/">Whiley v0.3.12 Released!</a></span>]]></description>
			<content:encoded><![CDATA[<p>Well, crikey, what a long time since the last release.  Things haven&#8217;t changed a whole lot, apart from various bug fixes.  Probably the most interesting update is the inclusion of <em>reference counting</em> of compound structures to enable in-place updates and prevent unnecessary cloning.  This leads to some nice performance improvements.  Quite a bit of work has been done on the benchmarks as well, and also the eclipse plugin.</p>
<h2>ChangeLog</h2>
<ul>
<li>Added support for proper reference counting of compound structures.  This employs a live variables analysis to determine when a local variable is dead.</li>
<li>Reworked the name resolution and module loading system.  This was done primarily to support build tools such as ant and eclipse.</li>
<li>Improved the Ant task.</li>
<li>Further improved the runtime checking of constraints.</li>
<li>Added some rudimentary reporting of memory usage by the various compiler stages.</li>
</ul>
<center>
<div class="wpfilebase-attachment">
 <div class="wpfilebase-rightcol">
  <div class="wpfilebase-filetitle">
   <a href="http://whiley.org/download/wdk/wdk-src-v0.3.12.tgz" title="Download wdk-src-v0.3.12">wdk-src-v0.3.12</a><br />
      Created on December 2, 2011.  143 Downloads, 2.1 MB.<br/>
      BSD License
  </div>
 </div>
 <div style="clear: both;"></div>
</div></center>
<center>
<div class="wpfilebase-attachment">
 <div class="wpfilebase-rightcol">
  <div class="wpfilebase-filetitle">
   <a href="http://whiley.org/download/jar/wyjc-v0.3.12.jar" title="Download wyjc-v0.3.12.jar">wyjc-v0.3.12.jar</a><br />
      Created on December 2, 2011.  155 Downloads, 1.0 MB.<br/>
      BSD License
  </div>
 </div>
 <div style="clear: both;"></div>
</div></center>
<center>
<div class="wpfilebase-attachment">
 <div class="wpfilebase-rightcol">
  <div class="wpfilebase-filetitle">
   <a href="http://whiley.org/download/bench/wybench-v0.1.5.tgz" title="Download wybench-v0.1.5">wybench-v0.1.5</a><br />
      Created on December 2, 2011.  127 Downloads, 5.8 MB.<br/>
      BSD License
  </div>
 </div>
 <div style="clear: both;"></div>
</div></center>
]]></content:encoded>
			<wfw:commentRss>http://whiley.org/2011/12/02/whiley-v0-3-12-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Not all Tests are Passing &#8230; is that so Bad?</title>
		<link>http://whiley.org/2011/11/01/not-all-tests-are-passing-is-that-so-bad/</link>
		<comments>http://whiley.org/2011/11/01/not-all-tests-are-passing-is-that-so-bad/#comments</comments>
		<pubDate>Mon, 31 Oct 2011 20:11:30 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://whiley.org/?p=3302</guid>
		<description><![CDATA[<p>For the Whiley compiler, I currently have over 500 end-end tests and in excess of 15,000 unit tests (most of which were auto-generated and target the type system).  Each end-end test is a short Whiley program that is  categorised as either valid or invalid.  Valid tests also include sample output and are expected to <span style="color:#777"> . . . &#8594; Read More: <a href="http://whiley.org/2011/11/01/not-all-tests-are-passing-is-that-so-bad/">Not all Tests are Passing &#8230; is that so Bad?</a></span>]]></description>
			<content:encoded><![CDATA[<p>For the Whiley compiler, I currently have over 500 end-end tests and in excess of 15,000 unit tests (most of which were auto-generated and target the type system).  Each end-end test is a short Whiley program that is  categorised as either <em>valid</em> or <em>invalid</em>.  Valid tests also include sample output and are expected to compile, execute and produce matching output.  The invalid tests are expected to generate a compile-time error.  To make this more precise, I categorise the kinds of error that the compiler can produce as:<em> internal failure</em>, <em>syntax error</em> or <em>verification error</em>.  Every invalid test is specified to raise either a syntax error or a verification error, and the test will not pass if the compiler does anything else.  Thus, an invalid test which should produce a syntax error will not pass if the compiler barfs out a <code>NullPointerException</code>.</p>
<p>Now, the thing is: <em>not all my end-end tests are currently passing</em>.  In fact, I don&#8217;t think it&#8217;s <em>ever been the case that all of the tests have passed</em>.  That may seem like a bad thing, but I think there are some mitigating circumstances:</p>
<ol>
<li>My test suite is constantly growing.  As soon as I spot an error, or think of a possible problem, I add a test.  Adding tests makes me feel good, and I love it!  That&#8217;s because a test is a piece of knowledge <em>locked-in</em>.  Once the test is written and checked in, <em>I can&#8217;t forget</em>.</li>
<li>Some failures represent issues that I&#8217;ve given very low priority to.  Eventually, I will get to them &#8230; but not yet.  I do sometimes make use of the <code>@Ignore</code> annotation for these kinds of tests.</li>
<li>Some failures represent significant design flaws in the system.  They should be high-priority, but represent weeks or months of refactoring and redesigning to fix.  I don&#8217;t like to <code>@Ignore</code> these ones &#8230; I prefer to feel the pain.</li>
</ol>
<p>In a way, my test suite is like a queue.  My tests never all pass because by the time I fixed all the ones that are failing &#8230; <em>I&#8217;ve added some more</em>!</p>
<p>This is similar to <a href="http://en.wikipedia.org/wiki/Test-driven development" target="_top" >Test-driven development</a> I suppose.  What I don&#8217;t like about TDD though, is that you&#8217;re supposed to write a test and then immediately make it pass.  That doesn&#8217;t work for me since,  in a few seconds, I can write a test that might takes months of careful refactoring to solve.   I&#8217;m never going to fix those ones immediately &#8230; they need serious <a href="http://blip.tv/clojure/hammock-driven-development-4475586">hammock time</a>!</p>
]]></content:encoded>
			<wfw:commentRss>http://whiley.org/2011/11/01/not-all-tests-are-passing-is-that-so-bad/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Whiley v0.3.11 Released!</title>
		<link>http://whiley.org/2011/10/28/whiley-v0-3-11-released/</link>
		<comments>http://whiley.org/2011/10/28/whiley-v0-3-11-released/#comments</comments>
		<pubDate>Fri, 28 Oct 2011 00:40:09 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Implementation]]></category>
		<category><![CDATA[Syntax]]></category>
		<category><![CDATA[Wyjc]]></category>

		<guid isPermaLink="false">http://whiley.org/?p=3224</guid>
		<description><![CDATA[<p>As usual, it&#8217;s been a surprising amount of effort &#8230; but the next release of Whiley is available!  It&#8217;s been quite a long time since the last update, but then quite a lot has improved.  Unfortunately, I did find a fairly serious problem with my type system, which means I&#8217;ve got to go back <span style="color:#777"> . . . &#8594; Read More: <a href="http://whiley.org/2011/10/28/whiley-v0-3-11-released/">Whiley v0.3.11 Released!</a></span>]]></description>
			<content:encoded><![CDATA[<p>As usual, it&#8217;s been a surprising amount of effort &#8230; but the next release of Whiley is available!  It&#8217;s been quite a long time since the last update, but then quite a lot has improved.  Unfortunately, I did find a fairly serious problem with my type system, which means I&#8217;ve got to go back to the drawing board and figure out how to fix it.</p>
<h2>ChangeLog</h2>
<ul>
<li>Fixed support for line numbers in classfiles (this required adding dead-code elimination phase at the WYIL level)</li>
<li>Added a generic class for handling error messages.  The aim here is improve the quality and consistency of error message reporting, by providing a central class which is responsible for this.  However, this is in its infancy and a lot remains to be done here.</li>
<li>Improved Compiler Documentation.</li>
<li>Added support for parsing and checking throws clauses.  This require extending the function and method types to include another slot for the throws clause, which behaves much like that for return values.</li>
<li>Added support for checking that catch handlers are not dead-code.  That is, every catch handler must be able to catch something in the try-catch block.</li>
<li>Added support for the <code>native</code> modifier on functions/methods.  This indicates that the given method is implemented as a native Java method.  Under-the-hood, such methods are redirected into a class of the same package and name (but with &#8220;$native&#8221; appended to the name).</li>
<li>Added support for the <code>export</code> modifier on functions/methods.  This tells the compiler that the function or method in question is intended to be called by some external (i.e. native Java) code.  Thus, the compiler omits the usual name mangling it would apply.</li>
<li>Added support for passing modifiers (including native and public) through into the WYIL (which was surprisingly absent).</li>
<li>Changed the way cases of a switch statement work.  Now, you can no longer &#8220;<a href="http://whiley.org/2011/10/26/fall-through-by-default-switch-statements/">fall-through by default</a>&#8220;.</li>
<li>Removed the implicit coercion from <code>char</code> to <code>int</code>, which really didn&#8217;t make sense.  You can still force this coercion using an explicit cast.</li>
<li>Added some more methods to the core standard library.</li>
<li>Added support for testing the length of a dictionary.</li>
<li>Added <code>do</code> &#8211; <code>while</code> statement.</li>
<li>Fixed a lot of bugs!</li>
</ul>
<h2>Future Work</h2>
<ul>
<li>Fix the problem with type difference, where currently <code>[int] - [int] =&gt; [void]</code></li>
<li>Back propagation doesn&#8217;t work properly for try-catch blocks</li>
<li>Back propagation doesn&#8217;t work through pre-/post-conditions and loop invariants.</li>
<li>Many of the type constraints are not expanded properly.</li>
<li>Include support for e.g. <em>effective</em> list types, etc.</li>
<li>There is currently no way to determine if when a dictionary contains a particular key.</li>
</ul>
<center>
<div class="wpfilebase-attachment">
 <div class="wpfilebase-rightcol">
  <div class="wpfilebase-filetitle">
   <a href="http://whiley.org/download/wdk/wdk-src-v0.3.11.tgz" title="Download wdk-src-v0.3.11">wdk-src-v0.3.11</a><br />
      Created on October 28, 2011.  160 Downloads, 2.1 MB.<br/>
      BSD License
  </div>
 </div>
 <div style="clear: both;"></div>
</div></center>
<center>
<div class="wpfilebase-attachment">
 <div class="wpfilebase-rightcol">
  <div class="wpfilebase-filetitle">
   <a href="http://whiley.org/download/jar/wyjc-v0.3.11.jar" title="Download wyjc-v0.3.11.jar">wyjc-v0.3.11.jar</a><br />
      Created on October 28, 2011.  180 Downloads, 1.0 MB.<br/>
      BSD License
  </div>
 </div>
 <div style="clear: both;"></div>
</div></center>
]]></content:encoded>
			<wfw:commentRss>http://whiley.org/2011/10/28/whiley-v0-3-11-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fall-Through by Default for Switch Statements?</title>
		<link>http://whiley.org/2011/10/26/fall-through-by-default-switch-statements/</link>
		<comments>http://whiley.org/2011/10/26/fall-through-by-default-switch-statements/#comments</comments>
		<pubDate>Tue, 25 Oct 2011 20:41:14 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Syntax]]></category>

		<guid isPermaLink="false">http://whiley.org/?p=3255</guid>
		<description><![CDATA[<p>The switch statement has a long history, and most languages support it or something similar.  In my experience, I found it to be very useful &#8212; both for conciseness, and also improving performance.  With the recent release of Java 7, you can finally switch over strings.</p> <p>In Whiley, the syntax for switch statements currently <span style="color:#777"> . . . &#8594; Read More: <a href="http://whiley.org/2011/10/26/fall-through-by-default-switch-statements/">Fall-Through by Default for Switch Statements?</a></span>]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://en.wikipedia.org/wiki/Switch_statement">switch statement</a> has a long history, and most languages support it or something similar.  In my experience, I found it to be very useful &#8212; both for conciseness, and also improving performance.  With the recent release of Java 7, you can finally <a href="http://code.joejag.com/2009/new-language-features-in-java-7/">switch over strings</a>.</p>
<p>In Whiley, the syntax for switch statements currently looks like this:</p>
<pre class="brush: whiley;">
for token in modifiers:
    modifier = null
    switch token.id:
       case &quot;PUBLIC&quot;:
       case &quot;public&quot;:
          modifier = ACC_PUBLIC
          break
       case &quot;FINAL&quot;:
       case &quot;final&quot;:
          modifier = ACC_FINAL
          break
       ...
       default:
          throw SyntaxError(&quot;invalid modifier&quot;,token)
</pre>
<p>(This excerpt is based on my <a href="https://github.com/DavePearce/wybench/tree/master/sequential/macro/jasm">Java Assembler/Disassembler</a> benchmark)</p>
<p>Now, I&#8217;m having something of a dilemma over this syntax: <em>should I support fall-through case statements or not?</em></p>
<p>As you can see from above, my initial reaction was: <em>yes</em>.  But, I&#8217;m starting to think this is a really bad idea and there are a few reasons:</p>
<ol>
<li>Obviously, fall-through by default is dangerous as its easy to forget the <code>break</code>.  I&#8217;ve already done this a few times!</li>
<li>Generally, I think fall-through is the exception, not the rule.  That is,  most cases do require the <code>break</code> and, hence, fall-through by default causes additional verbosity.</li>
<li>The overloaded <code>break</code> statement can be annoying when you actually want to break out of a loop.</li>
</ol>
<p>Now, of course, I&#8217;m not the first to think along these lines (see e.g. <a href="http://c2.com/cgi/wiki?IsBreakStatementArchaic">[1]</a><a href="http://stackoverflow.com/questions/188461/switch-statement-fallthrough-should-it-be-allowed">[2]</a><a href="http://javascript.crockford.com/code.html">[3]</a>) and, in fact, I&#8217;m probably being a bit slow on the uptake here!</p>
<p>Several languages (e.g. <a href="http://en.wikipedia.org/wiki/Go_%28programming_language%29">Go</a>, <a href="http://jashkenas.github.com/coffee-script/#switch">CoffeeScript</a>, <a href="http://en.wikipedia.org/wiki/Switch_statement#Pascal">Pascal</a>) do not support fall-through by default. For example, Go supports an explicit <code>fallthrough</code> statement, which might look something like this in Whiley:</p>
<pre class="brush: whiley;">
for token in modifiers:
    modifier = null
    switch token.id:
       case &quot;PUBLIC&quot;:
          fallthrough
       case &quot;public&quot;:
          modifier = ACC_PUBLIC
       case &quot;FINAL&quot;:
          fallthrough
       case &quot;final&quot;:
          modifier = ACC_FINAL
       ...
       default:
          throw SyntaxError(&quot;invalid modifier&quot;,token)
</pre>
<p>Go is quite interesting as it provides <a href="http://golangtutorials.blogspot.com/2011/06/control-structures-go-switch-case.html">multi-match case statements</a> as well, which might look like something like this in Whiley:</p>
<pre class="brush: whiley;">
for token in modifiers:
    modifier = null
    switch token.id:
       case &quot;PUBLIC&quot;,&quot;public&quot;:
          modifier = ACC_PUBLIC
       case &quot;FINAL&quot;,&quot;final&quot;:
          modifier = ACC_FINAL
       ...
       default:
          throw SyntaxError(&quot;invalid modifier&quot;,token)
</pre>
<p>I really like this syntax (which I think is <a href="http://en.wikipedia.org/wiki/Switch_statement#Pascal">originally from Pascal)</a>.  In Pascal, you can provide ranges as well which certainly makes sense.</p>
<p>So, all things considered, I&#8217;m convinced it is a mistake to support fall-through by default in Whiley.  Fortunately, it&#8217;s not too late for me to correct this!</p>
<p>The <strong>big question</strong> then is: <em>do I support explicit fallthrough, multi-match cases or both?</em></p>
]]></content:encoded>
			<wfw:commentRss>http://whiley.org/2011/10/26/fall-through-by-default-switch-statements/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>What Kind of Revert are You?</title>
		<link>http://whiley.org/2011/10/19/what-kind-of-revert-are-you/</link>
		<comments>http://whiley.org/2011/10/19/what-kind-of-revert-are-you/#comments</comments>
		<pubDate>Tue, 18 Oct 2011 20:35:04 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Other]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://whiley.org/?p=3231</guid>
		<description><![CDATA[<p>Reverting is tough.  There&#8217;s no doubt about it!  I don&#8217;t mean tough as in technically challenging &#8212; no, version control systems make this easy!  I mean tough as in mentally challenging.  You&#8217;re faced with days or weeks of effort going down the drain, and you have to decide when to pull the plug.  Sure, <span style="color:#777"> . . . &#8594; Read More: <a href="http://whiley.org/2011/10/19/what-kind-of-revert-are-you/">What Kind of Revert are You?</a></span>]]></description>
			<content:encoded><![CDATA[<p>Reverting is tough.  There&#8217;s no doubt about it!  I don&#8217;t mean tough as in <em>technically challenging</em> &#8212; no, version control systems make this easy!  I mean tough as in <em>mentally challenging</em>.  You&#8217;re faced with days or weeks of effort going down the drain, and you have to decide when to pull the plug.  Sure, it&#8217;ll still be in your history somewhere.  But, you&#8217;ll probably never get it out again.  And, if you do, it won&#8217;t integrate with the latest version as this is constantly marching on.</p>
<p>At some point though, you know it&#8217;s time.  You made a bad call, and you need to undo that and get back on track. It seems there are few different way to end up in this situation:</p>
<ol>
<li><strong>The Obvious Disaster.</strong> Perhaps this is the most common case.  You have an idea and start pushing it through.  But, after a while, it becomes obvious <em>it will never work</em>.  If you&#8217;d thought a bit harder up front, you&#8217;d probably have realised this.  Normally, not too much time is wasted.</li>
<li><strong>The Winding Road.</strong> This one is ugly.  You&#8217;ve had an idea which, unbeknown to you, is fatally flawed.  The unfortunate thing is that the reason behind the flaw is very, very subtle.  It&#8217;s only after a fairly significant amount of effort that you (finally) realise: <em>this approach can&#8217;t work</em>.  This costs serious time, and is fustrating at best.  For me, I try not to lose to much sleep over these ones &#8212; they come with the territory.</li>
<li><strong>The Good Idea. </strong> This one is even uglier!  You&#8217;ve had an idea <em>which will actually work</em>.  The problem is, it&#8217;s going to take a significant amount of effort to push through.  Furthermore, the gains from the improvement maybe somewhat marginal.  You&#8217;re completely paralysed: undo the work so far and waste what is a good idea simply because it&#8217;s going to take weeks or months to finish; OR, stubbornly keep going and push the damn thing through anyway.  I hate these ones, since I don&#8217;t like knowly going backwards.  These ones I lose lots of sleep over.</li>
<li><strong>The Ping Pong</strong>.   This is where you revert, and then realise actually maybe you should have stuck with it.  So you undo the revert!  Then, maybe, you realise why you reverted in the first place and revert again, and so on.  I think experience makes you aware of this cycle, and helps you avoid it by forcing you to think carefully before reverting.</li>
</ol>
<p>So, the question right now as I stare at my code is: <em>what kind of revert are you?</em></p>
]]></content:encoded>
			<wfw:commentRss>http://whiley.org/2011/10/19/what-kind-of-revert-are-you/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Whiley Gets Funding!</title>
		<link>http://whiley.org/2011/10/12/whiley-gets-funding/</link>
		<comments>http://whiley.org/2011/10/12/whiley-gets-funding/#comments</comments>
		<pubDate>Tue, 11 Oct 2011 21:00:59 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Interest]]></category>

		<guid isPermaLink="false">http://whiley.org/?p=3207</guid>
		<description><![CDATA[<p>I&#8217;m pretty excited to announce that I have been awarded a prestigious Marsden Grant to support research on the Whiley project over the next three years.  This is obviously great news for Whiley, and will go a long way towards making the project a reality.  The following Dom Post article covered the recent announcement <span style="color:#777"> . . . &#8594; Read More: <a href="http://whiley.org/2011/10/12/whiley-gets-funding/">Whiley Gets Funding!</a></span>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m pretty excited to announce that I have been awarded a prestigious <a href="http://www.royalsociety.org.nz/programmes/funds/marsden/">Marsden Grant</a> to support research on the Whiley project over the next three years.  This is obviously great news for Whiley, and will go a long way towards making the project a reality.  The following Dom Post article covered the recent announcement of awards (although my project doesn&#8217;t get a specific mention):</p>
<p><a href="http://whiley.org/wp-content/uploads/2011/10/DomPostMarden.png"><img class="aligncenter size-full wp-image-3210" title="DomPostMarden" src="http://whiley.org/wp-content/uploads/2011/10/DomPostMarden.png" alt="" width="400" height="300" /></a></p>
<p>Some more information about the awarded grants is available <a href="http://www.royalsociety.org.nz/programmes/funds/marsden/awards/2011/">here</a> as well.</p>
]]></content:encoded>
			<wfw:commentRss>http://whiley.org/2011/10/12/whiley-gets-funding/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Whiley v0.3.10 Released!</title>
		<link>http://whiley.org/2011/09/21/whiley-v0-3-10-released/</link>
		<comments>http://whiley.org/2011/09/21/whiley-v0-3-10-released/#comments</comments>
		<pubDate>Wed, 21 Sep 2011 00:11:59 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Implementation]]></category>
		<category><![CDATA[Syntax]]></category>
		<category><![CDATA[Types]]></category>
		<category><![CDATA[Typing]]></category>
		<category><![CDATA[Wyjc]]></category>

		<guid isPermaLink="false">http://whiley.org/?p=3033</guid>
		<description><![CDATA[<p>Well, it&#8217;s been a tough slog.  But, finally, we have a new release of Whiley!!  The main thing that&#8217;s improved over the past few weeks is the underlying type implementation.  This was causing problems before, as programs which should type-check were failing and vice-versa.  To resolve this, the type system has been reimplemented from <span style="color:#777"> . . . &#8594; Read More: <a href="http://whiley.org/2011/09/21/whiley-v0-3-10-released/">Whiley v0.3.10 Released!</a></span>]]></description>
			<content:encoded><![CDATA[<p>Well, it&#8217;s been a tough slog.  But, finally, we have a new release of Whiley!!  The main thing that&#8217;s improved over the past few weeks is the underlying type implementation.  This was causing problems before, as programs which should type-check were failing and vice-versa.  To resolve this, the type system has been reimplemented from scratch and (hopefully) it should be significantly better.  In particular, I&#8217;ve done quite a lot more testing (e.g. I now have around 15,000 individual unit tests for the <code>Type</code> class).</p>
<p>The other main difference is the way in which <code>import</code> statements are handled.  Whilst this is definitely a step in the right direction, it&#8217;s not without some outstanding issues &#8212; which I discussed in more detail <a href="http://whiley.org/2011/09/03/namespaces-in-whiley/">here</a>.</p>
<h2>ChangeLog</h2>
<ul>
<li>Changed the way imports are interpreted.  Previously, <code>import whiley.lang.String</code> would have imported all symbols from module <code>String</code>.  Now, it only imports the module name.  So, for example, to access the method <code>indexOf</code>, you need to write <code>String.indexOf(s,i)</code>.  However, you can still replicate the old approach by directly importing the names from a module.  For example, <code>import * from whiley.lang.String</code> imports all names from the <code>String</code> module</li>
<li>The entry point <code>main()</code> no longer has <code>System</code> as its receiver.  Instead, <code>System</code> is a parameter and <code>main</code> is a headless method.  This is necessary as, otherwise, the <code>System</code> process is blocked until main completes!</li>
<li>Split out the type system into a separate library called wyautl.  As part of this, I&#8217;ve essentially reimplemented the type system from scratch in an effort to really harden it up.  I&#8217;ve done a significant amount of testing, and it&#8217;s definitely a lot better.  The type system now directly supports <em>negation</em> and <em>intersection</em> types, which makes for some interesting possibilities.</li>
<li>Support for dictionaries is somewhat improved.  In particular, you can now create an empty dictionary!  An empty dictionary is denoted <code>{-&gt;}</code> (contrasting with <code>{}</code> for sets and <code>[]</code> for lists).</li>
<li>Support for try-catch blocks has been added (finally).  This means you can now catch exceptions in Whiley.  At the moment, however, the system does not check that you properly declare your exceptions &#8230; but that will come shortly.</li>
<li>Improved the JavaDoc documentation for the compiler.  I plan to make this documentation visible soon, in conjunction with detailed discussion of how the compiler actually works.</li>
<li>Various bug fixes.</li>
</ul>
<center>
<div class="wpfilebase-attachment">
 <div class="wpfilebase-rightcol">
  <div class="wpfilebase-filetitle">
   <a href="http://whiley.org/download/wdk/wdk-src-v0.3.10.tgz" title="Download wdk-src-v0.3.10">wdk-src-v0.3.10</a><br />
      Created on September 21, 2011.  157 Downloads, 2.0 MB.<br/>
      BSD License
  </div>
 </div>
 <div style="clear: both;"></div>
</div></center>
<center>
<div class="wpfilebase-attachment">
 <div class="wpfilebase-rightcol">
  <div class="wpfilebase-filetitle">
   <a href="http://whiley.org/download/jar/wyjc-v0.3.10.jar" title="Download wyjc-v0.3.10">wyjc-v0.3.10</a><br />
      Created on September 21, 2011.  187 Downloads, 956.0 KB.<br/>
      BSD License
  </div>
 </div>
 <div style="clear: both;"></div>
</div></center>
]]></content:encoded>
			<wfw:commentRss>http://whiley.org/2011/09/21/whiley-v0-3-10-released/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The Whiley Automata Library (WYAUTL)</title>
		<link>http://whiley.org/2011/09/20/the-whiley-automata-library-wyautl/</link>
		<comments>http://whiley.org/2011/09/20/the-whiley-automata-library-wyautl/#comments</comments>
		<pubDate>Tue, 20 Sep 2011 00:51:54 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Structural Subtyping]]></category>
		<category><![CDATA[Tree Automata]]></category>
		<category><![CDATA[Types]]></category>
		<category><![CDATA[Wyautl]]></category>

		<guid isPermaLink="false">http://whiley.org/?p=3139</guid>
		<description><![CDATA[<p>As part of the upcoming v0.3.10 release of Whiley, I&#8217;ve invested considerable effort reimplementing the type system.  This was necessary because, whilst my old implementation generally worked, writing larger programs exposed a lot of issues.  The most interesting aspect of this process is the development of a general-purpose library for representing and manipulating tree <span style="color:#777"> . . . &#8594; Read More: <a href="http://whiley.org/2011/09/20/the-whiley-automata-library-wyautl/">The Whiley Automata Library (WYAUTL)</a></span>]]></description>
			<content:encoded><![CDATA[<p>As part of the upcoming v0.3.10 release of Whiley, I&#8217;ve invested considerable effort reimplementing the type system.  This was necessary because, whilst my old implementation generally worked, writing larger programs exposed a lot of issues.  The most interesting aspect of this process is the development of a general-purpose library for representing and manipulating <a href="http://whiley.org/2011/07/06/regular-tree-automata/">tree automatas</a>.</p>
<h2>Tree Automata</h2>
<p>A tree automaton in WYAUTL is a directed graph where edges may have labels.  In general, the nodes of this graph are called <em>states</em>, whilst the edges are called <em>transitions</em>.  States have the following properties:</p>
<ul>
<li><strong>Kind</strong>.  Every state has a &#8220;kind&#8221;, which tells us something about it.  States with the same kind are somehow related and should be treated in the same way.  In Whiley, each distinct category of type has its own kind.  For example, <code>void</code>, <code>any</code> and <code>null</code> are distinct kinds.</li>
<li><strong>Children.</strong> Every state has zero or more children.  Essentially, the children of a state are those states directly reachable in a single transition.</li>
<li><strong>Determinism.</strong> Every state is either <em>deterministic</em> or <em>non-deterministic</em>.  This choice affects the way states are treated within the various algorithms.  Essentially, in a non-deterministic state, there is no fixed ordering of children.  In a deterministic state, every child is in a fixed position and can only accepts inputs in that position (more on this later).</li>
<li><strong>Supplementary Data.</strong> A state may additionally have some supplementary data.  This is specific to the problem domain the automata are used for.  In Whiley, for example, states corresponding to records have supplementary data to store field names.</li>
</ul>
<p>A tree automaton <em>accepts</em> matching <em>input trees</em>.  An input tree is essentially an acyclic automaton (generally speaking a tree, although it doesn&#8217;t actually need to be).   Tree states have kinds, children and (optionally) supplementary data.  However, tree states are always deterministic.  The intuition is that an automaton state matches a tree state if they have the same kind, and their children match as well.  The issue of determinism and supplementary data make this picture slightly more complicated, however.</p>
<h2>Example</h2>
<p>To begin with, I&#8217;ll consider an entirely abstract example.  This is because the automata library can represent arbitrary tree automata, not just those needed to for types in Whiley.  Then, we&#8217;ll see how this translates into the underlying representation of types in Whiley.</p>
<p><a href="http://whiley.org/wp-content/uploads/2011/09/TreeAutomata.png"><img class="aligncenter size-full wp-image-3157" style="border: 0pt none;" title="TreeAutomata" src="http://whiley.org/wp-content/uploads/2011/09/TreeAutomata.png" alt="" width="538" height="322" /></a>In this example, we have a single automaton on the left, and three matching tree inputs on the right.  In the automaton, square nodes indicate deterministic states, whilst circles indicate non-deterministic states.  The kind of a state is given by its colour.  We can see, for the deterministic states, that every transition is given a numeric label.  This indicates the &#8220;position&#8221; of that transition and, for simplicity, can be thought of as an edge label.  In the tree inputs, every node is deterministic and, hence, all transitions are labelled.</p>
<p>In the above example, a deterministic state in the automaton matches a state in an input tree if they have the same kind, and each child matches the corresponding child in the input.  For the non-deterministic states, however, it&#8217;s sufficient that, for every child state of the input, there exists a matching child state in the automaton.</p>
<h2>Back to Types</h2>
<p style="text-align: left;">Types in Whiley are represented using tree automata.  In fact, if you look at my previous posts on the algorithmic aspects of implementing types in Whiley  (see e.g. <a href="http://whiley.org/2011/08/30/simplification-vs-minimisation-of-types-in-whiley/">here</a>, <a href="http://whiley.org/2011/03/07/implementing-structural-types/">here</a> and <a href="http://whiley.org/2011/02/16/minimising-recursive-data-types/">here</a>) you&#8217;ll notice a striking resemblance with the above diagrams.  That&#8217;s because, well, they&#8217;re essentially the same.  Take, for example, the following Whiley type:</p>
<pre class="brush: whiley;">
define Tree as null | {int data, Tree left, Tree right}
</pre>
<p>The above type can be represented as a tree automaton, like so:</p>
<p><a href="http://whiley.org/wp-content/uploads/2011/09/TreeAutomata2.png"><img class="aligncenter size-full wp-image-3170" style="border: 0pt none;" title="TreeAutomata2" src="http://whiley.org/wp-content/uploads/2011/09/TreeAutomata2.png" alt="" width="529" height="254" /></a></p>
<p>Here, grey states represent <code>null</code>, yellow states represent <code>int</code>, blue states represent <code>records</code> (i.e. <code>{ ... }</code>) and green states unions (i.e. <code>... | ...</code>).  Furthermore, the rules for accepting inputs are adjusted so non-determinstic states do not themselves need to match a corresponding state in the input (although their children still do).  The latter is made possible in the library because one can extend the &#8220;default interpretation&#8221; of automata.</p>
<h2>Conclusion</h2>
<p>Overall, implementing a general purpose automata library has proved successful in reducing complexity of the Whiley compiler.  This is because the library abstracts many of the key differences between types, and handles various issues such as <em>minimisation</em> and <em>canonicalisation</em> automatically. The library can also be developed and tested in isolation, making it easier to focus on without getting distracted by Whiley specifics.</p>
]]></content:encoded>
			<wfw:commentRss>http://whiley.org/2011/09/20/the-whiley-automata-library-wyautl/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Namespaces in Whiley</title>
		<link>http://whiley.org/2011/09/03/namespaces-in-whiley/</link>
		<comments>http://whiley.org/2011/09/03/namespaces-in-whiley/#comments</comments>
		<pubDate>Sat, 03 Sep 2011 00:34:20 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Implementation]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Syntax]]></category>

		<guid isPermaLink="false">http://whiley.org/?p=3090</guid>
		<description><![CDATA[<p>With the upcoming v0.3.10 release of Whiley, the way import statements are interpreted has changed in a fairly significant manner.  The primary purpose of this is to give better support for namespaces. The following illustrates what&#8217;s changed:</p> import whiley.lang.Math bool check(int x, int y): return max(x,y) == x <p>Previously, the above code would compile <span style="color:#777"> . . . &#8594; Read More: <a href="http://whiley.org/2011/09/03/namespaces-in-whiley/">Namespaces in Whiley</a></span>]]></description>
			<content:encoded><![CDATA[<p>With the upcoming v0.3.10 release of Whiley, the way <code>import</code> statements are interpreted has changed in a fairly significant manner.  The primary purpose of this is to give better support for <a href="http://en.wikipedia.org/wiki/Namespace (computer science)" target="_top" >namespaces</a>. The following illustrates what&#8217;s changed:</p>
<pre class="brush: whiley;">
import whiley.lang.Math

bool check(int x, int y):
    return max(x,y) == x
</pre>
<p>Previously, the above code would compile with function <code>max()</code> being imported from <code>whiley.lang.Math</code>.  In other words, an <code>import</code> statement automatically imports all names from a given module.  However, this gives relatively little control over namespaces and quickly leads to <a href="http://bytebaker.com/2008/07/30/python-namespaces/">namespace pollution</a>.</p>
<p>Therefore, in the upcoming release of Whiley, the semantics of <code>import</code> statements has been brought more in line with <a href="http://en.wikipedia.org/wiki/Python (programming language)" target="_top" >Python</a>.  Thus, the above would not compile as is.  Instead, we would need to write:</p>
<pre class="brush: whiley;">
import whiley.lang.Math

bool check(int x, int y):
    return Math.max(x,y) == x
</pre>
<p>This all makes sense, and I&#8217;m absolutely happy with the choice to do this.  However, as usual, there are some hidden issues I didn&#8217;t foresee.</p>
<h2>Root Concepts</h2>
<p>The first issue with the above change came out from actually writing code using it!  In particular, I was working on my bytecode disassembler benchmark and constructed a module <code>ClassFile</code> as follows:</p>
<pre class="brush: whiley;">
define ClassFile as ...
define Reader as ...
define Writer as ...
</pre>
<p>This gives rise to the types <code>ClassFile.Reader</code> and <code>ClassFile.Writer</code>, both of which make sense.  But, it also gives rise to the type <code>ClassFile.ClassFile</code>, which frankly is rather cumbersome.  That&#8217;s because a <code>ClassFile</code> is both a key concept in my design and, coincidently, a namespace as well.  Of course, I could prossibly rename <code>ClassFile</code> to be module <code>Class</code> as follows:</p>
<pre class="brush: whiley;">
define File as ...
define Reader as ...
define Writer as ...
</pre>
<p>This gives rise to the types <code>Class.Reader</code>, <code>Class.Writer</code> and <code>Class.File</code>.  This is better, but I suspect such renaming won&#8217;t always fit well with the top-level design of a program.  I imagine Python must suffer from this problem as well, so I&#8217;ll have to look into it more &#8230;</p>
<h2>Processes and Messages</h2>
<p>Another problem with the above change to <code>import</code> statements, is how it affects process messages.  The following illustrates:</p>
<pre class="brush: whiley;">
import whiley.io.File

[byte] ::readFile(String filename):
    fr = File.Reader(filename)
    return fr.read()
</pre>
<p>This all looks fairly sensible, right?  Well, currently, it doesn&#8217;t compile.  The reason becomes apparent if we look inside the <code>File</code> module:</p>
<pre class="brush: whiley;">
package whiley.io

define Reader as process { ... }

Reader ::Reader(String filename):
    return spawn { ... }

[byte] Reader::read():
     ...
</pre>
<p>What we see is that <code>read()</code> is a message declared on process type <code>File.Reader</code>.  In Java terms, <code>read()</code> is a <code>static</code> method which accepts an argument of type <code>File.Reader</code>.  And, therein lies the problem.  To get our <code>readFile()</code> example to compile we need to write this:</p>
<pre class="brush: whiley;">
import whiley.io.File

[byte] ::readFile(String filename):
    fr = File.Reader(filename)
    return fr.(File.read)()
</pre>
<p>Or, alternatively, we could write it as this:</p>
<pre class="brush: whiley;">
import whiley.io.File
import read from whiley.io.File

[byte] ::readFile(String filename):
    fr = File.Reader(filename)
    return fr.read()
</pre>
<p>I find this somewhat annoying.  However, it&#8217;s not clear how much of a problem it really is.  That&#8217;s because, in practice, we&#8217;d probably want to define <code>File.Reader</code> as an <code>interface</code> like so:</p>
<pre class="brush: whiley;">
package whiley.io

define Reader as interface { [byte] read() }

Reader ::Reader(String filename):
    proc = spawn { ... }
    return { this: proc, read: &amp;read }

[byte] Reader::read():
     ...
</pre>
<p>An <code>interface</code> is a special kind of record with an explicit field <code>this</code>.  Then, when we access the field <code>read</code>, <code>this</code> is automatically used as the receiver.  With <code>Reader</code> implemented as above, our original incantation of <code>readFile()</code> would actually compile.  That&#8217;s because, in this case, <code>fr.read()</code> corresponds to an <em>indirect message send</em>, where as before it was a <em>direct message send</em>.</p>
<p>On the whole, I&#8217;m not sure what I&#8217;m complaining about!  Implementing <code>Reader</code> as an <code>interface</code> versus a <code>process</code> is much of a muchness.  There is a minor issue of performance as, for a process you get a static method invocation.  But, I&#8217;m probably just splitting hairs &#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://whiley.org/2011/09/03/namespaces-in-whiley/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Simplification vs Minimisation of Types in Whiley</title>
		<link>http://whiley.org/2011/08/30/simplification-vs-minimisation-of-types-in-whiley/</link>
		<comments>http://whiley.org/2011/08/30/simplification-vs-minimisation-of-types-in-whiley/#comments</comments>
		<pubDate>Mon, 29 Aug 2011 22:19:09 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Implementation]]></category>
		<category><![CDATA[Structural Subtyping]]></category>
		<category><![CDATA[Theory]]></category>
		<category><![CDATA[Types]]></category>
		<category><![CDATA[Typing]]></category>

		<guid isPermaLink="false">http://whiley.org/?p=3055</guid>
		<description><![CDATA[<p>Recently, I&#8217;ve been trying to harden up the implementation of Whiley&#8217;s type system. The reason for this is fairly straightforward: bugs in the code often prevent me from compiling correct programs!</p> <p>In thinking about how to restructure the algorithms I&#8217;m using, I realised its important to distinguish simplification from minimisation.  I&#8217;ve talked about minimisation <span style="color:#777"> . . . &#8594; Read More: <a href="http://whiley.org/2011/08/30/simplification-vs-minimisation-of-types-in-whiley/">Simplification vs Minimisation of Types in Whiley</a></span>]]></description>
			<content:encoded><![CDATA[<p>Recently, I&#8217;ve been trying to harden up the implementation of Whiley&#8217;s type system.  The reason for this is fairly straightforward: <em>bugs in the code often prevent me from compiling correct programs!</em></p>
<p>In thinking about how to restructure the algorithms I&#8217;m using, I realised its important to distinguish <em>simplification</em> from <em>minimisation</em>.  I&#8217;ve talked about minimisation in some detail before (see <a href="http://whiley.org/2011/02/16/minimising-recursive-data-types/">here</a> and <a href="http://whiley.org/2011/03/07/implementing-structural-types/">here</a>).</p>
<p>In fact, there are three phases in computing the ideal representation of a given type (in order of increasing difficulty):</p>
<ul>
<li><strong>Simplification.</strong> Here, we apply mostly straightforward simplifications.  Examples include: <code>(T|T) =&gt; T</code>, <code>(T|any) =&gt; any</code>, <code>(T|void) =&gt; T</code>, <code>(T|(S|U)) =&gt; (T|S|U)</code>, etc.</li>
</ul>
<ul>
<li><strong>Minimisation. </strong>Here, we ensure that no two nodes in the type graph are <em>equivalent</em> under the subtyping operator.  For example, the type <code>X&lt;[null|[null|X]]&gt;</code> is not minimised, whilst <code>X&lt;[null|X]&gt;</code> is its minimised form.</li>
</ul>
<ul>
<li><strong>Canonicalisation.</strong> The final step is to ensure equivalent types have an identical representation on the machine.  This is related to the <a href="http://en.wikipedia.org/wiki/Graph isomorphism" target="_top" >Graph isomorphism</a> problem and, more specifically, the issues of computing a <a href="http://en.wikipedia.org/wiki/Graph canonization" target="_top" >canonical form</a> of a graph.</li>
</ul>
<p>This all seems fairly straightforward &#8230; but there are of course some tricky bits!!</p>
<h2>Simplification is not that Simple!</h2>
<p>The first mistake I made was to assume that simplification was a simple step in the process.  Unfortunately, there are some gotchas:</p>
<pre class="brush: whiley;">
define LinkedList as null | { LinkedList next, int data }
define MyList as int | LinkedList
</pre>
<p>The problem is how to minimise this.  One property I want of the simplified form is to eliminate unions of unions.  But, consider the type graph for <code>MyList</code>:</p>
<p style="text-align: center;"><a href="http://whiley.org/wp-content/uploads/2011/08/SimplifyingRecursiveTypes1.png"><img class="aligncenter size-full wp-image-3065" style="border: 0pt none;" title="SimplifyingRecursiveTypes1" src="http://whiley.org/wp-content/uploads/2011/08/SimplifyingRecursiveTypes1.png" alt="" width="194" height="237" /></a></p>
<p>(here, circles represent unions, squares represent records, etc)</p>
<p>Now, it&#8217;s difficult to see how we simplify the above to remove the union (node <code>0</code>) of a union (node <code>2</code>).  That&#8217;s because of the recursive link directly into node <code>2</code>.  In fact, we can achieve this by judiciously expanding the type like so:</p>
<p><a href="http://whiley.org/wp-content/uploads/2011/08/SimplifyingRecursiveTypes2.png"><img class="aligncenter size-full wp-image-3068" style="border: 0pt none;" title="SimplifyingRecursiveTypes2" src="http://whiley.org/wp-content/uploads/2011/08/SimplifyingRecursiveTypes2.png" alt="" width="226" height="306" /></a>This does the trick and (I believe) it&#8217;s always possible to eliminate unions of unions in this way.</p>
<h2>Simplification Helps Minimisation!</h2>
<p>You might be wondering: <em>why bother with simplification at all? </em> Well, it&#8217;s because it simplifies the minimisation algorithm (which is one of the components that features a lot of bugs).  In essence, simplification gives me the following property:</p>
<blockquote><p><strong>Property 1</strong>.  Any two equivalent nodes in a simplified type have identical reachable structure.</p></blockquote>
<p>In a non-simplified type graph, the above is clearly not true.  For example, in the type <code>(any|int)</code> the outer union and <code>any</code> are equivalent (but have different structure).  Here&#8217;s another example:</p>
<p style="text-align: center;"><a href="http://whiley.org/wp-content/uploads/2011/08/SimplifyingRecursiveTypes3.png"><img class="aligncenter size-full wp-image-3070" style="border: 0pt none;" title="SimplifyingRecursiveTypes3" src="http://whiley.org/wp-content/uploads/2011/08/SimplifyingRecursiveTypes3.png" alt="" width="230" height="159" /></a></p>
<p>Here, nodes <code>0</code>, <code>1</code> and <code>2</code> are all equivalent.  But, whilst nodes <code>1</code> and <code>2</code> have identical reachable structure, this differs from node <code>0</code>.  In particular, the children of node <code>0</code> are in the same equivalence class as node <code>0</code>, whilst those for nodes <code>1</code> and <code>2</code> are not.  In practical terms, my minimisation algorithm would have to handle this edge case and its numerous variations.  With simplified form, however, these awkward cases disappear.</p>
<h2>Minimised is Simplified?</h2>
<p>An interesting question is whether a type remains in simplified form after minimisation.  I conjecture that the answer is yes!  Atleast, provided a little care is taken.</p>
<p>The main issue is that the minimisation process can remove union nodes; however, <em>it cannot introduce them</em>.  Consider this final example:</p>
<pre class="brush: whiley;">
define LinkedList as null | { LinkedList next, int data }
define InnerList as null | { OuterList next, int data }
define OuterList as null | { InnerList next, int data }
define MyList as LinkedList | OuterList
</pre>
<p>These definitions give rise to the following type graph for <code>MyList</code>:</p>
<p style="text-align: center;"><a href="http://whiley.org/wp-content/uploads/2011/08/SimplifyingRecursiveTypes23.png"><img class="aligncenter size-full wp-image-3077" style="border: 0pt none;" title="SimplifyingRecursiveTypes23" src="http://whiley.org/wp-content/uploads/2011/08/SimplifyingRecursiveTypes23.png" alt="" width="419" height="430" /></a></p>
<p>This type graph is fully simplified (it&#8217;s a bit of a monster though!).  This is because simplification does not attempt to eliminate <em>equivalent</em> nodes from a union, only <em>identical</em> ones.  After minimisation, the outermost union will be removed and we&#8217;ll be left with just this:</p>
<p style="text-align: center;"><a href="http://whiley.org/wp-content/uploads/2011/08/SimplifyingRecursiveTypes24.png"><img class="aligncenter size-full wp-image-3078" style="border: 0pt none;" title="SimplifyingRecursiveTypes24" src="http://whiley.org/wp-content/uploads/2011/08/SimplifyingRecursiveTypes24.png" alt="" width="138" height="176" /></a></p>
<p>Now, back to the original issue.  In the general case, we can remove a union node from between any two nodes.  However, we cannot remove other kinds of nodes unless the entire subtree below that node is removed.  Therefore, we cannot introduce a union of union through this process.</p>
]]></content:encoded>
			<wfw:commentRss>http://whiley.org/2011/08/30/simplification-vs-minimisation-of-types-in-whiley/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Subtyping Gotcha</title>
		<link>http://whiley.org/2011/08/29/a-subtyping-gotcha/</link>
		<comments>http://whiley.org/2011/08/29/a-subtyping-gotcha/#comments</comments>
		<pubDate>Mon, 29 Aug 2011 03:37:05 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Structural Subtyping]]></category>
		<category><![CDATA[Syntax]]></category>
		<category><![CDATA[Typing]]></category>

		<guid isPermaLink="false">http://whiley.org/?p=3040</guid>
		<description><![CDATA[<p>An interesting issue with the following piece of code has recently come to my attention:</p> define Queue as process { [int] items } int Queue::get(): item = this.items[0] this.items = this.items[1..] return item void Queue::put(int item): this.items = this.items + [item] Queue ::Queue(): // following line should be invalid return spawn { items: [] <span style="color:#777"> . . . &#8594; Read More: <a href="http://whiley.org/2011/08/29/a-subtyping-gotcha/">A Subtyping Gotcha</a></span>]]></description>
			<content:encoded><![CDATA[<p>An interesting issue with the following piece of code has recently come to my attention:</p>
<pre class="brush: whiley;">
define Queue as process { [int] items }

int Queue::get():
    item = this.items[0]
    this.items = this.items[1..]
    return item

void Queue::put(int item):
    this.items = this.items + [item]

Queue ::Queue():
    // following line should be invalid
    return spawn { items: [] }
</pre>
<p>The problem is that this code <em>should not compile</em>.  More specifically, the constructor <code>::Queue</code> should generate a syntax error.</p>
<p>The reason for this is that <em>we cannot safely subtype processes</em>.  To see why, consider the following (artificial) example:</p>
<pre class="brush: whiley;">
define MyProc as process { [int] field }
define BrokenProc as process { [int]|int field }

BrokenProc ident(MyProc mp):
    return mp // should not be allowed

void ::breakIt(MyProc mp):
    bp = ident(mp)
    bp.field = 1 // uh oh

[int] ::broken(MyProc mp):
    breakIt(mp)
    return mp.field // should be ok, but isn't...
</pre>
<p>The problem here arises from <em>aliasing</em>.  That is, since both <code>mp</code> and <code>bp</code> refer to the same process and, hence, assigning to <code>bp.field</code> corrupts the type of <code>mp.field</code>.  To resolve the above problem we require that <code>MyProc</code> is not a subtype of <code>BrokenProc</code>.  In other words, <em>no subtyping between process types should allowed</em>. </p>
<p>This issue may seem fairly innocuous since we easily can &#8220;fix&#8221; it by preventing subtyping between processes.  Unfortunately, does this means <code>spawn {field: []}</code> is not a subtype of <code>MyProc</code> either, since it has type <code>process {[void] field}</code>.  In other words, we cannot initialise a process field with an empty list!  There are some well-known ways in which we might get around this.  In particular, if we have a <em>unique reference</em> to a process, then subtyping is safe.  Looking at the <code>::Queue</code> constructor again, we actually do have a unique reference since we just spawned it!</p>
]]></content:encoded>
			<wfw:commentRss>http://whiley.org/2011/08/29/a-subtyping-gotcha/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Whiley v0.3.9 Released!</title>
		<link>http://whiley.org/2011/08/15/whiley-v0-3-9-released/</link>
		<comments>http://whiley.org/2011/08/15/whiley-v0-3-9-released/#comments</comments>
		<pubDate>Mon, 15 Aug 2011 08:38:13 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Implementation]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Syntax]]></category>
		<category><![CDATA[Wyclipse]]></category>

		<guid isPermaLink="false">http://whiley.org/?p=2994</guid>
		<description><![CDATA[<p>So, it&#8217;s that time again for another update of the Whiley compiler. Perhaps the most interesting update is that constraints are back! Admitedly, only runtime checking of constraints is back; and, there are quite a few problems with it. But, it&#8217;s a step in the right direction, and I&#8217;m pretty excited about it.</p> <p>I&#8217;ve <span style="color:#777"> . . . &#8594; Read More: <a href="http://whiley.org/2011/08/15/whiley-v0-3-9-released/">Whiley v0.3.9 Released!</a></span>]]></description>
			<content:encoded><![CDATA[<p>So, it&#8217;s that time again for another update of the Whiley compiler.  Perhaps the most interesting update is that <em>constraints are back</em>!  Admitedly, only runtime checking of constraints is back; and, there are quite a few problems with it.  But, it&#8217;s a step in the right direction, and I&#8217;m pretty excited about it.</p>
<p>I&#8217;ve also been doing some more work on the benchmark suite.  In particular, the Java Bytecode disassembler is coming along.  One problem I&#8217;ve encountered, unfortunately, is that my type system implementation remains quite fragile.  I will need to harden this up before the next release, otherwise I&#8217;ll never get the disassembler working &#8230;</p>
<h2>Performance</h2>
<p>Aside from large benchmarks (e.g. Chess and the Disassembler), I&#8217;ve also been adding micro benchmarks (both sequential and concurrent).  For the sequential ones, I&#8217;ve provided a Java implemetation as well for comparison purposes.  I thought it would be interesting to see how Whiley compares, so here we go:</p>
<p><a href="http://whiley.org/wp-content/uploads/2011/08/raw.jpg"><img class="aligncenter size-full wp-image-2997" style="border: 0pt none;" title="raw" src="http://whiley.org/wp-content/uploads/2011/08/raw.jpg" alt="" width="538" height="403" /></a>Note the log scale.  As expected, performance is truly woeful.  However, it provides a useful start point for  improving performance &#8230;</p>
<h2>Wyclipse</h2>
<p>Another interesting development, is that I&#8217;ve begun the process of  constructing an Eclipse plugin.  Things are pretty rudimentary at the  moment, but I was able to get an editor with syntax highlighting working  quite easily!  You can see the code over on <a href="https://github.com/DavePearce/wyclipse">GitHub</a>.</p>
<h2>ChangeLog</h2>
<ul>
<li>Added support for &#8220;headless&#8221; methods.  A headless method is a method which has side-effects but has no explicit receiver.  Such methods are useful, for example, for implementing constructors.</li>
<li>Added ability to iterate strings and dictionaries.</li>
<li>Changed semantics for dictionaries to support updating of keys.</li>
<li>Put back in support for runtime constraint checking.  The main outstanding issue here is that loop invariants don&#8217;t work.</li>
<li>Turned off constant propagation because it&#8217;s causing lots of problems in relation to the constraints.</li>
</ul>
<center>
<div class="wpfilebase-attachment">
 <div class="wpfilebase-rightcol">
  <div class="wpfilebase-filetitle">
   <a href="http://whiley.org/download/wdk/wdk-src-v0.3.9.tgz" title="Download wdk-src-v0.3.9">wdk-src-v0.3.9</a><br />
      Created on May 19, 2012.  108 Downloads, 1.8 MB.<br/>
      BSD License
  </div>
 </div>
 <div style="clear: both;"></div>
</div></center>
<center>
<div class="wpfilebase-attachment">
 <div class="wpfilebase-rightcol">
  <div class="wpfilebase-filetitle">
   <a href="http://whiley.org/download/jar/wyjc-v0.3.9.jar" title="Download wyjc-v0.3.9">wyjc-v0.3.9</a><br />
      Created on May 19, 2012.  145 Downloads, 745.7 KB.<br/>
      BSD License
  </div>
 </div>
 <div style="clear: both;"></div>
</div></center>
]]></content:encoded>
			<wfw:commentRss>http://whiley.org/2011/08/15/whiley-v0-3-9-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Parallel Sum in Whiley</title>
		<link>http://whiley.org/2011/08/03/parallel-sum-in-whiley/</link>
		<comments>http://whiley.org/2011/08/03/parallel-sum-in-whiley/#comments</comments>
		<pubDate>Tue, 02 Aug 2011 21:49:48 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Examples]]></category>
		<category><![CDATA[Actors]]></category>
		<category><![CDATA[Concurrency]]></category>

		<guid isPermaLink="false">http://whiley.org/?p=2973</guid>
		<description><![CDATA[<p>Recently, I&#8217;ve been working on a variety of sequential and concurrent micro benchmarks for testing Whiley&#8217;s performance.  An interesting and relatively simple example, is the parallel sum.  The idea is to sum a large list of integers whilst performing as much work as possible in parallel.</p> <p>To implement the parallel sum, I divide the <span style="color:#777"> . . . &#8594; Read More: <a href="http://whiley.org/2011/08/03/parallel-sum-in-whiley/">Parallel Sum in Whiley</a></span>]]></description>
			<content:encoded><![CDATA[<p>Recently, I&#8217;ve been working on a variety of sequential and concurrent micro benchmarks for testing Whiley&#8217;s performance.  An interesting and relatively simple example, is the parallel sum.  The idea is to sum a large list of integers whilst performing as much work as possible in parallel.</p>
<p>To implement the parallel sum, I divide the list into roughly equal sized chunks and assign one process to each:</p>
<pre class="brush: whiley;">
define Sum as process {
    [int] items,
    int start,
    int end,
    int result
}

void Sum::start():
    sum = 0
    for i in start..end:
        sum = sum + items[i]
    this.result = sum

int Sum::get():
    return result

// Sum constructor
Sum ::Sum([int] is, int s, int e):
    return spawn {
        items: i,
        start: s,
        end: e,
        result: 0
    }
</pre>
<p>Essentially, each <code>Sum</code> process holds the original list of <code>items</code> and a range <code>start..end</code> over which to operate.  The <code>result</code> is used to store the final sum until it is requested by the outer loop.  The idea is that we first construct the processes, then start them all asynchronously and, finally, collect up the results.</p>
<p>The outer loop looks something like this:</p>
<pre class="brush: whiley;">
define N as 100 // block size to use

int ::parSum([int] items):
    while |items| != 1:
        // Calculate how many workers required
        nworkers = max(1,|items| / N)
        size = |items| / nworkers
        // Construct and start workers
        pos = 0
        workers = []
        for i in 0..nworkers:
            if i &lt; (nworkers-1):
                worker = Sum(items,pos,pos+size)
            else:
                // Last worker picks up the slack
                worker = Sum(items,pos,|items|)
            // Start worker asynchronously
            worker!start()
            // Bookkeeping
            workers = workers + [worker]
            pos = pos + size
     // Collect up results
     items = []
     for i in 0 .. nworkers:
         items = items + [workers[i].get()]
 return items[0]
</pre>
<p>The key here is that the outer loop continues until the original list of <code>items</code> is reduced to a single result.  There maybe several iterations required, depending on the block size.  Furthermore, the block size determines how many items will be processed by each process in one go.  Smaller block sizes lead to more parallelism, but have higher overheads.  The optimal block size probably depends on the underlying architecture, and would ideally be chosen at runtime.</p>
]]></content:encoded>
			<wfw:commentRss>http://whiley.org/2011/08/03/parallel-sum-in-whiley/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 1.103 seconds -->

