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

basic-structures.mddocs/

Basic Algebraic Structures

The algebra library re-exports core algebraic type classes from cats-kernel, providing foundational mathematical structures like semigroups, monoids, and groups. These structures form the building blocks for more complex algebraic constructs.

Capabilities

Semigroup

An associative binary operation.

/**
 * Type alias to cats.kernel.Semigroup[A]
 * Provides an associative binary operation
 */
type Semigroup[A] = cats.kernel.Semigroup[A]
val Semigroup = cats.kernel.Semigroup

// Key operations available through cats-kernel:
// def combine(x: A, y: A): A
// def combineN(a: A, n: Int): A
// def combineAllOption(as: IterableOnce[A]): Option[A]

Usage Example:

import algebra._

val intSemigroup = Semigroup[Int]
val result = intSemigroup.combine(3, 5)  // 8

// For types with existing instances
val listSemigroup = Semigroup[List[Int]]
val combined = listSemigroup.combine(List(1, 2), List(3, 4))  // List(1, 2, 3, 4)

CommutativeSemigroup

A semigroup where the operation is commutative (order doesn't matter).

/**
 * Type alias to cats.kernel.CommutativeSemigroup[A]
 * Semigroup where combine(x, y) == combine(y, x)
 */
type CommutativeSemigroup[A] = cats.kernel.CommutativeSemigroup[A]
val CommutativeSemigroup = cats.kernel.CommutativeSemigroup

Monoid

A semigroup with an identity element.

/**
 * Type alias to cats.kernel.Monoid[A]
 * Semigroup with an identity element (empty)
 */
type Monoid[A] = cats.kernel.Monoid[A]
val Monoid = cats.kernel.Monoid

// Key operations available through cats-kernel:
// def empty: A
// def isEmpty(a: A)(implicit ev: Eq[A]): Boolean
// def combineAll(as: IterableOnce[A]): A

Usage Example:

import algebra._

val intMonoid = Monoid[Int]
val identity = intMonoid.empty  // 0
val sum = intMonoid.combineAll(List(1, 2, 3, 4, 5))  // 15

val stringMonoid = Monoid[String] 
val emptyString = stringMonoid.empty  // ""
val concatenated = stringMonoid.combineAll(List("Hello", " ", "World"))  // "Hello World"

CommutativeMonoid

A monoid where the operation is commutative.

/**
 * Type alias to cats.kernel.CommutativeMonoid[A]
 * Monoid where combine(x, y) == combine(y, x)
 */
type CommutativeMonoid[A] = cats.kernel.CommutativeMonoid[A]
val CommutativeMonoid = cats.kernel.CommutativeMonoid

Group

A monoid with inverse elements.

/**
 * Type alias to cats.kernel.Group[A]
 * Monoid with inverse operation
 */
type Group[A] = cats.kernel.Group[A]
val Group = cats.kernel.Group

// Key operations available through cats-kernel:
// def inverse(a: A): A
// def remove(a: A, b: A): A  // equivalent to combine(a, inverse(b))

Usage Example:

import algebra._

val intGroup = Group[Int]
val inverse = intGroup.inverse(5)  // -5
val difference = intGroup.remove(10, 3)  // 7 (equivalent to 10 + (-3))

CommutativeGroup

A group where the operation is commutative.

/**
 * Type alias to cats.kernel.CommutativeGroup[A]
 * Group where combine(x, y) == combine(y, x)
 */
type CommutativeGroup[A] = cats.kernel.CommutativeGroup[A]
val CommutativeGroup = cats.kernel.CommutativeGroup

Band

A semigroup where every element is idempotent.

/**
 * Type alias to cats.kernel.Band[A]
 * Semigroup where combine(x, x) == x for all x
 */
type Band[A] = cats.kernel.Band[A]
val Band = cats.kernel.Band

Semilattice

A commutative band (commutative and idempotent).

/**
 * Type alias to cats.kernel.Semilattice[A]
 * Commutative band - both commutative and idempotent
 */
type Semilattice[A] = cats.kernel.Semilattice[A]
val Semilattice = cats.kernel.Semilattice

BoundedSemilattice

A semilattice with a bound (identity element).

/**
 * Type alias to cats.kernel.BoundedSemilattice[A]
 * Semilattice with an identity element
 */
type BoundedSemilattice[A] = cats.kernel.BoundedSemilattice[A]
val BoundedSemilattice = cats.kernel.BoundedSemilattice

Comparison Types

Eq

Type-safe equality testing.

/**
 * Type alias to cats.kernel.Eq[A]
 * Type-safe equality comparison
 */
type Eq[A] = cats.kernel.Eq[A]
val Eq = cats.kernel.Eq

// Key operations available through cats-kernel:
// def eqv(x: A, y: A): Boolean
// def neqv(x: A, y: A): Boolean

PartialOrder

Partial ordering relation.

/**
 * Type alias to cats.kernel.PartialOrder[A]
 * Partial ordering where not all elements are comparable
 */
type PartialOrder[A] = cats.kernel.PartialOrder[A]
val PartialOrder = cats.kernel.PartialOrder

// Key operations available through cats-kernel:
// def partialCompare(x: A, y: A): Double
// def tryCompare(x: A, y: A): Option[Int]
// def pmin(x: A, y: A): Option[A]
// def pmax(x: A, y: A): Option[A]

Order

Total ordering relation.

/**
 * Type alias to cats.kernel.Order[A]
 * Total ordering where all elements are comparable
 */
type Order[A] = cats.kernel.Order[A]
val Order = cats.kernel.Order

// Key operations available through cats-kernel:
// def compare(x: A, y: A): Int
// def min(x: A, y: A): A
// def max(x: A, y: A): A
// def comparison(x: A, y: A): Comparison

Usage Example:

import algebra._

val intOrder = Order[Int]
val comparison = intOrder.compare(5, 3)  // 1 (positive, since 5 > 3)
val minimum = intOrder.min(5, 3)  // 3
val maximum = intOrder.max(5, 3)  // 5

Standard Instances

All basic algebraic structures have instances provided for Scala's standard types:

  • Numeric types: Byte, Short, Int, Long, Float, Double, BigInt, BigDecimal
  • Other types: Boolean, Char, String, Unit
  • Collections: List, Vector, Set, Map, Option
  • Tuples: (A, B), (A, B, C), etc. (when component types have instances)

These instances are available through the algebra.instances.all._ import or specific imports like algebra.instances.int._.