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

MonadTrans instance for RWST and make MonadTrans serializable #1680

Merged
merged 2 commits into from
May 18, 2017
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
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/MonadTrans.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package cats
* A type class which abstracts over the ability to lift an M[A] into a
* MonadTransformer
*/
trait MonadTrans[MT[_[_], _]] {
trait MonadTrans[MT[_[_], _]] extends Any with Serializable {

/**
* Lift a value of type M[A] into a monad transformer MT[M, A]
Expand Down
16 changes: 7 additions & 9 deletions core/src/main/scala/cats/data/ReaderWriterStateT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,9 @@ private[data] sealed trait RWSTInstances extends RWSTInstances1 {
implicit def L: Monoid[L] = L0
}

implicit def catsDataLiftForRWST[E, S, L](
implicit L0: Monoid[L]): TransLift.Aux[ReaderWriterStateT[?[_], E, S, L, ?], Applicative] =
new RWSTTransLift[E, S, L] {
implicit def catsDataMonadTransForRWST[E, S, L](
implicit L0: Monoid[L]): MonadTrans[ReaderWriterStateT[?[_], E, S, L, ?]] =
new RWSTMonadTrans[E, S, L] {
implicit def L: Monoid[L] = L0
}
}
Expand Down Expand Up @@ -487,12 +487,10 @@ private[data] sealed trait RWSTMonadState[F[_], E, S, L]
def set(s: S): ReaderWriterStateT[F, E, S, L, Unit] = ReaderWriterStateT.set(s)
}

private[data] sealed trait RWSTTransLift[E, S, L] extends TransLift[ReaderWriterStateT[?[_], E, S, L, ?]] {
private[data] sealed trait RWSTMonadTrans[E, S, L] extends MonadTrans[ReaderWriterStateT[?[_], E, S, L, ?]] {
implicit def L: Monoid[L]
type TC[F[_]] = Applicative[F]

def liftT[F[_], A](fa: F[A])(implicit F: Applicative[F]): ReaderWriterStateT[F, E, S, L, A] =
ReaderWriterStateT.lift(fa)
def liftT[M[_]: Monad, A](ma: M[A]): ReaderWriterStateT[M, E, S, L, A] =
ReaderWriterStateT.lift(ma)
}

private[data] sealed trait RWSTSemigroupK[F[_], E, S, L] extends SemigroupK[ReaderWriterStateT[F, E, S, L, ?]] {
Expand All @@ -507,7 +505,7 @@ private[data] sealed trait RWSTSemigroupK[F[_], E, S, L] extends SemigroupK[Read

private[data] sealed trait RWSTMonadCombine[F[_], E, S, L]
extends MonadCombine[ReaderWriterStateT[F, E, S, L, ?]] with RWSTMonad[F, E, S, L]
with RWSTSemigroupK[F, E, S, L] with RWSTTransLift[E, S, L] {
with RWSTSemigroupK[F, E, S, L] with RWSTMonadTrans[E, S, L] {

implicit def F: MonadCombine[F]
override def G: MonadCombine[F] = F
Expand Down
13 changes: 11 additions & 2 deletions tests/src/test/scala/cats/tests/ReaderWriterStateTTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,21 @@ class ReaderWriterStateTTests extends CatsSuite {
}

{
implicit def F = ListWrapper.monad
implicit def S = ListWrapper.semigroupK
implicit val F = ListWrapper.monad
implicit val S = ListWrapper.semigroupK

checkAll("ReaderWriterStateT[ListWrapper, String, Int, String, Int]",
SemigroupKTests[ReaderWriterStateT[ListWrapper, String, Int, String, ?]].semigroupK[Int])
}

{
implicit val F = ListWrapper.monad

checkAll("MonadTrans[ReaderWriterStateT[?[_], String, Int, String, ?]]",
MonadTransTests[ReaderWriterStateT[?[_], String, Int, String, ?]].monadTrans[ListWrapper, Int, Int])
checkAll("MonadTrans[ReaderWriterStateT[?[_], String, Int, String, ?]]",
SerializableTests.serializable(MonadTrans[ReaderWriterStateT[?[_], String, Int, String, ?]]))
}
}

object ReaderWriterStateTTests {
Expand Down
9 changes: 5 additions & 4 deletions tests/src/test/scala/cats/tests/StateTTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ class StateTTests extends CatsSuite {

checkAll("StateT[ListWrapper, Int, Int]", MonadTests[StateT[ListWrapper, Int, ?]].monad[Int, Int, Int])
checkAll("Monad[StateT[ListWrapper, Int, ?]]", SerializableTests.serializable(Monad[StateT[ListWrapper, Int, ?]]))
checkAll("MonadTrans[EitherT[?[_], String, ?]]", MonadTransTests[EitherT[?[_], String, ?]].monadTrans[ListWrapper, Int, Int])
checkAll("MonadTrans[StateT[?[_], Int, ?]]", MonadTransTests[StateT[?[_], String, ?]].monadTrans[ListWrapper, Int, Int])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops good catch!

checkAll("MonadTrans[StateT[?[_], Int, ?]]", SerializableTests.serializable(MonadTrans[StateT[?[_], Int, ?]]))

Monad[StateT[ListWrapper, Int, ?]]
FlatMap[StateT[ListWrapper, Int, ?]]
Expand All @@ -248,16 +249,16 @@ class StateTTests extends CatsSuite {

{
// F has a Monad and a SemigroupK
implicit def F = ListWrapper.monad
implicit def S = ListWrapper.semigroupK
implicit val F = ListWrapper.monad
implicit val S = ListWrapper.semigroupK

checkAll("StateT[ListWrapper, Int, Int]", SemigroupKTests[StateT[ListWrapper, Int, ?]].semigroupK[Int])
checkAll("SemigroupK[StateT[ListWrapper, Int, ?]]", SerializableTests.serializable(SemigroupK[StateT[ListWrapper, Int, ?]]))
}

{
// F has a MonadCombine
implicit def F = ListWrapper.monadCombine
implicit val F = ListWrapper.monadCombine

checkAll("StateT[ListWrapper, Int, Int]", MonadCombineTests[StateT[ListWrapper, Int, ?]].monadCombine[Int, Int, Int])
checkAll("MonadCombine[StateT[ListWrapper, Int, ?]]", SerializableTests.serializable(MonadCombine[StateT[ListWrapper, Int, ?]]))
Expand Down