<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Conal Elliott &#187; zip</title>
	<atom:link href="http://conal.net/blog/tag/zip/feed/" rel="self" type="application/rss+xml" />
	<link>http://conal.net/blog</link>
	<description>Inspirations &#38; experiments, mainly about denotational programming in Haskell</description>
	<lastBuildDate>Wed, 03 Feb 2010 17:10:16 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Enhancing a Zip</title>
		<link>http://conal.net/blog/posts/enhancing-a-zip/</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 functor]]></category>
		<category><![CDATA[fold]]></category>
		<category><![CDATA[functor]]></category>
		<category><![CDATA[proof]]></category>
		<category><![CDATA[type class morphism]]></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 functor, functor, fold, zip, type class 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>

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

<ul>
<li>2009-02-15: Simplified <code>WithCont</code>, collapsing two type parameters into one.  Added functor comment about <code>cfoldlc'</code>.</li>
</ul>

<!-- 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;">-- 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;">-- 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 z c = <span style="color: #050; font-weight: bold;">forall</span> a. WC <span style="color: green;">&#40;</span>z 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 b = WithCont <span style="color: green;">&#40;</span>Fold b<span style="color: green;">&#41;</span></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>(In a more general setting, use <code>fmap</code> in place of <code>(.)</code> to give meaning to <code>WithCont</code>.)</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' z c = <span style="color: #050; font-weight: bold;">forall</span> a. WC' <span style="color: green;">&#40;</span>z 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' b = WithCont' <span style="color: green;">&#40;</span>Fold b<span style="color: green;">&#41;</span>
&nbsp;
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>

<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 z<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 z =&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 z<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'<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'<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' z<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' z =&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' z<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>P a a'<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'<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'<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'<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'<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'<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'<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'<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'<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'<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 style=hidden>

&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-

What is the reasoning behind the ghc restriction that &#8220;A lazy (~) pattern cannot bind existential type variables&#8221;?

This error came up for me in the following code:

    &#8212; | Add a continuation.
    data WithCont z c = forall a. WC (z a) (a -> c)

    instance Functor (WithCont z) where
      fmap g ~(WC f k) = WC f (fmap g k)

The error message:

    A lazy (~) pattern cannot bind existential type variables
      `a&#8217; is a rigid type variable bound by
          the constructor `WC&#8217; at Data/Zip/FoldL.hs:66:11
    In the pattern: ~(WC f k)

I also tried this variation:

    instance Functor (WithCont z) where
      fmap g wc = WC f (fmap g k)
        where WC f k = wc

and got this message:

    My brain just exploded.
    I can&#8217;t handle pattern bindings for existentially-quantified constructors.
    Instead, use a case-expression, or do-notation, to unpack the constructor.
    In the binding group for
        WC f k
    In a pattern binding: WC f k = wc

I can work around these limitations by using a lambda:

    instance Functor (WithCont z) where
      fmap g = \ (WC f k) -> WC f (fmap g k)

which, iirc, is equivalent.

For infix definitions like (<*>), however, this work-around is less pleasant.
For instance,

    (<*>) = \ (WC hf hk) (WC xf xk) ->
      WC (hf `zip` xf) (\ (a,a&#8217;) -> (hk a) (xk a&#8217;))

instead of the forbidden

    instance Zip (z) => Applicative (WithCont z) where
      pure a = WC (error &#8220;unneeded pre-cont&#8221;) (pure a)
      ~(WC hf hk) <*> ~(WC xf xk) =
        WC (hf `zip` xf) (\ (a,a&#8217;) -> (hk a) (xk a&#8217;))

</div>
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/enhancing-a-zip/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Proofs for left fold zipping</title>
		<link>http://conal.net/blog/posts/proofs-for-left-fold-zipping/</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'<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'</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'<span style="color: green;">&#41;</span> bs == <span style="color: green;">&#40;</span>cfoldl f bs, cfoldl f' 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' e'<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' e'<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' e'<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'' <span style="color: green;">&#40;</span>e,e'<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'<span style="color: green;">&#41;</span> `op''` b = <span style="color: green;">&#40;</span>a `op` b, a' `op'` 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'' <span style="color: green;">&#40;</span>e,e'<span style="color: green;">&#41;</span> bs
  <span style="color: #050; font-weight: bold;">where</span> <span style="color: green;">&#40;</span>a,a'<span style="color: green;">&#41;</span> `op''` b = <span style="color: green;">&#40;</span>a `op` b, a' `op'` 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' e'<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' e' 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'' <span style="color: green;">&#40;</span>e,e'<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' e' bs<span style="color: green;">&#41;</span>
  <span style="color: #050; font-weight: bold;">where</span>
    <span style="color: green;">&#40;</span>a,a'<span style="color: green;">&#41;</span> `op''` b = <span style="color: green;">&#40;</span>a `op` b, a' `op'` 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> op <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'' <span style="color: green;">&#40;</span>e,e'<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'' = ...
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- foldl def  -}</span>
&nbsp;
<span style="color: green;">&#40;</span>e,e'<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' e' <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'' <span style="color: green;">&#40;</span>e,e'<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'' = ...
&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'' <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>e,e'<span style="color: green;">&#41;</span> `op''` b<span style="color: green;">&#41;</span> bs <span style="color: #050; font-weight: bold;">where</span> op'' = ...
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- op'' 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'' <span style="color: green;">&#40;</span>e `op` b, e' `op'` b<span style="color: green;">&#41;</span> bs <span style="color: #050; font-weight: bold;">where</span> op'' = ...
&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' <span style="color: green;">&#40;</span>e' `op'` 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' e' <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>'` f'<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'</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>' op'' <span style="color: green;">&#40;</span>P e e'<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>' 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>' op' e' bs<span style="color: green;">&#41;</span>
  <span style="color: #050; font-weight: bold;">where</span>
    P a a' `op''` b = P <span style="color: green;">&#40;</span>a `op` b<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>a' `op'` 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>' :: <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> =
  <span style="color: #050; font-weight: bold;">let</span> a' = a `op` b <span style="color: #050; font-weight: bold;">in</span> a' `<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>' f a' 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'' <span style="color: green;">&#40;</span>P e e'<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'' = ...
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- foldl def  -}</span>
&nbsp;
P e e'
&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' e' <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>' op'' <span style="color: green;">&#40;</span>P e e'<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'' = ...
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- foldl' def -}</span>
&nbsp;
<span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>P e e'<span style="color: green;">&#41;</span> `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>' op'' <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>P e e'<span style="color: green;">&#41;</span> `op''` b<span style="color: green;">&#41;</span> bs <span style="color: #050; font-weight: bold;">where</span> op'' = ...
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- op'' 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' `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>' op'' <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' `op'` 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' `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>`
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 <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>' op' <span style="color: green;">&#40;</span>e' `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' `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>`
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>' 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' `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>' op' <span style="color: green;">&#40;</span>e' `op` 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;
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;">&#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>' op' e' <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>
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/proofs-for-left-fold-zipping/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>More beautiful fold zipping</title>
		<link>http://conal.net/blog/posts/more-beautiful-fold-zipping/</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>' :: <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;">-- 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;">-- 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' <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</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' = P !c !c'
&nbsp;
zipF' :: Fold b a -&gt; Fold b a' -&gt; Fold b <span style="color: green;">&#40;</span>P a a'<span style="color: green;">&#41;</span>
F op e `zipF'` F op' e' = F op'' <span style="color: green;">&#40;</span>P e e'<span style="color: green;">&#41;</span>
 <span style="color: #050; font-weight: bold;">where</span>
   P a a' `op''` b = P <span style="color: green;">&#40;</span>a `op` b<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>a' `op'` 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' -&gt; Fold b <span style="color: green;">&#40;</span>a,a'<span style="color: green;">&#41;</span>
F op e `zipF` F op' e' = F op'' <span style="color: green;">&#40;</span>e,e'<span style="color: green;">&#41;</span>
 <span style="color: #050; font-weight: bold;">where</span>
   <span style="color: green;">&#40;</span>a,a'<span style="color: green;">&#41;</span> `op''` b = <span style="color: green;">&#40;</span>a `op` b, a' `op'` 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' 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>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' <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>

<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>'` 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>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' <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</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>
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/more-beautiful-fold-zipping/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Another lovely example of type class morphisms</title>
		<link>http://conal.net/blog/posts/another-lovely-example-of-type-class-morphisms/</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 functor]]></category>
		<category><![CDATA[fold]]></category>
		<category><![CDATA[functor]]></category>
		<category><![CDATA[semantics]]></category>
		<category><![CDATA[type class]]></category>
		<category><![CDATA[type class morphism]]></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 functor, functor, type class morphism, semantics, type class, 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' <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>' 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>' :: <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> =
  <span style="color: #050; font-weight: bold;">let</span> a' = a `op` b <span style="color: #050; font-weight: bold;">in</span> a' `<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>' f a' 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' = P !c !c'</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' e' k' = F op'' e'' k''
   <span style="color: #050; font-weight: bold;">where</span>
     P a a' `op''` b = P <span style="color: green;">&#40;</span>a `op` b<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>a' `op'` b<span style="color: green;">&#41;</span>
     e''             = P e e'
     k'' <span style="color: green;">&#40;</span>P a a'<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' a'<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' f xs<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>cfoldl' g xs<span style="color: green;">&#41;</span> == cfoldl' <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' f<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>cfoldl' g<span style="color: green;">&#41;</span> == cfoldl' <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' f &lt;*&gt; cfoldl' g == cfoldl' <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' <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' 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' <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' 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' <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' 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' 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' <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' <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' -}</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>' 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>' 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>' op e
&nbsp;
  ==  <span style="color: #5d478b; font-style: italic;">{- uninline cfoldl' -}</span>
&nbsp;
cfoldl' <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' <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' -}</span>
&nbsp;
cfoldl' <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>
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/another-lovely-example-of-type-class-morphisms/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
