Skip to content

Commit

Permalink
Move monoid and semigroup functions to the instance
Browse files Browse the repository at this point in the history
  • Loading branch information
Luka Jacobowitz committed Dec 12, 2017
1 parent 38b46e6 commit f4b7490
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 26 deletions.
93 changes: 93 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,99 @@ def mimaSettings(moduleName: String) = Seq(
exclude[ReversedMissingMethodProblem]("cats.syntax.MonadErrorSyntax.catsSyntaxMonadErrorRethrow"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.composeApply"),
exclude[DirectMissingMethodProblem]("cats.Semigroupal.catsSemigroupalForMonoid"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantMonoidal.monoid"),
exclude[ReversedMissingMethodProblem]("cats.ContravariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[ReversedMissingMethodProblem]("cats.Applicative.monoid"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantMonoidal.monoid"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantMonoidal.monoid"),
exclude[ReversedMissingMethodProblem]("cats.InvariantMonoidal.monoid"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantMonoidal.monoid"),
exclude[ReversedMissingMethodProblem]("cats.Apply.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantMonoidal.monoid"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantMonoidal.monoid"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantMonoidal.monoid"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantMonoidal.monoid"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantMonoidal.monoid"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantMonoidal.monoid"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantMonoidal.monoid"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantMonoidal.monoid"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantMonoidal.monoid"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantMonoidal.monoid"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantMonoidal.monoid"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantMonoidal.monoid"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantMonoidal.monoid"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantMonoidal.monoid"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantMonoidal.monoid"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantMonoidal.monoid"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantMonoidal.monoid"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantMonoidal.monoid"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantMonoidal.monoid"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantMonoidal.monoid"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantMonoidal.monoid"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantMonoidal.monoid"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantMonoidal.monoid"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantMonoidal.monoid"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantMonoidal.monoid"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantMonoidal.monoid"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantMonoidal.monoid"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantMonoidal.monoid"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantMonoidal.monoid"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantMonoidal.monoid"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantMonoidal.monoid"),
exclude[InheritedNewAbstractMethodProblem]("cats.InvariantSemigroupal.semigroup"),
exclude[ReversedMissingMethodProblem]("cats.instances.InvariantMonoidalInstances.catsSemigroupalForMonoid"),
exclude[DirectMissingMethodProblem]("cats.data.CokleisliArrow.id"),
exclude[IncompatibleResultTypeProblem]("cats.data.CokleisliArrow.id")
)
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/scala/cats/Applicative.scala
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ import simulacrum.typeclass
*/
def whenA[A](cond: Boolean)(f: => F[A]): F[Unit] =
if (cond) void(f) else pure(())

override def monoid[A](implicit A: Monoid[A]): Monoid[F[A]] =
new ApplicativeMonoid[F, A](this, A)
}

object Applicative {
Expand Down
5 changes: 4 additions & 1 deletion core/src/main/scala/cats/Apply.scala
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,14 @@ trait Apply[F[_]] extends Functor[F] with InvariantSemigroupal[F] with ApplyArit
val F = self
val G = Apply[G]
}

override def semigroup[A](implicit A: Semigroup[A]): Semigroup[F[A]] =
new ApplySemigroup[F, A](this, A)
}

object Apply {
def semigroup[F[_], A](implicit f: Apply[F], sg: Semigroup[A]): Semigroup[F[A]] =
new ApplySemigroup[F, A](f, sg)
f.semigroup
}

private[cats] class ApplySemigroup[F[_], A](f: Apply[F], sg: Semigroup[A]) extends Semigroup[F[A]] {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/ContravariantMonoidal.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import simulacrum.typeclass
override def pure[A](a: A): F[A] = unit
}
object ContravariantMonoidal extends SemigroupalArityFunctions {
def monoid[F[_], A](implicit f: ContravariantMonoidal[F], monoid: Monoid[A]): Monoid[F[A]] =
def monoid[F[_], A](implicit f: ContravariantMonoidal[F]): Monoid[F[A]] =
new ContravariantMonoidalMonoid[F, A](f)
}

Expand Down
3 changes: 3 additions & 0 deletions core/src/main/scala/cats/ContravariantSemigroupal.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import simulacrum.typeclass
def F = self
def G = Functor[G]
}

override def semigroup[A](implicit A: Semigroup[A]): Semigroup[F[A]] =
new ContravariantSemigroupalSemigroup[F, A](this)
}

object ContravariantSemigroupal extends SemigroupalArityFunctions {
Expand Down
11 changes: 7 additions & 4 deletions core/src/main/scala/cats/InvariantMonoidal.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ import simulacrum.typeclass
* }}}
*/
def pure[A](a: A): F[A]
}

