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-algebra for some endofunctor of some category into itself. Here is a morphism from to . Since it is initial, we know that whenever is another -algebra, i.e. a morphism from to , there is a unique homomorphism from to . By the definition of the category of -algebra, this corresponds to a morphism from to , conventionally also denoted , such that . In the context of -algebra, the uniquely specified morphism from the initial object is denoted by and hence characterized by the following relationship:
Terminology and history
Another notation 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 Erik Meijeret al.[1] 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 Meijeret al.,[1] which was in the context of the Squiggol formalism.
The general categorical definition was given by Grant Malcolm.
[2][3]
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:
dataMaybea=Nothing|Justa-- Maybe typeclassFunctorfwhere-- class for functorsfmap::(a->b)->(fa->fb)-- action of functor on morphismsinstanceFunctorMaybewhere-- turn Maybe into a functorfmapgNothing=Nothingfmapg(Justx)=Just(gx)
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:[4][5]
dataNat=Zero|SuccNat-- natural number typeini::MaybeNat->Nat-- initial object of Maybe-algebra (with slight abuse of notation) iniNothing=Zeroini(Justn)=Succn
cata::(Maybeb->b)->(Nat->b)catagZero=g(fmap(catag)Nothing)-- Notice: fmap (cata g) Nothing = g Nothing and Zero = ini(Nothing)catag(Succn)=g(fmap(catag)(Justn))-- Notice: fmap (cata g) (Just n) = Just (cata g n) and Succ n = ini(Just n)
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:
dataMaybeProdab=Nothing|Just(a,b)-- (a,b) is the product type of a and bclassFunctorfwhere-- class for functorsfmap::(a->b)->(fa->fb)-- action of functor on morphismsinstanceFunctor(MaybeProda)where-- turn MaybeProd a into a functor, the functoriality is only in the second type variablefmapgNothing=Nothingfmapg(Just(x,y))=Just(x,gy)
The initial algebra of MaybeProd a is given by the lists of elements with type a together with the morphism ini defined below:[6]
dataLista=EmptyList|Consa(Lista)ini::MaybeProda(Lista)->Lista-- initial algebra of MaybeProd ainiNothing=EmptyListini(Just(n,l))=Consnl
The cata map can be defined by:
cata::(MaybeProdab->b)->(Lista->b)catagEmptyList=g(fmap(catag)Nothing)-- Note: ini Nothing = EmptyListcatag(Conssl)=g(fmap(catag)(Just(s,l)))-- Note: Cons s l = ini (Just (s,l))
Notice also that cata g (Cons s l) = g (Just (s, cata g l)).
As an example consider the following morphism:
g::MaybeProdIntInt->IntgNothing=3g(Just(x,y))=x*y
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
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.
typeTreeAlgebraab=(a->b,b->b->b)-- the "two cases" function is encoded as (f, g)dataTreea=Leafa|Branch(Treea)(Treea)-- which turns out to be the initial algebrafoldTree::TreeAlgebraab->(Treea->b)-- catamorphisms map from (Tree a) to bfoldTree(f,g)(Leafx)=fxfoldTree(f,g)(Branchleftright)=g(foldTree(f,g)left)(foldTree(f,g)right)
treeDepth::TreeAlgebraaInteger-- an f-algebra to numbers, which works for any input typetreeDepth=(const1,\ij->1+maxij)treeSum::(Numa)=>TreeAlgebraaa-- an f-algebra, which works for any number type treeSum=(id,(+))
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.
typeAlgebrafa=fa->a-- the generic f-algebrasnewtypeFixf=Iso{invIso::f(Fixf)}-- gives us the initial algebra for the functor fcata::Functorf=>Algebrafa->(Fixf->a)-- catamorphism from Fix f to acataalg=alg.fmap(cataalg).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 Maybe's Nothing and identify a successor function with repeated application of the Just. This way the natural numbers arise.
typeNat=FixMaybezero::Natzero=IsoNothing-- every 'Maybe a' has a term Nothing, and Iso maps it into asuccessor::Nat->Natsuccessor=Iso.Just-- Just maps a to 'Maybe a' and Iso maps back to a new term
pleaseWait::AlgebraMaybeString-- again the silly f-algebra example from abovepleaseWait(Juststring)="wait.. "++stringpleaseWaitNothing="go!"
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).
^Malcolm, Grant (1990), "Data structures and program transformation", Science of Computer Programming, vol. 14, no. 2–3, pp. 255–279, doi:10.1016/0167-6423(90)90023-7.