Skip to content
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

Make Id covariant #3264

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions core/src/main/scala/cats/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,23 @@ package object cats {
* type `A` to get a pure value of type `B`. That is, the instance
* encodes pure unary function application.
*/
type Id[A] = A
type Id[+A] = A

// Workaround for a compiler bug that should be fixed soon.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now that the PR on scala is in, can we refer to it here?
scala/scala#8651

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kailuowang Yes, good idea (I linked it in the PR description but the comment would be more helpful).

// See https://github.com/scala/scala/pull/8651 for details.
private type IdWrapper = { type L[+A] = A }

type Endo[A] = A => A
implicit val catsInstancesForId
: Bimonad[Id] with CommutativeMonad[Id] with Comonad[Id] with NonEmptyTraverse[Id] with Distributive[Id] =
new Bimonad[Id] with CommutativeMonad[Id] with Comonad[Id] with NonEmptyTraverse[Id] with Distributive[Id] {
implicit val catsInstancesForId: Bimonad[IdWrapper#L]
with CommutativeMonad[IdWrapper#L]
with Comonad[IdWrapper#L]
with NonEmptyTraverse[IdWrapper#L]
with Distributive[IdWrapper#L] =
new Bimonad[IdWrapper#L]
with CommutativeMonad[IdWrapper#L]
with Comonad[IdWrapper#L]
with NonEmptyTraverse[IdWrapper#L]
with Distributive[IdWrapper#L] {
def pure[A](a: A): A = a
def extract[A](a: A): A = a
def flatMap[A, B](a: A)(f: A => B): B = f(a)
Expand Down Expand Up @@ -81,16 +93,18 @@ package object cats {
/**
* Witness for: Id[A] <-> Unit => A
*/
implicit val catsRepresentableForId: Representable.Aux[Id, Unit] = new Representable[Id] {
override type Representation = Unit
override val F: Functor[Id] = Functor[Id]
implicit val catsRepresentableForId: Representable.Aux[IdWrapper#L, Unit] =
new Representable[IdWrapper#L] {
override type Representation = Unit
override val F: Functor[Id] = Functor[Id]

override def tabulate[A](f: Unit => A): Id[A] = f(())
override def tabulate[A](f: Unit => A): Id[A] = f(())

override def index[A](f: Id[A]): Unit => A = (_: Unit) => f
}
override def index[A](f: Id[A]): Unit => A = (_: Unit) => f
}

implicit val catsParallelForId: Parallel.Aux[Id, Id] = Parallel.identity
implicit val catsParallelForId: Parallel.Aux[IdWrapper#L, IdWrapper#L] =
Parallel.identity[Id]

type Eq[A] = cats.kernel.Eq[A]
type PartialOrder[A] = cats.kernel.PartialOrder[A]
Expand Down