-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
152 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
core/src/main/scala-3/cats/tagless/macros/MacroSemigroupK.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* 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.{Semigroup, SemigroupK} | ||
|
||
import scala.annotation.experimental | ||
import scala.quoted.* | ||
|
||
@experimental | ||
object MacroSemigroupK: | ||
inline def derive[F[_]]: SemigroupK[F] = ${ semigroupK } | ||
|
||
def semigroupK[F[_]: Type](using Quotes): Expr[SemigroupK[F]] = '{ | ||
new SemigroupK[F]: | ||
def combineK[A](x: F[A], y: F[A]): F[A] = | ||
${ deriveCombineK('{ x }, '{ y }) } | ||
} | ||
|
||
private[macros] def deriveCombineK[F[_]: Type, A: Type]( | ||
x: Expr[F[A]], | ||
y: Expr[F[A]] | ||
)(using q: Quotes): Expr[F[A]] = | ||
import quotes.reflect.* | ||
given DeriveMacros[q.type] = new DeriveMacros | ||
|
||
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)) | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
tests/src/test/scala-3/cats/tagless/tests/autoSemigroupKTests.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* 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.data.Validated | ||
import cats.laws.discipline.arbitrary.* | ||
import cats.laws.discipline.eq.* | ||
import cats.laws.discipline.{SemigroupKTests, SerializableTests} | ||
import cats.tagless.derived.* | ||
import cats.{Comparison, Eq, SemigroupK} | ||
import org.scalacheck.{Arbitrary, Cogen, Gen} | ||
|
||
@experimental | ||
class autoSemigroupKTests extends CatsTaglessTestSuite: | ||
import autoSemigroupKTests.* | ||
|
||
checkAll("SemigroupK[TestAlgebra]", SemigroupKTests[TestAlgebra].semigroupK[Int]) | ||
checkAll("SemigroupK[TestAlgebra]", SerializableTests.serializable(SemigroupK[TestAlgebra])) | ||
|
||
object autoSemigroupKTests: | ||
import TestInstances.* | ||
|
||
trait TestAlgebra[T] derives SemigroupK: | ||
def abstractEffect(a: String): Validated[Int, T] | ||
def concreteEffect(a: String): Validated[Int, T] = abstractEffect(a + " concreteEffect") | ||
def abstractOther(t: T): String | ||
def concreteOther(a: String): String = a + " concreteOther" | ||
def withoutParams: Comparison | ||
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 => Validated[Int, T]] | ||
conEff <- Arbitrary.arbitrary[Option[String => Validated[Int, T]]] | ||
absOther <- Arbitrary.arbitrary[T => String] | ||
conOther <- Arbitrary.arbitrary[Option[String => String]] | ||
withoutParameters <- Gen.oneOf(Comparison.EqualTo, Comparison.GreaterThan, Comparison.LessThan) | ||
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) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters