Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port Applicative to Dotty #439

Merged
merged 6 commits into from
Feb 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions core/src/main/scala-3/cats/derived/DerivedApplicative.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cats.derived

import shapeless3.deriving.{Const, K1}
import cats.{Applicative, Monoid}

import scala.compiletime.*
import shapeless3.deriving.{Continue, K0, Labelling}

import scala.annotation.implicitNotFound
import scala.deriving.Mirror

@implicitNotFound("""Could not derive an instance of Applicative[F] where F = ${F}.
Make sure that F[_] satisfies one of the following conditions:
* it is a constant type λ[x => T] where T: Monoid
* it is a nested type λ[x => G[H[x]]] where G: Applicative and H: Applicative
* it is a generic case class where all fields have an Applicative instance

Note: using kind-projector notation - https://github.com/typelevel/kind-projector""")
type DerivedApplicative[F[_]] = Derived[Applicative[F]]
object DerivedApplicative:
type Or[F[_]] = Derived.Or[Applicative[F]]

inline def apply[F[_]]: Applicative[F] =
import DerivedApplicative.given
summonInline[DerivedApplicative[F]].instance

given [T](using T: Monoid[T]): DerivedApplicative[Const[T]] = new Applicative[Const[T]]:
def pure[A](x: A): Const[T][A] = T.empty
def ap[A, B](ff: T)(fa: T): Const[T][B] = T.combine(ff, fa)

given [F[_], G[_]](using F: Or[F], G: Or[G]): DerivedApplicative[[x] =>> F[G[x]]] =
F.unify.compose(G.unify)

given [F[_]](using inst: => K1.ProductInstances[Or, F]): DerivedApplicative[F] =
given K1.ProductInstances[Applicative, F] = inst.unify
new Product[Applicative, F] with DerivedApply.Product[Applicative, F] {}

trait Product[T[x[_]] <: Applicative[x], F[_]](using inst: K1.ProductInstances[T, F])
extends Applicative[F],
DerivedApply.Product[T, F]:
override def pure[A](x: A): F[A] = inst.construct([t[_]] => (apl: T[t]) => apl.pure[A](x))
5 changes: 5 additions & 0 deletions core/src/main/scala-3/cats/derived/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ extension (x: Monoid.type) inline def derived[A]: Monoid[A] = DerivedMonoid[A]
extension (x: CommutativeSemigroup.type) inline def derived[A]: CommutativeSemigroup[A] = DerivedCommutativeSemigroup[A]
extension (x: CommutativeMonoid.type) inline def derived[A]: CommutativeMonoid[A] = DerivedCommutativeMonoid[A]
extension (x: Show.type) inline def derived[A]: Show[A] = DerivedShow[A]
extension (x: Applicative.type) inline def derived[F[_]]: Applicative[F] = DerivedApplicative[F]
extension (x: Apply.type) inline def derived[F[_]]: Apply[F] = DerivedApply[F]
extension (x: Foldable.type) inline def derived[F[_]]: Foldable[F] = DerivedFoldable[F]
extension (x: Functor.type) inline def derived[F[_]]: Functor[F] = DerivedFunctor[F]
Expand All @@ -38,6 +39,7 @@ object semiauto
inline def monoid[A]: Monoid[A] = DerivedMonoid[A]
inline def commutativeSemigroup[A]: CommutativeSemigroup[A] = DerivedCommutativeSemigroup[A]
inline def commutativeMonoid[A]: CommutativeMonoid[A] = DerivedCommutativeMonoid[A]
inline def applicative[F[_]]: Applicative[F] = DerivedApplicative[F]
inline def apply[F[_]]: Apply[F] = DerivedApply[F]
inline def foldable[F[_]]: Foldable[F] = DerivedFoldable[F]
inline def functor[F[_]]: Functor[F] = DerivedFunctor[F]
Expand Down Expand Up @@ -71,6 +73,9 @@ object auto:
object show:
inline given [A](using NotGiven[Show[A]]): Show[A] = DerivedShow[A]

object applicative:
inline given [F[_]](using NotGiven[Applicative[F]]): Applicative[F] = DerivedApplicative[F]

object apply:
inline given [F[_]](using NotGiven[Apply[F]]): Apply[F] = DerivedApply[F]

Expand Down
73 changes: 73 additions & 0 deletions core/src/test/scala-3/cats/derived/ApplicativeSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2015 Miles Sabin
*
* 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.derived

import cats.Applicative
import cats.laws.discipline.{ApplicativeTests, SerializableTests}
import cats.laws.discipline.SemigroupalTests.Isomorphisms

class ApplicativeSuite extends KittensSuite {
import ApplicativeSuite._
import TestDefns._

def testApplicative(context: String)(using
caseClassWOption: Applicative[CaseClassWOption],
optList: Applicative[OptList],
// Requires Dotty 3.1.2
// andInt: Applicative[AndInt],
interleaved: Applicative[Interleaved],
listBox: Applicative[ListBox]
): Unit = {
checkAll(
s"$context.Applicative[CaseClassWOption]",
ApplicativeTests[CaseClassWOption].applicative[Int, String, Long]
)
checkAll(s"$context.Applicative[OptList]", ApplicativeTests[OptList].applicative[Int, String, Long])
// checkAll(s"$context.Applicative[AndInt]", ApplicativeTests[AndInt].applicative[Int, String, Long])
checkAll(s"$context.Applicative[Interleaved]", ApplicativeTests[Interleaved].applicative[Int, String, Long])
checkAll(s"$context.Applicative[ListBox]", ApplicativeTests[ListBox].applicative[Int, String, Long])
checkAll(s"$context.Applicative is Serializable", SerializableTests.serializable(Applicative[Interleaved]))
}

{
import auto.applicative.given
testApplicative("auto")
}

{
import semiInstances.given
testApplicative("semiauto")
}
}

object ApplicativeSuite {
import TestDefns._
import cats.instances.tuple._

type OptList[A] = Option[List[A]]
// type AndInt[A] = (A, Int)
type ListBox[A] = List[Box[A]]

object semiInstances {
given Applicative[Box] = semiauto.applicative
given Applicative[CaseClassWOption] = semiauto.applicative
given Applicative[OptList] = semiauto.applicative
// given Applicative[AndInt] = semiauto.applicative
given Applicative[Interleaved] = semiauto.applicative
given Applicative[ListBox] = semiauto.applicative
}
}