<?xml version="1.0"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">

<channel>
	<title>Planet Haskell</title>
	<link>http://planet.haskell.org/</link>
	<language>en</language>
	<description>Planet Haskell - http://planet.haskell.org/</description>

<item>
	<title>Neil Brown: Progression version 0.2 released</title>
	<guid isPermalink="false">http://chplib.wordpress.com/?p=1291</guid>
	<link>http://chplib.wordpress.com/2010/02/08/progression-version-0-2-released/</link>
	<description>&lt;br /&gt;&lt;p&gt;Version 0.2 of &lt;a href=&quot;http://chplib.wordpress.com/2010/02/04/progression-supporting-optimisation-in-haskell/&quot;&gt;Progression&lt;/a&gt; is now available on &lt;a href=&quot;http://hackage.haskell.org/package/progression&quot;&gt;Hackage&lt;/a&gt;.  There&amp;#8217;s a few minor changes; here&amp;#8217;s the relevant entry from the &lt;a href=&quot;http://www.patch-tag.com/r/twistedsquare/progression/snapshot/current/content/pretty/ChangeLog&quot;&gt;ChangeLog&lt;/a&gt;:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;Fixed a bug where the argument to the -c and -p options were not split on commas as the documentation described.  Also added in settings for: drawing the graph with a logarithmic Y axis, plot size, and the way the benchmarks are sorted on the X axis.
