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 Order to Dotty #440

Merged
merged 3 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
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ val catsVersion = "2.7.0"
val disciplineMunitVersion = "1.0.9"
val kindProjectorVersion = "0.13.2"
val shapeless2Version = "2.3.7"
val shapeless3Version = "3.0.3"
val shapeless3Version = "3.0.4"

lazy val commonSettings = Seq(
scalacOptions := Seq(
Expand Down
43 changes: 43 additions & 0 deletions core/src/main/scala-3/cats/derived/DerivedOrder.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package cats.derived

import cats.{Order, Show}

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

import scala.annotation.targetName
import scala.deriving.Mirror

type DerivedOrder[A] = Derived[Order[A]]
object DerivedOrder:
type Or[A] = Derived.Or[Order[A]]

inline def apply[A]: Order[A] =
import DerivedOrder.given
summonInline[DerivedOrder[A]].instance

given product[A](using inst: => K0.ProductInstances[Or, A]): DerivedOrder[A] =
given K0.ProductInstances[Order, A] = inst.unify
new Product[Order, A] {}

given coproduct[A](using inst: => K0.CoproductInstances[Or, A]): DerivedOrder[A] =
given K0.CoproductInstances[Order, A] = inst.unify
new Coproduct[Order, A] {}

trait Product[T[x] <: Order[x], A](using inst: K0.ProductInstances[T, A]) extends Order[A]:

def compare(x: A, y: A): Int =
inst.foldLeft2(x, y)(0: Int)(
[t] =>
(acc: Int, ord: T[t], t0: t, t1: t) => {
val cmp = ord.compare(t0, t1)
Complete(cmp != 0)(cmp)(acc)
}
)

trait Coproduct[T[x] <: Order[x], A](using inst: K0.CoproductInstances[T, A]) extends Order[A]:

def compare(x: A, y: A): Int =
inst.fold2(x, y)((x: Int, y: Int) => x - y)(
[t] => (ord: T[t], t0: t, t1: t) => ord.compare(t0, t1)
)
34 changes: 0 additions & 34 deletions core/src/main/scala-3/cats/derived/order.scala

This file was deleted.

6 changes: 5 additions & 1 deletion core/src/main/scala-3/cats/derived/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ extension (x: Hash.type) inline def derived[A]: Hash[A] = DerivedHash[A]
extension (x: Empty.type) inline def derived[A]: Empty[A] = DerivedEmpty[A]
extension (x: Semigroup.type) inline def derived[A]: Semigroup[A] = DerivedSemigroup[A]
extension (x: Monoid.type) inline def derived[A]: Monoid[A] = DerivedMonoid[A]
extension (x: Order.type) inline def derived[A]: Order[A] = DerivedOrder[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]
Expand All @@ -26,7 +27,6 @@ object semiauto
EmptyKDerivation,
InvariantDerivation,
MonoidKDerivation,
OrderDerivation,
PartialOrderDerivation,
SemigroupKDerivation,
Instances:
Expand All @@ -36,6 +36,7 @@ object semiauto
inline def empty[A]: Empty[A] = DerivedEmpty[A]
inline def semigroup[A]: Semigroup[A] = DerivedSemigroup[A]
inline def monoid[A]: Monoid[A] = DerivedMonoid[A]
inline def order[A]: Order[A] = DerivedOrder[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]
Expand All @@ -62,6 +63,9 @@ object auto:
object monoid:
inline given [A](using NotGiven[Monoid[A]]): Monoid[A] = DerivedMonoid[A]

object order:
inline given [A](using NotGiven[Order[A]]): Order[A] = DerivedOrder[A]

object commutativeSemigroup:
inline given [A](using NotGiven[CommutativeSemigroup[A]]): CommutativeSemigroup[A] = DerivedCommutativeSemigroup[A]

Expand Down
61 changes: 61 additions & 0 deletions core/src/test/scala-3/cats/derived/OrderSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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.Order
import cats.kernel.laws.discipline.{OrderTests, SerializableTests}
import org.scalacheck.Arbitrary
import scala.compiletime.summonInline

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

inline def orderTests[A]: OrderTests[A] =
OrderTests[A](summonInline)

inline def testOrder(context: String): Unit = {
checkAll(s"$context.Order[Inner]", orderTests[Inner].order)
checkAll(s"$context.Order[Outer]", orderTests[Outer].order)
checkAll(s"$context.Order[Interleaved[Int]]", orderTests[Interleaved[Int]].order)
checkAll(s"$context.Order[Recursive]", orderTests[Recursive].order)
checkAll(s"$context.Order[GenericAdt[Int]]", orderTests[GenericAdt[Int]].order)
checkAll(s"$context.Order is Serializable", SerializableTests.serializable(summonInline[Order[Interleaved[Int]]]))
}

{
import auto.order.given
testOrder("auto")
}

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

object OrderSuite {
import TestDefns._

object semiInstances {
given Order[Inner] = semiauto.order
given Order[Outer] = semiauto.order
given Order[Interleaved[Int]] = semiauto.order
given Order[Recursive] = semiauto.order
given Order[GenericAdt[Int]] = semiauto.order
}
}
2 changes: 1 addition & 1 deletion core/src/test/scala-3/cats/derived/OrderTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cats.derived

import alleycats.*
import cats.*
import cats.derived.semiauto.*
import cats.derived.*

class OrderTests { //
case class Foo(i: Int, b: Option[String]) derives Order
Expand Down