Boost.Hana  1.7.1
Your standard library for metaprogramming
Monoid

Description

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 Groups, Rings 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 Monoids:

As 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.

Minimal complete definition

plus and zero satisfying the laws

Laws

For all objects x, y and z of a Monoid M, the following laws must be satisfied:

plus(zero<M>(), x) == x // left zero
plus(x, zero<M>()) == x // right zero
plus(x, plus(y, z)) == plus(plus(x, y), z) // associativity
constexpr auto plus
Associative binary operation on a Monoid.
Definition: plus.hpp:47

Concrete models

hana::integral_constant

Free model for non-boolean arithmetic data types

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

plus(x, y) = (x + y)
zero<T>() = static_cast<T>(0)

#### Rationale for not making bool a Monoid by default First, it makes no sense whatsoever to define an additive Monoid over the bool type. Also, it could make sense to define a Monoid with logical conjunction or disjunction. However, C++ allows bools 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.

Structure-preserving functions

Let A and B be two Monoids. 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,

f(plus(x, y)) == plus(f(x), f(y))
f(zero<A>()) == zero<B>()

Functions with these properties interact nicely with Monoids, 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...
 

Variable Documentation

◆ plus

constexpr auto boost::hana::plus
constexpr

#include <boost/hana/fwd/plus.hpp>

Initial value:
= [](auto&& x, auto&& y) -> decltype(auto) {
return tag-dispatched;
}

Associative binary operation on a Monoid.

Parameters
x,yTwo objects to combine with the Monoid's binary operation.

Cross-type version of the method

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

  1. A and B share a common data type C, as determined by the common metafunction
  2. A, B and C are all Monoids when taken individually
  3. to<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

plus(x, y) = plus(to<C>(x), to<C>(y))

Example

// Copyright Louis Dionne 2013-2017
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
namespace hana = boost::hana;
BOOST_HANA_CONSTANT_CHECK(hana::plus(hana::int_c<3>, hana::int_c<5>) == hana::int_c<8>);
static_assert(hana::plus(1, 2) == 3, "");
static_assert(hana::plus(1.5f, 2.4) == 3.9, "");
int main() { }
Defines macros to perform different kinds of assertions.
Defines boost::hana::equal.
#define BOOST_HANA_CONSTANT_CHECK(...)
Equivalent to BOOST_HANA_CONSTANT_ASSERT, but not influenced by the BOOST_HANA_CONFIG_DISABLE_ASSERTI...
Definition: assert.hpp:239
Defines boost::hana::integral_constant.
Namespace containing everything in the library.
Definition: accessors.hpp:20
Defines boost::hana::plus.

◆ zero

template<typename M >
constexpr auto boost::hana::zero
constexpr

#include <boost/hana/fwd/zero.hpp>

Initial value:
= []() -> decltype(auto) {
return tag-dispatched;
}

Identity of plus.

Template Parameters
MThe tag (a Monoid) of the returned identity.

Example

// Copyright Louis Dionne 2013-2017
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
namespace hana = boost::hana;
BOOST_HANA_CONSTANT_CHECK(hana::zero<hana::integral_constant_tag<int>>() == hana::int_c<0>);
static_assert(hana::zero<long>() == 0l, "");
int main() { }
constexpr auto zero
Identity of plus.
Definition: zero.hpp:30
Defines boost::hana::zero.