In this section, all notions and notations relating to monoids as they are used in this paper are recalled and justified.
Mathematically, a monoid is a non-empty set equipped with an associative
internal binary operation and an identity element for this
operation. Formally, let
be a set,
a function from
to
, and
; then,
is a monoid iff, for any
in
:
Most familiar mathematical binary operations define monoids. For
example, taking the set of natural numbers
, and the set of
boolean values
, the following are monoids:
Note that the definition of a monoid does not preclude additional algebraic structure. Such structure may be specified by other equations augmenting the basic monoid equational theory given by the conjunction of equations (3) and (4). For example, all five monoids listed above are commutative; namely, they also obey equation (5):
Not all monoids are primitive monoids. That is, one may define a monoid purely syntactically whose operation only builds a syntactic structure rather than being interpreted using some semantic computation. For example, linear lists have such a structure: the operation is list concatenation and builds a list out of two lists; its identity element is the empty list. A syntactic monoid may also have additional algebraic structure. For example, the monoid of bags is also defined as a commutative syntactic monoid with the disjunct union operation and the empty bag as identity. Or, the monoid of sets is a commutative and idempotent syntactic monoid with the union operation and the empty set as identity.
Because they are not interpreted, syntactic monoids pose a problem as
far as representation of its elements is concerned. To illustrate this,
let us consider an empty-theory algebraic structure; that is, one
without any equations--not even associativity nor identity. Let us take
such a structure with one binary operation
on, say, the natural
numbers
. Saying that
is a ``syntactic'' operation means
that it constructs a syntactic term (i.e., an expression tree) by
composing two other syntactic terms. We thus can define the set
of
-terms on some base set, say the natural numbers,
inductively as the limit
where,
Let us now assume that the
operation is associative--i.e., that
-terms verify Equation (3). Note that this equation
defines a (syntactic) congruence on
which identifies
terms such as, say,
and
. In fact,
for such an associative
operation, the set
defined
in Equation (7) is not the appropriate domain. Rather, the
right domain is the quotient set whose elements are (syntactic)
congruence classes modulo associativity of
. Therefore, this
creates an ambiguity of representation of the syntactic
structures.5
Similarly, more algebraic structure defined by larger equational theories induces coarser quotients of the empty-theory algebra by putting together in common congruence classes all the syntactic expressions that can be identified modulo the theory's equations. The more equations, the more ambiguous the syntactic structures of expressions. Mathematically, this poses no problem as one can always abstract away from individuals to congruence classes. However, operationally one must resort to some concrete artifact to obtain a unique representation for all members of the same congruence class. One way is to devise a canonical representation into which to transform all terms. For example, an associative operation could systematically ``move'' nested subtrees from its left argument to its right argument--in effect using Equation (3) as a one-way rewrite rule. However, while this is possible for some equational theories, it is not so in general--e.g., take commutativity.6
From a programming standpoint (which is ours), we can abstract away from
the ambiguity of canonical representations of syntactic monoid terms
using a flat notation. For example, in LISP and Prolog, a list is seen as
the (flat) sequence of its constituents. Typically, a programmer writes
to represent the list whose elements are
,
and
in
this order, and does not care (nor need s/he be aware) of its concrete
representation. A set--i.e., a commutative idempotent syntactic
monoid--is usually denoted by the usual mathematical notation
,
implicitly relying on disallowing duplicate elements, not minding the
order in which the elements appear. A bag, or multiset--i.e., a
commutative but non-idempotent syntactic monoid--uses a similar
notation, allowing duplicate elements but paying no heed to the order in
wich they appear; i.e.,
is the bag containing
twice,
and
once.
Syntactic monoids are quite useful for programming as they provide
adquate data structures to represent collections of objects of a given
type. Thus, we refer to them as collection monoids. Now, a
definition such as Equation (7) for a syntactic monoid,
although sound mathematically, is not quite adequate for programming
purposes. This is because it defines the
operations on two
distinct types of elements; namely, the base elements (here
natural numbers) and constructed elements. In programming, it is
desirable that operations be given a crisp type. A way to achieve this is
by systematically ``wrapping'' each base element
into a term such as
. This ``wrapping'' is achieve by associating to the
monoid a function
from the base set into the monoid domain
called its unit injection. For example, if
is the list
monoid operation for concatenating two lists,
and one
may view the list
as
. Similarly, the set
is viewed as
, and the bag
as
. Clearly, this
bases the constructions on an isomorphic view of the base set rather than
the base set itself, while using a uniform type for the monoid operator.
Also, because the type of the base elements is irrelevant for the
construction other than imposing the constraint that all such elements be
of the same type, we present a collection monoid as a polymorphic
data type. This justifies the formal view of monoids we give next using
the programming notion of type.
Because it is characterized by its operation
, a monoid is often
simply referred to as
. Thus, a monoid operation is used as a
subscript to denote its characteristic attributes. Namely, for a monoid
,