-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Introduce Bifunctor Laws #559
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
package cats | ||
package data | ||
|
||
import cats.functor.Bifunctor | ||
|
||
/** Represents a right-biased disjunction that is either an `A`, or a `B`, or both an `A` and a `B`. | ||
* | ||
* An instance of `A [[Ior]] B` is one of: | ||
|
@@ -142,6 +144,11 @@ sealed abstract class IorInstances extends IorInstances0 { | |
def pure[B](b: B): A Ior B = Ior.right(b) | ||
def flatMap[B, C](fa: A Ior B)(f: B => A Ior C): A Ior C = fa.flatMap(f) | ||
} | ||
|
||
implicit def bifunctor: Bifunctor[Ior] = | ||
new Bifunctor[Ior] { | ||
override def bimap[A, B, C, D](fab: A Ior B)(f: (A) => C, g: (B) => D): C Ior D = fab.bimap(f, g) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you also remove the parens from |
||
} | ||
} | ||
|
||
sealed abstract class IorInstances0 { | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, this is super nitpicky, but could you name this
iorBifunctor
just to match the convention of the instances above it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similarly for the instances in the other files.