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

Added commutative apply and applicative to Const #2204

Merged
merged 5 commits into from
Mar 24, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
69 changes: 47 additions & 22 deletions core/src/main/scala/cats/data/Const.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cats
package data

import cats.Contravariant
import cats.kernel.{CommutativeMonoid, CommutativeSemigroup}

/**
* [[Const]] is a phantom type, it does not contain a value of its second type parameter `B`
Expand Down Expand Up @@ -105,6 +106,19 @@ private[data] sealed abstract class ConstInstances extends ConstInstances0 {
}

private[data] sealed abstract class ConstInstances0 extends ConstInstances1 {

implicit def catsDataCommutativeApplicativeForConst[C](implicit C: CommutativeMonoid[C]): CommutativeApplicative[Const[C, ?]] =
new ConstApplicative[C] with CommutativeApplicative[Const[C, ?]] { val C0: CommutativeMonoid[C] = C }
}

private[data] sealed abstract class ConstInstances1 extends ConstInstances2 {

implicit def catsDataCommutativeApplyForConst[C](implicit C: CommutativeSemigroup[C]): CommutativeApply[Const[C, ?]] =
new ConstApply[C] with CommutativeApply[Const[C, ?]] { val C0: CommutativeSemigroup[C] = C }
}

private[data] sealed abstract class ConstInstances2 extends ConstInstances3 {

implicit def catsDataSemigroupForConst[A: Semigroup, B]: Semigroup[Const[A, B]] = new Semigroup[Const[A, B]] {
def combine(x: Const[A, B], y: Const[A, B]): Const[A, B] = x combine y
}
Expand All @@ -119,36 +133,47 @@ private[data] sealed abstract class ConstInstances0 extends ConstInstances1 {
x partialCompare y
}

implicit def catsDataApplicativeForConst[C: Monoid]: Applicative[Const[C, ?]] = new Applicative[Const[C, ?]] {
def pure[A](x: A): Const[C, A] =
Const.empty

def ap[A, B](f: Const[C, A => B])(fa: Const[C, A]): Const[C, B] =
f.retag[B] combine fa.retag[B]

override def map[A, B](fa: Const[C, A])(f: A => B): Const[C, B] =
fa.retag[B]

override def product[A, B](fa: Const[C, A], fb: Const[C, B]): Const[C, (A, B)] =
fa.retag[(A, B)] combine fb.retag[(A, B)]
}
implicit def catsDataApplicativeForConst[C](implicit C: Monoid[C]): Applicative[Const[C, ?]] =
new ConstApplicative[C] { val C0: Monoid[C] = C }
}

private[data] sealed abstract class ConstInstances1 {
private[data] sealed abstract class ConstInstances3 extends ConstInstances4 {

implicit def catsDataEqForConst[A: Eq, B]: Eq[Const[A, B]] = new Eq[Const[A, B]] {
def eqv(x: Const[A, B], y: Const[A, B]): Boolean =
x === y
}

implicit def catsDataApplyForConst[C: Semigroup]: Apply[Const[C, ?]] = new Apply[Const[C, ?]] {
def ap[A, B](f: Const[C, A => B])(fa: Const[C, A]): Const[C, B] =
fa.retag[B] combine f.retag[B]
implicit def catsDataApplyForConst[C](implicit C: Semigroup[C]): Apply[Const[C, ?]] =
new ConstApply[C] { val C0: Semigroup[C] = C }
}

override def product[A, B](fa: Const[C, A], fb: Const[C, B]): Const[C, (A, B)] =
fa.retag[(A, B)] combine fb.retag[(A, B)]
private[data] sealed abstract class ConstInstances4 {

def map[A, B](fa: Const[C, A])(f: A => B): Const[C, B] =
fa.retag[B]
}
implicit def catsDataFunctorForConst[C]: Functor[Const[C, ?]] =
new ConstFunctor[C]{}
}

private[data] sealed trait ConstFunctor[C] extends Functor[Const[C, ?]] {
def map[A, B](fa: Const[C, A])(f: A => B): Const[C, B] =
fa.retag[B]
}

private[data] sealed trait ConstApply[C] extends ConstFunctor[C] with Apply[Const[C, ?]] {

implicit def C0: Semigroup[C]

def ap[A, B](f: Const[C, A => B])(fa: Const[C, A]): Const[C, B] =
f.retag[B] combine fa.retag[B]

override def product[A, B](fa: Const[C, A], fb: Const[C, B]): Const[C, (A, B)] =
fa.retag[(A, B)] combine fb.retag[(A, B)]
}

private[data] sealed trait ConstApplicative[C] extends ConstApply[C] with Applicative[Const[C, ?]] {

implicit def C0: Monoid[C]

def pure[A](x: A): Const[C, A] =
Const.empty
}
24 changes: 23 additions & 1 deletion tests/src/test/scala/cats/tests/ConstSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package cats
package tests


import cats.kernel.laws.discipline.{MonoidTests, SemigroupTests, OrderTests, PartialOrderTests, EqTests}
import cats.kernel.laws.discipline.{EqTests, MonoidTests, OrderTests, PartialOrderTests, SemigroupTests}
import cats.Contravariant
import cats.data.{Const, NonEmptyList}
import cats.kernel.{CommutativeMonoid, CommutativeSemigroup}
import cats.laws.discipline._
import cats.laws.discipline.arbitrary._

Expand Down Expand Up @@ -66,6 +67,27 @@ class ConstSuite extends CatsSuite {
}
}

checkAll("Const[String, Int]", FunctorTests[Const[String, ?]].functor[Int, Int, Int])
checkAll("Functor[Const[String, ?]]", SerializableTests.serializable(Functor[Const[String, ?]]))

{
implicit val intCommutativeMonoid: CommutativeMonoid[Int] =
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of defining this instance just for this test, I think that you should be able to use CMono from Helpers.scala. You can see an example of it being used in FunctionSuite.scala. Similarly for the commutative semigroup below (CSemi).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure, I will change it 👍

new CommutativeMonoid[Int] {
def empty: Int = 0
def combine(x: Int, y: Int): Int = x + y
}
implicit val iso = SemigroupalTests.Isomorphisms.invariant[Const[Int, ?]](Const.catsDataContravariantForConst)
checkAll("Const[Int, Int]", CommutativeApplicativeTests[Const[Int, ?]].commutativeApplicative[Int, Int, Int])
checkAll("CommutativeApplicative[Const[Int, ?]]", SerializableTests.serializable(CommutativeApplicative[Const[Int, ?]]))
}

{
implicit val intCommutativeSemigroup: CommutativeSemigroup[Int] =
new CommutativeSemigroup[Int] {
def combine(x: Int, y: Int): Int = x + y
}
implicit val iso = SemigroupalTests.Isomorphisms.invariant[Const[Int, ?]](Const.catsDataContravariantForConst)
checkAll("Const[Int, Int]", CommutativeApplyTests[Const[Int, ?]].commutativeApply[Int, Int, Int])
checkAll("CommutativeApply[Const[Int, ?]]", SerializableTests.serializable(CommutativeApply[Const[Int, ?]]))
}
}