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

<channel>
	<title>Paolo Capriotti</title>
	<atom:link href="http://pcapriotti.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://pcapriotti.wordpress.com</link>
	<description>KDE hacking and more</description>
	<lastBuildDate>Fri, 27 Jan 2012 19:12:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='pcapriotti.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Paolo Capriotti</title>
		<link>http://pcapriotti.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://pcapriotti.wordpress.com/osd.xml" title="Paolo Capriotti" />
	<atom:link rel='hub' href='http://pcapriotti.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Reinversion of control with continuations</title>
		<link>http://pcapriotti.wordpress.com/2012/01/18/reinversion-of-control-with-continuations/</link>
		<comments>http://pcapriotti.wordpress.com/2012/01/18/reinversion-of-control-with-continuations/#comments</comments>
		<pubDate>Wed, 18 Jan 2012 00:03:58 +0000</pubDate>
		<dc:creator>pcapriotti</dc:creator>
				<category><![CDATA[frp]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[kde]]></category>
		<category><![CDATA[monads]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://pcapriotti.wordpress.com/?p=176</guid>
		<description><![CDATA[In my last post I mentioned how it is possible to achieve a form of &#34;reinversion of control&#34; by using (green) threads. Some commenters noted how this is effectively a solved problem, as demonstrated for example by Erlang, as well as the numerous variations on CSP currently gaining a lot of popularity. I don&#8217;t disagree [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pcapriotti.wordpress.com&amp;blog=806312&amp;post=176&amp;subd=pcapriotti&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://pcapriotti.wordpress.com/2012/01/10/from-event-driven-programming-to-frp/">last post</a> I mentioned how it is possible to achieve a form of &quot;reinversion of control&quot; by using (green) threads. Some commenters noted how this is effectively a solved problem, as demonstrated for example by Erlang, as well as the numerous variations on CSP currently gaining a lot of popularity.</p>
<p>I don&#8217;t disagree with that, but it&#8217;s just not the point of this series of posts. This is about understanding the computational structure of event-driven code, and see how it&#8217;s possible to transform it into a less awkward form <strong>without</strong> introducing concurrency (or at least not in the traditional sense of the term).</p>
<p>Using threads to solve what is essentially a control flow problem is cheating. And you pay in terms of increased complexity, and code which is harder to reason about, since you introduced a whole lot of interleaving opportunities and possible race conditions. Using a non-preemptive concurrency abstraction with manual yield directives (like my <a href="https://gist.github.com/1086172">Python gist</a> does) will solve that, but then you&#8217;d have to think of how to schedule your coroutines, so that is also not a complete solution.</p>
<h2 id="programmable-semicolons">Programmable semicolons</h2>
<p>To find an alternative to the multitask-based approach, let&#8217;s focus on two particular lines of the last example:</p>
<pre style="vertical-align:baseline;border:none;margin:0;padding:0;"><code class="sourceCode python">    reply = start_request();
    get_data(reply)</code></pre>
<p>where I added an explicit semicolon at the end of the first line. A semicolon is an important component of an imperative program, even though, syntactically, it is often omitted in languages like Python. It corresponds to the <em>sequencing</em> operator: execute the instruction on the left side, then pass the result to the right side and execute that.</p>
<p>If the instruction on the left side corresponds to an asynchronous operation, we want to alter the meaning of sequencing. Given a sequence of statements of the form</p>
<pre style="vertical-align:baseline;border:none;margin:0;padding:0;"><code class="sourceCode c">    x = A(); B(x)</code></pre>
<p>we want to interpret that as: call <code>A</code>, then return control back to the main loop; when <code>A</code> is finished, bind its result to <code>x</code>, then run <code>B</code>.</p>
<p>So what we want is to be able to override the sequencing operator: we want <strong>programmable semicolons</strong>.</p>
<h2 id="the-continuation-monad">The continuation monad</h2>
<p>Since it is often really useful to look at the types of functions to understand how exactly they fit together, we&#8217;ll leave Python and start focusing on Haskell for our running example.</p>
<p>We can make a very important observation immediately by looking at the type of the callback registration function that our framework offers, and try to interpret it in the context of controlled side effects (i.e. the <code>IO</code> monad). For Qt, it could look something like:</p>
<table class="sourceCode">
<tr>
<td style="border-right:1px solid #AAAAAA;text-align:right;color:#AAAAAA;padding-right:5px;padding-left:5px;">
<pre style="vertical-align:baseline;border:none;margin:0;padding:0;">1</pre>
</td>
<td class="sourceCode">
<pre style="vertical-align:baseline;border:none;margin:0;padding:0;"><code class="sourceCode haskell"><span style="color:#007020;">    connect </span><span style="color:#007020;">::</span> <span style="color:#902000;">Object</span> <span style="color:#007020;">-&gt;</span> <span style="color:#902000;">String</span> <span style="color:#007020;">-&gt;</span> (a <span style="color:#007020;">-&gt;</span> <span style="color:#902000;">IO</span> ()) <span style="color:#007020;">-&gt;</span> <span style="color:#902000;">IO</span> ()</code></pre>
</td>
</tr>
</table>
<p>to be used, for example, like this:</p>
<table class="sourceCode">
<tr>
<td style="border-right:1px solid #AAAAAA;text-align:right;color:#AAAAAA;padding-right:5px;padding-left:5px;">
<pre style="vertical-align:baseline;border:none;margin:0;padding:0;">1
2</pre>
</td>
<td class="sourceCode">
<pre style="vertical-align:baseline;border:none;margin:0;padding:0;"><code class="sourceCode haskell">    connect httpReply <span style="color:#4070a0;">&quot;finished()&quot;</span> <span style="color:#06287e;">$</span> \_ <span style="color:#007020;">-&gt;</span> <span style="color:#007020;font-weight:bold;">do</span>
        <span style="color:#06287e;">putStrLn</span> <span style="color:#4070a0;">&quot;request finished&quot;</span></code></pre>
</td>
</tr>
</table>
<p>so the first argument is the object, the second is the C++ signature of the signal, and the third is a callback that will be invoked by the framework whenever the specified signal is emitted. Now, we can get rid of all the noise of actually connecting to a signal, and define a type representing just the act of registering a callback.</p>
<table class="sourceCode">
<tr>
<td style="border-right:1px solid #AAAAAA;text-align:right;color:#AAAAAA;padding-right:5px;padding-left:5px;">
<pre style="vertical-align:baseline;border:none;margin:0;padding:0;">1</pre>
</td>
<td class="sourceCode">
<pre style="vertical-align:baseline;border:none;margin:0;padding:0;"><code class="sourceCode haskell">    <span style="color:#007020;font-weight:bold;">newtype</span> <span style="color:#902000;">Event</span> a <span style="color:#06287e;">=</span> <span style="color:#902000;">Event</span> {<span style="color:#007020;"> on </span><span style="color:#007020;">::</span> (a <span style="color:#007020;">-&gt;</span> <span style="color:#902000;">IO</span> ()) <span style="color:#007020;">-&gt;</span> <span style="color:#902000;">IO</span> () }</code></pre>
</td>
</tr>
</table>
<p>Doesn&#8217;t that look familiar? It is <strong>exactly</strong> the <a href="http://hackage.haskell.org/packages/archive/mtl/latest/doc/html/Control-Monad-Cont.html">continuation monad transformer</a> applied to the <code>IO</code> monad! The usual monad instance for <code>ContT</code> perfectly captures the semantics we are looking for:</p>
<table class="sourceCode">
<tr>
<td style="border-right:1px solid #AAAAAA;text-align:right;color:#AAAAAA;padding-right:5px;padding-left:5px;">
<pre style="vertical-align:baseline;border:none;margin:0;padding:0;">1
2
3
4
5</pre>
</td>
<td class="sourceCode">
<pre style="vertical-align:baseline;border:none;margin:0;padding:0;"><code class="sourceCode haskell">    <span style="color:#007020;font-weight:bold;">instance</span> <span style="color:#007020;font-weight:bold;">Monad</span> <span style="color:#902000;">Event</span> <span style="color:#007020;font-weight:bold;">where</span>
      <span style="color:#06287e;">return</span> x <span style="color:#06287e;">=</span> <span style="color:#902000;">Event</span> <span style="color:#06287e;">$</span> \k <span style="color:#007020;">-&gt;</span> k x
      e <span style="color:#06287e;">&gt;&gt;=</span> f <span style="color:#06287e;">=</span> <span style="color:#902000;">Event</span> <span style="color:#06287e;">$</span> \k <span style="color:#007020;">-&gt;</span>
        on e <span style="color:#06287e;">$</span> \x <span style="color:#007020;">-&gt;</span>
          on (f x) k</code></pre>
</td>
</tr>
</table>
<p>The return function simply calls the callback immediately with the provided value, no actual connection is performed. The bind operator represents our custom semicolon: we connect to the first event, and when that fires, we take the value it yielded, apply it to <code>f</code>, and connect to the resulting event.</p>
<p>Now we can actually translate the Python code of the previous example to Haskell:</p>
<table class="sourceCode">
<tr>
<td style="border-right:1px solid #AAAAAA;text-align:right;color:#AAAAAA;padding-right:5px;padding-left:5px;">
<pre style="vertical-align:baseline;border:none;margin:0;padding:0;">1
2
3
4
5
6
7
8
9
10</pre>
</td>
<td class="sourceCode">
<pre style="vertical-align:baseline;border:none;margin:0;padding:0;"><code class="sourceCode haskell"><span style="color:#007020;">    ex </span><span style="color:#007020;">::</span> <span style="color:#902000;">Event</span> ()
    ex <span style="color:#06287e;">=</span> forever <span style="color:#06287e;">$</span> <span style="color:#007020;font-weight:bold;">do</span>
      result <span style="color:#007020;">&lt;-</span> untilRight <span style="color:#06287e;">.</span> <span style="color:#06287e;">replicate</span> <span style="color:#40a070;">2</span> <span style="color:#06287e;">$</span> <span style="color:#007020;font-weight:bold;">do</span>
        reply <span style="color:#007020;">&lt;-</span> startRequest
        <span style="color:#06287e;">either</span> (<span style="color:#06287e;">return</span> <span style="color:#06287e;">.</span> <span style="color:#007020;font-weight:bold;">Left</span>) (liftM <span style="color:#007020;font-weight:bold;">Right</span> <span style="color:#06287e;">.</span> getData) reply
      <span style="color:#06287e;">either</span> handleError displayData result

<span style="color:#007020;">    untilRight </span><span style="color:#007020;">::</span> <span style="color:#007020;font-weight:bold;">Monad</span> m <span style="color:#007020;">=&gt;</span> [m (<span style="color:#902000;">Either</span> a b)] <span style="color:#007020;">-&gt;</span> m (<span style="color:#902000;">Either</span> a b)
    untilRight [m] <span style="color:#06287e;">=</span> m
    untilRight (m <span style="color:#06287e;">:</span> ms) <span style="color:#06287e;">=</span> m <span style="color:#06287e;">&gt;&gt;=</span> <span style="color:#06287e;">either</span> (<span style="color:#06287e;">const</span> (untilRight ms)) (<span style="color:#06287e;">return</span> <span style="color:#06287e;">.</span> <span style="color:#007020;font-weight:bold;">Right</span>)</code></pre>
</td>
</tr>
</table>
<p>Again, this could be cleaned up by adding some error reporting functionality into the monad stack.</p>
<p>Implementing the missing functions in terms of <code>connect</code> is straightforward. For example, <code>startRequest</code> will look something like this:</p>
<table class="sourceCode">
<tr>
<td style="border-right:1px solid #AAAAAA;text-align:right;color:#AAAAAA;padding-right:5px;padding-left:5px;">
<pre style="vertical-align:baseline;border:none;margin:0;padding:0;">1
2
3
4
5</pre>
</td>
<td class="sourceCode">
<pre style="vertical-align:baseline;border:none;margin:0;padding:0;"><code class="sourceCode haskell"><span style="color:#007020;">    startRequest </span><span style="color:#007020;">::</span> <span style="color:#902000;">Event</span> (<span style="color:#902000;">Either</span> <span style="color:#902000;">String</span> <span style="color:#902000;">Reply</span>)
    startRequest <span style="color:#06287e;">=</span> <span style="color:#902000;">Event</span> <span style="color:#06287e;">$</span> \k <span style="color:#007020;">-&gt;</span> <span style="color:#007020;font-weight:bold;">do</span>
      reply <span style="color:#007020;">&lt;-</span> AccessManager.get <span style="color:#4070a0;">&quot;http://example.net&quot;</span>
      connect reply <span style="color:#4070a0;">&quot;finished()&quot;</span> <span style="color:#06287e;">$</span> \_ <span style="color:#007020;">-&gt;</span> k (<span style="color:#007020;font-weight:bold;">Right</span> reply)
      connect reply <span style="color:#4070a0;">&quot;error(QString)&quot;</span> <span style="color:#06287e;">$</span> \e <span style="color:#007020;">-&gt;</span> k (<span style="color:#007020;font-weight:bold;">Left</span> e)</code></pre>
</td>
</tr>
</table>
<p>where I took the liberty of glossing over some irrelevant API details.</p>
<p>How do we run such a monad? Well, the standard <code>runContT</code> does the job:</p>
<table class="sourceCode">
<tr>
<td style="border-right:1px solid #AAAAAA;text-align:right;color:#AAAAAA;padding-right:5px;padding-left:5px;">
<pre style="vertical-align:baseline;border:none;margin:0;padding:0;">1
2</pre>
</td>
<td class="sourceCode">
<pre style="vertical-align:baseline;border:none;margin:0;padding:0;"><code class="sourceCode haskell"><span style="color:#007020;">    runEvent </span><span style="color:#007020;">::</span> <span style="color:#902000;">Event</span> () <span style="color:#007020;">-&gt;</span> <span style="color:#902000;">IO</span> ()
    runEvent e <span style="color:#06287e;">=</span> on <span style="color:#06287e;">$</span> \k <span style="color:#007020;">-&gt;</span> <span style="color:#06287e;">return</span> ()</code></pre>
</td>
</tr>
</table>
<p>so</p>
<table class="sourceCode">
<tr>
<td style="border-right:1px solid #AAAAAA;text-align:right;color:#AAAAAA;padding-right:5px;padding-left:5px;">
<pre style="vertical-align:baseline;border:none;margin:0;padding:0;">1</pre>
</td>
<td class="sourceCode">
<pre style="vertical-align:baseline;border:none;margin:0;padding:0;"><code class="sourceCode haskell">    runEvent ex</code></pre>
</td>
</tr>
</table>
<p>will run until the first connection, return control to the main loop, resume when an event occurs, and so on.</p>
<h2 id="conclusion">Conclusion</h2>
<p>I love the simplicity and elegance of this approach, but unfortunately, it is far from a complete solution. So far we have only dealt with &quot;one-shot&quot; events, but what happens when an event fires multiple times? Also, as this is still very imperative in nature, can we do better? Is it possible to employ a more functional style, with emphasis on composability?</p>
<p>I&#8217;ll leave the (necessarily partial) answers to those questions for a future post.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pcapriotti.wordpress.com/176/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pcapriotti.wordpress.com/176/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pcapriotti.wordpress.com/176/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pcapriotti.wordpress.com/176/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pcapriotti.wordpress.com/176/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pcapriotti.wordpress.com/176/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pcapriotti.wordpress.com/176/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pcapriotti.wordpress.com/176/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pcapriotti.wordpress.com/176/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pcapriotti.wordpress.com/176/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pcapriotti.wordpress.com/176/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pcapriotti.wordpress.com/176/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pcapriotti.wordpress.com/176/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pcapriotti.wordpress.com/176/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pcapriotti.wordpress.com&amp;blog=806312&amp;post=176&amp;subd=pcapriotti&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pcapriotti.wordpress.com/2012/01/18/reinversion-of-control-with-continuations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/472181d606a709332975dcceb974eba9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">pcapriotti</media:title>
		</media:content>
	</item>
		<item>
		<title>From event-driven programming to FRP</title>
		<link>http://pcapriotti.wordpress.com/2012/01/10/from-event-driven-programming-to-frp/</link>
		<comments>http://pcapriotti.wordpress.com/2012/01/10/from-event-driven-programming-to-frp/#comments</comments>
		<pubDate>Tue, 10 Jan 2012 20:22:31 +0000</pubDate>
		<dc:creator>pcapriotti</dc:creator>
				<category><![CDATA[kde]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[qt]]></category>
		<category><![CDATA[frp]]></category>

		<guid isPermaLink="false">http://pcapriotti.wordpress.com/?p=146</guid>
		<description><![CDATA[The problem Most of modern programming is based on events. Event-driven frameworks are the proven and true abstraction to express any kind of asynchronous and interactive behavior, like in GUIs or client-server architectures. The core idea is inversion of control: the main loop is run by the framework, users only have to register some form [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pcapriotti.wordpress.com&amp;blog=806312&amp;post=146&amp;subd=pcapriotti&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h2 id="the-problem">The problem</h2>
<p>Most of modern programming is based on events. Event-driven frameworks are the proven and true abstraction to express any kind of asynchronous and interactive behavior, like in GUIs or client-server architectures.</p>
<p>The core idea is <strong>inversion of control</strong>: the main loop is run by the framework, users only have to register some form of &#8220;callbacks&#8221;, and the framework will take care of calling them at the appropriate times.</p>
<p>This solves many issues that a straightforward imperative/procedural approach would present, eliminates the need for any kind of polling, and creates all sorts of opportunities for general-purpose optimizations inside the framework, with no impact on the complexity of user code. All of this without introducing any concurrency.</p>
<p>There are drawbacks, however. Event-driven code is hideous to write in most languages, especially those lacking support for first class closures. More importantly, event-driven code is extremely hard to reason about. The very nature of this callback-based approach makes it impossible to use a functional style, and even the simplest of interactions requires some form of mutable state which has to be maintained across callback calls.</p>
<p>As a very simple example, suppose we want to perform a GET request and retrieve some data, handling any HTTP error that might occur. In a generic event-driven frameworkm, we would need to implement a simple <strong>state machine</strong> whose graph will look somewhat like this:</p>
<p><a href="http://pcapriotti.files.wordpress.com/2012/01/state_machine1.png"><img src="http://pcapriotti.files.wordpress.com/2012/01/state_machine1.png?w=292&#038;h=215" alt="A simple state machine" title="State machine 1" width="292" height="215" class="aligncenter size-medium wp-image-152" /></a></p>
<p>Each state (except the initial one) corresponds to a callback. The transitions are determined by the framework. To avoid starting more than one request at a time, we will need to explicitly keep track of the current state.</p>
<p>Now let&#8217;s try to make a simple change to our program: suppose we want to retry requests when they fail, but not more than once. Now the state machine becomes more complicated, since we need to add extra nodes for the non-fatal error condition.</p>
<p><a href="http://pcapriotti.files.wordpress.com/2012/01/state_machine2.png"><img src="http://pcapriotti.files.wordpress.com/2012/01/state_machine2.png?w=247&#038;h=342" alt="A slightly more complicated state machine" title="State machine 2" width="247" height="342" class="aligncenter size-medium wp-image-153" /></a></p>
<p>In our hypotetical event-driven code, we need to keep track of whether we already encountered an error, check this flag at each callback to perform the right action, and update it appropriately. Moreover, this time the code isn&#8217;t even shaped exactly like the state machine, because we reuse the same callback for multiple nodes. To test our code exhaustively, we need to trace every possible path through the graph and reproduce it.</p>
<p>Now assume we want to allow simultaneous requests&#8230; you get the idea. The code gets unwieldy pretty fast. Small changes in requirements have devastating consequences in terms of the state graph. In practice, what happens most of the times is that the state graph is kept implicit, which makes the code impossible to test reliably, and consequently impossible to modify.</p>
<h2 id="towards-a-solution">Towards a solution</h2>
<p>A very simple but effective solution can be found by observing that state graphs like those of the previous examples have a very clear <a href="http://en.wikipedia.org/wiki/Operational_semantics">operational</a> interpretation in the context of the equivalent synchronous code.</p>
<p>A single forward transition from <code>A</code> to <code>B</code> can be simply modelled as the sequence <code>A;B</code>, i.e. execute <code>A</code>, <strong>then</strong> execute <code>B</code>. Extra outward transitions from a single node can be mapped to exceptions, while backward arrows can be thought of as looping constructs.</p>
<p>Our second state machine can then be translated to the following pseudopython:</p>
<p><pre class="brush: python;">
while True:
    for i in xrange(2):
        error = None
        try:
            reply = start_request()
            data = get_data(reply)
            break
        except Exception as e:
            error = get_error(e)
    if error:
        handle_error(error)
    else:
        display_data(data)
</pre></p>
<p>This code is straightforward. It could be made cleaner by splitting it up in a couple of extra functions and removing local state, but that&#8217;s beside the point. Note how easy it is now to generalize to an arbitrary number of retries.</p>
<p>So the key observation is that we can transform asynchronous code into synchronous-looking code, provided that we attach the correct semantics to sequencing of operations, exceptions and loops.</p>
<p>Now the question becomes: is it possible to do so?</p>
<p>We could turn functions like <code>start_request</code> and <code>get_data</code> into blocking operations that can throw. This will work <em>locally</em>, but it will break asynchronicity, so it&#8217;s not an option.</p>
<p>One way to salvage this transformation is to run the code in its own thread. Asynchronous operations will block, but won&#8217;t hang the main loop, and the rest of the program will continue execution.</p>
<p>However, we need to be careful with the kind of threads that we use. Since we don&#8217;t need (and don&#8217;t want!) to run multiple threads simultaneously, but we need to spawn a thread for each asynchronous operation, we have to make sure that the overhead is minimal, context switching is fast, and we&#8217;re not paying the cost of scheduling and synchronization.</p>
<p><a href="https://gist.github.com/1086172">Here</a> you can find a sketched solution along these lines that I wrote in python. It&#8217;s based on the <a href="http://pypi.python.org/pypi/greenlet">greenlet</a> library, which provides cooperative multithreading.</p>
<p>In the next post I will talk about alternative solutions, as well as how to extend the idea further, and make event-driven code more declarative and less procedural.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pcapriotti.wordpress.com/146/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pcapriotti.wordpress.com/146/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pcapriotti.wordpress.com/146/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pcapriotti.wordpress.com/146/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pcapriotti.wordpress.com/146/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pcapriotti.wordpress.com/146/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pcapriotti.wordpress.com/146/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pcapriotti.wordpress.com/146/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pcapriotti.wordpress.com/146/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pcapriotti.wordpress.com/146/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pcapriotti.wordpress.com/146/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pcapriotti.wordpress.com/146/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pcapriotti.wordpress.com/146/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pcapriotti.wordpress.com/146/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pcapriotti.wordpress.com&amp;blog=806312&amp;post=146&amp;subd=pcapriotti&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pcapriotti.wordpress.com/2012/01/10/from-event-driven-programming-to-frp/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/472181d606a709332975dcceb974eba9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">pcapriotti</media:title>
		</media:content>

		<media:content url="http://pcapriotti.files.wordpress.com/2012/01/state_machine1.png?w=292" medium="image">
			<media:title type="html">State machine 1</media:title>
		</media:content>

		<media:content url="http://pcapriotti.files.wordpress.com/2012/01/state_machine2.png?w=247" medium="image">
			<media:title type="html">State machine 2</media:title>
		</media:content>
	</item>
		<item>
		<title>Effective Qt in ruby (part 3)</title>
		<link>http://pcapriotti.wordpress.com/2010/12/06/effective-qt-in-ruby-part-3-2/</link>
		<comments>http://pcapriotti.wordpress.com/2010/12/06/effective-qt-in-ruby-part-3-2/#comments</comments>
		<pubDate>Mon, 06 Dec 2010 19:54:41 +0000</pubDate>
		<dc:creator>pcapriotti</dc:creator>
				<category><![CDATA[kaya]]></category>
		<category><![CDATA[kde]]></category>
		<category><![CDATA[qt]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pcapriotti.wordpress.com/?p=128</guid>
		<description><![CDATA[This is the third article in my series on writing Qt applications in ruby. I was planning to write about the declarative GUI system that I use in kaya, but a comment on one of my previous posts motivated me to take a small detour, and illustrate a very simple technique to extend a qtruby [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pcapriotti.wordpress.com&amp;blog=806312&amp;post=128&amp;subd=pcapriotti&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This is the third article in my series on writing Qt applications in ruby. I was planning to write about the declarative GUI system that I use in <a href="http://pcapriotti.github.com/kaya/">kaya</a>, but a comment on one of my previous posts motivated me to take a small detour, and illustrate a very simple technique to extend a qtruby application with C++ code.</p>
<p>So, suppose you need to expose a C++ function like:</p>
<p><pre class="brush: cpp;">
void applyEffect(QImage* img, float arg);
</pre></p>
<p>that takes a QImage, an argument, and applies a graphic effect, mutating the image in place.</p>
<p>Directly exposing this function to ruby using the extension API is not easy, because you need to extract a QImage pointer from the ruby object corresponding to the QImage, and that would require you to make assumptions on exactly how QObjects are wrapped by the ruby binding code, which is not ideal for a number of reasons.</p>
<p>Fortunately, there exists an elegant solution to this problem. First, you need to define your C++ function as a slot of some QObject. For example:</p>
<p><pre class="brush: cpp;">
class Extensions : public QObject
{
Q_OBJECT
public slots:
  void applyEffect(QImage* img, float arg) const;
};
</pre></p>
<p>Then in your extension initialization function you can instantiate it with something like:</p>
<p><pre class="brush: cpp;">
Extensions* ext = new Extensions(QCoreApplication::instance());
ext-&amp;gt;setObjectName(&amp;quot;__extensions__&amp;quot;);
</pre></p>
<p>And finally access it from ruby code and wrap it in a nicer package:</p>
<p><pre class="brush: ruby;">
$ext = $qApp.findChild(Qt::Object, &amp;quot;__extensions__&amp;quot;)
class Qt::Image
  def apply_effect(arg)
    $ext.applyEffect(self, arg)
  end
end
</pre></p>
<p>This works because Qt allows you to call slots dynamically using runtime introspection of QObjects. It&#8217;s not as fast as a native function call, but in the context of a ruby method call, the additional cost should be pretty much negligible.</p>
<p>Of course, unless your extension is particularly large and complicated, you don&#8217;t need to create an Extension object for each of the functions you want to expose: you can add all of them as slots in a single Extension object, which is loaded at startup, and create a ruby-esque API for them directly in ruby code.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pcapriotti.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pcapriotti.wordpress.com/128/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pcapriotti.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pcapriotti.wordpress.com/128/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pcapriotti.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pcapriotti.wordpress.com/128/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pcapriotti.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pcapriotti.wordpress.com/128/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pcapriotti.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pcapriotti.wordpress.com/128/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pcapriotti.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pcapriotti.wordpress.com/128/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pcapriotti.wordpress.com/128/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pcapriotti.wordpress.com/128/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pcapriotti.wordpress.com&amp;blog=806312&amp;post=128&amp;subd=pcapriotti&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pcapriotti.wordpress.com/2010/12/06/effective-qt-in-ruby-part-3-2/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/472181d606a709332975dcceb974eba9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">pcapriotti</media:title>
		</media:content>
	</item>
		<item>
		<title>Effective Qt in ruby (part 2)</title>
		<link>http://pcapriotti.wordpress.com/2010/09/24/effective-qt-in-ruby-part-2/</link>
		<comments>http://pcapriotti.wordpress.com/2010/09/24/effective-qt-in-ruby-part-2/#comments</comments>
		<pubDate>Fri, 24 Sep 2010 22:30:31 +0000</pubDate>
		<dc:creator>pcapriotti</dc:creator>
				<category><![CDATA[kaya]]></category>
		<category><![CDATA[kde]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pcapriotti.wordpress.com/?p=87</guid>
		<description><![CDATA[In the first part of this series, I listed some of the reasons why you should consider writing your Qt/KDE applications in ruby. This post details some of the technical differences between writing Qt code in C++ and in ruby. One of the first problems that pop up when starting a new Qt/KDE project in [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pcapriotti.wordpress.com&amp;blog=806312&amp;post=87&amp;subd=pcapriotti&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In the <a href="http://pcapriotti.wordpress.com/2010/06/25/effective-qt-in-ruby-part-1/">first part</a> of this series, I listed some of the reasons why you should consider writing your Qt/KDE applications in ruby. This post details some of the technical differences between writing Qt code in C++ and in ruby.</p>
<p>One of the first problems that pop up when starting a new Qt/KDE project in ruby is how to use it in such a way that your code doesn&#8217;t end up being completely unidiomatic. This can happen very easily if one tries to stick to the usual conventions that apply when writing Qt code in C++.</p>
<p>If you take any piece of C++ code using Qt, you can very trivially translate it into ruby. That works, and sometimes is useful, but writing code in this way completely misses the point of using a dynamic language. You might as well write directly in C++, and enjoy the improved performance.</p>
<p>So I believe it&#8217;s important to identify the baggage that Qt brings from its C++ roots, and eliminate it when using it from ruby. Here are some ideas to achieve that.</p>
<div id="use-the-ruby-convention-for-method-names">
<h2>Use the ruby convention for method names</h2>
<p>A minor point, but important for code readability.</p>
<p>Qt uses camel case for method names, while ruby methods are conventionally written with underscores. Mixing the two styles inevitably results in an unreadable mess, so the ruby convention should be used at all times.</p>
<p>Fortunately, QtRuby allows you to call C++ methods by spelling their name with underscores, so it&#8217;s quite easy to achieve a satisfactory level of consistency with minimum effort.</p>
</div>
<div id="never-declare-signals">
<h2>Never declare signals</h2>
<p>The signal/slot mechanism is a very important Qt feature, because it allows to work around the static nature of C++ by allowing dynamic calls to methods. You won&#8217;t need that in ruby. For instance, you can use the standard observer library to fire events and set callbacks. It&#8217;s completely dynamic and there&#8217;s no need to define your signals beforehand.</p>
</div>
<div id="never-use-slots">
<h2>Never use slots</h2>
<p>Slots are useless in ruby. QtRuby allows you to attach a block to a connect call, and that is what you should always be using. Never use the SLOT function with a C++ signature.</p>
</div>
<div id="avoid-c-signatures-altogether">
<h2>Avoid C++ signatures altogether</h2>
<p>This seems impossible. It might be easy to use symbols (without using the SIGNAL &quot;macro&quot;) to specify signals with no arguments, like</p>
<p>
<pre class="brush: ruby;">
button.on(:clicked) { puts &quot;hello world&quot; }
</pre>
</p>
<p>but if a signal has arguments, and possibly overloads, specifying only its name doesn&#8217;t seem to be enough to determine which particular overload we are interested in.</p>
<p>Indeed, it&#8217;s not possible in general, but you can disambiguate using the block arity for most overloaded signals, and add type annotations in those rare cases where the arity is not enough.</p>
<p>Here is my <code>on</code> method, which accomplishes this:</p>
<p>
<pre class="brush: ruby;">
def on(sig, types = nil, &amp;blk)
  sig = Signal.create(sig, types)
  candidates = if is_a? Qt::Object
    signal_map[sig.symbol]
  end
  if candidates
    if types
      # find candidate with the correct argument types
      candidates = candidates.find_all{|s| s[1] == types }
    end
    if candidates.size &gt; 1
      # find candidate with the correct arity
      arity = blk.arity
      if blk.arity == -1
        # take first
        candidates = [candidates.first]
      else
        candidates = candidates.find_all{|s| s[1].size == arity }
      end
    end
    if candidates.size &gt; 1
      raise &quot;Ambiguous overload for #{sig} with arity #{arity}&quot;
    elsif candidates.empty?
      msg = if types
        &quot;with types #{types.join(' ')}&quot;
      else
        &quot;with arity #{blk.arity}&quot;
      end
      raise &quot;No overload for #{sig} #{msg}&quot;
    end
    sign = SIGNAL(candidates.first[0])
    connect(sign, &amp;blk)
    SignalDisconnecter.new(self, sign)
  else
    observer = observe(sig.symbol, &amp;blk)
    ObserverDisconnecter.new(self, observer)
  end
end
</pre>
</p>
<p>The <code>Signal</code> class maintains the signal name and (optional) specified types. The method lazily creates a <em>signal map</em> for each class, which maps symbols to C++ signatures, and proceeds to disambiguate among all the possibilities by using types, or just the block arity, when no explicit types are provided. If no signal is found, or if the ambiguity could not be resolved, an exception is thrown.</p>
<p>For example, the following line:</p>
<p>
<pre class="brush: ruby;">
combobox.on(:current_index_changed, [&quot;int&quot;]) {|i| self.index = i }
</pre>
</p>
<p>is referring to <code>currentIndexChanged(int)</code> and not to the other possible signal <code>currentIndexChanged(QString)</code>, because of the explicit type annotation.</p>
<p>The advantage of this trick is that I can write, for example:</p>
<p>
<pre class="brush: ruby;">
model.on(:rows_about_to_be_inserted) do |p, i, j|
  # ...
end
</pre>
</p>
<p>without specifying any C++ signature, which in this case would be quite hefty:</p>
<p>
<pre class="brush: cpp;">
rowsAboutToBeInserted(const QModelIndex&amp; parent, int start, int end)
</pre>
</p>
</div>
<div id="conclusion">
<h2>Conclusion</h2>
<p>QtRuby is an exceptional library, but to use it effectively you need to let go of some of the established practices of Qt programming in C++, and embrace the greater dynamicity of ruby.</p>
<p>In the next article I&#8217;ll show you how I tried to push this idea to the extreme with <em>AutoGUI</em>, a declarative GUI DSL built on top of QtRuby.</p>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pcapriotti.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pcapriotti.wordpress.com/87/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pcapriotti.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pcapriotti.wordpress.com/87/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pcapriotti.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pcapriotti.wordpress.com/87/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pcapriotti.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pcapriotti.wordpress.com/87/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pcapriotti.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pcapriotti.wordpress.com/87/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pcapriotti.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pcapriotti.wordpress.com/87/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pcapriotti.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pcapriotti.wordpress.com/87/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pcapriotti.wordpress.com&amp;blog=806312&amp;post=87&amp;subd=pcapriotti&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pcapriotti.wordpress.com/2010/09/24/effective-qt-in-ruby-part-2/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/472181d606a709332975dcceb974eba9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">pcapriotti</media:title>
		</media:content>
	</item>
		<item>
		<title>Effective Qt in ruby (part 1)</title>
		<link>http://pcapriotti.wordpress.com/2010/06/25/effective-qt-in-ruby-part-1/</link>
		<comments>http://pcapriotti.wordpress.com/2010/06/25/effective-qt-in-ruby-part-1/#comments</comments>
		<pubDate>Fri, 25 Jun 2010 23:36:48 +0000</pubDate>
		<dc:creator>pcapriotti</dc:creator>
				<category><![CDATA[kaya]]></category>
		<category><![CDATA[kde]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pcapriotti.wordpress.com/?p=64</guid>
		<description><![CDATA[As some of you might know, I&#8217;m working on a generic board game platform called Kaya. Kaya is a Qt/KDE-based application to play chess, shogi and variants thereof, and easily extensible to all sorts of board games (Go is in the works, for example). Kaya is written in ruby, and I have learned quite a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pcapriotti.wordpress.com&amp;blog=806312&amp;post=64&amp;subd=pcapriotti&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>As some of you might know, I&#8217;m working on a generic board game platform called <a href="http://pcapriotti.github.com/kaya">Kaya</a>. Kaya is a Qt/KDE-based application to play <a href="http://en.wikipedia.org/wiki/Chess">chess</a>, <a href="http://en.wikipedia.org/wiki/Shogi">shogi</a> and variants thereof, and easily extensible to all sorts of board games (<a href="http://en.wikipedia.org/wiki/Go_%28game%29">Go</a> is in the works, for example).</p>
<p>Kaya is written in ruby, and I have learned quite a few things about writing non-trivial GUI applications in ruby while working on it, so I decided to share some of my experience and code in the hope that it might be useful to others, and possibly inspire other Qt/KDE developers to try out ruby for their next project.</p>
<h2>Advantages</h2>
<p>Here is a list of what I think are the most important points that make programming GUIs in ruby so much more productive than in C++. I&#8217;ll leave out subjective arguments like &#8220;<em>it&#8217;s more fun</em>&#8221; or &#8220;<em>it has a nicer syntax</em>&#8221; because I think that the actual facts are more than compelling already.</p>
<h3>Fast Prototyping</h3>
<p>This is not specific to GUI programming. It is greatly aknowledged that ruby is orders of magnitude more convenient than C++ for throwing quick scripts together and in general for trying new ideas out. This turns out to be very important in a GUI context as well.</p>
<p>For example, it is very easy to write setup code for a single component of your application so that you can run it standalone and test it more efficiently. I have found myself resorting to this kind of trick very often, and it sometimes makes debugging a lot less painful. In C++, it would be unreasonable to write a new application skeleton (and alter your build scripts) just to test a new dialog you are developing.</p>
<p>Another example is &#8220;<em>fancy logging</em>&#8220;. If something breaks in the middle of complicated or highly interactive code, sometimes printing to the console or setting breakpoints doesn&#8217;t quite cut it. In these cases, it is a very good idea to hack together a simple but powerful visualization tool to show the internal state of the application in real time, and that is usually very helpful to identify the issue. Again, since this is basically throwaway code, ease of prototyping is very important.</p>
<h3>Declarative GUI Definition</h3>
<p>Ruby has very powerful metaprogramming facilities that make creating an embedded DSL extremely straightforward. I use a simple mechanism in Kaya that allows me to write things like:</p>
<p><pre class="brush: ruby;">
    @gui = KDE::autogui(:engine_prefs,
                        :caption =&gt; KDE.i18n(&quot;Configure Engines&quot;)) do |g|
      g.layout(:type =&gt; :horizontal) do |l|
        l.list(:list)
        l.layout(:type =&gt; :vertical) do |buttons|
          buttons.button(:add_engine,
                         :text =&gt; KDE.i18nc(&quot;engine&quot;, &quot;&amp;New...&quot;),
                         :icon =&gt; 'list-add')
          buttons.button(:edit_engine,
                         :text =&gt; KDE.i18nc(&quot;engine&quot;, &quot;&amp;Edit...&quot;),
                         :icon =&gt; 'configure')
          buttons.button(:delete_engine,
                         :text =&gt; KDE.i18nc(&quot;engine&quot;, &quot;&amp;Delete&quot;),
                         :icon =&gt; 'list-remove')
          buttons.stretch
        end
      end
    end
    setGUI(@gui)
</pre></p>
<p>I find it very convenient to define GUIs at this slightly higher level of abstraction, plus you have none of the boilerplate code typical of GUI construction in C++.</p>
<p>Defining GUIs in this way is so quick (and it&#8217;s easy to run them to immediately see the results), that it makes tools like Qt Designer completely redundant, in my opinion, except possibly when GUI design and coding are done by different people.</p>
<h3>Using Closures for Slots</h3>
<p>Take a look at this simple example, and try to imagine how many lines of code would be needed to implement it in C++:<br />
<pre class="brush: ruby;">
win.display.text = &quot;0&quot;
inc = 1
Qt::Timer.every(1000) do
  win.display.text = (win.display.text.to_i + inc).to_s
end
win.button.on(:clicked) { inc = -inc }
</pre></p>
<p>Here <code>win.display</code> is a <code>QTextEdit</code> and <code>win.button</code> is a <code>QPushButton</code>. What the example does is count up seconds in the <code>QTextEdit</code>, and toggle between that and counting down when the button is pressed.</p>
<p>Pretty trivial, granted, but in C++ you would need to define two slots to do that, plus keep track of the direction in which you are counting by using a member variable. In ruby you can just use local variables without littering the parent scope.</p>
<h3>Toolkit Independence</h3>
<p>If you write your application in C++, once you&#8217;ve decided that you are going to use KDElibs (or Qt), that decision is pretty much set in stone. You can&#8217;t even switch from KDE to pure Qt very easily, and it&#8217;s so hard to support both environments in a single code base, to make it almost completely not worth the effort.</p>
<p>Of course, this might not seem that big of an issue, given that KDE is now a lot more portable, but not many people are running KDE SC on Windows or Mac at the moment, and if you want to reach as many users as possible, having a Qt-only version of your application is going to help a lot.</p>
<p>So Kaya can currently run on Qt-only as well as KDE. When in KDE mode, all the usual KDE goodies are running under the hood: KPushButtons, KDialog, K-everything, and of course KXMLGUI and all the good stuff that comes with it, but it can switch to plain Qt classes and a simplistic replacement for the XML GUI if you don&#8217;t have KDElibs installed.</p>
<h3>Easier Automatic Testing</h3>
<p>Dynamicity, mock objects, plus the whole culture of test-driven development that characterizes the ruby ecosystem make it a lot easier to devise automated tests for your application. Effective GUI testing is still basically an open problem in software engineering, so don&#8217;t expect it to be a piece of cake, but in my experience, you can definitely reach a considerably higher test coverage with ruby than with C++.</p>
<h3>Trivial Extensibility Through Plugins</h3>
<p>KDE really shines when it comes to making applications easily extensible via scripts or plugins, but it can&#8217;t compare with ruby. The distinction between plugins and user scripts is now nonexistent, and loading a plugin is just a matter of calling the ruby <code>load</code> function.</p>
<p>Creating a sensibly extensible application still requires careful planning, of course, but it&#8217;s a lot easier when you can directly expose application functionality to plugins, without creating tons and tons of interfaces for even the most trivial uses.</p>
<p>In Kaya, the use of a dynamically typed language turned out to be key: the flexibility required to make its plugins easy enough to write without knowing much about the application internals is pretty much impossible to achieve in a statically typed context.</p>
<h2>Disadvantages</h2>
<p>Of course, like everything in software engineering, choosing ruby over C++ for GUI development involves a number of tradeoffs.</p>
<h3>Performance</h3>
<p>Everyone knows that ruby is slow. Painfully slow sometimes. That seems to be improving with 1.9, but ruby isn&#8217;t going to get faster than C anytime soon.</p>
<p>Now, the good thing is that its poor performance is almost always completely irrelevant. A typical GUI application is nowhere near being CPU bound, and the few computationally intensive parts are usually in the GUI library anyway. That said, if you have performance critical sections in your application, you are better off writing them in C/C++ and accessing them from your ruby code using the ruby extension API.</p>
<p>For example, Kaya includes a small C++ extension to supplement the missing blur functionality in Qt &lt; 4.6. Writing it and integrating it was trivial.</p>
<h3>Testing is Essential</h3>
<p>Automated testing is very important for software written in any language, but for dynamic languages you simply can&#8217;t do without it. Untested code will inevitably contain silly typos and type errors which will cause it to crash in your user&#8217;s faces, or in the best case will make your testing sessions painful and slow.</p>
<p>So you don&#8217;t have any choice but to add lots and lots of unit tests, integration tests and the like. Units tests for pure GUI code are not really effective, useful or easy to write, but you can settle on smoke tests that will cover the most common mistakes and be pretty confident that no silly errors will pop up that a compiler would catch.</p>
<h2>Conclusion</h2>
<p>I hope this gives a nice overview of the benefits you can get by switching from C++ to a dynamic language for your KDE (or general GUI) development. I used ruby as the main example, because that&#8217;s what I have experience with, but most of the points probably apply to dynamic languages in general (python, perl, clojure, etc).</p>
<p>In the following posts, I will dig a little bit more into Kaya&#8217;s code to show the kinds of tricks that I employed to better exploit the advantages that I discussed, and offer even more ways to get the most out of ruby for GUI programming. Stay tuned!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pcapriotti.wordpress.com/64/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pcapriotti.wordpress.com/64/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pcapriotti.wordpress.com/64/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pcapriotti.wordpress.com/64/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pcapriotti.wordpress.com/64/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pcapriotti.wordpress.com/64/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pcapriotti.wordpress.com/64/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pcapriotti.wordpress.com/64/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pcapriotti.wordpress.com/64/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pcapriotti.wordpress.com/64/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pcapriotti.wordpress.com/64/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pcapriotti.wordpress.com/64/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pcapriotti.wordpress.com/64/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pcapriotti.wordpress.com/64/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pcapriotti.wordpress.com&amp;blog=806312&amp;post=64&amp;subd=pcapriotti&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pcapriotti.wordpress.com/2010/06/25/effective-qt-in-ruby-part-1/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/472181d606a709332975dcceb974eba9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">pcapriotti</media:title>
		</media:content>
	</item>
		<item>
		<title>Monads for Markov chains</title>
		<link>http://pcapriotti.wordpress.com/2008/10/16/monads-for-markov-chains/</link>
		<comments>http://pcapriotti.wordpress.com/2008/10/16/monads-for-markov-chains/#comments</comments>
		<pubDate>Thu, 16 Oct 2008 13:11:06 +0000</pubDate>
		<dc:creator>pcapriotti</dc:creator>
				<category><![CDATA[haskell]]></category>
		<category><![CDATA[literate programming]]></category>
		<category><![CDATA[markov chains]]></category>
		<category><![CDATA[monads]]></category>

		<guid isPermaLink="false">http://pcapriotti.wordpress.com/?p=46</guid>
		<description><![CDATA[This is my first literate Haskell post, and actually my first post on Haskell, and my first attempt at literate programming. I spent (wasted?) a lot of time in trying to make the code segments render nicely on WordPress, with a process that was only partially automated and very tedious. I think I will cook [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pcapriotti.wordpress.com&amp;blog=806312&amp;post=46&amp;subd=pcapriotti&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><em>This is my first literate Haskell post, and actually my first post on Haskell, and my first attempt at literate programming. I spent (wasted?) a lot of time in trying to make the code segments render nicely on WordPress, with a process that was only partially automated and very tedious. I think I will cook up some script to automate it completely, if I ever get to write another such post.</em></p>
<p>Suppose you need to model a finite <a href="http://en.wikipedia.org/wiki/Markov_chain">Markov chain</a> in code. There are essentially two ways of doing that: one is to simply run a simulation of the Markov chain using a random number generator to obtain dice rolls and random cards from the decks, the other is to create a <a href="http://en.wikipedia.org/wiki/Stochastic_matrix">stochastic matrix</a> containing the transition probabilities for each pair of states.</p>
<p>In this post I will show how a single monadic description of the Markov chain dynamics can be used to obtain both a simulator and the transition matrix.</p>
<pre style="font-size:140%;"><span style="font-style:italic;color:#408080;">&gt; {-# LANGUAGE MultiParamTypeClasses, </span>
<span style="font-style:italic;color:#408080;">&gt;     FlexibleInstances, </span>
<span style="font-style:italic;color:#408080;">&gt;     GeneralizedNewtypeDeriving #-}</span>
<span style="font-style:italic;color:#408080;">&gt;</span>
<span style="font-style:italic;color:#408080;">&gt; </span><span style="font-weight:bold;color:#008000;">import</span> <span style="font-weight:bold;color:#0000ff;">Control.Arrow</span>
<span style="font-style:italic;color:#408080;">&gt; </span><span style="font-weight:bold;color:#008000;">import</span> <span style="font-weight:bold;color:#0000ff;">Control.Monad</span>
<span style="font-style:italic;color:#408080;">&gt; </span><span style="font-weight:bold;color:#008000;">import</span> <span style="font-weight:bold;color:#0000ff;">Control.Monad.State.Strict</span>
<span style="font-style:italic;color:#408080;">&gt; </span><span style="font-weight:bold;color:#008000;">import</span> <span style="font-weight:bold;color:#0000ff;">Data.Array</span>
<span style="font-style:italic;color:#408080;">&gt; </span><span style="font-weight:bold;color:#008000;">import</span> <span style="font-weight:bold;color:#0000ff;">Random</span></pre>
<p>Let&#8217;s start with an example of Markov chain and how we would like to be able to implement in Haskell. Consider a simplified version of the familiar <a href="http://en.wikipedia.org/wiki/Monopoly_(game)">Monopoly</a> game: there are 40 squares (numbered 0 to 39), you throw two 6-sided dice each turn, some special squares have particular effects (see below), if you get a double roll three times in a row, you go to jail. The special squares are:</p>
<p>30: go to jail<br />
2, 17, 33: Community Chest<br />
7, 22, 36: Chance</p>
<p>Community Chest (CC) and Chance (CH) make you take a card from a deck and move to some other place depending on what&#8217;s written on the card. You will find the details on the code, so I won&#8217;t explain them here.</p>
<p>This is of course a Markov chain, where the states can be represented by:</p>
<pre style="font-size:140%;"><span style="font-style:italic;color:#408080;">&gt; </span><span style="font-weight:bold;color:#008000;">type</span> <span style="color:#b00040;">Square</span> <span style="font-weight:bold;color:#aa22ff;">=</span> <span style="color:#b00040;">Int</span>
<span style="font-style:italic;color:#408080;">&gt; </span><span style="font-weight:bold;color:#008000;">data</span> <span style="color:#b00040;">GameState</span> <span style="font-weight:bold;color:#aa22ff;">=</span> <span style="color:#b00040;">GS</span> {
<span style="font-style:italic;color:#408080;">&gt;       </span><span style="color:#0000ff;">position</span> <span style="font-weight:bold;color:#aa22ff;">::</span> <span style="color:#b00040;">Square</span>,
<span style="font-style:italic;color:#408080;">&gt;       </span><span style="color:#0000ff;">doubles</span> <span style="font-weight:bold;color:#aa22ff;">::</span> <span style="color:#b00040;">Int</span> } <span style="font-weight:bold;color:#008000;">deriving</span> (<span style="color:#b00040;">Eq</span>, <span style="color:#b00040;">Ord</span>, <span style="color:#b00040;">Show</span>)</pre>
<p>and a description of the game can be given in a monadic style like this:</p>
<pre style="font-size:140%;"><span style="font-style:italic;color:#408080;">&gt; </span><span style="color:#0000ff;">sGO</span> <span style="font-weight:bold;color:#aa22ff;">::</span> <span style="color:#b00040;">Square</span>
<span style="font-style:italic;color:#408080;">&gt; </span><span style="color:#0000ff;">sGO</span> <span style="font-weight:bold;color:#aa22ff;">=</span> <span style="color:#666666;">0</span>
<span style="font-style:italic;color:#408080;">&gt; </span>
<span style="font-style:italic;color:#408080;">&gt; </span><span style="color:#0000ff;">sJAIL</span> <span style="font-weight:bold;color:#aa22ff;">::</span> <span style="color:#b00040;">Square</span>
<span style="font-style:italic;color:#408080;">&gt; </span><span style="color:#0000ff;">sJAIL</span> <span style="font-weight:bold;color:#aa22ff;">=</span> <span style="color:#666666;">10</span>
<span style="font-style:italic;color:#408080;">&gt;</span>
<span style="font-style:italic;color:#408080;">&gt; </span><span style="color:#0000ff;">finalize</span> <span style="font-weight:bold;color:#aa22ff;">::</span> <span style="color:#b00040;">Square</span> <span style="font-weight:bold;color:#aa22ff;">-&gt;</span> <span style="color:#b00040;">Game</span> <span style="color:#b00040;">Square</span>
<span style="font-style:italic;color:#408080;">&gt; </span><span style="color:#0000ff;">finalize</span> n
<span style="font-style:italic;color:#408080;">&gt;     </span><span style="color:#666666;">|</span> n <span style="color:#666666;">==</span> <span style="color:#666666;">2</span> <span style="color:#666666;">||</span> n <span style="color:#666666;">==</span> <span style="color:#666666;">17</span> <span style="color:#666666;">||</span> n <span style="color:#666666;">==</span> <span style="color:#666666;">33</span> <span style="font-weight:bold;color:#aa22ff;">=</span> cc n
<span style="font-style:italic;color:#408080;">&gt;     </span><span style="color:#666666;">|</span> n <span style="color:#666666;">==</span> <span style="color:#666666;">7</span> <span style="color:#666666;">||</span> n <span style="color:#666666;">==</span> <span style="color:#666666;">22</span> <span style="color:#666666;">||</span> n <span style="color:#666666;">==</span> <span style="color:#666666;">36</span> <span style="font-weight:bold;color:#aa22ff;">=</span> ch n
<span style="font-style:italic;color:#408080;">&gt;     </span><span style="color:#666666;">|</span> n <span style="color:#666666;">==</span> <span style="color:#666666;">30</span> <span style="font-weight:bold;color:#aa22ff;">=</span> return sJAIL
<span style="font-style:italic;color:#408080;">&gt;     </span><span style="color:#666666;">|</span> otherwise <span style="font-weight:bold;color:#aa22ff;">=</span> return n
<span style="font-style:italic;color:#408080;">&gt; </span>
<span style="font-style:italic;color:#408080;">&gt; </span><span style="color:#0000ff;">cc</span> <span style="font-weight:bold;color:#aa22ff;">::</span> <span style="color:#b00040;">Square</span> <span style="font-weight:bold;color:#aa22ff;">-&gt;</span> <span style="color:#b00040;">Game</span> <span style="color:#b00040;">Square</span>
<span style="font-style:italic;color:#408080;">&gt; </span><span style="color:#0000ff;">cc</span> n <span style="font-weight:bold;color:#aa22ff;">=</span> <span style="font-weight:bold;color:#008000;">do</span> i <span style="font-weight:bold;color:#aa22ff;">&lt;-</span> choose (<span style="color:#666666;">1</span> <span style="font-weight:bold;color:#aa22ff;">::</span> <span style="color:#b00040;">Int</span>, <span style="color:#666666;">16</span>)
<span style="font-style:italic;color:#408080;">&gt;           </span><span style="color:#0000ff;">return</span> <span style="color:#666666;">$</span> <span style="font-weight:bold;color:#008000;">case</span> i <span style="font-weight:bold;color:#008000;">of</span>
<span style="font-style:italic;color:#408080;">&gt;                      </span><span style="color:#666666;">1</span> <span style="font-weight:bold;color:#aa22ff;">-&gt;</span> sGO
<span style="font-style:italic;color:#408080;">&gt;                      </span><span style="color:#666666;">2</span> <span style="font-weight:bold;color:#aa22ff;">-&gt;</span> sJAIL
<span style="font-style:italic;color:#408080;">&gt;                      </span><span style="font-weight:bold;color:#008000;">_</span> <span style="font-weight:bold;color:#aa22ff;">-&gt;</span> n
<span style="font-style:italic;color:#408080;">&gt; </span>
<span style="font-style:italic;color:#408080;">&gt; </span><span style="color:#0000ff;">ch</span> <span style="font-weight:bold;color:#aa22ff;">::</span> <span style="color:#b00040;">Square</span> <span style="font-weight:bold;color:#aa22ff;">-&gt;</span> <span style="color:#b00040;">Game</span> <span style="color:#b00040;">Square</span>
<span style="font-style:italic;color:#408080;">&gt; </span><span style="color:#0000ff;">ch</span> n <span style="font-weight:bold;color:#aa22ff;">=</span> <span style="font-weight:bold;color:#008000;">do</span> i <span style="font-weight:bold;color:#aa22ff;">&lt;-</span> choose (<span style="color:#666666;">1</span> <span style="font-weight:bold;color:#aa22ff;">::</span> <span style="color:#b00040;">Int</span>, <span style="color:#666666;">16</span>)
<span style="font-style:italic;color:#408080;">&gt;           </span><span style="color:#0000ff;">return</span> <span style="color:#666666;">$</span> <span style="font-weight:bold;color:#008000;">case</span> i <span style="font-weight:bold;color:#008000;">of</span>
<span style="font-style:italic;color:#408080;">&gt;                      </span><span style="color:#666666;">1</span> <span style="font-weight:bold;color:#aa22ff;">-&gt;</span> sGO
<span style="font-style:italic;color:#408080;">&gt;                      </span><span style="color:#666666;">2</span> <span style="font-weight:bold;color:#aa22ff;">-&gt;</span> sJAIL
<span style="font-style:italic;color:#408080;">&gt;                      </span><span style="color:#666666;">3</span> <span style="font-weight:bold;color:#aa22ff;">-&gt;</span> <span style="color:#666666;">11</span>
<span style="font-style:italic;color:#408080;">&gt;                      </span><span style="color:#666666;">4</span> <span style="font-weight:bold;color:#aa22ff;">-&gt;</span> <span style="color:#666666;">24</span>
<span style="font-style:italic;color:#408080;">&gt;                      </span><span style="color:#666666;">5</span> <span style="font-weight:bold;color:#aa22ff;">-&gt;</span> <span style="color:#666666;">39</span>
<span style="font-style:italic;color:#408080;">&gt;                      </span><span style="color:#666666;">6</span> <span style="font-weight:bold;color:#aa22ff;">-&gt;</span> <span style="color:#666666;">5</span>
<span style="font-style:italic;color:#408080;">&gt;                      </span><span style="color:#666666;">7</span> <span style="font-weight:bold;color:#aa22ff;">-&gt;</span> nextR n
<span style="font-style:italic;color:#408080;">&gt;                      </span><span style="color:#666666;">8</span> <span style="font-weight:bold;color:#aa22ff;">-&gt;</span> nextR n
<span style="font-style:italic;color:#408080;">&gt;                      </span><span style="color:#666666;">9</span> <span style="font-weight:bold;color:#aa22ff;">-&gt;</span> nextU n
<span style="font-style:italic;color:#408080;">&gt;                      </span><span style="color:#666666;">10</span> <span style="font-weight:bold;color:#aa22ff;">-&gt;</span> n <span style="color:#666666;">-</span> <span style="color:#666666;">3</span>
<span style="font-style:italic;color:#408080;">&gt;                      </span><span style="font-weight:bold;color:#008000;">_</span> <span style="font-weight:bold;color:#aa22ff;">-&gt;</span> n
<span style="font-style:italic;color:#408080;">&gt;     </span><span style="font-weight:bold;color:#008000;">where</span>
<span style="font-style:italic;color:#408080;">&gt;       </span><span style="color:#0000ff;">nextR</span> n <span style="font-weight:bold;color:#aa22ff;">=</span> <span style="font-weight:bold;color:#008000;">let</span> n' <span style="font-weight:bold;color:#aa22ff;">=</span> n <span style="color:#666666;">+</span> <span style="color:#666666;">5</span>
<span style="font-style:italic;color:#408080;">&gt;                 </span><span style="font-weight:bold;color:#008000;">in</span> n' <span style="color:#666666;">-</span> (n' `mod` <span style="color:#666666;">5</span>)
<span style="font-style:italic;color:#408080;">&gt;       </span><span style="color:#0000ff;">nextU</span> n
<span style="font-style:italic;color:#408080;">&gt;           </span><span style="color:#666666;">|</span> n <span style="color:#666666;">&gt;=</span> <span style="color:#666666;">12</span> <span style="color:#666666;">&amp;&amp;</span> n <span style="color:#666666;">&lt;</span> <span style="color:#666666;">28</span> <span style="font-weight:bold;color:#aa22ff;">=</span> <span style="color:#666666;">28</span>
<span style="font-style:italic;color:#408080;">&gt;           </span><span style="color:#666666;">|</span> otherwise <span style="font-weight:bold;color:#aa22ff;">=</span> <span style="color:#666666;">12</span>
<span style="font-style:italic;color:#408080;">&gt; </span>
<span style="font-style:italic;color:#408080;">&gt; </span><span style="color:#0000ff;">roll</span> <span style="font-weight:bold;color:#aa22ff;">::</span> <span style="color:#b00040;">Game</span> (<span style="color:#b00040;">Int</span>, <span style="color:#b00040;">Int</span>)
<span style="font-style:italic;color:#408080;">&gt; </span><span style="color:#0000ff;">roll</span> <span style="font-weight:bold;color:#aa22ff;">=</span> <span style="font-weight:bold;color:#008000;">let</span> r1 <span style="font-weight:bold;color:#aa22ff;">=</span> choose (<span style="color:#666666;">1</span>, <span style="color:#666666;">6</span>)
<span style="font-style:italic;color:#408080;">&gt;        </span><span style="font-weight:bold;color:#008000;">in</span> liftM2 (,) r1 r1
<span style="font-style:italic;color:#408080;">&gt; </span>
<span style="font-style:italic;color:#408080;">&gt; </span><span style="color:#0000ff;">markDouble</span> <span style="font-weight:bold;color:#aa22ff;">::</span> <span style="color:#b00040;">Bool</span> <span style="font-weight:bold;color:#aa22ff;">-&gt;</span> <span style="color:#b00040;">Game</span> <span style="color:#008000;">()</span>
<span style="font-style:italic;color:#408080;">&gt; </span><span style="color:#0000ff;">markDouble</span> <span style="color:#b00040;">True</span> <span style="font-weight:bold;color:#aa22ff;">=</span> modify <span style="color:#666666;">$</span> <span style="color:#0000ff;">\</span>s <span style="font-weight:bold;color:#aa22ff;">-&gt;</span> s {
<span style="font-style:italic;color:#408080;">&gt;                     </span><span style="color:#0000ff;">doubles</span> <span style="font-weight:bold;color:#aa22ff;">=</span> doubles s <span style="color:#666666;">+</span> <span style="color:#666666;">1</span> }
<span style="font-style:italic;color:#408080;">&gt; </span><span style="color:#0000ff;">markDouble</span> <span style="color:#b00040;">False</span> <span style="font-weight:bold;color:#aa22ff;">=</span> modify <span style="color:#666666;">$</span> <span style="color:#0000ff;">\</span>s <span style="font-weight:bold;color:#aa22ff;">-&gt;</span> s {
<span style="font-style:italic;color:#408080;">&gt;                      </span><span style="color:#0000ff;">doubles</span> <span style="font-weight:bold;color:#aa22ff;">=</span> <span style="color:#666666;">0</span>
<span style="font-style:italic;color:#408080;">&gt;                    </span>}
<span style="font-style:italic;color:#408080;">&gt;</span>
<span style="font-style:italic;color:#408080;">&gt; </span><span style="color:#0000ff;">goTo</span> <span style="font-weight:bold;color:#aa22ff;">::</span> <span style="color:#b00040;">Square</span> <span style="font-weight:bold;color:#aa22ff;">-&gt;</span> <span style="color:#b00040;">Game</span> <span style="color:#008000;">()</span>
<span style="font-style:italic;color:#408080;">&gt; </span><span style="color:#0000ff;">goTo</span> n <span style="font-weight:bold;color:#aa22ff;">=</span> <span style="font-weight:bold;color:#008000;">let</span> n' <span style="font-weight:bold;color:#aa22ff;">=</span> n `mod` <span style="color:#666666;">40</span>
<span style="font-style:italic;color:#408080;">&gt;          </span><span style="font-weight:bold;color:#008000;">in</span> modify <span style="color:#666666;">$</span> <span style="color:#0000ff;">\</span>s <span style="font-weight:bold;color:#aa22ff;">-&gt;</span> s { position <span style="font-weight:bold;color:#aa22ff;">=</span> n' }
<span style="font-style:italic;color:#408080;">&gt; </span>
<span style="font-style:italic;color:#408080;">&gt; </span><span style="color:#0000ff;">game</span> <span style="font-weight:bold;color:#aa22ff;">::</span> <span style="color:#b00040;">Game</span> <span style="color:#008000;">()</span>
<span style="font-style:italic;color:#408080;">&gt; </span><span style="color:#0000ff;">game</span> <span style="font-weight:bold;color:#aa22ff;">=</span> <span style="font-weight:bold;color:#008000;">do</span> n <span style="font-weight:bold;color:#aa22ff;">&lt;-</span> liftM position get
<span style="font-style:italic;color:#408080;">&gt;           </span>(a, b) <span style="font-weight:bold;color:#aa22ff;">&lt;-</span> roll
<span style="font-style:italic;color:#408080;">&gt;           </span><span style="color:#0000ff;">markDouble</span> (a <span style="color:#666666;">==</span> b)
<span style="font-style:italic;color:#408080;">&gt;           </span><span style="color:#0000ff;">d</span> <span style="font-weight:bold;color:#aa22ff;">&lt;-</span> liftM doubles get
<span style="font-style:italic;color:#408080;">&gt;           </span><span style="font-weight:bold;color:#008000;">if</span> d <span style="color:#666666;">==</span> <span style="color:#666666;">3</span>
<span style="font-style:italic;color:#408080;">&gt;            </span><span style="font-weight:bold;color:#008000;">then</span> <span style="font-weight:bold;color:#008000;">do</span> markDouble <span style="color:#b00040;">False</span>
<span style="font-style:italic;color:#408080;">&gt;                    </span><span style="color:#0000ff;">goTo</span> sJAIL
<span style="font-style:italic;color:#408080;">&gt;            </span><span style="font-weight:bold;color:#008000;">else</span> <span style="font-weight:bold;color:#008000;">do</span> <span style="font-weight:bold;color:#008000;">let</span> n' <span style="font-weight:bold;color:#aa22ff;">=</span> n <span style="color:#666666;">+</span> a <span style="color:#666666;">+</span> b
<span style="font-style:italic;color:#408080;">&gt;                    </span><span style="color:#0000ff;">n''</span> <span style="font-weight:bold;color:#aa22ff;">&lt;-</span> finalize n'
<span style="font-style:italic;color:#408080;">&gt;                    </span><span style="color:#0000ff;">goTo</span> n''
<span style="font-style:italic;color:#408080;">&gt; </span></pre>
<p>As you can see, <code>Game</code> is a state monad, with an additional function <code>choose</code> that gives us a random element of a range:</p>
<pre style="font-size:140%;"><span style="font-style:italic;color:#408080;">&gt; </span><span style="font-weight:bold;color:#008000;">class</span> <span style="color:#b00040;">MonadState</span> s m <span style="font-weight:bold;color:#aa22ff;">=&gt;</span> <span style="color:#b00040;">MonadMC</span> s m <span style="font-weight:bold;color:#008000;">where</span>
<span style="font-style:italic;color:#408080;">&gt;     </span><span style="color:#0000ff;">choose</span> <span style="font-weight:bold;color:#aa22ff;">::</span> (<span style="color:#b00040;">Enum</span> a) <span style="font-weight:bold;color:#aa22ff;">=&gt;</span> (a, a) <span style="font-weight:bold;color:#aa22ff;">-&gt;</span> m a</pre>
<p>This can be implemented very easily using the (strict) state monad and a random generator:</p>
<pre style="font-size:140%;"><span style="font-style:italic;color:#408080;">&gt; </span><span style="font-weight:bold;color:#008000;">newtype</span> <span style="color:#b00040;">MCSim</span> s a <span style="font-weight:bold;color:#aa22ff;">=</span> <span style="color:#b00040;">MCSim</span> (<span style="color:#b00040;">State</span> ([s], <span style="color:#b00040;">StdGen</span>) a)
<span style="font-style:italic;color:#408080;">&gt;     </span><span style="font-weight:bold;color:#008000;">deriving</span> <span style="color:#b00040;">Monad</span>
<span style="font-style:italic;color:#408080;">&gt; </span>
<span style="font-style:italic;color:#408080;">&gt; </span><span style="font-weight:bold;color:#008000;">instance</span> <span style="color:#b00040;">MonadState</span> s (<span style="color:#b00040;">MCSim</span> s) <span style="font-weight:bold;color:#008000;">where</span>
<span style="font-style:italic;color:#408080;">&gt;     </span><span style="color:#0000ff;">get</span> <span style="font-weight:bold;color:#aa22ff;">=</span> <span style="color:#b00040;">MCSim</span> <span style="color:#666666;">$</span> liftM (head <span style="color:#666666;">.</span> fst) get
<span style="font-style:italic;color:#408080;">&gt;     </span><span style="color:#0000ff;">put</span> x <span style="font-weight:bold;color:#aa22ff;">=</span> <span style="color:#b00040;">MCSim</span> <span style="color:#666666;">.</span> modify <span style="color:#666666;">$</span> <span style="color:#0000ff;">\</span>(xs, g) <span style="font-weight:bold;color:#aa22ff;">-&gt;</span> (x <span style="color:#b00040;">:</span> xs, g)
<span style="font-style:italic;color:#408080;">&gt; </span>
<span style="font-style:italic;color:#408080;">&gt; </span><span style="font-weight:bold;color:#008000;">instance</span> <span style="color:#b00040;">MonadMC</span> s (<span style="color:#b00040;">MCSim</span> s) <span style="font-weight:bold;color:#008000;">where</span>
<span style="font-style:italic;color:#408080;">&gt;     </span><span style="color:#0000ff;">choose</span> (a, b) <span style="font-weight:bold;color:#aa22ff;">=</span> <span style="color:#b00040;">MCSim</span> <span style="color:#666666;">$</span>
<span style="font-style:italic;color:#408080;">&gt;                     </span><span style="font-weight:bold;color:#008000;">do</span> (xs, g) <span style="font-weight:bold;color:#aa22ff;">&lt;-</span> get
<span style="font-style:italic;color:#408080;">&gt;                        </span><span style="font-weight:bold;color:#008000;">let</span> bnds <span style="font-weight:bold;color:#aa22ff;">=</span> (fromEnum a, fromEnum b)
<span style="font-style:italic;color:#408080;">&gt;                        </span><span style="font-weight:bold;color:#008000;">let</span> (y, g') <span style="font-weight:bold;color:#aa22ff;">=</span> randomR bnds g
<span style="font-style:italic;color:#408080;">&gt;                        </span><span style="color:#0000ff;">put</span> (xs, g')
<span style="font-style:italic;color:#408080;">&gt;                        </span><span style="color:#0000ff;">return</span> <span style="color:#666666;">.</span> toEnum <span style="color:#666666;">$</span> y
<span style="font-style:italic;color:#408080;">&gt; </span>
<span style="font-style:italic;color:#408080;">&gt; -- type Game a = MCSim GameState a</span>
<span style="font-style:italic;color:#408080;">&gt;</span>
<span style="font-style:italic;color:#408080;">&gt; </span><span style="color:#0000ff;">runSim</span> <span style="font-weight:bold;color:#aa22ff;">::</span> <span style="color:#b00040;">StdGen</span> <span style="font-weight:bold;color:#aa22ff;">-&gt;</span> <span style="color:#b00040;">Int</span> <span style="font-weight:bold;color:#aa22ff;">-&gt;</span> s <span style="font-weight:bold;color:#aa22ff;">-&gt;</span> <span style="color:#b00040;">MCSim</span> s <span style="color:#008000;">()</span> <span style="font-weight:bold;color:#aa22ff;">-&gt;</span> [s]
<span style="font-style:italic;color:#408080;">&gt; </span><span style="color:#0000ff;">runSim</span> g n start m <span style="font-weight:bold;color:#aa22ff;">=</span> fst <span style="color:#666666;">$</span> execState m' ([start], g)
<span style="font-style:italic;color:#408080;">&gt;     </span><span style="font-weight:bold;color:#008000;">where</span>
<span style="font-style:italic;color:#408080;">&gt;       </span>(<span style="color:#b00040;">MCSim</span> m') <span style="font-weight:bold;color:#aa22ff;">=</span> foldr (<span style="color:#666666;">&gt;&gt;</span>) (return <span style="color:#008000;">()</span>) <span style="color:#666666;">$</span> replicate n m</pre>
<p>The <code>runSim</code> function runs the simulation and returns the list of visited states.  This is already quite nice, but the best thing is that the <em>same</em> code can be used to create the transition matrix, just swapping in a new implementation of the <code>Game</code> type alias:</p>
<pre style="font-size:140%;"><span style="font-style:italic;color:#408080;">&gt; </span><span style="font-weight:bold;color:#008000;">newtype</span> <span style="color:#b00040;">MC</span> s a <span style="font-weight:bold;color:#aa22ff;">=</span> <span style="color:#b00040;">MC</span> (s <span style="font-weight:bold;color:#aa22ff;">-&gt;</span> [(s, <span style="color:#b00040;">Double</span>, a)])
<span style="font-style:italic;color:#408080;">&gt; </span>
<span style="font-style:italic;color:#408080;">&gt; </span><span style="font-weight:bold;color:#008000;">instance</span> <span style="color:#b00040;">Monad</span> (<span style="color:#b00040;">MC</span> s) <span style="font-weight:bold;color:#008000;">where</span>
<span style="font-style:italic;color:#408080;">&gt;     </span><span style="color:#0000ff;">return</span> x <span style="font-weight:bold;color:#aa22ff;">=</span> <span style="color:#b00040;">MC</span> <span style="color:#666666;">$</span> <span style="color:#0000ff;">\</span>s <span style="font-weight:bold;color:#aa22ff;">-&gt;</span> return (s, <span style="color:#666666;">1.0</span>, x)
<span style="font-style:italic;color:#408080;">&gt;     </span>(<span style="color:#b00040;">MC</span> m) <span style="color:#666666;">&gt;&gt;=</span> f <span style="font-weight:bold;color:#aa22ff;">=</span> <span style="color:#b00040;">MC</span> <span style="color:#666666;">$</span> <span style="color:#0000ff;">\</span>s <span style="font-weight:bold;color:#aa22ff;">-&gt;</span>
<span style="font-style:italic;color:#408080;">&gt;                    </span><span style="font-weight:bold;color:#008000;">do</span> (s, p, x) <span style="font-weight:bold;color:#aa22ff;">&lt;-</span> m s
<span style="font-style:italic;color:#408080;">&gt;                       </span><span style="font-weight:bold;color:#008000;">let</span> (<span style="color:#b00040;">MC</span> m') <span style="font-weight:bold;color:#aa22ff;">=</span> f x
<span style="font-style:italic;color:#408080;">&gt;                       </span>(s', q, y) <span style="font-weight:bold;color:#aa22ff;">&lt;-</span> m' s
<span style="font-style:italic;color:#408080;">&gt;                       </span><span style="color:#0000ff;">return</span> (s', p <span style="color:#666666;">*</span> q, y)
<span style="font-style:italic;color:#408080;">&gt; </span>
<span style="font-style:italic;color:#408080;">&gt; </span><span style="font-weight:bold;color:#008000;">instance</span> <span style="color:#b00040;">MonadState</span> s (<span style="color:#b00040;">MC</span> s) <span style="font-weight:bold;color:#008000;">where</span>
<span style="font-style:italic;color:#408080;">&gt;     </span><span style="color:#0000ff;">get</span> <span style="font-weight:bold;color:#aa22ff;">=</span> <span style="color:#b00040;">MC</span> <span style="color:#666666;">$</span> <span style="color:#0000ff;">\</span>s <span style="font-weight:bold;color:#aa22ff;">-&gt;</span> return (s, <span style="color:#666666;">1.0</span>, s)
<span style="font-style:italic;color:#408080;">&gt;     </span><span style="color:#0000ff;">put</span> x <span style="font-weight:bold;color:#aa22ff;">=</span> <span style="color:#b00040;">MC</span> <span style="color:#666666;">$</span> <span style="color:#0000ff;">\</span>s <span style="font-weight:bold;color:#aa22ff;">-&gt;</span> return (x, <span style="color:#666666;">1.0</span>, <span style="color:#008000;">()</span>)
<span style="font-style:italic;color:#408080;">&gt; </span>
<span style="font-style:italic;color:#408080;">&gt; </span><span style="font-weight:bold;color:#008000;">instance</span> <span style="color:#b00040;">MonadMC</span> s (<span style="color:#b00040;">MC</span> s) <span style="font-weight:bold;color:#008000;">where</span>
<span style="font-style:italic;color:#408080;">&gt;     </span><span style="color:#0000ff;">choose</span> (a, b) <span style="font-weight:bold;color:#aa22ff;">=</span> <span style="font-weight:bold;color:#008000;">let</span> r <span style="font-weight:bold;color:#aa22ff;">=</span> [a<span style="color:#666666;">..</span>b]
<span style="font-style:italic;color:#408080;">&gt;                         </span><span style="color:#0000ff;">p</span> <span style="font-weight:bold;color:#aa22ff;">=</span> recip <span style="color:#666666;">.</span> fromIntegral <span style="color:#666666;">.</span> length <span style="color:#666666;">$</span> r
<span style="font-style:italic;color:#408080;">&gt;                     </span><span style="font-weight:bold;color:#008000;">in</span> <span style="color:#b00040;">MC</span> <span style="color:#666666;">$</span> <span style="color:#0000ff;">\</span>s <span style="font-weight:bold;color:#aa22ff;">-&gt;</span> map (<span style="color:#0000ff;">\</span>x <span style="font-weight:bold;color:#aa22ff;">-&gt;</span> (s, p, x)) r
<span style="font-style:italic;color:#408080;">&gt; </span>
<span style="font-style:italic;color:#408080;">&gt; </span><span style="font-weight:bold;color:#008000;">type</span> <span style="color:#b00040;">Game</span> a <span style="font-weight:bold;color:#aa22ff;">=</span> <span style="color:#b00040;">MC</span> <span style="color:#b00040;">GameState</span> a</pre>
<p>The idea is that we keep track of all possible destination states for a given state, with associated conditional probabilities. For those familiar with Eric Kidd&#8217;s <a href="http://www.randomhacks.net/articles/2007/02/21/refactoring-probability-distributions">series on probability monads</a>, this is basically:</p>
<pre style="font-size:140%;"><span style="font-weight:bold;color:#008000;">type</span> <span style="color:#b00040;">MC</span> s a <span style="font-weight:bold;color:#aa22ff;">=</span> <span style="color:#b00040;">StateT</span> s (<span style="color:#b00040;">PerhapsT</span> <span style="color:#b00040;">[]</span> a)</pre>
<p>Now, how to get a transition matrix from such a monad? Of course, we have to require that the states are <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data-Ix.html">indexable</a>:</p>
<pre style="font-size:140%;"><span style="font-style:italic;color:#408080;">&gt; </span><span style="color:#0000ff;">markov</span> <span style="font-weight:bold;color:#aa22ff;">::</span> <span style="color:#b00040;">Ix</span> s <span style="font-weight:bold;color:#aa22ff;">=&gt;</span>
<span style="font-style:italic;color:#408080;">&gt;           </span><span style="color:#b00040;">MC</span> s <span style="color:#008000;">()</span> <span style="font-weight:bold;color:#aa22ff;">-&gt;</span> (s, s) <span style="font-weight:bold;color:#aa22ff;">-&gt;</span> <span style="color:#b00040;">Array</span> (s, s) <span style="color:#b00040;">Double</span>
<span style="font-style:italic;color:#408080;">&gt; </span><span style="color:#0000ff;">markov</span> (<span style="color:#b00040;">MC</span> m) r <span style="font-weight:bold;color:#aa22ff;">=</span> accumArray (<span style="color:#666666;">+</span>) <span style="color:#666666;">0.0</span> (double r) <span style="color:#666666;">$</span>
<span style="font-style:italic;color:#408080;">&gt;  	     	    </span><span style="color:#0000ff;">range</span> r <span style="color:#666666;">&gt;&gt;=</span> transitions
<span style="font-style:italic;color:#408080;">&gt;     </span><span style="font-weight:bold;color:#008000;">where</span>
<span style="font-style:italic;color:#408080;">&gt;       </span><span style="color:#0000ff;">mkAssoc</span> s (s', p, <span style="font-weight:bold;color:#008000;">_</span>) <span style="font-weight:bold;color:#aa22ff;">=</span> ((s, s'), p)
<span style="font-style:italic;color:#408080;">&gt;       </span><span style="color:#0000ff;">transitions</span> s <span style="font-weight:bold;color:#aa22ff;">=</span> map (mkAssoc s) <span style="color:#666666;">$</span> m s
<span style="font-style:italic;color:#408080;">&gt; 	</span><span style="color:#0000ff;">double</span> (a, b) <span style="font-weight:bold;color:#aa22ff;">=</span> ((a, a), (b, b))</pre>
<p>So we iterate over all states and use the probability values contained in the monad to fill in the array cells corresponding to the selected state pair.</p>
<p>To actually apply this to our Monopoly example, we need to make <code>GameState</code> indexable:</p>
<pre style="font-size:140%;"><span style="font-style:italic;color:#408080;">&gt; </span><span style="color:#0000ff;">nextState</span> <span style="font-weight:bold;color:#aa22ff;">::</span> <span style="color:#b00040;">GameState</span> <span style="font-weight:bold;color:#aa22ff;">-&gt;</span> <span style="color:#b00040;">GameState</span>
<span style="font-style:italic;color:#408080;">&gt; </span><span style="color:#0000ff;">nextState</span> (<span style="color:#b00040;">GS</span> p d) <span style="font-weight:bold;color:#aa22ff;">=</span> <span style="font-weight:bold;color:#008000;">if</span> d <span style="color:#666666;">==</span> <span style="color:#666666;">2</span>
<span style="font-style:italic;color:#408080;">&gt;                      </span><span style="font-weight:bold;color:#008000;">then</span> <span style="color:#b00040;">GS</span> (p <span style="color:#666666;">+</span> <span style="color:#666666;">1</span>) <span style="color:#666666;">0</span>
<span style="font-style:italic;color:#408080;">&gt;                      </span><span style="font-weight:bold;color:#008000;">else</span> <span style="color:#b00040;">GS</span> p (d <span style="color:#666666;">+</span> <span style="color:#666666;">1</span>)
<span style="font-style:italic;color:#408080;">&gt; </span>
<span style="font-style:italic;color:#408080;">&gt; </span><span style="font-weight:bold;color:#008000;">instance</span> <span style="color:#b00040;">Ix</span> <span style="color:#b00040;">GameState</span> <span style="font-weight:bold;color:#008000;">where</span>
<span style="font-style:italic;color:#408080;">&gt;     </span><span style="color:#0000ff;">range</span> (s1, s2) <span style="font-weight:bold;color:#aa22ff;">=</span> takeWhile (<span style="color:#666666;">&lt;=</span> s2) <span style="color:#666666;">.</span>
<span style="font-style:italic;color:#408080;">&gt;                      </span><span style="color:#0000ff;">iterate</span> nextState <span style="color:#666666;">$</span> s1
<span style="font-style:italic;color:#408080;">&gt;     </span><span style="color:#0000ff;">index</span> (s1, s2) s <span style="font-weight:bold;color:#aa22ff;">=</span>
<span style="font-style:italic;color:#408080;">&gt;         </span><span style="font-weight:bold;color:#008000;">let</span> poss <span style="font-weight:bold;color:#aa22ff;">=</span> (position s1, position s2)
<span style="font-style:italic;color:#408080;">&gt;         </span><span style="font-weight:bold;color:#008000;">in</span> index poss (position s) <span style="color:#666666;">*</span> <span style="color:#666666;">3</span> <span style="color:#666666;">+</span>
<span style="font-style:italic;color:#408080;">&gt;            </span><span style="color:#0000ff;">doubles</span> s <span style="color:#666666;">-</span> doubles s1
<span style="font-style:italic;color:#408080;">&gt;     </span><span style="color:#0000ff;">inRange</span> (s1, s2) s <span style="font-weight:bold;color:#aa22ff;">=</span> s1 <span style="color:#666666;">&lt;=</span> s <span style="color:#666666;">&amp;&amp;</span> s <span style="color:#666666;">&lt;=</span> s2
<span style="font-style:italic;color:#408080;">&gt;     </span><span style="color:#0000ff;">rangeSize</span> (s1, s2) <span style="font-weight:bold;color:#aa22ff;">=</span> index (s1, s2) s2 <span style="color:#666666;">+</span> <span style="color:#666666;">1</span></pre>
<p>then finally we can try:</p>
<pre style="font-size:140%;"><span style="font-style:italic;color:#408080;">&gt; </span><span style="color:#0000ff;">monopoly</span> <span style="font-weight:bold;color:#aa22ff;">::</span> (<span style="color:#b00040;">GameState</span>, <span style="color:#b00040;">GameState</span>)
<span style="font-style:italic;color:#408080;">&gt; </span><span style="color:#0000ff;">monopoly</span> <span style="font-weight:bold;color:#aa22ff;">=</span> (<span style="color:#b00040;">GS</span> <span style="color:#666666;">0</span> <span style="color:#666666;">0</span>, <span style="color:#b00040;">GS</span> <span style="color:#666666;">39</span> <span style="color:#666666;">2</span>)
<span style="font-style:italic;color:#408080;">&gt; </span>
<span style="font-style:italic;color:#408080;">&gt; </span><span style="color:#0000ff;">initialState</span> <span style="font-weight:bold;color:#aa22ff;">::</span> <span style="color:#b00040;">Array</span> <span style="color:#b00040;">GameState</span> <span style="color:#b00040;">Double</span>
<span style="font-style:italic;color:#408080;">&gt; </span><span style="color:#0000ff;">initialState</span> <span style="font-weight:bold;color:#aa22ff;">=</span> <span style="font-weight:bold;color:#008000;">let</span> n <span style="font-weight:bold;color:#aa22ff;">=</span> rangeSize monopoly
<span style="font-style:italic;color:#408080;">&gt;                    </span><span style="color:#0000ff;">p</span> <span style="font-weight:bold;color:#aa22ff;">=</span> recip <span style="color:#666666;">$</span> fromIntegral n
<span style="font-style:italic;color:#408080;">&gt;                </span><span style="font-weight:bold;color:#008000;">in</span> listArray monopoly <span style="color:#666666;">$</span> replicate n p
<span style="font-style:italic;color:#408080;">&gt; </span>
<span style="font-style:italic;color:#408080;">&gt; </span><span style="color:#0000ff;">statDistr</span> <span style="font-weight:bold;color:#aa22ff;">::</span> <span style="color:#b00040;">Int</span> <span style="font-weight:bold;color:#aa22ff;">-&gt;</span> [(<span style="color:#b00040;">GameState</span>, <span style="color:#b00040;">Double</span>)]
<span style="font-style:italic;color:#408080;">&gt; </span><span style="color:#0000ff;">statDistr</span> n <span style="font-weight:bold;color:#aa22ff;">=</span> <span style="font-weight:bold;color:#008000;">let</span> mat <span style="font-weight:bold;color:#aa22ff;">=</span> markov game monopoly
<span style="font-style:italic;color:#408080;">&gt;                   </span><span style="color:#0000ff;">distributions</span> <span style="font-weight:bold;color:#aa22ff;">=</span> iterate (<span style="color:#666666;">.*</span> mat)
<span style="font-style:italic;color:#408080;">&gt;                         </span><span style="color:#0000ff;">initialState</span>
<span style="font-style:italic;color:#408080;">&gt;                   </span><span style="color:#0000ff;">st</span> <span style="font-weight:bold;color:#aa22ff;">=</span> distributions <span style="color:#666666;">!!</span> n
<span style="font-style:italic;color:#408080;">&gt;               </span><span style="font-weight:bold;color:#008000;">in</span> assocs st</pre>
<p>where .* is a simple vector-matrix multiplication function:</p>
<pre style="font-size:140%;"><span style="font-style:italic;color:#408080;">&gt; </span><span style="font-weight:bold;color:#008000;">infixl</span> <span style="color:#666666;">5</span> <span style="color:#666666;">.*</span>
<span style="font-style:italic;color:#408080;">&gt; </span>(<span style="color:#666666;">.*</span>) <span style="font-weight:bold;color:#aa22ff;">::</span> (<span style="color:#b00040;">Ix</span> i, <span style="color:#b00040;">Num</span> a) <span style="font-weight:bold;color:#aa22ff;">=&gt;</span>
<span style="font-style:italic;color:#408080;">&gt;            </span><span style="color:#b00040;">Array</span> i a <span style="font-weight:bold;color:#aa22ff;">-&gt;</span> <span style="color:#b00040;">Array</span> (i, i) a <span style="font-weight:bold;color:#aa22ff;">-&gt;</span> <span style="color:#b00040;">Array</span> i a
<span style="font-style:italic;color:#408080;">&gt; </span>(<span style="color:#666666;">.*</span>) x y <span style="font-weight:bold;color:#aa22ff;">=</span> array resultBounds
<span style="font-style:italic;color:#408080;">&gt;               </span>[(i, sum [x<span style="color:#666666;">!</span>k <span style="color:#666666;">*</span> y<span style="color:#666666;">!</span>(k,i) <span style="color:#666666;">|</span> k <span style="font-weight:bold;color:#aa22ff;">&lt;-</span> range (l,u)])
<span style="font-style:italic;color:#408080;">&gt;                </span><span style="color:#666666;">|</span> i <span style="font-weight:bold;color:#aa22ff;">&lt;-</span> range (l'',u'') ]
<span style="font-style:italic;color:#408080;">&gt;         </span><span style="font-weight:bold;color:#008000;">where</span> (l, u) <span style="font-weight:bold;color:#aa22ff;">=</span> bounds x
<span style="font-style:italic;color:#408080;">&gt;               </span>((l', l''), (u', u'')) <span style="font-weight:bold;color:#aa22ff;">=</span> bounds y
<span style="font-style:italic;color:#408080;">&gt;               </span><span style="color:#0000ff;">resultBounds</span>
<span style="font-style:italic;color:#408080;">&gt;                 </span><span style="color:#666666;">|</span> (l,u)<span style="color:#666666;">==</span>(l',u') <span style="font-weight:bold;color:#aa22ff;">=</span> (l'', u'')
<span style="font-style:italic;color:#408080;">&gt;                 </span><span style="color:#666666;">|</span> otherwise <span style="font-weight:bold;color:#aa22ff;">=</span> <span style="font-weight:bold;color:#d2413a;">error</span> <span style="color:#ba2121;">".*: incompatible bounds"</span></pre>
<p>Calling <code>statDistr 100</code> will return an association list of states with corresponding probability in an approximation of the stationary distribution, computed by applying the <a href="http://en.wikipedia.org/wiki/Power_method">power method</a> to the transition matrix. The number 100 is a pure guess, I don&#8217;t know how to estimate the number of iterations necessary for convergence, but that is out of the scope of this post, anyway.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pcapriotti.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pcapriotti.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pcapriotti.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pcapriotti.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pcapriotti.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pcapriotti.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pcapriotti.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pcapriotti.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pcapriotti.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pcapriotti.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pcapriotti.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pcapriotti.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pcapriotti.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pcapriotti.wordpress.com/46/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pcapriotti.wordpress.com&amp;blog=806312&amp;post=46&amp;subd=pcapriotti&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pcapriotti.wordpress.com/2008/10/16/monads-for-markov-chains/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/472181d606a709332975dcceb974eba9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">pcapriotti</media:title>
		</media:content>
	</item>
		<item>
		<title>Rewriting from scratch</title>
		<link>http://pcapriotti.wordpress.com/2008/04/27/rewriting-from-scratch/</link>
		<comments>http://pcapriotti.wordpress.com/2008/04/27/rewriting-from-scratch/#comments</comments>
		<pubDate>Sun, 27 Apr 2008 14:02:49 +0000</pubDate>
		<dc:creator>pcapriotti</dc:creator>
				<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://pcapriotti.wordpress.com/?p=40</guid>
		<description><![CDATA[I have recently come across this article (and those linked by it), while I was considering a rewrite for my own pet project. While I tend to think that rewriting from scratch is usually a bad idea, if we are talking about open source projects, there&#8217;s another element you should take into account: how pleasurable [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pcapriotti.wordpress.com&amp;blog=806312&amp;post=40&amp;subd=pcapriotti&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I have recently come across <a href="http://my.opera.com/Vorlath/blog/2007/09/25/code-rewrite-yes">this article</a> (and those linked by it), while I was considering a rewrite for my own pet project.</p>
<p>While I tend to think that rewriting from scratch is usually a bad idea, if we are talking about open source projects, there&#8217;s another element you should take into account: how pleasurable it is to work on it. If you end up with a big and hairy code base you cannot touch without suddenly feeling all sad and pessimistic about the fate of the world, you should realize that something has gone deeply wrong: you are supposed to work on your project for <em>fun</em> (yeah, to scratch an itch, maybe, too&#8230;), and when the fun stops, most of the reasons to continue disappear. You have no (paying) customers, you have no deadlines, you will be able (or unable, depending on what you do in the rest of your time) to pay your bills anyway.</p>
<p>At this point, either you throw everything into the garbage bin &#8211; which may be sad, disappointing and frustrating, both for you and your users &#8211; or you take whatever lessons you may have learned in the process, and use your extra wisdom to start from scratch a hopefully better and more mature design and implementation that won&#8217;t have those shortcomings that brought you to this miserable situation.</p>
<p>It will probably have other defects and issues, though, more subtle and harder to spot in an upfront design. You have to accept it. And you have to accept the fact that you may need to start from scratch once again in the future.</p>
<p>Here is a simple form you can use to announce your decision to start from scratch. Just copy-paste it into your blog, filling in the required fields. It may save you precious time that you will be able to spend on your n-th rewrite <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<pre>I'll rewrite my pet project from scratch because:

[ ] It's not modular enough
[ ] The original design didn't take __________________ into consideration
[ ] It's too slow
[ ] There is too much legacy code which I can't seem to get rid of
[ ] I want to write it in ____________________

----

[ ] And this time I'll use TDD/BDD

[ ] And this time it will be done right!

Signed (the developer)

_____________________________</pre>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/pcapriotti.wordpress.com/40/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/pcapriotti.wordpress.com/40/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pcapriotti.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pcapriotti.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pcapriotti.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pcapriotti.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pcapriotti.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pcapriotti.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pcapriotti.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pcapriotti.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pcapriotti.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pcapriotti.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pcapriotti.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pcapriotti.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pcapriotti.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pcapriotti.wordpress.com/40/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pcapriotti.wordpress.com&amp;blog=806312&amp;post=40&amp;subd=pcapriotti&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pcapriotti.wordpress.com/2008/04/27/rewriting-from-scratch/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/472181d606a709332975dcceb974eba9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">pcapriotti</media:title>
		</media:content>
	</item>
		<item>
		<title>Fighting zombies with a double fork</title>
		<link>http://pcapriotti.wordpress.com/2008/02/12/fighting-zombies-with-a-double-fork/</link>
		<comments>http://pcapriotti.wordpress.com/2008/02/12/fighting-zombies-with-a-double-fork/#comments</comments>
		<pubDate>Tue, 12 Feb 2008 01:39:01 +0000</pubDate>
		<dc:creator>pcapriotti</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[posix]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[zombies]]></category>

		<guid isPermaLink="false">http://pcapriotti.wordpress.com/?p=38</guid>
		<description><![CDATA[No, this post is not about a special weapon against Romero&#8217;s undeads (though that would be cool , but about a trick I&#8217;ve just discovered after a lot of struggling with child processes that aren&#8217;t properly reaped by their parent. Let me try to be a bit more specific: suppose you have a parent that [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pcapriotti.wordpress.com&amp;blog=806312&amp;post=38&amp;subd=pcapriotti&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>No, this post is not about a special weapon against Romero&#8217;s undeads (though that would be cool <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> , but about a trick I&#8217;ve just discovered after a lot of struggling with child processes that aren&#8217;t properly reaped by their parent.<br />
Let me try to be a bit more specific: suppose you have a parent that needs to spawn two kinds of child processes. Processes of the first kind perform a computation and return to the parent immediately (so they are handled via a fork+exec+waitpid combination), while those of the second kind should be started, their pid recorded so that they may be killed later, and then forgotten.<br />
The question is: how to handle the second kind of subprocesses? If you just fork+exec, when you later kill them, you get zombie processes. If you ignore SIGCHLD (so auto-reaping is enabled) you can&#8217;t perform a blocking wait on the children of the first kind anymore. Handling SIGCHLD is perhaps possible, but I think you need to keep track of the PIDs of the spawned processes and do some magic in the handler. I didn&#8217;t manage to make this working.</p>
<p>What I did manage to get working is the following: just spawn the process in a temporary child process. This way the grandchild will be orphaned and won&#8217;t require a wait from its grandfather.</p>
<p>There are however some problems with this approach. Namely, the child needs to send the PID of its own child to the parent. I&#8217;ve accomplished that through a pipe. Here is some example code to better illustrate this technique:<br />
<pre class="brush: python;">
from subprocess import Popen, PIPE, STDOUT
import sys

def spawn(cmd):
    cmdline = ('python', __file__) + cmd
    return int(Popen(cmdline, stdout = PIPE).stdout.read())

if __name__ == '__main__':
    print Popen(sys.argv[1:], stdout = open('/dev/null', 'w'), stderr = STDOUT).pid
    sys.exit(0)</pre></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/pcapriotti.wordpress.com/38/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/pcapriotti.wordpress.com/38/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pcapriotti.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pcapriotti.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pcapriotti.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pcapriotti.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pcapriotti.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pcapriotti.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pcapriotti.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pcapriotti.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pcapriotti.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pcapriotti.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pcapriotti.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pcapriotti.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pcapriotti.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pcapriotti.wordpress.com/38/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pcapriotti.wordpress.com&amp;blog=806312&amp;post=38&amp;subd=pcapriotti&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pcapriotti.wordpress.com/2008/02/12/fighting-zombies-with-a-double-fork/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/472181d606a709332975dcceb974eba9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">pcapriotti</media:title>
		</media:content>
	</item>
		<item>
		<title>skema 0.1</title>
		<link>http://pcapriotti.wordpress.com/2007/11/19/skema-01/</link>
		<comments>http://pcapriotti.wordpress.com/2007/11/19/skema-01/#comments</comments>
		<pubDate>Mon, 19 Nov 2007 19:49:51 +0000</pubDate>
		<dc:creator>pcapriotti</dc:creator>
				<category><![CDATA[kde]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[skema]]></category>

		<guid isPermaLink="false">http://pcapriotti.wordpress.com/2007/11/19/skema-01/</guid>
		<description><![CDATA[I have just released skema, a very simple tool I wrote to automate some repetitive coding tasks. Nothing fancy, just a ruby script that expands ERB templates in a directory, reading variable values from the command line, configuration files, or interactively. Its purpose is similar to kapptemplate (it already includes a minimal KDE application template), [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pcapriotti.wordpress.com&amp;blog=806312&amp;post=36&amp;subd=pcapriotti&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I have just released <strong>skema</strong>, a very simple tool I wrote to automate some repetitive coding tasks. Nothing fancy, just a ruby script that expands <a href="http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/">ERB</a> templates in a directory, reading variable values from the command line, configuration files, or interactively.<br />
Its purpose is similar to <a href="http://kapptemplate.granroth.org/">kapptemplate</a> (it already includes a minimal KDE application template), but it may be faster to use for little things, and it is probably easier to create templates for it.<br />
Here is a <a href="http://capriotti.selfip.net/skema-0.1.tar.gz">tarball</a> for those of you who want to try it out. You can find a short tutorial in the README file that should be enough to get you started.<br />
Here is a screenshot of skema in interactive mode while expanding the kapp template:<br />
<a href="http://pcapriotti.files.wordpress.com/2007/11/skema-shot.png" title="screenshot of skema 0.1"><img src="http://pcapriotti.files.wordpress.com/2007/11/skema-shot.png?w=450" alt="screenshot of skema 0.1" /></a></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/pcapriotti.wordpress.com/36/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/pcapriotti.wordpress.com/36/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pcapriotti.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pcapriotti.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pcapriotti.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pcapriotti.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pcapriotti.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pcapriotti.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pcapriotti.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pcapriotti.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pcapriotti.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pcapriotti.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pcapriotti.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pcapriotti.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pcapriotti.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pcapriotti.wordpress.com/36/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pcapriotti.wordpress.com&amp;blog=806312&amp;post=36&amp;subd=pcapriotti&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pcapriotti.wordpress.com/2007/11/19/skema-01/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/472181d606a709332975dcceb974eba9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">pcapriotti</media:title>
		</media:content>

		<media:content url="http://pcapriotti.files.wordpress.com/2007/11/skema-shot.png" medium="image">
			<media:title type="html">screenshot of skema 0.1</media:title>
		</media:content>
	</item>
		<item>
		<title>Some thoughts on git</title>
		<link>http://pcapriotti.wordpress.com/2007/10/08/some-thoughts-on-git/</link>
		<comments>http://pcapriotti.wordpress.com/2007/10/08/some-thoughts-on-git/#comments</comments>
		<pubDate>Mon, 08 Oct 2007 13:58:29 +0000</pubDate>
		<dc:creator>pcapriotti</dc:creator>
				<category><![CDATA[git]]></category>
		<category><![CDATA[kde]]></category>
		<category><![CDATA[tagua]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[distributed vcs]]></category>
		<category><![CDATA[modularity]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://pcapriotti.wordpress.com/2007/10/08/some-thoughts-on-git/</guid>
		<description><![CDATA[As I mentioned in a previous post, we started using git for managing Tagua source code. There&#8217;s currently a lot of controversy in the topic of distributed versus centralized VCS&#8217;s, and I feel like expressing my own ideas, too. I&#8217;m no git guru (yet), so please don&#8217;t get offended if I missed something in my [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pcapriotti.wordpress.com&amp;blog=806312&amp;post=34&amp;subd=pcapriotti&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>As I mentioned in a previous post, we started using git for managing Tagua source code. There&#8217;s currently a lot of controversy in the topic of distributed versus centralized VCS&#8217;s, and I feel like expressing my own ideas, too. <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
I&#8217;m no git guru (yet), so please don&#8217;t get offended if I missed something in my criticism. I hope to generate a fruitful discussion, because I really think that the VCS is an important element in the development of an open source project.</p>
<h3>Why git rules</h3>
<ul>
<li>It&#8217;s decentralized. I fully agree with Linus on this point: the decentralized model is superior, for various reasons I won&#8217;t talk about in detail here.</li>
<li>It&#8217;s simple. I don&#8217;t understand why people keep complaining about git being overly complicated. The object model is fairly straightforward, as soon as you stop thinking about revisions the cvs/svn way, and branching/merging is trivial.</li>
<li>The object model is very well designed, flexible and useful.</li>
</ul>
<h3>Why git sucks</h3>
<p>Well, git has a number of minor (?) defects, like portability issues, impossibility of doing partial checkouts (actually I think this is a problem with all the decentralized VCS&#8217;s) and stuff like that, but let me concentrate on those issues which are fundamental to git as an open source project, and are unlikely to be addressed in the future:</p>
<ul>
<li>It is written in (unreadable) C. I don&#8217;t want to start a flame war (really!), but browsing through git&#8217;s code is a painful experience. Huge .c files with tons of big functions with no apparent structure and almost no comment. Ubiquitous micro-management stuff for handling manually allocated buffers. In other words&#8230; a mess.</li>
<li>It (ab)uses the UNIX philosophy. <em>Do one thing, and do it well</em>. Yeah, of course it&#8217;s a great idea, but maybe they shouldn&#8217;t have taken it too literally. I mean, if we talk about modularity and reusable components, I&#8217;m all for it, but it doesn&#8217;t mean you have to make a different executable for each task (even low level ones that should be invisible to the end user) and chain them together via text pipes and bash scripts!</li>
<li>It&#8217;s not reusable by third party applications, at least not in any efficient/convenient way. If you want to build a higher-level abstraction over git (a so-called porcelain), you need to fork out every now and then to get input from git, and then parse its textual output. Yes, I know about libgit.a, but that&#8217;s not really usable as a library (half of git functions call <code>exit</code> on failure) and has no API documentation at all.</li>
</ul>
<p>That said, I have to admit that most of its defects aren&#8217;t that important if you are just going to use it, and not develop it. However, using something you have problems inspecting and modifying feels a bit like using a closed source product&#8230;</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/pcapriotti.wordpress.com/34/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/pcapriotti.wordpress.com/34/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pcapriotti.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pcapriotti.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pcapriotti.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pcapriotti.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pcapriotti.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pcapriotti.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pcapriotti.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pcapriotti.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pcapriotti.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pcapriotti.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pcapriotti.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pcapriotti.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pcapriotti.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pcapriotti.wordpress.com/34/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pcapriotti.wordpress.com&amp;blog=806312&amp;post=34&amp;subd=pcapriotti&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pcapriotti.wordpress.com/2007/10/08/some-thoughts-on-git/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/472181d606a709332975dcceb974eba9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">pcapriotti</media:title>
		</media:content>
	</item>
	</channel>
</rss>
