7

The well-known Church encoding of natural numbers can be generalized to use an arbitrary (covariant) functor F. The result is the type, call it C, defined by

  data C = Cfix { run :: forall r. (F r -> r) -> r }

Here and below, for simplicity, we will assume that F is a fixed, already defined functor.

It is widely known and stated that the type C is a fixpoint of the functor F, and also that C is an initial F-algebra. For example, if the functor F a is defined by

 data F a b = Empty | Cons a b

then a fixpoint of F a is [a] (the list of values of type a). Also, [a] is the initial algebra. The Church encoding of lists is well known. But I could not find a rigorous proof of either of these statements (C is a fixpoint, and C is the initial algebra).

The question is, how to prove rigorously one of the two statements:

  1. The type C is a fixpoint of the type isomorphism F C ≅ C. In other words, we need to prove that there exist two functions, fix :: F C -> C and unfix :: C -> F C such that fix . unfix = id and unfix . fix = id.
  2. The type C is the initial algebra of the functor F; that is, the initial object in the category of F-algebras. In other words, for any type A such that a function p :: F A -> A is given (that is, A is an F-algebra), we can find a unique function q :: C -> A which is an F-algebra morphism. This means, q must be such that the law q . fix = p . fmap q holds. We need to prove that, given A and p, such q exists and is unique.

These two statements are not equivalent; but proving (2) implies (1). (Lambek's theorem says that an initial algebra is an isomorphism.)

The code of the functions fix and unfix can be written relatively easily:

 fix :: F C -> C
 fix fc  = Cfix (forall r. \g -> g . fmap (\h -> h g) fc )
 unfix :: C -> F C
 unfix c = (run c) (fmap fix)

Given a function p :: F A -> A, the code of the function q is written as

 q :: C -> A
 q c = (run c) p

However, it seems difficult to prove directly that the functions fix, unfix, q satisfy the required properties. I was not able to find a complete proof.

Is it easier to prove that C is an initial algebra, i.e., that q is unique, than to prove that fix . unfix = id?

In the rest of this question, I will show some steps that I was able to make towards the proof that fix . unfix = id.

It is not possible to prove either (1) or (2) simply by using the given code of the functions. We need additional assumptions. Similarly to the Yoneda identity,

 forall r. (A -> r) -> F r   ≅   F A   ,

we need to assume that the functions' code is fully parametric (no side effects, no specially chosen values or fixed types) so that the parametricity theorem can be applied. So, we need to assume that the type C contains only functions of type forall r. (F r -> r) -> r that satisfy the appropriate naturality law (known as "free theorem" after P. Wadler's paper "Theorems for free").

The parametricity theorem gives the following naturality law ("free theorem") for this type signature:

For any types A and B, and for any functions p :: F B -> A and f :: A -> B, the function c :: forall r. (F r -> r) -> r must satisfy the equation

  c (f . p) = f . c (p . fmap f)

Using this naturality law with appropriately chosen p and f, one can show that the composition fix . unfix is a certain function of type C -> C that must be equal to \c -> (run c) fix.

However, further progress in the proof does not seem to be possible; it is not clear why this function must be equal to id.

Let us temporarily define the function m:

 m :: (F C -> C) -> C -> C
 m t c = (run c) t

Then the result I have is written as

fix . unfix  = m fix

One can also show that unfix . fix = fmap (m fix).

It remains to prove that m fix = id. Once that is proved, we will have proved that F C ≅ C.

The same naturality law of c with different choice of p and f gives the strange identity

 m fix . m (m fix . fix) = m (m fix . fix)

But I do not know how to derive from this identity that m fix = id.

winitzki
  • 291
  • 1
  • 8

1 Answers1

7

$\newcommand{\fix}{\mathsf{fix}}$ $\newcommand{\fold}{\mathsf{fold}}$ $\newcommand{\map}{\mathsf{map}}$

Here is, I believe, how one would use parametricity to prove your last lemma. I'm going to rework some stuff slightly for my own understanding. We have: $$C = ∀ r. (F r → r) → r$$ with $F$ functorial. We have: $$\fix : F C → C$$ corresponding to your definition, and I'm going to call a generalization of your m: $$\fold : (F r → r) → C → r \\ \fold\ α\ c = c\ α$$

So, we want to prove that for all $c : C$, $\fold\ \fix\ c = c$

Parametricity looks like this:

$$∀(R : a \Leftrightarrow b).\\ ∀ (α : F a → a) (β : F b → b).\\ (∀ x y. FR(x,y) → R (α\ x, β\ y))\\ → ∀ c. R (c\ α, c\ β)$$

To unpack this a bit, if we have a types $a,b$, a relation $R$ on them, algebra structures $α,β$, and a proof that they preserve the relation, then $R$ relates $c \ α$ to $c \ β$. The idea behind the "preservation" criterion is that $FR(x, y)$ holds if $x$ and $y$ have the same $F$ 'shape', and corresponding occurrences of $a$ and $b$ values are related by $R$.

So, let's make some choices. Suppose we have $ζ : F z → z$. Then: $$a \equiv C \\ b \equiv z \\ R(c, z) \equiv c\ ζ = z \\ α \equiv \fix \\ β \equiv ζ$$ the result of parametricity for these choices will be: $$R(c\ \fix, c\ ζ) \equiv c\ \fix\ ζ = c\ ζ$$

Then by function extensionality we will obtain $c = c\ \fix = \fold\ \fix\ c$. Our obligation is to prove:

$$∀ fc\ fz. FR(fc,fz) → R (\fix\ fc,ζ\ fz)$$

The goal is $$\fix\ fc\ ζ = ζ\ fz$$ By unfolding the definition of $\fix$ we know: $$\fix\ fc\ ζ = ζ (\map_F\ (\fold\ ζ)\ fc)$$

However, the meaning of $FR(fc,fz)$ is actually that $$\map_F\ (\fold\ ζ)\ fc = fz$$ So the result is immediate.

I haven't thought about whether it's possible to make due with dinaturality. I think it is not, but I could be wrong.

Edit:

The free theorem for this scenario is:

$$(∀ x. f (α\ x) = β (\map_F\ f \ x)) \Rightarrow f (c\ α) = c\ β$$

If we choose $$f = \fold\ ζ \\ α = \fix \\ β = ζ$$ then our obligation is:

$$\fold\ ζ\ (\fix\ x) = \fix\ x\ ζ = ζ (\map_F\ (\fold\ ζ)\ x)$$

which is just the definition of $\fix$. The result is:

$$\fold\ ζ\ (c\ \fix) = c\ ζ$$

which again gives us what we wanted. Note that this is also not the same as dinaturality, though. I believe the difference is that dinaturality allows us to shift $f$ around in the expression, but parametricity allows us to absorb it into one of the algebras.

Dan Doel
  • 2,967
  • 1
  • 10
  • 15