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 8 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
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/FlatMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import scala.annotation.implicitNotFound
* Must obey the laws defined in cats.laws.FlatMapLaws.
*/
@implicitNotFound("Could not find an instance of FlatMap for ${F}")
@typeclass trait FlatMap[F[_]] extends Apply[F] {
@typeclass trait FlatMap[F[_]] extends Select[F] {
def flatMap[A, B](fa: F[A])(f: A => F[B]): F[B]

/**
Expand Down
8 changes: 7 additions & 1 deletion 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 Selective[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] =
rossabaker marked this conversation as resolved.
Show resolved Hide resolved
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
8 changes: 1 addition & 7 deletions core/src/main/scala/cats/MonadError.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@ package cats
*
* This type class allows one to abstract over error-handling monads.
*/
trait MonadError[F[_], E] extends ApplicativeError[F, E] with Monad[F] {

/**
* Turns a successful value into an error if it does not satisfy a given predicate.
*/
def ensure[A](fa: F[A])(error: => E)(predicate: A => Boolean): F[A] =
flatMap(fa)(a => if (predicate(a)) pure(a) else raiseError(error))
trait MonadError[F[_], E] extends SelectiveError[F, E] with Monad[F] {

/**
* Turns a successful value into an error specified by the `error` function if it does not satisfy a given predicate.
Expand Down
13 changes: 13 additions & 0 deletions core/src/main/scala/cats/Select.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cats

import simulacrum.{typeclass}
import scala.annotation.implicitNotFound

@implicitNotFound("Could not find an instance of Select for ${F}")
@typeclass trait Select[F[_]] extends Apply[F] {
def select[A, B](fab: F[Either[A, B]])(ff: F[A => B]): F[B] =
map2(fab, ff) {
rossabaker marked this conversation as resolved.
Show resolved Hide resolved
case (Left(a), f) => f(a)
case (Right(b), f) => b
}
}
7 changes: 7 additions & 0 deletions core/src/main/scala/cats/Selective.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
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] with Select[F]
rossabaker marked this conversation as resolved.
Show resolved Hide resolved
15 changes: 15 additions & 0 deletions core/src/main/scala/cats/SelectiveError.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cats

import cats.syntax.either._
rossabaker marked this conversation as resolved.
Show resolved Hide resolved

trait SelectiveError[F[_], E] extends ApplicativeError[F, E] with Selective[F] {
rossabaker marked this conversation as resolved.
Show resolved Hide resolved

/**
* Turns a successful value into an error if it does not satisfy a given predicate.
*/
def ensure[A](fa: F[A])(error: => E)(predicate: A => Boolean): F[A] =
rossabaker marked this conversation as resolved.
Show resolved Hide resolved
select(map(fa) { (a: A) =>
if (predicate(a)) Right(a)
else Either.leftUnit
})(raiseError(error))
}
4 changes: 2 additions & 2 deletions core/src/main/scala/cats/data/EitherT.scala
Original file line number Diff line number Diff line change
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
28 changes: 22 additions & 6 deletions core/src/main/scala/cats/data/Validated.scala
Original file line number Diff line number Diff line change
Expand Up @@ -931,16 +931,19 @@ sealed abstract private[data] class ValidatedInstances extends ValidatedInstance
fab.leftMap(f)
}

implicit def catsDataApplicativeErrorForValidated[E](implicit E: Semigroup[E]): ApplicativeError[Validated[E, *], E] =
new ValidatedApplicative[E] with ApplicativeError[Validated[E, *], E] {

implicit def catsDataSelectiveErrorForValidated[E](implicit E: Semigroup[E]): SelectiveError[Validated[E, *], E] =
new ValidatedSelective[E] with SelectiveError[Validated[E, *], E] {
def handleErrorWith[A](fa: Validated[E, A])(f: E => Validated[E, A]): Validated[E, A] =
fa match {
case Validated.Invalid(e) => f(e)
case v @ Validated.Valid(_) => v
}
def raiseError[A](e: E): Validated[E, A] = Validated.Invalid(e)
}

@deprecated("Use catsDataSelectiveErrorForValidated", "2.4.0")
def catsDataApplicativeErrorForValidated[E](implicit E: Semigroup[E]): ApplicativeError[Validated[E, *], E] =
catsDataSelectiveErrorForValidated
}

sealed abstract private[data] class ValidatedInstances1 extends ValidatedInstances2 {
Expand All @@ -953,9 +956,13 @@ sealed abstract private[data] class ValidatedInstances1 extends ValidatedInstanc
def combine(x: Validated[A, B], y: Validated[A, B]): Validated[A, B] = x.combine(y)
}

implicit def catsDataCommutativeApplicativeForValidated[E: CommutativeSemigroup]
: CommutativeApplicative[Validated[E, *]] =
new ValidatedApplicative[E] with CommutativeApplicative[Validated[E, *]]
implicit def catsDataCommutativeSelectiveForValidated[E: CommutativeSemigroup]
: Selective[Validated[E, *]] with CommutativeApplicative[Validated[E, *]] =
new ValidatedSelective[E] with CommutativeApplicative[Validated[E, *]]

@deprecated("Use catsDataCommutativeSelectiveForValidated", "2.4.0")
def catsDataCommutativeApplicativeForValidated[E: CommutativeSemigroup]: CommutativeApplicative[Validated[E, *]] =
Copy link
Member Author

Choose a reason for hiding this comment

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

If there's a CommutativeApplicative and a CommutativeMonad, we need to spend a few moments thinking about whether there's a CommutativeSelective.

catsDataCommutativeApplicativeForValidated

implicit def catsDataPartialOrderForValidated[A: PartialOrder, B: PartialOrder]: PartialOrder[Validated[A, B]] =
new PartialOrder[Validated[A, B]] {
Expand Down Expand Up @@ -1035,6 +1042,15 @@ sealed abstract private[data] class ValidatedInstances2 {
// scalastyle:off method.length
}

private[data] class ValidatedSelective[E: Semigroup] extends ValidatedApplicative[E] with Selective[Validated[E, *]] {
override def select[A, B](fab: Validated[E, Either[A, B]])(ff: Validated[E, A => B]): Validated[E, B] =
rossabaker marked this conversation as resolved.
Show resolved Hide resolved
rossabaker marked this conversation as resolved.
Show resolved Hide resolved
fab match {
case Valid(Left(a)) => ff.map(_(a))
case Valid(Right(b)) => Valid(b)
case i @ Invalid(_) => i
}
}

private[data] class ValidatedApplicative[E: Semigroup] extends CommutativeApplicative[Validated[E, *]] {
override def map[A, B](fa: Validated[E, A])(f: A => B): Validated[E, B] =
fa.map(f)
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/instances/either.scala
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ trait EitherInstances extends cats.kernel.instances.EitherInstances {
new Parallel[Either[E, *]] {
type F[x] = Validated[E, x]

def applicative: Applicative[Validated[E, *]] = Validated.catsDataApplicativeErrorForValidated
def applicative: Applicative[Validated[E, *]] = Validated.catsDataSelectiveErrorForValidated
def monad: Monad[Either[E, *]] = cats.instances.either.catsStdInstancesForEither

def sequential: Validated[E, *] ~> Either[E, *] =
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/scala/cats/syntax/either.scala
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,11 @@ final class EitherObjectOps(private val either: Either.type) extends AnyVal { //
* Cached value of `Right(())` to avoid allocations for a common case.
*/
def unit[A]: Either[A, Unit] = EitherUtil.unit

/**
* Cached value of `Left(())` to avoid allocations for a common case.
*/
def leftUnit[B]: Either[Unit, B] = EitherUtil.leftUnit
rossabaker marked this conversation as resolved.
Show resolved Hide resolved
}

final class LeftOps[A, B](private val left: Left[A, B]) extends AnyVal {
Expand Down Expand Up @@ -509,4 +514,5 @@ private[cats] object EitherUtil {
left.asInstanceOf[Either[A, C]]

private[cats] val unit = Right(())
private[cats] val leftUnit = Left(())
rossabaker marked this conversation as resolved.
Show resolved Hide resolved
}