Skip to main content

Group

Trait Group 

Source
pub trait Group: Monoid {
    // Required method
    fn inverse(&self) -> Self;
}
Expand description

A group: a monoid where every element has an inverse.

Laws (not enforced by type system):

  • Associative: a.combine(b).combine(c) == a.combine(b.combine(c))
  • Identity: a.combine(empty()) == a == empty().combine(a)
  • Inverse: a.combine(a.inverse()) == empty() == a.inverse().combine(a)

§Example

use algebra::Group;
use algebra::Monoid;
use algebra::Semigroup;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct AddInt(i32);

impl Semigroup for AddInt {
    fn combine(&self, other: &Self) -> Self {
        AddInt(self.0 + other.0)
    }
}

impl Monoid for AddInt {
    fn empty() -> Self {
        AddInt(0)
    }
}

impl Group for AddInt {
    fn inverse(&self) -> Self {
        AddInt(-self.0)
    }
}

let x = AddInt(5);
let inv = x.inverse();
assert_eq!(x.combine(&inv), AddInt::empty());

Required Methods§

Source

fn inverse(&self) -> Self

Return the inverse of this element.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§