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

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

		<guid isPermaLink="false">http://conal.net/blog/?p=169</guid>
		<description><![CDATA[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. Lazy 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 [&#8230;]]]></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.cse.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>

<pre><code>type Context t = [Der (PF t) t]

type Zipper t = (Context t, t)
</code></pre>

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

<pre><code>type Context t = Stream (Der (PF t) t)
</code></pre>

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

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

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

<pre><code>data Quadtree a = Quadtree a (Quad (Quadtree a))

type Quad t = (t,t,t,t)
</code></pre>

<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><a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.83.8567" title="paper by Mitchell Wand">Continuation-Based Program Transformation Strategies</a></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>
<p><a href="http://conal.net/blog/?flattrss_redirect&amp;id=169&amp;md5=3629f5555d3fd4d816dcbb1907f9b289"><img src="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png" srcset="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@2x.png 2xhttp://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@3x.png 3x" alt="Flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/topless-data/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=conal&amp;popout=1&amp;url=http%3A%2F%2Fconal.net%2Fblog%2Fposts%2Ftopless-data&amp;language=en_GB&amp;category=text&amp;title=Topless+data&amp;description=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.+Lazy+functional...&amp;tags=zipper%2Cblog" type="text/html" />
	</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><![CDATA[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[The zipper is an efficient and elegant data structure for purely functional editing of tree-like data structures, first published by Gérard Huet. Zippers maintain a location of focus in a tree and support navigation operations (up, down, left, right) and editing (replace current focus). The original zipper type and operations are customized for a single [&#8230;]]]></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>

<pre><code>class Functor f ⇒ Holey f where
  type Der f :: * → *
  fillC :: Der f a → a → f a
</code></pre>

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

<pre><code>type Loc f a = (Der f a, a)
</code></pre>

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

<pre><code>fill :: Holey f ⇒ Loc f a → f a
fill = uncurry fillC
</code></pre>

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

<pre><code>guess1 :: f a → Loc f a
</code></pre>

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

<pre><code>guess2 :: f a → Maybe (Loc f a)
</code></pre>

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

<pre><code>guess3 :: f a → [Loc f a]
</code></pre>

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

<pre><code>class Functor f ⇒ Holey f where
  type Der f :: * → *
  fillC   :: Der f a → a → f a
  extract :: f a → f (Loc f a)
</code></pre>

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

<pre><code>instance Holey (Const x) where
  type Der (Const x) = Void
  fillC = voidF
  extract (Const x) = Const x
</code></pre>

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

<pre><code>instance Holey Id where
  type Der Id = Unit
  fillC (Const ()) = Id
  extract (Id a) = Id (Const (), a)
</code></pre>

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

<pre><code>InL fa :: (f :+: g) a

fa :: f a

extract fa :: f (Loc f a)

           :: f (Der f a, a)

(fmap.first) InL (extract fa) :: f ((Der f :+: Der g) a, a)

                              :: f ((Der (f :+: g) a), a)
</code></pre>

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

<pre><code>InL ((fmap.first) InL (extract fa)) :: (f :+: g) ((Der (f :+: g) a), a)

                                    :: (f :+: g) (Loc (f :+: g) a)
</code></pre>

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

<pre><code>InR ((fmap.first) InR (extract ga)) :: (f :+: g) (Loc (f :+: g) a)
</code></pre>

<p>So,</p>

<pre><code>instance (Holey f, Holey g) ⇒ Holey (f :+: g) where
  type Der (f :+: g) = Der f :+: Der g
  fillC (InL df) = InL ∘ fillC df
  fillC (InR df) = InR ∘ fillC df
  extract (InL fa) = InL ((fmap.first) InL (extract fa))
  extract (InR ga) = InR ((fmap.first) InR (extract ga))
</code></pre>

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

<pre><code>  type Der (f :*: g) = Der f :*: g  :+:  f :*: Der g
</code></pre>

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

<pre><code>fa :*: ga :: (f :*: g) a
</code></pre>

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

<pre><code>fa :: f a

extract fa :: f (Loc f a)
           :: f (Der f a, a)

(fmap.first) (:*: ga) (extract fa) :: f ((Der f :*: g) a, a)

(fmap.first) (InL ∘ (:*: ga)) (extract fa)
  :: f (((Der f :*: g) :+: (f :*: Der g)) a, a)

  :: f ((Der (f :*: g)) a, a)
</code></pre>

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

<pre><code>(fmap.first) (InR ∘ (fa :*:)) (extract ga)
  :: g ((Der (f :*: g)) a, a)
</code></pre>

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

<pre><code>(fmap.first) (InL ∘ (:*: ga)) (extract fa) :*:
(fmap.first) (InR ∘ (fa :*:)) (extract ga)
  :: (f :*: g) (Der (f :*: g) a, a)
</code></pre>

<p>So,</p>

<pre><code>instance (Holey f, Holey g) ⇒ Holey (f :*: g) where
  type Der (f :*: g) = Der f :*: g  :+:  f :*: Der g
  fillC (InL (dfa :*:  ga)) = (:*: ga) ∘ fillC dfa
  fillC (InR ( fa :*: dga)) = (fa :*:) ∘ fillC dga
  extract (fa :*: ga) = 
    (fmap.first) (InL ∘ (:*: ga)) (extract fa) :*:
    (fmap.first) (InR ∘ (fa :*:)) (extract ga)
</code></pre>

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

<pre><code>type Der (g :. f) = Der g :. f  :*:  Der f
</code></pre>

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

<pre><code>gfa :: g (f a)

extract gfa :: g (Loc g (f a))

            :: g (Der g (f a), f a)

fmap (second extract) (extract gfa)
  :: g (Der g (f a), f (Loc f a))
</code></pre>

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

<pre><code>tweak2 :: Functor f ⇒
          (dg (f a), f (df a, a)) → f (((dg :. f) :*: df) a, a)
tweak2 = (fmap.first) chainRule ∘ tweak1

tweak1 :: Functor f ⇒
          (dg (fa), f (dfa, a)) → f ((dg (fa), dfa), a)
tweak1 = fmap lassoc ∘ squishP

squishP :: Functor f ⇒ (a, f b) → f (a,b)
squishP (a,fb) = fmap (a,) fb

chainRule :: (dg (f a), df a) → ((dg :. f) :*: df) a
chainRule (dgfa, dfa) = O dgfa :*: dfa

lassoc :: (p,(q,r)) → ((p,q),r)
lassoc    (p,(q,r)) =  ((p,q),r)
</code></pre>

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

<pre><code>tweak2 (dgfa, fl) = (fmap.first) (O dgfa :*:) fl
</code></pre>

<p>More specifically,</p>

<pre><code>tweak2 :: Functor f =&gt; (Der g (f a), f (Loc f a))
                    -&gt; f (((Der g :. f) :*: Der f) a, a)
       :: Functor f =&gt; (Der g (f a), f (Loc f a))
                    -&gt; f (Der (g :. f) a, a)
       :: Functor f =&gt; (Der g (f a), f (Loc f a))
                    -&gt; f (Loc (g :. f) a)
</code></pre>

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

<pre><code>fmap (tweak2 ∘ second extract) (extract gfa) :: g (f (Loc (g :. f) a))
</code></pre>

<p>So</p>

<pre><code>extractGF :: (Holey f, Holey g) ⇒
             g (f a) → g (f (Loc (g :. f) a))
extractGF = fmap (tweak2 ∘ second extract) ∘ extract
</code></pre>

<p>and</p>

<pre><code>instance (Holey f, Holey g) ⇒ Holey (g :. f) where
  type Der (g :.  f) = Der g :. f  :*:  Der f
  fillC (O dgfa :*: dfa) = O ∘ fillC dgfa ∘ fillC dfa
  extract = inO extractGF
</code></pre>

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

<pre><code>-- | Apply a unary function within the 'O' constructor.
inO :: (g (f a) → g' (f' a')) → ((g :. f) a → (g' :. f') a')
inO = unO ~&gt; O

infixr 1 ~&gt;
-- | Add pre- and post processing
(~&gt;) :: (a' → a) → (b → b') → ((a → b) → (a' → b'))
(f ~&gt; h) g = h ∘ g ∘ f
</code></pre>

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

<pre><code>fix f = f (fix f)

type Fix f = f (Fix f)
</code></pre>

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

<pre><code>newtype Fix f = Fix { unFix :: f (Fix f) }
</code></pre>

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

<pre><code>data Context f = TopC | Context (Der f (Fix f)) (Context f)  -- first try
</code></pre>

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

<pre><code>type Context f = [Der f (Fix f)]
</code></pre>

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

<pre><code>type Loc f a = (Der f a, a)
</code></pre>

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

<pre><code>type Zipper f = (Context f, Fix f)
</code></pre>

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

<pre><code>up   :: Holey f ⇒ Zipper f →    Zipper f

down :: Holey f ⇒ Zipper f → f (Zipper f)
</code></pre>

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

<pre><code>up   :: Holey f ⇒ Zipper f →    Zipper f
up (d:ds', t) = (ds', Fix (fill (d,t)))
</code></pre>

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

<pre><code>(d:ds', t) :: Zipper f
(d:ds', t) :: (Context f, Fix f)

d:ds' :: [Der f (Fix f)]

t :: Fix f

d   ::  Der f (Fix f)
ds' :: [Der f (Fix f)]

fill :: Loc f b → f b
fill :: (Der f b, b) → f b
fill :: (Der f (Fix f), Fix f) → f (Fix f)

fill (d,t) :: f (Fix f)

Fix (fill (d,t)) :: Fix f

(ds', Fix (fill (d,t))) :: (Context f, Fix f)
                        :: Zipper f
</code></pre>

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

<pre><code>up' :: Holey f =&gt; Zipper f -&gt; Maybe (Zipper f)
up' ([]   , _) = Nothing
up' l          = Just (up l)
</code></pre>

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

<pre><code>down :: Holey f ⇒ Zipper f → f (Zipper f)
down (ds', t) = (fmap.first) (:ds') (extract (unFix t)) (unFix t))
</code></pre>

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

<pre><code>(ds',t) :: Zipper f
        :: (Context f, Fix f)
        :: ([Der f (Fix f)], Fix f)

ds' :: [Der f (Fix f)]
t :: Fix f
unFix t :: f (Fix f)

extract (unFix t) :: f (Der f (Fix f), Fix f)

(fmap.first) (:ds') (extract (unFix t))
  :: ([Der f (Fix f)], Fix f)
  :: (Context f, Fix f)
  :: LocFix f
</code></pre>

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

<pre><code>class Functor (PF t) ⇒ Regular t where
  type PF t :: * → *
  wrap   :: PF t t → t
  unwrap :: t → PF t t
</code></pre>

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

<pre><code>type Context t = [Der (PF t) t]

type Zipper t = (Context t, t)
</code></pre>

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

<pre><code>up   :: (Regular t, Holey (PF t)) ⇒ Zipper t →       Zipper t
down :: (Regular t, Holey (PF t)) ⇒ Zipper t → PF t (Zipper t)

up (d:ds', t) = (ds', wrap (fill (d,t)))

down (ds', t) = (fmap.first) (:ds') (extract (unwrap t))
</code></pre>
<p><a href="http://conal.net/blog/?flattrss_redirect&amp;id=154&amp;md5=29e8305b61bfd15d10c9a8d4f4b13bdf"><img src="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png" srcset="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@2x.png 2xhttp://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@3x.png 3x" alt="Flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/another-angle-on-zippers/feed</wfw:commentRss>
		<slash:comments>16</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=conal&amp;popout=1&amp;url=http%3A%2F%2Fconal.net%2Fblog%2Fposts%2Fanother-angle-on-zippers&amp;language=en_GB&amp;category=text&amp;title=Another+angle+on+zippers&amp;description=The+zipper+is+an+efficient+and+elegant+data+structure+for+purely+functional+editing+of+tree-like+data+structures%2C+first+published+by+G%C3%A9rard+Huet.+Zippers+maintain+a+location+of+focus+in+a...&amp;tags=derivative%2Cfunctor%2Czipper%2Cblog" type="text/html" />
	</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><![CDATA[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[A &#8220;one-hole context&#8221; is a data structure with one piece missing. Conor McBride pointed out that the derivative of a regular type is its type of one-hole contexts. 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 [&#8230;]]]></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>

<pre><code>data Void a   -- no constructors

type Unit a        = Const () a

data Const x a     = Const x

newtype Id a       = Id a

data (f :+: g) a   = InL (f a) | InR (g a)

data (f :*: g) a   = f a :*: g a

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

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

<pre><code>type family Der (f :: (* → *)) :: (* → *)
</code></pre>

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

<pre><code>der (const x) ≡ 0
der id        ≡ 1
</code></pre>

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

<pre><code>type instance Der (Const x) = Void   -- 0

type instance Der Id        = Unit   -- 1
</code></pre>

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

<pre><code>type instance Der (f :+: g) = Der f :+: Der g
</code></pre>

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

<pre><code>der (f + g) ≡ der f + der g
</code></pre>

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

<pre><code>type instance Der (f :*: g) = Der f :*: g  :+:  f :*: Der g
</code></pre>

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

<pre><code>der (f * g) ≡ der f * g + f * der g
</code></pre>

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

<pre><code>type instance Der (g :. f) = (Der g :. f) :*: Der f
</code></pre>

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

<pre><code>der (g ∘ f) ≡ (der g ∘ f) * der f
</code></pre>

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

<pre><code>der (g ∘ f) a ≡ der g (f a)  * der f a
</code></pre>

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

<pre><code>der (const x) ≡ 0
Der (Const x) ≡ Void

der id ≡ 1
Der Id ≡ Unit

der (f  +  g) ≡ der f  +  der g
Der (f :+: g) ≡ Der f :+: Der g

der (f  *  g) ≡ der f  *  g  +  f  *  der g
Der (f :*: g) ≡ Der f :*: g :+: f :*: Der g

der (g  ∘ f) ≡ (der g  ∘ f)  *  der f
Der (g :. f) ≡ (Der g :. f) :*: Der f
</code></pre>

<h3>Filling holes</h3>

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

<pre><code>fillC :: Functor f ⇒ Der f a → a → f a
</code></pre>

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

<pre><code>class Functor f ⇒ Holey f where
  type Der f :: * → *
  fillC :: Der f a → a → f a
</code></pre>

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

<pre><code>instance Holey (Const x) where
  type Der (Const x) = Void
  fillC = error "fillC for Const x: no Der values"
</code></pre>

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

<pre><code>  voidF :: Void a → b
  voidF = error "voidF: no value of type Void"
</code></pre>

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

<pre><code>  fillC = voidF
</code></pre>

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

<pre><code>instance Holey Id where
  type Der Id = Unit
  fillC (Const ()) a = Id a
</code></pre>

<p>More succinctly,</p>

<pre><code>  fillC (Const ()) = Id
</code></pre>

<p>For sums,</p>

<pre><code>instance (Holey f, Holey g) ⇒ Holey (f :+: g) where
  type Der (f :+: g) = Der f :+: Der g
  fillC (InL df) a = InL (fillC df a)
  fillC (InR df) a = InR (fillC df a)
</code></pre>

<p>or</p>

<pre><code>  fillC (InL df) = InL ∘ fillC df
  fillC (InR df) = InR ∘ fillC df
</code></pre>

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

<pre><code>instance (Holey f, Holey g) ⇒ Holey (f :*: g) where
  type Der (f :*: g) = Der f :*: g  :+:  f :*: Der g
  fillC (InL (dfa :*:  ga)) a = fillC dfa a :*: ga
  fillC (InR ( fa :*: dga)) a = fa :*: fillC dga a
</code></pre>

<p>Less pointfully,</p>

<pre><code>  fillC (InL (dfa :*:  ga)) = (:*: ga) ∘ fillC dfa
  fillC (InR ( fa :*: dga)) = (fa :*:) ∘ fillC dga
</code></pre>

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

<pre><code>instance (Holey f, Holey g) ⇒ Holey (g :. f) where
  type Der (g :. f) = (Der g :. f) :*: Der f
  fillC (O dgfa :*: dfa) a = O (fillC dgfa (fillC dfa a))
</code></pre>

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

<pre><code>  fillC (O dgfa :*: dfa) = O ∘ fillC dgfa ∘ fillC dfa
</code></pre>

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

<pre><code>der (g  ∘ f) ≡ (der g  ∘ f)  *  der f

Der (g :. f) ≡ (Der g :. f) :*: Der f
</code></pre>

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

<pre><code>  fillC (O dgfa :*: dfa) = O ∘ fillC dgfa ∘ fillC dfa
</code></pre>

<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>
<p><a href="http://conal.net/blog/?flattrss_redirect&amp;id=140&amp;md5=aa94cf980e9605e54025a8adb6e7d65f"><img src="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png" srcset="http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@2x.png 2xhttp://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white.png, http://conal.net/blog/wp-content/plugins/flattr/img/flattr-badge-white@3x.png 3x" alt="Flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://conal.net/blog/posts/differentiation-of-higher-order-types/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=conal&amp;popout=1&amp;url=http%3A%2F%2Fconal.net%2Fblog%2Fposts%2Fdifferentiation-of-higher-order-types&amp;language=en_GB&amp;category=text&amp;title=Differentiation+of+higher-order+types&amp;description=A+%26%238220%3Bone-hole+context%26%238221%3B+is+a+data+structure+with+one+piece+missing.+Conor+McBride+pointed+out+that+the+derivative+of+a+regular+type+is+its+type+of+one-hole+contexts.+When+a...&amp;tags=derivative%2Cfunctor%2Czipper%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Sequences, segments, and signals</title>
		<link>http://conal.net/blog/posts/sequences-segments-and-signals</link>
		<comments>http://conal.net/blog/posts/sequences-segments-and-signals#comments</comments>
		<pubDate>Fri, 05 Dec 2008 08:14:33 +0000</pubDate>
		<dc:creator><![CDATA[Conal]]></dc:creator>
				<category><![CDATA[Functional programming]]></category>
		<category><![CDATA[applicative functor]]></category>
		<category><![CDATA[comonad]]></category>
		<category><![CDATA[FRP]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[functional reactive programming]]></category>
		<category><![CDATA[functor]]></category>
		<category><![CDATA[monoid]]></category>
		<category><![CDATA[segment]]></category>
		<category><![CDATA[sequence]]></category>
		<category><![CDATA[type class morphism]]></category>
		<category><![CDATA[zipper]]></category>

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

Title: Sequences, segments, and signals

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

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

-->

<!-- references -->

<!-- teaser -->

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

<h3>Sequences of segments</h3>

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

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

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

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

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

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

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

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

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

<h4>Higher-order wrappers</h4>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

<p>Proof:</p>

<pre><code>meaning . fmap h 

  == {- fmap definition -}

meaning . S . fmap (fmap h) . unS

  == {- meaning definition -}

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

  == {- unS and S are inverses  -}

mconcat . fmap (fmap h) . unS

  == {- fmap h distributes over mappend -}

fmap h . mconcat . unS

  == {- meaning definition -}

fmap . meaning
</code></pre>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

<p>with content part:</p>

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

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

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

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

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

<h3>Closing remarks</h3>

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