Bounded Semilattice
BoundedSemilattice[A] is a commutative and idempotent monoid,
a semilattice with an identity element.
Laws of a bounded semilattice:
- Closure (closure): for \(\forall x, y \in CM\), \(x + y \in CM\).
- Associativity (associativity): for \(\forall x, y, z \in CM\), \((x + y) + z = x + (y + z)\).
- Identity (identity): there exists \(\exists e \in CM\) such that for \(\forall x \in CM\), \(e + x = x + e = x\)
- Commutativity (commutative): for \(\forall x, y \in CM\), \(x + y = y + x\).
- Idempotency (idempotency): for \(\forall x \in B\), \(x + x = x\).
Code
trait BoundedSemilattice[A] extends Semilattice[A], IdempotentMonoid[A], CMonoid[A]
Diagram
classDiagram
class Semigroup~A~{
+combine(x: A, y: A) A
}
class Monoid~A~{
+empty() A
}
Semigroup <|-- Monoid
class CommutativeSemigroup~A~
Semigroup <|-- CommutativeSemigroup
class CommutativeMonoid~A~
Monoid <|-- CommutativeMonoid
CommutativeSemigroup <|-- CommutativeMonoid
class IdempotentSemigroup~A~
Semigroup <|-- IdempotentSemigroup
class IdempotentMonoid~A~
Monoid <|-- IdempotentMonoid
IdempotentSemigroup <|-- IdempotentMonoid
class Semilattice~A~
CommutativeSemigroup <|-- Semilattice
IdempotentSemigroup <|-- Semilattice
class BoundedSemilattice~A~
CommutativeMonoid <|-- BoundedSemilattice
IdempotentMonoid <|-- BoundedSemilattice
Semilattice <|-- BoundedSemilattice
Implementation in Libraries
Implementation in Cats
import cats.kernel.BoundedSemilattice
import cats.implicits.*
BoundedSemilattice.combine(Set(1, 2), BoundedSemilattice[Set[Int]].empty)
// Set(1, 2)
References: