From 048ce2ef1364873488057d7a6a53cb42d0768c37 Mon Sep 17 00:00:00 2001 From: Arman Bilge Date: Sat, 15 May 2021 13:05:37 -0700 Subject: [PATCH] Implement algebra#108 --- .../src/main/scala/cats/algebra/lattice/Bool.scala | 14 +------------- .../lattice/BoundedDistributiveLattice.scala | 13 ------------- .../main/scala/cats/algebra/lattice/GenBool.scala | 11 ++--------- .../test/scala/cats/algebra/laws/LawTests.scala | 4 ++-- 4 files changed, 5 insertions(+), 37 deletions(-) diff --git a/algebra-core/src/main/scala/cats/algebra/lattice/Bool.scala b/algebra-core/src/main/scala/cats/algebra/lattice/Bool.scala index f2aaf3f7ef..408916ec34 100644 --- a/algebra-core/src/main/scala/cats/algebra/lattice/Bool.scala +++ b/algebra-core/src/main/scala/cats/algebra/lattice/Bool.scala @@ -34,17 +34,6 @@ trait Bool[@sp(Int, Long) A] extends Any with Heyting[A] with GenBool[A] { self or(without(a, b), without(b, a)) override def dual: Bool[A] = new DualBool(this) - - /** - * Every Boolean algebra is a BoolRing, with multiplication defined as - * `and` and addition defined as `xor`. Bool does not extend BoolRing - * because, e.g. we might want a Bool[Int] and CommutativeRing[Int] to - * refer to different structures, by default. - * - * Note that the ring returned by this method is not an extension of - * the `Rig` returned from `BoundedDistributiveLattice.asCommutativeRig`. - */ - override def asBoolRing: BoolRing[A] = new BoolRingFromBool(self) } class DualBool[@sp(Int, Long) A](orig: Bool[A]) extends Bool[A] { @@ -63,7 +52,7 @@ class DualBool[@sp(Int, Long) A](orig: Bool[A]) extends Bool[A] { override def dual: Bool[A] = orig } -private[lattice] class BoolRingFromBool[A](orig: Bool[A]) extends BoolRngFromGenBool(orig) with BoolRing[A] { +class BoolRingFromBool[A](orig: Bool[A]) extends BoolRngFromGenBool(orig) with BoolRing[A] { def one: A = orig.one } @@ -79,7 +68,6 @@ class BoolFromBoolRing[A](orig: BoolRing[A]) extends GenBoolFromBoolRng(orig) wi def one: A = orig.one def complement(a: A): A = orig.plus(orig.one, a) override def without(a: A, b: A): A = super[GenBoolFromBoolRng].without(a, b) - override def asBoolRing: BoolRing[A] = orig override def meet(a: A, b: A): A = super[GenBoolFromBoolRng].meet(a, b) override def join(a: A, b: A): A = super[GenBoolFromBoolRng].join(a, b) diff --git a/algebra-core/src/main/scala/cats/algebra/lattice/BoundedDistributiveLattice.scala b/algebra-core/src/main/scala/cats/algebra/lattice/BoundedDistributiveLattice.scala index 2c93ae7057..3aaac45764 100644 --- a/algebra-core/src/main/scala/cats/algebra/lattice/BoundedDistributiveLattice.scala +++ b/algebra-core/src/main/scala/cats/algebra/lattice/BoundedDistributiveLattice.scala @@ -3,7 +3,6 @@ package algebra package lattice import scala.{specialized => sp} -import cats.algebra.ring.CommutativeRig /** * A bounded distributive lattice is a lattice that both bounded and distributive @@ -13,18 +12,6 @@ trait BoundedDistributiveLattice[@sp(Int, Long, Float, Double) A] with BoundedLattice[A] with DistributiveLattice[A] { self => - /** - * Return a CommutativeRig using join and meet. Note this must obey the commutative rig laws since - * meet(a, one) = a, and meet and join are associative, commutative and distributive. - */ - def asCommutativeRig: CommutativeRig[A] = - new CommutativeRig[A] { - def zero: A = self.zero - def one: A = self.one - def plus(x: A, y: A): A = self.join(x, y) - def times(x: A, y: A): A = self.meet(x, y) - } - override def dual: BoundedDistributiveLattice[A] = new BoundedDistributiveLattice[A] { def meet(a: A, b: A) = self.join(a, b) def join(a: A, b: A) = self.meet(a, b) diff --git a/algebra-core/src/main/scala/cats/algebra/lattice/GenBool.scala b/algebra-core/src/main/scala/cats/algebra/lattice/GenBool.scala index 25b2be53b8..101bd8424a 100644 --- a/algebra-core/src/main/scala/cats/algebra/lattice/GenBool.scala +++ b/algebra-core/src/main/scala/cats/algebra/lattice/GenBool.scala @@ -30,12 +30,6 @@ trait GenBool[@sp(Int, Long) A] extends Any with DistributiveLattice[A] with Bou * Defined as `a\b ∨ b\a`. */ def xor(a: A, b: A): A = or(without(a, b), without(b, a)) - - /** - * Every generalized Boolean algebra is also a `BoolRng`, with - * multiplication defined as `and` and addition defined as `xor`. - */ - def asBoolRing: BoolRng[A] = new BoolRngFromGenBool(self) } /** @@ -48,17 +42,16 @@ trait GenBool[@sp(Int, Long) A] extends Any with DistributiveLattice[A] with Bou * * `BoolRng.asBool.asBoolRing` gives back the original `BoolRng`. * - * @see [[algebra.lattice.GenBool.asBoolRing]] + * @see [[algebra.lattice.BoolRngFromGenBool]] */ class GenBoolFromBoolRng[A](orig: BoolRng[A]) extends GenBool[A] { def zero: A = orig.zero def and(a: A, b: A): A = orig.times(a, b) def or(a: A, b: A): A = orig.plus(orig.plus(a, b), orig.times(a, b)) def without(a: A, b: A): A = orig.plus(a, orig.times(a, b)) - override def asBoolRing: BoolRng[A] = orig } -private[lattice] class BoolRngFromGenBool[@sp(Int, Long) A](orig: GenBool[A]) extends BoolRng[A] { +class BoolRngFromGenBool[@sp(Int, Long) A](orig: GenBool[A]) extends BoolRng[A] { def zero: A = orig.zero def plus(x: A, y: A): A = orig.xor(x, y) def times(x: A, y: A): A = orig.and(x, y) diff --git a/algebra-laws/shared/src/test/scala/cats/algebra/laws/LawTests.scala b/algebra-laws/shared/src/test/scala/cats/algebra/laws/LawTests.scala index 815b880e13..c5bfb57b1b 100644 --- a/algebra-laws/shared/src/test/scala/cats/algebra/laws/LawTests.scala +++ b/algebra-laws/shared/src/test/scala/cats/algebra/laws/LawTests.scala @@ -59,7 +59,7 @@ class LawTests extends munit.DisciplineSuite { checkAll("Boolean", RingLaws[Boolean].boolRing(booleanRing)) // ensure that Bool[A].asBoolRing is a valid BoolRing - checkAll("Boolean-ring-from-bool", RingLaws[Boolean].boolRing(Bool[Boolean].asBoolRing)) + checkAll("Boolean-ring-from-bool", RingLaws[Boolean].boolRing(new BoolRingFromBool[Boolean](Bool[Boolean]))) // ensure that BoolRing[A].asBool is a valid Bool checkAll("Boolean- bool-from-ring", LogicLaws[Boolean].bool(new BoolFromBoolRing(booleanRing))) @@ -90,7 +90,7 @@ class LawTests extends munit.DisciplineSuite { checkAll("Set[Byte]", LogicLaws[Set[Byte]].generalizedBool) checkAll("Set[Byte]", RingLaws[Set[Byte]].boolRng(setBoolRng[Byte])) checkAll("Set[Byte]-bool-from-rng", LogicLaws[Set[Byte]].generalizedBool(new GenBoolFromBoolRng(setBoolRng))) - checkAll("Set[Byte]-rng-from-bool", RingLaws[Set[Byte]].boolRng(GenBool[Set[Byte]].asBoolRing)) + checkAll("Set[Byte]-rng-from-bool", RingLaws[Set[Byte]].boolRng(new BoolRngFromGenBool(GenBool[Set[Byte]]))) checkAll("Set[Int]", PartialOrderTests[Set[Int]].partialOrder) checkAll("Set[Int]", RingLaws[Set[Int]].semiring) checkAll("Set[String]", RingLaws[Set[String]].semiring)