Fri 4 Jul 2008
Anamorphism
Posted by Edward Kmett under Category Theory , Mathematics , Recursion Schemes[7] Comments
Description
Anamorphisms are generalizations of Haskell's unfoldr. A anamorphism builds a data structure with an F-coalgebra by recursively unrolling a seed.
History
The name anamorphism comes from the Greek 'ανα-' meaning "upwards". [1,2]
Notation
An anamorphism for some F-coalgebra
is denoted with "lenses"
. When the functor F can be determined unambiguously, it is usually written
or ana
.
Implementation
ana :: Functor f => Coalgebra f a -> a -> FixF f ana g = InF . fmap (ana g) . g
Alternate implementations
ana g = hylo InF g
Duality
An anamorphism is the categorical dual of a catamorphism.
Derivation
If
is the final
-coalgebra for some endofunctor
and
is an
-coalgebra, then there is a unique
-coalgebra homomorphism from
to
which we denote
.
That is to say, the following diagram commutes:
Laws
| Rule | Haskell |
|---|---|
| ana-charn | fmap x . psi = outF . x x = ana psi |
| ana-self | fmap (ana psi) . psi = outF . ana psi |
| ana-id | id = ana outF |
| ana-uniq | fmap x . psi = outF . x fmap y . psi = outF . y => x = y |
| ana-fusion | fmap x . phi = psi . x => ana phi = ana psi . x |
| ana-compose | e :: f :~> g => ana (e . outF) . ana phi = ana (e . phi) |
Examples
data StreamF a x = a :> x type Stream a = FixF (StreamF a) instance Functor (StreamF a) where fmap f (a :> as) = a :> f as repeat :: a -> Stream a repeat = ana psi where psi n = n :> n from :: Num a => a -> Stream a from = ana psi where psi n = n :> (n + 1)
Links
[Haddock] [Source] [Field Guide]
References
[1] E. Meijer, M. Fokkinga, R. Paterson. Functional Programming with Bananas, Lenses, Envelopes and Barbed Wire. Proceedings, 5th ACM Conference on Functional Programming Languages and Computer Architecture.
[2] M. Fokkinga. Law and Order in Algorithmics. PhD Thesis. 1992

x = ana psi
fmap y . psi = outF . y => x = y
-Algebra
are commonly denoted using "barbed wire" as
or as para f. Uustalu and Vene [2] use the notation
so that apomorphisms, the categorical dual of paramorphisms, can have a symmetric notation.
and
we have
. [2, p9, Lemma 2]
, the following diagram commutes:
is denoted
. When the functor F can be determined unambiguously, it is usually written
or cata
.
is the initial
.



