Catamorphism

In functional programming, the concept of catamorphism (from the Ancient Greek: "downwards" and  "form, shape") denotes the unique homomorphism from an initial algebra into some other algebra.

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 $$(A, in)$$ 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 $$(X, f)$$ is another $$F$$-algebra, i.e. a morphism $$f$$ from $$FX$$ to $$X$$, there is a unique homomorphism $$h$$ from $$(A, in)$$ to $$(X, f)$$. By the definition of the category of $$F$$-algebra, this $$h$$ corresponds to a morphism from $$A$$ to $$X$$, conventionally also denoted $$h$$, such that $$h \circ in = f \circ Fh$$. In the context of $$F$$-algebra, the uniquely specified morphism from the initial object is denoted by $$\mathrm{cata}\ f$$ and hence characterized by the following relationship:


 * $$h = \mathrm{cata}\ f$$
 * $$h \circ in = f \circ Fh$$

Terminology and history
Another notation found in the literature is $$(\!|f|\!)$$. 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.

Catamorphism for Maybe-algebra
Consider the functor  defined in the below Haskell code:

The initial object of the Maybe-Algebra is the set of all objects of natural number type  together with the morphism   defined below: The  map can be defined as follows: As an example consider the following morphism: Then  will evaluate to "wait... wait... wait... go!".

List fold
For a fixed type  consider the functor   defined by the following: The initial algebra of  is given by the lists of elements with type   together with the morphism   defined below: The  map can be defined by: Notice also that. As an example consider the following morphism: evaluates to 30. This can be seen by expanding .

In the same way it can be shown, that will evaluate to 10*(100*(1000*3))=3.000.000.

The  map is closely related to the right fold (see Fold (higher-order function)) of lists. The morphism  defined by relates   to the right fold   of lists via: The definition of  implies, that   is the right fold and not the left fold. As an example:  will evaluate to 1111 and   to 3.000.000.

Tree fold
For a fixed type, consider the functor mapping types   to a type that contains a copy of each term of   as well as all pairs of  's (terms of the product type of two instances of the type  ). An algebra consists of a function to, which either acts on an   term or two   terms. This merging of a pair can be encoded as two functions of type  resp. .

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  as its fixed point a = f a. The recursively defined catamorphisms can now be coded in single line, where the case analysis (like in the different examples above) is encapsulated by the fmap. Since the domain of the latter are objects in the image of, the evaluation of the catamorphisms jumps back and forth between   and.

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, which arises from Maybe's   and identify a successor function with repeated application of the. This way the natural numbers arise.

Again, the following will evaluate to "wait.. wait.. wait.. wait.. go!":

And now again the tree example. For this we must provide the tree container data type so that we can set up the  (we didn't have to do it for the   functor, as it's part of the standard prelude).

The following will evaluate to 4: