<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/rss2full.xsl" type="text/xsl" media="screen"?><?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/itemcontent.css" type="text/css" media="screen"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Conal Elliott</title>
	
	<link>http://conal.net/blog</link>
	<description>Inspirations &amp; experiments, mainly about functional programming in Haskell</description>
	<pubDate>Sun, 16 Nov 2008 01:19:46 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5</generator>
	<language>en</language>
			<geo:lat>38.192823</geo:lat><geo:long>-120.642852</geo:long><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/conal" type="application/rss+xml" /><feedburner:emailServiceId>1744014</feedburner:emailServiceId><feedburner:feedburnerHostname>http://www.feedburner.com</feedburner:feedburnerHostname><item>
		<title>Enhancing a Zip</title>
		<link>http://feeds.feedburner.com/~r/conal/~3/454447926/</link>
		<comments>http://conal.net/blog/posts/enhancing-a-zip/#comments</comments>
		<pubDate>Sun, 16 Nov 2008 01:09:03 +0000</pubDate>
		<dc:creator>conal</dc:creator>
		
		<category><![CDATA[Functional programming]]></category>

		<category><![CDATA[applicative functors]]></category>

		<category><![CDATA[fold]]></category>

		<category><![CDATA[functors]]></category>

		<category><![CDATA[morphism]]></category>

		<category><![CDATA[proof]]></category>

		<category><![CDATA[zip]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=61</guid>
		<description><![CDATA[





This post is part four of the zip folding series inspired by Max Rabkin&#8217;s Beautiful folding post.
I meant to write one little post, but one post turned into four.
When I sense that something can be made simpler/clearer, I can&#8217;t leave it be.

To review:


Part One related Max&#8217;s data representation of left folds to type class morphisms, [...]]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Enhancing a Zip

Tags: applicative functors, functors, fold, zip, morphism, proof

URL: http://conal.net/blog/posts/enhancing-a-zip/

-->

<!-- references -->

<!-- teaser -->

<p>This post is part four of the zip folding series inspired by Max Rabkin&#8217;s <em><a href="http://squing.blogspot.com/2008/11/beautiful-folding.html" title="blog post by Max Rabkin">Beautiful folding</a></em> post.
I meant to write one little post, but one post turned into four.
When I sense that something can be made simpler/clearer, I can&#8217;t leave it be.</p>

<p>To review:</p>

<ul>
<li><a href="http://conal.net/blog/posts/another-lovely-example-of-type-class-morphisms" title="blog post">Part One</a> related Max&#8217;s data representation of left folds to type class morphisms, a pattern that&#8217;s been steering me lately toward natural (inevitable) design of libraries.</li>
<li><a href="http://conal.net/blog/posts/more-beautiful-fold-zipping" title="blog post">Part Two</a> simplified that representation to help get to the essence of zipping, and in doing so lost the expressiveness necessary to define <code>Functor</code>and <code>Applicative</code> instaces.</li>
<li><a href="http://conal.net/blog/posts/proofs-for-left-zip-folding/" title="blog post">Part Three</a> proved the suitability of the zipping definitions in <a href="http://conal.net/blog/posts/more-beautiful-fold-zipping" title="blog post">Part Two</a>.</li>
</ul>

<p>This post shows how to restore the <code>Functor</code> and <code>Applicative</code> (very useful composition tools) to folds but does so in a way that leaves the zipping functionality untouched.
This new layer is independent of folding and can be layered onto <em>any zippable type</em>.</p>

<p>You can <a href="http://hackage.haskell.org/cgi-bin/hackage-scripts/package/ZipFold" title="the ZipFold package">get the code</a> described below.</p>

<!--
**Edits**:

* 2008-02-09: just fiddling around
-->

<!-- without a comment or something here, the last item above becomes a paragraph -->

<p><span id="more-61"></span></p>

<h3>What&#8217;s missing?</h3>

<p>Max&#8217;s fold type packages up the first two arguments of <code>foldl'</code> and adds on an post-fold step, as needed for <code>fmap</code>:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">data</span> Fold b c = <span style="color: #050; font-weight: bold;">forall</span> a. F <span style="color: green;">&#40;</span>a -&gt; b -&gt; a<span style="color: green;">&#41;</span> a <span style="color: green;">&#40;</span>a -&gt; c<span style="color: green;">&#41;</span>  <span style="color: #5d478b; font-style: italic;">&#8211; clever version</span></pre>
</div></p>

<p>The simpler, less clever version just has the two <code>foldl'</code> arguments, and no <code>forall</code>:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">data</span> Fold b a = F <span style="color: green;">&#40;</span>a -&gt; b -&gt; a<span style="color: green;">&#41;</span> a                     <span style="color: #5d478b; font-style: italic;">&#8211; simple version</span></pre>
</div></p>

<p>Removing Max&#8217;s post-fold step gets to the essence of fold zipping but loses some useful composability.
In particular, we can no longer define <code>Functor</code> and <code>Applicative</code> instances.</p>

<p>The difference between the clever version and the simple one is a continuation and a <code>forall</code>, which we can write down as follows:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">data</span> WithCont h b c = <span style="color: #050; font-weight: bold;">forall</span> a. WC <span style="color: green;">&#40;</span>h b a<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>a -&gt; c<span style="color: green;">&#41;</span></pre>
</div></p>

<p>The clever version is equivalent to the following:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> FoldC = WithCont Fold</pre>
</div></p>

<p>Interpretating a <code>FoldC</code> just interprets the <code>Fold</code> and then applies the continuation:</p>

<p><div>
<pre class="haskell">cfoldlc :: FoldC b a -&gt; <span style="color: green;">&#91;</span>b<span style="color: green;">&#93;</span> -&gt; a
cfoldlc <span style="color: green;">&#40;</span>WC f k<span style="color: green;">&#41;</span> = k . cfoldl f</pre>
</div></p>

<p>Add a copy of <code>WithCont</code>, for reasons explained later, and use it for strict folds.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">data</span> WithCont&#8217; h b c = <span style="color: #050; font-weight: bold;">forall</span> a. WC&#8217; <span style="color: green;">&#40;</span>h b a<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>a -&gt; c<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #050; font-weight: bold;">type</span> FoldC&#8217; = WithCont&#8217; Fold
&nbsp;
cfoldlc&#8217; :: FoldC&#8217; b a -&gt; <span style="color: green;">&#91;</span>b<span style="color: green;">&#93;</span> -&gt; a
cfoldlc&#8217; <span style="color: green;">&#40;</span>WC&#8217; f k<span style="color: green;">&#41;</span> = k . cfoldl&#8217; f</pre>
</div></p>

<h3>From <code>Zip</code> to <code>Functor</code> and <code>Applicative</code></h3>

<p>It&#8217;s now a simple matter to recover the lost <code>Functor</code> and <code>Applicative</code> instances, for both non-strict and strict zipping.
The <code>Applicative</code> instances rely on zippability:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Functor"><span style="color: #833; font-weight: bold;">Functor</span></a> <span style="color: green;">&#40;</span>WithCont h b<span style="color: green;">&#41;</span> <span style="color: #050; 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> g <span style="color: green;">&#40;</span>WC f k<span style="color: green;">&#41;</span> = WC f <span style="color: green;">&#40;</span>g . k<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #050; font-weight: bold;">instance</span> Zip <span style="color: green;">&#40;</span>h b<span style="color: green;">&#41;</span> =&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Applicative"><span style="color: #833; font-weight: bold;">Applicative</span></a> <span style="color: green;">&#40;</span>WithCont h b<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:pure"><span style="font-weight: bold;">pure</span></a> a = WC <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;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:const"><span style="font-weight: bold;">const</span></a> a<span style="color: green;">&#41;</span>
  WC hf hk &lt;*&gt; WC xf xk =
    WC <span style="color: green;">&#40;</span>hf `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zip"><span style="font-weight: bold;">zip</span></a>` xf<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>\ <span style="color: green;">&#40;</span>a,a&#8217;<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</span>hk a<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>xk a&#8217;<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #050; font-weight: bold;">instance</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Functor"><span style="color: #833; font-weight: bold;">Functor</span></a> <span style="color: green;">&#40;</span>WithCont&#8217; h b<span style="color: green;">&#41;</span> <span style="color: #050; 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> g <span style="color: green;">&#40;</span>WC&#8217; f k<span style="color: green;">&#41;</span> = WC&#8217; f <span style="color: green;">&#40;</span>g . k<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #050; font-weight: bold;">instance</span> Zip&#8217; <span style="color: green;">&#40;</span>h b<span style="color: green;">&#41;</span> =&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Applicative"><span style="color: #833; font-weight: bold;">Applicative</span></a> <span style="color: green;">&#40;</span>WithCont&#8217; h b<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:pure"><span style="font-weight: bold;">pure</span></a> a = WC&#8217; <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;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:const"><span style="font-weight: bold;">const</span></a> a<span style="color: green;">&#41;</span>
  WC&#8217; hf hk &lt;*&gt; WC&#8217; xf xk =
    WC&#8217; <span style="color: green;">&#40;</span>hf `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zip"><span style="font-weight: bold;">zip</span></a>&#8216;` xf<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>\ <span style="color: green;">&#40;</span>P a a&#8217;<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</span>hk a<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>xk a&#8217;<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre>
</div></p>

<p>Now the reason for the duplicate <code>WithCont</code> definition becomes apparent.
Instance selection in Haskell is based entirely on the &#8220;head&#8221; of an instance.
If both <code>Applicative</code> instances used <code>WithCont</code>, the compiler would consider them to overlap.</p>

<h3>Morphism proofs</h3>

<p>It remains to prove that our interpretation functions are <code>Functor</code> and <code>Applicative</code> morphisms.
I&#8217;ll show the non-strict proofs.
The strict morphism proofs go through similarly.</p>

<h4>Functor</h4>

<p>The <code>Functor</code> morphism property for non-strict folding:</p>

<p><div>
<pre class="haskell">cfoldlc <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> g wc<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> g <span style="color: green;">&#40;</span>cfoldlc wc<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Adding some structure:</p>

<p><div>
<pre class="haskell">cfoldlc <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> g <span style="color: green;">&#40;</span>WC f k<span style="color: green;">&#41;</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> g <span style="color: green;">&#40;</span>cfoldlc <span style="color: green;">&#40;</span>WC f k<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre>
</div></p>

<p>Proof:</p>

<p><div>
<pre class="haskell">cfoldlc <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> g <span style="color: green;">&#40;</span>WC f k<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- fmap on WithCont -}</span>
&nbsp;
cfoldlc <span style="color: green;">&#40;</span>WC f <span style="color: green;">&#40;</span>g . k<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- cfoldlc def -}</span>
&nbsp;
<span style="color: green;">&#40;</span>g . k<span style="color: green;">&#41;</span> . cfoldl f
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- associativity of (.)  -}</span>
&nbsp;
g . <span style="color: green;">&#40;</span>k . cfoldl f<span style="color: green;">&#41;</span>
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- cfoldl def -}</span>
&nbsp;
g . cfoldlc <span style="color: green;">&#40;</span>WC f k<span style="color: green;">&#41;</span>
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- fmap on functions -}</span>
&nbsp;
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> g <span style="color: green;">&#40;</span>cfoldlc <span style="color: green;">&#40;</span>WC f k<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre>
</div></p>

<h4>Applicative</h4>

<p><code>Applicative</code> has two methods, so <code>cfoldlc</code> must satisfy two morphism properties.
One for <code>pure</code>:</p>

<p><div>
<pre class="haskell">cfoldlc <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:pure"><span style="font-weight: bold;">pure</span></a> a<span style="color: green;">&#41;</span> == <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:pure"><span style="font-weight: bold;">pure</span></a> a</pre>
</div></p>

<p>Proof:</p>

<p><div>
<pre class="haskell">cfoldlc <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:pure"><span style="font-weight: bold;">pure</span></a> a<span style="color: green;">&#41;</span>
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- pure on WithCont -}</span>
&nbsp;
cfoldlc <span style="color: green;">&#40;</span>WC <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;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:const"><span style="font-weight: bold;">const</span></a> a<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- cfoldlc def -}</span>
&nbsp;
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:const"><span style="font-weight: bold;">const</span></a> a . cfoldl <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:undefined"><span style="font-weight: bold;">undefined</span></a>
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- property of const -}</span>
&nbsp;
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:const"><span style="font-weight: bold;">const</span></a> a
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- pure on functions -}</span>
&nbsp;
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:pure"><span style="font-weight: bold;">pure</span></a> a</pre>
</div></p>

