Skip to content

Commit

Permalink
Move type class instances into implicit scope
Browse files Browse the repository at this point in the history
  • Loading branch information
travisbrown committed Mar 30, 2020
1 parent 97dbfeb commit 56d94da
Show file tree
Hide file tree
Showing 35 changed files with 965 additions and 31 deletions.
36 changes: 36 additions & 0 deletions core/src/main/scala-2.12/cats/ScalaVersionSpecificInstances.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cats

import cats.data.ZipStream

private[cats] trait ScalaVersionSpecificTraverseInstances {
implicit def catsTraverseForStream: Traverse[Stream] = cats.instances.stream.catsStdInstancesForStream
}

private[cats] trait ScalaVersionSpecificShowInstances {
implicit def catsShowForStream[A: Show]: Show[Stream[A]] = cats.instances.stream.catsStdShowForStream[A]
}

private[cats] trait ScalaVersionSpecificSemigroupalInstances {
implicit def catsSemigroupalForStream: Semigroupal[Stream] = cats.instances.stream.catsStdInstancesForStream
}

private[cats] trait ScalaVersionSpecificMonoidKInstances {
implicit def catsMonoidKForStream: MonoidK[Stream] = cats.instances.stream.catsStdInstancesForStream
}

private[cats] trait ScalaVersionSpecificParallelInstances {
implicit def catsStdParallelForZipStream: Parallel.Aux[Stream, ZipStream] =
cats.instances.stream.catsStdParallelForStreamZipStream
}

private[cats] trait ScalaVersionSpecificInvariantInstances {
implicit def catsInstancesForStream: Monad[Stream] with Alternative[Stream] with CoflatMap[Stream] =
cats.instances.stream.catsStdInstancesForStream
}

private[cats] trait ScalaVersionSpecificTraverseFilterInstances {
implicit def catsTraverseFilterForStream: TraverseFilter[Stream] =
cats.instances.stream.catsStdTraverseFilterForStream
}

private[cats] trait ScalaVersionSpecificAlignInstances
81 changes: 81 additions & 0 deletions core/src/main/scala-2.13+/cats/ScalaVersionSpecificInstances.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package cats

import cats.data.{ZipLazyList, ZipStream}
import scala.collection.immutable.ArraySeq

private[cats] trait ScalaVersionSpecificTraverseInstances {
@deprecated("Use catsTraverseForLazyList", "3.0.0")
implicit def catsTraverseForStream: Traverse[Stream] = cats.instances.stream.catsStdInstancesForStream

implicit def catsTraverseForLazyList: Traverse[LazyList] = cats.instances.lazyList.catsStdInstancesForLazyList
implicit def catsTraverseForArraySeq: Traverse[ArraySeq] = cats.instances.arraySeq.catsStdInstancesForArraySeq
}

private[cats] trait ScalaVersionSpecificShowInstances {
@deprecated("Use catsShowForLazyList", "3.0.0")
implicit def catsShowForStream[A: Show]: Show[Stream[A]] = cats.instances.stream.catsStdShowForStream[A]

implicit def catsShowForLazyList[A: Show]: Show[LazyList[A]] = cats.instances.lazyList.catsStdShowForLazyList[A]
implicit def catsShowForArraySeq[A: Show]: Show[ArraySeq[A]] = cats.instances.arraySeq.catsStdShowForArraySeq[A]
}

private[cats] trait ScalaVersionSpecificSemigroupalInstances {
@deprecated("Use catsSemigroupalForLazyList", "3.0.0")
implicit def catsSemigroupalForStream: Semigroupal[Stream] = cats.instances.stream.catsStdInstancesForStream

implicit def catsSemigroupalForLazyList: Semigroupal[LazyList] = cats.instances.lazyList.catsStdInstancesForLazyList
implicit def catsSemigroupalForArraySeq: Semigroupal[ArraySeq] = cats.instances.arraySeq.catsStdInstancesForArraySeq
}

private[cats] trait ScalaVersionSpecificMonoidKInstances {
@deprecated("Use catsMonoidKForLazyList", "3.0.0")
implicit def catsMonoidKForStream: MonoidK[Stream] = cats.instances.stream.catsStdInstancesForStream

implicit def catsMonoidKForLazyList: MonoidK[LazyList] = cats.instances.lazyList.catsStdInstancesForLazyList
implicit def catsMonoidKForArraySeq: MonoidK[ArraySeq] = cats.instances.arraySeq.catsStdInstancesForArraySeq
}

