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

Remove MonadFilter, and fix MonadCombineLaws #1342

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 4 additions & 1 deletion core/src/main/scala/cats/MonadCombine.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import simulacrum.typeclass
/**
* The combination of a Monad with a MonoidK
*/
@typeclass trait MonadCombine[F[_]] extends MonadFilter[F] with Alternative[F] {
@typeclass trait MonadCombine[F[_]] extends Monad[F] with FunctorFilter[F] with Alternative[F] {

override def mapFilter[A, B](fa: F[A])(f: A => Option[B]): F[B] =
flatMap(fa)(a => f(a).fold(empty[B])(pure))

/**
* Fold over the inner structure to combine all of the values with
Expand Down
18 changes: 0 additions & 18 deletions core/src/main/scala/cats/MonadFilter.scala

This file was deleted.

14 changes: 0 additions & 14 deletions core/src/main/scala/cats/data/EitherT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -314,14 +314,6 @@ private[data] abstract class EitherTInstances extends EitherTInstances1 {
}

private[data] abstract class EitherTInstances1 extends EitherTInstances2 {
/* TODO violates monadFilter right empty law -- re-enable when MonadFilter laws are split in to weak/strong
implicit def catsDataMonadFilterForEitherT[F[_], L](implicit F: Monad[F], L: Monoid[L]): MonadFilter[EitherT[F, L, ?]] = {
implicit val F0 = F
implicit val L0 = L
new EitherTMonadFilter[F, L] { implicit val F = F0; implicit val L = L0 }
}
*/

implicit def catsSemigroupForEitherT[F[_], L, A](implicit F: Semigroup[F[Either[L, A]]]): Semigroup[EitherT[F, L, A]] =
new EitherTSemigroup[F, L, A] { implicit val F0 = F }

Expand Down Expand Up @@ -418,12 +410,6 @@ private[data] trait EitherTMonadError[F[_], L] extends MonadError[EitherT[F, L,
fla.recoverWith(pf)
}

private[data] trait EitherTMonadFilter[F[_], L] extends MonadFilter[EitherT[F, L, ?]] with EitherTMonadError[F, L] {
implicit val F: Monad[F]
implicit val L: Monoid[L]
def empty[A]: EitherT[F, L, A] = EitherT(F.pure(Either.left(L.empty)))
}

/* TODO violates right absorbtion, right distributivity, and left distributivity -- re-enable when MonadCombine laws are split in to weak/strong
private[data] trait EitherTMonadCombine[F[_], L] extends MonadCombine[EitherT[F, L, ?]] with EitherTMonadFilter[F, L] with EitherTSemigroupK[F, L] {
implicit val F: Monad[F]
Expand Down
12 changes: 0 additions & 12 deletions core/src/main/scala/cats/data/WriterT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,6 @@ private[data] sealed abstract class WriterTInstances0 extends WriterTInstances1
}

private[data] sealed abstract class WriterTInstances1 extends WriterTInstances2 {
implicit def catsDataMonadFilterForWriterT[F[_], L](implicit F: MonadFilter[F], L: Monoid[L]): MonadFilter[WriterT[F, L, ?]] =
new WriterTMonadFilter[F, L] {
implicit val F0: MonadFilter[F] = F
implicit val L0: Monoid[L] = L
}

implicit def catsDataMonoidForWriterT[F[_], L, V](implicit W: Monoid[F[(L, V)]]): Monoid[WriterT[F, L, V]] =
new WriterTMonoid[F, L, V] {
implicit val F0: Monoid[F[(L, V)]] = W
Expand Down Expand Up @@ -359,12 +353,6 @@ private[data] sealed trait WriterTAlternative[F[_], L] extends Alternative[Write
override implicit def F0: Alternative[F]
}

private[data] sealed trait WriterTMonadFilter[F[_], L] extends MonadFilter[WriterT[F, L, ?]] with WriterTMonad[F, L] {
override implicit def F0: MonadFilter[F]

def empty[A]: WriterT[F, L, A] = WriterT(F0.empty)
}

private[data] sealed trait WriterTMonadCombine[F[_], L] extends MonadCombine[WriterT[F, L, ?]] with WriterTMonad[F, L] with WriterTAlternative[F, L] {
override implicit def F0: MonadCombine[F]
}
Expand Down
1 change: 0 additions & 1 deletion core/src/main/scala/cats/syntax/all.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ trait AllSyntax
with ListSyntax
with MonadCombineSyntax
with MonadErrorSyntax
with MonadFilterSyntax
with MonoidSyntax
with OptionSyntax
with OrderSyntax
Expand Down
12 changes: 0 additions & 12 deletions core/src/main/scala/cats/syntax/monadFilter.scala

This file was deleted.

1 change: 0 additions & 1 deletion core/src/main/scala/cats/syntax/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ package object syntax {
object invariant extends InvariantSyntax
object list extends ListSyntax
object monadCombine extends MonadCombineSyntax
object monadFilter extends MonadFilterSyntax
object monoid extends MonoidSyntax
object option extends OptionSyntax
object order extends OrderSyntax
Expand Down
9 changes: 0 additions & 9 deletions docs/src/main/tut/monadfilter.md

This file was deleted.

11 changes: 10 additions & 1 deletion laws/src/main/scala/cats/laws/MonadCombineLaws.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,18 @@ import cats.syntax.all._
/**
* Laws that must be obeyed by any `MonadCombine`.
*/
trait MonadCombineLaws[F[_]] extends MonadFilterLaws[F] with AlternativeLaws[F] {
trait MonadCombineLaws[F[_]] extends MonadLaws[F] with FunctorFilterLaws[F] with AlternativeLaws[F] {
implicit override def F: MonadCombine[F]

def monadCombineLeftEmpty[A, B](f: A => F[B]): IsEq[F[B]] =
F.empty[A].flatMap(f) <-> F.empty[B]

def monadCombineRightEmpty[A, B](fa: F[A]): IsEq[F[B]] =
fa.flatMap(_ => F.empty[B]) <-> F.empty[B]

def monadCombineConsistency[A, B](fa: F[A], f: A => Boolean): IsEq[F[A]] =
fa.filter(f) <-> fa.flatMap(a => if (f(a)) F.pure(a) else F.empty)

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)
}
Expand Down
25 changes: 0 additions & 25 deletions laws/src/main/scala/cats/laws/MonadFilterLaws.scala

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.scalacheck.Arbitrary
import org.scalacheck.Prop
import Prop._

trait MonadCombineTests[F[_]] extends MonadFilterTests[F] with AlternativeTests[F] {
trait MonadCombineTests[F[_]] extends MonadTests[F] with FunctorFilterTests[F] with AlternativeTests[F] {
def laws: MonadCombineLaws[F]

def monadCombine[A: Arbitrary: Eq, B: Arbitrary: Eq, C: Arbitrary: Eq](implicit
Expand All @@ -25,8 +25,11 @@ trait MonadCombineTests[F[_]] extends MonadFilterTests[F] with AlternativeTests[
new RuleSet {
def name: String = "monadCombine"
def bases: Seq[(String, RuleSet)] = Nil
def parents: Seq[RuleSet] = Seq(monadFilter[A, B, C], alternative[A, B, C])
def parents: Seq[RuleSet] = Seq(monad[A, B, C], functorFilter[A, B, C], alternative[A, B, C])
def props: Seq[(String, Prop)] = Seq(
"monadCombine left empty" -> forAll(laws.monadCombineLeftEmpty[A, B] _),
"monadCombine right empty" -> forAll(laws.monadCombineRightEmpty[A, B] _),
"monadCombine consistency" -> forAll(laws.monadCombineConsistency[A, B] _),
"monadCombine left distributivity" -> forAll(laws.monadCombineLeftDistributivity[A, B] _)
)
}
Expand Down
43 changes: 0 additions & 43 deletions laws/src/main/scala/cats/laws/discipline/MonadFilterTests.scala

This file was deleted.

2 changes: 0 additions & 2 deletions tests/src/test/scala/cats/tests/ListWrapper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@ object ListWrapper {

def monoidK: MonoidK[ListWrapper] = monadCombine

def monadFilter: MonadFilter[ListWrapper] = monadCombine

def alternative: Alternative[ListWrapper] = monadCombine

def monoid[A]: Monoid[ListWrapper[A]] = monadCombine.algebra[A]
Expand Down
16 changes: 0 additions & 16 deletions tests/src/test/scala/cats/tests/WriterTTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -293,21 +293,6 @@ class WriterTTests extends CatsSuite {
checkAll("Alternative[WriterT[ListWrapper, ListWrapper[Int], ?]]", SerializableTests.serializable(Alternative[WriterT[ListWrapper, ListWrapper[Int], ?]]))
}

{
// F has a MonadFilter and L has a Monoid
implicit val F: MonadFilter[ListWrapper] = ListWrapper.monadFilter
implicit val L: Monoid[ListWrapper[Int]] = ListWrapper.monoid[Int]

Functor[WriterT[ListWrapper, ListWrapper[Int], ?]]
Apply[WriterT[ListWrapper, ListWrapper[Int], ?]]
Applicative[WriterT[ListWrapper, ListWrapper[Int], ?]]
FlatMap[WriterT[ListWrapper, ListWrapper[Int], ?]]
CoflatMap[WriterT[ListWrapper, ListWrapper[Int], ?]]
Monad[WriterT[ListWrapper, ListWrapper[Int], ?]]
checkAll("WriterT[ListWrapper, ListWrapper[Int], ?]", MonadFilterTests[WriterT[ListWrapper, ListWrapper[Int], ?]].monadFilter[Int, Int, Int])
checkAll("MonadFilter[WriterT[ListWrapper, ListWrapper[Int], ?]]", SerializableTests.serializable(MonadFilter[WriterT[ListWrapper, ListWrapper[Int], ?]]))
}

{
// F has a MonadCombine and L has a Monoid
implicit val F: MonadCombine[ListWrapper] = ListWrapper.monadCombine
Expand All @@ -319,7 +304,6 @@ class WriterTTests extends CatsSuite {
FlatMap[WriterT[ListWrapper, ListWrapper[Int], ?]]
CoflatMap[WriterT[ListWrapper, ListWrapper[Int], ?]]
Monad[WriterT[ListWrapper, ListWrapper[Int], ?]]
MonadFilter[WriterT[ListWrapper, ListWrapper[Int], ?]]
Alternative[WriterT[ListWrapper, ListWrapper[Int], ?]]
SemigroupK[WriterT[ListWrapper, ListWrapper[Int], ?]]
MonoidK[WriterT[ListWrapper, ListWrapper[Int], ?]]
Expand Down