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

<channel>
	<title>Conal Elliott</title>
	<atom:link href="http://conal.net/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://conal.net/blog</link>
	<description>Inspirations &#38; experiments, mainly about denotational programming in Haskell</description>
	<pubDate>Sun, 21 Jun 2009 17:57:58 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5</generator>
	<language>en</language>
			<item>
		<title>Memoizing polymorphic functions - part two</title>
		<link>http://conal.net/blog/posts/memoizing-polymorphic-functions-part-two/</link>
		<comments>http://conal.net/blog/posts/memoizing-polymorphic-functions-part-two/#comments</comments>
		<pubDate>Fri, 12 Jun 2009 22:04:01 +0000</pubDate>
		<dc:creator>conal</dc:creator>
		
		<category><![CDATA[Functional programming]]></category>

		<category><![CDATA[memoization]]></category>

		<category><![CDATA[polymorphism]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=89</guid>
		<description><![CDATA[





Part one of this series introduced the problem of memoizing functions involving polymorphic recursion.
The caching data structures used in memoization typically handle only one type of argument at a time.
For instance, one can have finite maps of differing types, but each concrete finite map holds just one type of key and one type of value.

I [...]]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Memoizing polymorphic functions - part two
Tags: memoization, polymorphism

URL: http://conal.net/blog/posts/memoizing-polymorphic-functions-part-two/

-->

<!-- references -->

<!-- teaser -->

<p><a href="http://conal.net/blog/posts/memoizing-polymorphic-functions-part-one/" title="Blog post">Part one</a> of this series introduced the problem of memoizing functions involving polymorphic recursion.
The caching data structures used in memoization typically handle only one <em>type</em> of argument at a time.
For instance, one can have finite maps of differing types, but each concrete finite map holds just one type of key and one type of value.</p>

<p>I extended memoization to handle polymorphic recursion by using an existential type together with a reified type of types.
This extension works (afaik), but it is restricted to a particular form for the type of the polymorphic function being memoized, namely</p>

<p><div>
<pre class="haskell"><span style="color: #5d478b; font-style: italic;">&#8211; Polymorphic function</span>
<span style="color: #050; font-weight: bold;">type</span> k :<span style="color: #5d478b; font-style: italic;">&#8211;&gt; v = forall a. HasType a =&gt; k a -&gt; v a</span></pre>
</div></p>

<p>My motivating example is a GADT-based representation of typed lambda calculus, and some of the functions I want to memoize do not fit the pattern.
After writing <a href="http://conal.net/blog/posts/memoizing-polymorphic-functions-part-one/" title="Blog post">part one</a>, I fooled around and found that I could transform these awkwardly typed polymorphic functions into isomorphic form that does indeed fit the restricted pattern of polymorphic types I can handle.</p>

<!--
**Edits**:

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

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

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

<h3>Awkward types</h3>

<p>The first awkwardly typed memoizee is the function application constructor:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> AT = <span style="color: #050; font-weight: bold;">forall</span> a b . <span style="color: green;">&#40;</span>HasType a, HasType b<span style="color: green;">&#41;</span> =&gt; E <span style="color: green;">&#40;</span>a -&gt; b<span style="color: green;">&#41;</span> -&gt; E a -&gt; E b
&nbsp;
<span style="color: green;">&#40;</span>:^<span style="color: green;">&#41;</span> :: AT</pre>
</div></p>

<p>Right away <code>AT</code> misses the required form.
It has two <code>HasType</code> constraints, and the first argument is parameterized over two type variables instead of one.
However, the second argument looks more promising, so let&#8217;s <code>flip</code> the arguments to get an isomorphic type:</p>

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

<p>And then move the quantifier and constraint on <code>b</code> inside the outer (first) <code>-&gt;</code>:</p>

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

<p>We&#8217;re getting closer.
Next, define a <code>newtype</code> wrapper.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">newtype</span> A2 a = A2 <span style="color: green;">&#40;</span><span style="color: #050; font-weight: bold;">forall</span> b. HasType b =&gt; E <span style="color: green;">&#40;</span>a -&gt; b<span style="color: green;">&#41;</span> -&gt; E b<span style="color: green;">&#41;</span></pre>
</div></p>

<p>So that <code>AT</code> is isomorphic to</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">forall</span> a . HasType a =&gt; E a -&gt; A2 a</pre>
</div></p>

<p>i.e.,</p>

<p><div>
<pre class="haskell">E :<span style="color: #5d478b; font-style: italic;">&#8211;&gt; A2</span></pre>
</div></p>

<p>The function inside of <code>A2</code> doesn&#8217;t have the required form, but another <code>newtype</code> wrapper finishes the job.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">newtype</span> EF a b = EF <span style="color: green;">&#123;</span>unEF :: E <span style="color: green;">&#40;</span>a -&gt; b<span style="color: green;">&#41;</span> <span style="color: green;">&#125;</span>
&nbsp;
<span style="color: #050; font-weight: bold;">type</span> H&#8217; a = EF a :<span style="color: #5d478b; font-style: italic;">&#8211;&gt; E</span>
&nbsp;
<span style="color: #050; font-weight: bold;">newtype</span> H a = H <span style="color: green;">&#123;</span> unH :: H&#8217; a <span style="color: green;">&#125;</span></pre>
</div></p>

<p>The <code>AT</code> type is isomorphic <code>AP</code> where</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> AP = E :<span style="color: #5d478b; font-style: italic;">&#8211;&gt; H</span></pre>
</div></p>

<h3>Curried memoization</h3>

<p>A &#8220;curried memo function&#8221; is one that takes one argument and produces another memo function.
For a simple memo function, not involving polymorphic recursion, there&#8217;s a simple recipe for curried memoization:</p>

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

<p>Our more polymorphic <code>memo</code> makes currying a little more awkward.
First, here&#8217;s a helper function for working <em>inside</em> of the representation of an <code>H</code>:</p>

<p><div>
<pre class="haskell">inH :: <span style="color: green;">&#40;</span>H&#8217; a -&gt; H&#8217; a<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</span>H a -&gt; H a<span style="color: green;">&#41;</span>
inH h z = H <span style="color: green;">&#40;</span>h <span style="color: green;">&#40;</span>unH z<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre>
</div></p>

<p>The following more elegant definition don&#8217;t type-check, due to the rank 2 polymorphism:</p>

<p><div>
<pre class="haskell">inH f = H . f . unH  <span style="color: #5d478b; font-style: italic;">&#8211; type error</span></pre>
</div></p>

<p>Now our <code>AP</code> memoizer is much like <code>memo2</code>:</p>

<p><div>
<pre class="haskell">memoAP :: AP -&gt; AP
memoAP <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:app"><span style="font-weight: bold;">app</span></a>&#8216; = memo <span style="color: green;">&#40;</span>inH memo . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:app"><span style="font-weight: bold;">app</span></a>&#8216;<span style="color: green;">&#41;</span></pre>
</div></p>

<p>(A more general, consistent type for <code>memoAP</code> is <code>(f :--&gt; H) -&gt; (f :--&gt; H)</code>.)</p>

<h3>Isomorphisms</h3>

<p>Now, to define the isomorphisms.  Define</p>

<p><div>
<pre class="haskell">toAP   :: AT -&gt; AP
fromAP :: AP -&gt; AT</pre>
</div></p>

<p>The definitions:</p>

<p><div>
<pre class="haskell">toAP <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:app"><span style="font-weight: bold;">app</span></a> ea = H $ \ <span style="color: green;">&#40;</span>EF eab<span style="color: green;">&#41;</span> -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:app"><span style="font-weight: bold;">app</span></a> eab ea
fromAP <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:app"><span style="font-weight: bold;">app</span></a>&#8216; eab ea = unH <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:app"><span style="font-weight: bold;">app</span></a>&#8216; ea<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>EF eab<span style="color: green;">&#41;</span></pre>
</div></p>

<p>If you erase the <code>newtype</code> wrappers &amp; unwrappers, you&#8217;ll see that <code>toAP</code> and <code>fromAP</code> are both just <code>flip</code>.</p>

<p>I constructed <code>fromAP</code> from the following specification:</p>

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

<p>Transforming step-by-step into equivalent specifications:</p>

<p><div>
<pre class="haskell">\ ea -&gt; H $ \ <span style="color: green;">&#40;</span>EF eab<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</span>fromAP <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:app"><span style="font-weight: bold;">app</span></a>&#8216;<span style="color: green;">&#41;</span> eab ea == <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:app"><span style="font-weight: bold;">app</span></a>&#8216;
&nbsp;
H $ \ <span style="color: green;">&#40;</span>EF eab<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</span>fromAP <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:app"><span style="font-weight: bold;">app</span></a>&#8216;<span style="color: green;">&#41;</span> eab ea == <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:app"><span style="font-weight: bold;">app</span></a>&#8216; ea
&nbsp;
\ <span style="color: green;">&#40;</span>EF eab<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</span>fromAP <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:app"><span style="font-weight: bold;">app</span></a>&#8216;<span style="color: green;">&#41;</span> eab ea == unH <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:app"><span style="font-weight: bold;">app</span></a>&#8216; ea<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: green;">&#40;</span>fromAP <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:app"><span style="font-weight: bold;">app</span></a>&#8216;<span style="color: green;">&#41;</span> eab ea == unH <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:app"><span style="font-weight: bold;">app</span></a>&#8216; ea<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>EF eab<span style="color: green;">&#41;</span>
&nbsp;
fromAP <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:app"><span style="font-weight: bold;">app</span></a>&#8216; eab ea == unH <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:app"><span style="font-weight: bold;">app</span></a>&#8216; ea<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>EF eab<span style="color: green;">&#41;</span></pre>
</div></p>

<h3>Memoizing vis isomorphisms</h3>

<p>Finally, I can memoize</p>

<p><div>
<pre class="haskell">memoAT :: AT -&gt; AT
memoAT <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:app"><span style="font-weight: bold;">app</span></a> = fromAP <span style="color: green;">&#40;</span>memoAP <span style="color: green;">&#40;</span>toAP <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:app"><span style="font-weight: bold;">app</span></a><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre>
</div></p>

<p>Again, a more elegant definition via <code>(.)</code> fails to type-check, due to rank 2 polymorphism.</p>

<p>The <code>Lam</code> (lambda abstraction) constructor can be handled similarly:</p>

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

<p>This time, no <code>flip</code> is needed.</p>

<h3>I wonder</h3>

<p>How far does this isomorphism trick go?</p>

<p>Is there an easier way to memoize polymorphic functions?</p>
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/memoizing-polymorphic-functions-part-two/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Memoizing polymorphic functions - part one</title>
		<link>http://conal.net/blog/posts/memoizing-polymorphic-functions-part-one/</link>
		<comments>http://conal.net/blog/posts/memoizing-polymorphic-functions-part-one/#comments</comments>
		<pubDate>Thu, 11 Jun 2009 00:36:34 +0000</pubDate>
		<dc:creator>conal</dc:creator>
		
		<category><![CDATA[Functional programming]]></category>

		<category><![CDATA[memoization]]></category>

		<category><![CDATA[polymorphism]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=88</guid>
		<description><![CDATA[





Memoization takes a function and gives back a semantically equivalent function that reuses rather than recomputes when applied to the same argument more than once.
Variations include not-quite-equivalence due to added strictness, and replacing value equality with pointer equality.

Memoization is often packaged up polymorphically:


memo :: &#40;???&#41; =&#62; &#40;k -&#62; v&#41; -&#62; &#40;k -&#62; v&#41;


For pointer-based (&#8221;lazy&#8221;) [...]]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Memoizing polymorphic functions - part one
Tags: memoization, polymorphism

URL: http://conal.net/blog/posts/memoizing-polymorphic-functions-part-one/

-->

<!-- references -->

<!-- teaser -->

<p>Memoization takes a function and gives back a semantically equivalent function that reuses rather than recomputes when applied to the same argument more than once.
Variations include <a href="http://conal.net/blog/posts/elegant-memoization-with-functional-memo-tries/#comment-10166">not-quite-equivalence due to added strictness</a>, and replacing value equality with pointer equality.</p>

<p>Memoization is often packaged up polymorphically:</p>

<p><div>
<pre class="haskell">memo :: <span style="color: green;">&#40;</span>???<span style="color: green;">&#41;</span> =&gt; <span style="color: green;">&#40;</span>k -&gt; v<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</span>k -&gt; v<span style="color: green;">&#41;</span></pre>
</div></p>

<p>For pointer-based (&#8221;lazy&#8221;) memoization, the type constraint (&#8221;???&#8221;) is empty.
For equality-based memoization, we&#8217;d need at least <code>Eq k</code>, and probably <code>Ord k</code> or <a href="http://conal.net/blog/posts/elegant-memoization-with-functional-memo-tries/" title="blog post"><code>HasTrie k</code></a> for efficient lookup (in a finite map or a possibly infinite <a href="http://conal.net/blog/tag/memoization/">memo trie</a>).</p>

<p>Although <code>memo</code> is polymorphic, its argument is a <em>monomorphic</em> function.
Implementations that use maps or tries exploit that monomorphism in that they use a type like <code>Map k v</code> or <code>Trie k v</code>.
Each map or trie is built around a particular (monomorphic) type of keys.
That is, a single map or trie does not mix keys of different types.</p>

<p>Now I find myself wanting to memoize <em>polymorphic</em> functions, and I don&#8217;t know how to do it.</p>

<!--
**Edits**:

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

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

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

<h3>Flavors of polymorphism</h3>

<p>If a recursively defined function <code>f</code> is polymorphic, then the recursion may still be monomorphic.
That is, recursive calls may be restricted to the same type instance as the parent call.
Most recursive polymorphic functions fit this form, because most polymorphic recursive data types are &#8220;regular&#8221;, meaning that a polymorphic data type is included in itself only at the same type instance.
For instance, the usual polymorphic lists and trees are regular.</p>

<h3>Example: GADTs</h3>

<p>Among other places, non-regular, or &#8220;<a href="ftp://ftp.kestrel.edu/pub/papers/meertens/nest5.ps">nested data types</a>&#8221; arise in statically typed encodings of typed languages.
For instance, here&#8217;s a GADT (generalized algebraic data type) for typed lambda calculus expressions:</p>

<p><div>
<pre class="haskell"><span style="color: #5d478b; font-style: italic;">&#8211; Variables</span>
<span style="color: #050; font-weight: bold;">data</span> V a = V <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:String"><span style="color: #833; font-weight: bold;">String</span></a> <span style="color: green;">&#40;</span>Type a<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">&#8211; Expressions</span>
<span style="color: #050; font-weight: bold;">data</span> E :: * -&gt; * <span style="color: #050; font-weight: bold;">where</span>
  Lit  :: a -&gt; E a                      <span style="color: #5d478b; font-style: italic;">&#8211; literal</span>
  Var  :: V  a -&gt; E a                   <span style="color: #5d478b; font-style: italic;">&#8211; variable</span>
  <span style="color: green;">&#40;</span>:^<span style="color: green;">&#41;</span> :: E <span style="color: green;">&#40;</span>a -&gt; b<span style="color: green;">&#41;</span> -&gt; E a -&gt; E b      <span style="color: #5d478b; font-style: italic;">&#8211; application</span>
  Lam  :: V a -&gt; E b -&gt; E <span style="color: green;">&#40;</span>a -&gt; b<span style="color: green;">&#41;</span>      <span style="color: #5d478b; font-style: italic;">&#8211; abstraction</span></pre>
</div></p>

<p>The <code>Type</code> type is sort of like <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Typeable.html#v:TypeRep"><code>TypeRep</code></a>, except that it is statically typed.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">data</span> Type :: * -&gt; * <span style="color: #050; font-weight: bold;">where</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Bool"><span style="color: #833; font-weight: bold;">Bool</span></a>   :: Type <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Bool"><span style="color: #833; font-weight: bold;">Bool</span></a>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Float"><span style="color: #833; font-weight: bold;">Float</span></a>  :: Type <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Float"><span style="color: #833; font-weight: bold;">Float</span></a>
  &#8230;
  <span style="color: green;">&#40;</span>:*:<span style="color: green;">&#41;</span>  :: Type a -&gt; Type b -&gt; Type <span style="color: green;">&#40;</span>a,b<span style="color: green;">&#41;</span>
  <span style="color: green;">&#40;</span>:-&gt;:<span style="color: green;">&#41;</span> :: Type a -&gt; Type b -&gt; Type <span style="color: green;">&#40;</span>a-&gt;b<span style="color: green;">&#41;</span></pre>
</div></p>

<p>These GADTs (<code>E</code> and <code>Type</code>) are both non-regular, and so recursive functions over them will involve more than one argument type.</p>

<p>So how can we memoize?</p>

<h3>A first try</h3>

<p>Let&#8217;s consider a specific case of polymorphic functions:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> k :<span style="color: #5d478b; font-style: italic;">&#8211;&gt; v = ∀ a. HasType a =&gt; k a -&gt; v a</span></pre>
</div></p>

<p><code>HasType</code> is to <code>Typeable</code> as <code>Type</code> is to <code>TypeRep</code>.
The <code>memo</code> implementation I&#8217;m playing with relies on <code>HasType</code>.</p>

<p>The memoizer can have type</p>

<p><div>
<pre class="haskell">memo :: <span style="color: green;">&#40;</span>k :<span style="color: #5d478b; font-style: italic;">&#8211;&gt; v) -&gt; (k :&#8211;&gt; v)</span></pre>
</div></p>

<p>which uses rank 2 polymorphism (because of the argument type&#8217;s <code>∀</code>, which,, cannot be moved to the outside).</p>

<p>My implementation of <code>memo</code> is similar to the discussion in <em><a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.34.1948" title="Paper by Simon Peyton Jones, Simon Marlow, and Conal Elliott">Stretching the storage manager: weak pointers and stable names in Haskell</a></em>, but modernized a bit and adapted for polymorphism.
It uses an <a href="http://hackage.haskell.org/packages/archive/containers/0.2.0.1/doc/html/Data-IntMap.html"><code>IntMap</code></a> of lists of pairs of stable names (akin to pointers) for arguments and values for results.
The idea is to first use <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/System-Mem-StableName.html#v:hashStableName"><code>hashStableName</code></a> to get an <code>Int</code> to use as an <code>IntMap</code> key, and then linearly traverse the resulting list of binding pairs, comparing stable keys for equality.
(<code>StableName</code> has <code>Eq</code> but not <code>Ord</code>.)
Although <code>hashStableName</code> can map different stable names to the same hash value, collisions are rare, so the <code>StableBind</code> lists rarely have more than one element and the linear search is cheap.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> SM k v = I.IntMap <span style="color: green;">&#91;</span>StableBind k v<span style="color: green;">&#93;</span>  <span style="color: #5d478b; font-style: italic;">&#8211; Stable map</span>
&nbsp;
<span style="color: #050; font-weight: bold;">data</span> StableBind k v = ∀ a. HasType a =&gt; SB <span style="color: green;">&#40;</span>StableName <span style="color: green;">&#40;</span>k a<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>v a<span style="color: green;">&#41;</span></pre>
</div></p>

<p>The reason for hiding the type parameter <code>a</code> in <code>StableName</code> is so that different bindings can be for different types.</p>

<p>The key tricky bit is managing static typing while searching for a particular <code>StableName a</code> in a <code>[StableBind]</code>.
Here&#8217;s my implementation:</p>

<p><div>
<pre class="haskell">blookup :: ∀ k v a. HasType a =&gt;
           StableName <span style="color: green;">&#40;</span>k a<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#91;</span>StableBind k v<span style="color: green;">&#93;</span> -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Maybe"><span style="color: #833; font-weight: bold;">Maybe</span></a> <span style="color: green;">&#40;</span>v a<span style="color: green;">&#41;</span>
blookup stk = look
 <span style="color: #050; font-weight: bold;">where</span>
   look :: <span style="color: green;">&#91;</span>StableBind k v<span style="color: green;">&#93;</span> -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Maybe"><span style="color: #833; font-weight: bold;">Maybe</span></a> <span style="color: green;">&#40;</span>v a<span style="color: green;">&#41;</span>
   look <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> = Nothing
   look <span style="color: green;">&#40;</span>SB stk&#8217; v : binds&#8217;<span style="color: green;">&#41;</span> 
     | Just Refl &lt;- tya `tyEq` typeOf2 stk&#8217;, stk == stk&#8217; = Just v
     | <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:otherwise"><span style="font-weight: bold;">otherwise</span></a>                                         = look binds&#8217;
   tya :: Type a
   tya = typeT</pre>
</div></p>

<p>The crucial magic bit is</p>

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

<p>where <code>a :=: b</code> represents a proof that the types <code>a</code> and <code>b</code> are the same type.
The proof type has a simple GADT representation:</p>

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

<p>This simple type definition ensures that only valid type-equality proofs can exist.
Well, except for ⊥.
The guard&#8217;s pattern match with <code>Just Refl</code> will force evaluation, so that ⊥ can&#8217;t sneak by us.
That match also informs the type-checker that <code>stk</code> and <code>stk'</code> are stable names <em>of the same type</em> in that clause, which then makes <code>stk == stk'</code> be well-typed, and makes <code>Just v</code> have the required type, i.e., <code>Maybe (v a)</code>.</p>

<p>Finally, the <code>typeOf2</code> function is a simple helper that peels off two type constructors and extracts a <code>Type</code>:</p>

<p><div>
<pre class="haskell">typeOf2 :: HasType a =&gt; g <span style="color: green;">&#40;</span>f a<span style="color: green;">&#41;</span> -&gt; Type a</pre>
</div></p>

<h3>Wishing for more</h3>

<p>The type of <code>memo</code> above is too restrictive for my uses.
It only handles polymorphic functions of type <code>k a -&gt; v a</code>, and only with the single constraint <code>HasType a</code>.</p>

<p>The reason I want lazy memoization now is that I&#8217;m compressing expressions to maximize representation sharing, as John Hughes described in <em><a href="http://www.cs.chalmers.se/~rjmh/Papers/hughes_85_lazy.pdf" title="Paper by John Hughes">Lazy Memo-functions</a></em>.
Once sharing is maximized, pointer-based memoization works better, because equal values are pointer-equal.
To compress an expression, simply use a memoized copy function, as John suggested.</p>

<p><div>
<pre class="haskell">compress :: HasType a =&gt; E a -&gt; E a
compress e = mcopy e
 <span style="color: #050; font-weight: bold;">where</span>
   mcopy, copy :: HasType b =&gt; E b -&gt; E b
   <span style="color: #5d478b; font-style: italic;">&#8211; Memo version</span>
   mcopy = memo copy
   <span style="color: #5d478b; font-style: italic;">&#8211; Copier, with memo-copied components</span>
   copy <span style="color: green;">&#40;</span>u :^ v<span style="color: green;">&#41;</span>  = appM <span style="color: green;">&#40;</span>mcopy u<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>mcopy v<span style="color: green;">&#41;</span>
   copy <span style="color: green;">&#40;</span>Lam v b<span style="color: green;">&#41;</span> = lamM v <span style="color: green;">&#40;</span>mcopy b<span style="color: green;">&#41;</span>
   copy e         = e
   <span style="color: #5d478b; font-style: italic;">&#8211; memoized constructors</span>
   appM :: <span style="color: green;">&#40;</span>HasType a, HasType b<span style="color: green;">&#41;</span> =&gt; E <span style="color: green;">&#40;</span>a -&gt; b<span style="color: green;">&#41;</span> -&gt; E a -&gt; E b
   appM = memo2 <span style="color: green;">&#40;</span>:^<span style="color: green;">&#41;</span>
   lamM :: <span style="color: green;">&#40;</span>HasType a, HasType b<span style="color: green;">&#41;</span> =&gt; V a -&gt; E b -&gt; E <span style="color: green;">&#40;</span>a -&gt; b<span style="color: green;">&#41;</span>
   lamM = memo2 Lam</pre>
</div></p>

<p>The <code>memo2</code> function is defined in terms of <code>memo</code>, using some some <code>newtype</code> trickery.
Its type:</p>

<p><div>
<pre class="haskell">memo2 :: HasType a =&gt;
         <span style="color: green;">&#40;</span>k a -&gt; l a -&gt; v a<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</span>k a -&gt; l a -&gt; v a<span style="color: green;">&#41;</span></pre>
</div></p>

<p>But, sigh, the <code>(:^)</code> and <code>Lam</code> constructors I&#8217;m trying to memoize do not have the required types.
My higher-order-polymorphic memo functions do not have flexible enough types.</p>

<p>And this is where I&#8217;m stuck.</p>

<p>I&#8217;d appreciate your ideas and suggestions.</p>
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/memoizing-polymorphic-functions-part-one/feed/</wfw:commentRss>
		</item>
		<item>
		<title>The C language is purely functional</title>
		<link>http://conal.net/blog/posts/the-c-language-is-purely-functional/</link>
		<comments>http://conal.net/blog/posts/the-c-language-is-purely-functional/#comments</comments>
		<pubDate>Wed, 20 May 2009 01:19:21 +0000</pubDate>
		<dc:creator>conal</dc:creator>
		
		<category><![CDATA[Functional programming]]></category>

		<category><![CDATA[history]]></category>

		<category><![CDATA[monad]]></category>

		<category><![CDATA[philosophy]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=87</guid>
		<description><![CDATA[





There has been a scurry of reaction on twitter and reddit to Robert Fischer&#8217;s post saying that Scala is Not a Functional Programming Language.
James Iry responded by saying that analogous reasoning leads to the conclusion that Erlang is Not Functional

My first inclination was to suggest that Haskell, as commonly practiced (with monadic IO), is not [...]]]></description>
			<content:encoded><![CDATA[<!-- 

Title: The C language is purely functional

Tags: philosophy, monad, history

URL: http://conal.net/blog/posts/the-c-language-is-purely-functional/

-->

<!-- references -->

<!-- teaser -->

<p>There has been a scurry of reaction on twitter and reddit to Robert Fischer&#8217;s post saying that <a href="http://www.reddit.com/goto?id=8kl8l">Scala is Not a Functional Programming Language</a>.
James Iry responded by saying that analogous reasoning leads to the conclusion that <a href="http://www.reddit.com/goto?id=8krbo">Erlang is Not Functional</a></p>

<p>My first inclination was to suggest that Haskell, as commonly practiced (with monadic IO), is not a functional language either.
Instead, I&#8217;m going to explain how it is that <em>the C language is purely functional</em>.</p>

<!--
**Edits**:

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

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

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

<h3>The story of C</h3>

<p>First, let me make that claim more precise.
What I really mean is that almost everyone who &#8220;writes C programs&#8221; is really programming in a purely functional language.
Modulo some minor historical accidents.</p>

<p>&#8220;C programmers&#8221; really program not in C, but in the purely functional language <em>cpp</em>.
As with any purely functional language, cpp consists only of (nestable) <em>expressions</em>, not statements.
Now here&#8217;s the clever part: when <em>evaluated</em> (not executed), a cpp expression yields a (pure) value of type <code>C</code>, which is an ADT (abstract type) that represents imperative programs.
That value of type <code>C</code> is then <em>executed</em> (not evaluated) by the cpp language&#8217;s RTS (run-time system).
As a clever optimization, cpp&#8217;s RTS actually includes a code generator, considerably improving performance over more naïve implementations of the <code>C</code> abstract data type.</p>

<p>(So now you can see that, &#8220;C programmer&#8221; is a almost always a misnomer when applied to a person.
Instead people are cpp programmers, and cpp programs are the real C programmers (producers of <code>C</code>).
I don&#8217;t think I can buck this misnomer, so I&#8217;ll stay with the herd and use &#8220;C programming&#8221; to mean cpp programming.)</p>

<p>In this way, the geniuses Brian Kernighan and Dennis Ritchie applied monads (from category theory) to programming language design considerably before the work of Moggi and Wadler.
(Even earlier examples of this idea can be found, so I&#8217;m taking liberty with history, choosing to give K&amp;R credit for the sake of this post.)</p>

<p>Now along the way, some mistakes were made that tarnished the theoretical beauty of cpp+C:</p>

<ul>
<li><p>Although cpp is in spirit purely functional, pragmatists insisted on unnecessary and the theoretically awkward constructs <code>#undef</code>, which allows a single name to be re-defined.</p></li>
<li><p>Scoping rules are not as clean as, say, the lambda calculus or Scheme,  (But hey &#8212; even John McCarthy got scoping wrong.)</p></li>
<li><p>The <code>C</code> ADT is implemented simply as <code>String</code> (or <code>char *</code>, for you type theorists, using a notation from Kleene), and the representation is <em>exposed</em> rather than hidden, so that cpp programs operate directly on strings.  The wisdom of data abstraction caught on later.</p></li>
</ul>

<p>Fortunately for purists, the relatively obscure programming language <a href="http://haskell.org">Haskell</a> restored the original untarnished beauty of K&amp;R&#8217;s vision by fixing these defects.
The type system was modernized, scoping rules improved, and the <code>C</code> type (renamed to &#8220;<code>IO</code>&#8220;, to avoid expensive legal battles) made abstract.</p>

<p>(Admittedly, Haskell also had some shameful pre-teen influences from hooligans like Church, Curry, Landin, and Milner, before it settled its true cpp+C nature, under the guiding influence of Wadler (under the influence of Moggi).)</p>

<h3>What about Haskell?</h3>

<p>Which leads to the question: is Haskell+IO a purely functional programming language?</p>

<p>Sure it is, even as much as its predecessor C (more precisely, cpp+C).</p>

<p>Some who ought to know better would claim that Haskell+IO is only <em>technically</em> pure.
(See <a href="http://www.reddit.com/r/programming/comments/8krbo/erlang_is_not_functional_response_to_scala_is_not/c09lqfi">claims and follow-up discussion</a>, in response to a remark from James Iry.)</p>

<p>I must then respond that, in that case, <em>even C programming</em> is only technically pure.</p>

<p>Which is absurd.</p>
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/the-c-language-is-purely-functional/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Notions of purity in Haskell</title>
		<link>http://conal.net/blog/posts/notions-of-purity-in-haskell/</link>
		<comments>http://conal.net/blog/posts/notions-of-purity-in-haskell/#comments</comments>
		<pubDate>Mon, 30 Mar 2009 19:00:48 +0000</pubDate>
		<dc:creator>conal</dc:creator>
		
		<category><![CDATA[Functional programming]]></category>

		<category><![CDATA[purity]]></category>

		<category><![CDATA[referential transparency]]></category>

		<category><![CDATA[semantics]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=86</guid>
		<description><![CDATA[





Lately I&#8217;ve been learning that some programming principles I treasure are not widely shared among my Haskell comrades.
Or at least not widely among those I&#8217;ve been hearing from.
I was feeling bummed, so I decided to write this post, in order to help me process the news and to see who resonates with what I&#8217;m looking [...]]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Notions of purity in Haskell

Tags: purity, referential transparency, semantics

URL: http://conal.net/blog/posts/notions-of-purity-in-haskell/

-->

<!-- references -->

<!-- teaser -->

<p>Lately I&#8217;ve been learning that some programming principles I treasure are not widely shared among my Haskell comrades.
Or at least not widely among those I&#8217;ve been hearing from.
I was feeling bummed, so I decided to write this post, in order to help me process the news and to see who resonates with what I&#8217;m looking for.</p>

<p>One of the principles I&#8217;m talking about is that the value of a closed expression (one not containing free variables) depends solely on the expression itself &#8212; not influenced by the dynamic conditions under which it is executed.
I relate to this principle as the soul of functional programming and of referential transparency in particular.</p>

<!--
**Edits**:

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

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

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

<p>Recently I encountered two facts about standard Haskell libraries that I have trouble reconciling with this principle.</p>

<ul>
<li>The meaning of <code>Int</code> operations in overflow situations is machine-dependent.  Typically they use 32 bits when running on 32-bit machine and 64 bits when running on 64-bit machines.  Implementations are free to use as few as 29 bits.  Thus the value of the expression &#8220;<code>2^32 == (0 ::Int)</code>&#8221; may be either <code>False</code> or <code>True</code>, depending on the dynamic conditions which it is evaluated.</li>
<li>The expression &#8220;<code>System.Info.os</code>&#8221; has type <code>String</code>, although its value as a sequence of characters depends on the circumstances of its execution.  (Similarly for the other exports from <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/System-Info.html"><code>System.Info</code></a>.  Hm.  I just noticed that the module is labeled as &#8220;portable&#8221;.  Typo?  Joke?)</li>
</ul>

<p>Although I&#8217;ve been programming primarily in Haskell since around 1995, I didn&#8217;t realize that these implementation-dependent meanings were there.
As in many romantic relationships, I suppose I&#8217;ve been seeing Haskell not as she is, but as I idealized her to be.</p>

<p>There&#8217;s another principle that is closely related to the one above and even more fundamental to me: every type has a precise, specific, and preferably simple denotation.
If an expression <code>e</code> has type <code>T</code>, then the meaning (value) of <code>e</code> is a member of the collection denoted by <code>T</code>.
For instance, I think of the meaning of the type <code>String</code>, i.e., of <code>[Char]</code>, as being sequences of characters.
Well, not quite that simple, because it also contains some partially defined sequences and has a partial information ordering (non-flat in this case).
Given this second principle, if <code>os :: String</code>, then the meaning of <code>os</code> is some sequence of characters.
Assuming the sequence is finite and non-partial, it can be written down as a literal string, and that literal can be substituted for every occurrence of &#8220;<code>os</code>&#8221; in a program, without changing the program&#8217;s meaning.
However, <code>os</code> evaluates to &#8220;linux&#8221; on my machine and evaluates to &#8220;darwin&#8221; on my friend Bob&#8217;s machine, so substituting <em>any</em> literal string for &#8220;<code>os</code>&#8221; would change the meaning, as observable on at least one of these machines.</p>

<p>Now I realize I&#8217;m really talking about standard Haskell <em>libraries</em>, not Haskell itself.
When I <a href="http://tunes.org/~nef/logs/haskell/09.03.29">discussed my confusion &amp; dismay in the #haskell chat room</a>, someone suggested explaining these semantic differences in terms of different libraries and hence different programs (if one takes programs to include the libraries they use).
One would not expect different programs (due to different libraries) to have the same meaning.</p>

<p>I understand this different-library perspective &#8212; in a literal way.
And yet I&#8217;m not really satisfied.
What I get is that standard libraries are &#8220;standard&#8221; in signature (form), not in meaning (substance).
With no promises about semantic commonality, I don&#8217;t know how standard libraries can be useful.</p>

<p>Another perspective that came up on #haskell was that the kind of semantic consistency I&#8217;m looking for is <em>impossible</em>, because of possibilities of failure.
For instance, evaluating an expression might one time fail due to memory exhaustion, while succeeding (perhaps just barely) on another attempt.
After mulling over that point, I&#8217;d like to weaken my principle a little.
Instead of asking that all evaluations of an expression yield <em>same</em> value, I ask that all evaluations of an expression yield <em>consistent</em> answers.
By &#8220;consistent&#8221; I mean in the sense of information content.
<em>Answers don&#8217;t have to agree, but they must not disagree.</em>
Failures like exhausted memory are modeled as ⊥, which is called &#8220;bottom&#8221; because it is the bottom of the information partial ordering.
It contains no information and so is consistent with every value, disagreeing with no value.
More precisely, values are <em>consistent</em> when they have a shared upper (information) bound, and <em>inconsistent</em> when they don&#8217;t.
The value ⊥ means <em>i-don&#8217;t-know</em>, and the value <code>(1,⊥,3)</code> means (1, <em>i-don&#8217;t-know</em>, 3).
The consistent-value principle accepts possible failures due to finite resources and hardware failure, while rejecting &#8220;linux&#8221; vs &#8220;darwin&#8221; for <code>System.Info.os</code> or False vs True for &#8220;<code>2^32 == (0 ::Int)</code>.
It also accepts <code>System.Info.os :: IO String</code>, which is the type I would have expected, because the semantics of <code>IO String</code> is big enough to accommodate dependence on dynamic conditions.</p>

<p>If you also cherish the principles I mention above, I&#8217;d love to hear from you.</p>
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/notions-of-purity-in-haskell/feed/</wfw:commentRss>
		</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>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 morphism.

I&#8217;d love to [...]]]></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
-->
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/paper-beautiful-differentiation/feed/</wfw:commentRss>
		</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>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 appreciated, as well [...]]]></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
-->
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/denotational-design-with-type-class-morphisms/feed/</wfw:commentRss>
		</item>
		<item>
		<title>From the chain rule to automatic differentiation</title>
		<link>http://conal.net/blog/posts/from-the-chain-rule-to-automatic-differentiation/</link>
		<comments>http://conal.net/blog/posts/from-the-chain-rule-to-automatic-differentiation/#comments</comments>
		<pubDate>Sun, 08 Feb 2009 22:14:12 +0000</pubDate>
		<dc:creator>conal</dc:creator>
		
		<category><![CDATA[Functional programming]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=82</guid>
		<description><![CDATA[





In What is automatic differentiation, and why does it work?, I gave a semantic model that explains what automatic differentiation (AD) accomplishes.
Correct implementations then flowed from that model, by applying the principle of type class morphisms.
(An instance&#8217;s interpretation is the interpretation&#8217;s instance).

I&#8217;ve had a nagging discomfort about the role of the chain rule in AD, [...]]]></description>
			<content:encoded><![CDATA[<!-- 

Title: From the chain rule to automatic differentiation

Tags: derivative, semantics, type class morphism, rewriting

URL: http://conal.net/blog/posts/from-the-chain-rule-to-automatic-differentiation/

-->

<!-- references -->

<!-- teaser -->

<p>In <em><a href="http://conal.net/blog/posts/what-is-automatic-differentiation-and-why-does-it-work/" title="blog post">What is automatic differentiation, and why does it work?</a></em>, I gave a semantic model that explains what automatic differentiation (AD) accomplishes.
Correct implementations then flowed from that model, by applying the principle of <a href="http://conal.net/blog/tag/type-class-morphism/" title="Posts on type class morphisms">type class morphism</a>s.
(An instance&#8217;s interpretation is the interpretation&#8217;s instance).</p>

<p>I&#8217;ve had a nagging discomfort about the role of the chain rule in AD, with an intuition that the chain rule can carry a more central role the the specification and implementation.
This post gives a variation on the <a href="http://conal.net/blog/posts/what-is-automatic-differentiation-and-why-does-it-work/" title="blog post">previous AD post</a> that carries the chain rule further into the reasining and implementation, leading to simpler correctness proofs and a nearly unaltered implementation.</p>

<p>Finally, as a bonus, I&#8217;ll show how GHC rewrite rules enable an even simpler and more modular implementation.</p>

<p>I&#8217;ve included some optional content, including exercises.
You can see my answers to the exercises by examining the HTML.</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-82"></span></p>

<p>As before, I&#8217;ll start with a limited form of differentiation that works for functions of a scalar (1D) domain, where one can identify derivative values with regular values:</p>

<p><div>
<pre class="haskell">deriv :: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Num"><span style="color: #833; font-weight: bold;">Num</span></a> a =&gt; <span style="color: green;">&#40;</span>a -&gt; a<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</span>a -&gt; a<span style="color: green;">&#41;</span> <span style="color: #5d478b; font-style: italic;">&#8211;  simplification</span></pre>
</div></p>

<p>The development below extends to higher-order derivatives and higher-dimensional domains.</p>

<h3>The chain rule</h3>

<p>At the heart of AD is the chain rule:</p>

<p><div>
<pre class="haskell">deriv <span style="color: green;">&#40;</span>g . f<span style="color: green;">&#41;</span> x == deriv g <span style="color: green;">&#40;</span>f x<span style="color: green;">&#41;</span> * deriv f x</pre>
</div></p>

<p>Equivalently,</p>

<p><div>
<pre class="haskell">deriv <span style="color: green;">&#40;</span>g . f<span style="color: green;">&#41;</span> == <span style="color: green;">&#40;</span>deriv g . f<span style="color: green;">&#41;</span> * deriv f</pre>
</div></p>

<p>where this <code>(*)</code> is on functions: <code>(*) = liftA2 (*) == \ h k x -&gt; h x * k x</code>.</p>

<p>The traditional forward AD formulation is based on the chain rule but is not as symmetric as the chain rule.
In the function compositions <code>g . f</code> considered, <code>g</code> is always simple, while <code>f</code> may be arbitrarily complex.
(I think the reverse true for reverse-mode AD.)
What might we find if we delay introducing this asymmetry?</p>

<h3>A direct implementation of the chain rule</h3>

<p>The chain rule applies to functions and their derivatives, so let&#8217;s formulate a direct implementation.
Start with a type for holding two functions:</p>

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

<p><code>FD</code> is used to hold functions and their derivatives:</p>

<p><div>
<pre class="haskell">toFD :: <span style="color: green;">&#40;</span>a -&gt; a<span style="color: green;">&#41;</span> -&gt; FD a
toFD f = FD f <span style="color: green;">&#40;</span>deriv f<span style="color: green;">&#41;</span></pre>
</div></p>

<p>We do not have an implementation of <code>deriv</code>, so <code>toFD</code> here is part of the specification only, not the implementation.</p>

<p>Now we can specify a composition operator on <code>FD</code>:</p>

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

<p>We&#8217;ll want <code>(~.~)</code> to represent composition of functions, where we have access to the derivatives as well.
That is, <code>(~.~)</code> must satisfy:</p>

<p><div>
<pre class="haskell">toFD <span style="color: green;">&#40;</span>g . f<span style="color: green;">&#41;</span> == toFD g ~.~ toFD f</pre>
</div></p>

<p>The implementation and its correctness follow from the chain rule:</p>

<p><div>
<pre class="haskell">FD g g' ~.~ FD f f' = FD <span style="color: green;">&#40;</span>g . f<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>g&#8217; . f<span style="color: green;">&#41;</span> * f&#8217;<span style="color: green;">&#41;</span></pre>
</div></p>

<div class=exercise>

<p><strong>Exercise:</strong> Fill in the proof that <code>(~.~)</code> satisfies its specification.</p>

</div>

<div class=hidden>

       toFD (g . f)
    ==   {- def of toFD  -}
       FD (g . f) (deriv (g . f))
    ==   {- chain rule -}
       FD (g . f) ((deriv g . f) * deriv f)
    ==   {- def of (~.~) -}
       FD g (deriv g) ~.~ FD f (deriv f)
    ==   {- def of toFD -}
       toFD g ~.~ toFD f

</div>

<p><em>(Exercise solutions are in the post&#8217;s HTML.)</em></p>

<h3>From function to values</h3>

<p>The <code>FD</code> type and its composition operator implement the chain rule quite directly.
However, they are not suitable for AD, which operates on the <em>values</em> (range) of a function and of its derivative.</p>

<p>Let&#8217;s start over with the usual AD value representation:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">data</span> D a = D a a</pre>
</div></p>

<p><a href="http://conal.net/blog/posts/what-is-automatic-differentiation-and-why-does-it-work/" title="blog post">Previously</a>, I defined this ideal construction function:</p>

<p><div>
<pre class="haskell">toD :: <span style="color: green;">&#40;</span>a -&gt; a<span style="color: green;">&#41;</span> -&gt; a -&gt; D a
toD f x = D <span style="color: green;">&#40;</span>f x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>deriv f x<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">&#8211; or</span>
toD == <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:liftA2"><span style="font-weight: bold;">liftA2</span></a> D f <span style="color: green;">&#40;</span>deriv f<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Instead, let&#8217;s now define <code>toD</code> in terms of <code>toFD</code>.
First, how do the <code>FD</code> and <code>D</code> representations relate?</p>

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

<p>Then we can define <code>toD</code>:</p>

<p><div>
<pre class="haskell">toD = fdAt . toFD</pre>
</div></p>

<div class=exercise>

<p><strong>Exercise:</strong> Show that these definitions of <code>toD</code> are equivalent.</p>

</div>

<div class=hidden>
Expanding,

       toD f 
    ==
       fdAt (toFD f)
    ==
       fdAt (FD f (deriv f))
    ==
       liftA2 D f (deriv f)
    ==
       \ x -> D (f x) (deriv f x)

</div>

<p>Again, <code>toD</code> isn&#8217;t executable with this definition, because <code>toFD</code> isn&#8217;t (because <code>deriv</code> isn&#8217;t).
As before, <code>toD</code> must be eliminated in our journey from specification to implementation.</p>

<div class=exercise>

<p><strong>Optional:</strong>
We can also define an odd sort of inverse for <code>toD</code>:</p>

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

<p><code>fromD</code> must satisfy, for all <code>x</code>,</p>

<p><div>
<pre class="haskell">toD <span style="color: green;">&#40;</span>fromD d x<span style="color: green;">&#41;</span> x == d</pre>
</div></p>

<p>It&#8217;s more convenient to relate flipped version of <code>toD</code> and <code>fromD</code>:</p>

<p><div>
<pre class="haskell">toD'   :: a -&gt; <span style="color: green;">&#40;</span>a -&gt; a<span style="color: green;">&#41;</span> -&gt;   D a
fromD&#8217; :: a -&gt;    D a   -&gt; <span style="color: green;">&#40;</span>a -&gt; a<span style="color: green;">&#41;</span>
&nbsp;
toD&#8217;   = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:flip"><span style="font-weight: bold;">flip</span></a> toD
fromD&#8217; = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:flip"><span style="font-weight: bold;">flip</span></a> fromD</pre>
</div></p>

<p>Then <code>fromD</code> must satisfy, for all <code>x</code>,</p>

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

<div class=exercise>

<p><strong>Exercise:</strong> Give a simple definition for <code>fromD</code> and show that it&#8217;s correct (satisfies its specification).</p>

</div>

<div class=hidden>
Define

    fromD (D a a&#8217;) x = \ t -> a + a&#8217; * (t - x)

Then

       toD (fromD (D a a&#8217;) x) x
    ==
       D (fromD (D a a&#8217;) x) (deriv (fromD (D a a&#8217;)) x)
    ==
       D ((\ t -> a + a&#8217; * (t - x)) x) (deriv (\ t -> a + a&#8217; * (t - x)) x)
    ==
       D a (deriv (\ t -> a + a&#8217; * (t - x)) x)
    ==
       D a ((\ t -> a&#8217;) x)
    ==
       D a a&#8217;

</div>

</div>

<h3>A general, value-friendly chain rule</h3>

<p>In <em><a href="http://conal.net/blog/posts/what-is-automatic-differentiation-and-why-does-it-work/" title="blog post">What is AD &#8230;?</a></em>, I defined correctness of the numeric class instances <code>D</code> by saying that <code>toD</code> must be a <a href="http://conal.net/blog/tag/type-class-morphism/" title="Posts on type class morphisms">type class morphism</a> for each of the numeric classes it implements.
For example, let&#8217;s take the <code>sin</code> method.
The other unary methods will work just like it.
The morphism property:</p>

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

<p>Because of numeric overloading on functions, this property is equivalent to a more explicit one:</p>

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

<p>The <code>sin</code> on the left is on numbers, and the <code>sin</code> on the right is on <code>D a</code>.</p>

<p>Let&#8217;s suppose we have a function <code>adiff</code> (for automatic differentiation) such that for all <code>g</code> and <code>f</code>,</p>

<p><div>
<pre class="haskell">toD <span style="color: green;">&#40;</span>g . f<span style="color: green;">&#41;</span> == adiff g . toD f      <span style="color: #5d478b; font-style: italic;">&#8211; specification of adiff</span>
&nbsp;
adiff :: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Num"><span style="color: #833; font-weight: bold;">Num</span></a> =&gt; <span style="color: green;">&#40;</span>a -&gt; a<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</span>D a -&gt; D a<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Then our goal would become</p>

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

<p>and a correct definition of <code>sin</code> would be immediate, as would be the other definitions:</p>

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

<p>Note that the <code>adiff</code> specification above implies that for all <code>g</code>,</p>

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

<div class=exercise>

<p><strong>Exercise:</strong> Show that a necessary and sufficient definition for <code>adiff</code> satisfying its specification is</p>

<p><div>
<pre class="haskell">adiff g <span style="color: green;">&#40;</span>D a a&#8217;<span style="color: green;">&#41;</span> = D <span style="color: green;">&#40;</span>g a<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>deriv g a * a&#8217;<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Derive this definition of <code>adiff</code> from its specification.</p>

</div>

<div class=hidden>
Proof details:
First, sufficient.
LHS:

       toD (g . f)
    ==   {- toD def  -}
       liftA2 D (g . f) (deriv (g . f))
    ==   {- chain rule  -}
       liftA2 D (g . f) ((deriv g . f) * deriv f)
    ==   {- liftA2 on functions  -}
       \ x -> D (g (f x)) (deriv g (f x) * deriv f x)

RHS:

       adiff g . toD f
    ==   {- toD def  -}
       adiff g . \ x -> D (f x) (deriv f x)
    ==   {- (.) def  -}
       \ x -> adiff g (D (f x) (deriv f x))

The definition then falls out mechanically (with [second order pattern matching][]).

Next, necessary.

       adiff g dd@(D a a&#8217;)
    ==   {- toD/fromD  -}
       adiff g (toD (fromD dd x) x)
    ==   {- adiff spec  -}
       toD (g . fromD dd x) x
    ==   {- toD def  -}
       D (g (fromD dd x x)) (deriv (g . fromD dd x) x)
    ==   {- chain rule  -}
       D (g (fromD dd x x)) (deriv g (fromD dd x x) * deriv (fromD dd x) x)
    ==   {- fromD spec  -}
       D (g a) (deriv g a * a&#8217;)

</div>

<p>The <code>adiff</code> function satisfies a more symmetric property as well.
It distributes over composition:</p>

<p><div>
<pre class="haskell">adiff <span style="color: green;">&#40;</span>h . g<span style="color: green;">&#41;</span> == adiff h . adiff g</pre>
</div></p>

<div class=exercise>

<p><strong>Exercise:</strong> Prove it this property from the specification.</p>

</div>

<div class=hidden>
Here&#8217;s a proof from the definition.
Start from the right-hand side:

       (adiff h . adiff g) (D a a&#8217;)
    ==   {- (.) def  -}
       adiff h (adiff g (D a a&#8217;))
    ==   {- adiff def  -}
       adiff h (D (g a) (deriv g a * a&#8217;))
    ==   {- adiff def  -}
       D (h (g a)) (deriv h (g a) * (deriv g a * a&#8217;))
    ==   {- associativity of (*)  -}
       D (h (g a)) ((deriv h (g a) * deriv g a) * a&#8217;)
    ==   {- chain rule -}
       D (h (g a)) (deriv (h . g) a * a&#8217;)
    ==   {- (.) def -}
       D ((h . g) a) (deriv (h . g) a * a&#8217;)
    ==   {- adiff def -}
       adiff (h . g) (D a a&#8217;)

Alternatively, prove from the specification alone:

For all `h`, `g`, `f`,

       adiff (h . g) . toD f
    ==   {- adiff spec -}
       toD ((h . g) . f)
    ==   {- (.) associativity -}
       toD (h . (g . f))
    ==   {- adiff spec -}
       adiff h . toD (g . f)
    ==   {- adiff spec -}
       adiff h . (adiff g . toD f)
    ==   {- (.) associativity -}
       (adiff h . adiff g) . toD f

So `adiff (h . g)` and `(adiff h . adiff g)` agree on `toD f x` for all `f` and `x`.
Given an arbitrary `D` value `dd`, choose an arbitrary `x`.
Then

       adiff (h . g) dd
    ==
       adiff (h . g) (toD (fromD dd x) x)
    ==
       (adiff h . adiff g) (toD (fromD dd x) x)
    ==
       (adiff h . adiff g) dd

</div>

<p>Moreover, <code>adiff</code> maps the identity to the identity: <code>adiff id = id</code>.</p>

<div class=exercise>

<p><strong>Exercise:</strong> Show that for <em>any</em> definition of <code>adiff</code>, if for all <code>g</code>,</p>

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

<p>and if for all <code>h</code> and <code>g</code></p>

<p><div>
<pre class="haskell">adiff <span style="color: green;">&#40;</span>h . g<span style="color: green;">&#41;</span> == adiff h . adiff g</pre>
</div></p>

<p>then our <code>adiff</code> specification holds, i.e., for <code>g</code> and <code>f</code>,</p>

<p><div>
<pre class="haskell">adiff g . toD f == toD <span style="color: green;">&#40;</span>g . f<span style="color: green;">&#41;</span></pre>
</div></p>

</div>

<div class=hidden>

       toD (g . f)
    ==   {- first assumption  -}
       adiff (g . f) . toD id
    ==   {- second assumption  -}
       (adiff g . adiff f) . toD id
    ==   {- associativity of (.)  -}
       adiff g . (adiff f . toD id)
    ==   {- first assumption  -}
       adiff g . toD f

</div>

<h3>Back to an implementation</h3>

<p>We&#8217;re still not quite done, since <code>adiff</code> depends on <code>deriv</code>, which doesn&#8217;t have an implementation.
Let&#8217;s separate out the problematic <code>deriv</code> by refactoring <code>adiff</code>:</p>

<p><div>
<pre class="haskell">adiff g = g &gt;-&lt; deriv g</pre>
</div></p>

<p>where</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">infix</span>  <span style="color: red;">0</span> &gt;-&lt;
<span style="color: green;">&#40;</span>&gt;-&lt;<span style="color: green;">&#41;</span> :: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Num"><span style="color: #833; font-weight: bold;">Num</span></a> a =&gt; <span style="color: green;">&#40;</span>a -&gt; a<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</span>a -&gt; a<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</span>D a -&gt; D a<span style="color: green;">&#41;</span>
<span style="color: green;">&#40;</span>g &gt;-&lt; g&#8217;<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>D a a&#8217;<span style="color: green;">&#41;</span> = D <span style="color: green;">&#40;</span>g a<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>g&#8217; a * a&#8217;<span style="color: green;">&#41;</span></pre>
</div></p>

<p>After inlining this definition of <code>adiff</code>, the method definitions are</p>

<p><div>
<pre class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sin"><span style="font-weight: bold;">sin</span></a>  = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sin"><span style="font-weight: bold;">sin</span></a>  &gt;-&lt; deriv <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sin"><span style="font-weight: bold;">sin</span></a>
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sqrt"><span style="font-weight: bold;">sqrt</span></a> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sqrt"><span style="font-weight: bold;">sqrt</span></a> &gt;-&lt; deriv <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sqrt"><span style="font-weight: bold;">sqrt</span></a>
&#8230;</pre>
</div></p>

<p>Every remaining use of <code>deriv</code> is applied to a function whose derivative is known, so we can replace each use.</p>

<p><div>
<pre class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sin"><span style="font-weight: bold;">sin</span></a>  = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sin"><span style="font-weight: bold;">sin</span></a>  &gt;-&lt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:cos"><span style="font-weight: bold;">cos</span></a>
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sqrt"><span style="font-weight: bold;">sqrt</span></a> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sqrt"><span style="font-weight: bold;">sqrt</span></a> &gt;-&lt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:recip"><span style="font-weight: bold;">recip</span></a> <span style="color: green;">&#40;</span><span style="color: red;">2</span> * <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sqrt"><span style="font-weight: bold;">sqrt</span></a><span style="color: green;">&#41;</span>
&#8230;</pre>
</div></p>

<p>Now we have an executable implementation again.
These method definitions and the definition of <code>(&gt;-&lt;)</code> are exactly as in <em><a href="http://conal.net/blog/posts/what-is-automatic-differentiation-and-why-does-it-work/" title="blog post">What is automatic differentiation, and why does it work?</a></em>.</p>

<h3>Fun with rules</h3>

<p>Let&#8217;s back up to our more elegant method definitions using <code>adiff</code>:</p>

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

<p>We made these definitions executable in spite of their appeal to the non-executable <code>deriv</code> by (a) refactoring <code>adiff</code> to split the <code>deriv</code> from the residual function <code>(&gt;-&lt;)</code>, (b)
inlining <code>adiff</code>, and (c) rewriting applications of <code>deriv</code> with known derivative rules.</p>

<p>Now let&#8217;s get GHC to do these steps for us.</p>

<p>List the derivatives of known functions:</p>

<p><div>
<pre class="haskell"><span style="color: #5d478b; font-style: italic;">{-# RULES
&nbsp;
&quot;deriv negate&quot;  deriv negate  =  -1
&quot;deriv abs&quot;     deriv abs     =  signum
&quot;deriv signum&quot;  deriv signum  =  0
&quot;deriv recip&quot;   deriv recip   =  - sqr recip
&quot;deriv exp&quot;     deriv exp     =  exp
&quot;deriv log&quot;     deriv log     =  recip
&quot;deriv sqrt&quot;    deriv sqrt    =  recip (2 * sqrt)
&quot;deriv sin&quot;     deriv sin     =  cos
&quot;deriv cos&quot;     deriv cos     =  - sin
&quot;deriv asin&quot;    deriv asin    =  recip (sqrt (1-sqr))
&quot;deriv acos&quot;    deriv acos    =  recip (- sqrt (1-sqr))
&quot;deriv atan&quot;    deriv atan    =  recip (1+sqr)
&quot;deriv sinh&quot;    deriv sinh    =  cosh
&quot;deriv cosh&quot;    deriv cosh    =  sinh
&quot;deriv asinh&quot;   deriv asinh   =  recip (sqrt (1+sqr))
&quot;deriv acosh&quot;   deriv acosh   =  recip (- sqrt (sqr-1))
&quot;deriv atanh&quot;   deriv atanh   =  recip (1-sqr)
&nbsp;
 #-}</span></pre>
</div></p>

<p>Notice that these definitions are simpler and more modular than the standard differentiation rules, as they do not have the chain rule mixed in.
For instance, compare (a) <code>deriv sin = cos</code>, (b) <code>deriv (sin u) == cos u * deriv u</code>, and (c) <code>deriv (sin u) x == cos u x * deriv u x</code>.</p>

<p>Now we can use the incredibly simple <code>adiff</code>-based definitions of our methods, e.g., <code>asin = adiff asin</code>.</p>

<p>The definition of <code>adiff</code> must get inlined so as to reveal the <code>deriv</code> applications, which then get rewritten according to the rules.
Fortunately, the <code>adiff</code> definition is tiny, which encourages its inlining.
We could add an <code>INLINE</code> pragma as a reminder.
GHC requires that a definition must be given <code>deriv</code>, even it all uses are rewritten away, so use the following</p>

<p><div>
<pre class="haskell">deriv = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:error"><span style="font-weight: bold;">error</span></a> <span style="color: green;">&quot;deriv: undefined.  Missing rewrite rule?&quot;</span></pre>
</div></p>
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/from-the-chain-rule-to-automatic-differentiation/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Seeking advice on software licensing</title>
		<link>http://conal.net/blog/posts/seeking-advice-on-software-licensing/</link>
		<comments>http://conal.net/blog/posts/seeking-advice-on-software-licensing/#comments</comments>
		<pubDate>Wed, 04 Feb 2009 00:47:31 +0000</pubDate>
		<dc:creator>conal</dc:creator>
		
		<category><![CDATA[Functional programming]]></category>

		<category><![CDATA[license]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=80</guid>
		<description><![CDATA[





Thanks to my time at Microsoft Research (1994-2002), I was able to live for a while (modestly) on asset growth.
Last year I started working in earnest on two libraries, Reactive (functional reactive programming) and FieldTrip (functional 3D), plus some supporting libraries.
I placed these libraries all under the most liberal license I know, which is BSD3.
I&#8217;m [...]]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Seeking advice on software licensing

Tags: license

URL: http://conal.net/blog/posts/seeking-advice-on-software-licensing/

-->

<!-- references -->

<!-- teaser -->

<p>Thanks to my time at Microsoft Research (1994-2002), I was able to live for a while (modestly) on asset growth.
Last year I started working in earnest on two libraries, <a href="http://haskell.org/haskellwiki/reactive" title="wiki page">Reactive</a> (functional reactive programming) and <a href="">FieldTrip</a> (functional 3D), plus some supporting libraries.
I placed these libraries all under the most liberal license I know, which is BSD3.
I&#8217;m more enthusiastic than ever about my functional programming work and am enjoying active involvement with the Haskell community.</p>

<p>With the recent economic meltdown, my old means of sustainable income ended, and now I&#8217;m looking for a replacement.
I&#8217;m not yet in crisis, so I have time to make some thoughtful decisions and even take some risk.
Rather than abandoning Reactive, FieldTrip, and related projects (some new), I&#8217;m looking for ways to continue these projects while building potential for future income related to them.
At the same time, it&#8217;s very important to me to keep these projects open so as to advance purely functional techniques &amp; tools, as well as to have enjoyable creative connections, and to get feedback &amp; help.</p>

<p>For these reasons, I&#8217;m now considering licensing future releases of some my libraries for <em>non-commercial</em> use, with commercial uses to be arranged as separate licenses.
I know almost nothing about licensing issues, because I haven&#8217;t been interested, and I&#8217;ve always wanted maximum freedom for all users.</p>

<p>So, I&#8217;m looking for help in choosing a software license that enables &amp; encourages a creative community, while preserving opportunity to ask for some portion of return in future for-profit uses.
If people have alternative perspectives how to achieve my goals <em>without</em> changing license terms, I&#8217;m interested in hearing them as well.
I am not trying to &#8220;make a killing&#8221;, just a living, so that I can keep doing what I love and share it with others.</p>

<!--
**Edits**:

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

<!-- without a comment or something here, the last item above becomes a paragraph -->
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/seeking-advice-on-software-licensing/feed/</wfw:commentRss>
		</item>
		<item>
		<title>What is automatic differentiation, and why does it work?</title>
		<link>http://conal.net/blog/posts/what-is-automatic-differentiation-and-why-does-it-work/</link>
		<comments>http://conal.net/blog/posts/what-is-automatic-differentiation-and-why-does-it-work/#comments</comments>
		<pubDate>Wed, 28 Jan 2009 20:09:42 +0000</pubDate>
		<dc:creator>conal</dc:creator>
		
		<category><![CDATA[Functional programming]]></category>

		<category><![CDATA[derivative]]></category>

		<category><![CDATA[semantics]]></category>

		<category><![CDATA[type class morphism]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=79</guid>
		<description><![CDATA[





Bertrand Russell remarked that


  Everything is vague to a degree you do not realize till you have tried to make it precise.


I&#8217;m mulling over automatic differentiation (AD) again, neatening up previous posts on derivatives and on linear maps, working them into a coherent whole for an ICFP submission.
I understand the mechanics and some of [...]]]></description>
			<content:encoded><![CDATA[<!-- 

Title: What is automatic differentiation, and why does it work?

Tags: derivative, semantics, type class morphism

URL: http://conal.net/blog/posts/what-is-automatic-differentiation-and-why-does-it-work/

-->

<!-- references -->

<!-- teaser -->

<p>Bertrand Russell remarked that</p>

<blockquote>
  <p><em>Everything is vague to a degree you do not realize till you have tried to make it precise.</em></p>
</blockquote>

<p>I&#8217;m mulling over automatic differentiation (AD) again, neatening up previous posts on <a href="http://conal.net/blog/tag/derivative/" title="posts on derivatives">derivatives</a> and on <a href="http://conal.net/blog/tag/linear-map/" title="posts on linear maps">linear maps</a>, working them into a coherent whole for an ICFP submission.
I understand the mechanics and some of the reasons for its correctness.
After all, it&#8217;s &#8220;just the chain rule&#8221;.</p>

<p>As usual, in the process of writing, I bumped up against Russell&#8217;s principle.
I felt a growing uneasiness and realized that I didn&#8217;t understand AD in the way I like to understand software, namely,</p>

<ul>
<li><em>What</em> does it mean, independently of implementation?</li>
<li><em>How</em> do the implementation and its correctness flow gracefully from that meaning?</li>
<li><em>Where</em> else might we go, guided by answers to the first two questions?</li>
</ul>

<p>Ever since writing <em><a href="http://conal.net/papers/simply-reactive" title="paper">Simply efficient functional reactivity</a></em>, the idea of <a href="http://conal.net/blog/tag/type-class-morphism/" title="posts on type class morphisms">type class morphisms</a> keeps popping up for me as a framework in which to ask and answer these questions.
To my delight, this framework gives me new and more satisfying insight into automatic differentiation.</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-79"></span></p>

<h3>What&#8217;s a derivative?</h3>

<p>My first guess is that AD has something to do with derivatives, which then raises the question of what is a derivative.
For now, I&#8217;m going to substitute a popular but problematic answer to that question and say that</p>

<p><div>
<pre class="haskell">deriv :: ... =&gt; <span style="color: green;">&#40;</span>a -&gt; b<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</span>a -&gt; b<span style="color: green;">&#41;</span> <span style="color: #5d478b; font-style: italic;">&#8211;  simplification</span></pre>
</div></p>

<p>As discussed in <em><a href="http://conal.net/blog/posts/what-is-a-derivative-really/" title="blog post">What is a derivative, really?</a></em>, the popular answer has limited usefulness, applying just to scalar (one-dimensional) domain.
The real deal involves distinguishing the type <code>b</code> from the type <code>a :-* b</code> of <a href="http://conal.net/blog/tag/linear-map/" title="posts on linear maps">linear maps</a> from <code>a</code> to <code>b</code>.</p>

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

<h4>Why care about derivatives?</h4>

<p>Derivatives are useful in a variety of application areas, including root-finding, optimization, curve and surface tessellation, and computation of surface normals for 3D rendering. Considering the usefulness of derivatives, it is worthwhile to find software methods that are</p>

<ul>
<li>simple (to implement and verify),</li>
<li>convenient,</li>
<li>accurate,</li>
<li>efficient, and</li>
<li>general.</li>
</ul>

<h3>What <em>isn&#8217;t</em> AD?</h3>

<h4>Numeric approximation</h4>

<p>One differentiation method <em>numeric approximation</em>, using simple finite differences.
This method is based on the definition of (scalar) derivative:</p>

<p><div class=latex><img src='http://conal.net/blog/wp-content/latex/214/214b398fc4d0cd17e851513dc84801e6-ffffff000000.png' alt=' deriv f x \equiv \lim_{h \to 0} \frac{f (x + h) - f x}{h} ' class='latex' /></div></p>

<p>The left-hand side reads &#8220;the derivative of <em>f</em> at <em>x</em>&#8220;.</p>

<p>To approximate the derivative, use</p>

<p><div class=latex><img src='http://conal.net/blog/wp-content/latex/2fb/2fb30a7ff2bdc0945da6eb69eeb432ac-ffffff000000.png' alt=' deriv f x \approx \frac{f (x + h) - f x}{h} ' class='latex' /></div></p>

<p>for a small value of <em>h</em>.
While very simple, this method is often inaccurate, due to choosing either too large or too small a value for <em>h</em>.
(Small values of <em>h</em> lead to rounding errors.) More sophisticated variations improve accuracy while sacrificing simplicity.</p>

<h4>Symbolic differentiation</h4>

<p>A second method is <em>symbolic differentiation</em>.
Instead of using the definition of <em>deriv</em> directly, the symbolic method uses a collection of rules, such as those below:</p>

<p><div>
<pre class="haskell">deriv <span style="color: green;">&#40;</span>u + v<span style="color: green;">&#41;</span>   == deriv u + deriv v
deriv <span style="color: green;">&#40;</span>u * v<span style="color: green;">&#41;</span>   == deriv v * u + deriv u * v
deriv <span style="color: green;">&#40;</span>- u<span style="color: green;">&#41;</span>     == - deriv u
deriv <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:exp"><span style="font-weight: bold;">exp</span></a> u<span style="color: green;">&#41;</span>   == deriv u * <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:exp"><span style="font-weight: bold;">exp</span></a> u
deriv <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:log"><span style="font-weight: bold;">log</span></a> u<span style="color: green;">&#41;</span>   == deriv u / u
deriv <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sqrt"><span style="font-weight: bold;">sqrt</span></a> u<span style="color: green;">&#41;</span>  == deriv u / <span style="color: green;">&#40;</span><span style="color: red;">2</span> * <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sqrt"><span style="font-weight: bold;">sqrt</span></a> u<span style="color: green;">&#41;</span>
deriv <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sin"><span style="font-weight: bold;">sin</span></a> u<span style="color: green;">&#41;</span>   == deriv u * <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:cos"><span style="font-weight: bold;">cos</span></a> u
deriv <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:cos"><span style="font-weight: bold;">cos</span></a> u<span style="color: green;">&#41;</span>   == deriv u * <span style="color: green;">&#40;</span>- <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sin"><span style="font-weight: bold;">sin</span></a> u<span style="color: green;">&#41;</span>
deriv <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:asin"><span style="font-weight: bold;">asin</span></a> u<span style="color: green;">&#41;</span>  == deriv u/<span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sqrt"><span style="font-weight: bold;">sqrt</span></a> <span style="color: green;">&#40;</span><span style="color: red;">1</span> - u^<span style="color: red;">2</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
deriv <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:acos"><span style="font-weight: bold;">acos</span></a> u<span style="color: green;">&#41;</span>  == - deriv u/<span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sqrt"><span style="font-weight: bold;">sqrt</span></a> <span style="color: green;">&#40;</span><span style="color: red;">1</span> - u^<span style="color: red;">2</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
deriv <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:atan"><span style="font-weight: bold;">atan</span></a> u<span style="color: green;">&#41;</span>  == deriv u / <span style="color: green;">&#40;</span>u^<span style="color: red;">2</span> + <span style="color: red;">1</span><span style="color: green;">&#41;</span>
deriv <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sinh"><span style="font-weight: bold;">sinh</span></a> u<span style="color: green;">&#41;</span>  == deriv u * <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:cosh"><span style="font-weight: bold;">cosh</span></a> u
deriv <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:cosh"><span style="font-weight: bold;">cosh</span></a> u<span style="color: green;">&#41;</span>  == deriv u * <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sinh"><span style="font-weight: bold;">sinh</span></a> u
deriv <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:asinh"><span style="font-weight: bold;">asinh</span></a> u<span style="color: green;">&#41;</span> == deriv u / <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sqrt"><span style="font-weight: bold;">sqrt</span></a> <span style="color: green;">&#40;</span>u^<span style="color: red;">2</span> + <span style="color: red;">1</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
deriv <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:acosh"><span style="font-weight: bold;">acosh</span></a> u<span style="color: green;">&#41;</span> == - deriv u / <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sqrt"><span style="font-weight: bold;">sqrt</span></a> <span style="color: green;">&#40;</span>u^<span style="color: red;">2</span> - <span style="color: red;">1</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
deriv <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:atanh"><span style="font-weight: bold;">atanh</span></a> u<span style="color: green;">&#41;</span> == deriv u / <span style="color: green;">&#40;</span><span style="color: red;">1</span> - u^<span style="color: red;">2</span><span style="color: green;">&#41;</span></pre>
</div></p>

<p>There are two main drawbacks to the symbolic approach to differentiation.</p>

<ul>
<li>As a symbolic method, it requires access to and transformation of source code, and placing restrictions on that source code.</li>
<li>Implementations tend to be quite expensive and in particular perform redundant computation. (I wonder if this latter criticism is a straw man argument.  Are symbolic methods <em>necessarily</em> expensive or just when implemented naïvely?  For instance, can simply memoized symbolic differentiation be nearly as cheap as AD?)</li>
</ul>

<h3>What is AD and how does it work?</h3>

<p>A third method is the topic of this post, namely <em>automatic differentiation</em> (also called &#8220;algorithmic differentiation&#8221;), or &#8220;AD&#8221;.
The idea of AD is to simultaneously manipulate values and derivatives.
Overloading of the standard numerical operations (and literals) makes this combined manipulation as convenient and elegant as manipulating values without derivatives.</p>

<p>The implementation of AD can be quite simple, as shown below:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">data</span> D a = D a a <span style="color: #050; font-weight: bold;">deriving</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Eq"><span style="color: #833; font-weight: bold;">Eq</span></a>,<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Show"><span style="color: #833; font-weight: bold;">Show</span></a><span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #050; font-weight: bold;">instance</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Num"><span style="color: #833; font-weight: bold;">Num</span></a> a =&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Num"><span style="color: #833; font-weight: bold;">Num</span></a> <span style="color: green;">&#40;</span>D a<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  D x x&#8217; + D y y&#8217; = D <span style="color: green;">&#40;</span>x+y<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>x&#8217;+y&#8217;<span style="color: green;">&#41;</span>
  D x x&#8217; * D y y&#8217; = D <span style="color: green;">&#40;</span>x*y<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>y&#8217;*x + x&#8217;*y<span style="color: green;">&#41;</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fromInteger"><span style="font-weight: bold;">fromInteger</span></a> x   = D <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fromInteger"><span style="font-weight: bold;">fromInteger</span></a> x<span style="color: green;">&#41;</span> <span style="color: red;">0</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:negate"><span style="font-weight: bold;">negate</span></a> <span style="color: green;">&#40;</span>D x x&#8217;<span style="color: green;">&#41;</span> = D <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:negate"><span style="font-weight: bold;">negate</span></a> x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:negate"><span style="font-weight: bold;">negate</span></a> x&#8217;<span style="color: green;">&#41;</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:signum"><span style="font-weight: bold;">signum</span></a> <span style="color: green;">&#40;</span>D x _ <span style="color: green;">&#41;</span> = D <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:signum"><span style="font-weight: bold;">signum</span></a> x<span style="color: green;">&#41;</span> <span style="color: red;">0</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:abs"><span style="font-weight: bold;">abs</span></a>    <span style="color: green;">&#40;</span>D x x&#8217;<span style="color: green;">&#41;</span> = D <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:abs"><span style="font-weight: bold;">abs</span></a> x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>x&#8217; * <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:signum"><span style="font-weight: bold;">signum</span></a> x<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #050; font-weight: bold;">instance</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Fractional"><span style="color: #833; font-weight: bold;">Fractional</span></a> x =&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Fractional"><span style="color: #833; font-weight: bold;">Fractional</span></a> <span style="color: green;">&#40;</span>D x<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fromRational"><span style="font-weight: bold;">fromRational</span></a> x  = D <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fromRational"><span style="font-weight: bold;">fromRational</span></a> x<span style="color: green;">&#41;</span> <span style="color: red;">0</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:recip"><span style="font-weight: bold;">recip</span></a>  <span style="color: green;">&#40;</span>D x x&#8217;<span style="color: green;">&#41;</span> = D <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:recip"><span style="font-weight: bold;">recip</span></a> x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>x&#8217; / sqr x<span style="color: green;">&#41;</span>
&nbsp;
sqr :: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Num"><span style="color: #833; font-weight: bold;">Num</span></a> a =&gt; a -&gt; a
sqr x = x * x
&nbsp;
<span style="color: #050; font-weight: bold;">instance</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Floating"><span style="color: #833; font-weight: bold;">Floating</span></a> x =&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Floating"><span style="color: #833; font-weight: bold;">Floating</span></a> <span style="color: green;">&#40;</span>D x<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:pi"><span style="font-weight: bold;">pi</span></a>              = D <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:pi"><span style="font-weight: bold;">pi</span></a> <span style="color: red;">0</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:exp"><span style="font-weight: bold;">exp</span></a>    <span style="color: green;">&#40;</span>D x x&#8217;<span style="color: green;">&#41;</span> = D <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:exp"><span style="font-weight: bold;">exp</span></a>    x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>x&#8217; * <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:exp"><span style="font-weight: bold;">exp</span></a> x<span style="color: green;">&#41;</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:log"><span style="font-weight: bold;">log</span></a>    <span style="color: green;">&#40;</span>D x x&#8217;<span style="color: green;">&#41;</span> = D <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:log"><span style="font-weight: bold;">log</span></a>    x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>x&#8217; / x<span style="color: green;">&#41;</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sqrt"><span style="font-weight: bold;">sqrt</span></a>   <span style="color: green;">&#40;</span>D x x&#8217;<span style="color: green;">&#41;</span> = D <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sqrt"><span style="font-weight: bold;">sqrt</span></a>   x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>x&#8217; / <span style="color: green;">&#40;</span><span style="color: red;">2</span> * <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sqrt"><span style="font-weight: bold;">sqrt</span></a> x<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sin"><span style="font-weight: bold;">sin</span></a>    <span style="color: green;">&#40;</span>D x x&#8217;<span style="color: green;">&#41;</span> = D <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sin"><span style="font-weight: bold;">sin</span></a>    x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>x&#8217; * <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:cos"><span style="font-weight: bold;">cos</span></a> x<span style="color: green;">&#41;</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:cos"><span style="font-weight: bold;">cos</span></a>    <span style="color: green;">&#40;</span>D x x&#8217;<span style="color: green;">&#41;</span> = D <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:cos"><span style="font-weight: bold;">cos</span></a>    x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>x&#8217; * <span style="color: green;">&#40;</span>- <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sin"><span style="font-weight: bold;">sin</span></a> x<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:asin"><span style="font-weight: bold;">asin</span></a>   <span style="color: green;">&#40;</span>D x x&#8217;<span style="color: green;">&#41;</span> = D <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:asin"><span style="font-weight: bold;">asin</span></a>   x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>x&#8217; / <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sqrt"><span style="font-weight: bold;">sqrt</span></a> <span style="color: green;">&#40;</span><span style="color: red;">1</span> - sqr x<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:acos"><span style="font-weight: bold;">acos</span></a>   <span style="color: green;">&#40;</span>D x x&#8217;<span style="color: green;">&#41;</span> = D <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:acos"><span style="font-weight: bold;">acos</span></a>   x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>x&#8217; / <span style="color: green;">&#40;</span>-  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sqrt"><span style="font-weight: bold;">sqrt</span></a> <span style="color: green;">&#40;</span><span style="color: red;">1</span> - sqr x<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
  <span style="color: #5d478b; font-style: italic;">&#8211; &#8230;</span></pre>
</div></p>

<p>As an example, define</p>

<p><div>
<pre class="haskell">f1 :: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Floating"><span style="color: #833; font-weight: bold;">Floating</span></a> a =&gt; a -&gt; a
f1 z = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sqrt"><span style="font-weight: bold;">sqrt</span></a> <span style="color: green;">&#40;</span><span style="color: red;">3</span> * <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sin"><span style="font-weight: bold;">sin</span></a> z<span style="color: green;">&#41;</span></pre>
</div></p>

<p>and try it out in GHCi:</p>

<p><div>
<pre class="haskell">*Main&gt; f1 <span style="color: green;">&#40;</span>D <span style="color: red;">2</span> <span style="color: red;">1</span><span style="color: green;">&#41;</span>
D <span style="color: red;">1.6516332160855343</span> <span style="color: green;">&#40;</span><span style="color: red;">-0.3779412091869595</span><span style="color: green;">&#41;</span></pre>
</div></p>

<p>To test correctness, here is a symbolically differentiated version:</p>

<p><div>
<pre class="haskell">f2 :: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Floating"><span style="color: #833; font-weight: bold;">Floating</span></a> a =&gt; a -&gt; D a
f2 x = D <span style="color: green;">&#40;</span>f1 x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><span style="color: red;">3</span> * <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:cos"><span style="font-weight: bold;">cos</span></a> x / <span style="color: green;">&#40;</span><span style="color: red;">2</span> * <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sqrt"><span style="font-weight: bold;">sqrt</span></a> <span style="color: green;">&#40;</span><span style="color: red;">3</span> * <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sin"><span style="font-weight: bold;">sin</span></a> x<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre>
</div></p>

<p>Try it out:</p>

<p><div>
<pre class="haskell">*Main&gt; f2 <span style="color: red;">2</span>
D <span style="color: red;">1.6516332160855343</span> <span style="color: green;">&#40;</span><span style="color: red;">-0.3779412091869595</span><span style="color: green;">&#41;</span></pre>
</div></p>

<p>The can also be made prettier, as in <em><a href="http://conal.net/blog/posts/beautiful-differentiation/" title="blog post">Beautiful differentiation</a></em>.
Add an operator that captures the chain rule, which is behind the differentiation laws listed above.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">infix</span>  <span style="color: red;">0</span> &gt;-&lt;
<span style="color: green;">&#40;</span>&gt;-&lt;<span style="color: green;">&#41;</span> :: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Num"><span style="color: #833; font-weight: bold;">Num</span></a> a =&gt; <span style="color: green;">&#40;</span>a -&gt; a<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</span>a -&gt; a<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</span>D a -&gt; D a<span style="color: green;">&#41;</span>
<span style="color: green;">&#40;</span>f &gt;-&lt; f&#8217;<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>D a a&#8217;<span style="color: green;">&#41;</span> = D <span style="color: green;">&#40;</span>f a<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>a&#8217; * f&#8217; a<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Then, e.g.,</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Floating"><span style="color: #833; font-weight: bold;">Floating</span></a> a =&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Floating"><span style="color: #833; font-weight: bold;">Floating</span></a> <span style="color: green;">&#40;</span>D a<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:pi"><span style="font-weight: bold;">pi</span></a>   = D <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:pi"><span style="font-weight: bold;">pi</span></a> <span style="color: red;">0</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:exp"><span style="font-weight: bold;">exp</span></a>  = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:exp"><span style="font-weight: bold;">exp</span></a>  &gt;-&lt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:exp"><span style="font-weight: bold;">exp</span></a>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:log"><span style="font-weight: bold;">log</span></a>  = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:log"><span style="font-weight: bold;">log</span></a>  &gt;-&lt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:recip"><span style="font-weight: bold;">recip</span></a>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sqrt"><span style="font-weight: bold;">sqrt</span></a> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sqrt"><span style="font-weight: bold;">sqrt</span></a> &gt;-&lt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:recip"><span style="font-weight: bold;">recip</span></a> <span style="color: green;">&#40;</span><span style="color: red;">2</span> * <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sqrt"><span style="font-weight: bold;">sqrt</span></a><span style="color: green;">&#41;</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sin"><span style="font-weight: bold;">sin</span></a>  = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sin"><span style="font-weight: bold;">sin</span></a>  &gt;-&lt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:cos"><span style="font-weight: bold;">cos</span></a>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:cos"><span style="font-weight: bold;">cos</span></a>  = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:cos"><span style="font-weight: bold;">cos</span></a>  &gt;-&lt; - <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sin"><span style="font-weight: bold;">sin</span></a>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:asin"><span style="font-weight: bold;">asin</span></a> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:asin"><span style="font-weight: bold;">asin</span></a> &gt;-&lt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:recip"><span style="font-weight: bold;">recip</span></a> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sqrt"><span style="font-weight: bold;">sqrt</span></a> <span style="color: green;">&#40;</span><span style="color: red;">1</span>-sqr<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:acos"><span style="font-weight: bold;">acos</span></a> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:acos"><span style="font-weight: bold;">acos</span></a> &gt;-&lt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:recip"><span style="font-weight: bold;">recip</span></a> <span style="color: green;">&#40;</span>- <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sqrt"><span style="font-weight: bold;">sqrt</span></a> <span style="color: green;">&#40;</span><span style="color: red;">1</span>-sqr<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
  <span style="color: #5d478b; font-style: italic;">&#8211; &#8230;</span></pre>
</div></p>

<p>This AD implementation satisfy most of our criteria very well:</p>

<ul>
<li>It is simple to implement and verify.  Both the implementation and its correctness follow directly from the familiar laws given above.</li>
<li>It is convenient to use, as shown with <code>f1</code> above.</li>
<li>It is accurate, as shown above, producing <em>exactly</em> the same result as the symbolic differentiated code (<code>f2</code>). </li>
<li>It is efficient, involving no iteration or redundant computation.</li>
</ul>

<p>The formulation above does less well with <em>generality</em>:</p>

<ul>
<li>It computes only first derivatives.</li>
<li>It applies (correctly) only to functions over a scalar (one-dimensional) domain, excluding even complex numbers.</li>
</ul>

<p>Both of these limitations are removed in the post <em><a href="http://conal.net/blog/posts/higher-dimensional-higher-order-derivatives-functionally/" title="blog post">Higher-dimensional, higher-order derivatives, functionally</a></em>.</p>

<h3>What is AD, really?</h3>

<p>How do we know whether this AD implementation is correct?
We can&#8217;t begin to address this question until we first answer a more fundamental one: what does its correctness mean?</p>

<h4>A model for AD</h4>

<p>I&#8217;m pretty sure AD has something to do with calculating a function&#8217;s values and derivative values simultaneously, so I&#8217;ll start there.</p>

<p><div>
<pre class="haskell">withD :: ... =&gt; <span style="color: green;">&#40;</span>a -&gt; a<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</span>a -&gt; D a<span style="color: green;">&#41;</span>
withD f x = D <span style="color: green;">&#40;</span>f x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>deriv f x<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Or, in point-free form,</p>

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

<p>Since, on functions,</p>

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

<p>We don&#8217;t have an implementation of <code>deriv</code>, so this definition of <code>withD</code> will serve as a specification, not an implementation.</p>

<p>If AD is structured as type class instances, then I&#8217;d want there to be a compelling interpretation function that is faithful to each of those classes, as in the principle of <a href="http://conal.net/blog/tag/type-class-morphism/" title="posts on type class morphisms">type class morphisms</a>, which is to say that the interpretation of each method corresponds to the same method for the interpretation.</p>

<p>For AD, the interpretation function is <code>withD</code>.
It&#8217;s turned around this time (mapping <em>to</em> instead of <em>from</em> our type), as is sometimes the case.
The <code>Num</code>, <code>Fractional</code>, and <code>Floating</code> morphisms provide the specifications of the instances:</p>

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

<p>Note here that the methods on the left are on <code>a -&gt; a</code>, and on the right are on <code>a -&gt; D a</code>.</p>

<p>These (morphism) properties exactly define correctness of any implementation of AD, answering my first question:</p>

<blockquote>
  <p><em>What</em> does it mean, independently of implementation?</p>
</blockquote>

<h3>Deriving an AD implementation</h3>

<p>Now that we have a simple, formal specification of AD (numeric type class morphisms), we can try to prove that the implementation above satisfies the specification.
Better yet, let&#8217;s do the reverse, and use the morphism properties to <em>discover</em> the implementation, and prove it correct in the process.</p>

<h4>Addition</h4>

<p>Here is the addition specification:</p>

<p><div>
<pre class="haskell">withD <span style="color: green;">&#40;</span>u + v<span style="color: green;">&#41;</span> == withD u + withD v</pre>
</div></p>

<p>Start with the left-hand side:</p>

<p><div>
<pre class="haskell">   withD <span style="color: green;">&#40;</span>u + v<span style="color: green;">&#41;</span>
==   <span style="color: #5d478b; font-style: italic;">{- def of withD -}</span>
   <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:liftA2"><span style="font-weight: bold;">liftA2</span></a> D <span style="color: green;">&#40;</span>u + v<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>deriv <span style="color: green;">&#40;</span>u + v<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
==   <span style="color: #5d478b; font-style: italic;">{- deriv rule for (+) -}</span>
   <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:liftA2"><span style="font-weight: bold;">liftA2</span></a> D <span style="color: green;">&#40;</span>u + v<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>deriv u + deriv v<span style="color: green;">&#41;</span>
==   <span style="color: #5d478b; font-style: italic;">{- liftA2 on functions -}</span>
   \ x -&gt; D <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>u + v<span style="color: green;">&#41;</span> x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>deriv u + deriv v<span style="color: green;">&#41;</span> x<span style="color: green;">&#41;</span>
==   <span style="color: #5d478b; font-style: italic;">{- (+) on functions -}</span>
   \ x -&gt; D <span style="color: green;">&#40;</span>u x + v x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>deriv u x + deriv v x<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Then start over with the right-hand side:</p>

<p><div>
<pre class="haskell">   withD u + withD v
==   <span style="color: #5d478b; font-style: italic;">{- (+) on functions -}</span>
   \ x -&gt; withD u x + withD v x
==   <span style="color: #5d478b; font-style: italic;">{- def of withD -}</span>
   \ x -&gt; D <span style="color: green;">&#40;</span>u x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>deriv u x<span style="color: green;">&#41;</span> + D <span style="color: green;">&#40;</span>v x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>deriv v x<span style="color: green;">&#41;</span></pre>
</div></p>

<p>We need a definition of <code>(+)</code> on <code>D</code> that makes these two final forms equal, i.e.,</p>

<p><div>
<pre class="haskell">   \ x -&gt; D <span style="color: green;">&#40;</span>u x + v x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>deriv u x + deriv v x<span style="color: green;">&#41;</span>
==
   \ x -&gt; D <span style="color: green;">&#40;</span>u x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>deriv u x<span style="color: green;">&#41;</span> + D <span style="color: green;">&#40;</span>v x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>deriv v x<span style="color: green;">&#41;</span></pre>
</div></p>

<p>An easy choice is</p>

<p><div>
<pre class="haskell">D a a' + D b b' = D <span style="color: green;">&#40;</span>a + b<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>a&#8217; + b&#8217;<span style="color: green;">&#41;</span></pre>
</div></p>

<p>This definition provides the missing link and that completes the proof that</p>

<p><div>
<pre class="haskell">withD <span style="color: green;">&#40;</span>u + v<span style="color: green;">&#41;</span> == withD u + withD v</pre>
</div></p>

<h4>Multiplication</h4>

<p>The specification:</p>

<p><div>
<pre class="haskell">withD <span style="color: green;">&#40;</span>u * v<span style="color: green;">&#41;</span> == withD u * withD v</pre>
</div></p>

<p>Reason similarly to the addition case.
Begin with the left hand side:</p>

<p><div>
<pre class="haskell">   withD <span style="color: green;">&#40;</span>u * v<span style="color: green;">&#41;</span>
== <span style="color: #5d478b; font-style: italic;">{- def of withD -}</span>
   <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:liftA2"><span style="font-weight: bold;">liftA2</span></a> D <span style="color: green;">&#40;</span>u * v<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>deriv <span style="color: green;">&#40;</span>u * v<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
== <span style="color: #5d478b; font-style: italic;">{- deriv rule for (*) -}</span>
   <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:liftA2"><span style="font-weight: bold;">liftA2</span></a> D <span style="color: green;">&#40;</span>u * v<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>deriv u * v + deriv v * u<span style="color: green;">&#41;</span>
== <span style="color: #5d478b; font-style: italic;">{- liftA2 on functions -}</span>
   \ x -&gt; D <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>u * v<span style="color: green;">&#41;</span> x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>deriv u * v + deriv v * u<span style="color: green;">&#41;</span> x<span style="color: green;">&#41;</span>
== <span style="color: #5d478b; font-style: italic;">{- (*) and (+) on functions -}</span>
   \ x -&gt; D <span style="color: green;">&#40;</span>u x * v x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>deriv u x * v x + * deriv v x * u x<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Then start over with the right-hand side:</p>

<p><div>
<pre class="haskell">   withD u * withD v
== <span style="color: #5d478b; font-style: italic;">{- (*) on functions -}</span>
   \ x -&gt; withD u x * withD v x
== <span style="color: #5d478b; font-style: italic;">{- def of withD -}</span>
   \ x -&gt; D <span style="color: green;">&#40;</span>u x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>deriv u x<span style="color: green;">&#41;</span> * D <span style="color: green;">&#40;</span>v x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>deriv v x<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Sufficient definition:</p>

<p><div>
<pre class="haskell">D a a' * D b b' = D <span style="color: green;">&#40;</span>a + b<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>a&#8217; * b + b&#8217; * a<span style="color: green;">&#41;</span></pre>
</div></p>

<h4>Sine</h4>

<p>Specification:</p>

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

<p>Begin with the left hand side:</p>

<p><div>
<pre class="haskell">   withD <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sin"><span style="font-weight: bold;">sin</span></a> u<span style="color: green;">&#41;</span>
== <span style="color: #5d478b; font-style: italic;">{- def of withD -}</span>
   <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:liftA2"><span style="font-weight: bold;">liftA2</span></a> D <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sin"><span style="font-weight: bold;">sin</span></a> u<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>deriv <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sin"><span style="font-weight: bold;">sin</span></a> u<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
== <span style="color: #5d478b; font-style: italic;">{- deriv rule for sin -}</span>
   <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:liftA2"><span style="font-weight: bold;">liftA2</span></a> D <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sin"><span style="font-weight: bold;">sin</span></a> u<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>deriv u * <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:cos"><span style="font-weight: bold;">cos</span></a> u<span style="color: green;">&#41;</span>
== <span style="color: #5d478b; font-style: italic;">{- liftA2 on functions -}</span>
   \ x -&gt; D <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sin"><span style="font-weight: bold;">sin</span></a> u<span style="color: green;">&#41;</span> x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>deriv u * <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:cos"><span style="font-weight: bold;">cos</span></a> u<span style="color: green;">&#41;</span> x<span style="color: green;">&#41;</span>
== <span style="color: #5d478b; font-style: italic;">{- sin, (*) and cos on functions -}</span>
   \ x -&gt; D <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sin"><span style="font-weight: bold;">sin</span></a> <span style="color: green;">&#40;</span>u x<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>deriv u x * <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:cos"><span style="font-weight: bold;">cos</span></a> <span style="color: green;">&#40;</span>u x<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre>
</div></p>

<p>Then start over with the right-hand side:</p>

<p><div>
<pre class="haskell">   <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sin"><span style="font-weight: bold;">sin</span></a> <span style="color: green;">&#40;</span>withD u<span style="color: green;">&#41;</span>
== <span style="color: #5d478b; font-style: italic;">{- sin on functions -}</span>
   \ x -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sin"><span style="font-weight: bold;">sin</span></a> <span style="color: green;">&#40;</span>withD u x<span style="color: green;">&#41;</span>
== <span style="color: #5d478b; font-style: italic;">{- def of withD -}</span>
   \ x -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sin"><span style="font-weight: bold;">sin</span></a> <span style="color: green;">&#40;</span>D <span style="color: green;">&#40;</span>u x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>deriv u x<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre>
</div></p>

<p>Sufficient definition:</p>

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

<p>Or, using the chain rule operator,</p>

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

<p>The whole implementation can be derived in exactly this style, answering my second question:</p>

<blockquote>
  <p><em>How</em> does the implementation and its correctness flow gracefully from that meaning?</p>
</blockquote>

<h3>Higher-order derivatives</h3>

<p>Given answers to the first two questions, let&#8217;s, turn to the third:</p>

<blockquote>
  <p><em>Where</em> else might we go, guided by answers to the first two questions?</p>
</blockquote>

<p>Jerzy Karczmarczuk extended the <code>D</code> representation above to an infinite &#8220;lazy tower of derivatives&#8221;, in the paper <em><a href="http://citeseer.ist.psu.edu/karczmarczuk98functional.html" title="ICFP '98 paper by Jerzy Karczmarczuk">Functional Differentiation of Computer Programs</a></em>.</p>

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

<p>The <code>withD</code> function easily adapts to this new <code>D</code> type:</p>

<p><div>
<pre class="haskell">withD :: ... =&gt; <span style="color: green;">&#40;</span>a -&gt; a<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</span>a -&gt; D a<span style="color: green;">&#41;</span>
withD f x = D <span style="color: green;">&#40;</span>f x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>withD <span style="color: green;">&#40;</span>deriv f<span style="color: green;">&#41;</span> x<span style="color: green;">&#41;</span></pre>
</div></p>

<p>or</p>

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

<p>These definitions were not brilliant insights.
I looked for the simplest, type-correct possibility (without using &perp;).</p>

<p>Similarly, I&#8217;ll try tweaking the previous derivations and see what pops out.</p>

<h4>Addition</h4>

<p>Left-hand side:</p>

<p><div>
<pre class="haskell">   withD <span style="color: green;">&#40;</span>u + v<span style="color: green;">&#41;</span>
==   <span style="color: #5d478b; font-style: italic;">{- def of withD -}</span>
   <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:liftA2"><span style="font-weight: bold;">liftA2</span></a> D <span style="color: green;">&#40;</span>u + v<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>withD <span style="color: green;">&#40;</span>deriv <span style="color: green;">&#40;</span>u + v<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
==   <span style="color: #5d478b; font-style: italic;">{- deriv rule for (+) -}</span>
   <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:liftA2"><span style="font-weight: bold;">liftA2</span></a> D <span style="color: green;">&#40;</span>u + v<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>withD <span style="color: green;">&#40;</span>deriv u + deriv v<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
==   <span style="color: #5d478b; font-style: italic;">{- (fixed-point) induction withD and (+) -}</span>
   <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:liftA2"><span style="font-weight: bold;">liftA2</span></a> D <span style="color: green;">&#40;</span>u + v<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>withD <span style="color: green;">&#40;</span>deriv u<span style="color: green;">&#41;</span> + withD <span style="color: green;">&#40;</span>deriv v<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
==   <span style="color: #5d478b; font-style: italic;">{- def of liftA2 and (+) on functions -}</span>
   \ x -&gt; D <span style="color: green;">&#40;</span>u x + v x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>withD <span style="color: green;">&#40;</span>deriv u<span style="color: green;">&#41;</span> x + withD <span style="color: green;">&#40;</span>deriv v<span style="color: green;">&#41;</span> x<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Right-hand side:</p>

<p><div>
<pre class="haskell">   withD u + withD v
==   <span style="color: #5d478b; font-style: italic;">{- (+) on functions -}</span>
   \ x -&gt; withD u x + withD v x
==   <span style="color: #5d478b; font-style: italic;">{- def of withD -}</span>
   \ x -&gt; D <span style="color: green;">&#40;</span>u x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>withD <span style="color: green;">&#40;</span>deriv u x<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> + D <span style="color: green;">&#40;</span>v x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>withD <span style="color: green;">&#40;</span>deriv v x<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre>
</div></p>

<p>Again, we need a definition of <code>(+)</code> on <code>D</code> that makes the LHS and RHS final forms equal, i.e.,</p>

<p><div>
<pre class="haskell">   \ x -&gt; D <span style="color: green;">&#40;</span>u x + v x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>withD <span style="color: green;">&#40;</span>deriv u<span style="color: green;">&#41;</span> x + with <span style="color: green;">&#40;</span>deriv v<span style="color: green;">&#41;</span> x<span style="color: green;">&#41;</span>
==
   \ x -&gt; D <span style="color: green;">&#40;</span>u x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>withD <span style="color: green;">&#40;</span>deriv u<span style="color: green;">&#41;</span> x<span style="color: green;">&#41;</span> + D <span style="color: green;">&#40;</span>v x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>withD <span style="color: green;">&#40;</span>deriv v<span style="color: green;">&#41;</span> x<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Again, an easy choice is</p>

<p><div>
<pre class="haskell">D a a' + D b b' = D <span style="color: green;">&#40;</span>a + b<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>a&#8217; + b&#8217;<span style="color: green;">&#41;</span></pre>
</div></p>

<h4>Multiplication</h4>

<p>Left-hand side:</p>

<p><div>
<pre class="haskell">   withD <span style="color: green;">&#40;</span>u * v<span style="color: green;">&#41;</span>
==   <span style="color: #5d478b; font-style: italic;">{- def of withD -}</span>
   <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:liftA2"><span style="font-weight: bold;">liftA2</span></a> D <span style="color: green;">&#40;</span>u * v<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>withD <span style="color: green;">&#40;</span>deriv <span style="color: green;">&#40;</span>u * v<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
==   <span style="color: #5d478b; font-style: italic;">{- deriv rule for (*) -}</span>
   <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:liftA2"><span style="font-weight: bold;">liftA2</span></a> D <span style="color: green;">&#40;</span>u * v<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>withD <span style="color: green;">&#40;</span>deriv u * v + deriv v * u<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
==   <span style="color: #5d478b; font-style: italic;">{- induction for withD/(+) -}</span>
   <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:liftA2"><span style="font-weight: bold;">liftA2</span></a> D <span style="color: green;">&#40;</span>u * v<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>withD <span style="color: green;">&#40;</span>deriv u * v<span style="color: green;">&#41;</span> + withD <span style="color: green;">&#40;</span>deriv v * u<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
==   <span style="color: #5d478b; font-style: italic;">{- induction for withD/(*) -}</span>
   <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:liftA2"><span style="font-weight: bold;">liftA2</span></a> D <span style="color: green;">&#40;</span>u * v<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>withD <span style="color: green;">&#40;</span>deriv u<span style="color: green;">&#41;</span> * withD v + withD <span style="color: green;">&#40;</span>deriv v<span style="color: green;">&#41;</span> * withD u<span style="color: green;">&#41;</span>
==   <span style="color: #5d478b; font-style: italic;">{- liftA2, (*), (+) on functions -}</span>
   \ x -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:liftA2"><span style="font-weight: bold;">liftA2</span></a> D <span style="color: green;">&#40;</span>u x * v x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>withD <span style="color: green;">&#40;</span>deriv u<span style="color: green;">&#41;</span> x * withD v x + withD <span style="color: green;">&#40;</span>deriv v<span style="color: green;">&#41;</span> x * withD u x<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Right-hand side:</p>

<p><div>
<pre class="haskell">   withD u * withD v
==   <span style="color: #5d478b; font-style: italic;">{- def of withD -}</span>
   <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:liftA2"><span style="font-weight: bold;">liftA2</span></a> D u <span style="color: green;">&#40;</span>withD <span style="color: green;">&#40;</span>deriv u<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> * <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:liftA2"><span style="font-weight: bold;">liftA2</span></a> D v <span style="color: green;">&#40;</span>withD <span style="color: green;">&#40;</span>deriv v<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
==   <span style="color: #5d478b; font-style: italic;">{- liftA2 and (*) on functions -}</span>
   \ x -&gt; D <span style="color: green;">&#40;</span>u x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>withD <span style="color: green;">&#40;</span>deriv u<span style="color: green;">&#41;</span> x<span style="color: green;">&#41;</span> * D <span style="color: green;">&#40;</span>v x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>withD <span style="color: green;">&#40;</span>deriv v<span style="color: green;">&#41;</span> x<span style="color: green;">&#41;</span></pre>
</div></p>

<p>A sufficient definition:</p>

<p><div>
<pre class="haskell">a@<span style="color: green;">&#40;</span>D a0 a&#8217;<span style="color: green;">&#41;</span> * b@<span style="color: green;">&#40;</span>D b0 b&#8217;<span style="color: green;">&#41;</span> = D <span style="color: green;">&#40;</span>a0 + b0<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>a&#8217; * b + b&#8217; * a<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Because</p>

<p><div>
<pre class="haskell">withD u x == D <span style="color: green;">&#40;</span>u x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>withD <span style="color: green;">&#40;</span>deriv u<span style="color: green;">&#41;</span> x<span style="color: green;">&#41;</span>
&nbsp;
withD v x == D <span style="color: green;">&#40;</span>v x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>withD <span style="color: green;">&#40;</span>deriv v<span style="color: green;">&#41;</span> x<span style="color: green;">&#41;</span></pre>
</div></p>

<h4>Sine</h4>

<p>Left-hand side:</p>

<p><div>
<pre class="haskell">   withD <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sin"><span style="font-weight: bold;">sin</span></a> u<span style="color: green;">&#41;</span>
==   <span style="color: #5d478b; font-style: italic;">{- def of withD -}</span>
   <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:liftA2"><span style="font-weight: bold;">liftA2</span></a> D <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sin"><span style="font-weight: bold;">sin</span></a> u<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>withD <span style="color: green;">&#40;</span>deriv <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sin"><span style="font-weight: bold;">sin</span></a> u<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
==   <span style="color: #5d478b; font-style: italic;">{- deriv rule for sin -}</span>
   <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:liftA2"><span style="font-weight: bold;">liftA2</span></a> D <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sin"><span style="font-weight: bold;">sin</span></a> u<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>withD <span style="color: green;">&#40;</span>deriv u * <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:cos"><span style="font-weight: bold;">cos</span></a> u<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
==   <span style="color: #5d478b; font-style: italic;">{- induction for withD/(*) -}</span>
   <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:liftA2"><span style="font-weight: bold;">liftA2</span></a> D <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sin"><span style="font-weight: bold;">sin</span></a> u<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>withD <span style="color: green;">&#40;</span>deriv u<span style="color: green;">&#41;</span> * withD <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:cos"><span style="font-weight: bold;">cos</span></a> u<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
==   <span style="color: #5d478b; font-style: italic;">{- induction for withD/cos -}</span>
   <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:liftA2"><span style="font-weight: bold;">liftA2</span></a> D <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sin"><span style="font-weight: bold;">sin</span></a> u<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>withD <span style="color: green;">&#40;</span>deriv u<span style="color: green;">&#41;</span> * <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:cos"><span style="font-weight: bold;">cos</span></a> <span style="color: green;">&#40;</span>withD u<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
==   <span style="color: #5d478b; font-style: italic;">{- liftA2, sin, cos and (*) on functions -}</span>
   \ x -&gt; D <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sin"><span style="font-weight: bold;">sin</span></a> <span style="color: green;">&#40;</span>u x<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>withD <span style="color: green;">&#40;</span>deriv u<span style="color: green;">&#41;</span> x * <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:cos"><span style="font-weight: bold;">cos</span></a> <span style="color: green;">&#40;</span>withD u x<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre>
</div></p>

<p>Right-hand side:</p>

<p><div>
<pre class="haskell">   <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sin"><span style="font-weight: bold;">sin</span></a> <span style="color: green;">&#40;</span>withD u<span style="color: green;">&#41;</span>
==   <span style="color: #5d478b; font-style: italic;">{- def of withD -}</span>
   <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sin"><span style="font-weight: bold;">sin</span></a> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:liftA2"><span style="font-weight: bold;">liftA2</span></a> D u <span style="color: green;">&#40;</span>withD <span style="color: green;">&#40;</span>deriv u<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
==   <span style="color: #5d478b; font-style: italic;">{- liftA2 and sin on functions -}</span>
   \ x -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sin"><span style="font-weight: bold;">sin</span></a> <span style="color: green;">&#40;</span>D <span style="color: green;">&#40;</span>u x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>withD <span style="color: green;">&#40;</span>deriv u<span style="color: green;">&#41;</span> x<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre>
</div></p>

<p>To make the LHS and RHS final forms equal, define</p>

<p><div>
<pre class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sin"><span style="font-weight: bold;">sin</span></a> a@<span style="color: green;">&#40;</span>D a0 a&#8217;<span style="color: green;">&#41;</span> == D <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sin"><span style="font-weight: bold;">sin</span></a> a0<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>a&#8217; * <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:cos"><span style="font-weight: bold;">cos</span></a> a<span style="color: green;">&#41;</span></pre>
</div></p>

<h3>Higher-dimensional derivatives</h3>

<p>I&#8217;ll save non-scalar (&#8221;multi-variate&#8221;) differentiation for another time.
In addition to the considerations above, the key ideas are in <em><a href="http://conal.net/blog/posts/higher-dimensional-higher-order-derivatives-functionally/" title="blog post">Higher-dimensional, higher-order derivatives, functionally</a></em> and <em><a href="http://conal.net/blog/posts/simpler-more-efficient-functional-linear-maps/" title="blog post">Simpler, more efficient, functional linear maps</a></em>.</p>
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/what-is-automatic-differentiation-and-why-does-it-work/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Comparing formulations of higher-dimensional, higher-order derivatives</title>
		<link>http://conal.net/blog/posts/comparing-formulations-of-higher-dimensional-higher-order-derivatives/</link>
		<comments>http://conal.net/blog/posts/comparing-formulations-of-higher-dimensional-higher-order-derivatives/#comments</comments>
		<pubDate>Sun, 25 Jan 2009 01:40:07 +0000</pubDate>
		<dc:creator>conal</dc:creator>
		
		<category><![CDATA[Functional programming]]></category>

		<category><![CDATA[comparison]]></category>

		<category><![CDATA[derivatives]]></category>

		<category><![CDATA[linear maps]]></category>

		<category><![CDATA[math]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=77</guid>
		<description><![CDATA[





I just reread Jason Foutz&#8217;s post Higher order multivariate automatic differentiation in Haskell, as I&#8217;m thinking about this topic again.  I like his trick of using an IntMap to hold the partial derivatives and (recursively) the partials of those partials, etc.

Some thoughts:


I bet one can eliminate the constant (C) case in Jason&#8217;s representation, and [...]]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Comparing formulations of higher-dimensional, higher-order derivatives

Tags: derivatives, linear maps, math, comparison

URL: http://conal.net/blog/posts/comparing-formulations-of-higher-dimensional-higher-order-derivatives/

-->

<!-- references -->

<!-- -->

<p>I just reread Jason Foutz&#8217;s post <a href="http://metavar.blogspot.com/2008/02/higher-order-multivariate-automatic.html">Higher order multivariate automatic differentiation in Haskell</a>, as I&#8217;m thinking about this topic again.  I like his trick of using an <code>IntMap</code> to hold the partial derivatives and (recursively) the partials of those partials, etc.</p>

<p>Some thoughts:</p>

<ul>
<li><p>I bet one can eliminate the constant (<code>C</code>) case in Jason&#8217;s representation, and hence 3/4 of the cases to handle, without much loss in performance.  He already has a fairly efficient representation of constants, which is a <code>D</code> with an empty <code>IntMap</code>.</p></li>
<li><p>I imagine there&#8217;s also a nice generalization of the code for combining two finite maps used in his third multiply case.  The code&#8217;s meaning and correctness follows from a model for those maps as total functions with missing elements denoting a default value (zero in this case).</p></li>
<li><p>Jason&#8217;s data type reminds me of a sparse matrix representation, but cooler in how it&#8217;s infinitely nested.  Perhaps depth <em>n</em> (starting with zero) is a sparse <em>n-dimensional</em> matrix.</p></li>
<li><p>Finally, I suspect there&#8217;s a close connection between Jason&#8217;s <code>IntMap</code>-based implementation and my <code>LinearMap</code>-based implementation described in <em><a href="http://conal.net/blog/posts/higher-dimensional-higher-order-derivatives-functionally/" title="blog post">Higher-dimensional, higher-order derivatives, functionally</a></em> and in <em><a href="http://conal.net/blog/posts/simpler-more-efficient-functional-linear-maps/">Simpler, more efficient, functional linear maps</a></em>.  For the case of <em>R<sup>n</sup></em>, my formulation uses a trie with entries for <em>n</em> basis elements, while Jason&#8217;s uses an <code>IntMap</code> (which is also a trie) with <em>n</em> entries (counting any implicit zeros).</p></li>
</ul>

<p>I suspect Jason&#8217;s formulation is more efficient (since it optimizes the constant case), while mine is more statically typed and more flexible (since it handles more than <em>R<sup>n</sup></em>).</p>

<p>For optimizing constants, I think I&#8217;d prefer having a single constructor with a <code>Maybe</code> for the derivatives, to eliminate code duplication.</p>

<p>I am still trying to understand the paper <em><a href="http://www.bcl.hamilton.ie/~qobi/tower/papers/popl2007a.pdf" title="paper by Barak Pearlmutter and Jeffrey Siskind">Lazy Multivariate Higher-Order Forward-Mode AD</a></em>, with its management of various epsilons.</p>

<p>A final remark: I prefer the term &#8220;higher-dimensional&#8221; over the traditional &#8220;multivariate&#8221;.
I hear classic syntax/semantics confusion in the latter.</p>

<!--
**Edits**:

* 2008-02-09: just fiddling around
-->
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/comparing-formulations-of-higher-dimensional-higher-order-derivatives/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Fostering creativity by relinquishing the obvious</title>
		<link>http://conal.net/blog/posts/fostering-creativity-by-relinquishing-the-obvious/</link>
		<comments>http://conal.net/blog/posts/fostering-creativity-by-relinquishing-the-obvious/#comments</comments>
		<pubDate>Sun, 25 Jan 2009 01:08:19 +0000</pubDate>
		<dc:creator>conal</dc:creator>
		
		<category><![CDATA[Functional programming]]></category>

		<category><![CDATA[creativity]]></category>

		<category><![CDATA[curiosity]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=78</guid>
		<description><![CDATA[

&#8220;It&#8217;s obvious&#8221;

A recent thread on haskell-cafe included claims and counter-claims of what is &#8220;obvious&#8221;.

First,


  O[b]viously, bottom [⊥] is not ()


Then a second writer,


  Why is this obvious – I would argue that it&#8217;s &#8220;obvious&#8221; that bottom is () – the data type definition says there&#8217;s only one value in the type. [&#8230;]


And a [...]]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Fostering creativity by relinquishing the obvious

Tags: curiosity, creativity

URL: http://conal.net/blog/posts/fostering-creativity-by-relinquishing-the-obvious/

-->

<h3>&#8220;It&#8217;s obvious&#8221;</h3>

<p>A recent thread on haskell-cafe included claims and counter-claims of what is &#8220;obvious&#8221;.</p>

<p>First,</p>

<blockquote>
  <p>O[b]viously, bottom [⊥] is not <code>()</code></p>
</blockquote>

<p>Then a second writer,</p>

<blockquote>
  <p>Why is this obvious – I would argue that it&#8217;s &#8220;obvious&#8221; that bottom <em>is</em> <code>()</code> – the data type definition says there&#8217;s only one value in the type. [&#8230;]</p>
</blockquote>

<p>And a third,</p>

<blockquote>
  <p>It&#8217;s obvious because () is a defined value, while bottom is not - per definitionem.</p>
</blockquote>

<p>Personally, I have long suffered unaware under the affliction of &#8220;obviousness&#8221; that ⊥ is not <code>()</code>, and I suspect many others have as well.
My thanks to Bob for jostling me out of my preconceptions.
After weighing some alternatives, I might make one choice or another.
Still, I like being reminded that other possibilities are available.</p>

<p>I don&#8217;t mean to pick on these writers.
They just happened to provide at-hand examples of a communication (anti-)pattern I often hear.
Also, if you want to address the issue of whether <em>necessarily</em> <code>() /= ⊥</code>, I refer you to the haskell-cafe thread &#8220;Re: Laws and partial values&#8221;.</p>

<h3>A fallacy</h3>

<p>I understand discussions of what is &#8220;obvious&#8221; as being founded on a fallacy, namely believing that obviousness is a property of a thing itself, rather than of an individual&#8217;s or community&#8217;s mental habits (ruts).
(See <em><a href="http://www.overcomingbias.com/2008/06/2-place-and-1-p.html">2-Place and 1-Place Words</a></em>.)</p>

<h3>Creativity</h3>

<p>Still, I wondered why I get so <em>annoyed</em> about the uses of &#8220;obvious&#8221; in this discussion and in others.
(It&#8217;s never merely because <a href="http://xkcd.com/386/">&#8220;Someone is wrong on the Internet&#8221;</a>.)
Whenever I get bugged and I take the time to look under the surface of my emotional reaction, I find there&#8217;s something important &amp; beautiful to me.</p>

<p>My reaction to &#8220;obvious&#8221; comes from my seeing it as injurious to <em>creativity</em>, which is something I treasure in myself and others.
I understand &#8220;it&#8217;s obvious&#8221; to mean &#8220;I&#8217;m not curious&#8221;.
Worse yet, in a debate, I hear it as a tactic for discouraging curiosity in others.</p>

<p>For me, creativity begins with and is sustained by curiosity.
Creativity is what&#8217;s important &amp; beautiful to me here.
It&#8217;s a value instilled thoroughly &amp; joyfully in me from a very young age by my mother, as curiosity was by my father.</p>

<p>I wonder if &#8220;It&#8217;s obvious that&#8221; is one of those verbal devices that are most appealing when untrue.
For instance, &#8220;I&#8217;m sure that&#8221;, as in &#8220;I&#8217;m sure that your [sick] cat will be okay&#8221;.
Perhaps &#8220;It&#8217;s obvious that&#8221; most often means &#8220;I don&#8217;t want to imagine alternatives&#8221;or, when used in argument, &#8220;I don&#8217;t want <em>you</em> to imagine alternatives&#8221;.
Perhaps &#8220;I&#8217;m sure that&#8221; means &#8220;I&#8217;d like to be sure that&#8221;, or &#8220;I&#8217;d like you to be sure that&#8221;.</p>

<h3>In praise of curiosity</h3>

<p>Here is a quote from Albert Einstein:</p>

<blockquote>
  <p>The important thing is not to stop questioning.
  Curiosity has its own reason for existing.
  One cannot help but be in awe when he contemplates the mysteries of eternity, of life, of the marvelous structure of reality.
  It is enough if one tries merely to comprehend a little of this mystery every day.
  Never lose a holy curiosity.</p>
</blockquote>

<p>And another:</p>

<blockquote>
  <p>I have no special talents.  I am only passionately curious.</p>
</blockquote>

<p>Today I stumbled across a document that speaks to curiosity and more in a way that I resonate with: <a href="http://yudkowsky.net/rational/virtues">Twelve Virtues of Rationality</a>.  I warmly recommend it.</p>
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/fostering-creativity-by-relinquishing-the-obvious/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Lazier function definitions by merging partial values</title>
		<link>http://conal.net/blog/posts/lazier-function-definitions-by-merging-partial-values/</link>
		<comments>http://conal.net/blog/posts/lazier-function-definitions-by-merging-partial-values/#comments</comments>
		<pubDate>Wed, 21 Jan 2009 01:06:39 +0000</pubDate>
		<dc:creator>conal</dc:creator>
		
		<category><![CDATA[Functional programming]]></category>

		<category><![CDATA[lub]]></category>

		<category><![CDATA[partial value]]></category>

		<category><![CDATA[unamb]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=76</guid>
		<description><![CDATA[





This post continues from an idea of Ryan Ingram&#8217;s in an email thread How to make code least strict?.

A pretty story

Pattern matching in function definitions is very handy and has a declarative feel.
For instance,


sum &#91;&#93;     = 0
sum &#40;x:xs&#41; = x + sum xs


Simply replace &#8220;=&#8221; by &#8220;==&#8221; to read such a [...]]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Lazier function definitions by merging partial values

Tags: partial value, lub, unamb

URL: http://conal.net/blog/posts/lazier-function-definitions-by-merging-partial-values/

-->

<!-- references -->

<!-- teaser -->

<p>This post continues from an idea of Ryan Ingram&#8217;s in an email thread <em><a href="http://groups.google.com/group/fa.haskell/browse_thread/thread/56f1ec385cfb9394" title="email thread">How to make code least strict?</a></em>.</p>

<h3>A pretty story</h3>

<p>Pattern matching in function definitions is very handy and has a declarative feel.
For instance,</p>

<p><div>
<pre class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sum"><span style="font-weight: bold;">sum</span></a> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>     = <span style="color: red;">0</span>
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sum"><span style="font-weight: bold;">sum</span></a> <span style="color: green;">&#40;</span>x:xs<span style="color: green;">&#41;</span> = x + <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sum"><span style="font-weight: bold;">sum</span></a> xs</pre>
</div></p>

<p>Simply replace &#8220;<code>=</code>&#8221; by &#8220;<code>==</code>&#8221; to read such a set of pattern clauses (partial definitions) as a collection of properties specifying a <code>sum</code> function:</p>

<ul>
<li>The sum of an empty list equals zero</li>
<li>The sum of a (non-empty) list <code>x:xs</code> equals <code>x</code> plus the sum of the <code>xs</code>.</li>
</ul>

<p>Moreover, these properties <em>define</em> the <code>sum</code> function, in that <code>sum</code> is the least-defined function that satisfies these two properties.</p>

<p>Guards have a similar style and meaning:</p>

<p><div>
<pre class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:abs"><span style="font-weight: bold;">abs</span></a> x | x &lt;  <span style="color: red;">0</span> = -x
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:abs"><span style="font-weight: bold;">abs</span></a> x | x &gt;= <span style="color: red;">0</span> =  x</pre>
</div></p>

<p>Replacing &#8220;<code>=</code>&#8221; by &#8220;<code>==</code>&#8221; and guards by logical implication, we again have two properties that define <code>abs</code>:</p>

<p><div>
<pre class="haskell">x &lt;  <span style="color: red;">0</span> ==&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:abs"><span style="font-weight: bold;">abs</span></a> x == -x
x &gt;= <span style="color: red;">0</span> ==&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:abs"><span style="font-weight: bold;">abs</span></a> x ==  x</pre>
</div></p>

<h3>O, the lies!</h3>

<p>This pretty story is a lie, as becomes apparent when we look at overlapping clauses.
For instance, we&#8217;re more likely to write <code>abs</code> without the second guard:</p>

<p><div>
<pre class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:abs"><span style="font-weight: bold;">abs</span></a> x | x &lt;  <span style="color: red;">0</span> = -x
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:abs"><span style="font-weight: bold;">abs</span></a> x          =  x</pre>
</div></p>

<p>A declarative of the second clause (<em>∀ x. abs x == x</em>) is false.</p>

<p>I&#8217;d more likely write</p>

<p><div>
<pre class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:abs"><span style="font-weight: bold;">abs</span></a> x | x &lt; <span style="color: red;">0</span>     = -x
      | <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:otherwise"><span style="font-weight: bold;">otherwise</span></a> =  x</pre>
</div></p>

<p>which is all the more deceptive, since &#8220;<code>otherwise</code>&#8221; doesn&#8217;t really mean otherwise.
It&#8217;s just a synonym for &#8220;<code>True</code>&#8220;.</p>

<p>Another subtle but common problem arises with definitions like the following, as pointed out by ChrisK in <em><a href="http://groups.google.com/group/fa.haskell/browse_thread/thread/56f1ec385cfb9394" title="email thread">How to make code least strict?</a></em>:</p>

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

<p>These three clauses read like independently true properties for <code>zip</code>.
The first two clauses overlap, but their values agree, so what could possibly go wrong with a declarative reading?</p>

<p>The problem is that there are really <em>three</em> flavors of lists, not two.
This definition explicitly addresses the nil and cons cases, leaving ⊥.</p>

<p>By the definition above, the value of &#8216;<code>zip [] ⊥</code>&#8216; is indeed <code>[]</code>, which is consistent with each clause.
However, the value of &#8216;<code>zip ⊥ []</code>&#8216; is ⊥, because Haskell semantics says that each clause is tried in order, and the first clause forces evaluation of <code>⊥</code> when comparing it with <code>[]</code>.
This ⊥ value is inconsistent with reading the second clause as a property.
Swapping the first two clauses fixes the second example but breaks the first one.</p>

<p>Is it possible to fix <code>zip</code> so that its meaning is consistent with these three properties?
We seem to be stuck with an arbitrary bias, with strictness in the first or second argument.</p>

<p>Or are we?</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-76"></span></p>

<h3>Unambiguous choice</h3>

<p>Ryan Ingram suggested using <a href="http://haskell.org/haskellwiki/unamb" title="wiki page"><code>unamb</code></a>, and I agree that it is just the ticket for dilemmas like <code>zip</code> above, where we want unbiased laziness.
(See <a href="http://conal.net/blog/tag/unamb/" title="Posts on unamb">posts on unambiguous choice</a> and especially <em><a href="http://conal.net/blog/posts/functional-concurrency-with-unambiguous-choice/" title="blog post">Functional concurrency with unambiguous choice</a></em>.)
The <code>unamb</code> operator returns the more defined of its two arguments, which are required to be equal if neither is ⊥.
This precondition allows <code>unamb</code> to have a concurrent implementation with nondeterministic scheduling, while retaining simple functional (referentially transparent) semantics (over its domain of definition).</p>

<p>As Ryan said:</p>

<blockquote>

<p>Actually, I see a nice pattern here for unamb + pattern matching:</p>

<p><div>
<pre class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zip"><span style="font-weight: bold;">zip</span></a> xs ys = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldr"><span style="font-weight: bold;">foldr</span></a> unamb <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:undefined"><span style="font-weight: bold;">undefined</span></a> <span style="color: green;">&#91;</span>p1 xs ys, p2 xs ys, p3 xs ys<span style="color: green;">&#93;</span> <span style="color: #050; font-weight: bold;">where</span>
    p1 <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> _ = <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>
    p2 _ <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> = <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>
    p3 <span style="color: green;">&#40;</span>x:xs<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>y:ys<span style="color: green;">&#41;</span> = <span style="color: green;">&#40;</span>x,y<span style="color: green;">&#41;</span> : <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zip"><span style="font-weight: bold;">zip</span></a> xs ys</pre>
</div></p>

<p>Basically, split each pattern out into a separate function (which by definition is ⊥ if there is no match), then use <code>unamb</code> to combine them.</p>

<p>The invariant you need to maintain is that potentially overlapping pattern matches (<code>p1</code> and <code>p2</code>, here) must return the same result.</p>

<p>With a little typeclass hackery you could turn this into</p>

<p><div>
<pre class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zip"><span style="font-weight: bold;">zip</span></a> = unambPatterns <span style="color: green;">&#91;</span>p1,p2,p3<span style="color: green;">&#93;</span> <span style="color: #050; font-weight: bold;">where</span> <span style="color: #5d478b; font-style: italic;">{- p1, p2, p3 as above -}</span></pre>
</div></p>

</blockquote>

<p>I liked Ryan&#8217;s <code>unambPatterns</code> idea very much, and then it occurred me that the necessary &#8220;typeclass hackery&#8221; is already part of the <a href="http://haskell.org/haskellwiki/lub" title="wiki page">lub library</a>, as described in the post <em><a href="http://conal.net/blog/posts/merging-partial-values/" title="blog post">Merging partial values</a></em>.
The <code>lub</code> operator (&#8221;least upper bound&#8221;,  also written &#8220;⊔&#8221;), combines information from its arguments and is defined in different ways for different types (via a type class).
For functions,</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> HasLub b =&gt; HasLub <span style="color: green;">&#40;</span>a -&gt; b<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  f ⊔ g = \ a -&gt; f a ⊔ g a</pre>
</div></p>

<p>More succinctly, on functions, <code>(⊔) = liftA2 (⊔)</code>.</p>

<p>Using this instance twice gives a <code>(⊔)</code> suitable for curried functions of two arguments, which turns out to give us almost exactly what we want for Ryan&#8217;s <code>zip</code> definitions.</p>

<p><div>
<pre class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zip"><span style="font-weight: bold;">zip</span></a>&#8216; = lubs <span style="color: green;">&#91;</span>p1,p2,p3<span style="color: green;">&#93;</span>
 <span style="color: #050; font-weight: bold;">where</span>
   p1 <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>     _      = <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>
   p2 _      <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>     = <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>
   p3 <span style="color: green;">&#40;</span>x:xs<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>y:ys<span style="color: green;">&#41;</span> = <span style="color: green;">&#40;</span>x,y<span style="color: green;">&#41;</span> : <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zip"><span style="font-weight: bold;">zip</span></a>&#8216; xs ys</pre>
</div></p>

<p>where</p>

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

<p>The difference between <code>zip</code> and <code>zip'</code> shows up in inferred <code>HasLub</code> type constraints on <code>a</code> and <code>b</code>:</p>

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

<p>Let&#8217;s see if this definition works:</p>

<p><div>
<pre class="haskell">*Data.Lub&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zip"><span style="font-weight: bold;">zip</span></a>&#8216; <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:undefined"><span style="font-weight: bold;">undefined</span></a> :: <span style="color: green;">&#91;</span><span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #833; font-weight: bold;">Int</span></a>,<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #833; font-weight: bold;">Int</span></a><span style="color: green;">&#41;</span><span style="color: green;">&#93;</span>
<span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>
*Data.Lub&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zip"><span style="font-weight: bold;">zip</span></a>&#8216; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:undefined"><span style="font-weight: bold;">undefined</span></a> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> :: <span style="color: green;">&#91;</span><span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #833; font-weight: bold;">Int</span></a>,<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #833; font-weight: bold;">Int</span></a><span style="color: green;">&#41;</span><span style="color: green;">&#93;</span>
<span style="color: green;">&#91;</span><span style="color: green;">&#93;</span></pre>
</div></p>

<p>Next, an example that requires some recursive calls:</p>

<p><div>
<pre class="haskell">*Data.Lub&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zip"><span style="font-weight: bold;">zip</span></a>&#8216; <span style="color: green;">&#91;</span><span style="color: red;">10</span>,<span style="color: red;">20</span><span style="color: green;">&#93;</span> <span style="color: green;">&#40;</span><span style="color: red;">1</span> : <span style="color: red;">2</span> : <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:undefined"><span style="font-weight: bold;">undefined</span></a><span style="color: green;">&#41;</span>
<span style="color: green;">&#91;</span><span style="color: green;">&#40;</span><span style="color: red;">10</span>,<span style="color: red;">1</span><span style="color: green;">&#41;</span>,<span style="color: green;">&#40;</span><span style="color: red;">20</span>,<span style="color: red;">2</span><span style="color: green;">&#41;</span><span style="color: green;">&#93;</span>
*Data.Lub&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zip"><span style="font-weight: bold;">zip</span></a>&#8216; <span style="color: green;">&#40;</span><span style="color: red;">1</span> : <span style="color: red;">2</span> : <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:undefined"><span style="font-weight: bold;">undefined</span></a><span style="color: green;">&#41;</span> <span style="color: green;">&#91;</span><span style="color: red;">10</span>,<span style="color: red;">20</span><span style="color: green;">&#93;</span>
<span style="color: green;">&#91;</span><span style="color: green;">&#40;</span><span style="color: red;">1</span>,<span style="color: red;">10</span><span style="color: green;">&#41;</span>,<span style="color: green;">&#40;</span><span style="color: red;">2</span>,<span style="color: red;">20</span><span style="color: green;">&#41;</span><span style="color: green;">&#93;</span></pre>
</div></p>

<p>Lazy and bias-free!</p>

<p>If you want to try out out this example, be sure to get at least version 0.1.9 of <a href="http://haskell.org/haskellwiki/unamb" title="wiki page"><code>unamb</code></a>.
I tweaked the implementation to handle pattern-match failures gracefully.</p>

<h3>Checking totality</h3>

<p>One unfortunate aspect of this definition style is that the Haskell compiler can no longer reliably warn us about non-exhaustive pattern-based definitions.
With warnings turned on, I get the following messages:</p>

<p><div>
<pre class="haskell">Warning: Pattern match<span style="color: green;">&#40;</span>es<span style="color: green;">&#41;</span> are non-exhaustive
         In the definition <span style="color: #050; font-weight: bold;">of</span> `p1&#8242;: Patterns <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:not"><span style="font-weight: bold;">not</span></a> matched: <span style="color: green;">&#40;</span>_ : _<span style="color: green;">&#41;</span> _
&nbsp;
Warning: Pattern match<span style="color: green;">&#40;</span>es<span style="color: green;">&#41;</span> are non-exhaustive
         In the definition <span style="color: #050; font-weight: bold;">of</span> `p2&#8242;: Patterns <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:not"><span style="font-weight: bold;">not</span></a> matched: _ <span style="color: green;">&#40;</span>_ : _<span style="color: green;">&#41;</span>
&nbsp;
Warning: Pattern match<span style="color: green;">&#40;</span>es<span style="color: green;">&#41;</span> are non-exhaustive
         In the definition <span style="color: #050; font-weight: bold;">of</span> `p3&#8242;:
             Patterns <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:not"><span style="font-weight: bold;">not</span></a> matched:
                 <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> _
                 <span style="color: green;">&#40;</span>_ : _<span style="color: green;">&#41;</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span></pre>
</div></p>

<p>Perhaps a tool like Neil Mitchell&#8217;s <a href="http://www-users.cs.york.ac.uk/~ndm/catch/" title="web page: &quot;Catch: Case Totality Checker for Haskell&quot;">Catch</a> could be adapted to understand the semantics of <code>unamb</code> and <code>lub</code> (⊔) sufficiently to prove totality.</p>

<h3>More possibilities</h3>

<p>The <code>zip'</code> definition above places <code>HasLub</code> constraints on the type parameters <code>a</code> and <code>b</code>.
The origin of these constraints is the <code>HasLub</code> instance for pairs, which is roughly the following:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span>HasLub a, HasLub b<span style="color: green;">&#41;</span> =&gt; HasLub <span style="color: green;">&#40;</span>a,b<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <span style="color: green;">&#40;</span>a,b<span style="color: green;">&#41;</span> ⊔ <span style="color: green;">&#40;</span>a&#8217;,b&#8217;<span style="color: green;">&#41;</span> = <span style="color: green;">&#40;</span>a ⊔ a&#8217;, b ⊔ b&#8217;<span style="color: green;">&#41;</span></pre>
</div></p>

<p>This instance is subtly incorrect.
See <em><a href="http://conal.net/blog/posts/merging-partial-values/" title="blog post">Merging partial values</a></em>.</p>

<p>Thanks to this instance, each argument to <code>(⊔)</code> can contribute partial information to each half of the pair.
(As a special case, each argument could contribute a half.)</p>

<p>Although I haven&#8217;t run into a use for this flexibility yet, I&#8217;m confident that there are very cool uses waiting to be discovered.
Please let me know if you have any ideas.</p>
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/lazier-function-definitions-by-merging-partial-values/feed/</wfw:commentRss>
		</item>
		<item>
		<title>3D rendering as functional reactive programming</title>
		<link>http://conal.net/blog/posts/3d-rendering-as-functional-reactive-programming/</link>
		<comments>http://conal.net/blog/posts/3d-rendering-as-functional-reactive-programming/#comments</comments>
		<pubDate>Mon, 12 Jan 2009 05:38:58 +0000</pubDate>
		<dc:creator>conal</dc:creator>
		
		<category><![CDATA[Functional programming]]></category>

		<category><![CDATA[3D]]></category>

		<category><![CDATA[FRP]]></category>

		<category><![CDATA[functional reactive programming]]></category>

		<category><![CDATA[monoid]]></category>

		<category><![CDATA[semantics]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=75</guid>
		<description><![CDATA[





I&#8217;ve been playing with a simple/general semantics for 3D.
In the process, I was surprised to see that a key part of the semantics looks exactly like a key part of the semantics of functional reactivity as embodied in the library Reactive.
A closer look revealed a closer connection still, as described in this post.







What is 3D [...]]]></description>
			<content:encoded><![CDATA[<!-- 

Title: 3D rendering as functional reactive programming

Tags: 3D, semantics, FRP, functional reactive programming, monoid

nURL: http://conal.net/blog/posts/3d-rendering-as-functional-reactive-programming/

-->

<!-- references -->

<!-- teaser -->

<p>I&#8217;ve been playing with a simple/general semantics for 3D.
In the process, I was surprised to see that a key part of the semantics looks exactly like a key part of the semantics of functional reactivity as embodied in the library <em><a href="http://haskell.org/haskellwiki/Reactive" title="Wiki page for the Reactive library">Reactive</a></em>.
A closer look revealed a closer connection still, as described in this post.</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-75"></span></p>

<h3>What is 3D rendering?</h3>

<p>Most programmers think of 3D rendering as being about executing sequence of side-effects on frame buffer or some other mutable array of pixels.
This way of thinking (sequences of side-effects) comes to us from the design of early sequential computers.
Although computer hardware architecture has evolved a great deal, most programming languages, and hence most programming thinking, is still shaped by the this first sequential model.
(See John Backus&#8217;s Turing Award lecture <em><a href="www.stanford.edu/class/cs242/readings/backus.pdf" title="Turing Award lecture by John Backus">Can Programming Be Liberated from the von Neumann Style?  A functional style and its algebra of programs</a></em>.)
The invention of monadic <em><a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.53.2504" title="paper by Simon Peyton Jones and Philip Wadler">Imperative functional programming</a></em> allows Haskellers to think and program within the imperative paradigm as well.</p>

<p>What&#8217;s a <em>functional</em> alternative?
Rendering is a function from something to something else.
Let&#8217;s call these somethings (3D) &#8220;Geometry&#8221; and (2D) &#8220;Image&#8221;, where <code>Geometry</code> and <code>Image</code> are types of functional (immutable) values.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> Rendering = Image Color
&nbsp;
render :: Geometry -&gt; Rendering</pre>
</div></p>

<p>To simplify, I&#8217;m assuming a fixed view.
What remains is to define what these two types <em>mean</em> and, secondarily, how to represent and implement them.</p>

<p>An upcoming post will suggest an answer for the meaning of <code>Geometry</code>.
For now, think of it as a collection of curved and polygonal surfaces, i.e., the <em>outsides</em> (boundaries) of solid shapes.
Each point on these surfaces has a location, a normal (perpendicular direction), and material properties (determining how light is reflected by and transmitted through the surface at the point).
The geometry will contain light sources.</p>

<p>Next, what is the meaning of <code>Image</code>?
A popular answer is that an image is a rectangular array of finite-precision encodings of color (e.g., with eight bits for each of red, blue, green and possibly opacity).
This answer is leads to poor compositionality and complex meanings for operations like scaling and rotation, so I prefer another model.
As in <a href="http://conal.net/Pan" title="project web page">Pan</a>, an image (the meaning of the type <code>Image Color</code>) is a function from infinite continuous 2D space to colors, where the <code>Color</code> type includes partial opacity.
For motivation of this model and examples of its use, see <em><a href="http://conal.net/papers/functional-images/" title="book chapter">Functional images</a></em> and the corresponding <a href="http://conal.net/Pan/Gallery" title="gallery of functional images">Pan gallery</a> of functional images.
<em>Composition</em> occurs on infinite &amp; continuous images.</p>

<p>After all composition is done, the resulting image can be sampled into a finite, rectangular array of finite precision color encodings.
I&#8217;m talking about a conceptual/semantic pipeline.
The implementation computes the finite sampling without having to compute the values for entire infinite image.</p>

<p>Rendering has several components.
I&#8217;ll just address one and show how it relates to functional reactive programming (FRP).</p>

<h3>Visual occlusion</h3>

<p>One aspect of 3D rendering is <a href="en.wikipedia.org/wiki/Hidden_surface_determination">hidden surface determination</a>.
Relative to the viewer&#8217;s position and orientation, some 3D objects may fully or partially occluded by nearer objects.</p>

<p>An image is a function of (infinite and continuous) 2D space, so specifying that function is determining its value at every sample point.
Each point can correspond to a number of geometric objects, some closer and some further.
If we assume for now that our colors are fully opaque, then we&#8217;ll need to know the color (after transformation and lighting) of the <em>nearest</em> surface point that is projected onto the sample point.
(We&#8217;ll remove this opacity assumption later.)</p>

<p>Let&#8217;s consider how we&#8217;ll combine two <code>Geometry</code> values into one:</p>

<p><div>
<pre class="haskell">union :: Geometry -&gt; Geometry -&gt; Geometry</pre>
</div></p>

<p>Because of occlusion, the <code>render</code> function cannot be compositional with respect to <code>union</code>.
If it were, then there would exist a functions <code>unionR</code> such that</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">forall</span> ga gb. render <span style="color: green;">&#40;</span>ga `union` gb<span style="color: green;">&#41;</span> == render ga `unionR` render gb</pre>
</div></p>

<p>In other words, to render a union of two geometries, we can render each and combine the results.</p>

<p>The reason we can&#8217;t find such a <code>unionR</code> is that <code>render</code> doesn&#8217;t let <code>unionR</code> know how close each colored point is.
A solution then is simple: add in the missing depth information:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> RenderingD = Image <span style="color: green;">&#40;</span>Depth, Color<span style="color: green;">&#41;</span>  <span style="color: #5d478b; font-style: italic;">&#8211; first try</span>
&nbsp;
renderD :: Geometry -&gt; RenderingD</pre>
</div></p>

<p>Now we have enough information for compositional rendering, i.e., we can define <code>unionR</code> such that</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">forall</span> ga gb. renderD <span style="color: green;">&#40;</span>ga `union` gb<span style="color: green;">&#41;</span> == renderD ga `unionR` renderD gb</pre>
</div></p>

<p>where</p>

<p><div>
<pre class="haskell">unionR :: RenderingD -&gt; RenderingD -&gt; RenderingD
&nbsp;
unionR im im' p = <span style="color: #050; font-weight: bold;">if</span> d &lt;= d&#8217; <span style="color: #050; font-weight: bold;">then</span> <span style="color: green;">&#40;</span>d,c<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">else</span> <span style="color: green;">&#40;</span>d&#8217;,c&#8217;<span style="color: green;">&#41;</span>
 <span style="color: #050; font-weight: bold;">where</span>
   <span style="color: green;">&#40;</span>d ,c <span style="color: green;">&#41;</span> = im  p
   <span style="color: green;">&#40;</span>d&#8217;,c&#8217;<span style="color: green;">&#41;</span> = im&#8217; p</pre>
</div></p>

<p>When we&#8217;re done composing, we can discard the depths:</p>

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

<p>or, with <em><a href="http://conal.net/blog/posts/semantic-editor-combinators/" title="blog post">Semantic editor combinators</a></em>:</p>

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

<h3>Simpler, prettier</h3>

<p>The <code>unionR</code> is not very complicated, but still, I like to tease out common structure and reuse definitions wherever I can.
The first thing I notice about <code>unionR</code> is that it works pointwise.
That is, the value at a point is a function of the values of two other images at the same point.
The pattern is captured by <code>liftA2</code> on functions, thanks to the <code>Applicative</code> instance for functions.</p>

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

<p>So that</p>

<p><div>
<pre class="haskell">unionR = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:liftA2"><span style="font-weight: bold;">liftA2</span></a> closer
&nbsp;
closer <span style="color: green;">&#40;</span>d,c<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>d&#8217;,c&#8217;<span style="color: green;">&#41;</span> = <span style="color: #050; font-weight: bold;">if</span> d &lt;= d&#8217; <span style="color: #050; font-weight: bold;">then</span> <span style="color: green;">&#40;</span>d,c<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">else</span> <span style="color: green;">&#40;</span>d&#8217;,c&#8217;<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Or</p>

<p><div>
<pre class="haskell">closer dc@<span style="color: green;">&#40;</span>d,_<span style="color: green;">&#41;</span> dc&#8217;@<span style="color: green;">&#40;</span>d&#8217;,_<span style="color: green;">&#41;</span> = <span style="color: #050; font-weight: bold;">if</span> d &lt;= d&#8217; <span style="color: #050; font-weight: bold;">then</span> dc <span style="color: #050; font-weight: bold;">else</span> dc&#8217;</pre>
</div></p>

<p>Or even</p>

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

<p>where</p>

<p><div>
<pre class="haskell">minBy f u v = <span style="color: #050; font-weight: bold;">if</span> f u &lt;= f v <span style="color: #050; font-weight: bold;">then</span> u <span style="color: #050; font-weight: bold;">else</span> v</pre>
</div></p>

<p>This definition of <code>unionR</code> is not only simpler, it&#8217;s quite a bit more general, as type inference reveals:</p>

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

<p>Once again, simplicity and generality go hand-in-hand.</p>

<h3>Another type class morphism</h3>

<p>Let&#8217;s see if we can make <code>union</code> rendering simpler and more inevitable.
Rendering is <em>nearly</em> a homomorphism.
That is, <code>render</code> nearly distributes over <code>union</code>, but we have to replace <code>union</code> by <code>unionR</code>.
I&#8217;d rather eliminate this discrepancy, ending up with</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">forall</span> ga gb. renderD <span style="color: green;">&#40;</span>ga `op` gb<span style="color: green;">&#41;</span> == renderD ga `op` renderD gb</pre>
</div></p>

<p>for some <code>op</code> that is equal to <code>union</code> on the left and <code>unionR</code> on the right.
Since <code>union</code> and <code>unionR</code> have different types (with neither being a polymorphic instance of the other), <code>op</code> will have to be a method of some type class.</p>

<p>My favorite binary method is <code>mappend</code>, from <code>Monoid</code>, so let&#8217;s give it a try.
<code>Monoid</code> requires there also to be an identity element <code>mempty</code> and that <code>mappend</code> be associative.
For <code>Geometry</code>, we can define</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#t:Monoid"><span style="color: #833; font-weight: bold;">Monoid</span></a> Geometry <span style="color: #050; font-weight: bold;">where</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mempty"><span style="font-weight: bold;">mempty</span></a>  = emptyGeometry
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mappend"><span style="font-weight: bold;">mappend</span></a> = union</pre>
</div></p>

<p>Images with depth are a little trickier.
Image already has a <code>Monoid</code> instance, whose semantics is determined by the principle of <a href="http://conal.net/blog/tag/type-class-morphism/" title="Posts on type class morphisms">type class morphisms</a>, namely</p>

<blockquote>
  <p><em>The meaning of an instance is the instance of the meaning</em></p>
</blockquote>

<p>The meaning of an image is a function, and that functions have a <code>Monoid</code> instance:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#t:Monoid"><span style="color: #833; font-weight: bold;">Monoid</span></a> b =&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#t:Monoid"><span style="color: #833; font-weight: bold;">Monoid</span></a> <span style="color: green;">&#40;</span>a -&gt; b<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mempty"><span style="font-weight: bold;">mempty</span></a> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:const"><span style="font-weight: bold;">const</span></a> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mempty"><span style="font-weight: bold;">mempty</span></a>
  f `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mappend"><span style="font-weight: bold;">mappend</span></a>` g = \ a -&gt; f a `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mappend"><span style="font-weight: bold;">mappend</span></a>` g a</pre>
</div></p>

<p>which simplifies nicely to a standard form, by using the <code>Applicative</code> instance for functions.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Applicative"><span style="color: #833; font-weight: bold;">Applicative</span></a> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>-&gt;<span style="color: green;">&#41;</span> a<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:pure"><span style="font-weight: bold;">pure</span></a>      = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:const"><span style="font-weight: bold;">const</span></a>
  hf &lt;*&gt; xf = \ a -&gt; <span style="color: green;">&#40;</span>hf a<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>xf a<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #050; font-weight: bold;">instance</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#t:Monoid"><span style="color: #833; font-weight: bold;">Monoid</span></a> b =&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#t:Monoid"><span style="color: #833; font-weight: bold;">Monoid</span></a> <span style="color: green;">&#40;</span>a -&gt; b<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mempty"><span style="font-weight: bold;">mempty</span></a>  = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:pure"><span style="font-weight: bold;">pure</span></a>   <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mempty"><span style="font-weight: bold;">mempty</span></a>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mappend"><span style="font-weight: bold;">mappend</span></a> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:liftA2"><span style="font-weight: bold;">liftA2</span></a> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mappend"><span style="font-weight: bold;">mappend</span></a></pre>
</div></p>

<p>We&#8217;re in luck.
Since we&#8217;ve defined <code>unionR</code> as <code>liftA2 closer</code>, so we just need it to turn out that <code>closer == mappend</code> and that <code>closer</code> is associative and has an identity element.</p>

<p>However, <code>closer</code> is defined on pairs, and the standard <code>Monoid</code> instance on pairs doesn&#8217;t fit.</p>

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

<p>To avoid this conflict, define a new data type to be used in place of pairs.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">data</span> DepthG d a = Depth d a  <span style="color: #5d478b; font-style: italic;">&#8211; first try</span></pre>
</div></p>

<p>Alternatively,</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">newtype</span> DepthG d a = Depth <span style="color: green;">&#123;</span> unDepth :: <span style="color: green;">&#40;</span>d,a<span style="color: green;">&#41;</span> <span style="color: green;">&#125;</span></pre>
</div></p>

<p>I&#8217;ll go with this latter version, as it turns out to be more convenient.</p>

<p>Then we can define our monoid:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#t:Monoid"><span style="color: #833; font-weight: bold;">Monoid</span></a> <span style="color: green;">&#40;</span>DepthG d a<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mempty"><span style="font-weight: bold;">mempty</span></a>  = Depth <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:maxBound"><span style="font-weight: bold;">maxBound</span></a>,<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:undefined"><span style="font-weight: bold;">undefined</span></a><span style="color: green;">&#41;</span>
  Depth p `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mappend"><span style="font-weight: bold;">mappend</span></a>` Depth p&#8217; = Depth <span style="color: green;">&#40;</span>p `closer` p&#8217;<span style="color: green;">&#41;</span></pre>
</div></p>

<p>The second method definition can be simplified nicely</p>

<p><div>
<pre class="haskell">  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mappend"><span style="font-weight: bold;">mappend</span></a> = inDepth2 closer</pre>
</div></p>

<p>where</p>

<p><div>
<pre class="haskell">  inDepth2 = unDepth ~&gt; unDepth ~&gt; Depth</pre>
</div></p>

<p>using the ideas from <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> and the notational improvement from Matt Hellige&#8217;s <em><a href="http://matt.immute.net/content/pointless-fun" title="blog post by Matt Hellige">Pointless fun</a></em>.</p>

<h3>FRP &#8212; Future values</h3>

<p>The <code>Monoid</code> instance for <code>Depth</code> may look familiar to you if you&#8217;ve been following along with my <a href="http://conal.net/blog/tag/future-value/" title="Posts on futures values">future value</a>s or have read the paper <em><a href="http://conal.net/papers/simply-reactive" title="Paper: &quot;Simply efficient functional reactivity&quot;">Simply efficient functional reactivity</a></em>.
A <em>future value</em> has a time and a value.
Usually, the value cannot be known until its time arrives.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">newtype</span> FutureG t a = Future <span style="color: green;">&#40;</span>Time t, a<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #050; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Ord"><span style="color: #833; font-weight: bold;">Ord</span></a> t, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Bounded"><span style="color: #833; font-weight: bold;">Bounded</span></a> t<span style="color: green;">&#41;</span> =&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#t:Monoid"><span style="color: #833; font-weight: bold;">Monoid</span></a> <span style="color: green;">&#40;</span>FutureG t a<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mempty"><span style="font-weight: bold;">mempty</span></a> = Future <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:maxBound"><span style="font-weight: bold;">maxBound</span></a>, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:undefined"><span style="font-weight: bold;">undefined</span></a><span style="color: green;">&#41;</span>
  Future <span style="color: green;">&#40;</span>s,a<span style="color: green;">&#41;</span> `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mappend"><span style="font-weight: bold;">mappend</span></a>` Future <span style="color: green;">&#40;</span>t,b<span style="color: green;">&#41;</span> =
    Future <span style="color: green;">&#40;</span>s `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:min"><span style="font-weight: bold;">min</span></a>` t, <span style="color: #050; font-weight: bold;">if</span> s &lt;= t <span style="color: #050; font-weight: bold;">then</span> a <span style="color: #050; font-weight: bold;">else</span> b<span style="color: green;">&#41;</span></pre>
</div></p>

<p>When we&#8217;re using a non-lazy (flat) representation of time, this <code>mappend</code> definition can be written more simply:</p>

<p><div>
<pre class="haskell">  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mappend"><span style="font-weight: bold;">mappend</span></a> = minBy futTime
&nbsp;
  futTime <span style="color: green;">&#40;</span>Future <span style="color: green;">&#40;</span>t,_<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> = t</pre>
</div></p>

<p>Equivalently,</p>

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

<p>The <code>Time</code> type is really nothing special about time.
It is just a synonym for the <a href="http://hackage.haskell.org/packages/archive/reactive/latest/doc/html/Data-Max.html" title="module documentation"><code>Max</code> monoid</a>, as needed for the <code>Applicative</code> and <code>Monad</code> instances.</p>

<p>This connection with future values means we can discard more code.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> RenderingD d = Image <span style="color: green;">&#40;</span>FutureG d Color<span style="color: green;">&#41;</span>
renderD :: <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Ord"><span style="color: #833; font-weight: bold;">Ord</span></a> d, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Bounded"><span style="color: #833; font-weight: bold;">Bounded</span></a> d<span style="color: green;">&#41;</span> =&gt; Geometry -&gt; RenderingD d</pre>
</div></p>

<p>Now we have our monoid (homo)morphism properties:</p>

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

<p>And we&#8217;ve eliminated the code for <code>renderR</code> by reusing and existing type (future values).</p>

<h3>Future values?</h3>

<p>What does it mean to think about depth/color pairs as being &#8220;future&#8221; colors?
If we were to probe outward along a ray, say at the speed of light, we would bump into some number of 3D objects.
The one we hit earliest is the nearest, so in this sense, <code>mappend</code> on futures (choosing the earlier one) is the right tool for the job.</p>

<p>I once read that a popular belief in the past was that vision (light) reaches outward to strike objects, as I&#8217;ve just described.
I&#8217;ve forgotten where I read about that belief, though I think in a book about perspective, and I&#8217;d appreciate a pointer from someone else who might have a reference.</p>

<p>We moderns believe that light travels to us from the objects we see.
What we see of nearby objects comes from the very recent past, while of further objects we see the more remote past.
From this modern perspective, therefore, the connection I&#8217;ve made with future values is exactly backward.
Now that I think about it in this way, of course it&#8217;s backward, because we see (slightly) into the past rather than the future.</p>

<p>Fixing this conceptual flaw is simple: define a type of &#8220;past values&#8221;.
Give them exactly the same representation as future values, and deriving its class instances entirely.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">newtype</span> PastG t a = Past <span style="color: green;">&#40;</span>FutureG t a<span style="color: green;">&#41;</span>
  <span style="color: #050; font-weight: bold;">deriving</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#t:Monoid"><span style="color: #833; font-weight: bold;">Monoid</span></a>, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Functor"><span style="color: #833; font-weight: bold;">Functor</span></a>, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Applicative"><span style="color: #833; font-weight: bold;">Applicative</span></a>, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Monad"><span style="color: #833; font-weight: bold;">Monad</span></a><span style="color: green;">&#41;</span></pre>
</div></p>

<p>Alternatively, choose a temporally neutral replacement for the name &#8220;future values&#8221;.</p>

<h3>The bug in Z-buffering</h3>

<p>The <code>renderD</code> function implements continuous, infinite Z-buffering, with <code>mappend</code> performing the z-compare and conditional overwrite.
Z-buffering is the dominant algorithm used in real-time 3D graphics and is supported in hardware on even low-end graphics hardware (though not in its full continuous and infinite glory).</p>

<p>However, Z-buffering also has a serious bug: it is only correct for fully opaque colors.
Consider a geometry <code>g</code> and a point <code>p</code> in the domain of the result image.
There may be many different points in <code>g</code> that project to <code>p</code>.
If <code>g</code> has only fully opaque colors, then at most one place on <code>g</code> contributes to the rendered image at <code>p</code>, and specifically, the nearest such point.
If <code>g</code> is the <code>union</code> (<code>mappend</code>) of two other geometries, <code>g == ga `union` gb</code>, then the nearest contribution of <code>g</code> (for <code>p</code>) will be the nearer (<code>mappend</code>) of the nearest contributions of <code>ga</code> and of <code>gb</code>.</p>

<p>When colors may be <em>partially</em> opaque, the color of the rendering at a point <code>p</code> can depend on <em>all</em> of the points in the geometry that get projected to <code>p</code>.
Correct rendering in the presence of partial opacity requires a <code>fold</code> that combines all of the colors that project onto a point, <em>in order of distance</em>, where the color-combining function (alpha-blending) is <em>not</em> commutative.
Consider again <code>g == ga `union` gb</code>.
The contributions of <code>ga</code> to <code>p</code> might be entirely closer than the contributions of <code>gb</code>, or entirely further, or interleaved.
If interleaved, then the colors generated from each cannot be combined into a single color for further combination.
To handle the general case, replace the single distance/color pair with an ordered <em>collection</em> of them:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> RenderingD d = Image <span style="color: green;">&#91;</span>FutureG d Color<span style="color: green;">&#93;</span>  <span style="color: #5d478b; font-style: italic;">&#8211; multiple projections, first try</span></pre>
</div></p>

<p>Rendering a <code>union</code> (<code>mappend</code>) requires a merging of two lists of futures (distance/color pairs) into a single one.</p>

<h3>More FRP &#8212; Events</h3>

<p>Sadly, we&#8217;ve now lost our monoid morphism, because list <code>mappend</code> is <code>(++)</code>, not the required merging.
However, we can fix this problem as we did before, by introducing a new type.</p>

<p>Or, we can look for an existing type that matches our required semantics.
There is just such a thing in the <em><a href="http://haskell.org/haskellwiki/Reactive" title="Wiki page for the Reactive library">Reactive</a></em> formulation of FRP, namely an <em>event</em>.
We can simply use the FRP <code>Event</code> type:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> RenderingD d = Image <span style="color: green;">&#40;</span>EventG d Color<span style="color: green;">&#41;</span>
&nbsp;
renderD :: <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Ord"><span style="color: #833; font-weight: bold;">Ord</span></a> d, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Bounded"><span style="color: #833; font-weight: bold;">Bounded</span></a> d<span style="color: green;">&#41;</span> =&gt; Geometry -&gt; RenderingD d</pre>
</div></p>

<h3>Spatial transformation</h3>

<p>Introducing depths allowed rendering to be defined compositionally with respect to geometric union.
Is the depth model, enhanced with lists (events), sufficient for compositionality of rendering with respect to other <code>Geometry</code> operations as well?
Let&#8217;s look at spatial transformation.</p>

<p><div>
<pre class="haskell"><span style="color: green;">&#40;</span>*%<span style="color: green;">&#41;</span>  :: Transform3 -&gt; Geometry -&gt; Geometry</pre>
</div></p>

<p>Compositionally of rendering would mean that we can render <code>xf *% g</code> by rendering <code>g</code> and then using <code>xf</code> in some way to transform that rendering.
In other words there would have to exist a function <code>(*%%)</code> such that</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">forall</span> xf g. renderD <span style="color: green;">&#40;</span>xf *% g<span style="color: green;">&#41;</span> == xf *%% renderD g</pre>
</div></p>

<p>I don&#8217;t know if the required <code>(*%%)</code> function exists, or what restrictions on <code>Geometry</code> or <code>Transform3</code> it implies, or whether such a function could be useful in practice.
Instead, let&#8217;s change the type of renderings again, so that rendering can accumulate transformations and apply them to surfaces.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> RenderingDX = Transform3 -&gt; RenderingD
&nbsp;
renderDX :: <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Ord"><span style="color: #833; font-weight: bold;">Ord</span></a> d, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Bounded"><span style="color: #833; font-weight: bold;">Bounded</span></a> d<span style="color: green;">&#41;</span> =&gt; Geometry -&gt; RenderingDX d</pre>
</div></p>

<p>with or without correct treatment of partial opacity (i.e., using futures or events).</p>

<p>This new function has a simple specification:</p>

<p><div>
<pre class="haskell">renderDX g xf == renderD <span style="color: green;">&#40;</span>xf *% g<span style="color: green;">&#41;</span></pre>
</div></p>

<p>from which it follows that</p>

<p><div>
<pre class="haskell">renderD g == renderDX g identityX</pre>
</div></p>

<p>Rendering a transformed geometry then is a simple accumulation, justified as follows:</p>

<p><div>
<pre class="haskell">renderDX <span style="color: green;">&#40;</span>xfi *% g<span style="color: green;">&#41;</span>
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- specification of renderDX -}</span>
&nbsp;
\ xfo -&gt; renderD <span style="color: green;">&#40;</span>xfo *% <span style="color: green;">&#40;</span>xfi *% g<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- property of transformation -}</span>
&nbsp;
\ xfo -&gt; renderD <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>xfo `composeX` xfi<span style="color: green;">&#41;</span> *% g<span style="color: green;">&#41;</span>
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- specification of renderDX  -}</span>
&nbsp;
\ xfo -&gt; renderDX g <span style="color: green;">&#40;</span>xfo `composeX` xfi<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Render an empty geometry:</p>

<p><div>
<pre class="haskell">renderDX <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mempty"><span style="font-weight: bold;">mempty</span></a>
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- specification of renderDX -}</span>
&nbsp;
\ xf -&gt; renderD <span style="color: green;">&#40;</span>xf *% <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mempty"><span style="font-weight: bold;">mempty</span></a><span style="color: green;">&#41;</span>
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- property of (*%) and mempty -}</span>
&nbsp;
\ xf -&gt; renderD <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mempty"><span style="font-weight: bold;">mempty</span></a>
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- renderD is a monoid morphism -}</span>
&nbsp;
\ xf -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mempty"><span style="font-weight: bold;">mempty</span></a>
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- definition of pure on functions -}</span>
&nbsp;
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:pure"><span style="font-weight: bold;">pure</span></a> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mempty"><span style="font-weight: bold;">mempty</span></a>
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- definition of mempty on functions -}</span>
&nbsp;
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mempty"><span style="font-weight: bold;">mempty</span></a></pre>
</div></p>

<p>Render a geometric union:</p>

<p><div>
<pre class="haskell">renderDX <span style="color: green;">&#40;</span>ga `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mappend"><span style="font-weight: bold;">mappend</span></a>` gb<span style="color: green;">&#41;</span>
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- specification of renderDX -}</span>
&nbsp;
\ xf -&gt; renderD <span style="color: green;">&#40;</span>xf *% <span style="color: green;">&#40;</span>ga `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mappend"><span style="font-weight: bold;">mappend</span></a>` gb<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- property of transformation and union -}</span>
&nbsp;
\ xf -&gt; renderD <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>xf *% ga<span style="color: green;">&#41;</span> `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mappend"><span style="font-weight: bold;">mappend</span></a>` <span style="color: green;">&#40;</span>xf *% gb<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- renderD is a monoid morphism -}</span>
&nbsp;
\ xf -&gt; renderD <span style="color: green;">&#40;</span>xf *% ga<span style="color: green;">&#41;</span> `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mappend"><span style="font-weight: bold;">mappend</span></a>` renderD <span style="color: green;">&#40;</span>xf *% gb<span style="color: green;">&#41;</span>
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- specification of renderDX  -}</span>
&nbsp;
\ xf -&gt; renderDX ga xf `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mappend"><span style="font-weight: bold;">mappend</span></a>` renderDX gb xf
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- definition of liftA2/(&lt;*&gt;) on functions -}</span>
&nbsp;
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:liftA2"><span style="font-weight: bold;">liftA2</span></a> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mappend"><span style="font-weight: bold;">mappend</span></a> <span style="color: green;">&#40;</span>renderDX ga<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>renderDX gb<span style="color: green;">&#41;</span>
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- definition of mappend on functions -}</span>
&nbsp;
renderDX ga `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mappend"><span style="font-weight: bold;">mappend</span></a>` renderDX gb</pre>
</div></p>

<p>Hurray!
<code>renderDX</code> is still a monoid morphism.</p>

<p>The two properties of transformation and union used above say together that <code>(xf *%)</code> is a monoid morphism for all transforms <code>xf</code>.</p>
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/3d-rendering-as-functional-reactive-programming/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Another angle on functional future values</title>
		<link>http://conal.net/blog/posts/another-angle-on-functional-future-values/</link>
		<comments>http://conal.net/blog/posts/another-angle-on-functional-future-values/#comments</comments>
		<pubDate>Mon, 05 Jan 2009 04:01:05 +0000</pubDate>
		<dc:creator>conal</dc:creator>
		
		<category><![CDATA[Functional programming]]></category>

		<category><![CDATA[caching]]></category>

		<category><![CDATA[FRP]]></category>

		<category><![CDATA[functional reactive programming]]></category>

		<category><![CDATA[future value]]></category>

		<category><![CDATA[referential transparency]]></category>

		<category><![CDATA[type class morphism]]></category>

		<category><![CDATA[type composition]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=73</guid>
		<description><![CDATA[





An earlier post introduced functional future values, which are values that cannot be known until the future, but can be manipulated in the present.
That post presented a simple denotational semantics of future values simply as time/value pairs.
With a little care in the definition of Time (using the Max monoid), the instances of Functor, Applicative, Monad [...]]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Another angle on functional future values

Tags: future value, type class morphism, type composition, caching, referential transparency, FRP, functional reactive programming

URL: http://conal.net/blog/posts/another-angle-on-functional-future-values/

-->

<!-- references -->

<!-- teaser -->

<p>An earlier post introduced functional <em><a href="http://conal.net/blog/posts/future-values/" title="blog post">future values</a></em>, which are values that cannot be known until the future, but can be manipulated in the present.
That post presented a simple denotational semantics of future values simply as time/value pairs.
With a little care in the definition of <code>Time</code> (using the <a href="http://hackage.haskell.org/packages/archive/reactive/latest/doc/html/Data-Max.html" title="module documentation"><code>Max</code> monoid</a>), the instances of <code>Functor</code>, <code>Applicative</code>, <code>Monad</code> are all derived automatically.</p>

<p>A follow-up post gave an implementation of <em><a href="http://conal.net/blog/posts/future-values-via-multi-threading/" title="blog post">Future values via multi threading</a></em>.
Unfortunately, that implementation did not necessarily satisfy the semantics, because it allowed the nondeterminism of thread scheduling to leak through.
Although the implementation is usually correct, I wasn&#8217;t satisfied.</p>

<p>After a while, I hit upon an idea that really tickled me.
My original simple semantics could indeed serve as a correct and workable implementation if I used a subtler form of time that could reveal partial information.
Implementing this subtler form of time turned out to be quite tricky, and was my original motivation for the <code>unamb</code> operator described in the paper <em><a href="http://conal.net/papers/simply-reactive" title="Paper: &quot;Simply efficient functional reactivity&quot;">Simply efficient functional reactivity</a></em> and the post <em><a href="http://conal.net/blog/posts/functional-concurrency-with-unambiguous-choice/" title="blog post">Functional concurrency with unambiguous choice</a></em>.</p>

<p>It took me several days of doodling, pacing outside, and talking to myself before the idea for <code>unamb</code> broke through.
Like many of my favorite ideas, it&#8217;s simple and obvious in retrospect: to remove the ambiguity of nondeterministic choice (as in the <code>amb</code> operator), restrict its use to values that are equal when non-bottom.
Whenever we have two different methods of answering the same question (or possibly failing), we can use <code>unamb</code> to try them both.
Failures (errors or non-termination) are no problem in this context.
A more powerful variation on <code>unamb</code> is the least upper bound operator <code>lub</code>, as described in <em><a href="http://conal.net/blog/posts/merging-partial-values/" title="blog post: &quot;Merging partial values&quot;">Merging partial values</a></em>.</p>

<p>I&#8217;ve been having trouble with the <code>unamb</code> implementation.
When two (compatible) computations race, the loser gets killed so as to free up cycles that are no longer needed.
My first few implementations, however, did not recursively terminate <em>other</em> threads spawned in service of abandoned computations (from nested use of <code>unamb</code>).
I raised this problem in <em><a href="http://conal.net/blog/posts/smarter-termination-for-thread-racing/" title="blog post">Smarter termination for thread racing</a></em>, which suggested some better definitions.
In the course of several helpful reader comments, some problems with my definitions were addressed, particularly in regard to blocking and unblocking exceptions.
None of these definitions so far has done the trick reliably, and now it looks like there is a bug in the GHC run-time system.
I hope the bug (if there is one) will be fixed soon, because I&#8217;m seeing more &amp; more how <code>unamb</code> and <code>lub</code> can make functional programming even more modular (just as laziness does, as explained in <em><a href="http://www.cs.chalmers.se/~rjmh/Papers/whyfp.html" title="Paper by John Hughes">Why Functional Programming Matters</a></em>).</p>

<p>I started playing with future values and unambiguous choice as a way to implement <a href="http://haskell.org/haskellwiki/Reactive" title="Wiki page for the Reactive library">Reactive</a>, a library for functional reactive programming (FRP).
(See <em><a href="http://conal.net/blog/posts/reactive-values-from-the-future/" title="blog post">Reactive values from the future</a></em> and <em><a href="http://conal.net/papers/simply-reactive" title="Paper: &quot;Simply efficient functional reactivity&quot;">Simply efficient functional reactivity</a></em>.)
Over the last few days, I&#8217;ve given some thought to ways to implement future values that don&#8217;t need unambiguous choice.
This post describes one such alternative.</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-73"></span></p>

<h3>Futures, presently</h3>

<p>The current <code>Future</code> type is just a time and a value, wrapped in a a <code>newtype</code>:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">newtype</span> FutureG t a = Future <span style="color: green;">&#40;</span>Time t, a<span style="color: green;">&#41;</span>
  <span style="color: #050; font-weight: bold;">deriving</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Functor"><span style="color: #833; font-weight: bold;">Functor</span></a>, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Applicative"><span style="color: #833; font-weight: bold;">Applicative</span></a>, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Monad"><span style="color: #833; font-weight: bold;">Monad</span></a><span style="color: green;">&#41;</span></pre>
</div></p>

<p>Where the <code>Time</code> type is defined via the <a href="http://hackage.haskell.org/packages/archive/reactive/latest/doc/html/Data-Max.html" title="module documentation"><code>Max</code> monoid</a>.
The derived instances have exactly the intended meaning for futures, as explained in the post <em><a href="http://conal.net/blog/posts/future-values/" title="blog post">Future values</a></em> and the paper <em><a href="http://conal.net/papers/simply-reactive" title="Paper: &quot;Simply efficient functional reactivity&quot;">Simply efficient functional reactivity</a></em>.
The &#8220;G&#8221; in the name <code>FutureG</code> refers to generalized over time.</p>

<p>Note that <code>Future</code> is parameterized over both time and value.
Originally, I intended this definition as a denotational semantics of future values, but I realized that it could be a workable implementation with a lazy enough <code>t</code>.
In particular, the times have to reveal lower bounds and allow comparisons before they&#8217;re fully known.</p>

<p>Warren Burton explored an applicable notion in the 1980s, which he called &#8220;improving values&#8221;, having a concurrent implementation but deterministic functional semantics.
(See the paper <em><a href="http://journals.cambridge.org/action/displayAbstract?aid=1287720" title="paper by Warren Burton">Encapsulating nondeterminacy in an abstract data type with deterministic semantics</a></em> or the paper <em><a href="http://portal.acm.org/citation.cfm?id=99402" title="paper by Warren Burton">Indeterminate behavior with determinate semantics in parallel programs</a></em>.
I haven&#8217;t found a freely-available online copy of either.)
I adapted Warren&#8217;s idea and gave it an implementation via <code>unamb</code>.</p>

<p>Another operation finds the earlier of two futures.
This operation has an identity and is associative, so I wrapped it up as a <code>Monoid</code> instance:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Ord"><span style="color: #833; font-weight: bold;">Ord</span></a> t, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Bounded"><span style="color: #833; font-weight: bold;">Bounded</span></a> t<span style="color: green;">&#41;</span> =&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#t:Monoid"><span style="color: #833; font-weight: bold;">Monoid</span></a> <span style="color: green;">&#40;</span>FutureG t a<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mempty"><span style="font-weight: bold;">mempty</span></a> = Future <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:maxBound"><span style="font-weight: bold;">maxBound</span></a>, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:undefined"><span style="font-weight: bold;">undefined</span></a><span style="color: green;">&#41;</span>
  Future <span style="color: green;">&#40;</span>s,a<span style="color: green;">&#41;</span> `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mappend"><span style="font-weight: bold;">mappend</span></a>` Future <span style="color: green;">&#40;</span>t,b<span style="color: green;">&#41;</span> =
    Future <span style="color: green;">&#40;</span>s `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:min"><span style="font-weight: bold;">min</span></a>` t, <span style="color: #050; font-weight: bold;">if</span> s &lt;= t <span style="color: #050; font-weight: bold;">then</span> a <span style="color: #050; font-weight: bold;">else</span> b<span style="color: green;">&#41;</span></pre>
</div></p>

<p>This <code>mappend</code> definition could be written more simply:</p>

<p><div>
<pre class="haskell">  u@<span style="color: green;">&#40;</span>Future <span style="color: green;">&#40;</span>t,_<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mappend"><span style="font-weight: bold;">mappend</span></a>` u&#8217;@<span style="color: green;">&#40;</span>Future <span style="color: green;">&#40;</span>t&#8217;,_<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> = <span style="color: #050; font-weight: bold;">if</span> t &lt;= t&#8217; <span style="color: #050; font-weight: bold;">then</span> u <span style="color: #050; font-weight: bold;">else</span> u&#8217;</pre>
</div></p>

<p>However, the less simple version has more potential for laziness.
The time type might allow yielding partial information about a minimum before both of its arguments are fully known, which is the case with improving values.</p>

<h3>Futures as functions</h3>

<p>The <a href="http://haskell.org/haskellwiki/Reactive" title="Wiki page for the Reactive library">Reactive</a> library uses futures to define and implement reactivity, i.e., behaviors specified piecewise.
Simplifying away the notion of <em>events</em> for now,</p>

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

<p>The semantics (but not implementation) of <code>BehaviorG</code> is given by</p>

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

<p>The semantics of <code>until</code>:</p>

<p><div>
<pre class="haskell"><span style="color: green;">&#40;</span>b `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:until"><span style="font-weight: bold;">until</span></a>` Future <span style="color: green;">&#40;</span>t&#8217;,b&#8217;<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> `at` t = b&#8221; `at` t
 <span style="color: #050; font-weight: bold;">where</span>
   b&#8221; = <span style="color: #050; font-weight: bold;">if</span> t &lt;= t&#8217; <span style="color: #050; font-weight: bold;">then</span> b <span style="color: #050; font-weight: bold;">else</span> b&#8217;</pre>
</div></p>

<p>FRP (multi-occurrence) events are then built on top of future values, and reactivity on top of <code>until</code>.</p>

<p>The semantics of <code>until</code> shows what information we need from futures: given a time <code>t</code>, we need to know whether <code>t</code> is later than the future&#8217;s time and, <em>if so</em>, what the future&#8217;s value is.
For other purposes, we&#8217;ll also want to know the future&#8217;s time, but again, only once we&#8217;re past that time.
We might, therefore, represent futures as a function that gives exactly this information.
I&#8217;ll call this function representation &#8220;function futures&#8221; and use the prefix &#8220;S&#8221; to distinguish the original &#8220;simple&#8221; futures from these function futures.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> TryFuture t a = Time t -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Maybe"><span style="color: #833; font-weight: bold;">Maybe</span></a> <span style="color: green;">&#40;</span>S.FutureG t a<span style="color: green;">&#41;</span>
&nbsp;
tryFuture :: F.FutureG t a -&gt; TryFuture t a</pre>
</div></p>

<p>Given a probe time, <code>tryFuture</code> gives <code>Nothing</code> if the time is at or before the future&#8217;s time, or <code>Just u</code> otherwise, where <code>u</code> is the simple future.</p>

<p>We could represent <code>F.FutureG</code> simply as <code>TryFuture</code>:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> F.FutureG = TryFuture  <span style="color: #5d478b; font-style: italic;">&#8211; first try</span></pre>
</div></p>

<p>But then we&#8217;d be stuck with the <code>Functor</code> and <code>Applicative</code> instances for functions instead of futures.
Adding a <code>newtype</code> fixes that problem:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">newtype</span> FutureG t a = Future <span style="color: green;">&#123;</span> unFuture :: FutureGT t a <span style="color: green;">&#125;</span> <span style="color: #5d478b; font-style: italic;">&#8211; second try</span></pre>
</div></p>

<p>With this representation we can easily construct and try out function futures:</p>

<p><div>
<pre class="haskell">future :: TryFuture t a -&gt; FutureG t a
future = Future
&nbsp;
tryFuture :: FutureG t a -&gt; TryFuture t a
tryFuture = unFuture</pre>
</div></p>

<p>I like to define helpers for working inside representations:</p>

<p><div>
<pre class="haskell">inFuture  :: <span style="color: green;">&#40;</span>TryFuture t a -&gt; TryFuture t&#8217; a&#8217;<span style="color: green;">&#41;</span>
          -&gt; <span style="color: green;">&#40;</span>FutureG   t a -&gt; FutureG   t&#8217; a&#8217;<span style="color: green;">&#41;</span>
&nbsp;
inFuture2 :: <span style="color: green;">&#40;</span>TryFuture t a -&gt; TryFuture t&#8217; a&#8217; -&gt; TryFuture t&#8221; a&#8221;<span style="color: green;">&#41;</span>
          -&gt; <span style="color: green;">&#40;</span>FutureG   t a -&gt; FutureG   t&#8217; a&#8217; -&gt; FutureG   t&#8221; a&#8221;<span style="color: green;">&#41;</span></pre>
</div></p>

<p>The definitions of these helpers are very simple with the ideas from <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> and a lovely notation from Matt Hellige&#8217;s <em><a href="http://matt.immute.net/content/pointless-fun" title="blog post by Matt Hellige">Pointless fun</a></em>.</p>

<p><div>
<pre class="haskell">inFuture  = unFuture ~&gt; Future
&nbsp;
inFuture2 = unFuture ~&gt; inFuture 
&nbsp;
<span style="color: green;">&#40;</span>~&gt;<span style="color: green;">&#41;</span> :: <span style="color: green;">&#40;</span>a&#8217; -&gt; a<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</span>b -&gt; b&#8217;<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>a -&gt; b<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</span>a&#8217; -&gt; b&#8217;<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
g ~&gt; h = result h . argument g</pre>
</div></p>

<p>These helpers make for some easy definitions in the style of <em><a href="http://conal.net/blog/posts/semantic-editor-combinators/" title="blog post">Semantic editor combinators</a></em>:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Functor"><span style="color: #833; font-weight: bold;">Functor</span></a> <span style="color: green;">&#40;</span>FutureG t<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> = inFuture.<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a>.<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a>.<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a>
&nbsp;
<span style="color: #050; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Bounded"><span style="color: #833; font-weight: bold;">Bounded</span></a> t, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Ord"><span style="color: #833; font-weight: bold;">Ord</span></a> t<span style="color: green;">&#41;</span> =&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Applicative"><span style="color: #833; font-weight: bold;">Applicative</span></a> <span style="color: green;">&#40;</span>FutureG t<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:pure"><span style="font-weight: bold;">pure</span></a>  = Future . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:pure"><span style="font-weight: bold;">pure</span></a>.<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:pure"><span style="font-weight: bold;">pure</span></a>.<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:pure"><span style="font-weight: bold;">pure</span></a>
  <span style="color: green;">&#40;</span>&lt;*&gt;<span style="color: green;">&#41;</span> = <span style="color: green;">&#40;</span>inFuture2.<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:liftA2"><span style="font-weight: bold;">liftA2</span></a>.<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:liftA2"><span style="font-weight: bold;">liftA2</span></a><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>&lt;*&gt;<span style="color: green;">&#41;</span></pre>
</div></p>

<h4>Type composition</h4>

<p>These <code>Functor</code> and <code>Applicative</code> instances (for <code>FutureG t</code>) may look mysterious, but they have a common and inevitable form.
Every type whose representation is the (semantic and representational) composition of three functors has this style of <code>Functor</code> instance, and similarly for <code>Applicative</code>.</p>

<p>Instead of repeating this common pattern, let&#8217;s make the type composition explicit, using <a href="http://hackage.haskell.org/packages/archive/TypeCompose/0.6.3/doc/html/Control-Compose.html#2" title="module documentation"><code>Control.Compose</code></a> from the <a href="http://haskell.org/haskellwiki/TypeCompose" title="Wiki page for the TypeCompose library">TypeCompose</a> library:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> FutureG t = <span style="color: green;">&#40;</span>-&gt;<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>Time t<span style="color: green;">&#41;</span> :. <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Maybe"><span style="color: #833; font-weight: bold;">Maybe</span></a> :. S.FutureG t  <span style="color: #5d478b; font-style: italic;">&#8211; actual definition</span></pre>
</div></p>

<p>Now we can throw out <code>inFuture</code>, <code>inFuture2</code>, <code>(~&gt;)</code>, and the <code>Functor</code> and <code>Applicative</code> instances.
These instances follow from the general instances for type composition.</p>

<h4>Monoid</h4>

<p>The <code>Monoid</code> instance could also come automatically from type composition:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#t:Monoid"><span style="color: #833; font-weight: bold;">Monoid</span></a> <span style="color: green;">&#40;</span>g <span style="color: green;">&#40;</span>f a<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> =&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#t:Monoid"><span style="color: #833; font-weight: bold;">Monoid</span></a> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>g :. f<span style="color: green;">&#41;</span> a<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <span style="color: green;">&#123;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mempty"><span style="font-weight: bold;">mempty</span></a> = O <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mempty"><span style="font-weight: bold;">mempty</span></a>; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mappend"><span style="font-weight: bold;">mappend</span></a> = inO2 <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mappend"><span style="font-weight: bold;">mappend</span></a> <span style="color: green;">&#125;</span></pre>
</div></p>

<p>The <code>O</code> here is just the <code>newtype</code> constructor for <code>(:.)</code>, and the <code>inO2</code> function is similar to <code>inFuture2</code> above.</p>

<p>However, there is another often-useful <code>Monoid</code> instance:</p>

<p><div>
<pre class="haskell"><span style="color: #5d478b; font-style: italic;">&#8211; standard Monoid instance for Applicative applied to Monoid</span>
<span style="color: #050; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Applicative"><span style="color: #833; font-weight: bold;">Applicative</span></a> <span style="color: green;">&#40;</span>g :. f<span style="color: green;">&#41;</span>, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#t:Monoid"><span style="color: #833; font-weight: bold;">Monoid</span></a> a<span style="color: green;">&#41;</span> =&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#t:Monoid"><span style="color: #833; font-weight: bold;">Monoid</span></a> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>g :. f<span style="color: green;">&#41;</span> a<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <span style="color: green;">&#123;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mempty"><span style="font-weight: bold;">mempty</span></a> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:pure"><span style="font-weight: bold;">pure</span></a> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mempty"><span style="font-weight: bold;">mempty</span></a>; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mappend"><span style="font-weight: bold;">mappend</span></a> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:liftA2"><span style="font-weight: bold;">liftA2</span></a> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mappend"><span style="font-weight: bold;">mappend</span></a> <span style="color: green;">&#125;</span></pre>
</div></p>

<p>Because these two instances &#8220;overlap&#8221; are are both useful, neither one is declared in the general case.
Instead, specialized instances are declared where needed, e.g.,</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Ord"><span style="color: #833; font-weight: bold;">Ord</span></a> t, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Bounded"><span style="color: #833; font-weight: bold;">Bounded</span></a> t<span style="color: green;">&#41;</span> =&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#t:Monoid"><span style="color: #833; font-weight: bold;">Monoid</span></a> <span style="color: green;">&#40;</span>FutureG t a<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mempty"><span style="font-weight: bold;">mempty</span></a>  = <span style="color: green;">&#40;</span>  O .  O <span style="color: green;">&#41;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mempty"><span style="font-weight: bold;">mempty</span></a>   <span style="color: #5d478b; font-style: italic;">&#8211; or future mempty</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mappend"><span style="font-weight: bold;">mappend</span></a> = <span style="color: green;">&#40;</span>inO2.inO2<span style="color: green;">&#41;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mappend"><span style="font-weight: bold;">mappend</span></a></pre>
</div></p>

<p>How does the <code>Monoid</code> instance work?  Start with <code>mempty</code>.  Expanding:</p>

<p><div>
<pre class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mempty"><span style="font-weight: bold;">mempty</span></a>
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- definition -}</span> 
&nbsp;
O <span style="color: green;">&#40;</span>O <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mempty"><span style="font-weight: bold;">mempty</span></a><span style="color: green;">&#41;</span>
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- mempty on functions -}</span>
&nbsp;
O <span style="color: green;">&#40;</span>O <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:const"><span style="font-weight: bold;">const</span></a> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mempty"><span style="font-weight: bold;">mempty</span></a><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- mempty on Maybe -}</span>
&nbsp;
O <span style="color: green;">&#40;</span>O <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:const"><span style="font-weight: bold;">const</span></a> Nothing<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre>
</div></p>

<p>So, given any probe time, the empty (never-occurring) future says that it does not occur before the probe time.</p>

<p>Next, <code>mappend</code>:</p>

<p><div>
<pre class="haskell">O <span style="color: green;">&#40;</span>O f<span style="color: green;">&#41;</span> `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mappend"><span style="font-weight: bold;">mappend</span></a>` O <span style="color: green;">&#40;</span>O f&#8217;<span style="color: green;">&#41;</span>
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- mappend on FutureG -}</span>
&nbsp;
O <span style="color: green;">&#40;</span>O <span style="color: green;">&#40;</span>f `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mappend"><span style="font-weight: bold;">mappend</span></a>` f&#8217;<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- mappend on functions -}</span>
&nbsp;
O <span style="color: green;">&#40;</span>O <span style="color: green;">&#40;</span>\ t -&gt; f t `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mappend"><span style="font-weight: bold;">mappend</span></a>` f&#8217; t<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
&nbsp;
  == <span style="color: #5d478b; font-style: italic;">{- mappend on Maybe -}</span>
&nbsp;
O <span style="color: green;">&#40;</span>O <span style="color: green;">&#40;</span>\ t -&gt; f t `mappendMb` f&#8217; t<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
  <span style="color: #050; font-weight: bold;">where</span>
    Nothing `mappendMb` mb&#8217;    = mb&#8217;
    mb `mappendMb` Nothing     = mb
    Just u `mappendMb` Just u&#8217; = Just <span style="color: green;">&#40;</span>u `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mappend"><span style="font-weight: bold;">mappend</span></a>` u&#8217;<span style="color: green;">&#41;</span></pre>
</div></p>

<p>The <code>mappend</code> in this last line is on simple futures, as defined above, examining the (now known) times and choosing the earlier future.
Previously, I took special care in that <code>mappend</code> definition to enable <code>min</code> to produce information before knowing whether <code>t &lt;= t'</code>.
However, with this new approach to futures, I expect to use simple (flat) times, so it could instead be</p>

<p><div>
<pre class="haskell">u@<span style="color: green;">&#40;</span>Future <span style="color: green;">&#40;</span>s,_<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mappend"><span style="font-weight: bold;">mappend</span></a>` u&#8217;@<span style="color: green;">&#40;</span>Future <span style="color: green;">&#40;</span>s&#8217;,_<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> = <span style="color: #050; font-weight: bold;">if</span> s &lt;= s&#8217; <span style="color: #050; font-weight: bold;">then</span> u <span style="color: #050; font-weight: bold;">else</span> u&#8217;</pre>
</div></p>

<p>or</p>

<p><div>
<pre class="haskell">u `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mappend"><span style="font-weight: bold;">mappend</span></a>` u&#8217; = <span style="color: #050; font-weight: bold;">if</span> futTime u &lt;= futTime u&#8217; <span style="color: #050; font-weight: bold;">then</span> u <span style="color: #050; font-weight: bold;">else</span> u&#8217;
&nbsp;
futTime <span style="color: green;">&#40;</span>Future <span style="color: green;">&#40;</span>t,_<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> = t</pre>
</div></p>

<p>or</p>

<p><div>
<pre class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mappend"><span style="font-weight: bold;">mappend</span></a> = minBy futTime</pre>
</div></p>

<p>How does <code>mappend</code> work on function futures?
Given a test time <code>t</code>, if both futures are at least <code>t</code>, then the combined future is at least <code>t</code> (yielding <code>Nothing</code>).
If either future is before <code>t</code> and the other isn&#8217;t, then the combined future is the same as the one before <code>t</code>.
If both futures are before <code>t</code>, then the combined future is the earlier one.</p>

<h4>Relating function futures and simple futures</h4>

<p>The function-based representation of futures relates closely to the simple representation.
Let&#8217;s make this relationship explcit by defining mappings between them:</p>

<p><div>
<pre class="haskell">sToF :: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Ord"><span style="color: #833; font-weight: bold;">Ord</span></a> t =&gt; S.FutureG t a -&gt; F.FutureG t a
&nbsp;
fToS :: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Ord"><span style="color: #833; font-weight: bold;">Ord</span></a> t =&gt; F.FutureG t a -&gt; S.FutureG t a</pre>
</div></p>

<p>The first one is easy:</p>

<p><div>
<pre class="haskell">sToF u@<span style="color: green;">&#40;</span>S.Future <span style="color: green;">&#40;</span>t, _<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> =
  future <span style="color: green;">&#40;</span>\ t&#8217; -&gt; <span style="color: #050; font-weight: bold;">if</span> t&#8217; &lt;= t <span style="color: #050; font-weight: bold;">then</span> Nothing <span style="color: #050; font-weight: bold;">else</span> Just u<span style="color: green;">&#41;</span></pre>
</div></p>

<p>The reverse mapping, <code>fToS</code>, is trickier and is only defined on the image (codomain) of <code>sToF</code>.
I think it can be defined mathematically but not computationally.
There are two cases: either the function always returns <code>Nothing</code>, or there is at least one <code>t</code> for which it returns a <code>Just</code>.
If the former, then the simple future is <code>mempty</code>, which is <code>S.Future (maxBound, undefined)</code>.
If the latter, then there is only one such <code>Just</code>, and the simple future is the one in that <code>Just</code>.
Together, <code>(sToF, fToS)</code> form a projection-embedding pair.</p>

<p>We won&#8217;t really have to implement or invoke these functions.
Instead, they serve to further specify the type <code>F.FutureG</code> and the correctness of operations on it.
The representation of <code>F.FutureG</code> as given allows many values that do not correspond to futures.
To eliminate these representations, require an invariant that a function future must be the result of applying <code>sToF</code> to some simple future.</p>

<p>We&#8217;ll require that each operation preserves this invariant.
However, let&#8217;s prove something stronger, namely that operations on on <code>F.FutureG</code> correspond precisely to the same operations on <code>S.FutureG</code>, via <code>sToF</code>.
In other words, <code>sToF</code> preserves the shape of the operations on futures.
For type classes, these correspondences are the type class morphisms.
For instance, <code>sToF</code> is a <code>Monoid</code> morphism:</p>

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

<h3>Caching futures</h3>

<p>This function representation eliminates the need for tricky times (using improving values and <code>unamb</code>), but it loses the caching benefit that lazy functional programming affords to non-function representations.
Now let&#8217;s reclaim that benefit.
The trick is to exploit the restriction that every function future must be 
(semantically) the image of a simple future under <code>sToF</code>.</p>

<p>Examining the definition of <code>sToF</code>, we can deduce the following monotonicity properties of (legitimate) function futures:</p>

<ul>
<li>If the probe function yields <code>Nothing</code> for some <code>t'</code>, then it yields <code>Nothing</code> for earlier times.</li>
<li>If the probe function yields <code>Just u</code> for some <code>t'</code>, then it yields <code>Just u</code> for all later times.</li>
</ul>

<p>We can exploit these monotonicity properties by caching information as we learn it.
Caching of this sort is what distinguishes call-by-need from call-by-name and allows lazy evaluation to work efficiently for data representations.</p>

<p>Specifically, let&#8217;s save a best-known lower bound for the future time and the simple future when known.
Since the lower bound may get modified a few times, I&#8217;ll use a <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Concurrent-SampleVar.html#v:SampleVar"><code>SampleVar</code></a> (thread-safe rewritable variable).
The simple future will be discovered only once, so I&#8217;ll use an <a href="http://hackage.haskell.org/packages/archive/reactive/latest/doc/html/FRP-Reactive-Internal-IVar.html"><code>IVar</code></a>.
I&#8217;ll keep the function-future for probing when the cached information is not sufficient to answer a query.</p>

<p>Prefix this caching version with a &#8220;C&#8221;, to distinguish it from function futures (&#8221;F&#8221;) and the simple futures (&#8221;S&#8221;):</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">data</span> C.FutureG t a =
  Future <span style="color: green;">&#40;</span>SampleVar t<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>IVar <span style="color: green;">&#40;</span>S.FutureG t a<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>F.FutureG t a<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Either the simple future or the function future will be useful, so we could replace the second two fields with a single one:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">data</span> C.FutureG t a =
  Future <span style="color: green;">&#40;</span>SampleVar t<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>MVar <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Either"><span style="color: #833; font-weight: bold;">Either</span></a> <span style="color: green;">&#40;</span>F.FutureG t a<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>FF.FutureG t a<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre>
</div></p>

<p>We&#8217;ll have to be careful about multiple independent discoveries of the same simple future, which would correspond to multiple writes <code>IVar</code> with the same value.
(I imagine there are related mechanics in the GHC RTS for two threads evaluating the same thunk that would be helpful to understand.)
I guess I could use a <code>SampleVar</code> and just not worry about multiple writes, since they&#8217;d be equivalent.
For now, use the <code>IVar</code> version.</p>

<p>The caching representation relates to the function representation by means of two functions:</p>

<p><div>
<pre class="haskell">dToF :: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Ord"><span style="color: #833; font-weight: bold;">Ord</span></a>     t =&gt; C.FutureG t a -&gt; F.FutureG t a
&nbsp;
fToD :: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Bounded"><span style="color: #833; font-weight: bold;">Bounded</span></a> t =&gt; F.FutureG t a -&gt; C.FutureG t a</pre>
</div></p>

<p>The implementation</p>

<p><div>
<pre class="haskell">dToF <span style="color: green;">&#40;</span>C.Future tv uv uf<span style="color: green;">&#41;</span> =
  F.Future $ \ t&#8217; -&gt; unsafePerformIO $
    <span style="color: #050; font-weight: bold;">do</span> mb &lt;- tryReadIVar uv
       <span style="color: #050; font-weight: bold;">case</span> mb <span style="color: #050; font-weight: bold;">of</span>
         j@<span style="color: green;">&#40;</span>Just <span style="color: green;">&#40;</span>S.Future <span style="color: green;">&#40;</span>Max t,_<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> -&gt;
           <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:return"><span style="font-weight: bold;">return</span></a> <span style="color: green;">&#40;</span><span style="color: #050; font-weight: bold;">if</span> t&#8217; &lt;= t <span style="color: #050; font-weight: bold;">then</span> Nothing <span style="color: #050; font-weight: bold;">else</span> j<span style="color: green;">&#41;</span>
         Nothing        -&gt;
           <span style="color: #050; font-weight: bold;">do</span> tlo &lt;- readSampleVar tv
              <span style="color: #050; font-weight: bold;">if</span> t&#8217; &lt;= tlo <span style="color: #050; font-weight: bold;">then</span>
                 <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:return"><span style="font-weight: bold;">return</span></a> Nothing
               <span style="color: #050; font-weight: bold;">else</span>
                 <span style="color: #050; font-weight: bold;">do</span> <span style="color: #050; font-weight: bold;">let</span> mb&#8217; = F.unFuture uf t&#8217;
                    writeIVarMaybe uv mb&#8217;
                    <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:return"><span style="font-weight: bold;">return</span></a> mb&#8217;
&nbsp;
<span style="color: #5d478b; font-style: italic;">&#8211; Perhaps write to an IVar</span>
writeIVarMaybe :: IVar a -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Maybe"><span style="color: #833; font-weight: bold;">Maybe</span></a> a -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span style="color: #833; font-weight: bold;">IO</span></a> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>
writeIVarMaybe v = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:maybe"><span style="font-weight: bold;">maybe</span></a> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:return"><span style="font-weight: bold;">return</span></a> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>writeIVar v<span style="color: green;">&#41;</span>
&nbsp;
fToD uf = unsafePerformIO $
          <span style="color: #050; font-weight: bold;">do</span> tv &lt;- newSampleVar t0
             uv &lt;- newIVar
             writeIVarMaybe uv <span style="color: green;">&#40;</span>F.unFuture uf t0<span style="color: green;">&#41;</span>
             <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:return"><span style="font-weight: bold;">return</span></a> <span style="color: green;">&#40;</span>Future tv uv uf<span style="color: green;">&#41;</span>
 <span style="color: #050; font-weight: bold;">where</span>
   t0 = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:minBound"><span style="font-weight: bold;">minBound</span></a></pre>
</div></p>

<p>It&#8217;ll be handy to delegate operations to <code>F.Future</code>:</p>

<p><div>
<pre class="haskell">inF :: <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Ord"><span style="color: #833; font-weight: bold;">Ord</span></a> t, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Bounded"><span style="color: #833; font-weight: bold;">Bounded</span></a> t&#8217;<span style="color: green;">&#41;</span> =&gt;
       <span style="color: green;">&#40;</span>F.FutureG t a -&gt; F.FutureG t&#8217; a&#8217;<span style="color: green;">&#41;</span>
    -&gt; <span style="color: green;">&#40;</span>  FutureG t a -&gt;   FutureG t&#8217; a&#8217;<span style="color: green;">&#41;</span>
inF = dToF ~&gt; fToD
&nbsp;
inF2 :: <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Ord"><span style="color: #833; font-weight: bold;">Ord</span></a> t, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Bounded"><span style="color: #833; font-weight: bold;">Bounded</span></a> t&#8217;, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Ord"><span style="color: #833; font-weight: bold;">Ord</span></a> t&#8217;, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Bounded"><span style="color: #833; font-weight: bold;">Bounded</span></a> t&#8221;<span style="color: green;">&#41;</span> =&gt;
        <span style="color: green;">&#40;</span>F.FutureG t a -&gt; F.FutureG t&#8217; a&#8217; -&gt; F.FutureG t&#8221; a&#8221;<span style="color: green;">&#41;</span>
     -&gt; <span style="color: green;">&#40;</span>  FutureG t a -&gt;   FutureG t&#8217; a&#8217; -&gt;   FutureG t&#8221; a&#8221;<span style="color: green;">&#41;</span>
inF2 = dToF ~&gt; inF</pre>
</div></p>

<p>Then</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Ord"><span style="color: #833; font-weight: bold;">Ord</span></a> t, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Bounded"><span style="color: #833; font-weight: bold;">Bounded</span></a> t<span style="color: green;">&#41;</span> =&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#t:Monoid"><span style="color: #833; font-weight: bold;">Monoid</span></a> <span style="color: green;">&#40;</span>FutureG t a<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mempty"><span style="font-weight: bold;">mempty</span></a>  = fToD <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mempty"><span style="font-weight: bold;">mempty</span></a>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mappend"><span style="font-weight: bold;">mappend</span></a> = inF2 <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Monoid.html#v:mappend"><span style="font-weight: bold;">mappend</span></a>
&nbsp;
<span style="color: #050; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Ord"><span style="color: #833; font-weight: bold;">Ord</span></a> t, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Bounded"><span style="color: #833; font-weight: bold;">Bounded</span></a> t<span style="color: green;">&#41;</span> =&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Functor"><span style="color: #833; font-weight: bold;">Functor</span></a>     <span style="color: green;">&#40;</span>FutureG t<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> = inF . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a>
&nbsp;
<span style="color: #050; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Ord"><span style="color: #833; font-weight: bold;">Ord</span></a> t, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Bounded"><span style="color: #833; font-weight: bold;">Bounded</span></a> t<span style="color: green;">&#41;</span> =&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Applicative"><span style="color: #833; font-weight: bold;">Applicative</span></a> <span style="color: green;">&#40;</span>FutureG t<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:pure"><span style="font-weight: bold;">pure</span></a>  = fToD . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#v:pure"><span style="font-weight: bold;">pure</span></a>
  <span style="color: green;">&#40;</span>&lt;*&gt;<span style="color: green;">&#41;</span> = inF2 <span style="color: green;">&#40;</span>&lt;*&gt;<span style="color: green;">&#41;</span></pre>
</div></p>

<h3>Wrap-up</h3>

<p>Well, that&#8217;s the idea.
I&#8217;ve gotten as far as type-checking the code in this post, but I haven&#8217;t yet tried running it.</p>

<p>What interests me most above is the use of <code>unsafePerformIO</code> here while preserving referential transparency, thanks to the invariant on <code>F.FutureG</code> (and the consequent monotonicity property).
The heart of lazy evaluation of <em>pure</em> functional programs is just such an update, replacing a thunk with its weak head normal form (whnf).
What general principles can we construct that allow us to use efficient, destructive updating and still have referential transparency?
The important thing above seems to be the careful definition of an abstract interface such that the effect of state updates is semantically invisible through the interface.</p>
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/another-angle-on-functional-future-values/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Smarter termination for thread racing</title>
		<link>http://conal.net/blog/posts/smarter-termination-for-thread-racing/</link>
		<comments>http://conal.net/blog/posts/smarter-termination-for-thread-racing/#comments</comments>
		<pubDate>Fri, 19 Dec 2008 08:11:23 +0000</pubDate>
		<dc:creator>conal</dc:creator>
		
		<category><![CDATA[Functional programming]]></category>

		<category><![CDATA[concurrency]]></category>

		<category><![CDATA[unamb]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=72</guid>
		<description><![CDATA[





I realized in the shower this morning that there&#8217;s a serious flaw in my unamb implementation as described in Functional concurrency with unambiguous choice.
Here&#8217;s the code for racing two computations:


race :: IO a -&#62; IO a -&#62; IO a
a `race` b = do v  &#60;- newEmptyMVar
         [...]]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Smarter termination for thread racing

Tags: unamb, concurrency

URL: http://conal.net/blog/posts/smarter-termination-for-thread-racing/

-->

<!-- references -->

<!-- teaser -->

<p>I realized in the shower this morning that there&#8217;s a serious flaw in my unamb implementation as described in <em><a href="http://conal.net/blog/posts/functional-concurrency-with-unambiguous-choice/" title="blog post">Functional concurrency with unambiguous choice</a></em>.
Here&#8217;s the code for racing two computations:</p>

<p><div>
<pre class="haskell">race :: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span style="color: #833; font-weight: bold;">IO</span></a> a -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span style="color: #833; font-weight: bold;">IO</span></a> a -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span style="color: #833; font-weight: bold;">IO</span></a> a
a `race` b = <span style="color: #050; font-weight: bold;">do</span> v  &lt;- newEmptyMVar
                ta &lt;- forkPut a v
                tb &lt;- forkPut b v
                x  &lt;- takeMVar  v
                killThread ta
                killThread tb
                <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:return"><span style="font-weight: bold;">return</span></a> x
&nbsp;
forkPut :: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span style="color: #833; font-weight: bold;">IO</span></a> a -&gt; MVar a -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span style="color: #833; font-weight: bold;">IO</span></a> ThreadId
forkPut act v = forkIO <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>act &gt;&gt;= putMVar v<span style="color: green;">&#41;</span> `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:catch"><span style="font-weight: bold;">catch</span></a>` uhandler `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:catch"><span style="font-weight: bold;">catch</span></a>` bhandler<span style="color: green;">&#41;</span>
 <span style="color: #050; font-weight: bold;">where</span>
   uhandler <span style="color: green;">&#40;</span>ErrorCall <span style="color: green;">&quot;Prelude.undefined&quot;</span><span style="color: green;">&#41;</span> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:return"><span style="font-weight: bold;">return</span></a> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>
   uhandler err                             = throw err
   bhandler BlockedOnDeadMVar               = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:return"><span style="font-weight: bold;">return</span></a> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span></pre>
</div></p>

<p>The problem is that each of the threads <code>ta</code> and <code>tb</code> may have spawned other threads, directly or indirectly.
When I kill them, they don&#8217;t get a chance to kill their sub-threads.
If the parent thread does get killed, it will most likely happen during the <code>takeMVar</code>.</p>

<p>My first thought was to use some form of garbage collection of threads, perhaps akin to Henry Baker&#8217;s paper <em><a href="ftp://publications.ai.mit.edu/ai-publications/pdf/AIM-454.pdf" title="Paper by Henry Baker">The Incremental Garbage Collection of Processes</a></em>.
As with memory GC, dropping one consumer would sometimes result is cascading de-allocations.  That cascade is missing from my implementation above.</p>

<p>Or maybe there&#8217;s a simple and dependable manual solution, enhancing the method above.</p>

<p>I posted a note asking for ideas, and got the following suggestion from Peter Verswyvelen:</p>

<blockquote>
  <p>I thought that killing a thread was basically done by throwing a ThreadKilled exception using throwTo. Can&#8217;t these exception be caught?</p>
  
  <p>In C#/F# I usually use a similar technique: catch the exception that kills the thread, and perform cleanup.</p>
</blockquote>

<p>Playing with Peter&#8217;s suggestion works out very nicely, as described in this post.</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-72"></span></p>

<p>There is <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Exception.html#v:finally">a function</a> that takes a clean-up action to be executed even if the main computation is killed:</p>

<p><div>
<pre class="haskell">finally :: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span style="color: #833; font-weight: bold;">IO</span></a> a -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span style="color: #833; font-weight: bold;">IO</span></a> b -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span style="color: #833; font-weight: bold;">IO</span></a> a</pre>
</div></p>

<p>Using this function, the <code>race</code> definition becomes a little shorter and more descriptive:</p>

<p><div>
<pre class="haskell">a `race` b = <span style="color: #050; font-weight: bold;">do</span> v  &lt;- newEmptyMVar
                ta &lt;- forkPut a v
                tb &lt;- forkPut b v
                takeMVar v `finally`
                  <span style="color: green;">&#40;</span>killThread ta &gt;&gt; killThread tb<span style="color: green;">&#41;</span></pre>
</div></p>

<p>This code is vulnerable to being killed after the first forkPut and before the second one, which would then leave the first thread running.
The following variation is a bit safer:</p>

<p><div>
<pre class="haskell">a `race` b = <span style="color: #050; font-weight: bold;">do</span> v  &lt;- newEmptyMVar
                ta &lt;- forkPut a v
                <span style="color: green;">&#40;</span><span style="color: #050; font-weight: bold;">do</span> tb &lt;- forkPut b v
                    takeMVar v `finally` killThread tb<span style="color: green;">&#41;</span>
                 `finally` killThread ta</pre>
</div></p>

<p>Though I guess it&#8217;s still possible for the thread to get killed after the first fork and before the next statement begins.
Also, this code difficult to write and read.
The general pattern here is to fork a thread, do something else, and kill the thread.
Give that pattern a name:</p>

<p><div>
<pre class="haskell">forking :: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span style="color: #833; font-weight: bold;">IO</span></a> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span> -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span style="color: #833; font-weight: bold;">IO</span></a> b -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span style="color: #833; font-weight: bold;">IO</span></a> b
forking act k = <span style="color: #050; font-weight: bold;">do</span> tid &lt;- forkIO act
                   k `finally` killThread tid</pre>
</div></p>

<p>The post-fork action in both cases is to execute another action (<code>a</code> or <code>b</code>) and put the result into the mvar <code>v</code>.
Removing the <code>forkIO</code> from <code>forkPut</code>, leaves <code>putCatch</code>:</p>

<p><div>
<pre class="haskell">putCatch :: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span style="color: #833; font-weight: bold;">IO</span></a> a -&gt; MVar a -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span style="color: #833; font-weight: bold;">IO</span></a> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>
putCatch act v = <span style="color: green;">&#40;</span>act &gt;&gt;= putMVar v<span style="color: green;">&#41;</span> `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:catch"><span style="font-weight: bold;">catch</span></a>` uhandler `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:catch"><span style="font-weight: bold;">catch</span></a>` bhandler
 <span style="color: #050; font-weight: bold;">where</span>
   uhandler <span style="color: green;">&#40;</span>ErrorCall <span style="color: green;">&quot;Prelude.undefined&quot;</span><span style="color: green;">&#41;</span> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:return"><span style="font-weight: bold;">return</span></a> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>
   uhandler err                             = throw err
   bhandler BlockedOnDeadMVar               = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:return"><span style="font-weight: bold;">return</span></a> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span></pre>
</div></p>

<p>Combe <code>forking</code> and <code>putCatch</code> for convenience:</p>

<p><div>
<pre class="haskell">forkingPut :: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span style="color: #833; font-weight: bold;">IO</span></a> a -&gt; MVar a -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span style="color: #833; font-weight: bold;">IO</span></a> b -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span style="color: #833; font-weight: bold;">IO</span></a> b
forkingPut act v k = forking <span style="color: green;">&#40;</span>putCatch act v<span style="color: green;">&#41;</span> k</pre>
</div></p>

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

<p><div>
<pre class="haskell">forkingPut = <span style="color: green;">&#40;</span>result.result<span style="color: green;">&#41;</span> forking putCatch</pre>
</div></p>

<p>Now the code is tidy and safe:</p>

<p><div>
<pre class="haskell">a `race` b = <span style="color: #050; font-weight: bold;">do</span> v &lt;- newEmptyMVar
                forkingPut a v $
                  forkingPut b v $
                    takeMVar v</pre>
</div></p>

<p>Recall that there&#8217;s a very slim chance of the parent thread getting killed after spinning a child and before getting ready to kill the sub-thread (i.e., the <code>finally</code>).
If this case happens, we will not get an incorrect result.
Instead, an unnecessary thread will continue to run and write its result into an mvar that no one is reading.</p>
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/smarter-termination-for-thread-racing/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
