Fri 4 Jul 2008
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
July 4th, 2008 at 12:35 pm
[…] A catamorphism is the categorical dual of an anamorphism. […]
July 4th, 2008 at 12:36 pm
[…] a href=”http://comonad.com/reader/2008/anamorphism”>anamorphism† […]
July 5th, 2008 at 7:42 am
You accidentally typed cata instead of ana in the implementation.
July 5th, 2008 at 2:24 pm
Woops! Fixed.
July 5th, 2008 at 8:34 pm
Great work, edwardk! What’s next, hylos?
As an aside, I think it would be nice if in these field guides you could point to some examples of commonly used library functions that are ~morphisms under the hood. Saying “fold” is a catamorphism and “unfold” is an anamorphism is a good start, but giving people an idea of how common these patterns are might be good; like saying that filter too is a catamorphism, or that zip is an anamorphism, or what have you.
July 7th, 2008 at 7:57 am
I’ll slip hylo in at some point, but I’m presently planning to do apomorphisms (for symmetry with para) and then generalized anamorphisms so I can start to give an account of all of the morphisms in a unified framework of distributive laws combinators next.
July 7th, 2008 at 3:16 pm
That makes good sense. I’m looking forward to reading them.
Did you notice that your field guide got some love on reddit? There’s hope!