-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #812 from travisbrown/topic/applicative-error
Add ApplicativeError
- Loading branch information
Showing
8 changed files
with
204 additions
and
122 deletions.
There are no files selected for viewing
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,81 @@ | ||
package cats | ||
|
||
import cats.data.{Xor, XorT} | ||
|
||
/** | ||
* An applicative that also allows you to raise and or handle an error value. | ||
* | ||
* This type class allows one to abstract over error-handling applicatives. | ||
*/ | ||
trait ApplicativeError[F[_], E] extends Applicative[F] { | ||
/** | ||
* Lift an error into the `F` context. | ||
*/ | ||
def raiseError[A](e: E): F[A] | ||
|
||
/** | ||
* Handle any error, potentially recovering from it, by mapping it to an | ||
* `F[A]` value. | ||
* | ||
* @see [[handleError]] to handle any error by simply mapping it to an `A` | ||
* value instead of an `F[A]`. | ||
* | ||
* @see [[recoverWith]] to recover from only certain errors. | ||
*/ | ||
def handleErrorWith[A](fa: F[A])(f: E => F[A]): F[A] | ||
|
||
/** | ||
* Handle any error, by mapping it to an `A` value. | ||
* | ||
* @see [[handleErrorWith]] to map to an `F[A]` value instead of simply an | ||
* `A` value. | ||
* | ||
* @see [[recover]] to only recover from certain errors. | ||
*/ | ||
def handleError[A](fa: F[A])(f: E => A): F[A] = handleErrorWith(fa)(f andThen pure) | ||
|
||
/** | ||
* Handle errors by turning them into [[cats.data.Xor.Left]] values. | ||
* | ||
* If there is no error, then an [[cats.data.Xor.Right]] value will be returned instead. | ||
* | ||
* All non-fatal errors should be handled by this method. | ||
*/ | ||
def attempt[A](fa: F[A]): F[E Xor A] = handleErrorWith( | ||
map(fa)(Xor.right[E, A]) | ||
)(e => pure(Xor.left(e))) | ||
|
||
/** | ||
* Similar to [[attempt]], but wraps the result in a [[cats.data.XorT]] for | ||
* convenience. | ||
*/ | ||
def attemptT[A](fa: F[A]): XorT[F, E, A] = XorT(attempt(fa)) | ||
|
||
/** | ||
* Recover from certain errors by mapping them to an `A` value. | ||
* | ||
* @see [[handleError]] to handle any/all errors. | ||
* | ||
* @see [[recoverWith]] to recover from certain errors by mapping them to | ||
* `F[A]` values. | ||
*/ | ||
def recover[A](fa: F[A])(pf: PartialFunction[E, A]): F[A] = | ||
handleErrorWith(fa)(e => | ||
(pf andThen pure) applyOrElse(e, raiseError)) | ||
|
||
/** | ||
* Recover from certain errors by mapping them to an `F[A]` value. | ||
* | ||
* @see [[handleErrorWith]] to handle any/all errors. | ||
* | ||
* @see [[recover]] to recover from certain errors by mapping them to `A` | ||
* values. | ||
*/ | ||
def recoverWith[A](fa: F[A])(pf: PartialFunction[E, F[A]]): F[A] = | ||
handleErrorWith(fa)(e => | ||
pf applyOrElse(e, raiseError)) | ||
} | ||
|
||
object ApplicativeError { | ||
def apply[F[_], E](implicit F: ApplicativeError[F, E]): ApplicativeError[F, E] = F | ||
} |
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
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,44 @@ | ||
package cats | ||
package laws | ||
|
||
import cats.data.{Xor, XorT} | ||
|
||
// Taken from http://functorial.com/psc-pages/docs/Control/Monad/Error/Class/index.html | ||
trait ApplicativeErrorLaws[F[_], E] extends ApplicativeLaws[F] { | ||
implicit override def F: ApplicativeError[F, E] | ||
|
||
def applicativeErrorHandleWith[A](e: E, f: E => F[A]): IsEq[F[A]] = | ||
F.handleErrorWith(F.raiseError[A](e))(f) <-> f(e) | ||
|
||
def applicativeErrorHandle[A](e: E, f: E => A): IsEq[F[A]] = | ||
F.handleError(F.raiseError[A](e))(f) <-> F.pure(f(e)) | ||
|
||
def handleErrorWithPure[A](a: A, f: E => F[A]): IsEq[F[A]] = | ||
F.handleErrorWith(F.pure(a))(f) <-> F.pure(a) | ||
|
||
def handleErrorPure[A](a: A, f: E => A): IsEq[F[A]] = | ||
F.handleError(F.pure(a))(f) <-> F.pure(a) | ||
|
||
def raiseErrorAttempt(e: E): IsEq[F[E Xor Unit]] = | ||
F.attempt(F.raiseError[Unit](e)) <-> F.pure(Xor.left(e)) | ||
|
||
def pureAttempt[A](a: A): IsEq[F[E Xor A]] = | ||
F.attempt(F.pure(a)) <-> F.pure(Xor.right(a)) | ||
|
||
def handleErrorWithConsistentWithRecoverWith[A](fa: F[A], f: E => F[A]): IsEq[F[A]] = | ||
F.handleErrorWith(fa)(f) <-> F.recoverWith(fa)(PartialFunction(f)) | ||
|
||
def handleErrorConsistentWithRecover[A](fa: F[A], f: E => A): IsEq[F[A]] = | ||
F.handleError(fa)(f) <-> F.recover(fa)(PartialFunction(f)) | ||
|
||
def recoverConsistentWithRecoverWith[A](fa: F[A], pf: PartialFunction[E, A]): IsEq[F[A]] = | ||
F.recover(fa)(pf) <-> F.recoverWith(fa)(pf andThen F.pure) | ||
|
||
def attemptConsistentWithAttemptT[A](fa: F[A]): IsEq[XorT[F, E, A]] = | ||
XorT(F.attempt(fa)) <-> F.attemptT(fa) | ||
} | ||
|
||
object ApplicativeErrorLaws { | ||
def apply[F[_], E](implicit ev: ApplicativeError[F, E]): ApplicativeErrorLaws[F, E] = | ||
new ApplicativeErrorLaws[F, E] { def F: ApplicativeError[F, E] = ev } | ||
} |
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
57 changes: 57 additions & 0 deletions
57
laws/src/main/scala/cats/laws/discipline/ApplicativeErrorTests.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,57 @@ | ||
package cats | ||
package laws | ||
package discipline | ||
|
||
import cats.data.{ Xor, XorT } | ||
import cats.laws.discipline.MonoidalTests.Isomorphisms | ||
import cats.laws.discipline.arbitrary._ | ||
import cats.laws.discipline.eq.unitEq | ||
import org.scalacheck.{Arbitrary, Prop} | ||
import org.scalacheck.Prop.forAll | ||
|
||
trait ApplicativeErrorTests[F[_], E] extends ApplicativeTests[F] { | ||
def laws: ApplicativeErrorLaws[F, E] | ||
|
||
def applicativeError[A: Arbitrary: Eq, B: Arbitrary: Eq, C: Arbitrary: Eq](implicit | ||
ArbFA: Arbitrary[F[A]], | ||
ArbFB: Arbitrary[F[B]], | ||
ArbFC: Arbitrary[F[C]], | ||
ArbFAtoB: Arbitrary[F[A => B]], | ||
ArbFBtoC: Arbitrary[F[B => C]], | ||
ArbE: Arbitrary[E], | ||
EqFA: Eq[F[A]], | ||
EqFB: Eq[F[B]], | ||
EqFC: Eq[F[C]], | ||
EqE: Eq[E], | ||
EqFXorEU: Eq[F[E Xor Unit]], | ||
EqFXorEA: Eq[F[E Xor A]], | ||
EqXorTFEA: Eq[XorT[F, E, A]], | ||
EqFABC: Eq[F[(A, B, C)]], | ||
iso: Isomorphisms[F] | ||
): RuleSet = { | ||
new RuleSet { | ||
def name: String = "applicativeError" | ||
def bases: Seq[(String, RuleSet)] = Nil | ||
def parents: Seq[RuleSet] = Seq(applicative[A, B, C]) | ||
def props: Seq[(String, Prop)] = Seq( | ||
"applicativeError handleWith" -> forAll(laws.applicativeErrorHandleWith[A] _), | ||
"applicativeError handle" -> forAll(laws.applicativeErrorHandle[A] _), | ||
"applicativeError handleErrorWith pure" -> forAll(laws.handleErrorWithPure[A] _), | ||
"applicativeError handleError pure" -> forAll(laws.handleErrorPure[A] _), | ||
"applicativeError raiseError attempt" -> forAll(laws.raiseErrorAttempt _), | ||
"applicativeError pure attempt" -> forAll(laws.pureAttempt[A] _), | ||
"applicativeError handleErrorWith consistent with recoverWith" -> forAll(laws.handleErrorWithConsistentWithRecoverWith[A] _), | ||
"applicativeError handleError consistent with recover" -> forAll(laws.handleErrorConsistentWithRecover[A] _), | ||
"applicativeError recover consistent with recoverWith" -> forAll(laws.recoverConsistentWithRecoverWith[A] _), | ||
"applicativeError attempt consistent with attemptT" -> forAll(laws.attemptConsistentWithAttemptT[A] _) | ||
) | ||
} | ||
} | ||
} | ||
|
||
object ApplicativeErrorTests { | ||
def apply[F[_], E](implicit FE: ApplicativeError[F, E]): ApplicativeErrorTests[F, E] = | ||
new ApplicativeErrorTests[F, E] { | ||
def laws: ApplicativeErrorLaws[F, E] = ApplicativeErrorLaws[F, E] | ||
} | ||
} |
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