From 1d933059dd130e8ecc081d8a12d1c4896528f15a Mon Sep 17 00:00:00 2001 From: Filippo Mariotti Date: Wed, 10 Jun 2020 01:26:05 +0100 Subject: [PATCH 1/2] Added CommutativeMonoid for Option to scope --- .../shared/src/test/scala/cats/kernel/laws/LawTests.scala | 1 - kernel/src/main/scala/cats/kernel/Semigroup.scala | 5 ++++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/kernel-laws/shared/src/test/scala/cats/kernel/laws/LawTests.scala b/kernel-laws/shared/src/test/scala/cats/kernel/laws/LawTests.scala index 61f9c98677..2f6b530477 100644 --- a/kernel-laws/shared/src/test/scala/cats/kernel/laws/LawTests.scala +++ b/kernel-laws/shared/src/test/scala/cats/kernel/laws/LawTests.scala @@ -1,7 +1,6 @@ package cats.kernel package laws -import cats.kernel.instances.all._ import cats.kernel.laws.discipline._ import cats.platform.Platform diff --git a/kernel/src/main/scala/cats/kernel/Semigroup.scala b/kernel/src/main/scala/cats/kernel/Semigroup.scala index f82eb5557c..917f3b622e 100644 --- a/kernel/src/main/scala/cats/kernel/Semigroup.scala +++ b/kernel/src/main/scala/cats/kernel/Semigroup.scala @@ -267,7 +267,10 @@ private[kernel] trait SemigroupInstances { cats.kernel.instances.function.catsKernelSemigroupForFunction1[A, B] implicit def catsKernelSemigroupForEither[A, B: Semigroup]: Semigroup[Either[A, B]] = cats.kernel.instances.either.catsDataSemigroupForEither[A, B] - implicit def catsKernelSemigroupForTry[A: Semigroup]: Semigroup[Try[A]] = new TrySemigroup[A](Semigroup[A]) + implicit def catsKernelSemigroupForTry[A: Semigroup]: Semigroup[Try[A]] = + new TrySemigroup[A](Semigroup[A]) + implicit def catsKernelCommutativeMonoidForOption[A: CommutativeSemigroup]: CommutativeMonoid[Option[A]] = + cats.kernel.instances.option.catsKernelStdCommutativeMonoidForOption[A] } private class TryMonoid[A](A: Monoid[A]) extends TrySemigroup[A](A) with Monoid[Try[A]] { From 27741bd9b092c4b47fe9afe6c49c7fbec28bea6e Mon Sep 17 00:00:00 2001 From: Filippo Mariotti Date: Wed, 10 Jun 2020 18:27:34 +0100 Subject: [PATCH 2/2] Fix ambiguity when the base type has both Semigroup and CommutativeSemigroup --- .../src/test/scala/cats/kernel/laws/LawTests.scala | 9 +++++++++ kernel/src/main/scala/cats/kernel/Semigroup.scala | 11 ++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/kernel-laws/shared/src/test/scala/cats/kernel/laws/LawTests.scala b/kernel-laws/shared/src/test/scala/cats/kernel/laws/LawTests.scala index 2f6b530477..28c791f7b7 100644 --- a/kernel-laws/shared/src/test/scala/cats/kernel/laws/LawTests.scala +++ b/kernel-laws/shared/src/test/scala/cats/kernel/laws/LawTests.scala @@ -147,6 +147,15 @@ class Tests extends TestsConfig with AnyFunSuiteLike with FunSuiteDiscipline wit import KernelCheck._ + test("The instances in scope are not ambiguous") { + implicitly[Monoid[Option[String]]] + implicitly[Semigroup[Option[String]]] + implicitly[Monoid[Option[Int]]] + implicitly[Semigroup[Option[Int]]] + implicitly[CommutativeSemigroup[Option[Int]]] + implicitly[CommutativeMonoid[Option[Int]]] + } + { // needed for Cogen[Map[...]] implicit val ohe: Ordering[HasEq[Int]] = Ordering.by[HasEq[Int], Int](_.a) diff --git a/kernel/src/main/scala/cats/kernel/Semigroup.scala b/kernel/src/main/scala/cats/kernel/Semigroup.scala index 917f3b622e..aac2aa4e23 100644 --- a/kernel/src/main/scala/cats/kernel/Semigroup.scala +++ b/kernel/src/main/scala/cats/kernel/Semigroup.scala @@ -179,8 +179,6 @@ object Semigroup implicit def catsKernelMonoidForString: Monoid[String] = cats.kernel.instances.string.catsKernelStdMonoidForString - implicit def catsKernelMonoidForOption[A: Semigroup]: Monoid[Option[A]] = - cats.kernel.instances.option.catsKernelStdMonoidForOption[A] implicit def catsKernelMonoidForList[A]: Monoid[List[A]] = cats.kernel.instances.list.catsKernelStdMonoidForList[A] implicit def catsKernelMonoidForVector[A]: Monoid[Vector[A]] = cats.kernel.instances.vector.catsKernelStdMonoidForVector[A] @@ -230,6 +228,8 @@ private[kernel] trait CommutativeMonoidInstances extends MonoidInstances { cats.kernel.instances.function.catsKernelCommutativeMonoidForFunction0[A] implicit def catsKernelCommutativeMonoidForFunction1[A, B: CommutativeMonoid]: CommutativeMonoid[A => B] = cats.kernel.instances.function.catsKernelCommutativeMonoidForFunction1[A, B] + implicit def catsKernelCommutativeMonoidForOption[A: CommutativeSemigroup]: CommutativeMonoid[Option[A]] = + cats.kernel.instances.option.catsKernelStdCommutativeMonoidForOption[A] } private[kernel] trait MonoidInstances extends BandInstances { @@ -243,7 +243,10 @@ private[kernel] trait MonoidInstances extends BandInstances { cats.kernel.instances.sortedMap.catsKernelStdMonoidForSortedMap[K, V] implicit def catsKernelMonoidForEither[A, B: Monoid]: Monoid[Either[A, B]] = cats.kernel.instances.either.catsDataMonoidForEither[A, B] - implicit def catsKernelMonoidForTry[A: Monoid]: Monoid[Try[A]] = new TryMonoid[A](Monoid[A]) + implicit def catsKernelMonoidForTry[A: Monoid]: Monoid[Try[A]] = + new TryMonoid[A](Monoid[A]) + implicit def catsKernelMonoidForOption[A: Semigroup]: Monoid[Option[A]] = + cats.kernel.instances.option.catsKernelStdMonoidForOption[A] } private[kernel] trait BandInstances extends CommutativeSemigroupInstances { @@ -269,8 +272,6 @@ private[kernel] trait SemigroupInstances { cats.kernel.instances.either.catsDataSemigroupForEither[A, B] implicit def catsKernelSemigroupForTry[A: Semigroup]: Semigroup[Try[A]] = new TrySemigroup[A](Semigroup[A]) - implicit def catsKernelCommutativeMonoidForOption[A: CommutativeSemigroup]: CommutativeMonoid[Option[A]] = - cats.kernel.instances.option.catsKernelStdCommutativeMonoidForOption[A] } private class TryMonoid[A](A: Monoid[A]) extends TrySemigroup[A](A) with Monoid[Try[A]] {