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

<channel>
	<title>Conal Elliott</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>
	<lastBuildDate>Wed, 25 Aug 2010 05:36:03 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>&#8220;Everything is a function&#8221; in Haskell?</title>
		<link>http://conal.net/blog/posts/everything-is-a-function-in-haskell/</link>
		<comments>http://conal.net/blog/posts/everything-is-a-function-in-haskell/#comments</comments>
		<pubDate>Wed, 04 Aug 2010 22:22:39 +0000</pubDate>
		<dc:creator>conal</dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[belief]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=175</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<!-- 

Title: "Everything is a function" in Haskell?

Tags: belief

URL: http://conal.net/blog/posts/everything-is-a-function-in-haskell/

-->

<!-- references -->

<!-- teaser -->

<p>There a belief about Haskell that keeps popping up in chat rooms and mailing lists &#8212; one that I&#8217;ve been puzzling over for a while.
One expression of the belief is &#8220;everything is a function&#8221; in Haskell.</p>

<p>Of course, there are all of these non-functions that need to be accounted for, including integers, booleans, tuples, and lists.
What about them?
A recurring answer is that such things are &#8220;functions of no arguments&#8221; or functions of a one-element type or &#8220;constant functions&#8221;.</p>

<p>I wonder about how beliefs form, spread, and solidify, and so I asked around about how people came to this notion and how they managed to hold onto it.
I had a few conjectures in mind, which I kept to myself to avoid biasing people&#8217;s responses.
Of the responses I got, some were as I&#8217;d imagined, and some were quite surprising to me, revealing some of my blind spots about others&#8217; thinking and about conversation dynamics.</p>

<p>My thanks to the many Haskellers, especially newbies, who took the time to help me understand their thought processes.
If you&#8217;re interested and in a patient mood, you can see the unedited responses on a <a href="http://www.reddit.com/r/haskell/comments/cs3gw/so_im_eating_breakfast_this_morning_posting_about/c0uu70s">Haskell reddit thread</a> and on <a href="http://tunes.org/~nef/logs/haskell/10.07.22">a #haskell IRC log</a>.
There were also a few responses on Twitter.</p>

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

<ul>
<li>2009-08-04: Added &#8220;simplify&#8221;: &#8220;Would making everything a function really simplify the formal system that is Haskell programming?&#8221;.  Thanks, SLi.</li>
<li>2009-08-04: Focus on &#8220;constant function&#8221; story for &#8220;It makes things simpler&#8221;.  I realized that I hadn&#8217;t said what I intended there.  Thanks, Jonathan Cast.</li>
</ul>

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

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

<h3>Analogy with object-oriented programming</h3>

<blockquote>
  <p>Hmm&#8230; I think it was because I was originally taught &#8216;everything is an object&#8217; in OOP.</p>
</blockquote>

<p>Many people come to functional programming (FP) after object-oriented programming (OOP).
One may have heard that in a &#8220;pure OOP&#8221; language, everything is an object.
By analogy, one could then wonder whether in a &#8220;pure FP&#8221; language, everything is a function, i.e., whether function is to functional as object is to object-oriented.</p>

<p>Perhaps such an analogy sounds plausible at first, and some people don&#8217;t give it critical thought to test whether this first guess fits with reality.
Or perhaps they&#8217;ve heard someone speak with confidence, and they confused confidence with understanding.</p>

<blockquote>
  <p>To try to answer the question about the development of beliefs: I think part of it has to do with newcomers hearing the old-hands saying it, and so it becomes part of the dogma they learn.</p>
</blockquote>

<h3>It makes things simpler</h3>

<blockquote>
  <p>It&#8217;s a unification (like electro-weak interaction). Having only one kind of things is better than having two.</p>
</blockquote>

<p>Many of us like simple, unifying principles, and we dislike exceptions.
Fewer building blocks and fewer exceptions often leads to a simpler, more compelling precise formulation, which supports more tractable reasoning (mechanized in compilers and non-mechanized in programmers).</p>

<p>This reason is perhaps in the realm of wishful thinking.
As in &#8220;it would be nice if everything were a function in Haskell, since things would be simpler that way&#8221;.</p>

<p>Would making everything a function really simplify the formal system that is Haskell programming?
For instance, what would be the impact on the type rules?</p>

<p>First, let&#8217;s separate the nullary and constant-function perspectives.
The (formal) type system has no notion of functions other than unary, so I&#8217;ll focus on the constant-function story.
For instance, that &#8220;<code>7</code>&#8221; is shorthand for &#8220;<code>\ () -&gt; 7</code>&#8220;, and so &#8220;<code>x = 7</code>&#8221; means &#8220;<code>x = \ () -&gt; 7</code>&#8220;, i.e., &#8220;<code>x () = 7</code>&#8221; (adopting the usual syntactic sugar).
(<em>Edit:</em> added this paragraph.)</p>

<p>If we did decide that values of non-function type are constant functions, then what order functions?
1st, 2nd, 3rd, &#8230;?
In other words, if 7 is a nullary or constant function, what is the result type of that function?
If the answer is, say, <code>Integer</code>, then we&#8217;d have to recall that <code>Integer</code> really means nullary or constant function whose result type is <code>Integer</code>, ad infinitum.
In a first-order functional language, this troubling question would not arise, and for imperative programmers (including OOP-ers), functions are a mainly first-order concept.</p>

<blockquote>
  <p>Why complicate your universe with non-functions when functions naturally generalize to cover them?</p>
</blockquote>

<p>Similarly, why complicate your universe with functions when non-functions naturally generalize to cover them? For instance, every value (including functions) can be promoted to a list, tree, Maybe, pair,  Id (identity <code>newtype</code> wrapper), etc.</p>

<p>But then I wonder what type of thing is in those lists, trees, maybes, etc, and what type of thing is returned from those functions.</p>

<p>And I do like static typing, which is lost when one wraps up everything into a universal type.  For instance, we lose knowing that the list or tree has exactly one element, that the Maybe is a Just, and that the function is a constant function.</p>

<h3>Mixing up functions and definitions</h3>

<p>Many times, I&#8217;ve seen Haskell programmers (perhaps mainly newbies) use the word &#8220;function&#8221; to mean &#8220;top-level definition&#8221;.
Which surprises me, as the meaning I attach to &#8220;function&#8221; has nothing to do with &#8220;top-level&#8221; or with &#8220;definition&#8221;.</p>

<p>For instance,</p>

<blockquote>
  <p>&#8216;y = 3&#8242; can be considered as a function with no arguments that returns just 3.</p>
</blockquote>

<p>I guess what&#8217;s going on here is a conflation of <em>functions</em> and (top-level) <em>definitions</em>.
Maybe because these two notions are tightly connected in C programming, where functions are always named (defined) and always at the top level and are always immutable, whereas non-functions can be defined in a nested scope and historically were mutable (before <code>const</code>).</p>

<h3>Church&#8217;s lambda calculus</h3>

<blockquote>
  <p>Well, blame Church.</p>
</blockquote>

<p>In Church&#8217;s original lambda calculus, everything is a function.
For instance,</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</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> = ∀ a. a -&gt; a -&gt; a
&nbsp;
false, true :: <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>
true  t e = t
false t e = e
&nbsp;
<span style="color: #050; font-weight: bold;">type</span> Nat  = ∀ a. <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>
&nbsp;
three :: Nat
three f = f ∘ f ∘ f</pre>
</div></p>

<p>Do some folks believe we&#8217;re still doing what Church did, i.e., to encode all data as functions and build all types out of <code>-&gt;</code>?</p>

<h3>Operational thinking</h3>

<blockquote>
  <p>Haskell&#8217;s laziness by default was what made me think of constants as functions that take no parameters.</p>
</blockquote>

<p>A <em>thunk</em> at the operational level (language implementation) is vaguely reminiscent of a <em>function</em> at the level of language specification and semantics.</p>

<p>This bit of operational reasoning had not occurred to me as a source of thinking than even non-functions are &#8220;functions&#8221;.
I was looking for denotational reasons, i.e., what things mean rather than how an implementation might work.
One of my recurring blind spots when trying to understand other&#8217;s thought processes is this sort of operational-by-default thinking.</p>

<h3>Interested in a different conversation</h3>

<p>This one was the hardest of all for me to get so far and was a real lightbulb moment for some people&#8217;s belief that everything is a function in Haskell.
They don&#8217;t believe it.
They&#8217;d rather talk about something else, such as formalisms in which everything is a function.
Or maybe they even have a different notion of what topic is being discussed.</p>

<p>And I realize I often do the same thing.
I hear a discussion topic that overlaps with one interesting to me (such as how do people form, spread, and solidify beliefs), and I focus more on the topic that interests me and less on the original topic.
And if someone doesn&#8217;t notice the switch, they can get confused.</p>

<p>Wow &#8212; this reason was in another blind spot for me.</p>

<h3>Is everything a function in Haskell?</h3>

<blockquote>
  <p>conal, that&#8217;s a funny one.  in Haskell, static types give a pretty sharp objective determination of what is a function and what isn&#8217;t</p>
</blockquote>

<p>Haskell has a precisely specified type system that defines what it means for (the value of) an expression to have function type.</p>

<h3>Reasons vs rationalizations</h3>

<blockquote>
  <p>Most of our so-called reasoning consists in finding arguments for going on believing as we already do. (James Harvey Robinson)</p>
</blockquote>

<p>Although I keep hearing &#8220;everything is a function&#8221; (and 3 is a nullary or constant function), I don&#8217;t hear people say &#8220;everything is a list&#8221;, and <code>3</code> is really the singleton list <code>[3]</code>.
Or &#8220;everything is a pair&#8221;, and <code>7</code> is really <code>(7,⊥)</code> or some such.
Or &#8220;everything is a <code>Maybe</code>&#8220;, and <code>True</code> is really <code>Just True</code>.
Personally I don&#8217;t like to equate non-functions (number, bools, trees, etc) with 0-ary functions any more than I to equate them with singleton lists (or trees, &#8230;) or non-<code>Nothing</code> <code>Maybe</code> values, etc.</p>

<p>So my best guess is that a statement like &#8220;well, 3 is a constant function&#8221; is not so much a reason as a rationalization.
In other words, not so much a means of arriving at a belief as a means of holding onto belief in the face of evidence to the contrary.
After all, one could use the same style of &#8220;reasoning&#8221; to arrive at quite different beliefs, namely that 3 is a list, pair, or <code>Maybe</code> in Haskell.
On the other hand, if the task at hand is to solidify a given belief rather than arrive at an understanding, then one would be less likely to notice that a given rationalization can be easily adapted to alternative beliefs.</p>
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/everything-is-a-function-in-haskell/feed/</wfw:commentRss>
		<slash:comments>39</slash:comments>
		</item>
		<item>
		<title>Topless data</title>
		<link>http://conal.net/blog/posts/topless-data/</link>
		<comments>http://conal.net/blog/posts/topless-data/#comments</comments>
		<pubDate>Tue, 03 Aug 2010 01:58:43 +0000</pubDate>
		<dc:creator>conal</dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[zipper]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=169</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Topless data

Tags: zipper

URL: http://conal.net/blog/posts/topless-data/

-->

<!-- references -->

<!-- teaser -->

