Catamorphism
Encyclopedia
In category theory
Category theory
Category theory is an area of study in mathematics that examines in an abstract way the properties of particular mathematical concepts, by formalising them as collections of objects and arrows , where these collections satisfy certain basic conditions...

, the concept of catamorphism (from Greek
Greek language
Greek is an independent branch of the Indo-European family of languages. Native to the southern Balkans, it has the longest documented history of any Indo-European language, spanning 34 centuries of written records. Its writing system has been the Greek alphabet for the majority of its history;...

: κατά = downwards or according to; μορφή = form or shape) denotes the unique homomorphism
Homomorphism
In abstract algebra, a homomorphism is a structure-preserving map between two algebraic structures . The word homomorphism comes from the Greek language: ὁμός meaning "same" and μορφή meaning "shape".- Definition :The definition of homomorphism depends on the type of algebraic structure under...

 from an initial algebra
Initial algebra
In mathematics, an initial algebra is an initial object in the category of F-algebras for a given endofunctor F. The initiality provides a general framework for induction and recursion....

 into some other algebra. The concept has been applied to functional programming
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 to the imperative programming style, which emphasizes changes in state...

 as folds
Fold (higher-order function)
In functional programming, fold – also known variously as reduce, accumulate, compress, or inject – are a family of higher-order functions that analyze a recursive data structure and recombine through use of a given combining operation the results of recursively processing its...

.

The dual concept is that of anamorphism
Anamorphism
Anamorphosis is a distorted projection or perspective requiring the viewer to use special devices or occupy a specific vantage point to reconstitute the image...

. See also Hylomorphism (computer science)
Hylomorphism (computer science)
In computer science, and in particular functional programming, a hylomorphism is a recursive function, corresponding to the composition of an anamorphism and a catamorphism...


Catamorphisms in functional programming

In functional programming, a catamorphism is a generalization of the folds
Fold (higher-order function)
In functional programming, fold – also known variously as reduce, accumulate, compress, or inject – are a family of higher-order functions that analyze a recursive data structure and recombine through use of a given combining operation the results of recursively processing its...

on lists known from functional programming
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 to the imperative programming style, which emphasizes changes in state...

 to arbitrary algebraic data type
Algebraic data type
In computer programming, particularly functional programming and type theory, an algebraic data type is a datatype each of whose values is data from other datatypes wrapped in one of the constructors of the datatype. Any wrapped datum is an argument to the constructor...

s that can be described as initial algebra
Initial algebra
In mathematics, an initial algebra is an initial object in the category of F-algebras for a given endofunctor F. The initiality provides a general framework for induction and recursion....

s.

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
Erik Meijer (computer scientist)
Erik Meijer is a Dutch computer scientist who is currently a software architect for Microsoft SQL Server, Visual Studio and the .NET Framework. At Microsoft he heads the Cloud Programmability Team....

 et al.http://citeseer.ist.psu.edu/meijer91functional.html, which was in the context of the Squiggol formalism.

The dual concept is that of anamorphism
Anamorphism
Anamorphosis is a distorted projection or perspective requiring the viewer to use special devices or occupy a specific vantage point to reconstitute the image...

s, a generalization of unfolds.

Example

The following example is in Haskell
Haskell (programming language)
Haskell is a standardized, general-purpose purely functional programming language, with non-strict semantics and strong static typing. It is named after logician Haskell Curry. In Haskell, "a function is a first-class citizen" of the programming language. As a functional programming language, the...

:


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 = (const 1, \l r -> 1 + max l r)

sumTree :: (Num a) => TreeAlgebra a a
sumTree = (id, (+))


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
Category (mathematics)
In mathematics, a category is an algebraic structure that comprises "objects" that are linked by "arrows". A category has two basic properties: the ability to compose the arrows associatively and the existence of an identity arrow for each object. A simple example is the category of sets, whose...

 Set or some related concrete category). This was done by Grant Malcolm.

Returning to the above example, consider a functor
Functor
In category theory, a branch of mathematics, a functor is a special type of mapping between categories. Functors can be thought of as homomorphisms between categories, or morphisms when in the category of small categories....

 F sending r to a + r × r.
An F-algebra
F-algebra
In mathematics, specifically in category theory, an F-algebra is a structure defined according to a functor F. F-algebras can be used to represent data structures used in programming, such as lists and trees...

 for this specific case is a pair (r, [f1,f2]), where r is an object, and two morphisms, f1 and f2 are defined as f1: a → r, and f2: r × r → r.

