<?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>Patterns in Functional Programming</title>
	<atom:link href="http://patternsinfp.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://patternsinfp.wordpress.com</link>
	<description>Exploiting the relationship between data structure and program structure</description>
	<lastBuildDate>Thu, 02 May 2013 14:23:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='patternsinfp.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/4685775b27de8acb59e3d3690a4157f4?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Patterns in Functional Programming</title>
		<link>http://patternsinfp.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://patternsinfp.wordpress.com/osd.xml" title="Patterns in Functional Programming" />
	<atom:link rel='hub' href='http://patternsinfp.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Comprehensions</title>
		<link>http://patternsinfp.wordpress.com/2012/01/19/comprehensions/</link>
		<comments>http://patternsinfp.wordpress.com/2012/01/19/comprehensions/#comments</comments>
		<pubDate>Thu, 19 Jan 2012 12:04:45 +0000</pubDate>
		<dc:creator>jeremygibbons</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://patternsinfp.wordpress.com/?p=227</guid>
		<description><![CDATA[Prompted by some recent work I&#8217;ve been doing on reasoning about monadic computations, I&#8217;ve been looking back at the work from the 1990s by Phil Trinder, Limsoon Wong, Leonidas Fegaras, Torsten Grust, and others, on monad comprehensions as a framework &#8230; <a href="http://patternsinfp.wordpress.com/2012/01/19/comprehensions/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=patternsinfp.wordpress.com&#038;blog=15593982&#038;post=227&#038;subd=patternsinfp&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>
 Prompted by some recent work I&#8217;ve been doing on <a href="http://www.cs.ox.ac.uk/publications/publication4877-abstract.html">reasoning about monadic computations</a>, I&#8217;ve been looking back at the work from the 1990s by Phil Trinder, Limsoon Wong, Leonidas Fegaras, Torsten Grust, and others, on monad comprehensions as a framework for database queries. </p>
<p>
The idea goes back to <a href="http://patternsinfp.wordpress.com/2011/02/15/universal-properties-and-galois-connections/">the adjunction between extension and intension</a> in set theory&mdash;you can define a set by its <em>extension</em>, that is by listing its elements: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5C%7B+1%2C+9%2C+25%2C+49%2C+81+%5C%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;{ 1, 9, 25, 49, 81 &#92;} ' title='&#92;displaystyle  &#92;{ 1, 9, 25, 49, 81 &#92;} ' class='latex' />
</p></blockquote>
<p> or by its <em>intension</em>, that is by characterizing those elements: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5C%7B+n%5E2+%5Cmid+0+%3C+n+%3C+10+%5Cland+n+%5Cequiv+1+%28%5Cmathop%7Bmod%7D+2%29+%5C%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;{ n^2 &#92;mid 0 &lt; n &lt; 10 &#92;land n &#92;equiv 1 (&#92;mathop{mod} 2) &#92;} ' title='&#92;displaystyle  &#92;{ n^2 &#92;mid 0 &lt; n &lt; 10 &#92;land n &#92;equiv 1 (&#92;mathop{mod} 2) &#92;} ' class='latex' />
</p></blockquote>
<p> Expressions in the latter form are called <em>set comprehensions</em>. They inspired a programming notation in the <a href="http://en.wikipedia.org/wiki/SETL">SETL</a> language from NYU, and have become widely known through list comprehensions in languages like Haskell. The structure needed of sets or of lists to make this work is roughly that of a <em>monad</em>, and Phil Wadler showed how to <a href="http://dx.doi.org/10.1017/S0960129500001560">generalize comprehensions to arbitrary monads</a>, which led to the &#8220;<b>do</b>&#8221; notation in Haskell. Around the same time, Phil Trinder showed that <a href="http://www.cs.ox.ac.uk/publications/publication3861-abstract.html">comprehensions make a convenient database query language</a>. The comprehension notation has been extended to cover other important aspects of database queries, particularly <a href="http://dx.doi.org/10.1007/BFb0026993">aggregation</a> and <a href="http://dx.doi.org/10.1145/1291201.1291209">grouping</a>. Monads and aggregations have very nice algebraic structure, which leads to a useful body of laws to support database query optimization.</p>
<p><h2> List comprehensions </h2>
<p>
Just as a warm-up, here is a reminder about Haskell&#8217;s list comprehensions. </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5B+2+%5Ctimes+a+%2B+b+%5Cmid+a+%5Cleftarrow+%5B1%2C2%2C3%5D+%2C+b+%5Cleftarrow+%5B4%2C5%2C6%5D+%2C+b+%5Cmathbin%7B%5Cunderline%7B%5Csmash%7B%5Cmathit%7Bmod%7D%7D%7D%7D+a+%3D%3D+0+%5D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  [ 2 &#92;times a + b &#92;mid a &#92;leftarrow [1,2,3] , b &#92;leftarrow [4,5,6] , b &#92;mathbin{&#92;underline{&#92;smash{&#92;mathit{mod}}}} a == 0 ] ' title='&#92;displaystyle  [ 2 &#92;times a + b &#92;mid a &#92;leftarrow [1,2,3] , b &#92;leftarrow [4,5,6] , b &#92;mathbin{&#92;underline{&#92;smash{&#92;mathit{mod}}}} a == 0 ] ' class='latex' />
</p></blockquote>
<p> This (rather concocted) example yields the list of all values of the expression <img src='http://s0.wp.com/latex.php?latex=%7B2+%5Ctimes+a+%2B+b%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{2 &#92;times a + b}' title='{2 &#92;times a + b}' class='latex' /> as <img src='http://s0.wp.com/latex.php?latex=%7Ba%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{a}' title='{a}' class='latex' /> is drawn from <img src='http://s0.wp.com/latex.php?latex=%7B%5B1%2C2%2C3%5D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{[1,2,3]}' title='{[1,2,3]}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7Bb%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{b}' title='{b}' class='latex' /> from <img src='http://s0.wp.com/latex.php?latex=%7B%5B4%2C5%2C6%5D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{[4,5,6]}' title='{[4,5,6]}' class='latex' /> and such that <img src='http://s0.wp.com/latex.php?latex=%7Bb%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{b}' title='{b}' class='latex' /> is divisible by <img src='http://s0.wp.com/latex.php?latex=%7Ba%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{a}' title='{a}' class='latex' />, namely <img src='http://s0.wp.com/latex.php?latex=%7B%5B6%2C7%2C8%2C8%2C10%2C12%5D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{[6,7,8,8,10,12]}' title='{[6,7,8,8,10,12]}' class='latex' />.</p>
<p>
To the left of the vertical bar is the <em>term</em> (an expression). To the right is a comma-separated sequence of <em>qualifiers</em>, each of which is either a <em>generator</em> (of the form <img src='http://s0.wp.com/latex.php?latex=%7Ba+%5Cleftarrow+x%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{a &#92;leftarrow x}' title='{a &#92;leftarrow x}' class='latex' />, with a variable <img src='http://s0.wp.com/latex.php?latex=%7Ba%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{a}' title='{a}' class='latex' /> and a list expression <img src='http://s0.wp.com/latex.php?latex=%7Bx%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{x}' title='{x}' class='latex' />) or a <em>filter</em> (a boolean expression). The scope of a variable introduced by a generator extends to all subsequent generators and to the term. Note that, in contrast to the mathematical inspiration, bound variables need to be generated from some existing list.</p>
<p>
The semantics of list comprehensions is defined by translation; see for example Phil Wadler&#8217;s Chapter 7 of <a href="http://research.microsoft.com/en-us/um/people/simonpj/papers/slpj-book-1987/">The Implementation of Functional Programming Languages</a>. It can be expressed equationally as follows: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5B+e+%5Cmid+%5Cepsilon+%5D+%26%3D%26+%5Be%5D+%5C%5C+%7B%7D+%5B+e+%5Cmid+b+%5D+%26%3D%26+%5Cmathbf%7Bif%7D%5C%3Bb%5C%3B%5Cmathbf%7Bthen%7D%5C%3B%5B+e+%5D%5C%3B%5Cmathbf%7Belse%7D%5C%3B%5B%5C%2C%5D+%5C%5C+%7B%7D+%5B+e+%5Cmid+a+%5Cleftarrow+x+%5D+%26%3D%26+%5Cmathit%7Bmap%7D%5C%2C%28%5Clambda+a+%5Cmathbin%7B.%7D+e%29%5C%2Cx+%5C%5C+%7B%7D+%5B+e+%5Cmid+q%2C+q%27+%5D+%26%3D%26+%5Cmathit%7Bconcat%7D%5C%2C%5B+%5B+e+%5Cmid+q%27+%5D+%5Cmid+q+%5D+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} [ e &#92;mid &#92;epsilon ] &amp;=&amp; [e] &#92;&#92; {} [ e &#92;mid b ] &amp;=&amp; &#92;mathbf{if}&#92;;b&#92;;&#92;mathbf{then}&#92;;[ e ]&#92;;&#92;mathbf{else}&#92;;[&#92;,] &#92;&#92; {} [ e &#92;mid a &#92;leftarrow x ] &amp;=&amp; &#92;mathit{map}&#92;,(&#92;lambda a &#92;mathbin{.} e)&#92;,x &#92;&#92; {} [ e &#92;mid q, q&#039; ] &amp;=&amp; &#92;mathit{concat}&#92;,[ [ e &#92;mid q&#039; ] &#92;mid q ] &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} [ e &#92;mid &#92;epsilon ] &amp;=&amp; [e] &#92;&#92; {} [ e &#92;mid b ] &amp;=&amp; &#92;mathbf{if}&#92;;b&#92;;&#92;mathbf{then}&#92;;[ e ]&#92;;&#92;mathbf{else}&#92;;[&#92;,] &#92;&#92; {} [ e &#92;mid a &#92;leftarrow x ] &amp;=&amp; &#92;mathit{map}&#92;,(&#92;lambda a &#92;mathbin{.} e)&#92;,x &#92;&#92; {} [ e &#92;mid q, q&#039; ] &amp;=&amp; &#92;mathit{concat}&#92;,[ [ e &#92;mid q&#039; ] &#92;mid q ] &#92;end{array} ' class='latex' />
</p></blockquote>
<p> (Here, <img src='http://s0.wp.com/latex.php?latex=%7B%5Cepsilon%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;epsilon}' title='{&#92;epsilon}' class='latex' /> denotes the empty sequence of qualifiers. It&#8217;s not allowed in Haskell, but it is helpful in simplifying the translation.)</p>
<p>
Applying this translation to the example at the start of the section gives </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Bll%7D+%26+%5B+2+%5Ctimes+a+%2B+b+%5Cmid+a+%5Cleftarrow+%5B1%2C2%2C3%5D+%2C+b+%5Cleftarrow+%5B4%2C5%2C6%5D+%2C+b+%5Cmathbin%7B%5Cunderline%7B%5Csmash%7B%5Cmathit%7Bmod%7D%7D%7D%7D+a+%3D%3D+0+%5D+%5C%5C+%3D+%26+%5Cmathit%7Bconcat%7D%5C%2C%28%5Cmathit%7Bmap%7D%5C%2C%28%5Clambda+a+%5Cmathbin%7B.%7D+%5Cmathit%7Bconcat%7D%5C%2C%28%5Cmathit%7Bmap%7D%5C%2C%28%5Clambda+b+%5Cmathbin%7B.%7D+%5Cmathbf%7Bif%7D%5C%3Bb+%5Cmathbin%7B%5Cunderline%7B%5Csmash%7B%5Cmathit%7Bmod%7D%7D%7D%7D+a+%3D%3D+0%5C%3B%5Cmathbf%7Bthen%7D%5C%3B%5B2+%5Ctimes+a+%2B+b%5D%5C%3B%5Cmathbf%7Belse%7D%5C%3B%5B%5C%2C%5D%29%5C%2C%5B4%2C5%2C6%5D%29%29%5C%2C%5B1%2C2%2C3%5D%29+%5C%5C+%3D+%26+%5B6%2C7%2C8%2C8%2C10%2C12%5D+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{ll} &amp; [ 2 &#92;times a + b &#92;mid a &#92;leftarrow [1,2,3] , b &#92;leftarrow [4,5,6] , b &#92;mathbin{&#92;underline{&#92;smash{&#92;mathit{mod}}}} a == 0 ] &#92;&#92; = &amp; &#92;mathit{concat}&#92;,(&#92;mathit{map}&#92;,(&#92;lambda a &#92;mathbin{.} &#92;mathit{concat}&#92;,(&#92;mathit{map}&#92;,(&#92;lambda b &#92;mathbin{.} &#92;mathbf{if}&#92;;b &#92;mathbin{&#92;underline{&#92;smash{&#92;mathit{mod}}}} a == 0&#92;;&#92;mathbf{then}&#92;;[2 &#92;times a + b]&#92;;&#92;mathbf{else}&#92;;[&#92;,])&#92;,[4,5,6]))&#92;,[1,2,3]) &#92;&#92; = &amp; [6,7,8,8,10,12] &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{ll} &amp; [ 2 &#92;times a + b &#92;mid a &#92;leftarrow [1,2,3] , b &#92;leftarrow [4,5,6] , b &#92;mathbin{&#92;underline{&#92;smash{&#92;mathit{mod}}}} a == 0 ] &#92;&#92; = &amp; &#92;mathit{concat}&#92;,(&#92;mathit{map}&#92;,(&#92;lambda a &#92;mathbin{.} &#92;mathit{concat}&#92;,(&#92;mathit{map}&#92;,(&#92;lambda b &#92;mathbin{.} &#92;mathbf{if}&#92;;b &#92;mathbin{&#92;underline{&#92;smash{&#92;mathit{mod}}}} a == 0&#92;;&#92;mathbf{then}&#92;;[2 &#92;times a + b]&#92;;&#92;mathbf{else}&#92;;[&#92;,])&#92;,[4,5,6]))&#92;,[1,2,3]) &#92;&#92; = &amp; [6,7,8,8,10,12] &#92;end{array} ' class='latex' />
</p></blockquote>
<p> More generally, a generator may match against a pattern rather than just a variable. In that case, it may bind multiple (or indeed no) variables at once; moreover, the match may fail, in which case it is discarded. This is handled by modifying the translation for generators to use a function defined by pattern-matching, rather than a straight lambda-abstraction: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5B+e+%5Cmid+p+%5Cleftarrow+x+%5D+%3D+%5Cmathit%7Bconcat%7D%5C%2C%28%5Cmathit%7Bmap%7D%5C%2C%28%5Clambda+a+%5Cmathbin%7B.%7D+%5Cmathbf%7Bcase%7D%5C%3Ba%5C%3B%5Cmathbf%7Bof%7D%5C%3Bp+%5Crightarrow+%5B+e+%5D+%5C%3B%3B%5C%3B+%5C_+%5Crightarrow+%5B%5C%2C%5D%29%5C%2Cx%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  [ e &#92;mid p &#92;leftarrow x ] = &#92;mathit{concat}&#92;,(&#92;mathit{map}&#92;,(&#92;lambda a &#92;mathbin{.} &#92;mathbf{case}&#92;;a&#92;;&#92;mathbf{of}&#92;;p &#92;rightarrow [ e ] &#92;;;&#92;; &#92;_ &#92;rightarrow [&#92;,])&#92;,x) ' title='&#92;displaystyle  [ e &#92;mid p &#92;leftarrow x ] = &#92;mathit{concat}&#92;,(&#92;mathit{map}&#92;,(&#92;lambda a &#92;mathbin{.} &#92;mathbf{case}&#92;;a&#92;;&#92;mathbf{of}&#92;;p &#92;rightarrow [ e ] &#92;;;&#92;; &#92;_ &#92;rightarrow [&#92;,])&#92;,x) ' class='latex' />
</p></blockquote>
<p> or, more perspicuously, </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5B+e+%5Cmid+p+%5Cleftarrow+x+%5D+%3D+%5Cmathbf%7Blet%7D%5C%3Bh%5C%2Cp+%3D+%5B+e+%5D+%3B+h%5C%2C%5C_+%3D+%5B%5C%2C%5D%5C%3B%5Cmathbf%7Bin%7D%5C%3B+%5Cmathit%7Bconcat%7D%5C%2C%28%5Cmathit%7Bmap%7D%5C%2Ch%5C%2Cx%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  [ e &#92;mid p &#92;leftarrow x ] = &#92;mathbf{let}&#92;;h&#92;,p = [ e ] ; h&#92;,&#92;_ = [&#92;,]&#92;;&#92;mathbf{in}&#92;; &#92;mathit{concat}&#92;,(&#92;mathit{map}&#92;,h&#92;,x) ' title='&#92;displaystyle  [ e &#92;mid p &#92;leftarrow x ] = &#92;mathbf{let}&#92;;h&#92;,p = [ e ] ; h&#92;,&#92;_ = [&#92;,]&#92;;&#92;mathbf{in}&#92;; &#92;mathit{concat}&#92;,(&#92;mathit{map}&#92;,h&#92;,x) ' class='latex' />
</p></blockquote>
<p><h2> Monad comprehensions </h2>
<p>
It is clear from the above translation that the necessary ingredients for list comprehensions are <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bmap%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{map}}' title='{&#92;mathit{map}}' class='latex' />, singletons, <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bconcat%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{concat}}' title='{&#92;mathit{concat}}' class='latex' />, and the empty list. The first three are the operations arising from lists as a functor and a monad, which suggests that the same translation might be applicable to other monads too. But the fourth ingredient, the empty list, does not come from the functor and monad structures; that requires an extra assumption: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Bll%7D+%5Cmathbf%7Bclass%7D%5C%3B%5Cmathit%7BMonad%7D%5C%2Cm+%5CRightarrow+%5Cmathit%7BMonadZero%7D%5C%2Cm%5C%3B%5Cmathbf%7Bwhere%7D+%5C%5C+%5Cquad+%5Cmathit%7Bmzero%7D+%3A%3A+m%5C%2Ca+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{ll} &#92;mathbf{class}&#92;;&#92;mathit{Monad}&#92;,m &#92;Rightarrow &#92;mathit{MonadZero}&#92;,m&#92;;&#92;mathbf{where} &#92;&#92; &#92;quad &#92;mathit{mzero} :: m&#92;,a &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{ll} &#92;mathbf{class}&#92;;&#92;mathit{Monad}&#92;,m &#92;Rightarrow &#92;mathit{MonadZero}&#92;,m&#92;;&#92;mathbf{where} &#92;&#92; &#92;quad &#92;mathit{mzero} :: m&#92;,a &#92;end{array} ' class='latex' />
</p></blockquote>
<p> Then the translation for list comprehensions can be generalized to other monads: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5B+e+%5Cmid+%5Cepsilon+%5D+%26%3D%26+%5Cmathit%7Breturn%7D%5C%2Ce+%5C%5C+%7B%7D+%5B+e+%5Cmid+b+%5D+%26%3D%26+%5Cmathbf%7Bif%7D%5C%3Bb%5C%3B%5Cmathbf%7Bthen%7D%5C%3B%5Cmathit%7Breturn%7D%5C%2Ce%5C%3B%5Cmathbf%7Belse%7D%5C%3B%5Cmathit%7Bmzero%7D+%5C%5C+%7B%7D+%5B+e+%5Cmid+p+%5Cleftarrow+m+%5D+%26%3D%26+%5Cmathbf%7Blet%7D%5C%3Bh%5C%2Cp+%3D+%5Cmathit%7Breturn%7D%5C%2Ce+%3B+h%5C%2C%5C_+%3D+%5Cmathit%7Bmzero%7D%5C%3B%5Cmathbf%7Bin%7D%5C%3B+%5Cmathit%7Bjoin%7D%5C%2C%28%5Cmathit%7Bmap%7D%5C%2Ch%5C%2Cm%29+%5C%5C+%7B%7D+%5B+e+%5Cmid+q%2C+q%27+%5D+%26%3D%26+%5Cmathit%7Bjoin%7D%5C%2C%5B+%5B+e+%5Cmid+q%27+%5D+%5Cmid+q+%5D+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} [ e &#92;mid &#92;epsilon ] &amp;=&amp; &#92;mathit{return}&#92;,e &#92;&#92; {} [ e &#92;mid b ] &amp;=&amp; &#92;mathbf{if}&#92;;b&#92;;&#92;mathbf{then}&#92;;&#92;mathit{return}&#92;,e&#92;;&#92;mathbf{else}&#92;;&#92;mathit{mzero} &#92;&#92; {} [ e &#92;mid p &#92;leftarrow m ] &amp;=&amp; &#92;mathbf{let}&#92;;h&#92;,p = &#92;mathit{return}&#92;,e ; h&#92;,&#92;_ = &#92;mathit{mzero}&#92;;&#92;mathbf{in}&#92;; &#92;mathit{join}&#92;,(&#92;mathit{map}&#92;,h&#92;,m) &#92;&#92; {} [ e &#92;mid q, q&#039; ] &amp;=&amp; &#92;mathit{join}&#92;,[ [ e &#92;mid q&#039; ] &#92;mid q ] &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} [ e &#92;mid &#92;epsilon ] &amp;=&amp; &#92;mathit{return}&#92;,e &#92;&#92; {} [ e &#92;mid b ] &amp;=&amp; &#92;mathbf{if}&#92;;b&#92;;&#92;mathbf{then}&#92;;&#92;mathit{return}&#92;,e&#92;;&#92;mathbf{else}&#92;;&#92;mathit{mzero} &#92;&#92; {} [ e &#92;mid p &#92;leftarrow m ] &amp;=&amp; &#92;mathbf{let}&#92;;h&#92;,p = &#92;mathit{return}&#92;,e ; h&#92;,&#92;_ = &#92;mathit{mzero}&#92;;&#92;mathbf{in}&#92;; &#92;mathit{join}&#92;,(&#92;mathit{map}&#92;,h&#92;,m) &#92;&#92; {} [ e &#92;mid q, q&#039; ] &amp;=&amp; &#92;mathit{join}&#92;,[ [ e &#92;mid q&#039; ] &#92;mid q ] &#92;end{array} ' class='latex' />
</p></blockquote>
<p> (so <img src='http://s0.wp.com/latex.php?latex=%7B%5B+e+%5Cmid+%5Cepsilon+%5D+%3D+%5B+e+%5Cmid+%5Cmathit%7BTrue%7D+%5D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{[ e &#92;mid &#92;epsilon ] = [ e &#92;mid &#92;mathit{True} ]}' title='{[ e &#92;mid &#92;epsilon ] = [ e &#92;mid &#92;mathit{True} ]}' class='latex' />). The actual monad to be used is implicit; if we want to be explicit, we could use a subscript, as in &#8220;<img src='http://s0.wp.com/latex.php?latex=%7B%5B+e+%5Cmid+q+%5D_%5Cmathsf%7BList%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{[ e &#92;mid q ]_&#92;mathsf{List}}' title='{[ e &#92;mid q ]_&#92;mathsf{List}}' class='latex' />&#8220;.</p>
<p>
This translation is different from the one used in the Haskell language specification, which to my mind is a little awkward: the empty list crops up in two different ways in the translation of list comprehensions&mdash;for filters, and for generators with patterns&mdash;and these are generalized in two different ways to other monads (to the <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bmzero%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{mzero}}' title='{&#92;mathit{mzero}}' class='latex' /> method of the <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7BMonadPlus%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{MonadPlus}}' title='{&#92;mathit{MonadPlus}}' class='latex' /> class in the first case, and the <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfail%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{fail}}' title='{&#92;mathit{fail}}' class='latex' /> method of the <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7BMonad%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{Monad}}' title='{&#92;mathit{Monad}}' class='latex' /> class in the second). I think it is neater to have a monad subclass <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7BMonadZero%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{MonadZero}}' title='{&#92;mathit{MonadZero}}' class='latex' /> with a single method subsuming both these operators. Of course, this does mean that the translation forces a monad comprehension with filters or possibly failing generators to be interpreted in a monad in the <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7BMonadZero%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{MonadZero}}' title='{&#92;mathit{MonadZero}}' class='latex' /> subclass rather than just <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7BMonad%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{Monad}}' title='{&#92;mathit{Monad}}' class='latex' />&mdash;the type class constraints that are generated depend on the features used in the comprehension. (Perhaps this translation was tried in earlier versions of the language specification, and found wanting?)</p>
<p>
Taking this approach gives basically the monad comprehension notation from Wadler&#8217;s <a href="http://dx.doi.org/10.1017/S0960129500001560">Comprehending Monads</a> paper; it loosely corresponds to Haskell&#8217;s <b>do</b> notation, except that the term is to the left of a vertical bar rather than at the end, and that filters are just boolean expressions rather than introduced using <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bguard%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{guard}}' title='{&#92;mathit{guard}}' class='latex' />. </p>
<p>
We might impose the law that <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bmzero%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{mzero}}' title='{&#92;mathit{mzero}}' class='latex' /> is a &#8220;left&#8221; zero of composition, in the sense </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathit%7Bjoin%7D%5C%2C%5Cmathit%7Bmzero%7D+%3D+%5Cmathit%7Bmzero%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathit{join}&#92;,&#92;mathit{mzero} = &#92;mathit{mzero} ' title='&#92;displaystyle  &#92;mathit{join}&#92;,&#92;mathit{mzero} = &#92;mathit{mzero} ' class='latex' />
</p></blockquote>
<p> or, in terms of comprehensions, </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5B+e+%5Cmid+a+%5Cleftarrow+%5Cmathit%7Bmzero%7D+%5D+%3D+%5Cmathit%7Bmzero%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  [ e &#92;mid a &#92;leftarrow &#92;mathit{mzero} ] = &#92;mathit{mzero} ' title='&#92;displaystyle  [ e &#92;mid a &#92;leftarrow &#92;mathit{mzero} ] = &#92;mathit{mzero} ' class='latex' />
</p></blockquote>
<p> Informally, this means that any failing steps of the computation cleanly cut off subsequent branches. Conversely, we do not require that <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bmzero%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{mzero}}' title='{&#92;mathit{mzero}}' class='latex' /> is a &#8220;right&#8221; zero too: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathit%7Bjoin%7D%5C%2C%28%5Cmathit%7Bmap%7D%5C%2C%28%5Clambda+a+%5Cmathbin%7B.%7D+%5Cmathit%7Bmzero%7D%29%5C%2Cm%29+%5Cne+%5Cmathit%7Bmzero%7D+%5Cquad%5Cmbox%7B%28in+general%29%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathit{join}&#92;,(&#92;mathit{map}&#92;,(&#92;lambda a &#92;mathbin{.} &#92;mathit{mzero})&#92;,m) &#92;ne &#92;mathit{mzero} &#92;quad&#92;mbox{(in general)} ' title='&#92;displaystyle  &#92;mathit{join}&#92;,(&#92;mathit{map}&#92;,(&#92;lambda a &#92;mathbin{.} &#92;mathit{mzero})&#92;,m) &#92;ne &#92;mathit{mzero} &#92;quad&#92;mbox{(in general)} ' class='latex' />
</p></blockquote>
<p> This would have the consequence that a failing step also cleanly erases any effects from earlier parts of the computation, which is too strong a requirement for many monads&mdash;particularly those of the &#8220;launch missiles now&#8221; variety. (The names &#8220;left-&#8221; and &#8220;right zero&#8221; make more sense when the equations are expressed in terms of the usual Haskell bind operator <img src='http://s0.wp.com/latex.php?latex=%7B%28%5Cgg%5C%21%3D%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(&#92;gg&#92;!=)}' title='{(&#92;gg&#92;!=)}' class='latex' />, which is a kind of sequential composition.)</p>
<p><h2> Ringads and collection classes </h2>
<p>
One more ingredient is needed in order to characterize monads that correspond to &#8220;collection classes&#8221; such as sets and lists, and that is an analogue of set union or list append. It&#8217;s not difficult to see that this is inexpressible in terms of the operations introduced so far: given only collections <img src='http://s0.wp.com/latex.php?latex=%7Bm%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{m}' title='{m}' class='latex' /> of at most one element, any comprehension using generators of the form <img src='http://s0.wp.com/latex.php?latex=%7Ba+%5Cleftarrow+m%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{a &#92;leftarrow m}' title='{a &#92;leftarrow m}' class='latex' /> will only yield another such collection, whereas the union of two one-element collections will in general have two elements.</p>
<p>
To allow any finite collection to be expressed, it suffices to introduce a binary union operator <img src='http://s0.wp.com/latex.php?latex=%7B%5Cuplus%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;uplus}' title='{&#92;uplus}' class='latex' />: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Bll%7D+%5Cmathbf%7Bclass%7D%5C%3B%5Cmathit%7BMonad%7D%5C%2Cm+%5CRightarrow+%5Cmathit%7BMonadPlus%7D%5C%2Cm%5C%3B%5Cmathbf%7Bwhere%7D+%5C%5C+%5Cquad+%28%5Cuplus%29+%3A%3A+m%5C%2Ca+%5Ctimes+m%5C%2Ca+%5Crightarrow+m%5C%2Ca+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{ll} &#92;mathbf{class}&#92;;&#92;mathit{Monad}&#92;,m &#92;Rightarrow &#92;mathit{MonadPlus}&#92;,m&#92;;&#92;mathbf{where} &#92;&#92; &#92;quad (&#92;uplus) :: m&#92;,a &#92;times m&#92;,a &#92;rightarrow m&#92;,a &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{ll} &#92;mathbf{class}&#92;;&#92;mathit{Monad}&#92;,m &#92;Rightarrow &#92;mathit{MonadPlus}&#92;,m&#92;;&#92;mathbf{where} &#92;&#92; &#92;quad (&#92;uplus) :: m&#92;,a &#92;times m&#92;,a &#92;rightarrow m&#92;,a &#92;end{array} ' class='latex' />
</p></blockquote>
<p> We require composition to distribute over union, in the following sense: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathit%7Bjoin%7D%5C%2C%28m+%5Cuplus+n%29+%3D+%5Cmathit%7Bjoin%7D%5C%2Cm+%5Cuplus+%5Cmathit%7Bjoin%7D%5C%2Cn+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathit{join}&#92;,(m &#92;uplus n) = &#92;mathit{join}&#92;,m &#92;uplus &#92;mathit{join}&#92;,n ' title='&#92;displaystyle  &#92;mathit{join}&#92;,(m &#92;uplus n) = &#92;mathit{join}&#92;,m &#92;uplus &#92;mathit{join}&#92;,n ' class='latex' />
</p></blockquote>
<p> or, in terms of comprehensions, </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5B+e+%5Cmid+a+%5Cleftarrow+m+%5Cuplus+n%2C+q+%5D+%3D+%5B+e+%5Cmid+a+%5Cleftarrow+m%2C+q+%5D+%5Cuplus+%5B+e+%5Cmid+a+%5Cleftarrow+n%2C+q+%5D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  [ e &#92;mid a &#92;leftarrow m &#92;uplus n, q ] = [ e &#92;mid a &#92;leftarrow m, q ] &#92;uplus [ e &#92;mid a &#92;leftarrow n, q ] ' title='&#92;displaystyle  [ e &#92;mid a &#92;leftarrow m &#92;uplus n, q ] = [ e &#92;mid a &#92;leftarrow m, q ] &#92;uplus [ e &#92;mid a &#92;leftarrow n, q ] ' class='latex' />
</p></blockquote>
<p> For the remainder of this post, we will assume a monad in both <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7BMonadZero%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{MonadZero}}' title='{&#92;mathit{MonadZero}}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7BMonadPlus%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{MonadPlus}}' title='{&#92;mathit{MonadPlus}}' class='latex' />. Moreover, we will assume that <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bmzero%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{mzero}}' title='{&#92;mathit{mzero}}' class='latex' /> is the unit of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cuplus%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;uplus}' title='{&#92;uplus}' class='latex' />, and is both a left- and a right zero of composition. To stress the additional constraints, we will write &#8220;<img src='http://s0.wp.com/latex.php?latex=%7B%5Cemptyset%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;emptyset}' title='{&#92;emptyset}' class='latex' />&#8221; for &#8220;<img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bmzero%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{mzero}}' title='{&#92;mathit{mzero}}' class='latex' />&#8221; from now on. The intention is that such monads exactly capture collection classes; Phil Wadler has called these structures <em>ringads</em>. (He seems to have done so in an unpublished note <i>Notes on Monads and Ringads</i> from 1990, which is cited by some papers from the early 1990s. But Phil no longer has a copy of this note, and it&#8217;s not online anywhere&#8230; I&#8217;d love to see a copy, if anyone has one!) </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Bll%7D+%5Cmathbf%7Bclass%7D%5C%3B%28%5Cmathit%7BMonadZero%7D%5C%2Cm%2C+%5Cmathit%7BMonadPlus%7D%5C%2Cm%29+%5CRightarrow+%5Cmathit%7BRingad%7D%5C%2Cm%5C%3B%5Cmathbf%7Bwhere%7D+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{ll} &#92;mathbf{class}&#92;;(&#92;mathit{MonadZero}&#92;,m, &#92;mathit{MonadPlus}&#92;,m) &#92;Rightarrow &#92;mathit{Ringad}&#92;,m&#92;;&#92;mathbf{where} &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{ll} &#92;mathbf{class}&#92;;(&#92;mathit{MonadZero}&#92;,m, &#92;mathit{MonadPlus}&#92;,m) &#92;Rightarrow &#92;mathit{Ringad}&#92;,m&#92;;&#92;mathbf{where} &#92;end{array} ' class='latex' />
</p></blockquote>
<p> (There are no additional methods; the class <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7BRingad%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{Ringad}}' title='{&#92;mathit{Ringad}}' class='latex' /> is the intersection of the two parent classes <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7BMonadZero%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{MonadZero}}' title='{&#92;mathit{MonadZero}}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7BMonadPlus%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{MonadPlus}}' title='{&#92;mathit{MonadPlus}}' class='latex' />, with the union of the two interfaces, together with the laws above.) I used roughly the same construction already in <a href="http://patternsinfp.wordpress.com/2011/05/05/horners-rule/">the post on Horner&#8217;s Rule</a>.</p>
<p>
As well as (finite) sets and lists, ringad instances include (finite) bags and a funny kind of binary tree (externally labelled, possibly empty, in which the empty tree is a unit of the binary tree constructor). These are all members of the so-called <a href="http://www.cs.nott.ac.uk/~rcb/papers/abstract.html#exploration">Boom Hierarchy</a> of types&mdash;a name coined by Richard Bird, for an idea due to Hendrik Boom, who by happy coincidence is named for one of these structures in his native language. All members of the Boom Hierarchy are generated from the empty, singleton, and union operators, the difference being whether union is associative, commutative, and idempotent. Another ringad instance, but not a member of the Boom Hierarchy, is the type of probability distributions&mdash;either normalized, with a weight-indexed family of union operators, or unnormalized, with an additional scaling operator. </p>
<p><h2> Aggregation </h2>
<p>
The well-behaved operations over monadic values are called the <em>algebras</em> for that monad&mdash;functions <img src='http://s0.wp.com/latex.php?latex=%7Bk%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{k}' title='{k}' class='latex' /> such that <img src='http://s0.wp.com/latex.php?latex=%7Bk+%5Ccdot+%5Cmathit%7Breturn%7D+%3D+%5Cmathit%7Bid%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{k &#92;cdot &#92;mathit{return} = &#92;mathit{id}}' title='{k &#92;cdot &#92;mathit{return} = &#92;mathit{id}}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7Bk+%5Ccdot+%5Cmathit%7Bjoin%7D+%3D+k+%5Ccdot+%5Cmathit%7Bmap%7D%5C%2Ck%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{k &#92;cdot &#92;mathit{join} = k &#92;cdot &#92;mathit{map}&#92;,k}' title='{k &#92;cdot &#92;mathit{join} = k &#92;cdot &#92;mathit{map}&#92;,k}' class='latex' />. In particular, <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bjoin%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{join}}' title='{&#92;mathit{join}}' class='latex' /> is itself a monad algebra. When the monad is also a ringad, <img src='http://s0.wp.com/latex.php?latex=%7Bk%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{k}' title='{k}' class='latex' /> necessarily distributes also over <img src='http://s0.wp.com/latex.php?latex=%7B%5Cuplus%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;uplus}' title='{&#92;uplus}' class='latex' />&mdash;there is a binary operator <img src='http://s0.wp.com/latex.php?latex=%7B%5Coplus%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;oplus}' title='{&#92;oplus}' class='latex' /> such that <img src='http://s0.wp.com/latex.php?latex=%7Bk%5C%2C%28m+%5Cuplus+n%29+%3D+k%5C%2Cm+%5Coplus+k%5C%2Cn%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{k&#92;,(m &#92;uplus n) = k&#92;,m &#92;oplus k&#92;,n}' title='{k&#92;,(m &#92;uplus n) = k&#92;,m &#92;oplus k&#92;,n}' class='latex' /> (exercise!). Without loss of generality, we write <img src='http://s0.wp.com/latex.php?latex=%7B%5Coplus%2F%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;oplus/}' title='{&#92;oplus/}' class='latex' /> for <img src='http://s0.wp.com/latex.php?latex=%7Bk%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{k}' title='{k}' class='latex' />; these are the &#8220;reductions&#8221; of the Bird&ndash;Meertens Formalism. In that case, <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bjoin%7D+%3D+%5Cuplus%2F%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{join} = &#92;uplus/}' title='{&#92;mathit{join} = &#92;uplus/}' class='latex' /> is a ringad algebra.</p>
<p>
The algebras for a ringad amount to aggregation functions for a collection: the sum of a bag of integers, the maximum of a set of naturals, and so on. We could extend the comprehension notation to encompass aggregations too, for example by adding an optional annotation, writing say &#8220;<img src='http://s0.wp.com/latex.php?latex=%7B%5B+e+%5Cmid+q+%5D%5E%5Coplus%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{[ e &#92;mid q ]^&#92;oplus}' title='{[ e &#92;mid q ]^&#92;oplus}' class='latex' />&#8220;; although this doesn&#8217;t add much, because we could just have written &#8220;<img src='http://s0.wp.com/latex.php?latex=%7B%5Coplus%2F%5C%2C%5Be+%5Cmid+q%5D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;oplus/&#92;,[e &#92;mid q]}' title='{&#92;oplus/&#92;,[e &#92;mid q]}' class='latex' />&#8221; instead. We could generalize from reductions <img src='http://s0.wp.com/latex.php?latex=%7B%5Coplus%2F%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;oplus/}' title='{&#92;oplus/}' class='latex' /> to collection homomorphisms <img src='http://s0.wp.com/latex.php?latex=%7B%5Coplus%2F+%5Ccdot+%5Cmathit%7Bmap%7D%5C%2Cf%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;oplus/ &#92;cdot &#92;mathit{map}&#92;,f}' title='{&#92;oplus/ &#92;cdot &#92;mathit{map}&#92;,f}' class='latex' />; but this doesn&#8217;t add much either, because the map is easily combined with the comprehension&mdash;it&#8217;s easy to show the &#8220;map over comprehension&#8221; property </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathit%7Bmap%7D%5C%2Cf%5C%2C%5Be+%5Cmid+q%5D+%3D+%5Bf%5C%2Ce+%5Cmid+q%5D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathit{map}&#92;,f&#92;,[e &#92;mid q] = [f&#92;,e &#92;mid q] ' title='&#92;displaystyle  &#92;mathit{map}&#92;,f&#92;,[e &#92;mid q] = [f&#92;,e &#92;mid q] ' class='latex' />
</p></blockquote>
<p> Leonidas Fegaras and David Maier develop a <a href="http://dx.doi.org/10.1145/377674.377676">monoid comprehension calculus</a> around such aggregations; but I think their name is inappropriate, because nothing forces the binary aggregating operator to be associative.</p>
<p>
Note that, for <img src='http://s0.wp.com/latex.php?latex=%7B%5Coplus%2F%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;oplus/}' title='{&#92;oplus/}' class='latex' /> to be well-defined, <img src='http://s0.wp.com/latex.php?latex=%7B%5Coplus%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;oplus}' title='{&#92;oplus}' class='latex' /> must satisfy all the laws that <img src='http://s0.wp.com/latex.php?latex=%7B%5Cuplus%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;uplus}' title='{&#92;uplus}' class='latex' /> does&mdash;<img src='http://s0.wp.com/latex.php?latex=%7B%5Coplus%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;oplus}' title='{&#92;oplus}' class='latex' /> must be associative if <img src='http://s0.wp.com/latex.php?latex=%7B%5Cuplus%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;uplus}' title='{&#92;uplus}' class='latex' /> is associative, and so on. It is not hard to show, for instance, that there is no <img src='http://s0.wp.com/latex.php?latex=%7B%5Coplus%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;oplus}' title='{&#92;oplus}' class='latex' /> on sets of numbers for which <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bsum%7D%5C%2C%28x+%5Ccup+y%29+%3D+%5Cmathit%7Bsum%7D%5C%2Cx+%5Coplus+%5Cmathit%7Bsum%7D%5C%2Cy%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{sum}&#92;,(x &#92;cup y) = &#92;mathit{sum}&#92;,x &#92;oplus &#92;mathit{sum}&#92;,y}' title='{&#92;mathit{sum}&#92;,(x &#92;cup y) = &#92;mathit{sum}&#92;,x &#92;oplus &#92;mathit{sum}&#92;,y}' class='latex' />; such an <img src='http://s0.wp.com/latex.php?latex=%7B%5Coplus%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;oplus}' title='{&#92;oplus}' class='latex' /> would have to be idempotent, which is inconsistent with its relationship with <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bsum%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{sum}}' title='{&#92;mathit{sum}}' class='latex' />. (So, although <img src='http://s0.wp.com/latex.php?latex=%7B%5Ba%5E2+%5Cmid+a+%5Cleftarrow+x%2C+%5Cmathit%7Bodd%7D%5C%2Ca%5D_%5Cmathsf%7BBag%7D%5E%7B%2B%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{[a^2 &#92;mid a &#92;leftarrow x, &#92;mathit{odd}&#92;,a]_&#92;mathsf{Bag}^{+}}' title='{[a^2 &#92;mid a &#92;leftarrow x, &#92;mathit{odd}&#92;,a]_&#92;mathsf{Bag}^{+}}' class='latex' /> denotes the sum of the squares of the odd elements of bag <img src='http://s0.wp.com/latex.php?latex=%7Bx%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{x}' title='{x}' class='latex' />, the expression <img src='http://s0.wp.com/latex.php?latex=%7B%5Ba%5E2+%5Cmid+a+%5Cleftarrow+x%2C+%5Cmathit%7Bodd%7D%5C%2Ca%5D_%5Cmathsf%7BSet%7D%5E%7B%2B%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{[a^2 &#92;mid a &#92;leftarrow x, &#92;mathit{odd}&#92;,a]_&#92;mathsf{Set}^{+}}' title='{[a^2 &#92;mid a &#92;leftarrow x, &#92;mathit{odd}&#92;,a]_&#92;mathsf{Set}^{+}}' class='latex' /> (with <img src='http://s0.wp.com/latex.php?latex=%7Bx%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{x}' title='{x}' class='latex' /> now a set) is not defined, because <img src='http://s0.wp.com/latex.php?latex=%7B%2B%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{+}' title='{+}' class='latex' /> is not idempotent.) In particular, <img src='http://s0.wp.com/latex.php?latex=%7B%5Coplus%2F%5Cemptyset%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;oplus/&#92;emptyset}' title='{&#92;oplus/&#92;emptyset}' class='latex' /> must be the unit of <img src='http://s0.wp.com/latex.php?latex=%7B%5Coplus%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;oplus}' title='{&#92;oplus}' class='latex' />, which we write <img src='http://s0.wp.com/latex.php?latex=%7B1_%5Coplus%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{1_&#92;oplus}' title='{1_&#92;oplus}' class='latex' />. </p>
<p>
We can derive translation rules for aggregations from the definition </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5B+e+%5Cmid+q+%5D%5E%5Coplus+%3D+%5Coplus%2F%5C%2C%5Be+%5Cmid+q%5D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  [ e &#92;mid q ]^&#92;oplus = &#92;oplus/&#92;,[e &#92;mid q] ' title='&#92;displaystyle  [ e &#92;mid q ]^&#92;oplus = &#92;oplus/&#92;,[e &#92;mid q] ' class='latex' />
</p></blockquote>
<p> For empty aggregations, we have: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Bll%7D+%26+%5B+e+%5Cmid+%5Cepsilon+%5D%5E%5Coplus+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Baggregation%7D+%5C%7D+%5C%5C+%26+%5Coplus%2F%5C%2C%5B+e+%5Cmid+%5Cepsilon+%5D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bcomprehension%7D+%5C%7D+%5C%5C+%26+%5Coplus%2F%5C%2C%28%5Cmathit%7Breturn%7D%5C%2Ce%29+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bmonad+algebra%7D+%5C%7D+%5C%5C+%26+e+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{ll} &amp; [ e &#92;mid &#92;epsilon ]^&#92;oplus &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{aggregation} &#92;} &#92;&#92; &amp; &#92;oplus/&#92;,[ e &#92;mid &#92;epsilon ] &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{comprehension} &#92;} &#92;&#92; &amp; &#92;oplus/&#92;,(&#92;mathit{return}&#92;,e) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{monad algebra} &#92;} &#92;&#92; &amp; e &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{ll} &amp; [ e &#92;mid &#92;epsilon ]^&#92;oplus &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{aggregation} &#92;} &#92;&#92; &amp; &#92;oplus/&#92;,[ e &#92;mid &#92;epsilon ] &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{comprehension} &#92;} &#92;&#92; &amp; &#92;oplus/&#92;,(&#92;mathit{return}&#92;,e) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{monad algebra} &#92;} &#92;&#92; &amp; e &#92;end{array} ' class='latex' />
</p></blockquote>
<p> For filters, we have: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Bll%7D+%26+%5B+e+%5Cmid+b+%5D%5E%5Coplus+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Baggregation%7D+%5C%7D+%5C%5C+%26+%5Coplus%2F%5C%2C%5B+e+%5Cmid+b+%5D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bcomprehension%7D+%5C%7D+%5C%5C+%26+%5Coplus%2F%5C%2C%28%5Cmathbf%7Bif%7D%5C%3Bb%5C%3B%5Cmathbf%7Bthen%7D%5C%3B%5Cmathit%7Breturn%7D%5C%2Ce%5C%3B%5Cmathbf%7Belse%7D%5C%3B%5Cemptyset%29+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Blift+out+the+conditional%7D+%5C%7D+%5C%5C+%26+%5Cmathbf%7Bif%7D%5C%3Bb%5C%3B%5Cmathbf%7Bthen%7D%5C%3B%7B%5Coplus%2F%7D%5C%2C%28%5Cmathit%7Breturn%7D%5C%2Ce%29%5C%3B%5Cmathbf%7Belse%7D%5C%3B%7B%5Coplus%2F%7D%5C%2C%5Cemptyset+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bringad+algebra%7D+%5C%7D+%5C%5C+%26+%5Cmathbf%7Bif%7D%5C%3Bb%5C%3B%5Cmathbf%7Bthen%7D%5C%3Be%5C%3B%5Cmathbf%7Belse%7D%5C%3B1_%5Coplus+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{ll} &amp; [ e &#92;mid b ]^&#92;oplus &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{aggregation} &#92;} &#92;&#92; &amp; &#92;oplus/&#92;,[ e &#92;mid b ] &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{comprehension} &#92;} &#92;&#92; &amp; &#92;oplus/&#92;,(&#92;mathbf{if}&#92;;b&#92;;&#92;mathbf{then}&#92;;&#92;mathit{return}&#92;,e&#92;;&#92;mathbf{else}&#92;;&#92;emptyset) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{lift out the conditional} &#92;} &#92;&#92; &amp; &#92;mathbf{if}&#92;;b&#92;;&#92;mathbf{then}&#92;;{&#92;oplus/}&#92;,(&#92;mathit{return}&#92;,e)&#92;;&#92;mathbf{else}&#92;;{&#92;oplus/}&#92;,&#92;emptyset &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{ringad algebra} &#92;} &#92;&#92; &amp; &#92;mathbf{if}&#92;;b&#92;;&#92;mathbf{then}&#92;;e&#92;;&#92;mathbf{else}&#92;;1_&#92;oplus &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{ll} &amp; [ e &#92;mid b ]^&#92;oplus &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{aggregation} &#92;} &#92;&#92; &amp; &#92;oplus/&#92;,[ e &#92;mid b ] &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{comprehension} &#92;} &#92;&#92; &amp; &#92;oplus/&#92;,(&#92;mathbf{if}&#92;;b&#92;;&#92;mathbf{then}&#92;;&#92;mathit{return}&#92;,e&#92;;&#92;mathbf{else}&#92;;&#92;emptyset) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{lift out the conditional} &#92;} &#92;&#92; &amp; &#92;mathbf{if}&#92;;b&#92;;&#92;mathbf{then}&#92;;{&#92;oplus/}&#92;,(&#92;mathit{return}&#92;,e)&#92;;&#92;mathbf{else}&#92;;{&#92;oplus/}&#92;,&#92;emptyset &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{ringad algebra} &#92;} &#92;&#92; &amp; &#92;mathbf{if}&#92;;b&#92;;&#92;mathbf{then}&#92;;e&#92;;&#92;mathbf{else}&#92;;1_&#92;oplus &#92;end{array} ' class='latex' />
</p></blockquote>
<p> For generators, we have: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Bll%7D+%26+%5B+e+%5Cmid+p+%5Cleftarrow+m+%5D%5E%5Coplus+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Baggregation%7D+%5C%7D+%5C%5C+%26+%5Coplus%2F%5C%2C%5B+e+%5Cmid+p+%5Cleftarrow+m+%5D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bcomprehension%7D+%5C%7D+%5C%5C+%26+%5Coplus%2F%5C%2C%28%5Cmathbf%7Blet%7D%5C%3Bh%5C%2Cp+%3D+%5Cmathit%7Breturn%7D%5C%2Ce+%3B+h%5C%2C%5C_+%3D+%5Cemptyset%5C%3B%5Cmathbf%7Bin%7D%5C%3B%5Cmathit%7Bjoin%7D%5C%2C%28%5Cmathit%7Bmap%7D%5C%2Ch%5C%2Cm%29%29+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Blift+out+the+%5Ctextbf%7Blet%7D%7D+%5C%7D+%5C%5C+%26+%5Cmathbf%7Blet%7D%5C%3Bh%5C%2Cp+%3D+%5Cmathit%7Breturn%7D%2Ce+%3B+h%5C%2C%5C_+%3D+%5Cemptyset%5C%3B%5Cmathbf%7Bin%7D%5C%3B%7B%5Coplus%2F%7D%5C%2C%28%5Cmathit%7Bjoin%7D%5C%2C%28%5Cmathit%7Bmap%7D%5C%2Ch%5C%2Cm%29%29+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bmonad+algebra%7D+%5C%7D+%5C%5C+%26+%5Cmathbf%7Blet%7D%5C%3Bh%5C%2Cp+%3D+%5Cmathit%7Breturn%7D%5C%2Ce+%3B+h%5C%2C%5C_+%3D+%5Cemptyset%5C%3B%5Cmathbf%7Bin%7D%5C%3B%7B%5Coplus%2F%7D%5C%2C%28%5Cmathit%7Bmap%7D%5C%2C%28%5Coplus%2F%29%5C%2C%28%5Cmathit%7Bmap%7D%5C%2Ch%5C%2Cm%29%29+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bfunctors%7D+%5C%7D+%5C%5C+%26+%5Cmathbf%7Blet%7D%5C%3Bh%5C%2Cp+%3D+%5Cmathit%7Breturn%7D%5C%2Ce+%3B+h%5C%2C%5C_+%3D+%5Cemptyset%5C%3B%5Cmathbf%7Bin%7D%5C%3B%7B%5Coplus%2F%7D%5C%2C%28%5Cmathit%7Bmap%7D%5C%2C%28%5Coplus%2F+%5Ccdot+h%29%5C%2Cm%29+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Blet+%5C%28h%27+%3D+%5Coplus%2F+%5Ccdot+h%5C%29%7D+%5C%7D+%5C%5C+%26+%5Cmathbf%7Blet%7D%5C%3Bh%27%5C%2Cp+%3D+%5Coplus%2F%5C%2C%28%5Cmathit%7Breturn%7D%5C%2Ce%29+%3B+h%27%5C%2C%5C_+%3D+%5Coplus%2F%5C%2C%5Cemptyset%5C%3B%5Cmathbf%7Bin%7D%5C%3B%7B%5Coplus%2F%7D%5C%2C%28%5Cmathit%7Bmap%7D%5C%2Ch%27%5C%2Cm%29+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bringad+algebra%7D+%5C%7D+%5C%5C+%26+%5Cmathbf%7Blet%7D%5C%3Bh%27%5C%2Cp+%3D+e+%3B+h%27%5C%2C%5C_+%3D+1_%5Coplus%5C%3B%5Cmathbf%7Bin%7D%5C%3B%7B%5Coplus%2F%7D%5C%2C%28%5Cmathit%7Bmap%7D%5C%2Ch%27%5C%2Cm%29+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{ll} &amp; [ e &#92;mid p &#92;leftarrow m ]^&#92;oplus &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{aggregation} &#92;} &#92;&#92; &amp; &#92;oplus/&#92;,[ e &#92;mid p &#92;leftarrow m ] &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{comprehension} &#92;} &#92;&#92; &amp; &#92;oplus/&#92;,(&#92;mathbf{let}&#92;;h&#92;,p = &#92;mathit{return}&#92;,e ; h&#92;,&#92;_ = &#92;emptyset&#92;;&#92;mathbf{in}&#92;;&#92;mathit{join}&#92;,(&#92;mathit{map}&#92;,h&#92;,m)) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{lift out the &#92;textbf{let}} &#92;} &#92;&#92; &amp; &#92;mathbf{let}&#92;;h&#92;,p = &#92;mathit{return},e ; h&#92;,&#92;_ = &#92;emptyset&#92;;&#92;mathbf{in}&#92;;{&#92;oplus/}&#92;,(&#92;mathit{join}&#92;,(&#92;mathit{map}&#92;,h&#92;,m)) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{monad algebra} &#92;} &#92;&#92; &amp; &#92;mathbf{let}&#92;;h&#92;,p = &#92;mathit{return}&#92;,e ; h&#92;,&#92;_ = &#92;emptyset&#92;;&#92;mathbf{in}&#92;;{&#92;oplus/}&#92;,(&#92;mathit{map}&#92;,(&#92;oplus/)&#92;,(&#92;mathit{map}&#92;,h&#92;,m)) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{functors} &#92;} &#92;&#92; &amp; &#92;mathbf{let}&#92;;h&#92;,p = &#92;mathit{return}&#92;,e ; h&#92;,&#92;_ = &#92;emptyset&#92;;&#92;mathbf{in}&#92;;{&#92;oplus/}&#92;,(&#92;mathit{map}&#92;,(&#92;oplus/ &#92;cdot h)&#92;,m) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{let &#92;(h&#039; = &#92;oplus/ &#92;cdot h&#92;)} &#92;} &#92;&#92; &amp; &#92;mathbf{let}&#92;;h&#039;&#92;,p = &#92;oplus/&#92;,(&#92;mathit{return}&#92;,e) ; h&#039;&#92;,&#92;_ = &#92;oplus/&#92;,&#92;emptyset&#92;;&#92;mathbf{in}&#92;;{&#92;oplus/}&#92;,(&#92;mathit{map}&#92;,h&#039;&#92;,m) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{ringad algebra} &#92;} &#92;&#92; &amp; &#92;mathbf{let}&#92;;h&#039;&#92;,p = e ; h&#039;&#92;,&#92;_ = 1_&#92;oplus&#92;;&#92;mathbf{in}&#92;;{&#92;oplus/}&#92;,(&#92;mathit{map}&#92;,h&#039;&#92;,m) &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{ll} &amp; [ e &#92;mid p &#92;leftarrow m ]^&#92;oplus &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{aggregation} &#92;} &#92;&#92; &amp; &#92;oplus/&#92;,[ e &#92;mid p &#92;leftarrow m ] &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{comprehension} &#92;} &#92;&#92; &amp; &#92;oplus/&#92;,(&#92;mathbf{let}&#92;;h&#92;,p = &#92;mathit{return}&#92;,e ; h&#92;,&#92;_ = &#92;emptyset&#92;;&#92;mathbf{in}&#92;;&#92;mathit{join}&#92;,(&#92;mathit{map}&#92;,h&#92;,m)) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{lift out the &#92;textbf{let}} &#92;} &#92;&#92; &amp; &#92;mathbf{let}&#92;;h&#92;,p = &#92;mathit{return},e ; h&#92;,&#92;_ = &#92;emptyset&#92;;&#92;mathbf{in}&#92;;{&#92;oplus/}&#92;,(&#92;mathit{join}&#92;,(&#92;mathit{map}&#92;,h&#92;,m)) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{monad algebra} &#92;} &#92;&#92; &amp; &#92;mathbf{let}&#92;;h&#92;,p = &#92;mathit{return}&#92;,e ; h&#92;,&#92;_ = &#92;emptyset&#92;;&#92;mathbf{in}&#92;;{&#92;oplus/}&#92;,(&#92;mathit{map}&#92;,(&#92;oplus/)&#92;,(&#92;mathit{map}&#92;,h&#92;,m)) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{functors} &#92;} &#92;&#92; &amp; &#92;mathbf{let}&#92;;h&#92;,p = &#92;mathit{return}&#92;,e ; h&#92;,&#92;_ = &#92;emptyset&#92;;&#92;mathbf{in}&#92;;{&#92;oplus/}&#92;,(&#92;mathit{map}&#92;,(&#92;oplus/ &#92;cdot h)&#92;,m) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{let &#92;(h&#039; = &#92;oplus/ &#92;cdot h&#92;)} &#92;} &#92;&#92; &amp; &#92;mathbf{let}&#92;;h&#039;&#92;,p = &#92;oplus/&#92;,(&#92;mathit{return}&#92;,e) ; h&#039;&#92;,&#92;_ = &#92;oplus/&#92;,&#92;emptyset&#92;;&#92;mathbf{in}&#92;;{&#92;oplus/}&#92;,(&#92;mathit{map}&#92;,h&#039;&#92;,m) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{ringad algebra} &#92;} &#92;&#92; &amp; &#92;mathbf{let}&#92;;h&#039;&#92;,p = e ; h&#039;&#92;,&#92;_ = 1_&#92;oplus&#92;;&#92;mathbf{in}&#92;;{&#92;oplus/}&#92;,(&#92;mathit{map}&#92;,h&#039;&#92;,m) &#92;end{array} ' class='latex' />
</p></blockquote>
<p> And for sequences of qualifiers, we have: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Bll%7D+%26+%5B+e+%5Cmid+q%2C+q%27+%5D%5E%5Coplus+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Baggregation%7D+%5C%7D+%5C%5C+%26+%5Coplus%2F%5C%2C%5B+e+%5Cmid+q%2C+q%27+%5D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bcomprehension%7D+%5C%7D+%5C%5C+%26+%5Coplus%2F%5C%2C%28%5Cmathit%7Bjoin%7D%5C%2C%5B+%5B+e+%5Cmid+q%27%5D+%5Cmid+q+%5D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bmonad+algebra%7D+%5C%7D+%5C%5C+%26+%5Coplus%2F%5C%2C%28%5Cmathit%7Bmap%7D%5C%2C%28%5Coplus%2F%29%5C%2C%5B+%5B+e+%5Cmid+q%27%5D+%5Cmid+q+%5D%29+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bmap+over+comprehension%7D+%5C%7D+%5C%5C+%26+%5Coplus%2F%5C%2C%5B+%5Coplus%2F%5C%2C%5B+e+%5Cmid+q%27%5D+%5Cmid+q+%5D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Baggregation%7D+%5C%7D+%5C%5C+%26+%5B+%5B+e+%5Cmid+q%27%5D%5E%5Coplus+%5Cmid+q+%5D%5E%5Coplus+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{ll} &amp; [ e &#92;mid q, q&#039; ]^&#92;oplus &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{aggregation} &#92;} &#92;&#92; &amp; &#92;oplus/&#92;,[ e &#92;mid q, q&#039; ] &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{comprehension} &#92;} &#92;&#92; &amp; &#92;oplus/&#92;,(&#92;mathit{join}&#92;,[ [ e &#92;mid q&#039;] &#92;mid q ] &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{monad algebra} &#92;} &#92;&#92; &amp; &#92;oplus/&#92;,(&#92;mathit{map}&#92;,(&#92;oplus/)&#92;,[ [ e &#92;mid q&#039;] &#92;mid q ]) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{map over comprehension} &#92;} &#92;&#92; &amp; &#92;oplus/&#92;,[ &#92;oplus/&#92;,[ e &#92;mid q&#039;] &#92;mid q ] &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{aggregation} &#92;} &#92;&#92; &amp; [ [ e &#92;mid q&#039;]^&#92;oplus &#92;mid q ]^&#92;oplus &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{ll} &amp; [ e &#92;mid q, q&#039; ]^&#92;oplus &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{aggregation} &#92;} &#92;&#92; &amp; &#92;oplus/&#92;,[ e &#92;mid q, q&#039; ] &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{comprehension} &#92;} &#92;&#92; &amp; &#92;oplus/&#92;,(&#92;mathit{join}&#92;,[ [ e &#92;mid q&#039;] &#92;mid q ] &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{monad algebra} &#92;} &#92;&#92; &amp; &#92;oplus/&#92;,(&#92;mathit{map}&#92;,(&#92;oplus/)&#92;,[ [ e &#92;mid q&#039;] &#92;mid q ]) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{map over comprehension} &#92;} &#92;&#92; &amp; &#92;oplus/&#92;,[ &#92;oplus/&#92;,[ e &#92;mid q&#039;] &#92;mid q ] &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{aggregation} &#92;} &#92;&#92; &amp; [ [ e &#92;mid q&#039;]^&#92;oplus &#92;mid q ]^&#92;oplus &#92;end{array} ' class='latex' />
</p></blockquote>
<p> Putting all this together, we have: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5B+e+%5Cmid+%5Cepsilon+%5D%5E%5Coplus+%26%3D%26+e+%5C%5C+%7B%7D+%5B+e+%5Cmid+b+%5D%5E%5Coplus+%26%3D%26%5Cmathbf%7Bif%7D%5C%3Bb%5C%3B%5Cmathbf%7Bthen%7D%5C%3Be%5C%3B%5Cmathbf%7Belse%7D%5C%3B1_%5Coplus+%5C%5C+%7B%7D+%5B+e+%5Cmid+p+%5Cleftarrow+m+%5D%5E%5Coplus+%26%3D%26+%5Cmathbf%7Blet%7D%5C%3Bh%5C%2Cp+%3D+e+%3B+h%5C%2C%5C_+%3D+1_%5Coplus%5C%3B%5Cmathbf%7Bin%7D%5C%3B%7B%5Coplus%2F%7D%5C%2C%28%5Cmathit%7Bmap%7D%5C%2Ch%5C%2Cm%29+%5C%5C+%7B%7D+%5B+e+%5Cmid+q%2C+q%27+%5D%5E%5Coplus+%26%3D%26+%5B+%5B+e+%5Cmid+q%27%5D%5E%5Coplus+%5Cmid+q+%5D%5E%5Coplus+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} [ e &#92;mid &#92;epsilon ]^&#92;oplus &amp;=&amp; e &#92;&#92; {} [ e &#92;mid b ]^&#92;oplus &amp;=&amp;&#92;mathbf{if}&#92;;b&#92;;&#92;mathbf{then}&#92;;e&#92;;&#92;mathbf{else}&#92;;1_&#92;oplus &#92;&#92; {} [ e &#92;mid p &#92;leftarrow m ]^&#92;oplus &amp;=&amp; &#92;mathbf{let}&#92;;h&#92;,p = e ; h&#92;,&#92;_ = 1_&#92;oplus&#92;;&#92;mathbf{in}&#92;;{&#92;oplus/}&#92;,(&#92;mathit{map}&#92;,h&#92;,m) &#92;&#92; {} [ e &#92;mid q, q&#039; ]^&#92;oplus &amp;=&amp; [ [ e &#92;mid q&#039;]^&#92;oplus &#92;mid q ]^&#92;oplus &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} [ e &#92;mid &#92;epsilon ]^&#92;oplus &amp;=&amp; e &#92;&#92; {} [ e &#92;mid b ]^&#92;oplus &amp;=&amp;&#92;mathbf{if}&#92;;b&#92;;&#92;mathbf{then}&#92;;e&#92;;&#92;mathbf{else}&#92;;1_&#92;oplus &#92;&#92; {} [ e &#92;mid p &#92;leftarrow m ]^&#92;oplus &amp;=&amp; &#92;mathbf{let}&#92;;h&#92;,p = e ; h&#92;,&#92;_ = 1_&#92;oplus&#92;;&#92;mathbf{in}&#92;;{&#92;oplus/}&#92;,(&#92;mathit{map}&#92;,h&#92;,m) &#92;&#92; {} [ e &#92;mid q, q&#039; ]^&#92;oplus &amp;=&amp; [ [ e &#92;mid q&#039;]^&#92;oplus &#92;mid q ]^&#92;oplus &#92;end{array} ' class='latex' />
</p></blockquote>
<p><h2> Heterogeneous comprehensions </h2>
<p>
We have seen that comprehensions can be interpreted in an arbitrary ringad; for example, <img src='http://s0.wp.com/latex.php?latex=%7B%5Ba%5E2+%5Cmid+a+%5Cleftarrow+x%2C+%5Cmathit%7Bodd%7D%5C%2Ca%5D_%5Cmathsf%7BSet%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{[a^2 &#92;mid a &#92;leftarrow x, &#92;mathit{odd}&#92;,a]_&#92;mathsf{Set}}' title='{[a^2 &#92;mid a &#92;leftarrow x, &#92;mathit{odd}&#92;,a]_&#92;mathsf{Set}}' class='latex' /> denotes (the set of) the squares of the odd elements of (the set) <img src='http://s0.wp.com/latex.php?latex=%7Bx%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{x}' title='{x}' class='latex' />, whereas <img src='http://s0.wp.com/latex.php?latex=%7B%5Ba%5E2+%5Cmid+a+%5Cleftarrow+x%2C+%5Cmathit%7Bodd%7D%5C%2Ca%5D_%5Cmathsf%7BBag%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{[a^2 &#92;mid a &#92;leftarrow x, &#92;mathit{odd}&#92;,a]_&#92;mathsf{Bag}}' title='{[a^2 &#92;mid a &#92;leftarrow x, &#92;mathit{odd}&#92;,a]_&#92;mathsf{Bag}}' class='latex' /> denotes the bag of such elements, with <img src='http://s0.wp.com/latex.php?latex=%7Bx%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{x}' title='{x}' class='latex' /> a bag. Can we make sense of &#8220;heterogeneous comprehensions&#8221;, involving several different ringads?</p>
<p>
Let&#8217;s introduced the notion of a <em>ringad morphism</em>, extending the familiar analogue on monads. For monads <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BM%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{M}}' title='{&#92;mathsf{M}}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BN%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{N}}' title='{&#92;mathsf{N}}' class='latex' />, a monad morphism <img src='http://s0.wp.com/latex.php?latex=%7B%5Cphi+%3A+%5Cmathsf%7BM%7D+%5Cmathbin%7B%5Cstackrel%7B.%7D%7B%5Cto%7D%7D+%5Cmathsf%7BN%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;phi : &#92;mathsf{M} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{N}}' title='{&#92;phi : &#92;mathsf{M} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{N}}' class='latex' /> is a natural transformation <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BM%7D+%5Cmathbin%7B%5Cstackrel%7B.%7D%7B%5Cto%7D%7D+%5Cmathsf%7BN%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{M} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{N}}' title='{&#92;mathsf{M} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{N}}' class='latex' />&mdash;that is, a family <img src='http://s0.wp.com/latex.php?latex=%7B%5Cphi_%5Calpha+%3A%3A+%5Cmathsf%7BM%7D%5C%2C%5Calpha+%5Crightarrow+%5Cmathsf%7BN%7D%5C%2C%5Calpha%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;phi_&#92;alpha :: &#92;mathsf{M}&#92;,&#92;alpha &#92;rightarrow &#92;mathsf{N}&#92;,&#92;alpha}' title='{&#92;phi_&#92;alpha :: &#92;mathsf{M}&#92;,&#92;alpha &#92;rightarrow &#92;mathsf{N}&#92;,&#92;alpha}' class='latex' /> of arrows, coherent in the sense that <img src='http://s0.wp.com/latex.php?latex=%7B%5Cphi_%5Cbeta+%5Ccdot+%5Cmathsf%7BM%7D%5C%2Cf+%3D+%5Cmathsf%7BN%7D%5C%2Cf+%5Ccdot+%5Cphi_%5Calpha%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;phi_&#92;beta &#92;cdot &#92;mathsf{M}&#92;,f = &#92;mathsf{N}&#92;,f &#92;cdot &#92;phi_&#92;alpha}' title='{&#92;phi_&#92;beta &#92;cdot &#92;mathsf{M}&#92;,f = &#92;mathsf{N}&#92;,f &#92;cdot &#92;phi_&#92;alpha}' class='latex' /> for <img src='http://s0.wp.com/latex.php?latex=%7Bf+%3A%3A+%5Calpha+%5Crightarrow+%5Cbeta%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f :: &#92;alpha &#92;rightarrow &#92;beta}' title='{f :: &#92;alpha &#92;rightarrow &#92;beta}' class='latex' />&mdash;that also preserves the monad structure: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blclcl%7D+%5Cphi+%5Ccdot+%5Cmathit%7Breturn%7D_%5Cmathsf%7BM%7D+%26%3D%26+%5Cmathit%7Breturn%7D_%5Cmathsf%7BN%7D+%5C%5C+%5Cphi+%5Ccdot+%5Cmathit%7Bjoin%7D_%5Cmathsf%7BM%7D+%26%3D%26+%5Cmathit%7Bjoin%7D_%5Cmathsf%7BN%7D+%5Ccdot+%5Cphi+%5Ccdot+%5Cmathsf%7BM%7D%5C%2C%5Cphi+%26%3D%26+%5Cmathit%7Bjoin%7D_%5Cmathsf%7BN%7D+%5Ccdot+%5Cmathsf%7BN%7D%5C%2C%5Cphi+%5Ccdot+%5Cphi+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lclcl} &#92;phi &#92;cdot &#92;mathit{return}_&#92;mathsf{M} &amp;=&amp; &#92;mathit{return}_&#92;mathsf{N} &#92;&#92; &#92;phi &#92;cdot &#92;mathit{join}_&#92;mathsf{M} &amp;=&amp; &#92;mathit{join}_&#92;mathsf{N} &#92;cdot &#92;phi &#92;cdot &#92;mathsf{M}&#92;,&#92;phi &amp;=&amp; &#92;mathit{join}_&#92;mathsf{N} &#92;cdot &#92;mathsf{N}&#92;,&#92;phi &#92;cdot &#92;phi &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lclcl} &#92;phi &#92;cdot &#92;mathit{return}_&#92;mathsf{M} &amp;=&amp; &#92;mathit{return}_&#92;mathsf{N} &#92;&#92; &#92;phi &#92;cdot &#92;mathit{join}_&#92;mathsf{M} &amp;=&amp; &#92;mathit{join}_&#92;mathsf{N} &#92;cdot &#92;phi &#92;cdot &#92;mathsf{M}&#92;,&#92;phi &amp;=&amp; &#92;mathit{join}_&#92;mathsf{N} &#92;cdot &#92;mathsf{N}&#92;,&#92;phi &#92;cdot &#92;phi &#92;end{array} ' class='latex' />
</p></blockquote>
<p> A ringad morphism <img src='http://s0.wp.com/latex.php?latex=%7B%5Cphi+%3A+%5Cmathsf%7BM%7D+%5Cmathbin%7B%5Cstackrel%7B.%7D%7B%5Cto%7D%7D+%5Cmathsf%7BN%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;phi : &#92;mathsf{M} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{N}}' title='{&#92;phi : &#92;mathsf{M} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{N}}' class='latex' /> for ringads <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BM%7D%2C%5Cmathsf%7BN%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{M},&#92;mathsf{N}}' title='{&#92;mathsf{M},&#92;mathsf{N}}' class='latex' /> is a monad morphism <img src='http://s0.wp.com/latex.php?latex=%7B%5Cphi+%3A+%5Cmathsf%7BM%7D+%5Cmathbin%7B%5Cstackrel%7B.%7D%7B%5Cto%7D%7D+%5Cmathsf%7BN%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;phi : &#92;mathsf{M} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{N}}' title='{&#92;phi : &#92;mathsf{M} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{N}}' class='latex' /> that also respects the ringad structure: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Cphi%5C%2C%5Cemptyset_%5Cmathsf%7BM%7D+%26%3D%26+%5Cemptyset_%5Cmathsf%7BN%7D+%5C%5C+%5Cphi%5C%2C%28x+%5Cuplus_%5Cmathsf%7BM%7D+y%29+%26%3D%26+%5Cphi%5C%2Cx+%5Cuplus_%5Cmathsf%7BN%7D+%5Cphi%5C%2Cy+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;phi&#92;,&#92;emptyset_&#92;mathsf{M} &amp;=&amp; &#92;emptyset_&#92;mathsf{N} &#92;&#92; &#92;phi&#92;,(x &#92;uplus_&#92;mathsf{M} y) &amp;=&amp; &#92;phi&#92;,x &#92;uplus_&#92;mathsf{N} &#92;phi&#92;,y &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;phi&#92;,&#92;emptyset_&#92;mathsf{M} &amp;=&amp; &#92;emptyset_&#92;mathsf{N} &#92;&#92; &#92;phi&#92;,(x &#92;uplus_&#92;mathsf{M} y) &amp;=&amp; &#92;phi&#92;,x &#92;uplus_&#92;mathsf{N} &#92;phi&#92;,y &#92;end{array} ' class='latex' />
</p></blockquote>
<p> Then a ringad morphism behaves nicely with respect to ringad comprehensions&mdash;a comprehension interpreted in ringad <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BM%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{M}}' title='{&#92;mathsf{M}}' class='latex' />, using existing collections of type <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BM%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{M}}' title='{&#92;mathsf{M}}' class='latex' />, with the result transformed via a ringad morphism <img src='http://s0.wp.com/latex.php?latex=%7B%5Cphi+%3A+%5Cmathsf%7BM%7D+%5Cmathbin%7B%5Cstackrel%7B.%7D%7B%5Cto%7D%7D+%5Cmathsf%7BN%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;phi : &#92;mathsf{M} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{N}}' title='{&#92;phi : &#92;mathsf{M} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{N}}' class='latex' /> to ringad <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BN%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{N}}' title='{&#92;mathsf{N}}' class='latex' />, is equivalent to the comprehension interpreted in ringad <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BN%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{N}}' title='{&#92;mathsf{N}}' class='latex' /> in the first place, but with the initial collections transformed to type <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BN%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{N}}' title='{&#92;mathsf{N}}' class='latex' />. Informally, there will be no surprises arising from when ringad coercions take place, because the results are the same whenever this happens. This property is straightforward to show by induction over the structure of the comprehension. For the empty comprehension, we have: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Bll%7D+%26+%5Cphi%5C%2C%5B+e+%5Cmid+%5Cepsilon+%5D_%5Cmathsf%7BM%7D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bcomprehension%7D+%5C%7D+%5C%5C+%26+%5Cphi%5C%2C%28%5Cmathit%7Breturn%7D_%5Cmathsf%7BM%7D%5C%2Ce%29+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bringad+morphism%7D+%5C%7D+%5C%5C+%26+%5Cmathit%7Breturn%7D_%5Cmathsf%7BN%7D%5C%2Ce+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bcomprehension%7D+%5C%7D+%5C%5C+%26+%5Be+%5Cmid+%5Cepsilon+%5D_%5Cmathsf%7BN%7D+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;phi&#92;,[ e &#92;mid &#92;epsilon ]_&#92;mathsf{M} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{comprehension} &#92;} &#92;&#92; &amp; &#92;phi&#92;,(&#92;mathit{return}_&#92;mathsf{M}&#92;,e) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{ringad morphism} &#92;} &#92;&#92; &amp; &#92;mathit{return}_&#92;mathsf{N}&#92;,e &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{comprehension} &#92;} &#92;&#92; &amp; [e &#92;mid &#92;epsilon ]_&#92;mathsf{N} &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;phi&#92;,[ e &#92;mid &#92;epsilon ]_&#92;mathsf{M} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{comprehension} &#92;} &#92;&#92; &amp; &#92;phi&#92;,(&#92;mathit{return}_&#92;mathsf{M}&#92;,e) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{ringad morphism} &#92;} &#92;&#92; &amp; &#92;mathit{return}_&#92;mathsf{N}&#92;,e &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{comprehension} &#92;} &#92;&#92; &amp; [e &#92;mid &#92;epsilon ]_&#92;mathsf{N} &#92;end{array} ' class='latex' />
</p></blockquote>
<p> For filters, we have: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Bll%7D+%26+%5Cphi%5C%2C%5B+e+%5Cmid+b+%5D_%5Cmathsf%7BM%7D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bcomprehension%7D+%5C%7D+%5C%5C+%26+%5Cphi%5C%2C%28%5Cmathbf%7Bif%7D%5C%3Bb%5C%3B%5Cmathbf%7Bthen%7D%5C%3B%5Cmathit%7Breturn%7D_%5Cmathsf%7BM%7D%5C%2Ce%5C%3B%5Cmathbf%7Belse%7D%5C%3B%5Cemptyset_%5Cmathsf%7BM%7D%29+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Blift+out+the+conditional%7D+%5C%7D+%5C%5C+%26+%5Cmathbf%7Bif%7D%5C%3Bb%5C%3B%5Cmathbf%7Bthen%7D%5C%3B%5Cphi%5C%2C%28%5Cmathit%7Breturn%7D_%5Cmathsf%7BM%7D%5C%2Ce%29%5C%3B%5Cmathbf%7Belse%7D%5C%3B%5Cphi%5C%2C%5Cemptyset_%5Cmathsf%7BM%7D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bringad+morphism%7D+%5C%7D+%5C%5C+%26+%5Cmathbf%7Bif%7D%5C%3Bb%5C%3B%5Cmathbf%7Bthen%7D%5C%3B%5Cmathit%7Breturn%7D_%5Cmathsf%7BN%7D%5C%2Ce%5C%3B%5Cmathbf%7Belse%7D%5C%3B%5Cemptyset_%5Cmathsf%7BN%7D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bcomprehension%7D+%5C%7D+%5C%5C+%26+%5B+e+%5Cmid+b+%5D_%5Cmathsf%7BN%7D+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;phi&#92;,[ e &#92;mid b ]_&#92;mathsf{M} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{comprehension} &#92;} &#92;&#92; &amp; &#92;phi&#92;,(&#92;mathbf{if}&#92;;b&#92;;&#92;mathbf{then}&#92;;&#92;mathit{return}_&#92;mathsf{M}&#92;,e&#92;;&#92;mathbf{else}&#92;;&#92;emptyset_&#92;mathsf{M}) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{lift out the conditional} &#92;} &#92;&#92; &amp; &#92;mathbf{if}&#92;;b&#92;;&#92;mathbf{then}&#92;;&#92;phi&#92;,(&#92;mathit{return}_&#92;mathsf{M}&#92;,e)&#92;;&#92;mathbf{else}&#92;;&#92;phi&#92;,&#92;emptyset_&#92;mathsf{M} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{ringad morphism} &#92;} &#92;&#92; &amp; &#92;mathbf{if}&#92;;b&#92;;&#92;mathbf{then}&#92;;&#92;mathit{return}_&#92;mathsf{N}&#92;,e&#92;;&#92;mathbf{else}&#92;;&#92;emptyset_&#92;mathsf{N} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{comprehension} &#92;} &#92;&#92; &amp; [ e &#92;mid b ]_&#92;mathsf{N} &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;phi&#92;,[ e &#92;mid b ]_&#92;mathsf{M} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{comprehension} &#92;} &#92;&#92; &amp; &#92;phi&#92;,(&#92;mathbf{if}&#92;;b&#92;;&#92;mathbf{then}&#92;;&#92;mathit{return}_&#92;mathsf{M}&#92;,e&#92;;&#92;mathbf{else}&#92;;&#92;emptyset_&#92;mathsf{M}) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{lift out the conditional} &#92;} &#92;&#92; &amp; &#92;mathbf{if}&#92;;b&#92;;&#92;mathbf{then}&#92;;&#92;phi&#92;,(&#92;mathit{return}_&#92;mathsf{M}&#92;,e)&#92;;&#92;mathbf{else}&#92;;&#92;phi&#92;,&#92;emptyset_&#92;mathsf{M} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{ringad morphism} &#92;} &#92;&#92; &amp; &#92;mathbf{if}&#92;;b&#92;;&#92;mathbf{then}&#92;;&#92;mathit{return}_&#92;mathsf{N}&#92;,e&#92;;&#92;mathbf{else}&#92;;&#92;emptyset_&#92;mathsf{N} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{comprehension} &#92;} &#92;&#92; &amp; [ e &#92;mid b ]_&#92;mathsf{N} &#92;end{array} ' class='latex' />
</p></blockquote>
<p> For generators: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Bll%7D+%26+%5Cphi%5C%2C%5B+e+%5Cmid+p+%5Cleftarrow+m+%5D_%5Cmathsf%7BM%7D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bcomprehension%7D+%5C%7D+%5C%5C+%26+%5Cphi%5C%2C%28%5Cmathbf%7Blet%7D%5C%3Bh%5C%2Cp+%3D+%5Cmathit%7Breturn%7D_%5Cmathsf%7BM%7D%5C%2Ce+%3B+h%5C%2C%5C_+%3D+%5Cemptyset_%5Cmathsf%7BM%7D%5C%3B%5Cmathbf%7Bin%7D%5C%3B%5Cmathit%7Bjoin%7D_%5Cmathsf%7BM%7D%5C%2C%28%5Cmathit%7Bmap%7D_%5Cmathsf%7BM%7D%5C%2Ch%5C%2Cm%29%29+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Blift+out+the+%5Ctextbf%7Blet%7D%7D+%5C%7D+%5C%5C+%26+%5Cmathbf%7Blet%7D%5C%3Bh%5C%2Cp+%3D+%5Cmathit%7Breturn%7D_%5Cmathsf%7BM%7D%5C%2Ce+%3B+h%5C%2C%5C_+%3D+%5Cemptyset_%5Cmathsf%7BM%7D%5C%3B%5Cmathbf%7Bin%7D%5C%3B%5Cphi%5C%2C%28%5Cmathit%7Bjoin%7D_%5Cmathsf%7BM%7D%5C%2C%28%5Cmathit%7Bmap%7D_%5Cmathsf%7BM%7D%5C%2Ch%5C%2Cm%29%29+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bringad+morphism%2C+functors%7D+%5C%7D+%5C%5C+%26+%5Cmathbf%7Blet%7D%5C%3Bh%5C%2Cp+%3D+%5Cmathit%7Breturn%7D_%5Cmathsf%7BM%7D%5C%2Ce+%3B+h%5C%2C%5C_+%3D+%5Cemptyset_%5Cmathsf%7BM%7D%5C%3B%5Cmathbf%7Bin%7D%5C%3B%5Cmathit%7Bjoin%7D_%5Cmathsf%7BN%7D%5C%2C%28%5Cphi%5C%2C%28%5Cmathit%7Bmap%7D_%5Cmathsf%7BM%7D%5C%2C%28%5Cphi+%5Ccdot+h%29%5C%2Cm%29%29+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Blet+%5C%28h%27+%3D+%5Cphi+%5Ccdot+h%5C%29%7D+%5C%7D+%5C%5C+%26+%5Cmathbf%7Blet%7D%5C%3Bh%27%5C%2Cp+%3D+%5Cphi%5C%2C%28%5Cmathit%7Breturn%7D_%5Cmathsf%7BM%7D%5C%2Ce%29+%3B+h%27%5C%2C%5C_+%3D+%5Cphi%5C%2C%5Cemptyset_%5Cmathsf%7BM%7D%5C%3B%5Cmathbf%7Bin%7D%5C%3B%5Cmathit%7Bjoin%7D_%5Cmathsf%7BN%7D%5C%2C%28%5Cphi%5C%2C%28%5Cmathit%7Bmap%7D_%5Cmathsf%7BM%7D%5C%2Ch%27%5C%2Cm%29%29+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bringad+morphism%2C+induction%7D+%5C%7D+%5C%5C+%26+%5Cmathbf%7Blet%7D%5C%3Bh%27%5C%2Cp+%3D+%5Cmathit%7Breturn%7D_%5Cmathsf%7BN%7D%5C%2Ce+%3B+h%27%5C%2C%5C_+%3D+%5Cemptyset_%5Cmathsf%7BN%7D%5C%3B%5Cmathbf%7Bin%7D%5C%3B%5Cmathit%7Bjoin%7D_%5Cmathsf%7BN%7D%5C%2C%28%5Cphi%5C%2C%28%5Cmathit%7Bmap%7D_%5Cmathsf%7BM%7D%5C%2Ch%27%5C%2Cm%29%29+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bnaturality+of+%5C%28%5Cphi%5C%29%7D+%5C%7D+%5C%5C+%26+%5Cmathbf%7Blet%7D%5C%3Bh%27%5C%2Cp+%3D+%5Cmathit%7Breturn%7D_%5Cmathsf%7BN%7D%5C%2Ce+%3B+h%27%5C%2C%5C_+%3D+%5Cemptyset_%5Cmathsf%7BN%7D%5C%3B%5Cmathbf%7Bin%7D%5C%3B%5Cmathit%7Bjoin%7D_%5Cmathsf%7BN%7D%5C%2C%28%5Cmathit%7Bmap%7D_%5Cmathsf%7BN%7D%5C%2Ch%27%5C%2C%28%5Cphi%5C%2Cm%29%29+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bcomprehension%7D+%5C%7D+%5C%5C+%26+%5B+e+%5Cmid+p+%5Cleftarrow+%5Cphi%5C%2Cm+%5D_%5Cmathsf%7BN%7D+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;phi&#92;,[ e &#92;mid p &#92;leftarrow m ]_&#92;mathsf{M} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{comprehension} &#92;} &#92;&#92; &amp; &#92;phi&#92;,(&#92;mathbf{let}&#92;;h&#92;,p = &#92;mathit{return}_&#92;mathsf{M}&#92;,e ; h&#92;,&#92;_ = &#92;emptyset_&#92;mathsf{M}&#92;;&#92;mathbf{in}&#92;;&#92;mathit{join}_&#92;mathsf{M}&#92;,(&#92;mathit{map}_&#92;mathsf{M}&#92;,h&#92;,m)) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{lift out the &#92;textbf{let}} &#92;} &#92;&#92; &amp; &#92;mathbf{let}&#92;;h&#92;,p = &#92;mathit{return}_&#92;mathsf{M}&#92;,e ; h&#92;,&#92;_ = &#92;emptyset_&#92;mathsf{M}&#92;;&#92;mathbf{in}&#92;;&#92;phi&#92;,(&#92;mathit{join}_&#92;mathsf{M}&#92;,(&#92;mathit{map}_&#92;mathsf{M}&#92;,h&#92;,m)) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{ringad morphism, functors} &#92;} &#92;&#92; &amp; &#92;mathbf{let}&#92;;h&#92;,p = &#92;mathit{return}_&#92;mathsf{M}&#92;,e ; h&#92;,&#92;_ = &#92;emptyset_&#92;mathsf{M}&#92;;&#92;mathbf{in}&#92;;&#92;mathit{join}_&#92;mathsf{N}&#92;,(&#92;phi&#92;,(&#92;mathit{map}_&#92;mathsf{M}&#92;,(&#92;phi &#92;cdot h)&#92;,m)) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{let &#92;(h&#039; = &#92;phi &#92;cdot h&#92;)} &#92;} &#92;&#92; &amp; &#92;mathbf{let}&#92;;h&#039;&#92;,p = &#92;phi&#92;,(&#92;mathit{return}_&#92;mathsf{M}&#92;,e) ; h&#039;&#92;,&#92;_ = &#92;phi&#92;,&#92;emptyset_&#92;mathsf{M}&#92;;&#92;mathbf{in}&#92;;&#92;mathit{join}_&#92;mathsf{N}&#92;,(&#92;phi&#92;,(&#92;mathit{map}_&#92;mathsf{M}&#92;,h&#039;&#92;,m)) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{ringad morphism, induction} &#92;} &#92;&#92; &amp; &#92;mathbf{let}&#92;;h&#039;&#92;,p = &#92;mathit{return}_&#92;mathsf{N}&#92;,e ; h&#039;&#92;,&#92;_ = &#92;emptyset_&#92;mathsf{N}&#92;;&#92;mathbf{in}&#92;;&#92;mathit{join}_&#92;mathsf{N}&#92;,(&#92;phi&#92;,(&#92;mathit{map}_&#92;mathsf{M}&#92;,h&#039;&#92;,m)) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{naturality of &#92;(&#92;phi&#92;)} &#92;} &#92;&#92; &amp; &#92;mathbf{let}&#92;;h&#039;&#92;,p = &#92;mathit{return}_&#92;mathsf{N}&#92;,e ; h&#039;&#92;,&#92;_ = &#92;emptyset_&#92;mathsf{N}&#92;;&#92;mathbf{in}&#92;;&#92;mathit{join}_&#92;mathsf{N}&#92;,(&#92;mathit{map}_&#92;mathsf{N}&#92;,h&#039;&#92;,(&#92;phi&#92;,m)) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{comprehension} &#92;} &#92;&#92; &amp; [ e &#92;mid p &#92;leftarrow &#92;phi&#92;,m ]_&#92;mathsf{N} &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;phi&#92;,[ e &#92;mid p &#92;leftarrow m ]_&#92;mathsf{M} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{comprehension} &#92;} &#92;&#92; &amp; &#92;phi&#92;,(&#92;mathbf{let}&#92;;h&#92;,p = &#92;mathit{return}_&#92;mathsf{M}&#92;,e ; h&#92;,&#92;_ = &#92;emptyset_&#92;mathsf{M}&#92;;&#92;mathbf{in}&#92;;&#92;mathit{join}_&#92;mathsf{M}&#92;,(&#92;mathit{map}_&#92;mathsf{M}&#92;,h&#92;,m)) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{lift out the &#92;textbf{let}} &#92;} &#92;&#92; &amp; &#92;mathbf{let}&#92;;h&#92;,p = &#92;mathit{return}_&#92;mathsf{M}&#92;,e ; h&#92;,&#92;_ = &#92;emptyset_&#92;mathsf{M}&#92;;&#92;mathbf{in}&#92;;&#92;phi&#92;,(&#92;mathit{join}_&#92;mathsf{M}&#92;,(&#92;mathit{map}_&#92;mathsf{M}&#92;,h&#92;,m)) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{ringad morphism, functors} &#92;} &#92;&#92; &amp; &#92;mathbf{let}&#92;;h&#92;,p = &#92;mathit{return}_&#92;mathsf{M}&#92;,e ; h&#92;,&#92;_ = &#92;emptyset_&#92;mathsf{M}&#92;;&#92;mathbf{in}&#92;;&#92;mathit{join}_&#92;mathsf{N}&#92;,(&#92;phi&#92;,(&#92;mathit{map}_&#92;mathsf{M}&#92;,(&#92;phi &#92;cdot h)&#92;,m)) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{let &#92;(h&#039; = &#92;phi &#92;cdot h&#92;)} &#92;} &#92;&#92; &amp; &#92;mathbf{let}&#92;;h&#039;&#92;,p = &#92;phi&#92;,(&#92;mathit{return}_&#92;mathsf{M}&#92;,e) ; h&#039;&#92;,&#92;_ = &#92;phi&#92;,&#92;emptyset_&#92;mathsf{M}&#92;;&#92;mathbf{in}&#92;;&#92;mathit{join}_&#92;mathsf{N}&#92;,(&#92;phi&#92;,(&#92;mathit{map}_&#92;mathsf{M}&#92;,h&#039;&#92;,m)) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{ringad morphism, induction} &#92;} &#92;&#92; &amp; &#92;mathbf{let}&#92;;h&#039;&#92;,p = &#92;mathit{return}_&#92;mathsf{N}&#92;,e ; h&#039;&#92;,&#92;_ = &#92;emptyset_&#92;mathsf{N}&#92;;&#92;mathbf{in}&#92;;&#92;mathit{join}_&#92;mathsf{N}&#92;,(&#92;phi&#92;,(&#92;mathit{map}_&#92;mathsf{M}&#92;,h&#039;&#92;,m)) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{naturality of &#92;(&#92;phi&#92;)} &#92;} &#92;&#92; &amp; &#92;mathbf{let}&#92;;h&#039;&#92;,p = &#92;mathit{return}_&#92;mathsf{N}&#92;,e ; h&#039;&#92;,&#92;_ = &#92;emptyset_&#92;mathsf{N}&#92;;&#92;mathbf{in}&#92;;&#92;mathit{join}_&#92;mathsf{N}&#92;,(&#92;mathit{map}_&#92;mathsf{N}&#92;,h&#039;&#92;,(&#92;phi&#92;,m)) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{comprehension} &#92;} &#92;&#92; &amp; [ e &#92;mid p &#92;leftarrow &#92;phi&#92;,m ]_&#92;mathsf{N} &#92;end{array} ' class='latex' />
</p></blockquote>
<p> And for sequences of qualifiers: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Bll%7D+%26+%5Cphi%5C%2C%5B+e+%5Cmid+q%2C+q%27+%5D_%5Cmathsf%7BM%7D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bcomprehension%7D+%5C%7D+%5C%5C+%26+%5Cphi%5C%2C%28%5Cmathit%7Bjoin%7D%5C%2C%5B+%5B+e+%5Cmid+q%27+%5D_%5Cmathsf%7BM%7D+%5Cmid+q+%5D_%5Cmathsf%7BM%7D%29+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bringad+morphism%7D+%5C%7D+%5C%5C+%26+%5Cphi%5C%2C%28%5Cmathit%7Bmap%7D%5C%2C%5Cphi%5C%2C%5B+%5B+e+%5Cmid+q%27+%5D_%5Cmathsf%7BM%7D+%5Cmid+q+%5D_%5Cmathsf%7BM%7D%29+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bmap+over+comprehension%7D+%5C%7D+%5C%5C+%26+%5Cphi%5C%2C%5B+%5Cphi%5C%2C%5B+e+%5Cmid+q%27+%5D_%5Cmathsf%7BM%7D+%5Cmid+q+%5D_%5Cmathsf%7BM%7D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Binduction%7D+%5C%7D+%5C%5C+%26+%5B+%5B+e+%5Cmid+q%27+%5D_%5Cmathsf%7BN%7D+%5Cmid+q+%5D_%5Cmathsf%7BN%7D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bcomprehension%7D+%5C%7D+%5C%5C+%26+%5B+e+%5Cmid+q%2C+q%27+%5D_%5Cmathsf%7BN%7D+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;phi&#92;,[ e &#92;mid q, q&#039; ]_&#92;mathsf{M} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{comprehension} &#92;} &#92;&#92; &amp; &#92;phi&#92;,(&#92;mathit{join}&#92;,[ [ e &#92;mid q&#039; ]_&#92;mathsf{M} &#92;mid q ]_&#92;mathsf{M}) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{ringad morphism} &#92;} &#92;&#92; &amp; &#92;phi&#92;,(&#92;mathit{map}&#92;,&#92;phi&#92;,[ [ e &#92;mid q&#039; ]_&#92;mathsf{M} &#92;mid q ]_&#92;mathsf{M}) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{map over comprehension} &#92;} &#92;&#92; &amp; &#92;phi&#92;,[ &#92;phi&#92;,[ e &#92;mid q&#039; ]_&#92;mathsf{M} &#92;mid q ]_&#92;mathsf{M} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{induction} &#92;} &#92;&#92; &amp; [ [ e &#92;mid q&#039; ]_&#92;mathsf{N} &#92;mid q ]_&#92;mathsf{N} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{comprehension} &#92;} &#92;&#92; &amp; [ e &#92;mid q, q&#039; ]_&#92;mathsf{N} &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;phi&#92;,[ e &#92;mid q, q&#039; ]_&#92;mathsf{M} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{comprehension} &#92;} &#92;&#92; &amp; &#92;phi&#92;,(&#92;mathit{join}&#92;,[ [ e &#92;mid q&#039; ]_&#92;mathsf{M} &#92;mid q ]_&#92;mathsf{M}) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{ringad morphism} &#92;} &#92;&#92; &amp; &#92;phi&#92;,(&#92;mathit{map}&#92;,&#92;phi&#92;,[ [ e &#92;mid q&#039; ]_&#92;mathsf{M} &#92;mid q ]_&#92;mathsf{M}) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{map over comprehension} &#92;} &#92;&#92; &amp; &#92;phi&#92;,[ &#92;phi&#92;,[ e &#92;mid q&#039; ]_&#92;mathsf{M} &#92;mid q ]_&#92;mathsf{M} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{induction} &#92;} &#92;&#92; &amp; [ [ e &#92;mid q&#039; ]_&#92;mathsf{N} &#92;mid q ]_&#92;mathsf{N} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{comprehension} &#92;} &#92;&#92; &amp; [ e &#92;mid q, q&#039; ]_&#92;mathsf{N} &#92;end{array} ' class='latex' />
</p></blockquote>
<p>
For example, if <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bbag2set%7D+%3A+%5Cmathsf%7BBag%7D+%5Cmathbin%7B%5Cstackrel%7B.%7D%7B%5Cto%7D%7D+%5Cmathsf%7BSet%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{bag2set} : &#92;mathsf{Bag} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{Set}}' title='{&#92;mathit{bag2set} : &#92;mathsf{Bag} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{Set}}' class='latex' /> is the obvious ringad morphism from bags to sets, discarding information about the multiplicity of repeated elements, and <img src='http://s0.wp.com/latex.php?latex=%7Bx%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{x}' title='{x}' class='latex' /> a bag of numbers, then </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathit%7Bbag2set%7D%5C%2C%5Ba%5E2+%5Cmid+a+%5Cleftarrow+x%2C+%5Cmathit%7Bodd%7D%5C%2Ca%5D_%5Cmathsf%7BBag%7D+%3D+%5Ba%5E2+%5Cmid+a+%5Cleftarrow+%5Cmathit%7Bbag2set%7D%5C%2Cx%2C+%5Cmathit%7Bodd%7D%5C%2Ca%5D_%5Cmathsf%7BSet%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathit{bag2set}&#92;,[a^2 &#92;mid a &#92;leftarrow x, &#92;mathit{odd}&#92;,a]_&#92;mathsf{Bag} = [a^2 &#92;mid a &#92;leftarrow &#92;mathit{bag2set}&#92;,x, &#92;mathit{odd}&#92;,a]_&#92;mathsf{Set} ' title='&#92;displaystyle  &#92;mathit{bag2set}&#92;,[a^2 &#92;mid a &#92;leftarrow x, &#92;mathit{odd}&#92;,a]_&#92;mathsf{Bag} = [a^2 &#92;mid a &#92;leftarrow &#92;mathit{bag2set}&#92;,x, &#92;mathit{odd}&#92;,a]_&#92;mathsf{Set} ' class='latex' />
</p></blockquote>
<p> and both yield the set of squares of the odd members of <img src='http://s0.wp.com/latex.php?latex=%7Bx%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{x}' title='{x}' class='latex' />. As a notational convenience, we might elide use of the ringad morphism when it is &#8220;obvious from context&#8221;&mdash;we might write just <img src='http://s0.wp.com/latex.php?latex=%7B%5Ba%5E2+%5Cmid+a+%5Cleftarrow+x%2C+%5Cmathit%7Bodd%7D%5C%2Ca%5D_%5Cmathsf%7BSet%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{[a^2 &#92;mid a &#92;leftarrow x, &#92;mathit{odd}&#92;,a]_&#92;mathsf{Set}}' title='{[a^2 &#92;mid a &#92;leftarrow x, &#92;mathit{odd}&#92;,a]_&#92;mathsf{Set}}' class='latex' /> even when <img src='http://s0.wp.com/latex.php?latex=%7Bx%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{x}' title='{x}' class='latex' /> is a bag, relying on the &#8220;obvious&#8221; morphism <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bbag2set%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{bag2set}}' title='{&#92;mathit{bag2set}}' class='latex' />. This would allow us to write, for example, </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5B+a%2Bb+%5Cmid+a+%5Cleftarrow+%5B1%2C2%2C3%5D%2C+b+%5Cleftarrow+%5Clangle4%2C4%2C5%5Crangle+%5D_%5Cmathsf%7BSet%7D+%3D+%5C%7B+5%2C6%2C7%2C8+%5C%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  [ a+b &#92;mid a &#92;leftarrow [1,2,3], b &#92;leftarrow &#92;langle4,4,5&#92;rangle ]_&#92;mathsf{Set} = &#92;{ 5,6,7,8 &#92;} ' title='&#92;displaystyle  [ a+b &#92;mid a &#92;leftarrow [1,2,3], b &#92;leftarrow &#92;langle4,4,5&#92;rangle ]_&#92;mathsf{Set} = &#92;{ 5,6,7,8 &#92;} ' class='latex' />
</p></blockquote>
<p> (writing <img src='http://s0.wp.com/latex.php?latex=%7B%5Clangle%5Cldots%5Crangle%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;langle&#92;ldots&#92;rangle}' title='{&#92;langle&#92;ldots&#92;rangle}' class='latex' /> for the extension of a bag), instead of the more pedantic </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5B+a%2Bb+%5Cmid+a+%5Cleftarrow+%5Cmathit%7Blist2set%7D%5C%2C%5B1%2C2%2C3%5D%2C+b+%5Cleftarrow+%5Cmathit%7Bbag2set%7D%5C%2C%5Clangle4%2C4%2C5%5Crangle+%5D_%5Cmathsf%7BSet%7D+%3D+%5C%7B+5%2C6%2C7%2C8+%5C%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  [ a+b &#92;mid a &#92;leftarrow &#92;mathit{list2set}&#92;,[1,2,3], b &#92;leftarrow &#92;mathit{bag2set}&#92;,&#92;langle4,4,5&#92;rangle ]_&#92;mathsf{Set} = &#92;{ 5,6,7,8 &#92;} ' title='&#92;displaystyle  [ a+b &#92;mid a &#92;leftarrow &#92;mathit{list2set}&#92;,[1,2,3], b &#92;leftarrow &#92;mathit{bag2set}&#92;,&#92;langle4,4,5&#92;rangle ]_&#92;mathsf{Set} = &#92;{ 5,6,7,8 &#92;} ' class='latex' />
</p></blockquote>
<p> There is a forgetful function from any poorer member of the Boom hierarchy to a richer one, flattening some distinctions by imposing additional laws&mdash;for example, from bags to sets, flattening distinctions concerning multiplicity&mdash;and I would class these forgetful functions as &#8220;obvious&#8221; morphisms. On the other hand, any morphisms in the opposite direction&mdash;such as sorting, from bags to lists, and one-of-each, from sets to bags&mdash;are not &#8220;obvious&#8221;, and so should not be elided; and similarly, I&#8217;m not sure that I could justify as &#8220;obvious&#8221; any morphisms involving non-members of the Boom Hierarchy, such as probability distributions.</p>
<p>
<br />  <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=patternsinfp.wordpress.com&#038;blog=15593982&#038;post=227&#038;subd=patternsinfp&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://patternsinfp.wordpress.com/2012/01/19/comprehensions/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/39d7e0397cba13d1494d9a48c1f42167?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jeremygibbons</media:title>
		</media:content>
	</item>
		<item>
		<title>Upwards and downwards accumulations</title>
		<link>http://patternsinfp.wordpress.com/2011/07/12/upwards-and-downwards-accumulations/</link>
		<comments>http://patternsinfp.wordpress.com/2011/07/12/upwards-and-downwards-accumulations/#comments</comments>
		<pubDate>Tue, 12 Jul 2011 10:42:05 +0000</pubDate>
		<dc:creator>jeremygibbons</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://patternsinfp.wordpress.com/?p=213</guid>
		<description><![CDATA[Continuing my work in regress, this post revisits&#8212;with the benefit of much hindsight&#8212;what I was working on for my DPhil thesis (which was summarized in a paper at MPC 1992) and in subsequent papers at MPC 1998 and in SCP &#8230; <a href="http://patternsinfp.wordpress.com/2011/07/12/upwards-and-downwards-accumulations/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=patternsinfp.wordpress.com&#038;blog=15593982&#038;post=213&#038;subd=patternsinfp&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>
 Continuing my <a href="http://www.palgrave.com/products/title.aspx?pid=262597">work in regress</a>, this post revisits&mdash;with the benefit of much hindsight&mdash;what I was working on for <a href="http://web.comlab.ox.ac.uk/publications/publication2369-abstract.html">my DPhil thesis</a> (which was summarized in <a href="http://web.comlab.ox.ac.uk/publications/publication2367-abstract.html">a paper at MPC 1992</a>) and in subsequent papers <a href="http://web.comlab.ox.ac.uk/publications/publication2349-abstract.html">at MPC 1998</a> and <a href="http://web.comlab.ox.ac.uk/publications/publication2366-abstract.html">in SCP in 2000</a>. This is the topic of <em>accumulations</em> on data structures, which distribute information across the data structure. List instances are familiar from the Haskell standard libraries (and, to those with a long memory, from APL); my thesis presented instances for a variety of tree datatypes; and the later work was about making it datatype-generic. I now have a much better way of doing it, using Conor McBride&#8217;s <em>derivatives</em>.</p>
<p><h2> Accumulations </h2>
<p>
<em>Accumulations</em> or <em>scans</em> distribute information contained in a data structure across that data structure in a given direction. The paradigmatic example is computing the running totals of a list of numbers, which can be thought of as distributing the numbers rightwards across the list, summing them as you go. In Haskell, this is an instance of the <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bscanl%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{scanl}}' title='{&#92;mathit{scanl}}' class='latex' /> operator: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Cmathit%7Bscanl%7D+%26%3A%3A%26+%28%5Cbeta+%5Crightarrow+%5Calpha+%5Crightarrow+%5Cbeta%29+%5Crightarrow+%5Cbeta+%5Crightarrow+%5B%5Calpha%5D+%5Crightarrow+%5B%5Cbeta%5D+%5C%5C+%5Cmathit%7Bscanl%7D%5C%2Cf%5C%2Ce%5C%2C%5B%5C%2C%5D+%26%3D%26+%5Be%5D+%5C%5C+%5Cmathit%7Bscanl%7D%5C%2Cf%5C%2Ce%5C%2C%28a%3Ax%29+%26%3D%26+e+%3A+%5Cmathit%7Bscanl%7D%5C%2Cf%5C%2C%28f%5C%2Ce%5C%2Ca%29%5C%2Cx+%5Cmedskip+%5C%5C+%5Cmathit%7Btotals%7D+%26%3A%3A%26+%5B%7B%5Cmathbb+Z%7D%5D+%5Crightarrow+%5B%7B%5Cmathbb+Z%7D%5D+%5C%5C+%5Cmathit%7Btotals%7D+%26%3D%26+%5Cmathit%7Bscanl%7D%5C%2C%28%2B%29%5C%2C0+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{scanl} &amp;::&amp; (&#92;beta &#92;rightarrow &#92;alpha &#92;rightarrow &#92;beta) &#92;rightarrow &#92;beta &#92;rightarrow [&#92;alpha] &#92;rightarrow [&#92;beta] &#92;&#92; &#92;mathit{scanl}&#92;,f&#92;,e&#92;,[&#92;,] &amp;=&amp; [e] &#92;&#92; &#92;mathit{scanl}&#92;,f&#92;,e&#92;,(a:x) &amp;=&amp; e : &#92;mathit{scanl}&#92;,f&#92;,(f&#92;,e&#92;,a)&#92;,x &#92;medskip &#92;&#92; &#92;mathit{totals} &amp;::&amp; [{&#92;mathbb Z}] &#92;rightarrow [{&#92;mathbb Z}] &#92;&#92; &#92;mathit{totals} &amp;=&amp; &#92;mathit{scanl}&#92;,(+)&#92;,0 &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{scanl} &amp;::&amp; (&#92;beta &#92;rightarrow &#92;alpha &#92;rightarrow &#92;beta) &#92;rightarrow &#92;beta &#92;rightarrow [&#92;alpha] &#92;rightarrow [&#92;beta] &#92;&#92; &#92;mathit{scanl}&#92;,f&#92;,e&#92;,[&#92;,] &amp;=&amp; [e] &#92;&#92; &#92;mathit{scanl}&#92;,f&#92;,e&#92;,(a:x) &amp;=&amp; e : &#92;mathit{scanl}&#92;,f&#92;,(f&#92;,e&#92;,a)&#92;,x &#92;medskip &#92;&#92; &#92;mathit{totals} &amp;::&amp; [{&#92;mathbb Z}] &#92;rightarrow [{&#92;mathbb Z}] &#92;&#92; &#92;mathit{totals} &amp;=&amp; &#92;mathit{scanl}&#92;,(+)&#92;,0 &#92;end{array} ' class='latex' />
</p></blockquote>
<p> A special case of this pattern is to distribute the elements of a list rightwards across the list, simply collecting them as you go, rather than summing them. That&#8217;s the <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Binits%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{inits}}' title='{&#92;mathit{inits}}' class='latex' /> function, and it too is an instance of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bscanl%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{scanl}}' title='{&#92;mathit{scanl}}' class='latex' />: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathit%7Binits%7D+%3D+%5Cmathit%7Bscanl%7D%5C%2C%5Cmathit%7Bsnoc%7D%5C%2C%5B%5C%2C%5D+%5Cquad%5Cmathbf%7Bwhere%7D%5C%3B+%5Cmathit%7Bsnoc%7D%5C%2Cx%5C%2Ca+%3D+x+%5Cmathbin%7B%7B%2B%7D%5C%21%5C%21%5C%21%7B%2B%7D%7D+%5Ba%5D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathit{inits} = &#92;mathit{scanl}&#92;,&#92;mathit{snoc}&#92;,[&#92;,] &#92;quad&#92;mathbf{where}&#92;; &#92;mathit{snoc}&#92;,x&#92;,a = x &#92;mathbin{{+}&#92;!&#92;!&#92;!{+}} [a] ' title='&#92;displaystyle  &#92;mathit{inits} = &#92;mathit{scanl}&#92;,&#92;mathit{snoc}&#92;,[&#92;,] &#92;quad&#92;mathbf{where}&#92;; &#92;mathit{snoc}&#92;,x&#92;,a = x &#92;mathbin{{+}&#92;!&#92;!&#92;!{+}} [a] ' class='latex' />
</p></blockquote>
<p> It&#8217;s particularly special, in the sense that it is the most basic <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bscanl%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{scanl}}' title='{&#92;mathit{scanl}}' class='latex' />; any other instance can be expressed in terms of it: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathit%7Bscanl%7D%5C%2Cf%5C%2Ce+%3D+%5Cmathit%7Bmap%7D%5C%2C%28%5Cmathit%7Bfoldl%7D%5C%2Cf%5C%2Ce%29+%5Ccdot+%5Cmathit%7Binits%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathit{scanl}&#92;,f&#92;,e = &#92;mathit{map}&#92;,(&#92;mathit{foldl}&#92;,f&#92;,e) &#92;cdot &#92;mathit{inits} ' title='&#92;displaystyle  &#92;mathit{scanl}&#92;,f&#92;,e = &#92;mathit{map}&#92;,(&#92;mathit{foldl}&#92;,f&#92;,e) &#92;cdot &#92;mathit{inits} ' class='latex' />
</p></blockquote>
<p> This is called the <em>Scan Lemma</em> for <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bscanl%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{scanl}}' title='{&#92;mathit{scanl}}' class='latex' />. Roughly speaking, it states that a <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bscanl%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{scanl}}' title='{&#92;mathit{scanl}}' class='latex' /> replaces every node of a list with a <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfoldl%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{foldl}}' title='{&#92;mathit{foldl}}' class='latex' /> applied to that node&#8217;s predecessors. Read from right to left, the scan lemma is an efficiency-improving transformation, eliminating duplicate computations; but note that this only works on expressions <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bmap%7D%5C%2Cf+%5Ccdot+%5Cmathit%7Binits%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{map}&#92;,f &#92;cdot &#92;mathit{inits}}' title='{&#92;mathit{map}&#92;,f &#92;cdot &#92;mathit{inits}}' class='latex' /> where <img src='http://s0.wp.com/latex.php?latex=%7Bf%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f}' title='{f}' class='latex' /> is a <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfoldl%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{foldl}}' title='{&#92;mathit{foldl}}' class='latex' />, because only then are there duplicate computations to eliminate. It&#8217;s an important result, because it relates a clear and simple specification on the right to a more efficient implementation on the left.</p>
<p>
However, the left-to-right operators <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Binits%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{inits}}' title='{&#92;mathit{inits}}' class='latex' />, <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfoldl%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{foldl}}' title='{&#92;mathit{foldl}}' class='latex' />, and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bscanl%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{scanl}}' title='{&#92;mathit{scanl}}' class='latex' /> are a little awkward in Haskell, because they go against the grain of the cons-based (ie, right-to-left) structure of lists. I leave as a simple exercise for the reader the task of writing the more natural <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Btails%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{tails}}' title='{&#92;mathit{tails}}' class='latex' />, <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfoldr%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{foldr}}' title='{&#92;mathit{foldr}}' class='latex' />, and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bscanr%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{scanr}}' title='{&#92;mathit{scanr}}' class='latex' />, and identifying the relationships between them. Conversely, one can view <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Binits%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{inits}}' title='{&#92;mathit{inits}}' class='latex' /> etc as the natural operators for snoc-based lists, which are constructed from nil and snoc rather than from nil and cons.</p>
<p><h2> Upwards and downwards accumulations on binary trees </h2>
<p>
What would <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Binits%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{inits}}' title='{&#92;mathit{inits}}' class='latex' />, <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Btails%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{tails}}' title='{&#92;mathit{tails}}' class='latex' />, <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bscanl%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{scanl}}' title='{&#92;mathit{scanl}}' class='latex' />, etc look like on different&mdash;and in particular, non-linear&mdash;datatypes? Let&#8217;s consider a simple instance, for homogeneous binary trees; that is, trees with a label at both internal and external nodes. </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathbf%7Bdata%7D%5C%3B%5Cmathsf%7BTree%7D%5C%2C%5Calpha+%3D+%5Cmathit%7BLeaf%7D%5C%2C%5Calpha+%5Cmid+%5Cmathit%7BFork%7D%5C%2C%5Calpha%5C%2C%28%5Cmathsf%7BTree%7D%5C%2C%5Calpha%29%5C%2C%28%5Cmathsf%7BTree%7D%5C%2C%5Calpha%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathbf{data}&#92;;&#92;mathsf{Tree}&#92;,&#92;alpha = &#92;mathit{Leaf}&#92;,&#92;alpha &#92;mid &#92;mathit{Fork}&#92;,&#92;alpha&#92;,(&#92;mathsf{Tree}&#92;,&#92;alpha)&#92;,(&#92;mathsf{Tree}&#92;,&#92;alpha) ' title='&#92;displaystyle  &#92;mathbf{data}&#92;;&#92;mathsf{Tree}&#92;,&#92;alpha = &#92;mathit{Leaf}&#92;,&#92;alpha &#92;mid &#92;mathit{Fork}&#92;,&#92;alpha&#92;,(&#92;mathsf{Tree}&#92;,&#92;alpha)&#92;,(&#92;mathsf{Tree}&#92;,&#92;alpha) ' class='latex' />
</p></blockquote>
<p> for which the obvious fold operator is </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Cmathit%7Bfold%7D+%26%3A%3A%26+%28%5Calpha%5Crightarrow%5Cbeta%29+%5Crightarrow+%28%5Calpha%5Crightarrow%5Cbeta%5Crightarrow%5Cbeta%5Crightarrow%5Cbeta%29+%5Crightarrow+%5Cmathsf%7BTree%7D%5C%2C%5Calpha+%5Crightarrow+%5Cbeta+%5C%5C+%5Cmathit%7Bfold%7D%5C%2Cf%5C%2Cg%5C%2C%28%5Cmathit%7BLeaf%7D%5C%2Ca%29+%26%3D%26+f%5C%2Ca+%5C%5C+%5Cmathit%7Bfold%7D%5C%2Cf%5C%2Cg%5C%2C%28%5Cmathit%7BFork%7D%5C%2Ca%5C%2Ct%5C%2Cu%29+%26%3D%26+g%5C%2Ca%5C%2C%28%5Cmathit%7Bfold%7D%5C%2Cf%5C%2Cg%5C%2Ct%29%5C%2C%28%5Cmathit%7Bfold%7D%5C%2Cf%5C%2Cg%5C%2Cu%29+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{fold} &amp;::&amp; (&#92;alpha&#92;rightarrow&#92;beta) &#92;rightarrow (&#92;alpha&#92;rightarrow&#92;beta&#92;rightarrow&#92;beta&#92;rightarrow&#92;beta) &#92;rightarrow &#92;mathsf{Tree}&#92;,&#92;alpha &#92;rightarrow &#92;beta &#92;&#92; &#92;mathit{fold}&#92;,f&#92;,g&#92;,(&#92;mathit{Leaf}&#92;,a) &amp;=&amp; f&#92;,a &#92;&#92; &#92;mathit{fold}&#92;,f&#92;,g&#92;,(&#92;mathit{Fork}&#92;,a&#92;,t&#92;,u) &amp;=&amp; g&#92;,a&#92;,(&#92;mathit{fold}&#92;,f&#92;,g&#92;,t)&#92;,(&#92;mathit{fold}&#92;,f&#92;,g&#92;,u) &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{fold} &amp;::&amp; (&#92;alpha&#92;rightarrow&#92;beta) &#92;rightarrow (&#92;alpha&#92;rightarrow&#92;beta&#92;rightarrow&#92;beta&#92;rightarrow&#92;beta) &#92;rightarrow &#92;mathsf{Tree}&#92;,&#92;alpha &#92;rightarrow &#92;beta &#92;&#92; &#92;mathit{fold}&#92;,f&#92;,g&#92;,(&#92;mathit{Leaf}&#92;,a) &amp;=&amp; f&#92;,a &#92;&#92; &#92;mathit{fold}&#92;,f&#92;,g&#92;,(&#92;mathit{Fork}&#92;,a&#92;,t&#92;,u) &amp;=&amp; g&#92;,a&#92;,(&#92;mathit{fold}&#92;,f&#92;,g&#92;,t)&#92;,(&#92;mathit{fold}&#92;,f&#92;,g&#92;,u) &#92;end{array} ' class='latex' />
</p></blockquote>
<p> I&#8217;m taking the view that the appropriate generalization is to distribute data &#8220;upwards&#8221; and &#8220;downwards&#8221; through such a tree&mdash;from the leaves towards the root, and vice versa. This does indeed specialize to the definitions we had on lists when you view them vertically in terms of their &#8220;cons&#8221; structure: they&#8217;re long thin trees, in which every parent has exactly one child. (An alternative view would be to look at distributing data horizontally through a tree, from left to right and vice versa. Perhaps I&#8217;ll come back to that another time.)</p>
<p>
The upwards direction is the easier one to deal with. An upwards accumulation labels every node of the tree with some function of its <em>descendants</em>; moreover, the descendants of a node themselves form a tree, so can be easily represented, and folded. So we can quite straightforwardly define: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Cmathit%7Bscanu%7D+%26%3A%3A%26+%28%5Calpha%5Crightarrow%5Cbeta%29+%5Crightarrow+%28%5Calpha%5Crightarrow%5Cbeta%5Crightarrow%5Cbeta%5Crightarrow%5Cbeta%29+%5Crightarrow+%5Cmathsf%7BTree%7D%5C%2C%5Calpha+%5Crightarrow+%5Cmathsf%7BTree%7D%5C%2C%5Cbeta+%5C%5C+%5Cmathit%7Bscanu%7D%5C%2Cf%5C%2Cg%5C%2C%28%5Cmathit%7BLeaf%7D%5C%2Ca%29+%26%3D%26+%5Cmathit%7BLeaf%7D%5C%2C%28f%5C%2Ca%29+%5C%5C+%5Cmathit%7Bscanu%7D%5C%2Cf%5C%2Cg%5C%2C%28%5Cmathit%7BFork%7D%5C%2Ca%5C%2Ct%5C%2Cu%29+%26%3D%26+%5Cmathit%7BFork%7D%5C%2C%28g%5C%2Ca%5C%2C%28%5Cmathit%7Broot%7D%5C%2Ct%27%29%5C%2C%28%5Cmathit%7Broot%7D%5C%2Cu%27%29%29%5C%2Ct%27%5C%2Cu%27+%5C%5C+%26+%26+%5Cquad%5Cmathbf%7Bwhere%7D%5C%3B+t%27+%3D+%5Cmathit%7Bscanu%7D%5C%2Cf%5C%2Cg%5C%2Ct+%3B+u%27+%3D+%5Cmathit%7Bscanu%7D%5C%2Cf%5C%2Cg%5C%2Cu+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{scanu} &amp;::&amp; (&#92;alpha&#92;rightarrow&#92;beta) &#92;rightarrow (&#92;alpha&#92;rightarrow&#92;beta&#92;rightarrow&#92;beta&#92;rightarrow&#92;beta) &#92;rightarrow &#92;mathsf{Tree}&#92;,&#92;alpha &#92;rightarrow &#92;mathsf{Tree}&#92;,&#92;beta &#92;&#92; &#92;mathit{scanu}&#92;,f&#92;,g&#92;,(&#92;mathit{Leaf}&#92;,a) &amp;=&amp; &#92;mathit{Leaf}&#92;,(f&#92;,a) &#92;&#92; &#92;mathit{scanu}&#92;,f&#92;,g&#92;,(&#92;mathit{Fork}&#92;,a&#92;,t&#92;,u) &amp;=&amp; &#92;mathit{Fork}&#92;,(g&#92;,a&#92;,(&#92;mathit{root}&#92;,t&#039;)&#92;,(&#92;mathit{root}&#92;,u&#039;))&#92;,t&#039;&#92;,u&#039; &#92;&#92; &amp; &amp; &#92;quad&#92;mathbf{where}&#92;; t&#039; = &#92;mathit{scanu}&#92;,f&#92;,g&#92;,t ; u&#039; = &#92;mathit{scanu}&#92;,f&#92;,g&#92;,u &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{scanu} &amp;::&amp; (&#92;alpha&#92;rightarrow&#92;beta) &#92;rightarrow (&#92;alpha&#92;rightarrow&#92;beta&#92;rightarrow&#92;beta&#92;rightarrow&#92;beta) &#92;rightarrow &#92;mathsf{Tree}&#92;,&#92;alpha &#92;rightarrow &#92;mathsf{Tree}&#92;,&#92;beta &#92;&#92; &#92;mathit{scanu}&#92;,f&#92;,g&#92;,(&#92;mathit{Leaf}&#92;,a) &amp;=&amp; &#92;mathit{Leaf}&#92;,(f&#92;,a) &#92;&#92; &#92;mathit{scanu}&#92;,f&#92;,g&#92;,(&#92;mathit{Fork}&#92;,a&#92;,t&#92;,u) &amp;=&amp; &#92;mathit{Fork}&#92;,(g&#92;,a&#92;,(&#92;mathit{root}&#92;,t&#039;)&#92;,(&#92;mathit{root}&#92;,u&#039;))&#92;,t&#039;&#92;,u&#039; &#92;&#92; &amp; &amp; &#92;quad&#92;mathbf{where}&#92;; t&#039; = &#92;mathit{scanu}&#92;,f&#92;,g&#92;,t ; u&#039; = &#92;mathit{scanu}&#92;,f&#92;,g&#92;,u &#92;end{array} ' class='latex' />
</p></blockquote>
<p> where <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Broot%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{root}}' title='{&#92;mathit{root}}' class='latex' /> yields the root of a tree: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Cmathit%7Broot%7D+%26%3A%3A%26+%5Cmathsf%7BTree%7D%5C%2C%5Calpha+%5Crightarrow+%5Calpha+%5C%5C+%5Cmathit%7Broot%7D%5C%2C%28%5Cmathit%7BLeaf%7D%5C%2Ca%29+%26%3D%26+a+%5C%5C+%5Cmathit%7Broot%7D%5C%2C%28%5Cmathit%7BFork%7D%5C%2Ca%5C%2Ct%5C%2Cu%29+%26%3D%26+a+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{root} &amp;::&amp; &#92;mathsf{Tree}&#92;,&#92;alpha &#92;rightarrow &#92;alpha &#92;&#92; &#92;mathit{root}&#92;,(&#92;mathit{Leaf}&#92;,a) &amp;=&amp; a &#92;&#92; &#92;mathit{root}&#92;,(&#92;mathit{Fork}&#92;,a&#92;,t&#92;,u) &amp;=&amp; a &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{root} &amp;::&amp; &#92;mathsf{Tree}&#92;,&#92;alpha &#92;rightarrow &#92;alpha &#92;&#92; &#92;mathit{root}&#92;,(&#92;mathit{Leaf}&#92;,a) &amp;=&amp; a &#92;&#92; &#92;mathit{root}&#92;,(&#92;mathit{Fork}&#92;,a&#92;,t&#92;,u) &amp;=&amp; a &#92;end{array} ' class='latex' />
</p></blockquote>
<p> As with lists, the most basic upwards scan uses the constructors themselves as arguments: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Cmathit%7Bsubtrees%7D+%26%3A%3A%26+%5Cmathsf%7BTree%7D%5C%2C%5Calpha+%5Crightarrow+%5Cmathsf%7BTree%7D%5C%2C%28%5Cmathsf%7BTree%7D%5C%2C%5Calpha%29+%5C%5C+%5Cmathit%7Bsubtrees%7D+%26%3D%26+%5Cmathit%7Bscanu%7D%5C%2C%5Cmathit%7BLeaf%7D%5C%2C%5Cmathit%7BFork%7D+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{subtrees} &amp;::&amp; &#92;mathsf{Tree}&#92;,&#92;alpha &#92;rightarrow &#92;mathsf{Tree}&#92;,(&#92;mathsf{Tree}&#92;,&#92;alpha) &#92;&#92; &#92;mathit{subtrees} &amp;=&amp; &#92;mathit{scanu}&#92;,&#92;mathit{Leaf}&#92;,&#92;mathit{Fork} &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{subtrees} &amp;::&amp; &#92;mathsf{Tree}&#92;,&#92;alpha &#92;rightarrow &#92;mathsf{Tree}&#92;,(&#92;mathsf{Tree}&#92;,&#92;alpha) &#92;&#92; &#92;mathit{subtrees} &amp;=&amp; &#92;mathit{scanu}&#92;,&#92;mathit{Leaf}&#92;,&#92;mathit{Fork} &#92;end{array} ' class='latex' />
</p></blockquote>
<p> and any other scan can be expressed, albeit less efficiently, in terms of this: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathit%7Bscanu%7D%5C%2Cf%5C%2Cg+%3D+%5Cmathit%7Bfmap%7D%5C%2C%28%5Cmathit%7Bfold%7D%5C%2Cf%5C%2Cg%29+%5Ccdot+%5Cmathit%7Bsubtrees%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathit{scanu}&#92;,f&#92;,g = &#92;mathit{fmap}&#92;,(&#92;mathit{fold}&#92;,f&#92;,g) &#92;cdot &#92;mathit{subtrees} ' title='&#92;displaystyle  &#92;mathit{scanu}&#92;,f&#92;,g = &#92;mathit{fmap}&#92;,(&#92;mathit{fold}&#92;,f&#92;,g) &#92;cdot &#92;mathit{subtrees} ' class='latex' />
</p></blockquote>
<p>
The downwards direction is more difficult, though. A downwards accumulation should label every node with some function of its <em>ancestors</em>; but these do not form another tree. For example, in the homogeneous binary tree
<p align="center"><img width="200" src="http://patternsinfp.files.wordpress.com/2011/07/accumulations-tree.png?w=200"></p>
<p>  the ancestors of the node labelled <img src='http://s0.wp.com/latex.php?latex=%7B3%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{3}' title='{3}' class='latex' /> are the nodes labelled <img src='http://s0.wp.com/latex.php?latex=%7B2%2C4%2C3%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{2,4,3}' title='{2,4,3}' class='latex' />. One could represent those ancestors simply as a list, <img src='http://s0.wp.com/latex.php?latex=%7B%5B2%2C4%2C3%5D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{[2,4,3]}' title='{[2,4,3]}' class='latex' />; but that rules out the possibility of a downwards accumulation treating left children differently from right children, which is essential in a number of algorithms (such as the parallel prefix and tree drawing algorithms in my thesis). A more faithful rendering is to define a new datatype of <em>paths</em> that captures the left and right turns&mdash;a kind of non-empty cons list, but with both a &#8220;left cons&#8221; and a &#8220;right cons&#8221; constructor: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathbf%7Bdata%7D%5C%3B%5Cmathsf%7BPath%7D%5C%2C%5Calpha+%3D+%5Cmathit%7BSingle%7D%5C%2C%5Calpha+%5Cmid+%5Cmathit%7BLCons%7D%5C%2C%5Calpha%5C%2C%28%5Cmathsf%7BPath%7D%5C%2C%5Calpha%29+%5Cmid+%5Cmathit%7BRCons%7D%5C%2C%5Calpha%5C%2C%28%5Cmathsf%7BPath%7D%5C%2C%5Calpha%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathbf{data}&#92;;&#92;mathsf{Path}&#92;,&#92;alpha = &#92;mathit{Single}&#92;,&#92;alpha &#92;mid &#92;mathit{LCons}&#92;,&#92;alpha&#92;,(&#92;mathsf{Path}&#92;,&#92;alpha) &#92;mid &#92;mathit{RCons}&#92;,&#92;alpha&#92;,(&#92;mathsf{Path}&#92;,&#92;alpha) ' title='&#92;displaystyle  &#92;mathbf{data}&#92;;&#92;mathsf{Path}&#92;,&#92;alpha = &#92;mathit{Single}&#92;,&#92;alpha &#92;mid &#92;mathit{LCons}&#92;,&#92;alpha&#92;,(&#92;mathsf{Path}&#92;,&#92;alpha) &#92;mid &#92;mathit{RCons}&#92;,&#92;alpha&#92;,(&#92;mathsf{Path}&#92;,&#92;alpha) ' class='latex' />
</p></blockquote>
<p> (I called them &#8220;threads&#8221; in my thesis.) Then we can capture the data structure representing the ancestors of the node labelled <img src='http://s0.wp.com/latex.php?latex=%7B3%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{3}' title='{3}' class='latex' />
<p align="center"><img width="100" src="http://patternsinfp.files.wordpress.com/2011/07/accumulations-path.png?w=100"></p>
<p>  by the expression <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7BRCons%7D%5C%2C2%5C%2C%28%5Cmathit%7BLCons%7D%5C%2C4%5C%2C%28%5Cmathit%7BSingle%7D%5C%2C3%29%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{RCons}&#92;,2&#92;,(&#92;mathit{LCons}&#92;,4&#92;,(&#92;mathit{Single}&#92;,3))}' title='{&#92;mathit{RCons}&#92;,2&#92;,(&#92;mathit{LCons}&#92;,4&#92;,(&#92;mathit{Single}&#92;,3))}' class='latex' />. I leave it as an exercise for the more energetic reader to work out a definition for </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathit%7Bpaths%7D+%3A%3A+%5Cmathsf%7BTree%7D%5C%2C%5Calpha+%5Crightarrow+%5Cmathsf%7BTree%7D%5C%2C%28%5Cmathsf%7BPath%7D%5C%2C%5Calpha%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathit{paths} :: &#92;mathsf{Tree}&#92;,&#92;alpha &#92;rightarrow &#92;mathsf{Tree}&#92;,(&#92;mathsf{Path}&#92;,&#92;alpha) ' title='&#92;displaystyle  &#92;mathit{paths} :: &#92;mathsf{Tree}&#92;,&#92;alpha &#92;rightarrow &#92;mathsf{Tree}&#92;,(&#92;mathsf{Path}&#92;,&#92;alpha) ' class='latex' />
</p></blockquote>
<p> to compute the tree giving the ancestors of every node, and for a corresponding <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bscand%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{scand}}' title='{&#92;mathit{scand}}' class='latex' />.</p>
<p><h2> Generic upwards accumulations </h2>
<p>
Having seen ad-hoc constructions for a particular kind of binary tree, we should consider what the datatype-generic construction looks like. I discussed datatype-generic upwards accumulations already, in the post on <a href="http://patternsinfp.wordpress.com/2011/05/05/horners-rule/">Horner&#8217;s Rule</a>; the construction was given in the paper <a href="http://dx.doi.org/10.1017/S0956796800001556">Generic functional programming with types and relations</a> by Richard Bird, Oege de Moor and Paul Hoogendijk. As with homogeneous binary trees, it&#8217;s still the case that the generic version of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bsubtrees%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{subtrees}}' title='{&#92;mathit{subtrees}}' class='latex' /> labels every node of a data structure of type <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BT%7D%5Calpha+%3D+%5Cmu%5Cmathsf%7BF%7D%5Calpha%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{T}&#92;alpha = &#92;mu&#92;mathsf{F}&#92;alpha}' title='{&#92;mathsf{T}&#92;alpha = &#92;mu&#92;mathsf{F}&#92;alpha}' class='latex' /> with the descendants of that node, and still the case that the descendants form a data structure also of type <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BT%7D%5Calpha%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{T}&#92;alpha}' title='{&#92;mathsf{T}&#92;alpha}' class='latex' />. However, in general, the datatype <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BT%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{T}}' title='{&#92;mathsf{T}}' class='latex' /> does not allow for a label at every node, so we need the <em>labelled variant</em> <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BL%7D%5Calpha+%3D+%5Cmu%5Cmathsf%7BG%7D%5Calpha%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{L}&#92;alpha = &#92;mu&#92;mathsf{G}&#92;alpha}' title='{&#92;mathsf{L}&#92;alpha = &#92;mu&#92;mathsf{G}&#92;alpha}' class='latex' /> where <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BG%7D%28%5Calpha%2C%5Cbeta%29+%3D+%5Calpha+%5Ctimes+%5Cmathsf%7BF%7D%281%2C%5Cbeta%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{G}(&#92;alpha,&#92;beta) = &#92;alpha &#92;times &#92;mathsf{F}(1,&#92;beta)}' title='{&#92;mathsf{G}(&#92;alpha,&#92;beta) = &#92;alpha &#92;times &#92;mathsf{F}(1,&#92;beta)}' class='latex' />. Then we can define </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathit%7Bsubtrees%7D_%7B%5Cmathsf%7BF%7D%7D+%3D+%5Cmathit%7Bfold%7D_%7B%5Cmathsf%7BF%7D%7D%28%5Cmathit%7Bin%7D_%7B%5Cmathsf%7BG%7D%7D+%5Ccdot+%5Cmathit%7Bfork%7D%28%5Cmathit%7Bin%7D_%7B%5Cmathsf%7BF%7D%7D+%5Ccdot+%5Cmathsf%7BF%7D%28%5Cmathit%7Bid%7D%2C%5Cmathit%7Broot%7D%29%2C+%5Cmathsf%7BF%7D%28%21%2C%5Cmathit%7Bid%7D%29%29%29+%3A%3A+%5Cmathsf%7BT%7D%5Calpha+%5Crightarrow+%5Cmathsf%7BL%7D%28%5Cmathsf%7BT%7D%5Calpha%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathit{subtrees}_{&#92;mathsf{F}} = &#92;mathit{fold}_{&#92;mathsf{F}}(&#92;mathit{in}_{&#92;mathsf{G}} &#92;cdot &#92;mathit{fork}(&#92;mathit{in}_{&#92;mathsf{F}} &#92;cdot &#92;mathsf{F}(&#92;mathit{id},&#92;mathit{root}), &#92;mathsf{F}(!,&#92;mathit{id}))) :: &#92;mathsf{T}&#92;alpha &#92;rightarrow &#92;mathsf{L}(&#92;mathsf{T}&#92;alpha) ' title='&#92;displaystyle  &#92;mathit{subtrees}_{&#92;mathsf{F}} = &#92;mathit{fold}_{&#92;mathsf{F}}(&#92;mathit{in}_{&#92;mathsf{G}} &#92;cdot &#92;mathit{fork}(&#92;mathit{in}_{&#92;mathsf{F}} &#92;cdot &#92;mathsf{F}(&#92;mathit{id},&#92;mathit{root}), &#92;mathsf{F}(!,&#92;mathit{id}))) :: &#92;mathsf{T}&#92;alpha &#92;rightarrow &#92;mathsf{L}(&#92;mathsf{T}&#92;alpha) ' class='latex' />
</p></blockquote>
<p> where <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Broot%7D+%3D+%5Cmathit%7Bfst%7D+%5Ccdot+%5Cmathit%7Bin%7D_%7B%5Cmathsf%7BG%7D%7D%5E%7B-1%7D+%3D+%5Cmathit%7Bfold%7D_%7B%5Cmathsf%7BG%7D%7D%5C%2C%5Cmathit%7Bfst%7D+%3A%3A+%5Cmathsf%7BL%7D%5Calpha+%5Crightarrow+%5Calpha%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{root} = &#92;mathit{fst} &#92;cdot &#92;mathit{in}_{&#92;mathsf{G}}^{-1} = &#92;mathit{fold}_{&#92;mathsf{G}}&#92;,&#92;mathit{fst} :: &#92;mathsf{L}&#92;alpha &#92;rightarrow &#92;alpha}' title='{&#92;mathit{root} = &#92;mathit{fst} &#92;cdot &#92;mathit{in}_{&#92;mathsf{G}}^{-1} = &#92;mathit{fold}_{&#92;mathsf{G}}&#92;,&#92;mathit{fst} :: &#92;mathsf{L}&#92;alpha &#92;rightarrow &#92;alpha}' class='latex' /> returns the root label of a labelled data structure&mdash;by construction, every labelled data structure has a root label&mdash;and <img src='http://s0.wp.com/latex.php?latex=%7B%21_%7B%5Calpha%7D+%3A%3A+%5Calpha+%5Crightarrow+1%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{!_{&#92;alpha} :: &#92;alpha &#92;rightarrow 1}' title='{!_{&#92;alpha} :: &#92;alpha &#92;rightarrow 1}' class='latex' /> is the unique arrow to the unit type. Moreover, we get a datatype-generic <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bscanu%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{scanu}}' title='{&#92;mathit{scanu}}' class='latex' /> operator, and a Scan Lemma: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Cmathit%7Bscanu%7D_%7B%5Cmathsf%7BF%7D%7D+%26%3A%3A%26+%28%5Cmathsf%7BF%7D%28%5Calpha%2C%5Cbeta%29+%5Crightarrow+%5Cbeta%29+%5Crightarrow+%5Cmathsf%7BT%7D%5Calpha+%5Crightarrow+%5Cmathsf%7BL%7D%5Cbeta+%5C%5C+%5Cmathit%7Bscanu%7D_%7B%5Cmathsf%7BF%7D%7D%5C%2C%5Cphi+%26%3D%26+%5Cmathsf%7BL%7D%5C%2C%28%5Cmathit%7Bfold%7D_%7B%5Cmathsf%7BF%7D%7D%5C%2C%5Cphi%29+%5Ccdot+%5Cmathit%7Bsubtrees%7D_%7B%5Cmathsf%7BF%7D%7D+%5C%5C+%26%3D%26+%5Cmathit%7Bfold%7D_%7B%5Cmathsf%7BF%7D%7D%28%5Cmathit%7Bin%7D_%7B%5Cmathsf%7BG%7D%7D+%5Ccdot+%5Cmathit%7Bfork%7D%28%5Cphi+%5Ccdot+%5Cmathsf%7BF%7D%28%5Cmathit%7Bid%7D%2C%5Cmathit%7Broot%7D%29%2C+%5Cmathsf%7BF%7D%28%21%2C%5Cmathit%7Bid%7D%29%29%29+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{scanu}_{&#92;mathsf{F}} &amp;::&amp; (&#92;mathsf{F}(&#92;alpha,&#92;beta) &#92;rightarrow &#92;beta) &#92;rightarrow &#92;mathsf{T}&#92;alpha &#92;rightarrow &#92;mathsf{L}&#92;beta &#92;&#92; &#92;mathit{scanu}_{&#92;mathsf{F}}&#92;,&#92;phi &amp;=&amp; &#92;mathsf{L}&#92;,(&#92;mathit{fold}_{&#92;mathsf{F}}&#92;,&#92;phi) &#92;cdot &#92;mathit{subtrees}_{&#92;mathsf{F}} &#92;&#92; &amp;=&amp; &#92;mathit{fold}_{&#92;mathsf{F}}(&#92;mathit{in}_{&#92;mathsf{G}} &#92;cdot &#92;mathit{fork}(&#92;phi &#92;cdot &#92;mathsf{F}(&#92;mathit{id},&#92;mathit{root}), &#92;mathsf{F}(!,&#92;mathit{id}))) &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{scanu}_{&#92;mathsf{F}} &amp;::&amp; (&#92;mathsf{F}(&#92;alpha,&#92;beta) &#92;rightarrow &#92;beta) &#92;rightarrow &#92;mathsf{T}&#92;alpha &#92;rightarrow &#92;mathsf{L}&#92;beta &#92;&#92; &#92;mathit{scanu}_{&#92;mathsf{F}}&#92;,&#92;phi &amp;=&amp; &#92;mathsf{L}&#92;,(&#92;mathit{fold}_{&#92;mathsf{F}}&#92;,&#92;phi) &#92;cdot &#92;mathit{subtrees}_{&#92;mathsf{F}} &#92;&#92; &amp;=&amp; &#92;mathit{fold}_{&#92;mathsf{F}}(&#92;mathit{in}_{&#92;mathsf{G}} &#92;cdot &#92;mathit{fork}(&#92;phi &#92;cdot &#92;mathsf{F}(&#92;mathit{id},&#92;mathit{root}), &#92;mathsf{F}(!,&#92;mathit{id}))) &#92;end{array} ' class='latex' />
</p></blockquote>
<p><h2> Generic downwards accumulations, via linearization </h2>
<p>
The best part of a decade after my thesis work, inspired by the paper by Richard Bird &amp; co, I set out to try to define datatype-generic versions of downward accumulations too. I wrote a paper about it for <a href="http://web.comlab.ox.ac.uk/publications/publication2349-abstract.html">MPC 1998</a>, and then came up with a new construction for the journal version of that paper in <a href="http://web.comlab.ox.ac.uk/publications/publication2366-abstract.html">SCP in 2000</a>. I now think these constructions are rather clunky, and I have a better one; if you don&#8217;t care to explore the culs-de-sac, skip this section and the next and go straight to the <a href="#derivatives">section on derivatives</a>.</p>
<p>
The MPC construction was based around a datatype-generic version of the <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BPath%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{Path}}' title='{&#92;mathsf{Path}}' class='latex' /> datatype above, to represent the &#8220;ancestors&#8221; of a node in an inductive datatype. The tricky bit is that data structures in general are non-linear&mdash;a node may have many children&mdash;whereas paths are linear&mdash;every node has exactly one child, except the last which has none; how can we define a &#8220;linear version&#8221; <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%27%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}&#039;}' title='{&#92;mathsf{F}&#039;}' class='latex' /> of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />? Technically, we might say that a functor is linear (actually, &#8220;affine&#8221; would be a better word) if it distributes over sum.</p>
<p>
The construction in the paper assumed that <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' /> was a sum of products of literals </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Cmathsf%7BF%7D%28%5Calpha%2C%5Cbeta%29+%26%3D%26+%5Csum_%7Bi%3D1%7D%5E%7Bn%7D+%5Cmathsf%7BF%7D_i%28%5Calpha%2C%5Cbeta%29+%5C%5C+%5Cmathsf%7BF%7D_i%28%5Calpha%2C%5Cbeta%29+%26%3D%26+%5Cprod_%7Bj%3D1%7D%5E%7Bm_i%7D+%5Cmathsf%7BF%7D_%7Bi%2Cj%7D%28%5Calpha%2C%5Cbeta%29+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathsf{F}(&#92;alpha,&#92;beta) &amp;=&amp; &#92;sum_{i=1}^{n} &#92;mathsf{F}_i(&#92;alpha,&#92;beta) &#92;&#92; &#92;mathsf{F}_i(&#92;alpha,&#92;beta) &amp;=&amp; &#92;prod_{j=1}^{m_i} &#92;mathsf{F}_{i,j}(&#92;alpha,&#92;beta) &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathsf{F}(&#92;alpha,&#92;beta) &amp;=&amp; &#92;sum_{i=1}^{n} &#92;mathsf{F}_i(&#92;alpha,&#92;beta) &#92;&#92; &#92;mathsf{F}_i(&#92;alpha,&#92;beta) &amp;=&amp; &#92;prod_{j=1}^{m_i} &#92;mathsf{F}_{i,j}(&#92;alpha,&#92;beta) &#92;end{array} ' class='latex' />
</p></blockquote>
<p> where each <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D_%7Bi%2Cj%7D%28%5Calpha%2C%5Cbeta%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}_{i,j}(&#92;alpha,&#92;beta)}' title='{&#92;mathsf{F}_{i,j}(&#92;alpha,&#92;beta)}' class='latex' /> is either <img src='http://s0.wp.com/latex.php?latex=%7B%5Calpha%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;alpha}' title='{&#92;alpha}' class='latex' />, <img src='http://s0.wp.com/latex.php?latex=%7B%5Cbeta%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;beta}' title='{&#92;beta}' class='latex' />, or some constant type such as <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7BInt%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{Int}}' title='{&#92;mathit{Int}}' class='latex' /> or <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7BBool%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{Bool}}' title='{&#92;mathit{Bool}}' class='latex' />. For example, for leaf-labelled binary trees </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathbf%7Bdata%7D%5C%3B%5Cmathsf%7BTree%7D%5C%2C%5Calpha+%3D+%5Cmathit%7BTip%7D%5C%2C%5Calpha+%5Cmid+%5Cmathit%7BBin%7D%5C%2C%28%5Cmathsf%7BTree%7D%5C%2C%5Calpha%29%5C%2C%28%5Cmathsf%7BTree%7D%5C%2C%5Calpha%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathbf{data}&#92;;&#92;mathsf{Tree}&#92;,&#92;alpha = &#92;mathit{Tip}&#92;,&#92;alpha &#92;mid &#92;mathit{Bin}&#92;,(&#92;mathsf{Tree}&#92;,&#92;alpha)&#92;,(&#92;mathsf{Tree}&#92;,&#92;alpha) ' title='&#92;displaystyle  &#92;mathbf{data}&#92;;&#92;mathsf{Tree}&#92;,&#92;alpha = &#92;mathit{Tip}&#92;,&#92;alpha &#92;mid &#92;mathit{Bin}&#92;,(&#92;mathsf{Tree}&#92;,&#92;alpha)&#92;,(&#92;mathsf{Tree}&#92;,&#92;alpha) ' class='latex' />
</p></blockquote>
<p> the shape functor is <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%28%5Calpha%2C%5Cbeta%29+%3D+%5Calpha+%2B+%5Cbeta+%5Ctimes+%5Cbeta%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}(&#92;alpha,&#92;beta) = &#92;alpha + &#92;beta &#92;times &#92;beta}' title='{&#92;mathsf{F}(&#92;alpha,&#92;beta) = &#92;alpha + &#92;beta &#92;times &#92;beta}' class='latex' />, so <img src='http://s0.wp.com/latex.php?latex=%7Bn%3D2%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{n=2}' title='{n=2}' class='latex' /> (there are two variants), <img src='http://s0.wp.com/latex.php?latex=%7Bm_1%3D1%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{m_1=1}' title='{m_1=1}' class='latex' /> (the first variant has a single literal, <img src='http://s0.wp.com/latex.php?latex=%7B%5Calpha%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;alpha}' title='{&#92;alpha}' class='latex' />) and <img src='http://s0.wp.com/latex.php?latex=%7Bm_2%3D2%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{m_2=2}' title='{m_2=2}' class='latex' /> (the second variant has two literals, <img src='http://s0.wp.com/latex.php?latex=%7B%5Cbeta%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;beta}' title='{&#92;beta}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cbeta%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;beta}' title='{&#92;beta}' class='latex' />), and: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Cmathsf%7BF%7D%28%5Calpha%2C%5Cbeta%29+%26%3D%26+%5Cmathsf%7BF%7D_1%28%5Calpha%2C%5Cbeta%29+%2B+%5Cmathsf%7BF%7D_2%28%5Calpha%2C%5Cbeta%29+%5C%5C+%5Cmathsf%7BF%7D_1%28%5Calpha%2C%5Cbeta%29+%26%3D%26+%5Cmathsf%7BF%7D_%7B1%2C1%7D%28%5Calpha%2C%5Cbeta%29+%5C%5C+%5Cmathsf%7BF%7D_%7B1%2C1%7D%28%5Calpha%2C%5Cbeta%29+%26%3D%26+%5Calpha+%5C%5C+%5Cmathsf%7BF%7D_2%28%5Calpha%2C%5Cbeta%29+%26%3D%26+%5Cmathsf%7BF%7D_%7B2%2C1%7D%28%5Calpha%2C%5Cbeta%29+%5Ctimes+%5Cmathsf%7BF%7D_%7B2%2C2%7D%28%5Calpha%2C%5Cbeta%29+%5C%5C+%5Cmathsf%7BF%7D_%7B2%2C1%7D%28%5Calpha%2C%5Cbeta%29+%26%3D%26+%5Cbeta+%5C%5C+%5Cmathsf%7BF%7D_%7B2%2C1%7D%28%5Calpha%2C%5Cbeta%29+%26%3D%26+%5Cbeta+%5C%5C+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathsf{F}(&#92;alpha,&#92;beta) &amp;=&amp; &#92;mathsf{F}_1(&#92;alpha,&#92;beta) + &#92;mathsf{F}_2(&#92;alpha,&#92;beta) &#92;&#92; &#92;mathsf{F}_1(&#92;alpha,&#92;beta) &amp;=&amp; &#92;mathsf{F}_{1,1}(&#92;alpha,&#92;beta) &#92;&#92; &#92;mathsf{F}_{1,1}(&#92;alpha,&#92;beta) &amp;=&amp; &#92;alpha &#92;&#92; &#92;mathsf{F}_2(&#92;alpha,&#92;beta) &amp;=&amp; &#92;mathsf{F}_{2,1}(&#92;alpha,&#92;beta) &#92;times &#92;mathsf{F}_{2,2}(&#92;alpha,&#92;beta) &#92;&#92; &#92;mathsf{F}_{2,1}(&#92;alpha,&#92;beta) &amp;=&amp; &#92;beta &#92;&#92; &#92;mathsf{F}_{2,1}(&#92;alpha,&#92;beta) &amp;=&amp; &#92;beta &#92;&#92; &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathsf{F}(&#92;alpha,&#92;beta) &amp;=&amp; &#92;mathsf{F}_1(&#92;alpha,&#92;beta) + &#92;mathsf{F}_2(&#92;alpha,&#92;beta) &#92;&#92; &#92;mathsf{F}_1(&#92;alpha,&#92;beta) &amp;=&amp; &#92;mathsf{F}_{1,1}(&#92;alpha,&#92;beta) &#92;&#92; &#92;mathsf{F}_{1,1}(&#92;alpha,&#92;beta) &amp;=&amp; &#92;alpha &#92;&#92; &#92;mathsf{F}_2(&#92;alpha,&#92;beta) &amp;=&amp; &#92;mathsf{F}_{2,1}(&#92;alpha,&#92;beta) &#92;times &#92;mathsf{F}_{2,2}(&#92;alpha,&#92;beta) &#92;&#92; &#92;mathsf{F}_{2,1}(&#92;alpha,&#92;beta) &amp;=&amp; &#92;beta &#92;&#92; &#92;mathsf{F}_{2,1}(&#92;alpha,&#92;beta) &amp;=&amp; &#92;beta &#92;&#92; &#92;end{array} ' class='latex' />
</p></blockquote>
<p> Then for each <img src='http://s0.wp.com/latex.php?latex=%7Bi%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{i}' title='{i}' class='latex' /> we define a <img src='http://s0.wp.com/latex.php?latex=%7B%28k_i%2B1%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(k_i+1)}' title='{(k_i+1)}' class='latex' />-ary functor <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%27_i%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}&#039;_i}' title='{&#92;mathsf{F}&#039;_i}' class='latex' />, where <img src='http://s0.wp.com/latex.php?latex=%7Bk_i%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{k_i}' title='{k_i}' class='latex' /> is the &#8220;degree of branching&#8221; of variant <img src='http://s0.wp.com/latex.php?latex=%7Bi%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{i}' title='{i}' class='latex' /> (ie, the number of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cbeta%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;beta}' title='{&#92;beta}' class='latex' />s occurring in <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D_i%28%5Calpha%2C%5Cbeta%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}_i(&#92;alpha,&#92;beta)}' title='{&#92;mathsf{F}_i(&#92;alpha,&#92;beta)}' class='latex' />, which is the number of <img src='http://s0.wp.com/latex.php?latex=%7Bj%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{j}' title='{j}' class='latex' /> for which <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D_%7Bi%2Cj%7D%28%5Calpha%2C%5Cbeta%29%3D%5Cbeta%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}_{i,j}(&#92;alpha,&#92;beta)=&#92;beta}' title='{&#92;mathsf{F}_{i,j}(&#92;alpha,&#92;beta)=&#92;beta}' class='latex' />), in such a way that </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathsf%7BF%7D%27_i%28%5Calpha%2C%5Cbeta%2C%5Cldots%2C%5Cbeta%29+%3D+%5Cmathsf%7BF%7D_i%28%5Calpha%2C%5Cbeta%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathsf{F}&#039;_i(&#92;alpha,&#92;beta,&#92;ldots,&#92;beta) = &#92;mathsf{F}_i(&#92;alpha,&#92;beta) ' title='&#92;displaystyle  &#92;mathsf{F}&#039;_i(&#92;alpha,&#92;beta,&#92;ldots,&#92;beta) = &#92;mathsf{F}_i(&#92;alpha,&#92;beta) ' class='latex' />
</p></blockquote>
<p> and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%27_i%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}&#039;_i}' title='{&#92;mathsf{F}&#039;_i}' class='latex' /> is linear in each argument except perhaps the first. It&#8217;s a bit messy explicitly to give a construction for <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%27_i%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}&#039;_i}' title='{&#92;mathsf{F}&#039;_i}' class='latex' />, but roughly speaking, </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathsf%7BF%7D%27_i%28%5Calpha%2C%5Cbeta_1%2C%5Cldots%2C%5Cbeta_%7Bk_i%7D%29+%3D+%5Cprod_%7Bj%3D1%7D%5E%7Bm_i%7D+%5Cmathsf%7BF%7D%27_%7Bi%2Cj%7D%28%5Calpha%2C%5Cbeta_1%2C%5Cldots%2C%5Cbeta_%7Bk_i%7D%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathsf{F}&#039;_i(&#92;alpha,&#92;beta_1,&#92;ldots,&#92;beta_{k_i}) = &#92;prod_{j=1}^{m_i} &#92;mathsf{F}&#039;_{i,j}(&#92;alpha,&#92;beta_1,&#92;ldots,&#92;beta_{k_i}) ' title='&#92;displaystyle  &#92;mathsf{F}&#039;_i(&#92;alpha,&#92;beta_1,&#92;ldots,&#92;beta_{k_i}) = &#92;prod_{j=1}^{m_i} &#92;mathsf{F}&#039;_{i,j}(&#92;alpha,&#92;beta_1,&#92;ldots,&#92;beta_{k_i}) ' class='latex' />
</p></blockquote>
<p> where <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%27_%7Bi%2Cj%7D%28%5Calpha%2C%5Cbeta_1%2C%5Cldots%2C%5Cbeta_%7Bk_i%7D%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}&#039;_{i,j}(&#92;alpha,&#92;beta_1,&#92;ldots,&#92;beta_{k_i})}' title='{&#92;mathsf{F}&#039;_{i,j}(&#92;alpha,&#92;beta_1,&#92;ldots,&#92;beta_{k_i})}' class='latex' /> is &#8220;the next unused <img src='http://s0.wp.com/latex.php?latex=%7B%5Cbeta_i%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;beta_i}' title='{&#92;beta_i}' class='latex' />&#8221; when <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D_%7Bi%2Cj%7D%28%5Calpha%2C%5Cbeta%29%3D%5Cbeta%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}_{i,j}(&#92;alpha,&#92;beta)=&#92;beta}' title='{&#92;mathsf{F}_{i,j}(&#92;alpha,&#92;beta)=&#92;beta}' class='latex' />, and just <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D_%7Bi%2Cj%7D%28%5Calpha%2C%5Cbeta%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}_{i,j}(&#92;alpha,&#92;beta)}' title='{&#92;mathsf{F}_{i,j}(&#92;alpha,&#92;beta)}' class='latex' /> otherwise. For example, for leaf-labelled binary trees, we have: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Cmathsf%7BF%7D%27_1%28%5Calpha%29+%26%3D%26+%5Calpha+%5C%5C+%5Cmathsf%7BF%7D%27_2%28%5Calpha%2C%5Cbeta_1%2C%5Cbeta_2%29+%26%3D%26+%5Cbeta_1+%5Ctimes+%5Cbeta_2+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathsf{F}&#039;_1(&#92;alpha) &amp;=&amp; &#92;alpha &#92;&#92; &#92;mathsf{F}&#039;_2(&#92;alpha,&#92;beta_1,&#92;beta_2) &amp;=&amp; &#92;beta_1 &#92;times &#92;beta_2 &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathsf{F}&#039;_1(&#92;alpha) &amp;=&amp; &#92;alpha &#92;&#92; &#92;mathsf{F}&#039;_2(&#92;alpha,&#92;beta_1,&#92;beta_2) &amp;=&amp; &#92;beta_1 &#92;times &#92;beta_2 &#92;end{array} ' class='latex' />
</p></blockquote>
<p> Having defined the linear variant <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%27%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}&#039;}' title='{&#92;mathsf{F}&#039;}' class='latex' /> of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />, we can construct the datatype <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BP%7D%5Calpha+%3D+%5Cmu%5Cmathsf%7BH%7D%5Calpha%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{P}&#92;alpha = &#92;mu&#92;mathsf{H}&#92;alpha}' title='{&#92;mathsf{P}&#92;alpha = &#92;mu&#92;mathsf{H}&#92;alpha}' class='latex' /> of paths, as the inductive datatype of shape <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BH%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{H}}' title='{&#92;mathsf{H}}' class='latex' /> where </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathsf%7BH%7D%28%5Calpha%2C%5Cbeta%29+%3D+%5Cmathsf%7BF%7D%28%5Calpha%2C1%29+%2B+%5Csum_%7Bi%3D1%7D%5E%7Bn%7D+%5Csum_%7Bj%3D1%7D%5E%7Bk_i%7D+%28%5Cmathsf%7BF%7D_i%28%5Calpha%2C1%29+%5Ctimes+%5Cbeta%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathsf{H}(&#92;alpha,&#92;beta) = &#92;mathsf{F}(&#92;alpha,1) + &#92;sum_{i=1}^{n} &#92;sum_{j=1}^{k_i} (&#92;mathsf{F}_i(&#92;alpha,1) &#92;times &#92;beta) ' title='&#92;displaystyle  &#92;mathsf{H}(&#92;alpha,&#92;beta) = &#92;mathsf{F}(&#92;alpha,1) + &#92;sum_{i=1}^{n} &#92;sum_{j=1}^{k_i} (&#92;mathsf{F}_i(&#92;alpha,1) &#92;times &#92;beta) ' class='latex' />
</p></blockquote>
<p> That is, paths are a kind of non-empty cons list. The path ends at some node of the original data structure; so the last element of the path is of type <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%28%5Calpha%2C1%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}(&#92;alpha,1)}' title='{&#92;mathsf{F}(&#92;alpha,1)}' class='latex' />, which records the &#8220;local content&#8221; of a node (its shape and labels, but without any of its children). Every other element of the path consists of the local content of a node together with an indication of which direction to go next; this amounts to the choice of a variant <img src='http://s0.wp.com/latex.php?latex=%7Bi%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{i}' title='{i}' class='latex' />, followed by the choice of one of <img src='http://s0.wp.com/latex.php?latex=%7Bk_i%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{k_i}' title='{k_i}' class='latex' /> identical copies of the local contents <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D_i%28%5Calpha%2C1%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}_i(&#92;alpha,1)}' title='{&#92;mathsf{F}_i(&#92;alpha,1)}' class='latex' /> of variant <img src='http://s0.wp.com/latex.php?latex=%7Bi%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{i}' title='{i}' class='latex' />, where <img src='http://s0.wp.com/latex.php?latex=%7Bk_i%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{k_i}' title='{k_i}' class='latex' /> is the degree of branching of variant <img src='http://s0.wp.com/latex.php?latex=%7Bi%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{i}' title='{i}' class='latex' />. We model this as a base constructor <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7BEnd%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{End}}' title='{&#92;mathit{End}}' class='latex' /> and a family of &#8220;cons&#8221; constructors <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7BCons%7D_%7Bi%2Cj%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{Cons}_{i,j}}' title='{&#92;mathit{Cons}_{i,j}}' class='latex' /> for <img src='http://s0.wp.com/latex.php?latex=%7B1+%5Cle+i+%5Cle+n%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{1 &#92;le i &#92;le n}' title='{1 &#92;le i &#92;le n}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B1+%5Cle+j+%5Cle+k_i%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{1 &#92;le j &#92;le k_i}' title='{1 &#92;le j &#92;le k_i}' class='latex' />.</p>
<p>
For example, for leaf-labelled binary trees, the &#8220;local content&#8221; for the last element of the path is either a single label (for tips) or void (for bins), and for the other path elements, there are zero copies of the local content for a tip (because a tip has zero children), and two copies of the void local information for bins (because a bin has two children). Therefore, the path datatype for such trees is </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathbf%7Bdata%7D%5C%3B%5Cmathsf%7BPath%7D%5C%2C%5Calpha+%3D+%5Cmathit%7BEnd%7D%5C%2C%28%5Cmathsf%7BMaybe%7D%5C%2C%5Calpha%29+%5Cmid+%5Cmathit%7BCons%7D_%7B2%2C1%7D%5C%2C%28%5Cmathsf%7BPath%7D%5C%2C%5Calpha%29+%5Cmid+%5Cmathit%7BCons%7D_%7B2%2C2%7D%5C%2C%28%5Cmathsf%7BPath%7D%5C%2C%5Calpha%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathbf{data}&#92;;&#92;mathsf{Path}&#92;,&#92;alpha = &#92;mathit{End}&#92;,(&#92;mathsf{Maybe}&#92;,&#92;alpha) &#92;mid &#92;mathit{Cons}_{2,1}&#92;,(&#92;mathsf{Path}&#92;,&#92;alpha) &#92;mid &#92;mathit{Cons}_{2,2}&#92;,(&#92;mathsf{Path}&#92;,&#92;alpha) ' title='&#92;displaystyle  &#92;mathbf{data}&#92;;&#92;mathsf{Path}&#92;,&#92;alpha = &#92;mathit{End}&#92;,(&#92;mathsf{Maybe}&#92;,&#92;alpha) &#92;mid &#92;mathit{Cons}_{2,1}&#92;,(&#92;mathsf{Path}&#92;,&#92;alpha) &#92;mid &#92;mathit{Cons}_{2,2}&#92;,(&#92;mathsf{Path}&#92;,&#92;alpha) ' class='latex' />
</p></blockquote>
<p> which is isomorphic to the definition that you might have written yourself: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathbf%7Bdata%7D%5C%3B%5Cmathsf%7BPath%7D%5C%2C%5Calpha+%3D+%5Cmathit%7BExternal%7D%5C%2C%5Calpha+%5Cmid+%5Cmathit%7BInternal%7D+%5Cmid+%5Cmathit%7BLeft%7D%5C%2C%28%5Cmathsf%7BPath%7D%5C%2C%5Calpha%29+%5Cmid+%5Cmathit%7BRight%7D%5C%2C%28%5Cmathsf%7BPath%7D%5C%2C%5Calpha%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathbf{data}&#92;;&#92;mathsf{Path}&#92;,&#92;alpha = &#92;mathit{External}&#92;,&#92;alpha &#92;mid &#92;mathit{Internal} &#92;mid &#92;mathit{Left}&#92;,(&#92;mathsf{Path}&#92;,&#92;alpha) &#92;mid &#92;mathit{Right}&#92;,(&#92;mathsf{Path}&#92;,&#92;alpha) ' title='&#92;displaystyle  &#92;mathbf{data}&#92;;&#92;mathsf{Path}&#92;,&#92;alpha = &#92;mathit{External}&#92;,&#92;alpha &#92;mid &#92;mathit{Internal} &#92;mid &#92;mathit{Left}&#92;,(&#92;mathsf{Path}&#92;,&#92;alpha) &#92;mid &#92;mathit{Right}&#92;,(&#92;mathsf{Path}&#92;,&#92;alpha) ' class='latex' />
</p></blockquote>
<p> For homogeneous binary trees, the construction gives </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathbf%7Bdata%7D%5C%3B%5Cmathsf%7BPath%7D%5C%2C%5Calpha+%3D+%5Cmathit%7BExternal%7D%5C%2C%5Calpha+%5Cmid+%5Cmathit%7BInternal%7D%5C%2C%5Calpha+%5Cmid+%5Cmathit%7BLeft%7D%5C%2C%5Calpha%5C%2C%28%5Cmathsf%7BPath%7D%5C%2C%5Calpha%29+%5Cmid+%5Cmathit%7BRight%7D%5C%2C%5Calpha%5C%2C%28%5Cmathsf%7BPath%7D%5C%2C%5Calpha%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathbf{data}&#92;;&#92;mathsf{Path}&#92;,&#92;alpha = &#92;mathit{External}&#92;,&#92;alpha &#92;mid &#92;mathit{Internal}&#92;,&#92;alpha &#92;mid &#92;mathit{Left}&#92;,&#92;alpha&#92;,(&#92;mathsf{Path}&#92;,&#92;alpha) &#92;mid &#92;mathit{Right}&#92;,&#92;alpha&#92;,(&#92;mathsf{Path}&#92;,&#92;alpha) ' title='&#92;displaystyle  &#92;mathbf{data}&#92;;&#92;mathsf{Path}&#92;,&#92;alpha = &#92;mathit{External}&#92;,&#92;alpha &#92;mid &#92;mathit{Internal}&#92;,&#92;alpha &#92;mid &#92;mathit{Left}&#92;,&#92;alpha&#92;,(&#92;mathsf{Path}&#92;,&#92;alpha) &#92;mid &#92;mathit{Right}&#92;,&#92;alpha&#92;,(&#92;mathsf{Path}&#92;,&#92;alpha) ' class='latex' />
</p></blockquote>
<p> which is almost the ad-hoc definition we had two sections ago, except that it distinguishes singleton paths that terminate at an external node from those that terminate at an internal one.</p>
<p>
Now, analogous to the function <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bsubtrees%7D_%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{subtrees}_&#92;mathsf{F}}' title='{&#92;mathit{subtrees}_&#92;mathsf{F}}' class='latex' /> which labels every node with its descendants, we can define a function <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bpaths%7D_%5Cmathsf%7BF%7D+%3A+%5Cmathsf%7BT%7D%5Calpha+%5Crightarrow+%5Cmathsf%7BL%7D%28%5Cmathsf%7BP%7D%5Calpha%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{paths}_&#92;mathsf{F} : &#92;mathsf{T}&#92;alpha &#92;rightarrow &#92;mathsf{L}(&#92;mathsf{P}&#92;alpha)}' title='{&#92;mathit{paths}_&#92;mathsf{F} : &#92;mathsf{T}&#92;alpha &#92;rightarrow &#92;mathsf{L}(&#92;mathsf{P}&#92;alpha)}' class='latex' /> to label every node with its ancestors, in the form of the path to that node. One definition is as a fold; informally, at each stage we construct a singleton path to the root, and map the appropriate &#8220;cons&#8221; over the paths to each node in each of the children (see the paper for a concrete definition). This is inefficient, because of the repeated maps; it&#8217;s analogous to defining <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Binits%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{inits}}' title='{&#92;mathit{inits}}' class='latex' /> by </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Cmathit%7Binits%7D%5C%2C%5B%5C%2C%5D+%26%3D%26+%5B%5B%5C%2C%5D%5D+%5C%5C+%5Cmathit%7Binits%7D%5C%2C%28a%3Ax%29+%26%3D%26+%5B%5C%2C%5D+%3A+%5Cmathit%7Bmap%7D%5C%2C%28a%3A%29%5C%2C%28%5Cmathit%7Binits%7D%5C%2Cx%29+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{inits}&#92;,[&#92;,] &amp;=&amp; [[&#92;,]] &#92;&#92; &#92;mathit{inits}&#92;,(a:x) &amp;=&amp; [&#92;,] : &#92;mathit{map}&#92;,(a:)&#92;,(&#92;mathit{inits}&#92;,x) &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{inits}&#92;,[&#92;,] &amp;=&amp; [[&#92;,]] &#92;&#92; &#92;mathit{inits}&#92;,(a:x) &amp;=&amp; [&#92;,] : &#92;mathit{map}&#92;,(a:)&#92;,(&#92;mathit{inits}&#92;,x) &#92;end{array} ' class='latex' />
</p></blockquote>
<p> A second definition is as an unfold, maintaining as an accumulating parameter of type <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BP%7D%28%5Calpha%29%5Crightarrow%5Cmathsf%7BP%7D%28%5Calpha%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{P}(&#92;alpha)&#92;rightarrow&#92;mathsf{P}(&#92;alpha)}' title='{&#92;mathsf{P}(&#92;alpha)&#92;rightarrow&#92;mathsf{P}(&#92;alpha)}' class='latex' /> the &#8220;path so far&#8221;; this avoids the maps, but it is still quadratic because there are no common subexpressions among the various paths. (This is analogous to an accumulating-parameter definition of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Binits%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{inits}}' title='{&#92;mathit{inits}}' class='latex' />: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Cmathit%7Binits%7D+%26%3D%26+%5Cmathit%7Binits%7D%27%5C%2C%5Cmathit%7Bid%7D+%5Cmedskip+%5C%5C+%5Cmathit%7Binits%7D%27%5C%2Cf%5C%2C%5B%5C%2C%5D+%26%3D%26+f%5C%2C%5B%5C%2C%5D+%5C%5C+%5Cmathit%7Binits%7D%27%5C%2Cf%5C%2C%28a%3Ax%29+%26%3D%26+f%5C%2C%5B%5C%2C%5D+%3A+%5Cmathit%7Binits%7D%27%5C%2C%28f+%5Ccdot+%28a%3A%29%29%5C%2Cx+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{inits} &amp;=&amp; &#92;mathit{inits}&#039;&#92;,&#92;mathit{id} &#92;medskip &#92;&#92; &#92;mathit{inits}&#039;&#92;,f&#92;,[&#92;,] &amp;=&amp; f&#92;,[&#92;,] &#92;&#92; &#92;mathit{inits}&#039;&#92;,f&#92;,(a:x) &amp;=&amp; f&#92;,[&#92;,] : &#92;mathit{inits}&#039;&#92;,(f &#92;cdot (a:))&#92;,x &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{inits} &amp;=&amp; &#92;mathit{inits}&#039;&#92;,&#92;mathit{id} &#92;medskip &#92;&#92; &#92;mathit{inits}&#039;&#92;,f&#92;,[&#92;,] &amp;=&amp; f&#92;,[&#92;,] &#92;&#92; &#92;mathit{inits}&#039;&#92;,f&#92;,(a:x) &amp;=&amp; f&#92;,[&#92;,] : &#92;mathit{inits}&#039;&#92;,(f &#92;cdot (a:))&#92;,x &#92;end{array} ' class='latex' />
</p></blockquote>
<p> Even with an accumulating &#8220;Hughes list&#8221; parameter, it still takes quadratic time.)</p>
<p>
The downwards accumulation itself is defined as a path fold mapped over the paths, giving a Scan Lemma for downwards accumulations. With either the fold or the unfold definition of paths, this is still quadratic, again because of the lack of common subexpressions in a result of quadratic size. However, in some circumstances the path fold can be reassociated (analogous to turning a <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfoldr%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{foldr}}' title='{&#92;mathit{foldr}}' class='latex' /> into a <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfoldl%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{foldl}}' title='{&#92;mathit{foldl}}' class='latex' />), leading finally to a linear-time computation; see the paper for the details of how.</p>
<p><h2> Generic downwards accumulations, via zip </h2>
<p>
I was dissatisfied with the &#8220;&#8230;&#8221;s in the MPC construction of datatype-generic paths, but couldn&#8217;t see a good way of avoiding them. So in the subsequent SCP version of the paper, I presented an alternative construction of downwards accumulations, which does not go via a definition of paths; instead, it goes directly to the accumulation itself.</p>
<p>
As with the efficient version of the MPC construction, it is coinductive, and uses an accumulating parameter to carry in to each node the seed from higher up in the tree; so the downwards accumulation is of type <img src='http://s0.wp.com/latex.php?latex=%7B%5Cgamma+%5Ctimes+%5Cmathsf%7BT%7D%5Calpha+%5Crightarrow+%5Cmathsf%7BL%7D%5Cbeta%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;gamma &#92;times &#92;mathsf{T}&#92;alpha &#92;rightarrow &#92;mathsf{L}&#92;beta}' title='{&#92;gamma &#92;times &#92;mathsf{T}&#92;alpha &#92;rightarrow &#92;mathsf{L}&#92;beta}' class='latex' />. It is defined as an unfold, with a body <img src='http://s0.wp.com/latex.php?latex=%7Bg%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{g}' title='{g}' class='latex' /> of type </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cgamma+%5Ctimes+%5Cmathsf%7BT%7D%5Calpha+%5Crightarrow+%5Cmathsf%7BG%7D%28%5Cbeta%2C+%5Cgamma+%5Ctimes+%5Cmathsf%7BT%7D%5Calpha%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;gamma &#92;times &#92;mathsf{T}&#92;alpha &#92;rightarrow &#92;mathsf{G}(&#92;beta, &#92;gamma &#92;times &#92;mathsf{T}&#92;alpha) ' title='&#92;displaystyle  &#92;gamma &#92;times &#92;mathsf{T}&#92;alpha &#92;rightarrow &#92;mathsf{G}(&#92;beta, &#92;gamma &#92;times &#92;mathsf{T}&#92;alpha) ' class='latex' />
</p></blockquote>
<p> The result <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BG%7D%28%5Cbeta%2C+%5Cgamma+%5Ctimes+%5Cmathsf%7BT%7D%5Calpha%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{G}(&#92;beta, &#92;gamma &#92;times &#92;mathsf{T}&#92;alpha)}' title='{&#92;mathsf{G}(&#92;beta, &#92;gamma &#92;times &#92;mathsf{T}&#92;alpha)}' class='latex' /> of applying the body will be constructed from two components, of types <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BG%7D%28%5Cbeta%2C+%5Cgamma%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{G}(&#92;beta, &#92;gamma)}' title='{&#92;mathsf{G}(&#92;beta, &#92;gamma)}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BG%7D%281%2C+%5Cmathsf%7BT%7D%5Calpha%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{G}(1, &#92;mathsf{T}&#92;alpha)}' title='{&#92;mathsf{G}(1, &#92;mathsf{T}&#92;alpha)}' class='latex' />: the first gives the root label of the accumulation and the seeds for processing the children, and the second gives the children themselves. </p>
<p>
These two components get combined to make the whole result via a function </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathit%7Bzip%7D+%3A%3A+%5Cmathsf%7BG%7D%28%5Calpha%2C%5Cbeta%29+%5Ctimes+%5Cmathsf%7BG%7D%28%5Cgamma%2C%5Cdelta%29+%5Crightarrow+%5Cmathsf%7BG%7D%28%5Calpha+%5Ctimes+%5Cgamma%2C+%5Cbeta+%5Ctimes+%5Cdelta%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathit{zip} :: &#92;mathsf{G}(&#92;alpha,&#92;beta) &#92;times &#92;mathsf{G}(&#92;gamma,&#92;delta) &#92;rightarrow &#92;mathsf{G}(&#92;alpha &#92;times &#92;gamma, &#92;beta &#92;times &#92;delta) ' title='&#92;displaystyle  &#92;mathit{zip} :: &#92;mathsf{G}(&#92;alpha,&#92;beta) &#92;times &#92;mathsf{G}(&#92;gamma,&#92;delta) &#92;rightarrow &#92;mathsf{G}(&#92;alpha &#92;times &#92;gamma, &#92;beta &#92;times &#92;delta) ' class='latex' />
</p></blockquote>
<p> This will be partial in general, defined only for pairs of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BG%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{G}}' title='{&#92;mathsf{G}}' class='latex' />-structures of the same shape.</p>
<p>
The second component of <img src='http://s0.wp.com/latex.php?latex=%7Bg%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{g}' title='{g}' class='latex' /> is the easier to define; given input <img src='http://s0.wp.com/latex.php?latex=%7B%5Cgamma+%5Ctimes+%5Cmathsf%7BT%7D%5Calpha%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;gamma &#92;times &#92;mathsf{T}&#92;alpha}' title='{&#92;gamma &#92;times &#92;mathsf{T}&#92;alpha}' class='latex' />, it unpacks the <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BT%7D%5Calpha%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{T}&#92;alpha}' title='{&#92;mathsf{T}&#92;alpha}' class='latex' /> to <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%28%5Calpha%2C%5Cmathsf%7BT%7D%5Calpha%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}(&#92;alpha,&#92;mathsf{T}&#92;alpha)}' title='{&#92;mathsf{F}(&#92;alpha,&#92;mathsf{T}&#92;alpha)}' class='latex' />, and discards the <img src='http://s0.wp.com/latex.php?latex=%7B%5Cgamma%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;gamma}' title='{&#92;gamma}' class='latex' /> and the <img src='http://s0.wp.com/latex.php?latex=%7B%5Calpha%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;alpha}' title='{&#92;alpha}' class='latex' /> (recall that <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BL%7D%5Calpha%3D%5Cmu%5Cmathsf%7BG%7D%5Calpha%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{L}&#92;alpha=&#92;mu&#92;mathsf{G}&#92;alpha}' title='{&#92;mathsf{L}&#92;alpha=&#92;mu&#92;mathsf{G}&#92;alpha}' class='latex' /> is the labelled variant of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BT%7D%5Calpha%3D%5Cmu%5Cmathsf%7BF%7D%5Calpha%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{T}&#92;alpha=&#92;mu&#92;mathsf{F}&#92;alpha}' title='{&#92;mathsf{T}&#92;alpha=&#92;mu&#92;mathsf{F}&#92;alpha}' class='latex' />, where <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BG%7D%28%5Calpha%2C%5Cbeta%29+%3D+%5Calpha+%5Ctimes+%5Cmathsf%7BF%7D%281%2C%5Cbeta%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{G}(&#92;alpha,&#92;beta) = &#92;alpha &#92;times &#92;mathsf{F}(1,&#92;beta)}' title='{&#92;mathsf{G}(&#92;alpha,&#92;beta) = &#92;alpha &#92;times &#92;mathsf{F}(1,&#92;beta)}' class='latex' />).</p>
<p>
For the first component, we enforce the constraint that all output labels are dependent only on their ancestors by unpacking the <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BT%7D%5Calpha%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{T}&#92;alpha}' title='{&#92;mathsf{T}&#92;alpha}' class='latex' /> and pruning off the children, giving input <img src='http://s0.wp.com/latex.php?latex=%7B%5Cgamma+%5Ctimes+%5Cmathsf%7BF%7D%28%5Calpha%2C1%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;gamma &#92;times &#92;mathsf{F}(&#92;alpha,1)}' title='{&#92;gamma &#92;times &#92;mathsf{F}(&#92;alpha,1)}' class='latex' />. We then suppose as a parameter to the accumulation a function <img src='http://s0.wp.com/latex.php?latex=%7Bf%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f}' title='{f}' class='latex' /> of type <img src='http://s0.wp.com/latex.php?latex=%7B%5Cgamma+%5Ctimes+%5Cmathsf%7BF%7D%28%5Calpha%2C1%29+%5Crightarrow+%5Cbeta+%5Ctimes+%5Cmathsf%7BF%7D%281%2C%5Cgamma%29+%3D+%5Cmathsf%7BG%7D%28%5Cbeta%2C%5Cgamma%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;gamma &#92;times &#92;mathsf{F}(&#92;alpha,1) &#92;rightarrow &#92;beta &#92;times &#92;mathsf{F}(1,&#92;gamma) = &#92;mathsf{G}(&#92;beta,&#92;gamma)}' title='{&#92;gamma &#92;times &#92;mathsf{F}(&#92;alpha,1) &#92;rightarrow &#92;beta &#92;times &#92;mathsf{F}(1,&#92;gamma) = &#92;mathsf{G}(&#92;beta,&#92;gamma)}' class='latex' /> to complete the construction of the first component. In order that the two components can be zipped together, we require that <img src='http://s0.wp.com/latex.php?latex=%7Bf%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f}' title='{f}' class='latex' /> is shape-preserving in its second argument: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathsf%7BF%7D%28%21%2C%21%29+%5Ccdot+%5Cmathit%7Bsnd%7D+%3D+%5Cmathsf%7BF%7D%28%21%2C%21%29+%5Ccdot+f+%5Ccdot+%5Cmathit%7Bsnd%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathsf{F}(!,!) &#92;cdot &#92;mathit{snd} = &#92;mathsf{F}(!,!) &#92;cdot f &#92;cdot &#92;mathit{snd} ' title='&#92;displaystyle  &#92;mathsf{F}(!,!) &#92;cdot &#92;mathit{snd} = &#92;mathsf{F}(!,!) &#92;cdot f &#92;cdot &#92;mathit{snd} ' class='latex' />
</p></blockquote>
<p> where <img src='http://s0.wp.com/latex.php?latex=%7B%21+%3A+%5Calpha+%5Crightarrow+1%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{! : &#92;alpha &#92;rightarrow 1}' title='{! : &#92;alpha &#92;rightarrow 1}' class='latex' /> is the unique function to the unit type. Then, although the <img src='http://s0.wp.com/latex.php?latex=%7Bg%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{g}' title='{g}' class='latex' /> built from these two components depends on the partial function <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bzip%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{zip}}' title='{&#92;mathit{zip}}' class='latex' />, it will still itself be total.</p>
<p>
The SCP construction gets rid of the &#8220;&#8230;&#8221;s in the MPC construction. It is also inherently efficient, in the sense that if the core operation <img src='http://s0.wp.com/latex.php?latex=%7Bf%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f}' title='{f}' class='latex' /> takes constant time then the whole accumulation takes linear time. However, use of the partial <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bzip%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{zip}}' title='{&#92;mathit{zip}}' class='latex' /> function to define a total accumulation is a bit unsatisfactory, taking us outside the domain of sets and total functions. Moreover, there&#8217;s now only half an explanation in terms of paths: accumulations in which the label attached to each node depends only on the <em>list</em> of its ancestors, and not on the left-to-right ordering of siblings, can be factored into a list function (in fact, a <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfoldl%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{foldl}}' title='{&#92;mathit{foldl}}' class='latex' />) mapped over the &#8220;paths&#8221;, which is now a tree of lists; but accumulations in which left children are treated differently from right children, such as the parallel prefix and tree drawing algorithms mentioned earlier, can not.</p>
<p>
<a name="derivatives"></a> </p>
<h2> Generic downwards accumulations, via derivatives </h2>
<p>
After another interlude of about a decade, and with the benefit of new results to exploit, I had a &#8220;eureka&#8221; moment: the linearization of a shape functor is closely related to the beautiful notion of the <em>derivative</em> of a datatype, as promoted by <a href="http://www.strictlypositive.org/calculus/">Conor McBride</a>. The crucial observation Conor made is that the &#8220;one-hole contexts&#8221; of a datatype&mdash;that is, for a container datatype, the datatype of data structures with precisely one element missing&mdash;can be neatly formalized using an analogue of the rules of differential calculus. The one-hole contexts are precisely what you need to identify which particular child you&#8217;re talking about out of a collection of children. (If you&#8217;re going to follow along with some coding, I recommend that you also read Conor&#8217;s paper <a href="http://dx.doi.org/10.1145/1328438.1328474">Clowns to the left of me, jokers to the right</a>. This gives the more general construction of <em>dissecting</em> a datatype, identifying a unique hole, but also allowing the &#8220;clowns&#8221; to the left of the hole to have a different type from the &#8220;jokers&#8221; to the right. I think the explanation of the relationship with the differential calculus is much better explained here; the original notion of derivative can be retrieved by specializing the clowns and jokers to the same type.)</p>
<p>
The essence of the construction is the notion of a <em>derivative</em> <img src='http://s0.wp.com/latex.php?latex=%7B%5CDelta%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;Delta&#92;mathsf{F}}' title='{&#92;Delta&#92;mathsf{F}}' class='latex' /> of a functor <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />. For our purposes, we want the derivative in the second argument only of a bifunctor; informally, <img src='http://s0.wp.com/latex.php?latex=%7B%5CDelta%5Cmathsf%7BF%7D%28%5Calpha%2C%5Cbeta%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;Delta&#92;mathsf{F}(&#92;alpha,&#92;beta)}' title='{&#92;Delta&#92;mathsf{F}(&#92;alpha,&#92;beta)}' class='latex' /> is like <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%28%5Calpha%2C%5Cbeta%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}(&#92;alpha,&#92;beta)}' title='{&#92;mathsf{F}(&#92;alpha,&#92;beta)}' class='latex' />, but with precisely one <img src='http://s0.wp.com/latex.php?latex=%7B%5Cbeta%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;beta}' title='{&#92;beta}' class='latex' /> missing. Given such a one-hole context, and an element with which to plug the hole, one can reconstruct the whole structure: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathit%7Bplug%7D_%5Cmathsf%7BF%7D+%3A%3A+%5Cbeta+%5Ctimes+%5CDelta%5Cmathsf%7BF%7D%28%5Calpha%2C%5Cbeta%29+%5Crightarrow+%5Cmathsf%7BF%7D%28%5Calpha%2C%5Cbeta%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathit{plug}_&#92;mathsf{F} :: &#92;beta &#92;times &#92;Delta&#92;mathsf{F}(&#92;alpha,&#92;beta) &#92;rightarrow &#92;mathsf{F}(&#92;alpha,&#92;beta) ' title='&#92;displaystyle  &#92;mathit{plug}_&#92;mathsf{F} :: &#92;beta &#92;times &#92;Delta&#92;mathsf{F}(&#92;alpha,&#92;beta) &#92;rightarrow &#92;mathsf{F}(&#92;alpha,&#92;beta) ' class='latex' />
</p></blockquote>
<p> That&#8217;s how to consume one-hole contexts; how can we produce them? We could envisage some kind of inverse <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bunplug%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{unplug}}' title='{&#92;mathit{unplug}}' class='latex' /> of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bplug%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{plug}}' title='{&#92;mathit{plug}}' class='latex' />, which breaks an <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />-structure into an element and a context; but this requires us to invent a language for specifying which particular element we mean&mdash;<img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bplug%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{plug}}' title='{&#92;mathit{plug}}' class='latex' /> is not injective, so <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bunplug%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{unplug}}' title='{&#92;mathit{unplug}}' class='latex' /> needs an extra argument. A simpler approach is to provide an operator that annotates every position at once with the one-hole context for that position: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathit%7Bpositions%7D_%5Cmathsf%7BF%7D+%3A%3A+%5Cmathsf%7BF%7D%28%5Calpha%2C%5Cbeta%29+%5Crightarrow+%5Cmathsf%7BF%7D%28%5Calpha%2C+%5Cbeta+%5Ctimes+%5CDelta%5Cmathsf%7BF%7D%28%5Calpha%2C%5Cbeta%29%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathit{positions}_&#92;mathsf{F} :: &#92;mathsf{F}(&#92;alpha,&#92;beta) &#92;rightarrow &#92;mathsf{F}(&#92;alpha, &#92;beta &#92;times &#92;Delta&#92;mathsf{F}(&#92;alpha,&#92;beta)) ' title='&#92;displaystyle  &#92;mathit{positions}_&#92;mathsf{F} :: &#92;mathsf{F}(&#92;alpha,&#92;beta) &#92;rightarrow &#92;mathsf{F}(&#92;alpha, &#92;beta &#92;times &#92;Delta&#92;mathsf{F}(&#92;alpha,&#92;beta)) ' class='latex' />
</p></blockquote>
<p> One property of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bpositions%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{positions}}' title='{&#92;mathit{positions}}' class='latex' /> is that it really is an annotation&mdash;if you throw away the annotations, you get back what you started with: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathsf%7BF%7D%28%5Cmathit%7Bid%7D%2C%5Cmathit%7Bfst%7D%29%5C%2C%28%5Cmathit%7Bpositions%7D%5C%2Cx%29+%3D+x+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathsf{F}(&#92;mathit{id},&#92;mathit{fst})&#92;,(&#92;mathit{positions}&#92;,x) = x ' title='&#92;displaystyle  &#92;mathsf{F}(&#92;mathit{id},&#92;mathit{fst})&#92;,(&#92;mathit{positions}&#92;,x) = x ' class='latex' />
</p></blockquote>
<p> A second property relates it to <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bplug%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{plug}}' title='{&#92;mathit{plug}}' class='latex' />&mdash;each of elements in a hole position plugs into its associated one-hole context to yield the same whole structure back again: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathsf%7BF%7D%28%5Cmathit%7Bid%7D%2C%5Cmathit%7Bplug%7D%29%5C%2C%28%5Cmathit%7Bpositions%7D%5C%2Cx%29+%3D+%5Cmathsf%7BF%7D%28%5Cmathit%7Bid%7D%2C%5Cmathit%7Bconst%7D%5C%2Cx%29%5C%2Cx+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathsf{F}(&#92;mathit{id},&#92;mathit{plug})&#92;,(&#92;mathit{positions}&#92;,x) = &#92;mathsf{F}(&#92;mathit{id},&#92;mathit{const}&#92;,x)&#92;,x ' title='&#92;displaystyle  &#92;mathsf{F}(&#92;mathit{id},&#92;mathit{plug})&#92;,(&#92;mathit{positions}&#92;,x) = &#92;mathsf{F}(&#92;mathit{id},&#92;mathit{const}&#92;,x)&#92;,x ' class='latex' />
</p></blockquote>
<p> (I believe that those two properties completely determine <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bplug%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{plug}}' title='{&#92;mathit{plug}}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bpositions%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{positions}}' title='{&#92;mathit{positions}}' class='latex' />.)</p>
<p>
Incidentally, the derivative <img src='http://s0.wp.com/latex.php?latex=%7B%5CDelta%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;Delta&#92;mathsf{F}}' title='{&#92;Delta&#92;mathsf{F}}' class='latex' /> of a bifunctor can be elegantly represented as an <em>associated type synonym</em> in Haskell, in a type class <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7BDiff%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{Diff}}' title='{&#92;mathit{Diff}}' class='latex' /> of bifunctors differentiable in their second argument, along with <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bplug%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{plug}}' title='{&#92;mathit{plug}}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bpositions%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{positions}}' title='{&#92;mathit{positions}}' class='latex' />: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Crlap%7B%5C%28%5Cmathbf%7Bclass%7D%5C%3B+%5Cmathit%7BBifunctor%7D%5C%2Cf+%5CRightarrow+%5Cmathit%7BDiff%7D%5C%2Cf+%5C%3B%5Cmathbf%7Bwhere%7D%5C%29%7D+%5C%5C+%5Cqquad+%5Crlap%7B%5C%28%5Cmathbf%7Btype%7D%5C%3B+%5Cmathit%7BDelta%7D%5C%2Cf+%3A%3A+%5Cast+%5Crightarrow+%5Cast+%5Crightarrow+%5Cast%5C%29%7D+%5C%5C+%5Cqquad+%5Cmathit%7Bplug%7D+%26%3A%3A%26+%28%5Cbeta%2C+%5Cmathit%7BDelta%7D%5C%2Cf%5C%2C%5Calpha%5C%2C%5Cbeta%29+%5Crightarrow+f%5C%2C%5Calpha%5C%2C%5Cbeta+%5C%5C+%5Cqquad+%5Cmathit%7Bpositions%7D+%26%3A%3A%26+f%5C%2C%5Calpha%5C%2C%5Cbeta+%5Crightarrow+f%5C%2C%5Calpha%5C%2C%28%5Cbeta%2C+%5Cmathit%7BDelta%7D%5C%2Cf%5C%2C%5Calpha%5C%2C%5Cbeta%29+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;rlap{&#92;(&#92;mathbf{class}&#92;; &#92;mathit{Bifunctor}&#92;,f &#92;Rightarrow &#92;mathit{Diff}&#92;,f &#92;;&#92;mathbf{where}&#92;)} &#92;&#92; &#92;qquad &#92;rlap{&#92;(&#92;mathbf{type}&#92;; &#92;mathit{Delta}&#92;,f :: &#92;ast &#92;rightarrow &#92;ast &#92;rightarrow &#92;ast&#92;)} &#92;&#92; &#92;qquad &#92;mathit{plug} &amp;::&amp; (&#92;beta, &#92;mathit{Delta}&#92;,f&#92;,&#92;alpha&#92;,&#92;beta) &#92;rightarrow f&#92;,&#92;alpha&#92;,&#92;beta &#92;&#92; &#92;qquad &#92;mathit{positions} &amp;::&amp; f&#92;,&#92;alpha&#92;,&#92;beta &#92;rightarrow f&#92;,&#92;alpha&#92;,(&#92;beta, &#92;mathit{Delta}&#92;,f&#92;,&#92;alpha&#92;,&#92;beta) &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;rlap{&#92;(&#92;mathbf{class}&#92;; &#92;mathit{Bifunctor}&#92;,f &#92;Rightarrow &#92;mathit{Diff}&#92;,f &#92;;&#92;mathbf{where}&#92;)} &#92;&#92; &#92;qquad &#92;rlap{&#92;(&#92;mathbf{type}&#92;; &#92;mathit{Delta}&#92;,f :: &#92;ast &#92;rightarrow &#92;ast &#92;rightarrow &#92;ast&#92;)} &#92;&#92; &#92;qquad &#92;mathit{plug} &amp;::&amp; (&#92;beta, &#92;mathit{Delta}&#92;,f&#92;,&#92;alpha&#92;,&#92;beta) &#92;rightarrow f&#92;,&#92;alpha&#92;,&#92;beta &#92;&#92; &#92;qquad &#92;mathit{positions} &amp;::&amp; f&#92;,&#92;alpha&#92;,&#92;beta &#92;rightarrow f&#92;,&#92;alpha&#92;,(&#92;beta, &#92;mathit{Delta}&#92;,f&#92;,&#92;alpha&#92;,&#92;beta) &#92;end{array} ' class='latex' />
</p></blockquote>
<p> Conor&#8217;s papers show how to define instances of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7BDiff%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{Diff}}' title='{&#92;mathit{Diff}}' class='latex' /> for all polynomial functors <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />&mdash;anything made out of constants, projections, sums, and products.</p>
<p>
The path to a node in a data structure is simply a list of one-hole contexts&mdash;let&#8217;s say, innermost context first, although it doesn&#8217;t make much difference&mdash;but with all the data off the path (that is, the other children) stripped away: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathsf%7BP%7D%5Calpha+%3D+%5Cmathsf%7BList%7D%28%5CDelta%5Cmathsf%7BF%7D%28%5Calpha%2C1%29%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathsf{P}&#92;alpha = &#92;mathsf{List}(&#92;Delta&#92;mathsf{F}(&#92;alpha,1)) ' title='&#92;displaystyle  &#92;mathsf{P}&#92;alpha = &#92;mathsf{List}(&#92;Delta&#92;mathsf{F}(&#92;alpha,1)) ' class='latex' />
</p></blockquote>
<p> This is a projection of Huet&#8217;s <a href="http://dx.doi.org/10.1017/S0956796897002864">zipper</a>, which preserves the off-path children, and records also the subtree in focus at the end of the path: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathsf%7BZipper%7D_%5Cmathsf%7BF%7D%5C%2C%5Calpha+%3D+%5Cmathsf%7BList%7D%28%5CDelta%5Cmathsf%7BF%7D%28%5Calpha%2C%5Cmu%5Cmathsf%7BF%7D%5Calpha%29%29+%5Ctimes+%5Cmu%5Cmathsf%7BF%7D%5Calpha+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathsf{Zipper}_&#92;mathsf{F}&#92;,&#92;alpha = &#92;mathsf{List}(&#92;Delta&#92;mathsf{F}(&#92;alpha,&#92;mu&#92;mathsf{F}&#92;alpha)) &#92;times &#92;mu&#92;mathsf{F}&#92;alpha ' title='&#92;displaystyle  &#92;mathsf{Zipper}_&#92;mathsf{F}&#92;,&#92;alpha = &#92;mathsf{List}(&#92;Delta&#92;mathsf{F}(&#92;alpha,&#92;mu&#92;mathsf{F}&#92;alpha)) &#92;times &#92;mu&#92;mathsf{F}&#92;alpha ' class='latex' />
</p></blockquote>
<p> Since the contexts are listed innermost-first in the path, closing up a zipper to reconstruct a tree is a <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfoldl%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{foldl}}' title='{&#92;mathit{foldl}}' class='latex' /> over the path: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+close_%5Cmathsf%7BF%7D+%26%3A%3A%26+%5Cmathsf%7BZipper%7D_%5Cmathsf%7BF%7D%5C%2C%5Calpha+%5Crightarrow+%5Cmu%5Cmathsf%7BF%7D%5Calpha+%5C%5C+close_%5Cmathsf%7BF%7D%5C%2C%28p%2Ct%29+%26%3D%26+%5Cmathit%7Bfoldl%7D%5C%2C%28%5Cmathit%7Bin%7D%5Ccdot%5Cmathit%7Bplug%7D%29%5C%2Ct%5C%2Cp+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} close_&#92;mathsf{F} &amp;::&amp; &#92;mathsf{Zipper}_&#92;mathsf{F}&#92;,&#92;alpha &#92;rightarrow &#92;mu&#92;mathsf{F}&#92;alpha &#92;&#92; close_&#92;mathsf{F}&#92;,(p,t) &amp;=&amp; &#92;mathit{foldl}&#92;,(&#92;mathit{in}&#92;cdot&#92;mathit{plug})&#92;,t&#92;,p &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} close_&#92;mathsf{F} &amp;::&amp; &#92;mathsf{Zipper}_&#92;mathsf{F}&#92;,&#92;alpha &#92;rightarrow &#92;mu&#92;mathsf{F}&#92;alpha &#92;&#92; close_&#92;mathsf{F}&#92;,(p,t) &amp;=&amp; &#92;mathit{foldl}&#92;,(&#92;mathit{in}&#92;cdot&#92;mathit{plug})&#92;,t&#92;,p &#92;end{array} ' class='latex' />
</p></blockquote>
<p>
Now, let&#8217;s develop the function <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bpaths%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{paths}}' title='{&#92;mathit{paths}}' class='latex' />, which turns a tree into a labelled tree of paths. We will write it with an accumulating parameter, representing the &#8220;path so far&#8221;: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Cmathit%7Bpaths%7D_%5Cmathsf%7BF%7D+%26%3A%3A%26+%5Cmathsf%7BT%7D%5Calpha+%5Crightarrow+%5Cmathsf%7BL%7D%28%5Cmathsf%7BP%7D%5Calpha%29+%5C%5C+%5Cmathit%7Bpaths%7D_%5Cmathsf%7BF%7D%5C%2Ct+%26%3D%26+%5Cmathit%7Bpaths%7D%27_%5Cmathsf%7BF%7D%5C%2C%28t%2C%5B%5C%2C%5D%29+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{paths}_&#92;mathsf{F} &amp;::&amp; &#92;mathsf{T}&#92;alpha &#92;rightarrow &#92;mathsf{L}(&#92;mathsf{P}&#92;alpha) &#92;&#92; &#92;mathit{paths}_&#92;mathsf{F}&#92;,t &amp;=&amp; &#92;mathit{paths}&#039;_&#92;mathsf{F}&#92;,(t,[&#92;,]) &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{paths}_&#92;mathsf{F} &amp;::&amp; &#92;mathsf{T}&#92;alpha &#92;rightarrow &#92;mathsf{L}(&#92;mathsf{P}&#92;alpha) &#92;&#92; &#92;mathit{paths}_&#92;mathsf{F}&#92;,t &amp;=&amp; &#92;mathit{paths}&#039;_&#92;mathsf{F}&#92;,(t,[&#92;,]) &#92;end{array} ' class='latex' />
</p></blockquote>
<p> Given the components <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bin%7D_%5Cmathsf%7BF%7D%5C%2Cx%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{in}_&#92;mathsf{F}&#92;,x}' title='{&#92;mathit{in}_&#92;mathsf{F}&#92;,x}' class='latex' /> of a tree and a path <img src='http://s0.wp.com/latex.php?latex=%7Bp%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{p}' title='{p}' class='latex' /> to its root, <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bpaths%7D%27_%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{paths}&#039;_&#92;mathsf{F}}' title='{&#92;mathit{paths}&#039;_&#92;mathsf{F}}' class='latex' /> must construct the corresponding labelled tree of paths. Since <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BL%7D+%3D+%5Cmu%5Cmathsf%7BG%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{L} = &#92;mu&#92;mathsf{G}}' title='{&#92;mathsf{L} = &#92;mu&#92;mathsf{G}}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BG%7D%28%5Calpha%2C%5Cbeta%29+%3D+%5Calpha+%5Ctimes+%5Cmathsf%7BF%7D%281%2C%5Cbeta%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{G}(&#92;alpha,&#92;beta) = &#92;alpha &#92;times &#92;mathsf{F}(1,&#92;beta)}' title='{&#92;mathsf{G}(&#92;alpha,&#92;beta) = &#92;alpha &#92;times &#92;mathsf{F}(1,&#92;beta)}' class='latex' />, this amounts to constructing a value of type <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BP%7D%5Calpha+%5Ctimes+%5Cmathsf%7BF%7D%281%2C+%5Cmathsf%7BL%7D%28%5Cmathsf%7BP%7D%5Calpha%29%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{P}&#92;alpha &#92;times &#92;mathsf{F}(1, &#92;mathsf{L}(&#92;mathsf{P}&#92;alpha))}' title='{&#92;mathsf{P}&#92;alpha &#92;times &#92;mathsf{F}(1, &#92;mathsf{L}(&#92;mathsf{P}&#92;alpha))}' class='latex' />. For the first component of this pair we will use <img src='http://s0.wp.com/latex.php?latex=%7Bp%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{p}' title='{p}' class='latex' />, the path so far. The second component can be constructed from <img src='http://s0.wp.com/latex.php?latex=%7Bx%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{x}' title='{x}' class='latex' /> by identifying all children via <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bpositions%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{positions}}' title='{&#92;mathit{positions}}' class='latex' />, discarding some information with judicious <img src='http://s0.wp.com/latex.php?latex=%7B%21%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{!}' title='{!}' class='latex' />s, consing each one-hole context onto <img src='http://s0.wp.com/latex.php?latex=%7Bp%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{p}' title='{p}' class='latex' /> to make a longer path, then making recursive calls on each child:
<p align="center"><img width="200" src="http://patternsinfp.files.wordpress.com/2011/07/accumulations-scand.png?w=200"></p>
<p>  That is, </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Cmathit%7Bpaths%7D%27_%5Cmathsf%7BF%7D+%26%3A%3A%26+%5Cmathsf%7BT%7D%5Calpha%5Ctimes%5Cmathsf%7BP%7D%5Calpha+%5Crightarrow+%5Cmathsf%7BL%7D%28%5Cmathsf%7BP%7D%5Calpha%29+%5C%5C+%5Cmathit%7Bpaths%7D%27_%5Cmathsf%7BF%7D%5C%2C%28%5Cmathit%7Bin%7D_%5Cmathsf%7BF%7D%5C%2Cx%2Cp%29+%26%3D%26+%5Cmathit%7Bin%7D_%5Cmathsf%7BG%7D%28p%2C+%5Cmathsf%7BF%7D%28%21%2C+%5Cmathit%7Bpaths%7D%27_%5Cmathsf%7BF%7D+%5Ccdot+%5Cmathit%7Bid%7D%5Ctimes%28%28%3Ap%29%5Ccdot%5CDelta%5Cmathsf%7BF%7D%28%5Cmathit%7Bid%7D%2C%21%29%29+%29%5C%2C%28%5Cmathit%7Bpositions%7D%5C%2Cx%29%29+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{paths}&#039;_&#92;mathsf{F} &amp;::&amp; &#92;mathsf{T}&#92;alpha&#92;times&#92;mathsf{P}&#92;alpha &#92;rightarrow &#92;mathsf{L}(&#92;mathsf{P}&#92;alpha) &#92;&#92; &#92;mathit{paths}&#039;_&#92;mathsf{F}&#92;,(&#92;mathit{in}_&#92;mathsf{F}&#92;,x,p) &amp;=&amp; &#92;mathit{in}_&#92;mathsf{G}(p, &#92;mathsf{F}(!, &#92;mathit{paths}&#039;_&#92;mathsf{F} &#92;cdot &#92;mathit{id}&#92;times((:p)&#92;cdot&#92;Delta&#92;mathsf{F}(&#92;mathit{id},!)) )&#92;,(&#92;mathit{positions}&#92;,x)) &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{paths}&#039;_&#92;mathsf{F} &amp;::&amp; &#92;mathsf{T}&#92;alpha&#92;times&#92;mathsf{P}&#92;alpha &#92;rightarrow &#92;mathsf{L}(&#92;mathsf{P}&#92;alpha) &#92;&#92; &#92;mathit{paths}&#039;_&#92;mathsf{F}&#92;,(&#92;mathit{in}_&#92;mathsf{F}&#92;,x,p) &amp;=&amp; &#92;mathit{in}_&#92;mathsf{G}(p, &#92;mathsf{F}(!, &#92;mathit{paths}&#039;_&#92;mathsf{F} &#92;cdot &#92;mathit{id}&#92;times((:p)&#92;cdot&#92;Delta&#92;mathsf{F}(&#92;mathit{id},!)) )&#92;,(&#92;mathit{positions}&#92;,x)) &#92;end{array} ' class='latex' />
</p></blockquote>
<p> Downwards accumulations are then path functions mapped over the result of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bpaths%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{paths}}' title='{&#92;mathit{paths}}' class='latex' />. However, we restrict ourselves to path functions that are instances of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfoldr%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{foldr}}' title='{&#92;mathit{foldr}}' class='latex' />, because only then are there common subexpressions to be shared between a parent and its children (remember that paths are innermost-first, so related nodes share a tail of their ancestors). </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Cmathit%7Bscand%7D_%5Cmathsf%7BF%7D+%26%3A%3A%26+%28%5CDelta%5Cmathsf%7BF%7D%28%5Calpha%2C1%29%5Ctimes%5Cbeta%5Crightarrow%5Cbeta%29+%5Crightarrow+%5Cbeta+%5Crightarrow+%5Cmathsf%7BT%7D%5Calpha+%5Crightarrow+%5Cmathsf%7BL%7D%5Cbeta+%5C%5C+%5Cmathit%7Bscand%7D_%5Cmathsf%7BF%7D%5C%2Cf%5C%2Ce+%26%3D%26+%5Cmathit%7Bmap%7D%5C%2C%28%5Cmathit%7Bfoldr%7D%5C%2Cf%5C%2Ce%29+%5Ccdot+%5Cmathit%7Bpaths%7D+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{scand}_&#92;mathsf{F} &amp;::&amp; (&#92;Delta&#92;mathsf{F}(&#92;alpha,1)&#92;times&#92;beta&#92;rightarrow&#92;beta) &#92;rightarrow &#92;beta &#92;rightarrow &#92;mathsf{T}&#92;alpha &#92;rightarrow &#92;mathsf{L}&#92;beta &#92;&#92; &#92;mathit{scand}_&#92;mathsf{F}&#92;,f&#92;,e &amp;=&amp; &#92;mathit{map}&#92;,(&#92;mathit{foldr}&#92;,f&#92;,e) &#92;cdot &#92;mathit{paths} &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{scand}_&#92;mathsf{F} &amp;::&amp; (&#92;Delta&#92;mathsf{F}(&#92;alpha,1)&#92;times&#92;beta&#92;rightarrow&#92;beta) &#92;rightarrow &#92;beta &#92;rightarrow &#92;mathsf{T}&#92;alpha &#92;rightarrow &#92;mathsf{L}&#92;beta &#92;&#92; &#92;mathit{scand}_&#92;mathsf{F}&#92;,f&#92;,e &amp;=&amp; &#92;mathit{map}&#92;,(&#92;mathit{foldr}&#92;,f&#92;,e) &#92;cdot &#92;mathit{paths} &#92;end{array} ' class='latex' />
</p></blockquote>
<p> Moreover, it is straightforward to fuse the <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bmap%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{map}}' title='{&#92;mathit{map}}' class='latex' /> with <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bpaths%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{paths}}' title='{&#92;mathit{paths}}' class='latex' />, to obtain </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Cmathit%7Bscand%7D_%5Cmathsf%7BF%7D%5C%2Cf%5C%2Ce%5C%2Ct+%26%3D%26+%5Cmathit%7Bscand%7D%27_%5Cmathsf%7BF%7D%5C%2Cf%5C%2C%28t%2Ce%29+%5Cmedskip+%5C%5C+%5Cmathit%7Bscand%7D%27_%5Cmathsf%7BF%7D%5C%2Cf%5C%2C%28%5Cmathit%7Bin%7D_%5Cmathsf%7BF%7D%5C%2Cx%2Ce%29+%26%3D%26+%5Cmathit%7Bin%7D_%5Cmathsf%7BG%7D%28e%2C+%5Cmathsf%7BF%7D%28%21%2C+%5Cmathit%7Bscand%7D%27_%5Cmathsf%7BF%7D%5C%2Cf+%5Ccdot+%5Cmathit%7Bid%7D%5Ctimes+g+%29%5C%2C%28%5Cmathit%7Bpositions%7D%5C%2Cx%29%29+%5C%5C+%26+%26+%5Cquad%5Cmathbf%7Bwhere%7D%5C%3B+g%5C%2Cd+%3D+f%5C%2C%28%5CDelta%5Cmathsf%7BF%7D%28%5Cmathit%7Bid%7D%2C%21%29%5C%2Cd%2C+e%29+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{scand}_&#92;mathsf{F}&#92;,f&#92;,e&#92;,t &amp;=&amp; &#92;mathit{scand}&#039;_&#92;mathsf{F}&#92;,f&#92;,(t,e) &#92;medskip &#92;&#92; &#92;mathit{scand}&#039;_&#92;mathsf{F}&#92;,f&#92;,(&#92;mathit{in}_&#92;mathsf{F}&#92;,x,e) &amp;=&amp; &#92;mathit{in}_&#92;mathsf{G}(e, &#92;mathsf{F}(!, &#92;mathit{scand}&#039;_&#92;mathsf{F}&#92;,f &#92;cdot &#92;mathit{id}&#92;times g )&#92;,(&#92;mathit{positions}&#92;,x)) &#92;&#92; &amp; &amp; &#92;quad&#92;mathbf{where}&#92;; g&#92;,d = f&#92;,(&#92;Delta&#92;mathsf{F}(&#92;mathit{id},!)&#92;,d, e) &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{scand}_&#92;mathsf{F}&#92;,f&#92;,e&#92;,t &amp;=&amp; &#92;mathit{scand}&#039;_&#92;mathsf{F}&#92;,f&#92;,(t,e) &#92;medskip &#92;&#92; &#92;mathit{scand}&#039;_&#92;mathsf{F}&#92;,f&#92;,(&#92;mathit{in}_&#92;mathsf{F}&#92;,x,e) &amp;=&amp; &#92;mathit{in}_&#92;mathsf{G}(e, &#92;mathsf{F}(!, &#92;mathit{scand}&#039;_&#92;mathsf{F}&#92;,f &#92;cdot &#92;mathit{id}&#92;times g )&#92;,(&#92;mathit{positions}&#92;,x)) &#92;&#92; &amp; &amp; &#92;quad&#92;mathbf{where}&#92;; g&#92;,d = f&#92;,(&#92;Delta&#92;mathsf{F}(&#92;mathit{id},!)&#92;,d, e) &#92;end{array} ' class='latex' />
</p></blockquote>
<p> which takes time linear in the size of the tree, assuming that <img src='http://s0.wp.com/latex.php?latex=%7Bf%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f}' title='{f}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7Be%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{e}' title='{e}' class='latex' /> take constant time. </p>
<p>
Finally, in the case that the function being mapped over the paths is a <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfoldl%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{foldl}}' title='{&#92;mathit{foldl}}' class='latex' /> as well as a <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfoldr%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{foldr}}' title='{&#92;mathit{foldr}}' class='latex' />, then we can apply the <a href="http://dx.doi.org/10.1017/S0956796800001908">Third Homomorphism Theorem</a> to conclude that it is also an associative fold over lists. From this (I believe) we get a <a href="http://dx.doi.org/10.1016/0167-6423(94)00013-1">very efficient parallel algorithm</a> for computing the accumulation, taking time logarithmic in the size of the tree&mdash;even if the tree has greater than logarithmic depth.</p>
<p>
<br />  <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=patternsinfp.wordpress.com&#038;blog=15593982&#038;post=213&#038;subd=patternsinfp&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://patternsinfp.wordpress.com/2011/07/12/upwards-and-downwards-accumulations/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/39d7e0397cba13d1494d9a48c1f42167?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jeremygibbons</media:title>
		</media:content>

		<media:content url="http://patternsinfp.files.wordpress.com/2011/07/accumulations-tree.png" medium="image" />

		<media:content url="http://patternsinfp.files.wordpress.com/2011/07/accumulations-path.png" medium="image" />

		<media:content url="http://patternsinfp.files.wordpress.com/2011/07/accumulations-scand.png" medium="image" />
	</item>
		<item>
		<title>Distributivity in Horner&#8217;s Rule</title>
		<link>http://patternsinfp.wordpress.com/2011/05/17/distributivity-in-horners-rule/</link>
		<comments>http://patternsinfp.wordpress.com/2011/05/17/distributivity-in-horners-rule/#comments</comments>
		<pubDate>Tue, 17 May 2011 21:53:29 +0000</pubDate>
		<dc:creator>jeremygibbons</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://patternsinfp.wordpress.com/?p=205</guid>
		<description><![CDATA[This is a continuation of my previous post on Horner&#8217;s Rule, and in particular, of the discussion there about distributivity in the datatype-generic version of the Maximum Segment Sum problem: the essential property behind Horner&#8217;s Rule is one of distributivity. &#8230; <a href="http://patternsinfp.wordpress.com/2011/05/17/distributivity-in-horners-rule/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=patternsinfp.wordpress.com&#038;blog=15593982&#038;post=205&#038;subd=patternsinfp&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>
 This is a continuation of my <a href="http://patternsinfp.wordpress.com/2011/05/05/horners-rule/">previous post on Horner&#8217;s Rule</a>, and in particular, of the discussion there about distributivity in the datatype-generic version of the Maximum Segment Sum problem:<br />
<blockquote> the essential property behind Horner&#8217;s Rule is one of distributivity. In the datatype-generic case, we will model this as follows. We are given an <img src='http://s0.wp.com/latex.php?latex=%7B%28%5Cmathsf%7BF%7D%5C%2C%5Calpha%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(&#92;mathsf{F}&#92;,&#92;alpha)}' title='{(&#92;mathsf{F}&#92;,&#92;alpha)}' class='latex' />-algebra <img src='http://s0.wp.com/latex.php?latex=%7B%28%5Cbeta%2Cf%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(&#92;beta,f)}' title='{(&#92;beta,f)}' class='latex' /> [for a binary shape functor <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />], and a <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BM%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{M}}' title='{&#92;mathsf{M}}' class='latex' />-algebra <img src='http://s0.wp.com/latex.php?latex=%7B%28%5Cbeta%2Ck%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(&#92;beta,k)}' title='{(&#92;beta,k)}' class='latex' /> [for a collection monad <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BM%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{M}}' title='{&#92;mathsf{M}}' class='latex' />]; you might think of these as &#8220;datatype-generic product&#8221; and &#8220;collection sum&#8221;, respectively. Then there are two different methods of computing a <img src='http://s0.wp.com/latex.php?latex=%7B%5Cbeta%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;beta}' title='{&#92;beta}' class='latex' /> result from an <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%5C%2C%5Calpha%5C%2C%28%5Cmathsf%7BM%7D%5C%2C%5Cbeta%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}&#92;,&#92;alpha&#92;,(&#92;mathsf{M}&#92;,&#92;beta)}' title='{&#92;mathsf{F}&#92;,&#92;alpha&#92;,(&#92;mathsf{M}&#92;,&#92;beta)}' class='latex' /> structure: we can either distribute the <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%5C%2C%5Calpha%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}&#92;,&#92;alpha}' title='{&#92;mathsf{F}&#92;,&#92;alpha}' class='latex' /> structure over the collection(s) of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cbeta%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;beta}' title='{&#92;beta}' class='latex' />s, compute the &#8220;product&#8221; <img src='http://s0.wp.com/latex.php?latex=%7Bf%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f}' title='{f}' class='latex' /> of each structure, and then compute the &#8220;sum&#8221; <img src='http://s0.wp.com/latex.php?latex=%7Bk%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{k}' title='{k}' class='latex' /> of the resulting products; or we can &#8220;sum&#8221; each collection, then compute the &#8220;product&#8221; of the resulting structure. Distributivity of &#8220;product&#8221; over &#8220;sum&#8221; is the property that these two different methods agree, as illustrated in the following diagram.
<p align="center"><img width="300" src="http://patternsinfp.files.wordpress.com/2011/05/horner-distributivity.png?w=300"></p>
<p>  For example, with <img src='http://s0.wp.com/latex.php?latex=%7Bf+%3A%3A+%5Cmathsf%7BF%7D%5C%2C%7B%5Cmathbb+Z%7D%5C%2C%7B%5Cmathbb+Z%7D+%5Crightarrow+%7B%5Cmathbb+Z%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f :: &#92;mathsf{F}&#92;,{&#92;mathbb Z}&#92;,{&#92;mathbb Z} &#92;rightarrow {&#92;mathbb Z}}' title='{f :: &#92;mathsf{F}&#92;,{&#92;mathbb Z}&#92;,{&#92;mathbb Z} &#92;rightarrow {&#92;mathbb Z}}' class='latex' /> adding all the integers in an <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />-structure, and <img src='http://s0.wp.com/latex.php?latex=%7Bk+%3A%3A+%5Cmathsf%7BM%7D%5C%2C%7B%5Cmathbb+Z%7D+%5Crightarrow+%7B%5Cmathbb+Z%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{k :: &#92;mathsf{M}&#92;,{&#92;mathbb Z} &#92;rightarrow {&#92;mathbb Z}}' title='{k :: &#92;mathsf{M}&#92;,{&#92;mathbb Z} &#92;rightarrow {&#92;mathbb Z}}' class='latex' /> finding the maximum of a (non-empty) collection, the diagram commutes. </p></blockquote>
<p> There&#8217;s a bit of hand-waving above to justify the claim that this is really a kind of distributivity. What does it have to do with the common-or-garden equation </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++a+%5Cotimes+%28b+%5Coplus+c%29+%3D+%28a+%5Cotimes+b%29+%5Coplus+%28a+%5Cotimes+c%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  a &#92;otimes (b &#92;oplus c) = (a &#92;otimes b) &#92;oplus (a &#92;otimes c) ' title='&#92;displaystyle  a &#92;otimes (b &#92;oplus c) = (a &#92;otimes b) &#92;oplus (a &#92;otimes c) ' class='latex' />
</p></blockquote>
<p> stating distributivity of one binary operator over another? That question is the subject of this post.</p>
<p><h2> Distributing over effects </h2>
<p>
Recall that <img src='http://s0.wp.com/latex.php?latex=%7B%5Cdelta_2+%3A%3A+%28%5Cmathsf%7BF%7D%5C%2C%5Calpha%29%5Cmathsf%7BM%7D+%5Cmathbin%7B%5Cstackrel%7B.%7D%7B%5Cto%7D%7D+%5Cmathsf%7BM%7D%28%5Cmathsf%7BF%7D%5C%2C%5Calpha%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;delta_2 :: (&#92;mathsf{F}&#92;,&#92;alpha)&#92;mathsf{M} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{M}(&#92;mathsf{F}&#92;,&#92;alpha)}' title='{&#92;delta_2 :: (&#92;mathsf{F}&#92;,&#92;alpha)&#92;mathsf{M} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{M}(&#92;mathsf{F}&#92;,&#92;alpha)}' class='latex' /> distributes the shape functor <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' /> over the monad <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BM%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{M}}' title='{&#92;mathsf{M}}' class='latex' /> in its second argument; this is the form of distribution over effects that crops up in the datatype-generic Maximum Segment Sum problem. More generally, this works for any idiom <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BM%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{M}}' title='{&#92;mathsf{M}}' class='latex' />; this will be important below.</p>
<p>
Generalizing in another direction, one might think of distributing over an idiom in both arguments of the bifunctor, via an operator <img src='http://s0.wp.com/latex.php?latex=%7B%5Cdelta+%3A+%5Cmathsf%7BF%7D+%5Ccdot+%28%5Cmathsf%7BM%7D+%5Ctimes+%5Cmathsf%7BM%7D%29+%5Cmathbin%7B%5Cstackrel%7B.%7D%7B%5Cto%7D%7D+%5Cmathsf%7BM%7D+%5Ccdot+%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;delta : &#92;mathsf{F} &#92;cdot (&#92;mathsf{M} &#92;times &#92;mathsf{M}) &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{M} &#92;cdot &#92;mathsf{F}}' title='{&#92;delta : &#92;mathsf{F} &#92;cdot (&#92;mathsf{M} &#92;times &#92;mathsf{M}) &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{M} &#92;cdot &#92;mathsf{F}}' class='latex' />, which is to say, <img src='http://s0.wp.com/latex.php?latex=%7B%5Cdelta_%5Cbeta+%3A%3A+%5Cmathsf%7BF%7D%5C%2C%28%5Cmathsf%7BM%7D%5Cbeta%29%5C%2C%28%5Cmathsf%7BM%7D%5Cbeta%29+%5Crightarrow+%5Cmathsf%7BM%7D%28%5Cmathsf%7BF%7D%5Cbeta%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;delta_&#92;beta :: &#92;mathsf{F}&#92;,(&#92;mathsf{M}&#92;beta)&#92;,(&#92;mathsf{M}&#92;beta) &#92;rightarrow &#92;mathsf{M}(&#92;mathsf{F}&#92;beta)}' title='{&#92;delta_&#92;beta :: &#92;mathsf{F}&#92;,(&#92;mathsf{M}&#92;beta)&#92;,(&#92;mathsf{M}&#92;beta) &#92;rightarrow &#92;mathsf{M}(&#92;mathsf{F}&#92;beta)}' class='latex' />, natural in the <img src='http://s0.wp.com/latex.php?latex=%7B%5Cbeta%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;beta}' title='{&#92;beta}' class='latex' />. This is the <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bbidist%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{bidist}}' title='{&#92;mathit{bidist}}' class='latex' /> method of the <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7BBitraversable%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{Bitraversable}}' title='{&#92;mathit{Bitraversable}}' class='latex' /> subclass of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7BBifunctor%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{Bifunctor}}' title='{&#92;mathit{Bifunctor}}' class='latex' /> that Bruno Oliveira and I used in our <a href="http://web.comlab.ox.ac.uk/publications/publication1409-abstract.html">Essence of the Iterator Pattern</a> paper; informally, it requires just that <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' /> has a finite ordered sequence of &#8220;element positions&#8221;. Given <img src='http://s0.wp.com/latex.php?latex=%7B%5Cdelta%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;delta}' title='{&#92;delta}' class='latex' />, one can define <img src='http://s0.wp.com/latex.php?latex=%7B%5Cdelta_2+%3D+%5Cdelta+%5Ccdot+%5Cmathsf%7BF%7D%5C%2C%5Cmathit%7Bpure%7D%5C%2C%5Cmathit%7Bid%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;delta_2 = &#92;delta &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{pure}&#92;,&#92;mathit{id}}' title='{&#92;delta_2 = &#92;delta &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{pure}&#92;,&#92;mathit{id}}' class='latex' />.</p>
<p>
That traversability (or equivalently, distributivity over effects) for a bifunctor <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' /> is definable for any idiom, not just any monad, means that one can also conveniently define an operator <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bcontents%7D_%7B%5Cmathsf%7BH%7D%7D+%3A+%5Cmathsf%7BH%7D+%5Cmathbin%7B%5Cstackrel%7B.%7D%7B%5Cto%7D%7D+%5Cmathsf%7BList%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{contents}_{&#92;mathsf{H}} : &#92;mathsf{H} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{List}}' title='{&#92;mathit{contents}_{&#92;mathsf{H}} : &#92;mathsf{H} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{List}}' class='latex' /> for any traversable unary functor <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BH%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{H}}' title='{&#92;mathsf{H}}' class='latex' />. This is because the constant functor <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BK%7D_%7B%5B%5Cbeta%5D%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{K}_{[&#92;beta]}}' title='{&#92;mathsf{K}_{[&#92;beta]}}' class='latex' /> (which takes any <img src='http://s0.wp.com/latex.php?latex=%7B%5Calpha%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;alpha}' title='{&#92;alpha}' class='latex' /> to <img src='http://s0.wp.com/latex.php?latex=%7B%5B%5Cbeta%5D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{[&#92;beta]}' title='{[&#92;beta]}' class='latex' />) is an idiom: the <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bpure%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{pure}}' title='{&#92;mathit{pure}}' class='latex' /> method returns the empty list, and idiomatic application appends two lists. Then one can define </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathit%7Bcontents%7D_%7B%5Cmathsf%7BH%7D%7D+%3D+%5Cdelta+%5Ccdot+%5Cmathsf%7BH%7D%5C%2C%5Cmathit%7Bwrap%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathit{contents}_{&#92;mathsf{H}} = &#92;delta &#92;cdot &#92;mathsf{H}&#92;,&#92;mathit{wrap} ' title='&#92;displaystyle  &#92;mathit{contents}_{&#92;mathsf{H}} = &#92;delta &#92;cdot &#92;mathsf{H}&#92;,&#92;mathit{wrap} ' class='latex' />
</p></blockquote>
<p> where <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bwrap%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{wrap}}' title='{&#92;mathit{wrap}}' class='latex' /> makes a singleton list. For a traversable bifunctor <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />, we define <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bcontents%7D_%7B%5Cmathsf%7BF%7D%7D+%3D+%5Cmathit%7Bcontents%7D_%7B%5Cmathsf%7BF%7D%5Ccdot%5Ctriangle%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{contents}_{&#92;mathsf{F}} = &#92;mathit{contents}_{&#92;mathsf{F}&#92;cdot&#92;triangle}}' title='{&#92;mathit{contents}_{&#92;mathsf{F}} = &#92;mathit{contents}_{&#92;mathsf{F}&#92;cdot&#92;triangle}}' class='latex' /> where <img src='http://s0.wp.com/latex.php?latex=%7B%5Ctriangle%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;triangle}' title='{&#92;triangle}' class='latex' /> is the diagonal functor; that is, <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bcontents%7D_%7B%5Cmathsf%7BF%7D%7D+%3A%3A+%5Cmathsf%7BF%7D%5C%2C%5Cbeta%5C%2C%5Cbeta+%5Crightarrow+%5B%5Cbeta%5D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{contents}_{&#92;mathsf{F}} :: &#92;mathsf{F}&#92;,&#92;beta&#92;,&#92;beta &#92;rightarrow [&#92;beta]}' title='{&#92;mathit{contents}_{&#92;mathsf{F}} :: &#92;mathsf{F}&#92;,&#92;beta&#92;,&#92;beta &#92;rightarrow [&#92;beta]}' class='latex' />, natural in the <img src='http://s0.wp.com/latex.php?latex=%7B%5Cbeta%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;beta}' title='{&#92;beta}' class='latex' />. (No constant functor is a monad, except in trivial categories, so this convenient definition of contents doesn&#8217;t work monadically. Of course, one can use a writer monad, but this isn&#8217;t quite so convenient, because an additional step is needed to extract the output.)</p>
<p>
One important axiom of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cdelta%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;delta}' title='{&#92;delta}' class='latex' /> that I made recent use of in a paper with Richard Bird on <a href="http://web.comlab.ox.ac.uk/publications/publication4876-abstract.html">Effective Reasoning about Effectful Traversals</a> is that it should be &#8220;natural in the contents&#8221;: it should leave shape unchanged, and depend on contents only up to the extent of their ordering. Say that a natural transformation <img src='http://s0.wp.com/latex.php?latex=%7B%5Cphi+%3A+%5Cmathsf%7BF%7D+%5Cmathbin%7B%5Cstackrel%7B.%7D%7B%5Cto%7D%7D+%5Cmathsf%7BG%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;phi : &#92;mathsf{F} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{G}}' title='{&#92;phi : &#92;mathsf{F} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{G}}' class='latex' /> between traversable functors <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BG%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{G}}' title='{&#92;mathsf{G}}' class='latex' /> &#8220;preserves contents&#8221; if <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bcontents%7D_%7B%5Cmathsf%7BG%7D%7D+%5Ccdot+%5Cphi+%3D+%5Cmathit%7Bcontents%7D_%7B%5Cmathsf%7BF%7D%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{contents}_{&#92;mathsf{G}} &#92;cdot &#92;phi = &#92;mathit{contents}_{&#92;mathsf{F}}}' title='{&#92;mathit{contents}_{&#92;mathsf{G}} &#92;cdot &#92;phi = &#92;mathit{contents}_{&#92;mathsf{F}}}' class='latex' />. Then, in the case of unary functors, the formalization of &#8220;naturality in the contents&#8221; requires <img src='http://s0.wp.com/latex.php?latex=%7B%5Cdelta%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;delta}' title='{&#92;delta}' class='latex' /> to respect content-preserving <img src='http://s0.wp.com/latex.php?latex=%7B%5Cphi%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;phi}' title='{&#92;phi}' class='latex' />: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cdelta_%7B%5Cmathsf%7BG%7D%7D+%5Ccdot+%5Cphi+%3D+%5Cmathsf%7BM%7D%5Cphi+%5Ccdot+%5Cdelta_%7B%5Cmathsf%7BF%7D%7D+%3A+%5Cmathsf%7BT%7D%5Cmathsf%7BM%7D+%5Cmathbin%7B%5Cstackrel%7B.%7D%7B%5Cto%7D%7D+%5Cmathsf%7BM%7D%5Cmathsf%7BG%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;delta_{&#92;mathsf{G}} &#92;cdot &#92;phi = &#92;mathsf{M}&#92;phi &#92;cdot &#92;delta_{&#92;mathsf{F}} : &#92;mathsf{T}&#92;mathsf{M} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{M}&#92;mathsf{G} ' title='&#92;displaystyle  &#92;delta_{&#92;mathsf{G}} &#92;cdot &#92;phi = &#92;mathsf{M}&#92;phi &#92;cdot &#92;delta_{&#92;mathsf{F}} : &#92;mathsf{T}&#92;mathsf{M} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{M}&#92;mathsf{G} ' class='latex' />
</p></blockquote>
<p> In particular, <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bcontents%7D_%7B%5Cmathsf%7BF%7D%7D+%3A+%5Cmathsf%7BF%7D+%5Cmathbin%7B%5Cstackrel%7B.%7D%7B%5Cto%7D%7D+%5Cmathsf%7BList%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{contents}_{&#92;mathsf{F}} : &#92;mathsf{F} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{List}}' title='{&#92;mathit{contents}_{&#92;mathsf{F}} : &#92;mathsf{F} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{List}}' class='latex' /> itself preserves contents, and so we expect </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cdelta_%7B%5Cmathsf%7BList%7D%7D+%5Ccdot+%5Cmathit%7Bcontents%7D_%7B%5Cmathsf%7BF%7D%7D+%3D+%5Cmathsf%7BM%7D%28%5Cmathit%7Bcontents%7D_%7B%5Cmathsf%7BF%7D%7D%29+%5Ccdot+%5Cdelta_%7B%5Cmathsf%7BF%7D%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;delta_{&#92;mathsf{List}} &#92;cdot &#92;mathit{contents}_{&#92;mathsf{F}} = &#92;mathsf{M}(&#92;mathit{contents}_{&#92;mathsf{F}}) &#92;cdot &#92;delta_{&#92;mathsf{F}} ' title='&#92;displaystyle  &#92;delta_{&#92;mathsf{List}} &#92;cdot &#92;mathit{contents}_{&#92;mathsf{F}} = &#92;mathsf{M}(&#92;mathit{contents}_{&#92;mathsf{F}}) &#92;cdot &#92;delta_{&#92;mathsf{F}} ' class='latex' />
</p></blockquote>
<p> to hold. </p>
<p><h2> Folding a structure </h2>
<p>
Happily, the same generic operation <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bcontents%7D_%7B%5Cmathsf%7BF%7D%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{contents}_{&#92;mathsf{F}}}' title='{&#92;mathit{contents}_{&#92;mathsf{F}}}' class='latex' /> provides a datatype-generic means to &#8220;fold&#8221; over the elements of an <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />-structure. Given a binary operator <img src='http://s0.wp.com/latex.php?latex=%7B%5Cotimes+%3A%3A+%5Cbeta%5Ctimes%5Cbeta+%5Crightarrow+%5Cbeta%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;otimes :: &#92;beta&#92;times&#92;beta &#92;rightarrow &#92;beta}' title='{&#92;otimes :: &#92;beta&#92;times&#92;beta &#92;rightarrow &#92;beta}' class='latex' /> and an initial value <img src='http://s0.wp.com/latex.php?latex=%7Bb+%3A%3A+%5Cbeta%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{b :: &#92;beta}' title='{b :: &#92;beta}' class='latex' />, we can define an <img src='http://s0.wp.com/latex.php?latex=%7B%28%5Cmathsf%7BF%7D%5C%2C%5Cbeta%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(&#92;mathsf{F}&#92;,&#92;beta)}' title='{(&#92;mathsf{F}&#92;,&#92;beta)}' class='latex' />-algebra <img src='http://s0.wp.com/latex.php?latex=%7B%28%5Cbeta%2Cf%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(&#92;beta,f)}' title='{(&#92;beta,f)}' class='latex' />&mdash;that is, a function <img src='http://s0.wp.com/latex.php?latex=%7Bf+%3A%3A+%5Cmathsf%7BF%7D%5C%2C%5Cbeta%5C%2C%5Cbeta%5Crightarrow%5Cbeta%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f :: &#92;mathsf{F}&#92;,&#92;beta&#92;,&#92;beta&#92;rightarrow&#92;beta}' title='{f :: &#92;mathsf{F}&#92;,&#92;beta&#92;,&#92;beta&#92;rightarrow&#92;beta}' class='latex' />&mdash;by </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++f+%3D+%5Cmathit%7Bfoldr%7D%5C%2C%28%5Cotimes%29%5C%2Cb+%5Ccdot+%5Cmathit%7Bcontents%7D_%7B%5Cmathsf%7BF%7D%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  f = &#92;mathit{foldr}&#92;,(&#92;otimes)&#92;,b &#92;cdot &#92;mathit{contents}_{&#92;mathsf{F}} ' title='&#92;displaystyle  f = &#92;mathit{foldr}&#92;,(&#92;otimes)&#92;,b &#92;cdot &#92;mathit{contents}_{&#92;mathsf{F}} ' class='latex' />
</p></blockquote>
<p> (This is a slight specialization of the presentation of the datatype-generic MSS problem from last time; there we had <img src='http://s0.wp.com/latex.php?latex=%7Bf+%3A%3A+%5Cmathsf%7BF%7D%5C%2C%5Calpha%5C%2C%5Cbeta+%5Crightarrow+%5Cbeta%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f :: &#92;mathsf{F}&#92;,&#92;alpha&#92;,&#92;beta &#92;rightarrow &#92;beta}' title='{f :: &#92;mathsf{F}&#92;,&#92;alpha&#92;,&#92;beta &#92;rightarrow &#92;beta}' class='latex' />. The specialization arises because we are hoping to define such an <img src='http://s0.wp.com/latex.php?latex=%7Bf%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f}' title='{f}' class='latex' /> given a homogeneous binary operator <img src='http://s0.wp.com/latex.php?latex=%7B%5Cotimes%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;otimes}' title='{&#92;otimes}' class='latex' />. On the other hand, the introduction of the initial value <img src='http://s0.wp.com/latex.php?latex=%7Bb%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{b}' title='{b}' class='latex' /> is no specialization, as we needed such a value for the &#8220;product&#8221; of an empty &#8220;segment&#8221; anyway.)</p>
<p>
Incidentally, I believe that this &#8220;generic folding&#8221; construction is exactly what is intended in Ross Paterson&#8217;s <a href="http://www.haskell.org/ghc/docs/6.12.2/html/libraries/base-4.2.0.1/Data-Foldable.html">Data.Foldable library</a>.</p>
<p><h2> Summing a collection </h2>
<p>
The other ingredient we need is an <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BM%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{M}}' title='{&#92;mathsf{M}}' class='latex' />-algebra <img src='http://s0.wp.com/latex.php?latex=%7B%28%5Cbeta%2Ck%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(&#92;beta,k)}' title='{(&#92;beta,k)}' class='latex' />. We already decided last time to<br />
<blockquote> stick to <em>reductions</em>&mdash;<img src='http://s0.wp.com/latex.php?latex=%7Bk%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{k}' title='{k}' class='latex' />s of the form <img src='http://s0.wp.com/latex.php?latex=%7B%5Coplus%2F%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;oplus/}' title='{&#92;oplus/}' class='latex' /> for associative binary operator <img src='http://s0.wp.com/latex.php?latex=%7B%7B%5Coplus%7D+%3A%3A+%5Cbeta+%5Ctimes+%5Cbeta+%5Crightarrow+%5Cbeta%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{{&#92;oplus} :: &#92;beta &#92;times &#92;beta &#92;rightarrow &#92;beta}' title='{{&#92;oplus} :: &#92;beta &#92;times &#92;beta &#92;rightarrow &#92;beta}' class='latex' />; then we also have distribution over choice: <img src='http://s0.wp.com/latex.php?latex=%7B%5Coplus+%2F+%28x+%5Cmathbin%7B%5Cunderline%7B%5Csmash%7B%5Cmathit%7Bmplus%7D%7D%7D%7D+y%29+%3D+%28%5Coplus%2Fx%29+%5Coplus+%28%5Coplus%2Fy%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;oplus / (x &#92;mathbin{&#92;underline{&#92;smash{&#92;mathit{mplus}}}} y) = (&#92;oplus/x) &#92;oplus (&#92;oplus/y)}' title='{&#92;oplus / (x &#92;mathbin{&#92;underline{&#92;smash{&#92;mathit{mplus}}}} y) = (&#92;oplus/x) &#92;oplus (&#92;oplus/y)}' class='latex' />. Note also that we prohibited empty collections in <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BM%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{M}}' title='{&#92;mathsf{M}}' class='latex' />, so we do not need a unit for <img src='http://s0.wp.com/latex.php?latex=%7B%5Coplus%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;oplus}' title='{&#92;oplus}' class='latex' />. </p></blockquote>
<p> On account of <img src='http://s0.wp.com/latex.php?latex=%7B%5Coplus%2F%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;oplus/}' title='{&#92;oplus/}' class='latex' /> being an algebra for the collection monad <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BM%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{M}}' title='{&#92;mathsf{M}}' class='latex' />, we also get a singleton rule <img src='http://s0.wp.com/latex.php?latex=%7B%5Coplus%2F+%5Ccdot+%5Cmathit%7Breturn%7D+%3D+%5Cmathit%7Bid%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;oplus/ &#92;cdot &#92;mathit{return} = &#92;mathit{id}}' title='{&#92;oplus/ &#92;cdot &#92;mathit{return} = &#92;mathit{id}}' class='latex' />.</p>
<p><h2> Reduction to distributivity for lists </h2>
<p>
One of the take-home messages in the <i>Effective Reasoning about Effectful Traversals</i> paper is that it helps to reduce a traversal problem for datatypes in general to a more specific one about lists, exploiting the &#8220;naturality in contents&#8221; property of traversability. We&#8217;ll use that tactic for the distributivity property in the datatype-generic version Horner&#8217;s Rule. </p>
<p><p align="center"><img width="500" src="http://patternsinfp.files.wordpress.com/2011/05/horner2-generic.png?w=500"></p>
<p>  In this diagram, the perimeter is the commuting diagram given at the start of this post&mdash;the diagram we have to justify. Face (1) is the definition of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cdelta_2%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;delta_2}' title='{&#92;delta_2}' class='latex' /> in terms of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cdelta%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;delta}' title='{&#92;delta}' class='latex' />. Faces (2) and (3) are the expansion of <img src='http://s0.wp.com/latex.php?latex=%7Bf%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f}' title='{f}' class='latex' /> as generic folding of an <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />-structure. Face (4) follows from <img src='http://s0.wp.com/latex.php?latex=%7B%5Coplus%2F%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;oplus/}' title='{&#92;oplus/}' class='latex' /> being an <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BM%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{M}}' title='{&#92;mathsf{M}}' class='latex' />-algebra, and hence being a left-inverse of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Breturn%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{return}}' title='{&#92;mathit{return}}' class='latex' />. Face (5) is an instance of the naturality property of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bcontents%7D_%7B%5Cmathsf%7BF%7D%7D+%3A+%5Cmathsf%7BF%7D%5Ctriangle+%5Cmathbin%7B%5Cstackrel%7B.%7D%7B%5Cto%7D%7D+%5Cmathsf%7BList%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{contents}_{&#92;mathsf{F}} : &#92;mathsf{F}&#92;triangle &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{List}}' title='{&#92;mathit{contents}_{&#92;mathsf{F}} : &#92;mathsf{F}&#92;triangle &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{List}}' class='latex' />. Face (6) is the property that <img src='http://s0.wp.com/latex.php?latex=%7B%5Cdelta%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;delta}' title='{&#92;delta}' class='latex' /> respects the contents-preserving transformation <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bcontents%7D_%7B%5Cmathsf%7BF%7D%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{contents}_{&#92;mathsf{F}}}' title='{&#92;mathit{contents}_{&#92;mathsf{F}}}' class='latex' />. Therefore, the whole diagram commutes if Face (7) does&mdash;so let&#8217;s look at Face (7)!</p>
<p><h2> Distributivity for lists </h2>
<p>
Here&#8217;s Face (7) again:
<p align="center"><img width="300" src="http://patternsinfp.files.wordpress.com/2011/05/horner2-lists.png?w=300"></p>
<p>  Demonstrating that this diagram commutes is not too difficult, because both sides turn out to be list folds. </p>
<p>
Around the left and bottom edges, we have a fold <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfoldr%7D%5C%2C%28%5Cotimes%29%5C%2Cb%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{foldr}&#92;,(&#92;otimes)&#92;,b}' title='{&#92;mathit{foldr}&#92;,(&#92;otimes)&#92;,b}' class='latex' /> after a map <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BList%7D%5C%2C%28%5Coplus%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{List}&#92;,(&#92;oplus)}' title='{&#92;mathsf{List}&#92;,(&#92;oplus)}' class='latex' />, which automatically fuses to <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfoldr%7D%5C%2C%28%5Codot%29%5C%2Cb%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{foldr}&#92;,(&#92;odot)&#92;,b}' title='{&#92;mathit{foldr}&#92;,(&#92;odot)&#92;,b}' class='latex' />, where <img src='http://s0.wp.com/latex.php?latex=%7B%5Codot%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;odot}' title='{&#92;odot}' class='latex' /> is defined by </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++x+%5Codot+a+%3D+%28%5Coplus%2Fx%29+%5Cotimes+a+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  x &#92;odot a = (&#92;oplus/x) &#92;otimes a ' title='&#92;displaystyle  x &#92;odot a = (&#92;oplus/x) &#92;otimes a ' class='latex' />
</p></blockquote>
<p> or, pointlessly, </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%28%5Codot%29+%3D+%28%5Cotimes%29+%5Ccdot+%28%5Coplus%2F%29+%5Ctimes+%5Cmathit%7Bid%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  (&#92;odot) = (&#92;otimes) &#92;cdot (&#92;oplus/) &#92;times &#92;mathit{id} ' title='&#92;displaystyle  (&#92;odot) = (&#92;otimes) &#92;cdot (&#92;oplus/) &#92;times &#92;mathit{id} ' class='latex' />
</p></blockquote>
<p>
Around the top and right edges we have the composition <img src='http://s0.wp.com/latex.php?latex=%7B%5Coplus%2F+%5Ccdot+%5Cmathsf%7BM%7D%28%5Cmathit%7Bfoldr%7D%5C%2C%28%5Cotimes%29%5C%2Cb%29+%5Ccdot+%5Cdelta_%7B%5Cmathsf%7BList%7D%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;oplus/ &#92;cdot &#92;mathsf{M}(&#92;mathit{foldr}&#92;,(&#92;otimes)&#92;,b) &#92;cdot &#92;delta_{&#92;mathsf{List}}}' title='{&#92;oplus/ &#92;cdot &#92;mathsf{M}(&#92;mathit{foldr}&#92;,(&#92;otimes)&#92;,b) &#92;cdot &#92;delta_{&#92;mathsf{List}}}' class='latex' />. If we can write <img src='http://s0.wp.com/latex.php?latex=%7B%5Cdelta_%7B%5Cmathsf%7BList%7D%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;delta_{&#92;mathsf{List}}}' title='{&#92;delta_{&#92;mathsf{List}}}' class='latex' /> as an instance of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfoldr%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{foldr}}' title='{&#92;mathit{foldr}}' class='latex' />, we can then use the fusion law for <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfoldr%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{foldr}}' title='{&#92;mathit{foldr}}' class='latex' /> </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++h+%5Ccdot+%5Cmathit%7Bfoldr%7D%5C%2Cf%5C%2Ce+%3D+%5Cmathit%7Bfoldr%7D%5C%2Cf%27%5C%2Ce%27+%5C%3B%5CLeftarrow%5C%3B+h%5C%2Ce%3De%27+%5Cland+h+%5Ccdot+f+%3D+f%27+%5Ccdot+%5Cmathit%7Bid%7D%5Ctimes+h+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  h &#92;cdot &#92;mathit{foldr}&#92;,f&#92;,e = &#92;mathit{foldr}&#92;,f&#039;&#92;,e&#039; &#92;;&#92;Leftarrow&#92;; h&#92;,e=e&#039; &#92;land h &#92;cdot f = f&#039; &#92;cdot &#92;mathit{id}&#92;times h ' title='&#92;displaystyle  h &#92;cdot &#92;mathit{foldr}&#92;,f&#92;,e = &#92;mathit{foldr}&#92;,f&#039;&#92;,e&#039; &#92;;&#92;Leftarrow&#92;; h&#92;,e=e&#039; &#92;land h &#92;cdot f = f&#039; &#92;cdot &#92;mathit{id}&#92;times h ' class='latex' />
</p></blockquote>
<p> to prove that this composition equals <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfoldr%7D%5C%2C%28%5Codot%29%5C%2Cb%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{foldr}&#92;,(&#92;odot)&#92;,b}' title='{&#92;mathit{foldr}&#92;,(&#92;odot)&#92;,b}' class='latex' />.</p>
<p>
In fact, there are various equivalent ways of writing <img src='http://s0.wp.com/latex.php?latex=%7B%5Cdelta_%7B%5Cmathsf%7BList%7D%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;delta_{&#92;mathsf{List}}}' title='{&#92;delta_{&#92;mathsf{List}}}' class='latex' /> as an instance of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfoldr%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{foldr}}' title='{&#92;mathit{foldr}}' class='latex' />. The definition given by Conor McBride and Ross Paterson in their <a href="http://dx.doi.org/10.1017/S0956796807006326">original paper on idioms</a> looked like the identity function, but with added idiomness: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Cdelta_%7B%5Cmathsf%7BList%7D%7D%5C%2C%5B%5C%2C%5D+%26%3D%26+%5Cmathit%7Bpure%7D%5C%2C%5B%5C%2C%5D+%5C%5C+%5Cdelta_%7B%5Cmathsf%7BList%7D%7D%5C%2C%28%5Cmathit%7Bmb%7D+%3A+%5Cmathit%7Bmbs%7D%29+%26%3D%26+%5Cmathit%7Bpure%7D%5C%2C%28%3A%29+%5Ccircledast+%5Cmathit%7Bmb%7D+%5Ccircledast+%5Cdelta_%7B%5Cmathsf%7BList%7D%7D%5C%2C%5Cmathit%7Bmbs%7D+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;delta_{&#92;mathsf{List}}&#92;,[&#92;,] &amp;=&amp; &#92;mathit{pure}&#92;,[&#92;,] &#92;&#92; &#92;delta_{&#92;mathsf{List}}&#92;,(&#92;mathit{mb} : &#92;mathit{mbs}) &amp;=&amp; &#92;mathit{pure}&#92;,(:) &#92;circledast &#92;mathit{mb} &#92;circledast &#92;delta_{&#92;mathsf{List}}&#92;,&#92;mathit{mbs} &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;delta_{&#92;mathsf{List}}&#92;,[&#92;,] &amp;=&amp; &#92;mathit{pure}&#92;,[&#92;,] &#92;&#92; &#92;delta_{&#92;mathsf{List}}&#92;,(&#92;mathit{mb} : &#92;mathit{mbs}) &amp;=&amp; &#92;mathit{pure}&#92;,(:) &#92;circledast &#92;mathit{mb} &#92;circledast &#92;delta_{&#92;mathsf{List}}&#92;,&#92;mathit{mbs} &#92;end{array} ' class='latex' />
</p></blockquote>
<p> In the special case that the idiom is a monad, it can be written in terms of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7BliftM%7D_0%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{liftM}_0}' title='{&#92;mathit{liftM}_0}' class='latex' /> (aka <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Breturn%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{return}}' title='{&#92;mathit{return}}' class='latex' />) and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7BliftM%7D_2%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{liftM}_2}' title='{&#92;mathit{liftM}_2}' class='latex' />: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Cdelta_%7B%5Cmathsf%7BList%7D%7D%5C%2C%5B%5C%2C%5D+%26%3D%26+%5Cmathit%7BliftM%7D_0%5C%2C%5B%5C%2C%5D+%5C%5C+%5Cdelta_%7B%5Cmathsf%7BList%7D%7D%5C%2C%28%5Cmathit%7Bmb%7D+%3A+%5Cmathit%7Bmbs%7D%29+%26%3D%26+%5Cmathit%7BliftM%7D_2%5C%2C%28%3A%29%5C%2C%5Cmathit%7Bmb%7D%5C%2C%28%5Cdelta_%7B%5Cmathsf%7BList%7D%7D%5C%2C%5Cmathit%7Bmbs%7D%29+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;delta_{&#92;mathsf{List}}&#92;,[&#92;,] &amp;=&amp; &#92;mathit{liftM}_0&#92;,[&#92;,] &#92;&#92; &#92;delta_{&#92;mathsf{List}}&#92;,(&#92;mathit{mb} : &#92;mathit{mbs}) &amp;=&amp; &#92;mathit{liftM}_2&#92;,(:)&#92;,&#92;mathit{mb}&#92;,(&#92;delta_{&#92;mathsf{List}}&#92;,&#92;mathit{mbs}) &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;delta_{&#92;mathsf{List}}&#92;,[&#92;,] &amp;=&amp; &#92;mathit{liftM}_0&#92;,[&#92;,] &#92;&#92; &#92;delta_{&#92;mathsf{List}}&#92;,(&#92;mathit{mb} : &#92;mathit{mbs}) &amp;=&amp; &#92;mathit{liftM}_2&#92;,(:)&#92;,&#92;mathit{mb}&#92;,(&#92;delta_{&#92;mathsf{List}}&#92;,&#92;mathit{mbs}) &#92;end{array} ' class='latex' />
</p></blockquote>
<p> But we&#8217;ll use a third definition: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Cdelta_%7B%5Cmathsf%7BList%7D%7D%5C%2C%5B%5C%2C%5D+%26%3D%26+%5Cmathit%7Breturn%7D%5C%2C%5B%5C%2C%5D+%5C%5C+%5Cdelta_%7B%5Cmathsf%7BList%7D%7D%5C%2C%28%5Cmathit%7Bmb%7D+%3A+%5Cmathit%7Bmbs%7D%29+%26%3D%26+%5Cmathsf%7BM%7D%28%3A%29%5C%2C%28%5Cmathit%7Bcp%7D%5C%2C%28%5Cmathit%7Bmb%7D%2C+%5Cdelta_%7B%5Cmathsf%7BList%7D%7D%5C%2C%5Cmathit%7Bmbs%7D%29%29+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;delta_{&#92;mathsf{List}}&#92;,[&#92;,] &amp;=&amp; &#92;mathit{return}&#92;,[&#92;,] &#92;&#92; &#92;delta_{&#92;mathsf{List}}&#92;,(&#92;mathit{mb} : &#92;mathit{mbs}) &amp;=&amp; &#92;mathsf{M}(:)&#92;,(&#92;mathit{cp}&#92;,(&#92;mathit{mb}, &#92;delta_{&#92;mathsf{List}}&#92;,&#92;mathit{mbs})) &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;delta_{&#92;mathsf{List}}&#92;,[&#92;,] &amp;=&amp; &#92;mathit{return}&#92;,[&#92;,] &#92;&#92; &#92;delta_{&#92;mathsf{List}}&#92;,(&#92;mathit{mb} : &#92;mathit{mbs}) &amp;=&amp; &#92;mathsf{M}(:)&#92;,(&#92;mathit{cp}&#92;,(&#92;mathit{mb}, &#92;delta_{&#92;mathsf{List}}&#92;,&#92;mathit{mbs})) &#92;end{array} ' class='latex' />
</p></blockquote>
<p> where </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Cmathit%7Bcp%7D+%26%3A%3A%26+%5Cmathsf%7BM%7D%5C%2C%5Calpha+%5Ctimes+%5Cmathsf%7BM%7D%5C%2C%5Cbeta+%5Crightarrow+%5Cmathsf%7BM%7D%28%5Calpha%5Ctimes%5Cbeta%29+%5C%5C+%5Cmathit%7Bcp%7D%5C%2C%28x%2Cy%29+%26%3D%26+%5Cmathbf%7Bdo%7D%5C%2C%5C%7B%5C%2Ca+%5Cleftarrow+x+%5Cmathbin%7B%3B%7D+b+%5Cleftarrow+y+%5Cmathbin%7B%3B%7D+%5Cmathit%7Breturn%7D%5C%2C%28a%2Cb%29+%5C%7D+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{cp} &amp;::&amp; &#92;mathsf{M}&#92;,&#92;alpha &#92;times &#92;mathsf{M}&#92;,&#92;beta &#92;rightarrow &#92;mathsf{M}(&#92;alpha&#92;times&#92;beta) &#92;&#92; &#92;mathit{cp}&#92;,(x,y) &amp;=&amp; &#92;mathbf{do}&#92;,&#92;{&#92;,a &#92;leftarrow x &#92;mathbin{;} b &#92;leftarrow y &#92;mathbin{;} &#92;mathit{return}&#92;,(a,b) &#92;} &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{cp} &amp;::&amp; &#92;mathsf{M}&#92;,&#92;alpha &#92;times &#92;mathsf{M}&#92;,&#92;beta &#92;rightarrow &#92;mathsf{M}(&#92;alpha&#92;times&#92;beta) &#92;&#92; &#92;mathit{cp}&#92;,(x,y) &amp;=&amp; &#92;mathbf{do}&#92;,&#92;{&#92;,a &#92;leftarrow x &#92;mathbin{;} b &#92;leftarrow y &#92;mathbin{;} &#92;mathit{return}&#92;,(a,b) &#92;} &#92;end{array} ' class='latex' />
</p></blockquote>
<p> That is, </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cdelta_%7B%5Cmathsf%7BList%7D%7D+%3D+%5Cmathit%7Bfoldr%7D%5C%2C%28%5Cmathsf%7BM%7D%28%3A%29%5Ccdot%5Cmathit%7Bcp%7D%29%5C%2C%28%5Cmathit%7Breturn%7D%5C%2C%5B%5C%2C%5D%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;delta_{&#92;mathsf{List}} = &#92;mathit{foldr}&#92;,(&#92;mathsf{M}(:)&#92;cdot&#92;mathit{cp})&#92;,(&#92;mathit{return}&#92;,[&#92;,]) ' title='&#92;displaystyle  &#92;delta_{&#92;mathsf{List}} = &#92;mathit{foldr}&#92;,(&#92;mathsf{M}(:)&#92;cdot&#92;mathit{cp})&#92;,(&#92;mathit{return}&#92;,[&#92;,]) ' class='latex' />
</p></blockquote>
<p> Now, for the base case we have </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Coplus%2F%5C%2C%28%5Cmathsf%7BM%7D%28%5Cmathit%7Bfoldr%7D%5C%2C%28%5Cotimes%29%5C%2Cb%29%5C%2C%28%5Cmathit%7Breturn%7D%5C%2C%5B%5C%2C%5D%29%29+%3D+%5Coplus%2F%5C%2C%28%5Cmathit%7Breturn%7D%5C%2C%28%5Cmathit%7Bfoldr%7D%5C%2C%28%5Cotimes%29%5C%2Cb%5C%2C%5B%5C%2C%5D%29%29+%3D+%5Coplus%2F%5C%2C%28%5Cmathit%7Breturn%7D%5C%2Cb%29+%3D+b+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;oplus/&#92;,(&#92;mathsf{M}(&#92;mathit{foldr}&#92;,(&#92;otimes)&#92;,b)&#92;,(&#92;mathit{return}&#92;,[&#92;,])) = &#92;oplus/&#92;,(&#92;mathit{return}&#92;,(&#92;mathit{foldr}&#92;,(&#92;otimes)&#92;,b&#92;,[&#92;,])) = &#92;oplus/&#92;,(&#92;mathit{return}&#92;,b) = b ' title='&#92;displaystyle  &#92;oplus/&#92;,(&#92;mathsf{M}(&#92;mathit{foldr}&#92;,(&#92;otimes)&#92;,b)&#92;,(&#92;mathit{return}&#92;,[&#92;,])) = &#92;oplus/&#92;,(&#92;mathit{return}&#92;,(&#92;mathit{foldr}&#92;,(&#92;otimes)&#92;,b&#92;,[&#92;,])) = &#92;oplus/&#92;,(&#92;mathit{return}&#92;,b) = b ' class='latex' />
</p></blockquote>
<p> as required. For the inductive step, we have: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Bll%7D+%26+%5Coplus%2F+%5Ccdot+%5Cmathsf%7BM%7D%28%5Cmathit%7Bfoldr%7D%5C%2C%28%5Cotimes%29%5C%2Cb%29+%5Ccdot+%5Cmathsf%7BM%7D%28%3A%29+%5Ccdot+%5Cmathit%7Bcp%7D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bfunctors%7D+%5C%7D+%5C%5C+%26+%5Coplus%2F+%5Ccdot+%5Cmathsf%7BM%7D%28%5Cmathit%7Bfoldr%7D%5C%2C%28%5Cotimes%29%5C%2Cb+%5Ccdot+%28%3A%29%29+%5Ccdot+%5Cmathit%7Bcp%7D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bevaluation+for+%5C%28%5Cmathit%7Bfoldr%7D%5C%29%7D+%5C%7D+%5C%5C+%26+%5Coplus%2F+%5Ccdot+%5Cmathsf%7BM%7D%28%28%5Cotimes%29+%5Ccdot+%5Cmathit%7Bid%7D%5Ctimes%5Cmathit%7Bfoldr%7D%5C%2C%28%5Cotimes%29%5C%2Cb%29+%5Ccdot+%5Cmathit%7Bcp%7D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bfunctors%3B+naturality+of+%5C%28%5Cmathit%7Bcp%7D%5C%29%7D+%5C%7D+%5C%5C+%26+%5Coplus%2F+%5Ccdot+%5Cmathsf%7BM%7D%28%5Cotimes%29+%5Ccdot+%5Cmathit%7Bcp%7D+%5Ccdot+%5Cmathsf%7BM%7D%5Cmathit%7Bid%7D%5Ctimes%5Cmathsf%7BM%7D%28%5Cmathit%7Bfoldr%7D%5C%2C%28%5Cotimes%29%5C%2Cb%29+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bdistributivity+for+%5C%28%5Cmathit%7Bcp%7D%5C%29%3A+see+below%7D+%5C%7D+%5C%5C+%26+%28%5Cotimes%29+%5Ccdot+%28%5Coplus%2F%29%5Ctimes%28%5Coplus%2F%29+%5Ccdot+%5Cmathsf%7BM%7D%5Cmathit%7Bid%7D%5Ctimes%5Cmathsf%7BM%7D%28%5Cmathit%7Bfoldr%7D%5C%2C%28%5Cotimes%29%5C%2Cb%29+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bfunctors%7D+%5C%7D+%5C%5C+%26+%28%5Cotimes%29+%5Ccdot+%28%5Coplus%2F%29%5Ctimes%5Cmathit%7Bid%7D+%5Ccdot+%5Cmathit%7Bid%7D%5Ctimes%5Cmathsf%7BM%7D%28%5Coplus%2F%5Ccdot%5Cmathit%7Bfoldr%7D%5C%2C%28%5Cotimes%29%5C%2Cb%29+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;oplus/ &#92;cdot &#92;mathsf{M}(&#92;mathit{foldr}&#92;,(&#92;otimes)&#92;,b) &#92;cdot &#92;mathsf{M}(:) &#92;cdot &#92;mathit{cp} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{functors} &#92;} &#92;&#92; &amp; &#92;oplus/ &#92;cdot &#92;mathsf{M}(&#92;mathit{foldr}&#92;,(&#92;otimes)&#92;,b &#92;cdot (:)) &#92;cdot &#92;mathit{cp} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{evaluation for &#92;(&#92;mathit{foldr}&#92;)} &#92;} &#92;&#92; &amp; &#92;oplus/ &#92;cdot &#92;mathsf{M}((&#92;otimes) &#92;cdot &#92;mathit{id}&#92;times&#92;mathit{foldr}&#92;,(&#92;otimes)&#92;,b) &#92;cdot &#92;mathit{cp} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{functors; naturality of &#92;(&#92;mathit{cp}&#92;)} &#92;} &#92;&#92; &amp; &#92;oplus/ &#92;cdot &#92;mathsf{M}(&#92;otimes) &#92;cdot &#92;mathit{cp} &#92;cdot &#92;mathsf{M}&#92;mathit{id}&#92;times&#92;mathsf{M}(&#92;mathit{foldr}&#92;,(&#92;otimes)&#92;,b) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{distributivity for &#92;(&#92;mathit{cp}&#92;): see below} &#92;} &#92;&#92; &amp; (&#92;otimes) &#92;cdot (&#92;oplus/)&#92;times(&#92;oplus/) &#92;cdot &#92;mathsf{M}&#92;mathit{id}&#92;times&#92;mathsf{M}(&#92;mathit{foldr}&#92;,(&#92;otimes)&#92;,b) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{functors} &#92;} &#92;&#92; &amp; (&#92;otimes) &#92;cdot (&#92;oplus/)&#92;times&#92;mathit{id} &#92;cdot &#92;mathit{id}&#92;times&#92;mathsf{M}(&#92;oplus/&#92;cdot&#92;mathit{foldr}&#92;,(&#92;otimes)&#92;,b) &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;oplus/ &#92;cdot &#92;mathsf{M}(&#92;mathit{foldr}&#92;,(&#92;otimes)&#92;,b) &#92;cdot &#92;mathsf{M}(:) &#92;cdot &#92;mathit{cp} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{functors} &#92;} &#92;&#92; &amp; &#92;oplus/ &#92;cdot &#92;mathsf{M}(&#92;mathit{foldr}&#92;,(&#92;otimes)&#92;,b &#92;cdot (:)) &#92;cdot &#92;mathit{cp} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{evaluation for &#92;(&#92;mathit{foldr}&#92;)} &#92;} &#92;&#92; &amp; &#92;oplus/ &#92;cdot &#92;mathsf{M}((&#92;otimes) &#92;cdot &#92;mathit{id}&#92;times&#92;mathit{foldr}&#92;,(&#92;otimes)&#92;,b) &#92;cdot &#92;mathit{cp} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{functors; naturality of &#92;(&#92;mathit{cp}&#92;)} &#92;} &#92;&#92; &amp; &#92;oplus/ &#92;cdot &#92;mathsf{M}(&#92;otimes) &#92;cdot &#92;mathit{cp} &#92;cdot &#92;mathsf{M}&#92;mathit{id}&#92;times&#92;mathsf{M}(&#92;mathit{foldr}&#92;,(&#92;otimes)&#92;,b) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{distributivity for &#92;(&#92;mathit{cp}&#92;): see below} &#92;} &#92;&#92; &amp; (&#92;otimes) &#92;cdot (&#92;oplus/)&#92;times(&#92;oplus/) &#92;cdot &#92;mathsf{M}&#92;mathit{id}&#92;times&#92;mathsf{M}(&#92;mathit{foldr}&#92;,(&#92;otimes)&#92;,b) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{functors} &#92;} &#92;&#92; &amp; (&#92;otimes) &#92;cdot (&#92;oplus/)&#92;times&#92;mathit{id} &#92;cdot &#92;mathit{id}&#92;times&#92;mathsf{M}(&#92;oplus/&#92;cdot&#92;mathit{foldr}&#92;,(&#92;otimes)&#92;,b) &#92;end{array} ' class='latex' />
</p></blockquote>
<p> which completes the fusion proof, modulo the wish about distributivity for <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bcp%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{cp}}' title='{&#92;mathit{cp}}' class='latex' />: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Coplus%2F+%5Ccdot+%5Cmathsf%7BM%7D%28%5Cotimes%29+%5Ccdot+%5Cmathit%7Bcp%7D+%3D+%28%5Cotimes%29+%5Ccdot+%28%5Coplus%2F%29%5Ctimes%28%5Coplus%2F%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;oplus/ &#92;cdot &#92;mathsf{M}(&#92;otimes) &#92;cdot &#92;mathit{cp} = (&#92;otimes) &#92;cdot (&#92;oplus/)&#92;times(&#92;oplus/) ' title='&#92;displaystyle  &#92;oplus/ &#92;cdot &#92;mathsf{M}(&#92;otimes) &#92;cdot &#92;mathit{cp} = (&#92;otimes) &#92;cdot (&#92;oplus/)&#92;times(&#92;oplus/) ' class='latex' />
</p></blockquote>
<p><h2> Distributivity for cartesian product </h2>
<p>
As for that wish about distributivity for <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bcp%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{cp}}' title='{&#92;mathit{cp}}' class='latex' />: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Bll%7D+%26+%5Coplus%2F+%5Cmathbin%7B%5Chbox%7B%5Cfootnotesize%5C%24%7D%7D+%5Cmathsf%7BM%7D%28%5Cotimes%29+%5Cmathbin%7B%5Chbox%7B%5Cfootnotesize%5C%24%7D%7D+%5Cmathit%7Bcp%7D%5C%2C%28x%2Cy%29+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bdefinition+of+%5C%28%5Cmathit%7Bcp%7D%5C%29%7D+%5C%7D+%5C%5C+%26+%5Coplus%2F+%5Cmathbin%7B%5Chbox%7B%5Cfootnotesize%5C%24%7D%7D+%5Cmathsf%7BM%7D%28%5Cotimes%29+%5Cmathbin%7B%5Chbox%7B%5Cfootnotesize%5C%24%7D%7D+%5Cmathbf%7Bdo%7D%5C%2C%5C%7B%5C%2Ca+%5Cleftarrow+x+%5Cmathbin%7B%3B%7D+b+%5Cleftarrow+y+%5Cmathbin%7B%3B%7D+%5Cmathit%7Breturn%7D%5C%2C%28a%2Cb%29+%5C%2C%5C%7D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bmap+over+%5C%28%5Cmathbf%7Bdo%7D%5C%29%7D+%5C%7D+%5C%5C+%26+%5Coplus%2F+%5Cmathbin%7B%5Chbox%7B%5Cfootnotesize%5C%24%7D%7D+%5Cmathbf%7Bdo%7D%5C%2C%5C%7B%5C%2Ca+%5Cleftarrow+x+%5Cmathbin%7B%3B%7D+b+%5Cleftarrow+y+%5Cmathbin%7B%3B%7D+%5Cmathit%7Breturn%7D%5C%2C%28a+%5Cotimes+b%29+%5C%2C%5C%7D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bexpanding+%5C%28%5Cmathbf%7Bdo%7D%5C%29%7D+%5C%7D+%5C%5C+%26+%5Coplus%2F+%5Cmathbin%7B%5Chbox%7B%5Cfootnotesize%5C%24%7D%7D+%5Cmathit%7Bjoin%7D+%5Cmathbin%7B%5Chbox%7B%5Cfootnotesize%5C%24%7D%7D+%5Cmathsf%7BM%7D%5C%2C%28%5Clambda+a+%5Cmathbin%7B.%7D+%5Cmathsf%7BM%7D%5C%2C%28a%5Cotimes%29%5C%2Cy%29%5C%2Cx+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7B%5C%28%5Coplus%2F%5C%29+is+an+%5C%28%5Cmathsf%7BM%7D%5C%29-algebra%7D+%5C%7D+%5C%5C+%26+%5Coplus%2F+%5Cmathbin%7B%5Chbox%7B%5Cfootnotesize%5C%24%7D%7D+%5Cmathsf%7BM%7D%28%5Coplus%2F%29+%5Cmathbin%7B%5Chbox%7B%5Cfootnotesize%5C%24%7D%7D+%5Cmathsf%7BM%7D%5C%2C%28%5Clambda+a+%5Cmathbin%7B.%7D+%5Cmathsf%7BM%7D%5C%2C%28a%5Cotimes%29%5C%2Cy%29%5C%2Cx+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bfunctors%7D+%5C%7D+%5C%5C+%26+%5Coplus%2F+%5Cmathbin%7B%5Chbox%7B%5Cfootnotesize%5C%24%7D%7D+%5Cmathsf%7BM%7D%5C%2C%28%5Clambda+a+%5Cmathbin%7B.%7D+%5Coplus%2F%28%5Cmathsf%7BM%7D%5C%2C%28a%5Cotimes%29%5C%2Cy%29%29%5C%2Cx+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bdistributivity+for+collections%3A+see+below%7D+%5C%7D+%5C%5C+%26+%5Coplus%2F+%5Cmathbin%7B%5Chbox%7B%5Cfootnotesize%5C%24%7D%7D+%5Cmathsf%7BM%7D%5C%2C%28%5Clambda+a+%5Cmathbin%7B.%7D+a+%5Cotimes+%28%5Coplus%2F%5C%2Cy%29%29%5C%2Cx+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bsectioning%7D+%5C%7D+%5C%5C+%26+%5Coplus%2F+%5Cmathbin%7B%5Chbox%7B%5Cfootnotesize%5C%24%7D%7D+%5Cmathsf%7BM%7D%5C%2C%28%5Cotimes+%28%5Coplus%2F%5C%2Cy%29%29%5C%2Cx+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bdistributivity+for+collections+again%7D+%5C%7D+%5C%5C+%26+%28%5Cotimes+%28%5Coplus%2F%5C%2Cy%29%29%5C%2C%28%5Coplus%2F%5C%2Cx%29+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bsectioning%7D+%5C%7D+%5C%5C+%26+%28%5Coplus%2F%5C%2Cx%29+%5Cotimes+%28%5Coplus%2F%5C%2Cy%29+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Beta-expansion%7D+%5C%7D+%5C%5C+%26+%28%5Cotimes%29+%5Cmathbin%7B%5Chbox%7B%5Cfootnotesize%5C%24%7D%7D+%28%5Coplus%2F+%5Ctimes+%5Coplus%2F%29+%5Cmathbin%7B%5Chbox%7B%5Cfootnotesize%5C%24%7D%7D+%28x%2Cy%29+%5C%5C+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;oplus/ &#92;mathbin{&#92;hbox{&#92;footnotesize&#92;$}} &#92;mathsf{M}(&#92;otimes) &#92;mathbin{&#92;hbox{&#92;footnotesize&#92;$}} &#92;mathit{cp}&#92;,(x,y) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{definition of &#92;(&#92;mathit{cp}&#92;)} &#92;} &#92;&#92; &amp; &#92;oplus/ &#92;mathbin{&#92;hbox{&#92;footnotesize&#92;$}} &#92;mathsf{M}(&#92;otimes) &#92;mathbin{&#92;hbox{&#92;footnotesize&#92;$}} &#92;mathbf{do}&#92;,&#92;{&#92;,a &#92;leftarrow x &#92;mathbin{;} b &#92;leftarrow y &#92;mathbin{;} &#92;mathit{return}&#92;,(a,b) &#92;,&#92;} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{map over &#92;(&#92;mathbf{do}&#92;)} &#92;} &#92;&#92; &amp; &#92;oplus/ &#92;mathbin{&#92;hbox{&#92;footnotesize&#92;$}} &#92;mathbf{do}&#92;,&#92;{&#92;,a &#92;leftarrow x &#92;mathbin{;} b &#92;leftarrow y &#92;mathbin{;} &#92;mathit{return}&#92;,(a &#92;otimes b) &#92;,&#92;} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{expanding &#92;(&#92;mathbf{do}&#92;)} &#92;} &#92;&#92; &amp; &#92;oplus/ &#92;mathbin{&#92;hbox{&#92;footnotesize&#92;$}} &#92;mathit{join} &#92;mathbin{&#92;hbox{&#92;footnotesize&#92;$}} &#92;mathsf{M}&#92;,(&#92;lambda a &#92;mathbin{.} &#92;mathsf{M}&#92;,(a&#92;otimes)&#92;,y)&#92;,x &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{&#92;(&#92;oplus/&#92;) is an &#92;(&#92;mathsf{M}&#92;)-algebra} &#92;} &#92;&#92; &amp; &#92;oplus/ &#92;mathbin{&#92;hbox{&#92;footnotesize&#92;$}} &#92;mathsf{M}(&#92;oplus/) &#92;mathbin{&#92;hbox{&#92;footnotesize&#92;$}} &#92;mathsf{M}&#92;,(&#92;lambda a &#92;mathbin{.} &#92;mathsf{M}&#92;,(a&#92;otimes)&#92;,y)&#92;,x &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{functors} &#92;} &#92;&#92; &amp; &#92;oplus/ &#92;mathbin{&#92;hbox{&#92;footnotesize&#92;$}} &#92;mathsf{M}&#92;,(&#92;lambda a &#92;mathbin{.} &#92;oplus/(&#92;mathsf{M}&#92;,(a&#92;otimes)&#92;,y))&#92;,x &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{distributivity for collections: see below} &#92;} &#92;&#92; &amp; &#92;oplus/ &#92;mathbin{&#92;hbox{&#92;footnotesize&#92;$}} &#92;mathsf{M}&#92;,(&#92;lambda a &#92;mathbin{.} a &#92;otimes (&#92;oplus/&#92;,y))&#92;,x &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{sectioning} &#92;} &#92;&#92; &amp; &#92;oplus/ &#92;mathbin{&#92;hbox{&#92;footnotesize&#92;$}} &#92;mathsf{M}&#92;,(&#92;otimes (&#92;oplus/&#92;,y))&#92;,x &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{distributivity for collections again} &#92;} &#92;&#92; &amp; (&#92;otimes (&#92;oplus/&#92;,y))&#92;,(&#92;oplus/&#92;,x) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{sectioning} &#92;} &#92;&#92; &amp; (&#92;oplus/&#92;,x) &#92;otimes (&#92;oplus/&#92;,y) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{eta-expansion} &#92;} &#92;&#92; &amp; (&#92;otimes) &#92;mathbin{&#92;hbox{&#92;footnotesize&#92;$}} (&#92;oplus/ &#92;times &#92;oplus/) &#92;mathbin{&#92;hbox{&#92;footnotesize&#92;$}} (x,y) &#92;&#92; &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;oplus/ &#92;mathbin{&#92;hbox{&#92;footnotesize&#92;$}} &#92;mathsf{M}(&#92;otimes) &#92;mathbin{&#92;hbox{&#92;footnotesize&#92;$}} &#92;mathit{cp}&#92;,(x,y) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{definition of &#92;(&#92;mathit{cp}&#92;)} &#92;} &#92;&#92; &amp; &#92;oplus/ &#92;mathbin{&#92;hbox{&#92;footnotesize&#92;$}} &#92;mathsf{M}(&#92;otimes) &#92;mathbin{&#92;hbox{&#92;footnotesize&#92;$}} &#92;mathbf{do}&#92;,&#92;{&#92;,a &#92;leftarrow x &#92;mathbin{;} b &#92;leftarrow y &#92;mathbin{;} &#92;mathit{return}&#92;,(a,b) &#92;,&#92;} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{map over &#92;(&#92;mathbf{do}&#92;)} &#92;} &#92;&#92; &amp; &#92;oplus/ &#92;mathbin{&#92;hbox{&#92;footnotesize&#92;$}} &#92;mathbf{do}&#92;,&#92;{&#92;,a &#92;leftarrow x &#92;mathbin{;} b &#92;leftarrow y &#92;mathbin{;} &#92;mathit{return}&#92;,(a &#92;otimes b) &#92;,&#92;} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{expanding &#92;(&#92;mathbf{do}&#92;)} &#92;} &#92;&#92; &amp; &#92;oplus/ &#92;mathbin{&#92;hbox{&#92;footnotesize&#92;$}} &#92;mathit{join} &#92;mathbin{&#92;hbox{&#92;footnotesize&#92;$}} &#92;mathsf{M}&#92;,(&#92;lambda a &#92;mathbin{.} &#92;mathsf{M}&#92;,(a&#92;otimes)&#92;,y)&#92;,x &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{&#92;(&#92;oplus/&#92;) is an &#92;(&#92;mathsf{M}&#92;)-algebra} &#92;} &#92;&#92; &amp; &#92;oplus/ &#92;mathbin{&#92;hbox{&#92;footnotesize&#92;$}} &#92;mathsf{M}(&#92;oplus/) &#92;mathbin{&#92;hbox{&#92;footnotesize&#92;$}} &#92;mathsf{M}&#92;,(&#92;lambda a &#92;mathbin{.} &#92;mathsf{M}&#92;,(a&#92;otimes)&#92;,y)&#92;,x &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{functors} &#92;} &#92;&#92; &amp; &#92;oplus/ &#92;mathbin{&#92;hbox{&#92;footnotesize&#92;$}} &#92;mathsf{M}&#92;,(&#92;lambda a &#92;mathbin{.} &#92;oplus/(&#92;mathsf{M}&#92;,(a&#92;otimes)&#92;,y))&#92;,x &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{distributivity for collections: see below} &#92;} &#92;&#92; &amp; &#92;oplus/ &#92;mathbin{&#92;hbox{&#92;footnotesize&#92;$}} &#92;mathsf{M}&#92;,(&#92;lambda a &#92;mathbin{.} a &#92;otimes (&#92;oplus/&#92;,y))&#92;,x &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{sectioning} &#92;} &#92;&#92; &amp; &#92;oplus/ &#92;mathbin{&#92;hbox{&#92;footnotesize&#92;$}} &#92;mathsf{M}&#92;,(&#92;otimes (&#92;oplus/&#92;,y))&#92;,x &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{distributivity for collections again} &#92;} &#92;&#92; &amp; (&#92;otimes (&#92;oplus/&#92;,y))&#92;,(&#92;oplus/&#92;,x) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{sectioning} &#92;} &#92;&#92; &amp; (&#92;oplus/&#92;,x) &#92;otimes (&#92;oplus/&#92;,y) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{eta-expansion} &#92;} &#92;&#92; &amp; (&#92;otimes) &#92;mathbin{&#92;hbox{&#92;footnotesize&#92;$}} (&#92;oplus/ &#92;times &#92;oplus/) &#92;mathbin{&#92;hbox{&#92;footnotesize&#92;$}} (x,y) &#92;&#92; &#92;end{array} ' class='latex' />
</p></blockquote>
<p> which discharges the proof obligation about distributivity for cartesian product, but again modulo two symmetric wishes about distributivity for collections: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Coplus%2F+%5Ccdot+%5Cmathsf%7BM%7D%28a%5Cotimes%29+%26%3D%26+%28a%5Cotimes%29+%5Ccdot+%5Coplus%2F+%5C%5C+%5Coplus%2F+%5Ccdot+%5Cmathsf%7BM%7D%28%5Cotimes+b%29+%26%3D%26+%28%5Cotimes+b%29+%5Ccdot+%5Coplus%2F+%5C%5C+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;oplus/ &#92;cdot &#92;mathsf{M}(a&#92;otimes) &amp;=&amp; (a&#92;otimes) &#92;cdot &#92;oplus/ &#92;&#92; &#92;oplus/ &#92;cdot &#92;mathsf{M}(&#92;otimes b) &amp;=&amp; (&#92;otimes b) &#92;cdot &#92;oplus/ &#92;&#92; &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;oplus/ &#92;cdot &#92;mathsf{M}(a&#92;otimes) &amp;=&amp; (a&#92;otimes) &#92;cdot &#92;oplus/ &#92;&#92; &#92;oplus/ &#92;cdot &#92;mathsf{M}(&#92;otimes b) &amp;=&amp; (&#92;otimes b) &#92;cdot &#92;oplus/ &#92;&#92; &#92;end{array} ' class='latex' />
</p></blockquote>
<p><h2> Distributivity for collections </h2>
<p>
Finally, the proof obligations about distributivity for collections are easily discharged, by induction over the size of the (finite!) collection, provided that the binary operator <img src='http://s0.wp.com/latex.php?latex=%7B%5Cotimes%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;otimes}' title='{&#92;otimes}' class='latex' /> distributes over <img src='http://s0.wp.com/latex.php?latex=%7B%5Coplus%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;oplus}' title='{&#92;oplus}' class='latex' /> in the familiar sense. The base case is for a singleton collection, ie in the image of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Breturn%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{return}}' title='{&#92;mathit{return}}' class='latex' /> (because we disallowed empty collections); this case follows from the fact that <img src='http://s0.wp.com/latex.php?latex=%7B%5Coplus%2F%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;oplus/}' title='{&#92;oplus/}' class='latex' /> is an <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BM%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{M}}' title='{&#92;mathsf{M}}' class='latex' />-algebra. The inductive step is for a collection of the form <img src='http://s0.wp.com/latex.php?latex=%7Bu+%5Cmathbin%7B%5Cunderline%7B%5Csmash%7B%5Cmathit%7Bmplus%7D%7D%7D%7D+v%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{u &#92;mathbin{&#92;underline{&#92;smash{&#92;mathit{mplus}}}} v}' title='{u &#92;mathbin{&#92;underline{&#92;smash{&#92;mathit{mplus}}}} v}' class='latex' /> with <img src='http://s0.wp.com/latex.php?latex=%7Bu%2Cv%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{u,v}' title='{u,v}' class='latex' /> both strictly smaller than the whole (so, if the monad is idempotent, disjoint, or at least not nested); this requires the distribution of the algebra over choice <img src='http://s0.wp.com/latex.php?latex=%7B%5Coplus+%2F+%28u+%5Cmathbin%7B%5Cunderline%7B%5Csmash%7B%5Cmathit%7Bmplus%7D%7D%7D%7D+v%29+%3D+%28%5Coplus%2Fu%29+%5Coplus+%28%5Coplus%2Fv%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;oplus / (u &#92;mathbin{&#92;underline{&#92;smash{&#92;mathit{mplus}}}} v) = (&#92;oplus/u) &#92;oplus (&#92;oplus/v)}' title='{&#92;oplus / (u &#92;mathbin{&#92;underline{&#92;smash{&#92;mathit{mplus}}}} v) = (&#92;oplus/u) &#92;oplus (&#92;oplus/v)}' class='latex' />, together with the familiar distribution of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cotimes%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;otimes}' title='{&#92;otimes}' class='latex' /> over <img src='http://s0.wp.com/latex.php?latex=%7B%5Coplus%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;oplus}' title='{&#92;oplus}' class='latex' />.</p>
<p><h2> Summary </h2>
<p>
So, the datatype-generic distributivity for <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />-structures of collections that we used for the Maximum Segment Sum problem reduced to distributivity for lists of collections, which reduced to the cartesian product of collections, which reduced to that for pairs. That&#8217;s a much deeper hierarchy than I was expecting; can it be streamlined?</p>
<p>
<br />  <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=patternsinfp.wordpress.com&#038;blog=15593982&#038;post=205&#038;subd=patternsinfp&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://patternsinfp.wordpress.com/2011/05/17/distributivity-in-horners-rule/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/39d7e0397cba13d1494d9a48c1f42167?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jeremygibbons</media:title>
		</media:content>

		<media:content url="http://patternsinfp.files.wordpress.com/2011/05/horner-distributivity.png" medium="image" />

		<media:content url="http://patternsinfp.files.wordpress.com/2011/05/horner2-generic.png" medium="image" />

		<media:content url="http://patternsinfp.files.wordpress.com/2011/05/horner2-lists.png" medium="image" />
	</item>
		<item>
		<title>Horner&#8217;s Rule</title>
		<link>http://patternsinfp.wordpress.com/2011/05/05/horners-rule/</link>
		<comments>http://patternsinfp.wordpress.com/2011/05/05/horners-rule/#comments</comments>
		<pubDate>Thu, 05 May 2011 11:29:56 +0000</pubDate>
		<dc:creator>jeremygibbons</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://patternsinfp.wordpress.com/?p=197</guid>
		<description><![CDATA[This post is about my all-time favourite calculation, of a linear-time algorithm for the maximum segment sum problem, based on Horner&#8217;s Rule. The problem was popularized in Jon Bentley&#8217;s Programming Pearls series in CACM (and in the subsequent book), but &#8230; <a href="http://patternsinfp.wordpress.com/2011/05/05/horners-rule/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=patternsinfp.wordpress.com&#038;blog=15593982&#038;post=197&#038;subd=patternsinfp&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>
 This post is about my all-time favourite calculation, of a linear-time algorithm for the <em>maximum segment sum</em> problem, based on <em>Horner&#8217;s Rule</em>. The problem was popularized in Jon Bentley&#8217;s <a href="http://dx.doi.org/10.1145/358234.381162">Programming Pearls</a> series in CACM (and in the subsequent book), but I learnt about it from Richard Bird&#8217;s lecture notes on <a href="http://web.comlab.ox.ac.uk/publications/publication3837-abstract.html">The Theory of Lists</a> and <a href="http://web.comlab.ox.ac.uk/publications/publication3849-abstract.html">Constructive Functional Programming</a> and his paper <a href="http://dx.doi.org/10.1093/comjnl/32.2.122">Algebraic Identities for Program Calculation</a>, which he was working on around the time I started my DPhil. It seems like I&#8217;m not the only one for whom the problem is a favourite, because it has since become a bit of a clich&eacute; among program calculators; but that won&#8217;t stop me writing about it again.</p>
<p><h2> Maximum segment sum </h2>
<p>
The original problem is as follows. Given a list of numbers (say, a possibly empty list of integers), find the largest of the sums of the contiguous segments of that list. In Haskell, this specification could be written like so: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Cmathit%7Bmss%7D+%26%3D%26+%5Cmathit%7Bmaximum%7D+%5Ccdot+%5Cmathit%7Bmap%7D%5C%2C%5Cmathit%7Bsum%7D+%5Ccdot+%5Cmathit%7Bsegs%7D+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{mss} &amp;=&amp; &#92;mathit{maximum} &#92;cdot &#92;mathit{map}&#92;,&#92;mathit{sum} &#92;cdot &#92;mathit{segs} &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{mss} &amp;=&amp; &#92;mathit{maximum} &#92;cdot &#92;mathit{map}&#92;,&#92;mathit{sum} &#92;cdot &#92;mathit{segs} &#92;end{array} ' class='latex' />
</p></blockquote>
<p> where <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bsegs%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{segs}}' title='{&#92;mathit{segs}}' class='latex' /> computes the contiguous segments of a list: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Cmathit%7Bsegs%7D+%26%3D%26+%5Cmathit%7Bconcat%7D+%5Ccdot+%5Cmathit%7Bmap%7D%5C%2C%5Cmathit%7Binits%7D+%5Ccdot+%5Cmathit%7Btails%7D+%5C%5C+%5Cmathit%7Btails%7D+%26%3D%26+%5Cmathit%7Bfoldr%7D%5C%2Cf%5C%2C%5B%5B%5C%2C%5D%5D+%5Cquad%5Cmathbf%7Bwhere%7D%5C%3B+f%5C%2C%5Cmathit%7Bx%7D%5C%2C%5Cmathit%7Bxss%7D+%3D+%28%5Cmathit%7Bx%7D%3A%5Cmathit%7Bhead%7D%5C%2C%5Cmathit%7Bxss%7D%29%3A%5Cmathit%7Bxss%7D+%5C%5C+%5Cmathit%7Binits%7D+%26%3D%26+%5Cmathit%7Bfoldr%7D%5C%2Cg%5C%2C%5B%5B%5C%2C%5D%5D+%5Cquad%5Cmathbf%7Bwhere%7D%5C%3B+g%5C%2C%5Cmathit%7Bx%7D%5C%2C%5Cmathit%7Bxss%7D+%3D+%5B%5C%2C%5D+%3A+%5Cmathit%7Bmap%7D%5C%2C%28%5Cmathit%7Bx%7D%3A%29%5C%2C%5Cmathit%7Bxss%7D+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{segs} &amp;=&amp; &#92;mathit{concat} &#92;cdot &#92;mathit{map}&#92;,&#92;mathit{inits} &#92;cdot &#92;mathit{tails} &#92;&#92; &#92;mathit{tails} &amp;=&amp; &#92;mathit{foldr}&#92;,f&#92;,[[&#92;,]] &#92;quad&#92;mathbf{where}&#92;; f&#92;,&#92;mathit{x}&#92;,&#92;mathit{xss} = (&#92;mathit{x}:&#92;mathit{head}&#92;,&#92;mathit{xss}):&#92;mathit{xss} &#92;&#92; &#92;mathit{inits} &amp;=&amp; &#92;mathit{foldr}&#92;,g&#92;,[[&#92;,]] &#92;quad&#92;mathbf{where}&#92;; g&#92;,&#92;mathit{x}&#92;,&#92;mathit{xss} = [&#92;,] : &#92;mathit{map}&#92;,(&#92;mathit{x}:)&#92;,&#92;mathit{xss} &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{segs} &amp;=&amp; &#92;mathit{concat} &#92;cdot &#92;mathit{map}&#92;,&#92;mathit{inits} &#92;cdot &#92;mathit{tails} &#92;&#92; &#92;mathit{tails} &amp;=&amp; &#92;mathit{foldr}&#92;,f&#92;,[[&#92;,]] &#92;quad&#92;mathbf{where}&#92;; f&#92;,&#92;mathit{x}&#92;,&#92;mathit{xss} = (&#92;mathit{x}:&#92;mathit{head}&#92;,&#92;mathit{xss}):&#92;mathit{xss} &#92;&#92; &#92;mathit{inits} &amp;=&amp; &#92;mathit{foldr}&#92;,g&#92;,[[&#92;,]] &#92;quad&#92;mathbf{where}&#92;; g&#92;,&#92;mathit{x}&#92;,&#92;mathit{xss} = [&#92;,] : &#92;mathit{map}&#92;,(&#92;mathit{x}:)&#92;,&#92;mathit{xss} &#92;end{array} ' class='latex' />
</p></blockquote>
<p> and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bsum%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{sum}}' title='{&#92;mathit{sum}}' class='latex' /> computes the sum of a list, and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bmaximum%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{maximum}}' title='{&#92;mathit{maximum}}' class='latex' /> the maximum of a nonempty list: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Cmathit%7Bsum%7D+%26%3D%26+%5Cmathit%7Bfoldr%7D%5C%2C%28%2B%29%5C%2C0+%5C%5C+%5Cmathit%7Bmaximum%7D+%26%3D%26+%5Cmathit%7Bfoldr%7D_1%5C%2C%5Cmax+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{sum} &amp;=&amp; &#92;mathit{foldr}&#92;,(+)&#92;,0 &#92;&#92; &#92;mathit{maximum} &amp;=&amp; &#92;mathit{foldr}_1&#92;,&#92;max &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{sum} &amp;=&amp; &#92;mathit{foldr}&#92;,(+)&#92;,0 &#92;&#92; &#92;mathit{maximum} &amp;=&amp; &#92;mathit{foldr}_1&#92;,&#92;max &#92;end{array} ' class='latex' />
</p></blockquote>
<p> This specification is executable, but takes cubic time; the problem is to do better.</p>
<p>
We can get quite a long way just with standard properties of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bmap%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{map}}' title='{&#92;mathit{map}}' class='latex' />, <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Binits%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{inits}}' title='{&#92;mathit{inits}}' class='latex' />, etc: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Bll%7D+%26+%5Cmathit%7Bmss%7D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bdefinition+of+%5C%28%5Cmathit%7Bmss%7D%5C%29%7D+%5C%7D+%5C%5C+%26+%5Cmathit%7Bmaximum%7D+%5Ccdot+%5Cmathit%7Bmap%7D%5C%2C%5Cmathit%7Bsum%7D+%5Ccdot+%5Cmathit%7Bsegs%7D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bdefinition+of+%5C%28%5Cmathit%7Bsegs%7D%5C%29%7D+%5C%7D+%5C%5C+%26+%5Cmathit%7Bmaximum%7D+%5Ccdot+%5Cmathit%7Bmap%7D%5C%2C%5Cmathit%7Bsum%7D+%5Ccdot+%5Cmathit%7Bconcat%7D+%5Ccdot+%5Cmathit%7Bmap%7D%5C%2C%5Cmathit%7Binits%7D+%5Ccdot+%5Cmathit%7Btails%7D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bnaturality+of+%5C%28%5Cmathit%7Bconcat%7D%5C%29%7D+%5C%7D+%5C%5C+%26+%5Cmathit%7Bmaximum%7D+%5Ccdot+%5Cmathit%7Bconcat%7D+%5Ccdot+%5Cmathit%7Bmap%7D%5C%2C%28%5Cmathit%7Bmap%7D%5C%2C%5Cmathit%7Bsum%7D%29+%5Ccdot+%5Cmathit%7Bmap%7D%5C%2C%5Cmathit%7Binits%7D+%5Ccdot+%5Cmathit%7Btails%7D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7B%5C%28%5Cmathit%7Bmaximum%7D+%5Ccdot+%5Cmathit%7Bconcat%7D+%3D+%5Cmathit%7Bmaximum%7D+%5Ccdot+%5Cmathit%7Bmap%7D%5C%2C%5Cmathit%7Bmaximum%7D%5C%29%7D+%5C%7D+%5C%5C+%26+%5Cmathit%7Bmaximum%7D+%5Ccdot+%5Cmathit%7Bmap%7D%5C%2C%5Cmathit%7Bmaximum%7D+%5Ccdot+%5Cmathit%7Bmap%7D%5C%2C%28%5Cmathit%7Bmap%7D%5C%2C%5Cmathit%7Bsum%7D%29+%5Ccdot+%5Cmathit%7Bmap%7D%5C%2C%5Cmathit%7Binits%7D+%5Ccdot+%5Cmathit%7Btails%7D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bfunctors%7D+%5C%7D+%5C%5C+%26+%5Cmathit%7Bmaximum%7D+%5Ccdot+%5Cmathit%7Bmap%7D%5C%2C%28%5Cmathit%7Bmaximum%7D+%5Ccdot+%5Cmathit%7Bmap%7D%5C%2C%5Cmathit%7Bsum%7D+%5Ccdot+%5Cmathit%7Binits%7D%29+%5Ccdot+%5Cmathit%7Btails%7D+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;mathit{mss} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{definition of &#92;(&#92;mathit{mss}&#92;)} &#92;} &#92;&#92; &amp; &#92;mathit{maximum} &#92;cdot &#92;mathit{map}&#92;,&#92;mathit{sum} &#92;cdot &#92;mathit{segs} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{definition of &#92;(&#92;mathit{segs}&#92;)} &#92;} &#92;&#92; &amp; &#92;mathit{maximum} &#92;cdot &#92;mathit{map}&#92;,&#92;mathit{sum} &#92;cdot &#92;mathit{concat} &#92;cdot &#92;mathit{map}&#92;,&#92;mathit{inits} &#92;cdot &#92;mathit{tails} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{naturality of &#92;(&#92;mathit{concat}&#92;)} &#92;} &#92;&#92; &amp; &#92;mathit{maximum} &#92;cdot &#92;mathit{concat} &#92;cdot &#92;mathit{map}&#92;,(&#92;mathit{map}&#92;,&#92;mathit{sum}) &#92;cdot &#92;mathit{map}&#92;,&#92;mathit{inits} &#92;cdot &#92;mathit{tails} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{&#92;(&#92;mathit{maximum} &#92;cdot &#92;mathit{concat} = &#92;mathit{maximum} &#92;cdot &#92;mathit{map}&#92;,&#92;mathit{maximum}&#92;)} &#92;} &#92;&#92; &amp; &#92;mathit{maximum} &#92;cdot &#92;mathit{map}&#92;,&#92;mathit{maximum} &#92;cdot &#92;mathit{map}&#92;,(&#92;mathit{map}&#92;,&#92;mathit{sum}) &#92;cdot &#92;mathit{map}&#92;,&#92;mathit{inits} &#92;cdot &#92;mathit{tails} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{functors} &#92;} &#92;&#92; &amp; &#92;mathit{maximum} &#92;cdot &#92;mathit{map}&#92;,(&#92;mathit{maximum} &#92;cdot &#92;mathit{map}&#92;,&#92;mathit{sum} &#92;cdot &#92;mathit{inits}) &#92;cdot &#92;mathit{tails} &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;mathit{mss} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{definition of &#92;(&#92;mathit{mss}&#92;)} &#92;} &#92;&#92; &amp; &#92;mathit{maximum} &#92;cdot &#92;mathit{map}&#92;,&#92;mathit{sum} &#92;cdot &#92;mathit{segs} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{definition of &#92;(&#92;mathit{segs}&#92;)} &#92;} &#92;&#92; &amp; &#92;mathit{maximum} &#92;cdot &#92;mathit{map}&#92;,&#92;mathit{sum} &#92;cdot &#92;mathit{concat} &#92;cdot &#92;mathit{map}&#92;,&#92;mathit{inits} &#92;cdot &#92;mathit{tails} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{naturality of &#92;(&#92;mathit{concat}&#92;)} &#92;} &#92;&#92; &amp; &#92;mathit{maximum} &#92;cdot &#92;mathit{concat} &#92;cdot &#92;mathit{map}&#92;,(&#92;mathit{map}&#92;,&#92;mathit{sum}) &#92;cdot &#92;mathit{map}&#92;,&#92;mathit{inits} &#92;cdot &#92;mathit{tails} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{&#92;(&#92;mathit{maximum} &#92;cdot &#92;mathit{concat} = &#92;mathit{maximum} &#92;cdot &#92;mathit{map}&#92;,&#92;mathit{maximum}&#92;)} &#92;} &#92;&#92; &amp; &#92;mathit{maximum} &#92;cdot &#92;mathit{map}&#92;,&#92;mathit{maximum} &#92;cdot &#92;mathit{map}&#92;,(&#92;mathit{map}&#92;,&#92;mathit{sum}) &#92;cdot &#92;mathit{map}&#92;,&#92;mathit{inits} &#92;cdot &#92;mathit{tails} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{functors} &#92;} &#92;&#92; &amp; &#92;mathit{maximum} &#92;cdot &#92;mathit{map}&#92;,(&#92;mathit{maximum} &#92;cdot &#92;mathit{map}&#92;,&#92;mathit{sum} &#92;cdot &#92;mathit{inits}) &#92;cdot &#92;mathit{tails} &#92;end{array} ' class='latex' />
</p></blockquote>
<p> For the final step, if we can write <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bmaximum%7D+%5Ccdot+%5Cmathit%7Bmap%7D%5C%2C%5Cmathit%7Bsum%7D+%5Ccdot+%5Cmathit%7Binits%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{maximum} &#92;cdot &#92;mathit{map}&#92;,&#92;mathit{sum} &#92;cdot &#92;mathit{inits}}' title='{&#92;mathit{maximum} &#92;cdot &#92;mathit{map}&#92;,&#92;mathit{sum} &#92;cdot &#92;mathit{inits}}' class='latex' /> in the form <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfoldr%7D%5C%2Ch%5C%2Ce%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{foldr}&#92;,h&#92;,e}' title='{&#92;mathit{foldr}&#92;,h&#92;,e}' class='latex' />, then the <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bmap%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{map}}' title='{&#92;mathit{map}}' class='latex' /> of this can be fused with the <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Btails%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{tails}}' title='{&#92;mathit{tails}}' class='latex' /> to yield <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bscanr%7D%5C%2Ch%5C%2Ce%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{scanr}&#92;,h&#92;,e}' title='{&#92;mathit{scanr}&#92;,h&#92;,e}' class='latex' />; this observation is known as the <em>Scan Lemma</em>. Moreover, if <img src='http://s0.wp.com/latex.php?latex=%7Bh%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h}' title='{h}' class='latex' /> takes constant time, then this gives a linear-time algorithm for <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bmss%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{mss}}' title='{&#92;mathit{mss}}' class='latex' />.</p>
<p>
The crucial observation is based on Horner&#8217;s Rule for evaluation of polynomials, which is the first important thing you learn in numerical computing&mdash;I was literally taught it in secondary school, in my sixth-year classes in mathematics. Here is its familiar form: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Csum_%7Bi%3D0%7D%5E%7Bn-1%7D+a_i+x%5Ei+%3D+a_0+%2B+a_1+x+%2B+a_2+x%5E2+%2B+%5Ccdots+%2B+a_%7Bn-1%7D+x%5E%7Bn-1%7D+%3D+a_0+%2B+x%28a_1+%2B+x%28a_2+%2B+%5Ccdots+%2B+x%5C%2Ca_%7Bn-1%7D%29%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;sum_{i=0}^{n-1} a_i x^i = a_0 + a_1 x + a_2 x^2 + &#92;cdots + a_{n-1} x^{n-1} = a_0 + x(a_1 + x(a_2 + &#92;cdots + x&#92;,a_{n-1})) ' title='&#92;displaystyle  &#92;sum_{i=0}^{n-1} a_i x^i = a_0 + a_1 x + a_2 x^2 + &#92;cdots + a_{n-1} x^{n-1} = a_0 + x(a_1 + x(a_2 + &#92;cdots + x&#92;,a_{n-1})) ' class='latex' />
</p></blockquote>
<p> but the essence of the rule is about sums of products: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Csum_%7Bi%3D0%7D%5E%7Bn-1%7D+%5Cprod_%7Bj%3D0%7D%5E%7Bi-1%7D+u_j+%3D+1+%2B+u_0+%2B+u_0u_1+%2B+%5Ccdots+%2B+u_0u_1%5Cldots+u_%7Bn-1%7D+%3D+1+%2B+u_0%281+%2B+u_1%281+%2B+%5Ccdots+%2B+u_%7Bn-1%7D%29%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;sum_{i=0}^{n-1} &#92;prod_{j=0}^{i-1} u_j = 1 + u_0 + u_0u_1 + &#92;cdots + u_0u_1&#92;ldots u_{n-1} = 1 + u_0(1 + u_1(1 + &#92;cdots + u_{n-1})) ' title='&#92;displaystyle  &#92;sum_{i=0}^{n-1} &#92;prod_{j=0}^{i-1} u_j = 1 + u_0 + u_0u_1 + &#92;cdots + u_0u_1&#92;ldots u_{n-1} = 1 + u_0(1 + u_1(1 + &#92;cdots + u_{n-1})) ' class='latex' />
</p></blockquote>
<p> Expressed in Haskell, this is captured by the equation </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathit%7Bsum%7D+%5Ccdot+%5Cmathit%7Bmap%7D%5C%2C%5Cmathit%7Bproduct%7D+%5Ccdot+%5Cmathit%7Binits%7D+%3D+%5Cmathit%7Bfoldr%7D%5C%2C%28%5Coplus%29%5C%2Ce+%5Cquad+%5Cmathbf%7Bwhere%7D%5C%3B+e+%3D+1+%5Cmathbin%7B%3B%7D+u+%5Coplus+z+%3D+e+%2B+u+%5Ctimes+z+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathit{sum} &#92;cdot &#92;mathit{map}&#92;,&#92;mathit{product} &#92;cdot &#92;mathit{inits} = &#92;mathit{foldr}&#92;,(&#92;oplus)&#92;,e &#92;quad &#92;mathbf{where}&#92;; e = 1 &#92;mathbin{;} u &#92;oplus z = e + u &#92;times z ' title='&#92;displaystyle  &#92;mathit{sum} &#92;cdot &#92;mathit{map}&#92;,&#92;mathit{product} &#92;cdot &#92;mathit{inits} = &#92;mathit{foldr}&#92;,(&#92;oplus)&#92;,e &#92;quad &#92;mathbf{where}&#92;; e = 1 &#92;mathbin{;} u &#92;oplus z = e + u &#92;times z ' class='latex' />
</p></blockquote>
<p> (where <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bproduct%7D+%3D+%5Cmathit%7Bfoldr%7D%5C%2C%28%5Ctimes%29%5C%2C1%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{product} = &#92;mathit{foldr}&#92;,(&#92;times)&#92;,1}' title='{&#92;mathit{product} = &#92;mathit{foldr}&#92;,(&#92;times)&#92;,1}' class='latex' /> computes the product of a list of integers).</p>
<p>
But Horner&#8217;s Rule is not restricted to sums and products; the essential properties are that addition and multiplication are associative, that multiplication has a unit, and that multiplication distributes over addition. This the algebraic structure of a <em>semiring</em> (but without needing commutativity and a unit of addition, or that that unit is a zero of multiplication). In particular, the so-called <em>tropical semiring</em> on the integers, in which &#8220;addition&#8221; is binary <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmax%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;max}' title='{&#92;max}' class='latex' /> and &#8220;multiplication&#8221; is integer addition, satisfies the requirements. So for the maximum segment sum problem, we get </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathit%7Bmaximum%7D+%5Ccdot+%5Cmathit%7Bmap%7D%5C%2C%5Cmathit%7Bsum%7D+%5Ccdot+%5Cmathit%7Binits%7D+%3D+%5Cmathit%7Bfoldr%7D%5C%2C%28%5Coplus%29%5C%2Ce+%5Cquad+%5Cmathbf%7Bwhere%7D%5C%3B+e+%3D+0+%5Cmathbin%7B%3B%7D+u+%5Coplus+z+%3D+e+%5Cmax+%28u+%2B+z%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathit{maximum} &#92;cdot &#92;mathit{map}&#92;,&#92;mathit{sum} &#92;cdot &#92;mathit{inits} = &#92;mathit{foldr}&#92;,(&#92;oplus)&#92;,e &#92;quad &#92;mathbf{where}&#92;; e = 0 &#92;mathbin{;} u &#92;oplus z = e &#92;max (u + z) ' title='&#92;displaystyle  &#92;mathit{maximum} &#92;cdot &#92;mathit{map}&#92;,&#92;mathit{sum} &#92;cdot &#92;mathit{inits} = &#92;mathit{foldr}&#92;,(&#92;oplus)&#92;,e &#92;quad &#92;mathbf{where}&#92;; e = 0 &#92;mathbin{;} u &#92;oplus z = e &#92;max (u + z) ' class='latex' />
</p></blockquote>
<p> Moreover, <img src='http://s0.wp.com/latex.php?latex=%7B%5Coplus%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;oplus}' title='{&#92;oplus}' class='latex' /> takes constant time, so this gives a linear-time algorithm for <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bmss%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{mss}}' title='{&#92;mathit{mss}}' class='latex' />.</p>
<p><h2> Tail segments, datatype-generically </h2>
<p>
About a decade after the initial &#8220;theory of lists&#8221; work on the maximum segment sum problem, Richard Bird (with Oege de Moor and Paul Hoogendijk) came up with a datatype-generic version of the problem in the paper <a href="http://dx/doi.org/10.1017/S0956796800001556">Generic functional programming with types and relations</a>. It&#8217;s clear what &#8220;maximum&#8221; and &#8220;sum&#8221; mean generically, but not so clear what &#8220;segment&#8221; means for nonlinear datatypes; the point of their paper is basically to resolve that issue.</p>
<p>
Recalling the definition of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bsegs%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{segs}}' title='{&#92;mathit{segs}}' class='latex' /> in terms of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Binits%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{inits}}' title='{&#92;mathit{inits}}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Btails%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{tails}}' title='{&#92;mathit{tails}}' class='latex' />, we see that it would suffice to develop datatype-generic notions of &#8220;initial segment&#8221; and &#8220;tail segment&#8221;. One fruitful perspective is given in Bird &amp; co&#8217;s paper: a &#8220;tail segment&#8221; of a cons list is just a subterm of that list, and an &#8220;initial segment&#8221; is the list but with some tail (that is, some subterm) replaced with the empty structure.</p>
<p>
So, representing a generic &#8220;tail&#8221; of a data structure is easy: it&#8217;s a data structure of the same type, and a subterm of the term denoting the original structure. A datatype-generic definition of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Btails%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{tails}}' title='{&#92;mathit{tails}}' class='latex' /> is a little trickier, though. For lists, you can see it as follows: <em>every node of the original list is labelled with the subterm of the original list rooted at that node</em>. I find this a helpful observation, because it explains why the <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Btails%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{tails}}' title='{&#92;mathit{tails}}' class='latex' /> of a list is one element longer than the list itself: a list with <img src='http://s0.wp.com/latex.php?latex=%7Bn%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{n}' title='{n}' class='latex' /> elements has <img src='http://s0.wp.com/latex.php?latex=%7Bn%2B1%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{n+1}' title='{n+1}' class='latex' /> nodes (<img src='http://s0.wp.com/latex.php?latex=%7Bn%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{n}' title='{n}' class='latex' /> conses and a nil), and each of those nodes gets labelled with one of the <img src='http://s0.wp.com/latex.php?latex=%7Bn%2B1%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{n+1}' title='{n+1}' class='latex' /> subterms of the list. Indeed, <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Btails%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{tails}}' title='{&#92;mathit{tails}}' class='latex' /> ought morally to take a possibly empty list and return a <em>non-empty list</em> of possibly empty lists&mdash;there are two different datatypes involved. Similarly, if one wants the &#8220;tails&#8221; of a data structure of a type in which some nodes have no labels (such as leaf-labelled trees, or indeed such as the &#8220;nil&#8221; constructor of lists), one needs a variant of the datatype providing labels at those positions. Also, for a data structure in which some nodes have multiple labels, or in which there are different types of labels, one needs a variant for which <em>every node has precisely one label</em>. </p>
<p>
Bird &amp; co call this the <em>labelled variant</em> of the original datatype; if the original is a polymorphic datatype <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BT%7D%5C%2C%5Calpha+%3D+%5Cmu%28%5Cmathsf%7BF%7D%5C%2C%5Calpha%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{T}&#92;,&#92;alpha = &#92;mu(&#92;mathsf{F}&#92;,&#92;alpha)}' title='{&#92;mathsf{T}&#92;,&#92;alpha = &#92;mu(&#92;mathsf{F}&#92;,&#92;alpha)}' class='latex' /> for some binary shape functor <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />, then the labelled variant is <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BL%7D%5C%2C%5Calpha+%3D+%5Cmu%28%5Cmathsf%7BG%7D%5C%2C%5Calpha%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{L}&#92;,&#92;alpha = &#92;mu(&#92;mathsf{G}&#92;,&#92;alpha)}' title='{&#92;mathsf{L}&#92;,&#92;alpha = &#92;mu(&#92;mathsf{G}&#92;,&#92;alpha)}' class='latex' /> where <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BG%7D%5C%2C%5Calpha%5C%2C%5Cbeta+%3D+%5Calpha+%5Ctimes+%5Cmathsf%7BF%7D%5C%2C1%5C%2C%5Cbeta%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{G}&#92;,&#92;alpha&#92;,&#92;beta = &#92;alpha &#92;times &#92;mathsf{F}&#92;,1&#92;,&#92;beta}' title='{&#92;mathsf{G}&#92;,&#92;alpha&#92;,&#92;beta = &#92;alpha &#92;times &#92;mathsf{F}&#92;,1&#92;,&#92;beta}' class='latex' />&mdash;whatever labels <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' /> may or may not have specified are ignored, and precisely one label per node is provided. Given this insight, it is straightforward to define a datatype-generic variant <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bsubterms%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{subterms}}' title='{&#92;mathit{subterms}}' class='latex' /> of the <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Btails%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{tails}}' title='{&#92;mathit{tails}}' class='latex' /> function: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathit%7Bsubterms%7D_%7B%5Cmathsf%7BF%7D%7D+%3D+%5Cmathit%7Bfold%7D_%7B%5Cmathsf%7BF%7D%7D%28%5Cmathit%7Bin%7D_%7B%5Cmathsf%7BG%7D%7D+%5Ccdot+%5Cmathit%7Bfork%7D%28%5Cmathit%7Bin%7D_%7B%5Cmathsf%7BF%7D%7D+%5Ccdot+%5Cmathsf%7BF%7D%5C%2C%5Cmathit%7Bid%7D%5C%2C%5Cmathit%7Broot%7D%2C+%5Cmathsf%7BF%7D%5C%2C%21%5C%2C%5Cmathit%7Bid%7D%29%29+%3A%3A+%5Cmathsf%7BT%7D%5C%2C%5Calpha+%5Crightarrow+%5Cmathsf%7BL%7D%28%5Cmathsf%7BT%7D%5C%2C%5Calpha%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathit{subterms}_{&#92;mathsf{F}} = &#92;mathit{fold}_{&#92;mathsf{F}}(&#92;mathit{in}_{&#92;mathsf{G}} &#92;cdot &#92;mathit{fork}(&#92;mathit{in}_{&#92;mathsf{F}} &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{id}&#92;,&#92;mathit{root}, &#92;mathsf{F}&#92;,!&#92;,&#92;mathit{id})) :: &#92;mathsf{T}&#92;,&#92;alpha &#92;rightarrow &#92;mathsf{L}(&#92;mathsf{T}&#92;,&#92;alpha) ' title='&#92;displaystyle  &#92;mathit{subterms}_{&#92;mathsf{F}} = &#92;mathit{fold}_{&#92;mathsf{F}}(&#92;mathit{in}_{&#92;mathsf{G}} &#92;cdot &#92;mathit{fork}(&#92;mathit{in}_{&#92;mathsf{F}} &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{id}&#92;,&#92;mathit{root}, &#92;mathsf{F}&#92;,!&#92;,&#92;mathit{id})) :: &#92;mathsf{T}&#92;,&#92;alpha &#92;rightarrow &#92;mathsf{L}(&#92;mathsf{T}&#92;,&#92;alpha) ' class='latex' />
</p></blockquote>
<p> where <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Broot%7D+%3D+%5Cmathit%7Bfst%7D+%5Ccdot+%5Cmathit%7Bin%7D_%7B%5Cmathsf%7BG%7D%7D%5E%7B-1%7D+%3D+%5Cmathit%7Bfold%7D_%7B%5Cmathsf%7BG%7D%7D%5C%2C%5Cmathit%7Bfst%7D+%3A%3A+%5Cmathsf%7BL%7D%5C%2C%5Calpha+%5Crightarrow+%5Calpha%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{root} = &#92;mathit{fst} &#92;cdot &#92;mathit{in}_{&#92;mathsf{G}}^{-1} = &#92;mathit{fold}_{&#92;mathsf{G}}&#92;,&#92;mathit{fst} :: &#92;mathsf{L}&#92;,&#92;alpha &#92;rightarrow &#92;alpha}' title='{&#92;mathit{root} = &#92;mathit{fst} &#92;cdot &#92;mathit{in}_{&#92;mathsf{G}}^{-1} = &#92;mathit{fold}_{&#92;mathsf{G}}&#92;,&#92;mathit{fst} :: &#92;mathsf{L}&#92;,&#92;alpha &#92;rightarrow &#92;alpha}' class='latex' /> returns the root label of a labelled data structure, and <img src='http://s0.wp.com/latex.php?latex=%7B%21_%7B%5Calpha%7D+%3A%3A+%5Calpha+%5Crightarrow+1%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{!_{&#92;alpha} :: &#92;alpha &#92;rightarrow 1}' title='{!_{&#92;alpha} :: &#92;alpha &#92;rightarrow 1}' class='latex' /> is the unique arrow to the unit type. (Informally, having computed the tree of subterms for each child of a node, we make the tree of subterms for this node by assembling all the child trees with the label for this node; the label should be the whole structure rooted at this node, which can be reconstructed from the roots of the child trees.) What&#8217;s more, there&#8217;s a datatype-generic scan lemma too: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Cmathit%7Bscan%7D_%7B%5Cmathsf%7BF%7D%7D+%26%3A%3A%26+%28%5Cmathsf%7BF%7D%5C%2C%5Calpha%5C%2C%5Cbeta+%5Crightarrow+%5Cbeta%29+%5Crightarrow+%5Cmathsf%7BT%7D%5C%2C%5Calpha+%5Crightarrow+%5Cmathsf%7BL%7D%5C%2C%5Cbeta+%5C%5C+%5Cmathit%7Bscan%7D_%7B%5Cmathsf%7BF%7D%7D%5C%2C%5Cphi+%26%3D%26+%5Cmathsf%7BL%7D%5C%2C%28%5Cmathit%7Bfold%7D_%7B%5Cmathsf%7BF%7D%7D%5C%2C%5Cphi%29+%5Ccdot+%5Cmathit%7Bsubterms%7D_%7B%5Cmathsf%7BF%7D%7D+%5C%5C+%26%3D%26+%5Cmathit%7Bfold%7D_%7B%5Cmathsf%7BF%7D%7D%28%5Cmathit%7Bin%7D_%7B%5Cmathsf%7BG%7D%7D+%5Ccdot+%5Cmathit%7Bfork%7D%28%5Cphi+%5Ccdot+%5Cmathsf%7BF%7D%5C%2C%5Cmathit%7Bid%7D%5C%2C%5Cmathit%7Broot%7D%2C+%5Cmathsf%7BF%7D%5C%2C%21%5C%2C%5Cmathit%7Bid%7D%29%29+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{scan}_{&#92;mathsf{F}} &amp;::&amp; (&#92;mathsf{F}&#92;,&#92;alpha&#92;,&#92;beta &#92;rightarrow &#92;beta) &#92;rightarrow &#92;mathsf{T}&#92;,&#92;alpha &#92;rightarrow &#92;mathsf{L}&#92;,&#92;beta &#92;&#92; &#92;mathit{scan}_{&#92;mathsf{F}}&#92;,&#92;phi &amp;=&amp; &#92;mathsf{L}&#92;,(&#92;mathit{fold}_{&#92;mathsf{F}}&#92;,&#92;phi) &#92;cdot &#92;mathit{subterms}_{&#92;mathsf{F}} &#92;&#92; &amp;=&amp; &#92;mathit{fold}_{&#92;mathsf{F}}(&#92;mathit{in}_{&#92;mathsf{G}} &#92;cdot &#92;mathit{fork}(&#92;phi &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{id}&#92;,&#92;mathit{root}, &#92;mathsf{F}&#92;,!&#92;,&#92;mathit{id})) &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{scan}_{&#92;mathsf{F}} &amp;::&amp; (&#92;mathsf{F}&#92;,&#92;alpha&#92;,&#92;beta &#92;rightarrow &#92;beta) &#92;rightarrow &#92;mathsf{T}&#92;,&#92;alpha &#92;rightarrow &#92;mathsf{L}&#92;,&#92;beta &#92;&#92; &#92;mathit{scan}_{&#92;mathsf{F}}&#92;,&#92;phi &amp;=&amp; &#92;mathsf{L}&#92;,(&#92;mathit{fold}_{&#92;mathsf{F}}&#92;,&#92;phi) &#92;cdot &#92;mathit{subterms}_{&#92;mathsf{F}} &#92;&#92; &amp;=&amp; &#92;mathit{fold}_{&#92;mathsf{F}}(&#92;mathit{in}_{&#92;mathsf{G}} &#92;cdot &#92;mathit{fork}(&#92;phi &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{id}&#92;,&#92;mathit{root}, &#92;mathsf{F}&#92;,!&#92;,&#92;mathit{id})) &#92;end{array} ' class='latex' />
</p></blockquote>
<p> (Again, the label for each node can be constructed from the root labels of each of the child trees.) In fact, <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bsubterms%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{subterms}}' title='{&#92;mathit{subterms}}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bscan%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{scan}}' title='{&#92;mathit{scan}}' class='latex' /> are paramorphisms, and can also be nicely written coinductively as well as inductively. I&#8217;ll return to this in a future post.</p>
<p><h2> Initial segments, datatype-generically </h2>
<p>
What about a datatype-generic &#8220;initial segment&#8221;? As suggested above, that&#8217;s obtained from the original data structure by replacing some subterms with the empty structure. Here I think Bird &amp; co sell themselves a little short, because they insist that the datatype <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BT%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{T}}' title='{&#92;mathsf{T}}' class='latex' /> supports empty structures, which is to say, that <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' /> is of the form <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%5C%2C%5Calpha%5C%2C%5Cbeta+%3D+1+%2B+%5Cmathsf%7BF%7D%27%5C%2C%5Calpha%5C%2C%5Cbeta%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}&#92;,&#92;alpha&#92;,&#92;beta = 1 + &#92;mathsf{F}&#039;&#92;,&#92;alpha&#92;,&#92;beta}' title='{&#92;mathsf{F}&#92;,&#92;alpha&#92;,&#92;beta = 1 + &#92;mathsf{F}&#039;&#92;,&#92;alpha&#92;,&#92;beta}' class='latex' /> for some <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%27%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}&#039;}' title='{&#92;mathsf{F}&#039;}' class='latex' />. This isn&#8217;t necessary: for an arbitrary <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />, we can easily manufacture the appropriate datatype <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BU%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{U}}' title='{&#92;mathsf{U}}' class='latex' /> of &#8220;data structures in which some subterms may be replaced by empty&#8221;, by defining <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BH%7D%5C%2C%5Calpha%5C%2C%5Cbeta+%3D+1+%2B+%5Cmathsf%7BF%7D%5C%2C%5Calpha%5C%2C%5Cbeta%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{H}&#92;,&#92;alpha&#92;,&#92;beta = 1 + &#92;mathsf{F}&#92;,&#92;alpha&#92;,&#92;beta}' title='{&#92;mathsf{H}&#92;,&#92;alpha&#92;,&#92;beta = 1 + &#92;mathsf{F}&#92;,&#92;alpha&#92;,&#92;beta}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BU%7D%5C%2C%5Calpha+%3D+%5Cmu%28%5Cmathsf%7BH%7D%5C%2C%5Calpha%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{U}&#92;,&#92;alpha = &#92;mu(&#92;mathsf{H}&#92;,&#92;alpha)}' title='{&#92;mathsf{U}&#92;,&#92;alpha = &#92;mu(&#92;mathsf{H}&#92;,&#92;alpha)}' class='latex' />.</p>
<p>
As with <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bsubterms%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{subterms}}' title='{&#92;mathit{subterms}}' class='latex' />, the datatype-generic version of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Binits%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{inits}}' title='{&#92;mathit{inits}}' class='latex' /> is a bit trickier&mdash;and this time, the special case of lists is misleading. You might think that because a list has just as many initial segments as it does tail segments, so the labelled variant ought to suffice just as well here too. But this doesn&#8217;t work for non-linear data structures such as trees&mdash;in general, there are many more &#8220;initial&#8221; segments than &#8220;tail&#8221; segments (because one can make independent choices about replacing subterms with the empty structure in each child), and they don&#8217;t align themselves conveniently with the nodes of the original structure.</p>
<p>
The approach I prefer here is just to use an unstructured collection type to hold the &#8220;initial segments&#8221;; that is, a monad. This could be the monad of finite lists, or of finite sets, or of finite bags&mdash;we will defer until later the discussion about precisely which, and write simply <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BM%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{M}}' title='{&#92;mathsf{M}}' class='latex' />. We require only that it provide a <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7BMonadPlus%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{MonadPlus}}' title='{&#92;mathit{MonadPlus}}' class='latex' />-like interface, in the sense of an operator <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bmplus%7D+%3A%3A+%5Cmathsf%7BM%7D%5C%2C%5Calpha+%5Ctimes+%5Cmathsf%7BM%7D%5C%2C%5Calpha+%5Crightarrow+%5Cmathsf%7BM%7D%5C%2C%5Calpha%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{mplus} :: &#92;mathsf{M}&#92;,&#92;alpha &#92;times &#92;mathsf{M}&#92;,&#92;alpha &#92;rightarrow &#92;mathsf{M}&#92;,&#92;alpha}' title='{&#92;mathit{mplus} :: &#92;mathsf{M}&#92;,&#92;alpha &#92;times &#92;mathsf{M}&#92;,&#92;alpha &#92;rightarrow &#92;mathsf{M}&#92;,&#92;alpha}' class='latex' />; however, for reasons that will become clear, we will expect that it does <em>not</em> provide a <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bmzero%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{mzero}}' title='{&#92;mathit{mzero}}' class='latex' /> operator yielding empty collections. </p>
<p>
Now we can think of the datatype-generic version of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Binits%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{inits}}' title='{&#92;mathit{inits}}' class='latex' /> as nondeterministically pruning a data structure by arbitrarily replacing some subterms with the empty structure; or equivalently, as generating the collection of all such prunings. </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathit%7Bprune%7D+%3D+%5Cmathit%7Bfold%7D_%7B%5Cmathsf%7BF%7D%7D%28%5Cmathsf%7BM%7D%5C%2C%5Cmathit%7Bin%7D_%7B%5Cmathsf%7BH%7D%7D+%5Ccdot+%5Cmathit%7Bopt%7D%5C%2C%5Cmathit%7BNothing%7D+%5Ccdot+%5Cmathsf%7BM%7D%5C%2C%5Cmathit%7BJust%7D+%5Ccdot+%5Cdelta_2%29+%3A%3A+%5Cmu%28%5Cmathsf%7BF%7D%5C%2C%5Calpha%29+%5Crightarrow+%5Cmathsf%7BM%7D%28%5Cmu%28%5Cmathsf%7BH%7D%5C%2C%5Calpha%29%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathit{prune} = &#92;mathit{fold}_{&#92;mathsf{F}}(&#92;mathsf{M}&#92;,&#92;mathit{in}_{&#92;mathsf{H}} &#92;cdot &#92;mathit{opt}&#92;,&#92;mathit{Nothing} &#92;cdot &#92;mathsf{M}&#92;,&#92;mathit{Just} &#92;cdot &#92;delta_2) :: &#92;mu(&#92;mathsf{F}&#92;,&#92;alpha) &#92;rightarrow &#92;mathsf{M}(&#92;mu(&#92;mathsf{H}&#92;,&#92;alpha)) ' title='&#92;displaystyle  &#92;mathit{prune} = &#92;mathit{fold}_{&#92;mathsf{F}}(&#92;mathsf{M}&#92;,&#92;mathit{in}_{&#92;mathsf{H}} &#92;cdot &#92;mathit{opt}&#92;,&#92;mathit{Nothing} &#92;cdot &#92;mathsf{M}&#92;,&#92;mathit{Just} &#92;cdot &#92;delta_2) :: &#92;mu(&#92;mathsf{F}&#92;,&#92;alpha) &#92;rightarrow &#92;mathsf{M}(&#92;mu(&#92;mathsf{H}&#92;,&#92;alpha)) ' class='latex' />
</p></blockquote>
<p> Here, <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bopt%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{opt}}' title='{&#92;mathit{opt}}' class='latex' /> supplies a new alternative for a nondeterministic computation: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++opt%5C%2Ca%5C%2C%5Cmathit%7Bmx%7D+%3D+%5Cmathit%7Breturn%7D%5C%2Ca+%5Cmathbin%7B%5Cunderline%7B%5Csmash%7B%5Cmathit%7Bmplus%7D%7D%7D%7D+%5Cmathit%7Bmx%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  opt&#92;,a&#92;,&#92;mathit{mx} = &#92;mathit{return}&#92;,a &#92;mathbin{&#92;underline{&#92;smash{&#92;mathit{mplus}}}} &#92;mathit{mx} ' title='&#92;displaystyle  opt&#92;,a&#92;,&#92;mathit{mx} = &#92;mathit{return}&#92;,a &#92;mathbin{&#92;underline{&#92;smash{&#92;mathit{mplus}}}} &#92;mathit{mx} ' class='latex' />
</p></blockquote>
<p> and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cdelta_2+%3A%3A+%28%5Cmathsf%7BF%7D%5C%2C%5Calpha%29%5Cmathsf%7BM%7D+%5Cmathbin%7B%5Cstackrel%7B.%7D%7B%5Cto%7D%7D+%5Cmathsf%7BM%7D%28%5Cmathsf%7BF%7D%5Calpha%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;delta_2 :: (&#92;mathsf{F}&#92;,&#92;alpha)&#92;mathsf{M} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{M}(&#92;mathsf{F}&#92;alpha)}' title='{&#92;delta_2 :: (&#92;mathsf{F}&#92;,&#92;alpha)&#92;mathsf{M} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{M}(&#92;mathsf{F}&#92;alpha)}' class='latex' /> distributes the shape functor <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' /> over the monad <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BM%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{M}}' title='{&#92;mathsf{M}}' class='latex' /> (which can be defined for all <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7BTraversable%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{Traversable}}' title='{&#92;mathit{Traversable}}' class='latex' /> functors <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%5C%2C%5Calpha%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}&#92;,&#92;alpha}' title='{&#92;mathsf{F}&#92;,&#92;alpha}' class='latex' />). Informally, once you have computed all possible ways of pruning each of the children of a node, a pruning of the node itself is formed either as <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7BJust%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{Just}}' title='{&#92;mathit{Just}}' class='latex' /> some node assembled from arbitrarily pruned children, or <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7BNothing%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{Nothing}}' title='{&#92;mathit{Nothing}}' class='latex' /> for the empty structure.</p>
<p><h2> Horner&#8217;s Rule, datatype-generically </h2>
<p>
As we&#8217;ve seen, the essential property behind Horner&#8217;s Rule is one of distributivity. In the datatype-generic case, we will model this as follows. We are given an <img src='http://s0.wp.com/latex.php?latex=%7B%28%5Cmathsf%7BF%7D%5C%2C%5Calpha%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(&#92;mathsf{F}&#92;,&#92;alpha)}' title='{(&#92;mathsf{F}&#92;,&#92;alpha)}' class='latex' />-algebra <img src='http://s0.wp.com/latex.php?latex=%7B%28%5Cbeta%2Cf%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(&#92;beta,f)}' title='{(&#92;beta,f)}' class='latex' />, and a <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BM%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{M}}' title='{&#92;mathsf{M}}' class='latex' />-algebra <img src='http://s0.wp.com/latex.php?latex=%7B%28%5Cbeta%2Ck%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(&#92;beta,k)}' title='{(&#92;beta,k)}' class='latex' />; you might think of these as &#8220;datatype-generic product&#8221; and &#8220;collection sum&#8221;, respectively. Then there are two different methods of computing a <img src='http://s0.wp.com/latex.php?latex=%7B%5Cbeta%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;beta}' title='{&#92;beta}' class='latex' /> result from an <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%5C%2C%5Calpha%5C%2C%28%5Cmathsf%7BM%7D%5C%2C%5Cbeta%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}&#92;,&#92;alpha&#92;,(&#92;mathsf{M}&#92;,&#92;beta)}' title='{&#92;mathsf{F}&#92;,&#92;alpha&#92;,(&#92;mathsf{M}&#92;,&#92;beta)}' class='latex' /> structure: we can either distribute the <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%5C%2C%5Calpha%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}&#92;,&#92;alpha}' title='{&#92;mathsf{F}&#92;,&#92;alpha}' class='latex' /> structure over the collection(s) of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cbeta%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;beta}' title='{&#92;beta}' class='latex' />s, compute the &#8220;product&#8221; <img src='http://s0.wp.com/latex.php?latex=%7Bf%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f}' title='{f}' class='latex' /> of each structure, and then compute the &#8220;sum&#8221; <img src='http://s0.wp.com/latex.php?latex=%7Bk%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{k}' title='{k}' class='latex' /> of the resulting products; or we can &#8220;sum&#8221; each collection, then compute the &#8220;product&#8221; of the resulting structure. Distributivity of &#8220;product&#8221; over &#8220;sum&#8221; is the property that these two different methods agree, as illustrated in the following diagram.
<p align="center"><img width="300" src="http://patternsinfp.files.wordpress.com/2011/05/horner-distributivity.png?w=300"></p>
<p>  For example, with <img src='http://s0.wp.com/latex.php?latex=%7Bf+%3A%3A+%5Cmathsf%7BF%7D%5C%2C%7B%5Cmathbb+Z%7D%5C%2C%7B%5Cmathbb+Z%7D+%5Crightarrow+%7B%5Cmathbb+Z%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f :: &#92;mathsf{F}&#92;,{&#92;mathbb Z}&#92;,{&#92;mathbb Z} &#92;rightarrow {&#92;mathbb Z}}' title='{f :: &#92;mathsf{F}&#92;,{&#92;mathbb Z}&#92;,{&#92;mathbb Z} &#92;rightarrow {&#92;mathbb Z}}' class='latex' /> adding all the integers in an <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />-structure, and <img src='http://s0.wp.com/latex.php?latex=%7Bk+%3A%3A+%5Cmathsf%7BM%7D%5C%2C%7B%5Cmathbb+Z%7D+%5Crightarrow+%7B%5Cmathbb+Z%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{k :: &#92;mathsf{M}&#92;,{&#92;mathbb Z} &#92;rightarrow {&#92;mathbb Z}}' title='{k :: &#92;mathsf{M}&#92;,{&#92;mathbb Z} &#92;rightarrow {&#92;mathbb Z}}' class='latex' /> finding the maximum of a (non-empty) collection, the diagram commutes. (To match up with the rest of the story, we have presented distributivity in terms of a bifunctor <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />, although the first parameter <img src='http://s0.wp.com/latex.php?latex=%7B%5Calpha%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;alpha}' title='{&#92;alpha}' class='latex' /> plays no role. We could just have well have used a unary functor, dropping the <img src='http://s0.wp.com/latex.php?latex=%7B%5Calpha%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;alpha}' title='{&#92;alpha}' class='latex' />, and changing the distributor to <img src='http://s0.wp.com/latex.php?latex=%7B%5Cdelta+%3A%3A+%5Cmathsf%7BF%7D%5Cmathsf%7BM%7D+%5Cmathbin%7B%5Cstackrel%7B.%7D%7B%5Cto%7D%7D+%5Cmathsf%7BM%7D%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;delta :: &#92;mathsf{F}&#92;mathsf{M} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{M}&#92;mathsf{F}}' title='{&#92;delta :: &#92;mathsf{F}&#92;mathsf{M} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{M}&#92;mathsf{F}}' class='latex' />.)</p>
<p>
Note that <img src='http://s0.wp.com/latex.php?latex=%7B%28%5Cbeta%2Ck%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(&#92;beta,k)}' title='{(&#92;beta,k)}' class='latex' /> is required to be an algebra for the monad <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BM%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{M}}' title='{&#92;mathsf{M}}' class='latex' />. This means that it is not only an algebra for <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BM%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{M}}' title='{&#92;mathsf{M}}' class='latex' /> as a functor (namely, of type <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BM%7D%5C%2C%5Cbeta+%5Crightarrow+%5Cbeta%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{M}&#92;,&#92;beta &#92;rightarrow &#92;beta}' title='{&#92;mathsf{M}&#92;,&#92;beta &#92;rightarrow &#92;beta}' class='latex' />), but also it should respect the extra structure of the monad: <img src='http://s0.wp.com/latex.php?latex=%7Bk+%5Ccdot+%5Cmathit%7Breturn%7D+%3D+%5Cmathit%7Bid%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{k &#92;cdot &#92;mathit{return} = &#92;mathit{id}}' title='{k &#92;cdot &#92;mathit{return} = &#92;mathit{id}}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7Bk+%5Ccdot+%5Cmathit%7Bjoin%7D+%3D+k+%5Ccdot+%5Cmathsf%7BM%7D%5C%2Ck%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{k &#92;cdot &#92;mathit{join} = k &#92;cdot &#92;mathsf{M}&#92;,k}' title='{k &#92;cdot &#92;mathit{join} = k &#92;cdot &#92;mathsf{M}&#92;,k}' class='latex' />. For the special case of monads for associative collections (such as lists, bags, and sets), and in homage to the old Squiggol papers, we will stick to <em>reductions</em>&mdash;<img src='http://s0.wp.com/latex.php?latex=%7Bk%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{k}' title='{k}' class='latex' />s of the form <img src='http://s0.wp.com/latex.php?latex=%7B%5Coplus%2F%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;oplus/}' title='{&#92;oplus/}' class='latex' /> for associative binary operator <img src='http://s0.wp.com/latex.php?latex=%7B%7B%5Coplus%7D+%3A%3A+%5Cbeta+%5Ctimes+%5Cbeta+%5Crightarrow+%5Cbeta%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{{&#92;oplus} :: &#92;beta &#92;times &#92;beta &#92;rightarrow &#92;beta}' title='{{&#92;oplus} :: &#92;beta &#92;times &#92;beta &#92;rightarrow &#92;beta}' class='latex' />; then we also have distribution over choice: <img src='http://s0.wp.com/latex.php?latex=%7B%5Coplus+%2F+%28x+%5Cmathbin%7B%5Cunderline%7B%5Csmash%7B%5Cmathit%7Bmplus%7D%7D%7D%7D+y%29+%3D+%28%5Coplus%2Fx%29+%5Coplus+%28%5Coplus%2Fy%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;oplus / (x &#92;mathbin{&#92;underline{&#92;smash{&#92;mathit{mplus}}}} y) = (&#92;oplus/x) &#92;oplus (&#92;oplus/y)}' title='{&#92;oplus / (x &#92;mathbin{&#92;underline{&#92;smash{&#92;mathit{mplus}}}} y) = (&#92;oplus/x) &#92;oplus (&#92;oplus/y)}' class='latex' />. Note also that we prohibited empty collections in <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BM%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{M}}' title='{&#92;mathsf{M}}' class='latex' />, so we do not need a unit for <img src='http://s0.wp.com/latex.php?latex=%7B%5Coplus%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;oplus}' title='{&#92;oplus}' class='latex' />.</p>
<p>
Recall that we modelled an &#8220;initial segment&#8221; of a structure of type <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmu%28%5Cmathsf%7BF%7D%5C%2C%5Calpha%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mu(&#92;mathsf{F}&#92;,&#92;alpha)}' title='{&#92;mu(&#92;mathsf{F}&#92;,&#92;alpha)}' class='latex' /> as being of type <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmu%28%5Cmathsf%7BH%7D%5C%2C%5Calpha%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mu(&#92;mathsf{H}&#92;,&#92;alpha)}' title='{&#92;mu(&#92;mathsf{H}&#92;,&#92;alpha)}' class='latex' />, where <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BH%7D%5C%2C%5Calpha%5C%2C%5Cbeta+%3D+1+%2B+%5Cmathsf%7BF%7D%5C%2C%5Calpha%5C%2C%5Cbeta%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{H}&#92;,&#92;alpha&#92;,&#92;beta = 1 + &#92;mathsf{F}&#92;,&#92;alpha&#92;,&#92;beta}' title='{&#92;mathsf{H}&#92;,&#92;alpha&#92;,&#92;beta = 1 + &#92;mathsf{F}&#92;,&#92;alpha&#92;,&#92;beta}' class='latex' />. We need to generalize &#8220;product&#8221; to work on this extended structure, which is to say, we need to specify the value <img src='http://s0.wp.com/latex.php?latex=%7Bb%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{b}' title='{b}' class='latex' /> of the &#8220;product&#8221; of the empty structure too. Then we let <img src='http://s0.wp.com/latex.php?latex=%7Bg+%3D+%5Cmathit%7Bmaybe%7D%5C%2Cb%5C%2Cf+%3A%3A+%5Cmathsf%7BH%7D%5C%2C%5Calpha%5C%2C%5Cbeta+%5Crightarrow+%5Cbeta%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{g = &#92;mathit{maybe}&#92;,b&#92;,f :: &#92;mathsf{H}&#92;,&#92;alpha&#92;,&#92;beta &#92;rightarrow &#92;beta}' title='{g = &#92;mathit{maybe}&#92;,b&#92;,f :: &#92;mathsf{H}&#92;,&#92;alpha&#92;,&#92;beta &#92;rightarrow &#92;beta}' class='latex' />, so that <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfold%7D_%7B%5Cmathsf%7BH%7D%7D%28g%29+%3A%3A+%5Cmu%28%5Cmathsf%7BH%7D%5C%2C%5Calpha%29+%5Crightarrow+%5Cbeta%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{fold}_{&#92;mathsf{H}}(g) :: &#92;mu(&#92;mathsf{H}&#92;,&#92;alpha) &#92;rightarrow &#92;beta}' title='{&#92;mathit{fold}_{&#92;mathsf{H}}(g) :: &#92;mu(&#92;mathsf{H}&#92;,&#92;alpha) &#92;rightarrow &#92;beta}' class='latex' />.</p>
<p>
The datatype-generic version of Horner&#8217;s Rule is then about computing the &#8220;sum&#8221; of the &#8220;products&#8221; of each of the &#8220;initial segments&#8221; of a data structure: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%7B%5Coplus%2F%7D+%5Ccdot+%5Cmathsf%7BM%7D%28%5Cmathit%7Bfold%7D_%7B%5Cmathsf%7BH%7D%7D%28g%29%29+%5Ccdot+%5Cmathit%7Bprune%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  {&#92;oplus/} &#92;cdot &#92;mathsf{M}(&#92;mathit{fold}_{&#92;mathsf{H}}(g)) &#92;cdot &#92;mathit{prune} ' title='&#92;displaystyle  {&#92;oplus/} &#92;cdot &#92;mathsf{M}(&#92;mathit{fold}_{&#92;mathsf{H}}(g)) &#92;cdot &#92;mathit{prune} ' class='latex' />
</p></blockquote>
<p> We will use fold fusion to show that this can be computed as a single fold, given the necessary distributivity property. </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Bll%7D+%26+%5Cmathord%7B%5Coplus%2F%7D+%5Ccdot+%5Cmathsf%7BM%7D%28%5Cmathit%7Bfold%7D_%7B%5Cmathsf%7BH%7D%7D%28g%29%29+%5Ccdot+%5Cmathit%7Bprune%7D+%5Ccdot+%5Cmathit%7Bin%7D_%7B%5Cmathsf%7BF%7D%7D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bevaluation+for+%5C%28%5Cmathit%7Bprune%7D%5C%29%7D+%5C%7D+%5C%5C+%26+%5Cmathord%7B%5Coplus%2F%7D+%5Ccdot+%5Cmathsf%7BM%7D%28%5Cmathit%7Bfold%7D_%7B%5Cmathsf%7BH%7D%7D%28g%29%29+%5Ccdot+%5Cmathsf%7BM%7D%5C%2C%5Cmathit%7Bin%7D_%7B%5Cmathsf%7BH%7D%7D+%5Ccdot+%5Cmathit%7Bopt%7D%5C%2C%5Cmathit%7BNothing%7D+%5Ccdot+%5Cmathsf%7BM%7D%5C%2C%5Cmathit%7BJust%7D+%5Ccdot+%5Cdelta_2+%5Ccdot+%5Cmathsf%7BF%7D%5C%2C%5Cmathit%7Bid%7D%5C%2C%5Cmathit%7Bprune%7D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bfunctors%3B+evaluation+for+%5C%28%5Cmathit%7Bfold%7D_%7B%5Cmathsf%7BH%7D%7D%5C%29%7D+%5C%7D+%5C%5C+%26+%5Cmathord%7B%5Coplus%2F%7D+%5Ccdot+%5Cmathsf%7BM%7D%28g+%5Ccdot+%5Cmathsf%7BH%7D%5C%2C%5Cmathit%7Bid%7D%5C%2C%28%5Cmathit%7Bfold%7D_%7B%5Cmathsf%7BH%7D%7D%28g%29%29%29+%5Ccdot+%5Cmathit%7Bopt%7D%5C%2C%5Cmathit%7BNothing%7D+%5Ccdot+%5Cmathsf%7BM%7D%5C%2C%5Cmathit%7BJust%7D+%5Ccdot+%5Cdelta_2+%5Ccdot+%5Cmathsf%7BF%7D%5C%2C%5Cmathit%7Bid%7D%5C%2C%5Cmathit%7Bprune%7D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7B%5C%28%5Cmathsf%7BM%7D%5C%2Ch+%5Ccdot+%5Cmathit%7Bopt%7D%5C%2Ca+%3D+%5Cmathit%7Bopt%7D%5C%2C%28h%5C%2Ca%29+%5Ccdot+%5Cmathsf%7BM%7D%5C%2Ch%5C%29%7D+%5C%7D+%5C%5C+%26+%5Cmathord%7B%5Coplus%2F%7D+%5Ccdot+%5Cmathit%7Bopt%7D%5C%2C%28g%5C%2C%5Cmathit%7BNothing%7D%29+%5Ccdot+%5Cmathsf%7BM%7D%28g+%5Ccdot+%5Cmathsf%7BH%7D%5C%2C%5Cmathit%7Bid%7D%5C%2C%28%5Cmathit%7Bfold%7D_%7B%5Cmathsf%7BH%7D%7D%28g%29%29%29+%5Ccdot+%5Cmathsf%7BM%7D%5C%2C%5Cmathit%7BJust%7D+%5Ccdot+%5Cdelta_2+%5Ccdot+%5Cmathsf%7BF%7D%5C%2C%5Cmathit%7Bid%7D%5C%2C%5Cmathit%7Bprune%7D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bfunctors%3B+%5C%28%5Cmathit%7BJust%7D+%3A%3A+%5Cmathsf%7BF%7D%5C%2C%5Calpha+%5Cmathbin%7B%5Cstackrel%7B.%7D%7B%5Cto%7D%7D+%5Cmathsf%7BH%7D%5C%2C%5Calpha%5C%29%7D+%5C%7D+%5C%5C+%26+%5Cmathord%7B%5Coplus%2F%7D+%5Ccdot+%5Cmathit%7Bopt%7D%5C%2C%28g%5C%2C%5Cmathit%7BNothing%7D%29+%5Ccdot+%5Cmathsf%7BM%7D%5C%2Cg+%5Ccdot+%5Cmathsf%7BM%7D%28%5Cmathit%7BJust%7D+%5Ccdot+%5Cmathsf%7BF%7D%5C%2C%5Cmathit%7Bid%7D%5C%2C%28%5Cmathit%7Bfold%7D_%7B%5Cmathsf%7BH%7D%7D%28g%29%29%29+%5Ccdot+%5Cdelta_2+%5Ccdot+%5Cmathsf%7BF%7D%5C%2C%5Cmathit%7Bid%7D%5C%2C%5Cmathit%7Bprune%7D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bfunctors%3B+%5C%28%5Cdelta_2+%3A%3A+%28%5Cmathsf%7BF%7D%5Calpha%29%5Cmathsf%7BM%7D+%5Cmathbin%7B%5Cstackrel%7B.%7D%7B%5Cto%7D%7D+%5Cmathsf%7BM%7D%28%5Cmathsf%7BF%7D%5Calpha%29%5C%29%7D+%5C%7D+%5C%5C+%26+%5Cmathord%7B%5Coplus%2F%7D+%5Ccdot+%5Cmathit%7Bopt%7D%5C%2C%28g%5C%2C%5Cmathit%7BNothing%7D%29+%5Ccdot+%5Cmathsf%7BM%7D%28g+%5Ccdot+%5Cmathit%7BJust%7D%29+%5Ccdot+%5Cdelta_2+%5Ccdot+%5Cmathsf%7BF%7D%5C%2C%5Cmathit%7Bid%7D%5C%2C%28%5Cmathsf%7BM%7D%28%5Cmathit%7Bfold%7D_%7B%5Cmathsf%7BH%7D%7D%28g%29%29%29+%5Ccdot+%5Cmathsf%7BF%7D%5C%2C%5Cmathit%7Bid%7D%5C%2C%5Cmathit%7Bprune%7D+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;mathord{&#92;oplus/} &#92;cdot &#92;mathsf{M}(&#92;mathit{fold}_{&#92;mathsf{H}}(g)) &#92;cdot &#92;mathit{prune} &#92;cdot &#92;mathit{in}_{&#92;mathsf{F}} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{evaluation for &#92;(&#92;mathit{prune}&#92;)} &#92;} &#92;&#92; &amp; &#92;mathord{&#92;oplus/} &#92;cdot &#92;mathsf{M}(&#92;mathit{fold}_{&#92;mathsf{H}}(g)) &#92;cdot &#92;mathsf{M}&#92;,&#92;mathit{in}_{&#92;mathsf{H}} &#92;cdot &#92;mathit{opt}&#92;,&#92;mathit{Nothing} &#92;cdot &#92;mathsf{M}&#92;,&#92;mathit{Just} &#92;cdot &#92;delta_2 &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{id}&#92;,&#92;mathit{prune} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{functors; evaluation for &#92;(&#92;mathit{fold}_{&#92;mathsf{H}}&#92;)} &#92;} &#92;&#92; &amp; &#92;mathord{&#92;oplus/} &#92;cdot &#92;mathsf{M}(g &#92;cdot &#92;mathsf{H}&#92;,&#92;mathit{id}&#92;,(&#92;mathit{fold}_{&#92;mathsf{H}}(g))) &#92;cdot &#92;mathit{opt}&#92;,&#92;mathit{Nothing} &#92;cdot &#92;mathsf{M}&#92;,&#92;mathit{Just} &#92;cdot &#92;delta_2 &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{id}&#92;,&#92;mathit{prune} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{&#92;(&#92;mathsf{M}&#92;,h &#92;cdot &#92;mathit{opt}&#92;,a = &#92;mathit{opt}&#92;,(h&#92;,a) &#92;cdot &#92;mathsf{M}&#92;,h&#92;)} &#92;} &#92;&#92; &amp; &#92;mathord{&#92;oplus/} &#92;cdot &#92;mathit{opt}&#92;,(g&#92;,&#92;mathit{Nothing}) &#92;cdot &#92;mathsf{M}(g &#92;cdot &#92;mathsf{H}&#92;,&#92;mathit{id}&#92;,(&#92;mathit{fold}_{&#92;mathsf{H}}(g))) &#92;cdot &#92;mathsf{M}&#92;,&#92;mathit{Just} &#92;cdot &#92;delta_2 &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{id}&#92;,&#92;mathit{prune} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{functors; &#92;(&#92;mathit{Just} :: &#92;mathsf{F}&#92;,&#92;alpha &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{H}&#92;,&#92;alpha&#92;)} &#92;} &#92;&#92; &amp; &#92;mathord{&#92;oplus/} &#92;cdot &#92;mathit{opt}&#92;,(g&#92;,&#92;mathit{Nothing}) &#92;cdot &#92;mathsf{M}&#92;,g &#92;cdot &#92;mathsf{M}(&#92;mathit{Just} &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{id}&#92;,(&#92;mathit{fold}_{&#92;mathsf{H}}(g))) &#92;cdot &#92;delta_2 &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{id}&#92;,&#92;mathit{prune} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{functors; &#92;(&#92;delta_2 :: (&#92;mathsf{F}&#92;alpha)&#92;mathsf{M} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{M}(&#92;mathsf{F}&#92;alpha)&#92;)} &#92;} &#92;&#92; &amp; &#92;mathord{&#92;oplus/} &#92;cdot &#92;mathit{opt}&#92;,(g&#92;,&#92;mathit{Nothing}) &#92;cdot &#92;mathsf{M}(g &#92;cdot &#92;mathit{Just}) &#92;cdot &#92;delta_2 &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{id}&#92;,(&#92;mathsf{M}(&#92;mathit{fold}_{&#92;mathsf{H}}(g))) &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{id}&#92;,&#92;mathit{prune} &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;mathord{&#92;oplus/} &#92;cdot &#92;mathsf{M}(&#92;mathit{fold}_{&#92;mathsf{H}}(g)) &#92;cdot &#92;mathit{prune} &#92;cdot &#92;mathit{in}_{&#92;mathsf{F}} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{evaluation for &#92;(&#92;mathit{prune}&#92;)} &#92;} &#92;&#92; &amp; &#92;mathord{&#92;oplus/} &#92;cdot &#92;mathsf{M}(&#92;mathit{fold}_{&#92;mathsf{H}}(g)) &#92;cdot &#92;mathsf{M}&#92;,&#92;mathit{in}_{&#92;mathsf{H}} &#92;cdot &#92;mathit{opt}&#92;,&#92;mathit{Nothing} &#92;cdot &#92;mathsf{M}&#92;,&#92;mathit{Just} &#92;cdot &#92;delta_2 &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{id}&#92;,&#92;mathit{prune} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{functors; evaluation for &#92;(&#92;mathit{fold}_{&#92;mathsf{H}}&#92;)} &#92;} &#92;&#92; &amp; &#92;mathord{&#92;oplus/} &#92;cdot &#92;mathsf{M}(g &#92;cdot &#92;mathsf{H}&#92;,&#92;mathit{id}&#92;,(&#92;mathit{fold}_{&#92;mathsf{H}}(g))) &#92;cdot &#92;mathit{opt}&#92;,&#92;mathit{Nothing} &#92;cdot &#92;mathsf{M}&#92;,&#92;mathit{Just} &#92;cdot &#92;delta_2 &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{id}&#92;,&#92;mathit{prune} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{&#92;(&#92;mathsf{M}&#92;,h &#92;cdot &#92;mathit{opt}&#92;,a = &#92;mathit{opt}&#92;,(h&#92;,a) &#92;cdot &#92;mathsf{M}&#92;,h&#92;)} &#92;} &#92;&#92; &amp; &#92;mathord{&#92;oplus/} &#92;cdot &#92;mathit{opt}&#92;,(g&#92;,&#92;mathit{Nothing}) &#92;cdot &#92;mathsf{M}(g &#92;cdot &#92;mathsf{H}&#92;,&#92;mathit{id}&#92;,(&#92;mathit{fold}_{&#92;mathsf{H}}(g))) &#92;cdot &#92;mathsf{M}&#92;,&#92;mathit{Just} &#92;cdot &#92;delta_2 &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{id}&#92;,&#92;mathit{prune} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{functors; &#92;(&#92;mathit{Just} :: &#92;mathsf{F}&#92;,&#92;alpha &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{H}&#92;,&#92;alpha&#92;)} &#92;} &#92;&#92; &amp; &#92;mathord{&#92;oplus/} &#92;cdot &#92;mathit{opt}&#92;,(g&#92;,&#92;mathit{Nothing}) &#92;cdot &#92;mathsf{M}&#92;,g &#92;cdot &#92;mathsf{M}(&#92;mathit{Just} &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{id}&#92;,(&#92;mathit{fold}_{&#92;mathsf{H}}(g))) &#92;cdot &#92;delta_2 &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{id}&#92;,&#92;mathit{prune} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{functors; &#92;(&#92;delta_2 :: (&#92;mathsf{F}&#92;alpha)&#92;mathsf{M} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{M}(&#92;mathsf{F}&#92;alpha)&#92;)} &#92;} &#92;&#92; &amp; &#92;mathord{&#92;oplus/} &#92;cdot &#92;mathit{opt}&#92;,(g&#92;,&#92;mathit{Nothing}) &#92;cdot &#92;mathsf{M}(g &#92;cdot &#92;mathit{Just}) &#92;cdot &#92;delta_2 &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{id}&#92;,(&#92;mathsf{M}(&#92;mathit{fold}_{&#92;mathsf{H}}(g))) &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{id}&#92;,&#92;mathit{prune} &#92;end{array} ' class='latex' />
</p></blockquote>
<p> (Sadly, I have to break this calculation in two to get it through WordPress&#8217;s somewhat fragile LaTeX processor&#8230; where were we? Ah, yes:) </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Bll%7D+%26+%5Cmathord%7B%5Coplus%2F%7D+%5Ccdot+%5Cmathit%7Bopt%7D%5C%2C%28g%5C%2C%5Cmathit%7BNothing%7D%29+%5Ccdot+%5Cmathsf%7BM%7D%28g+%5Ccdot+%5Cmathit%7BJust%7D%29+%5Ccdot+%5Cdelta_2+%5Ccdot+%5Cmathsf%7BF%7D%5C%2C%5Cmathit%7Bid%7D%5C%2C%28%5Cmathsf%7BM%7D%28%5Cmathit%7Bfold%7D_%7B%5Cmathsf%7BH%7D%7D%28g%29%29%29+%5Ccdot+%5Cmathsf%7BF%7D%5C%2C%5Cmathit%7Bid%7D%5C%2C%5Cmathit%7Bprune%7D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7B%5C%28g+%3D+%5Cmathit%7Bmaybe%7D%5C%2Cb%5C%2Cf%5C%29%7D+%5C%7D+%5C%5C+%26+%5Cmathord%7B%5Coplus%2F%7D+%5Ccdot+%5Cmathit%7Bopt%7D%5C%2Cb+%5Ccdot+%5Cmathsf%7BM%7D%5C%2Cf+%5Ccdot+%5Cdelta_2+%5Ccdot+%5Cmathsf%7BF%7D%5C%2C%5Cmathit%7Bid%7D%5C%2C%28%5Cmathsf%7BM%7D%28%5Cmathit%7Bfold%7D_%7B%5Cmathsf%7BH%7D%7D%28g%29%29%29+%5Ccdot+%5Cmathsf%7BF%7D%5C%2C%5Cmathit%7Bid%7D%5C%2C%5Cmathit%7Bprune%7D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7B%5C%28%7B%5Coplus%2F%7D+%5Ccdot+%5Cmathit%7Bopt%7D%5C%2Cb+%3D+%28b%5Coplus%29+%5Ccdot+%7B%5Coplus%2F%7D%5C%29%7D+%5C%7D+%5C%5C+%26+%28b%5Coplus%29+%5Ccdot+%7B%5Coplus%2F%7D+%5Ccdot+%5Cmathsf%7BM%7D%5C%2Cf+%5Ccdot+%5Cdelta_2+%5Ccdot+%5Cmathsf%7BF%7D%5C%2C%5Cmathit%7Bid%7D%5C%2C%28%5Cmathsf%7BM%7D%28%5Cmathit%7Bfold%7D_%7B%5Cmathsf%7BH%7D%7D%28g%29%29%29+%5Ccdot+%5Cmathsf%7BF%7D%5C%2C%5Cmathit%7Bid%7D%5C%2C%5Cmathit%7Bprune%7D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bdistributivity%3A+%5C%28%7B%5Coplus%2F%7D+%5Ccdot+%5Cmathsf%7BM%7D%5C%2Cf+%5Ccdot+%5Cdelta_2+%3D+f+%5Ccdot+%5Cmathsf%7BF%7D%5C%2C%5Cmathit%7Bid%7D%5C%2C%28%5Coplus%2F%29%5C%29%7D+%5C%7D+%5C%5C+%26+%28b%5Coplus%29+%5Ccdot+f+%5Ccdot+%5Cmathsf%7BF%7D%5C%2C%5Cmathit%7Bid%7D%5C%2C%28%5Coplus%2F%29+%5Ccdot+%5Cmathsf%7BF%7D%5C%2C%5Cmathit%7Bid%7D%5C%2C%28%5Cmathsf%7BM%7D%28%5Cmathit%7Bfold%7D_%7B%5Cmathsf%7BH%7D%7D%28g%29%29%29+%5Ccdot+%5Cmathsf%7BF%7D%5C%2C%5Cmathit%7Bid%7D%5C%2C%5Cmathit%7Bprune%7D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bfunctors%7D+%5C%7D+%5C%5C+%26+%28b%5Coplus%29+%5Ccdot+f+%5Ccdot+%5Cmathsf%7BF%7D%5C%2C%5Cmathit%7Bid%7D%5C%2C%28%7B%5Coplus%2F%7D+%5Ccdot+%5Cmathsf%7BM%7D%28%5Cmathit%7Bfold%7D_%7B%5Cmathsf%7BH%7D%7D%28g%29%29+%5Ccdot+%5Cmathit%7Bprune%7D%29+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;mathord{&#92;oplus/} &#92;cdot &#92;mathit{opt}&#92;,(g&#92;,&#92;mathit{Nothing}) &#92;cdot &#92;mathsf{M}(g &#92;cdot &#92;mathit{Just}) &#92;cdot &#92;delta_2 &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{id}&#92;,(&#92;mathsf{M}(&#92;mathit{fold}_{&#92;mathsf{H}}(g))) &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{id}&#92;,&#92;mathit{prune} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{&#92;(g = &#92;mathit{maybe}&#92;,b&#92;,f&#92;)} &#92;} &#92;&#92; &amp; &#92;mathord{&#92;oplus/} &#92;cdot &#92;mathit{opt}&#92;,b &#92;cdot &#92;mathsf{M}&#92;,f &#92;cdot &#92;delta_2 &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{id}&#92;,(&#92;mathsf{M}(&#92;mathit{fold}_{&#92;mathsf{H}}(g))) &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{id}&#92;,&#92;mathit{prune} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{&#92;({&#92;oplus/} &#92;cdot &#92;mathit{opt}&#92;,b = (b&#92;oplus) &#92;cdot {&#92;oplus/}&#92;)} &#92;} &#92;&#92; &amp; (b&#92;oplus) &#92;cdot {&#92;oplus/} &#92;cdot &#92;mathsf{M}&#92;,f &#92;cdot &#92;delta_2 &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{id}&#92;,(&#92;mathsf{M}(&#92;mathit{fold}_{&#92;mathsf{H}}(g))) &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{id}&#92;,&#92;mathit{prune} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{distributivity: &#92;({&#92;oplus/} &#92;cdot &#92;mathsf{M}&#92;,f &#92;cdot &#92;delta_2 = f &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{id}&#92;,(&#92;oplus/)&#92;)} &#92;} &#92;&#92; &amp; (b&#92;oplus) &#92;cdot f &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{id}&#92;,(&#92;oplus/) &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{id}&#92;,(&#92;mathsf{M}(&#92;mathit{fold}_{&#92;mathsf{H}}(g))) &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{id}&#92;,&#92;mathit{prune} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{functors} &#92;} &#92;&#92; &amp; (b&#92;oplus) &#92;cdot f &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{id}&#92;,({&#92;oplus/} &#92;cdot &#92;mathsf{M}(&#92;mathit{fold}_{&#92;mathsf{H}}(g)) &#92;cdot &#92;mathit{prune}) &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;mathord{&#92;oplus/} &#92;cdot &#92;mathit{opt}&#92;,(g&#92;,&#92;mathit{Nothing}) &#92;cdot &#92;mathsf{M}(g &#92;cdot &#92;mathit{Just}) &#92;cdot &#92;delta_2 &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{id}&#92;,(&#92;mathsf{M}(&#92;mathit{fold}_{&#92;mathsf{H}}(g))) &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{id}&#92;,&#92;mathit{prune} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{&#92;(g = &#92;mathit{maybe}&#92;,b&#92;,f&#92;)} &#92;} &#92;&#92; &amp; &#92;mathord{&#92;oplus/} &#92;cdot &#92;mathit{opt}&#92;,b &#92;cdot &#92;mathsf{M}&#92;,f &#92;cdot &#92;delta_2 &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{id}&#92;,(&#92;mathsf{M}(&#92;mathit{fold}_{&#92;mathsf{H}}(g))) &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{id}&#92;,&#92;mathit{prune} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{&#92;({&#92;oplus/} &#92;cdot &#92;mathit{opt}&#92;,b = (b&#92;oplus) &#92;cdot {&#92;oplus/}&#92;)} &#92;} &#92;&#92; &amp; (b&#92;oplus) &#92;cdot {&#92;oplus/} &#92;cdot &#92;mathsf{M}&#92;,f &#92;cdot &#92;delta_2 &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{id}&#92;,(&#92;mathsf{M}(&#92;mathit{fold}_{&#92;mathsf{H}}(g))) &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{id}&#92;,&#92;mathit{prune} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{distributivity: &#92;({&#92;oplus/} &#92;cdot &#92;mathsf{M}&#92;,f &#92;cdot &#92;delta_2 = f &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{id}&#92;,(&#92;oplus/)&#92;)} &#92;} &#92;&#92; &amp; (b&#92;oplus) &#92;cdot f &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{id}&#92;,(&#92;oplus/) &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{id}&#92;,(&#92;mathsf{M}(&#92;mathit{fold}_{&#92;mathsf{H}}(g))) &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{id}&#92;,&#92;mathit{prune} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{functors} &#92;} &#92;&#92; &amp; (b&#92;oplus) &#92;cdot f &#92;cdot &#92;mathsf{F}&#92;,&#92;mathit{id}&#92;,({&#92;oplus/} &#92;cdot &#92;mathsf{M}(&#92;mathit{fold}_{&#92;mathsf{H}}(g)) &#92;cdot &#92;mathit{prune}) &#92;end{array} ' class='latex' />
</p></blockquote>
<p> Therefore, </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%7B%5Coplus%2F%7D+%5Ccdot+%5Cmathsf%7BM%7D%28%5Cmathit%7Bfold%7D_%7B%5Cmathsf%7BH%7D%7D%28%5Cmathit%7Bmaybe%7D%5C%2Cb%5C%2Cf%29%29+%5Ccdot+%5Cmathit%7Bprune%7D+%3D+%5Cmathit%7Bfold%7D_%7B%5Cmathsf%7BF%7D%7D%28%28b%5Coplus%29+%5Ccdot+f%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  {&#92;oplus/} &#92;cdot &#92;mathsf{M}(&#92;mathit{fold}_{&#92;mathsf{H}}(&#92;mathit{maybe}&#92;,b&#92;,f)) &#92;cdot &#92;mathit{prune} = &#92;mathit{fold}_{&#92;mathsf{F}}((b&#92;oplus) &#92;cdot f) ' title='&#92;displaystyle  {&#92;oplus/} &#92;cdot &#92;mathsf{M}(&#92;mathit{fold}_{&#92;mathsf{H}}(&#92;mathit{maybe}&#92;,b&#92;,f)) &#92;cdot &#92;mathit{prune} = &#92;mathit{fold}_{&#92;mathsf{F}}((b&#92;oplus) &#92;cdot f) ' class='latex' />
</p></blockquote>
<p> (Curiously, it doesn&#8217;t seem to matter what value is chosen for <img src='http://s0.wp.com/latex.php?latex=%7Bb%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{b}' title='{b}' class='latex' />.)</p>
<p><h2> Maximum segment sum, datatype-generically </h2>
<p>
We&#8217;re nearly there. We start with the traversable shape bifunctor <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />, a collection monad <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BM%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{M}}' title='{&#92;mathsf{M}}' class='latex' />, and a distributive law <img src='http://s0.wp.com/latex.php?latex=%7B%5Cdelta_2+%3A%3A+%28%5Cmathsf%7BF%7D%5C%2C%5Calpha%29%5Cmathsf%7BM%7D+%5Cmathbin%7B%5Cstackrel%7B.%7D%7B%5Cto%7D%7D+%5Cmathsf%7BM%7D%28%5Cmathsf%7BF%7D%5Calpha%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;delta_2 :: (&#92;mathsf{F}&#92;,&#92;alpha)&#92;mathsf{M} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{M}(&#92;mathsf{F}&#92;alpha)}' title='{&#92;delta_2 :: (&#92;mathsf{F}&#92;,&#92;alpha)&#92;mathsf{M} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{M}(&#92;mathsf{F}&#92;alpha)}' class='latex' />. We are given an <img src='http://s0.wp.com/latex.php?latex=%7B%28%5Cmathsf%7BF%7D%5C%2C%5Calpha%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(&#92;mathsf{F}&#92;,&#92;alpha)}' title='{(&#92;mathsf{F}&#92;,&#92;alpha)}' class='latex' />-algebra <img src='http://s0.wp.com/latex.php?latex=%7B%28%5Cbeta%2Cf%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(&#92;beta,f)}' title='{(&#92;beta,f)}' class='latex' />, an additional element <img src='http://s0.wp.com/latex.php?latex=%7Bb+%3A%3A+%5Cbeta%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{b :: &#92;beta}' title='{b :: &#92;beta}' class='latex' />, and a <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BM%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{M}}' title='{&#92;mathsf{M}}' class='latex' />-algebra <img src='http://s0.wp.com/latex.php?latex=%7B%28%5Cbeta%2C%7B%5Coplus%2F%7D%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(&#92;beta,{&#92;oplus/})}' title='{(&#92;beta,{&#92;oplus/})}' class='latex' />, such that <img src='http://s0.wp.com/latex.php?latex=%7Bf%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f}' title='{f}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B%5Coplus%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;oplus}' title='{&#92;oplus}' class='latex' /> take constant time and <img src='http://s0.wp.com/latex.php?latex=%7Bf%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f}' title='{f}' class='latex' /> distributes over <img src='http://s0.wp.com/latex.php?latex=%7B%5Coplus%2F%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;oplus/}' title='{&#92;oplus/}' class='latex' /> in the sense above. Then </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%7B%5Coplus%2F%7D+%5Ccdot+%5Cmathsf%7BM%7D%28%5Cmathit%7Bfold%7D_%7B%5Cmathsf%7BH%7D%7D%28%5Cmathit%7Bmaybe%7D%5C%2Cb%5C%2Cf%29%29+%5Ccdot+%5Cmathit%7Bsegs%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  {&#92;oplus/} &#92;cdot &#92;mathsf{M}(&#92;mathit{fold}_{&#92;mathsf{H}}(&#92;mathit{maybe}&#92;,b&#92;,f)) &#92;cdot &#92;mathit{segs} ' title='&#92;displaystyle  {&#92;oplus/} &#92;cdot &#92;mathsf{M}(&#92;mathit{fold}_{&#92;mathsf{H}}(&#92;mathit{maybe}&#92;,b&#92;,f)) &#92;cdot &#92;mathit{segs} ' class='latex' />
</p></blockquote>
<p> can be computed in linear time, where </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathit%7Bsegs%7D+%3D+%5Cmathit%7Bjoin%7D+%5Ccdot+%5Cmathsf%7BM%7D%5C%2C%5Cmathit%7Bprune%7D+%5Ccdot+%5Cmathit%7Bcontents%7D_%7B%5Cmathsf%7BL%7D%7D+%5Ccdot+%5Cmathit%7Bsubterms%7D+%3A%3A+%5Cmu%28%5Cmathsf%7BF%7D%5C%2C%5Calpha%29+%5Crightarrow+%5Cmathsf%7BM%7D%28%5Cmu%28%5Cmathsf%7BH%7D%5C%2C%5Calpha%29%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathit{segs} = &#92;mathit{join} &#92;cdot &#92;mathsf{M}&#92;,&#92;mathit{prune} &#92;cdot &#92;mathit{contents}_{&#92;mathsf{L}} &#92;cdot &#92;mathit{subterms} :: &#92;mu(&#92;mathsf{F}&#92;,&#92;alpha) &#92;rightarrow &#92;mathsf{M}(&#92;mu(&#92;mathsf{H}&#92;,&#92;alpha)) ' title='&#92;displaystyle  &#92;mathit{segs} = &#92;mathit{join} &#92;cdot &#92;mathsf{M}&#92;,&#92;mathit{prune} &#92;cdot &#92;mathit{contents}_{&#92;mathsf{L}} &#92;cdot &#92;mathit{subterms} :: &#92;mu(&#92;mathsf{F}&#92;,&#92;alpha) &#92;rightarrow &#92;mathsf{M}(&#92;mu(&#92;mathsf{H}&#92;,&#92;alpha)) ' class='latex' />
</p></blockquote>
<p> and where </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathit%7Bcontents%7D_%7B%5Cmathsf%7BL%7D%7D+%3A%3A+%5Cmathsf%7BL%7D+%5Cmathbin%7B%5Cstackrel%7B.%7D%7B%5Cto%7D%7D+%5Cmathsf%7BM%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathit{contents}_{&#92;mathsf{L}} :: &#92;mathsf{L} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{M} ' title='&#92;displaystyle  &#92;mathit{contents}_{&#92;mathsf{L}} :: &#92;mathsf{L} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{M} ' class='latex' />
</p></blockquote>
<p> computes the contents of an <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BL%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{L}}' title='{&#92;mathsf{L}}' class='latex' />-structure (which, like <img src='http://s0.wp.com/latex.php?latex=%7B%5Cdelta_2%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;delta_2}' title='{&#92;delta_2}' class='latex' />, can be defined using the traversability of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />). Here&#8217;s the calculation: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Bll%7D+%26+%5Cmathord%7B%5Coplus%2F%7D+%5Ccdot+%5Cmathsf%7BM%7D%28%5Cmathit%7Bfold%7D_%7B%5Cmathsf%7BH%7D%7D%28%5Cmathit%7Bmaybe%7D%5C%2Cb%5C%2Cf%29%29+%5Ccdot+%5Cmathit%7Bsegs%7D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bdefinition+of+%5C%28%5Cmathit%7Bsegs%7D%5C%29%7D+%5C%7D+%5C%5C+%26+%5Cmathord%7B%5Coplus%2F%7D+%5Ccdot+%5Cmathsf%7BM%7D%28%5Cmathit%7Bfold%7D_%7B%5Cmathsf%7BH%7D%7D%28%5Cmathit%7Bmaybe%7D%5C%2Cb%5C%2Cf%29%29+%5Ccdot+%5Cmathit%7Bjoin%7D+%5Ccdot+%5Cmathsf%7BM%7D%5C%2C%5Cmathit%7Bprune%7D+%5Ccdot+%5Cmathit%7Bcontents%7D_%7B%5Cmathsf%7BL%7D%7D+%5Ccdot+%5Cmathit%7Bsubterms%7D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bnaturality+of+%5C%28%5Cmathit%7Bjoin%7D+%3A%3A+%5Cmathsf%7BM%7D%5Cmathsf%7BM%7D+%5Cmathbin%7B%5Cstackrel%7B.%7D%7B%5Cto%7D%7D+%5Cmathsf%7BM%7D%5C%29%3B+functors%7D+%5C%7D+%5C%5C+%26+%5Cmathord%7B%5Coplus%2F%7D+%5Ccdot+%5Cmathit%7Bjoin%7D+%5Ccdot+%5Cmathsf%7BM%7D%28%5Cmathsf%7BM%7D%28%5Cmathit%7Bfold%7D_%7B%5Cmathsf%7BH%7D%7D%28%5Cmathit%7Bmaybe%7D%5C%2Cb%5C%2Cf%29%29+%5Ccdot%5Cmathit%7Bprune%7D%29+%5Ccdot+%5Cmathit%7Bcontents%7D_%7B%5Cmathsf%7BL%7D%7D+%5Ccdot+%5Cmathit%7Bsubterms%7D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7B%5C%28%5Coplus%2F%5C%29+is+an+%5C%28%5Cmathsf%7BM%7D%5C%29-algebra%3B+functors%7D+%5C%7D+%5C%5C+%26+%5Cmathord%7B%5Coplus%2F%7D+%5Ccdot+%5Cmathsf%7BM%7D%28%7B%5Coplus%2F%7D+%5Ccdot+%5Cmathsf%7BM%7D%28%5Cmathit%7Bfold%7D_%7B%5Cmathsf%7BH%7D%7D%28%5Cmathit%7Bmaybe%7D%5C%2Cb%5C%2Cf%29%29+%5Ccdot%5Cmathit%7Bprune%7D%29+%5Ccdot+%5Cmathit%7Bcontents%7D_%7B%5Cmathsf%7BL%7D%7D+%5Ccdot+%5Cmathit%7Bsubterms%7D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bnaturality+of+%5C%28%5Cmathit%7Bcontents%7D+%3A%3A+%5Cmathsf%7BL%7D+%5Cmathbin%7B%5Cstackrel%7B.%7D%7B%5Cto%7D%7D+%5Cmathsf%7BM%7D%5C%29%7D+%5C%7D+%5C%5C+%26+%5Cmathord%7B%5Coplus%2F%7D+%5Ccdot+%5Cmathit%7Bcontents%7D_%7B%5Cmathsf%7BL%7D%7D+%5Ccdot+%5Cmathsf%7BL%7D%28%7B%5Coplus%2F%7D+%5Ccdot+%5Cmathsf%7BM%7D%28%5Cmathit%7Bfold%7D_%7B%5Cmathsf%7BH%7D%7D%28%5Cmathit%7Bmaybe%7D%5C%2Cb%5C%2Cf%29%29+%5Ccdot%5Cmathit%7Bprune%7D%29+%5Ccdot+%5Cmathit%7Bsubterms%7D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7BHorner%27s+Rule%7D+%5C%7D+%5C%5C+%26+%5Cmathord%7B%5Coplus%2F%7D+%5Ccdot+%5Cmathit%7Bcontents%7D_%7B%5Cmathsf%7BL%7D%7D+%5Ccdot+%5Cmathsf%7BL%7D%28%5Cmathit%7Bfold%7D_%7B%5Cmathsf%7BF%7D%7D%28%28b%5Coplus%29%5Ccdot+f%29%29+%5Ccdot+%5Cmathit%7Bsubterms%7D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7BScan+Lemma%7D+%5C%7D+%5C%5C+%26+%5Cmathord%7B%5Coplus%2F%7D+%5Ccdot+%5Cmathit%7Bcontents%7D_%7B%5Cmathsf%7BL%7D%7D+%5Ccdot+%5Cmathit%7Bscan%7D_%7B%5Cmathsf%7BF%7D%7D%28%28b%5Coplus%29%5Ccdot+f%29+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;mathord{&#92;oplus/} &#92;cdot &#92;mathsf{M}(&#92;mathit{fold}_{&#92;mathsf{H}}(&#92;mathit{maybe}&#92;,b&#92;,f)) &#92;cdot &#92;mathit{segs} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{definition of &#92;(&#92;mathit{segs}&#92;)} &#92;} &#92;&#92; &amp; &#92;mathord{&#92;oplus/} &#92;cdot &#92;mathsf{M}(&#92;mathit{fold}_{&#92;mathsf{H}}(&#92;mathit{maybe}&#92;,b&#92;,f)) &#92;cdot &#92;mathit{join} &#92;cdot &#92;mathsf{M}&#92;,&#92;mathit{prune} &#92;cdot &#92;mathit{contents}_{&#92;mathsf{L}} &#92;cdot &#92;mathit{subterms} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{naturality of &#92;(&#92;mathit{join} :: &#92;mathsf{M}&#92;mathsf{M} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{M}&#92;); functors} &#92;} &#92;&#92; &amp; &#92;mathord{&#92;oplus/} &#92;cdot &#92;mathit{join} &#92;cdot &#92;mathsf{M}(&#92;mathsf{M}(&#92;mathit{fold}_{&#92;mathsf{H}}(&#92;mathit{maybe}&#92;,b&#92;,f)) &#92;cdot&#92;mathit{prune}) &#92;cdot &#92;mathit{contents}_{&#92;mathsf{L}} &#92;cdot &#92;mathit{subterms} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{&#92;(&#92;oplus/&#92;) is an &#92;(&#92;mathsf{M}&#92;)-algebra; functors} &#92;} &#92;&#92; &amp; &#92;mathord{&#92;oplus/} &#92;cdot &#92;mathsf{M}({&#92;oplus/} &#92;cdot &#92;mathsf{M}(&#92;mathit{fold}_{&#92;mathsf{H}}(&#92;mathit{maybe}&#92;,b&#92;,f)) &#92;cdot&#92;mathit{prune}) &#92;cdot &#92;mathit{contents}_{&#92;mathsf{L}} &#92;cdot &#92;mathit{subterms} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{naturality of &#92;(&#92;mathit{contents} :: &#92;mathsf{L} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{M}&#92;)} &#92;} &#92;&#92; &amp; &#92;mathord{&#92;oplus/} &#92;cdot &#92;mathit{contents}_{&#92;mathsf{L}} &#92;cdot &#92;mathsf{L}({&#92;oplus/} &#92;cdot &#92;mathsf{M}(&#92;mathit{fold}_{&#92;mathsf{H}}(&#92;mathit{maybe}&#92;,b&#92;,f)) &#92;cdot&#92;mathit{prune}) &#92;cdot &#92;mathit{subterms} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{Horner&#039;s Rule} &#92;} &#92;&#92; &amp; &#92;mathord{&#92;oplus/} &#92;cdot &#92;mathit{contents}_{&#92;mathsf{L}} &#92;cdot &#92;mathsf{L}(&#92;mathit{fold}_{&#92;mathsf{F}}((b&#92;oplus)&#92;cdot f)) &#92;cdot &#92;mathit{subterms} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{Scan Lemma} &#92;} &#92;&#92; &amp; &#92;mathord{&#92;oplus/} &#92;cdot &#92;mathit{contents}_{&#92;mathsf{L}} &#92;cdot &#92;mathit{scan}_{&#92;mathsf{F}}((b&#92;oplus)&#92;cdot f) &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;mathord{&#92;oplus/} &#92;cdot &#92;mathsf{M}(&#92;mathit{fold}_{&#92;mathsf{H}}(&#92;mathit{maybe}&#92;,b&#92;,f)) &#92;cdot &#92;mathit{segs} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{definition of &#92;(&#92;mathit{segs}&#92;)} &#92;} &#92;&#92; &amp; &#92;mathord{&#92;oplus/} &#92;cdot &#92;mathsf{M}(&#92;mathit{fold}_{&#92;mathsf{H}}(&#92;mathit{maybe}&#92;,b&#92;,f)) &#92;cdot &#92;mathit{join} &#92;cdot &#92;mathsf{M}&#92;,&#92;mathit{prune} &#92;cdot &#92;mathit{contents}_{&#92;mathsf{L}} &#92;cdot &#92;mathit{subterms} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{naturality of &#92;(&#92;mathit{join} :: &#92;mathsf{M}&#92;mathsf{M} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{M}&#92;); functors} &#92;} &#92;&#92; &amp; &#92;mathord{&#92;oplus/} &#92;cdot &#92;mathit{join} &#92;cdot &#92;mathsf{M}(&#92;mathsf{M}(&#92;mathit{fold}_{&#92;mathsf{H}}(&#92;mathit{maybe}&#92;,b&#92;,f)) &#92;cdot&#92;mathit{prune}) &#92;cdot &#92;mathit{contents}_{&#92;mathsf{L}} &#92;cdot &#92;mathit{subterms} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{&#92;(&#92;oplus/&#92;) is an &#92;(&#92;mathsf{M}&#92;)-algebra; functors} &#92;} &#92;&#92; &amp; &#92;mathord{&#92;oplus/} &#92;cdot &#92;mathsf{M}({&#92;oplus/} &#92;cdot &#92;mathsf{M}(&#92;mathit{fold}_{&#92;mathsf{H}}(&#92;mathit{maybe}&#92;,b&#92;,f)) &#92;cdot&#92;mathit{prune}) &#92;cdot &#92;mathit{contents}_{&#92;mathsf{L}} &#92;cdot &#92;mathit{subterms} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{naturality of &#92;(&#92;mathit{contents} :: &#92;mathsf{L} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{M}&#92;)} &#92;} &#92;&#92; &amp; &#92;mathord{&#92;oplus/} &#92;cdot &#92;mathit{contents}_{&#92;mathsf{L}} &#92;cdot &#92;mathsf{L}({&#92;oplus/} &#92;cdot &#92;mathsf{M}(&#92;mathit{fold}_{&#92;mathsf{H}}(&#92;mathit{maybe}&#92;,b&#92;,f)) &#92;cdot&#92;mathit{prune}) &#92;cdot &#92;mathit{subterms} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{Horner&#039;s Rule} &#92;} &#92;&#92; &amp; &#92;mathord{&#92;oplus/} &#92;cdot &#92;mathit{contents}_{&#92;mathsf{L}} &#92;cdot &#92;mathsf{L}(&#92;mathit{fold}_{&#92;mathsf{F}}((b&#92;oplus)&#92;cdot f)) &#92;cdot &#92;mathit{subterms} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{Scan Lemma} &#92;} &#92;&#92; &amp; &#92;mathord{&#92;oplus/} &#92;cdot &#92;mathit{contents}_{&#92;mathsf{L}} &#92;cdot &#92;mathit{scan}_{&#92;mathsf{F}}((b&#92;oplus)&#92;cdot f) &#92;end{array} ' class='latex' />
</p></blockquote>
<p> The scan can be computed in linear time, because its body takes constant time; moreover, the &#8220;sum&#8221; <img src='http://s0.wp.com/latex.php?latex=%7B%5Coplus%2F%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;oplus/}' title='{&#92;oplus/}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bcontents%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{contents}}' title='{&#92;mathit{contents}}' class='latex' /> can also be computed in linear time (and what&#8217;s more, they can be fused into a single pass). </p>
<p>
For example, with <img src='http://s0.wp.com/latex.php?latex=%7Bf+%3A%3A+%5Cmathsf%7BF%7D%5C%2C%7B%5Cmathbb+Z%7D%5C%2C%7B%5Cmathbb+Z%7D+%5Crightarrow+%7B%5Cmathbb+Z%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f :: &#92;mathsf{F}&#92;,{&#92;mathbb Z}&#92;,{&#92;mathbb Z} &#92;rightarrow {&#92;mathbb Z}}' title='{f :: &#92;mathsf{F}&#92;,{&#92;mathbb Z}&#92;,{&#92;mathbb Z} &#92;rightarrow {&#92;mathbb Z}}' class='latex' /> adding all the integers in an <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />-structure, <img src='http://s0.wp.com/latex.php?latex=%7Bb+%3D+0+%3A%3A+%7B%5Cmathbb+Z%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{b = 0 :: {&#92;mathbb Z}}' title='{b = 0 :: {&#92;mathbb Z}}' class='latex' />, and <img src='http://s0.wp.com/latex.php?latex=%7B%7B%5Coplus%7D+%3A%3A+%7B%5Cmathbb+Z%7D%5Ctimes%7B%5Cmathbb+Z%7D+%5Crightarrow+%7B%5Cmathbb+Z%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{{&#92;oplus} :: {&#92;mathbb Z}&#92;times{&#92;mathbb Z} &#92;rightarrow {&#92;mathbb Z}}' title='{{&#92;oplus} :: {&#92;mathbb Z}&#92;times{&#92;mathbb Z} &#92;rightarrow {&#92;mathbb Z}}' class='latex' /> returning the greater of two integers, we get a datatype-generic version of the linear-time maximum segment sum algorithm.</p>
<p><h2> Monads versus relations </h2>
<p>
As the title of their paper suggests, Bird &amp; co carried out their development using the relational approach set out in the <i>Algebra of Programming</i> book; for example, their version of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bprune%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{prune}}' title='{&#92;mathit{prune}}' class='latex' /> is a relation between data structures and their prunings, rather than being a function that takes a structure and returns the collection of all its prunings. There&#8217;s a well-known isomorphism between relations and set-valued functions, so their relational approach roughly looks equivalent to the monadic one I&#8217;ve taken.</p>
<p>
I&#8217;ve known their paper well for over a decade (I made extensive use of the &#8220;labelled variant&#8221; construction in my own papers on generic downwards accumulations), but I&#8217;ve only just noticed that although they discuss the maximum segment sum problem, they don&#8217;t discuss problems based on other semirings, such as the obvious one of integers with addition and multiplication&mdash;which is, after all, the origin of Horner&#8217;s Rule. Why not? It turns out that the relational approach doesn&#8217;t work in that case!</p>
<p>
There&#8217;s a hidden condition in the calculation, which relates back to our earlier comment about which collection monad&mdash;finite sets, finite bags, lists, etc&mdash;to use. When <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BM%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{M}}' title='{&#92;mathsf{M}}' class='latex' /> is the set monad, distribution over choice (<img src='http://s0.wp.com/latex.php?latex=%7B%5Coplus+%2F+%28x+%5Cmathbin%7B%5Cunderline%7B%5Csmash%7B%5Cmathit%7Bmplus%7D%7D%7D%7D+y%29+%3D+%28%5Coplus%2Fx%29+%5Coplus+%28%5Coplus%2Fy%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;oplus / (x &#92;mathbin{&#92;underline{&#92;smash{&#92;mathit{mplus}}}} y) = (&#92;oplus/x) &#92;oplus (&#92;oplus/y)}' title='{&#92;oplus / (x &#92;mathbin{&#92;underline{&#92;smash{&#92;mathit{mplus}}}} y) = (&#92;oplus/x) &#92;oplus (&#92;oplus/y)}' class='latex' />)&mdash;and consequently the condition <img src='http://s0.wp.com/latex.php?latex=%7B%7B%5Coplus%2F%7D+%5Ccdot+%5Cmathit%7Bopt%7D%5C%2Cb+%3D+%28b%5Coplus%29+%5Ccdot+%7B%5Coplus%2F%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{{&#92;oplus/} &#92;cdot &#92;mathit{opt}&#92;,b = (b&#92;oplus) &#92;cdot {&#92;oplus/}}' title='{{&#92;oplus/} &#92;cdot &#92;mathit{opt}&#92;,b = (b&#92;oplus) &#92;cdot {&#92;oplus/}}' class='latex' /> that we used in proving Horner&#8217;s Rule&mdash;require <img src='http://s0.wp.com/latex.php?latex=%7B%5Coplus%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;oplus}' title='{&#92;oplus}' class='latex' /> to be idempotent, because <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bmplus%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{mplus}}' title='{&#92;mathit{mplus}}' class='latex' /> itself is idempotent; but addition is not idempotent. For the same reason, the distributivity property does not hold for addition with the set monad. But everything does work out for the bag monad, for which <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bmplus%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{mplus}}' title='{&#92;mathit{mplus}}' class='latex' /> is not idempotent. The bag monad models a flavour of nondeterminism in which multiplicity of results matters&mdash;as it does for the sum-of-products instance of the problem, when two copies of the same segment should be treated differently from just one copy. Similarly, if the order of results matters&mdash;if, for example, we were looking for the &#8220;first&#8221; solution&mdash;then we would have to use the list monad rather than bags or sets. Seen from a monadic perspective, the relational approach is <em>programming with just one monad</em>, namely the set monad; if that monad doesn&#8217;t capture your effects faithfully, you&#8217;re stuck.</p>
<p>
(On the other hand, there are aspects of the problem that work much better relationally. We have carefully used <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmax%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;max}' title='{&#92;max}' class='latex' /> only for a linear order, namely the usual ordering of the integers. A partial order is more awkward monadically, because there need not be a unique maximal value. For example, it is not so easy to compute a segment with maximal sum, unless we refine the sum ordering on segments to make it once more a linear order; relationally, this works out perfectly straightforwardly. We can try the same trick of turning the relation &#8220;maximal under a partial order&#8221; into the collection-valued function &#8220;all maxima under a partial order&#8221;, but I fear that the equivalent trick on the ordering itself&mdash;turning the relation &#8220;<img src='http://s0.wp.com/latex.php?latex=%7B%3C%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&lt;}' title='{&lt;}' class='latex' />&#8221; into the collection-valued function &#8220;all values less than this one&#8221;&mdash;runs into problems from taking us outside the world of <em>finite</em> nondeterminism.)</p>
<p>
<br />  <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=patternsinfp.wordpress.com&#038;blog=15593982&#038;post=197&#038;subd=patternsinfp&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://patternsinfp.wordpress.com/2011/05/05/horners-rule/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/39d7e0397cba13d1494d9a48c1f42167?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jeremygibbons</media:title>
		</media:content>

		<media:content url="http://patternsinfp.files.wordpress.com/2011/05/horner-distributivity.png" medium="image" />
	</item>
		<item>
		<title>Morality and temptation</title>
		<link>http://patternsinfp.wordpress.com/2011/04/11/morality-and-temptation/</link>
		<comments>http://patternsinfp.wordpress.com/2011/04/11/morality-and-temptation/#comments</comments>
		<pubDate>Mon, 11 Apr 2011 12:58:23 +0000</pubDate>
		<dc:creator>jeremygibbons</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://patternsinfp.wordpress.com/?p=179</guid>
		<description><![CDATA[Inspired by Bob Harper&#8217;s recent postings, I too have a confession to make. I know what is morally right; but sometimes the temptation is too great, and my resolve is weak, and I lapse. Fast and loose reasoning may excuse &#8230; <a href="http://patternsinfp.wordpress.com/2011/04/11/morality-and-temptation/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=patternsinfp.wordpress.com&#038;blog=15593982&#038;post=179&#038;subd=patternsinfp&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>
 Inspired by Bob Harper&#8217;s <a href="http://existentialtype.wordpress.com/2011/03/27/the-holy-trinity/">recent</a> <a href="http://existentialtype.wordpress.com/2011/04/02/functions-are-values/">postings</a>, I too have a confession to make. I know what is morally right; but sometimes the temptation is too great, and my resolve is weak, and I lapse. <a href="http://dx.doi.org/10.1145/1111037.1111056">Fast and loose reasoning</a> may excuse me, but my conscience would be clearer if I could remain pure in the first place.</p>
<p><h2> Initial algebras, final coalgebras </h2>
<p>
We know and love initial algebras, because of the ease of reasoning with their universal properties. We can tell a simple story about recursive programs, solely in terms of sets and total functions. As we discussed in the <a href="http://patternsinfp.wordpress.com/2011/03/31/extreme-solutions/">previous post</a>, given a functor <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D+%3A+%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D+%5Crightarrow+%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F} : &#92;mathbb{S}&#92;mathrm{et} &#92;rightarrow &#92;mathbb{S}&#92;mathrm{et}}' title='{&#92;mathsf{F} : &#92;mathbb{S}&#92;mathrm{et} &#92;rightarrow &#92;mathbb{S}&#92;mathrm{et}}' class='latex' />, an <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />-algebra is a pair <img src='http://s0.wp.com/latex.php?latex=%7B%28X%2Cf%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(X,f)}' title='{(X,f)}' class='latex' /> consisting of an object <img src='http://s0.wp.com/latex.php?latex=%7BX%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{X}' title='{X}' class='latex' /> and an arrow <img src='http://s0.wp.com/latex.php?latex=%7Bf+%3A+%5Cmathsf%7BF%7D%28X%29+%5Crightarrow+X%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f : &#92;mathsf{F}(X) &#92;rightarrow X}' title='{f : &#92;mathsf{F}(X) &#92;rightarrow X}' class='latex' />. A <em>homomorphism</em> between <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />-algebras <img src='http://s0.wp.com/latex.php?latex=%7B%28X%2Cf%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(X,f)}' title='{(X,f)}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B%28Y%2Cg%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(Y,g)}' title='{(Y,g)}' class='latex' /> is an arrow <img src='http://s0.wp.com/latex.php?latex=%7Bh+%3A+X+%5Crightarrow+Y%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h : X &#92;rightarrow Y}' title='{h : X &#92;rightarrow Y}' class='latex' /> such that <img src='http://s0.wp.com/latex.php?latex=%7Bh+%5Ccdot+f+%3D+g+%5Ccdot+%5Cmathsf%7BF%7D%28h%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h &#92;cdot f = g &#92;cdot &#92;mathsf{F}(h)}' title='{h &#92;cdot f = g &#92;cdot &#92;mathsf{F}(h)}' class='latex' />:
<p align="center"><img width="150" src="http://patternsinfp.files.wordpress.com/2011/04/morality-homomorphism.png?w=150"></p>
<p>  The <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />-algebra <img src='http://s0.wp.com/latex.php?latex=%7B%28X%2Cf%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(X,f)}' title='{(X,f)}' class='latex' /> is <em>initial</em> iff there is a unique such <img src='http://s0.wp.com/latex.php?latex=%7Bh%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h}' title='{h}' class='latex' /> for each <img src='http://s0.wp.com/latex.php?latex=%7B%28Y%2Cg%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(Y,g)}' title='{(Y,g)}' class='latex' />; for well-behaved functors <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />, such as the polynomial functors on <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{S}&#92;mathrm{et}}' title='{&#92;mathbb{S}&#92;mathrm{et}}' class='latex' />, an initial algebra always exists. We conventionally write &#8220;<img src='http://s0.wp.com/latex.php?latex=%7B%28%5Cmu%5Cmathsf%7BF%7D%2C%5Cmathit%7Bin%7D%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(&#92;mu&#92;mathsf{F},&#92;mathit{in})}' title='{(&#92;mu&#92;mathsf{F},&#92;mathit{in})}' class='latex' />&#8221; for the initial algebra, and &#8220;<img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfold%7D_%7B%5Cmathsf%7BF%7D%7D%28g%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{fold}_{&#92;mathsf{F}}(g)}' title='{&#92;mathit{fold}_{&#92;mathsf{F}}(g)}' class='latex' />&#8221; for the unique homomorphism <img src='http://s0.wp.com/latex.php?latex=%7Bh%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h}' title='{h}' class='latex' /> to another <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />-algebra <img src='http://s0.wp.com/latex.php?latex=%7B%28Y%2Cg%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(Y,g)}' title='{(Y,g)}' class='latex' />. (In <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{S}&#92;mathrm{et}}' title='{&#92;mathbb{S}&#92;mathrm{et}}' class='latex' />, initial algebras correspond to datatypes of finite recursive data structures.)
<p align="center"><img width="150" src="http://patternsinfp.files.wordpress.com/2011/04/morality-initial.png?w=150"></p>
<p>  The uniqueness of the solution is captured in the universal property: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++h+%3D+%5Cmathit%7Bfold%7D%28g%29+%5CLeftrightarrow+h+%5Ccdot+%5Cmathit%7Bin%7D+%3D+g+%5Ccdot+%5Cmathsf%7BF%7D%28h%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  h = &#92;mathit{fold}(g) &#92;Leftrightarrow h &#92;cdot &#92;mathit{in} = g &#92;cdot &#92;mathsf{F}(h) ' title='&#92;displaystyle  h = &#92;mathit{fold}(g) &#92;Leftrightarrow h &#92;cdot &#92;mathit{in} = g &#92;cdot &#92;mathsf{F}(h) ' class='latex' />
</p></blockquote>
<p> In words, <img src='http://s0.wp.com/latex.php?latex=%7Bh%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h}' title='{h}' class='latex' /> is this fold iff <img src='http://s0.wp.com/latex.php?latex=%7Bh%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h}' title='{h}' class='latex' /> satisfies the defining equation for the fold.</p>
<p>
The universal property is crucial. For one thing, the homomorphism equation is a very convenient style in which to define a function; it&#8217;s the datatype-generic abstraction of the familiar pattern for defining functions on lists: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+h%5C%2C%5B%5D+%26%3D%26+e+%5C%5C+h%5C%2C%28x%3A%5Cmathit%7Bxs%7D%29+%26%3D%26+f%5C%2Cx%5C%2C%28h%5C%2C%5Cmathit%7Bxs%7D%29+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} h&#92;,[] &amp;=&amp; e &#92;&#92; h&#92;,(x:&#92;mathit{xs}) &amp;=&amp; f&#92;,x&#92;,(h&#92;,&#92;mathit{xs}) &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} h&#92;,[] &amp;=&amp; e &#92;&#92; h&#92;,(x:&#92;mathit{xs}) &amp;=&amp; f&#92;,x&#92;,(h&#92;,&#92;mathit{xs}) &#92;end{array} ' class='latex' />
</p></blockquote>
<p> These two equations implicitly characterizing <img src='http://s0.wp.com/latex.php?latex=%7Bh%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h}' title='{h}' class='latex' /> are much more comprehensible and manipulable than a single equation </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++h+%3D+%5Clambda+%5Cmathit%7Bxs%7D%5C%3B.%5C%3B+%5Ctextbf%7Bif%7D%5C%3B%5Cmathit%7Bnull%7D%5C%2C%5Cmathit%7Bxs%7D%5C%3B%5Ctextbf%7Bthen%7D%5C%3Be%5C%3B%5Ctextbf%7Belse%7D%5C%3Bf%5C%2C%28%5Cmathit%7Bhead%7D%5C%2C%5Cmathit%7Bxs%7D%29%5C%2C%28h%5C%2C%28%5Cmathit%7Btail%7D%5C%2C%5Cmathit%7Bxs%7D%29%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  h = &#92;lambda &#92;mathit{xs}&#92;;.&#92;; &#92;textbf{if}&#92;;&#92;mathit{null}&#92;,&#92;mathit{xs}&#92;;&#92;textbf{then}&#92;;e&#92;;&#92;textbf{else}&#92;;f&#92;,(&#92;mathit{head}&#92;,&#92;mathit{xs})&#92;,(h&#92;,(&#92;mathit{tail}&#92;,&#92;mathit{xs})) ' title='&#92;displaystyle  h = &#92;lambda &#92;mathit{xs}&#92;;.&#92;; &#92;textbf{if}&#92;;&#92;mathit{null}&#92;,&#92;mathit{xs}&#92;;&#92;textbf{then}&#92;;e&#92;;&#92;textbf{else}&#92;;f&#92;,(&#92;mathit{head}&#92;,&#92;mathit{xs})&#92;,(h&#92;,(&#92;mathit{tail}&#92;,&#92;mathit{xs})) ' class='latex' />
</p></blockquote>
<p> explicitly giving a value for <img src='http://s0.wp.com/latex.php?latex=%7Bh%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h}' title='{h}' class='latex' />. But how do we know that this assortment of two facts about <img src='http://s0.wp.com/latex.php?latex=%7Bh%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h}' title='{h}' class='latex' /> is enough to form a definition? Of course! A system of equations in this form has a <em>unique solution</em>. </p>
<p>
Moreover, the very expression of the uniqueness of the solution as an equivalence <img src='http://s0.wp.com/latex.php?latex=%7Bh+%3D+%5Cldots+%5CLeftrightarrow+%5Cldots%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h = &#92;ldots &#92;Leftrightarrow &#92;ldots}' title='{h = &#92;ldots &#92;Leftrightarrow &#92;ldots}' class='latex' /> provides many footholds for reasoning: </p>
<ul>
<li> Read as an implication from left to right, instantiating <img src='http://s0.wp.com/latex.php?latex=%7Bh%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h}' title='{h}' class='latex' /> to <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfold%7D%28g%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{fold}(g)}' title='{&#92;mathit{fold}(g)}' class='latex' /> to make the left-hand side trivially true, we get an <em>evaluation rule</em> for folds: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathit%7Bfold%7D%28g%29+%5Ccdot+%5Cmathit%7Bin%7D+%3D+g+%5Ccdot+%5Cmathsf%7BF%7D%28%5Cmathit%7Bfold%7D%28g%29%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathit{fold}(g) &#92;cdot &#92;mathit{in} = g &#92;cdot &#92;mathsf{F}(&#92;mathit{fold}(g)) ' title='&#92;displaystyle  &#92;mathit{fold}(g) &#92;cdot &#92;mathit{in} = g &#92;cdot &#92;mathsf{F}(&#92;mathit{fold}(g)) ' class='latex' />
</p></blockquote>
<li> Read as an implication from right to left, we get a proof rule for demonstrating that some complicated expression <img src='http://s0.wp.com/latex.php?latex=%7Bh%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h}' title='{h}' class='latex' /> is a fold:<br />
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++h+%3D+%5Cmathit%7Bfold%7D%28g%29+%5CLeftarrow+%5Cldots+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  h = &#92;mathit{fold}(g) &#92;Leftarrow &#92;ldots ' title='&#92;displaystyle  h = &#92;mathit{fold}(g) &#92;Leftarrow &#92;ldots ' class='latex' />
</p></blockquote>
<li> In particular, we can quickly see that the identity function is a fold:<br />
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Bll%7D+%26+%5Cmathit%7Bid%7D+%3D+%5Cmathit%7Bfold%7D%28g%29+%5C%5C+%5CLeftarrow+%26+%5Cqquad+%5C%7B+%5Cmbox%7Buniversal+property%7D+%5C%7D+%5C%5C+%26+%5Cmathit%7Bid%7D+%5Ccdot+%5Cmathit%7Bin%7D+%3D+g+%5Ccdot+%5Cmathsf%7BF%7D%28%5Cmathit%7Bid%7D%29+%5C%5C+%5CLeftrightarrow+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bidentities%7D+%5C%7D+%5C%5C+%26+%5Cmathit%7Bin%7D+%3D+g+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;mathit{id} = &#92;mathit{fold}(g) &#92;&#92; &#92;Leftarrow &amp; &#92;qquad &#92;{ &#92;mbox{universal property} &#92;} &#92;&#92; &amp; &#92;mathit{id} &#92;cdot &#92;mathit{in} = g &#92;cdot &#92;mathsf{F}(&#92;mathit{id}) &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{identities} &#92;} &#92;&#92; &amp; &#92;mathit{in} = g &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;mathit{id} = &#92;mathit{fold}(g) &#92;&#92; &#92;Leftarrow &amp; &#92;qquad &#92;{ &#92;mbox{universal property} &#92;} &#92;&#92; &amp; &#92;mathit{id} &#92;cdot &#92;mathit{in} = g &#92;cdot &#92;mathsf{F}(&#92;mathit{id}) &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{identities} &#92;} &#92;&#92; &amp; &#92;mathit{in} = g &#92;end{array} ' class='latex' />
</p></blockquote>
<p> so <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bid%7D+%3D+%5Cmathit%7Bfold%7D%28%5Cmathit%7Bin%7D%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{id} = &#92;mathit{fold}(&#92;mathit{in})}' title='{&#92;mathit{id} = &#92;mathit{fold}(&#92;mathit{in})}' class='latex' />. (In fact, this one&#8217;s an equivalence.)
<li> We get a very simple proof of a <em>fusion rule</em>, for combining a following function with a fold to make another fold: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Bll%7D+%26+h+%5Ccdot+%5Cmathit%7Bfold%7D%28f%29+%3D+%5Cmathit%7Bfold%7D%28g%29+%5C%5C+%5CLeftrightarrow+%26+%5Cqquad+%5C%7B+%5Cmbox%7Buniversal+property%7D+%5C%7D+%5C%5C+%26+h+%5Ccdot+%5Cmathit%7Bfold%7D%28f%29+%5Ccdot+%5Cmathit%7Bin%7D+%3D+g+%5Ccdot+%5Cmathsf%7BF%7D%28h+%5Ccdot+%5Cmathit%7Bfold%7D%28f%29%29+%5C%5C+%5CLeftrightarrow+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bevaluation+rule%2C+functors%7D+%5C%7D+%5C%5C+%26+h+%5Ccdot+f+%5Ccdot+%5Cmathsf%7BF%7D%28%5Cmathit%7Bfold%7D%28f%29%29+%3D+g+%5Ccdot+%5Cmathsf%7BF%7D%28h%29+%5Ccdot+%5Cmathsf%7BF%7D%28%5Cmathit%7Bfold%7D%28f%29%29+%5C%5C+%5CLeftarrow+%26+%5Cqquad+%5C%7B+%5Cmbox%7BLeibniz%7D+%5C%7D+%5C%5C+%26+h+%5Ccdot+f+%3D+g+%5Ccdot+%5Cmathsf%7BF%7D%28h%29+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{ll} &amp; h &#92;cdot &#92;mathit{fold}(f) = &#92;mathit{fold}(g) &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{universal property} &#92;} &#92;&#92; &amp; h &#92;cdot &#92;mathit{fold}(f) &#92;cdot &#92;mathit{in} = g &#92;cdot &#92;mathsf{F}(h &#92;cdot &#92;mathit{fold}(f)) &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{evaluation rule, functors} &#92;} &#92;&#92; &amp; h &#92;cdot f &#92;cdot &#92;mathsf{F}(&#92;mathit{fold}(f)) = g &#92;cdot &#92;mathsf{F}(h) &#92;cdot &#92;mathsf{F}(&#92;mathit{fold}(f)) &#92;&#92; &#92;Leftarrow &amp; &#92;qquad &#92;{ &#92;mbox{Leibniz} &#92;} &#92;&#92; &amp; h &#92;cdot f = g &#92;cdot &#92;mathsf{F}(h) &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{ll} &amp; h &#92;cdot &#92;mathit{fold}(f) = &#92;mathit{fold}(g) &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{universal property} &#92;} &#92;&#92; &amp; h &#92;cdot &#92;mathit{fold}(f) &#92;cdot &#92;mathit{in} = g &#92;cdot &#92;mathsf{F}(h &#92;cdot &#92;mathit{fold}(f)) &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{evaluation rule, functors} &#92;} &#92;&#92; &amp; h &#92;cdot f &#92;cdot &#92;mathsf{F}(&#92;mathit{fold}(f)) = g &#92;cdot &#92;mathsf{F}(h) &#92;cdot &#92;mathsf{F}(&#92;mathit{fold}(f)) &#92;&#92; &#92;Leftarrow &amp; &#92;qquad &#92;{ &#92;mbox{Leibniz} &#92;} &#92;&#92; &amp; h &#92;cdot f = g &#92;cdot &#92;mathsf{F}(h) &#92;end{array} ' class='latex' />
</p></blockquote>
<li> Using this, we can deduce <em>Lambek&#8217;s Lemma</em>, that the constructors <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bin%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{in}}' title='{&#92;mathit{in}}' class='latex' /> form an isomorphism. Supposing that there is a right inverse, and it is a fold, what must it look like?<br />
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Bll%7D+%26+%5Cmathit%7Bin%7D+%5Ccdot+%5Cmathit%7Bfold%7D%28f%29+%3D+%5Cmathit%7Bid%7D+%5C%5C+%5CLeftrightarrow+%26+%5Cqquad+%5C%7B+%5Cmbox%7B%5C%28%5Cmathit%7Bid%7D%5C%29+as+a+fold%7D+%5C%7D+%5C%5C+%26+%5Cmathit%7Bin%7D+%5Ccdot+%5Cmathit%7Bfold%7D%28f%29+%3D+%5Cmathit%7Bfold%7D%28%5Cmathit%7Bin%7D%29+%5C%5C+%5CLeftarrow+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bfusion%7D+%5C%7D+%5C%5C+%26+%5Cmathit%7Bin%7D+%5Ccdot+f+%3D+%5Cmathit%7Bin%7D+%5Ccdot+%5Cmathsf%7BF%7D%28%5Cmathit%7Bin%7D%29+%5C%5C+%5CLeftarrow+%26+%5Cqquad+%5C%7B+%5Cmbox%7BLeibniz%7D+%5C%7D+%5C%5C+%26+f+%3D+%5Cmathsf%7BF%7D%28%5Cmathit%7Bin%7D%29+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;mathit{in} &#92;cdot &#92;mathit{fold}(f) = &#92;mathit{id} &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{&#92;(&#92;mathit{id}&#92;) as a fold} &#92;} &#92;&#92; &amp; &#92;mathit{in} &#92;cdot &#92;mathit{fold}(f) = &#92;mathit{fold}(&#92;mathit{in}) &#92;&#92; &#92;Leftarrow &amp; &#92;qquad &#92;{ &#92;mbox{fusion} &#92;} &#92;&#92; &amp; &#92;mathit{in} &#92;cdot f = &#92;mathit{in} &#92;cdot &#92;mathsf{F}(&#92;mathit{in}) &#92;&#92; &#92;Leftarrow &amp; &#92;qquad &#92;{ &#92;mbox{Leibniz} &#92;} &#92;&#92; &amp; f = &#92;mathsf{F}(&#92;mathit{in}) &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;mathit{in} &#92;cdot &#92;mathit{fold}(f) = &#92;mathit{id} &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{&#92;(&#92;mathit{id}&#92;) as a fold} &#92;} &#92;&#92; &amp; &#92;mathit{in} &#92;cdot &#92;mathit{fold}(f) = &#92;mathit{fold}(&#92;mathit{in}) &#92;&#92; &#92;Leftarrow &amp; &#92;qquad &#92;{ &#92;mbox{fusion} &#92;} &#92;&#92; &amp; &#92;mathit{in} &#92;cdot f = &#92;mathit{in} &#92;cdot &#92;mathsf{F}(&#92;mathit{in}) &#92;&#92; &#92;Leftarrow &amp; &#92;qquad &#92;{ &#92;mbox{Leibniz} &#92;} &#92;&#92; &amp; f = &#92;mathsf{F}(&#92;mathit{in}) &#92;end{array} ' class='latex' />
</p></blockquote>
<p> So if we define <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bin%7D%5E%7B-1%7D+%3D+%5Cmathit%7Bfold%7D%28%5Cmathsf%7BF%7D%28%5Cmathit%7Bin%7D%29%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{in}^{-1} = &#92;mathit{fold}(&#92;mathsf{F}(&#92;mathit{in}))}' title='{&#92;mathit{in}^{-1} = &#92;mathit{fold}(&#92;mathsf{F}(&#92;mathit{in}))}' class='latex' />, we get <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bin%7D+%5Ccdot+%5Cmathit%7Bin%7D%5E%7B-1%7D+%3D+%5Cmathit%7Bid%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{in} &#92;cdot &#92;mathit{in}^{-1} = &#92;mathit{id}}' title='{&#92;mathit{in} &#92;cdot &#92;mathit{in}^{-1} = &#92;mathit{id}}' class='latex' />. We should also check the left inverse property: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Bll%7D+%26+%5Cmathit%7Bin%7D%5E%7B-1%7D+%5Ccdot+%5Cmathit%7Bin%7D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7B%5C%28%5Cmathit%7Bin%7D%5E%7B-1%7D%5C%29+as+a+fold%7D+%5C%7D+%5C%5C+%26+%5Cmathit%7Bfold%7D%28%5Cmathsf%7BF%7D%28%5Cmathit%7Bin%7D%29%29+%5Ccdot+%5Cmathit%7Bin%7D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bevaluation+rule%7D+%5C%7D+%5C%5C+%26+%5Cmathsf%7BF%7D%28%5Cmathit%7Bin%7D%29+%5Ccdot+%5Cmathsf%7BF%7D%28%5Cmathit%7Bfold%7D%28%5Cmathsf%7BF%7D%28%5Cmathit%7Bin%7D%29%29%29+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7B%5C%28%5Cmathit%7Bin%7D%5E%7B-1%7D%5C%29+as+a+fold+again%7D+%5C%7D+%5C%5C+%26+%5Cmathsf%7BF%7D%28%5Cmathit%7Bin%7D%29+%5Ccdot+%5Cmathsf%7BF%7D%28%5Cmathit%7Bin%7D%5E%7B-1%7D%29+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bfunctors%7D+%5C%7D+%5C%5C+%26+%5Cmathsf%7BF%7D%28%5Cmathit%7Bin%7D+%5Ccdot+%5Cmathit%7Bin%7D%5E%7B-1%7D%29+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bright+inverse%7D+%5C%7D+%5C%5C+%26+%5Cmathsf%7BF%7D%28%5Cmathit%7Bid%7D%29+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bfunctors%7D+%5C%7D+%5C%5C+%26+%5Cmathit%7Bid%7D+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;mathit{in}^{-1} &#92;cdot &#92;mathit{in} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{&#92;(&#92;mathit{in}^{-1}&#92;) as a fold} &#92;} &#92;&#92; &amp; &#92;mathit{fold}(&#92;mathsf{F}(&#92;mathit{in})) &#92;cdot &#92;mathit{in} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{evaluation rule} &#92;} &#92;&#92; &amp; &#92;mathsf{F}(&#92;mathit{in}) &#92;cdot &#92;mathsf{F}(&#92;mathit{fold}(&#92;mathsf{F}(&#92;mathit{in}))) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{&#92;(&#92;mathit{in}^{-1}&#92;) as a fold again} &#92;} &#92;&#92; &amp; &#92;mathsf{F}(&#92;mathit{in}) &#92;cdot &#92;mathsf{F}(&#92;mathit{in}^{-1}) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{functors} &#92;} &#92;&#92; &amp; &#92;mathsf{F}(&#92;mathit{in} &#92;cdot &#92;mathit{in}^{-1}) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{right inverse} &#92;} &#92;&#92; &amp; &#92;mathsf{F}(&#92;mathit{id}) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{functors} &#92;} &#92;&#92; &amp; &#92;mathit{id} &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;mathit{in}^{-1} &#92;cdot &#92;mathit{in} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{&#92;(&#92;mathit{in}^{-1}&#92;) as a fold} &#92;} &#92;&#92; &amp; &#92;mathit{fold}(&#92;mathsf{F}(&#92;mathit{in})) &#92;cdot &#92;mathit{in} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{evaluation rule} &#92;} &#92;&#92; &amp; &#92;mathsf{F}(&#92;mathit{in}) &#92;cdot &#92;mathsf{F}(&#92;mathit{fold}(&#92;mathsf{F}(&#92;mathit{in}))) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{&#92;(&#92;mathit{in}^{-1}&#92;) as a fold again} &#92;} &#92;&#92; &amp; &#92;mathsf{F}(&#92;mathit{in}) &#92;cdot &#92;mathsf{F}(&#92;mathit{in}^{-1}) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{functors} &#92;} &#92;&#92; &amp; &#92;mathsf{F}(&#92;mathit{in} &#92;cdot &#92;mathit{in}^{-1}) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{right inverse} &#92;} &#92;&#92; &amp; &#92;mathsf{F}(&#92;mathit{id}) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{functors} &#92;} &#92;&#92; &amp; &#92;mathit{id} &#92;end{array} ' class='latex' />
</p></blockquote>
</ul>
<p> And so on, and so on. Many useful functions can be written as instances of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfold%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{fold}}' title='{&#92;mathit{fold}}' class='latex' />, and the universal property gives us a very powerful reasoning tool&mdash;the universal property of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfold%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{fold}}' title='{&#92;mathit{fold}}' class='latex' /> is a marvel to behold. </p>
<p>
And of course, it all dualizes beautifully. An <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />-coalgebra is a pair <img src='http://s0.wp.com/latex.php?latex=%7B%28X%2Cf%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(X,f)}' title='{(X,f)}' class='latex' /> with <img src='http://s0.wp.com/latex.php?latex=%7Bf+%3A+X+%5Crightarrow+%5Cmathsf%7BF%7D%28X%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f : X &#92;rightarrow &#92;mathsf{F}(X)}' title='{f : X &#92;rightarrow &#92;mathsf{F}(X)}' class='latex' />. A homomorphism between <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />-coalgebras <img src='http://s0.wp.com/latex.php?latex=%7B%28X%2Cf%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(X,f)}' title='{(X,f)}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B%28Y%2Cg%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(Y,g)}' title='{(Y,g)}' class='latex' /> is a function <img src='http://s0.wp.com/latex.php?latex=%7Bh+%3A+X+%5Crightarrow+Y%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h : X &#92;rightarrow Y}' title='{h : X &#92;rightarrow Y}' class='latex' /> such that <img src='http://s0.wp.com/latex.php?latex=%7Bg+%5Ccdot+h+%3D+%5Cmathsf%7BF%7D%28h%29+%5Ccdot+f%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{g &#92;cdot h = &#92;mathsf{F}(h) &#92;cdot f}' title='{g &#92;cdot h = &#92;mathsf{F}(h) &#92;cdot f}' class='latex' />:
<p align="center"><img width="150" src="http://patternsinfp.files.wordpress.com/2011/04/morality-cohomomorphism.png?w=150"></p>
<p>  The <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />-coalgebra <img src='http://s0.wp.com/latex.php?latex=%7B%28Y%2Cg%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(Y,g)}' title='{(Y,g)}' class='latex' /> is <em>final</em> iff there is a unique homomorphism <em>to</em> it from each <img src='http://s0.wp.com/latex.php?latex=%7B%28X%2Cf%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(X,f)}' title='{(X,f)}' class='latex' />; again, for well-behaved <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />, final coalgebras always exist. We write &#8220;<img src='http://s0.wp.com/latex.php?latex=%7B%28%5Cnu%5Cmathsf%7BF%7D%2C%5Cmathit%7Bout%7D%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(&#92;nu&#92;mathsf{F},&#92;mathit{out})}' title='{(&#92;nu&#92;mathsf{F},&#92;mathit{out})}' class='latex' />&#8221; for the final coalgebra, and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bunfold%7D_%7B%5Cmathsf%7BF%7D%7D%28f%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{unfold}_{&#92;mathsf{F}}(f)}' title='{&#92;mathit{unfold}_{&#92;mathsf{F}}(f)}' class='latex' /> for the unique homomorphism to it. (In <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{S}&#92;mathrm{et}}' title='{&#92;mathbb{S}&#92;mathrm{et}}' class='latex' />, final coalgebras correspond to datatypes of finite-or-infinite recursive data structures.)
<p align="center"><img width="150" src="http://patternsinfp.files.wordpress.com/2011/04/morality-final.png?w=150"></p>
<p>  Uniqueness is captured by the universal property </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++h+%3D+%5Cmathit%7Bunfold%7D%28f%29+%5CLeftrightarrow+%5Cmathit%7Bout%7D+%5Ccdot+h+%3D+%5Cmathsf%7BF%7D%28h%29+%5Ccdot+f+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  h = &#92;mathit{unfold}(f) &#92;Leftrightarrow &#92;mathit{out} &#92;cdot h = &#92;mathsf{F}(h) &#92;cdot f ' title='&#92;displaystyle  h = &#92;mathit{unfold}(f) &#92;Leftrightarrow &#92;mathit{out} &#92;cdot h = &#92;mathsf{F}(h) &#92;cdot f ' class='latex' />
</p></blockquote>
<p> which has just as many marvellous consequences. Many other useful functions are definable as instances of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bunfold%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{unfold}}' title='{&#92;mathit{unfold}}' class='latex' />, and again the universal property gives a very powerful tool for reasoning with them.</p>
<p><h2> Hylomorphisms </h2>
<p>
There are also many interesting functions that are best described as a <em>combination</em> of a fold and an unfold. The <em>hylomorphism</em> pattern, with an unfold followed by a fold, is the best known: the unfold produces a recursive structure, which the fold consumes. </p>
<p>
The factorial function is a simple example. The datatype of lists of natural numbers is determined by the shape functor </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathsf%7BL%7D%28X%29+%3D+1+%2B+%5Cmathbb%7BN%7D+%5Ctimes+X+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathsf{L}(X) = 1 + &#92;mathbb{N} &#92;times X ' title='&#92;displaystyle  &#92;mathsf{L}(X) = 1 + &#92;mathbb{N} &#92;times X ' class='latex' />
</p></blockquote>
<p> Then we might hope to write </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathit%7Bfact%7D+%3D+%5Cmathit%7Bproduct%7D+%5Ccdot+%5Cmathit%7BdownFrom%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathit{fact} = &#92;mathit{product} &#92;cdot &#92;mathit{downFrom} ' title='&#92;displaystyle  &#92;mathit{fact} = &#92;mathit{product} &#92;cdot &#92;mathit{downFrom} ' class='latex' />
</p></blockquote>
<p> where <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7BdownFrom%7D+%3D+%5Cmathit%7Bunfold%7D_%7B%5Cmathsf%7BL%7D%7D%28d%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{downFrom} = &#92;mathit{unfold}_{&#92;mathsf{L}}(d)}' title='{&#92;mathit{downFrom} = &#92;mathit{unfold}_{&#92;mathsf{L}}(d)}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bproduct%7D+%3D+%5Cmathit%7Bfold%7D_%7B%5Cmathsf%7BL%7D%7D%28m%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{product} = &#92;mathit{fold}_{&#92;mathsf{L}}(m)}' title='{&#92;mathit{product} = &#92;mathit{fold}_{&#92;mathsf{L}}(m)}' class='latex' /> with </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+d+%26%3A%3A%26+%5Cmathbb%7BN%7D+%5Crightarrow+%5Cmathsf%7BL%7D%28%5Cmathbb%7BN%7D%29+%5C%5C+d%5C%2C0+%26%3D%26+%5Cmathit%7Binl%7D%5C%2C%28%29+%5C%5C+d%5C%2C%28n%2B1%29+%26%3D%26+%5Cmathit%7Binr%7D%5C%2C%28n%2B1%2Cn%29+%5Cbigskip%5C%5C+m+%26%3A%3A%26+%5Cmathsf%7BL%7D%28%5Cmathbb%7BN%7D%29+%5Crightarrow+%5Cmathbb%7BN%7D+%5C%5C+m%5C%2C%28%5Cmathit%7Binl%7D%5C%2C%28%29%29+%26%3D%26+1+%5C%5C+m%5C%2C%28%5Cmathit%7Binr%7D%5C%2C%28n%2Cn%27%29%29+%26%3D%26+n+%5Ctimes+n%27+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} d &amp;::&amp; &#92;mathbb{N} &#92;rightarrow &#92;mathsf{L}(&#92;mathbb{N}) &#92;&#92; d&#92;,0 &amp;=&amp; &#92;mathit{inl}&#92;,() &#92;&#92; d&#92;,(n+1) &amp;=&amp; &#92;mathit{inr}&#92;,(n+1,n) &#92;bigskip&#92;&#92; m &amp;::&amp; &#92;mathsf{L}(&#92;mathbb{N}) &#92;rightarrow &#92;mathbb{N} &#92;&#92; m&#92;,(&#92;mathit{inl}&#92;,()) &amp;=&amp; 1 &#92;&#92; m&#92;,(&#92;mathit{inr}&#92;,(n,n&#039;)) &amp;=&amp; n &#92;times n&#039; &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} d &amp;::&amp; &#92;mathbb{N} &#92;rightarrow &#92;mathsf{L}(&#92;mathbb{N}) &#92;&#92; d&#92;,0 &amp;=&amp; &#92;mathit{inl}&#92;,() &#92;&#92; d&#92;,(n+1) &amp;=&amp; &#92;mathit{inr}&#92;,(n+1,n) &#92;bigskip&#92;&#92; m &amp;::&amp; &#92;mathsf{L}(&#92;mathbb{N}) &#92;rightarrow &#92;mathbb{N} &#92;&#92; m&#92;,(&#92;mathit{inl}&#92;,()) &amp;=&amp; 1 &#92;&#92; m&#92;,(&#92;mathit{inr}&#92;,(n,n&#039;)) &amp;=&amp; n &#92;times n&#039; &#92;end{array} ' class='latex' />
</p></blockquote>
<p> More elaborately, we might hope to write <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bquicksort%7D+%3A+%5Cmathsf%7BList%7D%28%7B%5Cmathbb+Z%7D%29+%5Crightarrow+%5Cmathsf%7BList%7D%28%7B%5Cmathbb+Z%7D%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{quicksort} : &#92;mathsf{List}({&#92;mathbb Z}) &#92;rightarrow &#92;mathsf{List}({&#92;mathbb Z})}' title='{&#92;mathit{quicksort} : &#92;mathsf{List}({&#92;mathbb Z}) &#92;rightarrow &#92;mathsf{List}({&#92;mathbb Z})}' class='latex' /> as the composition of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bunfold%7D_%5Cmathsf%7BB%7D%28s%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{unfold}_&#92;mathsf{B}(s)}' title='{&#92;mathit{unfold}_&#92;mathsf{B}(s)}' class='latex' /> (to generate a binary search tree) and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfold%7D_%5Cmathsf%7BB%7D%28g%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{fold}_&#92;mathsf{B}(g)}' title='{&#92;mathit{fold}_&#92;mathsf{B}(g)}' class='latex' /> (to flatten that tree to a list), where <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BB%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{B}}' title='{&#92;mathsf{B}}' class='latex' /> is the shape functor for internally-labelled binary trees, </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++p+%3A+%5Cmathsf%7BList%7D%28%7B%5Cmathbb+Z%7D%29+%5Crightarrow+%5Cmathsf%7BB%7D%28%5Cmathsf%7BList%7D%28%7B%5Cmathbb+Z%7D%29%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  p : &#92;mathsf{List}({&#92;mathbb Z}) &#92;rightarrow &#92;mathsf{B}(&#92;mathsf{List}({&#92;mathbb Z})) ' title='&#92;displaystyle  p : &#92;mathsf{List}({&#92;mathbb Z}) &#92;rightarrow &#92;mathsf{B}(&#92;mathsf{List}({&#92;mathbb Z})) ' class='latex' />
</p></blockquote>
<p> partitions a list of integers into the unit or a pivot and two sublists, and </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++g+%3A+%5Cmathsf%7BB%7D%28%5Cmathsf%7BList%7D%28%7B%5Cmathbb+Z%7D%29%29+%5Crightarrow+%5Cmathsf%7BList%7D%28%7B%5Cmathbb+Z%7D%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  g : &#92;mathsf{B}(&#92;mathsf{List}({&#92;mathbb Z})) &#92;rightarrow &#92;mathsf{List}({&#92;mathbb Z}) ' title='&#92;displaystyle  g : &#92;mathsf{B}(&#92;mathsf{List}({&#92;mathbb Z})) &#92;rightarrow &#92;mathsf{List}({&#92;mathbb Z}) ' class='latex' />
</p></blockquote>
<p> glues together the unit or a pivot and two sorted lists into one list. In fact, any divide-and-conquer algorithm can be expressed in terms of an unfold computing a tree of subproblems top-down, followed by a fold that solves the subproblems bottom-up.</p>
<p>
But sadly, this doesn&#8217;t work in <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{S}&#92;mathrm{et}}' title='{&#92;mathbb{S}&#92;mathrm{et}}' class='latex' />, because the types don&#8217;t meet in the middle. The source type of the fold is (the carrier of) an initial algebra, but the target type of the unfold is a final coalgebra, and these are different constructions. </p>
<p>
This is entirely reasonable, when you think about it. Our definitions in <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{S}&#92;mathrm{et}}' title='{&#92;mathbb{S}&#92;mathrm{et}}' class='latex' />&mdash;the category of sets and total functions&mdash;necessarily gave us folds and unfolds as total functions; the composition of two total functions is a total function, and so a fold after an unfold ought to be a total function too. But it is easy to define total instances of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bunfold%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{unfold}}' title='{&#92;mathit{unfold}}' class='latex' /> that generate infinite data structures (such as a function <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7BupFrom%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{upFrom}}' title='{&#92;mathit{upFrom}}' class='latex' />, which generates an infinite ascending list of naturals), on which a following fold is undefined (such as &#8220;the product&#8221; of an infinite ascending list of naturals). The composition then should not be a total function.</p>
<p>
One might try interposing a conversion function of type <img src='http://s0.wp.com/latex.php?latex=%7B%5Cnu%5Cmathsf%7BF%7D+%5Crightarrow+%5Cmu%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;nu&#92;mathsf{F} &#92;rightarrow &#92;mu&#92;mathsf{F}}' title='{&#92;nu&#92;mathsf{F} &#92;rightarrow &#92;mu&#92;mathsf{F}}' class='latex' />, coercing the final data structure produced by the unfold into an initial data structure for consumption by the fold. But there is no canonical way of doing this, because final data structures may be &#8220;bigger&#8221; (perhaps infinitely so) than initial ones. (In contrast, there is a canonical function of type <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmu%5Cmathsf%7BF%7D+%5Crightarrow+%5Cnu%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mu&#92;mathsf{F} &#92;rightarrow &#92;nu&#92;mathsf{F}}' title='{&#92;mu&#92;mathsf{F} &#92;rightarrow &#92;nu&#92;mathsf{F}}' class='latex' />. In fact, there are two obvious definitions of it, and they agree&mdash;a nice exercise!)</p>
<p>
One might try parametrizing that conversion function with a natural number, bounding the depth to which the final data structure is traversed. Then the coercion is nicely structural (in fact, it&#8217;s a fold over the depth), and everything works out type-wise. But having to thread such &#8220;resource bounds&#8221; through the code does terrible violence to the elegant structure; it&#8217;s not very satisfactory.</p>
<p><h2> Continuous algebras </h2>
<p>
The usual solution to this conundrum is to give up on <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{S}&#92;mathrm{et}}' title='{&#92;mathbb{S}&#92;mathrm{et}}' class='latex' />, and to admit that richer domain structures than sets and total functions are required. Specifically, in order to support recursive definitions in general, and the hylomorphism in particular, one should move to the category <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BC%7D%5Cmathrm%7Bpo%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{C}&#92;mathrm{po}}' title='{&#92;mathbb{C}&#92;mathrm{po}}' class='latex' /> of <em>continuous functions</em> between <em>complete partial orders</em> (CPOs). Now is not the place to give all the definitions; see any textbook on denotational semantics. The bottom line, so to speak, is that one has to accept a definedness ordering <img src='http://s0.wp.com/latex.php?latex=%7B%5Csqsubseteq%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;sqsubseteq}' title='{&#92;sqsubseteq}' class='latex' /> on values&mdash;both on &#8220;data&#8221; and on functions&mdash;and allow some values to be less than fully defined.</p>
<p>
Actually, in order to give meaning to all recursive definitions, one has to further restrict the setting to <em>pointed</em> CPOs&mdash;in which there is a least-defined &#8220;bottom&#8221; element <img src='http://s0.wp.com/latex.php?latex=%7B%5Cbot_X%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;bot_X}' title='{&#92;bot_X}' class='latex' /> for each type <img src='http://s0.wp.com/latex.php?latex=%7BX%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{X}' title='{X}' class='latex' />, which can be given as the &#8220;meaning&#8221; (solution) of the degenerate recursive definition <img src='http://s0.wp.com/latex.php?latex=%7Bx%3Dx%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{x=x}' title='{x=x}' class='latex' /> at type <img src='http://s0.wp.com/latex.php?latex=%7BX%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{X}' title='{X}' class='latex' />. Then there is no &#8220;empty&#8221; CPO; the smallest CPO <img src='http://s0.wp.com/latex.php?latex=%7B0%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{0}' title='{0}' class='latex' /> has just a single element, namely <img src='http://s0.wp.com/latex.php?latex=%7B%5Cbot%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;bot}' title='{&#92;bot}' class='latex' />. As with <a href="http://patternsinfp.wordpress.com/2011/03/31/extreme-solutions/">colimits in general</a>, this smallest object is used as the start of a chain of approximations to a limiting solution. But in order for <img src='http://s0.wp.com/latex.php?latex=%7B0%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{0}' title='{0}' class='latex' /> really to be an initial object, one also has to constrain the arrows to be <em>strict</em>, that is, to preserve <img src='http://s0.wp.com/latex.php?latex=%7B%5Cbot%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;bot}' title='{&#92;bot}' class='latex' />; only then is there a unique arrow <img src='http://s0.wp.com/latex.php?latex=%7B0+%5Crightarrow+A%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{0 &#92;rightarrow A}' title='{0 &#92;rightarrow A}' class='latex' /> for each <img src='http://s0.wp.com/latex.php?latex=%7BA%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{A}' title='{A}' class='latex' />. The category of <em>strict continuous functions</em> between <em>pointed CPOs</em> is called <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BC%7D%5Cmathrm%7Bpo%7D_%5Cbot%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{C}&#92;mathrm{po}_&#92;bot}' title='{&#92;mathbb{C}&#92;mathrm{po}_&#92;bot}' class='latex' />. </p>
<p>
It so happens that in <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BC%7D%5Cmathrm%7Bpo%7D_%5Cbot%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{C}&#92;mathrm{po}_&#92;bot}' title='{&#92;mathbb{C}&#92;mathrm{po}_&#92;bot}' class='latex' />, initial algebras and final coalgebras coincide: the objects (pointed CPOs) <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmu%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mu&#92;mathsf{F}}' title='{&#92;mu&#92;mathsf{F}}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cnu%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;nu&#92;mathsf{F}}' title='{&#92;nu&#92;mathsf{F}}' class='latex' /> are identical. This is very convenient, because it means that the hylomorphism pattern works fine: the structure generated by the unfold is exactly what is expected by the fold.
<p align="center"><img width="450" src="http://patternsinfp.files.wordpress.com/2011/04/morality-hylo.png?w=450"></p>
<p>  Of course, it still happen that the composition yields a &#8220;partial&#8221; (less than fully defined) function; but at least it now type-checks. Categories with this initial algebra/final coalgebra coincidence are called <em>algebraically compact</em>; they were studied by Freyd, but there&#8217;s a <a href="http://www.tu-braunschweig.de/Medien-DB/iti/survey_full.pdf">very good survey</a> by Ad&aacute;mek, Milius and Moss.</p>
<p>
However, the story gets murkier than that. For one thing, <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BC%7D%5Cmathrm%7Bpo%7D_%5Cbot%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{C}&#92;mathrm{po}_&#92;bot}' title='{&#92;mathbb{C}&#92;mathrm{po}_&#92;bot}' class='latex' /> does not have proper products. (Indeed, an algebraically compact category with products collapses.) But beyond that, <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BC%7D%5Cmathrm%7Bpo%7D_%5Cbot%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{C}&#92;mathrm{po}_&#92;bot}' title='{&#92;mathbb{C}&#92;mathrm{po}_&#92;bot}' class='latex' />&mdash;with its restriction to strict arrows&mdash;is not a good model of lazy functional programming; <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BC%7D%5Cmathrm%7Bpo%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{C}&#92;mathrm{po}}' title='{&#92;mathbb{C}&#92;mathrm{po}}' class='latex' />, with non-strict arrows too, is better. So one needs a careful balance of the two categories. The consequences for initial algebras and final coalgebras are spelled out in one of my favourite papers, <a href="http://doc.utwente.nl/66626/">Program Calculation Properties of Continuous Algebras</a> by Fokkinga and Meijer. In a nutshell, one can only say that the defining equation <img src='http://s0.wp.com/latex.php?latex=%7Bh+%5Ccdot+%5Cmathit%7Bin%7D+%3D+g+%5Ccdot+%5Cmathsf%7BF%7D%28h%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h &#92;cdot &#92;mathit{in} = g &#92;cdot &#92;mathsf{F}(h)}' title='{h &#92;cdot &#92;mathit{in} = g &#92;cdot &#92;mathsf{F}(h)}' class='latex' /> for folds has a unique <em>strict</em> solution in <img src='http://s0.wp.com/latex.php?latex=%7Bh%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h}' title='{h}' class='latex' />; without the strictness side-condition, <img src='http://s0.wp.com/latex.php?latex=%7Bh%5C%2C%5Cbot%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h&#92;,&#92;bot}' title='{h&#92;,&#92;bot}' class='latex' /> is unconstrained (because <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bin%7D%5C%2Cx+%5Cne+%5Cbot%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{in}&#92;,x &#92;ne &#92;bot}' title='{&#92;mathit{in}&#92;,x &#92;ne &#92;bot}' class='latex' /> for any <img src='http://s0.wp.com/latex.php?latex=%7Bx%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{x}' title='{x}' class='latex' />). But the situation for coalgebras remains unchanged&mdash;the defining equation <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bout%7D+%5Ccdot+h+%3D+%5Cmathsf%7BF%7D%28h%29+%5Ccdot+f%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{out} &#92;cdot h = &#92;mathsf{F}(h) &#92;cdot f}' title='{&#92;mathit{out} &#92;cdot h = &#92;mathsf{F}(h) &#92;cdot f}' class='latex' /> for unfolds has a unique solution (and moreover, it is strict when <img src='http://s0.wp.com/latex.php?latex=%7Bf%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f}' title='{f}' class='latex' /> is strict).</p>
<p>
This works, but it means various strictness side-conditions have to be borne in mind when reasoning about folds. Done rigorously, it&#8217;s rather painful.</p>
<p><h2> Recursive coalgebras </h2>
<p>
So, back to my confession. I want to write divide-and-conquer programs, which produce intermediate data structures and then consume them. Folds and unfolds in <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{S}&#92;mathrm{et}}' title='{&#92;mathbb{S}&#92;mathrm{et}}' class='latex' /> do not satisfy me; I want more&mdash;hylos. Morally, I realise that I should pay careful attention to those strictness side-conditions. But they&#8217;re so fiddly and boring, and my resolve is weak, so I usually just brush them aside. Is there away that I can satisfy my appetite for divide-and-conquer programs while still remaining in the pure <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{S}&#92;mathrm{et}}' title='{&#92;mathbb{S}&#92;mathrm{et}}' class='latex' /> world?</p>
<p>
Tarmo Uustalu and colleagues have a suggestion. Final coalgebras and algebraic compactness are sufficient but not necessary for the hylo diagram above to have a unique solution; they propose to focus on <em>recursive coalgebras</em> instead. The <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />-coalgebra <img src='http://s0.wp.com/latex.php?latex=%7B%28X%2Cf%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(X,f)}' title='{(X,f)}' class='latex' /> is &#8220;recursive&#8221; iff, for each <img src='http://s0.wp.com/latex.php?latex=%7Bg+%3A+%5Cmathsf%7BF%7D%28Y%29+%5Crightarrow+Y%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{g : &#92;mathsf{F}(Y) &#92;rightarrow Y}' title='{g : &#92;mathsf{F}(Y) &#92;rightarrow Y}' class='latex' />, there is a unique <img src='http://s0.wp.com/latex.php?latex=%7Bh%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h}' title='{h}' class='latex' /> such that <img src='http://s0.wp.com/latex.php?latex=%7Bh+%3D+g+%5Ccdot+%5Cmathsf%7BF%7D%28h%29+%5Ccdot+f%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h = g &#92;cdot &#92;mathsf{F}(h) &#92;cdot f}' title='{h = g &#92;cdot &#92;mathsf{F}(h) &#92;cdot f}' class='latex' />:
<p align="center"><img width="150" src="http://patternsinfp.files.wordpress.com/2011/04/morality-recursive.png?w=150"></p>
<p>  This is a generalization of initial algebras: if <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' /> has an initial algebra <img src='http://s0.wp.com/latex.php?latex=%7B%28%5Cmu%5Cmathsf%7BF%7D%2C%5Cmathit%7Bin%7D%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(&#92;mu&#92;mathsf{F},&#92;mathit{in})}' title='{(&#92;mu&#92;mathsf{F},&#92;mathit{in})}' class='latex' />, then by Lambek&#8217;s Lemma <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bin%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{in}}' title='{&#92;mathit{in}}' class='latex' /> has an inverse <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bin%7D%5E%7B-1%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{in}^{-1}}' title='{&#92;mathit{in}^{-1}}' class='latex' />, and <img src='http://s0.wp.com/latex.php?latex=%7B%28%5Cmu%5Cmathsf%7BF%7D%2C%5Cmathit%7Bin%7D%5E%7B-1%7D%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(&#92;mu&#92;mathsf{F},&#92;mathit{in}^{-1})}' title='{(&#92;mu&#92;mathsf{F},&#92;mathit{in}^{-1})}' class='latex' /> is a recursive coalgebra. And it is a strict generalization: it also covers patterns such as <em>paramorphisms</em> (primitive recursion)&mdash;since <img src='http://s0.wp.com/latex.php?latex=%7B%28%5Cmu%5Cmathsf%7BF%7D%2C+%5Cmathsf%7BF%7D%28%5Cmathit%7Bfork%7D%28%5Cmathit%7Bid%7D%2C%5Cmathit%7Bid%7D%29%29%5Ccdot%5Cmathit%7Bin%7D_%5Cmathsf%7BF%7D%5E%7B-1%7D%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(&#92;mu&#92;mathsf{F}, &#92;mathsf{F}(&#92;mathit{fork}(&#92;mathit{id},&#92;mathit{id}))&#92;cdot&#92;mathit{in}_&#92;mathsf{F}^{-1})}' title='{(&#92;mu&#92;mathsf{F}, &#92;mathsf{F}(&#92;mathit{fork}(&#92;mathit{id},&#92;mathit{id}))&#92;cdot&#92;mathit{in}_&#92;mathsf{F}^{-1})}' class='latex' /> is a recursive <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BG%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{G}}' title='{&#92;mathsf{G}}' class='latex' />-coalgebra where <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BG%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{G}}' title='{&#92;mathsf{G}}' class='latex' /> is the functor taking <img src='http://s0.wp.com/latex.php?latex=%7BX%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{X}' title='{X}' class='latex' /> to <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%28X+%5Ctimes+%5Cmathsf%7BF%7D%28X%29%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}(X &#92;times &#92;mathsf{F}(X))}' title='{&#92;mathsf{F}(X &#92;times &#92;mathsf{F}(X))}' class='latex' />&mdash;and the &#8220;back one or two steps&#8221; pattern used in the Fibonacci function. </p>
<p>
Crucially for us, almost by definition it covers all of the &#8220;reasonable&#8221; hylomorphisms too. For example, <img src='http://s0.wp.com/latex.php?latex=%7B%28%5Cmathbb%7BN%7D%2Cd%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(&#92;mathbb{N},d)}' title='{(&#92;mathbb{N},d)}' class='latex' /> is a recursive <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BL%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{L}}' title='{&#92;mathsf{L}}' class='latex' />-coalgebra, where <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BL%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{L}}' title='{&#92;mathsf{L}}' class='latex' /> is the shape functor for lists of naturals and <img src='http://s0.wp.com/latex.php?latex=%7Bd%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{d}' title='{d}' class='latex' /> the <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BL%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{L}}' title='{&#92;mathsf{L}}' class='latex' />-coalgebra introduced above that analyzes a natural into nothing (for zero) or itself and its predecessor (for non-zero inputs). Which is to say, for each <img src='http://s0.wp.com/latex.php?latex=%7Bm+%3A+%5Cmathsf%7BL%7D%28X%29+%5Crightarrow+X%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{m : &#92;mathsf{L}(X) &#92;rightarrow X}' title='{m : &#92;mathsf{L}(X) &#92;rightarrow X}' class='latex' />, there is a unique <img src='http://s0.wp.com/latex.php?latex=%7Bh%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h}' title='{h}' class='latex' /> such that <img src='http://s0.wp.com/latex.php?latex=%7Bh+%3D+m+%5Ccdot+%5Cmathsf%7BL%7D%28h%29+%5Ccdot+d%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h = m &#92;cdot &#92;mathsf{L}(h) &#92;cdot d}' title='{h = m &#92;cdot &#92;mathsf{L}(h) &#92;cdot d}' class='latex' />; in particular, for the <img src='http://s0.wp.com/latex.php?latex=%7Bm%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{m}' title='{m}' class='latex' /> given above that returns 1 or multiplies, the unique <img src='http://s0.wp.com/latex.php?latex=%7Bh%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h}' title='{h}' class='latex' /> is the factorial function. (In fact, this example is also an instance of a paramorphism.) And <img src='http://s0.wp.com/latex.php?latex=%7B%28%5Cmathsf%7BList%7D%28%7B%5Cmathbb+Z%7D%29%2Cp%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(&#92;mathsf{List}({&#92;mathbb Z}),p)}' title='{(&#92;mathsf{List}({&#92;mathbb Z}),p)}' class='latex' /> is a recursive <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BB%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{B}}' title='{&#92;mathsf{B}}' class='latex' />-coalgebra, where <img src='http://s0.wp.com/latex.php?latex=%7Bp%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{p}' title='{p}' class='latex' /> is the partition function of quicksort&mdash;for any <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BB%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{B}}' title='{&#92;mathsf{B}}' class='latex' />-algebra <img src='http://s0.wp.com/latex.php?latex=%7B%28Y%2Cg%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(Y,g)}' title='{(Y,g)}' class='latex' />, there is a unique <img src='http://s0.wp.com/latex.php?latex=%7Bh%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h}' title='{h}' class='latex' /> such that <img src='http://s0.wp.com/latex.php?latex=%7Bh+%3D+g+%5Ccdot+%5Cmathsf%7BB%7D%28h%29+%5Ccdot+p%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h = g &#92;cdot &#92;mathsf{B}(h) &#92;cdot p}' title='{h = g &#92;cdot &#92;mathsf{B}(h) &#92;cdot p}' class='latex' />, and in particular when <img src='http://s0.wp.com/latex.php?latex=%7Bg%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{g}' title='{g}' class='latex' /> is the glue function for quicksort, that unique solution is quicksort itself.</p>
<p>
This works perfectly nicely in <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{S}&#92;mathrm{et}}' title='{&#92;mathbb{S}&#92;mathrm{et}}' class='latex' />; there is no need to move to more complicated settings such as <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BC%7D%5Cmathrm%7Bpo%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{C}&#92;mathrm{po}}' title='{&#92;mathbb{C}&#92;mathrm{po}}' class='latex' /> or <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BC%7D%5Cmathrm%7Bpo%7D_%5Cbot%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{C}&#92;mathrm{po}_&#92;bot}' title='{&#92;mathbb{C}&#92;mathrm{po}_&#92;bot}' class='latex' />, or to consider partiality, or strictness, or definedness orderings. The only snag is the need to prove that a particular coalgebra of interest is indeed recursive. <a href="http://dx.doi.org/10.1016/j.ic.2005.08.005">Capretta et al.</a> study a handful of &#8220;basic&#8221; recursive coalgebras and of constructions on coalgebras that preserve recursivity. </p>
<p>
More conveniently, <a href="http://www.cs.man.ac.uk/~pt/Practical_Foundations/">Taylor</a> and <a href="http://dx.doi.org/10.1051/ita:2007028">Ad&aacute;mek et al.</a> relate recursivity of coalgebras to the more familiar notion of <em>variant</em> function, ie well-founded ordering on arguments of recursive calls. They restrict attention to <em>finitary</em> shape functors; technically, preserving directed colimits, but informally, I think that&#8217;s equivalent to requiring that each element of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%28X%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}(X)}' title='{&#92;mathsf{F}(X)}' class='latex' /> has a finite number of <img src='http://s0.wp.com/latex.php?latex=%7BX%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{X}' title='{X}' class='latex' /> elements&mdash;so polynomial functors are ok, as is the finite powerset functor, but not powerset in general. If I understand those sources right, for a finitary functor <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' /> and an <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />-coalgebra <img src='http://s0.wp.com/latex.php?latex=%7B%28X%2Cf%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(X,f)}' title='{(X,f)}' class='latex' />, the following conditions are equivalent: (i) <img src='http://s0.wp.com/latex.php?latex=%7B%28X%2Cf%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(X,f)}' title='{(X,f)}' class='latex' /> is corecursive; (ii) <img src='http://s0.wp.com/latex.php?latex=%7Bf%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f}' title='{f}' class='latex' /> is well-founded, in the sense that there is a well-founded ordering <img src='http://s0.wp.com/latex.php?latex=%7B%5Cprec%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;prec}' title='{&#92;prec}' class='latex' /> such that <img src='http://s0.wp.com/latex.php?latex=%7By+%5Cprec+x%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{y &#92;prec x}' title='{y &#92;prec x}' class='latex' /> for each &#8220;element&#8221; <img src='http://s0.wp.com/latex.php?latex=%7By%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{y}' title='{y}' class='latex' /> of <img src='http://s0.wp.com/latex.php?latex=%7Bf%28x%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f(x)}' title='{f(x)}' class='latex' />; (iii) every element of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bunfold%7D_%5Cmathsf%7BF%7D%28f%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{unfold}_&#92;mathsf{F}(f)}' title='{&#92;mathit{unfold}_&#92;mathsf{F}(f)}' class='latex' /> has finite depth; and (iv) there is a coalgebra homomorphism from <img src='http://s0.wp.com/latex.php?latex=%7B%28X%2Cf%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(X,f)}' title='{(X,f)}' class='latex' /> to <img src='http://s0.wp.com/latex.php?latex=%7B%28%5Cmu%5Cmathsf%7BF%7D%2C%5Cmathit%7Bin%7D%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(&#92;mu&#92;mathsf{F},&#92;mathit{in})}' title='{(&#92;mu&#92;mathsf{F},&#92;mathit{in})}' class='latex' />.</p>
<p>
This means that I can resort to simple and familiar arguments in terms of variant functions to justify hylo-style programs. The factorial function is fine, because (<img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BL%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{L}}' title='{&#92;mathsf{L}}' class='latex' /> is a finitary functor, being polynomial, and) the chain of recursive calls to which <img src='http://s0.wp.com/latex.php?latex=%7Bd%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{d}' title='{d}' class='latex' /> leads is well-founded; quicksort is fine, because the partitioning step is well-founded; and so on. Which takes a great weight of guilt off my shoulders: I can give in to the temptation to write interesting programs, and still remain morally as pure as the driven snow.</p>
<p>
<br />  <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=patternsinfp.wordpress.com&#038;blog=15593982&#038;post=179&#038;subd=patternsinfp&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://patternsinfp.wordpress.com/2011/04/11/morality-and-temptation/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/39d7e0397cba13d1494d9a48c1f42167?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jeremygibbons</media:title>
		</media:content>

		<media:content url="http://patternsinfp.files.wordpress.com/2011/04/morality-homomorphism.png" medium="image" />

		<media:content url="http://patternsinfp.files.wordpress.com/2011/04/morality-initial.png" medium="image" />

		<media:content url="http://patternsinfp.files.wordpress.com/2011/04/morality-cohomomorphism.png" medium="image" />

		<media:content url="http://patternsinfp.files.wordpress.com/2011/04/morality-final.png" medium="image" />

		<media:content url="http://patternsinfp.files.wordpress.com/2011/04/morality-hylo.png" medium="image" />

		<media:content url="http://patternsinfp.files.wordpress.com/2011/04/morality-recursive.png" medium="image" />
	</item>
		<item>
		<title>Extreme solutions</title>
		<link>http://patternsinfp.wordpress.com/2011/03/31/extreme-solutions/</link>
		<comments>http://patternsinfp.wordpress.com/2011/03/31/extreme-solutions/#comments</comments>
		<pubDate>Thu, 31 Mar 2011 15:03:35 +0000</pubDate>
		<dc:creator>jeremygibbons</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://patternsinfp.wordpress.com/?p=158</guid>
		<description><![CDATA[Not all problem statements are amenable to translation via Galois connections or adjunctions. A reasonable characterization of those that are suitable is the optimization problems&#8212;finding the least or greatest solution satisfying a given collection of constraints, according to some ordering. &#8230; <a href="http://patternsinfp.wordpress.com/2011/03/31/extreme-solutions/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=patternsinfp.wordpress.com&#038;blog=15593982&#038;post=158&#038;subd=patternsinfp&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>
 Not all problem statements are amenable to translation via Galois connections or adjunctions. A reasonable characterization of those that are suitable is the <em>optimization problems</em>&mdash;finding the <em>least or greatest solution</em> satisfying a given collection of constraints, according to some ordering. </p>
<p><h2> Least and greatest solutions </h2>
<p>
For example, the Galois connection determining integer division that we considered <a href="http://patternsinfp.wordpress.com/2011/02/15/universal-properties-and-galois-connections/">a couple of posts ago</a> </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++n+%5Cdiv+k+%5Cge+m+%5CLeftrightarrow+n+%5Cge+m+%5Ctimes+k+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  n &#92;div k &#92;ge m &#92;Leftrightarrow n &#92;ge m &#92;times k ' title='&#92;displaystyle  n &#92;div k &#92;ge m &#92;Leftrightarrow n &#92;ge m &#92;times k ' class='latex' />
</p></blockquote>
<p> defines <img src='http://s0.wp.com/latex.php?latex=%7Bn+%5Cdiv+k%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{n &#92;div k}' title='{n &#92;div k}' class='latex' /> to be the greatest solution <img src='http://s0.wp.com/latex.php?latex=%7Bm%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{m}' title='{m}' class='latex' /> to the equation <img src='http://s0.wp.com/latex.php?latex=%7Bn+%5Cge+m+%5Ctimes+k%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{n &#92;ge m &#92;times k}' title='{n &#92;ge m &#92;times k}' class='latex' />. It does so in a very pithy way: reading the equivalence as an implication from left to right, instantiating <img src='http://s0.wp.com/latex.php?latex=%7Bm%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{m}' title='{m}' class='latex' /> to <img src='http://s0.wp.com/latex.php?latex=%7Bn+%5Cdiv+k%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{n &#92;div k}' title='{n &#92;div k}' class='latex' /> (and exploiting the reflexivity of the ordering <img src='http://s0.wp.com/latex.php?latex=%7B%5Cge%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;ge}' title='{&#92;ge}' class='latex' />), we get that <img src='http://s0.wp.com/latex.php?latex=%7Bn+%5Cge+%28n+%5Cdiv+k%29+%5Ctimes+k%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{n &#92;ge (n &#92;div k) &#92;times k}' title='{n &#92;ge (n &#92;div k) &#92;times k}' class='latex' />, so <img src='http://s0.wp.com/latex.php?latex=%7Bn+%5Cdiv+k%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{n &#92;div k}' title='{n &#92;div k}' class='latex' /> is indeed a solution to the equation on the right; reading the equivalence from right to left, we get that <img src='http://s0.wp.com/latex.php?latex=%7Bn+%5Cdiv+k+%5Cge+m%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{n &#92;div k &#92;ge m}' title='{n &#92;div k &#92;ge m}' class='latex' /> for any solution <img src='http://s0.wp.com/latex.php?latex=%7Bm%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{m}' title='{m}' class='latex' />, so <img src='http://s0.wp.com/latex.php?latex=%7Bn+%5Cdiv+k%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{n &#92;div k}' title='{n &#92;div k}' class='latex' /> is in fact the greatest solution.</p>
<p>
Similarly, the characterization of the floor function <img src='http://s0.wp.com/latex.php?latex=%7B%5Clfloor%5Ccdot%5Crfloor%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;lfloor&#92;cdot&#92;rfloor}' title='{&#92;lfloor&#92;cdot&#92;rfloor}' class='latex' /> from reals to integers </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathit%7Binj%7D%28n%29+%5Cle_R+x+%5CLeftrightarrow+n+%5Cle_I+%5Clfloor+x+%5Crfloor+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathit{inj}(n) &#92;le_R x &#92;Leftrightarrow n &#92;le_I &#92;lfloor x &#92;rfloor ' title='&#92;displaystyle  &#92;mathit{inj}(n) &#92;le_R x &#92;Leftrightarrow n &#92;le_I &#92;lfloor x &#92;rfloor ' class='latex' />
</p></blockquote>
<p> defines <img src='http://s0.wp.com/latex.php?latex=%7B%5Clfloor+x+%5Crfloor%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;lfloor x &#92;rfloor}' title='{&#92;lfloor x &#92;rfloor}' class='latex' /> as the greatest integer <img src='http://s0.wp.com/latex.php?latex=%7Bn%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{n}' title='{n}' class='latex' /> for which <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Binj%7D%28n%29+%5Cle_R+x%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{inj}(n) &#92;le_R x}' title='{&#92;mathit{inj}(n) &#92;le_R x}' class='latex' />, and the Galois connection involving <img src='http://s0.wp.com/latex.php?latex=%7B%5Ccap%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;cap}' title='{&#92;cap}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B%5Ccup%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;cup}' title='{&#92;cup}' class='latex' /> </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++A+%5Ccap+X+%5Csubseteq+B+%5CLeftrightarrow+A+%5Csubseteq+B+%5Ccup+%5Coverline%7BX%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  A &#92;cap X &#92;subseteq B &#92;Leftrightarrow A &#92;subseteq B &#92;cup &#92;overline{X} ' title='&#92;displaystyle  A &#92;cap X &#92;subseteq B &#92;Leftrightarrow A &#92;subseteq B &#92;cup &#92;overline{X} ' class='latex' />
</p></blockquote>
<p> characterizes <img src='http://s0.wp.com/latex.php?latex=%7BB+%5Ccup+%5Coverline%7BX%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{B &#92;cup &#92;overline{X}}' title='{B &#92;cup &#92;overline{X}}' class='latex' /> as the greatest set <img src='http://s0.wp.com/latex.php?latex=%7BA%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{A}' title='{A}' class='latex' /> (under the usual subset ordering) for which <img src='http://s0.wp.com/latex.php?latex=%7BA+%5Ccap+X+%5Csubseteq+B%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{A &#92;cap X &#92;subseteq B}' title='{A &#92;cap X &#92;subseteq B}' class='latex' />.</p>
<p><h2> Limits and colimits </h2>
<p>
The characterization of greatest solutions might be equivalently expressed in terms of <em>greatest lower bounds</em>. Given a preordered set <img src='http://s0.wp.com/latex.php?latex=%7B%28X%2C%5Cle%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(X,&#92;le)}' title='{(X,&#92;le)}' class='latex' />, and a subset <img src='http://s0.wp.com/latex.php?latex=%7BY%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{Y}' title='{Y}' class='latex' /> of <img src='http://s0.wp.com/latex.php?latex=%7BX%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{X}' title='{X}' class='latex' />, an element <img src='http://s0.wp.com/latex.php?latex=%7Bx+%5Cin+X%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{x &#92;in X}' title='{x &#92;in X}' class='latex' /> is a <em>lower bound</em> of <img src='http://s0.wp.com/latex.php?latex=%7BY%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{Y}' title='{Y}' class='latex' /> in <img src='http://s0.wp.com/latex.php?latex=%7BX%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{X}' title='{X}' class='latex' /> if <img src='http://s0.wp.com/latex.php?latex=%7Bx+%5Cle+y%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{x &#92;le y}' title='{x &#92;le y}' class='latex' /> for every <img src='http://s0.wp.com/latex.php?latex=%7By+%5Cin+Y%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{y &#92;in Y}' title='{y &#92;in Y}' class='latex' />; in addition, <img src='http://s0.wp.com/latex.php?latex=%7Bx%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{x}' title='{x}' class='latex' /> is a <em>greatest lower bound</em> <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathrm%7Binf%7D%28Y%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathrm{inf}(Y)}' title='{&#92;mathrm{inf}(Y)}' class='latex' /> of <img src='http://s0.wp.com/latex.php?latex=%7BY%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{Y}' title='{Y}' class='latex' /> if <img src='http://s0.wp.com/latex.php?latex=%7Bx%27+%5Cle+x%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{x&#039; &#92;le x}' title='{x&#039; &#92;le x}' class='latex' /> for any other lower bound <img src='http://s0.wp.com/latex.php?latex=%7Bx%27%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{x&#039;}' title='{x&#039;}' class='latex' />. (Note &#8220;a&#8221; rather than &#8220;the&#8221;, as there may be multiple such. But they are all related by <img src='http://s0.wp.com/latex.php?latex=%7B%5Cle%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;le}' title='{&#92;le}' class='latex' />; if the ordering is a partial order, the <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathrm%7Binf%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathrm{inf}}' title='{&#92;mathrm{inf}}' class='latex' /> is unique when it exists. Note also that <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathrm%7Binf%7D%28Y%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathrm{inf}(Y)}' title='{&#92;mathrm{inf}(Y)}' class='latex' /> need not be in <img src='http://s0.wp.com/latex.php?latex=%7BY%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{Y}' title='{Y}' class='latex' /> itself, even when it does exist.)</p>
<p>
This construction can be phrased in terms of Galois connections as follows. The two ordered sets are <img src='http://s0.wp.com/latex.php?latex=%7B%28X%2C%5Cle%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(X,&#92;le)}' title='{(X,&#92;le)}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B%28%5Cmathsf%7BP%7D%5E%2B%28X%29%2C%5Cle%5E%5Cast%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(&#92;mathsf{P}^+(X),&#92;le^&#92;ast)}' title='{(&#92;mathsf{P}^+(X),&#92;le^&#92;ast)}' class='latex' />, where <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BP%7D%5E%2B%28X%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{P}^+(X)}' title='{&#92;mathsf{P}^+(X)}' class='latex' /> is the set of <em>nonempty</em> subsets of <img src='http://s0.wp.com/latex.php?latex=%7BX%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{X}' title='{X}' class='latex' />, with ordering <img src='http://s0.wp.com/latex.php?latex=%7B%5Cle%5E%5Cast%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;le^&#92;ast}' title='{&#92;le^&#92;ast}' class='latex' /> defined pointwise: <img src='http://s0.wp.com/latex.php?latex=%7BY+%5Cle%5E%5Cast+Z%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{Y &#92;le^&#92;ast Z}' title='{Y &#92;le^&#92;ast Z}' class='latex' /> iff <img src='http://s0.wp.com/latex.php?latex=%7Bx+%5Cle+x%27%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{x &#92;le x&#039;}' title='{x &#92;le x&#039;}' class='latex' /> for all <img src='http://s0.wp.com/latex.php?latex=%7Bx+%5Cin+Y%2C+x%27+%5Cin+Z%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{x &#92;in Y, x&#039; &#92;in Z}' title='{x &#92;in Y, x&#039; &#92;in Z}' class='latex' />. The mappings in either direction are the singleton set former <img src='http://s0.wp.com/latex.php?latex=%7B%5C%7B%5Ccdot%5C%7D+%3A+X+%5Crightarrow+%5Cmathsf%7BP%7D%5E%2B%28X%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;{&#92;cdot&#92;} : X &#92;rightarrow &#92;mathsf{P}^+(X)}' title='{&#92;{&#92;cdot&#92;} : X &#92;rightarrow &#92;mathsf{P}^+(X)}' class='latex' /> and greatest lower bound <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathrm%7Binf%7D+%3A+%5Cmathsf%7BP%7D%5E%2B%28X%29+%5Crightarrow+X%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathrm{inf} : &#92;mathsf{P}^+(X) &#92;rightarrow X}' title='{&#92;mathrm{inf} : &#92;mathsf{P}^+(X) &#92;rightarrow X}' class='latex' />, related by the Galois connection <img src='http://s0.wp.com/latex.php?latex=%7Bx+%5Cle+%5Cmathrm%7Binf%7D%28Y%29+%5CLeftrightarrow+%5C%7Bx%5C%7D+%5Cle%5E%5Cast+Y%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{x &#92;le &#92;mathrm{inf}(Y) &#92;Leftrightarrow &#92;{x&#92;} &#92;le^&#92;ast Y}' title='{x &#92;le &#92;mathrm{inf}(Y) &#92;Leftrightarrow &#92;{x&#92;} &#92;le^&#92;ast Y}' class='latex' />. Here&#8217;s how it looks with <img src='http://s0.wp.com/latex.php?latex=%7Bx%27+%5Cle+x+%3D+%5Cmathrm%7Binf%7D%28Y%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{x&#039; &#92;le x = &#92;mathrm{inf}(Y)}' title='{x&#039; &#92;le x = &#92;mathrm{inf}(Y)}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7BY+%3D+%5C%7B+x_0%2Cx_1%2Cx_2+%5C%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{Y = &#92;{ x_0,x_1,x_2 &#92;}}' title='{Y = &#92;{ x_0,x_1,x_2 &#92;}}' class='latex' />:
<p align="center"><img width="200" src="http://patternsinfp.files.wordpress.com/2011/03/extreme-inf.png?w=200"></p>
<p>
The categorical perspective on greatest lower bounds is the notion of <em>limit</em>; it&#8217;s just the generalization of the diagram above to an arbitrary category. Here is a very brief outline. The fragment of the diagram consisting of <img src='http://s0.wp.com/latex.php?latex=%7Bx%2C+x_0%2C+x_1%2C+x_2%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{x, x_0, x_1, x_2}' title='{x, x_0, x_1, x_2}' class='latex' /> is called a <em>cone</em>, from vertex <img src='http://s0.wp.com/latex.php?latex=%7Bx%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{x}' title='{x}' class='latex' /> to base <img src='http://s0.wp.com/latex.php?latex=%7Bx_0%2Cx_1%2Cx_2%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{x_0,x_1,x_2}' title='{x_0,x_1,x_2}' class='latex' /> (and so is <img src='http://s0.wp.com/latex.php?latex=%7Bx%27%2Cx_0%2Cx_1%2Cx_2%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{x&#039;,x_0,x_1,x_2}' title='{x&#039;,x_0,x_1,x_2}' class='latex' />). The cone <img src='http://s0.wp.com/latex.php?latex=%7Bx%2Cx_0%2Cx_1%2Cx_2%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{x,x_0,x_1,x_2}' title='{x,x_0,x_1,x_2}' class='latex' /> is called a <em>limit</em> when, for any other cone from vertex <img src='http://s0.wp.com/latex.php?latex=%7Bx%27%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{x&#039;}' title='{x&#039;}' class='latex' /> to the same base, there is a unique arrow <img src='http://s0.wp.com/latex.php?latex=%7Bx%27+%5Crightarrow+x%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{x&#039; &#92;rightarrow x}' title='{x&#039; &#92;rightarrow x}' class='latex' /> making the diagram commute. </p>
<p>
Commutativity of the diagram above isn&#8217;t very interesting&mdash;because the category is a partial order, but also because the base is degenerate: just three discrete objects. In general, the base will also contain arrows; then a cone consists of a vertex (<img src='http://s0.wp.com/latex.php?latex=%7BX%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{X}' title='{X}' class='latex' /> in the diagram below) with arrows to each of the objects in the base (<img src='http://s0.wp.com/latex.php?latex=%7Bf_i+%3A+X+%5Crightarrow+X_i%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f_i : X &#92;rightarrow X_i}' title='{f_i : X &#92;rightarrow X_i}' class='latex' />) making the diagram commute (<img src='http://s0.wp.com/latex.php?latex=%7Bg_0+%5Ccdot+f_0+%3D+f_1%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{g_0 &#92;cdot f_0 = f_1}' title='{g_0 &#92;cdot f_0 = f_1}' class='latex' />, etc). As before, the cone from vertex <img src='http://s0.wp.com/latex.php?latex=%7BX%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{X}' title='{X}' class='latex' /> is a limit if any other cone factors uniquely through it.
<p align="center"><img width="200" src="http://patternsinfp.files.wordpress.com/2011/03/extreme-limit1.png?w=200"></p>
<p>
Of course, it all dualizes beautifully. The categorical perspective on least upper bounds is expressed in terms of cones <em>from</em> a base (<img src='http://s0.wp.com/latex.php?latex=%7BX_0%2CX_1%2CX_2%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{X_0,X_1,X_2}' title='{X_0,X_1,X_2}' class='latex' /> below) to a vertex (<img src='http://s0.wp.com/latex.php?latex=%7BX%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{X}' title='{X}' class='latex' />), being a <em>colimit</em> if allowing unique factorization for any other cone from the same base to another vertex (<img src='http://s0.wp.com/latex.php?latex=%7BX%27%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{X&#039;}' title='{X&#039;}' class='latex' />).
<p align="center"><img width="200" src="http://patternsinfp.files.wordpress.com/2011/03/extreme-colimit1.png?w=200"></p>
<p><h2> Initial algebras and final coalgebras </h2>
<p>
Recall that, for a functor <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />, an <em><img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />-algebra</em> is a pair <img src='http://s0.wp.com/latex.php?latex=%7B%28X%2Cf%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(X,f)}' title='{(X,f)}' class='latex' /> consisting of an object <img src='http://s0.wp.com/latex.php?latex=%7BX%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{X}' title='{X}' class='latex' /> and an arrow <img src='http://s0.wp.com/latex.php?latex=%7Bf+%3A+%5Cmathsf%7BF%7D%28X%29+%5Crightarrow+X%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f : &#92;mathsf{F}(X) &#92;rightarrow X}' title='{f : &#92;mathsf{F}(X) &#92;rightarrow X}' class='latex' />. A <em>homomorphism</em> between <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />-algebras <img src='http://s0.wp.com/latex.php?latex=%7B%28X%2Cf%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(X,f)}' title='{(X,f)}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B%28Y%2Cg%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(Y,g)}' title='{(Y,g)}' class='latex' /> is an arrow <img src='http://s0.wp.com/latex.php?latex=%7Bh+%3A+X+%5Crightarrow+Y%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h : X &#92;rightarrow Y}' title='{h : X &#92;rightarrow Y}' class='latex' /> such that:
<p align="center"><img width="150" src="http://patternsinfp.files.wordpress.com/2011/03/extreme-homomorphism.png?w=150"></p>
<p>  The <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />-algebra <img src='http://s0.wp.com/latex.php?latex=%7B%28X%2Cf%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(X,f)}' title='{(X,f)}' class='latex' /> is <em>initial</em> if there is a unique such <img src='http://s0.wp.com/latex.php?latex=%7Bh%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h}' title='{h}' class='latex' /> for each <img src='http://s0.wp.com/latex.php?latex=%7B%28Y%2Cg%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(Y,g)}' title='{(Y,g)}' class='latex' />. We usually write <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmu%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mu&#92;mathsf{F}}' title='{&#92;mu&#92;mathsf{F}}' class='latex' /> for the &#8220;carrier&#8221; of this initial algebra (because it is the &#8220;least fixed point&#8221; of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />, as we shall see below), and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bin%7D+%3A+%5Cmathsf%7BF%7D%28%5Cmu%5Cmathsf%7BF%7D%29+%5Crightarrow+%5Cmu%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{in} : &#92;mathsf{F}(&#92;mu&#92;mathsf{F}) &#92;rightarrow &#92;mu&#92;mathsf{F}}' title='{&#92;mathit{in} : &#92;mathsf{F}(&#92;mu&#92;mathsf{F}) &#92;rightarrow &#92;mu&#92;mathsf{F}}' class='latex' /> for the &#8220;constructor&#8221; (and indeed, it is an isomorphism, so a constructed piece of data can be deconstructed again); we write <img src='http://s0.wp.com/latex.php?latex=%7Bh%3D%5Cmathit%7Bfold%7D%28g%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h=&#92;mathit{fold}(g)}' title='{h=&#92;mathit{fold}(g)}' class='latex' /> for the unique <img src='http://s0.wp.com/latex.php?latex=%7Bh%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h}' title='{h}' class='latex' /> such that <img src='http://s0.wp.com/latex.php?latex=%7Bh+%5Ccdot+%5Cmathit%7Bin%7D+%3D+g+%5Ccdot+%5Cmathsf%7BF%7D%28h%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h &#92;cdot &#92;mathit{in} = g &#92;cdot &#92;mathsf{F}(h)}' title='{h &#92;cdot &#92;mathit{in} = g &#92;cdot &#92;mathsf{F}(h)}' class='latex' />.</p>
<p>
As you might expect, &#8220;initial&#8221; things are extreme solutions too, albeit not in a very interesting way. An <em>initial object</em> in a category is an object from which there is a unique arrow (often written &#8220;<img src='http://s0.wp.com/latex.php?latex=%7B%21%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{!}' title='{!}' class='latex' />&#8220;) to any other object. An initial object is a colimit of the diagram generated from the empty category&mdash;which has no objects, and hence no arrows either. (Any object forms the vertex of a (trivial) cone, so the colimiting vertex is simply one from which there is a unique arrow to any other vertex, with no additional constraints.) In particular, an initial <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />-algebra is an initial object in the category of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />-algebras, whose objects are <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />-algebras and whose arrows are homomorphisms between them.</p>
<p>
And of course, it all dualizes nicely, to final coalgebras, which are in some sense &#8220;greatest fixed points&#8221; of functors; final objects are the vertices of limiting cones <em>to</em> the empty base.</p>
<p><h2> Extreme (co-)algebras as (co-)limits </h2>
<p>
Here is a more illuminating presentation of initial algebras as extreme solutions, explaining rather better in what way they correspond to &#8220;least fixed points&#8221; of functors. (The construction is well known; I&#8217;ve based this presentation on a <a href="http://www.pps.jussieu.fr/~metayer/PDF/fix.pdf">manuscript</a> by Fran&ccedil;ois M&eacute;tayer.) Initial algebras can be constructed as an instance of the colimit construction above, in which the base consists of a <em>countable chain</em> of objects and arrows:
<p align="center"><img width="300" src="http://patternsinfp.files.wordpress.com/2011/03/extreme-chain.png?w=300"></p>
<p>  In the category <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{S}&#92;mathrm{et}}' title='{&#92;mathbb{S}&#92;mathrm{et}}' class='latex' />, every such a chain has a colimit (categories with this property are called <em><img src='http://s0.wp.com/latex.php?latex=%7B%5Comega%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;omega}' title='{&#92;omega}' class='latex' />-categories</em>). </p>
<p>
If the category has an <em>initial object</em> <img src='http://s0.wp.com/latex.php?latex=%7B0%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{0}' title='{0}' class='latex' />, then any endofunctor <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' /> induces such a countable chain:
<p align="center"><img width="300" src="http://patternsinfp.files.wordpress.com/2011/03/extreme-fchain.png?w=300"></p>
<p>  Under mild assumptions, the colimit of this chain is (the carrier of) an initial <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />-algebra. (Besides assuming an <img src='http://s0.wp.com/latex.php?latex=%7B%5Comega%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;omega}' title='{&#92;omega}' class='latex' />-category with an initial object, we have to assume that <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' /> is <em><img src='http://s0.wp.com/latex.php?latex=%7B%5Comega%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;omega}' title='{&#92;omega}' class='latex' />-cocontinuous</em>&mdash;that is, that it transforms the colimit <img src='http://s0.wp.com/latex.php?latex=%7BX%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{X}' title='{X}' class='latex' /> of the countable chain <img src='http://s0.wp.com/latex.php?latex=%7BX_0+%5Crightarrow+X_1+%5Crightarrow+%5Ccdots%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{X_0 &#92;rightarrow X_1 &#92;rightarrow &#92;cdots}' title='{X_0 &#92;rightarrow X_1 &#92;rightarrow &#92;cdots}' class='latex' /> into a colimit <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%28X%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}(X)}' title='{&#92;mathsf{F}(X)}' class='latex' /> of the countable chain <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%28X_0%29+%5Crightarrow+%5Cmathsf%7BF%7D%28X_1%29+%5Crightarrow+%5Ccdots%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}(X_0) &#92;rightarrow &#92;mathsf{F}(X_1) &#92;rightarrow &#92;cdots}' title='{&#92;mathsf{F}(X_0) &#92;rightarrow &#92;mathsf{F}(X_1) &#92;rightarrow &#92;cdots}' class='latex' />. One can show that any <em>polynomial functor</em>&mdash;one built from constants using sum and product&mdash;is <img src='http://s0.wp.com/latex.php?latex=%7B%5Comega%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;omega}' title='{&#92;omega}' class='latex' />-cocontinuous.)</p>
<p>
The construction goes as follows. By assumption, the countable chain <img src='http://s0.wp.com/latex.php?latex=%7B0+%5Crightarrow+%5Cmathsf%7BF%7D%280%29+%5Crightarrow+%5Ccdots%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{0 &#92;rightarrow &#92;mathsf{F}(0) &#92;rightarrow &#92;cdots}' title='{0 &#92;rightarrow &#92;mathsf{F}(0) &#92;rightarrow &#92;cdots}' class='latex' /> has a colimit; let&#8217;s suggestively call the vertex <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmu+%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mu &#92;mathsf{F}}' title='{&#92;mu &#92;mathsf{F}}' class='latex' />, so that the edges <img src='http://s0.wp.com/latex.php?latex=%7Bu_i+%3A+%5Cmathsf%7BF%7D%5Ei%280%29+%5Crightarrow+%5Cmu%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{u_i : &#92;mathsf{F}^i(0) &#92;rightarrow &#92;mu&#92;mathsf{F}}' title='{u_i : &#92;mathsf{F}^i(0) &#92;rightarrow &#92;mu&#92;mathsf{F}}' class='latex' /> satisfy <img src='http://s0.wp.com/latex.php?latex=%7Bu_%7Bi%2B1%7D+%5Ccdot+%5Cmathsf%7BF%7D%5Ei%28%21%29+%3D+u_i%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{u_{i+1} &#92;cdot &#92;mathsf{F}^i(!) = u_i}' title='{u_{i+1} &#92;cdot &#92;mathsf{F}^i(!) = u_i}' class='latex' /> for each <img src='http://s0.wp.com/latex.php?latex=%7Bi%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{i}' title='{i}' class='latex' />.
<p align="center"><img width="300" src="http://patternsinfp.files.wordpress.com/2011/03/extreme-initial.png?w=300"></p>
<p>  Since <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' /> is <img src='http://s0.wp.com/latex.php?latex=%7B%5Comega%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;omega}' title='{&#92;omega}' class='latex' />-cocontinuous, it transforms this diagram into another colimiting cone, with base shifted one place to the right and vertex <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%28%5Cmu+%5Cmathsf%7BF%7D%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}(&#92;mu &#92;mathsf{F})}' title='{&#92;mathsf{F}(&#92;mu &#92;mathsf{F})}' class='latex' />. But <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmu+%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mu &#92;mathsf{F}}' title='{&#92;mu &#92;mathsf{F}}' class='latex' /> is the vertex of another cone over the same shifted base; and since <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%28%5Cmu+%5Cmathsf%7BF%7D%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}(&#92;mu &#92;mathsf{F})}' title='{&#92;mathsf{F}(&#92;mu &#92;mathsf{F})}' class='latex' /> is the colimit, there is a unique arrow&mdash;let&#8217;s suggestively call it <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bin%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{in}}' title='{&#92;mathit{in}}' class='latex' />&mdash;making the diagram below commute (<img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bin%7D+%5Ccdot+%5Cmathsf%7BF%7D%28u_i%29+%3D+u_%7Bi%2B1%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{in} &#92;cdot &#92;mathsf{F}(u_i) = u_{i+1}}' title='{&#92;mathit{in} &#92;cdot &#92;mathsf{F}(u_i) = u_{i+1}}' class='latex' /> etc).
<p align="center"><img width="300" src="http://patternsinfp.files.wordpress.com/2011/03/extreme-initial-shift.png?w=300"></p>
<p>  All we have to do now is to show that <img src='http://s0.wp.com/latex.php?latex=%7B%28%5Cmu%5Cmathsf%7BF%7D%2C+%5Cmathit%7Bin%7D%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(&#92;mu&#92;mathsf{F}, &#92;mathit{in})}' title='{(&#92;mu&#92;mathsf{F}, &#92;mathit{in})}' class='latex' /> is indeed the initial <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />-algebra, as claimed. Suppose we are given another <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />-algebra <img src='http://s0.wp.com/latex.php?latex=%7B%28Y%2Cg%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(Y,g)}' title='{(Y,g)}' class='latex' />; we will (i)~construct an arrow <img src='http://s0.wp.com/latex.php?latex=%7Bh+%3A+%5Cmu%5Cmathsf%7BF%7D+%5Crightarrow+Y%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h : &#92;mu&#92;mathsf{F} &#92;rightarrow Y}' title='{h : &#92;mu&#92;mathsf{F} &#92;rightarrow Y}' class='latex' />, (ii)~show that it is a homomorphism between the algebras, <img src='http://s0.wp.com/latex.php?latex=%7Bh+%5Ccdot+%5Cmathit%7Bin%7D+%3D+g+%5Ccdot+%5Cmathsf%7BF%7D%28h%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h &#92;cdot &#92;mathit{in} = g &#92;cdot &#92;mathsf{F}(h)}' title='{h &#92;cdot &#92;mathit{in} = g &#92;cdot &#92;mathsf{F}(h)}' class='latex' />, and (iii)~show that it is the only such.</p>
<p>
For (i), given the target <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />-algebra <img src='http://s0.wp.com/latex.php?latex=%7B%28Y%2Cg%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(Y,g)}' title='{(Y,g)}' class='latex' />, we can construct a square as follows:
<p align="center"><img width="150" src="http://patternsinfp.files.wordpress.com/2011/03/extreme-eval-box.png?w=150"></p>
<p>  which commutes by virtue of the initiality of <img src='http://s0.wp.com/latex.php?latex=%7B0%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{0}' title='{0}' class='latex' />. Applying <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' /> to this square yields another, which can be pasted alongside; and this can be repeated indefinitely, yielding the following ladder:
<p align="center"><img width="300" src="http://patternsinfp.files.wordpress.com/2011/03/extreme-eval-ladder.png?w=300"></p>
<p>  Then we can pick out arrows <img src='http://s0.wp.com/latex.php?latex=%7B%5Cxi_i+%3D+%5Cmathsf%7BF%7D%5E0%28g%29+%5Ccdot+%5Cmathsf%7BF%7D%5E1%28g%29+%5Ccdots+%5Cmathsf%7BF%7D%5E%7Bi-1%7D%28g%29+%5Ccdot+%5Cmathsf%7BF%7D%5Ei%28%21_Y%29+%3A+%5Cmathsf%7BF%7D%5Ei%280%29+%5Crightarrow+Y%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;xi_i = &#92;mathsf{F}^0(g) &#92;cdot &#92;mathsf{F}^1(g) &#92;cdots &#92;mathsf{F}^{i-1}(g) &#92;cdot &#92;mathsf{F}^i(!_Y) : &#92;mathsf{F}^i(0) &#92;rightarrow Y}' title='{&#92;xi_i = &#92;mathsf{F}^0(g) &#92;cdot &#92;mathsf{F}^1(g) &#92;cdots &#92;mathsf{F}^{i-1}(g) &#92;cdot &#92;mathsf{F}^i(!_Y) : &#92;mathsf{F}^i(0) &#92;rightarrow Y}' class='latex' />. Moreover, these <img src='http://s0.wp.com/latex.php?latex=%7B%5Cxi_i%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;xi_i}' title='{&#92;xi_i}' class='latex' />s commute with the base of the colimit diagram (<img src='http://s0.wp.com/latex.php?latex=%7B%5Cxi_%7Bi%2B1%7D+%5Ccdot+%5Cmathsf%7BF%7D%5Ei%28%21%29+%3D+%5Cxi_i%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;xi_{i+1} &#92;cdot &#92;mathsf{F}^i(!) = &#92;xi_i}' title='{&#92;xi_{i+1} &#92;cdot &#92;mathsf{F}^i(!) = &#92;xi_i}' class='latex' />, etc) to yield another cone to vertex <img src='http://s0.wp.com/latex.php?latex=%7BY%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{Y}' title='{Y}' class='latex' />; we therefore conclude that there is a unique <img src='http://s0.wp.com/latex.php?latex=%7Bh+%3A+%5Cmu%5Cmathsf%7BF%7D+%5Crightarrow+Y%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h : &#92;mu&#92;mathsf{F} &#92;rightarrow Y}' title='{h : &#92;mu&#92;mathsf{F} &#92;rightarrow Y}' class='latex' /> such that <img src='http://s0.wp.com/latex.php?latex=%7Bh+%5Ccdot+u_i+%3D+%5Cxi_i%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h &#92;cdot u_i = &#92;xi_i}' title='{h &#92;cdot u_i = &#92;xi_i}' class='latex' /> for each <img src='http://s0.wp.com/latex.php?latex=%7Bi%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{i}' title='{i}' class='latex' />.
<p align="center"><img width="300" src="http://patternsinfp.files.wordpress.com/2011/03/extreme-fold.png?w=300"></p>
<p>  Now for (ii). Note that <img src='http://s0.wp.com/latex.php?latex=%7BY%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{Y}' title='{Y}' class='latex' /> and the <img src='http://s0.wp.com/latex.php?latex=%7B%5Cxi_%7Bi%2B1%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;xi_{i+1}}' title='{&#92;xi_{i+1}}' class='latex' /> also form a cone over the shifted base starting from <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%280%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}(0)}' title='{&#92;mathsf{F}(0)}' class='latex' />; and because <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%28%5Cmu%5Cmathsf%7BF%7D%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}(&#92;mu&#92;mathsf{F})}' title='{&#92;mathsf{F}(&#92;mu&#92;mathsf{F})}' class='latex' /> is the colimit from this shifted base, we also get a unique mediating arrow <img src='http://s0.wp.com/latex.php?latex=%7Bk+%3A+%5Cmathsf%7BF%7D%28%5Cmu%5Cmathsf%7BF%7D%29+%5Crightarrow+Y%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{k : &#92;mathsf{F}(&#92;mu&#92;mathsf{F}) &#92;rightarrow Y}' title='{k : &#92;mathsf{F}(&#92;mu&#92;mathsf{F}) &#92;rightarrow Y}' class='latex' /> such that <img src='http://s0.wp.com/latex.php?latex=%7Bk+%5Ccdot+%5Cmathsf%7BF%7D%28u_i%29+%3D+%5Cxi_%7Bi%2B1%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{k &#92;cdot &#92;mathsf{F}(u_i) = &#92;xi_{i+1}}' title='{k &#92;cdot &#92;mathsf{F}(u_i) = &#92;xi_{i+1}}' class='latex' /> for each <img src='http://s0.wp.com/latex.php?latex=%7Bi%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{i}' title='{i}' class='latex' />.
<p align="center"><img width="300" src="http://patternsinfp.files.wordpress.com/2011/03/extreme-ffold.png?w=300"></p>
<p>  Moreover, both <img src='http://s0.wp.com/latex.php?latex=%7Bh+%5Ccdot+%5Cmathit%7Bin%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h &#92;cdot &#92;mathit{in}}' title='{h &#92;cdot &#92;mathit{in}}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7Bg+%5Ccdot+%5Cmathsf%7BF%7D%28h%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{g &#92;cdot &#92;mathsf{F}(h)}' title='{g &#92;cdot &#92;mathsf{F}(h)}' class='latex' /> are also such mediating arrows: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++h+%5Ccdot+%5Cmathit%7Bin%7D+%5Ccdot+%5Cmathsf%7BF%7D%28u_i%29+%3D+h+%5Ccdot+u_%7Bi%2B1%7D+%3D+%5Cxi_%7Bi%2B1%7D+%3D+g+%5Ccdot+%5Cmathsf%7BF%7D%28%5Cxi_i%29+%3D+g+%5Ccdot+%5Cmathsf%7BF%7D%28h+%5Ccdot+u_i%29+%3D+g+%5Ccdot+%5Cmathsf%7BF%7D%28h%29+%5Ccdot+%5Cmathsf%7BF%7D%28u_i%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  h &#92;cdot &#92;mathit{in} &#92;cdot &#92;mathsf{F}(u_i) = h &#92;cdot u_{i+1} = &#92;xi_{i+1} = g &#92;cdot &#92;mathsf{F}(&#92;xi_i) = g &#92;cdot &#92;mathsf{F}(h &#92;cdot u_i) = g &#92;cdot &#92;mathsf{F}(h) &#92;cdot &#92;mathsf{F}(u_i) ' title='&#92;displaystyle  h &#92;cdot &#92;mathit{in} &#92;cdot &#92;mathsf{F}(u_i) = h &#92;cdot u_{i+1} = &#92;xi_{i+1} = g &#92;cdot &#92;mathsf{F}(&#92;xi_i) = g &#92;cdot &#92;mathsf{F}(h &#92;cdot u_i) = g &#92;cdot &#92;mathsf{F}(h) &#92;cdot &#92;mathsf{F}(u_i) ' class='latex' />
</p></blockquote>
<p> so both must equal <img src='http://s0.wp.com/latex.php?latex=%7Bk%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{k}' title='{k}' class='latex' /> and hence also each other: <img src='http://s0.wp.com/latex.php?latex=%7Bh+%5Ccdot+%5Cmathit%7Bin%7D+%3D+g+%5Ccdot+%5Cmathsf%7BF%7D%28h%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h &#92;cdot &#92;mathit{in} = g &#92;cdot &#92;mathsf{F}(h)}' title='{h &#92;cdot &#92;mathit{in} = g &#92;cdot &#92;mathsf{F}(h)}' class='latex' />.</p>
<p>
Finally, for (iii), suppose we have another <img src='http://s0.wp.com/latex.php?latex=%7Bh%27+%3A+%5Cmu%5Cmathsf%7BF%7D+%5Crightarrow+Y%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h&#039; : &#92;mu&#92;mathsf{F} &#92;rightarrow Y}' title='{h&#039; : &#92;mu&#92;mathsf{F} &#92;rightarrow Y}' class='latex' /> for which <img src='http://s0.wp.com/latex.php?latex=%7Bh%27+%5Ccdot+%5Cmathit%7Bin%7D+%3D+g+%5Ccdot+%5Cmathsf%7BF%7D%28h%27%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h&#039; &#92;cdot &#92;mathit{in} = g &#92;cdot &#92;mathsf{F}(h&#039;)}' title='{h&#039; &#92;cdot &#92;mathit{in} = g &#92;cdot &#92;mathsf{F}(h&#039;)}' class='latex' />; we have to show that <img src='http://s0.wp.com/latex.php?latex=%7Bh%27+%3D+h%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h&#039; = h}' title='{h&#039; = h}' class='latex' />. By the uniqueness of the mediating arrow, it suffices to show that <img src='http://s0.wp.com/latex.php?latex=%7Bh+%5Ccdot+u_i+%3D+%5Cxi_i%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h &#92;cdot u_i = &#92;xi_i}' title='{h &#92;cdot u_i = &#92;xi_i}' class='latex' /> for each <img src='http://s0.wp.com/latex.php?latex=%7Bi%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{i}' title='{i}' class='latex' />, which is easily done by induction.</p>
<p>
That is, given <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />-algebra <img src='http://s0.wp.com/latex.php?latex=%7B%28Y%2Cg%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(Y,g)}' title='{(Y,g)}' class='latex' />, there exists a unique <img src='http://s0.wp.com/latex.php?latex=%7Bh+%3A+%5Cmu%5Cmathsf%7BF%7D+%5Crightarrow+Y%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h : &#92;mu&#92;mathsf{F} &#92;rightarrow Y}' title='{h : &#92;mu&#92;mathsf{F} &#92;rightarrow Y}' class='latex' /> (for which we write &#8220;<img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfold%7D%28g%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{fold}(g)}' title='{&#92;mathit{fold}(g)}' class='latex' />&#8220;) such that <img src='http://s0.wp.com/latex.php?latex=%7Bh+%5Ccdot+%5Cmathit%7Bin%7D+%3D+g+%5Ccdot+%5Cmathsf%7BF%7D%28h%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h &#92;cdot &#92;mathit{in} = g &#92;cdot &#92;mathsf{F}(h)}' title='{h &#92;cdot &#92;mathit{in} = g &#92;cdot &#92;mathsf{F}(h)}' class='latex' />. If you squint at this in the right way, you can see the inductive definition of the recursive datatype, and of the folds over it. Each <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%5Ei%280%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}^i(0)}' title='{&#92;mathsf{F}^i(0)}' class='latex' /> is an approximation to <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmu%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mu&#92;mathsf{F}}' title='{&#92;mu&#92;mathsf{F}}' class='latex' />, cut off at depth <img src='http://s0.wp.com/latex.php?latex=%7Bi%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{i}' title='{i}' class='latex' />; they all embed into <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmu%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mu&#92;mathsf{F}}' title='{&#92;mu&#92;mathsf{F}}' class='latex' />, and indeed, <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmu%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mu&#92;mathsf{F}}' title='{&#92;mu&#92;mathsf{F}}' class='latex' /> is the least extension&mdash;the colimit&mdash;of them all. Each <img src='http://s0.wp.com/latex.php?latex=%7B%5Cxi_i%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;xi_i}' title='{&#92;xi_i}' class='latex' /> is an approximation to <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfold%7D%28g%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{fold}(g)}' title='{&#92;mathit{fold}(g)}' class='latex' />, again restricted to data structures cut off at depth <img src='http://s0.wp.com/latex.php?latex=%7Bi%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{i}' title='{i}' class='latex' />, and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfold%7D%28g%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{fold}(g)}' title='{&#92;mathit{fold}(g)}' class='latex' /> is the completion of all the <img src='http://s0.wp.com/latex.php?latex=%7B%5Cxi_i%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;xi_i}' title='{&#92;xi_i}' class='latex' />.</p>
<p>
Naturally, it all dualizes for final coalgebras: then we need &#8220;cochains&#8221; <img src='http://s0.wp.com/latex.php?latex=%7B1+%5Cleftarrow+%5Cmathsf%7BF%7D%281%29+%5Cleftarrow+%5Cmathsf%7BF%7D%5E2%281%29+%5Cleftarrow+%5Ccdots%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{1 &#92;leftarrow &#92;mathsf{F}(1) &#92;leftarrow &#92;mathsf{F}^2(1) &#92;leftarrow &#92;cdots}' title='{1 &#92;leftarrow &#92;mathsf{F}(1) &#92;leftarrow &#92;mathsf{F}^2(1) &#92;leftarrow &#92;cdots}' class='latex' /> to a terminal object <img src='http://s0.wp.com/latex.php?latex=%7B1%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{1}' title='{1}' class='latex' />; an <img src='http://s0.wp.com/latex.php?latex=%7B%5Comega%5E%7B%5Cmathrm%7Bop%7D%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;omega^{&#92;mathrm{op}}}' title='{&#92;omega^{&#92;mathrm{op}}}' class='latex' />-category is one in which all such countable cochains have a limit; <img src='http://s0.wp.com/latex.php?latex=%7B%5Comega%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;omega}' title='{&#92;omega}' class='latex' />-continuous functors preserve limits of countable cochains. (It is a bit unfortunate that the interesting extreme algebra, namely the initial algebra, is a <em>co</em>limit, whereas the final <em>co</em>algebra is a limit, but sometimes life is like that.)</p>
<p><h2> (Co-)limits as adjunctions </h2>
<p>
The definition of limits can be made more concise and precise by noting that the base corresponds to the image of some functor <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D+%3A+%5Cmathbb%7BJ%7D+%5Crightarrow+%5Cmathbb%7BC%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F} : &#92;mathbb{J} &#92;rightarrow &#92;mathbb{C}}' title='{&#92;mathsf{F} : &#92;mathbb{J} &#92;rightarrow &#92;mathbb{C}}' class='latex' />, where <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BC%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{C}}' title='{&#92;mathbb{C}}' class='latex' /> is the category of interest, and <em>index category</em> <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BJ%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{J}}' title='{&#92;mathbb{J}}' class='latex' /> determines the shape of the base&mdash;for each object <img src='http://s0.wp.com/latex.php?latex=%7Bj%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{j}' title='{j}' class='latex' /> of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BJ%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{J}}' title='{&#92;mathbb{J}}' class='latex' />, there is an object <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%28j%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}(j)}' title='{&#92;mathsf{F}(j)}' class='latex' /> of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BC%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{C}}' title='{&#92;mathbb{C}}' class='latex' /> in the base (<img src='http://s0.wp.com/latex.php?latex=%7BX_j%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{X_j}' title='{X_j}' class='latex' /> in the diagram below), and for each arrow <img src='http://s0.wp.com/latex.php?latex=%7Ba+%3A+i+%5Crightarrow+j%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{a : i &#92;rightarrow j}' title='{a : i &#92;rightarrow j}' class='latex' /> of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BJ%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{J}}' title='{&#92;mathbb{J}}' class='latex' />, an arrow <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%28a%29+%3A+%5Cmathsf%7BF%7D%28i%29+%5Crightarrow+%5Cmathsf%7BF%7D%28j%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}(a) : &#92;mathsf{F}(i) &#92;rightarrow &#92;mathsf{F}(j)}' title='{&#92;mathsf{F}(a) : &#92;mathsf{F}(i) &#92;rightarrow &#92;mathsf{F}(j)}' class='latex' /> of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BC%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{C}}' title='{&#92;mathbb{C}}' class='latex' /> in the base (<img src='http://s0.wp.com/latex.php?latex=%7Bg_a+%3A+X_i+%5Crightarrow+X_j%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{g_a : X_i &#92;rightarrow X_j}' title='{g_a : X_i &#92;rightarrow X_j}' class='latex' /> below).
<p align="center"><img width="200" src="http://patternsinfp.files.wordpress.com/2011/04/extreme-adjn.png?w=200"></p>
<p>  (In the <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathrm%7Binf%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathrm{inf}}' title='{&#92;mathrm{inf}}' class='latex' /> diagram, the index category is the discrete category on three objects&mdash;with no arrows other than identity arrows. In the diagram above, <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BJ%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{J}}' title='{&#92;mathbb{J}}' class='latex' /> is <img src='http://s0.wp.com/latex.php?latex=%7B%5Cbullet+%5Cmathrel%7B%7B-%7D%5Cvcenter%7B%5Chbox%7B%5Cscriptsize0%7D%7D%7B%5Crightarrow%7D%7D+%5Cbullet+%5Cmathrel%7B%7B%5Cleftarrow%7D%5Cvcenter%7B%5Chbox%7B%5Cscriptsize2%7D%7D%7B-%7D%7D+%5Cbullet%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;bullet &#92;mathrel{{-}&#92;vcenter{&#92;hbox{&#92;scriptsize0}}{&#92;rightarrow}} &#92;bullet &#92;mathrel{{&#92;leftarrow}&#92;vcenter{&#92;hbox{&#92;scriptsize2}}{-}} &#92;bullet}' title='{&#92;bullet &#92;mathrel{{-}&#92;vcenter{&#92;hbox{&#92;scriptsize0}}{&#92;rightarrow}} &#92;bullet &#92;mathrel{{&#92;leftarrow}&#92;vcenter{&#92;hbox{&#92;scriptsize2}}{-}} &#92;bullet}' class='latex' />, with three objects and two generating arrows. In the construction of initial algebras, the index category is <img src='http://s0.wp.com/latex.php?latex=%7B%5Comega+%3D+%5Cbullet+%5Crightarrow+%5Cbullet+%5Crightarrow+%5Ccdots%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;omega = &#92;bullet &#92;rightarrow &#92;bullet &#92;rightarrow &#92;cdots}' title='{&#92;omega = &#92;bullet &#92;rightarrow &#92;bullet &#92;rightarrow &#92;cdots}' class='latex' />, equivalent to the usual <img src='http://s0.wp.com/latex.php?latex=%7B%5Cle%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;le}' title='{&#92;le}' class='latex' /> ordering on the natural numbers, whereas for final coalgebras it is <img src='http://s0.wp.com/latex.php?latex=%7B%5Cbullet+%5Cleftarrow+%5Cbullet+%5Cleftarrow+%5Ccdots%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;bullet &#92;leftarrow &#92;bullet &#92;leftarrow &#92;cdots}' title='{&#92;bullet &#92;leftarrow &#92;bullet &#92;leftarrow &#92;cdots}' class='latex' />, equivalent to <img src='http://s0.wp.com/latex.php?latex=%7B%5Cge%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;ge}' title='{&#92;ge}' class='latex' /> on natural numbers.)</p>
<p>
The vertex <img src='http://s0.wp.com/latex.php?latex=%7BX%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{X}' title='{X}' class='latex' /> too can be seen as the image of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BJ%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{J}}' title='{&#92;mathbb{J}}' class='latex' /> under a particular, degenerate functor&mdash;the diagonal functor <img src='http://s0.wp.com/latex.php?latex=%7B%5CDelta+X+%3A+%5Cmathbb%7BJ%7D+%5Crightarrow+%5Cmathbb%7BC%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;Delta X : &#92;mathbb{J} &#92;rightarrow &#92;mathbb{C}}' title='{&#92;Delta X : &#92;mathbb{J} &#92;rightarrow &#92;mathbb{C}}' class='latex' />, defined by <img src='http://s0.wp.com/latex.php?latex=%7B%5CDelta+X%28j%29+%3D+X%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;Delta X(j) = X}' title='{&#92;Delta X(j) = X}' class='latex' /> for each object <img src='http://s0.wp.com/latex.php?latex=%7Bj%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{j}' title='{j}' class='latex' /> of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BJ%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{J}}' title='{&#92;mathbb{J}}' class='latex' />, and <img src='http://s0.wp.com/latex.php?latex=%7B%5CDelta+X%28a%29+%3D+%5Cmathit%7Bid%7D_X%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;Delta X(a) = &#92;mathit{id}_X}' title='{&#92;Delta X(a) = &#92;mathit{id}_X}' class='latex' /> for each arrow <img src='http://s0.wp.com/latex.php?latex=%7Ba%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{a}' title='{a}' class='latex' />. Then &#8220;the cone <img src='http://s0.wp.com/latex.php?latex=%7Bf%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f}' title='{f}' class='latex' /> from vertex <img src='http://s0.wp.com/latex.php?latex=%7BX%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{X}' title='{X}' class='latex' /> to base <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />&#8221; corresponds to a natural transformation <img src='http://s0.wp.com/latex.php?latex=%7Bf+%3A+%5CDelta+X+%5Cmathbin%7B%5Cstackrel%7B.%7D%7B%5Cto%7D%7D+%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f : &#92;Delta X &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{F}}' title='{f : &#92;Delta X &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{F}}' class='latex' />: naturality is exactly the condition that the cone commutes. We write &#8220;<img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathrm%7BLim%7D%5C%2C%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathrm{Lim}&#92;,&#92;mathsf{F}}' title='{&#92;mathrm{Lim}&#92;,&#92;mathsf{F}}' class='latex' />&#8221; for the limiting object, <img src='http://s0.wp.com/latex.php?latex=%7BX%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{X}' title='{X}' class='latex' />; its universal property is that, for any cone <img src='http://s0.wp.com/latex.php?latex=%7Bf%27%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f&#039;}' title='{f&#039;}' class='latex' /> from <img src='http://s0.wp.com/latex.php?latex=%7BX%27%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{X&#039;}' title='{X&#039;}' class='latex' /> to <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' />, there exists a unique <img src='http://s0.wp.com/latex.php?latex=%7Bh+%3A+X%27+%5Crightarrow+X%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h : X&#039; &#92;rightarrow X}' title='{h : X&#039; &#92;rightarrow X}' class='latex' /> such that <img src='http://s0.wp.com/latex.php?latex=%7Bf_i+%5Ccdot+h+%3D+f%27_i%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f_i &#92;cdot h = f&#039;_i}' title='{f_i &#92;cdot h = f&#039;_i}' class='latex' /> for each <img src='http://s0.wp.com/latex.php?latex=%7Bi%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{i}' title='{i}' class='latex' />. In other words, there is a (natural) isomorphism between the natural transformations <img src='http://s0.wp.com/latex.php?latex=%7B%5CDelta+X%27+%5Cmathbin%7B%5Cstackrel%7B.%7D%7B%5Cto%7D%7D+%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;Delta X&#039; &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{F}}' title='{&#92;Delta X&#039; &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{F}}' class='latex' /> and the arrows <img src='http://s0.wp.com/latex.php?latex=%7BX%27+%5Crightarrow+%5Cmathrm%7BLim%7D%5C%2C%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{X&#039; &#92;rightarrow &#92;mathrm{Lim}&#92;,&#92;mathsf{F}}' title='{X&#039; &#92;rightarrow &#92;mathrm{Lim}&#92;,&#92;mathsf{F}}' class='latex' />; that is, an adjunction <img src='http://s0.wp.com/latex.php?latex=%7B%5CDelta+%5Cdashv+%5Cmathrm%7BLim%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;Delta &#92;dashv &#92;mathrm{Lim}}' title='{&#92;Delta &#92;dashv &#92;mathrm{Lim}}' class='latex' />, with limit being right adjoint to the diagonal.</p>
<p>
Dually, of course, colimits turn out to be left adjoints: the whole construction is encapsulated in three symbols, <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathrm%7BColim%7D+%5Cdashv+%5CDelta%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathrm{Colim} &#92;dashv &#92;Delta}' title='{&#92;mathrm{Colim} &#92;dashv &#92;Delta}' class='latex' />.</p>
<p>
<br />  <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=patternsinfp.wordpress.com&#038;blog=15593982&#038;post=158&#038;subd=patternsinfp&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://patternsinfp.wordpress.com/2011/03/31/extreme-solutions/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/39d7e0397cba13d1494d9a48c1f42167?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jeremygibbons</media:title>
		</media:content>

		<media:content url="http://patternsinfp.files.wordpress.com/2011/03/extreme-inf.png" medium="image" />

		<media:content url="http://patternsinfp.files.wordpress.com/2011/03/extreme-limit1.png" medium="image" />

		<media:content url="http://patternsinfp.files.wordpress.com/2011/03/extreme-colimit1.png" medium="image" />

		<media:content url="http://patternsinfp.files.wordpress.com/2011/03/extreme-homomorphism.png" medium="image" />

		<media:content url="http://patternsinfp.files.wordpress.com/2011/03/extreme-chain.png" medium="image" />

		<media:content url="http://patternsinfp.files.wordpress.com/2011/03/extreme-fchain.png" medium="image" />

		<media:content url="http://patternsinfp.files.wordpress.com/2011/03/extreme-initial.png" medium="image" />

		<media:content url="http://patternsinfp.files.wordpress.com/2011/03/extreme-initial-shift.png" medium="image" />

		<media:content url="http://patternsinfp.files.wordpress.com/2011/03/extreme-eval-box.png" medium="image" />

		<media:content url="http://patternsinfp.files.wordpress.com/2011/03/extreme-eval-ladder.png" medium="image" />

		<media:content url="http://patternsinfp.files.wordpress.com/2011/03/extreme-fold.png" medium="image" />

		<media:content url="http://patternsinfp.files.wordpress.com/2011/03/extreme-ffold.png" medium="image" />

		<media:content url="http://patternsinfp.files.wordpress.com/2011/04/extreme-adjn.png" medium="image" />
	</item>
		<item>
		<title>Adjunctions</title>
		<link>http://patternsinfp.wordpress.com/2011/03/28/adjunctions/</link>
		<comments>http://patternsinfp.wordpress.com/2011/03/28/adjunctions/#comments</comments>
		<pubDate>Mon, 28 Mar 2011 10:58:16 +0000</pubDate>
		<dc:creator>jeremygibbons</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://patternsinfp.wordpress.com/?p=135</guid>
		<description><![CDATA[Universal properties are a generalization of the notion of a Galois connection between two orderings. Or perhaps I should say: universal properties arise from adjunctions, and it is adjunctions that are a generalization of Galois connections. Adjunctions capture in an &#8230; <a href="http://patternsinfp.wordpress.com/2011/03/28/adjunctions/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=patternsinfp.wordpress.com&#038;blog=15593982&#038;post=135&#038;subd=patternsinfp&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>
 <a href="http://patternsinfp.wordpress.com/universal-properties-and-galois-connections/">Universal properties</a> are a generalization of the notion of a <em>Galois connection</em> between two orderings. Or perhaps I should say: universal properties arise from <em>adjunctions</em>, and it is adjunctions that are a generalization of Galois connections. Adjunctions capture in an abstract categorical setting the idea of &#8220;optimal solutions to a problem&#8221;; and this idea is itself very general, capturing many of the structures underlying common patterns in programming (not to mention the rest of mathematics). Solutions to equations, products, limits of sequences of approximations, and minimality and maximality are just some of the instances of this powerful abstraction that we will make use of. In the preface to <i>Categories for the Working Mathematician</i>, Mac Lane wrote that &#8220;adjoint functors arise everywhere&#8221;.</p>
<p><h2> Adjoint functors </h2>
<p>
Two functors <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D+%3A+%5Cmathbb%7BD%7D+%5Cleadsto+%5Cmathbb%7BC%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F} : &#92;mathbb{D} &#92;leadsto &#92;mathbb{C}}' title='{&#92;mathsf{F} : &#92;mathbb{D} &#92;leadsto &#92;mathbb{C}}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BG%7D+%3A+%5Cmathbb%7BC%7D+%5Cleadsto+%5Cmathbb%7BD%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{G} : &#92;mathbb{C} &#92;leadsto &#92;mathbb{D}}' title='{&#92;mathsf{G} : &#92;mathbb{C} &#92;leadsto &#92;mathbb{D}}' class='latex' /> form an adjunction, written <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D+%5Cdashv+%5Cmathsf%7BG%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F} &#92;dashv &#92;mathsf{G}}' title='{&#92;mathsf{F} &#92;dashv &#92;mathsf{G}}' class='latex' />, if there is an isomorphism between the sets of arrows <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%28B%29+%5Crightarrow+A%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}(B) &#92;rightarrow A}' title='{&#92;mathsf{F}(B) &#92;rightarrow A}' class='latex' /> in <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BC%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{C}}' title='{&#92;mathbb{C}}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7BB+%5Crightarrow+%5Cmathsf%7BG%7D%28A%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{B &#92;rightarrow &#92;mathsf{G}(A)}' title='{B &#92;rightarrow &#92;mathsf{G}(A)}' class='latex' /> in <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BD%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{D}}' title='{&#92;mathbb{D}}' class='latex' />. We say that <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{F}}' title='{&#92;mathsf{F}}' class='latex' /> is the <em>left adjoint</em> and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BG%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{G}}' title='{&#92;mathsf{G}}' class='latex' /> the <em>right adjoint</em>. The essence of the isomorphism is captured by two natural transformations <img src='http://s0.wp.com/latex.php?latex=%7B%5Ceta+%3A+%5Cmathsf%7BId%7D+%5Cmathbin%7B%5Cstackrel%7B.%7D%7B%5Cto%7D%7D+%5Cmathsf%7BG%7D+%5Cmathbin%7B%5Ccdot%7D+%5Cmathsf%7BF%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;eta : &#92;mathsf{Id} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{G} &#92;mathbin{&#92;cdot} &#92;mathsf{F}}' title='{&#92;eta : &#92;mathsf{Id} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{G} &#92;mathbin{&#92;cdot} &#92;mathsf{F}}' class='latex' /> in <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BD%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{D}}' title='{&#92;mathbb{D}}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cepsilon+%3A+%5Cmathsf%7BF%7D+%5Cmathbin%7B%5Ccdot%7D+%5Cmathsf%7BG%7D+%5Cmathbin%7B%5Cstackrel%7B.%7D%7B%5Cto%7D%7D+%5Cmathsf%7BId%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;epsilon : &#92;mathsf{F} &#92;mathbin{&#92;cdot} &#92;mathsf{G} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{Id}}' title='{&#92;epsilon : &#92;mathsf{F} &#92;mathbin{&#92;cdot} &#92;mathsf{G} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{Id}}' class='latex' /> in <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BC%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{C}}' title='{&#92;mathbb{C}}' class='latex' />, called the <em>unit</em> and <em>counit</em> of the adjunction; <img src='http://s0.wp.com/latex.php?latex=%7B%5Ceta%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;eta}' title='{&#92;eta}' class='latex' /> is the image in <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BD%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{D}}' title='{&#92;mathbb{D}}' class='latex' /> of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bid%7D_%7B%5Cmathsf%7BF%7D%28B%29%7D+%3A+%5Cmathsf%7BF%7D%28B%29+%5Crightarrow+%5Cmathsf%7BF%7D%28B%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{id}_{&#92;mathsf{F}(B)} : &#92;mathsf{F}(B) &#92;rightarrow &#92;mathsf{F}(B)}' title='{&#92;mathit{id}_{&#92;mathsf{F}(B)} : &#92;mathsf{F}(B) &#92;rightarrow &#92;mathsf{F}(B)}' class='latex' /> in <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BC%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{C}}' title='{&#92;mathbb{C}}' class='latex' />, and conversely, <img src='http://s0.wp.com/latex.php?latex=%7B%5Cepsilon%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;epsilon}' title='{&#92;epsilon}' class='latex' /> is the image in <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BC%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{C}}' title='{&#92;mathbb{C}}' class='latex' /> of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bid%7D_%7B%5Cmathsf%7BG%7D%28A%29%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{id}_{&#92;mathsf{G}(A)}}' title='{&#92;mathit{id}_{&#92;mathsf{G}(A)}}' class='latex' /> in <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BD%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{D}}' title='{&#92;mathbb{D}}' class='latex' />. The unit and counit satisfy the laws </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Cepsilon_%7B%5Cmathsf%7BF%7D%28B%29%7D+%5Ccdot+%5Cmathsf%7BF%7D%28%5Ceta_B%29+%26%3D%26+%5Cmathit%7Bid%7D_%7B%5Cmathsf%7BF%7D%28B%29%7D+%5C%5C+%5Cmathsf%7BG%7D%28%5Cepsilon_A%29+%5Ccdot+%5Ceta_%7B%5Cmathsf%7BG%7D%28A%29%7D+%26%3D%26+%5Cmathit%7Bid%7D_%7B%5Cmathsf%7BG%7D%28A%29%7D+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;epsilon_{&#92;mathsf{F}(B)} &#92;cdot &#92;mathsf{F}(&#92;eta_B) &amp;=&amp; &#92;mathit{id}_{&#92;mathsf{F}(B)} &#92;&#92; &#92;mathsf{G}(&#92;epsilon_A) &#92;cdot &#92;eta_{&#92;mathsf{G}(A)} &amp;=&amp; &#92;mathit{id}_{&#92;mathsf{G}(A)} &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;epsilon_{&#92;mathsf{F}(B)} &#92;cdot &#92;mathsf{F}(&#92;eta_B) &amp;=&amp; &#92;mathit{id}_{&#92;mathsf{F}(B)} &#92;&#92; &#92;mathsf{G}(&#92;epsilon_A) &#92;cdot &#92;eta_{&#92;mathsf{G}(A)} &amp;=&amp; &#92;mathit{id}_{&#92;mathsf{G}(A)} &#92;end{array} ' class='latex' />
</p></blockquote>
<p> From them one can construct the witnesses to the isomorphism for arbitrary arrows: for each arrow <img src='http://s0.wp.com/latex.php?latex=%7Bf+%3A+%5Cmathsf%7BF%7D%28B%29+%5Crightarrow+A%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f : &#92;mathsf{F}(B) &#92;rightarrow A}' title='{f : &#92;mathsf{F}(B) &#92;rightarrow A}' class='latex' /> in <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BC%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{C}}' title='{&#92;mathbb{C}}' class='latex' />, there is a unique arrow <img src='http://s0.wp.com/latex.php?latex=%7Bg+%3A+B+%5Crightarrow+%5Cmathsf%7BG%7D%28A%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{g : B &#92;rightarrow &#92;mathsf{G}(A)}' title='{g : B &#92;rightarrow &#92;mathsf{G}(A)}' class='latex' /> in <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BD%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{D}}' title='{&#92;mathbb{D}}' class='latex' /> such that <img src='http://s0.wp.com/latex.php?latex=%7B%5Cepsilon_A+%5Ccdot+%5Cmathsf%7BF%7D%28g%29+%3D+f%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;epsilon_A &#92;cdot &#92;mathsf{F}(g) = f}' title='{&#92;epsilon_A &#92;cdot &#92;mathsf{F}(g) = f}' class='latex' />, given by <img src='http://s0.wp.com/latex.php?latex=%7Bg+%3D+%5Cmathsf%7BG%7D%28f%29+%5Ccdot+%5Ceta_B%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{g = &#92;mathsf{G}(f) &#92;cdot &#92;eta_B}' title='{g = &#92;mathsf{G}(f) &#92;cdot &#92;eta_B}' class='latex' />; and conversely, for each arrow <img src='http://s0.wp.com/latex.php?latex=%7Bg+%3A+B+%5Crightarrow+%5Cmathsf%7BG%7D%28A%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{g : B &#92;rightarrow &#92;mathsf{G}(A)}' title='{g : B &#92;rightarrow &#92;mathsf{G}(A)}' class='latex' /> in <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BD%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{D}}' title='{&#92;mathbb{D}}' class='latex' />, there is a unique arrow <img src='http://s0.wp.com/latex.php?latex=%7Bf+%3A+%5Cmathsf%7BF%7D%28B%29+%5Crightarrow+A%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f : &#92;mathsf{F}(B) &#92;rightarrow A}' title='{f : &#92;mathsf{F}(B) &#92;rightarrow A}' class='latex' /> in <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BC%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{C}}' title='{&#92;mathbb{C}}' class='latex' /> such that <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BG%7D%28f%29+%5Ccdot+%5Ceta_+B+%3D+g%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{G}(f) &#92;cdot &#92;eta_ B = g}' title='{&#92;mathsf{G}(f) &#92;cdot &#92;eta_ B = g}' class='latex' />, given by <img src='http://s0.wp.com/latex.php?latex=%7Bf+%3D+%5Cepsilon_B+%5Ccdot+%5Cmathsf%7BF%7D%28g%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f = &#92;epsilon_B &#92;cdot &#92;mathsf{F}(g)}' title='{f = &#92;epsilon_B &#92;cdot &#92;mathsf{F}(g)}' class='latex' />; and moreover, these two constructions are each other&#8217;s inverses.</p>
<p><h2> Adjunctions from Galois connections </h2>
<p>
A preorder <img src='http://s0.wp.com/latex.php?latex=%7B%28X%2C%7B%5Cle%7D%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(X,{&#92;le})}' title='{(X,{&#92;le})}' class='latex' /> forms a category: the objects of the category are the elements of the set~<img src='http://s0.wp.com/latex.php?latex=%7BX%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{X}' title='{X}' class='latex' />, and between any two elements <img src='http://s0.wp.com/latex.php?latex=%7Bx%2Cy+%5Cin+X%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{x,y &#92;in X}' title='{x,y &#92;in X}' class='latex' />, there is a unique arrow if <img src='http://s0.wp.com/latex.php?latex=%7Bx+%5Cle+y%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{x &#92;le y}' title='{x &#92;le y}' class='latex' />, and no arrow otherwise. That adjunctions are a generalization of Galois connections follows straightforwardly from the fact that there is at most one arrow between any two objects in a preorder category. Then monotonic functions <img src='http://s0.wp.com/latex.php?latex=%7Bf+%3A+%28X%2C%7B%5Cle_X%7D%29+%5Crightarrow+%28Y%2C%7B%5Cle_Y%7D%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f : (X,{&#92;le_X}) &#92;rightarrow (Y,{&#92;le_Y})}' title='{f : (X,{&#92;le_X}) &#92;rightarrow (Y,{&#92;le_Y})}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7Bg+%3A+%28Y%2C%7B%5Cle_Y%7D%29+%5Crightarrow+%28X%2C%7B%5Cle_X%7D%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{g : (Y,{&#92;le_Y}) &#92;rightarrow (X,{&#92;le_X})}' title='{g : (Y,{&#92;le_Y}) &#92;rightarrow (X,{&#92;le_X})}' class='latex' /> between preorders <img src='http://s0.wp.com/latex.php?latex=%7B%28X%2C%7B%5Cle_X%7D%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(X,{&#92;le_X})}' title='{(X,{&#92;le_X})}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B%28Y%2C%7B%5Cle_Y%7D%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(Y,{&#92;le_Y})}' title='{(Y,{&#92;le_Y})}' class='latex' /> form a Galois connection precisely if the sets of arrows <img src='http://s0.wp.com/latex.php?latex=%7Bf%28y%29+%5Crightarrow+x%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f(y) &#92;rightarrow x}' title='{f(y) &#92;rightarrow x}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7By+%5Crightarrow+g%28x%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{y &#92;rightarrow g(x)}' title='{y &#92;rightarrow g(x)}' class='latex' /> are isomorphic&mdash;that is, if both <img src='http://s0.wp.com/latex.php?latex=%7Bf%28y%29+%5Cle_X+x%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f(y) &#92;le_X x}' title='{f(y) &#92;le_X x}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7By+%5Cle_Y+g%28x%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{y &#92;le_Y g(x)}' title='{y &#92;le_Y g(x)}' class='latex' /> hold, or neither do, or in other words, </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++f%28y%29+%5Cle_X+x+%5CLeftrightarrow+y+%5Cle_Y+g%28x%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  f(y) &#92;le_X x &#92;Leftrightarrow y &#92;le_Y g(x) ' title='&#92;displaystyle  f(y) &#92;le_X x &#92;Leftrightarrow y &#92;le_Y g(x) ' class='latex' />
</p></blockquote>
<p><h2> Adjoints of the diagonal functor </h2>
<p>
A very useful example of adjunctions arises in the definition of products&mdash;in the category <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{S}&#92;mathrm{et}}' title='{&#92;mathbb{S}&#92;mathrm{et}}' class='latex' /> of sets and total functions, for given types <img src='http://s0.wp.com/latex.php?latex=%7BA%2CB%2CC%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{A,B,C}' title='{A,B,C}' class='latex' />, there is an isomorphism between the set of pair-generating functions, of type <img src='http://s0.wp.com/latex.php?latex=%7BA+%5Crightarrow+B+%5Ctimes+C%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{A &#92;rightarrow B &#92;times C}' title='{A &#92;rightarrow B &#92;times C}' class='latex' />, and their two projections, pairs of functions of types <img src='http://s0.wp.com/latex.php?latex=%7BA+%5Crightarrow+B%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{A &#92;rightarrow B}' title='{A &#92;rightarrow B}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7BA+%5Crightarrow+C%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{A &#92;rightarrow C}' title='{A &#92;rightarrow C}' class='latex' />. (Indeed, given functions <img src='http://s0.wp.com/latex.php?latex=%7Bf%3AA+%5Crightarrow+B%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f:A &#92;rightarrow B}' title='{f:A &#92;rightarrow B}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7Bg%3AA+%5Crightarrow+C%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{g:A &#92;rightarrow C}' title='{g:A &#92;rightarrow C}' class='latex' />, one can construct the pair-generating function <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfork%7D%28f%2Cg%29+%3A+A+%5Crightarrow+B+%5Ctimes+C%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{fork}(f,g) : A &#92;rightarrow B &#92;times C}' title='{&#92;mathit{fork}(f,g) : A &#92;rightarrow B &#92;times C}' class='latex' />; and conversely, given a pair-generating function <img src='http://s0.wp.com/latex.php?latex=%7Bh+%3A+A+%5Crightarrow+B+%5Ctimes+C%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h : A &#92;rightarrow B &#92;times C}' title='{h : A &#92;rightarrow B &#92;times C}' class='latex' />, one can construct its two projections <img src='http://s0.wp.com/latex.php?latex=%7Bfst+%5Ccdot+h+%3A+A+%5Crightarrow+B%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{fst &#92;cdot h : A &#92;rightarrow B}' title='{fst &#92;cdot h : A &#92;rightarrow B}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7Bsnd+%5Ccdot+h+%3A+A+%5Crightarrow+C%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{snd &#92;cdot h : A &#92;rightarrow C}' title='{snd &#92;cdot h : A &#92;rightarrow C}' class='latex' />; and moreover, these two constructions are inverses.) </p>
<p>
The &#8220;isomorphism between sets of arrows&#8221; can be elegantly expressed as an adjunction; since it concerns pairs of arrows, one side of the adjunction involves the product category <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D+%5Ctimes+%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{S}&#92;mathrm{et} &#92;times &#92;mathbb{S}&#92;mathrm{et}}' title='{&#92;mathbb{S}&#92;mathrm{et} &#92;times &#92;mathbb{S}&#92;mathrm{et}}' class='latex' />. The right adjoint is the product functor <img src='http://s0.wp.com/latex.php?latex=%7B%28%5Ctimes%29+%3A+%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D+%5Ctimes+%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D+%5Cleadsto+%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(&#92;times) : &#92;mathbb{S}&#92;mathrm{et} &#92;times &#92;mathbb{S}&#92;mathrm{et} &#92;leadsto &#92;mathbb{S}&#92;mathrm{et}}' title='{(&#92;times) : &#92;mathbb{S}&#92;mathrm{et} &#92;times &#92;mathbb{S}&#92;mathrm{et} &#92;leadsto &#92;mathbb{S}&#92;mathrm{et}}' class='latex' />, mapping an object in <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D+%5Ctimes+%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{S}&#92;mathrm{et} &#92;times &#92;mathbb{S}&#92;mathrm{et}}' title='{&#92;mathbb{S}&#92;mathrm{et} &#92;times &#92;mathbb{S}&#92;mathrm{et}}' class='latex' />&mdash;that is, a pair of sets&mdash;to their cartesian product as an object in <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{S}&#92;mathrm{et}}' title='{&#92;mathbb{S}&#92;mathrm{et}}' class='latex' />, and an arrow in <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D+%5Ctimes+%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{S}&#92;mathrm{et} &#92;times &#92;mathbb{S}&#92;mathrm{et}}' title='{&#92;mathbb{S}&#92;mathrm{et} &#92;times &#92;mathbb{S}&#92;mathrm{et}}' class='latex' />&mdash;that is, a parallel pair of functions&mdash;to a function in <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{S}&#92;mathrm{et}}' title='{&#92;mathbb{S}&#92;mathrm{et}}' class='latex' /> acting pointwise on pairs. In the other direction, the left adjoint is the <em>diagonal</em> functor <img src='http://s0.wp.com/latex.php?latex=%7B%5Ctriangle+%3A+%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D+%5Cleadsto+%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D+%5Ctimes+%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;triangle : &#92;mathbb{S}&#92;mathrm{et} &#92;leadsto &#92;mathbb{S}&#92;mathrm{et} &#92;times &#92;mathbb{S}&#92;mathrm{et}}' title='{&#92;triangle : &#92;mathbb{S}&#92;mathrm{et} &#92;leadsto &#92;mathbb{S}&#92;mathrm{et} &#92;times &#92;mathbb{S}&#92;mathrm{et}}' class='latex' />, mapping an object <img src='http://s0.wp.com/latex.php?latex=%7BA%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{A}' title='{A}' class='latex' /> in <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{S}&#92;mathrm{et}}' title='{&#92;mathbb{S}&#92;mathrm{et}}' class='latex' /> to the object <img src='http://s0.wp.com/latex.php?latex=%7B%28A%2CA%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(A,A)}' title='{(A,A)}' class='latex' /> in <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D+%5Ctimes+%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{S}&#92;mathrm{et} &#92;times &#92;mathbb{S}&#92;mathrm{et}}' title='{&#92;mathbb{S}&#92;mathrm{et} &#92;times &#92;mathbb{S}&#92;mathrm{et}}' class='latex' />, and a function <img src='http://s0.wp.com/latex.php?latex=%7Bf%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f}' title='{f}' class='latex' /> to the pair of functions <img src='http://s0.wp.com/latex.php?latex=%7B%28f%2Cf%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(f,f)}' title='{(f,f)}' class='latex' /> as an arrow in <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D+%5Ctimes+%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{S}&#92;mathrm{et} &#92;times &#92;mathbb{S}&#92;mathrm{et}}' title='{&#92;mathbb{S}&#92;mathrm{et} &#92;times &#92;mathbb{S}&#92;mathrm{et}}' class='latex' />. The adjunction <img src='http://s0.wp.com/latex.php?latex=%7B%7B%5Ctriangle%7D+%5Cdashv+%28%5Ctimes%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{{&#92;triangle} &#92;dashv (&#92;times)}' title='{{&#92;triangle} &#92;dashv (&#92;times)}' class='latex' /> amounts to the isomorphism </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Ctriangle+A+%5Crightarrow+%28B%2CC%29+%5Capprox+A+%5Crightarrow+%7B%5Ctimes%7D+%28B%2CC%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;triangle A &#92;rightarrow (B,C) &#92;approx A &#92;rightarrow {&#92;times} (B,C) ' title='&#92;displaystyle  &#92;triangle A &#92;rightarrow (B,C) &#92;approx A &#92;rightarrow {&#92;times} (B,C) ' class='latex' />
</p></blockquote>
<p> or equivalently, </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%28A+%5Crightarrow+B%29%5Ctimes%28A+%5Crightarrow+C%29+%5Capprox+A+%5Crightarrow+%28B%5Ctimes+C%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  (A &#92;rightarrow B)&#92;times(A &#92;rightarrow C) &#92;approx A &#92;rightarrow (B&#92;times C) ' title='&#92;displaystyle  (A &#92;rightarrow B)&#92;times(A &#92;rightarrow C) &#92;approx A &#92;rightarrow (B&#92;times C) ' class='latex' />
</p></blockquote>
<p>
The unit and counit of the adjunction are <img src='http://s0.wp.com/latex.php?latex=%7B%5Ceta+%3A+%5Cmathsf%7BId%7D+%5Cmathbin%7B%5Cstackrel%7B.%7D%7B%5Cto%7D%7D+%28%5Ctimes%29+%5Cmathbin%7B%5Ccdot%7D+%5Ctriangle%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;eta : &#92;mathsf{Id} &#92;mathbin{&#92;stackrel{.}{&#92;to}} (&#92;times) &#92;mathbin{&#92;cdot} &#92;triangle}' title='{&#92;eta : &#92;mathsf{Id} &#92;mathbin{&#92;stackrel{.}{&#92;to}} (&#92;times) &#92;mathbin{&#92;cdot} &#92;triangle}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cepsilon+%3A+%5Ctriangle+%5Cmathbin%7B%5Ccdot%7D+%28%5Ctimes%29+%5Cmathbin%7B%5Cstackrel%7B.%7D%7B%5Cto%7D%7D+%5Cmathsf%7BId%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;epsilon : &#92;triangle &#92;mathbin{&#92;cdot} (&#92;times) &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{Id}}' title='{&#92;epsilon : &#92;triangle &#92;mathbin{&#92;cdot} (&#92;times) &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{Id}}' class='latex' />. In more familiar terms, the unit is a natural transformation in <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{S}&#92;mathrm{et}}' title='{&#92;mathbb{S}&#92;mathrm{et}}' class='latex' />, so a polymorphic function; in fact, it&#8217;s the function of type <img src='http://s0.wp.com/latex.php?latex=%7BA+%5Crightarrow+A+%5Ctimes+A%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{A &#92;rightarrow A &#92;times A}' title='{A &#92;rightarrow A &#92;times A}' class='latex' /> that we might call <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bdouble%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{double}}' title='{&#92;mathit{double}}' class='latex' />. However, the counit is a natural transformation <img src='http://s0.wp.com/latex.php?latex=%7B%28A+%5Ctimes+B%2CA+%5Ctimes+B%29+%5Crightarrow+%28A%2CB%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(A &#92;times B,A &#92;times B) &#92;rightarrow (A,B)}' title='{(A &#92;times B,A &#92;times B) &#92;rightarrow (A,B)}' class='latex' /> in <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D+%5Ctimes+%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{S}&#92;mathrm{et} &#92;times &#92;mathbb{S}&#92;mathrm{et}}' title='{&#92;mathbb{S}&#92;mathrm{et} &#92;times &#92;mathbb{S}&#92;mathrm{et}}' class='latex' />, so not simply a (polymorphic) function; but arrows in <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D+%5Ctimes+%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{S}&#92;mathrm{et} &#92;times &#92;mathbb{S}&#92;mathrm{et}}' title='{&#92;mathbb{S}&#92;mathrm{et} &#92;times &#92;mathbb{S}&#92;mathrm{et}}' class='latex' /> are pairs of functions, so we might write this <img src='http://s0.wp.com/latex.php?latex=%7B%28%5Cmathit%7Bfst%7D%2C%5Cmathit%7Bsnd%7D%29+%3A%3A+%28A+%5Ctimes+B+%5Crightarrow+A%2C+A+%5Ctimes+B+%5Crightarrow+B%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(&#92;mathit{fst},&#92;mathit{snd}) :: (A &#92;times B &#92;rightarrow A, A &#92;times B &#92;rightarrow B)}' title='{(&#92;mathit{fst},&#92;mathit{snd}) :: (A &#92;times B &#92;rightarrow A, A &#92;times B &#92;rightarrow B)}' class='latex' />. </p>
<p>
Then the &#8220;fork&#8221; operation is in fact one of the two witnesses to the isomorphism between the sets of arrows: given an arrow <img src='http://s0.wp.com/latex.php?latex=%7B%5Ctriangle+A+%5Crightarrow+%28B%2CC%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;triangle A &#92;rightarrow (B,C)}' title='{&#92;triangle A &#92;rightarrow (B,C)}' class='latex' /> in <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D+%5Ctimes+%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{S}&#92;mathrm{et} &#92;times &#92;mathbb{S}&#92;mathrm{et}}' title='{&#92;mathbb{S}&#92;mathrm{et} &#92;times &#92;mathbb{S}&#92;mathrm{et}}' class='latex' />, that is, a pair <img src='http://s0.wp.com/latex.php?latex=%7B%28f%2Cg%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(f,g)}' title='{(f,g)}' class='latex' /> of functions of types <img src='http://s0.wp.com/latex.php?latex=%7B%28A+%5Crightarrow+B%2CA+%5Crightarrow+C%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(A &#92;rightarrow B,A &#92;rightarrow C)}' title='{(A &#92;rightarrow B,A &#92;rightarrow C)}' class='latex' />, then <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfork%7D%28f%2Cg%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{fork}(f,g)}' title='{&#92;mathit{fork}(f,g)}' class='latex' /> is an arrow <img src='http://s0.wp.com/latex.php?latex=%7BA+%5Crightarrow+%7B%5Ctimes%7D+%28B%2CC%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{A &#92;rightarrow {&#92;times} (B,C)}' title='{A &#92;rightarrow {&#92;times} (B,C)}' class='latex' /> in <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{S}&#92;mathrm{et}}' title='{&#92;mathbb{S}&#92;mathrm{et}}' class='latex' />, that is, a function of type <img src='http://s0.wp.com/latex.php?latex=%7BA+%5Crightarrow+B+%5Ctimes+C%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{A &#92;rightarrow B &#92;times C}' title='{A &#92;rightarrow B &#92;times C}' class='latex' />, given by the construction above: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathit%7Bfork%7D%28f%2Cg%29+%3D+%28%5Ctimes%29+%28f%2Cg%29+%5Ccdot+%5Cmathit%7Bdouble%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathit{fork}(f,g) = (&#92;times) (f,g) &#92;cdot &#92;mathit{double} ' title='&#92;displaystyle  &#92;mathit{fork}(f,g) = (&#92;times) (f,g) &#92;cdot &#92;mathit{double} ' class='latex' />
</p></blockquote>
<p> or, with more points, </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathit%7Bfork%7D+%28f%2Cg%29%5C%2Ca+%3D+%28f%5C%2Ca%2C+g%5C%2Ca%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathit{fork} (f,g)&#92;,a = (f&#92;,a, g&#92;,a) ' title='&#92;displaystyle  &#92;mathit{fork} (f,g)&#92;,a = (f&#92;,a, g&#92;,a) ' class='latex' />
</p></blockquote>
<p>
The laws that the unit and counit satisfy are </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%28%5Cmathit%7Bfst%7D%2C%5Cmathit%7Bsnd%7D%29+%5Ccdot+%5Ctriangle+%5Cmathit%7Bdouble%7D+%26%3D%26+%5Cmathit%7Bid%7D+%5C%5C+%28%5Ctimes%29+%28%5Cmathit%7Bfst%7D%2C%5Cmathit%7Bsnd%7D%29+%5Ccdot+%5Cmathit%7Bdouble%7D+%26%3D%26+%5Cmathit%7Bid%7D+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} (&#92;mathit{fst},&#92;mathit{snd}) &#92;cdot &#92;triangle &#92;mathit{double} &amp;=&amp; &#92;mathit{id} &#92;&#92; (&#92;times) (&#92;mathit{fst},&#92;mathit{snd}) &#92;cdot &#92;mathit{double} &amp;=&amp; &#92;mathit{id} &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} (&#92;mathit{fst},&#92;mathit{snd}) &#92;cdot &#92;triangle &#92;mathit{double} &amp;=&amp; &#92;mathit{id} &#92;&#92; (&#92;times) (&#92;mathit{fst},&#92;mathit{snd}) &#92;cdot &#92;mathit{double} &amp;=&amp; &#92;mathit{id} &#92;end{array} ' class='latex' />
</p></blockquote>
<p> or, in more familiar terms, </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Cmathit%7Bfst%7D+%5Ccdot+%5Cmathit%7Bdouble%7D+%26%3D%26+%5Cmathit%7Bid%7D+%5C%5C+%5Cmathit%7Bsnd%7D+%5Ccdot+%5Cmathit%7Bdouble%7D+%26%3D%26+%5Cmathit%7Bid%7D+%5C%5C+%5Cmathit%7Bfork%7D+%28%5Cmathit%7Bfst%7D%2C%5Cmathit%7Bsnd%7D%29+%26%3D%26+%5Cmathit%7Bid%7D+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{fst} &#92;cdot &#92;mathit{double} &amp;=&amp; &#92;mathit{id} &#92;&#92; &#92;mathit{snd} &#92;cdot &#92;mathit{double} &amp;=&amp; &#92;mathit{id} &#92;&#92; &#92;mathit{fork} (&#92;mathit{fst},&#92;mathit{snd}) &amp;=&amp; &#92;mathit{id} &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{fst} &#92;cdot &#92;mathit{double} &amp;=&amp; &#92;mathit{id} &#92;&#92; &#92;mathit{snd} &#92;cdot &#92;mathit{double} &amp;=&amp; &#92;mathit{id} &#92;&#92; &#92;mathit{fork} (&#92;mathit{fst},&#92;mathit{snd}) &amp;=&amp; &#92;mathit{id} &#92;end{array} ' class='latex' />
</p></blockquote>
<p>
The universal property of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfork%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{fork}}' title='{&#92;mathit{fork}}' class='latex' /> follows from the isomorphism between sets of arrows: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Bll%7D+%26+h+%3D+%5Cmathit%7Bfork%7D%28f%2Cg%29+%5C%5C+%5CLeftrightarrow+%26+%5Cqquad+%5C%7B+%5Cmbox%7B%5C%28%5Cmathit%7Bfork%7D%5C%29%7D+%5C%7D+%5C%5C+%26+h+%3D+%28%5Ctimes%29+%28f%2Cg%29+%5Ccdot+%5Cmathit%7Bdouble%7D+%5C%5C+%5CLeftrightarrow+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bisomorphism+between+arrow+sets%7D+%5C%7D+%5C%5C+%26+%28%5Cmathit%7Bfst%7D%2C%5Cmathit%7Bsnd%7D%29+%5Ccdot+%5Ctriangle+h+%3D+%28f%2Cg%29+%5C%5C+%5CLeftrightarrow+%26+%5Cqquad+%5C%7B+%5Cmbox%7B%5C%28%5Ctriangle%5C%29%7D+%5C%7D+%5C%5C+%26+%28%5Cmathit%7Bfst%7D%2C%5Cmathit%7Bsnd%7D%29+%5Ccdot+%28h%2Ch%29+%3D+%28f%2Cg%29+%5C%5C+%5CLeftrightarrow+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bcomposition+in+%5C%28%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D+%5Ctimes+%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D%5C%29+is+pointwise%7D+%5C%7D+%5C%5C+%26+%28%5Cmathit%7Bfst%7D+%5Ccdot+h%2C%5Cmathit%7Bsnd%7D+%5Ccdot+h%29+%3D+%28f%2Cg%29+%5C%5C+%5CLeftrightarrow+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bequality+of+pairs+is+pointwise%7D+%5C%7D+%5C%5C+%26+%5Cmathit%7Bfst%7D+%5Ccdot+h%3Df+%5Cland+%5Cmathit%7Bsnd%7D+%5Ccdot+h%3Dg+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{ll} &amp; h = &#92;mathit{fork}(f,g) &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{&#92;(&#92;mathit{fork}&#92;)} &#92;} &#92;&#92; &amp; h = (&#92;times) (f,g) &#92;cdot &#92;mathit{double} &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{isomorphism between arrow sets} &#92;} &#92;&#92; &amp; (&#92;mathit{fst},&#92;mathit{snd}) &#92;cdot &#92;triangle h = (f,g) &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{&#92;(&#92;triangle&#92;)} &#92;} &#92;&#92; &amp; (&#92;mathit{fst},&#92;mathit{snd}) &#92;cdot (h,h) = (f,g) &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{composition in &#92;(&#92;mathbb{S}&#92;mathrm{et} &#92;times &#92;mathbb{S}&#92;mathrm{et}&#92;) is pointwise} &#92;} &#92;&#92; &amp; (&#92;mathit{fst} &#92;cdot h,&#92;mathit{snd} &#92;cdot h) = (f,g) &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{equality of pairs is pointwise} &#92;} &#92;&#92; &amp; &#92;mathit{fst} &#92;cdot h=f &#92;land &#92;mathit{snd} &#92;cdot h=g &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{ll} &amp; h = &#92;mathit{fork}(f,g) &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{&#92;(&#92;mathit{fork}&#92;)} &#92;} &#92;&#92; &amp; h = (&#92;times) (f,g) &#92;cdot &#92;mathit{double} &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{isomorphism between arrow sets} &#92;} &#92;&#92; &amp; (&#92;mathit{fst},&#92;mathit{snd}) &#92;cdot &#92;triangle h = (f,g) &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{&#92;(&#92;triangle&#92;)} &#92;} &#92;&#92; &amp; (&#92;mathit{fst},&#92;mathit{snd}) &#92;cdot (h,h) = (f,g) &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{composition in &#92;(&#92;mathbb{S}&#92;mathrm{et} &#92;times &#92;mathbb{S}&#92;mathrm{et}&#92;) is pointwise} &#92;} &#92;&#92; &amp; (&#92;mathit{fst} &#92;cdot h,&#92;mathit{snd} &#92;cdot h) = (f,g) &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{equality of pairs is pointwise} &#92;} &#92;&#92; &amp; &#92;mathit{fst} &#92;cdot h=f &#92;land &#92;mathit{snd} &#92;cdot h=g &#92;end{array} ' class='latex' />
</p></blockquote>
<p> The universal property of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfork%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{fork}}' title='{&#92;mathit{fork}}' class='latex' /> underlies all the useful laws of that operator.</p>
<p>
Of course, the situation nicely dualizes too. Coproducts in <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{S}&#92;mathrm{et}}' title='{&#92;mathbb{S}&#92;mathrm{et}}' class='latex' /> arise from the isomorphism between the set of arrows <img src='http://s0.wp.com/latex.php?latex=%7BA%2BB+%5Crightarrow+C%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{A+B &#92;rightarrow C}' title='{A+B &#92;rightarrow C}' class='latex' /> and the pairs of arrows in <img src='http://s0.wp.com/latex.php?latex=%7BA+%5Crightarrow+C%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{A &#92;rightarrow C}' title='{A &#92;rightarrow C}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7BB+%5Crightarrow+C%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{B &#92;rightarrow C}' title='{B &#92;rightarrow C}' class='latex' />. Again, &#8220;pairs of arrows&#8221; suggest the product category; but this time, the diagonal functor is the right adjoint, with the coproduct functor <img src='http://s0.wp.com/latex.php?latex=%7B%28%2B%29+%3A+%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D+%5Ctimes+%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D+%5Crightarrow+%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(+) : &#92;mathbb{S}&#92;mathrm{et} &#92;times &#92;mathbb{S}&#92;mathrm{et} &#92;rightarrow &#92;mathbb{S}&#92;mathrm{et}}' title='{(+) : &#92;mathbb{S}&#92;mathrm{et} &#92;times &#92;mathbb{S}&#92;mathrm{et} &#92;rightarrow &#92;mathbb{S}&#92;mathrm{et}}' class='latex' /> (which takes a pair of sets <img src='http://s0.wp.com/latex.php?latex=%7B%28A%2CB%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(A,B)}' title='{(A,B)}' class='latex' /> to their disjoint union) as the left adjoint. That is, the adjunction is <img src='http://s0.wp.com/latex.php?latex=%7B%28%2B%29+%5Cdashv+%5Ctriangle%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(+) &#92;dashv &#92;triangle}' title='{(+) &#92;dashv &#92;triangle}' class='latex' />, and the isomorphism is </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%28%2B%29+%28A%2CB%29+%5Crightarrow+C+%5Capprox+%28A%2CB%29+%5Crightarrow+%5Ctriangle+C+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  (+) (A,B) &#92;rightarrow C &#92;approx (A,B) &#92;rightarrow &#92;triangle C ' title='&#92;displaystyle  (+) (A,B) &#92;rightarrow C &#92;approx (A,B) &#92;rightarrow &#92;triangle C ' class='latex' />
</p></blockquote>
<p> The unit <img src='http://s0.wp.com/latex.php?latex=%7B%5Ceta+%3A+%5Cmathsf%7BId%7D+%5Cmathbin%7B%5Cstackrel%7B.%7D%7B%5Cto%7D%7D+%5Ctriangle+%5Cmathbin%7B%5Ccdot%7D+%28%2B%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;eta : &#92;mathsf{Id} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;triangle &#92;mathbin{&#92;cdot} (+)}' title='{&#92;eta : &#92;mathsf{Id} &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;triangle &#92;mathbin{&#92;cdot} (+)}' class='latex' /> is a natural transformation <img src='http://s0.wp.com/latex.php?latex=%7B%28A%2CB%29+%5Crightarrow+%28A%2BB%2CA%2BB%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(A,B) &#92;rightarrow (A+B,A+B)}' title='{(A,B) &#92;rightarrow (A+B,A+B)}' class='latex' /> in <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D+%5Ctimes+%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{S}&#92;mathrm{et} &#92;times &#92;mathbb{S}&#92;mathrm{et}}' title='{&#92;mathbb{S}&#92;mathrm{et} &#92;times &#92;mathbb{S}&#92;mathrm{et}}' class='latex' />, that is, a pair of functions <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Binl%7D+%3A+A+%5Crightarrow+A%2BB%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{inl} : A &#92;rightarrow A+B}' title='{&#92;mathit{inl} : A &#92;rightarrow A+B}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Binr%7D+%3A+B+%5Crightarrow+A%2BB%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{inr} : B &#92;rightarrow A+B}' title='{&#92;mathit{inr} : B &#92;rightarrow A+B}' class='latex' />. The counit <img src='http://s0.wp.com/latex.php?latex=%7B%5Cepsilon+%3A+%28%2B%29+%5Cmathbin%7B%5Ccdot%7D+%5Ctriangle+%5Cmathbin%7B%5Cstackrel%7B.%7D%7B%5Cto%7D%7D+%5Cmathsf%7BId%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;epsilon : (+) &#92;mathbin{&#92;cdot} &#92;triangle &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{Id}}' title='{&#92;epsilon : (+) &#92;mathbin{&#92;cdot} &#92;triangle &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{Id}}' class='latex' /> is a natural transformation <img src='http://s0.wp.com/latex.php?latex=%7BA%2BA+%5Crightarrow+A%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{A+A &#92;rightarrow A}' title='{A+A &#92;rightarrow A}' class='latex' /> in <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{S}&#92;mathrm{et}}' title='{&#92;mathbb{S}&#92;mathrm{et}}' class='latex' />, which we might call <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bmerge%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{merge}}' title='{&#92;mathit{merge}}' class='latex' />. The &#8220;join&#8221; of two functions with a common range is a witness to one half of the isomorphism&mdash;given an arrow <img src='http://s0.wp.com/latex.php?latex=%7B%28f%2Cg%29+%3A+%28A%2CB%29+%5Crightarrow+%5Ctriangle+C%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(f,g) : (A,B) &#92;rightarrow &#92;triangle C}' title='{(f,g) : (A,B) &#92;rightarrow &#92;triangle C}' class='latex' /> in <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D+%5Ctimes+%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{S}&#92;mathrm{et} &#92;times &#92;mathbb{S}&#92;mathrm{et}}' title='{&#92;mathbb{S}&#92;mathrm{et} &#92;times &#92;mathbb{S}&#92;mathrm{et}}' class='latex' />, then <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bjoin%7D+%28f%2Cg%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{join} (f,g)}' title='{&#92;mathit{join} (f,g)}' class='latex' /> is an arrow <img src='http://s0.wp.com/latex.php?latex=%7B%28%2B%29+%28A%2CB%29+%5Crightarrow+C%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(+) (A,B) &#92;rightarrow C}' title='{(+) (A,B) &#92;rightarrow C}' class='latex' /> in <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{S}&#92;mathrm{et}}' title='{&#92;mathbb{S}&#92;mathrm{et}}' class='latex' />, defined by </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathit%7Bjoin%7D+%28f%2Cg%29+%3D+%5Cmathit%7Bmerge%7D+%5Ccdot+%28%2B%29+%28f%2Cg%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathit{join} (f,g) = &#92;mathit{merge} &#92;cdot (+) (f,g) ' title='&#92;displaystyle  &#92;mathit{join} (f,g) = &#92;mathit{merge} &#92;cdot (+) (f,g) ' class='latex' />
</p></blockquote>
<p> The two laws that the unit and counit satisfy are: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Cmathit%7Bmerge%7D+%5Ccdot+%28%2B%29+%28%5Cmathit%7Binl%7D%2C%5Cmathit%7Binr%7D%29+%26%3D%26+%5Cmathit%7Bid%7D+%5C%5C+%5Ctriangle+%5Cmathit%7Bmerge%7D+%5Ccdot+%28%5Cmathit%7Binl%7D%2C%5Cmathit%7Binr%7D%29+%26%3D%26+%5Cmathit%7Bid%7D+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{merge} &#92;cdot (+) (&#92;mathit{inl},&#92;mathit{inr}) &amp;=&amp; &#92;mathit{id} &#92;&#92; &#92;triangle &#92;mathit{merge} &#92;cdot (&#92;mathit{inl},&#92;mathit{inr}) &amp;=&amp; &#92;mathit{id} &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{merge} &#92;cdot (+) (&#92;mathit{inl},&#92;mathit{inr}) &amp;=&amp; &#92;mathit{id} &#92;&#92; &#92;triangle &#92;mathit{merge} &#92;cdot (&#92;mathit{inl},&#92;mathit{inr}) &amp;=&amp; &#92;mathit{id} &#92;end{array} ' class='latex' />
</p></blockquote>
<p> or, perhaps more perspicuously, </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Cmathit%7Bjoin%7D+%28%5Cmathit%7Binl%7D%2C%5Cmathit%7Binr%7D%29+%26%3D%26+%5Cmathit%7Bid%7D+%5C%5C+%5Cmathit%7Bmerge%7D+%5Ccdot+%5Cmathit%7Binl%7D+%26%3D%26+%5Cmathit%7Bid%7D+%5C%5C+%5Cmathit%7Bmerge%7D+%5Ccdot+%5Cmathit%7Binr%7D+%26%3D%26+%5Cmathit%7Bid%7D+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{join} (&#92;mathit{inl},&#92;mathit{inr}) &amp;=&amp; &#92;mathit{id} &#92;&#92; &#92;mathit{merge} &#92;cdot &#92;mathit{inl} &amp;=&amp; &#92;mathit{id} &#92;&#92; &#92;mathit{merge} &#92;cdot &#92;mathit{inr} &amp;=&amp; &#92;mathit{id} &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{join} (&#92;mathit{inl},&#92;mathit{inr}) &amp;=&amp; &#92;mathit{id} &#92;&#92; &#92;mathit{merge} &#92;cdot &#92;mathit{inl} &amp;=&amp; &#92;mathit{id} &#92;&#92; &#92;mathit{merge} &#92;cdot &#92;mathit{inr} &amp;=&amp; &#92;mathit{id} &#92;end{array} ' class='latex' />
</p></blockquote>
<p>
Another familiar example from functional programming is the notion of <em>currying</em>, which arises when one can construct the <em>function space</em> <img src='http://s0.wp.com/latex.php?latex=%7BA+%5CRightarrow+B%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{A &#92;Rightarrow B}' title='{A &#92;Rightarrow B}' class='latex' /> (the type of functions from <img src='http://s0.wp.com/latex.php?latex=%7BA%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{A}' title='{A}' class='latex' /> to <img src='http://s0.wp.com/latex.php?latex=%7BB%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{B}' title='{B}' class='latex' />, for each type <img src='http://s0.wp.com/latex.php?latex=%7BA%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{A}' title='{A}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7BB%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{B}' title='{B}' class='latex' />), such that there is an isomorphism between the sets of arrows <img src='http://s0.wp.com/latex.php?latex=%7BA+%5Crightarrow+%28B+%5CRightarrow+C%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{A &#92;rightarrow (B &#92;Rightarrow C)}' title='{A &#92;rightarrow (B &#92;Rightarrow C)}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7BA+%5Ctimes+B+%5Crightarrow+C%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{A &#92;times B &#92;rightarrow C}' title='{A &#92;times B &#92;rightarrow C}' class='latex' />. Here, the adjunction is <img src='http://s0.wp.com/latex.php?latex=%7B%28+%5Ctimes+B%29+%5Cdashv+%28B+%5CRightarrow+%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{( &#92;times B) &#92;dashv (B &#92;Rightarrow )}' title='{( &#92;times B) &#92;dashv (B &#92;Rightarrow )}' class='latex' />&mdash;in this case, both functors are endofunctors on <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathbb%7BS%7D%5Cmathrm%7Bet%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathbb{S}&#92;mathrm{et}}' title='{&#92;mathbb{S}&#92;mathrm{et}}' class='latex' />. The unit and counit are natural transformations <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathsf%7BId%7D+%5Cmathbin%7B%5Cstackrel%7B.%7D%7B%5Cto%7D%7D+%28B+%5CRightarrow+%29%5Cmathbin%7B%5Ccdot%7D%28+%5Ctimes+B%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathsf{Id} &#92;mathbin{&#92;stackrel{.}{&#92;to}} (B &#92;Rightarrow )&#92;mathbin{&#92;cdot}( &#92;times B)}' title='{&#92;mathsf{Id} &#92;mathbin{&#92;stackrel{.}{&#92;to}} (B &#92;Rightarrow )&#92;mathbin{&#92;cdot}( &#92;times B)}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B%28+%5Ctimes+B%29%5Cmathbin%7B%5Ccdot%7D%28B+%5CRightarrow+%29+%5Cmathbin%7B%5Cstackrel%7B.%7D%7B%5Cto%7D%7D+%5Cmathsf%7BId%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{( &#92;times B)&#92;mathbin{&#92;cdot}(B &#92;Rightarrow ) &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{Id}}' title='{( &#92;times B)&#92;mathbin{&#92;cdot}(B &#92;Rightarrow ) &#92;mathbin{&#92;stackrel{.}{&#92;to}} &#92;mathsf{Id}}' class='latex' />. We might call these <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bpair%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{pair}}' title='{&#92;mathit{pair}}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bapply%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{apply}}' title='{&#92;mathit{apply}}' class='latex' />, since the first is a curried pair-forming operator, and the second applies a function to an argument: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Cmathit%7Bpair%7D+%26%3A%26+A+%5Crightarrow+%28B+%5CRightarrow+%28A+%5Ctimes+B%29%29+%5C%5C+%5Cmathit%7Bapply%7D+%26%3A%26+%28B+%5CRightarrow+A%29+%5Ctimes+B+%5Crightarrow+A+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{pair} &amp;:&amp; A &#92;rightarrow (B &#92;Rightarrow (A &#92;times B)) &#92;&#92; &#92;mathit{apply} &amp;:&amp; (B &#92;Rightarrow A) &#92;times B &#92;rightarrow A &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{pair} &amp;:&amp; A &#92;rightarrow (B &#92;Rightarrow (A &#92;times B)) &#92;&#92; &#92;mathit{apply} &amp;:&amp; (B &#92;Rightarrow A) &#92;times B &#92;rightarrow A &#92;end{array} ' class='latex' />
</p></blockquote>
<p> The laws they satisfy are as follows: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Cmathit%7Bapply%7D+%5Ccdot+%28+%5Ctimes+B%29+%5Cmathit%7Bpair%7D+%26%3D%26+%5Cmathit%7Bid%7D+%5C%5C+%28B+%5CRightarrow+%29%5Cmathit%7Bapply%7D+%5Ccdot+%5Cmathit%7Bpair%7D+%26%3D%26+%5Cmathit%7Bid%7D+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{apply} &#92;cdot ( &#92;times B) &#92;mathit{pair} &amp;=&amp; &#92;mathit{id} &#92;&#92; (B &#92;Rightarrow )&#92;mathit{apply} &#92;cdot &#92;mathit{pair} &amp;=&amp; &#92;mathit{id} &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{apply} &#92;cdot ( &#92;times B) &#92;mathit{pair} &amp;=&amp; &#92;mathit{id} &#92;&#92; (B &#92;Rightarrow )&#92;mathit{apply} &#92;cdot &#92;mathit{pair} &amp;=&amp; &#92;mathit{id} &#92;end{array} ' class='latex' />
</p></blockquote>
<p> or, in points, </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Cmathit%7Bapply%7D+%28%5Cmathit%7Bpair%7D%5C%2Ca%2Cb%29+%26%3D%26+%28a%2Cb%29+%5C%5C+%5Cmathit%7Bapply%7D+%5Ccdot+%5Cmathit%7Bpair%7D%5C%2Cf+%26%3D%26+f+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{apply} (&#92;mathit{pair}&#92;,a,b) &amp;=&amp; (a,b) &#92;&#92; &#92;mathit{apply} &#92;cdot &#92;mathit{pair}&#92;,f &amp;=&amp; f &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{apply} (&#92;mathit{pair}&#92;,a,b) &amp;=&amp; (a,b) &#92;&#92; &#92;mathit{apply} &#92;cdot &#92;mathit{pair}&#92;,f &amp;=&amp; f &#92;end{array} ' class='latex' />
</p></blockquote>
<p> The isomorphism itself is witnessed by the two inverse functions </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Cmathit%7Bcurry%7D%5C%2Cf+%26%3D%26+%28B+%5CRightarrow+%29+f+%5Ccdot+%5Cmathit%7Bpair%7D+%5C%5C+%5Cmathit%7Buncurry%7D%5C%2Cg+%26%3D%26+%5Cmathit%7Bapply%7D+%5Ccdot+%28+%5Ctimes+B%29+g+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{curry}&#92;,f &amp;=&amp; (B &#92;Rightarrow ) f &#92;cdot &#92;mathit{pair} &#92;&#92; &#92;mathit{uncurry}&#92;,g &amp;=&amp; &#92;mathit{apply} &#92;cdot ( &#92;times B) g &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{curry}&#92;,f &amp;=&amp; (B &#92;Rightarrow ) f &#92;cdot &#92;mathit{pair} &#92;&#92; &#92;mathit{uncurry}&#92;,g &amp;=&amp; &#92;mathit{apply} &#92;cdot ( &#92;times B) g &#92;end{array} ' class='latex' />
</p></blockquote>
<p> where <img src='http://s0.wp.com/latex.php?latex=%7Bf+%3A+A+%5Ctimes+B+%5Crightarrow+C%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f : A &#92;times B &#92;rightarrow C}' title='{f : A &#92;times B &#92;rightarrow C}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7Bg+%3A+A+%5Crightarrow+%28B+%5CRightarrow+C%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{g : A &#92;rightarrow (B &#92;Rightarrow C)}' title='{g : A &#92;rightarrow (B &#92;Rightarrow C)}' class='latex' />.</p>
<p>
<br />  <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=patternsinfp.wordpress.com&#038;blog=15593982&#038;post=135&#038;subd=patternsinfp&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://patternsinfp.wordpress.com/2011/03/28/adjunctions/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/39d7e0397cba13d1494d9a48c1f42167?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jeremygibbons</media:title>
		</media:content>
	</item>
		<item>
		<title>Universal properties and Galois connections</title>
		<link>http://patternsinfp.wordpress.com/2011/02/15/universal-properties-and-galois-connections/</link>
		<comments>http://patternsinfp.wordpress.com/2011/02/15/universal-properties-and-galois-connections/#comments</comments>
		<pubDate>Tue, 15 Feb 2011 13:45:51 +0000</pubDate>
		<dc:creator>jeremygibbons</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://patternsinfp.wordpress.com/?p=124</guid>
		<description><![CDATA[One recurring theme throughout this series will be that of a universal property&#8212;an identity that captures an indirect means of solving a problem, by transforming that problem into a different (and hopefully simpler) domain, while still preserving all its essential &#8230; <a href="http://patternsinfp.wordpress.com/2011/02/15/universal-properties-and-galois-connections/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=patternsinfp.wordpress.com&#038;blog=15593982&#038;post=124&#038;subd=patternsinfp&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>
 One recurring theme throughout this series will be that of a <em>universal property</em>&mdash;an identity that captures an indirect means of solving a problem, by transforming that problem into a different (and hopefully simpler) domain, while still preserving all its essential properties. In particular, the original problem has a solution if and only if the transformed problem does, and moreover, the solution to the transformed problem can easily be translated back into a solution to the original problem. One can see universal properties as a generalization of the notion of a <em>Galois connection</em> between two orderings, which are a similarly powerful technique of relating problems in two different settings. (In fact, the proper generalization of Galois connections is to <em>adjunctions</em>, but that&#8217;s <a href="http://patternsinfp.wordpress.com/2011/03/28/adjunctions/">a story for next time</a>.)</p>
<p><h2> Universal properties </h2>
<p>
The universal property of the <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfork%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{fork}}' title='{&#92;mathit{fork}}' class='latex' /> operation for products is a representative example. Recall that <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfork%7D%5C%2C%28f%2Cg%29+%3A%3A+a+%5Crightarrow+%28b%2Cc%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{fork}&#92;,(f,g) :: a &#92;rightarrow (b,c)}' title='{&#92;mathit{fork}&#92;,(f,g) :: a &#92;rightarrow (b,c)}' class='latex' /> when <img src='http://s0.wp.com/latex.php?latex=%7Bf+%3A%3A+a+%5Crightarrow+b%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f :: a &#92;rightarrow b}' title='{f :: a &#92;rightarrow b}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7Bg+%3A%3A+a+%5Crightarrow+c%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{g :: a &#92;rightarrow c}' title='{g :: a &#92;rightarrow c}' class='latex' />; and that <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfst%7D+%3A%3A+%28b%2Cc%29+%5Crightarrow+b%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{fst} :: (b,c) &#92;rightarrow b}' title='{&#92;mathit{fst} :: (b,c) &#92;rightarrow b}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bsnd%7D+%3A%3A+%28b%2Cc%29+%5Crightarrow+c%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{snd} :: (b,c) &#92;rightarrow c}' title='{&#92;mathit{snd} :: (b,c) &#92;rightarrow c}' class='latex' />. Then <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfork%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{fork}}' title='{&#92;mathit{fork}}' class='latex' /> is completely defined by its universal property: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++h+%3D+%5Cmathit%7Bfork%7D%5C%2C%28f%2Cg%29+%5Cquad%5CLeftrightarrow%5Cquad+%5Cmathit%7Bfst%7D+%5Ccdot+h+%3D+f+%5Cland+%5Cmathit%7Bsnd%7D+%5Ccdot+h+%3D+g+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  h = &#92;mathit{fork}&#92;,(f,g) &#92;quad&#92;Leftrightarrow&#92;quad &#92;mathit{fst} &#92;cdot h = f &#92;land &#92;mathit{snd} &#92;cdot h = g ' title='&#92;displaystyle  h = &#92;mathit{fork}&#92;,(f,g) &#92;quad&#92;Leftrightarrow&#92;quad &#92;mathit{fst} &#92;cdot h = f &#92;land &#92;mathit{snd} &#92;cdot h = g ' class='latex' />
</p></blockquote>
<p> This identity repays careful study. </p>
<ul>
<li> It translates a problem in the more complex domain of products (namely, the problem of showing how some complicated expression <img src='http://s0.wp.com/latex.php?latex=%7Bh%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h}' title='{h}' class='latex' /> can be written in terms of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfork%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{fork}}' title='{&#92;mathit{fork}}' class='latex' />) into simpler problems (here, equations about the two projections of <img src='http://s0.wp.com/latex.php?latex=%7Bh%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h}' title='{h}' class='latex' />).
<li> It&#8217;s an equivalence. So not only do you have an implication from left to right (any <img src='http://s0.wp.com/latex.php?latex=%7Bh%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h}' title='{h}' class='latex' /> expressible as a <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfork%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{fork}}' title='{&#92;mathit{fork}}' class='latex' /> satisfies the two properties on the right), you also have one from right to left (any pair of functions <img src='http://s0.wp.com/latex.php?latex=%7Bf%2Cg%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f,g}' title='{f,g}' class='latex' /> satisfying the two properties on the right induces a <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfork%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{fork}}' title='{&#92;mathit{fork}}' class='latex' />). In other words, <img src='http://s0.wp.com/latex.php?latex=%7Bh%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h}' title='{h}' class='latex' /> is a solution to the equation on the left iff it is a solution on the right; not only does a solution on the right yield a construction on the left, but also the absence of solutions on the right implies the absence on the left. Or again: the equations on the right have a unique solution in <img src='http://s0.wp.com/latex.php?latex=%7Bh%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h}' title='{h}' class='latex' />&mdash;since any two solutions <img src='http://s0.wp.com/latex.php?latex=%7Bh%2Ch%27%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h,h&#039;}' title='{h,h&#039;}' class='latex' /> must both be equal to the same expression on the left.
<li> It has many useful simple consequences. You can make the left-hand side trivially true by letting <img src='http://s0.wp.com/latex.php?latex=%7Bh+%3D+%5Cmathit%7Bfork%7D%5C%2C%28f%2Cg%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h = &#92;mathit{fork}&#92;,(f,g)}' title='{h = &#92;mathit{fork}&#92;,(f,g)}' class='latex' />; then the right-hand side must also be true:<br />
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Cmathit%7Bfst%7D+%5Ccdot+%5Cmathit%7Bfork%7D%5C%2C%28f%2Cg%29+%26%3D%26+f+%5C%5C+%5Cmathit%7Bsnd%7D+%5Ccdot+%5Cmathit%7Bfork%7D%5C%2C%28f%2Cg%29+%26%3D%26+g+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{fst} &#92;cdot &#92;mathit{fork}&#92;,(f,g) &amp;=&amp; f &#92;&#92; &#92;mathit{snd} &#92;cdot &#92;mathit{fork}&#92;,(f,g) &amp;=&amp; g &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{fst} &#92;cdot &#92;mathit{fork}&#92;,(f,g) &amp;=&amp; f &#92;&#92; &#92;mathit{snd} &#92;cdot &#92;mathit{fork}&#92;,(f,g) &amp;=&amp; g &#92;end{array} ' class='latex' />
</p></blockquote>
<p> Symmetrically, you can make the right-hand side trivially true by letting <img src='http://s0.wp.com/latex.php?latex=%7Bf+%3D+%5Cmathit%7Bfst%7D+%5Ccdot+h%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f = &#92;mathit{fst} &#92;cdot h}' title='{f = &#92;mathit{fst} &#92;cdot h}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7Bg+%3D+%5Cmathit%7Bsnd%7D+%5Ccdot+h%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{g = &#92;mathit{snd} &#92;cdot h}' title='{g = &#92;mathit{snd} &#92;cdot h}' class='latex' />; then the left-hand side must also be true: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++h+%3D+%5Cmathit%7Bfork%7D%5C%2C%28%5Cmathit%7Bfst%7D+%5Ccdot+h%2C+%5Cmathit%7Bsnd%7D+%5Ccdot+h%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  h = &#92;mathit{fork}&#92;,(&#92;mathit{fst} &#92;cdot h, &#92;mathit{snd} &#92;cdot h) ' title='&#92;displaystyle  h = &#92;mathit{fork}&#92;,(&#92;mathit{fst} &#92;cdot h, &#92;mathit{snd} &#92;cdot h) ' class='latex' />
</p></blockquote>
<p> If you further let <img src='http://s0.wp.com/latex.php?latex=%7Bh+%3D+%5Cmathit%7Bid%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h = &#92;mathit{id}}' title='{h = &#92;mathit{id}}' class='latex' />, you conclude that every pair consists solely of its two projections, nothing more: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathit%7Bid%7D+%3D+%5Cmathit%7Bfork%7D%5C%2C%28%5Cmathit%7Bfst%7D%2C+%5Cmathit%7Bsnd%7D%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathit{id} = &#92;mathit{fork}&#92;,(&#92;mathit{fst}, &#92;mathit{snd}) ' title='&#92;displaystyle  &#92;mathit{id} = &#92;mathit{fork}&#92;,(&#92;mathit{fst}, &#92;mathit{snd}) ' class='latex' />
</p></blockquote>
<p> In fact, the universal property of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfork%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{fork}}' title='{&#92;mathit{fork}}' class='latex' /> tells you everything you need to know about <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfork%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{fork}}' title='{&#92;mathit{fork}}' class='latex' />; you might take that as one justification for the term &#8220;universal&#8221;.</p>
<li> It also has many useful less obvious consequences. For example, if you&#8217;re searching for an <img src='http://s0.wp.com/latex.php?latex=%7Bh%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h}' title='{h}' class='latex' /> that acts independently on the two components of a pair&mdash;<img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfst%7D+%5Ccdot+h+%3D+h_1+%5Ccdot+%5Cmathit%7Bfst%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{fst} &#92;cdot h = h_1 &#92;cdot &#92;mathit{fst}}' title='{&#92;mathit{fst} &#92;cdot h = h_1 &#92;cdot &#92;mathit{fst}}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bsnd%7D+%5Ccdot+h+%3D+h_2+%5Ccdot+%5Cmathit%7Bsnd%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{snd} &#92;cdot h = h_2 &#92;cdot &#92;mathit{snd}}' title='{&#92;mathit{snd} &#92;cdot h = h_2 &#92;cdot &#92;mathit{snd}}' class='latex' />&mdash;just let <img src='http://s0.wp.com/latex.php?latex=%7Bf+%3D+h_1+%5Ccdot+%5Cmathit%7Bfst%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f = h_1 &#92;cdot &#92;mathit{fst}}' title='{f = h_1 &#92;cdot &#92;mathit{fst}}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7Bg+%3D+h_2+%5Ccdot+%5Cmathit%7Bsnd%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{g = h_2 &#92;cdot &#92;mathit{snd}}' title='{g = h_2 &#92;cdot &#92;mathit{snd}}' class='latex' /> in the universal property, and conclude<br />
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++h+%3D+%5Cmathit%7Bfork%7D%5C%2C%28h_1%5Ccdot%5Cmathit%7Bfst%7D%2C+h_2%5Ccdot%5Cmathit%7Bsnd%7D%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  h = &#92;mathit{fork}&#92;,(h_1&#92;cdot&#92;mathit{fst}, h_2&#92;cdot&#92;mathit{snd}) ' title='&#92;displaystyle  h = &#92;mathit{fork}&#92;,(h_1&#92;cdot&#92;mathit{fst}, h_2&#92;cdot&#92;mathit{snd}) ' class='latex' />
</p></blockquote>
<p> (which we&#8217;ve written &#8220;<img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bprod%7D%5C%2C%28h_1%2Ch_2%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{prod}&#92;,(h_1,h_2)}' title='{&#92;mathit{prod}&#92;,(h_1,h_2)}' class='latex' />&#8221; elsewhere). For another example, we can deduce a fusion law for <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfork%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{fork}}' title='{&#92;mathit{fork}}' class='latex' />: for what <img src='http://s0.wp.com/latex.php?latex=%7Bf%27%2Cg%27%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f&#039;,g&#039;}' title='{f&#039;,g&#039;}' class='latex' /> does the equation </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathit%7Bfork%7D%5C%2C%28f%2Cg%29+%5Ccdot+k+%3D+%5Cmathit%7Bfork%7D%5C%2C%28f%27%2Cg%27%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathit{fork}&#92;,(f,g) &#92;cdot k = &#92;mathit{fork}&#92;,(f&#039;,g&#039;) ' title='&#92;displaystyle  &#92;mathit{fork}&#92;,(f,g) &#92;cdot k = &#92;mathit{fork}&#92;,(f&#039;,g&#039;) ' class='latex' />
</p></blockquote>
<p> hold? This matches the left-hand side of the universal property; expanding the right-hand side yields </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blclcl%7D+f%27+%26%3D%26+%5Cmathit%7Bfst%7D%5Ccdot%5Cmathit%7Bfork%7D%5C%2C%28f%2Cg%29%5Ccdot+k+%26%3D%26+f+%5Ccdot+k+%5C%5C+g%27+%26%3D%26+%5Cmathit%7Bsnd%7D%5Ccdot%5Cmathit%7Bfork%7D%5C%2C%28f%2Cg%29%5Ccdot+k+%26%3D%26+g+%5Ccdot+k+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lclcl} f&#039; &amp;=&amp; &#92;mathit{fst}&#92;cdot&#92;mathit{fork}&#92;,(f,g)&#92;cdot k &amp;=&amp; f &#92;cdot k &#92;&#92; g&#039; &amp;=&amp; &#92;mathit{snd}&#92;cdot&#92;mathit{fork}&#92;,(f,g)&#92;cdot k &amp;=&amp; g &#92;cdot k &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lclcl} f&#039; &amp;=&amp; &#92;mathit{fst}&#92;cdot&#92;mathit{fork}&#92;,(f,g)&#92;cdot k &amp;=&amp; f &#92;cdot k &#92;&#92; g&#039; &amp;=&amp; &#92;mathit{snd}&#92;cdot&#92;mathit{fork}&#92;,(f,g)&#92;cdot k &amp;=&amp; g &#92;cdot k &#92;end{array} ' class='latex' />
</p></blockquote>
</ul>
<p> Such a rich harvest from so small a seed! (In fact, we will see later that an even smaller seed suffices.)</p>
<p><h2> Galois connections </h2>
<p>
We can see the same structures that occur in universal properties like that of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfork%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{fork}}' title='{&#92;mathit{fork}}' class='latex' /> above also in relationships between orderings. As a very simple example, consider the problem of dividing a natural number <img src='http://s0.wp.com/latex.php?latex=%7Bn%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{n}' title='{n}' class='latex' /> by two, exactly; the universal property of a solution <img src='http://s0.wp.com/latex.php?latex=%7Bm%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{m}' title='{m}' class='latex' /> to this problem is the equivalence </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++n+%2F+2+%3D+m+%5CLeftrightarrow+n+%3D+m+%5Ctimes+2+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  n / 2 = m &#92;Leftrightarrow n = m &#92;times 2 ' title='&#92;displaystyle  n / 2 = m &#92;Leftrightarrow n = m &#92;times 2 ' class='latex' />
</p></blockquote>
<p> That is, <img src='http://s0.wp.com/latex.php?latex=%7Bm%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{m}' title='{m}' class='latex' /> is a solution to the problem &#8220;compute <img src='http://s0.wp.com/latex.php?latex=%7Bn+%2F+2%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{n / 2}' title='{n / 2}' class='latex' />&#8221; precisely when <img src='http://s0.wp.com/latex.php?latex=%7Bn+%3D+m+%5Ctimes+2%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{n = m &#92;times 2}' title='{n = m &#92;times 2}' class='latex' />; both the existence and the identification of a solution to a problem expressed in terms of division has been translated to one in terms of multiplication&mdash;which is arguably a simpler setting. Note that the universal property amounts to an equivalence </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++f%28n%29+%3D+m+%5CLeftrightarrow+n+%3D+g%28m%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  f(n) = m &#92;Leftrightarrow n = g(m) ' title='&#92;displaystyle  f(n) = m &#92;Leftrightarrow n = g(m) ' class='latex' />
</p></blockquote>
<p> involving the two functions <img src='http://s0.wp.com/latex.php?latex=%7Bf+%3D+%28%2F2%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f = (/2)}' title='{f = (/2)}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7Bg+%3D+%28%5Ctimes+2%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{g = (&#92;times 2)}' title='{g = (&#92;times 2)}' class='latex' />, which are in some sense inverses. This pattern will crop up over and over again.</p>
<p>
The division example involved an equivalence between the two identities <img src='http://s0.wp.com/latex.php?latex=%7Bf%28n%29%3Dm%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f(n)=m}' title='{f(n)=m}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7Bn%3Dg%28m%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{n=g(m)}' title='{n=g(m)}' class='latex' />. More generally, another relation than &#8220;<img src='http://s0.wp.com/latex.php?latex=%7B%3D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{=}' title='{=}' class='latex' />&#8221; might be involved. Extending the previous example to integer division, rounding down, we have for <img src='http://s0.wp.com/latex.php?latex=%7Bk%3E0%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{k&gt;0}' title='{k&gt;0}' class='latex' />: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++n+%5Cdiv+k+%5Cge+m+%5CLeftrightarrow+n+%5Cge+m+%5Ctimes+k+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  n &#92;div k &#92;ge m &#92;Leftrightarrow n &#92;ge m &#92;times k ' title='&#92;displaystyle  n &#92;div k &#92;ge m &#92;Leftrightarrow n &#92;ge m &#92;times k ' class='latex' />
</p></blockquote>
<p> Again, this relates the two (in some sense inverse) functions <img src='http://s0.wp.com/latex.php?latex=%7B%28%5Cdiv+k%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(&#92;div k)}' title='{(&#92;div k)}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B%28%5Ctimes+k%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(&#92;times k)}' title='{(&#92;times k)}' class='latex' />; but this time equality is inadequate for stating the problem, and it perhaps more convincing to claim that a more complicated problem <img src='http://s0.wp.com/latex.php?latex=%7B%28%5Cdiv+k%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(&#92;div k)}' title='{(&#92;div k)}' class='latex' /> has been translated into a simpler one <img src='http://s0.wp.com/latex.php?latex=%7B%28%5Ctimes+k%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(&#92;times k)}' title='{(&#92;times k)}' class='latex' />. What is more, translating the problem via this universal property pays dividends when it comes to reasoning about the problem, because the simpler problem space is much more amenable to calculation. For example, properties of repeated division <img src='http://s0.wp.com/latex.php?latex=%7B%28n+%5Cdiv+k%29+%5Cdiv+l%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(n &#92;div k) &#92;div l}' title='{(n &#92;div k) &#92;div l}' class='latex' /> (for <img src='http://s0.wp.com/latex.php?latex=%7Bk%2Cl%3E0%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{k,l&gt;0}' title='{k,l&gt;0}' class='latex' />) do not trip off the tongue; but we can reason straightforwardly as follows: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Bll%7D+%26+%28n+%5Cdiv+k%29+%5Cdiv+l+%5Cge+m+%5C%5C+%5CLeftrightarrow+%26+%5Cqquad+%5C%7B+%5Cmbox%7Buniversal+property%7D+%5C%7D+%5C%5C+%26+n+%5Cdiv+k+%5Cge+m+%5Ctimes+l+%5C%5C+%5CLeftrightarrow+%26+%5Cqquad+%5C%7B+%5Cmbox%7Buniversal+property%7D+%5C%7D+%5C%5C+%26+n+%5Cge+%28m+%5Ctimes+l%29+%5Ctimes+k+%5C%5C+%5CLeftrightarrow+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bmultiplication+is+associative%7D+%5C%7D+%5C%5C+%26+n+%5Cge+m+%5Ctimes+%28l+%5Ctimes+k%29+%5C%5C+%5CLeftrightarrow+%26+%5Cqquad+%5C%7B+%5Cmbox%7Buniversal+property%7D+%5C%7D+%5C%5C+%26+n+%5Cdiv+%28l+%5Ctimes+k%29+%5Cge+m+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{ll} &amp; (n &#92;div k) &#92;div l &#92;ge m &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{universal property} &#92;} &#92;&#92; &amp; n &#92;div k &#92;ge m &#92;times l &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{universal property} &#92;} &#92;&#92; &amp; n &#92;ge (m &#92;times l) &#92;times k &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{multiplication is associative} &#92;} &#92;&#92; &amp; n &#92;ge m &#92;times (l &#92;times k) &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{universal property} &#92;} &#92;&#92; &amp; n &#92;div (l &#92;times k) &#92;ge m &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{ll} &amp; (n &#92;div k) &#92;div l &#92;ge m &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{universal property} &#92;} &#92;&#92; &amp; n &#92;div k &#92;ge m &#92;times l &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{universal property} &#92;} &#92;&#92; &amp; n &#92;ge (m &#92;times l) &#92;times k &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{multiplication is associative} &#92;} &#92;&#92; &amp; n &#92;ge m &#92;times (l &#92;times k) &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{universal property} &#92;} &#92;&#92; &amp; n &#92;div (l &#92;times k) &#92;ge m &#92;end{array} ' class='latex' />
</p></blockquote>
<p> Thus, <img src='http://s0.wp.com/latex.php?latex=%7B%28n+%5Cdiv+k%29+%5Cdiv+l+%5Cge+m%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(n &#92;div k) &#92;div l &#92;ge m}' title='{(n &#92;div k) &#92;div l &#92;ge m}' class='latex' /> precisely when <img src='http://s0.wp.com/latex.php?latex=%7Bn+%5Cdiv+%28l+%5Ctimes+k%29+%5Cge+m%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{n &#92;div (l &#92;times k) &#92;ge m}' title='{n &#92;div (l &#92;times k) &#92;ge m}' class='latex' />, or in other words, <img src='http://s0.wp.com/latex.php?latex=%7B%28n+%5Cdiv+k%29+%5Cdiv+l+%3D+n+%5Cdiv+%28l+%5Ctimes+k%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(n &#92;div k) &#92;div l = n &#92;div (l &#92;times k)}' title='{(n &#92;div k) &#92;div l = n &#92;div (l &#92;times k)}' class='latex' />.</p>
<p>
In this case, the two problem spaces have both involved the same relation <img src='http://s0.wp.com/latex.php?latex=%7B%5Cge%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;ge}' title='{&#92;ge}' class='latex' /> on the same domain, namely the natural numbers; that is not essential. For example, the universal property of the floor function <img src='http://s0.wp.com/latex.php?latex=%7B%5Clfloor%5Ccdot%5Crfloor%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;lfloor&#92;cdot&#92;rfloor}' title='{&#92;lfloor&#92;cdot&#92;rfloor}' class='latex' /> from reals to integers is given by: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathit%7Binj%7D%28n%29+%5Cle_R+x+%5CLeftrightarrow+n+%5Cle_I+%5Clfloor+x+%5Crfloor+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathit{inj}(n) &#92;le_R x &#92;Leftrightarrow n &#92;le_I &#92;lfloor x &#92;rfloor ' title='&#92;displaystyle  &#92;mathit{inj}(n) &#92;le_R x &#92;Leftrightarrow n &#92;le_I &#92;lfloor x &#92;rfloor ' class='latex' />
</p></blockquote>
<p> where, to be completely explicit, we have written <img src='http://s0.wp.com/latex.php?latex=%7B%5Cle_R%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;le_R}' title='{&#92;le_R}' class='latex' /> for the usual ordering on reals and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cle_I%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;le_I}' title='{&#92;le_I}' class='latex' /> for the corresponding ordering on integers, and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Binj%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{inj}}' title='{&#92;mathit{inj}}' class='latex' /> for the injection from the integers into the reals. This time the two problem spaces involve two different orderings on different domains; we say that the pair of functions <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Binj%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{inj}}' title='{&#92;mathit{inj}}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B%5Clfloor%5Ccdot%5Crfloor%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;lfloor&#92;cdot&#92;rfloor}' title='{&#92;lfloor&#92;cdot&#92;rfloor}' class='latex' /> form a <em>Galois connection</em> between the orderings <img src='http://s0.wp.com/latex.php?latex=%7B%5Cle_R%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;le_R}' title='{&#92;le_R}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cle_I%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;le_I}' title='{&#92;le_I}' class='latex' />. (We also see that the relationship between the two functions <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Binj%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{inj}}' title='{&#92;mathit{inj}}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B%5Clfloor%5Ccdot%5Crfloor%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;lfloor&#92;cdot&#92;rfloor}' title='{&#92;lfloor&#92;cdot&#92;rfloor}' class='latex' /> is becoming less like a pure inverse relationship, and more of an embedding&ndash;projection pair.)</p>
<p>
As a simple non-arithmetical example of a Galois connection on a single domain, consider some set <img src='http://s0.wp.com/latex.php?latex=%7BU%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{U}' title='{U}' class='latex' /> and a fixed subset <img src='http://s0.wp.com/latex.php?latex=%7BX+%5Csubseteq+U%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{X &#92;subseteq U}' title='{X &#92;subseteq U}' class='latex' />; then </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++A+%5Ccap+X+%5Csubseteq+B+%5CLeftrightarrow+A+%5Csubseteq+B+%5Ccup+%5Coverline%7BX%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  A &#92;cap X &#92;subseteq B &#92;Leftrightarrow A &#92;subseteq B &#92;cup &#92;overline{X} ' title='&#92;displaystyle  A &#92;cap X &#92;subseteq B &#92;Leftrightarrow A &#92;subseteq B &#92;cup &#92;overline{X} ' class='latex' />
</p></blockquote>
<p> That is, <img src='http://s0.wp.com/latex.php?latex=%7B%28%5Ccap+X%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(&#92;cap X)}' title='{(&#92;cap X)}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B%28%5Ccup+%5Coverline%7BX%7D%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(&#92;cup &#92;overline{X})}' title='{(&#92;cup &#92;overline{X})}' class='latex' /> form a Galois connection between <img src='http://s0.wp.com/latex.php?latex=%7B%5Csubseteq%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;subseteq}' title='{&#92;subseteq}' class='latex' /> and itself. </p>
<p>
A non-arithmetical example between two different domains is afforded by the field of <em>formal concept analysis</em>, which relates &#8220;objects&#8221; and their &#8220;properties&#8221;. Given are sets <img src='http://s0.wp.com/latex.php?latex=%7BO%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{O}' title='{O}' class='latex' /> of objects and <img src='http://s0.wp.com/latex.php?latex=%7BP%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{P}' title='{P}' class='latex' /> of properties, and a relation <img src='http://s0.wp.com/latex.php?latex=%7B%28%5Cvdash%29+%5Csubseteq+O+%5Ctimes+P%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(&#92;vdash) &#92;subseteq O &#92;times P}' title='{(&#92;vdash) &#92;subseteq O &#92;times P}' class='latex' />; we write <img src='http://s0.wp.com/latex.php?latex=%7Bo+%5Cmathrel%7B%5Cvdash%7D+p%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{o &#92;mathrel{&#92;vdash} p}' title='{o &#92;mathrel{&#92;vdash} p}' class='latex' /> to denote that object <img src='http://s0.wp.com/latex.php?latex=%7Bo%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{o}' title='{o}' class='latex' /> has property <img src='http://s0.wp.com/latex.php?latex=%7Bp%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{p}' title='{p}' class='latex' />. This induces &#8220;concept-forming operators&#8221; <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bintent%7D+%3A+2%5EO+%5Crightarrow+2%5EP%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{intent} : 2^O &#92;rightarrow 2^P}' title='{&#92;mathit{intent} : 2^O &#92;rightarrow 2^P}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bextent%7D+%3A+2%5EP+%5Crightarrow+2%5EO%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{extent} : 2^P &#92;rightarrow 2^O}' title='{&#92;mathit{extent} : 2^P &#92;rightarrow 2^O}' class='latex' /> defined by: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Cmathit%7Bintent%7D%28E%29+%26%3D%26+%5C%7B+p+%5Cin+P+%5Cmid+%5Cforall+o+%5Cin+E+.%5C%3B+o+%5Cmathrel%7B%5Cvdash%7D+p+%5C%7D+%5C%5C+%5Cmathit%7Bextent%7D%28I%29+%26%3D%26+%5C%7B+o+%5Cin+O+%5Cmid+%5Cforall+p+%5Cin+I+.%5C%3B+o+%5Cmathrel%7B%5Cvdash%7D+p+%5C%7D+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{intent}(E) &amp;=&amp; &#92;{ p &#92;in P &#92;mid &#92;forall o &#92;in E .&#92;; o &#92;mathrel{&#92;vdash} p &#92;} &#92;&#92; &#92;mathit{extent}(I) &amp;=&amp; &#92;{ o &#92;in O &#92;mid &#92;forall p &#92;in I .&#92;; o &#92;mathrel{&#92;vdash} p &#92;} &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{intent}(E) &amp;=&amp; &#92;{ p &#92;in P &#92;mid &#92;forall o &#92;in E .&#92;; o &#92;mathrel{&#92;vdash} p &#92;} &#92;&#92; &#92;mathit{extent}(I) &amp;=&amp; &#92;{ o &#92;in O &#92;mid &#92;forall p &#92;in I .&#92;; o &#92;mathrel{&#92;vdash} p &#92;} &#92;end{array} ' class='latex' />
</p></blockquote>
<p> That is, <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bintent%7D%28E%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{intent}(E)}' title='{&#92;mathit{intent}(E)}' class='latex' /> is the set of properties enjoyed by all objects in <img src='http://s0.wp.com/latex.php?latex=%7BE%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{E}' title='{E}' class='latex' />, and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bextent%7D%28I%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{extent}(I)}' title='{&#92;mathit{extent}(I)}' class='latex' /> is the set of objects enjoying all the properties in <img src='http://s0.wp.com/latex.php?latex=%7BI%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{I}' title='{I}' class='latex' />; a <em>concept</em> is a pair <img src='http://s0.wp.com/latex.php?latex=%7B%28E%2CI%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(E,I)}' title='{(E,I)}' class='latex' /> with <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bintent%7D%28E%29+%3D+I%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{intent}(E) = I}' title='{&#92;mathit{intent}(E) = I}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bextent%7D%28I%29+%3D+E%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{extent}(I) = E}' title='{&#92;mathit{extent}(I) = E}' class='latex' />. The concept-forming operators form a Galois connection between <img src='http://s0.wp.com/latex.php?latex=%7B%5Csubseteq%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;subseteq}' title='{&#92;subseteq}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B%5Csupseteq%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;supseteq}' title='{&#92;supseteq}' class='latex' />: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Bll%7D+%26+%5Cmathit%7Bextent%7D%28I%29+%5Csupseteq+E+%5C%5C+%5CLeftrightarrow%26+%5Cqquad+%5C%7B+%5Cmbox%7Bcharacteristic+of+%5C%28%5Cmathit%7Bextent%7D%5C%29%7D+%5C%7D+%5C%5C+%26+%5Cforall+o+%5Cin+E+.%5C%3B+%28%5Cforall+p+%5Cin+I+.%5C%3B+o+%5Cmathrel%7B%5Cvdash%7D+p%29+%5C%5C+%5CLeftrightarrow%26+%5Cqquad+%5C%7B+%5Cmbox%7Bcommuting+quantifiers%7D+%5C%7D+%5C%5C+%26+%5Cforall+p+%5Cin+I+.%5C%3B+%28%5Cforall+o+%5Cin+E+.%5C%3B+o+%5Cmathrel%7B%5Cvdash%7D+p%29+%5C%5C+%5CLeftrightarrow%26+%5Cqquad+%5C%7B+%5Cmbox%7Bcharacteristic+of+%5C%28%5Cmathit%7Bintent%7D%5C%29%7D+%5C%7D+%5C%5C+%26+I+%5Csubseteq+%5Cmathit%7Bintent%7D%28E%29+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;mathit{extent}(I) &#92;supseteq E &#92;&#92; &#92;Leftrightarrow&amp; &#92;qquad &#92;{ &#92;mbox{characteristic of &#92;(&#92;mathit{extent}&#92;)} &#92;} &#92;&#92; &amp; &#92;forall o &#92;in E .&#92;; (&#92;forall p &#92;in I .&#92;; o &#92;mathrel{&#92;vdash} p) &#92;&#92; &#92;Leftrightarrow&amp; &#92;qquad &#92;{ &#92;mbox{commuting quantifiers} &#92;} &#92;&#92; &amp; &#92;forall p &#92;in I .&#92;; (&#92;forall o &#92;in E .&#92;; o &#92;mathrel{&#92;vdash} p) &#92;&#92; &#92;Leftrightarrow&amp; &#92;qquad &#92;{ &#92;mbox{characteristic of &#92;(&#92;mathit{intent}&#92;)} &#92;} &#92;&#92; &amp; I &#92;subseteq &#92;mathit{intent}(E) &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;mathit{extent}(I) &#92;supseteq E &#92;&#92; &#92;Leftrightarrow&amp; &#92;qquad &#92;{ &#92;mbox{characteristic of &#92;(&#92;mathit{extent}&#92;)} &#92;} &#92;&#92; &amp; &#92;forall o &#92;in E .&#92;; (&#92;forall p &#92;in I .&#92;; o &#92;mathrel{&#92;vdash} p) &#92;&#92; &#92;Leftrightarrow&amp; &#92;qquad &#92;{ &#92;mbox{commuting quantifiers} &#92;} &#92;&#92; &amp; &#92;forall p &#92;in I .&#92;; (&#92;forall o &#92;in E .&#92;; o &#92;mathrel{&#92;vdash} p) &#92;&#92; &#92;Leftrightarrow&amp; &#92;qquad &#92;{ &#92;mbox{characteristic of &#92;(&#92;mathit{intent}&#92;)} &#92;} &#92;&#92; &amp; I &#92;subseteq &#92;mathit{intent}(E) &#92;end{array} ' class='latex' />
</p></blockquote>
<p> This construction can be used to translate a problem about the extension of a concept (that is, an enumeration of its instances) into one about the intension (that is, the characteristic properties of its instances). It is related to the observation that &#8220;<a href="http://www.logicmatters.net/2010/06/the-galois-connection-between-syntax-and-semantics/">syntax and semantics are adjoint</a>&#8220;&mdash;under the analogy that &#8220;objects&#8221; are sets of mathematical structures, &#8220;properties&#8221; are axioms, and the relation is &#8220;satisfaction&#8221;, the models of an axiomatic theory <img src='http://s0.wp.com/latex.php?latex=%7BT%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{T}' title='{T}' class='latex' /> are included in a set of structures <img src='http://s0.wp.com/latex.php?latex=%7BS%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{S}' title='{S}' class='latex' /> if and only if the theory <img src='http://s0.wp.com/latex.php?latex=%7BT%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{T}' title='{T}' class='latex' /> logically entails the minimal axiomatization of <img src='http://s0.wp.com/latex.php?latex=%7BS%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{S}' title='{S}' class='latex' />.</p>
<p>
<br />  <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=patternsinfp.wordpress.com&#038;blog=15593982&#038;post=124&#038;subd=patternsinfp&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://patternsinfp.wordpress.com/2011/02/15/universal-properties-and-galois-connections/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/39d7e0397cba13d1494d9a48c1f42167?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jeremygibbons</media:title>
		</media:content>
	</item>
		<item>
		<title>Lenses are the coalgebras for the costate comonad</title>
		<link>http://patternsinfp.wordpress.com/2011/01/31/lenses-are-the-coalgebras-for-the-costate-comonad/</link>
		<comments>http://patternsinfp.wordpress.com/2011/01/31/lenses-are-the-coalgebras-for-the-costate-comonad/#comments</comments>
		<pubDate>Mon, 31 Jan 2011 13:50:13 +0000</pubDate>
		<dc:creator>jeremygibbons</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://patternsinfp.wordpress.com/?p=118</guid>
		<description><![CDATA[I took part in the Dagstuhl Seminar on Bidirectional Transformations &#8220;BX&#8221; earlier this month. It was a meeting of people from four communities&#8212;databases, graph transformations, programming languages, and software engineering&#8212;discussing their various perspectives&#8212;namely the view&#8211;update problem in databases, triple graph &#8230; <a href="http://patternsinfp.wordpress.com/2011/01/31/lenses-are-the-coalgebras-for-the-costate-comonad/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=patternsinfp.wordpress.com&#038;blog=15593982&#038;post=118&#038;subd=patternsinfp&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>
 I took part in the <a href="http://www.dagstuhl.de/11031">Dagstuhl Seminar on Bidirectional Transformations &#8220;BX&#8221;</a> earlier this month. It was a meeting of people from four communities&mdash;databases, graph transformations, programming languages, and software engineering&mdash;discussing their various perspectives&mdash;namely the view&ndash;update problem in databases, triple graph grammars, lenses, and model synchronization&mdash;on the common problem of &#8220;BX&#8221;.</p>
<p>
While there, I reported on a marvellous observation made by <a href="http://r6.ca/">Russell O&#8217;Connor</a>, that <a href="http://r6research.livejournal.com/23705.html">lenses are exactly the coalgebras for the costate comonad</a>. That is, the independently identified notion of a &#8220;very well-behaved lens&#8221; in <a href="http://dx.doi.org/10.1145/1232420.1232424">the work of Pierce and others</a> coincides exactly with the categorical notion of a &#8220;coalgebra&#8221; for a particular comonad, the &#8220;costate&#8221; comonad. I&#8217;ll unpack that claim here.</p>
<p><h2> Lenses </h2>
<p>
Pierce&#8217;s lenses are pairs of functions between &#8220;source&#8221; and &#8220;view&#8221; datatypes <img src='http://s0.wp.com/latex.php?latex=%7BS%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{S}' title='{S}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7BV%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{V}' title='{V}' class='latex' />: a &#8220;get&#8221; function <img src='http://s0.wp.com/latex.php?latex=%7Bg+%3A+S+%5Crightarrow+V%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{g : S &#92;rightarrow V}' title='{g : S &#92;rightarrow V}' class='latex' /> and a &#8220;put&#8221; function <img src='http://s0.wp.com/latex.php?latex=%7Bp+%3A+S+%5Ctimes+V+%5Crightarrow+S%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{p : S &#92;times V &#92;rightarrow S}' title='{p : S &#92;times V &#92;rightarrow S}' class='latex' />. The story is that the view is some projection of the data in the source&mdash;perhaps a subset of the data, or the data in a simpler format&mdash;and so in order to update the source given a modified view, one needs also a copy of the original source from which to reconstruct the missing information.</p>
<p>
For these two functions to capture a &#8220;well-behaved&#8221; lens, they should satisfy the so-called Get&ndash;Put and Put&ndash;Get laws: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+p%5C%2C%28s%2Cg%5C%2Cs%29+%26%3D%26+s+%5C%5C+g%5C%2C%28p%5C%2C%28s%2Cv%29%29+%26%3D%26+v+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} p&#92;,(s,g&#92;,s) &amp;=&amp; s &#92;&#92; g&#92;,(p&#92;,(s,v)) &amp;=&amp; v &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} p&#92;,(s,g&#92;,s) &amp;=&amp; s &#92;&#92; g&#92;,(p&#92;,(s,v)) &amp;=&amp; v &#92;end{array} ' class='latex' />
</p></blockquote>
<p> The Get&ndash;Put law says that if you &#8220;get&#8221; a view of the source, and then &#8220;put&#8221; it straight back without modifying it, the source remains unmodified: a no-op edit on the view translates into a no-op on the source. The Put&ndash;Get law says that if you &#8220;put&#8221; any view into a source and then &#8220;get&#8221; it back, you end up with the view you first thought of: nothing is lost from the view when it is put back.</p>
<p>
Additionally, for these two functions to capture a &#8220;very well-behaved&#8221; lens, they must satisfy a third law, the Put&ndash;Put law: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+p%5C%2C%28p%5C%2C%28s%2Cv%29%2Cu%29+%26%3D%26+p%5C%2C%28s%2Cu%29+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} p&#92;,(p&#92;,(s,v),u) &amp;=&amp; p&#92;,(s,u) &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} p&#92;,(p&#92;,(s,v),u) &amp;=&amp; p&#92;,(s,u) &#92;end{array} ' class='latex' />
</p></blockquote>
<p> In words, &#8220;put&#8221;ting back two views <img src='http://s0.wp.com/latex.php?latex=%7Bv%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{v}' title='{v}' class='latex' /> then <img src='http://s0.wp.com/latex.php?latex=%7Bu%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{u}' title='{u}' class='latex' /> is equivalent to &#8220;put&#8221;ting back just the second; any changes to the source from putting back <img src='http://s0.wp.com/latex.php?latex=%7Bv%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{v}' title='{v}' class='latex' /> are completely overwritten when putting back <img src='http://s0.wp.com/latex.php?latex=%7Bu%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{u}' title='{u}' class='latex' />. (This turns out to be rather a strong condition, requiring that the source basically factors into the view and a completely independent &#8220;complement&#8221;; few real applications of bidirectional transformation satisfy it. But that&#8217;s another story.)</p>
<p><h2> The costate comonad </h2>
<p>
Intuitively, comonads capture &#8220;data in context&#8221;. A comonad <img src='http://s0.wp.com/latex.php?latex=%7B%28D%2C%5Cmathit%7Bextr%7D%2C%5Cmathit%7Bdupl%7D%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(D,&#92;mathit{extr},&#92;mathit{dupl})}' title='{(D,&#92;mathit{extr},&#92;mathit{dupl})}' class='latex' /> consists of a functor <img src='http://s0.wp.com/latex.php?latex=%7BD%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{D}' title='{D}' class='latex' /> together with two natural transformations <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bextr%7D+%3A+D+%5Crightarrow+1%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{extr} : D &#92;rightarrow 1}' title='{&#92;mathit{extr} : D &#92;rightarrow 1}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bdupl%7D+%3A+D+%5Crightarrow+DD%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{dupl} : D &#92;rightarrow DD}' title='{&#92;mathit{dupl} : D &#92;rightarrow DD}' class='latex' /> that extract the data from its context and duplicate the context, satisfying the three axioms: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Cmathit%7Bextr%7D+%5Ccdot+%5Cmathit%7Bdupl%7D+%26%3D%26+%5Cmathit%7Bid%7D+%5C%5C+%5Cmathit%7Bfmap%7D%5C%2C%5Cmathit%7Bextr%7D+%5Ccdot+%5Cmathit%7Bdupl%7D+%26%3D%26+%5Cmathit%7Bid%7D+%5C%5C+%5Cmathit%7Bfmap%7D%5C%2C%5Cmathit%7Bdupl%7D+%5Ccdot+%5Cmathit%7Bdupl%7D+%26%3D%26+%5Cmathit%7Bdupl%7D+%5Ccdot+%5Cmathit%7Bdupl%7D+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{extr} &#92;cdot &#92;mathit{dupl} &amp;=&amp; &#92;mathit{id} &#92;&#92; &#92;mathit{fmap}&#92;,&#92;mathit{extr} &#92;cdot &#92;mathit{dupl} &amp;=&amp; &#92;mathit{id} &#92;&#92; &#92;mathit{fmap}&#92;,&#92;mathit{dupl} &#92;cdot &#92;mathit{dupl} &amp;=&amp; &#92;mathit{dupl} &#92;cdot &#92;mathit{dupl} &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{extr} &#92;cdot &#92;mathit{dupl} &amp;=&amp; &#92;mathit{id} &#92;&#92; &#92;mathit{fmap}&#92;,&#92;mathit{extr} &#92;cdot &#92;mathit{dupl} &amp;=&amp; &#92;mathit{id} &#92;&#92; &#92;mathit{fmap}&#92;,&#92;mathit{dupl} &#92;cdot &#92;mathit{dupl} &amp;=&amp; &#92;mathit{dupl} &#92;cdot &#92;mathit{dupl} &#92;end{array} ' class='latex' />
</p></blockquote>
<p> One example of a comonad is the &#8220;costate&#8221; construction: for fixed <img src='http://s0.wp.com/latex.php?latex=%7BV%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{V}' title='{V}' class='latex' />, define functor <img src='http://s0.wp.com/latex.php?latex=%7BD%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{D}' title='{D}' class='latex' /> by </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+D%5C%2CA+%26%3D%26+V+%5Ctimes+%28V+%5Crightarrow+A%29+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} D&#92;,A &amp;=&amp; V &#92;times (V &#92;rightarrow A) &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} D&#92;,A &amp;=&amp; V &#92;times (V &#92;rightarrow A) &#92;end{array} ' class='latex' />
</p></blockquote>
<p> so that the &#8220;map&#8221; function for <img src='http://s0.wp.com/latex.php?latex=%7BD%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{D}' title='{D}' class='latex' /> satisfies <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfmap%7D%5C%2Ch%5C%2C%28v%2Cf%29+%3D+%28v%2C+h+%5Ccdot+f%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{fmap}&#92;,h&#92;,(v,f) = (v, h &#92;cdot f)}' title='{&#92;mathit{fmap}&#92;,h&#92;,(v,f) = (v, h &#92;cdot f)}' class='latex' />. The operations are given by </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Cmathit%7Bextr%7D%5C%2C%28v%2Cf%29+%26%3D%26+f%5C%2Cv+%5C%5C+%5Cmathit%7Bdupl%7D%5C%2C%28v%2Cf%29+%26%3D%26+%28v%2C+%5Clambda+u+%5Crightarrow+%28u%2Cf%29%29+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{extr}&#92;,(v,f) &amp;=&amp; f&#92;,v &#92;&#92; &#92;mathit{dupl}&#92;,(v,f) &amp;=&amp; (v, &#92;lambda u &#92;rightarrow (u,f)) &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{extr}&#92;,(v,f) &amp;=&amp; f&#92;,v &#92;&#92; &#92;mathit{dupl}&#92;,(v,f) &amp;=&amp; (v, &#92;lambda u &#92;rightarrow (u,f)) &#92;end{array} ' class='latex' />
</p></blockquote>
<p> Verifying that these definitions satisfy the comonad axioms is left as an exercise for the interested reader.</p>
<p>
(Incidentally, I think it&#8217;s called the &#8220;costate&#8221; comonad more because it is the dual <img src='http://s0.wp.com/latex.php?latex=%7B%28V%5Ctimes%29%5Ccdot%28V%5Crightarrow%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(V&#92;times)&#92;cdot(V&#92;rightarrow)}' title='{(V&#92;times)&#92;cdot(V&#92;rightarrow)}' class='latex' /> of the &#8220;state&#8221; monad <img src='http://s0.wp.com/latex.php?latex=%7B%28V%5Crightarrow%29%5Ccdot%28V%5Ctimes%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(V&#92;rightarrow)&#92;cdot(V&#92;times)}' title='{(V&#92;rightarrow)&#92;cdot(V&#92;times)}' class='latex' />, rather than because it has anything to do with stateful computations. However, it does model state in the sense of stored variables; and indeed, <a href="http://r6research.livejournal.com/23705.html">Russell O&#8217;Connor&#8217;s blog posting</a> calls <img src='http://s0.wp.com/latex.php?latex=%7BD%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{D}' title='{D}' class='latex' /> the &#8220;store&#8221; comonad.)</p>
<p><h2> Coalgebras of a comonad </h2>
<p>
For a functor <img src='http://s0.wp.com/latex.php?latex=%7BF%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{F}' title='{F}' class='latex' />, an <img src='http://s0.wp.com/latex.php?latex=%7BF%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{F}' title='{F}' class='latex' />-coalgebra is a pair <img src='http://s0.wp.com/latex.php?latex=%7B%28A%2Cf%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(A,f)}' title='{(A,f)}' class='latex' /> of a type <img src='http://s0.wp.com/latex.php?latex=%7BA%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{A}' title='{A}' class='latex' /> and a function <img src='http://s0.wp.com/latex.php?latex=%7Bf+%3A+A+%5Crightarrow+F%5C%2CA%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f : A &#92;rightarrow F&#92;,A}' title='{f : A &#92;rightarrow F&#92;,A}' class='latex' />. A &#8220;coalgebra for a comonad <img src='http://s0.wp.com/latex.php?latex=%7BD%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{D}' title='{D}' class='latex' />&#8221; is a <img src='http://s0.wp.com/latex.php?latex=%7BD%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{D}' title='{D}' class='latex' />-coalgebra that interacts well with the operations <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bextr%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{extr}}' title='{&#92;mathit{extr}}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bdupl%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{dupl}}' title='{&#92;mathit{dupl}}' class='latex' /> of the comonad; that is, the function <img src='http://s0.wp.com/latex.php?latex=%7Bf%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f}' title='{f}' class='latex' /> should also satisfy the laws: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Cmathit%7Bextr%7D+%5Ccdot+f+%26%3D%26+%5Cmathit%7Bid%7D+%5C%5C+%5Cmathit%7Bdupl%7D+%5Ccdot+f+%26%3D%26+%5Cmathit%7Bfmap%7D%5C%2Cf+%5Ccdot+f+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{extr} &#92;cdot f &amp;=&amp; &#92;mathit{id} &#92;&#92; &#92;mathit{dupl} &#92;cdot f &amp;=&amp; &#92;mathit{fmap}&#92;,f &#92;cdot f &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{extr} &#92;cdot f &amp;=&amp; &#92;mathit{id} &#92;&#92; &#92;mathit{dupl} &#92;cdot f &amp;=&amp; &#92;mathit{fmap}&#92;,f &#92;cdot f &#92;end{array} ' class='latex' />
</p></blockquote>
<p>
(Another incidentally: I don&#8217;t have a feeling for what these laws mean, in the way that I do for the laws of an algebra of a monad. At least for the free monads <img src='http://s0.wp.com/latex.php?latex=%7BT%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{T}' title='{T}' class='latex' /> that represent terms with free variables, an algebra is a pair <img src='http://s0.wp.com/latex.php?latex=%7B%28A%2C+f+%3A+T%5C%2CA+%5Crightarrow+A%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{(A, f : T&#92;,A &#92;rightarrow A)}' title='{(A, f : T&#92;,A &#92;rightarrow A)}' class='latex' /> such that <img src='http://s0.wp.com/latex.php?latex=%7Bf%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f}' title='{f}' class='latex' /> makes sense as an &#8220;expression evaluator&#8221;&mdash;it respects singleton variables and substitution. It&#8217;s clear to me that the laws of a coalgebra for a comonad are the obvious duals of those for the algebra of a monad; and that they describe the interesting ways of putting together the coalgebra operation with the comonad operations; but I still don&#8217;t have a direct intuition. Any comments gratefully received!)</p>
<p><h2> Lenses are coalgebras of the costate comonad </h2>
<p>
Now it&#8217;s just a matter of putting the pieces together. Curry the &#8220;put&#8221; function of a lens to obtain <img src='http://s0.wp.com/latex.php?latex=%7Bp+%3A+S+%5Crightarrow+%28V+%5Crightarrow+S%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{p : S &#92;rightarrow (V &#92;rightarrow S)}' title='{p : S &#92;rightarrow (V &#92;rightarrow S)}' class='latex' />, and define a lens to be the fork of the &#8220;get&#8221; and &#8220;put&#8221; functions: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Cell%5C%2Cs+%26%3D%26+%28g%5C%2Cs%2C+p%5C%2Cs%29+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;ell&#92;,s &amp;=&amp; (g&#92;,s, p&#92;,s) &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;ell&#92;,s &amp;=&amp; (g&#92;,s, p&#92;,s) &#92;end{array} ' class='latex' />
</p></blockquote>
<p> Note that now <img src='http://s0.wp.com/latex.php?latex=%7B%5Cell+%3A+S+%5Crightarrow+D%5C%2CS%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;ell : S &#92;rightarrow D&#92;,S}' title='{&#92;ell : S &#92;rightarrow D&#92;,S}' class='latex' /> where <img src='http://s0.wp.com/latex.php?latex=%7BD%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{D}' title='{D}' class='latex' /> is the costate comonad. The Get&ndash;Put law is equivalent to the counit axiom of the coalgebra: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Bll%7D+%26+%5Cmathit%7Bextr%7D+%5Ccdot+%5Cell+%3D+%5Cmathit%7Bid%7D+%5C%5C+%5CLeftrightarrow+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bapply+to+an+%5C%28s%5C%29%7D+%5C%7D+%5C%5C+%26+%5Cmathit%7Bextr%7D%5C%2C%28%5Cell%5C%2Cs%29+%3D+s+%5C%5C+%5CLeftrightarrow+%26+%5Cqquad+%5C%7B+%5Cmbox%7B%5C%28%5Cell%5C%29%7D+%5C%7D+%5C%5C+%26+%5Cmathit%7Bextr%7D%5C%2C%28g%5C%2Cs%2C+p%5C%2Cs%29+%3D+s+%5C%5C+%5CLeftrightarrow+%26+%5Cqquad+%5C%7B+%5Cmbox%7B%5C%28%5Cmathit%7Bextr%7D%5C%29%7D+%5C%7D+%5C%5C+%26+p%5C%2Cs%5C%2C%28g%5C%2Cs%29+%3D+s+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;mathit{extr} &#92;cdot &#92;ell = &#92;mathit{id} &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{apply to an &#92;(s&#92;)} &#92;} &#92;&#92; &amp; &#92;mathit{extr}&#92;,(&#92;ell&#92;,s) = s &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{&#92;(&#92;ell&#92;)} &#92;} &#92;&#92; &amp; &#92;mathit{extr}&#92;,(g&#92;,s, p&#92;,s) = s &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{&#92;(&#92;mathit{extr}&#92;)} &#92;} &#92;&#92; &amp; p&#92;,s&#92;,(g&#92;,s) = s &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;mathit{extr} &#92;cdot &#92;ell = &#92;mathit{id} &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{apply to an &#92;(s&#92;)} &#92;} &#92;&#92; &amp; &#92;mathit{extr}&#92;,(&#92;ell&#92;,s) = s &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{&#92;(&#92;ell&#92;)} &#92;} &#92;&#92; &amp; &#92;mathit{extr}&#92;,(g&#92;,s, p&#92;,s) = s &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{&#92;(&#92;mathit{extr}&#92;)} &#92;} &#92;&#92; &amp; p&#92;,s&#92;,(g&#92;,s) = s &#92;end{array} ' class='latex' />
</p></blockquote>
<p> And the Put&ndash;Get and Put&ndash;Put laws together are equivalent to the coassociativity axiom: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Bll%7D+%26+%5Cmathit%7Bdupl%7D+%5Ccdot+%5Cell+%3D+%5Cmathit%7Bfmap%7D%5C%2C%5Cell+%5Ccdot+%5Cell+%5C%5C+%5CLeftrightarrow+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bapply+to+an+%5C%28s%5C%29%7D+%5C%7D+%5C%5C+%26+%5Cmathit%7Bdupl%7D%5C%2C%28%5Cell%5C%2Cs%29+%3D+%5Cmathit%7Bfmap%7D%5C%2C%5Cell%5C%2C%28%5Cell%5C%2Cs%29+%5C%5C+%5CLeftrightarrow+%26+%5Cqquad+%5C%7B+%5Cmbox%7B%5C%28%5Cell%5C%29%7D+%5C%7D+%5C%5C+%26+%5Cmathit%7Bdupl%7D%5C%2C%28g%5C%2Cs%2C+p%5C%2Cs%29+%3D+%5Cmathit%7Bfmap%7D%5C%2C%5Cell%5C%2C%28g%5C%2Cs%2C+p%5C%2Cs%29+%5C%5C+%5CLeftrightarrow+%26+%5Cqquad+%5C%7B+%5Cmbox%7B%5C%28%5Cmathit%7Bfmap%7D%5C%29+for+%5C%28D%5C%29%7D+%5C%7D+%5C%5C+%26+%5Cmathit%7Bdupl%7D%5C%2C%28g%5C%2Cs%2C+p%5C%2Cs%29+%3D+%28g%5C%2Cs%2C+%5Cell+%5Ccdot+p%5C%2Cs%29+%5C%5C+%5CLeftrightarrow+%26+%5Cqquad+%5C%7B+%5Cmbox%7B%5C%28%5Cmathit%7Bdupl%7D%5C%29%7D+%5C%7D+%5C%5C+%26+%28g%5C%2Cs%2C+%5Clambda+v+%5Crightarrow+%28v%2C+p%5C%2Cs%29%29+%3D+%28g%5C%2Cs%2C+%5Cell+%5Ccdot+p%5C%2Cs%29+%5C%5C+%5CLeftrightarrow+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bfirst+components+are+clearly+equal%7D+%5C%7D+%5C%5C+%26+%5Clambda+v+%5Crightarrow+%28v%2C+p%5C%2Cs%29+%3D+%5Cell+%5Ccdot+p%5C%2Cs+%5C%5C+%5CLeftrightarrow+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bapply+to+a+%5C%28v%5C%29%7D+%5C%7D+%5C%5C+%26+%28v%2C+p%5C%2Cs%29+%3D+%5Cell%5C%2C%28p%5C%2Cs%5C%2Cv%29+%5C%5C+%5CLeftrightarrow+%26+%5Cqquad+%5C%7B+%5Cmbox%7B%5C%28%5Cell%5C%29%7D+%5C%7D+%5C%5C+%26+%28v%2C+p%5C%2Cs%29+%3D+%28g%5C%2C%28p%5C%2Cs%5C%2Cv%29%2C+p%5C%2C%28p%5C%2Cs%5C%2Cv%29%29+%5C%5C+%5CLeftrightarrow+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bapply+second+components+to+a+%5C%28u%5C%29%7D+%5C%7D+%5C%5C+%26+%28v%2C+p%5C%2Cs%5C%2Cu%29+%3D+%28g%5C%2C%28p%5C%2Cs%5C%2Cv%29%2C+p%5C%2C%28p%5C%2Cs%5C%2Cv%29%5C%2Cu%29+%5C%5C+%5CLeftrightarrow+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bpair+equality+is+pointwise%7D+%5C%7D+%5C%5C+%26+v+%3D+g%5C%2C%28p%5C%2Cs%5C%2Cv%29+%5Cland+p%5C%2Cs%5C%2Cu+%3D+p%5C%2C%28p%5C%2Cs%5C%2Cv%29%5C%2Cu+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;mathit{dupl} &#92;cdot &#92;ell = &#92;mathit{fmap}&#92;,&#92;ell &#92;cdot &#92;ell &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{apply to an &#92;(s&#92;)} &#92;} &#92;&#92; &amp; &#92;mathit{dupl}&#92;,(&#92;ell&#92;,s) = &#92;mathit{fmap}&#92;,&#92;ell&#92;,(&#92;ell&#92;,s) &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{&#92;(&#92;ell&#92;)} &#92;} &#92;&#92; &amp; &#92;mathit{dupl}&#92;,(g&#92;,s, p&#92;,s) = &#92;mathit{fmap}&#92;,&#92;ell&#92;,(g&#92;,s, p&#92;,s) &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{&#92;(&#92;mathit{fmap}&#92;) for &#92;(D&#92;)} &#92;} &#92;&#92; &amp; &#92;mathit{dupl}&#92;,(g&#92;,s, p&#92;,s) = (g&#92;,s, &#92;ell &#92;cdot p&#92;,s) &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{&#92;(&#92;mathit{dupl}&#92;)} &#92;} &#92;&#92; &amp; (g&#92;,s, &#92;lambda v &#92;rightarrow (v, p&#92;,s)) = (g&#92;,s, &#92;ell &#92;cdot p&#92;,s) &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{first components are clearly equal} &#92;} &#92;&#92; &amp; &#92;lambda v &#92;rightarrow (v, p&#92;,s) = &#92;ell &#92;cdot p&#92;,s &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{apply to a &#92;(v&#92;)} &#92;} &#92;&#92; &amp; (v, p&#92;,s) = &#92;ell&#92;,(p&#92;,s&#92;,v) &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{&#92;(&#92;ell&#92;)} &#92;} &#92;&#92; &amp; (v, p&#92;,s) = (g&#92;,(p&#92;,s&#92;,v), p&#92;,(p&#92;,s&#92;,v)) &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{apply second components to a &#92;(u&#92;)} &#92;} &#92;&#92; &amp; (v, p&#92;,s&#92;,u) = (g&#92;,(p&#92;,s&#92;,v), p&#92;,(p&#92;,s&#92;,v)&#92;,u) &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{pair equality is pointwise} &#92;} &#92;&#92; &amp; v = g&#92;,(p&#92;,s&#92;,v) &#92;land p&#92;,s&#92;,u = p&#92;,(p&#92;,s&#92;,v)&#92;,u &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;mathit{dupl} &#92;cdot &#92;ell = &#92;mathit{fmap}&#92;,&#92;ell &#92;cdot &#92;ell &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{apply to an &#92;(s&#92;)} &#92;} &#92;&#92; &amp; &#92;mathit{dupl}&#92;,(&#92;ell&#92;,s) = &#92;mathit{fmap}&#92;,&#92;ell&#92;,(&#92;ell&#92;,s) &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{&#92;(&#92;ell&#92;)} &#92;} &#92;&#92; &amp; &#92;mathit{dupl}&#92;,(g&#92;,s, p&#92;,s) = &#92;mathit{fmap}&#92;,&#92;ell&#92;,(g&#92;,s, p&#92;,s) &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{&#92;(&#92;mathit{fmap}&#92;) for &#92;(D&#92;)} &#92;} &#92;&#92; &amp; &#92;mathit{dupl}&#92;,(g&#92;,s, p&#92;,s) = (g&#92;,s, &#92;ell &#92;cdot p&#92;,s) &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{&#92;(&#92;mathit{dupl}&#92;)} &#92;} &#92;&#92; &amp; (g&#92;,s, &#92;lambda v &#92;rightarrow (v, p&#92;,s)) = (g&#92;,s, &#92;ell &#92;cdot p&#92;,s) &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{first components are clearly equal} &#92;} &#92;&#92; &amp; &#92;lambda v &#92;rightarrow (v, p&#92;,s) = &#92;ell &#92;cdot p&#92;,s &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{apply to a &#92;(v&#92;)} &#92;} &#92;&#92; &amp; (v, p&#92;,s) = &#92;ell&#92;,(p&#92;,s&#92;,v) &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{&#92;(&#92;ell&#92;)} &#92;} &#92;&#92; &amp; (v, p&#92;,s) = (g&#92;,(p&#92;,s&#92;,v), p&#92;,(p&#92;,s&#92;,v)) &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{apply second components to a &#92;(u&#92;)} &#92;} &#92;&#92; &amp; (v, p&#92;,s&#92;,u) = (g&#92;,(p&#92;,s&#92;,v), p&#92;,(p&#92;,s&#92;,v)&#92;,u) &#92;&#92; &#92;Leftrightarrow &amp; &#92;qquad &#92;{ &#92;mbox{pair equality is pointwise} &#92;} &#92;&#92; &amp; v = g&#92;,(p&#92;,s&#92;,v) &#92;land p&#92;,s&#92;,u = p&#92;,(p&#92;,s&#92;,v)&#92;,u &#92;end{array} ' class='latex' />
</p></blockquote>
<p>
<br />  <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=patternsinfp.wordpress.com&#038;blog=15593982&#038;post=118&#038;subd=patternsinfp&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://patternsinfp.wordpress.com/2011/01/31/lenses-are-the-coalgebras-for-the-costate-comonad/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/39d7e0397cba13d1494d9a48c1f42167?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jeremygibbons</media:title>
		</media:content>
	</item>
		<item>
		<title>The stream monad</title>
		<link>http://patternsinfp.wordpress.com/2010/12/31/stream-monad/</link>
		<comments>http://patternsinfp.wordpress.com/2010/12/31/stream-monad/#comments</comments>
		<pubDate>Fri, 31 Dec 2010 10:00:25 +0000</pubDate>
		<dc:creator>jeremygibbons</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://patternsinfp.wordpress.com/?p=72</guid>
		<description><![CDATA[I read quite a nice problem on Nick Wu&#8217;s blog, which will serve as a fine warm-up exercise. It&#8217;s about the fact that streams (infinite lists) form a monad, in a different way from lists. Nick shows the &#8220;right&#8221; and &#8230; <a href="http://patternsinfp.wordpress.com/2010/12/31/stream-monad/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=patternsinfp.wordpress.com&#038;blog=15593982&#038;post=72&#038;subd=patternsinfp&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>
 I read quite a nice problem on <a href="http://zenzike.com/posts/2010-10-21-streams-and-monad-laws/">Nick Wu&#8217;s blog</a>, which will serve as a fine warm-up exercise. It&#8217;s about the fact that streams (infinite lists) form a monad, in a different way from lists. Nick shows the &#8220;right&#8221; and two &#8220;wrong&#8221; definitions of the join or bind operation, distinguishing them on the basis of the monad laws. But I think Nick&#8217;s proofs are more complicated than they need to be, because he hasn&#8217;t fully exploited the recursion patterns that underlie his definitions.</p>
<p>
This post will involve some language that we have not yet covered. Fear not! I hope it will be clear from context. But in case it isn&#8217;t, you might want to take a look at some of the background material (especially the paper <a href="http://www.comlab.ox.ac.uk/publications/publication2360-abstract.html">Calculating Functional Programs</a>).</p>
<p><h2> Streams </h2>
<p>
Like Nick, for simplicity we will take the datatype of streams to be a synonym for lists; in all that follows, assume that lists are properly infinite (not finite, or partial). </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathbf%7Btype%7D%5C%3B%5Cmathit%7BStream%7D%5C%2Ca+%3D+%5Ba%5D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathbf{type}&#92;;&#92;mathit{Stream}&#92;,a = [a] ' title='&#92;displaystyle  &#92;mathbf{type}&#92;;&#92;mathit{Stream}&#92;,a = [a] ' class='latex' />
</p></blockquote>
<p>
Streams are naturally a codatatype rather than a datatype: in the category of sets and total functions, they would be represented as a final coalgebra rather than an initial algebra. In Haskell, which is roughly based on the category of CPOs and continuous functions, initial algebras and final coalgebras coincide, so we need not (indeed, we cannot) make the distinction formally. But we can make it informally, by stipulating that the basic pattern of computation for streams is the <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bunfold%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{unfold}}' title='{&#92;mathit{unfold}}' class='latex' />: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Bl%7D+%5Cmathit%7Bunfold%7D+%3A%3A+%28b+%5Crightarrow+%28a%2Cb%29%29+%5Crightarrow+b+%5Crightarrow+%5Cmathit%7BStream%7D%5C%2Ca+%5C%5C+%5Cmathit%7Bunfold%7D%5C%2Cf%5C%2Cb+%3D+a+%3A+%5Cmathit%7Bunfold%7D%5C%2Cf%5C%2Cb%27+%5Cqquad%5Cmathbf%7Bwhere%7D%5Cqquad+%28a%2Cb%27%29+%3D+f%5C%2Cb+%5Cend%7Barray%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{l} &#92;mathit{unfold} :: (b &#92;rightarrow (a,b)) &#92;rightarrow b &#92;rightarrow &#92;mathit{Stream}&#92;,a &#92;&#92; &#92;mathit{unfold}&#92;,f&#92;,b = a : &#92;mathit{unfold}&#92;,f&#92;,b&#039; &#92;qquad&#92;mathbf{where}&#92;qquad (a,b&#039;) = f&#92;,b &#92;end{array}' title='&#92;displaystyle  &#92;begin{array}{l} &#92;mathit{unfold} :: (b &#92;rightarrow (a,b)) &#92;rightarrow b &#92;rightarrow &#92;mathit{Stream}&#92;,a &#92;&#92; &#92;mathit{unfold}&#92;,f&#92;,b = a : &#92;mathit{unfold}&#92;,f&#92;,b&#039; &#92;qquad&#92;mathbf{where}&#92;qquad (a,b&#039;) = f&#92;,b &#92;end{array}' class='latex' />
</p></blockquote>
<p>
<img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bunfold%7D%5C%2Cf%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{unfold}&#92;,f}' title='{&#92;mathit{unfold}&#92;,f}' class='latex' /> generates a stream from a seed, using the body <img src='http://s0.wp.com/latex.php?latex=%7Bf%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{f}' title='{f}' class='latex' /> that transforms a seed <img src='http://s0.wp.com/latex.php?latex=%7Bb%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{b}' title='{b}' class='latex' /> into an element <img src='http://s0.wp.com/latex.php?latex=%7Ba%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{a}' title='{a}' class='latex' /> and a new seed <img src='http://s0.wp.com/latex.php?latex=%7Bb%27%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{b&#039;}' title='{b&#039;}' class='latex' />. For example, the map function for streams uses the input stream as the seed, repeatedly splitting it into its head and tail: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Bl%7D+%5Cmathit%7BmapS%7D+%3A%3A+%28a+%5Crightarrow+b%29+%5Crightarrow+%5Cmathit%7BStream%7D%5C%2Ca+%5Crightarrow+%5Cmathit%7BStream%7D%5C%2Cb+%5C%5C+%5Cmathit%7BmapS%7D%5C%2Cf+%3D+%5Cmathit%7Bunfold%7D%5C%2C%28%5Cmathit%7Bfork%7D%5C%2C%28f+%5Ccdot+%5Cmathit%7Bhead%7D%2C+%5Cmathit%7Btail%7D%29%29+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{l} &#92;mathit{mapS} :: (a &#92;rightarrow b) &#92;rightarrow &#92;mathit{Stream}&#92;,a &#92;rightarrow &#92;mathit{Stream}&#92;,b &#92;&#92; &#92;mathit{mapS}&#92;,f = &#92;mathit{unfold}&#92;,(&#92;mathit{fork}&#92;,(f &#92;cdot &#92;mathit{head}, &#92;mathit{tail})) &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{l} &#92;mathit{mapS} :: (a &#92;rightarrow b) &#92;rightarrow &#92;mathit{Stream}&#92;,a &#92;rightarrow &#92;mathit{Stream}&#92;,b &#92;&#92; &#92;mathit{mapS}&#92;,f = &#92;mathit{unfold}&#92;,(&#92;mathit{fork}&#92;,(f &#92;cdot &#92;mathit{head}, &#92;mathit{tail})) &#92;end{array} ' class='latex' />
</p></blockquote>
<p> where <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bfork%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{fork}}' title='{&#92;mathit{fork}}' class='latex' /> applies two functions to the same argument: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathit%7Bfork%7D%5C%2C%28f%2Cg%29%5C%2Ca+%3D+%28f%5C%2Ca%2C+g%5C%2Ca%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathit{fork}&#92;,(f,g)&#92;,a = (f&#92;,a, g&#92;,a) ' title='&#92;displaystyle  &#92;mathit{fork}&#92;,(f,g)&#92;,a = (f&#92;,a, g&#92;,a) ' class='latex' />
</p></blockquote>
<p>
The crucial property of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bunfold%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{unfold}}' title='{&#92;mathit{unfold}}' class='latex' /> is its <em>universal property</em>, which provides necessary and sufficient conditions for a computation to be expressible as an instance of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bunfold%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{unfold}}' title='{&#92;mathit{unfold}}' class='latex' />: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++h+%3D+%5Cmathit%7Bunfold%7D%5C%2Cf+%5CLeftrightarrow+%5Cmathit%7Bout%7D+%5Ccdot+h+%3D+%5Cmathit%7Bprod%7D%5C%2C%28%5Cmathit%7Bid%7D%2Ch%29+%5Ccdot+f+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  h = &#92;mathit{unfold}&#92;,f &#92;Leftrightarrow &#92;mathit{out} &#92;cdot h = &#92;mathit{prod}&#92;,(&#92;mathit{id},h) &#92;cdot f ' title='&#92;displaystyle  h = &#92;mathit{unfold}&#92;,f &#92;Leftrightarrow &#92;mathit{out} &#92;cdot h = &#92;mathit{prod}&#92;,(&#92;mathit{id},h) &#92;cdot f ' class='latex' />
</p></blockquote>
<p> where <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bout%7D+%3D+%5Cmathit%7Bfork%7D%5C%2C%28%5Cmathit%7Bhead%7D%2Ctail%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{out} = &#92;mathit{fork}&#92;,(&#92;mathit{head},tail)}' title='{&#92;mathit{out} = &#92;mathit{fork}&#92;,(&#92;mathit{head},tail)}' class='latex' /> deconstructs a stream into its head and tail, and </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathit%7Bprod%7D%5C%2C%28f%2Cg%29%5C%2C%28a%2Cb%29+%3D+%28f%5C%2Ca%2C+g%5C%2Cb%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathit{prod}&#92;,(f,g)&#92;,(a,b) = (f&#92;,a, g&#92;,b) ' title='&#92;displaystyle  &#92;mathit{prod}&#92;,(f,g)&#92;,(a,b) = (f&#92;,a, g&#92;,b) ' class='latex' />
</p></blockquote>
<p>
From the universal property, one can easily (exercise!) prove three simple consequences (we&#8217;ll call them the &#8220;identity&#8221; and two &#8220;evaluation&#8221; rules): </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Bl%7D+%5Cmathit%7Bunfold%7D%5C%2C%5Cmathit%7Bout%7D+%3D+%5Cmathit%7Bid%7D+%5C%5C+%5Cmathit%7Bhead%7D+%5Ccdot+%5Cmathit%7Bunfold%7D%5C%2C%28%5Cmathit%7Bfork%7D%5C%2C%28f%2Cg%29%29+%3D+f+%5C%5C+%5Cmathit%7Btail%7D+%5Ccdot+%5Cmathit%7Bunfold%7D%5C%2C%28%5Cmathit%7Bfork%7D%5C%2C%28f%2Cg%29%29+%3D+%5Cmathit%7Bunfold%7D%5C%2C%28%5Cmathit%7Bfork%7D%5C%2C%28f%2Cg%29%29+%5Ccdot+g+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{l} &#92;mathit{unfold}&#92;,&#92;mathit{out} = &#92;mathit{id} &#92;&#92; &#92;mathit{head} &#92;cdot &#92;mathit{unfold}&#92;,(&#92;mathit{fork}&#92;,(f,g)) = f &#92;&#92; &#92;mathit{tail} &#92;cdot &#92;mathit{unfold}&#92;,(&#92;mathit{fork}&#92;,(f,g)) = &#92;mathit{unfold}&#92;,(&#92;mathit{fork}&#92;,(f,g)) &#92;cdot g &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{l} &#92;mathit{unfold}&#92;,&#92;mathit{out} = &#92;mathit{id} &#92;&#92; &#92;mathit{head} &#92;cdot &#92;mathit{unfold}&#92;,(&#92;mathit{fork}&#92;,(f,g)) = f &#92;&#92; &#92;mathit{tail} &#92;cdot &#92;mathit{unfold}&#92;,(&#92;mathit{fork}&#92;,(f,g)) = &#92;mathit{unfold}&#92;,(&#92;mathit{fork}&#92;,(f,g)) &#92;cdot g &#92;end{array} ' class='latex' />
</p></blockquote>
<p> and the very important <em>fusion law</em>: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathit%7Bunfold%7D%5C%2Cf+%5Ccdot+h+%3D+%5Cmathit%7Bunfold%7D%5C%2Cg+%5CLeftarrow+f+%5Ccdot+h+%3D+%5Cmathit%7Bprod%7D%5C%2C%28%5Cmathit%7Bid%7D%2Ch%29+%5Ccdot+g&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathit{unfold}&#92;,f &#92;cdot h = &#92;mathit{unfold}&#92;,g &#92;Leftarrow f &#92;cdot h = &#92;mathit{prod}&#92;,(&#92;mathit{id},h) &#92;cdot g' title='&#92;displaystyle  &#92;mathit{unfold}&#92;,f &#92;cdot h = &#92;mathit{unfold}&#92;,g &#92;Leftarrow f &#92;cdot h = &#92;mathit{prod}&#92;,(&#92;mathit{id},h) &#92;cdot g' class='latex' />
</p></blockquote>
<p> allowing a preceding function <img src='http://s0.wp.com/latex.php?latex=%7Bh%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h}' title='{h}' class='latex' /> to be absorbed into the unfold.</p>
<p><h2> Streams as a monad </h2>
<p>
Making streams a monad amounts to defining functions </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Cmathit%7Breturn%7D+%26%3A%3A%26+a+%5Crightarrow+%5Cmathit%7BStream%7D%5C%2Ca+%5C%5C+%5Cmathit%7Bjoin%7D+%26%3A%3A%26+%5Cmathit%7BStream%7D%5C%2C%28%5Cmathit%7BStream%7D%5C%2Ca%29+%5Crightarrow+%5Cmathit%7BStream%7D%5C%2Ca+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{return} &amp;::&amp; a &#92;rightarrow &#92;mathit{Stream}&#92;,a &#92;&#92; &#92;mathit{join} &amp;::&amp; &#92;mathit{Stream}&#92;,(&#92;mathit{Stream}&#92;,a) &#92;rightarrow &#92;mathit{Stream}&#92;,a &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{return} &amp;::&amp; a &#92;rightarrow &#92;mathit{Stream}&#92;,a &#92;&#92; &#92;mathit{join} &amp;::&amp; &#92;mathit{Stream}&#92;,(&#92;mathit{Stream}&#92;,a) &#92;rightarrow &#92;mathit{Stream}&#92;,a &#92;end{array} ' class='latex' />
</p></blockquote>
<p> satisfying the monad laws: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Cmathit%7Bjoin%7D+%5Ccdot+%5Cmathit%7Breturn%7D+%26%3D%26+%5Cmathit%7Bid%7D+%5C%5C+%5Cmathit%7Bjoin%7D+%5Ccdot+%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Breturn%7D+%26%3D%26+%5Cmathit%7Bid%7D+%5C%5C+%5Cmathit%7Bjoin%7D+%5Ccdot+%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Bjoin%7D+%26%3D%26+%5Cmathit%7Bjoin%7D+%5Ccdot+%5Cmathit%7Bjoin%7D+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{join} &#92;cdot &#92;mathit{return} &amp;=&amp; &#92;mathit{id} &#92;&#92; &#92;mathit{join} &#92;cdot &#92;mathit{mapS}&#92;,&#92;mathit{return} &amp;=&amp; &#92;mathit{id} &#92;&#92; &#92;mathit{join} &#92;cdot &#92;mathit{mapS}&#92;,&#92;mathit{join} &amp;=&amp; &#92;mathit{join} &#92;cdot &#92;mathit{join} &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{join} &#92;cdot &#92;mathit{return} &amp;=&amp; &#92;mathit{id} &#92;&#92; &#92;mathit{join} &#92;cdot &#92;mathit{mapS}&#92;,&#92;mathit{return} &amp;=&amp; &#92;mathit{id} &#92;&#92; &#92;mathit{join} &#92;cdot &#92;mathit{mapS}&#92;,&#92;mathit{join} &amp;=&amp; &#92;mathit{join} &#92;cdot &#92;mathit{join} &#92;end{array} ' class='latex' />
</p></blockquote>
<p> Looking at the type, the obvious (indeed, I think the only possible) definition one can give for <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Breturn%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{return}}' title='{&#92;mathit{return}}' class='latex' /> is <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Breturn%7D+%3D+%5Cmathit%7Brepeat%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{return} = &#92;mathit{repeat}}' title='{&#92;mathit{return} = &#92;mathit{repeat}}' class='latex' /> where </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathit%7Brepeat%7D+%3D+%5Cmathit%7Bunfold%7D%5C%2C%5Cmathit%7Bdouble%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathit{repeat} = &#92;mathit{unfold}&#92;,&#92;mathit{double} ' title='&#92;displaystyle  &#92;mathit{repeat} = &#92;mathit{unfold}&#92;,&#92;mathit{double} ' class='latex' />
</p></blockquote>
<p> and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bdouble%7D+%3D+%5Cmathit%7Bfork%7D%5C%2C%28%5Cmathit%7Bid%7D%2C%5Cmathit%7Bid%7D%29%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{double} = &#92;mathit{fork}&#92;,(&#92;mathit{id},&#92;mathit{id})}' title='{&#92;mathit{double} = &#92;mathit{fork}&#92;,(&#92;mathit{id},&#92;mathit{id})}' class='latex' /> makes two copies of its argument. However, there are many type-correct definitions one could give for <img src='http://s0.wp.com/latex.php?latex=%7B+%5Cmathit%7Bjoin%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{ &#92;mathit{join}}' title='{ &#92;mathit{join}}' class='latex' />, including <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bhead%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{head}}' title='{&#92;mathit{head}}' class='latex' />, <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Bhead%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{mapS}&#92;,&#92;mathit{head}}' title='{&#92;mathit{mapS}&#92;,&#92;mathit{head}}' class='latex' />, and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bdiag%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{diag}}' title='{&#92;mathit{diag}}' class='latex' />, where </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathit%7Bdiag%7D+%3D+%5Cmathit%7Bunfold%7D%5C%2C%5Cmathit%7Bhhtt%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathit{diag} = &#92;mathit{unfold}&#92;,&#92;mathit{hhtt} ' title='&#92;displaystyle  &#92;mathit{diag} = &#92;mathit{unfold}&#92;,&#92;mathit{hhtt} ' class='latex' />
</p></blockquote>
<p> and where (for brevity in what follows) we define </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+%5Cmathit%7Bhhtt%7D+%26%3D%26+%5Cmathit%7Bfork%7D%5C%2C%28%5Cmathit%7Bhh%7D%2C%5Cmathit%7Btt%7D%29+%5C%5C+%5Cmathit%7Bhh%7D+%26%3D%26+%5Cmathit%7Bhead%7D%5Ccdot%5Cmathit%7Bhead%7D+%5C%5C+%5Cmathit%7Btt%7D+%26%3D%26+%5Cmathit%7Btail%7D%5Ccdot%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Btail%7D+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{hhtt} &amp;=&amp; &#92;mathit{fork}&#92;,(&#92;mathit{hh},&#92;mathit{tt}) &#92;&#92; &#92;mathit{hh} &amp;=&amp; &#92;mathit{head}&#92;cdot&#92;mathit{head} &#92;&#92; &#92;mathit{tt} &amp;=&amp; &#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{tail} &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} &#92;mathit{hhtt} &amp;=&amp; &#92;mathit{fork}&#92;,(&#92;mathit{hh},&#92;mathit{tt}) &#92;&#92; &#92;mathit{hh} &amp;=&amp; &#92;mathit{head}&#92;cdot&#92;mathit{head} &#92;&#92; &#92;mathit{tt} &amp;=&amp; &#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{tail} &#92;end{array} ' class='latex' />
</p></blockquote>
<p> Obviously, <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bhead%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{head}}' title='{&#92;mathit{head}}' class='latex' /> yields the first &#8220;row&#8221; of a stream of streams (if one considers it in row-major order), and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Bhead%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{mapS}&#92;,&#92;mathit{head}}' title='{&#92;mathit{mapS}&#92;,&#92;mathit{head}}' class='latex' /> yields the first column; as the name suggests, <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bdiag%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{diag}}' title='{&#92;mathit{diag}}' class='latex' /> yields the leading diagonal. Nick&#8217;s post demonstrates that the first two, although type-correct, do not satisfy the monad laws. He also provides a proof that the third does, which we turn to next.</p>
<p><h2> Checking the monad laws </h2>
<p>
The proofs that <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Brepeat%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{repeat}}' title='{&#92;mathit{repeat}}' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bdiag%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{diag}}' title='{&#92;mathit{diag}}' class='latex' /> satisfy the three monad laws are very straightforward, using the universal property of <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bunfold%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{unfold}}' title='{&#92;mathit{unfold}}' class='latex' /> and its consequences.</p>
<p>
For the first monad law, fusion gives us the condition to check: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Bll%7D+%26+%5Cmathit%7Bdiag%7D%5Ccdot%5Cmathit%7Brepeat%7D+%3D+%5Cmathit%7Bid%7D+%5C%5C+%5CLeftarrow+%26+%5C%5C+%26+%5Cmathit%7Bhhtt%7D%5Ccdot%5Cmathit%7Brepeat%7D+%3D+%5Cmathit%7Bprod%7D%5C%2C%28%5Cmathit%7Bid%7D%2C%5Cmathit%7Brepeat%7D%29%5Ccdot%5Cmathit%7Bfork%7D%5C%2C%28%5Cmathit%7Bhead%7D%2C%5Cmathit%7Btail%7D%29+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;mathit{diag}&#92;cdot&#92;mathit{repeat} = &#92;mathit{id} &#92;&#92; &#92;Leftarrow &amp; &#92;&#92; &amp; &#92;mathit{hhtt}&#92;cdot&#92;mathit{repeat} = &#92;mathit{prod}&#92;,(&#92;mathit{id},&#92;mathit{repeat})&#92;cdot&#92;mathit{fork}&#92;,(&#92;mathit{head},&#92;mathit{tail}) &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;mathit{diag}&#92;cdot&#92;mathit{repeat} = &#92;mathit{id} &#92;&#92; &#92;Leftarrow &amp; &#92;&#92; &amp; &#92;mathit{hhtt}&#92;cdot&#92;mathit{repeat} = &#92;mathit{prod}&#92;,(&#92;mathit{id},&#92;mathit{repeat})&#92;cdot&#92;mathit{fork}&#92;,(&#92;mathit{head},&#92;mathit{tail}) &#92;end{array} ' class='latex' />
</p></blockquote>
<p> Working on the right-hand side, we have: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Bll%7D+%26+%5Cmathit%7Bhhtt%7D%5Ccdot%5Cmathit%7Brepeat%7D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bdefinition%7D+%5C%7D+%5C%5C+%26+%5Cmathit%7Bfork%7D%5C%2C%28%5Cmathit%7Bhh%7D%2C%5Cmathit%7Btt%7D%29%5Ccdot%5Cmathit%7Brepeat%7D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bcomposition+distributes+backwards+over+fork%7D+%5C%7D+%5C%5C+%26+%5Cmathit%7Bfork%7D%5C%2C%28%5Cmathit%7Bhh%7D%5Ccdot%5Cmathit%7Brepeat%7D%2C%5Cmathit%7Btt%7D%5Ccdot%5Cmathit%7Brepeat%7D%29+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bdefinitions%7D+%5C%7D+%5C%5C+%26+%5Cmathit%7Bfork%7D%5C%2C%28%5Cmathit%7Bhead%7D%5Ccdot%5Cmathit%7Bhead%7D%5Ccdot%5Cmathit%7Brepeat%7D%2C%5Cmathit%7Btail%7D%5Ccdot%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Btail%7D%5Ccdot%5Cmathit%7Brepeat%7D%29+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bevaluation+for+%5C%28%5Cmathit%7Brepeat%7D%5C%29%7D+%5C%7D+%5C%5C+%26+%5Cmathit%7Bfork%7D%5C%2C%28%5Cmathit%7Bhead%7D%2C%5Cmathit%7Btail%7D%5Ccdot%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Btail%7D%5Ccdot%5Cmathit%7Brepeat%7D%29+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bnaturality%3B+evaluation%7D+%5C%7D+%5C%5C+%26+%5Cmathit%7Bfork%7D%5C%2C%28%5Cmathit%7Bhead%7D%2C%5Cmathit%7Brepeat%7D%5Ccdot%5Cmathit%7Btail%7D%29+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bpairs%7D+%5C%7D+%5C%5C+%26+%5Cmathit%7Bprod%7D%5C%2C%28%5Cmathit%7Bid%7D%2C%5Cmathit%7Brepeat%7D%29+%5Ccdot+%5Cmathit%7Bfork%7D%5C%2C%28%5Cmathit%7Bhead%7D%2C%5Cmathit%7Btail%7D%29+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;mathit{hhtt}&#92;cdot&#92;mathit{repeat} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{definition} &#92;} &#92;&#92; &amp; &#92;mathit{fork}&#92;,(&#92;mathit{hh},&#92;mathit{tt})&#92;cdot&#92;mathit{repeat} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{composition distributes backwards over fork} &#92;} &#92;&#92; &amp; &#92;mathit{fork}&#92;,(&#92;mathit{hh}&#92;cdot&#92;mathit{repeat},&#92;mathit{tt}&#92;cdot&#92;mathit{repeat}) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{definitions} &#92;} &#92;&#92; &amp; &#92;mathit{fork}&#92;,(&#92;mathit{head}&#92;cdot&#92;mathit{head}&#92;cdot&#92;mathit{repeat},&#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{tail}&#92;cdot&#92;mathit{repeat}) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{evaluation for &#92;(&#92;mathit{repeat}&#92;)} &#92;} &#92;&#92; &amp; &#92;mathit{fork}&#92;,(&#92;mathit{head},&#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{tail}&#92;cdot&#92;mathit{repeat}) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{naturality; evaluation} &#92;} &#92;&#92; &amp; &#92;mathit{fork}&#92;,(&#92;mathit{head},&#92;mathit{repeat}&#92;cdot&#92;mathit{tail}) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{pairs} &#92;} &#92;&#92; &amp; &#92;mathit{prod}&#92;,(&#92;mathit{id},&#92;mathit{repeat}) &#92;cdot &#92;mathit{fork}&#92;,(&#92;mathit{head},&#92;mathit{tail}) &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;mathit{hhtt}&#92;cdot&#92;mathit{repeat} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{definition} &#92;} &#92;&#92; &amp; &#92;mathit{fork}&#92;,(&#92;mathit{hh},&#92;mathit{tt})&#92;cdot&#92;mathit{repeat} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{composition distributes backwards over fork} &#92;} &#92;&#92; &amp; &#92;mathit{fork}&#92;,(&#92;mathit{hh}&#92;cdot&#92;mathit{repeat},&#92;mathit{tt}&#92;cdot&#92;mathit{repeat}) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{definitions} &#92;} &#92;&#92; &amp; &#92;mathit{fork}&#92;,(&#92;mathit{head}&#92;cdot&#92;mathit{head}&#92;cdot&#92;mathit{repeat},&#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{tail}&#92;cdot&#92;mathit{repeat}) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{evaluation for &#92;(&#92;mathit{repeat}&#92;)} &#92;} &#92;&#92; &amp; &#92;mathit{fork}&#92;,(&#92;mathit{head},&#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{tail}&#92;cdot&#92;mathit{repeat}) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{naturality; evaluation} &#92;} &#92;&#92; &amp; &#92;mathit{fork}&#92;,(&#92;mathit{head},&#92;mathit{repeat}&#92;cdot&#92;mathit{tail}) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{pairs} &#92;} &#92;&#92; &amp; &#92;mathit{prod}&#92;,(&#92;mathit{id},&#92;mathit{repeat}) &#92;cdot &#92;mathit{fork}&#92;,(&#92;mathit{head},&#92;mathit{tail}) &#92;end{array} ' class='latex' />
</p></blockquote>
<p> discharging the proof obligation.</p>
<p>
Similarly, for the second monad law, fusion gives us the condition: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Bll%7D+%26+%5Cmathit%7Bdiag%7D%5Ccdot%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Brepeat%7D+%3D+%5Cmathit%7Bid%7D+%5C%5C+%5CLeftarrow+%26+%5C%5C+%26+%5Cmathit%7Bhhtt%7D%5Ccdot%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Brepeat%7D+%3D+%5Cmathit%7Bprod%7D%5C%2C%28%5Cmathit%7Bid%7D%2C%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Brepeat%7D%29%5Ccdot%5Cmathit%7Bfork%7D%5C%2C%28%5Cmathit%7Bhead%7D%2C%5Cmathit%7Btail%7D%29+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;mathit{diag}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{repeat} = &#92;mathit{id} &#92;&#92; &#92;Leftarrow &amp; &#92;&#92; &amp; &#92;mathit{hhtt}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{repeat} = &#92;mathit{prod}&#92;,(&#92;mathit{id},&#92;mathit{mapS}&#92;,&#92;mathit{repeat})&#92;cdot&#92;mathit{fork}&#92;,(&#92;mathit{head},&#92;mathit{tail}) &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;mathit{diag}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{repeat} = &#92;mathit{id} &#92;&#92; &#92;Leftarrow &amp; &#92;&#92; &amp; &#92;mathit{hhtt}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{repeat} = &#92;mathit{prod}&#92;,(&#92;mathit{id},&#92;mathit{mapS}&#92;,&#92;mathit{repeat})&#92;cdot&#92;mathit{fork}&#92;,(&#92;mathit{head},&#92;mathit{tail}) &#92;end{array} ' class='latex' />
</p></blockquote>
<p> and working on the right-hand side, in almost exactly the same steps we get: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Bll%7D+%26+%5Cmathit%7Bhhtt%7D%5Ccdot%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Brepeat%7D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bdefinition%7D+%5C%7D+%5C%5C+%26+%5Cmathit%7Bfork%7D%5C%2C%28%5Cmathit%7Bhh%7D%2C%5Cmathit%7Btt%7D%29%5Ccdot%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Brepeat%7D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bcomposition+distributes+backwards+over+fork%7D+%5C%7D+%5C%5C+%26+%5Cmathit%7Bfork%7D%5C%2C%28%5Cmathit%7Bhh%7D%5Ccdot%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Brepeat%7D%2C%5Cmathit%7Btt%7D%5Ccdot%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Brepeat%7D%29+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bdefinitions%7D+%5C%7D+%5C%5C+%26+%5Cmathit%7Bfork%7D%5C%2C%28%5Cmathit%7Bhead%7D%5Ccdot%5Cmathit%7Bhead%7D%5Ccdot%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Brepeat%7D%2C%5Cmathit%7Btail%7D%5Ccdot%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Btail%7D%5Ccdot%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Brepeat%7D%29+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bnaturality%3B+evaluation%7D+%5C%7D+%5C%5C+%26+%5Cmathit%7Bfork%7D%5C%2C%28%5Cmathit%7Bhead%7D%2C%5Cmathit%7Btail%7D%5Ccdot%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Btail%7D%5Ccdot%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Brepeat%7D%29+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bnaturality%3B+functors%3B+evaluation%7D+%5C%7D+%5C%5C+%26+%5Cmathit%7Bfork%7D%5C%2C%28%5Cmathit%7Bhead%7D%2C%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Brepeat%7D%5Ccdot%5Cmathit%7Btail%7D%29+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bpairs%7D+%5C%7D+%5C%5C+%26+%5Cmathit%7Bprod%7D%5C%2C%28%5Cmathit%7Bid%7D%2C%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Brepeat%7D%29+%5Ccdot+%5Cmathit%7Bfork%7D%5C%2C%28%5Cmathit%7Bhead%7D%2C%5Cmathit%7Btail%7D%29+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;mathit{hhtt}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{repeat} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{definition} &#92;} &#92;&#92; &amp; &#92;mathit{fork}&#92;,(&#92;mathit{hh},&#92;mathit{tt})&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{repeat} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{composition distributes backwards over fork} &#92;} &#92;&#92; &amp; &#92;mathit{fork}&#92;,(&#92;mathit{hh}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{repeat},&#92;mathit{tt}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{repeat}) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{definitions} &#92;} &#92;&#92; &amp; &#92;mathit{fork}&#92;,(&#92;mathit{head}&#92;cdot&#92;mathit{head}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{repeat},&#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{repeat}) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{naturality; evaluation} &#92;} &#92;&#92; &amp; &#92;mathit{fork}&#92;,(&#92;mathit{head},&#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{repeat}) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{naturality; functors; evaluation} &#92;} &#92;&#92; &amp; &#92;mathit{fork}&#92;,(&#92;mathit{head},&#92;mathit{mapS}&#92;,&#92;mathit{repeat}&#92;cdot&#92;mathit{tail}) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{pairs} &#92;} &#92;&#92; &amp; &#92;mathit{prod}&#92;,(&#92;mathit{id},&#92;mathit{mapS}&#92;,&#92;mathit{repeat}) &#92;cdot &#92;mathit{fork}&#92;,(&#92;mathit{head},&#92;mathit{tail}) &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;mathit{hhtt}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{repeat} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{definition} &#92;} &#92;&#92; &amp; &#92;mathit{fork}&#92;,(&#92;mathit{hh},&#92;mathit{tt})&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{repeat} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{composition distributes backwards over fork} &#92;} &#92;&#92; &amp; &#92;mathit{fork}&#92;,(&#92;mathit{hh}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{repeat},&#92;mathit{tt}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{repeat}) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{definitions} &#92;} &#92;&#92; &amp; &#92;mathit{fork}&#92;,(&#92;mathit{head}&#92;cdot&#92;mathit{head}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{repeat},&#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{repeat}) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{naturality; evaluation} &#92;} &#92;&#92; &amp; &#92;mathit{fork}&#92;,(&#92;mathit{head},&#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{repeat}) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{naturality; functors; evaluation} &#92;} &#92;&#92; &amp; &#92;mathit{fork}&#92;,(&#92;mathit{head},&#92;mathit{mapS}&#92;,&#92;mathit{repeat}&#92;cdot&#92;mathit{tail}) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{pairs} &#92;} &#92;&#92; &amp; &#92;mathit{prod}&#92;,(&#92;mathit{id},&#92;mathit{mapS}&#92;,&#92;mathit{repeat}) &#92;cdot &#92;mathit{fork}&#92;,(&#92;mathit{head},&#92;mathit{tail}) &#92;end{array} ' class='latex' />
</p></blockquote>
<p> discharging the obligation.</p>
<p>
What about the third monad law? To apply the universal property (or fusion), we need one side to be expressed as an unfold; but neither side of the equation <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bdiag%7D%5Ccdot%5Cmathit%7Bdiag%7D+%3D+%5Cmathit%7Bdiag%7D%5Ccdot%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Bdiag%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{diag}&#92;cdot&#92;mathit{diag} = &#92;mathit{diag}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{diag}}' title='{&#92;mathit{diag}&#92;cdot&#92;mathit{diag} = &#92;mathit{diag}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{diag}}' class='latex' /> is in that form. No matter; let us hypothesize that one side&mdash;say, the left&mdash;can be expressed in the form <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bunfold%7D%5C%2Ch%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{unfold}&#92;,h}' title='{&#92;mathit{unfold}&#92;,h}' class='latex' /> for some <img src='http://s0.wp.com/latex.php?latex=%7Bh%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h}' title='{h}' class='latex' />, then calculate a suitable definition for <img src='http://s0.wp.com/latex.php?latex=%7Bh%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h}' title='{h}' class='latex' /> (if one exists). Assuming we succeed, then we can use fusion to check that the other side equals <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bunfold%7D%5C%2Ch%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{unfold}&#92;,h}' title='{&#92;mathit{unfold}&#92;,h}' class='latex' />. (This strategy doesn&#8217;t work if we can find no such <img src='http://s0.wp.com/latex.php?latex=%7Bh%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{h}' title='{h}' class='latex' />!)</p>
<p>
Again, fusion gives us </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathit%7Bdiag%7D%5Ccdot%5Cmathit%7Bdiag%7D+%3D+%5Cmathit%7Bunfold%7D%5C%2Ch+%5CLeftarrow+%5Cmathit%7Bhhtt%7D%5Ccdot%5Cmathit%7Bdiag%7D+%3D+%5Cmathit%7Bprod%7D%5C%2C%28%5Cmathit%7Bid%7D%2C%5Cmathit%7Bdiag%7D%29%5Ccdot+h+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathit{diag}&#92;cdot&#92;mathit{diag} = &#92;mathit{unfold}&#92;,h &#92;Leftarrow &#92;mathit{hhtt}&#92;cdot&#92;mathit{diag} = &#92;mathit{prod}&#92;,(&#92;mathit{id},&#92;mathit{diag})&#92;cdot h ' title='&#92;displaystyle  &#92;mathit{diag}&#92;cdot&#92;mathit{diag} = &#92;mathit{unfold}&#92;,h &#92;Leftarrow &#92;mathit{hhtt}&#92;cdot&#92;mathit{diag} = &#92;mathit{prod}&#92;,(&#92;mathit{id},&#92;mathit{diag})&#92;cdot h ' class='latex' />
</p></blockquote>
<p> so we calculate: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Bll%7D+%26+%5Cmathit%7Bhhtt%7D%5Ccdot%5Cmathit%7Bdiag%7D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bdefinition%3B+distribution%7D+%5C%7D+%5C%5C+%26+%5Cmathit%7Bfork%7D%5C%2C%28%5Cmathit%7Bhead%7D%5Ccdot%5Cmathit%7Bhead%7D%5Ccdot%5Cmathit%7Bdiag%7D%2C%5Cmathit%7Btail%7D%5Ccdot%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Btail%7D%5Ccdot%5Cmathit%7Bdiag%7D%29+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bevaluation+for+%5C%28%5Cmathit%7Bdiag%7D%5C%29%7D+%5C%7D+%5C%5C+%26+%5Cmathit%7Bfork%7D%5C%2C%28%5Cmathit%7Bhead%7D%5Ccdot%5Cmathit%7Bhead%7D%5Ccdot%5Cmathit%7Bhead%7D%2C%5Cmathit%7Btail%7D%5Ccdot%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Btail%7D%5Ccdot%5Cmathit%7Bdiag%7D%29+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bnaturality%3B+evaluation%7D+%5C%7D+%5C%5C+%26+%5Cmathit%7Bfork%7D%5C%2C%28%5Cmathit%7Bhead%7D%5Ccdot%5Cmathit%7Bhead%7D%5Ccdot%5Cmathit%7Bhead%7D%2C%5Cmathit%7Bdiag%7D%5Ccdot%5Cmathit%7Btail%7D%5Ccdot%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Btail%7D%5Ccdot%5Cmathit%7BmapS%7D%5C%2C%28%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Btail%7D%29%29+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bpairs%7D+%5C%7D+%5C%5C+%26+%5Cmathit%7Bprod%7D%5C%2C%28%5Cmathit%7Bid%7D%2C%5Cmathit%7Bdiag%7D%29%5Ccdot%5Cmathit%7Bfork%7D%5C%2C%28%5Cmathit%7Bhead%7D%5Ccdot%5Cmathit%7Bhead%7D%5Ccdot%5Cmathit%7Bhead%7D%2C%5Cmathit%7Btail%7D%5Ccdot%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Btail%7D%5Ccdot%5Cmathit%7BmapS%7D%5C%2C%28%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Btail%7D%29%29+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;mathit{hhtt}&#92;cdot&#92;mathit{diag} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{definition; distribution} &#92;} &#92;&#92; &amp; &#92;mathit{fork}&#92;,(&#92;mathit{head}&#92;cdot&#92;mathit{head}&#92;cdot&#92;mathit{diag},&#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{tail}&#92;cdot&#92;mathit{diag}) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{evaluation for &#92;(&#92;mathit{diag}&#92;)} &#92;} &#92;&#92; &amp; &#92;mathit{fork}&#92;,(&#92;mathit{head}&#92;cdot&#92;mathit{head}&#92;cdot&#92;mathit{head},&#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{tail}&#92;cdot&#92;mathit{diag}) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{naturality; evaluation} &#92;} &#92;&#92; &amp; &#92;mathit{fork}&#92;,(&#92;mathit{head}&#92;cdot&#92;mathit{head}&#92;cdot&#92;mathit{head},&#92;mathit{diag}&#92;cdot&#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,(&#92;mathit{mapS}&#92;,&#92;mathit{tail})) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{pairs} &#92;} &#92;&#92; &amp; &#92;mathit{prod}&#92;,(&#92;mathit{id},&#92;mathit{diag})&#92;cdot&#92;mathit{fork}&#92;,(&#92;mathit{head}&#92;cdot&#92;mathit{head}&#92;cdot&#92;mathit{head},&#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,(&#92;mathit{mapS}&#92;,&#92;mathit{tail})) &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;mathit{hhtt}&#92;cdot&#92;mathit{diag} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{definition; distribution} &#92;} &#92;&#92; &amp; &#92;mathit{fork}&#92;,(&#92;mathit{head}&#92;cdot&#92;mathit{head}&#92;cdot&#92;mathit{diag},&#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{tail}&#92;cdot&#92;mathit{diag}) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{evaluation for &#92;(&#92;mathit{diag}&#92;)} &#92;} &#92;&#92; &amp; &#92;mathit{fork}&#92;,(&#92;mathit{head}&#92;cdot&#92;mathit{head}&#92;cdot&#92;mathit{head},&#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{tail}&#92;cdot&#92;mathit{diag}) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{naturality; evaluation} &#92;} &#92;&#92; &amp; &#92;mathit{fork}&#92;,(&#92;mathit{head}&#92;cdot&#92;mathit{head}&#92;cdot&#92;mathit{head},&#92;mathit{diag}&#92;cdot&#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,(&#92;mathit{mapS}&#92;,&#92;mathit{tail})) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{pairs} &#92;} &#92;&#92; &amp; &#92;mathit{prod}&#92;,(&#92;mathit{id},&#92;mathit{diag})&#92;cdot&#92;mathit{fork}&#92;,(&#92;mathit{head}&#92;cdot&#92;mathit{head}&#92;cdot&#92;mathit{head},&#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,(&#92;mathit{mapS}&#92;,&#92;mathit{tail})) &#92;end{array} ' class='latex' />
</p></blockquote>
<p> Therefore, letting </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathit%7Bhhhttt%7D+%3D+%5Cmathit%7Bfork%7D%5C%2C%28%5Cmathit%7Bhead%7D%5Ccdot%5Cmathit%7Bhead%7D%5Ccdot%5Cmathit%7Bhead%7D%2C%5Cmathit%7Btail%7D%5Ccdot%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Btail%7D%5Ccdot%5Cmathit%7BmapS%7D%5C%2C%28%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Btail%7D%29%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathit{hhhttt} = &#92;mathit{fork}&#92;,(&#92;mathit{head}&#92;cdot&#92;mathit{head}&#92;cdot&#92;mathit{head},&#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,(&#92;mathit{mapS}&#92;,&#92;mathit{tail})) ' title='&#92;displaystyle  &#92;mathit{hhhttt} = &#92;mathit{fork}&#92;,(&#92;mathit{head}&#92;cdot&#92;mathit{head}&#92;cdot&#92;mathit{head},&#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,(&#92;mathit{mapS}&#92;,&#92;mathit{tail})) ' class='latex' />
</p></blockquote>
<p> we have concluded that </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cmathit%7Bdiag%7D%5Ccdot%5Cmathit%7Bdiag%7D+%3D+%5Cmathit%7Bunfold%7D%5C%2C%5Cmathit%7Bhhhttt%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;mathit{diag}&#92;cdot&#92;mathit{diag} = &#92;mathit{unfold}&#92;,&#92;mathit{hhhttt} ' title='&#92;displaystyle  &#92;mathit{diag}&#92;cdot&#92;mathit{diag} = &#92;mathit{unfold}&#92;,&#92;mathit{hhhttt} ' class='latex' />
</p></blockquote>
<p>
Now all we have to do is to check that the right-hand side of the third monad law also equals this; fusion gives us the condition </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Bll%7D+%26+%5Cmathit%7Bdiag%7D%5Ccdot%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Bdiag%7D+%3D+%5Cmathit%7Bunfold%7D%5C%2C%5Cmathit%7Bhhhttt%7D+%5C%5C+%5CLeftarrow+%26+%5C%5C+%26+%5Cmathit%7Bhhtt%7D%5Ccdot%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Bdiag%7D+%3D+%5Cmathit%7Bprod%7D%5C%2C%28%5Cmathit%7Bid%7D%2C%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Bdiag%7D%29%5Ccdot+%5Cmathit%7Bhhhttt%7D+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;mathit{diag}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{diag} = &#92;mathit{unfold}&#92;,&#92;mathit{hhhttt} &#92;&#92; &#92;Leftarrow &amp; &#92;&#92; &amp; &#92;mathit{hhtt}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{diag} = &#92;mathit{prod}&#92;,(&#92;mathit{id},&#92;mathit{mapS}&#92;,&#92;mathit{diag})&#92;cdot &#92;mathit{hhhttt} &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;mathit{diag}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{diag} = &#92;mathit{unfold}&#92;,&#92;mathit{hhhttt} &#92;&#92; &#92;Leftarrow &amp; &#92;&#92; &amp; &#92;mathit{hhtt}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{diag} = &#92;mathit{prod}&#92;,(&#92;mathit{id},&#92;mathit{mapS}&#92;,&#92;mathit{diag})&#92;cdot &#92;mathit{hhhttt} &#92;end{array} ' class='latex' />
</p></blockquote>
<p> and we calculate on the right-hand side: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Bll%7D+%26+%5Cmathit%7Bhhtt%7D%5Ccdot%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Bdiag%7D+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bdefinition%3B+distribution%7D+%5C%7D+%5C%5C+%26+%5Cmathit%7Bfork%7D%5C%2C%28%5Cmathit%7Bhead%7D%5Ccdot%5Cmathit%7Bhead%7D%5Ccdot%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Bdiag%7D%2C%5Cmathit%7Btail%7D%5Ccdot%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Btail%7D%5Ccdot%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Bdiag%7D%29+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bnaturality%3B+evaluation%7D+%5C%7D+%5C%5C+%26+%5Cmathit%7Bfork%7D%5C%2C%28%5Cmathit%7Bhead%7D%5Ccdot%5Cmathit%7Bhead%7D%5Ccdot%5Cmathit%7Bhead%7D%2C%5Cmathit%7Btail%7D%5Ccdot%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Btail%7D%5Ccdot%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Bdiag%7D%29+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bfunctors%3B+naturality%3B+evaluation%7D+%5C%7D+%5C%5C+%26+%5Cmathit%7Bfork%7D%5C%2C%28%5Cmathit%7Bhead%7D%5Ccdot%5Cmathit%7Bhead%7D%5Ccdot%5Cmathit%7Bhead%7D%2C%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Bdiag%7D%5Ccdot%5Cmathit%7Btail%7D%5Ccdot%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Btail%7D%5Ccdot%5Cmathit%7BmapS%7D%5C%2C%28%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Btail%7D%29%29+%5C%5C+%3D+%26+%5Cqquad+%5C%7B+%5Cmbox%7Bpairs%3B+definition%7D+%5C%7D+%5C%5C+%26+%5Cmathit%7Bprod%7D%5C%2C%28%5Cmathit%7Bid%7D%2C%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Bdiag%7D%29%5Ccdot%5Cmathit%7Bhhhttt%7D+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;mathit{hhtt}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{diag} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{definition; distribution} &#92;} &#92;&#92; &amp; &#92;mathit{fork}&#92;,(&#92;mathit{head}&#92;cdot&#92;mathit{head}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{diag},&#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{diag}) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{naturality; evaluation} &#92;} &#92;&#92; &amp; &#92;mathit{fork}&#92;,(&#92;mathit{head}&#92;cdot&#92;mathit{head}&#92;cdot&#92;mathit{head},&#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{diag}) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{functors; naturality; evaluation} &#92;} &#92;&#92; &amp; &#92;mathit{fork}&#92;,(&#92;mathit{head}&#92;cdot&#92;mathit{head}&#92;cdot&#92;mathit{head},&#92;mathit{mapS}&#92;,&#92;mathit{diag}&#92;cdot&#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,(&#92;mathit{mapS}&#92;,&#92;mathit{tail})) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{pairs; definition} &#92;} &#92;&#92; &amp; &#92;mathit{prod}&#92;,(&#92;mathit{id},&#92;mathit{mapS}&#92;,&#92;mathit{diag})&#92;cdot&#92;mathit{hhhttt} &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{ll} &amp; &#92;mathit{hhtt}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{diag} &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{definition; distribution} &#92;} &#92;&#92; &amp; &#92;mathit{fork}&#92;,(&#92;mathit{head}&#92;cdot&#92;mathit{head}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{diag},&#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{diag}) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{naturality; evaluation} &#92;} &#92;&#92; &amp; &#92;mathit{fork}&#92;,(&#92;mathit{head}&#92;cdot&#92;mathit{head}&#92;cdot&#92;mathit{head},&#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{diag}) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{functors; naturality; evaluation} &#92;} &#92;&#92; &amp; &#92;mathit{fork}&#92;,(&#92;mathit{head}&#92;cdot&#92;mathit{head}&#92;cdot&#92;mathit{head},&#92;mathit{mapS}&#92;,&#92;mathit{diag}&#92;cdot&#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{tail}&#92;cdot&#92;mathit{mapS}&#92;,(&#92;mathit{mapS}&#92;,&#92;mathit{tail})) &#92;&#92; = &amp; &#92;qquad &#92;{ &#92;mbox{pairs; definition} &#92;} &#92;&#92; &amp; &#92;mathit{prod}&#92;,(&#92;mathit{id},&#92;mathit{mapS}&#92;,&#92;mathit{diag})&#92;cdot&#92;mathit{hhhttt} &#92;end{array} ' class='latex' />
</p></blockquote>
<p> completing the proof.</p>
<p>
As you&#8217;ll see, the calculations are all quite short and simple, whereas in Nick&#8217;s formulation, they were rather hard work; I think that was (a) because he wasn&#8217;t exploiting the universal property, and (b) because he was working in terms of the &#8220;bind&#8221; rather than the &#8220;join&#8221; of the monad, which forced him into a more pointwise rather than pointfree style. Points are helpful when writing programs, but less so when reasoning about them.</p>
<p><h2> Deducing diag </h2>
<p>
Here&#8217;s another way of looking at the problem. Nick&#8217;s blog presented three plausible (that is, type-correct) definitions for the <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bjoin%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{join}}' title='{&#92;mathit{join}}' class='latex' /> operation. Two of these didn&#8217;t satisfy the necessary laws, so were evidently wrong. The third, <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Bdiag%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{diag}}' title='{&#92;mathit{diag}}' class='latex' />, does satisfy the laws, but is it the only possible definition that does? I believe that it is the only solution in the form of an unfold; but I only have a hand-waving argument as to why.</p>
<p>
Let us suppose that indeed </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++join+%3D+%5Cmathit%7Bunfold%7D%5C%2Ck+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  join = &#92;mathit{unfold}&#92;,k ' title='&#92;displaystyle  join = &#92;mathit{unfold}&#92;,k ' class='latex' />
</p></blockquote>
<p> for some <img src='http://s0.wp.com/latex.php?latex=%7Bk%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{k}' title='{k}' class='latex' />. Without loss of generality, let us suppose also that </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++k+%3D+%5Cmathit%7Bfork%7D%5C%2C%28k_1%2Ck_2%29+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  k = &#92;mathit{fork}&#92;,(k_1,k_2) ' title='&#92;displaystyle  k = &#92;mathit{fork}&#92;,(k_1,k_2) ' class='latex' />
</p></blockquote>
<p> with </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+k_1+%26%3A%3A%26+%5Cmathit%7BStream%7D%5C%2C%28%5Cmathit%7BStream%7D%5C%2Ca%29+%5Crightarrow+a+%5C%5C+k_2+%26%3A%3A%26+%5Cmathit%7BStream%7D%5C%2C%28%5Cmathit%7BStream%7D%5C%2Ca%29+%5Crightarrow+%5Cmathit%7BStream%7D%5C%2C%28%5Cmathit%7BStream%7D%5C%2Ca%29+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} k_1 &amp;::&amp; &#92;mathit{Stream}&#92;,(&#92;mathit{Stream}&#92;,a) &#92;rightarrow a &#92;&#92; k_2 &amp;::&amp; &#92;mathit{Stream}&#92;,(&#92;mathit{Stream}&#92;,a) &#92;rightarrow &#92;mathit{Stream}&#92;,(&#92;mathit{Stream}&#92;,a) &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} k_1 &amp;::&amp; &#92;mathit{Stream}&#92;,(&#92;mathit{Stream}&#92;,a) &#92;rightarrow a &#92;&#92; k_2 &amp;::&amp; &#92;mathit{Stream}&#92;,(&#92;mathit{Stream}&#92;,a) &#92;rightarrow &#92;mathit{Stream}&#92;,(&#92;mathit{Stream}&#92;,a) &#92;end{array} ' class='latex' />
</p></blockquote>
<p> I claimed above that <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Brepeat%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{repeat}}' title='{&#92;mathit{repeat}}' class='latex' /> is the only type-correct definition of the <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Breturn%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{return}}' title='{&#92;mathit{return}}' class='latex' /> operation. (Ignoring bottoms, that is. Which is to say, in Haskell, all type-correct definitions are approximations in the definedness ordering to <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Brepeat%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{repeat}}' title='{&#92;mathit{repeat}}' class='latex' />.)</p>
<p>
Consideration of just the first two monad laws gives us some constraints on <img src='http://s0.wp.com/latex.php?latex=%7Bk%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{k}' title='{k}' class='latex' />, since we know that <img src='http://s0.wp.com/latex.php?latex=%7B%5Cmathit%7Breturn%7D+%3D+%5Cmathit%7Brepeat%7D%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{&#92;mathit{return} = &#92;mathit{repeat}}' title='{&#92;mathit{return} = &#92;mathit{repeat}}' class='latex' />: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcl%7D+k%5Ccdot%5Cmathit%7Brepeat%7D+%26%3D%26+%5Cmathit%7Bprod%7D%5C%2C%28%5Cmathit%7Bid%7D%2C%5Cmathit%7Brepeat%7D%29%5Ccdot%5Cmathit%7Bfork%7D%5C%2C%28%5Cmathit%7Bhead%7D%2C%5Cmathit%7Btail%7D%29+%5C%5C+k%5Ccdot%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Brepeat%7D+%26%3D%26+%5Cmathit%7Bprod%7D%5C%2C%28%5Cmathit%7Bid%7D%2C%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Brepeat%7D%29%5Ccdot%5Cmathit%7Bfork%7D%5C%2C%28%5Cmathit%7Bhead%7D%2C%5Cmathit%7Btail%7D%29+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcl} k&#92;cdot&#92;mathit{repeat} &amp;=&amp; &#92;mathit{prod}&#92;,(&#92;mathit{id},&#92;mathit{repeat})&#92;cdot&#92;mathit{fork}&#92;,(&#92;mathit{head},&#92;mathit{tail}) &#92;&#92; k&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{repeat} &amp;=&amp; &#92;mathit{prod}&#92;,(&#92;mathit{id},&#92;mathit{mapS}&#92;,&#92;mathit{repeat})&#92;cdot&#92;mathit{fork}&#92;,(&#92;mathit{head},&#92;mathit{tail}) &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcl} k&#92;cdot&#92;mathit{repeat} &amp;=&amp; &#92;mathit{prod}&#92;,(&#92;mathit{id},&#92;mathit{repeat})&#92;cdot&#92;mathit{fork}&#92;,(&#92;mathit{head},&#92;mathit{tail}) &#92;&#92; k&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{repeat} &amp;=&amp; &#92;mathit{prod}&#92;,(&#92;mathit{id},&#92;mathit{mapS}&#92;,&#92;mathit{repeat})&#92;cdot&#92;mathit{fork}&#92;,(&#92;mathit{head},&#92;mathit{tail}) &#92;end{array} ' class='latex' />
</p></blockquote>
<p> Or in terms of <img src='http://s0.wp.com/latex.php?latex=%7Bk%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{k}' title='{k}' class='latex' />&#8216;s two components, </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++%5Cbegin%7Barray%7D%7Blcll%7D+k_1%5Ccdot%5Cmathit%7Brepeat%7D+%26%3D%26+%5Cmathit%7Bhead%7D+%26%281%29%5C%5C+k_2%5Ccdot%5Cmathit%7Brepeat%7D+%26%3D%26+%5Cmathit%7Brepeat%7D%5Ccdot%5Cmathit%7Btail%7D+%26%282%29%5C%5C+k_1%5Ccdot%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Brepeat%7D+%26%3D%26+%5Cmathit%7Bhead%7D+%26%283%29%5C%5C+k_2%5Ccdot%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Brepeat%7D+%26%3D%26+%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Brepeat%7D%5Ccdot%5Cmathit%7Btail%7D+%26%284%29+%5Cend%7Barray%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  &#92;begin{array}{lcll} k_1&#92;cdot&#92;mathit{repeat} &amp;=&amp; &#92;mathit{head} &amp;(1)&#92;&#92; k_2&#92;cdot&#92;mathit{repeat} &amp;=&amp; &#92;mathit{repeat}&#92;cdot&#92;mathit{tail} &amp;(2)&#92;&#92; k_1&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{repeat} &amp;=&amp; &#92;mathit{head} &amp;(3)&#92;&#92; k_2&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{repeat} &amp;=&amp; &#92;mathit{mapS}&#92;,&#92;mathit{repeat}&#92;cdot&#92;mathit{tail} &amp;(4) &#92;end{array} ' title='&#92;displaystyle  &#92;begin{array}{lcll} k_1&#92;cdot&#92;mathit{repeat} &amp;=&amp; &#92;mathit{head} &amp;(1)&#92;&#92; k_2&#92;cdot&#92;mathit{repeat} &amp;=&amp; &#92;mathit{repeat}&#92;cdot&#92;mathit{tail} &amp;(2)&#92;&#92; k_1&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{repeat} &amp;=&amp; &#92;mathit{head} &amp;(3)&#92;&#92; k_2&#92;cdot&#92;mathit{mapS}&#92;,&#92;mathit{repeat} &amp;=&amp; &#92;mathit{mapS}&#92;,&#92;mathit{repeat}&#92;cdot&#92;mathit{tail} &amp;(4) &#92;end{array} ' class='latex' />
</p></blockquote>
<p> I claim that (1) entails that <img src='http://s0.wp.com/latex.php?latex=%7Bk_1%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{k_1}' title='{k_1}' class='latex' /> picks some element out of the first &#8220;column&#8221; of a stream of streams (thinking of the input as an infinite matrix in row-major order again)&mdash;for the equation says that when the input consists of infinitely many copies of the same stream, <img src='http://s0.wp.com/latex.php?latex=%7Bk_1%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{k_1}' title='{k_1}' class='latex' /> picks (one of the many copies of) the head of that stream. Symmetrically, (3) entails that, when given a infinite matrix whose columns are all equal, <img src='http://s0.wp.com/latex.php?latex=%7Bk_1%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{k_1}' title='{k_1}' class='latex' /> picks some element out of the first &#8220;row&#8221;. And because <img src='http://s0.wp.com/latex.php?latex=%7Bk_1%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{k_1}' title='{k_1}' class='latex' /> has to be polymorphic, it cannot behave differently on special matrices like these than it does in general. Putting those statements together, and waving my hands in the air, I conclude that <img src='http://s0.wp.com/latex.php?latex=%7Bk_1%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{k_1}' title='{k_1}' class='latex' /> picks the only element that is in both the first row and the first column: </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++k_1+%3D+%5Cmathit%7Bhead%7D+%5Ccdot+%5Cmathit%7Bhead%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  k_1 = &#92;mathit{head} &#92;cdot &#92;mathit{head} ' title='&#92;displaystyle  k_1 = &#92;mathit{head} &#92;cdot &#92;mathit{head} ' class='latex' />
</p></blockquote>
<p>
Similarly, Equation (2) says that, given an infinite input matrix all of whose rows are equal, <img src='http://s0.wp.com/latex.php?latex=%7Bk_2%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{k_2}' title='{k_2}' class='latex' /> drops the first column (and possibly some of the rows are duplicated or dropped, and the order of the rows may change; but the elements of the rows are untouched). Symmetrically, (4) says that, given an input whose columns are all equal, <img src='http://s0.wp.com/latex.php?latex=%7Bk_2%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{k_2}' title='{k_2}' class='latex' /> drops the first row (and may duplicate, drop, or rearrange the columns, but not change any of them). And again, the behaviour in general must be consistent with these special cases. Putting these observations together, <img src='http://s0.wp.com/latex.php?latex=%7Bk_2%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{k_2}' title='{k_2}' class='latex' /> must drop the first row and the first column, and cannot change any of the remainder of the matrix. </p>
<blockquote><p>
<img src='http://s0.wp.com/latex.php?latex=%5Cdisplaystyle++k_2+%3D+%5Cmathit%7Btail%7D+%5Ccdot+%5Cmathit%7BmapS%7D%5C%2C%5Cmathit%7Btail%7D+&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='&#92;displaystyle  k_2 = &#92;mathit{tail} &#92;cdot &#92;mathit{mapS}&#92;,&#92;mathit{tail} ' title='&#92;displaystyle  k_2 = &#92;mathit{tail} &#92;cdot &#92;mathit{mapS}&#92;,&#92;mathit{tail} ' class='latex' />
</p></blockquote>
<p>
What is the right framework in which to present such arguments more formally? It feels rather like Paul Hoogendijk&#8217;s <a href="http://dx.doi.org/10.1017/S0956796899003640">relational approach to generic programming</a>, which has to talk about largest natural transformations of a given type: the relational setting provides the conjunction one needs in order to express the two separate constraints on <img src='http://s0.wp.com/latex.php?latex=%7Bk_1%7D&amp;bg=ffffff&amp;fg=000000&amp;s=0' alt='{k_1}' title='{k_1}' class='latex' />.</p>
<p>
<br />  <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=patternsinfp.wordpress.com&#038;blog=15593982&#038;post=72&#038;subd=patternsinfp&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://patternsinfp.wordpress.com/2010/12/31/stream-monad/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/39d7e0397cba13d1494d9a48c1f42167?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jeremygibbons</media:title>
		</media:content>
	</item>
	</channel>
</rss>
