Catamorphism
In category theory, the concept of catamorphism denotes the unique homomorphism from an initial algebra into some other algebra.
In functional programming, catamorphisms provide generalizations of folds of lists to arbitrary algebraic data types, which can be described as initial algebras.
The dual concept is that of anamorphism that generalize unfolds. A hylomorphism is the composition of an anamorphism followed by a catamorphism.
Definition
Consider an initial F-algebra for some endofunctor F of some category into itself. Here in is a morphism from FA to A. Since it is initial, we know that whenever is another F-algebra, i.e. a morphism f from FX to X, there is a unique homomorphism h from to. By the definition of the category of F-algebras, this h corresponds to a morphism from A to X, conventionally also denoted h, such that. In the context of F-algebras, the uniquely specified morphism from the initial object is denoted by cata f and hence characterized by the following relationship:Terminology and history
Another notation found in the literature is. The open brackets used are known as banana brackets, after which catamorphisms are sometimes referred to as bananas, as mentioned in Erik Meijer et al. One of the first publications to introduce the notion of a catamorphism in the context of programming was the paper “Functional Programming with Bananas, Lenses, Envelopes and Barbed Wire”, by Erik Meijer et al., which was in the context of the Squiggol formalism.The general categorical definition was given by Grant Malcolm.
Examples
We give a series of examples, and then a more global approach to catamorphisms, in the Haskell programming language.Iteration
Iteration-step prescriptions lead to natural numbers as initial object.Consider the functor
fmaybe
mapping a data type b
to a data type fmaybe b
, which contains a copy of each term from b
as well as one additional term Nothing
. This can be encoded using one term and one function. So let an instance of a StepAlgebra also include a function from fmaybe b
to b
, which maps Nothing
to a fixed term nil
of b
, and where the actions on the copied terms will be called next
.type StepAlgebra b = -- the algebras, which we encode as pairs
data Nat = Zero | Succ Nat -- which is the initial algebra for the functor described above
foldSteps :: StepAlgebra b -> -- the catamorphisms map from Nat to b
foldSteps Zero = nil
foldSteps = next $ foldSteps nat
As a silly example, consider the algebra on strings encoded as
, for which Nothing
is mapped to "go!"
and otherwise "wait.. "
is prepended. As
denotes the number four in Nat
, the following will evaluate to "wait.. wait.. wait.. wait.. go!":
, which is passed to foldSteps
List fold
For a fixed typea
, consider the functor mapping types b
to the product type of those two types. We moreover also add a term Nil
to this resulting type. An f-algebra shall now map Nil
to some special term nil
of b
or "merge" a pair into a term of b
. This merging of a pair can be encoded as a function of type a -> b -> b
.type ContainerAlgebra a b = -- f-algebra encoded as
data List a = Nil | Cons a -- which turns out to be the initial algebra
foldrList :: ContainerAlgebra a b -> -- catamorphisms map from to b
foldrList Nil = nil
foldrList = merge x $ foldrList xs
As an example, consider the algebra on numbers types encoded as
, for which the number from a
acts on the number from b
by plain multiplication. Then the following will evaluate to 3.000.000: Tree fold
For a fixed typea
, consider the functor mapping types b
to a type that contains a copy of each term of a
as well as all pairs of b
's. An algebra consists of a function to b
, which either acts on an a
term or two b
terms. This merging of a pair can be encoded as two functions of type a -> b
resp. b -> b -> b
.type TreeAlgebra a b = -- the "two cases" function is encoded as
data Tree a = Leaf a | Branch -- which turns out to be the initial algebra
foldTree :: TreeAlgebra a b -> -- catamorphisms map from to b
foldTree = f x
foldTree = g
treeDepth :: TreeAlgebra a Integer -- an f-algebra to numbers, which works for any input type
treeDepth =
treeSum :: => TreeAlgebra a a -- an f-algebra, which works for any number type
treeSum =
General case
Deeper category theoretical studies of initial algebras reveal that the F-algebra obtained from applying the functor to its own initial algebra is isomorphic to it.Strong type systems enable us to abstractly specify the initial algebra of a functor
f
as its fixed point a = f a. The recursively defined catamorphisms can now be coded in single line, where the case analysis is encapsulated by the fmap. Since the domain of the latter are objects in the image of f
, the evaluation of the catamorphisms jumps back and forth between a
and f a
.type Algebra f a = f a -> a -- the generic f-algebras
newtype Fix f = Iso -- gives us the initial algebra for the functor f
cata :: Functor f => Algebra f a -> -- catamorphism from Fix f to a
cata alg = alg. fmap . invIso -- note that invIso and alg map in opposite directions
Now again the first example, but now via passing the Maybe functor to Fix. Repeated application of the Maybe functor generates a chain of types, which, however, can be united by the isomorphism from the fixed point theorem. We introduce the term
zero
, which arises from Maybes's Nothing
and identify a successor function with repeated application of the Just
. This way the natural numbers arise.type Nat = Fix Maybe
zero :: Nat
zero = Iso Nothing -- every 'Maybe a' has a term Nothing, and Iso maps it into a
successor :: Nat -> Nat
successor = Iso. Just -- Just maps a to 'Maybe a' and Iso maps back to a new term
pleaseWait :: Algebra Maybe String -- again the silly f-algebra example from above
pleaseWait = "wait.. " ++ string
pleaseWait Nothing = "go!"
Again, the following will evaluate to "wait.. wait.. wait.. wait.. go!":
cata pleaseWait
And now again the tree example. For this we must provide the tree container data type so that we can set up the
fmap
.data Tcon a b = TconL a | TconR b b
instance Functor where
fmap f = TconL x
fmap f = TconR
type Tree a = Fix -- the initial algebra
end :: a -> Tree a
end = Iso. TconL
meet :: Tree a -> Tree a -> Tree a
meet l r = Iso $ TconR l r
treeDepth :: Algebra Integer -- again, the treeDepth f-algebra example
treeDepth = 1
treeDepth = 1 + max y z
The following will evaluate to 4:
cata treeDepth $ meet ) )