Skip to content

Commit

Permalink
[Scala 3] Derive MonoidK (#542)
Browse files Browse the repository at this point in the history
  • Loading branch information
joroKr21 committed May 22, 2024
1 parent 5d90802 commit be9b574
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 11 deletions.
1 change: 1 addition & 0 deletions core/src/main/scala-3/cats/tagless/Derive.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ object Derive:
inline def semigroupal[F[_]]: Semigroupal[F] = MacroSemigroupal.derive
inline def apply[F[_]]: Apply[F] = MacroApply.derive
inline def semigroupK[F[_]]: SemigroupK[F] = MacroSemigroupK.derive
inline def monoidK[F[_]]: MonoidK[F] = MacroMonoidK.derive
inline def bifunctor[F[_, _]]: Bifunctor[F] = MacroBifunctor.derive
inline def profunctor[F[_, _]]: Profunctor[F] = MacroProfunctor.derive
inline def functorK[Alg[_[_]]]: FunctorK[Alg] = MacroFunctorK.derive
Expand Down
1 change: 1 addition & 0 deletions core/src/main/scala-3/cats/tagless/derived/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ extension (x: Invariant.type) @experimental inline def derived[F[_]]: Invariant[
extension (x: Semigroupal.type) @experimental inline def derived[F[_]]: Semigroupal[F] = Derive.semigroupal
extension (x: Apply.type) @experimental inline def derived[F[_]]: Apply[F] = Derive.apply
extension (x: SemigroupK.type) @experimental inline def derived[F[_]]: SemigroupK[F] = Derive.semigroupK
extension (x: MonoidK.type) @experimental inline def derived[F[_]]: MonoidK[F] = Derive.monoidK
extension (x: Bifunctor.type) @experimental inline def derived[F[_, _]]: Bifunctor[F] = Derive.bifunctor
extension (x: Profunctor.type) @experimental inline def derived[F[_, _]]: Profunctor[F] = Derive.profunctor
50 changes: 50 additions & 0 deletions core/src/main/scala-3/cats/tagless/macros/MacroMonoidK.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2019 cats-tagless maintainers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cats.tagless.macros

import cats.tagless.*
import cats.{MonoidK, Monoid}

import scala.annotation.experimental
import scala.quoted.*

@experimental
object MacroMonoidK:
inline def derive[F[_]]: MonoidK[F] = ${ monoidK }

def monoidK[F[_]: Type](using Quotes): Expr[MonoidK[F]] = '{
new MonoidK[F]:
def empty[A]: F[A] = ${ deriveEmpty }
def combineK[A](x: F[A], y: F[A]): F[A] =
${ MacroSemigroupK.deriveCombineK('{ x }, '{ y }) }
}

private[macros] def deriveEmpty[F[_]: Type, A: Type](using q: Quotes): Expr[F[A]] =
import quotes.reflect.*
given DeriveMacros[q.type] = new DeriveMacros

val A = TypeRepr.of[A]
val a = A.typeSymbol

'{ null.asInstanceOf[F[A]] }.asTerm.transformTo[F[A]](body = {
case (tpe, _) if tpe.contains(a) =>
Select
.unique(tpe.summonLambda[MonoidK](a), "empty")
.appliedToTypes(List(A))
case (tpe, _) =>
Select.unique(TypeRepr.of[Monoid].appliedTo(tpe).summon, "empty")
})
20 changes: 9 additions & 11 deletions core/src/main/scala-3/cats/tagless/macros/MacroSemigroupK.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,12 @@ object MacroSemigroupK:
val A = TypeRepr.of[A]
val a = A.typeSymbol

List(x.asTerm, y.asTerm).combineTo[F[A]](
body = {
case (tpe, x :: y :: Nil) if tpe.contains(a) =>
Select
.unique(tpe.summonLambda[SemigroupK](a), "combineK")
.appliedToTypes(List(A))
.appliedTo(x, y)
case (tpe, x :: y :: Nil) =>
tpe.summonOpt[Semigroup].fold(x)(Select.unique(_, "combine").appliedTo(x, y))
}
)
List(x.asTerm, y.asTerm).combineTo[F[A]](body = {
case (tpe, x :: y :: Nil) if tpe.contains(a) =>
Select
.unique(tpe.summonLambda[SemigroupK](a), "combineK")
.appliedToTypes(List(A))
.appliedTo(x, y)
case (tpe, x :: y :: Nil) =>
tpe.summonOpt[Semigroup].fold(x)(Select.unique(_, "combine").appliedTo(x, y))
})
69 changes: 69 additions & 0 deletions tests/src/test/scala-3/cats/tagless/tests/autoMonoidKTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2019 cats-tagless maintainers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cats.tagless.tests

import cats.laws.discipline.eq.*
import cats.laws.discipline.{MonoidKTests, SerializableTests}
import cats.tagless.derived.*
import cats.{Eq, MonoidK}
import org.scalacheck.{Arbitrary, Cogen}

@experimental
class autoMonoidKTests extends CatsTaglessTestSuite:
import autoMonoidKTests.*

checkAll("MonoidK[TestAlgebra]", MonoidKTests[TestAlgebra].monoidK[Int])
checkAll("MonoidK[TestAlgebra]", SerializableTests.serializable(MonoidK[TestAlgebra]))

object autoMonoidKTests:
import TestInstances.*

trait TestAlgebra[T] derives MonoidK:
def abstractEffect(a: String): Map[String, T]
def concreteEffect(a: String): Map[String, T] = abstractEffect(a + " concreteEffect")
def abstractOther(t: T): String
def concreteOther(a: String): String = a + " concreteOther"
def withoutParams: Int
def headOption(ts: List[T]): Option[T]

given [T: Arbitrary: Eq]: Eq[TestAlgebra[T]] =
Eq.by: algebra =>
(
algebra.abstractEffect,
algebra.concreteEffect,
algebra.abstractOther,
algebra.concreteOther,
algebra.withoutParams,
algebra.headOption
)

given [T: Arbitrary: Cogen]: Arbitrary[TestAlgebra[T]] =
Arbitrary(for
absEff <- Arbitrary.arbitrary[String => Map[String, T]]
conEff <- Arbitrary.arbitrary[Option[String => Map[String, T]]]
absOther <- Arbitrary.arbitrary[T => String]
conOther <- Arbitrary.arbitrary[Option[String => String]]
withoutParameters <- Arbitrary.arbitrary[Int]
hOpt <- Arbitrary.arbitrary[List[T] => Option[T]]
yield new TestAlgebra[T]:
override def abstractEffect(i: String) = absEff(i)
override def concreteEffect(a: String) = conEff.getOrElse(super.concreteEffect)(a)
override def abstractOther(t: T) = absOther(t)
override def concreteOther(a: String) = conOther.getOrElse(super.concreteOther)(a)
override def withoutParams = withoutParameters
override def headOption(ts: List[T]): Option[T] = hOpt(ts)
)

0 comments on commit be9b574

Please sign in to comment.