Commutative Semigroup
CSemigroup[A] is a semigroup that is commutative.
In addition to the laws of a semigroup:
- Closure (closure): for \(\forall x, y \in CS\), \(x + y \in CS\).
- Associativity (associativity): for \(\forall x, y, z \in CS\), \((x + y) + z = x + (y + z)\).
the following law must hold:
- Commutativity (commutative): for \(\forall x, y \in CS\), \(x + y = y + x\).
Code
trait CSemigroup[A] extends Semigroup[A]
Laws
Commutativity means that the order of elements when combining does not matter.
In other words, the result of the combine operation will be the same regardless of
the order in which you pass elements x and y.
This can be written as: cs.combine(x, y) == cs.combine(y, x)
Diagram
classDiagram
class Semigroup~A~{
+combine(x: A, y: A) A
}
class CommutativeSemigroup~A~
Semigroup <|-- CommutativeSemigroup
Implementation in Libraries
Implementation in Spire
import spire.algebra.CSemigroup
CSemigroup.combine(1, 2) == CSemigroup.combine(2, 1)
References: