# Group ## Metadata **Status**:: #x **Zettel**:: #zettel/literature **Created**:: [[2024-03-27]] ## Synopsis Group is a [[Monoid (Math)|Monoid]] and every element in the set has an inverse. - Abelian group is a set U under the binary operator ⨁. - ([[Magma]]) The binary operator is closed: $\forall a, b \in U: a \oplus b \in U$ - ([[Semigroup]]) The binary operator is associative: $(a \oplus b) \oplus c = a \oplus (b \oplus c)$ - ([[Monoid (Math)|Monoid]]) There exists an identity element: $\exists 0 \in U: a \oplus 0 = 0 \oplus a = a$ - (Group) Every element has an inverse: $\forall a \in U: \exists b \in U: a \oplus b = 0$ ## Analogy for Programmer A group can be thought of as a type with a defined operator. The operator determines the operations can be performed on the type. ```rust pub trait Group { // monoid const EMPTY: Self; // magma fn combine(self, rhs: Self) -> Self; // group fn inverse(self) -> Self; } impl Group for i8 { const EMPTY: Self = 0i8; fn combine(self, rhs: Self) -> Self { return self + rhs; } fn inverse(self) -> Self { return -self; } } pub fn constraints<T: Group + Copy + Eq + std::fmt::Debug>(a: T, b: T, c: T) { // semigroup assert_eq!(a.combine(b).combine(c), a.combine(b.combine(c))); // monoid assert_eq!(a.combine(T::EMPTY), a); assert_eq!(T::EMPTY.combine(a), a); // group assert_eq!(a.combine(a.inverse()), T::EMPTY); } pub fn main() { constraints(-1, 0, 1); } ```