<?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; applicative functor</title>
	<atom:link href="http://conal.net/blog/tag/applicative-functor/feed" rel="self" type="application/rss+xml" />
	<link>http://conal.net/blog</link>
	<description>Inspirations &#38; experiments, mainly about denotative/functional programming in Haskell</description>
	<lastBuildDate>Thu, 25 Jul 2019 18:15:11 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.1.17</generator>
	<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=conal&amp;popout=1&amp;url=http%3A%2F%2Fconal.net%2Fblog%2F&amp;language=en_US&amp;category=text&amp;title=Conal+Elliott&amp;description=Inspirations+%26amp%3B+experiments%2C+mainly+about+denotative%2Ffunctional+programming+in+Haskell&amp;tags=blog" type="text/html" />
	<item>
		<title>Doing more with length-typed vectors</title>
		<link>http://conal.net/blog/posts/doing-more-with-length-typed-vectors</link>
		<comments>http://conal.net/blog/posts/doing-more-with-length-typed-vectors#comments</comments>
		<pubDate>Mon, 31 Jan 2011 01:16:09 +0000</pubDate>
		<dc:creator><![CDATA[Conal]]></dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[applicative functor]]></category>
		<category><![CDATA[monad]]></category>
		<category><![CDATA[number]]></category>
		<category><![CDATA[vector]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=293</guid>
		<description><![CDATA[The post Fixing lists defined a (commonly used) type of vectors, whose lengths are determined statically, by type. In Vec n a, the length is n, and the elements have type a, where n is a type-encoded unary number, built up from zero and successor (Z and S). infixr 5 :&#60;data Vec &#8759; * &#8594; [&#8230;]]]></description>
				<content:encoded><![CDATA[<!-- teaser -->

<p
>The post <a href="http://conal.net/blog/posts/fixing-lists/" title="blog post"
  ><em
    >Fixing lists</em
    ></a
  > defined a (commonly used) type of vectors, whose lengths are determined statically, by type. In <code
  >Vec n a</code
  >, the length is <code
  >n</code
  >, and the elements have type <code
  >a</code
  >, where <code
  >n</code
  > is a type-encoded unary number, built up from zero and successor (<code
  >Z</code
  > and <code
  >S</code
  >).</p
>

<pre class="sourceCode haskell"
><code
  ><span class="kw"
    >infixr</span
    > <span class="dv"
    >5</span
    > <span class="fu"
    >:&lt;</span
    ><br
     /><br
     /><span class="kw"
    >data</span
    > <span class="dt"
    >Vec</span
    > <span class="dv"
    >&#8759;</span
    > <span class="fu"
    >*</span
    > &#8594; <span class="fu"
    >*</span
    > &#8594; <span class="fu"
    >*</span
    > <span class="kw"
    >where</span
    ><br
     />  <span class="dt"
    >ZVec</span
    > <span class="dv"
    >&#8759;</span
    >                <span class="dt"
    >Vec</span
    > <span class="dt"
    >Z</span
    >     a<br
     />  (<span class="fu"
    >:&lt;</span
    >) <span class="dv"
    >&#8759;</span
    > a &#8594; <span class="dt"
    >Vec</span
    > n a &#8594; <span class="dt"
    >Vec</span
    > (<span class="dt"
    >S</span
    > n) a<br
     /></code
  ></pre
>

<p
>It was fairly easy to define <code
  >foldr</code
  > for a <code
  >Foldable</code
  > instance, <code
  >fmap</code
  > for <code
  >Functor</code
  >, and <code
  >(&#8859;)</code
  > for <code
  >Applicative</code
  >. Completing the <code
  >Applicative</code
  > instance is tricky, however. Unlike <code
  >foldr</code
  >, <code
  >fmap</code
  >, and <code
  >(&#8859;)</code
  >, <code
  >pure</code
  > doesn't have a vector structure to crawl over. It must create just the right structure anyway. I left this challenge as a question to amuse readers. In this post, I give a few solutions, including my current favorite.</p
>

<p
>You can find the code for this post and the two <a href="http://conal.net/blog/posts/type-bounded-numbers/" title="blog post"
  >previous</a
  > <a href="http://conal.net/blog/posts/fixing-lists/" title="blog post"
  >ones</a
  > in a <a href="https://github.com/conal/numbers-vectors-trees/" title="github repository"
  >code repository</a
  >.</p
>

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

<div id="an-applicative-instance"
><h3
  >An Applicative instance</h3
  ><p
  >As a review, here is our <code
    >Functor</code
    > instance:</p
  ><pre class="sourceCode haskell"
  ><code
    ><span class="kw"
      >instance</span
      > <span class="kw"
      >Functor</span
      > (<span class="dt"
      >Vec</span
      > n) <span class="kw"
      >where</span
      ><br
       />  <span class="fu"
      >fmap</span
      > _ <span class="dt"
      >ZVec</span
      >     <span class="fu"
      >=</span
      > <span class="dt"
      >ZVec</span
      ><br
       />  <span class="fu"
      >fmap</span
      > f (a <span class="fu"
      >:&lt;</span
      > u) <span class="fu"
      >=</span
      > f a <span class="fu"
      >:&lt;</span
      > <span class="fu"
      >fmap</span
      > f u<br
       /></code
    ></pre
  ><p
  >And part of an <code
    >Applicative</code
    > instance:</p
  ><pre class="sourceCode haskell"
  ><code
    ><span class="kw"
      >instance</span
      > <span class="dt"
      >Applicative</span
      > (<span class="dt"
      >Vec</span
      > n) <span class="kw"
      >where</span
      ><br
       />  pure a <span class="fu"
      >=</span
      > <span class="fu"
      >??</span
      ><br
       />  <span class="dt"
      >ZVec</span
      >      &#8859; <span class="dt"
      >ZVec</span
      >      <span class="fu"
      >=</span
      > <span class="dt"
      >ZVec</span
      ><br
       />  (f <span class="fu"
      >:&lt;</span
      > fs) &#8859; (x <span class="fu"
      >:&lt;</span
      > xs) <span class="fu"
      >=</span
      > f x <span class="fu"
      >:&lt;</span
      > (fs &#8859; xs)<br
       /></code
    ></pre
  ><p
  >For <code
    >pure</code
    >, recall the troublesome goal signature:</p
  ><pre class="sourceCode haskell"
  ><code
    >  pure <span class="dv"
      >&#8759;</span
      > a &#8594; <span class="dt"
      >Vec</span
      > n a<br
       /></code
    ></pre
  ><p
  >There's at least one very good reason this type is problematic. The type <code
    >n</code
    > is <em
    >completely</em
    > unrestricted. There is nothing to require <code
    >n</code
    > to be a natural number type, rather than <code
    >Bool</code
    >, <code
    >String</code
    >, <code
    >String &#8594; Bool</code
    >, etc.</p
  ><p
  >In contrast to this difficulty with <code
    >pure</code
    >, consider what if <code
    >n &#8801; String</code
    > in the type of <code
    >fmap</code
    >:</p
  ><pre class="sourceCode haskell"
  ><code
    ><span class="fu"
      >fmap</span
      > <span class="dv"
      >&#8759;</span
      > (a &#8594; b) &#8594; <span class="dt"
      >Vec</span
      > n a &#8594; <span class="dt"
      >Vec</span
      > n b<br
       /></code
    ></pre
  ><p
  >The definition of <code
    >Vec</code
    > guarantees that that there are no values of type <code
    >Vec String a</code
    >. So it's vacuously easy to cover that case (with an empty function). Similarly for <code
    >(&#8859;)</code
    >.</p
  ><p
  >If we were to somehow define <code
    >pure</code
    > with the type given above, then <code
    >pure ()</code
    > would have type <code
    >Vec String ()</code
    > (among many other types). However, there are no values of that type. Hence, <code
    >pure</code
    > cannot be defined without restricting <code
    >n</code
    >.</p
  ><p
  >Since the essential difficulty here is the unrestricted nature of <code
    >n</code
    >, let's look at restricting it. We'll want to include exactly the types that can arise in constructing <code
    >Vec</code
    > values, namely <code
    >Z</code
    >, <code
    >S Z</code
    >, <code
    >S (S Z)</code
    >, <code
    >S (S (S Z))</code
    >, etc.</p
  ><p
  >As a first try, define a class with two instances:</p
  ><pre class="sourceCode haskell"
  ><code
    ><span class="kw"
      >class</span
      > <span class="dt"
      >IsNat</span
      > n<br
       /><br
       /><span class="kw"
      >instance</span
      > <span class="dt"
      >IsNat</span
      > <span class="dt"
      >Z</span
      ><br
       /><span class="kw"
      >instance</span
      > <span class="dt"
      >IsNat</span
      > n &#8658; <span class="dt"
      >IsNat</span
      > (<span class="dt"
      >S</span
      > n)<br
       /></code
    ></pre
  ><p
  >Then change the <code
    >Applicative</code
    > instance to require <code
    >IsNat n</code
    >:</p
  ><pre class="sourceCode haskell"
  ><code
    ><span class="kw"
      >instance</span
      > <span class="dt"
      >IsNat</span
      > n &#8658; <span class="dt"
      >Applicative</span
      > (<span class="dt"
      >Vec</span
      > n) <span class="kw"
      >where</span
      ><br
       />  &#8943;<br
       /></code
    ></pre
  ><p
  >The definition of <code
    >(&#8859;)</code
    > given above still type-checks. Well, not quite. Really, the recursive call to <code
    >(&#8859;)</code
    > fails to type-check, because the <code
    >IsNat</code
    > constraint cannot be proved. One solution is to add that constraint to the vector type:</p
  ><pre class="sourceCode haskell"
  ><code
    ><span class="kw"
      >data</span
      > <span class="dt"
      >Vec</span
      > <span class="dv"
      >&#8759;</span
      > <span class="fu"
      >*</span
      > &#8594; <span class="fu"
      >*</span
      > &#8594; <span class="fu"
      >*</span
      > <span class="kw"
      >where</span
      ><br
       />  <span class="dt"
      >ZVec</span
      > <span class="dv"
      >&#8759;</span
      > <span class="dt"
      >Vec</span
      > <span class="dt"
      >Z</span
      > a<br
       />  (<span class="fu"
      >:&lt;</span
      >) <span class="dv"
      >&#8759;</span
      > <span class="dt"
      >IsNat</span
      > n &#8658; a &#8594; <span class="dt"
      >Vec</span
      > n a &#8594; <span class="dt"
      >Vec</span
      > (<span class="dt"
      >S</span
      > n) a<br
       /></code
    ></pre
  ><p
  >Another is to break the definition <code
    >(&#8859;)</code
    > out into a separate recursion that omits the <code
    >IsNat</code
    > constraint:</p
  ><pre class="sourceCode haskell"
  ><code
    ><span class="kw"
      >instance</span
      > <span class="dt"
      >IsNat</span
      > n &#8658; <span class="dt"
      >Applicative</span
      > (<span class="dt"
      >Vec</span
      > n) <span class="kw"
      >where</span
      ><br
       />  pure <span class="fu"
      >=</span
      > <span class="fu"
      >???</span
      ><br
       />  (&#8859;)  <span class="fu"
      >=</span
      > applyV<br
       /><br
       />applyV <span class="dv"
      >&#8759;</span
      > <span class="dt"
      >Vec</span
      > n (a &#8594; b) &#8594; <span class="dt"
      >Vec</span
      > n a &#8594; <span class="dt"
      >Vec</span
      > n b<br
       /><span class="dt"
      >ZVec</span
      >      <span class="ot"
      >`applyV`</span
      > <span class="dt"
      >ZVec</span
      >      <span class="fu"
      >=</span
      > <span class="dt"
      >ZVec</span
      ><br
       />(f <span class="fu"
      >:&lt;</span
      > fs) <span class="ot"
      >`applyV`</span
      > (x <span class="fu"
      >:&lt;</span
      > xs) <span class="fu"
      >=</span
      > f x <span class="fu"
      >:&lt;</span
      > (fs <span class="ot"
      >`applyV`</span
      > xs)<br
       /></code
    ></pre
  ><p
  >Now, how can we define <code
    >pure</code
    >? We still don't have enough structure. To get that structure, add a method to <code
    >IsNat</code
    >. That method could simply be the definition of <code
    >pure</code
    > that we need.</p
  ><pre class="sourceCode haskell"
  ><code
    ><span class="kw"
      >class</span
      > <span class="dt"
      >IsNat</span
      > n <span class="kw"
      >where</span
      > pureN <span class="dv"
      >&#8759;</span
      > a &#8594; <span class="dt"
      >Vec</span
      > n a<br
       /><br
       /><span class="kw"
      >instance</span
      > <span class="dt"
      >IsNat</span
      > <span class="dt"
      >Z</span
      >                <span class="kw"
      >where</span
      > pureN a <span class="fu"
      >=</span
      > <span class="dt"
      >ZVec</span
      ><br
       /><span class="kw"
      >instance</span
      > <span class="dt"
      >IsNat</span
      > n &#8658; <span class="dt"
      >IsNat</span
      > (<span class="dt"
      >S</span
      > n) <span class="kw"
      >where</span
      > pureN a <span class="fu"
      >=</span
      > a <span class="fu"
      >:&lt;</span
      > pureN a<br
       /></code
    ></pre
  ><p
  >To get this second instance to type-check, we'll have to add the constraint <code
    >IsNat n</code
    > to the <code
    >(:&lt;)</code
    > constructor in <code
    >Vec</code
    >. Then define <code
    >pure = pureN</code
    > for <code
    >Vec</code
    >.</p
  ><p
  >I prefer a variation on this solution. Instead of <code
    >pureN</code
    >, use a method that can only make vectors of <code
    >()</code
    >:</p
  ><pre class="sourceCode haskell"
  ><code
    ><span class="kw"
      >class</span
      > <span class="dt"
      >IsNat</span
      > n <span class="kw"
      >where</span
      > units <span class="dv"
      >&#8759;</span
      > <span class="dt"
      >Vec</span
      > n ()<br
       /><br
       /><span class="kw"
      >instance</span
      >            <span class="dt"
      >IsNat</span
      > <span class="dt"
      >Z</span
      >     <span class="kw"
      >where</span
      > units <span class="fu"
      >=</span
      > <span class="dt"
      >ZVec</span
      ><br
       /><span class="kw"
      >instance</span
      > <span class="dt"
      >IsNat</span
      > n &#8658; <span class="dt"
      >IsNat</span
      > (<span class="dt"
      >S</span
      > n) <span class="kw"
      >where</span
      > units <span class="fu"
      >=</span
      > () <span class="fu"
      >:&lt;</span
      > units<br
       /></code
    ></pre
  ><p
  >Then define</p
  ><pre class="sourceCode haskell"
  ><code
    >  pure a <span class="fu"
      >=</span
      > <span class="fu"
      >fmap</span
      > (<span class="fu"
      >const</span
      > a) units<br
       /></code
    ></pre
  ><p
  >Neat trick, huh? I got it from <a href="http://www.soi.city.ac.uk/~ross/papers/Applicative.html" title="Paper by Conor McBride and Ross Paterson"
    ><em
      >Applicative Programming with Effects</em
      ></a
    > (section 7).</p
  ></div
>

<div id="value-typed-natural-numbers"
><h3
  >Value-typed natural numbers</h3
  ><p
  >There's still another way to define <code
    >IsNat</code
    >, and it's the one I actually use.</p
  ><p
  >Define a type of natural number with matching value &amp; type:</p
  ><pre class="sourceCode haskell"
  ><code
    ><span class="kw"
      >data</span
      > <span class="dt"
      >Nat</span
      > <span class="dv"
      >&#8759;</span
      > <span class="fu"
      >*</span
      > &#8594; <span class="fu"
      >*</span
      > <span class="kw"
      >where</span
      ><br
       />  <span class="dt"
      >Zero</span
      > <span class="dv"
      >&#8759;</span
      >                    <span class="dt"
      >Nat</span
      > <span class="dt"
      >Z</span
      ><br
       />  <span class="dt"
      >Succ</span
      > <span class="dv"
      >&#8759;</span
      > <span class="dt"
      >IsNat</span
      > n &#8658; <span class="dt"
      >Nat</span
      > n &#8594; <span class="dt"
      >Nat</span
      > (<span class="dt"
      >S</span
      > n)<br
       /></code
    ></pre
  ><p
  >Interpret a <code
    >Nat</code
    > as an <code
    >Integer</code
    ></p
  ><pre class="sourceCode haskell"
  ><code
    >natToZ <span class="dv"
      >&#8759;</span
      > <span class="dt"
      >Nat</span
      > n &#8594; <span class="dt"
      >Integer</span
      ><br
       />natToZ <span class="dt"
      >Zero</span
      >     <span class="fu"
      >=</span
      > <span class="dv"
      >0</span
      ><br
       />natToZ (<span class="dt"
      >Succ</span
      > n) <span class="fu"
      >=</span
      > (<span class="fu"
      >succ</span
      > &#8728; natToZ) n<br
       /></code
    ></pre
  ><p
  >I wrote the second clause strangely to emphasize the following lovely property, which corresponds to a simple commutative diagram:</p
  ><pre class="sourceCode haskell"
  ><code
    >natToZ &#8728; <span class="dt"
      >Succ</span
      > <span class="fu"
      >=</span
      > <span class="fu"
      >succ</span
      > &#8728; natToZ<br
       /></code
    ></pre
  ><p
  >This <code
    >natToZ</code
    > function is handy for showing natural numbers:</p
  ><pre class="sourceCode haskell"
  ><code
    ><span class="kw"
      >instance</span
      > <span class="kw"
      >Show</span
      > (<span class="dt"
      >Nat</span
      > n) <span class="kw"
      >where</span
      > <span class="fu"
      >show</span
      > <span class="fu"
      >=</span
      > <span class="fu"
      >show</span
      > &#8728; natToZ<br
       /></code
    ></pre
  ><p
  >A fun &amp; strange thing about <code
    >Nat n</code
    > is that it can have at most one inhabitant for any type <code
    >n</code
    >. We can synthesize that inhabitant via an alternative definition of the <code
    >IsNat</code
    > class defined (twice) above:</p
  ><pre class="sourceCode haskell"
  ><code
    ><span class="kw"
      >class</span
      > <span class="dt"
      >IsNat</span
      > n <span class="kw"
      >where</span
      > nat <span class="dv"
      >&#8759;</span
      > <span class="dt"
      >Nat</span
      > n<br
       /><br
       /><span class="kw"
      >instance</span
      >            <span class="dt"
      >IsNat</span
      > <span class="dt"
      >Z</span
      >     <span class="kw"
      >where</span
      > nat <span class="fu"
      >=</span
      > <span class="dt"
      >Zero</span
      ><br
       /><span class="kw"
      >instance</span
      > <span class="dt"
      >IsNat</span
      > n &#8658; <span class="dt"
      >IsNat</span
      > (<span class="dt"
      >S</span
      > n) <span class="kw"
      >where</span
      > nat <span class="fu"
      >=</span
      > <span class="dt"
      >Succ</span
      > nat<br
       /></code
    ></pre
  ><p
  >Using this latest version of <code
    >IsNat</code
    >, we can easily define <code
    >units</code
    > (and hence <code
    >pure</code
    > on <code
    >Vec n</code
    > for <code
    >IsNat n</code
    >):</p
  ><pre class="sourceCode haskell"
  ><code
    >units <span class="dv"
      >&#8759;</span
      > <span class="dt"
      >IsNat</span
      > n &#8658; <span class="dt"
      >Vec</span
      > n ()<br
       />units <span class="fu"
      >=</span
      > unitsN nat<br
       /><br
       />unitsN <span class="dv"
      >&#8759;</span
      > <span class="dt"
      >Nat</span
      > n &#8594; <span class="dt"
      >Vec</span
      > n ()<br
       />unitsN <span class="dt"
      >Zero</span
      >     <span class="fu"
      >=</span
      > <span class="dt"
      >ZVec</span
      ><br
       />unitsN (<span class="dt"
      >Succ</span
      > n) <span class="fu"
      >=</span
      > () <span class="fu"
      >:&lt;</span
      > unitsN n<br
       /></code
    ></pre
  ><p
  >I prefer this latest <code
    >IsNat</code
    > definition over the previous two, because it relies only on <code
    >Nat</code
    >, which is simpler and more broadly useful than <code
    >Vec</code
    >. Examples abound, including improving an recent post, as we'll see now.</p
  ></div
>

<div id="revisiting-type-bounded-numbers"
><h3
  >Revisiting type-bounded numbers</h3
  ><p
  >The post <a href="http://conal.net/blog/posts/type-bounded-numbers/" title="blog post"
    ><em
      >Type-bounded numbers</em
      ></a
    > defined a type <code
    >BNat n</code
    > of natural numbers less than <code
    >n</code
    >, which can be used, for instance, as numerical digits in base <code
    >n</code
    >.</p
  ><pre class="sourceCode haskell"
  ><code
    ><span class="kw"
      >data</span
      > <span class="dt"
      >BNat</span
      > <span class="dv"
      >&#8759;</span
      > <span class="fu"
      >*</span
      > &#8594; <span class="fu"
      >*</span
      > <span class="kw"
      >where</span
      ><br
       />  <span class="dt"
      >BZero</span
      > <span class="dv"
      >&#8759;</span
      >          <span class="dt"
      >BNat</span
      > (<span class="dt"
      >S</span
      > n)<br
       />  <span class="dt"
      >BSucc</span
      > <span class="dv"
      >&#8759;</span
      > <span class="dt"
      >BNat</span
      > n &#8594; <span class="dt"
      >BNat</span
      > (<span class="dt"
      >S</span
      > n)<br
       /></code
    ></pre
  ><p
  >One useful operation is conversion from integer to <code
    >BNat n</code
    >. This operation had the awkward task of coming up with <code
    >BNat</code
    > structure. The solution given was to introduce a type class, with instances for <code
    >Z</code
    > and <code
    >S</code
    >:</p
  ><pre class="sourceCode haskell"
  ><code
    ><span class="kw"
      >class</span
      > <span class="dt"
      >HasBNat</span
      > n <span class="kw"
      >where</span
      > toBNat <span class="dv"
      >&#8759;</span
      > <span class="dt"
      >Integer</span
      > &#8594; <span class="dt"
      >Maybe</span
      > (<span class="dt"
      >BNat</span
      > n)<br
       /><br
       /><span class="kw"
      >instance</span
      > <span class="dt"
      >HasBNat</span
      > <span class="dt"
      >Z</span
      > <span class="kw"
      >where</span
      > toBNat _ <span class="fu"
      >=</span
      > <span class="kw"
      >Nothing</span
      ><br
       /><br
       /><span class="kw"
      >instance</span
      > <span class="dt"
      >HasBNat</span
      > n &#8658; <span class="dt"
      >HasBNat</span
      > (<span class="dt"
      >S</span
      > n) <span class="kw"
      >where</span
      ><br
       />  toBNat m <span class="fu"
      >|</span
      > m <span class="fu"
      >&lt;</span
      > <span class="dv"
      >1</span
      >     <span class="fu"
      >=</span
      > <span class="kw"
      >Just</span
      > <span class="dt"
      >BZero</span
      ><br
       />           <span class="fu"
      >|</span
      > <span class="fu"
      >otherwise</span
      > <span class="fu"
      >=</span
      > <span class="fu"
      >fmap</span
      > <span class="dt"
      >BSucc</span
      > (toBNat (<span class="fu"
      >pred</span
      > m))<br
       /></code
    ></pre
  ><p
  >We can instead eliminate the <code
    >HasBNat</code
    > class and reuse the <code
    >IsNat</code
    > class, as in the last technique above for defining <code
    >units</code
    > or <code
    >pure</code
    >.</p
  ><pre class="sourceCode haskell"
  ><code
    >toBNat <span class="dv"
      >&#8759;</span
      > &#8704; n<span class="fu"
      >.</span
      > <span class="dt"
      >IsNat</span
      > n &#8658; <span class="dt"
      >Integer</span
      > &#8594; <span class="dt"
      >Maybe</span
      > (<span class="dt"
      >BNat</span
      > n)<br
       />toBNat <span class="fu"
      >=</span
      > loop n <span class="kw"
      >where</span
      ><br
       />  n <span class="fu"
      >=</span
      > nat <span class="dv"
      >&#8759;</span
      > <span class="dt"
      >Nat</span
      > n<br
       />  loop <span class="dv"
      >&#8759;</span
      > <span class="dt"
      >Nat</span
      > n' &#8594; <span class="dt"
      >Integer</span
      > &#8594; <span class="dt"
      >Maybe</span
      > (<span class="dt"
      >BNat</span
      > n')<br
       />  loop <span class="dt"
      >Zero</span
      >      _ <span class="fu"
      >=</span
      > <span class="kw"
      >Nothing</span
      ><br
       />  loop (<span class="dt"
      >Succ</span
      > _)  <span class="dv"
      >0</span
      > <span class="fu"
      >=</span
      > <span class="kw"
      >Just</span
      > <span class="dt"
      >BZero</span
      ><br
       />  loop (<span class="dt"
      >Succ</span
      > n') m <span class="fu"
      >=</span
      > <span class="fu"
      >fmap</span
      > <span class="dt"
      >BSucc</span
      > (loop n' (<span class="fu"
      >pred</span
      > m))<br
       /></code
    ></pre
  ></div
>

<div id="a-monad-instance"
><h3
  >A Monad instance</h3
  ><p
  >First the easy parts: standard definitions in terms of <code
    >pure</code
    > and <code
    >join</code
    >:</p
  ><pre class="sourceCode haskell"
  ><code
    ><span class="kw"
      >instance</span
      > <span class="dt"
      >IsNat</span
      > n &#8658; <span class="kw"
      >Monad</span
      > (<span class="dt"
      >Vec</span
      > n) <span class="kw"
      >where</span
      ><br
       />  <span class="fu"
      >return</span
      >  <span class="fu"
      >=</span
      > pure<br
       />  v <span class="fu"
      >&gt;&gt;=</span
      > f <span class="fu"
      >=</span
      > join (<span class="fu"
      >fmap</span
      > f v)<br
       /></code
    ></pre
  ><p
  >The <code
    >join</code
    > function on <code
    >Vec n</code
    > is just like <code
    >join</code
    > for functions and for streams. (Rightly so, considering the principle of <a href="http://conal.net/blog/tag/type-class-morphism/" title="Posts on type class morphisms"
    >type class morphism</a
    >s.) It uses diagonalization, and one way to think of vector <code
    >join</code
    > is that it extracts the diagonal of a square matrix.</p
  ><pre class="sourceCode haskell"
  ><code
    >join <span class="dv"
      >&#8759;</span
      > <span class="dt"
      >Vec</span
      > n (<span class="dt"
      >Vec</span
      > n a) &#8594; <span class="dt"
      >Vec</span
      > n a<br
       />join <span class="dt"
      >ZVec</span
      >      <span class="fu"
      >=</span
      > <span class="dt"
      >ZVec</span
      ><br
       />join (v <span class="fu"
      >:&lt;</span
      > vs) <span class="fu"
      >=</span
      > headV v <span class="fu"
      >:&lt;</span
      > join (<span class="fu"
      >fmap</span
      > tailV vs)<br
       /></code
    ></pre
  ><p
  >The <code
    >headV</code
    > and <code
    >tailV</code
    > functions are like <code
    >head</code
    > and <code
    >tail</code
    > but understand lengths:</p
  ><pre class="sourceCode haskell"
  ><code
    >headV <span class="dv"
      >&#8759;</span
      > <span class="dt"
      >Vec</span
      > (<span class="dt"
      >S</span
      > n) a &#8594; a<br
       />headV (a <span class="fu"
      >:&lt;</span
      > _) <span class="fu"
      >=</span
      > a<br
       /></code
    ></pre
  ><pre class="sourceCode haskell"
  ><code
    >tailV <span class="dv"
      >&#8759;</span
      > <span class="dt"
      >Vec</span
      > (<span class="dt"
      >S</span
      > n) a &#8594; <span class="dt"
      >Vec</span
      > n a<br
       />tailV (_ <span class="fu"
      >:&lt;</span
      > as) <span class="fu"
      >=</span
      > as<br
       /></code
    ></pre
  ><p
  >Unlike their list counterparts, <code
    >headV</code
    > and <code
    >tailV</code
    > are <em
    >safe</em
    >, in that the precondition of non-emptiness is verified statically.</p
  ></div
>
<p><a href="http://conal.net/blog/?flattrss_redirect&amp;id=293&amp;md5=0fd4950067e60840fabf4f89a33ab982"><img src="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png" srcset="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@2x.png 2xhttp://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@3x.png 3x" alt="Flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/doing-more-with-length-typed-vectors/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=conal&amp;popout=1&amp;url=http%3A%2F%2Fconal.net%2Fblog%2Fposts%2Fdoing-more-with-length-typed-vectors&amp;language=en_GB&amp;category=text&amp;title=Doing+more+with+length-typed+vectors&amp;description=The+post+Fixing+lists+defined+a+%28commonly+used%29+type+of+vectors%2C+whose+lengths+are+determined+statically%2C+by+type.+In+Vec+n+a%2C+the+length+is+n%2C+and+the+elements+have...&amp;tags=applicative+functor%2Cmonad%2Cnumber%2Cvector%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Fixing lists</title>
		<link>http://conal.net/blog/posts/fixing-lists</link>
		<comments>http://conal.net/blog/posts/fixing-lists#comments</comments>
		<pubDate>Sun, 30 Jan 2011 18:14:30 +0000</pubDate>
		<dc:creator><![CDATA[Conal]]></dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[applicative functor]]></category>
		<category><![CDATA[functor]]></category>
		<category><![CDATA[number]]></category>
		<category><![CDATA[trie]]></category>
		<category><![CDATA[vector]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=284</guid>
		<description><![CDATA[In the post Memoizing polymorphic functions via unmemoization, I toyed with the idea of lists as tries. I don&#8217;t think [a] is a trie, simply because [a] is a sum type (being either nil or a cons), while tries are built out of the identity, product, and composition functors. In contrast, Stream is a trie, [&#8230;]]]></description>
				<content:encoded><![CDATA[<!-- teaser -->

<p
>In the post <a href="http://conal.net/blog/posts/memoizing-polymorphic-functions-via-unmemoization/" title="blog post"
  ><em
    >Memoizing polymorphic functions via unmemoization</em
    ></a
  >, I toyed with the idea of lists as tries. I don&#8217;t think <code
  >[a]</code
  > is a trie, simply because <code
  >[a]</code
  > is a <em
  >sum</em
  > type (being either nil or a cons), while tries are built out of the identity, product, and composition functors. In contrast, <code
  >Stream</code
  > <em
  >is</em
  > a trie, being built solely with the identity and product functors. Moreover, <code
  >Stream</code
  > is not just any old trie, it is the trie that corresponds to Peano (unary natural) numbers, i.e., <code
  >Stream a &#8773; N &#8594; a</code
  >, where</p
>

<pre class="sourceCode haskell"
><code
  ><span class="kw"
    >data</span
    > <span class="dt"
    >N</span
    > <span class="fu"
    >=</span
    > <span class="dt"
    >Zero</span
    > <span class="fu"
    >|</span
    > <span class="dt"
    >Succ</span
    > <span class="dt"
    >N</span
    ><br
     /><br
     /><span class="kw"
    >data</span
    > <span class="dt"
    >Stream</span
    > a <span class="fu"
    >=</span
    > <span class="dt"
    >Cons</span
    > a (<span class="dt"
    >Stream</span
    > a)<br
     /></code
  ></pre
>

<p
>If we didn't already know the <code
  >Stream</code
  > type, we would derive it systematically from <code
  >N</code
  >, using standard isomorphisms.</p
>

<p
><code
  >Stream</code
  > is a trie (over unary numbers), thanks to it having no choice points, i.e., no sums in its construction. However, streams are infinite-only, which is not always what we want. In contrast, lists can be finite, but are not a trie in any sense I understand. In this post, I look at how to <em
  >fix</em
  > lists, so they can be finite and yet be a trie, thanks to having no choice points (sums)?</p
>

<p
>You can find the code for this post and the <a href="http://conal.net/blog/posts/type-bounded-numbers/" title="blog post"
  >previous one</a
  > in a <a href="https://github.com/conal/numbers-vectors-trees/" title="github repository"
  >code repository</a
  >.</p
>

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

<ul
><li
  >2011-01-30: Added spoilers warning.</li
  ><li
  >2011-01-30: Pointer to <a href="https://github.com/conal/numbers-vectors-trees/" title="github repository"
    >code repository</a
    >.</li
  ></ul
>

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

<div id="fixing-lists"
><h3
  >Fixing lists</h3
  ><p
  >Is there a type of finite lists without choice points (sums)? Yes. There are lots of them. One for each length. Instead of having a single type of lists, have an infinite family of types of <span class="math"
    ><em
      >n</em
      ></span
    >-element lists, one type for each <span class="math"
    ><em
      >n</em
      ></span
    >.</p
  ><p
  >In other words, to fix the problem with lists (trie-unfriendliness), split up the usual list type into subtypes (so to speak), each of which has a fixed length.</p
  ><p
  >I realize I'm changing the question to a simpler one. I hope you'll forgive me and hang in to see where this ride goes.</p
  ><p
  >As a first try, we might use tuples as our fixed-length lists:</p
  ><pre class="sourceCode haskell"
  ><code
    ><span class="kw"
      >type</span
      > <span class="dt"
      >L0</span
      > a <span class="fu"
      >=</span
      > ()<br
       /><span class="kw"
      >type</span
      > <span class="dt"
      >L1</span
      > a <span class="fu"
      >=</span
      > (a)<br
       /><span class="kw"
      >type</span
      > <span class="dt"
      >L2</span
      > a <span class="fu"
      >=</span
      > (a,a)<br
       /><span class="kw"
      >type</span
      > <span class="dt"
      >L3</span
      > a <span class="fu"
      >=</span
      > (a,a,a)<br
       />&#8943;<br
       /></code
    ></pre
  ><p
  >However, we can only write down finitely many such types, and I don't know how we could write any definitions that are polymorphic over <em
    >length</em
    >.</p
  ><p
  >What can &quot;polymorphic over length&quot; mean in a setting like Haskell, where polymorphism is over <em
    >types</em
    > rather than <em
    >values</em
    >. Can we express numbers (for lengths, etc) as types? Yes, as in the previous post, <a href="http://conal.net/blog/posts/type-bounded-numbers/" title="blog post"
    ><em
      >Type-bounded numbers</em
      ></a
    >, using a common encoding:</p
  ><pre class="sourceCode haskell"
  ><code
    ><span class="kw"
      >data</span
      > <span class="dt"
      >Z</span
      >    <span class="co"
      >-- zero</span
      ><br
       /><span class="kw"
      >data</span
      > <span class="dt"
      >S</span
      > n  <span class="co"
      >-- successor</span
      ><br
       /></code
    ></pre
  ><p
  >Given these type-level numbers, we can define a data type <code
    >Vec n a</code
    >, containing only vectors (fixed lists) of length <code
    >n</code
    > and elements of type <code
    >a</code
    >. Such vectors can be built up as either the zero-length vector, or by adding an element to an vector of length <span class="math"
    ><em
      >n</em
      ></span
    > to get a vector of length <span class="math"
    ><em
      >n</em
      > + 1</span
    >. I don't know how to define this type as a regular algebraic data type, but it's easy as a <em
    >generalized</em
    > algebraic data type (<a href="http://en.wikibooks.org/wiki/Haskell/GADT" title="Haskell Wikibook page"
    >GADT</a
    >):</p
  ><pre class="sourceCode haskell"
  ><code
    ><span class="kw"
      >infixr</span
      > <span class="dv"
      >5</span
      > <span class="fu"
      >:&lt;</span
      ><br
       /><br
       /><span class="kw"
      >data</span
      > <span class="dt"
      >Vec</span
      > <span class="dv"
      >&#8759;</span
      > <span class="fu"
      >*</span
      > &#8594; <span class="fu"
      >*</span
      > &#8594; <span class="fu"
      >*</span
      > <span class="kw"
      >where</span
      ><br
       />  <span class="dt"
      >ZVec</span
      > <span class="dv"
      >&#8759;</span
      >                <span class="dt"
      >Vec</span
      > <span class="dt"
      >Z</span
      >     a<br
       />  (<span class="fu"
      >:&lt;</span
      >) <span class="dv"
      >&#8759;</span
      > a &#8594; <span class="dt"
      >Vec</span
      > n a &#8594; <span class="dt"
      >Vec</span
      > (<span class="dt"
      >S</span
      > n) a<br
       /></code
    ></pre
  ><p
  >For example,</p
  ><pre class="sourceCode haskell"
  ><code
    ><span class="fu"
      >*</span
      ><span class="dt"
      >Vec</span
      ><span class="fu"
      >&gt;</span
      > <span class="fu"
      >:</span
      >ty <span class="ch"
      >'z'</span
      > <span class="fu"
      >:&lt;</span
      > <span class="ch"
      >'o'</span
      > <span class="fu"
      >:&lt;</span
      > <span class="ch"
      >'m'</span
      > <span class="fu"
      >:&lt;</span
      > <span class="ch"
      >'g'</span
      > <span class="fu"
      >:&lt;</span
      > <span class="dt"
      >ZVec</span
      ><br
       /><span class="ch"
      >'z'</span
      > <span class="fu"
      >:&lt;</span
      > <span class="ch"
      >'o'</span
      > <span class="fu"
      >:&lt;</span
      > <span class="ch"
      >'m'</span
      > <span class="fu"
      >:&lt;</span
      > <span class="ch"
      >'g'</span
      > <span class="fu"
      >:&lt;</span
      > <span class="dt"
      >ZVec</span
      > <span class="dv"
      >&#8759;</span
      > <span class="dt"
      >Vec</span
      > (<span class="dt"
      >S</span
      > (<span class="dt"
      >S</span
      > (<span class="dt"
      >S</span
      > (<span class="dt"
      >S</span
      > <span class="dt"
      >Z</span
      >)))) <span class="dt"
      >Char</span
      ><br
       /></code
    ></pre
  ><p
  >As desired, <code
    >Vec</code
    > is length-typed, covers all (finite) lengths, and allows definition of length-polymorphic functions. For instance, it's easy to map functions over vectors:</p
  ><pre class="sourceCode haskell"
  ><code
    ><span class="kw"
      >instance</span
      > <span class="kw"
      >Functor</span
      > (<span class="dt"
      >Vec</span
      > n) <span class="kw"
      >where</span
      ><br
       />  <span class="fu"
      >fmap</span
      > _ <span class="dt"
      >ZVec</span
      >     <span class="fu"
      >=</span
      > <span class="dt"
      >ZVec</span
      ><br
       />  <span class="fu"
      >fmap</span
      > f (a <span class="fu"
      >:&lt;</span
      > u) <span class="fu"
      >=</span
      > f a <span class="fu"
      >:&lt;</span
      > <span class="fu"
      >fmap</span
      > f u<br
       /></code
    ></pre
  ><p
  >The type of <code
    >fmap</code
    > here is <code
    >(a &#8594; b) &#8594; Vec n a &#8594; Vec n b</code
    >.</p
  ><p
  >Folding over vectors is also straightforward:</p
  ><pre class="sourceCode haskell"
  ><code
    ><span class="kw"
      >instance</span
      > <span class="dt"
      >Foldable</span
      > (<span class="dt"
      >Vec</span
      > n) <span class="kw"
      >where</span
      ><br
       />  <span class="fu"
      >foldr</span
      > _ b <span class="dt"
      >ZVec</span
      >      <span class="fu"
      >=</span
      > b<br
       />  <span class="fu"
      >foldr</span
      > h b (a <span class="fu"
      >:&lt;</span
      > as) <span class="fu"
      >=</span
      > a <span class="ot"
      >`h`</span
      > <span class="fu"
      >foldr</span
      > h b as<br
       /></code
    ></pre
  ><p
  >Is <code
    >Vec n</code
    > an applicative functor as well?</p
  ><pre class="sourceCode haskell"
  ><code
    ><span class="kw"
      >instance</span
      > <span class="dt"
      >Applicative</span
      > (<span class="dt"
      >Vec</span
      > n) <span class="kw"
      >where</span
      ><br
       />  &#8943;<br
       /></code
    ></pre
  ><p
  >We would need</p
  ><pre class="sourceCode haskell"
  ><code
    >pure <span class="dv"
      >&#8759;</span
      > a &#8594; <span class="dt"
      >Vec</span
      > n a<br
       />(&#8859;)  <span class="dv"
      >&#8759;</span
      > <span class="dt"
      >Vec</span
      > n (a &#8594; b) &#8594; <span class="dt"
      >Vec</span
      > n a &#8594; <span class="dt"
      >Vec</span
      > n b<br
       /></code
    ></pre
  ><p
  >The <code
    >(&#8859;)</code
    > method can be defined similarly to <code
    >fmap</code
    >:</p
  ><pre class="sourceCode haskell"
  ><code
    >  <span class="dt"
      >ZVec</span
      >      &#8859; <span class="dt"
      >ZVec</span
      >      <span class="fu"
      >=</span
      > <span class="dt"
      >ZVec</span
      ><br
       />  (f <span class="fu"
      >:&lt;</span
      > fs) &#8859; (x <span class="fu"
      >:&lt;</span
      > xs) <span class="fu"
      >=</span
      > f x <span class="fu"
      >:&lt;</span
      > (fs &#8859; xs)<br
       /></code
    ></pre
  ><p
  >Unlike <code
    >fmap</code
    > and <code
    >(&#8859;)</code
    >, <code
    >pure</code
    > doesn't have a vector structure to crawl over. It must create just the right structure anyway. You might enjoy thinking about how to solve this puzzle, which I'll tackle in my next post. (Warning: spoilers in the comments below.)</p
  ></div
>
<p><a href="http://conal.net/blog/?flattrss_redirect&amp;id=284&amp;md5=8a876a1ca74b5e365684d743df52c81c"><img src="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png" srcset="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@2x.png 2xhttp://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@3x.png 3x" alt="Flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/fixing-lists/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=conal&amp;popout=1&amp;url=http%3A%2F%2Fconal.net%2Fblog%2Fposts%2Ffixing-lists&amp;language=en_GB&amp;category=text&amp;title=Fixing+lists&amp;description=In+the+post+Memoizing+polymorphic+functions+via+unmemoization%2C+I+toyed+with+the+idea+of+lists+as+tries.+I+don%26%238217%3Bt+think+%5Ba%5D+is+a+trie%2C+simply+because+%5Ba%5D+is+a+sum...&amp;tags=applicative+functor%2Cfunctor%2Cnumber%2Ctrie%2Cvector%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Paper: Beautiful differentiation</title>
		<link>http://conal.net/blog/posts/paper-beautiful-differentiation</link>
		<comments>http://conal.net/blog/posts/paper-beautiful-differentiation#comments</comments>
		<pubDate>Tue, 24 Feb 2009 08:05:10 +0000</pubDate>
		<dc:creator><![CDATA[Conal]]></dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[applicative functor]]></category>
		<category><![CDATA[beautiful code]]></category>
		<category><![CDATA[calculus on manifolds]]></category>
		<category><![CDATA[derivative]]></category>
		<category><![CDATA[functor]]></category>
		<category><![CDATA[linear map]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[paper]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=85</guid>
		<description><![CDATA[I have another paper draft for submission to ICFP 2009. This one is called Beautiful differentiation, The paper is a culmination of the several posts I&#8217;ve written on derivatives and automatic differentiation (AD). I&#8217;m happy with how the derivation keeps getting simpler. Now I&#8217;ve boiled extremely general higher-order AD down to a Functor and Applicative [&#8230;]]]></description>
				<content:encoded><![CDATA[<!-- 

Title: Paper: Beautiful differentiation

Tags: derivative, functor, applicative functor, beautiful code, calculus on manifolds, linear map, math, paper

URL: http://conal.net/blog/posts/paper-beautiful-differentiation/

-->

<!-- references -->

<!-- teaser -->

<p>I have another paper draft for submission to <a href="http://www.cs.nott.ac.uk/~gmh/icfp09.html" title="conference page">ICFP 2009</a>.
This one is called <em><a href="http://conal.net/papers/beautiful-differentiation" title="paper">Beautiful differentiation</a></em>, 
The paper is a culmination of the <a href="http://conal.net/blog/tag/derivative/">several posts</a> I&#8217;ve written on derivatives and automatic differentiation (AD).
I&#8217;m happy with how the derivation keeps getting simpler.
Now I&#8217;ve boiled extremely general higher-order AD down to a <code>Functor</code> and <code>Applicative</code> morphism.</p>

<p>I&#8217;d love to get some readings and feedback.
I&#8217;m a bit over the page the limit, so I&#8217;ll have to do some trimming before submitting.</p>

<p>The abstract:</p>

<blockquote>
  <p>Automatic differentiation (AD) is a precise, efficient, and convenient
  method for computing derivatives of functions. Its implementation can be
  quite simple even when extended to compute all of the higher-order
  derivatives as well. The higher-dimensional case has also been tackled,
  though with extra complexity. This paper develops an implementation of
  higher-dimensional, higher-order differentiation in the extremely
  general and elegant setting of <em>calculus on manifolds</em> and derives that
  implementation from a simple and precise specification.</p>
  
  <p>In order to motivate and discover the implementation, the paper poses
  the question &#8220;What does AD mean, independently of implementation?&#8221; An
  answer arises in the form of <em>naturality</em> of sampling a function and its
  derivative. Automatic differentiation flows out of this naturality
  condition, together with the chain rule. Graduating from first-order to
  higher-order AD corresponds to sampling all derivatives instead of just
  one. Next, the notion of a derivative is generalized via the notions of
  vector space and linear maps. The specification of AD adapts to this
  elegant and very general setting, which even <em>simplifies</em> the
  development.</p>
</blockquote>

<p>You can <a href="http://conal.net/papers/beautiful-differentiation" title="paper">get the paper and see current errata here</a>.</p>

<p>The submission deadline is March 2, so comments before then are most helpful to me.</p>

<p>Enjoy, and thanks!</p>

<!--
**Edits**:

* 2009-02-09: just fiddling around
-->
<p><a href="http://conal.net/blog/?flattrss_redirect&amp;id=85&amp;md5=2f6565c8f001a5925c3e6dbd29158c37"><img src="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png" srcset="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@2x.png 2xhttp://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@3x.png 3x" alt="Flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/paper-beautiful-differentiation/feed</wfw:commentRss>
		<slash:comments>22</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=conal&amp;popout=1&amp;url=http%3A%2F%2Fconal.net%2Fblog%2Fposts%2Fpaper-beautiful-differentiation&amp;language=en_GB&amp;category=text&amp;title=Paper%3A+Beautiful+differentiation&amp;description=I+have+another+paper+draft+for+submission+to+ICFP+2009.+This+one+is+called+Beautiful+differentiation%2C+The+paper+is+a+culmination+of+the+several+posts+I%26%238217%3Bve+written+on+derivatives+and...&amp;tags=applicative+functor%2Cbeautiful+code%2Ccalculus+on+manifolds%2Cderivative%2Cfunctor%2Clinear+map%2Cmath%2Cpaper%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Denotational design with type class morphisms</title>
		<link>http://conal.net/blog/posts/denotational-design-with-type-class-morphisms</link>
		<comments>http://conal.net/blog/posts/denotational-design-with-type-class-morphisms#comments</comments>
		<pubDate>Thu, 19 Feb 2009 02:34:08 +0000</pubDate>
		<dc:creator><![CDATA[Conal]]></dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[applicative functor]]></category>
		<category><![CDATA[arrow]]></category>
		<category><![CDATA[associated type]]></category>
		<category><![CDATA[functor]]></category>
		<category><![CDATA[monad]]></category>
		<category><![CDATA[monoid]]></category>
		<category><![CDATA[paper]]></category>
		<category><![CDATA[semantics]]></category>
		<category><![CDATA[trie]]></category>
		<category><![CDATA[type class morphism]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=84</guid>
		<description><![CDATA[I&#8217;ve just finished a draft of a paper called Denotational design with type class morphisms, for submission to ICFP 2009. The paper is on a theme I&#8217;ve explored in several posts, which is semantics-based design, guided by type class morphisms. I&#8217;d love to get some readings and feedback. Pointers to related work would be particularly [&#8230;]]]></description>
				<content:encoded><![CDATA[<!-- 

Title: Denotational design with type class morphisms

Tags: paper, semantics, type class morphism, monoid, functor, applicative functor, monad, arrow, associated type, trie

URL: http://conal.net/blog/posts/denotational-design-with-type-class-morphisms/

-->

<!-- references -->

<!-- teaser -->

<p>I&#8217;ve just finished a draft of a paper called <em><a href="http://conal.net/papers/type-class-morphisms" title="paper">Denotational design with type class morphisms</a></em>, for submission to <a href="http://www.cs.nott.ac.uk/~gmh/icfp09.html" title="conference page">ICFP 2009</a>.
The paper is on a theme I&#8217;ve explored in <a href="http://conal.net/blog/tag/type-class-morphism/">several posts</a>, which is semantics-based design, guided by type class morphisms.</p>

<p>I&#8217;d love to get some readings and feedback.
Pointers to related work would be particularly appreciated, as well as what&#8217;s unclear and what could be cut.
It&#8217;s an entire page over the limit, so I&#8217;ll have to do some trimming before submitting.</p>

<p>The abstract:</p>

<blockquote>
  <p>Type classes provide a mechanism for varied implementations of standard
  interfaces. Many of these interfaces are founded in mathematical
  tradition and so have regularity not only of <em>types</em> but also of
  <em>properties</em> (laws) that must hold. Types and properties give strong
  guidance to the library implementor, while leaving freedom as well. Some
  of the remaining freedom is in <em>how</em> the implementation works, and some
  is in <em>what</em> it accomplishes.</p>
  
  <p>To give additional guidance to the <em>what</em>, without impinging on the
  <em>how</em>, this paper proposes a principle of <em>type class morphisms</em> (TCMs),
  which further refines the compositional style of denotational
  semantics. The TCM idea is simply that <em>the instance&#8217;s meaning is the
  meaning&#8217;s instance</em>. This principle determines the meaning of each type
  class instance, and hence defines correctness of implementation. In some
  cases, it also provides a systematic guide to implementation, and in
  some cases, valuable design feedback.</p>
  
  <p>The paper is illustrated with several examples of type, meanings, and
  morphisms.</p>
</blockquote>

<p>You can <a href="http://conal.net/papers/type-class-morphisms" title="paper">get the paper and see current errata here</a>.</p>

<p>The submission deadline is March 2, so comments before then are most helpful to me.</p>

<p>Enjoy, and thanks!</p>

<!--
**Edits**:

* 2009-02-09: just fiddling around
-->
<p><a href="http://conal.net/blog/?flattrss_redirect&amp;id=84&amp;md5=8ce3b83d01ccfad97ade1469b72d2a04"><img src="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png" srcset="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@2x.png 2xhttp://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@3x.png 3x" alt="Flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/denotational-design-with-type-class-morphisms/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=conal&amp;popout=1&amp;url=http%3A%2F%2Fconal.net%2Fblog%2Fposts%2Fdenotational-design-with-type-class-morphisms&amp;language=en_GB&amp;category=text&amp;title=Denotational+design+with+type+class+morphisms&amp;description=I%26%238217%3Bve+just+finished+a+draft+of+a+paper+called+Denotational+design+with+type+class+morphisms%2C+for+submission+to+ICFP+2009.+The+paper+is+on+a+theme+I%26%238217%3Bve+explored+in+several...&amp;tags=applicative+functor%2Carrow%2Cassociated+type%2Cfunctor%2Cmonad%2Cmonoid%2Cpaper%2Csemantics%2Ctrie%2Ctype+class+morphism%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Sequences, segments, and signals</title>
		<link>http://conal.net/blog/posts/sequences-segments-and-signals</link>
		<comments>http://conal.net/blog/posts/sequences-segments-and-signals#comments</comments>
		<pubDate>Fri, 05 Dec 2008 08:14:33 +0000</pubDate>
		<dc:creator><![CDATA[Conal]]></dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[applicative functor]]></category>
		<category><![CDATA[comonad]]></category>
		<category><![CDATA[FRP]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[functional reactive programming]]></category>
		<category><![CDATA[functor]]></category>
		<category><![CDATA[monoid]]></category>
		<category><![CDATA[segment]]></category>
		<category><![CDATA[sequence]]></category>
		<category><![CDATA[type class morphism]]></category>
		<category><![CDATA[zipper]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=67</guid>
		<description><![CDATA[The post Sequences, streams, and segments offered an answer to the the question of what&#8217;s missing in the following box: infinitefinite discreteStream Sequence continuousFunction ??? I presented a simple type of function segments, whose representation contains a length (duration) and a function. This type implements most of the usual classes: Monoid, Functor, Zip, and Applicative, [&#8230;]]]></description>
				<content:encoded><![CDATA[<!-- 

Title: Sequences, segments, and signals

Tags: function, sequence, monoid, functor, applicative functor, comonad, FRP, functional reactive programming, segment, type class morphism, zipper

URL: http://conal.net/blog/posts/sequences-segments-and-signals/

-->

<!-- references -->

<!-- teaser -->

<p>The post <em><a href="http://conal.net/blog/posts/sequences-streams-and-segments/" title="blog post">Sequences, streams, and segments</a></em> offered an answer to the the question of what&#8217;s missing in the following box:</p>

<div align=center style="margin-bottom:10px">
  <table border="2">
    <tr><td></td><td style="text-align:center;padding:5px"><strong>infinite</strong><td style="text-align:center;padding:5px"><strong>finite</strong></third></tr>
    <tr><td style="text-align:center;padding:5px"><strong>discrete</strong><td style="text-align:center;padding:7px">Stream</td> <td style="text-align:center;padding:7px">Sequence</td></tr>
    <tr><td style="text-align:center;padding:5px"><strong>continuous</strong><td style="text-align:center;padding:7px">Function</td> <td style="text-align:center;padding:7px"><em>???</em></td></tr>
  </table>
</div>

<p>I presented a simple type of <em>function segments</em>, whose representation contains a length (duration) and a function.
This type implements most of the usual classes: <code>Monoid</code>, <code>Functor</code>, <code>Zip</code>, and <code>Applicative</code>, as well <code>Comonad</code>, but not <code>Monad</code>.
It also implements a new type class, <code>Segment</code>, which generalizes the list functions <code>length</code>, <code>take</code>, and <code>drop</code>.</p>

<p>The function type is simple and useful in itself.
I believe it can also serve as a semantic foundation for functional reactive programming (FRP), as I&#8217;ll explain in another post.
However, the type has a serious performance problem that makes it impractical for some purposes, including as implementation of FRP.</p>

<p>Fortunately, we can solve the performance problem by adding a simple layer on top of function segments, to get what I&#8217;ll call &#8220;signals&#8221;.
With this new layer, we have an efficient replacement for function segments that implements exactly the same interface with exactly the same semantics.
Pleasantly, the class instances are defined fairly simply in terms of the corresponding instances on function segments.</p>

<p>You can download the <a href="http://conal.net/blog/code/Signal.hs">code for this post</a>.</p>

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

<ul>
<li>2008-12-06: <code>dup [] = []</code> near the end (was <code>[mempty]</code>).</li>
<li>2008-12-09: Fixed <code>take</code> and <code>drop</code> default definitions (thanks to sclv) and added point-free variant.</li>
<li>2008-12-18: Fixed <code>appl</code>, thanks to sclv.</li>
<li>2011-08-18: Eliminated accidental emoticon in the definition of <code>dup</code>, thanks to anonymous.</li>
</ul>

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

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

<h3>The problem with function segments</h3>

<p>The type of function segments is defined as follows:</p>

<pre><code>data t :-&gt;# a = FS t (t -&gt; a)
</code></pre>

<p>The domain of the function segment is from zero up to but not including the given length.</p>

<p>An efficiency problem becomes apparent when we look at the <code>Monoid</code> instance:</p>

<pre><code>instance (Ord t, Num t) =&gt; Monoid (t :-&gt;# a) where
    mempty = FS 0 (error "sampling empty 't :-&gt;# a'")
    FS c f `mappend` FS d g =
      FS (c + d) ( t -&gt; if t &lt;= c then f t else g (t - c))
</code></pre>

<p>Concatenation (<code>mappend</code>) creates a new segment that chooses, for every domain value <code>t</code>, whether to use one function or another.
If the second, then <code>t</code> must be shifted backward, since the function is being shifted forward.</p>

<p>This implementation would be fine if we use <code>mappend</code> just on simple segments.
Once we get started, however, we&#8217;ll want to concatenate lots &amp; lots of segments.
In FRP, time-varying values go through many phases (segments) as time progresses.
Each quantity is described by a single &#8220;behavior&#8221; (sometimes called a &#8220;signal&#8221;) with many, and often infinitely many, phases.
Imagine an infinite tree of concatenations, which is typical for FRP behaviors.
At every moment, one phase is active.
Every sampling must recursively discover the active phase and the accumulated domain translation (from successive subtractions) to apply when sampling that phase.
Quite commonly, concatenation trees get progressively deeper on the right (larger <code>t</code> values).
In that case, sampling will get slower and slower with time.</p>

<p>I like to refer to these progressive slow-downs as &#8220;time leaks&#8221;.
There is also a serious space leak, since all of the durations and functions that go into a composed segment will be retained.</p>

<h3>Sequences of segments</h3>

<p>The problem above can be solved with a simple representation change.
Instead of combining functions into functions, just keep a list of simple function segments.</p>

<pre><code>-- | Signal indexed by t with values of type a.
newtype t :-&gt; a = S { unS :: [t :-&gt;# a] }
</code></pre>

<p>I&#8217;ll restrict in function segments to be <em>non-empty</em>, to keep the rest of the implementation simple and efficient.</p>

<p>This new representation allows for efficient <em>monotonic</em> sampling of signals.
As old segments are passed up, they can be dropped.</p>

<h4>What does it mean?</h4>

<p>There&#8217;s one central question for me in defining any data type: <em>What does it mean?</em></p>

<p>The meaning I&#8217;ll take for signals is function segments.
This interpretation is made precise in a function that maps from the type to the model (meaning).
In this case, simply concatenate all of the function segments:</p>

<pre><code>meaning :: (Ord t, Num t) =&gt; (t :-&gt; a) -&gt; (t :-&gt;# a)
meaning = mconcat . unS
</code></pre>

<p>Specifying the meaning of a type gives users a working model, and it defines correctness of implementation.
It also tells me what class instances to implement and tells users what instances to expect.
If a type&#8217;s meaning implements a class then I want the type to as well.
Moreover, the type&#8217;s intances have to agree with the model&#8217;s instances.
I&#8217;ve described this latter principle 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> and <a href="http://conal.net/blog/tag/type-class-morphism/" title="Posts on type class morphisms">some other posts</a>.</p>

<h4>Higher-order wrappers</h4>

<p>To keep the code below short and clear, I&#8217;ll use some functions for adding and removing the newtype wrappers.
These higher-order function apply functions inside of <code>(:-&gt;)</code> representations:</p>

<pre><code>inS  :: ([s :-&gt;# a] -&gt; [t :-&gt;# b])
     -&gt; ((s :-&gt;  a) -&gt; (t :-&gt;  b))

inS2 :: ([s :-&gt;# a] -&gt; [t :-&gt;# b] -&gt; [u :-&gt;# c])
     -&gt; ((s :-&gt;  a) -&gt; (t :-&gt;  b) -&gt; (u :-&gt;  c))
</code></pre>

<p>Using the trick described in <em><a href="http://conal.net/blog/posts/prettier-functions-for-wrapping-and-wrapping/" title="blog post">Prettier functions for wrapping and wrapping</a></em>, the definitions are simpler than the types:</p>

<pre><code>inS  = result   S . argument unS
inS2 = result inS . argument unS
</code></pre>

<h4><code>Functor</code></h4>

<p>The <code>Functor</code> instance applies a given function inside the function segments inside the lists:</p>

<pre><code>instance Functor ((:-&gt;) t) where
    fmap h (S ss) = S (fmap (fmap h) ss)
</code></pre>

<p>Or, in the style of <em><a href="http://conal.net/blog/posts/semantic-editor-combinators/" title="blog post">Semantic editor combinators</a></em>,</p>

<pre><code>instance Functor ((:-&gt;) t) where
    fmap = inS . fmap . fmap
</code></pre>

<p>Why this definition?
Because it is correct with respect to the semantic model, i.e., the meaning of <code>fmap</code> is <code>fmap</code> of the meaning, i.e.,</p>

<pre><code> meaning . fmap h == fmap h . meaning
</code></pre>

<p>which is to say that the following diagram commutes:</p>

<div align=center><img src="http://conal.net/blog/pictures/signal-meaning-fmap-morphism.png"/></div>

<p>Proof:</p>

<pre><code>meaning . fmap h 

  == {- fmap definition -}

meaning . S . fmap (fmap h) . unS

  == {- meaning definition -}

mconcat . unS . S . fmap (fmap h) . unS

  == {- unS and S are inverses  -}

mconcat . fmap (fmap h) . unS

  == {- fmap h distributes over mappend -}

fmap h . mconcat . unS

  == {- meaning definition -}

fmap . meaning
</code></pre>

<h4><code>Applicative</code> and <code>Zip</code></h4>

<p>Again, the <code>meaning</code> functions tells us what the <code>Applicative</code> instance has to mean.
We only get to choose how to implement that meaning correctly.
The <code>Applicative</code> <a href="http://conal.net/blog/tag/type-class-morphism/" title="Posts on type class morphisms">morphism properties</a>:</p>

<pre><code>meaning (pure a)    == pure a
meaning (bf &lt;*&gt; bx) == meaning bf &lt;*&gt; meaning bx
</code></pre>

<p>Our <code>Applicative</code> instance has a definition similar in simplicity and style to the <code>Functor</code> instance, assuming a worker function <code>appl</code> for <code>(&lt;*&gt;)</code>:</p>

<pre><code>instance (Ord t, Num t, Bounded t) =&gt; Applicative ((:-&gt;) t) where
    pure  = S . pure . pure
    (&lt;*&gt;) = inS2 appl

appl :: (Ord t, Num t, Bounded t) =&gt;
        [t :-&gt;# (a -&gt; b)] -&gt; [t :-&gt;# a] -&gt; [t :-&gt;# b]
</code></pre>

<p>This worker function is somewhat intricate.
At least my implementation of it is, and perhaps there&#8217;s a simpler one.</p>

<p>Again, the <code>meaning</code> functions tells us what the <code>Applicative</code> instance has to mean.
We only get to choose how to implement that meaning correctly.</p>

<p>First, if either segment list runs out, the combination runs out (because the same is true for the <em>meaning</em> of signals).</p>

<pre><code>[] `appl` _  = []

_  `appl` [] = []
</code></pre>

<p>If neither segment list is empty, open up the first segment.
Split the longer segment into a prefix that matches the shorter segment, and combine the two segments with <code>(&lt;*&gt;)</code> (on function segments).
Toss the left-over piece back in its list, and continue.</p>

<pre><code>(fs:fss') `appl` (xs:xss')
   | fd == xd  = (fs  &lt;*&gt; xs ) : (fss' `appl`       xss' )
   | fd &lt;  xd  = (fs  &lt;*&gt; xs') : (fss' `appl` (xs'':xss'))
   | otherwise = (fs' &lt;*&gt; xs ) : ((fs'':fss') `appl` xss')
 where
   fd         = length fs
   xd         = length xs
   (fs',fs'') = splitAt xd fs
   (xs',xs'') = splitAt fd xs
</code></pre>

<p>A <code>Zip</code> instance is easy as always with applicative functors:</p>

<pre><code>instance (Ord t, Num t, Bounded t) =&gt; Zip ((:-&gt;) t) where zip = liftA2 (,)
</code></pre>

<h4><code>Monoid</code></h4>

<p>The <code>Monoid</code> instance:</p>

<pre><code>instance Monoid (t :-&gt; a) where
  mempty = S []
  S xss `mappend` S yss = S (xss ++ yss)
</code></pre>

<p>Correctness follows from properties of <code>mconcat</code> (as used in the <code>meaning</code> function).</p>

<p>We&#8217;re really just using the <code>Monoid</code> instance for the underlying representation, i.e.,</p>

<pre><code>instance Monoid (t :-&gt; a) where
    mempty  = S mempty
    mappend = inS2 mappend
</code></pre>

<h4><code>Segment</code></h4>

<p>The <code>Segment</code> class has <code>length</code>, <code>take</code> and <code>drop</code>.
It&#8217;s handy to include also <code>null</code> and <code>splitAt</code>, both modeled after their counterparts on lists.</p>

<p>The new &amp; improved <code>Segment</code> class:</p>

<pre><code>class Segment seg dur | seg -&gt; dur where
    null    :: seg -&gt; Bool
    length  :: seg -&gt; dur
    take    :: dur -&gt; seg -&gt; seg
    drop    :: dur -&gt; seg -&gt; seg
    splitAt :: dur -&gt; seg -&gt; (seg,seg)
    -- Defaults:
    splitAt d s = (take d s, drop d s)
    take    d s = fst (splitAt d s)
    drop    d s = snd (splitAt d s)
</code></pre>

<p>If we wanted to require <code>dur</code> to be numeric, we could add a default for <code>null</code> as well.
This default could be quite expensive in some cases.
(In the style of <em><a href="http://conal.net/blog/posts/semantic-editor-combinators/" title="blog post">Semantic editor combinators</a></em>, <code>take = (result.result) fst splitAt</code>, and similarly for <code>drop</code>.)</p>

<p>The <code>null</code> and <code>length</code> definitions are simple, following from to properties of <code>mconcat</code>.</p>

<pre><code>instance (Ord t, Num t) =&gt; Segment (t :-&gt; a) t where
  null   (S xss) = null xss
  length (S xss) = sum (length &lt;$&gt; xss)
  ...
</code></pre>

<p>The null case says that the signal is empty exactly when there are no function segments.
This simple definition relies on our restriction to non-empty function segments.
If we drop that restriction, we&#8217;d have to check that every segment is empty:</p>

<pre><code>  -- Alternative definition
  null (S xss) = all null xss
</code></pre>

<p>The <code>length</code> is just the sum of the lengths.</p>

<p>The tricky piece is <code>splitAt</code> (used to define both <code>take</code> and drop), which must assemble segments to satisfy the requested prefix length.
The last segment used might have to get split into two, with one part going into the prefix and one to the suffix.</p>

<pre><code>  splitAt _ (S [])  = (mempty,mempty)
  splitAt d b | d &lt;= 0 = (mempty, b)
  splitAt d (S (xs:xss')) =
    case (d `compare` xd) of
      EQ -&gt; (S [xs], S xss')
      LT -&gt; let (xs',xs'') = splitAt d xs in
              (S [xs'], S (xs'':xss'))
      GT -&gt; let (S uss, suffix) = splitAt (d-xd) (S xss') in
              (S (xs:uss), suffix)
   where
     xd = length xs
</code></pre>

<h4><code>Copointed</code> and <code>Comonad</code></h4>

<p>To extract an element from a signal, extract an element from its first function segment.
Awkwardly, extraction will fail (produce &perp;/error) when the signal is empty.</p>

<pre><code>instance Num t =&gt; Copointed ((:-&gt;) t) where
    extract (S [])     = error "extract: empty S"
    extract (S (xs:_)) = extract xs
</code></pre>

<p>I&#8217;ve exploited our restriction to non-empty function segments.
Otherwise, <code>extract</code> would have to skip past the empty ones:</p>

<pre><code>-- Alternative definition
instance Num t =&gt; Copointed ((:-&gt;) t) where
    extract (S []) = error "extract: empty S"
    extract (S (xs:xss'))
      | null xs     = extract (S xss')
      | otherwise   = extract xs
</code></pre>

<p>The error/&perp; in this definition is dicey, as is the one for function segments.
If we allow the same abuse in order to define a list <code>Copointed</code>, we can get an alternative to the first definition that is prettier but gives a less helpful error message:</p>

<pre><code>instance Num t =&gt; Copointed ((:-&gt;) t) where
    extract = extract . extract . unS
</code></pre>

<p>See the closing remarks for more about this diciness.</p>

<p>Finally, we have <code>Comonad</code>, with its <code>duplicate</code> method.</p>

<pre><code>duplicate :: (t :-&gt; a) -&gt; (t :-&gt; (t :-&gt; a))
</code></pre>

<p>I get confused with wrapping and unwrapping, so let&#8217;s separate the definition into a packaging part and a content part.</p>

<pre><code>instance (Ord t, Num t) =&gt; Comonad ((:-&gt;) t) where
    duplicate = fmap S . inS dup
</code></pre>

<p>with content part:</p>

<pre><code>dup :: (Ord t, Num t) =&gt; [t :-&gt;# a] -&gt; [t :-&gt;# [t :-&gt;# a]]
</code></pre>

<p><!-- 
I think of `duplicate` as forming all of the *tails* of a segment (as it does with lists via the standard `tails` function). 
-->
The helper function, <code>dup</code>, takes each function segment and prepends each of its tails onto the remaining list of segments.</p>

<p>If the segment list is empty, then it has only one tail, also empty.</p>

<pre><code>dup []        = []

dup (xs:xss') = ((: xss') &lt;$&gt; duplicate xs) : dup xss'
</code></pre>

<h3>Closing remarks</h3>

<ul>
<li><p>The definitions above use the function segment type only through its type class interfaces, and so can all of them can be generalized.
Several definitions rely on the <code>Segment</code> instance, but otherwise, each method for the composite type relies on the corresponding method for the underlying segment type.
(For instance, <code>fmap</code> uses <code>fmap</code>, <code>(&lt;*&gt;)</code> uses <code>(&lt;*&gt;)</code>, <code>Segment</code> uses <code>Segment</code>, etc.)
This generality lets us replace function segments with more efficient representations, e.g., doing constant propagation, as in <a href="http://haskell.org/haskellwiki/Reactive" title="Wiki page for the Reactive library">Reactive</a>.
We can also generalize from lists in the definitions above.</p></li>
<li><p>Even without concatenation, function segments can become expensive when <code>drop</code> is repeatedly applied, because function shifts accumulate (e.g., <code>f . (+ 0.01) . (+ 0.01) ....</code>).
A more <code>drop</code>-friendly representation for function segments would be a function and an offset.
Successive drops would add offsets, and <code>extract</code> would always apply the function to its offset.
This representation is similar to the <code>FunArg</code> comonad, mentioned in <a href="http://cs.ioc.ee/~tarmo/papers/essence.pdf" title="Paper by Tarmo Uustalu and Varmo Vene">The Essence of Dataflow Programming</a> (Section 5.2).</p></li>
<li><p>The list-of-segments representation enables efficient monotonic sampling, simply by dropping after each sample.
A variation is to use a list zipper instead of a list.
Then non-monotonic sampling will be efficient as long as successive samplings are for nearby domain values.
Switching to multi-directional representation would lose the space efficiency of unidirectional representations.
The latter work well with lazy evaluation, often running in constant space, because old values are recycled while new values are getting evaluated.</p></li>
<li><p>Still another variation is to use a binary tree instead of a list, to avoid the list append in the <code>Monoid</code> instance for <code>(t :-&gt; a)</code>.
A tree zipper would allow non-monotonic sampling.
Sufficient care in data structure design could perhaps yield efficient random access.</p></li>
<li><p>There&#8217;s a tension between the <code>Copointed</code> and <code>Monoid</code> interfaces.
<code>Copointed</code> has <code>extract</code>, and <code>Monoid</code> has <code>mempty</code>, so what is the value of <code>extract mempty</code>?
Given that <code>Copointed</code> is parameterized over <em>arbitrary</em> types, the only possible answer seems to be &perp; (as in the use of <code>error</code> above).
I don&#8217;t know if the comonad laws can all hold for possibly-empty function segments or for possibly-empty signals.
I&#8217;m grateful to <a href="http://comonad.com/reader/">Edward Kmett</a> for helping me understand this conflict.
He suggested using two coordinated types, one possibly-empty (a monoid) and the other non-empty (a comonad).
I&#8217;m curious to see whether that idea works out.</p></li>
</ul>
<p><a href="http://conal.net/blog/?flattrss_redirect&amp;id=67&amp;md5=e15e8c9dfd9e8f9c0aadd8dc805515dc"><img src="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png" srcset="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@2x.png 2xhttp://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@3x.png 3x" alt="Flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/sequences-segments-and-signals/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=conal&amp;popout=1&amp;url=http%3A%2F%2Fconal.net%2Fblog%2Fposts%2Fsequences-segments-and-signals&amp;language=en_GB&amp;category=text&amp;title=Sequences%2C+segments%2C+and+signals&amp;description=The+post+Sequences%2C+streams%2C+and+segments+offered+an+answer+to+the+the+question+of+what%26%238217%3Bs+missing+in+the+following+box%3A+infinitefinite+discreteStream+Sequence+continuousFunction+%3F%3F%3F+I+presented+a+simple+type...&amp;tags=applicative+functor%2Ccomonad%2CFRP%2Cfunction%2Cfunctional+reactive+programming%2Cfunctor%2Cmonoid%2Csegment%2Csequence%2Ctype+class+morphism%2Czipper%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Sequences, streams, and segments</title>
		<link>http://conal.net/blog/posts/sequences-streams-and-segments</link>
		<comments>http://conal.net/blog/posts/sequences-streams-and-segments#comments</comments>
		<pubDate>Mon, 01 Dec 2008 07:29:27 +0000</pubDate>
		<dc:creator><![CDATA[Conal]]></dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[applicative functor]]></category>
		<category><![CDATA[comonad]]></category>
		<category><![CDATA[FRP]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[functional reactive programming]]></category>
		<category><![CDATA[functor]]></category>
		<category><![CDATA[monoid]]></category>
		<category><![CDATA[sequence]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=65</guid>
		<description><![CDATA[What kind of thing is a movie? Or a song? Or a trajectory from point A to point B? If you&#8217;re a computer programmer/programmee, you might say that such things are sequences of values (frames, audio samples, or spatial locations). I&#8217;d suggest that these discrete sequences are representations of something more essential, namely a flow [&#8230;]]]></description>
				<content:encoded><![CDATA[<!-- 

Title: Sequences, streams, and segments

Tags: function, sequence, monoid, functor, applicative functor, comonad, FRP, functional reactive programming, segment

URL: http://conal.net/blog/posts/sequences-functions-and-segments/

-->

<!-- references -->

<!-- teaser -->

<p>What kind of thing is a movie?
Or a song?
Or a trajectory from point A to point B?
If you&#8217;re a computer programmer/programmee, you might say that such things are sequences of values (frames, audio samples, or spatial locations).
I&#8217;d suggest that these discrete sequences are representations of something more essential, namely a <em>flow</em> of continuously time-varying values.
Continuous models, whether in time or space, are often more compact, precise, adaptive, and composable than their discrete counterparts.</p>

<p>Functional programming offers great support for sequences of variable length.
<em>Lazy</em> functional programming adds <em>infinite</em> sequences, often called <em>streams</em>, which allows for more elegant and modular programming.</p>

<p>Functional programming also has functions as first class values, and when the function&#8217;s domain is (conceptually) continuous, we get a continuous counterpart to infinite streams.</p>

<p>Streams, sequences, and functions are three corners of a square.
Streams are discrete and infinite, sequences are discrete and finite, and functions-on-reals are continuous and infinite.
The missing corner is continuous and finite, and that corner is the topic of this post.</p>

<div align=center>
  <table border="2">
    <tr><td></td><td style="text-align:center;padding:5px"><strong>infinite</strong><td style="text-align:center;padding:5px"><strong>finite</strong></third></tr>
    <tr><td style="text-align:center;padding:5px"><strong>discrete</strong><td style="text-align:center;padding:15px">Stream</td> <td style="text-align:center;padding:15px">Sequence</td></tr>
    <tr><td style="text-align:center;padding:5px"><strong>continuous</strong><td style="text-align:center;padding:15px">Function</td> <td style="text-align:center;padding:15px"><em>???</em></td></tr>
  </table>
</div>

<p>You can download the <a href="http://conal.net/blog/code/Segment.hs">code for this post</a>.</p>

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

<ul>
<li>2008-12-01: Added <a href="http://conal.net/blog/code/Segment.hs">Segment.hs</a> link.</li>
<li>2008-12-01: Added <code>Monoid</code> instance for function segments.</li>
<li>2008-12-01: Renamed constructor &#8220;<code>DF</code>&#8221; to &#8220;<code>FS</code>&#8221; (for &#8220;function segment&#8221;)</li>
<li>2008-12-05: Tweaked the inequality in <code>mappend</code> on <code>(t :-&gt;# a)</code>. </li>
</ul>

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

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

<!--
This play has a cast of characters you may have met before.
`Monoid` is the star of the show, with `Functor` and `Applicative` playing supporting roles.
`Monad` appears only briefly.
Making her debut appearance on this blog is `Comonad`.
We'll be seeing more of her in future episodes.
-->

<h3>Streams</h3>

<p>I&#8217;ll be using Wouter Swierstra&#8217;s <a href="http://hackage.haskell.org/cgi-bin/hackage-scripts/package/Stream" title="Haskell library on Hackage">Stream library</a>.
A stream is an infinite sequence of values:</p>

<pre><code>data Stream a = Cons a (Stream a)
</code></pre>

<!--, but renaming his `Cons` constructor to `(:<)`, as used in *[The Essence of Dataflow Programming][]*. -->

<p><code>Stream</code> is a functor and an applicative functor.</p>

<pre><code>instance Functor Stream where
    fmap f (Cons x xs) = Cons (f x) (fmap f xs)

instance Applicative Stream where
    pure  = repeat
    (&lt;*&gt;) = zipWith ($)

repeat :: a -&gt; Stream a
repeat x = Cons x (repeat x)
</code></pre>

<h3>Comonads</h3>

<p>Recently I&#8217;ve gotten enamored with comonads, which are dual to monads.
In other words, comonads are like monads but wearing their category arrows backwards.
I&#8217;ll be using the comonad definitions from the <a href="http://hackage.haskell.org/cgi-bin/hackage-scripts/package/category-extras" title="Haskell library on Hackage">category-extras library</a>.</p>

<p>The most helpful intuitive description I&#8217;ve found is that comonads describe <em>values in context</em>.</p>

<p>The <code>return</code> method injects a pure value into a monadic value (having no effect).</p>

<pre><code>return  :: Monad m     =&gt; a -&gt; m a
</code></pre>

<p>The dual to monadic <code>return</code> is <code>extract</code> (sometimes called &#8220;<code>counit</code>&#8221; or &#8220;<code>coreturn</code>&#8220;), which extracts a value out of a comonadic value (discarding the value&#8217;s context).
<a href="http://hackage.haskell.org/cgi-bin/hackage-scripts/package/category-extras" title="Haskell library on Hackage">category-extras library</a> splites this method out from <code>Comonad</code> into the <code>Copointed</code> class:</p>

<pre><code>extract :: Copointed w =&gt; w a -&gt; a
</code></pre>

<p>Monadic values are typically <em>produced</em> in effectful computations:</p>

<pre><code>a -&gt; m b
</code></pre>

<p>Comonadic values are typically <em>consumed</em> in context-sensitive computations:</p>

<pre><code>w a -&gt; b
</code></pre>

<p>(Kleisli arrows wrap the producer pattern, while CoKleisli arrows wrap the consumer pattern.)</p>

<p>Monads have a way to extend a monadic producer into one that consumes to an entire monadic value:</p>

<pre><code>(=&lt;&lt;) :: (Monad m) =&gt; (a -&gt; m b) -&gt; (m a -&gt; m b)
</code></pre>

<p>We more often see this operation in its flipped form (obscuring the conceptual distinction between Haskell arrows and arbitrary category arrows):</p>

<pre><code>(&gt;&gt;=) :: (Monad m) =&gt; m a -&gt; (a -&gt; m b) -&gt; m b
</code></pre>

<p>Dually, comonads have a way to extend a comonadic consumer into one that produces an entire comonadic value:</p>

<pre><code>extend :: (Comonad w) =&gt; (w a -&gt; b) -&gt; (w a -&gt; w b)
</code></pre>

<p>which also has a flipped version:</p>

<pre><code>(=&gt;&gt;) :: (Comonad w) =&gt; w a -&gt; (w a -&gt; b) -&gt; w b
</code></pre>

<p>Another view on monads is as having a way to <code>join</code> two monadic levels into one.</p>

<pre><code>join      :: (Monad   m) =&gt; m (m a) -&gt; m a
</code></pre>

<p>Dually, comonads have a way to <code>duplicate</code> one level into two:</p>

<pre><code>duplicate :: (Comonad w) =&gt; w a -&gt; w (w a)
</code></pre>

<p>For a monad, any of <code>join</code>, <code>(=&lt;&lt;)</code>, and <code>(&gt;&gt;=)</code> can be used to define the others.
For a comonad, any of <code>duplicate</code>, <code>extend</code>, and <code>(=&gt;&gt;)</code> can be used to define the others.</p>

<h3>The Stream comonad</h3>

<p>What might the stream comonad be?</p>

<p>The Stream library already has functions of the necessary types for <code>extract</code> and <code>duplicate</code>, corresponding to familiar list functions:</p>

<pre><code>head :: Stream a -&gt; a
head (Cons x _ ) = x

tails :: Stream a -&gt; Stream (Stream a)
tails xs = Cons xs (tails (tail xs))
</code></pre>

<p>where</p>

<pre><code>tail :: Stream a -&gt; Stream a
tail (Cons _ xs) = xs
</code></pre>

<p>Indeed, <code>head</code> and <code>tails</code> are just what we&#8217;re looking for.</p>

<pre><code>instance Copointed Stream where extract   = head
instance Comonad   Stream where duplicate = tails
</code></pre>

<p>There is also a <code>Monad</code> instance for <code>Stream</code>, in which <code>return</code> is <code>repeat</code> (matching <code>pure</code> as expected) and <code>join</code> is diagonalization, producing a stream whose <em>n<sup>th</sup></em> element is the <em>n<sup>th</sup></em> element of the <em>n<sup>th</sup></em> element of a given stream of streams.</p>

<div class="exercise">

<p><strong>Exercise</strong>: The indexing function <code>(!!)</code> is a sort of semantic function for <code>Stream</code>.
Show that <code>(!!)</code> is a morphism for <code>Functor</code>, <code>Applicative</code>, <code>Monad</code>, and <code>Comonad</code>.
In other words, the meaning of the functor is the functor of the meanings, and similarly for the other type classes.
The <code>Comonad</code> case has a little wrinkle.
See the posts on <a href="http://conal.net/blog/tag/type-class-morphism/" title="Posts on morphisms">type class morphisms</a>.</p>

</div>

<h3>Adding finiteness</h3>

<p>Lists and other possibly-finite sequence types add an interesting new aspect over streams, which is concatenation, usually wrapped in a <code>Monoid</code> instance.</p>

<pre><code>class Monoid o where
    mempty  :: o
    mappend :: o -&gt; o -&gt; o
</code></pre>

<p>Lists also have <code>take</code> and <code>drop</code> operations, which can undo the effect of concatenation, as well as a notion of <code>length</code> (duration).
Let&#8217;s generalize these three to be methods of a new type class, <code>Segment</code>, so that we can defined <em>continuous</em> versions.</p>

<pre><code>class Segment seg len where
    length :: seg -&gt; len
    drop   :: len -&gt; seg -&gt; seg
    take   :: len -&gt; seg -&gt; seg
</code></pre>

<p>For lists, we can use the prelude functions</p>

<pre><code>instance Segment [a] Int where
    length = Prelude.length
    drop   = Prelude.drop
    take   = Prelude.take
</code></pre>

<p>Or the more <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-List.html#26">generic versions</a>:</p>

<pre><code>instance Integral i =&gt; Segment [a] i where
    length = genericLength
    drop   = genericDrop
    take   = genericTake
</code></pre>

<p>These three functions relate to <code>mappend</code>, to give us the following &#8220;Segment laws&#8221;:</p>

<pre><code>drop (length as) (as `mappend` bs) == bs
take (length as) (as `mappend` bs) == as

t &lt;= length as ==&gt; length (take t as) == t
t &lt;= length as ==&gt; length (drop t as) == length as - t
</code></pre>

<h3 id="adding-continuity">Adding continuity</h3>

<p>Streams and lists are <em>discrete</em>, containing countably many or finitely many elements.
They both have <em>continuous</em> counterparts.</p>

<p>When we think of a stream as a function from natural numbers, then John Reynolds&#8217;s alternative arises: functions over <em>real numbers</em>, i.e., a continuum of values.
If we want uni-directional streams, then stick with non-negative reals.</p>

<p>Many stream and list operations are meaningful and useful not only for discrete sequences but also for their continuous counterparts.</p>

<p>The infinite (stream-like) case is already handled by the class instances for functions found in the GHC base libraries (<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Functor-Instances.html" title="Haskell module documentation">Control.Functor.Instances</a> and <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html" title="Haskell module documentation">Control.Applicative</a>).</p>

<pre><code>instance Functor ((-&gt;) t) where
    fmap = (.)

instance Applicative ((-&gt;) t) where
    pure = const
    (f &lt;*&gt; g) x = (f x) (g x)

instance Monad ((-&gt;) t) where
    return = const
    f &gt;&gt;= k =  t -&gt; k (f t) t
</code></pre>

<p>As a consequence,</p>

<pre><code>  join f == f &gt;&gt;= id ==  t -&gt; f t t
</code></pre>

<p>Assume a type wrapper, <code>NonNeg</code>, for non-negative values.
For discrete streams, <code>r == NonNeg Integer</code>, while for continuous streams, <code>r == NonNeg R</code>, for some type <code>R</code> representing reals.</p>

<p>The <a href="http://hackage.haskell.org/packages/archive/category-extras/latest/doc/html/Control-Comonad.html">co-monadic instances</a> from the <a href="http://hackage.haskell.org/cgi-bin/hackage-scripts/package/category-extras" title="Haskell library on Hackage">category-extras library</a>:</p>

<pre><code>instance Monoid o =&gt; Copointed ((-&gt;) o) where
    extract f = f mempty

instance Monoid o =&gt; Comonad ((-&gt;) o) where
    duplicate f x =  y -&gt; f (x `mappend` y)
</code></pre>

<h3>Finite and continuous</h3>

<p>Functions provide a setting for generalized streams.
How do we add finiteness?
A very simple answer is to combine a length (duration) with a function, to form a &#8220;function segment&#8221;:</p>

<pre><code>data t :-&gt;# a = FS t (t -&gt; a)
</code></pre>

<p>The domain of this function is from zero to just short of the given length.</p>

<p>Now let&#8217;s define class instances.</p>

<p><strong>Exercise</strong>: Show that all of the instances below are semantically consistent with the <code>Stream</code> and <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t%3AZipList"><code>ZipList</code></a> instances.</p>

<h4><code>Monoid</code></h4>

<p>Empty function segments have zero duration.
Concatenation adds durations and samples either function, right-shifting the second one.</p>

<pre><code>instance (Ord t, Num t) =&gt; Monoid (t :-&gt;# a) where
    mempty = FS 0 (error "sampling empty 't :-&gt;# a'")
    FS c f `mappend` FS d g =
      FS (c + d) ( t -&gt; if t &lt;= c then f t else g (t - c))
</code></pre>

<h4><code>Segment</code></h4>

<p>The <code>Segment</code> operations are easy to define:</p>

<pre><code>instance Num t =&gt; Segment (t :-&gt;# a) t where
    length (FS d _) = d
    drop t (FS d f) = FS (d - t) ( t' -&gt; f (t + t'))
    take t (FS _ f) = FS t f
</code></pre>

<p>Notice what&#8217;s going on with <code>drop</code>.
The length gets shortened by <code>t</code> (the amount dropped), and the function gets shifted (to the &#8220;left&#8221;) by <code>t</code>.</p>

<p>There&#8217;s also a tantalizing resemblance between this <code>drop</code> definition and <code>duplicate</code> for the function comonad.
We&#8217;ll return in another post to tease out this and</p>

<p>I&#8217;ve allowed dropping or taking more than is present, though these cases can be handled with an error or a by taking or dropping fewer elements (as with the list <code>drop</code> and <code>take</code> functions).</p>

<h4><code>Functor</code>, <code>Zip</code> and <code>Applicative</code></h4>

<p><code>fmap</code> applies a given function to each of the function values, leaving the length unchanged.</p>

<pre><code>instance Functor ((:-&gt;#) t) where
    fmap h (FS d f) = FS d (h . f)
</code></pre>

<p><code>zip</code> pairs corresponding segment values and runs out with the shorter segment.
(See <em><a href="http://conal.net/blog/posts/more-beautiful-fold-zipping" title="blog post">More beautiful fold zipping</a></em> for the <code>Zip</code> class.)</p>

<pre><code>instance Ord t =&gt; Zip ((:-&gt;#) t) where
    FS xd xf `zip` FS yd yf = FS (xd `min` yd) (xf `zip` yf)
</code></pre>

<p><code>pure</code> produces a constant value going forever.
<code>(&lt;*&gt;)</code> applies functions to corresponding arguments, running out with the shorter.</p>

<pre><code>instance (Ord t, Bounded t) =&gt; Applicative ((:-&gt;#) t) where
    pure a = FS maxBound (const a)
    (&lt;*&gt;)  = zipWith ($)
</code></pre>

<h4><code>Copointed</code> and <code>Comonad</code></h4>

<p><code>extract</code> pulls out the initial value (like <code>head</code>).</p>

<pre><code>instance Num t =&gt; Copointed ((:-&gt;#) t) where
    extract (FS _ f) = f 0
</code></pre>

<p><code>duplicate</code> acts like <code>tails</code>.
The generated segments are progressivly <code>drop</code>ped versions of the original segment.</p>

<pre><code>instance Num t =&gt; Comonad ((:-&gt;#) t) where
    duplicate s = FS (length s) (flip drop s)
</code></pre>

<h4><code>Monad</code></h4>

<p>I don&#8217;t know if there is a monad instance for <code>((:-&gt;#) t)</code>.
Simple diagonalization doesn&#8217;t work for <code>join</code>, since the <em>n<sup>th</sup></em> segment might be shorter than <em>n</em>.</p>

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

<p>The instances above remind me strongly of type class instances for several common types.
Another post will tease out some patterns and reconstruct <code>(t :-&gt;# a)</code> out of standard components, so that most of the code above can disappear.</p>

<p>Another post incorporates <code>(t :-&gt;# a)</code> into a new model and implementation of relative-time, comonadic FRP.</p>
<p><a href="http://conal.net/blog/?flattrss_redirect&amp;id=65&amp;md5=844ce48c997f35fb216e2d03108a8a21"><img src="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png" srcset="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@2x.png 2xhttp://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@3x.png 3x" alt="Flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/sequences-streams-and-segments/feed</wfw:commentRss>
		<slash:comments>13</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=conal&amp;popout=1&amp;url=http%3A%2F%2Fconal.net%2Fblog%2Fposts%2Fsequences-streams-and-segments&amp;language=en_GB&amp;category=text&amp;title=Sequences%2C+streams%2C+and+segments&amp;description=What+kind+of+thing+is+a+movie%3F+Or+a+song%3F+Or+a+trajectory+from+point+A+to+point+B%3F+If+you%26%238217%3Bre+a+computer+programmer%2Fprogrammee%2C+you+might+say+that+such+things...&amp;tags=applicative+functor%2Ccomonad%2CFRP%2Cfunction%2Cfunctional+reactive+programming%2Cfunctor%2Cmonoid%2Csequence%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Semantic editor combinators</title>
		<link>http://conal.net/blog/posts/semantic-editor-combinators</link>
		<comments>http://conal.net/blog/posts/semantic-editor-combinators#comments</comments>
		<pubDate>Tue, 25 Nov 2008 06:06:52 +0000</pubDate>
		<dc:creator><![CDATA[Conal]]></dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[applicative functor]]></category>
		<category><![CDATA[arrow]]></category>
		<category><![CDATA[combinator]]></category>
		<category><![CDATA[editor]]></category>
		<category><![CDATA[technique]]></category>
		<category><![CDATA[type composition]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=64</guid>
		<description><![CDATA[While working on Eros, I encountered a function programming pattern I hadn&#8217;t known. I was struck by the simplicity and power of this pattern, and I wondered why I hadn&#8217;t run into it before. I call this idea &#8220;semantic editor combinators&#8221;, because it&#8217;s a composable way to create transformations on rich values. I&#8217;m writing this [&#8230;]]]></description>
				<content:encoded><![CDATA[<!-- 

Title: Semantic editor combinators

Tags: editor, combinator, arrow, applicative functor, technique, type composition

URL: http://conal.net/blog/posts/semantic-editor-combinators/

-->

<!-- references -->

<!-- teaser -->

<p>While working on <a href="http://haskell.org/haskellwiki/Eros" title="Wiki page: Eros -- tangible functional programming">Eros</a>, I encountered a function programming pattern I hadn&#8217;t known.
I was struck by the simplicity and power of this pattern, and I wondered why I hadn&#8217;t run into it before.
I call this idea &#8220;semantic editor combinators&#8221;, because it&#8217;s a composable way to create transformations on rich values.
I&#8217;m writing this post in order to share this simple idea, which is perhaps &#8220;almost obvious&#8221;, but not quite, due to two interfering habits:</p>

<ul>
<li>thinking of function composition as binary instead of unary, and</li>
<li>seeing the functions <code>first</code> and <code>second</code> as about arrows, and therefore esoteric.</li>
</ul>

<p>What I enjoy most about these (semantic) editor combinators is that their use is type-directed and so doesn&#8217;t require much imagination.
When I have the type of a complex value, and I want to edit some piece buried inside, I just read off the path in the containing type, on the way to the buried value.</p>

<p>I started writing this post last year and put it aside.
Recent threads on the <a href="http://www.haskell.org/mailman/listinfo/reactive">Reactive mailing list</a> (including a dandy <a href="http://www.haskell.org/pipermail/reactive/2008-November/000054.html">explanation by Peter Verswyvelen</a>) and on <a href="http://netsuperbrain.com/blog/">David Sankel&#8217;s blog</a> reminded me of my unfinished post, so I picked it up again.</p>

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

<ul>
<li>2008-11-29: added type of <code>v6</code> example.  Tweaked <code>inO2</code> alignment.</li>
</ul>

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

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

<h3>Example</h3>

<p>Suppose we have a value defined as follows.</p>

<pre><code>v0 :: (Int, Char -&gt; (String,Bool))
v0 = (3,  c -&gt; ([c,'q',c,'r'], isDigit c))
</code></pre>

<p>Now, how can one edit this value?
By &#8220;edit&#8221;, I simply mean to produce a value that resembles <code>v0</code> but has alterations made.</p>

<p>For concreteness, let&#8217;s say I want to reverse the string.
A programmer might answer by firing up vim, Emacs, or <a href="http://www.haskell.org/haskellwiki/Yi">yi (whee!)</a>, and change the definition to</p>

<pre><code>v0 = (3,  c -&gt; (['r',c,'q',c], isDigit c))
</code></pre>

<p>But this answer doesn&#8217;t really fit the question, which was to edit the <em>value</em>.
I want a way to edit the <em>semantics</em>, not the <em>syntax</em>, i.e., the value rather than the expression.</p>

<p>Similarly, I might want to</p>

<ul>
<li>negate the boolean,</li>
<li>double the Int,</li>
<li>swap the inner pair, or</li>
<li>swap the outer pair.</li>
</ul>

<p>Semantic editor combinators makes these tasks easy, using the following recipe, writing from left to right:</p>

<ul>
<li>Read out the path in the type of the value being edited (<code>v0</code> here), from the outside to the part to edit, naming each step, according to <em>part</em> of the type being entered, using the names &#8220;first&#8221; and &#8220;second&#8221; for pairs, and &#8220;result&#8221; for functions.</li>
<li>Write down that path as its part names, separated by periods.  If the path has at least two components, surrounded by parentheses.</li>
<li>Next write the alteration function to be applied.</li>
<li>Finally, the value being edited.</li>
</ul>

<p>In our string-reversal example, these steps look like</p>

<ul>
<li><code>(second.result.first)</code> &#8212; <em>second of the pair, result of that function, and first of that pair</em></li>
<li><code>reverse</code></li>
<li><code>v0</code></li>
</ul>

<p>So the value editing is accomplished by</p>

<pre><code>v1 = (second.result.first) reverse v0
</code></pre>

<p>Let&#8217;s see if the editing worked.
First, inspect <code>v0</code>, using GHCi:</p>

<pre><code>&gt; fst v0
3
&gt; (snd v0) 'z'
("zqzr",False)
</code></pre>

<p>and then <code>v1</code>:</p>

<pre><code>&gt; :ty v1
v1 :: (Int, Char -&gt; (String, Bool))
&gt; fst v1
3
&gt; (snd v1) 'z'
("rzqz",False)
</code></pre>

<p>The other four editings listed above are just as easy</p>

<pre><code>double x   = x+x
swap (x,y) = (y,x)

-- negate the boolean,
v2 = (second.result.second) not v0

-- double the Int,
v3 = first double v0

-- swap the inner pair, or
v4 = (second.result) swap v0

-- swap the outer pair
v5 = swap v0
</code></pre>

<p>Testing in GHCi:</p>

<pre><code>&gt; :ty v2
v2 :: (Int, Char -&gt; (String, Bool))
&gt; fst v2
3
&gt; (snd v2) 'z'
("zqzr",True)

&gt; :ty v3
v3 :: (Int, Char -&gt; (String, Bool))
&gt; fst v3
6
&gt; (snd v3) 'z'
("zqzr",False)

&gt; :ty v4
v4 :: (Int, Char -&gt; (Bool, String))
&gt; fst v4
3
&gt; (snd v4) 'z'
(False,"zqzr")

&gt; :ty v5
v5 :: (Char -&gt; (String, Bool), Int)
&gt; fst v5
&lt;interactive&gt;:1:0: No instance for (Show (Char -&gt; (String, Bool))) ...
&gt; snd v5
3
&gt; fst v5 'z'
("zqzr",False)
</code></pre>

<p>Since <code>String</code> is a synonym for <code>[Char]</code>, the type of <code>v0</code> has even more structure we can delve into:</p>

<pre><code>v0 :: (Int, Char -&gt; ([Char],Bool))
</code></pre>

<p>Add a fourth path component, <code>element</code>, for entering elements of lists.
Now we can edit the characters:</p>

<pre><code>-- previous character
v6 = (second.result.first.element) pred v0

-- character value
v7 = (second.result.first.element) ord v0
</code></pre>

<p>Testing:</p>

<pre><code>&gt; :ty v6
v6 :: (Int, Char -&gt; ([Char], Bool))
&gt; fst v6
3
&gt; (snd v6) 'z'
("ypyq",False)

&gt; :ty v7
v7 :: (Int, Char -&gt; ([Int], Bool))
&gt; fst v7
3
&gt; (snd v7) 'z'
([122,113,122,114],False)
</code></pre>

<h3>What&#8217;s going on here?</h3>

<p>You may be familiar with the functions <code>first</code> and <code>second</code>.  They&#8217;re methods on the <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html"><code>Arrow</code> class</a>, so they&#8217;re pretty general.
When operating on <em>functions</em>, they have the following types and definitions:</p>

<pre><code>first  :: (a -&gt; a') -&gt; ((a,b) -&gt; (a',b))
second :: (b -&gt; b') -&gt; ((a,b) -&gt; (a,b'))

first  f =  (a,b) -&gt; (f a, b)
second g =  (a,b) -&gt; (a, g b)
</code></pre>

<p>Note that, as needed, <code>first</code> and <code>second</code> apply given functions to part of a pair value, carrying along the other half of the pair.</p>

<p>When working syntactically, we often apply functions under lambdas.
The corresponding value-level, embedding combinator is just function composition.
I&#8217;ll use the name <code>result</code> as a synonym for <code>(.)</code>, for consistency with <code>first</code> and <code>second</code> and, more importantly, for later generalization.</p>

<pre><code>result :: (b -&gt; b') -&gt; ((a -&gt; b) -&gt; (a -&gt; b'))
result =  (.)
</code></pre>

<p>As with <code>first</code> and <code>second</code>, I&#8217;ve used parentheses in the type signatures to emphasize the path components as being <em>unary</em>, not binary.
It&#8217;s this unusual unary view that makes <code>first</code>, <code>second</code>, and <code>result</code> (or <code>(.)</code>) composable and guides us toward composable generalizations.</p>

<p>Similarly, the <code>element</code> component is synonym for <code>fmap</code>:</p>

<pre><code>element :: (a -&gt; b) -&gt; ([a] -&gt; [b])
element = fmap
</code></pre>

<p>However, using <code>fmap</code> directly is very useful, since it encompasses <em>all</em> <code>Functor</code> types.
Since functions from any given type is a functor, I often use <code>fmap</code> in place of <code>result</code>, just to avoid having to define result.
Thanks to the <code>Functor</code> instance of pairing, we can also use <code>fmap</code> in place <code>second</code>.
(The two, however, are <a href="http://netsuperbrain.com/blog/posts/analysis-of-lazy-stream-programs/">not quite the same</a>.)
An advantage of names like <code>result</code>, <code>element</code> and <code>second</code> is that they&#8217;re more specifically descriptive.
Hence they are easier for people to read, and they lead to more helpful error messages.</p>

<h3>Found in the wild</h3>

<p>The examples above are toys I contrived to give you the basic idea.
Now I&#8217;d like to show you how very useful this technique can be in practice.</p>

<p><a href="http://haskell.org/haskellwiki/Reactive" title="Wiki page">Reactive</a> represents events via two types, <code>Future</code> and <code>Reactive</code>:</p>

<pre><code>newtype EventG t a = Event (FutureG t (ReactiveG t a))
</code></pre>

<p>The <code>Functor</code> instance uses the functor instances of <code>FutureG</code> and <code>ReactiveG</code>:</p>

<pre><code>instance Functor (EventG t) where fmap = inEvent.fmap.fmap
</code></pre>

<p>The <code>inEvent</code> functional is also a semantic editor combinator, though not so obvious from the types.
It applies a given function inside the representation of an event.</p>

<p>One pattern I use quite a lot is applying a function to the result of a curried-style multi-argument function.
For instance, <a href="http://haskell.org/haskellwiki/Reactive" title="Wiki page">Reactive</a> has a <a href="http://hackage.haskell.org/packages/archive/reactive/latest/doc/html/FRP-Reactive-Reactive.html#v%3AcountE"><code>countE</code></a> function to count event occurrences.
Each occurrence value get paired with the number of occurrences so far.</p>

<pre><code>countE :: Num n =&gt; Event b -&gt; Event (b,n)
countE = stateE 0 (+1)
</code></pre>

<p>Quite often, I use a forgetful form, <a href="(http://hackage.haskell.org/packages/archive/reactive/latest/doc/html/FRP-Reactive-Reactive.html#v%3AcountE_)"><code>countE_</code></a>, which discards the original values.
Looking at the type of <code>countE</code>, I know to direct <code>snd</code> into the <code>result</code> and then into the values of the event.
The definition follows robomatically from the type.</p>

<pre><code>countE_ :: Num n =&gt; Event b -&gt; Event n
countE_ = (result.fmap) snd countE
</code></pre>

<p>You can play this game with any number of arguments.
For instance, Reactive has <a href="http://hackage.haskell.org/packages/archive/reactive/latest/doc/html/FRP-Reactive-Behavior.html#v%3Asnapshot">a function for snaphotting behaviors on each occurrence of an event</a>, taking an initial state, state transition function, and an input event.</p>

<pre><code>snapshot :: Event a -&gt; Reactive b -&gt; Event (a,b)
</code></pre>

<p>The forgetful version descends into two results and one event, to edit the pair found there:</p>

<pre><code>snapshot_ :: Event a -&gt; Reactive b -&gt; Event b
snapshot_ = (result.result.fmap) snd snapshot
</code></pre>

<p>I could have written the following pointful version instead:</p>

<pre><code>snapshot_ e b = fmap snd (snapshot e b)
</code></pre>

<p>As a more varied example, consider these two functions:</p>

<pre><code>withRestE :: Event a -&gt; Event (a, Event a)

firstE :: Event a -&gt; a
</code></pre>

<p>The function <a href="http://hackage.haskell.org/packages/archive/reactive/latest/doc/html/FRP-Reactive-Reactive.html#v%3AwithNextE"><code>withNextE</code></a> directs <code>firstE</code> into the inner event of <code>withRestE</code>.</p>

<pre><code>withNextE :: Event a -&gt; Event (a,a)
withNextE = (result.fmap.second) firstE withRestE
</code></pre>

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

<p>We have <code>first</code> <code>second</code> to edit the first and second part of a pair.
We also have <code>result</code> to edit result part of a function.
We can also edit the <code>argument</code> part:</p>

<pre><code>argument :: (a' -&gt; a) -&gt; ((a -&gt; b) -&gt; (a' -&gt; b))
</code></pre>

<p>This editor combinator has a different flavor from the others, in that it is <em>contravariant</em>.
(Note the primes.)
Its definition is simple:</p>

<pre><code>argument = flip (.)
</code></pre>

<h3>Higher arity</h3>

<p>The combinators above direct unary functions into single structures.
A similar game works for n-ary functions, using the applicative functor lifters.
For instance,</p>

<pre><code>liftA2 :: (Applicative f) =&gt;
          (a -&gt; b -&gt; c) -&gt; (f a -&gt; f b -&gt; f c)
</code></pre>

<p>Since <code>liftA2</code> promotes <em>arbitrary</em> binary functions to binary functions, it can be       applied again &amp; again.</p>

<pre><code>*Main Control.Applicative&gt; :ty liftA2.liftA2.liftA2
liftA2.liftA2.liftA2 ::
  (Applicative f, Applicative g , Applicative h) =&gt;
  (a -&gt; b -&gt; c) -&gt; f (g (h a)) -&gt; f (g (h b)) -&gt; f (g (h c))
</code></pre>

<p>Similarly for <code>liftA3</code> etc.</p>

<p>Now an in-the-wild higher-arity example, taken from the <a href="http://haskell.org/haskellwiki/TypeCompose" title="Wiki page">TypeCompose</a> library.</p>

<p>Here&#8217;s a very handy way to compose two type constructors:</p>

<pre><code>newtype (g :. f) a = O { unO :: g (f a) }
</code></pre>

<p>We can apply n-ary function within <code>O</code> constructors:</p>

<pre><code>inO :: (g (f a) -&gt; g' (f' a')) -&gt; ((g :. f) a -&gt; (g' :. f') a')
inO h (O gfa) = O (h gfa)

inO2 :: ( g (f a)   -&gt;  g' (f' a')   -&gt;  g'' (f'' a''))
     -&gt; ((g :. f) a -&gt; (g' :. f') a' -&gt; (g'' :. f'') a'')
inO2 h (O gfa) (O gfa') = O (h gfa gfa')

...
</code></pre>

<p>Less pointedly,</p>

<pre><code>inO  = (  O  .) . (. unO)
inO2 = (inO  .) . (. unO)
inO3 = (inO2 .) . (. unO)
...
</code></pre>

<p>Functors compose into functors, and applicatives compose into applicatives.
(See <em><a href="http://www.soi.city.ac.uk/~ross/papers/Applicative.html" title="paper by Conor McBride and Ross Paterson">Applicative Programming with Effects</a></em>.)
The semantic-editor-combinator programming style makes these for very simple instance definitions:</p>

<pre><code>instance (Functor g, Functor f) =&gt; Functor (g :. f) where
  fmap  = inO.fmap.fmap

instance (Applicative g, Applicative f) =&gt; Applicative (g :. f) where
  pure  = O . pure . pure
  (&lt;*&gt;) = (inO2.liftA2) (&lt;*&gt;)
</code></pre>

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

<p>Two of our semantic editor combinators (path components) have more general types than we&#8217;ve used so far:</p>

<pre><code>first  :: Arrow (~&gt;) =&gt; (a ~&gt; a') -&gt; ((a,b) ~&gt; (a',b))
second :: Arrow (~&gt;) =&gt; (b ~&gt; b') -&gt; ((a,b) ~&gt; (a,b'))
</code></pre>

<p>I&#8217;ve simply replaced some of the <code>(-&gt;)</code>&#8216;s in the function-specific types with an arbitrary arrow type <code>(~&gt;)</code>.</p>

<p>Another post will similarly generalize <code>result</code> and then will give some nifty uses of this generalization for applying combinator-based semantic editing beyond simple values to</p>

<ul>
<li>Type representations</li>
<li>Code</li>
<li>Graphical user interfaces</li>
<li>Combinations of all of the above (including values)</li>
</ul>
<p><a href="http://conal.net/blog/?flattrss_redirect&amp;id=64&amp;md5=ade9ae9f5430ab6618064641dafbc6d4"><img src="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png" srcset="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@2x.png 2xhttp://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@3x.png 3x" alt="Flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/semantic-editor-combinators/feed</wfw:commentRss>
		<slash:comments>17</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=conal&amp;popout=1&amp;url=http%3A%2F%2Fconal.net%2Fblog%2Fposts%2Fsemantic-editor-combinators&amp;language=en_GB&amp;category=text&amp;title=Semantic+editor+combinators&amp;description=While+working+on+Eros%2C+I+encountered+a+function+programming+pattern+I+hadn%26%238217%3Bt+known.+I+was+struck+by+the+simplicity+and+power+of+this+pattern%2C+and+I+wondered+why+I+hadn%26%238217%3Bt...&amp;tags=applicative+functor%2Carrow%2Ccombinator%2Ceditor%2Ctechnique%2Ctype+composition%2Cblog" type="text/html" />
	</item>
		<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><![CDATA[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 [&#8230;]]]></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-fold-zipping/" 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>

<pre><code>data Fold b c = forall a. F (a -&gt; b -&gt; a) a (a -&gt; c)  -- clever version
</code></pre>

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

<pre><code>data Fold b a = F (a -&gt; b -&gt; a) a                     -- simple version
</code></pre>

<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>

<pre><code>data WithCont z c = forall a. WC (z a) (a -&gt; c)
</code></pre>

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

<pre><code>type FoldC b = WithCont (Fold b)
</code></pre>

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

<pre><code>cfoldlc :: FoldC b a -&gt; [b] -&gt; a
cfoldlc (WC f k) = k . cfoldl f
</code></pre>

<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>

<pre><code>data WithCont' z c = forall a. WC' (z a) (a -&gt; c)

type FoldC' b = WithCont' (Fold b)

cfoldlc' :: FoldC' b a -&gt; [b] -&gt; a
cfoldlc' (WC' f k) = k . cfoldl' f
</code></pre>

<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>

<pre><code>instance Functor (WithCont z) where
  fmap g (WC f k) = WC f (g . k)

instance Zip z =&gt; Applicative (WithCont z) where
  pure a = WC undefined (const a)
  WC hf hk &lt;*&gt; WC xf xk =
    WC (hf `zip` xf) ( (a,a') -&gt; (hk a) (xk a'))


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

instance Zip' z =&gt; Applicative (WithCont' z) where
  pure a = WC' undefined (const a)
  WC' hf hk &lt;*&gt; WC' xf xk =
    WC' (hf `zip'` xf) ( (P a a') -&gt; (hk a) (xk a'))
</code></pre>

<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>

<pre><code>cfoldlc (fmap g wc) == fmap g (cfoldlc wc)
</code></pre>

<p>Adding some structure:</p>

<pre><code>cfoldlc (fmap g (WC f k)) == fmap g (cfoldlc (WC f k))
</code></pre>

<p>Proof:</p>

<pre><code>cfoldlc (fmap g (WC f k))

  == {- fmap on WithCont -}

cfoldlc (WC f (g . k))

  == {- cfoldlc def -}

(g . k) . cfoldl f

  == {- associativity of (.)  -}

g . (k . cfoldl f)

  == {- cfoldl def -}

g . cfoldlc (WC f k)

  == {- fmap on functions -}

fmap g (cfoldlc (WC f k))
</code></pre>

<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>

<pre><code>cfoldlc (pure a) == pure a
</code></pre>

<p>Proof:</p>

<pre><code>cfoldlc (pure a)

  == {- pure on WithCont -}

cfoldlc (WC undefined (const a))

  == {- cfoldlc def -}

const a . cfoldl undefined

  == {- property of const -}

const a

  == {- pure on functions -}

pure a
</code></pre>

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

<pre><code>cfoldlc (wch &lt;*&gt; wcx) == cfoldlc wch &lt;*&gt; cfoldlc wcx
</code></pre>

<p>Adding structure:</p>

<pre><code>cfoldlc ((WC hf hk) &lt;*&gt; (WC xf xk)) == cfoldlc (WC hf hk) &lt;*&gt; cfoldlc (WC xf xk)
</code></pre>

<p>Proof:</p>

<pre><code>cfoldlc ((WC hf hk) &lt;*&gt; (WC xf xk))

  == {- (&lt;*&gt;) on WithCont -}

cfoldlc (WC (hf `zip` xf) ( (a,a') -&gt; (hk a) (xk a')))

  == {- cfoldlc def -}

( (a,a') -&gt; (hk a) (xk a')) . cfoldl (hf `zip` xf)

  == {- Zip morphism law for Fold -}

( (a,a') -&gt; (hk a) (xk a')) . (cfoldl hf `zip` cfoldl xf)

  == {- zip def on functions -}

( (a,a') -&gt; (hk a) (xk a')) .  bs -&gt; (cfoldl hf bs, cfoldl xf bs)

  == {- (.) def -}

 bs -&gt; (hk (cfoldl hf bs)) (xk (cfoldl xf bs))

  == {- (&lt;*&gt;) on functions -}

(hk . cfoldl hf bs) &lt;*&gt; (xk . cfoldl xf)

  == {- cfoldlc def, twice -}

cfoldlc (WC hf hk) &lt;*&gt; cfoldlc (WC xf xk)
</code></pre>

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

<p>I like that <code>WithCont</code> allows composing the morphism proofs, in addition to the implementations.</p>
<p><a href="http://conal.net/blog/?flattrss_redirect&amp;id=61&amp;md5=5eb0b4e41def72139d912156d5ec8d84"><img src="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png" srcset="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@2x.png 2xhttp://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@3x.png 3x" alt="Flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/enhancing-a-zip/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=conal&amp;popout=1&amp;url=http%3A%2F%2Fconal.net%2Fblog%2Fposts%2Fenhancing-a-zip&amp;language=en_GB&amp;category=text&amp;title=Enhancing+a+Zip&amp;description=This+post+is+part+four+of+the+zip+folding+series+inspired+by+Max+Rabkin%26%238217%3Bs+Beautiful+folding+post.+I+meant+to+write+one+little+post%2C+but+one+post+turned+into+four....&amp;tags=applicative+functor%2Cfold%2Cfunctor%2Cproof%2Ctype+class+morphism%2Czip%2Cblog" type="text/html" />
	</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><![CDATA[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 [&#8230;]]]></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>

<pre><code>data Fold b c = forall a. F (a -&gt; b -&gt; a) a (a -&gt; c)
</code></pre>

<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>

<pre><code>cfoldl' :: Fold b c -&gt; [b] -&gt; c
cfoldl' (F op e k) = k . foldl' op e
</code></pre>

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

<pre><code>foldl' :: (a -&gt; b -&gt; a) -&gt; a -&gt; [b] -&gt; a
foldl' op a []     = a
foldl' op a (b:bs) =
  let a' = a `op` b in a' `seq` foldl' f a' bs
</code></pre>

<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>

<pre><code>instance Functor (Fold b) where
  fmap h (F op e k) = F op e (h . k)
</code></pre>

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

<pre><code>data Pair c c' = P !c !c'
</code></pre>

<p>The instance:</p>

<pre><code>instance Applicative (Fold b) where
  pure a = F (error "no op") (error "no e") (const a)

  F op e k &lt;*&gt; F op' e' k' = F op'' e'' k''
   where
     P a a' `op''` b = P (a `op` b) (a' `op'` b)
     e''             = P e e'
     k'' (P a a')    = (k a) (k' a')
</code></pre>

<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>

<pre><code>forall c f g xs.
   h (cfoldl' f xs) (cfoldl' g xs) == cfoldl' (liftA2 h f g) xs
</code></pre>

<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>

<pre><code>forall c f g.
  liftA2 h (cfoldl' f) (cfoldl' g) == cfoldl' (liftA2 h f g)
</code></pre>

<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>

<pre><code>forall f g.
  cfoldl' f &lt;*&gt; cfoldl' g == cfoldl' (f &lt;*&gt; g)
</code></pre>

<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>

<pre><code>cfoldl' (pure a  ) == pure a

cfoldl' (fmap h f) == fmap h (cfoldl' f)
</code></pre>

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

<pre><code>cfoldl' (pure a  ) == const a

cfoldl' (fmap h f) == h . cfoldl' f
</code></pre>

<p>or</p>

<pre><code>cfoldl' (pure a  ) xs == a

cfoldl' (fmap h f) xs == h (cfoldl' f xs)
</code></pre>

<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>

<pre><code>cfoldl' (fmap h f) == fmap h (cfoldl' f)
</code></pre>

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

<pre><code>cfoldl' (fmap h (F op e k)) == fmap h (cfoldl' (F op e k))
</code></pre>

<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>

<pre><code>fmap h (cfoldl' (F op e k))

  ==  {- inline cfoldl' -}

fmap h (k . foldl' op e)

  ==  {- inline fmap on functions -}

h . (k . foldl' op e)

  ==  {- associativity of (.) -}

(h . k) . foldl' op e

  ==  {- uninline cfoldl' -}

cfoldl' (F op e (h . k))

  ==  {- uninline fmap on Fold  -}

cfoldl' (fmap h (F op e k))
</code></pre>

<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>

<pre><code>data FoldS a b = FS (a -&gt; b -&gt; a) a
</code></pre>

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

<pre><code>h . foldsl' op e
</code></pre>

<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>

<pre><code>cfoldl' (pure a) == pure a
</code></pre>

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

<pre><code>pure a

  ==  {- inline pure on functions -}

const a

  ==  {- property of const -}

const a . foldl op e

  ==  {- uninline cfoldl' -}

cfoldl' (F (const a) op e)
</code></pre>

<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>

<pre><code>instance Applicative (Fold b) where
  pure a = F undefined undefined (const a)
</code></pre>

<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>
<p><a href="http://conal.net/blog/?flattrss_redirect&amp;id=58&amp;md5=654d33555237a7a03d4b82df83d84d6f"><img src="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png" srcset="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@2x.png 2xhttp://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@3x.png 3x" alt="Flattr this!"/></a></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>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=conal&amp;popout=1&amp;url=http%3A%2F%2Fconal.net%2Fblog%2Fposts%2Fanother-lovely-example-of-type-class-morphisms&amp;language=en_GB&amp;category=text&amp;title=Another+lovely+example+of+type+class+morphisms&amp;description=I+read+Max+Rabkin%26%238217%3Bs+recent+post+Beautiful+folding+with+great+excitement.+He+shows+how+to+make+combine+multiple+folds+over+the+same+list+into+a+single+pass%2C+which+can+then...&amp;tags=applicative+functor%2Cfold%2Cfunctor%2Csemantics%2Ctype+class%2Ctype+class+morphism%2Czip%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Beautiful differentiation</title>
		<link>http://conal.net/blog/posts/beautiful-differentiation</link>
		<comments>http://conal.net/blog/posts/beautiful-differentiation#comments</comments>
		<pubDate>Wed, 07 May 2008 22:26:08 +0000</pubDate>
		<dc:creator><![CDATA[Conal]]></dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[applicative functor]]></category>
		<category><![CDATA[beautiful code]]></category>
		<category><![CDATA[derivative]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=43</guid>
		<description><![CDATA[Lately I&#8217;ve been playing again with parametric surfaces in Haskell. Surface rendering requires normals, which can be constructed from partial derivatives, which brings up automatic differentiation (AD). Playing with some refactoring, I&#8217;ve stumbled across a terser, lovelier formulation for the derivative rules than I&#8217;ve seen before. Edits: 2008-05-08: Added source files: NumInstances.hs and Dif.hs. 2008-05-20: [&#8230;]]]></description>
				<content:encoded><![CDATA[<!-- 

Title: Beautiful differentiation

Tags: automatic differentiation, applicative functors, beautiful code

URL: http://conal.net/blog/posts/beautiful-differentiation/

-->

<!-- references -->

<!-- teaser -->

<p>Lately I&#8217;ve been playing again with parametric surfaces in Haskell.
Surface rendering requires normals, which can be constructed from partial derivatives, which brings up <em>automatic differentiation</em> (AD).
Playing with some refactoring, I&#8217;ve stumbled across a terser, lovelier formulation for the derivative rules than I&#8217;ve seen before.</p>

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

<ul>
<li>2008-05-08: Added source files: <a href='http://conal.net/blog/wp-content/uploads/2008/05/NumInstances.hs'>NumInstances.hs</a> and <a href='http://conal.net/blog/wp-content/uploads/2008/05/Dif.hs'>Dif.hs</a>.</li>
<li>2008-05-20: Changed some variable names for clarity and consistency.  For instance, <code>x@(D x0 x')</code> instead of <code>p@(D x x')</code>.</li>
<li>2008-05-20: Removed extraneous <code>Fractional</code> constraint in the <code>Floating</code> instance of <code>Dif</code>.</li>
</ul>

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

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

<h3>Automatic differentiation</h3>

<p>The idea of AD is to simultaneously manipulate values and derivatives.
Overloading of the standard numerical operations (and literals) makes this combined manipulation as simple and pretty as manipulating values without derivatives.</p>

<p>In <em><a href="http://citeseer.ist.psu.edu/karczmarczuk98functional.html" title="ICFP '98 paper &quot;Functional Differentiation of Computer Programs&quot; by Jerzy Karczmarczuk">Functional Differentiation of Computer Programs</a></em>, Jerzy Karczmarczuk extended the usual trick to a &#8220;lazy tower of derivatives&#8221;.
He exploited Haskell&#8217;s laziness to carry <em>infinitely many</em> derivatives, rather than just one.
<a href="http://augustss.blogspot.com/2007/04/overloading-haskell-numbers-part-2.html" title="Blog post: &quot;Overloading Haskell numbers, part 2, Forward Automatic Differentiation&quot;">Lennart Augustsson&#8217;s AD post</a> contains a summary of Jerzy&#8217;s idea and an application.
I&#8217;ll use some of the details from Lennart&#8217;s version, for simplicity.</p>

<p>For some perspectives on the mathematical structuure under AD, see <a href="http://sigfpe.blogspot.com/2005/07/automatic-differentiation.html">sigfpe&#8217;s AD post</a>, and <em><a href="http://vandreev.wordpress.com/2006/12/04/non-standard-analysis-and-automatic-differentiation/" title="Blog post by Vlad Andreev">Non-standard analysis, automatic differentiation, Haskell, and other stories</a></em>.</p>

<h3>Representation and overloadings</h3>

<p>The tower of derivatives can be represented as an infinite list.
Since we&#8217;ll use operator overloadings that are not meaningful for lists in general, let&#8217;s instead define a new data type:</p>

<pre><code>data Dif a = D a (Dif a)
</code></pre>

<p>Given a function <code>f :: a -&gt; Dif b</code>, <code>f a</code> has the form <code>D x (D x' (D x'' ...))</code>, where <code>x</code> is the value at <code>a</code>, and <code>x'</code>, <code>x''</code> &#8230;, are the derivatives (first, second, &#8230;) at <code>a</code>.</p>

<p>Constant functions have all derivatives equal to zero.</p>

<pre><code>dConst :: Num a =&gt; a -&gt; Dif a
dConst x0 = D x0 dZero

dZero :: Num a =&gt; Dif a
dZero = D 0 dZero
</code></pre>

<p>Numeric overloadings then are simple.
For instance,</p>

<pre><code>instance Num a =&gt; Num (Dif a) where
  fromInteger               = dConst . fromInteger
  D x0 x' + D y0 y'         = D (x0 + y0) (x' + y')
  D x0 x' - D y0 y'         = D (x0 - y0) (x' - y')
  x@(D x0 x') * y@(D y0 y') = D (x0 * y0) (x' * y + x * y')
</code></pre>

<p>In each of the right-hand sides of these last three definitions, the first argument to <code>D</code> is constructed using <code>Num a</code>, while the second argument is <em>recursively</em> constructed using <code>Num (Dif a)</code>.</p>

<p><a href="http://citeseer.ist.psu.edu/karczmarczuk98functional.html" title="ICFP '98 paper &quot;Functional Differentiation of Computer Programs&quot; by Jerzy Karczmarczuk">Jerzy&#8217;s paper</a> uses a function to provide all of the derivatives of a given function (called <code>dlift</code> from Section 3.3):</p>

<pre><code>lift :: Num a =&gt; [a -&gt; a] -&gt; Dif a -&gt; Dif a
lift (f : f') p@(D x x') = D (f x) (x' * lift f' p)
</code></pre>

<p>The given list of functions are all of the derivatives of a given function.
Then, derivative towers can be constructed by definitions like the following:</p>

<pre><code>instance Floating a =&gt; Floating (Dif a) where
  pi               = dConst pi
  exp (D x x')     = r where r = D (exp x) (x' * r)
  log p@(D x x')   = D (log x) (x' / p)
  sqrt (D x x')    = r where r = D (sqrt x) (x' / (2 * r))
  sin              = lift (cycle [sin, cos, negate . sin, negate . cos])
  cos              = lift (cycle [cos, negate . sin, negate . cos, sin])
  asin p@(D x x')  = D (asin x) ( x' / sqrt(1 - sqr p))
  acos p@(D x x')  = D (acos x) (-x' / sqrt(1 - sqr p))
  atan p@(D x x')  = D (atan x) ( x' / (sqr p - 1))

sqr :: Num a =&gt; a -&gt; a
sqr x = x*x
</code></pre>

<h3>Reintroducing the chain rule</h3>

<p>The code above, which corresponds to section 3 of <a href="http://citeseer.ist.psu.edu/karczmarczuk98functional.html" title="ICFP '98 paper &quot;Functional Differentiation of Computer Programs&quot; by Jerzy Karczmarczuk">Jerzy&#8217;s paper</a>, is fairly compact.
It can be made prettier, however, which is the point of this blog post.</p>

<p>First, let&#8217;s simplify the <code>lift</code> so that it expresses the chain rule directly.
In fact, this definition is just like <code>dlift</code> from Section 2 (not Section 3) of <a href="http://citeseer.ist.psu.edu/karczmarczuk98functional.html" title="ICFP '98 paper &quot;Functional Differentiation of Computer Programs&quot; by Jerzy Karczmarczuk">Jerzy&#8217;s paper</a>.
It&#8217;s the same code, but at a different type, here being used to manipulate infinite derivative towers instead of just value and derivative.</p>

<pre><code>dlift :: Num a =&gt; (a -&gt; a) -&gt; (Dif a -&gt; Dif a) -&gt; Dif a -&gt; Dif a
dlift f f' =  u@(D u0 u') -&gt; D (f u0) (f' u * u')
</code></pre>

<p>This operator lets us write simpler definitions.</p>

<pre><code>instance Floating a =&gt; Floating (Dif a) where
  pi    = dConst pi
  exp   = dlift exp exp
  log   = dlift log recip
  sqrt  = dlift sqrt (recip . (2*) . sqrt)
  sin   = dlift sin cos
  cos   = dlift cos (negate . sin)
  asin  = dlift asin ( x -&gt; recip (sqrt (1 - sqr x)))
  acos  = dlift acos ( x -&gt; - recip (sqrt (1 - sqr x)))
  atan  = dlift atan ( x -&gt; recip (sqr x + 1))
  sinh  = dlift sinh cosh
  cosh  = dlift cosh sinh
  asinh = dlift asinh ( x -&gt; recip (sqrt (sqr x + 1)))
  acosh = dlift acosh ( x -&gt; - recip (sqrt (sqr x - 1)))
  atanh = dlift atanh ( x -&gt; recip (1 - sqr x))
</code></pre>

<p>The necessary recursion has moved out of the lifting function into the class instance (second argument to <code>dlift</code>).</p>

<p>Notice that <code>dlift</code> and the <code>Floating</code> instance are the <em>same code</em> (with minor variations) as in Jerzy&#8217;s section two.
In that section, however, the code computes only first derivatives, while here, we&#8217;re computing all of them.</p>

<h3>Prettier still, with function-level overloading</h3>

<p>The last steps are cosmetic.
The goal is to make the derivative functions used with <code>lift</code> easier to read and write.</p>

<p>Just as we&#8217;ve overloaded numeric operations for derivative towers (<code>Dif</code>), let&#8217;s also overload them for <em>functions</em>.
This trick is often used informally in math.
For instance, given functions <code>f</code> and <code>g</code>, one might write <code>f + g</code> to mean <code>x -&gt; f x + g x</code>.
Using <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html" title="Documentation for Control.Applicative: applicative functors">applicative functor</a> notation makes these instances a breeze to define:</p>

<pre><code>instance Num b =&gt; Num (a-&gt;b) where
  fromInteger = pure . fromInteger
  (+)         = liftA2 (+)
  (*)         = liftA2 (*)
  negate      = fmap negate
  abs         = fmap abs
  signum      = fmap signum
</code></pre>

<p>The other numeric class instances are analogous.
(<em>Any</em> <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html" title="Documentation for Control.Applicative: applicative functors">applicative functor</a> can be given these same instance definitions.)</p>

<p>As a final touch, define an infix operator to replace the name &#8220;<code>dlift</code>&#8220;:</p>

<pre><code>infix 0 &gt;-&lt;
(&gt;-&lt;) = dlift
</code></pre>

<p>Now the complete code:</p>

<pre><code>instance Num a =&gt; Num (Dif a) where
  fromInteger               = dConst . fromInteger
  D x0 x' + D y0 y'         = D (x0 + y0) (x' + y')
  D x0 x' - D y0 y'         = D (x0 - y0) (x' - y')
  x@(D x0 x') * y@(D y0 y') = D (x0 * y0) (x' * y + x * y')

  negate = negate &gt;-&lt; -1
  abs    = abs    &gt;-&lt; signum
  signum = signum &gt;-&lt; 0

instance Fractional a =&gt; Fractional (Dif a) where
  fromRational = dConst . fromRational
  recip        = recip &gt;-&lt; - sqr recip

instance Floating a =&gt; Floating (Dif a) where
  pi    = dConst pi
  exp   = exp   &gt;-&lt; exp
  log   = log   &gt;-&lt; recip
  sqrt  = sqrt  &gt;-&lt; recip (2 * sqrt)
  sin   = sin   &gt;-&lt; cos
  cos   = cos   &gt;-&lt; - sin
  sinh  = sinh  &gt;-&lt; cosh
  cosh  = cosh  &gt;-&lt; sinh
  asin  = asin  &gt;-&lt; recip (sqrt (1-sqr))
  acos  = acos  &gt;-&lt; recip (- sqrt (1-sqr))
  atan  = atan  &gt;-&lt; recip (1+sqr)
  asinh = asinh &gt;-&lt; recip (sqrt (1+sqr))
  acosh = acosh &gt;-&lt; recip (- sqrt (sqr-1))
  atanh = atanh &gt;-&lt; recip (1-sqr)
</code></pre>

<p>The operators and literals on the right of the <code>(&gt;-&lt;)</code> are overloaded for the type <code>Dif a -&gt; Dif a</code>.
For instance, in the definition of <code>sqrt</code>,</p>

<pre><code>2     :: Dif a -&gt; Dif a
recip :: (Dif a -&gt; Dif a) -&gt; (Dif a -&gt; Dif a)
(*)   :: (Dif a -&gt; Dif a) -&gt; (Dif a -&gt; Dif a)
      -&gt; (Dif a -&gt; Dif a)
</code></pre>

<h3>Try it</h3>

<p>You can try out this code yourself.
Just grab the source files: <a href='http://conal.net/blog/wp-content/uploads/2008/05/NumInstances.hs'>NumInstances.hs</a> and <a href='http://conal.net/blog/wp-content/uploads/2008/05/Dif.hs'>Dif.hs</a>.
Enjoy!</p>
<p><a href="http://conal.net/blog/?flattrss_redirect&amp;id=43&amp;md5=75941b13bc061408cf92f7ed5b89141f"><img src="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png" srcset="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@2x.png 2xhttp://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@3x.png 3x" alt="Flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/beautiful-differentiation/feed</wfw:commentRss>
		<slash:comments>12</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=conal&amp;popout=1&amp;url=http%3A%2F%2Fconal.net%2Fblog%2Fposts%2Fbeautiful-differentiation&amp;language=en_GB&amp;category=text&amp;title=Beautiful+differentiation&amp;description=Lately+I%26%238217%3Bve+been+playing+again+with+parametric+surfaces+in+Haskell.+Surface+rendering+requires+normals%2C+which+can+be+constructed+from+partial+derivatives%2C+which+brings+up+automatic+differentiation+%28AD%29.+Playing+with+some...&amp;tags=applicative+functor%2Cbeautiful+code%2Cderivative%2Cblog" type="text/html" />
	</item>
	</channel>
</rss>
