-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1621 from wedens/monad_trans
Replace TransLift with MonadTrans
- Loading branch information
Showing
23 changed files
with
127 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package cats | ||
|
||
/** | ||
* A type class which abstracts over the ability to lift an M[A] into a | ||
* MonadTransformer | ||
*/ | ||
trait MonadTrans[MT[_[_], _]] { | ||
|
||
/** | ||
* Lift a value of type M[A] into a monad transformer MT[M, A] | ||
*/ | ||
def liftT[M[_]: Monad, A](ma: M[A]): MT[M, A] | ||
} | ||
|
||
object MonadTrans { | ||
def apply[MT[_[_], _]](implicit MT: MonadTrans[MT]): MonadTrans[MT] = MT | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package cats | ||
package syntax | ||
|
||
trait MonadTransSyntax { | ||
implicit final def catsSyntaxMonadTrans[F[_], A](fa: F[A]): MonadTransOps[F, A] = new MonadTransOps(fa) | ||
} | ||
|
||
final class MonadTransOps[F[_], A](val fa: F[A]) extends AnyVal { | ||
def liftT[MT[_[_], _]](implicit F: Monad[F], MT: MonadTrans[MT]): MT[F, A] = | ||
MT.liftT(fa) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package cats | ||
package laws | ||
|
||
trait MonadTransLaws[MT[_[_], _]] { | ||
implicit def MT: MonadTrans[MT] | ||
|
||
def identity[G[_], A](a: A)(implicit G: Monad[G], MTM: Monad[MT[G, ?]]): IsEq[MT[G, A]] = | ||
MT.liftT(G.pure(a)) <-> MTM.pure(a) | ||
|
||
def composition[G[_], A, B](ga: G[A], f: A => G[B])(implicit G: Monad[G], MTM: Monad[MT[G, ?]]): IsEq[MT[G, B]] = | ||
MT.liftT(G.flatMap(ga)(f)) <-> MTM.flatMap(MT.liftT(ga))(a => MT.liftT(f(a))) | ||
} | ||
|
||
object MonadTransLaws { | ||
def apply[MT[_[_], _]](implicit ev: MonadTrans[MT]): MonadTransLaws[MT] = | ||
new MonadTransLaws[MT] { def MT: MonadTrans[MT] = ev } | ||
} |
36 changes: 36 additions & 0 deletions
36
laws/src/main/scala/cats/laws/discipline/MonadTransTests.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package cats | ||
package laws | ||
package discipline | ||
|
||
import org.scalacheck.{Arbitrary, Cogen, Prop} | ||
import org.typelevel.discipline.Laws | ||
import Prop._ | ||
|
||
trait MonadTransTests[MT[_[_], _]] extends Laws { | ||
def laws: MonadTransLaws[MT] | ||
|
||
def monadTrans[G[_]: Monad, A: Arbitrary: Eq, B: Eq](implicit | ||
MonadMTG: Monad[MT[G, ?]], | ||
ArbGA: Arbitrary[G[A]], | ||
ArbGB: Arbitrary[G[B]], | ||
CogenA: Cogen[A], | ||
EqGA: Eq[G[A]], | ||
EqGB: Eq[G[B]], | ||
EqMTGA: Eq[MT[G, A]], | ||
EqMTGB: Eq[MT[G, B]] | ||
): RuleSet = { | ||
new DefaultRuleSet( | ||
name = "monadTrans", | ||
parent = None, | ||
"monadTrans identity" -> forAll(laws.identity[G, A] _), | ||
"monadTrans composition" -> forAll(laws.composition[G, A, B] _) | ||
) | ||
} | ||
} | ||
|
||
object MonadTransTests { | ||
def apply[MT[_[_], _]: MonadTrans]: MonadTransTests[MT] = | ||
new MonadTransTests[MT] { | ||
def laws: MonadTransLaws[MT] = MonadTransLaws[MT] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.