From 9026c96dedfc6eec6be66f17c7b2f27cfbc913f4 Mon Sep 17 00:00:00 2001 From: Luka Jacobowitz Date: Wed, 27 Sep 2017 23:11:31 +0200 Subject: [PATCH] Add id --- .../src/main/scala/cats/kernel/laws/MonoidLaws.scala | 6 +++++- .../cats/kernel/laws/discipline/MonoidLawTests.scala | 9 ++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/kernel-laws/src/main/scala/cats/kernel/laws/MonoidLaws.scala b/kernel-laws/src/main/scala/cats/kernel/laws/MonoidLaws.scala index 397c5c2c2a8..979d80ca0a1 100644 --- a/kernel-laws/src/main/scala/cats/kernel/laws/MonoidLaws.scala +++ b/kernel-laws/src/main/scala/cats/kernel/laws/MonoidLaws.scala @@ -1,6 +1,6 @@ package cats.kernel.laws -import cats.kernel.Monoid +import cats.kernel.{Eq, Monoid} trait MonoidLaws[A] extends SemigroupLaws[A] { override implicit def S: Monoid[A] @@ -20,6 +20,10 @@ trait MonoidLaws[A] extends SemigroupLaws[A] { def combineAll(xs: Vector[A]): IsEq[A] = S.combineAll(xs) <-> (S.empty +: xs).reduce(S.combine) + def isId(x: A, eqv: Eq[A]): IsEq[Boolean] = { + eqv.eqv(x, S.empty) <-> S.isEmpty(x)(eqv) + } + } object MonoidLaws { diff --git a/kernel-laws/src/main/scala/cats/kernel/laws/discipline/MonoidLawTests.scala b/kernel-laws/src/main/scala/cats/kernel/laws/discipline/MonoidLawTests.scala index 3e09e2558be..7e8a4bb7fc7 100644 --- a/kernel-laws/src/main/scala/cats/kernel/laws/discipline/MonoidLawTests.scala +++ b/kernel-laws/src/main/scala/cats/kernel/laws/discipline/MonoidLawTests.scala @@ -1,7 +1,9 @@ -package cats.kernel.laws.discipline +package cats +package kernel +package laws +package discipline -import cats.kernel.{Monoid, Eq} -import cats.kernel.laws.MonoidLaws +import cats.kernel.instances.boolean._ import org.scalacheck.Arbitrary import org.scalacheck.Prop.forAll @@ -17,6 +19,7 @@ trait MonoidLawTests[A] extends SemigroupLawTests[A] { "right identity" -> forAll(laws.rightIdentity _), "combine all" -> forAll(laws.combineAll _), "collect0" -> forAll(laws.collect0 _), + "is id" -> forAll((a: A) => laws.isId(a, eqA)), "repeat0" -> forAll(laws.repeat0 _)) }