This commit is contained in:
AF 2023-05-20 06:02:51 +00:00
parent 08d0404ee5
commit f1cdbcc2d0
6 changed files with 15 additions and 10 deletions

View File

@ -223,10 +223,8 @@ pub trait Monad: Applicative {
} }
} }
/// Equivalent of Haskell's `MonadFail`. /// Part of [`MonadFail`] responsible for Haskell's `fail`.
/// pub trait Fail<E>: WeakFunctor {
/// <https://hackage.haskell.org/package/base-4.18.0.0/docs/Control-Monad.html>
pub trait MonadFail<E>: Monad {
/// Equivalent of Haskell's `fail`. /// Equivalent of Haskell's `fail`.
fn fail<'a, A: 'a>(e: E) -> Self::F<'a, A> fn fail<'a, A: 'a>(e: E) -> Self::F<'a, A>
where where
@ -234,6 +232,13 @@ pub trait MonadFail<E>: Monad {
E: 'a; E: 'a;
} }
/// Equivalent of Haskell's `MonadFail`. Auto-implemented for all [`Fail`]`+`[`Monad`].
///
/// <https://hackage.haskell.org/package/base-4.18.0.0/docs/Control-Monad.html>
pub trait MonadFail<E>: Monad + Fail<E> {}
impl<E, T: Monad + Fail<E>> MonadFail<E> for T {}
/// Equivalent of Haskell's `Alternative`. /// Equivalent of Haskell's `Alternative`.
/// Lacks `some`/`many` definitions due to [`FnOnce`] category semantics. /// Lacks `some`/`many` definitions due to [`FnOnce`] category semantics.
/// ///
@ -296,7 +301,7 @@ pub trait MonadFailAny {
{ {
<Self::W<Result<E1, E0>> as Monad>::bind(Self::map_err(wa, Err), |fa| match fa { <Self::W<Result<E1, E0>> as Monad>::bind(Self::map_err(wa, Err), |fa| match fa {
Ok(a) => <Self::W<Result<E1, E0>> as Pure>::pure(a), Ok(a) => <Self::W<Result<E1, E0>> as Pure>::pure(a),
Err(e) => <Self::W<Result<E1, E0>> as MonadFail<Result<E1, E0>>>::fail(Ok(e)), Err(e) => <Self::W<Result<E1, E0>> as Fail<Result<E1, E0>>>::fail(Ok(e)),
}) })
} }
} }

View File

@ -189,7 +189,7 @@ impl<
} }
} }
impl<E, U: Monad, V: MonadFail<E> + LocalFunctor> MonadFail<E> for CompositionClass<U, V> { impl<E, U: Monad, V: Fail<E> + LocalFunctor> Fail<E> for CompositionClass<U, V> {
fn fail<'a, A: 'a>(e: E) -> Self::F<'a, A> fn fail<'a, A: 'a>(e: E) -> Self::F<'a, A>
where where
Self: 'a, Self: 'a,

View File

@ -164,7 +164,7 @@ impl LocalFunctor for OptionClass {
} }
} }
impl MonadFail<()> for OptionClass { impl Fail<()> for OptionClass {
fn fail<'a, A: 'a>(_e: ()) -> Self::F<'a, A> fn fail<'a, A: 'a>(_e: ()) -> Self::F<'a, A>
where where
Self: 'a, Self: 'a,

View File

@ -188,7 +188,7 @@ impl<E> LocalFunctor for ResultClass<E> {
} }
} }
impl<E> MonadFail<E> for ResultClass<E> { impl<E> Fail<E> for ResultClass<E> {
fn fail<'a, A: 'a>(e: E) -> Self::F<'a, A> fn fail<'a, A: 'a>(e: E) -> Self::F<'a, A>
where where
Self: 'a, Self: 'a,

View File

@ -145,7 +145,7 @@ impl LocalFunctor for SoloClass {
} }
} }
impl MonadFail<std::convert::Infallible> for SoloClass { impl Fail<std::convert::Infallible> for SoloClass {
fn fail<'a, A: 'a>(e: std::convert::Infallible) -> Self::F<'a, A> fn fail<'a, A: 'a>(e: std::convert::Infallible) -> Self::F<'a, A>
where where
Self: 'a, Self: 'a,

View File

@ -182,7 +182,7 @@ impl<E: Clone> SharedFunctor for TryFutureClass<E> {
} }
} }
impl<E> MonadFail<E> for TryFutureClass<E> { impl<E> Fail<E> for TryFutureClass<E> {
fn fail<'a, A: 'a>(e: E) -> Self::F<'a, A> fn fail<'a, A: 'a>(e: E) -> Self::F<'a, A>
where where
Self: 'a, Self: 'a,