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

Attempt to implement ContT instances #372

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
10 changes: 10 additions & 0 deletions core/src/main/scala/cats/mtl/Chronicle.scala
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,16 @@ private[mtl] trait ChronicleInstances {
case Ior.Right((l, s2, a)) => (l, s2, Ior.Right(a))
})
}

implicit def chronicleForContT[F[_]: Defer: Monad, E, C: Monoid](
implicit F: Chronicle[F, E]): Chronicle[ContT[F, C, *], E] =
new Chronicle[ContT[F, C, *], E] {
val monad: Monad[ContT[F, C, *]] =
MonadPartialOrder.monadPartialOrderForContT[F, C].monadG
def dictate(c: E): ContT[F, C, Unit] = ContT.liftF(F.dictate(c))
def confess[A](c: E): ContT[F, C, A] = ContT.liftF(F.confess[A](c))
def materialize[A](fa: ContT[F, C, A]): ContT[F, C, Ior[E, A]] = ???
}
}

object Chronicle extends ChronicleInstances {
Expand Down
15 changes: 15 additions & 0 deletions core/src/main/scala/cats/mtl/Handle.scala
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,21 @@ private[mtl] trait HandleInstances extends HandleLowPriorityInstances {
f: E => RWST[F, R, L, S, A]): RWST[F, R, L, S, A] =
RWST { case (r, s) => F0.handleWith(fa.run(r, s))(e => f(e).run(r, s)) }
}

implicit def handleContT[F[_]: Defer, E, C](
implicit F0: Handle[F, E],
M: Monad[F]): Handle[ContT[F, C, *], E] =
new RaiseMonadPartialOrder[F, ContT[F, C, *], E] with Handle[ContT[F, C, *], E] {

val applicative: Applicative[ContT[F, C, *]] = ContT.catsDataContTMonad

val F: Raise[F, E] = F0
val lift: MonadPartialOrder[F, ContT[F, C, *]] =
MonadPartialOrder.monadPartialOrderForContT

def handleWith[A](fa: ContT[F, C, A])(f: E => ContT[F, C, A]): ContT[F, C, A] =
ContT(c => F0.handleWith(fa.run(c))(e => f(e).run(c)))
}
}

object Handle extends HandleInstances {
Expand Down
22 changes: 19 additions & 3 deletions core/src/main/scala/cats/mtl/Listen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,18 @@
package cats
package mtl

import cats.data.{EitherT, StateT, IorT, Kleisli, OptionT, ReaderWriterStateT => RWST, WriterT}
import cats.data.{
ContT,
EitherT,
Ior,
IorT,
Kleisli,
OptionT,
StateT,
WriterT,
ReaderWriterStateT => RWST
}
import cats.syntax.all._
import cats.data.Ior

import scala.annotation.implicitNotFound

Expand Down Expand Up @@ -75,7 +84,7 @@ private[mtl] abstract class ListenKleisli[F[_]: Functor, R, L](implicit F: Liste
def functor = Functor[Kleisli[F, R, *]]
def tell(l: L) = Kleisli.liftF(F.tell(l))
def listen[A](fa: Kleisli[F, R, A]) =
Kleisli(a => F.listen(fa.run(a)))
Kleisli((a: R) => F.listen(fa.run(a)))
}

private[mtl] abstract class ListenStateT[F[_]: Applicative, S, L](implicit F: Listen[F, L])
Expand Down Expand Up @@ -148,6 +157,13 @@ private[mtl] abstract class ListenInductiveRWST[F[_]: Applicative, E, L0: Monoid
}
}

private[mtl] abstract class ListenContT[F[_]: FlatMap, R, L](implicit F: Listen[F, L])
extends Listen[ContT[F, R, *], L] {
def functor: Functor[ContT[F, R, *]] = Functor.asInstanceOf[Functor[ContT[F, R, *]]]
def tell(l: L): ContT[F, R, Unit] = ContT.liftF[F, R, Unit](F.tell(l))
def listen[A](fa: ContT[F, R, A]): ContT[F, R, (A, L)] = ???
}

private[mtl] trait LowPriorityListenInstances {

implicit def inductiveListenForWriterT[F[_]: Applicative, L, L0: Monoid](
Expand Down
7 changes: 7 additions & 0 deletions core/src/main/scala/cats/mtl/MonadPartialOrder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ private[mtl] trait MonadPartialOrderInstances {
val monadF: Monad[F] = F
val monadG: Monad[IorT[F, E, *]] = IorT.catsDataMonadErrorForIorT[F, E]
}

implicit def monadPartialOrderForContT[F[_]: Defer, C](implicit F: Monad[F]): MonadPartialOrder[F, ContT[F, C, *]] =
new MonadPartialOrder[F, ContT[F, C, *]] {
def apply[A](fa: F[A]): ContT[F, C, A] = ContT.liftF(fa)
val monadF: Monad[F] = F
val monadG: Monad[ContT[F, C, *]] = ContT.catsDataContTMonad[F, C]
}
}

object MonadPartialOrder extends MonadPartialOrderInstances {
Expand Down