The Monoid
concept represents data types with an associative binary operation that has an identity.
Specifically, a Monoid is a basic algebraic structure typically used in mathematics to construct more complex algebraic structures like Group
s, Ring
s and so on. They are useful in several contexts, notably to define the properties of numbers in a granular way. At its core, a Monoid
is a set S
of objects along with a binary operation (let's say +
) that is associative and that has an identity in S
. There are many examples of Monoid
s:
0
as the identity1
as the identityAs you can see with the integers, there are some sets that can be viewed as a monoid in more than one way, depending on the choice of the binary operation and identity. The method names used here refer to the monoid of integers under addition; plus
is the binary operation and zero
is the identity element of that operation.
plus
and zero
satisfying the laws
For all objects x
, y
and z
of a Monoid
M
, the following laws must be satisfied:
A data type T
is arithmetic if std::is_arithmetic<T>::value
is true. For a non-boolean arithmetic data type T
, a model of Monoid
is automatically defined by setting
Rationale for not making
bool
aMonoid
by defaultFirst, it makes no sense whatsoever to define an additive
Monoid
over thebool
type. Also, it could make sense to define aMonoid
with logical conjunction or disjunction. However, C++ allowsbool
s to be added, and the method names of this concept really suggest addition. In line with the principle of least surprise, no model is provided by default.
Let A
and B
be two Monoid
s. A function f : A -> B
is said to be a Monoid morphism if it preserves the monoidal structure between A
and B
. Rigorously, for all objects x, y
of data type A
,
Functions with these properties interact nicely with Monoid
s, which is why they are given such a special treatment.
Variables | |
constexpr auto | boost::hana::plus |
Associative binary operation on a Monoid . More... | |
template<typename M > | |
constexpr auto | boost::hana::zero |
Identity of plus . More... | |
constexpr auto boost::hana::plus |
#include <boost/hana/fwd/plus.hpp>
Associative binary operation on a Monoid
.
x,y | Two objects to combine with the Monoid 's binary operation. |
The plus
method is "overloaded" to handle distinct data types with certain properties. Specifically, plus
is defined for distinct data types A
and B
such that
A
and B
share a common data type C
, as determined by the common
metafunctionA
, B
and C
are all Monoid
s when taken individuallyto<C> : A -> B
and to<C> : B -> C
are Monoid
-embeddings, as determined by the is_embedding
metafunction.The definition of plus
for data types satisfying the above properties is obtained by setting
constexpr auto boost::hana::zero |
#include <boost/hana/fwd/zero.hpp>
Identity of plus
.
M | The tag (a Monoid ) of the returned identity. |