diff --git a/build.sbt b/build.sbt index b8f0ca2604..9c9122c794 100644 --- a/build.sbt +++ b/build.sbt @@ -391,7 +391,11 @@ def mimaSettings(moduleName: String) = Seq( exclude[InheritedNewAbstractMethodProblem]("cats.instances.Function1Instances0.catsStdDistributiveForFunction1"), exclude[InheritedNewAbstractMethodProblem]("cats.instances.Function1Instances0.catsStdDistributiveForFunction1"), exclude[ReversedMissingMethodProblem]("cats.kernel.instances.MapInstances.catsKernelStdCommutativeMonoidForMap"), - exclude[IncompatibleResultTypeProblem]("cats.data.KleisliInstances0.catsDataMonadForKleisliId") + exclude[IncompatibleResultTypeProblem]("cats.data.KleisliInstances0.catsDataMonadForKleisliId"), + exclude[InheritedNewAbstractMethodProblem]("cats.kernel.PartialOrderToPartialOrderingConversion.catsKernelPartialOrderingForPartialOrder"), + exclude[InheritedNewAbstractMethodProblem]("cats.kernel.EqToEquivConversion.catsKernelEquivForEq"), + exclude[InheritedNewAbstractMethodProblem]("cats.kernel.OrderToOrderingConversion.catsKernelOrderingForOrder"), + exclude[InheritedNewAbstractMethodProblem]("cats.kernel.HashToHashingConversion.catsKernelHashToHashing") ) } ) diff --git a/core/src/main/scala/cats/instances/eq.scala b/core/src/main/scala/cats/instances/eq.scala index dc304686d5..364c1962d7 100644 --- a/core/src/main/scala/cats/instances/eq.scala +++ b/core/src/main/scala/cats/instances/eq.scala @@ -1,7 +1,7 @@ package cats package instances -trait EqInstances { +trait EqInstances extends kernel.instances.EqInstances { implicit val catsContravariantMonoidalForEq: ContravariantMonoidal[Eq] = new ContravariantMonoidal[Eq] { /** diff --git a/core/src/main/scala/cats/instances/hash.scala b/core/src/main/scala/cats/instances/hash.scala index 443ef96e39..2401313148 100644 --- a/core/src/main/scala/cats/instances/hash.scala +++ b/core/src/main/scala/cats/instances/hash.scala @@ -2,7 +2,7 @@ package cats package instances -trait HashInstances { +trait HashInstances extends kernel.instances.HashInstances { implicit val catsContravariantForHash: Contravariant[Hash] = new Contravariant[Hash] { diff --git a/core/src/main/scala/cats/instances/order.scala b/core/src/main/scala/cats/instances/order.scala index 8a239d264c..1e44c3f932 100644 --- a/core/src/main/scala/cats/instances/order.scala +++ b/core/src/main/scala/cats/instances/order.scala @@ -1,7 +1,7 @@ package cats package instances -trait OrderInstances extends cats.kernel.OrderToOrderingConversion { +trait OrderInstances extends kernel.instances.OrderInstances { implicit val catsContravariantMonoidalForOrder: ContravariantMonoidal[Order] = new ContravariantMonoidal[Order] { /** diff --git a/core/src/main/scala/cats/instances/partialOrder.scala b/core/src/main/scala/cats/instances/partialOrder.scala index 671e0b82cf..cfdab8db32 100644 --- a/core/src/main/scala/cats/instances/partialOrder.scala +++ b/core/src/main/scala/cats/instances/partialOrder.scala @@ -1,7 +1,7 @@ package cats package instances -trait PartialOrderInstances { +trait PartialOrderInstances extends kernel.instances.PartialOrderInstances { implicit val catsContravariantSemigroupalForPartialOrder: ContravariantSemigroupal[PartialOrder] = new ContravariantSemigroupal[PartialOrder] { /** Derive a `PartialOrder` for `B` given a `PartialOrder[A]` and a function `B => A`. diff --git a/kernel-laws/src/test/scala/cats/kernel/laws/LawTests.scala b/kernel-laws/src/test/scala/cats/kernel/laws/LawTests.scala index 94aec53ea3..b5cf16a036 100644 --- a/kernel-laws/src/test/scala/cats/kernel/laws/LawTests.scala +++ b/kernel-laws/src/test/scala/cats/kernel/laws/LawTests.scala @@ -243,6 +243,21 @@ class Tests extends FunSuite with Discipline { checkAll("subsetPartialOrder[Int]", PartialOrderTests(subsetPartialOrder[Int]).partialOrder) + { + implicit def subsetPartialOrdering[A]: PartialOrdering[Set[A]] = new PartialOrdering[Set[A]] { + + override def tryCompare(x: Set[A], y: Set[A]): Option[Int] = { + if (x == y) Some(0) + else if (x subsetOf y) Some(-1) + else if (y subsetOf x) Some(1) + else None + } + + override def lteq(x: Set[A], y: Set[A]): Boolean = (x subsetOf y) || (x == y) + } + checkAll("fromPartialOrdering[Int]", PartialOrderTests(PartialOrder.fromPartialOrdering[Set[Int]]).partialOrder) + } + implicit val arbitraryComparison: Arbitrary[Comparison] = Arbitrary(Gen.oneOf(Comparison.GreaterThan, Comparison.EqualTo, Comparison.LessThan)) diff --git a/kernel/src/main/scala/cats/kernel/Eq.scala b/kernel/src/main/scala/cats/kernel/Eq.scala index 227937c632..9caf1c0700 100644 --- a/kernel/src/main/scala/cats/kernel/Eq.scala +++ b/kernel/src/main/scala/cats/kernel/Eq.scala @@ -32,7 +32,17 @@ abstract class EqFunctions[E[T] <: Eq[T]] { } -object Eq extends EqFunctions[Eq] { +trait EqToEquivConversion { + /** + * Implicitly derive a `scala.math.Equiv[A]` from a `Eq[A]` + * instance. + */ + implicit def catsKernelEquivForEq[A](implicit ev: Eq[A]): Equiv[A] = new Equiv[A] { + def equiv(a: A, b: A) = ev.eqv(a, b) + } +} + +object Eq extends EqFunctions[Eq] with EqToEquivConversion { /** * Access an implicit `Eq[A]`. @@ -66,13 +76,6 @@ object Eq extends EqFunctions[Eq] { def eqv(x: A, y: A) = eq1.eqv(x, y) || eq2.eqv(x, y) } - /** - * This gives compatibility with scala's Equiv trait - */ - implicit def catsKernelEquivForEq[A](implicit ev: Eq[A]): Equiv[A] = - new Equiv[A] { - def equiv(a: A, b: A) = ev.eqv(a, b) - } /** * Create an `Eq` instance from an `eqv` implementation. diff --git a/kernel/src/main/scala/cats/kernel/Hash.scala b/kernel/src/main/scala/cats/kernel/Hash.scala index ef8e88d742..4579f12184 100644 --- a/kernel/src/main/scala/cats/kernel/Hash.scala +++ b/kernel/src/main/scala/cats/kernel/Hash.scala @@ -54,3 +54,9 @@ object Hash extends HashFunctions[Hash] { } } + +trait HashToHashingConversion { + implicit def catsKernelHashToHashing[A](implicit ev: Hash[A]): Hashing[A] = new Hashing[A] { + override def hash(x: A): Int = ev.hash(x) + } +} diff --git a/kernel/src/main/scala/cats/kernel/Order.scala b/kernel/src/main/scala/cats/kernel/Order.scala index 2b580cd142..4ddff806d6 100644 --- a/kernel/src/main/scala/cats/kernel/Order.scala +++ b/kernel/src/main/scala/cats/kernel/Order.scala @@ -115,9 +115,10 @@ trait OrderToOrderingConversion { */ implicit def catsKernelOrderingForOrder[A](implicit ev: Order[A]): Ordering[A] = ev.toOrdering + } -object Order extends OrderFunctions[Order] { +object Order extends OrderFunctions[Order] with OrderToOrderingConversion { /** * Access an implicit `Order[A]`. */ @@ -164,13 +165,6 @@ object Order extends OrderFunctions[Order] { def compare(x: A, y: A) = f(x, y) } - /** - * Implicitly convert a `Order[A]` to a `scala.math.Ordering[A]` - * instance. - */ - implicit def catsKernelOrderingForOrder[A](implicit ev: Order[A]): Ordering[A] = - ev.toOrdering - /** * An `Order` instance that considers all `A` instances to be equal. */ diff --git a/kernel/src/main/scala/cats/kernel/PartialOrder.scala b/kernel/src/main/scala/cats/kernel/PartialOrder.scala index 048041b0f1..caa3baf535 100644 --- a/kernel/src/main/scala/cats/kernel/PartialOrder.scala +++ b/kernel/src/main/scala/cats/kernel/PartialOrder.scala @@ -123,7 +123,7 @@ abstract class PartialOrderFunctions[P[T] <: PartialOrder[T]] extends EqFunction ev.gt(x, y) } -object PartialOrder extends PartialOrderFunctions[PartialOrder] { +object PartialOrder extends PartialOrderFunctions[PartialOrder] with PartialOrderToPartialOrderingConversion { /** * Access an implicit `PartialOrder[A]`. */ @@ -154,10 +154,14 @@ object PartialOrder extends PartialOrderFunctions[PartialOrder] { def partialCompare(x: A, y: A) = f(x, y) } - /** - * Implicitly convert a `PartialOrder[A]` to a - * `scala.math.PartialOrdering[A]` instance. - */ + def fromPartialOrdering[A](implicit ev: PartialOrdering[A]): PartialOrder[A] = new PartialOrder[A] { + def partialCompare(x: A, y: A): Double = + ev.tryCompare(x, y).fold(Double.NaN)(_.toDouble) + } +} + + +trait PartialOrderToPartialOrderingConversion { implicit def catsKernelPartialOrderingForPartialOrder[A](implicit ev: PartialOrder[A]): PartialOrdering[A] = new PartialOrdering[A] { def tryCompare(x: A, y: A): Option[Int] = ev.tryCompare(x, y) diff --git a/kernel/src/main/scala/cats/kernel/instances/all.scala b/kernel/src/main/scala/cats/kernel/instances/all.scala index 954431c68a..1af2f0b0da 100644 --- a/kernel/src/main/scala/cats/kernel/instances/all.scala +++ b/kernel/src/main/scala/cats/kernel/instances/all.scala @@ -11,15 +11,19 @@ trait AllInstances with ByteInstances with CharInstances with DoubleInstances + with EqInstances with EitherInstances with DurationInstances with FloatInstances with FunctionInstances + with HashInstances with IntInstances with ListInstances with LongInstances with MapInstances with OptionInstances + with OrderInstances + with PartialOrderInstances with QueueInstances with SetInstances with ShortInstances diff --git a/kernel/src/main/scala/cats/kernel/instances/eq.scala b/kernel/src/main/scala/cats/kernel/instances/eq.scala new file mode 100644 index 0000000000..2ffbf559ad --- /dev/null +++ b/kernel/src/main/scala/cats/kernel/instances/eq.scala @@ -0,0 +1,6 @@ +package cats.kernel +package instances + +trait EqInstances extends EqToEquivConversion + +object eq extends EqInstances diff --git a/kernel/src/main/scala/cats/kernel/instances/hash.scala b/kernel/src/main/scala/cats/kernel/instances/hash.scala new file mode 100644 index 0000000000..3822096596 --- /dev/null +++ b/kernel/src/main/scala/cats/kernel/instances/hash.scala @@ -0,0 +1,6 @@ +package cats.kernel +package instances + +trait HashInstances extends HashToHashingConversion + +object hash extends HashInstances diff --git a/kernel/src/main/scala/cats/kernel/instances/order.scala b/kernel/src/main/scala/cats/kernel/instances/order.scala new file mode 100644 index 0000000000..e678dc34d9 --- /dev/null +++ b/kernel/src/main/scala/cats/kernel/instances/order.scala @@ -0,0 +1,6 @@ +package cats.kernel +package instances + +trait OrderInstances extends OrderToOrderingConversion + +object order extends OrderInstances diff --git a/kernel/src/main/scala/cats/kernel/instances/partialOrder.scala b/kernel/src/main/scala/cats/kernel/instances/partialOrder.scala new file mode 100644 index 0000000000..b7dfc5819b --- /dev/null +++ b/kernel/src/main/scala/cats/kernel/instances/partialOrder.scala @@ -0,0 +1,6 @@ +package cats.kernel +package instances + +trait PartialOrderInstances extends PartialOrderToPartialOrderingConversion + +object partialOrder extends PartialOrderInstances