<p>Functional programming abounds with recursively defined data types.
We often draw these structured values as trees with the root at the top and the leaves at the bottom.
<em>Lazy</em> functional programming allows values (structures) of these types to be &#8220;bottomless&#8221;, meaning we can descend forever.
There are many examples of how supporting such values gives an enormous boost to modularity.
(See, e.g., John Hughes&#8217;s classic paper <em><a href="http://www.cs.chalmers.se/~rjmh/Papers/whyfp.html" title="Paper by John Hughes">Why Functional Programming Matters</a></em>.)
We usually refer to these values as &#8220;infinite&#8221;, but I&#8217;d like to suggest &#8220;bottomless&#8221; as a more specific alternative, and to point out a limitation that perhaps is not widely noticed.</p>

<p>Although we can <em>descend</em> infinitely in lazy functional programming, we can only <em>ascend</em> finitely.
If I&#8217;m traversing a lazy list, there may be infinitely many elements on my right (yet to be visited) but only finitely many on my left (already visited).
While traversing a tree, there may be infinite paths below but only a finite one above (leading to my current position).</p>

<p>In other words, our data is bottomless, but not topless.
What would it be like to go beyond our usual merely <em>uni-infinite</em> data and program with <em>bi-infinite</em> data instead?
With data that is both bottomless and topless?</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-169"></span></p>

<p>In what sense are our current lazy data types only uni-infinite, i.e., &#8220;topped&#8221; rather than topless.
What does it even mean to <em>ascend</em> in a data structure, when all of the pointers are downward (from parent to child)?
I&#8217;m thinking of recursively defined functions, in which each descension is a recursive call (passing in a structure&#8217;s component), and each ascension is a return.
The topped nature of data is reflected in the finiteness of the call stack at every point.</p>

<p>What keeps our call stacks finite?
I guess that the stack starts out empty, and each deepening step (function call) takes some time.
In contrast, the data depth can start out infinite.</p>

<p>To get topless data, <em>start with an infinite stack</em>.</p>

<p>To apply this idea in an existing language, say Haskell, start by reifying the control stack into a lazy data structure of some sort.
For instance, a lazy list (stream).
Or a zipper!
As pointed out in <em><a href="http://conal.net/blog/posts/another-angle-on-zippers/" title="blog post">Another angle on zippers</a></em>, these two ideas are very close together.
A zipper can be represented as a list of derivatives (one-hole contexts), together with a sub-structure of focus:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> Context t = <span style="color: green;">&#91;</span>Der <span style="color: green;">&#40;</span>PF t<span style="color: green;">&#41;</span> t<span style="color: green;">&#93;</span>
&nbsp;
<span style="color: #050; font-weight: bold;">type</span> Zipper t = <span style="color: green;">&#40;</span>Context t, t<span style="color: green;">&#41;</span></pre>
</div></p>

<p>This <code>Context</code> type uses a list to allow for topped data.
For purely topless types (as in the example below), we can use a more precise type of infinite-only lists:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> Context t = Stream <span style="color: green;">&#40;</span>Der <span style="color: green;">&#40;</span>PF t<span style="color: green;">&#41;</span> t<span style="color: green;">&#41;</span></pre>
</div></p>

<p>where <code>Stream</code> comes from <a href="http://www.cse.chalmers.se/~wouter/">Wouter Swierstra</a>&#8217;s <a href="http://hackage.haskell.org/package/Stream" title="Haskell package: Stream">Stream package</a>:</p>

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

<p>Using <code>Stream</code>, we can eliminate the only failure mode in the zipper operations of <em><a href="http://conal.net/blog/posts/another-angle-on-zippers/" title="blog post">Another angle on zippers</a></em>, namely going <code>up</code> with an empty context.</p>

<p>Now let&#8217;s look at an example of a purely topless data type.</p>

<h3>Representing images</h3>

<p>I&#8217;ve noticed functional programming&#8217;s bottomless vs topless disparity over the years when thinking about functional imagery.
I like formulating imagery as functions over continuous and infinite space, i.e., infinite in detail and infinite in extent.
These properties make images more simply and generally composable than the usual implementation-oriented approach to images (discrete and finite).
(Ditto for behavior, as functions of continuous and infinite time.)</p>

<p>An easy way to implement the function-of-space model of images is directly as functions of space, i.e., use the computable functions from our programming language to represent the mathematical functions in the semantic model.
In Pan and a few following systems, I used a variant of this simple implementation approach, using Haskell functions that generate <em>expressions</em> to be compiled into highly optimized low-level code.</p>

<p>Representing functions as functions has the benefit of simplicity.
Ironically, however, <em>functional</em> programming penalizes the use of <em>functions</em> as a representation.
Other than functions, data representations cache their components.
For instance, if I extract the second element of a triple twice, that element will be computed only once.
In contrast, if I apply a function twice to the second value in a three-value domain (e.g., <code>Cold</code>, <code>Medium</code>, <code>Hot</code>), then the result will be computed twice, not once.</p>

<p>One way to avoid this penalty is <a href="http://conal.net/blog/tag/memoization/" title="Posts on memoization">memoization</a>, which is the conversion of functions into data structures.
However, I think memoization isn&#8217;t quite what I&#8217;m looking for to implement imagery or behavior.
In those cases, the function domains are <em>continuous</em>, and the likelihood of a repeated sampling at exactly the same domain points is vanishingly small.
Well, that statement isn&#8217;t true, as it assumes uniformly random sampling over time &amp; space, but often some repeated sampling occurs by construction, and caching has been useful in function-based FRP implementations.</p>

<p>For imagery, I like to pan, zoom, and rotate interactively.
The simplest implementation model I know samples an image once for each pixel.
A variation, e.g. used in <a href="http://conal.net/Pan" title="project web page">Pan</a> and <a href="http://conal.net/Pajama" title="project web page">Pajama</a>, is to sample several times per pixel for progressive, stochastic anti-aliasing.
One could perhaps go even further and use something like <a href="http://conal.net/blog/posts/exact-numeric-integration/" title="blog post">exact numeric integration</a> to anti-alias perfectly.</p>

<p>Being function-centered, these implementation styles have tremendous waste, computing each display frame (sampling in discrete time &amp; space) from scratch, reusing no sampling work from previous frames, even though the resulting images are very similar while interactively perusing.
Modern graphics hardware can pan, zoom, and rotate (PZR) discrete finite samplings (&#8220;bitmaps&#8221;) with modest filtering <em>very</em> fast.</p>

<p>I want to exploit that hardware without compromising the simple denotational model of continuous &amp; discrete images.
An idea I&#8217;ve been playing with is to use an image pyramid, which is a stack of progressively higher resolution samplings, as in hardware mip-maps.
Those samplings can be organized into an infinite quadtree.
Each quadtree level has a texture map (bitmap stored in graphics memory), and all texture maps have the same of samples (pixels), e.g., 256 × 256.
In addition to a texture map, each quadtree has four subtrees, corresponding to four spatial subquadrants.
Abstracting out the texture map, we might write</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">data</span> Quadtree a = Quadtree a <span style="color: green;">&#40;</span>Quad <span style="color: green;">&#40;</span>Quadtree a<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #050; font-weight: bold;">type</span> Quad t = <span style="color: green;">&#40;</span>t,t,t,t<span style="color: green;">&#41;</span></pre>
</div></p>

<p>It&#8217;s fairly easy to write a lazy functional recursive renderer that converts a function-style image into a quadtree.
The recursive argument to this renderer is a square region of space.
To display such a tree interactively, at each frame pick a level that suits the current zoom factor, e.g., so that one sample corresponds roughly to one pixel.
Rendering a single frame involves choosing a subset of the quadtree&#8217;s texture maps and displaying them with hardware-accelerated adjustments for PZR.</p>

<p>As an important optimization, reject any subtrees that are outside of the current view window, given the current view transformation (pan vector, zoom factor, and rotation angle).
Thanks to laziness, this structure will get computed and filled in only as it&#8217;s accessed.
<a href="http://www.cs.sfu.ca/~burton/">Warren Burton</a> explored lazy functional quadtrees in the 1980s.
See, e.g., <em>Functional programming and quadtrees</em> by F. W. Burton and J. G. Kollias in IEEE Software 6(1):90-97, January 1989.</p>

<p>There&#8217;s a problem with this idea of using quadtrees for infinite continuous images.
The image representation captures the <em>continuous</em> aspect but not the infinite aspect.
Or more precisely, it captures infinite resolution but not infinite extent.
Descending in a spatial quadtree doubles the resolution (in each direction) and halves the extent.
Conversely, ascending halves the resolution and doubles the extent.
To find a collection of texture maps of a desired resolution and spatial region, ascend high enough to encompass the region, and then descend low enough to get the required resolution.</p>

<p>So, just as bottomlessness gives rise to infinite resolution, toplessness would give rise to infinite extent.</p>

<p>For functions of continuous &amp; infinite time, the domain is 1D instead of 2D, so replace quadtrees and their zippers by binary trees and their zippers.
(See <em><a href="http://conal.net/blog/posts/sequences-segments-and-signals/" title="blog post">Sequences, segments, and signals</a></em> for related remarks.)</p>

<h3>Aside: thought tools</h3>

<p>I have a prototype implementation of the lazy quadtree scheme in Objective C for the iPhone OS.
Given the clumsiness of Objective C for functional programming, and particularly the language&#8217;s weak compile-time typing and lack of parametric polymorphism, I switched to Haskell to work out the zipper aspect.</p>

<p>Once I was writing Haskell, the imperative machinery faded from view and I couldn&#8217;t help but start seeing essential patterns.
Tinkering with those patterns led me to new insights, including the ones described in my recent series of posts on higher-order types, derivatives, and zippers.
I am grateful to have this higher-order lazy language with rich static typing as a <em>thought tool</em>, to help me gain insights and weed out my mistakes.
It&#8217;s a bonus to me that the language is executable as well.</p>

<h3>Conclusion</h3>

<p>This idea of topless data (with its infinite call stacks) has been bumping around in my mind for 15 years or so.
It was probably inspired in part by Mitch Wand&#8217;s 1980 paper <em>[Continuation-Based Program Transformation Strategies]</em>, which I read in grad school.
That article is on my short list of research writings that have most influenced me.
If you haven&#8217;t read it, or haven&#8217;t read it recently, please do.</p>

<p>In finishing this blog post, I&#8217;m a little uncomfortable with the fuzziness of what I&#8217;ve presented.
Buckminster Fuller said &#8220;I call intuition cosmic fishing. You feel a nibble, then you&#8217;ve got to hook the fish.&#8221;
Topless data has been a helpful intuition for me.
I&#8217;m not sure I&#8217;ve quite hooked the fish, but perhaps you&#8217;ll catch something useful to you in this post anyway.</p>
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/topless-data/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Another angle on zippers</title>
		<link>http://conal.net/blog/posts/another-angle-on-zippers/</link>
		<comments>http://conal.net/blog/posts/another-angle-on-zippers/#comments</comments>
		<pubDate>Thu, 29 Jul 2010 17:06:10 +0000</pubDate>
		<dc:creator>conal</dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[derivative]]></category>
		<category><![CDATA[functor]]></category>
		<category><![CDATA[zipper]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=154</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Another angle on zippers

Tags: derivative, functor, zipper

URL: http://conal.net/blog/posts/another-angle-on-zippers/

-->

<!-- references -->

<!-- teaser -->

<p>The zipper is an efficient and elegant data structure for purely functional editing of tree-like data structures, <a href="http://www.st.cs.uni-saarland.de/edu/seminare/2005/advanced-fp/docs/huet-zipper.pdf" title="paper by Gérard Huet">first published by Gérard Huet</a>.
Zippers maintain a location of focus in a tree and support navigation operations (up, down, left, right) and editing (replace current focus).</p>

<p>The original zipper type and operations are customized for a single type, but it&#8217;s not hard to see how to adapt to other tree-like types, and hence to regular data types.
There have been many follow-up papers to <em><a href="http://www.st.cs.uni-saarland.de/edu/seminare/2005/advanced-fp/docs/huet-zipper.pdf" title="paper by Gérard Huet">The Zipper</a></em>, including a polytypic version in the paper <em><a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.1.6342" title="paper by Ralf Hinze, Johan Jeuring, and Andres Löh">Type-indexed data types</a></em>.</p>

<p>All of the zipper adaptations and generalizations I&#8217;ve seen so far maintain the original navigation interface.
In this post, I propose an alternative interface that appears to significantly simplify matters.
There are only two navigation functions instead of four, and each of the two is specified and implemented via a fairly simple one-liner.</p>

<p>I haven&#8217;t used this new zipper formulation in an application yet, so I do not know whether some usefulness has been lost in simplifying the interface.</p>

<p>The code in this blog post is taken from the Haskell library <a href="http://hackage.haskell.org/package/functor-combo" title="Hackage entry: functor-combo">functor-combo</a> and completes the <code>Holey</code> type class introduced in <em><a href="http://conal.net/blog/posts/differentiation-of-higher-order-types/" title="blog post">Differentiation of higher-order types</a></em>.</p>

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

<ul>
<li>2010-07-29: Removed some stray <code>Just</code> applications in <code>up</code> definitions.  (Thanks, illissius.)</li>
<li>2010-07-29: Augmented my complicated definition of <code>tweak2</code> with a much simpler version from Sjoerd Visscher.</li>
<li>2010-07-29: Replaced <code>fmap (first (:ds'))</code> with <code>(fmap.first) (:ds')</code> in <code>down</code> definitions.  (Thanks, Sjoerd.)</li>
</ul>

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

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

<h3>Extraction</h3>

<p>The post <em><a href="http://conal.net/blog/posts/differentiation-of-higher-order-types/" title="blog post">Differentiation of higher-order types</a></em> gave part of a type class for one-hole contexts (functor derivatives) and the filling of those contexts:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">class</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> f ⇒ Holey f <span style="color: #050; font-weight: bold;">where</span>
  <span style="color: #050; font-weight: bold;">type</span> Der f :: * → *
  fillC :: Der f a → a → f a</pre>
</div></p>

<p>The arguments of <code>fillC</code> correspond roughly to the components of what Gérard Huet called a &#8220;location&#8221;, namely context and something to fill the context:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> Loc f a = <span style="color: green;">&#40;</span>Der f a, a<span style="color: green;">&#41;</span></pre>
</div></p>

<p>So an alternative hole-filling interface is</p>

<p><div>
<pre class="haskell">fill :: Holey f ⇒ Loc f a → f a
fill = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:uncurry"><span style="font-weight: bold;">uncurry</span></a> fillC</pre>
</div></p>

<p>Now consider a reverse operation, a kind of <em>extraction</em>:</p>

<p><div>
<pre class="haskell">guess1 :: f a → Loc f a</pre>
</div></p>

<p>There&#8217;s an awkward problem here.
What if <code>f a</code> has more than one possible hole, or has no hole at all?
If more than one, then which do we pick?
Perhaps the left-most.
If none, then we might want to have a failure representation, e.g.,</p>

<p><div>
<pre class="haskell">guess2 :: f a → <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>Loc f a<span style="color: green;">&#41;</span></pre>
</div></p>

<p>To handle the more-than-one possibility, we could add another method for traversing the various extractions, like the <code>go_right</code> operation in <em><a href="http://www.st.cs.uni-saarland.de/edu/seminare/2005/advanced-fp/docs/huet-zipper.pdf" title="paper by Gérard Huet">The Zipper</a></em>, section 2.2.
I don&#8217;t know what changes we&#8217;d have to make to the <code>Loc</code> type.</p>

<p>We could instead use a list of possible extractions.</p>

<p><div>
<pre class="haskell">guess3 :: f a → <span style="color: green;">&#91;</span>Loc f a<span style="color: green;">&#93;</span></pre>
</div></p>

<p>Why a <em>list</em>?
I guess because it&#8217;s in our habitual functional toolbox, and it covers any number of alternative extracted locations.
On the other hand, our toolbox is growing, and sometimes list isn&#8217;t the best functor for the job.
For instance, we might use a finger tree, which has better performance for some sequence operations.</p>

<p>Or we could a functor closer at hand, namely <code>f</code> itself.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">class</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> f ⇒ Holey f <span style="color: #050; font-weight: bold;">where</span>
  <span style="color: #050; font-weight: bold;">type</span> Der f :: * → *
  fillC   :: Der f a → a → f a
  extract :: f a → f <span style="color: green;">&#40;</span>Loc f a<span style="color: green;">&#41;</span></pre>
</div></p>

<p>For instance, when <code>f ≡ []</code>, <code>extract</code> returns a list of extractions; and when <code>f ≡ Id :*: Id</code>, <code>extract</code> returns a pair of extractions.</p>

<h3>How to extract</h3>

<p>A constant functor has void derivative.
Extraction yields another constant structure, with the same data but a different type:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> Holey <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Const"><span style="color: #833; font-weight: bold;">Const</span></a> x<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <span style="color: #050; font-weight: bold;">type</span> Der <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Const"><span style="color: #833; font-weight: bold;">Const</span></a> x<span style="color: green;">&#41;</span> = Void
  fillC = voidF
  extract <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Const"><span style="color: #833; font-weight: bold;">Const</span></a> x<span style="color: green;">&#41;</span> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Const"><span style="color: #833; font-weight: bold;">Const</span></a> x</pre>
</div></p>

<p>The identity functor has exactly one opportunity for a hole, leaving no information behind:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> Holey Id <span style="color: #050; font-weight: bold;">where</span>
  <span style="color: #050; font-weight: bold;">type</span> Der Id = Unit
  fillC <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Const"><span style="color: #833; font-weight: bold;">Const</span></a> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> = Id
  extract <span style="color: green;">&#40;</span>Id a<span style="color: green;">&#41;</span> = Id <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Const"><span style="color: #833; font-weight: bold;">Const</span></a> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>, a<span style="color: green;">&#41;</span></pre>
</div></p>

<p>The definitions of <code>Der</code> and <code>fillC</code> above and below are lifted directly from <em><a href="http://conal.net/blog/posts/differentiation-of-higher-order-types/" title="blog post">Differentiation of higher-order types</a></em>.</p>

<p>For sums, there are two cases: <code>InL fa, InR ga :: (f :+: g) a</code>.
Starting with the first case:</p>

<p><div>
<pre class="haskell">InL fa :: <span style="color: green;">&#40;</span>f :+: g<span style="color: green;">&#41;</span> a
&nbsp;
fa :: f a
&nbsp;
extract fa :: f <span style="color: green;">&#40;</span>Loc f a<span style="color: green;">&#41;</span>
&nbsp;
           :: f <span style="color: green;">&#40;</span>Der f a, a<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a>.<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:first"><span style="font-weight: bold;">first</span></a><span style="color: green;">&#41;</span> InL <span style="color: green;">&#40;</span>extract fa<span style="color: green;">&#41;</span> :: f <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>Der f :+: Der g<span style="color: green;">&#41;</span> a, a<span style="color: green;">&#41;</span>
&nbsp;
                              :: f <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>Der <span style="color: green;">&#40;</span>f :+: g<span style="color: green;">&#41;</span> a<span style="color: green;">&#41;</span>, a<span style="color: green;">&#41;</span></pre>
</div></p>

<p>See <em><a href="http://conal.net/blog/posts/semantic-editor-combinators/" title="blog post">Semantic editor combinators</a></em> for an explanation of <code>(fmap.first)</code> friends.
Continuing, apply the definition of <code>Der</code> on sums:</p>

<p><div>
<pre class="haskell">InL <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:fmap"><span style="font-weight: bold;">fmap</span></a>.<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:first"><span style="font-weight: bold;">first</span></a><span style="color: green;">&#41;</span> InL <span style="color: green;">&#40;</span>extract fa<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> :: <span style="color: green;">&#40;</span>f :+: g<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>Der <span style="color: green;">&#40;</span>f :+: g<span style="color: green;">&#41;</span> a<span style="color: green;">&#41;</span>, a<span style="color: green;">&#41;</span>
&nbsp;
                                    :: <span style="color: green;">&#40;</span>f :+: g<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>Loc <span style="color: green;">&#40;</span>f :+: g<span style="color: green;">&#41;</span> a<span style="color: green;">&#41;</span></pre>
</div></p>

<p>The two steps that introduce <code>g</code> are motivated by the required type of <code>extract</code>.
Similarly, for the second case:</p>

<p><div>
<pre class="haskell">InR <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:fmap"><span style="font-weight: bold;">fmap</span></a>.<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:first"><span style="font-weight: bold;">first</span></a><span style="color: green;">&#41;</span> InR <span style="color: green;">&#40;</span>extract ga<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> :: <span style="color: green;">&#40;</span>f :+: g<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>Loc <span style="color: green;">&#40;</span>f :+: g<span style="color: green;">&#41;</span> a<span style="color: green;">&#41;</span></pre>
</div></p>

<p>So,</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span>Holey f, Holey g<span style="color: green;">&#41;</span> ⇒ Holey <span style="color: green;">&#40;</span>f :+: g<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <span style="color: #050; font-weight: bold;">type</span> Der <span style="color: green;">&#40;</span>f :+: g<span style="color: green;">&#41;</span> = Der f :+: Der g
  fillC <span style="color: green;">&#40;</span>InL df<span style="color: green;">&#41;</span> = InL ∘ fillC df
  fillC <span style="color: green;">&#40;</span>InR df<span style="color: green;">&#41;</span> = InR ∘ fillC df
  extract <span style="color: green;">&#40;</span>InL fa<span style="color: green;">&#41;</span> = InL <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:fmap"><span style="font-weight: bold;">fmap</span></a>.<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:first"><span style="font-weight: bold;">first</span></a><span style="color: green;">&#41;</span> InL <span style="color: green;">&#40;</span>extract fa<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
  extract <span style="color: green;">&#40;</span>InR ga<span style="color: green;">&#41;</span> = InR <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:fmap"><span style="font-weight: bold;">fmap</span></a>.<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:first"><span style="font-weight: bold;">first</span></a><span style="color: green;">&#41;</span> InR <span style="color: green;">&#40;</span>extract ga<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre>
</div></p>

<p>For products, recall the derivative type:</p>

<p><div>
<pre class="haskell">  <span style="color: #050; font-weight: bold;">type</span> Der <span style="color: green;">&#40;</span>f :*: g<span style="color: green;">&#41;</span> = Der f :*: g  :+:  f :*: Der g</pre>
</div></p>

<p>To extract from a product, we extract from either component and then pair with the other component.
The form of an argument to <code>extract</code> is</p>

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

<p>Again, start with the left part:</p>

<p><div>
<pre class="haskell">fa :: f a
&nbsp;
extract fa :: f <span style="color: green;">&#40;</span>Loc f a<span style="color: green;">&#41;</span>
           :: f <span style="color: green;">&#40;</span>Der f a, a<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a>.<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:first"><span style="font-weight: bold;">first</span></a><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>:*: ga<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>extract fa<span style="color: green;">&#41;</span> :: f <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>Der f :*: g<span style="color: green;">&#41;</span> a, a<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a>.<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:first"><span style="font-weight: bold;">first</span></a><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>InL ∘ <span style="color: green;">&#40;</span>:*: ga<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>extract fa<span style="color: green;">&#41;</span>
  :: f <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>Der f :*: g<span style="color: green;">&#41;</span> :+: <span style="color: green;">&#40;</span>f :*: Der g<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> a, a<span style="color: green;">&#41;</span>
&nbsp;
  :: f <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>Der <span style="color: green;">&#40;</span>f :*: g<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> a, a<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Similarly, for the second component,</p>

<p><div>
<pre class="haskell"><span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a>.<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:first"><span style="font-weight: bold;">first</span></a><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>InR ∘ <span style="color: green;">&#40;</span>fa :*:<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>extract ga<span style="color: green;">&#41;</span>
  :: g <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>Der <span style="color: green;">&#40;</span>f :*: g<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> a, a<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Combining the two extraction routes:</p>

<p><div>
<pre class="haskell"><span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a>.<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:first"><span style="font-weight: bold;">first</span></a><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>InL ∘ <span style="color: green;">&#40;</span>:*: ga<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>extract fa<span style="color: green;">&#41;</span> :*:
<span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a>.<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:first"><span style="font-weight: bold;">first</span></a><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>InR ∘ <span style="color: green;">&#40;</span>fa :*:<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>extract ga<span style="color: green;">&#41;</span>
  :: <span style="color: green;">&#40;</span>f :*: g<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>Der <span style="color: green;">&#40;</span>f :*: g<span style="color: green;">&#41;</span> a, a<span style="color: green;">&#41;</span></pre>
</div></p>

<p>So,</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span>Holey f, Holey g<span style="color: green;">&#41;</span> ⇒ Holey <span style="color: green;">&#40;</span>f :*: g<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <span style="color: #050; font-weight: bold;">type</span> Der <span style="color: green;">&#40;</span>f :*: g<span style="color: green;">&#41;</span> = Der f :*: g  :+:  f :*: Der g
  fillC <span style="color: green;">&#40;</span>InL <span style="color: green;">&#40;</span>dfa :*:  ga<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> = <span style="color: green;">&#40;</span>:*: ga<span style="color: green;">&#41;</span> ∘ fillC dfa
  fillC <span style="color: green;">&#40;</span>InR <span style="color: green;">&#40;</span> fa :*: dga<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> = <span style="color: green;">&#40;</span>fa :*:<span style="color: green;">&#41;</span> ∘ fillC dga
  extract <span style="color: green;">&#40;</span>fa :*: ga<span style="color: green;">&#41;</span> = 
    <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a>.<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:first"><span style="font-weight: bold;">first</span></a><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>InL ∘ <span style="color: green;">&#40;</span>:*: ga<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>extract fa<span style="color: green;">&#41;</span> :*:
    <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a>.<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:first"><span style="font-weight: bold;">first</span></a><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>InR ∘ <span style="color: green;">&#40;</span>fa :*:<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>extract ga<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Finally, the chain rule, for functor composition:</p>

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

<p>A value of type <code>(g :. f) a</code> has form <code>O gfa</code>, where <code>gfa :: g (f a)</code>.
To extract:</p>

<ul>
<li>form all <code>g</code>-extractions, yielding values of type <code>fa :: f a</code> and their contexts of type <code>Der g (f a)</code>;</li>
<li>form all <code>f</code>-extractions of each such <code>fa</code>, yielding values of type <code>a</code> and their contexts of type <code>Der f a</code>; and</li>
<li>reassemble these pieces into the shape determined by <code>Der (g :. f)</code>.</li>
</ul>

<p>Let&#8217;s go:</p>

<p><div>
<pre class="haskell">gfa :: g <span style="color: green;">&#40;</span>f a<span style="color: green;">&#41;</span>
&nbsp;
extract gfa :: g <span style="color: green;">&#40;</span>Loc g <span style="color: green;">&#40;</span>f a<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
&nbsp;
            :: g <span style="color: green;">&#40;</span>Der g <span style="color: green;">&#40;</span>f a<span style="color: green;">&#41;</span>, f a<span style="color: green;">&#41;</span>
&nbsp;
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:second"><span style="font-weight: bold;">second</span></a> extract<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>extract gfa<span style="color: green;">&#41;</span>
  :: g <span style="color: green;">&#40;</span>Der g <span style="color: green;">&#40;</span>f a<span style="color: green;">&#41;</span>, f <span style="color: green;">&#40;</span>Loc f a<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre>
</div></p>

<p>Continuing, the following lemmas come in handy.</p>

<p><div>
<pre class="haskell">tweak2 :: <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> f ⇒
          <span style="color: green;">&#40;</span>dg <span style="color: green;">&#40;</span>f a<span style="color: green;">&#41;</span>, f <span style="color: green;">&#40;</span>df a, a<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> → f <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>dg :. f<span style="color: green;">&#41;</span> :*: df<span style="color: green;">&#41;</span> a, a<span style="color: green;">&#41;</span>
tweak2 = <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a>.<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:first"><span style="font-weight: bold;">first</span></a><span style="color: green;">&#41;</span> chainRule ∘ tweak1
&nbsp;
tweak1 :: <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> f ⇒
          <span style="color: green;">&#40;</span>dg <span style="color: green;">&#40;</span>fa<span style="color: green;">&#41;</span>, f <span style="color: green;">&#40;</span>dfa, a<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> → f <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>dg <span style="color: green;">&#40;</span>fa<span style="color: green;">&#41;</span>, dfa<span style="color: green;">&#41;</span>, a<span style="color: green;">&#41;</span>
tweak1 = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> lassoc ∘ squishP
&nbsp;
squishP :: <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> f ⇒ <span style="color: green;">&#40;</span>a, f b<span style="color: green;">&#41;</span> → f <span style="color: green;">&#40;</span>a,b<span style="color: green;">&#41;</span>
squishP <span style="color: green;">&#40;</span>a,fb<span style="color: green;">&#41;</span> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> <span style="color: green;">&#40;</span>a,<span style="color: green;">&#41;</span> fb
&nbsp;
chainRule :: <span style="color: green;">&#40;</span>dg <span style="color: green;">&#40;</span>f a<span style="color: green;">&#41;</span>, df a<span style="color: green;">&#41;</span> → <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>dg :. f<span style="color: green;">&#41;</span> :*: df<span style="color: green;">&#41;</span> a
chainRule <span style="color: green;">&#40;</span>dgfa, dfa<span style="color: green;">&#41;</span> = O dgfa :*: dfa
&nbsp;
lassoc :: <span style="color: green;">&#40;</span>p,<span style="color: green;">&#40;</span>q,r<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> → <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>p,q<span style="color: green;">&#41;</span>,r<span style="color: green;">&#41;</span>
lassoc    <span style="color: green;">&#40;</span>p,<span style="color: green;">&#40;</span>q,r<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> =  <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>p,q<span style="color: green;">&#41;</span>,r<span style="color: green;">&#41;</span></pre>
</div></p>

<p><em>Edit:</em> Sjoerd Visscher found a much simpler form to replace the previous group of definitions:</p>

<p><div>
<pre class="haskell">tweak2 <span style="color: green;">&#40;</span>dgfa, fl<span style="color: green;">&#41;</span> = <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a>.<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:first"><span style="font-weight: bold;">first</span></a><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>O dgfa :*:<span style="color: green;">&#41;</span> fl</pre>
</div></p>

<p>More specifically,</p>

<p><div>
<pre class="haskell">tweak2 :: <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> f =&gt; <span style="color: green;">&#40;</span>Der g <span style="color: green;">&#40;</span>f a<span style="color: green;">&#41;</span>, f <span style="color: green;">&#40;</span>Loc f a<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
                    -&gt; f <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>Der g :. f<span style="color: green;">&#41;</span> :*: Der f<span style="color: green;">&#41;</span> a, a<span style="color: green;">&#41;</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> f =&gt; <span style="color: green;">&#40;</span>Der g <span style="color: green;">&#40;</span>f a<span style="color: green;">&#41;</span>, f <span style="color: green;">&#40;</span>Loc f a<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
                    -&gt; f <span style="color: green;">&#40;</span>Der <span style="color: green;">&#40;</span>g :. f<span style="color: green;">&#41;</span> a, a<span style="color: green;">&#41;</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> f =&gt; <span style="color: green;">&#40;</span>Der g <span style="color: green;">&#40;</span>f a<span style="color: green;">&#41;</span>, f <span style="color: green;">&#40;</span>Loc f a<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
                    -&gt; f <span style="color: green;">&#40;</span>Loc <span style="color: green;">&#40;</span>g :. f<span style="color: green;">&#41;</span> a<span style="color: green;">&#41;</span></pre>
</div></p>

<p>This lemma gives just what we need to tweak the inner extraction:</p>

<p><div>
<pre class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> <span style="color: green;">&#40;</span>tweak2 ∘ <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:second"><span style="font-weight: bold;">second</span></a> extract<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>extract gfa<span style="color: green;">&#41;</span> :: g <span style="color: green;">&#40;</span>f <span style="color: green;">&#40;</span>Loc <span style="color: green;">&#40;</span>g :. f<span style="color: green;">&#41;</span> a<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre>
</div></p>

<p>So</p>

<p><div>
<pre class="haskell">extractGF :: <span style="color: green;">&#40;</span>Holey f, Holey g<span style="color: green;">&#41;</span> ⇒
             g <span style="color: green;">&#40;</span>f a<span style="color: green;">&#41;</span> → g <span style="color: green;">&#40;</span>f <span style="color: green;">&#40;</span>Loc <span style="color: green;">&#40;</span>g :. f<span style="color: green;">&#41;</span> a<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
extractGF = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> <span style="color: green;">&#40;</span>tweak2 ∘ <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:second"><span style="font-weight: bold;">second</span></a> extract<span style="color: green;">&#41;</span> ∘ extract</pre>
</div></p>

<p>and</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span>Holey f, Holey g<span style="color: green;">&#41;</span> ⇒ Holey <span style="color: green;">&#40;</span>g :. f<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <span style="color: #050; font-weight: bold;">type</span> Der <span style="color: green;">&#40;</span>g :.  f<span style="color: green;">&#41;</span> = Der g :. f  :*:  Der f
  fillC <span style="color: green;">&#40;</span>O dgfa :*: dfa<span style="color: green;">&#41;</span> = O ∘ fillC dgfa ∘ fillC dfa
  extract = inO extractGF</pre>
</div></p>

<p>where <code>inO</code> is from <a href="http://hackage.haskell.org/packages/archive/TypeCompose/latest/doc/html/Control-Compose.html" title="module documentation">Control.Compose</a>, and is defined 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>

<p><div>
<pre class="haskell"><span style="color: #5d478b; font-style: italic;">-- | Apply a unary function within the 'O' constructor.</span>
inO :: <span style="color: green;">&#40;</span>g <span style="color: green;">&#40;</span>f a<span style="color: green;">&#41;</span> → g' <span style="color: green;">&#40;</span>f' a'<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> → <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;">&#40;</span>g' :. f'<span style="color: green;">&#41;</span> a'<span style="color: green;">&#41;</span>
inO = unO ~&gt; O
&nbsp;
<span style="color: #050; font-weight: bold;">infixr</span> <span style="color: red;">1</span> ~&gt;
<span style="color: #5d478b; font-style: italic;">-- | Add pre- and post processing</span>
<span style="color: green;">&#40;</span>~&gt;<span style="color: green;">&#41;</span> :: <span style="color: green;">&#40;</span>a' → a<span style="color: green;">&#41;</span> → <span style="color: green;">&#40;</span>b → b'<span style="color: green;">&#41;</span> → <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>a → b<span style="color: green;">&#41;</span> → <span style="color: green;">&#40;</span>a' → b'<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
<span style="color: green;">&#40;</span>f ~&gt; h<span style="color: green;">&#41;</span> g = h ∘ g ∘ f</pre>
</div></p>

<p>In case you&#8217;re wondering, these definitions did not come to me effortlessly.
I sweated through the derivation, guided always by my intuition and the necessary types, as determined by the shape of <code>Der (g :. f)</code>.
The type-checker helped me get from one step to the next.</p>

<p>I do a lot of type-directed derivations of this style while I program in Haskell, with the type-checker checking each step for me.
I&#8217;d love to have mechanized help in <em>creating</em> these derivations, not just <em>checking</em> them.</p>

<h3>Zippers</h3>

<p>How does the <code>Holey</code> class relate to zippers?
As in a few recent blog posts, let&#8217;s use the fact that regular data types are isomorphic to fixed-points of functors.</p>

<p>Functor fixed-points are like function fixed points</p>

<p><div>
<pre class="haskell">fix f = f <span style="color: green;">&#40;</span>fix f<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #050; font-weight: bold;">type</span> Fix f = f <span style="color: green;">&#40;</span>Fix f<span style="color: green;">&#41;</span></pre>
</div></p>

<p>However, Haskell doesn&#8217;t support recursive type synonyms, so use a <code>newtype</code>:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">newtype</span> Fix f = Fix <span style="color: green;">&#123;</span> unFix :: f <span style="color: green;">&#40;</span>Fix f<span style="color: green;">&#41;</span> <span style="color: green;">&#125;</span></pre>
</div></p>

<p>A context for a functor fixed-point is either empty, if we&#8217;re at the very top of an &#8220;<code>f</code>-tree&#8221;, or it&#8217;s an <code>f</code>-context for <code>f (Fix f)</code>, and a parent context:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">data</span> Context f = TopC | Context <span style="color: green;">&#40;</span>Der f <span style="color: green;">&#40;</span>Fix f<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>Context f<span style="color: green;">&#41;</span>  <span style="color: #5d478b; font-style: italic;">-- first try</span></pre>
</div></p>

<p>Hm.
On the outside, <code>Context f</code> looks like a list, so let&#8217;s use a list instead:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> Context f = <span style="color: green;">&#91;</span>Der f <span style="color: green;">&#40;</span>Fix f<span style="color: green;">&#41;</span><span style="color: green;">&#93;</span></pre>
</div></p>

<p>The location type we used above is</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> Loc f a = <span style="color: green;">&#40;</span>Der f a, a<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Similarly, define a type of zippers (also called &#8220;locations&#8221;) for functor fixed-points:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> Zipper f = <span style="color: green;">&#40;</span>Context f, Fix f<span style="color: green;">&#41;</span></pre>
</div></p>

<p>This <code>Zipper</code> type corresponds to a zipper, and has operations <code>up</code> and <code>down</code>.
The <code>down</code> motion can yield multiple results.</p>

<p><div>
<pre class="haskell">up   :: Holey f ⇒ Zipper f →    Zipper f
&nbsp;
down :: Holey f ⇒ Zipper f → f <span style="color: green;">&#40;</span>Zipper f<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Since <code>down</code> yields an <code>f</code>-collection of locations, we do not need sibling navigation functions (<code>left</code> &amp; <code>right</code>).</p>

<p>To move up in <code>Zipper</code>, strip off a derivative (one-hole functor context) and fill the hole with the current tree, leaving the other derivatives as the remaining fixed-point context.
Like so:</p>

<p><div>
<pre class="haskell">up   :: Holey f ⇒ Zipper f →    Zipper f
up <span style="color: green;">&#40;</span>d:ds', t<span style="color: green;">&#41;</span> = <span style="color: green;">&#40;</span>ds', Fix <span style="color: green;">&#40;</span>fill <span style="color: green;">&#40;</span>d,t<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre>
</div></p>

<p>To see how the typing works out:</p>

<p><div>
<pre class="haskell"><span style="color: green;">&#40;</span>d:ds', t<span style="color: green;">&#41;</span> :: Zipper f
<span style="color: green;">&#40;</span>d:ds', t<span style="color: green;">&#41;</span> :: <span style="color: green;">&#40;</span>Context f, Fix f<span style="color: green;">&#41;</span>
&nbsp;
d:ds' :: <span style="color: green;">&#91;</span>Der f <span style="color: green;">&#40;</span>Fix f<span style="color: green;">&#41;</span><span style="color: green;">&#93;</span>
&nbsp;
t :: Fix f
&nbsp;
d   ::  Der f <span style="color: green;">&#40;</span>Fix f<span style="color: green;">&#41;</span>
ds' :: <span style="color: green;">&#91;</span>Der f <span style="color: green;">&#40;</span>Fix f<span style="color: green;">&#41;</span><span style="color: green;">&#93;</span>
&nbsp;
fill :: Loc f b → f b
fill :: <span style="color: green;">&#40;</span>Der f b, b<span style="color: green;">&#41;</span> → f b
fill :: <span style="color: green;">&#40;</span>Der f <span style="color: green;">&#40;</span>Fix f<span style="color: green;">&#41;</span>, Fix f<span style="color: green;">&#41;</span> → f <span style="color: green;">&#40;</span>Fix f<span style="color: green;">&#41;</span>
&nbsp;
fill <span style="color: green;">&#40;</span>d,t<span style="color: green;">&#41;</span> :: f <span style="color: green;">&#40;</span>Fix f<span style="color: green;">&#41;</span>
&nbsp;
Fix <span style="color: green;">&#40;</span>fill <span style="color: green;">&#40;</span>d,t<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> :: Fix f
&nbsp;
<span style="color: green;">&#40;</span>ds', Fix <span style="color: green;">&#40;</span>fill <span style="color: green;">&#40;</span>d,t<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> :: <span style="color: green;">&#40;</span>Context f, Fix f<span style="color: green;">&#41;</span>
                        :: Zipper f</pre>
</div></p>

<p>Note that the <code>up</code> motion fails when at the top of a zipper (empty context).
If desired, we can also provide an unfailing version (really, a version with explictly typed failure):</p>

<p><div>
<pre class="haskell">up' :: Holey f =&gt; Zipper f -&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>Zipper f<span style="color: green;">&#41;</span>
up' <span style="color: green;">&#40;</span><span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>   , _<span style="color: green;">&#41;</span> = Nothing
up' l          = Just <span style="color: green;">&#40;</span>up l<span style="color: green;">&#41;</span></pre>
</div></p>

<p>To move down in an <code>f</code>-tree <code>t</code>, form the extractions of <code>t</code>, each of which has a derivative and a sub-tree.
The derivative becomes part of an extended fixed-point context, and the sub-tree becomes the new sub-tree of focus.</p>

<p><div>
<pre class="haskell">down :: Holey f ⇒ Zipper f → f <span style="color: green;">&#40;</span>Zipper f<span style="color: green;">&#41;</span>
down <span style="color: green;">&#40;</span>ds', t<span style="color: green;">&#41;</span> = <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a>.<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:first"><span style="font-weight: bold;">first</span></a><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>:ds'<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>extract <span style="color: green;">&#40;</span>unFix t<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>unFix t<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre>
</div></p>

<p>The typing (in case you&#8217;re curious):</p>

<p><div>
<pre class="haskell"><span style="color: green;">&#40;</span>ds',t<span style="color: green;">&#41;</span> :: Zipper f
        :: <span style="color: green;">&#40;</span>Context f, Fix f<span style="color: green;">&#41;</span>
        :: <span style="color: green;">&#40;</span><span style="color: green;">&#91;</span>Der f <span style="color: green;">&#40;</span>Fix f<span style="color: green;">&#41;</span><span style="color: green;">&#93;</span>, Fix f<span style="color: green;">&#41;</span>
&nbsp;
ds' :: <span style="color: green;">&#91;</span>Der f <span style="color: green;">&#40;</span>Fix f<span style="color: green;">&#41;</span><span style="color: green;">&#93;</span>
t :: Fix f
unFix t :: f <span style="color: green;">&#40;</span>Fix f<span style="color: green;">&#41;</span>
&nbsp;
extract <span style="color: green;">&#40;</span>unFix t<span style="color: green;">&#41;</span> :: f <span style="color: green;">&#40;</span>Der f <span style="color: green;">&#40;</span>Fix f<span style="color: green;">&#41;</span>, Fix f<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a>.<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:first"><span style="font-weight: bold;">first</span></a><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>:ds'<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>extract <span style="color: green;">&#40;</span>unFix t<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
  :: <span style="color: green;">&#40;</span><span style="color: green;">&#91;</span>Der f <span style="color: green;">&#40;</span>Fix f<span style="color: green;">&#41;</span><span style="color: green;">&#93;</span>, Fix f<span style="color: green;">&#41;</span>
  :: <span style="color: green;">&#40;</span>Context f, Fix f<span style="color: green;">&#41;</span>
  :: LocFix f</pre>
</div></p>

<h3>Zipping back to regular data types</h3>

<p>I like the (functor) fixed-point perspective on regular data types, for its austere formal simplicity.
It shows me the naked essence of regular data types, so I can more easily see and more deeply understand patterns like memoization, derivatives, and zippers.</p>

<p>For convenience and friendliness of <em>use</em>, I prefer working with regular types directly, rather than through the (nearly) isomorphic form of functor fixed-points.
While the fixed-point perspective is formalism-friendly, the <em>pattern functor</em> perspective is more user-friendly, allowing us to work with our familiar regular data as they are.</p>

<p>As in <em><a href="http://conal.net/blog/posts/elegant-memoization-with-higher-order-types/" title="blog post">Elegant memoization with higher-order types</a></em>, let&#8217;s use the following class:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">class</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>PF t<span style="color: green;">&#41;</span> ⇒ Regular t <span style="color: #050; font-weight: bold;">where</span>
  <span style="color: #050; font-weight: bold;">type</span> PF t :: * → *
  wrap   :: PF t t → t
  unwrap :: t → PF t t</pre>
</div></p>

<p>The idea is that a type <code>t</code> is isomorphic to <code>Fix (PF t)</code>, although really there may be more points of undefinedness in the fixed-point representation, so rather than an isomorphism, we have an embedding/projection pair.</p>

<p>The notions of context and location are similar to the ones above:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> Context t = <span style="color: green;">&#91;</span>Der <span style="color: green;">&#40;</span>PF t<span style="color: green;">&#41;</span> t<span style="color: green;">&#93;</span>
&nbsp;
<span style="color: #050; font-weight: bold;">type</span> Zipper t = <span style="color: green;">&#40;</span>Context t, t<span style="color: green;">&#41;</span></pre>
</div></p>

<p>So are the <code>up</code> and <code>down</code> motions, in which <code>wrap</code> and <code>unwrap</code> replace <code>Fix</code> and <code>unFix</code>:</p>

<p><div>
<pre class="haskell">up   :: <span style="color: green;">&#40;</span>Regular t, Holey <span style="color: green;">&#40;</span>PF t<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> ⇒ Zipper t →       Zipper t
down :: <span style="color: green;">&#40;</span>Regular t, Holey <span style="color: green;">&#40;</span>PF t<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> ⇒ Zipper t → PF t <span style="color: green;">&#40;</span>Zipper t<span style="color: green;">&#41;</span>
&nbsp;
up <span style="color: green;">&#40;</span>d:ds', t<span style="color: green;">&#41;</span> = <span style="color: green;">&#40;</span>ds', wrap <span style="color: green;">&#40;</span>fill <span style="color: green;">&#40;</span>d,t<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
&nbsp;
down <span style="color: green;">&#40;</span>ds', t<span style="color: green;">&#41;</span> = <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a>.<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Arrow.html#v:first"><span style="font-weight: bold;">first</span></a><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>:ds'<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>extract <span style="color: green;">&#40;</span>unwrap t<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre>
</div></p>
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/another-angle-on-zippers/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Differentiation of higher-order types</title>
		<link>http://conal.net/blog/posts/differentiation-of-higher-order-types/</link>
		<comments>http://conal.net/blog/posts/differentiation-of-higher-order-types/#comments</comments>
		<pubDate>Thu, 29 Jul 2010 02:45:51 +0000</pubDate>
		<dc:creator>conal</dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[derivative]]></category>
		<category><![CDATA[functor]]></category>
		<category><![CDATA[zipper]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=140</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Differentiation of higher-order types

Tags: derivative, functor, zipper

URL: http://conal.net/blog/posts/differentiation-of-higher-order-types/

-->

<!-- references -->

<!-- teaser -->

<p>A &#8220;one-hole context&#8221; is a data structure with one piece missing.
Conor McBride pointed out that <a href="http://www.cs.nott.ac.uk/~ctm/diff.pdf" title="paper by Conor McBride">the derivative of a regular type is its type of one-hole contexts</a>.
When a data structure is assembled out of common functor combinators, a corresponding type of one-hole contexts can be derived mechanically by rules that mirror the standard derivative rules learned in beginning differential calculus.</p>

<p>I&#8217;ve been playing with functor combinators lately.
I was delighted to find that the data-structure derivatives can be expressed directly using the standard functor combinators and type families.</p>

<p>The code in this blog post is taken from the Haskell library <a href="http://hackage.haskell.org/package/functor-combo" title="Hackage entry: functor-combo">functor-combo</a>.</p>

<p>See also the <a href="http://en.wikibooks.org/wiki/Haskell/Zippers" title="Wikibooks entry">Haskell Wikibooks page on zippers</a>, especially the section called &#8220;Differentiation of data types&#8221;.</p>

<p>I mean this post not as new research, but rather as a tidy, concrete presentation of some of Conor&#8217;s delightful insight.</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-140"></span></p>

<h3>Functor combinators</h3>

<p>Let&#8217;s use the same set of functor combinators as in <em><a href="http://conal.net/blog/posts/elegant-memoization-with-higher-order-types/" title="blog post">Elegant memoization with higher-order types</a></em> and <em><a href="http://conal.net/blog/posts/memoizing-higher-order-functions/" title="blog post">Memoizing higher-order functions</a></em>:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">data</span> Void a   <span style="color: #5d478b; font-style: italic;">-- no constructors</span>
&nbsp;
<span style="color: #050; font-weight: bold;">type</span> Unit a        = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Const"><span style="color: #833; font-weight: bold;">Const</span></a> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span> a
&nbsp;
<span style="color: #050; font-weight: bold;">data</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Const"><span style="color: #833; font-weight: bold;">Const</span></a> x a     = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Const"><span style="color: #833; font-weight: bold;">Const</span></a> x
&nbsp;
<span style="color: #050; font-weight: bold;">newtype</span> Id a       = Id a
&nbsp;
<span style="color: #050; font-weight: bold;">data</span> <span style="color: green;">&#40;</span>f :+: g<span style="color: green;">&#41;</span> a   = InL <span style="color: green;">&#40;</span>f a<span style="color: green;">&#41;</span> | InR <span style="color: green;">&#40;</span>g a<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #050; font-weight: bold;">data</span> <span style="color: green;">&#40;</span>f :*: g<span style="color: green;">&#41;</span> a   = f a :*: g a
&nbsp;
<span style="color: #050; font-weight: bold;">newtype</span> <span style="color: green;">&#40;</span>g :. f<span style="color: green;">&#41;</span> a = O <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></pre>
</div></p>

<h3>Derivatives</h3>

<p>The derivative of a functor is another functor.
Since the shape of the derivative is non-uniform (depends on the shape of the functor being differentiated) define a higher-order <a href="http://www.haskell.org/ghc/docs/latest/html/users_guide/type-families.html" title="GHC documentation on type families">type family</a>:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> family Der <span style="color: green;">&#40;</span>f :: <span style="color: green;">&#40;</span>* → *<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> :: <span style="color: green;">&#40;</span>* → *<span style="color: green;">&#41;</span></pre>
</div></p>

<p>The usual derivative rules can then be translated without applying much imagination.
That is, if we start with derivative rules in their <em>functional</em> form (e.g., as in the paper <em><a href="http://conal.net/blog/posts/paper-beautiful-differentiation/" title="blog post">Beautiful differentiation</a></em>, Section 2 and Figure 1).</p>

<p>For instance, the derivative of the constant function is the constant 0 function, and the derivative of the identity function is the constant 1 function.
If <code>der</code> is the derivative functional mapping functions (of real numbers) to functions,</p>

<p><div>
<pre class="haskell">der <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> x<span style="color: green;">&#41;</span> ≡ <span style="color: red;">0</span>
der <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:id"><span style="font-weight: bold;">id</span></a>        ≡ <span style="color: red;">1</span></pre>
</div></p>

<p>On the right-hand sides, I am exploiting the function instances of <code>Num</code> from the library <a href="http://hackage.haskell.org/cgi-bin/hackage-scripts/package/applicative-numbers" title="Haskell library">applicative-numbers</a>.
To be more explicit, I could have written &#8220;<code>const 0</code>&#8221; and &#8220;<code>const 1</code>&#8220;.</p>

<p>Correspondingly,</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> <span style="color: #050; font-weight: bold;">instance</span> Der <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Const"><span style="color: #833; font-weight: bold;">Const</span></a> x<span style="color: green;">&#41;</span> = Void   <span style="color: #5d478b; font-style: italic;">-- 0</span>
&nbsp;
<span style="color: #050; font-weight: bold;">type</span> <span style="color: #050; font-weight: bold;">instance</span> Der Id        = Unit   <span style="color: #5d478b; font-style: italic;">-- 1</span></pre>
</div></p>

<p>Note that the types <code>Void a</code> and <code>Unit a</code> have 0 and 1 element, respectively, if we ignore ⊥.
Moreover, <code>Void</code> is a sort of additive identity, and <code>Unit</code> is a sort of multiplicative identity, again ignoring ⊥.
For these reasons, <code>Void</code> and <code>Unit</code> might be more aptly named &#8220;<code>Zero</code>&#8221; and &#8220;<code>One</code>&#8220;.</p>

<p>The first rule says that the a value of type <code>Const x a</code> has no one-hole context (for type <code>a</code>), which is true, since there is an <code>x</code> but no <code>a</code>.
The second rule says that there is exactly one possible context for <code>Id a</code>, since the one and only <code>a</code> value must be removed, and no information remains.</p>

<p>A (one-hole) context for a sum is a context for the left or the right possibility of the sum:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> <span style="color: #050; font-weight: bold;">instance</span> Der <span style="color: green;">&#40;</span>f :+: g<span style="color: green;">&#41;</span> = Der f :+: Der g</pre>
</div></p>

<p>Correspondingly, the derivative of a sum of functions is the sum of the functions&#8217; derivatives::</p>

<p><div>
<pre class="haskell">der <span style="color: green;">&#40;</span>f + g<span style="color: green;">&#41;</span> ≡ der f + der g</pre>
</div></p>

<p>Again I&#8217;m using the function <code>Num</code> instance from <a href="http://hackage.haskell.org/cgi-bin/hackage-scripts/package/applicative-numbers" title="Haskell library">applicative-numbers</a>.</p>

<p>For a pair, the one hole of a context can be made somewhere in the first component or somewhere in the second component.
So the pair context consists of a holey first component and a full second component or a full first component and a holey second component.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> <span style="color: #050; font-weight: bold;">instance</span> Der <span style="color: green;">&#40;</span>f :*: g<span style="color: green;">&#41;</span> = Der f :*: g  :+:  f :*: Der g</pre>
</div></p>

<p>Similarly, for functions:</p>

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

<p>Finally, consider functor composition.
If <code>g</code> and <code>f</code> are container types, then <code>(g :. f) a</code> is the type of <code>g</code> containers of <code>f</code> containers of <code>a</code> elements.
The <code>a</code>-shaped hole must come from one of the contained <code>f a</code> structures.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> <span style="color: #050; font-weight: bold;">instance</span> Der <span style="color: green;">&#40;</span>g :. f<span style="color: green;">&#41;</span> = <span style="color: green;">&#40;</span>Der g :. f<span style="color: green;">&#41;</span> :*: Der f</pre>
</div></p>

<p>Here&#8217;s one way to think of this derivative functor:
to make an <code>a</code>-shaped hole in a <code>g (f a)</code>, first remove an <code>f a</code> structure, leaving an <code>(f a)</code>-shaped hole, and then put back all but an <code>a</code> value extracted from the removed <code>f a</code> struture.
So the overall (one-hole) context can be assembled from two parts: a <code>g</code> context of <code>f a</code> structures, and an <code>f</code> context of <code>a</code> values.</p>

<p>The corresponding rule for function derivatives:</p>

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

<p>which again uses <code>Num</code> on functions.
Written out more explicitly:</p>

<p><div>
<pre class="haskell">der <span style="color: green;">&#40;</span>g ∘ f<span style="color: green;">&#41;</span> a ≡ der g <span style="color: green;">&#40;</span>f a<span style="color: green;">&#41;</span>  * der f a</pre>
</div></p>

<p>which may look more like the form you&#8217;re used to.</p>

<h3>Summary of derivatives</h3>

<p>To emphasize the correspondence between forms of differentiation, here are rules for <em>function</em> and <em>functor</em> derivatives:</p>

<p><div>
<pre class="haskell">der <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> x<span style="color: green;">&#41;</span> ≡ <span style="color: red;">0</span>
Der <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Const"><span style="color: #833; font-weight: bold;">Const</span></a> x<span style="color: green;">&#41;</span> ≡ Void
&nbsp;
der <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:id"><span style="font-weight: bold;">id</span></a> ≡ <span style="color: red;">1</span>
Der Id ≡ Unit
&nbsp;
der <span style="color: green;">&#40;</span>f  +  g<span style="color: green;">&#41;</span> ≡ der f  +  der g
Der <span style="color: green;">&#40;</span>f :+: g<span style="color: green;">&#41;</span> ≡ Der f :+: Der g
&nbsp;
der <span style="color: green;">&#40;</span>f  *  g<span style="color: green;">&#41;</span> ≡ der f  *  g  +  f  *  der g
Der <span style="color: green;">&#40;</span>f :*: g<span style="color: green;">&#41;</span> ≡ Der f :*: g :+: f :*: Der g
&nbsp;
der <span style="color: green;">&#40;</span>g  ∘ f<span style="color: green;">&#41;</span> ≡ <span style="color: green;">&#40;</span>der g  ∘ f<span style="color: green;">&#41;</span>  *  der f
Der <span style="color: green;">&#40;</span>g :. f<span style="color: green;">&#41;</span> ≡ <span style="color: green;">&#40;</span>Der g :. f<span style="color: green;">&#41;</span> :*: Der f</pre>
</div></p>

<h3>Filling holes</h3>

<p>Each derivative functor is a one-hole container.
One useful operation on derivatives is filling that hole.</p>

<p><div>
<pre class="haskell">fillC :: <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> f ⇒ Der f a → a → f a</pre>
</div></p>

<p>The specifics of how to fill in a hole will depend on the choice of functor <code>f</code>, so let&#8217;s make the <code>fillC</code> operation a method of a new type class.
This new class is also a handy place to stash the associated type of derivatives, as an alternative to the top-level declarations above.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">class</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> f ⇒ Holey f <span style="color: #050; font-weight: bold;">where</span>
  <span style="color: #050; font-weight: bold;">type</span> Der f :: * → *
  fillC :: Der f a → a → f a</pre>
</div></p>

<p>I&#8217;ll add one more method to this class in an upcoming post.</p>

<p>For <code>Const x</code>, there are no cases to handle, since there are no holes.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> Holey <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Const"><span style="color: #833; font-weight: bold;">Const</span></a> x<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <span style="color: #050; font-weight: bold;">type</span> Der <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Const"><span style="color: #833; font-weight: bold;">Const</span></a> x<span style="color: green;">&#41;</span> = Void
  fillC = <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;fillC for Const x: no Der values&quot;</span></pre>
</div></p>

<p>I added a definition just to keep the compiler from complaining.
This particular <code>fillC</code> can only be applied to a value of type <code>Void a</code>, and there are no such values other than ⊥.</p>

<p>Is there a more elegant way to define functions over data types with no constructors?
One idea is to provide a single, polymorphic function over void types:</p>

<p><div>
<pre class="haskell">  voidF :: Void a → b
  voidF = <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;voidF: no value of type Void&quot;</span></pre>
</div></p>

<p>And use whenever as needed, e.g.,</p>

<p><div>
<pre class="haskell">  fillC = voidF</pre>
</div></p>

<p>Next is our identity functor:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> Holey Id <span style="color: #050; font-weight: bold;">where</span>
  <span style="color: #050; font-weight: bold;">type</span> Der Id = Unit
  fillC <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Const"><span style="color: #833; font-weight: bold;">Const</span></a> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> a = Id a</pre>
</div></p>

<p>More succinctly,</p>

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

<p>For sums,</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span>Holey f, Holey g<span style="color: green;">&#41;</span> ⇒ Holey <span style="color: green;">&#40;</span>f :+: g<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <span style="color: #050; font-weight: bold;">type</span> Der <span style="color: green;">&#40;</span>f :+: g<span style="color: green;">&#41;</span> = Der f :+: Der g
  fillC <span style="color: green;">&#40;</span>InL df<span style="color: green;">&#41;</span> a = InL <span style="color: green;">&#40;</span>fillC df a<span style="color: green;">&#41;</span>
  fillC <span style="color: green;">&#40;</span>InR df<span style="color: green;">&#41;</span> a = InR <span style="color: green;">&#40;</span>fillC df a<span style="color: green;">&#41;</span></pre>
</div></p>

<p>or</p>

<p><div>
<pre class="haskell">  fillC <span style="color: green;">&#40;</span>InL df<span style="color: green;">&#41;</span> = InL ∘ fillC df
  fillC <span style="color: green;">&#40;</span>InR df<span style="color: green;">&#41;</span> = InR ∘ fillC df</pre>
</div></p>

<p>Products also have two cases, since the derivative of a product is a sum:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span>Holey f, Holey g<span style="color: green;">&#41;</span> ⇒ Holey <span style="color: green;">&#40;</span>f :*: g<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <span style="color: #050; font-weight: bold;">type</span> Der <span style="color: green;">&#40;</span>f :*: g<span style="color: green;">&#41;</span> = Der f :*: g  :+:  f :*: Der g
  fillC <span style="color: green;">&#40;</span>InL <span style="color: green;">&#40;</span>dfa :*:  ga<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> a = fillC dfa a :*: ga
  fillC <span style="color: green;">&#40;</span>InR <span style="color: green;">&#40;</span> fa :*: dga<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> a = fa :*: fillC dga a</pre>
</div></p>

<p>Less pointfully,</p>

<p><div>
<pre class="haskell">  fillC <span style="color: green;">&#40;</span>InL <span style="color: green;">&#40;</span>dfa :*:  ga<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> = <span style="color: green;">&#40;</span>:*: ga<span style="color: green;">&#41;</span> ∘ fillC dfa
  fillC <span style="color: green;">&#40;</span>InR <span style="color: green;">&#40;</span> fa :*: dga<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> = <span style="color: green;">&#40;</span>fa :*:<span style="color: green;">&#41;</span> ∘ fillC dga</pre>
</div></p>

<p>Finally, functor composition:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span>Holey f, Holey g<span style="color: green;">&#41;</span> ⇒ Holey <span style="color: green;">&#40;</span>g :. f<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <span style="color: #050; font-weight: bold;">type</span> Der <span style="color: green;">&#40;</span>g :. f<span style="color: green;">&#41;</span> = <span style="color: green;">&#40;</span>Der g :. f<span style="color: green;">&#41;</span> :*: Der f
  fillC <span style="color: green;">&#40;</span>O dgfa :*: dfa<span style="color: green;">&#41;</span> a = O <span style="color: green;">&#40;</span>fillC dgfa <span style="color: green;">&#40;</span>fillC dfa a<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre>
</div></p>

<p>The less pointful form is more telling.</p>

<p><div>
<pre class="haskell">  fillC <span style="color: green;">&#40;</span>O dgfa :*: dfa<span style="color: green;">&#41;</span> = O ∘ fillC dgfa ∘ fillC dfa</pre>
</div></p>

<p>In words: filling of the derivative of a composition is a composition of filling of the derivatives.</p>

<h3>Thoughts on composition</h3>

<p>Let&#8217;s return to the derivative rules for composition, i.e., the chain rule, on functions and on functors:</p>

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

<p>Written in this way, the functor rule looks quite compelling.
Something bothers me, however.
For functions, multiplication is a special case, not the general case, and is only meaningful and correct when differentiating functions from scalars to scalars.
In general, derivative values are <em>linear maps</em>, and the chain rule uses composition on linear maps rather than multiplication on scalars (that <em>represent</em> linear maps).
I&#8217;ve written several <a href="http://conal.net/blog/tag/derivative/" title="Posts on derivatives">posts on derivatives</a> and a paper <em><a href="http://conal.net/blog/posts/paper-beautiful-differentiation/" title="blog post">Beautiful differentiation</a></em>, describing this perspective, which comes from calculus on manifolds.</p>

<p>Look again at the less pointful formulation of <code>fillC</code> for derivatives of compositions:</p>

<p><div>
<pre class="haskell">  fillC <span style="color: green;">&#40;</span>O dgfa :*: dfa<span style="color: green;">&#41;</span> = O ∘ fillC dgfa ∘ fillC dfa</pre>
</div></p>

<p>The product in this case is just structural.
The actual use in <code>fillC</code> is indeed a composition of linear maps.
In this context, &#8220;linear&#8221; has a different meaning from before.
It&#8217;s another way of saying &#8220;fills a <em>one-hole</em> context&#8221; (as the linear patterns of term rewriting and of ML &amp; Haskell).</p>

<p>So maybe there&#8217;s a more general/abstract view of <em>functor</em> derivatives, just as there is a more general/abstract view of <em>function</em> derivatives.
In that view, we might replace the functor chain rule&#8217;s product with a notion of composition.</p>
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/differentiation-of-higher-order-types/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Details for non-strict memoization, part 1</title>
		<link>http://conal.net/blog/posts/details-for-nonstrict-memoization-part-1/</link>
		<comments>http://conal.net/blog/posts/details-for-nonstrict-memoization-part-1/#comments</comments>
		<pubDate>Tue, 27 Jul 2010 00:14:20 +0000</pubDate>
		<dc:creator>conal</dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[functor]]></category>
		<category><![CDATA[memoization]]></category>
		<category><![CDATA[trie]]></category>
		<category><![CDATA[unamb]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=131</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Details for non-strict memoization, part 1

Tags: memoization, functor, trie, unamb

URL: http://conal.net/blog/posts/details-for-nonstrict-memoization-part-1/

-->

<!-- references -->

<!-- teaser -->

<p>In <em><a href="http://conal.net/blog/posts/nonstrict-memoization/" title="blog post">Non-strict memoization</a></em>, I sketched out a means of memoizing non-strict functions.
I gave the essential insight but did not show the details of how a nonstrict memoization library comes together.
In this new post, I give details, which are a bit delicate, in terms of the implementation described in <em><a href="http://conal.net/blog/posts/elegant-memoization-with-higher-order-types/" title="blog post">Elegant memoization with higher-order types</a></em>.</p>

<p>Near the end, I run into some trouble with regular data types, which I don&#8217;t know how to resolve cleanly and efficiently.</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-131"></span></p>

<h3>Hyper-strict memo tries</h3>

<p>Strict memoization (really <em>hyper-strict</em>) is centered on a family of trie functors, defined as a functor <code>Trie k</code>, associated with a type <code>k</code>.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> k :→: v = Trie k v
&nbsp;
<span style="color: #050; font-weight: bold;">class</span> HasTrie k <span style="color: #050; font-weight: bold;">where</span>
    <span style="color: #050; font-weight: bold;">type</span> Trie k :: * → *
    trie   :: <span style="color: green;">&#40;</span>k  →  v<span style="color: green;">&#41;</span> → <span style="color: green;">&#40;</span>k :→: v<span style="color: green;">&#41;</span>
    untrie :: <span style="color: green;">&#40;</span>k :→: v<span style="color: green;">&#41;</span> → <span style="color: green;">&#40;</span>k  →  v<span style="color: green;">&#41;</span></pre>
</div></p>

<p>The simplest instance is for the unit type:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> HasTrie <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <span style="color: #050; font-weight: bold;">type</span> Trie <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>  = Id
  trie   f      = Id <span style="color: green;">&#40;</span>f <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
  untrie <span style="color: green;">&#40;</span>Id v<span style="color: green;">&#41;</span> = λ <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span> → v</pre>
</div></p>

<p>For consistency with other types, I just made a small change from the previous version, which used <code>const v</code> instead of the stricter <code>λ () → v</code>.</p>

<p>Sums and products are a little more intricate:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span>HasTrie a, HasTrie b<span style="color: green;">&#41;</span> ⇒ HasTrie <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> a b<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <span style="color: #050; font-weight: bold;">type</span> Trie <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> a b<span style="color: green;">&#41;</span> = Trie a :*: Trie b
  trie   f           = trie <span style="color: green;">&#40;</span>f ∘ Left<span style="color: green;">&#41;</span> :*: trie <span style="color: green;">&#40;</span>f ∘ Right<span style="color: green;">&#41;</span>
  untrie <span style="color: green;">&#40;</span>ta :*: tb<span style="color: green;">&#41;</span> = untrie ta `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:either"><span style="font-weight: bold;">either</span></a>` untrie tb
&nbsp;
<span style="color: #050; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span>HasTrie a, HasTrie b<span style="color: green;">&#41;</span> ⇒ HasTrie <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: #050; font-weight: bold;">type</span> Trie <span style="color: green;">&#40;</span>a , b<span style="color: green;">&#41;</span> = Trie a :. Trie b
  trie   f = O <span style="color: green;">&#40;</span>trie <span style="color: green;">&#40;</span>trie ∘ <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:curry"><span style="font-weight: bold;">curry</span></a> f<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
  untrie <span style="color: green;">&#40;</span>O tt<span style="color: green;">&#41;</span> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:uncurry"><span style="font-weight: bold;">uncurry</span></a> <span style="color: green;">&#40;</span>untrie ∘ untrie tt<span style="color: green;">&#41;</span></pre>
</div></p>

<p>These trie types are not just strict, they&#8217;re <em>hyper-strict</em>.
During trie search, arguments get thorougly evaluated.
(See Section 9 in the paper <em><a href="http://conal.net/blog/posts/denotational-design-with-type-class-morphisms/" title="blog post">Denotational design with type class morphisms</a></em>.)
In other words, all of the points of possible undefinedness are lost.</p>

<h3>Strict and non-strict memo tries</h3>

<p>The formulation of strict tries will look very like the hyper-strict tries we&#8217;ve already seen, with new names for the associated trie type and the conversion methods:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> k :→ v = STrie k v
&nbsp;
<span style="color: #050; font-weight: bold;">class</span> HasTrie k <span style="color: #050; font-weight: bold;">where</span>
    <span style="color: #050; font-weight: bold;">type</span> STrie k :: * → *
    sTrie   ::             <span style="color: green;">&#40;</span>k  → v<span style="color: green;">&#41;</span> → <span style="color: green;">&#40;</span>k :→ v<span style="color: green;">&#41;</span>
    sUntrie :: HasLub v ⇒ <span style="color: green;">&#40;</span>k :→ v<span style="color: green;">&#41;</span> → <span style="color: green;">&#40;</span>k  → v<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Besides renaming, I&#8217;ve also added a <code>HasLub</code> constraint for <code>sUntrie</code>, which we&#8217;ll need later.</p>

<p>For instance, the (almost) simplest strict trie is the one for the unit type, defined exactly as before (with new names):</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> HasTrie <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <span style="color: #050; font-weight: bold;">type</span> STrie <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>  = Id
  sTrie   f      = Id <span style="color: green;">&#40;</span>f <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
  sUntrie <span style="color: green;">&#40;</span>Id v<span style="color: green;">&#41;</span> = λ <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span> → v</pre>
</div></p>

<p>For <em>non-strict</em> memoization, we&#8217;ll want to recover all of the points of possible undefinedness lost in hyper-strict memoization.
At every level of a structured value, there is the possibility of ⊥ or of a non-⊥ value.
Correspondingly, a non-strict trie consists of the value corresponding to the argument ⊥, together with a strict (but <em>not</em> hyper-strict) trie for the non-⊥ values:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">data</span> Trie k v = Trie v <span style="color: green;">&#40;</span>k :→ v<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #050; font-weight: bold;">type</span> k :→: v = Trie k v</pre>
</div></p>

<p>The conversions between functions and non-strict tries are no longer methods, as they can be defined uniformly for all domain types.
To form a non-strict trie, capture the function&#8217;s value at ⊥, and build a strict (but not hyper-strict) trie:</p>

<p><div>
<pre class="haskell">trie   :: <span style="color: green;">&#40;</span>HasTrie k          <span style="color: green;">&#41;</span> ⇒ <span style="color: green;">&#40;</span>k  →  v<span style="color: green;">&#41;</span> → <span style="color: green;">&#40;</span>k :→: v<span style="color: green;">&#41;</span>
trie f = Trie <span style="color: green;">&#40;</span>f bottom<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>sTrie f<span style="color: green;">&#41;</span></pre>
</div></p>

<p>To convert back from a non-strict trie to a (now memoized) function, combine the information from two sources: the original function&#8217;s value at ⊥, and the function resulting from the strict (but not hyper-strict) trie:</p>

<p><div>
<pre class="haskell">untrie :: <span style="color: green;">&#40;</span>HasTrie k, HasLub v<span style="color: green;">&#41;</span> ⇒ <span style="color: green;">&#40;</span>k :→: v<span style="color: green;">&#41;</span> → <span style="color: green;">&#40;</span>k  →  v<span style="color: green;">&#41;</span>
untrie <span style="color: green;">&#40;</span>Trie b t<span style="color: green;">&#41;</span> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:const"><span style="font-weight: bold;">const</span></a> b ⊔ sUntrie t</pre>
</div></p>

<p>The least-upper-bound (⊔) here is well-defined because its arguments are information-compatible (consistent, non-contradictory).
More strongly, <code>const b ⊑ sUntrie t</code>, i.e., the first argument is an information approximation to (contains no information absent from) the second argument.
Now we see the need for <code>HasLub v</code> in the type of <code>sUntrie</code> above: functions are ⊔-able exactly when their result types are.</p>

<h3>Sums</h3>

<p>Just as non-strict tries contain strict tries, so also strict tries contain non-strict tries.
For instance, consider a sum type, <code>Either a b</code>.
An element is either ⊥ or <code>Left x</code> or <code>Right y</code>, for <code>x :: a</code> and <code>y :: b</code>.
The types <code>a</code> and <code>b</code> also contain a bottom element, so we&#8217;ll need non-strict memo tries for them:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span>HasTrie a, HasTrie b<span style="color: green;">&#41;</span> ⇒ HasTrie <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> a b<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <span style="color: #050; font-weight: bold;">type</span> STrie <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> a b<span style="color: green;">&#41;</span> = Trie a :*: Trie b
  sTrie   f           = trie <span style="color: green;">&#40;</span>f ∘ Left<span style="color: green;">&#41;</span> :*: trie <span style="color: green;">&#40;</span>f ∘ Right<span style="color: green;">&#41;</span>
  sUntrie <span style="color: green;">&#40;</span>ta :*: tb<span style="color: green;">&#41;</span> = untrie ta `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:either"><span style="font-weight: bold;">either</span></a>` untrie tb</pre>
</div></p>

<p>Just as in the unit instance (above), the only visible change from hyper-strict to strict is that the left-hand sides use the strict trie type and operations.
The right-hand sides are written exactly as before, though now they refer to non-strict tries and their operations.</p>

<h3>Products</h3>

<p>With product, we run into some trouble.
As a first attempt, change only the names on the left-hand side:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span>HasTrie a, HasTrie b<span style="color: green;">&#41;</span> ⇒ HasTrie <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: #050; font-weight: bold;">type</span> STrie <span style="color: green;">&#40;</span>a , b<span style="color: green;">&#41;</span> = Trie a :. Trie b
  sTrie   f      = O <span style="color: green;">&#40;</span>trie <span style="color: green;">&#40;</span>trie ∘ <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:curry"><span style="font-weight: bold;">curry</span></a> f<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
  sUntrie <span style="color: green;">&#40;</span>O tt<span style="color: green;">&#41;</span> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:uncurry"><span style="font-weight: bold;">uncurry</span></a> <span style="color: green;">&#40;</span>untrie ∘ untrie tt<span style="color: green;">&#41;</span></pre>
</div></p>

<p>This <code>sUntrie</code> definition, however, leads to an error in type-checking:</p>

<p><div>
<pre class="haskell">Could <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:not"><span style="font-weight: bold;">not</span></a> deduce <span style="color: green;">&#40;</span>HasLub <span style="color: green;">&#40;</span>Trie b v<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> from the context <span style="color: green;">&#40;</span>HasLub v<span style="color: green;">&#41;</span>
  arising from a use <span style="color: #050; font-weight: bold;">of</span> `untrie'</pre>
</div></p>

<p>The troublesome <code>untrie</code> use is the one applied directly to <code>tt</code>.
(Thank you for column numbers, GHC.)</p>

<p>So what&#8217;s going on here?
Since <code>sUntrie</code> in this definition takes a <code>(a,b) :→ v</code>, or equivalently, <code>STrie (a,b) v</code>,</p>

<p><div>
<pre class="haskell">O tt :: <span style="color: green;">&#40;</span>a,b<span style="color: green;">&#41;</span> :→ v
     :: STrie <span style="color: green;">&#40;</span>a,b<span style="color: green;">&#41;</span> v
     :: <span style="color: green;">&#40;</span>Trie a :. Trie b<span style="color: green;">&#41;</span> v</pre>
</div></p>

<p>The definition of type composition (from <a href="http://conal.net/blog/posts/elegant-memoization-with-higher-order-types/" title="blog post">an earlier post</a>) is</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">newtype</span> <span style="color: green;">&#40;</span>g :. f<span style="color: green;">&#41;</span> x = O <span style="color: green;">&#40;</span>g <span style="color: green;">&#40;</span>f x<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre>
</div></p>

<p>So</p>

<p><div>
<pre class="haskell">tt :: Trie a <span style="color: green;">&#40;</span>Trie b v<span style="color: green;">&#41;</span>
   :: a :→: b :→: v</pre>
</div></p>

<p>and</p>

<p><div>
<pre class="haskell">untrie tt :: HasLub <span style="color: green;">&#40;</span>b :→: v<span style="color: green;">&#41;</span> ⇒ a → <span style="color: green;">&#40;</span>b :→: v<span style="color: green;">&#41;</span></pre>
</div></p>

<p>The <code>HasLub</code> constraint comes from the type of <code>untrie</code> (above).</p>

<p>Continuing,</p>

<p><div>
<pre class="haskell">untrie ∘ untrie tt ::
  <span style="color: green;">&#40;</span>HasLub v, HasLub <span style="color: green;">&#40;</span>b :→: v<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> ⇒ a → <span style="color: green;">&#40;</span>b → v<span style="color: green;">&#41;</span>
&nbsp;
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:uncurry"><span style="font-weight: bold;">uncurry</span></a> <span style="color: green;">&#40;</span>untrie ∘ untrie tt<span style="color: green;">&#41;</span> ::
  <span style="color: green;">&#40;</span>HasLub v, HasLub <span style="color: green;">&#40;</span>b :→: v<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> ⇒ <span style="color: green;">&#40;</span>a , b<span style="color: green;">&#41;</span> → v</pre>
</div></p>

<p>which is <em>almost</em> the required type but contains the extra requirement that <code>HasLub (b :→: v)</code>.</p>

<p>Hm.</p>

<p>Looking at the definition of <code>Trie</code> and the definitions of <code>STrie</code> for various domain types <code>b</code>, I think it&#8217;s the case that <code>HasLub (b :→: v)</code>, whenever <code>HasLub v</code>, exactly as needed.
In principle, I could make this requirement of <code>b</code> explicit as a superclass for <code>HasTrie</code>:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">class</span> <span style="color: green;">&#40;</span><span style="color: #050; font-weight: bold;">forall</span> v. HasLub v ⇒ HasLub <span style="color: green;">&#40;</span>b :→: v<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> ⇒ HasTrie k <span style="color: #050; font-weight: bold;">where</span> ...</pre>
</div></p>

<p>However, Haskell&#8217;s type system isn&#8217;t quite expressive enough, even with GHC extensions (as far as I know).</p>

<h4>A possible solution</h4>

<p>We could instead define a functor-level variant of <code>HasLub</code>:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">class</span> HasLubF f <span style="color: #050; font-weight: bold;">where</span>
  lubF :: HasLub v ⇒ f v → f v → f v</pre>
</div></p>

<p>and then use <code>lubF</code> instead of <code>(⊔)</code> in <code>sUntrie</code>.
The revised <code>HasTrie</code> class definition:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">class</span> HasLubF <span style="color: green;">&#40;</span>Trie k<span style="color: green;">&#41;</span> ⇒ HasTrie k <span style="color: #050; font-weight: bold;">where</span>
    <span style="color: #050; font-weight: bold;">type</span> STrie k :: * → *
    sTrie   ::             <span style="color: green;">&#40;</span>k  → v<span style="color: green;">&#41;</span> → <span style="color: green;">&#40;</span>k :→ v<span style="color: green;">&#41;</span>
    sUntrie :: HasLub v ⇒ <span style="color: green;">&#40;</span>k :→ v<span style="color: green;">&#41;</span> → <span style="color: green;">&#40;</span>k  → v<span style="color: green;">&#41;</span></pre>
</div></p>

<p>I would rather not replicate and modify the <code>HasLub</code> class and all of its instances, so I&#8217;m going to set this idea aside and look for another.</p>

<h4>Another route</h4>

<p>Let&#8217;s return to the problematic definition of <code>sUntrie</code> for pairs:</p>

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

<p>and recall that <code>tt :: a :→: b :→: v</code>.
The strategy here was to first convert the outer trie (with domain <code>a</code>) and then the inner trie (with domain <code>b</code>).</p>

<p>Alternatively, we might reverse the order.</p>

<p>If we&#8217;re going to convert inside-out instead of outside-in, then we&#8217;ll need a way to transform each of the <em>range</em> elements of a trie.
Which is exactly what <code>fmap</code> is for.
If only we had a functor instance for <code>Trie a</code>, then we could re-define <code>sUntrie</code> on pair tries as follows:</p>

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

<p>As a sanity check, try compiling this definition.
Sure enough, it&#8217;s okay except for a missing <code>Functor</code> instance:</p>

<p><div>
<pre class="haskell">Could <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:not"><span style="font-weight: bold;">not</span></a> deduce <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> <span style="color: green;">&#40;</span>Trie a<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
  from the context <span style="color: green;">&#40;</span>HasTrie <span style="color: green;">&#40;</span>a, b<span style="color: green;">&#41;</span>, HasTrie a, HasTrie b<span style="color: green;">&#41;</span>
  arising from a use <span style="color: #050; font-weight: bold;">of</span> `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a>'</pre>
</div></p>

<p>Fixed easily enough:</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>STrie k<span style="color: green;">&#41;</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>Trie k<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> f <span style="color: green;">&#40;</span>Trie b t<span style="color: green;">&#41;</span> = Trie <span style="color: green;">&#40;</span>f b<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> f t<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Or even, using the GHC language extensions <code>DeriveFunctor</code> and <code>StandaloneDeriving</code>, just</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">deriving</span> <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>STrie k<span style="color: green;">&#41;</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>Trie k<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Now we get a slightly different error message.
We&#8217;re now missing a Functor instance for <code>STrie a</code> instead of <code>Trie a</code>:</p>

<p><div>
<pre class="haskell">Could <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:not"><span style="font-weight: bold;">not</span></a> deduce <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> <span style="color: green;">&#40;</span>STrie a<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
  from the context <span style="color: green;">&#40;</span>HasTrie <span style="color: green;">&#40;</span>a, b<span style="color: green;">&#41;</span>, HasTrie a, HasTrie b<span style="color: green;">&#41;</span>
  arising from a use <span style="color: #050; font-weight: bold;">of</span> `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a>'</pre>
</div></p>

<p>By the way, we can also construct tries inside-out, if we want:</p>

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

<p>So we&#8217;ll be in good shape <em>if</em> we can satisfy the <code>Functor</code> requirement on strict tries.
Fortunately, all of the strict trie (higher-order) types appearing are indeed functors, since we built them up using functor combinators.</p>

<p>Still, we&#8217;ll have to help the type-checker <em>prove</em> that all of the trie types it involved must indeed be functors.
Again, a superclass constraint can capture this requirement:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">class</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>STrie k<span style="color: green;">&#41;</span> ⇒ HasTrie k <span style="color: #050; font-weight: bold;">where</span> ...</pre>
</div></p>

<p>Unlike <code>HasLub</code>, this time the required constraint is already at the functor level, so we don&#8217;t have to define a new class.
We don&#8217;t even have to define any new instances, as our functor combinators come with <code>Functor</code> instances, all of which can be derived automatically by GHC.</p>

<p>With this one change, all of the <code>HasTrie</code> instances go through.</p>

<h3>Isomorphisms</h3>

<p>As pointed out in <em><a href="http://conal.net/blog/posts/memoizing-higher-order-functions/" title="blog post">Memoizing higher-order functions</a></em>, type isomorphism is the central, repeated theme of functional memoization.
In addition to the isomorphism between functions and tries, the tries for many types are given via isomorphism with other types that have tries.
In this way, we only have to define tries for our tiny set of functor combinators.</p>

<p>Isomorphism support is as in <em><a href="http://conal.net/blog/posts/elegant-memoization-with-higher-order-types/" title="blog post">Elegant memoization with higher-order types</a></em>, just using the new names:</p>

<p><div>
<pre class="haskell">#define HasTrieIsomorph<span style="color: green;">&#40;</span>Context,Type,IsoType,toIso,fromIso<span style="color: green;">&#41;</span> \
<span style="color: #050; font-weight: bold;">instance</span> Context ⇒ HasTrie <span style="color: green;">&#40;</span>Type<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span> <span style="color: green;">&#123;</span> \
  <span style="color: #050; font-weight: bold;">type</span> STrie <span style="color: green;">&#40;</span>Type<span style="color: green;">&#41;</span> = STrie <span style="color: green;">&#40;</span>IsoType<span style="color: green;">&#41;</span>; \
  sTrie f = sTrie <span style="color: green;">&#40;</span>f ∘ <span style="color: green;">&#40;</span>fromIso<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>; \
  sUntrie t = sUntrie t ∘ <span style="color: green;">&#40;</span>toIso<span style="color: green;">&#41;</span>; \
<span style="color: green;">&#125;</span></pre>
</div></p>

<p>Note the use of strict tries even on the right-hand sides.</p>

<p><em>Aside:</em> as mentioned in <em><a href="http://conal.net/blog/posts/composing-memo-tries/" title="blog post">Composing memo tries</a></em>, <code>trie</code>/<code>untrie</code> forms not just an isomorphism but a pair of <a href="http://conal.net/blog/tag/type-class-morphism/" title="Posts on type class morphisms">type class morphism</a>s (TCMs).
(For motivation and examples of TCMs in software design, see <em><a href="http://conal.net/blog/posts/denotational-design-with-type-class-morphisms/" title="blog post">Denotational design with type class morphisms</a></em>.)</p>

<h3>Regular data types</h3>

<p><em>Regular data types</em> are isomorphic to fixed-points of functors.
<em><a href="http://conal.net/blog/posts/elegant-memoization-with-higher-order-types/" title="blog post">Elegant memoization with higher-order types</a></em> gives a brief introduction to these notions and pointers to more information.
That post also shows how to use the <code>Regular</code> type class and its instances (defined for other purposes as well) to provide hyper-strict memo tries for all regular data types.</p>

<p>Switching from hyper-strict to non-strict raises an awkward issue.
The functor isomorphisms we used only correct for fully defined data-types.
When we allow full or partial undefinedness, as in a lazy language like Haskell, our isomorphisms break down.</p>

<p>Following <em><a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.142.4778" title="Paper by Thomas Noort, Alexey Rodriguez, Stefan Holdermans, Johan Jeuring, Bastiaan Heeren">A Lightweight Approach to Datatype-Generic Rewriting</a></em>, here is the class I used, where &#8220;<code>PF</code>&#8221; stands for &#8220;pattern functor&#8221;:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">class</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>PF t<span style="color: green;">&#41;</span> ⇒ Regular t <span style="color: #050; font-weight: bold;">where</span>
  <span style="color: #050; font-weight: bold;">type</span> PF t :: * → *
  unwrap :: t → PF t t
  wrap   :: PF t t → t</pre>
</div></p>

<p>The <code>unwrap</code> method peels off a single layer from a regular type.
For example, the top level of a list is either a unit (nil) or a pair (cons) of an element and a hole in which a list can be placed.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> Regular <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> <span style="color: #050; font-weight: bold;">where</span>
  <span style="color: #050; font-weight: bold;">type</span> PF <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> = Unit :+: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Const"><span style="color: #833; font-weight: bold;">Const</span></a> a :*: Id   <span style="color: #5d478b; font-style: italic;">-- where Unit == Const ()</span>
&nbsp;
  unwrap <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>     = InL <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Const"><span style="color: #833; font-weight: bold;">Const</span></a> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
  unwrap <span style="color: green;">&#40;</span>a:<span style="color: #050; font-weight: bold;">as</span><span style="color: green;">&#41;</span> = InR <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Const"><span style="color: #833; font-weight: bold;">Const</span></a> a :*: Id <span style="color: #050; font-weight: bold;">as</span><span style="color: green;">&#41;</span>
&nbsp;
  wrap <span style="color: green;">&#40;</span>InL <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Const"><span style="color: #833; font-weight: bold;">Const</span></a> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>          = <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>
  wrap <span style="color: green;">&#40;</span>InR <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Const"><span style="color: #833; font-weight: bold;">Const</span></a> a :*: Id <span style="color: #050; font-weight: bold;">as</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> = a:<span style="color: #050; font-weight: bold;">as</span></pre>
</div></p>

<p>The catch here is that the <code>unwrap</code> and <code>wrap</code> methods do not really form an isomorphism.
Instead, they satisfy a weaker connection: they form embedding/projection pair.
That is,</p>

<p><div>
<pre class="haskell">wrap ∘ unwrap ≡ <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:id"><span style="font-weight: bold;">id</span></a>
unwrap ∘ wrap ⊑ <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>To see the mismatch between <code>[a]</code> and <code>PF [a] [a]</code>, note that the latter has opportunities for partial undefinedness that have no corresponding opportunities in <code>[a]</code>.
Specifically, ⊥ could occur at type <code>Const () [a]</code>, <code>()</code>,  <code>(Const a :*: Id) [a]</code>, <code>Const a [a]</code>, or <code>Id [a]</code>.
Any of these ⊥ values will result in <code>wrap</code> returning ⊥ altogether.
For instance, if</p>

<p><div>
<pre class="haskell">oops :: PF <span style="color: green;">&#91;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Integer"><span style="color: #833; font-weight: bold;">Integer</span></a><span style="color: green;">&#93;</span>
oops = InR <span style="color: green;">&#40;</span>⊥ :*: Id <span style="color: green;">&#91;</span><span style="color: red;">3</span>,<span style="color: red;">5</span><span style="color: green;">&#93;</span><span style="color: green;">&#41;</span></pre>
</div></p>

<p>then</p>

<p><div>
<pre class="haskell">unwrap <span style="color: green;">&#40;</span>wrap oops<span style="color: green;">&#41;</span> ≡ unwrap ⊥ ≡ ⊥ ⊑ oops</pre>
</div></p>

<p>By examining various cases, we can prove that <code>unwrap (wrap p) ⊑ p</code> for all <code>p</code>, which is to say <code>unwrap ∘ wrap ⊑ id</code>, since
information ordering on functions is defined point-wise.
(See <em><a href="http://conal.net/blog/posts/merging-partial-values/" title="blog post">Merging partial values</a></em>.)</p>

<p>Examining the definition of <code>unwrap</code> above shows that these troublesome ⊥ points do not arise, and so a trivial equational proof shows that <code>wrap ∘ unwrap ≡ id</code>.</p>

<p>In the context of memoization, the additional undefined values are problematic.
Consider the case of lists.
The specification macro</p>

<p><div>
<pre class="haskell">HasTrieRegular1<span style="color: green;">&#40;</span><span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>, ListSTrie<span style="color: green;">&#41;</span></pre>
</div></p>

<p>expands into a <code>newtype</code> and its <code>HasTrie</code> instance.
Changing only the associated type and method names in the <a href="http://conal.net/blog/posts/elegant-memoization-with-higher-order-types/" title="blog post">version for hyper-strict memoization</a>:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">newtype</span> ListSTrie a v = ListSTrie <span style="color: green;">&#40;</span>PF <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> :→: v<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #050; font-weight: bold;">instance</span> HasTrie a ⇒ HasTrie <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> <span style="color: #050; font-weight: bold;">where</span>
  <span style="color: #050; font-weight: bold;">type</span> STrie <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> = ListSTrie a
  sTrie f = ListSTrie <span style="color: green;">&#40;</span>sTrie <span style="color: green;">&#40;</span>f . wrap<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
  sUntrie <span style="color: green;">&#40;</span>ListSTrie t<span style="color: green;">&#41;</span> = sUntrie t . unwrap</pre>
</div></p>

<p>Note that the trie in <code>ListSTrie</code> trie contains entries for many ⊥ sub-elements that do not correspond to any list values.
The memoized function is <code>f ∘ wrap</code>, which will have many fewer ⊥ possibilities than the trie structure supports.
At each of the superfluous ⊥ points, the function sampled is strict, so the <code>Trie</code> (rather than <code>STrie</code>) will contain a predictable ⊥.
Considering the definition of <code>untrie</code>:</p>

<p><div>
<pre class="haskell">untrie <span style="color: green;">&#40;</span>Trie b t<span style="color: green;">&#41;</span> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:const"><span style="font-weight: bold;">const</span></a> b ⊔ sUntrie t</pre>
</div></p>

<p>we know <code>b ≡ ⊥</code>, and so <code>const b ⊔ sUntrie t ≡ sUntrie t</code>.
Thus, at these points, the ⊥ value is never helpful, and we could use a strict (though not hyper-strict) trie instead of a non-strict trie.</p>

<p>Perhaps we could safely ignore this whole issue and lose only some efficiency, rather than correctness.
Still, I&#8217;d rather build and traverse just the right trie for our regular types.</p>

<p>As this post is already longer than I intended, and my attention is wandering, I&#8217;ll publish it here and pick up later.
Comments &amp; suggestions please!</p>
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/details-for-nonstrict-memoization-part-1/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Memoizing higher-order functions</title>
		<link>http://conal.net/blog/posts/memoizing-higher-order-functions/</link>
		<comments>http://conal.net/blog/posts/memoizing-higher-order-functions/#comments</comments>
		<pubDate>Wed, 21 Jul 2010 15:41:17 +0000</pubDate>
		<dc:creator>conal</dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[isomorphism]]></category>
		<category><![CDATA[memoization]]></category>
		<category><![CDATA[trie]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=119</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Memoizing higher-order functions

Or:

Higher-order memoization

Tags: Memoization, isomorphism, trie

URL: http://conal.net/blog/posts/memoizing-higher-order-functions/

-->

<!-- references -->

<!-- teaser -->

<p>Memoization incrementally converts functions into data structures.
It pays off when a function is repeatedly applied to the same arguments and applying the function is more expensive than accessing the corresponding data structure.</p>

<p>In <em>lazy functional</em> memoization, the conversion from function to data structure happens all at once from a denotational perspective, and incrementally from an operational perspective.
See <em><a href="http://conal.net/blog/posts/elegant-memoization-with-functional-memo-tries/" title="blog post">Elegant memoization with functional memo tries</a></em> and <em><a href="http://conal.net/blog/posts/elegant-memoization-with-higher-order-types/" title="blog post">Elegant memoization with higher-order types</a></em>.</p>

<p>As Ralf Hinze presented in <em><a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.8.4069" title="Paper: &quot;Generalizing Generalized Tries&quot; by Ralf Hinze">Generalizing Generalized Tries</a></em>, trie-based memoization follows from three simple isomorphisms involving functions types:</p>

<p><div class=latex><img src='http://conal.net/blog/wp-content/latex/e12/e121d75c52269c4d1276012143af51f0-ffffff000000.png' alt='1 \to a \cong a' class='latex' /></div>
<div class=latex><img src='http://conal.net/blog/wp-content/latex/d16/d163a6d2e1dcc0eff65ddbeb410c494b-ffffff000000.png' alt='(a + b) \to c \cong (a \to c) \times (b \to c)' class='latex' /></div>
<div class=latex><img src='http://conal.net/blog/wp-content/latex/67b/67b03e3a05d4854c5c9c098102ccc5b8-ffffff000000.png' alt='(a \times b) \to c \cong a \to (b \to c)' class='latex' /></div></p>

<p>which correspond to the familiar laws of exponents</p>

<p><div class=latex><img src='http://conal.net/blog/wp-content/latex/3db/3dbe60976b3ddccd1f9507e219cad153-ffffff000000.png' alt='a ^ 1 = a' class='latex' /></div>
<div class=latex><img src='http://conal.net/blog/wp-content/latex/81e/81ea13dadaa3c958d16a5a640e3caa37-ffffff000000.png' alt='c^{a + b} = c^a \times c^b' class='latex' /></div>
<div class=latex><img src='http://conal.net/blog/wp-content/latex/229/2297dd299e17e365b47869ee60be3c2d-ffffff000000.png' alt='c^{a \times b} = (c ^ b) ^ a' class='latex' /></div></p>

<p>When applied as a transformation from left to right, each law simplifies the domain part of a function type.
Repeated application of the rules then eliminate all function types or reduce them to functions of atomic types.
These atomic domains are eliminated as well by additional mappings, such as between a natural number and a list of bits (as in <a href="http://hackage.haskell.org/packages/archive/containers/0.2.0.1/doc/html/Data-IntMap.html" title="hackage package">patricia trees</a>).
Algebraic data types corresponding to sums of products and so are eliminated by the sum and product rules.
<em>Recursive</em> algebraic data types (lists, trees, etc) give rise to correspondingly recursive trie types.</p>

<p>So, with a few simple and familiar rules, we can memoize functions over an infinite variety of common types.
Have we missed any?</p>

<p>Yes.
<em>What about functions over functions?</em></p>

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

<ul>
<li>2010-07-22: Made the memoization example polymorphic and switched from pairs to lists.  The old example accidentally coincided with a specialized version of <code>trie</code> itself.</li>
</ul>

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

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

<h3>Tries</h3>

<p>In <em><a href="http://conal.net/blog/posts/elegant-memoization-with-higher-order-types/" title="blog post">Elegant memoization with higher-order types</a></em>, I showed a formulation of functional memoization using functor combinators.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> k :→: v = Trie k v
&nbsp;
<span style="color: #050; font-weight: bold;">class</span> HasTrie k <span style="color: #050; font-weight: bold;">where</span>
    <span style="color: #050; font-weight: bold;">type</span> Trie k :: * → *
    trie   :: <span style="color: green;">&#40;</span>k  →  v<span style="color: green;">&#41;</span> → <span style="color: green;">&#40;</span>k :→: v<span style="color: green;">&#41;</span>
    untrie :: <span style="color: green;">&#40;</span>k :→: v<span style="color: green;">&#41;</span> → <span style="color: green;">&#40;</span>k  →  v<span style="color: green;">&#41;</span></pre>
</div></p>

<p>I will describe higher-order memoization in terms of this formulation.
I imagine it would also work out, though less elegantly, in the associated data types formulation described in <em><a href="http://conal.net/blog/posts/elegant-memoization-with-functional-memo-tries/" title="blog post">Elegant memoization with functional memo tries</a></em>.</p>

<h3>Domain isomorphisms</h3>

<p><em><a href="http://conal.net/blog/posts/elegant-memoization-with-higher-order-types/" title="blog post">Elegant memoization with higher-order types</a></em> showed how to define a <code>HasTrie</code> instance in terms of the instance of an isomorphic type, e.g., reducing tuples to nested pairs or booleans to a sum of unit types.
A C macro, <code>HasTrieIsomorph</code> encapsulates the domain isomorphism technique.
For instance, to reduce triples to pairs:</p>

<p><div>
<pre class="haskell">HasTrieIsomorph<span style="color: green;">&#40;</span> <span style="color: green;">&#40;</span>HasTrie a, HasTrie b, HasTrie c<span style="color: green;">&#41;</span>, <span style="color: green;">&#40;</span>a,b,c<span style="color: green;">&#41;</span>, <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>a,b<span style="color: green;">&#41;</span>,c<span style="color: green;">&#41;</span>
               , λ <span style="color: green;">&#40;</span>a,b,c<span style="color: green;">&#41;</span> → <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>a,b<span style="color: green;">&#41;</span>,c<span style="color: green;">&#41;</span>, λ <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>a,b<span style="color: green;">&#41;</span>,c<span style="color: green;">&#41;</span> → <span style="color: green;">&#40;</span>a,b,c<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre>
</div></p>

<p>This isomorphism technique applies as well to the standard functor combinators used for constructing tries (any many other purposes).
Those combinators again:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">data</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Const"><span style="color: #833; font-weight: bold;">Const</span></a> x a = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Const"><span style="color: #833; font-weight: bold;">Const</span></a> x
&nbsp;
<span style="color: #050; font-weight: bold;">data</span> Id a = Id a
&nbsp;
<span style="color: #050; font-weight: bold;">data</span> <span style="color: green;">&#40;</span>f :*: g<span style="color: green;">&#41;</span> a = f a :*: g a
&nbsp;
<span style="color: #050; font-weight: bold;">data</span> <span style="color: green;">&#40;</span>f :+: g<span style="color: green;">&#41;</span> a = InL <span style="color: green;">&#40;</span>f a<span style="color: green;">&#41;</span> | InR <span style="color: green;">&#40;</span>g a<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #050; font-weight: bold;">newtype</span> <span style="color: green;">&#40;</span>g :. f<span style="color: green;">&#41;</span> a = O <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></pre>
</div></p>

<p>and their trie definitions:</p>

<p><div>
<pre class="haskell">HasTrieIsomorph<span style="color: green;">&#40;</span> HasTrie a, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Const"><span style="color: #833; font-weight: bold;">Const</span></a> a x, a, getConst, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Const"><span style="color: #833; font-weight: bold;">Const</span></a> <span style="color: green;">&#41;</span>
&nbsp;
HasTrieIsomorph<span style="color: green;">&#40;</span> HasTrie a, Id a, a, unId, Id <span style="color: green;">&#41;</span>
&nbsp;
HasTrieIsomorph<span style="color: green;">&#40;</span> <span style="color: green;">&#40;</span>HasTrie <span style="color: green;">&#40;</span>f a<span style="color: green;">&#41;</span>, HasTrie <span style="color: green;">&#40;</span>g a<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
               , <span style="color: green;">&#40;</span>f :*: g<span style="color: green;">&#41;</span> a, <span style="color: green;">&#40;</span>f a,g a<span style="color: green;">&#41;</span>
               , λ <span style="color: green;">&#40;</span>fa :*: ga<span style="color: green;">&#41;</span> → <span style="color: green;">&#40;</span>fa,ga<span style="color: green;">&#41;</span>, λ <span style="color: green;">&#40;</span>fa,ga<span style="color: green;">&#41;</span> → <span style="color: green;">&#40;</span>fa :*: ga<span style="color: green;">&#41;</span> <span style="color: green;">&#41;</span>
&nbsp;
HasTrieIsomorph<span style="color: green;">&#40;</span> <span style="color: green;">&#40;</span>HasTrie <span style="color: green;">&#40;</span>f a<span style="color: green;">&#41;</span>, HasTrie <span style="color: green;">&#40;</span>g a<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
               , <span style="color: green;">&#40;</span>f :+: g<span style="color: green;">&#41;</span> a, <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 a<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>g a<span style="color: green;">&#41;</span>
               , eitherF Left Right, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:either"><span style="font-weight: bold;">either</span></a> InL InR <span style="color: green;">&#41;</span>
&nbsp;
HasTrieIsomorph<span style="color: green;">&#40;</span> HasTrie <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>
               , <span style="color: green;">&#40;</span>g :. f<span style="color: green;">&#41;</span> a, g <span style="color: green;">&#40;</span>f a<span style="color: green;">&#41;</span> , unO, O <span style="color: green;">&#41;</span></pre>
</div></p>

<p>The <code>eitherF</code> function is a variation on <a href="http://haskell.org/ghc/docs/6.12.1/html/libraries/base-4.2.0.0/Prelude.html#v:either"><code>either</code></a>:</p>

<p><div>
<pre class="haskell">eitherF :: <span style="color: green;">&#40;</span>f a → b<span style="color: green;">&#41;</span> → <span style="color: green;">&#40;</span>g a → b<span style="color: green;">&#41;</span> → <span style="color: green;">&#40;</span>f :+: g<span style="color: green;">&#41;</span> a → b
eitherF p _ <span style="color: green;">&#40;</span>InL fa<span style="color: green;">&#41;</span> = p fa
eitherF _ q <span style="color: green;">&#40;</span>InR ga<span style="color: green;">&#41;</span> = q ga</pre>
</div></p>

<h3>Higher-order memoization</h3>

<p>Now higher-order memoization is easy.
Apply yet another isomorphism, this time between functions and tries:
The <code>trie</code> and <code>untrie</code> methods are exactly the mappings we need.</p>

<p><div>
<pre class="haskell">HasTrieIsomorph<span style="color: green;">&#40;</span> <span style="color: green;">&#40;</span>HasTrie a, HasTrie <span style="color: green;">&#40;</span>a :→: b<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
               , a → b, a :→: b, trie, untrie<span style="color: green;">&#41;</span></pre>
</div></p>

<p>So, to memoize a higher-order function <code>f :: (a → b) → v</code>, we only a trie type for <code>a</code> and one for <code>a :→: b</code>.
The latter (tries for trie-valued domains) are provided by the isomorphisms above, and additional ones.</p>

<h3>Demo</h3>

<p>Our sample higher-order function will take a function of booleans and yield its value at <code>False</code> and at <code>True</code>:</p>

<p><div>
<pre class="haskell">ft1 :: <span style="color: green;">&#40;</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> → a<span style="color: green;">&#41;</span> → <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span>
ft1 f = <span style="color: green;">&#91;</span>f False, f True<span style="color: green;">&#93;</span></pre>
</div></p>

<p>A sample input converts <code>False</code> to <code>0</code> and <code>True</code> to <code>1</code>:</p>

<p><div>
<pre class="haskell">f1 :: <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:Int"><span style="color: #833; font-weight: bold;">Int</span></a>
f1 False = <span style="color: red;">0</span>
f1 True  = <span style="color: red;">1</span></pre>
</div></p>

<p>A sample run without memoization:</p>

<p><div>
<pre class="haskell">*FunctorCombo.MemoTrie&gt; ft1 f1
<span style="color: green;">&#91;</span><span style="color: red;">0</span>,<span style="color: red;">1</span><span style="color: green;">&#93;</span></pre>
</div></p>

<p>and one with memoization:</p>

<p><div>
<pre class="haskell">*FunctorCombo.MemoTrie&gt; memo ft1 f1
<span style="color: green;">&#91;</span><span style="color: red;">0</span>,<span style="color: red;">1</span><span style="color: green;">&#93;</span></pre>
</div></p>

<p>To illustrate what&#8217;s going on behind the scenes, the following definitions (all of which type-check) progressively reveal the representation of the underlying memo trie.
Most steps result from inlining a single <code>Trie</code> definition (as well as switching between <code>Trie k v</code> and the synonymous form <code>k :-&gt;: v</code>).</p>

<p><div>
<pre class="haskell">trie1a :: HasTrie a =&gt; <span style="color: green;">&#40;</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> -&gt; a<span style="color: green;">&#41;</span> :-&gt;: <span style="color: green;">&#40;</span>a, a<span style="color: green;">&#41;</span>
trie1a = trie ft1
&nbsp;
trie1b :: HasTrie a =&gt; <span style="color: green;">&#40;</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> :-&gt;: a<span style="color: green;">&#41;</span> :-&gt;: <span style="color: green;">&#40;</span>a, a<span style="color: green;">&#41;</span>
trie1b = trie1a
&nbsp;
trie1c :: HasTrie a =&gt; <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><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span> :-&gt;: a<span style="color: green;">&#41;</span> :-&gt;: <span style="color: green;">&#40;</span>a, a<span style="color: green;">&#41;</span>
trie1c = trie1a
&nbsp;
trie1d :: HasTrie a =&gt; <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>Trie <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span> :*: Trie <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> a<span style="color: green;">&#41;</span> :-&gt;: <span style="color: green;">&#40;</span>a, a<span style="color: green;">&#41;</span>
trie1d = trie1a
&nbsp;
trie1e :: HasTrie a =&gt; <span style="color: green;">&#40;</span>Trie <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span> a, Trie <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span> a<span style="color: green;">&#41;</span> :-&gt;: <span style="color: green;">&#40;</span>a, a<span style="color: green;">&#41;</span>
trie1e = trie1a
&nbsp;
trie1f :: HasTrie a =&gt; <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span><span style="color: green;">&#41;</span> :-&gt;: a, <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span> :-&gt;: a<span style="color: green;">&#41;</span> :-&gt;: <span style="color: green;">&#40;</span>a, a<span style="color: green;">&#41;</span>
trie1f = trie1a
&nbsp;
trie1g :: HasTrie a =&gt; <span style="color: green;">&#40;</span>a, a<span style="color: green;">&#41;</span> :-&gt;: <span style="color: green;">&#40;</span>a, a<span style="color: green;">&#41;</span>
trie1g = trie1a
&nbsp;
trie1h :: HasTrie a =&gt; <span style="color: green;">&#40;</span>Trie a :. Trie a<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>a, a<span style="color: green;">&#41;</span>
trie1h = trie1a
&nbsp;
trie1i :: HasTrie a =&gt; a :-&gt;: a :-&gt;: <span style="color: green;">&#40;</span>a, a<span style="color: green;">&#41;</span>
trie1i = unO trie1a</pre>
</div></p>

<h3>Pragmatics</h3>

<p>I&#8217;m happy with the correctness and elegance of the method in this post.
It gives me the feeling of inevitable simplicity that I strive for &#8212; obvious in hindsight.
What about performance?
After all, memoization is motivated by a desire to efficiency &#8212; specifically, to reduce the cost of repeatedly applying the same function to the same argument, while keeping almost all of the modularity &amp; simplicity of a naïve algorithm.</p>

<p>Memoization pays off when (a) a function is repeatedly applied to some arguments, and (b) when the cost of recomputing an application exceeds the cost of finding the previously computed result.
(I&#8217;m over-simplifying here.  Space efficiency matters also and can affect time efficiency.)
The isomorphism technique used in this post and <a href="http://conal.net/blog/posts/elegant-memoization-with-higher-order-types/" title="blog post">a previous one</a> requires transforming an argument to the isomorphic type for each look-up and from the isomorphic type for each application.
(I&#8217;m using &#8220;isomorphic type&#8221; to mean the type for which a <code>HasTrie</code> instance is already defined.)
When these transformations are between function and trie form, I wonder how high the break-even threshold becomes.</p>

<p>How might we avoid these transformations, thus reducing the overhead of memoizing?</p>

<p>For conversion to isomorphic type during trie lookup, perhaps the cost could be reduced substantially through deforestation &#8212; inlining chains of <code>untrie</code> methods and applying optimizations to eliminate the many intermediate representation layers.
GHC has gotten awfully good at this sort of thing.
Maybe someone with more Haskell performance analysis &amp; optimization experience than I have would be interested in collaborating.</p>

<p>For trie construction, I suspect the conversion <em>back</em> from the isomorphic type could be avoided by somehow holding onto the original form of the argument, before it was converted to the isomorphic type.
I haven&#8217;t attempted this idea yet.</p>

<p>Another angle on reducing the cost of the isomorphism technique is to use memoization!
After all, if memoizing is worthwhile at all, there will be repeated applications of the memoized function to the same arguments.
Exactly in such a case, the conversion of arguments to isomorphic form will also be done repeatedly for these same arguments.
When a conversion is both expensive and repeated, we&#8217;d like to memoize.
I don&#8217;t know how to get off the ground with this idea, however.
If I&#8217;m trying to memoize a function of type <code>a → b</code>, then the required conversion has type <code>a → a'</code> for some type <code>a'</code> with a <code>HasTrie</code> instance.
Memoizing that conversion is just as hard as memoizing the function we started with.</p>

<h3>Conclusion</h3>

<p>Existing accounts of functional memoization I know of cover functions of the unit type, sums, and products, and they do so quite elegantly.</p>

<p><em>Type isomorphisms</em> form the consistent, central theme in this work.
Functions from unit, sums and products have isomorphic forms with simpler domain types (and so on, recursively).
Additional isomorphisms extend these fundamental building blocks to many other types, including integer types and algebraic data types.
However, functions over function-valued domains are conspicuously missing (though I hadn&#8217;t noticed until recently).
This post fills that gap neatly, using yet another isomorphism, and moreover an isomorphism that has been staring us in the face all along: the one between functions and tries.</p>

<p>I wonder:</p>

<ul>
<li>Given how this trick shouts to be noticed, has it been discovered and written up?</li>
<li>How useful will higher-order memoization turn out to be?</li>
<li>How efficient is the straightforward implementation given above?</li>
<li>Can the conversions between isomorphic domain types be done inexpensively, perhaps eliminating many altogether?</li>
<li>How does <a href="http://conal.net/blog/posts/nonstrict-memoization/" title="blog post">non-strict memoization</a> fit in with higher-order memoization?</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/memoizing-higher-order-functions/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Elegant memoization with higher-order types</title>
		<link>http://conal.net/blog/posts/elegant-memoization-with-higher-order-types/</link>
		<comments>http://conal.net/blog/posts/elegant-memoization-with-higher-order-types/#comments</comments>
		<pubDate>Wed, 21 Jul 2010 04:48:22 +0000</pubDate>
		<dc:creator>conal</dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[functor]]></category>
		<category><![CDATA[isomorphism]]></category>
		<category><![CDATA[memoization]]></category>
		<category><![CDATA[trie]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=117</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Elegant memoization with higher-order types

Tags: functor, memoization, trie

URL: http://conal.net/blog/posts/elegant-memoization-with-higher-order-types/

-->

<!-- references -->

<!-- teaser -->

<p>A while back, I got interested in functional memoization, especially after seeing some code from Spencer Janssen using the essential idea of Ralf Hinze&#8217;s paper <em><a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.8.4069" title="Paper: &quot;Generalizing Generalized Tries&quot; by Ralf Hinze">Generalizing Generalized Tries</a></em>.
The blog post <em><a href="http://conal.net/blog/posts/elegant-memoization-with-functional-memo-tries/" title="blog post">Elegant memoization with functional memo tries</a></em> describes a library, <a href="http://haskell.org/haskellwiki/MemoTrie" title="Haskell wiki page for the MemoTrie library">MemoTrie</a>, based on both of these sources, and using <a href="http://www.cse.unsw.edu.au/~chak/papers/papers.html#assoc" title="Paper: &quot;Associated Types with Class&quot;">associated data types</a>.
I would have rather used associated type synonyms and standard types, but I couldn&#8217;t see how to get the details to work out.
Recently, while playing with functor combinators, I realized that they might work for memoization, which they do quite nicely.</p>

<p>This blog post shows how functor combinators lead to an even more elegant formulation of functional memoization.
The code is available as part of the <a href="http://hackage.haskell.org/package/functor-combo" title="Hackage entry: functor-combo">functor-combo</a> package.</p>

<p>The techniques in this post are not so much new as they are ones that have recently been sinking in for me.
See <em><a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.8.4069" title="Paper: &quot;Generalizing Generalized Tries&quot; by Ralf Hinze">Generalizing Generalized Tries</a></em>, as well as <em><a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.140.2412" title="Paper by Alexey Rodriguez, Stefan Holdermans, Andres Löh, and Johan Jeuring">Generic programming with fixed points for mutually recursive datatypes</a></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-117"></span></p>

<h3>Tries as associated data type</h3>

<p>The <a href="http://haskell.org/haskellwiki/MemoTrie" title="Haskell wiki page for the MemoTrie library">MemoTrie</a> library is centered on a class <code>HasTrie</code> with an associated data type of tries (efficient indexing structures for memoized functions):</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">class</span> HasTrie k <span style="color: #050; font-weight: bold;">where</span>
    <span style="color: #050; font-weight: bold;">data</span> <span style="color: green;">&#40;</span>:→:<span style="color: green;">&#41;</span> k :: * → *
    trie   :: <span style="color: green;">&#40;</span>k  →  v<span style="color: green;">&#41;</span> → <span style="color: green;">&#40;</span>k :→: v<span style="color: green;">&#41;</span>
    untrie :: <span style="color: green;">&#40;</span>k :→: v<span style="color: green;">&#41;</span> → <span style="color: green;">&#40;</span>k  →  v<span style="color: green;">&#41;</span></pre>
</div></p>

<p>The type <code>a :→: b</code> represents a trie that maps values of type <code>a</code> to values of type <code>b</code>.
The trie representation depends only on <code>a</code>.</p>

<p>Memoization is a simple combination of these two methods:</p>

<p><div>
<pre class="haskell">memo :: HasTrie a ⇒ <span style="color: green;">&#40;</span>a → b<span style="color: green;">&#41;</span> → <span style="color: green;">&#40;</span>a → b<span style="color: green;">&#41;</span>
memo = untrie . trie</pre>
</div></p>

<p>The <code>HasTrie</code> instance definitions correspond to isomorphisms invoving function types.
The isomorphisms correspond to the familiar rules of exponents, if we translate <em>a → b</em> into <em>b^^a^^</em>.
(See <em><a href="http://conal.net/blog/posts/elegant-memoization-with-functional-memo-tries/" title="blog post">Elegant memoization with functional memo tries</a></em> for more explanation.)</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> HasTrie <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
    <span style="color: #050; font-weight: bold;">data</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span> :→: x = UnitTrie x
    trie f = UnitTrie <span style="color: green;">&#40;</span>f <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
    untrie <span style="color: green;">&#40;</span>UnitTrie x<span style="color: green;">&#41;</span> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:const"><span style="font-weight: bold;">const</span></a> x
&nbsp;
<span style="color: #050; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span>HasTrie a, HasTrie b<span style="color: green;">&#41;</span> ⇒ HasTrie <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> a b<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
    <span style="color: #050; font-weight: bold;">data</span> <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> a b<span style="color: green;">&#41;</span> :→: x = EitherTrie <span style="color: green;">&#40;</span>a :→: x<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>b :→: x<span style="color: green;">&#41;</span>
    trie f = EitherTrie <span style="color: green;">&#40;</span>trie <span style="color: green;">&#40;</span>f . Left<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>trie <span style="color: green;">&#40;</span>f . Right<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
    untrie <span style="color: green;">&#40;</span>EitherTrie s t<span style="color: green;">&#41;</span> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:either"><span style="font-weight: bold;">either</span></a> <span style="color: green;">&#40;</span>untrie s<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>untrie t<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #050; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span>HasTrie a, HasTrie b<span style="color: green;">&#41;</span> ⇒ HasTrie <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: #050; font-weight: bold;">data</span> <span style="color: green;">&#40;</span>a,b<span style="color: green;">&#41;</span> :→: x = PairTrie <span style="color: green;">&#40;</span>a :→: <span style="color: green;">&#40;</span>b :→: x<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
    trie f = PairTrie <span style="color: green;">&#40;</span>trie <span style="color: green;">&#40;</span>trie . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:curry"><span style="font-weight: bold;">curry</span></a> f<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
    untrie <span style="color: green;">&#40;</span>PairTrie t<span style="color: green;">&#41;</span> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:uncurry"><span style="font-weight: bold;">uncurry</span></a> <span style="color: green;">&#40;</span>untrie .  untrie t<span style="color: green;">&#41;</span></pre>
</div></p>

<h3>Functors and functor combinators</h3>

<p>For notational convenience, let &#8220;<code>(:→:)</code>&#8221; be a synonym for &#8220;<code>Trie</code>&#8220;:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> k :→: v = Trie k v</pre>
</div></p>

<p>And replace the associated <code>data</code> with an associated <code>type</code>.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">class</span> HasTrie k <span style="color: #050; font-weight: bold;">where</span>
    <span style="color: #050; font-weight: bold;">type</span> Trie k :: * → *
    trie   :: <span style="color: green;">&#40;</span>k  →  v<span style="color: green;">&#41;</span> → <span style="color: green;">&#40;</span>k :→: v<span style="color: green;">&#41;</span>
    untrie :: <span style="color: green;">&#40;</span>k :→: v<span style="color: green;">&#41;</span> → <span style="color: green;">&#40;</span>k  →  v<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Then, imitating the three <code>HasTrie</code> instances above,</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> Trie <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span> v = v
&nbsp;
<span style="color: #050; font-weight: bold;">type</span> Trie <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> a b<span style="color: green;">&#41;</span> v = <span style="color: green;">&#40;</span>Trie a v, Trie b v<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #050; font-weight: bold;">type</span> Trie <span style="color: green;">&#40;</span>a,b<span style="color: green;">&#41;</span> v = Trie a <span style="color: green;">&#40;</span>Trie b v<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Imagine that we have type lambdas for writing higher-kinded types.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> Trie <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span> = λ v → v
&nbsp;
<span style="color: #050; font-weight: bold;">type</span> Trie <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> a b<span style="color: green;">&#41;</span> = λ v → <span style="color: green;">&#40;</span>Trie a v, Trie b v<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #050; font-weight: bold;">type</span> Trie <span style="color: green;">&#40;</span>a,b<span style="color: green;">&#41;</span> = λ v → Trie a <span style="color: green;">&#40;</span>Trie b v<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Type lambdas are often written as &#8220;Λ&#8221; (capital &#8220;λ&#8221;) instead.
In the land of values, these three right-hand sides correspond to common building blocks for functions, namely identity, product, and composition:</p>

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

<p>These building blocks arise in the land of types.</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">newtype</span> Id a = Id a
&nbsp;
<span style="color: #050; font-weight: bold;">data</span> <span style="color: green;">&#40;</span>f :*: g<span style="color: green;">&#41;</span> a = f a :*: g a
&nbsp;
<span style="color: #050; font-weight: bold;">newtype</span> <span style="color: green;">&#40;</span>g :. f<span style="color: green;">&#41;</span> a = O <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></pre>
</div></p>

<p>where <code>Id</code>, <code>f</code> and <code>g</code> are functors.
Sum and a constant functor are also common building blocks:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">data</span> <span style="color: green;">&#40;</span>f :+: g<span style="color: green;">&#41;</span> a = InL <span style="color: green;">&#40;</span>f a<span style="color: green;">&#41;</span> | InR <span style="color: green;">&#40;</span>g a<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #050; font-weight: bold;">data</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Const"><span style="color: #833; font-weight: bold;">Const</span></a> x a = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Const"><span style="color: #833; font-weight: bold;">Const</span></a> x</pre>
</div></p>

<h3>Tries as associated type synonym</h3>

<p>Given these standard definitions, we can eliminate the special-purpose data types used, replacing them with our standard functor combinators:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> HasTrie <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <span style="color: #050; font-weight: bold;">type</span> Trie <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>  = Id
  trie   f      = Id <span style="color: green;">&#40;</span>f <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
  untrie <span style="color: green;">&#40;</span>Id v<span style="color: green;">&#41;</span> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:const"><span style="font-weight: bold;">const</span></a> v
&nbsp;
<span style="color: #050; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span>HasTrie a, HasTrie b<span style="color: green;">&#41;</span> =&gt; HasTrie <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> a b<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
  <span style="color: #050; font-weight: bold;">type</span> Trie <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> a b<span style="color: green;">&#41;</span> = Trie a :*: Trie b
  trie   f           = trie <span style="color: green;">&#40;</span>f . Left<span style="color: green;">&#41;</span> :*: trie <span style="color: green;">&#40;</span>f . Right<span style="color: green;">&#41;</span>
  untrie <span style="color: green;">&#40;</span>ta :*: tb<span style="color: green;">&#41;</span> = untrie ta `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:either"><span style="font-weight: bold;">either</span></a>` untrie tb
&nbsp;
<span style="color: #050; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span>HasTrie a, HasTrie b<span style="color: green;">&#41;</span> ⇒ HasTrie <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: #050; font-weight: bold;">type</span> Trie <span style="color: green;">&#40;</span>a , b<span style="color: green;">&#41;</span> = Trie a :. Trie b
  trie   f = O <span style="color: green;">&#40;</span>trie <span style="color: green;">&#40;</span>trie . <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:curry"><span style="font-weight: bold;">curry</span></a> f<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
  untrie <span style="color: green;">&#40;</span>O tt<span style="color: green;">&#41;</span> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:uncurry"><span style="font-weight: bold;">uncurry</span></a> <span style="color: green;">&#40;</span>untrie . untrie tt<span style="color: green;">&#41;</span></pre>
</div></p>

<p>At first blush, it might appear that we&#8217;ve simply moved the data type definitions outside of the instances.
However, the extracted functor combinators have other uses, as explored in polytypic programming.
I&#8217;ll point out some of these uses in the next few blog posts.</p>

<h3>Isomorphisms</h3>

<p>Many types are isomorphic variations, and so their corresponding tries can share a common representation.
For instance, triples are isomorphic to nested pairs:</p>

<p><div>
<pre class="haskell">detrip :: <span style="color: green;">&#40;</span>a,b,c<span style="color: green;">&#41;</span> → <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>a,b<span style="color: green;">&#41;</span>,c<span style="color: green;">&#41;</span>
detrip <span style="color: green;">&#40;</span>a,b,c<span style="color: green;">&#41;</span> = <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>a,b<span style="color: green;">&#41;</span>,c<span style="color: green;">&#41;</span>
&nbsp;
trip :: <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>a,b<span style="color: green;">&#41;</span>,c<span style="color: green;">&#41;</span> → <span style="color: green;">&#40;</span>a,b,c<span style="color: green;">&#41;</span>
trip <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>a,b<span style="color: green;">&#41;</span>,c<span style="color: green;">&#41;</span> = <span style="color: green;">&#40;</span>a,b,c<span style="color: green;">&#41;</span></pre>
</div></p>

<p>A trie for triples can be a a trie for pairs (already defined).
The <code>trie</code> and <code>untrie</code> methods then just perform conversions around the corresponding methods on pairs:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> <span style="color: green;">&#40;</span>HasTrie a, HasTrie b, HasTrie c<span style="color: green;">&#41;</span> ⇒ HasTrie <span style="color: green;">&#40;</span>a,b,c<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
    <span style="color: #050; font-weight: bold;">type</span> Trie <span style="color: green;">&#40;</span>a,b,c<span style="color: green;">&#41;</span> = Trie <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>a,b<span style="color: green;">&#41;</span>,c<span style="color: green;">&#41;</span>
    trie f = trie <span style="color: green;">&#40;</span>f . trip<span style="color: green;">&#41;</span>
    untrie t = untrie t . detrip</pre>
</div></p>

<p>All type isomorphisms can use this same pattern.
I don&#8217;t think Haskell is sufficiently expressive to capture this pattern within the language, so I&#8217;ll resort to a C macro.
There are five parameters:</p>

<ul>
<li><code>Context</code>: the instance context;</li>
<li><code>Type</code>: the type whose instance is being defined;</li>
<li><code>IsoType</code>: the isomorphic type;</li>
<li><code>toIso</code>: conversion function <em>to</em> <code>IsoType</code>; and</li>
<li><code>fromIso</code>: conversion function <em>from</em> <code>IsoType</code>.</li>
</ul>

<p>The macro:</p>

<p><div>
<pre class="haskell">#define HasTrieIsomorph<span style="color: green;">&#40;</span>Context,Type,IsoType,toIso,fromIso<span style="color: green;">&#41;</span> \ 
<span style="color: #050; font-weight: bold;">instance</span> Context ⇒ HasTrie <span style="color: green;">&#40;</span>Type<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span> <span style="color: green;">&#123;</span> \ 
  <span style="color: #050; font-weight: bold;">type</span> Trie <span style="color: green;">&#40;</span>Type<span style="color: green;">&#41;</span> = Trie <span style="color: green;">&#40;</span>IsoType<span style="color: green;">&#41;</span>; \ 
  trie f = trie <span style="color: green;">&#40;</span>f . <span style="color: green;">&#40;</span>fromIso<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>; \ 
  untrie t = untrie t . <span style="color: green;">&#40;</span>toIso<span style="color: green;">&#41;</span>; \ 
<span style="color: green;">&#125;</span></pre>
</div></p>

<p>Now we can easily define <code>HasTrie</code> instances:</p>

<p><div>
<pre class="haskell">HasTrieIsomorph<span style="color: green;">&#40;</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>, <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Bool"><span style="color: #833; font-weight: bold;">Bool</span></a>, <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><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>
               , \ c -&gt; <span style="color: #050; font-weight: bold;">if</span> c <span style="color: #050; font-weight: bold;">then</span> Left <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">else</span> Right <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:either"><span style="font-weight: bold;">either</span></a> <span style="color: green;">&#40;</span>\ <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span> -&gt; True<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>\ <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span> -&gt; False<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
&nbsp;
HasTrieIsomorph<span style="color: green;">&#40;</span> <span style="color: green;">&#40;</span>HasTrie a, HasTrie b, HasTrie c<span style="color: green;">&#41;</span>, <span style="color: green;">&#40;</span>a,b,c<span style="color: green;">&#41;</span>, <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>a,b<span style="color: green;">&#41;</span>,c<span style="color: green;">&#41;</span>
               , λ <span style="color: green;">&#40;</span>a,b,c<span style="color: green;">&#41;</span> → <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>a,b<span style="color: green;">&#41;</span>,c<span style="color: green;">&#41;</span>, λ <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>a,b<span style="color: green;">&#41;</span>,c<span style="color: green;">&#41;</span> → <span style="color: green;">&#40;</span>a,b,c<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
&nbsp;
HasTrieIsomorph<span style="color: green;">&#40;</span> <span style="color: green;">&#40;</span>HasTrie a, HasTrie b, HasTrie c, HasTrie d<span style="color: green;">&#41;</span>
               , <span style="color: green;">&#40;</span>a,b,c,d<span style="color: green;">&#41;</span>, <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>a,b,c<span style="color: green;">&#41;</span>,d<span style="color: green;">&#41;</span>
               , λ <span style="color: green;">&#40;</span>a,b,c,d<span style="color: green;">&#41;</span> → <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>a,b,c<span style="color: green;">&#41;</span>,d<span style="color: green;">&#41;</span>, λ <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>a,b,c<span style="color: green;">&#41;</span>,d<span style="color: green;">&#41;</span> → <span style="color: green;">&#40;</span>a,b,c,d<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre>
</div></p>

<p>In most (but not all) cases, the first argument (<code>Context</code>) could simply be that the isomorphic type <code>HasTrie</code>, e.g.,</p>

<p><div>
<pre class="haskell">HasTrieIsomorph<span style="color: green;">&#40;</span> HasTrie <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>a,b<span style="color: green;">&#41;</span>,c<span style="color: green;">&#41;</span>, <span style="color: green;">&#40;</span>a,b,c<span style="color: green;">&#41;</span>, <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>a,b<span style="color: green;">&#41;</span>,c<span style="color: green;">&#41;</span>
               , λ <span style="color: green;">&#40;</span>a,b,c<span style="color: green;">&#41;</span> → <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>a,b<span style="color: green;">&#41;</span>,c<span style="color: green;">&#41;</span>, λ <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span>a,b<span style="color: green;">&#41;</span>,c<span style="color: green;">&#41;</span> → <span style="color: green;">&#40;</span>a,b,c<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre>
</div></p>

<p>We could define another macro that captures this pattern and requires one fewer argument.
On the other hand, there is merit to keeping the contextual requirements explicit.</p>

<h3>Regular data types</h3>

<p>A regular data type is one in which the recursive uses are at the same type.
Functions over such types are often defined via <em>monomorphic</em> recursion.
Data types that do not satisfy this constraint are called &#8220;<a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.31.3551" title="Paper by Richard Bird and Lambert Meertens">nested</a>&#8220;.</p>

<p>As in several recent generic programming systems, regular data types can be encoded generically through a type class that unwraps one level of functor from a type.
The regular data type is the fixpoint of that functor.
See, e.g., <em><a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.14.6390" title="Paper by Ulf Norell and Patrik Jansson">Polytypic programming in Haskell</a></em>.
Adopting the style of <em><a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.142.4778" title="Paper by Thomas Noort, Alexey Rodriguez, Stefan Holdermans, Johan Jeuring, Bastiaan Heeren">A Lightweight Approach to Datatype-Generic Rewriting</a></em>,</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">class</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>PF t<span style="color: green;">&#41;</span> ⇒ Regular t <span style="color: #050; font-weight: bold;">where</span>
  <span style="color: #050; font-weight: bold;">type</span> PF t :: * → *
  wrap   :: PF t t → t
  unwrap :: t → PF t t</pre>
</div></p>

<p>Here &#8220;<code>PF</code>&#8221; stands for &#8220;pattern functor&#8221;.</p>

<p>The pattern functors can be constructed out of the functor combinators above.
For instance, a list at the top level is either empty or a value and a list.
Translating this description:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> Regular <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> <span style="color: #050; font-weight: bold;">where</span>
  <span style="color: #050; font-weight: bold;">type</span> PF <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> = Unit :+: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Const"><span style="color: #833; font-weight: bold;">Const</span></a> a :*: Id
&nbsp;
  unwrap <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>     = InL <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Const"><span style="color: #833; font-weight: bold;">Const</span></a> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
  unwrap <span style="color: green;">&#40;</span>a:<span style="color: #050; font-weight: bold;">as</span><span style="color: green;">&#41;</span> = InR <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Const"><span style="color: #833; font-weight: bold;">Const</span></a> a :*: Id <span style="color: #050; font-weight: bold;">as</span><span style="color: green;">&#41;</span>
&nbsp;
  wrap <span style="color: green;">&#40;</span>InL <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Const"><span style="color: #833; font-weight: bold;">Const</span></a> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>          = <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>
  wrap <span style="color: green;">&#40;</span>InR <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Const"><span style="color: #833; font-weight: bold;">Const</span></a> a :*: Id <span style="color: #050; font-weight: bold;">as</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> = a:<span style="color: #050; font-weight: bold;">as</span></pre>
</div></p>

<p>As another example, consider rose trees ([<code>Data.Tree</code>][]):</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">data</span> Tree  a = Node a <span style="color: green;">&#91;</span>Tree a<span style="color: green;">&#93;</span>
&nbsp;
<span style="color: #050; font-weight: bold;">instance</span> Regular <span style="color: green;">&#40;</span>Tree a<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span>
&nbsp;
  <span style="color: #050; font-weight: bold;">type</span> PF <span style="color: green;">&#40;</span>Tree a<span style="color: green;">&#41;</span> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Const"><span style="color: #833; font-weight: bold;">Const</span></a> a :*: <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>
&nbsp;
  unwrap <span style="color: green;">&#40;</span>Node a ts<span style="color: green;">&#41;</span> = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Const"><span style="color: #833; font-weight: bold;">Const</span></a> a :*: ts
&nbsp;
  wrap <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Applicative.html#t:Const"><span style="color: #833; font-weight: bold;">Const</span></a> a :*: ts<span style="color: green;">&#41;</span> = Node a ts</pre>
</div></p>

<p>Regular types allow for even more succinct <code>HasTrie</code> instance implementations.
Specialize <code>HasTrieIsomorph</code> further:</p>

<p><div>
<pre class="haskell">#define HasTrieRegular<span style="color: green;">&#40;</span>Context,Type<span style="color: green;">&#41;</span> \ 
HasTrieIsomorph<span style="color: green;">&#40;</span>Context, Type, PF <span style="color: green;">&#40;</span>Type<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>Type<span style="color: green;">&#41;</span> , unwrap, wrap<span style="color: green;">&#41;</span></pre>
</div></p>

<p>For instance, for lists and rose trees:</p>

<p><div>
<pre class="haskell">HasTrieRegular<span style="color: green;">&#40;</span>HasTrie a, <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span><span style="color: green;">&#41;</span>
HasTrieRegular<span style="color: green;">&#40;</span>HasTrie a, Tree a<span style="color: green;">&#41;</span></pre>
</div></p>

<p>The <code>HasTrieRegular</code> macro could be specialized even further for single-parameter polymorphic data types:</p>

<p><div>
<pre class="haskell">#define HasTrieRegular1<span style="color: green;">&#40;</span>TypeCon<span style="color: green;">&#41;</span> HasTrieRegular<span style="color: green;">&#40;</span>HasTrie a, TypeCon a<span style="color: green;">&#41;</span>
&nbsp;
HasTrieRegular1<span style="color: green;">&#40;</span><span style="color: green;">&#91;</span><span style="color: green;">&#93;</span><span style="color: green;">&#41;</span>
HasTrieRegular1<span style="color: green;">&#40;</span>Tree<span style="color: green;">&#41;</span></pre>
</div></p>

<p>You might wonder if I&#8217;m cheating here, by claiming very simple trie specifications when I&#8217;m really just shuffling code around.
After all, the complexity removed from <code>HasTrie</code> instances shows up in <code>Regular</code> instances.
The win in making this shuffle is that <code>Regular</code> is handy for other purposes, as illustrated in <em><a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.140.2412" title="Paper by Alexey Rodriguez, Stefan Holdermans, Andres Löh, and Johan Jeuring">Generic programming with fixed points for mutually recursive datatypes</a></em> (including <code>fold</code>, <code>unfold</code>, and <code>fmap</code>).
(More examples in <em><a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.142.4778" title="Paper by Thomas Noort, Alexey Rodriguez, Stefan Holdermans, Johan Jeuring, Bastiaan Heeren">A Lightweight Approach to Datatype-Generic Rewriting</a></em>.)</p>

<h3>Trouble</h3>

<p>Sadly, these elegant trie definitions have a problem.
Trying to compile them leads to a error message from GHC.
For instance,</p>

<p><div>
<pre class="haskell">Nested <span style="color: #050; font-weight: bold;">type</span> family application
  <span style="color: #050; font-weight: bold;">in</span> the <span style="color: #050; font-weight: bold;">type</span> family application: Trie <span style="color: green;">&#40;</span>PF <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span><span style="color: green;">&#41;</span>
<span style="color: green;">&#40;</span>Use -XUndecidableInstances to permit this<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Adding <code>UndecidableInstances</code> silences this error message, but leads to nontermination in the compiler.</p>

<p>Expanding definitions, I can see the likely cause of nontermination.
The definition in terms of a type family allows an infinite type to sneak through, and I guess GHC&#8217;s type checker is unfolding infinitely.</p>

<p>As a simpler example:</p>

<p><div>
<pre class="haskell"><span style="color: #5d478b; font-style: italic;">{-# LANGUAGE TypeFamilies, UndecidableInstances #-}</span>
&nbsp;
<span style="color: #050; font-weight: bold;">type</span> family List a :: *
&nbsp;
<span style="color: #050; font-weight: bold;">type</span> <span style="color: #050; font-weight: bold;">instance</span> List a = <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><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>a, List a<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- Hangs ghc 6.12.1:</span>
nil :: List a
nil = Left <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span></pre>
</div></p>

<h3>A solution</h3>

<p>Since GHC&#8217;s type-checker cannot handle directly recursive types, perhaps we can use a standard avoidance strategy, namely introducing a <code>newtype</code> or <code>data</code> definition to break the cycle.
For instance, as a trie for <code>[a]</code>, we got into trouble by using the trie of the unwrapped form of <code>[a]</code>, i.e., <code>Trie (PF [a] [a])</code>.
So instead,</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">newtype</span> ListTrie a v = ListTrie <span style="color: green;">&#40;</span>Trie <span style="color: green;">&#40;</span>PF <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span><span style="color: green;">&#41;</span> v<span style="color: green;">&#41;</span></pre>
</div></p>

<p>which is to say</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">newtype</span> ListTrie a v = ListTrie <span style="color: green;">&#40;</span>PF <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> :→: v<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Now <code>wrap</code> and <code>unwrap</code> as before, and add &amp; remove <code>ListTrie</code> as needed:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">instance</span> HasTrie a ⇒ HasTrie <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> <span style="color: #050; font-weight: bold;">where</span>
  <span style="color: #050; font-weight: bold;">type</span> Trie <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> = ListTrie a
  trie f = ListTrie <span style="color: green;">&#40;</span>trie <span style="color: green;">&#40;</span>f . wrap<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
  untrie <span style="color: green;">&#40;</span>ListTrie t<span style="color: green;">&#41;</span> = untrie t . unwrap</pre>
</div></p>

<p>Again, abstract the boilerplate code into a C macro:</p>

<p><div>
<pre class="haskell">#define HasTrieRegular<span style="color: green;">&#40;</span>Context,Type,TrieType,TrieCon<span style="color: green;">&#41;</span> \
<span style="color: #050; font-weight: bold;">newtype</span> TrieType v = TrieCon <span style="color: green;">&#40;</span>PF <span style="color: green;">&#40;</span>Type<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>Type<span style="color: green;">&#41;</span> :→: v<span style="color: green;">&#41;</span>; \
<span style="color: #050; font-weight: bold;">instance</span> Context ⇒ HasTrie <span style="color: green;">&#40;</span>Type<span style="color: green;">&#41;</span> <span style="color: #050; font-weight: bold;">where</span> <span style="color: green;">&#123;</span> \
  <span style="color: #050; font-weight: bold;">type</span> Trie <span style="color: green;">&#40;</span>Type<span style="color: green;">&#41;</span> = TrieType; \
  trie f = TrieCon <span style="color: green;">&#40;</span>trie <span style="color: green;">&#40;</span>f . wrap<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>; \
  untrie <span style="color: green;">&#40;</span>TrieCon t<span style="color: green;">&#41;</span> = untrie t . unwrap; \
<span style="color: green;">&#125;</span></pre>
</div></p>

<p>For instance,</p>

<p><div>
<pre class="haskell">HasTrieRegular<span style="color: green;">&#40;</span>HasTrie a, <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> , ListTrie a, ListTrie<span style="color: green;">&#41;</span>
HasTrieRegular<span style="color: green;">&#40;</span>HasTrie a, Tree, TreeTrie a, TreeTrie<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Again, simplify a bit with a specialization to unary regular types:</p>

<p><div>
<pre class="haskell">#define HasTrieRegular1<span style="color: green;">&#40;</span>TypeCon,TrieCon<span style="color: green;">&#41;</span> \
HasTrieRegular<span style="color: green;">&#40;</span>HasTrie a, TypeCon a, TrieCon a, TrieCon<span style="color: green;">&#41;</span></pre>
</div></p>

<p>And then use the following declarations instead:</p>

<p><div>
<pre class="haskell">HasTrieRegular1<span style="color: green;">&#40;</span><span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>  , ListTrie<span style="color: green;">&#41;</span>
HasTrieRegular1<span style="color: green;">&#40;</span>Tree, TreeTrie<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Similarly for binary etc as needed.</p>

<p>The second macro parameter (<code>TrieCon</code>) is just a name, which I don&#8217;t to be used other than in the macro-generated code.
It could be eliminated, if there were a way to gensym the name.
Perhaps with Template Haskell?</p>

<h3>Conclusion</h3>

<p>I like the elegance of constructing memo tries in terms of common functor combinators.
Standard pattern functors allow for extremely succinct trie specifications for regular data types.
However, these specifications lead to nontermination of the type checker, which can then be avoided by the standard trick of introducing a newtype to break type recursion.
As often, this trick brings introduces some clumsiness.
Perhaps the problem can also be avoided by using a formulation using <em>bifunctors</em>, as in <em><a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.60.5251" title="Paper by Jeremy Gibbons">Design Patterns as Higher-Order Datatype-Generic Programs</a></em> and <em><a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.14.6390" title="Paper by Ulf Norell and Patrik Jansson">Polytypic programming in Haskell</a></em>, which allows the fixed-point nature of regular data types to be exposed.</p>
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/elegant-memoization-with-higher-order-types/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Non-strict memoization</title>
		<link>http://conal.net/blog/posts/nonstrict-memoization/</link>
		<comments>http://conal.net/blog/posts/nonstrict-memoization/#comments</comments>
		<pubDate>Wed, 14 Jul 2010 02:46:23 +0000</pubDate>
		<dc:creator>conal</dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[memoization]]></category>
		<category><![CDATA[trie]]></category>
		<category><![CDATA[unamb]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=110</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Non-strict memoization

Tags: unamb, memoization, trie

URL: http://conal.net/blog/posts/nonstrict-memoization/

-->

<!-- references -->

<!-- teaser -->

<p>I&#8217;ve written a few <a href="http://conal.net/blog/tag/memoization/" title="Posts on memoization">posts about functional memoization</a>.
In <a href="http://conal.net/blog/posts/elegant-memoization-with-functional-memo-tries/" title="blog post">one of them</a>, <a href="http://lukepalmer.wordpress.com/" title="Luke Palmer's blog">Luke Palmer</a> commented that the memoization methods are correct only for strict functions, which I had not noticed before.
In this note, I correct this flaw, extending correct memoization to non-strict functions as well.
The semantic notion of <a href="http://conal.net/blog/tag/unamb/" title="Posts on unambiguous choice and least upper bound"><em>least upper bound</em> (which can be built of <em>unambiguous choice</em>)</a> plays a crucial role.</p>

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

<ul>
<li>2010-07-13: Fixed the non-strict memoization example to use an argument of <code>undefined</code> (⊥) as intended.</li>
<li>2010-07-23: Changed spelling from &#8220;nonstrict&#8221; to the much more popular &#8220;non-strict&#8221;.</li>
</ul>

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

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

<h3>What is memoization?</h3>

<p>In purely functional programming, applying a function to equal arguments gives equal results.
However, the second application is as costly as the first one.
The idea of memoization, invented by Donald Michie in the 1960s, is to cache the results of applications and reuse those results in subsequent applications.
Memoization is a handy technique to know, as it can dramatically reduces expense while making little impact on an algorithm&#8217;s simplicity.</p>

<p>Early implementations of memoization were imperative.
Some sort of table (e.g., a hash table) is initialized as empty.
Whenever the memoized function is applied, the argument is looked up in the table.
If present, the corresponding result is returned.
Otherwise, the original function is applied to the argument, and the result is stored in the table, keyed by the argument.</p>

<h3>Functional memoization</h3>

<p>Can memoization be implemented functionally (without assignment)?
One might argue that it cannot, considering that we want the table structure to get filled in destructively, as the memoized function is sampled.</p>

<p>However, this argument is flawed (like many informal arguments of impossibility).
Although we want a mutation to happen, we needn&#8217;t ask for one explicitly.
Instead, we can exploit the mutation that happens <em>inside the implementation</em> of laziness.</p>

<p>For instance, consider memoizing a function of booleans:</p>

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

<p>In this case, the &#8220;table&#8221; can simply be a pair, with one slot for the argument <code>False</code> and one for <code>True:</code></p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> BoolTable a = <span style="color: green;">&#40;</span>a,a<span style="color: green;">&#41;</span>
&nbsp;
memoBool f = lookupBool <span style="color: green;">&#40;</span>f False, f True<span style="color: green;">&#41;</span>
&nbsp;
lookupBool :: BoolTable b -&gt; <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> -&gt; b
lookupBool <span style="color: green;">&#40;</span>f,_<span style="color: green;">&#41;</span> False = f
lookupBool <span style="color: green;">&#40;</span>_,t<span style="color: green;">&#41;</span> True  = t</pre>
</div></p>

<p>For instance, consider this simple function and a memoized version:</p>

<p><div>
<pre class="haskell">f1 b = <span style="color: #050; font-weight: bold;">if</span> b <span style="color: #050; font-weight: bold;">then</span> <span style="color: red;">3</span> <span style="color: #050; font-weight: bold;">else</span> <span style="color: red;">4</span>
&nbsp;
s1 = memoBool f1</pre>
</div></p>

<p>The memo table will be <code>(f False, f True)</code>, i.e., <code>(4,3)</code>.
Checking that <code>s1</code> is equivalent to <code>f1</code>:</p>

<p><div>
<pre class="haskell">s1 False ≡ lookupBool <span style="color: green;">&#40;</span><span style="color: red;">4</span>,<span style="color: red;">3</span><span style="color: green;">&#41;</span> False ≡ <span style="color: red;">4</span> ≡ f1 False
s1 True  ≡ lookupBool <span style="color: green;">&#40;</span><span style="color: red;">4</span>,<span style="color: red;">3</span><span style="color: green;">&#41;</span> True  ≡ <span style="color: red;">3</span> ≡ f1 True</pre>
</div></p>

<p>Other argument types have other table representations, and these table types can be defined <a href="http://conal.net/blog/posts/elegant-memoization-with-functional-memo-tries/" title="blog post">systematically and elegantly</a>.</p>

<p>Now, wait a minute!
Building an entire table up-front doesn&#8217;t sound like the incremental algorithm Richie invented, especially considering that the domain type can be quite large and even infinite.
However, in a <em>lazy</em> language, incremental construction of data structures is automatic and pervasive, and infinite data structures are bread &amp; butter.
So the computing and updating doesn&#8217;t have to be <em>expressed</em> imperatively.</p>

<p>While lazy construction can be helpful for pairs, it&#8217;s <em>essential</em> for infinite tables, as needed for domain types that are enornmously large (e.g., <code>Int</code>), and even infinitely large (e.g., <code>Integer</code>, or <code>[Bool]</code>).
However, laziness brings to memoization not only a gift, but also a difficulty, namely the challenge of correctly memoizing <em>non-strict</em> functions, as we&#8217;ll see next.</p>

<h3>A problem with memoizing non-strict functions</h3>

<p>The confirmation above that <code>s1 ≡ f1</code> has a mistake: it fails to consider a third possible choice of argument, namely ⊥.
Let&#8217;s check this case now:</p>

<p><div>
<pre class="haskell">s1 ⊥ ≡ lookupBool <span style="color: green;">&#40;</span><span style="color: red;">4</span>,<span style="color: red;">3</span><span style="color: green;">&#41;</span> ⊥ ≡ ⊥ ≡ f1 ⊥</pre>
</div></p>

<p>The ⊥ case does not show up explicitly in the definition of <code>lookupBool</code>, but is implied by the use of pattern-matching against <code>True</code> and <code>False</code>.
For the same reason (in the definition of <code>if-then-else</code>), <code>f1 ⊥ ≡ ⊥</code>, so indeed <code>s1 ≡ f1</code>.
The key saving grace here is that <code>f1</code> is already strict, so the strictness introduced by <code>lookupBool</code> is harmless.</p>

<p>To see how memoization add strictness, consider a memoizing a <em>non-strict</em> function of booleans:</p>

<p><div>
<pre class="haskell">f2 b = <span style="color: red;">5</span>
&nbsp;
s2 = memoBool f2</pre>
</div></p>

<p>The memo table will be <code>(f False, f True)</code>, i.e., <code>(5,5)</code>.
Checking that <code>s2</code> is equivalent to <code>f2</code>:</p>

<p><div>
<pre class="haskell">s2 False ≡ lookupBool <span style="color: green;">&#40;</span><span style="color: red;">5</span>,<span style="color: red;">5</span><span style="color: green;">&#41;</span> False ≡ <span style="color: red;">5</span> ≡ f2 False
s2 True  ≡ lookupBool <span style="color: green;">&#40;</span><span style="color: red;">5</span>,<span style="color: red;">5</span><span style="color: green;">&#41;</span> True  ≡ <span style="color: red;">5</span> ≡ f2 True</pre>
</div></p>

<p>However,</p>

<p><div>
<pre class="haskell">s2 ⊥ ≡ lookupBool <span style="color: green;">&#40;</span><span style="color: red;">5</span>,<span style="color: red;">5</span><span style="color: green;">&#41;</span> ⊥ ≡ ⊥</pre>
</div></p>

<p>The latter equality is due again to pattern matching against <code>False</code> and <code>True</code> in <code>lookupBool</code>.</p>

<p>In contrast, <code>f2 ⊥ ≡ 5</code>, so <code>s2 ≢ f2</code>, so <code>memoBool</code> does not correctly memoize.</p>

<h3>Non-strict memoization</h3>

<p>The bug in <code>memoBool</code> comes from ignoring one of the possible boolean values.
In a lazy language, <code>Bool</code> has three possible values, not two.
A simple solution then might be for the memo table to be a triple instead of a pair:</p>

<p><div>
<pre class="haskell"><span style="color: #050; font-weight: bold;">type</span> BoolTable a = <span style="color: green;">&#40;</span>a,a,a<span style="color: green;">&#41;</span>
&nbsp;
memoBool h = lookupBool <span style="color: green;">&#40;</span>h ⊥, h False, h True<span style="color: green;">&#41;</span></pre>
</div></p>

<p>Table lookup needs one additional case:</p>

<p><div>
<pre class="haskell">lookupBool :: BoolTable a -&gt; <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> -&gt; a
lookupBool <span style="color: green;">&#40;</span>b,_,_<span style="color: green;">&#41;</span> ⊥     = b
lookupBool <span style="color: green;">&#40;</span>_,f,_<span style="color: green;">&#41;</span> False = f
lookupBool <span style="color: green;">&#40;</span>_,_,t<span style="color: green;">&#41;</span> True  = t</pre>
</div></p>

<p>I hope you read my posts with a good deal of open-mindedness, but also with some skepticism.
This revised definition of <code>lookupBool</code> is not legitimate Haskell code, and for a good reason.
If we could write and run this kind of code, we could solve the halting problem:</p>

<p><div>
<pre class="haskell">halts :: a -&gt; <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>
halts ⊥ = False
halts _ = True</pre>
</div></p>

<p>The problem here is not just that ⊥ is not a legitimate Haskell <em>pattern</em>, but more fundamentally that equality with ⊥ is non-computable.</p>

<p>The revised <code>lookupBool</code> function and the <code>halts</code> function violate a fundamental semantic property, namely <em>monotonicity</em>  (of information content).
Monotonicity of a function <code>h</code> means that</p>

<p><div>
<pre class="haskell">∀ a b. a ⊑ b ⟹ h a ⊑ h b</pre>
</div></p>

<p>where &#8220;⊑&#8221; means has less (or equal) information content, as explained in <em><a href="http://conal.net/blog/posts/merging-partial-values/" title="blog post">Merging partial values</a></em>.
In other words, if you tell <code>f</code> more about an argument, it will tell you more about the result, where &#8220;more&#8221; (really more-or-equal) includes compatibility (no contradiction of previous knowledge).</p>

<p>The <code>halts</code> function is nonmonotonic, since, for instance, <code>⊥ ⊑ 3</code>, and <code>h ⊥ ≡ False</code> and <code>h 3 ≡ True</code>, but <code>False ⋢ True</code>.
(<code>False</code> and <code>True</code> are incompatible, i.e., they contradict each other.)</p>

<p>Similarly, the function <code>lookupBool (5,3,4)</code> is nonmonotonic, which you can verify by applying it to ⊥ and to <code>False</code>.
Although <code>⊥ ⊑ False</code>, <code>h ⊥ ≡ 5</code> and <code>h False ≡ 3</code>, but <code>5 ⋢ 3</code>.
Similarly, <code>⊥ ⊑ True</code>, <code>h ⊥ ≡ 5</code> and <code>h True ≡ 5</code>, but <code>5 ⋢ 4</code>.</p>

<p>So this particular memo table gets us into trouble (nonmonotonicity).
Are there other memo tables <code>(b,f,t)</code> that lead to monotonic lookup?
Re-examining the breakdown shows us a necessary and sufficient condition, which is that <code>b ⊑ f</code> and <code>b ⊑ t</code>.</p>

<p>Look again at the particular use of <code>lookupBool</code> in the definition of <code>memoBool</code> above, and you&#8217;ll see that</p>

<p><div>
<pre class="haskell">b ≡ h ⊥
f ≡ h False
t ≡ h True</pre>
</div></p>

<p>so the monotonicity condition becomes <code>h ⊥ ⊑ h False</code> and <code>h ⊥ ⊑ h True</code>.
This condition holds, thanks to the monotonicity of all computable functions <code>h</code>.</p>

<p>So the triple-based <code>lookupBool</code> can be semantically problematic outside of its motivating context, but never as used in <code>memoBool</code>.
That is, the triple-based definition of <code>memoBool</code> correctly specifies the (computable) meaning we want, but isn&#8217;t an implementation.
How might we correctly implement <code>memoBool</code>?</p>

<p>In <em><a href="http://conal.net/blog/posts/lazier-function-definitions-by-merging-partial-values/" title="blog post">Lazier function definitions by merging partial values</a></em>, I examined the standard Haskell style (inherited from predecessors) of definition by clauses, pointing out how that style is teasingly close to a declarative reading in which each clause is a true equation (possibly conditional).
I transform the standard style into a form with modular, declarative semantics.</p>

<p>Let&#8217;s try transforming <code>lookupBool</code> into this modular form:</p>

<p><div>
<pre class="haskell">lookupBool :: BoolTable a -&gt; <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> -&gt; a
lookupBool <span style="color: green;">&#40;</span>b,f,t<span style="color: green;">&#41;</span> = <span style="color: green;">&#40;</span>λ ⊥ → b<span style="color: green;">&#41;</span> ⊔ <span style="color: green;">&#40;</span>λ False → f<span style="color: green;">&#41;</span> ⊔ <span style="color: green;">&#40;</span>λ True → t<span style="color: green;">&#41;</span></pre>
</div></p>

<p>We still have the problem with <code>λ ⊥ → b</code> (nonmonotonicity), but it&#8217;s now isolated.
What if we broaden the domain from just ⊥ (for which we cannot dependably test) to <em>all</em> arguments, i.e., <code>λ _ → b</code> (i.e., <code>const b</code>)?
This latter function is the least one (in the information ordering) that is monotonic and contains all the information present in <code>λ ⊥ → b</code>.
(Exercise: prove.)
Dissecting this function:</p>

<p><div>
<pre class="haskell"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:const"><span style="font-weight: bold;">const</span></a> b ≡ <span style="color: green;">&#40;</span>λ _ → b<span style="color: green;">&#41;</span> ≡ <span style="color: green;">&#40;</span>λ ⊥ → b<span style="color: green;">&#41;</span> ⊔ <span style="color: green;">&#40;</span>λ False → b<span style="color: green;">&#41;</span> ⊔ <span style="color: green;">&#40;</span>λ True → b<span style="color: green;">&#41;</span></pre>
</div></p>

<p>So</p>

<p><div>
<pre class="haskell">  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:const"><span style="font-weight: bold;">const</span></a> b ⊔ <span style="color: green;">&#40;</span>λ False → f<span style="color: green;">&#41;</span> ⊔ <span style="color: green;">&#40;</span>λ True → t<span style="color: green;">&#41;</span>
≡ <span style="color: green;">&#40;</span>λ ⊥ → b<span style="color: green;">&#41;</span> ⊔ <span style="color: green;">&#40;</span>λ False → b<span style="color: green;">&#41;</span> ⊔ <span style="color: green;">&#40;</span>λ True → b<span style="color: green;">&#41;</span> ⊔ <span style="color: green;">&#40;</span>λ False → f<span style="color: green;">&#41;</span> ⊔ <span style="color: green;">&#40;</span>λ True → t<span style="color: green;">&#41;</span>
≡ <span style="color: green;">&#40;</span>λ ⊥ → b<span style="color: green;">&#41;</span> ⊔ <span style="color: green;">&#40;</span>λ False → b<span style="color: green;">&#41;</span> ⊔ <span style="color: green;">&#40;</span>λ False → f<span style="color: green;">&#41;</span> ⊔ <span style="color: green;">&#40;</span>λ True → b<span style="color: green;">&#41;</span> ⊔ <span style="color: green;">&#40;</span>λ True → t<span style="color: green;">&#41;</span>
≡ <span style="color: green;">&#40;</span>λ ⊥ → b<span style="color: green;">&#41;</span> ⊔ <span style="color: green;">&#40;</span>λ False → <span style="color: green;">&#40;</span>b ⊔ f<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> ⊔ <span style="color: green;">&#40;</span>λ True → <span style="color: green;">&#40;</span>b ⊔ t<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
≡ <span style="color: green;">&#40;</span>λ ⊥ → b<span style="color: green;">&#41;</span> ⊔ <span style="color: green;">&#40;</span>λ False →      f <span style="color: green;">&#41;</span> ⊔ <span style="color: green;">&#40;</span>λ True →      t <span style="color: green;">&#41;</span></pre>
</div></p>

<p>under the condition that <code>b ⊑ f</code> and <code>b ⊑ t</code>, which does hold in the context of our use (again by monotonicity of the <code>h</code> in <code>memoBool</code>).
Therefore, in this context, we can replace the nonmonotonic <code>λ ⊥ → b</code> with the monotonic <code>const b</code>, while preserving the meaning of <code>memoBool</code>.</p>

<p>Behind the dancing symbols in the proof above lies the insight that we can use the ⊥ case even for non-⊥ arguments, because the result will be subsumed by non-⊥ cases, thanks to the lubs (⊔).</p>

<p>The original two non-⊥ cases can be combined back into their more standard (less modular) Haskell form, and we can revert to our original strict table and lookup function.
Our use of ⊔ requires the result type to be ⊔-able.</p>

<p><div>
<pre class="haskell">memoBool :: HasLub b =&gt; <span style="color: green;">&#40;</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> -&gt; b<span style="color: green;">&#41;</span> -&gt; <span style="color: green;">&#40;</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> -&gt; b<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #050; font-weight: bold;">type</span> BoolTable a = <span style="color: green;">&#40;</span>a,a<span style="color: green;">&#41;</span>
&nbsp;
memoBool h = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:const"><span style="font-weight: bold;">const</span></a> <span style="color: green;">&#40;</span>h ⊥<span style="color: green;">&#41;</span> ⊔ lookupBool <span style="color: green;">&#40;</span>h False, h True<span style="color: green;">&#41;</span>
&nbsp;
lookupBool :: BoolTable b -&gt; <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> -&gt; b
lookupBool <span style="color: green;">&#40;</span>f,_<span style="color: green;">&#41;</span> False = f
lookupBool <span style="color: green;">&#40;</span>_,t<span style="color: green;">&#41;</span> True  = t</pre>
</div></p>

<p>So the differences between our original, too-strict <code>memoBool</code> and this correct one are quite small: the <code>HasLub</code> constraint and the &#8220;<code>const (f ⊥) ⊔</code>&#8220;.</p>

<p>The <code>HasLub</code> constraint on result warns us of a possible loss of generality.
Are there types for which we do not know how to ⊔?
Primitive types are flat, where ⊔ is equivalent to <code>unamb</code>; and there are <code>HasLub</code> instances for functions, sums, and products.
(See <em><a href="http://conal.net/blog/posts/merging-partial-values/" title="blog post">Merging partial values</a></em>.)
<code>HasLub</code> could be derived automatically for algebraic data types (labeled sums of products) and trivially for <code>newtype</code>.
Perhaps abstract types need some extra thought.</p>

<h3>Demo</h3>

<p>First, import the <a href="http://haskell.org/haskellwiki/lub" title="wiki page">lub</a> package:</p>

<p><div>
<pre class="haskell"><span style="color: #5d478b; font-style: italic;">{-# LANGUAGE Rank2Types #-}</span>
<span style="color: #5d478b; font-style: italic;">{-# OPTIONS -Wall #-}</span>
<span style="color: #050; font-weight: bold;">import</span> Data.Lub</pre>
</div></p>

<p>And define a type of <em>strict</em> memoization.
Borrowing from <a href="http://lukepalmer.wordpress.com/" title="Luke Palmer's blog">Luke Palmer</a>&#8217;s <a href="http://lukepalmer.wordpress.com/2008/10/14/data-memocombinators/" title="blog post by Luke Palmer">MemoCombinators</a> package, define a type of strict memoizers:</p>

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

<p>Now a strict memoizer for <code>Bool</code>, as above:</p>

<p><div>
<pre class="haskell">memoBoolStrict :: MemoStrict <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>
memoBoolStrict h = lookupBool <span style="color: green;">&#40;</span>h False, h True<span style="color: green;">&#41;</span>
 <span style="color: #050; font-weight: bold;">where</span>
   lookupBool <span style="color: green;">&#40;</span>f,_<span style="color: green;">&#41;</span> False = f
   lookupBool <span style="color: green;">&#40;</span>_,t<span style="color: green;">&#41;</span> True  = t</pre>
</div></p>

<p>Test out the strict memoizer.
First on a strict function:</p>

<p><div>
<pre class="haskell">h1, s1 :: <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> -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Integer"><span style="color: #833; font-weight: bold;">Integer</span></a>
h1 = \ b -&gt; <span style="color: #050; font-weight: bold;">if</span> b <span style="color: #050; font-weight: bold;">then</span> <span style="color: red;">3</span> <span style="color: #050; font-weight: bold;">else</span> <span style="color: red;">4</span>
s1 = memoBoolStrict h1</pre>
</div></p>

<p>A test run:</p>

<p><div>
<pre class="haskell">*Main&gt; h1 True
<span style="color: red;">3</span>
*Main&gt; s1 True
<span style="color: red;">3</span></pre>
</div></p>

<p>Next on a non-strict function:</p>

<p><div>
<pre class="haskell">h2, s2 :: <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> -&gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Integer"><span style="color: #833; font-weight: bold;">Integer</span></a>
h2 = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:const"><span style="font-weight: bold;">const</span></a> <span style="color: red;">5</span>
s2 = memoBoolStrict h2</pre>
</div></p>

<p>And test:</p>

<p><div>
<pre class="haskell">*Main&gt; h2 <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: red;">5</span>
*Main&gt; s2 <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:undefined"><span style="font-weight: bold;">undefined</span></a>
*** Exception: <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html"><span style="colo: #050; font-weight: bold;">Prelude</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></pre>
</div></p>

<p>Now define a type of non-strict memoizers:</p>

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

<p>And a non-strict <code>Bool</code> memoizer:</p>

<p><div>
<pre class="haskell">memoBool :: Memo <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>
memoBool h = <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:const"><span style="font-weight: bold;">const</span></a> <span style="color: green;">&#40;</span>h <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> `lub` memoBoolStrict h</pre>
</div></p>

<p>Testing:</p>

<p><div>
<pre class="haskell">*Main&gt; h2 <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: red;">5</span>
*Main&gt; n2 <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: red;">5</span></pre>
</div></p>

<p>Success!</p>

<h3>Beyond <code>Bool</code></h3>

<p>To determine how to generalize <code>memoBool</code> to types other than <code>Bool</code>, consider what properties of <code>Bool</code> mattered in our development.</p>

<ul>
<li>We know how to strictly memoize over <code>Bool</code> (i.e., what shape to use for the memo table and how to fill it).</li>
<li><code>Bool</code> is flat.</li>
</ul>

<p>The first condition also holds (<a href="http://conal.net/blog/posts/elegant-memoization-with-functional-memo-tries/" title="blog post">elegantly</a>) for integral types, sums, products, and algebraic types.</p>

<p>The second condition is terribly restrictive and fails to hold for sums, products and most algebraic types (e.g., <code>Maybe</code> and <code>[]</code>).</p>

<p>Consider a Haskell function <code>h :: (a,b) -&gt; c</code>.
An element of type <code>(a,b)</code> is either <code>⊥</code> or <code>(x,y)</code>, where <code>x :: a</code> and <code>y :: b</code>.
We can cover the ⊥ case as we did with <code>Bool</code>, by ⊔-ing in <code>const (h ⊥)</code>.
For the <code>(x,y)</code> case, we can proceed just as in strict memoization, by uncurrying, memoizing the outer and inner functions (of <code>a</code> and of <code>b</code> respectively), and recurrying.
For details, see <em><a href="http://conal.net/blog/posts/elegant-memoization-with-functional-memo-tries/" title="blog post">Elegant memoization with functional memo tries</a></em>.</p>

<p>Similarly for sum types.
(A value of type <code>Either a b</code> is either ⊥, or <code>Left x</code> or <code>Right y</code>, where <code>x :: a</code> and <code>y :: b</code>.)
And by following the treatment of products and sums, we can correctly memoize functions over any algebraic type.</p>

<h3>Related work</h3>

<h4>Lazy Memo-functions</h4>

<p>In 1985, John Hughes published a paper <em><a href="http://www.cs.chalmers.se/~rjmh/Papers/hughes_85_lazy.pdf" title="Paper by John Hughes">Lazy Memo-functions</a></em>, in which he points out the laziness-harming property of standard memoization.</p>

<blockquote>
  <p>[...] In a language with lazy evaluation this problem is aggravated: since verifying that two data-structures are equal requires that each be completely evaluated, all memoised functions are completely strict. This means they cannot be applied to circular or infinite arguments, or to arguments which (for one reason or another) cannot yet be completely evaluated. Therefore memo-functions cannot be combined with the most powerful features of lazy languages.</p>
</blockquote>

<p>John gives a laziness-friendlier alternative, which is to use the <em>addresses</em> rather than contents in the case of structured arguments.
Since it does force evaluation on atomic arguments, I don&#8217;t think it preserves non-strictness.
Moreover, it leads to redundant computation when structured arguments are equal but not pointer-equal.</p>

<h3>Conclusion</h3>

<p>Formulations of function memoization can be quite elegant and practical in a non-strict/lazy functional language.
In such a setting, however, I cannot help but want to correctly handle <em>all</em> functions, including non-strict ones.
This post gives a technique for doing so, making crucial use of the least upper bound (⊔) operator described in <a href="http://conal.net/blog/tag/unamb/" title="Posts on unambiguous choice and least upper bound">various other posts</a>.</p>

<p>Despite the many words above, the modification to strict memoization is simple: for a function <code>h</code>, given an argument <code>x</code>, in addition to indexing a memo trie with <code>x</code>, also evaluate <code>h ⊥</code>, and merge the information obtained from these two attempts (conceptually run in parallel).
Indexing a memo trie forces evaluation of <code>x</code>, which is a problem when <code>h</code> is non-strict and <code>x</code> evaluates to ⊥.
In exactly that case, however, <code>h ⊥</code> is not ⊥, and so provides exactly the information we need.
Moreover, information-monotonicity of <code>h</code> (a property of all computable functions) guarantees that <code>h ⊥ ⊑ h x</code>, so the information being merged is compatible.</p>

<p>Note that this condition is even stronger than compatibility, so perhaps we could use a more restricted and more efficient alternative to the fully general least upper bound.
The technique in <em><a href="http://conal.net/blog/posts/exact-numeric-integration/" title="blog post">Exact numeric integration</a></em> also used this restricted form.</p>

<p>How does this method for correct, non-strict memoization work in practice?
I guess the answer mainly depends on the efficiency and robustness of ⊔ (or of the restricted form mentioned just above).
The current implementation could probably be improved considerably if brought into the runtime system (RTS) and implemented by an RTS expert (which I&#8217;m not).</p>

<p>Information ordering and ⊔ play a central role in the denotational semantics of programming languages.
Since first stumbling onto a use for <code>⊔</code> (initially in its flat form, <code>unamb</code>), I&#8217;ve become very curious about how this operator might impact programming <em>practice</em> as well as theory.
My impression so far is that it is a powerful modularization tool, just as laziness is (as illustrated by John Hughes in <em><a href="http://www.cs.chalmers.se/~rjmh/Papers/whyfp.html" title="Paper by John Hughes">Why Functional Programming Matters</a></em>).
I&#8217;m looking for more examples, to further explore this impression.</p>
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/nonstrict-memoization/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Blog upgraded.  RSS fixed.</title>
		<link>http://conal.net/blog/posts/blog-upgraded-rss-fixed/</link>
		<comments>http://conal.net/blog/posts/blog-upgraded-rss-fixed/#comments</comments>
		<pubDate>Fri, 22 Jan 2010 01:56:22 +0000</pubDate>
		<dc:creator>conal</dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[blog]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=99</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Blog upgraded.  RSS fixed.

Tags: blog

URL: http://conal.net/blog/posts/blog-upgraded-rss-fixed/

-->

<!--
**Edits**:

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

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

<p>I&#8217;ve heard from a few people that my blog&#8217;s RSS feeds were broken.
I just upgraded my blog to Wordpress 2.9.1, which appears to have fixed the problem with RSS generation.</p>

<p>If you notice any problems with RSS or anything else, please let me know.</p>
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/blog-upgraded-rss-fixed/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Is Haskell a purely functional language?</title>
		<link>http://conal.net/blog/posts/is-haskell-a-purely-functional-language/</link>
		<comments>http://conal.net/blog/posts/is-haskell-a-purely-functional-language/#comments</comments>
		<pubDate>Tue, 05 Jan 2010 23:08:31 +0000</pubDate>
		<dc:creator>conal</dc:creator>
				<category><![CDATA[Functional programming]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=98</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Is Haskell a purely functional language?

Tags: 

URL: http://conal.net/blog/posts/is-haskell-a-purely-functional-language/


Alternative titles:

Is monadic IO functional?

What is a purely functional language?

-->

<!-- references -->

<p>There is a lot of confusion about the meaning of &#8220;functional&#8221; and &#8220;declarative&#8221; as descriptions of programming languages and paradigms.
For instance, Haskell is sometimes advertised as a <a href="http://haskell.org">&#8220;purely functional programming language&#8221;</a> and other times as <a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.13.9123">&#8220;the world&#8217;s finest imperative programming language&#8221;</a>.
I added some playful confusion (and clarity, I hope) with my post <em><a href="http://conal.net/blog/posts/the-c-language-is-purely-functional/" title="blog post">The C language is purely functional</a></em>.</p>

<p>I still regularly hear people ask and disagree about whether Haskell&#8217;s monadic I/O is &#8220;functional&#8221;.
I would say yes in one (technical) sense of &#8220;functional&#8221;.
But not the same sense in which we say &#8220;functional programming has simple, precise semantics&#8221; or &#8220;functional programming has good compositional properties&#8221; or &#8220;functional programming is good for reasoning about&#8221;.
Monadic I/O is a clever trick for encapsulating sequential, imperative computation, so that it can &#8220;do no evil&#8221; to the part that really does have precise semantics and good compositional properties.</p>

<p>It&#8217;s because of this confusion that I&#8217;ve started using the more specific term &#8220;denotational programming&#8221; in my blog subtitle and elsewhere, as an alternative to what I used to call &#8220;functional programming&#8221;.
While there are other notions of &#8220;functional&#8221;, applicable even to monadic IO, I think &#8220;denotational&#8221; captures the fundamental and far-reaching benefits that we called &#8220;good for reasoning about&#8221; and &#8220;powerfully compositional&#8221;.</p>

<p>When I bash monadic I/O, my real complaint isn&#8217;t with the technical invention&#8211;which I like.
Rather, I&#8217;m cranky about confusion and misleading communication, and about distraction from what we originally meant by &#8220;functional programming&#8221;&#8211;from what I call &#8220;denotational programming&#8221;.</p>

<p>I don&#8217;t mind that we haven&#8217;t yet been <a href="http://conal.net/blog/posts/can-functional-programming-be-liberated-from-the-von-neumann-paradigm/" title="blog post">liberated from the von Neumann model</a>.
As long as <em>remember</em> we haven&#8217;t.</p>

<p>As long as we keep on searching.</p>

<!--
**Edits**:

* 2009-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/is-haskell-a-purely-functional-language/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Can functional programming be liberated from the von Neumann paradigm?</title>
		<link>http://conal.net/blog/posts/can-functional-programming-be-liberated-from-the-von-neumann-paradigm/</link>
		<comments>http://conal.net/blog/posts/can-functional-programming-be-liberated-from-the-von-neumann-paradigm/#comments</comments>
		<pubDate>Tue, 05 Jan 2010 18:32:59 +0000</pubDate>
		<dc:creator>conal</dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[vision]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=97</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Can functional programming be liberated from the von Neumann paradigm?

Tags: vision

URL: http://conal.net/blog/posts/can-functional-programming-be-liberated-from-the-von-neumann-paradigm/

-->

<!-- references -->

<!-- teaser -->

<p>This post describes an idea I&#8217;ve had in mind for years and have chatted about with friends but haven&#8217;t written down before.</p>

<p>Functional programmers used to worry about how to solve &#8220;the I/O problem&#8221;.
Functional programming (FP) eschews any notion of side-effect or order of <em>execution</em>, and instead has an order-independent notion of <em>evaluation</em>.
In contrast, input &amp; output (I/O) seems to be an inherently effectful, ordered notion.</p>

<p>For a long time, I&#8217;ve been dissatisfied with the current Haskell solution to &#8220;the I/O problem&#8221;.
Of course it&#8217;s very clever and effective.
With &#8220;monadic IO&#8221; we can program both functionally and imperatively, and the imperative semantics doesn&#8217;t compromise the functional semantics.</p>

<p>I look at monadic IO not as any solution to functional I/O, but rather as a (clever) way <em>not</em> to bring I/O into the functional world.
There&#8217;s a ton of confusion buzzing around monadic IO, so let me try to restate more carefully:
&#8220;monadic IO&#8221; is a way to <em>generate</em> imperative computations (not just I/O) without having to say anything at all about the semantic content of those computations.
Rather than retooling our mental habits completely away from the imperative model of computation, monadic IO is a way to hang onto these old mental habits, and use them in a hybrid functional/imperative programming style, in which the imperative doesn&#8217;t contaminate the functional, semantically.</p>

<p>John Backus offered us a gift and a blessing in his 1977 Turing Award lecture, <em><a href="http://www.thocp.net/biographies/papers/backus_turingaward_lecture.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>.  He told us how the sequential/imperative model enslaves us in weak thoughts, inhibiting manageable scaling (sections 1 through 5.1).
In the abstract, Backus says</p>

<blockquote>
  <p>Conventional programming languages are growing ever more enormous, but not stronger. Inherent defects at the most basic level cause them to be both fat and weak: their primitive word-at-a-time style of programming inherited from their common ancestor&#8211;the von Neumann computer, their close coupling of semantics to state transitions, their division of programming into a world of expressions and a world of statements, their inability to effectively use powerful combining forms for building new programs from existing ones, and their lack of useful mathematical properties for reasoning about programs.</p>
</blockquote>

<p>As well, Backus showed one way to throw off the shackles of this mental and expressive slavery &#8212; how to <em>liberate</em> our notions of programming by leaving the von Newmann mental model behind.</p>

<p>Haskell is often described as a &#8220;purely functional programming language&#8221;, and in a technical sense it is.
(In good company, too; see <em><a href="http://conal.net/blog/posts/the-c-language-is-purely-functional/" title="blog post">The C language is purely functional</a></em>.)
However, as commonly practiced, imperative computation still plays a significant role in most Haskell programs.
Although monadic IO is wily enough to keep a good chunk of purity in our model, Haskell programmers still use the imperative model as well and therefore have to cope with the same fundamental difficulties that Backus told us about.
In a sense, we&#8217;re essentially still programming in Fortran, with its mixture of statements and expressions (or, semantically, of commands and values).
Don&#8217;t get me wrong; Haskell is a <em>much better</em> Fortran than the original.
(If you have an urge to inform me how monadic IO works, re-read what I&#8217;m really saying.  Honestly, I know.)</p>

<p>(Sadly, the very definition of &#8220;program&#8221; in Haskell requires that there be a <code>main</code> of type <code>IO ()</code>!
Haskell programs thus cannot be purely functional (except technically), and so cannot be composed except very weakly, as Backus describes.
For an alternative model, see 
<em><a href="http://conal.net/blog/posts/tangible-functional-programming-a-modern-marriage-of-usability-and-composability/" title="blog post">Tangible Functional Programming: a modern marriage of usability and composability</a></em>.)</p>

<p>In a sense, I see us as in a worse position than before.
Since the adoption of monadic IO, it&#8217;s been less immediately apparent that we&#8217;re still enslaved, so there&#8217;s been much less activity in searching for the promised land that the prophet JB foretold.
Our current home is not painful enough to goad us onward, as were continuation-based I/O and stream-based I/O.
(See <a href="http://research.microsoft.com/~simonpj/papers/history-of-haskell/">A History of Haskell</a>, section 7.1.)
Nor does it fully liberate us.</p>

<p>Since I/O is an intrisically sequential, effectful notion, and we need some way of doing input &amp; output, perhaps the best we can do is what we&#8217;ve done with monadic I/O: support both imperative and functional programming and proctect the functional from the imperative.</p>

<p>Or can we do better?</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-97"></span></p>

<p>In my recent post, <em><a href="http://conal.net/blog/posts/why-program-with-continuous-time/" title="blog post">Why program with continuous time?</a></em>, I had suggested that the abstraction of continuous time is useful even if &#8220;in the end all streams are discrete&#8221;.
I advised designing APIs based on the principle that &#8220;programming is more about the middle than the end, i.e., more about composition than about output.&#8221;
As an example, I pointed out that we don&#8217;t represent numbers as strings even if &#8220;in the end&#8221;, numbers are output as strings, suggesting that the key reason to choose an abstraction other than strings is composability (here, arithmetic).  The post gives a few other examples of this principle of designing an API for composition, not for input or output.</p>

<p>Roly Perera noted that</p>

<blockquote>
  <p>&#8230; you never really need to reach “the end”. (It really is all about composition.)</p>
</blockquote>

<p>To extend an example in the previous post, after numbers, then strings, and then pixels, are phosphors the end?
(Sorry for the obsolete technology.)
After phosphors come photons.
After photons comes retina &amp; optic nerve.
Then cognitive processing (and emotional and who-knows-what-else).
Then, via motor control, to mouse, keyboard, joystick etc.
Then machine operating system, and software again.
Round &amp; round.
More importantly, our interactions with other wetware organisms and with our planet and cosmos, and so on.</p>

<p>Roly added:</p>

<blockquote>
  <p>What looks like imperative output can be just what you observe at the boundary between two subsystems.</p>
</blockquote>

<p>Which is exactly how I look at imperative input/output.</p>

<p>Representation shifts often happen at programming interfaces.
<em>Major</em> interfaces in a whole system sometimes require a major representation shift, namely marshalling to and unmarshalling from serialized, memory-external representations.
For instance, conversion from strings to numbers on input to a computer or brain and conversion of numbers to strings on the way out.
Or replace strings with other rendered representations.
Another example: conversion from continuous time &amp; space to discrete time &amp; space (&#8220;sampling&#8221;) on input to a digital machine, and conversion from discrete time &amp; space to continuous time &amp; space (&#8220;reconstruction&#8221;) on the way from digital machine (computer, CD/DVD player) to a brain.</p>

<p>This perspective is what&#8217;s behind my confidence that we will someday really learn to look at whole systems in a consistently functional/denotational style (simple &amp; precise semantics).
The imperative interfaces in today OSs and data-bases are troubling at first, and indeed I often hear people (even on #haskell) using these examples as demonstration that the imperative programming model is inescapable.
These examples don&#8217;t trouble me, however, because I see that we&#8217;ve already dealt with others of their kind.
Underneath the <em>implementation</em> of our current functional abstractions (numbers, strings, trees, functions, etc), there are imperative mechanisms, such as memory allocation &amp; deallocation, stack frame modification, and thunk overwriting (to implement laziness).
Every one of these mechanisms can be used as evidence against the functional paradigm by someone who hasn&#8217;t yet realized how the mechanism can be shifted out of the role of a programming <em>model</em> notion and into the role of <em>implementation</em> of a programming model notion.
The new notion is more composable by virtue of being semantically simpler and mathematically more well-behaved.</p>

<p>Stack and register munging and jump/GOTO are implementations of the semantically simpler notion of function application.
(I mean &#8220;function&#8221; in the sense of math and of pure functional programming.)  Moreover, stack &amp; register munging <em>is</em> the input/output (IO) part of the implementation of function application.
Information goes <strong>out</strong> of one context and <strong>in</strong>to another.
My vision of &#8220;solving the I/O problem&#8221; for functional programming is nothing like Haskell&#8217;s semantically imperative (non-)solution (caled &#8220;IO&#8221; in Haskell), but rather to continue shifting semantically imperative mechanisms out of the programming model and into the implementation of (semantically simple) function application.</p>

<p>How do we get from here (still stuck in the quagmire of imperative programming models) to there (simple semantics and consistently excellent composability)?
First, stop rehearsing the thinking that keeps us stuck.
When we keep telling ourselves and each other that imperative notions are inevitable, accompanying such statements with hand-waving arguments, we are weaving self-fulfilling prophecies.
A person convinced of (and perhaps ego-invested in) the nonexistence of new possibilities is a person actively resisting their opportunity to discover of new possibilities &#8212; too <a href="http://conal.net/blog/posts/fostering-creativity-by-relinquishing-the-obvious/" title="blog post">stuck in the obvious</a> to discover new truths.</p>

<blockquote>
  <p><em>The significant problems of our time cannot be solved by the same level of thinking that created them.</em> &#8211; Albert Einstein</p>
  
  <p><em>They are ill discoverers that think there is no land, when they can see nothing but sea.</em> &#8211; Francis Bacon</p>
</blockquote>

<p>Hand-waving is an important factor in staying stuck in impossibility thinking, since rigorous argument uncovers unconscious limiting assumptions.
I&#8217;m just as happy if you are working on showing <em>impossibility</em> as discovering the possible, <em>as long as you are rigorous</em> and get rigorous peer review.
Otherwise, we all fall into the trap of self-deceit:</p>

<blockquote>
  <p><em>Nothing is easier than self-deceit. For what each man wishes, that he also believes to be true.</em> &#8211; Demosthenes</p>
  
  <p><em>If you wish to see the truth, then hold no opinion for or against</em> &#8211; Osho (Rajneesh Chandra Mohan Jain)</p>
  
  <p><em>The discovery of this reality is hindered rather than helped by belief, whether one believes in God or believes in atheism. We must make here a clear distinction between belief and faith, because, in general practice, belief has come to mean a state of mind which is almost the opposite of faith. Belief, as I use the word here, is the insistence that the truth is what one would &#8220;lief&#8221; or wish it to be. The believer will open his mind to the truth on the condition that it fits in with his preconceived ideas and wishes. Faith, on the other hand, is an unreserved opening of the mind to the truth, whatever it may turn out to be. Faith has no preconceptions; it is a plunge into the unknown. Belief clings, but faith lets go. In this sense of the word, faith is the essential virtue of science, and likewise of any religion that is not self-deception.</em> &#8211; Alan Watts</p>
</blockquote>

<p>In summary, I&#8217;m suggesting we take a fresh look at I/O and functional programming.
Instead of putting I/O into functional programming model (as in continuation- and stream-based I/O), or adopting a hybrid functional/imperative model (as in monadic I/O), let&#8217;s explore how to move I/O entirely out of our programming model into the <em>implementation</em> of a denotationally simple model for whole systems.</p>
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/can-functional-programming-be-liberated-from-the-von-neumann-paradigm/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
		<item>
		<title>Garbage collecting the semantics of FRP</title>
		<link>http://conal.net/blog/posts/garbage-collecting-the-semantics-of-frp/</link>
		<comments>http://conal.net/blog/posts/garbage-collecting-the-semantics-of-frp/#comments</comments>
		<pubDate>Mon, 04 Jan 2010 21:55:30 +0000</pubDate>
		<dc:creator>conal</dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[derivative]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[FRP]]></category>
		<category><![CDATA[functional reactive programming]]></category>
		<category><![CDATA[semantics]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=96</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Garbage collecting the semantics of FRP

Tags: FRP, functional reactive programming, semantics, design, derivative

URL: http://conal.net/blog/posts/garbage-collecting-the-semantics-of-frp/

-->

<!-- references -->

<!-- teaser -->

<p>Ever since <a href="http://conal.net/papers/ActiveVRML/" title="Tech report: &quot;A Brief Introduction to ActiveVRML&quot;">ActiveVRML</a>, the model we&#8217;ve been using in functional reactive programming (FRP) for interactive behaviors is <code>(T-&gt;a) -&gt; (T-&gt;b)</code>, for dynamic (time-varying) input of type <code>a</code> and dynamic output of type <code>b</code> (where <code>T</code> is time).
In &#8220;Classic FRP&#8221; formulations (including <a href="http://conal.net/papers/ActiveVRML/" title="Tech report: &quot;A Brief Introduction to ActiveVRML&quot;">ActiveVRML</a>, <a href="http://conal.net/papers/icfp97/" title="paper">Fran</a> &amp; <a href="http://conal.net/papers/push-pull-frp/" title="Paper by Conal Elliott and Paul Hudak">Reactive</a>), there is a &#8220;behavior&#8221; abstraction whose denotation is a function of time.
Interactive behaviors are then modeled as host language (e.g., Haskell) functions between behaviors.
Problems with this formulation are described in <em><a href="http://conal.net/blog/posts/why-classic-FRP-does-not-fit-interactive-behavior/" title="blog post">Why classic FRP does not fit interactive behavior</a></em>.
These same problems motivated &#8220;Arrowized FRP&#8221;.
In Arrowized FRP, behaviors (renamed &#8220;signals&#8221;) are purely conceptual.
They are part of the semantic model but do not have any realization in the programming interface.
Instead, the abstraction is a <em>signal transformer</em>, <code>SF a b</code>, whose semantics is <code>(T-&gt;a) -&gt; (T-&gt;b)</code>.
See <em><a href="http://conal.net/papers/genuinely-functional-guis.pdf" title="Paper by Antony Courtney and Conal Elliott">Genuinely Functional User Interfaces</a></em> and <em><a href="http://www.haskell.org/yale/papers/haskellworkshop02/" title="Paper by Henrik Nilsson, Antony Courtney, and John Peterson">Functional Reactive Programming, Continued</a></em>.</p>

<p>Whether in its classic or arrowized embodiment, I&#8217;ve been growing uncomfortable with this semantic model of functions between time functions.
A few weeks ago, I realized that one source of discomfort is that this model is <em>mostly junk</em>.</p>

<p>This post contains some partially formed thoughts about how to eliminate the junk (&#8220;garbage collect the semantics&#8221;), and what might remain.</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-96"></span></p>

<p>There are two generally desirable properties for a denotational semantics: <em>full abstraction</em> and <em>junk-freeness</em>.
Roughly, &#8220;full abstraction&#8221; means we must not distinguish between what is (operationally) indistinguishable, while &#8220;junk-freeness&#8221; means that every semantic value must be denotable.</p>

<p>FRP&#8217;s semantic model, <code>(T-&gt;a) -&gt; (T-&gt;b)</code>, allows not only arbitrary (computable) transformation of input values, but also of time.
The output at some time can depend on the input at any time at all, or even on the input at arbitrarily many different times.
Consequently, this model allows respoding to <em>future</em> input, violating a principle sometimes called &#8220;causality&#8221;, which is that outputs may depend on the past or present but not the future.</p>

<p>In a causal system, the present can reach backward to the past but not forward the future.
I&#8217;m uneasy about this ability as well.
Arbitrary access to the past may be much more powerful than necessary.
As evidence, consult the system we call (physical) Reality.
As far as I can tell, Reality operates without arbitrary access to the past or to the future, and it does a pretty good job at expressiveness.</p>

<p>Moreover, arbitrary past access is also problematic to implement in its semantically simple generality.</p>

<p>There is a thing we call informally &#8220;memory&#8221;, which at first blush may look like access to the past, it isn&#8217;t really.
Rather, memory is access to a <em>present</em> input, which has come into being through a process of filtering, gradual accumulation, and discarding (forgetting).
I&#8217;m talking about &#8220;memory&#8221; here in the sense of what our brains do, but also what all the rest of physical reality does.
For instance, weather marks on a rock are part of the rock&#8217;s (present) memory of the past weather.</p>

<p>A very simple memory-less semantic model of interactive behavior is just <code>a -&gt; b</code>.
This model is too restrictive, however, as it cannot support <em>any</em> influence of the past on the present.</p>

<p>Which leaves a question: what is a simple and adequate formal model of interactive behavior that reaches neither into the past nor into the future, and yet still allows the past to influence the present?
Inspired in part by a design principle I call &#8220;what would reality do?&#8221; (WWRD), I&#8217;m happy to have some kind of infinitesimal access to the past, but nothing further.</p>

<p>My current intuition is that differentiation/integration plays a crucial role.
That information is carried forward moment by moment in time as &#8220;momentum&#8221; in some sense.</p>

<blockquote>
  <p><em>I call intuition cosmic fishing. You feel a nibble, then you&#8217;ve got to hook the fish.</em> &#8211; Buckminster Fuller</p>
</blockquote>

<p>Where to go with these intuitions?</p>

<p>Perhaps interactive behaviors are some sort of function with all of its derivatives.
See <em><a href="http://conal.net/blog/posts/beautiful-differentiation/" title="blog post">Beautiful differentiation</a></em> for an specification and derived implementation of numeric operations, and more generally of <code>Functor</code> and <code>Applicative</code>, on which much of FRP is based.</p>

<p>I suspect the whole event model can be replaced by integration.
Integration is the main remaining piece.</p>

<p>How weak a semantic model can let us define integration?</p>

<h3>Thanks</h3>

<p>My thanks to Luke Palmer and to Noam Lewis for some clarifying chats about these half-baked ideas.
And to the folks on #haskell IRC for <a href="http://tunes.org/~nef/logs/haskell/10.01.04">brainstorming titles for this post</a>.
My favorite suggestions were</p>

<ul>
<li>luqui: instance HasJunk FRP where</li>
<li>luqui: Functional reactive programming&#8217;s semantic baggage</li>
<li>sinelaw: FRP, please take out the trash!</li>
<li>cale: Garbage collecting the semantics of FRP</li>
<li>BMeph: Take out the FRP-ing Trash</li>
</ul>

<p>all of which I preferred over my original &#8220;FRP is mostly junk&#8221;.</p>
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/garbage-collecting-the-semantics-of-frp/feed/</wfw:commentRss>
		<slash:comments>30</slash:comments>
		</item>
		<item>
		<title>Communication, curiosity, and personal style</title>
		<link>http://conal.net/blog/posts/communication-curiosity-and-personal-style/</link>
		<comments>http://conal.net/blog/posts/communication-curiosity-and-personal-style/#comments</comments>
		<pubDate>Mon, 04 Jan 2010 07:11:02 +0000</pubDate>
		<dc:creator>conal</dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[communication]]></category>
		<category><![CDATA[creativity]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=95</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Communication, curiosity, and personal style

Tags: creativity, communication

URL: http://conal.net/blog/posts/communication-curiosity-and-personal-style/

-->

<!-- references -->

<p>When people talk, <em>especially</em> via the Internet, I expect misunderstandings, since human language is so fraught with ambiguity.  And though I work very hard at clarity, I know my skill is limited and I sometimes fail.</p>

<p>So I see communication as an iterative process: I say something that you don&#8217;t quite get, and you say &#8220;Huh?&#8221; about the what or why or how of some particular part, and I refine my message.  Likewise, I listen to you, and check out the places were I&#8217;m puzzled.</p>

<p>I&#8217;m very sensitive where I think I&#8217;m seeing criticisms/dismissals without inquiry/curiosity, and hence some peevishness in some remarks (now edited) in my previous post, <em><a href="http://conal.net/blog/posts/why-program-with-continuous-time/" title="blog post">Why program with continuous time?</a></em>.
See <a href="http://conal.net/blog/posts/fostering-creativity-by-relinquishing-the-obvious/">Fostering creativity by relinquishing the obvious</a> for related remarks.</p>

<p>In some venues, like reddit, aggressive non-inquiry can become the dominant mode of discussion.  Because I prefer high-quality, open-minded inquiry, I mostly choose moderated blogging instead.
If I don&#8217;t pass a comment through, I&#8217;ll usually offer the writer some suggestions, perhaps suggesting a possible misreading of whatever was being responded to, and invite re-submission.
If I&#8217;m particularly annoyed with the writer, I&#8217;ll usually take time to get over my annoyance before responding, so there can be a delay.</p>

<p>In this way, I try to keep a high signal-to-noise ratio, where noise includes assumptions, reactions to misreadings, and often compounded public attempts by people to get each other to listen more carefully.</p>

<p>I&#8217;m starting to discover that people I don&#8217;t get along with sometimes have a very different style from mine.  I like to invite many possibilities into a space and explore them.
My mother shared with me a quote from Henry Nelson Wieman, which she now uses as her email signature:</p>

<blockquote>
  <p>To get the viewpoint of the other person appreciatively and profoundly and reconcile it with his own so far as possible is the supreme achievement of man and his highest vocation.</p>
</blockquote>

<p>While I&#8217;m agnostic about the &#8220;supreme/highest&#8221; part (and open to it), I very much like Wieman&#8217;s description of individual and collective learning as progressing most powerfully by integrating different viewpoints, as founded on working to understanding each other &#8220;appreciatively and profoundly&#8221;.</p>

<p>I&#8217;m learning that some other folks have an oppositional style of learning and discovering.</p>

<p>When <a href="http://noordering.wordpress.com/">Thomas (Bob) Davie</a> and I worked together in Antwerp, he told me that his style is to fiercely resist (battle) any new idea proposed to him.  Whatever breaks through his defenses is worth his learning.  I was flabbergasted at the distance between his style and mine.  And greatly relieved also, because I had to work with him, and I had previously interpreted his behavior as non-curious and even anti-creative.  Although I wasn&#8217;t willing to collaborate in the battle mode at the time, fortunately he was willing to try shifting his style.  And the recognition of our differing style toward similar ends helped greatly in relieving the building tension between us.  Now I enjoy him very much.</p>

<p>Since this surprising discovery, I&#8217;ve wondered how often friction I have with other people coincides with this particular difference in personal styles and whether there are additional style that I hadn&#8217;t been aware of.  So when friction arises, I now try to find out, via a private chat or email.</p>

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

<ul>
<li>2010-01-04: Filled in Bob&#8217;s name (with permission).</li>
</ul>

<!-- without a comment or something here, the last item above becomes a paragraph -->
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/communication-curiosity-and-personal-style/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Is program proving viable and useful?</title>
		<link>http://conal.net/blog/posts/is-program-proving-viable-and-useful/</link>
		<comments>http://conal.net/blog/posts/is-program-proving-viable-and-useful/#comments</comments>
		<pubDate>Sun, 03 Jan 2010 03:21:48 +0000</pubDate>
		<dc:creator>conal</dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[creativity]]></category>
		<category><![CDATA[proof]]></category>
		<category><![CDATA[simplicity]]></category>
		<category><![CDATA[specification]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=94</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Is program proving viable and useful?

Tags: specification, proof, creativity, simplicity

URL: http://conal.net/blog/posts/is-program-proving-viable-and-useful/

-->

<!-- references -->

<!--
**Edits**:

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

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

<p>Is program proving a viable and useful endeavor?
Does it create more problems than it solves?</p>

<p>Today I participated in <a href="http://www.reddit.com/r/programming/comments/akhvr/reflections_on_a_holy_grail_functional_reactive/c0i2gsd">a reddit discussion  thread</a>.
The discussion brought up some issues I care about, and I think others will care also.</p>

<p>Although I&#8217;m quoting from another person whose viewpoint may contrast with mine, I mean no insult or disrespect to him.
I think his views are fairly common, and I appreciate his putting them into writing and stimulating me to put some of mine into writing as well.</p>

<blockquote>
  <p>[...] you could specify the behavior it should have precisely.
  However in this case (and in most practical cases that aren&#8217;t tricky algorithms actually) the mathematical spec is not much shorter than the implementation.
  So meeting the spec does not help, the spec is just as likely to be wrong/incomplete as the implementation.
  With wrong/incomplete I mean that even if you meet the spec you don&#8217;t attain your goal.</p>
</blockquote>

<p>I like the observation that precise specifications can be as complex and difficult to get correct as precise programs.
And indeed specification complexity is problematic.
Rather than give up on precision, my own interest is in finding simpler and clearer ways to think about things so that specifications can be quite simple.
Thus my focus on simple and precise semantic models.
One example is as given in <em><a href="http://conal.net/papers/push-pull-frp/" title="Paper">Push-pull functional reactive programming</a></em>.
I&#8217;m proud of the simplicity of this specification and of how clearly it shows what parts of the model could use improving.
(The <code>Event</code> type doesn&#8217;t follow the <a href="">type class morphism</a> principle, and indeed has difficulty in practice.)
And since such semantics-based software design methods are still in their infancy, I agree that it&#8217;s usually more practical to abandon preciseness in software development.</p>

<p>For more examples of formally simple specifications, see the papers <em><a href="http://conal.net/blog/posts/denotational-design-with-type-class-morphisms/" title="blog post">Denotational design with type class morphisms</a></em> and <em><a href="http://conal.net/blog/posts/beautiful-differentiation/" title="blog post">Beautiful differentiation</a></em>.</p>

<p>I suspect the crux of the issue here is that <em>combining precision and simplicity is very difficult</em> &#8212; whether for specifications or programs (or for many other other human endeavors).</p>

<blockquote>
  <p><em>Any intelligent fool can make things bigger and more complex &#8230; it takes a touch of genius &#8212; and a lot of courage &#8212; to   move in the opposite direction.</em> &#8211; Albert Einstein</p>
</blockquote>

<p>We had been writing informal/imprecise &#8220;programs&#8221; for a long time before computers were invented, as various kinds of instructions/recipies.
With mechanization of program handling in the 1940s came the requirement of precision.
Since simple precision is difficult (according to my hypothesis), and precision is required, simplicity usually loses out.
So our history of executably-precise programs is populated mostly by complexity.</p>

<blockquote>
  <p><em>I conclude that there are two ways of constructing a software design: One way is to make it so simple that there are   obviously no deficiencies, and the other way is make it so complicated that there are no obvious deficiencies.
  The first   method is far more difficult.</em>  &#8211; C.A.R. Hoare, <a href="http://scifac.ru.ac.za/cspt/hoare.htm">The Emperor&#8217;s Old Clothes, Turing Award lecture,   1980</a></p>
</blockquote>

<p>As much as one assumes that the future is bound to perpetuate the past, one might believe that we will <em>never</em> escape complexity.
In my own creative process, I like to distinguish between the notion of <em>necessarily true</em> and merely <em>historically true</em>.
I understand progress itself to be about this distinction &#8212; the overturning of the historically true that turns out (often surprisingly) not to be necessarily true.</p>

<p>What about <em>specification</em>?
Are precise specifications <em>necessarily</em> complex, or merely <em>historically</em> complex?
(I.e., <em>must</em> we sit by while the future repeats the complexity of the past?)  This question I see as even more unexplored than the same question for programs, because we have not been required to work with formal specifications nearly as much as with formal programs.
Even so, some people have <em>chosen</em> to work on precise specification and so some progress has been made in improving their simplicity, and I&#8217;m optimistic about the future.
Optimistic enough to devote some of my life energy to the search.</p>

<blockquote>
  <p><em>Simplicity is the most difficult thing to secure in this world; it is the last limit of experience and the last effort of genius.</em> &#8211; George Sand</p>
  
  <p><em>Everything is vague to a degree you do not realize till you have tried to make it precise.</em> &#8211; Bertrand Russell</p>
</blockquote>

<p>I expect that future software developers will look back on our era as barbaric, the way we look at some of the medical practices of the past.
Of course, we&#8217;re doing the best we know how, just as past medical practitioners were.</p>

<p>I know people are apt to hear criticism/insult whether present or not, and I hope that these words of mine are not heard as such.
I appreciate both the current practitioners, doing their best with tools at hand, and the pioneer, groping toward the methods of the future.
Personally, I play both roles, and I&#8217;m guessing you do as well, perhaps differing only in balance.
I urge us all to appreciate and respect each other more as we muddle on.</p>
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/is-program-proving-viable-and-useful/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Why program with continuous time?</title>
		<link>http://conal.net/blog/posts/why-program-with-continuous-time/</link>
		<comments>http://conal.net/blog/posts/why-program-with-continuous-time/#comments</comments>
		<pubDate>Sun, 03 Jan 2010 02:39:49 +0000</pubDate>
		<dc:creator>conal</dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[continuous]]></category>
		<category><![CDATA[design]]></category>

		<guid isPermaLink="false">http://conal.net/blog/?p=93</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<!-- 

Title: Why program with continuous time?

Tags: design, continuous

URL: http://conal.net/blog/posts/why-program-with-continuous-time/

-->

<!-- references -->

<p>For me functional reactive programming (FRP) has been mainly about two principles.</p>

<p>One is <em>denotational design</em> (i.e., based on simple, precise denotational semantics), which has been integral to FRP since the first incarnation as <a href="http://conal.net/papers/ActiveVRML/" title="Paper">ActiveVRML</a>.
I&#8217;ve written <a href="http://conal.net/papers/push-pull-frp/" title="Paper">several</a> <a href="http://conal.net/blog/posts/beautiful-differentiation/" title="blog post">things</a> <a href="http://conal.net/blog/posts/denotational-design-with-type-class-morphisms/" title="blog post">recently</a> about denotational design.</p>

<p>My second core principle is <em>continuous time</em>, which has been present since <a href="http://conal.net/papers/icfp97/" title="Paper">Fran</a> &amp; ActiveVRML&#8217;s predecessor <a href="http://conal.net/papers/siggraph94.pdf" title="Paper">TBAG</a>.</p>

<p>Today I read a confusion that I&#8217;ve heard many times before about continuous time, which I&#8217;d like to bring up, in part because I love continuous time, and in part because there&#8217;s a much broader underlying principle of software design.</p>

<blockquote>
  <p>[...] I don&#8217;t see why the lack of continuous streams leaves a &#8220;gap&#8221;.  In the end all streams are discrete.</p>
</blockquote>

<p>&#8220;In the end&#8221;, yes.
Just as in the end, numbers are displayed as ascii numerals.
However, programming is more about the middle than the end, i.e., more about composition than about output.
For that reason, we don&#8217;t generally use strings in place of numbers.
If we did, composition operations (arithmetic) would be very awkward.
Similarly, continuity in space and in time is better for composition/modularity, leaving discreteness to the output step.</p>

<p>Another name for &#8220;continuous&#8221; is &#8220;resolution-independent&#8221;, and thus able to be transformed in time and space with ease and without propagating and amplifying sampling artifacts.</p>

<p>As another example, consider the data types in a 3D graphics API.
In the end, all graphics is pixels, isn&#8217;t it?
So what gap is left in a pixel-oriented API that doesn&#8217;t address higher-level continuous notions like triangles or curved surfaces?
(Hint: it&#8217;s not just speed.)</p>

<p>One could go further than strings and pixels, and say that &#8220;in the end&#8221; my data types will end up as phosphors or electrical impulses, so programming at those levels is perfectly adequate.
Again, composability would suffer.</p>

<p>Another example is functional vs imperative programming.
It&#8217;s all side-effects <em>in the end</em>.
Functional programming excels in composability, as explained and illustrated by John Hughes in <em><a href="http://www.cs.chalmers.se/~rjmh/Papers/whyfp.html" title="Paper by John Hughes">Why Functional Programming Matters</a></em>.
And I&#8217;m not just talking about <em>pure</em> functional programming, but the availability of compound <em>expressions</em>, as introduced in Fortran (controversially), despite that machines just execute sequences of atomic, side-effecting statements <em>in the end</em>.</p>

<p>Another example, and really the heart of John&#8217;s paper, is finite vs infinite data structures.
We only access a finite amount of data <em>in the end</em>.
However, allowing infinite data structures <em>in the middle</em> makes for a much more composable programming style.</p>

<p>Some unsolicited advice to us all: Next time you see someone doing something and you don&#8217;t understand their underlying motivation, ask them.
Many issues are not immediately obvious, so don&#8217;t be shy!
Reading papers can help as well.</p>

<p>For more on continuous time:</p>

<ul>
<li><em><a href="http://conal.net/papers/siggraph94.pdf" title="Paper">TBAG: A High Level Framework for Interactive, Animated 3D Graphics Applications</a></em></li>
<li><em><a href="http://conal.net/papers/icfp97/" title="Paper">Functional Reactive Animation</a></em></li>
<li><em><a href="http://conal.net/papers/tse-modeled-animation/" title="Paper">An Embedded Modeling Language Approach to Interactive 3D and Multimedia Animation</a></em></li>
<li><em><a href="http://conal.net/papers/plilpalp98/" title="Paper">Functional Implementations of Continuous Modeled Animation</a></em></li>
</ul>

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

<ul>
<li>2010-01-03: Trimmed &amp; tweaked the unsolicited advice.  Hopefully less peevish/patronizing now.  Thanks for the feedback.</li>
<li>2010-01-07: Trimmed quote.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/why-program-with-continuous-time/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>
