Commutative Monoid
CMonoid[A] is a monoid that is commutative.
In addition to the laws of a monoid:
- Closure (closure): for \(\forall x, y \in CM\), \(x + y \in CM\).
- Associativity (associativity): for \(\forall x, y, z \in CM\), \((x + y) + z = x + (y + z)\).
- Identity (identity): there exists \(\exists e \in CM\) such that for \(\forall x \in CM\), \(e + x = x + e = x\)
the following law must hold:
- Commutativity (commutative): for \(\forall x, y \in CM\), \(x + y = y + x\).
Code
trait CMonoid[A] extends Monoid[A], CSemigroup[A]
Laws
Laws are inherited from monoid and commutative semigroup.
Diagram
classDiagram
class Semigroup~A~{
+combine(x: A, y: A) A
}
class Monoid~A~{
+empty() A
}
Semigroup <|-- Monoid
class CommutativeSemigroup~A~
Semigroup <|-- CommutativeSemigroup
class CommutativeMonoid~A~
Monoid <|-- CommutativeMonoid
CommutativeSemigroup <|-- CommutativeMonoid
Implementation in Libraries
Implementation in Spire
CMonoid[A] is a monoid that is commutative.
import spire.algebra.CMonoid
CMonoid.combine(100, CMonoid.empty[Int]) // 100
References: