Skip to content

Commit

Permalink
Derive Commutative{Semigroup, Monoid} (#341)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimWSpence authored May 19, 2021
1 parent cd6baa2 commit 803b582
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 10 deletions.
14 changes: 8 additions & 6 deletions core/src/main/scala-3.0.0-RC3/cats/derived/all.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package cats.derived

object all extends
CommutativeMonoidDerivation,
CommutativeSemigroupDerivation,
EmptyDerivation,
SemigroupDerivation,
MonoidDerivation,
EqDerivation,
PartialOrderDerivation,
OrderDerivation,
HashDerivation,
FunctorDerivation,
FoldableDerivation,
FunctorDerivation,
HashDerivation,
MonoidDerivation,
OrderDerivation,
PartialOrderDerivation,
SemigroupDerivation,
TraverseDerivation
14 changes: 14 additions & 0 deletions core/src/main/scala-3.0.0-RC3/cats/derived/commutativeMonoid.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cats.derived

import cats.kernel.CommutativeMonoid
import shapeless3.deriving.K0

object commutativeMonoid extends CommutativeMonoidDerivation

trait ProductCommutativeMonoid[T[x] <: CommutativeMonoid[x], A](using inst: K0.ProductInstances[T, A])
extends ProductCommutativeSemigroup[T, A], ProductMonoid[T, A], CommutativeMonoid[A] {}

trait CommutativeMonoidDerivation:
extension (F: CommutativeMonoid.type)
inline def derived[A](using gen: K0.ProductGeneric[A]): CommutativeMonoid[A] =
new ProductCommutativeMonoid[CommutativeMonoid, A]{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cats.derived

import cats.kernel.CommutativeSemigroup
import shapeless3.deriving.{K0, Continue}

object commutativeSemigroup extends CommutativeSemigroupDerivation

trait ProductCommutativeSemigroup[T[x] <: CommutativeSemigroup[x], A](using inst: K0.ProductInstances[T, A])
extends ProductSemigroup[T, A], CommutativeSemigroup[A] {}

trait CommutativeSemigroupDerivation:
extension (F: CommutativeSemigroup.type)
inline def derived[A](using gen: K0.ProductGeneric[A]): CommutativeSemigroup[A] =
new ProductCommutativeSemigroup[CommutativeSemigroup, A]{}
4 changes: 2 additions & 2 deletions core/src/main/scala-3.0.0-RC3/cats/derived/monoid.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import shapeless3.deriving.K0

object monoid extends MonoidDerivation

class ProductMonoid[F[x] <: Monoid[x], A](
trait ProductMonoid[F[x] <: Monoid[x], A](
using inst: K0.ProductInstances[F, A]
) extends ProductSemigroup[F, A], Monoid[A]:
val empty: A = inst.construct([A] => (F: F[A]) => F.empty)

trait MonoidDerivation:
extension (M: Monoid.type)
inline def derived[A](using gen: K0.ProductGeneric[A]): Monoid[A] =
ProductMonoid(using K0.mkProductInstances)
new ProductMonoid[Monoid, A]{}
4 changes: 2 additions & 2 deletions core/src/main/scala-3.0.0-RC3/cats/derived/semigroup.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import shapeless3.deriving.K0

object semigroup extends SemigroupDerivation

class ProductSemigroup[F[x] <: Semigroup[x], A](
trait ProductSemigroup[F[x] <: Semigroup[x], A](
using inst: K0.ProductInstances[F, A]
) extends Semigroup[A]:
def combine(x: A, y: A): A =
Expand All @@ -14,4 +14,4 @@ class ProductSemigroup[F[x] <: Semigroup[x], A](
trait SemigroupDerivation:
extension (S: Semigroup.type)
inline def derived[A](using gen: K0.ProductGeneric[A]): Semigroup[A] =
ProductSemigroup(using K0.mkProductInstances)
new ProductSemigroup[Semigroup, A]{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package cats.derived

import alleycats._
import cats._
import cats.kernel.CommutativeMonoid
import cats.derived.all._

class CommutativeMonoidTests { //
case class Foo(i: Int, b: Option[Int]) derives CommutativeMonoid
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package cats.derived

import alleycats._
import cats._
import cats.kernel.CommutativeSemigroup
import cats.derived.all._

class CommutativeSemigroupTests { //
case class Foo(i: Int, b: Option[Int]) derives CommutativeSemigroup
}

0 comments on commit 803b582

Please sign in to comment.