ArticleslgStudy

science

Anamorphism

Anamorphism is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Anamorphism rather than just read about it. In short: In computer programming, an anamorphism is a function that generates a sequence by repeated application of the function to its previous result. You begin with some value A and apply a function f to it to get B.

Key takeaways

  • Anamorphism belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Anamorphism to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Anamorphism from memory before moving on to harder problems.

Reference excerpt

In computer programming, an anamorphism is a function that generates a sequence by repeated application of the function to its previous result. You begin with some value A and apply a function f to it to get B. Then you apply f to B to get C, and so on until some terminating condition is reached. The anamorphism is the function that generates the list of A, B, C, etc. The anamorphism can be thought of as unfolding the initial value into a sequence. The above layman's description can be stated more formally in category theory: the anamorphism of a coinductive type denotes the assignment of a coalgebra to its unique morphism to the final coalgebra of an endofunctor. These objects are used in functional programming as unfolds. The categorical dual (the notion most naturally considered the "opposite") of the anamorphism is the catamorphism.

Anamorphisms in functional programming In functional programming, an anamorphism is a generalization of the concept of unfolds on coinductive lists. Formally, anamorphisms are generic functions that can corecursively construct a result of a certain type and which are parameterized by functions that determine the next single step of the construction. The data type in question is defined as the greatest fixed point ν X . F X of a functor F. By the universal property of final coalgebras, there is a unique coalgebra morphism A → ν X . F X for any other F-coalgebra a : A → F A. Thus, one can define functions from a type A into a coinductive datatype by specifying a coalgebra structure a on A.

Example: Potentially infinite lists As an example, the type of potentially infinite lists (with elements of a fixed type value) is given as the fixed point [value] = ν X . value × X + 1, i.e. a list consists either of a value and a further list, or it is empty. A (pseudo-)Haskell-Definition might look like this:

It is the fixed point of the functor F value, where:

One can easily check that indeed the type [value] is isomorphic to F value [value], and thus [value] is the fixed point. (Also note that in Haskell, least and greatest fixed points of functors coincide, therefore inductive lists are the same as coinductive, potentially infinite lists.) The anamorphism for lists (then usually known as unfold) would build a (potentially infinite) list from a state value. Typically, the unfold takes a state value x and a function f that yields either a pair of a value and a new state, or an indication that the list has ended. The anamorphism would then begin with a first seed, compute whether the list continues or ends, and in case of a nonempty list, prepend the computed value to the recursive call to the anamorphism. A Haskell definition of an unfold, or anamorphism for lists, called ana, is as follows:

We can now implement quite general functions using ana, for example a countdown:

This function will decrement an integer and output it at the same time, until it is negative, at which point it will mark the end of the list. Correspondingly, ana f 3 will compute the list [2,1,0].

Anamorphisms on other data structures An anamorphism can be defined for any recursive type, according to a generic pattern, generalizing the ana for lists given above. For example, the unfold for the tree data structure

is as follows

To better see the relationship between the recursive type and its anamorphism, note that Tree and List can be defined thus:

The analogy with ana appears by renaming b in its type:

With these definitions, the argument to the constructor of the type has the same type as the return type of the first argument of ana, with the recursive mentions of the type replaced with b.

History One of the first publications to introduce the notion of an anamorphism in the context of programming was the paper Functional Programming with Bananas, Lenses, Envelopes and Barbed Wire, by Erik Meijer et al., which was in the context of the Bird–Meertens formalism, informally known as Squiggol.

Applications Functions like zip and iterate are examples of anamorphisms. zip takes a pair of lists, say ['a','b','c'] and [1,2,3] and returns a list of pairs [('a',1),('b',2),('c',3)]. The function iterate takes a thing, x, and a function, f, from such things to such things, and returns the infinite list that comes from repeated application of f, i.e. the list [x, (f x), (f (f x)), (f (f (f x))), ...].

To see this, we can define both in terms of the generic unfold ana given above:

In a language like Haskell, even the abstract functions unfold and ana are merely defined terms, as we have seen from the definitions given above.

Anamorphisms in category theory In category theory, anamorphisms are the categorical dual of catamorphisms. That means the following. Suppose (A, fin) is a final F-coalgebra for some endofunctor F on some category. Thus, fin is a morphism from A to FA, and since it is assumed to be final we know that whenever (X, f) is another F-coalgebra (a morphism f from X to FX), there will be a unique homomorphism h from (X, f) to (A, fin), that is a morphism h from X to A such that fin . h = Fh . f. Then for each such f we denote by ana f that uniquely specified morphism h. In other words, we have the following defining relationship, given some fixed F, A, and fin as above:

h=ana f fin∘h=Fh∘f

Notation A notation for ana f found in the literature is [(f)]. The brackets used are known as lens brackets, after which anamorphisms are sometimes referred to as lenses.

See also Morphism Morphisms of F-algebras and F-coalgebras From an initial algebra to an algebra: Catamorphism An anamorphism followed by a catamorphism: Hylomorphism Extension of the idea of catamorphisms: Paramorphism Extension of the idea of anamorphisms: Apomorphism

References

External links Anamorphisms in Haskell

Worked examples

Example 1 — a first encounter with Anamorphism

Start with the simplest possible case. Write down what Anamorphism claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Anamorphism before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Anamorphism ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Anamorphism

In research
Anamorphism appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Anamorphism in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Anamorphism is common in secondary-school and first-year university syllabi. It links to neighbouring topics Category theory, Recursion schemes, so understanding it makes those chapters shorter.
In everyday life
Look for Anamorphism outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Anamorphism” →

How to study Anamorphism in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Anamorphism means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Anamorphism out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Anamorphism in simple terms?

In computer programming, an anamorphism is a function that generates a sequence by repeated application of the function to its previous result. You begin with some value A and apply a function f to it to get B.

Why does Anamorphism matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Anamorphism?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Anamorphism.

Tags

  • Category theory
  • Recursion schemes

Keep exploring