-
Notifications
You must be signed in to change notification settings - Fork 64
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 contravariant to new scheme #475
Merged
joroKr21
merged 15 commits into
typelevel:dotty
from
TimWSpence:port-contravariant-to-new-scheme
May 25, 2022
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
6cca398
Port SemigroupK and MonoidK to new derivation scheme
TimWSpence 415df3c
WIP port of SemigroupK suite to scala 3
TimWSpence 7f74610
Nested derivations for SemigroupK
TimWSpence fe2b8b4
Priority for derived SemigroupK given instances
TimWSpence 84b0664
Port scala 2 MonoidK tests to scala 3
TimWSpence a1e9750
Various improvements
TimWSpence 819ab4f
Use inline in tests
TimWSpence fc2fced
SemigroupK/MonoidK test for derives syntax
TimWSpence d241800
Port Contravariant to new derivation scheme
TimWSpence b4aa026
WIP port scala 2 Contravariant tests to scala 3
TimWSpence df8aacf
Merge remote-tracking branch 'upstream/dotty' into port-contravariant…
TimWSpence 813704b
Link commented out tests to issues
TimWSpence d928cd5
derives syntax tests for Contravariant
TimWSpence 73d8dfa
derives syntax tests for Contravariant
TimWSpence fd5423b
Scala 3 import syntax
TimWSpence File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
core/src/main/scala-3/cats/derived/DerivedContravariant.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,35 @@ | ||
package cats.derived | ||
|
||
import cats.{Contravariant, Functor} | ||
import shapeless3.deriving.{Const, K1} | ||
|
||
import scala.annotation.implicitNotFound | ||
import scala.compiletime.* | ||
|
||
@implicitNotFound("""Could not derive an instance of Contravariant[F] where F = ${F}. | ||
Make sure that F[_] satisfies one of the following conditions: | ||
* it is a constant type [x] =>> T | ||
* it is a nested type [x] =>> G[H[x]] where G: Functor and H: Contravariant | ||
* it is a generic case class where all fields have a Contravariant instance | ||
* it is a generic sealed trait where all subclasses have a Contravariant instance""") | ||
type DerivedContravariant[F[_]] = Derived[Contravariant[F]] | ||
object DerivedContravariant: | ||
type Or[F[_]] = Derived.Or[Contravariant[F]] | ||
inline def apply[F[_]]: Contravariant[F] = | ||
import DerivedContravariant.given | ||
summonInline[DerivedContravariant[F]].instance | ||
|
||
given [T]: DerivedContravariant[Const[T]] = new Contravariant[Const[T]]: | ||
def contramap[A, B](fa: T)(f: B => A): T = fa | ||
|
||
given [F[_], G[_]](using F: DerivedFunctor.Or[F], G: Or[G]): DerivedContravariant[[x] =>> F[G[x]]] = | ||
given Contravariant[G] = G.unify | ||
F.unify.composeContravariant[G] | ||
|
||
given [F[_]](using inst: => K1.Instances[Or, F]): DerivedContravariant[F] = | ||
given K1.Instances[Contravariant, F] = inst.unify | ||
new Generic[Contravariant, F] {} | ||
|
||
trait Generic[T[x[_]] <: Contravariant[x], F[_]](using inst: K1.Instances[T, F]) extends Contravariant[F]: | ||
final override def contramap[A, B](fa: F[A])(f: B => A): F[B] = | ||
inst.map(fa: F[A])([f[_]] => (tf: T[f], fa: f[A]) => tf.contramap(fa)(f)) |
This file was deleted.
Oops, something went wrong.
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
109 changes: 109 additions & 0 deletions
109
core/src/test/scala-3/cats/derived/ContravariantSuite.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,109 @@ | ||
/* | ||
* 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 | ||
package derived | ||
|
||
import cats.laws.discipline.* | ||
import cats.laws.discipline.arbitrary.* | ||
import cats.laws.discipline.eq.* | ||
import scala.compiletime.* | ||
|
||
class ContravariantSuite extends KittensSuite: | ||
import ContravariantSuite.* | ||
import TestDefns.* | ||
|
||
inline def contravariantTests[F[_]]: ContravariantTests[F] = ContravariantTests[F](summonInline) | ||
|
||
inline def testContravariant(context: String): Unit = | ||
checkAll(s"$context.Contravariant[OptPred]", contravariantTests[OptPred].contravariant[MiniInt, String, Boolean]) | ||
checkAll(s"$context.Contravariant[TreePred]", contravariantTests[TreePred].contravariant[MiniInt, String, Boolean]) | ||
checkAll(s"$context.Contravariant[ListPred]", contravariantTests[ListPred].contravariant[MiniInt, String, Boolean]) | ||
checkAll( | ||
s"$context.Contravariant[GenericAdtPred]", | ||
contravariantTests[GenericAdtPred].contravariant[MiniInt, String, Boolean] | ||
) | ||
// TODO https://github.com/typelevel/kittens/issues/473 | ||
// checkAll( | ||
// s"$context.Contravariant[InterleavedPred]", | ||
// ContravariantTests[InterleavedPred].contravariant[MiniInt, String, Boolean] | ||
// ) | ||
checkAll( | ||
s"$context.Contravariant[AndCharPred]", | ||
contravariantTests[AndCharPred].contravariant[MiniInt, String, Boolean] | ||
) | ||
checkAll( | ||
s"$context.Contravariant[ListSnocF]", | ||
contravariantTests[ListSnocF].contravariant[MiniInt, String, Boolean] | ||
) | ||
checkAll( | ||
s"$context.Contravariant is Serializable", | ||
SerializableTests.serializable(summonInline[Contravariant[TreePred]]) | ||
) | ||
|
||
// TODO https://github.com/typelevel/kittens/issues/476 | ||
// test(s"$context.Contravariant.contramap is stack safe") { | ||
// val C = summonInline[Contravariant[ListSnocF]] | ||
// val n = 10000 | ||
// val largeBoxed = Snoc.fromSeq((1 until n).map((j: Int) => (i: Int) => i + j)) :: Nil | ||
// val actualBoxed = C.contramap[Int, Int](largeBoxed)((j: Int) => j + 1).flatMap(Snoc.toList) | ||
// val expected = (3 until n + 2).toList | ||
// assert(actualBoxed.map(_.apply(1)) == expected) | ||
// } | ||
|
||
locally { | ||
import auto.contravariant.given | ||
testContravariant("auto") | ||
} | ||
|
||
locally { | ||
import semiInstances.given | ||
testContravariant("semiauto") | ||
} | ||
|
||
object ContravariantSuite: | ||
import TestDefns.* | ||
|
||
type OptPred[A] = Option[A => Boolean] | ||
type ListPred[A] = List[A => Boolean] | ||
type GenericAdtPred[A] = GenericAdt[A => Boolean] | ||
type ListSnocF[A] = List[Snoc[A => Int]] | ||
type InterleavedPred[A] = Interleaved[A => Boolean] | ||
type AndCharPred[A] = (A => Boolean, Char) | ||
type TreePred[A] = Tree[A => Boolean] | ||
|
||
object semiInstances: | ||
implicit val optPred: Contravariant[OptPred] = semiauto.contravariant | ||
implicit val treePred: Contravariant[TreePred] = semiauto.contravariant | ||
implicit val listPred: Contravariant[ListPred] = semiauto.contravariant | ||
implicit val genericAdtPred: Contravariant[GenericAdtPred] = semiauto.contravariant | ||
// implicit val interleavePred: Contravariant[InterleavedPred] = semiauto.contravariant | ||
implicit val andCharPred: Contravariant[AndCharPred] = semiauto.contravariant | ||
implicit val listSnocF: Contravariant[ListSnocF] = semiauto.contravariant | ||
|
||
case class Single[A](value: A => Unit) derives Contravariant | ||
|
||
enum Many[-A] derives Contravariant: | ||
case Naught | ||
case More(value: A => Unit, rest: Many[A]) | ||
|
||
enum AtMostOne[-A] derives Contravariant: | ||
case Naught | ||
case Single(value: A => Unit) | ||
|
||
enum AtLeastOne[-A] derives Contravariant: | ||
case Single(value: A => Unit) | ||
case More(value: A => Unit, rest: Option[AtLeastOne[A]]) |
10 changes: 0 additions & 10 deletions
10
core/src/test/scala-3/cats/derived/ContravariantTests.scala
This file was deleted.
Oops, something went wrong.
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we can now support the other composition as well which is
F: Contravariant
andG: Functor
- I think it was ambiguous in Scala 2.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I was just doing
Invariant
and realized there are missing cases. I assume so 👍There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Literally came here to check if that instance existed and found your comment 😂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just tried it and it doesn't seem to resolve properly. Given that
Invariant
needs the same treatment, I'm wondering about making a separate issue for it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean that
Invariant
needs the same treatment?About making a separate issue - feel free 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, there are two more instances we could define for
Invariant
. Opened #479 to track it