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

<channel>
	<title>blog :: Brent -&#62; [String]</title>
	<atom:link href="https://byorgey.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>https://byorgey.wordpress.com</link>
	<description></description>
	<lastBuildDate>Tue, 21 May 2013 21:29:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='byorgey.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>https://s2.wp.com/i/buttonw-com.png</url>
		<title>blog :: Brent -&#62; [String]</title>
		<link>https://byorgey.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="https://byorgey.wordpress.com/osd.xml" title="blog :: Brent -&#62; [String]" />
	<atom:link rel='hub' href='https://byorgey.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Workshop on Functional Art, Music, Modeling and Design</title>
		<link>https://byorgey.wordpress.com/2013/05/13/workshop-on-functional-art-music-modeling-and-design/</link>
		<comments>https://byorgey.wordpress.com/2013/05/13/workshop-on-functional-art-music-modeling-and-design/#comments</comments>
		<pubDate>Mon, 13 May 2013 21:10:59 +0000</pubDate>
		<dc:creator>Brent</dc:creator>
				<category><![CDATA[meta]]></category>
		<category><![CDATA[art]]></category>
		<category><![CDATA[beauty]]></category>
		<category><![CDATA[CFP]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[functional]]></category>
		<category><![CDATA[ICFP]]></category>
		<category><![CDATA[music]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[workshop]]></category>

		<guid isPermaLink="false">http://byorgey.wordpress.com/?p=1089</guid>
		<description><![CDATA[I’m helping organize a new workshop, FARM, to be held in Boston this September (right after ICFP). Many readers of this blog may have already seen the announcement, but I thought it worth saying a bit more about it here, &#8230; <a href="https://byorgey.wordpress.com/2013/05/13/workshop-on-functional-art-music-modeling-and-design/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="https://stats.wordpress.com/b.gif?host=byorgey.wordpress.com&#038;blog=1152889&#038;post=1089&#038;subd=byorgey&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I’m helping organize a new workshop, <a href="http://www.cis.upenn.edu/~byorgey/farm13/">FARM</a>, to be held in Boston this September (right after ICFP). Many readers of this blog may have already seen the announcement, but I thought it worth saying a bit more about it here, in the spirit of trying to spread the word as widely as possible.</p>
<p>The short of it is—it should be super interesting and a lot of fun. If you are at all interested in the intersection of functional programming and design, art, music—anything that has to do with using <strong>beautiful code to produce beautiful artifacts</strong>—you should consider submitting a paper, or planning to attend! Papers can be submitted in two categories, full papers novel research contribution) and “aesthetic applications” (which should describe some sort of beautiful way to produce something beautiful). The deadline for submissions is June 14. See <a href="http://www.cis.upenn.edu/~byorgey/farm13/">the website</a> for more details.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/byorgey.wordpress.com/1089/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/byorgey.wordpress.com/1089/" /></a> <img alt="" border="0" src="https://stats.wordpress.com/b.gif?host=byorgey.wordpress.com&#038;blog=1152889&#038;post=1089&#038;subd=byorgey&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>https://byorgey.wordpress.com/2013/05/13/workshop-on-functional-art-music-modeling-and-design/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="https://0.gravatar.com/avatar/cc113924265dbeb535c8b2fefe4e33ee?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Brent</media:title>
		</media:content>
	</item>
		<item>
		<title>Monad transformers: a cautionary tale</title>
		<link>https://byorgey.wordpress.com/2013/04/29/monad-transformers-a-cautionary-tale/</link>
		<comments>https://byorgey.wordpress.com/2013/04/29/monad-transformers-a-cautionary-tale/#comments</comments>
		<pubDate>Mon, 29 Apr 2013 21:03:48 +0000</pubDate>
		<dc:creator>Brent</dc:creator>
				<category><![CDATA[haskell]]></category>
		<category><![CDATA[commutative]]></category>
		<category><![CDATA[monad]]></category>
		<category><![CDATA[random]]></category>
		<category><![CDATA[transformer]]></category>

		<guid isPermaLink="false">http://byorgey.wordpress.com/?p=1084</guid>
		<description><![CDATA[When writing the code in my previous post, I wanted to have a monad which combined the ability to generate random numbers with the ability to fail. Naturally, I decided to use RandT Maybe. But when I tried to write &#8230; <a href="https://byorgey.wordpress.com/2013/04/29/monad-transformers-a-cautionary-tale/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="https://stats.wordpress.com/b.gif?host=byorgey.wordpress.com&#038;blog=1152889&#038;post=1084&#038;subd=byorgey&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>When writing the code in <a href="http://byorgey.wordpress.com/2013/04/25/random-binary-trees-with-a-size-limited-critical-boltzmann-sampler-2/">my previous post</a>, I wanted to have a monad which combined the ability to generate random numbers with the ability to fail. Naturally, I decided to use <code>RandT Maybe</code>. But when I tried to write a failing computation of this type, I got a type error:</p>
<pre><code>    No instance for (MonadPlus (RandT StdGen Maybe))
      arising from a use of `mzero'</code></pre>
<p>It seems that no one ever bothered to add a <code>MonadPlus</code> instance for <code>RandT</code>. Well, that’s easy to fix. Since <code>RandT</code> is just a newtype wrapper around <code>StateT</code> we can even derive a <code>MonadPlus</code> instance automatically using <code>-XGeneralizedNewtypeDeriving</code>. So I modified the <code>MonadRandom</code> package, and everything worked great.</p>
<p>…That is, everything worked great until I started to get some strange behavior—sometimes computations would hang when I expected them to complete quickly. I finally was able to boil it down to the following minimal example. <code>foo</code> succeeds or fails with equal probability; <code>bar</code> reruns <code>foo</code> until it succeeds.</p>
<pre><code><span>foo</span> <span style="color:red;">::</span> <span>RandT</span> <span>StdGen</span> <span>Maybe</span> <span>()</span>
<span>foo</span> <span style="color:red;">=</span> <span style="color:blue;font-weight:bold;">do</span>
  <span>r</span> <span style="color:red;">&lt;-</span> <span>getRandomR</span> <span style="color:red;">(</span><span class="hs-num">0</span><span style="color:red;">,</span><span class="hs-num">1</span><span style="color:red;">)</span>
  <span style="color:blue;font-weight:bold;">if</span> <span>r</span> <span>&lt;</span> <span class="hs-num">1</span><span>/</span><span class="hs-num">2</span> <span style="color:blue;font-weight:bold;">then</span> <span>return</span> <span>()</span> <span style="color:blue;font-weight:bold;">else</span> <span>mzero</span>

<span>bar</span> <span style="color:red;">::</span> <span>RandT</span> <span>StdGen</span> <span>Maybe</span> <span>()</span>
<span>bar</span> <span style="color:red;">=</span> <span>foo</span> <span>`mplus`</span> <span>bar</span></code></pre>
<p>Seems straightforward, right? <code>bar</code> should always succeed pretty much instantly, since there’s only a <img src='https://s-ssl.wordpress.com/latex.php?latex=1%2F2%5En&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='1/2^n' title='1/2^n' class='latex' /> chance that it will have to call <code>foo</code> <img src='https://s-ssl.wordpress.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='n' title='n' class='latex' /> times.</p>
<p>However, this is not what happens: some of the time <code>bar</code> returns instantly as expected, and some of the time it hangs in what seems like an infinite loop! What gives?</p>
<p>Have you figured it out yet? (If you like these sorts of puzzles you might want to stop and see if you can figure out what was going on.) The problem is that the <code>mplus</code> operation for <code>RandT StdGen Maybe</code> runs both of its arguments with the same random seed! In other words, when a computation fails the generator state gets thrown away. And if we think about how monad transformers work this is actually not surprising. We have the following isomorphisms:</p>
<pre><code>   RandT StdGen Maybe ()
== StateT StdGen Maybe ()
== StdGen -&gt; Maybe ((), StdGen)</code></pre>
<p>So when a computation fails you just get <code>Nothing</code>—in particular you <em>don’t</em> get to see what the new <code>StdGen</code> value would have been, so you can’t (say) pass it along to the second argument of <code>mplus</code>. The upshot is that <code>bar</code> succeeds if the first call to <code>foo</code> happens to succeed; otherwise it simply keeps calling <code>foo</code> with the exact same seed and <code>foo</code> keeps failing every time.</p>
<p>The general principle here is that “the effects of inner monad transformers take precedence over the effects of outer transformers”—in this case the failure effect of the inner <code>Maybe</code> takes precedence and causes the random generator state to be lost.</p>
<p>So what I really wanted was <code>MaybeT (Rand StdGen)</code>, which—after adding a <code>MonadRandom</code> instance for <code>MaybeT</code>, now released as <a href="http://hackage.haskell.org/package/MonadRandom"><code>MonadRandom</code>-0.1.9</a>—works perfectly.</p>
<p>The moral of the story: monad transformers aren’t (in general) commutative! Think carefully about what order you want. (I actually <a href="http://byorgey.wordpress.com/2011/02/24/enumerating-linear-inhabitants/">wrote about this once before</a>; you’d think I would have learned my lesson.)</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/byorgey.wordpress.com/1084/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/byorgey.wordpress.com/1084/" /></a> <img alt="" border="0" src="https://stats.wordpress.com/b.gif?host=byorgey.wordpress.com&#038;blog=1152889&#038;post=1084&#038;subd=byorgey&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>https://byorgey.wordpress.com/2013/04/29/monad-transformers-a-cautionary-tale/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="https://0.gravatar.com/avatar/cc113924265dbeb535c8b2fefe4e33ee?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Brent</media:title>
		</media:content>
	</item>
		<item>
		<title>Random binary trees with a size-limited critical Boltzmann sampler</title>
		<link>https://byorgey.wordpress.com/2013/04/25/random-binary-trees-with-a-size-limited-critical-boltzmann-sampler-2/</link>
		<comments>https://byorgey.wordpress.com/2013/04/25/random-binary-trees-with-a-size-limited-critical-boltzmann-sampler-2/#comments</comments>
		<pubDate>Thu, 25 Apr 2013 21:20:31 +0000</pubDate>
		<dc:creator>Brent</dc:creator>
				<category><![CDATA[combinatorics]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[species]]></category>
		<category><![CDATA[Boltzmann]]></category>
		<category><![CDATA[generation]]></category>
		<category><![CDATA[QuickCheck]]></category>
		<category><![CDATA[random]]></category>
		<category><![CDATA[sampler]]></category>
		<category><![CDATA[tree]]></category>

		<guid isPermaLink="false">http://byorgey.wordpress.com/?p=1074</guid>
		<description><![CDATA[Today I’d like to talk about generating random trees. First, some imports and such (this post is literate Haskell). &#62; {-# LANGUAGE GeneralizedNewtypeDeriving #-} &#62; &#62; module BoltzmannTrees where &#62; &#62; import Control.Applicative &#62; import Control.Arrow ((&#38;&#38;&#38;)) &#62; import Control.Lens &#8230; <a href="https://byorgey.wordpress.com/2013/04/25/random-binary-trees-with-a-size-limited-critical-boltzmann-sampler-2/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="https://stats.wordpress.com/b.gif?host=byorgey.wordpress.com&#038;blog=1152889&#038;post=1074&#038;subd=byorgey&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Today I’d like to talk about generating random trees. First, some imports and such (this post is literate Haskell).</p>
<pre><code><span>&gt;</span> <span style="color:green;">{-# LANGUAGE GeneralizedNewtypeDeriving #-}</span>
<span>&gt;</span> 
<span>&gt;</span> <span style="color:blue;font-weight:bold;">module</span> <span>BoltzmannTrees</span> <span style="color:blue;font-weight:bold;">where</span>
<span>&gt;</span> 
<span>&gt;</span> <span style="color:blue;font-weight:bold;">import</span>           <span>Control</span><span>.</span><span>Applicative</span>
<span>&gt;</span> <span style="color:blue;font-weight:bold;">import</span>           <span>Control</span><span>.</span><span>Arrow</span>                  <span style="color:red;">(</span><span style="color:red;">(</span><span>&amp;&amp;&amp;</span><span style="color:red;">)</span><span style="color:red;">)</span>
<span>&gt;</span> <span style="color:blue;font-weight:bold;">import</span>           <span>Control</span><span>.</span><span>Lens</span>                   <span style="color:red;">(</span><span style="color:red;">(</span><span>??</span><span style="color:red;">)</span><span style="color:red;">)</span>
<span>&gt;</span> <span style="color:blue;font-weight:bold;">import</span>           <span>Control</span><span>.</span><span>Monad</span><span>.</span><span>Random</span>
<span>&gt;</span> <span style="color:blue;font-weight:bold;">import</span>           <span>Control</span><span>.</span><span>Monad</span><span>.</span><span>Reader</span>
<span>&gt;</span> <span style="color:blue;font-weight:bold;">import</span>           <span>Control</span><span>.</span><span>Monad</span><span>.</span><span>State</span>
<span>&gt;</span> <span style="color:blue;font-weight:bold;">import</span>           <span>Control</span><span>.</span><span>Monad</span><span>.</span><span>Trans</span><span>.</span><span>Maybe</span>
<span>&gt;</span> <span style="color:blue;font-weight:bold;">import</span>           <span>Data</span><span>.</span><span>List</span>                      <span style="color:red;">(</span><span>sort</span><span style="color:red;">)</span>
<span>&gt;</span> <span style="color:blue;font-weight:bold;">import</span>           <span>Data</span><span>.</span><span>Maybe</span>                     <span style="color:red;">(</span><span>fromJust</span><span style="color:red;">)</span>
<span>&gt;</span> <span style="color:blue;font-weight:bold;">import</span>           <span>System</span><span>.</span><span>Environment</span>             <span style="color:red;">(</span><span>getArgs</span><span style="color:red;">)</span>
</code></pre>
<p>So here’s a simple type of binary tree shapes, containing no data:</p>
<pre><code><span>&gt;</span> <span style="color:blue;font-weight:bold;">data</span> <span>Tree</span> <span style="color:red;">=</span> <span>Leaf</span> <span style="color:red;">|</span> <span>Branch</span> <span>Tree</span> <span>Tree</span>
<span>&gt;</span>   <span style="color:blue;font-weight:bold;">deriving</span> <span>Show</span>
</code></pre>
<p>We’ll count each constructor (<code>Leaf</code> or <code>Branch</code>) as having a size of 1:</p>
<pre><code><span>&gt;</span> <span>size</span> <span style="color:red;">::</span> <span>Tree</span> <span style="color:red;">-&gt;</span> <span>Int</span>
<span>&gt;</span> <span>size</span> <span>Leaf</span> <span style="color:red;">=</span> <span class="hs-num">1</span>
<span>&gt;</span> <span>size</span> <span style="color:red;">(</span><span>Branch</span> <span>l</span> <span>r</span><span style="color:red;">)</span> <span style="color:red;">=</span> <span class="hs-num">1</span> <span>+</span> <span>size</span> <span>l</span> <span>+</span> <span>size</span> <span>r</span>
</code></pre>
<p>Now, suppose we want to randomly generate these trees. This is an entirely reasonable and useful thing to do: perhaps we want to, say, randomly test properties of functions over <code>Tree</code> using <a href="http://hackage.haskell.org/package/QuickCheck"><code>QuickCheck</code></a>. Here’s the simplest, most naïve way to do it:</p>
<pre><code><span>&gt;</span> <span>randomTree</span> <span style="color:red;">::</span> <span style="color:red;">(</span><span>Applicative</span> <span>m</span><span style="color:red;">,</span> <span>MonadRandom</span> <span>m</span><span style="color:red;">)</span> <span style="color:red;">=&gt;</span> <span>m</span> <span>Tree</span>
<span>&gt;</span> <span>randomTree</span> <span style="color:red;">=</span> <span style="color:blue;font-weight:bold;">do</span>
<span>&gt;</span>   <span>r</span> <span style="color:red;">&lt;-</span> <span>getRandom</span>
<span>&gt;</span>   <span style="color:blue;font-weight:bold;">if</span> <span>r</span> <span>&lt;</span> <span style="color:red;">(</span><span class="hs-num">1</span><span>/</span><span class="hs-num">2</span> <span style="color:red;">::</span> <span>Double</span><span style="color:red;">)</span>
<span>&gt;</span>     <span style="color:blue;font-weight:bold;">then</span> <span>return</span> <span>Leaf</span>
<span>&gt;</span>     <span style="color:blue;font-weight:bold;">else</span> <span>Branch</span> <span>&lt;$&gt;</span> <span>randomTree</span> <span>&lt;*&gt;</span> <span>randomTree</span>
</code></pre>
<p>We choose each of the constructors with probability <img src='https://s-ssl.wordpress.com/latex.php?latex=1%2F2&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='1/2' title='1/2' class='latex' />, and recurse in the <code>Branch</code> case.</p>
<p>Now, <a href="http://www.cse.chalmers.se/~rjmh/QuickCheck/manual.html">as is well-known</a>, this works rather poorly. Why is that? Let’s generate 100 random trees and print out their sizes in descending order:</p>
<pre><code><span style="color:gray;">ghci&gt; </span>reverse . sort . map size &lt;$&gt; replicateM 100 randomTree
  [118331,7753,2783,763,237,203,195,163,159,73,65,63,49,41,39,29,29,23,23,21,19,19,15,11,9,9,9,9,7,7,7,5,5,5,5,5,5,5,5,5,3,3,3,3,3,3,3,3,3,3,3,3,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
</code></pre>
<p>As you can see, this is a really weird distribution of sizes. For one thing, we get lots of trees that are very small—in fact, it’s easy to see that we expect about 50 of them to be single leaf nodes. The other weird thing, however, is that we also get some really humongous trees. The above output gets randomly regenerated every time I process this post—so I don’t know exactly what sizes you’ll end up seeing—but it’s a good bet that there is at least one tree with a size greater than <img src='https://s-ssl.wordpress.com/latex.php?latex=10%5E4&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='10^4' title='10^4' class='latex' />. To get an intuitive idea of why this happens, imagine generating the tree in a breadth-first manner. At each new level we have a collection of “active” nodes corresponding to pending recursive calls to <code>randomTree</code>. Each active node generates zero or two new active nodes on the next level with equal probability, so <em>on average</em> the number of active nodes remains the same from level to level. So if we happen to make a lot of <code>Branch</code> choices right off the bat, it may take a long time before the tree “thins out” again. And if this distribution didn’t seem weird enough already, it turns out (though it is far from obvious how to prove this) that the <em>expected</em> size of the generated trees is <em>infinite</em>!</p>
<p>The usual solution with <code>QuickCheck</code> is to use the <code>sized</code> combinator to limit the size of generated structures, but this does not help with the problem of having too many very small trees.</p>
<p>Here’s a (seemingly!) stupid idea. Suppose we want to generate trees of size approximately 100 (say, within 10%). Let’s simply use the above algorithm, but with the following modifications:</p>
<ol style="list-style-type:decimal;">
<li>If we generate a tree of size &lt; 90, throw it away and start over.</li>
<li>If we generate a tree of size &gt; 110, throw it away and start over. As an optimization, however, we will stop <em>as soon as the size goes over 110</em>; that is, we will keep track of the current size while generating and stop early if the size gets too big.</li>
</ol>
<p>Here’s some code. First, a monad onion:</p>
<pre><code><span>&gt;</span> <span style="color:blue;font-weight:bold;">newtype</span> <span>GenM</span> <span>a</span> <span style="color:red;">=</span> <span>GenM</span> 
<span>&gt;</span>     <span style="color:red;">{</span> <span>unGenM</span> <span style="color:red;">::</span> <span>ReaderT</span> <span style="color:red;">(</span><span>Int</span><span style="color:red;">,</span><span>Int</span><span style="color:red;">)</span> <span style="color:red;">(</span><span>StateT</span> <span>Int</span> <span style="color:red;">(</span><span>MaybeT</span> <span style="color:red;">(</span><span>Rand</span> <span>StdGen</span><span style="color:red;">)</span><span style="color:red;">)</span><span style="color:red;">)</span> <span>a</span> <span style="color:red;">}</span>
<span>&gt;</span>   <span style="color:blue;font-weight:bold;">deriving</span> <span style="color:red;">(</span><span>Functor</span><span style="color:red;">,</span> <span>Applicative</span><span style="color:red;">,</span> <span>Monad</span><span style="color:red;">,</span> <span>MonadPlus</span><span style="color:red;">,</span> <span>MonadRandom</span><span style="color:red;">,</span>
<span>&gt;</span>             <span>MonadState</span> <span>Int</span><span style="color:red;">,</span> <span>MonadReader</span> <span style="color:red;">(</span><span>Int</span><span style="color:red;">,</span><span>Int</span><span style="color:red;">)</span><span style="color:red;">)</span>
</code></pre>
<p>The <code>ReaderT</code> holds the min and max allowed sizes; the <code>StateT</code> holds the current size; the <code>MaybeT</code> allows for possible failure (if the tree gets too big or ends up too small), and the <code>Rand StdGen</code> is, of course, for generating random numbers. To run a computation in this monad we take a target size and a tolerance and use them to compute minimum and maximum sizes. (The <code>(??)</code> in the code below is an infix version of <code>flip</code>, defined in the <code>lens</code> package.)</p>
<pre><code><span>&gt;</span> <span>runGenM</span> <span style="color:red;">::</span> <span>Int</span> <span style="color:red;">-&gt;</span> <span>Double</span> <span style="color:red;">-&gt;</span> <span>GenM</span> <span>a</span> <span style="color:red;">-&gt;</span> <span>IO</span> <span style="color:red;">(</span><span>Maybe</span> <span>a</span><span style="color:red;">)</span>
<span>&gt;</span> <span>runGenM</span> <span>targetSize</span> <span>eps</span> <span>m</span> <span style="color:red;">=</span> <span style="color:blue;font-weight:bold;">do</span>
<span>&gt;</span>   <span style="color:blue;font-weight:bold;">let</span> <span>wiggle</span>  <span style="color:red;">=</span> <span>floor</span> <span>$</span> <span>fromIntegral</span> <span>targetSize</span> <span>*</span> <span>eps</span>
<span>&gt;</span>       <span>minSize</span> <span style="color:red;">=</span> <span>targetSize</span> <span style="color:green;">-</span> <span>wiggle</span>
<span>&gt;</span>       <span>maxSize</span> <span style="color:red;">=</span> <span>targetSize</span> <span>+</span> <span>wiggle</span>
<span>&gt;</span>   <span>g</span> <span style="color:red;">&lt;-</span> <span>newStdGen</span>
<span>&gt;</span>   <span>return</span> <span>.</span> <span style="color:red;">(</span><span>evalRand</span> <span>??</span> <span>g</span><span style="color:red;">)</span> <span>.</span> <span>runMaybeT</span> <span>.</span> <span style="color:red;">(</span><span>evalStateT</span> <span>??</span> <span class="hs-num">0</span><span style="color:red;">)</span>
<span>&gt;</span>          <span>.</span> <span style="color:red;">(</span><span>runReaderT</span> <span>??</span> <span style="color:red;">(</span><span>minSize</span><span style="color:red;">,</span> <span>maxSize</span><span style="color:red;">)</span><span style="color:red;">)</span> <span>.</span> <span>unGenM</span>
<span>&gt;</span>          <span>$</span> <span>m</span>
</code></pre>
<p>Here’s the code to try generating a tree: we call the <code>atom</code> function to record the increase in size, and choose between the two constructors with equal probability. <code>atom</code>, in turn, handles failing early if the size gets too big.</p>
<pre><code><span>&gt;</span> <span>genTreeUB</span> <span style="color:red;">::</span> <span>GenM</span> <span>Tree</span>
<span>&gt;</span> <span>genTreeUB</span> <span style="color:red;">=</span> <span style="color:blue;font-weight:bold;">do</span>
<span>&gt;</span>   <span>r</span> <span style="color:red;">&lt;-</span> <span>getRandom</span>
<span>&gt;</span>   <span>atom</span>
<span>&gt;</span>   <span style="color:blue;font-weight:bold;">if</span> <span>r</span> <span>&lt;=</span> <span style="color:red;">(</span><span class="hs-num">1</span><span>/</span><span class="hs-num">2</span> <span style="color:red;">::</span> <span>Double</span><span style="color:red;">)</span>
<span>&gt;</span>     <span style="color:blue;font-weight:bold;">then</span> <span>return</span> <span>Leaf</span>
<span>&gt;</span>     <span style="color:blue;font-weight:bold;">else</span> <span>Branch</span> <span>&lt;$&gt;</span> <span>genTreeUB</span> <span>&lt;*&gt;</span> <span>genTreeUB</span>
<span>&gt;</span> 
<span>&gt;</span> <span>atom</span> <span style="color:red;">::</span> <span>GenM</span> <span>()</span>
<span>&gt;</span> <span>atom</span> <span style="color:red;">=</span> <span style="color:blue;font-weight:bold;">do</span>
<span>&gt;</span>   <span style="color:red;">(</span><span style="color:blue;font-weight:bold;">_</span><span style="color:red;">,</span> <span>maxSize</span><span style="color:red;">)</span> <span style="color:red;">&lt;-</span> <span>ask</span>
<span>&gt;</span>   <span>curSize</span> <span style="color:red;">&lt;-</span> <span>get</span>
<span>&gt;</span>   <span>when</span> <span style="color:red;">(</span><span>curSize</span> <span>&gt;=</span> <span>maxSize</span><span style="color:red;">)</span> <span>mzero</span>
<span>&gt;</span>   <span>put</span> <span style="color:red;">(</span><span>curSize</span> <span>+</span> <span class="hs-num">1</span><span style="color:red;">)</span>
</code></pre>
<p><code>genTreeLB</code> calls <code>genTreeUB</code> and then performs the lower bound check on the size.</p>
<pre><code><span>&gt;</span> <span>genTreeLB</span> <span style="color:red;">::</span> <span>GenM</span> <span>Tree</span>
<span>&gt;</span> <span>genTreeLB</span> <span style="color:red;">=</span> <span style="color:blue;font-weight:bold;">do</span>
<span>&gt;</span>   <span>put</span> <span class="hs-num">0</span>
<span>&gt;</span>   <span>t</span> <span style="color:red;">&lt;-</span> <span>genTreeUB</span>
<span>&gt;</span>   <span>tSize</span> <span style="color:red;">&lt;-</span> <span>get</span>
<span>&gt;</span>   <span style="color:red;">(</span><span>minSize</span><span style="color:red;">,</span> <span style="color:blue;font-weight:bold;">_</span><span style="color:red;">)</span> <span style="color:red;">&lt;-</span> <span>ask</span>
<span>&gt;</span>   <span>guard</span> <span>$</span> <span>tSize</span> <span>&gt;=</span> <span>minSize</span>
<span>&gt;</span>   <span>return</span> <span>t</span>
</code></pre>
<p>Finally, <code>genTree</code> just calls <code>genTreeLB</code> repeatedly until it succeeds.</p>
<pre><code><span>&gt;</span> <span>genTree</span> <span style="color:red;">::</span> <span>GenM</span> <span>Tree</span>
<span>&gt;</span> <span>genTree</span> <span style="color:red;">=</span> <span>genTreeLB</span> <span>`mplus`</span> <span>genTree</span>
</code></pre>
<p>Let’s make sure it works:</p>
<pre><code><span style="color:gray;">ghci&gt; </span>map size . fromJust &lt;$&gt; runGenM 100 0.1 (replicateM 30 genTree)
  [105,91,105,103,107,101,105,93,93,93,95,91,103,91,91,107,105,103,97,95,105,107,93,97,93,103,91,103,101,95]
</code></pre>
<p>Neat! Okay, but surely this is really, really slow, right? We spend a bunch of time just throwing away trees of the wrong size. Before reading on, would you care to guess the asymptotic time complexity to generate a tree of size <img src='https://s-ssl.wordpress.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='n' title='n' class='latex' /> using this algorithm?</p>
<p>And while you think about that, here is a random binary tree of size approximately 1000.</p>
<div style="text-align:center;">
<p><img src="http://byorgey.files.wordpress.com/2013/04/5f8af30e809e8b0fa48b9a1a8eaf3a64.png?w=640" /></p>
</div>
<p>And the answer is… it is <em>linear</em>! That is, it takes <img src='https://s-ssl.wordpress.com/latex.php?latex=O%28n%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='O(n)' title='O(n)' class='latex' /> time to generate a tree of size <img src='https://s-ssl.wordpress.com/latex.php?latex=n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='n' title='n' class='latex' />. This is astounding—it’s the best we could possibly hope for, because of course it takes at least <img src='https://s-ssl.wordpress.com/latex.php?latex=O%28n%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='O(n)' title='O(n)' class='latex' /> time to generate an object of size <img src='https://s-ssl.wordpress.com/latex.php?latex=O%28n%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='O(n)' title='O(n)' class='latex' />. If you don’t believe me, I invite you to run some experiments with this code yourself. I did, and it sure looks linear:</p>
<pre><code>main = do
  [sz] &lt;- getArgs
  Just ts &lt;- runGenM (read sz) 0.1 $ replicateM 1000 genTree
  print . (/fromIntegral n) . fromIntegral . sum . map size $ ts

archimedes :: research/species/boltzmann » time ./GenTree 50
49.682
./GenTree 50  1.37s user 0.01s system 99% cpu 1.387 total
archimedes :: research/species/boltzmann » time ./GenTree 100
99.474
./GenTree 100  3.11s user 0.02s system 99% cpu 3.152 total
archimedes :: research/species/boltzmann » time ./GenTree 200
198.494
./GenTree 200  6.82s user 0.04s system 99% cpu 6.876 total
archimedes :: research/species/boltzmann » time ./GenTree 400
398.798
./GenTree 400  13.08s user 0.08s system 99% cpu 13.208 total
archimedes :: research/species/boltzmann » time ./GenTree 800
795.798
./GenTree 800  25.99s user 0.16s system 99% cpu 26.228 total</code></pre>
<p>The proof of this astounding fact uses some <em>complex analysis</em> which I do not understand; I wish I was joking. Of course, the constant factor can be big, depending on how small you set the “epsilon” allowing for wiggle room around the target size.<sup><a href="#fn1" class="footnoteRef" id="fnref1">1</a></sup> But it is still quite feasible to generate rather large trees (with, say, <img src='https://s-ssl.wordpress.com/latex.php?latex=10%5E5&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='10^5' title='10^5' class='latex' /> nodes).</p>
<p>There is much, much more to say on this topic. I just wanted to start out with a simple example before jumping into more of the technical details and generalizations, which I plan to write about in future posts. I also hope to package this and a bunch of other stuff into a library. In the meantime, you can read Duchon <em>et. al</em><sup><a href="#fn2" class="footnoteRef" id="fnref2">2</a></sup> if you want the details.</p>
<div class="footnotes">
<hr />
<ol>
<li id="fn1">
<p>Actually, if you set epsilon to zero, the asymptotic complexity jumps to <img src='https://s-ssl.wordpress.com/latex.php?latex=O%28n%5E2%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='O(n^2)' title='O(n^2)' class='latex' />.<a href="#fnref1">↩</a></p>
</li>
<li id="fn2">
<p>Duchon, Philippe, <em>et al.</em> “Boltzmann samplers for the random generation of combinatorial structures.” Combinatorics Probability and Computing 13.4-5 (2004): 577-625.<a href="#fnref2">↩</a></p>
</li>
</ol>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/byorgey.wordpress.com/1074/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/byorgey.wordpress.com/1074/" /></a> <img alt="" border="0" src="https://stats.wordpress.com/b.gif?host=byorgey.wordpress.com&#038;blog=1152889&#038;post=1074&#038;subd=byorgey&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>https://byorgey.wordpress.com/2013/04/25/random-binary-trees-with-a-size-limited-critical-boltzmann-sampler-2/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="https://0.gravatar.com/avatar/cc113924265dbeb535c8b2fefe4e33ee?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Brent</media:title>
		</media:content>

		<media:content url="http://byorgey.files.wordpress.com/2013/04/5f8af30e809e8b0fa48b9a1a8eaf3a64.png" medium="image" />
	</item>
		<item>
		<title>Beeminding for fun and profit</title>
		<link>https://byorgey.wordpress.com/2013/04/16/beeminding-for-fun-and-profit/</link>
		<comments>https://byorgey.wordpress.com/2013/04/16/beeminding-for-fun-and-profit/#comments</comments>
		<pubDate>Wed, 17 Apr 2013 03:22:28 +0000</pubDate>
		<dc:creator>Brent</dc:creator>
				<category><![CDATA[grad school]]></category>
		<category><![CDATA[meta]]></category>
		<category><![CDATA[Beeminder]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[goals]]></category>
		<category><![CDATA[productivity]]></category>

		<guid isPermaLink="false">http://byorgey.wordpress.com/?p=1064</guid>
		<description><![CDATA[I’ve been using Beeminder (which I’ve mentioned once before) for a little over six months now. The verdict? Beeminder has changed my life. That sounds dramatic, but I’m not kidding. I am far more productive than I’ve ever been. I’m &#8230; <a href="https://byorgey.wordpress.com/2013/04/16/beeminding-for-fun-and-profit/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="https://stats.wordpress.com/b.gif?host=byorgey.wordpress.com&#038;blog=1152889&#038;post=1064&#038;subd=byorgey&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><!-- -*- compile-command: "BlogLiterately beeminder.markdown &gt; beeminder.html" -*- --></p>
<p>I’ve been using <a href="https://www.beeminder.com/">Beeminder</a> (which I’ve <a href="http://byorgey.wordpress.com/2012/10/13/fogbugz-beeminder-and-pure-functions-in-the-cloud/">mentioned once before</a>) for a little over six months now. The verdict?</p>
<div style='text-align:center;'>
<strong>Beeminder has changed my life.</strong>
</div>
<p></p>
<p>That sounds dramatic, but I’m not kidding. I am far more productive than I’ve ever been. I’m taking better care of myself. I’m finally taking the initiative to act on various long-held intentions (<em>e.g.</em> learning Hebrew). And I no longer have a constant nagging sense of guilt over all the big goals and projects that I <em>ought</em> to be working on more. It’s not for everyone, but I’m sure there are many others for whom it could be similarly transformative.</p>
<div style="text-align:center;">
<div class="figure">
<img src="https://www.beeminder.com/images/logo_hi.png" />
</div>
</div>
<p>So, what is Beeminder? The basic idea is that it helps you keep track of progress on any quantifiable goals, and gives you short-term incentive to stay on track: if you don’t, Beeminder takes your money. But it’s not just about the fear of losing money. Shiny graphs tracking your progress coupled with helpfully concrete short-term goals (“today you need to write 1.3 pages of that paper”) make for excellent positive motivation, too. Another somewhat intangible but important reason it works is that the Beeminder developers are really awesome and responsive, and are sincerely dedicated to helping their users meet goals, not just to making money. (They recently introduced some <a href="http://blog.beeminder.com/premium/">paid premium plans</a> which I happily signed up for, not because I need the premium features, but because I want to support continued development—in fact, I’ve otherwise paid Beeminder only $5 over the past six months!) If you want to know more, I encourage you to <a href="https://www.beeminder.com/overview">read Beeminder’s own overview</a>, which does a much better job of explaining how and why it works.</p>
<p>Six months: quite long enough for the initial “shiny new toy” enthusiasm to wear off, and long enough, I think, to get a good sense of what works for me and what doesn’t. So I’m writing this post in the hope that my experience will be useful or inspiring to others.</p>
<p>So here are some of the ways I’m using it, which I have found to work well. (You can <a href="https://www.beeminder.com/byorgey">see all my beeminder goals here</a>.<sup><a href="#fn1" class="footnoteRef" id="fnref1">1</a></sup>) I hope some of these may inspire you with ways to make yourself more productive, whether you use Beeminder or not.</p>
<ul>
<li>
<p><strong>Big projects</strong></p>
<p>Consistently spending time on big, long-term projects is really hard—at least, it <em>was</em> hard before I started using Beeminder! Now I just make a goal for each project requiring me to spend a certain amount of time on it each week. This helps me stay on track and also gets rid of that nagging guilt—once I’ve done enough to stay on track, I can stop and do other things and not feel guilty about it! I’ve used this to spend a certain amount of time preparing for courses I’m going to teach; I use it for <a href="https://www.beeminder.com/byorgey/goals/research">getting research done</a>, and for <a href="https://www.beeminder.com/byorgey/goals/diagrams">working on diagrams</a>. A year or so ago I posted on Google+ complaining that I needed a <a href="https://plus.google.com/106370345151235486956/posts/5i67T33arE9">scheduling algorithm for my life</a>, and in many ways Beeminder has filled that role. It’s also a great way to get some cold, hard data on how much time I actually spend on various projects (you can start with a “flat” goal and just record data for a while if you don’t know what a reasonable rate for the goal is).</p>
</li>
<li>
<p><strong>Reading and writing projects</strong></p>
<p>There’s no way I ever would have gotten my <a href="https://www.beeminder.com/byorgey/goals/thesis-proposal">thesis proposal</a> written without Beeminder. The important thing to note is that the goal was based on <em>page count</em> rather than time spent. (The “Odometer” goal type is useful for this sort of thing.) This forced me to actually get real writing done, rather than frittering time away adjusting the kerning or whatever. Interestingly, it also forced me to get creative about padding the page count, by pasting in text I’d already written before (from blog posts, grant proposals, <em>etc.</em>). In the end, reusing text I’d written before and then editing it was a much better use of my time than writing everything from scratch, but for whatever reason I’m not sure my perfectionist self would have done it without the pressure of “you have to write two pages in the next three hours OR ELSE”.</p>
<p>I’ve also made goals for reviews I’ve been asked to do, again using an “Odometer” goal to track page numbers.</p>
<p>I also have a <a href="https://www.beeminder.com/byorgey/goals/blogging">goal to write blog posts</a> with a certain frequency on either of my <a href="http://byorgey.wordpress.com">two</a> <a href="http://mathlesstraveled.com">blogs</a>. (In fact, I’m finally finishing this blog post because otherwise in about an hour I’m going to owe Beeminder $5!)</p>
</li>
<li>
<p><strong>Learning</strong></p>
<p>I have long intended to learn to read Hebrew, but it never seemed like the “right time”. I finally admitted that there will never be a “right time”, and just started<sup><a href="#fn2" class="footnoteRef" id="fnref2">2</a></sup>. Starting is one thing; continuing to regularly study after the initial excitement has worn off is only possible because of my <a href="https://www.beeminder.com/byorgey/goals/heb">Beeminder goal</a>, which also serves as a check on discouragement. It will be a long time before I am any good at reading Hebrew; but in the meantime I am motivated by logging time on my goal.</p>
<p>I use <a href="http://ankisrs.net/">anki</a> for memorizing all sorts of things—ancient Greek and Hebrew vocabulary, recipes, emacs commands, and names and faces of students. To help me stay on track reviewing flash cards, I have a <a href="https://www.beeminder.com/byorgey/goals/anki">Beeminder goal to review 100 Anki cards a day</a>. Recently, the number of cards coming due each day started dropping significantly below 100, so instead of lessening the Beeminder goal I decided to start learning some geography (flags, countries, capitals, <em>etc.</em>) which has been a lot of fun.</p>
</li>
<li>
<p><strong>Productivity</strong></p>
<p>I have a number of goals directly intended to increase my productivity.</p>
<ul>
<li>
<p>I use <a href="http://www.fogcreek.com/fogbugz/">FogBugz</a> to keep track of all my tasks and todos. I use three different Beeminder goals in relation to FogBugz:</p>
<ul>
<li>As <a href="http://byorgey.wordpress.com/2012/10/13/fogbugz-beeminder-and-pure-functions-in-the-cloud/">described in previous blog post</a>, I have one goal to close a certain number of cases per day (currently 4 per day, which is historically about average for me). This goal is automatically updated every time I close a case in FogBugz.</li>
<li>When I get an email requiring me to act or respond in some way, I very often just forward it to FogBugz to deal with later. So I have <a href="https://www.beeminder.com/byorgey/goals/fb-inbox">a goal to spend a certain amount of time</a> dealing with cases in my FogBugz inbox; otherwise it’s too easy to just let these rot.</li>
<li>It’s way too easy to ignore todo items which have no real deadline and are somehow distasteful, intimidating, or both. To help overcome this inertia, I’ve come up with something that works fairly well. I have a <a href="https://www.beeminder.com/byorgey/goals/fogbugz-review">Beeminder goal to spend a certain amount of time doing “FogBugz review”</a>. It works like this: I have a certain query defined in FogBugz which shows me the five <em>least recently edited</em> open tickets. When working on review, I must pick one of these five cases and make <em>some sort of progress</em> on it (it’s perfectly fine if I don’t complete it). After making some progress I add a note to the ticket explaining what I did. This both helps me pick up where I left off next time I come to work on the ticket, and makes the ticket automatically drop out of the review query, since it has now been edited. I then look at the top five tickets again (including some new ticket that has now moved into the top five), choose one, and repeat.</li>
</ul>
</li>
<li>
<p>I have found that I am much more productive if at the start of each day I intentionally plan out the rest of the day, recalling the things I have scheduled and deciding how to spend the remaining unscheduled time—consulting FogBugz and Beeminder to decide what my priorities should be for the day and how much time to spend on each. To force myself to do this consistently, I of course made a <a href="https://www.beeminder.com/byorgey/goals/plan">Beeminder goal</a> to do this planning a certain number of days each week. The catch is that I have to do the planning <em>before</em> checking email, Facebook, or IRC, or else the planning only counts for half a day.<sup><a href="#fn3" class="footnoteRef" id="fnref3">3</a></sup></p>
</li>
<li>
<p>Another thing which I’ve found helps my productivity is to <em>turn off my computer before going to bed</em>. The choices I make when I first get up tend to have a ripple effect on the rest of the day. If my computer is on when I first get up, it’s very tempting to immediately start aimlessly checking email; if it’s off, it’s that much easier to make deliberate choices about how to begin my day. The important point here is that I’ve made a <a href="https://www.beeminder.com/byorgey/goals/shutdown"><em>positive</em> goal (to turn off my computer)</a> instead of <em>negative</em> goal (to spend less than X amount of time checking email, <em>etc.</em>, in the morning). I’ve found that negative goals don’t work nearly as well: they are far less motivating, and psychologically speaking it’s too easy to lie to Beeminder by <em>neglecting to report</em> data—by contrast, actively lying by <em>submitting false data</em> is much more difficult.</p>
</li>
</ul>
</li>
<li>
<p><strong>Personal goals</strong></p>
<p>Last but not least, I now take better care of myself and my stuff in some simple but important ways. I have <a href="https://www.beeminder.com/byorgey/goals/floss">flossed</a> more in the last six months than the rest of my life put together. I trim <a href="https://www.beeminder.com/byorgey/goals/beard">my beard</a> and my <a href="https://www.beeminder.com/byorgey/goals/toenails">toenails</a> more regularly, <a href="https://www.beeminder.com/byorgey/goals/allergy-meds">take allergy medication</a> almost every day, <a href="https://www.beeminder.com/byorgey/goals/bike-care">take care of my bike</a> by inflating the tires and greasing the chain, and <a href="https://www.beeminder.com/byorgey/goals/clean">clean around</a> <a href="https://www.beeminder.com/byorgey/goals/clean-the-shower">the house</a> (which my wife loves).</p>
</li>
</ul>
<p>So there you have it. If you end up trying Beeminder, or come up with some cool goal-based life hacks, or just have questions, I’d love to hear from you!</p>
<div class="footnotes">
<hr />
<ol>
<li id="fn1">
<p>You’ll notice that some of my goals are private/hidden. Mostly these are personal or relate to religious commitments, and for various reasons I’d rather not broadcast them to the whole Internet—but at the same time, I have no secrets and would be glad to discuss them with anyone who’s interested.<a href="#fnref1">↩</a></p>
</li>
<li id="fn2">
<p>Well, at the time it was a way of <a href="http://www.structuredprocrastination.com/">procrastinating from working on my thesis proposal</a><a href="#fnref2">↩</a></p>
</li>
<li id="fn3">
<p>This is completely self-enforced, of course, but it’s ten times harder to actively choose to lie to Beeminder (which I have never done) than it was to “just check a few emails first” before I had any sort of external accountability.<a href="#fnref3">↩</a></p>
</li>
</ol>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/byorgey.wordpress.com/1064/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/byorgey.wordpress.com/1064/" /></a> <img alt="" border="0" src="https://stats.wordpress.com/b.gif?host=byorgey.wordpress.com&#038;blog=1152889&#038;post=1064&#038;subd=byorgey&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>https://byorgey.wordpress.com/2013/04/16/beeminding-for-fun-and-profit/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
	
		<media:content url="https://0.gravatar.com/avatar/cc113924265dbeb535c8b2fefe4e33ee?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Brent</media:title>
		</media:content>

		<media:content url="https://www.beeminder.com/images/logo_hi.png" medium="image" />
	</item>
		<item>
		<title>Introducing diagrams-haddock</title>
		<link>https://byorgey.wordpress.com/2013/03/23/introducing-diagrams-haddock/</link>
		<comments>https://byorgey.wordpress.com/2013/03/23/introducing-diagrams-haddock/#comments</comments>
		<pubDate>Sun, 24 Mar 2013 00:24:18 +0000</pubDate>
		<dc:creator>Brent</dc:creator>
				<category><![CDATA[diagrams]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[writing]]></category>
		<category><![CDATA[documentation]]></category>
		<category><![CDATA[Haddock]]></category>
		<category><![CDATA[images]]></category>

		<guid isPermaLink="false">http://byorgey.wordpress.com/?p=1060</guid>
		<description><![CDATA[I am quite pleased to announce the release of diagrams-haddock, a tool enabling you to easily include programmatically generated diagrams in your Haddock documentation. Why might you want to do this? “A picture is worth a thousand words”—in many instances &#8230; <a href="https://byorgey.wordpress.com/2013/03/23/introducing-diagrams-haddock/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="https://stats.wordpress.com/b.gif?host=byorgey.wordpress.com&#038;blog=1152889&#038;post=1060&#038;subd=byorgey&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I am quite pleased to announce the release of <a href="http://hackage.haskell.org/package/diagrams%2Dhaddock">diagrams-haddock</a>, a tool enabling you to <strong>easily include programmatically generated diagrams in your Haddock documentation</strong>. Why might you want to do this? “A picture is worth a thousand words”—in many instances a diagram or illustration can dramatically increase the comprehension of users reading your library’s documentation. The <a href="http://projects.haskell.org/diagrams"><code>diagrams</code> project</a> itself will be using this for documentation, beginning with the <a href="http://hackage.haskell.org/package/diagrams%2Dcontrib"><code>diagrams-contrib</code> package</a> (for example, check out the <a href="http://hackage.haskell.org/packages/archive/diagrams-contrib/0.6.1/doc/html/Diagrams-TwoD-Path-IteratedSubset.html">documentation for Diagrams.TwoD.Path.IteratedSubset</a>). But inline images can benefit the documentation of just about any library.</p>
<p>Before jumping into a more detailed example, here are the main selling points of <code>diagrams-haddock</code>:</p>
<ol style="list-style-type:decimal;">
<li>
<p>You get to create arbitrary images to enhance your documentation, using the powerful <a href="http://projects.haskell.org/diagrams"><code>diagrams</code> framework</a>.</p>
</li>
<li>
<p>The code for your images goes <em>right into your source files themselves</em>, alongside the documentation—there is no need to maintain a bunch of auxiliary files, or (heaven forbid) multiple versions of your source files.</p>
</li>
<li>
<p>Images are regenerated when, and <em>only</em> when, their definition changes—so you can include many diagrams in your documentation without having to recompile all of them every time you make a change to just one.</p>
</li>
<li>
<p>You have to do a little bit of work to integrate the generated images into your Cabal package, but it’s relatively simple and you only have to do it once per package. No one else needs to have <code>diagrams-haddock</code> installed in order to build your documentation with the images (this includes Hackage).</p>
</li>
</ol>
<p>So, how does it work? (For full details, consult the <a href="https://github.com/diagrams/diagrams-haddock/blob/master/README.md"><code>diagrams-haddock</code> documentation</a>.) Suppose we have some Haddock documentation that looks like this:</p>
<pre><code><span style="color:green;">-- | The foozle function takes a widget and turns it into an</span>
<span style="color:green;">--   infinite list of widgets which alternate between red and</span>
<span style="color:green;">--   yellow.</span>
<span style="color:green;">--</span>
<span>foozle</span> <span style="color:red;">::</span> <span>Widget</span> <span style="color:red;">-&gt;</span> <span style="color:red;">[</span><span>Widget</span><span style="color:red;">]</span>
<span>foozle</span> <span style="color:red;">=</span> <span>...</span></code></pre>
<p>It would be really nice to illustrate this with a picture, don’t you think? First, we insert an image placeholder like so:</p>
<pre><code><span style="color:green;">-- | The foozle function takes a widget and turns it into an</span>
<span style="color:green;">--   infinite list of widgets which alternate between red and</span>
<span style="color:green;">--   yellow.</span>
<span style="color:green;">--</span>
<span style="color:green;">--   &lt;&lt;dummy#diagram=foozleDia&amp;width=300&gt;&gt;</span>
<span style="color:green;">--</span>
<span>foozle</span> <span style="color:red;">::</span> <span>Widget</span> <span style="color:red;">-&gt;</span> <span style="color:red;">[</span><span>Widget</span><span style="color:red;">]</span>
<span>foozle</span> <span style="color:red;">=</span> <span>...</span></code></pre>
<p>It doesn’t matter what we put in place of <code>dummy</code>; <code>diagrams-haddock</code> is going to shortly replace it anyway. The stuff following the <code>#</code> is a list of parameters to <code>diagrams-haddock</code>: we tell it to insert here an image built from the diagram called <code>foozleDia</code>, and that it should have a width of 300 pixels.</p>
<p>Now we just have to give a definition for <code>foozleDia</code>, which we do simply by creating a code block (set off with bird tracks) in a comment:</p>
<pre><code><span style="color:green;">-- | The foozle function takes a widget and turns it into an</span>
<span style="color:green;">--   infinite list of widgets which alternate between red and</span>
<span style="color:green;">--   yellow.</span>
<span style="color:green;">--</span>
<span style="color:green;">--   &lt;&lt;dummy#diagram=foozleDia&amp;width=300&gt;&gt;</span>
<span style="color:green;">--</span>
<span>foozle</span> <span style="color:red;">::</span> <span>Widget</span> <span style="color:red;">-&gt;</span> <span style="color:red;">[</span><span>Widget</span><span style="color:red;">]</span>
<span>foozle</span> <span style="color:red;">=</span> <span>...</span>

<span style="color:green;">-- &gt; widget =</span>
<span style="color:green;">-- &gt;   (  stroke (circle 1.25 &lt;&gt; circle 0.75 # reversePath)</span>
<span style="color:green;">-- &gt;   &lt;&gt; mconcat (iterateN 10 (rotateBy (1/10)) (square 0.5 # translateX 1.3))</span>
<span style="color:green;">-- &gt;   )</span>
<span style="color:green;">-- &gt;   # lw 0</span>
<span style="color:green;">-- &gt;</span>
<span style="color:green;">-- &gt; foozleDia =</span>
<span style="color:green;">-- &gt;   hcat' with {sep = 2}</span>
<span style="color:green;">-- &gt;   [ widget # fc black</span>
<span style="color:green;">-- &gt;   , hrule 4 # alignR &lt;&gt; triangle 1 # rotateBy (-1/4) # fc black</span>
<span style="color:green;">-- &gt;   , hcat' with {sep = 0.5} (zipWith fc (cycle [red, yellow]) (replicate 6 widget))</span>
<span style="color:green;">-- &gt;   ]</span></code></pre>
<p>Note that this definition for <code>foozleDia</code> isn’t in a Haddock comment, so it won’t be typeset in the Haddock output. (However, if you want users reading your documentation to see the code used to generate the pictures—as, <em>e.g.</em>, we often do in the documentation for <code>diagrams</code> itself—it’s as simple as sticking the definitions in a Haddock comment.) It also doesn’t have to go right after the definition of <code>foozle</code>—for example, we could stick it all the way at the end of the source file if we didn’t want it cluttering up the code.</p>
<p>Now we simply run <code>diagrams-haddock</code> on our file (or on the whole <code>Cabal</code> project), and it will generate an appropriate SVG image and replace <code>&lt;&lt;dummy#...&gt;&gt;</code> with something like <code>&lt;&lt;diagrams/foozleDia.svg#...&gt;&gt;</code>. The Haddock documentation now displays something like</p>
<div style="text-align:center;">
<p><img src="http://byorgey.files.wordpress.com/2013/03/97bc1304d6782598753944eab0ba9252.png?w=640" /></p>
</div>
<p>after the documentation for <code>foozle</code>. Hooray! Note that <code>diagrams-haddock</code> only replaces the stuff before the <code>#</code> (the clever bit is that browsers will ignore everything after the <code>#</code>). Running <code>diagrams-haddock</code> again at this point will do nothing. If we change the definition of <code>foozleDia</code> and then rerun <code>diagrams-haddock</code>, it will regenerate the image.</p>
<p>Okay, but how will others (or, for that matter, Hackage) be able to see the diagram for <code>foozle</code> when they build the documentation, without needing <code>diagrams-haddock</code> themselves? It’s actually fairly straightforward—we simply include the generated images in the source tarball, and tell <code>cabal</code> to copy the images in alongside the documentation when it is built, using either a custom <code>Setup.hs</code>, or (once it is released and sufficiently ubiquitous) the new <code>extra-html-files:</code> field in the <code>.cabal</code> file. The <a href="https://github.com/diagrams/diagrams-haddock/blob/master/README.md"><code>diagrams-haddock</code> documentation</a> has full details with step-by-step instructions.</p>
<p>I hope this silly example has piqued your interest; again, for full details please consult the <a href="https://github.com/diagrams/diagrams-haddock/blob/master/README.md"><code>diagrams-haddock</code> documentation</a>. Now go forth and illustrate your documentation!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/byorgey.wordpress.com/1060/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/byorgey.wordpress.com/1060/" /></a> <img alt="" border="0" src="https://stats.wordpress.com/b.gif?host=byorgey.wordpress.com&#038;blog=1152889&#038;post=1060&#038;subd=byorgey&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>https://byorgey.wordpress.com/2013/03/23/introducing-diagrams-haddock/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="https://0.gravatar.com/avatar/cc113924265dbeb535c8b2fefe4e33ee?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Brent</media:title>
		</media:content>

		<media:content url="http://byorgey.files.wordpress.com/2013/03/97bc1304d6782598753944eab0ba9252.png" medium="image" />
	</item>
		<item>
		<title>BlogLiterately 0.6</title>
		<link>https://byorgey.wordpress.com/2013/03/10/blogliterately-0-6-2/</link>
		<comments>https://byorgey.wordpress.com/2013/03/10/blogliterately-0-6-2/#comments</comments>
		<pubDate>Sun, 10 Mar 2013 17:38:15 +0000</pubDate>
		<dc:creator>Brent</dc:creator>
				<category><![CDATA[haskell]]></category>
		<category><![CDATA[writing]]></category>
		<category><![CDATA[BlogLiterately]]></category>
		<category><![CDATA[options]]></category>
		<category><![CDATA[release]]></category>

		<guid isPermaLink="false">http://byorgey.wordpress.com/?p=1053</guid>
		<description><![CDATA[I’m very proud to announce the release of BlogLiterately version 0.6, a tool for formatting and uploading blog posts, including syntax highlighting, generation of ghci sessions, LaTeX support, automatic image uploading, and more. tl;dr: Instead of cumbersomely specifying all options &#8230; <a href="https://byorgey.wordpress.com/2013/03/10/blogliterately-0-6-2/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="https://stats.wordpress.com/b.gif?host=byorgey.wordpress.com&#038;blog=1152889&#038;post=1053&#038;subd=byorgey&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I’m very proud to announce the release of <a href="http://hackage.haskell.org/package/BlogLiterately-0.6"><code>BlogLiterately</code> version 0.6</a>, a tool for formatting and uploading blog posts, including syntax highlighting, generation of <code>ghci</code> sessions, LaTeX support, automatic image uploading, and more.</p>
<p><strong>tl;dr</strong>: Instead of cumbersomely specifying all options on the command-line, you can now specify options using a combination of “profiles” (<em>e.g.</em> for common sets of options such as blog URL and password) and options embedded within the <code>.markdown</code> or <code>.lhs</code> documents themselves (<em>e.g.</em> for post-specific options like title, tags, and categories).</p>
<p>There are a few other changes and improvements as well. For more information, see the <a href="http://byorgey.wordpress.com/blogliterately/">documentation</a> or keep reading below!</p>
<h2 id="specifying-options">Specifying options</h2>
<p>With previous releases, uploading a post usually went something like this:</p>
<pre><code>BlogLiterately MyPost.md --blog "http://my.blog.url/xmlrpc.php \
  --user me --password 1234567 --postid 9999 --title "My awesome post" \
  --tag tag1 --tag tag2 --tag tag3 --category Stuff \
  --category OtherStuff --ghci --wplatex</code></pre>
<p>which is incredibly tedious and error-prone. Now we do things the Right Way &#8482;. First, you can create one or more <em>profiles</em>, specifying a common set of options that can be referred to by name. For example, you might have a profile for a particular blog, or a profile for a particular type of post which always needs the same options. Suppose we put this in <code>$HOME/.BlogLiterately/foo.cfg</code> (or in something like <code>C:/Documents And Settings/user/Application Data/BlogLiterately/foo.cfg</code> on Windows):</p>
<pre><code>blog        = http://my.blog.url/xmlrpc.php
user        = me
password    = 1234567
wplatex     = true</code></pre>
<p>Now the previous command line is reduced to</p>
<pre><code>BlogLiterately MyPost.md -P foo --postid 9999 --title "My awesome post" \
  --tag tag1 --tag tag2 --tag tag3 --category Stuff \
  --category OtherStuff --ghci</code></pre>
<p>which is already a big improvement! But it doesn’t stop there. The title, tags, categories, and other such things are really inherent to the post itself; there’s no reason they should go on the command line. So, we add this indented block somewhere in <code>MyPost.md</code> (probably near the top, though it doesn’t matter):</p>
<pre><code>    [BLOpts]
    profile    = foo
    postid     = 9999
    title      = "My awesome post"
    tags       = tag1, tag2, tag3
    categories = Stuff, OtherStuff
    ghci       = true</code></pre>
<p>And now we only have to write</p>
<pre><code>BlogLiterately MyPost.md</code></pre>
<p>with no options on the command line at all! Notice how we can even specify which profile to use in the <code>[BLOpts]</code> block. When we’re satisfied with the post we can publish it with</p>
<pre><code>BlogLiterately MyPost.md --publish</code></pre>
<h2 id="generating-html-only">Generating HTML only</h2>
<p>In the past, to get a “preview” version of the HTML output written to stdout, all you had to do was omit a <code>--blog</code> option. However, if you specify a profile with a <code>blog</code> field as in the above example, this is more problematic. For this reason, a new option <code>--html-only</code> has been added. When this option is specified, nothing will be uploaded, and the HTML output written to stdout.</p>
<h2 id="changes-to-transforms">Changes to Transforms</h2>
<p>In order to make the above features possible, the definition of <code>Transform</code> has changed. This only affects those users who have created their own custom transformations. The definition used to be</p>
<pre><code><span style="color:blue;font-weight:bold;">data</span> <span>Transform</span>
  <span style="color:red;">=</span> <span>Transform</span>
    <span style="color:red;">{</span> <span>getTransform</span> <span style="color:red;">::</span> <span>BlogLiterately</span> <span style="color:red;">-&gt;</span> <span>Kleisli</span> <span>IO</span> <span>Pandoc</span> <span>Pandoc</span>
    <span style="color:red;">,</span> <span>xfCond</span>       <span style="color:red;">::</span> <span>BlogLiterately</span> <span style="color:red;">-&gt;</span> <span>Bool</span>
    <span style="color:red;">}</span></code></pre>
<p>that is, a <code>Transform</code> was a transformation on <code>Pandoc</code> documents, parameterized by an options record and able to have effects in the <code>IO</code> monad. The definition is now</p>
<pre><code><span style="color:blue;font-weight:bold;">data</span> <span>Transform</span>
  <span style="color:red;">=</span> <span>Transform</span>
    <span style="color:red;">{</span> <span>getTransform</span> <span style="color:red;">::</span> <span>StateT</span> <span style="color:red;">(</span><span>BlogLiterately</span><span style="color:red;">,</span> <span>Pandoc</span><span style="color:red;">)</span> <span>IO</span> <span>()</span>
    <span style="color:red;">,</span> <span>xfCond</span>       <span style="color:red;">::</span> <span>BlogLiterately</span> <span style="color:red;">-&gt;</span> <span>Bool</span>
    <span style="color:red;">}</span></code></pre>
<p>meaning that a <code>Transform</code> is able to transform <em>both</em> a <code>Pandoc</code> document <em>and</em> the options record. This is crucial for being able to do things like embedding options within the document itself, because we don’t know all the options until we start processing the document! Also, I switched from using <code>Kleisli</code> arrows to using <code>StateT</code>, since I find it simpler to work with, especially now that multiple pieces of state are involved. For more information and help upgrading, see the <a href="http://hackage.haskell.org/packages/archive/BlogLiterately/latest/doc/html/Text-BlogLiterately-Transform.html">documentation for <code>Text.BlogLiterately.Transform</code></a>.</p>
<h2 id="move-to-github">Move to github</h2>
<p>The other change is that I have moved the <code>BlogLiterately</code> repository from darcshub to github. In general, for small personal projects and miscellaneous sorts of things I use <code>darcs</code> and <code>hub.darcs.net</code>; for larger projects where I want to raise the visibility and encourage contributions from other users, I use github. At some point <code>BlogLiterately</code> crossed the line.</p>
<h2 id="learning-more-and-contacting-me">Learning more, and contacting me</h2>
<p>For more information, see the <a href="http://byorgey.wordpress.com/blogliterately/">full documentation</a>. I’m always happy to receive comments, questions, feature requests, bug reports, and so on, via the <a href="https://github.com/byorgey/BlogLiterately/issues">bug tracker on github</a>, IRC (<code>byorgey</code> on freenode), or email (the same as my IRC nick, at gmail).</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/byorgey.wordpress.com/1053/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/byorgey.wordpress.com/1053/" /></a> <img alt="" border="0" src="https://stats.wordpress.com/b.gif?host=byorgey.wordpress.com&#038;blog=1152889&#038;post=1053&#038;subd=byorgey&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>https://byorgey.wordpress.com/2013/03/10/blogliterately-0-6-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="https://0.gravatar.com/avatar/cc113924265dbeb535c8b2fefe4e33ee?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Brent</media:title>
		</media:content>
	</item>
		<item>
		<title>The Dawn of Software Engineering</title>
		<link>https://byorgey.wordpress.com/2013/01/13/the-dawn-of-software-engineering/</link>
		<comments>https://byorgey.wordpress.com/2013/01/13/the-dawn-of-software-engineering/#comments</comments>
		<pubDate>Sun, 13 Jan 2013 04:01:44 +0000</pubDate>
		<dc:creator>Brent</dc:creator>
				<category><![CDATA[haskell]]></category>

		<guid isPermaLink="false">http://byorgey.wordpress.com/?p=1048</guid>
		<description><![CDATA[The Dawn of Software Engineering: From Turing to DijkstraEdgar G. Daylight Edgar sent me a review copy of his book a while back—it made for quite interesting reading and gave me new perspective on the historical origins of my field. &#8230; <a href="https://byorgey.wordpress.com/2013/01/13/the-dawn-of-software-engineering/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="https://stats.wordpress.com/b.gif?host=byorgey.wordpress.com&#038;blog=1152889&#038;post=1048&#038;subd=byorgey&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><!-- -*- compile-command: "BlogLiteratelyD dawn-of-software.markdown &gt; dawn-of-software.html" -*- --></p>
<div style="float:right;margin-left:20px;">
<p><a href="http://www.dijkstrascry.com/dawn"> <img src="http://www.dijkstrascry.com/sites/default/files/dawn_front_377.jpg" /> </a>
</div>
<p><strong><a href="http://www.dijkstrascry.com/dawn">The Dawn of Software Engineering: From Turing to Dijkstra</a></strong><br /><em>Edgar G. Daylight</em></p>
<p>Edgar sent me a review copy of his book a while back—it made for quite interesting reading and gave me new perspective on the historical origins of my field. I daresay many readers of this blog might be interested in giving it a read.</p>
<p>Alan Turing is widely regarded today as the father of digital computers. But as Daylight argues in this fascinating historical account of the development of computer programming as a discipline in the 1950s and 60s, the real story is much more complicated. Turing’s ideas didn’t actually have much influence on the building of the first computers themselves—but did gradually come to influence the practice of writing computer <em>programs</em>.</p>
<p>It will be interesting to compare and contrast this book with <a href="http://www.amazon.com/Turings-Cathedral-Origins-Digital-Universe/dp/0375422773">George Dyson’s book <em>Turing’s Cathedral</em></a>, which I have just begun reading—though I’m not far enough along to make any comparisons yet.</p>
<p>As an aside, I find it interesting that the subfield Dijkstra called “software engineering” —the subfield that was influenced by Turing’s ideas—really seems to comprise what is now known as “programming languages”. “Software engineering” now means something completely different, focusing on the business-oriented, large-scale aspects of building software systems. It’s difficult to imagine “software engineering” these days being influenced by Turing’s ideas (or abstract mathematical ideas, period—though perhaps I am being uncharitable).</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/byorgey.wordpress.com/1048/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/byorgey.wordpress.com/1048/" /></a> <img alt="" border="0" src="https://stats.wordpress.com/b.gif?host=byorgey.wordpress.com&#038;blog=1152889&#038;post=1048&#038;subd=byorgey&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>https://byorgey.wordpress.com/2013/01/13/the-dawn-of-software-engineering/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="https://0.gravatar.com/avatar/cc113924265dbeb535c8b2fefe4e33ee?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Brent</media:title>
		</media:content>

		<media:content url="http://www.dijkstrascry.com/sites/default/files/dawn_front_377.jpg" medium="image" />
	</item>
		<item>
		<title>The algebra of species: primitives</title>
		<link>https://byorgey.wordpress.com/2013/01/07/the-algebra-of-species-primitives/</link>
		<comments>https://byorgey.wordpress.com/2013/01/07/the-algebra-of-species-primitives/#comments</comments>
		<pubDate>Mon, 07 Jan 2013 15:48:22 +0000</pubDate>
		<dc:creator>Brent</dc:creator>
				<category><![CDATA[math]]></category>
		<category><![CDATA[species]]></category>
		<category><![CDATA[algebra]]></category>
		<category><![CDATA[bag]]></category>
		<category><![CDATA[identity]]></category>
		<category><![CDATA[one]]></category>
		<category><![CDATA[primitive]]></category>
		<category><![CDATA[singleton]]></category>
		<category><![CDATA[zero]]></category>

		<guid isPermaLink="false">http://byorgey.wordpress.com/?p=1027</guid>
		<description><![CDATA[[This is the fifth in a series of posts about combinatorial species. Previous posts: And now, back to your regularly scheduled combinatorial species; Decomposing data structures; Combinatorial species definition, Species definition clarification and exercises.] Recall that a species is a &#8230; <a href="https://byorgey.wordpress.com/2013/01/07/the-algebra-of-species-primitives/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="https://stats.wordpress.com/b.gif?host=byorgey.wordpress.com&#038;blog=1152889&#038;post=1027&#038;subd=byorgey&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><!-- -*- compile-command: "BlogLiteratelyD 05-algebra.markdown &gt; 05-algebra.html" -*- --> [<em>This is the fifth in a series of posts about combinatorial species. Previous posts: <a href="http://byorgey.wordpress.com/2012/10/17/and-now-back-to-your-regularly-scheduled-combinatorial-species/">And now, back to your regularly scheduled combinatorial species</a>; <a href="http://byorgey.wordpress.com/2012/10/27/decomposing-data-structures/">Decomposing data structures</a>; <a href="https://byorgey.wordpress.com/2012/11/20/combinatorial-species-definition/">Combinatorial species definition</a>, <a href="http://byorgey.wordpress.com/2012/12/06/species-definition-clarification-and-exercises/">Species definition clarification and exercises</a>.</em>]</p>
<p><a href="http://byorgey.wordpress.com/2012/11/20/combinatorial-species-definition/">Recall</a> that a <em>species</em> is a functor from <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbb%7BB%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbb{B}' title='&#92;mathbb{B}' class='latex' />, the category of finite sets and bijections, to <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbb%7BE%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbb{E}' title='&#92;mathbb{E}' class='latex' />,<sup><a href="#fn1" class="footnoteRef" id="fnref1">1</a></sup> the category of finite sets and total functions. (Equivalently, species are endofunctors on <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbb%7BB%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbb{B}' title='&#92;mathbb{B}' class='latex' />, but in this post I’m going to want to think about them as the former.) That is, a species <img src='https://s-ssl.wordpress.com/latex.php?latex=F&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='F' title='F' class='latex' /> is a mapping sending every set of labels <img src='https://s-ssl.wordpress.com/latex.php?latex=U&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='U' title='U' class='latex' /> to a set of structures <img src='https://s-ssl.wordpress.com/latex.php?latex=F%5BU%5D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='F[U]' title='F[U]' class='latex' />, which also lifts relabelings <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Csigma+%3A+U+%5Cleftrightarrow+V&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;sigma : U &#92;leftrightarrow V' title='&#92;sigma : U &#92;leftrightarrow V' class='latex' /> to functions <img src='https://s-ssl.wordpress.com/latex.php?latex=F%5B%5Csigma%5D+%3A+U+%5Cto+V&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='F[&#92;sigma] : U &#92;to V' title='F[&#92;sigma] : U &#92;to V' class='latex' /> in a way that respects the compositional structure of bijections.</p>
<p>However, as I hinted in a previous post, it’s inconvenient to work directly with this definition in practice. Instead, we use an algebraic theory that lets us compositionally build up certain species from a collection of primitive species and species operations. (It’s important to note that it does <em>not</em> allow us to build <em>all</em> species, but it does allow us to build many of the ones we care about.)</p>
<p>In this post we’ll begin by examining a few natural species to take as primitive.</p>
<ul>
<li>
<p>The <em>zero</em> or <em>empty</em> species, denoted <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbf%7B0%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbf{0}' title='&#92;mathbf{0}' class='latex' />, is the unique species with no structures whatsoever; that is,</p>
<p><img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbf%7B0%7D%5BU%5D+%3D+%5Cemptyset&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbf{0}[U] = &#92;emptyset' title='&#92;mathbf{0}[U] = &#92;emptyset' class='latex' /></p>
<p>and</p>
<p><img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbf%7B0%7D%5B%5Csigma+%3A+U+%5Cleftrightarrow+V%5D+%3D+id_%7B%5Cemptyset%7D+%3A+%5Cmathbf%7B0%7D%5BU%5D+%5Cto+%5Cmathbf%7B0%7D%5BV%5D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbf{0}[&#92;sigma : U &#92;leftrightarrow V] = id_{&#92;emptyset} : &#92;mathbf{0}[U] &#92;to &#92;mathbf{0}[V]' title='&#92;mathbf{0}[&#92;sigma : U &#92;leftrightarrow V] = id_{&#92;emptyset} : &#92;mathbf{0}[U] &#92;to &#92;mathbf{0}[V]' class='latex' />.</p>
<p>Of course, <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbf%7B0%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbf{0}' title='&#92;mathbf{0}' class='latex' /> will turn out to be the identity element for species sum (which I’ll define in my next post, though it’s not hard to figure out what it should mean).</p>
</li>
<li>
<p>The <em>unit</em> species, denoted <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbf%7B1%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbf{1}' title='&#92;mathbf{1}' class='latex' />, is defined by</p>
<p><img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cbegin%7Barray%7D%7Blcl%7D%5Cmathbf%7B1%7D%5B%5Cemptyset%5D+%26%3D%26+%5C%7B%5Cstar%5C%7D+%5C%5C+%5Cmathbf%7B1%7D%5BU%5D+%26%3D%26+%5Cemptyset+%5Cqquad+%28U+%5Cneq+%5Cemptyset%29%5Cend%7Barray%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;begin{array}{lcl}&#92;mathbf{1}[&#92;emptyset] &amp;=&amp; &#92;{&#92;star&#92;} &#92;&#92; &#92;mathbf{1}[U] &amp;=&amp; &#92;emptyset &#92;qquad (U &#92;neq &#92;emptyset)&#92;end{array}' title='&#92;begin{array}{lcl}&#92;mathbf{1}[&#92;emptyset] &amp;=&amp; &#92;{&#92;star&#92;} &#92;&#92; &#92;mathbf{1}[U] &amp;=&amp; &#92;emptyset &#92;qquad (U &#92;neq &#92;emptyset)&#92;end{array}' class='latex' /></p>
<p>That is, there is a unique <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbf%7B1%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbf{1}' title='&#92;mathbf{1}' class='latex' />-structure indexed by the empty set of labels, and no <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbf%7B1%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbf{1}' title='&#92;mathbf{1}' class='latex' />-structures with any positive number of labels. <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbf%7B1%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbf{1}' title='&#92;mathbf{1}' class='latex' /> lifts bijections in the obvious way, sending every bijection to the appropriate identity function.</p>
<p>Some people initially find this definition surprising, expecting something like <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbf%7B1%7D%5BU%5D+%3D+%5C%7B+%5Cstar+%5C%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbf{1}[U] = &#92;{ &#92;star &#92;}' title='&#92;mathbf{1}[U] = &#92;{ &#92;star &#92;}' class='latex' /> for all <img src='https://s-ssl.wordpress.com/latex.php?latex=U&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='U' title='U' class='latex' /> instead. That is indeed a valid species, and we will meet it below; but as I hope you will come to see, it doesn’t deserve the name <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbf%7B1%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbf{1}' title='&#92;mathbf{1}' class='latex' />.</p>
<p>Of course we should also verify that this definition satisfies the requisite functoriality properties, which is not difficult.</p>
<p>More abstractly, for those who know some category theory, it’s worth mentioning that <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbf%7B1%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbf{1}' title='&#92;mathbf{1}' class='latex' /> can be defined as <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbb%7BB%7D%28%5Cemptyset%2C+-%29+%3A+%5Cmathbb%7BB%7D+%5Cto+%5Cmathbb%7BE%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbb{B}(&#92;emptyset, -) : &#92;mathbb{B} &#92;to &#92;mathbb{E}' title='&#92;mathbb{B}(&#92;emptyset, -) : &#92;mathbb{B} &#92;to &#92;mathbb{E}' class='latex' />, that is, the covariant hom-functor sending each finite set <img src='https://s-ssl.wordpress.com/latex.php?latex=U+%5Cin+%5Cmathbb%7BB%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='U &#92;in &#92;mathbb{B}' title='U &#92;in &#92;mathbb{B}' class='latex' /> to the (finite) set of bijections <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cemptyset+%5Cleftrightarrow+U&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;emptyset &#92;leftrightarrow U' title='&#92;emptyset &#92;leftrightarrow U' class='latex' />. (This is why I wanted to think of species as functors <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbb%7BB%7D+%5Cto+%5Cmathbb%7BE%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbb{B} &#92;to &#92;mathbb{E}' title='&#92;mathbb{B} &#92;to &#92;mathbb{E}' class='latex' />. I learned this fact from Yeh (1986).) There is, of course, a unique bijection <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cemptyset+%5Cleftrightarrow+%5Cemptyset&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;emptyset &#92;leftrightarrow &#92;emptyset' title='&#92;emptyset &#92;leftrightarrow &#92;emptyset' class='latex' /> and no bijections <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cemptyset+%5Cleftrightarrow+U&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;emptyset &#92;leftrightarrow U' title='&#92;emptyset &#92;leftrightarrow U' class='latex' /> for nonempty <img src='https://s-ssl.wordpress.com/latex.php?latex=U&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='U' title='U' class='latex' />, thus giving rise to the definition above.</p>
<p>As you might expect, <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbf%7B1%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbf{1}' title='&#92;mathbf{1}' class='latex' /> will be the identity element for species product. Like <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbf%7B1%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbf{1}' title='&#92;mathbf{1}' class='latex' /> itself, species product isn’t defined quite as most people would initially guess. If you haven’t seen it before, you might like to try working out how product can be defined in order to make <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbf%7B1%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbf{1}' title='&#92;mathbf{1}' class='latex' /> an identity element.</p>
</li>
<li>
<p>The <em>singleton</em> species, denoted <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbf%7BX%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbf{X}' title='&#92;mathbf{X}' class='latex' />, is defined by</p>
<p><img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbf%7BX%7D%5BU%5D+%3D+%5Cbegin%7Bcases%7D+U+%26+%7CU%7C+%3D+1+%5C%5C+%5Cemptyset+%26+%5Ctext%7Botherwise%7D+%5Cend%7Bcases%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbf{X}[U] = &#92;begin{cases} U &amp; |U| = 1 &#92;&#92; &#92;emptyset &amp; &#92;text{otherwise} &#92;end{cases}' title='&#92;mathbf{X}[U] = &#92;begin{cases} U &amp; |U| = 1 &#92;&#92; &#92;emptyset &amp; &#92;text{otherwise} &#92;end{cases}' class='latex' /></p>
<p>with lifting of bijections defined in the evident manner. That is, there is a single <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbf%7BX%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbf{X}' title='&#92;mathbf{X}' class='latex' />-structure on a label set of size <img src='https://s-ssl.wordpress.com/latex.php?latex=1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='1' title='1' class='latex' /> (which we identify with the label itself, though we could have also defined <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbf%7BX%7D%5BU%5D+%3D+%5C%7B%5Cstar%5C%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbf{X}[U] = &#92;{&#92;star&#92;}' title='&#92;mathbf{X}[U] = &#92;{&#92;star&#92;}' class='latex' /> when <img src='https://s-ssl.wordpress.com/latex.php?latex=%7CU%7C+%3D+1&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='|U| = 1' title='|U| = 1' class='latex' />), and no <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbf%7BX%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbf{X}' title='&#92;mathbf{X}' class='latex' />-structures indexed by any other number of labels.</p>
<p>As with <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbf%7B1%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbf{1}' title='&#92;mathbf{1}' class='latex' />, we may equivalently define <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbf%7BX%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbf{X}' title='&#92;mathbf{X}' class='latex' /> as a hom-functor, namely <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbf%7BX%7D+%3D+%5Cmathbb%7BB%7D%28%5C%7B%5Cstar%5C%7D%2C+-%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbf{X} = &#92;mathbb{B}(&#92;{&#92;star&#92;}, -)' title='&#92;mathbf{X} = &#92;mathbb{B}(&#92;{&#92;star&#92;}, -)' class='latex' />.</p>
<p>It’s worth noting again that although <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbf%7B1%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbf{1}' title='&#92;mathbf{1}' class='latex' /> and <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbf%7BX%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbf{X}' title='&#92;mathbf{X}' class='latex' /> do “case analysis” on the label set <img src='https://s-ssl.wordpress.com/latex.php?latex=U&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='U' title='U' class='latex' />, they actually only depend on the <em>size</em> of <img src='https://s-ssl.wordpress.com/latex.php?latex=U&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='U' title='U' class='latex' />; indeed, as we <a href="http://byorgey.wordpress.com/2012/12/06/species-definition-clarification-and-exercises/">noted previously</a>, by functoriality this is all they can do.</p>
</li>
<li>
<p>The species of <em>bags</em><sup><a href="#fn2" class="footnoteRef" id="fnref2">2</a></sup>, denoted <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbf%7BE%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbf{E}' title='&#92;mathbf{E}' class='latex' />, is defined by</p>
<p><img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbf%7BE%7D%5BU%5D+%3D+%5C%7BU%5C%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbf{E}[U] = &#92;{U&#92;}' title='&#92;mathbf{E}[U] = &#92;{U&#92;}' class='latex' />,</p>
<p>that is, there is a single <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbf%7BE%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbf{E}' title='&#92;mathbf{E}' class='latex' />-structure on any set of labels <img src='https://s-ssl.wordpress.com/latex.php?latex=U&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='U' title='U' class='latex' />, which we usually identify with the set of labels itself (although we could equivalently define <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbf%7BE%7D%5BU%5D+%3D+%5C%7B%5Cstar%5C%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbf{E}[U] = &#92;{&#92;star&#92;}' title='&#92;mathbf{E}[U] = &#92;{&#92;star&#92;}' class='latex' />). The idea is that an <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbf%7BE%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbf{E}' title='&#92;mathbf{E}' class='latex' />-structure consists solely of a collection of labels, with no imposed ordering whatsoever.</p>
<p>If you want to abuse types slightly, one can define <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbf%7BE%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbf{E}' title='&#92;mathbf{E}' class='latex' /> as a hom-functor too, namely <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbb%7BE%7D%28-%2C%5C%7B%5Cstar%5C%7D%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbb{E}(-,&#92;{&#92;star&#92;})' title='&#92;mathbb{E}(-,&#92;{&#92;star&#92;})' class='latex' />. (Yeh (1986) actually has <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbb%7BB%7D%28-%2C+%5C%7B%5Cstar%5C%7D%29&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbb{B}(-, &#92;{&#92;star&#92;})' title='&#92;mathbb{B}(-, &#92;{&#92;star&#92;})' class='latex' />, but that’s wrong.)</p>
</li>
</ul>
<p>As a summary, here’s a graphic showing <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbf%7B0%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbf{0}' title='&#92;mathbf{0}' class='latex' />-, <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbf%7B1%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbf{1}' title='&#92;mathbf{1}' class='latex' />-, <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbf%7BX%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbf{X}' title='&#92;mathbf{X}' class='latex' />-, and <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbf%7BE%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbf{E}' title='&#92;mathbf{E}' class='latex' />-structures arranged by size (<em>i.e.</em>, the size of the underlying set of labels <img src='https://s-ssl.wordpress.com/latex.php?latex=U&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='U' title='U' class='latex' />): a dot indicates a single structure, and the size of the label set increases as you move to the right.</p>
<div style="text-align:center;">
<div class="figure">
<img src="http://byorgey.files.wordpress.com/2013/01/a3f68a37ffc133339fc3b8f097570c28.png?w=640" />
<p class="caption"></p>
</div>
</div>
<p>Just as a teaser, it turns out that <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbf%7BX%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbf{X}' title='&#92;mathbf{X}' class='latex' /> and <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbf%7BE%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbf{E}' title='&#92;mathbf{E}' class='latex' /> are identity elements for certain binary operations on species as well, though you’ll have to wait to find out which ones!</p>
<p>Next up, addition!</p>
<h2 id="references">References</h2>
<p>Yeh, Yeong-Nan. 1986. “The calculus of virtual species and K-species.” In <em>Combinatoire énumérative</em>, ed. Gilbert Labelle and Pierre Leroux, 1234:351–369. Springer Berlin Heidelberg. <a href="http://dx.doi.org/10.1007/BFb0072525" title="http://dx.doi.org/10.1007/BFb0072525">http://dx.doi.org/10.1007/BFb0072525</a>.</p>
<div class="footnotes">
<hr />
<ol>
<li id="fn1">
<p>Last time I called this category <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbf%7BFinSet%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbf{FinSet}' title='&#92;mathbf{FinSet}' class='latex' />, but <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbb%7BE%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbb{E}' title='&#92;mathbb{E}' class='latex' /> is more concise and matches the species literarure.<a href="#fnref1">↩</a></p>
</li>
<li id="fn2">
<p>The species literature calls this the species of <em>sets</em>, but that’s misleading to computer scientists, who expect the word “set” to imply that elements cannot be repeated.<a href="#fnref2">↩</a></p>
</li>
</ol>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/byorgey.wordpress.com/1027/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/byorgey.wordpress.com/1027/" /></a> <img alt="" border="0" src="https://stats.wordpress.com/b.gif?host=byorgey.wordpress.com&#038;blog=1152889&#038;post=1027&#038;subd=byorgey&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>https://byorgey.wordpress.com/2013/01/07/the-algebra-of-species-primitives/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="https://0.gravatar.com/avatar/cc113924265dbeb535c8b2fefe4e33ee?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Brent</media:title>
		</media:content>

		<media:content url="http://byorgey.files.wordpress.com/2013/01/a3f68a37ffc133339fc3b8f097570c28.png" medium="image" />
	</item>
		<item>
		<title>Diagrams 0.6</title>
		<link>https://byorgey.wordpress.com/2012/12/11/diagrams-0-6/</link>
		<comments>https://byorgey.wordpress.com/2012/12/11/diagrams-0-6/#comments</comments>
		<pubDate>Wed, 12 Dec 2012 02:32:05 +0000</pubDate>
		<dc:creator>Brent</dc:creator>
				<category><![CDATA[diagrams]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[projects]]></category>
		<category><![CDATA[EDSL]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[release]]></category>

		<guid isPermaLink="false">http://byorgey.wordpress.com/?p=1023</guid>
		<description><![CDATA[I am pleased to announce the release of version 0.6 of diagrams, a full-featured framework and embedded domain-specific language for declarative drawing. Check out the gallery for examples of what it can do! Highlights of this release include: Diagrams now &#8230; <a href="https://byorgey.wordpress.com/2012/12/11/diagrams-0-6/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="https://stats.wordpress.com/b.gif?host=byorgey.wordpress.com&#038;blog=1152889&#038;post=1023&#038;subd=byorgey&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I am pleased to announce the release of version 0.6 of <a href="http://projects.haskell.org/diagrams">diagrams</a>, a full-featured framework and embedded domain-specific language for declarative drawing. Check out the <a href="http://projects.haskell.org/diagrams/gallery.html">gallery</a> for examples of what it can do!</p>
<div style='text-align:center;'>
<p><a href="http://mathlesstraveled.com/2012/11/05/more-factorization-diagrams/"><img src="http://mathlesstraveled.files.wordpress.com/2012/11/77455a72e05ff905c9c81cd250b45fa6.png?w=640" /></a></p>
<p><a href="http://projects.haskell.org/diagrams/gallery/PythagoreanTree.html"><img src="http://projects.haskell.org/diagrams/gallery/PythagoreanTree.png" /></a></p>
</div>
<p>Highlights of this release include:</p>
<ul>
<li>
<p>Diagrams now comes with a native-Haskell <a href="http://hackage.haskell.org/package/diagrams%2Dsvg">SVG backend</a> by default. If you were holding off on trying diagrams because you couldn’t install cairo, you no longer have an excuse!</p>
</li>
<li>
<p>Proper support for subdiagrams: previous versions of diagrams-core had a mechanism for associating names with a pair of a location and an envelope. Now, names are associated with actual subdiagrams (including their location and envelope, along with all the other information stored by a diagram). This enables cool techniques like constructing a diagram in order to position its subelements and then taking it apart again, or constructing animations via keyframing.</p>
</li>
<li>
<p>Traces: in addition to an envelope, each diagram now stores a “trace”, which is like an embedded raytracer: given any ray (represented by a base point and a vector), the trace computes the closest point of intersection with the diagram along the ray. This is useful for determining points on the boundary of a diagram, <em>e.g.</em> when drawing arrows between diagrams.</p>
</li>
<li>
<p>The core data structure underlying diagrams has been completely refactored and split out into its own separate package, <a href="http://hackage.haskell.org/package/dual%2Dtree">dual-tree</a>.</p>
</li>
<li>
<p>Support for GHC 7.6.</p>
</li>
<li>
<p>Many more new features, bug fixes, and improvements! See the <a href="http://projects.haskell.org/diagrams/releases.html">release notes</a> for complete details, and the <a href="http://www.haskell.org/haskellwiki/Diagrams/Migrate0.6">diagrams wiki</a> for help migrating from 0.5 to 0.6.</p>
</li>
</ul>
<h2 id="try-it-out">Try it out</h2>
<p>For the truly impatient:</p>
<pre><code>cabal install diagrams</code></pre>
<p>Diagrams is supported under GHC 7.0 through 7.6, with the exception that the cairo and gtk backends do not build under GHC 7.0 (but the SVG backend does), and the gtk backend does not build under GHC 7.6.</p>
<p>To get started with diagrams, read the <a href="http://projects.haskell.org/diagrams/tutorial/DiagramsTutorial.html">quick tutorial</a>, which will introduce you to the fundamentals of the framework.</p>
<p>For those who are less impatient and want to really dig in and use the power features, read the <a href="http://projects.haskell.org/diagrams/manual/diagrams-manual.html">user manual</a>.</p>
<h2 id="get-involved">Get involved</h2>
<p>Subscribe to the <a href="http://groups.google.com/group/diagrams-discuss">project mailing list</a>, and/or come hang out in the <code>#diagrams</code> IRC channel on freenode.org for help and discussion. Make some diagrams. <a href="http://github.com/diagrams/">Fix some bugs</a>. Submit your cool examples for inclusion in the <a href="http://projects.haskell.org/diagrams/gallery.html">gallery</a> or your cool code for inclusion in the <a href="http://hackage.haskell.org/package/diagrams%2Dcontrib">diagrams-contrib</a> package!</p>
<p>Happy diagramming!</p>
<p>Brought to you by the diagrams team:</p>
<ul>
<li>Michael Sloan</li>
<li>Ryan Yates</li>
<li>Brent Yorgey</li>
</ul>
<p>with contributions from:</p>
<ul>
<li>Sam Griffin</li>
<li>Niklas Haas</li>
<li>Peter Hall</li>
<li>Claude Heiland-Allen</li>
<li>Deepak Jois</li>
<li>John Lato</li>
<li>Felipe Lessa</li>
<li>Chris Mears</li>
<li>Ian Ross</li>
<li>Vilhelm Sjöberg</li>
<li>Jim Snavely</li>
<li>Luite Stegeman</li>
<li>Kanchalai Suveepattananont</li>
<li>Michael Thompson</li>
<li>Scott Walck</li>
</ul>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/byorgey.wordpress.com/1023/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/byorgey.wordpress.com/1023/" /></a> <img alt="" border="0" src="https://stats.wordpress.com/b.gif?host=byorgey.wordpress.com&#038;blog=1152889&#038;post=1023&#038;subd=byorgey&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>https://byorgey.wordpress.com/2012/12/11/diagrams-0-6/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="https://0.gravatar.com/avatar/cc113924265dbeb535c8b2fefe4e33ee?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Brent</media:title>
		</media:content>

		<media:content url="http://mathlesstraveled.files.wordpress.com/2012/11/77455a72e05ff905c9c81cd250b45fa6.png" medium="image" />

		<media:content url="http://projects.haskell.org/diagrams/gallery/PythagoreanTree.png" medium="image" />
	</item>
		<item>
		<title>Species definition clarification and exercises</title>
		<link>https://byorgey.wordpress.com/2012/12/06/species-definition-clarification-and-exercises/</link>
		<comments>https://byorgey.wordpress.com/2012/12/06/species-definition-clarification-and-exercises/#comments</comments>
		<pubDate>Thu, 06 Dec 2012 16:44:33 +0000</pubDate>
		<dc:creator>Brent</dc:creator>
				<category><![CDATA[math]]></category>
		<category><![CDATA[species]]></category>
		<category><![CDATA[finite]]></category>
		<category><![CDATA[functor]]></category>
		<category><![CDATA[exercises]]></category>
		<category><![CDATA[size]]></category>

		<guid isPermaLink="false">http://byorgey.wordpress.com/?p=1020</guid>
		<description><![CDATA[[This is the fourth in a series of posts about combinatorial species. Previous posts: And now, back to your regularly scheduled combinatorial species; Decomposing data structures; Combinatorial species definition.] In my previous post I neglected to mention something quite crucial, &#8230; <a href="https://byorgey.wordpress.com/2012/12/06/species-definition-clarification-and-exercises/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="https://stats.wordpress.com/b.gif?host=byorgey.wordpress.com&#038;blog=1152889&#038;post=1020&#038;subd=byorgey&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><!-- -*- compile-command: "BlogLiteratelyD 04-defn2.markdown &gt; 04-defn2.html" -*- --></p>
<p>[<em>This is the fourth in a series of posts about combinatorial species. Previous posts: <a href="http://byorgey.wordpress.com/2012/10/17/and-now-back-to-your-regularly-scheduled-combinatorial-species/">And now, back to your regularly scheduled combinatorial species</a>; <a href="http://byorgey.wordpress.com/2012/10/27/decomposing-data-structures/">Decomposing data structures</a>; <a href="https://byorgey.wordpress.com/2012/11/20/combinatorial-species-definition/">Combinatorial species definition</a>.</em>]</p>
<p>In <a href="https://byorgey.wordpress.com/2012/11/20/combinatorial-species-definition/">my previous post</a> I neglected to mention something quite crucial, namely, that (at least for now) we are only talking about <em>finite</em> sets of labels and <em>finite</em> sets of structures. That is, we do not consider structures with infinitely many labels, and given a particular finite set of labels, there must be only a finite number of structures with those labels. The category <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbb%7BB%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbb{B}' title='&#92;mathbb{B}' class='latex' /> is the category of <em>finite</em> sets and bijections, not of all sets as I said previously.</p>
<p>Of course, in practice we <em>do</em> want to think about infinite sets of structures and labels, especially in relation to a non-strict language like Haskell! But the theory was invented by mathematicians interested in <em>counting things</em>. I do intend to explore ways to extend the theory to encompass infinite structures, but for now we’ll stick to the finite.</p>
<p>Before moving on to talk about the algebraic approach to species, I also want to point out a few simple implications of the <a href="https://byorgey.wordpress.com/2012/11/20/combinatorial-species-definition/">formal definition</a>. Instead of spelling them out in detail, I will pose them as exercises: you can either take them on faith, or try working through the exercises to deepen your understanding.</p>
<ol style="list-style-type:decimal;">
<li>Let <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Bn%5D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='[n]' title='[n]' class='latex' /> denote the set <img src='https://s-ssl.wordpress.com/latex.php?latex=%5C%7B0%2C+%5Cdots%2C+n-1%5C%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;{0, &#92;dots, n-1&#92;}' title='&#92;{0, &#92;dots, n-1&#92;}' class='latex' /><sup><a href="#fn1" class="footnoteRef" id="fnref1">1</a></sup>, and write <img src='https://s-ssl.wordpress.com/latex.php?latex=F%5Bn%5D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='F[n]' title='F[n]' class='latex' /> as an abbreviation for <img src='https://s-ssl.wordpress.com/latex.php?latex=F%5B%5Bn%5D%5D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='F[[n]]' title='F[[n]]' class='latex' />, <em>i.e.</em> the application of the species <img src='https://s-ssl.wordpress.com/latex.php?latex=F&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='F' title='F' class='latex' /> to the label set <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Bn%5D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='[n]' title='[n]' class='latex' />. Show that given <img src='https://s-ssl.wordpress.com/latex.php?latex=F%5Bn%5D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='F[n]' title='F[n]' class='latex' /> we can determine <img src='https://s-ssl.wordpress.com/latex.php?latex=F%5BU%5D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='F[U]' title='F[U]' class='latex' /> for any <img src='https://s-ssl.wordpress.com/latex.php?latex=U&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='U' title='U' class='latex' /> with <img src='https://s-ssl.wordpress.com/latex.php?latex=%7CU%7C+%3D+n&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='|U| = n' title='|U| = n' class='latex' />.</li>
</ol>
<p>(This shows that “size is all that matters”: in some sense species are really indexed not by sets of labels but by <em>size</em>.)</p>
<ol start="2" style="list-style-type:decimal;">
<li>[BLL<sup><a href="#fn2" class="footnoteRef" id="fnref2">2</a></sup> Exercise 1.1.2] Show that we get an equivalent definition if we take species to be functors from <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbb%7BB%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbb{B}' title='&#92;mathbb{B}' class='latex' /> to <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbf%7BFinSet%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbf{FinSet}' title='&#92;mathbf{FinSet}' class='latex' /> (the category of finite sets and (total) functions) instead of endofunctors on <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbb%7BB%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbb{B}' title='&#92;mathbb{B}' class='latex' />.</li>
</ol>
<p>(Apparently one can generalize the notion of species by replacing <img src='https://s-ssl.wordpress.com/latex.php?latex=%5Cmathbf%7BFinSet%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;mathbf{FinSet}' title='&#92;mathbf{FinSet}' class='latex' /> with other categories, but at present I am not sure of the implications.)</p>
<div class="footnotes">
<hr />
<ol>
<li id="fn1">
<p>The species literature uses <img src='https://s-ssl.wordpress.com/latex.php?latex=%5C%7B1%2C+%5Cdots%2C+n%5C%7D&amp;bg=ffffff&amp;fg=333333&amp;s=0' alt='&#92;{1, &#92;dots, n&#92;}' title='&#92;{1, &#92;dots, n&#92;}' class='latex' />, but (as every good computer scientist knows) counting ought to begin at zero.<a href="#fnref1">↩</a></p>
</li>
<li id="fn2">
<p>I will use “BLL” to refer to Bergeron, Labelle, and Leroux, <em>Combinatorial Species and Tree-Like Structures</em> (see the references at the end of <a href="http://byorgey.wordpress.com/2012/11/20/combinatorial-species-definition/">the previous post</a>  for the full citation).<a href="#fnref2">↩</a></p>
</li>
</ol>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/byorgey.wordpress.com/1020/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/byorgey.wordpress.com/1020/" /></a> <img alt="" border="0" src="https://stats.wordpress.com/b.gif?host=byorgey.wordpress.com&#038;blog=1152889&#038;post=1020&#038;subd=byorgey&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>https://byorgey.wordpress.com/2012/12/06/species-definition-clarification-and-exercises/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="https://0.gravatar.com/avatar/cc113924265dbeb535c8b2fefe4e33ee?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Brent</media:title>
		</media:content>
	</item>
	</channel>
</rss>
