radn-rs/src/func/instances/result.rs
2023-05-26 09:25:58 +00:00

247 lines
6.4 KiB
Rust

//! Implementation of [`MonadFail<E>`] for [`Result<A, E>`].
//!
//! If any of the input values are [`Err`], you can expect the output to be [`Err`] as well.
//! That includes
//! [`ResultInstance::replace`] and [`ResultInstance::discard_first`]/[`ResultInstance::discard_second`],
//! even if the value of the option would be ignored.
//!
//! For [`Option<A>`] alternative see [`super::option`]
use crate::func::*;
#[derive(SharedFunctor, CovariantFunctor)]
pub struct ResultInstance<E>(E);
impl<E> WeakFunctor for ResultInstance<E> {
type F<'a, A: 'a> = Result<A, E> where Self: 'a;
}
impl<E> Functor for ResultInstance<E> {
fn fmap<'a, A: 'a, B: 'a>(f: impl 'a + FnOnce(A) -> B, fa: Self::F<'a, A>) -> Self::F<'a, B>
where
Self: 'a,
{
fa.map(f)
}
fn replace<'a, A: 'a, B: 'a>(fa: Self::F<'a, A>, b: B) -> Self::F<'a, B>
where
Self: 'a,
{
fa?;
Self::pure(b)
}
fn void<'a, A: 'a>(fa: Self::F<'a, A>) -> Self::F<'a, ()>
where
Self: 'a,
{
fa?;
Self::pure(())
}
}
impl<'a, E: 'a> Pure<'a> for ResultInstance<E> {
fn pure<A: 'a>(a: A) -> Self::F<'a, A> {
Ok(a)
}
}
impl<'a, E: 'a> ApplicativeSeq<'a> for ResultInstance<E> {
fn seq<A: 'a, B: 'a>(
ff: Self::F<'a, impl 'a + FnOnce(A) -> B>,
fa: Self::F<'a, A>,
) -> Self::F<'a, B> {
Self::pure(ff?(fa?))
}
}
impl<'a, E: 'a> ApplicativeLA2<'a> for ResultInstance<E> {
fn la2<A: 'a, B: 'a, C: 'a>(
f: impl 'a + FnOnce(A, B) -> C,
fa: Self::F<'a, A>,
fb: Self::F<'a, B>,
) -> Self::F<'a, C> {
Self::pure(f(fa?, fb?))
}
}
impl<'a, E: 'a> ApplicativeTuple<'a> for ResultInstance<E> {
fn tuple<A: 'a, B: 'a>((fa, fb): (Self::F<'a, A>, Self::F<'a, B>)) -> Self::F<'a, (A, B)> {
Self::pure((fa?, fb?))
}
}
impl<'a, E: 'a> ApplicativeSelect<'a> for ResultInstance<E> {}
impl<'a, E: 'a> Applicative<'a> for ResultInstance<E> {
fn discard_first<A: 'a, B: 'a>(fa: Self::F<'a, A>, fb: Self::F<'a, B>) -> Self::F<'a, B> {
fa?;
fb
}
fn discard_second<A: 'a, B: 'a>(fa: Self::F<'a, A>, fb: Self::F<'a, B>) -> Self::F<'a, A> {
fb?;
fa
}
}
impl<'a, E: 'a> Monad<'a> for ResultInstance<E> {
fn bind<A: 'a, B: 'a>(
fa: Self::F<'a, A>,
f: impl 'a + FnOnce(A) -> Self::F<'a, B>,
) -> Self::F<'a, B> {
f(fa?)
}
fn iterate<B: 'a>(mut f: impl Iterative<'a, T = Self, B = B>) -> Self::F<'a, B> {
loop {
match f.next()? {
ControlFlow::Continue(next_f) => f = next_f,
ControlFlow::Break(b) => return Self::pure(b),
}
}
}
fn join<A: 'a>(ffa: Self::F<'a, Self::F<'a, A>>) -> Self::F<'a, A> {
ffa?
}
}
impl<E> LocalFunctor for ResultInstance<E> {
fn unstuff<'a, A: 'a, B: 'a>(
state: Self::F<'a, ControlFlow<B, A>>,
) -> ControlFlow<Self::F<'a, B>, A>
where
Self: 'a,
{
match state {
Ok(ControlFlow::Continue(a)) => ControlFlow::Continue(a),
Ok(ControlFlow::Break(b)) => ControlFlow::Break(Ok(b)),
Err(e) => ControlFlow::Break(Err(e)),
}
}
fn stuff<'a, A: 'a, T: Pure<'a>>(fa: Self::F<'a, T::F<'a, A>>) -> T::F<'a, Self::F<'a, A>>
where
Self: 'a,
{
match fa {
Ok(ua) => T::fmap(Ok, ua),
Err(e) => T::pure(Err(e)),
}
}
}
impl<'a, E: 'a> Fail<'a, E> for ResultInstance<E> {
fn fail<A: 'a>(e: E) -> Self::F<'a, A> {
Err(e)
}
}
pub struct ResultFailAny;
trait ResultExt<'a, A: 'a, E0: 'a>: 'a {
fn bind_err<E1: 'a>(self, f: impl 'a + FnOnce(E0) -> Result<A, E1>) -> Result<A, E1>;
}
impl<'a, A: 'a, E0: 'a> ResultExt<'a, A, E0> for Result<A, E0> {
fn bind_err<E1: 'a>(self, f: impl 'a + FnOnce(E0) -> Result<A, E1>) -> Result<A, E1> {
match self {
Ok(a) => Ok(a),
Err(e) => f(e),
}
}
}
impl<'a> MonadFailAny<'a> for ResultFailAny {
type W<E: 'a> = ResultInstance<E>;
type T = instances::solo::SoloInstance;
fn unstuff<A: 'a, E: 'a>(wa: WrapE<'a, A, E, Self>) -> Wrap<'a, Result<A, E>, Self::T> {
wa
}
fn stuff<A: 'a, E: 'a>(fa: Wrap<'a, Result<A, E>, Self::T>) -> WrapE<'a, A, E, Self> {
fa
}
fn map_err<A: 'a, E0: 'a, E1: 'a>(
wa: WrapE<'a, A, E0, Self>,
f: impl 'a + FnOnce(E0) -> E1,
) -> WrapE<'a, A, E1, Self> {
wa.map_err(f)
}
fn bind_err<A: 'a, E0: 'a, E1: 'a>(
wa: WrapE<'a, A, E0, Self>,
f: impl 'a + FnOnce(E0) -> WrapE<'a, A, E1, Self>,
) -> WrapE<'a, A, E1, Self> {
wa.bind_err(f)
}
fn bind<A: 'a, B: 'a, E0: 'a, E1: 'a>(
wa: WrapE<'a, A, E0, Self>,
f: impl 'a + FnOnce(Result<A, E0>) -> WrapE<'a, B, E1, Self>,
) -> WrapE<'a, B, E1, Self> {
f(wa)
}
fn rotate_out<A: 'a, E0: 'a, E1: 'a>(
wa: WrapE<'a, Result<A, E1>, E0, Self>,
) -> WrapE<'a, A, Result<E1, E0>, Self> {
match wa {
Ok(Ok(a)) => Ok(a),
Ok(Err(e)) => Err(Ok(e)),
Err(e) => Err(Err(e)),
}
}
}
pub struct ResultFailOver<T>(T);
impl<'a, T: Monad<'a>> MonadFailAny<'a> for ResultFailOver<T> {
type W<E: 'a> = super::composition::CompositionInstance<T, ResultInstance<E>>;
type T = T;
fn unstuff<A: 'a, E: 'a>(wa: WrapE<'a, A, E, Self>) -> Wrap<'a, Result<A, E>, Self::T> {
wa
}
fn stuff<A: 'a, E: 'a>(fa: Wrap<'a, Result<A, E>, Self::T>) -> WrapE<'a, A, E, Self> {
fa
}
fn map_err<A: 'a, E0: 'a, E1: 'a>(
wa: WrapE<'a, A, E0, Self>,
f: impl 'a + FnOnce(E0) -> E1,
) -> WrapE<'a, A, E1, Self> {
T::fmap(|a| a.map_err(f), wa)
}
fn bind_err<A: 'a, E0: 'a, E1: 'a>(
wa: WrapE<'a, A, E0, Self>,
f: impl 'a + FnOnce(E0) -> WrapE<'a, A, E1, Self>,
) -> WrapE<'a, A, E1, Self> {
T::bind(wa, |a| match a {
Ok(a) => T::pure(Ok(a)),
Err(e) => f(e),
})
}
fn bind<A: 'a, B: 'a, E0: 'a, E1: 'a>(
wa: WrapE<'a, A, E0, Self>,
f: impl 'a + FnOnce(Result<A, E0>) -> WrapE<'a, B, E1, Self>,
) -> WrapE<'a, B, E1, Self> {
T::bind(wa, f)
}
fn rotate_out<A: 'a, E0: 'a, E1: 'a>(
wa: WrapE<'a, Result<A, E1>, E0, Self>,
) -> WrapE<'a, A, Result<E1, E0>, Self> {
T::fmap(<ResultFailAny as MonadFailAny>::rotate_out, wa)
}
}