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 Apply to Dotty #441

Merged
merged 3 commits into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
42 changes: 42 additions & 0 deletions core/src/main/scala-3/cats/derived/DerivedApply.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cats.derived

import shapeless3.deriving.{Const, K1}
import cats.{Apply, Semigroup}

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

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

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

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

inline def apply[F[_]]: Apply[F] =
import DerivedApply.given
summonInline[DerivedApply[F]].instance

given [T](using T: Semigroup[T]): DerivedApply[Const[T]] = new Apply[Const[T]]:
def ap[A, B](ff: T)(fa: T) = T.combine(ff, fa)
def map[A, B](fa: T)(f: A => B) = fa

given [F[_], G[_]](using F: Apply[F], G: Apply[G]): DerivedApply[[x] =>> F[G[x]]] =
F.compose(G)
andrzejressel marked this conversation as resolved.
Show resolved Hide resolved

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

trait Product[T[x[_]] <: Apply[x], F[_]](using inst: K1.ProductInstances[T, F]) extends Apply[F]:
override def ap[A, B](ff: F[A => B])(fa: F[A]): F[B] =
inst.map2(ff, fa)([t[_]] => (apl: T[t], tt: t[A => B], ta: t[A]) => apl.ap(tt)(ta))
override def map[A, B](fa: F[A])(f: A => B): F[B] =
inst.map(fa: F[A])([f[_]] => (tf: T[f], fa: f[A]) => tf.map(fa)(f))
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: 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]
extension (x: Reducible.type) inline def derived[F[_]]: Reducible[F] = DerivedReducible[F]
Expand All @@ -37,6 +38,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 apply[F[_]]: Apply[F] = DerivedApply[F]
inline def foldable[F[_]]: Foldable[F] = DerivedFoldable[F]
inline def functor[F[_]]: Functor[F] = DerivedFunctor[F]
inline def reducible[F[_]]: Reducible[F] = DerivedReducible[F]
Expand Down Expand Up @@ -69,6 +71,9 @@ object auto:
object show:
inline given [A](using NotGiven[Show[A]]): Show[A] = DerivedShow[A]

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

object functor:
inline given [F[_]](using NotGiven[Functor[F]]): Functor[F] = DerivedFunctor[F]

Expand Down
73 changes: 73 additions & 0 deletions core/src/test/scala-3/cats/derived/ApplySuite.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.Apply
import cats.laws.discipline.{ApplyTests, SerializableTests}
import cats.laws.discipline.SemigroupalTests.Isomorphisms

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

def testApply(context: String)(using
caseClassWOption: Apply[CaseClassWOption],
optList: Apply[OptList],
// Requires scala 3.1.2
// andInt: Apply[AndInt],
interleaved: Apply[Interleaved],
listBox: Apply[ListBox]
): Unit = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should make this function inline to get rid the using clauses - see FunctorSuite for an example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason it required declaration of Isomorphisms for every class which is kinda weird, but it works fine now

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ok if doesn't work - forget it 😄
It's just a test anyway.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wait - so it did work 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, no - it works fine. But I feel there is something wrong with how it works - I guess I'll have to do more digging with newer Scalas. I for some weird reason have to declare new implicits when there was not need to before.

given isoOptList: Isomorphisms[OptList] = Isomorphisms.invariant(optList)
// given isoAndInt: Isomorphisms[AndInt] = Isomorphisms.invariant(andInt)
given isoListBox: Isomorphisms[ListBox] = Isomorphisms.invariant(listBox)

checkAll(s"$context.Apply[CaseClassWOption]", ApplyTests[CaseClassWOption].apply[Int, String, Long])
checkAll(s"$context.Apply[OptList]", ApplyTests[OptList].apply[Int, String, Long])
// checkAll(s"$context.Apply[AndInt]", ApplyTests[AndInt].apply[Int, String, Long])
checkAll(s"$context.Apply[Interleaved]", ApplyTests[Interleaved].apply[Int, String, Long])
checkAll(s"$context.Apply[ListBox]", ApplyTests[ListBox].apply[Int, String, Long])
checkAll(s"$context.Apply is Serializable", SerializableTests.serializable(Apply[Interleaved]))
}

{
import auto.apply.given
testApply("auto")
}

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

object ApplySuite {
import TestDefns._

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

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