Skip to content

Commit

Permalink
Derive MonoidK hierarchy (#342)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimWSpence authored May 20, 2021
1 parent 3d864d9 commit dd422a3
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 1 deletion.
3 changes: 3 additions & 0 deletions core/src/main/scala-3.0.0-RC3/cats/derived/all.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ object all extends
CommutativeMonoidDerivation,
CommutativeSemigroupDerivation,
EmptyDerivation,
EmptyKDerivation,
EqDerivation,
FoldableDerivation,
FunctorDerivation,
HashDerivation,
MonoidDerivation,
MonoidKDerivation,
OrderDerivation,
PartialOrderDerivation,
SemigroupDerivation,
SemigroupKDerivation,
ShowDerivation,
TraverseDerivation
19 changes: 19 additions & 0 deletions core/src/main/scala-3.0.0-RC3/cats/derived/emptyk.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cats.derived

import alleycats.{Empty, EmptyK}
import shapeless3.deriving.K1

object emptyk extends EmptyKDerivation

trait ProductEmptyK[T[x[_]] <: EmptyK[x], F[_]](
using inst: K1.ProductInstances[T, F]
) extends EmptyK[F]:
def empty[A]: F[A] = inst.construct([t[_]] => (emp: T[t]) => emp.empty[A])

trait EmptyKDerivation:
extension (E: EmptyK.type)
inline def derived[F[_]](using gen: K1.ProductGeneric[F]): EmptyK[F] =
new ProductEmptyK[EmptyK, F]{}

given [X](using X: Empty[X]): EmptyK[Const[X]] with
def empty[A]: Const[X][A] = X.empty
2 changes: 1 addition & 1 deletion core/src/main/scala-3.0.0-RC3/cats/derived/functor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ trait FunctorDerivation:
new ProductFunctor[Functor, F]{}

given [X]: Functor[Const[X]] with
def map[A, B](fa: Const[X][A])(f: A => B): Const[X][B] = fa
def map[A, B](fa: Const[X][A])(f: A => B): Const[X][B] = fa
20 changes: 20 additions & 0 deletions core/src/main/scala-3.0.0-RC3/cats/derived/monoidk.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cats.derived

import cats.{Monoid, MonoidK}
import shapeless3.deriving.K1

object monoidk extends MonoidKDerivation

trait ProductMonoidK[T[x[_]] <: MonoidK[x], F[_]](using inst: K1.ProductInstances[T, F])
extends ProductSemigroupK[T, F], MonoidK[F]:
def empty[A]: F[A] = inst.construct([t[_]] => (emp: T[t]) => emp.empty[A])

trait MonoidKDerivation:
extension (F: MonoidK.type)
inline def derived[F[_]](using gen: K1.Generic[F]): MonoidK[F] = ???

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)
21 changes: 21 additions & 0 deletions core/src/main/scala-3.0.0-RC3/cats/derived/semigroupk.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cats.derived

import cats.{Semigroup, SemigroupK}
import shapeless3.deriving.K1

object semigroupk extends SemigroupKDerivation

trait ProductSemigroupK[T[x[_]] <: SemigroupK[x], F[_]](using inst: K1.ProductInstances[T, F])
extends SemigroupK[F]:
def combineK[A](x: F[A], y: F[A]): F[A] = inst.map2[A, A, A](x,y)(
[t[_]] => (smgrpk: T[t], t0: t[A], t1: t[A]) => smgrpk.combineK(t0, t1)
)

trait SemigroupKDerivation:
extension (F: SemigroupK.type)
inline def derived[F[_]](using gen: K1.ProductGeneric[F]): SemigroupK[F] =
new ProductSemigroupK[SemigroupK, F]{}

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)
11 changes: 11 additions & 0 deletions core/src/test/scala-3.0.0-RC3/cats/derived/EmptyKTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package cats.derived

import alleycats._
import alleycats.std.all._
import cats._
import cats.derived.all._
import cats.derived.all.given

class EmptyKTests { //
case class Foo[A](i: String, l: List[A]) derives EmptyK
}
10 changes: 10 additions & 0 deletions core/src/test/scala-3.0.0-RC3/cats/derived/MonoidKTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package cats.derived

import alleycats._
import cats._
import cats.derived.all._
import cats.derived.all.given

class MonoidKTests { //
case class Foo[A](i: String, l: List[A]) derives MonoidK
}
10 changes: 10 additions & 0 deletions core/src/test/scala-3.0.0-RC3/cats/derived/SemigroupKTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package cats.derived

import alleycats._
import cats._
import cats.derived.all._
import cats.derived.all.given

class SemigroupKTests { //
case class Foo[A](i: String, l: List[A]) derives SemigroupK
}

0 comments on commit dd422a3

Please sign in to comment.