Expand description
§algebra-core — Core algebraic abstractions
Part of the postbox workspace
This crate provides fundamental algebraic structures as Rust traits:
Semigroup: associative binary operationMonoid: semigroup with identity elementCommutativeMonoid: monoid with commutative operationGroup: monoid with inverse elementsAbelianGroup: commutative groupSemigroupHom: structure-preserving map between semigroupsMonoidHom: structure-preserving map between monoidsJoinSemilattice: associative, commutative, idempotent operation (least upper bound)BoundedJoinSemilattice: join-semilattice with bottom elementMeetSemilattice: associative, commutative, idempotent operation (greatest lower bound)BoundedMeetSemilattice: meet-semilattice with top elementLattice: both join and meet operationsBoundedLattice: lattice with both bottom and top elements
§Recursion schemes
TypeApp: higher-kinded type encodingFunctor: functor on one type parameterFix: least fixed point (μF) for recursive data structuresfold: fold / catamorphism forFix(given an F-algebra)
§Quick start
use algebra_core::{Semigroup, Monoid, CommutativeMonoid, Sum};
// Sum<T> is provided by the library for addition monoids
let a = Sum(5);
let b = Sum(3);
let c = a.combine(&b);
assert_eq!(c, Sum(8));
assert_eq!(Sum::<i32>::empty(), Sum(0));§Standard library implementations
This crate provides implementations for common standard library types:
§Sets (union as join, intersection as meet)
HashSet<T>:JoinSemilattice,BoundedJoinSemilattice,MeetSemilattice,Lattice,Semigroup,Monoid,CommutativeMonoid- join = union, meet = intersection, bottom = ∅ (no top for arbitrary T)
BTreeSet<T>:JoinSemilattice,BoundedJoinSemilattice,MeetSemilattice,Lattice,Semigroup,Monoid,CommutativeMonoid
§Booleans (OR/AND lattice)
bool:JoinSemilattice,BoundedJoinSemilattice,MeetSemilattice,BoundedMeetSemilattice,BoundedLattice- join = OR, meet = AND, bottom = false, top = true
§Strings (concatenation)
String:Semigroup,Monoid
§Optional values (lifted monoid/lattice)
Option<M>(whereM: Semigroup + Clone):SemigroupOption<M>(whereM: Monoid + Clone):Monoid,SemigroupOption<M>(whereM: CommutativeMonoid + Clone):CommutativeMonoidOption<L>(whereL: JoinSemilattice + Clone):JoinSemilattice,BoundedJoinSemilatticeNoneis bottom,Some(a) ⊔ Some(b) = Some(a ⊔ b)
Option<L>(whereL: MeetSemilattice + Clone):MeetSemilatticeNone ⊓ x = None(bottom ⊓ anything = bottom)
Option<L>(whereL: BoundedMeetSemilattice + Clone):BoundedMeetSemilattice- top =
Some(L::top())
- top =
§Tuples (product algebras)
(): All traits (trivial implementations)(A,),(A, B),(A, B, C),(A, B, C, D):Semigroup,Monoid,CommutativeMonoid(componentwise)JoinSemilattice,BoundedJoinSemilattice(componentwise)MeetSemilattice,BoundedMeetSemilattice(componentwise)
All tuple implementations require component types to implement the corresponding trait, and operations are applied componentwise.
Re-exports§
Modules§
- fix
- Fixed-point types and recursion schemes
Structs§
Traits§
- Abelian
Group - An abelian group (commutative group): a group where combine is commutative.
- Bounded
Join Semilattice - A bounded join-semilattice: a join-semilattice with a bottom element.
- Bounded
Lattice - A bounded lattice: a lattice with both bottom and top elements.
- Bounded
Meet Semilattice - A bounded meet-semilattice: a meet-semilattice with a top element.
- Commutative
Monoid - A commutative monoid: a monoid where combine is commutative.
- Group
- A group: a monoid where every element has an inverse.
- Join
Semilattice - A join-semilattice: a type with an associative, commutative, idempotent binary operation.
- Lattice
- A lattice: a type with both join (least upper bound) and meet (greatest lower bound) operations.
- Meet
Semilattice - A meet-semilattice: a type with an associative, commutative, idempotent binary operation that computes greatest lower bounds.
- Monoid
- A monoid: a semigroup with an identity element.
- Monoid
Hom - A monoid homomorphism: a structure-preserving map between monoids.
- Monoid
HomFrom To - Helper trait for explicitly specifying source and target monoids.
- Semigroup
- A semigroup: a type with an associative binary operation.
- Semigroup
Hom - A semigroup homomorphism: a structure-preserving map between semigroups.
- Semigroup
HomFrom To - Helper trait for explicitly specifying source and target types.
Derive Macros§
- Abelian
Group - Derive macro for
AbelianGroup. - Bounded
Join Semilattice - Derive macro for
BoundedJoinSemilattice. - Bounded
Meet Semilattice - Derive macro for
BoundedMeetSemilattice. - Commutative
Monoid - Derive macro for
CommutativeMonoid. - Group
- Derive macro for
Group. - Join
Semilattice - Derive macro for
JoinSemilattice. - Meet
Semilattice - Derive macro for
MeetSemilattice. - Monoid
- Derive macro for
Monoid. - Semigroup
- Derive macro for
Semigroup.