To understand -algebra, we will need some observations, the first one is we can summarize an algebra with a signature. For example, monoid has a signature:
or we can say ring is:
The next observation is we can consider these as objects in a proper category , at here is a [cartesian closed category](math-000D). For example
- monoid is a -morphism
- ring is a -morphism
Now, we generalize algebra's definition.
Definition.
-algebra (algebra for an endofunctor) [math-000F]
Definition. -algebra (algebra for an endofunctor) [math-000F]
With a proper category which can encode the signature of , and an [endofunctor](math-001T) , the -algebra is a triple:
where is a -morphism.
With the definition of -algebra, -algebras of a fixed form a category
Definition. category of
-algebras [math-000G]
Definition. category of -algebras [math-000G]
For a fixed endofunctor in a proper category (below notations omit fixed ),
- the F-algebras form the objects of the category,
- morphisms are homomorphisms of objects , composition is given by functor laws.
A homomorphism is a -morphism makes below diagram commutes.
We can extra check identity indeed works.
There is a theorem about the initial object of the category.
Theorem. Lambek's [math-000H]
Theorem. Lambek's [math-000H]
The evaluator of an initial algebra is an isomorphism.
Proof
Let be an initial in the -algebra category, the following diagram commutes for any -object
Now, replace with , we have commute diagram
Then we consider a trivial commute diagram by duplicating the path
Combine two diagrams, then we get another commute diagram
By definition now we know is a homomorphism, and since is an initial, we must have
Therefore, we also have
so is the inverse of , proves is an isomorphism.
catamorphism [math-000I]
catamorphism [math-000I]
The theorem actually proves the corresponding -object of initial algebra is a fixed point of . By reversing with its inverse, we get a commute diagram below.
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
as constructor Fix
,
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]
anamorphism [math-000J]
As a dual concept, we can draw coalgebra diagram
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.