Category: functors | Component type: concept |
Argument type | The type of the Monoid Operation's first argument and second argument, and also the type returned when the Monoid Operation is returned. |
F | A type that is a model of MonoidOperation |
T | F's argument type. |
f | Object of type F |
x, y, z | Objects of type T |
Name | Expression | Type requirements | Return type |
---|---|---|---|
Function call | f(x, y) | T | |
Identity element | identity_element(f) [3] | T |
Name | Expression | Precondition | Semantics | Postcondition |
---|---|---|---|---|
Function call | f(x, y) | x and y are in the domain of f. | Calls f with x and y as arguments. | |
Identity element | identity_element(f) | Returns the monoid's identity element. That is, the return value is a value id of type T such that, for all x in the domain of f, f(x, id) and f(id, x) both return x. |
Associativity | For any x, y, and z of type T, f(x, f(y, z)) and f(f(x, y), z) return the same value. [4] |
Identity element. | There exists some element id of type T such that, for all x of type T, f(x, id) and f(id, x) both return x. The expression identity_element(f) returns id. |
[1] A monoid is one of three closely related algebraic structures. A semigroup is a set S, and a binary operation *, with the properties that * is closed on S (that is, if x and y are elements of S then x * y is also a member of S) and that * is associative (that is, if x, y, and z are elements of S, then x * (y * z) = (x * y) * z). A monoid is a semigroup that has an identity element. That is, there exists some element id such that, for all x in S, x * id = id * x = x. Finally, a group is a monoid with the property that every element has an inverse. That is, for every x in S, there exists an element xi such that x * xi = xi * x = id. As an example, the set of real numbers under multiplication is a monoid (the identity element is 1), but it isn't a group. It isn't a group because 0 has no inverse.
[2] Mathematics textbooks typically write this as an equation, instead of using words like "is the same as". We can't use equality in this definition, however, because F's argument type might not be equality comparable. If F's argument type is equality comparable, however, then these two expression are expected to be equal: the condition of associativity becomes f(x, f(y, z)) == f(f(x, y), z)
[3] This is implemented as an overloaded function. The function identity_element is defined, in the standard header functional, and the nonstandard backward-compatibility header function.h, for arguments of type plus<T> and multiplies<T>. If you define a new Monoid Operation F (matrix multiplication, for example), you must overload identity_element for arguments of type F. The identity_element function is an SGI extension; it is not part of the C++ standard.
[4] Associativity is not the same as commutativity. That is, the requirement that x * (y * z) == (x * y) * z is completely unrelated to the requirement that x * y == y * x. Monoid operations are required to be associative, but they are not required to be commutative. As an example, square matrices under multiplication form a monoid even though matrix multiplication is not commutative.