Skip to content

Commit

Permalink
Add MFunctor type class for mapping monad morphisms over monad transf…
Browse files Browse the repository at this point in the history
…ormers
  • Loading branch information
kcsongor committed Dec 10, 2016
1 parent 73a6481 commit 87589d9
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 0 deletions.
16 changes: 16 additions & 0 deletions core/src/main/scala/cats/MFunctor.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package cats

/**
* A type class which abstracts over the ability to lift a natural transformation
* (monad morphism) `M ~> N` into a MonadTransformer. In other words, a functor
* in the category of monads.
*/

trait MFunctor[MT[_[_]]] {

/**
* Analogous to fmap
*/
def hoist[M[_], N[_]](m: M ~> N): MT[M] => MT[N]

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

implicit def catsDataMFunctorForEither[E, A]: MFunctor[EitherT[?[_], E, A]] =
new MFunctor[EitherT[?[_], E, A]] {
def hoist[M[_], N[_]](m: M ~> N): EitherT[M, E, A] => EitherT[N, E, A] =
e => EitherT(m(e.value))
}

implicit def catsMonoidForEitherT[F[_], L, A](implicit F: Monoid[F[Either[L, A]]]): Monoid[EitherT[F, L, A]] =
new EitherTMonoid[F, L, A] { implicit val F0 = F }

Expand Down
6 changes: 6 additions & 0 deletions core/src/main/scala/cats/data/IdT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ private[data] sealed abstract class IdTInstances0 extends IdTInstances1 {

implicit def catsDataOrderForIdT[F[_], A](implicit F: Order[F[A]]): Order[IdT[F, A]] =
F.on(_.value)

implicit def catsDataMFunctorForIdT[F[_], A]: MFunctor[IdT[?[_], A]] =
new MFunctor[IdT[?[_], A]] {
def hoist[M[_], N[_]](m: M ~> N): IdT[M, A] => IdT[N, A] =
i => IdT(m(i.value))
}
}

private[data] sealed abstract class IdTInstances extends IdTInstances0 {
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/scala/cats/data/OptionT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,12 @@ private[data] sealed trait OptionTInstances1 extends OptionTInstances2 {
def liftT[M[_]: Functor, A](ma: M[A]): OptionT[M, A] = OptionT.liftF(ma)
}

implicit def catsDataMFunctorForOptionT[A]: MFunctor[OptionT[?[_], A]] =
new MFunctor[OptionT[?[_], A]] {
def hoist[M[_], N[_]](m: M ~> N): OptionT[M, A] => OptionT[N, A] =
o => OptionT(m(o.value))
}

implicit def catsDataMonoidKForOptionT[F[_]](implicit F0: Monad[F]): MonoidK[OptionT[F, ?]] =
new OptionTMonoidK[F] { implicit val F = F0 }

Expand Down
6 changes: 6 additions & 0 deletions core/src/main/scala/cats/data/WriterT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ private[data] sealed abstract class WriterTInstances extends WriterTInstances0 {
WriterT(Functor[M].map(ma)((W.empty, _)))
}

implicit def catsDataMFunctorForWriterT[W, A](implicit W: Monoid[W]): MFunctor[WriterT[?[_], W, A]] =
new MFunctor[WriterT[?[_], W, A]] {
def hoist[M[_], N[_]](m: M ~> N): WriterT[M, W, A] => WriterT[N, W, A] =
w => WriterT(m(w.run))
}

implicit def catsDataShowForWriterT[F[_], L, V](implicit F: Show[F[(L, V)]]): Show[WriterT[F, L, V]] = new Show[WriterT[F, L, V]] {
override def show(f: WriterT[F, L, V]): String = f.show
}
Expand Down
6 changes: 6 additions & 0 deletions free/src/main/scala/cats/free/FreeT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,12 @@ private[free] sealed trait FreeTInstances1 extends FreeTInstances2 {
override def liftT[M[_]: Functor, A](ma: M[A]): FreeT[S, M, A] =
FreeT.liftT(ma)
}

implicit def catsDataMFunctorForFreeT[S[_], A]: MFunctor[FreeT[S, ?[_], A]] =
new MFunctor[FreeT[S, ?[_], A]] {
def hoist[M[_], N[_]](m: M ~> N): FreeT[S, M, A] => FreeT[S, N, A] =
f => f.hoist(m)
}
}

private[free] sealed trait FreeTInstances0 extends FreeTInstances1 {
Expand Down

0 comments on commit 87589d9

Please sign in to comment.