-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Derive Contravariant and Invariant (#346)
- Loading branch information
1 parent
970ec37
commit 9ffcece
Showing
11 changed files
with
95 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package cats.derived | ||
|
||
import cats.Contravariant | ||
import shapeless3.deriving.K1 | ||
|
||
object contravariant extends ContravariantDerivation, Instances | ||
|
||
trait GenericContravariant[T[x[_]] <: Contravariant[x], F[_]](using inst: K1.Instances[T, F]) | ||
extends Contravariant[F]: | ||
|
||
def contramap[A, B](fa: F[A])(f: B => A): F[B] = inst.map(fa)( | ||
[t[_]] => (contra: T[t], t0: t[A]) => contra.contramap(t0)(f) | ||
) | ||
|
||
trait ContravariantDerivation: | ||
extension (F: Contravariant.type) | ||
inline def derived[F[_]](using gen: K1.Generic[F]): Contravariant[F] = | ||
new GenericContravariant[Contravariant, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package cats.derived | ||
|
||
import cats.* | ||
import shapeless3.deriving.Const | ||
|
||
private[derived] trait Instances extends Instances1: | ||
|
||
given [X](using X: Monoid[X]): MonoidK[Const[X]] with | ||
def empty[A]: Const[X][A] = X.empty | ||
|
||
def combineK[A](x: Const[X][A], y: Const[X][A]): Const[X][A] = | ||
X.combine(x, y) | ||
|
||
given [X]: Traverse[Const[X]] with Contravariant[Const[X]] with | ||
override def map[A, B](fa: Const[X][A])(f: A => B): Const[X][B] = fa | ||
|
||
def contramap[A, B](fa: X)(f: B => A): X = fa | ||
|
||
def foldLeft[A, B](fa: Const[X][A], b: B)(f: (B, A) => B): B = b | ||
|
||
def foldRight[A, B](fa: Const[X][A], lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] = lb | ||
|
||
def traverse[G[_]: Applicative, A, B](fa: Const[X][A])(f: A => G[B]): G[Const[X][B]] = | ||
Applicative[G].pure(fa) | ||
|
||
private[derived] trait Instances1: | ||
given [X](using X: Semigroup[X]): SemigroupK[Const[X]] with | ||
def combineK[A](x: Const[X][A], y: Const[X][A]): Const[X][A] = | ||
X.combine(x, y) |
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,17 @@ | ||
package cats.derived | ||
|
||
import cats.Invariant | ||
import shapeless3.deriving.K1 | ||
|
||
object invariant extends InvariantDerivation, Instances | ||
|
||
trait GenericInvariant[T[x[_]] <: Invariant[x], F[_]](using inst: K1.Instances[T, F]) | ||
extends Invariant[F]: | ||
def imap[A, B](fa: F[A])(f: A => B)(g: B => A): F[B] = inst.map(fa)( | ||
[t[_]] => (inv: T[t], t0: t[A]) => inv.imap(t0)(f)(g) | ||
) | ||
|
||
trait InvariantDerivation: | ||
extension (F: Invariant.type) | ||
inline def derived[F[_]](using gen: K1.Generic[F]): Invariant[F] = | ||
new GenericInvariant[Invariant, 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
11 changes: 11 additions & 0 deletions
11
core/src/test/scala-3/cats/derived/ContravariantTests.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,11 @@ | ||
package cats.derived | ||
|
||
import cats.Contravariant | ||
import cats.derived.all._ | ||
import cats.derived.all.given | ||
|
||
class ContravariantTests { | ||
|
||
case class Foo[A](f: A => String) derives Contravariant | ||
|
||
} |
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,11 @@ | ||
package cats.derived | ||
|
||
import cats.Invariant | ||
import cats.derived.all._ | ||
import cats.derived.all.given | ||
|
||
class InvariantTests { | ||
|
||
case class Foo[A](bar: String, baz: Option[A]) derives Invariant | ||
|
||
} |