Catamorphism

From Wikipedia, the free encyclopedia
Jump to: navigation, search

In category theory, the concept of catamorphism (from Greek: κατά = downwards or according to; μορφή = form or shape) 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 and they generalize unfolds. See also Hylomorphism.

Definition[edit]

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-algebras, 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-algebras, the uniquely specified morphism from the initial object is denoted by cata f and hence characterized by the following relationship:

  • h = \mathrm{cata}\ f
  • h \circ in = f \circ Fh

Terminology and history[edit]

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 Meijer 1991. 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.[1], which was in the context of the Squiggol formalism. The general categorical definition was given by Grant Malcolm. [1][2]

Examples[edit]

We give a series of examples, and then a more global approach to catamorphisms, in the Haskell programming language.

Iteration[edit]

Iteration-step prescriptions lead to natural numbers as initial object.

Consider the functor f mapping a data type b to a data type f b, which contains a copy of each term from b as well as one additional term Nothing (f can be the Maybe functor in Haskell). Let an instance of a StepAlgebra be a function from f b -> 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 = (b, b->b) -- the algebras, which we encode as pairs (nil, next)
 
data Nat = Zero | Succ Nat -- which is the initial algebra for the functor described above
 
foldSteps :: StepAlgebra b -> (Nat -> b) -- the catamorphisms map from Nat to b
foldSteps (nil, next) Zero       = nil
foldSteps (nil, next) (Succ nat) = next $ foldSteps (nil, next) nat

As a silly example, consider the algebra on strings encoded as ("go!", \s -> "wait.. " ++ s), for which Nothing is mapped to "go!" and otherwise "wait.. " is prepended. As (Succ . Succ . Succ . Succ $ Zero) denotes the number four in Nat, the following will evaluate to "wait.. wait.. wait.. wait.. go!": foldSteps ("go!", \s -> "wait.. " ++ s) (Succ . Succ . Succ . Succ $ Zero). We can easily change the code to a more useful operation, say repeated operation of an algebraic operation on numbers, just by changing the F-algebra (nil, next), which is passed to foldSteps

List fold[edit]

For a fixed type a, consider the functor mapping types b to the product type of the two. We moreover also add a term Nil to this type. Here an f-algebra will map Nil to some special term nil of b or "merge" a pair coming from the product type into a term of b.

type ContainerAlgebra a b = (b, a -> b -> b) -- f-algebra, encoded as (nil, merge)
 
data List a = Nil | Cons a (List a) -- which turns out to be the initial algbera
 
foldrList :: ContainerAlgebra a b -> (List a -> b) -- catamorphisms map from (List a) to b
foldrList (nil, merge) Nil         = nil
foldrList (nil, merge) (Cons x xs) = merge x $ listFoldr (nil, merge) xs

As an example, consider the algebra on numbers types encoded as (3, \x-> \y-> x*y), for which the number from a acts on the number from b by plain multiplication. Then the following will evaluate to 3.000.000: foldrList (3, \x-> \y-> x*y) (Cons 10 $ Cons 100 $ Cons 1000 Nil)

Tree fold[edit]

For a fixed type a, consider the functor mapping types b to the product type of b with itself, as well as a copy of each term of a. An algebra consists of a function to b, which either acts on a the copy terms, or on two b terms.

type TreeAlgebra a b = (a -> b, b -> b -> b) -- the "two cases" function is encoded as (f, g)
 
data Tree a = Leaf a | Branch (Tree a) (Tree a) -- which turns out to be the initial algebra
 
foldTree :: TreeAlgebra a b -> (Tree a -> b) -- catamorphisms map from (Tree a) to b
foldTree (f, g) (Leaf x)            = f x
foldTree (f, g) (Branch left right) = g (foldTree (f, g) left) (foldTree (f, g) right)
treeDepth :: TreeAlgebra a Integer -- an f-algebra to numbers, which works for any input type
treeDepth = (const 1, \i j -> 1 + max i j)
 
treeSum :: (Num a) => TreeAlgebra a a -- an f-algebra, which works  for any number type 
treeSum = (id, (+))

General case[edit]

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.

Compiler magic for 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.

type Algebra f a = f a -> a -- the generic f-algebras
 
newtype Fix f = Iso { invIso :: f (Fix f) } -- gives us the initial algebra for the functor f
 
cata :: Functor f => Algebra f a -> (Fix f -> a) -- catamorphism from Fix f to a
cata alg = alg . fmap (cata alg) . 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
suck :: Nat -> Nat
suck = 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 (Just string) = "wait.. " ++ string
pleaseWait Nothing = "go!"

Again, the following will evaluate to "wait.. wait.. wait.. wait.. go!": cata pleaseWait (suck.suck.suck.suck $ 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).

data Tcon a b = TconL a | TconR b b
instance Functor (Tcon a) where
    fmap f (TconL x)   = TconL x
    fmap f (TconR y z) = TconR (f y) (f z)
type Tree a = Fix (Tcon a) -- 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 (Tcon a) Integer -- again, the treeDepth f-algebra example
treeDepth (TconL x)   = 1
treeDepth (TconR y z) = 1 + max y z

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

See also[edit]

References[edit]

  1. ^ Malcolm, Grant Reynold (1990), Algebraic Data Types and Program Transformation (PDF) (Ph.D. Thesis), University of Groningen .
  2. ^ Malcolm, Grant (1990), Data structures and program transformation, Science of Computer Programming 14 (2-3): 255–279, doi:10.1016/0167-6423(90)90023-7 .

Further reading[edit]

External links[edit]