Skip to content

Commit

Permalink
Remove left-distributivity law from MonadCombine.
Browse files Browse the repository at this point in the history
Also remove various obsolete/commented code.
  • Loading branch information
erik-stripe committed Sep 6, 2016
1 parent 80a63f9 commit 742e1d2
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 58 deletions.
25 changes: 0 additions & 25 deletions js/src/test/scala/cats/tests/FutureTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,6 @@ class FutureTests extends CatsSuite {
implicit def cogenForFuture[A]: Cogen[Future[A]] =
Cogen[Unit].contramap(_ => ())

// FIXME: for some reason we aren't able to link against the correct
// function generator (Arbitrary.arbFunction1) here. i dont'
// understand why. this method overrides the default implicit in
// scope.
//
// if you comment this method you'll get a compilation error:
//
// Referring to non-existent method org.scalacheck.Arbitrary$.arbFunction1(org.scalacheck.Arbitrary)org.scalacheck.Arbitrary
// called from cats.js.tests.FutureTests.<init>()
// called from cats.js.tests.FutureTests.__exportedInits
// exported to JavaScript with @JSExport
// involving instantiated classes:
// cats.js.tests.FutureTests
//
// if you look, you'll see that this is the wrong version of
// arbFunction1 -- it only takes an Arbitrary instead of an
// Arbitrary and a Cogen.
//
// what's going on? is this compiling against an earlier version of
// ScalaCheck? is something else happening? who knows?
//
// for now we'll let sleeping dogs lie.
//implicit def fakeArbitraryFunction[A: Cogen, B: Arbitrary]: Arbitrary[A => B] =
// Arbitrary(arbitrary[B].map(b => (a: A) => b))

checkAll("Future[Int]", MonadErrorTests[Future, Throwable].monadError[Int, Int, Int])
checkAll("Future[Int]", ComonadTests[Future].comonad[Int, Int, Int])
checkAll("Future", MonadTests[Future].monad[Int, Int, Int])
Expand Down
12 changes: 10 additions & 2 deletions laws/src/main/scala/cats/laws/MonadCombineLaws.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@ import cats.syntax.all._
trait MonadCombineLaws[F[_]] extends MonadFilterLaws[F] with AlternativeLaws[F] {
implicit override def F: MonadCombine[F]

def monadCombineLeftDistributivity[A, B](fa: F[A], fa2: F[A], f: A => F[B]): IsEq[F[B]] =
F.combineK(fa, fa2).flatMap(f) <-> F.combineK(fa flatMap f, fa2 flatMap f)
// the left distributivity law does not hold for things like
// MonadCombine[Option]; here's a counter-example:
//
// def f(x: Int): Option[Int] = if (x == 0) None else Some(x)
// val a = Option(0)
// val b = Option(1)
// (a <+> b).flatMap(f) != (a.flatMap(f) <+> b.flatMap(f))
//
// def monadCombineLeftDistributivity[A, B](fa: F[A], fa2: F[A], f: A => F[B]): IsEq[F[B]] =
// F.combineK(fa, fa2).flatMap(f) <-> F.combineK(fa flatMap f, fa2 flatMap f)
}

object MonadCombineLaws {
Expand Down
12 changes: 0 additions & 12 deletions laws/src/main/scala/cats/laws/discipline/Arbitrary.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,6 @@ object arbitrary extends ArbitraryInstances0 {
implicit def catsLawsCogenForFunction0[A](implicit A: Cogen[A]): Cogen[Function0[A]] =
A.contramap(_())

// // A special function1Arbitrary for testing operations like dropWhile specifically
// // in the context of Int => Boolean. Once scalacheck supports better Function1 arbitrary
// // instances this can be removed.
// implicit def catsLawsArbitraryForIntToBool: Arbitrary[(Int) => Boolean] =
// Arbitrary(
// getArbitrary[Int].map(x =>
// new Function1[Int, Boolean] {
// def apply(i: Int): Boolean = i < x
//
// override def toString = s"<function testing whether input is less than $x>"
// }))

implicit def catsLawsArbitraryForConst[A, B](implicit A: Arbitrary[A]): Arbitrary[Const[A, B]] =
Arbitrary(A.arbitrary.map(Const[A, B]))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ trait MonadCombineTests[F[_]] extends MonadFilterTests[F] with AlternativeTests[
def name: String = "monadCombine"
def bases: Seq[(String, RuleSet)] = Nil
def parents: Seq[RuleSet] = Seq(monadFilter[A, B, C], alternative[A, B, C])
def props: Seq[(String, Prop)] = Seq(
"monadCombine left distributivity" -> forAll(laws.monadCombineLeftDistributivity[A, B] _)
)
def props: Seq[(String, Prop)] = Nil
// def props: Seq[(String, Prop)] = Seq(
// "monadCombine left distributivity" -> forAll(laws.monadCombineLeftDistributivity[A, B] _)
// )
}
}
}
Expand Down
14 changes: 0 additions & 14 deletions tests/src/test/scala/cats/tests/CatsSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ import org.scalatest.{FunSuite, FunSuiteLike, Matchers}
import org.scalatest.prop.{Configuration, GeneratorDrivenPropertyChecks}
import org.typelevel.discipline.scalatest.Discipline

//import org.scalacheck.{Arbitrary, Gen}
//import org.scalacheck.Arbitrary.arbitrary

//import scala.util.{Failure, Success, Try}

trait TestSettings extends Configuration with Matchers {

lazy val checkConfiguration: PropertyCheckConfiguration =
Expand All @@ -42,7 +37,6 @@ trait CatsSuite extends FunSuite
with TestSettings
with AllInstances
with AllSyntax
with TestInstances
with StrictCatsEquality { self: FunSuiteLike =>

implicit override val generatorDrivenConfig: PropertyCheckConfiguration =
Expand All @@ -61,11 +55,3 @@ trait SlowCatsSuite extends CatsSuite {
implicit override val generatorDrivenConfig: PropertyCheckConfiguration =
slowCheckConfiguration
}

sealed trait TestInstances {
// // To be replaced by https://github.com/rickynils/scalacheck/pull/170
// implicit def arbitraryTry[A: Arbitrary]: Arbitrary[Try[A]] =
// Arbitrary(Gen.oneOf(
// arbitrary[A].map(Success(_)),
// arbitrary[Throwable].map(Failure(_))))
}
3 changes: 1 addition & 2 deletions tests/src/test/scala/cats/tests/OptionTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ class OptionTests extends CatsSuite {
checkAll("Option[Int]", CoflatMapTests[Option].coflatMap[Int, Int, Int])
checkAll("CoflatMap[Option]", SerializableTests.serializable(CoflatMap[Option]))

// not actually lawful, fails distributivity
//checkAll("Option[Int]", MonadCombineTests[Option].monadCombine[Int, Int, Int])
checkAll("Option[Int]", MonadCombineTests[Option].monadCombine[Int, Int, Int])
checkAll("MonadCombine[Option]", SerializableTests.serializable(MonadCombine[Option]))

checkAll("Option[Int]", MonadTests[Option].monad[Int, Int, Int])
Expand Down

0 comments on commit 742e1d2

Please sign in to comment.