object InvariantMonoidal {
def monoid[F[_], A](implicit f: InvariantMonoidal[F], monoid: Monoid[A]): Monoid[F[A]] =
new InvariantMonoidalMonoid[F, A](f, monoid)
/**
* Gives a `Monoid` instance if A itself has a `Monoid` instance.
*/
def monoid[A](implicit A: Monoid[A]): Monoid[F[A]] =
new InvariantMonoidalMonoid[F, A](this, A)

}


private[cats] class InvariantMonoidalMonoid[F[_], A](f: InvariantMonoidal[F], monoid: Monoid[A]) extends InvariantSemigroupalSemigroup(f, monoid) with Monoid[F[A]] {
def empty: F[A] = f.pure(monoid.empty)
}
23 changes: 6 additions & 17 deletions core/src/main/scala/cats/InvariantSemigroupal.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,15 @@ import simulacrum.typeclass
def G = Apply[G]
}

/**
* Gives a `Semigroup` instance if A itself has a `Semigroup` instance.
*/
def semigroup[A](implicit A: Semigroup[A]): Semigroup[F[A]] =
new InvariantSemigroupalSemigroup[F, A](this, A)

}

object InvariantSemigroupal extends SemigroupalArityFunctions {
def semigroup[F[_], A](implicit f: InvariantSemigroupal[F], sg: Semigroup[A]): Semigroup[F[A]] =
new InvariantSemigroupalSemigroup[F, A](f, sg)

implicit def catsSemigroupalForMonoid: InvariantSemigroupal[Monoid] = new InvariantSemigroupal[Monoid] {
def product[A, B](fa: Monoid[A], fb: Monoid[B]): Monoid[(A, B)] = new Monoid[(A, B)] {
val empty = fa.empty -> fb.empty
def combine(x: (A, B), y: (A, B)): (A, B) = fa.combine(x._1, y._1) -> fb.combine(x._2, y._2)
}

def imap[A, B](fa: Monoid[A])(f: A => B)(g: B => A): Monoid[B] = new Monoid[B] {
def empty: B = f(fa.empty)

def combine(x: B, y: B): B = f(fa.combine(g(x), g(y)))
}
}
}
object InvariantSemigroupal extends SemigroupalArityFunctions

private[cats] class InvariantSemigroupalSemigroup[F[_], A](f: InvariantSemigroupal[F], sg: Semigroup[A]) extends Semigroup[F[A]] {
def combine(a: F[A], b: F[A]): F[A] =
Expand Down
15 changes: 14 additions & 1 deletion core/src/main/scala/cats/instances/invariant.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
package cats.instances

import cats.kernel._
import cats.InvariantMonoidal
import cats.{InvariantMonoidal, Monoid, InvariantSemigroupal}

trait InvariantMonoidalInstances {

implicit def catsSemigroupalForMonoid: InvariantSemigroupal[Monoid] = new InvariantSemigroupal[Monoid] {
def product[A, B](fa: Monoid[A], fb: Monoid[B]): Monoid[(A, B)] = new Monoid[(A, B)] {
val empty = fa.empty -> fb.empty
def combine(x: (A, B), y: (A, B)): (A, B) = fa.combine(x._1, y._1) -> fb.combine(x._2, y._2)
}

def imap[A, B](fa: Monoid[A])(f: A => B)(g: B => A): Monoid[B] = new Monoid[B] {
def empty: B = f(fa.empty)

def combine(x: B, y: B): B = f(fa.combine(g(x), g(y)))
}
}

implicit val catsInvariantMonoidalSemigroup: InvariantMonoidal[Semigroup] = new InvariantMonoidal[Semigroup] {
def product[A, B](fa: Semigroup[A], fb: Semigroup[B]): Semigroup[(A, B)] = new Semigroup[(A, B)] {
def combine(x: (A, B), y: (A, B)): (A, B) = fa.combine(x._1, y._1) -> fb.combine(x._2, y._2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class CsvCodecInvariantMonoidalSuite extends CatsSuite {
checkAll("InvariantMonoidal[CsvCodec]", SerializableTests.serializable(InvariantMonoidal[CsvCodec]))

{
implicit val csvMonoid = InvariantMonoidal.monoid[CsvCodec, Int]
implicit val csvMonoid = InvariantMonoidal[CsvCodec].monoid[Int]
checkAll("InvariantMonoidal[CsvCodec].monoid", MonoidTests[CsvCodec[Int]].monoid)
}
}
1 change: 0 additions & 1 deletion tests/src/test/scala/cats/tests/MonoidSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class MonoidSuite extends CatsSuite {
object MonoidSuite {
def summonInstance(): Unit = {
Invariant[Monoid]
InvariantSemigroupal[Monoid]
()
}

Expand Down

0 comments on commit f4b7490

Please sign in to comment.