<p>And one for <code>(&lt;*&gt;)</code>:</p>

<p><div>
<pre class="haskell">cfoldlc <span style="color: green;">&#40;</span>wch &lt;*&gt; wcx<span style="color: green;">&#41;</span> == cfoldlc wch &lt;*&gt; cfoldlc wcx</pre>
</div></p>

<p>Adding structure:</p>

<p><div>
<pre class="haskell">cfoldlc <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>WC hf hk<span style="color: green;">&#41;</span> &lt;*&gt; <span style="color: green;">&#40;</span>WC xf xk<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> == cfoldlc <span style="color: green;">&#40;</span>WC hf hk<span style="color: green;">&#41;</span> &lt;*&gt; cfoldlc <span style="color: green;">&#40;</span>WC xf xk<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Proof:</p>

<p><div>
<pre class="haskell">cfoldlc <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>WC hf hk<span style="color: green;">&#41;</span> &lt;*&gt; <span style="color: green;">&#40;</span>WC xf xk<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- (&lt;*&gt;) on WithCont -}</span>
&nbsp;
cfoldlc <span style="color: green;">&#40;</span>WC <span style="color: green;">&#40;</span>hf `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zip"><span style="font-weight: bold;">zip</span></a>` xf<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>\ <span style="color: green;">&#40;</span>a,a&#8217;<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</span>hk a<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>xk a&#8217;<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- cfoldlc def -}</span>
&nbsp;
<span style="color: green;">&#40;</span>\ <span style="color: green;">&#40;</span>a,a&#8217;<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</span>hk a<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>xk a&#8217;<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> . cfoldl <span style="color: green;">&#40;</span>hf `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zip"><span style="font-weight: bold;">zip</span></a>` xf<span style="color: green;">&#41;</span>
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- Zip morphism law for Fold -}</span>
&nbsp;
<span style="color: green;">&#40;</span>\ <span style="color: green;">&#40;</span>a,a&#8217;<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</span>hk a<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>xk a&#8217;<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> . <span style="color: green;">&#40;</span>cfoldl hf `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zip"><span style="font-weight: bold;">zip</span></a>` cfoldl xf<span style="color: green;">&#41;</span>
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- zip def on functions -}</span>
&nbsp;
<span style="color: green;">&#40;</span>\ <span style="color: green;">&#40;</span>a,a&#8217;<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</span>hk a<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>xk a&#8217;<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> . \ bs -&gt; <span style="color: green;">&#40;</span>cfoldl hf bs, cfoldl xf bs<span style="color: green;">&#41;</span>
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- (.) def -}</span>
&nbsp;
\ bs -&gt; <span style="color: green;">&#40;</span>hk <span style="color: green;">&#40;</span>cfoldl hf bs<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>xk <span style="color: green;">&#40;</span>cfoldl xf bs<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- (&lt;*&gt;) on functions -}</span>
&nbsp;
<span style="color: green;">&#40;</span>hk . cfoldl hf bs<span style="color: green;">&#41;</span> &lt;*&gt; <span style="color: green;">&#40;</span>xk . cfoldl xf<span style="color: green;">&#41;</span>
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- cfoldlc def, twice -}</span>
&nbsp;
cfoldlc <span style="color: green;">&#40;</span>WC hf hk<span style="color: green;">&#41;</span> &lt;*&gt; cfoldlc <span style="color: green;">&#40;</span>WC xf xk<span style="color: green;">&#41;</span></pre>
</div></p>

<p>That&#8217;s it.</p>

<p>I like that <code>WithCont</code> allows composing the morphism proofs, in addition to the implementations.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~f/conal?a=ivQQN"><img src="http://feeds.feedburner.com/~f/conal?i=ivQQN" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/conal?a=LJJBn"><img src="http://feeds.feedburner.com/~f/conal?i=LJJBn" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/conal?a=4tZcn"><img src="http://feeds.feedburner.com/~f/conal?i=4tZcn" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/conal/~4/454447926" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/enhancing-a-zip/feed/</wfw:commentRss>
		<feedburner:origLink>http://conal.net/blog/posts/enhancing-a-zip/</feedburner:origLink></item>
		<item>
		<title>Proofs for left fold zipping</title>
		<link>http://feeds.feedburner.com/~r/conal/~3/454297817/</link>
		<comments>http://conal.net/blog/posts/proofs-for-left-fold-zipping/#comments</comments>
		<pubDate>Sat, 15 Nov 2008 21:30:31 +0000</pubDate>
		<dc:creator>conal</dc:creator>
		
		<category><![CDATA[Functional programming]]></category>

		<category><![CDATA[fold]]></category>

		<category><![CDATA[proof]]></category>

		<category><![CDATA[zip]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=60</guid>
		<description><![CDATA[





The post More beautiful fold zipping showed a formulation of left-fold zipping, simplified from the ideas in Max Rabkin&#8217;s Beautiful folding.  I claimed that the semantic functions are the inevitable (natural) ones in that they preserve zipping stucture.
This post gives the promised proofs.







The correctness of fold zipping can be expressed as follows.
Is cfoldl a [...]]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Proofs for left fold zipping

Tags: fold, zip, proof

URL: http://conal.net/blog/posts/proofs-for-left-zip-folding/

-->

<!-- references -->

<!-- teaser -->

<p>The post <em><a href="http://conal.net/blog/posts/more-beautiful-fold-zipping" title="blog post">More beautiful fold zipping</a></em> showed a formulation of left-fold zipping, simplified from the ideas in Max Rabkin&#8217;s <em><a href="http://squing.blogspot.com/2008/11/beautiful-folding.html" title="blog post by Max Rabkin">Beautiful folding</a></em>.  I claimed that the semantic functions are the inevitable (natural) ones in that they preserve zipping stucture.
This post gives the promised proofs.</p>

<!--
**Edits**:

* 2008-02-09: just fiddling around
-->

<!-- without a comment or something here, the last item above becomes a paragraph -->

<p><span id="more-60"></span></p>

<p>The correctness of fold zipping can be expressed as follows.
Is <code>cfoldl</code> a <code>Zip</code> morphism, and is <code>cfoldl'</code> a <code>Zip'</code> morphism?
(See <em><a href="http://conal.net/blog/posts/more-beautiful-fold-zipping" title="blog post">More beautiful fold zipping</a></em> for definitions of these functions and classes, and see <em><a href="http://conal.net/blog/posts/simplifying-semantics-with-type-class-morphisms" title="blog post">Simplifying semantics with type class morphisms</a></em> for the notion of type class morphisms.)</p>

<h3>Non-strict</h3>

<p>The non-strict version is simpler, though less useful.
We want to prove</p>

<p><div>
<pre class="haskell">cfoldl <span style="color: green;">&#40;</span>f `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zip"><span style="font-weight: bold;">zip</span></a>` f&#8217;<span style="color: green;">&#41;</span> == cfoldl f `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zip"><span style="font-weight: bold;">zip</span></a>` cfoldl f&#8217;</pre>
</div></p>

<p>First, substitute the definition of <code>zip</code> for functions.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> Zip <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>-&gt;<span style="color: green;">&#41;</span> u<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zip"><span style="font-weight: bold;">zip</span></a> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:liftA2"><span style="font-weight: bold;">liftA2</span></a> <span style="color: green;">&#40;</span>,<span style="color: green;">&#41;</span></pre>
</div></p>

<p>This definition is a standard one for zipping applicative functors.
Given the <code>Applicative</code> instance for functions, this <code>Zip</code> instance is equivalent to</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> Zip <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>-&gt;<span style="color: green;">&#41;</span> u<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span> r `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zip"><span style="font-weight: bold;">zip</span></a>` s = \ u -&gt; <span style="color: green;">&#40;</span>r u, s u<span style="color: green;">&#41;</span></pre>
</div></p>

<p>So the <code>Zip</code> morphism property becomes</p>

<p><div>
<pre class="haskell">cfoldl <span style="color: green;">&#40;</span>f `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zip"><span style="font-weight: bold;">zip</span></a>` f&#8217;<span style="color: green;">&#41;</span> bs == <span style="color: green;">&#40;</span>cfoldl f bs, cfoldl f&#8217; bs<span style="color: green;">&#41;</span></pre>
</div></p>

<p>We&#8217;ll want some structure to our folds as well:</p>

<p><div>
<pre class="haskell">cfoldl <span style="color: green;">&#40;</span>F op e `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zip"><span style="font-weight: bold;">zip</span></a>` F op&#8217; e&#8217;<span style="color: green;">&#41;</span> bs
  == <span style="color: green;">&#40;</span>cfoldl <span style="color: green;">&#40;</span>F op e<span style="color: green;">&#41;</span> bs, cfoldl <span style="color: green;">&#40;</span>F op&#8217; e&#8217;<span style="color: green;">&#41;</span> bs<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Simplify the LHS:</p>

<p><div>
<pre class="haskell">cfoldl <span style="color: green;">&#40;</span>F op e `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zip"><span style="font-weight: bold;">zip</span></a>` F op&#8217; e&#8217;<span style="color: green;">&#41;</span> bs
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- Fold zip def -}</span>
&nbsp;
cfoldl <span style="color: green;">&#40;</span>F op&#8221; <span style="color: green;">&#40;</span>e,e&#8217;<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> bs
  <span style="color: #050; font-weight: bold;">where</span> <span style="color: green;">&#40;</span>a,a&#8217;<span style="color: green;">&#41;</span> `op&#8221;` b = <span style="color: green;">&#40;</span>a `op` b, a&#8217; `op&#8217;` b<span style="color: green;">&#41;</span>
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- cfoldl def -}</span>
&nbsp;
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a> op&#8221; <span style="color: green;">&#40;</span>e,e&#8217;<span style="color: green;">&#41;</span> bs
  <span style="color: #050; font-weight: bold;">where</span> <span style="color: green;">&#40;</span>a,a&#8217;<span style="color: green;">&#41;</span> `op&#8221;` b = <span style="color: green;">&#40;</span>a `op` b, a&#8217; `op&#8217;` b<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Then simplify the RHS:</p>

<p><div>
<pre class="haskell"><span style="color: green;">&#40;</span>cfoldl <span style="color: green;">&#40;</span>F op e<span style="color: green;">&#41;</span> bs, cfoldl <span style="color: green;">&#40;</span>F op&#8217; e&#8217;<span style="color: green;">&#41;</span> bs<span style="color: green;">&#41;</span>
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- cfoldl def -}</span>
&nbsp;
<span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a> op e bs, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a> op&#8217; e&#8217; bs<span style="color: green;">&#41;</span></pre>
</div></p>

<p>So the morphism law becomes</p>

<p><div>
<pre class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a> op&#8221; <span style="color: green;">&#40;</span>e,e&#8217;<span style="color: green;">&#41;</span> bs == <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a> op e bs, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a> op&#8217; e&#8217; bs<span style="color: green;">&#41;</span>
  <span style="color: #050; font-weight: bold;">where</span>
    <span style="color: green;">&#40;</span>a,a&#8217;<span style="color: green;">&#41;</span> `op&#8221;` b = <span style="color: green;">&#40;</span>a `op` b, a&#8217; `op&#8217;` b<span style="color: green;">&#41;</span></pre>
</div></p>

<p>We&#8217;ll prove this form inductively in the list <code>bs</code>, using the definition of <code>foldl</code>:</p>

<p><div>
<pre class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a> :: <span style="color: green;">&#40;</span>a -&gt; b -&gt; a<span style="color: green;">&#41;</span> -&gt; a -&gt; <span style="color: green;">&#91;</span>b<span style="color: green;">&#93;</span> -&gt; a
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a> op a <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>     = a
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a> op a <span style="color: green;">&#40;</span>b:bs<span style="color: green;">&#41;</span> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a> f <span style="color: green;">&#40;</span>a `op` b<span style="color: green;">&#41;</span> bs</pre>
</div></p>

<p>First, empty lists.</p>

