-
-
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.
Add CommutativeApply and Applicative and an instance for Validated[Co…
…mmutativeSemigroup, ?]
- Loading branch information
Luka Jacobowitz
committed
Sep 24, 2017
1 parent
4a7f8f6
commit 8c95312
Showing
14 changed files
with
171 additions
and
8 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,14 @@ | ||
package cats | ||
|
||
import simulacrum.typeclass | ||
|
||
/** | ||
* Commutative Applicative. | ||
* | ||
* Further than an Applicative, which just allows composition of independent effectful functions, | ||
* in a Commutative Applicative those functions can be composed in any order, which guarantees | ||
* that their effects do not interfere. | ||
* | ||
* Must obey the laws defined in cats.laws.CommutativeApplicativeLaws. | ||
*/ | ||
@typeclass trait CommutativeApplicative[F[_]] extends Applicative[F] with CommutativeApply[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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package cats | ||
|
||
import simulacrum.typeclass | ||
|
||
/** | ||
* Commutative Apply. | ||
* | ||
* Further than an Apply, which just allows composition of independent effectful functions, | ||
* in a Commutative Apply those functions can be composed in any order, which guarantees | ||
* that their effects do not interfere. | ||
* | ||
* Must obey the laws defined in cats.laws.CommutativeApplyLaws. | ||
*/ | ||
@typeclass trait CommutativeApply[F[_]] extends Apply[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
12 changes: 12 additions & 0 deletions
12
laws/src/main/scala/cats/laws/CommutativeApplicativeLaws.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,12 @@ | ||
package cats.laws | ||
|
||
import cats.CommutativeApplicative | ||
|
||
trait CommutativeApplicativeLaws[F[_]] extends CommutativeApplyLaws[F] with ApplicativeLaws[F] { | ||
implicit override def F: CommutativeApplicative[F] | ||
} | ||
|
||
object CommutativeApplicativeLaws { | ||
def apply[F[_]](implicit ev: CommutativeApplicative[F]): CommutativeApplicativeLaws[F] = | ||
new CommutativeApplicativeLaws[F] { def F: CommutativeApplicative[F] = ev } | ||
} |
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,19 @@ | ||
package cats.laws | ||
|
||
import cats.CommutativeApply | ||
|
||
/** | ||
* Laws that must be obeyed by any `CommutativeApply`. | ||
*/ | ||
trait CommutativeApplyLaws[F[_]] extends ApplyLaws[F] { | ||
implicit override def F: CommutativeApply[F] | ||
|
||
def applyCommutative[A, B, C](fa: F[A], fb: F[B], f: (A, B) => C): IsEq[F[C]] = | ||
F.map2(fa, fb)(f) <-> F.map2(fb, fa)((b, a) => f(a, b)) | ||
|
||
} | ||
|
||
object CommutativeApplyLaws { | ||
def apply[F[_]](implicit ev: CommutativeApply[F]): CommutativeApplyLaws[F] = | ||
new CommutativeApplyLaws[F] { def F: CommutativeApply[F] = ev } | ||
} |
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
42 changes: 42 additions & 0 deletions
42
laws/src/main/scala/cats/laws/discipline/CommutativeApplicativeTests.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,42 @@ | ||
package cats | ||
package laws | ||
package discipline | ||
|
||
import cats.laws.discipline.CartesianTests.Isomorphisms | ||
import org.scalacheck.{Arbitrary, Cogen, Prop} | ||
|
||
trait CommutativeApplicativeTests[F[_]] extends CommutativeApplyTests[F] with ApplicativeTests[F] { | ||
|
||
def laws: CommutativeApplicativeLaws[F] | ||
|
||
def commutativeApplicative[A: Arbitrary: Eq, B: Arbitrary: Eq, C: Arbitrary: Eq](implicit | ||
ArbFA: Arbitrary[F[A]], | ||
ArbFB: Arbitrary[F[B]], | ||
ArbFC: Arbitrary[F[C]], | ||
ArbFAtoB: Arbitrary[F[A => B]], | ||
ArbFBtoC: Arbitrary[F[B => C]], | ||
CogenA: Cogen[A], | ||
CogenB: Cogen[B], | ||
CogenC: Cogen[C], | ||
EqFA: Eq[F[A]], | ||
EqFB: Eq[F[B]], | ||
EqFC: Eq[F[C]], | ||
EqFABC: Eq[F[(A, B, C)]], | ||
EqFInt: Eq[F[Int]], | ||
iso: Isomorphisms[F] | ||
): RuleSet = { | ||
new RuleSet { | ||
def name: String = "commutative applicative" | ||
def bases: Seq[(String, RuleSet)] = Nil | ||
def parents: Seq[RuleSet] = Seq(applicative[A, B, C], commutativeApply[A, B, C]) | ||
def props: Seq[(String, Prop)] = Nil | ||
} | ||
} | ||
} | ||
|
||
object CommutativeApplicativeTests { | ||
def apply[F[_]: CommutativeApplicative]: CommutativeApplicativeTests[F] = | ||
new CommutativeApplicativeTests[F] { | ||
def laws: CommutativeApplicativeLaws[F] = CommutativeApplicativeLaws[F] | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
laws/src/main/scala/cats/laws/discipline/CommutativeApplyTests.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,45 @@ | ||
package cats | ||
package laws | ||
package discipline | ||
|
||
import cats.laws.discipline.CartesianTests.Isomorphisms | ||
import org.scalacheck.{Arbitrary, Cogen, Prop} | ||
import Prop._ | ||
|
||
trait CommutativeApplyTests[F[_]] extends ApplyTests[F] { | ||
def laws: CommutativeApplyLaws[F] | ||
|
||
def commutativeApply[A: Arbitrary: Eq, B: Arbitrary: Eq, C: Arbitrary: Eq](implicit | ||
ArbFA: Arbitrary[F[A]], | ||
ArbFB: Arbitrary[F[B]], | ||
ArbFC: Arbitrary[F[C]], | ||
ArbFAtoB: Arbitrary[F[A => B]], | ||
ArbFBtoC: Arbitrary[F[B => C]], | ||
CogenA: Cogen[A], | ||
CogenB: Cogen[B], | ||
CogenC: Cogen[C], | ||
EqFA: Eq[F[A]], | ||
EqFB: Eq[F[B]], | ||
EqFC: Eq[F[C]], | ||
EqFABC: Eq[F[(A, B, C)]], | ||
EqFInt: Eq[F[Int]], | ||
iso: Isomorphisms[F] | ||
): RuleSet = { | ||
new RuleSet { | ||
def name: String = "commutative apply" | ||
def bases: Seq[(String, RuleSet)] = Nil | ||
def parents: Seq[RuleSet] = Seq(apply[A, B, C]) | ||
def props: Seq[(String, Prop)] = Seq( | ||
"apply commutativity" -> forAll(laws.applyCommutative[A, B, C] _) | ||
) | ||
} | ||
} | ||
|
||
} | ||
|
||
object CommutativeApplyTests { | ||
def apply[F[_]: CommutativeFlatMap]: CommutativeApplyTests[F] = | ||
new CommutativeApplyTests[F] { | ||
def laws: CommutativeApplyLaws[F] = CommutativeApplyLaws[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