pub trait MonoidHom: SemigroupHom { }Expand description
A monoid homomorphism: a structure-preserving map between monoids that preserves both combine and identity.
A homomorphism f: M → N preserves both the monoid operation and
identity:
Laws (not enforced by type system):
- Preserve combine:
f(x.combine(y)) == f(x).combine(f(y)) - Preserve identity:
f(M::empty()) == N::empty()
§Example
use algebra::Max;
use algebra::Monoid;
use algebra::MonoidHom;
use algebra::Semigroup;
use algebra::SemigroupHom;
// A monoid homomorphism that widens Max<u32> to Max<u64>.
// This preserves identity because bottom for both is 0.
struct Widen;
impl SemigroupHom for Widen {
type Source = Max<u32>;
type Target = Max<u64>;
fn apply(&self, x: &Max<u32>) -> Max<u64> {
Max(x.0 as u64)
}
}
impl MonoidHom for Widen {}
// Verify identity preservation: f(⊥) = ⊥
let widen = Widen;
assert_eq!(widen.apply(&Max::<u32>::empty()), Max::<u64>::empty());
// Verify combine preservation
let a = Max(10u32);
let b = Max(20u32);
assert_eq!(
widen.apply(&a.combine(&b)),
widen.apply(&a).combine(&widen.apply(&b))
);