<?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>Michi's blog &#187; Haskell</title>
	<atom:link href="http://blog.mikael.johanssons.org/archive/category/computer/programming/haskell/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.mikael.johanssons.org</link>
	<description>Because my LiveJournal is too silly</description>
	<lastBuildDate>Sat, 12 Nov 2011 15:09:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Species, derivatives of types and GrÃ¶bner bases for operads</title>
		<link>http://blog.mikael.johanssons.org/archive/2010/12/species-derivatives-of-types-and-grobner-bases-for-operads/</link>
		<comments>http://blog.mikael.johanssons.org/archive/2010/12/species-derivatives-of-types-and-grobner-bases-for-operads/#comments</comments>
		<pubDate>Sat, 18 Dec 2010 01:05:30 +0000</pubDate>
		<dc:creator>Michi</dc:creator>
				<category><![CDATA[Algebra]]></category>
		<category><![CDATA[Category theory]]></category>
		<category><![CDATA[Combinatorics]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Operads and PROPs]]></category>
		<category><![CDATA[Research]]></category>

		<guid isPermaLink="false">http://blog.mikael.johanssons.org/?p=270</guid>
		<description><![CDATA[This is a typed up copy of my lecture notes from the combinatorics seminar at KTH, 2010-09-01. This is not a perfect copy of what was said at the seminar, rather a starting point from which the talk grew. In some points, I&#8217;ve tried to fill in the most sketchy and un-articulated points with some [...]]]></description>
			<content:encoded><![CDATA[<p><em>This is a typed up copy of my lecture notes from the combinatorics seminar at KTH, 2010-09-01. This is not a perfect copy of what was said at the seminar, rather a starting point from which the talk grew.</p>
<p>In some points, I&#8217;ve tried to fill in the most sketchy and un-articulated points with some simile of what I ended up actually saying.<br />
</em></p>
<p>Combinatorial species started out as a theory to deal with enumerative combinatorics, by providing a toolset &#038; calculus for formal power series. (see Bergeron-Labelle-Leroux and Joyal)</p>
<p>As it turns out, not only is species useful for manipulating generating functions, btu it provides this with a categorical approach that may be transplanted into other areas.</p>
<p>For the benefit of the entire audience, I shall introduce some definitions.</p>
<p><strong>Definition</strong>: A <em>category</em> C is a collection of <em>objects</em> and <em>arrows</em> with each arrow assigned a <em>source</em> and <em>target</em> object, such that </p>
<ol>
<li>Each object has its own <em>identity arrow</em> 1.</li>
<li>Chains of arrows are associatively <em>composable</em>, with 1 the identity of this composition.</li>
</ol>
<p><strong>Examples</strong>: Sets, Finite sets, k-Vector spaces, left and right R-Modules, Graphs, Groups, Abelian groups.</p>
<p><img src='/latexrender/pictures/e19c41413e1b974679b02bd79d5d494c.png' title='\mathbb B' alt='\mathbb B' align='middle' />: finite sets with only bijections as morphisms.</p>
<p><strong>Examples</strong>: </p>
<ul>
<li>Category of a monoid.</li>
<li>Category generated by a graph</li>
<li>Category of a group (groupoid version of the category of a monoid)</li>
<li>Category of a poset</li>
<li>Category of Haskell types and functions.</li>
</ul>
<p><strong>Definition</strong>: A <em>functor</em> F is a map of categories, in other words, a pair of maps Fo on objects and Fa on arrows, such that the identity arrow maps to identity arrows and compositions of maps map to compositions of their images.</p>
<p><strong>Examples</strong>:<br />
Constant functor: sends every object to A, every map to 1.</p>
<p>Identity functor: sends objects and arrows to themselves.</p>
<p>Underlying set functor, free monoid/vector space/module/&#8230; functors.</p>
<p>We are now equipped to define Species:<br />
<strong>Definition</strong>: A <em>species of structures</em> is a functor <img src='/latexrender/pictures/248ed9b3523a819e669addcfca0dd9e1.png' title='\mathbb B\to\mathbb B' alt='\mathbb B\to\mathbb B' align='middle' />.</p>
<p>The idea is a set gets mapped to the set of all structures labelled by the elements in the original set. A bijection on labels maps to the <em>transport of structures</em> along the relabeling. </p>
<p><strong>Examples</strong>:<br />
<img src='/latexrender/pictures/7fc56270e7a70fa81a5935b72eacbe29.png' title='A' alt='A' align='middle' />: rooted trees, labels on vertices<br />
<img src='/latexrender/pictures/dfcf28d0734569a6a693bc8194de62bf.png' title='G' alt='G' align='middle' />: simple graphs, labels on vertices<br />
<img src='/latexrender/pictures/68306c3af365d0638ca6a65e96012770.png' title='Gc' alt='Gc' align='middle' />: connected simple graphs, labels on vertices.<br />
<img src='/latexrender/pictures/0cc175b9c0f1b6a831c399e269772661.png' title='a' alt='a' align='middle' />: trees, labels on vertices.<br />
<img src='/latexrender/pictures/f623e75af30e62bbd73d6df5b50bb7b5.png' title='D' alt='D' align='middle' />: directed graphs, labels on vertices<br />
<img src='/latexrender/pictures/1a13d972a2c8473f075ded6eca465169.png' title='\wp' alt='\wp' align='middle' />: subsets, <img src='/latexrender/pictures/ab03f17fcfe3bf3f9f730c032cf12151.png' title='\wp[U] = \{S: S\subseteq U\}' alt='\wp[U] = \{S: S\subseteq U\}' align='middle' />.<br />
<img src='/latexrender/pictures/87557f11575c0ad78e4e28abedc13b6e.png' title='End' alt='End' align='middle' />: endofunctions<br />
<img src='/latexrender/pictures/a7e871520a392b978d3c9e6344c4407f.png' title='Inv' alt='Inv' align='middle' />: involutions<br />
<img src='/latexrender/pictures/5dbc98dcc983a70728bd082d1a47546e.png' title='S' alt='S' align='middle' />: permutations<br />
<img src='/latexrender/pictures/0d61f8370cad1d412f80b84d143e1257.png' title='C' alt='C' align='middle' />: cycles<br />
<img src='/latexrender/pictures/d20caec3b48a1eef164cb4ca81ba2587.png' title='L' alt='L' align='middle' />: linear orders<br />
<img src='/latexrender/pictures/3a3ea00cfc35332cedf6e5e9a32e94da.png' title='E' alt='E' align='middle' />: sets: <img src='/latexrender/pictures/4784af6daa47ce1b4dc27c7f968b48a3.png' title='E[U] = \{U\}' alt='E[U] = \{U\}' align='middle' /><br />
<img src='/latexrender/pictures/e1671797c52e15f763380b45e841ec32.png' title='e' alt='e' align='middle' />: elements: <img src='/latexrender/pictures/8f19454f9794245cdbaabf2330720444.png' title='e[U] = U' alt='e[U] = U' align='middle' /><br />
<img src='/latexrender/pictures/02129bb861061d1a052c592e2dc6b383.png' title='X' alt='X' align='middle' />: singletons: <img src='/latexrender/pictures/eedeabedbaf43a4ce7a1b347c32faf43.png' title='X[U] = U' alt='X[U] = U' align='middle' /> if <img src='/latexrender/pictures/a03dbebf1f9af1a9557fff5ace5c7534.png' title='|U|=1' alt='|U|=1' align='middle' /> and <img src='/latexrender/pictures/0118beed08546bf49c98e18e68f4e401.png' title='X[U] = \emptyset' alt='X[U] = \emptyset' align='middle' /> otherwise<br />
<img src='/latexrender/pictures/c4ca4238a0b923820dcc509a6f75849b.png' title='1' alt='1' align='middle' />: empty set: <img src='/latexrender/pictures/09f8d10bb99358b7c16cdbe36bea58a0.png' title='1[\emptyset]=\{\emptyset\}' alt='1[\emptyset]=\{\emptyset\}' align='middle' />, <img src='/latexrender/pictures/926ff03720b581dc624f4de2af07bac3.png' title='1[U] = \emptyset' alt='1[U] = \emptyset' align='middle' /> otherwise<br />
<img src='/latexrender/pictures/cfcd208495d565ef66e7dff9f98764da.png' title='0' alt='0' align='middle' />: empty species: <img src='/latexrender/pictures/e864d4557560227f5f4276edab39b730.png' title='0[U] = \emptyset' alt='0[U] = \emptyset' align='middle' />.</p>
<p>In enumerative combinatorics, the power of species resides in the association to each species a number of generating functions:</p>
<p>The generating series of a species F is the exponential formal power series<br />
<img src='/latexrender/pictures/4d16123f57c0b624af2c2edf792d1cd8.png' title='F(x) = \sum_{n=0}^\infty |F[n]|\frac{x^n}{n!}' alt='F(x) = \sum_{n=0}^\infty |F[n]|\frac{x^n}{n!}' align='middle' /><br />
where we use the convention <img src='/latexrender/pictures/aa863c04e18ac592a7560ac3e3a581b1.png' title='[n] = \{1,2,\dots,n\}' alt='[n] = \{1,2,\dots,n\}' align='middle' />, and <img src='/latexrender/pictures/e246dceb6995c5555b1eeb2d505e1708.png' title='F[n] = F[[n]]' alt='F[n] = F[[n]]' align='middle' />.<br />
Thus:<br />
<img src='/latexrender/pictures/3263ff4d9c8a689eae466290bc3052c8.png' title='L(x) = \frac{1}{1-x}' alt='L(x) = \frac{1}{1-x}' align='middle' /><br />
<img src='/latexrender/pictures/3f525bcdaa7612df493b0d76e1364151.png' title='S(x) = \frac{1}{1-x}' alt='S(x) = \frac{1}{1-x}' align='middle' /><br />
<img src='/latexrender/pictures/f77a81e173d3ac51473b37a36e4b323c.png' title='E(x) = e^x' alt='E(x) = e^x' align='middle' /><br />
<img src='/latexrender/pictures/7c5625058ffe6ddb20482fc3079c784a.png' title='e(x) = xe^x' alt='e(x) = xe^x' align='middle' /><br />
<img src='/latexrender/pictures/9063d24c51c77a8c1f84d329a2bed613.png' title='\wp(x) = e^{2x}' alt='\wp(x) = e^{2x}' align='middle' /><br />
<img src='/latexrender/pictures/38d07f869ac97a6ed1deb8574ed0953e.png' title='X(x) = x' alt='X(x) = x' align='middle' /><br />
<img src='/latexrender/pictures/da2ca5efb767fbf1d7d2b4409f3c83f0.png' title='1(x) = 1' alt='1(x) = 1' align='middle' /><br />
<img src='/latexrender/pictures/a78d5784bdd192b7dc4c1b784d719d26.png' title='0(x) = 0' alt='0(x) = 0' align='middle' /></p>
<p>There are a number other in use for combinatorics, but for my purposes, this is the one I&#8217;ll focus on.</p>
<h2>Operations on species</h2>
<p>The real power, though, emerges when we start combining species, and carry over the combinations to actions on the corresponding power series.</p>
<h3>Addition</h3>
<p>The number of ways to form either, say, a graph or a linear order on a set of labels is the sum of the numbers of ways to form either in isolation.</p>
<p>This corresponds cleanly to the coproduct in Set, and we may write F+G for the species<br />
<img src='/latexrender/pictures/d6b9d55d36e1ef2d9b3cce2ce7b09615.png' title='(F+G)[U] = F[U] + G[U]' alt='(F+G)[U] = F[U] + G[U]' align='middle' /><br />
(where the second + is the coproduct â€” i.e. disjoint union â€” of sets)</p>
<p>In the power series, we get <img src='/latexrender/pictures/66cb1c86460c5b712cc3ba8b2a0d173b.png' title='(F+G)(x) = F(x) + G(x)' alt='(F+G)(x) = F(x) + G(x)' align='middle' />.</p>
<p>Examples: We may define the species of all non-empty sets <img src='/latexrender/pictures/335d135de25fe2dbf0ccd0f20e0150e1.png' title='E_+' alt='E_+' align='middle' /> by<br />
<img src='/latexrender/pictures/d3315d79ecf2826aaf6b198de80e58d7.png' title='E = 1 + E_+' alt='E = 1 + E_+' align='middle' /><br />
This kind of functional equations is where the theory of species starts to really <em>shine</em>.</p>
<h3>Multiplication</h3>
<p>A tricoloring of a set is a subdivision of the set into three disjoint subsets covering the original set. The number of tricolorings of size n is<br />
<img src='/latexrender/pictures/2cfc95b03d7d4f37d4b7410b9361c214.png' title='\sum_{i+j+k=n} \#\text{sets of size i}\cdot\#\text{sets of size j}\cdot\#\text{sets of size k}' alt='\sum_{i+j+k=n} \#\text{sets of size i}\cdot\#\text{sets of size j}\cdot\#\text{sets of size k}' align='middle' /></p>
<p>A permutation fixes some set of points. The permutation restricted to the non-fixed points is a <em>derangement</em>. Total number of permutations on n elements is<br />
<img src='/latexrender/pictures/475edeb2b536c63ecf62bc9221c06f10.png' title='\sum_{i+j=n}\#\text{sets of size i}\cdot\#\text{derangements of size j}' alt='\sum_{i+j=n}\#\text{sets of size i}\cdot\#\text{derangements of size j}' align='middle' /></p>
<p>In both of these cases, the total generating series is a product of the component series, and we end up defining<br />
<img src='/latexrender/pictures/f05a2036cc62d20e8b6c8a7262f09461.png' title='F\cdot G[U] = \{(f,g): f\in F[U_1], g\in G[U_2], U_1\cap U_2 = \emptyset, U_1\cup U_2 = U\}' alt='F\cdot G[U] = \{(f,g): f\in F[U_1], g\in G[U_2], U_1\cap U_2 = \emptyset, U_1\cup U_2 = U\}' align='middle' /><br />
So <img src='/latexrender/pictures/25b126e16d73bffde96f15011ec62ba6.png' title='F\cdot G[U] = \sum_{U_1,U_2\text{ decompose }U} F[U_1]\times G[U_2]' alt='F\cdot G[U] = \sum_{U_1,U_2\text{ decompose }U} F[U_1]\times G[U_2]' align='middle' />.</p>
<p>Thus, tricolorings are <img src='/latexrender/pictures/77d013a0d46948384f8cd9b41535b43c.png' title='E\cdot E\cdot E' alt='E\cdot E\cdot E' align='middle' /> and permutations are <img src='/latexrender/pictures/65637937dfc1372e746af745b30dabe8.png' title='S = E\cdot Der' alt='S = E\cdot Der' align='middle' />, where the set is that of fixed points, and the derangement captures the actual action of the permutation.</p>
<h3>Composition</h3>
<p>Endofunctions of sets decompose in their actions on the points as cycles or directed trees leading in to these cycles.</p>
<p>Since a collection of disjoint cycles corresponds to a permutation, we can consider such endofunctions to be permutations decorated with rooted trees attached to points of the permutations; or even permutations of rooted trees.</p>
<p>To form such a structure on a set U, we&#8217;d first partition U into subsets, put the structure of a rooted tree on each subset, and then the structure of a permutation on the set of these subsets.</p>
<p>Thus, the number of such structures on n elements is<br />
<img src='/latexrender/pictures/335e2e52de4ab24686357d7fff2b2b15.png' title='\sum_{r\leq n}\sum_{\sum_{k=1}^n i_k = n} \#\tex{permutations on [r]}\cdot\prod_{k=1}^r\#\text{rooted trees on [i_k]}' alt='\sum_{r\leq n}\sum_{\sum_{k=1}^n i_k = n} \#\tex{permutations on [r]}\cdot\prod_{k=1}^r\#\text{rooted trees on [i_k]}' align='middle' /></p>
<p>This corresponds to the power series <img src='/latexrender/pictures/e0411f63150f22a2f99be1510b5910f0.png' title='S(A(x))' alt='S(A(x))' align='middle' />, and we write, in general, <img src='/latexrender/pictures/31029fa092055be1deacf9e244f20333.png' title='F\circ G[U]' alt='F\circ G[U]' align='middle' /> for the species of F-structures of G-structures on subsets.</p>
<p><strong>Examples</strong>:<br />
A = XÂ·E(A)<br />
L = 1 + XÂ·L<br />
B = 1 + XÂ·BÂ·B</p>
<h3>Pointing</h3>
<p>Picking out a single point in a structure on n points can be done in precisely n ways.</p>
<p>Thus the corresponding generating function will be<br />
<img src='/latexrender/pictures/149891bfc400f34cdb0f99d080acc7ef.png' title='\sum n\cdot f_n\cdot\frac{x^n}{n!}' alt='\sum n\cdot f_n\cdot\frac{x^n}{n!}' align='middle' /><br />
for f-structures with a single label distinguished.</p>
<p>Since we&#8217;re working with exponential power series, we may notice that<br />
<img src='/latexrender/pictures/c561d318629404536b769f934f360554.png' title='\frac{\partial}{\partial x} \sum f_n\frac{x^n}{n!} = \sum f_{n+1}\frac{x^n}{n!}' alt='\frac{\partial}{\partial x} \sum f_n\frac{x^n}{n!} = \sum f_{n+1}\frac{x^n}{n!}' align='middle' /><br />
and thus that derivatives are shifts.<br />
Furthermore, <img src='/latexrender/pictures/be3a9d4b57954e8b767df9234c2529b4.png' title='x\cdot\sum f_n\frac{x^n}{n!} = \sum f_n\frac{x^{n+1}}{n!} = \sum f_{n-1}n\cdot{x^n}{n!}' alt='x\cdot\sum f_n\frac{x^n}{n!} = \sum f_n\frac{x^{n+1}}{n!} = \sum f_{n-1}n\cdot{x^n}{n!}' align='middle' /><br />
so that the generating function for F-structures with a single distinguished are<br />
<img src='/latexrender/pictures/594ef9019767eed975d7e51efbddf20a.png' title='F^\bullet(x) = x\cdot\frac{\partial}{\partial x}F(x)' alt='F^\bullet(x) = x\cdot\frac{\partial}{\partial x}F(x)' align='middle' /></p>
<p>In species, this process is called pointing.</p>
<p>In functional programming, Conor McBride related this construction to Huet&#8217;s Zipper datatypes.</p>
<p>As it turns out, many of the constructions for species make eminent sense outside the category <img src='/latexrender/pictures/e19c41413e1b974679b02bd79d5d494c.png' title='\mathbb B' alt='\mathbb B' align='middle' />. In fact, species in Hask are known to programming language researchers as <em>container datatypes</em> and the whole calculus translates relatively cleanly.</p>
<p>Functional equations translate to the standard new data type definitions in Haskell.</p>
<p><strong>Examples</strong>:<br />
L = 1 + XÂ·L<br />
<code><br />
data List a = Nil | Cons a (List a)<br />
</code></p>
<p>B = 1 + XÂ·BÂ·B<br />
<code><br />
data BinaryTree a = Leaf | Node (BinaryTree a) a (BinaryTree a)<br />
</code></p>
<p>A = XÂ·E(A)<br />
<code><br />
data RootedTree a = Node a (Set (RootedTree a))<br />
</code><br />
usually, we simulate the Set here by a List. If we need for our rooted trees to be planar, we can in fact impose a Linear Order structure instead, and get something like<br />
Ap = XÂ·L(Ap)</p>
<p>The species interpretation of <img src='/latexrender/pictures/4d6c379f66675645b3ffe28a15306857.png' title='\frac{\partial}{\partial x}' alt='\frac{\partial}{\partial x}' align='middle' /> corresponding to leaving a hole in the structure carries over cleanly, so that <img src='/latexrender/pictures/27f5948c4a69349055a73e917f5f5b6e.png' title='\frac{\partial}{\partial x}T' alt='\frac{\partial}{\partial x}T' align='middle' /> is the type of T-with-a-hole.</p>
<h2>Two derivatives of lists</h2>
<p>We can deal with  <img src='/latexrender/pictures/12b5d0430c70edf45c18de84a4609c6b.png' title='\frac{\partial}{\partial x}L' alt='\frac{\partial}{\partial x}L' align='middle' /> in two different ways:</p>
<p>1.<br />
DL = D(1+XÂ·L) = D1 + D(XÂ·L) = 0 + DXÂ·L+XÂ·DL<br />
and thus<br />
DL-XÂ·DL = 1Â·L<br />
so (1-X)Â·DL = L<br />
and thus<br />
DL = LÂ·1/(1-X)</p>
<p>Now, from L=1+XÂ·L follows by a similar cavalier use of subtraction and division â€” which, by the way, in species theory is captured by the idea of a <em>virtual species</em>, and dealt with relatively cleanly â€” that<br />
L = 1+XÂ·L<br />
so<br />
L-XÂ·L = 1<br />
and thus<br />
(1-X)Â·L = 1<br />
so<br />
L = 1/(1-X)</p>
<p>Thus, we can conclude that<br />
DL = LÂ·1/(1-X) = LÂ·L<br />
and thus, a list with a hole is a pair of lists: the stuff before the hole and the stuff after the hole.</p>
<p>2.<br />
We could, instead of using implicit differentiation, as in 1, attack the derivation we had of<br />
L = 1/(1-X) = 1+X+XÂ·X+XÂ·XÂ·X+â€¦</p>
<p>Indeed,<br />
DL = D(1/(1-X)) = D(1+X+XÂ·X+â€¦) = 0+1+2X+3XÂ·X+â€¦<br />
which we can observe factors as<br />
= (1+X+XÂ·X+XÂ·XÂ·X+â€¦)Â·(1+X+XÂ·X+XÂ·XÂ·X+â€¦) = LÂ·L</p>
<p>Or we can just use the division rule<br />
DL = D(1/(1-X)) = D(1/u)Â·D(1-X) = -1/(uÂ·u)Â·(-1) = 1/[(1-X)Â·(1-X)] = LÂ·L</p>
<h2>GrÃ¶bner bases for operads</h2>
<p>All of this becomes relevant to the implementation of Buchberger&#8217;s algorithm on shuffle operads (see Dotsenkoâ€”Khoroshkin and Dotsenkoâ€”Vejdemo-Johansson)  in the step where the S-polynomials (and thereby also the reductions) are defined. With a common multiple defined, we need some way to extend the modifications that take the initial term to the common multiple to the rest of that term.</p>
<p>For this, it turns out, that the derivative of the tree datatype used provides theoretical guarantees that only partially filled in trees of the right size, with holes the right size, can be introduced; and also provides an easy and relatively efficient algorithm for contructing the hole-y trees and later filling in the holes.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mikael.johanssons.org/archive/2010/12/species-derivatives-of-types-and-grobner-bases-for-operads/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>[Stanford] MATH 198: Category Theory and Functional Programming</title>
		<link>http://blog.mikael.johanssons.org/archive/2009/08/stanford-math-198-category-theory-and-functional-programming/</link>
		<comments>http://blog.mikael.johanssons.org/archive/2009/08/stanford-math-198-category-theory-and-functional-programming/#comments</comments>
		<pubDate>Sat, 29 Aug 2009 06:19:27 +0000</pubDate>
		<dc:creator>Michi</dc:creator>
				<category><![CDATA[Category theory]]></category>
		<category><![CDATA[English]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Teaching]]></category>

		<guid isPermaLink="false">http://blog.mikael.johanssons.org/?p=226</guid>
		<description><![CDATA[Category theory, with an origin in algebra and topology, has found use in recent decades for computer science and logic applications. Possibly most clearly, this is seen in the design of the programming language Haskell &#8211; where the categorical paradigm suffuses the language design, and gives rise to several of the language constructs, most prominently [...]]]></description>
			<content:encoded><![CDATA[<p>Category theory, with an origin in algebra and topology, has found use in recent decades for computer science and logic applications. Possibly most clearly, this is seen in the design of the programming language Haskell &#8211; where the categorical paradigm suffuses the language design, and gives rise to several of the language constructs, most prominently the Monad.</p>
<p>In this course, we will teach category theory from first principles with an eye towards its applications to and correspondences with Haskell and the theory of functional programming. We expect students to previously or currently be taking CS242 and to have some level of mathematical maturity. We also expect students to have had contact with linear algebra and discrete mathematics in order to follow the motivating examples behind the theory expounded.</p>
<p>Wednesdays at 4.15.</p>
<p>Online notes will appear successively on the Haskell wiki on http://haskell.org/haskellwiki/User:Michiexile/MATH198</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mikael.johanssons.org/archive/2009/08/stanford-math-198-category-theory-and-functional-programming/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Soliciting advice</title>
		<link>http://blog.mikael.johanssons.org/archive/2009/07/soliciting-advice/</link>
		<comments>http://blog.mikael.johanssons.org/archive/2009/07/soliciting-advice/#comments</comments>
		<pubDate>Wed, 22 Jul 2009 14:28:55 +0000</pubDate>
		<dc:creator>Michi</dc:creator>
				<category><![CDATA[Category theory]]></category>
		<category><![CDATA[English]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Teaching]]></category>

		<guid isPermaLink="false">http://blog.mikael.johanssons.org/?p=224</guid>
		<description><![CDATA[Dear blogosphere, come this fall, I shall be teaching. My first lecture course, ever. The subject shall be on introducing Category Theory from the bottom up, in a manner digestible for Computer Science Undergraduates who have seen Haskell and been left wanting more from that contact. And thus comes my question to you all: what [...]]]></description>
			<content:encoded><![CDATA[<p>Dear blogosphere, </p>
<p>come this fall, I shall be teaching. My first lecture course, ever.</p>
<p>The subject shall be on introducing Category Theory from the bottom up, in a manner digestible for Computer Science Undergraduates who have seen Haskell and been left wanting more from that contact.</p>
<p>And thus comes my question to you all: what would you like to see in such a course? Is there any advice you want to give me on how to make the course awesome?</p>
<p>The obvious bits are obvious. I shall have to discuss categories, functors, (co)products, (co)limits, monads, monoids, adjoints, natural transformations, the Curry-Howard isomorphism, the Hom-Tensor adjunction, categorical interpretation of data types. And all of it with explicit reference to how all these things influence Haskell, as well as plenty of mathematical examples.</p>
<p>But what ideas can you give me to make this greater than I&#8217;d make it on my own?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mikael.johanssons.org/archive/2009/07/soliciting-advice/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>GrÃ¶bner bases for Operads â€” or what I did in my vacation</title>
		<link>http://blog.mikael.johanssons.org/archive/2009/05/grobner-bases-for-operads-or-what-i-did-in-my-vacation/</link>
		<comments>http://blog.mikael.johanssons.org/archive/2009/05/grobner-bases-for-operads-or-what-i-did-in-my-vacation/#comments</comments>
		<pubDate>Fri, 08 May 2009 17:28:57 +0000</pubDate>
		<dc:creator>Michi</dc:creator>
				<category><![CDATA[Algebra]]></category>
		<category><![CDATA[Computer]]></category>
		<category><![CDATA[English]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Operads and PROPs]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Research]]></category>

		<guid isPermaLink="false">http://blog.mikael.johanssons.org/?p=206</guid>
		<description><![CDATA[This post is to give you all a very swift and breakneck introduction to GrÃ¶bner bases; not trying to be a nice and soft introduction, but much more leading up to announcing the latest research output from me. Recall how you would run the Gaussian algorithm on a matrix. You&#8217;d take the leftmost upmost non-zero [...]]]></description>
			<content:encoded><![CDATA[<p>This post is to give you all a very swift and breakneck introduction to GrÃ¶bner bases; not trying to be a nice and soft introduction, but much more leading up to announcing the latest research output from me.</p>
<p>Recall how you would run the Gaussian algorithm on a matrix. You&#8217;d take the leftmost upmost non-zero entry, divide its row by its value, and then use that row to eliminate anything in the corresponding column.</p>
<p>Once we have the matrix on echelon form, we can then do lots of things with it. Importantly, we can use substitution using the leading terms to do equation system solving.</p>
<p>The starting point for the theory of GrÃ¶bner bases was that the same method could be used &#8211; with some modification &#8211; to produce something from a bunch of polynomials that ends up being as useful as a row-reduced echelon form.</p>
<p>Basically, the central theorem-definition of GrÃ¶bner bases (by Buchberger, named for his thesis advisor GrÃ¶bner), says that a GrÃ¶bner basis for some ideal in some polynomial ring is a bunch of generators for that ideal such that if we do polynomial division of any element of the polynomial in the ring with the generators, one after the other, until no more divisions could be performed, then what we get out of it all is uniquely determined.</p>
<p>This need not necessarily be the case, even to begin with &#8211; we could get weird loops and various kinds of bad behaviour that screws up the concept of dividing, with remainder, by a whole bunch of polynomials. Having a GrÃ¶bner basis means this is no longer a problem.</p>
<p>The important bit of the theorem is that we can GET a GrÃ¶bner basis by just iteratively trying out combinations of the generators, trying to find overlaps of the leading terms, and generating &#8220;bad examples&#8221;. Any bad example that doesn&#8217;t get completely reduced to 0 by division by the generators is also needed to complete the GrÃ¶bner basis, and by adjoining it we grow our generating set, but not the things it generates. And the method works by growing the generating set until anything we can build out of two of the generators really will reduce completely to 0.</p>
<p>And, the central theorem says, if THIS works, then reduction works in general, and we have a tool as good for solving systems of polynomial equations as the echelon form is for linear equation systems.</p>
<h2>Losing commutativity</h2>
<p>The next interesting step is to look at non-commutative polynomial rings. Here, we suddenly have a deep theoretic issue popping up. We know that the <i>word problem</i> for monoids &#8211; i.e. whether a specific string can be reduced with rewriting rules to an empty string &#8211; is unsolvable in general. It hooks up to Turing machines and the limits of theoretical computer science, but in essence we can encode problems as GrÃ¶bner basis computations for non-commutative algebras that we know cannot possibly be solved in finite time.</p>
<p>So we cannot hope for the situation to be as good as for commutative algebras. However, there is a theorem floating around &#8211; the Diamond lemma by Bergman &#8211; that says that if we DO get a GrÃ¶bner basis computation that halts in finite time, then all the good things that we had in the commutative case &#8211; such as reductions modulo the generators being well defined &#8211; hold for the things we&#8217;ve computed. In other words, the only thing that could go wrong would be that the computation of the GrÃ¶bner basis doesn&#8217;t finish in finite time.</p>
<h2>Operads</h2>
<p>Now, what I really wanted to talk about was <i>operads</i>.</p>
<p>Here, an operad is a collection {O(n) : n?1} of vector spaces with permutations attached to them. Hence, for each n, there is a map S<sub>n</sub> &larr; Hom(O(n),O(n)), or in other words, we can apply any permutation of n things to any element in the component O(n) and get something back out of it.</p>
<p>Furthermore, an operad O = {O(n)} has defined on it structure operations. These behave like the composition of multilinear functions &#8211; so we have a way of plugging one function into another:<br />
f(a1,a2,&#8230;,g(ai,&#8230;,aj),&#8230;,an)<br />
and this composition is associative, so the order we figure out some sequence of compositions doesn&#8217;t matter.</p>
<p>These gadgets show up in modern approaches to universal algebra like questions, and also all over the place in topology; some of the earliest instances were from homotopy theory. </p>
<p>Recently, Dotsenko and Khoroshkin released a paper on <a href=http://arxiv.org/abs/0812.4069>GrÃ¶bner bases for operads</a>. In the paper, they figure out a way to find the kind of canonical and ordered basis that you need in order to mimic the whole workflow of GrÃ¶bner bases above; and specified how one should go about producing a Diamond Lemma for operads. It turns out that GrÃ¶bner bases would be useful to prove operads to be Koszul &#8211; something I won&#8217;t discuss here, but which is important in the operad theory context.</p>
<p>Basically, instead of working in a polynomial ring on some set of variables, we start out with out variables in one of these graded sets {V(n)}. Then we can build rooted trees, whose internal vertices of degree n+1 are labeled by elements from V(n).</p>
<p>The vector space spanned by all such trees forms an operad where the composition operation works by taking a tree and attaching the root to the leaf numbered by whatever position we need to compose at. </p>
<p>The resulting construction is the free operad on the generating set and takes the role that the polynomial ring had in the previous examples. And what Dotsenko and Khoroshkin pointed out was that if we restrict the kinds of actions we allow from permutations on these trees somewhat, we end up with something that has exactly one representative that fits in the restricted context for each tree that occurs as a basis element of the free operad.</p>
<p>So we can impose some sort of ordering on these trees, and use them to mimic the Diamond lemma.</p>
<p>Indeed, what we end up doing is forming the overlaps between leading terms by finding trees that parts of the trees from the leading terms from pairs of operad elements can embed into, and using the resulting procedure to build the same kind of  bad cases we need to test for Buchberger&#8217;s algorithm.</p>
<p>And again, it turns out that while we may not always get an answer within finite time, if we&#8217;re lucky then the answer we DO get has all the properties we could dream of. And not only that &#8211; all the previous kinds of GrÃ¶bner bases embed as special cases of doing it this way.</p>
<p>I heard of this, and got my hands on the paper, when I first arrived in CIRM in Luminy, outside Marseille, for 2 weeks of operad theory with a master&#8217;s course and a conference on the subject. And I got so excited &#8211; once upon a time this was essentially my proposal for PhD thesis project &#8211; that I decided to sit down and code the whole thing up right away!</p>
<p>And code away I did. Once the two weeks were gone, with the valuable help from Vladimir Dotsenko and Eric Hoffbeck, I had ended up with a working implementation in Haskell of the whole paradigm. It&#8217;s now <a href="http://hackage.haskell.org/cgi-bin/hackage-scripts/package/Operads">available from the HackageDB</a>, and runs at least in GHC 6.8 and 6.10:</p>
<div class="dean_ch" style="white-space: wrap;">
ghci -cpp Math.Operad<br />
GHCi, version <span class="nu0">6.10</span><span class="nu0">.1</span>: http://www.haskell.org/ghc/ &nbsp;:? for help<br />
Loading package ghc-prim &#8230; linking &#8230; done.<br />
Loading package integer &#8230; linking &#8230; done.<br />
Loading package base &#8230; linking &#8230; done.<br />
<span class="br0">&#91;</span><span class="nu0">1</span> <span class="kw1">of</span> <span class="nu0">6</span><span class="br0">&#93;</span> Compiling Math.Operad.PPrint <span class="br0">&#40;</span> Math/Operad/PPrint.hs, interpreted <span class="br0">&#41;</span><br />
<span class="br0">&#91;</span><span class="nu0">2</span> <span class="kw1">of</span> <span class="nu0">6</span><span class="br0">&#93;</span> Compiling Math.Operad.OrderedTree <span class="br0">&#40;</span> Math/Operad/OrderedTree.hs, interpreted <span class="br0">&#41;</span><br />
<span class="br0">&#91;</span><span class="nu0">3</span> <span class="kw1">of</span> <span class="nu0">6</span><span class="br0">&#93;</span> Compiling Math.Operad.Map &nbsp;<span class="br0">&#40;</span> Math/Operad/Map.hs, interpreted <span class="br0">&#41;</span><br />
<span class="br0">&#91;</span><span class="nu0">4</span> <span class="kw1">of</span> <span class="nu0">6</span><span class="br0">&#93;</span> Compiling Math.Operad.MapOperad <span class="br0">&#40;</span> Math/Operad/MapOperad.hs, interpreted <span class="br0">&#41;</span><br />
<span class="br0">&#91;</span><span class="nu0">5</span> <span class="kw1">of</span> <span class="nu0">6</span><span class="br0">&#93;</span> Compiling Math.Operad.OperadGB <span class="br0">&#40;</span> Math/Operad/OperadGB.hs, interpreted <span class="br0">&#41;</span><br />
<span class="br0">&#91;</span><span class="nu0">6</span> <span class="kw1">of</span> <span class="nu0">6</span><span class="br0">&#93;</span> Compiling Math.Operad &nbsp; &nbsp; &nbsp;<span class="br0">&#40;</span> Math/Operad.hs, interpreted <span class="br0">&#41;</span><br />
Ok, modules loaded: Math.Operad, Math.Operad.OperadGB, Math.Operad.OrderedTree, Math.Operad.PPrint, Math.Operad.MapOperad, Math.Operad.Map.</p>
<p>*Math.Operad&gt; <span class="kw1">let</span> v = corolla <span class="nu0">2</span> <span class="br0">&#91;</span><span class="nu0">1</span>,<span class="nu0">2</span><span class="br0">&#93;</span><br />
Loading package mtl<span class="nu0">-1.1</span><span class="nu0">.0</span><span class="nu0">.2</span> &#8230; linking &#8230; done.<br />
*Math.Operad&gt; <span class="kw1">let</span> g1t1 = nsCompose <span class="nu0">1</span> v v <br />
Loading package syb &#8230; linking &#8230; done.<br />
Loading package array<span class="nu0">-0.2</span><span class="nu0">.0</span><span class="nu0">.0</span> &#8230; linking &#8230; done.<br />
Loading package containers<span class="nu0">-0.2</span><span class="nu0">.0</span><span class="nu0">.0</span> &#8230; linking &#8230; done.<br />
*Math.Operad&gt; <span class="kw1">let</span> g1t2 = nsCompose <span class="nu0">2</span> v v<br />
*Math.Operad&gt; <span class="kw1">let</span> g2t2 = shuffleCompose <span class="nu0">1</span> <span class="br0">&#91;</span><span class="nu0">1</span>,<span class="nu0">3</span>,<span class="nu0">2</span><span class="br0">&#93;</span> v v<br />
*Math.Operad&gt; <span class="kw1">let</span> g1 = <span class="br0">&#40;</span>oet g1t1<span class="br0">&#41;</span> + <span class="br0">&#40;</span>oet g1t2<span class="br0">&#41;</span> :: FreeOperad <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Integer"><span class="kw4">Integer</span></a><br />
*Math.Operad&gt; <span class="kw1">let</span> g2 = <span class="br0">&#40;</span>oet g2t2<span class="br0">&#41;</span> &#8211; <span class="br0">&#40;</span>oet g1t2<span class="br0">&#41;</span> :: FreeOperad <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Integer"><span class="kw4">Integer</span></a><br />
*Math.Operad&gt; <span class="kw1">let</span> ac = <span class="br0">&#91;</span>g1,g2<span class="br0">&#93;</span><br />
*Math.Operad&gt; :set +s<br />
*Math.Operad&gt; <span class="kw1">let</span> acGB = operadicBuchberger ac<br />
<span class="br0">&#40;</span><span class="nu0">0.00</span> secs, <span class="nu0">524996</span> bytes<span class="br0">&#41;</span><br />
*Math.Operad&gt; pP acGB<br />
<span class="br0">&#91;</span><br />
<span class="nu0">+1</span> % <span class="nu0">1</span>*m2<span class="br0">&#40;</span><span class="nu0">1</span>,m2<span class="br0">&#40;</span><span class="nu0">2</span>,m2<span class="br0">&#40;</span><span class="nu0">3</span>,<span class="nu0">4</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>,</p>
<p><span class="nu0">+1</span> % <span class="nu0">1</span>*m2<span class="br0">&#40;</span>m2<span class="br0">&#40;</span><span class="nu0">1</span>,<span class="nu0">2</span><span class="br0">&#41;</span>,<span class="nu0">3</span><span class="br0">&#41;</span><br />
<span class="nu0">+1</span> % <span class="nu0">1</span>*m2<span class="br0">&#40;</span><span class="nu0">1</span>,m2<span class="br0">&#40;</span><span class="nu0">2</span>,<span class="nu0">3</span><span class="br0">&#41;</span><span class="br0">&#41;</span>,</p>
<p><span class="nu0">+1</span> % <span class="nu0">1</span>*m2<span class="br0">&#40;</span>m2<span class="br0">&#40;</span><span class="nu0">1</span>,<span class="nu0">3</span><span class="br0">&#41;</span>,<span class="nu0">2</span><span class="br0">&#41;</span><br />
+<span class="br0">&#40;</span><span class="nu0">-1</span><span class="br0">&#41;</span> % <span class="nu0">1</span>*m2<span class="br0">&#40;</span><span class="nu0">1</span>,m2<span class="br0">&#40;</span><span class="nu0">2</span>,<span class="nu0">3</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#93;</span><br />
<span class="br0">&#40;</span><span class="nu0">0.41</span> secs, <span class="nu0">55184352</span> bytes<span class="br0">&#41;</span><br />
&nbsp;</div>
<p>And we see here that with the particular set of generators given, namely using m2 as the variable, and for a generating set, we pick<br />
m2(m2(x1,x2),x3) + m2(x1,m2(x2,x3))<br />
and<br />
m2(m2(x1,x3),x2) &#8211; m2(x1,m2(x2,x3))</p>
<p>we get a GrÃ¶bner basis almost instantly containing additionally the generator<br />
m2(x1,m2(x2,m2(x3,x4)))</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mikael.johanssons.org/archive/2009/05/grobner-bases-for-operads-or-what-i-did-in-my-vacation/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Scripting Games in Haskell</title>
		<link>http://blog.mikael.johanssons.org/archive/2008/02/scripting-games-in-haskell/</link>
		<comments>http://blog.mikael.johanssons.org/archive/2008/02/scripting-games-in-haskell/#comments</comments>
		<pubDate>Thu, 21 Feb 2008 00:15:04 +0000</pubDate>
		<dc:creator>Michi</dc:creator>
				<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.mikael.johanssons.org/archive/2008/02/scripting-games-in-haskell/</guid>
		<description><![CDATA[I saw the Cerebrate solve the first Scripting Games challenge: Pairing off. And immediately thought &#8220;I can do that in Haskell too&#8221;. So, here it is. import Data.List cards = [(1,7),(0,5),(3,7),(2,7),(2,13)] countpairs [] = 0 countpairs [a] = 0 countpairs (a:as) = length . filter (((snd a)==) . snd) $ as pairingOff = sum . [...]]]></description>
			<content:encoded><![CDATA[<p>I saw <a href="http://feeds.feedburner.com/~r/cerebratescontemplations/~3/238385247/scripting-games-event-1beginne.html">the Cerebrate solve</a> the first <a href="http://www.microsoft.com/technet/scriptcenter/funzone/games/games08/bevent1.mspx">Scripting Games challenge: Pairing off</a>. And immediately thought &#8220;I can do that in Haskell too&#8221;.</p>
<p>So, here it is. </p>
<pre lang=haskell>
import Data.List

cards = [(1,7),(0,5),(3,7),(2,7),(2,13)]

countpairs [] = 0
countpairs [a] = 0
countpairs (a:as) = length . filter (((snd a)==) . snd) $ as

pairingOff = sum . map countpairs . tails
</pre>
<p>And that&#8217;s that. Alas, the actual competition only takes Perl, VBScript and PowerShell, so I won&#8217;t be submitting this.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mikael.johanssons.org/archive/2008/02/scripting-games-in-haskell/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Enumerating the Saneblidze-Umble diagonal in Haskell</title>
		<link>http://blog.mikael.johanssons.org/archive/2007/06/enumerating-the-saneblidze-umble-diagonal-in-haskell/</link>
		<comments>http://blog.mikael.johanssons.org/archive/2007/06/enumerating-the-saneblidze-umble-diagonal-in-haskell/#comments</comments>
		<pubDate>Mon, 11 Jun 2007 14:47:02 +0000</pubDate>
		<dc:creator>Michi</dc:creator>
				<category><![CDATA[A-infinity]]></category>
		<category><![CDATA[Algebra]]></category>
		<category><![CDATA[Combinatorics]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[PhD]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.mikael.johanssons.org/archive/2007/06/enumerating-the-saneblidze-umble-diagonal-in-haskell/</guid>
		<description><![CDATA[IMPORTANT: Note that the implementation herein is severely flawed. Do not use this. One subject I spent a lot of time thinking about this spring was taking tensor products of A&#8734;-algebras. This turns out to actually already being solved &#8211; having a very combinatorial and pretty neat solution. Recall that we can describe ways to [...]]]></description>
			<content:encoded><![CDATA[<p><i>IMPORTANT: Note that the implementation herein is severely flawed. Do not use this.</i></p>
<p>One subject I spent a lot of time thinking about this spring was taking tensor products of A<sub>&infin;</sub>-algebras. This turns out to actually already being solved &#8211; having a very combinatorial and pretty neat solution.</p>
<p>Recall that we can describe ways to associate operations and homotopy of associators by a sequence of polyhedra K<sub>n</sub>, n=2,3,.., called the associahedra. An A<sub>&infin;</sub>-algebra can be defined as being a map from the <i>cellular chains on the Associahedra</i> to <i>n</i>-ary endomorphisms of a graded vector space.</p>
<p>If this was incomprehensible to you, no matter for this post. The essence is that by figuring out how to deal with these polyhedra, we can figure out how to deal with A<sub>&infin;</sub>-algebras.</p>
<p>A tensor product of algebras is basically the direct product of the polyhedra, and we can get precisely the thing we want if only we find a map from the cellular chains on K<sub>n</sub> to the cellular chains on K<sub>n</sub>xK<sub>n</sub> fulfilling a few neat properties.</p>
<p>This map is what is known in the literature as the Saneblidze-Umble diagonal from the people who presented it in a paper from the late 90s. They describe in that paper, and more clearly in a later paper, how to actually get hold of one map with the right properties. The road goes over basic matrices, derived matrices and connecting matrices to trees describing associations. I&#8217;d like to take this opportunity to work through the implementation I just wrote of an enumerator for these, displaying the code and discussing it a little bit.</p>
<p>This post will be written as literate Haskell, so copy the text into a file, and then just save it as <code>SaneblidzeUmble.lhs</code> and you&#8217;re ready to go.</p>
<p>So, we will build a module named SaneblidzeUmble that shall contain the code:</p>
<div class="dean_ch" style="white-space: wrap;">
&gt; <span class="kw1">module</span> SaneblidzeUmble <span class="kw1">where</span><br />
&nbsp;</div>
<p>We&#8217;re going to juggle a lot with lists, and a little with potentially failing calculations, so we&#8217;re going to include a couple of standard utility function sets too.</p>
<div class="dean_ch" style="white-space: wrap;">
&gt; <span class="kw1">import</span> Data.List<br />
&gt; <span class="kw1">import</span> Data.<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Maybe"><span class="kw4">Maybe</span></a><br />
&nbsp;</div>
<h2>Permutations and staircase matrices</h2>
<p>The issue we&#8217;ll want to solve in the end is to enumerate all the terms entering into a diagonal on K<sub>n</sub>xK<sub>n</sub> for one fixed <i>n</i>. These terms are, using the later to be mentioned terms, in correspondence to derived matrices of staircase matrices with entries from [1,..,n-1]. A <i>staircase matrix</i> is one with one entry for each diagonal (parallel to the main diagonal), and such that along each row and column, all entries are adjacent and strictly increasing down and to the right. </p>
<p>As an example, we have the matrix</p>
<pre>
.14
25.
3..
</pre>
<p>Given a permutation of [1,..,n-1], we can write the images in the order they occur, and then just take this as a description of the entries from lower left to upper right of a staircase matrix. </p>
<p>It turns out that the basic matrices involved in this process are in bijection to permutations of [1,..,n-1] by placing the digits into the matrix, going up whenever the next entry is lower, and going to the right whenever it is larger.</p>
<p>Thus, the example above is given by the permutation (32514).</p>
<p>So, to handle this, we need to be able to list all permutations as well as pick out all rising and all falling part runs of a permutation</p>
<div class="dean_ch" style="white-space: wrap;">
&gt; <span class="kw1">type</span> Permutation <span class="br0">&#91;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span class="kw4">Int</span></a><span class="br0">&#93;</span><br />
&gt; permutations :: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span class="kw4">Int</span></a> -&gt; <span class="br0">&#91;</span>Permutation<span class="br0">&#93;</span><br />
&gt; permutations n = permuteList <span class="br0">&#91;</span><span class="nu0">1</span>..n<span class="br0">&#93;</span></p>
<p>&gt; permuteList :: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Eq"><span class="kw4">Eq</span></a> a =&gt; <span class="br0">&#91;</span>a<span class="br0">&#93;</span> -&gt; <span class="br0">&#91;</span><span class="br0">&#91;</span>a<span class="br0">&#93;</span><span class="br0">&#93;</span><br />
&gt; permuteList <span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="br0">&#91;</span><span class="br0">&#93;</span><br />
&gt; permuteList <span class="br0">&#91;</span>a<span class="br0">&#93;</span> = <span class="br0">&#91;</span><span class="br0">&#91;</span>a<span class="br0">&#93;</span><span class="br0">&#93;</span><br />
&gt; permuteList list = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:concatMap"><span class="kw3">concatMap</span></a> <span class="br0">&#40;</span>\x -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map"><span class="kw3">map</span></a> <span class="br0">&#40;</span>x:<span class="br0">&#41;</span> <span class="br0">&#40;</span>permuteList <span class="br0">&#40;</span>l \\ <span class="br0">&#91;</span>x<span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> list<br />
&nbsp;</div>
<p>This is almost readable as it stands. The permutations of [1..n] are just the permutations of the list [1..n].<br />
There are no permutations of an empty list, the list with one element has one permutation of itself, and for any larger list we pick out one element at the time, permute the rest and prepend this element. The collected results are compiled into a single long list and returned.</p>
<p>Solves, by recursion, neatly and with a lot of connection to the mathematical thought behind it our problems.</p>
<p>Now, for the chopping permutations into monotonic runs, I&#8217;ll solve it all in a manner slightly decreasing the amount of code repetition. We have a &#8220;factory function&#8221;</p>
<div class="dean_ch" style="white-space: wrap;">
&gt; monotonic :: <span class="br0">&#40;</span>a -&gt; a -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Bool"><span class="kw4">Bool</span></a><span class="br0">&#41;</span> <span class="br0">&#91;</span>a<span class="br0">&#93;</span> -&gt; <span class="br0">&#91;</span><span class="br0">&#91;</span>a<span class="br0">&#93;</span><span class="br0">&#93;</span><br />
&gt; monotonic _ <span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="br0">&#91;</span><span class="br0">&#93;</span><br />
&gt; monotonic _ <span class="br0">&#91;</span>x<span class="br0">&#93;</span> = <span class="br0">&#91;</span><span class="br0">&#91;</span>x<span class="br0">&#93;</span><span class="br0">&#93;</span><br />
&gt; monotonic lt <span class="br0">&#40;</span>x:y:etc<span class="br0">&#41;</span> = <span class="kw1">if</span> lt x y <br />
&gt; &nbsp; &nbsp; <span class="kw1">then</span> <span class="br0">&#40;</span>x:s<span class="br0">&#41;</span>:ss<br />
&gt; &nbsp; &nbsp; <span class="kw1">else</span> <span class="br0">&#91;</span>x<span class="br0">&#93;</span>:<span class="br0">&#40;</span>s:ss<span class="br0">&#41;</span><br />
&gt; &nbsp; <span class="kw1">where</span> <span class="br0">&#40;</span>s:ss<span class="br0">&#41;</span> = monotonic lt <span class="br0">&#40;</span>y:etc<span class="br0">&#41;</span><br />
&nbsp;</div>
<p>and two use cases of this</p>
<div class="dean_ch" style="white-space: wrap;">
&gt; risers = monotonic <span class="br0">&#40;</span>&lt;=<span class="br0">&#41;</span><br />
&gt; fallers = monotonic <span class="br0">&#40;</span>&gt;=<span class="br0">&#41;</span><br />
&nbsp;</div>
<p>Using this, we can build the matrix out of a given permutation. The only really interesting thing for the diagonal about such a matrix is the contents of the rows and of the columns, so we shall just use that. This means we can represent a matrix as the list of rows and the list of columns. Since row and columns &#8220;look basically the same&#8221;, they&#8217;ll be represented by the same type. So we have</p>
<div class="dean_ch" style="white-space: wrap;">
&gt; <span class="kw1">type</span> Row <span class="br0">&#91;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span class="kw4">Int</span></a><span class="br0">&#93;</span><br />
&gt; buildMatrix :: Permutation -&gt; <span class="br0">&#40;</span><span class="br0">&#91;</span>Row<span class="br0">&#93;</span>,<span class="br0">&#91;</span>Row<span class="br0">&#93;</span><span class="br0">&#41;</span><br />
&gt; buildMatrix p = <span class="br0">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map"><span class="kw3">map</span></a> sort <span class="br0">&#40;</span>fallers p<span class="br0">&#41;</span>, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse"><span class="kw3">reverse</span></a> <span class="br0">&#40;</span>risers p<span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp;</div>
<p>The reason we use map sort and reverse is to make the result fit with the conventions chosen by Saneblidze and Umble. Note: when actually reading the results, users should take care to read the Columns backwards.</p>
<h2>Deriving staircase matrices</h2>
<p>Deriving the matrices is done with so called moves. We have a downward move, and a rightward move &#8211; both consisting of picking a subset of elements in one row or one column, and pushing them over to the next &#8211; given the condition that all the moved elements have to be larger than the largest they move over to. They also have to move to empty positions, but the ordering conditions for the staircase matrices will guarantee that if they really are larger, then the corresponding position is already empty.</p>
<p>So, we can write a a function that gives us all the subsets of the first column/row that might possibly be moved to the second in a given column/row-list of a matrix</p>
<div class="dean_ch" style="white-space: wrap;">
&gt; listMovable :: <span class="br0">&#91;</span>Row<span class="br0">&#93;</span> -&gt; <span class="br0">&#91;</span><span class="br0">&#91;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span class="kw4">Int</span></a><span class="br0">&#93;</span><span class="br0">&#93;</span><br />
&gt; listMovable <span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="br0">&#91;</span><span class="br0">&#93;</span><br />
&gt; listMovable <span class="br0">&#91;</span>a<span class="br0">&#93;</span> = <span class="br0">&#91;</span><span class="br0">&#93;</span><br />
&gt; listMovable <span class="br0">&#40;</span>a:b:<span class="kw1">as</span><span class="br0">&#41;</span> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:filter"><span class="kw3">filter</span></a> <span class="br0">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:not"><span class="kw3">not</span></a>.<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:null"><span class="kw3">null</span></a><span class="br0">&#41;</span> $ subsets <span class="br0">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:filter"><span class="kw3">filter</span></a> <span class="br0">&#40;</span>&gt;<span class="br0">&#40;</span>m b<span class="br0">&#41;</span><span class="br0">&#41;</span> a<span class="br0">&#41;</span> <span class="kw1">where</span><br />
&gt; &nbsp; m <span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="nu0">0</span><br />
&gt; &nbsp; m b = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:maximum"><span class="kw3">maximum</span></a> b<br />
&nbsp;</div>
<p>This function picks out all the subsets of elements that fulfill the requirement of being larger than the largest element of the next row. But hang on &#8211; this <code>subset</code>-function actually isn&#8217;t defined yet. No worries &#8211; it&#8217;s easily done:</p>
<div class="dean_ch" style="white-space: wrap;">
&gt; subsets :: <span class="br0">&#91;</span>a<span class="br0">&#93;</span> -&gt; <span class="br0">&#91;</span><span class="br0">&#91;</span>a<span class="br0">&#93;</span><span class="br0">&#93;</span><br />
&gt; subsets <span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="br0">&#91;</span><span class="br0">&#91;</span><span class="br0">&#93;</span><span class="br0">&#93;</span><br />
&gt; subsets <span class="br0">&#40;</span>a:<span class="kw1">as</span><span class="br0">&#41;</span> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map"><span class="kw3">map</span></a> <span class="br0">&#40;</span>a:<span class="br0">&#41;</span> <span class="br0">&#40;</span>subsets <span class="kw1">as</span><span class="br0">&#41;</span> ++ subsets <span class="kw1">as</span><br />
&nbsp;</div>
<p>Again, we define everything recursively, relying on a base case of &#8220;blinding&#8221; simplicity, and end up with a very mathematical looking description. Here, the subsets of the empty set is just the empty set itself, and the subsets of any set with one element existing in it consists of the subsets containing that element and those that do not contain it. We simply list first those with and then those without. </p>
<p>Now, for the actual deriving move, we might run into a host of trouble, so we&#8217;ll just let it be able to return an error condition. It first checks a few possible error cases, and then writes down the actual move by picking out the rows it needs, modifying those, and putting it all together again in the end.</p>
<p>Supposing we have some set <i>m</i> we want to move, and we have a list of the rows where it could be moved, then we&#8217;ll want to first check whether all of <i>m</i> is in a single row together. Thus, for each row, we pick out all elements that are in our set <i>m</i>. But we don&#8217;t really care for what the elements are &#8211; we only need the number in each. So we compile a list of the lengths of these filtered out pieces.</p>
<p>We shall require the number of rows that have all the pieces we want to move to be 1, and also for that single row to have a good position within the matrix.</p>
<div class="dean_ch" style="white-space: wrap;">
&gt; moveDerivate :: <span class="br0">&#91;</span>Row<span class="br0">&#93;</span> -&gt; <span class="br0">&#91;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span class="kw4">Int</span></a><span class="br0">&#93;</span> -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Maybe"><span class="kw4">Maybe</span></a> <span class="br0">&#91;</span>Row<span class="br0">&#93;</span><br />
&gt; moveDerivate rows <span class="br0">&#91;</span><span class="br0">&#93;</span> = Nothing<br />
&gt; moveDerivate rows m = returnVal <span class="kw1">where</span><br />
&gt; &nbsp; movableInRow = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map"><span class="kw3">map</span></a> <span class="br0">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:filter"><span class="kw3">filter</span></a> <span class="br0">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:flip"><span class="kw3">flip</span></a> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:elem"><span class="kw3">elem</span></a> m<span class="br0">&#41;</span><span class="br0">&#41;</span> tr<br />
&gt; &nbsp; numberMovableInRow = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map"><span class="kw3">map</span></a> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:length"><span class="kw3">length</span></a> movableInRow<br />
&gt; &nbsp; countRowsWithMovable = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:length"><span class="kw3">length</span></a> <span class="br0">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:filter"><span class="kw3">filter</span></a> <span class="br0">&#40;</span>==<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:length"><span class="kw3">length</span></a> m<span class="br0">&#41;</span> numberMovableInRow<span class="br0">&#41;</span><br />
&gt; &nbsp; indexRow = findIndex <span class="br0">&#40;</span>==<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:length"><span class="kw3">length</span></a> m<span class="br0">&#41;</span> numberMovableInRow<br />
&gt; &nbsp; returnVal <br />
&gt; &nbsp; &nbsp; | countRowsWithMovable /= <span class="nu0">1</span> = Nothing<br />
&gt; &nbsp; &nbsp; | isNothing indexRow = Nothing<br />
&gt; &nbsp; &nbsp; | index &gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:length"><span class="kw3">length</span></a> rows &#8211; <span class="nu0">2</span> = Nothing<br />
&gt; &nbsp; &nbsp; | <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:minimum"><span class="kw3">minimum</span></a> m &lt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:maximum"><span class="kw3">maximum</span></a> <span class="br0">&#40;</span>rows !! index<span class="br0">&#41;</span> = Nothing<br />
&gt; &nbsp; &nbsp; | <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:otherwise"><span class="kw3">otherwise</span></a> = moveThem rows m index<br />
&gt; &nbsp; &nbsp; <span class="kw1">where</span><br />
&gt; &nbsp; &nbsp; &nbsp; index = fromJust indexRow<br />
&gt; &nbsp; &nbsp; &nbsp; moveThem rows m idx = Just $ <br />
&gt; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:take"><span class="kw3">take</span></a> index rows<span class="br0">&#41;</span> ++ <br />
&gt; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span><span class="br0">&#40;</span>rows !! index<span class="br0">&#41;</span> \\ m<span class="br0">&#93;</span> ++<br />
&gt; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#91;</span><span class="br0">&#40;</span>sort <span class="br0">&#40;</span>rows !! <span class="br0">&#40;</span>index + <span class="nu0">1</span><span class="br0">&#41;</span> ++ m<span class="br0">&#41;</span><span class="br0">&#93;</span> ++<br />
&gt; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:drop"><span class="kw3">drop</span></a> <span class="br0">&#40;</span>index<span class="nu0">+2</span><span class="br0">&#41;</span> rows<span class="br0">&#41;</span><br />
&nbsp;</div>
<p>That monstrous expression at the end is known as a guard expression. It works through the cases until something actually is true, and then returns whatever is after the equal sign as the value. The conditions I listed weed out all the pathologies, and lets the program pick out something sensible whenever possible.</p>
<p>So, we can list all possibilities of one row, and we can move them. This should be enough to simply generate all derived matrices, right? Let&#8217;s start simple. Let&#8217;s start with all we can do on one side of the pair &#8211; either all possible down moves or all possible right moves (they are essentially the same anyway&#8230;)</p>
<div class="dean_ch" style="white-space: wrap;">
&gt; derivedRows :: <span class="br0">&#91;</span>Rows<span class="br0">&#93;</span> -&gt; <span class="br0">&#91;</span><span class="br0">&#91;</span>Rows<span class="br0">&#93;</span><span class="br0">&#93;</span><br />
&gt; derivedRows m = derivedAcc <span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="br0">&#91;</span>m<span class="br0">&#93;</span> <span class="kw1">where</span><br />
&gt; &nbsp; derivedAcc out <span class="br0">&#91;</span><span class="br0">&#93;</span> = out<br />
&gt; &nbsp; derivedAcc out <span class="br0">&#40;</span>q:queue<span class="br0">&#41;</span> = derivedAcc <span class="br0">&#40;</span>out++<span class="br0">&#91;</span>q<span class="br0">&#93;</span><span class="br0">&#41;</span> <span class="br0">&#40;</span>queue++rest<span class="br0">&#41;</span> <span class="kw1">where</span><br />
&gt; &nbsp; &nbsp; movables = <span class="br0">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:concatMap"><span class="kw3">concatMap</span></a> listMovable . tails<span class="br0">&#41;</span> q<br />
&gt; &nbsp; &nbsp; rest = mapMaybe <span class="br0">&#40;</span>moveDerivate q<span class="br0">&#41;</span> movables<br />
&nbsp;</div>
<p>Guiding you through this piece of code &#8211; when encountered with a list of rows, we look at each starting point, list all the possibly movable sets at that point, and move the corresponding sets, placing all the results at the back of a queue that we work through. Once the queue is empty, we know we have worked through all the results from all the moves we could find, and that no others can be found.</p>
<p>So, if we have our pair of row-lists? We first move to the right, and then down. At each point, we keep the other half fixed, and simply add it on to all the results of moving in the first part.  </p>
<div class="dean_ch" style="white-space: wrap;">
&gt; derivedMatrices :: <span class="br0">&#40;</span><span class="br0">&#91;</span>Rows<span class="br0">&#93;</span>,<span class="br0">&#91;</span>Rows<span class="br0">&#93;</span><span class="br0">&#41;</span> -&gt; <span class="br0">&#91;</span><span class="br0">&#40;</span><span class="br0">&#91;</span>Rows<span class="br0">&#93;</span>,<span class="br0">&#91;</span>Rows<span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#93;</span><br />
&gt; derivedMatrices matrix@<span class="br0">&#40;</span>m1,m2<span class="br0">&#41;</span> = nub <span class="br0">&#40;</span>derivedFirst <span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="br0">&#91;</span>matrix<span class="br0">&#93;</span><span class="br0">&#41;</span> <span class="kw1">where</span><br />
&gt; &nbsp; derivedFirst matrices <span class="br0">&#91;</span><span class="br0">&#93;</span> = derivedSecond <span class="br0">&#91;</span><span class="br0">&#93;</span> matrices<br />
&gt; &nbsp; derivedFirst out <span class="br0">&#40;</span><span class="br0">&#40;</span>q1,q2<span class="br0">&#41;</span>:queue<span class="br0">&#41;</span> = derivedFirst <span class="br0">&#40;</span>out++new<span class="br0">&#41;</span> queue <span class="kw1">where</span><br />
&gt; &nbsp; &nbsp; new = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map"><span class="kw3">map</span></a> <span class="br0">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:flip"><span class="kw3">flip</span></a> <span class="br0">&#40;</span>,<span class="br0">&#41;</span> q2<span class="br0">&#41;</span> <span class="br0">&#40;</span>derivedRows q1<span class="br0">&#41;</span><br />
&gt; &nbsp; derivedSecond matrices <span class="br0">&#91;</span><span class="br0">&#93;</span> = matrices<br />
&gt; &nbsp; derivedSecond out <span class="br0">&#40;</span><span class="br0">&#40;</span>q1,q2<span class="br0">&#41;</span>:queue<span class="br0">&#41;</span> = derivedSecond <span class="br0">&#40;</span>out++new<span class="br0">&#41;</span> queue <span class="kw1">where</span><br />
&gt; &nbsp; &nbsp; new = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map"><span class="kw3">map</span></a> <span class="br0">&#40;</span><span class="br0">&#40;</span>,<span class="br0">&#41;</span> q1<span class="br0">&#41;</span> <span class="br0">&#40;</span>derivedRows q2<span class="br0">&#41;</span><br />
&nbsp;</div>
<p>This first works through one direction, taking all derived rows, and tacking on the constant second half, then when that&#8217;s done, changes to the same but for the other half of the pair.</p>
<p>Now, we&#8217;re almost able to state what a specific diagonal looks like. There is only one more thing to handle: we might not want the results of a particular computation. More specifically, we recognize something we do not want by it having a row that isn&#8217;t consecutive, not even when including previously occurring rows. So, we need to check that.</p>
<div class="dean_ch" style="white-space: wrap;">
&gt; checkTree :: <span class="br0">&#91;</span>Row<span class="br0">&#93;</span> -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Bool"><span class="kw4">Bool</span></a><br />
&gt; checkTree rows = checkAcc <span class="br0">&#91;</span><span class="br0">&#93;</span> rows <span class="kw1">where</span><br />
&gt; &nbsp; checkAcc seen <span class="br0">&#91;</span><span class="br0">&#93;</span> = True<br />
&gt; &nbsp; checkAcc seen <span class="br0">&#40;</span><span class="br0">&#91;</span><span class="br0">&#93;</span>:morerows<span class="br0">&#41;</span> = checkAcc seen morerows<br />
&gt; &nbsp; checkAcc seen <span class="br0">&#40;</span>row:morerows<span class="br0">&#41;</span> <br />
&gt; &nbsp; &nbsp; | isOK = checkAcc newseen morerows<br />
&gt; &nbsp; &nbsp; | <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:otherwise"><span class="kw3">otherwise</span></a> = False<br />
&gt; &nbsp; &nbsp; <span class="kw1">where</span><br />
&gt; &nbsp; &nbsp; &nbsp; newseen = seen ++ row<br />
&gt; &nbsp; &nbsp; &nbsp; minimal = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:minimum"><span class="kw3">minimum</span></a> newseen<br />
&gt; &nbsp; &nbsp; &nbsp; maximal = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:maximum"><span class="kw3">maximum</span></a> newseen<br />
&gt; &nbsp; &nbsp; &nbsp; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:length"><span class="kw3">length</span></a> = maximal-minimal<span class="nu0">+1</span><br />
&gt; &nbsp; &nbsp; &nbsp; isOK = <span class="br0">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:length"><span class="kw3">length</span></a> == <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:length"><span class="kw3">length</span></a> newseen<span class="br0">&#41;</span><br />
&nbsp;</div>
<p>Now it&#8217;s just a matter of putting the pieces together</p>
<div class="dean_ch" style="white-space: wrap;">
&gt; diagonalTerms :: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span class="kw4">Int</span></a> -&gt; <span class="br0">&#91;</span><span class="br0">&#40;</span><span class="br0">&#91;</span>Row<span class="br0">&#93;</span>,<span class="br0">&#91;</span>Row<span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#93;</span><br />
&gt; diagonalTerms n = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:filter"><span class="kw3">filter</span></a> checkTrees allMatrices <span class="kw1">where</span><br />
&gt; &nbsp; checkTrees <span class="br0">&#40;</span>t1,t2<span class="br0">&#41;</span> = checkTree t1 &amp;&amp; checkTree <span class="br0">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse"><span class="kw3">reverse</span></a> t2<span class="br0">&#41;</span><br />
&gt; &nbsp; allMatrices = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:concatMap"><span class="kw3">concatMap</span></a> derivedMatrices matrices<br />
&gt; &nbsp; matrices = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map"><span class="kw3">map</span></a> buildMatrix <span class="br0">&#40;</span>permutations n<span class="br0">&#41;</span><br />
&nbsp;</div>
<p>Previously, a student of Ron Umble has done an implementation of the same task in C++. This cannot, according to Umble, handle anything larger than n=5. This Haskell implementation, while being surprisingly short and readable, has on my (64 bit, modern, high memory) workstation been able to meet the following execution parameters:</p>
<table border="1" width="75%" halign="center">
<tr>
<th width="10%">n</th>
<th>number of terms</th>
<th>execution time</th>
<th>memory used</th>
</tr>
<tr>
<td>5</td>
<td>46</td>
<td>0.02 s</td>
<td>4M</td>
</tr>
<tr>
<td>6</td>
<td>124</td>
<td>0.33 s</td>
<td>62M</td>
</tr>
<tr>
<td>7</td>
<td>335</td>
<td>8.77 s</td>
<td>1.9G</td>
</tr>
<tr>
<td>8</td>
<td>911</td>
<td>1020 s</td>
<td>441G</td>
</tr>
</table>
]]></content:encoded>
			<wfw:commentRss>http://blog.mikael.johanssons.org/archive/2007/06/enumerating-the-saneblidze-umble-diagonal-in-haskell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>looksay &#8211; today&#8217;s Haskell snippet</title>
		<link>http://blog.mikael.johanssons.org/archive/2007/04/looksay-todays-haskell-snippet/</link>
		<comments>http://blog.mikael.johanssons.org/archive/2007/04/looksay-todays-haskell-snippet/#comments</comments>
		<pubDate>Wed, 18 Apr 2007 08:34:17 +0000</pubDate>
		<dc:creator>Michi</dc:creator>
				<category><![CDATA[Combinatorics]]></category>
		<category><![CDATA[Computer]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Sillyness]]></category>

		<guid isPermaLink="false">http://blog.mikael.johanssons.org/archive/2007/04/looksay-todays-haskell-snippet/</guid>
		<description><![CDATA[nextLookSay = foldr &#40;\xs -&#62; &#40;&#91;length xs, head xs&#93;++&#41;&#41; &#91;&#93; . group lookSay = iterate nextLookSay &#91;1&#93; &#160; Conway&#8217;s Look-and-say sequence]]></description>
			<content:encoded><![CDATA[<div class="dean_ch" style="white-space: wrap;">
nextLookSay = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldr"><span class="kw3">foldr</span></a> <span class="br0">&#40;</span>\xs -&gt; <span class="br0">&#40;</span><span class="br0">&#91;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:length"><span class="kw3">length</span></a> xs, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:head"><span class="kw3">head</span></a> xs<span class="br0">&#93;</span>++<span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#91;</span><span class="br0">&#93;</span> . group<br />
lookSay = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:iterate"><span class="kw3">iterate</span></a> nextLookSay <span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><br />
&nbsp;</div>
<p><a href=http://en.wikipedia.org/wiki/Look_and_say_sequence>Conway&#8217;s Look-and-say sequence</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mikael.johanssons.org/archive/2007/04/looksay-todays-haskell-snippet/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>More silly random text</title>
		<link>http://blog.mikael.johanssons.org/archive/2007/02/more-silly-random-text/</link>
		<comments>http://blog.mikael.johanssons.org/archive/2007/02/more-silly-random-text/#comments</comments>
		<pubDate>Fri, 09 Feb 2007 16:34:11 +0000</pubDate>
		<dc:creator>Michi</dc:creator>
				<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Sillyness]]></category>

		<guid isPermaLink="false">http://blog.mikael.johanssons.org/archive/2007/02/more-silly-random-text/</guid>
		<description><![CDATA[Syntaxfree writes over at his blog about a silly little toy he wrote, using the PFP library, to generate random text. Now, his text is unreadable. I mean, it&#8217;s even unpronounceable. Why? Because he&#8217;s looking at bigram distributions of letter. Great, I thought, I&#8217;ll do him one better. Random text using bigram distributions on words [...]]]></description>
			<content:encoded><![CDATA[<p>Syntaxfree writes over at his blog about a silly little toy he wrote, using the <a href=http://web.engr.oregonstate.edu/~erwig/pfp/>PFP library</a>, to <a href=http://syntaxfree.wordpress.com/2007/02/09/some-probabilistic-text-generation/>generate random text</a>.</p>
<p>Now, his text is unreadable. I mean, it&#8217;s even unpronounceable. Why? Because he&#8217;s looking at bigram distributions of <i>letter</i>.</p>
<p>Great, I thought, I&#8217;ll do him one better. Random text using bigram distributions on words must surely be a LOT better than random text using bigram distributions on letters. At least the words come out readable, and they may even come out in a decent order. </p>
<p>So I sat down with his code, and hacked, tweaked, and monadized it to this</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">module</span> Test <span class="kw1">where</span></p>
<p><span class="kw1">import</span> Probability<br />
<span class="kw1">import</span> Data.<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Char"><span class="kw4">Char</span></a><br />
<span class="kw1">import</span> Control.<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Monad"><span class="kw4">Monad</span></a></p>
<p>filename=<span class="st0">&quot;kjv.genesis&quot;</span></p>
<p>bigram t = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zip"><span class="kw3">zip</span></a> ws <span class="br0">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:tail"><span class="kw3">tail</span></a> ws<span class="br0">&#41;</span> <span class="kw1">where</span><br />
&nbsp; ws = <span class="br0">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:words"><span class="kw3">words</span></a> . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map"><span class="kw3">map</span></a> toLower . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:filter"><span class="kw3">filter</span></a> <span class="br0">&#40;</span>\x -&gt; isAlpha x || isSpace x<span class="br0">&#41;</span><span class="br0">&#41;</span> t</p>
<p>distro = uniform . bigram</p>
<p>goal = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:readFile"><span class="kw3">readFile</span></a> filename</p>
<p>goalD = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span class="kw3">fmap</span></a> distro goal</p>
<p>one = <span class="kw1">do</span><br />
&nbsp; gD &lt;- goalD<br />
&nbsp; <span class="br0">&#40;</span>a,b<span class="br0">&#41;</span> &lt;- pick gD<br />
&nbsp; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:return"><span class="kw3">return</span></a> <span class="br0">&#91;</span>a,b<span class="br0">&#93;</span></p>
<p>many n = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span class="kw3">fmap</span></a> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:unwords"><span class="kw3">unwords</span></a> $ <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span class="kw3">fmap</span></a> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:concat"><span class="kw3">concat</span></a> $ <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sequence"><span class="kw3">sequence</span></a> $ <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:take"><span class="kw3">take</span></a> n $ <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:repeat"><span class="kw3">repeat</span></a> one<br />
&nbsp;</div>
<p>So, I have some corpus &#8212; I just pulled the King James Genesis to have some sort of body of text to work it, and saved it into the <i>kjv.genesis</i> file. Then, I can pop over to my beloved GHCi and execute</p>
<div class="dean_ch" style="white-space: wrap;">
Prelude&gt; :l Test<br />
<span class="br0">&#91;</span><span class="nu0">1</span> <span class="kw1">of</span> <span class="nu0">4</span><span class="br0">&#93;</span> Compiling ListUtils &nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#40;</span> ListUtils.hs, interpreted <span class="br0">&#41;</span><br />
<span class="br0">&#91;</span><span class="nu0">2</span> <span class="kw1">of</span> <span class="nu0">4</span><span class="br0">&#93;</span> Compiling <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Show"><span class="kw4">Show</span></a> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#40;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Show"><span class="kw4">Show</span></a>.hs, interpreted <span class="br0">&#41;</span><br />
<span class="br0">&#91;</span><span class="nu0">3</span> <span class="kw1">of</span> <span class="nu0">4</span><span class="br0">&#93;</span> Compiling Probability &nbsp; &nbsp; &nbsp;<span class="br0">&#40;</span> Probability.hs, interpreted <span class="br0">&#41;</span><br />
<span class="br0">&#91;</span><span class="nu0">4</span> <span class="kw1">of</span> <span class="nu0">4</span><span class="br0">&#93;</span> Compiling Test &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#40;</span> Test.hs, interpreted <span class="br0">&#41;</span><br />
Ok, modules loaded: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Show"><span class="kw4">Show</span></a>, Test, Probability, ListUtils.<br />
*Test&gt; many <span class="nu0">30</span><br />
Loading package haskell98 &#8230; linking &#8230; done.<br />
<span class="st0">&quot;house and it to circumcised all cool of these things daughters from ye go dreamed for stead and in unto god of for wives done to i give god shall bowed himself and shem tower of small and said lord he was called his the days these are thither therefore cainan and and he rachel and hear my sons born&quot;</span><br />
&nbsp;</div>
<p>The first execution will take a while, since it has to, y&#8217;know, digest the actual text, calculate distributions, and set everything up.</p>
<p>Subsequent executions also take quite some time, and I&#8217;m not at all certain why. An explanation would be nice, if someone has it.</p>
<p>And for some sample poetry, I give you</p>
<blockquote><p>
house and it to circumcised all cool of these things daughters from ye go dreamed for stead and in unto god of for wives done to i give god shall bowed himself and shem tower of small and said lord he was called his the days these are thither therefore cainan and and he rachel and hear my sons born</p>
<p>when isaac dry land unto him isaac and have i children struggled lord hath had eaten goods which morning was ye shall by the all the me this naphtali and years old was her to see of the his brother out of names after they feed thy mothers of ephron god said he put him into after his the tent</p>
<p>unto us i will to him i will the presence hands to his right name was possession of days journey down in that thou perizzites and and he they for god made that is of the twentys sake jacob so itself after thou and of anah years and in isaac pillar of and the builded a of canaan noah and
</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://blog.mikael.johanssons.org/archive/2007/02/more-silly-random-text/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Retrospection 2006</title>
		<link>http://blog.mikael.johanssons.org/archive/2006/12/retrospection-2006/</link>
		<comments>http://blog.mikael.johanssons.org/archive/2006/12/retrospection-2006/#comments</comments>
		<pubDate>Sat, 30 Dec 2006 18:31:57 +0000</pubDate>
		<dc:creator>Michi</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[PhD]]></category>
		<category><![CDATA[Politics]]></category>
		<category><![CDATA[Privacy]]></category>
		<category><![CDATA[Research]]></category>

		<guid isPermaLink="false">http://blog.mikael.johanssons.org/archive/2006/12/retrospection-2006/</guid>
		<description><![CDATA[Inspired by other bloggers on Planet Haskell, I thought I&#8217;d just sit down and write a retrospection post, reviewing the past year &#8211; primarily from angles such as mathematics, computers and my generic life situation. It divides neatly into two different sections: the months as a commercial programmer and the months as PhD student and [...]]]></description>
			<content:encoded><![CDATA[<p>Inspired by other bloggers on <a href="http://planet.haskell.org">Planet Haskell</a>, I thought I&#8217;d just sit down and write a retrospection post, reviewing the past year &#8211; primarily from angles such as mathematics, computers and my generic life situation.</p>
<p>It divides neatly into two different sections: the months as a commercial programmer and the months as PhD student and academic careerist. </p>
<p>The year began still working for Teleca Systems, and with security consulting for Stockholm-based firms and frequent trips back home.</p>
<p>Then as the year went on and my PhD applications grew more and more, I started getting results. I got invited to Bonn for an interview with the Homology and Homotopy graduate school program &#8211; which was in the end turned down because I was more of a homological algebraist than a topologist. And the week after that, I was invited to Jena for an interview for a position doing PhD work on computational homological algebra. The interview went well, the potential advisor was nice (and a once-roleplaying gamer to sweeten the deal more) and I got the position just a few days later.</p>
<p>While in Switzerland.</p>
<p>Skiing.</p>
<p>On a weeklong luxurious conference in operad theory at a skiing resort in the western part of the swiss alps. Lots of skiing, fantastic food, lots of REALLY COOL people (I&#8217;m looking at you, pozorvlak, among others) and lots of fascinating mathematics.</p>
<p>As I returned there, it was time to wrap up my position with Teleca, disengage from all my duties, document everything I had done, train everyone else in my stuff, and get out of there. Preferably before the summer term started. Oh, and get through the bureaucracy involved in getting hired at a German university. It&#8217;s a fascinating experience, I can tell you. Especially when you get asked whether you were involved with the Stasi during communist east Germany.</p>
<p>Freshly hired, I had a week to get my affairs in order, and then got thrown on my first bunch of students. Teaching and reading up on cooooool mathematics for several months, leaving a little bit for visits to Sweden, and then hitting the vacation hard with a long relaxing visit to Sweden, and a marriage proposal.</p>
<p>The summer also saw the Pirate Bay raid, my heightened interest in the politics of privacy, and membership in the Pirate Party, the election, and following the election results explicitly with attention to the Pirate Party results.</p>
<p>After the vacation came conference summer, stretching August through September, with between 2 and 4 conferences depending on how you count them, and a little bit of research. At the end of this, a new term started and with that a new direction for my PhD research. This was, at the same time, the period when I got enamoured with Haskell, read up on it, grew more and more interested, fascinated, captivated.</p>
<p>During the time in Jena, I got involved in a roleplaying troupe, in a mathematical magazine (with two articles published by now!), in a group organizing math camps (participating in one camp), and getting hold, slowly, of a decently sized social circle.</p>
<p>And then the end of the year hit before I knew what happened. It ends tomorrow &#8211; and I have things hanging in idle loops swarming my head like angry bees and waiting to be channeled through my consciousness out on paper and code, and get me running with my research.</p>
<p>That&#8217;s my year in review. How was your?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mikael.johanssons.org/archive/2006/12/retrospection-2006/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Prototyping thought</title>
		<link>http://blog.mikael.johanssons.org/archive/2006/10/prototyping-thought/</link>
		<comments>http://blog.mikael.johanssons.org/archive/2006/10/prototyping-thought/#comments</comments>
		<pubDate>Sat, 28 Oct 2006 20:43:01 +0000</pubDate>
		<dc:creator>Michi</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Research]]></category>

		<guid isPermaLink="false">http://blog.mikael.johanssons.org/archive/2006/10/prototyping-thought/</guid>
		<description><![CDATA[In a recent post, pozorvlak reminded me of one of the reason it is important to have a good, obvious, and quick-to-write programming language around. He, as I, is a mathematician, spending his time thinking, finding patterns, and trying to formulate (more or less) absolute proof that his patterns hold all the time, alternatively ways [...]]]></description>
			<content:encoded><![CDATA[<p>In a recent <a href=http://pozorvlak.livejournal.com/30994.html>post</a>, <a href=http://pozorvlak.livejournal.com>pozorvlak</a> reminded me of one of the reason it is important to have a good, obvious, and quick-to-write programming language around.</p>
<p>He, as I, is a mathematician, spending his time thinking, finding patterns, and trying to formulate (more or less) absolute proof that his patterns hold all the time, alternatively ways to demonstrate that they may not be universal.</p>
<p>In the post linked above, he starts by a neat little exercise, gets interested, and goes out to look at more examples. These show a very clear pattern, and after following this pattern quite some way out, he finally believes the pattern enough to start searching for a proof: which he also finds.</p>
<p>Now, this is one central heuristic pattern in mathematics. Calculate examples. Look at the examples. All of a sudden a pattern appears, and you think up a reason why such a pattern could be there. While proving the &#8220;correctness&#8221;, if you may, of the pattern, you&#8217;ll either end up formulating in enough detail what&#8217;s going on so that you may possibly expand the statement to cover even more &#8212; or you may realize that it does not hold and find a way to construct a counterexample. Either way, the mass of knowledge increases.</p>
<p>Now, what does this have to do with programming languages?</p>
<p>I started looking at Haskell due to a sequence of blogposts from <a href=http://sigfpe.blogspot.com>sigfpe</a>, who was describing Haskell programming from a highly algebraic point of view. And even after my involving myself in the subject, one thing that stands out is the relatively low distance between thought expressed in my ordinary day-to-day mathematical discourse, and thought expressed in Haskell code. For the ubiquitous example, let&#8217;s take the factorial function. Mathematically, I think about factorials as<br />
&#8220;n! is the product of all integers from 1 to n&#8221;<br />
or<br />
&#8220;n!=&Gamma;(n-1)&#8221; (though this not so often, I must admit)</p>
<p>Then again, &#8220;the product of all&#8221;, when I introduce it strictly for the first time tends to be expressed along the lines of<br />
&#8220;&Pi;<sub>n=1</sub><sup>k</sup> f(n) is f(k)*&Pi;<sub>n=1</sub><sup>k-1</sup>&#8221; or something along those lines.</p>
<p>So, what happens if I want to start playing around a bit with factorials, and see whether I can say anything interesting about them? </p>
<p>In C or similar languages, I&#8217;ll fire up an editor, and a command shell, and start writing code that looks something like</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="co2">#include &lt;stdio.h&gt;</span><br />
<span class="kw4">int</span> main<span class="br0">&#40;</span><span class="kw4">int</span> argc, <span class="kw4">void</span>* argv<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp;<span class="kw4">int</span> i, j, f=<span class="nu0">1</span>;<br />
&nbsp;scanf<span class="br0">&#40;</span><span class="st0">&quot;%d&quot;</span>, &amp;i<span class="br0">&#41;</span>;<br />
&nbsp;<span class="kw1">for</span><span class="br0">&#40;</span>j=<span class="nu0">1</span>; j&lt;i; j++<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; f=f*j;<br />
&nbsp;<span class="br0">&#125;</span><br />
&nbsp;<a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span class="kw3">printf</span></a><span class="br0">&#40;</span><span class="st0">&quot;%d&quot;</span>,f<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div>
<p>and I will spend more time writing scaffolding and reworking the problem into something I can write in C than I will actually working with my mathematical intuition about it.</p>
<p>C++, Java, PHP, Perl &#8212; it won&#8217;t be much different in any of these. None of them have an interpreter I like, none of them differ all too much from the mindset of C. (Yes, I say this knowing full well the difference between imperative and object-oriented programming&#8230;)</p>
<p>In functional languages, the way I write the code is very much more reminiscent of the way I define it in my head. I could dig out enough Lisp or Scheme from the back of my mind to give you an example, but it&#8217;s getting late and I couldn&#8217;t be bothered. So instead I&#8217;ll go straight on to the thingie I want to praise.</p>
<p>In Haskell, I&#8217;ll fire up my interpreter, and write at the prompt something very much like</p>
<div class="dean_ch" style="white-space: wrap;">
&gt; <span class="kw1">let</span> fac <span class="nu0">0</span> = <span class="nu0">1</span>; fac n = n*fac <span class="br0">&#40;</span>n<span class="nu0">-1</span><span class="br0">&#41;</span><br />
&nbsp;</div>
<p>and then start firing off actual calculations into the box. </p>
<div class="dean_ch" style="white-space: wrap;">
&gt; <span class="br0">&#40;</span>fac <span class="nu0">8</span><span class="br0">&#41;</span>/<span class="br0">&#40;</span><span class="br0">&#40;</span>fac <span class="nu0">3</span><span class="br0">&#41;</span>*<span class="br0">&#40;</span>fac <span class="nu0">5</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
<span class="nu0">56.0</span><br />
&nbsp;</div>
<p>During my time so far, I&#8217;ve used Haskell quite a bit in my research to generate lists of testcases. Not necessarily because Haskell is the best possible language for it, but because I could very quickly write down my thoughts, get working code out of it, and just go ahead and do what I wanted. Most languages I&#8217;ve worked with have their uses &#8211; and I&#8217;m not out to bash anything right now. But with Haskell, I&#8217;ve had a much lower threshold thought-to-code, and much less distance between my coder head and my mathematician head, than with anything else I&#8217;ve seen so far.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mikael.johanssons.org/archive/2006/10/prototyping-thought/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Computational Group Theory in Haskell (1 in a series)</title>
		<link>http://blog.mikael.johanssons.org/archive/2006/10/computational-group-theory-in-haskell-1-in-a-series/</link>
		<comments>http://blog.mikael.johanssons.org/archive/2006/10/computational-group-theory-in-haskell-1-in-a-series/#comments</comments>
		<pubDate>Wed, 18 Oct 2006 14:37:24 +0000</pubDate>
		<dc:creator>Michi</dc:creator>
				<category><![CDATA[Algebra]]></category>
		<category><![CDATA[English]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.mikael.johanssons.org/archive/2006/10/computational-group-theory-in-haskell-1-in-a-series/</guid>
		<description><![CDATA[This term, I&#8217;m listening to a lecture course on Computational Group Theory. As a good exercise, I plan to implement everything we talk about as Haskell functions as well. The first lecture was mainly an introduction to the area, ending with a very naïve algorithm to generate a permutation group from a set of generators. [...]]]></description>
			<content:encoded><![CDATA[<p>This term, I&#8217;m listening to a lecture course on Computational Group Theory. As a good exercise, I plan to implement everything we talk about as Haskell functions as well.</p>
<p>The first lecture was mainly an introduction to the area, ending with a very naïve algorithm to generate a permutation group from a set of generators. Next week will bring less naïve algorithms with not quite as horrible complexity.</p>
<p>Before the algorithm can be brought, we&#8217;d want some undergrowth: we&#8217;d want to be able to work with permutations at all. So, we&#8217;ll start with the basic group theory and permutation implementations. A lot of this is stolen or rewritten from <a href=http://www.polyomino.f2s.com/david/haskell/codeindex.html>this permutation group code</a>.</p>
<p>Our code will make use of two libraries, so if you collate code snippets while reading this, you&#8217;ll want to use </p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">import</span> Array<br />
<span class="kw1">import</span> List<br />
&nbsp;</div>
<p>If you don&#8217;t want to bother with that, the code is available <a href=http://www.mikael.johanssons.org/haskell/CGT/PermutationGroups_1.hs>here</a>.</p>
<p>One of the things I love the most with Haskell is the way that you just write down what you think it means, and end up with good code. So, what is a group? It is this type class:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">class</span> Group a <span class="kw1">where</span><br />
&nbsp; identity :: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span class="kw4">Int</span></a> -&gt; a<br />
&nbsp; isIdentity :: a -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Bool"><span class="kw4">Bool</span></a><br />
&nbsp; <span class="br0">&#40;</span>*&gt;<span class="br0">&#41;</span> :: a -&gt; a -&gt; a &nbsp; &nbsp; <span class="co1">&#8211; right multiplication</span><br />
&nbsp; inverse :: a -&gt; a<br />
&nbsp;</div>
<p>with some conditions on the functions given. This is geared towards implementing permutation groups in the first line &#8212; so the idea is that you give your group as a subgroup of S<sub>n</sub>, and you can retrieve the identity in S<sub>n</sub> by using the identity function.</p>
<p>We tend to want to be able to conjugate elements and multiply a whole list of elements, so let&#8217;s add a few functions:</p>
<div class="dean_ch" style="white-space: wrap;">
conjugate g h = inverse h *&gt; g *&gt; h<br />
prod gs = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl1"><span class="kw3">foldl1</span></a> <span class="br0">&#40;</span>*&gt;<span class="br0">&#41;</span> gs &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp;</div>
<p>And now we can start looking at permutations. We will store a permutation as the list of images, in order. So for instance, the permutation given by (1 2)(1 3) will be stored as the list [2,3,1]. (note that in the good tradition of computational group theory, we act from the right &#8211; thus motivating this result and not [2,1,3])</p>
<p>The interesting bit about permutations, though, is that they form a group; so we instantiate our type class as well.</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">newtype</span> Permutation = PL <span class="br0">&#91;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span class="kw4">Int</span></a><span class="br0">&#93;</span> <span class="kw1">deriving</span> <span class="br0">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Eq"><span class="kw4">Eq</span></a>, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Ord"><span class="kw4">Ord</span></a>, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Read"><span class="kw4">Read</span></a>, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Show"><span class="kw4">Show</span></a><span class="br0">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
x .^ <span class="br0">&#40;</span>PL g<span class="br0">&#41;</span> = g !! <span class="br0">&#40;</span>x<span class="nu0">-1</span><span class="br0">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
<span class="kw1">instance</span> Group Permutation <span class="kw1">where</span><br />
&nbsp; identity n = PL <span class="br0">&#91;</span><span class="nu0">1</span>..n<span class="br0">&#93;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; isIdentity <span class="br0">&#40;</span>PL xs<span class="br0">&#41;</span> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:all"><span class="kw3">all</span></a> <span class="br0">&#40;</span>\<span class="br0">&#40;</span>i,j<span class="br0">&#41;</span> -&gt; i==j<span class="br0">&#41;</span> <span class="br0">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zip"><span class="kw3">zip</span></a> <span class="br0">&#91;</span><span class="nu0">1</span>..<span class="br0">&#93;</span> xs<span class="br0">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; <span class="br0">&#40;</span>PL g<span class="br0">&#41;</span> *&gt; h = PL <span class="br0">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map"><span class="kw3">map</span></a> <span class="br0">&#40;</span>.^ h<span class="br0">&#41;</span> g<span class="br0">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; inverse <span class="br0">&#40;</span>PL g<span class="br0">&#41;</span> = PL <span class="br0">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map"><span class="kw3">map</span></a> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:snd"><span class="kw3">snd</span></a> <span class="br0">&#40;</span>sort <span class="br0">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zip"><span class="kw3">zip</span></a> g <span class="br0">&#91;</span><span class="nu0">1</span>..<span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp;</div>
<p>We are used to being able to write permutations in cycle notation, so we&#8217;d want to be able to convert to and from that, too.</p>
<div class="dean_ch" style="white-space: wrap;">
fromCycles cs = PL <span class="br0">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map"><span class="kw3">map</span></a> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:snd"><span class="kw3">snd</span></a> <span class="br0">&#40;</span>sort pointActions<span class="br0">&#41;</span><span class="br0">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; <span class="kw1">where</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; pointActions = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:concat"><span class="kw3">concat</span></a> <span class="br0">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map"><span class="kw3">map</span></a> fromCycle cs<span class="br0">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fromCycle is = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zip"><span class="kw3">zip</span></a> is <span class="br0">&#40;</span>rotateL is<span class="br0">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fromCycles&#8217; n cs = PL <span class="br0">&#40;</span>elems <span class="br0">&#40;</span>array <span class="br0">&#40;</span><span class="nu0">1</span>,n<span class="br0">&#41;</span> <span class="br0">&#91;</span><span class="br0">&#40;</span>i,i<span class="br0">&#41;</span> | i &lt;- <span class="br0">&#91;</span><span class="nu0">1</span>..n<span class="br0">&#93;</span><span class="br0">&#93;</span> // <span class="br0">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:concat"><span class="kw3">concat</span></a> <span class="br0">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map"><span class="kw3">map</span></a> fromCycle cs<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
findOrbit <span class="br0">&#40;</span>PL permutation<span class="br0">&#41;</span> elt = &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; <span class="kw1">let</span> buildOrbit os ps e = &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:elem"><span class="kw3">elem</span></a> e os<span class="br0">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; <span class="kw1">then</span> os &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; <span class="kw1">else</span> <span class="kw1">let</span> ei = elemIndex e ps <span class="kw1">in</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">case</span> ei <span class="kw1">of</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Nothing -&gt; os &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Just e&#8217; -&gt; buildOrbit <span class="br0">&#40;</span><span class="br0">&#91;</span>e<span class="br0">&#93;</span> ++ os<span class="br0">&#41;</span> ps <span class="br0">&#40;</span>e&#8217;<span class="nu0">+1</span><span class="br0">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; <span class="kw1">in</span> buildOrbit <span class="br0">&#91;</span><span class="br0">&#93;</span> permutation elt &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;toCycles <span class="br0">&#40;</span>PL permutation<span class="br0">&#41;</span> = &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; <span class="kw1">let</span><br />
&nbsp; &nbsp; getOrbits out <span class="br0">&#91;</span><span class="br0">&#93;</span> per = out &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; getOrbits out els per = &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; <span class="kw1">let</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; orbit = findOrbit per <span class="br0">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:head"><span class="kw3">head</span></a> els<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; els&#8217; = els \\ orbit &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; <span class="kw1">in</span> getOrbits <span class="br0">&#40;</span>out ++ <span class="br0">&#91;</span>orbit<span class="br0">&#93;</span><span class="br0">&#41;</span> els&#8217; per<br />
&nbsp; <span class="kw1">in</span> getOrbits <span class="br0">&#91;</span><span class="br0">&#93;</span> permutation <span class="br0">&#40;</span>PL permutation<span class="br0">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
rotateL <span class="br0">&#40;</span>x:xs<span class="br0">&#41;</span> = xs ++ <span class="br0">&#91;</span>x<span class="br0">&#93;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
saturateCycles :: <span class="br0">&#91;</span><span class="br0">&#91;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span class="kw4">Int</span></a><span class="br0">&#93;</span><span class="br0">&#93;</span> -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span class="kw4">Int</span></a> -&gt; <span class="br0">&#91;</span><span class="br0">&#91;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span class="kw4">Int</span></a><span class="br0">&#93;</span><span class="br0">&#93;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; saturateCycles cs n = cs ++ <span class="br0">&#40;</span> group <span class="br0">&#40;</span> <span class="br0">&#91;</span><span class="nu0">1</span>..n<span class="br0">&#93;</span> \\ <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl1"><span class="kw3">foldl1</span></a> <span class="br0">&#40;</span>++<span class="br0">&#41;</span> cs <span class="br0">&#41;</span> <span class="br0">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
desaturateCycles cs = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:filter"><span class="kw3">filter</span></a> <span class="br0">&#40;</span>\x -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:length"><span class="kw3">length</span></a> x &gt; <span class="nu0">1</span><span class="br0">&#41;</span> cs<br />
&nbsp;</div>
<p>We can use saturateCycles and desaturateCycles in order to convert to and from the normal way of writing permutations. The toCycles and fromCycles functions both expect fixpoints to be listed as well. We need, however, to tell the saturateCycles which symmetric group we want to live in.</p>
<p>Now, the most naïve algorithm to list all elements in a group starts with the one element we know will live in the group: the identity. And for each iteration, all generators are multiplied onto the first known candidate in a list of group element candidates, and all results that are not yet known are added to this list.</p>
<p>In Haskell code:</p>
<div class="dean_ch" style="white-space: wrap;">
generateGroup generators =<br />
&nbsp; <span class="kw1">let</span> g0 = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:head"><span class="kw3">head</span></a> generators<br />
&nbsp; &nbsp; &nbsp; i = g0 *&gt; inverse g0<br />
&nbsp; &nbsp; &nbsp; addElts group <span class="br0">&#91;</span><span class="br0">&#93;</span> gens = group<br />
&nbsp; &nbsp; &nbsp; addElts group cands gens =<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">let</span> cand = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:take"><span class="kw3">take</span></a> <span class="nu0">1</span> cands<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; group&#8217; = group ++ cand<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newcands = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map"><span class="kw3">map</span></a> <span class="br0">&#40;</span>*&gt; <span class="br0">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:head"><span class="kw3">head</span></a> cand<span class="br0">&#41;</span><span class="br0">&#41;</span> gens<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cands&#8217; = <span class="br0">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:tail"><span class="kw3">tail</span></a> cands<span class="br0">&#41;</span> ++ <span class="br0">&#40;</span>newcands \\ <span class="br0">&#40;</span>group&#8217; ++ cands<span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">in</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; addElts group&#8217; cands&#8217; gens<br />
&nbsp; <span class="kw1">in</span> addElts <span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="br0">&#91;</span>i<span class="br0">&#93;</span> generators<br />
&nbsp;</div>
<p>We keep on adding tested candidates to the first list, and anything not yet seen that we get from our multiplication with generators gets added to the middle list. Once the middle list is empty, we&#8217;re done.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mikael.johanssons.org/archive/2006/10/computational-group-theory-in-haskell-1-in-a-series/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>OpenGL programming in Haskell, a tutorial (Part 2)</title>
		<link>http://blog.mikael.johanssons.org/archive/2006/09/opengl-programming-in-haskell-a-tutorial-part-2/</link>
		<comments>http://blog.mikael.johanssons.org/archive/2006/09/opengl-programming-in-haskell-a-tutorial-part-2/#comments</comments>
		<pubDate>Tue, 19 Sep 2006 16:55:29 +0000</pubDate>
		<dc:creator>Michi</dc:creator>
				<category><![CDATA[Computer]]></category>
		<category><![CDATA[English]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.mikael.johanssons.org/archive/2006/09/opengl-programming-in-haskell-a-tutorial-part-2/</guid>
		<description><![CDATA[As we left off the last installment, we were just about capable to open up a window, and draw some basic things in it by giving coordinate lists to the command renderPrimitive. The programs we built suffered under a couple of very infringing and ugly restraints when we wrote them &#8211; for one, they weren&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>As we left off the <a href=http://blog.mikael.johanssons.org/archive/2006/09/opengl-programming-in-haskell-a-tutorial-part-1/>last installment</a>, we were just about capable to open up a window, and draw some basic things in it by giving coordinate lists to the command renderPrimitive. The programs we built suffered under a couple of very infringing and ugly restraints when we wrote them &#8211; for one, they weren&#8217;t really very modularized. The code would have been much clearer had we farmed out important subtasks on other modules. For another, we never even considered the fact that some manipulations would not necessarily be good to do on the entire picture.</p>
<h2>Some modules</h2>
<p>To deal with the first problem, let&#8217;s break apart our program a little bit, forming several more or less independent code files linked together to form a whole.</p>
<p>First off, HelloWorld.hs &#8211; containing a very generic program skeleton. We will use our module Bindings to setup everything else we might need, and tie them to the callbacks.</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">import</span> Graphics.Rendering.OpenGL<br />
<span class="kw1">import</span> Graphics.UI.GLUT<br />
<span class="kw1">import</span> Bindings</p>
<p>main = <span class="kw1">do</span><br />
&nbsp; <span class="br0">&#40;</span>progname,_<span class="br0">&#41;</span> &lt;- getArgsAndInitialize<br />
&nbsp; createWindow <span class="st0">&quot;Hello World&quot;</span><br />
&nbsp; displayCallback $= display<br />
&nbsp; reshapeCallback $= Just reshape<br />
&nbsp; keyboardMouseCallback $= Just keyboardMouse<br />
&nbsp; mainLoop<br />
&nbsp;</div>
<p>Then Bindings.hs &#8211; our switchboard</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">module</span> Bindings <span class="br0">&#40;</span>display,reshape,keyboardMouse<span class="br0">&#41;</span> <span class="kw1">where</span><br />
<span class="kw1">import</span> Graphics.Rendering.OpenGL<br />
<span class="kw1">import</span> Graphics.UI.GLUT</p>
<p><span class="kw1">import</span> Display</p>
<p>reshape s@<span class="br0">&#40;</span>Size w h<span class="br0">&#41;</span> = <span class="kw1">do</span> <br />
&nbsp; viewport $= <span class="br0">&#40;</span>Position <span class="nu0">0</span> <span class="nu0">0</span>, s<span class="br0">&#41;</span></p>
<p>keyboardMouse key state modifiers position = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:return"><span class="kw3">return</span></a> <span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp;</div>
<p>We&#8217;re going to be hacking around a LOT with the display function, so let&#8217;s isolate that one to a module of it&#8217;s own: Display.hs</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">module</span> Display <span class="br0">&#40;</span>display<span class="br0">&#41;</span> <span class="kw1">where</span><br />
<span class="kw1">import</span> Graphics.Rendering.OpenGL<br />
<span class="kw1">import</span> Graphics.UI.GLUT<br />
<span class="kw1">import</span> Cube</p>
<p>display = <span class="kw1">do</span> <br />
&nbsp; clear <span class="br0">&#91;</span>ColorBuffer<span class="br0">&#93;</span><br />
&nbsp; cube <span class="br0">&#40;</span><span class="nu0">0.2</span>::GLfloat<span class="br0">&#41;</span><br />
&nbsp; flush<br />
&nbsp;</div>
<p>And a first utility module, containing the gritty details of drawing the cube <img src='/latexrender/pictures/4c46e7c3df967cf41b16a8fa86813529.png' title='[-w,w]^3' alt='[-w,w]^3' align='middle' />, called Cube.hs</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">module</span> Cube <span class="kw1">where</span><br />
<span class="kw1">import</span> Graphics.Rendering.OpenGL<br />
<span class="kw1">import</span> Graphics.UI.GLUT</p>
<p>cube w = <span class="kw1">do</span> <br />
&nbsp; renderPrimitive Quads $ <span class="kw1">do</span><br />
&nbsp; &nbsp; vertex $ Vertex3 w w w<br />
&nbsp; &nbsp; vertex $ Vertex3 w w <span class="br0">&#40;</span>-w<span class="br0">&#41;</span><br />
&nbsp; &nbsp; vertex $ Vertex3 w <span class="br0">&#40;</span>-w<span class="br0">&#41;</span> <span class="br0">&#40;</span>-w<span class="br0">&#41;</span><br />
&nbsp; &nbsp; vertex $ Vertex3 w <span class="br0">&#40;</span>-w<span class="br0">&#41;</span> w</p>
<p>&nbsp; &nbsp; vertex $ Vertex3 w w w<br />
&nbsp; &nbsp; vertex $ Vertex3 w w <span class="br0">&#40;</span>-w<span class="br0">&#41;</span><br />
&nbsp; &nbsp; vertex $ Vertex3 <span class="br0">&#40;</span>-w<span class="br0">&#41;</span> w <span class="br0">&#40;</span>-w<span class="br0">&#41;</span><br />
&nbsp; &nbsp; vertex $ Vertex3 <span class="br0">&#40;</span>-w<span class="br0">&#41;</span> w w</p>
<p>&nbsp; &nbsp; vertex $ Vertex3 w w w<br />
&nbsp; &nbsp; vertex $ Vertex3 w <span class="br0">&#40;</span>-w<span class="br0">&#41;</span> w<br />
&nbsp; &nbsp; vertex $ Vertex3 <span class="br0">&#40;</span>-w<span class="br0">&#41;</span> <span class="br0">&#40;</span>-w<span class="br0">&#41;</span> w<br />
&nbsp; &nbsp; vertex $ Vertex3 <span class="br0">&#40;</span>-w<span class="br0">&#41;</span> w w</p>
<p>&nbsp; &nbsp; vertex $ Vertex3 <span class="br0">&#40;</span>-w<span class="br0">&#41;</span> w w<br />
&nbsp; &nbsp; vertex $ Vertex3 <span class="br0">&#40;</span>-w<span class="br0">&#41;</span> w <span class="br0">&#40;</span>-w<span class="br0">&#41;</span><br />
&nbsp; &nbsp; vertex $ Vertex3 <span class="br0">&#40;</span>-w<span class="br0">&#41;</span> <span class="br0">&#40;</span>-w<span class="br0">&#41;</span> <span class="br0">&#40;</span>-w<span class="br0">&#41;</span><br />
&nbsp; &nbsp; vertex $ Vertex3 <span class="br0">&#40;</span>-w<span class="br0">&#41;</span> <span class="br0">&#40;</span>-w<span class="br0">&#41;</span> w</p>
<p>&nbsp; &nbsp; vertex $ Vertex3 w <span class="br0">&#40;</span>-w<span class="br0">&#41;</span> w<br />
&nbsp; &nbsp; vertex $ Vertex3 w <span class="br0">&#40;</span>-w<span class="br0">&#41;</span> <span class="br0">&#40;</span>-w<span class="br0">&#41;</span><br />
&nbsp; &nbsp; vertex $ Vertex3 <span class="br0">&#40;</span>-w<span class="br0">&#41;</span> <span class="br0">&#40;</span>-w<span class="br0">&#41;</span> <span class="br0">&#40;</span>-w<span class="br0">&#41;</span><br />
&nbsp; &nbsp; vertex $ Vertex3 <span class="br0">&#40;</span>-w<span class="br0">&#41;</span> <span class="br0">&#40;</span>-w<span class="br0">&#41;</span> w</p>
<p>&nbsp; &nbsp; vertex $ Vertex3 w w <span class="br0">&#40;</span>-w<span class="br0">&#41;</span><br />
&nbsp; &nbsp; vertex $ Vertex3 w <span class="br0">&#40;</span>-w<span class="br0">&#41;</span> <span class="br0">&#40;</span>-w<span class="br0">&#41;</span><br />
&nbsp; &nbsp; vertex $ Vertex3 <span class="br0">&#40;</span>-w<span class="br0">&#41;</span> <span class="br0">&#40;</span>-w<span class="br0">&#41;</span> <span class="br0">&#40;</span>-w<span class="br0">&#41;</span><br />
&nbsp; &nbsp; vertex $ Vertex3 <span class="br0">&#40;</span>-w<span class="br0">&#41;</span> w <span class="br0">&#40;</span>-w<span class="br0">&#41;</span><br />
&nbsp;</div>
<p>Now, compiling this entire section with the command</p>
<div class="dean_ch" style="white-space: wrap;">
ghc <span class="co1">&#8211;make -package GLUT HelloWorld.hs -o HelloWorld</span><br />
&nbsp;</div>
<p>compiles and links each module needed, and produces, in the end, an executable to be used. There we go! Much more modularized, much smaller and simpler bits and pieces. And &#8211; an added boon &#8211; we won&#8217;t normally need to recompile as much for each change we do.</p>
<p>This skeletal program will look like <img src="http://mikael.johanssons.org/images/hopengl/tut2/skeleton.png" />.</p>
<h2>Local transformations</h2>
<p>One of the core reasons I started to write this tutorial series was that I wanted to figure out why <a href=http://www.tfh-berlin.de/~panitz/hopengl/skript.html>Panitz&#8217; tutorial</a> didn&#8217;t work for me. The core explanation is simple &#8211; the names of some of the functions used has changed since he wrote them. Thus, the matrixExcursion in his tutorial is nowadays named preservingMatrix. This may well change further &#8211; though I hope it won&#8217;t &#8211; in which case this tutorial will be painfully out of date as well.</p>
<p>The idea of preservingMatrix, however, is to take a small piece of drawing actions, and perform them independent of the transformations going on outside that small piece. For demonstration, let&#8217;s draw a bunch of cubes, shall we?</p>
<p>We&#8217;ll change the rather boring display subroutine in Display.hs into one using preservingMatrix to modify each cube drawn individually, giving a new Display.hs:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">module</span> Display <span class="br0">&#40;</span>display<span class="br0">&#41;</span> <span class="kw1">where</span></p>
<p><span class="kw1">import</span> Graphics.Rendering.OpenGL<br />
<span class="kw1">import</span> Graphics.UI.GLUT</p>
<p><span class="kw1">import</span> Cube</p>
<p>points :: <span class="br0">&#91;</span><span class="br0">&#40;</span>GLfloat,GLfloat,GLfloat<span class="br0">&#41;</span><span class="br0">&#93;</span><br />
points = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map"><span class="kw3">map</span></a> <span class="br0">&#40;</span>\k -&gt; <span class="br0">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sin"><span class="kw3">sin</span></a><span class="br0">&#40;</span><span class="nu0">2</span>*<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:pi"><span class="kw3">pi</span></a>*k/<span class="nu0">12</span><span class="br0">&#41;</span>,<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:cos"><span class="kw3">cos</span></a><span class="br0">&#40;</span><span class="nu0">2</span>*<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:pi"><span class="kw3">pi</span></a>*k/<span class="nu0">12</span><span class="br0">&#41;</span>,<span class="nu0">0.0</span><span class="br0">&#41;</span><span class="br0">&#41;</span> &nbsp;<span class="br0">&#91;</span><span class="nu0">1</span>..<span class="nu0">12</span><span class="br0">&#93;</span></p>
<p>display = <span class="kw1">do</span> <br />
&nbsp; clear <span class="br0">&#91;</span>ColorBuffer<span class="br0">&#93;</span><br />
&nbsp; <span class="kw1">do</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:mapM_"><span class="kw3">mapM_</span></a> <span class="br0">&#40;</span>\<span class="br0">&#40;</span>x,y,z<span class="br0">&#41;</span> -&gt; preservingMatrix $ <span class="kw1">do</span><br />
&nbsp; &nbsp; color $ Color3 x y z<br />
&nbsp; &nbsp; translate $ Vector3 x y z<br />
&nbsp; &nbsp; cube <span class="br0">&#40;</span><span class="nu0">0.1</span>::GLfloat<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#41;</span> points<br />
&nbsp; flush<br />
&nbsp;</div>
<p>Say&#8230; Those points on the unit circle might be something we&#8217;ll want more of. Let&#8217;s abstract some again! We&#8217;ll break them out to a Points.hs. We&#8217;ll have to juggle a bit with the typesystem to get things to work out, and in the end we get</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">module</span> Points <span class="kw1">where</span><br />
<span class="kw1">import</span> Graphics.Rendering.OpenGL</p>
<p>points :: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span class="kw4">Int</span></a> -&gt; <span class="br0">&#91;</span><span class="br0">&#40;</span>GLfloat,GLfloat,GLfloat<span class="br0">&#41;</span><span class="br0">&#93;</span><br />
points n&#8217; = <span class="kw1">let</span> n = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fromIntegral"><span class="kw3">fromIntegral</span></a> n&#8217; <span class="kw1">in</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map"><span class="kw3">map</span></a> <span class="br0">&#40;</span>\k -&gt; <span class="kw1">let</span> t = <span class="nu0">2</span>*<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:pi"><span class="kw3">pi</span></a>*k/n <span class="kw1">in</span> <span class="br0">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sin"><span class="kw3">sin</span></a><span class="br0">&#40;</span>t<span class="br0">&#41;</span>,<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:cos"><span class="kw3">cos</span></a><span class="br0">&#40;</span>t<span class="br0">&#41;</span>,<span class="nu0">0.0</span><span class="br0">&#41;</span><span class="br0">&#41;</span> &nbsp;<span class="br0">&#91;</span><span class="nu0">1</span>..n<span class="br0">&#93;</span><br />
&lt;/code&gt;<br />
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:and"><span class="kw3">and</span></a> <span class="kw1">then</span> we get the Display.hs<br />
&lt;code <span class="kw1">type</span>=<span class="st0">&quot;haskell&quot;</span>&gt;<br />
<span class="kw1">module</span> Display <span class="br0">&#40;</span>display<span class="br0">&#41;</span> <span class="kw1">where</span></p>
<p><span class="kw1">import</span> Graphics.Rendering.OpenGL<br />
<span class="kw1">import</span> Graphics.UI.GLUT</p>
<p><span class="kw1">import</span> Cube<br />
<span class="kw1">import</span> Points</p>
<p>display = <span class="kw1">do</span> <br />
&nbsp; clear <span class="br0">&#91;</span>ColorBuffer<span class="br0">&#93;</span><br />
&nbsp; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:mapM_"><span class="kw3">mapM_</span></a> <span class="br0">&#40;</span>\<span class="br0">&#40;</span>x,y,z<span class="br0">&#41;</span> -&gt; preservingMatrix $ <span class="kw1">do</span><br />
&nbsp; &nbsp; color $ Color3 <span class="br0">&#40;</span><span class="br0">&#40;</span>x<span class="nu0">+1.0</span><span class="br0">&#41;</span>/<span class="nu0">2.0</span><span class="br0">&#41;</span> <span class="br0">&#40;</span><span class="br0">&#40;</span>y<span class="nu0">+1.0</span><span class="br0">&#41;</span>/<span class="nu0">2.0</span><span class="br0">&#41;</span> <span class="br0">&#40;</span><span class="br0">&#40;</span>z<span class="nu0">+1.0</span><span class="br0">&#41;</span>/<span class="nu0">2.0</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; translate $ Vector3 x y z<br />
&nbsp; &nbsp; cube <span class="br0">&#40;</span><span class="nu0">0.1</span>::GLfloat<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#41;</span> $ points <span class="nu0">7</span><br />
&nbsp; flush<br />
&nbsp;</div>
<p>where we note that we need to renormalize our colours to get them within the interval [0,1] from the interval [-1,1] in order to get valid colour values. The program looks like<br />
<img src="http://mikael.johanssons.org/images/hopengl/tut2/7circle.png" /></p>
<p>The point of this yoga doesn&#8217;t come apparent until you start adding some global transformations as well. So let&#8217;s! We add the line</p>
<div class="dean_ch" style="white-space: wrap;">
&nbsp; scale <span class="nu0">0.7</span> <span class="nu0">0.7</span> <span class="br0">&#40;</span><span class="nu0">0.7</span>::GLfloat<span class="br0">&#41;</span><br />
&nbsp;</div>
<p>just after the clear [ColorBuffer], in order to scale the entire picture. As a result, we get<br />
<img src="http://mikael.johanssons.org/images/hopengl/tut2/7circlescaled.png" /></p>
<p>We can do this with all sorts of transformations &#8211; we can rotate the picture, skew it, move the entire picture around. Using preservingMatrix, we make sure that the transformations &#8220;outside&#8221; apply in the way we&#8217;d expect them to.</p>
<h2>Back to the callbacks</h2>
<h3>Animation</h3>
<p>A lot of the OpenGL programming is centered around the program being prepared to launch some sequenc when some event occurs. Let&#8217;s use this to build a rotating version of our bunch of points up there. In order to do things over time, we&#8217;re going to be using the global callbacks idleCallback and timerCallback. So, we&#8217;ll modify the structure of our files a bit &#8211; starting from the top.</p>
<p>We&#8217;ll need a new callback. And we&#8217;ll also need a state variable of our own, which in turn needs to be fed to all functions that may need to use it. Incorporating these changes, we get a new HelloWorld.hs:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">import</span> Graphics.Rendering.OpenGL<br />
<span class="kw1">import</span> Graphics.UI.GLUT<br />
<span class="kw1">import</span> Bindings<br />
<span class="kw1">import</span> Data.IORef</p>
<p>main = <span class="kw1">do</span><br />
&nbsp; <span class="br0">&#40;</span>progname,_<span class="br0">&#41;</span> &lt;- getArgsAndInitialize<br />
&nbsp; createWindow <span class="st0">&quot;Hello World&quot;</span><br />
&nbsp; reshapeCallback $= Just reshape<br />
&nbsp; keyboardMouseCallback $= Just keyboardMouse<br />
&nbsp; angle &lt;- newIORef <span class="nu0">0.0</span><br />
&nbsp; displayCallback $= <span class="br0">&#40;</span>display angle<span class="br0">&#41;</span><br />
&nbsp; idleCallback $= Just <span class="br0">&#40;</span>idle angle<span class="br0">&#41;</span><br />
&nbsp; mainLoop<br />
&nbsp;</div>
<p>Note the addition of an angle, and an idle. We need to feed the value of angle both to idle and to display, in order for them to use it accordingly. Now, we need to define idle somewhere &#8211; and since we keep all the bits and pieces we modify a LOT in display, let&#8217;s put it in there.<br />
Exporting it all the way requires us to change the first line of Bindings.hs to </p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">module</span> Bindings <span class="br0">&#40;</span>idle,display,reshape,keyboardMouse<span class="br0">&#41;</span> <span class="kw1">where</span><br />
&nbsp;</div>
<p>Display.hs:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">module</span> Display <span class="br0">&#40;</span>display,idle<span class="br0">&#41;</span> <span class="kw1">where</span></p>
<p><span class="kw1">import</span> Graphics.Rendering.OpenGL<br />
<span class="kw1">import</span> Graphics.UI.GLUT<br />
<span class="kw1">import</span> Data.IORef</p>
<p><span class="kw1">import</span> Cube<br />
<span class="kw1">import</span> Points</p>
<p>display angle = <span class="kw1">do</span> <br />
&nbsp; clear <span class="br0">&#91;</span>ColorBuffer<span class="br0">&#93;</span><br />
&nbsp; a &lt;- get angle<br />
&nbsp; rotate a $ Vector3 <span class="nu0">0</span> <span class="nu0">0</span> <span class="br0">&#40;</span><span class="nu0">1</span>::GLfloat<span class="br0">&#41;</span><br />
&nbsp; scale <span class="nu0">0.7</span> <span class="nu0">0.7</span> <span class="br0">&#40;</span><span class="nu0">0.7</span>::GLfloat<span class="br0">&#41;</span><br />
&nbsp; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:mapM_"><span class="kw3">mapM_</span></a> <span class="br0">&#40;</span>\<span class="br0">&#40;</span>x,y,z<span class="br0">&#41;</span> -&gt; preservingMatrix $ <span class="kw1">do</span><br />
&nbsp; &nbsp; color $ Color3 <span class="br0">&#40;</span><span class="br0">&#40;</span>x<span class="nu0">+1.0</span><span class="br0">&#41;</span>/<span class="nu0">2.0</span><span class="br0">&#41;</span> <span class="br0">&#40;</span><span class="br0">&#40;</span>y<span class="nu0">+1.0</span><span class="br0">&#41;</span>/<span class="nu0">2.0</span><span class="br0">&#41;</span> <span class="br0">&#40;</span><span class="br0">&#40;</span>z<span class="nu0">+1.0</span><span class="br0">&#41;</span>/<span class="nu0">2.0</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; translate $ Vector3 x y z<br />
&nbsp; &nbsp; cube <span class="br0">&#40;</span><span class="nu0">0.1</span>::GLfloat<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#41;</span> $ points <span class="nu0">7</span><br />
&nbsp; flush</p>
<p>idle angle = <span class="kw1">do</span><br />
&nbsp; a &lt;- get angle<br />
&nbsp; angle $= a + <span class="nu0">0.1</span><br />
&nbsp;</div>
<p>Now, running this program makes a couple of different things painfully obvious. One is that things flicker. Another is that our ring is shrinking violently. The shrinking is due to our forgetting to reset all our transformations before we apply the next, and the flicker is because we&#8217;re redrawing an entire picture step by step. Much smoother animation&#8217;ll be had if we use a double buffering technique. Now, this isn&#8217;t at all hard. We need to modify a few places &#8211; tell HOpenGL that we want to do doublebuffering and also when we want to swap the ready drawn canvas for the one on the screen. So, we modify, again, HelloWorld.hs:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">import</span> Graphics.Rendering.OpenGL<br />
<span class="kw1">import</span> Graphics.UI.GLUT<br />
<span class="kw1">import</span> Data.IORef<br />
<span class="kw1">import</span> Bindings</p>
<p>main = <span class="kw1">do</span><br />
&nbsp; <span class="br0">&#40;</span>progname,_<span class="br0">&#41;</span> &lt;- getArgsAndInitialize<br />
&nbsp; initialDisplayMode $= <span class="br0">&#91;</span>DoubleBuffered<span class="br0">&#93;</span><br />
&nbsp; createWindow <span class="st0">&quot;Hello World&quot;</span><br />
&nbsp; reshapeCallback $= Just reshape<br />
&nbsp; keyboardMouseCallback $= Just keyboardMouse<br />
&nbsp; angle &lt;- newIORef <span class="nu0">0.0</span><br />
&nbsp; idleCallback $= Just <span class="br0">&#40;</span>idle angle<span class="br0">&#41;</span><br />
&nbsp; displayCallback $= <span class="br0">&#40;</span>display angle<span class="br0">&#41;</span><br />
&nbsp; mainLoop<br />
&nbsp;</div>
<p>and we also need to modify Display.hs to implement the bufferswapping. While we&#8217;re at it, we add the command loadIdentity, which resets the modification matrix.</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">module</span> Display <span class="br0">&#40;</span>display,idle<span class="br0">&#41;</span> <span class="kw1">where</span></p>
<p><span class="kw1">import</span> Graphics.Rendering.OpenGL<br />
<span class="kw1">import</span> Graphics.UI.GLUT<br />
<span class="kw1">import</span> Data.IORef</p>
<p><span class="kw1">import</span> Cube<br />
<span class="kw1">import</span> Points</p>
<p>display angle = <span class="kw1">do</span> <br />
&nbsp; clear <span class="br0">&#91;</span>ColorBuffer<span class="br0">&#93;</span><br />
&nbsp; loadIdentity<br />
&nbsp; a &lt;- get angle<br />
&nbsp; rotate a $ Vector3 <span class="nu0">0</span> <span class="nu0">0</span> <span class="br0">&#40;</span><span class="nu0">1</span>::GLfloat<span class="br0">&#41;</span><br />
&nbsp; scale <span class="nu0">0.7</span> <span class="nu0">0.7</span> <span class="br0">&#40;</span><span class="nu0">0.7</span>::GLfloat<span class="br0">&#41;</span><br />
&nbsp; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:mapM_"><span class="kw3">mapM_</span></a> <span class="br0">&#40;</span>\<span class="br0">&#40;</span>x,y,z<span class="br0">&#41;</span> -&gt; preservingMatrix $ <span class="kw1">do</span><br />
&nbsp; &nbsp; color $ Color3 <span class="br0">&#40;</span><span class="br0">&#40;</span>x<span class="nu0">+1.0</span><span class="br0">&#41;</span>/<span class="nu0">2.0</span><span class="br0">&#41;</span> <span class="br0">&#40;</span><span class="br0">&#40;</span>y<span class="nu0">+1.0</span><span class="br0">&#41;</span>/<span class="nu0">2.0</span><span class="br0">&#41;</span> <span class="br0">&#40;</span><span class="br0">&#40;</span>z<span class="nu0">+1.0</span><span class="br0">&#41;</span>/<span class="nu0">2.0</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; translate $ Vector3 x y z<br />
&nbsp; &nbsp; cube <span class="br0">&#40;</span><span class="nu0">0.1</span>::GLfloat<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#41;</span> $ points <span class="nu0">7</span><br />
&nbsp; swapBuffers</p>
<p>idle angle = <span class="kw1">do</span><br />
&nbsp; a &lt;- get angle<br />
&nbsp; angle $= a<span class="nu0">+0.1</span><br />
&nbsp; postRedisplay Nothing<br />
&nbsp;</div>
<p>There we are! That looks pretty, doesn&#8217;t it? Now, we could start adding control to the user, couldn&#8217;t we? Let&#8217;s add some keyboard interfaces. We&#8217;ll start by letting the rotation direction change when we press spacebar, and let the arrows displace the whole figure and + and &#8211; increase/decrease the rotation speed.</p>
<p>Again, we&#8217;re adding states, so we need to modify HelloWorld.hs</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">import</span> Graphics.Rendering.OpenGL<br />
<span class="kw1">import</span> Graphics.UI.GLUT<br />
<span class="kw1">import</span> Data.IORef<br />
<span class="kw1">import</span> Bindings</p>
<p>main = <span class="kw1">do</span><br />
&nbsp; <span class="br0">&#40;</span>progname,_<span class="br0">&#41;</span> &lt;- getArgsAndInitialize<br />
&nbsp; initialDisplayMode $= <span class="br0">&#91;</span>DoubleBuffered<span class="br0">&#93;</span><br />
&nbsp; createWindow <span class="st0">&quot;Hello World&quot;</span><br />
&nbsp; reshapeCallback $= Just reshape<br />
&nbsp; angle &lt;- newIORef <span class="br0">&#40;</span><span class="nu0">0.0</span>::GLfloat<span class="br0">&#41;</span><br />
&nbsp; delta &lt;- newIORef <span class="br0">&#40;</span><span class="nu0">0.1</span>::GLfloat<span class="br0">&#41;</span><br />
&nbsp; position &lt;- newIORef <span class="br0">&#40;</span><span class="nu0">0.0</span>::GLfloat, <span class="nu0">0.0</span><span class="br0">&#41;</span><br />
&nbsp; keyboardMouseCallback $= Just <span class="br0">&#40;</span>keyboardMouse delta position<span class="br0">&#41;</span><br />
&nbsp; idleCallback $= Just <span class="br0">&#40;</span>idle angle delta<span class="br0">&#41;</span><br />
&nbsp; displayCallback $= <span class="br0">&#40;</span>display angle position<span class="br0">&#41;</span><br />
&nbsp; mainLoop<br />
&nbsp;</div>
<p>Note that position is sent along to the keyboard as well as the display callbacks. And in Bindings.hs, we give the keyboard callback actual function</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">module</span> Bindings <span class="br0">&#40;</span>idle,display,reshape,keyboardMouse<span class="br0">&#41;</span> <span class="kw1">where</span><br />
<span class="kw1">import</span> Graphics.Rendering.OpenGL<br />
<span class="kw1">import</span> Graphics.UI.GLUT<br />
<span class="kw1">import</span> Data.IORef</p>
<p><span class="kw1">import</span> Display</p>
<p>reshape s@<span class="br0">&#40;</span>Size w h<span class="br0">&#41;</span> = <span class="kw1">do</span> <br />
&nbsp; viewport $= <span class="br0">&#40;</span>Position <span class="nu0">0</span> <span class="nu0">0</span>, s<span class="br0">&#41;</span></p>
<p>keyboardAct a p <span class="br0">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Char"><span class="kw4">Char</span></a> &#8216; &#8216;<span class="br0">&#41;</span> Down = <span class="kw1">do</span><br />
&nbsp; a&#8217; &lt;- get a<br />
&nbsp; a $= -a&#8217;<br />
keyboardAct a p <span class="br0">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Char"><span class="kw4">Char</span></a> &#8216;+&#8217;<span class="br0">&#41;</span> Down = <span class="kw1">do</span><br />
&nbsp; a&#8217; &lt;- get a<br />
&nbsp; a $= <span class="nu0">2</span>*a&#8217;<br />
keyboardAct a p <span class="br0">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Char"><span class="kw4">Char</span></a> &#8216;-&#8217;<span class="br0">&#41;</span> Down = <span class="kw1">do</span><br />
&nbsp; a&#8217; &lt;- get a<br />
&nbsp; a $= a&#8217;/<span class="nu0">2</span><br />
keyboardAct a p <span class="br0">&#40;</span>SpecialKey KeyLeft<span class="br0">&#41;</span> Down = <span class="kw1">do</span><br />
&nbsp; <span class="br0">&#40;</span>x,y<span class="br0">&#41;</span> &lt;- get p<br />
&nbsp; p $= <span class="br0">&#40;</span>x<span class="nu0">-0.1</span>,y<span class="br0">&#41;</span><br />
keyboardAct a p <span class="br0">&#40;</span>SpecialKey KeyRight<span class="br0">&#41;</span> Down = <span class="kw1">do</span><br />
&nbsp; <span class="br0">&#40;</span>x,y<span class="br0">&#41;</span> &lt;- get p<br />
&nbsp; p $= <span class="br0">&#40;</span>x<span class="nu0">+0.1</span>,y<span class="br0">&#41;</span><br />
keyboardAct a p<span class="br0">&#40;</span>SpecialKey KeyUp<span class="br0">&#41;</span> Down = <span class="kw1">do</span><br />
&nbsp; <span class="br0">&#40;</span>x,y<span class="br0">&#41;</span> &lt;- get p<br />
&nbsp; p $= <span class="br0">&#40;</span>x,y<span class="nu0">+0.1</span><span class="br0">&#41;</span><br />
keyboardAct a p <span class="br0">&#40;</span>SpecialKey KeyDown<span class="br0">&#41;</span> Down = <span class="kw1">do</span><br />
&nbsp; <span class="br0">&#40;</span>x,y<span class="br0">&#41;</span> &lt;- get p<br />
&nbsp; p $= <span class="br0">&#40;</span>x,y<span class="nu0">-0.1</span><span class="br0">&#41;</span><br />
keyboardAct _ _ _ _ = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:return"><span class="kw3">return</span></a> <span class="br0">&#40;</span><span class="br0">&#41;</span></p>
<p>keyboardMouse angle pos key state modifiers position = <span class="kw1">do</span><br />
&nbsp; keyboardAct angle pos key state<br />
&nbsp;</div>
<p>finally, in Display.hs we use the new information to accordingly redraw the scene, specifically the now changing amount to change the current angle with. Note that in order to avoid the placement of the circle to be pulled in with all the other modifications we&#8217;re doing, we do the translation outside a preservingMatrix call.</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">module</span> Display <span class="br0">&#40;</span>display,idle<span class="br0">&#41;</span> <span class="kw1">where</span></p>
<p><span class="kw1">import</span> Graphics.Rendering.OpenGL<br />
<span class="kw1">import</span> Graphics.UI.GLUT<br />
<span class="kw1">import</span> Data.IORef</p>
<p><span class="kw1">import</span> Cube<br />
<span class="kw1">import</span> Points</p>
<p>display angle position = <span class="kw1">do</span> <br />
&nbsp; clear <span class="br0">&#91;</span>ColorBuffer<span class="br0">&#93;</span><br />
&nbsp; loadIdentity<br />
&nbsp; <span class="br0">&#40;</span>x,y<span class="br0">&#41;</span> &lt;- get position<br />
&nbsp; translate $ Vector3 x y <span class="nu0">0</span><br />
&nbsp; preservingMatrix $ <span class="kw1">do</span> <br />
&nbsp; &nbsp; a &lt;- get angle<br />
&nbsp; &nbsp; rotate a $ Vector3 <span class="nu0">0</span> <span class="nu0">0</span> <span class="br0">&#40;</span><span class="nu0">1</span>::GLfloat<span class="br0">&#41;</span><br />
&nbsp; &nbsp; scale <span class="nu0">0.7</span> <span class="nu0">0.7</span> <span class="br0">&#40;</span><span class="nu0">0.7</span>::GLfloat<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:mapM_"><span class="kw3">mapM_</span></a> <span class="br0">&#40;</span>\<span class="br0">&#40;</span>x,y,z<span class="br0">&#41;</span> -&gt; preservingMatrix $ <span class="kw1">do</span><br />
&nbsp; &nbsp; &nbsp; color $ Color3 <span class="br0">&#40;</span><span class="br0">&#40;</span>x<span class="nu0">+1.0</span><span class="br0">&#41;</span>/<span class="nu0">2.0</span><span class="br0">&#41;</span> <span class="br0">&#40;</span><span class="br0">&#40;</span>y<span class="nu0">+1.0</span><span class="br0">&#41;</span>/<span class="nu0">2.0</span><span class="br0">&#41;</span> <span class="br0">&#40;</span><span class="br0">&#40;</span>z<span class="nu0">+1.0</span><span class="br0">&#41;</span>/<span class="nu0">2.0</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; translate $ Vector3 x y z<br />
&nbsp; &nbsp; &nbsp; cube <span class="br0">&#40;</span><span class="nu0">0.1</span>::GLfloat<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span> $ points <span class="nu0">7</span><br />
&nbsp; swapBuffers</p>
<p>idle angle delta = <span class="kw1">do</span><br />
&nbsp; a &lt;- get angle<br />
&nbsp; d &lt;- get delta<br />
&nbsp; angle $= a+d<br />
&nbsp; postRedisplay Nothing<br />
&nbsp;</div>
<h2>Summary</h2>
<p>We now know how to modify only parts of a picture, and we also know how to use the idle and the keyboardMouse callback to support animations and keyboard input.</p>
<p>In order to somewhat limit the amount of typing I need to do, I&#8217;ll give links that give details on some of the themes we&#8217;ve touched upon.</p>
<p>First of all, the callbacks are described in more detail and with call signatures at<br />
<a href=http://haskell.org/ghc/docs/latest/html/libraries/GLUT/Graphics-UI-GLUT-Callbacks-Global.html>http://haskell.org/ghc/docs/latest/html/libraries/GLUT/Graphics-UI-GLUT-Callbacks-Global.html</a> for the global callbacks (menu systems, and timing/idle callbacks)<br />
<a href=http://haskell.org/ghc/docs/latest/html/libraries/GLUT/Graphics-UI-GLUT-Callbacks-Window.html>http://haskell.org/ghc/docs/latest/html/libraries/GLUT/Graphics-UI-GLUT-Callbacks-Window.html</a> for the window-specific callbacks (display, reshape, keyboard&#038;mouse, visibility changes, window closing, mouse movement, spaceballs, drawing tablets, joysticks and dial&#038;button)</p>
<p>Furthermore, the various primitives for drawing are listed at <a href= http://haskell.org/ghc/docs/latest/html/libraries/OpenGL/Graphics-Rendering-OpenGL-GL-BeginEnd.html>http://haskell.org/ghc/docs/latest/html/libraries/OpenGL/Graphics-Rendering-OpenGL-GL-BeginEnd.html</a>.<br />
There are 3-dimensional primitives ready as well. These can be found at <a href=http://haskell.org/ghc/docs/latest/html/libraries/GLUT/Graphics-UI-GLUT-Objects.html>http://haskell.org/ghc/docs/latest/html/libraries/GLUT/Graphics-UI-GLUT-Objects.html</a></p>
<p>The flag I set to trigger double buffering is described among the GLUT initialization methods, see <a href=http://haskell.org/ghc/docs/latest/html/libraries/GLUT/Graphics-UI-GLUT-Initialization.html>http://haskell.org/ghc/docs/latest/html/libraries/GLUT/Graphics-UI-GLUT-Initialization.html</a> for everything you can do there.</p>
<p>Next time, I figure I&#8217;ll get around to do Mouse callbacks and 3d graphics. We&#8217;ll see.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mikael.johanssons.org/archive/2006/09/opengl-programming-in-haskell-a-tutorial-part-2/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Hello, Planet Haskell</title>
		<link>http://blog.mikael.johanssons.org/archive/2006/09/hello-planet-haskell/</link>
		<comments>http://blog.mikael.johanssons.org/archive/2006/09/hello-planet-haskell/#comments</comments>
		<pubDate>Thu, 14 Sep 2006 16:28:43 +0000</pubDate>
		<dc:creator>Michi</dc:creator>
				<category><![CDATA[Administrative]]></category>
		<category><![CDATA[Computer]]></category>
		<category><![CDATA[English]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.mikael.johanssons.org/archive/2006/09/hello-planet-haskell/</guid>
		<description><![CDATA[Since the content rate of haskell-related posts is going up, the feed of this blog will get added to Planet Haskell. Hi, Planet!]]></description>
			<content:encoded><![CDATA[<p>Since the content rate of haskell-related posts is going up, the feed of this blog will get added to <a href=http://planet.haskell.org>Planet Haskell</a>. Hi, Planet!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mikael.johanssons.org/archive/2006/09/hello-planet-haskell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OpenGL programming in Haskell &#8211; a tutorial (part 1)</title>
		<link>http://blog.mikael.johanssons.org/archive/2006/09/opengl-programming-in-haskell-a-tutorial-part-1/</link>
		<comments>http://blog.mikael.johanssons.org/archive/2006/09/opengl-programming-in-haskell-a-tutorial-part-1/#comments</comments>
		<pubDate>Thu, 14 Sep 2006 15:17:45 +0000</pubDate>
		<dc:creator>Michi</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.mikael.johanssons.org/archive/2006/09/opengl-programming-in-haskell-a-tutorial-part-1/</guid>
		<description><![CDATA[After having failed following the googled tutorial in HOpenGL programming, I thought I&#8217;d write down the steps I actually can get to work in a tutorial-like fashion. It may be a good idea to read this in parallell to the tutorial linked, since Panitz actually brings a lot of good explanations, even though his syntax [...]]]></description>
			<content:encoded><![CDATA[<p>After having failed following the <a href=http://www.tfh-berlin.de/~panitz/hopengl/skript.html>googled tutorial in HOpenGL programming</a>, I thought I&#8217;d write down the steps I actually can get to work in a tutorial-like fashion. It may be a good idea to read this in parallell to the tutorial linked, since Panitz actually brings a lot of good explanations, even though his syntax isn&#8217;t up to speed with the latest HOpenGL at all points.</p>
<h2>Hello World</h2>
<p>First of all, we&#8217;ll want to load the OpenGL libraries, throw up a window, and generally get to grips with what needs to be done to get a program running at all. </p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">import</span> Graphics.Rendering.OpenGL<br />
<span class="kw1">import</span> Graphics.UI.GLUT</p>
<p>main = <span class="kw1">do</span> <br />
&nbsp; <span class="br0">&#40;</span>progname, _<span class="br0">&#41;</span> &lt;- getArgsAndInitialize<br />
&nbsp; createWindow <span class="st0">&quot;Hello World&quot;</span><br />
&nbsp; mainLoop<br />
&nbsp;</div>
<p>This code throws up a window, with a given title. Nothing more happens, though. This is the skeleton that we&#8217;ll be building on to. Save it to HelloWorld.hs and compile it by running<br />
<code type="haskell"><br />
ghc -package GLUT HelloWorld.hs -o HelloWorld<br />
</code></p>
<p>However, as a skeleton, it is profoundly worthless. It doesn&#8217;t even redraw the window, so we should definitely make sure to have a function that takes care of that in there somewhere. Telling the OpenGL-system what to do is done by using state variables, and these, in turn are handled by the datatype Data.IORef. So we modify our code to the following</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">import</span> Graphics.Rendering.OpenGL<br />
<span class="kw1">import</span> Graphics.UI.GLUT</p>
<p>main = <span class="kw1">do</span><br />
&nbsp; <span class="br0">&#40;</span>progname, _<span class="br0">&#41;</span> &lt;- getArgsAndInitialize<br />
&nbsp; createWindow <span class="st0">&quot;Hello World&quot;</span><br />
&nbsp; displayCallback $= clear <span class="br0">&#91;</span> ColorBuffer <span class="br0">&#93;</span><br />
&nbsp; mainLoop<br />
&nbsp;</div>
<p>This sets the global state variable carrying the callback function responsible for drawing the window to be the function that clears the color state. Save this to the HelloWorld.hs, recompile, and rerun. This program no longer carries the original pixels along, but rather clears everything out.</p>
<p>The displayCallback is a globally defined IORef, which can be accessed through a host of functions defined in Data.IORef. However, deep within the OpenGL code, there are a couple of definition providing the interface functions $= and get to fascilitate interactions with these. Thus we can do things like</p>
<div class="dean_ch" style="white-space: wrap;">
height = newIORef <span class="nu0">1.0</span><br />
currentheight &lt;- get height<br />
height $= <span class="nu0">1.5</span><br />
&nbsp;</div>
<h2>Using the drawing canvas</h2>
<p>So, we have a window, we have a display callback that clears the canvas. Don&#8217;t we want more out of it? Sure we do. So let&#8217;s draw some things.</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">import</span> Graphics.Rendering.OpenGL<br />
<span class="kw1">import</span> Graphics.UI.GLUT</p>
<p>myPoints :: <span class="br0">&#91;</span><span class="br0">&#40;</span>GLfloat,GLfloat,GLfloat<span class="br0">&#41;</span><span class="br0">&#93;</span><br />
myPoints = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map"><span class="kw3">map</span></a> <span class="br0">&#40;</span>\k -&gt; <span class="br0">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sin"><span class="kw3">sin</span></a><span class="br0">&#40;</span><span class="nu0">2</span>*<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:pi"><span class="kw3">pi</span></a>*k/<span class="nu0">12</span><span class="br0">&#41;</span>,<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:cos"><span class="kw3">cos</span></a><span class="br0">&#40;</span><span class="nu0">2</span>*<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:pi"><span class="kw3">pi</span></a>*k/<span class="nu0">12</span><span class="br0">&#41;</span>,<span class="nu0">0.0</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#91;</span><span class="nu0">1</span>..<span class="nu0">12</span><span class="br0">&#93;</span></p>
<p>main = <span class="kw1">do</span> <br />
&nbsp; <span class="br0">&#40;</span>progname, _<span class="br0">&#41;</span> &lt;- getArgsAndInitialize<br />
&nbsp; createWindow <span class="st0">&quot;Hello World&quot;</span><br />
&nbsp; displayCallback $= display<br />
&nbsp; mainLoop</p>
<p>display = <span class="kw1">do</span> <br />
&nbsp; clear <span class="br0">&#91;</span>ColorBuffer<span class="br0">&#93;</span><br />
&nbsp; renderPrimitive Points $ <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:mapM_"><span class="kw3">mapM_</span></a> <span class="br0">&#40;</span>\<span class="br0">&#40;</span>x, y, z<span class="br0">&#41;</span>-&gt;vertex$Vertex3 x y z<span class="br0">&#41;</span> myPoints<br />
&nbsp; flush<br />
&nbsp;</div>
<p>Now, the important thing to notice in this codeextract is that last line. It starts a rendering definition, gives the type to be rendered, and then a sequence of function calls, each of which adds a vertex to the rendering canvas. The statement is basically equivalent to something along the lines of</p>
<div class="dean_ch" style="white-space: wrap;">
renderPrimitive Points <span class="kw1">do</span><br />
&nbsp;vertex Vertex3 &#8230; <br />
&nbsp;vertex Vertex3 &#8230;<br />
&nbsp;</div>
<p>for appropriate triples of coordinate values at the appropriate places. This results in the rendition here:<br />
<img src="http://mikael.johanssons.org/images/hopengl/points.png" /></p>
<p>We can replace Points with other primitives, leading to the rendering of:</p>
<h3>Triangles</h3>
<p><img src="http://mikael.johanssons.org/images/hopengl/triangles.png" /><br />
Each three coordinates following each other define a triangle. The last n mod 3 coordinates are ignored.<br />
Keyword Triangles</p>
<h3>Triangle strips</h3>
<p><img src="http://mikael.johanssons.org/images/hopengl/trianglestrip.png" /><br />
Triangles are drawn according to a &#8220;moving window&#8221; of size three, so the two last coordinates in the previous triangle become the two first in the next triangle.<br />
Keyword TriangleStrip</p>
<h3>Triangle fans</h3>
<p><img src="http://mikael.johanssons.org/images/hopengl/trianglesfan.png" /><br />
Triangle fans have the first given coordinate as a basepoint, and takes each pair of subsequent coordinates to define a triangle together with the first coordinate.<br />
Keyword TriangleFan</p>
<h3>Lines</h3>
<p><img src="http://mikael.johanssons.org/images/hopengl/lines.png" /><br />
Each pair of coordinates define a line.<br />
Keyword Lines</p>
<h3>Line loops</h3>
<p><img src="http://mikael.johanssons.org/images/hopengl/lineloop.png" /><br />
With line loops, each further coordinate defines a line together with the last coordinate used. Once all coordinates are used up, an additional line is drawn back to the beginning.<br />
Keyword LineLoop</p>
<h3>Line strips</h3>
<p><img src="http://mikael.johanssons.org/images/hopengl/linestrip.png" /><br />
Line strips are like line loops, only without the last link added.<br />
Keyword LineStrip</p>
<h3>Quadrangles</h3>
<p><img src="http://mikael.johanssons.org/images/hopengl/quad.png" /><br />
For the Quads keyword, each four coordinates given define a quadrangle.<br />
Keyword Quads</p>
<h3>Quadrangle strips</h3>
<p><img src="http://mikael.johanssons.org/images/hopengl/quadstrip.png" /><br />
And a Quadstrip works as the trianglestrip, only the window is 4 coordinates wide and steps 2 steps each time, so each new pair of coordinates attaches a new quadrangle to the last edge of the last quadrangle.<br />
Keyword QuadStrip</p>
<h3>Polygon</h3>
<p><img src="http://mikael.johanssons.org/images/hopengl/polygon.png" /><br />
A Polygon is a filled line loop. Simple as that!<br />
Keyword Polygon</p>
<p>There are more things we can do on our canvas than just spreading out coordinates. Within the command list constructed after a renderPrimitive, we can give several different commands that control what things are supposed to look like, so for instance we could use the following</p>
<div class="dean_ch" style="white-space: wrap;">
display = <span class="kw1">do</span> <br />
&nbsp; clear <span class="br0">&#91;</span>ColorBuffer<span class="br0">&#93;</span><br />
&nbsp; renderPrimitive Quads $ <span class="kw1">do</span><br />
&nbsp; &nbsp; color $ <span class="br0">&#40;</span>Color3 <span class="br0">&#40;</span><span class="nu0">1.0</span>::GLfloat<span class="br0">&#41;</span> <span class="nu0">0</span> <span class="nu0">0</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; vertex $ <span class="br0">&#40;</span>Vertex3 <span class="br0">&#40;</span><span class="nu0">0</span>::GLfloat<span class="br0">&#41;</span> <span class="nu0">0</span> <span class="nu0">0</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; vertex $ <span class="br0">&#40;</span>Vertex3 <span class="br0">&#40;</span><span class="nu0">0</span>::GLfloat<span class="br0">&#41;</span> <span class="nu0">0.2</span> <span class="nu0">0</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; vertex $ <span class="br0">&#40;</span>Vertex3 <span class="br0">&#40;</span><span class="nu0">0.2</span>::GLfloat<span class="br0">&#41;</span> <span class="nu0">0.2</span> <span class="nu0">0</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; vertex $ <span class="br0">&#40;</span>Vertex3 <span class="br0">&#40;</span><span class="nu0">0.2</span>::GLfloat<span class="br0">&#41;</span> <span class="nu0">0</span> <span class="nu0">0</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; color $ <span class="br0">&#40;</span>Color3 <span class="br0">&#40;</span><span class="nu0">0</span>::GLfloat<span class="br0">&#41;</span> <span class="nu0">1</span> <span class="nu0">0</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; vertex $ <span class="br0">&#40;</span>Vertex3 <span class="br0">&#40;</span><span class="nu0">0</span>::GLfloat<span class="br0">&#41;</span> <span class="nu0">0</span> <span class="nu0">0</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; vertex $ <span class="br0">&#40;</span>Vertex3 <span class="br0">&#40;</span><span class="nu0">0</span>::GLfloat<span class="br0">&#41;</span> <span class="br0">&#40;</span><span class="nu0">-0.2</span><span class="br0">&#41;</span> <span class="nu0">0</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; vertex $ <span class="br0">&#40;</span>Vertex3 <span class="br0">&#40;</span><span class="nu0">0.2</span>::GLfloat<span class="br0">&#41;</span> <span class="br0">&#40;</span><span class="nu0">-0.2</span><span class="br0">&#41;</span> <span class="nu0">0</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; vertex $ <span class="br0">&#40;</span>Vertex3 <span class="br0">&#40;</span><span class="nu0">0.2</span>::GLfloat<span class="br0">&#41;</span> <span class="nu0">0</span> <span class="nu0">0</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; color $ <span class="br0">&#40;</span>Color3 <span class="br0">&#40;</span><span class="nu0">0</span>::GLfloat<span class="br0">&#41;</span> <span class="nu0">0</span> <span class="nu0">1</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; vertex $ <span class="br0">&#40;</span>Vertex3 <span class="br0">&#40;</span><span class="nu0">0</span>::GLfloat<span class="br0">&#41;</span> <span class="nu0">0</span> <span class="nu0">0</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; vertex $ <span class="br0">&#40;</span>Vertex3 <span class="br0">&#40;</span><span class="nu0">0</span>::GLfloat<span class="br0">&#41;</span> <span class="br0">&#40;</span><span class="nu0">-0.2</span><span class="br0">&#41;</span> <span class="nu0">0</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; vertex $ <span class="br0">&#40;</span>Vertex3 <span class="br0">&#40;</span><span class="br0">&#40;</span><span class="nu0">-0.2</span><span class="br0">&#41;</span>::GLfloat<span class="br0">&#41;</span> <span class="br0">&#40;</span><span class="nu0">-0.2</span><span class="br0">&#41;</span> <span class="nu0">0</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; vertex $ <span class="br0">&#40;</span>Vertex3 <span class="br0">&#40;</span><span class="br0">&#40;</span><span class="nu0">-0.2</span><span class="br0">&#41;</span>::GLfloat<span class="br0">&#41;</span> <span class="nu0">0</span> <span class="nu0">0</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; color $ <span class="br0">&#40;</span>Color3 <span class="br0">&#40;</span><span class="nu0">1</span>::GLfloat<span class="br0">&#41;</span> <span class="nu0">0</span> <span class="nu0">1</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; vertex $ <span class="br0">&#40;</span>Vertex3 <span class="br0">&#40;</span><span class="nu0">0</span>::GLfloat<span class="br0">&#41;</span> <span class="nu0">0</span> <span class="nu0">0</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; vertex $ <span class="br0">&#40;</span>Vertex3 <span class="br0">&#40;</span><span class="nu0">0</span>::GLfloat<span class="br0">&#41;</span> <span class="nu0">0.2</span> <span class="nu0">0</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; vertex $ <span class="br0">&#40;</span>Vertex3 <span class="br0">&#40;</span><span class="br0">&#40;</span><span class="nu0">-0.2</span>::GLfloat<span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="nu0">0.2</span> <span class="nu0">0</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; vertex $ <span class="br0">&#40;</span>Vertex3 <span class="br0">&#40;</span><span class="br0">&#40;</span><span class="nu0">-0.2</span>::GLfloat<span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="nu0">0</span> <span class="nu0">0</span><span class="br0">&#41;</span><br />
&nbsp; flush<br />
&nbsp;</div>
<p>in order to produce these four coloured squares<br />
<img src="http://mikael.johanssons.org/images/hopengl/colorsquares.png" /><br />
where each color command sets the color for the next item drawn, and the vertex commands give vertices for the four squares.</p>
<h2>Callbacks &#8211; how we react to changes</h2>
<p>We have already seen one callback in action: displayCallBack. The Callbacks are state variables of the HOpenGL system, and are called in order to handle various things that may happen to the place the drawing canvas lives. For a first exercise, go resize the latest window you&#8217;ve used. Go on, do it now.</p>
<p>I bet it looked ugly, didn&#8217;t it?</p>
<p>This is because we have no code handling what to do if the window should suddenly change. Handling this is done in a callback, residing in the IORef reshapeCallback. Similarily, repainting is done in displayCallback, keyboard and mouse input is in keyboardMouseCallback, and so on. We can refer to the HOpenGL documentation for <a href=http://haskell.org/ghc/docs/latest/html/libraries/GLUT/Graphics-UI-GLUT-Callbacks-Window.html>window callbacks</a> and for <a href=http://haskell.org/ghc/docs/latest/html/libraries/GLUT/Graphics-UI-GLUT-Callbacks-Window.html>global callbacks</a>. Window callbacks are things like display, keyboard and mouse, and reshape. Global callbacks deal with timing issues (for those snazzy animations) and the menu interface systems.</p>
<p>In order for a callback to possibly not be defined, most are typed within the Maybe monad, so by setting the state variable to Nothing, a callback can be disabled. Thus, setting callbacks is done using the keyword Just. We&#8217;ll add a callback for reshaping the window to our neat code, changing the main function to</p>
<div class="dean_ch" style="white-space: wrap;">
main = <span class="kw1">do</span> <br />
&nbsp; <span class="br0">&#40;</span>progname, _<span class="br0">&#41;</span> &lt;- getArgsAndInitialize<br />
&nbsp; createWindow <span class="st0">&quot;Hello World&quot;</span><br />
&nbsp; displayCallback $= display<br />
&nbsp; reshapeCallback $= Just reshape<br />
&nbsp; mainLoop</p>
<p>reshape s@<span class="br0">&#40;</span>Size w h<span class="br0">&#41;</span> = <span class="kw1">do</span><br />
&nbsp; viewport $= <span class="br0">&#40;</span>Position <span class="nu0">0</span> <span class="nu0">0</span>, s<span class="br0">&#41;</span><br />
&nbsp; postRedisplay Nothing<br />
&nbsp;</div>
<p>Here, the code for the reshape function resizes the viewport so that our drawing area contains the entire new window. After setting the new viewport, it also tells the windowing system that something has happened to the window, and that therefore, the display function should be called.</p>
<h2>Summary</h2>
<p>So, in conclusion, so far we can display a window, post basic callbacks to get the windowhandling to run smoothly, and draw in our window. Next installment of the tutorial will bring you 3d drawing, keyboard and mouse interactions, the incredible power of matrices and the ability to rotate 3d objects for your leisure. Possibly, we&#8217;ll even look into animations.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mikael.johanssons.org/archive/2006/09/opengl-programming-in-haskell-a-tutorial-part-1/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Weekly Report: Back up again</title>
		<link>http://blog.mikael.johanssons.org/archive/2006/07/weekly-report-back-up-again/</link>
		<comments>http://blog.mikael.johanssons.org/archive/2006/07/weekly-report-back-up-again/#comments</comments>
		<pubDate>Sun, 23 Jul 2006 09:54:57 +0000</pubDate>
		<dc:creator>Michi</dc:creator>
				<category><![CDATA[Administrative]]></category>
		<category><![CDATA[Computer]]></category>
		<category><![CDATA[English]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[PhD]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Weekly report]]></category>

		<guid isPermaLink="false">http://blog.mikael.johanssons.org/archive/2006/07/weekly-report-back-up-again/</guid>
		<description><![CDATA[The weekly reports have been dead for a while. Reason? The blog has been dead for a while. Hardware woes The old computer running this website had some problem all of a sudden about 3 weeks ago. These problems appeared as a complete lockdown of the system &#8211; no response to anything. So my brother [...]]]></description>
			<content:encoded><![CDATA[<p>The weekly reports have been dead for a while. Reason? The blog has been dead for a while.</p>
<h2>Hardware woes</h2>
<p>The old computer running this website had some problem all of a sudden about 3 weeks ago. These problems appeared as a complete lockdown of the system &#8211; no response to anything. So my brother &#8211; with me on the other side of a telephone, tried to reboot the box; but couldn&#8217;t get it back up online again. He was headed out to a LARP anyway within hours &#8211; and so couldn&#8217;t really do much more about it.</p>
<p>Right.</p>
<p>End result? I joined forces with a good friend of mine; we split hardware costs for a slick new box &#8211; an Asus barebone box with a 64bit processor and a gig of RAM. It received the harddrive and network interface from the old box, and was with that good to go &#8211; only .. processor architecture changed; and so for optimal performance, it&#8217;d be a nice idea to actually use a new system install that took advantage of the extra available bitwidth.</p>
<p>A completely failed OpenBSD installation later, we settled for Gentoo &#8211; which now is humming along nicely. It even turned out to be surprisingly easy to restore the old data on the new box.</p>
<p>There. I won&#8217;t blather more about my hardware woes &#8211; but instead poke about other themes for a while. </p>
<h2>Party party party</h2>
<p>My students here for the course rock. The REALLY rock. We had the course evaluations a couple of weeks ago &#8211; and apparently my exercise sessions has been gooooood. At least the students thought so. I average in the upper fifth of possible ratings in every single evaluation area. All of them. I kick ass, yo!</p>
<p>Compared &#8211; both to the faculty mean, and to the workgroup results &#8211; I outperform in all evaluated areas.</p>
<p>And, in addition to this cause for celebration, the end of the school term (last week) comes with all manners of parties, celebrations, meets, and activities. I think I had my first non-planned evening in about 2-3 weeks yesterday.</p>
<h2>Summer in the city</h2>
<p>The Jena climate, though, leaves a bit to wish for. I sincerely long to get back to Sweden during the hottest summer weeks we have up there, in order for me to cool down a bit. We have had 25&deg;+ since beginning of June (i.e. about 2 months of active time with weekly lectures, seminars and exercise classes &#8211; in tropical bloody heat); and the last about 2 weeks or so, the temperatures have been in the upper 30&deg;ies.</p>
<p>Horrible. Horrible, I tell you.</p>
<p>And of course, this heatwave has coincided with the World Cup, the Thüringertage and several other huge parties that cover the town in DJ booths on the streets and drunk germans shouting on the streets at 3am.</p>
<p>This wouldn&#8217;t have had to be a problem, if it wasn&#8217;t for the fact that the main bar street begins at my front door. And the heat forces me to keep as many windows open as ever possible, lest I boil away in the night.</p>
<h2>The wild swinging of the PhD topic</h2>
<p>As I begun here, I was told that since the people here don&#8217;t know much about A<sub>&infin;</sub>-algebras, operads, props and similar topics, so it&#8217;d be best if I shelf it and keep it a hobby. My advisor hesitated on suggesting one possible and highly computational theme since he couldn&#8217;t see any applications of it later on; no directions for further research. And so I studied group cohomology in order to at least try and understand what&#8217;s going on. (I&#8217;m getting there &#8211; by and by&#8230;) Then my advisor went to Oberwolfach for a week. Upon returning, A<sub>&infin;</sub>-stuff is hot, operads seem related, the computational theme is worthy of serious consideration and I should consider whether I really want to commit that deeply to the original ring-theoretic ideas.</p>
<p>So &#8230; right now I know even less of what I&#8217;m going to do for my PhD than a month ago. On the other hand, one of the leads would amount to my programming for about half a year, and then publishing a paper about that and start using that program to gain intuition for results to put into the PhD. This might be a good direction to go. </p>
<h2>The constant distractions</h2>
<p>I&#8217;ve started looking at Haskell. I -like- it. It&#8217;s a language where I&#8217;m capable of hacking together an efficient algorithm for enumerating subsets just by thinking through what a subset is. How did you do it, you may ask, whereupon I answer: Recursion.</p>
<p>Consider a total ordering of your finite set. Pick the first element. For each subset, either this element is there, or it&#8217;s not. So if we take all subsets of the rest, and throw in once with and once without this particular element, we get what we want.</p>
<p>Now, just translate this to actual code &#8211; and the result is</p>
<div class="dean_ch" style="white-space: wrap;">
subsets :: <span class="br0">&#91;</span>a<span class="br0">&#93;</span> -&gt; <span class="br0">&#91;</span><span class="br0">&#91;</span>a<span class="br0">&#93;</span><span class="br0">&#93;</span> &nbsp; &nbsp; &nbsp; <span class="co1">&#8211; it is a function that takes a list of somethings to a list of lists of somethings</span><br />
subsets <span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="br0">&#91;</span><span class="br0">&#91;</span><span class="br0">&#93;</span><span class="br0">&#93;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">&#8211; it sends the empty list to the list of the empty list</span><br />
subsets <span class="br0">&#40;</span>x:xs<span class="br0">&#41;</span> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map"><span class="kw3">map</span></a> <span class="br0">&#40;</span>x:<span class="br0">&#41;</span> w ++ w <span class="kw1">where</span> w = subsets xs &nbsp; <span class="co1">&#8211; see the discussion above</span><br />
&nbsp;</div>
<p>Useful for parsing this is to know that : is the cons operator, and ++ concatenates lists. map applies the given function to all elements of the following list; and where lets you define local variables postfixedly.</p>
<p>A shoutout to sigfpe for piqueing my interest in these things enough to try and make sense of the language. Without his category theory heavy posts on Haskell, I wouldn&#8217;t have looked into it.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mikael.johanssons.org/archive/2006/07/weekly-report-back-up-again/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