<p><div>
<pre class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a> op&#8221; <span style="color: green;">&#40;</span>e,e&#8217;<span style="color: green;">&#41;</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> <span style="color: #050; font-weight: bold;">where</span> op&#8221; = &#8230;
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- foldl def  -}</span>
&nbsp;
<span style="color: green;">&#40;</span>e,e&#8217;<span style="color: green;">&#41;</span>
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- foldl def, twice -}</span>
&nbsp;
<span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a> op e <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:foldl"><span style="font-weight: bold;">foldl</span></a> op&#8217; e&#8217; <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span><span style="color: green;">&#41;</span></pre>
</div></p>

<p>Next, non-empty lists.</p>

<p><div>
<pre class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a> op&#8221; <span style="color: green;">&#40;</span>e,e&#8217;<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>b:bs<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span> op&#8221; = &#8230;
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- foldl def -}</span>
&nbsp;
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a> op&#8221; <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>e,e&#8217;<span style="color: green;">&#41;</span> `op&#8221;` b<span style="color: green;">&#41;</span> bs <span style="color: #050; font-weight: bold;">where</span> op&#8221; = &#8230;
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- op&#8221; definition -}</span>
&nbsp;
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a> op&#8221; <span style="color: green;">&#40;</span>e `op` b, e&#8217; `op&#8217;` b<span style="color: green;">&#41;</span> bs <span style="color: #050; font-weight: bold;">where</span> op&#8221; = &#8230;
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- induction hypothesis -}</span>
&nbsp;
<span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a> op <span style="color: green;">&#40;</span>e `op` b<span style="color: green;">&#41;</span> bs, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a> op&#8217; <span style="color: green;">&#40;</span>e&#8217; `op&#8217;` b<span style="color: green;">&#41;</span> bs<span style="color: green;">&#41;</span>
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- foldl def, twice -}</span>
&nbsp;
<span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a> op e <span style="color: green;">&#40;</span>b:bs<span style="color: green;">&#41;</span>, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a> op&#8217; e&#8217; <span style="color: green;">&#40;</span>b:bs<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre>
</div></p>

<h3>Strict</h3>

<p>The morphism law:</p>

<p><div>
<pre class="haskell">cfoldl' <span style="color: green;">&#40;</span>f `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zip"><span style="font-weight: bold;">zip</span></a>&#8216;` f&#8217;<span style="color: green;">&#41;</span> == cfoldl&#8217; f `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zip"><span style="font-weight: bold;">zip</span></a>&#8216;` cfoldl&#8217; f&#8217;</pre>
</div></p>

<p>which simplifies to</p>

<p><div>
<pre class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a>&#8216; op&#8221; <span style="color: green;">&#40;</span>P e e&#8217;<span style="color: green;">&#41;</span> bs == P <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a>&#8216; op e bs<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:foldl"><span style="font-weight: bold;">foldl</span></a>&#8216; op&#8217; e&#8217; bs<span style="color: green;">&#41;</span>
  <span style="color: #050; font-weight: bold;">where</span>
    P a a&#8217; `op&#8221;` b = P <span style="color: green;">&#40;</span>a `op` b<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>a&#8217; `op&#8217;` b<span style="color: green;">&#41;</span></pre>
</div></p>

<p>This property can be proved similarly to the non-strict version.
The new aspect is manipulating the use of <code>seq</code>.</p>

<p>Again, use induction on lists, guided by the fold definition:</p>

<p><div>
<pre class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a>&#8216; :: <span style="color: green;">&#40;</span>a -&gt; b -&gt; a<span style="color: green;">&#41;</span> -&gt; a -&gt; <span style="color: green;">&#91;</span>b<span style="color: green;">&#93;</span> -&gt; a
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a>&#8216; op a <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>     = a
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a>&#8216; op a <span style="color: green;">&#40;</span>b:bs<span style="color: green;">&#41;</span> =
  <span style="color: #050; font-weight: bold;">let</span> a&#8217; = a `op` b <span style="color: #050; font-weight: bold;">in</span> a&#8217; `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:seq"><span style="font-weight: bold;">seq</span></a>` <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a>&#8216; f a&#8217; bs</pre>
</div></p>

<p>First, empty lists.</p>

<p><div>
<pre class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a> op&#8221; <span style="color: green;">&#40;</span>P e e&#8217;<span style="color: green;">&#41;</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> <span style="color: #050; font-weight: bold;">where</span> op&#8221; = &#8230;
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- foldl def  -}</span>
&nbsp;
P e e&#8217;
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- foldl def, twice -}</span>
&nbsp;
P <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a> op e <span style="color: green;">&#91;</span><span style="color: green;">&#93;</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#v:foldl"><span style="font-weight: bold;">foldl</span></a> op&#8217; e&#8217; <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span><span style="color: green;">&#41;</span></pre>
</div></p>

<p>Next, non-empty lists.
For simplicity, I&#8217;ll inline the <code>let</code> in the definition of <code>foldl'</code> (which affects performance but not meaning):</p>

<p><div>
<pre class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a>&#8216; op&#8221; <span style="color: green;">&#40;</span>P e e&#8217;<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>b:bs<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span> op&#8221; = &#8230;
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- foldl&#8217; def -}</span>
&nbsp;
<span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>P e e&#8217;<span style="color: green;">&#41;</span> `op&#8221;` b<span style="color: green;">&#41;</span> `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:seq"><span style="font-weight: bold;">seq</span></a>`
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a>&#8216; op&#8221; <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>P e e&#8217;<span style="color: green;">&#41;</span> `op&#8221;` b<span style="color: green;">&#41;</span> bs <span style="color: #050; font-weight: bold;">where</span> op&#8221; = &#8230;
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- op&#8221; definition -}</span>
&nbsp;
P <span style="color: green;">&#40;</span>e `op` b<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>e&#8217; `op&#8217;` b<span style="color: green;">&#41;</span> `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:seq"><span style="font-weight: bold;">seq</span></a>`
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a>&#8216; op&#8221; <span style="color: green;">&#40;</span>P <span style="color: green;">&#40;</span>e `op` b<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>e&#8217; `op&#8217;` b<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> bs
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- induction hypothesis -}</span>
&nbsp;
P <span style="color: green;">&#40;</span>e `op` b<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>e&#8217; `op&#8217;` b<span style="color: green;">&#41;</span> `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:seq"><span style="font-weight: bold;">seq</span></a>`
P <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a>&#8216; op <span style="color: green;">&#40;</span>e `op` b<span style="color: green;">&#41;</span> bs<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:foldl"><span style="font-weight: bold;">foldl</span></a>&#8216; op&#8217; <span style="color: green;">&#40;</span>e&#8217; `op` b<span style="color: green;">&#41;</span> bs<span style="color: green;">&#41;</span>
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- P strictness -}</span>
&nbsp;
P <span style="color: green;">&#40;</span>e `op` b<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>e&#8217; `op&#8217;` b<span style="color: green;">&#41;</span> `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:seq"><span style="font-weight: bold;">seq</span></a>`
P <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>e  `op` b<span style="color: green;">&#41;</span> `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:seq"><span style="font-weight: bold;">seq</span></a>` <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a>&#8216; op  <span style="color: green;">&#40;</span>e  `op` b<span style="color: green;">&#41;</span> bs<span style="color: green;">&#41;</span>
  <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>e&#8217; `op` b<span style="color: green;">&#41;</span> `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:seq"><span style="font-weight: bold;">seq</span></a>` <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a>&#8216; op&#8217; <span style="color: green;">&#40;</span>e&#8217; `op` b<span style="color: green;">&#41;</span> bs<span style="color: green;">&#41;</span>
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- foldl&#8217; def, twice -}</span>
&nbsp;
P <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a>&#8216; op e <span style="color: green;">&#40;</span>b:bs<span style="color: green;">&#41;</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#v:foldl"><span style="font-weight: bold;">foldl</span></a>&#8216; op&#8217; e&#8217; <span style="color: green;">&#40;</span>b:bs<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre>
</div></p>

