In
computer science
Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to practical disciplines (includin ...
, and in particular
functional programming
In computer science, functional programming is a programming paradigm where programs are constructed by applying and composing functions. It is a declarative programming paradigm in which function definitions are trees of expressions that ...
, a hylomorphism is a
recursive
Recursion (adjective: ''recursive'') occurs when a thing is defined in terms of itself or of its type. Recursion is used in a variety of disciplines ranging from linguistics to logic. The most common application of recursion is in mathemati ...
function, corresponding to the
composition
Composition or Compositions may refer to:
Arts and literature
* Composition (dance), practice and teaching of choreography
*Composition (language), in literature and rhetoric, producing a work in spoken tradition and written discourse, to include ...
of an
anamorphism
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 ...
(which first builds a set of results; also known as 'unfolding') followed by a
catamorphism
In category theory, the concept of catamorphism (from the Ancient Greek: "downwards" and "form, shape") denotes the unique homomorphism from an initial algebra into some other algebra.
In functional programming, catamorphisms provide generaliza ...
(which then
folds
Benjamin Scott Folds (born September 12, 1966) is an American singer-songwriter, musician, and composer, who is the first artistic advisor to the National Symphony Orchestra at the John F. Kennedy Center for the Performing Arts, Kennedy Center in ...
these results into a final
return value
In computer programming, a return statement causes execution to leave the current subroutine and resume at the point in the code immediately after the instruction which called the subroutine, known as its return address. The return address is s ...
). Fusion of these two recursive computations into a single recursive pattern then avoids building the intermediate data structure. This is an example of
deforestation
Deforestation or forest clearance is the removal of a forest or stand of trees from land that is then land conversion, converted to non-forest use. Deforestation can involve conversion of forest land to farms, ranches, or urban area, urban ...
, a program optimization strategy. A related type of function is a metamorphism, which is a catamorphism followed by an anamorphism.
Formal definition
A hylomorphism
can be defined in terms of its separate anamorphic and catamorphic parts.
The anamorphic part can be defined in terms of a
unary function
defining the list of elements in
by repeated application (''"unfolding"''), and a
predicate
Predicate or predication may refer to:
* Predicate (grammar), in linguistics
* Predication (philosophy)
* several closely related uses in mathematics and formal logic:
**Predicate (mathematical logic)
**Propositional function
**Finitary relation, ...
providing the terminating condition.
The catamorphic part can be defined as a combination of an initial value
for the fold and a binary
operator
Operator may refer to:
Mathematics
* A symbol indicating a mathematical operation
* Logical operator or logical connective in mathematical logic
* Operator (mathematics), mapping that acts on elements of a space to produce elements of another ...
used to perform the fold.
Thus a hylomorphism
:
may be defined (assuming appropriate definitions of
&
).
Notation
An abbreviated notation for the above hylomorphism is
) function calls. Therefore, it is sometimes necessary to generate a temporary list of intermediate results before reducing this list to a single result.
One example of a commonly encountered hylomorphism is the canonical factorial function.
) it can be seen that this function, applied to any given valid input, will generate a linear call tree
to a list. For example, given ''n'' = 5 it will produce the following:
factorial 5 = 5 * (factorial 4) = 120
factorial 4 = 4 * (factorial 3) = 24
factorial 3 = 3 * (factorial 2) = 6
factorial 2 = 2 * (factorial 1) = 2
factorial 1 = 1 * (factorial 0) = 1
factorial 0 = 1
In this example, the anamorphic part of the process is the generation of the call tree which is isomorphic to the list