<?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>jalf.dk &#187; functional</title>
	<atom:link href="http://jalf.dk/blog/tag/functional/feed/" rel="self" type="application/rss+xml" />
	<link>http://jalf.dk/blog</link>
	<description>Musings and thoughts on programming and other geeky stuff</description>
	<lastBuildDate>Sat, 07 Jan 2012 15:42:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Being functional in an imperative language</title>
		<link>http://jalf.dk/blog/2009/10/being-functional-in-an-imperative-language/</link>
		<comments>http://jalf.dk/blog/2009/10/being-functional-in-an-imperative-language/#comments</comments>
		<pubDate>Sat, 03 Oct 2009 12:00:25 +0000</pubDate>
		<dc:creator>jalf</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[functional]]></category>
		<category><![CDATA[imperative]]></category>
		<category><![CDATA[stm]]></category>
		<category><![CDATA[thesis]]></category>
		<category><![CDATA[transactional-memory]]></category>

		<guid isPermaLink="false">http://jalf.dk/blog/?p=334</guid>
		<description><![CDATA[By now, I’ve read an awful lot of papers about STM systems, and certain trends are really starting to stand out, not so much in terms of the algorithms used or the clever schemes invented to make transactions appear atomic, but in how they interface with the actual language. It has really underlined to me [...]]]></description>
			<content:encoded><![CDATA[<p>By now, I’ve read an awful lot of papers about STM systems, and certain trends are really starting to stand out, not so much in terms of the algorithms used or the clever schemes invented to make transactions appear atomic, but in how they interface with the actual language.</p>

<p>It has really underlined to me just how deeply entrenched most Java, C and C++ programmers are in the imperative mindset.
<span id="more-334"></span>
When we want to perform a transaction, we’ll need to execute something like the following pseudocode:</p>

<pre><code>for (;;) {
  tx = new Transaction();
  try {
    // ... fill in body of the transaction ...
    tx.Validate();
    tx.Commit()
  }
  catch (InvalidTxException){
    tx.Rollback();
  }
}
</code></pre>

<p>And several STM systems actually require the user to write the above whenever a transaction is to be executed.</p>

<p>Some implementers have realized that this is both verbose, messy and error-prone.
So they try to hide it behind a macro.</p>

<pre><code>#define BEGIN_ATOMIC \
  for (;;) { \
    tx = new Transaction();\
    try { \

#define END_ATOMIC \
      tx.Validate();
      tx.Commit()
    }
    catch (InvalidTxException){
      tx.Rollback();
    }
  }
</code></pre>

<p>and now a transaction looks like this instead, from the user’s point of view:</p>

<pre><code>BEGIN_ATOMIC
// ... fill in body of the transaction ...
END_ATOMIC
</code></pre>

<p>Which is obviously shorter and requires less duplicate code. On the other hand, it relies on macros. Eeeew, yuck!</p>

<p>In Java, most implementers have tried to extend the language, to add a language-level <code>atomic</code> statement, allowing code like this:</p>

<pre><code>atomic {
  // ... fill in body of the transaction ...
}
</code></pre>

<p>And yes, this obviously works too, and has the potential to integrate much more closely with the compiler and type-checker. But  it’s also a <em>very</em> imperative kind of thinking. “add another type of scope inside whichever function wants to perform the transaction”.</p>

<p>What astonishes me is that no one (apart from Peyton Jones, who implemented Haskell STM) have realized that what we really want is nothing more than a higher-order function. A function which implements the transaction infrastructure, and then calls a user-defined function.</p>

<p>What we’d like to do, ideally, is to define a function <code>atomic</code>, as in the following pseudocode:</p>

<pre><code>T atomic(f : Tx -&gt; T){
  for (;;) {
    tx = new Transaction();
    try {
      T ret = f(tx);
      tx.Validate();
      tx.Commit();
      return ret;
    }
    catch (InvalidTxException){
      tx.Rollback();
    }
  }
}
</code></pre>

<p>Of course, if we wanted to be <em>really</em> functional, we could use recursion instead of the loop, but let’s not get carried away. Ultimately, what we want is just to hide all the messy infrastructure of a transaction from the user.</p>

<p>The above declares a function <code>atomic</code> which takes as its parameter a function which, given a transaction (type <code>Tx</code>), returns type <code>T</code>.</p>

<p><code>atomic</code> implements the messy transaction loop, and simply calls the user-supplied function inside it.</p>

<p>And presto, the user can now create a transaction like this:</p>

<pre><code>int myTx(Tx tx) {
  // do whatever
}
int result = atomic(myTx);
</code></pre>

<p>Or, in a language which supports lambda expressions,</p>

<pre><code>int result = atomic((tx) =&gt; {/* do whatever */ });
</code></pre>

<p>Bit nicer than messing around with user-implemented loops <em>or</em> macros, isn’t it?
Of course, I’ve so far sought refuge within the peaceful realms of pseudocode, where anything is possible. We obviously can’t do this in Java or C++, can we?</p>

<p>No, and yes, in that order. In Java, I agree, we’re just screwed. Of course, we could just pass in an object instead of a function, and if it implements some ICallableTransactionBody interface, <code>atomic</code> could call its <code>RunTx()</code> method. That would work, but it’s not quite as elegant.</p>

<p>In C++, though, the situation is different. A large part of the standard library (all the STL algorithms) relies on higher-order functions. .</p>

<p>In C++, we can define <code>atomic</code> as follows:</p>

<pre><code>template &lt;typename Func&gt;
void atomic(Func f){
  for (;;) {
    tx = new Transaction();
    try {
      f(tx);
      tx.Validate();
      tx.Commit();
    }
    catch (InvalidTxException){
      tx.Rollback();
    }
  }
}
</code></pre>

<p>First, I should mention that I cheated. I removed the return value. <code>atomic</code> now returns <code>void</code>. The reason is that it’s not quite trivial to infer the return type of a function in C++. <a href="http://en.wikipedia.org/wiki/C++_Technical_Report_1">TR1</a> does introduce a function <code>std::result_of</code> but it doesn’t work in every case. C++0x will fix this once and for all, by providing the necessary language features to implement it. But until then, we could hack around it, either by requiring the user to explicitly specify the return type (<code>atomic&lt;int&gt;(myTx)</code>), or by using <code>result_of</code> and simply telling the user to avoid the cases where it doesn’t work. But to keep the above code example simple, I simply removed the return type instead. But it <em>can</em> be solved, and my prototype does it.</p>

<p>Now, on with the show. We have now defined our <code>atomic</code> function. How do we call it?</p>

<p>In one of three ways, in decreasing order of familiarity:</p>

<p>First, with a function pointer. Hopefully every C++ programmer knows about these:</p>

<pre><code>void myTx(Tx tx) {... }
atomic(myTx);
</code></pre>

<p>Fairly clean and simple. One downside is that because the <code>atomic</code> function is just passed a function pointer, the compiler may not be able to inline the call to <code>myTx</code>. It is doubtful that it’d make much difference here, but it is nevertheless a valid concern.</p>

<p>The second approach fixes this, as well as enabling some more flexibility: Functors, or function objects, which every C++ programmer <em>should</em> know about, but unfortunately, not everyone does:</p>

<pre><code>struct myTx {
  void operator()(Tx tx) {...}
};
atomic(myTx());
</code></pre>

<p>Now we’re passing in an object of type <code>myTx</code>, which means that the compiler knows exactly which function to call: <code>myTx::operator()(Tx)</code>. So it can inline this trivially, eliminating the above performance concern.</p>

<p>Perhaps more importantly, our function can now hold state. It is an object, so it can be given a constructor and destructor. It can be given other member functions to allow the user to interact with it. It has become a lot more versatile.</p>

<p>Finally, we can wait for C++0x, which introduces lambda expressions:</p>

<pre><code>atomic([](Tx tx) { ...});
</code></pre>

<p>And what’s noteworthy is that the <em>same</em> <code>atomic</code> definition works in all three cases! So the user is actually given the choice of which method to use. Our STM library supports them all.</p>

<p>Isn’t this cleaner than the original approach, of requiring the user to implement the whole loop himself?
So why have no one used it before?</p>

<p>Ultimately, I can think of two possible reasons:</p>

<ul>
<li>the writers, usually very clever researchers and academics, were not familiar with higher-order functions, or with functional programming in general, or</li>
<li>the writers did not know that this was possible in C++</li>
</ul>

<p>I suppose the second option is the most forgiveable. C++ is a big messy language, and not everyone are familiar with every corner of it. That’s fair enough, but even then, most of this could have been done in C too, by using function pointers. It’s not <em>that</em> esoteric, is it? But I sincerely hope it is not the first case. I could understand if Joe Coder at Insignificant Software Inc. had never heard of functional programming, but most of these are computer science professors!</p>

<p>Incidentally, this should also provide a nice example for when an imperative programmer asks “how would it benefit me to learn a functional language?”</p>

<p>Here we have a clear example of how using a very simple functional technique can significantly clean up the code and make it much more robust<sup id="fnref:1"><a href="#fn:1" rel="footnote">1</a></sup>, even in an imperative language like C++.</p>

<div class="footnotes">
<hr />
<ol>

<li id="fn:1">
<p>Imagine what would have happened in the original loop– or macro-based versions, if the user had dared to put a <code>break</code> or <code>return</code> in their transaction… But both are safe in the higher-order function version. <a href="#fnref:1" rev="footnote">↩</a></p>
</li>

</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://jalf.dk/blog/2009/10/being-functional-in-an-imperative-language/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

