<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>LAMBDAPHONE &#187; haskell</title>
	<atom:link href="http://coder.bsimmons.name/blog/tag/haskell/feed/" rel="self" type="application/rss+xml" />
	<link>http://coder.bsimmons.name/blog</link>
	<description>fragmentary ideas  ䷿  intellectual what-nots  ䷷  and haskell programming  ䷴</description>
	<lastBuildDate>Tue, 27 Jul 2010 16:58:03 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>The Gift of Inconsistency</title>
		<link>http://coder.bsimmons.name/blog/2010/07/the-gift-of-inconsistency/</link>
		<comments>http://coder.bsimmons.name/blog/2010/07/the-gift-of-inconsistency/#comments</comments>
		<pubDate>Thu, 22 Jul 2010 23:35:23 +0000</pubDate>
		<dc:creator>jberryman</dc:creator>
				<category><![CDATA[haskell]]></category>
		<category><![CDATA[EsotericLanguages]]></category>
		<category><![CDATA[LambdaCalculus]]></category>
		<category><![CDATA[laziness]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[theory]]></category>

		<guid isPermaLink="false">http://coder.bsimmons.name/blog/?p=453</guid>
		<description><![CDATA[<p>Computer science is a fascinating and maddening thing. Even the most seemingly-esoteric topics turn out to be fundamental. Go out to the fringes of CS and you find yourself smack in the middle of <a href="http://en.wikipedia.org/wiki/G%C3%B6del%27s_incompleteness_theorems#Minds_and_machines">Philosophy</a>. You try to understand&#8230; <a href="http://coder.bsimmons.name/blog/2010/07/the-gift-of-inconsistency/" class="read_more">   [ R E A D &#124; M O R E ]</a></p>]]></description>
			<content:encoded><![CDATA[<p>Computer science is a fascinating and maddening thing. Even the most seemingly-esoteric topics turn out to be fundamental. Go out to the fringes of CS and you find yourself smack in the middle of <a href="http://en.wikipedia.org/wiki/G%C3%B6del%27s_incompleteness_theorems#Minds_and_machines">Philosophy</a>. You try to understand a single point and you suddenly find yourself embracing everything. </p>
<p>So this post comes from that infinitely-recursive rabbit hole of wikipedia topics I&#8217;ve been falling down for the last few weeks, and you can probably expect a few more like this one; I&#8217;ll try to keep focused.</p>
<p>We begin (as do All Good Things) with the <a href="http://en.wikipedia.org/wiki/Untyped_lambda_calculus">Untyped Lambda Calculus</a>:<br />
<span id="more-453"></span></p>
<h2>The System</h2>
<p>The lambda calculus, invented by <a href="http://en.wikipedia.org/wiki/Alonzo_Church">Alonzo Church</a>, is a formal system for modeling functional computation and logic. It has an extraordinarily simple set of semantics, has no notion of data as a separate idea, and yet (as was proven later) is Turing complete. It serves as the skeleton and sinew of the Lisp and Scheme programming languages.</p>
<p>At its invention it seems that the system sat right at the crossroads of formal logic, mathematics and computational theory and could embrace all three fields. But it quickly became apparent that there were apparent flaws with the Lambda Calculus as a universal system.</p>
<h2>The Paradox</h2>
<p>In 1935 it was shown that the lambda calculus was <em>logically inconsistent</em> by the <a href="http://en.wikipedia.org/wiki/Kleene%E2%80%93Rosser_paradox">Kleene–Rosser paradox</a>. The &#8220;paradox&#8221; is an apparently self-contradictory lambda expression, and goes like this:</p>
<p>We have a lambda calculus expression we will call <code>'k'</code>, which takes an argument, applies it to to itself, and negates (the ¬ symbol) the result (for some undefined, logical definition of &#8220;negate&#8221;). </p>
<blockquote><p>
k = λx.( ¬ (x x))
</p></blockquote>
<p>Okay, no problem. But look what happens when we apply the function <code>'k'</code> to itself:</p>
<blockquote><p>
k k = λx.( ¬ (x x)) k<br />
k k = ¬ (k k)
</p></blockquote>
<p>After we reduce the expression, we seem to be saying that <code>(k k)</code> is equal to the opposite of itself, a self-contradictory statement.</p>
<p>To address this apparent shortcoming of his system, Church went on to define a <a href="http://en.wikipedia.org/wiki/Simply_typed_lambda_calculus">typed lambda calculus</a>. This new system differentiated between function types and simple terms in order to disallow these kinds of problematic expressions.</p>
<p>But as with many paradoxes, this one was really an issue of perspective&#8230;</p>
<h2>The Foundation</h2>
<p>Church&#8217;s new &#8220;Simply Typed Lambda Calculus&#8221; had an interesting property that the untyped lambda calculus didn&#8217;t have: the <a href="http://en.wikipedia.org/wiki/Strongly_normalizing">normalization property</a>.</p>
<p>This more restricted version of the Lambda Calculus was &#8220;strongly normalizing&#8221;, meaning that any expression could be reduced to some <em>normal form</em> (i.e. a form that cannot be reduced further) through some sequence of rewrite/reduction steps. In programming terms, this means that every expression in the <em>typed</em> lambda calculus is <em>guaranteed to terminate</em>.</p>
<p>That sounds pretty damn useful until you realize that this interesting property also makes Church&#8217;s new typed version of his system <em>not Turing complete</em>! </p>
<p>To prove this is so, consider this: imagine you encoded a program in the typed lambda calculus; it searched for (and halted) when it found an integer that was greater than two, and was <em>not</em> the sum of two primes. </p>
<p>Merely being able to encode such a function would imply the function halted, thus disproving <a href="http://en.wikipedia.org/wiki/Goldbach%27s_conjecture">Goldbach&#8217;s conjecture</a>, one of the great open problems of mathematics. All without even needing to evaluate said expression! The function referred to is of course not expressible in the typed lambda calculus.</p>
<p>It turns out that an apparent logical contradiction was actual the essential secret to computation.</p>
<h3>Embracing Inconsistency</h3>
<p>When you think of the LC as a model of computation, rather than a framework for logical assertions, the Kleene–Rosser paradox becomes what we might call the &#8220;Kleene–Rosser useless function&#8221;. </p>
<p>Let&#8217;s go ahead and express a nearly identical paradox in <a href="http://en.wikipedia.org/wiki/Haskell_%28programming_language%29">haskell</a>:</p>
<blockquote><p>
k :: (Num a) => a<br />
k = negate k
</p></blockquote>
<p>Haskell has no notion of mutability, so what we are saying here is &#8220;k is the negation of itself&#8221;. No logical fallacies or great existential questions here, just an infinite loop (and not a very useful one)! </p>
<p>Let&#8217;s express another similar &#8220;paradox&#8221;, one we can actually <em>use</em>:</p>
<blockquote><p>
babble :: [ String ]<br />
babble = &#8220;blah&#8221; : babble
</p></blockquote>
<p>How can a list be equal to itself with an extra element added? Doesn&#8217;t that imply a paradox? Well I know the runtime isn&#8217;t swayed by that argument, producing an infinite stream of &#8220;blah&#8221;s. We can even <em>use</em> this stream because of haskell&#8217;s lazy evaluation strategy.</p>
<p>But note: it isn&#8217;t laziness that lets us get away with defining paradoxes; it just allows us to make use of some more blatant paradoxical expressions without them blowing up in our faces as soon as we call them.</p>
<h3>Conclusion and Further Thoughts</h3>
<p>Interestingly, there are quite a number of non-turing complete programming languages (or language subsets) that have been created both for theoretical purposes and for practical use. <a href="http://en.wikipedia.org/wiki/Charity_%28programming_language%29">Here</a> <a href="http://en.wikipedia.org/wiki/BlooP_and_FlooP">are</a> <a href="http://www-fp.cs.st-andrews.ac.uk/hume/index.shtml">a few</a>. They seem to owe their existence and usefulness to an environment of turing complete systems.</p>
<p>So does this mean that formal logic is Turing Incomplete? Let me know your thoughts, and please let me know if I harmed any knowledge in the making of this post.</p>
]]></content:encoded>
			<wfw:commentRss>http://coder.bsimmons.name/blog/2010/07/the-gift-of-inconsistency/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>17&#215;17: A Simulated Annealing approach using thresholds</title>
		<link>http://coder.bsimmons.name/blog/2010/07/17x17-a-simulated-annealing-approach-using-thresholds/</link>
		<comments>http://coder.bsimmons.name/blog/2010/07/17x17-a-simulated-annealing-approach-using-thresholds/#comments</comments>
		<pubDate>Sat, 10 Jul 2010 17:05:07 +0000</pubDate>
		<dc:creator>jberryman</dc:creator>
				<category><![CDATA[haskell]]></category>
		<category><![CDATA[17x17]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[Array]]></category>
		<category><![CDATA[heuristics]]></category>

		<guid isPermaLink="false">http://coder.bsimmons.name/blog/?p=443</guid>
		<description><![CDATA[<blockquote><p><em>Note: this is part of a <a href="http://coder.bsimmons.name/blog/tag/17x17/">series of posts</a> is related to the &#8220;<a href="http://blog.computationalcomplexity.org/2009/11/17x17-challenge-worth-28900-this-is-not.html">17&#215;17 Challenge</a>&#8221; posted by Bill Gasarch. The goal is to color cells of a 17 by 17 grid, using only four colors, such that no</em></p></blockquote><p>&#8230; <a href="http://coder.bsimmons.name/blog/2010/07/17x17-a-simulated-annealing-approach-using-thresholds/" class="read_more">   [ R E A D &#124; M O R E ]</a></p>]]></description>
			<content:encoded><![CDATA[<blockquote><p><em>Note: this is part of a <a href="http://coder.bsimmons.name/blog/tag/17x17/">series of posts</a> is related to the &#8220;<a href="http://blog.computationalcomplexity.org/2009/11/17x17-challenge-worth-28900-this-is-not.html">17&#215;17 Challenge</a>&#8221; posted by Bill Gasarch. The goal is to color cells of a 17 by 17 grid, using only four colors, such that no rectangle is formed from four cells of the same color.</em></p></blockquote>
<p>This is something I&#8217;ve been wanting to play with for months now, but haven&#8217;t made the time to implement: I wondered if <a href="http://en.wikipedia.org/wiki/Simulated_annealing">simulated annealing</a> techniques could be effective in finding a complete grid covering. </p>
<p>We would simply start with four copies of the 74-color grid and swap their rows and columns around, within each color set, trying to cover all of the cells by the union of the four subsets. So all of the sets of cells of a single color are fundamentally simply different permutations of the same 74-cell rectangle-free Graph. </p>
<p>Here is an illustration of the concept. Our algorithm starts with all four colored sets of cell overlapping (the red cells are on top, covering the other three colors):</p>
<p><a href="http://coder.bsimmons.name/blog/wp-content/uploads/Screenshot-74-coloring.png"><img src="http://coder.bsimmons.name/blog/wp-content/uploads/Screenshot-74-coloring-285x300.png" alt="Illustration showing four sets of colored cells, all overlapping in every cell" title="Screenshot - 74-coloring" width="285" height="300" class="aligncenter size-medium wp-image-446" /></a></p>
<p>&#8230;then we swap two rows or two columns from a colored subset of cells, until the four colored subsets spread out covering as much of the grid as possible. Here we cover all but 21 cells:</p>
<p><a href="http://coder.bsimmons.name/blog/wp-content/uploads/Screenshot-20-free.png"><img src="http://coder.bsimmons.name/blog/wp-content/uploads/Screenshot-20-free-300x291.png" alt="Four layers of cells of colors RGBY, spread out to cover most of the grid" title="Screenshot - 20 free" width="300" height="291" class="aligncenter size-medium wp-image-447" /></a></p>
<p><span id="more-443"></span></p>
<h2>A Brief Intro to Simulated Annealing</h2>
<p>Simulated Annealing is a bit of an intimidating term (has its origins in <a href="http://en.wikipedia.org/wiki/Annealing_%28metallurgy%29">metallurgy</a> of all things), but the idea is simple: you start off with an initial solution (in my case, four sets of cells, all of which overlap) and then you mutate it randomly by choosing a random &#8220;neighbor&#8221; solution (in my case, a grid in which two rows or two columns of the same color are swapped).</p>
<p>You score these solutions so that you can compare a current solution with a proposed mutated solution, and here&#8217;s the key: </p>
<blockquote><p>
You begin by accepting nearly every mutation, whether it improves your solution or not. But as the procedure progresses you become more picky about &#8220;how much worse&#8221; a new solution can be from the previous one.
</p></blockquote>
<p>So in the classic <a href="http://en.wikipedia.org/wiki/Travelling_salesman_problem">Traveling Salesman Problem</a> a neighbor solution would be a new route that goes from city C to city E, where in the last solution we went from city E to city C.</p>
<h3>Threshold Acceptance</h3>
<p>There are various ways of accomplishing the task of &#8220;ratcheting down&#8221; the computation over time. The traditional method (the method most matching the metallurgical metaphor, if you will) is to have a function that computes the probability of a proposed change being accepted. This probability function changes over time, such that as we progress it becomes more and more unlikely that a solution worse than the previous will be accepted.</p>
<p>Another method, and the one I chose to implement, seems to be referred to as <a href="http://comisef.wikidot.com/concept:thresholdaccepting">Threshold Accepting</a>; instead of having a probability function, you accept all mutations that are below a certain threshold of change from the previous solution. The threshold becomes more strict over time.</p>
<p>So in our case we might start off with a threshold of five, meaning that we will accept all changes that give us a <em>worse</em> solution of no more than 5 new un-colored cells. So for example: if swapping blue rows 5 and 12 create a new solution with 6 fewer colored (covered) cells from the previous solution (i.e. a worse solution by six), then it won&#8217;t be accepted by our meta-algorithm at this stage.</p>
<h2>Notes on the implementation</h2>
<p>The approach I tried to describe above was really attractive to me because it short-circuits the whole complex &#8220;rectangle-free&#8221; constraint entirely. We simply start with a known rectangle-free subset and see if we can make four copies fit together. </p>
<p>Also, because we swap two rows or columns at a time, we need only look at the local change of score produced by the swap and then add it to the global score to obtain the new global score.</p>
<p>The astronomical number (<code>17!^2</code>) of possible permutations of a single rectangle-free subset give me hope that this approach could work, but that is an open question as far as I know. </p>
<p>I wrote this first draft of the script in haskell (as usual), and was happy with how fast it is, thanks in large part to the <a href="http://hackage.haskell.org/cgi-bin/hackage-scripts/package/vector">vector library</a>, which was a pleasure to work with. The code uses 4 pairs of <code>Vector</code>s (arrays of Ints), 8 in total, each of which represent a row or column ordering of one of the four colors. </p>
<p>We store the original 74-cell rectangle-free subset as a 2D <code>Array (Int,Int) Bool</code>. The pairs of Vectors act as references to row or column slices of this 2D array.</p>
<p>When we want to score a swap, we have to &#8220;render&#8221; all four colors of the pair of rows/columns in question using our 2D reference array. Then a swap consists of swapping two Ints in a single Vector. This is much more friendly on more poor laptop than if we were to try storing all four color subsets as 2D arrays and swapping them around!</p>
<p>You can <a href="http://coder.bsimmons.name/code/simulatedAnnealing.lhs">download the code here</a>, but mind it isn&#8217;t incredibly pretty.</p>
<h2>Initial findings</h2>
<p>The best I&#8217;ve done with this initial version is a grid with 19 empty cells:</p>
<p><a href="http://coder.bsimmons.name/blog/wp-content/uploads/Screenshot-19-free.png"><img src="http://coder.bsimmons.name/blog/wp-content/uploads/Screenshot-19-free-283x300.png" alt="A 17 by 17 grid showing all but 19 cells colored" title="Screenshot - 19 free" width="283" height="300" class="aligncenter size-medium wp-image-448" /></a></p>
<p>It is obvious though that the heuristic needs a lot of tuning, and that in my case the &#8220;Threshold Accepting&#8221; approach isn&#8217;t working well. That is because it is far too granular: we get from complete disorder to a minimum in just a handful of threshold rounds.</p>
<p>Here is a quick graph overlaying two runs, one of which starts with a threshold (each threshold round bracketed in black) of 5, the other (in blue) starts with a threshold of 2. The Y-axis is the number of uncolored cells in the grid at the current solution:</p>
<p><a href="http://coder.bsimmons.name/blog/wp-content/uploads/5-2.combined-annotated.png"><img src="http://coder.bsimmons.name/blog/wp-content/uploads/5-2.combined-annotated-300x136.png" alt="" title="5-2.combined-annotated" width="300" height="136" class="aligncenter size-medium wp-image-449" /></a></p>
<p>You can see that both basically look like <a href="http://en.wikipedia.org/wiki/Brownian_motion">random Brownian walks</a> during the course of a threshold round, and quickly drop to a lower energy level (better solution) as soon as the threshold is tightened. Most dramatic is the jump when we go from 2 to 1.</p>
<p>This looks like we&#8217;re being thrust into a local minima, which is the opposite of what we want here. But whether a more finely-tuned annealing schedule can do better is anyone&#8217;s guess.</p>
<h2>What&#8217;s next?</h2>
<p>It should be very simple to implement a more traditional annealing procedure that would ease us into lower &#8220;energy levels&#8221; without the harsh dives we get from using only a handful of threshold levels. So that is what is next on the agenda for this problem.</p>
<p>If this annealing business looks promising after I master the black art of &#8220;tuning the meta-heuristic&#8221;, it would be cool to come up with a single-coloring algorithm, capable of generating rectangle free colorings of 72,73, or 74 cells. We would then feed those solutions into our annealing framework, finding the best combinations of colorings via a genetic algorithm or the like.</p>
<p>But that leads to the question I&#8217;ve posed earlier: it is sometimes trivial to determine if two colorings are &#8220;unique&#8221; (i.e. by counting colored cells in rows and columns); but when it isn&#8217;t trivial, determining whether two colorings are <em>&#8220;equivalent&#8221;</em> seems to be a very difficult problem. </p>
<p>What a hole I&#8217;ve fallen into&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://coder.bsimmons.name/blog/2010/07/17x17-a-simulated-annealing-approach-using-thresholds/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>17&#215;17: Further Thoughts &amp; Some Pretty Pictures</title>
		<link>http://coder.bsimmons.name/blog/2010/06/17x17-further-thoughts-some-pretty-pictures/</link>
		<comments>http://coder.bsimmons.name/blog/2010/06/17x17-further-thoughts-some-pretty-pictures/#comments</comments>
		<pubDate>Tue, 01 Jun 2010 20:35:06 +0000</pubDate>
		<dc:creator>jberryman</dc:creator>
				<category><![CDATA[haskell]]></category>
		<category><![CDATA[17x17]]></category>
		<category><![CDATA[Graph Theory]]></category>

		<guid isPermaLink="false">http://coder.bsimmons.name/blog/?p=421</guid>
		<description><![CDATA[<blockquote><p><em>Note: this is part of a <a href="http://coder.bsimmons.name/blog/tag/17x17/">series of posts</a> is related to the &#8220;<a href="http://blog.computationalcomplexity.org/2009/11/17x17-challenge-worth-28900-this-is-not.html">17&#215;17 Challenge</a>&#8221; posted by Bill Gasarch. The goal is to color cells of a 17 by 17 grid, using only four colors, such that no</em></p></blockquote><p>&#8230; <a href="http://coder.bsimmons.name/blog/2010/06/17x17-further-thoughts-some-pretty-pictures/" class="read_more">   [ R E A D &#124; M O R E ]</a></p>]]></description>
			<content:encoded><![CDATA[<blockquote><p><em>Note: this is part of a <a href="http://coder.bsimmons.name/blog/tag/17x17/">series of posts</a> is related to the &#8220;<a href="http://blog.computationalcomplexity.org/2009/11/17x17-challenge-worth-28900-this-is-not.html">17&#215;17 Challenge</a>&#8221; posted by Bill Gasarch. The goal is to color cells of a 17 by 17 grid, using only four colors, such that no rectangle is formed from four cells of the same color.</em></p></blockquote>
<p>An almost silly amount of time passes between my new ideas on this problem and investigating/coding those ideas, but I&#8217;m trying to be better about helping my thoughts see the light of day before I get entirely bored of them. </p>
<p>So in that spirit, here are a few more thoughts on the 17&#215;17 Problem investigated.</p>
<h3>Will Graph Layout Algorithms show Symmetry?</h3>
<p><a href="http://en.wikipedia.org/wiki/Force-based_algorithms_%28graph_drawing%29">Force-based algorithms</a> tend to be good at creating symmetrical layouts of graphs. The idea is to translate a Graph into a system of springs (edges) and charged particles (nodes). You then run a simulation of this 2-dimensional sytem and the graph eventually should work itself out into some equilibrium state. </p>
<p>At that point hopefully you get a layout which is more clear and perhaps makes clear some important relationships of the Graph.</p>
<p>I used the <a href="http://www.graphviz.org/">graphviz software suite</a> to do the force-directed layouts and wrote some ugly code to coax some rectangle-free single-colorings into the graphviz file format. I hoped that some of the <a href="http://coder.bsimmons.name/blog/2010/03/17x17-some-thoughts-on-the-problem/">single-</a> and <a href="http://coder.bsimmons.name/blog/2010/04/17x17-some-attempts-at-doubly-symmetrical-rotations/">double-symmetrical</a> relationships that I noticed would emerge in the process.</p>
<p><span id="more-421"></span></p>
<p>This worked perfectly for a small 5&#215;5 cell single-coloring:</p>
<p><a href="http://coder.bsimmons.name/blog/wp-content/uploads/5x5.graph.png"><img src="http://coder.bsimmons.name/blog/wp-content/uploads/5x5.graph-231x300.png" alt="A graph showing symmetry of a 5x5 grid" title="5x5.graph" width="231" height="300" class="aligncenter size-medium wp-image-424" /></a></p>
<p>Since what we&#8217;re really looking for are symmetrical relationships between <em>rows and columns</em> rather than between cells, I&#8217;ve expressed the graph above as colored cell nodes connected to nodes representing the row and the column in which the cell resides.</p>
<p>I realized I could tighten up the graph by getting rid of the nodes for the cells themselves and just showing Column nodes connecting to Row nodes, where an edge exists between the two when a colored cell lies on their intersection. (note: the rows and columns here don&#8217;t correspond to the ones above):</p>
<p><a href="http://coder.bsimmons.name/blog/wp-content/uploads/5x5.csv_.graph.png"><img src="http://coder.bsimmons.name/blog/wp-content/uploads/5x5.csv_.graph-300x245.png" alt="A tighter graph of the same" title="5x5.csv.graph" width="300" height="245" class="aligncenter size-medium wp-image-425" /></a></p>
<p>Unfortunately this wasn&#8217;t effective for the 17&#215;17 single-coloring:</p>
<p><a href="http://coder.bsimmons.name/blog/wp-content/uploads/17x17-small1.png"><img src="http://coder.bsimmons.name/blog/wp-content/uploads/17x17-small1-290x300.png" alt="A messy graph with 74 nodes" title="17x17-small" width="290" height="300" class="aligncenter size-medium wp-image-428" /></a></p>
<p>I suppose there were too many nodes and the symmetrical relationships are too tenuous to emerge. That said, I&#8217;m no graphviz expert so it&#8217;s quite possible that this could work with the right black magic.</p>
<h3>Investigating Weights of Colored and &#8220;Toxic&#8221; Cells</h3>
<blockquote><p>Note: I&#8217;ve started calling cells which have been made un-colorable (because they would form a rectangle of that color) <strong>&#8220;toxic&#8221;</strong>; so that&#8217;s how I&#8217;ll refer to them here.</p></blockquote>
<p>I wanted to investigate the relationships between colored cells and the cells that they make toxic (and vice versa), so I coded up a Haskell script that parsed a CSV format grid and did the following:</p>
<ol>
<li>For each colored cell, show the toxic cells that it has a part in making toxic</li>
<li>For each toxic cell, list all the triplets of colored cells that each make this cell toxic</li>
</ol>
<p>What I was primarily looking for were some insights that could lead to an heuristic to guide some kind of ladder-climbing search algorithm. For example if all the colored cells </p>
<p>The first point wasn&#8217;t particularly interesting to me: in the 17&#215;17 grid of 74 colored, each colored cell helped toxify between 31 and 40 un-colorable cells, which corresponded with the number of row/column neighbors that a colored cell had.</p>
<p>What was more interesting was when I looked at the graph from the perspective of the toxic (blank) cells. </p>
<p>Each toxic cell had 3, 4 or 5 unique triplets of colored cells making it toxic. Here you can see a kind of heatmap with the toxic cells in shades of gray, corresponding to how many different 3-groups of colored cells help make this toxic:</p>
<p><iframe width='500' height='500' frameborder='0' src='http://spreadsheets.google.com/pub?key=0AifWmvDBLwGodFdwVHVPNDI1WC1LbzNpQXd0VDlpLWc&#038;hl=en&#038;output=html&#038;widget=true'></iframe></p>
<h3>An Obvious Property of Toxic Cells</h3>
<p>Looking at toxic cells helped me realize an interesting property of those cells: </p>
<blockquote><p>
There is a one-to-one correspondence between a toxic cell&#8217;s row neighbors and its column neighbors in rendering the cell in question toxic.
</p></blockquote>
<p>That is for a toxic cell <em>T</em> a colored cell in <em>T</em>&#8217;s column will team up with at most a single colored cell from <em>T</em>&#8217;s row (and vice versa) in toxifying our cell. If this weren&#8217;t the case, it would imply a rectangle on our grid (you can work it out on paper if you like).</p>
<p>To go further, this means that if <em>T</em> contains a colored cell in its row and its column, <em>both of which</em> don&#8217;t contribute to making <em>T</em> toxic, then <em>T</em> is essentially not doing it&#8217;s job as well as it can. That is we might be able to add a new colored cell to form a triplet with those two colored cells, adding a new colored cell to our graph and making <em>T</em> a more effective &#8220;rectangle sink&#8221;. </p>
<p>Here&#8217;s an illustration of two different toxic cells and their respective (and partially overlapping) sets of toxifying triplets. The second tab shows how we might &#8220;improve&#8221; one of the toxic cells:</p>
<p><iframe width='500' height='500' frameborder='0' src='http://spreadsheets.google.com/pub?key=0AifWmvDBLwGodG5xNUtOajVnb3EwUlJqOXFnS005ZEE&#038;output=html&#038;widget=true'></iframe></p>
<p>Of course adding a colored cell to form a triplet with the two cells in <em>T</em>&#8217;s row, but not affecting <em>T</em> would very likely cause other currently colored cells to become toxic in the process.</p>
<h3>Conclusion</h3>
<p>I&#8217;m going to try code up an algorithm that attempts to produce an optimal single-coloring by trying to make toxic cells as effective as possible, e.g. by looking at where we can add a single colored cell and bridge several ineffective pairs at one time. I&#8217;ll also look at some other more conventional search strategies.</p>
<p>My next and hopefully last post on this junk should be of shorter and of more general compsci interest.</p>
]]></content:encoded>
			<wfw:commentRss>http://coder.bsimmons.name/blog/2010/06/17x17-further-thoughts-some-pretty-pictures/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Code Jam 2010: Incrementing a Binary Counter</title>
		<link>http://coder.bsimmons.name/blog/2010/05/code-jam-2010-incrementing-a-binary-counter/</link>
		<comments>http://coder.bsimmons.name/blog/2010/05/code-jam-2010-incrementing-a-binary-counter/#comments</comments>
		<pubDate>Sun, 09 May 2010 21:36:44 +0000</pubDate>
		<dc:creator>jberryman</dc:creator>
				<category><![CDATA[haskell]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[cellularAutomata]]></category>
		<category><![CDATA[CodeJam]]></category>
		<category><![CDATA[recursion]]></category>

		<guid isPermaLink="false">http://coder.bsimmons.name/blog/?p=406</guid>
		<description><![CDATA[<h3>The Problem</h3>
<p><a href="http://code.google.com/codejam/contest/dashboard?c=433101#s=p0">Problem A</a> in the qualifying round of this year&#8217;s Google CodeJam contest was really clever. The problem used a classic made-for-TV gadget from the 80s: <a href="http://www.youtube.com/watch?v=-XUOhjW2AXM">The Clapper™</a> (&#8220;snapper&#8221; in google&#8217;s version) and went like this:</p>
<blockquote><p>
The</p></blockquote><p>&#8230; <a href="http://coder.bsimmons.name/blog/2010/05/code-jam-2010-incrementing-a-binary-counter/" class="read_more">   [ R E A D &#124; M O R E ]</a></p>]]></description>
			<content:encoded><![CDATA[<h3>The Problem</h3>
<p><a href="http://code.google.com/codejam/contest/dashboard?c=433101#s=p0">Problem A</a> in the qualifying round of this year&#8217;s Google CodeJam contest was really clever. The problem used a classic made-for-TV gadget from the 80s: <a href="http://www.youtube.com/watch?v=-XUOhjW2AXM">The Clapper™</a> (&#8220;snapper&#8221; in google&#8217;s version) and went like this:</p>
<blockquote><p>
The Snapper is a clever little device that, on one side, plugs its input plug into an output socket, and, on the other side, exposes an output socket for plugging in a light or other device.</p>
<p>When a Snapper is in the ON state and is receiving power from its input plug, then the device connected to its output socket is receiving power as well. When you snap your fingers &#8212; making a clicking sound &#8212; any Snapper receiving power at the time of the snap toggles between the ON and OFF states.</p>
<p>In hopes of destroying the universe by means of a singularity, I have purchased N Snapper devices and chained them together by plugging the first one into a power socket, the second one into the first one, and so on. The light is plugged into the Nth Snapper.</p>
<p>Initially, all the Snappers are in the OFF state, so only the first one is receiving power from the socket, and the light is off. I snap my fingers once, which toggles the first Snapper into the ON state and gives power to the second one. I snap my fingers again, which toggles both Snappers and then promptly cuts power off from the second one, leaving it in the ON state, but with no power. I snap my fingers the third time, which toggles the first Snapper again and gives power to the second one. Now both Snappers are in the ON state, and if my light is plugged into the second Snapper it will be on. </p>
<p>I keep doing this for hours. Will the light be on or off after I have snapped my fingers K times? The light is on if and only if it&#8217;s receiving power from the Snapper it&#8217;s plugged into.
</p></blockquote>
<p><span id="more-406"></span><br />
It&#8217;s a great problem because we have a situation that is easily modeled with a computer program, and yet a naive solution will be far too inefficient to process the inputs that google provides (e.g. 10^8 claps).</p>
<p>Here is a direct-to-code translation of the functioning of a &#8220;chain of snappers&#8221; implemented in (very verbose) Haskell:</p>
<p><div class="vimblock"><br />
<span class="Comment">&gt;</span>&nbsp;<span class="Type">module</span>&nbsp;Main<br />
<span class="Comment">&gt;</span>&nbsp;&nbsp;&nbsp;&nbsp; <span class="Type">where</span><br />
<span class="Comment">&gt;</span><br />
<span class="Comment">&gt;</span>&nbsp;<span class="Type">type</span>&nbsp;ClapperChain&nbsp;<span class="Statement">=</span>&nbsp;[<span class="Type">Bool</span>]<br />
<span class="Comment">&gt;</span><br />
<span class="Comment">&gt;</span><br />
<span class="Comment">&gt;</span>&nbsp;clapOnOff&nbsp;<span class="Statement">::</span>&nbsp;ClapperChain&nbsp;<span class="Statement">-&gt;</span>&nbsp;ClapperChain<br />
<span class="Comment">&gt;</span>&nbsp;clapOnOff&nbsp;[]&nbsp;&nbsp;&nbsp;&nbsp; <span class="Statement">=</span>&nbsp;[]<br />
<span class="Comment">&gt;</span>&nbsp;clapOnOff&nbsp;(c<span class="Statement">:</span>cs)&nbsp;<span class="Statement">=</span><br />
<span class="Comment">&gt;</span>&nbsp;&nbsp;&nbsp;&nbsp; <span class="Statement">if</span>&nbsp;isPowered&nbsp;cs<br />
<span class="Comment">&gt;</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="Statement">then</span>&nbsp;toggleState&nbsp;c&nbsp;<span class="Statement">:</span>&nbsp;clapOnOff&nbsp;cs<br />
<span class="Comment">&gt;</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="Statement">else</span>&nbsp;c&nbsp;<span class="Statement">:</span>&nbsp;clapOnOff&nbsp;cs<br />
<span class="Comment">&gt;</span><br />
<span class="Comment">&gt;</span>&nbsp;isPowered&nbsp;<span class="Statement">::</span>&nbsp;ClapperChain&nbsp;<span class="Statement">-&gt;</span>&nbsp;<span class="Type">Bool</span><br />
<span class="Comment">&gt;</span>&nbsp;isPowered&nbsp;[]&nbsp;&nbsp;&nbsp;&nbsp; <span class="Statement">=</span>&nbsp;<span class="Constant">True</span>&nbsp;<span class="Comment">-- at the outlet</span><br />
<span class="Comment">&gt;</span>&nbsp;isPowered&nbsp;(c<span class="Statement">:</span>cs)&nbsp;<span class="Statement">=</span>&nbsp;inOnState&nbsp;c&nbsp;<span class="Statement">&amp;&amp;</span>&nbsp;isPowered&nbsp;cs<br />
<span class="Comment">&gt;</span><br />
<span class="Comment">&gt;</span>&nbsp;inOnState&nbsp;<span class="Statement">=</span>&nbsp;<span class="Identifier">id</span><br />
<span class="Comment">&gt;</span>&nbsp;toggleState&nbsp;<span class="Statement">=</span>&nbsp;<span class="Identifier">not</span>&nbsp;<br />
<br /></div></p>
<p>We can then look at a few iterations (snaps) and see how the chain changes. <code>True</code> is a &#8220;snapper&#8221; in the ON state:</p>
<blockquote><pre>
*Main> take 4 $ iterate clapOnOff [False,False,False]
[[False,False,False],[False,False,True],[False,True,False],[False,True,True]]</pre>
</blockquote>
<h3>A Solution</h3>
<p>After working out a few iterations on paper I realized that the snappers follow the pattern of an ascending binary number count: <code>000, 001, 010, 011...</code></p>
<p>The lamp will be on when all of the snappers are <code>1s</code> (i.e. in the ON state) which will occur every (2^number_of_snappers) snaps. Thus my solution was simply the following:</p>
<p><blockquote class="vimblock"><br />
caseAlgorithm&nbsp;(n,k)&nbsp;<span class="Statement">=</span>&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="Statement">if</span>&nbsp;(((k<span class="Statement">+</span><span class="Constant">1</span>)&nbsp;<span class="Statement">`mod`</span>&nbsp;<span class="Constant">2</span><span class="Statement">^</span>n)&nbsp;<span class="Statement">==</span>&nbsp;<span class="Constant">0</span>)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="Statement">then</span>&nbsp;ON<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="Statement">else</span>&nbsp;OFF<br />
<br /></blockquote></p>
<h3>More Code and Thoughts</h3>
<p>Here is a more terse and better version of the Snapper simulation code I posted above, now simply called <code>increment</code> for incrementing a binary number:</p>
<p><div class="vimblock"><br />
<span class="Comment">&gt;</span>&nbsp;increment&nbsp;<span class="Statement">::</span>&nbsp;ClapperChain&nbsp;<span class="Statement">-&gt;</span>&nbsp;ClapperChain<br />
<span class="Comment">&gt;</span>&nbsp;increment&nbsp;[]&nbsp;&nbsp;&nbsp;&nbsp;<span class="Statement">=</span>&nbsp;[]&nbsp;&nbsp;<span class="Comment">-- [] is the outlet</span><br />
<span class="Comment">&gt;</span>&nbsp;increment&nbsp;chain<span class="Statement">@</span>(c<span class="Statement">:</span>cs)&nbsp;&nbsp;<br />
<span class="Comment">&gt;</span>&nbsp;&nbsp;&nbsp;&nbsp; <span class="Statement">|</span>&nbsp;<span class="Identifier">and</span>&nbsp;cs&nbsp;&nbsp;&nbsp;&nbsp;<span class="Statement">=</span>&nbsp;<span class="Identifier">map</span>&nbsp;<span class="Identifier">not</span>&nbsp;chain<br />
<span class="Comment">&gt;</span>&nbsp;&nbsp;&nbsp;&nbsp; <span class="Statement">|</span>&nbsp;<span class="Identifier">otherwise</span>&nbsp;<span class="Statement">=</span>&nbsp;c&nbsp;<span class="Statement">:</span>&nbsp;increment&nbsp;cs<br />
<br /></div></p>
<p>We can convert our lists into pretty strings with&#8230;</p>
<p><div class="vimblock"><br />
<span class="Comment">&gt;</span>&nbsp;showBinary&nbsp;<span class="Statement">::</span>&nbsp;ClapperChain&nbsp;<span class="Statement">-&gt;</span>&nbsp;<span class="Type">String</span><br />
<span class="Comment">&gt;</span>&nbsp;showBinary&nbsp;<span class="Statement">=</span>&nbsp;<span class="Identifier">map</span>&nbsp;(<span class="Statement">\</span>b<span class="Statement">-&gt;</span>&nbsp;<span class="Statement">if</span>&nbsp;b&nbsp;<span class="Statement">then</span>&nbsp;<span class="Constant">'1'</span>&nbsp;<span class="Statement">else</span>&nbsp;<span class="Constant">'0'</span>)<br />
<br /></div></p>
<p>And play with it like so:</p>
<blockquote><pre>
*Main> map showBinary $ take 9 $ iterate increment  [False,False,False]
["000","001","010","011","100","101","110","111","000"]</pre>
</blockquote>
<p>I liked realizing that a binary count could be represented recursively and <a href="http://en.wikipedia.org/wiki/Counter">mechanically</a>. It led me to look into how <a href="http://www.play-hookey.com/digital/ripple_counter.html">binary</a> <a href="http://www.play-hookey.com/digital/synchronous_counter.html">counter</a> <a href="http://hyperphysics.phy-astr.gsu.edu/hbase/electronic/bincount.html">circuits</a> are implemented in electronics (it turns out The Clapper™  is not generally involved), and got me thinking about <a href="http://en.wikipedia.org/wiki/Cellular_automaton">cellular automata</a>.</p>
<p>A binary counter could be implemented as Cellular Automata in which each bit is an Automaton with two states: an ON/OFF state and a POWERED/UNPOWERED state.</p>
<p>&#8230;although I think POWERED or UNPOWERED would have to be evaluated lazily, which might not fit the model.</p>
<p>Oh well, good luck to everyone doing the competition this year!</p>
]]></content:encoded>
			<wfw:commentRss>http://coder.bsimmons.name/blog/2010/05/code-jam-2010-incrementing-a-binary-counter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lazy Arithmetic in Haskell</title>
		<link>http://coder.bsimmons.name/blog/2010/03/lazy-arithmetic-in-haskell/</link>
		<comments>http://coder.bsimmons.name/blog/2010/03/lazy-arithmetic-in-haskell/#comments</comments>
		<pubDate>Wed, 24 Mar 2010 17:24:08 +0000</pubDate>
		<dc:creator>jberryman</dc:creator>
				<category><![CDATA[haskell]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[hackage]]></category>
		<category><![CDATA[laziness]]></category>
		<category><![CDATA[NumericTypes]]></category>

		<guid isPermaLink="false">http://coder.bsimmons.name/blog/?p=372</guid>
		<description><![CDATA[<p><em>We don&#8217;t usually give much thought to Numeric data types beyond whether we want to work with integers or decimal numbers. And that is a shame! In this post I&#8217;ll look at how we can actually do arithmetic operations lazily,</em>&#8230; <a href="http://coder.bsimmons.name/blog/2010/03/lazy-arithmetic-in-haskell/" class="read_more">   [ R E A D &#124; M O R E ]</a></p>]]></description>
			<content:encoded><![CDATA[<p><em>We don&#8217;t usually give much thought to Numeric data types beyond whether we want to work with integers or decimal numbers. And that is a shame! In this post I&#8217;ll look at how we can actually do arithmetic operations lazily, and in the process hopefully reveal a bit about haskell&#8217;s numeric classes.</em></p>
<p><div class="vimblock"><br />
<span class="Comment">&gt;</span>&nbsp;<span class="Type">module</span>&nbsp;LazyArithmetic<br />
<span class="Comment">&gt;</span>&nbsp;&nbsp;&nbsp;&nbsp; <span class="Type">where</span><br />
<br /></div></p>
<p>We will be using <a href="http://hackage.haskell.org/package/numbers-2009.8.9">Lennart Augustsson&#8217;s <code>numbers</code> library</a> which can be installed from hackage with <a href="http://hackage.haskell.org/trac/hackage/wiki/CabalInstall">cabal-install</a>:</p>
<blockquote><p>$> cabal install numbers</p></blockquote>
<p><div class="vimblock"><br />
<span class="Comment">&gt;</span>&nbsp;<span class="PreProc">import</span>&nbsp;Data.Number.Natural<br />
<span class="Comment">&gt;</span>&nbsp;<span class="PreProc">import</span>&nbsp;Data.List(genericLength)<br />
<br /></div></p>
<p>Consider two functions: the Prelude function <code>sum</code> and <code>genericLength</code> from the List library:</p>
<p><blockquote class="vimblock"><br />
<span class="Comment"></span>&nbsp;<span class="Identifier">genericLength</span>&nbsp;<span class="Statement">::</span>&nbsp;(<span class="Type">Num</span>&nbsp;i)&nbsp;<span class="Statement">=&gt;</span>&nbsp;[b]&nbsp;<span class="Statement">-&gt;</span>&nbsp;i<br />
<span class="Comment"></span>&nbsp;<span class="Identifier">sum</span>&nbsp;<span class="Statement">::</span>&nbsp;(<span class="Type">Num</span>&nbsp;a)&nbsp;<span class="Statement">=&gt;</span>&nbsp;[a]&nbsp;<span class="Statement">-&gt;</span>&nbsp;a<br />
<br /></blockquote></p>
<p>&#8230;two simple functions that have the potential to be very expensive, depending on the length of the list and the values of the elements.<br />
<span id="more-372"></span><br />
Say for instance that all we want is to use:</p>
<p><div class="vimblock"><br />
<span class="Comment">&gt;</span>&nbsp;sumLengths&nbsp;<span class="Statement">=</span>&nbsp;<span class="Identifier">sum</span>&nbsp;<span class="Statement">.</span>&nbsp;<span class="Identifier">map</span>&nbsp;genericLength<br />
<span class="Comment">&gt;</span><br />
<span class="Comment">&gt;</span>&nbsp;checkLengths&nbsp;as&nbsp;n&nbsp;<span class="Statement">=</span>&nbsp;(sumLengths&nbsp;as&nbsp;<span class="Statement">`div`</span>&nbsp;n)&nbsp;<span class="Statement">&gt;</span>&nbsp;<span class="Constant">2</span><br />
<br /></div></p>
<p>The <code>checkLengths</code> function above has to count every element of every element of every sub-list in order to return a <code>Bool</code> value, and if any of those lists are infinite our program will never terminate:</p>
<p><div class="vimblock"><br />
<span class="Comment">&gt;</span>&nbsp;hopeless&nbsp;<span class="Statement">=</span>&nbsp;checkLengths&nbsp;[[<span class="Constant">'a'</span><span class="Statement">..</span><span class="Constant">'z'</span>],&nbsp;[<span class="Constant">'0'</span>],&nbsp;<span class="Identifier">repeat</span>&nbsp;<span class="Constant">'Z'</span>]&nbsp;&nbsp;<span class="Constant">99</span><br />
<br /></div></p>
<p><em>&#8230;or so we might think!</em></p>
<h3>Lazy Natural Numbers</h3>
<p>It turns out we can solve our seemingly hopeless situation with a type signature:</p>
<p><div class="vimblock"><br />
<span class="Comment">&gt;</span>&nbsp;sumLengths&nbsp;<span class="Statement">::</span>&nbsp;[[b]]&nbsp;<span class="Statement">-&gt;</span>&nbsp;Natural<br />
<br /></div></p>
<p>And suddenly, as if by magic:</p>
<blockquote><p><code>*LazyArithmetic> hopeless<br />
True</code></p></blockquote>
<p>What&#8217;s going on here? Well it turns out the problem is that Haskell&#8217;s built-in numeric types are boring, old-fashioned and (in some cases) just plain wrong. But let&#8217;s step behind the curtain and look at how the Data.Numbers library defines the Natural type internally:</p>
<p><blockquote class="vimblock"><br />
<span class="Comment"></span>&nbsp;<span class="Type">data</span>&nbsp;Natural&nbsp;<span class="Statement">=</span>&nbsp;Z&nbsp;<span class="Statement">|</span>&nbsp;S&nbsp;Natural<br />
<br /></blockquote></p>
<p>We see that we&#8217;re using an abstract data type to represent non-negative integers, and it looks nearly identical to our list type <code>[]</code>. The upshot of that is we gain, in our numeric type and arithmetic operations, the same type of laziness that lists afford us!</p>
<p>So in the above example, <code>hopeless</code>, we need only carry out the operations far enough to see that the result is <code>> 2</code>.</p>
<p>The numeric type in <code>Data.List.Natural</code> are actually known as <a href="http://www.haskell.org/haskellwiki/Peano_numbers">Peano numbers</a>. They are essentially just lists of units and, as you might imagine, are not the most efficient way to represent an integer.</p>
<p>But they are exactly what we want for this application. If we failed to realize that we could solve our problem with an appropriate numeric representation, we might try to rig up something like this:</p>
<p><div class="vimblock"><br />
<span class="Comment">&gt;</span>&nbsp;checkLengthsUnclear&nbsp;as&nbsp;n&nbsp;<span class="Statement">=</span>&nbsp;<span class="Identifier">not</span>&nbsp;<span class="Statement">$</span>&nbsp;<span class="Identifier">null</span>&nbsp;<span class="Statement">$</span>&nbsp;<span class="Identifier">drop</span>&nbsp;(n<span class="Statement">*</span><span class="Constant">2</span>)&nbsp;<span class="Statement">$</span>&nbsp;<span class="Identifier">concat</span>&nbsp;as<br />
<br /></div></p>
<p>&#8230;which works identically to <code>checkLengths</code> with Peano Numbers, but completely obfuscates the intention of the function. As usual laziness gives us <em>expressive power</em> rather than an efficiency boost.</p>
<h3>Limitations, Haskell&#8217;s Numeric Type Classes, and the Numeric Prelude</h3>
<p>The <code>Data.Number.Natural</code> library is convenient because it provides Num and Integral instances so we can use the the Natural type with any function that is polymorphic on those classes.</p>
<p>The problem is <a href="http://hackage.haskell.org/packages/archive/numbers/2009.8.9/doc/html/src/Data-Number-Natural.html#line-44">the Num instance</a> is incomplete and error-prone: and that&#8217;s because a <a href="http://en.wikipedia.org/wiki/Natural_number">natural numbe</a>r type <em>shouldn&#8217;t be an instance of Num</em>! We can&#8217;t negate a Natural, which in turn makes subtraction dangerous, etc.</p>
<blockquote><p><code>*LazyArithmetic> let x = 1 :: Natural; y = 2 in x - y<br />
*** Exception: Natural: (-)</code></p></blockquote>
<p>We might wish that haskell&#8217;s Numeric type classes were more expressive, allowing for more abstract numeric types. For example it might be more &#8220;haskelly&#8221; for the length function to be defined with the type:</p>
<p><blockquote class="vimblock"><br />
<span class="Comment"></span>&nbsp;<span class="Identifier">length</span>&nbsp;<span class="Statement">::</span>&nbsp;Natural&nbsp;n&nbsp;<span class="Statement">=&gt;</span>&nbsp;[a]&nbsp;<span class="Statement">-&gt;</span>&nbsp;n<br />
<br /></blockquote></p>
<p>After all we don&#8217;t use integers for boolean values as in C; we have the abstract type <code>Bool</code>. So why return an Int when a Natural type (or class) might be more expressive? The answer of course is that that gets really complicated really fast. </p>
<p>For one approach at making haskell&#8217;s numeric class hierarchy more expressive and flexible, check out the <a href="http://www.haskell.org/haskellwiki/Numeric_Prelude">Numeric Prelude</a>. It&#8217;s <a href="http://hackage.haskell.org/package/numeric-prelude">haddock documentation</a> is very thorough and an interesting read, and you can see the sheer number of design decisions necessary for such an undertaking. </p>
<p>For some other approaches and links, see <a href="http://www.haskell.org/haskellwiki/Libraries_and_tools/Mathematics#Type_class_hierarchies">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://coder.bsimmons.name/blog/2010/03/lazy-arithmetic-in-haskell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