In the category of F-algebras over such F, an initial algebra, if it exists, represents a Tree, or, in Haskell terms, it is (Tree a, [Leaf, Branch]).

Tree a being an initial object in the category of F-algebras, there is a unique homomorphism of F-algebras from Tree a to any given F-algebra. This unique homomorphism is called catamorphism.

The general case

In category theory
Category theory
Category theory is an area of study in mathematics that examines in an abstract way the properties of particular mathematical concepts, by formalising them as collections of objects and arrows , where these collections satisfy certain basic conditions...

, catamorphisms are the categorical dual of anamorphism
Anamorphism
Anamorphosis is a distorted projection or perspective requiring the viewer to use special devices or occupy a specific vantage point to reconstitute the image...

s (and anamorphisms are the categorical dual of catamorphisms).

That means the following.
Suppose (A, in) is an initial
Initial algebra
In mathematics, an initial algebra is an initial object in the category of F-algebras for a given endofunctor F. The initiality provides a general framework for induction and recursion....

 F-algebra
F-algebra
In mathematics, specifically in category theory, an F-algebra is a structure defined according to a functor F. F-algebras can be used to represent data structures used in programming, such as lists and trees...

 for some endofunctor F of some category
Category (mathematics)
In mathematics, a category is an algebraic structure that comprises "objects" that are linked by "arrows". A category has two basic properties: the ability to compose the arrows associatively and the existence of an identity arrow for each object. A simple example is the category of sets, whose...

 into itself.
Thus, in is a morphism
Morphism
In mathematics, a morphism is an abstraction derived from structure-preserving mappings between two mathematical structures. The notion of morphism recurs in much of contemporary mathematics...

 from FA to A, and since it is assumed to be initial we know that whenever (X, f) is another F-algebra (a morphism f from FX to X), there will be a unique homomorphism
Homomorphism
In abstract algebra, a homomorphism is a structure-preserving map between two algebraic structures . The word homomorphism comes from the Greek language: ὁμός meaning "same" and μορφή meaning "shape".- Definition :The definition of homomorphism depends on the type of algebraic structure under...

 h from (A, in) to (X, f), that is a morphism h from A to X such that h . in = f . Fh.
Then for each such f we denote by cata f that uniquely specified morphism h.

In other words, we have the following defining relationship, given some fixed F, A, and in as above:

Notation

Another notation for cata f 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 .

See also

  • Anamorphism
    Anamorphism
    Anamorphosis is a distorted projection or perspective requiring the viewer to use special devices or occupy a specific vantage point to reconstitute the image...

  • Apomorphism
    Apomorphism
    An apomorphism is the categorical dual of a paramorphism and an extension of the concept of anamorphism...

  • Hylomorphism
    Hylomorphism (computer science)
    In computer science, and in particular functional programming, a hylomorphism is a recursive function, corresponding to the composition of an anamorphism and a catamorphism...

  • Paramorphism
    Paramorphism
    A paramorphism is an extension of the concept of catamorphism to deal with a form which “eats its argument and keeps it too”, as exemplified by the factorial function. Its categorical dual is the apomorphism...


External links


Detailed example

Brian McNamara, a developer on the F# team, has created a series of blog posts that describe catamorphisms and their synergy with tail recursion, continuations and monads. He explores how the concept of catamorphism interacts with other functional tools.
  • Catamorphisms, part one (Initial example)
  • Catamorphisms, part two (Generalization for arbitrary data structures, example: processing binary tree
    Binary tree
    In computer science, a binary tree is a tree data structure in which each node has at most two child nodes, usually distinguished as "left" and "right". Nodes with children are parent nodes, and child nodes may contain references to their parents. Outside the tree, there is often a reference to...

    s, application of tail recursion)
  • Catamorphisms, part three (Example: processing an AST
    Abstract syntax tree
    In computer science, an abstract syntax tree , or just syntax tree, is a tree representation of the abstract syntactic structure of source code written in a programming language. Each node of the tree denotes a construct occurring in the source code. The syntax is 'abstract' in the sense that it...

    )
  • Catamorphisms, part four (Using catamorphisms to produce new ASTs)
  • Catamorphisms, part five (Minimizing memory pressure for AST rewriting task, application of continuation
    Continuation
    In computer science and programming, a continuation is an abstract representation of the control state of a computer program. A continuation reifies the program control state, i.e...

    s)
  • Catamorphisms, part six (Making code from previous article to be tail-recursive)
  • Catamorphisms, part seven (Using monads to reduce boilerplate)
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK