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

WIP: Selective #3709

Draft
wants to merge 47 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
444e3bd
Introduce Selective
rossabaker Dec 9, 2020
da7f756
Split into Select and Selective
rossabaker Dec 9, 2020
d220349
Selective[Validated[E, *]]
rossabaker Dec 9, 2020
4c7f87e
SelectiveError
rossabaker Dec 9, 2020
14d3e42
FlatMap extends Select
rossabaker Dec 9, 2020
f554862
Merge branch 'master' into selective
rossabaker Dec 9, 2020
b7112ac
Required parens on lambda
rossabaker Dec 9, 2020
b8dc63b
Default select from Apply until Monad
rossabaker Dec 9, 2020
7fd409b
Start writing SelectiveLaws
rossabaker Dec 9, 2020
254bf7f
Look at me, not compiling before I push
rossabaker Dec 9, 2020
ed71f06
Never mind on Select
rossabaker Dec 9, 2020
7767906
branch, ifS, whenS
rossabaker Dec 9, 2020
466354b
select is an abstract member of Selective
rossabaker Dec 10, 2020
f31020a
Law for monad select rigidity
rossabaker Dec 10, 2020
4a2b0be
scalafmt
rossabaker Dec 10, 2020
955a03b
Use our new syntax
rossabaker Dec 10, 2020
e35a097
Test default operations
rossabaker Dec 10, 2020
df699d1
Derive missing implicits for selective laws
rossabaker Dec 14, 2020
a50da48
Remove SelectiveError for now
rossabaker Dec 14, 2020
387ab75
Selective distributivity and associativity
rossabaker Dec 14, 2020
cb51502
Clean up syntax
rossabaker Dec 14, 2020
06abe62
apS and RigidSelective laws
rossabaker Dec 16, 2020
9b001dd
Remove inner scope in branch
rossabaker Dec 16, 2020
19b67b4
Move EitherUtil to root package for use in typeclasses
rossabaker Dec 16, 2020
662cf5a
Use cached either units in ifS
rossabaker Dec 16, 2020
936847f
Fix loop in laws inheritance
rossabaker Dec 16, 2020
3372948
Remove unnecessary private[cats] in EitherUtil
rossabaker Dec 16, 2020
a866a20
Apply.selectA
rossabaker Dec 16, 2020
260a219
select's function is lazy; skip on right law for rigids
rossabaker Dec 16, 2020
a0f8709
Skip effects in branch and ifS in rigid selectives
rossabaker Dec 16, 2020
37594b9
Skip effect of whenS when false in rigid selectives
rossabaker Dec 16, 2020
b56130d
Replace apS with RigidSelective typeclass
rossabaker Dec 16, 2020
9079e16
Validated is rigid
rossabaker Dec 17, 2020
020a9fb
Revert "Validated is rigid" -- it's not rigid yet
rossabaker Dec 19, 2020
92f262f
Push select down to Apply
rossabaker Dec 19, 2020
762dd7d
Push branch and ifS down to Apply
rossabaker Dec 19, 2020
691144e
Push whenS down to Applicative
rossabaker Dec 19, 2020
3e31609
Push identity and distributivity laws to Applicative
rossabaker Dec 19, 2020
6b496ec
Remove Selective
rossabaker Dec 19, 2020
acce38c
Remove commented implicits
rossabaker Dec 20, 2020
7c78d54
RigidSelective default ap causes loops
rossabaker Dec 20, 2020
c4c1442
Add back lost selective associativity test
rossabaker Dec 20, 2020
3bcb791
Restate ifS skip laws to accommodate Under
rossabaker Dec 20, 2020
dba3dc0
Selective has re-entered the chat
rossabaker Dec 21, 2020
7a4d66a
Selective[ZipLazyList]
rossabaker Dec 21, 2020
6616707
Selective[Func] and RigidSelective[Func]
rossabaker Dec 21, 2020
bd0d4d9
Fix name of selective laws
rossabaker Dec 21, 2020
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
11 changes: 9 additions & 2 deletions core/src/main/scala-2.13+/cats/data/ZipLazyList.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ object ZipLazyList {

def apply[A](value: LazyList[A]): ZipLazyList[A] = new ZipLazyList(value)

implicit val catsDataAlternativeForZipLazyList: Alternative[ZipLazyList] with CommutativeApplicative[ZipLazyList] =
new Alternative[ZipLazyList] with CommutativeApplicative[ZipLazyList] {
implicit val catsDataAlternativeForZipLazyList
: Alternative[ZipLazyList] with CommutativeApplicative[ZipLazyList] with Selective[ZipLazyList] =
new Alternative[ZipLazyList] with CommutativeApplicative[ZipLazyList] with Selective[ZipLazyList] {
def pure[A](x: A): ZipLazyList[A] = new ZipLazyList(LazyList.continually(x))

override def map[A, B](fa: ZipLazyList[A])(f: (A) => B): ZipLazyList[B] =
Expand All @@ -20,6 +21,12 @@ object ZipLazyList {
override def product[A, B](fa: ZipLazyList[A], fb: ZipLazyList[B]): ZipLazyList[(A, B)] =
ZipLazyList(fa.value.zip(fb.value))

def select[A, B](fab: ZipLazyList[Either[A, B]])(ff: => ZipLazyList[A => B]): ZipLazyList[B] =
ZipLazyList(fab.value.lazyZip(ff.value).map {
case (Left(a), f) => f(a)
case (Right(b), _) => b
})

def empty[A]: ZipLazyList[A] = ZipLazyList(LazyList.empty[A])

def combineK[A](x: ZipLazyList[A], y: ZipLazyList[A]): ZipLazyList[A] =
Expand Down
23 changes: 10 additions & 13 deletions core/src/main/scala/cats/Applicative.scala
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ import scala.annotation.implicitNotFound
*/
def whenA[A](cond: Boolean)(f: => F[A]): F[Unit] =
if (cond) void(f) else unit

}

object Applicative {
Expand Down Expand Up @@ -243,12 +242,11 @@ object Applicative {
object ops {
implicit def toAllApplicativeOps[F[_], A](target: F[A])(implicit tc: Applicative[F]): AllOps[F, A] {
type TypeClassType = Applicative[F]
} =
new AllOps[F, A] {
type TypeClassType = Applicative[F]
val self: F[A] = target
val typeClassInstance: TypeClassType = tc
}
} = new AllOps[F, A] {
type TypeClassType = Applicative[F]
val self: F[A] = target
val typeClassInstance: TypeClassType = tc
}
}
trait Ops[F[_], A] extends Serializable {
type TypeClassType <: Applicative[F]
Expand All @@ -261,12 +259,11 @@ object Applicative {
trait ToApplicativeOps extends Serializable {
implicit def toApplicativeOps[F[_], A](target: F[A])(implicit tc: Applicative[F]): Ops[F, A] {
type TypeClassType = Applicative[F]
} =
new Ops[F, A] {
type TypeClassType = Applicative[F]
val self: F[A] = target
val typeClassInstance: TypeClassType = tc
}
} = new Ops[F, A] {
type TypeClassType = Applicative[F]
val self: F[A] = target
val typeClassInstance: TypeClassType = tc
}
}
@deprecated("Use cats.syntax object imports", "2.2.0")
object nonInheritedOps extends ToApplicativeOps
Expand Down
22 changes: 10 additions & 12 deletions core/src/main/scala/cats/Apply.scala
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,11 @@ object Apply {
object ops {
implicit def toAllApplyOps[F[_], A](target: F[A])(implicit tc: Apply[F]): AllOps[F, A] {
type TypeClassType = Apply[F]
} =
new AllOps[F, A] {
type TypeClassType = Apply[F]
val self: F[A] = target
val typeClassInstance: TypeClassType = tc
}
} = new AllOps[F, A] {
type TypeClassType = Apply[F]
val self: F[A] = target
val typeClassInstance: TypeClassType = tc
}
}
trait Ops[F[_], A] extends Serializable {
type TypeClassType <: Apply[F]
Expand All @@ -319,12 +318,11 @@ object Apply {
trait ToApplyOps extends Serializable {
implicit def toApplyOps[F[_], A](target: F[A])(implicit tc: Apply[F]): Ops[F, A] {
type TypeClassType = Apply[F]
} =
new Ops[F, A] {
type TypeClassType = Apply[F]
val self: F[A] = target
val typeClassInstance: TypeClassType = tc
}
} = new Ops[F, A] {
type TypeClassType = Apply[F]
val self: F[A] = target
val typeClassInstance: TypeClassType = tc
}
}
@deprecated("Use cats.syntax object imports", "2.2.0")
object nonInheritedOps extends ToApplyOps
Expand Down
14 changes: 14 additions & 0 deletions core/src/main/scala/cats/EitherUtil.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cats

/**
* Convenience methods and values for Either.
*/
private[cats] object EitherUtil {
def leftCast[A, B, C](right: Right[A, B]): Either[C, B] =
right.asInstanceOf[Either[C, B]]
def rightCast[A, B, C](left: Left[A, B]): Either[A, C] =
left.asInstanceOf[Either[A, C]]

val unit = Right(())
val leftUnit = Left(())
}
32 changes: 18 additions & 14 deletions core/src/main/scala/cats/Monad.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ import scala.annotation.implicitNotFound
* Must obey the laws defined in cats.laws.MonadLaws.
*/
@implicitNotFound("Could not find an instance of Monad for ${F}")
@typeclass trait Monad[F[_]] extends FlatMap[F] with Applicative[F] {
@typeclass trait Monad[F[_]] extends FlatMap[F] with RigidSelective[F] {
override def map[A, B](fa: F[A])(f: A => B): F[B] =
flatMap(fa)(a => pure(f(a)))

override def select[A, B](fab: F[Either[A, B]])(ff: => F[A => B]): F[B] =
flatMap(fab) {
case Left(a) => map(ff)(_(a))
case Right(b) => pure(b)
}

/**
* Execute an action repeatedly as long as the given `Boolean` expression
* returns `true`. The condition is evaluated before the loop body.
Expand Down Expand Up @@ -161,12 +167,11 @@ object Monad {
object ops {
implicit def toAllMonadOps[F[_], A](target: F[A])(implicit tc: Monad[F]): AllOps[F, A] {
type TypeClassType = Monad[F]
} =
new AllOps[F, A] {
type TypeClassType = Monad[F]
val self: F[A] = target
val typeClassInstance: TypeClassType = tc
}
} = new AllOps[F, A] {
type TypeClassType = Monad[F]
val self: F[A] = target
val typeClassInstance: TypeClassType = tc
}
}
trait Ops[F[_], A] extends Serializable {
type TypeClassType <: Monad[F]
Expand All @@ -178,18 +183,17 @@ object Monad {
def iterateWhile(p: A => Boolean): F[A] = typeClassInstance.iterateWhile[A](self)(p)
def iterateUntil(p: A => Boolean): F[A] = typeClassInstance.iterateUntil[A](self)(p)
}
trait AllOps[F[_], A] extends Ops[F, A] with FlatMap.AllOps[F, A] with Applicative.AllOps[F, A] {
trait AllOps[F[_], A] extends Ops[F, A] with FlatMap.AllOps[F, A] with RigidSelective.AllOps[F, A] {
type TypeClassType <: Monad[F]
}
trait ToMonadOps extends Serializable {
implicit def toMonadOps[F[_], A](target: F[A])(implicit tc: Monad[F]): Ops[F, A] {
type TypeClassType = Monad[F]
} =
new Ops[F, A] {
type TypeClassType = Monad[F]
val self: F[A] = target
val typeClassInstance: TypeClassType = tc
}
} = new Ops[F, A] {
type TypeClassType = Monad[F]
val self: F[A] = target
val typeClassInstance: TypeClassType = tc
}
}
@deprecated("Use cats.syntax object imports", "2.2.0")
object nonInheritedOps extends ToMonadOps
Expand Down
53 changes: 53 additions & 0 deletions core/src/main/scala/cats/RigidSelective.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package cats

import simulacrum.typeclass
import scala.annotation.implicitNotFound

@implicitNotFound("Could not find an instance of RigidSelective for ${F}")
@typeclass trait RigidSelective[F[_]] extends Selective[F]

object RigidSelective {
/* ======================================================================== */
/* THE FOLLOWING CODE IS MANAGED BY SIMULACRUM; PLEASE DO NOT EDIT!!!! */
/* ======================================================================== */

/**
* Summon an instance of [[RigidSelective]] for `F`.
*/
@inline def apply[F[_]](implicit instance: RigidSelective[F]): RigidSelective[F] = instance

@deprecated("Use cats.syntax object imports", "2.2.0")
object ops {
implicit def toAllRigidSelectiveOps[F[_], A](target: F[A])(implicit tc: RigidSelective[F]): AllOps[F, A] {
type TypeClassType = RigidSelective[F]
} = new AllOps[F, A] {
type TypeClassType = RigidSelective[F]
val self: F[A] = target
val typeClassInstance: TypeClassType = tc
}
}
trait Ops[F[_], A] extends Serializable {
type TypeClassType <: RigidSelective[F]
def self: F[A]
val typeClassInstance: TypeClassType
}
trait AllOps[F[_], A] extends Ops[F, A] with Selective.AllOps[F, A] {
type TypeClassType <: RigidSelective[F]
}
trait ToRigidSelectiveOps extends Serializable {
implicit def toRigidSelectiveOps[F[_], A](target: F[A])(implicit tc: RigidSelective[F]): Ops[F, A] {
type TypeClassType = RigidSelective[F]
} = new Ops[F, A] {
type TypeClassType = RigidSelective[F]
val self: F[A] = target
val typeClassInstance: TypeClassType = tc
}
}
@deprecated("Use cats.syntax object imports", "2.2.0")
object nonInheritedOps extends ToRigidSelectiveOps

/* ======================================================================== */
/* END OF SIMULACRUM-MANAGED CODE */
/* ======================================================================== */

}
66 changes: 66 additions & 0 deletions core/src/main/scala/cats/Selective.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package cats

import simulacrum.typeclass
import scala.annotation.implicitNotFound

@implicitNotFound("Could not find an instance of Selective for ${F}")
@typeclass trait Selective[F[_]] extends Applicative[F] {
rossabaker marked this conversation as resolved.
Show resolved Hide resolved
def select[A, B](fab: F[Either[A, B]])(ff: => F[A => B]): F[B]

def branch[A, B, C](fab: F[Either[A, B]])(fl: => F[A => C])(fr: => F[B => C]): F[C] = {
val innerLhs: F[Either[A, Either[B, C]]] = map(fab)(_.map(Left(_)))
def innerRhs: F[A => Either[B, C]] = map(fl)(_.andThen(Right(_)))
val lhs = select(innerLhs)(innerRhs)
select(lhs)(fr)
}
}

object Selective {
/* ======================================================================== */
/* THE FOLLOWING CODE IS MANAGED BY SIMULACRUM; PLEASE DO NOT EDIT!!!! */
/* ======================================================================== */

/**
* Summon an instance of [[Selective]] for `F`.
*/
@inline def apply[F[_]](implicit instance: Selective[F]): Selective[F] = instance

@deprecated("Use cats.syntax object imports", "2.2.0")
object ops {
implicit def toAllSelectiveOps[F[_], A](target: F[A])(implicit tc: Selective[F]): AllOps[F, A] {
type TypeClassType = Selective[F]
} = new AllOps[F, A] {
type TypeClassType = Selective[F]
val self: F[A] = target
val typeClassInstance: TypeClassType = tc
}
}
trait Ops[F[_], A] extends Serializable {
type TypeClassType <: Selective[F]
def self: F[A]
val typeClassInstance: TypeClassType
def select[B, C](ff: => F[B => C])(implicit ev$1: A <:< Either[B, C]): F[C] =
typeClassInstance.select[B, C](self.asInstanceOf[F[Either[B, C]]])(ff)
def branch[B, C, D](fl: => F[B => D])(fr: => F[C => D])(implicit ev$1: A <:< Either[B, C]): F[D] =
typeClassInstance.branch[B, C, D](self.asInstanceOf[F[Either[B, C]]])(fl)(fr)
}
trait AllOps[F[_], A] extends Ops[F, A] with Applicative.AllOps[F, A] {
type TypeClassType <: Selective[F]
}
trait ToSelectiveOps extends Serializable {
implicit def toSelectiveOps[F[_], A](target: F[A])(implicit tc: Selective[F]): Ops[F, A] {
type TypeClassType = Selective[F]
} = new Ops[F, A] {
type TypeClassType = Selective[F]
val self: F[A] = target
val typeClassInstance: TypeClassType = tc
}
}
@deprecated("Use cats.syntax object imports", "2.2.0")
object nonInheritedOps extends ToSelectiveOps

/* ======================================================================== */
/* END OF SIMULACRUM-MANAGED CODE */
/* ======================================================================== */

}
6 changes: 3 additions & 3 deletions core/src/main/scala/cats/data/EitherT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cats
package data

import cats.Bifunctor
import cats.syntax.EitherUtil
import cats.EitherUtil

/**
* Transformer for `Either`, allowing the effect of an arbitrary type constructor `F` to be combined with the
Expand Down Expand Up @@ -922,7 +922,7 @@ abstract private[data] class EitherTInstances extends EitherTInstances1 {
implicit val monadEither: Monad[Either[E, *]] = cats.instances.either.catsStdInstancesForEither

def applicative: Applicative[Nested[P.F, Validated[E, *], *]] =
cats.data.Nested.catsDataApplicativeForNested(P.applicative, Validated.catsDataApplicativeErrorForValidated)
cats.data.Nested.catsDataApplicativeForNested(P.applicative, Validated.catsDataSelectiveErrorForValidated)

def monad: Monad[EitherT[M, E, *]] = cats.data.EitherT.catsDataMonadErrorForEitherT

Expand Down Expand Up @@ -983,7 +983,7 @@ abstract private[data] class EitherTInstances1 extends EitherTInstances2 {
new Parallel[EitherT[M, E, *]] {
type F[x] = Nested[M, Validated[E, *], x]

implicit val appValidated: Applicative[Validated[E, *]] = Validated.catsDataApplicativeErrorForValidated
implicit val appValidated: Applicative[Validated[E, *]] = Validated.catsDataSelectiveErrorForValidated
implicit val monadEither: Monad[Either[E, *]] = cats.instances.either.catsStdInstancesForEither

def applicative: Applicative[Nested[M, Validated[E, *], *]] =
Expand Down
32 changes: 30 additions & 2 deletions core/src/main/scala/cats/data/Func.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,36 @@ object Func extends FuncInstances {
}

abstract private[data] class FuncInstances extends FuncInstances0 {
implicit def catsDataRigidSelectiveForFunc[F[_], C](implicit
FF: RigidSelective[F]
): RigidSelective[λ[α => Func[F, C, α]]] =
new FuncRigidSelective[F, C] {
def F: RigidSelective[F] = FF
}
}

abstract private[data] class FuncInstances0 extends FuncInstances1 {
implicit def catsDataSelectiveForFunc[F[_], C](implicit FF: Selective[F]): Selective[λ[α => Func[F, C, α]]] =
new FuncSelective[F, C] {
def F: Selective[F] = FF
}
}

abstract private[data] class FuncInstances1 extends FuncInstances2 {
implicit def catsDataApplicativeForFunc[F[_], C](implicit FF: Applicative[F]): Applicative[λ[α => Func[F, C, α]]] =
new FuncApplicative[F, C] {
def F: Applicative[F] = FF
}
}

abstract private[data] class FuncInstances0 extends FuncInstances1 {
abstract private[data] class FuncInstances2 extends FuncInstances3 {
implicit def catsDataApplyForFunc[F[_], C](implicit FF: Apply[F]): Apply[λ[α => Func[F, C, α]]] =
new FuncApply[F, C] {
def F: Apply[F] = FF
}
}

abstract private[data] class FuncInstances1 {
abstract private[data] class FuncInstances3 {
implicit def catsDataFunctorForFunc[F[_], C](implicit FF: Functor[F]): Functor[λ[α => Func[F, C, α]]] =
new FuncFunctor[F, C] {
def F: Functor[F] = FF
Expand Down Expand Up @@ -95,6 +111,18 @@ sealed private[data] trait FuncApplicative[F[_], C] extends Applicative[λ[α =>
Func.func(c => F.pure(a))
}

sealed private[data] trait FuncSelective[F[_], C] extends Selective[λ[α => Func[F, C, α]]] with FuncApplicative[F, C] {
def F: Selective[F]
def select[A, B](fab: Func[F, C, Either[A, B]])(ff: => Func[F, C, A => B]): Func[F, C, B] =
Func.func(c => F.select(fab.run(c))(ff.run(c)))
}

sealed private[data] trait FuncRigidSelective[F[_], C]
extends RigidSelective[λ[α => Func[F, C, α]]]
with FuncSelective[F, C] {
def F: RigidSelective[F]
}

/**
* An implementation of [[Func]] that's specialized to [[Applicative]].
*/
Expand Down
Loading