Catamorphism

Catamorphism

The concept of a catamorphism is grounded in category theory, and has been applied to functional programming. It denotes the unique homomorphism for an initial algebra. The term comes from Greek Polytonic|κατα- (downwards, according to) + morphism, the latter from Greek Polytonic|μορφή (form, shape). The dual concept is that of anamorphism.

Catamorphisms in functional programming

In functional programming, a catamorphism is a generalization of the "folds" on lists known from functional programming to arbitrary abstract data types that can be described as initial algebras.

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." [http://citeseer.ist.psu.edu/meijer91functional.html] , which was in the context of the Squiggol formalism.

The dual concept is of anamorphisms, a generalisation of "unfolds".

Example

The following example is in Haskell-like pseudocode, assuming a class Group of types supporting an "addition" operation.

data Tree "a" = Leaf "a" " " | Branch (Tree "a") (Tree "a") type TreeAlgebra "a" "r" = ("a" -> "r", "r" -> "r" -> "r") foldTree ∷ TreeAlgebra "a" "r" -> Tree "a" -> "r" foldTree ("f", "g") (Leaf "x") " " = "f" "x" foldTree ("f", "g") (Branch "l" "r") = "g" (foldTree ("f", "g") "l") (foldTree ("f", "g") r) treeDepth ∷ TreeAlgebra "a" Integer treeDepth = ( λ"x". 1, λ"l" "r". 1 + max "l" "r" ) sumTree ∷ (Group "a") => TreeAlgebra "a" "a" sumTree = ( λ"x". "x", λ"l" "r". "l"+"r" )

Here foldTree ("f", "g") is a "catamorphism" for the Tree "a" datatype; treeDepth and sumTree are called "algebras".

Catamorphisms in category theory

Category theory provides the necessary concepts to give a generic definition that accounts for all initial data types (using an identification of functions in functional programming with the morphisms of the category Set or some related concrete category). This was done by Grant Malcolm in his Ph.D. Thesis "Algebraic Types and Program Transformation" (Univ. Groningen, 1990) [http://citeseer.ist.psu.edu/context/881598/0] and the article "Data structures and program transformation" [http://citeseer.ist.psu.edu/context/25271/0] .

Specialized to the above example, the formal definition of a catamorphism is as follows: For fixed type a, the pair (r, [f1,f2] ) is an "F"-algebra, where "F" is the functor sending r to a + r × r.The pair (Tree a, [Leaf,Branch] ) is also an "F"-algebra, and specifically the initial "F"-algebra.This means that there is a unique homomorphism to any other "F"-algebra. That unique homomorphism is called a "catamorphism".

In the code example above, foldTree is the function that constructs these catamorphisms.

The general case

If ("A", "in") is the initial "F"-algebra for some endofunctor "F" (so "in" is a morphism from "FA" to "A"),and ("X", "f") is an "F"-algebra, there is a unique homomorphism from ("A", "in") to ("X", "f"), which may be denoted cata "f" (leaving the "carrier" "X" implicit). (Here "cata" is a generic name for what was called "foldTree" for the specific case from the example.)

Properties

Let "F" be given and fixed. Using the definition of homomorphism, the uniqueness property can be expressed as: for all "F"-algebras ("X", "f"), and all "h" from "A" to "X", the following two statements are equivalent:

*h = mathrm{cata} f
*h circ in = f circ F h

Notation

Another notation for cata "f" found in the literature is (!|f|!). The open brackets used are known as banana brackets, after which catamorphisms are sometimes referred to as "bananas".

See also

*Hylomorphism
*Paramorphism
*Apomorphism

External links

* Erik Meijer, Maarten Fokkinga, and Ross Paterson. "Functional Programming with Bananas, Lenses, Envelopes, and Barbed Wire" [http://research.microsoft.com/~emeijer/Papers/fpca91.pdf] , contains additional definitions and examples
* [http://caos.di.uminho.pt/~ulisses/blog/2007/12/19/catamorphisms-in-haskell/ Catamorphisms in Haskell]


Wikimedia Foundation. 2010.

Игры ⚽ Нужно решить контрольную?

Look at other dictionaries:

  • catamorphism — noun a generalization of the folds on lists known from functional programming to arbitrary abstract data types that can be described as initial algebras …   Wiktionary

  • catamorphism — variant of katamorphism …   Useful english dictionary

  • Hylomorphism (computer science) — In computer science, and in particular functional programming, a hylomorphism is a recursive function, corresponding to the composition of an anamorphism (which first builds a set of results; also known as unfolding ) and a catamorphism (which… …   Wikipedia

  • Catamorphisme — ██████████40  …   Wikipédia en Français

  • Fold (higher-order function) — In functional programming, fold, also known variously as reduce, accumulate, compress or inject, is a family of higher order functions that process a data structure in some order and build up a return value. Typically, a fold deals with two… …   Wikipedia

  • Anamorphism — is a concept from functional programming grounded in category theory. The term comes from Greek Polytonic|ανα (upwards) + morphism (from Greek Polytonic|μορφή, or form, shape).Anamorphisms in functional programmingIn functional programming, an… …   Wikipedia

  • Paramorphism — A paramorphism (from Greek παρα , meaning close together ) is an extension of the concept of catamorphism to deal with a form which “eats its argument and keeps it too” [Phil Wadler. Views: A way for pattern matching to cohabit with data… …   Wikipedia

  • Functional programming — In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast with the… …   Wikipedia

  • Banana (disambiguation) — A banana is tropical treelike plant and the elongated curved fruit it bears.Banana may also refer to: * BANANA, an acronym for Build Absolutely Nothing Anywhere Near Anything * Banana (video game), a video game for the Nintendo Entertainment… …   Wikipedia

  • Charity (programming language) — Charity Paradigm(s) pure functional Appeared in 1992[1] Developer The Charity Development Group Preview release 1.99.1 (beta) …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”