Ограниченная полурешетка
BoundedSemilattice[A] - коммутативный и идемпотентный моноид,
решетка с нейтральным элементом.
Законы ограниченной полурешетки:
- Замыкание (closure): для \(\forall x, y \in CM\) выполняется \(x + y \in CM\).
- Ассоциативность (associativity): для \(\forall x, y, z \in CM\) выполняется \((x + y) + z = x + (y + z)\).
- Тождественность или существования нейтрального элемента (identity): существует \(\exists e \in CM\) такой, что для \(\forall x \in CM\) выполняется \(e + x = x + e = x\)
- Коммутативность (commutative): для \(\forall x, y \in CM\) выполняется \(x + y = y + x\).
- Идемпотентность (idempotency): для \(\forall x \in B\) выполняется \(x + x = x\).
Код
trait BoundedSemilattice[A] extends Semilattice[A], IdempotentMonoid[A], CMonoid[A]
Схема
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
Реализация в библиотеках
Реализация в Cats
import cats.kernel.BoundedSemilattice
import cats.implicits.*
BoundedSemilattice.combine(Set(1, 2), BoundedSemilattice[Set[Int]].empty)
// Set(1, 2)
Ссылки: