I am trying to solve some of the exercises in Bird's Algebra of Programming. He defines an initial algebra as the initial object in the category of some F-algebra.
In the book, he says that
data Nat = Zero | Succ Nat
is the initial algebra of the Maybe
Functor, i.e, the functor F, where $FA = 1 + A$ and $Ff = id_1 + f$.
Then he asks the following question as an exercise:
What is the initial algebra of the Identity Functor?
I have two guesses for this:
- My first guess, going by the pattern indicated for Nat, would be some bizzare object like this:
data X = Tag X
. But in this case, I cannot figure out what the homomorphism fromX
to an arbitraryA
be. - My second guess is that the initial object is some kind of tagged union of all the types and the homomorphism is an appropriate projection.
Any help is appreciated
NOTE: copy-paste the sections in this answer to play with inside
ghci
Note that the initial object can be represented in Haskell as as an inhabitant of the type
Mu
(as shown below).I now exhibit the ismorphism between
Nat
andMu Maybe
below:Next, I show that the initial object generated by
Id
is isomorphic toVoid
--- that is, it is uninhabited.Intuitively, the initial object contains "arbitrary many nestings" of the original functor
F
. In theMaybe
case, we had the ability to useNothing
to terminate this nesting. However, in the case ofId
, this is no longer the case: there is no "branch" in the typeId
to break this arbitrary recursion. Hence, it cannot be inhabited by any inductive value.Void
is a way to declare values in haskell that cannot be inhabited, in case you have not seen it before.Void
has no data constructors.Claim:
isoFwd : Mu Id -> Void
proves thatMu Id
is not inhabitedAssume for contradiction that
Mu Id
is inhabited. Hence, there exists anx : Mu Id
. Now, computey = (isoFwd x) : Void
. But we know thatVoid
is uninhabited. Hence, contradiction. Therefore,Mu Id
must be uninhabited.Claim:
isoFwd, isoBwd
define an isomorphism betweenVoid
andMu Id
To discuss equality of functions, we define: \begin{align*} \forall f, g : X \rightarrow Y, \quad f ~\texttt{==}~g \equiv \forall x \in X, f (x) = g(x) \end{align*}
Note that if $X$ is uninhabited, then the relation ${f~\texttt{==}~g}$ is vacuously true. Armed with this, we show that
isoFwd. isoBwd = id = isoBwd . isoFwd
.isoFwd . isoBwd :: Void -> Void == id :: Void -> Void
sinceVoid
is uninhabited, and hence the two LHS and RHS are vacuously equal.isoBwd . isoFwd :: Mu Id -> Mu Id == id :: Mu Id -> Mu Id
sinceMu Id
is uninhabited, and hence the LHS and RHS are vacuously equal.Further reading
For more of this kind of computational perspective, I can recommend reading about recursion schemes and cata/anamorphisms, from Bananas, Lenses, and Barbed Wire as well as the
recursion-schemes
library.