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

Add two new laws for Bimonad. #596

Merged
merged 1 commit into from
Nov 3, 2015
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
9 changes: 9 additions & 0 deletions laws/src/main/scala/cats/laws/BimonadLaws.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package laws

/**
* Laws that must be obeyed by any `Bimonad`.
*
* For more information, see definition 4.1 from this paper:
* http://arxiv.org/pdf/0710.1163v3.pdf
*/
trait BimonadLaws[F[_]] extends MonadLaws[F] with ComonadLaws[F] {
implicit override def F: Bimonad[F]
Expand All @@ -12,6 +15,12 @@ trait BimonadLaws[F[_]] extends MonadLaws[F] with ComonadLaws[F] {

def extractPureIsId[A](fa: F[A]): IsEq[F[A]] =
F.pure(F.extract(fa)) <-> fa

def extractFlatMapEntwining[A](ffa: F[F[A]]): IsEq[A] =
F.extract(F.flatten(ffa)) <-> F.extract(F.map(ffa)(F.extract))

def pureCoflatMapEntwining[A](a: A): IsEq[F[F[A]]] =
F.coflatten(F.pure(a)) <-> F.map(F.pure(a))(F.pure)
}

object BimonadLaws {
Expand Down
6 changes: 5 additions & 1 deletion laws/src/main/scala/cats/laws/discipline/BimonadTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ trait BimonadTests[F[_]] extends MonadTests[F] with ComonadTests[F] {

def bimonad[A: Arbitrary: Eq, B: Arbitrary: Eq, C: Arbitrary: Eq]: RuleSet = {
implicit val arbfa: Arbitrary[F[A]] = ArbitraryK[F].synthesize[A]
implicit val arbffa: Arbitrary[F[F[A]]] = ArbitraryK[F].synthesize[F[A]]
implicit val eqfa: Eq[F[A]] = EqK[F].synthesize[A]
implicit val eqffa: Eq[F[F[A]]] = EqK[F].synthesize[F[A]]
new RuleSet {
def name: String = "bimonad"
def bases: Seq[(String, RuleSet)] = Nil
def parents: Seq[RuleSet] = Seq(monad[A, B, C], comonad[A, B, C])
def props: Seq[(String, Prop)] = Seq(
"pure andThen extract = id" -> forAll(laws.pureExtractIsId[A] _),
"extract andThen pure = id" -> forAll(laws.extractPureIsId[A] _)
"extract andThen pure = id" -> forAll(laws.extractPureIsId[A] _),
"extract/flatMap entwining" -> forAll(laws.extractFlatMapEntwining[A] _),
"pure/coflatMap entwining" -> forAll(laws.pureCoflatMapEntwining[A] _)
)
}
}
Expand Down