Because many operations and data structures are monoids, it is
interesting to use the associated concepts as the computational building
block of an essential calculus. In particular, iteration over collection
types can be elegantly formulated as computing a monoid
homomorphism. This notion coincides with the usual mathematical notion
of homomorphism, albeit given here from an operational standpoint and
biased toward collection monoids. Basically, a monoid homomorphism
maps a function
from a collection monoid
to any monoid
by collecting all the
-images of elements
of a
-collection using the
operation. For example, the
expression
applied to the list
returns the set
. In other words, the monoid
homomorphism
of a function
applied to a list
corresponds to the following loop computation:
This is formulated formally as follows:
Again, computationally, this amounts to executing the following iteration:
The reader may be puzzled by the condition
in
Definition 4.1. It means that a monoid homomorphisms may only be
defined from a collection monoid to a monoid that has at least the same
equational theory. In other words, one can only go from an empty theory
monoid, to either a
-monoid or an
-monoid, or yet to a
-monoid. This requirement is due to an algebraic technicality,
and relaxing it would enable a monoid homomorphism to be ill-defined.
To see this, consider going from, say, a commutative-idempotent monoid to
one that is commutative but not idempotent. Let us take, for example,
. Then, this entails:
The reader may have noticed that this restriction has the unfortunate consequence of disallowing potentially useful computations, notable examples being computing the cardinality of a set, or converting a set into a list. However, this drawback can be easily overcome with a suitable modification of the third clause in Definition 4.1, and other expressions based on it, ensuring that anomalous cases such as the above are dealt with by appropriate tests.
Also of importance for the consistency of Definition 4.1 is
the fact that a non-idempotent monoid must be anti-idempotent, and a
non-commutative monoid must be anti-commutative. Indeed, if
is
non-idempotent as well as non-anti-idempotent (say,
for some
), then this entails:
Here are a few familar functions expressed with well-defined monoid homomorphisms: