As far as I understand, recursive data types from Haskell correspond to initial algebras of endofunctors from the Hask
category [1, 2]. For example:
- Natural numbers,
data Nat = Zero | Succ Nat
, correspond to the initial algebra of the endofunctorF(-) = 1 + (-)
. - Lists,
data List a = Nil | Cons a (List a)
, correspond to the initial algebra of the endofunctorF(A, -) = 1 + A × (-)
.
However, it's not clear to me what the endofunctor corresponding to the rose trees should be:
data Rose a = Node a (List (Rose a))
What confuses me is that there are two recursions: one for the rose tree and the other for the list. According to my calculations, I would get the following functor, but it doesn't seem right:
F(A, •, -) = A × (1 + (-) × (•))
Alternatively, rose trees can be defined as mutually recursive data types:
data Rose a = Node a (Forest a)
type Forest a = List (Rose a)
Do mutually recursive data types have an interpretation in category theory?
As you noticed, the definition of the functor for
Rose a
is trickier due to the fact that the recursive occurrence of the type is fed into aList
. The problem is thatList
is itself a recursive type obtained as a fixed point.List (Rose a)
basically corresponds to an "arbitrary number of elements ofRose a
", something that you cannot express with a signature of products and sums alone, hence the need for additional abstraction over these multiple recursive points.A functor
F A - : * -> *
will not work, as we would need to find something such thatOne way to do it is to just treat
List
as primitive. ThenRose a
is just the fixed point ofAnother, more interesting way is to follow the suggestion in the reference you posted, and notice that the type of
Rose a
can be generalized to abstract over the functor in which the recursive occurrence is fed intonow
GRose
has type(* -> *) -> (* -> *)
, hence it is an higher order functor mapping an endofunctor into another one. In our example, it would map the functorList
into the type of rose trees.Notice however that GRose is still recursive, so the above is actually stating an isomorphism rather than a solution to our problem. We can try to fix (wink wink) this by additionally abstracting over the recursive point
Notice that now
HRose
is a regular higher-order functor of type((* -> *) -> (* -> *)) -> (* -> *) -> (* -> *)
, hence it maps higher-order functors into higher-order functors. Computing the least fixed point ofHRose
gives usSo if we put
Rose ≡ μ(HRose) List
, we getwhich is exactly the defining equation for rose trees. You can find many further examples of the theory and practice of generic programming using fixed points over higher-order functors. Here, for example, Bird and Paterson develop it in the context of nested datatypes (but the definitions clearly hold in general). They also show the systematic construction of folds over datatypes defined in such way, as well as various laws.
You seem to understand how this is modelled
by taking, for any given
A
, the initial algebra of the endofunctorF(A, -) = 1 + A × (-)
. Let's call this initial algebraL(A)
.If we forget the morphism in
L(A)
, we can sat thatL(A)
is an object of our category. Better,L(-)
is not only a mapping from objects to objects, but can be seen as an endofunctor.Once
L
is seed as an endofunctor, the recursive typeis interpreted by taking, for any
A
m the initial algebra of the functorwhich is a functor obtained by composing
L
and*
(and the diagonal functor). Hence, the same approach works.I would discourage talk of "the Hask Category" because it subconsciously conditions you against looking for other categorical structure in Haskell programming.
Indeed, rose trees can be seen as the fixpoint of an endofunctor on types-and-functions, a category which we might be better to call
Type
, now thatType
is the type of types. If we give ourselves some of the usual functor kit......and fixpoints...
...then we may take
The fact that
[]
is itself recursive does not prevent its use in the formation of recursive datatypes viaFix
. To spell out the recursion explicitly, we have nested fixpoints, here with bound variable names chosen suggestively:Now, by the time we've arrived in the second fixpoint, we have a type formula
which is functorial (indeed, strictly positive) in both
rose
andlist
. One might say it is aBifunctor
, but that's unnecessary terminology: it's a functor from(Type, Type)
toType
. You can make aType -> Type
functor by taking a fixpoint in the second component of the pair, and that's just what happened above.The above definition of
Rose
loses an important property. It is not true thatmerely that
Rose x :: Type
ifx :: Type
. In particular,is not a well typed constraint, which is a pity, as intuitively, rose trees ought to be functorial in the elements they store.
You can fix this by building
Rose
as itself being the fixpoint of aBifunctor
. So, in effect, by the time we get to lists, we have three type variables in scope,a
,rose
andlist
, and we have functoriality in all of them. You need a different fixpoint type constructor, and a different kit for buildingBifunctor
instances: forRose
, life gets easier because thea
parameter is not used in the inner fixpoint, but in general, to define bifunctors as fixpoints requires trifunctors, and off we go!This answer of mine shows how to fight the proliferation by showing how indexed types are closed under a fixpoint-of-functor construction. That's to say, work not in
Type
but ini -> Type
(for the full variety of index typesi
) and you're ready for mutual recursion, GADTs, and so on.So, zooming out, rose trees are given by mutual fixpoints, which have a perfectly sensible categorical account, provided you see which categories are actually at work.
This is not really an answer to the question you're asking, but perhaps interesting anyway. Note that with
and the fact that
*
distributes over+
, you have(the equality really denotes isomorphism). So you might as well have defined
or in Haskell,
Which is to say, rose trees are isomorphic to ordinary (leaf-labelled) binary trees, and which clearly form a normal initial algebra.