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 fromXto an arbitraryAbe. - 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
ghciNote 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
NatandMu Maybebelow:Next, I show that the initial object generated by
Idis isomorphic toVoid--- that is, it is uninhabited.Intuitively, the initial object contains "arbitrary many nestings" of the original functor
F. In theMaybecase, we had the ability to useNothingto terminate this nesting. However, in the case ofId, this is no longer the case: there is no "branch" in the typeIdto break this arbitrary recursion. Hence, it cannot be inhabited by any inductive value.Voidis a way to declare values in haskell that cannot be inhabited, in case you have not seen it before.Voidhas no data constructors.Claim:
isoFwd : Mu Id -> Voidproves thatMu Idis not inhabitedAssume for contradiction that
Mu Idis inhabited. Hence, there exists anx : Mu Id. Now, computey = (isoFwd x) : Void. But we know thatVoidis uninhabited. Hence, contradiction. Therefore,Mu Idmust be uninhabited.Claim:
isoFwd, isoBwddefine an isomorphism betweenVoidandMu IdTo 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 -> VoidsinceVoidis uninhabited, and hence the two LHS and RHS are vacuously equal.isoBwd . isoFwd :: Mu Id -> Mu Id == id :: Mu Id -> Mu IdsinceMu Idis 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-schemeslibrary.