&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Most changes to Progression are likely to touch the Config types, which will therefore entail a major version number bump (&lt;a href=&quot;http://www.haskell.org/haskellwiki/Package_versioning_policy&quot;&gt;in accordance with the PVP&lt;/a&gt;).&lt;/p&gt;
  &lt;a rel=&quot;nofollow&quot; href=&quot;http://feeds.wordpress.com/1.0/gocomments/chplib.wordpress.com/1291/&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://feeds.wordpress.com/1.0/comments/chplib.wordpress.com/1291/&quot; /&gt;&lt;/a&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;http://feeds.wordpress.com/1.0/godelicious/chplib.wordpress.com/1291/&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://feeds.wordpress.com/1.0/delicious/chplib.wordpress.com/1291/&quot; /&gt;&lt;/a&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;http://feeds.wordpress.com/1.0/gostumble/chplib.wordpress.com/1291/&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://feeds.wordpress.com/1.0/stumble/chplib.wordpress.com/1291/&quot; /&gt;&lt;/a&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;http://feeds.wordpress.com/1.0/godigg/chplib.wordpress.com/1291/&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://feeds.wordpress.com/1.0/digg/chplib.wordpress.com/1291/&quot; /&gt;&lt;/a&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;http://feeds.wordpress.com/1.0/goreddit/chplib.wordpress.com/1291/&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://feeds.wordpress.com/1.0/reddit/chplib.wordpress.com/1291/&quot; /&gt;&lt;/a&gt; &lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://stats.wordpress.com/b.gif?host=chplib.wordpress.com&amp;amp;blog=9351306&amp;amp;post=1291&amp;amp;subd=chplib&amp;amp;ref=&amp;amp;feed=1&quot; /&gt;</description>
	<pubDate>Mon, 08 Feb 2010 18:51:35 +0000</pubDate>
	<dc:creator>Neil Brown</dc:creator>
</item>
<item>
	<title>Haskell Weekly News: Haskell Weekly News: Issue 149 - February , 2010</title>
	<guid isPermalink="false">http://sequence.complete.org/461 at http://sequence.complete.org</guid>
	<link>http://sequence.complete.org/hwn/20100208</link>
	<description>Haskell Weekly News: February 08, 2010
&lt;p&gt;Welcome to issue 149 of HWN, a newsletter covering
   developments in the &lt;a href=&quot;http://haskell.org/&quot;&gt;Haskell&lt;/a&gt; community.&lt;/p&gt;
&lt;p&gt;Hello Haskellers, this week's HWN was delayed a bit in the hopes of making
it a bit more substantial. I hate putting up thin HWNs, but of course this
must occasionally happen. We have several new CFPs for workshops this week,
a new benchmarking package, and some fun quotes. Till next week, Haskellers,
your Haskell Weekly News!&lt;/p&gt; &lt;h4&gt;Announcements&lt;/h4&gt; &lt;p&gt;&lt;em&gt;Call for Papers:
Haskell Symposium 2010&lt;/em&gt;. Jeremy.Gibbons
   &lt;a href=&quot;http://article.gmane.org/gmane.comp.lang.haskell.general/17778&quot;&gt;announced&lt;/a&gt;
   a call for papers for the 2010 ACM SIGPLAN Haskell Symposium 2010 in
   Baltimore, Maryland; on 30th September.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;2nd CfP: LOPSTR 2010&lt;/em&gt;. Temur Kutsia
   &lt;a href=&quot;http://article.gmane.org/gmane.comp.lang.haskell.general/17770&quot;&gt;announced&lt;/a&gt;
   a second call for papers for the 20th International Symposium on Logic-Based
   Program Synthesis and Transformation, being held in Hagenberg, Austria,
   July 23-25, 2010 (co-located with PPDP 2010).&lt;/p&gt;
&lt;p&gt;&lt;em&gt;PAR 2010: First CFP&lt;/em&gt;. Koen Claessen
   &lt;a href=&quot;http://article.gmane.org/gmane.comp.lang.haskell.general/17769&quot;&gt;announced&lt;/a&gt;
   a first call for papers for PAR'10, the Workshop on Partiality and
   Recursion in Interactive Theorem Provers.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;data-ordlist-0.2&lt;/em&gt;. Leon Smith
   &lt;a href=&quot;http://article.gmane.org/gmane.comp.lang.haskell.cafe/70035&quot;&gt;announced&lt;/a&gt;

   a new release of ordlist, including a change to the module name and
   bug fixes.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;progression-0.1&lt;/em&gt;. Neil Brown
   &lt;a href=&quot;http://article.gmane.org/gmane.comp.lang.haskell.cafe/69929&quot;&gt;announced&lt;/a&gt;
   Progression, a metalibrary which consolidates various existing tools for
   Haskell optimization (notably Criterion).&lt;/p&gt;
&lt;p&gt;&lt;em&gt;HList darcs repo&lt;/em&gt;. Oleg
   He who inhabits all types, but is not _|_, has &lt;a href=&quot;http://article.gmane.org/gmane.comp.lang.haskell.cafe/69898&quot;&gt;announced&lt;/a&gt;
   a new darcs repo for HList (and OOHaskell) on community.haskell.org.&lt;/p&gt;

&lt;h4&gt;Discussion&lt;/h4&gt; &lt;p&gt;&lt;em&gt;a beginner question:
decorate-op-undecorate&lt;/em&gt;. Aran Donohue
   &lt;a href=&quot;http://thread.gmane.org/gmane.comp.lang.haskell.cafe/69984&quot;&gt;asked&lt;/a&gt;
   about how to write a function which can 'inspect' inside a datatype.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Translation of Haskell type classes&lt;/em&gt;. Enrique Martin
   &lt;a href=&quot;http://thread.gmane.org/gmane.comp.lang.haskell.cafe/69914&quot;&gt;talked&lt;/a&gt;
   about some experiments he's done with type classes, and asked some
   questions regarding optimization related to them.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Category Theory woes&lt;/em&gt;. Mark Spezzano
   &lt;a href=&quot;http://thread.gmane.org/gmane.comp.lang.haskell.cafe/69803&quot;&gt;asked&lt;/a&gt;

   about resources for learning about category theory.&lt;/p&gt;
&lt;h4&gt;Blog noise&lt;/h4&gt; &lt;a href=&quot;http://planet.haskell.org&quot;&gt;Haskell news&lt;/a&gt; from
the  &lt;a href=&quot;http://haskell.org/haskellwiki/Blog_articles&quot;&gt;blogosphere.&lt;/a&gt;
Blog posts from people new to the Haskell community are marked
with &gt;&gt;&gt;, be sure to welcome them!  &lt;ul&gt;&lt;li&gt;&lt;p&gt;Dan Piponi (sigfpe): &lt;a href=&quot;http://blog.sigfpe.com/2010/02/decategorification-of-naturals.html&quot;&gt;&lt;em&gt;The
Categorification of the Naturals&lt;/em&gt;&lt;/a&gt;. &lt;/p&gt;&lt;/li&gt;
    &lt;li&gt;&lt;p&gt;Roman Cheplyaka: &lt;a href=&quot;http://ro-che.blogspot.com/2010/02/hledger.html&quot;&gt;&lt;em&gt;hledger&lt;/em&gt;&lt;/a&gt;.
    &lt;/p&gt;&lt;/li&gt; &lt;li&gt;&lt;p&gt;Luke Palmer: &lt;a href=&quot;http://lukepalmer.wordpress.com/2010/02/05/associative-alpha-blending/&quot;&gt;&lt;em&gt;Associative
    Alpha Blending&lt;/em&gt;&lt;/a&gt;. &lt;/p&gt;&lt;/li&gt; &lt;li&gt;&lt;p&gt;Neil Brown: &lt;a href=&quot;http://chplib.wordpress.com/2010/02/04/progression-supporting-optimisation-in-haskell/&quot;&gt;&lt;em&gt;Progression:
    Supporting Optimisation in Haskell&lt;/em&gt;&lt;/a&gt;. &lt;/p&gt;&lt;/li&gt; &lt;li&gt;&lt;p&gt;Holumbus: &lt;a href=&quot;http://holumbus.fh-wedel.de/blog/?p=33&quot;&gt;&lt;em&gt;Hayoo! Webservice
    API&lt;/em&gt;&lt;/a&gt;. &lt;/p&gt;&lt;/li&gt; &lt;li&gt;&lt;p&gt;apfelmus: &lt;a href=&quot;http://apfelmus.nfshost.com/articles/operational-monad.html&quot;&gt;&lt;em&gt;The
    Operational Monad Tutorial&lt;/em&gt;&lt;/a&gt;. &lt;/p&gt;&lt;/li&gt;

    &lt;li&gt;&lt;p&gt;Dan Piponi (sigfpe): &lt;a href=&quot;http://blog.sigfpe.com/2010/02/tagging-monad-transformer-layers.html&quot;&gt;&lt;em&gt;Tagging
    Monad Transformer Layers&lt;/em&gt;&lt;/a&gt;. &lt;/p&gt;&lt;/li&gt; &lt;li&gt;&lt;p&gt;Darcs: &lt;a href=&quot;http://blog.darcs.net/2010/02/darcs-weekly-news-53.html&quot;&gt;&lt;em&gt;darcs
    weekly news #53&lt;/em&gt;&lt;/a&gt;. &lt;/p&gt;&lt;/li&gt; &lt;li&gt;&lt;p&gt;JP Moresmau: &lt;a href=&quot;http://jpmoresmau.blogspot.com/2010/02/eclipsefp-1110-released-with-debugging.html&quot;&gt;&lt;em&gt;EclipseFP
    1.110 released, with debugging support&lt;/em&gt;&lt;/a&gt;. &lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;
&lt;h4&gt;Quotes of the Week&lt;/h4&gt; &lt;ul&gt;&lt;li&gt;&lt;em&gt;lispy|web&lt;/em&gt;: This curses binding
appears to be terminally broken&lt;/li&gt;

    &lt;li&gt;&lt;em&gt;lispy&lt;/em&gt;: I did, 'cabal install mage' and it complains about
    curses&lt;/li&gt; &lt;li&gt;&lt;em&gt;lament&lt;/em&gt;: Just use fix to find the least funny
    joke&lt;/li&gt; &lt;li&gt;&lt;em&gt;copumpkin&lt;/em&gt;: A monad is just a lax functor from a
    terminal bicategory, duh. fuck that monoid in category of endofunctors
    shit&lt;/li&gt;&lt;/ul&gt;
&lt;h4&gt;About the Haskell Weekly News&lt;/h4&gt; &lt;p&gt;New editions are posted to
   &lt;a href=&quot;http://www.haskell.org/mailman/listinfo/haskell&quot;&gt;the Haskell
   mailing list&lt;/a&gt; as well as to &lt;a href=&quot;http://sequence.complete.org/&quot;&gt;the
   Haskell Sequence&lt;/a&gt; and &lt;a href=&quot;http://planet.haskell.org/&quot;&gt;Planet
   Haskell&lt;/a&gt;.  &lt;a href=&quot;http://sequence.complete.org/node/feed&quot;&gt;RSS&lt;/a&gt;

   is also available, and headlines appear on &lt;a href=&quot;http://haskell.org&quot;&gt;haskell.org&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;To help create new editions of this newsletter, please
   see the information on &lt;a href=&quot;http://haskell.org/haskellwiki/HWN&quot;&gt;how
   to contribute&lt;/a&gt;. Send stories to &lt;code&gt;jfredett . at . gmail . dot
   . com&lt;/code&gt;.  The darcs repository is available at &lt;code&gt;darcs
   get &lt;a href=&quot;http://patch-tag.com/r/jfredett/HWN2/pullrepo
   HWN2&quot;&gt;http://patch-tag.com/r/jfredett/HWN2/pullrepo HWN2&lt;/a&gt; .&lt;/code&gt;&lt;/p&gt;</description>
	<pubDate>Mon, 08 Feb 2010 17:43:48 +0000</pubDate>
	<dc:creator>jfredett</dc:creator>
</item>
<item>
	<title>Epilogue for Epigram: Quotients</title>
	<guid isPermalink="false">http://www.e-pig.org/epilogue/?p=319</guid>
	<link>http://www.e-pig.org/epilogue/?p=319</link>
	<description>I&amp;#8217;ve had an enquiry for more details on quotients in the new Epigram setup, so I&amp;#8217;ll take that as a cue to blog a bit.
First, I&amp;#8217;d better sketch some basics about propositions and equality. It&amp;#8217;s the novel treatment of these things which lets us handle quotients in (hopefully) a neater way than has been possible [...]</description>
	<pubDate>Mon, 08 Feb 2010 14:02:54 +0000</pubDate>
	<dc:creator>Conor</dc:creator>
</item>
<item>
	<title>Tom Schrijvers: Haskell @ AOSD 2010</title>
	<guid isPermalink="false">tag:blogger.com,1999:blog-5844006452378085451.post-8940008518622293474</guid>
	<link>http://tomschrijvers.blogspot.com/2009/09/effectiveadvice-aop-mixin-inheritance.html</link>
	<description>How to reason about effectful advice? Write your AOP programs in Haskell, the world's best imperative programming language. Use monads and monad transformers for effects and functional mixins for advice. In return you get powerful reasoning tools: equational reasoning and parametricity.&lt;br /&gt;&lt;br /&gt;&lt;span&gt;Update:&lt;/span&gt; Our &lt;a href=&quot;http://www.cs.kuleuven.be/%7Etoms/Research/papers/aosd2010.pdf&quot;&gt;paper&lt;/a&gt; on the topic has been accepted at AOSD 2010. The &lt;a href=&quot;http://www.cs.kuleuven.be/publicaties/rapporten/cw/CW556.abs.html&quot;&gt;technical report&lt;/a&gt; has some pretty cool parametricity proofs in its appendix on non-interference of advice, based on Janis' Voigtlaender's ICFP'09 paper &quot;Free Theorems Involving Type Constructor Classes&quot;. &lt;a href=&quot;http://www.cs.kuleuven.be/%7Etoms/Research/talks/EffectiveAdvice.pdf&quot;&gt;Slides&lt;/a&gt; of my PLSIG seminar at K.U.Leuven are also available.&lt;br /&gt;&lt;br /&gt;&lt;em&gt;&lt;/em&gt;&lt;blockquote&gt;&lt;b&gt;EffectiveAdvice: Disciplined Advice with Explicit Effects&lt;/b&gt;&lt;br /&gt;Bruno Oliveira, Tom Schrijvers and William Cook&lt;br /&gt;&lt;p&gt;&lt;b&gt;Abstract&lt;/b&gt;&lt;/p&gt; &lt;p&gt; Advice is a mechanism, widely used in aspect-oriented languages, that allows one program component to augment or modify the behavior of other components. Advice is useful for modularizing concerns, including logging, error handling, and some optimizations, that would otherwise require code to be scattered throughout a system. When advice and other components are composed together they become tightly coupled, sharing both control and data flows. However this creates important problems: modular reasoning about a component becomes very difficult; and two tightly coupled components may interfere with the control and data flows of each other. &lt;/p&gt; &lt;p&gt; This paper presents EffectiveAdvice, a disciplined model of advice, inspired by Aldrich's Open Modules, that has full support for effects in both base components and advice. With EffectiveAdvice, equivalence of advice, as well as base components, can be checked by equational reasoning. The paper describes an implementation of EffectiveAdvice as a Haskell library and shows how to use it to solve well-known programming problems. Advice is modeled by mixin inheritance and effects are modeled by monads. Interference patterns previously identified in the literature are expressed as combinators. Parametricity, together with the combinators, is used to prove two harmless advice theorems. The result is an effective model of advice that supports effects in both advice and base components, and allows these effects to be separated with strong non-interference guarantees, or merged as needed.&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;&lt;/p&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;img width=&quot;1&quot; height=&quot;1&quot; src=&quot;https://blogger.googleusercontent.com/tracker/5844006452378085451-8940008518622293474?l=tomschrijvers.blogspot.com&quot; alt=&quot;&quot; /&gt;&lt;/div&gt;</description>
	<pubDate>Mon, 08 Feb 2010 09:01:46 +0000</pubDate>
	<dc:creator>Tom Schrijvers (noreply@blogger.com)</dc:creator>
</item>
<item>
	<title>Joachim Breitner: If I were a caricaturist</title>
	<guid isPermalink="false">https://www.joachim-breitner.de/blog/archives/374-guid.html</guid>
	<link>https://www.joachim-breitner.de/blog/archives/374-If-I-were-a-caricaturist.html</link>
	<description>&lt;p&gt;I’d draw a caricature involving a Toyota car, representing capitalism, with a stuck gas petal and Barack Obama trying to fix it. But as I cannot draw very well, especially recognizable people, I created this collage:&lt;/p&gt; 
&lt;div align=&quot;center&quot;&gt;&lt;img width=&quot;682&quot; height=&quot;549&quot; src=&quot;http://www.joachim-breitner.de/various/obama-toyota-callback.jpg&quot; /&gt;&lt;/div&gt; 
&lt;p&gt;The photo of Obama was created by &lt;a href=&quot;http://www.flickr.com/photos/bethcanphoto/2287026145/&quot;&gt;Beth Rankin&lt;/a&gt;.&lt;/p&gt;</description>
	<pubDate>Sun, 07 Feb 2010 20:33:45 +0000</pubDate>
	<dc:creator>mail@joachim-breitner.de (nomeata)</dc:creator>
</item>
<item>
	<title>Dan Piponi (sigfpe): The Categorification of the Naturals</title>
	<guid isPermalink="false">tag:blogger.com,1999:blog-11295132.post-1814777119392201914</guid>
	<link>http://blog.sigfpe.com/2010/02/decategorification-of-naturals.html</link>
	<description>A heavyweight looking title, but this post is really about nothing more than doing arithmetic.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Peano Arithmetic&lt;/b&gt;&lt;br /&gt;I've seen &lt;a href=&quot;http://www.haskell.org/haskellwiki/Type_arithmetic&quot;&gt;many&lt;/a&gt; &lt;a href=&quot;http://okmij.org/ftp/Computation/type-arithmetics.html&quot;&gt;articles&lt;/a&gt; on &lt;a href=&quot;http://www.sigfpe.com/Computing/peano.html&quot;&gt;type&lt;/a&gt; level arithmetic. They all seem to share the idea that the Haskell type system can be made to perform computations by treating types as symbols that can be manipulated according to rules. But every article I have seen seems to miss the important idea that the naturals don't have to simply be empty symbols - that they are perfectly good types with elements and that the basic operations of arithmetic have nice a interpretation as functions between types. Implementing these missing pieces will also give an example of &lt;a href=&quot;http://en.wikipedia.org/wiki/Categorification&quot;&gt;categorification&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;As usual, some Haskell administration first because this post is runnable Haskell code:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&amp;gt; {-# LANGUAGE ScopedTypeVariables, UndecidableInstances #-}&lt;br /&gt;&amp;gt; {-# OPTIONS -fglasgow-exts #-}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Here are what are commonly called (some of) the &lt;a href=&quot;http://en.wikipedia.org/wiki/Peano_axioms&quot;&gt;Peano axioms&lt;/a&gt; defining addition and multiplication:&lt;br /&gt;&lt;blockquote&gt;&lt;br /&gt;1. 0+b = b&lt;br /&gt;2. Sa+b = S(a+b)&lt;br /&gt;3. 0.b = 0&lt;br /&gt;4. Sa.b = b+a.b&lt;br /&gt;&lt;/blockquote&gt;&lt;br /&gt;The idea is that S represents the &quot;successor&quot; function maping n to n+1. Using just these definitions, and induction, we can define addition and multiplication for all natural numbers. For example, 3 is represented by SSS0 and 2 by SS0 and we can compute 3+2 using&lt;br /&gt;&lt;blockquote&gt;&lt;br /&gt;2+3&lt;br /&gt;= SS0+SSS0 by definition&lt;br /&gt;= S(S0+SSS0) by 2&lt;br /&gt;= S(S(0+SSS0)) by 2&lt;br /&gt;= SSSSS0 by 1&lt;br /&gt;= 5 by definition&lt;br /&gt;&lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;But where do addition and multiplication come from? One point of view is that the natural numbers are what we get when we take finite sets but consider sets of the same size to be equal. We can do the same with finite types. The type &lt;tt&gt;Bool&lt;/tt&gt; and &lt;tt&gt;Maybe ()&lt;/tt&gt; both have two elements (ignoring bottoms) and are isomorpic. We can just consider these to be the same type, called 2. Given two types &lt;tt&gt;A&lt;/tt&gt; and &lt;tt&gt;B&lt;/tt&gt; we can form &lt;tt&gt;Either A B&lt;/tt&gt;. The number of elements in this new type is the sum of the number of elements in &lt;tt&gt;A&lt;/tt&gt; and &lt;tt&gt;B&lt;/tt&gt;. If we blur the distinction between isomorphic types we can think &lt;tt&gt;Either&lt;/tt&gt; as being the addition operator. Similarly, &lt;tt&gt;(,)&lt;/tt&gt; can be thought of as multiplication. The Peano axioms now describe the properties of addition and multiplication defined in this way.&lt;br /&gt;&lt;br /&gt;When we consider different types to be equal we lose some information. In particular, we lose that fact that given two types of the same size, we can construct an explicit isomorphism between them. But there's no need to do this. We can go back to the Peano axioms and reinterpret them as a recipe for constructing the isomorphism. If we do this, then any theorem we prove (constructively) using the Peano axioms can be interpreted as explicitly constructing an isomorphism between types. We normally just forget about the isomorphism. This 'forgetting' is so common that it has a name: &lt;a href=&quot;http://ncatlab.org/nlab/show/decategorification&quot;&gt;decategorification&lt;/a&gt;. Putting the structure back is called &lt;a href=&quot;http://ncatlab.org/nlab/show/vertical+categorification&quot;&gt;categorification&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Type Level Naturals&lt;/b&gt;&lt;br /&gt;We will represent the natural number n as a type with precisely n elements. We'll start with the type representing zero. Obviously it must have no elements. It's traditionally called &lt;tt&gt;Void&lt;/tt&gt;.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&amp;gt; data Void&lt;br /&gt;&amp;gt; instance Show Void where&lt;br /&gt;&amp;gt;     show _ = undefined&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;That &lt;tt&gt;undefined&lt;/tt&gt; will cause no problems as we can never pass an argument into &lt;tt&gt;show&lt;/tt&gt;.&lt;br /&gt;&lt;br /&gt;If &lt;tt&gt;Void&lt;/tt&gt; is playing the role of 0 we need something to play the role of S. That's &lt;tt&gt;Maybe&lt;/tt&gt;. Given a type &lt;tt&gt;A&lt;/tt&gt;, &lt;tt&gt;Maybe A&lt;/tt&gt; is the type with one more element. So we can mimic the definitions of the natural numbers:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&amp;gt; type One   = Maybe Void&lt;br /&gt;&amp;gt; type Two   = Maybe One&lt;br /&gt;&amp;gt; type Three = Maybe Two&lt;br /&gt;&amp;gt; type Four  = Maybe Three&lt;br /&gt;&amp;gt; type Five  = Maybe Four&lt;br /&gt;&amp;gt; type Six   = Maybe Five&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;and so on. I'll call these the natural number types. We can also label the elements of these types. Here are some elements:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&amp;gt; zero  = Nothing&lt;br /&gt;&amp;gt; one   = Just zero&lt;br /&gt;&amp;gt; two   = Just one&lt;br /&gt;&amp;gt; three = Just two&lt;br /&gt;&amp;gt; four  = Just three&lt;br /&gt;&amp;gt; five  = Just four&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;b&gt;Addition&lt;/b&gt;&lt;br /&gt;Now we can define addition. We want to be able to take a pair of natural number types &lt;tt&gt;A&lt;/tt&gt; and &lt;tt&gt;B&lt;/tt&gt; and construct an explicit isomorphism between &lt;tt&gt;Either A B&lt;/tt&gt; and a natural number type which I'll label &lt;tt&gt;Plus A B&lt;/tt&gt;. I'll call the isomorphisms one way &lt;tt&gt;plus&lt;/tt&gt; and the other way &lt;tt&gt;plus'&lt;/tt&gt;. Here's a suitable type class:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&amp;gt; class Plussable a b where&lt;br /&gt;&amp;gt;    type Plus a b&lt;br /&gt;&amp;gt;    plus  :: Either a b -&amp;gt; Plus a b&lt;br /&gt;&amp;gt;    plus' :: Plus a b -&amp;gt; Either a b&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;From axiom 1 we want 0+b=b. This immediately gives:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&amp;gt; instance Plussable Void b where&lt;br /&gt;&amp;gt;    type Plus Void b = b&lt;br /&gt;&lt;br /&gt;&amp;gt;    plus (Right b) = b&lt;br /&gt;&amp;gt;    plus'       b  = Right b&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;We can view axiom 2, Sa+b = S(a+b), as:&lt;br /&gt;&lt;br /&gt;&lt;a href=&quot;http://1.bp.blogspot.com/_UdKHLrHa05M/S233ATAMJKI/AAAAAAAAAeU/Fp29HdfcNGA/s1600-h/peano1.png&quot;&gt;&lt;img src=&quot;http://1.bp.blogspot.com/_UdKHLrHa05M/S233ATAMJKI/AAAAAAAAAeU/Fp29HdfcNGA/s400/peano1.png&quot; border=&quot;0&quot; alt=&quot;&quot; id=&quot;BLOGGER_PHOTO_ID_5435271909779186850&quot; /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;The implementation of &lt;tt&gt;plus&lt;/tt&gt; implements the mapping of the shaded square directly. If we ignore the shaded square and consider only the unshaded ones, then we are left with another simpler addition. We can implement the isomorphism for that by using &lt;tt&gt;plus&lt;/tt&gt; recursively. Here's the code:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&amp;gt; instance Plussable a b =&amp;gt; Plussable (Maybe a) b where&lt;br /&gt;&amp;gt;    type Plus (Maybe a) b = Maybe (Plus a b)&lt;br /&gt;&amp;gt;    plus (Left Nothing)  = Nothing&lt;br /&gt;&amp;gt;    plus (Left (Just a)) = Just ((plus :: Either a b -&amp;gt; Plus a b) (Left a))&lt;br /&gt;&amp;gt;    plus (Right b)       = Just ((plus :: Either a b -&amp;gt; Plus a b) (Right b))&lt;br /&gt;&lt;br /&gt;&amp;gt;    plus' Nothing = Left Nothing&lt;br /&gt;&amp;gt;    plus' (Just x) =&lt;br /&gt;&amp;gt;        let i' = plus' :: Plus a b -&amp;gt; Either a b&lt;br /&gt;&amp;gt;        in  case i' x of&lt;br /&gt;&amp;gt;            Left  a -&amp;gt; Left (Just a)&lt;br /&gt;&amp;gt;            Right b -&amp;gt; Right b&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;b&gt;Multiplication&lt;/b&gt;&lt;br /&gt;Now we can implement multiplication similarly. First the type class:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&amp;gt; class Timesable a b where&lt;br /&gt;&amp;gt;    type Times a b&lt;br /&gt;&amp;gt;    times  :: (a, b) -&amp;gt; Times a b&lt;br /&gt;&amp;gt;    times' :: Times a b -&amp;gt; (a, b)&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Multiplication by zero gives zero. This is straightforward to implement for the simply reason that we don't actually have to implement isomorphisms for the empty type:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&amp;gt; instance Timesable Void b where&lt;br /&gt;&amp;gt;    type Times Void b = Void&lt;br /&gt;&amp;gt;    times  _ = undefined&lt;br /&gt;&amp;gt;    times' _ = undefined&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;(That's not quite true, Haskell, for some reason, forces us to write a line of code that can never be used. I think this ought to be fixed.)&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&amp;gt; instance (Timesable a b, Plussable b (Times a b)) =&amp;gt; Timesable (Maybe a) b where&lt;br /&gt;&amp;gt;     type Times (Maybe a) b = Plus b (Times a b)&lt;br /&gt;&lt;br /&gt;&amp;gt;     times (Nothing, b) =&lt;br /&gt;&amp;gt;         let i = plus :: Either b (Times a b) -&amp;gt; Plus b (Times a b)&lt;br /&gt;&amp;gt;         in  i (Left b)&lt;br /&gt;&lt;br /&gt;&amp;gt;     times (Just a, b) =&lt;br /&gt;&amp;gt;         let i = plus :: Either b (Times a b) -&amp;gt; Plus b (Times a b)&lt;br /&gt;&amp;gt;         in  i (Right (times ((a, b))))&lt;br /&gt;&lt;br /&gt;&amp;gt;     times' b = &lt;br /&gt;&amp;gt;         let i' = plus' :: Plus b (Times a b) -&amp;gt; Either b (Times a b)&lt;br /&gt;&amp;gt;         in case i' b of&lt;br /&gt;&amp;gt;             Left  b  -&amp;gt; (Nothing, b)&lt;br /&gt;&amp;gt;             Right ab -&amp;gt; let (a, b) = times' ab in (Just a, b)&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;That's it. We've decategorified type level arithmetic. Given an equality like 2*3=6 we automatically get an isomorphism like&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;times :: (Two, Three) -&gt; Six&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Isomorphisms from Equations&lt;/b&gt;&lt;br /&gt;But what about more general equation like 2*2+5 = 3*3? Can we automatically construct the isomorphism?&lt;br /&gt;&lt;br /&gt;One approach is simply to reduce each side of the equation to its canonical form, in this case 9, and then use this to construct a pair of isomorphisms, one from the left hand side to the 9 element natural number type, and one from 9 element natural number type to the right hand side. We'll use a type class to indicate that a type can be reduced to canonical form. The map doing the reduction will be called &lt;tt&gt;canonical&lt;/tt&gt;:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&amp;gt; class Canonicable a where&lt;br /&gt;&amp;gt;     type Canonical a&lt;br /&gt;&lt;br /&gt;&amp;gt;     canonical  :: a -&amp;gt; Canonical a&lt;br /&gt;&amp;gt;     canonical' :: Canonical a -&amp;gt; a&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;tt&gt;Void&lt;/tt&gt; is already in canonical form so there's nothing to do in this case:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&amp;gt; instance Canonicable Void where&lt;br /&gt;&amp;gt;     type Canonical Void = Void&lt;br /&gt;&lt;br /&gt;&amp;gt;     canonical  = id&lt;br /&gt;&amp;gt;     canonical' = id&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;If something is of type &lt;tt&gt;Maybe A&lt;/tt&gt;, and &lt;tt&gt;A&lt;/tt&gt; is reducible to canonical form, then we can simply reduce &lt;tt&gt;Maybe A&lt;/tt&gt; in two steps:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&amp;gt; instance Canonicable a =&amp;gt; Canonicable (Maybe a) where&lt;br /&gt;&amp;gt;     type Canonical (Maybe a) = Maybe (Canonical a)&lt;br /&gt;&lt;br /&gt;&amp;gt;     canonical  Nothing  = Nothing&lt;br /&gt;&amp;gt;     canonical  (Just n) = Just (canonical n)&lt;br /&gt;&amp;gt;     canonical' Nothing  = Nothing&lt;br /&gt;&amp;gt;     canonical' (Just n) = Just (canonical' n)&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Now I give the rule for reducing &lt;tt&gt;Either A B&lt;/tt&gt; to canonical form. We just have to reduce &lt;tt&gt;A&lt;/tt&gt; and &lt;tt&gt;B&lt;/tt&gt; to canonical form and then apply &lt;tt&gt;plus&lt;/tt&gt;:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&amp;gt; instance (Canonicable m, Canonicable n, Plussable (Canonical m) (Canonical n)) =&amp;gt; Canonicable (Either m n) where&lt;br /&gt;&amp;gt;     type Canonical (Either m n) = Plus (Canonical m) (Canonical n)&lt;br /&gt;&amp;gt;     canonical (Left m) =&lt;br /&gt;&amp;gt;         let i = plus :: Either (Canonical m) (Canonical n) -&amp;gt; Plus (Canonical m) (Canonical n)&lt;br /&gt;&amp;gt;         in  i (Left (canonical m))&lt;br /&gt;&amp;gt;     canonical (Right n) =&lt;br /&gt;&amp;gt;         let i = plus :: Either (Canonical m) (Canonical n) -&amp;gt; Plus (Canonical m) (Canonical n)&lt;br /&gt;&amp;gt;         in  i (Right (canonical n))&lt;br /&gt;&amp;gt;     canonical' x =&lt;br /&gt;&amp;gt;         let i' = plus' :: Plus (Canonical m) (Canonical n) -&amp;gt; Either (Canonical m) (Canonical n)&lt;br /&gt;&amp;gt;         in case i' x of&lt;br /&gt;&amp;gt;             Left  m -&amp;gt; Left  (canonical' m)&lt;br /&gt;&amp;gt;             Right n -&amp;gt; Right (canonical' n)&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Now we need to do the same for multiplication. I'm beginning to feel sorry for the stress we're putting the compiler through:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&amp;gt; instance (Canonicable m, Canonicable n, Timesable (Canonical m) (Canonical n)) =&amp;gt; Canonicable (m, n) where&lt;br /&gt;&amp;gt;     type Canonical (m, n) = Times (Canonical m) (Canonical n)&lt;br /&gt;&amp;gt;     canonical (m, n) =&lt;br /&gt;&amp;gt;         let i = times :: (Canonical m, Canonical n) -&amp;gt; Times (Canonical m) (Canonical n)&lt;br /&gt;&amp;gt;         in  i (canonical m, canonical n)&lt;br /&gt;&amp;gt;     canonical' x =&lt;br /&gt;&amp;gt;         let i' = times' :: Times (Canonical m) (Canonical n) -&amp;gt; (Canonical m, Canonical n)&lt;br /&gt;&amp;gt;             (m, n) = times' x&lt;br /&gt;&amp;gt;         in (canonical' m, canonical' n)&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Now using the canonical forms we can build the isomorphism for any equation:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&amp;gt; iso :: (Canonical m ~ Canonical n, Canonicable m, Canonicable n) =&amp;gt; m -&amp;gt; n&lt;br /&gt;&amp;gt; iso m = canonical' (canonical m)&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;So let's return to 2*2+5=3*3. The isomorphism should be:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&amp;gt; test = iso :: Either (Two, Two) Five -&amp;gt; (Three, Three)&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;If we've done our job correctly, the compiler won't complain that it can't build the isomorphism.&lt;br /&gt;&lt;br /&gt;If you really want you can try running this code for a few values:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;&amp;gt; go1 = Left (zero, one)&lt;br /&gt;&amp;gt; go2 = Left (one, zero)&lt;br /&gt;&amp;gt; go3 = Right four&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Try writing code to implement the inverse, checking that it does give the inverse for these three cases.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Conclusions&lt;/b&gt;&lt;br /&gt;So there you have it, categorified arithmetic. Of course categorifying the naturals isn't so hard. But what does it mean to &lt;a href=&quot;http://math.ucr.edu/home/baez/twf_ascii/week188&quot;&gt;categorify the number&lt;/a&gt; &amp;pi;? You'll have to read some John Baez to find out more.&lt;br /&gt;&lt;br /&gt;There sort of is an application of the operations defined above. The type &lt;tt&gt;Three&lt;/tt&gt; say is the type of indices into a three element type. More generally, these natural number types give indices into fixed length containers and the addition and multiplication operations give type safe ways to map between containers that have the same size. This could be used to pack n-dimensional fixed size arrays into 1-dimensional arrays and vice-versa with compile-time checking of array indices. In practice, however, the compiler would need to be smart enough to realise it could use integers internally rather than the more complex structures it's probably using. But it's curious to see similar operations appear in some OpenCL array manipulation code I've been playing with.&lt;br /&gt;&lt;br /&gt;The code above isn't all that pretty. As I've said before: Haskell is two languages. There's the value level language and the type level one. The former is much prettier than the latter, especially if you can use type inference to eliminate the latter.&lt;br /&gt;&lt;br /&gt;By the way, you can view &lt;tt&gt;iso&lt;/tt&gt; as a command to trigger the Haskell compiler to prove there is an isomorphism between two types of a certain class. This is very similar to what a tactic in Coq does. In fact, the code I've written above is very similar to what a proof in Coq might look like. The main difference is that Coq gives you a helping hand and can fill in details whereas Haskell forces us to do all of the work ourselves.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;An Irrelevant Aside&lt;/b&gt;&lt;br /&gt;When I was still at high school a friend returned to visit after a few months at university. He'd been playing with Prolog and showed me how to define Peano arithmetic in that language. Since then, I've sort of been obsessed with squeezing Peano arithmetic out of every computational system that can do it. Hence my C++ code &lt;a href=&quot;http://homepage.mac.com/sigfpe/Computing/peano.html&quot;&gt;here&lt;/a&gt;. I looked him up on the web and it turns out he also wrote the original BSD automounter. Small world.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;img width=&quot;1&quot; height=&quot;1&quot; src=&quot;https://blogger.googleusercontent.com/tracker/11295132-1814777119392201914?l=blog.sigfpe.com&quot; alt=&quot;&quot; /&gt;&lt;/div&gt;</description>
	<pubDate>Sun, 07 Feb 2010 10:24:47 +0000</pubDate>
	<dc:creator>sigfpe (noreply@blogger.com)</dc:creator>
</item>
<item>
	<title>Roman Cheplyaka: hledger</title>
	<guid isPermalink="false">tag:blogger.com,1999:blog-3525174544175275879.post-3811139234303989115</guid>
	<link>http://ro-che.blogspot.com/2010/02/hledger.html</link>
	<description>&lt;p&gt;Since &lt;a href=&quot;http://v-dot-v.blogspot.com/&quot;&gt;Maria&lt;/a&gt; and I moved to Kiev in
September 2009 we actively use &lt;a href=&quot;http://hledger.org/&quot;&gt;hledger&lt;/a&gt; to
track our money.&lt;/p&gt;
&lt;p&gt;On average, we record 5 transactions each day&amp;nbsp;&amp;mdash; or 35 transactions
if you record them at the end of the week. In order for this operation not to
consume too much time, transaction entry should be very quick and easy. That's
why I made several changes to hledger so that it better fits my workflow.&lt;/p&gt;
&lt;p&gt;It took a while before my changes made their way to the main hledger
repository. Benefits of some of these patches may be not obvious, so I am going
to make a series of blog posts which describe my workflow and show how to
efficiently use hledger.&lt;/p&gt;
&lt;p&gt;I am going to start with the &lt;strong&gt;chart&lt;/strong&gt; facility. It is useful
e.g. when you want to know the structure of your expenses, like: where the most of the
money go, where you can save and where saving does not make sense etc.&lt;/p&gt;
&lt;p&gt;By the way, I find a lot of criticism of pie charts. But for this purpose I
don't see any good alternative.&lt;/p&gt;
&lt;p&gt;In order to create ledger charts you have to enable it. E.g. if you use
cabal-install, just type &quot;cabal install -fchart&quot; from inside hledger source
directory (as of 2010-02-06, hledger with chart facility is not yet
released). You will need &lt;a href=&quot;http://hackage.haskell.org/package/Chart&quot;&gt;Chart&lt;/a&gt;
package installed.&lt;/p&gt;
&lt;p&gt;Everything else is trivial. Just type &lt;code&gt;hledger chart ^Expenses&lt;/code&gt;
to get a fancy pie chart packed in a PNG file. Here's an example:&lt;br /&gt;
&lt;img src=&quot;http://lh3.ggpht.com/_W18npoo34JQ/S23kCTOsfrI/AAAAAAAAAOo/4xc8m-n9PtE/hledger.png&quot; alt=&quot;ledger pie chart&quot; /&gt;
&lt;/p&gt;
&lt;p&gt;Some tips:&lt;/p&gt;
&lt;ol&gt;
    &lt;li&gt;Plotting everything does not make sense since ledger has both positive
    and negative accounts. Make sure that you plot only accounts with the same
    sign. &amp;laquo;Expenses&amp;raquo; accounts are almost always positive.&lt;/li&gt;
    &lt;li&gt;Usual hledger filtering constructs apply. Some useful examples:
    &lt;ul&gt;
        &lt;li&gt;&lt;code&gt;hledger chart --depth 2 ^Expenses&lt;/code&gt;&amp;nbsp;&amp;mdash; plot only
        top-level subaccounts of Expenses&lt;/li&gt;
        &lt;li&gt;&lt;code&gt;hledger chart -p Dec ^Expenses&lt;/code&gt;&amp;nbsp;&amp;mdash;make a report for the
        last December&lt;/li&gt;
        &lt;li&gt;&lt;code&gt;hledger chart ^Expenses not:Rent&lt;/code&gt;&amp;nbsp;&amp;mdash; ignore
        Rent account (useful if its balance is too big relative to other
        expenses)&lt;/li&gt;
    &lt;/ul&gt;&lt;/li&gt;
    &lt;li&gt;chart command has some additional options. Use &lt;code&gt;hledger chart -o
        mychart.png --size 300x500 ^Expenses&lt;/code&gt; to specify output file name
    and dimensions of the image.&lt;/li&gt;
&lt;/ol&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;img width=&quot;1&quot; height=&quot;1&quot; src=&quot;https://blogger.googleusercontent.com/tracker/3525174544175275879-3811139234303989115?l=ro-che.blogspot.com&quot; alt=&quot;&quot; /&gt;&lt;/div&gt;</description>
	<pubDate>Sun, 07 Feb 2010 08:13:47 +0000</pubDate>
	<dc:creator>Roman Cheplyaka (noreply@blogger.com)</dc:creator>
</item>
<item>
	<title>Luke Palmer: Associative Alpha Blending</title>
	<guid isPermalink="false">http://lukepalmer.wordpress.com/?p=1147</guid>
	<link>http://lukepalmer.wordpress.com/2010/02/05/associative-alpha-blending/</link>
	<description>&lt;br /&gt;&lt;p&gt;I recently revamped my &lt;a href=&quot;http://hackage.haskell.org/package/graphics-drawingcombinators&quot;&gt;graphics-drawingcombinators&lt;/a&gt; module to have a handsome denotational semantics.  I may write a post going into full detail later, but the executive summary is that &lt;i&gt;Image a = R&lt;sup&gt;2&lt;/sup&gt; &amp;rarr; (Color, a)&lt;/i&gt;.  Due to &lt;a href=&quot;http://conal.net/papers/type-class-morphisms/&quot;&gt;TCM&lt;/a&gt;, I get semantics for Image&amp;#8217;s Functor instance from this, and if Color is a monoid I get the semantics for the Applicative and Monoid instances as well.  OpenGL combines colors by alpha blending, so I happily defined my monoid instance as alpha blending:&lt;/p&gt;
&lt;pre&gt;
 mempty = Color 0 0 0 0
 mappend c@(Color _ _ _ a) c' = a*c + (1-a)*c'
&lt;/pre&gt;
&lt;p&gt;It doesn&amp;#8217;t take a genius to see that this violates two of the three monoid laws (the only one it satisfies is left unit: &lt;tt&gt;mempty `mappend` x = x&lt;/tt&gt;).  This is embarrassing.  My new rigorous denotational semantics has a pretty awesome flaw. &lt;/p&gt;
&lt;p&gt;To make this right, let&amp;#8217;s redefine Color not as something which is drawn, but as a transformation on alpha-free colors.  I do this because functions make great monoids: composition is always associative and identity is always a unit.  But it&amp;#8217;s not just any function, it&amp;#8217;s an alpha blending function.  So we say that &lt;i&gt;f&lt;/i&gt; is a &amp;#8220;Color&amp;#8221; if there exists constants &lt;i&gt;a&lt;/i&gt; and &lt;i&gt;x&lt;/i&gt; such that &lt;i&gt;f(c) = a x + (1-a) c&lt;/i&gt;, which I will write as &lt;i&gt;f = [a; x]&lt;/i&gt;.  Here &lt;i&gt;a&lt;/i&gt; is a scalar and &lt;i&gt;x&lt;/i&gt; is another alpha-free color like &lt;i&gt;c&lt;/i&gt;.  We would really like it if the composition of two Colors were also a Color.  Let&amp;#8217;s try:&lt;/p&gt;
&lt;pre&gt;
f(g(c)) = [fa;fx]([ga;gx](c))
        = fa fx + (1 - fa) ([ga;gx](c))
        = fa fx + (1 - fa) (ga gx + (1 - ga) c)
        = fa fx + (1 - fa) ga gx + (1 - fa) (1 - ga) c
        = fa fx + (1 - fa) ga gx + (1 - fa - ga + fa ga) c
        = (fa fx + (1 - fa) ga gx) + (1 - (fa + ga - fa ga)) c
&lt;/pre&gt;
&lt;p&gt;It looks like the &amp;#8220;alpha&amp;#8221; of our composition is &lt;i&gt;(fa + ga &amp;#8211; fa ga)&lt;/i&gt;.  Let&amp;#8217;s call this &lt;i&gt;a&amp;#8217;&lt;/i&gt;.  Now we have to get the left side of the addition into the form &lt;i&gt;a&amp;#8217; r&lt;/i&gt; for some r.  Let&amp;#8217;s just hack it: multiply and divide&lt;sup&gt;1&lt;/sup&gt; by &lt;i&gt;a&amp;#8217;&lt;/i&gt;.&lt;/p&gt;
&lt;pre&gt;
        = a' (fa fx + (1 - fa) ga gx) / a' + (1 - a') c
&lt;/pre&gt;
&lt;p&gt;And then we can read off the answer:&lt;/p&gt;
&lt;pre&gt;
[fa;fx] . [ga;gx] = [a' ; (fa fx + (1 - fa) ga gx) / a']
   where a' = fa + ga - fa ga
&lt;/pre&gt;
&lt;p&gt;For the identity, we need:&lt;/p&gt;
&lt;pre&gt;
a x + (1 - a) c = c
&lt;/pre&gt;
&lt;p&gt;Which is satisfied for &lt;i&gt;a = 0&lt;/i&gt; with &lt;i&gt;x = &lt;/i&gt; anything, so we can use [0,0].  &lt;/p&gt;
&lt;p&gt;Because we derived this from composition and identity, the laws &lt;i&gt;must&lt;/i&gt; hold.  The mathematically untrusting may wish to check this.&lt;/p&gt;
&lt;p&gt;And there you have it: my new Color monoid which actually satisfies the laws.  This is &lt;i&gt;the&lt;/i&gt; way to compose alpha-blended colors &amp;#8212; it reflects what happens if you draw one after the other, blending as you go.  This is the math that pre-blends any segment of that sequence. &lt;/p&gt;
&lt;p&gt;I should have known that OpenGL&amp;#8217;s colors were transformations all along, since the color of an object that you see can depend on the color you used to clear the screen.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;&lt;sup&gt;1&lt;/sup&gt; But what if &lt;i&gt;(fa + ga &amp;#8211; fa ga) = 0&lt;/i&gt;?  Fortunately, the only place this happens when these variables are between 0 and 1 is fa = ga = 0, which means both f and g are the identity color.&lt;/p&gt;
  &lt;a rel=&quot;nofollow&quot; href=&quot;http://feeds.wordpress.com/1.0/gocomments/lukepalmer.wordpress.com/1147/&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://feeds.wordpress.com/1.0/comments/lukepalmer.wordpress.com/1147/&quot; /&gt;&lt;/a&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;http://feeds.wordpress.com/1.0/godelicious/lukepalmer.wordpress.com/1147/&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://feeds.wordpress.com/1.0/delicious/lukepalmer.wordpress.com/1147/&quot; /&gt;&lt;/a&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;http://feeds.wordpress.com/1.0/gostumble/lukepalmer.wordpress.com/1147/&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://feeds.wordpress.com/1.0/stumble/lukepalmer.wordpress.com/1147/&quot; /&gt;&lt;/a&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;http://feeds.wordpress.com/1.0/godigg/lukepalmer.wordpress.com/1147/&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://feeds.wordpress.com/1.0/digg/lukepalmer.wordpress.com/1147/&quot; /&gt;&lt;/a&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;http://feeds.wordpress.com/1.0/goreddit/lukepalmer.wordpress.com/1147/&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://feeds.wordpress.com/1.0/reddit/lukepalmer.wordpress.com/1147/&quot; /&gt;&lt;/a&gt; &lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://stats.wordpress.com/b.gif?host=lukepalmer.wordpress.com&amp;amp;blog=5292379&amp;amp;post=1147&amp;amp;subd=lukepalmer&amp;amp;ref=&amp;amp;feed=1&quot; /&gt;</description>
	<pubDate>Sat, 06 Feb 2010 19:02:09 +0000</pubDate>
	<dc:creator>Luke</dc:creator>
</item>
<item>
	<title>John Goerzen (CosmicRay): The Big-Publisher Ebook Scam</title>
	<guid isPermalink="false">http://changelog.complete.org/?p=1315</guid>
	<link>http://changelog.complete.org/archives/1315-the-big-publisher-ebook-scam</link>
	<description>&lt;p&gt;There&amp;#8217;s been a lot written about the Amazon vs. Macmillan dust-up.  I&amp;#8217;ve seen a lot of posts by people that work for publishers saying that there are costs to making a book, and that $9.99 just won&amp;#8217;t cut it for an ebook.  They say that publishers invest in typesetting, editing, selection, art, and various stages of quality control.  All of that is true.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Too bad they aren&amp;#8217;t doing it with ebooks.&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;I&amp;#8217;ve had some books published, and while the process varies from publisher to publisher, the editing process usually involves technical editors (people that check my facts), copy editors (people that help the writing and grammar), cover designers, and QC staff.  Often I will see PDFs or printed pages at the final stage, and at that point can catch things like bad table formatting or lines split at inopportune places.  My point here is that there&amp;#8217;s a lot of editing going on, and there are many pairs of eyeballs looking at the printed page before it goes to the presses.&lt;/p&gt;
&lt;p&gt;In the year or so since I&amp;#8217;ve owned my Kindle, I can absolutely guarantee you that this process is &lt;b&gt;not&lt;/b&gt; happening with ebooks.  Most of the time, it is quite obvious that nobody has even looked at the finished product.  Some intern has whipped up a quick conversion from whatever typesetting software they use, give it a quick glance, and call it good.  One of my own books, Real World Haskell, is available in Kindle form.  O&amp;#8217;Reilly took better than average care of that process, but even so, I certainly didn&amp;#8217;t approve screenshots before it went out like I did for paper (not that I&amp;#8217;d have had time after the paper project was done anyway.)  From memory, some of the flaws I&amp;#8217;m aware of:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Huston Smith&amp;#8217;s &lt;a href=&quot;http://changelog.complete.org/archives/989-review-why-religion-matters-by-huston-smith&quot;&gt;Why Religion Matters&lt;/a&gt; included some illustrations, but they were scanned at such a low resolution that the text was completely unreadable, even at maximum zoom.&lt;/li&gt;
&lt;li&gt;All electronic editions of Lord of the Rings, not just the Kindle version, suffered &lt;a href=&quot;http://www.amazon.com/review/RGR0OVZ4DM6QW/ref=cm_cr_pr_viewpnt#RGR0OVZ4DM6QW&quot;&gt;some serious issues&lt;/a&gt; including missing words and &lt;a href=&quot;http://www.mobileread.com/forums/showthread.php?t=45212&amp;#038;page=15&quot;&gt;deleting 19 characters after each instance of a particular letter&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Shmoop converted Project Gutenberg versions of The Iliad and Odyssey, but &lt;a href=&quot;http://changelog.complete.org/archives/1269-review-shmoops-coverage-of-homers-iliad&quot;&gt;converted each line of Gutenberg text to a Kindle paragraph&lt;/a&gt;, wrecking word wrapping.&lt;/li&gt;
&lt;li&gt;Countless examples of hyphenated words in the middle of lines (probably from someone forcing a hyphenation point in a print edition), paragraphs that should have been block-indented but weren&amp;#8217;t, etc.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In some of these cases, it is quite obvious that a human didn&amp;#8217;t even bother to look at the result.  Harper Collins got a huge black eye after their LOTR fiasco, and still took quite a long time to fix it.&lt;/p&gt;
&lt;p&gt;Now, if the publishers were actually going to put as much care into the quality of their ebooks as they do into the quality of their paper books, then sure, I&amp;#8217;d pay almost the cost of a paperback.  But very few of them are doing that.  It is quite obvious to me usually by the end of the first chapter of a book whether anybody even looked at the result of their conversion.&lt;/p&gt;
&lt;p&gt;Bottom line: If they&amp;#8217;re going to sell me an inferior product, don&amp;#8217;t expect me to pay near full price.  If they can get their act together on quality, &lt;b&gt;only then&lt;/b&gt; would they have room to start arguing for higher prices.  If all you&amp;#8217;re going to do with the ebook is run the paper version through some buggy filter, you haven&amp;#8217;t incurred much additional cost, and it is plainly visible to all.&lt;/p&gt;
&lt;p&gt;Note: I would like to say that Lonely Planet and O&amp;#8217;Reilly have done good jobs with the tools available, and while their results aren&amp;#8217;t perfect, they have done a good job working with rendering their sometimes very complex print layouts for a Kindle.&lt;/p&gt;</description>
	<pubDate>Sat, 06 Feb 2010 04:07:32 +0000</pubDate>
	<dc:creator>John Goerzen</dc:creator>
</item>
<item>
	<title>Bryn Keller: How to Measure Anything</title>
	<guid isPermalink="false">http://www.xoltar.org/?p=25</guid>
	<link>http://www.xoltar.org/?p=25</link>
	<description>&lt;p&gt;&lt;a href=&quot;http://www.howtomeasureanything.com&quot;&gt;&lt;img title=&quot;how-to-measure-anything&quot; border=&quot;0&quot; alt=&quot;how-to-measure-anything&quot; src=&quot;http://www.xoltar.org/blog/wp-content/uploads/howtomeasureanything2.jpg&quot; width=&quot;132&quot; height=&quot;197&quot; /&gt;&lt;/a&gt;
&lt;p&gt;&amp;#160;&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;We find no sense in talking about something unless we specify how we measure it; a definition by the method of measuring a quantity is the one sure way of avoiding talking nonsense&amp;#8230;      &lt;br /&gt;- Sir Hermann Bondi&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This book was a great read and completely changed the way I think about measurement. It offers many techniques for taking the data you already have and analyzing them in ways that allow you make better decisions. It also teaches you how to evaluate what kinds of additional information would be most helpful to solving your problem, and even put a dollar value on how much it would be worthwhile to spend get that information in order to make a better decision.&lt;/p&gt;
&lt;p&gt;Most importantly, it does an excellent job of challenging the basic assumption that some things just aren’t measurable. After reading this book, I feel like there’s always &lt;em&gt;something&lt;/em&gt; I can do to help get the information I need, understand my problem better, and make a better decision. I heartily recommend this book.&lt;/p&gt;&lt;/p&gt;</description>
	<pubDate>Fri, 05 Feb 2010 21:59:00 +0000</pubDate>
	<dc:creator>xoltar</dc:creator>
</item>
<item>
	<title>Neil Brown: Progression: Supporting Optimisation in Haskell</title>
	<guid isPermalink="false">http://chplib.wordpress.com/?p=447</guid>
	<link>http://chplib.wordpress.com/2010/02/04/progression-supporting-optimisation-in-haskell/</link>
	<description>&lt;br /&gt;&lt;h4&gt;Progression&lt;/h4&gt;
&lt;p&gt;I have recently been working on optimising CHP.  In order to optimise a program, you need a set of benchmarks/tasks for which you want to improve performance.  Then, you need to record how long the original version takes to complete the benchmark, record how long a changed version takes to complete the benchmark and compare the two to see if your change was sufficiently beneficial.  This process is tedious, and staring at two columns of numbers is not always instructive, so I constructed a small Haskell library to help with optimisation.  I&amp;#8217;ve called it Progression, and it is &lt;a href=&quot;http://hackage.haskell.org/package/progression&quot;&gt;now available on Hackage&lt;/a&gt;.  Progression allows you to specify some benchmarks, run them, and graph their performance against each other; so what you get is a graph like this:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://chplib.files.wordpress.com/2010/02/plot1.png&quot;&gt;&lt;img src=&quot;http://chplib.files.wordpress.com/2010/02/plot1.png?w=512&amp;#038;h=384&quot; alt=&quot;&quot; title=&quot;Progression Example Plot&quot; width=&quot;512&quot; height=&quot;384&quot; class=&quot;aligncenter size-full wp-image-1257&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Each line is a different version of my CHP library, and each group of points is a different benchmark.  (The versions were made in the order purple, blue, green, red; I ultimately stuck with the green version.)&lt;/p&gt;
&lt;h4&gt;Using Progression&lt;/h4&gt;
&lt;p&gt;Progression uses the excellent &lt;a href=&quot;http://hackage.haskell.org/package/criterion&quot;&gt;Criterion&lt;/a&gt; library for doing the benchmarking, so it is used in a similar fashion to Criterion.  You construct a small wrapper program that defines/imports your benchmarks and passes them to the Progression library to be run.  For example, here is the end of my file containing my CHP benchmarks:&lt;/p&gt;
&lt;pre&gt;runAll = defaultMain &lt;b&gt;.&lt;/b&gt; bgroup &quot;&quot; &lt;b&gt;.&lt;/b&gt; map (uncurry bench &lt;b&gt;.&lt;/b&gt; second runCHP_)
main = runAll [(&quot;SimpleChannel&quot;, simpleChannel)
              ,(&quot;SimpleChoice 2&quot;, choice 2)
              ,(&quot;SimpleChoice 5&quot;, choice 5)
              ,(&quot;SimpleChoice 10&quot;, choice 10)
              ]
&lt;/pre&gt;
&lt;p&gt;My runAll function turns the second part of each pair from a &lt;tt&gt;CHP ()&lt;/tt&gt; type into &lt;tt&gt;IO ()&lt;/tt&gt;, then I map the (Criterion) function bench over the list, use the (Criterion) function bgroup to join them all together, then pass them to the (Progression) function defaultMain.  I compile this file into an executable.&lt;/p&gt;
&lt;p&gt;When you run the program, you can either pass in settings via the command-line arguments, or if they are not present, you will be prompted with an interactive tab-completing prompt (thanks to the easy-to-use &lt;a href=&quot;http://hackage.haskell.org/package/haskeline&quot;&gt;Haskeline&lt;/a&gt; library).  There are three main settings:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;which benchmark groups you want to run (if you only have one group, you won&amp;#8217;t be prompted), &lt;/li&gt;
&lt;li&gt;what to label the recording you are about to make, and&lt;/li&gt;
&lt;li&gt;which previous labels you want to compare against in the graph.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Once you&amp;#8217;ve entered these two or three items, the benchmarks will all be run by Criterion, the results stored into a CSV file (using the handy &lt;a href=&quot;http://hackage.haskell.org/package/txt-sushi&quot;&gt;txt-sushi&lt;/a&gt; library; this way you can easily manually inspect the data or further process it in a spreadsheet program), which is then combined with the previous stored CSV files and fed to GNUplot.  (Unfortunately the gnuplot binding in Haskell is not in great shape at the moment, so I&amp;#8217;m using a plain system call to run it &amp;#8212; if you want graphs, make sure you have GNUplot installed on your system.)  The program creates a graph like the one shown at the beginning of the post &amp;#8212; by default in the file plot.png (but again, configurable by command-line option).  If, later on, you want to just graph previous results against each other, you can do that by running the program with &amp;#8220;-m graph&amp;#8221;.  On the graph you get points plotted at the means (staggered for each benchmark to aid readability and comparison) and error bars for the bounds that Criterion gives &amp;#8212; by default these are the 95% confidence intervals, but that is configurable, too (by passing through an option to Criterion).&lt;/p&gt;
&lt;h4&gt;Installing Progression&lt;/h4&gt;
&lt;p&gt;1. Make sure gnuplot is installed on your system and available in your path.  Gnuplot is very likely to be in your package manager if you&amp;#8217;re on Linux.&lt;br /&gt;
2. &lt;tt&gt;cabal update &amp;amp;&amp;amp; cabal install progression&lt;/tt&gt;&lt;/p&gt;
&lt;p&gt;Note that by default, Criterion uses the Chart library for plotting (a feature of Criterion that Progression does not use), which is in turn dependent on gtk2hs, which can be problematic for some people to install.  If you get messages about cairo not being installed and you don&amp;#8217;t want to install gtk2hs, you can install Criterion without this support for plotting (and thus without the gtk2hs dependency).  The command &lt;tt&gt;cabal install criterion -f-Chart&lt;/tt&gt; should do this (the minus between f and Chart is crucial), but unfortunately it seems after that, that you must install progression manually by downloading it and running Setup.lhs (I had hoped &lt;tt&gt;cabal install progression&lt;/tt&gt; would work but that seems to attempt to satisfy the Chart dependency even though criterion is by then installed).&lt;/p&gt;
&lt;h4&gt;Sharing Progression&lt;/h4&gt;
&lt;p&gt;I&amp;#8217;ve now &lt;a href=&quot;http://www.patch-tag.com/r/twistedsquare/progression&quot;&gt;uploaded the progression repository onto patch-tag&lt;/a&gt;.  You can get a copy using &lt;tt&gt;darcs get http://patch-tag.com/r/twistedsquare/progression&lt;/tt&gt; if you want the very latest version or wish to contribute some patches.&lt;/p&gt;
&lt;h4&gt;Issues with the 0.1 release&lt;/h4&gt;
&lt;p&gt;I realise that the graph is technically invalid.  I shouldn&amp;#8217;t connect the points with a line because the X-axis is discrete, not continuous.  However, without the line (i.e. with just points and error bars) it&amp;#8217;s much less readable at a glance, and a bar chart with error bars didn&amp;#8217;t seem too readable either when I tried it.  The graph display still isn&amp;#8217;t perfect though; it works best when you have benchmarks that take roughly similar times, and if you make one huge saving on one benchmark, as I did (a factor of about 100), this throws off the whole display.  Normalising all the times, so that one of the versions has all its times normalised to one, would partially fix the problems.   Also, if you run a lot of benchmarks, the CSV files do start to litter the directory; I wonder if I should store them in a subdirectory.&lt;/p&gt;
&lt;h4&gt;Pipe Dream&lt;/h4&gt;
&lt;p&gt;That&amp;#8217;s the summary of Progression.  I hope that Progression helps people who are optimising Haskell programs (especially alongside profiling and tools such as &lt;a href=&quot;http://research.microsoft.com/en-us/projects/threadscope/&quot;&gt;ThreadScope&lt;/a&gt;, that can help pinpoint possible candidates for optimisation).  But the work-flow is not perfect.  Often, not all the benchmarks are written and complete when you start benchmarking; you both make changes and add new benchmarks as you work.  Currently, when graphing, Progression ignores any benchmarks for which it does not have data for every recording being graphed (this should perhaps be fixed).  Ideally, you would re-run the old version of your library/program (for example, the original version before you began optimising) with the benchmarks to get some data.  For this, we need access to an old version.  All developers store their projects in version control (and with Haskell, that&amp;#8217;s typically darcs) so we could automatically pull out the old version from there rather than making the programmer do it themselves.&lt;/p&gt;
&lt;p&gt;So perhaps what we would do is dig through the version history for the latest tag starting &amp;#8220;OPT:&amp;#8221; (which is the &amp;#8220;original&amp;#8221; for this current round of optimisation), then re-run that version with the latest benchmarks.  In fact, why restrict it to just the original?  We could look back to find the last tag with &amp;#8220;OPT:&amp;#8221;, then work forwards from there, looking for patches, marking out those starting &amp;#8220;BENCH:&amp;#8221; (the ones that introduce new benchmarks).  We would then try and create versions of your program from the OPT tag forwards, with all the different versions since then, but always featuring all the BENCH patches.  This would give us complete benchmark data for all old versions, and would also mean you wouldn&amp;#8217;t have to tell Progression what the labels are, it could just use the patch names/dates.  We could also try different combinations of patches (if you&amp;#8217;ve recorded A, B, and C, where B depends on A, try A, A&amp;amp;B, A&amp;amp;B&amp;amp;C, A&amp;amp;C) to see how different independent changes combine together to find the fastest combination.&lt;/p&gt;
&lt;p&gt;I&amp;#8217;m not sure how much work that would be, but it sounds like it might form quite a useful tool.  Or, it might turn out to be too over-the-top and awkward &amp;#8212; it might be best to just stick to Progression&amp;#8217;s current straightforward design.  Comments are welcome below, as are any  questions or feedback about the current release of Progression.&lt;/p&gt;
  &lt;a rel=&quot;nofollow&quot; href=&quot;http://feeds.wordpress.com/1.0/gocomments/chplib.wordpress.com/447/&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://feeds.wordpress.com/1.0/comments/chplib.wordpress.com/447/&quot; /&gt;&lt;/a&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;http://feeds.wordpress.com/1.0/godelicious/chplib.wordpress.com/447/&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://feeds.wordpress.com/1.0/delicious/chplib.wordpress.com/447/&quot; /&gt;&lt;/a&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;http://feeds.wordpress.com/1.0/gostumble/chplib.wordpress.com/447/&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://feeds.wordpress.com/1.0/stumble/chplib.wordpress.com/447/&quot; /&gt;&lt;/a&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;http://feeds.wordpress.com/1.0/godigg/chplib.wordpress.com/447/&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://feeds.wordpress.com/1.0/digg/chplib.wordpress.com/447/&quot; /&gt;&lt;/a&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;http://feeds.wordpress.com/1.0/goreddit/chplib.wordpress.com/447/&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://feeds.wordpress.com/1.0/reddit/chplib.wordpress.com/447/&quot; /&gt;&lt;/a&gt; &lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://stats.wordpress.com/b.gif?host=chplib.wordpress.com&amp;amp;blog=9351306&amp;amp;post=447&amp;amp;subd=chplib&amp;amp;ref=&amp;amp;feed=1&quot; /&gt;</description>
	<pubDate>Fri, 05 Feb 2010 21:46:14 +0000</pubDate>
	<dc:creator>Neil Brown</dc:creator>
</item>
<item>
	<title>Kevin Reid (kpreid)</title>
	<guid isPermalink="false">http://kpreid.livejournal.com/21542.html</guid>
	<link>http://kpreid.livejournal.com/21542.html</link>
	<description>&lt;blockquote&gt;The people who have come to rely on features that are actually implementation errors are called ‘mistakeholders’.&lt;/blockquote&gt;
&lt;div&gt;— Chip Morningstar, today's &lt;a href=&quot;http://groups.google.com/group/friam&quot;&gt;friam&lt;/a&gt;&lt;/div&gt;</description>
	<pubDate>Fri, 05 Feb 2010 19:53:23 +0000</pubDate>
	<dc:creator>kpreid@mac.com</dc:creator>
</item>
<item>
	<title>Joachim Breitner: FontForge-Article in the German Linux-Magazin</title>
	<guid isPermalink="false">https://www.joachim-breitner.de/blog/archives/371-guid.html</guid>
	<link>https://www.joachim-breitner.de/blog/archives/371-FontForge-Article-in-the-German-Linux-Magazin.html</link>
	<description>&lt;p&gt;Yesterday, I found the 3/10-issue of the German “&lt;a href=&quot;http://www.linux-magazin.de/Heft-Abo/Ausgaben/2010/03&quot;&gt;Linux-Magazin&lt;/a&gt;” in my mailbox. (I don’t dare to call it the March issue – they are a bit off schedule...) On page 62, you can find my 3½ page article about creating a symbol font with &lt;a href=&quot;http://fontforge.sourceforge.net/&quot;&gt;FontForge&lt;/a&gt;. I &lt;a href=&quot;http://www.joachim-breitner.de/blog/archives/310-My-very-first-font.html&quot;&gt;briefly covered the topic&lt;/a&gt; on my blog and later thought that it would made a nice article, even though I’m not an expert on this area. The article will be freely available in about three years.This is already my third publication, after my article on the &lt;a href=&quot;http://www.joachim-breitner.de/blog/archives/56-Like-XSS,-just-simpler-and-harder-to-prevent-The-Cross-Site-Auth-XSA-Attack.html&quot;&gt;Cross-Site-Authentication attack&lt;/a&gt; that was published &lt;a href=&quot;http://www.joachim-breitner.de/blog/archives/84-Erste-Schritte-im-Journalismus.html&quot;&gt;in the same magazine&lt;/a&gt; (circulation ~63.000) and in &lt;a href=&quot;http://www.joachim-breitner.de/blog/archives/108-International-Journalism-and-me.html&quot;&gt;its international counterpart&lt;/a&gt; in 2005 and my recent &lt;a href=&quot;http://www.joachim-breitner.de/blog/archives/354-Second-Post!.html&quot;&gt;article in the “freeX” magazine&lt;/a&gt; (circulation ~15.000). Looks like I’ll have to add a&amp;#160; “Publications” section to my website soon...&lt;/p&gt;</description>
	<pubDate>Thu, 04 Feb 2010 21:50:11 +0000</pubDate>
	<dc:creator>mail@joachim-breitner.de (nomeata)</dc:creator>
</item>
<item>
	<title>Kevin Reid (kpreid): “Not an exit”</title>
	<guid isPermalink="false">http://kpreid.livejournal.com/21477.html</guid>
	<link>http://kpreid.livejournal.com/21477.html</link>
	<description>&lt;p&gt;This is the door by which I &lt;em&gt;exit&lt;/em&gt; the dorm every morning:&lt;/p&gt;

&lt;blockquote&gt;&lt;a href=&quot;http://switchb.org/kpreid/2009/not-an-exit&quot;&gt;&lt;img src=&quot;http://switchb.org/kpreid/2009/not-an-exit&quot; alt=&quot;316 STAIRWAY/NOT AN EXIT/B&quot; /&gt;&lt;/a&gt;&lt;/blockquote&gt;

&lt;p&gt;This is what Geoffrey K. Pullum of Language Log calls “&lt;a href=&quot;http://languagelog.ldc.upenn.edu/nll/?cat=106&quot;&gt;nerdview&lt;/a&gt;”. By what definition is this not an exit? Fire safety! This stairwell does not have an exterior door at ground level, but only leads back into the building (right next to the lobby and main entrance). Therefore it is &lt;em&gt;not an exit&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;But for the ordinary mind, this sign is crying wolf. You'd think they could have said “Do not use in case of fire” or “Not an emergency exit”.&lt;/p&gt;</description>
	<pubDate>Thu, 04 Feb 2010 20:28:10 +0000</pubDate>
	<dc:creator>kpreid@mac.com</dc:creator>
</item>
<item>
	<title>Lee Pike: &amp;#8220;Schrodinger&amp;#8217;s Probability&amp;#8221; for Error-Checking Codes</title>
	<guid isPermalink="false">http://leepike.wordpress.com/?p=108</guid>
	<link>http://leepike.wordpress.com/2009/05/15/schrodingers-ratio-for-error-checking-codes/</link>
	<description>&lt;div class=&quot;snap_preview&quot;&gt;&lt;br /&gt;&lt;p&gt;In a &lt;a href=&quot;http://leepike.wordpress.com/2009/04/18/reconsidering-cyclic-redundancy-checks-crc/&quot;&gt;previous post&lt;/a&gt;, I discussed the notion of &lt;em&gt;Schrödinger CRCs&lt;/em&gt;, first described by Kevin Driscoll et al. in their paper &lt;a href=&quot;http://www.springerlink.com/content/hx63utlym13x85nv/&quot;&gt;&lt;em&gt;Byzantine Fault Tolerance, from Theory to Reality&lt;/em&gt;&lt;/a&gt;.  The basic idea is that error-detecting codes do not necessarily prevent two receivers from obtaining messages that are semantically different (i.e., different data) but syntactically valid (i.e., the CRC matches the respective data words received).  The upshot is that even with CRCs, you can suffer &lt;a href=&quot;http://en.wikipedia.org/wiki/Byzantine_Fault_Tolerance&quot;&gt;Byzantine faults&lt;/a&gt;, with some probability.&lt;/p&gt;
&lt;p&gt;&amp;#8230; So what &lt;em&gt;is&lt;/em&gt; that probability of a Schrödinger’s CRC?  That&amp;#8217;s the topic of this post.  I&amp;#8217;m trying out these ideas for the first time, so feedback is appreciated.&lt;/p&gt;
&lt;h2&gt;Background&lt;/h2&gt;
&lt;p&gt;To begin, recall the idea of a &lt;a href=&quot;http://en.wikipedia.org/wiki/Hamming_weight&quot;&gt;Hamming Weight&lt;/a&gt; (HW).  The HW is a function on the set of data words of a fixed length and a fixed number of bit-corruptions that may occur (in either the data word or its associated CRC code).  It returns the number of corruptions that the CRC fails to detect.  For example, for the &lt;a href=&quot;http://en.wikipedia.org/wiki/Parity_bit&quot;&gt;parity bit&lt;/a&gt; on data words of length three, the following is one instance in the HW count for 2-bit corruptions:&lt;/p&gt;
&lt;pre&gt;           data word  bit
sent       0 1 1      0
corrupted  1 0 1      0&lt;/pre&gt;
&lt;p&gt;The &lt;a href=&quot;http://en.wikipedia.org/wiki/Hamming_distance&quot;&gt;Hamming Distance&lt;/a&gt; (HD) for a fixed data word length and CRC is the smallest number of bit-corruptions resulting in a non-zero Hamming Weight (for the parity bit, it&amp;#8217;s two).  Koopan and Chakravarty &lt;a href=&quot;http://www.ece.cmu.edu/~koopman/roses/dsn04/koopman04_crc_poly_embedded.pdf&quot;&gt;document the HDs&lt;/a&gt; of a number of CRCs.&lt;/p&gt;
&lt;p&gt;While the concept of a HD might be appropriate for measuring the resilience of a CRC to random bit errors, it may not be a good metric for calculating the probability of Schrödinger’s CRCs.  Consider the following quotation from Paulitsch et al. in their paper, &lt;em&gt;&lt;a href=&quot;http://www.ece.cmu.edu/~koopman/pubs/paultisch05_dsn_crc_ultradependable.pdf&quot;&gt;Coverage and the Use of Cyclic Redundancy Codes in Ultra-Dependable Systems&lt;/a&gt;&lt;/em&gt;:&lt;/p&gt;
&lt;p&gt;&amp;#8230;Network inter-stages can exhibit arbitrary faults, accidentally forging valid CRC check sequences. These faults can dominate system dependability issues, resulting in undetected failures at the &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=10%5E%7B-6%7D&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;10^{-6}&quot; title=&quot;10^{-6}&quot; class=&quot;latex&quot; /&gt; component failure rate.&lt;/p&gt;
&lt;p&gt;The point here is that hardware that manipulates transmitted data (i.e., the inter-stages) may suffer random failures that cause correlated faults in the transmitted data.  For example, a bus driver may be &amp;#8220;stuck at 1/2&amp;#8243; so that it delivers an intermediate voltage that will be interpreted by some receives as a &amp;#8220;1&amp;#8243; and by others as a &amp;#8220;0&amp;#8243;.  Or an inter-stage may have a bad oscillator, causing it suffer a slightly-out-of-spec timing fault, so that different receivers interpret the incoming bits differently.&lt;/p&gt;
&lt;p&gt;In data corruption caused by these kinds of hardware failures, we must change our assumptions:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Data transmission faults are random but correlated.  By this I mean that we might presume that all bit errors are &amp;#8220;in the same direction&amp;#8221;: either 0s become 1s or 1s become 0s.  For example, with a stuck-at-1/2 fault in which a 1 is transmitted at a slightly too-high voltage (assuming 1s are denoted by a low signal), a receiver might interpret the signal arbitrarily, but consistently.&lt;/li&gt;
&lt;li&gt;Bit-error rates are much higher.  Paulitsch et al. note that typical assumed bit-error rates range from &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=10%5E%7B-6%7D&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;10^{-6}&quot; title=&quot;10^{-6}&quot; class=&quot;latex&quot; /&gt; to &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=10%5E%7B-13%7D&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;10^{-13}&quot; title=&quot;10^{-13}&quot; class=&quot;latex&quot; /&gt;, but actual rates vary wildly.  However, if there is a hardware failure (e.g., a stuck-at-1/2 fault), a dramatically higher BER (for correlated bit-errors) might be induced.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;We define &lt;em&gt;Schrödinger’s Hamming Distance&lt;/em&gt; (SHD) for a error-checking code as follows.  Suppose in a data word and its associated error-checking code all bit-errors are exclusively of one of the following type:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;0s may randomly be flipped to 1s.&lt;/li&gt;
&lt;li&gt;1s may randomly be flipped to 0s.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Then the &lt;em&gt;Schrödinger Hamming Weight&lt;/em&gt; (SHW) of the code for a fixed data word width is the number of undetected errors from (1) or (2).  We&amp;#8217;ll refer to a bit corruption that contributes to a non-zero SHW as a &lt;em&gt;Schrödinger CRC&lt;em&gt;. &lt;/em&gt;&lt;/em&gt;The &lt;em&gt;Schrödinger Hamming Distance&lt;/em&gt; is the least number of errors required for a non-zero SHW.&lt;/p&gt;
&lt;p&gt;The SHWs for a CRC and data word size are no greater than the respective HWs.  For example, the parity-bit example above would be included in the calculation of the Hamming Weight but would be excluded in the calculation of Schrödinger Hamming Weight (becuase we flipped a 0 to a 1 and a 1 to a zero).  However, the following would be included in both, since we only flip 0s to 1s:&lt;/p&gt;
&lt;pre&gt;           data word  bit
sent       1 0 1      0
corrupted  1 1 1      1&lt;/pre&gt;
&lt;p&gt;Clearly, SHWs can never be greater than HWs.  However, I do not know whether for some CRC and data word length, the Schrödinger Hamming Distance is ever &lt;em&gt;strictly greater&lt;/em&gt; than the Hamming Distance.  (My suspicion is not.)&lt;/p&gt;
&lt;p&gt;To calculate the probability of a Schrödinger CRC involves a small analytical analysis together with a some probabilistic simulation.  We&amp;#8217;ll describe the simulation first.&lt;/p&gt;
&lt;h2&gt;Simulations&lt;/h2&gt;
&lt;p&gt;For a fixed CRC and data word length, we&amp;#8217;ll randomly generate data words and then compute their CRCs, within some constraints.  We do not simulate arbitrary bit-flips, since we want our simulation to be as productive as possible&amp;#8212;we do not want to have to simulate 10s of millions of times to get reliable statisitical data.&lt;/p&gt;
&lt;p&gt;So without loss of generality, we&amp;#8217;ll simulate some number of 0s being flipped to 1s (we could just have easily simulated 1s being flipped to 0s) in the data word together with its CRC.  Of course, we can flip no more 0s than appear in the data word and CRC.  Furthermore, it does no good to simulate less than HD bit-errors, since the CRC is guaranteed to protect against any bit-errors up to (but not including) the HD.  So we&amp;#8217;ll simulate at least HD bit-errors.  If there are less than HD 0s in the data word and its CRC, then it is counted as a  test run in which the CRC successfully catches errors.&lt;/p&gt;
&lt;p&gt;To carry out the simulation, I wrote a little &lt;a href=&quot;http://leepike.wordpress.com/source-code/crc-hs/&quot;&gt;Haskell program&lt;/a&gt;.  (By the way, this is not the most beautiful or most efficient code possible, but I can do a million simulations in minutes on a commodity laptop.  Good enough for my purposes.)  The simulation uses &lt;a href=&quot;http://en.wikipedia.org/wiki/QuickCheck&quot;&gt;QuickCheck&lt;/a&gt;.  To execute it, you will need to add a small patch to QuickCheck&amp;#8212;see the patch and installation instructions &lt;a href=&quot;http://www.cs.indiana.edu/~lepike/pub_pages/qc-biphase.html&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;So this gives us some failure rate.  For example, for the &lt;a href=&quot;http://en.wikipedia.org/wiki/Cyclic_redundancy_check&quot;&gt;USB-5 CRC&lt;/a&gt; (&lt;img src=&quot;http://l.wordpress.com/latex.php?latex=x%5E5+%2B+x%5E2+%2B+1&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;x^5 + x^2 + 1&quot; title=&quot;x^5 + x^2 + 1&quot; class=&quot;latex&quot; /&gt;) on 11-bit data words (with a  HD of 3), after 10,000 simulations, we get somewhere around 3% of the generated runs producing bit-errors that result in the CRC failing to capture the fault.  For something like the &lt;a href=&quot;http://en.wikipedia.org/wiki/Controller_Area_Network&quot;&gt;CAN CRC&lt;/a&gt; (&lt;img src=&quot;http://l.wordpress.com/latex.php?latex=x%5E%7B15%7D+%2B+x%5E%7B14%7D+%2B+x%5E%7B10%7D+%2B+x%5E8+%2B+x%5E7+%2B+x%5E4+%2B+x%5E3+%2B+1&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;x^{15} + x^{14} + x^{10} + x^8 + x^7 + x^4 + x^3 + 1&quot; title=&quot;x^{15} + x^{14} + x^{10} + x^8 + x^7 + x^4 + x^3 + 1&quot; class=&quot;latex&quot; /&gt;) on 64-bit data words (with a HD of 6), after a million simulations, we get a failure rate of around &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=3.9+%5Ctimes+10%5E%7B-5%7D&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;3.9 \times 10^{-5}&quot; title=&quot;3.9 \times 10^{-5}&quot; class=&quot;latex&quot; /&gt;.&lt;/p&gt;
&lt;h2&gt;Calculations&lt;/h2&gt;
&lt;p&gt;Now for the analytical part.  For an arbitrary data word and its corresponding CRC, we need to calculate the probability of the simulation runs occurring.&lt;/p&gt;
&lt;p&gt;We do this as follows.  First, on average, if a data word together with its CRC comprises &lt;em&gt;n&lt;/em&gt; bits, we&amp;#8217;ll assume that roughly half are 1s and half are 0s.  Thus, the maximum number of 0s that could be flipped is approximately half the total number of bits in the data word and its CRC.&lt;/p&gt;
&lt;p&gt;If &lt;em&gt;hd&lt;/em&gt; is the Hamming Distance, then we want to sum the probabilities of suffering exactly &lt;em&gt;hd&lt;/em&gt;, &lt;em&gt;hd&lt;/em&gt;+1, &lt;em&gt;hd&lt;/em&gt;+2, &amp;#8230;, &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=%5Cfrac%7Bn%7D%7B2%7D&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;\frac{n}{2}&quot; title=&quot;\frac{n}{2}&quot; class=&quot;latex&quot; /&gt; bit-errors.  The total number of possible bit-errors for each number of corrupted bits is determined with the &lt;a href=&quot;http://en.wikipedia.org/wiki/Combination&quot;&gt;choose function&lt;/a&gt;:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;http://l.wordpress.com/latex.php?latex=%7Bn+%5Cchoose+i%7D+%3D+%5Cdfrac%7Bn%21%7D%7Bi%21%28n-i%29%21%7D&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;{n \choose i} = \dfrac{n!}{i!(n-i)!}&quot; title=&quot;{n \choose i} = \dfrac{n!}{i!(n-i)!}&quot; class=&quot;latex&quot; /&gt;&lt;/p&gt;
&lt;p&gt;So, for example, the total number of coin tosses in which exactly two coins show heads out of three tosses is&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;http://l.wordpress.com/latex.php?latex=%7B3+%5Cchoose+2%7D+%3D+%5Cdfrac%7B3%21%7D%7B2%21%283-2%29%21%7D+%3D+3&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;{3 \choose 2} = \dfrac{3!}{2!(3-2)!} = 3&quot; title=&quot;{3 \choose 2} = \dfrac{3!}{2!(3-2)!} = 3&quot; class=&quot;latex&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Now, if the probability of a bit-error is &lt;em&gt;p&lt;/em&gt;, then the probability of &lt;em&gt;i&lt;/em&gt; bit-errors is&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;http://l.wordpress.com/latex.php?latex=p%5Ei+%281-p%29%5E%7Bn-i%7D&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;p^i (1-p)^{n-i}&quot; title=&quot;p^i (1-p)^{n-i}&quot; class=&quot;latex&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Thus, the probability of any of the bit-errors introduced in our simulation occurring (regardless of whether they led to an undetected corruption), is&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;http://l.wordpress.com/latex.php?latex=%5Cdisplaystyle%5Csum%5E%7Bn%2F2%7D_%7Bi%3Dhd%7D%7B%5Cfrac%7Bn%7D%7B2%7D+%5Cchoose+i%7D+%5Ctimes+p%5Ei+%281-p%29%5E%7B%5Cfrac%7Bn%7D%7B2%7D+-+i%7D&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;\displaystyle\sum^{n/2}_{i=hd}{\frac{n}{2} \choose i} \times p^i (1-p)^{\frac{n}{2} - i}&quot; title=&quot;\displaystyle\sum^{n/2}_{i=hd}{\frac{n}{2} \choose i} \times p^i (1-p)^{\frac{n}{2} - i}&quot; class=&quot;latex&quot; /&gt;&lt;/p&gt;
&lt;p&gt;We multiply this summation by the percentage of undetected corruptions determined from the simulation.  So for USB-5 mentioned above on 11-bit data words, recall that our simulation showed a ~3% of the simulations resulting in a uncaught error.  If the probability &lt;em&gt;p&lt;/em&gt; of a single bit corruption is 0.01 (assuming the hardware is faulty), then&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;http://l.wordpress.com/latex.php?latex=%5Cdisplaystyle%5Csum%5E%7B%5Cfrac%7B16%7D%7B2%7D%7D_%7Bi%3D3%7D%7B%5Cfrac%7B16%7D%7B2%7D+%5Cchoose+i%7D+%5Ctimes+0.01%5Ei+%281-0.01%29%5E%7B%5Cfrac%7B16%7D%7B2%7D+-+i%7D+%3D+5.39+%5Ctimes+10%5E%7B-5%7D&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;\displaystyle\sum^{\frac{16}{2}}_{i=3}{\frac{16}{2} \choose i} \times 0.01^i (1-0.01)^{\frac{16}{2} - i} = 5.39 \times 10^{-5}&quot; title=&quot;\displaystyle\sum^{\frac{16}{2}}_{i=3}{\frac{16}{2} \choose i} \times 0.01^i (1-0.01)^{\frac{16}{2} - i} = 5.39 \times 10^{-5}&quot; class=&quot;latex&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;http://l.wordpress.com/latex.php?latex=5.39+%5Ctimes+10%5E%7B-5%7D&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;5.39 \times 10^{-5}&quot; title=&quot;5.39 \times 10^{-5}&quot; class=&quot;latex&quot; /&gt; multiplied by 3% gives us &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=1.617+%5Ctimes+10%5E%7B-6%7D&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;1.617 \times 10^{-6}&quot; title=&quot;1.617 \times 10^{-6}&quot; class=&quot;latex&quot; /&gt;.  For a single message, assuming a fault-arrival rate for bit corruptions, that&amp;#8217;s the probability of a Schrödinger CRC.&lt;/p&gt;
&lt;h2&gt;Combining the Results&lt;/h2&gt;
&lt;p&gt;But that number by itself is an incomplete portrayal of the arrival rate of Schrödinger CRCs in a system.  We must also take into account the following to get a system-wide Schrödinger CRC arrival-rate per hour of operation:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The number of inter-stages.&lt;/li&gt;
&lt;li&gt;The failure rate of the hardware in the inter-stages (particularly for failures that can lead to slightly-out-of-spec faults, so this includes transient faults).&lt;/li&gt;
&lt;li&gt;The number of receivers.&lt;/li&gt;
&lt;li&gt;The number of messages per hour.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Specifically, from the hardware failure rate and the number of inter-stages, we&amp;#8217;ll calculate the probability of hardware failure, and we&amp;#8217;ve already computed the probability of a Schrödinger CRC given a hardware failure.  Then we&amp;#8217;ll do a final calculation to get the probability per hour.&lt;/p&gt;
&lt;p&gt;For a concrete example, let&amp;#8217;s assume there are 10 inter-stages messages pass through, the failure rate of the hardware is &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=10%5E%7B-3%7D&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;10^{-3}&quot; title=&quot;10^{-3}&quot; class=&quot;latex&quot; /&gt; per hour (again, we&amp;#8217;re including transient faults, so we&amp;#8217;re using a high rate), and there are 20 receivers in the broadcast.  We&amp;#8217;ll assume a fairly slow data-rate, of 1 kilobit/second.&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s find the probability of a faulty inter-stage first.  If there are 10 inter-stages, and we assume the probability of hardware failures is independent, then the probability of at least one of them being faulty is &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=1+-+%28%2810%5E%7B-3%7D%29%5E0+%5Ctimes+%281+-+10%5E%7B-3%7D%29%5E%7B10%7D%29+%3D+9.96+%5Ctimes+10%5E%7B-3%7D&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;1 - ((10^{-3})^0 \times (1 - 10^{-3})^{10}) = 9.96 \times 10^{-3}&quot; title=&quot;1 - ((10^{-3})^0 \times (1 - 10^{-3})^{10}) = 9.96 \times 10^{-3}&quot; class=&quot;latex&quot; /&gt;.&lt;/p&gt;
&lt;p&gt;So now we have the probability of a uncaught error per message, assuming faulty hardware (&lt;img src=&quot;http://l.wordpress.com/latex.php?latex=1.617+%5Ctimes+10%5E%7B-6%7D&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;1.617 \times 10^{-6}&quot; title=&quot;1.617 \times 10^{-6}&quot; class=&quot;latex&quot; /&gt;) and the probability of faulty hardware (&lt;img src=&quot;http://l.wordpress.com/latex.php?latex=9.96+%5Ctimes+10%5E%7B-3%7D&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;9.96 \times 10^{-3}&quot; title=&quot;9.96 \times 10^{-3}&quot; class=&quot;latex&quot; /&gt;).  Multiplying these, we get that the probability of a Schrödinger CRC per message is &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=1.61+%5Ctimes+10%5E%7B-8%7D&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;1.61 \times 10^{-8}&quot; title=&quot;1.61 \times 10^{-8}&quot; class=&quot;latex&quot; /&gt;.  If the throughput is 1kb/s, then that&amp;#8217;s 225,000 16-bit messages/hour.  So the probability of a Schrödinger CRC per hour of operation is &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=1+-+%28%281.61+%5Ctimes+10%5E%7B-8%7D%29%5E0+%5Ctimes+%281+-+1.61+%5Ctimes+10%5E%7B-8%7D%29%5E%7B225%2C000%7D%29&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;1 - ((1.61 \times 10^{-8})^0 \times (1 - 1.61 \times 10^{-8})^{225,000})&quot; title=&quot;1 - ((1.61 \times 10^{-8})^0 \times (1 - 1.61 \times 10^{-8})^{225,000})&quot; class=&quot;latex&quot; /&gt; &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=%3D+3.61+%5Ctimes+10%5E%7B-3%7D&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;= 3.61 \times 10^{-3}&quot; title=&quot;= 3.61 \times 10^{-3}&quot; class=&quot;latex&quot; /&gt;.  That&amp;#8217;s a high rate, which is not acceptable for an ultra-reliable system that depends on CRCs to protect against Byzantine faults.  Of course, the constants we used in our calculations are system-specific, but I hope this suffices to demonstrate that Schrödinger CRCs should be of concern in reliable system design.&lt;/p&gt;
&lt;p&gt;For fun, let&amp;#8217;s run the numbers on the CAN CRC using the same constants as above.  Our simulations for CAN on 64-bit data words gives us a probability of a Schrödinger CRC, assuming faulty hardware, of &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=3.9+%5Ctimes+10%5E%7B-5%7D+%5Ctimes+7.248+%5Ctimes+10%5E%7B-7%7D+%3D+2.83+%5Ctimes+10%5E%7B-11%7D&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;3.9 \times 10^{-5} \times 7.248 \times 10^{-7} = 2.83 \times 10^{-11}&quot; title=&quot;3.9 \times 10^{-5} \times 7.248 \times 10^{-7} = 2.83 \times 10^{-11}&quot; class=&quot;latex&quot; /&gt;.  This gives a probability of a Schrödinger CRC at &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=2.82+%5Ctimes+10%5E%7B-13%7D&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;2.82 \times 10^{-13}&quot; title=&quot;2.82 \times 10^{-13}&quot; class=&quot;latex&quot; /&gt; per message or &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=1+-+%282.82+%5Ctimes+10%5E%7B-13%7D%29%5E56250+%3D+1.59+%5Ctimes+10%5E%7B-8%7D&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;1 - (2.82 \times 10^{-13})^56250 = 1.59 \times 10^{-8}&quot; title=&quot;1 - (2.82 \times 10^{-13})^56250 = 1.59 \times 10^{-8}&quot; class=&quot;latex&quot; /&gt;, potentially still too probable for ultra-dependable systems with required failure rates of &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=10%5E%7B-9%7D&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;10^{-9}&quot; title=&quot;10^{-9}&quot; class=&quot;latex&quot; /&gt; or lower.&lt;/p&gt;
&lt;h2&gt;Conclusions&lt;/h2&gt;
&lt;p&gt;What are the lessons to draw from this?&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;We&amp;#8217;ve described new concepts of Schrödinger Hamming Weights and Schrödinger Hamming Distance&lt;br /&gt;
as another metric for the quality of error-checking codes.&lt;/li&gt;
&lt;li&gt;We&amp;#8217;ve discussed how one might use a combination of simulation and probability calculations to discover the probability of Schrödinger CRCs in a system (assuming I got all my calculations correct!).&lt;/li&gt;
&lt;li&gt;Depending on the architecture and requirements, these calculations could be as important in determining system reliability as simply using the Hamming Distance of a CRC.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Open questions include&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;How Schrödinger Hamming Weights compare with Hamming Weights and similarly for Schrödinger Hamming Distances and Hamming Distances.  In particular, is the Schrödinger Hamming Distance ever strictly greater than the Hamming Distance?&lt;/li&gt;
&lt;li&gt;Are any CRCs well-suited to protect against Schrödinger CRCs?&lt;/li&gt;
&lt;li&gt;Under what constraints should Schrödinger HWs be the primary reliability measure in system design?  Should error-detecting codes ever be used to protect against Byzatine behavior?&lt;/li&gt;
&lt;/ol&gt;
  &lt;a rel=&quot;nofollow&quot; href=&quot;http://feeds.wordpress.com/1.0/gocomments/leepike.wordpress.com/108/&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://feeds.wordpress.com/1.0/comments/leepike.wordpress.com/108/&quot; /&gt;&lt;/a&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;http://feeds.wordpress.com/1.0/godelicious/leepike.wordpress.com/108/&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://feeds.wordpress.com/1.0/delicious/leepike.wordpress.com/108/&quot; /&gt;&lt;/a&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;http://feeds.wordpress.com/1.0/gostumble/leepike.wordpress.com/108/&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://feeds.wordpress.com/1.0/stumble/leepike.wordpress.com/108/&quot; /&gt;&lt;/a&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;http://feeds.wordpress.com/1.0/godigg/leepike.wordpress.com/108/&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://feeds.wordpress.com/1.0/digg/leepike.wordpress.com/108/&quot; /&gt;&lt;/a&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;http://feeds.wordpress.com/1.0/goreddit/leepike.wordpress.com/108/&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://feeds.wordpress.com/1.0/reddit/leepike.wordpress.com/108/&quot; /&gt;&lt;/a&gt; &lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://stats.wordpress.com/b.gif?host=leepike.wordpress.com&amp;amp;blog=7358124&amp;amp;post=108&amp;amp;subd=leepike&amp;amp;ref=&amp;amp;feed=1&quot; /&gt;&lt;/div&gt;</description>
	<pubDate>Thu, 04 Feb 2010 08:38:48 +0000</pubDate>
	<dc:creator>Lee Pike</dc:creator>
</item>
<item>
	<title>Bryn Keller: Tech Note: Finding All the Assemblies Available to Your Application</title>
	<guid isPermalink="false">http://www.xoltar.org/?p=23</guid>
	<link>http://www.xoltar.org/?p=23</link>
	<description>&lt;p&gt;I sometimes have .Net library code that needs to work both in web applications and in console or service applications. The differences between the two environments, even for a library, are sometimes surprising. For example, I recently needed to find all the assemblies available to (i.e., in the same folder with) the application. My first implementation worked fine for the command line app, but failed miserably for the web site. After trying a few different approaches, I found one that works well. I’m recording this for the benefit of anyone else who has to perform this trick.&lt;/p&gt;
&lt;p&gt;The “obvious” way to do this, I thought, was to use Assembly.GetExecutingAssembly().Location to find where the assembly lived, use DirectoryInfo.GetFiles() to find the dll and exe files in that location’s directory, and then use Assembly.LoadFile() to load them. This all works just fine in the command line application, but in a web site, there’s shadow copying involved – the assembly returned to you from GetExecutingAssembly isn’t in your Bin folder, it’s in a semi-randomly named folder nesteded several levels deep from your Temporary ASP.Net Files folder. To add difficulty, that one assembly (along with its pdb file, perhaps) is all that’s in the folder. Each of the assemblies in your Bin folder gets copied to a unique folder of its own before it’s loaded into the application, so there’s no easy way to find them all and iterate over them. I didn’t check this, but it’s entirely possible that the assembly isn’t copied at all until it needs to be loaded, in which case even scrubbing through the funny folders looking for assemblies wouldn’t work.&lt;/p&gt;
&lt;p&gt;So if Assembly.Location doesn’t work, what does? Something on the AppDomain, perhaps. AppDomain.CurrentDomain.BaseDirectory works for command line apps, but for web sites it gives you the root of the site, not the bin folder. For a web site, the Bin folder is in PrivateBinPath. PrivateBinPath is null for command line apps, though. So it turns out the way to find all your assemblies is by using PrivateBinPath if it’s not null, or else BaseDirectory.&lt;/p&gt;
&lt;p&gt;Now that we have a way to find all the available assemblies, what about loading them? If your application is doing shadow copying, you can’t use Assembly.LoadFromFile, since you’re working with assemblies from the Bin folder, but your application is loading them from the shadow copy directories with the funny names, which means if you need a type called “Foo.Bar.Baz,MyAssembly” and you load it from an assembly in the Bin folder, it won’t be compatible with the types that are already loaded in your application, because they were all loaded from different assemblies in the shadow copy folders. &lt;/p&gt;
&lt;p&gt;A better way to load them is to ask the AppDomain to Load() them. Get the name from the file, strip the extension, pass it to AppDomain.CurrentDomain.Load(). That way it will load types from the correct assemblies – either from the application directory, or from the shadow copy directories, whichever is correct for your application.&lt;/p&gt;</description>
	<pubDate>Thu, 04 Feb 2010 03:59:00 +0000</pubDate>
	<dc:creator>xoltar</dc:creator>
</item>
<item>
	<title>Lee Pike: 10 to the -9</title>
	<guid isPermalink="false">http://leepike.wordpress.com/?p=235</guid>
	<link>http://leepike.wordpress.com/2010/01/24/10-9/</link>
	<description>&lt;div class=&quot;snap_preview&quot;&gt;&lt;br /&gt;&lt;p&gt;&lt;img src=&quot;http://l.wordpress.com/latex.php?latex=10%5E%7B-9%7D&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;10^{-9}&quot; title=&quot;10^{-9}&quot; class=&quot;latex&quot; /&gt;, or one-in-a-billion, is the famed number given for the maximum probability of a catastrophic failure, per hour of operation, in life-critical systems like commercial aircraft.  The number is part of the folklore of the safety-critical systems literature; where does it come from?&lt;/p&gt;
&lt;p&gt;First, it&amp;#8217;s worth noting just how small that number is.  As pointed out by Driscoll &lt;em&gt;et al&lt;/em&gt;. in the paper, &lt;a href=&quot;http://books.google.com/books?id=N4tGyPJ5qYkC&amp;amp;pg=PA235&amp;amp;lpg=PA235&amp;amp;dq=byzantine++from+theory+to+reality&amp;amp;source=bl&amp;amp;ots=rx0FcInyRS&amp;amp;sig=tVxuS1kaN8Mhkr0Mks8wuN2Zj3c&amp;amp;hl=en&amp;amp;ei=-XVYS7XWKI7KsQOF5PjFBw&amp;amp;sa=X&amp;amp;oi=book_result&amp;amp;ct=result&amp;amp;resnum=4&amp;amp;ved=0CBsQ6AEwAw#v=onepage&amp;amp;q=byzantine%20%20from%20theory%20to%20reality&amp;amp;f=false&quot;&gt;Byzantine Fault Tolerance, from Theory to Reality&lt;/a&gt;, the probability of winning the U.K. lottery is 1 in 10s of millions, and the probability of being struck by lightening (in the U.S.) is &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=1.6+%5Ctimes+10%5E%7B-6%7D%2C&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;1.6 \times 10^{-6},&quot; title=&quot;1.6 \times 10^{-6},&quot; class=&quot;latex&quot; /&gt; more than a 1,000 times more likely than &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=10%5E%7B-9%7D.&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;10^{-9}.&quot; title=&quot;10^{-9}.&quot; class=&quot;latex&quot; /&gt;&lt;/p&gt;
&lt;p&gt;So where did &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=10%5E%7B-9%7D&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;10^{-9}&quot; title=&quot;10^{-9}&quot; class=&quot;latex&quot; /&gt; come from?  A nice explanation comes from &lt;a href=&quot;http://www.csl.sri.com/users/rushby/abstracts/sefm09&quot;&gt;a recent paper by John Rushby&lt;/a&gt;:&lt;/p&gt;
&lt;p&gt;If we consider the example of an airplane type with 100 members, each flying &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=3000&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;3000&quot; title=&quot;3000&quot; class=&quot;latex&quot; /&gt; hours per year over an operational life of 33 years, then we have a total exposure of about 107 flight hours. If hazard analysis reveals ten potentially catastrophic failures in each of ten subsystems, then the &amp;#8220;budget&amp;#8221; for each, if none are expected to occur in the life of the fleet, is a failure probability of about &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=10%5E%7B-9%7D&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;10^{-9}&quot; title=&quot;10^{-9}&quot; class=&quot;latex&quot; /&gt; per hour [1, page 37]. This serves to explain the well-known &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=10%5E%7B-9%7D&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;10^{-9}&quot; title=&quot;10^{-9}&quot; class=&quot;latex&quot; /&gt; requirement, which is stated as follows: &amp;#8220;when using quantitative analyses. . . numerical probabilities. . . on the order of &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=10%5E%7B-9%7D&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;10^{-9}&quot; title=&quot;10^{-9}&quot; class=&quot;latex&quot; /&gt; per flight-hour. . . based on a flight of mean duration for the airplane type may be used. . . as aids to engineering judgment. . . to. . . help determine compliance&amp;#8221; (with the requirement for extremely improbable failure conditions) [2, paragraph 10.b].&lt;/p&gt;
&lt;p&gt;[1] E. Lloyd and W. Tye, Systematic Safety: Safety Assessment of Aircraft Systems. London, England: Civil Aviation Authority, 1982, reprinted 1992.&lt;/p&gt;
&lt;p&gt;[2] System Design and Analysis, Federal Aviation Administration, Jun. 21, 1988, advisory Circular 25.1309-1A.&lt;/p&gt;
&lt;p&gt;(By the way, it&amp;#8217;s worth reading the rest of the paper&amp;#8212;it&amp;#8217;s the first attempt I know of to formally connect the notions of (software) formal verification and reliability.)&lt;/p&gt;
&lt;p&gt;So there a probabilistic argument being made, but let&amp;#8217;s spell it out in a little more detail.  If there are 10 potential failures in 10 subsystems, then there are &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=10+%5Ctimes+10+%3D+100&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;10 \times 10 = 100&quot; title=&quot;10 \times 10 = 100&quot; class=&quot;latex&quot; /&gt; potential failures.  Thus, there are &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=2%5E%7B100%7D&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;2^{100}&quot; title=&quot;2^{100}&quot; class=&quot;latex&quot; /&gt; possible configurations of failure/non-failure in the subsystems.  Only one of these configurations is acceptable&amp;#8212;the one in which there are no faults.&lt;/p&gt;
&lt;p&gt;If the probability of failure is &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=x%2C&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;x,&quot; title=&quot;x,&quot; class=&quot;latex&quot; /&gt; then the probability of non-failure is &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=1+-+x.&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;1 - x.&quot; title=&quot;1 - x.&quot; class=&quot;latex&quot; /&gt;  So if the probability of failure for each subsystem is &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=10%5E%7B-9%7D%2C&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;10^{-9},&quot; title=&quot;10^{-9},&quot; class=&quot;latex&quot; /&gt; then the probability of being in the one non-failure configuration is&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;http://l.wordpress.com/latex.php?latex=%5Cdisplaystyle%281+-+10%5E%7B-9%7D%29%5E%7B100%7D&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;\displaystyle(1 - 10^{-9})^{100}&quot; title=&quot;\displaystyle(1 - 10^{-9})^{100}&quot; class=&quot;latex&quot; /&gt;&lt;/p&gt;
&lt;p&gt;We want that probability of non-failure to be greater than the required probability of non-failure, given the total number of flight hours.  Thus,&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;http://l.wordpress.com/latex.php?latex=%5Cdisplaystyle+%281+-+10%5E%7B-9%7D%29%5E%7B100%7D+%3E+1+-+10%5E%7B-7%7D&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;\displaystyle (1 - 10^{-9})^{100} &amp;gt; 1 - 10^{-7}&quot; title=&quot;\displaystyle (1 - 10^{-9})^{100} &amp;gt; 1 - 10^{-7}&quot; class=&quot;latex&quot; /&gt;&lt;/p&gt;
&lt;p&gt;which indeed holds:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;http://l.wordpress.com/latex.php?latex=%5Cdisplaystyle+%281+-+10%5E%7B-9%7D%29%5E%7B100%7D+-+%281+-+10%5E%7B-7%7D%29&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;\displaystyle (1 - 10^{-9})^{100} - (1 - 10^{-7})&quot; title=&quot;\displaystyle (1 - 10^{-9})^{100} - (1 - 10^{-7})&quot; class=&quot;latex&quot; /&gt;&lt;/p&gt;
&lt;p&gt;is around &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=4.95+%5Ctimes+10%5E%7B-15%7D.&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;4.95 \times 10^{-15}.&quot; title=&quot;4.95 \times 10^{-15}.&quot; class=&quot;latex&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Can we generalize the inequality?  The hint for how to do so is that the number of subsystems (&lt;img src=&quot;http://l.wordpress.com/latex.php?latex=100&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;100&quot; title=&quot;100&quot; class=&quot;latex&quot; /&gt;) is no more than the overall failure rate divided by the subsystem rate:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;http://l.wordpress.com/latex.php?latex=%5Cdisplaystyle+%5Cfrac%7B10%5E%7B-7%7D%7D%7B10%5E%7B-9%7D%7D&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;\displaystyle \frac{10^{-7}}{10^{-9}}&quot; title=&quot;\displaystyle \frac{10^{-7}}{10^{-9}}&quot; class=&quot;latex&quot; /&gt;&lt;/p&gt;
&lt;p&gt;This suggests the general form is something like&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;&lt;em&gt;Subsystem reliability inequality&lt;/em&gt;: &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=%5Cdisplaystyle+%281+-+C%5E%7B-n%7D%29%5E%7BC%5E%7Bn-m%7D%7D+%5Cgeq+1+-+C%5E%7B-m%7D&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;\displaystyle (1 - C^{-n})^{C^{n-m}} \geq 1 - C^{-m}&quot; title=&quot;\displaystyle (1 - C^{-n})^{C^{n-m}} \geq 1 - C^{-m}&quot; class=&quot;latex&quot; /&gt;&lt;/p&gt;
&lt;hr /&gt;where &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=C%2C&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;C,&quot; title=&quot;C,&quot; class=&quot;latex&quot; /&gt; &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=n%2C&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;n,&quot; title=&quot;n,&quot; class=&quot;latex&quot; /&gt; and &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=m&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;m&quot; title=&quot;m&quot; class=&quot;latex&quot; /&gt; are real numbers, &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=C+%5Cgeq+1%2C&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;C \geq 1,&quot; title=&quot;C \geq 1,&quot; class=&quot;latex&quot; /&gt; &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=n+%5Cgeq+0%2C&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;n \geq 0,&quot; title=&quot;n \geq 0,&quot; class=&quot;latex&quot; /&gt; and &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=n+%5Cgeq+m.&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;n \geq m.&quot; title=&quot;n \geq m.&quot; class=&quot;latex&quot; /&gt;&lt;/div&gt;
&lt;p&gt;Let&amp;#8217;s prove the inequality holds.  &lt;a href=&quot;http://www.gilith.com/&quot;&gt;Joe Hurd&lt;/a&gt; figured out the proof, sketched below (but I take responsibility for any mistakes in it&amp;#8217;s presentation).  For convenience, we&amp;#8217;ll prove the inequality holds specifically when &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=C+%3D+e%2C&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;C = e,&quot; title=&quot;C = e,&quot; class=&quot;latex&quot; /&gt; but the proof can be generalized.  &lt;/p&gt;
&lt;p&gt;First, if &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=n+%3D+0%2C&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;n = 0,&quot; title=&quot;n = 0,&quot; class=&quot;latex&quot; /&gt; the inequality holds immediately.  Next, we&amp;#8217;ll show that&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;http://l.wordpress.com/latex.php?latex=%5Cdisplaystyle+%281+-+e%5E%7B-n%7D%29%5E%7Be%5E%7Bn-m%7D%7D&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;\displaystyle (1 - e^{-n})^{e^{n-m}}&quot; title=&quot;\displaystyle (1 - e^{-n})^{e^{n-m}}&quot; class=&quot;latex&quot; /&gt;&lt;/p&gt;
&lt;p&gt;is monotonically non-decreasing with respect to &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=n&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;n&quot; title=&quot;n&quot; class=&quot;latex&quot; /&gt; by showing that the derivative of its logarithm is greater or equal to zero for all &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=n+%3E+0.&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;n &amp;gt; 0.&quot; title=&quot;n &amp;gt; 0.&quot; class=&quot;latex&quot; /&gt;  So the derivative of its logarithm is&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;http://l.wordpress.com/latex.php?latex=%5Cdisplaystyle+%5Cfrac%7Bd%7D%7Bdn%7D+%5C%3B+e%5E%7Bn-m%7D%5Cln%281-e%5E%7B-n%7D%29+%3D+e%5E%7Bn-m%7D%5Cln%281-e%5E%7B-n%7D%29%2B%5Cfrac%7Be%5E%7B-m%7D%7D%7B1-e%5E%7B-n%7D%7D&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;\displaystyle \frac{d}{dn} \; e^{n-m}\ln(1-e^{-n}) = e^{n-m}\ln(1-e^{-n})+\frac{e^{-m}}{1-e^{-n}}&quot; title=&quot;\displaystyle \frac{d}{dn} \; e^{n-m}\ln(1-e^{-n}) = e^{n-m}\ln(1-e^{-n})+\frac{e^{-m}}{1-e^{-n}}&quot; class=&quot;latex&quot; /&gt;&lt;/p&gt;
&lt;p&gt;We show&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;http://l.wordpress.com/latex.php?latex=%5Cdisplaystyle+e%5E%7Bn-m%7D%5Cln%281-e%7B-n%7D%29%2B%5Cfrac%7Be%5E%7B-m%7D%7D%7B1-e%5E%7B-n%7D%7D+%5Cgeq+0&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;\displaystyle e^{n-m}\ln(1-e{-n})+\frac{e^{-m}}{1-e^{-n}} \geq 0&quot; title=&quot;\displaystyle e^{n-m}\ln(1-e{-n})+\frac{e^{-m}}{1-e^{-n}} \geq 0&quot; class=&quot;latex&quot; /&gt;&lt;/p&gt;
&lt;p&gt;iff&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;http://l.wordpress.com/latex.php?latex=%5Cdisplaystyle+e%5E%7B-m%7D%5Cleft%28e%5E%7Bn%7D%5Cln%281-e%5E%7B-n%7D%29+%2B+%5Cfrac%7B1%7D%7B1-e%5E%7B-n%7D%7D%5Cright%29+%5Cgeq+0&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;\displaystyle e^{-m}\left(e^{n}\ln(1-e^{-n}) + \frac{1}{1-e^{-n}}\right) \geq 0&quot; title=&quot;\displaystyle e^{-m}\left(e^{n}\ln(1-e^{-n}) + \frac{1}{1-e^{-n}}\right) \geq 0&quot; class=&quot;latex&quot; /&gt;&lt;/p&gt;
&lt;p&gt;and since &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=e%5E%7B-m%7D+%5Cgeq+0%2C&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;e^{-m} \geq 0,&quot; title=&quot;e^{-m} \geq 0,&quot; class=&quot;latex&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;http://l.wordpress.com/latex.php?latex=%5Cdisplaystyle+e%5E%7Bn%7D%5Cln%281-e%5E%7B-n%7D%29+%2B+%5Cfrac%7B1%7D%7B1-e%5E%7B-n%7D%7D+%5Cgeq+0&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;\displaystyle e^{n}\ln(1-e^{-n}) + \frac{1}{1-e^{-n}} \geq 0&quot; title=&quot;\displaystyle e^{n}\ln(1-e^{-n}) + \frac{1}{1-e^{-n}} \geq 0&quot; class=&quot;latex&quot; /&gt;&lt;/p&gt;
&lt;p&gt;iff&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;http://l.wordpress.com/latex.php?latex=%5Cdisplaystyle+e%5E%7Bn%7D%5Cln%281-e%5E%7B-n%7D%29+%5Cgeq+-+%5Cfrac%7B1%7D%7B1-e%5E%7B-n%7D%7D&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;\displaystyle e^{n}\ln(1-e^{-n}) \geq - \frac{1}{1-e^{-n}}&quot; title=&quot;\displaystyle e^{n}\ln(1-e^{-n}) \geq - \frac{1}{1-e^{-n}}&quot; class=&quot;latex&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Let &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=x+%3D+e%5E%7B-n%7D&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;x = e^{-n}&quot; title=&quot;x = e^{-n}&quot; class=&quot;latex&quot; /&gt;, so the range of &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=x&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;x&quot; title=&quot;x&quot; class=&quot;latex&quot; /&gt; is &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=0+%3C+x+%3C+1.&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;0 &amp;lt; x &amp;lt; 1.&quot; title=&quot;0 &amp;lt; x &amp;lt; 1.&quot; class=&quot;latex&quot; /&gt;&lt;br /&gt;
&lt;img src=&quot;http://l.wordpress.com/latex.php?latex=%5Cdisplaystyle%5Cln%281-x%29+%5Cgeq+-+%5Cfrac%7Bx%7D%7B1-x%7D&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;\displaystyle\ln(1-x) \geq - \frac{x}{1-x}&quot; title=&quot;\displaystyle\ln(1-x) \geq - \frac{x}{1-x}&quot; class=&quot;latex&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Now we show that in the range of &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=x&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;x&quot; title=&quot;x&quot; class=&quot;latex&quot; /&gt;, the left-hand side is bounded below by the right-hand side of the inequality.&lt;br /&gt;
&lt;img src=&quot;http://l.wordpress.com/latex.php?latex=%5Cdisplaystyle+%5Clim_%7Bx+%5Cto+0%7D+%5C%3B+%5Cln%281-x%29+%3D+0&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;\displaystyle \lim_{x \to 0} \; \ln(1-x) = 0&quot; title=&quot;\displaystyle \lim_{x \to 0} \; \ln(1-x) = 0&quot; class=&quot;latex&quot; /&gt;&lt;/p&gt;
&lt;p&gt;and&lt;br /&gt;
&lt;img src=&quot;http://l.wordpress.com/latex.php?latex=%5Cdisplaystyle+-+%5Cfrac%7Bx%7D%7B1-x%7D+%3D+0&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;\displaystyle - \frac{x}{1-x} = 0&quot; title=&quot;\displaystyle - \frac{x}{1-x} = 0&quot; class=&quot;latex&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Now taking their derivatives&lt;br /&gt;
&lt;img src=&quot;http://l.wordpress.com/latex.php?latex=%5Cdisplaystyle+%5Cfrac%7Bd%7D%7Bdx%7D+%5C%3B+%5Cln%281-x%29+%3D+%5Cfrac%7B1%7D%7Bx-1%7D&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;\displaystyle \frac{d}{dx} \; \ln(1-x) = \frac{1}{x-1}&quot; title=&quot;\displaystyle \frac{d}{dx} \; \ln(1-x) = \frac{1}{x-1}&quot; class=&quot;latex&quot; /&gt;&lt;/p&gt;
&lt;p&gt;and&lt;br /&gt;
&lt;img src=&quot;http://l.wordpress.com/latex.php?latex=%5Cdisplaystyle+%5Cfrac%7Bd%7D%7Bdx%7D+%5C%3B+-+%5Cfrac%7Bx%7D%7B1-x%7D+%3D+-+%5Cfrac%7B1%7D%7B%28x-1%29%5E2%7D&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;\displaystyle \frac{d}{dx} \; - \frac{x}{1-x} = - \frac{1}{(x-1)^2}&quot; title=&quot;\displaystyle \frac{d}{dx} \; - \frac{x}{1-x} = - \frac{1}{(x-1)^2}&quot; class=&quot;latex&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Because &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=%5Cdisplaystyle+x-1+%5Cgeq+-+%28x-1%29%5E2&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;\displaystyle x-1 \geq - (x-1)^2&quot; title=&quot;\displaystyle x-1 \geq - (x-1)^2&quot; class=&quot;latex&quot; /&gt; in the range of &lt;img src=&quot;http://l.wordpress.com/latex.php?latex=x&amp;#038;bg=ffffff&amp;#038;fg=000000&amp;#038;s=0&quot; alt=&quot;x&quot; title=&quot;x&quot; class=&quot;latex&quot; /&gt;, our proof holds.&lt;/p&gt;
&lt;p&gt;The purpose of this post was to clarify the folklore of ultra-reliable systems.  The subsystem reliability inequality presented allows for easy generalization to other reliable systems.&lt;/p&gt;
&lt;p&gt;Thanks again for the help, Joe!&lt;span id=&quot;more-235&quot;&gt;&lt;/span&gt;&lt;!--more--&gt;&lt;!--more--&gt;&lt;!--more--&gt;&lt;!--more--&gt;&lt;/p&gt;
  &lt;a rel=&quot;nofollow&quot; href=&quot;http://feeds.wordpress.com/1.0/gocomments/leepike.wordpress.com/235/&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://feeds.wordpress.com/1.0/comments/leepike.wordpress.com/235/&quot; /&gt;&lt;/a&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;http://feeds.wordpress.com/1.0/godelicious/leepike.wordpress.com/235/&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://feeds.wordpress.com/1.0/delicious/leepike.wordpress.com/235/&quot; /&gt;&lt;/a&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;http://feeds.wordpress.com/1.0/gostumble/leepike.wordpress.com/235/&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://feeds.wordpress.com/1.0/stumble/leepike.wordpress.com/235/&quot; /&gt;&lt;/a&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;http://feeds.wordpress.com/1.0/godigg/leepike.wordpress.com/235/&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://feeds.wordpress.com/1.0/digg/leepike.wordpress.com/235/&quot; /&gt;&lt;/a&gt; &lt;a rel=&quot;nofollow&quot; href=&quot;http://feeds.wordpress.com/1.0/goreddit/leepike.wordpress.com/235/&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://feeds.wordpress.com/1.0/reddit/leepike.wordpress.com/235/&quot; /&gt;&lt;/a&gt; &lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://stats.wordpress.com/b.gif?host=leepike.wordpress.com&amp;amp;blog=7358124&amp;amp;post=235&amp;amp;subd=leepike&amp;amp;ref=&amp;amp;feed=1&quot; /&gt;</description>
	<pubDate>Thu, 04 Feb 2010 03:41:12 +0000</pubDate>
	<dc:creator>Lee Pike</dc:creator>
</item>
<item>
	<title>Holumbus: Hayoo! Webservice API</title>
	<guid isPermalink="false">http://holumbus.fh-wedel.de/blog/?p=33</guid>
	<link>http://holumbus.fh-wedel.de/blog/?p=33</link>
	<description>&lt;p&gt; We have just deployed a new version of Hayoo!, which now provides a &lt;a href=&quot;http://www.json.org&quot;&gt;JSON&lt;/a&gt;-based webservice API.&lt;/p&gt;
&lt;p&gt;Queries may be posted to Hayoo! via this URI:&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://holumbus.fh-wedel.de/hayoo/hayoo.json?query=YOUR_QUERY&quot;&gt;http://holumbus.fh-wedel.de/hayoo/hayoo.json?query=YOUR_QUERY&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Search results are returned as JSON encoded string. More details about the structure of the result can be found &lt;a href=&quot;http://holumbus.fh-wedel.de/hayoo/api.html&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;We would be very happy to see many (web-) applications to include Hayoo! search functionality. Your own creativity is the only limit ;)&lt;/p&gt;
&lt;p&gt;If you need help or come across any problems, just drop us a line.&lt;/p&gt;</description>
	<pubDate>Wed, 03 Feb 2010 21:22:33 +0000</pubDate>
	<dc:creator>tbh</dc:creator>
</item>
<item>
	<title>apfelmus: The Operational Monad Tutorial</title>
	<guid isPermalink="false">http://apfelmus.nfshost.com/articles/operational-monad.html</guid>
	<link>http://apfelmus.nfshost.com/articles/operational-monad.html</link>
	<description>&lt;div id=&quot;TOC&quot;&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#introduction&quot;&gt;Introduction&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#stack-machine---list-of-instructions&quot;&gt;Stack Machine - List of Instructions?&lt;/a&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#representation&quot;&gt;Representation&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#concatenation-and-thoughts-on-the-interface&quot;&gt;Concatenation and thoughts on the interface&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#interpreter&quot;&gt;Interpreter&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#oops&quot;&gt;Oops&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#stack-machine---monad&quot;&gt;Stack Machine - Monad&lt;/a&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#representation-1&quot;&gt;Representation&lt;/a&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#return-types&quot;&gt;Return types&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#binding-variables&quot;&gt;Binding variables&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#empty-program&quot;&gt;Empty program&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#the-fancy-list&quot;&gt;The fancy list&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#interpreter-1&quot;&gt;Interpreter&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#concatenation-and-interface&quot;&gt;Concatenation and interface&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#what-weve-done-so-far&quot;&gt;What we've done so far&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#multiple-interpreters&quot;&gt;Multiple Interpreters&lt;/a&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#random-numbers&quot;&gt;Random Numbers&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#monadic-parser-combinators&quot;&gt;Monadic Parser Combinators&lt;/a&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#primitives&quot;&gt;Primitives&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#a-first-implementation&quot;&gt;A first implementation&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#a-note-on-technique&quot;&gt;A note on technique&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#depth-first&quot;&gt;Depth-first&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#breadth-first&quot;&gt;Breadth-first&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#conclusion&quot;&gt;Conclusion&lt;/a&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#further-examples&quot;&gt;Further Examples&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#connection-with-the-continuation-monad&quot;&gt;Connection with the Continuation Monad&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#drawbacks&quot;&gt;Drawbacks&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#about-the-author&quot;&gt;About the author&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;
&lt;p&gt;&lt;em&gt;This article was first published in &lt;a href=&quot;http://themonadreader.wordpress.com/2010/01/26/issue-15/&quot;&gt;issue 15&lt;/a&gt; of &lt;a href=&quot;http://themonadreader.wordpress.com/&quot;&gt;The Monad.Reader&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;&lt;div id=&quot;introduction&quot;&gt;&lt;h1&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#TOC&quot;&gt;Introduction&lt;/a&gt;&lt;/h1&gt;&lt;p&gt;Another monad tutorial? Oh my god, why!? Fear not, this article is aimed at Haskellers who are already familiar with monads, though I have of course tried to keep the material as accessible as possible; the first two sections may serve as an initial introduction to monads and the monad laws for the brave.&lt;/p&gt;&lt;p&gt;In this tutorial, I would like to present monads from the viewpoint of &lt;em&gt;&lt;a href=&quot;http://www.cse.iitd.ernet.in/~sanjiva/opsem.ps&quot;&gt;operational semantics&lt;/a&gt;&lt;/em&gt; and how it makes designing and implementing new monads a piece of cake. Put differently, &lt;code&gt;s -&amp;gt; (a,s)&lt;/code&gt; is not the only way to implement the state monad and this tutorial aims to present a much more systematic way. I think it is still regrettably underused, hence this text.&lt;/p&gt;&lt;p&gt;The main idea is to view monads as a sequence of instructions to be executed by a machine, so that the task of implementing monads is equivalent to writing an interpreter. The introductory example will be a stack automaton, followed by a remark on a monad for random numbers. Then, to showcase the simplicity of this approach, we will implement backtracking parser combinators, culminating in a straightforward breadth-first implementation equivalent to &lt;a href=&quot;http://www.cs.chalmers.se/~koen/pubs/entry-jfp04-parser.html&quot; title=&quot;Koen Claessen. Parallel Parsing Processes&quot;&gt;Claessen's parallel parsing processes&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;For those in the know, I'm basically going to present the principles of &lt;a href=&quot;http://web.cecs.pdx.edu/~cklin/papers/unimo-143.pdf&quot; title=&quot;Chuan-kai Lin. Programming Monads Operationally with Unimo.&quot;&gt;Chuan-kai Lin's Unimo paper&lt;/a&gt;. The approach is neither new nor unfamiliar; for example, &lt;a href=&quot;http://citeseer.ist.psu.edu/hughes95design.html&quot; title=&quot;John Hughes. The Design of a Pretty-printing Library.&quot;&gt;John Hughes&lt;/a&gt; already used it to derive the state monad. But until I read Lin's paper, I did not understand how valuable it is when done systematically and in Haskell. &lt;a href=&quot;http://hackage.haskell.org/package/MonadPrompt&quot; title=&quot;Ryan Ingram's MonadPrompt package&quot;&gt;Ryan Ingram's &lt;code&gt;MonadPrompt&lt;/code&gt;&lt;/a&gt; package is another recent formulation.&lt;/p&gt;&lt;p&gt;To encourage reuse, I have also released a package &lt;a href=&quot;http://hackage.haskell.org/package/operational&quot; title=&quot;Heinrich Apfelmus' operational package&quot;&gt;&lt;code&gt;operational&lt;/code&gt;&lt;/a&gt; on &lt;a href=&quot;http://hackage.haskell.org&quot;&gt;hackage&lt;/a&gt; which collects the generic bits of these ideas in a small library. For convenient study, the &lt;a href=&quot;http://apfelmus.nfshost.com/articles/operational-monad/code.zip&quot; title=&quot;Accompanying source code for 'The Operational Monad Tutorial'&quot;&gt;source code&lt;/a&gt; from each section of this article is also available.&lt;/p&gt;&lt;/div&gt;&lt;div id=&quot;stack-machine---list-of-instructions&quot;&gt;&lt;h1&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#TOC&quot;&gt;Stack Machine - List of Instructions?&lt;/a&gt;&lt;/h1&gt;&lt;p&gt;Our introductory example will be a stack machine, i.e. an imperative mini-language featuring two instructions &lt;code&gt;push&lt;/code&gt; and &lt;code&gt;pop&lt;/code&gt; for pushing and popping values onto and from a stack.&lt;/p&gt;&lt;p&gt;In other words, I have imperative programs like the following in mind:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;push 5; push 42; pop;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Instructions are separated by semicolons. As shown in the following picture, this program first puts the number &lt;code&gt;5&lt;/code&gt; on the stack, then puts the number &lt;code&gt;42&lt;/code&gt; on top of the stack and proceeds to remove it again.&lt;/p&gt;&lt;img src=&quot;http://apfelmus.nfshost.com/articles/operational-monad/stack.png&quot; alt=&quot;Example stack program&quot; width=&quot;400&quot; /&gt;&lt;p&gt;How can we embed such programs into Haskell?&lt;/p&gt;&lt;div id=&quot;representation&quot;&gt;&lt;h2&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#TOC&quot;&gt;Representation&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;First we need some way of representing the program text, for instance as a list of instructions:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;type Program instr    = [instr]
type StackProgram     = Program StackInstruction
data StackInstruction = Push Int | Pop
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Our example is represented as&lt;/p&gt;&lt;pre&gt;&lt;code&gt;example = Push 5 : Push 42 : Pop : []
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;In a sense, the colon &lt;code&gt;(:)&lt;/code&gt; for building lists takes the role of the semicolon for sequencing instructions.&lt;/p&gt;&lt;/div&gt;&lt;div id=&quot;concatenation-and-thoughts-on-the-interface&quot;&gt;&lt;h2&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#TOC&quot;&gt;Concatenation and thoughts on the interface&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;Note that this representation gives us a very convenient tool for assembling bigger programs from smaller subprograms: list concatenation &lt;code&gt;(++)&lt;/code&gt;. For instance,&lt;/p&gt;&lt;pre&gt;&lt;code&gt;exampleTwice = example ++ example
    = Push 5 : Push 42 : Pop : Push 5 : Push 42 : Pop : []
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;is a program that executes &lt;code&gt;example&lt;/code&gt; twice. Together with the empty program&lt;/p&gt;&lt;pre&gt;&lt;code&gt;empty = []
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;concatenation obeys the following three well-known laws:&lt;/p&gt;&lt;pre&gt;&lt;code&gt; empty ++ is      =  is                  -- left unit
    is ++ empty   =  is                  -- right unit
(is ++ js) ++ ks  =  is ++ (js ++ ks)    -- associativity
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;which seem almost too evident to be worth mentioning. For example, it is customary to leave out the parenthesis in the last line altogether.&lt;/p&gt;&lt;p&gt;Once accustomed to the notion of programs and &lt;code&gt;(++)&lt;/code&gt; to combine them, the special case of single instructions and &lt;code&gt;(:)&lt;/code&gt; for sequencing them is unnecessary. The user of our language does not care that we deem &lt;code&gt;push&lt;/code&gt; and &lt;code&gt;pop&lt;/code&gt; to be primitive operations but not, for example, the program&lt;/p&gt;&lt;pre&gt;&lt;code&gt;replace a = Pop : Push a : []
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;which replaces the topmost stack element with &lt;code&gt;a&lt;/code&gt;; he is entirely content to be given two programs&lt;/p&gt;&lt;pre&gt;&lt;code&gt;push :: Int -&amp;gt; StackProgram
pop  :: StackProgram
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;and two general combinators for building new ones&lt;/p&gt;&lt;pre&gt;&lt;code&gt;empty :: StackProgram
(++)  :: StackProgram -&amp;gt; StackProgram -&amp;gt; StackProgram 
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;without any mention of the distinction between single instruction and compound program. Their difference is but an implementation detail.&lt;/p&gt;&lt;/div&gt;&lt;div id=&quot;interpreter&quot;&gt;&lt;h2&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#TOC&quot;&gt;Interpreter&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;Well, to be entirely content, the user also needs a way to run programs. In particular, we need to implement a function &lt;code&gt;interpret&lt;/code&gt; that maps the program text to its intended meaning, here a function that transforms a stack of integers.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;type Stack a = [a]

interpret :: StackProgram -&amp;gt; (Stack Int -&amp;gt; Stack Int)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The implementation follows the style of operational semantics: inspect the first instruction, change the stack accordingly, and recursively proceed with the remaining list of instructions &lt;code&gt;is&lt;/code&gt;:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;interpret (Push a : is) stack = interpret is (a : stack)
interpret (Pop    : is) stack = interpret is (tail stack)
interpret []            stack = stack
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div id=&quot;oops&quot;&gt;&lt;h2&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#TOC&quot;&gt;Oops&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;&amp;quot;All well and good, but why all the fuss with 'monads' then, when lists of instructions will do?&amp;quot; you may ask. Alas, the problem is of course that lists won't do! We forgot something very important: our programs are completely unable to inspect values from the stack.&lt;/p&gt;&lt;p&gt;For instance, how to write a program that pops the two topmost values and pushes their sum onto the stack? Clearly, we want something like&lt;/p&gt;&lt;pre&gt;&lt;code&gt;a &amp;lt;- pop;
b &amp;lt;- pop;
push (a+b);
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;where each &lt;code&gt;pop&lt;/code&gt; returns the element just removed and the arrow &lt;code&gt;&amp;lt;-&lt;/code&gt; binds it to a variable. But binding variables is simply impossible to express with our current representation of programs as lists of instructions.&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;&lt;div id=&quot;stack-machine---monad&quot;&gt;&lt;h1&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#TOC&quot;&gt;Stack Machine - Monad&lt;/a&gt;&lt;/h1&gt;&lt;p&gt;Well, if ordinary lists of instructions are not enough to represent programs that involve binding variables like&lt;/p&gt;&lt;pre&gt;&lt;code&gt;a &amp;lt;- pop; b &amp;lt;- pop; push (a+b);
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;then let's invent some fancy kind of list of instructions that will! The following presentation will be in close analogy to the structure of the previous section.&lt;/p&gt;&lt;div id=&quot;representation-1&quot;&gt;&lt;h2&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#TOC&quot;&gt;Representation&lt;/a&gt;&lt;/h2&gt;&lt;div id=&quot;return-types&quot;&gt;&lt;h3&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#TOC&quot;&gt;Return types&lt;/a&gt;&lt;/h3&gt;&lt;p&gt;First, if we want to interpret &lt;code&gt;pop&lt;/code&gt; as a function that returns something, we had better label it with the type of the value returned! Hence, instead of a plain type&lt;/p&gt;&lt;pre&gt;&lt;code&gt;Pop :: StackInstruction
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;we need an additional type argument&lt;/p&gt;&lt;pre&gt;&lt;code&gt;Pop :: StackInstruction Int
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;which indicates that the &lt;code&gt;Pop&lt;/code&gt; instruction somehow returns a value of type &lt;code&gt;Int&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;For simplicity, we attribute a return type to &lt;code&gt;push&lt;/code&gt; as well, even though it doesn't really return anything. This can modeled just fine with the unit type &lt;code&gt;()&lt;/code&gt;.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;Push 42 :: StackInstruction ()
Push    :: Int -&amp;gt; StackInstruction ()
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Putting both together, our type of instructions will become&lt;/p&gt;&lt;pre&gt;&lt;code&gt;data StackInstruction a where
    Pop  :: StackInstruction Int
    Push :: Int -&amp;gt; StackInstruction ()
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;If this syntax is alien to you: this is a &lt;a href=&quot;http://www.haskell.org/haskellwiki/GADT&quot;&gt;Generalized Algebraic Data Type&lt;/a&gt; (GADT) which allows us to define a data type by declaring the types of its constructors directly. As of Haskell 2010, GADTs are not yet part of the language standard, but they are supported by &lt;a href=&quot;http://www.haskell.org/ghc/&quot;&gt;GHC&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;Like instructions, we also have to annotate programs with their return type, so that the definition for &lt;code&gt;StackProgram&lt;/code&gt; becomes&lt;/p&gt;&lt;pre&gt;&lt;code&gt;data Program instr a where ...

type StackProgram a = Program StackInstruction a
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;As before, &lt;code&gt;instr&lt;/code&gt; is the type of instructions, whereas &lt;code&gt;a&lt;/code&gt; is the newly annotated return type.&lt;/p&gt;&lt;/div&gt;&lt;div id=&quot;binding-variables&quot;&gt;&lt;h3&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#TOC&quot;&gt;Binding variables&lt;/a&gt;&lt;/h3&gt;&lt;p&gt;How to represent the binding of variables? Lambda abstractions will do the trick; imagine the following:&lt;/p&gt;&lt;table class=&quot;noborder&quot;&gt;
&lt;tr&gt;&lt;td width=&quot;50%&quot;&gt;take a binding&lt;td&gt;&lt;code&gt;a &amp;lt;- pop; rest&lt;/code&gt;&lt;tr&gt;&lt;td&gt;turn the arrow to the right&lt;td&gt;&lt;code&gt;pop -&amp;gt; a; rest&lt;/code&gt;&lt;tr&gt;&lt;td&gt;and use a lambda expression to move it past the semicolon&lt;td&gt;&lt;code&gt;pop; \a -&amp;gt; rest&lt;/code&gt;&lt;/td&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/td&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/td&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;p&gt;Voila, the last step can be represented in Haskell, with a constructor named &lt;code&gt;Then&lt;/code&gt; taking the role of the semicolon:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;Pop `Then` \a -&amp;gt; rest 
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The idea is that &lt;code&gt;Then&lt;/code&gt; plugs the value returned by &lt;code&gt;pop&lt;/code&gt; into the variable &lt;code&gt;a&lt;/code&gt;. By the way, this is akin to how &lt;code&gt;let&lt;/code&gt; expressions can be expressed as lambda abstractions in Haskell:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;let a = foo in bar   &amp;lt;=&amp;gt;   (\a -&amp;gt; bar) foo
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Anyway, our motivating example can now be represented as&lt;/p&gt;&lt;pre&gt;&lt;code&gt;example2 = Pop `Then` (\a -&amp;gt; Pop `Then`
                        (\b -&amp;gt; Push (a+b) `Then` Return))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;where &lt;code&gt;Return&lt;/code&gt; represents the empty program which we will discuss in a moment. Remember that parentheses around the lambda expressions are optional, so we can also write&lt;/p&gt;&lt;pre&gt;&lt;code&gt;example2 = Pop `Then` \a -&amp;gt;
           Pop `Then` \b -&amp;gt;
           Push (a+b) `Then`
           Return
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;It is instructive to think about the type of &lt;code&gt;Then&lt;/code&gt;. It has to be&lt;/p&gt;&lt;pre&gt;&lt;code&gt;Then :: instr a -&amp;gt; (a -&amp;gt; Program instr b) -&amp;gt; Program instr b
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Except for the return type &lt;code&gt;a&lt;/code&gt; in &lt;code&gt;instr a&lt;/code&gt; and the lambda abstraction, this is entirely analogous to the &amp;quot;cons&amp;quot; operation &lt;code&gt;(:)&lt;/code&gt; for lists.&lt;/p&gt;&lt;/div&gt;&lt;div id=&quot;empty-program&quot;&gt;&lt;h3&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#TOC&quot;&gt;Empty program&lt;/a&gt;&lt;/h3&gt;&lt;p&gt;The empty program, corresponding to the empty list &lt;code&gt;[]&lt;/code&gt;, is best represented by a constructor&lt;/p&gt;&lt;pre&gt;&lt;code&gt;Return :: a -&amp;gt; Program instr a
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;that is not &amp;quot;entirely empty&amp;quot; but rather denotes a trivial instruction that just returns the given value &lt;code&gt;a&lt;/code&gt; (hence the name). This is very useful, since we can now choose return values freely. For instance,&lt;/p&gt;&lt;pre&gt;&lt;code&gt;example3 = Pop `Then` \a -&amp;gt; Pop `Then` \b -&amp;gt; Return (a*b)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;is a program that pops two values from the stack but whose return value is their product.&lt;/p&gt;&lt;/div&gt;&lt;div id=&quot;the-fancy-list&quot;&gt;&lt;h3&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#TOC&quot;&gt;The fancy list&lt;/a&gt;&lt;/h3&gt;&lt;p&gt;Taking everything together, we obtain a fancy list of instructions, once again a GADT:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;data Program instr a where
    Then   :: instr a -&amp;gt; (a -&amp;gt; Program instr b) -&amp;gt; Program instr b
    Return :: a -&amp;gt; Program instr a
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;And specialized to our stack machine language, we get&lt;/p&gt;&lt;pre&gt;&lt;code&gt;type StackProgram a = Program StackInstruction a
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div id=&quot;interpreter-1&quot;&gt;&lt;h2&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#TOC&quot;&gt;Interpreter&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;Before thinking thinking further about our new representation, let's first write the interpreter to see the stack machine in action. This time, however, we are not interested in the final stack, only in the value returned.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;interpret :: StackProgram a -&amp;gt; (Stack Int -&amp;gt; a)
interpret (Push a `Then` is) stack     = interpret (is ()) (a:stack)
interpret (Pop    `Then` is) (b:stack) = interpret (is b ) stack
interpret (Return c)         stack     = c
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The implementation is like the previous one, except that now, we also have to pass the return values like &lt;code&gt;()&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; to the remaining instructions &lt;code&gt;is&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;Our example program executes as expected:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;GHCi&amp;gt; interpret example3 [7,11]
77
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div id=&quot;concatenation-and-interface&quot;&gt;&lt;h2&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#TOC&quot;&gt;Concatenation and interface&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;Just as with lists, we can build large programs by concatenating smaller subprograms. And as before, we don't want the user to bother with the distinction between single instruction and compound program.&lt;/p&gt;&lt;p&gt;We begin with the latter: the function&lt;/p&gt;&lt;pre&gt;&lt;code&gt;singleton :: instr a -&amp;gt; Program instr a
singleton i = i `Then` Return
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;takes the role of &lt;code&gt;\x -&amp;gt; [x]&lt;/code&gt; and helps us blur the line between program and instructions:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;pop  :: StackProgram Int
push :: Int -&amp;gt; StackProgram ()

pop  = singleton Pop
push = singleton . Push
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Now, we define the concatenation operator (often dubbed &amp;quot;bind&amp;quot;) that glues two programs together:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;(&amp;gt;&amp;gt;=) :: Program i a -&amp;gt; (a -&amp;gt; Program i b) -&amp;gt; Program i b
(Return a)    &amp;gt;&amp;gt;= js  = js a
(i `Then` is) &amp;gt;&amp;gt;= js  = i `Then` (\a -&amp;gt; is a &amp;gt;&amp;gt;= js)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Apart from the new symbol &lt;code&gt;(&amp;gt;&amp;gt;=)&lt;/code&gt; and the new type signature, the purpose and implementation is entirely analogous to &lt;code&gt;(++)&lt;/code&gt;. And as before, together with the empty program,&lt;/p&gt;&lt;pre&gt;&lt;code&gt;return = Return
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;it obeys three evident laws&lt;/p&gt;&lt;pre&gt;&lt;code&gt;return a &amp;gt;&amp;gt;= is     = is a                        -- left unit
is &amp;gt;&amp;gt;= return       = is                          -- right unit
(is &amp;gt;&amp;gt;= js) &amp;gt;&amp;gt;= ks  = is &amp;gt;&amp;gt;= (\a -&amp;gt; js a &amp;gt;&amp;gt;= ks)  -- associativity
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;also called the &lt;em&gt;monad laws&lt;/em&gt;. Since we need to pass return values, the laws are slightly different from the concatenation laws for ordinary lists, but their essence is the same.&lt;/p&gt;&lt;p&gt;The reason that these equations are called the &amp;quot;monad laws&amp;quot; is that any data type supporting two such operations and obeying the three laws is called a &lt;em&gt;monad&lt;/em&gt;. In Haskell, monads are assembled in the type class &lt;code&gt;Monad&lt;/code&gt;, so we'd have to make an instance&lt;/p&gt;&lt;pre&gt;&lt;code&gt;instance Monad (Program instr) where
    (&amp;gt;&amp;gt;=)  = ...
    return = ...
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This is similar to lists which are said to constitute a &lt;a href=&quot;http://haskell.org/haskellwiki/Monoid&quot;&gt;&lt;em&gt;monoid&lt;/em&gt;&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;We conclude the first part of this tutorial by remarking that the &lt;code&gt;(&amp;gt;&amp;gt;=)&lt;/code&gt; operator is the basis for many other functions that build big programs from small ones; these can be found in the &lt;code&gt;Control.Monad&lt;/code&gt; module and are described &lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml&quot;&gt;elsewhere&lt;/a&gt;.&lt;/p&gt;&lt;hr /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div id=&quot;what-weve-done-so-far&quot;&gt;&lt;h1&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#TOC&quot;&gt;What we've done so far&lt;/a&gt;&lt;/h1&gt;&lt;p&gt;Those familiar with the state monad will recognize that the whole stack machine was just&lt;/p&gt;&lt;pre&gt;&lt;code&gt;State (Stack Int)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;in disguise. But surprisingly, we haven't used the pattern &lt;code&gt;s -&amp;gt; (a,s)&lt;/code&gt; for threading state anywhere! Instead, we were able to implement the equivalent of&lt;/p&gt;&lt;pre&gt;&lt;code&gt;evalState :: State s -&amp;gt; (s -&amp;gt; a)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;directly, even though the type &lt;code&gt;s -&amp;gt; a&lt;/code&gt; by itself is too &amp;quot;weak&amp;quot; to serve as an implementation of the state monad.&lt;/p&gt;&lt;p&gt;This is a very general phenomenon and it is of course the main benefit of the operational viewpoint and the new &lt;code&gt;Program instr a&lt;/code&gt; type. No matter what we choose as interpreter function or instruction set, the monad laws for &lt;code&gt;(&amp;gt;&amp;gt;=)&lt;/code&gt; and &lt;code&gt;return&lt;/code&gt; will always hold, for they are entirely independent of these choices. This makes it much easier to define and implement new monads and the remainder of this article aims to give a taste of its power.&lt;/p&gt;&lt;/div&gt;&lt;div id=&quot;multiple-interpreters&quot;&gt;&lt;h1&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#TOC&quot;&gt;Multiple Interpreters&lt;/a&gt;&lt;/h1&gt;&lt;p&gt;A first advantage of the operational approach is that it allows us to equip one and the same monad with multiple interpreters. We'll demonstrate this flexibility with an example monad &lt;code&gt;Random&lt;/code&gt; that expresses randomness and probability distributions.&lt;/p&gt;&lt;p&gt;The ability to write multiple interpreters is also very useful for implementing games, specifically to account for both human and computer opponents as well as replaying a game from a script. This is what prompted Ryan Ingram to write his &lt;a href=&quot;http://hackage.haskell.org/package/MonadPrompt&quot; title=&quot;Ryan Ingram's MonadPrompt package&quot;&gt;&lt;code&gt;MonadPrompt&lt;/code&gt; package&lt;/a&gt;.&lt;/p&gt;&lt;div id=&quot;random-numbers&quot;&gt;&lt;h2&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#TOC&quot;&gt;Random Numbers&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;At the heart of random computations is a type &lt;code&gt;Random a&lt;/code&gt; which denotes &lt;em&gt;random variables&lt;/em&gt; taking values in &lt;code&gt;a&lt;/code&gt;. Traditionally, the type &lt;code&gt;a&lt;/code&gt; would be a numeric type like &lt;code&gt;Int&lt;/code&gt;, so that &lt;code&gt;Random Int&lt;/code&gt; denotes &amp;quot;random numbers&amp;quot;. But for the Haskell programmer, it is only natural to generalize it to any type &lt;code&gt;a&lt;/code&gt;. This generalization is also very useful, because it reveals hidden structure: it turns out that &lt;code&gt;Random&lt;/code&gt; is actually a monad.&lt;/p&gt;&lt;p&gt;There are two ways to implement this monad: one way is to interpret random variables as a recipe for creating pseudo-random values from a seed, which is commonly written&lt;/p&gt;&lt;pre&gt;&lt;code&gt;type Random a = StdGen -&amp;gt; (a,StdGen)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The other is to view them as a probability distribution, as for example expressed in &lt;a href=&quot;http://web.engr.oregonstate.edu/~erwig/pfp/&quot; title=&quot;Martin Erwig, Steve Kollmansberger. Probabilistic Functional Programming.&quot;&gt;probabilistic functional programming&lt;/a&gt; as&lt;/p&gt;&lt;pre&gt;&lt;code&gt;type Probability = Double
type Random a    = [(a,Probability)]
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Traditionally, we'd have to choose between one way or the other depending on the application. But with the operational approach, we can have our cake and eat it, too! The two ways of implementing random variables can be delegated to two different interpreter functions for one and the same monad &lt;code&gt;Random&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;For demonstration purposes, we represent &lt;code&gt;Random&lt;/code&gt; as a language with just one instruction &lt;code&gt;uniform&lt;/code&gt; that randomly selects an element from a list with uniform probability&lt;/p&gt;&lt;pre&gt;&lt;code&gt;type Random a = Program RandomInstruction a

data RandomInstruction a where
    Uniform :: [a] -&amp;gt; RandomInstruction a

uniform :: [a] -&amp;gt; Random a
uniform = singleton . Uniform
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;For example, a roll of a die is modeled as&lt;/p&gt;&lt;pre&gt;&lt;code&gt;die :: Random Int
die = uniform [1..6]
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;and the sum of two dice rolls is&lt;/p&gt;&lt;pre&gt;&lt;code&gt;sum2Dies = die &amp;gt;&amp;gt;= \a -&amp;gt; die &amp;gt;&amp;gt;= \b -&amp;gt; return (a+b)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Now, the two different interpretations are: sampling a random variable by generating pseudo-random values&lt;/p&gt;&lt;pre&gt;&lt;code&gt;sample :: Random a -&amp;gt; StdGen -&amp;gt; (a,StdGen)
sample (Return a)             gen = (a,gen)
sample (Uniform xs `Then` is) gen = sample (is $ xs !! k) gen'
    where (k,gen') = System.Random.randomR (0,length xs-1) gen
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;and calculating its probability distribution&lt;/p&gt;&lt;pre&gt;&lt;code&gt;distribution :: Random a -&amp;gt; [(a,Probability)]
distribution (Return a)             = [(a,1)]
distribution (Uniform xs `Then` is) =
    [(a,p/n) | x &amp;lt;- xs, (a,p) &amp;lt;- distribution (is x)]
    where n = fromIntegral (length xs)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Truth to be told, the &lt;code&gt;distribution&lt;/code&gt; interpreter has a flaw, namely that it never tallies the probabilities of equal outcomes. That's because this would require an additional &lt;code&gt;Eq a&lt;/code&gt; constraint on the types of &lt;code&gt;return&lt;/code&gt; and &lt;code&gt;(&amp;gt;&amp;gt;=)&lt;/code&gt;, which is unfortunately not possible with the current &lt;code&gt;Monad&lt;/code&gt; type class. A workaround for this known limitation can be found in the &lt;code&gt;norm&lt;/code&gt; function from the &lt;a href=&quot;http://web.engr.oregonstate.edu/~erwig/pfp/&quot; title=&quot;Martin Erwig, Steve Kollmansberger. Probabilistic Functional Programming.&quot;&gt;paper&lt;/a&gt; on probabilistic functional programming.&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;&lt;div id=&quot;monadic-parser-combinators&quot;&gt;&lt;h1&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#TOC&quot;&gt;Monadic Parser Combinators&lt;/a&gt;&lt;/h1&gt;&lt;p&gt;Now, it is time to demonstrate that the operational viewpoint also makes the implementation of otherwise advanced monads a piece of cake. Our example will be &lt;a href=&quot;http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.2.4159&quot; title=&quot;Graham Hutton, Erik Meijer. Monadic Parser Combinators.&quot;&gt;monadic parser combinators&lt;/a&gt; and for the remainder of this article, I will assume that you are somewhat familiar with them already. The goal will be to derive an implementation of &lt;a href=&quot;http://www.cs.chalmers.se/~koen/pubs/entry-jfp04-parser.html&quot; title=&quot;Koen Claessen. Parallel Parsing Processes&quot;&gt;Koen Claessen's ideas&lt;/a&gt; from scratch.&lt;/p&gt;&lt;div id=&quot;primitives&quot;&gt;&lt;h2&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#TOC&quot;&gt;Primitives&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;At their core, monadic parser combinators are a monad &lt;code&gt;Parser&lt;/code&gt; with just three primitives:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;symbol :: Parser Char
mzero  :: Parser a
mplus  :: Parser a -&amp;gt; Parser a -&amp;gt; Parser a
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;which represent&lt;/p&gt;&lt;ul&gt;&lt;li&gt;a parser that reads the next symbol from the input stream&lt;/li&gt;&lt;li&gt;a parser that never succeeds&lt;/li&gt;&lt;li&gt;a combinator that runs two parsers in parallel&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;respectively. (The last two operations define the &lt;code&gt;MonadPlus&lt;/code&gt; type class.) Furthermore, we need an interpreter, i.e. a function&lt;/p&gt;&lt;pre&gt;&lt;code&gt;interpret :: Parser a -&amp;gt; (String -&amp;gt; [a])
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;that runs the parser on the string and returns all successful parses.&lt;/p&gt;&lt;p&gt;The three primitives are enough to express virtually any parsing problem; here is an example of a parser &lt;code&gt;number&lt;/code&gt; that recognizes integers:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;satisfies p = symbol &amp;gt;&amp;gt;= \c -&amp;gt; if p c then return c else mzero 
many  p     = return [] `mplus` many1 p
many1 p     = liftM2 (:) p (many p) 
digit       = satisfies isDigit &amp;gt;&amp;gt;= \c -&amp;gt; return (ord c - ord '0')
number      = many1 digit &amp;gt;&amp;gt;= return . foldl (\x d -&amp;gt; 10*x + d) 0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div id=&quot;a-first-implementation&quot;&gt;&lt;h2&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#TOC&quot;&gt;A first implementation&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;The instruction set for our parser language will of course consist of these three primitive operations:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;data ParserInstruction a where
    Symbol :: ParserInstruction Char
    MZero  :: ParserInstruction a
    MPlus  :: Parser a -&amp;gt; Parser a -&amp;gt; ParserInstruction a

type Parser a = Program ParserInstruction a
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;A straightforward implementation of &lt;code&gt;interpret&lt;/code&gt; looks like this:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;interpret :: Parser a -&amp;gt; String -&amp;gt; [a]
interpret (Return a)            s = if null s then [a] else []
interpret (Symbol    `Then` is) s = case s of
    c:cs -&amp;gt; interpret (is c) cs
    []   -&amp;gt; []
interpret (MZero     `Then` is) s = []
interpret (MPlus p q `Then` is) s =
    interpret (p &amp;gt;&amp;gt;= is) s ++ interpret (q &amp;gt;&amp;gt;= is) s
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;For each instruction, we specify the intended effects, often calling &lt;code&gt;interpret&lt;/code&gt; recursively on the remaining program &lt;code&gt;is&lt;/code&gt;. In prose, the four cases are&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;code&gt;Return&lt;/code&gt; at the end of a program will return a result if the input was parsed completely.&lt;/li&gt;&lt;li&gt;&lt;code&gt;Symbol&lt;/code&gt; reads a single character from the input stream if available and fails otherwise.&lt;/li&gt;&lt;li&gt;&lt;code&gt;MZero&lt;/code&gt; returns an empty result immediately.&lt;/li&gt;&lt;li&gt;&lt;code&gt;MPlus&lt;/code&gt; runs two parsers in parallel and collects their results.&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div id=&quot;a-note-on-technique&quot;&gt;&lt;h2&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#TOC&quot;&gt;A note on technique&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;The cases for &lt;code&gt;MZero&lt;/code&gt; and &lt;code&gt;MPlus&lt;/code&gt; are a bit roundabout; the equations&lt;/p&gt;&lt;pre&gt;&lt;code&gt;interpret mzero       = \s -&amp;gt; []
interpret (mplus p q) = \s -&amp;gt; interpret p s ++ interpret q s
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;express our intention more plainly. Of course, these two equations do not constitute valid Haskell code for we may not pattern match on &lt;code&gt;mzero&lt;/code&gt; or &lt;code&gt;mplus&lt;/code&gt; directly. The only thing we may pattern match on is a constructor, for example like this&lt;/p&gt;&lt;pre&gt;&lt;code&gt;interpret (Mplus p q `Then` is) = ...
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;But even though our final Haskell code will have this form, this does not mean that jotting down the left hand side and thinking hard about the &lt;code&gt;...&lt;/code&gt; is the best way to write Haskell code. No, we should rather use the full power of purely functional programming and use a more &lt;em&gt;calculational&lt;/em&gt; approach, deriving the pattern matches from more evident equations like the ones above.&lt;/p&gt;&lt;p&gt;In this case, we can combine the two equations with the &lt;code&gt;MonadPlus&lt;/code&gt; laws&lt;/p&gt;&lt;pre&gt;&lt;code&gt;    mzero &amp;gt;&amp;gt;= m = mzero
mplus p q &amp;gt;&amp;gt;= m = mplus (p &amp;gt;&amp;gt;= m) (q &amp;gt;&amp;gt;= m)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;which specify how &lt;code&gt;mzero&lt;/code&gt; and &lt;code&gt;mplus&lt;/code&gt; interact with &lt;code&gt;(&amp;gt;&amp;gt;=)&lt;/code&gt;, to derive the desired pattern match&lt;/p&gt;&lt;pre&gt;&lt;code&gt;  interpret (Mplus p q `Then` is)
=   { definition of concatenation and mplus }
  interpret (mplus p q &amp;gt;&amp;gt;= is)
=   { MonadPlus law }
  interpret (mplus (p &amp;gt;&amp;gt;= is) (q &amp;gt;&amp;gt;= is))
=   { intended meaning }
  \s -&amp;gt; interpret (p &amp;gt;&amp;gt;= is) s ++ interpret (q &amp;gt;&amp;gt;= is) s
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Now, in light of the first step of this derivation, I even suggest to forget about constructors entirely and instead regard&lt;/p&gt;&lt;pre&gt;&lt;code&gt;interpret (mplus p q &amp;gt;&amp;gt;= is) = ...
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;as &amp;quot;valid&amp;quot; Haskell code; after all, it is straightforwardly converted to a valid pattern match. In other words, it is once again beneficial to not distinguish between single instructions and compound programs, at least in notation.&lt;/p&gt;&lt;/div&gt;&lt;div id=&quot;depth-first&quot;&gt;&lt;h2&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#TOC&quot;&gt;Depth-first&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;Unfortunately, our first implementation has a potential space leak, namely in the case&lt;/p&gt;&lt;pre&gt;&lt;code&gt;interpret (MPlus p q `Then` is) s =
    interpret (p &amp;gt;&amp;gt;= is) s ++ interpret (q &amp;gt;&amp;gt;= is) s
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The string &lt;code&gt;s&lt;/code&gt; is shared by the recursive calls and has to be held in memory for a long time.&lt;/p&gt;&lt;p&gt;In particular, the implementation will try to parse &lt;code&gt;s&lt;/code&gt; with the parser &lt;code&gt;p &amp;gt;&amp;gt;= is&lt;/code&gt; first, and then backtrack to the beginning of &lt;code&gt;s&lt;/code&gt; to parse it again with the second alternative &lt;code&gt;q &amp;gt;&amp;gt;= is&lt;/code&gt;. That's why this is called a &lt;em&gt;depth-first&lt;/em&gt; or &lt;em&gt;backtracking&lt;/em&gt; implementation. The string &lt;code&gt;s&lt;/code&gt; has to be held in memory as long the second parser has not started yet.&lt;/p&gt;&lt;/div&gt;&lt;div id=&quot;breadth-first&quot;&gt;&lt;h2&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#TOC&quot;&gt;Breadth-first&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;To ameliorate the space leak, we would like to create a &lt;em&gt;breadth-first&lt;/em&gt; implementation, one which does not try alternative parsers in sequence, but rather keeps a collection of all possible alternatives and advances them at once.&lt;/p&gt;&lt;p&gt;How to make this precise? The key idea is the following equation:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;  (symbol &amp;gt;&amp;gt;= is) `mplus` (symbol &amp;gt;&amp;gt;= js)
=  symbol &amp;gt;&amp;gt;= (\c -&amp;gt; is c `mplus` js c)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;When the parsers on both sides of &lt;code&gt;mplus&lt;/code&gt; are waiting for the next input symbol, we can group them together and make sure that the next symbol will be fetched only once from the input stream.&lt;/p&gt;&lt;p&gt;Clearly, this equation readily extends to more than two parsers, like for example&lt;/p&gt;&lt;pre&gt;&lt;code&gt;  (symbol &amp;gt;&amp;gt;= is) `mplus` (symbol &amp;gt;&amp;gt;= js) `mplus` (symbol &amp;gt;&amp;gt;= ks)
=  symbol &amp;gt;&amp;gt;= (\c -&amp;gt; is c `mplus` js c `mplus` ks c)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;and so on.&lt;/p&gt;&lt;p&gt;We want to use this equation as a function definition, mapping the left hand side to the right hand side. Of course, we can't do so directly because the left hand side is not one of the four patterns we can match upon. But thanks to the &lt;code&gt;MonadPlus&lt;/code&gt; laws, what we can do is to rewrite any parser into this form, namely with a function&lt;/p&gt;&lt;pre&gt;&lt;code&gt;expand :: Parser a -&amp;gt; [Parser a]
expand (MPlus p q `Then` is) = expand (p &amp;gt;&amp;gt;= is) ++
                               expand (q &amp;gt;&amp;gt;= is)
expand (MZero     `Then` is) = []
expand x                     = [x]
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The idea is that &lt;code&gt;expand&lt;/code&gt; fulfills&lt;/p&gt;&lt;pre&gt;&lt;code&gt;foldr mplus mzero . expand = id
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;and thus turns a parser into a list of summands which we now can pattern match upon. In other words, this function expands parsers matching &lt;code&gt;mzero &amp;gt;&amp;gt;= is&lt;/code&gt; and &lt;code&gt;mplus p q &amp;gt;&amp;gt;= is&lt;/code&gt; until only summands of the form &lt;code&gt;symbol &amp;gt;&amp;gt;= is&lt;/code&gt; and &lt;code&gt;return a&lt;/code&gt; remain.&lt;/p&gt;&lt;p&gt;With the parser expressed as a big &amp;quot;sum&amp;quot;, we can now apply our key idea and group all summands of the form &lt;code&gt;symbol &amp;gt;&amp;gt;= is&lt;/code&gt;; and we also have to take care of the other summands of the form &lt;code&gt;return a&lt;/code&gt;. The following definition will do the right thing:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;interpret :: Parser a -&amp;gt; String -&amp;gt; [a]
interpret p = interpret' (expand p)
    where
    interpret' :: [Parser a] -&amp;gt; String -&amp;gt; [a]
    interpret' ps []     = [a | Return a &amp;lt;- ps]
    interpret' ps (c:cs) = interpret'
        [p | (Symbol `Then` is) &amp;lt;- ps, p &amp;lt;- expand (is c)] cs
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Namely, how to handle each of the summands depends on the input stream:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;If there are still input symbols to be consumed, then only the summands of the form &lt;code&gt;symbol &amp;gt;&amp;gt;= is&lt;/code&gt; will proceed, the other parsers have ended prematurely.&lt;/li&gt;&lt;li&gt;If the input stream is empty, then only the parsers of the form &lt;code&gt;return x&lt;/code&gt; have parsed the input correctly, and their results are to be returned.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;That's it, this is our breadth-first interpreter, obtained by using laws and equations to rewrite instruction lists. It is equivalent to &lt;a href=&quot;http://www.cs.chalmers.se/~koen/pubs/entry-jfp04-parser.html&quot; title=&quot;Koen Claessen. Parallel Parsing Processes&quot;&gt;Koen Claessen's implementation&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;As an amusing last remark, I would like to mention that our calculations can be visualized as high school algebra if we ignore that &lt;code&gt;(&amp;gt;&amp;gt;=)&lt;/code&gt; has to pass around variables, as shown in the following table:&lt;/p&gt;&lt;table&gt;&lt;tr class=&quot;header&quot;&gt;&lt;th align=&quot;left&quot;&gt;Term&lt;/th&gt;&lt;th align=&quot;left&quot;&gt;Mathematical operation&lt;/th&gt;&lt;/tr&gt;&lt;tr class=&quot;odd&quot;&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;return&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;span class=&quot;math&quot;&gt;1&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr class=&quot;even&quot;&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;(&amp;gt;&amp;gt;=)&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;span class=&quot;math&quot;&gt; × &lt;/span&gt; multiplication&lt;/td&gt;&lt;/tr&gt;&lt;tr class=&quot;odd&quot;&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;mzero&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;span class=&quot;math&quot;&gt;0&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr class=&quot;even&quot;&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;mplus&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;span class=&quot;math&quot;&gt;+&lt;/span&gt; addition&lt;/td&gt;&lt;/tr&gt;&lt;tr class=&quot;odd&quot;&gt;&lt;td align=&quot;left&quot;&gt;&lt;code&gt;symbol&lt;/code&gt;&lt;/td&gt;&lt;td align=&quot;left&quot;&gt;&lt;span class=&quot;math&quot;&gt;&lt;em&gt;x&lt;/em&gt;&lt;/span&gt; indeterminate&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;p&gt;For example, our key idea corresponds to the distributive law&lt;/p&gt;&lt;p&gt;&lt;span class=&quot;math&quot;&gt;&lt;em&gt;x&lt;/em&gt; × &lt;em&gt;a&lt;/em&gt;+&lt;em&gt;x&lt;/em&gt; × &lt;em&gt;b&lt;/em&gt;=&lt;em&gt;x&lt;/em&gt; × (&lt;em&gt;a&lt;/em&gt;+&lt;em&gt;b&lt;/em&gt;)&lt;/span&gt;&lt;/p&gt;&lt;p&gt;and the monad and &lt;code&gt;MonadPlus&lt;/code&gt; laws have well-known counterparts in algebra as well.&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;&lt;div id=&quot;conclusion&quot;&gt;&lt;h1&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#TOC&quot;&gt;Conclusion&lt;/a&gt;&lt;/h1&gt;&lt;div id=&quot;further-examples&quot;&gt;&lt;h2&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#TOC&quot;&gt;Further Examples&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;I hope I have managed to convincingly demonstrate the virtues of the operational viewpoint with my choice of examples.&lt;/p&gt;&lt;p&gt;There are many other advanced monads whose implementations also become clearer when approached this way, such as the &lt;a href=&quot;http://www.haskell.org/haskellwiki/ListT_done_right&quot;&gt;list monad transformer&lt;/a&gt; (where the naive &lt;code&gt;m [a]&lt;/code&gt; is known not to work), Oleg Kiselyov's &lt;a href=&quot;http://okmij.org/ftp/papers/LogicT.pdf&quot;&gt;&lt;code&gt;LogicT&lt;/code&gt;&lt;/a&gt;, Koen Claessen's &lt;a href=&quot;http://www.cs.chalmers.se/~koen/pubs/entry-jfp99-monad.html&quot;&gt;poor man's concurrency monad&lt;/a&gt;, as well coroutines like Peter Thiemann's ingenious &lt;a href=&quot;http://www.informatik.uni-freiburg.de/~thiemann/WASH/draft.pdf&quot;&gt;WASH&lt;/a&gt; which includes a monad for tracking session state in a web server.&lt;/p&gt;&lt;p&gt;The &lt;a href=&quot;http://hackage.haskell.org/package/operational&quot; title=&quot;Heinrich Apfelmus' operational package&quot;&gt;&lt;code&gt;operational&lt;/code&gt; package&lt;/a&gt; includes a few of these examples.&lt;/p&gt;&lt;/div&gt;&lt;div id=&quot;connection-with-the-continuation-monad&quot;&gt;&lt;h2&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#TOC&quot;&gt;Connection with the Continuation Monad&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;Traditionally, the &lt;em&gt;continuation monad transformer&lt;/em&gt;&lt;/p&gt;&lt;pre&gt;&lt;code&gt;data Cont m a = Cont { runCont :: forall b. (a -&amp;gt; m b) -&amp;gt; m b }
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;has been used to implement these advanced monads. This is no accident; both approaches are capable of implementing any monad. In fact, they are almost the same thing: the continuation monad is the &lt;a href=&quot;http://www.brics.dk/RS/07/7/BRICS-RS-07-7.pdf&quot; title=&quot;Oliver Danvy, Kevin Millikin. Refunctionalization at work.&quot;&gt;refunctionalization&lt;/a&gt; of instructions as functions&lt;/p&gt;&lt;pre&gt;&lt;code&gt;\k -&amp;gt; interpret (Instruction `Then` k)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;But alas, I think that this unfortunate melange of instruction, interpreter and continuation does not explain or clarify what is going on; it is the algebraic data type &lt;code&gt;Program&lt;/code&gt; that offers a clear notion of what a monad is and what it means to implement one. Hence, in my opinion, the algebraic data type should be the preferred way of presenting new monads and also of implementing them, at least before program optimizations.&lt;/p&gt;&lt;p&gt;Actually, &lt;code&gt;Program&lt;/code&gt; is not a plain algebraic data type, it is a &lt;em&gt;generalized&lt;/em&gt; algebraic data type. It seems to me that this is also the reason why the continuation monad has found more use, despite being conceptually more difficult: GADTs simply weren't available in Haskell. I believe that the &lt;code&gt;Program&lt;/code&gt; type is a strong argument to include GADTs into a future Haskell standard.&lt;/p&gt;&lt;/div&gt;&lt;div id=&quot;drawbacks&quot;&gt;&lt;h2&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#TOC&quot;&gt;Drawbacks&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;Compared to specialized implementations, like for example &lt;code&gt;s -&amp;gt; (a,s)&lt;/code&gt; for the state monad, the operational approach is not entirely without drawbacks.&lt;/p&gt;&lt;p&gt;First, the given implementation of &lt;code&gt;(&amp;gt;&amp;gt;=)&lt;/code&gt; has the same quadratic running time problem as &lt;code&gt;(++)&lt;/code&gt; when used in a left-associative fashion. Fortunately, this can be ameliorated with a different (fancy) list data type; the &lt;a href=&quot;http://hackage.haskell.org/package/operational&quot; title=&quot;Heinrich Apfelmus' operational package&quot;&gt;&lt;code&gt;operational&lt;/code&gt; library&lt;/a&gt; implements one.&lt;/p&gt;&lt;p&gt;Second, and this cannot be ameliorated, we lose laziness. The state monad represented as &lt;code&gt;s -&amp;gt; (a,s)&lt;/code&gt; can cope with some infinite programs like&lt;/p&gt;&lt;pre&gt;&lt;code&gt;evalState (sequence . repeat . State $ \s -&amp;gt; (s,s+1)) 0
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;whereas the list of instructions approach has no hope of ever handling that, since only the very last &lt;code&gt;Return&lt;/code&gt; instruction can return values.&lt;/p&gt;&lt;p&gt;I also think that this loss of laziness also makes &lt;a href=&quot;http://citeseer.ist.psu.edu/590305&quot; title=&quot;Levent Erk&amp;#65533;k. Value Recursion in Monadic Computations.&quot;&gt;value recursion&lt;/a&gt; a la &lt;code&gt;MonadFix&lt;/code&gt; very difficult.&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;&lt;div id=&quot;about-the-author&quot;&gt;&lt;h1&gt;&lt;a href=&quot;http://apfelmus.nfshost.com/rss.xml#TOC&quot;&gt;About the author&lt;/a&gt;&lt;/h1&gt;&lt;p&gt;After some initial programming experience in Pascal, Heinrich Apfelmus picked up Haskell and purely functional programming just at the dawn of the new millenium. He has never looked back ever since, for he not only admires Haskell's mathematical elegance, but also its practicality in personal life. For instance, he was always too lazy to tie knots, but that has changed and he now accepts shoe laces instead of velcro.&lt;/p&gt;&lt;/div&gt;</description>
	<pubDate>Wed, 03 Feb 2010 14:11:43 +0000</pubDate>
</item>
<item>
	<title>Mikael Vejdemo Johansson (Syzygy-): Testing out the wplatex package</title>
	<guid isPermalink="false">http://blog.mikael.johanssons.org/?p=243</guid>
	<link>http://blog.mikael.johanssons.org/archive/2010/02/testing-out-the-wplatex-package/</link>
	<description>&lt;p&gt;
Eric Finster, over at &lt;a href=&quot;http://curiousreasoning.wordpress.com&quot;&gt;Curious Reasoning&lt;/a&gt; has built a python script to allow you to write Wordpress posts entirely in LaTeX , and upload them. The script parses the LaTeX code and generates HTML that expresses the same structure. &lt;/p&gt;
&lt;p&gt;
This, here, is me trying it out. With any luck, the appearance of a new toy will get me back to actually blogging some more &amp;#8211; it’s been winding down a bit much here lately. &lt;/p&gt;</description>
	<pubDate>Wed, 03 Feb 2010 04:38:45 +0000</pubDate>
	<dc:creator>Michi</dc:creator>
</item>
<item>
	<title>Gareth Smith (gds): Random 11pm thought: Privacy through insecurity.</title>
	<guid isPermalink="false">http://totherme.livejournal.com/10063.html</guid>
	<link>http://totherme.livejournal.com/10063.html</link>
	<description>If you haven't considered the potential for (and potential dangers of) total lack of privacy in the information age, go watch &lt;a href=&quot;http://www.openrightsgroup.org/blog/2009/Bruce-Schneier-video-and-book-giveaway&quot;&gt;this&lt;/a&gt; before reading the rest of this post.&lt;br /&gt;&lt;br /&gt;I've met a whole bunch of cool new people in the last year, and I keep &quot;in touch&quot; with many of them primarily using facebook. You know how this story goes - you go to a conference or take a short course of gardening classes or something ; you meet new folk ; you spend a lot of time with them for a few weeks ; then you go your separate ways and communicate for a year or two only through the medium of broadcast &quot;status updates&quot; that can be read by anyone you've ever met.&lt;br /&gt;&lt;br /&gt;One of the folk I met last year has been getting increasingly facebook-eccentric. They've been posting increasingly embarrassing and personal status updates, starting their own fan club, joining the support groups for controversial political parties, and most recently, writing long essays defending their strange and cultish religious views. Of course, none of that stuff was &lt;em&gt;actually&lt;/em&gt; my new friend - all they're really guilty of is forgetting to log out of facebook after using a public terminal. Repeatedly.&lt;br /&gt;&lt;br /&gt;So now, any time I see anything embarrassing on my new friend's facebook page, I'll just assume that they've left themselves logged in again, and someone's having a laugh at their expense. I don't know what's actually going on - I haven't seen or spoken to them in months. I'll just assume that anything &quot;normal&quot; is the truth, and anything out of the ordinary is a practical joke. And so will anyone else who knows them. If they run for the presidency of the US in 20 years time, and some journalist finds something juicy in the old digital records, the spin doctors will be able to laugh it all off. &lt;q&gt;That wasn't our candidate - that was a well documented series of attacks by notorious hackers of the time.&lt;/q&gt;&lt;br /&gt;&lt;br /&gt;Of course, this is just a special case of increasing the signal to noise ratio on the net - which you can do any number of ways. It's fairly well known that companies will post &lt;a href=&quot;http://www.google.com/search?q=fake+product+review&quot;&gt;fake product reviews&lt;/a&gt; if they can get away with it.  Perhaps we could all open large numbers of facebook accounts, and use each of them to communicate with 1/n of our friends. Perhaps spin doctors should spend their time inventing implausible stories about their candidates, and filling the net with them, so the real stories get lost in the mess. Perhaps they already do. I'm sure none of this is a new idea, but it tickled me that forgetting to log out, or having an easy to guess password might offer my friend more privacy in the long run, not less.&lt;br /&gt;&lt;br /&gt;&lt;small&gt;Also:  I decided to try using the plural in this note - if anyone has an opinion on that vs &lt;a href=&quot;http://en.wikipedia.org/wiki/Gender-neutral_pronoun&quot;&gt;GNP&lt;/a&gt;s or any other way of writing what I wanted to write, then feel free to comment.&lt;/small&gt;</description>
	<pubDate>Mon, 01 Feb 2010 23:47:31 +0000</pubDate>
</item>

</channel>
</rss>
