<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.0.4" -->
<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/"
	>

<channel>
	<title>The Comonad.Reader</title>
	<link>http://comonad.com/reader</link>
	<description>types, (co)monads, substructural logic</description>
	<pubDate>Sat, 19 Jul 2008 18:37:31 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0.4</generator>
	<language>en</language>
			<item>
		<title>A Sort of Difference</title>
		<link>http://comonad.com/reader/2008/a-sort-of-difference/</link>
		<comments>http://comonad.com/reader/2008/a-sort-of-difference/#comments</comments>
		<pubDate>Wed, 16 Jul 2008 21:32:44 +0000</pubDate>
		<dc:creator>Edward Kmett</dc:creator>
		
	<category>Haskell</category>
	<category>Data Structures</category>
	<category>Algorithms</category>
		<guid isPermaLink="false">http://comonad.com/reader/2008/a-sort-of-difference/</guid>
		<description><![CDATA[[Edit: My apologies to mfp of eigenclass; my original analysis was flawed. I've restructured this to serve as an introduction to difference lists.]
Recently there was a post on eigenclass that was picked up by programming.reddit wherein the author performed an analysis of the classic Haskell quicksort example.
Bowing to Lennart's biases I'll admit the Haskell quicksort [...]]]></description>
			<content:encoded><![CDATA[<p>[Edit: My apologies to mfp of eigenclass; my original analysis was flawed. I've restructured this to serve as an introduction to difference lists.]</p>
<p>Recently there was a post on <a href="http://eigenclass.org/hiki/quicksort-erratum">eigenclass</a> that was picked up by <a href="http://www.reddit.com/r/programming/info/6ruap/comments/">programming.reddit</a> wherein the author performed an analysis of the classic Haskell quicksort example.</p>
<p>Bowing to <a href="http://augustss.blogspot.com/2007/08/quicksort-in-haskell-quicksort-is.html">Lennart</a>'s biases I'll admit the Haskell quicksort is not exactly the same thing and refer to it as "quicksort" in somewhat patronizing quotes hereafter.</p>
<pre class="haskell">&nbsp;
<span style="color: #06c; font-weight: bold;">import</span> Data.List <span style="color: green;">&#40;</span>partition<span style="color: green;">&#41;</span>
&nbsp;
qsort :: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Ord"><span style="background-color: #efefbf; font-weight: bold;">Ord</span></a> a =&gt; <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> -&gt; <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span>
qsort <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> = <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>
qsort <span style="color: green;">&#40;</span>a:<span style="color: #06c; font-weight: bold;">as</span><span style="color: green;">&#41;</span> = qsort ls ++ <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> ++ qsort rs <span style="color: #06c; font-weight: bold;">where</span>
    <span style="color: green;">&#40;</span>ls,rs<span style="color: green;">&#41;</span> = partition <span style="color: green;">&#40;</span>&lt; = a<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">as</span>
&nbsp;</pre>
<p>I've slightly modified the above to use partition rather than use filter twice, but its the same thing just with a single traversal -- unfortunately my blog inserts a space in the &lt;=.</p>
<p>Profiling the following will see <img src='http://comonad.com/latex/c5566036dd2bd924fef1c6263072eb45.png' title='$\mathcal{O}(n^2)$' alt='$\mathcal{O}(n^2)$' align=absmiddle> performance.</p>
</pre>
<pre class="haskell">&nbsp;
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse"><span style="font-weight: bold;">reverse</span></a> <span style="color: green;">&#40;</span>a:<span style="color: #06c; font-weight: bold;">as</span><span style="color: green;">&#41;</span> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse"><span style="font-weight: bold;">reverse</span></a> <span style="color: #06c; font-weight: bold;">as</span> ++ <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span>
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse"><span style="font-weight: bold;">reverse</span></a> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> = <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>
&nbsp;</pre>
<pre class="haskell">&nbsp;
T<span style="color: green;">&#40;</span><span style="color: red;">0</span><span style="color: green;">&#41;</span> = <span style="color: red;">1</span>
T<span style="color: green;">&#40;</span>n<span style="color: green;">&#41;</span> = T<span style="color: green;">&#40;</span>n - <span style="color: red;">1</span><span style="color: green;">&#41;</span> + n - <span style="color: red;">1</span> + <span style="color: red;">1</span> = T <span style="color: green;">&#40;</span>n - <span style="color: red;">1</span><span style="color: green;">&#41;</span> + n
T<span style="color: green;">&#40;</span>n<span style="color: green;">&#41;</span> = O<span style="color: green;">&#40;</span>n^<span style="color: red;">2</span><span style="color: green;">&#41;</span>
&nbsp;</pre>
<p>It is also fairly well known that you can convert the naive reverse to a tail recursive form and get a linear time list reversal.</p>
<pre class="haskell">&nbsp;
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse"><span style="font-weight: bold;">reverse</span></a> <span style="color: #06c; font-weight: bold;">as</span> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse"><span style="font-weight: bold;">reverse</span></a>' <span style="color: #06c; font-weight: bold;">as</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> <span style="color: #06c; font-weight: bold;">where</span>
    <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse"><span style="font-weight: bold;">reverse</span></a>' <span style="color: green;">&#40;</span>a:<span style="color: #06c; font-weight: bold;">as</span><span style="color: green;">&#41;</span> xs = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse"><span style="font-weight: bold;">reverse</span></a>' <span style="color: #06c; font-weight: bold;">as</span> <span style="color: green;">&#40;</span>a:xs<span style="color: green;">&#41;</span>
    <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse"><span style="font-weight: bold;">reverse</span></a>' <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> xs = xs
&nbsp;</pre>
<p>Here we can clearly see that we pattern match once and cons once per time through reverse'.</p>
<pre class="haskell">&nbsp;
T<span style="color: green;">&#40;</span><span style="color: red;">0</span><span style="color: green;">&#41;</span> = <span style="color: red;">2</span>
T<span style="color: green;">&#40;</span>n<span style="color: green;">&#41;</span> = T<span style="color: green;">&#40;</span>n - <span style="color: red;">1</span><span style="color: green;">&#41;</span> + <span style="color: red;">2</span>
T<span style="color: green;">&#40;</span>n<span style="color: green;">&#41;</span> = O<span style="color: green;">&#40;</span>n<span style="color: green;">&#41;</span>
&nbsp;</pre>
<p>One way to think about this implementation is to think about it in terms of a difference list. Don Stewart has <a href="http://hackage.haskell.org/cgi-bin/hackage-scripts/package/dlist">a nice library for difference lists</a>, which uses a <code>newtype</code> but I'll stick to a <code>type</code> here to avoid syntactic noise.</p>
<pre class="haskell">&nbsp;
<span style="color: #06c; font-weight: bold;">type</span> DList a = <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> -&gt; <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span>
&nbsp;</pre>
<p>To see that connection, lets rewrite this point-free and highlight the result type by applying the above type alias.</p>
<pre class="haskell">&nbsp;
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse"><span style="font-weight: bold;">reverse</span></a> <span style="color: #06c; font-weight: bold;">as</span> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse"><span style="font-weight: bold;">reverse</span></a>' <span style="color: #06c; font-weight: bold;">as</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> <span style="color: #06c; font-weight: bold;">where</span>
    <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse"><span style="font-weight: bold;">reverse</span></a>' :: <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> -&gt; <span style="color: green;">&#40;</span><span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> -&gt; <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span><span style="color: green;">&#41;</span>
    <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse"><span style="font-weight: bold;">reverse</span></a>' <span style="color: green;">&#40;</span>a:<span style="color: #06c; font-weight: bold;">as</span><span style="color: green;">&#41;</span> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse"><span style="font-weight: bold;">reverse</span></a>' <span style="color: #06c; font-weight: bold;">as</span> . <span style="color: green;">&#40;</span>a:<span style="color: green;">&#41;</span>
    <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse"><span style="font-weight: bold;">reverse</span></a>' <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:id"><span style="font-weight: bold;">id</span></a>
&nbsp;</pre>
<p>With the above we can see that:</p>
<pre class="haskell">&nbsp;
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse"><span style="font-weight: bold;">reverse</span></a>' :: <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> -&gt; DList a
&nbsp;</pre>
<p>Motivated by the above, we can see that a difference list is just a function that accepts a list which it will use <i>as its tail</i>. So a difference list is a slightly constrained type of function from [a] -> [a].</p>
<p>Now, if we look at the type of</p>
<pre class="haskell">&nbsp;
singleton :: a -&gt; DList a
singleton = <span style="color: green;">&#40;</span>:<span style="color: green;">&#41;</span>
&nbsp;</pre>
<p>we can see that (a:) is already difference list. However, [] is not.</p>
<p>We can represent the empty list as a difference list with:</p>
<pre class="haskell">&nbsp;
nil :: DList a
nil = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:id"><span style="font-weight: bold;">id</span></a>
&nbsp;</pre>
<p>Now in this representation we can append difference lists by just using (.):</p>
<pre class="haskell">&nbsp;
append :: DList a -&gt; DList a -&gt; DList a
append = <span style="color: green;">&#40;</span>.<span style="color: green;">&#41;</span>
&nbsp;</pre>
<p>and you can convert from a difference list to a list by just applying it to [].</p>
<p>So if these are so good, why don't we use them everywhere?</p>
<p>1. Well, the amortization schedule for the costs you incur while tearing down a difference list is different than for a traditional list, so you pay prices at different times.<br />
2. The type is 'too large' in that it doesn't adequately capture the constraint that the function must use the list its given and must append it to its output and can't use the list in any other ways.<br />
3. Difference lists do not generate thunks that remember their contents after being forced the first time. However, if all you are going to do when you are done is convert the overall result back to a normal list, then you can get that list to get the memoization-like thunking effect from the result list.</p>
<p>So with these in hand, we can easily see reverse' as taking a list and returning a difference list, and then we are just converting it back to a normal list using the fairly standard <a href="http://www.cs.nott.ac.uk/~gmh/wrapper.pdf">worker/wrapper</a> pattern.</p>
<pre class="haskell">&nbsp;
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse"><span style="font-weight: bold;">reverse</span></a> <span style="color: #06c; font-weight: bold;">as</span> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse"><span style="font-weight: bold;">reverse</span></a>' <span style="color: #06c; font-weight: bold;">as</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> <span style="color: #06c; font-weight: bold;">where</span>
    <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse"><span style="font-weight: bold;">reverse</span></a>' :: <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> -&gt; DList a
    <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse"><span style="font-weight: bold;">reverse</span></a>' <span style="color: green;">&#40;</span>a:<span style="color: #06c; font-weight: bold;">as</span><span style="color: green;">&#41;</span> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse"><span style="font-weight: bold;">reverse</span></a>' <span style="color: #06c; font-weight: bold;">as</span> `append` singleton a
    <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:reverse"><span style="font-weight: bold;">reverse</span></a>' <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> = nil
&nbsp;</pre>
<p>Ok. So recalling qsort,</p>
<pre class="haskell">&nbsp;
qsort <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> = <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>
qsort <span style="color: green;">&#40;</span>a:<span style="color: #06c; font-weight: bold;">as</span><span style="color: green;">&#41;</span> = qsort ls ++ <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> ++ qsort rs <span style="color: #06c; font-weight: bold;">where</span>
    <span style="color: green;">&#40;</span>ls,rs<span style="color: green;">&#41;</span> = partition <span style="color: green;">&#40;</span>&lt; =a<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">as</span>
&nbsp;</pre>
<p>Lets apply the same transformation that we applied to reverse to the well known quicksort example, transforming its output to a difference list.</p>
</pre>
<pre class="haskell">&nbsp;
qsort' :: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Ord"><span style="background-color: #efefbf; font-weight: bold;">Ord</span></a> a =&gt; <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> -&gt; DList a
qsort' <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> = nil
qsort' <span style="color: green;">&#40;</span>a:<span style="color: #06c; font-weight: bold;">as</span><span style="color: green;">&#41;</span> = qsort' ls `append` singleton a `append` qsort' rs <span style="color: #06c; font-weight: bold;">where</span>
    <span style="color: green;">&#40;</span>ls,rs<span style="color: green;">&#41;</span> = partition <span style="color: green;">&#40;</span>&lt; =a<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">as</span>
&nbsp;</pre>
<p>And after inlining the very succinct definitions and transforming the output back to a list we get a visibly similar worker/wrapper combo:</p>
</pre>
<pre class="haskell">&nbsp;
qsort :: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Ord"><span style="background-color: #efefbf; font-weight: bold;">Ord</span></a> a =&gt; <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> -&gt; <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span>
qsort <span style="color: #06c; font-weight: bold;">as</span> = qsort' <span style="color: #06c; font-weight: bold;">as</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> <span style="color: #06c; font-weight: bold;">where</span>
    qsort' <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:id"><span style="font-weight: bold;">id</span></a>
    qsort' <span style="color: green;">&#40;</span>a:<span style="color: #06c; font-weight: bold;">as</span><span style="color: green;">&#41;</span> = qsort' ls . <span style="color: green;">&#40;</span>a:<span style="color: green;">&#41;</span> . qsort' rs <span style="color: #06c; font-weight: bold;">where</span>
        <span style="color: green;">&#40;</span>ls,rs<span style="color: green;">&#41;</span> = partition <span style="color: green;">&#40;</span>&lt; =a<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">as</span>
&nbsp;</pre>
<p>However, we now get better performance.</p>
<p><img id="image78" alt=Performance src="http://comonad.com/reader/wp-content/uploads/2008/07/performance.png" /></p>
<p>Green is the time taken by the difference list version. Blue is the traditional Haskell qsort.</pre>
]]></content:encoded>
			<wfw:commentRSS>http://comonad.com/reader/2008/a-sort-of-difference/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>A Lively Haskell Café</title>
		<link>http://comonad.com/reader/2008/haskell-cafe/</link>
		<comments>http://comonad.com/reader/2008/haskell-cafe/#comments</comments>
		<pubDate>Wed, 09 Jul 2008 18:32:27 +0000</pubDate>
		<dc:creator>Edward Kmett</dc:creator>
		
	<category>Haskell</category>
	<category>Lively</category>
		<guid isPermaLink="false">http://comonad.com/reader/2008/haskell-cafe/</guid>
		<description><![CDATA[
[Link]
To embed the Haskell Café on a web page:

&#60;iframe src='http://embed.lively.com/iframe?rid=-4485567674160322075'
    width='460' height='400'
    marginwidth='0' marginheight='0'
    frameborder='0' scrolling='no'&#62;
&#60;/iframe&#62;

]]></description>
			<content:encoded><![CDATA[<p><iframe src='http://embed.lively.com/iframe?rid=-4485567674160322075' width='460' height='400' marginwidth='0' marginheight='0' frameborder='0' scrolling='no'></iframe><br />
[<a href="http://www.lively.com/dr?rid=-4485567674160322075">Link</a>]</p>
<p>To embed the Haskell Café on a web page:</p>
<pre>
&lt;iframe src='http://embed.lively.com/iframe?rid=-4485567674160322075'
    width='460' height='400'
    marginwidth='0' marginheight='0'
    frameborder='0' scrolling='no'&gt;
&lt;/iframe&gt;
</pre>
]]></content:encoded>
			<wfw:commentRSS>http://comonad.com/reader/2008/haskell-cafe/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Anamorphism</title>
		<link>http://comonad.com/reader/2008/anamorphism/</link>
		<comments>http://comonad.com/reader/2008/anamorphism/#comments</comments>
		<pubDate>Fri, 04 Jul 2008 17:34:08 +0000</pubDate>
		<dc:creator>Edward Kmett</dc:creator>
		
	<category>Category Theory</category>
	<category>Mathematics</category>
	<category>Recursion Schemes</category>
		<guid isPermaLink="false">http://comonad.com/reader/2008/anamorphism/</guid>
		<description><![CDATA[Description
Anamorphisms are generalizations of Haskell's unfoldr. A anamorphism builds a data structure with an F-coalgebra by recursively unrolling a seed.
History
The name anamorphism comes from the Greek 'ανα-' meaning "upwards". [1,2]
Notation
An anamorphism for some F-coalgebra  is denoted with "lenses" . When the functor F can be determined unambiguously, it is usually written  or ana [...]]]></description>
			<content:encoded><![CDATA[<p><b>Description</b><br />
Anamorphisms are generalizations of Haskell's <code>unfoldr</code>. A anamorphism builds a data structure with an F-coalgebra by recursively unrolling a seed.</p>
<p><b>History</b><br />
The name anamorphism comes from the Greek 'ανα-' meaning "upwards". [1,2]</p>
<p><b>Notation</b><br />
An anamorphism for some F-coalgebra <img src='http://comonad.com/latex/1ca5d4abe3b8d50fbd6186a750a23d2a.png' title='$(X,\psi)$' alt='$(X,\psi)$' align=absmiddle> is denoted with "lenses" <img src='http://comonad.com/latex/c9328b1f2d2adf0e8da446eb7147ce52.png' title='$[\hspace{-.2em}( \psi )\hspace{-.2em}]_F$' alt='$[\hspace{-.2em}( \psi )\hspace{-.2em}]_F$' align=absmiddle>. When the functor F can be determined unambiguously, it is usually written <img src='http://comonad.com/latex/6b5c6221e18580bee7c5a7926d39eb9c.png' title='$[\hspace{-.2em}( \psi )\hspace{-.2em}]$' alt='$[\hspace{-.2em}( \psi )\hspace{-.2em}]$' align=absmiddle> or <b>ana</b> <img src='http://comonad.com/latex/7e3c241c2dec821bd6c6fbd314fe4762.png' title='$\psi$' alt='$\psi$' align=absmiddle>. </p>
<p><b>Implementation</b></p>
<pre class="haskell">&nbsp;
ana :: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Functor"><span style="background-color: #efefbf; font-weight: bold;">Functor</span></a> f =&gt; Coalgebra f a -&gt; a -&gt; FixF f
ana g = InF . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> <span style="color: green;">&#40;</span>ana g<span style="color: green;">&#41;</span> . g
&nbsp;</pre>
<p><b>Alternate implementations</b></p>
<pre class="haskell">&nbsp;
ana g = hylo InF g
&nbsp;</pre>
<p><b>Duality</b></p>
<p>An anamorphism is the categorical dual of a <a href="http://comonad.com/reader/2008/catamorphism">catamorphism</a>.</p>
<p><b>Derivation</b><br />
If <img src='http://comonad.com/latex/6c0d793f7e651c8a9780a858cd2554f8.png' title='$(\nu F,\mathrm{out}_F)$' alt='$(\nu F,\mathrm{out}_F)$' align=absmiddle> is the final <img src='http://comonad.com/latex/b8bc815b5e9d5177af01fd4d3d3c2f10.png' title='$F$' alt='$F$' align=absmiddle>-coalgebra for some endofunctor <img src='http://comonad.com/latex/b8bc815b5e9d5177af01fd4d3d3c2f10.png' title='$F$' alt='$F$' align=absmiddle> and <img src='http://comonad.com/latex/1ca5d4abe3b8d50fbd6186a750a23d2a.png' title='$(X,\psi)$' alt='$(X,\psi)$' align=absmiddle> is an <img src='http://comonad.com/latex/b8bc815b5e9d5177af01fd4d3d3c2f10.png' title='$F$' alt='$F$' align=absmiddle>-coalgebra, then there is a unique <img src='http://comonad.com/latex/b8bc815b5e9d5177af01fd4d3d3c2f10.png' title='$F$' alt='$F$' align=absmiddle>-coalgebra homomorphism from <img src='http://comonad.com/latex/1ca5d4abe3b8d50fbd6186a750a23d2a.png' title='$(X,\psi)$' alt='$(X,\psi)$' align=absmiddle> to <img src='http://comonad.com/latex/6c0d793f7e651c8a9780a858cd2554f8.png' title='$(\nu F,\mathrm{out}_F)$' alt='$(\nu F,\mathrm{out}_F)$' align=absmiddle> which we denote <img src='http://comonad.com/latex/1ee46bc200bea55176d532b043758969.png' title='$[\!( \psi )\!]_F$' alt='$[\!( \psi )\!]_F$' align=absmiddle>. </p>
<p>That is to say, the following diagram commutes:</p>
<div class="codeblock" align="center">
<img src='http://comonad.com/latex/323693a3ab9a06bd8d1ee2eaa44661c8.png' title='\bfig \square/&gt;`&gt;`&gt;`&gt;/[X`F X`\nu F`F \nu F;\psi`{[}\!{(}\psi{)}\!{]}`F {[}\!{(}\psi{)}\!{]}`\mathrm{out}_F] \efig ' alt='\bfig \square/&gt;`&gt;`&gt;`&gt;/[X`F X`\nu F`F \nu F;\psi`{[}\!{(}\psi{)}\!{]}`F {[}\!{(}\psi{)}\!{]}`\mathrm{out}_F] \efig ' align=absmiddle>
</div>
<p><b>Laws</b></p>
<table>
<tr>
<th>Rule</th>
<th>Haskell</th>
</tr>
<tr>
<th>ana-charn</th>
<td>fmap x . psi = outF . x <img src='http://comonad.com/latex/ebf45b23c8b2fe7cb8bf20cb8bbd565d.png' title='$\equiv$' alt='$\equiv$' align=absmiddle> x = ana psi</td>
</tr>
<tr>
<th>ana-self</th>
<td>fmap (ana psi) . psi = outF . ana psi</td>
</tr>
<tr>
<th>ana-id</th>
<td>id = ana outF</td>
</tr>
<tr>
<th>ana-uniq</th>
<td>fmap x . psi = outF . x <img src='http://comonad.com/latex/27290dc895d845aaaa0cf6cd9efb862f.png' title='$\wedge$' alt='$\wedge$' align=absmiddle> fmap y . psi = outF . y => x = y</td>
</tr>
<tr>
<th>ana-fusion</th>
<td>fmap x . phi = psi  . x => ana phi = ana psi . x</td>
</tr>
<tr>
<th>ana-compose</th>
<td>e :: f :~> g => ana (e . outF) . ana phi = ana (e . phi)</td>
</tr>
</table>
<p><b>Examples</b></p>
<pre class="haskell">&nbsp;
<span style="color: #06c; font-weight: bold;">data</span> StreamF a x = a :&gt; x
<span style="color: #06c; font-weight: bold;">type</span> Stream a = FixF <span style="color: green;">&#40;</span>StreamF a<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #06c; font-weight: bold;">instance</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Functor"><span style="background-color: #efefbf; font-weight: bold;">Functor</span></a> <span style="color: green;">&#40;</span>StreamF a<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span>
    <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> f <span style="color: green;">&#40;</span>a :&gt; <span style="color: #06c; font-weight: bold;">as</span><span style="color: green;">&#41;</span> = a :&gt; f <span style="color: #06c; font-weight: bold;">as</span>
&nbsp;
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:repeat"><span style="font-weight: bold;">repeat</span></a> :: a -&gt; Stream a
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:repeat"><span style="font-weight: bold;">repeat</span></a> = ana psi <span style="color: #06c; font-weight: bold;">where</span>
    psi n = n :&gt; n
&nbsp;
from :: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Num"><span style="background-color: #efefbf; font-weight: bold;">Num</span></a> a =&gt; a -&gt; Stream a
from = ana psi <span style="color: #06c; font-weight: bold;">where</span>
    psi n = n :&gt; <span style="color: green;">&#40;</span>n + <span style="color: red;">1</span><span style="color: green;">&#41;</span>
&nbsp;</pre>
<p><b>Links</b><br />
[<a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Morphism-Ana.html">Haddock</a>] [<a href="http://comonad.com/haskell/category-extras/src/Control/Morphism/Ana.hs">Source</a>] [<a href="http://comonad.com/reader/2008/recursion-schemes/">Field Guide</a>]</p>
<p><b>References</b></p>
<p>[1] E. Meijer, M. Fokkinga, R. Paterson. Functional Programming with Bananas, Lenses, Envelopes and Barbed Wire. Proceedings, 5th ACM Conference on Functional Programming Languages and Computer Architecture.<br />
[2] M. Fokkinga. <a href="http://dbappl.cs.utwente.nl/Publications/PaperStore/db-utwente-404F4540.pdf">Law and Order in Algorithmics</a>. PhD Thesis. 1992
</p>
]]></content:encoded>
			<wfw:commentRSS>http://comonad.com/reader/2008/anamorphism/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>MSFP 2008</title>
		<link>http://comonad.com/reader/2008/msfp/</link>
		<comments>http://comonad.com/reader/2008/msfp/#comments</comments>
		<pubDate>Thu, 03 Jul 2008 18:02:29 +0000</pubDate>
		<dc:creator>Edward Kmett</dc:creator>
		
	<category>Category Theory</category>
	<category>Mathematics</category>
	<category>Conferences</category>
		<guid isPermaLink="false">http://comonad.com/reader/2008/msfp/</guid>
		<description><![CDATA[I'm off to MSFP'08 (which is colocated with ICALP) and should have a day or two spare on either side of the workshop to let me find stuff to do in Iceland and meet people.
I'd love to get a chance to meet folks in person.
If anyone else is planning on being in the area, please [...]]]></description>
			<content:encoded><![CDATA[<p>I'm off to <a href="http://msfp.org.uk/">MSFP'08</a> (which is colocated with <a href="http://www.ru.is/icalp08/">ICALP</a>) and should have a day or two spare on either side of the workshop to let me find stuff to do in Iceland and meet people.</p>
<p>I'd love to get a chance to meet folks in person.</p>
<p>If anyone else is planning on being in the area, please feel free to <a href="mailto:ekmett@gmail.com">drop me a line</a>.
</p>
]]></content:encoded>
			<wfw:commentRSS>http://comonad.com/reader/2008/msfp/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Memoizing Context</title>
		<link>http://comonad.com/reader/2008/memoizing-context/</link>
		<comments>http://comonad.com/reader/2008/memoizing-context/#comments</comments>
		<pubDate>Fri, 27 Jun 2008 02:38:38 +0000</pubDate>
		<dc:creator>Edward Kmett</dc:creator>
		
	<category>Haskell</category>
	<category>Category Theory</category>
	<category>Comonads</category>
	<category>Mathematics</category>
		<guid isPermaLink="false">http://comonad.com/reader/2008/memoizing-context/</guid>
		<description><![CDATA[[Edit: re-introduced the domain function]
Recently Andy Gill posted a nice use of data families to memoize a narrow range of values to optimize Conal Elliott's functional linear maps. 
I've tweaked Andy's definition slightly below to use a type family for D e instead of a data family, which avoids some of the syntactic noise of [...]]]></description>
			<content:encoded><![CDATA[<p>[Edit: re-introduced the domain function]</p>
<p>Recently Andy Gill <a href="http://blog.unsafeperformio.com/?p=30">posted</a> a nice use of data families to memoize a narrow range of values to optimize Conal Elliott's <a href="http://conal.net/blog/posts/functional-linear-maps/">functional linear maps</a>. </p>
<p>I've tweaked Andy's definition slightly below to use a type family for D e instead of a data family, which avoids some of the syntactic noise of the domain function when D e = e, and lets it this be used transparently in some places. This change lacks substance however, and the source code links at the bottom include the code with and without this change.</p>
<p>Since I'm using data and type families, you'll need GHC 6.9. </p>
<pre class="haskell">&nbsp;
<span style="color: #06c; font-weight: bold;">class</span> Applicative <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>:~*<span style="color: green;">&#41;</span> e<span style="color: green;">&#41;</span> =&gt; NarrowMemo e <span style="color: #06c; font-weight: bold;">where</span>
        <span style="color: #06c; font-weight: bold;">data</span> <span style="color: green;">&#40;</span>:~*<span style="color: green;">&#41;</span> e :: * -&gt; *
        <span style="color: #06c; font-weight: bold;">type</span> D e :: *
        apply :: <span style="color: green;">&#40;</span>e :~* r<span style="color: green;">&#41;</span> -&gt; D e -&gt; r
        memo :: <span style="color: green;">&#40;</span>D e -&gt; r<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</span>e :~* r<span style="color: green;">&#41;</span>
        domain :: D e -&gt; e
&nbsp;
<span style="color: #06c; font-weight: bold;">instance</span> NarrowMemo <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Bool"><span style="background-color: #efefbf; font-weight: bold;">Bool</span></a> <span style="color: #06c; font-weight: bold;">where</span>
        <span style="color: #06c; font-weight: bold;">data</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Bool"><span style="background-color: #efefbf; font-weight: bold;">Bool</span></a> :~* a = MemoBool a a
        <span style="color: #06c; font-weight: bold;">type</span> D <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Bool"><span style="background-color: #efefbf; font-weight: bold;">Bool</span></a> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Bool"><span style="background-color: #efefbf; font-weight: bold;">Bool</span></a>
        apply <span style="color: green;">&#40;</span>MemoBool o1 o2<span style="color: green;">&#41;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:True"><span style="font-weight: bold;">True</span></a> = o1
        apply <span style="color: green;">&#40;</span>MemoBool o1 o2<span style="color: green;">&#41;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:False"><span style="font-weight: bold;">False</span></a> = o2
        memo f = MemoBool <span style="color: green;">&#40;</span>f <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:True"><span style="font-weight: bold;">True</span></a><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>f <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:False"><span style="font-weight: bold;">False</span></a><span style="color: green;">&#41;</span>
        domain = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:id"><span style="font-weight: bold;">id</span></a>
&nbsp;</pre>
<p>We can quickly add in the missing bits for the Bool demo he supplied:</p>
<pre class="haskell">&nbsp;
<span style="color: #06c; font-weight: bold;">instance</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Functor"><span style="background-color: #efefbf; font-weight: bold;">Functor</span></a> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>:~*<span style="color: green;">&#41;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Bool"><span style="background-color: #efefbf; font-weight: bold;">Bool</span></a><span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span>
        <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> f <span style="color: green;">&#40;</span>MemoBool a b<span style="color: green;">&#41;</span> = MemoBool <span style="color: green;">&#40;</span>f a<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>f b<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #06c; font-weight: bold;">instance</span> Applicative <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>:~*<span style="color: green;">&#41;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Bool"><span style="background-color: #efefbf; font-weight: bold;">Bool</span></a><span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span>
        pure x = MemoBool x x
        MemoBool f g  &lt; *&gt; MemoBool a b = MemoBool <span style="color: green;">&#40;</span>f a<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>g b<span style="color: green;">&#41;</span>
&nbsp;</pre>
<p>And we can automatically generate an instance of Monad for (:~*)e if we really want to:</p>
<pre class="haskell">&nbsp;
<span style="color: #06c; font-weight: bold;">instance</span> NarrowMemo e =&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Monad"><span style="background-color: #efefbf; font-weight: bold;">Monad</span></a> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>:~*<span style="color: green;">&#41;</span> e<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span>
        <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:return"><span style="font-weight: bold;">return</span></a> = pure
        m &gt;&gt;= k = memo $ apply <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> k m<span style="color: green;">&#41;</span> &gt;&gt;= apply <span style="color: #5d478b; font-style: italic;">-- in (-&gt;)e</span>
&nbsp;</pre>
<p>And we can drop in a couple more instances to make it interesting. The tensor product:</p>
<pre class="haskell">&nbsp;
<span style="color: #06c; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span>NarrowMemo a, NarrowMemo b<span style="color: green;">&#41;</span> =&gt;
    NarrowMemo <span style="color: green;">&#40;</span>a,b<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span>
        <span style="color: #06c; font-weight: bold;">data</span> <span style="color: green;">&#40;</span>a,b<span style="color: green;">&#41;</span> :~* e = MemoBoth <span style="color: green;">&#40;</span>a :~* <span style="color: green;">&#40;</span>b :~* e<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
        <span style="color: #06c; font-weight: bold;">type</span> D <span style="color: green;">&#40;</span>a,b<span style="color: green;">&#41;</span> = <span style="color: green;">&#40;</span>D a, D b<span style="color: green;">&#41;</span>
        apply <span style="color: green;">&#40;</span>MemoBoth f<span style="color: green;">&#41;</span> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:uncurry"><span style="font-weight: bold;">uncurry</span></a> <span style="color: green;">&#40;</span>apply . apply f<span style="color: green;">&#41;</span>
        memo f = MemoBoth $ memo $ \a -&gt; memo <span style="color: green;">&#40;</span>f . <span style="color: green;">&#40;</span>,<span style="color: green;">&#41;</span> a<span style="color: green;">&#41;</span>
        domain = domain *** domain
&nbsp;
<span style="color: #06c; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span>NarrowMemo a, NarrowMemo b<span style="color: green;">&#41;</span> =&gt;
    <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Functor"><span style="background-color: #efefbf; font-weight: bold;">Functor</span></a> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>:~*<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>a,b<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span>
        <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> f <span style="color: green;">&#40;</span>MemoBoth g<span style="color: green;">&#41;</span> = MemoBoth <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> f<span style="color: green;">&#41;</span> g<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #06c; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span>NarrowMemo a, NarrowMemo b<span style="color: green;">&#41;</span> =&gt;
    Applicative <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>:~*<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>a,b<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span>
        pure = MemoBoth . pure . pure
        f &lt; *&gt; a = MemoBoth $
                memo $ \da -&gt;
                memo $ \db -&gt;
                <span style="color: #06c; font-weight: bold;">let</span> e = <span style="color: green;">&#40;</span>da, db<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">in</span>
                apply f e <span style="color: green;">&#40;</span>apply a e<span style="color: green;">&#41;</span>
&nbsp;</pre>
<p>and a disjoint sum, if only so we have some more memo tables to play with.</p>
<pre class="haskell">&nbsp;
<span style="color: #06c; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span>NarrowMemo a, NarrowMemo b<span style="color: green;">&#41;</span> =&gt;
    NarrowMemo <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Either"><span style="background-color: #efefbf; font-weight: bold;">Either</span></a> a b<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span>
        <span style="color: #06c; font-weight: bold;">data</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Either"><span style="background-color: #efefbf; font-weight: bold;">Either</span></a> a b :~* e = MemoEither <span style="color: green;">&#40;</span>a :~* e<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>b :~* e<span style="color: green;">&#41;</span>
        <span style="color: #06c; font-weight: bold;">type</span> D <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Either"><span style="background-color: #efefbf; font-weight: bold;">Either</span></a> a b<span style="color: green;">&#41;</span> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Either"><span style="background-color: #efefbf; font-weight: bold;">Either</span></a> <span style="color: green;">&#40;</span>D a<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>D b<span style="color: green;">&#41;</span>
        apply <span style="color: green;">&#40;</span>MemoEither l _<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:Left"><span style="font-weight: bold;">Left</span></a> a<span style="color: green;">&#41;</span> = apply l a
        apply <span style="color: green;">&#40;</span>MemoEither _ r<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:Right"><span style="font-weight: bold;">Right</span></a> b<span style="color: green;">&#41;</span> = apply r b
        memo f = MemoEither <span style="color: green;">&#40;</span>memo <span style="color: green;">&#40;</span>f . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:Left"><span style="font-weight: bold;">Left</span></a><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>memo <span style="color: green;">&#40;</span>f . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:Right"><span style="font-weight: bold;">Right</span></a><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
        domain = domain +++ domain
&nbsp;
<span style="color: #06c; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span>NarrowMemo a, NarrowMemo b<span style="color: green;">&#41;</span> =&gt;
    <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Functor"><span style="background-color: #efefbf; font-weight: bold;">Functor</span></a> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>:~*<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Either"><span style="background-color: #efefbf; font-weight: bold;">Either</span></a> a b<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span>
        <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> f <span style="color: green;">&#40;</span>MemoEither a b<span style="color: green;">&#41;</span> = MemoEither <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> f a<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> f b<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #06c; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span>NarrowMemo a, NarrowMemo b<span style="color: green;">&#41;</span> =&gt;
    Applicative <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>:~*<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Either"><span style="background-color: #efefbf; font-weight: bold;">Either</span></a> a b<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span>
        pure f = MemoEither <span style="color: green;">&#40;</span>pure f<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>pure f<span style="color: green;">&#41;</span>
        MemoEither f g &lt; *&gt; MemoEither a b = MemoEither <span style="color: green;">&#40;</span>f &lt; *&gt; a<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>g &lt; *&gt; b<span style="color: green;">&#41;</span>
&nbsp;</pre>
<p>Now, the reason I wanted to play with this was I hacked up a memoizing state-in-context comonad a couple of weeks back using unsafePerformIO and a memo table, but using Andy's trick we can build up a pure memoizing context comonad for smaller or more regular domains.</p>
<p>Recall the <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Comonad-Context.html">state-in-context</a> comonad from category-extras.</p>
<pre class="haskell">&nbsp;
<span style="color: #06c; font-weight: bold;">class</span> <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Comonad.html#t:Comonad"><span style="background-color: #efefbf; font-weight: bold;">Comonad</span></a> w =&gt; ComonadContext s w | w -&gt; s <span style="color: #06c; font-weight: bold;">where</span>
        getC :: w a -&gt; s
        modifyC :: <span style="color: green;">&#40;</span>s -&gt; s<span style="color: green;">&#41;</span> -&gt; w a -&gt; a
&nbsp;
<span style="color: #06c; font-weight: bold;">data</span> Context s a = Context <span style="color: green;">&#40;</span>s -&gt; a<span style="color: green;">&#41;</span> s
&nbsp;
<span style="color: #06c; font-weight: bold;">instance</span> ComonadContext s <span style="color: green;">&#40;</span>Context s<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span>
        getC <span style="color: green;">&#40;</span>Context _ s<span style="color: green;">&#41;</span> = s
        modifyC m <span style="color: green;">&#40;</span>Context f c<span style="color: green;">&#41;</span> = f <span style="color: green;">&#40;</span>m c<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #06c; font-weight: bold;">instance</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Functor"><span style="background-color: #efefbf; font-weight: bold;">Functor</span></a> <span style="color: green;">&#40;</span>Context s<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span>
        <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> f <span style="color: green;">&#40;</span>Context f' s<span style="color: green;">&#41;</span> = Context <span style="color: green;">&#40;</span>f . f'<span style="color: green;">&#41;</span> s
&nbsp;
<span style="color: #06c; font-weight: bold;">instance</span> Copointed <span style="color: green;">&#40;</span>Context s<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span>
        <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Comonad.html#v:extract"><span style="font-weight: bold;">extract</span></a>   <span style="color: green;">&#40;</span>Context f a<span style="color: green;">&#41;</span> = f a
&nbsp;
<span style="color: #06c; font-weight: bold;">instance</span> <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Comonad.html#t:Comonad"><span style="background-color: #efefbf; font-weight: bold;">Comonad</span></a> <span style="color: green;">&#40;</span>Context s<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span>
        <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Comonad.html#v:duplicate"><span style="font-weight: bold;">duplicate</span></a> <span style="color: green;">&#40;</span>Context f a<span style="color: green;">&#41;</span> = Context <span style="color: green;">&#40;</span>Context f<span style="color: green;">&#41;</span> a
&nbsp;</pre>
<p>We can modify the definition to replace (->) with the version above of Andy's (:~*) to obtain a narrowly memoized version:</p>
<pre class="haskell">&nbsp;
<span style="color: #06c; font-weight: bold;">data</span> NarrowContext e a = NarrowContext <span style="color: green;">&#40;</span>e :~* a<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>D e<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #06c; font-weight: bold;">instance</span> NarrowMemo e =&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Functor"><span style="background-color: #efefbf; font-weight: bold;">Functor</span></a> <span style="color: green;">&#40;</span>NarrowContext e<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span>
        <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> f <span style="color: green;">&#40;</span>NarrowContext t d<span style="color: green;">&#41;</span> = NarrowContext <span style="color: green;">&#40;</span>memo <span style="color: green;">&#40;</span>f . apply t<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> d
&nbsp;
<span style="color: #06c; font-weight: bold;">instance</span> NarrowMemo e =&gt; Copointed <span style="color: green;">&#40;</span>NarrowContext e<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span>
        <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Comonad.html#v:extract"><span style="font-weight: bold;">extract</span></a> <span style="color: green;">&#40;</span>NarrowContext t d<span style="color: green;">&#41;</span> = apply t d
&nbsp;
<span style="color: #06c; font-weight: bold;">instance</span> NarrowMemo e =&gt; <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Comonad.html#t:Comonad"><span style="background-color: #efefbf; font-weight: bold;">Comonad</span></a> <span style="color: green;">&#40;</span>NarrowContext e<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span>
        <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Comonad.html#v:duplicate"><span style="font-weight: bold;">duplicate</span></a> <span style="color: green;">&#40;</span>NarrowContext f a<span style="color: green;">&#41;</span> =
                NarrowContext <span style="color: green;">&#40;</span>memo <span style="color: green;">&#40;</span>NarrowContext f<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> a
&nbsp;
<span style="color: #06c; font-weight: bold;">instance</span> NarrowMemo e =&gt; ComonadContext <span style="color: green;">&#40;</span>D e<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>NarrowContext e a<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span>
        getC <span style="color: green;">&#40;</span>NarrowContext _ e<span style="color: green;">&#41;</span> = e
        modifyC f <span style="color: green;">&#40;</span>NarrowContext g e<span style="color: green;">&#41;</span> = apply g <span style="color: green;">&#40;</span>f e<span style="color: green;">&#41;</span>
&nbsp;</pre>
<p>Now any computation done in this modified state-in-context comonad is memoized and compositions of those computations are also memoized and built up from the memoized intermediate steps.</p>
<p>We should be able to play similar games with a lot of other (co)monads that use exponentials as well.</p>
<p>For instance (:~*) itself can play the role of a narrowly-memoized anonymous exponential comonad.</p>
<pre class="haskell">&nbsp;
<span style="color: #06c; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span>NarrowMemo e, Monoid <span style="color: green;">&#40;</span>D e<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> =&gt; Copointed <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>:~*<span style="color: green;">&#41;</span> e<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span>
        <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Comonad.html#v:extract"><span style="font-weight: bold;">extract</span></a> t = apply t mempty
&nbsp;
<span style="color: #06c; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span>NarrowMemo e, Monoid <span style="color: green;">&#40;</span>D e<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> =&gt; <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Comonad.html#t:Comonad"><span style="background-color: #efefbf; font-weight: bold;">Comonad</span></a> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>:~*<span style="color: green;">&#41;</span> e<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span>
        <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Comonad.html#v:duplicate"><span style="font-weight: bold;">duplicate</span></a> f = memo $ m -&gt; memo <span style="color: green;">&#40;</span>apply f . mappend m<span style="color: green;">&#41;</span>
&nbsp;</pre>
<p>[ <a href="http://comonad.com/haskell/posts/NarrowContext.hs">Source Code (using data families only)</a> ]<br />
[ <a href="http://comonad.com/haskell/posts/NarrowContextWithTypeFamilies.hs">Source Code (using data and type families)</a> ]</p>
]]></content:encoded>
			<wfw:commentRSS>http://comonad.com/reader/2008/memoizing-context/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Paramorphism</title>
		<link>http://comonad.com/reader/2008/paramorphism/</link>
		<comments>http://comonad.com/reader/2008/paramorphism/#comments</comments>
		<pubDate>Mon, 23 Jun 2008 03:23:09 +0000</pubDate>
		<dc:creator>Edward Kmett</dc:creator>
		
	<category>Category Theory</category>
	<category>Mathematics</category>
	<category>Recursion Schemes</category>
		<guid isPermaLink="false">http://comonad.com/reader/2008/paramorphism/</guid>
		<description><![CDATA[Description
A paramorphism is used when a catamorphism won't quite do because you need not only the result of recursing with the F-algebra over the tail of a structure, but you also need the tail itself. This is another way to say that you call in a paramorphism when you need the power of primitive recursion. [...]]]></description>
			<content:encoded><![CDATA[<p><b>Description</b><br />
A paramorphism is used when a <a href="http://comonad.com/reader/2008/catamorphism">catamorphism</a> won't quite do because you need not only the result of recursing with the F-algebra over the tail of a structure, but you also need the tail itself. This is another way to say that you call in a paramorphism when you need the power of primitive recursion. </p>
<p><b>History</b><br />
Lambert Meertens coined the name paramorphism [1]. The name comes from the greek παρα meaning "alongside", the root of parallel.</p>
<p><b>Notation</b><br />
A paramorphisms defined in terms of an <img src='http://comonad.com/latex/b8bc815b5e9d5177af01fd4d3d3c2f10.png' title='$F$' alt='$F$' align=absmiddle>-<img src='http://comonad.com/latex/a06a0d09f3b4a0c10b5aba8fe0822b13.png' title='$(\mu F \times)$' alt='$(\mu F \times)$' align=absmiddle>-Algebra <img src='http://comonad.com/latex/ff34d6684e92e5be6ba531d5c62a57b6.png' title='$(X,\varphi)$' alt='$(X,\varphi)$' align=absmiddle> are commonly denoted using "barbed wire" as <img src='http://comonad.com/latex/fad56a738ec02785f2b4b964876d7f38.png' title='${[}\!\!\langle f \rangle\!\!{]}$' alt='${[}\!\!\langle f \rangle\!\!{]}$' align=absmiddle> or as <b>para</b> f. Uustalu and Vene [2] use the notation <img src='http://comonad.com/latex/e44f579c3cc4bbfb60b37cf570502bce.png' title='$\langle\!| f |\!\rangle$' alt='$\langle\!| f |\!\rangle$' align=absmiddle> so that apomorphisms, the categorical dual of paramorphisms, can have a symmetric notation.</p>
<p><b>Implementation</b></p>
<pre class="haskell">&nbsp;
<span style="color: #06c; font-weight: bold;">type</span> Para f = <span style="color: green;">&#40;</span>,<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>FixF f<span style="color: green;">&#41;</span>
<span style="color: #5d478b; font-style: italic;">-- para :: Functor f =&gt; GAlgebra f (Para f) a -&gt; FixF f -&gt; a</span>
para :: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Functor"><span style="background-color: #efefbf; font-weight: bold;">Functor</span></a> f =&gt; <span style="color: green;">&#40;</span>f <span style="color: green;">&#40;</span>FixF f,a<span style="color: green;">&#41;</span> -&gt; a<span style="color: green;">&#41;</span> -&gt; FixF f -&gt; a
para f = f . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:id"><span style="font-weight: bold;">id</span></a> &amp;&amp;&amp; para f<span style="color: green;">&#41;</span> . outF <span style="color: #5d478b; font-style: italic;">-- from para-cancel</span>
&nbsp;</pre>
<p><b>Alternate Implementations</b></p>
<pre class="haskell">&nbsp;
para f = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:snd"><span style="font-weight: bold;">snd</span></a> . cata <span style="color: green;">&#40;</span>InF . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fst"><span style="font-weight: bold;">fst</span></a> &amp;&amp;&amp; f<span style="color: green;">&#41;</span> <span style="color: #5d478b; font-style: italic;">-- para-def</span>
para = zygo InF
&nbsp;</pre>
<p><b>Laws</b></p>
<table>
<tr>
<th>Rule</th>
<th>Haskell</th>
</tr>
<tr>
<th>para-def</th>
<td>para phi = snd . cata (InF . fmap fst &&& phi)</td>
</tr>
<tr>
<th>para-charn</th>
<td>f . InF = phi . fmap (f &&& id) &lt;=&gt; f = para phi</td>
</tr>
<tr>
<th>para-cancel</th>
<td>para phi . InF = phi . fmap (para phi &&& id)</td>
</tr>
<tr>
<th>para-refl</th>
<td>para (InF . fmap snd) = id</td>
</tr>
<tr>
<th>para-fusion</th>
<td>f . phi = psi . fmap (id *** f) => f . para phi = para psi</td>
</tr>
<tr>
<th>para-cata</th>
<td>cata phi = para (phi . fmap fst)</td>
</tr>
<tr>
<th>para-any</th>
<td>f = para (f . InF . fmap fst)</td>
</tr>
<tr>
<th>para-out</th>
<td>para (fmap fst) = outF</td>
</tr>
</table>
<p><b>Derivation</b></p>
<p>For any two morphisms <img src='http://comonad.com/latex/b859fd3ba2944c1bbb40d6e523c16bc0.png' title='$f : \mu F -&gt; X$' alt='$f : \mu F -&gt; X$' align=absmiddle> and <img src='http://comonad.com/latex/0d4f940819c68af39824e9c0a3362406.png' title='$\varphi : F (X \times \mu F) -&gt; X$' alt='$\varphi : F (X \times \mu F) -&gt; X$' align=absmiddle> we have <img src='http://comonad.com/latex/65e8c383a368fc4ad278e19763a5af54.png' title='$f \circ \mathrm{in}_F = \varphi \circ F \langle f, \mathrm{id}_{\mu F}\rangle \Leftrightarrow f = \mathrm{fst} \circ (\!| \langle \varphi, \mathrm{in}_F \circ F \mathrm{snd} \rangle |\!)$' alt='$f \circ \mathrm{in}_F = \varphi \circ F \langle f, \mathrm{id}_{\mu F}\rangle \Leftrightarrow f = \mathrm{fst} \circ (\!| \langle \varphi, \mathrm{in}_F \circ F \mathrm{snd} \rangle |\!)$' align=absmiddle>. [2, p9, Lemma 2] </p>
<p>In other words, if we define <img src='http://comonad.com/latex/531fa60c832bed3c7e302095af83644e.png' title='${[}\!\!\langle \varphi \rangle\!\!{]} = f$' alt='${[}\!\!\langle \varphi \rangle\!\!{]} = f$' align=absmiddle>, the following diagram commutes:</p>
<div class="codeblock" align="center">
<img src='http://comonad.com/latex/c1dae3e8857928767ea3a532bb956bca.png' title='\bfig \square/&gt;`&gt;`&gt;`&gt;/[F \mu F`\mu F`F (X \times \mu F)`X;\mathrm{in}_F`F \langle {[}\!\!\langle \varphi \rangle\!\!{]}, id_{\mu F}\rangle`{[}\!\!\langle \varphi \rangle\!\!{]}`\varphi] \efig ' alt='\bfig \square/&gt;`&gt;`&gt;`&gt;/[F \mu F`\mu F`F (X \times \mu F)`X;\mathrm{in}_F`F \langle {[}\!\!\langle \varphi \rangle\!\!{]}, id_{\mu F}\rangle`{[}\!\!\langle \varphi \rangle\!\!{]}`\varphi] \efig ' align=absmiddle>
</div>
<p><b>Example</b></p>
<pre class="haskell">&nbsp;
<span style="color: #06c; font-weight: bold;">data</span> NatF a = S a | Z <span style="color: #06c; font-weight: bold;">deriving</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Eq"><span style="background-color: #efefbf; font-weight: bold;">Eq</span></a>,<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Show"><span style="background-color: #efefbf; font-weight: bold;">Show</span></a><span style="color: green;">&#41;</span>
<span style="color: #06c; font-weight: bold;">type</span> Nat = FixF NatF
&nbsp;
<span style="color: #06c; font-weight: bold;">instance</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Functor"><span style="background-color: #efefbf; font-weight: bold;">Functor</span></a> NatF <span style="color: #06c; font-weight: bold;">where</span>
     <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> f Z = Z
     <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> f <span style="color: green;">&#40;</span>S z<span style="color: green;">&#41;</span> = S <span style="color: green;">&#40;</span>f z<span style="color: green;">&#41;</span>
&nbsp;
plus :: Nat -&gt; Nat -&gt; Nat
plus n = cata phi <span style="color: #06c; font-weight: bold;">where</span>
     phi Z = n
     phi <span style="color: green;">&#40;</span>S m<span style="color: green;">&#41;</span> = s m
&nbsp;
times :: Nat -&gt; Nat -&gt; Nat
times n = cata phi <span style="color: #06c; font-weight: bold;">where</span>
     phi Z = z
     phi <span style="color: green;">&#40;</span>S m<span style="color: green;">&#41;</span> = plus n m
&nbsp;
z :: Nat
z = InF Z
&nbsp;
s :: Nat -&gt; Nat
s = InF . S
&nbsp;
fac :: Nat -&gt; Nat
fac = para phi <span style="color: #06c; font-weight: bold;">where</span>
     phi Z = s z
     phi <span style="color: green;">&#40;</span>S <span style="color: green;">&#40;</span>n,f<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> = times f <span style="color: green;">&#40;</span>s n<span style="color: green;">&#41;</span>
&nbsp;</pre>
<p><b>Links</b><br />
[<a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Morphism-Para.html">Haddock</a>] [<a href="http://comonad.com/haskell/category-extras/src/Control/Morphism/Para.hs">Source</a>] [<a href="http://comonad.com/reader/2008/recursion-schemes/">Field Guide</a>]</p>
<p><b>References</b></p>
<p>[1] L. Meertens. Paramorphisms. Formal Aspects of Computing, 4(5):413-424, 1992.<br />
[2] T. Uustalu and V. Vene. Primitive (Co)Recursion and Course-of-Value (Co)Iteration, Categorically. Informatica, 1999 Vol. 10, No. 1, 1-0</p>
]]></content:encoded>
			<wfw:commentRSS>http://comonad.com/reader/2008/paramorphism/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Catamorphism</title>
		<link>http://comonad.com/reader/2008/catamorphism/</link>
		<comments>http://comonad.com/reader/2008/catamorphism/#comments</comments>
		<pubDate>Mon, 23 Jun 2008 03:21:51 +0000</pubDate>
		<dc:creator>Edward Kmett</dc:creator>
		
	<category>Category Theory</category>
	<category>Mathematics</category>
	<category>Recursion Schemes</category>
		<guid isPermaLink="false">http://comonad.com/reader/2008/catamorphism/</guid>
		<description><![CDATA[Description
Catamorphisms are generalizations of Haskell's foldr. A catamorphism deconstructs a data structure with an F-algebra.
History
The name catamorphism appears to have been chosen by Lambert Meertens [1]. The category theoretic machinery behind these was resolved by Grant Malcolm [2,3], and they were popularized by Meijer, Fokkinga and Paterson [4,5]. The name comes from the Greek 'κατα-' [...]]]></description>
			<content:encoded><![CDATA[<p><b>Description</b><br />
Catamorphisms are generalizations of Haskell's foldr. A catamorphism deconstructs a data structure with an F-algebra.</p>
<p><b>History</b><br />
The name catamorphism appears to have been chosen by Lambert Meertens [1]. The category theoretic machinery behind these was resolved by Grant Malcolm [2,3], and they were popularized by Meijer, Fokkinga and Paterson [4,5]. The name comes from the Greek 'κατα-' meaning "downward or according to". A useful mnemonic is to think of a catastrophe destroying something.</p>
<p><b>Notation</b><br />
A catamorphism for some F-algebra <img src='http://comonad.com/latex/85334d1decefb1be0e5df4bedaa72493.png' title='$(X,f)$' alt='$(X,f)$' align=absmiddle> is denoted <img src='http://comonad.com/latex/9afbea186424dbf021a59542dc631b60.png' title='$(\!| f |\!)_F$' alt='$(\!| f |\!)_F$' align=absmiddle>. When the functor F can be determined unambiguously, it is usually written <img src='http://comonad.com/latex/2c843038110502f0f751be6dbcc7d1f4.png' title='$(\!| \varphi |\!)$' alt='$(\!| \varphi |\!)$' align=absmiddle> or <b>cata</b> <img src='http://comonad.com/latex/417a5301693b60807fa658e5ef9f9535.png' title='$\varphi$' alt='$\varphi$' align=absmiddle>.</p>
<p><b>Implementation</b></p>
<pre class="haskell">&nbsp;
cata :: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Functor"><span style="background-color: #efefbf; font-weight: bold;">Functor</span></a> f =&gt; Algebra f a -&gt; FixF f -&gt; a
cata f = f . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> <span style="color: green;">&#40;</span>cata f<span style="color: green;">&#41;</span> . outF
&nbsp;</pre>
<p><b>Alternate implementations</b></p>
<pre class="haskell">&nbsp;
cata f = hylo f outF
cata f = para <span style="color: green;">&#40;</span>f . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fst"><span style="font-weight: bold;">fst</span></a><span style="color: green;">&#41;</span>
&nbsp;</pre>
<p><b>Duality</b></p>
<p>A catamorphism is the categorical dual of an <a href="http://comonad.com/reader/2008/anamorphism">anamorphism</a>.</p>
<p><b>Derivation</b><br />
If <img src='http://comonad.com/latex/e1548946125db2b0af1abfbcf436a41b.png' title='$(\mu F,\mathrm{in}_F)$' alt='$(\mu F,\mathrm{in}_F)$' align=absmiddle> is the initial <img src='http://comonad.com/latex/b8bc815b5e9d5177af01fd4d3d3c2f10.png' title='$F$' alt='$F$' align=absmiddle>-algebra for some endofunctor <img src='http://comonad.com/latex/b8bc815b5e9d5177af01fd4d3d3c2f10.png' title='$F$' alt='$F$' align=absmiddle> and <img src='http://comonad.com/latex/ff34d6684e92e5be6ba531d5c62a57b6.png' title='$(X,\varphi)$' alt='$(X,\varphi)$' align=absmiddle> is an <img src='http://comonad.com/latex/b8bc815b5e9d5177af01fd4d3d3c2f10.png' title='$F$' alt='$F$' align=absmiddle>-algebra, then there is a unique <img src='http://comonad.com/latex/b8bc815b5e9d5177af01fd4d3d3c2f10.png' title='$F$' alt='$F$' align=absmiddle>-algebra homomorphism from <img src='http://comonad.com/latex/e1548946125db2b0af1abfbcf436a41b.png' title='$(\mu F,\mathrm{in}_F)$' alt='$(\mu F,\mathrm{in}_F)$' align=absmiddle> to <img src='http://comonad.com/latex/ff34d6684e92e5be6ba531d5c62a57b6.png' title='$(X,\varphi)$' alt='$(X,\varphi)$' align=absmiddle> which we denote <img src='http://comonad.com/latex/fbbb93d55276abdd8d71ba5695e492bb.png' title='$(\!| \varphi |\!)_F$' alt='$(\!| \varphi |\!)_F$' align=absmiddle>. </p>
<p>That is to say, the following diagram commutes:</p>
<div class="codeblock" align="center">
<img src='http://comonad.com/latex/14c443c51541664e017338a69089a3fe.png' title='\bfig \square/&gt;`&gt;`&gt;`&gt;/[F \mu F`\mu F`F X`X;\mathrm{in}_F`F {(}\!{|}\varphi{|}\!{)}`{(}\!{|}\varphi{|}\!{)}`\varphi] \efig ' alt='\bfig \square/&gt;`&gt;`&gt;`&gt;/[F \mu F`\mu F`F X`X;\mathrm{in}_F`F {(}\!{|}\varphi{|}\!{)}`{(}\!{|}\varphi{|}\!{)}`\varphi] \efig ' align=absmiddle>
</div>
<p><b>Laws</b></p>
<table>
<tr>
<th>Rule</th>
<th>Categorically</th>
<th>Haskell</th>
</tr>
<tr>
<th>cata-cancel</th>
<td><img src='http://comonad.com/latex/f1a842c3816e85ff1c5f3a16edc28d61.png' title='${(}\!{|}\varphi{|}\!{)}_F \circ \mathrm{in}_F = \varphi \circ F {(}\!{|}\varphi{|}\!{)}_F$' alt='${(}\!{|}\varphi{|}\!{)}_F \circ \mathrm{in}_F = \varphi \circ F {(}\!{|}\varphi{|}\!{)}_F$' align=absmiddle></td>
<td>cata phi . InF = phi . fmap (cata phi)</td>
</tr>
<tr>
<th>cata-refl</th>
<td><img src='http://comonad.com/latex/ebb5debd72db2ce5d14c8ab35305893d.png' title='${(}\!{|}\mathrm{in}_F{|}\!{)}_F = \mathrm{id}_{\mu F}$' alt='${(}\!{|}\mathrm{in}_F{|}\!{)}_F = \mathrm{id}_{\mu F}$' align=absmiddle></td>
<td>cata InF = id</td>
</tr>
<tr>
<th>cata-fusion</th>
<td><img src='http://comonad.com/latex/632891701990b65ee510ae75140405d7.png' title='$\inference{f \circ \varphi = \varphi \circ F f}{f \circ {(}\!{|}\varphi{|}\!{)}_F = {(}\!{|}\varphi{|}\!{)}_F}$' alt='$\inference{f \circ \varphi = \varphi \circ F f}{f \circ {(}\!{|}\varphi{|}\!{)}_F = {(}\!{|}\varphi{|}\!{)}_F}$' align=absmiddle></td>
<td>f . phi = phi . fmap f => <br /> f . cata phi = cata phi </td>
</tr>
<tr>
<th>cata-compose</th>
<td><img src='http://comonad.com/latex/98862ec3fba57e3e113863c4128df0ef.png' title='$\inference{\varepsilon : F \stackrel{\centerdot}{-&gt;} G}{(\!| \varphi |\!)_G \circ (\!|\mathrm{in}_G \circ \varepsilon|\!)_F = (\!|\varphi \circ \varepsilon|\!)_F}$' alt='$\inference{\varepsilon : F \stackrel{\centerdot}{-&gt;} G}{(\!| \varphi |\!)_G \circ (\!|\mathrm{in}_G \circ \varepsilon|\!)_F = (\!|\varphi \circ \varepsilon|\!)_F}$' align=absmiddle></td>
<td>eps :: f :~> g =><br /> cata phi . cata (In . eps) =<br /> cata (phi . eps)</td>
</tr>
</table>
<p><!--<br />
<tr>
<th>cata-strict</th>
<td><img src='http://comonad.com/latex/60e4be850ae9ac7c433a12ae799a75f6.png' title='$\varphi \mathrm{strict} =&gt; {(}\!{|}\varphi{|}\!{)} \mathrm{strict}$' alt='$\varphi \mathrm{strict} =&gt; {(}\!{|}\varphi{|}\!{)} \mathrm{strict}$' align=absmiddle></td>
<p> --><br />
<b>Examples</b></p>
<pre class="haskell">&nbsp;
<span style="color: #06c; font-weight: bold;">data</span> StrF x = Cons <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Char"><span style="background-color: #efefbf; font-weight: bold;">Char</span></a> x | Nil
<span style="color: #06c; font-weight: bold;">type</span> Str = FixF StrF
&nbsp;
<span style="color: #06c; font-weight: bold;">instance</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Functor"><span style="background-color: #efefbf; font-weight: bold;">Functor</span></a> StrF <span style="color: #06c; font-weight: bold;">where</span>
    <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> f <span style="color: green;">&#40;</span>Cons a <span style="color: #06c; font-weight: bold;">as</span><span style="color: green;">&#41;</span> = Cons a <span style="color: green;">&#40;</span>f <span style="color: #06c; font-weight: bold;">as</span><span style="color: green;">&#41;</span>
    <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> f Nil = Nil
&nbsp;
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:length"><span style="font-weight: bold;">length</span></a> :: Str -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="background-color: #efefbf; font-weight: bold;">Int</span></a>
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:length"><span style="font-weight: bold;">length</span></a> = cata phi <span style="color: #06c; font-weight: bold;">where</span>
    phi <span style="color: green;">&#40;</span>Cons a b<span style="color: green;">&#41;</span> = <span style="color: red;">1</span> + b
    phi Nil = <span style="color: red;">0</span>
&nbsp;
<span style="color: #06c; font-weight: bold;">data</span> NatF a = S a | Z <span style="color: #06c; font-weight: bold;">deriving</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Eq"><span style="background-color: #efefbf; font-weight: bold;">Eq</span></a>,<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Show"><span style="background-color: #efefbf; font-weight: bold;">Show</span></a><span style="color: green;">&#41;</span>
<span style="color: #06c; font-weight: bold;">type</span> Nat = FixF NatF
&nbsp;
<span style="color: #06c; font-weight: bold;">instance</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Functor"><span style="background-color: #efefbf; font-weight: bold;">Functor</span></a> NatF <span style="color: #06c; font-weight: bold;">where</span>
     <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> f Z = Z
     <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> f <span style="color: green;">&#40;</span>S z<span style="color: green;">&#41;</span> = S <span style="color: green;">&#40;</span>f z<span style="color: green;">&#41;</span>
&nbsp;
plus :: Nat -&gt; Nat -&gt; Nat
plus n = cata phi <span style="color: #06c; font-weight: bold;">where</span>
     phi Z = n
     phi <span style="color: green;">&#40;</span>S m<span style="color: green;">&#41;</span> = s m
&nbsp;
times :: Nat -&gt; Nat -&gt; Nat
times n = cata phi <span style="color: #06c; font-weight: bold;">where</span>
     phi Z = z
     phi <span style="color: green;">&#40;</span>S m<span style="color: green;">&#41;</span> = plus n m
&nbsp;
z :: Nat
z = InF Z
&nbsp;
s :: Nat -&gt; Nat
s = InF . S
&nbsp;</pre>
<p><b>Links</b><br />
[<a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Morphism-Cata.html">Haddock</a>] [<a href="http://comonad.com/haskell/category-extras/src/Control/Morphism/Cata.hs">Source</a>] [<a href="http://comonad.com/reader/2008/recursion-schemes/">Field Guide</a>]</p>
<p><b>References</b></p>
<p>[1] L. Meertens. First Steps towards the theory of Rose Trees. Draft Report, CWI, Amsterdam, 1987.<br />
[2] G. Malcolm. PhD Thesis. University of Gronigen, 1990.<br />
[3] G. Malcolm. Data structures and program transformation. Science of Computer Programming, 14:255--279, 1990.<br />
[4] E. Meijer. Calculating Compilers, Ph.D thesis, Utrecht State University 1992.<br />
[5] E. Meijer, M. Fokkinga, R. Paterson, Functional Programming with Bananas, Lenses, Envelopes and Barbed Wire. Proceedings, 5th ACM Conference on Functional Programming Languages and Computer Architecture.
</p>
]]></content:encoded>
			<wfw:commentRSS>http://comonad.com/reader/2008/catamorphism/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Recursion Schemes: A Field Guide</title>
		<link>http://comonad.com/reader/2008/recursion-schemes/</link>
		<comments>http://comonad.com/reader/2008/recursion-schemes/#comments</comments>
		<pubDate>Mon, 23 Jun 2008 03:18:45 +0000</pubDate>
		<dc:creator>Edward Kmett</dc:creator>
		
	<category>Haskell</category>
	<category>Category Theory</category>
	<category>Mathematics</category>
	<category>Recursion Schemes</category>
		<guid isPermaLink="false">http://comonad.com/reader/2008/recursion-schemes/</guid>
		<description><![CDATA[I talk an awful lot about different recursion schemes in this blog. 
What I want to do for the next few posts is taxonomize a list of the general recursion schemes I have seen, cite the original references for them where available, and provide a concise implementation of each and try to motivate the connections [...]]]></description>
			<content:encoded><![CDATA[<p>I talk an awful lot about different recursion schemes in this blog. </p>
<p>What I want to do for the next few posts is taxonomize a list of the general recursion schemes I have seen, cite the original references for them where available, and provide a concise implementation of each and try to motivate the connections between them. I'll likely go back through and add more information or post motivating examples for each as I work.</p>
<p>The following recursion schemes can be found in <a href="http://hackage.haskell.org/cgi-bin/hackage-scripts/package/category-extras">category-extras</a>, along with variations on the underlying themes, so this should work as a punch-list. I'll update this post with links as I go.</p>
<table>
<tr>
<th colspan="3">Folds</th>
</tr>
<tr>
<th>Scheme</th>
<th>Code</th>
<th>Description</th>
</tr>
<tr>
<td><a href="http://comonad.com/reader/2008/catamorphism">catamorphism</a>†</td>
<td><a href="http://comonad.com/haskell/category-extras/src/Control/Morphism/Cata.hs">Cata</a></td>
<td>tears down a structure level by level</td>
</tr>
<tr>
<td><a href="http://comonad.com/reader/2008/paramorphism">paramorphism</a>*†</td>
<td><a href="http://comonad.com/haskell/category-extras/src/Control/Morphism/Para.hs">Para</a></td>
<td>tears down a structure with primitive recursion</td>
</tr>
<tr>
<td>zygomorphism*†</td>
<td><a href="http://comonad.com/haskell/category-extras/src/Control/Morphism/Zygo.hs">Zygo</a></td>
<td>tears down a structure with the aid of a helper function</td>
</tr>
<tr>
<td>histomorphism†</td>
<td><a href="http://comonad.com/haskell/category-extras/src/Control/Morphism/Histo.hs">Histo</a></td>
<td>tears down a structure with the aid of the previous answers it has given.</td>
</tr>
<tr>
<td>prepromorphism*†</td>
<td><a href="http://comonad.com/haskell/category-extras/src/Control/Morphism/Prepro.hs">Prepro</a></td>
<td>tears down a structure after repeatedly transforming it</td>
</tr>
<tr>
<th colspan="3">Unfolds</th>
</tr>
<tr>
<th>Scheme</th>
<th>Code</th>
<th>Description</th>
</tr>
<tr>
<td><a href="http://comonad.com/reader/2008/anamorphism">anamorphism</a>†</td>
<td><a href="http://comonad.com/haskell/category-extras/src/Control/Morphism/Ana.hs">Ana</a></td>
<td>builds up a structure level by level</td>
</tr>
<tr>
<td>apomorphism*†</td>
<td><a href="http://comonad.com/haskell/category-extras/src/Control/Morphism/Apo.hs">Apo</a></td>
<td>builds up a structure opting to return a single level or an entire branch at each point</td>
</tr>
<tr>
<td>futumorphism†</td>
<td><a href="http://comonad.com/haskell/category-extras/src/Control/Morphism/Futu.hs">Futu</a></td>
<td>builds up a structure multiple levels at a time</td>
</tr>
<tr>
<td>postpromorphism*†</td>
<td><a href="http://comonad.com/haskell/category-extras/src/Control/Morphism/Postpro.hs">Postpro</a></td>
<td>builds up a structure and repeatedly transforms it</td>
</tr>
<tr>
<th colspan="3">Refolds</th>
</tr>
<tr>
<th>Scheme</th>
<th>Code</th>
<th>Description</th>
</tr>
<tr>
<td>hylomorphism†</td>
<td><a href="http://comonad.com/haskell/category-extras/src/Control/Morphism/Hylo.hs">Hylo</a></td>
<td>builds up and tears down a virtual structure</td>
</tr>
<tr>
<td>chronomorphism†</td>
<td><a href="http://comonad.com/haskell/category-extras/src/Control/Morphism/Chrono.hs">Chrono</a></td>
<td>builds up a virtual structure with a futumorphism and tears it down with a histomorphism</td>
</tr>
<tr>
<td>synchromorphism</td>
<td><a href="http://comonad.com/haskell/category-extras/src/Control/Morphism/Synchro.hs">Synchro</a></td>
<td>a high level transformation between data structures using a third data structure to queue intermediate results</td>
</tr>
<tr>
<td>exomorphism</td>
<td><a href="http://comonad.com/haskell/category-extras/src/Control/Morphism/Exo.hs">Exo</a></td>
<td>a high level transformation between data structures from a trialgebra to a bialgebra</td>
</tr>
<tr>
<td>metamorphism</td>
<td><a href="http://comonad.com/haskell/category-extras/src/Control/Morphism/Meta/Erwig.hs">Erwig</a></td>
<td>a hylomorphism expressed in terms of bialgebras</td>
</tr>
<tr>
<td>metamorphism</td>
<td><a href="http://comonad.com/haskell/category-extras/src/Control/Morphism/Meta/Gibbons.hs">Gibbons</a></td>
<td>A fold followed by an unfold; change of representation</td>
</tr>
<tr>
<td>dynamorphism†</td>
<td><a href="http://comonad.com/haskell/category-extras/src/Control/Morphism/Dyna.hs">Dyna</a></td>
<td>builds up a virtual structure with an anamorphism and tears it down with a histomorphism</td>
</tr>
<tr>
<td>Elgot algebra</td>
<td><a href="http://comonad.com/haskell/category-extras/src/Control/Functor/Algebra/Elgot.hs">Elgot</a></td>
<td>builds up a structure and tears it down but may shortcircuit the process during construction</td>
</tr>
<tr>
<td>Elgot coalgebra</td>
<td><a href="http://comonad.com/haskell/category-extras/src/Control/Functor/Algebra/Elgot.hs">Elgot</a></td>
<td>builds up a structure and tears it down but may shortcircuit the process during deconstruction</td>
</tr>
</table>
<p>* This gives rise to a family of related recursion schemes, modeled in category-extras with distributive law combinators.<br />
† The scheme can be generalized to accept one or more F-distributive (co)monads.</p>
<p><a id="more-69"></a></p>
<h2>Haskell preliminaries</h2>
<p>We'll need a few preliminary types. </p>
<p>First, we need to be able to take the fixed point of a functor. Since the least and greatest fixpoints of a functor coincide in Haskell, we just call this 'Fix' rather than Mu or Nu. </p>
<pre class="haskell">&nbsp;
<span style="color: #06c; font-weight: bold;">data</span> FixF f = InF <span style="color: green;">&#123;</span> outF :: f <span style="color: green;">&#40;</span>FixF f<span style="color: green;">&#41;</span> <span style="color: green;">&#125;</span>
&nbsp;</pre>
<p>Second, we'll need to define F-algebras and F-coalgebras.</p>
<pre class="haskell">&nbsp;
<span style="color: #06c; font-weight: bold;">type</span> Algebra f a = f a -&gt; a
<span style="color: #06c; font-weight: bold;">type</span> Coalgebra f a = a -&gt; f a
&nbsp;</pre>
<p>Finally, we need a slight generalization of these. For a comonad W, an F-W-Algebra takes the form:</p>
<pre class="haskell">&nbsp;
<span style="color: #06c; font-weight: bold;">type</span> GAlgebra f w a = f <span style="color: green;">&#40;</span>w a<span style="color: green;">&#41;</span> -&gt; a
&nbsp;</pre>
<p>And similarly for a monad M, an F-M-coalgebra has the form:</p>
<pre class="haskell">&nbsp;
<span style="color: #06c; font-weight: bold;">type</span> GCoalgebra f m a = a -&gt; f <span style="color: green;">&#40;</span>m a<span style="color: green;">&#41;</span>
&nbsp;</pre>
<p>The intended use of GAlgebra and GCoalgebra is that you use them with F-distributive comonads and monads respectively in the sense of Uustalu, Vene and Pardo [1].</p>
<p>We can obtain all of these by importing from <a href="http://hackage.haskell.org/cgi-bin/hackage-scripts/package/category-extras">category-extras</a>.</p>
<pre class="haskell">&nbsp;
<span style="color: #06c; font-weight: bold;">import</span> Control.<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Functor"><span style="background-color: #efefbf; font-weight: bold;">Functor</span></a>.Fix
<span style="color: #06c; font-weight: bold;">import</span> Control.<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Functor"><span style="background-color: #efefbf; font-weight: bold;">Functor</span></a>.Algebra
&nbsp;</pre>
<p><b>References</b></p>
<p>[1] T. Uustalu, V. Vene, A. Pardo. Recursion Schemes from Comonads. Nordic Journal of Computing 8(2001), 366-390.</p>
]]></content:encoded>
			<wfw:commentRSS>http://comonad.com/reader/2008/recursion-schemes/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Zapping Adjunctions</title>
		<link>http://comonad.com/reader/2008/zapping-strong-adjunctions/</link>
		<comments>http://comonad.com/reader/2008/zapping-strong-adjunctions/#comments</comments>
		<pubDate>Thu, 05 Jun 2008 19:29:05 +0000</pubDate>
		<dc:creator>Edward Kmett</dc:creator>
		
	<category>Haskell</category>
	<category>Category Theory</category>
		<guid isPermaLink="false">http://comonad.com/reader/2008/zapping-strong-adjunctions/</guid>
		<description><![CDATA[As you may recall, every functor in Haskell is strong, in the sense that if you provided an instance of Monad for that functor the following definition would satisfy the requirements mentioned here:
&#160;
strength :: Functor f =&#62; a -&#62; f b -&#62; f &#40;a,b&#41;
strength = fmap . &#40;,&#41;
&#160;
In an earlier post about the cofree comonad [...]]]></description>
			<content:encoded><![CDATA[<p>As you may recall, every functor in Haskell is strong, in the sense that if you provided an instance of Monad for that functor the following definition would satisfy the requirements mentioned <a href="http://en.wikipedia.org/wiki/Strong_monad">here</a>:</p>
<pre class="haskell">&nbsp;
strength :: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Functor"><span style="background-color: #efefbf; font-weight: bold;">Functor</span></a> f =&gt; a -&gt; f b -&gt; f <span style="color: green;">&#40;</span>a,b<span style="color: green;">&#41;</span>
strength = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> . <span style="color: green;">&#40;</span>,<span style="color: green;">&#41;</span>
&nbsp;</pre>
<p>In an earlier post about <a href="http://comonad.com/reader/2008/the-cofree-comonad-and-the-expression-problem/">the cofree comonad and the expression problem</a>, I used a typeclass defining a form of duality that enables you to let two functors annihilate each other, letting one select the path whenever the other offered up multiple options. To have a shared set of conventions with the material in <a href="http://comonad.com/reader/2008/zipping-and-unzipping-functors/">Zipping and Unzipping Functors</a>, I have since remodeled that class slightly:</p>
<pre class="haskell">&nbsp;
<span style="color: #06c; font-weight: bold;">class</span> Zap f g | f -&gt; g, g -&gt; f <span style="color: #06c; font-weight: bold;">where</span>
	zapWith :: <span style="color: green;">&#40;</span>a -&gt; b -&gt; c<span style="color: green;">&#41;</span> -&gt; f a -&gt; g b -&gt; c
	zap :: f <span style="color: green;">&#40;</span>a -&gt; b<span style="color: green;">&#41;</span> -&gt; g a -&gt; b
	zap = zapWith <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:id"><span style="font-weight: bold;">id</span></a>
&nbsp;</pre>
<p>Interestingly, we can use the fact that every functor in Haskell is strong to derive not only one instance of <code>Zap</code>, but two, given any <code>Adjunction</code> <img src='http://comonad.com/latex/e0edd4e054355b0c1e7c7c75a7b59336.png' title='$f \dashv g$' alt='$f \dashv g$' align=absmiddle>.</p>
<p>I'll give one instance here:</p>
<pre class="haskell">&nbsp;
zapWithAdjunctionGF :: <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Adjunction.html#t:Adjunction"><span style="background-color: #efefbf; font-weight: bold;">Adjunction</span></a> g f =&gt;
    <span style="color: green;">&#40;</span>a -&gt; b -&gt; c<span style="color: green;">&#41;</span> -&gt; f a -&gt; g b -&gt; c
zapWithAdjunctionGF f a b =
    <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:uncurry"><span style="font-weight: bold;">uncurry</span></a> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:flip"><span style="font-weight: bold;">flip</span></a> f<span style="color: green;">&#41;</span> . <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Adjunction.html#v:counit"><span style="font-weight: bold;">counit</span></a> . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:uncurry"><span style="font-weight: bold;">uncurry</span></a> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:flip"><span style="font-weight: bold;">flip</span></a> strength<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> $
    strength a b
&nbsp;</pre>
<p>And I'll leave the substantially similar derivation of the following to the reader:</p>
<pre class="haskell">&nbsp;
zapWithAdjunctionFG :: <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Adjunction.html#t:Adjunction"><span style="background-color: #efefbf; font-weight: bold;">Adjunction</span></a> f g =&gt; <span style="color: green;">&#40;</span>a -&gt; b -&gt; c<span style="color: green;">&#41;</span> -&gt; f a -&gt; g b -&gt; c
&nbsp;</pre>
<p>Now, we would like to do the same thing we did with <code>Representable</code> <a href="http://comonad.com/reader/2008/representing-adjunctions/">last time</a>, and just require the user of the <code>Adjunction</code> class to provide us with more instances, something like:</p>
<pre class="haskell">&nbsp;
<span style="color: #06c; font-weight: bold;">class</span> <span style="color: green;">&#40;</span>Zap f g, Zap g f, Representable g <span style="color: green;">&#40;</span>f Void<span style="color: green;">&#41;</span>, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Functor"><span style="background-color: #efefbf; font-weight: bold;">Functor</span></a> f<span style="color: green;">&#41;</span> =&gt;
    <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Adjunction.html#t:Adjunction"><span style="background-color: #efefbf; font-weight: bold;">Adjunction</span></a> f g | f -&gt; g, g -&gt; f <span style="color: #06c; font-weight: bold;">where</span>
        ...
&nbsp;</pre>
<p>But there is a problem: Adjunction composition.</p>
<p>If you will recall from before, we were able to define an instance for an <code>Adjunction</code> for a composition of two adjunctions:</p>
<pre class="haskell">&nbsp;
<span style="color: #06c; font-weight: bold;">newtype</span> O f g a = Compose <span style="color: green;">&#123;</span> decompose :: f <span style="color: green;">&#40;</span>g a<span style="color: green;">&#41;</span> <span style="color: green;">&#125;</span>
&nbsp;
<span style="color: #06c; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Functor"><span style="background-color: #efefbf; font-weight: bold;">Functor</span></a> f, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Functor"><span style="background-color: #efefbf; font-weight: bold;">Functor</span></a> g<span style="color: green;">&#41;</span> =&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Functor"><span style="background-color: #efefbf; font-weight: bold;">Functor</span></a> <span style="color: green;">&#40;</span>f `O` g<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span>
        <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> f = Compose . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> f<span style="color: green;">&#41;</span> . decompose
&nbsp;
<span style="color: #06c; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span><a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Adjunction.html#t:Adjunction"><span style="background-color: #efefbf; font-weight: bold;">Adjunction</span></a> f1 g1, <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Adjunction.html#t:Adjunction"><span style="background-color: #efefbf; font-weight: bold;">Adjunction</span></a> f2 g2<span style="color: green;">&#41;</span> =&gt;
    <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Adjunction.html#t:Adjunction"><span style="background-color: #efefbf; font-weight: bold;">Adjunction</span></a> <span style="color: green;">&#40;</span>f2 `O` f1<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>g1 `O` g2<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span>
        <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Adjunction.html#v:counit"><span style="font-weight: bold;">counit</span></a> =
               <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Adjunction.html#v:counit"><span style="font-weight: bold;">counit</span></a> .
               <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> <span style="color: green;">&#40;</span><a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Adjunction.html#v:counit"><span style="font-weight: bold;">counit</span></a> . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> decompose<span style="color: green;">&#41;</span> .
               decompose
        <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Adjunction.html#v:unit"><span style="font-weight: bold;">unit</span></a> =
               Compose .
               <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> Compose . <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Adjunction.html#v:unit"><span style="font-weight: bold;">unit</span></a><span style="color: green;">&#41;</span> .
               <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Adjunction.html#v:unit"><span style="font-weight: bold;">unit</span></a>
&nbsp;</pre>
<p>The problem is that we have <strong>three</strong> different ways to build an instance of <code>Zap</code> for this composition and we would need to define two of them that conflict!</p>
<p>We would need both of these, which would lead to ambiguous instance heads:</p>
<pre class="haskell">&nbsp;
<span style="color: #06c; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span><a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Adjunction.html#t:Adjunction"><span style="background-color: #efefbf; font-weight: bold;">Adjunction</span></a> f1 g1, <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Adjunction.html#t:Adjunction"><span style="background-color: #efefbf; font-weight: bold;">Adjunction</span></a> f2 g2<span style="color: green;">&#41;</span> =&gt;
    Zap <span style="color: green;">&#40;</span>f2 `O` f1<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>g1 `O` g2<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span>
        zapWith = zapWithAdjunctionFG
<span style="color: #06c; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span><a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Adjunction.html#t:Adjunction"><span style="background-color: #efefbf; font-weight: bold;">Adjunction</span></a> f1 g1, <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Adjunction.html#t:Adjunction"><span style="background-color: #efefbf; font-weight: bold;">Adjunction</span></a> f2 g2<span style="color: green;">&#41;</span> =&gt;
    Zap <span style="color: green;">&#40;</span>g1 `O` g2<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>f2 `O` f1<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span>
        zapWith = zapWithAdjunctionGF
&nbsp;</pre>
<p>Furthermore, we can also define a third instance of <code>Zap</code> over composition, which doesn't even care about <code>Adjunction</code>, which also conflicts with the above:</p>
<pre class="haskell">&nbsp;
<span style="color: #06c; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span>Zap f g, Zap h k<span style="color: green;">&#41;</span> =&gt; Zap <span style="color: green;">&#40;</span>f `O` h<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>g `O` k<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span>
    zapWith f a b =
        zapWith <span style="color: green;">&#40;</span>zapWith f<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>decompose a<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>decompose b<span style="color: green;">&#41;</span>
&nbsp;</pre>
<p>We could use the standard Haskell trick of making different compositions based on which instance of <code>Zap</code> you want to support, but the combinatorial explosion of constructors here when combined with the other reasons you may want to compose a pair of functors leads to a bit of absurdity, especially since I'm using it to capture a relationship no one cares about.</p>
<p>Consequently, <a href="http://comonad.com/haskell/category-extras/">category-extras</a> does not capture this constraint.</p>
<p><b>Cozapping</b></p>
<p>As a final aside, we noted previously that <code>Traversable</code> functors were costrong. If a strong <code>Adjunction</code> gives rise to a couple of instances of <code>Zap</code>, we'd expect a similar relationship between a notion of <code>Cozap</code> and a costrong <code>Adjunction</code>.</p>
<p>But what would cozapping be?</p>
<p>First lets take a step back and break down zipWith into a couple of steps. If we note that <code>zapWith (,)</code> looks like:</p>
<pre class="haskell">&nbsp;
prezapAdjunctionGF :: <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Adjunction.html#t:Adjunction"><span style="background-color: #efefbf; font-weight: bold;">Adjunction</span></a> g f =&gt; f a -&gt; g b -&gt; <span style="color: green;">&#40;</span>a,b<span style="color: green;">&#41;</span>
prezapAdjunctionGF a b =
    swap . <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Adjunction.html#v:counit"><span style="font-weight: bold;">counit</span></a> . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:uncurry"><span style="font-weight: bold;">uncurry</span></a> strength . swap<span style="color: green;">&#41;</span> $ strength a b
    <span style="color: #06c; font-weight: bold;">where</span> swap ~<span style="color: green;">&#40;</span>a,b<span style="color: green;">&#41;</span> = <span style="color: green;">&#40;</span>b,a<span style="color: green;">&#41;</span>
&nbsp;</pre>
<p>Whereupon we can run the output through the canonical eval morphism for exponentials in <img src='http://comonad.com/latex/129cf6e597bd76a4da1b575b1fa73b20.png' title='$\mathbf{Hask}$' alt='$\mathbf{Hask}$' align=absmiddle>:</p>
<pre class="haskell">&nbsp;
eval :: <span style="color: green;">&#40;</span>a -&gt; b, a<span style="color: green;">&#41;</span> -&gt; b
eval <span style="color: green;">&#40;</span>f,a<span style="color: green;">&#41;</span> = f a
&nbsp;</pre>
<p>We can run everything backwards (modulo the noise caused by currying) in the first definition above and get:</p>
<pre class="haskell">&nbsp;
precozapAdjunctionFG ::
    <span style="color: green;">&#40;</span><a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Adjunction.html#t:Adjunction"><span style="background-color: #efefbf; font-weight: bold;">Adjunction</span></a> f g, Traversable f, Traversable g<span style="color: green;">&#41;</span> =&gt;
    <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Either"><span style="background-color: #efefbf; font-weight: bold;">Either</span></a> a b -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Either"><span style="background-color: #efefbf; font-weight: bold;">Either</span></a> <span style="color: green;">&#40;</span>f a<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>g b<span style="color: green;">&#41;</span>
precozapAdjunctionFG =
    costrength . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> <span style="color: green;">&#40;</span>swap . costrength<span style="color: green;">&#41;</span> . <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Adjunction.html#v:unit"><span style="font-weight: bold;">unit</span></a> . swap
&nbsp;</pre>
<p>However, we lack a <code>coeval</code> morphism for <a href="http://citeseer.ist.psu.edu/filinski89declarative.html">coexponentials</a>, since <img src='http://comonad.com/latex/129cf6e597bd76a4da1b575b1fa73b20.png' title='$\mathbf{Hask}$' alt='$\mathbf{Hask}$' align=absmiddle> lacks coexponentials -- with good reason! If <img src='http://comonad.com/latex/129cf6e597bd76a4da1b575b1fa73b20.png' title='$\mathbf{Hask}$' alt='$\mathbf{Hask}$' align=absmiddle> was a co-CCC then it would degenerate to a rather boring poset.</p>
<p>But that said, even getting this far, how many adjunctions are there between <code>Traversable</code> functors in Haskell, really?
</p>
]]></content:encoded>
			<wfw:commentRSS>http://comonad.com/reader/2008/zapping-strong-adjunctions/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Representing Adjunctions</title>
		<link>http://comonad.com/reader/2008/representing-adjunctions/</link>
		<comments>http://comonad.com/reader/2008/representing-adjunctions/#comments</comments>
		<pubDate>Thu, 05 Jun 2008 16:48:53 +0000</pubDate>
		<dc:creator>Edward Kmett</dc:creator>
		
	<category>Haskell</category>
	<category>Category Theory</category>
		<guid isPermaLink="false">http://comonad.com/reader/2008/representing-adjunctions/</guid>
		<description><![CDATA[I've had a few people ask me questions about Adjunctions since my recent post and a request for some more introductory material, so I figured I would take a couple of short posts to tie Adjunctions to some other concepts. 
Representable Functors
A covariant functor  is said to be representable by an object  if [...]]]></description>
			<content:encoded><![CDATA[<p>I've had a few people ask me questions about Adjunctions since my <a href="http://comonad.com/reader/2008/kan-extensions-ii/">recent post</a> and a request for some more introductory material, so I figured I would take a couple of short posts to tie Adjunctions to some other concepts. </p>
<p><b>Representable Functors</b></p>
<p>A covariant functor <img src='http://comonad.com/latex/33758b56f2465f3a2dca572d3dde1dcb.png' title='$F : \mathcal{C} -&gt; \mathbf{Set}$' alt='$F : \mathcal{C} -&gt; \mathbf{Set}$' align=absmiddle> is said to be <a href="http://en.wikipedia.org/wiki/Representable_functor">representable</a> by an object <img src='http://comonad.com/latex/bc4271643b2d67fbae80b362be29e936.png' title='$x \in \mathcal{C}$' alt='$x \in \mathcal{C}$' align=absmiddle> if it is naturally isomorphic to <img src='http://comonad.com/latex/084aae1f3de3245112b5152187542ed6.png' title='$\mathbf{Hom}_C(x,-)$' alt='$\mathbf{Hom}_C(x,-)$' align=absmiddle>.</p>
<p>We can translate that into Haskell, letting <img src='http://comonad.com/latex/129cf6e597bd76a4da1b575b1fa73b20.png' title='$\mathbf{Hask}$' alt='$\mathbf{Hask}$' align=absmiddle> play the role of <img src='http://comonad.com/latex/60e8ad4206d900f0a34a79b144d80097.png' title='$\mathbf{Set}$' alt='$\mathbf{Set}$' align=absmiddle> with:</p>
<pre class="haskell">&nbsp;
<span style="color: #06c; font-weight: bold;">class</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Functor"><span style="background-color: #efefbf; font-weight: bold;">Functor</span></a> f =&gt; Representable f x <span style="color: #06c; font-weight: bold;">where</span>
    rep :: <span style="color: green;">&#40;</span>x -&gt; a<span style="color: green;">&#41;</span> -&gt; f a
    unrep :: f a -&gt; <span style="color: green;">&#40;</span>x -&gt; a<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">{-# RULES
&quot;rep/unrep&quot; rep . unrep = id
&quot;unrep/rep&quot; unrep . rep = id
 #-}</span>
&nbsp;</pre>
<p>It is trivial to show that any two representations of a given functor must be isomorphic, and that there is a natural isomorphism between any two functors with the same representation, so we could strengthen the signature of the type class above by adding a pair of functional dependencies: f -> x, x -> f, but lets work without this straightjacket for now.</p>
<p><b>Example</b></p>
<p>We can represent the anonymous reader monad with its environment.</p>
<pre class="haskell">&nbsp;
<span style="color: #06c; font-weight: bold;">instance</span> Representable <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>-&gt;<span style="color: green;">&#41;</span>x<span style="color: green;">&#41;</span> x <span style="color: #06c; font-weight: bold;">where</span>
    rep = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:id"><span style="font-weight: bold;">id</span></a>
    unrep = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:id"><span style="font-weight: bold;">id</span></a>
&nbsp;</pre>
<p><b>Example</b></p>
<p>We could adopt the pleasant fiction that () has a single inhabitant to avoid bringing in an empty type, but lets do this correctly. Clearly the Identity functor needs no extra information from its representation.</p>
<pre class="haskell">&nbsp;
<span style="color: #06c; font-weight: bold;">data</span> Void <span style="color: #5d478b; font-style: italic;">{- you'll need EmptyDataDecls -}</span>
&nbsp;
<span style="color: #06c; font-weight: bold;">instance</span> Representable Identity Void <span style="color: #06c; font-weight: bold;">where</span>
    rep f = Identity <span style="color: green;">&#40;</span>f <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:undefined"><span style="font-weight: bold;">undefined</span></a><span style="color: green;">&#41;</span>
    unrep = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:const"><span style="font-weight: bold;">const</span></a> . runIdentity
&nbsp;</pre>
<p><b>Adjunctions</b></p>
<p>If you recall the definition of Adjunctions over <img src='http://comonad.com/latex/129cf6e597bd76a4da1b575b1fa73b20.png' title='$\mathbf{Hask}$' alt='$\mathbf{Hask}$' align=absmiddle> from before:</p>
<pre class="haskell">&nbsp;
<span style="color: #06c; font-weight: bold;">class</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Functor"><span style="background-color: #efefbf; font-weight: bold;">Functor</span></a> f, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Functor"><span style="background-color: #efefbf; font-weight: bold;">Functor</span></a> g<span style="color: green;">&#41;</span> =&gt;
    <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Adjunction.html#t:Adjunction"><span style="background-color: #efefbf; font-weight: bold;">Adjunction</span></a> f g | f -&gt; g, g -&gt; f <span style="color: #06c; font-weight: bold;">where</span>
        <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Adjunction.html#v:unit"><span style="font-weight: bold;">unit</span></a>   :: a -&gt; g <span style="color: green;">&#40;</span>f a<span style="color: green;">&#41;</span>
        <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Adjunction.html#v:counit"><span style="font-weight: bold;">counit</span></a> :: f <span style="color: green;">&#40;</span>g a<span style="color: green;">&#41;</span> -&gt; a
        <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Adjunction.html#v:leftAdjunct"><span style="font-weight: bold;">leftAdjunct</span></a>  :: <span style="color: green;">&#40;</span>f a -&gt; b<span style="color: green;">&#41;</span> -&gt; a -&gt; g b
        <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Adjunction.html#v:rightAdjunct"><span style="font-weight: bold;">rightAdjunct</span></a> :: <span style="color: green;">&#40;</span>a -&gt; g b<span style="color: green;">&#41;</span> -&gt; f a -&gt; b
&nbsp;
        <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Adjunction.html#v:unit"><span style="font-weight: bold;">unit</span></a> = <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Adjunction.html#v:leftAdjunct"><span style="font-weight: bold;">leftAdjunct</span></a> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:id"><span style="font-weight: bold;">id</span></a>
        <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Adjunction.html#v:counit"><span style="font-weight: bold;">counit</span></a> = <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Adjunction.html#v:rightAdjunct"><span style="font-weight: bold;">rightAdjunct</span></a> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:id"><span style="font-weight: bold;">id</span></a>
        <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Adjunction.html#v:leftAdjunct"><span style="font-weight: bold;">leftAdjunct</span></a> f = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> f . <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Adjunction.html#v:unit"><span style="font-weight: bold;">unit</span></a>
        <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Adjunction.html#v:rightAdjunct"><span style="font-weight: bold;">rightAdjunct</span></a> f = <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Adjunction.html#v:counit"><span style="font-weight: bold;">counit</span></a> . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> f
&nbsp;</pre>
<p>We can generate a lot of representable functors, by turning to the theorem mentioned in <a href="http://en.wikipedia.org/wiki/Representable_functor#Left_adjoint">the Wikipedia article</a> about the representability of a right adjoint in terms of its left adjoint wrapped around a singleton element: </p>
<blockquote><p>
Any functor <img src='http://comonad.com/latex/992ca4d6e7a041ad8f83a91a766da629.png' title='$K : \mathcal{C} -&gt; \mathbf{Set}$' alt='$K : \mathcal{C} -&gt; \mathbf{Set}$' align=absmiddle> with a left adjoint <img src='http://comonad.com/latex/83acee5a42088b1ed44d8b5b98e9f1a2.png' title='$F : \mathbf{Set} -&gt; \mathcal{C}$' alt='$F : \mathbf{Set} -&gt; \mathcal{C}$' align=absmiddle> is represented by (FX, ηX(•)) where X = {•} is a singleton set and η is the unit of the adjunction.
</p></blockquote>
<p>Well, earlier we defined a singleton set, <code>Void</code>, and <img src='http://comonad.com/latex/129cf6e597bd76a4da1b575b1fa73b20.png' title='$\mathbf{Hask}$' alt='$\mathbf{Hask}$' align=absmiddle> can play the role of <img src='http://comonad.com/latex/60e8ad4206d900f0a34a79b144d80097.png' title='$\mathbf{Set}$' alt='$\mathbf{Set}$' align=absmiddle> as we did above. For <img src='http://comonad.com/latex/8d340a729769514f33c9ee93c4416e59.png' title='$\mathcal{C} = \mathbf{Hask}$' alt='$\mathcal{C} = \mathbf{Hask}$' align=absmiddle>, we can translate the remainder quite easily:</p>
<pre class="haskell">&nbsp;
repAdjunction :: <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Adjunction.html#t:Adjunction"><span style="background-color: #efefbf; font-weight: bold;">Adjunction</span></a> f g =&gt; <span style="color: green;">&#40;</span>f Void -&gt; a<span style="color: green;">&#41;</span> -&gt; g a
repAdjunction f = <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Adjunction.html#v:leftAdjunct"><span style="font-weight: bold;">leftAdjunct</span></a> f <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:undefined"><span style="font-weight: bold;">undefined</span></a>
&nbsp;
unrepAdjunction :: <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Adjunction.html#t:Adjunction"><span style="background-color: #efefbf; font-weight: bold;">Adjunction</span></a> f g =&gt; g a -&gt; <span style="color: green;">&#40;</span>f Void -&gt; a<span style="color: green;">&#41;</span>
unrepAdjunction = <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Adjunction.html#v:rightAdjunct"><span style="font-weight: bold;">rightAdjunct</span></a> . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:const"><span style="font-weight: bold;">const</span></a>
&nbsp;</pre>
<p>Now, as usual the way type class inference works in Haskell requires us to reason somewhat backwards.</p>
<p>You'd like to say:</p>
<pre class="haskell">&nbsp;
<span style="color: #06c; font-weight: bold;">instance</span> <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Adjunction.html#t:Adjunction"><span style="background-color: #efefbf; font-weight: bold;">Adjunction</span></a> f g =&gt; Representable g <span style="color: green;">&#40;</span>f Void<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span>
        rep = repAdjunction;
        unrep = unrepAdjunction
&nbsp;</pre>
<p>But if you do so, you can't define any other instances for <code>Representable</code>, you'll have used up the instance head, so the previous definitions couldn't be made.</p>
<p>On the other hand, you can create the obligation for an appropriate instance of <code>Representable</code> by changing the signature of <code>Adjunction</code>:</p>
<pre class="haskell">&nbsp;
<span style="color: #06c; font-weight: bold;">class</span> <span style="color: green;">&#40;</span>Representable g <span style="color: green;">&#40;</span>f Void<span style="color: green;">&#41;</span>, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Functor"><span style="background-color: #efefbf; font-weight: bold;">Functor</span></a> f<span style="color: green;">&#41;</span> =&gt;
    <a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Adjunction.html#t:Adjunction"><span style="background-color: #efefbf; font-weight: bold;">Adjunction</span></a> f g | f -&gt; g, g -&gt; f <span style="color: #06c; font-weight: bold;">where</span>
        ...
&nbsp;</pre>
<p>Then the definitions for <code>repAdjunction</code> and <code>unrepAdjunction</code> can be used by any would-be <code>Adjunction</code> to automatically generate the corresponding <code>Representable</code> instance, just like <code>liftM</code> can alwyas be used to make a Haskell <code>Monad</code> into a <code>Functor</code>.</p>
<p>We can also go the other way and define an <code>Adjunction</code> given a representation for the right adjoint, but I'll leave that as an exercise for the reader. (Hint: you'll probably want to weaken the signature for Adjunction to remove the fundeps, so you can test some simple cases). You may also want to take a look at the section on Adjunctions as Kan extensions portion of the <a href="http://comonad.com/reader/2008/kan-extensions-ii/">earlier post</a>.</p>
<p>I have since modified <a href="http://hackage.haskell.org/cgi-bin/hackage-scripts/package/category-extras">category-extras</a> definition of Adjunction to require the instance for Representable motivated above.</p>
<p><b>Haddock:</b><br />
[<a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Adjunction.html">Control.Functor.Adjunction</a>]<br />
[<a href="http://comonad.com/haskell/category-extras/dist/doc/html/category-extras/Control-Functor-Representable.html">Control.Functor.Representable</a>]
</p>
]]></content:encoded>
			<wfw:commentRSS>http://comonad.com/reader/2008/representing-adjunctions/feed/</wfw:commentRSS>
		</item>
	</channel>
</rss>