private[cats] trait ScalaVersionSpecificParallelInstances {
@deprecated("Use catsParallelForLazyList", "3.0.0")
implicit def catsStdParallelForZipStream: Parallel.Aux[Stream, ZipStream] =
cats.instances.parallel.catsStdParallelForZipStream

implicit def catsStdParallelForZipLazyList: Parallel.Aux[LazyList, ZipLazyList] =
cats.instances.lazyList.catsStdParallelForLazyListZipLazyList
}

private[cats] trait ScalaVersionSpecificInvariantInstances {
@deprecated("Use catsInstancesForLazyList", "3.0.0")
implicit def catsInstancesForStream: Monad[Stream] with Alternative[Stream] with CoflatMap[Stream] =
cats.instances.stream.catsStdInstancesForStream

implicit def catsInstancesForLazyList: Monad[LazyList] with Alternative[LazyList] with CoflatMap[LazyList] =
cats.instances.lazyList.catsStdInstancesForLazyList

implicit def catsInstancesForArraySeq: Monad[ArraySeq] with Alternative[ArraySeq] with CoflatMap[ArraySeq] =
cats.instances.arraySeq.catsStdInstancesForArraySeq
}

private[cats] trait ScalaVersionSpecificTraverseFilterInstances {
@deprecated("Use catsTraverseFilterForLazyList", "3.0.0")
implicit def catsTraverseFilterForStream: TraverseFilter[Stream] =
cats.instances.stream.catsStdTraverseFilterForStream

implicit def catsTraverseFilterForLazyList: TraverseFilter[LazyList] =
cats.instances.lazyList.catsStdTraverseFilterForLazyList

implicit def catsTraverseFilterForArraySeq: TraverseFilter[ArraySeq] =
cats.instances.arraySeq.catsStdTraverseFilterForArraySeq
}

private[cats] trait ScalaVersionSpecificAlignInstances {
@deprecated("Use catsTraverseFilterForLazyList", "3.0.0")
implicit def catsAlignForStream: Align[Stream] =
cats.instances.stream.catsStdInstancesForStream

implicit def catsAlignForLazyList: Align[LazyList] =
cats.instances.lazyList.catsStdInstancesForLazyList

implicit def catsAlignForArraySeq: Align[ArraySeq] =
cats.instances.arraySeq.catsStdInstancesForArraySeq
}
11 changes: 10 additions & 1 deletion core/src/main/scala/cats/Align.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cats
import simulacrum.typeclass

import cats.data.Ior
import scala.collection.immutable.SortedMap

/**
* `Align` supports zipping together structures with different shapes,
Expand Down Expand Up @@ -100,11 +101,19 @@ import cats.data.Ior
}
}

object Align {
object Align extends ScalaVersionSpecificAlignInstances {
def semigroup[F[_], A](implicit F: Align[F], A: Semigroup[A]): Semigroup[F[A]] = new Semigroup[F[A]] {
def combine(x: F[A], y: F[A]): F[A] = Align[F].alignCombine(x, y)
}

implicit def catsAlignForList: Align[List] = cats.instances.list.catsStdInstancesForList
implicit def catsAlignForOption: Align[Option] = cats.instances.option.catsStdInstancesForOption
implicit def catsAlignForVector: Align[Vector] = cats.instances.vector.catsStdInstancesForVector
implicit def catsAlignForMap[K]: Align[Map[K, *]] = cats.instances.map.catsStdInstancesForMap[K]
implicit def catsAlignForSortedMap[K: Order]: Align[SortedMap[K, *]] =
cats.instances.sortedMap.catsStdInstancesForSortedMap[K]
implicit def catsAlignForEither[A]: Align[Either[A, *]] = cats.instances.either.catsStdInstancesForEither[A]

private[cats] def alignWithIterator[A, B, C](fa: Iterable[A], fb: Iterable[B])(f: Ior[A, B] => C): Iterator[C] =
new Iterator[C] {
private[this] val iterA = fa.iterator
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/scala/cats/Applicative.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cats

import cats.arrow.Arrow
import cats.instances.list._
import simulacrum.typeclass

/**
Expand Down Expand Up @@ -206,7 +205,7 @@ object Applicative {
* res0: (Long, Int) = (3,6)
* }}}
*/
implicit def catsApplicativeForArrow[F[_, _], A](implicit F: Arrow[F]): Applicative[F[A, *]] =
def catsApplicativeForArrow[F[_, _], A](implicit F: Arrow[F]): Applicative[F[A, *]] =
new ArrowApplicative[F, A](F)

