Type classes for algebraic structures including basic algebraic structures, ring-like structures, and lattice-like structures in functional programming
npx @tessl/cli install tessl/maven-org-typelevel--algebra_2-13@2.13.0Algebra is a comprehensive Scala library providing type classes for representing algebraic structures in functional programming. It enables developers to abstract over mathematical structures like semigroups, monoids, groups, rings, fields, and lattices through a systematic type class hierarchy. The library offers basic algebraic structures with properties like associativity and commutativity, ring-like structures combining additive and multiplicative operations with proper mathematical laws, and lattice-like structures for representing bounded partially ordered sets with meet and join operations.
libraryDependencies += "org.typelevel" %% "algebra" % "2.13.0"Basic algebraic structures (re-exported from cats-kernel):
import algebra._
// Imports: Semigroup, Monoid, Group, CommutativeGroup, etc.Ring structures:
import algebra.ring._
// Imports: Ring, Field, Semiring, etc.Lattice structures:
import algebra.lattice._
// Imports: Lattice, Bool, Heyting, etc.Type class instances:
import algebra.instances.all._
// Imports all standard type instancesimport algebra._
import algebra.ring._
import algebra.lattice._
import algebra.instances.all._
// Using basic algebraic structures
val intMonoid = Monoid[Int]
val sum = intMonoid.combine(5, 3) // 8
// Using ring operations
val intRing = Ring[Int]
val result = intRing.plus(intRing.times(3, 4), 2) // 14
val fromInt = intRing.fromInt(42) // 42
// Using field operations for rational numbers
import spire.math.Rational
val ratField = Field[Rational]
val quotient = ratField.div(Rational(3, 4), Rational(1, 2)) // 3/2
// Using lattice operations
val boolLattice = Bool[Boolean]
val conjunction = boolLattice.and(true, false) // false
val disjunction = boolLattice.or(true, false) // trueAlgebra is built around several key type class hierarchies:
The library follows mathematical conventions and laws, ensuring type-safe abstractions over algebraic structures.
Core algebraic type classes re-exported from cats-kernel, including semigroups, monoids, and groups with their commutative variants.
// Type aliases to cats-kernel structures
type Semigroup[A] = cats.kernel.Semigroup[A]
val Semigroup = cats.kernel.Semigroup
type Monoid[A] = cats.kernel.Monoid[A]
val Monoid = cats.kernel.Monoid
type Group[A] = cats.kernel.Group[A]
val Group = cats.kernel.Group
type CommutativeGroup[A] = cats.kernel.CommutativeGroup[A]
val CommutativeGroup = cats.kernel.CommutativeGroupRing-like algebraic structures combining additive and multiplicative operations with distributivity laws, from basic semirings to advanced structures like fields and Euclidean rings.
trait Ring[A] extends Rig[A] with Rng[A] {
def fromInt(n: Int): A
def fromBigInt(n: BigInt): A
}
trait Field[A] extends EuclideanRing[A] with DivisionRing[A] with CommutativeSemifield[A] {
def fromDouble(a: Double): A
def gcd(a: A, b: A)(implicit eqA: Eq[A]): A
def lcm(a: A, b: A)(implicit eqA: Eq[A]): A
}Lattice-like structures for representing bounded partially ordered sets with meet and join operations, including Boolean algebras and Heyting algebras for logical operations.
trait Lattice[A] extends JoinSemilattice[A] with MeetSemilattice[A] {
def dual: Lattice[A]
}
trait Bool[A] extends Heyting[A] with GenBool[A] {
def and(a: A, b: A): A
def or(a: A, b: A): A
def not(a: A): A
def xor(a: A, b: A): A
}Comprehensive type class instances for Scala's standard types, providing algebraic structure implementations for primitives, collections, and other common types.
// Instance aggregation trait
trait AllInstances extends
ArrayInstances with BigDecimalInstances with BigIntInstances with
BooleanInstances with ByteInstances with CharInstances with
DoubleInstances with FloatInstances with IntInstances with
ListInstances with LongInstances with MapInstances with
OptionInstances with SetInstances with ShortInstances with
StringInstances with TupleInstances with UnitInstancesAdvanced implicit resolution system for composable type class derivation, allowing fine-grained control over instance selection and fallback behavior.
sealed trait Priority[+P, +F] {
def fold[Z](f: P => Z, g: F => Z): Z
def toEither: Either[F, P]
def isPreferred: Boolean
def isFallback: Boolean
}
case class Preferred[P](get: P) extends Priority[P, Nothing]
case class Fallback[F](get: F) extends Priority[Nothing, F]// Basic algebraic structures (re-exported from cats-kernel)
type Band[A] = cats.kernel.Band[A]
type BoundedSemilattice[A] = cats.kernel.BoundedSemilattice[A]
type CommutativeGroup[A] = cats.kernel.CommutativeGroup[A]
type CommutativeMonoid[A] = cats.kernel.CommutativeMonoid[A]
type CommutativeSemigroup[A] = cats.kernel.CommutativeSemigroup[A]
type Eq[A] = cats.kernel.Eq[A]
type Group[A] = cats.kernel.Group[A]
type Monoid[A] = cats.kernel.Monoid[A]
type Order[A] = cats.kernel.Order[A]
type PartialOrder[A] = cats.kernel.PartialOrder[A]
type Semigroup[A] = cats.kernel.Semigroup[A]
type Semilattice[A] = cats.kernel.Semilattice[A]// Sign enumeration for signed types
object Sign {
case object Zero extends Sign
case object Positive extends Sign
case object Negative extends Sign
}