-
-
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.
- Loading branch information
1 parent
f4722ac
commit 3993dba
Showing
3 changed files
with
93 additions
and
0 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,50 @@ | ||
package cats | ||
package data | ||
|
||
import cats.arrow._ | ||
|
||
/** | ||
* The dual category of some other category, `Arr`. | ||
*/ | ||
final case class Op[Arr[_, _], A, B](run: Arr[B, A]) { | ||
def compose[Z](op: Op[Arr, Z, A])(implicit Arr: Compose[Arr]): Op[Arr, Z, B] = | ||
Op(Arr.compose(op.run, run)) | ||
|
||
def eqv(op: Op[Arr, A, B])(implicit Arr: Eq[Arr[B, A]]): Boolean = | ||
Arr.eqv(run, op.run) | ||
} | ||
|
||
object Op extends OpInstances | ||
|
||
private[data] sealed abstract class OpInstances extends OpInstances0 { | ||
implicit def catsDataCategoryForOp[Arr[_, _]](implicit ArrC: Category[Arr]): Category[Op[Arr, ?, ?]] = | ||
new OpCategory[Arr] { def Arr: Category[Arr] = ArrC } | ||
|
||
implicit def catsKernelEqForOp[Arr[_, _], A, B](implicit ArrEq: Eq[Arr[B, A]]): Eq[Op[Arr, A, B]] = | ||
new OpEq[Arr, A, B] { def Arr: Eq[Arr[B, A]] = ArrEq } | ||
} | ||
|
||
private[data] sealed abstract class OpInstances0 { | ||
implicit def catsDataComposeForOp[Arr[_, _]](implicit ArrC: Compose[Arr]): Compose[Op[Arr, ?, ?]] = | ||
new OpCompose[Arr] { def Arr: Compose[Arr] = ArrC } | ||
} | ||
|
||
private[data] trait OpCategory[Arr[_, _]] extends Category[Op[Arr, ?, ?]] with OpCompose[Arr] { | ||
implicit def Arr: Category[Arr] | ||
|
||
override def id[A]: Op[Arr, A, A] = Op(Arr.id) | ||
} | ||
|
||
private[data] trait OpCompose[Arr[_, _]] extends Compose[Op[Arr, ?, ?]] { | ||
implicit def Arr: Compose[Arr] | ||
|
||
def compose[A, B, C](f: Op[Arr, B, C], g: Op[Arr, A, B]): Op[Arr, A, C] = | ||
f.compose(g) | ||
} | ||
|
||
private[data] trait OpEq[Arr[_, _], A, B] extends Eq[Op[Arr, A, B]] { | ||
implicit def Arr: Eq[Arr[B, A]] | ||
|
||
def eqv(f: Op[Arr, A, B], g: Op[Arr, A, B]): Boolean = | ||
f.eqv(g) | ||
} |
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,37 @@ | ||
package cats | ||
package tests | ||
|
||
import cats.arrow._ | ||
import cats.data.{Kleisli, Op} | ||
import cats.laws.discipline._ | ||
import cats.laws.discipline.arbitrary._ | ||
import cats.laws.discipline.eq._ | ||
import cats.kernel.laws.discipline.EqTests | ||
|
||
class OpSuite extends CatsSuite { | ||
{ | ||
implicit val catsKernelEqForOp = Op.catsKernelEqForOp[Function1, Char, Int] | ||
checkAll("Op[Function1, Char, Int]", EqTests[Op[Function1, Char, Int]].eqv) | ||
checkAll("Eq[Op[Function1, Char, Int]]", SerializableTests.serializable(Eq[Op[Function1, Char, Int]])) | ||
} | ||
|
||
{ | ||
implicit val catsDataCategoryForOp = Op.catsDataCategoryForOp[Function1] | ||
checkAll("Op[Function1, Char, Int]", CategoryTests[Op[Function1, ?, ?]].category[Char, Int, Char, Int]) | ||
checkAll("Category[Op[Function1, ?, ?]]", SerializableTests.serializable(Category[Op[Function1, ?, ?]])) | ||
} | ||
|
||
/** | ||
* Testing that implicit resolution works. If it compiles, the "test" passes. | ||
*/ | ||
object ImplicitResolution { | ||
// Arr is Function1 | ||
Category[Op[Function1, ?, ?]] | ||
Compose[Op[Function1, ?, ?]] | ||
Eq[Op[Function1, Char, Int]] | ||
|
||
// Arr is Kleisli[Option, ?, ?] | ||
Category[Op[Kleisli[Option, ?, ?], ?, ?]] | ||
Compose[Op[Kleisli[Option, ?, ?], ?, ?]] | ||
} | ||
} |