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

<channel>
	<title>Types, Programming, etc.</title>
	<atom:link href="http://edwinb.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://edwinb.wordpress.com</link>
	<description>Blogging my Brain</description>
	<lastBuildDate>Thu, 12 Feb 2009 21:04:04 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='edwinb.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/097fa1b5a8cff8fed9344102c2645175?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Types, Programming, etc.</title>
		<link>http://edwinb.wordpress.com</link>
	</image>
			<item>
		<title>Resource aware DSELs</title>
		<link>http://edwinb.wordpress.com/2009/02/12/resource-aware-dsels/</link>
		<comments>http://edwinb.wordpress.com/2009/02/12/resource-aware-dsels/#comments</comments>
		<pubDate>Thu, 12 Feb 2009 21:04:04 +0000</pubDate>
		<dc:creator>edwinb</dc:creator>
				<category><![CDATA[Dependent Types]]></category>
		<category><![CDATA[Domain Specific Languages]]></category>

		<guid isPermaLink="false">http://edwinb.wordpress.com/?p=19</guid>
		<description><![CDATA[Just wrote a short note about implementing resource aware domain specific languages by embedding in Idris. Still much to do, but the notation is getting better (less need to write strange looking types, for example).
       <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=edwinb.wordpress.com&blog=666773&post=19&subd=edwinb&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Just wrote a <a href="http://www-fp.cs.st-and.ac.uk/wordpress/?p=72">short note</a> about implementing resource aware domain specific languages by embedding in Idris. Still much to do, but the notation is getting better (less need to write strange looking types, for example).</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/edwinb.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/edwinb.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/edwinb.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/edwinb.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/edwinb.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/edwinb.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/edwinb.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/edwinb.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/edwinb.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/edwinb.wordpress.com/19/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=edwinb.wordpress.com&blog=666773&post=19&subd=edwinb&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://edwinb.wordpress.com/2009/02/12/resource-aware-dsels/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">edwin</media:title>
		</media:content>
	</item>
		<item>
		<title>FP@StAnd</title>
		<link>http://edwinb.wordpress.com/2009/02/09/fpstand/</link>
		<comments>http://edwinb.wordpress.com/2009/02/09/fpstand/#comments</comments>
		<pubDate>Mon, 09 Feb 2009 22:25:53 +0000</pubDate>
		<dc:creator>edwinb</dc:creator>
				<category><![CDATA[Dependent Types]]></category>

		<guid isPermaLink="false">http://edwinb.wordpress.com/?p=16</guid>
		<description><![CDATA[We&#8217;ve finally gone all Web 2.0 in the Functional Programming group at St Andrews. Lots of us are now twittering (I&#8217;m &#8220;edwinbrady&#8220;), and we&#8217;ve started a group blog. I&#8217;ve just posted there about some Idris hacking.
       <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=edwinb.wordpress.com&blog=666773&post=16&subd=edwinb&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>We&#8217;ve finally gone all Web 2.0 in the Functional Programming group at St Andrews. Lots of us are now twittering (I&#8217;m &#8220;<a href="http://twitter.com/edwinbrady">edwinbrady</a>&#8220;), and we&#8217;ve started a group blog. I&#8217;ve just <a href="http://www-fp.cs.st-and.ac.uk/wordpress/?p=52">posted there</a> about some Idris hacking.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/edwinb.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/edwinb.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/edwinb.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/edwinb.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/edwinb.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/edwinb.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/edwinb.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/edwinb.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/edwinb.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/edwinb.wordpress.com/16/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=edwinb.wordpress.com&blog=666773&post=16&subd=edwinb&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://edwinb.wordpress.com/2009/02/09/fpstand/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">edwin</media:title>
		</media:content>
	</item>
		<item>
		<title>Correct-by-construction Concurrency</title>
		<link>http://edwinb.wordpress.com/2008/04/03/correct-by-construction-concurrency/</link>
		<comments>http://edwinb.wordpress.com/2008/04/03/correct-by-construction-concurrency/#comments</comments>
		<pubDate>Thu, 03 Apr 2008 11:37:32 +0000</pubDate>
		<dc:creator>edwinb</dc:creator>
				<category><![CDATA[Dependent Types]]></category>
		<category><![CDATA[Domain Specific Languages]]></category>

		<guid isPermaLink="false">http://edwinb.wordpress.com/?p=15</guid>
		<description><![CDATA[I&#8217;ve been hacking merrily away in Idris over the last couple of weeks, seeing what I can make it do. One of the first things I wanted to try was an idea I had for a domain specific embedded language (DSEL) for concurrent programming. The result is written up in this draft paper.
I think one [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=edwinb.wordpress.com&blog=666773&post=15&subd=edwinb&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I&#8217;ve been hacking merrily away in <a href="http://www.cs.st-and.ac.uk/~eb/Idris">Idris</a> over the last couple of weeks, seeing what I can make it do. One of the first things I wanted to try was an idea I had for a domain specific embedded language (DSEL) for concurrent programming. The result is written up in <a href="http://www.cs.st-and.ac.uk/~eb/drafts/icfp08.pdf">this draft paper</a>.</p>
<p>I think one of the areas where dependently typed languages will become important, given appropriate and mature tools, will be the design and implementation of <em>verified</em> DSEL.<br />
Here we use a DSEL to manage the lock state (i.e. number of times locked, type of the value stored in it) of each shared resource &#8211; it&#8217;s essentially a state monad parametrised over the type and lock status of the resources &#8211; which means that the lock state is statically known. As a result, the typechecker can verify that resources are locked before they are accessed, released before exit even in exceptional conditions, and even that they are requested in an order which guarantees deadlock freedom. Programs in the DSEL look (more or less) like monadic Haskell programs.</p>
<p><span id="more-15"></span></p>
<p>There is, as you would expect, an occasional need for an explicit proof term, especially to convince the typechecker that resource requests are ordered in a way which guarantees deadlock freedom. These proof terms can be computed dynamically if necessary, and we have found in practice that, when the ordering is safe, the proofs are reasonably simple. So assuming that you have put some thought into <em>why</em> your program is deadlock free, there should be no difficulty in proving it. I do think this is a situation where good tool support is necessary though &#8211; for the sake of the reader of the program, how do you hide the proof details so as not to obscure the algorithm, and for the sake of the programmer, how much can you automate? Can the DSEL author provide a library of lemmas and decision procedures to take the burden of proof away as far as possible? If you want complete static safety, as you probably do in a safety critical real-time system, you should expect to have to do <em>some</em> work to prove it, but we want to make this as painless as possible!</p>
<p>Any comments much appreciated!</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/edwinb.wordpress.com/15/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/edwinb.wordpress.com/15/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/edwinb.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/edwinb.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/edwinb.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/edwinb.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/edwinb.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/edwinb.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/edwinb.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/edwinb.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/edwinb.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/edwinb.wordpress.com/15/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=edwinb.wordpress.com&blog=666773&post=15&subd=edwinb&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://edwinb.wordpress.com/2008/04/03/correct-by-construction-concurrency/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">edwin</media:title>
		</media:content>
	</item>
		<item>
		<title>Idris, a language with dependent types</title>
		<link>http://edwinb.wordpress.com/2008/03/09/idris-a-language-with-dependent-types/</link>
		<comments>http://edwinb.wordpress.com/2008/03/09/idris-a-language-with-dependent-types/#comments</comments>
		<pubDate>Sun, 09 Mar 2008 23:53:43 +0000</pubDate>
		<dc:creator>edwinb</dc:creator>
				<category><![CDATA[Dependent Types]]></category>

		<guid isPermaLink="false">http://edwinb.wordpress.com/?p=14</guid>
		<description><![CDATA[I was at the Dependently Typed Programming &#8216;08 workshop a couple of weeks ago, and it was a lot of fun. Lots of interesting people were there talking variously about fun programs and implementation techniques. 
One thing I thought was a little sad, though, is that we don&#8217;t yet have a real programming language (that [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=edwinb.wordpress.com&blog=666773&post=14&subd=edwinb&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I was at the <a href="http://sneezy.cs.nott.ac.uk/darcs/DTP08">Dependently Typed Programming &#8216;08</a> workshop a couple of weeks ago, and it was a lot of fun. Lots of interesting people were there talking variously about fun programs and implementation techniques. </p>
<p>One thing I thought was a little sad, though, is that we don&#8217;t yet have a real programming language (that is, a full one you can write programs in and actually run them, and interact with the outside world, read and write files, output HTML, etc, etc &#8230;, rather than proof assistants such as Coq and Agda) in which to try out the ideas for real. If you want to do that, the best you can do at the minute is Haskell with GADTs, or Tim Sheard&#8217;s Omega. These are interesting in themselves, but I want to know what we can do with full dependent types. Can we write the functional programs we know and love in the style we are used to, and add bits of dependency when we want some invariants checked, do we have to approach programming in a new way, or is some combination possible? It would be nice to have a system in which to try this out.</p>
<p>I&#8217;ve been working on file handling and concurrency recently, using the type system to manage state to prevent unauthorised access to resources and to ensure locks are managed correctly, even looking at how types can help prevent deadlock. I&#8217;ve been doing this in <a href="http://www.cs.st-and.ac.uk/~eb/Ivor">Ivor</a>, but I think now is the time to give it a go in a real language. To this end&#8230;</p>
<p>&#8230;I&#8217;ve spent the last few weeks hacking on a fledgling dependently typed language called <a href="http://www.cs.st-and.ac.uk/~eb/Idris">Idris</a>.</p>
<p>It has full dependent types, pattern matching, and some basic IO with Haskell style do notation. I&#8217;ve tried a few simple examples, including the traditional <a href="http://www-fp.cs.st-and.ac.uk/~eb/darcs/Idris/samples/interp.idr">well-typed interpreter</a>. Types need to be given for pattern matching functions, but can often be omitted for constants &#8211; full type inference is of course impossible with full dependent types.</p>
<p>[Aside: An interesting thing about this is the <code>fPlus</code> function in the interpreter example:</p>
<p><code><br />
fPlus : Term VNil (TyFun TyNat (TyFun TyNat TyNat));<br />
fPlus = Lam (Lam (plusOp (Var fO) (Var (fS fO))));<br />
</code></p>
<p>This needs a type declaration because typed terms exist relative to an environement, so we have to tell the type checker the initial environment is empty. It turns out we can also just give the environment:</p>
<p><code><br />
fPlus = Lam {G=VNil} (Lam (plusOp (Var fO) (Var (fS fO))));<br />
</code></p>
<p>So inference on terms in Idris gives type inference in the object language.] </p>
<p>There are a lot of theoretical issues still to sort out in dependently typed programming, such as how full dependent types interact with partial, general recursive functions, or how the universe hierarchy should be organised. I have decided (at least as far as this project goes) not to be interested in such things. I&#8217;d just like to get on with programming, and deal with such issues when the answers become more clear.</p>
<p>Early days yet, but I&#8217;m going to try using this for real work, and we&#8217;ll see how it goes. Any interesting updates, I&#8217;ll post them here.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/edwinb.wordpress.com/14/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/edwinb.wordpress.com/14/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/edwinb.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/edwinb.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/edwinb.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/edwinb.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/edwinb.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/edwinb.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/edwinb.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/edwinb.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/edwinb.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/edwinb.wordpress.com/14/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=edwinb.wordpress.com&blog=666773&post=14&subd=edwinb&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://edwinb.wordpress.com/2008/03/09/idris-a-language-with-dependent-types/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">edwin</media:title>
		</media:content>
	</item>
		<item>
		<title>Silence, and adding things up</title>
		<link>http://edwinb.wordpress.com/2007/05/11/silence-and-adding-things-up/</link>
		<comments>http://edwinb.wordpress.com/2007/05/11/silence-and-adding-things-up/#comments</comments>
		<pubDate>Fri, 11 May 2007 23:34:46 +0000</pubDate>
		<dc:creator>edwinb</dc:creator>
				<category><![CDATA[Dependent Types]]></category>
		<category><![CDATA[Ivor]]></category>

		<guid isPermaLink="false">http://edwinb.wordpress.com/2007/05/11/silence-and-adding-things-up/</guid>
		<description><![CDATA[Well, so much for new year resolutions  . I&#8217;d like to claim I haven&#8217;t posted anything for a while because I&#8217;ve been being super-productive, but I think it&#8217;s more due to teaching and not paying much attention to blogland. Perhaps there will be more brain dumps when the students have gone&#8230;
Anyway. We&#8217;ve just submitted [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=edwinb.wordpress.com&blog=666773&post=13&subd=edwinb&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Well, so much for <a href="http://edwinb.wordpress.com/2007/01/08/new-year-resolutions/">new year resolutions</a> <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> . I&#8217;d like to claim I haven&#8217;t posted anything for a while because I&#8217;ve been being super-productive, but I think it&#8217;s more due to teaching and not paying much attention to blogland. Perhaps there will be more brain dumps when the students have gone&#8230;</p>
<p>Anyway. We&#8217;ve just submitted a paper on the <a href="http://www.cs.st-andrews.ac.uk/~eb/drafts/tfp07.pdf">carry ripple adder</a> for binary numbers, using dependent types, to TFP 2007. </p>
<p><span id="more-13"></span></p>
<p>The brief summary is this: instead of writing the binary adder directly, and proving important properties such as commutativity, associativity etc directly, we write an addition function inductively over natural numbers, then the properties we care about are easy to show by induction. To write the binary adder, we construct a representation of binary numbers indexed over the natural numbers, so each binary number has an explicit decoding as a natural number visible in its type, and declare that the decoding of the addition of two binary numbers is the sum of the two decodings. If we can write such a function, we have a binary adder with all the properties of the natural number addition for free.</p>
<p>We&#8217;re using dependent types to give an explicit and machine checkable link between a high level implementation where correctness properties are easy to show, and a low level implementation which is something we&#8217;re more likely to want to implement in real hardware but is hard to reason about. I&#8217;m always on the lookout for more concrete problems along these lines, please let me know if you can think of others!</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/edwinb.wordpress.com/13/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/edwinb.wordpress.com/13/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/edwinb.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/edwinb.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/edwinb.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/edwinb.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/edwinb.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/edwinb.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/edwinb.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/edwinb.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/edwinb.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/edwinb.wordpress.com/13/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=edwinb.wordpress.com&blog=666773&post=13&subd=edwinb&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://edwinb.wordpress.com/2007/05/11/silence-and-adding-things-up/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">edwin</media:title>
		</media:content>
	</item>
		<item>
		<title>How to write programs in two easy steps</title>
		<link>http://edwinb.wordpress.com/2007/02/19/how-to-write-programs-in-two-easy-steps/</link>
		<comments>http://edwinb.wordpress.com/2007/02/19/how-to-write-programs-in-two-easy-steps/#comments</comments>
		<pubDate>Mon, 19 Feb 2007 01:33:45 +0000</pubDate>
		<dc:creator>edwinb</dc:creator>
				<category><![CDATA[Drunken rambling]]></category>
		<category><![CDATA[Haskell]]></category>

		<guid isPermaLink="false">http://edwinb.wordpress.com/2007/02/19/how-to-write-programs-in-two-easy-steps/</guid>
		<description><![CDATA[I&#8217;m bored, so I&#8217;m browsing the blog-o-tubes, and noticing the occasional comment wondering why functional languages like Haskell and Lisp are typically used for no more than implementing other programming languages. I won&#8217;t comment on the truth or otherwise of this claim, because I don&#8217;t think it&#8217;s a bad thing in any case. Here&#8217;s why [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=edwinb.wordpress.com&blog=666773&post=12&subd=edwinb&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I&#8217;m bored, so I&#8217;m <a href="http://programming.reddit.com/">browsing the blog-o-tubes</a>, and noticing the <a href="http://www.stifflog.com/2007/02/18/language-of-the-gods-c/">occasional comment</a> wondering why functional languages like Haskell and Lisp are typically used for no more than implementing other programming languages. I won&#8217;t comment on the truth or otherwise of this claim, because I don&#8217;t think it&#8217;s a bad thing in any case. Here&#8217;s why &#8212; this is a technique I learned from <a href="http://www.cs.nott.ac.uk/~ctm/">Conor McBride</a> (who learned it from his father Fred McBride), explaining how to write any computer program in two easy stages:</p>
<p><span id="more-12"></span></p>
<ul>
<li> Design and implement the programming language which would be best for solving the problem.
<li>Write the program in the language you&#8217;ve just implemented.
</ul>
<p>This may, on first hearing, seem less than helpful; sort of like telling someone that to play the guitar you just grab a guitar and move your hands around like a guitarist. But consider what is involved in implementing a non-trivial system (e.g. an AI for playing <a href="http://senseis.xmp.net/">Go</a>) &#8212; you spend some time coding up the domain knowledge (e.g. the rules of the game &#8212; this is where you design the language) and the rest of the time applying this knowledge to a specific problem (e.g. a strategy for winning the game &#8212; this is where you use the language).</p>
<p>I make the bold claim that most people who are faced with the task of programming a computer are not computer scientists or programmers by training, nor do they really want to know. They are perhaps physicists, engineers, mathematicians, accountants &#8212; experts in some other domain. I wouldn&#8217;t (in general, though I know several exceptions) expect them to have a deep understanding of my language of choice, <a href="http://www.haskell.org/">Haskell</a>, any more than they would expect me to, say, know how to design a bridge that doesn&#8217;t fall down, get an aeroplane off the ground, understand my tax return, or indeed do anything practically useful.</p>
<p>This is where the two stage approach to program design comes in. The programming language geek, with the help of a domain expert, designs a Domain Specific Language (DSL) for solving a certain class of problems. Then the physicists, engineers, mathematicians, accountants etc. can hack away at their own real problems blissfully unaware that they are making extensive use of laziness, monads, GADTs and so on. It&#8217;s just code reuse&#8230;</p>
<p>Haskell (along with other similar languages) is a particularly good language for implementing DSLs, in particular because algebraic data types allow you to express and manipulate abstract syntax easily and because Haskell makes it possible to embed DSLs as a library (Domain Specific Embedded Languages, or DSELs), thus saving you from writing a parser.  Some examples which already exist are <a href="http://raintown.org/lava">Lava</a> (a language for describing circuit designs), <a href="http://www.haskell.org/haskore/">Haskore</a> (a music language) and <a href="http://www.cs.uu.nl/~daan/parsec.html">Parsec</a> (a language for building parsers). You could even consider <a href="http://www.cs.st-andrews.ac.uk/~eb/Ivor">Ivor</a> a DSEL for building tactics for theorem proving.  I think it&#8217;s also an area where dependently typed languages like <a href="http://www.e-pig.org">Epigram</a> will shine, because it will one day be possible to write <a href="http://www.cs.st-andrews.ac.uk/~eb/writings/verified_staged.pdf"> <em>verified</em> interpreters</a> for domain specific languages, and the users need not know anything about the details.</p>
<p>I don&#8217;t worry that I spend time working on languages that are primarily good for implementing other languages in. It&#8217;s these other languages that allow people to get real work done (like saving me from the wrath of the tax man), and if we write tools which make the verified implementation of these languages easier, we have done a good thing.</p>
<p><a href="http://en.wikipedia.org/wiki/Greenspun's_Tenth_Rule">Greenspun&#8217;s Tenth Rule</a> states that &#8220;Any sufficiently complicated C or Fortran program contains an ad hoc, informally-specified, bug-ridden, slow implementation of half of Common Lisp.&#8221;  I like to think this means that everyone writing in C or Fortran is following Fred McBride&#8217;s method, implementing a language then writing a program in it, whether they know it or not&#8230;</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/edwinb.wordpress.com/12/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/edwinb.wordpress.com/12/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/edwinb.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/edwinb.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/edwinb.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/edwinb.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/edwinb.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/edwinb.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/edwinb.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/edwinb.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/edwinb.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/edwinb.wordpress.com/12/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=edwinb.wordpress.com&blog=666773&post=12&subd=edwinb&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://edwinb.wordpress.com/2007/02/19/how-to-write-programs-in-two-easy-steps/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">edwin</media:title>
		</media:content>
	</item>
		<item>
		<title>Stack machines</title>
		<link>http://edwinb.wordpress.com/2007/02/11/stack-machines/</link>
		<comments>http://edwinb.wordpress.com/2007/02/11/stack-machines/#comments</comments>
		<pubDate>Sun, 11 Feb 2007 22:18:40 +0000</pubDate>
		<dc:creator>edwinb</dc:creator>
				<category><![CDATA[Dependent Types]]></category>
		<category><![CDATA[Resource Aware Programming]]></category>

		<guid isPermaLink="false">http://edwinb.wordpress.com/2007/02/11/stack-machines/</guid>
		<description><![CDATA[Back in the time before at Durham, we had a snazzy whiteboard on which you could press a button and get a hard copy of your scribblings. We don&#8217;t have such a thing at St Andrews, but there are two other options. One is the smart board software, where you write onto a screen then [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=edwinb.wordpress.com&blog=666773&post=10&subd=edwinb&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Back in the time before at Durham, we had a snazzy whiteboard on which you could press a button and get a hard copy of your scribblings. We don&#8217;t have such a thing at St Andrews, but there are two other options. One is the <a href="http://smartboard.co.uk/sbsoftware/index.asp">smart board</a> software, where you write onto a screen then the computer then renders it illegible for you (at least it does when I try it). My preferred way, though, is one I&#8217;ve borrowed from the <a href="http://www.e-pig.org/epilogue">Epigram</a> gang. Just take a digital photo of the whiteboard&#8230;</p>
<p>At the minute, we&#8217;re trying to find a typed intermediate code for representing functional programs with explicit stack bounds. Here&#8217;s what James and I came up with on Friday:</p>
<p><a href="http://edwinb.files.wordpress.com/2007/02/img_1223.JPG"><br />
<img src="http://edwinb.files.wordpress.com/2007/02/whiteboard_thumb.jpg" alt="Whiteboard scribblings" /></a></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/edwinb.wordpress.com/10/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/edwinb.wordpress.com/10/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/edwinb.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/edwinb.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/edwinb.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/edwinb.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/edwinb.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/edwinb.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/edwinb.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/edwinb.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/edwinb.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/edwinb.wordpress.com/10/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=edwinb.wordpress.com&blog=666773&post=10&subd=edwinb&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://edwinb.wordpress.com/2007/02/11/stack-machines/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">edwin</media:title>
		</media:content>

		<media:content url="http://edwinb.files.wordpress.com/2007/02/whiteboard_thumb.jpg" medium="image">
			<media:title type="html">Whiteboard scribblings</media:title>
		</media:content>
	</item>
		<item>
		<title>Correctness by Construction &#8211; a Carry-Ripple Adder</title>
		<link>http://edwinb.wordpress.com/2007/01/28/correctness-by-construction-a-carry-ripple-adder/</link>
		<comments>http://edwinb.wordpress.com/2007/01/28/correctness-by-construction-a-carry-ripple-adder/#comments</comments>
		<pubDate>Sun, 28 Jan 2007 23:15:55 +0000</pubDate>
		<dc:creator>edwinb</dc:creator>
				<category><![CDATA[Dependent Types]]></category>
		<category><![CDATA[Ivor]]></category>

		<guid isPermaLink="false">http://edwinb.wordpress.com/2007/01/28/correctness-by-construction-a-carry-ripple-adder/</guid>
		<description><![CDATA[I dedided to to try out Ivor&#8217;s new pattern matching gadgetry by implementing a program I&#8217;ve been wanting to write for ages. This is a carry-ripple adder for binary numbers, where the representation of binary numbers is indexed over its meaning (i.e. the natural number it represents). Doing this has the advantage that any program [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=edwinb.wordpress.com&blog=666773&post=8&subd=edwinb&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I dedided to to try out <a href="http://www.cs.st-andrews.ac.uk/~eb/Ivor">Ivor</a>&#8217;s new pattern matching gadgetry by implementing a program I&#8217;ve been wanting to write for ages. This is a carry-ripple adder for binary numbers, where the representation of binary numbers is indexed over its meaning (i.e. the natural number it represents). Doing this has the advantage that any program written over the binary numbers must satisfy the appropriate invariant properties; a carry-ripple adder really must implement addition corresponding to addition of the natural numbers.</p>
<p><span id="more-8"></span></p>
<p>For reference, natural number addition is implemented as follows:</p>
<pre>
Data Nat : * = O : Nat | S : (k:Nat)Nat;

Patt plus : Nat -&gt; Nat -&gt; Nat =
    plus O y = k
  | plus (S k) y = S (plus k y)
</pre>
<p>A bit on its own represents either one or zero:</p>
<pre>
Data Bit : Nat -&gt; * = On : Bit (S O) | Off : Bit O;
</pre>
<p>A number is then a sequence of binary digits where the meaning is computed in the type. Note that the <code>bit</code> constructor extends an n-bit representation of the number v by one bit, b, giving a new number representing 2^n*b+v:</p>
<pre>
Data Number : (len:Nat)(val:Nat)*
  = none : Number O O
  | bit : (bv:Nat)(b:Bit bv)-&gt;
          (len:Nat)(val:Nat)(num:Number len val)-&gt;
	  (Number (S len) (plus (mult (power (S (S O)) len) bv) val));
</pre>
<p>Indexing the binary representation this way means that we can give a precise specification to the add with carry function which guarantees that any well-typed function really does implement addition corresponding to addition of naturals:</p>
<pre>
Data NumCarry : (len:Nat)(val:Nat)*
  = numcarry : (cv:Nat)(carry:Bit cv)-&gt;
               (len:Nat)(val:Nat)(n:Number len val)-&gt;
               (NumCarry len (plus (mult (power (S (S O)) len) cv) val));

addNumber : (len:Nat)(cv:Nat)(carry:Bit cv)-&gt;
                          (lv:Nat)(l:Number len lv)-&gt;
                          (rv:Nat)(r:Number len rv)-&gt;
			  (NumCarry len (plus (plus lv rv) cv))
</pre>
<p>Actually getting a function which works is somewhat involved, since at each stage you need to convince the typechecker that what you&#8217;re doing is sound. The full script is <a href="http://www-fp.cs.st-andrews.ac.uk/~eb/carryripple.tt">available here</a>. I think the proof script (which is doing no more than high school algebra) looks more scary than it needs to though. It&#8217;d be interesting to find a way for the machine to give more help with these.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/edwinb.wordpress.com/8/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/edwinb.wordpress.com/8/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/edwinb.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/edwinb.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/edwinb.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/edwinb.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/edwinb.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/edwinb.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/edwinb.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/edwinb.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/edwinb.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/edwinb.wordpress.com/8/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=edwinb.wordpress.com&blog=666773&post=8&subd=edwinb&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://edwinb.wordpress.com/2007/01/28/correctness-by-construction-a-carry-ripple-adder/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">edwin</media:title>
		</media:content>
	</item>
		<item>
		<title>Dependent Pattern Matching in Ivor</title>
		<link>http://edwinb.wordpress.com/2007/01/27/dependent-pattern-matching-in-ivor/</link>
		<comments>http://edwinb.wordpress.com/2007/01/27/dependent-pattern-matching-in-ivor/#comments</comments>
		<pubDate>Sat, 27 Jan 2007 02:26:46 +0000</pubDate>
		<dc:creator>edwinb</dc:creator>
				<category><![CDATA[Dependent Types]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Ivor]]></category>

		<guid isPermaLink="false">http://edwinb.wordpress.com/2007/01/27/dependent-pattern-matching-in-ivor/</guid>
		<description><![CDATA[I&#8217;m working on Ivor, a theorem proving library for Haskell. It&#8217;s an implementation of dependent type theory which is embeddable in Haskell applications and easily extensible with domain specific tactics. You can read a paper explaining the motivation and design in more detail.
I&#8217;ve been using it for research into using dependent types for proving resource [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=edwinb.wordpress.com&blog=666773&post=7&subd=edwinb&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I&#8217;m working on <a href="http://www.cs.st-andrews.ac.uk/~eb/Ivor">Ivor</a>, a theorem proving library for Haskell. It&#8217;s an implementation of dependent type theory which is <em>embeddable</em> in Haskell applications and easily <em>extensible</em> with domain specific tactics. You can read a <a href="http://www.cs.st-andrews.ac.uk/~eb/drafts/ivor.pdf">paper</a> explaining the motivation and design in more detail.</p>
<p>I&#8217;ve been using it for research into using dependent types for proving resource bounds of functional programs, and more generally for experiments with <a href="http://www.cs.st-andrews.ac.uk/~eb/writings/verified_staged.pdf">multi-stage programming with dependent types</a>. However, I&#8217;ve found it frustratingly hard to implement interesting dependently typed programs, because of the lack of pattern matching, or an Epigram-like <a href="http://www.cs.nott.ac.uk/~ctm/elim.ps.gz">elimination with a motive</a> tactic. Doing the latter by hand is, er, an interesting challenge up to a point, but gets tedious very quickly.</p>
<p>And so, over the last couple of days I&#8217;ve been adding dependent pattern matching. You can now do such things as bounds safe vector lookup functions as follows:</p>
<pre>
Data Nat:* = O:Nat | S:(k:Nat)Nat; -- Natural numbers

Data Fin:(n:Nat)*
  = fz:(k:Nat)Fin (S k)
  | fs:(k:Nat)(i:Fin k)(Fin (S k)); -- Bounded natural numbers

Data Vect (A:*) : (n:Nat)*
  = nil : Vect A O
  | cons : (k:Nat)(xs:Vect A k)(Vect A (S k)); -- Bounded vector

Match lookup : (A:*)(n:Nat)(i:Fin n)(xs:Vect A n)A =
    lookup _ _ (fz _) (cons _ _ x xs) = x
  | lookup _ _ (fs n i) (cons _ n x xs) = lookup _ _ i xs;</pre>
<p>Ivor checks that the patterns for <code>lookup</code> cover all possible cases (any other patterns would not be well-typed, in particular cases for <code>vnil</code>), and that recursive calls are well-founded.<br />
It will, of course, only match on constructor patterns (you can&#8217;t do anything like Epigram&#8217;s views), and you can&#8217;t develop functions interactively (i.e. leave holes in terms to fill in later) but nevertheless it&#8217;s a big improvement on what was available before.</p>
<p>I really ought to write up the way the system works out which patterns are needed to guarantee all possibilities are covered; intuitively I&#8217;m sure it works but writing it up more formally may reveal a few holes. But it&#8217;s too late in the evening (um, early in the morning in fact) to do that just now&#8230;</p>
<p>I think it would be nice to deal with implicit arguments, so that you could write, e.g.:</p>
<pre>
Match lookup : (i:Fin n)(xs:Vect A n)A =
    lookup fz (cons x xs) = x
  | lookup (fs n i) (cons x xs) = lookup i xs;</pre>
<p>Then it&#8217;s almost beginning to look like a proper programming language. However, I expect there are more important things to do first&#8230;</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/edwinb.wordpress.com/7/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/edwinb.wordpress.com/7/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/edwinb.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/edwinb.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/edwinb.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/edwinb.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/edwinb.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/edwinb.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/edwinb.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/edwinb.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/edwinb.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/edwinb.wordpress.com/7/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=edwinb.wordpress.com&blog=666773&post=7&subd=edwinb&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://edwinb.wordpress.com/2007/01/27/dependent-pattern-matching-in-ivor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">edwin</media:title>
		</media:content>
	</item>
		<item>
		<title>Error Handling in Haskell</title>
		<link>http://edwinb.wordpress.com/2007/01/15/error-handling-in-haskell/</link>
		<comments>http://edwinb.wordpress.com/2007/01/15/error-handling-in-haskell/#comments</comments>
		<pubDate>Mon, 15 Jan 2007 15:37:07 +0000</pubDate>
		<dc:creator>edwinb</dc:creator>
				<category><![CDATA[Haskell]]></category>

		<guid isPermaLink="false">http://edwinb.wordpress.com/2007/01/15/error-handling-in-haskell/</guid>
		<description><![CDATA[I like to program in Haskell, and typically write theorem provers and compilers and that sort of thing. This typically involves processing of large data structures (syntax trees representing formulas or programs) which might fail at any point. So, usually, I run such functions inside a monad; forexample, part of a typechecker might look like:

typecheck [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=edwinb.wordpress.com&blog=666773&post=6&subd=edwinb&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I like to program in <a href="http://www.haskell.org">Haskell</a>, and typically write <a href="http://www.cs.st-and.ac.uk/~eb/Ivor">theorem provers</a> and <a href="http://www.cs.st-and.ac.uk/~eb/esc.php">compilers</a> and that sort of thing. This typically involves processing of large data structures (syntax trees representing formulas or programs) which might fail at any point. So, usually, I run such functions inside a monad; forexample, part of a typechecker might look like:</p>
<pre>
typecheck :: Monad m =&gt; RawCode -&gt; m (Expression, Type)
typecheck (RawApply f a) =
	do (fgood, ftype) &lt;- typecheck f
	   (agood, atype) &lt;- typecheck a
	   if (domain ftype == atype)
		then return (Apply f a, codomain ftype)
		else fail $ "Type error " ++ ftype ++ ", " ++ atype</pre>
<p>This way, I can call <code>typecheck</code> recursively on <code>f</code> and <code>a</code> and let the machine deal with what happens if there&#8217;s an error, rather than endlessly nesting <code>case</code> expressions.</p>
<p>However, the fact that <code>fail</code> takes a <code>String</code> can be somewhat limiting. What if I want to handle the error in some appropriate way which relies on me knowing the structure of <code>ftype</code>, for example? Or there might be some specific kinds of error I can recover from that I want to handle specially (e.g. if name lookup fails, I might want to look in some other module). I might even just want to translate error messages into a number of languages. This is the sort of thing that exception handling systems in mainstream languages can do quite easily (define an exception which carries the relevant data), but in Haskell I have to make do with a <code>String</code>.</p>
<p>The Haskell library contains an <a href="http://haskell.org/ghc/docs/latest/html/libraries/mtl/Control-Monad-Error.html">Error</a> type, which I think would handle this sort of situation. But I don&#8217;t really like gratuitous use of non-portable features like multi-parameter type classes &#8212; I prefer to look for a simpler solution first. I like to keep things simple, so that other people can understand my code fairly easily (Six months from now, I will be one of those other people myself anyway).</p>
<p>[Aside: yes, I do believe <a href="http://www.e-pig.org/">Dependent Types</a> make things simpler all round. That's a topic for another day...]</p>
<p>So, anyway, now I do things like this:</p>
<pre>
data Possibly a b = Success b | Failure a
   deriving (Show, Eq, Ord)

failure :: a -&gt; Possibly a b
failure x = Failure x

success :: b -&gt; Possibly a b
success x = Success x

instance Monad (Possibly a) where
    (Success r) &gt;&gt;= k = k r
    (Failure err) &gt;&gt;= k = Failure err
    return = Success
    fail s = error $ "Possibly definitely broken: " ++ s</pre>
<p>Yes, it&#8217;s just like <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Either.html">Either</a> (but is <code>Either a</code> a monad? Why not? It seems to obey the monad laws; perhaps there is something I&#8217;m missing&#8230;) I like my name better for reasons you&#8217;ll see from the type of the next function. Now I can write code just like before, letting the monad deal with failure, while having more expressive errors. e.g.</p>
<pre>
data Broken = TooBig Int | TooSmall Int

doThings :: Int -&gt; Int -&gt; Possibly Broken Int
doThings x y = do x &lt;- check x
                  y &lt;- check y
                  success (x+y)

check :: Int -&gt; Possibly Broken Int
check x | x &gt; 5 &amp;&amp; x &lt; 20 = success x
        | x &lt; 6 = failure $ TooSmall x
        | x &gt; 19 = failure $ TooBig x</pre>
<p>And then the calling function can handle the error in some appropriate and informative way, e.g.:</p>
<pre>
run :: Int -&gt; Int -&gt; Int
run x y = case doThings x y of
            Success x -&gt; x
            Failure (TooSmall x) -&gt;
                error $ show x ++ " needs to be at least " ++
                         show (6-x) ++ " bigger"
            Failure (TooBig x) -&gt;
                error $ show x ++ " needs to be at least " ++
                         show (x-19) ++ " smaller"</pre>
<p>It seems nice and simple, and a neat way of dealing with errors more nicely in a large program, so there&#8217;s probably something deeply flawed with it. Please let me know&#8230;</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/edwinb.wordpress.com/6/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/edwinb.wordpress.com/6/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/edwinb.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/edwinb.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/edwinb.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/edwinb.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/edwinb.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/edwinb.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/edwinb.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/edwinb.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/edwinb.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/edwinb.wordpress.com/6/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=edwinb.wordpress.com&blog=666773&post=6&subd=edwinb&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://edwinb.wordpress.com/2007/01/15/error-handling-in-haskell/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">edwin</media:title>
		</media:content>
	</item>
	</channel>
</rss>