## Fixed Point Datatypes

This is more Haskell translation of my reading of “Functional Programming with Bananas, Lenses, Envelopes and Barbed Wire”, this time concerning fixed points of data types.

First, what is a fixed point (aka. fixpoint)? In calculus, functions can have fixed points. These are values $x$ such that $f(x) = x$. As an example, $f(x) = x^2$ has a fixed point at both 0 and 1. For $f(x) = x$, every $x$ is a fixpoint!

The notion of fixpoints in programming is somewhat different, though related. In programming fixed points are about self-application, rather than the calculus definition above. I hope the distinction will become clear with some examples.

First, consider the following definition of a simple pair-like type:

`data Pair a b = Pair a b`

I can use any type I like for a and b, including, say, Pair a b:

`example1 = Pair 1 (Pair 2 'a')`

I could go further:

`example2 = Pair 1 (Pair 2 (Pair 3 (Pair 4 (Pair 5 'a'))))`

Is that starting to look familiar? This is an approximation of lists, though not exactly a pleasant one to work with.
Note that Pair a is a Functor. This idea of applying a Functor to itself is exactly fixpoints of data types. With the Pair a Functor, there is nothing to stop the self-application, so we have something isomorphic to an infinite [a].
Augmenting Pair to include a second, nullary, constructor, thus:

`data Pair a b = Stop | Pair a b`

We can then take the fixpoint of the Functor Pair a, which is isomorphic to:

`data PairFix a = Stop | Pair a (PairFix a)`

And that is clearly isomorphic to desugared lists:

`data List a = Empty | Cons a (List a)`

We can even abstract this notion of self-application of a Functor in a curious newtype:

`newtype Functor f => Mu f = In (f (Mu f))`

Such that PairFix a = Mu (Pair a), assuming we’ve declared an instance of Functor for Pair a.

Lists are actually only one example. Mu can be applied to any Functor. Using the binary tree implementation from a previous post ,

`data Tree a = Empty | Tree a (Tree a) (Tree a)`

This is the fixpoint of the following type:

`data Cell a b = Empty | Cell a b b`

Since Cell a forms a Functor, Mu (Cell a) == Tree a.

This is all merely an intriguing bit of type theory until we make the realization that all of these structures have something in common. That something is that they can all be recursed over. This can be seen most clearly by looking at lists, which as I demonstrated above are isomorphic to Mu (Pair a) using the second definition of Pair. The main thrust of Functional Programming with Bananas, Lenses, Envelopes and Barbed Wire is that recursion patterns (ie. higher-order functions) we generally think of as being for lists are in fact applicable to any fixpoint data structure.

The paper goes on to describe four core “foomorphisms”: catamorphisms, anamorphisms, hylomorphisms and paramorphisms. The names are from Greek, and like “monad” and “functor” are far scarier than the things they name. These are generalizations of foldr, unfoldr, primitive recursion and primitive recursion with an accumulating parameter, respectively.

I had originally planned to describe these in more detail here, but Edward Kmett (edwardk) has begun writing a Field Guide to all of these recursion schemes. His posts on each of the foomorphisms include both category theory and concrete examples, and are an excellent read. As you can see from the front page of his Field Guide, there are many, many more than the four described in the original paper. Implementations of all of these and much more are available in category-extras on Hackage.

I will still describe some of the core examples and definitions from the paper in later posts. In particular my explorations of the standard examples (Peano numbers, factorial, trees) from Edward Kmett’s Guide and elsewhere. My goal is to learn to recognize them well “in the wild”, since they aid understanding even when the function doesn’t explicitly use the foomorphism.

Advertisements

### 4 Responses to Fixed Point Datatypes

1. Derek Elkins says:

The notion of fixpoint in programming is the same as in “calculus” (or better algebra). If F is a functor, then G is it’s fixed point if F(G) ~ G where ~ is being used for isomorphic to. Just as in algebra or calculus, one way (that need not always work) at arriving at a fixed point is via iteration. Let’s take f(x) = x^2 as an example. The sequence a_0 = 1/2 and a_n = f(a_{n-1}) converges to 0, one of the fixed points of x^2. There is nothing inherent about self-application in the programming notion of fixed point and in fact none of your examples have any self-application. There is a definite relationship between recursion and fixed points, though fixed points aren’t inextricably linked to recursion. For example the constant functor for any constant or the identity functor both have fixed points as well as the functor PA=AxA (namely P1) and other examples.

2. bradenshep says:

Thanks for the comment.

I’m not sure how much of this is semantics and how much is content, but:

When I refer to self-application, I meant taking a type like Pair a b and using Pair a b again for b: Pair a (Pair a b). Perhaps self-application isn’t the right term for that. I think it mostly comes from when I was first learning about fixpoints, this “self-application” leap was what made it slip into focus for me. I couldn’t figure out how fix could take an arbitrary function and find a fixpoint of it while I was thinking about it from the f(x) = x viewpoint.

I do understand the point about a_n converging to 0, and thinking about the constant functor definitely improved my understanding.

3. web design says:

Type algebra is the hot new thing, it seems. What with this and all the zipper / type calculus hackery of late.

4. Today on a math IRC channel I saw the question

“what is sqrt(6+sqrt(6+… ?”

It is an instance of a rather popular kind of exercises in calculus.

Letting f(x) = sqrt(6 + x), the question asks

“what is f(f(… ?”

The solution is to first ensure that the sequence s0 = some starting point, s1 = f(s0), s2 = f(s1), … converges, then find out what it converges to, by solving x = sqrt(6+x), i.e., x = f(x).

I think it illustrates a strong analogy between calculus fixed points and programming fixed points, including the self-application part “what is f(f(… ?”