The algebra library provides comprehensive type class instances for Scala's standard types, enabling algebraic operations on primitives, collections, and other common types. These instances implement the algebraic structures defined in the type class hierarchy.
Aggregates all type class instances into a single trait.
/**
* Trait that aggregates all type class instances
* Mix this in or import algebra.instances.all._ to get all instances
*/
trait AllInstances extends
ArrayInstances with BigDecimalInstances with BigIntInstances with
BitSetInstances 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
UnitInstances
// Package object provides direct access
package object all extends AllInstancesUsage:
import algebra.instances.all._
// Now all instances are available
val intRing = Ring[Int]
val stringMonoid = Monoid[String]
val boolLattice = Bool[Boolean]All integer types provide CommutativeRing instances and bounded distributive lattices.
// Byte instances
trait ByteInstances extends cats.kernel.instances.ByteInstances {
implicit val byteAlgebra: CommutativeRing[Byte] with Signed[Byte]
implicit val ByteMinMaxLattice: BoundedDistributiveLattice[Byte]
}
// Short instances
trait ShortInstances extends cats.kernel.instances.ShortInstances {
implicit val shortAlgebra: CommutativeRing[Short] with Signed[Short]
implicit val ShortMinMaxLattice: BoundedDistributiveLattice[Short]
}
// Int instances
trait IntInstances extends cats.kernel.instances.IntInstances {
implicit val intAlgebra: CommutativeRing[Int] with Signed[Int] with TruncatedDivision[Int]
implicit val IntMinMaxLattice: BoundedDistributiveLattice[Int]
}
// Long instances
trait LongInstances extends cats.kernel.instances.LongInstances {
implicit val longAlgebra: CommutativeRing[Long] with Signed[Long] with TruncatedDivision[Long]
implicit val LongMinMaxLattice: BoundedDistributiveLattice[Long]
}Usage Example:
import algebra.ring._
import algebra.lattice._
import algebra.instances.all._
val intRing = Ring[Int]
val sum = intRing.plus(3, 4) // 7
val product = intRing.times(3, 4) // 12
val intLattice = Lattice[Int]
val min = intLattice.meet(5, 3) // 3
val max = intLattice.join(5, 3) // 5
val signed = Signed[Int]
val abs = signed.abs(-42) // 42
val sign = signed.sign(-42) // Sign.NegativeFloating point types provide Field instances.
// Float instances
trait FloatInstances extends cats.kernel.instances.FloatInstances {
implicit val floatAlgebra: Field[Float] with Signed[Float]
}
// Double instances
trait DoubleInstances extends cats.kernel.instances.DoubleInstances {
implicit val doubleAlgebra: Field[Double] with Signed[Double]
}Usage Example:
import algebra.ring._
import algebra.instances.all._
val doubleField = Field[Double]
val quotient = doubleField.div(15.0, 3.0) // 5.0
val fromInt = doubleField.fromInt(42) // 42.0
val fromDouble = doubleField.fromDouble(3.14159) // 3.14159
val reciprocal = doubleField.reciprocal(2.0) // 0.5// BigInt instances
trait BigIntInstances extends cats.kernel.instances.BigIntInstances {
implicit val bigIntAlgebra: EuclideanRing[BigInt] with Signed[BigInt]
}
// BigDecimal instances
trait BigDecimalInstances extends cats.kernel.instances.BigDecimalInstances {
implicit val bigDecimalAlgebra: Field[BigDecimal] with Signed[BigDecimal]
}Usage Example:
import algebra.ring._
import algebra.instances.all._
val bigIntRing = EuclideanRing[BigInt]
val a = BigInt("123456789012345678901234567890")
val b = BigInt("987654321098765432109876543210")
val gcd = bigIntRing.gcd(a, b)
val bigDecimalField = Field[BigDecimal]
val precise = BigDecimal("1.23456789012345678901234567890")
val doubled = bigDecimalField.plus(precise, precise)Boolean provides a complete Boolean algebra.
/**
* Boolean instances providing complete Boolean algebra
*/
trait BooleanInstances extends cats.kernel.instances.BooleanInstances {
implicit val booleanAlgebra: Bool[Boolean] with CommutativeRig[Boolean]
}Usage Example:
import algebra.lattice._
import algebra.instances.all._
val bool = Bool[Boolean]
val and = bool.and(true, false) // false
val or = bool.or(true, false) // true
val not = bool.complement(true) // false
val xor = bool.xor(true, false) // true
val implies = bool.imp(true, false) // false/**
* Character instances with basic ordering
*/
trait CharInstances extends cats.kernel.instances.CharInstances {
implicit val charMinMaxLattice: BoundedDistributiveLattice[Char]
}String provides monoid structure via concatenation.
/**
* String instances with concatenation monoid
*/
trait StringInstances extends cats.kernel.instances.StringInstances {
// Inherits Monoid[String] from cats-kernel where:
// empty = ""
// combine(a, b) = a + b
}Usage Example:
import algebra._
import algebra.instances.all._
val stringMonoid = Monoid[String]
val empty = stringMonoid.empty // ""
val combined = stringMonoid.combine("Hello", " World") // "Hello World"
val all = stringMonoid.combineAll(List("A", "B", "C")) // "ABC"Unit provides trivial algebraic structures.
/**
* Unit instances - trivial structures with single element
*/
trait UnitInstances extends cats.kernel.instances.UnitInstances {
implicit val unitAlgebra: CommutativeGroup[Unit] with BoundedDistributiveLattice[Unit]
}Lists provide monoid structure via concatenation.
/**
* List instances with concatenation monoid
*/
trait ListInstances extends cats.kernel.instances.ListInstances {
// Inherits Monoid[List[A]] from cats-kernel where:
// empty = Nil
// combine(as, bs) = as ++ bs
}Usage Example:
import algebra._
import algebra.instances.all._
val listMonoid = Monoid[List[Int]]
val empty = listMonoid.empty // Nil
val combined = listMonoid.combine(List(1, 2), List(3, 4)) // List(1, 2, 3, 4)
val flattened = listMonoid.combineAll(List(List(1), List(2, 3), List(4, 5))) // List(1, 2, 3, 4, 5)Sets provide semilattice structure via union.
/**
* Set instances with union semilattice
*/
trait SetInstances extends cats.kernel.instances.SetInstances {
// Provides Semilattice[Set[A]] where combine is union
implicit def setLattice[A]: Lattice[Set[A]] // meet = intersection, join = union
}Usage Example:
import algebra.lattice._
import algebra.instances.all._
val setLattice = Lattice[Set[Int]]
val set1 = Set(1, 2, 3)
val set2 = Set(2, 3, 4)
val union = setLattice.join(set1, set2) // Set(1, 2, 3, 4)
val intersection = setLattice.meet(set1, set2) // Set(2, 3)Maps provide monoid structure via key-wise combination.
/**
* Map instances with key-wise combination
*/
trait MapInstances extends cats.kernel.instances.MapInstances {
// Inherits Monoid[Map[K, V]] when V has Semigroup
// empty = Map.empty
// combine merges maps, combining values with V's semigroup
}Usage Example:
import algebra._
import algebra.instances.all._
val mapMonoid = Monoid[Map[String, Int]]
val map1 = Map("a" -> 1, "b" -> 2)
val map2 = Map("b" -> 3, "c" -> 4)
val combined = mapMonoid.combine(map1, map2) // Map("a" -> 1, "b" -> 5, "c" -> 4)Arrays provide various algebraic structures.
/**
* Array instances
* Note: Arrays are mutable, so instances may have different semantics
*/
trait ArrayInstances {
// Provides instances for Array[A] when A has appropriate structure
}BitSet provides generalized Boolean algebra.
/**
* BitSet instances with Boolean algebra operations
*/
trait BitSetInstances {
implicit val bitSetBoolAlgebra: GenBool[scala.collection.immutable.BitSet]
// join = union, meet = intersection, without = difference
}Usage Example:
import algebra.lattice._
import algebra.instances.all._
import scala.collection.immutable.BitSet
val bitSetAlgebra = GenBool[BitSet]
val set1 = BitSet(1, 2, 3)
val set2 = BitSet(2, 3, 4)
val union = bitSetAlgebra.or(set1, set2) // BitSet(1, 2, 3, 4)
val intersection = bitSetAlgebra.and(set1, set2) // BitSet(2, 3)
val difference = bitSetAlgebra.without(set1, set2) // BitSet(1)
val xor = bitSetAlgebra.xor(set1, set2) // BitSet(1, 4)Options provide various structures based on underlying type.
/**
* Option instances that lift underlying structures
*/
trait OptionInstances extends cats.kernel.instances.OptionInstances {
// When A has Semigroup: Option[A] has Monoid with None as empty
// When A has Lattice: Option[A] has Lattice with None as bottom
}Usage Example:
import algebra._
import algebra.lattice._
import algebra.instances.all._
val optionMonoid = Monoid[Option[Int]]
val some5 = Some(5)
val some3 = Some(3)
val none = None
val combined = optionMonoid.combine(some5, some3) // Some(8)
val withNone = optionMonoid.combine(some5, none) // Some(5)
val empty = optionMonoid.empty // None
val optionLattice = Lattice[Option[Int]]
val meet = optionLattice.meet(some5, some3) // Some(3) (min)
val join = optionLattice.join(some5, none) // Some(5)Tuples provide component-wise algebraic structures.
/**
* Tuple instances with component-wise operations
*/
trait TupleInstances extends cats.kernel.instances.TupleInstances {
// When A has Semigroup and B has Semigroup: (A, B) has Semigroup
// When A has Monoid and B has Monoid: (A, B) has Monoid
// When A has Ring and B has Ring: (A, B) has Ring
// etc.
}Usage Example:
import algebra._
import algebra.ring._
import algebra.instances.all._
val tupleMonoid = Monoid[(Int, String)]
val tuple1 = (3, "Hello")
val tuple2 = (4, " World")
val combined = tupleMonoid.combine(tuple1, tuple2) // (7, "Hello World")
val empty = tupleMonoid.empty // (0, "")
val tupleRing = Ring[(Int, Int)]
val ring1 = (2, 3)
val ring2 = (4, 5)
val sum = tupleRing.plus(ring1, ring2) // (6, 8)
val product = tupleRing.times(ring1, ring2) // (8, 15)Provides utility functions for instance implementations.
/**
* Static utility methods used by instance implementations
*/
object StaticMethods {
/** Efficient exponentiation for Long values */
def pow(base: Long, exponent: Long): Long
}The library uses Scala's implicit resolution with careful priority control:
IntInstances) have higher priorityBest Practices:
// Import all instances (recommended for most use cases)
import algebra.instances.all._
// Or import specific instances for fine control
import algebra.instances.int._
import algebra.instances.string._
// Access instances explicitly if needed
val intRing = Ring[Int]
val stringMonoid = Monoid[String]This comprehensive instance system enables algebraic programming across Scala's type system while maintaining mathematical correctness and performance.