or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

basic-structures.mdindex.mdinstances.mdlattice-structures.mdpriority-system.mdring-structures.md
tile.json

tessl/maven-org-typelevel--algebra_2-13

Type classes for algebraic structures including basic algebraic structures, ring-like structures, and lattice-like structures in functional programming

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/org.typelevel/algebra_2.13@2.13.x

To install, run

npx @tessl/cli install tessl/maven-org-typelevel--algebra_2-13@2.13.0

index.mddocs/

Algebra

Algebra 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.

Package Information

  • Package Name: org.typelevel:algebra_2.13
  • Package Type: Maven
  • Language: Scala
  • Installation: libraryDependencies += "org.typelevel" %% "algebra" % "2.13.0"

Core Imports

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 instances

Basic Usage

import 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)   // true

Architecture

Algebra is built around several key type class hierarchies:

  • Basic Structures: Core algebraic concepts (Semigroup, Monoid, Group) re-exported from cats-kernel
  • Ring Hierarchy: Additive and multiplicative structures combined with distributivity laws
  • Lattice Hierarchy: Order-theoretic structures with meet and join operations
  • Instances System: Comprehensive type class instances for Scala's standard types
  • Priority System: Advanced implicit resolution for composable type class derivation

The library follows mathematical conventions and laws, ensuring type-safe abstractions over algebraic structures.

Capabilities

Basic 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.CommutativeGroup

Basic Structures

Ring Structures

Ring-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
}

Ring Structures

Lattice Structures

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
}

Lattice Structures

Type Class Instances

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 UnitInstances

Type Class Instances

Priority System

Advanced 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]

Priority System

Types

Core Type Aliases

// 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 Type

// Sign enumeration for signed types
object Sign {
  case object Zero extends Sign
  case object Positive extends Sign  
  case object Negative extends Sign
}