/**
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/scala/cats/Bifoldable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ import simulacrum.typeclass
}
}

object Bifoldable {
implicit def catsBitraverseForEither: Bitraverse[Either] = cats.instances.either.catsStdBitraverseForEither
implicit def catsBitraverseForTuple2: Bitraverse[Tuple2] = cats.instances.tuple.catsStdBitraverseForTuple2
}

private[cats] trait ComposedBifoldable[F[_, _], G[_, _]] extends Bifoldable[λ[(α, β) => F[G[α, β], G[α, β]]]] {
implicit def F: Bifoldable[F]
implicit def G: Bifoldable[G]
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/scala/cats/Bifunctor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ import simulacrum.typeclass
def leftWiden[A, B, AA >: A](fab: F[A, B]): F[AA, B] = fab.asInstanceOf[F[AA, B]]
}

object Bifunctor {
implicit def catsBifunctorForEither: Bifunctor[Either] = cats.instances.either.catsStdBitraverseForEither
implicit def catsBifunctorForTuple2: Bifunctor[Tuple2] = cats.instances.tuple.catsStdBitraverseForTuple2
}

private[cats] trait ComposedBifunctor[F[_, _], G[_, _]] extends Bifunctor[λ[(A, B) => F[G[A, B], G[A, B]]]] {
def F: Bifunctor[F]
def G: Bifunctor[G]
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/scala/cats/Defer.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cats

import scala.util.control.TailCalls.TailRec

/**
* Defer is a type class that shows the ability to defer creation
* inside of the type constructor F[_].
Expand Down Expand Up @@ -53,4 +55,8 @@ trait Defer[F[_]] extends Serializable {

object Defer {
def apply[F[_]](implicit defer: Defer[F]): Defer[F] = defer

implicit def catsDeferForFunction0: Defer[Function0] = cats.instances.function.catsSddDeferForFunction0
implicit def catsDeferForFunction1[A]: Defer[Function1[A, *]] = cats.instances.function.catsStdDeferForFunction1[A]
implicit def catsDeferForTailRec: Defer[TailRec] = cats.instances.tailRec.catsInstancesForTailRec
}
3 changes: 0 additions & 3 deletions core/src/main/scala/cats/Foldable.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cats

import scala.collection.mutable
import cats.instances.either._
import cats.kernel.CommutativeMonoid
import simulacrum.{noop, typeclass}
import Foldable.sentinel
Expand Down Expand Up @@ -693,8 +692,6 @@ import Foldable.sentinel
* }}}
*/
def partitionEither[A, B, C](fa: F[A])(f: A => Either[B, C])(implicit A: Alternative[F]): (F[B], F[C]) = {
import cats.instances.tuple._

implicit val mb: Monoid[F[B]] = A.algebra[B]
implicit val mc: Monoid[F[C]] = A.algebra[C]

Expand Down
15 changes: 15 additions & 0 deletions core/src/main/scala/cats/FunctorFilter.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cats

import scala.collection.immutable.{Queue, SortedMap}
import simulacrum.typeclass

/**
Expand Down Expand Up @@ -74,3 +75,17 @@ trait FunctorFilter[F[_]] extends Serializable {
def filterNot[A](fa: F[A])(f: A => Boolean): F[A] =
mapFilter(fa)(Some(_).filterNot(f))
}

object FunctorFilter extends ScalaVersionSpecificTraverseFilterInstances {
implicit def catsTraverseFilterForOption: TraverseFilter[Option] =
cats.instances.option.catsStdTraverseFilterForOption
implicit def catsTraverseFilterForList: TraverseFilter[List] = cats.instances.list.catsStdTraverseFilterForList
implicit def catsTraverseFilterForVector: TraverseFilter[Vector] =
cats.instances.vector.catsStdTraverseFilterForVector
implicit def catsFunctorFilterForMap[K: Order]: FunctorFilter[Map[K, *]] =
cats.instances.map.catsStdFunctorFilterForMap[K]
implicit def catsTraverseFilterForSortedMap[K: Order]: TraverseFilter[SortedMap[K, *]] =
cats.instances.sortedMap.catsStdTraverseFilterForSortedMap[K]
implicit def catsTraverseFilterForQueue: TraverseFilter[Queue] =
cats.instances.queue.catsStdTraverseFilterForQueue
}
81 changes: 80 additions & 1 deletion core/src/main/scala/cats/Invariant.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package cats

import cats.arrow.Arrow
import cats.kernel._
import simulacrum.typeclass
import cats.kernel.compat.scalaVersionSpecific._
import scala.collection.immutable.{Queue, SortedMap}
import scala.util.Try
import scala.util.control.TailCalls.TailRec

/**
* Must obey the laws defined in cats.laws.InvariantLaws.
Expand Down Expand Up @@ -45,7 +49,55 @@ import cats.kernel.compat.scalaVersionSpecific._
}

@suppressUnusedImportWarningForScalaVersionSpecific
object Invariant {
object Invariant extends ScalaVersionSpecificInvariantInstances with InvariantInstances0 {
implicit def catsInstancesForId: Distributive[Id] with Comonad[Id] = cats.catsInstancesForId
implicit def catsComonadForTuple2[A]: Comonad[(A, *)] = cats.instances.tuple.catsStdInstancesForTuple2[A]
implicit def catsMonadErrorForEither[A]: MonadError[Either[A, *], A] =
cats.instances.either.catsStdInstancesForEither[A]
implicit def catsInstancesForOption
: MonadError[Option, Unit] with Alternative[Option] with CoflatMap[Option] with CommutativeMonad[Option] =
cats.instances.option.catsStdInstancesForOption
implicit def catsInstancesForList: Monad[List] with Alternative[List] with CoflatMap[List] =
cats.instances.list.catsStdInstancesForList
implicit def catsInstancesForVector: Monad[Vector] with Alternative[Vector] with CoflatMap[Vector] =
cats.instances.vector.catsStdInstancesForVector
implicit def catsInstancesForQueue: Monad[Queue] with Alternative[Queue] with CoflatMap[Queue] =
cats.instances.queue.catsStdInstancesForQueue
implicit def catsMonadForTailRec: Monad[TailRec] = cats.instances.tailRec.catsInstancesForTailRec

implicit def catsFlatMapForMap[K]: FlatMap[Map[K, *]] = cats.instances.map.catsStdInstancesForMap[K]
implicit def catsFlatMapForSortedMap[K: Order]: FlatMap[SortedMap[K, *]] =
cats.instances.sortedMap.catsStdInstancesForSortedMap[K]
implicit def catsBimonadForFunction0[I]: Bimonad[Function0] = cats.instances.function.catsStdBimonadForFunction0
implicit def catsMonadForFunction1[I]: Monad[I => *] = cats.instances.function.catsStdMonadForFunction1[I]
implicit def catsContravariantMonoidalForFunction1[R: Monoid]: ContravariantMonoidal[* => R] =
cats.instances.function.catsStdContravariantMonoidalForFunction1[R]
implicit def catsFunctorForPair: Functor[λ[P => (P, P)]] = cats.instances.tuple.catsDataFunctorForPair

implicit def catsInstancesForTry: MonadError[Try, Throwable] with CoflatMap[Try] =
cats.instances.try_.catsStdInstancesForTry

implicit def catsContravariantMonoidalForOrder: ContravariantMonoidal[Order] =
cats.instances.order.catsContravariantMonoidalForOrder
implicit def catsContravariantMonoidalForPartialOrder: ContravariantMonoidal[PartialOrder] =
cats.instances.partialOrder.catsContravariantMonoidalForPartialOrder
implicit def catsContravariantMonoidalForOrdering: ContravariantMonoidal[Ordering] =
cats.instances.ordering.catsContravariantMonoidalForOrdering
implicit def catsContravariantMonoidalForPartialOrdering: ContravariantMonoidal[PartialOrdering] =
cats.instances.partialOrdering.catsContravariantMonoidalForPartialOrdering
implicit def catsContravariantMonoidalForEq: ContravariantMonoidal[Eq] =
cats.instances.eq.catsContravariantMonoidalForEq
implicit def catsContravariantMonoidalForEquiv: ContravariantMonoidal[Equiv] =
cats.instances.equiv.catsContravariantMonoidalForEquiv
implicit def catsContravariantForHash: Contravariant[Hash] =
cats.instances.all.catsContravariantForHash
implicit def catsInvariantMonoidalForSemigroup: InvariantMonoidal[Semigroup] =
cats.instances.invariant.catsInvariantMonoidalSemigroup
implicit def catsInvariantMonoidalForCommutativeSemigroup: InvariantMonoidal[CommutativeSemigroup] =
cats.instances.invariant.catsInvariantMonoidalCommutativeSemigroup
implicit def catsInvariantSemigroupalForMonoid: InvariantSemigroupal[Monoid] =
cats.instances.invariant.catsSemigroupalForMonoid

implicit val catsInvariantMonoid: Invariant[Monoid] = new Invariant[Monoid] {

def imap[A, B](fa: Monoid[A])(f: A => B)(g: B => A): Monoid[B] = new Monoid[B] {
Expand Down Expand Up @@ -122,3 +174,30 @@ object Invariant {

}
}

private[cats] trait InvariantInstances0 extends TupleInstances0 {
implicit def catsCommutativeMonadForTuple2[X](implicit X: CommutativeMonoid[X]): CommutativeMonad[(X, *)] =
cats.instances.tuple.catsStdCommutativeMonadForTuple2[X]
implicit def catsContravariantForFunction1[R]: Contravariant[* => R] =
cats.instances.function.catsStdContravariantForFunction1[R]
implicit def catsDistributiveForFunction0: Distributive[Function0] = cats.instances.function.function0Distributive
implicit def catsDistributiveForFunction1[I]: Distributive[I => *] =
cats.instances.function.catsStdDistributiveForFunction1[I]
implicit def catsApplicativeForArrow[F[_, _], A](implicit F: Arrow[F]): Applicative[F[A, *]] =
new ArrowApplicative[F, A](F)
}

private trait TupleInstances0 extends TupleInstances1 {
implicit def catsCommutativeFlatMapForTuple2[X](implicit X: CommutativeSemigroup[X]): CommutativeFlatMap[(X, *)] =
cats.instances.tuple.catsStdCommutativeFlatMapForTuple2[X]
}

private trait TupleInstances1 extends TupleInstances2 {
implicit def catsMonadForTuple2[X](implicit X: Monoid[X]): Monad[(X, *)] =
cats.instances.tuple.catsStdMonadForTuple2[X]
}

private trait TupleInstances2 {
implicit def catsFlatMapForTuple2[X](implicit X: Semigroup[X]): FlatMap[(X, *)] =
cats.instances.tuple.catsStdFlatMapForTuple2[X]
}
12 changes: 11 additions & 1 deletion core/src/main/scala/cats/Parallel.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cats

import cats.arrow.FunctionK
import cats.data.{Validated, ZipList, ZipVector}

/**
* Some types that form a FlatMap, are also capable of forming an Apply that supports parallel composition.
Expand Down Expand Up @@ -106,11 +107,20 @@ trait Parallel[M[_]] extends NonEmptyParallel[M] {
}
}

object NonEmptyParallel {
object NonEmptyParallel extends ScalaVersionSpecificParallelInstances {
type Aux[M[_], F0[_]] = NonEmptyParallel[M] { type F[x] = F0[x] }

def apply[M[_], F[_]](implicit P: NonEmptyParallel.Aux[M, F]): NonEmptyParallel.Aux[M, F] = P
def apply[M[_]](implicit P: NonEmptyParallel[M], D: DummyImplicit): NonEmptyParallel.Aux[M, P.F] = P

implicit def catsParallelForEitherValidated[E: Semigroup]: Parallel.Aux[Either[E, *], Validated[E, *]] =
cats.instances.either.catsParallelForEitherAndValidated[E]

implicit def catsStdNonEmptyParallelForZipList: NonEmptyParallel.Aux[List, ZipList] =
cats.instances.list.catsStdNonEmptyParallelForListZipList

implicit def catsStdNonEmptyParallelForZipVector: NonEmptyParallel.Aux[Vector, ZipVector] =
cats.instances.vector.catsStdNonEmptyParallelForVectorZipVector
}

object Parallel extends ParallelArityFunctions2 {
Expand Down
7 changes: 7 additions & 0 deletions core/src/main/scala/cats/Representable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,11 @@ object Representable {
new RepresentableDistributive[F, Rep.Representation] {
override def R: Aux[F, Rep.Representation] = Rep
}

implicit def catsRepresentableForFunction1[E](implicit EF: Functor[E => *]): Representable.Aux[E => *, E] =
cats.instances.function.catsStdRepresentableForFunction1[E]

implicit def catsRepresentableForPair(
implicit PF: Functor[λ[P => (P, P)]]
): Representable.Aux[λ[P => (P, P)], Boolean] = cats.instances.tuple.catsDataRepresentableForPair
}
Loading

0 comments on commit 56d94da

Please sign in to comment.