Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Reducible laws #893

Merged
merged 1 commit into from
Feb 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions laws/src/main/scala/cats/laws/ReducibleLaws.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package cats
package laws

import cats.implicits._

trait ReducibleLaws[F[_]] extends FoldableLaws[F] {
implicit def F: Reducible[F]

def reduceLeftToConsistentWithReduceMap[A, B](
fa: F[A],
f: A => B
)(implicit
B: Semigroup[B]
): IsEq[B] =
fa.reduceMap(f) <-> fa.reduceLeftTo(f)((b, a) => b |+| f(a))

def reduceRightToConsistentWithReduceMap[A, B](
fa: F[A],
f: A => B
)(implicit
B: Semigroup[B]
): IsEq[B] =
fa.reduceMap(f) <-> fa.reduceRightTo(f)((a, eb) => eb.map(f(a) |+| _)).value
}

object ReducibleLaws {
def apply[F[_]](implicit ev: Reducible[F]): ReducibleLaws[F] =
new ReducibleLaws[F] { def F: Reducible[F] = ev }
}
27 changes: 27 additions & 0 deletions laws/src/main/scala/cats/laws/discipline/ReducibleTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cats
package laws
package discipline

import org.scalacheck.Arbitrary
import org.scalacheck.Prop.forAll

trait ReducibleTests[F[_]] extends FoldableTests[F] {
def laws: ReducibleLaws[F]

def reducible[A: Arbitrary, B: Arbitrary](implicit
ArbFA: Arbitrary[F[A]],
B: Monoid[B],
EqB: Eq[B]
): RuleSet =
new DefaultRuleSet(
name = "reducible",
parent = Some(foldable[A, B]),
"reduceLeftTo consistent with reduceMap" -> forAll(laws.reduceLeftToConsistentWithReduceMap[A, B] _),
"reduceRightTo consistent with reduceMap" -> forAll(laws.reduceRightToConsistentWithReduceMap[A, B] _)
)
}

object ReducibleTests {
def apply[F[_] : Reducible]: ReducibleTests[F] =
new ReducibleTests[F] { def laws: ReducibleLaws[F] = ReducibleLaws[F] }
}
5 changes: 4 additions & 1 deletion tests/src/test/scala/cats/tests/OneAndTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package tests
import algebra.laws.{GroupLaws, OrderLaws}

import cats.data.{NonEmptyList, OneAnd}
import cats.laws.discipline.{ComonadTests, FunctorTests, SemigroupKTests, FoldableTests, MonadTests, SerializableTests, CartesianTests, TraverseTests}
import cats.laws.discipline.{ComonadTests, FunctorTests, SemigroupKTests, FoldableTests, MonadTests, SerializableTests, CartesianTests, TraverseTests, ReducibleTests}
import cats.laws.discipline.arbitrary.{evalArbitrary, oneAndArbitrary}
import cats.laws.discipline.eq._

Expand All @@ -16,6 +16,9 @@ class OneAndTests extends CatsSuite {
checkAll("OneAnd[List, Int] with Option", TraverseTests[OneAnd[List, ?]].traverse[Int, Int, Int, Int, Option, Option])
checkAll("Traverse[OneAnd[List, A]]", SerializableTests.serializable(Traverse[OneAnd[List, ?]]))

checkAll("OneAnd[List, Int]", ReducibleTests[OneAnd[List, ?]].reducible[Int, Int])
checkAll("Reducible[OneAnd[List, ?]]", SerializableTests.serializable(Reducible[OneAnd[List, ?]]))

implicit val iso = CartesianTests.Isomorphisms.invariant[OneAnd[ListWrapper, ?]](OneAnd.oneAndFunctor(ListWrapper.functor))

// Test instances that have more general constraints
Expand Down