-
-
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 #559 from agenovese/Issue557
Introduce Bifunctor Laws
- Loading branch information
Showing
10 changed files
with
104 additions
and
6 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
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,25 @@ | ||
package cats.laws | ||
|
||
import cats.functor.Bifunctor | ||
import cats.syntax.bifunctor._ | ||
|
||
/** | ||
* Laws that must be obeyed by any `Bifunctor`. | ||
*/ | ||
trait BifunctorLaws[F[_, _]] { | ||
implicit def F: Bifunctor[F] | ||
|
||
def bifunctorIdentity[A, B](fa: F[A, B]): IsEq[F[A, B]] = | ||
fa.bimap(identity, identity) <-> fa | ||
|
||
def bifunctorComposition[A, B, C, X, Y, Z](fa: F[A, X], f: A => B, f2: B => C, g: X => Y, g2: Y => Z): IsEq[F[C, Z]] = { | ||
fa.bimap(f, g).bimap(f2, g2) <-> fa.bimap(f andThen f2, g andThen g2) | ||
} | ||
} | ||
|
||
object BifunctorLaws { | ||
def apply[F[_,_]](implicit ev: Bifunctor[F]): BifunctorLaws[F] = | ||
new BifunctorLaws[F] { | ||
def F: Bifunctor[F] = ev | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
laws/src/main/scala/cats/laws/discipline/BifunctorTests.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.laws.discipline | ||
|
||
import cats.Eq | ||
import cats.functor.Bifunctor | ||
import cats.laws.BifunctorLaws | ||
import org.scalacheck.Arbitrary | ||
import org.scalacheck.Prop._ | ||
import org.typelevel.discipline.Laws | ||
|
||
trait BifunctorTests[F[_, _]] extends Laws { | ||
def laws: BifunctorLaws[F] | ||
|
||
def bifunctor[A, A2, A3, B, B2, B3](implicit | ||
ArbFAB: Arbitrary[F[A, B]], | ||
ArbA2: Arbitrary[A => A2], | ||
ArbA3: Arbitrary[A2 => A3], | ||
ArbB2: Arbitrary[B => B2], | ||
ArbB3: Arbitrary[B2 => B3], | ||
EqFAB: Eq[F[A, B]], | ||
EqFCZ: Eq[F[A3, B3]] | ||
): RuleSet = { | ||
new DefaultRuleSet( | ||
name = "Bifunctor", | ||
parent = None, | ||
"Bifunctor Identity" -> forAll(laws.bifunctorIdentity[A, B] _), | ||
"Bifunctor associativity" -> forAll(laws.bifunctorComposition[A, A2, A3, B, B2, B3] _) | ||
) | ||
} | ||
} | ||
|
||
object BifunctorTests { | ||
def apply[F[_, _] : Bifunctor]: BifunctorTests[F] = | ||
new BifunctorTests[F] { | ||
def laws: BifunctorLaws[F] = BifunctorLaws[F] | ||
} | ||
} |
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