From 4599c32f2bc8ab1146decea85061c92132230ded Mon Sep 17 00:00:00 2001 From: Cody Allen Date: Tue, 12 Dec 2017 14:30:01 -0800 Subject: [PATCH 1/3] Add a CommutativeMonoid for Map Since we already have a `Monoid` instance for `Map`, it seems natural to have a `CommutativeMonoid` when the value type has a commutative semigroup. Note: Unfortunately, I believe that this is a binary-incompatible change in cats kernel. cc @johnynek --- kernel-laws/src/test/scala/cats/kernel/laws/LawTests.scala | 3 +++ kernel/src/main/scala/cats/kernel/instances/map.scala | 3 +++ 2 files changed, 6 insertions(+) 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 96359bbdf8..571873645b 100644 --- a/kernel-laws/src/test/scala/cats/kernel/laws/LawTests.scala +++ b/kernel-laws/src/test/scala/cats/kernel/laws/LawTests.scala @@ -158,6 +158,9 @@ class Tests extends FunSuite with Discipline { checkAll("Monoid[Queue[Int]]", MonoidTests[Queue[Int]].monoid) checkAll("Monoid[Queue[Int]]", SerializableTests.serializable(Monoid[Queue[Int]])) + checkAll("CommutativeMonoid[Map[String, Int]]", CommutativeMonoidTests[Map[String, Int]].commutativeMonoid) + checkAll("CommutativeMonoid[Map[String, Int]]", SerializableTests.serializable(CommutativeMonoid[Map[String, Int]])) + checkAll("BoundedSemilattice[BitSet]", BoundedSemilatticeTests[BitSet].boundedSemilattice) checkAll("BoundedSemilattice[BitSet]", SerializableTests.serializable(BoundedSemilattice[BitSet])) checkAll("BoundedSemilattice[Set[Int]]", BoundedSemilatticeTests[Set[Int]].boundedSemilattice) diff --git a/kernel/src/main/scala/cats/kernel/instances/map.scala b/kernel/src/main/scala/cats/kernel/instances/map.scala index 668d5243c8..3ad5db23da 100644 --- a/kernel/src/main/scala/cats/kernel/instances/map.scala +++ b/kernel/src/main/scala/cats/kernel/instances/map.scala @@ -8,6 +8,9 @@ package object map extends MapInstances trait MapInstances extends MapInstances1 { implicit def catsKernelStdHashForMap[K: Hash, V: Hash]: Hash[Map[K, V]] = new MapHash[K, V] + + implicit def catsKernelStdCommutativeMonoidForMap[K, V: CommutativeSemigroup]: CommutativeMonoid[Map[K, V]] = + new MapMonoid[K, V] with CommutativeMonoid[Map[K, V]] } trait MapInstances1 { From 9b9dbcc2c6cd5c2774b060a657c6bfd9bf16b90f Mon Sep 17 00:00:00 2001 From: Cody Allen Date: Tue, 12 Dec 2017 15:45:20 -0800 Subject: [PATCH 2/3] Add mima exception for Map commutative monoid --- build.sbt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/build.sbt b/build.sbt index a631e4c1a4..c5a8f2b56c 100644 --- a/build.sbt +++ b/build.sbt @@ -334,7 +334,8 @@ def mimaSettings(moduleName: String) = Seq( exclude[InheritedNewAbstractMethodProblem]("cats.instances.Function0Instances0.function0Distributive"), exclude[InheritedNewAbstractMethodProblem]("cats.instances.Function1Instances0.catsStdDistributiveForFunction1"), exclude[InheritedNewAbstractMethodProblem]("cats.instances.Function1Instances0.catsStdDistributiveForFunction1"), - exclude[InheritedNewAbstractMethodProblem]("cats.instances.Function1Instances0.catsStdDistributiveForFunction1") + exclude[InheritedNewAbstractMethodProblem]("cats.instances.Function1Instances0.catsStdDistributiveForFunction1"), + exclude[ReversedMissingMethodProblem]("cats.kernel.instances.MapInstances.catsKernelStdCommutativeMonoidForMap") ) } ) From fa99829ae04790427e04bdcec5a699c9f90a81a1 Mon Sep 17 00:00:00 2001 From: LukaJCB Date: Wed, 13 Dec 2017 23:40:01 +0000 Subject: [PATCH 3/3] Use non-commutative monoid for MonoidTests of Map --- kernel-laws/src/test/scala/cats/kernel/laws/LawTests.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 571873645b..94aec53ea3 100644 --- a/kernel-laws/src/test/scala/cats/kernel/laws/LawTests.scala +++ b/kernel-laws/src/test/scala/cats/kernel/laws/LawTests.scala @@ -153,8 +153,8 @@ class Tests extends FunSuite with Discipline { checkAll("Monoid[Stream[Int]]", SerializableTests.serializable(Monoid[Stream[Int]])) checkAll("Monoid[List[String]]", MonoidTests[List[String]].monoid) checkAll("Monoid[List[String]]", SerializableTests.serializable(Monoid[List[String]])) - checkAll("Monoid[Map[String, Int]]", MonoidTests[Map[String, Int]].monoid) - checkAll("Monoid[Map[String, Int]]", SerializableTests.serializable(Monoid[Map[String, Int]])) + checkAll("Monoid[Map[String, String]]", MonoidTests[Map[String, String]].monoid) + checkAll("Monoid[Map[String, String]]", SerializableTests.serializable(Monoid[Map[String, String]])) checkAll("Monoid[Queue[Int]]", MonoidTests[Queue[Int]].monoid) checkAll("Monoid[Queue[Int]]", SerializableTests.serializable(Monoid[Queue[Int]]))