« Home

understanding FF -algebra [math-000E]

To understand FF -algebra, we will need some observations, the first one is we can summarize an algebra with a signature. For example, monoid has a signature:

{1:1m:m×mm\begin{cases} 1 : 1 \to m \\ \cdot : m \times m \to m \end{cases}

or we can say ring is:

{0:1m1:1m+:m×mm×:m×mm:mm\begin{cases} 0 : 1 \to m \\ 1 : 1 \to m \\ + : m \times m \to m \\ \times : m \times m \to m \\ - : m \to m \end{cases}

The next observation is we can consider these mm as objects in a proper category CC , at here is a [cartesian closed category](math-000D). For example

  • monoid is a CC -morphism 1+m×mm1 + m \times m \to m
  • ring is a CC -morphism 1+1+m×m+m×m+mm1 + 1 + m \times m + m \times m + m \to m

Now, we generalize algebra's definition.

Definition. FF -algebra (algebra for an endofunctor) [math-000F]

With a proper category CC which can encode the signature of F()F(-) , and an [endofunctor](math-001T) F:CCF : C \to C , the FF -algebra is a triple:

(F,x,α)(F, x, \alpha)

where α:F  xx\alpha : F \; x \to x is a CC -morphism.

figure tex808

With the definition of FF -algebra, FF -algebras of a fixed FF form a category

Definition. category of FF -algebras [math-000G]

For a fixed endofunctor FF in a proper category CC (below notations omit fixed FF ),

  1. the F-algebras (x,α)(x, \alpha) form the objects of the category,
  2. morphisms are homomorphisms of objects (x,α)F  m(y,β)(x, \alpha) \xrightarrow{F\;m} (y, \beta) , composition is given by functor laws.

A homomorphism is a CC -morphism mm makes below diagram commutes.

figure tex808

We can extra check identity indeed works.

figure tex809

There is a theorem about the initial object of the category.

Theorem. Lambek's [math-000H]

The evaluator jj of an initial algebra (F,i,j)(F, i, j) is an isomorphism.

Proof

Let F  iF\;i be an initial in the FF -algebra category, the following diagram commutes for any CC -object aa

figure tex808

Now, replace aa with F  iF\;i , we have commute diagram

figure tex809

Then we consider a trivial commute diagram by duplicating the path F  iiF\;i \to i

figure tex810

Combine two diagrams, then we get another commute diagram

figure tex811

By definition now we know jmj \circ m is a homomorphism, and since F  iF\;i is an initial, we must have

F  jF  m=1FiF\;j \circ F\;m = 1_{Fi}

Therefore, we also have

jm=1ij \circ m = 1_i

so mm is the inverse of jj , proves jj is an isomorphism.

catamorphism [math-000I]

The theorem actually proves the corresponding CC -object ii of initial algebra (F,i,j)(F, i, j) is a fixed point of FF . By reversing jj with its inverse, we get a commute diagram below.

figure tex808

In Haskell, we are able to define initial algebra

newtype Fix f = Fix (f (Fix f))

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         unFix :: Fix f -> f (Fix f)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         unFix (Fix x) = x

View jj as constructor Fix, j1j^{-1} as unFix, then we can define m = alg . fmap m . unFix. Since m :: Fix f -> a, we have definition of catamorphism.

cata :: Functor f => (f a -> a) -> Fix f -> a
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         cata alg = alg . fmap (cata alg) . unFix

An usual example is foldr, a convenient specialization of catamorphism.

anamorphism [math-000J]

As a dual concept, we can draw coalgebra diagram

figure tex808

and define anamorphism as well.

ana :: Functor f => (a -> f a) -> a -> Fix f
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         ana coalg = Fix . fmap (ana coalg) . coalg

I mostly learn F-algebras from Category Theory for Programmers, and take a while to express the core idea in details with my voice with a lot practicing. The article answered some questions, but we always with more. What's an algebra of monad? What's an algebra of a programming language (usually has a recursive syntax tree)? Hope you also understand it well through the article and practicing.