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:
- The type
C
is a fixpoint of the type isomorphismF C ≅ C
. In other words, we need to prove that there exist two functions,fix :: F C -> C
andunfix :: C -> F C
such thatfix . unfix = id
andunfix . fix = id
. - The type
C
is the initial algebra of the functorF
; that is, the initial object in the category ofF
-algebras. In other words, for any typeA
such that a functionp :: F A -> A
is given (that is,A
is anF
-algebra), we can find a unique functionq :: C -> A
which is an F-algebra morphism. This means,q
must be such that the lawq . fix = p . fmap q
holds. We need to prove that, givenA
andp
, suchq
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
.
c (f . p) = f . c (p . fmap f)
is already the most general result that one would get from the relational law. (It is the dinaturality law for the dinatural transformationc
.) $\endgroup$F
. For negativeF
initial algebras provably don't exist. $\endgroup$