<p>That&#8217;s it.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~f/conal?a=IE2nN"><img src="http://feeds.feedburner.com/~f/conal?i=IE2nN" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/conal?a=tvTxn"><img src="http://feeds.feedburner.com/~f/conal?i=tvTxn" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/conal?a=8g4nn"><img src="http://feeds.feedburner.com/~f/conal?i=8g4nn" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/conal/~4/454297817" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/proofs-for-left-fold-zipping/feed/</wfw:commentRss>
		<feedburner:origLink>http://conal.net/blog/posts/proofs-for-left-fold-zipping/</feedburner:origLink></item>
		<item>
		<title>More beautiful fold zipping</title>
		<link>http://feeds.feedburner.com/~r/conal/~3/453765396/</link>
		<comments>http://conal.net/blog/posts/more-beautiful-fold-zipping/#comments</comments>
		<pubDate>Sat, 15 Nov 2008 07:24:21 +0000</pubDate>
		<dc:creator>conal</dc:creator>
		
		<category><![CDATA[Functional programming]]></category>

		<category><![CDATA[fold]]></category>

		<category><![CDATA[zip]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=59</guid>
		<description><![CDATA[





My previous post, Another lovely example of type class morphisms, placed some standard structure around Max Rabkin&#8217;s Beautiful folding, using type class morphisms to confirm that the Functor and Applicative instances agreed with their inevitable meanings.

In the process of working out the Applicative case, I realized the essence of fold zipping was getting a bit [...]]]></description>
			<content:encoded><![CDATA[<!-- 

Title: More beautiful fold zipping

Tags: fold, zip

URL: http://conal.net/blog/posts/more-beautiful-fold-zipping

-->

<!-- references -->

<!-- teaser -->

<p>My previous post, <em><a href="http://conal.net/blog/posts/another-lovely-example-of-type-class-morphisms" title="blog post">Another lovely example of type class morphisms</a></em>, placed some standard structure around Max Rabkin&#8217;s <em><a href="http://squing.blogspot.com/2008/11/beautiful-folding.html" title="blog post by Max Rabkin">Beautiful folding</a></em>, using <a href="http://conal.net/blog/posts/simplifying-semantics-with-type-class-morphisms" title="blog post">type class morphisms</a> to confirm that the <code>Functor</code> and <code>Applicative</code> instances agreed with their inevitable meanings.</p>

<p>In the process of working out the <code>Applicative</code> case, I realized the essence of fold zipping was getting a bit obscured.
This post simplifies Max&#8217;s <code>Fold</code> data type a bit and shows how to zip strict left folds in the simpler setting.
You can <a href="http://hackage.haskell.org/cgi-bin/hackage-scripts/package/ZipFold" title="the ZipFold package">get the code</a> described here.</p>

<p><strong>Edits</strong>:</p>

<ul>
<li>2008-11-15: I renamed the <code>Pair</code> and <code>Pair'</code> classes to <code>Zip</code> and <code>Zip'</code>.</li>
</ul>

<!-- without a comment or something here, the last item above becomes a paragraph -->

<p><span id="more-59"></span></p>

<h3>A simpler fold</h3>

<p>The strict left-folding functional is</p>

<p><div>
<pre class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a>&#8216; :: <span style="color: green;">&#40;</span>a -&gt; b -&gt; a<span style="color: green;">&#41;</span> -&gt; a -&gt; <span style="color: green;">&#91;</span>b<span style="color: green;">&#93;</span> -&gt; a</pre>
</div></p>

<p>Max&#8217;s fold type packages up the first two arguments of <code>foldl'</code> and adds on an post-fold step, as needed for <code>fmap</code>:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">data</span> Fold b c = <span style="color: #050; font-weight: bold;">forall</span> a. F <span style="color: green;">&#40;</span>a -&gt; b -&gt; a<span style="color: green;">&#41;</span> a <span style="color: green;">&#40;</span>a -&gt; c<span style="color: green;">&#41;</span>  <span style="color: #5d478b; font-style: italic;">&#8211; clever version</span></pre>
</div></p>

<p>A simpler, less clever version just has the two <code>foldl'</code> arguments, and no <code>forall</code>:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">data</span> Fold b a = F <span style="color: green;">&#40;</span>a -&gt; b -&gt; a<span style="color: green;">&#41;</span> a  <span style="color: #5d478b; font-style: italic;">&#8211; simple version</span></pre>
</div></p>

<p>I&#8217;ll use this simpler version for zipping and later enhance it for <code>fmap</code>.</p>

<p>The strict interpretation of <code>Fold</code> simply unpacks and folds:</p>

<p><div>
<pre class="haskell">cfoldl' :: Fold b a -&gt; <span style="color: green;">&#91;</span>b<span style="color: green;">&#93;</span> -&gt; a
cfoldl&#8217; <span style="color: green;">&#40;</span>F op e<span style="color: green;">&#41;</span> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a>&#8216; op e</pre>
</div></p>

<p>Zipping is then just a simplification of the <code>(&lt;*&gt;)</code> definition in <a href="http://conal.net/blog/posts/another-lovely-example-of-type-class-morphisms" title="blog post">the previous post</a>.
As before, <code>P</code> is a type and constructor of strict pairs.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">data</span> P c c&#8217; = P !c !c&#8217;
&nbsp;
zipF&#8217; :: Fold b a -&gt; Fold b a&#8217; -&gt; Fold b <span style="color: green;">&#40;</span>P a a&#8217;<span style="color: green;">&#41;</span>
F op e `zipF&#8217;` F op&#8217; e&#8217; = F op&#8221; <span style="color: green;">&#40;</span>P e e&#8217;<span style="color: green;">&#41;</span>
 <span style="color: #050; font-weight: bold;">where</span>
   P a a&#8217; `op&#8221;` b = P <span style="color: green;">&#40;</span>a `op` b<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>a&#8217; `op&#8217;` b<span style="color: green;">&#41;</span></pre>
</div></p>

<p>We can also define non-strict counterparts:</p>

<p><div>
<pre class="haskell">cfoldl :: Fold b a -&gt; <span style="color: green;">&#91;</span>b<span style="color: green;">&#93;</span> -&gt; a
cfoldl <span style="color: green;">&#40;</span>F op e<span style="color: green;">&#41;</span> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a> op e
&nbsp;
zipF :: Fold b a -&gt; Fold b a&#8217; -&gt; Fold b <span style="color: green;">&#40;</span>a,a&#8217;<span style="color: green;">&#41;</span>
F op e `zipF` F op&#8217; e&#8217; = F op&#8221; <span style="color: green;">&#40;</span>e,e&#8217;<span style="color: green;">&#41;</span>
 <span style="color: #050; font-weight: bold;">where</span>
   <span style="color: green;">&#40;</span>a,a&#8217;<span style="color: green;">&#41;</span> `op&#8221;` b = <span style="color: green;">&#40;</span>a `op` b, a&#8217; `op&#8217;` b<span style="color: green;">&#41;</span></pre>
</div></p>

<h3>More type classes &#8230;</h3>

<p>A <a href="http://conal.net/blog/posts/simplifying-semantics-with-type-class-morphisms" title="blog post">previous post</a> suggested a criterion for inevitability of interpretation functions like <code>cfoldl</code> and <code>cfoldl'</code>.
Whatever class instances exists for the source type (<code>Fold</code> here), interpretation functions should be
&#8220;type class morphisms&#8221; for those classes, which loosely means that the meaning of a method is the same method on the meaning.</p>

<p>Where&#8217;s the type class for our simplified <code>Fold</code>?
It lacks the magic ingredient Max added to make it a <code>Functor</code> and an <code>Applicative</code>, namely the post-fold step.</p>

<p>However, there&#8217;s <a href="http://hackage.haskell.org/packages/archive/TypeCompose/latest/doc/html/Data-Pair.html" title="module from the TypeCompose package">another class</a> that&#8217;s just the thing:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">class</span> Zip f <span style="color: #050; font-weight: bold;">where</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zip"><span style="font-weight: bold;">zip</span></a> :: f a -&gt; f b -&gt; f <span style="color: green;">&#40;</span>a,b<span style="color: green;">&#41;</span></pre>
</div></p>

<p>(I&#8217;ve used this class with type representations and user interfaces.)</p>

<p>The <code>Zip</code> class from <code>TypeCompose</code> is almost identical to <a href="http://hackage.haskell.org/packages/archive/category-extras/latest/doc/html/Control-Functor-Zip.html" title="module from the category-extras package">the <code>Zip</code> class</a> in <code>category-extras</code>.
The latter class requires <code>Functor</code>, however, which is too restrictive for the simplified <code>Fold</code> type.</p>

<p>Our <em>non-strict</em> left folds have a <code>Zip</code> instance:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> Zip <span style="color: green;">&#40;</span>Fold b<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zip"><span style="font-weight: bold;">zip</span></a> = zipF</pre>
</div></p>

<p>For strict folds, define a strict variant on <code>Zip</code>:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">class</span> Zip&#8217; f <span style="color: #050; font-weight: bold;">where</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zip"><span style="font-weight: bold;">zip</span></a>&#8216; :: f a -&gt; f b -&gt; f <span style="color: green;">&#40;</span>P a b<span style="color: green;">&#41;</span></pre>
</div></p>

<p>and a <code>Zip'</code> instance for <code>Fold</code>:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> Zip&#8217; <span style="color: green;">&#40;</span>Fold b<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zip"><span style="font-weight: bold;">zip</span></a>&#8216; = zipF&#8217;</pre>
</div></p>

<h3>&#8230; and more morphisms</h3>

<p>These zipping classes have associated morphism properties.
A function <code>q</code> mapping from one <code>Zip</code> instance to another is a &#8220;<code>Zip</code> morphism&#8221; when</p>

<p><div>
<pre class="haskell">q <span style="color: green;">&#40;</span>f `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zip"><span style="font-weight: bold;">zip</span></a>` g<span style="color: green;">&#41;</span> == q f `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zip"><span style="font-weight: bold;">zip</span></a>` q g</pre>
</div></p>

<p>for all folds <code>f</code> and <code>g</code>.</p>

<p>Similarly for <code>Zip'</code> morphisms:</p>

<p><div>
<pre class="haskell">q <span style="color: green;">&#40;</span>f `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zip"><span style="font-weight: bold;">zip</span></a>&#8216;` g<span style="color: green;">&#41;</span> == q f `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zip"><span style="font-weight: bold;">zip</span></a>&#8216;` q g</pre>
</div></p>

<p>In other words, <code>q</code> distributes over <code>zip</code> or <code>zip'</code>.</p>

<p>As I&#8217;ll show in another post, the two interpretation functions <code>cfoldl</code> and <code>cfoldl'</code> are <code>Zip</code> and <code>Zip'</code> morphisms, respectively, i.e.,</p>

<p><div>
<pre class="haskell">cfoldl  <span style="color: green;">&#40;</span>f `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zip"><span style="font-weight: bold;">zip</span></a>`  g<span style="color: green;">&#41;</span> == cfoldl  f `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zip"><span style="font-weight: bold;">zip</span></a>`  cfoldl  g
&nbsp;
cfoldl&#8217; <span style="color: green;">&#40;</span>f `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zip"><span style="font-weight: bold;">zip</span></a>&#8216;` g<span style="color: green;">&#41;</span> == cfoldl&#8217; f `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zip"><span style="font-weight: bold;">zip</span></a>&#8216;` cfoldl&#8217; g</pre>
</div></p>

<p>which means that zip folding as defined above works correctly.</p>

<h3>What&#8217;s next?</h3>

<p>I like to keep blog posts fairly short, so I&#8217;ll share two more pieces of this story in upcoming blog posts:</p>

<ul>
<li>Regaining the lost <code>Functor</code> and <code>Applicative</code> instances.</li>
<li>The <code>Zip</code> and <code>Zip'</code> morphism proofs for zip folding.</li>
</ul>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~f/conal?a=Kx7LN"><img src="http://feeds.feedburner.com/~f/conal?i=Kx7LN" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/conal?a=qeR3n"><img src="http://feeds.feedburner.com/~f/conal?i=qeR3n" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/conal?a=Ab46n"><img src="http://feeds.feedburner.com/~f/conal?i=Ab46n" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/conal/~4/453765396" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/more-beautiful-fold-zipping/feed/</wfw:commentRss>
		<feedburner:origLink>http://conal.net/blog/posts/more-beautiful-fold-zipping/</feedburner:origLink></item>
		<item>
		<title>Another lovely example of type class morphisms</title>
		<link>http://feeds.feedburner.com/~r/conal/~3/452607365/</link>
		<comments>http://conal.net/blog/posts/another-lovely-example-of-type-class-morphisms/#comments</comments>
		<pubDate>Fri, 14 Nov 2008 06:20:06 +0000</pubDate>
		<dc:creator>conal</dc:creator>
		
		<category><![CDATA[Functional programming]]></category>

		<category><![CDATA[applicative functors]]></category>

		<category><![CDATA[fold]]></category>

		<category><![CDATA[functors]]></category>

		<category><![CDATA[morphisms]]></category>

		<category><![CDATA[semantics]]></category>

		<category><![CDATA[type classes]]></category>

		<category><![CDATA[zip]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=58</guid>
		<description><![CDATA[





I read Max Rabkin&#8217;s recent post Beautiful folding with great excitement.
He shows how to make combine multiple folds over the same list into a single pass, which can then drastically reduce memory requirements of a lazy functional program.
Max&#8217;s trick is giving folds a data representation and a way to combine representations that corresponds to combining [...]]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Another lovely example of type class morphisms

Tags: applicative functors, functors, morphisms, semantics, type classes, fold, zip

URL: http://conal.net/blog/posts/another-lovely-example-of-type-class-morphisms/

-->

<!-- references -->

<!-- teaser -->

<p>I read Max Rabkin&#8217;s recent post <a href="http://squing.blogspot.com/2008/11/beautiful-folding.html" title="blog post by Max Rabkin">Beautiful folding</a> with great excitement.
He shows how to make combine multiple folds over the same list into a single pass, which can then drastically reduce memory requirements of a lazy functional program.
Max&#8217;s trick is giving folds a data representation and a way to combine representations that corresponds to combining the folds.</p>

<p>Peeking out from behind Max&#8217;s definitions is a lovely pattern I&#8217;ve been noticing more and more over the last couple of years, namely <a href="http://conal.net/blog/posts/simplifying-semantics-with-type-class-morphisms" title="blog post">type class morphisms</a>.</p>

<!--
**Edits**:

* 2008-02-09: just fiddling around
-->

<!-- without a comment or something here, the last item above becomes a paragraph -->

<p><span id="more-58"></span></p>

<h3>Folds as data</h3>

<p>Max gives a data representation of folds and adds on an post-fold step, which makes them composable.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">data</span> Fold b c = <span style="color: #050; font-weight: bold;">forall</span> a. F <span style="color: green;">&#40;</span>a -&gt; b -&gt; a<span style="color: green;">&#41;</span> a <span style="color: green;">&#40;</span>a -&gt; c<span style="color: green;">&#41;</span></pre>
</div></p>

<p>The components of a <code>Fold</code> are a (strict left) fold&#8217;s combiner function and initial value, plus a post-fold step.
This interpretation is done by a function <code>cfoldl'</code>, which turns these data folds into function folds:</p>

<p><div>
<pre class="haskell">cfoldl' :: Fold b c -&gt; <span style="color: green;">&#91;</span>b<span style="color: green;">&#93;</span> -&gt; c
cfoldl&#8217; <span style="color: green;">&#40;</span>F op e k<span style="color: green;">&#41;</span> = k . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a>&#8216; op e</pre>
</div></p>

<p>where <code>foldl'</code> is the standard strict, left-fold functional:</p>

<p><div>
<pre class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a>&#8216; :: <span style="color: green;">&#40;</span>a -&gt; b -&gt; a<span style="color: green;">&#41;</span> -&gt; a -&gt; <span style="color: green;">&#91;</span>b<span style="color: green;">&#93;</span> -&gt; a
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a>&#8216; op a <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>     = a
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a>&#8216; op a <span style="color: green;">&#40;</span>b:bs<span style="color: green;">&#41;</span> =
  <span style="color: #050; font-weight: bold;">let</span> a&#8217; = a `op` b <span style="color: #050; font-weight: bold;">in</span> a&#8217; `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:seq"><span style="font-weight: bold;">seq</span></a>` <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a>&#8216; f a&#8217; bs</pre>
</div></p>

<h3>Standard classes</h3>

<p>As Twan van Laarhoven pointed out in a comment on <a href="http://squing.blogspot.com/2008/11/beautiful-folding.html" title="blog post by Max Rabkin">Max&#8217;s post</a>, <code>Fold b</code> is a functor and an applicative functor, so some of Max&#8217;s <code>Fold</code>-manipulating functions can be replaced by standard vocabulary.</p>

<p>The <code>Functor</code> instance is pretty simple:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Functor"><span style="color: #833; font-weight: bold;">Functor</span></a> <span style="color: green;">&#40;</span>Fold b<span style="color: green;">&#41;</span> <span style="color: #050; 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> h <span style="color: green;">&#40;</span>F op e k<span style="color: green;">&#41;</span> = F op e <span style="color: green;">&#40;</span>h . k<span style="color: green;">&#41;</span></pre>
</div></p>

<p>The <code>Applicative</code> instance is a bit trickier.
For strictness, Max used used a type of strict pairs:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">data</span> Pair c c&#8217; = P !c !c&#8217;</pre>
</div></p>

<p>The instance:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Applicative"><span style="color: #833; font-weight: bold;">Applicative</span></a> <span style="color: green;">&#40;</span>Fold b<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:pure"><span style="font-weight: bold;">pure</span></a> a = F <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:error"><span style="font-weight: bold;">error</span></a> <span style="color: green;">&quot;no op&quot;</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#v:error"><span style="font-weight: bold;">error</span></a> <span style="color: green;">&quot;no e&quot;</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#v:const"><span style="font-weight: bold;">const</span></a> a<span style="color: green;">&#41;</span>
&nbsp;
  F op e k &lt;*&gt; F op&#8217; e&#8217; k&#8217; = F op&#8221; e&#8221; k&#8221;
   <span style="color: #050; font-weight: bold;">where</span>
     P a a&#8217; `op&#8221;` b = P <span style="color: green;">&#40;</span>a `op` b<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>a&#8217; `op&#8217;` b<span style="color: green;">&#41;</span>
     e&#8221;             = P e e&#8217;
     k&#8221; <span style="color: green;">&#40;</span>P a a&#8217;<span style="color: green;">&#41;</span>    = <span style="color: green;">&#40;</span>k a<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>k&#8217; a&#8217;<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Given that <code>Fold b</code> is an applicative functor, Max&#8217;s <code>bothWith</code> function is then <code>liftA2</code>.
Max&#8217;s <code>multi-cfoldl'</code> rule then becomes:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">forall</span> c f g xs.
   h <span style="color: green;">&#40;</span>cfoldl&#8217; f xs<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>cfoldl&#8217; g xs<span style="color: green;">&#41;</span> == cfoldl&#8217; <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:liftA2"><span style="font-weight: bold;">liftA2</span></a> h f g<span style="color: green;">&#41;</span> xs</pre>
</div></p>

<p>thus replacing two passes with one pass.</p>

<h3>Beautiful properties</h3>

<p>Now here&#8217;s the fun part.
Looking at the <code>Applicative</code> instance for <code>((-&gt;) a)</code>, the rule above is equivalent to</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">forall</span> c f g.
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:liftA2"><span style="font-weight: bold;">liftA2</span></a> h <span style="color: green;">&#40;</span>cfoldl&#8217; f<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>cfoldl&#8217; g<span style="color: green;">&#41;</span> == cfoldl&#8217; <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:liftA2"><span style="font-weight: bold;">liftA2</span></a> h f g<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Flipped around, this rule says that <code>liftA2</code> distributes over <code>cfoldl'</code>.
Or, &#8220;the meaning of <code>liftA2</code> is <code>liftA2</code>&#8220;.
Neat, huh?</p>

<p>Moreover, this <code>liftA2</code> property is equivalent to the following:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">forall</span> f g.
  cfoldl&#8217; f &lt;*&gt; cfoldl&#8217; g == cfoldl&#8217; <span style="color: green;">&#40;</span>f &lt;*&gt; g<span style="color: green;">&#41;</span></pre>
</div></p>

<p>This form is one of the two <code>Applicative</code> morphism laws (which I usually write in the reverse direction):</p>

<p>For more about these morphisms, see <a href="http://conal.net/blog/posts/simplifying-semantics-with-type-class-morphisms" title="blog post">Simplifying semantics with type class morphisms</a>.
That post suggests that semantic functions in particular ought to be type class morphisms (and if not, then you&#8217;d have an abstraction leak).
And <code>cfoldl'</code> is a semantic function, in that it gives meaning to a <code>Fold</code>.</p>

<p>The other type class morphisms in this case are</p>

<p><div>
<pre class="haskell">cfoldl' <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:pure"><span style="font-weight: bold;">pure</span></a> a  <span style="color: green;">&#41;</span> == <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:pure"><span style="font-weight: bold;">pure</span></a> a
&nbsp;
cfoldl&#8217; <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> h f<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> h <span style="color: green;">&#40;</span>cfoldl&#8217; f<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Given the <code>Functor</code> and <code>Applicative</code> instances of <code>((-&gt;) a)</code>, these two properties are equivalent to</p>

<p><div>
<pre class="haskell">cfoldl' <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:pure"><span style="font-weight: bold;">pure</span></a> a  <span style="color: green;">&#41;</span> == <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:const"><span style="font-weight: bold;">const</span></a> a
&nbsp;
cfoldl&#8217; <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> h f<span style="color: green;">&#41;</span> == h . cfoldl&#8217; f</pre>
</div></p>

<p>or</p>

<p><div>
<pre class="haskell">cfoldl' <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:pure"><span style="font-weight: bold;">pure</span></a> a  <span style="color: green;">&#41;</span> xs == a
&nbsp;
cfoldl&#8217; <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> h f<span style="color: green;">&#41;</span> xs == h <span style="color: green;">&#40;</span>cfoldl&#8217; f xs<span style="color: green;">&#41;</span></pre>
</div></p>

<h3>Rewrite rules</h3>

<p>Max pointed out that GHC does not handle his original <code>multi-cfoldl'</code> rule.
The reason is that the head of the LHS (left-hand side) is a variable.
However, the type class morphism laws have constant (known) functions at the head, so I expect they could usefully act as fusion rewrite rules.</p>

<h3>Inevitable instances</h3>

<p>Given the implementations (instances) of <code>Functor</code> and <code>Applicative</code> for <code>Fold</code>, I&#8217;d like to verify that the morphism laws for <code>cfoldl'</code> (above) hold.</p>

<h4>Functor</h4>

<p>Start with <code>fmap</code>.
The morphism law:</p>

<p><div>
<pre class="haskell">cfoldl' <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> h f<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> h <span style="color: green;">&#40;</span>cfoldl&#8217; f<span style="color: green;">&#41;</span></pre>
</div></p>

<p>First, give the <code>Fold</code> argument more structure, so that (without loss of generality) the law becomes</p>

<p><div>
<pre class="haskell">cfoldl' <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> h <span style="color: green;">&#40;</span>F op e k<span style="color: green;">&#41;</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> h <span style="color: green;">&#40;</span>cfoldl&#8217; <span style="color: green;">&#40;</span>F op e k<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre>
</div></p>

<p>The game is to work backward from this law to the definition of <code>fmap</code> for <code>Fold</code>.
I&#8217;ll do so by massaging the RHS (right-hand side) into the form <code>cfoldl' (...)</code>, where &#8220;<code>...</code>&#8221; is the definition <code>fmap h (F op e k)</code>.</p>

<p><div>
<pre class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> h <span style="color: green;">&#40;</span>cfoldl&#8217; <span style="color: green;">&#40;</span>F op e k<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
&nbsp;
  ==  <span style="color: #5d478b; font-style: italic;">{- inline cfoldl&#8217; -}</span>
&nbsp;
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> h <span style="color: green;">&#40;</span>k . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a>&#8216; op e<span style="color: green;">&#41;</span>
&nbsp;
  ==  <span style="color: #5d478b; font-style: italic;">{- inline fmap on functions -}</span>
&nbsp;
h . <span style="color: green;">&#40;</span>k . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a>&#8216; op e<span style="color: green;">&#41;</span>
&nbsp;
  ==  <span style="color: #5d478b; font-style: italic;">{- associativity of (.) -}</span>
&nbsp;
<span style="color: green;">&#40;</span>h . k<span style="color: green;">&#41;</span> . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a>&#8216; op e
&nbsp;
  ==  <span style="color: #5d478b; font-style: italic;">{- uninline cfoldl&#8217; -}</span>
&nbsp;
cfoldl&#8217; <span style="color: green;">&#40;</span>F op e <span style="color: green;">&#40;</span>h . k<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
&nbsp;
  ==  <span style="color: #5d478b; font-style: italic;">{- uninline fmap on Fold  -}</span>
&nbsp;
cfoldl&#8217; <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> h <span style="color: green;">&#40;</span>F op e k<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre>
</div></p>

<p>This proof show why Max had to add the post-fold function <code>k</code> to his <code>Fold</code> type.
If <code>k</code> weren&#8217;t there, we couldn&#8217;t have buried the <code>h</code> in it.</p>

<p>More usefully, this proof suggests how we could have discovered the <code>fmap</code> definition.
For instance, we might have tried with a simpler and more obvious <code>Fold</code> representation:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">data</span> FoldS a b = FS <span style="color: green;">&#40;</span>a -&gt; b -&gt; a<span style="color: green;">&#41;</span> a</pre>
</div></p>

<p>Getting into the <code>fmap</code> derivation, we&#8217;d come to</p>

<p><div>
<pre class="haskell">h . foldsl' op e</pre>
</div></p>

<p>and then we&#8217;d be stuck.
But not really, because the awkward extra bit (<code>h .</code>) beckons us to generalize by adding Max&#8217;s post-fold function.</p>

<h4>Applicative</h4>

<p>Next pure:</p>

<p><div>
<pre class="haskell">cfoldl' <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:pure"><span style="font-weight: bold;">pure</span></a> a<span style="color: green;">&#41;</span> == <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:pure"><span style="font-weight: bold;">pure</span></a> a</pre>
</div></p>

<p>Reason as before, starting with the RHS</p>

<p><div>
<pre class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:pure"><span style="font-weight: bold;">pure</span></a> a
&nbsp;
  ==  <span style="color: #5d478b; font-style: italic;">{- inline pure on functions -}</span>
&nbsp;
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:const"><span style="font-weight: bold;">const</span></a> a
&nbsp;
  ==  <span style="color: #5d478b; font-style: italic;">{- property of const -}</span>
&nbsp;
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:const"><span style="font-weight: bold;">const</span></a> a . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a> op e
&nbsp;
  ==  <span style="color: #5d478b; font-style: italic;">{- uninline cfoldl&#8217; -}</span>
&nbsp;
cfoldl&#8217; <span style="color: green;">&#40;</span>F <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:const"><span style="font-weight: bold;">const</span></a> a<span style="color: green;">&#41;</span> op e<span style="color: green;">&#41;</span></pre>
</div></p>

<p>The imaginative step was inventing structure to match the definition of <code>cfoldl'</code>.
This definition is true for <em>any</em> values of <code>op</code> and <code>e</code>, so we can use bottom in the definition:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Applicative"><span style="color: #833; font-weight: bold;">Applicative</span></a> <span style="color: green;">&#40;</span>Fold b<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:pure"><span style="font-weight: bold;">pure</span></a> 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> <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;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:const"><span style="font-weight: bold;">const</span></a> a<span style="color: green;">&#41;</span></pre>
</div></p>

<p>As Twan noticed, the existential (<code>forall</code>) also lets us pick defined values for <code>op</code> and <code>e</code>.
He chose <code>(\_ _ -&gt; ())</code> and <code>()</code>.</p>

<p>The derivation of <code>(&lt;*&gt;)</code> is trickier and is the heart of the problem of fusing folds to reduce multiple traversals to a single one.
Why the heart?  Because <code>(&lt;*&gt;)</code> is all about <em>combining</em> two things into one.</p>

<h4>Intermission</h4>

<p>I&#8217;m taking a break here.
While fiddling with a proof of the <code>(&lt;*&gt;)</code> morphism law, I realized a simpler way to structure these folds, which will be the topic of an upcoming post.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~f/conal?a=aNypN"><img src="http://feeds.feedburner.com/~f/conal?i=aNypN" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/conal?a=EQJVn"><img src="http://feeds.feedburner.com/~f/conal?i=EQJVn" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/conal?a=6EBzn"><img src="http://feeds.feedburner.com/~f/conal?i=6EBzn" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/conal/~4/452607365" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/another-lovely-example-of-type-class-morphisms/feed/</wfw:commentRss>
		<feedburner:origLink>http://conal.net/blog/posts/another-lovely-example-of-type-class-morphisms/</feedburner:origLink></item>
		<item>
		<title>Simpler, more efficient, functional linear maps</title>
		<link>http://feeds.feedburner.com/~r/conal/~3/425946113/</link>
		<comments>http://conal.net/blog/posts/simpler-more-efficient-functional-linear-maps/#comments</comments>
		<pubDate>Mon, 20 Oct 2008 01:26:05 +0000</pubDate>
		<dc:creator>conal</dc:creator>
		
		<category><![CDATA[Functional programming]]></category>

		<category><![CDATA[linear maps]]></category>

		<category><![CDATA[math]]></category>

		<category><![CDATA[memoization]]></category>

		<category><![CDATA[tries]]></category>

		<category><![CDATA[type families]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=57</guid>
		<description><![CDATA[





A previous post described a data type of functional linear maps.
As Andy Gill pointed out, we had a heck of a time trying to get good performance.
This note describes a new representation that is very simple and much more efficient.
It&#8217;s terribly obvious in retrospect but took me a good while to stumble onto.

The Haskell module [...]]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Simpler, more efficient, functional linear maps

Tags: vector spaces, linear maps, math, type families, memoization, tries

URL: http://conal.net/blog/posts/simpler-more-efficient-functional-linear-maps/

-->

<!-- references -->

<!-- teaser -->

<p>A <a href="http://conal.net/blog/posts/functional-linear-maps/" title="Blog post: &quot;Functional linear maps&quot;">previous post</a> described a data type of functional linear maps.
As <a href="http://www.unsafeperformio.com/index.php" title="Andy Gill's home page">Andy Gill</a> <a href="http://blog.unsafeperformio.com/?p=23" title="Blog post: &quot;Performance problems with functional representation of derivatives&quot;">pointed out</a>, we had a heck of a time trying to get good performance.
This note describes a new representation that is <em>very simple</em> and much more efficient.
It&#8217;s terribly obvious in retrospect but took me a good while to stumble onto.</p>

<p>The Haskell module described here is part of the <a href="http://haskell.org/haskellwiki/vector-space" title="Library wiki page: &quot;vector-space&quot;">vector-space library</a> (version 0.5 or later) and requires ghc version 6.10 or better (for associated types).</p>

<p><strong>Edits</strong>:</p>

<ul>
<li>2008-11-09: Changed remarks about versions.  The vector-space version 0.5 depends on ghc 6.10.</li>
<li>2008-10-21: Fixed the <a href="http://haskell.org/haskellwiki/vector-space" title="Library wiki page: &quot;vector-space&quot;">vector-space library</a> link in the teaser.</li>
</ul>

<!-- without a comment or something here, the last item above becomes a paragraph -->

<p><span id="more-57"></span></p>

<h3>Linear maps</h3>

<p>Semantically, a <em>linear map</em> is a function <code>f :: a -&gt; b</code> such that, for all scalar values <code>s</code> and &#8220;vectors&#8221; <code>u, v :: a</code>, the following properties hold:</p>

<p><div class=latex><img src='http://conal.net/blog/wp-content/latex/301/301ab9db4cdf97efebdbfde71acc1b25-ffffff000000.png' alt='f (s \cdot u) = s \cdot f u' class='latex' /></div>
<div class=latex><img src='http://conal.net/blog/wp-content/latex/864/86429c558884d2a47481163eb3bcb653-ffffff000000.png' alt='f (u + v) = f u + f v' class='latex' /></div></p>

<p>By repeated application of these properties,</p>

<p><div class=latex><img src='http://conal.net/blog/wp-content/latex/c1d/c1df825c8a572e687fac4dc1d73edadb-ffffff000000.png' alt='f (s_1 \cdot u_1 + \cdots + s_n \cdot u_n) = s_1 \cdot f u_1 + \cdots + s_n \cdot f u_n' class='latex' /></div></p>

<p>Taking the <em>u<sub>i</sub></em> as basis vectors, this form implies that a linear function is determined by its behavior on any <a href="http://en.wikipedia.org/wiki/Basis_(linear_algebra)" title="Wikipedia article">basis</a> of its domain type.</p>

<p>Therefore, a linear function can be represented simply as a function from a basis, using the representation described in <a href="http://conal.net/blog/posts/vector-space-bases-via-type-families/" title="Blog post: &quot;Vector space bases via type families&quot;">Vector space bases via type families</a>.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> u :-* v = Basis u -&gt; v</pre>
</div></p>

<p>The semantic function converts from <code>(u :-* v)</code> to <code>(u -&gt; v)</code>.
It decomposes a source vector into its coordinates, applies the basis function to basis representations, and linearly combines the results.</p>

<p><div>
<pre class="haskell">lapply :: <span style="color: green;">&#40;</span> VectorSpace u, VectorSpace v
          , Scalar u ~ Scalar v, HasBasis u <span style="color: green;">&#41;</span> =&gt;
          <span style="color: green;">&#40;</span>u :-* v<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</span>u -&gt; v<span style="color: green;">&#41;</span>
lapply lm = \ u -&gt; sumV <span style="color: green;">&#91;</span>s *^ lm b | <span style="color: green;">&#40;</span>b,s<span style="color: green;">&#41;</span> &lt;- decompose u<span style="color: green;">&#93;</span></pre>
</div></p>

<p>or</p>

<p><div>
<pre class="haskell">lapply lm = linearCombo . <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/Control-Arrow.html#v:first"><span style="font-weight: bold;">first</span></a> lm<span style="color: green;">&#41;</span> . decompose</pre>
</div></p>

<p>The reverse function is easier.
Convert a function <code>f</code>, presumed linear, to a linear map representation:</p>

<p><div>
<pre class="haskell">linear :: <span style="color: green;">&#40;</span>VectorSpace u, VectorSpace v, HasBasis u<span style="color: green;">&#41;</span> =&gt;
          <span style="color: green;">&#40;</span>u -&gt; v<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</span>u :-* v<span style="color: green;">&#41;</span></pre>
</div></p>

<p>It suffices to apply <code>f</code> to basis values:</p>

<p><div>
<pre class="haskell">linear f = f . basisValue</pre>
</div></p>

<h3>Memoization</h3>

<p>The idea of the linear map representation is to reconstruct an entire (linear) function out of just a few samples.
In other words, we can make a very small sampling of function&#8217;s domain, and re-use those values in order to compute the function&#8217;s value at <em>all</em> domain values.
As implemented above, however, this trick makes function application more expensive, not less.
If <code>lm = linear f</code>, then each use of <code>lapply lm</code> can apply <code>f</code> to the value of every basis element, and then linearly combine results.</p>

<p>A simple trick fixes this efficiency problem: <em>memoize</em> the linear map.
We could do the memoization privately, e.g.,</p>

<p><div>
<pre class="haskell">linear f = memo <span style="color: green;">&#40;</span>f . basisValue<span style="color: green;">&#41;</span></pre>
</div></p>

<p>If <code>lm = linear f</code>, then no matter how many times <code>lapply lm</code> is applied, the function <code>f</code> can only get applied as many times as the dimension of the domain of <code>f</code>.</p>

<p>However, there are several other ways to make linear maps, and it would be easy to forget to memoize each combining form.
So, instead of the function representation above, I ensure that the function be memoized by representing it as a <a href="http://conal.net/blog/posts/elegant-memoization-with-functional-memo-tries/" title="Blog post: &quot;Elegant memoization with functional memo tries&quot;">memo trie</a>.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> u :-* v = Basis u :-&gt;: v</pre>
</div></p>

<p>The conversion functions <code>linear</code> and <code>lapply</code> need just a little tweaking.
Split <code>memo</code> into its definition <code>untrie . trie</code>, and then move the second phase (<code>untrie</code>) into <code>lapply</code>.
We&#8217;ll also have to add <code>HasTrie</code> constraints:</p>

<p><div>
<pre class="haskell">linear :: <span style="color: green;">&#40;</span> VectorSpace u, VectorSpace v
          , HasBasis u, HasTrie <span style="color: green;">&#40;</span>Basis u<span style="color: green;">&#41;</span> <span style="color: green;">&#41;</span> =&gt;
          <span style="color: green;">&#40;</span>u -&gt; v<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</span>u :-* v<span style="color: green;">&#41;</span>
linear f = trie <span style="color: green;">&#40;</span>f . basisValue<span style="color: green;">&#41;</span>
&nbsp;
lapply :: <span style="color: green;">&#40;</span> VectorSpace u, VectorSpace v, Scalar u ~ Scalar v
          , HasBasis u, HasTrie <span style="color: green;">&#40;</span>Basis u<span style="color: green;">&#41;</span> <span style="color: green;">&#41;</span> =&gt;
          <span style="color: green;">&#40;</span>u :-* v<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</span>u -&gt; v<span style="color: green;">&#41;</span>
lapply lm = linearCombo . <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/Control-Arrow.html#v:first"><span style="font-weight: bold;">first</span></a> <span style="color: green;">&#40;</span>untrie lm<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> . decompose</pre>
</div></p>

<p>Now we can build up linear maps conveniently and efficiently by using the operations on memo tries shown in <a href="http://conal.net/blog/posts/composing-memo-tries/" title="blog post">Composing memo tries</a>.
For instance, suppose that <code>h</code> is a linear function of two arguments (linear in <em>both</em>, not it <em>each</em>) and <code>m</code> and <code>n</code> are two linear maps.
Then <code>liftA2 h m n</code> is the linear function that applies <code>h</code> to the results of <code>m</code> and <code>n</code>.</p>

<p><div>
<pre class="haskell">lapply <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:liftA2"><span style="font-weight: bold;">liftA2</span></a> h m n<span style="color: green;">&#41;</span> a = h <span style="color: green;">&#40;</span>lapply m a<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>lapply n a<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Exploting the applicative functor instance for functions, we get another formulation:</p>

<p><div>
<pre class="haskell">lapply <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:liftA2"><span style="font-weight: bold;">liftA2</span></a> h m n<span style="color: green;">&#41;</span> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:liftA2"><span style="font-weight: bold;">liftA2</span></a> h <span style="color: green;">&#40;</span>lapply m<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>lapply n<span style="color: green;">&#41;</span></pre>
</div></p>

<p>In other words, the meaning of a <code>liftA2</code> is the <code>liftA2</code> of the meanings, as discussed in <a href="http://conal.net/blog/posts/simplifying-semantics-with-type-class-morphisms" title="blog post">Simplifying semantics with type class morphisms</a>.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~f/conal?a=KErBM"><img src="http://feeds.feedburner.com/~f/conal?i=KErBM" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/conal?a=El6Em"><img src="http://feeds.feedburner.com/~f/conal?i=El6Em" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/conal?a=9FNRm"><img src="http://feeds.feedburner.com/~f/conal?i=9FNRm" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/conal/~4/425946113" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/simpler-more-efficient-functional-linear-maps/feed/</wfw:commentRss>
		<feedburner:origLink>http://conal.net/blog/posts/simpler-more-efficient-functional-linear-maps/</feedburner:origLink></item>
		<item>
		<title>Vector space bases via type families</title>
		<link>http://feeds.feedburner.com/~r/conal/~3/425935010/</link>
		<comments>http://conal.net/blog/posts/vector-space-bases-via-type-families/#comments</comments>
		<pubDate>Mon, 20 Oct 2008 01:23:27 +0000</pubDate>
		<dc:creator>conal</dc:creator>
		
		<category><![CDATA[Functional programming]]></category>

		<category><![CDATA[type families]]></category>

		<category><![CDATA[vector spaces]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=56</guid>
		<description><![CDATA[





A basis B of a vector space V is a subset B of V, such that the elements of B are linearly independent and span V.
That is to say, every element (vector) of V is a linear combination of elements of B, and no element of B is a linear combination of the other elements [...]]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Vector space bases via type families

Tags: vector spaces, type families

URL: http://conal.net/blog/posts/vector-space-bases-via-type-families/

-->

<!-- references -->

<!-- teaser -->

<p>A <a href="http://en.wikipedia.org/wiki/Basis_(linear_algebra)" title="Wikipedia article">basis</a> <em>B</em> of a vector space <em>V</em> is a subset <em>B</em> of <em>V</em>, such that the elements of <em>B</em> are linearly independent and span <em>V</em>.
That is to say, every element (vector) of <em>V</em> is a linear combination of elements of <em>B</em>, and no element of <em>B</em> is a linear combination of the other elements of <em>B</em>.
Moreover, every basis determines a unique decomposition of any member of <em>V</em> into coordinates relative to <em>B</em>.</p>

<p>This post gives a simple Haskell implementation for a canonical basis of a vector space, and a means of decomposing vectors into coordinates.
It uses <a href="http://research.microsoft.com/~simonpj/papers/assoc-types/" title="Collection of papers: &quot;Indexed type families&quot;">indexed type families</a> (associated types), and is quite general, despite its simplicity.</p>

<p>The Haskell module described here is part of the vector-space library (version 0.4 or later), which available on Hackage and a darcs repository.
See the <a href="http://haskell.org/haskellwiki/vector-space" title="Wiki page: vector-space">wiki page</a>, <a href="http://code.haskell.org/vector-space/doc/html/Data-ABasis.html" title="Interface documentation: Data.ABasis">interface documentation</a>, and <a href="http://code.haskell.org/vector-space/doc/html/src/Data-ABasis.html" title="Source module: Data.ABasis">source code</a>.
The library version described below (0.5 or later) relies on ghc 6.10.</p>

<p><strong>Edits</strong>:</p>

<ul>
<li>2008-11-09: Tweaked comment above about version.</li>
<li>2008-02-09: just fiddling around</li>
</ul>

<!-- without a comment or something here, the last item above becomes a paragraph -->

<p><span id="more-56"></span></p>

<h3>Additive groups and vector spaces</h3>

<p>We&#8217;ll need a bit of preliminary before jumping into basis types.</p>

<p>An <em><a href="http://code.haskell.org/vector-space/doc/html/Data-AdditiveGroup.html" title="Interface documentation: Data.AdditiveGroup">additive group</a></em> has addition operation, with an identity (zero) and an additive inverse:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">class</span> AdditiveGroup v <span style="color: #050; font-weight: bold;">where</span>
  <span style="color: green;">&#40;</span>^+^<span style="color: green;">&#41;</span>   :: v -&gt; v -&gt; v
  zeroV   :: v
  negateV :: v -&gt; v</pre>
</div></p>

<p>A <em><a href="http://code.haskell.org/vector-space/doc/html/Data-AVectorSpace.html" title="Interface documentation: Data.AVectorSpace">vector space</a></em> is an additive group with scalar multiplication, so it also has an associated type of scalars:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">class</span> AdditiveGroup v =&gt; VectorSpace v <span style="color: #050; font-weight: bold;">where</span>
  <span style="color: #050; font-weight: bold;">type</span> Scalar v :: *
  <span style="color: green;">&#40;</span>*^<span style="color: green;">&#41;</span> :: Scalar v -&gt; v -&gt; v</pre>
</div></p>

<p>This associated type could instead by written as a functional dependency (fundep).
In fact, the type family implementation in ghc 6.9 is not quite up to working with the code in this post, so the library contains a (6.9-friendly) version together with as a fundep (<code>Data.VectorSpace</code>) and the version given in this post (<code>Data.AVectorSpace</code>).
Sometime after ghc-6.10 is released, I will retire the fundep version and rename <code>AVectorSpace</code> to <code>VectorSpace</code>.
Similarly, the library temporarily contains <code>Data.ABasis</code>.</p>

<h3>Basis types</h3>

<p>Since Haskell doesn&#8217;t have subtyping, we can&#8217;t represent a basis type directly as a subset.
Instead, for an arbitrary vector space <code>v</code>, represent the (canonical) basis as an associated type, <code>Basis v</code>, and a function that interprets a basis representation as a vector.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">class</span> VectorSpace v =&gt; HasBasis v <span style="color: #050; font-weight: bold;">where</span>
  <span style="color: #050; font-weight: bold;">type</span> Basis v :: *
  basisValue   :: Basis v -&gt; v</pre>
</div></p>

<p>Another method extracts coordinates (coefficients) for a vector.
Each coordinate is associated with a basis element.</p>

<p><div>
<pre class="haskell">  decompose    :: v -&gt; <span style="color: green;">&#91;</span><span style="color: green;">&#40;</span>Basis v, Scalar v<span style="color: green;">&#41;</span><span style="color: green;">&#93;</span></pre>
</div></p>

<p>We can also reverse the process, recomposing into a vector, by linearly combining the basis elements:</p>

<p><div>
<pre class="haskell">linearCombo :: VectorSpace v =&gt; <span style="color: green;">&#91;</span><span style="color: green;">&#40;</span>v,Scalar v<span style="color: green;">&#41;</span><span style="color: green;">&#93;</span> -&gt; v
linearCombo ps = sumV <span style="color: green;">&#91;</span>s *^ v | <span style="color: green;">&#40;</span>v,s<span style="color: green;">&#41;</span> &lt;- ps<span style="color: green;">&#93;</span>
&nbsp;
recompose :: HasBasis v =&gt; <span style="color: green;">&#91;</span><span style="color: green;">&#40;</span>Basis v, Scalar v<span style="color: green;">&#41;</span><span style="color: green;">&#93;</span> -&gt; v
recompose = linearCombo . <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/Control-Arrow.html#v:first"><span style="font-weight: bold;">first</span></a> basisValue<span style="color: green;">&#41;</span></pre>
</div></p>

<p>The defining property is</p>

<p><div>
<pre class="haskell">recompose . decompose == <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:id"><span style="font-weight: bold;">id</span></a></pre>
</div></p>

<p><strong>Exercise</strong>: why might <code>decompose . recompose</code> not be the identity?  What if the decomposition were represented instead as a <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/containers/Data-Map.html">finite map</a>?</p>

<h3>Primitive bases</h3>

<p>Any scalar field is also a vector space over itself.
For instance,</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> AdditiveGroup <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Double"><span style="color: #833; font-weight: bold;">Double</span></a> <span style="color: #050; font-weight: bold;">where</span>
  zeroV   = <span style="color: red;">0.0</span>
  <span style="color: green;">&#40;</span>^+^<span style="color: green;">&#41;</span>   = <span style="color: green;">&#40;</span>+<span style="color: green;">&#41;</span>
  negateV = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:negate"><span style="font-weight: bold;">negate</span></a>
&nbsp;
<span style="color: #050; font-weight: bold;">instance</span> VectorSpace <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Double"><span style="color: #833; font-weight: bold;">Double</span></a> <span style="color: #050; font-weight: bold;">where</span>
  <span style="color: #050; font-weight: bold;">type</span> Scalar <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Double"><span style="color: #833; font-weight: bold;">Double</span></a> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Double"><span style="color: #833; font-weight: bold;">Double</span></a>
  <span style="color: green;">&#40;</span>*^<span style="color: green;">&#41;</span> = <span style="color: green;">&#40;</span>*<span style="color: green;">&#41;</span></pre>
</div></p>

<p>The canonical basis of a one-dimensional space has only one element, namely <code>1</code>, which can be represented with no information.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> HasBasis <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Double"><span style="color: #833; font-weight: bold;">Double</span></a> <span style="color: #050; font-weight: bold;">where</span>
  <span style="color: #050; font-weight: bold;">type</span> Basis <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Double"><span style="color: #833; font-weight: bold;">Double</span></a> = <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>
  basisValue <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>     = <span style="color: red;">1</span>
  decompose s       = <span style="color: green;">&#91;</span><span style="color: green;">&#40;</span><span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>,s<span style="color: green;">&#41;</span><span style="color: green;">&#93;</span></pre>
</div></p>

<h3>Composing bases</h3>

<p>Pairs of additive groups form additive groups:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span>AdditiveGroup u,AdditiveGroup v<span style="color: green;">&#41;</span>
      =&gt; AdditiveGroup <span style="color: green;">&#40;</span>u,v<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  zeroV             = <span style="color: green;">&#40;</span>zeroV,zeroV<span style="color: green;">&#41;</span>
  <span style="color: green;">&#40;</span>u,v<span style="color: green;">&#41;</span> ^+^ <span style="color: green;">&#40;</span>u&#8217;,v&#8217;<span style="color: green;">&#41;</span> = <span style="color: green;">&#40;</span>u^+^u&#8217;,v^+^v&#8217;<span style="color: green;">&#41;</span>
  negateV <span style="color: green;">&#40;</span>u,v<span style="color: green;">&#41;</span>     = <span style="color: green;">&#40;</span>negateV u,negateV v<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Pairs of vector spaces, over the same scalar field, form vector spaces:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span>VectorSpace u,VectorSpace v, Scalar u ~ Scalar v<span style="color: green;">&#41;</span>
      =&gt; VectorSpace <span style="color: green;">&#40;</span>u,v<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <span style="color: #050; font-weight: bold;">type</span> Scalar <span style="color: green;">&#40;</span>u,v<span style="color: green;">&#41;</span> = Scalar u
  s *^ <span style="color: green;">&#40;</span>u,v<span style="color: green;">&#41;</span> = <span style="color: green;">&#40;</span>s*^u,s*^v<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Given vector spaces <code>u</code> and <code>v</code>, a basis representation for <code>(u,v)</code> will be one basis representation or the other, tagged with <code>Left</code> or <code>Right</code>:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span>HasBasis u, HasBasis v, Scalar u ~ Scalar v<span style="color: green;">&#41;</span>
      =&gt; HasBasis <span style="color: green;">&#40;</span>u,v<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <span style="color: #050; font-weight: bold;">type</span> Basis <span style="color: green;">&#40;</span>u,v<span style="color: green;">&#41;</span> = Basis u `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Either"><span style="color: #833; font-weight: bold;">Either</span></a>` Basis v</pre>
</div></p>

<p>The basis vectors themselves will be <code>(ub,0)</code> or <code>(0,vb)</code>, where <code>ub</code> is a basis vector for <code>u</code>, and <code>vb</code> is a basis vector for <code>v</code>.
As expected then, the dimensionality of the cross product is the sum of the dimensions.</p>

<p><div>
<pre class="haskell">  basisValue <span style="color: green;">&#40;</span>Left  a<span style="color: green;">&#41;</span> = <span style="color: green;">&#40;</span>basisValue a, zeroV<span style="color: green;">&#41;</span>
  basisValue <span style="color: green;">&#40;</span>Right b<span style="color: green;">&#41;</span> = <span style="color: green;">&#40;</span>zeroV, basisValue b<span style="color: green;">&#41;</span></pre>
</div></p>

<p>The decomposition of a vector <code>(u,v)</code> contains left-tagged decompositions of <code>u</code> and right-tagged decompositions of <code>v</code>.</p>

<p><div>
<pre class="haskell">  decompose <span style="color: green;">&#40;</span>u,v<span style="color: green;">&#41;</span> = decomp2 Left u ++ decomp2 Right v</pre>
</div></p>

<p>where</p>

<p><div>
<pre class="haskell">decomp2 :: HasBasis w =&gt; <span style="color: green;">&#40;</span>Basis w -&gt; b<span style="color: green;">&#41;</span> -&gt; w -&gt; <span style="color: green;">&#91;</span><span style="color: green;">&#40;</span>Scalar w, b<span style="color: green;">&#41;</span><span style="color: green;">&#93;</span>
decomp2 inject = <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/Control-Arrow.html#v:first"><span style="font-weight: bold;">first</span></a> inject<span style="color: green;">&#41;</span> . decompose</pre>
</div></p>

<p>Triples etc, can be handled similarly.
Instead, the library implementation reduces them to the pair case:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span> HasBasis u, s ~ Scalar u
         , HasBasis v, s ~ Scalar v
         , HasBasis w, s ~ Scalar w <span style="color: green;">&#41;</span>
      =&gt; HasBasis <span style="color: green;">&#40;</span>u,v,w<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <span style="color: #050; font-weight: bold;">type</span> Basis <span style="color: green;">&#40;</span>u,v,w<span style="color: green;">&#41;</span> = Basis <span style="color: green;">&#40;</span>u,<span style="color: green;">&#40;</span>v,w<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
  &#8230;</pre>
</div></p>

<p><strong>Exercise</strong>: complete this instance definition (without peeking).</p>

<h3>Bases in spaces</h3>

<p>What about other vector spaces, particularly infinite dimensional ones?
The result type for <code>decompose</code> is not convenient:</p>

<p><div>
<pre class="haskell">decompose :: v -&gt; <span style="color: green;">&#91;</span><span style="color: green;">&#40;</span>Basis v, Scalar v<span style="color: green;">&#41;</span><span style="color: green;">&#93;</span></pre>
</div></p>

<p>Moreover, its definition for pair types would have to be changed, e.g., to use interleaving instead of append.
On the other hand, this type can be thought of as an association list, representing <code>Basis v -&gt; Scalar v</code>.
Instead, we might use the function representation directly:</p>

<p><div>
<pre class="haskell">decompose :: v -&gt; <span style="color: green;">&#40;</span>Basis v -&gt; Scalar v<span style="color: green;">&#41;</span></pre>
</div></p>

<p>In that case, the definition of <code>decompose</code> on pairs is</p>

<p><div>
<pre class="haskell">decompose <span style="color: green;">&#40;</span>u,v<span style="color: green;">&#41;</span> = decompose u `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:either"><span style="font-weight: bold;">either</span></a>` decompose v</pre>
</div></p>

<p>which beautifully mirrors the basis type definition:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> Basis <span style="color: green;">&#40;</span>u,v<span style="color: green;">&#41;</span> = Basis u `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Either"><span style="color: #833; font-weight: bold;">Either</span></a>` Basis v</pre>
</div></p>

<p>I guess we&#8217;d have to somehow extend the definition of <code>recompose</code> as well, or avoid it.</p>

<p>One example of an infinite dimensional vector space is a function over an infinite domain.
The additive group and vector space instances follow a standard form for applicative functors applied to an additive group or vector space.
In this case, the applicative functor is <code>((-&gt;) a)</code>.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> AdditiveGroup v =&gt; AdditiveGroup <span style="color: green;">&#40;</span>a -&gt; v<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  zeroV   = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:pure"><span style="font-weight: bold;">pure</span></a>   zeroV
  <span style="color: green;">&#40;</span>^+^<span style="color: green;">&#41;</span>   = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:liftA2"><span style="font-weight: bold;">liftA2</span></a> <span style="color: green;">&#40;</span>^+^<span style="color: green;">&#41;</span>
  negateV = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a>   negateV
&nbsp;
<span style="color: #050; font-weight: bold;">instance</span> VectorSpace v =&gt; VectorSpace <span style="color: green;">&#40;</span>a -&gt; v<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <span style="color: #050; font-weight: bold;">type</span> Scalar <span style="color: green;">&#40;</span>a -&gt; v<span style="color: green;">&#41;</span> = Scalar v
  <span style="color: green;">&#40;</span>*^<span style="color: green;">&#41;</span> s = <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;">&#40;</span>*^<span style="color: green;">&#41;</span> s<span style="color: green;">&#41;</span></pre>
</div></p>

<p>As a basis for a function space <code>a-&gt;u</code>, let&#8217;s use the subset of functions that map one domain value to some basis vector for <code>u</code> and map all other domain values to zero.
By linearly combining these functions, we can construct <em>any</em> function in <code>a-&gt;u</code> that is nonzero for finitely many domain values.
If we stretch the notion of linear combinations beyond finite combinations, perhaps we can go further and cover at least the countably infinite domain types.</p>

<p>To represent these functions, it suffices to record the choice of domain element and the representation of the corresponding basis vector for <code>u</code>:</p>

<p><div>
<pre class="haskell"><span style="color: #050; 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:Eq"><span style="color: #833; font-weight: bold;">Eq</span></a> a, HasBasis u<span style="color: green;">&#41;</span> =&gt; HasBasis <span style="color: green;">&#40;</span>a -&gt; u<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <span style="color: #050; font-weight: bold;">type</span> Basis <span style="color: green;">&#40;</span>a -&gt; u<span style="color: green;">&#41;</span> = <span style="color: green;">&#40;</span>a, Basis u<span style="color: green;">&#41;</span>
&nbsp;
  basisValue <span style="color: green;">&#40;</span>a,b<span style="color: green;">&#41;</span> a&#8217; | a == a&#8217;   = basisValue b
                      | <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:otherwise"><span style="font-weight: bold;">otherwise</span></a> = zeroV</pre>
</div></p>

<p>The actual implementation is a bit more efficient, avoiding recomputation of <code>basisValue b</code> for each <code>a'</code>.</p>

<p>Decomposing a function into its coordinates is even simpler:</p>

<p><div>
<pre class="haskell">  decompose g <span style="color: green;">&#40;</span>a,b<span style="color: green;">&#41;</span> = decompose <span style="color: green;">&#40;</span>g a<span style="color: green;">&#41;</span> b</pre>
</div></p>

<h3>Some isomorphisms</h3>

<p>This instance rule for functions will be applied repeatedly for curried functions.
For instance, <code>Basis (a -&gt; b -&gt; u) == (a, (b, Basis u))</code>.
The isomorphic uncurried form has an isomorphic basis: <code>Basis ((a,b) -&gt; u) == ((a,b), Basis u)</code>.</p>

<p>Pairing in the range instead of domain gives rise to another pair of isomorphisms:</p>

<p><div>
<pre class="haskell">Basis <span style="color: green;">&#40;</span>a -&gt; <span style="color: green;">&#40;</span>b,c<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> == <span style="color: green;">&#40;</span>a, Basis b `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Either"><span style="color: #833; font-weight: bold;">Either</span></a>` Basis c<span style="color: green;">&#41;</span>
&nbsp;
Basis <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>a -&gt; b, a -&gt; c<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> == <span style="color: green;">&#40;</span>a, Basis b<span style="color: green;">&#41;</span> `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Either"><span style="color: #833; font-weight: bold;">Either</span></a>` <span style="color: green;">&#40;</span>a, Basis c<span style="color: green;">&#41;</span></pre>
</div></p>

<p>The rules for basis types look like logarithms, especially if we add a basis for <code>()</code>:</p>

<p><div>
<pre class="haskell">  <span style="color: #050; font-weight: bold;">type</span> Basis <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span> = Void
&nbsp;
  <span style="color: #050; font-weight: bold;">type</span> Basis <span style="color: green;">&#40;</span>u,v<span style="color: green;">&#41;</span> = Basis u `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Either"><span style="color: #833; font-weight: bold;">Either</span></a>` Basis v
&nbsp;
  <span style="color: #050; font-weight: bold;">type</span> Basis <span style="color: green;">&#40;</span>a -&gt; u<span style="color: green;">&#41;</span> = <span style="color: green;">&#40;</span>a, Basis u<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Compare with:</p>

<p><div class=latex><img src='http://conal.net/blog/wp-content/latex/55a/55a3af2c856a2d1c288ff9b772f992d4-ffffff000000.png' alt='\log 1 = 0' class='latex' /></div>
<div class=latex><img src='http://conal.net/blog/wp-content/latex/f39/f39335e2b42b7ccdc2a819f438fdf703-ffffff000000.png' alt='\log (u \times v) = \log u + \log v' class='latex' /></div>
<div class=latex><img src='http://conal.net/blog/wp-content/latex/fcd/fcd90ab1b3200b1673b7d3070f42420c-ffffff000000.png' alt='\log (u ^ a) = a \times \log u' class='latex' /></div></p>

<p>The rules are also essentially the same as the ones used for <a href="http://conal.net/blog/posts/elegant-memoization-with-functional-memo-tries/" title="blog post">memo tries</a>, but phrased in terms of logarithms instead of (explicit) exponents.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~f/conal?a=EUvEM"><img src="http://feeds.feedburner.com/~f/conal?i=EUvEM" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/conal?a=M2xam"><img src="http://feeds.feedburner.com/~f/conal?i=M2xam" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/conal?a=7nXWm"><img src="http://feeds.feedburner.com/~f/conal?i=7nXWm" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/conal/~4/425935010" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/vector-space-bases-via-type-families/feed/</wfw:commentRss>
		<feedburner:origLink>http://conal.net/blog/posts/vector-space-bases-via-type-families/</feedburner:origLink></item>
		<item>
		<title>Composing memo tries</title>
		<link>http://feeds.feedburner.com/~r/conal/~3/422189172/</link>
		<comments>http://conal.net/blog/posts/composing-memo-tries/#comments</comments>
		<pubDate>Thu, 16 Oct 2008 02:18:12 +0000</pubDate>
		<dc:creator>conal</dc:creator>
		
		<category><![CDATA[Functional programming]]></category>

		<category><![CDATA[memoization]]></category>

		<category><![CDATA[morphisms]]></category>

		<category><![CDATA[tries]]></category>

		<category><![CDATA[type classes]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=55</guid>
		<description><![CDATA[





The post Elegant memoization with functional memo tries showed a simple type of search tries and their use for functional memoization of functions.
This post provides some composition tools for memo tries, whose definitions are inevitable, in that they are determined by the principle presented in Simplifying semantics with type class morphisms.







Compositional semantics and type class [...]]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Composing memo tries

Tags: memoization, tries, type classes, morphisms

URL: http://conal.net/blog/posts/composing-memo-tries/

-->

<!-- references -->

<!-- teaser -->

<p>The post <em><a href="http://conal.net/blog/posts/elegant-memoization-with-functional-memo-tries/" title="blog post">Elegant memoization with functional memo tries</a></em> showed a simple type of search tries and their use for functional memoization of functions.
This post provides some composition tools for memo tries, whose definitions are <em>inevitable</em>, in that they are determined by the principle presented in <em><a href="http://conal.net/blog/posts/simplifying-semantics-with-type-class-morphisms" title="blog post">Simplifying semantics with type class morphisms</a></em>.</p>

<!--
**Edits**:

* 2008-02-09: just fiddling around
-->

<!-- without a comment or something here, the last item above becomes a paragraph -->

<p><span id="more-55"></span></p>

<h3>Compositional semantics and type class morphisms</h3>

<p>The discipline of denotational semantics defines meaning functions <em>compositionally</em>, i.e., the meaning of a construct must be some function of the meanings of its components.</p>

<p><a href="http://conal.net/blog/posts/simplifying-semantics-with-type-class-morphisms" title="blog post">Type class morphisms</a> make the denotational discipline even more specific:</p>

<blockquote>
  <p>The meaning of each method corresponds to the same method for the meaning.</p>
</blockquote>

<p>For instance,</p>

<p><div>
<pre class="haskell">meaning <span style="color: green;">&#40;</span>a `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mappend"><span style="font-weight: bold;">mappend</span></a>` b<span style="color: green;">&#41;</span> == meaning a `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mappend"><span style="font-weight: bold;">mappend</span></a>` meaning b</pre>
</div></p>

<h3>Memo tries, semantics, and morphisms</h3>

<p>The semantic function for a memo trie is <code>untrie</code>, which converts a trie (back) to a function:</p>

<p><div>
<pre class="haskell">untrie :: <span style="color: green;">&#40;</span>a :-&gt;: b<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</span>a  -&gt;  b<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Let&#8217;s look at the consequences of requiring that <code>untrie</code> be a morphism over <code>Monoid</code>, <code>Functor</code>, <code>Applicative</code>, <code>Monad</code>, <code>Category</code>, and <code>Arrow</code>, i.e.,</p>

<p><div>
<pre class="haskell">untrie <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mempty"><span style="font-weight: bold;">mempty</span></a>          == <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mempty"><span style="font-weight: bold;">mempty</span></a>
untrie <span style="color: green;">&#40;</span>s `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mappend"><span style="font-weight: bold;">mappend</span></a>` t<span style="color: green;">&#41;</span> == untrie s `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mappend"><span style="font-weight: bold;">mappend</span></a>` untrie t
&nbsp;
untrie <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 t<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 <span style="color: green;">&#40;</span>untrie t<span style="color: green;">&#41;</span>
&nbsp;
untrie <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:pure"><span style="font-weight: bold;">pure</span></a> a<span style="color: green;">&#41;</span>        == <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:pure"><span style="font-weight: bold;">pure</span></a> a
untrie <span style="color: green;">&#40;</span>tf &lt;*&gt; tx<span style="color: green;">&#41;</span>     == untrie tf &lt;*&gt; untrie tx
&nbsp;
untrie <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html