Skip to content

Commit

Permalink
MonadTrans instance for RWST; fix MonadTrans test for StateT
Browse files Browse the repository at this point in the history
  • Loading branch information
wedens committed May 18, 2017
1 parent b4995b2 commit 688eff6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
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])
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

0 comments on commit 688eff6

Please sign in to comment.