ArticleslgStudy

mathematics

Catamorphism

Catamorphism is a mathematics topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Catamorphism rather than just read about it. In short: 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.

Key takeaways

  • Catamorphism belongs to mathematics; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Catamorphism to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Catamorphism from memory before moving on to harder problems.

Reference excerpt

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∘in=f∘Fh. In the context of F-algebra, the uniquely specified morphism from the initial object is denoted by cata f and hence characterized by the following relationship:

h=cata f h∘in=f∘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 Maybe defined in the below Haskell code:

The initial object of the Maybe-Algebra is the set of all objects of natural number type Nat together with the morphism ini defined below:

The cata map can be defined as follows:

As an example consider the following morphism:

Then cata g ((Succ. Succ . Succ) Zero) will evaluate to "wait... wait... wait... go!".

List fold For a fixed type a consider the functor MaybeProd a defined by the following:

The initial algebra of MaybeProd a is given by the lists of elements with type a together with the morphism ini defined below:

The cata map can be defined by:

Notice also that cata g (Cons s l) = g (Just (s, cata g l)). As an example consider the following morphism:

cata g (Cons 10 EmptyList) evaluates to 30. This can be seen by expanding cata g (Cons 10 EmptyList) = g (Just (10,cata g EmptyList)) = 10*(cata g EmptyList) = 10*(g Nothing) = 10*3. In the same way it can be shown, that cata g (Cons 10 (Cons 100 (Cons 1000 EmptyList))) will evaluate to 10*(100*(1000*3)) = 3.000.000. The cata map is closely related to the right fold (see Fold (higher-order function)) of lists foldrList. The morphism lift defined by

relates cata to the right fold foldrList of lists via:

The definition of cata implies, that foldrList is the right fold and not the left fold. As an example: foldrList (+) 1 (Cons 10 (Cons 100 (Cons 1000 EmptyList))) will evaluate to 1111 and foldrList (*) 3 (Cons 10 (Cons 100 (Cons 1000 EmptyList)) to 3.000.000.

Tree fold For a fixed type a, 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 (terms of the product type of two instances of the type b). 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.

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 (like in the different examples above) 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.

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

Again, the following will evaluate to "wait.. wait.. wait.. wait.. go!": cata pleaseWait (successor.successor.successor.successor $ zero) And now again the tree example. For this we must provide the tree container data type so that we can set up the fmap (we didn't have to do it for the Maybe functor, as it's part of the standard prelude).

The following will evaluate to 4: cata treeDepth $ meet (end "X") (meet (meet (end "YXX") (end "YXY")) (end "YY"))

See also Morphism Morphisms of F-algebras From a coalgebra to a final coalgebra: Anamorphism An anamorphism followed by an catamorphism: Hylomorphism Extension of the idea of catamorphisms: Paramorphism Extension of the idea of anamorphisms: Apomorphism

References

Further reading

External links Catamorphisms at HaskellWiki Catamorphisms by Edward Kmett Catamorphisms in F# (Part 1, 2, 3, 4, 5, 6, 7) by Brian McNamara Catamorphisms in Haskell

Worked examples

Example 1 — a first encounter with Catamorphism

Start with the simplest possible case. Write down what Catamorphism claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In mathematics, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Catamorphism before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Catamorphism ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Catamorphism

In research
Catamorphism appears in mathematics research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Catamorphism in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Catamorphism is common in secondary-school and first-year university syllabi. It links to neighbouring topics Category theory, Functional programming, Iteration in programming, so understanding it makes those chapters shorter.
In everyday life
Look for Catamorphism outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Catamorphism” →

How to study Catamorphism in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Catamorphism means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Catamorphism out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Catamorphism in simple terms?

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…

Why does Catamorphism matter?

Because it connects several mathematics ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Catamorphism?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Catamorphism.

Tags

  • Category theory
  • Functional programming
  • Iteration in programming
  • Morphisms
  • Recursion schemes

Keep exploring